// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP:   bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/alias/import_order.carbon
// TIP: To dump output, run:
// TIP:   bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/alias/import_order.carbon

// --- a.carbon

library "[[@TEST_NAME]]";

class C { var v: (); }
alias a = C;
alias b = a;
alias c = b;
alias d = c;

// --- b.carbon

library "[[@TEST_NAME]]";

import library "a";

// Access imports in reverse order of export.
var d_val: d = {.v = ()};
var c_val: c = {.v = d_val.v};
var b_val: b = {.v = c_val.v};
var a_val: a = {.v = b_val.v};

// CHECK:STDOUT: --- a.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT:   %C: type = class_type @C [concrete]
// CHECK:STDOUT:   %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT:   %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
// CHECK:STDOUT:   %C.elem: type = unbound_element_type %C, %empty_tuple.type [concrete]
// CHECK:STDOUT:   %struct_type.v: type = struct_type {.v: %empty_tuple.type} [concrete]
// CHECK:STDOUT:   %complete_type: <witness> = complete_type_witness %struct_type.v [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT:   package: <namespace> = namespace [concrete] {
// CHECK:STDOUT:     .C = %C.decl
// CHECK:STDOUT:     .a = %a
// CHECK:STDOUT:     .b = %b
// CHECK:STDOUT:     .c = %c
// CHECK:STDOUT:     .d = %d
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
// CHECK:STDOUT:   %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
// CHECK:STDOUT:   %a: type = alias_binding a, %C.decl [concrete = constants.%C]
// CHECK:STDOUT:   %a.ref: type = name_ref a, %a [concrete = constants.%C]
// CHECK:STDOUT:   %b: type = alias_binding b, %a [concrete = constants.%C]
// CHECK:STDOUT:   %b.ref: type = name_ref b, %b [concrete = constants.%C]
// CHECK:STDOUT:   %c: type = alias_binding c, %b [concrete = constants.%C]
// CHECK:STDOUT:   %c.ref: type = name_ref c, %c [concrete = constants.%C]
// CHECK:STDOUT:   %d: type = alias_binding d, %c [concrete = constants.%C]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT:   %.loc4_19.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc4_19.2: type = converted %.loc4_19.1, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
// CHECK:STDOUT:   %.loc4_16: %C.elem = field_decl v, element0 [concrete]
// CHECK:STDOUT:   %complete_type: <witness> = complete_type_witness constants.%struct_type.v [concrete = constants.%complete_type]
// CHECK:STDOUT:   complete_type_witness = %complete_type
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT:   .Self = constants.%C
// CHECK:STDOUT:   .v = %.loc4_16
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- b.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT:   %C: type = class_type @C [concrete]
// CHECK:STDOUT:   %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT:   %struct_type.v: type = struct_type {.v: %empty_tuple.type} [concrete]
// CHECK:STDOUT:   %complete_type: <witness> = complete_type_witness %struct_type.v [concrete]
// CHECK:STDOUT:   %pattern_type: type = pattern_type %C [concrete]
// CHECK:STDOUT:   %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
// CHECK:STDOUT:   %struct.5f2: %struct_type.v = struct_value (%empty_tuple) [concrete]
// CHECK:STDOUT:   %.cad: ref %empty_tuple.type = class_element_access file.%d_val.var, element0 [concrete]
// CHECK:STDOUT:   %C.val: %C = struct_value (%empty_tuple) [concrete]
// CHECK:STDOUT:   %C.elem: type = unbound_element_type %C, %empty_tuple.type [concrete]
// CHECK:STDOUT:   %struct.d07: %struct_type.v = struct_value (%.cad) [concrete]
// CHECK:STDOUT:   %.da4: ref %empty_tuple.type = class_element_access file.%c_val.var, element0 [concrete]
// CHECK:STDOUT:   %struct.f0b: %struct_type.v = struct_value (%.da4) [concrete]
// CHECK:STDOUT:   %.0f2: ref %empty_tuple.type = class_element_access file.%b_val.var, element0 [concrete]
// CHECK:STDOUT:   %struct.938: %struct_type.v = struct_value (%.0f2) [concrete]
// CHECK:STDOUT:   %.851: ref %empty_tuple.type = class_element_access file.%a_val.var, element0 [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT:   %Main.C = import_ref Main//a, C, unloaded
// CHECK:STDOUT:   %Main.a: type = import_ref Main//a, a, loaded [concrete = constants.%C]
// CHECK:STDOUT:   %Main.b: type = import_ref Main//a, b, loaded [concrete = constants.%C]
// CHECK:STDOUT:   %Main.c: type = import_ref Main//a, c, loaded [concrete = constants.%C]
// CHECK:STDOUT:   %Main.d: type = import_ref Main//a, d, loaded [concrete = constants.%C]
// CHECK:STDOUT:   %Main.import_ref.146: <witness> = import_ref Main//a, loc4_22, loaded [concrete = constants.%complete_type]
// CHECK:STDOUT:   %Main.import_ref.743 = import_ref Main//a, inst{{[0-9A-F]+}} [no loc], unloaded
// CHECK:STDOUT:   %Main.import_ref.d43: %C.elem = import_ref Main//a, loc4_16, loaded [concrete = %.c09]
// CHECK:STDOUT:   %.c09: %C.elem = field_decl v, element0 [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT:   package: <namespace> = namespace [concrete] {
// CHECK:STDOUT:     .C = imports.%Main.C
// CHECK:STDOUT:     .a = imports.%Main.a
// CHECK:STDOUT:     .b = imports.%Main.b
// CHECK:STDOUT:     .c = imports.%Main.c
// CHECK:STDOUT:     .d = imports.%Main.d
// CHECK:STDOUT:     .d_val = %d_val
// CHECK:STDOUT:     .c_val = %c_val
// CHECK:STDOUT:     .b_val = %b_val
// CHECK:STDOUT:     .a_val = %a_val
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %default.import = import <none>
// CHECK:STDOUT:   name_binding_decl {
// CHECK:STDOUT:     %d_val.patt: %pattern_type = ref_binding_pattern d_val [concrete]
// CHECK:STDOUT:     %d_val.var_patt: %pattern_type = var_pattern %d_val.patt [concrete]
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %d_val.var: ref %C = var %d_val.var_patt [concrete]
// CHECK:STDOUT:   %d.ref: type = name_ref d, imports.%Main.d [concrete = constants.%C]
// CHECK:STDOUT:   %d_val: ref %C = ref_binding d_val, %d_val.var [concrete = %d_val.var]
// CHECK:STDOUT:   name_binding_decl {
// CHECK:STDOUT:     %c_val.patt: %pattern_type = ref_binding_pattern c_val [concrete]
// CHECK:STDOUT:     %c_val.var_patt: %pattern_type = var_pattern %c_val.patt [concrete]
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %c_val.var: ref %C = var %c_val.var_patt [concrete]
// CHECK:STDOUT:   %c.ref: type = name_ref c, imports.%Main.c [concrete = constants.%C]
// CHECK:STDOUT:   %c_val: ref %C = ref_binding c_val, %c_val.var [concrete = %c_val.var]
// CHECK:STDOUT:   name_binding_decl {
// CHECK:STDOUT:     %b_val.patt: %pattern_type = ref_binding_pattern b_val [concrete]
// CHECK:STDOUT:     %b_val.var_patt: %pattern_type = var_pattern %b_val.patt [concrete]
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %b_val.var: ref %C = var %b_val.var_patt [concrete]
// CHECK:STDOUT:   %b.ref: type = name_ref b, imports.%Main.b [concrete = constants.%C]
// CHECK:STDOUT:   %b_val: ref %C = ref_binding b_val, %b_val.var [concrete = %b_val.var]
// CHECK:STDOUT:   name_binding_decl {
// CHECK:STDOUT:     %a_val.patt: %pattern_type = ref_binding_pattern a_val [concrete]
// CHECK:STDOUT:     %a_val.var_patt: %pattern_type = var_pattern %a_val.patt [concrete]
// CHECK:STDOUT:   }
// CHECK:STDOUT:   %a_val.var: ref %C = var %a_val.var_patt [concrete]
// CHECK:STDOUT:   %a.ref: type = name_ref a, imports.%Main.a [concrete = constants.%C]
// CHECK:STDOUT:   %a_val: ref %C = ref_binding a_val, %a_val.var [concrete = %a_val.var]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C [from "a.carbon"] {
// CHECK:STDOUT:   complete_type_witness = imports.%Main.import_ref.146
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT:   .Self = imports.%Main.import_ref.743
// CHECK:STDOUT:   .v = imports.%Main.import_ref.d43
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT:   %.loc7_23.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc7_24.1: %struct_type.v = struct_literal (%.loc7_23.1) [concrete = constants.%struct.5f2]
// CHECK:STDOUT:   %.loc7_24.2: ref %empty_tuple.type = class_element_access file.%d_val.var, element0 [concrete = constants.%.cad]
// CHECK:STDOUT:   %.loc7_23.2: init %empty_tuple.type to %.loc7_24.2 = tuple_init () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc7_24.3: init %empty_tuple.type = converted %.loc7_23.1, %.loc7_23.2 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc7_24.4: init %empty_tuple.type to %.loc7_24.2 = in_place_init %.loc7_24.3 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc7_24.5: init %C to file.%d_val.var = class_init (%.loc7_24.4) [concrete = constants.%C.val]
// CHECK:STDOUT:   %.loc7_1: init %C = converted %.loc7_24.1, %.loc7_24.5 [concrete = constants.%C.val]
// CHECK:STDOUT:   assign file.%d_val.var, %.loc7_1
// CHECK:STDOUT:   %d_val.ref: ref %C = name_ref d_val, file.%d_val [concrete = file.%d_val.var]
// CHECK:STDOUT:   %v.ref.loc8: %C.elem = name_ref v, imports.%Main.import_ref.d43 [concrete = imports.%.c09]
// CHECK:STDOUT:   %.loc8_27.1: ref %empty_tuple.type = class_element_access %d_val.ref, element0 [concrete = constants.%.cad]
// CHECK:STDOUT:   %.loc8_29.1: %struct_type.v = struct_literal (%.loc8_27.1) [concrete = constants.%struct.d07]
// CHECK:STDOUT:   %.loc8_29.2: ref %empty_tuple.type = class_element_access file.%c_val.var, element0 [concrete = constants.%.da4]
// CHECK:STDOUT:   %.loc8_27.2: init %empty_tuple.type to %.loc8_29.2 = tuple_init () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc8_29.3: init %empty_tuple.type = converted %.loc8_27.1, %.loc8_27.2 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc8_29.4: init %empty_tuple.type to %.loc8_29.2 = in_place_init %.loc8_29.3 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc8_29.5: init %C to file.%c_val.var = class_init (%.loc8_29.4) [concrete = constants.%C.val]
// CHECK:STDOUT:   %.loc8_1: init %C = converted %.loc8_29.1, %.loc8_29.5 [concrete = constants.%C.val]
// CHECK:STDOUT:   assign file.%c_val.var, %.loc8_1
// CHECK:STDOUT:   %c_val.ref: ref %C = name_ref c_val, file.%c_val [concrete = file.%c_val.var]
// CHECK:STDOUT:   %v.ref.loc9: %C.elem = name_ref v, imports.%Main.import_ref.d43 [concrete = imports.%.c09]
// CHECK:STDOUT:   %.loc9_27.1: ref %empty_tuple.type = class_element_access %c_val.ref, element0 [concrete = constants.%.da4]
// CHECK:STDOUT:   %.loc9_29.1: %struct_type.v = struct_literal (%.loc9_27.1) [concrete = constants.%struct.f0b]
// CHECK:STDOUT:   %.loc9_29.2: ref %empty_tuple.type = class_element_access file.%b_val.var, element0 [concrete = constants.%.0f2]
// CHECK:STDOUT:   %.loc9_27.2: init %empty_tuple.type to %.loc9_29.2 = tuple_init () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc9_29.3: init %empty_tuple.type = converted %.loc9_27.1, %.loc9_27.2 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc9_29.4: init %empty_tuple.type to %.loc9_29.2 = in_place_init %.loc9_29.3 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc9_29.5: init %C to file.%b_val.var = class_init (%.loc9_29.4) [concrete = constants.%C.val]
// CHECK:STDOUT:   %.loc9_1: init %C = converted %.loc9_29.1, %.loc9_29.5 [concrete = constants.%C.val]
// CHECK:STDOUT:   assign file.%b_val.var, %.loc9_1
// CHECK:STDOUT:   %b_val.ref: ref %C = name_ref b_val, file.%b_val [concrete = file.%b_val.var]
// CHECK:STDOUT:   %v.ref.loc10: %C.elem = name_ref v, imports.%Main.import_ref.d43 [concrete = imports.%.c09]
// CHECK:STDOUT:   %.loc10_27.1: ref %empty_tuple.type = class_element_access %b_val.ref, element0 [concrete = constants.%.0f2]
// CHECK:STDOUT:   %.loc10_29.1: %struct_type.v = struct_literal (%.loc10_27.1) [concrete = constants.%struct.938]
// CHECK:STDOUT:   %.loc10_29.2: ref %empty_tuple.type = class_element_access file.%a_val.var, element0 [concrete = constants.%.851]
// CHECK:STDOUT:   %.loc10_27.2: init %empty_tuple.type to %.loc10_29.2 = tuple_init () [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc10_29.3: init %empty_tuple.type = converted %.loc10_27.1, %.loc10_27.2 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc10_29.4: init %empty_tuple.type to %.loc10_29.2 = in_place_init %.loc10_29.3 [concrete = constants.%empty_tuple]
// CHECK:STDOUT:   %.loc10_29.5: init %C to file.%a_val.var = class_init (%.loc10_29.4) [concrete = constants.%C.val]
// CHECK:STDOUT:   %.loc10_1: init %C = converted %.loc10_29.1, %.loc10_29.5 [concrete = constants.%C.val]
// CHECK:STDOUT:   assign file.%a_val.var, %.loc10_1
// CHECK:STDOUT:   return
// CHECK:STDOUT: }
// CHECK:STDOUT:
