Skip to content

Commit ee42b2d

Browse files
authored
C++ interop: Support more binary operators (#6017)
Already supported: `+`. Newly supported: `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, `>>`, `==`, `!=`, `<`, `>`, `<=`, `>=`. Partially supported due to lack of reference support: `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`. Not supported due to lack of reference support: `<<=`, `>>=`. Not supported (I think Carbon doesn't want overloading these): `&&`, `||`. C++ Interop Demo: ```c++ // my_number.h class MyNumber { public: explicit MyNumber(int value) : value_(value) {} auto value() const -> int { return value_; } void set_value(int value) { value_ = value; } private: int value_; }; // Arithmetic auto operator+(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator-(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator*(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator/(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator%(MyNumber lhs, MyNumber rhs) -> MyNumber; // Bitwise auto operator&(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator|(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator^(MyNumber lhs, MyNumber rhs) -> MyNumber; auto operator<<(MyNumber lhs, int shift) -> MyNumber; auto operator>>(MyNumber lhs, int shift) -> MyNumber; // Compound Arithmetic auto operator+=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator-=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator*=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator/=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator%=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; // Compound Bitwise auto operator&=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator|=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; auto operator^=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull; // Relational auto operator==(MyNumber lhs, MyNumber rhs) -> bool; auto operator!=(MyNumber lhs, MyNumber rhs) -> bool; auto operator<(MyNumber lhs, MyNumber rhs) -> bool; auto operator>(MyNumber lhs, MyNumber rhs) -> bool; auto operator<=(MyNumber lhs, MyNumber rhs) -> bool; auto operator>=(MyNumber lhs, MyNumber rhs) -> bool; ``` ```c++ // my_number.cpp #include "my_number.h" // Arithmetic auto operator+(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() + rhs.value()); } auto operator-(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() - rhs.value()); } auto operator*(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() * rhs.value()); } auto operator/(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() / rhs.value()); } auto operator%(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() % rhs.value()); } // Bitwise auto operator&(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() & rhs.value()); } auto operator|(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() | rhs.value()); } auto operator^(MyNumber lhs, MyNumber rhs) -> MyNumber { return MyNumber(lhs.value() ^ rhs.value()); } auto operator<<(MyNumber lhs, int shift) -> MyNumber { return MyNumber(lhs.value() << shift); } auto operator>>(MyNumber lhs, int shift) -> MyNumber { return MyNumber(lhs.value() >> shift); } // Compound Arithmetic auto operator+=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs + rhs); } auto operator-=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs - rhs); } auto operator*=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs * rhs); } auto operator/=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs / rhs); } auto operator%=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs % rhs); } // Compound Bitwise auto operator&=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs & rhs); } auto operator|=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs | rhs); } auto operator^=(MyNumber* _Nonnull lhs, MyNumber rhs) -> MyNumber* _Nonnull { return &(*lhs = *lhs ^ rhs); } // Relational auto operator==(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() == rhs.value(); } auto operator!=(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() != rhs.value(); } auto operator<(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() < rhs.value(); } auto operator>(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() > rhs.value(); } auto operator<=(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() <= rhs.value(); } auto operator>=(MyNumber lhs, MyNumber rhs) -> bool { return lhs.value() >= rhs.value(); } ``` ```carbon // main.carbon library "Main"; import Core library "io"; import Cpp library "my_number.h"; fn PrintBool(b: bool) { if (b) { Core.Print(1); } else { Core.Print(0); } } fn Run() -> i32 { // Arithmetic var num1: Cpp.MyNumber = Cpp.MyNumber.MyNumber(14); var num2: Cpp.MyNumber = Cpp.MyNumber.MyNumber(5); Core.Print(num1.value()); Core.Print(num2.value()); Core.Print((num1 + num2).value()); Core.Print((num1 - num2).value()); Core.Print((num1 * num2).value()); Core.Print((num1 / num2).value()); Core.Print((num1 % num2).value()); // Bitwise var bits1: Cpp.MyNumber = Cpp.MyNumber.MyNumber(12); var bits2: Cpp.MyNumber = Cpp.MyNumber.MyNumber(10); Core.Print(bits1.value()); Core.Print(bits2.value()); Core.Print((bits1 & bits2).value()); Core.Print((bits1 | bits2).value()); Core.Print((bits1 ^ bits2).value()); Core.Print((bits1 << 2).value()); Core.Print((bits1 >> 1).value()); // Compound Arithmetic var c: Cpp.MyNumber = Cpp.MyNumber.MyNumber(100); Core.Print(c.value()); &c += Cpp.MyNumber.MyNumber(10); Core.Print(c.value()); &c -= Cpp.MyNumber.MyNumber(20); Core.Print(c.value()); &c *= Cpp.MyNumber.MyNumber(2); Core.Print(c.value()); &c /= Cpp.MyNumber.MyNumber(6); Core.Print(c.value()); &c %= Cpp.MyNumber.MyNumber(9); Core.Print(c.value()); // Compound Bitwise &c |= Cpp.MyNumber.MyNumber(12); Core.Print(c.value()); &c &= Cpp.MyNumber.MyNumber(7); Core.Print(c.value()); &c ^= Cpp.MyNumber.MyNumber(10); Core.Print(c.value()); // Relational var rel1: Cpp.MyNumber = Cpp.MyNumber.MyNumber(20); var rel2: Cpp.MyNumber = Cpp.MyNumber.MyNumber(30); var rel3: Cpp.MyNumber = Cpp.MyNumber.MyNumber(20); Core.Print(rel1.value()); Core.Print(rel2.value()); Core.Print(rel3.value()); PrintBool(rel1 == rel3); PrintBool(rel1 != rel2); PrintBool(rel1 < rel2); PrintBool(rel2 > rel1); PrintBool(rel1 <= rel3); PrintBool(rel1 >= rel2); return 0; } ``` ```shell $ clang -c my_number.cpp $ bazel-bin/toolchain/carbon compile main.carbon $ bazel-bin/toolchain/carbon link my_number.o main.o --output=demo $ ./demo 14 5 19 9 70 2 4 12 10 8 14 6 48 6 100 110 90 180 30 3 15 7 13 20 30 20 1 1 1 1 1 0 ``` Part of #5995.
1 parent db0a00d commit ee42b2d

File tree

2 files changed

+1037
-166
lines changed

2 files changed

+1037
-166
lines changed

toolchain/check/import_cpp.cpp

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,12 +2017,114 @@ auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
20172017
access);
20182018
}
20192019

2020-
static auto GetOperatorKind(Context& context, SemIR::LocId loc_id,
2021-
llvm::StringLiteral interface_name)
2020+
static auto GetClangOperatorKind(Context& context, SemIR::LocId loc_id,
2021+
llvm::StringLiteral interface_name,
2022+
llvm::StringLiteral op_name)
20222023
-> std::optional<clang::OverloadedOperatorKind> {
2024+
// Arithmetic Operators.
20232025
if (interface_name == "AddWith") {
2026+
CARBON_CHECK(op_name == "Op");
20242027
return clang::OO_Plus;
20252028
}
2029+
if (interface_name == "SubWith") {
2030+
CARBON_CHECK(op_name == "Op");
2031+
return clang::OO_Minus;
2032+
}
2033+
if (interface_name == "MulWith") {
2034+
CARBON_CHECK(op_name == "Op");
2035+
return clang::OO_Star;
2036+
}
2037+
if (interface_name == "DivWith") {
2038+
CARBON_CHECK(op_name == "Op");
2039+
return clang::OO_Slash;
2040+
}
2041+
if (interface_name == "ModWith") {
2042+
CARBON_CHECK(op_name == "Op");
2043+
return clang::OO_Percent;
2044+
}
2045+
2046+
// Bitwise Operators.
2047+
if (interface_name == "BitAndWith") {
2048+
CARBON_CHECK(op_name == "Op");
2049+
return clang::OO_Amp;
2050+
}
2051+
if (interface_name == "BitOrWith") {
2052+
CARBON_CHECK(op_name == "Op");
2053+
return clang::OO_Pipe;
2054+
}
2055+
if (interface_name == "BitXorWith") {
2056+
CARBON_CHECK(op_name == "Op");
2057+
return clang::OO_Caret;
2058+
}
2059+
if (interface_name == "LeftShiftWith") {
2060+
CARBON_CHECK(op_name == "Op");
2061+
return clang::OO_LessLess;
2062+
}
2063+
if (interface_name == "RightShiftWith") {
2064+
CARBON_CHECK(op_name == "Op");
2065+
return clang::OO_GreaterGreater;
2066+
}
2067+
2068+
// Compound Assignment Arithmetic Operators.
2069+
if (interface_name == "AddAssignWith") {
2070+
CARBON_CHECK(op_name == "Op");
2071+
return clang::OO_PlusEqual;
2072+
}
2073+
if (interface_name == "SubAssignWith") {
2074+
CARBON_CHECK(op_name == "Op");
2075+
return clang::OO_MinusEqual;
2076+
}
2077+
if (interface_name == "MulAssignWith") {
2078+
CARBON_CHECK(op_name == "Op");
2079+
return clang::OO_StarEqual;
2080+
}
2081+
if (interface_name == "DivAssignWith") {
2082+
CARBON_CHECK(op_name == "Op");
2083+
return clang::OO_SlashEqual;
2084+
}
2085+
if (interface_name == "ModAssignWith") {
2086+
CARBON_CHECK(op_name == "Op");
2087+
return clang::OO_PercentEqual;
2088+
}
2089+
2090+
// Compound Assignment Bitwise Operators.
2091+
if (interface_name == "BitAndAssignWith") {
2092+
CARBON_CHECK(op_name == "Op");
2093+
return clang::OO_AmpEqual;
2094+
}
2095+
if (interface_name == "BitOrAssignWith") {
2096+
CARBON_CHECK(op_name == "Op");
2097+
return clang::OO_PipeEqual;
2098+
}
2099+
if (interface_name == "BitXorAssignWith") {
2100+
CARBON_CHECK(op_name == "Op");
2101+
return clang::OO_CaretEqual;
2102+
}
2103+
// TODO: Add support for `LeftShiftAssignWith` (`OO_LessLessEqual`) and
2104+
// `RightShiftAssignWith` (`OO_GreaterGreaterEqual`) when references are
2105+
// supported.
2106+
2107+
// Relational Operators.
2108+
if (interface_name == "EqWith") {
2109+
if (op_name == "Equal") {
2110+
return clang::OO_EqualEqual;
2111+
}
2112+
CARBON_CHECK(op_name == "NotEqual");
2113+
return clang::OO_ExclaimEqual;
2114+
}
2115+
if (interface_name == "OrderedWith") {
2116+
if (op_name == "Less") {
2117+
return clang::OO_Less;
2118+
}
2119+
if (op_name == "Greater") {
2120+
return clang::OO_Greater;
2121+
}
2122+
if (op_name == "LessOrEquivalent") {
2123+
return clang::OO_LessEqual;
2124+
}
2125+
CARBON_CHECK(op_name == "GreaterOrEquivalent");
2126+
return clang::OO_GreaterEqual;
2127+
}
20262128

20272129
context.TODO(loc_id, llvm::formatv("Unsupported operator interface `{0}`",
20282130
interface_name));
@@ -2038,7 +2140,8 @@ auto ImportOperatorFromCpp(Context& context, SemIR::LocId loc_id, Operator op)
20382140
builder.Note(loc_id, InCppOperatorLookup, op.interface_name.str());
20392141
});
20402142

2041-
auto op_kind = GetOperatorKind(context, loc_id, op.interface_name);
2143+
auto op_kind =
2144+
GetClangOperatorKind(context, loc_id, op.interface_name, op.op_name);
20422145
if (!op_kind) {
20432146
return SemIR::ScopeLookupResult::MakeNotFound();
20442147
}

0 commit comments

Comments
 (0)