base.carbon 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/base.carbon
  10. base class Base {
  11. var b: i32;
  12. }
  13. class Derived {
  14. extend base: Base;
  15. var d: i32;
  16. }
  17. fn Make() -> Derived {
  18. return {.base = {.b = 4}, .d = 7};
  19. }
  20. fn Access(d: Derived) -> (i32, i32) {
  21. return (d.d, d.base.b);
  22. }
  23. // CHECK:STDOUT: --- base.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 {.b: 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} [template]
  37. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  38. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  39. // CHECK:STDOUT: %.8: type = struct_type {.base: %.4, .d: i32} [template]
  40. // CHECK:STDOUT: %.9: type = ptr_type %.8 [template]
  41. // CHECK:STDOUT: %.10: type = ptr_type %.7 [template]
  42. // CHECK:STDOUT: %.11: i32 = int_literal 4 [template]
  43. // CHECK:STDOUT: %.12: i32 = int_literal 7 [template]
  44. // CHECK:STDOUT: %.13: type = struct_type {.base: %.3, .d: i32} [template]
  45. // CHECK:STDOUT: %struct.1: %Base = struct_value (%.11) [template]
  46. // CHECK:STDOUT: %struct.2: %Derived = struct_value (%struct.1, %.12) [template]
  47. // CHECK:STDOUT: %.14: type = tuple_type (type, type) [template]
  48. // CHECK:STDOUT: %.15: type = tuple_type (i32, i32) [template]
  49. // CHECK:STDOUT: %Access.type: type = fn_type @Access [template]
  50. // CHECK:STDOUT: %Access: %Access.type = struct_value () [template]
  51. // CHECK:STDOUT: %.16: type = ptr_type %.15 [template]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = %Core
  57. // CHECK:STDOUT: .Base = %Base.decl
  58. // CHECK:STDOUT: .Derived = %Derived.decl
  59. // CHECK:STDOUT: .Make = %Make.decl
  60. // CHECK:STDOUT: .Access = %Access.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  63. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  64. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  65. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  66. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  67. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  68. // CHECK:STDOUT: %Derived.ref.loc21: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  69. // CHECK:STDOUT: @Make.%return: ref %Derived = var <return slot>
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  72. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  73. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [template = constants.%Access] {
  74. // CHECK:STDOUT: %Derived.ref.loc25: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  75. // CHECK:STDOUT: %d.loc25_11.1: %Derived = param d
  76. // CHECK:STDOUT: @Access.%d: %Derived = bind_name d, %d.loc25_11.1
  77. // CHECK:STDOUT: %int.make_type_32.loc25_27: init type = call constants.%Int32() [template = i32]
  78. // CHECK:STDOUT: %int.make_type_32.loc25_32: init type = call constants.%Int32() [template = i32]
  79. // CHECK:STDOUT: %.loc25_35.1: %.14 = tuple_literal (%int.make_type_32.loc25_27, %int.make_type_32.loc25_32)
  80. // CHECK:STDOUT: %.loc25_35.2: type = value_of_initializer %int.make_type_32.loc25_27 [template = i32]
  81. // CHECK:STDOUT: %.loc25_35.3: type = converted %int.make_type_32.loc25_27, %.loc25_35.2 [template = i32]
  82. // CHECK:STDOUT: %.loc25_35.4: type = value_of_initializer %int.make_type_32.loc25_32 [template = i32]
  83. // CHECK:STDOUT: %.loc25_35.5: type = converted %int.make_type_32.loc25_32, %.loc25_35.4 [template = i32]
  84. // CHECK:STDOUT: %.loc25_35.6: type = converted %.loc25_35.1, constants.%.15 [template = constants.%.15]
  85. // CHECK:STDOUT: @Access.%return: ref %.15 = var <return slot>
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: class @Base {
  90. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  91. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  92. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  93. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl b, element0 [template]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !members:
  96. // CHECK:STDOUT: .Self = constants.%Base
  97. // CHECK:STDOUT: .b = %.loc12_8
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: class @Derived {
  101. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  102. // CHECK:STDOUT: %.loc16: %.5 = base_decl %Base, element0 [template]
  103. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  105. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_32, %.loc18_10.1 [template = i32]
  106. // CHECK:STDOUT: %.loc18_8: %.6 = field_decl d, element1 [template]
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !members:
  109. // CHECK:STDOUT: .Self = constants.%Derived
  110. // CHECK:STDOUT: .base = %.loc16
  111. // CHECK:STDOUT: .d = %.loc18_8
  112. // CHECK:STDOUT: extend name_scope2
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn @Make() -> %return: %Derived {
  118. // CHECK:STDOUT: !entry:
  119. // CHECK:STDOUT: %.loc22_25: i32 = int_literal 4 [template = constants.%.11]
  120. // CHECK:STDOUT: %.loc22_26.1: %.3 = struct_literal (%.loc22_25)
  121. // CHECK:STDOUT: %.loc22_34: i32 = int_literal 7 [template = constants.%.12]
  122. // CHECK:STDOUT: %.loc22_35.1: %.13 = struct_literal (%.loc22_26.1, %.loc22_34)
  123. // CHECK:STDOUT: %.loc22_35.2: ref %Base = class_element_access %return, element0
  124. // CHECK:STDOUT: %.loc22_26.2: ref i32 = class_element_access %.loc22_35.2, element0
  125. // CHECK:STDOUT: %.loc22_26.3: init i32 = initialize_from %.loc22_25 to %.loc22_26.2 [template = constants.%.11]
  126. // CHECK:STDOUT: %.loc22_26.4: init %Base = class_init (%.loc22_26.3), %.loc22_35.2 [template = constants.%struct.1]
  127. // CHECK:STDOUT: %.loc22_35.3: init %Base = converted %.loc22_26.1, %.loc22_26.4 [template = constants.%struct.1]
  128. // CHECK:STDOUT: %.loc22_35.4: ref i32 = class_element_access %return, element1
  129. // CHECK:STDOUT: %.loc22_35.5: init i32 = initialize_from %.loc22_34 to %.loc22_35.4 [template = constants.%.12]
  130. // CHECK:STDOUT: %.loc22_35.6: init %Derived = class_init (%.loc22_35.3, %.loc22_35.5), %return [template = constants.%struct.2]
  131. // CHECK:STDOUT: %.loc22_36: init %Derived = converted %.loc22_35.1, %.loc22_35.6 [template = constants.%struct.2]
  132. // CHECK:STDOUT: return %.loc22_36 to %return
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: fn @Access(%d: %Derived) -> %return: %.15 {
  136. // CHECK:STDOUT: !entry:
  137. // CHECK:STDOUT: %d.ref.loc26_11: %Derived = name_ref d, %d
  138. // CHECK:STDOUT: %d.ref.loc26_12: %.6 = name_ref d, @Derived.%.loc18_8 [template = @Derived.%.loc18_8]
  139. // CHECK:STDOUT: %.loc26_12.1: ref i32 = class_element_access %d.ref.loc26_11, element1
  140. // CHECK:STDOUT: %.loc26_12.2: i32 = bind_value %.loc26_12.1
  141. // CHECK:STDOUT: %d.ref.loc26_16: %Derived = name_ref d, %d
  142. // CHECK:STDOUT: %base.ref: %.5 = name_ref base, @Derived.%.loc16 [template = @Derived.%.loc16]
  143. // CHECK:STDOUT: %.loc26_17.1: ref %Base = class_element_access %d.ref.loc26_16, element0
  144. // CHECK:STDOUT: %.loc26_17.2: %Base = bind_value %.loc26_17.1
  145. // CHECK:STDOUT: %b.ref: %.2 = name_ref b, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  146. // CHECK:STDOUT: %.loc26_22.1: ref i32 = class_element_access %.loc26_17.2, element0
  147. // CHECK:STDOUT: %.loc26_22.2: i32 = bind_value %.loc26_22.1
  148. // CHECK:STDOUT: %.loc26_24.1: %.15 = tuple_literal (%.loc26_12.2, %.loc26_22.2)
  149. // CHECK:STDOUT: %.loc26_24.2: ref i32 = tuple_access %return, element0
  150. // CHECK:STDOUT: %.loc26_24.3: init i32 = initialize_from %.loc26_12.2 to %.loc26_24.2
  151. // CHECK:STDOUT: %.loc26_24.4: ref i32 = tuple_access %return, element1
  152. // CHECK:STDOUT: %.loc26_24.5: init i32 = initialize_from %.loc26_22.2 to %.loc26_24.4
  153. // CHECK:STDOUT: %.loc26_24.6: init %.15 = tuple_init (%.loc26_24.3, %.loc26_24.5) to %return
  154. // CHECK:STDOUT: %.loc26_25: init %.15 = converted %.loc26_24.1, %.loc26_24.6
  155. // CHECK:STDOUT: return %.loc26_25 to %return
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: