base_field.carbon 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/base_field.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base_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 Access(p: Derived*) -> i32* {
  21. return &(*p).c;
  22. }
  23. // CHECK:STDOUT: --- base_field.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  27. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  28. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  29. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  30. // CHECK:STDOUT: %.2: type = unbound_element_type %Base, i32 [template]
  31. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32, .c: i32} [template]
  32. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  33. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  34. // CHECK:STDOUT: %.5: type = unbound_element_type %Derived, %Base [template]
  35. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, i32 [template]
  36. // CHECK:STDOUT: %.7: type = struct_type {.base: %Base, .d: i32, .e: i32} [template]
  37. // CHECK:STDOUT: %.8: type = ptr_type %Derived [template]
  38. // CHECK:STDOUT: %.9: type = ptr_type i32 [template]
  39. // CHECK:STDOUT: %Access.type: type = fn_type @Access [template]
  40. // CHECK:STDOUT: %Access: %Access.type = struct_value () [template]
  41. // CHECK:STDOUT: %.10: type = struct_type {.base: %.4, .d: i32, .e: i32} [template]
  42. // CHECK:STDOUT: %.11: type = ptr_type %.10 [template]
  43. // CHECK:STDOUT: %.12: type = ptr_type %.7 [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: file {
  47. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  48. // CHECK:STDOUT: .Core = %Core
  49. // CHECK:STDOUT: .Base = %Base.decl
  50. // CHECK:STDOUT: .Derived = %Derived.decl
  51. // CHECK:STDOUT: .Access = %Access.decl
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  54. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  55. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  56. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  57. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  58. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  59. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  60. // CHECK:STDOUT: %import_ref.5: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  61. // CHECK:STDOUT: %import_ref.6: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [template = constants.%Access] {
  63. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  64. // CHECK:STDOUT: %.loc24_21: type = ptr_type %Derived [template = constants.%.8]
  65. // CHECK:STDOUT: %p.loc24_11.1: %.8 = param p
  66. // CHECK:STDOUT: @Access.%p: %.8 = bind_name p, %p.loc24_11.1
  67. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  68. // CHECK:STDOUT: %.loc24_30.1: type = value_of_initializer %int.make_type_32 [template = i32]
  69. // CHECK:STDOUT: %.loc24_30.2: type = converted %int.make_type_32, %.loc24_30.1 [template = i32]
  70. // CHECK:STDOUT: %.loc24_30.3: type = ptr_type i32 [template = constants.%.9]
  71. // CHECK:STDOUT: @Access.%return: ref %.9 = var <return slot>
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: class @Base {
  76. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  77. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  78. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  79. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  80. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  82. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  83. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl b, element1 [template]
  84. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  85. // CHECK:STDOUT: %.loc14_10.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  86. // CHECK:STDOUT: %.loc14_10.2: type = converted %int.make_type_32.loc14, %.loc14_10.1 [template = i32]
  87. // CHECK:STDOUT: %.loc14_8: %.2 = field_decl c, element2 [template]
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: !members:
  90. // CHECK:STDOUT: .Self = constants.%Base
  91. // CHECK:STDOUT: .a = %.loc12_8
  92. // CHECK:STDOUT: .b = %.loc13_8
  93. // CHECK:STDOUT: .c = %.loc14_8
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: class @Derived {
  97. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  98. // CHECK:STDOUT: %.loc18: %.5 = base_decl %Base, element0 [template]
  99. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  100. // CHECK:STDOUT: %.loc20_10.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  101. // CHECK:STDOUT: %.loc20_10.2: type = converted %int.make_type_32.loc20, %.loc20_10.1 [template = i32]
  102. // CHECK:STDOUT: %.loc20_8: %.6 = field_decl d, element1 [template]
  103. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc21_10.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  105. // CHECK:STDOUT: %.loc21_10.2: type = converted %int.make_type_32.loc21, %.loc21_10.1 [template = i32]
  106. // CHECK:STDOUT: %.loc21_8: %.6 = field_decl e, element2 [template]
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !members:
  109. // CHECK:STDOUT: .Self = constants.%Derived
  110. // CHECK:STDOUT: .base = %.loc18
  111. // CHECK:STDOUT: .d = %.loc20_8
  112. // CHECK:STDOUT: .e = %.loc21_8
  113. // CHECK:STDOUT: extend name_scope2
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @Access(%p: %.8) -> %.9 {
  119. // CHECK:STDOUT: !entry:
  120. // CHECK:STDOUT: %p.ref: %.8 = name_ref p, %p
  121. // CHECK:STDOUT: %.loc25_12: ref %Derived = deref %p.ref
  122. // CHECK:STDOUT: %c.ref: %.2 = name_ref c, @Base.%.loc14_8 [template = @Base.%.loc14_8]
  123. // CHECK:STDOUT: %.loc25_15.1: ref %Base = class_element_access %.loc25_12, element0
  124. // CHECK:STDOUT: %.loc25_15.2: ref %Base = converted %.loc25_12, %.loc25_15.1
  125. // CHECK:STDOUT: %.loc25_15.3: ref i32 = class_element_access %.loc25_15.2, element2
  126. // CHECK:STDOUT: %.loc25_10: %.9 = addr_of %.loc25_15.3
  127. // CHECK:STDOUT: return %.loc25_10
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: