expr_category.carbon 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. fn F() -> [i32; 3];
  7. fn G(b: [i32; 3]) {
  8. var a: [i32; 3] = (1, 2, 3);
  9. // Indexing a durable array reference gives a durable reference.
  10. var pa: i32* = &a[0];
  11. a[0] = 4;
  12. }
  13. fn ValueBinding(b: [i32; 3]) {
  14. var a: [i32; 3] = (1, 2, 3);
  15. // Index but don't do anything else so we can check that a value binding is
  16. // produced when appropriate.
  17. a[0];
  18. b[0];
  19. F()[0];
  20. }
  21. // CHECK:STDOUT: --- expr_category.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %.1: i32 = int_literal 3 [template]
  25. // CHECK:STDOUT: %.2: type = array_type %.1, i32 [template]
  26. // CHECK:STDOUT: %.3: type = ptr_type [i32; 3] [template]
  27. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  28. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  29. // CHECK:STDOUT: %.6: type = tuple_type (i32, i32, i32) [template]
  30. // CHECK:STDOUT: %.7: i32 = int_literal 0 [template]
  31. // CHECK:STDOUT: %.8: [i32; 3] = tuple_value (%.4, %.5, %.1) [template]
  32. // CHECK:STDOUT: %.9: type = ptr_type i32 [template]
  33. // CHECK:STDOUT: %.10: i32 = int_literal 4 [template]
  34. // CHECK:STDOUT: }
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: file {
  37. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  38. // CHECK:STDOUT: .F = %F
  39. // CHECK:STDOUT: .G = %G
  40. // CHECK:STDOUT: .ValueBinding = %ValueBinding
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
  43. // CHECK:STDOUT: %.loc7_17: i32 = int_literal 3 [template = constants.%.1]
  44. // CHECK:STDOUT: %.loc7_18: type = array_type %.loc7_17, i32 [template = constants.%.2]
  45. // CHECK:STDOUT: @F.%return: ref [i32; 3] = var <return slot>
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT: %G: <function> = fn_decl @G [template] {
  48. // CHECK:STDOUT: %.loc9_15: i32 = int_literal 3 [template = constants.%.1]
  49. // CHECK:STDOUT: %.loc9_16: type = array_type %.loc9_15, i32 [template = constants.%.2]
  50. // CHECK:STDOUT: %b.loc9_6.1: [i32; 3] = param b
  51. // CHECK:STDOUT: @G.%b: [i32; 3] = bind_name b, %b.loc9_6.1
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %ValueBinding: <function> = fn_decl @ValueBinding [template] {
  54. // CHECK:STDOUT: %.loc17_26: i32 = int_literal 3 [template = constants.%.1]
  55. // CHECK:STDOUT: %.loc17_27: type = array_type %.loc17_26, i32 [template = constants.%.2]
  56. // CHECK:STDOUT: %b.loc17_17.1: [i32; 3] = param b
  57. // CHECK:STDOUT: @ValueBinding.%b: [i32; 3] = bind_name b, %b.loc17_17.1
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: fn @F() -> %return: [i32; 3];
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: fn @G(%b: [i32; 3]) {
  64. // CHECK:STDOUT: !entry:
  65. // CHECK:STDOUT: %.loc10_16: i32 = int_literal 3 [template = constants.%.1]
  66. // CHECK:STDOUT: %.loc10_17: type = array_type %.loc10_16, i32 [template = constants.%.2]
  67. // CHECK:STDOUT: %a.var: ref [i32; 3] = var a
  68. // CHECK:STDOUT: %a: ref [i32; 3] = bind_name a, %a.var
  69. // CHECK:STDOUT: %.loc10_22: i32 = int_literal 1 [template = constants.%.4]
  70. // CHECK:STDOUT: %.loc10_25: i32 = int_literal 2 [template = constants.%.5]
  71. // CHECK:STDOUT: %.loc10_28: i32 = int_literal 3 [template = constants.%.1]
  72. // CHECK:STDOUT: %.loc10_29.1: (i32, i32, i32) = tuple_literal (%.loc10_22, %.loc10_25, %.loc10_28)
  73. // CHECK:STDOUT: %.loc10_29.2: i32 = int_literal 0 [template = constants.%.7]
  74. // CHECK:STDOUT: %.loc10_29.3: ref i32 = array_index %a.var, %.loc10_29.2
  75. // CHECK:STDOUT: %.loc10_29.4: init i32 = initialize_from %.loc10_22 to %.loc10_29.3 [template = constants.%.4]
  76. // CHECK:STDOUT: %.loc10_29.5: i32 = int_literal 1 [template = constants.%.4]
  77. // CHECK:STDOUT: %.loc10_29.6: ref i32 = array_index %a.var, %.loc10_29.5
  78. // CHECK:STDOUT: %.loc10_29.7: init i32 = initialize_from %.loc10_25 to %.loc10_29.6 [template = constants.%.5]
  79. // CHECK:STDOUT: %.loc10_29.8: i32 = int_literal 2 [template = constants.%.5]
  80. // CHECK:STDOUT: %.loc10_29.9: ref i32 = array_index %a.var, %.loc10_29.8
  81. // CHECK:STDOUT: %.loc10_29.10: init i32 = initialize_from %.loc10_28 to %.loc10_29.9 [template = constants.%.1]
  82. // CHECK:STDOUT: %.loc10_29.11: init [i32; 3] = array_init (%.loc10_29.4, %.loc10_29.7, %.loc10_29.10) to %a.var [template = constants.%.8]
  83. // CHECK:STDOUT: %.loc10_29.12: init [i32; 3] = converted %.loc10_29.1, %.loc10_29.11 [template = constants.%.8]
  84. // CHECK:STDOUT: assign %a.var, %.loc10_29.12
  85. // CHECK:STDOUT: %.loc13_14: type = ptr_type i32 [template = constants.%.9]
  86. // CHECK:STDOUT: %pa.var: ref i32* = var pa
  87. // CHECK:STDOUT: %pa: ref i32* = bind_name pa, %pa.var
  88. // CHECK:STDOUT: %a.ref.loc13: ref [i32; 3] = name_ref a, %a
  89. // CHECK:STDOUT: %.loc13_21: i32 = int_literal 0 [template = constants.%.7]
  90. // CHECK:STDOUT: %.loc13_22: ref i32 = array_index %a.ref.loc13, %.loc13_21
  91. // CHECK:STDOUT: %.loc13_18: i32* = addr_of %.loc13_22
  92. // CHECK:STDOUT: assign %pa.var, %.loc13_18
  93. // CHECK:STDOUT: %a.ref.loc14: ref [i32; 3] = name_ref a, %a
  94. // CHECK:STDOUT: %.loc14_5: i32 = int_literal 0 [template = constants.%.7]
  95. // CHECK:STDOUT: %.loc14_6: ref i32 = array_index %a.ref.loc14, %.loc14_5
  96. // CHECK:STDOUT: %.loc14_10: i32 = int_literal 4 [template = constants.%.10]
  97. // CHECK:STDOUT: assign %.loc14_6, %.loc14_10
  98. // CHECK:STDOUT: return
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: fn @ValueBinding(%b: [i32; 3]) {
  102. // CHECK:STDOUT: !entry:
  103. // CHECK:STDOUT: %.loc18_16: i32 = int_literal 3 [template = constants.%.1]
  104. // CHECK:STDOUT: %.loc18_17: type = array_type %.loc18_16, i32 [template = constants.%.2]
  105. // CHECK:STDOUT: %a.var: ref [i32; 3] = var a
  106. // CHECK:STDOUT: %a: ref [i32; 3] = bind_name a, %a.var
  107. // CHECK:STDOUT: %.loc18_22: i32 = int_literal 1 [template = constants.%.4]
  108. // CHECK:STDOUT: %.loc18_25: i32 = int_literal 2 [template = constants.%.5]
  109. // CHECK:STDOUT: %.loc18_28: i32 = int_literal 3 [template = constants.%.1]
  110. // CHECK:STDOUT: %.loc18_29.1: (i32, i32, i32) = tuple_literal (%.loc18_22, %.loc18_25, %.loc18_28)
  111. // CHECK:STDOUT: %.loc18_29.2: i32 = int_literal 0 [template = constants.%.7]
  112. // CHECK:STDOUT: %.loc18_29.3: ref i32 = array_index %a.var, %.loc18_29.2
  113. // CHECK:STDOUT: %.loc18_29.4: init i32 = initialize_from %.loc18_22 to %.loc18_29.3 [template = constants.%.4]
  114. // CHECK:STDOUT: %.loc18_29.5: i32 = int_literal 1 [template = constants.%.4]
  115. // CHECK:STDOUT: %.loc18_29.6: ref i32 = array_index %a.var, %.loc18_29.5
  116. // CHECK:STDOUT: %.loc18_29.7: init i32 = initialize_from %.loc18_25 to %.loc18_29.6 [template = constants.%.5]
  117. // CHECK:STDOUT: %.loc18_29.8: i32 = int_literal 2 [template = constants.%.5]
  118. // CHECK:STDOUT: %.loc18_29.9: ref i32 = array_index %a.var, %.loc18_29.8
  119. // CHECK:STDOUT: %.loc18_29.10: init i32 = initialize_from %.loc18_28 to %.loc18_29.9 [template = constants.%.1]
  120. // CHECK:STDOUT: %.loc18_29.11: init [i32; 3] = array_init (%.loc18_29.4, %.loc18_29.7, %.loc18_29.10) to %a.var [template = constants.%.8]
  121. // CHECK:STDOUT: %.loc18_29.12: init [i32; 3] = converted %.loc18_29.1, %.loc18_29.11 [template = constants.%.8]
  122. // CHECK:STDOUT: assign %a.var, %.loc18_29.12
  123. // CHECK:STDOUT: %a.ref: ref [i32; 3] = name_ref a, %a
  124. // CHECK:STDOUT: %.loc22_5: i32 = int_literal 0 [template = constants.%.7]
  125. // CHECK:STDOUT: %.loc22_6: ref i32 = array_index %a.ref, %.loc22_5
  126. // CHECK:STDOUT: %b.ref: [i32; 3] = name_ref b, %b
  127. // CHECK:STDOUT: %.loc23_5: i32 = int_literal 0 [template = constants.%.7]
  128. // CHECK:STDOUT: %.loc23_6.1: ref [i32; 3] = value_as_ref %b.ref
  129. // CHECK:STDOUT: %.loc23_6.2: ref i32 = array_index %.loc23_6.1, %.loc23_5
  130. // CHECK:STDOUT: %.loc23_6.3: i32 = bind_value %.loc23_6.2
  131. // CHECK:STDOUT: %F.ref: <function> = name_ref F, file.%F [template = file.%F]
  132. // CHECK:STDOUT: %.loc24_4.1: ref [i32; 3] = temporary_storage
  133. // CHECK:STDOUT: %.loc24_4.2: init [i32; 3] = call %F.ref() to %.loc24_4.1
  134. // CHECK:STDOUT: %.loc24_7: i32 = int_literal 0 [template = constants.%.7]
  135. // CHECK:STDOUT: %.loc24_4.3: ref [i32; 3] = temporary %.loc24_4.1, %.loc24_4.2
  136. // CHECK:STDOUT: %.loc24_8.1: ref i32 = array_index %.loc24_4.3, %.loc24_7
  137. // CHECK:STDOUT: %.loc24_8.2: i32 = bind_value %.loc24_8.1
  138. // CHECK:STDOUT: return
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: