mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Roundtrip (export/reimport) class declarations (#7182)
Roundtrip (export/reimport) class declarations The remapping was previously implemented using name_scopes, which aren't created for class declarations, only definitions - causing the reimport to import a fresh copy of the type that mismatched with the original (as seen in the test baseline). By changing the mapping to use the reverse part of the clang_decls mapping this should generalize better (& we probably should further migrate to that mapping). Though it did trip over some issue with exactly which instruction is used as the key in the clang_decls map - this change moves towards standardizing on the first decl id of the class as its map key.
This commit is contained in:
@@ -116,8 +116,7 @@ auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id,
|
||||
}
|
||||
|
||||
auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::InstId class_inst_id, SemIR::ClassType class_type)
|
||||
-> clang::TagDecl* {
|
||||
SemIR::ClassType class_type) -> clang::TagDecl* {
|
||||
// TODO: A lot of logic in this function is shared with ExportNameScopeToCpp.
|
||||
// This should be refactored.
|
||||
|
||||
@@ -131,9 +130,11 @@ auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
|
||||
// If this class was produced by importing a C++ declaration or has
|
||||
// already been exported to C++, return the corresponding Clang declaration.
|
||||
// That could either be a CXXRecordDecl or an EnumDecl.
|
||||
if (auto* decl_context =
|
||||
GetClangDeclContextForScope(context, class_info.scope_id)) {
|
||||
return cast<clang::TagDecl>(decl_context);
|
||||
if (auto clang_decl_id =
|
||||
context.clang_decls().Lookup(class_info.first_decl_id());
|
||||
clang_decl_id.has_value()) {
|
||||
return cast<clang::TagDecl>(
|
||||
context.clang_decls().Get(clang_decl_id).key.decl);
|
||||
}
|
||||
|
||||
auto* identifier_info = GetClangIdentifierInfo(context, class_info.name_id);
|
||||
@@ -157,8 +158,8 @@ auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
|
||||
|
||||
auto key =
|
||||
SemIR::ClangDeclKey::ForNonFunctionDecl(cast<clang::Decl>(record_decl));
|
||||
auto clang_decl_id =
|
||||
context.clang_decls().Add({.key = key, .inst_id = class_inst_id});
|
||||
auto clang_decl_id = context.clang_decls().Add(
|
||||
{.key = key, .inst_id = class_info.first_decl_id()});
|
||||
if (class_info.scope_id.has_value()) {
|
||||
// TODO: Record the Carbon class -> clang declaration mapping for incomplete
|
||||
// classes too.
|
||||
|
||||
@@ -28,8 +28,7 @@ auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id,
|
||||
// Otherwise, creates a new C++ class and returns it. Returns nullptr if the
|
||||
// class could not be exported and an error was diagnosed.
|
||||
auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::InstId class_inst_id, SemIR::ClassType class_type)
|
||||
-> clang::TagDecl*;
|
||||
SemIR::ClassType class_type) -> clang::TagDecl*;
|
||||
|
||||
// Export all `SemIR::FieldDecl`s in the class body as `clang::FieldDecl`s.
|
||||
auto ExportAllFieldsToCpp(Context& context, SemIR::Class& class_info) -> void;
|
||||
|
||||
@@ -226,8 +226,8 @@ static auto TryMapClassType(Context& context, SemIR::TypeInstId class_inst_id,
|
||||
}
|
||||
|
||||
// Otherwise, find the existing C++ declaration or create a new one.
|
||||
auto* tag_decl = ExportClassToCpp(context, SemIR::LocId(class_inst_id),
|
||||
class_inst_id, class_type);
|
||||
auto* tag_decl =
|
||||
ExportClassToCpp(context, SemIR::LocId(class_inst_id), class_type);
|
||||
if (!tag_decl) {
|
||||
return clang::QualType();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user