Files
carbon-lang/toolchain/check/testdata/struct/member_access.carbon
T
67f2c9ce26 Add a FacetValue instruction (#4545)
The new `FacetValue` instruction represents `C as I` for some type `C`
and facet type `I`. It is named `FacetValue` instead of just `Facet` to
parallel the `FacetType` instruction.

This PR uses this instruction represent the facet value `Self` in an
`impl` declaration. This instruction will be used in the future to also
support things like:

* `C as I` where `C` is a class; and
* forming a specific for a generic with a `T:! I` parameter where `T` is
being given a concrete value.

(Here `I` is an interface or other non-`type` facet type.)

Also do some renaming and add some comments to make things a bit more
clear.

* `FacetTypeAccess` -> `FacetAccessType` to clarify this is not access
of a facet type, but access of the type of a facet
* `.facet_id` -> `.facet_value_inst_id` to parallel the `FacetValue`
instruction

`FacetAccessWitness` will be in a future PR.

---------

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-11-21 22:29:53 +00:00

109 lines
6.9 KiB
Plaintext

// 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
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/struct/member_access.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/struct/member_access.carbon
var x: {.a: f64, .b: i32} = {.a = 0.0, .b = 1};
var y: i32 = x.b;
var z: i32 = y;
// CHECK:STDOUT: --- member_access.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.1: Core.IntLiteral = int_value 64 [template]
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
// CHECK:STDOUT: %.2: Core.IntLiteral = int_value 32 [template]
// CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
// CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
// CHECK:STDOUT: %i32: type = int_type signed, %.2 [template]
// CHECK:STDOUT: %.3: type = struct_type {.a: f64, .b: %i32} [template]
// CHECK:STDOUT: %.5: f64 = float_literal 0 [template]
// CHECK:STDOUT: %.6: Core.IntLiteral = int_value 1 [template]
// CHECK:STDOUT: %.7: type = struct_type {.a: f64, .b: Core.IntLiteral} [template]
// CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
// CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.2) [template]
// CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
// CHECK:STDOUT: %.31: <witness> = interface_witness (%Convert.14) [template]
// CHECK:STDOUT: %.32: <bound method> = bound_method %.6, %Convert.14 [template]
// CHECK:STDOUT: %.33: <specific function> = specific_function %.32, @Convert.2(%.2) [template]
// CHECK:STDOUT: %.34: %i32 = int_value 1 [template]
// CHECK:STDOUT: %struct: %.3 = struct_value (%.5, %.34) [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
// CHECK:STDOUT: .Float = %import_ref.1
// CHECK:STDOUT: .Int = %import_ref.2
// CHECK:STDOUT: .ImplicitAs = %import_ref.3
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .x = %x
// CHECK:STDOUT: .y = %y
// CHECK:STDOUT: .z = %z
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %.loc11_13.1: Core.IntLiteral = int_value 64 [template = constants.%.1]
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc11_13.1) [template = f64]
// CHECK:STDOUT: %.loc11_13.2: type = value_of_initializer %float.make_type [template = f64]
// CHECK:STDOUT: %.loc11_13.3: type = converted %float.make_type, %.loc11_13.2 [template = f64]
// CHECK:STDOUT: %.loc11_22.1: Core.IntLiteral = int_value 32 [template = constants.%.2]
// CHECK:STDOUT: %int.make_type_signed.loc11: init type = call constants.%Int(%.loc11_22.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc11_22.2: type = value_of_initializer %int.make_type_signed.loc11 [template = constants.%i32]
// CHECK:STDOUT: %.loc11_22.3: type = converted %int.make_type_signed.loc11, %.loc11_22.2 [template = constants.%i32]
// CHECK:STDOUT: %.loc11_25: type = struct_type {.a: f64, .b: %i32} [template = constants.%.3]
// CHECK:STDOUT: %x.var: ref %.3 = var x
// CHECK:STDOUT: %x: ref %.3 = bind_name x, %x.var
// CHECK:STDOUT: %.loc12_8.1: Core.IntLiteral = int_value 32 [template = constants.%.2]
// CHECK:STDOUT: %int.make_type_signed.loc12: init type = call constants.%Int(%.loc12_8.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc12_8.2: type = value_of_initializer %int.make_type_signed.loc12 [template = constants.%i32]
// CHECK:STDOUT: %.loc12_8.3: type = converted %int.make_type_signed.loc12, %.loc12_8.2 [template = constants.%i32]
// CHECK:STDOUT: %y.var: ref %i32 = var y
// CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
// CHECK:STDOUT: %.loc13_8.1: Core.IntLiteral = int_value 32 [template = constants.%.2]
// CHECK:STDOUT: %int.make_type_signed.loc13: init type = call constants.%Int(%.loc13_8.1) [template = constants.%i32]
// CHECK:STDOUT: %.loc13_8.2: type = value_of_initializer %int.make_type_signed.loc13 [template = constants.%i32]
// CHECK:STDOUT: %.loc13_8.3: type = converted %int.make_type_signed.loc13, %.loc13_8.2 [template = constants.%i32]
// CHECK:STDOUT: %z.var: ref %i32 = var z
// CHECK:STDOUT: %z: ref %i32 = bind_name z, %z.var
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc11_35: f64 = float_literal 0 [template = constants.%.5]
// CHECK:STDOUT: %.loc11_45: Core.IntLiteral = int_value 1 [template = constants.%.6]
// CHECK:STDOUT: %.loc11_46.1: %.7 = struct_literal (%.loc11_35, %.loc11_45)
// CHECK:STDOUT: %.loc11_46.2: ref f64 = struct_access file.%x.var, element0
// CHECK:STDOUT: %.loc11_46.3: init f64 = initialize_from %.loc11_35 to %.loc11_46.2 [template = constants.%.5]
// CHECK:STDOUT: %.loc11_46.4: %Convert.type.2 = interface_witness_access constants.%.31, element0 [template = constants.%Convert.14]
// CHECK:STDOUT: %.loc11_46.5: <bound method> = bound_method %.loc11_45, %.loc11_46.4 [template = constants.%.32]
// CHECK:STDOUT: %.loc11_46.6: <specific function> = specific_function %.loc11_46.5, @Convert.2(constants.%.2) [template = constants.%.33]
// CHECK:STDOUT: %int.convert_checked: init %i32 = call %.loc11_46.6(%.loc11_45) [template = constants.%.34]
// CHECK:STDOUT: %.loc11_46.7: init %i32 = converted %.loc11_45, %int.convert_checked [template = constants.%.34]
// CHECK:STDOUT: %.loc11_46.8: ref %i32 = struct_access file.%x.var, element1
// CHECK:STDOUT: %.loc11_46.9: init %i32 = initialize_from %.loc11_46.7 to %.loc11_46.8 [template = constants.%.34]
// CHECK:STDOUT: %.loc11_46.10: init %.3 = struct_init (%.loc11_46.3, %.loc11_46.9) to file.%x.var [template = constants.%struct]
// CHECK:STDOUT: %.loc11_47: init %.3 = converted %.loc11_46.1, %.loc11_46.10 [template = constants.%struct]
// CHECK:STDOUT: assign file.%x.var, %.loc11_47
// CHECK:STDOUT: %x.ref: ref %.3 = name_ref x, file.%x
// CHECK:STDOUT: %.loc12_15.1: ref %i32 = struct_access %x.ref, element1
// CHECK:STDOUT: %.loc12_15.2: %i32 = bind_value %.loc12_15.1
// CHECK:STDOUT: assign file.%y.var, %.loc12_15.2
// CHECK:STDOUT: %y.ref: ref %i32 = name_ref y, file.%y
// CHECK:STDOUT: %.loc13: %i32 = bind_value %y.ref
// CHECK:STDOUT: assign file.%z.var, %.loc13
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: