base_method_qualified.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/base_method_qualified.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base_method_qualified.carbon
  10. class Derived;
  11. base class Base {
  12. fn F[self: Self]() -> i32;
  13. fn G[self: Derived]() -> i32;
  14. }
  15. class Derived {
  16. extend base: Base;
  17. fn F[self: Self]();
  18. fn G[self: Self]();
  19. }
  20. fn Call(a: Derived) -> i32 {
  21. return a.(Base.F)();
  22. }
  23. fn CallIndirect(p: Derived*) -> i32 {
  24. return p->(Base.F)();
  25. }
  26. fn PassDerivedToBase(a: Derived) -> i32 {
  27. return a.(Base.G)();
  28. }
  29. fn PassDerivedToBaseIndirect(p: Derived*) -> i32 {
  30. return p->(Base.G)();
  31. }
  32. // CHECK:STDOUT: --- base_method_qualified.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  36. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  37. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  38. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  39. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  40. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  41. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  42. // CHECK:STDOUT: %G.type.1: type = fn_type @G.1 [template]
  43. // CHECK:STDOUT: %G.1: %G.type.1 = struct_value () [template]
  44. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  45. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  46. // CHECK:STDOUT: %.4: type = unbound_element_type %Derived, %Base [template]
  47. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  48. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  49. // CHECK:STDOUT: %G.type.2: type = fn_type @G.2 [template]
  50. // CHECK:STDOUT: %G.2: %G.type.2 = struct_value () [template]
  51. // CHECK:STDOUT: %.5: type = struct_type {.base: %Base} [template]
  52. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  53. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  54. // CHECK:STDOUT: %.6: type = struct_type {.base: %.3} [template]
  55. // CHECK:STDOUT: %.7: type = ptr_type %.5 [template]
  56. // CHECK:STDOUT: %.8: type = ptr_type %Derived [template]
  57. // CHECK:STDOUT: %CallIndirect.type: type = fn_type @CallIndirect [template]
  58. // CHECK:STDOUT: %CallIndirect: %CallIndirect.type = struct_value () [template]
  59. // CHECK:STDOUT: %PassDerivedToBase.type: type = fn_type @PassDerivedToBase [template]
  60. // CHECK:STDOUT: %PassDerivedToBase: %PassDerivedToBase.type = struct_value () [template]
  61. // CHECK:STDOUT: %PassDerivedToBaseIndirect.type: type = fn_type @PassDerivedToBaseIndirect [template]
  62. // CHECK:STDOUT: %PassDerivedToBaseIndirect: %PassDerivedToBaseIndirect.type = struct_value () [template]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = %Core
  68. // CHECK:STDOUT: .Derived = %Derived.decl.loc11
  69. // CHECK:STDOUT: .Base = %Base.decl
  70. // CHECK:STDOUT: .Call = %Call.decl
  71. // CHECK:STDOUT: .CallIndirect = %CallIndirect.decl
  72. // CHECK:STDOUT: .PassDerivedToBase = %PassDerivedToBase.decl
  73. // CHECK:STDOUT: .PassDerivedToBaseIndirect = %PassDerivedToBaseIndirect.decl
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  76. // CHECK:STDOUT: %Derived.decl.loc11: type = class_decl @Derived [template = constants.%Derived] {}
  77. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  78. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  79. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  80. // CHECK:STDOUT: %Derived.decl.loc18: type = class_decl @Derived [template = constants.%Derived] {}
  81. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  82. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  83. // CHECK:STDOUT: %Derived.ref.loc25: type = name_ref Derived, %Derived.decl.loc11 [template = constants.%Derived]
  84. // CHECK:STDOUT: %a.loc25_9.1: %Derived = param a
  85. // CHECK:STDOUT: @Call.%a: %Derived = bind_name a, %a.loc25_9.1
  86. // CHECK:STDOUT: %int.make_type_32.loc25: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc25_24.1: type = value_of_initializer %int.make_type_32.loc25 [template = i32]
  88. // CHECK:STDOUT: %.loc25_24.2: type = converted %int.make_type_32.loc25, %.loc25_24.1 [template = i32]
  89. // CHECK:STDOUT: @Call.%return: ref i32 = var <return slot>
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  92. // CHECK:STDOUT: %CallIndirect.decl: %CallIndirect.type = fn_decl @CallIndirect [template = constants.%CallIndirect] {
  93. // CHECK:STDOUT: %Derived.ref.loc29: type = name_ref Derived, %Derived.decl.loc11 [template = constants.%Derived]
  94. // CHECK:STDOUT: %.loc29_27: type = ptr_type %Derived [template = constants.%.8]
  95. // CHECK:STDOUT: %p.loc29_17.1: %.8 = param p
  96. // CHECK:STDOUT: @CallIndirect.%p: %.8 = bind_name p, %p.loc29_17.1
  97. // CHECK:STDOUT: %int.make_type_32.loc29: init type = call constants.%Int32() [template = i32]
  98. // CHECK:STDOUT: %.loc29_33.1: type = value_of_initializer %int.make_type_32.loc29 [template = i32]
  99. // CHECK:STDOUT: %.loc29_33.2: type = converted %int.make_type_32.loc29, %.loc29_33.1 [template = i32]
  100. // CHECK:STDOUT: @CallIndirect.%return: ref i32 = var <return slot>
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  103. // CHECK:STDOUT: %PassDerivedToBase.decl: %PassDerivedToBase.type = fn_decl @PassDerivedToBase [template = constants.%PassDerivedToBase] {
  104. // CHECK:STDOUT: %Derived.ref.loc33: type = name_ref Derived, %Derived.decl.loc11 [template = constants.%Derived]
  105. // CHECK:STDOUT: %a.loc33_22.1: %Derived = param a
  106. // CHECK:STDOUT: @PassDerivedToBase.%a: %Derived = bind_name a, %a.loc33_22.1
  107. // CHECK:STDOUT: %int.make_type_32.loc33: init type = call constants.%Int32() [template = i32]
  108. // CHECK:STDOUT: %.loc33_37.1: type = value_of_initializer %int.make_type_32.loc33 [template = i32]
  109. // CHECK:STDOUT: %.loc33_37.2: type = converted %int.make_type_32.loc33, %.loc33_37.1 [template = i32]
  110. // CHECK:STDOUT: @PassDerivedToBase.%return: ref i32 = var <return slot>
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  113. // CHECK:STDOUT: %PassDerivedToBaseIndirect.decl: %PassDerivedToBaseIndirect.type = fn_decl @PassDerivedToBaseIndirect [template = constants.%PassDerivedToBaseIndirect] {
  114. // CHECK:STDOUT: %Derived.ref.loc37: type = name_ref Derived, %Derived.decl.loc11 [template = constants.%Derived]
  115. // CHECK:STDOUT: %.loc37_40: type = ptr_type %Derived [template = constants.%.8]
  116. // CHECK:STDOUT: %p.loc37_30.1: %.8 = param p
  117. // CHECK:STDOUT: @PassDerivedToBaseIndirect.%p: %.8 = bind_name p, %p.loc37_30.1
  118. // CHECK:STDOUT: %int.make_type_32.loc37: init type = call constants.%Int32() [template = i32]
  119. // CHECK:STDOUT: %.loc37_46.1: type = value_of_initializer %int.make_type_32.loc37 [template = i32]
  120. // CHECK:STDOUT: %.loc37_46.2: type = converted %int.make_type_32.loc37, %.loc37_46.1 [template = i32]
  121. // CHECK:STDOUT: @PassDerivedToBaseIndirect.%return: ref i32 = var <return slot>
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: class @Derived {
  126. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  127. // CHECK:STDOUT: %.loc19: %.4 = base_decl %Base, element0 [template]
  128. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  129. // CHECK:STDOUT: %Self.ref.loc21: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  130. // CHECK:STDOUT: %self.loc21_8.1: %Derived = param self
  131. // CHECK:STDOUT: %self.loc21_8.2: %Derived = bind_name self, %self.loc21_8.1
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %G.decl: %G.type.2 = fn_decl @G.2 [template = constants.%G.2] {
  134. // CHECK:STDOUT: %Self.ref.loc22: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  135. // CHECK:STDOUT: %self.loc22_8.1: %Derived = param self
  136. // CHECK:STDOUT: %self.loc22_8.2: %Derived = bind_name self, %self.loc22_8.1
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !members:
  140. // CHECK:STDOUT: .Self = constants.%Derived
  141. // CHECK:STDOUT: .base = %.loc19
  142. // CHECK:STDOUT: .F = %F.decl
  143. // CHECK:STDOUT: .G = %G.decl
  144. // CHECK:STDOUT: extend name_scope2
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: class @Base {
  148. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  149. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  150. // CHECK:STDOUT: %self.loc14_8.1: %Base = param self
  151. // CHECK:STDOUT: %self.loc14_8.2: %Base = bind_name self, %self.loc14_8.1
  152. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  153. // CHECK:STDOUT: %.loc14_25.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  154. // CHECK:STDOUT: %.loc14_25.2: type = converted %int.make_type_32.loc14, %.loc14_25.1 [template = i32]
  155. // CHECK:STDOUT: %return.var.loc14: ref i32 = var <return slot>
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: %G.decl: %G.type.1 = fn_decl @G.1 [template = constants.%G.1] {
  158. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  159. // CHECK:STDOUT: %self.loc15_8.1: %Derived = param self
  160. // CHECK:STDOUT: %self.loc15_8.2: %Derived = bind_name self, %self.loc15_8.1
  161. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  162. // CHECK:STDOUT: %.loc15_28.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  163. // CHECK:STDOUT: %.loc15_28.2: type = converted %int.make_type_32.loc15, %.loc15_28.1 [template = i32]
  164. // CHECK:STDOUT: %return.var.loc15: ref i32 = var <return slot>
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !members:
  168. // CHECK:STDOUT: .Self = constants.%Base
  169. // CHECK:STDOUT: .F = %F.decl
  170. // CHECK:STDOUT: .G = %G.decl
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: fn @F.1[@Base.%self.loc14_8.2: %Base]() -> i32;
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @G.1[@Base.%self.loc15_8.2: %Derived]() -> i32;
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: fn @F.2[@Derived.%self.loc21_8.2: %Derived]();
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: fn @G.2[@Derived.%self.loc22_8.2: %Derived]();
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: fn @Call(%a: %Derived) -> i32 {
  184. // CHECK:STDOUT: !entry:
  185. // CHECK:STDOUT: %a.ref: %Derived = name_ref a, %a
  186. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  187. // CHECK:STDOUT: %F.ref: %F.type.1 = name_ref F, @Base.%F.decl [template = constants.%F.1]
  188. // CHECK:STDOUT: %.loc26_11: <bound method> = bound_method %a.ref, %F.ref
  189. // CHECK:STDOUT: %.loc26_20.1: ref %Base = class_element_access %a.ref, element0
  190. // CHECK:STDOUT: %.loc26_20.2: ref %Base = converted %a.ref, %.loc26_20.1
  191. // CHECK:STDOUT: %.loc26_20.3: %Base = bind_value %.loc26_20.2
  192. // CHECK:STDOUT: %F.call: init i32 = call %.loc26_11(%.loc26_20.3)
  193. // CHECK:STDOUT: %.loc26_22.1: i32 = value_of_initializer %F.call
  194. // CHECK:STDOUT: %.loc26_22.2: i32 = converted %F.call, %.loc26_22.1
  195. // CHECK:STDOUT: return %.loc26_22.2
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: fn @CallIndirect(%p: %.8) -> i32 {
  199. // CHECK:STDOUT: !entry:
  200. // CHECK:STDOUT: %p.ref: %.8 = name_ref p, %p
  201. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  202. // CHECK:STDOUT: %F.ref: %F.type.1 = name_ref F, @Base.%F.decl [template = constants.%F.1]
  203. // CHECK:STDOUT: %.loc30_11.1: ref %Derived = deref %p.ref
  204. // CHECK:STDOUT: %.loc30_11.2: <bound method> = bound_method %.loc30_11.1, %F.ref
  205. // CHECK:STDOUT: %.loc30_21.1: ref %Base = class_element_access %.loc30_11.1, element0
  206. // CHECK:STDOUT: %.loc30_21.2: ref %Base = converted %.loc30_11.1, %.loc30_21.1
  207. // CHECK:STDOUT: %.loc30_21.3: %Base = bind_value %.loc30_21.2
  208. // CHECK:STDOUT: %F.call: init i32 = call %.loc30_11.2(%.loc30_21.3)
  209. // CHECK:STDOUT: %.loc30_23.1: i32 = value_of_initializer %F.call
  210. // CHECK:STDOUT: %.loc30_23.2: i32 = converted %F.call, %.loc30_23.1
  211. // CHECK:STDOUT: return %.loc30_23.2
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: fn @PassDerivedToBase(%a: %Derived) -> i32 {
  215. // CHECK:STDOUT: !entry:
  216. // CHECK:STDOUT: %a.ref: %Derived = name_ref a, %a
  217. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  218. // CHECK:STDOUT: %G.ref: %G.type.1 = name_ref G, @Base.%G.decl [template = constants.%G.1]
  219. // CHECK:STDOUT: %.loc34_11: <bound method> = bound_method %a.ref, %G.ref
  220. // CHECK:STDOUT: %G.call: init i32 = call %.loc34_11(%a.ref)
  221. // CHECK:STDOUT: %.loc34_22.1: i32 = value_of_initializer %G.call
  222. // CHECK:STDOUT: %.loc34_22.2: i32 = converted %G.call, %.loc34_22.1
  223. // CHECK:STDOUT: return %.loc34_22.2
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: fn @PassDerivedToBaseIndirect(%p: %.8) -> i32 {
  227. // CHECK:STDOUT: !entry:
  228. // CHECK:STDOUT: %p.ref: %.8 = name_ref p, %p
  229. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  230. // CHECK:STDOUT: %G.ref: %G.type.1 = name_ref G, @Base.%G.decl [template = constants.%G.1]
  231. // CHECK:STDOUT: %.loc38_11.1: ref %Derived = deref %p.ref
  232. // CHECK:STDOUT: %.loc38_11.2: <bound method> = bound_method %.loc38_11.1, %G.ref
  233. // CHECK:STDOUT: %.loc38_11.3: %Derived = bind_value %.loc38_11.1
  234. // CHECK:STDOUT: %G.call: init i32 = call %.loc38_11.2(%.loc38_11.3)
  235. // CHECK:STDOUT: %.loc38_23.1: i32 = value_of_initializer %G.call
  236. // CHECK:STDOUT: %.loc38_23.2: i32 = converted %G.call, %.loc38_23.1
  237. // CHECK:STDOUT: return %.loc38_23.2
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT: