base_field.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_field.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base_field.carbon
  14. base class Base {
  15. var a: i32;
  16. var b: i32;
  17. var c: i32;
  18. }
  19. class Derived {
  20. extend base: Base;
  21. var d: i32;
  22. var e: i32;
  23. }
  24. fn Access(p: Derived*) -> i32* {
  25. return &(*p).c;
  26. }
  27. // CHECK:STDOUT: --- base_field.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: %Int.generic: %Int.type = struct_value () [concrete]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  35. // CHECK:STDOUT: %Base.elem: type = unbound_element_type %Base, %i32 [concrete]
  36. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  37. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  38. // CHECK:STDOUT: %Destroy.impl_witness.ae4: <witness> = impl_witness @Base.%Destroy.impl_witness_table [concrete]
  39. // CHECK:STDOUT: %ptr.11f: type = ptr_type %Base [concrete]
  40. // CHECK:STDOUT: %pattern_type.1b9: type = pattern_type %ptr.11f [concrete]
  41. // CHECK:STDOUT: %Base.as.Destroy.impl.Op.type: type = fn_type @Base.as.Destroy.impl.Op [concrete]
  42. // CHECK:STDOUT: %Base.as.Destroy.impl.Op: %Base.as.Destroy.impl.Op.type = struct_value () [concrete]
  43. // CHECK:STDOUT: %struct_type.a.b.c: type = struct_type {.a: %i32, .b: %i32, .c: %i32} [concrete]
  44. // CHECK:STDOUT: %complete_type.ebc: <witness> = complete_type_witness %struct_type.a.b.c [concrete]
  45. // CHECK:STDOUT: %Derived: type = class_type @Derived [concrete]
  46. // CHECK:STDOUT: %Derived.elem.69e: type = unbound_element_type %Derived, %Base [concrete]
  47. // CHECK:STDOUT: %Derived.elem.344: type = unbound_element_type %Derived, %i32 [concrete]
  48. // CHECK:STDOUT: %Destroy.impl_witness.e52: <witness> = impl_witness @Derived.%Destroy.impl_witness_table [concrete]
  49. // CHECK:STDOUT: %ptr.404: type = ptr_type %Derived [concrete]
  50. // CHECK:STDOUT: %pattern_type.605: type = pattern_type %ptr.404 [concrete]
  51. // CHECK:STDOUT: %Derived.as.Destroy.impl.Op.type: type = fn_type @Derived.as.Destroy.impl.Op [concrete]
  52. // CHECK:STDOUT: %Derived.as.Destroy.impl.Op: %Derived.as.Destroy.impl.Op.type = struct_value () [concrete]
  53. // CHECK:STDOUT: %struct_type.base.d.e.6a7: type = struct_type {.base: %Base, .d: %i32, .e: %i32} [concrete]
  54. // CHECK:STDOUT: %complete_type.401: <witness> = complete_type_witness %struct_type.base.d.e.6a7 [concrete]
  55. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
  56. // CHECK:STDOUT: %pattern_type.fe8: type = pattern_type %ptr.235 [concrete]
  57. // CHECK:STDOUT: %Access.type: type = fn_type @Access [concrete]
  58. // CHECK:STDOUT: %Access: %Access.type = struct_value () [concrete]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  63. // CHECK:STDOUT: .Int = %Core.Int
  64. // CHECK:STDOUT: .Destroy = %Core.Destroy
  65. // CHECK:STDOUT: import Core//prelude
  66. // CHECK:STDOUT: import Core//prelude/...
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  69. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .Base = %Base.decl
  76. // CHECK:STDOUT: .Derived = %Derived.decl
  77. // CHECK:STDOUT: .Access = %Access.decl
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %Core.import = import Core
  80. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [concrete = constants.%Base] {} {}
  81. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [concrete = constants.%Derived] {} {}
  82. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [concrete = constants.%Access] {
  83. // CHECK:STDOUT: %p.patt: %pattern_type.605 = binding_pattern p [concrete]
  84. // CHECK:STDOUT: %p.param_patt: %pattern_type.605 = value_param_pattern %p.patt, call_param0 [concrete]
  85. // CHECK:STDOUT: %return.patt: %pattern_type.fe8 = return_slot_pattern [concrete]
  86. // CHECK:STDOUT: %return.param_patt: %pattern_type.fe8 = out_param_pattern %return.patt, call_param1 [concrete]
  87. // CHECK:STDOUT: } {
  88. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  89. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  90. // CHECK:STDOUT: %ptr.loc28_30: type = ptr_type %i32 [concrete = constants.%ptr.235]
  91. // CHECK:STDOUT: %p.param: %ptr.404 = value_param call_param0
  92. // CHECK:STDOUT: %.loc28: type = splice_block %ptr.loc28_21 [concrete = constants.%ptr.404] {
  93. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [concrete = constants.%Derived]
  94. // CHECK:STDOUT: %ptr.loc28_21: type = ptr_type %Derived.ref [concrete = constants.%ptr.404]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %p: %ptr.404 = bind_name p, %p.param
  97. // CHECK:STDOUT: %return.param: ref %ptr.235 = out_param call_param1
  98. // CHECK:STDOUT: %return: ref %ptr.235 = return_slot %return.param
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: impl @Base.as.Destroy.impl: @Base.%Self.ref as constants.%Destroy.type {
  103. // CHECK:STDOUT: %Base.as.Destroy.impl.Op.decl: %Base.as.Destroy.impl.Op.type = fn_decl @Base.as.Destroy.impl.Op [concrete = constants.%Base.as.Destroy.impl.Op] {
  104. // CHECK:STDOUT: %self.patt: %pattern_type.1b9 = binding_pattern self [concrete]
  105. // CHECK:STDOUT: %self.param_patt: %pattern_type.1b9 = value_param_pattern %self.patt, call_param0 [concrete]
  106. // CHECK:STDOUT: %.loc15: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  107. // CHECK:STDOUT: } {
  108. // CHECK:STDOUT: %self.param: %ptr.11f = value_param call_param0
  109. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [concrete = constants.%Base]
  110. // CHECK:STDOUT: %self: %ptr.11f = bind_name self, %self.param
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: !members:
  114. // CHECK:STDOUT: .Op = %Base.as.Destroy.impl.Op.decl
  115. // CHECK:STDOUT: witness = @Base.%Destroy.impl_witness
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: impl @Derived.as.Destroy.impl: @Derived.%Self.ref as constants.%Destroy.type {
  119. // CHECK:STDOUT: %Derived.as.Destroy.impl.Op.decl: %Derived.as.Destroy.impl.Op.type = fn_decl @Derived.as.Destroy.impl.Op [concrete = constants.%Derived.as.Destroy.impl.Op] {
  120. // CHECK:STDOUT: %self.patt: %pattern_type.605 = binding_pattern self [concrete]
  121. // CHECK:STDOUT: %self.param_patt: %pattern_type.605 = value_param_pattern %self.patt, call_param0 [concrete]
  122. // CHECK:STDOUT: %.loc21: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  123. // CHECK:STDOUT: } {
  124. // CHECK:STDOUT: %self.param: %ptr.404 = value_param call_param0
  125. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Derived [concrete = constants.%Derived]
  126. // CHECK:STDOUT: %self: %ptr.404 = bind_name self, %self.param
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !members:
  130. // CHECK:STDOUT: .Op = %Derived.as.Destroy.impl.Op.decl
  131. // CHECK:STDOUT: witness = @Derived.%Destroy.impl_witness
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: class @Base {
  135. // CHECK:STDOUT: %int_32.loc16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  136. // CHECK:STDOUT: %i32.loc16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  137. // CHECK:STDOUT: %.loc16: %Base.elem = field_decl a, element0 [concrete]
  138. // CHECK:STDOUT: %int_32.loc17: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  139. // CHECK:STDOUT: %i32.loc17: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  140. // CHECK:STDOUT: %.loc17: %Base.elem = field_decl b, element1 [concrete]
  141. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  142. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  143. // CHECK:STDOUT: %.loc18: %Base.elem = field_decl c, element2 [concrete]
  144. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Base [concrete = constants.%Base]
  145. // CHECK:STDOUT: impl_decl @Base.as.Destroy.impl [concrete] {} {}
  146. // CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@Base.as.Destroy.impl.%Base.as.Destroy.impl.Op.decl), @Base.as.Destroy.impl [concrete]
  147. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.ae4]
  148. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.a.b.c [concrete = constants.%complete_type.ebc]
  149. // CHECK:STDOUT: complete_type_witness = %complete_type
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !members:
  152. // CHECK:STDOUT: .Self = constants.%Base
  153. // CHECK:STDOUT: .a = %.loc16
  154. // CHECK:STDOUT: .b = %.loc17
  155. // CHECK:STDOUT: .c = %.loc18
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: class @Derived {
  159. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [concrete = constants.%Base]
  160. // CHECK:STDOUT: %.loc22: %Derived.elem.69e = base_decl %Base.ref, element0 [concrete]
  161. // CHECK:STDOUT: %int_32.loc24: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  162. // CHECK:STDOUT: %i32.loc24: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  163. // CHECK:STDOUT: %.loc24: %Derived.elem.344 = field_decl d, element1 [concrete]
  164. // CHECK:STDOUT: %int_32.loc25: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  165. // CHECK:STDOUT: %i32.loc25: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  166. // CHECK:STDOUT: %.loc25: %Derived.elem.344 = field_decl e, element2 [concrete]
  167. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Derived [concrete = constants.%Derived]
  168. // CHECK:STDOUT: impl_decl @Derived.as.Destroy.impl [concrete] {} {}
  169. // CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@Derived.as.Destroy.impl.%Derived.as.Destroy.impl.Op.decl), @Derived.as.Destroy.impl [concrete]
  170. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.e52]
  171. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%struct_type.base.d.e.6a7 [concrete = constants.%complete_type.401]
  172. // CHECK:STDOUT: complete_type_witness = %complete_type
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: !members:
  175. // CHECK:STDOUT: .Self = constants.%Derived
  176. // CHECK:STDOUT: .Base = <poisoned>
  177. // CHECK:STDOUT: .base = %.loc22
  178. // CHECK:STDOUT: .d = %.loc24
  179. // CHECK:STDOUT: .e = %.loc25
  180. // CHECK:STDOUT: .c = <poisoned>
  181. // CHECK:STDOUT: extend %Base.ref
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: fn @Base.as.Destroy.impl.Op(%self.param: %ptr.11f) = "no_op";
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn @Derived.as.Destroy.impl.Op(%self.param: %ptr.404) = "no_op";
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: fn @Access(%p.param: %ptr.404) -> %ptr.235 {
  189. // CHECK:STDOUT: !entry:
  190. // CHECK:STDOUT: %p.ref: %ptr.404 = name_ref p, %p
  191. // CHECK:STDOUT: %.loc29_12: ref %Derived = deref %p.ref
  192. // CHECK:STDOUT: %c.ref: %Base.elem = name_ref c, @Base.%.loc18 [concrete = @Base.%.loc18]
  193. // CHECK:STDOUT: %.loc29_15.1: ref %Base = class_element_access %.loc29_12, element0
  194. // CHECK:STDOUT: %.loc29_15.2: ref %Base = converted %.loc29_12, %.loc29_15.1
  195. // CHECK:STDOUT: %.loc29_15.3: ref %i32 = class_element_access %.loc29_15.2, element2
  196. // CHECK:STDOUT: %addr: %ptr.235 = addr_of %.loc29_15.3
  197. // CHECK:STDOUT: return %addr
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT: