reorder_qualified.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/reorder_qualified.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/reorder_qualified.carbon
  10. class A {
  11. class B {
  12. class C;
  13. fn BF();
  14. var b: i32;
  15. }
  16. class B.C {
  17. class D {
  18. fn F();
  19. fn DF();
  20. var d: i32;
  21. }
  22. fn D.DF() {
  23. // A, B, C, and D are complete here.
  24. var a: A = {.a = 1};
  25. var b: B = {.b = 2};
  26. var c: C = {.c = 3};
  27. var d: D = {.d = 4};
  28. // Unqualified lookup looks in all of them.
  29. AF();
  30. BF();
  31. CF();
  32. DF();
  33. }
  34. fn CF();
  35. var c: i32;
  36. }
  37. fn AF();
  38. var a: i32;
  39. }
  40. // CHECK:STDOUT: --- reorder_qualified.carbon
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: constants {
  43. // CHECK:STDOUT: %A: type = class_type @A [template]
  44. // CHECK:STDOUT: %B: type = class_type @B [template]
  45. // CHECK:STDOUT: %C: type = class_type @C [template]
  46. // CHECK:STDOUT: %BF.type: type = fn_type @BF [template]
  47. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  48. // CHECK:STDOUT: %BF: %BF.type = struct_value () [template]
  49. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  50. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  51. // CHECK:STDOUT: %.1: type = unbound_element_type %B, i32 [template]
  52. // CHECK:STDOUT: %.2: type = struct_type {.b: i32} [template]
  53. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  54. // CHECK:STDOUT: %D: type = class_type @D [template]
  55. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  56. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  57. // CHECK:STDOUT: %DF.type: type = fn_type @DF [template]
  58. // CHECK:STDOUT: %DF: %DF.type = struct_value () [template]
  59. // CHECK:STDOUT: %.4: type = unbound_element_type %D, i32 [template]
  60. // CHECK:STDOUT: %.5: type = struct_type {.d: i32} [template]
  61. // CHECK:STDOUT: %.6: <witness> = complete_type_witness %.5 [template]
  62. // CHECK:STDOUT: %CF.type: type = fn_type @CF [template]
  63. // CHECK:STDOUT: %CF: %CF.type = struct_value () [template]
  64. // CHECK:STDOUT: %.7: type = unbound_element_type %C, i32 [template]
  65. // CHECK:STDOUT: %.8: type = struct_type {.c: i32} [template]
  66. // CHECK:STDOUT: %.9: <witness> = complete_type_witness %.8 [template]
  67. // CHECK:STDOUT: %AF.type: type = fn_type @AF [template]
  68. // CHECK:STDOUT: %AF: %AF.type = struct_value () [template]
  69. // CHECK:STDOUT: %.10: type = unbound_element_type %A, i32 [template]
  70. // CHECK:STDOUT: %.11: type = struct_type {.a: i32} [template]
  71. // CHECK:STDOUT: %.12: <witness> = complete_type_witness %.11 [template]
  72. // CHECK:STDOUT: %.14: Core.IntLiteral = int_value 1 [template]
  73. // CHECK:STDOUT: %.15: type = struct_type {.a: Core.IntLiteral} [template]
  74. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(i32) [template]
  75. // CHECK:STDOUT: %Convert.type.15: type = fn_type @Convert.11 [template]
  76. // CHECK:STDOUT: %Convert.15: %Convert.type.15 = struct_value () [template]
  77. // CHECK:STDOUT: %.39: <witness> = interface_witness (%Convert.15) [template]
  78. // CHECK:STDOUT: %.40: <bound method> = bound_method %.14, %Convert.15 [template]
  79. // CHECK:STDOUT: %.41: i32 = int_value 1 [template]
  80. // CHECK:STDOUT: %struct.1: %A = struct_value (%.41) [template]
  81. // CHECK:STDOUT: %.43: Core.IntLiteral = int_value 2 [template]
  82. // CHECK:STDOUT: %.44: type = struct_type {.b: Core.IntLiteral} [template]
  83. // CHECK:STDOUT: %.45: <bound method> = bound_method %.43, %Convert.15 [template]
  84. // CHECK:STDOUT: %.46: i32 = int_value 2 [template]
  85. // CHECK:STDOUT: %struct.2: %B = struct_value (%.46) [template]
  86. // CHECK:STDOUT: %.48: Core.IntLiteral = int_value 3 [template]
  87. // CHECK:STDOUT: %.49: type = struct_type {.c: Core.IntLiteral} [template]
  88. // CHECK:STDOUT: %.50: <bound method> = bound_method %.48, %Convert.15 [template]
  89. // CHECK:STDOUT: %.51: i32 = int_value 3 [template]
  90. // CHECK:STDOUT: %struct.3: %C = struct_value (%.51) [template]
  91. // CHECK:STDOUT: %.53: Core.IntLiteral = int_value 4 [template]
  92. // CHECK:STDOUT: %.54: type = struct_type {.d: Core.IntLiteral} [template]
  93. // CHECK:STDOUT: %.55: <bound method> = bound_method %.53, %Convert.15 [template]
  94. // CHECK:STDOUT: %.56: i32 = int_value 4 [template]
  95. // CHECK:STDOUT: %struct.4: %D = struct_value (%.56) [template]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: imports {
  99. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  100. // CHECK:STDOUT: .Int32 = %import_ref.1
  101. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  102. // CHECK:STDOUT: import Core//prelude
  103. // CHECK:STDOUT: import Core//prelude/...
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: file {
  108. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  109. // CHECK:STDOUT: .Core = imports.%Core
  110. // CHECK:STDOUT: .A = %A.decl
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %Core.import = import Core
  113. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: class @A {
  117. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {} {}
  118. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  119. // CHECK:STDOUT: %AF.decl: %AF.type = fn_decl @AF [template = constants.%AF] {} {}
  120. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  121. // CHECK:STDOUT: %.loc46_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  122. // CHECK:STDOUT: %.loc46_10.2: type = converted %int.make_type_32, %.loc46_10.1 [template = i32]
  123. // CHECK:STDOUT: %.loc46_8: %.10 = field_decl a, element0 [template]
  124. // CHECK:STDOUT: %.loc47: <witness> = complete_type_witness %.11 [template = constants.%.12]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = constants.%A
  128. // CHECK:STDOUT: .B = %B.decl
  129. // CHECK:STDOUT: .AF = %AF.decl
  130. // CHECK:STDOUT: .a = %.loc46_8
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: class @B {
  134. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  135. // CHECK:STDOUT: %BF.decl: %BF.type = fn_decl @BF [template = constants.%BF] {} {}
  136. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  137. // CHECK:STDOUT: %.loc16_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  138. // CHECK:STDOUT: %.loc16_12.2: type = converted %int.make_type_32, %.loc16_12.1 [template = i32]
  139. // CHECK:STDOUT: %.loc16_10: %.1 = field_decl b, element0 [template]
  140. // CHECK:STDOUT: %.loc17: <witness> = complete_type_witness %.2 [template = constants.%.3]
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: !members:
  143. // CHECK:STDOUT: .Self = constants.%B
  144. // CHECK:STDOUT: .C = %C.decl
  145. // CHECK:STDOUT: .BF = %BF.decl
  146. // CHECK:STDOUT: .b = %.loc16_10
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: class @C {
  150. // CHECK:STDOUT: %D.decl: type = class_decl @D [template = constants.%D] {} {}
  151. // CHECK:STDOUT: %DF.decl: %DF.type = fn_decl @DF [template = constants.%DF] {} {}
  152. // CHECK:STDOUT: %CF.decl: %CF.type = fn_decl @CF [template = constants.%CF] {} {}
  153. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  154. // CHECK:STDOUT: %.loc42_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  155. // CHECK:STDOUT: %.loc42_12.2: type = converted %int.make_type_32, %.loc42_12.1 [template = i32]
  156. // CHECK:STDOUT: %.loc42_10: %.7 = field_decl c, element0 [template]
  157. // CHECK:STDOUT: %.loc43: <witness> = complete_type_witness %.8 [template = constants.%.9]
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: !members:
  160. // CHECK:STDOUT: .Self = constants.%C
  161. // CHECK:STDOUT: .D = %D.decl
  162. // CHECK:STDOUT: .CF = %CF.decl
  163. // CHECK:STDOUT: .c = %.loc42_10
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: class @D {
  167. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  168. // CHECK:STDOUT: %DF.decl: %DF.type = fn_decl @DF [template = constants.%DF] {} {}
  169. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  170. // CHECK:STDOUT: %.loc24_14.1: type = value_of_initializer %int.make_type_32 [template = i32]
  171. // CHECK:STDOUT: %.loc24_14.2: type = converted %int.make_type_32, %.loc24_14.1 [template = i32]
  172. // CHECK:STDOUT: %.loc24_12: %.4 = field_decl d, element0 [template]
  173. // CHECK:STDOUT: %.loc25: <witness> = complete_type_witness %.5 [template = constants.%.6]
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: !members:
  176. // CHECK:STDOUT: .Self = constants.%D
  177. // CHECK:STDOUT: .F = %F.decl
  178. // CHECK:STDOUT: .DF = %DF.decl
  179. // CHECK:STDOUT: .d = %.loc24_12
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn @BF();
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: fn @F();
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn @DF() {
  187. // CHECK:STDOUT: !entry:
  188. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  189. // CHECK:STDOUT: %a.var: ref %A = var a
  190. // CHECK:STDOUT: %a: ref %A = bind_name a, %a.var
  191. // CHECK:STDOUT: %.loc29_24: Core.IntLiteral = int_value 1 [template = constants.%.14]
  192. // CHECK:STDOUT: %.loc29_25.1: %.15 = struct_literal (%.loc29_24)
  193. // CHECK:STDOUT: %.loc29_25.2: %Convert.type.2 = interface_witness_access constants.%.39, element0 [template = constants.%Convert.15]
  194. // CHECK:STDOUT: %.loc29_25.3: <bound method> = bound_method %.loc29_24, %.loc29_25.2 [template = constants.%.40]
  195. // CHECK:STDOUT: %int.convert_checked.loc29: init i32 = call %.loc29_25.3(%.loc29_24) [template = constants.%.41]
  196. // CHECK:STDOUT: %.loc29_25.4: init i32 = converted %.loc29_24, %int.convert_checked.loc29 [template = constants.%.41]
  197. // CHECK:STDOUT: %.loc29_25.5: ref i32 = class_element_access %a.var, element0
  198. // CHECK:STDOUT: %.loc29_25.6: init i32 = initialize_from %.loc29_25.4 to %.loc29_25.5 [template = constants.%.41]
  199. // CHECK:STDOUT: %.loc29_25.7: init %A = class_init (%.loc29_25.6), %a.var [template = constants.%struct.1]
  200. // CHECK:STDOUT: %.loc29_26: init %A = converted %.loc29_25.1, %.loc29_25.7 [template = constants.%struct.1]
  201. // CHECK:STDOUT: assign %a.var, %.loc29_26
  202. // CHECK:STDOUT: %B.ref: type = name_ref B, @A.%B.decl [template = constants.%B]
  203. // CHECK:STDOUT: %b.var: ref %B = var b
  204. // CHECK:STDOUT: %b: ref %B = bind_name b, %b.var
  205. // CHECK:STDOUT: %.loc30_24: Core.IntLiteral = int_value 2 [template = constants.%.43]
  206. // CHECK:STDOUT: %.loc30_25.1: %.44 = struct_literal (%.loc30_24)
  207. // CHECK:STDOUT: %.loc30_25.2: %Convert.type.2 = interface_witness_access constants.%.39, element0 [template = constants.%Convert.15]
  208. // CHECK:STDOUT: %.loc30_25.3: <bound method> = bound_method %.loc30_24, %.loc30_25.2 [template = constants.%.45]
  209. // CHECK:STDOUT: %int.convert_checked.loc30: init i32 = call %.loc30_25.3(%.loc30_24) [template = constants.%.46]
  210. // CHECK:STDOUT: %.loc30_25.4: init i32 = converted %.loc30_24, %int.convert_checked.loc30 [template = constants.%.46]
  211. // CHECK:STDOUT: %.loc30_25.5: ref i32 = class_element_access %b.var, element0
  212. // CHECK:STDOUT: %.loc30_25.6: init i32 = initialize_from %.loc30_25.4 to %.loc30_25.5 [template = constants.%.46]
  213. // CHECK:STDOUT: %.loc30_25.7: init %B = class_init (%.loc30_25.6), %b.var [template = constants.%struct.2]
  214. // CHECK:STDOUT: %.loc30_26: init %B = converted %.loc30_25.1, %.loc30_25.7 [template = constants.%struct.2]
  215. // CHECK:STDOUT: assign %b.var, %.loc30_26
  216. // CHECK:STDOUT: %C.ref: type = name_ref C, @B.%C.decl [template = constants.%C]
  217. // CHECK:STDOUT: %c.var: ref %C = var c
  218. // CHECK:STDOUT: %c: ref %C = bind_name c, %c.var
  219. // CHECK:STDOUT: %.loc31_24: Core.IntLiteral = int_value 3 [template = constants.%.48]
  220. // CHECK:STDOUT: %.loc31_25.1: %.49 = struct_literal (%.loc31_24)
  221. // CHECK:STDOUT: %.loc31_25.2: %Convert.type.2 = interface_witness_access constants.%.39, element0 [template = constants.%Convert.15]
  222. // CHECK:STDOUT: %.loc31_25.3: <bound method> = bound_method %.loc31_24, %.loc31_25.2 [template = constants.%.50]
  223. // CHECK:STDOUT: %int.convert_checked.loc31: init i32 = call %.loc31_25.3(%.loc31_24) [template = constants.%.51]
  224. // CHECK:STDOUT: %.loc31_25.4: init i32 = converted %.loc31_24, %int.convert_checked.loc31 [template = constants.%.51]
  225. // CHECK:STDOUT: %.loc31_25.5: ref i32 = class_element_access %c.var, element0
  226. // CHECK:STDOUT: %.loc31_25.6: init i32 = initialize_from %.loc31_25.4 to %.loc31_25.5 [template = constants.%.51]
  227. // CHECK:STDOUT: %.loc31_25.7: init %C = class_init (%.loc31_25.6), %c.var [template = constants.%struct.3]
  228. // CHECK:STDOUT: %.loc31_26: init %C = converted %.loc31_25.1, %.loc31_25.7 [template = constants.%struct.3]
  229. // CHECK:STDOUT: assign %c.var, %.loc31_26
  230. // CHECK:STDOUT: %D.ref: type = name_ref D, @C.%D.decl [template = constants.%D]
  231. // CHECK:STDOUT: %d.var: ref %D = var d
  232. // CHECK:STDOUT: %d: ref %D = bind_name d, %d.var
  233. // CHECK:STDOUT: %.loc32_24: Core.IntLiteral = int_value 4 [template = constants.%.53]
  234. // CHECK:STDOUT: %.loc32_25.1: %.54 = struct_literal (%.loc32_24)
  235. // CHECK:STDOUT: %.loc32_25.2: %Convert.type.2 = interface_witness_access constants.%.39, element0 [template = constants.%Convert.15]
  236. // CHECK:STDOUT: %.loc32_25.3: <bound method> = bound_method %.loc32_24, %.loc32_25.2 [template = constants.%.55]
  237. // CHECK:STDOUT: %int.convert_checked.loc32: init i32 = call %.loc32_25.3(%.loc32_24) [template = constants.%.56]
  238. // CHECK:STDOUT: %.loc32_25.4: init i32 = converted %.loc32_24, %int.convert_checked.loc32 [template = constants.%.56]
  239. // CHECK:STDOUT: %.loc32_25.5: ref i32 = class_element_access %d.var, element0
  240. // CHECK:STDOUT: %.loc32_25.6: init i32 = initialize_from %.loc32_25.4 to %.loc32_25.5 [template = constants.%.56]
  241. // CHECK:STDOUT: %.loc32_25.7: init %D = class_init (%.loc32_25.6), %d.var [template = constants.%struct.4]
  242. // CHECK:STDOUT: %.loc32_26: init %D = converted %.loc32_25.1, %.loc32_25.7 [template = constants.%struct.4]
  243. // CHECK:STDOUT: assign %d.var, %.loc32_26
  244. // CHECK:STDOUT: %AF.ref: %AF.type = name_ref AF, @A.%AF.decl [template = constants.%AF]
  245. // CHECK:STDOUT: %AF.call: init %empty_tuple.type = call %AF.ref()
  246. // CHECK:STDOUT: %BF.ref: %BF.type = name_ref BF, @B.%BF.decl [template = constants.%BF]
  247. // CHECK:STDOUT: %BF.call: init %empty_tuple.type = call %BF.ref()
  248. // CHECK:STDOUT: %CF.ref: %CF.type = name_ref CF, @C.%CF.decl [template = constants.%CF]
  249. // CHECK:STDOUT: %CF.call: init %empty_tuple.type = call %CF.ref()
  250. // CHECK:STDOUT: %DF.ref: %DF.type = name_ref DF, @D.%DF.decl [template = constants.%DF]
  251. // CHECK:STDOUT: %DF.call: init %empty_tuple.type = call %DF.ref()
  252. // CHECK:STDOUT: return
  253. // CHECK:STDOUT: }
  254. // CHECK:STDOUT:
  255. // CHECK:STDOUT: fn @CF();
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: fn @AF();
  258. // CHECK:STDOUT: