Merge `TypeMapper` back into `Importer` - part 6/N: `MapKnownCcTypeToRsType`.
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 transitions the `TypeMapper::MapKnownCcTypeToRsType` into a free
function. This is/was the last method of the `TypeMapper`, so the CL
goes one step further and removes the `TypeMapper` class altogether.
PiperOrigin-RevId: 451513327
diff --git a/rs_bindings_from_cc/decl_importer.h b/rs_bindings_from_cc/decl_importer.h
index 59c2bed..27982a9 100644
--- a/rs_bindings_from_cc/decl_importer.h
+++ b/rs_bindings_from_cc/decl_importer.h
@@ -59,27 +59,10 @@
header_targets_;
};
-// The currently known canonical type decls that we know how to map into
-// Rust.
-//
-// TODO(b/228868369): Merge TypeMapper back into Importer.
-class TypeMapper {
- public:
- TypeMapper(const clang::ASTContext* ctx) : ctx_(ctx) {}
-
- TypeMapper(const TypeMapper& other) = default;
- TypeMapper& operator=(const TypeMapper& other) = default;
-
- // TODO(b/209390498): This method doesn't use any member variables or member
- // functions - it should be a static method or a free function instead.
- std::optional<absl::string_view> MapKnownCcTypeToRsType(
- absl::string_view cc_type) const;
-
- private:
- // TODO(b/209390498): The `ctx_` field is unused - it will disappear when
- // TypeMapper class is removed / once TypeMapper is merged back into Importer.
- const clang::ASTContext* ctx_;
-};
+// Converts primitive types like `std::usize` or `int64_t` into their Rust
+// equivalents.
+std::optional<absl::string_view> MapKnownCcTypeToRsType(
+ absl::string_view cc_type);
// Explicitly defined interface that defines how `DeclImporter`s are allowed to
// interface with the global state of the importer.
@@ -87,7 +70,7 @@
public:
ImportContext(Invocation& invocation, clang::ASTContext& ctx,
clang::Sema& sema)
- : invocation_(invocation), ctx_(ctx), sema_(sema), type_mapper_(&ctx) {}
+ : invocation_(invocation), ctx_(ctx), sema_(sema) {}
virtual ~ImportContext() {}
// Imports all decls contained in a `DeclContext`.
@@ -175,7 +158,6 @@
Invocation& invocation_;
clang::ASTContext& ctx_;
clang::Sema& sema_;
- TypeMapper type_mapper_;
};
// Interface for components that can import decls of a certain category.
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index 8e966e8..521f83d 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -70,8 +70,8 @@
// A mapping of C++ standard types to their equivalent Rust types.
// To produce more idiomatic results, these types receive special handling
// instead of using the generic type mapping mechanism.
-std::optional<absl::string_view> TypeMapper::MapKnownCcTypeToRsType(
- absl::string_view cc_type) const {
+std::optional<absl::string_view> MapKnownCcTypeToRsType(
+ absl::string_view cc_type) {
static const auto* const kWellKnownTypes =
new absl::flat_hash_map<absl::string_view, absl::string_view>({
{"ptrdiff_t", "isize"},
@@ -630,7 +630,7 @@
// Qualifiers are handled separately in ConvertQualType().
std::string type_string = clang::QualType(type, 0).getAsString();
- if (auto maybe_mapped_type = type_mapper_.MapKnownCcTypeToRsType(type_string);
+ if (auto maybe_mapped_type = MapKnownCcTypeToRsType(type_string);
maybe_mapped_type.has_value()) {
return MappedType::Simple(std::string(*maybe_mapped_type), type_string);
} else if (type->isPointerType() || type->isLValueReferenceType() ||
diff --git a/rs_bindings_from_cc/importers/typedef_name.cc b/rs_bindings_from_cc/importers/typedef_name.cc
index dfc31f8..e0f39b8 100644
--- a/rs_bindings_from_cc/importers/typedef_name.cc
+++ b/rs_bindings_from_cc/importers/typedef_name.cc
@@ -24,8 +24,7 @@
clang::QualType type =
typedef_name_decl->getASTContext().getTypedefType(typedef_name_decl);
- if (ictx_.type_mapper_.MapKnownCcTypeToRsType(type.getAsString())
- .has_value()) {
+ if (MapKnownCcTypeToRsType(type.getAsString()).has_value()) {
return std::nullopt;
}