and.carbon 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/and.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/and.carbon
  12. // --- int_and.carbon
  13. library "[[@TEST_NAME]]";
  14. fn And(a: i32, b: i32) -> i32 = "int.and";
  15. var arr: array(i32, And(12, 10));
  16. let arr_p: array(i32, 8)* = &arr;
  17. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  18. //@dump-sem-ir-begin
  19. return And(a, b);
  20. //@dump-sem-ir-end
  21. }
  22. // --- literal.carbon
  23. library "[[@TEST_NAME]]";
  24. fn And(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  25. class Expect(N:! Core.IntLiteral()) {}
  26. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  27. fn F() {
  28. Test(And(1, 2)) as Expect(0);
  29. Test(And(12, 10)) as Expect(8);
  30. Test(And(1, -1)) as Expect(1);
  31. Test(And(-2, -3)) as Expect(-4);
  32. // Ensure the sign bit is treated properly even for 64-bit numbers.
  33. Test(And(0x7FFF_FFFF_FFFF_FFFF, -3)) as Expect(0x7FFF_FFFF_FFFF_FFFD);
  34. Test(And(0x8000_0000_0000_0000, -1)) as Expect(0x8000_0000_0000_0000);
  35. }
  36. // --- fail_literal_runtime.carbon
  37. library "[[@TEST_NAME]]";
  38. fn AndLit(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  39. fn F(a: Core.IntLiteral()) -> Core.IntLiteral() {
  40. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE+7]]:10: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  41. // CHECK:STDERR: return AndLit(5, a);
  42. // CHECK:STDERR: ^~~~~~~~~~~~
  43. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE-6]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  44. // CHECK:STDERR: fn AndLit(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  45. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  46. // CHECK:STDERR:
  47. return AndLit(5, a);
  48. }
  49. // --- fail_bad_decl.carbon
  50. library "[[@TEST_NAME]]";
  51. // Heterogeneous "and" is not supported.
  52. // CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "int.and" [InvalidBuiltinSignature]
  53. // CHECK:STDERR: fn MixedAnd1(a: i32, b: Core.IntLiteral()) -> i32 = "int.and";
  54. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // CHECK:STDERR:
  56. fn MixedAnd1(a: i32, b: Core.IntLiteral()) -> i32 = "int.and";
  57. // CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "int.and" [InvalidBuiltinSignature]
  58. // CHECK:STDERR: fn MixedAnd2(a: Core.IntLiteral(), b: i32) -> i32 = "int.and";
  59. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. // CHECK:STDERR:
  61. fn MixedAnd2(a: Core.IntLiteral(), b: i32) -> i32 = "int.and";
  62. // CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "int.and" [InvalidBuiltinSignature]
  63. // CHECK:STDERR: fn MixedAnd3(a: i32, b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR:
  66. fn MixedAnd3(a: i32, b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  67. // CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: error: invalid signature for builtin function "int.and" [InvalidBuiltinSignature]
  68. // CHECK:STDERR: fn MixedAnd4(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.and";
  69. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. // CHECK:STDERR:
  71. fn MixedAnd4(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.and";
  72. // --- fail_runtime_literal.carbon
  73. library "[[@TEST_NAME]]";
  74. fn And(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  75. fn Test(n: Core.IntLiteral()) {
  76. // OK
  77. And(1, 1);
  78. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  79. // CHECK:STDERR: And(n, 1);
  80. // CHECK:STDERR: ^~~~~~~~~
  81. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-8]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  82. // CHECK:STDERR: fn And(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  83. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. // CHECK:STDERR:
  85. And(n, 1);
  86. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  87. // CHECK:STDERR: And(1, n);
  88. // CHECK:STDERR: ^~~~~~~~~
  89. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-16]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  90. // CHECK:STDERR: fn And(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  91. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. // CHECK:STDERR:
  93. And(1, n);
  94. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  95. // CHECK:STDERR: And(n, n);
  96. // CHECK:STDERR: ^~~~~~~~~
  97. // CHECK:STDERR: fail_runtime_literal.carbon:[[@LINE-24]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  98. // CHECK:STDERR: fn And(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.and";
  99. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. // CHECK:STDERR:
  101. And(n, n);
  102. }
  103. // CHECK:STDOUT: --- int_and.carbon
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: constants {
  106. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  107. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  108. // CHECK:STDOUT: %And.type: type = fn_type @And [concrete]
  109. // CHECK:STDOUT: %And: %And.type = struct_value () [concrete]
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: imports {
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> %i32 {
  116. // CHECK:STDOUT: !entry:
  117. // CHECK:STDOUT: %And.ref: %And.type = name_ref And, file.%And.decl [concrete = constants.%And]
  118. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  119. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  120. // CHECK:STDOUT: %And.call: init %i32 = call %And.ref(%a.ref, %b.ref)
  121. // CHECK:STDOUT: %.loc11_19.1: %i32 = value_of_initializer %And.call
  122. // CHECK:STDOUT: %.loc11_19.2: %i32 = converted %And.call, %.loc11_19.1
  123. // CHECK:STDOUT: return %.loc11_19.2
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @__global_init() {
  127. // CHECK:STDOUT: !entry:
  128. // CHECK:STDOUT: <elided>
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: