base_method.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/base_method.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base_method.carbon
  14. base class Base {
  15. var a: i32;
  16. fn F[addr self: Self*]();
  17. }
  18. fn Base.F[addr self: Self*]() {
  19. (*self).a = 1;
  20. }
  21. class Derived {
  22. extend base: Base;
  23. }
  24. fn Call(p: Derived*) {
  25. (*p).F();
  26. }
  27. // CHECK:STDOUT: --- base_method.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %Base: type = class_type @Base [concrete]
  31. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  32. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  33. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  34. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  35. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  36. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %i32 [concrete]
  37. // CHECK:STDOUT: %ptr.11f: type = ptr_type %Base [concrete]
  38. // CHECK:STDOUT: %pattern_type.1b9: type = pattern_type %ptr.11f [concrete]
  39. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  40. // CHECK:STDOUT: %Base.F.type: type = fn_type @Base.F [concrete]
  41. // CHECK:STDOUT: %Base.F: %Base.F.type = struct_value () [concrete]
  42. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %i32} [concrete]
  43. // CHECK:STDOUT: %complete_type.fd7: <witness> = complete_type_witness %struct_type.a [concrete]
  44. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  45. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  46. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  47. // CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  48. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  49. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  50. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  51. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340 = struct_value () [symbolic]
  52. // CHECK:STDOUT: %ImplicitAs.impl_witness.204: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.9e9, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  53. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  54. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584 = struct_value () [concrete]
  55. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.204) [concrete]
  56. // CHECK:STDOUT: %.1df: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  57. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete]
  58. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  59. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  60. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  61. // CHECK:STDOUT: %Derived: type = class_type @Derived [concrete]
  62. // CHECK:STDOUT: %Derived.elem: type = unbound_element_type %Derived, %Base [concrete]
  63. // CHECK:STDOUT: %struct_type.base.b1e: type = struct_type {.base: %Base} [concrete]
  64. // CHECK:STDOUT: %complete_type.15c: <witness> = complete_type_witness %struct_type.base.b1e [concrete]
  65. // CHECK:STDOUT: %ptr.404: type = ptr_type %Derived [concrete]
  66. // CHECK:STDOUT: %pattern_type.605: type = pattern_type %ptr.404 [concrete]
  67. // CHECK:STDOUT: %Call.type: type = fn_type @Call [concrete]
  68. // CHECK:STDOUT: %Call: %Call.type = struct_value () [concrete]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: imports {
  72. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  73. // CHECK:STDOUT: .Int = %Core.Int
  74. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  75. // CHECK:STDOUT: import Core//prelude
  76. // CHECK:STDOUT: import Core//prelude/...
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  79. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  80. // CHECK:STDOUT: %Core.import_ref.ee7: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0)]
  81. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.9e9 = impl_witness_table (%Core.import_ref.ee7), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: file {
  85. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  86. // CHECK:STDOUT: .Core = imports.%Core
  87. // CHECK:STDOUT: .Base = %Base.decl
  88. // CHECK:STDOUT: .Derived = %Derived.decl
  89. // CHECK:STDOUT: .Call = %Call.decl
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %Core.import = import Core
  92. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [concrete = constants.%Base] {} {}
  93. // CHECK:STDOUT: %Base.F.decl: %Base.F.type = fn_decl @Base.F [concrete = constants.%Base.F] {
  94. // CHECK:STDOUT: %self.patt: %pattern_type.1b9 = binding_pattern self [concrete]
  95. // CHECK:STDOUT: %self.param_patt: %pattern_type.1b9 = value_param_pattern %self.patt, call_param0 [concrete]
  96. // CHECK:STDOUT: %.loc21_11: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  97. // CHECK:STDOUT: } {
  98. // CHECK:STDOUT: %self.param.loc21: %ptr.11f = value_param call_param0
  99. // CHECK:STDOUT: %.loc21_26: type = splice_block %ptr.loc21 [concrete = constants.%ptr.11f] {
  100. // CHECK:STDOUT: %Self.ref.loc21: type = name_ref Self, constants.%Base [concrete = constants.%Base]
  101. // CHECK:STDOUT: %ptr.loc21: type = ptr_type %Self.ref.loc21 [concrete = constants.%ptr.11f]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %self.loc21: %ptr.11f = bind_name self, %self.param.loc21
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [concrete = constants.%Derived] {} {}
  106. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [concrete = constants.%Call] {
  107. // CHECK:STDOUT: %p.patt: %pattern_type.605 = binding_pattern p [concrete]
  108. // CHECK:STDOUT: %p.param_patt: %pattern_type.605 = value_param_pattern %p.patt, call_param0 [concrete]
  109. // CHECK:STDOUT: } {
  110. // CHECK:STDOUT: %p.param: %ptr.404 = value_param call_param0
  111. // CHECK:STDOUT: %.loc29: type = splice_block %ptr [concrete = constants.%ptr.404] {
  112. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [concrete = constants.%Derived]
  113. // CHECK:STDOUT: %ptr: type = ptr_type %Derived.ref [concrete = constants.%ptr.404]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %p: %ptr.404 = bind_name p, %p.param
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: class @Base {
  120. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  121. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  122. // CHECK:STDOUT: %.loc16: %Base.elem = field_decl a, element0 [concrete]
  123. // CHECK:STDOUT: %Base.F.decl: %Base.F.type = fn_decl @Base.F [concrete = constants.%Base.F] {
  124. // CHECK:STDOUT: %self.patt: %pattern_type.1b9 = binding_pattern self [concrete]
  125. // CHECK:STDOUT: %self.param_patt: %pattern_type.1b9 = value_param_pattern %self.patt, call_param0 [concrete]
  126. // CHECK:STDOUT: %.loc21_11: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  127. // CHECK:STDOUT: } {
  128. // CHECK:STDOUT: %self.param.loc18: %ptr.11f = value_param call_param0
  129. // CHECK:STDOUT: %.loc18: type = splice_block %ptr.loc18 [concrete = constants.%ptr.11f] {
  130. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Base [concrete = constants.%Base]
  131. // CHECK:STDOUT: %ptr.loc18: type = ptr_type %Self.ref.loc18 [concrete = constants.%ptr.11f]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %self.loc18: %ptr.11f = bind_name self, %self.param.loc18
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.a [concrete = constants.%complete_type.fd7]
  136. // CHECK:STDOUT: complete_type_witness = %complete_type
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !members:
  139. // CHECK:STDOUT: .Self = constants.%Base
  140. // CHECK:STDOUT: .a = %.loc16
  141. // CHECK:STDOUT: .F = %Base.F.decl
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: class @Derived {
  145. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [concrete = constants.%Base]
  146. // CHECK:STDOUT: %.loc26: %Derived.elem = base_decl %Base.ref, element0 [concrete]
  147. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.base.b1e [concrete = constants.%complete_type.15c]
  148. // CHECK:STDOUT: complete_type_witness = %complete_type
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: !members:
  151. // CHECK:STDOUT: .Self = constants.%Derived
  152. // CHECK:STDOUT: .Base = <poisoned>
  153. // CHECK:STDOUT: .base = %.loc26
  154. // CHECK:STDOUT: .F = <poisoned>
  155. // CHECK:STDOUT: extend %Base.ref
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: fn @Base.F(%self.param.loc21: %ptr.11f) {
  159. // CHECK:STDOUT: !entry:
  160. // CHECK:STDOUT: %self.ref: %ptr.11f = name_ref self, %self.loc21
  161. // CHECK:STDOUT: %.loc22_4: ref %Base = deref %self.ref
  162. // CHECK:STDOUT: %a.ref: %Base.elem = name_ref a, @Base.%.loc16 [concrete = @Base.%.loc16]
  163. // CHECK:STDOUT: %.loc22_10: ref %i32 = class_element_access %.loc22_4, element0
  164. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  165. // CHECK:STDOUT: %impl.elem0: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0]
  166. // CHECK:STDOUT: %bound_method.loc22_13.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound]
  167. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  168. // CHECK:STDOUT: %bound_method.loc22_13.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method]
  169. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call: init %i32 = call %bound_method.loc22_13.2(%int_1) [concrete = constants.%int_1.5d2]
  170. // CHECK:STDOUT: %.loc22_13: init %i32 = converted %int_1, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_1.5d2]
  171. // CHECK:STDOUT: assign %.loc22_10, %.loc22_13
  172. // CHECK:STDOUT: return
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: fn @Call(%p.param: %ptr.404) {
  176. // CHECK:STDOUT: !entry:
  177. // CHECK:STDOUT: %p.ref: %ptr.404 = name_ref p, %p
  178. // CHECK:STDOUT: %.loc30_4.1: ref %Derived = deref %p.ref
  179. // CHECK:STDOUT: %F.ref: %Base.F.type = name_ref F, @Base.%Base.F.decl [concrete = constants.%Base.F]
  180. // CHECK:STDOUT: %Base.F.bound: <bound method> = bound_method %.loc30_4.1, %F.ref
  181. // CHECK:STDOUT: %addr.loc30_4.1: %ptr.404 = addr_of %.loc30_4.1
  182. // CHECK:STDOUT: %.loc30_4.2: ref %Derived = deref %addr.loc30_4.1
  183. // CHECK:STDOUT: %.loc30_4.3: ref %Base = class_element_access %.loc30_4.2, element0
  184. // CHECK:STDOUT: %addr.loc30_4.2: %ptr.11f = addr_of %.loc30_4.3
  185. // CHECK:STDOUT: %.loc30_4.4: %ptr.11f = converted %addr.loc30_4.1, %addr.loc30_4.2
  186. // CHECK:STDOUT: %Base.F.call: init %empty_tuple.type = call %Base.F.bound(%.loc30_4.4)
  187. // CHECK:STDOUT: return
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT: