array_element_access.carbon 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/index/array_element_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/array_element_access.carbon
  10. var a: [i32; 2] = (12, 24);
  11. var b: i32 = 1;
  12. var c: i32 = a[0];
  13. var d: i32 = a[b];
  14. // CHECK:STDOUT: --- array_element_access.carbon
  15. // CHECK:STDOUT:
  16. // CHECK:STDOUT: constants {
  17. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  18. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  19. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  20. // CHECK:STDOUT: %.2: i32 = int_literal 2 [template]
  21. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  22. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  23. // CHECK:STDOUT: %.5: i32 = int_literal 12 [template]
  24. // CHECK:STDOUT: %.6: i32 = int_literal 24 [template]
  25. // CHECK:STDOUT: %.7: type = tuple_type (i32, i32) [template]
  26. // CHECK:STDOUT: %.8: i32 = int_literal 0 [template]
  27. // CHECK:STDOUT: %.9: i32 = int_literal 1 [template]
  28. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5, %.6) [template]
  29. // CHECK:STDOUT: }
  30. // CHECK:STDOUT:
  31. // CHECK:STDOUT: file {
  32. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  33. // CHECK:STDOUT: .Core = %Core
  34. // CHECK:STDOUT: .a = %a
  35. // CHECK:STDOUT: .b = %b
  36. // CHECK:STDOUT: .c = %c
  37. // CHECK:STDOUT: .d = %d
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  40. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  41. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  42. // CHECK:STDOUT: %.loc11_14: i32 = int_literal 2 [template = constants.%.2]
  43. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  44. // CHECK:STDOUT: %.loc11_9.2: type = converted %int.make_type_32.loc11, %.loc11_9.1 [template = i32]
  45. // CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
  46. // CHECK:STDOUT: %a.var: ref %.3 = var a
  47. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  48. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  50. // CHECK:STDOUT: %.loc12_8.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  51. // CHECK:STDOUT: %.loc12_8.2: type = converted %int.make_type_32.loc12, %.loc12_8.1 [template = i32]
  52. // CHECK:STDOUT: %b.var: ref i32 = var b
  53. // CHECK:STDOUT: %b: ref i32 = bind_name b, %b.var
  54. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  55. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  56. // CHECK:STDOUT: %.loc13_8.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  57. // CHECK:STDOUT: %.loc13_8.2: type = converted %int.make_type_32.loc13, %.loc13_8.1 [template = i32]
  58. // CHECK:STDOUT: %c.var: ref i32 = var c
  59. // CHECK:STDOUT: %c: ref i32 = bind_name c, %c.var
  60. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  61. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  62. // CHECK:STDOUT: %.loc14_8.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  63. // CHECK:STDOUT: %.loc14_8.2: type = converted %int.make_type_32.loc14, %.loc14_8.1 [template = i32]
  64. // CHECK:STDOUT: %d.var: ref i32 = var d
  65. // CHECK:STDOUT: %d: ref i32 = bind_name d, %d.var
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: fn @__global_init() {
  71. // CHECK:STDOUT: !entry:
  72. // CHECK:STDOUT: %.loc11_20: i32 = int_literal 12 [template = constants.%.5]
  73. // CHECK:STDOUT: %.loc11_24: i32 = int_literal 24 [template = constants.%.6]
  74. // CHECK:STDOUT: %.loc11_26.1: %.7 = tuple_literal (%.loc11_20, %.loc11_24)
  75. // CHECK:STDOUT: %.loc11_26.2: i32 = int_literal 0 [template = constants.%.8]
  76. // CHECK:STDOUT: %.loc11_26.3: ref i32 = array_index file.%a.var, %.loc11_26.2
  77. // CHECK:STDOUT: %.loc11_26.4: init i32 = initialize_from %.loc11_20 to %.loc11_26.3 [template = constants.%.5]
  78. // CHECK:STDOUT: %.loc11_26.5: i32 = int_literal 1 [template = constants.%.9]
  79. // CHECK:STDOUT: %.loc11_26.6: ref i32 = array_index file.%a.var, %.loc11_26.5
  80. // CHECK:STDOUT: %.loc11_26.7: init i32 = initialize_from %.loc11_24 to %.loc11_26.6 [template = constants.%.6]
  81. // CHECK:STDOUT: %.loc11_26.8: init %.3 = array_init (%.loc11_26.4, %.loc11_26.7) to file.%a.var [template = constants.%array]
  82. // CHECK:STDOUT: %.loc11_27: init %.3 = converted %.loc11_26.1, %.loc11_26.8 [template = constants.%array]
  83. // CHECK:STDOUT: assign file.%a.var, %.loc11_27
  84. // CHECK:STDOUT: %.loc12: i32 = int_literal 1 [template = constants.%.9]
  85. // CHECK:STDOUT: assign file.%b.var, %.loc12
  86. // CHECK:STDOUT: %a.ref.loc13: ref %.3 = name_ref a, file.%a
  87. // CHECK:STDOUT: %.loc13_16: i32 = int_literal 0 [template = constants.%.8]
  88. // CHECK:STDOUT: %.loc13_17.1: ref i32 = array_index %a.ref.loc13, %.loc13_16
  89. // CHECK:STDOUT: %.loc13_17.2: i32 = bind_value %.loc13_17.1
  90. // CHECK:STDOUT: assign file.%c.var, %.loc13_17.2
  91. // CHECK:STDOUT: %a.ref.loc14: ref %.3 = name_ref a, file.%a
  92. // CHECK:STDOUT: %b.ref: ref i32 = name_ref b, file.%b
  93. // CHECK:STDOUT: %.loc14_16: i32 = bind_value %b.ref
  94. // CHECK:STDOUT: %.loc14_17.1: ref i32 = array_index %a.ref.loc14, %.loc14_16
  95. // CHECK:STDOUT: %.loc14_17.2: i32 = bind_value %.loc14_17.1
  96. // CHECK:STDOUT: assign file.%d.var, %.loc14_17.2
  97. // CHECK:STDOUT: return
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: