self_conversion.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/class/self_conversion.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/self_conversion.carbon
  10. base class Base {
  11. var a: i32;
  12. }
  13. class Derived {
  14. extend base: Base;
  15. fn SelfBase[self: Base]() -> i32;
  16. fn AddrSelfBase[addr self: Base*]();
  17. }
  18. fn Derived.SelfBase[self: Base]() -> i32 {
  19. return self.a;
  20. }
  21. fn Derived.AddrSelfBase[addr self: Base*]() {
  22. (*self).a = 1;
  23. }
  24. fn Call(p: Derived*) -> i32 {
  25. (*p).AddrSelfBase();
  26. return (*p).SelfBase();
  27. }
  28. // CHECK:STDOUT: --- self_conversion.carbon
  29. // CHECK:STDOUT:
  30. // CHECK:STDOUT: constants {
  31. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  32. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  33. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  35. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %i32 [template]
  36. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %i32} [template]
  37. // CHECK:STDOUT: %complete_type.fd7: <witness> = complete_type_witness %struct_type.a [template]
  38. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  39. // CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived, %Base [template]
  40. // CHECK:STDOUT: %SelfBase.type: type = fn_type @SelfBase [template]
  41. // CHECK:STDOUT: %SelfBase: %SelfBase.type = struct_value () [template]
  42. // CHECK:STDOUT: %ptr.11f: type = ptr_type %Base [template]
  43. // CHECK:STDOUT: %AddrSelfBase.type: type = fn_type @AddrSelfBase [template]
  44. // CHECK:STDOUT: %AddrSelfBase: %AddrSelfBase.type = struct_value () [template]
  45. // CHECK:STDOUT: %struct_type.base.b1e: type = struct_type {.base: %Base} [template]
  46. // CHECK:STDOUT: %complete_type.15c: <witness> = complete_type_witness %struct_type.base.b1e [template]
  47. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  48. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  49. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  50. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  51. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  52. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  53. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  54. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  55. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  56. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  57. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  58. // CHECK:STDOUT: %ptr.404: type = ptr_type %Derived [template]
  59. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  60. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: imports {
  64. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  65. // CHECK:STDOUT: .Int = %Core.Int
  66. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  67. // CHECK:STDOUT: import Core//prelude
  68. // CHECK:STDOUT: import Core//prelude/...
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .Base = %Base.decl
  76. // CHECK:STDOUT: .Derived = %Derived.decl
  77. // CHECK:STDOUT: .Call = %Call.decl
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %Core.import = import Core
  80. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  81. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  82. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  83. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  84. // CHECK:STDOUT: %self.param_patt: %Base = value_param_pattern %self.patt, runtime_param0
  85. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  86. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  87. // CHECK:STDOUT: } {
  88. // CHECK:STDOUT: %int_32.loc22: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  89. // CHECK:STDOUT: %i32.loc22: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  90. // CHECK:STDOUT: %self.param.loc22: %Base = value_param runtime_param0
  91. // CHECK:STDOUT: %Base.ref.loc22: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  92. // CHECK:STDOUT: %self.loc22: %Base = bind_name self, %self.param.loc22
  93. // CHECK:STDOUT: %return.param.loc22: ref %i32 = out_param runtime_param1
  94. // CHECK:STDOUT: %return.loc22: ref %i32 = return_slot %return.param.loc22
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  97. // CHECK:STDOUT: %self.patt: %ptr.11f = binding_pattern self
  98. // CHECK:STDOUT: %self.param_patt: %ptr.11f = value_param_pattern %self.patt, runtime_param0
  99. // CHECK:STDOUT: %.loc26_25: auto = addr_pattern %self.param_patt
  100. // CHECK:STDOUT: } {
  101. // CHECK:STDOUT: %self.param.loc26: %ptr.11f = value_param runtime_param0
  102. // CHECK:STDOUT: %.loc26_40: type = splice_block %ptr.loc26 [template = constants.%ptr.11f] {
  103. // CHECK:STDOUT: %Base.ref.loc26: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  104. // CHECK:STDOUT: %ptr.loc26: type = ptr_type %Base [template = constants.%ptr.11f]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %self.loc26: %ptr.11f = bind_name self, %self.param.loc26
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  109. // CHECK:STDOUT: %p.patt: %ptr.404 = binding_pattern p
  110. // CHECK:STDOUT: %p.param_patt: %ptr.404 = value_param_pattern %p.patt, runtime_param0
  111. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  112. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  113. // CHECK:STDOUT: } {
  114. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  115. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  116. // CHECK:STDOUT: %p.param: %ptr.404 = value_param runtime_param0
  117. // CHECK:STDOUT: %.loc30: type = splice_block %ptr [template = constants.%ptr.404] {
  118. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  119. // CHECK:STDOUT: %ptr: type = ptr_type %Derived [template = constants.%ptr.404]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %p: %ptr.404 = bind_name p, %p.param
  122. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  123. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: class @Base {
  128. // CHECK:STDOUT: %.loc12_8: %Base.elem = field_decl a, element0 [template]
  129. // CHECK:STDOUT: name_binding_decl {
  130. // CHECK:STDOUT: %.loc12_3: %Base.elem = var_pattern %.loc12_8
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %.var: ref %Base.elem = var <none>
  133. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a [template = constants.%complete_type.fd7]
  134. // CHECK:STDOUT: complete_type_witness = %complete_type
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: !members:
  137. // CHECK:STDOUT: .Self = constants.%Base
  138. // CHECK:STDOUT: .a = %.loc12_8
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: class @Derived {
  142. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  143. // CHECK:STDOUT: %.loc16: %Derived.elem = base_decl %Base.ref, element0 [template]
  144. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  145. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  146. // CHECK:STDOUT: %self.param_patt: %Base = value_param_pattern %self.patt, runtime_param0
  147. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  148. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  149. // CHECK:STDOUT: } {
  150. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  151. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  152. // CHECK:STDOUT: %self.param.loc18: %Base = value_param runtime_param0
  153. // CHECK:STDOUT: %Base.ref.loc18: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  154. // CHECK:STDOUT: %self.loc18: %Base = bind_name self, %self.param.loc18
  155. // CHECK:STDOUT: %return.param.loc18: ref %i32 = out_param runtime_param1
  156. // CHECK:STDOUT: %return.loc18: ref %i32 = return_slot %return.param.loc18
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  159. // CHECK:STDOUT: %self.patt: %ptr.11f = binding_pattern self
  160. // CHECK:STDOUT: %self.param_patt: %ptr.11f = value_param_pattern %self.patt, runtime_param0
  161. // CHECK:STDOUT: %.loc26_25: auto = addr_pattern %self.param_patt
  162. // CHECK:STDOUT: } {
  163. // CHECK:STDOUT: %self.param.loc19: %ptr.11f = value_param runtime_param0
  164. // CHECK:STDOUT: %.loc19: type = splice_block %ptr.loc19 [template = constants.%ptr.11f] {
  165. // CHECK:STDOUT: %Base.ref.loc19: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  166. // CHECK:STDOUT: %ptr.loc19: type = ptr_type %Base [template = constants.%ptr.11f]
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %self.loc19: %ptr.11f = bind_name self, %self.param.loc19
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.base.b1e [template = constants.%complete_type.15c]
  171. // CHECK:STDOUT: complete_type_witness = %complete_type
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !members:
  174. // CHECK:STDOUT: .Self = constants.%Derived
  175. // CHECK:STDOUT: .base = %.loc16
  176. // CHECK:STDOUT: .SelfBase = %SelfBase.decl
  177. // CHECK:STDOUT: .AddrSelfBase = %AddrSelfBase.decl
  178. // CHECK:STDOUT: extend %Base.ref
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: fn @SelfBase[%self.param_patt: %Base]() -> %i32 {
  182. // CHECK:STDOUT: !entry:
  183. // CHECK:STDOUT: %self.ref: %Base = name_ref self, %self.loc22
  184. // CHECK:STDOUT: %a.ref: %Base.elem = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  185. // CHECK:STDOUT: %.loc23_14.1: ref %i32 = class_element_access %self.ref, element0
  186. // CHECK:STDOUT: %.loc23_14.2: %i32 = bind_value %.loc23_14.1
  187. // CHECK:STDOUT: return %.loc23_14.2
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn @AddrSelfBase[addr %self.param_patt: %ptr.11f]() {
  191. // CHECK:STDOUT: !entry:
  192. // CHECK:STDOUT: %self.ref: %ptr.11f = name_ref self, %self.loc26
  193. // CHECK:STDOUT: %.loc27_4: ref %Base = deref %self.ref
  194. // CHECK:STDOUT: %a.ref: %Base.elem = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  195. // CHECK:STDOUT: %.loc27_10: ref %i32 = class_element_access %.loc27_4, element0
  196. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  197. // CHECK:STDOUT: %impl.elem0: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  198. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound]
  199. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %bound_method, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  200. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %specific_fn(%int_1) [template = constants.%int_1.5d2]
  201. // CHECK:STDOUT: %.loc27_13: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.5d2]
  202. // CHECK:STDOUT: assign %.loc27_10, %.loc27_13
  203. // CHECK:STDOUT: return
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: fn @Call(%p.param_patt: %ptr.404) -> %i32 {
  207. // CHECK:STDOUT: !entry:
  208. // CHECK:STDOUT: %p.ref.loc31: %ptr.404 = name_ref p, %p
  209. // CHECK:STDOUT: %.loc31_4.1: ref %Derived = deref %p.ref.loc31
  210. // CHECK:STDOUT: %AddrSelfBase.ref: %AddrSelfBase.type = name_ref AddrSelfBase, @Derived.%AddrSelfBase.decl [template = constants.%AddrSelfBase]
  211. // CHECK:STDOUT: %AddrSelfBase.bound: <bound method> = bound_method %.loc31_4.1, %AddrSelfBase.ref
  212. // CHECK:STDOUT: %addr.loc31_4.1: %ptr.404 = addr_of %.loc31_4.1
  213. // CHECK:STDOUT: %.loc31_4.2: ref %Derived = deref %addr.loc31_4.1
  214. // CHECK:STDOUT: %.loc31_4.3: ref %Base = class_element_access %.loc31_4.2, element0
  215. // CHECK:STDOUT: %addr.loc31_4.2: %ptr.11f = addr_of %.loc31_4.3
  216. // CHECK:STDOUT: %.loc31_4.4: %ptr.11f = converted %addr.loc31_4.1, %addr.loc31_4.2
  217. // CHECK:STDOUT: %AddrSelfBase.call: init %empty_tuple.type = call %AddrSelfBase.bound(%.loc31_4.4)
  218. // CHECK:STDOUT: %p.ref.loc32: %ptr.404 = name_ref p, %p
  219. // CHECK:STDOUT: %.loc32_11.1: ref %Derived = deref %p.ref.loc32
  220. // CHECK:STDOUT: %SelfBase.ref: %SelfBase.type = name_ref SelfBase, @Derived.%SelfBase.decl [template = constants.%SelfBase]
  221. // CHECK:STDOUT: %SelfBase.bound: <bound method> = bound_method %.loc32_11.1, %SelfBase.ref
  222. // CHECK:STDOUT: %.loc32_11.2: ref %Base = class_element_access %.loc32_11.1, element0
  223. // CHECK:STDOUT: %.loc32_11.3: ref %Base = converted %.loc32_11.1, %.loc32_11.2
  224. // CHECK:STDOUT: %.loc32_11.4: %Base = bind_value %.loc32_11.3
  225. // CHECK:STDOUT: %SelfBase.call: init %i32 = call %SelfBase.bound(%.loc32_11.4)
  226. // CHECK:STDOUT: %.loc32_25.1: %i32 = value_of_initializer %SelfBase.call
  227. // CHECK:STDOUT: %.loc32_25.2: %i32 = converted %SelfBase.call, %.loc32_25.1
  228. // CHECK:STDOUT: return %.loc32_25.2
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT: