-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
C++ interoperability goals #175
Conversation
Co-authored-by: Geoff Romer <[email protected]>
Co-authored-by: Geoff Romer <[email protected]>
ability to understand code rewrites. It should be reasonable to have these | ||
instead rewritten to use Carbon's metaprogramming model. | ||
|
||
## Open questions to be resolved later |
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 might mention common language extensions here as well. They seem like something that could go through a case-by-case cost-benefit analysis like you suggest for CRTP.
Attributes seem like a good example.
I imagine that Carbon will have some way of expressing that a function doesn't return, or the type of a function that does not return. Clang knows about several such ways to express this in C and C++. And being able to pass a Carbon no-return function to a C++ function with a _Noreturn or [[noreturn]] parameter could be worthwhile. Similarly, non-null C++ attributes may be worth the effort.
Whereas attributes for Renderscript or Objective-C garbage collection may not be, and not be something Carbon provides any support for, let alone interoperability.
Interactions with the linker (specifying sections and so on) and inline assembly may fall in between. Carbon may need to provide functionality here, but maybe not interoperability with C++.
My point isn't so much the specific examples (though they are of general interest to the kind of programming I'm representing), but more that I'd suggest that this section is not complete, and not intended to be complete, and suggest that you leave room for evaluating particular extensions in the future.
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 think that "most idiomatic usage of advanced C++ features" could be considered to cover this. I've added attributes explicitly there. Enumerating specific attributes is definitely too detailed for a "goals" doc.
both Carbon and C/C++. Pragmatically, Carbon will likely be the limiting factor | ||
here. | ||
|
||
#### Interoperability with and migration from existing C++ code |
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 may be overly pedantic or covered in some other context I missed; sorry if so.
Sometimes this document says C/C++; sometimes it says C++.
In particular, right here it says "C++ migration". I wanted to know if this means really only migration from C++17, or if migration from C is something you are planning.
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.
Sometimes this document says C/C++; sometimes it says C++.
That's a little deliberate in this doc: I've tried to be a little precise to avoid bikesheds of "oh, you plan C++ interop, but are ignoring C". I try to talk about C++ without C when talking about C++-specific features, like templates.
In particular, right here it says "C++ migration".
Please note, this is the phrasing of the language goal, not specific to this proposal.
I wanted to know if this means really only migration from C++17, or if migration from C is something you are planning.
Rephrased the first sentence to specify C++17.
proposals/p0175.md
Outdated
should be provided where possible. However, native Carbon code will be easier to | ||
make safe than Carbon code that invokes C/C++. C/C++ code will not benefit from | ||
the same set of safety mechanisms that Carbon offers. As a result, safety | ||
guarantees will be weaker around interoperability layers. |
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.
s/will be/may be/
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 prefer stronger phrasing, and I'm wondering if you're misunderstanding why "will" is appropriate -- I've elaborated a little to try to improve this point without softening it.
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 think it may for some parts of safety and may not for others, so the stronger phrasing feels misleading
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.
To make sure we're discussing the same text, the phrasing now is:
C/C++ code will not benefit from the same set of safety
mechanisms that Carbon offers, so Carbon code calling into C/C++ will accept
higher safety risks.
Are you saying that you feel this is still misleading? I think "same set" should address your concern regarding "some parts", shouldn't it?
|
||
**Incomplete types** must have a mapping with similar semantics, similar to | ||
primitive types. | ||
|
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 think we should state that the Carbon memory model for concurrency will also be compatible with C++ (possibly excepting memory_order_consume). And that a release in Carbon will establish a happens before relationship with an acquire in C++ and vice versa
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.
Does this need to be part of the goals? It's important, yes, but is this detail really something for the high-level goals, versus a design about the memory model?
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.
you are enumerating a bunch of "unsurprising mappings", it seems to make sense that this would be a good place to call out both the important of memory model and the fact that it will be unsurprising.
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.
FWIW, I think having a goal around the fundamentals of the memory model makes a lot of sense. It is a question that has come up in multiple introductions to Carbon I've done so far...
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've added a "Compatibility with the C/C++ memory model" goal under "mixing toolchains", because it feels like a better fit there.
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.
Nice addition there. Flows well
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.
Thanks!
proposals/p0175.md
Outdated
|
||
- An interface which can hide the difference between whether `std::vector<T>` | ||
or the equivalent, idiomatic Carbon type is in use may also be offered, | ||
although this could end up similarly adding performance overhead. |
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 expect that a wrapper type + specialization would be sufficient to get nearly zero overhead access to std::vector<T>
as an idiomatic carbon type. If we cannot provide this for unordered_map
, string
, and vector
I would be quite worried about being able to incrementally migrate to idiomatic carbon.
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.
+1, we should aim to make generics efficient (from a runtime perspective, with some potential compile-time cost) for this use case.
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.
Rephrased to avoid a performance argument. I'm not convinced that you can guarantee zero-overhead in every possible case with generics wrapping C++ types vs native idiomatic Carbon, but I don't think it's important here.
To be clear though, "nearly zero" and "zero" are important to differentiate if we're positioning Carbon as a performance-first language. If it's not zero, then we should anticipate that somebody will ask for a better option.
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.
by "nearly zero" I think of the level of overhead that unique_ptr introduces. Not zero, but close enough to it that most everyone is satisfied most of the time.
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.
Acknowledged, with my noted concern -- actually captured under "Performance-critical software" already. However, I don't think that affects this section as written.
proposals/p0175.md
Outdated
#### Support use of advanced C++ features | ||
|
||
There should be support for most idiomatic usage of advanced C++ features. For | ||
example, templates, overload sets, and ADL. |
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.
Not a sentence.
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.
"A few examples are"
proposals/p0175.md
Outdated
|
||
- An interface which can hide the difference between whether `std::vector<T>` | ||
or the equivalent, idiomatic Carbon type is in use may also be offered, | ||
although this could end up similarly adding performance overhead. |
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.
+1, we should aim to make generics efficient (from a runtime perspective, with some potential compile-time cost) for this use case.
Co-authored-by: josh11b <[email protected]>
proposals/p0175.md
Outdated
With a Carbon toolchain compiling both Carbon and C++ code, Carbon templates can | ||
be handled by adding C++ compiler code paths that use portions of the Carbon AST | ||
generated by the Carbon template code. While this approach is still complex and |
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.
Carbon templates can
be handled by adding C++ compiler code paths that use portions of the Carbon AST
generated by the Carbon template code.
This sentence is pretty central to understanding what your saying, but I didn't succeed in gathering what it was saying. Are you talking about C++ templates with Carbon types as parameters, or maybe Carbon templates with C++ types? These aren't what I think of if you say "Carbon templates". I don't know what "C++ compiler code paths" are -- "paths" reminds me of files, but I don't think that is what you mean.
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.
Rephrased a little:
exposing the Carbon compiler's AST to the C++ compiler directly,
as a compiler extension
proposals/p0175.md
Outdated
able to use the generated C++ source. However, generating that source is | ||
complex: whereas mixed toolchain interoperability assumes Carbon can generate | ||
declarations for C++ headers, that's a simple task compared to translating | ||
arbitrary Carbon template code to C++. Supporting Carbon to C++ code |
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.
You might point out that the operators will be different between Carbon and C++. For example, C++ uses <<
both for streamed output and binary shift left
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 already talk a bit more about mixing toolchains than I'd like -- the section is pretty long. My sense is that it's better to avoid talking about operator<<
, because of the amount of discussion that doing so would require, and it's not clear to me that it would add much:
- There's a design decision to be made about whether
operator<<
should really be that different. - I'd expect any decision to address the C++ incompatibility, given the prevalence of use.
- There should still be some interoperable method for using C++'s operator<<, and likely even overloading it from Carbon, in order to support calling standard C++ code from Carbon.
- In context here, a template could rely on using whatever interoperable method is chosen.
Explaining all of this seems pretty expensive, and also possibly bleeds operator<<
design choices into this "goals" proposal. I think it's better to stay at a high level.
Co-authored-by: Richard Smith <[email protected]>
…lang into interop-skeleton
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.
Responding to zygoloid
proposals/p0175.md
Outdated
|
||
### Philosophy | ||
|
||
The C/C++ interoperability layer of Carbon is the section wherein a specific, |
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.
Fine fine, replaced C/C++ with C. Added a specific C goal.
I disagree that C should not be mentioned here -- there are still many maintained systems that rely on C APIs, and I think that part of writing a successor to C++ is guaranteeing we can still interact with C APIs.
proposals/p0175.md
Outdated
Mixing toolchains restricts functionality to what's feasible with the C/C++ ABI. | ||
For example, developers should expect that Carbon templates will be callable | ||
from C++ when using the Carbon toolchain, and will not be available when mixing | ||
toolchains because it would require a substantially different and more complex | ||
interoperability implementation. This degraded interoperability should still be | ||
sufficient for most developers, albeit with the potential of more bridge code. |
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.
Added:
Any C++ interoperability code that works when mixing toolchains must work when
using the native Carbon toolchain. The mixed toolchain support must not have
semantic divergence. The converse is not true, and the native Carbon toolchain
may have additional language support and optimizations.
|
||
### Non-goals | ||
|
||
#### Full parity between a Carbon-only toolchain and mixing C++/Carbon toolchains |
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 think I was trying to say this, but rephrased this non-goal.
proposals/p0175.md
Outdated
|
||
#### Allow C++ bridge code in Carbon files | ||
|
||
Carbon files should support inline C++ bridge code. Where bridge code is |
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.
The latter, rephrased
Co-authored-by: Dmitri Gribenko <[email protected]> Co-authored-by: Chandler Carruth <[email protected]>
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.
Accepted on 2020-11-24.
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.
Explicitly leaving an LGTM on my part.
Related threads:
|
This takes #175 and publishes it as interop goals. I've made a few small editorial changes, but the main addition is the "Offer equivalent support for languages other than C++" non-goal which I thought may be useful (in particular, it can be used to clarify why this directory is "interoperability" and not "interoperability_cpp").
* Decision for #175: C++ interoperability goals * Update proposals/p0175_decision.md Co-authored-by: Chandler Carruth <[email protected]> * Prettified suggested changes * Update table of contents Co-authored-by: Chandler Carruth <[email protected]>
Related threads: - From austern, [Initial draft of C++ Interoperability principles doc #62](#62) - From me, [Carbon <-> C/C++ interoperability #80](#80) - [Doc](https://docs.google.com/document/d/1va8VgvDdA966WG3znJyUrlComYqNfNBV7__hUd9XxxU/edit) - [Ideas topic](https://forums.carbon-lang.dev/t/draft-carbon-c-c-interoperability/77) - [RFC topic](https://forums.carbon-lang.dev/t/rfc-carbon-c-c-interoperability/89) - From chandlerc, [Interop implementation strategies](https://forums.carbon-lang.dev/t/interop-implementation-strategies/108) For this PR: - [RFC topic](https://forums.carbon-lang.dev/t/rfc-c-interoperability-goals-175/156) - [Decision topic](https://forums.carbon-lang.dev/t/request-for-decision-c-interoperability-goals/171) - [Decision announcement](https://forums.carbon-lang.dev/t/accepted-c-interoperability-goals/175) - [Decision PR](#200)
This takes #175 and publishes it as interop goals. I've made a few small editorial changes, but the main addition is the "Offer equivalent support for languages other than C++" non-goal which I thought may be useful (in particular, it can be used to clarify why this directory is "interoperability" and not "interoperability_cpp").
* Decision for #175: C++ interoperability goals * Update proposals/p0175_decision.md Co-authored-by: Chandler Carruth <[email protected]> * Prettified suggested changes * Update table of contents Co-authored-by: Chandler Carruth <[email protected]>
Related threads:
For this PR: