-
Notifications
You must be signed in to change notification settings - Fork 1.5k
C++ Interop: API importing and semantics #6358
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
wants to merge
10
commits into
carbon-language:trunk
Choose a base branch
from
bricknerb:interop2
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+321
−1
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aa33a1e
C++ Interop design details - the basics
bricknerb 56249f7
Use sentence case for headings
bricknerb 6da998b
Focus on language design and address review comments.
bricknerb db976ee
Avoid mentioning C.
bricknerb 5aec63f
Remove double-quotes.
bricknerb a7efde0
For C++ interop type definition, replace containing a member with ext…
bricknerb 1f2f37f
Fix double period typo.
bricknerb 9c59aa1
Filling out template with PR 6358
bricknerb a8a3c4f
Fix typo.
bricknerb d06da8e
Add C++ Interop: API importing and semantics proposal
bricknerb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,27 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||||||||||||
|
|
||||||||||||||
| - [Philosophy and goals](#philosophy-and-goals) | ||||||||||||||
| - [Overview](#overview) | ||||||||||||||
| - [C++ Interoperability Model: Introduction and Principles](#c-interoperability-model-introduction-and-principles) | ||||||||||||||
| - [The "Successor Language" Mandate](#the-successor-language-mandate) | ||||||||||||||
| - [The `Cpp` Associated Type: The Interop Trigger](#the-cpp-associated-type-the-interop-trigger) | ||||||||||||||
| - [Importing C++ APIs into Carbon](#importing-c-apis-into-carbon) | ||||||||||||||
| - [Importing C++ Libraries (Header-Based)](#importing-c-libraries-header-based) | ||||||||||||||
| - [TODO: Importing C++ Code (Inline)](#todo-importing-c-code-inline) | ||||||||||||||
| - [Accessing Built-in C++ Entities (File-less)](#accessing-built-in-c-entities-file-less) | ||||||||||||||
| - [The `Cpp` Namespace](#the-cpp-namespace) | ||||||||||||||
| - [TODO: Importing C++ Macros](#todo-importing-c-macros) | ||||||||||||||
| - [Calling C++ Code from Carbon](#calling-c-code-from-carbon) | ||||||||||||||
| - [Function Call Syntax and Semantics](#function-call-syntax-and-semantics) | ||||||||||||||
| - [TODO: Overload Resolution](#todo-overload-resolution) | ||||||||||||||
| - [TODO: Thunks](#todo-thunks) | ||||||||||||||
| - [TODO: Constructors](#todo-constructors) | ||||||||||||||
| - [TODO: Struct Literals](#todo-struct-literals) | ||||||||||||||
| - [TODO: Accessing C++ Classes, Structs, and Members](#todo-accessing-c-classes-structs-and-members) | ||||||||||||||
| - [TODO: Accessing Global Variables](#todo-accessing-global-variables) | ||||||||||||||
| - [TODO: Bi-directional Type Mapping: Primitives and Core Types](#todo-bi-directional-type-mapping-primitives-and-core-types) | ||||||||||||||
| - [TODO: Advanced Type Mapping: Pointers, References, and `const`](#todo-advanced-type-mapping-pointers-references-and-const) | ||||||||||||||
| - [TODO: Bi-directional Type Mapping: Standard Library Types](#todo-bi-directional-type-mapping-standard-library-types) | ||||||||||||||
| - [TODO: The Operator Interoperability Model](#todo-the-operator-interoperability-model) | ||||||||||||||
|
|
||||||||||||||
| <!-- tocstop --> | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -29,4 +50,209 @@ more detail. | |||||||||||||
|
|
||||||||||||||
| ## Overview | ||||||||||||||
|
|
||||||||||||||
| TODO | ||||||||||||||
| Carbon's bidirectional interoperability with C++ is | ||||||||||||||
| [a cornerstone of its design](/docs/project/goals.md#interoperability-with-and-migration-from-existing-c-code), | ||||||||||||||
| enabling a gradual transition from existing C++ codebases. The goal is not just | ||||||||||||||
| a foreign function interface (FFI), but a seamless, high-fidelity integration | ||||||||||||||
| that supports advanced C++ features, from templates to class hierarchies. | ||||||||||||||
|
|
||||||||||||||
| C++ APIs are imported into Carbon using an `import Cpp` directive, which makes | ||||||||||||||
| C++ declarations available within a dedicated `Cpp` namespace in Carbon. This | ||||||||||||||
| prevents name collisions and makes the origin of symbols explicit. Carbon code | ||||||||||||||
| can then call C++ functions, instantiate C++ classes, and use C++ types, while | ||||||||||||||
| respecting C++'s semantics, including its complex overload resolution rules. | ||||||||||||||
|
|
||||||||||||||
| Similarly, Carbon APIs can be designed to be callable from C++. The | ||||||||||||||
| interoperability layer is designed to be zero-cost, avoiding unnecessary | ||||||||||||||
| allocations or copies when calling between the two languages This is achieved | ||||||||||||||
| through a deep semantic co-design, where the Carbon compiler embeds a C++ | ||||||||||||||
| compiler frontend (Clang) to understand and map C++ constructs with high | ||||||||||||||
| fidelity. This includes preserving the nominal distinctions between C++ types | ||||||||||||||
| like `long` and `long long`, or `T*` and `T&`, which is critical for correct | ||||||||||||||
| overload resolution and template instantiation. | ||||||||||||||
|
|
||||||||||||||
| ## C++ Interoperability Model: Introduction and Principles | ||||||||||||||
|
|
||||||||||||||
| ### The "Successor Language" Mandate | ||||||||||||||
|
|
||||||||||||||
| The design of Carbon's C++ interoperability is governed by its foundational | ||||||||||||||
| goal: [to be a successor language](/README.md), not merely a language with a | ||||||||||||||
| foreign function interface (FFI). This mandate dictates a design that moves | ||||||||||||||
| beyond the C-style FFI adopted by most modern languages and instead provides | ||||||||||||||
| "seamless, bidirectional interoperability". The objective is to support deep | ||||||||||||||
| integration with existing C++ code, encompassing its most complex features, | ||||||||||||||
| "from inheritance to templates". | ||||||||||||||
|
|
||||||||||||||
| This goal has profound implications for the Carbon compiler and language | ||||||||||||||
| semantics. It requires that C++ is not treated as a "foreign" entity. Instead, | ||||||||||||||
| Carbon's semantic model must be _co-designed_ to understand, map, and interact | ||||||||||||||
| with C++'s semantic constructs—including templates, class hierarchies, and | ||||||||||||||
| complex overload resolution—with high fidelity. The interoperability layer must, | ||||||||||||||
| therefore, operate at the semantic analysis (SemIR) level, not just at the | ||||||||||||||
| linking (ABI) level. This document specifies the design of this semantic | ||||||||||||||
| contract. | ||||||||||||||
|
|
||||||||||||||
| ### The `Cpp` Associated Type: The Interop Trigger | ||||||||||||||
|
|
||||||||||||||
| A core mechanism in this design is the `Cpp` associated type. This concept | ||||||||||||||
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| defines the "trigger" that activates C++-specific semantic rules within the | ||||||||||||||
| Carbon compiler. Any operation involving a type that is designated as a `Cpp` | ||||||||||||||
| associated type will invoke the specialized interoperability logic, such as the | ||||||||||||||
| operator model detailed in | ||||||||||||||
| [The Operator Interoperability Model Section](#todo-the-operator-interoperability-model). | ||||||||||||||
|
|
||||||||||||||
| A type is considered a `Cpp` associated type if its definition involves an | ||||||||||||||
| imported C++ type in any of the following ways: | ||||||||||||||
|
|
||||||||||||||
| 1. The C++ type itself (for example, `Cpp.Widget`). | ||||||||||||||
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| 2. A pointer to a C++ type (for example, `Cpp.Widget*`). | ||||||||||||||
| 3. A Carbon generic type parameterized with a C++ type (for example, | ||||||||||||||
| `MyCarbonVector(Cpp.Widget)`). | ||||||||||||||
| 4. A Carbon struct or class containing a C++ type as a member (for example, | ||||||||||||||
| `MyCarbonStruct { x: Cpp.Widget }`). | ||||||||||||||
|
|
||||||||||||||
| This "pervasive" model of C++-awareness is a fundamental design choice. The C++ | ||||||||||||||
| semantics are not confined to a specific `unsafe` or `extern "C++"` block; they | ||||||||||||||
| "infect" any Carbon type that composes them. For example, when the Carbon | ||||||||||||||
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| compiler instantiates a _Carbon_ generic type like `MyCarbonVector(Cpp.Widget)`, | ||||||||||||||
| its type system must be aware that the `Cpp.Widget` parameter carries | ||||||||||||||
| C++-specific rules. This mandates that Carbon's own generic system, struct | ||||||||||||||
| layout logic, and operator lookup must query the type system for the presence of | ||||||||||||||
| a `Cpp` associated type. If present, the compiler must branch to a different, | ||||||||||||||
| more complex logic path (for example, C++-aware overload resolution). This | ||||||||||||||
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| design prioritizes the goal of a "seamless" and "intuitive" user experience over | ||||||||||||||
| implementation simplicity. | ||||||||||||||
|
|
||||||||||||||
| ## Importing C++ APIs into Carbon | ||||||||||||||
|
|
||||||||||||||
| ### Importing C++ Libraries (Header-Based) | ||||||||||||||
|
|
||||||||||||||
| The primary mechanism for importing existing, user-defined C++ code is through | ||||||||||||||
| header file inclusion. The Carbon toolchain must be able to parse and analyze | ||||||||||||||
| C++ header files to make their declarations available within Carbon. | ||||||||||||||
|
|
||||||||||||||
| **Syntax:** The syntax for this operation is `import Cpp library "header_name"`. | ||||||||||||||
| This syntax is used for both C-style standard libraries and C++ headers: | ||||||||||||||
|
|
||||||||||||||
| - **C Standard Library:** | ||||||||||||||
|
||||||||||||||
| This syntax is used for both C-style standard libraries and C++ headers: | |
| - **C Standard Library:** | |
| This syntax is used for both standard library headers and user-defined headers: | |
| - **Standard Library:** |
I think we should try to avoid mentioning C here.
Contributor
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jonmeow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.