member_access.carbon 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/struct/member_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/struct/member_access.carbon
  10. var x: {.a: f64, .b: i32} = {.a = 0.0, .b = 1};
  11. var y: i32 = x.b;
  12. var z: i32 = y;
  13. // CHECK:STDOUT: --- member_access.carbon
  14. // CHECK:STDOUT:
  15. // CHECK:STDOUT: constants {
  16. // CHECK:STDOUT: %.1: i32 = int_literal 64 [template]
  17. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  18. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  19. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  20. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  21. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  22. // CHECK:STDOUT: %.3: type = struct_type {.a: f64, .b: i32} [template]
  23. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  24. // CHECK:STDOUT: %.5: f64 = float_literal 0 [template]
  25. // CHECK:STDOUT: %.6: i32 = int_literal 1 [template]
  26. // CHECK:STDOUT: %struct: %.3 = struct_value (%.5, %.6) [template]
  27. // CHECK:STDOUT: }
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: imports {
  30. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  31. // CHECK:STDOUT: .Float = %import_ref.1
  32. // CHECK:STDOUT: .Int32 = %import_ref.2
  33. // CHECK:STDOUT: import Core//prelude
  34. // CHECK:STDOUT: import Core//prelude/operators
  35. // CHECK:STDOUT: import Core//prelude/types
  36. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  37. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  38. // CHECK:STDOUT: import Core//prelude/operators/comparison
  39. // CHECK:STDOUT: import Core//prelude/types/bool
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT: %import_ref.1: %Float.type = import_ref Core//prelude/types, inst+32, loaded [template = constants.%Float]
  42. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: file {
  46. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  47. // CHECK:STDOUT: .Core = imports.%Core
  48. // CHECK:STDOUT: .x = %x
  49. // CHECK:STDOUT: .y = %y
  50. // CHECK:STDOUT: .z = %z
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %Core.import = import Core
  53. // CHECK:STDOUT: %.loc11_13.1: i32 = int_literal 64 [template = constants.%.1]
  54. // CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc11_13.1) [template = f64]
  55. // CHECK:STDOUT: %.loc11_13.2: type = value_of_initializer %float.make_type [template = f64]
  56. // CHECK:STDOUT: %.loc11_13.3: type = converted %float.make_type, %.loc11_13.2 [template = f64]
  57. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  58. // CHECK:STDOUT: %.loc11_22.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  59. // CHECK:STDOUT: %.loc11_22.2: type = converted %int.make_type_32.loc11, %.loc11_22.1 [template = i32]
  60. // CHECK:STDOUT: %.loc11_25: type = struct_type {.a: f64, .b: i32} [template = constants.%.3]
  61. // CHECK:STDOUT: %x.var: ref %.3 = var x
  62. // CHECK:STDOUT: %x: ref %.3 = bind_name x, %x.var
  63. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  64. // CHECK:STDOUT: %.loc12_8.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  65. // CHECK:STDOUT: %.loc12_8.2: type = converted %int.make_type_32.loc12, %.loc12_8.1 [template = i32]
  66. // CHECK:STDOUT: %y.var: ref i32 = var y
  67. // CHECK:STDOUT: %y: ref i32 = bind_name y, %y.var
  68. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc13_8.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  70. // CHECK:STDOUT: %.loc13_8.2: type = converted %int.make_type_32.loc13, %.loc13_8.1 [template = i32]
  71. // CHECK:STDOUT: %z.var: ref i32 = var z
  72. // CHECK:STDOUT: %z: ref i32 = bind_name z, %z.var
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: fn @Float(%size: i32) -> type = "float.make_type";
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: fn @__global_init() {
  80. // CHECK:STDOUT: !entry:
  81. // CHECK:STDOUT: %.loc11_35: f64 = float_literal 0 [template = constants.%.5]
  82. // CHECK:STDOUT: %.loc11_45: i32 = int_literal 1 [template = constants.%.6]
  83. // CHECK:STDOUT: %.loc11_46.1: %.3 = struct_literal (%.loc11_35, %.loc11_45)
  84. // CHECK:STDOUT: %.loc11_46.2: ref f64 = struct_access file.%x.var, element0
  85. // CHECK:STDOUT: %.loc11_46.3: init f64 = initialize_from %.loc11_35 to %.loc11_46.2 [template = constants.%.5]
  86. // CHECK:STDOUT: %.loc11_46.4: ref i32 = struct_access file.%x.var, element1
  87. // CHECK:STDOUT: %.loc11_46.5: init i32 = initialize_from %.loc11_45 to %.loc11_46.4 [template = constants.%.6]
  88. // CHECK:STDOUT: %.loc11_46.6: init %.3 = struct_init (%.loc11_46.3, %.loc11_46.5) to file.%x.var [template = constants.%struct]
  89. // CHECK:STDOUT: %.loc11_47: init %.3 = converted %.loc11_46.1, %.loc11_46.6 [template = constants.%struct]
  90. // CHECK:STDOUT: assign file.%x.var, %.loc11_47
  91. // CHECK:STDOUT: %x.ref: ref %.3 = name_ref x, file.%x
  92. // CHECK:STDOUT: %.loc12_15.1: ref i32 = struct_access %x.ref, element1
  93. // CHECK:STDOUT: %.loc12_15.2: i32 = bind_value %.loc12_15.1
  94. // CHECK:STDOUT: assign file.%y.var, %.loc12_15.2
  95. // CHECK:STDOUT: %y.ref: ref i32 = name_ref y, file.%y
  96. // CHECK:STDOUT: %.loc13: i32 = bind_value %y.ref
  97. // CHECK:STDOUT: assign file.%z.var, %.loc13
  98. // CHECK:STDOUT: return
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: