mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +01:00
Fixes integer builtins to produce the correct values (and not CHECK-fail) when used on integer literals. Also adds impls to the prelude to use the new builtins to perform operations on integer literals. Perhaps most importantly, this allows directly initializing `i32` values with negative numbers, as the negation operation on integer literals now works. For testing I've added tests for use of literals with one operator in each class (addition, multiplication, ordering, bitwise, etc) for which there are distinct rules or overflow behavior, rather than exhaustively testing all the combinations. This is aimed at finding a good tradeoff between maintainability of the tests and thorough test coverage. Also fixes lowering of heterogeneous shifts and comparisons. These are currently disabled when one of the operands is an integer literal, but we may want to allow that when the integer literal operand has a known constant value.
100 lines
2.0 KiB
Plaintext
100 lines
2.0 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";
|
|
|
|
// Addition: `a + b`.
|
|
interface Add {
|
|
fn Op[self: Self](other: Self) -> Self;
|
|
}
|
|
|
|
// Addition with assignment: `a += b`.
|
|
interface AddAssign {
|
|
fn Op[addr self: Self*](other: Self);
|
|
}
|
|
|
|
// Increment: `++a`.
|
|
interface Inc {
|
|
fn Op[addr self: Self*]();
|
|
}
|
|
|
|
// Negation: `-a`.
|
|
interface Negate {
|
|
fn Op[self: Self]() -> Self;
|
|
}
|
|
|
|
// Subtraction: `a - b`.
|
|
interface Sub {
|
|
fn Op[self: Self](other: Self) -> Self;
|
|
}
|
|
|
|
// Subtraction with assignment: `a -= b`.
|
|
interface SubAssign {
|
|
fn Op[addr self: Self*](other: Self);
|
|
}
|
|
|
|
// Decrement: `--a`.
|
|
interface Dec {
|
|
fn Op[addr self: Self*]();
|
|
}
|
|
|
|
// Multiplication: `a * b`.
|
|
interface Mul {
|
|
fn Op[self: Self](other: Self) -> Self;
|
|
}
|
|
|
|
// Multiplication with assignment: `a *= b`.
|
|
interface MulAssign {
|
|
fn Op[addr self: Self*](other: Self);
|
|
}
|
|
|
|
// Division: `a / b`.
|
|
interface Div {
|
|
fn Op[self: Self](other: Self) -> Self;
|
|
}
|
|
|
|
// Division with assignment: `a /= b`.
|
|
interface DivAssign {
|
|
fn Op[addr self: Self*](other: Self);
|
|
}
|
|
|
|
// Modulo: `a % b`.
|
|
interface Mod {
|
|
fn Op[self: Self](other: Self) -> Self;
|
|
}
|
|
|
|
// Modulo with assignment: `a %= b`.
|
|
interface ModAssign {
|
|
fn Op[addr self: Self*](other: Self);
|
|
}
|
|
|
|
|
|
// Operations for IntLiteral. These need to be here because IntLiteral has no
|
|
// associated library of its own.
|
|
impl IntLiteral() as Add {
|
|
fn Op[self: Self](other: Self) -> Self = "int.sadd";
|
|
}
|
|
|
|
impl IntLiteral() as Div {
|
|
fn Op[self: Self](other: Self) -> Self = "int.sdiv";
|
|
}
|
|
|
|
impl IntLiteral() as Mod {
|
|
fn Op[self: Self](other: Self) -> Self = "int.smod";
|
|
}
|
|
|
|
impl IntLiteral() as Mul {
|
|
fn Op[self: Self](other: Self) -> Self = "int.smul";
|
|
}
|
|
|
|
impl IntLiteral() as Negate {
|
|
fn Op[self: Self]() -> Self = "int.snegate";
|
|
}
|
|
|
|
impl IntLiteral() as Sub {
|
|
fn Op[self: Self](other: Self) -> Self = "int.ssub";
|
|
}
|