left_shift.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // --- int_left_shift.carbon
  7. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  8. var arr: [i32; LeftShift(5, 2)];
  9. let arr_p: [i32; 20]* = &arr;
  10. fn RuntimeCall(a: i32, b: i32) -> i32 {
  11. return LeftShift(a, b);
  12. }
  13. // TODO: Test mixed types for LHS and RHS.
  14. // --- fail_bad_shift.carbon
  15. package BadShift api;
  16. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  17. fn Negate(a: i32) -> i32 = "int.negate";
  18. // Shift greater than size is disallowed.
  19. let size_1: i32 = LeftShift(1, 31);
  20. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: ERROR: Shift distance not in range [0, 32) in 1 << 32.
  21. // CHECK:STDERR: let size_2: i32 = LeftShift(1, 32);
  22. // CHECK:STDERR: ^~~~~~~~~~
  23. // CHECK:STDERR:
  24. let size_2: i32 = LeftShift(1, 32);
  25. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: ERROR: Shift distance not in range [0, 32) in 1 << 33.
  26. // CHECK:STDERR: let size_3: i32 = LeftShift(1, 33);
  27. // CHECK:STDERR: ^~~~~~~~~~
  28. // CHECK:STDERR:
  29. let size_3: i32 = LeftShift(1, 33);
  30. // Overflow is allowed if the shift distance is in bounds.
  31. let overflow_1: i32 = LeftShift(1000, 31);
  32. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:23: ERROR: Shift distance not in range [0, 32) in 1000 << 32.
  33. // CHECK:STDERR: let overflow_2: i32 = LeftShift(1000, 32);
  34. // CHECK:STDERR: ^~~~~~~~~~
  35. // CHECK:STDERR:
  36. let overflow_2: i32 = LeftShift(1000, 32);
  37. // Oversize shifts aren't allowed even if there's no overflow.
  38. let no_overflow_1: i32 = LeftShift(0, 31);
  39. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: ERROR: Shift distance not in range [0, 32) in 0 << 32.
  40. // CHECK:STDERR: let no_overflow_2: i32 = LeftShift(0, 32);
  41. // CHECK:STDERR: ^~~~~~~~~~
  42. // CHECK:STDERR:
  43. let no_overflow_2: i32 = LeftShift(0, 32);
  44. // Negative shifts aren't allowed either.
  45. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+3]]:21: ERROR: Shift distance not in range [0, 32) in 1 << -1.
  46. // CHECK:STDERR: let negative: i32 = LeftShift(1, Negate(1));
  47. // CHECK:STDERR: ^~~~~~~~~~
  48. let negative: i32 = LeftShift(1, Negate(1));
  49. // CHECK:STDOUT: --- int_left_shift.carbon
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: constants {
  52. // CHECK:STDOUT: %.1: i32 = int_literal 5 [template]
  53. // CHECK:STDOUT: %.2: i32 = int_literal 2 [template]
  54. // CHECK:STDOUT: %.3: i32 = int_literal 20 [template]
  55. // CHECK:STDOUT: %.4: type = array_type %.3, i32 [template]
  56. // CHECK:STDOUT: %.5: type = ptr_type [i32; 20] [template]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .LeftShift = %LeftShift
  62. // CHECK:STDOUT: .arr = %arr
  63. // CHECK:STDOUT: .RuntimeCall = %RuntimeCall
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %LeftShift: <function> = fn_decl @LeftShift [template] {
  66. // CHECK:STDOUT: %a.loc2_14.1: i32 = param a
  67. // CHECK:STDOUT: @LeftShift.%a: i32 = bind_name a, %a.loc2_14.1
  68. // CHECK:STDOUT: %b.loc2_22.1: i32 = param b
  69. // CHECK:STDOUT: @LeftShift.%b: i32 = bind_name b, %b.loc2_22.1
  70. // CHECK:STDOUT: %return.var.loc2: ref i32 = var <return slot>
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %LeftShift.ref: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  73. // CHECK:STDOUT: %.loc4_26: i32 = int_literal 5 [template = constants.%.1]
  74. // CHECK:STDOUT: %.loc4_29: i32 = int_literal 2 [template = constants.%.2]
  75. // CHECK:STDOUT: %.loc4_25: init i32 = call %LeftShift.ref(%.loc4_26, %.loc4_29) [template = constants.%.3]
  76. // CHECK:STDOUT: %.loc4_31: type = array_type %.loc4_25, i32 [template = constants.%.4]
  77. // CHECK:STDOUT: %arr.var: ref [i32; 20] = var arr
  78. // CHECK:STDOUT: %arr: ref [i32; 20] = bind_name arr, %arr.var
  79. // CHECK:STDOUT: %.loc5_18: i32 = int_literal 20 [template = constants.%.3]
  80. // CHECK:STDOUT: %.loc5_20: type = array_type %.loc5_18, i32 [template = constants.%.4]
  81. // CHECK:STDOUT: %.loc5_21: type = ptr_type [i32; 20] [template = constants.%.5]
  82. // CHECK:STDOUT: %arr.ref: ref [i32; 20] = name_ref arr, %arr
  83. // CHECK:STDOUT: %.loc5_25: [i32; 20]* = addr_of %arr.ref
  84. // CHECK:STDOUT: %arr_p: [i32; 20]* = bind_name arr_p, %.loc5_25
  85. // CHECK:STDOUT: %RuntimeCall: <function> = fn_decl @RuntimeCall [template] {
  86. // CHECK:STDOUT: %a.loc7_16.1: i32 = param a
  87. // CHECK:STDOUT: @RuntimeCall.%a: i32 = bind_name a, %a.loc7_16.1
  88. // CHECK:STDOUT: %b.loc7_24.1: i32 = param b
  89. // CHECK:STDOUT: @RuntimeCall.%b: i32 = bind_name b, %b.loc7_24.1
  90. // CHECK:STDOUT: %return.var.loc7: ref i32 = var <return slot>
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @LeftShift(%a: i32, %b: i32) -> i32 = "int.left_shift";
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @RuntimeCall(%a: i32, %b: i32) -> i32 {
  97. // CHECK:STDOUT: !entry:
  98. // CHECK:STDOUT: %LeftShift.ref: <function> = name_ref LeftShift, file.%LeftShift [template = file.%LeftShift]
  99. // CHECK:STDOUT: %a.ref: i32 = name_ref a, %a
  100. // CHECK:STDOUT: %b.ref: i32 = name_ref b, %b
  101. // CHECK:STDOUT: %.loc8_19.1: init i32 = call %LeftShift.ref(%a.ref, %b.ref)
  102. // CHECK:STDOUT: %.loc8_25: i32 = value_of_initializer %.loc8_19.1
  103. // CHECK:STDOUT: %.loc8_19.2: i32 = converted %.loc8_19.1, %.loc8_25
  104. // CHECK:STDOUT: return %.loc8_19.2
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: --- fail_bad_shift.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %.1: i32 = int_literal 1 [template]
  111. // CHECK:STDOUT: %.2: i32 = int_literal 31 [template]
  112. // CHECK:STDOUT: %.3: i32 = int_literal -2147483648 [template]
  113. // CHECK:STDOUT: %.4: i32 = int_literal 32 [template]
  114. // CHECK:STDOUT: %.5: i32 = int_literal 33 [template]
  115. // CHECK:STDOUT: %.6: i32 = int_literal 1000 [template]
  116. // CHECK:STDOUT: %.7: i32 = int_literal 0 [template]
  117. // CHECK:STDOUT: %.8: i32 = int_literal -1 [template]
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: file {
  121. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  122. // CHECK:STDOUT: .LeftShift = %LeftShift
  123. // CHECK:STDOUT: .Negate = %Negate
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: %LeftShift: <function> = fn_decl @LeftShift [template] {
  126. // CHECK:STDOUT: %a.loc4_14.1: i32 = param a
  127. // CHECK:STDOUT: @LeftShift.%a: i32 = bind_name a, %a.loc4_14.1
  128. // CHECK:STDOUT: %b.loc4_22.1: i32 = param b
  129. // CHECK:STDOUT: @LeftShift.%b: i32 = bind_name b, %b.loc4_22.1
  130. // CHECK:STDOUT: %return.var.loc4: ref i32 = var <return slot>
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %Negate: <function> = fn_decl @Negate [template] {
  133. // CHECK:STDOUT: %a.loc5_11.1: i32 = param a
  134. // CHECK:STDOUT: @Negate.%a: i32 = bind_name a, %a.loc5_11.1
  135. // CHECK:STDOUT: %return.var.loc5: ref i32 = var <return slot>
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %LeftShift.ref.loc8: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  138. // CHECK:STDOUT: %.loc8_29: i32 = int_literal 1 [template = constants.%.1]
  139. // CHECK:STDOUT: %.loc8_32: i32 = int_literal 31 [template = constants.%.2]
  140. // CHECK:STDOUT: %.loc8_28.1: init i32 = call %LeftShift.ref.loc8(%.loc8_29, %.loc8_32) [template = constants.%.3]
  141. // CHECK:STDOUT: %.loc8_35: i32 = value_of_initializer %.loc8_28.1 [template = constants.%.3]
  142. // CHECK:STDOUT: %.loc8_28.2: i32 = converted %.loc8_28.1, %.loc8_35 [template = constants.%.3]
  143. // CHECK:STDOUT: %size_1: i32 = bind_name size_1, %.loc8_28.2
  144. // CHECK:STDOUT: %LeftShift.ref.loc13: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  145. // CHECK:STDOUT: %.loc13_29: i32 = int_literal 1 [template = constants.%.1]
  146. // CHECK:STDOUT: %.loc13_32: i32 = int_literal 32 [template = constants.%.4]
  147. // CHECK:STDOUT: %.loc13_28.1: init i32 = call %LeftShift.ref.loc13(%.loc13_29, %.loc13_32) [template = <error>]
  148. // CHECK:STDOUT: %.loc13_35: i32 = value_of_initializer %.loc13_28.1 [template = <error>]
  149. // CHECK:STDOUT: %.loc13_28.2: i32 = converted %.loc13_28.1, %.loc13_35 [template = <error>]
  150. // CHECK:STDOUT: %size_2: i32 = bind_name size_2, %.loc13_28.2
  151. // CHECK:STDOUT: %LeftShift.ref.loc18: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  152. // CHECK:STDOUT: %.loc18_29: i32 = int_literal 1 [template = constants.%.1]
  153. // CHECK:STDOUT: %.loc18_32: i32 = int_literal 33 [template = constants.%.5]
  154. // CHECK:STDOUT: %.loc18_28.1: init i32 = call %LeftShift.ref.loc18(%.loc18_29, %.loc18_32) [template = <error>]
  155. // CHECK:STDOUT: %.loc18_35: i32 = value_of_initializer %.loc18_28.1 [template = <error>]
  156. // CHECK:STDOUT: %.loc18_28.2: i32 = converted %.loc18_28.1, %.loc18_35 [template = <error>]
  157. // CHECK:STDOUT: %size_3: i32 = bind_name size_3, %.loc18_28.2
  158. // CHECK:STDOUT: %LeftShift.ref.loc21: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  159. // CHECK:STDOUT: %.loc21_33: i32 = int_literal 1000 [template = constants.%.6]
  160. // CHECK:STDOUT: %.loc21_39: i32 = int_literal 31 [template = constants.%.2]
  161. // CHECK:STDOUT: %.loc21_32.1: init i32 = call %LeftShift.ref.loc21(%.loc21_33, %.loc21_39) [template = constants.%.7]
  162. // CHECK:STDOUT: %.loc21_42: i32 = value_of_initializer %.loc21_32.1 [template = constants.%.7]
  163. // CHECK:STDOUT: %.loc21_32.2: i32 = converted %.loc21_32.1, %.loc21_42 [template = constants.%.7]
  164. // CHECK:STDOUT: %overflow_1: i32 = bind_name overflow_1, %.loc21_32.2
  165. // CHECK:STDOUT: %LeftShift.ref.loc26: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  166. // CHECK:STDOUT: %.loc26_33: i32 = int_literal 1000 [template = constants.%.6]
  167. // CHECK:STDOUT: %.loc26_39: i32 = int_literal 32 [template = constants.%.4]
  168. // CHECK:STDOUT: %.loc26_32.1: init i32 = call %LeftShift.ref.loc26(%.loc26_33, %.loc26_39) [template = <error>]
  169. // CHECK:STDOUT: %.loc26_42: i32 = value_of_initializer %.loc26_32.1 [template = <error>]
  170. // CHECK:STDOUT: %.loc26_32.2: i32 = converted %.loc26_32.1, %.loc26_42 [template = <error>]
  171. // CHECK:STDOUT: %overflow_2: i32 = bind_name overflow_2, %.loc26_32.2
  172. // CHECK:STDOUT: %LeftShift.ref.loc29: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  173. // CHECK:STDOUT: %.loc29_36: i32 = int_literal 0 [template = constants.%.7]
  174. // CHECK:STDOUT: %.loc29_39: i32 = int_literal 31 [template = constants.%.2]
  175. // CHECK:STDOUT: %.loc29_35.1: init i32 = call %LeftShift.ref.loc29(%.loc29_36, %.loc29_39) [template = constants.%.7]
  176. // CHECK:STDOUT: %.loc29_42: i32 = value_of_initializer %.loc29_35.1 [template = constants.%.7]
  177. // CHECK:STDOUT: %.loc29_35.2: i32 = converted %.loc29_35.1, %.loc29_42 [template = constants.%.7]
  178. // CHECK:STDOUT: %no_overflow_1: i32 = bind_name no_overflow_1, %.loc29_35.2
  179. // CHECK:STDOUT: %LeftShift.ref.loc34: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  180. // CHECK:STDOUT: %.loc34_36: i32 = int_literal 0 [template = constants.%.7]
  181. // CHECK:STDOUT: %.loc34_39: i32 = int_literal 32 [template = constants.%.4]
  182. // CHECK:STDOUT: %.loc34_35.1: init i32 = call %LeftShift.ref.loc34(%.loc34_36, %.loc34_39) [template = <error>]
  183. // CHECK:STDOUT: %.loc34_42: i32 = value_of_initializer %.loc34_35.1 [template = <error>]
  184. // CHECK:STDOUT: %.loc34_35.2: i32 = converted %.loc34_35.1, %.loc34_42 [template = <error>]
  185. // CHECK:STDOUT: %no_overflow_2: i32 = bind_name no_overflow_2, %.loc34_35.2
  186. // CHECK:STDOUT: %LeftShift.ref.loc40: <function> = name_ref LeftShift, %LeftShift [template = %LeftShift]
  187. // CHECK:STDOUT: %.loc40_31: i32 = int_literal 1 [template = constants.%.1]
  188. // CHECK:STDOUT: %Negate.ref: <function> = name_ref Negate, %Negate [template = %Negate]
  189. // CHECK:STDOUT: %.loc40_41: i32 = int_literal 1 [template = constants.%.1]
  190. // CHECK:STDOUT: %.loc40_40.1: init i32 = call %Negate.ref(%.loc40_41) [template = constants.%.8]
  191. // CHECK:STDOUT: %.loc40_30.1: i32 = value_of_initializer %.loc40_40.1 [template = constants.%.8]
  192. // CHECK:STDOUT: %.loc40_40.2: i32 = converted %.loc40_40.1, %.loc40_30.1 [template = constants.%.8]
  193. // CHECK:STDOUT: %.loc40_30.2: init i32 = call %LeftShift.ref.loc40(%.loc40_31, %.loc40_40.2) [template = <error>]
  194. // CHECK:STDOUT: %.loc40_44: i32 = value_of_initializer %.loc40_30.2 [template = <error>]
  195. // CHECK:STDOUT: %.loc40_30.3: i32 = converted %.loc40_30.2, %.loc40_44 [template = <error>]
  196. // CHECK:STDOUT: %negative: i32 = bind_name negative, %.loc40_30.3
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @LeftShift(%a: i32, %b: i32) -> i32 = "int.left_shift";
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: fn @Negate(%a: i32) -> i32 = "int.negate";
  202. // CHECK:STDOUT: