Files
carbon-lang/core/prelude/operators/arithmetic.carbon
T
Richard Smith 344f0b7550 Support for heterogeneous operators. (#5653)
Replace the binary `Operation` interfaces with the `OperationWith(T:!
type)` interfaces described in the design, and add a `Result` associated
type for both unary and binary operations. Update the `impl`s in the
prelude for integer types to use the new form, including supporting
implicit conversion of either operand.

I've tried to split this PR up into commits focused on distinct changes
for review convenience. It may be simplest to review it one commit at a
time.
2025-06-13 01:55:24 +00:00

118 lines
2.9 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
package Core library "prelude/operators/arithmetic";
import library "prelude/types/int_literal";
// TODO: Per the design, the associated type `Result` in each of these
// interfaces should have a default value of `Self`:
//
// default let Result:! type = Self;
// TODO: Per the design, for each *With interface there should also be a
// non-With named constraint, such as:
//
// constraint Add {
// extend require impls AddWith(Self) where .Result = Self;
// }
// Addition: `a + b`.
interface AddWith(Other:! type) {
let Result:! type;
fn Op[self: Self](other: Other) -> Result;
}
// Addition with assignment: `a += b`.
interface AddAssignWith(Other:! type) {
fn Op[addr self: Self*](other: Other);
}
// Increment: `++a`.
interface Inc {
fn Op[addr self: Self*]();
}
// Negation: `-a`.
interface Negate {
let Result:! type;
fn Op[self: Self]() -> Result;
}
// Subtraction: `a - b`.
interface SubWith(Other:! type) {
let Result:! type;
fn Op[self: Self](other: Other) -> Result;
}
// Subtraction with assignment: `a -= b`.
interface SubAssignWith(Other:! type) {
fn Op[addr self: Self*](other: Other);
}
// Decrement: `--a`.
interface Dec {
fn Op[addr self: Self*]();
}
// Multiplication: `a * b`.
interface MulWith(Other:! type) {
let Result:! type;
fn Op[self: Self](other: Other) -> Result;
}
// Multiplication with assignment: `a *= b`.
interface MulAssignWith(Other:! type) {
fn Op[addr self: Self*](other: Other);
}
// Division: `a / b`.
interface DivWith(Other:! type) {
let Result:! type;
fn Op[self: Self](other: Other) -> Result;
}
// Division with assignment: `a /= b`.
interface DivAssignWith(Other:! type) {
fn Op[addr self: Self*](other: Other);
}
// Modulo: `a % b`.
interface ModWith(Other:! type) {
let Result:! type;
fn Op[self: Self](other: Other) -> Result;
}
// Modulo with assignment: `a %= b`.
interface ModAssignWith(Other:! type) {
fn Op[addr self: Self*](other: Other);
}
// Operations for IntLiteral. These need to be here because IntLiteral has no
// associated library of its own.
impl IntLiteral() as AddWith(Self) where .Result = Self {
fn Op[self: Self](other: Self) -> Self = "int.sadd";
}
impl IntLiteral() as DivWith(Self) where .Result = Self {
fn Op[self: Self](other: Self) -> Self = "int.sdiv";
}
impl IntLiteral() as ModWith(Self) where .Result = Self {
fn Op[self: Self](other: Self) -> Self = "int.smod";
}
impl IntLiteral() as MulWith(Self) where .Result = Self {
fn Op[self: Self](other: Self) -> Self = "int.smul";
}
impl IntLiteral() as Negate where .Result = Self {
fn Op[self: Self]() -> Self = "int.snegate";
}
impl IntLiteral() as SubWith(Self) where .Result = Self {
fn Op[self: Self](other: Self) -> Self = "int.ssub";
}