Skip to content

C++ interop: Call a subset of C++ from Carbon #4666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bricknerb opened this issue Dec 11, 2024 · 3 comments
Open

C++ interop: Call a subset of C++ from Carbon #4666

bricknerb opened this issue Dec 11, 2024 · 3 comments
Assignees
Labels
long term issue Issues expected to take over 90 days to resolve. Does not apply to PRs.

Comments

@bricknerb
Copy link
Contributor

Doc: Carbon: Early interop in toolchain

@SalmanDeveloperz
Copy link

Hi, Is this issue still opened?

@bricknerb
Copy link
Contributor Author

Hi, Is this issue still opened?

Yes, I'm looking into this one.

bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Dec 16, 2024
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Jan 10, 2025
Ignore the AST, for now.
Report cpp compilation errors and warnings.
Part of carbon-language#4666
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Jan 10, 2025
Ignore the AST, for now.
Report cpp compilation errors and warnings.
Part of carbon-language#4666
github-merge-queue bot pushed a commit that referenced this issue Jan 14, 2025
Ignore the AST and support a single Cpp import, for now.
Report cpp compilation errors and warnings.
Part of #4666
github-merge-queue bot pushed a commit that referenced this issue Jan 15, 2025
Propagate `FileSystem` to `buildASTFromCodeWithArgs`().
Part of #4666
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Jan 16, 2025
Instead of compiling the imported file, generate a C++ header that includes all `Cpp` import files.
Part of carbon-language#4666
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Jan 20, 2025
Instead of compiling the imported file, generate a C++ header that includes all `Cpp` import files.
Part of carbon-language#4666
github-merge-queue bot pushed a commit that referenced this issue Jan 20, 2025
Instead of compiling the imported file, generate a C++ header that
includes all `Cpp` import files.
Part of #4666
dwblaikie pushed a commit to dwblaikie/carbon-lang that referenced this issue Jan 21, 2025
…age#4814)

Instead of compiling the imported file, generate a C++ header that
includes all `Cpp` import files.
Part of carbon-language#4666
github-merge-queue bot pushed a commit that referenced this issue Feb 12, 2025
Also defined a dedicated `ImportCppDecl` `InstKind`.
Part of #4666
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Feb 14, 2025
This function is used by `import.cpp` and `import_cpp.cpp` to deduplciate some of the logic. However, it's not really part of the importing API, so it makes more sense to put it in the bottom. In the .cpp, it was between static functions, just for easier reviewing of the refactoring PR (carbon-language#4873).
Part of carbon-language#4666.
github-merge-queue bot pushed a commit that referenced this issue Mar 3, 2025
ASTUnit is owned by `CompileSubcommand`, passed through `Unit` to be
populated in `ImportCppFiles()` and used via `SemIR::File`.
When generating the AST, pass `-x c++` args to compile C++ (temporary
until we pass args properly).
`Cpp` namespace is marked as a special namespace and has dedicated logic
in `LookupNameInExactScope()`.
The logic for importing declarations from C++ to Carbon is in
`import_cpp.cpp`, but we're likely to want to refactor this
signfiicantly over time as it grows (perhaps a dedicated directory?).

Part of #4666.
github-merge-queue bot pushed a commit that referenced this issue Mar 6, 2025
…#5062)

Keep a pointer to the Clang declaration in Carbon's function declaration
and use it in Carbon mangling by calling Clang mangling.
Create Clang's `MangleContext` once on demand.

Part of #4666.

C++ Interop Demo:

```c++
// hello_world.h

void hello_world();
```

```c++
// hello_world.cpp

#include <cstdio>

void hello_world() { printf("Hello World!\n"); }
```

```
// main.carbon

library "Main";

import Cpp library "hello_world.h";

fn Run() -> i32 {
  Cpp.hello_world();
  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
Hello World!
```
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Mar 10, 2025
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Mar 11, 2025
github-merge-queue bot pushed a commit that referenced this issue Mar 11, 2025
…decl.carbon` (#5104)

It's not specific for functions

Part of #4666
github-merge-queue bot pushed a commit that referenced this issue Mar 12, 2025
This is a followup of [a
comment](https://github.com/carbon-language/carbon-lang/pull/5062/files/89e56d51858bcc18d4242d4e5c9ee0e7496d887e#r1979993815)
in #5062.

Add a mutable AST pointer to `FileContext`.

This is necessary since we use [Clang with lack of const
correctness](llvm/llvm-project#130096 (comment)).

Alternatives in Clang:
* Change `ASTUnit::getASTContext() const` to return a non-const
`ASTContext`. [Tried and was rejected upstream due to weakening const
correctness](llvm/llvm-project#130096).
* Change `createMangleContext()` to be `const`. Tried that and it seems
like it relies heavily on non const API.
* Change `MangleContext::mangleName()` to `const`. Tried that but there
are several lazy initialization and id creations happening that modify
the context. See details in
llvm/llvm-project#130613.

Alternatives in Carbon:
* Use `const_cast` on `ASTContext` when calling `createMangleContext()`.
* Make `FileContext::sem_ir_` point to a mutable `SemIR::File`.
* Change `File::cpp_ast()` to be const while keeping it return a mutable
pointer.

Part of #4666.
Copy link

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term issue label can also be added for issues which are expected to take time.

This issue is labeled inactive because the last activity was over 90 days ago.

@github-actions github-actions bot added the inactive Issues and PRs which have been inactive for at least 90 days. label Mar 17, 2025
@bricknerb bricknerb added long term issue Issues expected to take over 90 days to resolve. Does not apply to PRs. and removed inactive Issues and PRs which have been inactive for at least 90 days. labels Mar 17, 2025
bricknerb added a commit to bricknerb/carbon-lang that referenced this issue Mar 21, 2025
Currently has no effect because we can't do lookup in classes, yet.

Part of carbon-language#4666.
github-merge-queue bot pushed a commit that referenced this issue Mar 24, 2025
…5142)

Currently has no effect because we can't do lookup in classes, yet.

Part of #4666.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
long term issue Issues expected to take over 90 days to resolve. Does not apply to PRs.
Projects
None yet
Development

No branches or pull requests

2 participants