derived_to_base.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 A {
  7. var a: i32;
  8. }
  9. base class B {
  10. extend base: A;
  11. var b: i32;
  12. }
  13. class C {
  14. extend base: B;
  15. var c: i32;
  16. }
  17. fn ConvertCToB(p: C*) -> B* { return p; }
  18. fn ConvertBToA(p: B*) -> A* { return p; }
  19. fn ConvertCToA(p: C*) -> A* { return p; }
  20. fn ConvertValue(c: C) {
  21. let a: A = c;
  22. }
  23. fn ConvertRef(c: C*) -> A* {
  24. return &(*c as A);
  25. }
  26. fn ConvertInit() {
  27. let a: A = {.base = {.base = {.a = 1}, .b = 2}, .c = 3} as C;
  28. }
  29. // CHECK:STDOUT: --- derived_to_base.carbon
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: constants {
  32. // CHECK:STDOUT: %A: type = class_type @A [template]
  33. // CHECK:STDOUT: %.1: type = unbound_element_type A, i32 [template]
  34. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  35. // CHECK:STDOUT: %B: type = class_type @B [template]
  36. // CHECK:STDOUT: %.3: type = ptr_type {.a: i32} [template]
  37. // CHECK:STDOUT: %.4: type = unbound_element_type B, A [template]
  38. // CHECK:STDOUT: %.5: type = unbound_element_type B, i32 [template]
  39. // CHECK:STDOUT: %.6: type = struct_type {.base: A, .b: i32} [template]
  40. // CHECK:STDOUT: %C: type = class_type @C [template]
  41. // CHECK:STDOUT: %.7: type = struct_type {.base: {.a: i32}*, .b: i32} [template]
  42. // CHECK:STDOUT: %.8: type = ptr_type {.base: {.a: i32}*, .b: i32} [template]
  43. // CHECK:STDOUT: %.9: type = ptr_type {.base: A, .b: i32} [template]
  44. // CHECK:STDOUT: %.10: type = unbound_element_type C, B [template]
  45. // CHECK:STDOUT: %.11: type = unbound_element_type C, i32 [template]
  46. // CHECK:STDOUT: %.12: type = struct_type {.base: B, .c: i32} [template]
  47. // CHECK:STDOUT: %.13: type = ptr_type C [template]
  48. // CHECK:STDOUT: %.14: type = ptr_type B [template]
  49. // CHECK:STDOUT: %.15: type = struct_type {.base: {.base: A, .b: i32}*, .c: i32} [template]
  50. // CHECK:STDOUT: %.16: type = ptr_type {.base: {.base: A, .b: i32}*, .c: i32} [template]
  51. // CHECK:STDOUT: %.17: type = ptr_type {.base: B, .c: i32} [template]
  52. // CHECK:STDOUT: %.18: type = ptr_type A [template]
  53. // CHECK:STDOUT: %.19: i32 = int_literal 1 [template]
  54. // CHECK:STDOUT: %.20: i32 = int_literal 2 [template]
  55. // CHECK:STDOUT: %.21: type = struct_type {.base: {.a: i32}, .b: i32} [template]
  56. // CHECK:STDOUT: %.22: i32 = int_literal 3 [template]
  57. // CHECK:STDOUT: %.23: type = struct_type {.base: {.base: {.a: i32}, .b: i32}, .c: i32} [template]
  58. // CHECK:STDOUT: %.24: A = struct_value (%.19) [template]
  59. // CHECK:STDOUT: %.25: B = struct_value (%.24, %.20) [template]
  60. // CHECK:STDOUT: %.26: C = struct_value (%.25, %.22) [template]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  65. // CHECK:STDOUT: .Core = %Core
  66. // CHECK:STDOUT: .A = %A.decl
  67. // CHECK:STDOUT: .B = %B.decl
  68. // CHECK:STDOUT: .C = %C.decl
  69. // CHECK:STDOUT: .ConvertCToB = %ConvertCToB
  70. // CHECK:STDOUT: .ConvertBToA = %ConvertBToA
  71. // CHECK:STDOUT: .ConvertCToA = %ConvertCToA
  72. // CHECK:STDOUT: .ConvertValue = %ConvertValue
  73. // CHECK:STDOUT: .ConvertRef = %ConvertRef
  74. // CHECK:STDOUT: .ConvertInit = %ConvertInit
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  77. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  78. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  79. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  80. // CHECK:STDOUT: %ConvertCToB: <function> = fn_decl @ConvertCToB [template] {
  81. // CHECK:STDOUT: %C.ref.loc21: type = name_ref C, %C.decl [template = constants.%C]
  82. // CHECK:STDOUT: %.loc21_20: type = ptr_type C [template = constants.%.13]
  83. // CHECK:STDOUT: %p.loc21_16.1: C* = param p
  84. // CHECK:STDOUT: @ConvertCToB.%p: C* = bind_name p, %p.loc21_16.1
  85. // CHECK:STDOUT: %B.ref.loc21: type = name_ref B, %B.decl [template = constants.%B]
  86. // CHECK:STDOUT: %.loc21_27: type = ptr_type B [template = constants.%.14]
  87. // CHECK:STDOUT: %return.var.loc21: ref B* = var <return slot>
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %ConvertBToA: <function> = fn_decl @ConvertBToA [template] {
  90. // CHECK:STDOUT: %B.ref.loc22: type = name_ref B, %B.decl [template = constants.%B]
  91. // CHECK:STDOUT: %.loc22_20: type = ptr_type B [template = constants.%.14]
  92. // CHECK:STDOUT: %p.loc22_16.1: B* = param p
  93. // CHECK:STDOUT: @ConvertBToA.%p: B* = bind_name p, %p.loc22_16.1
  94. // CHECK:STDOUT: %A.ref.loc22: type = name_ref A, %A.decl [template = constants.%A]
  95. // CHECK:STDOUT: %.loc22_27: type = ptr_type A [template = constants.%.18]
  96. // CHECK:STDOUT: %return.var.loc22: ref A* = var <return slot>
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %ConvertCToA: <function> = fn_decl @ConvertCToA [template] {
  99. // CHECK:STDOUT: %C.ref.loc23: type = name_ref C, %C.decl [template = constants.%C]
  100. // CHECK:STDOUT: %.loc23_20: type = ptr_type C [template = constants.%.13]
  101. // CHECK:STDOUT: %p.loc23_16.1: C* = param p
  102. // CHECK:STDOUT: @ConvertCToA.%p: C* = bind_name p, %p.loc23_16.1
  103. // CHECK:STDOUT: %A.ref.loc23: type = name_ref A, %A.decl [template = constants.%A]
  104. // CHECK:STDOUT: %.loc23_27: type = ptr_type A [template = constants.%.18]
  105. // CHECK:STDOUT: %return.var.loc23: ref A* = var <return slot>
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %ConvertValue: <function> = fn_decl @ConvertValue [template] {
  108. // CHECK:STDOUT: %C.ref.loc25: type = name_ref C, %C.decl [template = constants.%C]
  109. // CHECK:STDOUT: %c.loc25_17.1: C = param c
  110. // CHECK:STDOUT: @ConvertValue.%c: C = bind_name c, %c.loc25_17.1
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %ConvertRef: <function> = fn_decl @ConvertRef [template] {
  113. // CHECK:STDOUT: %C.ref.loc29: type = name_ref C, %C.decl [template = constants.%C]
  114. // CHECK:STDOUT: %.loc29_19: type = ptr_type C [template = constants.%.13]
  115. // CHECK:STDOUT: %c.loc29_15.1: C* = param c
  116. // CHECK:STDOUT: @ConvertRef.%c: C* = bind_name c, %c.loc29_15.1
  117. // CHECK:STDOUT: %A.ref.loc29: type = name_ref A, %A.decl [template = constants.%A]
  118. // CHECK:STDOUT: %.loc29_26: type = ptr_type A [template = constants.%.18]
  119. // CHECK:STDOUT: %return.var.loc29: ref A* = var <return slot>
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %ConvertInit: <function> = fn_decl @ConvertInit [template] {}
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: class @A {
  125. // CHECK:STDOUT: %.loc8: <unbound element of class A> = field_decl a, element0 [template]
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Self = constants.%A
  129. // CHECK:STDOUT: .a = %.loc8
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: class @B {
  133. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  134. // CHECK:STDOUT: %.loc12: <unbound element of class B> = base_decl A, element0 [template]
  135. // CHECK:STDOUT: %.loc13: <unbound element of class B> = field_decl b, element1 [template]
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = constants.%B
  139. // CHECK:STDOUT: .base = %.loc12
  140. // CHECK:STDOUT: .b = %.loc13
  141. // CHECK:STDOUT: extend name_scope2
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: class @C {
  145. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  146. // CHECK:STDOUT: %.loc17: <unbound element of class C> = base_decl B, element0 [template]
  147. // CHECK:STDOUT: %.loc18: <unbound element of class C> = field_decl c, element1 [template]
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !members:
  150. // CHECK:STDOUT: .Self = constants.%C
  151. // CHECK:STDOUT: .base = %.loc17
  152. // CHECK:STDOUT: .c = %.loc18
  153. // CHECK:STDOUT: extend name_scope3
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @ConvertCToB(%p: C*) -> B* {
  157. // CHECK:STDOUT: !entry:
  158. // CHECK:STDOUT: %p.ref: C* = name_ref p, %p
  159. // CHECK:STDOUT: %.loc21_39.1: ref C = deref %p.ref
  160. // CHECK:STDOUT: %.loc21_39.2: ref B = class_element_access %.loc21_39.1, element0
  161. // CHECK:STDOUT: %.loc21_39.3: B* = addr_of %.loc21_39.2
  162. // CHECK:STDOUT: %.loc21_38: B* = converted %p.ref, %.loc21_39.3
  163. // CHECK:STDOUT: return %.loc21_38
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @ConvertBToA(%p: B*) -> A* {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %p.ref: B* = name_ref p, %p
  169. // CHECK:STDOUT: %.loc22_39.1: ref B = deref %p.ref
  170. // CHECK:STDOUT: %.loc22_39.2: ref A = class_element_access %.loc22_39.1, element0
  171. // CHECK:STDOUT: %.loc22_39.3: A* = addr_of %.loc22_39.2
  172. // CHECK:STDOUT: %.loc22_38: A* = converted %p.ref, %.loc22_39.3
  173. // CHECK:STDOUT: return %.loc22_38
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @ConvertCToA(%p: C*) -> A* {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %p.ref: C* = name_ref p, %p
  179. // CHECK:STDOUT: %.loc23_39.1: ref C = deref %p.ref
  180. // CHECK:STDOUT: %.loc23_39.2: ref B = class_element_access %.loc23_39.1, element0
  181. // CHECK:STDOUT: %.loc23_39.3: ref A = class_element_access %.loc23_39.2, element0
  182. // CHECK:STDOUT: %.loc23_39.4: A* = addr_of %.loc23_39.3
  183. // CHECK:STDOUT: %.loc23_38: A* = converted %p.ref, %.loc23_39.4
  184. // CHECK:STDOUT: return %.loc23_38
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @ConvertValue(%c: C) {
  188. // CHECK:STDOUT: !entry:
  189. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  190. // CHECK:STDOUT: %c.ref: C = name_ref c, %c
  191. // CHECK:STDOUT: %.loc26_15.1: ref B = class_element_access %c.ref, element0
  192. // CHECK:STDOUT: %.loc26_15.2: ref A = class_element_access %.loc26_15.1, element0
  193. // CHECK:STDOUT: %.loc26_14.1: ref A = converted %c.ref, %.loc26_15.2
  194. // CHECK:STDOUT: %.loc26_14.2: A = bind_value %.loc26_14.1
  195. // CHECK:STDOUT: %a: A = bind_name a, %.loc26_14.2
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @ConvertRef(%c: C*) -> A* {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %c.ref: C* = name_ref c, %c
  202. // CHECK:STDOUT: %.loc30_12.1: ref C = deref %c.ref
  203. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  204. // CHECK:STDOUT: %.loc30_15.1: ref B = class_element_access %.loc30_12.1, element0
  205. // CHECK:STDOUT: %.loc30_15.2: ref A = class_element_access %.loc30_15.1, element0
  206. // CHECK:STDOUT: %.loc30_12.2: ref A = converted %.loc30_12.1, %.loc30_15.2
  207. // CHECK:STDOUT: %.loc30_10: A* = addr_of %.loc30_12.2
  208. // CHECK:STDOUT: return %.loc30_10
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: fn @ConvertInit() {
  212. // CHECK:STDOUT: !entry:
  213. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  214. // CHECK:STDOUT: %.loc34_38: i32 = int_literal 1 [template = constants.%.19]
  215. // CHECK:STDOUT: %.loc34_39.1: {.a: i32} = struct_literal (%.loc34_38)
  216. // CHECK:STDOUT: %.loc34_47: i32 = int_literal 2 [template = constants.%.20]
  217. // CHECK:STDOUT: %.loc34_48.1: {.base: {.a: i32}, .b: i32} = struct_literal (%.loc34_39.1, %.loc34_47)
  218. // CHECK:STDOUT: %.loc34_56: i32 = int_literal 3 [template = constants.%.22]
  219. // CHECK:STDOUT: %.loc34_57.1: {.base: {.base: {.a: i32}, .b: i32}, .c: i32} = struct_literal (%.loc34_48.1, %.loc34_56)
  220. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  221. // CHECK:STDOUT: %.loc34_57.2: ref C = temporary_storage
  222. // CHECK:STDOUT: %.loc34_57.3: ref B = class_element_access %.loc34_57.2, element0
  223. // CHECK:STDOUT: %.loc34_48.2: ref A = class_element_access %.loc34_57.3, element0
  224. // CHECK:STDOUT: %.loc34_39.2: ref i32 = class_element_access %.loc34_48.2, element0
  225. // CHECK:STDOUT: %.loc34_39.3: init i32 = initialize_from %.loc34_38 to %.loc34_39.2 [template = constants.%.19]
  226. // CHECK:STDOUT: %.loc34_39.4: init A = class_init (%.loc34_39.3), %.loc34_48.2 [template = constants.%.24]
  227. // CHECK:STDOUT: %.loc34_39.5: init A = converted %.loc34_39.1, %.loc34_39.4 [template = constants.%.24]
  228. // CHECK:STDOUT: %.loc34_48.3: ref i32 = class_element_access %.loc34_57.3, element1
  229. // CHECK:STDOUT: %.loc34_48.4: init i32 = initialize_from %.loc34_47 to %.loc34_48.3 [template = constants.%.20]
  230. // CHECK:STDOUT: %.loc34_48.5: init B = class_init (%.loc34_39.5, %.loc34_48.4), %.loc34_57.3 [template = constants.%.25]
  231. // CHECK:STDOUT: %.loc34_48.6: init B = converted %.loc34_48.1, %.loc34_48.5 [template = constants.%.25]
  232. // CHECK:STDOUT: %.loc34_57.4: ref i32 = class_element_access %.loc34_57.2, element1
  233. // CHECK:STDOUT: %.loc34_57.5: init i32 = initialize_from %.loc34_56 to %.loc34_57.4 [template = constants.%.22]
  234. // CHECK:STDOUT: %.loc34_57.6: init C = class_init (%.loc34_48.6, %.loc34_57.5), %.loc34_57.2 [template = constants.%.26]
  235. // CHECK:STDOUT: %.loc34_57.7: ref C = temporary %.loc34_57.2, %.loc34_57.6
  236. // CHECK:STDOUT: %.loc34_57.8: ref C = converted %.loc34_57.1, %.loc34_57.7
  237. // CHECK:STDOUT: %.loc34_63.1: ref B = class_element_access %.loc34_57.8, element0
  238. // CHECK:STDOUT: %.loc34_63.2: ref A = class_element_access %.loc34_63.1, element0
  239. // CHECK:STDOUT: %.loc34_57.9: ref A = converted %.loc34_57.8, %.loc34_63.2
  240. // CHECK:STDOUT: %.loc34_57.10: A = bind_value %.loc34_57.9
  241. // CHECK:STDOUT: %a: A = bind_name a, %.loc34_57.10
  242. // CHECK:STDOUT: return
  243. // CHECK:STDOUT: }
  244. // CHECK:STDOUT: