self_conversion.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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: %Derived: type = class_type @Derived [template]
  38. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  39. // CHECK:STDOUT: %.5: 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: %.6: 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: %.7: type = struct_type {.base: %Base} [template]
  46. // CHECK:STDOUT: %.8: i32 = int_literal 1 [template]
  47. // CHECK:STDOUT: %.9: type = ptr_type %Derived [template]
  48. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  49. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  50. // CHECK:STDOUT: %.10: type = struct_type {.base: %.4} [template]
  51. // CHECK:STDOUT: %.11: type = ptr_type %.7 [template]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = %Core
  57. // CHECK:STDOUT: .Base = %Base.decl
  58. // CHECK:STDOUT: .Derived = %Derived.decl
  59. // CHECK:STDOUT: .Call = %Call.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  62. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  63. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  65. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  66. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  67. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  68. // CHECK:STDOUT: %Base.ref.loc22: type = name_ref Base, %Base.decl [template = constants.%Base]
  69. // CHECK:STDOUT: %self.loc22_21.1: %Base = param self
  70. // CHECK:STDOUT: @SelfBase.%self: %Base = bind_name self, %self.loc22_21.1
  71. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  72. // CHECK:STDOUT: %.loc22_38.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  73. // CHECK:STDOUT: %.loc22_38.2: type = converted %int.make_type_32.loc22, %.loc22_38.1 [template = i32]
  74. // CHECK:STDOUT: @SelfBase.%return: ref i32 = var <return slot>
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  77. // CHECK:STDOUT: %Base.ref.loc26: type = name_ref Base, %Base.decl [template = constants.%Base]
  78. // CHECK:STDOUT: %.loc26_40: type = ptr_type %Base [template = constants.%.6]
  79. // CHECK:STDOUT: %self.loc26_30.1: %.6 = param self
  80. // CHECK:STDOUT: @AddrSelfBase.%self: %.6 = bind_name self, %self.loc26_30.1
  81. // CHECK:STDOUT: @AddrSelfBase.%.loc26: %.6 = addr_pattern @AddrSelfBase.%self
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  84. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  85. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  86. // CHECK:STDOUT: %.loc30_19: type = ptr_type %Derived [template = constants.%.9]
  87. // CHECK:STDOUT: %p.loc30_9.1: %.9 = param p
  88. // CHECK:STDOUT: @Call.%p: %.9 = bind_name p, %p.loc30_9.1
  89. // CHECK:STDOUT: %int.make_type_32.loc30: init type = call constants.%Int32() [template = i32]
  90. // CHECK:STDOUT: %.loc30_25.1: type = value_of_initializer %int.make_type_32.loc30 [template = i32]
  91. // CHECK:STDOUT: %.loc30_25.2: type = converted %int.make_type_32.loc30, %.loc30_25.1 [template = i32]
  92. // CHECK:STDOUT: @Call.%return: ref i32 = var <return slot>
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: class @Base {
  97. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  98. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  99. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  100. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !members:
  103. // CHECK:STDOUT: .Self = constants.%Base
  104. // CHECK:STDOUT: .a = %.loc12_8
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: class @Derived {
  108. // CHECK:STDOUT: %Base.ref.loc16: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  109. // CHECK:STDOUT: %.loc16: %.5 = base_decl %Base, element0 [template]
  110. // CHECK:STDOUT: %SelfBase.decl: %SelfBase.type = fn_decl @SelfBase [template = constants.%SelfBase] {
  111. // CHECK:STDOUT: %Base.ref.loc18: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  112. // CHECK:STDOUT: %self.loc18_15.1: %Base = param self
  113. // CHECK:STDOUT: %self.loc18_15.2: %Base = bind_name self, %self.loc18_15.1
  114. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  115. // CHECK:STDOUT: %.loc18_32.1: type = value_of_initializer %int.make_type_32 [template = i32]
  116. // CHECK:STDOUT: %.loc18_32.2: type = converted %int.make_type_32, %.loc18_32.1 [template = i32]
  117. // CHECK:STDOUT: %return.var: ref i32 = var <return slot>
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %AddrSelfBase.decl: %AddrSelfBase.type = fn_decl @AddrSelfBase [template = constants.%AddrSelfBase] {
  120. // CHECK:STDOUT: %Base.ref.loc19: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  121. // CHECK:STDOUT: %.loc19_34: type = ptr_type %Base [template = constants.%.6]
  122. // CHECK:STDOUT: %self.loc19_24.1: %.6 = param self
  123. // CHECK:STDOUT: %self.loc19_24.3: %.6 = bind_name self, %self.loc19_24.1
  124. // CHECK:STDOUT: %.loc19_19: %.6 = addr_pattern %self.loc19_24.3
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Self = constants.%Derived
  129. // CHECK:STDOUT: .base = %.loc16
  130. // CHECK:STDOUT: .SelfBase = %SelfBase.decl
  131. // CHECK:STDOUT: .AddrSelfBase = %AddrSelfBase.decl
  132. // CHECK:STDOUT: extend name_scope2
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: fn @SelfBase[%self: %Base]() -> i32 {
  138. // CHECK:STDOUT: !entry:
  139. // CHECK:STDOUT: %self.ref: %Base = name_ref self, %self
  140. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  141. // CHECK:STDOUT: %.loc23_14.1: ref i32 = class_element_access %self.ref, element0
  142. // CHECK:STDOUT: %.loc23_14.2: i32 = bind_value %.loc23_14.1
  143. // CHECK:STDOUT: return %.loc23_14.2
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: fn @AddrSelfBase[addr %self: %.6]() {
  147. // CHECK:STDOUT: !entry:
  148. // CHECK:STDOUT: %self.ref: %.6 = name_ref self, %self
  149. // CHECK:STDOUT: %.loc27_4: ref %Base = deref %self.ref
  150. // CHECK:STDOUT: %a.ref: %.2 = name_ref a, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  151. // CHECK:STDOUT: %.loc27_10: ref i32 = class_element_access %.loc27_4, element0
  152. // CHECK:STDOUT: %.loc27_15: i32 = int_literal 1 [template = constants.%.8]
  153. // CHECK:STDOUT: assign %.loc27_10, %.loc27_15
  154. // CHECK:STDOUT: return
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: fn @Call(%p: %.9) -> i32 {
  158. // CHECK:STDOUT: !entry:
  159. // CHECK:STDOUT: %p.ref.loc31: %.9 = name_ref p, %p
  160. // CHECK:STDOUT: %.loc31_4.1: ref %Derived = deref %p.ref.loc31
  161. // CHECK:STDOUT: %AddrSelfBase.ref: %AddrSelfBase.type = name_ref AddrSelfBase, @Derived.%AddrSelfBase.decl [template = constants.%AddrSelfBase]
  162. // CHECK:STDOUT: %.loc31_7: <bound method> = bound_method %.loc31_4.1, %AddrSelfBase.ref
  163. // CHECK:STDOUT: %.loc31_4.2: %.9 = addr_of %.loc31_4.1
  164. // CHECK:STDOUT: %.loc31_20.1: ref %Derived = deref %.loc31_4.2
  165. // CHECK:STDOUT: %.loc31_20.2: ref %Base = class_element_access %.loc31_20.1, element0
  166. // CHECK:STDOUT: %.loc31_20.3: %.6 = addr_of %.loc31_20.2
  167. // CHECK:STDOUT: %.loc31_20.4: %.6 = converted %.loc31_4.2, %.loc31_20.3
  168. // CHECK:STDOUT: %AddrSelfBase.call: init %.1 = call %.loc31_7(%.loc31_20.4)
  169. // CHECK:STDOUT: %p.ref.loc32: %.9 = name_ref p, %p
  170. // CHECK:STDOUT: %.loc32_11: ref %Derived = deref %p.ref.loc32
  171. // CHECK:STDOUT: %SelfBase.ref: %SelfBase.type = name_ref SelfBase, @Derived.%SelfBase.decl [template = constants.%SelfBase]
  172. // CHECK:STDOUT: %.loc32_14: <bound method> = bound_method %.loc32_11, %SelfBase.ref
  173. // CHECK:STDOUT: %.loc32_23.1: ref %Base = class_element_access %.loc32_11, element0
  174. // CHECK:STDOUT: %.loc32_23.2: ref %Base = converted %.loc32_11, %.loc32_23.1
  175. // CHECK:STDOUT: %.loc32_23.3: %Base = bind_value %.loc32_23.2
  176. // CHECK:STDOUT: %SelfBase.call: init i32 = call %.loc32_14(%.loc32_23.3)
  177. // CHECK:STDOUT: %.loc32_25.1: i32 = value_of_initializer %SelfBase.call
  178. // CHECK:STDOUT: %.loc32_25.2: i32 = converted %SelfBase.call, %.loc32_25.1
  179. // CHECK:STDOUT: return %.loc32_25.2
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: