base_method_qualified.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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: %F.1: type = fn_type @F.1 [template]
  34. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  35. // CHECK:STDOUT: %struct.1: F = struct_value () [template]
  36. // CHECK:STDOUT: %G.1: type = fn_type @G.1 [template]
  37. // CHECK:STDOUT: %struct.2: G = struct_value () [template]
  38. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  39. // CHECK:STDOUT: %.3: type = ptr_type {} [template]
  40. // CHECK:STDOUT: %.4: type = unbound_element_type Derived, Base [template]
  41. // CHECK:STDOUT: %F.2: type = fn_type @F.2 [template]
  42. // CHECK:STDOUT: %struct.3: F = struct_value () [template]
  43. // CHECK:STDOUT: %G.2: type = fn_type @G.2 [template]
  44. // CHECK:STDOUT: %struct.4: G = struct_value () [template]
  45. // CHECK:STDOUT: %.5: type = struct_type {.base: Base} [template]
  46. // CHECK:STDOUT: %Call: type = fn_type @Call [template]
  47. // CHECK:STDOUT: %struct.5: Call = struct_value () [template]
  48. // CHECK:STDOUT: %.6: type = struct_type {.base: {}*} [template]
  49. // CHECK:STDOUT: %.7: type = ptr_type {.base: Base} [template]
  50. // CHECK:STDOUT: %.8: type = ptr_type Derived [template]
  51. // CHECK:STDOUT: %CallIndirect: type = fn_type @CallIndirect [template]
  52. // CHECK:STDOUT: %struct.6: CallIndirect = struct_value () [template]
  53. // CHECK:STDOUT: %PassDerivedToBase: type = fn_type @PassDerivedToBase [template]
  54. // CHECK:STDOUT: %struct.7: PassDerivedToBase = struct_value () [template]
  55. // CHECK:STDOUT: %PassDerivedToBaseIndirect: type = fn_type @PassDerivedToBaseIndirect [template]
  56. // CHECK:STDOUT: %struct.8: PassDerivedToBaseIndirect = struct_value () [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .Core = %Core
  62. // CHECK:STDOUT: .Derived = %Derived.decl.loc7
  63. // CHECK:STDOUT: .Base = %Base.decl
  64. // CHECK:STDOUT: .Call = %Call.decl
  65. // CHECK:STDOUT: .CallIndirect = %CallIndirect.decl
  66. // CHECK:STDOUT: .PassDerivedToBase = %PassDerivedToBase.decl
  67. // CHECK:STDOUT: .PassDerivedToBaseIndirect = %PassDerivedToBaseIndirect.decl
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  70. // CHECK:STDOUT: %Derived.decl.loc7: type = class_decl @Derived [template = constants.%Derived] {}
  71. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  72. // CHECK:STDOUT: %Derived.decl.loc14: type = class_decl @Derived [template = constants.%Derived] {}
  73. // CHECK:STDOUT: %Call.decl: Call = fn_decl @Call [template = constants.%struct.5] {
  74. // CHECK:STDOUT: %Derived.ref.loc21: type = name_ref Derived, %Derived.decl.loc7 [template = constants.%Derived]
  75. // CHECK:STDOUT: %a.loc21_9.1: Derived = param a
  76. // CHECK:STDOUT: @Call.%a: Derived = bind_name a, %a.loc21_9.1
  77. // CHECK:STDOUT: @Call.%return: ref i32 = var <return slot>
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %CallIndirect.decl: CallIndirect = fn_decl @CallIndirect [template = constants.%struct.6] {
  80. // CHECK:STDOUT: %Derived.ref.loc25: type = name_ref Derived, %Derived.decl.loc7 [template = constants.%Derived]
  81. // CHECK:STDOUT: %.loc25: type = ptr_type Derived [template = constants.%.8]
  82. // CHECK:STDOUT: %p.loc25_17.1: Derived* = param p
  83. // CHECK:STDOUT: @CallIndirect.%p: Derived* = bind_name p, %p.loc25_17.1
  84. // CHECK:STDOUT: @CallIndirect.%return: ref i32 = var <return slot>
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %PassDerivedToBase.decl: PassDerivedToBase = fn_decl @PassDerivedToBase [template = constants.%struct.7] {
  87. // CHECK:STDOUT: %Derived.ref.loc29: type = name_ref Derived, %Derived.decl.loc7 [template = constants.%Derived]
  88. // CHECK:STDOUT: %a.loc29_22.1: Derived = param a
  89. // CHECK:STDOUT: @PassDerivedToBase.%a: Derived = bind_name a, %a.loc29_22.1
  90. // CHECK:STDOUT: @PassDerivedToBase.%return: ref i32 = var <return slot>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %PassDerivedToBaseIndirect.decl: PassDerivedToBaseIndirect = fn_decl @PassDerivedToBaseIndirect [template = constants.%struct.8] {
  93. // CHECK:STDOUT: %Derived.ref.loc33: type = name_ref Derived, %Derived.decl.loc7 [template = constants.%Derived]
  94. // CHECK:STDOUT: %.loc33: type = ptr_type Derived [template = constants.%.8]
  95. // CHECK:STDOUT: %p.loc33_30.1: Derived* = param p
  96. // CHECK:STDOUT: @PassDerivedToBaseIndirect.%p: Derived* = bind_name p, %p.loc33_30.1
  97. // CHECK:STDOUT: @PassDerivedToBaseIndirect.%return: ref i32 = var <return slot>
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: class @Derived {
  102. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  103. // CHECK:STDOUT: %.loc15: <unbound element of class Derived> = base_decl Base, element0 [template]
  104. // CHECK:STDOUT: %F.decl: F = fn_decl @F.2 [template = constants.%struct.3] {
  105. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  106. // CHECK:STDOUT: %self.loc17_8.1: Derived = param self
  107. // CHECK:STDOUT: %self.loc17_8.2: Derived = bind_name self, %self.loc17_8.1
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT: %G.decl: G = fn_decl @G.2 [template = constants.%struct.4] {
  110. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Derived [template = constants.%Derived]
  111. // CHECK:STDOUT: %self.loc18_8.1: Derived = param self
  112. // CHECK:STDOUT: %self.loc18_8.2: Derived = bind_name self, %self.loc18_8.1
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !members:
  116. // CHECK:STDOUT: .Self = constants.%Derived
  117. // CHECK:STDOUT: .base = %.loc15
  118. // CHECK:STDOUT: .F = %F.decl
  119. // CHECK:STDOUT: .G = %G.decl
  120. // CHECK:STDOUT: extend name_scope2
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: class @Base {
  124. // CHECK:STDOUT: %F.decl: F = fn_decl @F.1 [template = constants.%struct.1] {
  125. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [template = constants.%Base]
  126. // CHECK:STDOUT: %self.loc10_8.1: Base = param self
  127. // CHECK:STDOUT: %self.loc10_8.2: Base = bind_name self, %self.loc10_8.1
  128. // CHECK:STDOUT: %return.var.loc10: ref i32 = var <return slot>
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %G.decl: G = fn_decl @G.1 [template = constants.%struct.2] {
  131. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl.loc7 [template = constants.%Derived]
  132. // CHECK:STDOUT: %self.loc11_8.1: Derived = param self
  133. // CHECK:STDOUT: %self.loc11_8.2: Derived = bind_name self, %self.loc11_8.1
  134. // CHECK:STDOUT: %return.var.loc11: ref i32 = var <return slot>
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = constants.%Base
  139. // CHECK:STDOUT: .F = %F.decl
  140. // CHECK:STDOUT: .G = %G.decl
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn @F.1[@Base.%self.loc10_8.2: Base]() -> i32;
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @G.1[@Base.%self.loc11_8.2: Derived]() -> i32;
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: fn @F.2[@Derived.%self.loc17_8.2: Derived]();
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: fn @G.2[@Derived.%self.loc18_8.2: Derived]();
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @Call(%a: Derived) -> i32 {
  152. // CHECK:STDOUT: !entry:
  153. // CHECK:STDOUT: %a.ref: Derived = name_ref a, %a
  154. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  155. // CHECK:STDOUT: %F.ref: F = name_ref F, @Base.%F.decl [template = constants.%struct.1]
  156. // CHECK:STDOUT: %.loc22_11: <bound method> = bound_method %a.ref, %F.ref
  157. // CHECK:STDOUT: %.loc22_20.1: ref Base = class_element_access %a.ref, element0
  158. // CHECK:STDOUT: %.loc22_20.2: ref Base = converted %a.ref, %.loc22_20.1
  159. // CHECK:STDOUT: %.loc22_20.3: Base = bind_value %.loc22_20.2
  160. // CHECK:STDOUT: %F.call: init i32 = call %.loc22_11(%.loc22_20.3)
  161. // CHECK:STDOUT: %.loc22_22.1: i32 = value_of_initializer %F.call
  162. // CHECK:STDOUT: %.loc22_22.2: i32 = converted %F.call, %.loc22_22.1
  163. // CHECK:STDOUT: return %.loc22_22.2
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @CallIndirect(%p: Derived*) -> i32 {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %p.ref: Derived* = name_ref p, %p
  169. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  170. // CHECK:STDOUT: %F.ref: F = name_ref F, @Base.%F.decl [template = constants.%struct.1]
  171. // CHECK:STDOUT: %.loc26_11.1: ref Derived = deref %p.ref
  172. // CHECK:STDOUT: %.loc26_11.2: <bound method> = bound_method %.loc26_11.1, %F.ref
  173. // CHECK:STDOUT: %.loc26_21.1: ref Base = class_element_access %.loc26_11.1, element0
  174. // CHECK:STDOUT: %.loc26_21.2: ref Base = converted %.loc26_11.1, %.loc26_21.1
  175. // CHECK:STDOUT: %.loc26_21.3: Base = bind_value %.loc26_21.2
  176. // CHECK:STDOUT: %F.call: init i32 = call %.loc26_11.2(%.loc26_21.3)
  177. // CHECK:STDOUT: %.loc26_23.1: i32 = value_of_initializer %F.call
  178. // CHECK:STDOUT: %.loc26_23.2: i32 = converted %F.call, %.loc26_23.1
  179. // CHECK:STDOUT: return %.loc26_23.2
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn @PassDerivedToBase(%a: Derived) -> i32 {
  183. // CHECK:STDOUT: !entry:
  184. // CHECK:STDOUT: %a.ref: Derived = name_ref a, %a
  185. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  186. // CHECK:STDOUT: %G.ref: G = name_ref G, @Base.%G.decl [template = constants.%struct.2]
  187. // CHECK:STDOUT: %.loc30_11: <bound method> = bound_method %a.ref, %G.ref
  188. // CHECK:STDOUT: %G.call: init i32 = call %.loc30_11(%a.ref)
  189. // CHECK:STDOUT: %.loc30_22.1: i32 = value_of_initializer %G.call
  190. // CHECK:STDOUT: %.loc30_22.2: i32 = converted %G.call, %.loc30_22.1
  191. // CHECK:STDOUT: return %.loc30_22.2
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: fn @PassDerivedToBaseIndirect(%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: %G.ref: G = name_ref G, @Base.%G.decl [template = constants.%struct.2]
  199. // CHECK:STDOUT: %.loc34_11.1: ref Derived = deref %p.ref
  200. // CHECK:STDOUT: %.loc34_11.2: <bound method> = bound_method %.loc34_11.1, %G.ref
  201. // CHECK:STDOUT: %.loc34_11.3: Derived = bind_value %.loc34_11.1
  202. // CHECK:STDOUT: %G.call: init i32 = call %.loc34_11.2(%.loc34_11.3)
  203. // CHECK:STDOUT: %.loc34_23.1: i32 = value_of_initializer %G.call
  204. // CHECK:STDOUT: %.loc34_23.2: i32 = converted %G.call, %.loc34_23.1
  205. // CHECK:STDOUT: return %.loc34_23.2
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT: