Merge `TypeMapper` back into `Importer` - part 3/N: `Insert`.
This CL is one of steps toward merging `TypeMapper` back into
`Importer`. The merge is:
- possible (after https://github.com/google/crubit/commit/16a5610d96eda5720bb57b5f927b00f00635ddf0 which means that there is only 1 instance
of `TypeMapper`, because `ImportFields` doesn't anymore need to
temporarily assume that the containing record can be imported),
- desirable (to support b/228868369 we need to let `ConvertType` call
into `Importer::ConvertTemplateSpecializationType`).
This CL makes `TypeMapper::Insert` a private method of `TypeMapper`.
Callers of `TypeMapper::Insert` are transitioned to call the newly
introduced, equivalent `Importer::MarkAsSuccessfullyImported`.
PiperOrigin-RevId: 451509535
diff --git a/rs_bindings_from_cc/decl_importer.h b/rs_bindings_from_cc/decl_importer.h
index d3cb20a..253415b 100644
--- a/rs_bindings_from_cc/decl_importer.h
+++ b/rs_bindings_from_cc/decl_importer.h
@@ -91,22 +91,23 @@
std::optional<absl::string_view> MapKnownCcTypeToRsType(
absl::string_view cc_type) const;
- void Insert(const clang::TypeDecl* decl) {
- known_type_decls_.insert(
- clang::cast<clang::TypeDecl>(decl->getCanonicalDecl()));
- }
-
private:
// TODO(b/209390498): The `friend`ship declaration is temporary - it will
// disappear when TypeMapper class is removed / once TypeMapper is merged back
// into Importer.
friend class Importer;
absl::StatusOr<MappedType> ConvertTypeDecl(const clang::TypeDecl* decl) const;
+
bool Contains(const clang::TypeDecl* decl) const {
return known_type_decls_.contains(
clang::cast<clang::TypeDecl>(decl->getCanonicalDecl()));
}
+ void Insert(const clang::TypeDecl* decl) {
+ known_type_decls_.insert(
+ clang::cast<clang::TypeDecl>(decl->getCanonicalDecl()));
+ }
+
const clang::ASTContext* ctx_;
absl::flat_hash_set<const clang::TypeDecl*> known_type_decls_;
};
@@ -175,9 +176,15 @@
virtual absl::StatusOr<MappedType> ConvertTemplateSpecializationType(
const clang::TemplateSpecializationType* type) = 0;
+ // Marks `decl` as successfully imported. Other pieces of code can check
+ // HasBeenAlreadySuccessfullyImported to avoid introducing dangling ItemIds
+ // that refer to an unimportable `decl`.
+ virtual void MarkAsSuccessfullyImported(const clang::TypeDecl* decl) = 0;
+
// Returns whether the `decl` has been already successfully imported (maybe
// partially - e.g. CXXRecordDeclImporter::Import marks the import as success
- // before importing the fields, because the latter cannot fail).
+ // before importing the fields, because the latter cannot fail). See also
+ // MarkAsSuccessfullyImported.
virtual bool HasBeenAlreadySuccessfullyImported(
const clang::TypeDecl* decl) const = 0;
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index 2043e16..3d193fd 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -885,6 +885,10 @@
}
}
+void Importer::MarkAsSuccessfullyImported(const clang::TypeDecl* decl) {
+ type_mapper_.Insert(decl);
+}
+
bool Importer::HasBeenAlreadySuccessfullyImported(
const clang::TypeDecl* decl) const {
return type_mapper_.Contains(decl);
diff --git a/rs_bindings_from_cc/importer.h b/rs_bindings_from_cc/importer.h
index 1c74d39..0cb2365 100644
--- a/rs_bindings_from_cc/importer.h
+++ b/rs_bindings_from_cc/importer.h
@@ -75,6 +75,7 @@
SourceLoc ConvertSourceLocation(clang::SourceLocation loc) const override;
absl::StatusOr<MappedType> ConvertTemplateSpecializationType(
const clang::TemplateSpecializationType* type) override;
+ void MarkAsSuccessfullyImported(const clang::TypeDecl* decl) override;
bool HasBeenAlreadySuccessfullyImported(
const clang::TypeDecl* decl) const override;
diff --git a/rs_bindings_from_cc/importers/cxx_record.cc b/rs_bindings_from_cc/importers/cxx_record.cc
index 91c8b6f..d2e0908 100644
--- a/rs_bindings_from_cc/importers/cxx_record.cc
+++ b/rs_bindings_from_cc/importers/cxx_record.cc
@@ -87,7 +87,7 @@
record_decl = complete;
} else {
CRUBIT_CHECK(!record_decl->isCompleteDefinition());
- ictx_.type_mapper_.Insert(record_decl);
+ ictx_.MarkAsSuccessfullyImported(record_decl);
return IncompleteRecord{
.cc_name = std::move(cc_name),
.id = GenerateItemId(record_decl),
@@ -97,7 +97,7 @@
// At this point we know that the import of `record_decl` will succeed /
// cannot fail.
- ictx_.type_mapper_.Insert(record_decl);
+ ictx_.MarkAsSuccessfullyImported(record_decl);
ictx_.sema_.ForceDeclarationOfImplicitMembers(record_decl);
diff --git a/rs_bindings_from_cc/importers/typedef_name.cc b/rs_bindings_from_cc/importers/typedef_name.cc
index 4e961fd..4d366c1 100644
--- a/rs_bindings_from_cc/importers/typedef_name.cc
+++ b/rs_bindings_from_cc/importers/typedef_name.cc
@@ -52,7 +52,7 @@
typedef_name_decl,
"Typedef only used to introduce a name in C. Not importing.");
}
- ictx_.type_mapper_.Insert(typedef_name_decl);
+ ictx_.MarkAsSuccessfullyImported(typedef_name_decl);
return TypeAlias{
.identifier = *identifier,
.id = GenerateItemId(typedef_name_decl),