bitwise.carbon 2.0 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. package Core library "prelude/operators/bitwise";
  5. import library "prelude/types/int_literal";
  6. // Bit complement: `^a`.
  7. interface BitComplement {
  8. fn Op[self: Self]() -> Self;
  9. }
  10. // Bitwise AND: `a & b`.
  11. interface BitAnd {
  12. fn Op[self: Self](other: Self) -> Self;
  13. }
  14. // Bitwise AND with assignment: `a &= b`.
  15. interface BitAndAssign {
  16. fn Op[addr self: Self*](other: Self);
  17. }
  18. // Bitwise OR: `a | b`.
  19. interface BitOr {
  20. fn Op[self: Self](other: Self) -> Self;
  21. }
  22. // Bitwise OR with assignment: `a |= b`.
  23. interface BitOrAssign {
  24. fn Op[addr self: Self*](other: Self);
  25. }
  26. // Bitwise XOR: `a ^ b`.
  27. interface BitXor {
  28. fn Op[self: Self](other: Self) -> Self;
  29. }
  30. // Bitwise XOR with assignment: `a ^= b`.
  31. interface BitXorAssign {
  32. fn Op[addr self: Self*](other: Self);
  33. }
  34. // Left shift: `a << b`.
  35. interface LeftShift {
  36. fn Op[self: Self](other: Self) -> Self;
  37. }
  38. // Left shift with assignment: `a <<= b`.
  39. interface LeftShiftAssign {
  40. fn Op[addr self: Self*](other: Self);
  41. }
  42. // Right shift: `a >> b`.
  43. interface RightShift {
  44. fn Op[self: Self](other: Self) -> Self;
  45. }
  46. // Right shift with assignment: `a >>= b`.
  47. interface RightShiftAssign {
  48. fn Op[addr self: Self*](other: Self);
  49. }
  50. // Operations for IntLiteral. These need to be here because IntLiteral has no
  51. // associated library of its own.
  52. impl IntLiteral() as BitAnd {
  53. fn Op[self: Self](other: Self) -> Self = "int.and";
  54. }
  55. impl IntLiteral() as BitComplement {
  56. fn Op[self: Self]() -> Self = "int.complement";
  57. }
  58. impl IntLiteral() as BitOr {
  59. fn Op[self: Self](other: Self) -> Self = "int.or";
  60. }
  61. impl IntLiteral() as BitXor {
  62. fn Op[self: Self](other: Self) -> Self = "int.xor";
  63. }
  64. impl IntLiteral() as LeftShift {
  65. fn Op[self: Self](other: Self) -> Self = "int.left_shift";
  66. }
  67. impl IntLiteral() as RightShift {
  68. fn Op[self: Self](other: Self) -> Self = "int.right_shift";
  69. }