expr_category.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/expr_category.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/expr_category.carbon
  10. fn F() -> [i32; 3];
  11. fn G(b: [i32; 3]) {
  12. var a: [i32; 3] = (1, 2, 3);
  13. // Indexing a durable array reference gives a durable reference.
  14. var pa: i32* = &a[0];
  15. a[0] = 4;
  16. }
  17. fn ValueBinding(b: [i32; 3]) {
  18. var a: [i32; 3] = (1, 2, 3);
  19. // Index but don't do anything else so we can check that a value binding is
  20. // produced when appropriate.
  21. a[0];
  22. b[0];
  23. F()[0];
  24. }
  25. // CHECK:STDOUT: --- expr_category.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  29. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  30. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  31. // CHECK:STDOUT: %.2: i32 = int_literal 3 [template]
  32. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  33. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  34. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  35. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  36. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  37. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  38. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  39. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  40. // CHECK:STDOUT: %.7: type = tuple_type (i32, i32, i32) [template]
  41. // CHECK:STDOUT: %.8: i32 = int_literal 0 [template]
  42. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5, %.6, %.2) [template]
  43. // CHECK:STDOUT: %.9: type = ptr_type i32 [template]
  44. // CHECK:STDOUT: %.10: i32 = int_literal 4 [template]
  45. // CHECK:STDOUT: %ValueBinding.type: type = fn_type @ValueBinding [template]
  46. // CHECK:STDOUT: %ValueBinding: %ValueBinding.type = struct_value () [template]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: imports {
  50. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  51. // CHECK:STDOUT: .Int32 = %import_ref
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/operators
  54. // CHECK:STDOUT: import Core//prelude/types
  55. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  56. // CHECK:STDOUT: import Core//prelude/operators/as
  57. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  58. // CHECK:STDOUT: import Core//prelude/operators/comparison
  59. // CHECK:STDOUT: import Core//prelude/types/bool
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: file {
  65. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  66. // CHECK:STDOUT: .Core = imports.%Core
  67. // CHECK:STDOUT: .F = %F.decl
  68. // CHECK:STDOUT: .G = %G.decl
  69. // CHECK:STDOUT: .ValueBinding = %ValueBinding.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {
  73. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %.loc11_17: i32 = int_literal 3 [template = constants.%.2]
  75. // CHECK:STDOUT: %.loc11_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  76. // CHECK:STDOUT: %.loc11_12.2: type = converted %int.make_type_32, %.loc11_12.1 [template = i32]
  77. // CHECK:STDOUT: %.loc11_18: type = array_type %.loc11_17, i32 [template = constants.%.3]
  78. // CHECK:STDOUT: %return: ref %.3 = var <return slot>
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  81. // CHECK:STDOUT: %b.patt: %.3 = binding_pattern b
  82. // CHECK:STDOUT: } {
  83. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  84. // CHECK:STDOUT: %.loc13_15: i32 = int_literal 3 [template = constants.%.2]
  85. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  86. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  87. // CHECK:STDOUT: %.loc13_16: type = array_type %.loc13_15, i32 [template = constants.%.3]
  88. // CHECK:STDOUT: %b.param: %.3 = param b, runtime_param0
  89. // CHECK:STDOUT: %b: %.3 = bind_name b, %b.param
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %ValueBinding.decl: %ValueBinding.type = fn_decl @ValueBinding [template = constants.%ValueBinding] {
  92. // CHECK:STDOUT: %b.patt: %.3 = binding_pattern b
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  95. // CHECK:STDOUT: %.loc21_26: i32 = int_literal 3 [template = constants.%.2]
  96. // CHECK:STDOUT: %.loc21_21.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  97. // CHECK:STDOUT: %.loc21_21.2: type = converted %int.make_type_32.loc21, %.loc21_21.1 [template = i32]
  98. // CHECK:STDOUT: %.loc21_27: type = array_type %.loc21_26, i32 [template = constants.%.3]
  99. // CHECK:STDOUT: %b.param: %.3 = param b, runtime_param0
  100. // CHECK:STDOUT: %b: %.3 = bind_name b, %b.param
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: fn @F() -> %.3;
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: fn @G(%b: %.3) {
  109. // CHECK:STDOUT: !entry:
  110. // CHECK:STDOUT: %int.make_type_32.loc14: init type = call constants.%Int32() [template = i32]
  111. // CHECK:STDOUT: %.loc14_16: i32 = int_literal 3 [template = constants.%.2]
  112. // CHECK:STDOUT: %.loc14_11.1: type = value_of_initializer %int.make_type_32.loc14 [template = i32]
  113. // CHECK:STDOUT: %.loc14_11.2: type = converted %int.make_type_32.loc14, %.loc14_11.1 [template = i32]
  114. // CHECK:STDOUT: %.loc14_17: type = array_type %.loc14_16, i32 [template = constants.%.3]
  115. // CHECK:STDOUT: %a.var: ref %.3 = var a
  116. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  117. // CHECK:STDOUT: %.loc14_22: i32 = int_literal 1 [template = constants.%.5]
  118. // CHECK:STDOUT: %.loc14_25: i32 = int_literal 2 [template = constants.%.6]
  119. // CHECK:STDOUT: %.loc14_28: i32 = int_literal 3 [template = constants.%.2]
  120. // CHECK:STDOUT: %.loc14_29.1: %.7 = tuple_literal (%.loc14_22, %.loc14_25, %.loc14_28)
  121. // CHECK:STDOUT: %.loc14_29.2: i32 = int_literal 0 [template = constants.%.8]
  122. // CHECK:STDOUT: %.loc14_29.3: ref i32 = array_index %a.var, %.loc14_29.2
  123. // CHECK:STDOUT: %.loc14_29.4: init i32 = initialize_from %.loc14_22 to %.loc14_29.3 [template = constants.%.5]
  124. // CHECK:STDOUT: %.loc14_29.5: i32 = int_literal 1 [template = constants.%.5]
  125. // CHECK:STDOUT: %.loc14_29.6: ref i32 = array_index %a.var, %.loc14_29.5
  126. // CHECK:STDOUT: %.loc14_29.7: init i32 = initialize_from %.loc14_25 to %.loc14_29.6 [template = constants.%.6]
  127. // CHECK:STDOUT: %.loc14_29.8: i32 = int_literal 2 [template = constants.%.6]
  128. // CHECK:STDOUT: %.loc14_29.9: ref i32 = array_index %a.var, %.loc14_29.8
  129. // CHECK:STDOUT: %.loc14_29.10: init i32 = initialize_from %.loc14_28 to %.loc14_29.9 [template = constants.%.2]
  130. // CHECK:STDOUT: %.loc14_29.11: init %.3 = array_init (%.loc14_29.4, %.loc14_29.7, %.loc14_29.10) to %a.var [template = constants.%array]
  131. // CHECK:STDOUT: %.loc14_30: init %.3 = converted %.loc14_29.1, %.loc14_29.11 [template = constants.%array]
  132. // CHECK:STDOUT: assign %a.var, %.loc14_30
  133. // CHECK:STDOUT: %int.make_type_32.loc17: init type = call constants.%Int32() [template = i32]
  134. // CHECK:STDOUT: %.loc17_14.1: type = value_of_initializer %int.make_type_32.loc17 [template = i32]
  135. // CHECK:STDOUT: %.loc17_14.2: type = converted %int.make_type_32.loc17, %.loc17_14.1 [template = i32]
  136. // CHECK:STDOUT: %.loc17_14.3: type = ptr_type i32 [template = constants.%.9]
  137. // CHECK:STDOUT: %pa.var: ref %.9 = var pa
  138. // CHECK:STDOUT: %pa: ref %.9 = bind_name pa, %pa.var
  139. // CHECK:STDOUT: %a.ref.loc17: ref %.3 = name_ref a, %a
  140. // CHECK:STDOUT: %.loc17_21: i32 = int_literal 0 [template = constants.%.8]
  141. // CHECK:STDOUT: %.loc17_22: ref i32 = array_index %a.ref.loc17, %.loc17_21
  142. // CHECK:STDOUT: %.loc17_18: %.9 = addr_of %.loc17_22
  143. // CHECK:STDOUT: assign %pa.var, %.loc17_18
  144. // CHECK:STDOUT: %a.ref.loc18: ref %.3 = name_ref a, %a
  145. // CHECK:STDOUT: %.loc18_5: i32 = int_literal 0 [template = constants.%.8]
  146. // CHECK:STDOUT: %.loc18_6: ref i32 = array_index %a.ref.loc18, %.loc18_5
  147. // CHECK:STDOUT: %.loc18_10: i32 = int_literal 4 [template = constants.%.10]
  148. // CHECK:STDOUT: assign %.loc18_6, %.loc18_10
  149. // CHECK:STDOUT: return
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: fn @ValueBinding(%b: %.3) {
  153. // CHECK:STDOUT: !entry:
  154. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  155. // CHECK:STDOUT: %.loc22_16: i32 = int_literal 3 [template = constants.%.2]
  156. // CHECK:STDOUT: %.loc22_11.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  157. // CHECK:STDOUT: %.loc22_11.2: type = converted %int.make_type_32.loc22, %.loc22_11.1 [template = i32]
  158. // CHECK:STDOUT: %.loc22_17: type = array_type %.loc22_16, i32 [template = constants.%.3]
  159. // CHECK:STDOUT: %a.var: ref %.3 = var a
  160. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  161. // CHECK:STDOUT: %.loc22_22: i32 = int_literal 1 [template = constants.%.5]
  162. // CHECK:STDOUT: %.loc22_25: i32 = int_literal 2 [template = constants.%.6]
  163. // CHECK:STDOUT: %.loc22_28: i32 = int_literal 3 [template = constants.%.2]
  164. // CHECK:STDOUT: %.loc22_29.1: %.7 = tuple_literal (%.loc22_22, %.loc22_25, %.loc22_28)
  165. // CHECK:STDOUT: %.loc22_29.2: i32 = int_literal 0 [template = constants.%.8]
  166. // CHECK:STDOUT: %.loc22_29.3: ref i32 = array_index %a.var, %.loc22_29.2
  167. // CHECK:STDOUT: %.loc22_29.4: init i32 = initialize_from %.loc22_22 to %.loc22_29.3 [template = constants.%.5]
  168. // CHECK:STDOUT: %.loc22_29.5: i32 = int_literal 1 [template = constants.%.5]
  169. // CHECK:STDOUT: %.loc22_29.6: ref i32 = array_index %a.var, %.loc22_29.5
  170. // CHECK:STDOUT: %.loc22_29.7: init i32 = initialize_from %.loc22_25 to %.loc22_29.6 [template = constants.%.6]
  171. // CHECK:STDOUT: %.loc22_29.8: i32 = int_literal 2 [template = constants.%.6]
  172. // CHECK:STDOUT: %.loc22_29.9: ref i32 = array_index %a.var, %.loc22_29.8
  173. // CHECK:STDOUT: %.loc22_29.10: init i32 = initialize_from %.loc22_28 to %.loc22_29.9 [template = constants.%.2]
  174. // CHECK:STDOUT: %.loc22_29.11: init %.3 = array_init (%.loc22_29.4, %.loc22_29.7, %.loc22_29.10) to %a.var [template = constants.%array]
  175. // CHECK:STDOUT: %.loc22_30: init %.3 = converted %.loc22_29.1, %.loc22_29.11 [template = constants.%array]
  176. // CHECK:STDOUT: assign %a.var, %.loc22_30
  177. // CHECK:STDOUT: %a.ref: ref %.3 = name_ref a, %a
  178. // CHECK:STDOUT: %.loc26_5: i32 = int_literal 0 [template = constants.%.8]
  179. // CHECK:STDOUT: %.loc26_6: ref i32 = array_index %a.ref, %.loc26_5
  180. // CHECK:STDOUT: %b.ref: %.3 = name_ref b, %b
  181. // CHECK:STDOUT: %.loc27_5: i32 = int_literal 0 [template = constants.%.8]
  182. // CHECK:STDOUT: %.loc27_6.1: ref %.3 = value_as_ref %b.ref
  183. // CHECK:STDOUT: %.loc27_6.2: ref i32 = array_index %.loc27_6.1, %.loc27_5
  184. // CHECK:STDOUT: %.loc27_6.3: i32 = bind_value %.loc27_6.2
  185. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  186. // CHECK:STDOUT: %.loc28_4.1: ref %.3 = temporary_storage
  187. // CHECK:STDOUT: %F.call: init %.3 = call %F.ref() to %.loc28_4.1
  188. // CHECK:STDOUT: %.loc28_7: i32 = int_literal 0 [template = constants.%.8]
  189. // CHECK:STDOUT: %.loc28_4.2: ref %.3 = temporary %.loc28_4.1, %F.call
  190. // CHECK:STDOUT: %.loc28_8.1: ref i32 = array_index %.loc28_4.2, %.loc28_7
  191. // CHECK:STDOUT: %.loc28_8.2: i32 = bind_value %.loc28_8.1
  192. // CHECK:STDOUT: return
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: