bitwise.carbon 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // TODO: Per the design, the associated type `Result` in each of these
  7. // interfaces should have a default value of `Self`:
  8. //
  9. // default let Result:! type = Self;
  10. // TODO: Per the design, for each *With interface there should also be a
  11. // non-With named constraint, such as:
  12. //
  13. // constraint BitAnd {
  14. // extend require impls BitAndWith(Self) where .Result = Self;
  15. // }
  16. // Bit complement: `^a`.
  17. interface BitComplement {
  18. let Result:! type;
  19. fn Op[self: Self]() -> Result;
  20. }
  21. // Bitwise AND: `a & b`.
  22. interface BitAndWith(Other:! type) {
  23. let Result:! type;
  24. fn Op[self: Self](other: Other) -> Result;
  25. }
  26. // Bitwise AND with assignment: `a &= b`.
  27. interface BitAndAssignWith(Other:! type) {
  28. fn Op[ref self: Self](other: Other);
  29. }
  30. // Bitwise OR: `a | b`.
  31. interface BitOrWith(Other:! type) {
  32. let Result:! type;
  33. fn Op[self: Self](other: Other) -> Result;
  34. }
  35. // Bitwise OR with assignment: `a |= b`.
  36. interface BitOrAssignWith(Other:! type) {
  37. fn Op[ref self: Self](other: Other);
  38. }
  39. // Bitwise XOR: `a ^ b`.
  40. interface BitXorWith(Other:! type) {
  41. let Result:! type;
  42. fn Op[self: Self](other: Other) -> Result;
  43. }
  44. // Bitwise XOR with assignment: `a ^= b`.
  45. interface BitXorAssignWith(Other:! type) {
  46. fn Op[ref self: Self](other: Other);
  47. }
  48. // Left shift: `a << b`.
  49. interface LeftShiftWith(Other:! type) {
  50. let Result:! type;
  51. fn Op[self: Self](other: Other) -> Result;
  52. }
  53. // Left shift with assignment: `a <<= b`.
  54. interface LeftShiftAssignWith(Other:! type) {
  55. fn Op[ref self: Self](other: Other);
  56. }
  57. // Right shift: `a >> b`.
  58. interface RightShiftWith(Other:! type) {
  59. let Result:! type;
  60. fn Op[self: Self](other: Other) -> Result;
  61. }
  62. // Right shift with assignment: `a >>= b`.
  63. interface RightShiftAssignWith(Other:! type) {
  64. fn Op[ref self: Self](other: Other);
  65. }
  66. // Operations for IntLiteral. These need to be here because IntLiteral has no
  67. // associated library of its own.
  68. impl IntLiteral() as BitAndWith(Self) where .Result = Self {
  69. fn Op[self: Self](other: Self) -> Self = "int.and";
  70. }
  71. impl IntLiteral() as BitComplement where .Result = Self {
  72. fn Op[self: Self]() -> Self = "int.complement";
  73. }
  74. impl IntLiteral() as BitOrWith(Self) where .Result = Self {
  75. fn Op[self: Self](other: Self) -> Self = "int.or";
  76. }
  77. impl IntLiteral() as BitXorWith(Self) where .Result = Self {
  78. fn Op[self: Self](other: Self) -> Self = "int.xor";
  79. }
  80. impl IntLiteral() as LeftShiftWith(Self) where .Result = Self {
  81. fn Op[self: Self](other: Self) -> Self = "int.left_shift";
  82. }
  83. impl IntLiteral() as RightShiftWith(Self) where .Result = Self {
  84. fn Op[self: Self](other: Self) -> Self = "int.right_shift";
  85. }
  86. // Operations for `type`. These need to be here because `type` has no
  87. // associated library of its own.
  88. // Facet type combination.
  89. impl type as BitAndWith(Self) where .Result = Self {
  90. fn Op[self: Self](other: Self) -> Self = "type.and";
  91. }