member_access.carbon 9.7 KB

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