-
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
base: trunk
Are you sure you want to change the base?
Changes from 3 commits
aa33a1e
56249f7
6da998b
db976ee
5aec63f
a7efde0
1f2f37f
9c59aa1
a8a3c4f
d06da8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,26 @@ 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 C++ interop type](#the-c-interop-type) | ||||||||||||||
| - [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` package](#the-cpp-package) | ||||||||||||||
| - [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: 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 +49,165 @@ 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` package 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 and | ||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| ## 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 level, not just at the linking (ABI) | ||||||||||||||
| level. This document specifies the design of this semantic contract. | ||||||||||||||
|
|
||||||||||||||
| ### The C++ interop type | ||||||||||||||
|
|
||||||||||||||
| A core mechanism in this design is the C++ interop type. This concept defines | ||||||||||||||
| the "trigger" that activates C++-specific semantic rules within the Carbon | ||||||||||||||
| compiler. Any operation involving a type that is designated as a C++ interop | ||||||||||||||
| type could invoke the specialized interoperability logic, such as C++ overload | ||||||||||||||
| resolution or operator overload resolution that involves both Carbon and C++ | ||||||||||||||
| operator overloads.. | ||||||||||||||
|
||||||||||||||
| operator overloads.. | |
| operator overloads. |
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.
Outdated
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.
Do we want this fourth case? It sounds expensive to check whether Carbon classes are compounded from C++ types in general, and I'm not sure we know yet what Carbon struct types would map into in C++, but I don't think it's likely that there'll be C++ operations defined for either case.
Maybe we can narrow this down to just Carbon class types that extend a C++ type?
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.
Outdated
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.
As above, I'd drop the double-quotes here.
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.
Outdated
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.
| 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.
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.
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.
I would drop all the double-quotes here. While these quotes are pulled from the main README, I don't think that's worth calling out, especially since we're quoting ourselves.
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.