-
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
Add support for importing a trivial global C++ function #5033
Conversation
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?).
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.
Very exciting, this looks like a great step :)
toolchain/check/import_cpp.cpp
Outdated
clang::Sema& sema = ast->getSema(); | ||
|
||
auto name = context.names().GetAsStringIfIdentifier(name_id); | ||
CARBON_CHECK(name); |
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.
What happens if we try to look up a non-identifier Carbon name, such as via Cpp.Self
or Cpp.base
? Perhaps we should produce a lookup failure for non-identifier names for now.
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 a test and now using GetIRBaseName()
.
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.
My general understanding is we want to require raw keyword syntax in this sort of situation: i.e., Cpp.r#base
, with Cpp.base
being an error. So GetAsStringIfIdentifier
was correct, rather than GetIRBaseName
(which I think mainly exists for debug output).
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.
Similarly, nice to see progress here! I realize I missed the publish button though.
…ted, return error `InstId` to avoid diagnosing on name not found
It's not initialized everywhere and no warning is given. https://github.com/carbon-language/carbon-lang/blob/43b9969058b3de2e0b05896ef8e4489c1dc85cb6/toolchain/language_server/context.cpp#L146
… value as a parameter
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.
If you want to address final comments and merge, that seems fine. Note I've suggested a significant change not to use GetIRBaseName
(explanation: #5033 (comment))
toolchain/check/import_cpp.cpp
Outdated
{// Parse C++ (and not C) | ||
"-x", "c++"}, |
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.
{// Parse C++ (and not C) | |
"-x", "c++"}, | |
// Parse C++ (and not C). | |
{"-x", "c++"}, |
Per style: "Only use line comments (with //, not /* ... */), on a line by themselves"
https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md
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.
toolchain/check/import_cpp.cpp
Outdated
clang::Sema& sema = ast->getSema(); | ||
|
||
auto name = context.names().GetAsStringIfIdentifier(name_id); | ||
CARBON_CHECK(name); |
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.
My general understanding is we want to require raw keyword syntax in this sort of situation: i.e., Cpp.r#base
, with Cpp.base
being an error. So GetAsStringIfIdentifier
was correct, rather than GetIRBaseName
(which I think mainly exists for debug output).
toolchain/check/import_cpp.cpp
Outdated
return std::move(generated_ast); | ||
} | ||
|
||
// Lookups the given name in the Clang AST. Returns the lookup result if lookup |
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.
// Lookups the given name in the Clang AST. Returns the lookup result if lookup | |
// Looks up the given name in the Clang AST. Returns the lookup result if lookup |
Small grammar thing.
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.
toolchain/check/import_cpp.cpp
Outdated
lookup, ast->getASTContext().getTranslationUnitDecl()); | ||
|
||
if (lookup.isClassLookup()) { | ||
// TODO: When support class lookup, also check access. |
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.
// TODO: When support class lookup, also check access. | |
// TODO: To support class lookup, also return the AccessKind for storage. |
To be sure, access can't be checked here; that could lead to something like a protected
member being incorrectly hidden, when the caller doesn't have access. Instead the AccessKind needs to be part of the returned information so that it can be stored in the lookup table (instead of AccessKind::Public
), and then used by lookup to determine access.
(also, small grammar thing -- "to support" or "when supporting")
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.
|
||
if (lookup.isClassLookup()) { | ||
// TODO: When support class lookup, also check access. | ||
context.TODO(loc_id, "Unsupported: Lookup in Class"); |
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.
Maybe (given the "unsupported" string):
context.TODO(loc_id, "Unsupported: Lookup in Class"); | |
context.TODO(loc_id, "Unsupported: Lookup in Class"); | |
return std::nullopt; |
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.
toolchain/check/import_cpp.cpp
Outdated
CARBON_CHECK(ast); | ||
clang::Sema& sema = ast->getSema(); | ||
|
||
llvm::StringRef name = context.names().GetIRBaseName(name_id); |
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.
llvm::StringRef name = context.names().GetIRBaseName(name_id); | |
std::optional<llvm::StringRef> name = context.names().GetAsStringIfIdentifier(name_id); | |
if (!name) { | |
// Special names never exist in C++ code. | |
return std::nullopt; | |
} |
(see comment)
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.
I thought of using a dedicated error for this use case to point out this is a special name (and maybe even tell the user how to fix), but keeping as is.
See 16867c9
Thanks! |
ASTUnit is owned by
CompileSubcommand
, passed throughUnit
to be populated inImportCppFiles()
and used viaSemIR::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 inLookupNameInExactScope()
.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.