overloaded.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: %.1: Core.IntLiteral = int_value 32 [template]
  25. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  26. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  27. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  28. // CHECK:STDOUT: %.2: type = unbound_element_type %X, %i32 [template]
  29. // CHECK:STDOUT: %.3: type = struct_type {.n: %i32} [template]
  30. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  31. // CHECK:STDOUT: %As.type.1: type = generic_interface_type @As [template]
  32. // CHECK:STDOUT: %As: %As.type.1 = struct_value () [template]
  33. // CHECK:STDOUT: %As.type.3: type = facet_type <@As, @As(%X)> [template]
  34. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.2 [template]
  35. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  36. // CHECK:STDOUT: %Convert.type.3: type = fn_type @Convert.1, @As(%X) [template]
  37. // CHECK:STDOUT: %.12: <witness> = interface_witness (%Convert.2) [template]
  38. // CHECK:STDOUT: %As.type.4: type = facet_type <@As, @As(%i32)> [template]
  39. // CHECK:STDOUT: %Convert.type.4: type = fn_type @Convert.3 [template]
  40. // CHECK:STDOUT: %Convert.4: %Convert.type.4 = struct_value () [template]
  41. // CHECK:STDOUT: %Convert.type.5: type = fn_type @Convert.1, @As(%i32) [template]
  42. // CHECK:STDOUT: %.17: <witness> = interface_witness (%Convert.4) [template]
  43. // CHECK:STDOUT: %.18: Core.IntLiteral = int_value 4 [template]
  44. // CHECK:STDOUT: %Convert.type.17: type = fn_type @Convert.9, @impl.7(%.1) [template]
  45. // CHECK:STDOUT: %Convert.17: %Convert.type.17 = struct_value () [template]
  46. // CHECK:STDOUT: %.36: <witness> = interface_witness (%Convert.17) [template]
  47. // CHECK:STDOUT: %.37: <bound method> = bound_method %.18, %Convert.17 [template]
  48. // CHECK:STDOUT: %.38: <specific function> = specific_function %.37, @Convert.9(%.1) [template]
  49. // CHECK:STDOUT: %.39: %i32 = int_value 4 [template]
  50. // CHECK:STDOUT: %.40: <bound method> = bound_method %.39, %Convert.2 [template]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: imports {
  54. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  55. // CHECK:STDOUT: .Int = %import_ref.1
  56. // CHECK:STDOUT: .As = %import_ref.2
  57. // CHECK:STDOUT: import Core//prelude
  58. // CHECK:STDOUT: import Core//prelude/...
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %import_ref.2: %As.type.1 = import_ref Core//prelude/operators/as, inst+13, loaded [template = constants.%As]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .X = %X.decl
  67. // CHECK:STDOUT: .n = @__global_init.%n
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %Core.import = import Core
  70. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {} {}
  71. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  72. // CHECK:STDOUT: %.loc15_6.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  73. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc15_6.1) [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc15_6.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  75. // CHECK:STDOUT: %.loc15_6.3: type = converted %int.make_type_signed, %.loc15_6.2 [template = constants.%i32]
  76. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  77. // CHECK:STDOUT: %As.ref: %As.type.1 = name_ref As, imports.%import_ref.2 [template = constants.%As]
  78. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  79. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%X)> [template = constants.%As.type.3]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: impl_decl @impl.2 [template] {} {
  82. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  83. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  84. // CHECK:STDOUT: %As.ref: %As.type.1 = name_ref As, imports.%import_ref.2 [template = constants.%As]
  85. // CHECK:STDOUT: %.loc19_19: Core.IntLiteral = int_value 32 [template = constants.%.1]
  86. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc19_19) [template = constants.%i32]
  87. // CHECK:STDOUT: %.loc19_18.1: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  88. // CHECK:STDOUT: %.loc19_18.2: type = converted %int.make_type_signed, %.loc19_18.1 [template = constants.%i32]
  89. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%i32)> [template = constants.%As.type.4]
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %.loc23_8.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  92. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc23_8.1) [template = constants.%i32]
  93. // CHECK:STDOUT: %.loc23_8.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  94. // CHECK:STDOUT: %.loc23_8.3: type = converted %int.make_type_signed, %.loc23_8.2 [template = constants.%i32]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: impl @impl.1: %.loc15_6.3 as %As.type {
  98. // CHECK:STDOUT: %Convert.decl: %Convert.type.2 = fn_decl @Convert.2 [template = constants.%Convert.2] {
  99. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  100. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, runtime_param0
  101. // CHECK:STDOUT: %return.patt: %X = return_slot_pattern
  102. // CHECK:STDOUT: %return.param_patt: %X = out_param_pattern %return.patt, runtime_param1
  103. // CHECK:STDOUT: } {
  104. // CHECK:STDOUT: %.loc16_20.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  105. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc16_20.1) [template = constants.%i32]
  106. // CHECK:STDOUT: %.loc16_20.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  107. // CHECK:STDOUT: %.loc16_20.3: type = converted %int.make_type_signed, %.loc16_20.2 [template = constants.%i32]
  108. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  109. // CHECK:STDOUT: %self.param: %i32 = value_param runtime_param0
  110. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  111. // CHECK:STDOUT: %return.param: ref %X = out_param runtime_param1
  112. // CHECK:STDOUT: %return: ref %X = return_slot %return.param
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %.loc15_24: <witness> = interface_witness (%Convert.decl) [template = constants.%.12]
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !members:
  117. // CHECK:STDOUT: .Convert = %Convert.decl
  118. // CHECK:STDOUT: witness = %.loc15_24
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: impl @impl.2: %X.ref as %As.type {
  122. // CHECK:STDOUT: %Convert.decl: %Convert.type.4 = fn_decl @Convert.3 [template = constants.%Convert.4] {
  123. // CHECK:STDOUT: %self.patt: %X = binding_pattern self
  124. // CHECK:STDOUT: %self.param_patt: %X = value_param_pattern %self.patt, runtime_param0
  125. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  126. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  127. // CHECK:STDOUT: } {
  128. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  129. // CHECK:STDOUT: %.loc20_28.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  130. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc20_28.1) [template = constants.%i32]
  131. // CHECK:STDOUT: %.loc20_28.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  132. // CHECK:STDOUT: %.loc20_28.3: type = converted %int.make_type_signed, %.loc20_28.2 [template = constants.%i32]
  133. // CHECK:STDOUT: %self.param: %X = value_param runtime_param0
  134. // CHECK:STDOUT: %self: %X = bind_name self, %self.param
  135. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  136. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %.loc19_24: <witness> = interface_witness (%Convert.decl) [template = constants.%.17]
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !members:
  141. // CHECK:STDOUT: .Convert = %Convert.decl
  142. // CHECK:STDOUT: witness = %.loc19_24
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: class @X {
  146. // CHECK:STDOUT: %.loc12_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  147. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc12_10.1) [template = constants.%i32]
  148. // CHECK:STDOUT: %.loc12_10.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  149. // CHECK:STDOUT: %.loc12_10.3: type = converted %int.make_type_signed, %.loc12_10.2 [template = constants.%i32]
  150. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl n, element0 [template]
  151. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.3 [template = constants.%.4]
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !members:
  154. // CHECK:STDOUT: .Self = constants.%X
  155. // CHECK:STDOUT: .n = %.loc12_8
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: fn @Convert.2[%self.param_patt: %i32]() -> %return: %X {
  159. // CHECK:STDOUT: !entry:
  160. // CHECK:STDOUT: %self.ref: %i32 = name_ref self, %self
  161. // CHECK:STDOUT: %.loc16_51.1: %.3 = struct_literal (%self.ref)
  162. // CHECK:STDOUT: %.loc16_51.2: ref %i32 = class_element_access %return, element0
  163. // CHECK:STDOUT: %.loc16_51.3: init %i32 = initialize_from %self.ref to %.loc16_51.2
  164. // CHECK:STDOUT: %.loc16_51.4: init %X = class_init (%.loc16_51.3), %return
  165. // CHECK:STDOUT: %.loc16_52: init %X = converted %.loc16_51.1, %.loc16_51.4
  166. // CHECK:STDOUT: return %.loc16_52 to %return
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn @Convert.3[%self.param_patt: %X]() -> %i32 {
  170. // CHECK:STDOUT: !entry:
  171. // CHECK:STDOUT: %self.ref: %X = name_ref self, %self
  172. // CHECK:STDOUT: %n.ref: %.2 = name_ref n, @X.%.loc12_8 [template = @X.%.loc12_8]
  173. // CHECK:STDOUT: %.loc20_45.1: ref %i32 = class_element_access %self.ref, element0
  174. // CHECK:STDOUT: %.loc20_45.2: %i32 = bind_value %.loc20_45.1
  175. // CHECK:STDOUT: return %.loc20_45.2
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: fn @__global_init() {
  179. // CHECK:STDOUT: !entry:
  180. // CHECK:STDOUT: %.loc23_16: Core.IntLiteral = int_value 4 [template = constants.%.18]
  181. // CHECK:STDOUT: %.loc23_21.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  182. // CHECK:STDOUT: %int.make_type_signed.loc23_21: init type = call constants.%Int(%.loc23_21.1) [template = constants.%i32]
  183. // CHECK:STDOUT: %.loc23_21.2: type = value_of_initializer %int.make_type_signed.loc23_21 [template = constants.%i32]
  184. // CHECK:STDOUT: %.loc23_21.3: type = converted %int.make_type_signed.loc23_21, %.loc23_21.2 [template = constants.%i32]
  185. // CHECK:STDOUT: %.loc23_18.1: %Convert.type.5 = interface_witness_access constants.%.36, element0 [template = constants.%Convert.17]
  186. // CHECK:STDOUT: %.loc23_18.2: <bound method> = bound_method %.loc23_16, %.loc23_18.1 [template = constants.%.37]
  187. // CHECK:STDOUT: %.loc23_18.3: <specific function> = specific_function %.loc23_18.2, @Convert.9(constants.%.1) [template = constants.%.38]
  188. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %.loc23_18.3(%.loc23_16) [template = constants.%.39]
  189. // CHECK:STDOUT: %.loc23_18.4: %i32 = value_of_initializer %int.convert_checked [template = constants.%.39]
  190. // CHECK:STDOUT: %.loc23_18.5: %i32 = converted %.loc23_16, %.loc23_18.4 [template = constants.%.39]
  191. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [template = constants.%X]
  192. // CHECK:STDOUT: %.loc23_26.1: %Convert.type.3 = interface_witness_access constants.%.12, element0 [template = constants.%Convert.2]
  193. // CHECK:STDOUT: %.loc23_26.2: <bound method> = bound_method %.loc23_18.5, %.loc23_26.1 [template = constants.%.40]
  194. // CHECK:STDOUT: %.loc23_26.3: ref %X = temporary_storage
  195. // CHECK:STDOUT: %Convert.call.loc23_26: init %X = call %.loc23_26.2(%.loc23_18.5) to %.loc23_26.3
  196. // CHECK:STDOUT: %.loc23_26.4: init %X = converted %.loc23_18.5, %Convert.call.loc23_26
  197. // CHECK:STDOUT: %.loc23_35.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  198. // CHECK:STDOUT: %int.make_type_signed.loc23_35: init type = call constants.%Int(%.loc23_35.1) [template = constants.%i32]
  199. // CHECK:STDOUT: %.loc23_35.2: type = value_of_initializer %int.make_type_signed.loc23_35 [template = constants.%i32]
  200. // CHECK:STDOUT: %.loc23_35.3: type = converted %int.make_type_signed.loc23_35, %.loc23_35.2 [template = constants.%i32]
  201. // CHECK:STDOUT: %.loc23_32.1: %Convert.type.5 = interface_witness_access constants.%.17, element0 [template = constants.%Convert.4]
  202. // CHECK:STDOUT: %.loc23_32.2: <bound method> = bound_method %.loc23_26.4, %.loc23_32.1
  203. // CHECK:STDOUT: %.loc23_26.5: ref %X = temporary %.loc23_26.3, %.loc23_26.4
  204. // CHECK:STDOUT: %.loc23_26.6: %X = bind_value %.loc23_26.5
  205. // CHECK:STDOUT: %Convert.call.loc23_32: init %i32 = call %.loc23_32.2(%.loc23_26.6)
  206. // CHECK:STDOUT: %.loc23_32.3: %i32 = value_of_initializer %Convert.call.loc23_32
  207. // CHECK:STDOUT: %.loc23_32.4: %i32 = converted %.loc23_26.4, %.loc23_32.3
  208. // CHECK:STDOUT: %n: %i32 = bind_name n, %.loc23_32.4
  209. // CHECK:STDOUT: return
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT: