compound_field.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/compound_field.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/compound_field.carbon
  10. base class Base {
  11. var a: i32;
  12. var b: i32;
  13. var c: i32;
  14. }
  15. class Derived {
  16. extend base: Base;
  17. var d: i32;
  18. var e: i32;
  19. }
  20. fn AccessDerived(d: Derived) -> i32 {
  21. return d.(Derived.d);
  22. }
  23. fn AccessBase(d: Derived) -> i32 {
  24. return d.(Base.b);
  25. }
  26. fn AccessDerivedIndirect(p: Derived*) -> i32* {
  27. return &p->(Derived.d);
  28. }
  29. fn AccessBaseIndirect(p: Derived*) -> i32* {
  30. return &p->(Base.b);
  31. }
  32. // CHECK:STDOUT: --- compound_field.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  36. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  37. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  38. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  39. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, i32 [template]
  40. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32, .c: i32} [template]
  41. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  42. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  43. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  44. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, %Base [template]
  45. // CHECK:STDOUT: %.7: type = unbound_element_type %Derived, i32 [template]
  46. // CHECK:STDOUT: %.8: type = struct_type {.base: %Base, .d: i32, .e: i32} [template]
  47. // CHECK:STDOUT: %.9: <witness> = complete_type_witness %.8 [template]
  48. // CHECK:STDOUT: %AccessDerived.type: type = fn_type @AccessDerived [template]
  49. // CHECK:STDOUT: %AccessDerived: %AccessDerived.type = struct_value () [template]
  50. // CHECK:STDOUT: %.10: type = struct_type {.base: %.5, .d: i32, .e: i32} [template]
  51. // CHECK:STDOUT: %.11: type = ptr_type %.10 [template]
  52. // CHECK:STDOUT: %.12: type = ptr_type %.8 [template]
  53. // CHECK:STDOUT: %AccessBase.type: type = fn_type @AccessBase [template]
  54. // CHECK:STDOUT: %AccessBase: %AccessBase.type = struct_value () [template]
  55. // CHECK:STDOUT: %.13: type = ptr_type %Derived [template]
  56. // CHECK:STDOUT: %.14: type = ptr_type i32 [template]
  57. // CHECK:STDOUT: %AccessDerivedIndirect.type: type = fn_type @AccessDerivedIndirect [template]
  58. // CHECK:STDOUT: %AccessDerivedIndirect: %AccessDerivedIndirect.type = struct_value () [template]
  59. // CHECK:STDOUT: %AccessBaseIndirect.type: type = fn_type @AccessBaseIndirect [template]
  60. // CHECK:STDOUT: %AccessBaseIndirect: %AccessBaseIndirect.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/operators
  68. // CHECK:STDOUT: import Core//prelude/types
  69. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  70. // CHECK:STDOUT: import Core//prelude/operators/as
  71. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  72. // CHECK:STDOUT: import Core//prelude/operators/comparison
  73. // CHECK:STDOUT: import Core//prelude/types/bool
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: file {
  79. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  80. // CHECK:STDOUT: .Core = imports.%Core
  81. // CHECK:STDOUT: .Base = %Base.decl
  82. // CHECK:STDOUT: .Derived = %Derived.decl
  83. // CHECK:STDOUT: .AccessDerived = %AccessDerived.decl
  84. // CHECK:STDOUT: .AccessBase = %AccessBase.decl
  85. // CHECK:STDOUT: .AccessDerivedIndirect = %AccessDerivedIndirect.decl
  86. // CHECK:STDOUT: .AccessBaseIndirect = %AccessBaseIndirect.decl
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %Core.import = import Core
  89. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {} {}
  90. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  91. // CHECK:STDOUT: %AccessDerived.decl: %AccessDerived.type = fn_decl @AccessDerived [template = constants.%AccessDerived] {
  92. // CHECK:STDOUT: %d.patt: %Derived = binding_pattern d
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %Derived.ref.loc24: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  95. // CHECK:STDOUT: %d.param: %Derived = param d, runtime_param0
  96. // CHECK:STDOUT: %d: %Derived = bind_name d, %d.param
  97. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  98. // CHECK:STDOUT: %.loc24_33.1: type = value_of_initializer %int.make_type_32 [template = i32]
  99. // CHECK:STDOUT: %.loc24_33.2: type = converted %int.make_type_32, %.loc24_33.1 [template = i32]
  100. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %AccessBase.decl: %AccessBase.type = fn_decl @AccessBase [template = constants.%AccessBase] {
  103. // CHECK:STDOUT: %d.patt: %Derived = binding_pattern d
  104. // CHECK:STDOUT: } {
  105. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  106. // CHECK:STDOUT: %d.param: %Derived = param d, runtime_param0
  107. // CHECK:STDOUT: %d: %Derived = bind_name d, %d.param
  108. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc28_30.1: type = value_of_initializer %int.make_type_32 [template = i32]
  110. // CHECK:STDOUT: %.loc28_30.2: type = converted %int.make_type_32, %.loc28_30.1 [template = i32]
  111. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %AccessDerivedIndirect.decl: %AccessDerivedIndirect.type = fn_decl @AccessDerivedIndirect [template = constants.%AccessDerivedIndirect] {
  114. // CHECK:STDOUT: %p.patt: %.13 = binding_pattern p
  115. // CHECK:STDOUT: } {
  116. // CHECK:STDOUT: %Derived.ref.loc32: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  117. // CHECK:STDOUT: %.loc32_36: type = ptr_type %Derived [template = constants.%.13]
  118. // CHECK:STDOUT: %p.param: %.13 = param p, runtime_param0
  119. // CHECK:STDOUT: %p: %.13 = bind_name p, %p.param
  120. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  121. // CHECK:STDOUT: %.loc32_45.1: type = value_of_initializer %int.make_type_32 [template = i32]
  122. // CHECK:STDOUT: %.loc32_45.2: type = converted %int.make_type_32, %.loc32_45.1 [template = i32]
  123. // CHECK:STDOUT: %.loc32_45.3: type = ptr_type i32 [template = constants.%.14]
  124. // CHECK:STDOUT: %return: ref %.14 = var <return slot>
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: %AccessBaseIndirect.decl: %AccessBaseIndirect.type = fn_decl @AccessBaseIndirect [template = constants.%AccessBaseIndirect] {
  127. // CHECK:STDOUT: %p.patt: %.13 = binding_pattern p
  128. // CHECK:STDOUT: } {
  129. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  130. // CHECK:STDOUT: %.loc36_33: type = ptr_type %Derived [template = constants.%.13]
  131. // CHECK:STDOUT: %p.param: %.13 = param p, runtime_param0
  132. // CHECK:STDOUT: %p: %.13 = bind_name p, %p.param
  133. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  134. // CHECK:STDOUT: %.loc36_42.1: type = value_of_initializer %int.make_type_32 [template = i32]
  135. // CHECK:STDOUT: %.loc36_42.2: type = converted %int.make_type_32, %.loc36_42.1 [template = i32]
  136. // CHECK:STDOUT: %.loc36_42.3: type = ptr_type i32 [template = constants.%.14]
  137. // CHECK:STDOUT: %return: ref %.14 = var <return slot>
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: class @Base {
  142. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  143. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  144. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  145. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  146. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  147. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  148. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  149. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl b, element1 [template]
  150. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  151. // CHECK:STDOUT: %.loc14_10.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  152. // CHECK:STDOUT: %.loc14_10.2: type = converted %int.make_type_32.loc14, %.loc14_10.1 [template = i32]
  153. // CHECK:STDOUT: %.loc14_8: %.2 = field_decl c, element2 [template]
  154. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.3 [template = constants.%.4]
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = constants.%Base
  158. // CHECK:STDOUT: .a = %.loc12_8
  159. // CHECK:STDOUT: .b = %.loc13_8
  160. // CHECK:STDOUT: .c = %.loc14_8
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: class @Derived {
  164. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  165. // CHECK:STDOUT: %.loc18: %.6 = base_decl %Base, element0 [template]
  166. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  167. // CHECK:STDOUT: %.loc20_10.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  168. // CHECK:STDOUT: %.loc20_10.2: type = converted %int.make_type_32.loc20, %.loc20_10.1 [template = i32]
  169. // CHECK:STDOUT: %.loc20_8: %.7 = field_decl d, element1 [template]
  170. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  171. // CHECK:STDOUT: %.loc21_10.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  172. // CHECK:STDOUT: %.loc21_10.2: type = converted %int.make_type_32.loc21, %.loc21_10.1 [template = i32]
  173. // CHECK:STDOUT: %.loc21_8: %.7 = field_decl e, element2 [template]
  174. // CHECK:STDOUT: %.loc22: <witness> = complete_type_witness %.8 [template = constants.%.9]
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: !members:
  177. // CHECK:STDOUT: .Self = constants.%Derived
  178. // CHECK:STDOUT: .base = %.loc18
  179. // CHECK:STDOUT: .d = %.loc20_8
  180. // CHECK:STDOUT: .e = %.loc21_8
  181. // CHECK:STDOUT: extend name_scope2
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn @AccessDerived(%d: %Derived) -> i32 {
  187. // CHECK:STDOUT: !entry:
  188. // CHECK:STDOUT: %d.ref.loc25_10: %Derived = name_ref d, %d
  189. // CHECK:STDOUT: %Derived.ref.loc25: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  190. // CHECK:STDOUT: %d.ref.loc25_20: %.7 = name_ref d, @Derived.%.loc20_8 [template = @Derived.%.loc20_8]
  191. // CHECK:STDOUT: %.loc25_11.1: ref i32 = class_element_access %d.ref.loc25_10, element1
  192. // CHECK:STDOUT: %.loc25_11.2: i32 = bind_value %.loc25_11.1
  193. // CHECK:STDOUT: return %.loc25_11.2
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: fn @AccessBase(%d: %Derived) -> i32 {
  197. // CHECK:STDOUT: !entry:
  198. // CHECK:STDOUT: %d.ref: %Derived = name_ref d, %d
  199. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  200. // CHECK:STDOUT: %b.ref: %.2 = name_ref b, @Base.%.loc13_8 [template = @Base.%.loc13_8]
  201. // CHECK:STDOUT: %.loc29_11.1: ref %Base = class_element_access %d.ref, element0
  202. // CHECK:STDOUT: %.loc29_11.2: ref %Base = converted %d.ref, %.loc29_11.1
  203. // CHECK:STDOUT: %.loc29_11.3: ref i32 = class_element_access %.loc29_11.2, element1
  204. // CHECK:STDOUT: %.loc29_11.4: i32 = bind_value %.loc29_11.3
  205. // CHECK:STDOUT: return %.loc29_11.4
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: fn @AccessDerivedIndirect(%p: %.13) -> %.14 {
  209. // CHECK:STDOUT: !entry:
  210. // CHECK:STDOUT: %p.ref: %.13 = name_ref p, %p
  211. // CHECK:STDOUT: %Derived.ref.loc33: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  212. // CHECK:STDOUT: %d.ref: %.7 = name_ref d, @Derived.%.loc20_8 [template = @Derived.%.loc20_8]
  213. // CHECK:STDOUT: %.loc33_12.1: ref %Derived = deref %p.ref
  214. // CHECK:STDOUT: %.loc33_12.2: ref i32 = class_element_access %.loc33_12.1, element1
  215. // CHECK:STDOUT: %.loc33_10: %.14 = addr_of %.loc33_12.2
  216. // CHECK:STDOUT: return %.loc33_10
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @AccessBaseIndirect(%p: %.13) -> %.14 {
  220. // CHECK:STDOUT: !entry:
  221. // CHECK:STDOUT: %p.ref: %.13 = name_ref p, %p
  222. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  223. // CHECK:STDOUT: %b.ref: %.2 = name_ref b, @Base.%.loc13_8 [template = @Base.%.loc13_8]
  224. // CHECK:STDOUT: %.loc37_12.1: ref %Derived = deref %p.ref
  225. // CHECK:STDOUT: %.loc37_12.2: ref %Base = class_element_access %.loc37_12.1, element0
  226. // CHECK:STDOUT: %.loc37_12.3: ref %Base = converted %.loc37_12.1, %.loc37_12.2
  227. // CHECK:STDOUT: %.loc37_12.4: ref i32 = class_element_access %.loc37_12.3, element1
  228. // CHECK:STDOUT: %.loc37_10: %.14 = addr_of %.loc37_12.4
  229. // CHECK:STDOUT: return %.loc37_10
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT: