complement.carbon 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/complement.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/complement.carbon
  12. // --- int_complement.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Complement(a: i32) -> i32 = "int.complement";
  15. fn And(a: i32, b: i32) -> i32 = "int.and";
  16. var arr: array(i32, And(Complement(0x123456), 0xFFFFFF));
  17. let arr_p: array(i32, 0xEDCBA9)* = &arr;
  18. fn RuntimeCallIsValid(a: i32) -> i32 {
  19. //@dump-sem-ir-begin
  20. return Complement(a);
  21. //@dump-sem-ir-end
  22. }
  23. // --- literal.carbon
  24. library "[[@TEST_NAME]]";
  25. fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  26. class Expect(N:! Core.IntLiteral()) {}
  27. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  28. fn F() {
  29. Test(Complement(0)) as Expect(-1);
  30. Test(Complement(1)) as Expect(-2);
  31. Test(Complement(-1)) as Expect(0);
  32. Test(Complement(-0x7FFF_FFFF_FFFF_FFFF)) as Expect(0x7FFF_FFFF_FFFF_FFFE);
  33. Test(Complement(-0x8000_0000_0000_0000)) as Expect(0x7FFF_FFFF_FFFF_FFFF);
  34. Test(Complement(0x7FFF_FFFF_FFFF_FFFF)) as Expect(-0x8000_0000_0000_0000);
  35. Test(Complement(0x8000_0000_0000_0000)) as Expect(-0x8000_0000_0000_0001);
  36. }
  37. // --- fail_literal_runtime.carbon
  38. library "[[@TEST_NAME]]";
  39. fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  40. fn F(a: Core.IntLiteral()) -> Core.IntLiteral() {
  41. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE+7]]:10: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  42. // CHECK:STDERR: return Complement(a);
  43. // CHECK:STDERR: ^~~~~~~~~~~~~
  44. // CHECK:STDERR: fail_literal_runtime.carbon:[[@LINE-6]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  45. // CHECK:STDERR: fn Complement(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.complement";
  46. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. // CHECK:STDERR:
  48. return Complement(a);
  49. }
  50. // CHECK:STDOUT: --- int_complement.carbon
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: constants {
  53. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  54. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  55. // CHECK:STDOUT: %Complement.type: type = fn_type @Complement [concrete]
  56. // CHECK:STDOUT: %Complement: %Complement.type = struct_value () [concrete]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: imports {
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32) -> %i32 {
  63. // CHECK:STDOUT: !entry:
  64. // CHECK:STDOUT: %Complement.ref: %Complement.type = name_ref Complement, file.%Complement.decl [concrete = constants.%Complement]
  65. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  66. // CHECK:STDOUT: %Complement.call: init %i32 = call %Complement.ref(%a.ref)
  67. // CHECK:STDOUT: return %Complement.call to %return.param
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: fn @__global_init() {
  71. // CHECK:STDOUT: !entry:
  72. // CHECK:STDOUT: <elided>
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: