base_method_qualified.carbon 14 KB

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