base.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: %.4: <witness> = complete_type_witness %.3 [template]
  33. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  34. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  35. // CHECK:STDOUT: %.6: type = unbound_element_type %Derived, %Base [template]
  36. // CHECK:STDOUT: %.7: type = unbound_element_type %Derived, i32 [template]
  37. // CHECK:STDOUT: %.8: type = struct_type {.base: %Base, .d: i32} [template]
  38. // CHECK:STDOUT: %.9: <witness> = complete_type_witness %.8 [template]
  39. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  40. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  41. // CHECK:STDOUT: %.10: type = struct_type {.base: %.5, .d: i32} [template]
  42. // CHECK:STDOUT: %.11: type = ptr_type %.10 [template]
  43. // CHECK:STDOUT: %.12: type = ptr_type %.8 [template]
  44. // CHECK:STDOUT: %.13: i32 = int_literal 4 [template]
  45. // CHECK:STDOUT: %.14: i32 = int_literal 7 [template]
  46. // CHECK:STDOUT: %.15: type = struct_type {.base: %.3, .d: i32} [template]
  47. // CHECK:STDOUT: %struct.1: %Base = struct_value (%.13) [template]
  48. // CHECK:STDOUT: %struct.2: %Derived = struct_value (%struct.1, %.14) [template]
  49. // CHECK:STDOUT: %.16: type = tuple_type (type, type) [template]
  50. // CHECK:STDOUT: %.17: type = tuple_type (i32, i32) [template]
  51. // CHECK:STDOUT: %Access.type: type = fn_type @Access [template]
  52. // CHECK:STDOUT: %Access: %Access.type = struct_value () [template]
  53. // CHECK:STDOUT: %.18: type = ptr_type %.17 [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: .Int32 = %import_ref
  59. // CHECK:STDOUT: import Core//prelude
  60. // CHECK:STDOUT: import Core//prelude/operators
  61. // CHECK:STDOUT: import Core//prelude/types
  62. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  63. // CHECK:STDOUT: import Core//prelude/operators/as
  64. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  65. // CHECK:STDOUT: import Core//prelude/operators/comparison
  66. // CHECK:STDOUT: import Core//prelude/types/bool
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  73. // CHECK:STDOUT: .Core = imports.%Core
  74. // CHECK:STDOUT: .Base = %Base.decl
  75. // CHECK:STDOUT: .Derived = %Derived.decl
  76. // CHECK:STDOUT: .Make = %Make.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 [template = constants.%Base] {} {}
  81. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {} {}
  82. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {} {
  83. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  84. // CHECK:STDOUT: %return: ref %Derived = var <return slot>
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Access.decl: %Access.type = fn_decl @Access [template = constants.%Access] {
  87. // CHECK:STDOUT: %d.patt: %Derived = binding_pattern d
  88. // CHECK:STDOUT: %d.param_patt: %Derived = param_pattern %d.patt, runtime_param0
  89. // CHECK:STDOUT: } {
  90. // CHECK:STDOUT: %Derived.ref: type = name_ref Derived, file.%Derived.decl [template = constants.%Derived]
  91. // CHECK:STDOUT: %int.make_type_32.loc25_27: init type = call constants.%Int32() [template = i32]
  92. // CHECK:STDOUT: %int.make_type_32.loc25_32: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc25_35.1: %.16 = tuple_literal (%int.make_type_32.loc25_27, %int.make_type_32.loc25_32)
  94. // CHECK:STDOUT: %.loc25_35.2: type = value_of_initializer %int.make_type_32.loc25_27 [template = i32]
  95. // CHECK:STDOUT: %.loc25_35.3: type = converted %int.make_type_32.loc25_27, %.loc25_35.2 [template = i32]
  96. // CHECK:STDOUT: %.loc25_35.4: type = value_of_initializer %int.make_type_32.loc25_32 [template = i32]
  97. // CHECK:STDOUT: %.loc25_35.5: type = converted %int.make_type_32.loc25_32, %.loc25_35.4 [template = i32]
  98. // CHECK:STDOUT: %.loc25_35.6: type = converted %.loc25_35.1, constants.%.17 [template = constants.%.17]
  99. // CHECK:STDOUT: %return: ref %.17 = var <return slot>
  100. // CHECK:STDOUT: %param: %Derived = param runtime_param0
  101. // CHECK:STDOUT: %d: %Derived = bind_name d, %param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: class @Base {
  106. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  107. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  108. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  109. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl b, element0 [template]
  110. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.3 [template = constants.%.4]
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !members:
  113. // CHECK:STDOUT: .Self = constants.%Base
  114. // CHECK:STDOUT: .b = %.loc12_8
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: class @Derived {
  118. // CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base.decl [template = constants.%Base]
  119. // CHECK:STDOUT: %.loc16: %.6 = base_decl %Base, element0 [template]
  120. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  121. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  122. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_32, %.loc18_10.1 [template = i32]
  123. // CHECK:STDOUT: %.loc18_8: %.7 = field_decl d, element1 [template]
  124. // CHECK:STDOUT: %.loc19: <witness> = complete_type_witness %.8 [template = constants.%.9]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = constants.%Derived
  128. // CHECK:STDOUT: .base = %.loc16
  129. // CHECK:STDOUT: .d = %.loc18_8
  130. // CHECK:STDOUT: extend name_scope2
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: fn @Make() -> %return: %Derived {
  136. // CHECK:STDOUT: !entry:
  137. // CHECK:STDOUT: %.loc22_25: i32 = int_literal 4 [template = constants.%.13]
  138. // CHECK:STDOUT: %.loc22_26.1: %.3 = struct_literal (%.loc22_25)
  139. // CHECK:STDOUT: %.loc22_34: i32 = int_literal 7 [template = constants.%.14]
  140. // CHECK:STDOUT: %.loc22_35.1: %.15 = struct_literal (%.loc22_26.1, %.loc22_34)
  141. // CHECK:STDOUT: %.loc22_35.2: ref %Base = class_element_access %return, element0
  142. // CHECK:STDOUT: %.loc22_26.2: ref i32 = class_element_access %.loc22_35.2, element0
  143. // CHECK:STDOUT: %.loc22_26.3: init i32 = initialize_from %.loc22_25 to %.loc22_26.2 [template = constants.%.13]
  144. // CHECK:STDOUT: %.loc22_26.4: init %Base = class_init (%.loc22_26.3), %.loc22_35.2 [template = constants.%struct.1]
  145. // CHECK:STDOUT: %.loc22_35.3: init %Base = converted %.loc22_26.1, %.loc22_26.4 [template = constants.%struct.1]
  146. // CHECK:STDOUT: %.loc22_35.4: ref i32 = class_element_access %return, element1
  147. // CHECK:STDOUT: %.loc22_35.5: init i32 = initialize_from %.loc22_34 to %.loc22_35.4 [template = constants.%.14]
  148. // CHECK:STDOUT: %.loc22_35.6: init %Derived = class_init (%.loc22_35.3, %.loc22_35.5), %return [template = constants.%struct.2]
  149. // CHECK:STDOUT: %.loc22_36: init %Derived = converted %.loc22_35.1, %.loc22_35.6 [template = constants.%struct.2]
  150. // CHECK:STDOUT: return %.loc22_36 to %return
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @Access(%d.param_patt: %Derived) -> %return: %.17 {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %d.ref.loc26_11: %Derived = name_ref d, %d
  156. // CHECK:STDOUT: %d.ref.loc26_12: %.7 = name_ref d, @Derived.%.loc18_8 [template = @Derived.%.loc18_8]
  157. // CHECK:STDOUT: %.loc26_12.1: ref i32 = class_element_access %d.ref.loc26_11, element1
  158. // CHECK:STDOUT: %.loc26_12.2: i32 = bind_value %.loc26_12.1
  159. // CHECK:STDOUT: %d.ref.loc26_16: %Derived = name_ref d, %d
  160. // CHECK:STDOUT: %base.ref: %.6 = name_ref base, @Derived.%.loc16 [template = @Derived.%.loc16]
  161. // CHECK:STDOUT: %.loc26_17.1: ref %Base = class_element_access %d.ref.loc26_16, element0
  162. // CHECK:STDOUT: %.loc26_17.2: %Base = bind_value %.loc26_17.1
  163. // CHECK:STDOUT: %b.ref: %.2 = name_ref b, @Base.%.loc12_8 [template = @Base.%.loc12_8]
  164. // CHECK:STDOUT: %.loc26_22.1: ref i32 = class_element_access %.loc26_17.2, element0
  165. // CHECK:STDOUT: %.loc26_22.2: i32 = bind_value %.loc26_22.1
  166. // CHECK:STDOUT: %.loc26_24.1: %.17 = tuple_literal (%.loc26_12.2, %.loc26_22.2)
  167. // CHECK:STDOUT: %.loc26_24.2: ref i32 = tuple_access %return, element0
  168. // CHECK:STDOUT: %.loc26_24.3: init i32 = initialize_from %.loc26_12.2 to %.loc26_24.2
  169. // CHECK:STDOUT: %.loc26_24.4: ref i32 = tuple_access %return, element1
  170. // CHECK:STDOUT: %.loc26_24.5: init i32 = initialize_from %.loc26_22.2 to %.loc26_24.4
  171. // CHECK:STDOUT: %.loc26_24.6: init %.17 = tuple_init (%.loc26_24.3, %.loc26_24.5) to %return
  172. // CHECK:STDOUT: %.loc26_25: init %.17 = converted %.loc26_24.1, %.loc26_24.6
  173. // CHECK:STDOUT: return %.loc26_25 to %return
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: