self_conversion.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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: %Int32.type: type = fn_type @Int32 [template]
  33. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  34. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  35. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, i32 [template]
  36. // CHECK:STDOUT: %.3: type = struct_type {.a: i32} [template]
  37. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  38. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  39. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  40. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, %Base [template]
  41. // CHECK:STDOUT: %SelfBase.type: type = fn_type @SelfBase [template]
  42. // CHECK:STDOUT: %SelfBase: %SelfBase.type = struct_value () [template]
  43. // CHECK:STDOUT: %.7: type = ptr_type %Base [template]
  44. // CHECK:STDOUT: %AddrSelfBase.type: type = fn_type @AddrSelfBase [template]
  45. // CHECK:STDOUT: %AddrSelfBase: %AddrSelfBase.type = struct_value () [template]
  46. // CHECK:STDOUT: %.8: type = struct_type {.base: %Base} [template]
  47. // CHECK:STDOUT: %.9: <witness> = complete_type_witness %.8 [template]
  48. // CHECK:STDOUT: %.10: i32 = int_literal 1 [template]
  49. // CHECK:STDOUT: %.11: type = ptr_type %Derived [template]
  50. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  51. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  52. // CHECK:STDOUT: %.12: type = struct_type {.base: %.5} [template]
  53. // CHECK:STDOUT: %.13: type = ptr_type %.8 [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: .Int32 = %import_ref
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/operators
  61. // CHECK:STDOUT: import Core//prelude/types
  62. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  63. // CHECK:STDOUT: import Core//prelude/operators/as
  64. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  65. // CHECK:STDOUT: import Core//prelude/operators/comparison
  66. // CHECK:STDOUT: import Core//prelude/types/bool
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  73. // CHECK:STDOUT: .Core = imports.%Core
  74. // CHECK:STDOUT: .Base = %Base.decl
  75. // CHECK:STDOUT: .Derived = %Derived.decl
  76. // CHECK:STDOUT: .Call = %Call.decl
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  80. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  81. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  82. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  83. // CHECK:STDOUT: } {
  84. // CHECK:STDOUT: %Base.ref.loc22: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  85. // CHECK:STDOUT: %self.param.loc22: %Base = param self, runtime_param0
  86. // CHECK:STDOUT: %self.loc22: %Base = bind_name self, %self.param.loc22
  87. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc22_38.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  89. // CHECK:STDOUT: %.loc22_38.2: type = converted %int.make_type_32.loc22, %.loc22_38.1 [template = i32]
  90. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  93. // CHECK:STDOUT: %self.patt: %.7 = binding_pattern self
  94. // CHECK:STDOUT: } {
  95. // CHECK:STDOUT: %Base.ref.loc26: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  96. // CHECK:STDOUT: %.loc26_40: type = ptr_type %Base [template = constants.%.7]
  97. // CHECK:STDOUT: %self.param.loc26: %.7 = param self, runtime_param0
  98. // CHECK:STDOUT: %self.loc26: %.7 = bind_name self, %self.param.loc26
  99. // CHECK:STDOUT: %.loc26_25: %.7 = addr_pattern %self.loc26
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  102. // CHECK:STDOUT: %p.patt: %.11 = binding_pattern p
  103. // CHECK:STDOUT: } {
  104. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  105. // CHECK:STDOUT: %.loc30_19: type = ptr_type %Derived [template = constants.%.11]
  106. // CHECK:STDOUT: %p.param: %.11 = param p, runtime_param0
  107. // CHECK:STDOUT: %p: %.11 = bind_name p, %p.param
  108. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc30_25.1: type = value_of_initializer %int.make_type_32 [template = i32]
  110. // CHECK:STDOUT: %.loc30_25.2: type = converted %int.make_type_32, %.loc30_25.1 [template = i32]
  111. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: class @Base {
  116. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  117. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  118. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  119. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  120. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.3 [template = constants.%.4]
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: !members:
  123. // CHECK:STDOUT: .Self = constants.%Base
  124. // CHECK:STDOUT: .a = %.loc12_8
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: class @Derived {
  128. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  129. // CHECK:STDOUT: %.loc16: %.6 = base_decl %Base, element0 [template]
  130. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  131. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  132. // CHECK:STDOUT: } {
  133. // CHECK:STDOUT: %Base.ref.loc18: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  134. // CHECK:STDOUT: %self.param.loc18: %Base = param self, runtime_param0
  135. // CHECK:STDOUT: %self.loc18: %Base = bind_name self, %self.param.loc18
  136. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  137. // CHECK:STDOUT: %.loc18_32.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  138. // CHECK:STDOUT: %.loc18_32.2: type = converted %int.make_type_32.loc18, %.loc18_32.1 [template = i32]
  139. // CHECK:STDOUT: %return.var.loc18: ref i32 = var <return slot>
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  142. // CHECK:STDOUT: %self.patt: %.7 = binding_pattern self
  143. // CHECK:STDOUT: } {
  144. // CHECK:STDOUT: %Base.ref.loc19: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  145. // CHECK:STDOUT: %.loc19_34: type = ptr_type %Base [template = constants.%.7]
  146. // CHECK:STDOUT: %self.param.loc19: %.7 = param self, runtime_param0
  147. // CHECK:STDOUT: %self.loc19: %.7 = bind_name self, %self.param.loc19
  148. // CHECK:STDOUT: %.loc19_19: %.7 = addr_pattern %self.loc19
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: %.loc20: <witness> = complete_type_witness %.8 [template = constants.%.9]
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !members:
  153. // CHECK:STDOUT: .Self = constants.%Derived
  154. // CHECK:STDOUT: .base = %.loc16
  155. // CHECK:STDOUT: .SelfBase = %SelfBase.decl
  156. // CHECK:STDOUT: .AddrSelfBase = %AddrSelfBase.decl
  157. // CHECK:STDOUT: extend name_scope2
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn @SelfBase[%self.loc22: %Base]() -> i32 {
  163. // CHECK:STDOUT: !entry:
  164. // CHECK:STDOUT: %self.ref: %Base = name_ref self, %self.loc22
  165. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  166. // CHECK:STDOUT: %.loc23_14.1: ref i32 = class_element_access %self.ref, element0
  167. // CHECK:STDOUT: %.loc23_14.2: i32 = bind_value %.loc23_14.1
  168. // CHECK:STDOUT: return %.loc23_14.2
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: fn @AddrSelfBase[addr %self.loc26: %.7]() {
  172. // CHECK:STDOUT: !entry:
  173. // CHECK:STDOUT: %self.ref: %.7 = name_ref self, %self.loc26
  174. // CHECK:STDOUT: %.loc27_4: ref %Base = deref %self.ref
  175. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  176. // CHECK:STDOUT: %.loc27_10: ref i32 = class_element_access %.loc27_4, element0
  177. // CHECK:STDOUT: %.loc27_15: i32 = int_literal 1 [template = constants.%.10]
  178. // CHECK:STDOUT: assign %.loc27_10, %.loc27_15
  179. // CHECK:STDOUT: return
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn @Call(%p: %.11) -> i32 {
  183. // CHECK:STDOUT: !entry:
  184. // CHECK:STDOUT: %p.ref.loc31: %.11 = name_ref p, %p
  185. // CHECK:STDOUT: %.loc31_4.1: ref %Derived = deref %p.ref.loc31
  186. // CHECK:STDOUT: %AddrSelfBase.ref: %AddrSelfBase.type = name_ref AddrSelfBase, @Derived.%AddrSelfBase.decl [template = constants.%AddrSelfBase]
  187. // CHECK:STDOUT: %.loc31_7: <bound method> = bound_method %.loc31_4.1, %AddrSelfBase.ref
  188. // CHECK:STDOUT: %.loc31_4.2: %.11 = addr_of %.loc31_4.1
  189. // CHECK:STDOUT: %.loc31_20.1: ref %Derived = deref %.loc31_4.2
  190. // CHECK:STDOUT: %.loc31_20.2: ref %Base = class_element_access %.loc31_20.1, element0
  191. // CHECK:STDOUT: %.loc31_20.3: %.7 = addr_of %.loc31_20.2
  192. // CHECK:STDOUT: %.loc31_20.4: %.7 = converted %.loc31_4.2, %.loc31_20.3
  193. // CHECK:STDOUT: %AddrSelfBase.call: init %.1 = call %.loc31_7(%.loc31_20.4)
  194. // CHECK:STDOUT: %p.ref.loc32: %.11 = name_ref p, %p
  195. // CHECK:STDOUT: %.loc32_11: ref %Derived = deref %p.ref.loc32
  196. // CHECK:STDOUT: %SelfBase.ref: %SelfBase.type = name_ref SelfBase, @Derived.%SelfBase.decl [template = constants.%SelfBase]
  197. // CHECK:STDOUT: %.loc32_14: <bound method> = bound_method %.loc32_11, %SelfBase.ref
  198. // CHECK:STDOUT: %.loc32_23.1: ref %Base = class_element_access %.loc32_11, element0
  199. // CHECK:STDOUT: %.loc32_23.2: ref %Base = converted %.loc32_11, %.loc32_23.1
  200. // CHECK:STDOUT: %.loc32_23.3: %Base = bind_value %.loc32_23.2
  201. // CHECK:STDOUT: %SelfBase.call: init i32 = call %.loc32_14(%.loc32_23.3)
  202. // CHECK:STDOUT: %.loc32_25.1: i32 = value_of_initializer %SelfBase.call
  203. // CHECK:STDOUT: %.loc32_25.2: i32 = converted %SelfBase.call, %.loc32_25.1
  204. // CHECK:STDOUT: return %.loc32_25.2
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT: