numeric_literals.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/basics/numeric_literals.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/basics/numeric_literals.carbon
  10. fn F() {
  11. // 8 and 9 trigger special behavior in APInt when mishandling signed versus
  12. // unsigned, so we pay extra attention to those.
  13. var ints: [i32; 6] = (
  14. 8,
  15. 9,
  16. 0x8,
  17. 0b1000,
  18. 2147483647,
  19. 0x7FFFFFFF,
  20. );
  21. var floats: [f64; 6] = (
  22. 0.9,
  23. 8.0,
  24. 80.0,
  25. 1.0e7,
  26. 1.0e8,
  27. 1.0e-8
  28. );
  29. }
  30. // CHECK:STDOUT: --- numeric_literals.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  34. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  35. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  36. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  37. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  38. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  39. // CHECK:STDOUT: %.2: Core.IntLiteral = int_value 6 [template]
  40. // CHECK:STDOUT: %.3: type = array_type %.2, %i32 [template]
  41. // CHECK:STDOUT: %.5: Core.IntLiteral = int_value 8 [template]
  42. // CHECK:STDOUT: %.6: Core.IntLiteral = int_value 9 [template]
  43. // CHECK:STDOUT: %.7: Core.IntLiteral = int_value 2147483647 [template]
  44. // CHECK:STDOUT: %tuple.type.1: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral, Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
  45. // CHECK:STDOUT: %.8: Core.IntLiteral = int_value 0 [template]
  46. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  47. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  48. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  49. // CHECK:STDOUT: %.32: <witness> = interface_witness (%Convert.14) [template]
  50. // CHECK:STDOUT: %.33: <bound method> = bound_method %.5, %Convert.14 [template]
  51. // CHECK:STDOUT: %.34: <specific function> = specific_function %.33, @Convert.2(%.1) [template]
  52. // CHECK:STDOUT: %.35: %i32 = int_value 8 [template]
  53. // CHECK:STDOUT: %.36: Core.IntLiteral = int_value 1 [template]
  54. // CHECK:STDOUT: %.37: <bound method> = bound_method %.6, %Convert.14 [template]
  55. // CHECK:STDOUT: %.38: <specific function> = specific_function %.37, @Convert.2(%.1) [template]
  56. // CHECK:STDOUT: %.39: %i32 = int_value 9 [template]
  57. // CHECK:STDOUT: %.40: Core.IntLiteral = int_value 2 [template]
  58. // CHECK:STDOUT: %.41: Core.IntLiteral = int_value 3 [template]
  59. // CHECK:STDOUT: %.42: Core.IntLiteral = int_value 4 [template]
  60. // CHECK:STDOUT: %.43: <bound method> = bound_method %.7, %Convert.14 [template]
  61. // CHECK:STDOUT: %.44: <specific function> = specific_function %.43, @Convert.2(%.1) [template]
  62. // CHECK:STDOUT: %.45: %i32 = int_value 2147483647 [template]
  63. // CHECK:STDOUT: %.46: Core.IntLiteral = int_value 5 [template]
  64. // CHECK:STDOUT: %array.1: %.3 = tuple_value (%.35, %.39, %.35, %.35, %.45, %.45) [template]
  65. // CHECK:STDOUT: %.47: Core.IntLiteral = int_value 64 [template]
  66. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  67. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  68. // CHECK:STDOUT: %.48: type = array_type %.2, f64 [template]
  69. // CHECK:STDOUT: %.50: f64 = float_literal 0.90000000000000002 [template]
  70. // CHECK:STDOUT: %.51: f64 = float_literal 8 [template]
  71. // CHECK:STDOUT: %.52: f64 = float_literal 80 [template]
  72. // CHECK:STDOUT: %.53: f64 = float_literal 1.0E+7 [template]
  73. // CHECK:STDOUT: %.54: f64 = float_literal 1.0E+8 [template]
  74. // CHECK:STDOUT: %.55: f64 = float_literal 1.0E-8 [template]
  75. // CHECK:STDOUT: %tuple.type.2: type = tuple_type (f64, f64, f64, f64, f64, f64) [template]
  76. // CHECK:STDOUT: %array.2: %.48 = tuple_value (%.50, %.51, %.52, %.53, %.54, %.55) [template]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: imports {
  80. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  81. // CHECK:STDOUT: .Int = %import_ref.1
  82. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  83. // CHECK:STDOUT: .Float = %import_ref.38
  84. // CHECK:STDOUT: import Core//prelude
  85. // CHECK:STDOUT: import Core//prelude/...
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  91. // CHECK:STDOUT: .Core = imports.%Core
  92. // CHECK:STDOUT: .F = %F.decl
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %Core.import = import Core
  95. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @F() {
  99. // CHECK:STDOUT: !entry:
  100. // CHECK:STDOUT: %.loc14_14.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  101. // CHECK:STDOUT: %int.make_type_signed: init type = call constants.%Int(%.loc14_14.1) [template = constants.%i32]
  102. // CHECK:STDOUT: %.loc14_19: Core.IntLiteral = int_value 6 [template = constants.%.2]
  103. // CHECK:STDOUT: %.loc14_14.2: type = value_of_initializer %int.make_type_signed [template = constants.%i32]
  104. // CHECK:STDOUT: %.loc14_14.3: type = converted %int.make_type_signed, %.loc14_14.2 [template = constants.%i32]
  105. // CHECK:STDOUT: %.loc14_20: type = array_type %.loc14_19, %i32 [template = constants.%.3]
  106. // CHECK:STDOUT: %ints.var: ref %.3 = var ints
  107. // CHECK:STDOUT: %ints: ref %.3 = bind_name ints, %ints.var
  108. // CHECK:STDOUT: %.loc15: Core.IntLiteral = int_value 8 [template = constants.%.5]
  109. // CHECK:STDOUT: %.loc16: Core.IntLiteral = int_value 9 [template = constants.%.6]
  110. // CHECK:STDOUT: %.loc17: Core.IntLiteral = int_value 8 [template = constants.%.5]
  111. // CHECK:STDOUT: %.loc18: Core.IntLiteral = int_value 8 [template = constants.%.5]
  112. // CHECK:STDOUT: %.loc19: Core.IntLiteral = int_value 2147483647 [template = constants.%.7]
  113. // CHECK:STDOUT: %.loc20: Core.IntLiteral = int_value 2147483647 [template = constants.%.7]
  114. // CHECK:STDOUT: %.loc21_3.1: %tuple.type.1 = tuple_literal (%.loc15, %.loc16, %.loc17, %.loc18, %.loc19, %.loc20)
  115. // CHECK:STDOUT: %.loc21_3.2: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  116. // CHECK:STDOUT: %.loc21_3.3: <bound method> = bound_method %.loc15, %.loc21_3.2 [template = constants.%.33]
  117. // CHECK:STDOUT: %.loc21_3.4: <specific function> = specific_function %.loc21_3.3, @Convert.2(constants.%.1) [template = constants.%.34]
  118. // CHECK:STDOUT: %int.convert_checked.loc21_3.1: init %i32 = call %.loc21_3.4(%.loc15) [template = constants.%.35]
  119. // CHECK:STDOUT: %.loc21_3.5: init %i32 = converted %.loc15, %int.convert_checked.loc21_3.1 [template = constants.%.35]
  120. // CHECK:STDOUT: %.loc21_3.6: Core.IntLiteral = int_value 0 [template = constants.%.8]
  121. // CHECK:STDOUT: %.loc21_3.7: ref %i32 = array_index %ints.var, %.loc21_3.6
  122. // CHECK:STDOUT: %.loc21_3.8: init %i32 = initialize_from %.loc21_3.5 to %.loc21_3.7 [template = constants.%.35]
  123. // CHECK:STDOUT: %.loc21_3.9: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  124. // CHECK:STDOUT: %.loc21_3.10: <bound method> = bound_method %.loc16, %.loc21_3.9 [template = constants.%.37]
  125. // CHECK:STDOUT: %.loc21_3.11: <specific function> = specific_function %.loc21_3.10, @Convert.2(constants.%.1) [template = constants.%.38]
  126. // CHECK:STDOUT: %int.convert_checked.loc21_3.2: init %i32 = call %.loc21_3.11(%.loc16) [template = constants.%.39]
  127. // CHECK:STDOUT: %.loc21_3.12: init %i32 = converted %.loc16, %int.convert_checked.loc21_3.2 [template = constants.%.39]
  128. // CHECK:STDOUT: %.loc21_3.13: Core.IntLiteral = int_value 1 [template = constants.%.36]
  129. // CHECK:STDOUT: %.loc21_3.14: ref %i32 = array_index %ints.var, %.loc21_3.13
  130. // CHECK:STDOUT: %.loc21_3.15: init %i32 = initialize_from %.loc21_3.12 to %.loc21_3.14 [template = constants.%.39]
  131. // CHECK:STDOUT: %.loc21_3.16: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  132. // CHECK:STDOUT: %.loc21_3.17: <bound method> = bound_method %.loc17, %.loc21_3.16 [template = constants.%.33]
  133. // CHECK:STDOUT: %.loc21_3.18: <specific function> = specific_function %.loc21_3.17, @Convert.2(constants.%.1) [template = constants.%.34]
  134. // CHECK:STDOUT: %int.convert_checked.loc21_3.3: init %i32 = call %.loc21_3.18(%.loc17) [template = constants.%.35]
  135. // CHECK:STDOUT: %.loc21_3.19: init %i32 = converted %.loc17, %int.convert_checked.loc21_3.3 [template = constants.%.35]
  136. // CHECK:STDOUT: %.loc21_3.20: Core.IntLiteral = int_value 2 [template = constants.%.40]
  137. // CHECK:STDOUT: %.loc21_3.21: ref %i32 = array_index %ints.var, %.loc21_3.20
  138. // CHECK:STDOUT: %.loc21_3.22: init %i32 = initialize_from %.loc21_3.19 to %.loc21_3.21 [template = constants.%.35]
  139. // CHECK:STDOUT: %.loc21_3.23: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  140. // CHECK:STDOUT: %.loc21_3.24: <bound method> = bound_method %.loc18, %.loc21_3.23 [template = constants.%.33]
  141. // CHECK:STDOUT: %.loc21_3.25: <specific function> = specific_function %.loc21_3.24, @Convert.2(constants.%.1) [template = constants.%.34]
  142. // CHECK:STDOUT: %int.convert_checked.loc21_3.4: init %i32 = call %.loc21_3.25(%.loc18) [template = constants.%.35]
  143. // CHECK:STDOUT: %.loc21_3.26: init %i32 = converted %.loc18, %int.convert_checked.loc21_3.4 [template = constants.%.35]
  144. // CHECK:STDOUT: %.loc21_3.27: Core.IntLiteral = int_value 3 [template = constants.%.41]
  145. // CHECK:STDOUT: %.loc21_3.28: ref %i32 = array_index %ints.var, %.loc21_3.27
  146. // CHECK:STDOUT: %.loc21_3.29: init %i32 = initialize_from %.loc21_3.26 to %.loc21_3.28 [template = constants.%.35]
  147. // CHECK:STDOUT: %.loc21_3.30: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  148. // CHECK:STDOUT: %.loc21_3.31: <bound method> = bound_method %.loc19, %.loc21_3.30 [template = constants.%.43]
  149. // CHECK:STDOUT: %.loc21_3.32: <specific function> = specific_function %.loc21_3.31, @Convert.2(constants.%.1) [template = constants.%.44]
  150. // CHECK:STDOUT: %int.convert_checked.loc21_3.5: init %i32 = call %.loc21_3.32(%.loc19) [template = constants.%.45]
  151. // CHECK:STDOUT: %.loc21_3.33: init %i32 = converted %.loc19, %int.convert_checked.loc21_3.5 [template = constants.%.45]
  152. // CHECK:STDOUT: %.loc21_3.34: Core.IntLiteral = int_value 4 [template = constants.%.42]
  153. // CHECK:STDOUT: %.loc21_3.35: ref %i32 = array_index %ints.var, %.loc21_3.34
  154. // CHECK:STDOUT: %.loc21_3.36: init %i32 = initialize_from %.loc21_3.33 to %.loc21_3.35 [template = constants.%.45]
  155. // CHECK:STDOUT: %.loc21_3.37: %Convert.type.2 = interface_witness_access constants.%.32, element0 [template = constants.%Convert.14]
  156. // CHECK:STDOUT: %.loc21_3.38: <bound method> = bound_method %.loc20, %.loc21_3.37 [template = constants.%.43]
  157. // CHECK:STDOUT: %.loc21_3.39: <specific function> = specific_function %.loc21_3.38, @Convert.2(constants.%.1) [template = constants.%.44]
  158. // CHECK:STDOUT: %int.convert_checked.loc21_3.6: init %i32 = call %.loc21_3.39(%.loc20) [template = constants.%.45]
  159. // CHECK:STDOUT: %.loc21_3.40: init %i32 = converted %.loc20, %int.convert_checked.loc21_3.6 [template = constants.%.45]
  160. // CHECK:STDOUT: %.loc21_3.41: Core.IntLiteral = int_value 5 [template = constants.%.46]
  161. // CHECK:STDOUT: %.loc21_3.42: ref %i32 = array_index %ints.var, %.loc21_3.41
  162. // CHECK:STDOUT: %.loc21_3.43: init %i32 = initialize_from %.loc21_3.40 to %.loc21_3.42 [template = constants.%.45]
  163. // CHECK:STDOUT: %.loc21_3.44: init %.3 = array_init (%.loc21_3.8, %.loc21_3.15, %.loc21_3.22, %.loc21_3.29, %.loc21_3.36, %.loc21_3.43) to %ints.var [template = constants.%array.1]
  164. // CHECK:STDOUT: %.loc21_4: init %.3 = converted %.loc21_3.1, %.loc21_3.44 [template = constants.%array.1]
  165. // CHECK:STDOUT: assign %ints.var, %.loc21_4
  166. // CHECK:STDOUT: %.loc22_16.1: Core.IntLiteral = int_value 64 [template = constants.%.47]
  167. // CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc22_16.1) [template = f64]
  168. // CHECK:STDOUT: %.loc22_21: Core.IntLiteral = int_value 6 [template = constants.%.2]
  169. // CHECK:STDOUT: %.loc22_16.2: type = value_of_initializer %float.make_type [template = f64]
  170. // CHECK:STDOUT: %.loc22_16.3: type = converted %float.make_type, %.loc22_16.2 [template = f64]
  171. // CHECK:STDOUT: %.loc22_22: type = array_type %.loc22_21, f64 [template = constants.%.48]
  172. // CHECK:STDOUT: %floats.var: ref %.48 = var floats
  173. // CHECK:STDOUT: %floats: ref %.48 = bind_name floats, %floats.var
  174. // CHECK:STDOUT: %.loc23: f64 = float_literal 0.90000000000000002 [template = constants.%.50]
  175. // CHECK:STDOUT: %.loc24: f64 = float_literal 8 [template = constants.%.51]
  176. // CHECK:STDOUT: %.loc25: f64 = float_literal 80 [template = constants.%.52]
  177. // CHECK:STDOUT: %.loc26: f64 = float_literal 1.0E+7 [template = constants.%.53]
  178. // CHECK:STDOUT: %.loc27: f64 = float_literal 1.0E+8 [template = constants.%.54]
  179. // CHECK:STDOUT: %.loc28: f64 = float_literal 1.0E-8 [template = constants.%.55]
  180. // CHECK:STDOUT: %.loc29_3.1: %tuple.type.2 = tuple_literal (%.loc23, %.loc24, %.loc25, %.loc26, %.loc27, %.loc28)
  181. // CHECK:STDOUT: %.loc29_3.2: Core.IntLiteral = int_value 0 [template = constants.%.8]
  182. // CHECK:STDOUT: %.loc29_3.3: ref f64 = array_index %floats.var, %.loc29_3.2
  183. // CHECK:STDOUT: %.loc29_3.4: init f64 = initialize_from %.loc23 to %.loc29_3.3 [template = constants.%.50]
  184. // CHECK:STDOUT: %.loc29_3.5: Core.IntLiteral = int_value 1 [template = constants.%.36]
  185. // CHECK:STDOUT: %.loc29_3.6: ref f64 = array_index %floats.var, %.loc29_3.5
  186. // CHECK:STDOUT: %.loc29_3.7: init f64 = initialize_from %.loc24 to %.loc29_3.6 [template = constants.%.51]
  187. // CHECK:STDOUT: %.loc29_3.8: Core.IntLiteral = int_value 2 [template = constants.%.40]
  188. // CHECK:STDOUT: %.loc29_3.9: ref f64 = array_index %floats.var, %.loc29_3.8
  189. // CHECK:STDOUT: %.loc29_3.10: init f64 = initialize_from %.loc25 to %.loc29_3.9 [template = constants.%.52]
  190. // CHECK:STDOUT: %.loc29_3.11: Core.IntLiteral = int_value 3 [template = constants.%.41]
  191. // CHECK:STDOUT: %.loc29_3.12: ref f64 = array_index %floats.var, %.loc29_3.11
  192. // CHECK:STDOUT: %.loc29_3.13: init f64 = initialize_from %.loc26 to %.loc29_3.12 [template = constants.%.53]
  193. // CHECK:STDOUT: %.loc29_3.14: Core.IntLiteral = int_value 4 [template = constants.%.42]
  194. // CHECK:STDOUT: %.loc29_3.15: ref f64 = array_index %floats.var, %.loc29_3.14
  195. // CHECK:STDOUT: %.loc29_3.16: init f64 = initialize_from %.loc27 to %.loc29_3.15 [template = constants.%.54]
  196. // CHECK:STDOUT: %.loc29_3.17: Core.IntLiteral = int_value 5 [template = constants.%.46]
  197. // CHECK:STDOUT: %.loc29_3.18: ref f64 = array_index %floats.var, %.loc29_3.17
  198. // CHECK:STDOUT: %.loc29_3.19: init f64 = initialize_from %.loc28 to %.loc29_3.18 [template = constants.%.55]
  199. // CHECK:STDOUT: %.loc29_3.20: init %.48 = array_init (%.loc29_3.4, %.loc29_3.7, %.loc29_3.10, %.loc29_3.13, %.loc29_3.16, %.loc29_3.19) to %floats.var [template = constants.%array.2]
  200. // CHECK:STDOUT: %.loc29_4: init %.48 = converted %.loc29_3.1, %.loc29_3.20 [template = constants.%array.2]
  201. // CHECK:STDOUT: assign %floats.var, %.loc29_4
  202. // CHECK:STDOUT: return
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: