mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 16:32:41 +00: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.
48 lines
1.6 KiB
Plaintext
48 lines
1.6 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/comparison";
|
|
|
|
export import library "prelude/types/bool";
|
|
import library "prelude/types/int_literal";
|
|
|
|
// Equality comparison: `a == b` and `a != b`.
|
|
interface Eq {
|
|
fn Equal[self: Self](other: Self) -> bool;
|
|
fn NotEqual[self: Self](other: Self) -> bool;
|
|
}
|
|
|
|
// Relational comparison: `a < b`, `a <= b`, `a > b`, `a >= b`.
|
|
interface Ordered {
|
|
// TODO: fn Compare
|
|
fn Less[self: Self](other: Self) -> bool;
|
|
fn LessOrEquivalent[self: Self](other: Self) -> bool;
|
|
fn Greater[self: Self](other: Self) -> bool;
|
|
fn GreaterOrEquivalent[self: Self](other: Self) -> bool;
|
|
}
|
|
|
|
// Equality comparison for `bool`.
|
|
// Note that this must be provided in this library as `bool` doesn't have any
|
|
// associated libraries of its own.
|
|
impl bool as Eq {
|
|
fn Equal[self: Self](other: Self) -> bool = "bool.eq";
|
|
fn NotEqual[self: Self](other: Self) -> bool = "bool.neq";
|
|
}
|
|
|
|
|
|
// Operations for IntLiteral. These need to be here because IntLiteral has no
|
|
// associated library of its own.
|
|
impl IntLiteral() as Eq {
|
|
fn Equal[self: Self](other: Self) -> bool = "int.eq";
|
|
fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
|
|
}
|
|
|
|
impl IntLiteral() as Ordered {
|
|
// TODO: fn Compare
|
|
fn Less[self: Self](other: Self) -> bool = "int.less";
|
|
fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
|
|
fn Greater[self: Self](other: Self) -> bool = "int.greater";
|
|
fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
|
|
}
|