derived_to_base.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/derived_to_base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/derived_to_base.carbon
  10. base class A {
  11. var a: i32;
  12. }
  13. base class B {
  14. extend base: A;
  15. var b: i32;
  16. }
  17. class C {
  18. extend base: B;
  19. var c: i32;
  20. }
  21. fn ConvertCToB(p: C*) -> B* { return p; }
  22. fn ConvertBToA(p: B*) -> A* { return p; }
  23. fn ConvertCToA(p: C*) -> A* { return p; }
  24. fn ConvertValue(c: C) {
  25. let a: A = c;
  26. }
  27. fn ConvertRef(c: C*) -> A* {
  28. return &(*c as A);
  29. }
  30. fn ConvertInit() {
  31. let a: A = {.base = {.base = {.a = 1}, .b = 2}, .c = 3} as C;
  32. }
  33. // CHECK:STDOUT: --- derived_to_base.carbon
  34. // CHECK:STDOUT:
  35. // CHECK:STDOUT: constants {
  36. // CHECK:STDOUT: %A: type = class_type @A [template]
  37. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  38. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  39. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  40. // CHECK:STDOUT: %.2: type = unbound_element_type %A, i32 [template]
  41. // CHECK:STDOUT: %.3: type = struct_type {.a: i32} [template]
  42. // CHECK:STDOUT: %B: type = class_type @B [template]
  43. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  44. // CHECK:STDOUT: %.5: type = unbound_element_type %B, %A [template]
  45. // CHECK:STDOUT: %.6: type = unbound_element_type %B, i32 [template]
  46. // CHECK:STDOUT: %.7: type = struct_type {.base: %A, .b: i32} [template]
  47. // CHECK:STDOUT: %C: type = class_type @C [template]
  48. // CHECK:STDOUT: %.8: type = struct_type {.base: %.4, .b: i32} [template]
  49. // CHECK:STDOUT: %.9: type = ptr_type %.8 [template]
  50. // CHECK:STDOUT: %.10: type = ptr_type %.7 [template]
  51. // CHECK:STDOUT: %.11: type = unbound_element_type %C, %B [template]
  52. // CHECK:STDOUT: %.12: type = unbound_element_type %C, i32 [template]
  53. // CHECK:STDOUT: %.13: type = struct_type {.base: %B, .c: i32} [template]
  54. // CHECK:STDOUT: %.14: type = ptr_type %C [template]
  55. // CHECK:STDOUT: %.15: type = ptr_type %B [template]
  56. // CHECK:STDOUT: %ConvertCToB.type: type = fn_type @ConvertCToB [template]
  57. // CHECK:STDOUT: %ConvertCToB: %ConvertCToB.type = struct_value () [template]
  58. // CHECK:STDOUT: %.16: type = struct_type {.base: %.10, .c: i32} [template]
  59. // CHECK:STDOUT: %.17: type = ptr_type %.16 [template]
  60. // CHECK:STDOUT: %.18: type = ptr_type %.13 [template]
  61. // CHECK:STDOUT: %.19: type = ptr_type %A [template]
  62. // CHECK:STDOUT: %ConvertBToA.type: type = fn_type @ConvertBToA [template]
  63. // CHECK:STDOUT: %ConvertBToA: %ConvertBToA.type = struct_value () [template]
  64. // CHECK:STDOUT: %ConvertCToA.type: type = fn_type @ConvertCToA [template]
  65. // CHECK:STDOUT: %ConvertCToA: %ConvertCToA.type = struct_value () [template]
  66. // CHECK:STDOUT: %ConvertValue.type: type = fn_type @ConvertValue [template]
  67. // CHECK:STDOUT: %ConvertValue: %ConvertValue.type = struct_value () [template]
  68. // CHECK:STDOUT: %ConvertRef.type: type = fn_type @ConvertRef [template]
  69. // CHECK:STDOUT: %ConvertRef: %ConvertRef.type = struct_value () [template]
  70. // CHECK:STDOUT: %ConvertInit.type: type = fn_type @ConvertInit [template]
  71. // CHECK:STDOUT: %ConvertInit: %ConvertInit.type = struct_value () [template]
  72. // CHECK:STDOUT: %.20: i32 = int_literal 1 [template]
  73. // CHECK:STDOUT: %.21: i32 = int_literal 2 [template]
  74. // CHECK:STDOUT: %.22: type = struct_type {.base: %.3, .b: i32} [template]
  75. // CHECK:STDOUT: %.23: i32 = int_literal 3 [template]
  76. // CHECK:STDOUT: %.24: type = struct_type {.base: %.22, .c: i32} [template]
  77. // CHECK:STDOUT: %struct.1: %A = struct_value (%.20) [template]
  78. // CHECK:STDOUT: %struct.2: %B = struct_value (%struct.1, %.21) [template]
  79. // CHECK:STDOUT: %struct.3: %C = struct_value (%struct.2, %.23) [template]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: file {
  83. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  84. // CHECK:STDOUT: .Core = %Core
  85. // CHECK:STDOUT: .A = %A.decl
  86. // CHECK:STDOUT: .B = %B.decl
  87. // CHECK:STDOUT: .C = %C.decl
  88. // CHECK:STDOUT: .ConvertCToB = %ConvertCToB.decl
  89. // CHECK:STDOUT: .ConvertBToA = %ConvertBToA.decl
  90. // CHECK:STDOUT: .ConvertCToA = %ConvertCToA.decl
  91. // CHECK:STDOUT: .ConvertValue = %ConvertValue.decl
  92. // CHECK:STDOUT: .ConvertRef = %ConvertRef.decl
  93. // CHECK:STDOUT: .ConvertInit = %ConvertInit.decl
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  96. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  97. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  98. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  99. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  100. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  101. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  102. // CHECK:STDOUT: %ConvertCToB.decl: %ConvertCToB.type = fn_decl @ConvertCToB [template = constants.%ConvertCToB] {
  103. // CHECK:STDOUT: %C.ref.loc25: type = name_ref C, %C.decl [template = constants.%C]
  104. // CHECK:STDOUT: %.loc25_20: type = ptr_type %C [template = constants.%.14]
  105. // CHECK:STDOUT: %p.loc25_16.1: %.14 = param p
  106. // CHECK:STDOUT: @ConvertCToB.%p: %.14 = bind_name p, %p.loc25_16.1
  107. // CHECK:STDOUT: %B.ref.loc25: type = name_ref B, %B.decl [template = constants.%B]
  108. // CHECK:STDOUT: %.loc25_27: type = ptr_type %B [template = constants.%.15]
  109. // CHECK:STDOUT: @ConvertCToB.%return: ref %.15 = var <return slot>
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %ConvertBToA.decl: %ConvertBToA.type = fn_decl @ConvertBToA [template = constants.%ConvertBToA] {
  112. // CHECK:STDOUT: %B.ref.loc26: type = name_ref B, %B.decl [template = constants.%B]
  113. // CHECK:STDOUT: %.loc26_20: type = ptr_type %B [template = constants.%.15]
  114. // CHECK:STDOUT: %p.loc26_16.1: %.15 = param p
  115. // CHECK:STDOUT: @ConvertBToA.%p: %.15 = bind_name p, %p.loc26_16.1
  116. // CHECK:STDOUT: %A.ref.loc26: type = name_ref A, %A.decl [template = constants.%A]
  117. // CHECK:STDOUT: %.loc26_27: type = ptr_type %A [template = constants.%.19]
  118. // CHECK:STDOUT: @ConvertBToA.%return: ref %.19 = var <return slot>
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %ConvertCToA.decl: %ConvertCToA.type = fn_decl @ConvertCToA [template = constants.%ConvertCToA] {
  121. // CHECK:STDOUT: %C.ref.loc27: type = name_ref C, %C.decl [template = constants.%C]
  122. // CHECK:STDOUT: %.loc27_20: type = ptr_type %C [template = constants.%.14]
  123. // CHECK:STDOUT: %p.loc27_16.1: %.14 = param p
  124. // CHECK:STDOUT: @ConvertCToA.%p: %.14 = bind_name p, %p.loc27_16.1
  125. // CHECK:STDOUT: %A.ref.loc27: type = name_ref A, %A.decl [template = constants.%A]
  126. // CHECK:STDOUT: %.loc27_27: type = ptr_type %A [template = constants.%.19]
  127. // CHECK:STDOUT: @ConvertCToA.%return: ref %.19 = var <return slot>
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %ConvertValue.decl: %ConvertValue.type = fn_decl @ConvertValue [template = constants.%ConvertValue] {
  130. // CHECK:STDOUT: %C.ref.loc29: type = name_ref C, %C.decl [template = constants.%C]
  131. // CHECK:STDOUT: %c.loc29_17.1: %C = param c
  132. // CHECK:STDOUT: @ConvertValue.%c: %C = bind_name c, %c.loc29_17.1
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: %ConvertRef.decl: %ConvertRef.type = fn_decl @ConvertRef [template = constants.%ConvertRef] {
  135. // CHECK:STDOUT: %C.ref.loc33: type = name_ref C, %C.decl [template = constants.%C]
  136. // CHECK:STDOUT: %.loc33_19: type = ptr_type %C [template = constants.%.14]
  137. // CHECK:STDOUT: %c.loc33_15.1: %.14 = param c
  138. // CHECK:STDOUT: @ConvertRef.%c: %.14 = bind_name c, %c.loc33_15.1
  139. // CHECK:STDOUT: %A.ref.loc33: type = name_ref A, %A.decl [template = constants.%A]
  140. // CHECK:STDOUT: %.loc33_26: type = ptr_type %A [template = constants.%.19]
  141. // CHECK:STDOUT: @ConvertRef.%return: ref %.19 = var <return slot>
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %ConvertInit.decl: %ConvertInit.type = fn_decl @ConvertInit [template = constants.%ConvertInit] {}
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: class @A {
  147. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  148. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  149. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32, %.loc12_10.1 [template = i32]
  150. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl a, element0 [template]
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: !members:
  153. // CHECK:STDOUT: .Self = constants.%A
  154. // CHECK:STDOUT: .a = %.loc12_8
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: class @B {
  158. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  159. // CHECK:STDOUT: %.loc16: %.5 = base_decl %A, element0 [template]
  160. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  161. // CHECK:STDOUT: %.loc17_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  162. // CHECK:STDOUT: %.loc17_10.2: type = converted %int.make_type_32, %.loc17_10.1 [template = i32]
  163. // CHECK:STDOUT: %.loc17_8: %.6 = field_decl b, element1 [template]
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !members:
  166. // CHECK:STDOUT: .Self = constants.%B
  167. // CHECK:STDOUT: .base = %.loc16
  168. // CHECK:STDOUT: .b = %.loc17_8
  169. // CHECK:STDOUT: extend name_scope2
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: class @C {
  173. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  174. // CHECK:STDOUT: %.loc21: %.11 = base_decl %B, element0 [template]
  175. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  176. // CHECK:STDOUT: %.loc22_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  177. // CHECK:STDOUT: %.loc22_10.2: type = converted %int.make_type_32, %.loc22_10.1 [template = i32]
  178. // CHECK:STDOUT: %.loc22_8: %.12 = field_decl c, element1 [template]
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: !members:
  181. // CHECK:STDOUT: .Self = constants.%C
  182. // CHECK:STDOUT: .base = %.loc21
  183. // CHECK:STDOUT: .c = %.loc22_8
  184. // CHECK:STDOUT: extend name_scope3
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn @ConvertCToB(%p: %.14) -> %.15 {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: %p.ref: %.14 = name_ref p, %p
  192. // CHECK:STDOUT: %.loc25_39.1: ref %C = deref %p.ref
  193. // CHECK:STDOUT: %.loc25_39.2: ref %B = class_element_access %.loc25_39.1, element0
  194. // CHECK:STDOUT: %.loc25_39.3: %.15 = addr_of %.loc25_39.2
  195. // CHECK:STDOUT: %.loc25_39.4: %.15 = converted %p.ref, %.loc25_39.3
  196. // CHECK:STDOUT: return %.loc25_39.4
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @ConvertBToA(%p: %.15) -> %.19 {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %p.ref: %.15 = name_ref p, %p
  202. // CHECK:STDOUT: %.loc26_39.1: ref %B = deref %p.ref
  203. // CHECK:STDOUT: %.loc26_39.2: ref %A = class_element_access %.loc26_39.1, element0
  204. // CHECK:STDOUT: %.loc26_39.3: %.19 = addr_of %.loc26_39.2
  205. // CHECK:STDOUT: %.loc26_39.4: %.19 = converted %p.ref, %.loc26_39.3
  206. // CHECK:STDOUT: return %.loc26_39.4
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: fn @ConvertCToA(%p: %.14) -> %.19 {
  210. // CHECK:STDOUT: !entry:
  211. // CHECK:STDOUT: %p.ref: %.14 = name_ref p, %p
  212. // CHECK:STDOUT: %.loc27_39.1: ref %C = deref %p.ref
  213. // CHECK:STDOUT: %.loc27_39.2: ref %B = class_element_access %.loc27_39.1, element0
  214. // CHECK:STDOUT: %.loc27_39.3: ref %A = class_element_access %.loc27_39.2, element0
  215. // CHECK:STDOUT: %.loc27_39.4: %.19 = addr_of %.loc27_39.3
  216. // CHECK:STDOUT: %.loc27_39.5: %.19 = converted %p.ref, %.loc27_39.4
  217. // CHECK:STDOUT: return %.loc27_39.5
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: fn @ConvertValue(%c: %C) {
  221. // CHECK:STDOUT: !entry:
  222. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  223. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  224. // CHECK:STDOUT: %.loc30_15.1: ref %B = class_element_access %c.ref, element0
  225. // CHECK:STDOUT: %.loc30_15.2: ref %A = class_element_access %.loc30_15.1, element0
  226. // CHECK:STDOUT: %.loc30_15.3: ref %A = converted %c.ref, %.loc30_15.2
  227. // CHECK:STDOUT: %.loc30_15.4: %A = bind_value %.loc30_15.3
  228. // CHECK:STDOUT: %a: %A = bind_name a, %.loc30_15.4
  229. // CHECK:STDOUT: return
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: fn @ConvertRef(%c: %.14) -> %.19 {
  233. // CHECK:STDOUT: !entry:
  234. // CHECK:STDOUT: %c.ref: %.14 = name_ref c, %c
  235. // CHECK:STDOUT: %.loc34_12: ref %C = deref %c.ref
  236. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  237. // CHECK:STDOUT: %.loc34_15.1: ref %B = class_element_access %.loc34_12, element0
  238. // CHECK:STDOUT: %.loc34_15.2: ref %A = class_element_access %.loc34_15.1, element0
  239. // CHECK:STDOUT: %.loc34_15.3: ref %A = converted %.loc34_12, %.loc34_15.2
  240. // CHECK:STDOUT: %.loc34_10: %.19 = addr_of %.loc34_15.3
  241. // CHECK:STDOUT: return %.loc34_10
  242. // CHECK:STDOUT: }
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: fn @ConvertInit() {
  245. // CHECK:STDOUT: !entry:
  246. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  247. // CHECK:STDOUT: %.loc38_38: i32 = int_literal 1 [template = constants.%.20]
  248. // CHECK:STDOUT: %.loc38_39.1: %.3 = struct_literal (%.loc38_38)
  249. // CHECK:STDOUT: %.loc38_47: i32 = int_literal 2 [template = constants.%.21]
  250. // CHECK:STDOUT: %.loc38_48.1: %.22 = struct_literal (%.loc38_39.1, %.loc38_47)
  251. // CHECK:STDOUT: %.loc38_56: i32 = int_literal 3 [template = constants.%.23]
  252. // CHECK:STDOUT: %.loc38_57.1: %.24 = struct_literal (%.loc38_48.1, %.loc38_56)
  253. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  254. // CHECK:STDOUT: %.loc38_57.2: ref %C = temporary_storage
  255. // CHECK:STDOUT: %.loc38_57.3: ref %B = class_element_access %.loc38_57.2, element0
  256. // CHECK:STDOUT: %.loc38_48.2: ref %A = class_element_access %.loc38_57.3, element0
  257. // CHECK:STDOUT: %.loc38_39.2: ref i32 = class_element_access %.loc38_48.2, element0
  258. // CHECK:STDOUT: %.loc38_39.3: init i32 = initialize_from %.loc38_38 to %.loc38_39.2 [template = constants.%.20]
  259. // CHECK:STDOUT: %.loc38_39.4: init %A = class_init (%.loc38_39.3), %.loc38_48.2 [template = constants.%struct.1]
  260. // CHECK:STDOUT: %.loc38_48.3: init %A = converted %.loc38_39.1, %.loc38_39.4 [template = constants.%struct.1]
  261. // CHECK:STDOUT: %.loc38_48.4: ref i32 = class_element_access %.loc38_57.3, element1
  262. // CHECK:STDOUT: %.loc38_48.5: init i32 = initialize_from %.loc38_47 to %.loc38_48.4 [template = constants.%.21]
  263. // CHECK:STDOUT: %.loc38_48.6: init %B = class_init (%.loc38_48.3, %.loc38_48.5), %.loc38_57.3 [template = constants.%struct.2]
  264. // CHECK:STDOUT: %.loc38_57.4: init %B = converted %.loc38_48.1, %.loc38_48.6 [template = constants.%struct.2]
  265. // CHECK:STDOUT: %.loc38_57.5: ref i32 = class_element_access %.loc38_57.2, element1
  266. // CHECK:STDOUT: %.loc38_57.6: init i32 = initialize_from %.loc38_56 to %.loc38_57.5 [template = constants.%.23]
  267. // CHECK:STDOUT: %.loc38_57.7: init %C = class_init (%.loc38_57.4, %.loc38_57.6), %.loc38_57.2 [template = constants.%struct.3]
  268. // CHECK:STDOUT: %.loc38_57.8: ref %C = temporary %.loc38_57.2, %.loc38_57.7
  269. // CHECK:STDOUT: %.loc38_59: ref %C = converted %.loc38_57.1, %.loc38_57.8
  270. // CHECK:STDOUT: %.loc38_63.1: ref %B = class_element_access %.loc38_59, element0
  271. // CHECK:STDOUT: %.loc38_63.2: ref %A = class_element_access %.loc38_63.1, element0
  272. // CHECK:STDOUT: %.loc38_63.3: ref %A = converted %.loc38_59, %.loc38_63.2
  273. // CHECK:STDOUT: %.loc38_63.4: %A = bind_value %.loc38_63.3
  274. // CHECK:STDOUT: %a: %A = bind_name a, %.loc38_63.4
  275. // CHECK:STDOUT: return
  276. // CHECK:STDOUT: }
  277. // CHECK:STDOUT: