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