Files
carbon-lang/toolchain/sem_ir/name.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

67 lines
2.3 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/sem_ir/name.h"
#include "llvm/ADT/StringSwitch.h"
namespace Carbon::SemIR {
// Get the spelling to use for a special name.
static auto GetSpecialName(NameId name_id, bool for_ir) -> llvm::StringRef {
if (name_id == NameId::None) {
return for_ir ? "" : "<none>";
}
auto special_name_id = name_id.AsSpecialNameId();
CARBON_CHECK(special_name_id, "Not a special name");
switch (*special_name_id) {
case NameId::SpecialNameId::Base:
return "base";
case NameId::SpecialNameId::ChoiceDiscriminant:
return "discriminant";
case NameId::SpecialNameId::Core:
return "Core";
case NameId::SpecialNameId::Destroy:
return "destroy";
case NameId::SpecialNameId::PackageNamespace:
return "package";
case NameId::SpecialNameId::PeriodSelf:
return ".Self";
case NameId::SpecialNameId::ReturnSlot:
return for_ir ? "return" : "<return slot>";
case NameId::SpecialNameId::SelfType:
return "Self";
case NameId::SpecialNameId::SelfValue:
return "self";
case NameId::SpecialNameId::Underscore:
return "_";
case NameId::SpecialNameId::Vptr:
return for_ir ? "vptr" : "<vptr>";
case NameId::SpecialNameId::CppOperator:
return for_ir ? "cpp_operator" : "<C++ operator>";
}
}
auto NameStoreWrapper::GetFormatted(NameId name_id) const -> llvm::StringRef {
// If the name is an identifier name with a keyword spelling, format it with
// an `r#` prefix. Format any other identifier name as just the identifier.
if (auto string_name = GetAsStringIfIdentifier(name_id)) {
return llvm::StringSwitch<llvm::StringRef>(*string_name)
#define CARBON_KEYWORD_TOKEN(Name, Spelling) .Case(Spelling, "r#" Spelling)
#include "toolchain/lex/token_kind.def"
.Default(*string_name);
}
return GetSpecialName(name_id, /*for_ir=*/false);
}
auto NameStoreWrapper::GetIRBaseName(NameId name_id) const -> llvm::StringRef {
if (auto string_name = GetAsStringIfIdentifier(name_id)) {
return *string_name;
}
return GetSpecialName(name_id, /*for_ir=*/true);
}
} // namespace Carbon::SemIR