arithmetic.carbon 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/arithmetic";
  5. import library "prelude/types/int_literal";
  6. // Addition: `a + b`.
  7. interface Add {
  8. fn Op[self: Self](other: Self) -> Self;
  9. }
  10. // Addition with assignment: `a += b`.
  11. interface AddAssign {
  12. fn Op[addr self: Self*](other: Self);
  13. }
  14. // Increment: `++a`.
  15. interface Inc {
  16. fn Op[addr self: Self*]();
  17. }
  18. // Negation: `-a`.
  19. interface Negate {
  20. fn Op[self: Self]() -> Self;
  21. }
  22. // Subtraction: `a - b`.
  23. interface Sub {
  24. fn Op[self: Self](other: Self) -> Self;
  25. }
  26. // Subtraction with assignment: `a -= b`.
  27. interface SubAssign {
  28. fn Op[addr self: Self*](other: Self);
  29. }
  30. // Decrement: `--a`.
  31. interface Dec {
  32. fn Op[addr self: Self*]();
  33. }
  34. // Multiplication: `a * b`.
  35. interface Mul {
  36. fn Op[self: Self](other: Self) -> Self;
  37. }
  38. // Multiplication with assignment: `a *= b`.
  39. interface MulAssign {
  40. fn Op[addr self: Self*](other: Self);
  41. }
  42. // Division: `a / b`.
  43. interface Div {
  44. fn Op[self: Self](other: Self) -> Self;
  45. }
  46. // Division with assignment: `a /= b`.
  47. interface DivAssign {
  48. fn Op[addr self: Self*](other: Self);
  49. }
  50. // Modulo: `a % b`.
  51. interface Mod {
  52. fn Op[self: Self](other: Self) -> Self;
  53. }
  54. // Modulo with assignment: `a %= b`.
  55. interface ModAssign {
  56. fn Op[addr self: Self*](other: Self);
  57. }
  58. // Operations for IntLiteral. These need to be here because IntLiteral has no
  59. // associated library of its own.
  60. impl IntLiteral() as Add {
  61. fn Op[self: Self](other: Self) -> Self = "int.sadd";
  62. }
  63. impl IntLiteral() as Div {
  64. fn Op[self: Self](other: Self) -> Self = "int.sdiv";
  65. }
  66. impl IntLiteral() as Mod {
  67. fn Op[self: Self](other: Self) -> Self = "int.smod";
  68. }
  69. impl IntLiteral() as Mul {
  70. fn Op[self: Self](other: Self) -> Self = "int.smul";
  71. }
  72. impl IntLiteral() as Negate {
  73. fn Op[self: Self]() -> Self = "int.snegate";
  74. }
  75. impl IntLiteral() as Sub {
  76. fn Op[self: Self](other: Self) -> Self = "int.ssub";
  77. }