member_access.carbon 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: %int_64: Core.IntLiteral = int_value 64 [template]
  17. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  18. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  19. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  20. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  21. // CHECK:STDOUT: %struct_type.a.b.ddf: type = struct_type {.a: f64, .b: %i32} [template]
  22. // CHECK:STDOUT: %float: f64 = float_literal 0 [template]
  23. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  24. // CHECK:STDOUT: %struct_type.a.b.fbb: type = struct_type {.a: f64, .b: Core.IntLiteral} [template]
  25. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  26. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  27. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  28. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  29. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  30. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  31. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  32. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  33. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  34. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  35. // CHECK:STDOUT: %struct: %struct_type.a.b.ddf = struct_value (%float, %int_1.5d2) [template]
  36. // CHECK:STDOUT: }
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: imports {
  39. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  40. // CHECK:STDOUT: .Float = %Core.Float
  41. // CHECK:STDOUT: .Int = %Core.Int
  42. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/...
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = imports.%Core
  51. // CHECK:STDOUT: .x = %x
  52. // CHECK:STDOUT: .y = %y
  53. // CHECK:STDOUT: .z = %z
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %Core.import = import Core
  56. // CHECK:STDOUT: name_binding_decl {
  57. // CHECK:STDOUT: %x.patt: %struct_type.a.b.ddf = binding_pattern x
  58. // CHECK:STDOUT: %.loc11_1: %struct_type.a.b.ddf = var_pattern %x.patt
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %x.var: ref %struct_type.a.b.ddf = var x
  61. // CHECK:STDOUT: %.loc11_25: type = splice_block %struct_type.a.b [template = constants.%struct_type.a.b.ddf] {
  62. // CHECK:STDOUT: %int_64: Core.IntLiteral = int_value 64 [template = constants.%int_64]
  63. // CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%int_64) [template = f64]
  64. // CHECK:STDOUT: %.loc11_13.1: type = value_of_initializer %float.make_type [template = f64]
  65. // CHECK:STDOUT: %.loc11_13.2: type = converted %float.make_type, %.loc11_13.1 [template = f64]
  66. // CHECK:STDOUT: %int_32.loc11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  67. // CHECK:STDOUT: %i32.loc11: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  68. // CHECK:STDOUT: %struct_type.a.b: type = struct_type {.a: f64, .b: %i32} [template = constants.%struct_type.a.b.ddf]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %x: ref %struct_type.a.b.ddf = bind_name x, %x.var
  71. // CHECK:STDOUT: name_binding_decl {
  72. // CHECK:STDOUT: %y.patt: %i32 = binding_pattern y
  73. // CHECK:STDOUT: %.loc12_1: %i32 = var_pattern %y.patt
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %y.var: ref %i32 = var y
  76. // CHECK:STDOUT: %.loc12_8: type = splice_block %i32.loc12 [template = constants.%i32] {
  77. // CHECK:STDOUT: %int_32.loc12: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  78. // CHECK:STDOUT: %i32.loc12: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
  81. // CHECK:STDOUT: name_binding_decl {
  82. // CHECK:STDOUT: %z.patt: %i32 = binding_pattern z
  83. // CHECK:STDOUT: %.loc13_1: %i32 = var_pattern %z.patt
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %z.var: ref %i32 = var z
  86. // CHECK:STDOUT: %.loc13_8: type = splice_block %i32.loc13 [template = constants.%i32] {
  87. // CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  88. // CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %z: ref %i32 = bind_name z, %z.var
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @__global_init() {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: %float: f64 = float_literal 0 [template = constants.%float]
  96. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  97. // CHECK:STDOUT: %.loc11_46.1: %struct_type.a.b.fbb = struct_literal (%float, %int_1)
  98. // CHECK:STDOUT: %.loc11_46.2: ref f64 = struct_access file.%x.var, element0
  99. // CHECK:STDOUT: %.loc11_46.3: init f64 = initialize_from %float to %.loc11_46.2 [template = constants.%float]
  100. // CHECK:STDOUT: %impl.elem0: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  101. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound]
  102. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %bound_method, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  103. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %specific_fn(%int_1) [template = constants.%int_1.5d2]
  104. // CHECK:STDOUT: %.loc11_46.4: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.5d2]
  105. // CHECK:STDOUT: %.loc11_46.5: ref %i32 = struct_access file.%x.var, element1
  106. // CHECK:STDOUT: %.loc11_46.6: init %i32 = initialize_from %.loc11_46.4 to %.loc11_46.5 [template = constants.%int_1.5d2]
  107. // CHECK:STDOUT: %.loc11_46.7: init %struct_type.a.b.ddf = struct_init (%.loc11_46.3, %.loc11_46.6) to file.%x.var [template = constants.%struct]
  108. // CHECK:STDOUT: %.loc11_1: init %struct_type.a.b.ddf = converted %.loc11_46.1, %.loc11_46.7 [template = constants.%struct]
  109. // CHECK:STDOUT: assign file.%x.var, %.loc11_1
  110. // CHECK:STDOUT: %x.ref: ref %struct_type.a.b.ddf = name_ref x, file.%x
  111. // CHECK:STDOUT: %.loc12_15.1: ref %i32 = struct_access %x.ref, element1
  112. // CHECK:STDOUT: %.loc12_15.2: %i32 = bind_value %.loc12_15.1
  113. // CHECK:STDOUT: assign file.%y.var, %.loc12_15.2
  114. // CHECK:STDOUT: %y.ref: ref %i32 = name_ref y, file.%y
  115. // CHECK:STDOUT: %.loc13: %i32 = bind_value %y.ref
  116. // CHECK:STDOUT: assign file.%z.var, %.loc13
  117. // CHECK:STDOUT: return
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: