base.carbon 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. base class Base {
  7. var b: i32;
  8. }
  9. class Derived {
  10. extend base: Base;
  11. var d: i32;
  12. }
  13. fn Make() -> Derived {
  14. return {.base = {.b = 4}, .d = 7};
  15. }
  16. fn Access(d: Derived) -> (i32, i32) {
  17. return (d.d, d.base.b);
  18. }
  19. // CHECK:STDOUT: --- base.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %Base: type = class_type @Base [template]
  23. // CHECK:STDOUT: %Int32: type = fn_type @Int32 [template]
  24. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  25. // CHECK:STDOUT: %struct.1: Int32 = struct_value () [template]
  26. // CHECK:STDOUT: %.2: type = unbound_element_type Base, i32 [template]
  27. // CHECK:STDOUT: %.3: type = struct_type {.b: i32} [template]
  28. // CHECK:STDOUT: %Derived: type = class_type @Derived [template]
  29. // CHECK:STDOUT: %.4: type = ptr_type {.b: i32} [template]
  30. // CHECK:STDOUT: %.5: type = unbound_element_type Derived, Base [template]
  31. // CHECK:STDOUT: %.6: type = unbound_element_type Derived, i32 [template]
  32. // CHECK:STDOUT: %.7: type = struct_type {.base: Base, .d: i32} [template]
  33. // CHECK:STDOUT: %Make: type = fn_type @Make [template]
  34. // CHECK:STDOUT: %struct.2: Make = struct_value () [template]
  35. // CHECK:STDOUT: %.8: type = struct_type {.base: {.b: i32}*, .d: i32} [template]
  36. // CHECK:STDOUT: %.9: type = ptr_type {.base: {.b: i32}*, .d: i32} [template]
  37. // CHECK:STDOUT: %.10: type = ptr_type {.base: Base, .d: i32} [template]
  38. // CHECK:STDOUT: %.11: i32 = int_literal 4 [template]
  39. // CHECK:STDOUT: %.12: i32 = int_literal 7 [template]
  40. // CHECK:STDOUT: %.13: type = struct_type {.base: {.b: i32}, .d: i32} [template]
  41. // CHECK:STDOUT: %struct.3: Base = struct_value (%.11) [template]
  42. // CHECK:STDOUT: %struct.4: Derived = struct_value (%struct.3, %.12) [template]
  43. // CHECK:STDOUT: %.14: type = tuple_type (type, type) [template]
  44. // CHECK:STDOUT: %.15: type = tuple_type (i32, i32) [template]
  45. // CHECK:STDOUT: %Access: type = fn_type @Access [template]
  46. // CHECK:STDOUT: %struct.5: Access = struct_value () [template]
  47. // CHECK:STDOUT: %.16: type = ptr_type (i32, i32) [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: file {
  51. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  52. // CHECK:STDOUT: .Core = %Core
  53. // CHECK:STDOUT: .Base = %Base.decl
  54. // CHECK:STDOUT: .Derived = %Derived.decl
  55. // CHECK:STDOUT: .Make = %Make.decl
  56. // CHECK:STDOUT: .Access = %Access.decl
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  59. // CHECK:STDOUT: %Base.decl: type = class_decl @Base [template = constants.%Base] {}
  60. // CHECK:STDOUT: %import_ref.1: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
  61. // CHECK:STDOUT: %Derived.decl: type = class_decl @Derived [template = constants.%Derived] {}
  62. // CHECK:STDOUT: %import_ref.2: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
  63. // CHECK:STDOUT: %Make.decl: Make = fn_decl @Make [template = constants.%struct.2] {
  64. // CHECK:STDOUT: %Derived.ref.loc17: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  65. // CHECK:STDOUT: @Make.%return: ref Derived = var <return slot>
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %import_ref.3: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
  68. // CHECK:STDOUT: %import_ref.4: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
  69. // CHECK:STDOUT: %Access.decl: Access = fn_decl @Access [template = constants.%struct.5] {
  70. // CHECK:STDOUT: %Derived.ref.loc21: type = name_ref Derived, %Derived.decl [template = constants.%Derived]
  71. // CHECK:STDOUT: %d.loc21_11.1: Derived = param d
  72. // CHECK:STDOUT: @Access.%d: Derived = bind_name d, %d.loc21_11.1
  73. // CHECK:STDOUT: %int.make_type_32.loc21_27: init type = call constants.%struct.1() [template = i32]
  74. // CHECK:STDOUT: %int.make_type_32.loc21_32: init type = call constants.%struct.1() [template = i32]
  75. // CHECK:STDOUT: %.loc21_35.1: (type, type) = tuple_literal (%int.make_type_32.loc21_27, %int.make_type_32.loc21_32)
  76. // CHECK:STDOUT: %.loc21_35.2: type = value_of_initializer %int.make_type_32.loc21_27 [template = i32]
  77. // CHECK:STDOUT: %.loc21_35.3: type = converted %int.make_type_32.loc21_27, %.loc21_35.2 [template = i32]
  78. // CHECK:STDOUT: %.loc21_35.4: type = value_of_initializer %int.make_type_32.loc21_32 [template = i32]
  79. // CHECK:STDOUT: %.loc21_35.5: type = converted %int.make_type_32.loc21_32, %.loc21_35.4 [template = i32]
  80. // CHECK:STDOUT: %.loc21_35.6: type = converted %.loc21_35.1, constants.%.15 [template = constants.%.15]
  81. // CHECK:STDOUT: @Access.%return: ref (i32, i32) = var <return slot>
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: class @Base {
  86. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%struct.1() [template = i32]
  87. // CHECK:STDOUT: %.loc8_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  88. // CHECK:STDOUT: %.loc8_10.2: type = converted %int.make_type_32, %.loc8_10.1 [template = i32]
  89. // CHECK:STDOUT: %.loc8_8: <unbound element of class Base> = field_decl b, element0 [template]
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !members:
  92. // CHECK:STDOUT: .Self = constants.%Base
  93. // CHECK:STDOUT: .b = %.loc8_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: %.loc12: <unbound element of class Derived> = base_decl Base, element0 [template]
  99. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%struct.1() [template = i32]
  100. // CHECK:STDOUT: %.loc14_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  101. // CHECK:STDOUT: %.loc14_10.2: type = converted %int.make_type_32, %.loc14_10.1 [template = i32]
  102. // CHECK:STDOUT: %.loc14_8: <unbound element of class Derived> = field_decl d, element1 [template]
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !members:
  105. // CHECK:STDOUT: .Self = constants.%Derived
  106. // CHECK:STDOUT: .base = %.loc12
  107. // CHECK:STDOUT: .d = %.loc14_8
  108. // CHECK:STDOUT: extend name_scope2
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: fn @Make() -> %return: Derived {
  114. // CHECK:STDOUT: !entry:
  115. // CHECK:STDOUT: %.loc18_25: i32 = int_literal 4 [template = constants.%.11]
  116. // CHECK:STDOUT: %.loc18_26.1: {.b: i32} = struct_literal (%.loc18_25)
  117. // CHECK:STDOUT: %.loc18_34: i32 = int_literal 7 [template = constants.%.12]
  118. // CHECK:STDOUT: %.loc18_35.1: {.base: {.b: i32}, .d: i32} = struct_literal (%.loc18_26.1, %.loc18_34)
  119. // CHECK:STDOUT: %.loc18_35.2: ref Base = class_element_access %return, element0
  120. // CHECK:STDOUT: %.loc18_26.2: ref i32 = class_element_access %.loc18_35.2, element0
  121. // CHECK:STDOUT: %.loc18_26.3: init i32 = initialize_from %.loc18_25 to %.loc18_26.2 [template = constants.%.11]
  122. // CHECK:STDOUT: %.loc18_26.4: init Base = class_init (%.loc18_26.3), %.loc18_35.2 [template = constants.%struct.3]
  123. // CHECK:STDOUT: %.loc18_35.3: init Base = converted %.loc18_26.1, %.loc18_26.4 [template = constants.%struct.3]
  124. // CHECK:STDOUT: %.loc18_35.4: ref i32 = class_element_access %return, element1
  125. // CHECK:STDOUT: %.loc18_35.5: init i32 = initialize_from %.loc18_34 to %.loc18_35.4 [template = constants.%.12]
  126. // CHECK:STDOUT: %.loc18_35.6: init Derived = class_init (%.loc18_35.3, %.loc18_35.5), %return [template = constants.%struct.4]
  127. // CHECK:STDOUT: %.loc18_36: init Derived = converted %.loc18_35.1, %.loc18_35.6 [template = constants.%struct.4]
  128. // CHECK:STDOUT: return %.loc18_36 to %return
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: fn @Access(%d: Derived) -> %return: (i32, i32) {
  132. // CHECK:STDOUT: !entry:
  133. // CHECK:STDOUT: %d.ref.loc22_11: Derived = name_ref d, %d
  134. // CHECK:STDOUT: %d.ref.loc22_12: <unbound element of class Derived> = name_ref d, @Derived.%.loc14_8 [template = @Derived.%.loc14_8]
  135. // CHECK:STDOUT: %.loc22_12.1: ref i32 = class_element_access %d.ref.loc22_11, element1
  136. // CHECK:STDOUT: %.loc22_12.2: i32 = bind_value %.loc22_12.1
  137. // CHECK:STDOUT: %d.ref.loc22_16: Derived = name_ref d, %d
  138. // CHECK:STDOUT: %base.ref: <unbound element of class Derived> = name_ref base, @Derived.%.loc12 [template = @Derived.%.loc12]
  139. // CHECK:STDOUT: %.loc22_17.1: ref Base = class_element_access %d.ref.loc22_16, element0
  140. // CHECK:STDOUT: %.loc22_17.2: Base = bind_value %.loc22_17.1
  141. // CHECK:STDOUT: %b.ref: <unbound element of class Base> = name_ref b, @Base.%.loc8_8 [template = @Base.%.loc8_8]
  142. // CHECK:STDOUT: %.loc22_22.1: ref i32 = class_element_access %.loc22_17.2, element0
  143. // CHECK:STDOUT: %.loc22_22.2: i32 = bind_value %.loc22_22.1
  144. // CHECK:STDOUT: %.loc22_24.1: (i32, i32) = tuple_literal (%.loc22_12.2, %.loc22_22.2)
  145. // CHECK:STDOUT: %.loc22_24.2: ref i32 = tuple_access %return, element0
  146. // CHECK:STDOUT: %.loc22_24.3: init i32 = initialize_from %.loc22_12.2 to %.loc22_24.2
  147. // CHECK:STDOUT: %.loc22_24.4: ref i32 = tuple_access %return, element1
  148. // CHECK:STDOUT: %.loc22_24.5: init i32 = initialize_from %.loc22_22.2 to %.loc22_24.4
  149. // CHECK:STDOUT: %.loc22_24.6: init (i32, i32) = tuple_init (%.loc22_24.3, %.loc22_24.5) to %return
  150. // CHECK:STDOUT: %.loc22_25: init (i32, i32) = converted %.loc22_24.1, %.loc22_24.6
  151. // CHECK:STDOUT: return %.loc22_25 to %return
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: