numeric_literals.carbon 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: %.1: type = tuple_type () [template]
  35. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  36. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  37. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  38. // CHECK:STDOUT: %.2: i32 = int_literal 6 [template]
  39. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  40. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  41. // CHECK:STDOUT: %.5: i32 = int_literal 8 [template]
  42. // CHECK:STDOUT: %.6: i32 = int_literal 9 [template]
  43. // CHECK:STDOUT: %.7: i32 = int_literal 2147483647 [template]
  44. // CHECK:STDOUT: %.8: type = tuple_type (i32, i32, i32, i32, i32, i32) [template]
  45. // CHECK:STDOUT: %.9: i32 = int_literal 0 [template]
  46. // CHECK:STDOUT: %.10: i32 = int_literal 1 [template]
  47. // CHECK:STDOUT: %.11: i32 = int_literal 2 [template]
  48. // CHECK:STDOUT: %.12: i32 = int_literal 3 [template]
  49. // CHECK:STDOUT: %.13: i32 = int_literal 4 [template]
  50. // CHECK:STDOUT: %.14: i32 = int_literal 5 [template]
  51. // CHECK:STDOUT: %array.1: %.3 = tuple_value (%.5, %.6, %.5, %.5, %.7, %.7) [template]
  52. // CHECK:STDOUT: %.15: i32 = int_literal 64 [template]
  53. // CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
  54. // CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
  55. // CHECK:STDOUT: %.16: type = array_type %.2, f64 [template]
  56. // CHECK:STDOUT: %.17: type = ptr_type %.16 [template]
  57. // CHECK:STDOUT: %.18: f64 = float_literal 0.90000000000000002 [template]
  58. // CHECK:STDOUT: %.19: f64 = float_literal 8 [template]
  59. // CHECK:STDOUT: %.20: f64 = float_literal 80 [template]
  60. // CHECK:STDOUT: %.21: f64 = float_literal 1.0E+7 [template]
  61. // CHECK:STDOUT: %.22: f64 = float_literal 1.0E+8 [template]
  62. // CHECK:STDOUT: %.23: f64 = float_literal 1.0E-8 [template]
  63. // CHECK:STDOUT: %.24: type = tuple_type (f64, f64, f64, f64, f64, f64) [template]
  64. // CHECK:STDOUT: %array.2: %.16 = tuple_value (%.18, %.19, %.20, %.21, %.22, %.23) [template]
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: file {
  68. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  69. // CHECK:STDOUT: .Core = %Core
  70. // CHECK:STDOUT: .F = %F.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  73. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {}
  74. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  75. // CHECK:STDOUT: %import_ref.2: %Float.type = import_ref ir3, inst+31, loaded [template = constants.%Float]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: fn @F() {
  79. // CHECK:STDOUT: !entry:
  80. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc14_19: i32 = int_literal 6 [template = constants.%.2]
  82. // CHECK:STDOUT: %.loc14_14.1: type = value_of_initializer %int.make_type_32 [template = i32]
  83. // CHECK:STDOUT: %.loc14_14.2: type = converted %int.make_type_32, %.loc14_14.1 [template = i32]
  84. // CHECK:STDOUT: %.loc14_20: type = array_type %.loc14_19, i32 [template = constants.%.3]
  85. // CHECK:STDOUT: %ints.var: ref %.3 = var ints
  86. // CHECK:STDOUT: %ints: ref %.3 = bind_name ints, %ints.var
  87. // CHECK:STDOUT: %.loc15: i32 = int_literal 8 [template = constants.%.5]
  88. // CHECK:STDOUT: %.loc16: i32 = int_literal 9 [template = constants.%.6]
  89. // CHECK:STDOUT: %.loc17: i32 = int_literal 8 [template = constants.%.5]
  90. // CHECK:STDOUT: %.loc18: i32 = int_literal 8 [template = constants.%.5]
  91. // CHECK:STDOUT: %.loc19: i32 = int_literal 2147483647 [template = constants.%.7]
  92. // CHECK:STDOUT: %.loc20: i32 = int_literal 2147483647 [template = constants.%.7]
  93. // CHECK:STDOUT: %.loc21_3.1: %.8 = tuple_literal (%.loc15, %.loc16, %.loc17, %.loc18, %.loc19, %.loc20)
  94. // CHECK:STDOUT: %.loc21_3.2: i32 = int_literal 0 [template = constants.%.9]
  95. // CHECK:STDOUT: %.loc21_3.3: ref i32 = array_index %ints.var, %.loc21_3.2
  96. // CHECK:STDOUT: %.loc21_3.4: init i32 = initialize_from %.loc15 to %.loc21_3.3 [template = constants.%.5]
  97. // CHECK:STDOUT: %.loc21_3.5: i32 = int_literal 1 [template = constants.%.10]
  98. // CHECK:STDOUT: %.loc21_3.6: ref i32 = array_index %ints.var, %.loc21_3.5
  99. // CHECK:STDOUT: %.loc21_3.7: init i32 = initialize_from %.loc16 to %.loc21_3.6 [template = constants.%.6]
  100. // CHECK:STDOUT: %.loc21_3.8: i32 = int_literal 2 [template = constants.%.11]
  101. // CHECK:STDOUT: %.loc21_3.9: ref i32 = array_index %ints.var, %.loc21_3.8
  102. // CHECK:STDOUT: %.loc21_3.10: init i32 = initialize_from %.loc17 to %.loc21_3.9 [template = constants.%.5]
  103. // CHECK:STDOUT: %.loc21_3.11: i32 = int_literal 3 [template = constants.%.12]
  104. // CHECK:STDOUT: %.loc21_3.12: ref i32 = array_index %ints.var, %.loc21_3.11
  105. // CHECK:STDOUT: %.loc21_3.13: init i32 = initialize_from %.loc18 to %.loc21_3.12 [template = constants.%.5]
  106. // CHECK:STDOUT: %.loc21_3.14: i32 = int_literal 4 [template = constants.%.13]
  107. // CHECK:STDOUT: %.loc21_3.15: ref i32 = array_index %ints.var, %.loc21_3.14
  108. // CHECK:STDOUT: %.loc21_3.16: init i32 = initialize_from %.loc19 to %.loc21_3.15 [template = constants.%.7]
  109. // CHECK:STDOUT: %.loc21_3.17: i32 = int_literal 5 [template = constants.%.14]
  110. // CHECK:STDOUT: %.loc21_3.18: ref i32 = array_index %ints.var, %.loc21_3.17
  111. // CHECK:STDOUT: %.loc21_3.19: init i32 = initialize_from %.loc20 to %.loc21_3.18 [template = constants.%.7]
  112. // CHECK:STDOUT: %.loc21_3.20: init %.3 = array_init (%.loc21_3.4, %.loc21_3.7, %.loc21_3.10, %.loc21_3.13, %.loc21_3.16, %.loc21_3.19) to %ints.var [template = constants.%array.1]
  113. // CHECK:STDOUT: %.loc21_4: init %.3 = converted %.loc21_3.1, %.loc21_3.20 [template = constants.%array.1]
  114. // CHECK:STDOUT: assign %ints.var, %.loc21_4
  115. // CHECK:STDOUT: %.loc22_16.1: i32 = int_literal 64 [template = constants.%.15]
  116. // CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc22_16.1) [template = f64]
  117. // CHECK:STDOUT: %.loc22_21: i32 = int_literal 6 [template = constants.%.2]
  118. // CHECK:STDOUT: %.loc22_16.2: type = value_of_initializer %float.make_type [template = f64]
  119. // CHECK:STDOUT: %.loc22_16.3: type = converted %float.make_type, %.loc22_16.2 [template = f64]
  120. // CHECK:STDOUT: %.loc22_22: type = array_type %.loc22_21, f64 [template = constants.%.16]
  121. // CHECK:STDOUT: %floats.var: ref %.16 = var floats
  122. // CHECK:STDOUT: %floats: ref %.16 = bind_name floats, %floats.var
  123. // CHECK:STDOUT: %.loc23: f64 = float_literal 0.90000000000000002 [template = constants.%.18]
  124. // CHECK:STDOUT: %.loc24: f64 = float_literal 8 [template = constants.%.19]
  125. // CHECK:STDOUT: %.loc25: f64 = float_literal 80 [template = constants.%.20]
  126. // CHECK:STDOUT: %.loc26: f64 = float_literal 1.0E+7 [template = constants.%.21]
  127. // CHECK:STDOUT: %.loc27: f64 = float_literal 1.0E+8 [template = constants.%.22]
  128. // CHECK:STDOUT: %.loc28: f64 = float_literal 1.0E-8 [template = constants.%.23]
  129. // CHECK:STDOUT: %.loc29_3.1: %.24 = tuple_literal (%.loc23, %.loc24, %.loc25, %.loc26, %.loc27, %.loc28)
  130. // CHECK:STDOUT: %.loc29_3.2: i32 = int_literal 0 [template = constants.%.9]
  131. // CHECK:STDOUT: %.loc29_3.3: ref f64 = array_index %floats.var, %.loc29_3.2
  132. // CHECK:STDOUT: %.loc29_3.4: init f64 = initialize_from %.loc23 to %.loc29_3.3 [template = constants.%.18]
  133. // CHECK:STDOUT: %.loc29_3.5: i32 = int_literal 1 [template = constants.%.10]
  134. // CHECK:STDOUT: %.loc29_3.6: ref f64 = array_index %floats.var, %.loc29_3.5
  135. // CHECK:STDOUT: %.loc29_3.7: init f64 = initialize_from %.loc24 to %.loc29_3.6 [template = constants.%.19]
  136. // CHECK:STDOUT: %.loc29_3.8: i32 = int_literal 2 [template = constants.%.11]
  137. // CHECK:STDOUT: %.loc29_3.9: ref f64 = array_index %floats.var, %.loc29_3.8
  138. // CHECK:STDOUT: %.loc29_3.10: init f64 = initialize_from %.loc25 to %.loc29_3.9 [template = constants.%.20]
  139. // CHECK:STDOUT: %.loc29_3.11: i32 = int_literal 3 [template = constants.%.12]
  140. // CHECK:STDOUT: %.loc29_3.12: ref f64 = array_index %floats.var, %.loc29_3.11
  141. // CHECK:STDOUT: %.loc29_3.13: init f64 = initialize_from %.loc26 to %.loc29_3.12 [template = constants.%.21]
  142. // CHECK:STDOUT: %.loc29_3.14: i32 = int_literal 4 [template = constants.%.13]
  143. // CHECK:STDOUT: %.loc29_3.15: ref f64 = array_index %floats.var, %.loc29_3.14
  144. // CHECK:STDOUT: %.loc29_3.16: init f64 = initialize_from %.loc27 to %.loc29_3.15 [template = constants.%.22]
  145. // CHECK:STDOUT: %.loc29_3.17: i32 = int_literal 5 [template = constants.%.14]
  146. // CHECK:STDOUT: %.loc29_3.18: ref f64 = array_index %floats.var, %.loc29_3.17
  147. // CHECK:STDOUT: %.loc29_3.19: init f64 = initialize_from %.loc28 to %.loc29_3.18 [template = constants.%.23]
  148. // CHECK:STDOUT: %.loc29_3.20: init %.16 = 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]
  149. // CHECK:STDOUT: %.loc29_4: init %.16 = converted %.loc29_3.1, %.loc29_3.20 [template = constants.%array.2]
  150. // CHECK:STDOUT: assign %floats.var, %.loc29_4
  151. // CHECK:STDOUT: return
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @Float(%size: i32) -> type = "float.make_type";
  157. // CHECK:STDOUT: