overloaded.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/as/overloaded.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/overloaded.carbon
  10. class X {
  11. var n: i32;
  12. }
  13. impl i32 as Core.As(X) {
  14. fn Convert[self: i32]() -> X { return {.n = self}; }
  15. }
  16. impl X as Core.As(i32) {
  17. fn Convert[self: X]() -> i32 { return self.n; }
  18. }
  19. let n: i32 = ((4 as i32) as X) as i32;
  20. // CHECK:STDOUT: --- overloaded.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %X: type = class_type @X [template]
  24. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  25. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  26. // CHECK:STDOUT: %X.elem: type = unbound_element_type %X, %i32 [template]
  27. // CHECK:STDOUT: %struct_type.n: type = struct_type {.n: %i32} [template]
  28. // CHECK:STDOUT: %complete_type.9b7: <witness> = complete_type_witness %struct_type.n [template]
  29. // CHECK:STDOUT: %As.type.90f: type = generic_interface_type @As [template]
  30. // CHECK:STDOUT: %As.generic: %As.type.90f = struct_value () [template]
  31. // CHECK:STDOUT: %As.type.678: type = facet_type <@As, @As(%X)> [template]
  32. // CHECK:STDOUT: %Convert.type.73d: type = fn_type @Convert.1, @As(%X) [template]
  33. // CHECK:STDOUT: %impl_witness.cf4: <witness> = impl_witness (@impl.1.%Convert.decl) [template]
  34. // CHECK:STDOUT: %Convert.type.5bf: type = fn_type @Convert.2 [template]
  35. // CHECK:STDOUT: %Convert.f4a: %Convert.type.5bf = struct_value () [template]
  36. // CHECK:STDOUT: %As.type.a50: type = facet_type <@As, @As(%i32)> [template]
  37. // CHECK:STDOUT: %Convert.type.1b9: type = fn_type @Convert.1, @As(%i32) [template]
  38. // CHECK:STDOUT: %impl_witness.d22: <witness> = impl_witness (@impl.2.%Convert.decl) [template]
  39. // CHECK:STDOUT: %Convert.type.45a: type = fn_type @Convert.3 [template]
  40. // CHECK:STDOUT: %Convert.5bc: %Convert.type.45a = struct_value () [template]
  41. // CHECK:STDOUT: %int_4.0c1: Core.IntLiteral = int_value 4 [template]
  42. // CHECK:STDOUT: %impl_witness.c9a: <witness> = impl_witness (imports.%import_ref.6fa), @impl.5(%int_32) [template]
  43. // CHECK:STDOUT: %Convert.type.531: type = fn_type @Convert.7, @impl.5(%int_32) [template]
  44. // CHECK:STDOUT: %Convert.6c4: %Convert.type.531 = struct_value () [template]
  45. // CHECK:STDOUT: %Convert.bound.416: <bound method> = bound_method %int_4.0c1, %Convert.6c4 [template]
  46. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound.416, @Convert.7(%int_32) [template]
  47. // CHECK:STDOUT: %int_4.2ec: %i32 = int_value 4 [template]
  48. // CHECK:STDOUT: %Convert.bound.5eb: <bound method> = bound_method %int_4.2ec, %Convert.f4a [template]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: imports {
  52. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  53. // CHECK:STDOUT: .Int = %import_ref.187
  54. // CHECK:STDOUT: .As = %import_ref.166
  55. // CHECK:STDOUT: import Core//prelude
  56. // CHECK:STDOUT: import Core//prelude/...
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %import_ref.166: %As.type.90f = import_ref Core//prelude/operators/as, As, loaded [template = constants.%As.generic]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Core = imports.%Core
  64. // CHECK:STDOUT: .X = %X.decl
  65. // CHECK:STDOUT: .n = @__global_init.%n
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Core.import = import Core
  68. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {} {}
  69. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  70. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  71. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  72. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  73. // CHECK:STDOUT: %As.ref: %As.type.90f = name_ref As, imports.%import_ref.166 [template = constants.%As.generic]
  74. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  75. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%X)> [template = constants.%As.type.678]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %impl_witness.loc15: <witness> = impl_witness (@impl.1.%Convert.decl) [template = constants.%impl_witness.cf4]
  78. // CHECK:STDOUT: impl_decl @impl.2 [template] {} {
  79. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  80. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  81. // CHECK:STDOUT: %As.ref: %As.type.90f = name_ref As, imports.%import_ref.166 [template = constants.%As.generic]
  82. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  83. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  84. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%i32)> [template = constants.%As.type.a50]
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %impl_witness.loc19: <witness> = impl_witness (@impl.2.%Convert.decl) [template = constants.%impl_witness.d22]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: impl @impl.1: %i32 as %As.type {
  90. // CHECK:STDOUT: %Convert.decl: %Convert.type.5bf = fn_decl @Convert.2 [template = constants.%Convert.f4a] {
  91. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  92. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, runtime_param0
  93. // CHECK:STDOUT: %return.patt: %X = return_slot_pattern
  94. // CHECK:STDOUT: %return.param_patt: %X = out_param_pattern %return.patt, runtime_param1
  95. // CHECK:STDOUT: } {
  96. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  97. // CHECK:STDOUT: %self.param: %i32 = value_param runtime_param0
  98. // CHECK:STDOUT: %.loc16_20: type = splice_block %i32 [template = constants.%i32] {
  99. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  100. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  103. // CHECK:STDOUT: %return.param: ref %X = out_param runtime_param1
  104. // CHECK:STDOUT: %return: ref %X = return_slot %return.param
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !members:
  108. // CHECK:STDOUT: .Convert = %Convert.decl
  109. // CHECK:STDOUT: witness = file.%impl_witness.loc15
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: impl @impl.2: %X.ref as %As.type {
  113. // CHECK:STDOUT: %Convert.decl: %Convert.type.45a = fn_decl @Convert.3 [template = constants.%Convert.5bc] {
  114. // CHECK:STDOUT: %self.patt: %X = binding_pattern self
  115. // CHECK:STDOUT: %self.param_patt: %X = value_param_pattern %self.patt, runtime_param0
  116. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  117. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  118. // CHECK:STDOUT: } {
  119. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  120. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  121. // CHECK:STDOUT: %self.param: %X = value_param runtime_param0
  122. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  123. // CHECK:STDOUT: %self: %X = bind_name self, %self.param
  124. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  125. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !members:
  129. // CHECK:STDOUT: .Convert = %Convert.decl
  130. // CHECK:STDOUT: witness = file.%impl_witness.loc19
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: class @X {
  134. // CHECK:STDOUT: %.loc12: %X.elem = field_decl n, element0 [template]
  135. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n [template = constants.%complete_type.9b7]
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = constants.%X
  139. // CHECK:STDOUT: .n = %.loc12
  140. // CHECK:STDOUT: complete_type_witness = %complete_type
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn @Convert.2[%self.param_patt: %i32]() -> %return.param_patt: %X {
  144. // CHECK:STDOUT: !entry:
  145. // CHECK:STDOUT: %self.ref: %i32 = name_ref self, %self
  146. // CHECK:STDOUT: %.loc16_51.1: %struct_type.n = struct_literal (%self.ref)
  147. // CHECK:STDOUT: %.loc16_51.2: ref %i32 = class_element_access %return, element0
  148. // CHECK:STDOUT: %.loc16_51.3: init %i32 = initialize_from %self.ref to %.loc16_51.2
  149. // CHECK:STDOUT: %.loc16_51.4: init %X = class_init (%.loc16_51.3), %return
  150. // CHECK:STDOUT: %.loc16_52: init %X = converted %.loc16_51.1, %.loc16_51.4
  151. // CHECK:STDOUT: return %.loc16_52 to %return
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @Convert.3[%self.param_patt: %X]() -> %i32 {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %self.ref: %X = name_ref self, %self
  157. // CHECK:STDOUT: %n.ref: %X.elem = name_ref n, @X.%.loc12 [template = @X.%.loc12]
  158. // CHECK:STDOUT: %.loc20_45.1: ref %i32 = class_element_access %self.ref, element0
  159. // CHECK:STDOUT: %.loc20_45.2: %i32 = bind_value %.loc20_45.1
  160. // CHECK:STDOUT: return %.loc20_45.2
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: fn @__global_init() {
  164. // CHECK:STDOUT: !entry:
  165. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [template = constants.%int_4.0c1]
  166. // CHECK:STDOUT: %int_32.loc23_21: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  167. // CHECK:STDOUT: %i32.loc23_21: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  168. // CHECK:STDOUT: %impl.elem0.loc23_18: %Convert.type.1b9 = impl_witness_access constants.%impl_witness.c9a, element0 [template = constants.%Convert.6c4]
  169. // CHECK:STDOUT: %Convert.bound.loc23_18: <bound method> = bound_method %int_4, %impl.elem0.loc23_18 [template = constants.%Convert.bound.416]
  170. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound.loc23_18, @Convert.7(constants.%int_32) [template = constants.%Convert.specific_fn]
  171. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_4) [template = constants.%int_4.2ec]
  172. // CHECK:STDOUT: %.loc23_18.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_4.2ec]
  173. // CHECK:STDOUT: %.loc23_18.2: %i32 = converted %int_4, %.loc23_18.1 [template = constants.%int_4.2ec]
  174. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  175. // CHECK:STDOUT: %impl.elem0.loc23_26: %Convert.type.73d = impl_witness_access constants.%impl_witness.cf4, element0 [template = constants.%Convert.f4a]
  176. // CHECK:STDOUT: %Convert.bound.loc23_26: <bound method> = bound_method %.loc23_18.2, %impl.elem0.loc23_26 [template = constants.%Convert.bound.5eb]
  177. // CHECK:STDOUT: %.loc23_26.1: ref %X = temporary_storage
  178. // CHECK:STDOUT: %Convert.call.loc23_26: init %X = call %Convert.bound.loc23_26(%.loc23_18.2) to %.loc23_26.1
  179. // CHECK:STDOUT: %.loc23_26.2: init %X = converted %.loc23_18.2, %Convert.call.loc23_26
  180. // CHECK:STDOUT: %int_32.loc23_35: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  181. // CHECK:STDOUT: %i32.loc23_35: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  182. // CHECK:STDOUT: %impl.elem0.loc23_32: %Convert.type.1b9 = impl_witness_access constants.%impl_witness.d22, element0 [template = constants.%Convert.5bc]
  183. // CHECK:STDOUT: %Convert.bound.loc23_32: <bound method> = bound_method %.loc23_26.2, %impl.elem0.loc23_32
  184. // CHECK:STDOUT: %.loc23_26.3: ref %X = temporary %.loc23_26.1, %.loc23_26.2
  185. // CHECK:STDOUT: %.loc23_26.4: %X = bind_value %.loc23_26.3
  186. // CHECK:STDOUT: %Convert.call.loc23_32: init %i32 = call %Convert.bound.loc23_32(%.loc23_26.4)
  187. // CHECK:STDOUT: %.loc23_32.1: %i32 = value_of_initializer %Convert.call.loc23_32
  188. // CHECK:STDOUT: %.loc23_32.2: %i32 = converted %.loc23_26.2, %.loc23_32.1
  189. // CHECK:STDOUT: %n: %i32 = bind_name n, %.loc23_32.2
  190. // CHECK:STDOUT: return
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT: