base_method_qualified.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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: %Int32: %Int32.type = struct_value () [template]
  39. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  40. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  41. // CHECK:STDOUT: %G.type.1: type = fn_type @G.1 [template]
  42. // CHECK:STDOUT: %G.1: %G.type.1 = struct_value () [template]
  43. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  44. // CHECK:STDOUT: %.2: <witness> = complete_type_witness %.1 [template]
  45. // CHECK:STDOUT: %.4: type = unbound_element_type %Derived, %Base [template]
  46. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  47. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  48. // CHECK:STDOUT: %G.type.2: type = fn_type @G.2 [template]
  49. // CHECK:STDOUT: %G.2: %G.type.2 = struct_value () [template]
  50. // CHECK:STDOUT: %.5: type = struct_type {.base: %Base} [template]
  51. // CHECK:STDOUT: %.6: <witness> = complete_type_witness %.5 [template]
  52. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  53. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  54. // CHECK:STDOUT: %.9: type = ptr_type %Derived [template]
  55. // CHECK:STDOUT: %CallIndirect.type: type = fn_type @CallIndirect [template]
  56. // CHECK:STDOUT: %CallIndirect: %CallIndirect.type = struct_value () [template]
  57. // CHECK:STDOUT: %PassDerivedToBase.type: type = fn_type @PassDerivedToBase [template]
  58. // CHECK:STDOUT: %PassDerivedToBase: %PassDerivedToBase.type = struct_value () [template]
  59. // CHECK:STDOUT: %PassDerivedToBaseIndirect.type: type = fn_type @PassDerivedToBaseIndirect [template]
  60. // CHECK:STDOUT: %PassDerivedToBaseIndirect: %PassDerivedToBaseIndirect.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: .Int32 = %import_ref
  66. // CHECK:STDOUT: import Core//prelude
  67. // CHECK:STDOUT: import Core//prelude/...
  68. // CHECK:STDOUT: }
  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: .Derived = %Derived.decl.loc11
  75. // CHECK:STDOUT: .Base = %Base.decl
  76. // CHECK:STDOUT: .Call = %Call.decl
  77. // CHECK:STDOUT: .CallIndirect = %CallIndirect.decl
  78. // CHECK:STDOUT: .PassDerivedToBase = %PassDerivedToBase.decl
  79. // CHECK:STDOUT: .PassDerivedToBaseIndirect = %PassDerivedToBaseIndirect.decl
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %Core.import = import Core
  82. // CHECK:STDOUT: %Derived.decl.loc11: type = class_decl @Derived [template = constants.%Derived] {} {}
  83. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  84. // CHECK:STDOUT: %Derived.decl.loc18: type = class_decl @Derived [template = constants.%Derived] {} {}
  85. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {
  86. // CHECK:STDOUT: %a.patt: %Derived = binding_pattern a
  87. // CHECK:STDOUT: %a.param_patt: %Derived = value_param_pattern %a.patt, runtime_param0
  88. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  89. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  90. // CHECK:STDOUT: } {
  91. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  92. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc25_24.1: type = value_of_initializer %int.make_type_32 [template = i32]
  94. // CHECK:STDOUT: %.loc25_24.2: type = converted %int.make_type_32, %.loc25_24.1 [template = i32]
  95. // CHECK:STDOUT: %a.param: %Derived = value_param runtime_param0
  96. // CHECK:STDOUT: %a: %Derived = bind_name a, %a.param
  97. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  98. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %CallIndirect.decl: %CallIndirect.type = fn_decl @CallIndirect [template = constants.%CallIndirect] {
  101. // CHECK:STDOUT: %p.patt: %.9 = binding_pattern p
  102. // CHECK:STDOUT: %p.param_patt: %.9 = value_param_pattern %p.patt, runtime_param0
  103. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  104. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  105. // CHECK:STDOUT: } {
  106. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  107. // CHECK:STDOUT: %.loc29_27: type = ptr_type %Derived [template = constants.%.9]
  108. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc29_33.1: type = value_of_initializer %int.make_type_32 [template = i32]
  110. // CHECK:STDOUT: %.loc29_33.2: type = converted %int.make_type_32, %.loc29_33.1 [template = i32]
  111. // CHECK:STDOUT: %p.param: %.9 = value_param runtime_param0
  112. // CHECK:STDOUT: %p: %.9 = bind_name p, %p.param
  113. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  114. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: %PassDerivedToBase.decl: %PassDerivedToBase.type = fn_decl @PassDerivedToBase [template = constants.%PassDerivedToBase] {
  117. // CHECK:STDOUT: %a.patt: %Derived = binding_pattern a
  118. // CHECK:STDOUT: %a.param_patt: %Derived = value_param_pattern %a.patt, runtime_param0
  119. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  120. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  121. // CHECK:STDOUT: } {
  122. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  123. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  124. // CHECK:STDOUT: %.loc33_37.1: type = value_of_initializer %int.make_type_32 [template = i32]
  125. // CHECK:STDOUT: %.loc33_37.2: type = converted %int.make_type_32, %.loc33_37.1 [template = i32]
  126. // CHECK:STDOUT: %a.param: %Derived = value_param runtime_param0
  127. // CHECK:STDOUT: %a: %Derived = bind_name a, %a.param
  128. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  129. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: %PassDerivedToBaseIndirect.decl: %PassDerivedToBaseIndirect.type = fn_decl @PassDerivedToBaseIndirect [template = constants.%PassDerivedToBaseIndirect] {
  132. // CHECK:STDOUT: %p.patt: %.9 = binding_pattern p
  133. // CHECK:STDOUT: %p.param_patt: %.9 = value_param_pattern %p.patt, runtime_param0
  134. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  135. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  136. // CHECK:STDOUT: } {
  137. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  138. // CHECK:STDOUT: %.loc37_40: type = ptr_type %Derived [template = constants.%.9]
  139. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  140. // CHECK:STDOUT: %.loc37_46.1: type = value_of_initializer %int.make_type_32 [template = i32]
  141. // CHECK:STDOUT: %.loc37_46.2: type = converted %int.make_type_32, %.loc37_46.1 [template = i32]
  142. // CHECK:STDOUT: %p.param: %.9 = value_param runtime_param0
  143. // CHECK:STDOUT: %p: %.9 = bind_name p, %p.param
  144. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  145. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: class @Derived {
  150. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  151. // CHECK:STDOUT: %.loc19: %.4 = base_decl %Base, element0 [template]
  152. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  153. // CHECK:STDOUT: %self.patt: %Derived = binding_pattern self
  154. // CHECK:STDOUT: %self.param_patt: %Derived = value_param_pattern %self.patt, runtime_param0
  155. // CHECK:STDOUT: } {
  156. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  157. // CHECK:STDOUT: %self.param: %Derived = value_param runtime_param0
  158. // CHECK:STDOUT: %self: %Derived = bind_name self, %self.param
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %G.decl: %G.type.2 = fn_decl @G.2 [template = constants.%G.2] {
  161. // CHECK:STDOUT: %self.patt: %Derived = binding_pattern self
  162. // CHECK:STDOUT: %self.param_patt: %Derived = value_param_pattern %self.patt, runtime_param0
  163. // CHECK:STDOUT: } {
  164. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  165. // CHECK:STDOUT: %self.param: %Derived = value_param runtime_param0
  166. // CHECK:STDOUT: %self: %Derived = bind_name self, %self.param
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %.loc23: <witness> = complete_type_witness %.5 [template = constants.%.6]
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !members:
  171. // CHECK:STDOUT: .Self = constants.%Derived
  172. // CHECK:STDOUT: .base = %.loc19
  173. // CHECK:STDOUT: .F = %F.decl
  174. // CHECK:STDOUT: .G = %G.decl
  175. // CHECK:STDOUT: extend %Base.ref
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: class @Base {
  179. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  180. // CHECK:STDOUT: %self.patt: %Base = binding_pattern self
  181. // CHECK:STDOUT: %self.param_patt: %Base = value_param_pattern %self.patt, runtime_param0
  182. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  183. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  184. // CHECK:STDOUT: } {
  185. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  186. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  187. // CHECK:STDOUT: %.loc14_25.1: type = value_of_initializer %int.make_type_32 [template = i32]
  188. // CHECK:STDOUT: %.loc14_25.2: type = converted %int.make_type_32, %.loc14_25.1 [template = i32]
  189. // CHECK:STDOUT: %self.param: %Base = value_param runtime_param0
  190. // CHECK:STDOUT: %self: %Base = bind_name self, %self.param
  191. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  192. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: %G.decl: %G.type.1 = fn_decl @G.1 [template = constants.%G.1] {
  195. // CHECK:STDOUT: %self.patt: %Derived = binding_pattern self
  196. // CHECK:STDOUT: %self.param_patt: %Derived = value_param_pattern %self.patt, runtime_param0
  197. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  198. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param1
  199. // CHECK:STDOUT: } {
  200. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc11 [template = constants.%Derived]
  201. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  202. // CHECK:STDOUT: %.loc15_28.1: type = value_of_initializer %int.make_type_32 [template = i32]
  203. // CHECK:STDOUT: %.loc15_28.2: type = converted %int.make_type_32, %.loc15_28.1 [template = i32]
  204. // CHECK:STDOUT: %self.param: %Derived = value_param runtime_param0
  205. // CHECK:STDOUT: %self: %Derived = bind_name self, %self.param
  206. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param1
  207. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: %.loc16: <witness> = complete_type_witness %.1 [template = constants.%.2]
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: !members:
  212. // CHECK:STDOUT: .Self = constants.%Base
  213. // CHECK:STDOUT: .F = %F.decl
  214. // CHECK:STDOUT: .G = %G.decl
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: fn @F.1[%self.param_patt: %Base]() -> i32;
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @G.1[%self.param_patt: %Derived]() -> i32;
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: fn @F.2[%self.param_patt: %Derived]();
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: fn @G.2[%self.param_patt: %Derived]();
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: fn @Call(%a.param_patt: %Derived) -> i32 {
  226. // CHECK:STDOUT: !entry:
  227. // CHECK:STDOUT: %a.ref: %Derived = name_ref a, %a
  228. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  229. // CHECK:STDOUT: %F.ref: %F.type.1 = name_ref F, @Base.%F.decl [template = constants.%F.1]
  230. // CHECK:STDOUT: %.loc26_11: <bound method> = bound_method %a.ref, %F.ref
  231. // CHECK:STDOUT: %.loc26_10.1: ref %Base = class_element_access %a.ref, element0
  232. // CHECK:STDOUT: %.loc26_10.2: ref %Base = converted %a.ref, %.loc26_10.1
  233. // CHECK:STDOUT: %.loc26_10.3: %Base = bind_value %.loc26_10.2
  234. // CHECK:STDOUT: %F.call: init i32 = call %.loc26_11(%.loc26_10.3)
  235. // CHECK:STDOUT: %.loc26_22.1: i32 = value_of_initializer %F.call
  236. // CHECK:STDOUT: %.loc26_22.2: i32 = converted %F.call, %.loc26_22.1
  237. // CHECK:STDOUT: return %.loc26_22.2
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: fn @CallIndirect(%p.param_patt: %.9) -> i32 {
  241. // CHECK:STDOUT: !entry:
  242. // CHECK:STDOUT: %p.ref: %.9 = name_ref p, %p
  243. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  244. // CHECK:STDOUT: %F.ref: %F.type.1 = name_ref F, @Base.%F.decl [template = constants.%F.1]
  245. // CHECK:STDOUT: %.loc30_11.1: ref %Derived = deref %p.ref
  246. // CHECK:STDOUT: %.loc30_11.2: <bound method> = bound_method %.loc30_11.1, %F.ref
  247. // CHECK:STDOUT: %.loc30_11.3: ref %Base = class_element_access %.loc30_11.1, element0
  248. // CHECK:STDOUT: %.loc30_11.4: ref %Base = converted %.loc30_11.1, %.loc30_11.3
  249. // CHECK:STDOUT: %.loc30_11.5: %Base = bind_value %.loc30_11.4
  250. // CHECK:STDOUT: %F.call: init i32 = call %.loc30_11.2(%.loc30_11.5)
  251. // CHECK:STDOUT: %.loc30_23.1: i32 = value_of_initializer %F.call
  252. // CHECK:STDOUT: %.loc30_23.2: i32 = converted %F.call, %.loc30_23.1
  253. // CHECK:STDOUT: return %.loc30_23.2
  254. // CHECK:STDOUT: }
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: fn @PassDerivedToBase(%a.param_patt: %Derived) -> i32 {
  257. // CHECK:STDOUT: !entry:
  258. // CHECK:STDOUT: %a.ref: %Derived = name_ref a, %a
  259. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  260. // CHECK:STDOUT: %G.ref: %G.type.1 = name_ref G, @Base.%G.decl [template = constants.%G.1]
  261. // CHECK:STDOUT: %.loc34_11: <bound method> = bound_method %a.ref, %G.ref
  262. // CHECK:STDOUT: %G.call: init i32 = call %.loc34_11(%a.ref)
  263. // CHECK:STDOUT: %.loc34_22.1: i32 = value_of_initializer %G.call
  264. // CHECK:STDOUT: %.loc34_22.2: i32 = converted %G.call, %.loc34_22.1
  265. // CHECK:STDOUT: return %.loc34_22.2
  266. // CHECK:STDOUT: }
  267. // CHECK:STDOUT:
  268. // CHECK:STDOUT: fn @PassDerivedToBaseIndirect(%p.param_patt: %.9) -> i32 {
  269. // CHECK:STDOUT: !entry:
  270. // CHECK:STDOUT: %p.ref: %.9 = name_ref p, %p
  271. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  272. // CHECK:STDOUT: %G.ref: %G.type.1 = name_ref G, @Base.%G.decl [template = constants.%G.1]
  273. // CHECK:STDOUT: %.loc38_11.1: ref %Derived = deref %p.ref
  274. // CHECK:STDOUT: %.loc38_11.2: <bound method> = bound_method %.loc38_11.1, %G.ref
  275. // CHECK:STDOUT: %.loc38_11.3: %Derived = bind_value %.loc38_11.1
  276. // CHECK:STDOUT: %G.call: init i32 = call %.loc38_11.2(%.loc38_11.3)
  277. // CHECK:STDOUT: %.loc38_23.1: i32 = value_of_initializer %G.call
  278. // CHECK:STDOUT: %.loc38_23.2: i32 = converted %G.call, %.loc38_23.1
  279. // CHECK:STDOUT: return %.loc38_23.2
  280. // CHECK:STDOUT: }
  281. // CHECK:STDOUT: