Files
carbon-lang/toolchain/check/operator.cpp
T
Boaz Brickner 870c5380a0 C++ interop: Support importing binary operator+ (#5996)
Triggered by calling a binary operator with LHS being an imported C++
class type.

Not supported (yet):
* Multiple overloads.
* Other operators.

C++ Interop Demo:

```c++
// hello_world.h

class C {
 public:
  C(int x) : x_(x) {}
  auto x() const -> int { return x_; }

 private:
  int x_ = 0; 
};

auto operator+ (C c1, C c2) -> C;
```

```c++
// hello_world.cpp

#include "hello_world.h"

#include <cstdio>

auto operator+ (C c1, C c2) -> C {
  printf("Adding %d with %d\n", c1.x(), c2.x());
  return C(c1.x() + c2.x());
}
```

```carbon
// main.carbon

library "Main";

import Cpp library "hello_world.h";

fn Run() -> i32 {
  let c1 : Cpp.C = Cpp.C.C(7);
  let c2 : Cpp.C = Cpp.C.C(8);
  let c3 : Cpp.C = c1 + c2;
  let c4 : Cpp.C = c3 + c2;
  return 0;
}

```

```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
Adding 7 with 8
Adding 15 with 8
```

Part of #5995.
2025-09-03 12:01:20 +00:00

106 lines
4.1 KiB
C++

// 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
#include "toolchain/check/operator.h"
#include "toolchain/check/call.h"
#include "toolchain/check/context.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/import_cpp.h"
#include "toolchain/check/member_access.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
// Returns the `Op` function for the specified operator.
static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
Operator op) -> SemIR::InstId {
auto implicit_loc_id = context.insts().GetLocIdForDesugaring(loc_id);
// Look up the interface, and pass it any generic arguments.
auto interface_id =
LookupNameInCore(context, implicit_loc_id, op.interface_name);
if (!op.interface_args_ref.empty()) {
interface_id = PerformCall(context, implicit_loc_id, interface_id,
op.interface_args_ref);
}
// Look up the interface member.
auto op_name_id =
SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
return PerformMemberAccess(context, implicit_loc_id, interface_id,
op_name_id);
}
auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
SemIR::InstId operand_id,
MakeDiagnosticBuilderFn missing_impl_diagnoser)
-> SemIR::InstId {
// Look up the operator function.
auto op_fn = GetOperatorOpFunction(context, loc_id, op);
// Form `operand.(Op)`.
auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
op_fn, missing_impl_diagnoser);
if (bound_op_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::InstId;
}
// Form `bound_op()`.
return PerformCall(context, loc_id, bound_op_id, {});
}
// Returns whether the type of the instruction is a C++ class.
static auto IsOfCppClassType(Context& context, SemIR::InstId inst_id) -> bool {
auto class_type = context.insts().TryGetAs<SemIR::ClassType>(
context.types().GetInstId(context.insts().Get(inst_id).type_id()));
if (!class_type) {
// Not a class.
return false;
}
return context.name_scopes()
.Get(context.classes().Get(class_type->class_id).scope_id)
.is_cpp_scope();
}
auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
SemIR::InstId lhs_id, SemIR::InstId rhs_id,
MakeDiagnosticBuilderFn missing_impl_diagnoser)
-> SemIR::InstId {
// For binary operators with a C++ class as at least one of the operands, try
// to import and call the C++ operator.
// TODO: Instead of hooking this here, change impl lookup, so that a generic
// constraint such as `T:! Core.Add` is satisfied by C++ class types that are
// addable. See
// https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308666348
// and
// https://github.com/carbon-language/carbon-lang/pull/5996/files/5d01fa69511b76f87efbc0387f5e40abcf4c911a#r2308664536
if (IsOfCppClassType(context, lhs_id) || IsOfCppClassType(context, rhs_id)) {
SemIR::ScopeLookupResult cpp_lookup_result =
ImportOperatorFromCpp(context, loc_id, op);
if (cpp_lookup_result.is_found()) {
return PerformCall(context, loc_id, cpp_lookup_result.target_inst_id(),
{lhs_id, rhs_id});
}
}
// Look up the operator function.
auto op_fn = GetOperatorOpFunction(context, loc_id, op);
// Form `lhs.(Op)`.
auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
missing_impl_diagnoser);
if (bound_op_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::InstId;
}
// Form `bound_op(rhs)`.
return PerformCall(context, loc_id, bound_op_id, {rhs_id});
}
} // namespace Carbon::Check