rs_bindings_from_cc: Use an attribute to dynamically specify type mapping.
---
This Cl allows you to override Crubit's type-mapping, so that instead of generating bindings for a type `X`, it instead looks up an already-existing type `Y`. This is useful, for example:
* For specialty types like `rs_char`, where the other language's equivalent is a builtin type.
* For types with native language support, like protocol buffers, where we want to use the native languages' proto rather than generated bindings
* For types which are themselves bindings: the bindings for the bindings for `X` should be `X` again, not an infinite-turtle bindings-of-bindings situation.
This is the first step to all of those: for calling C++ from Rust, we allow C++ types to override which Rust type they are.
A rough TODO list of next steps:
1. suppress bindings generation for the record itself, when annotated with `crubit_rust_type`.
2. apply `crubit_rust_type` annotations to generated bindings, so that bindings-of-bindings can DTRT.
3. do ~something about crubit features. Currently they would block round-tripped bindings.
4. rinse and repeat for cc_bindings_from_rs.
PiperOrigin-RevId: 529212737
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index 3afb187..94fb472 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -33,7 +33,7 @@
#include "rs_bindings_from_cc/ast_util.h"
#include "rs_bindings_from_cc/bazel_types.h"
#include "rs_bindings_from_cc/ir.h"
-#include "rs_bindings_from_cc/known_types_map.h"
+#include "rs_bindings_from_cc/type_map.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
@@ -759,9 +759,9 @@
// Qualifiers are handled separately in ConvertQualType().
std::string type_string = clang::QualType(type, 0).getAsString();
- if (auto maybe_mapped_type = MapKnownCcTypeToRsType(type_string);
- maybe_mapped_type.has_value()) {
- return MappedType::Simple(std::string(*maybe_mapped_type), type_string);
+ CRUBIT_ASSIGN_OR_RETURN(auto override_type, TypeMapOverride(*type));
+ if (override_type.has_value()) {
+ return *std::move(override_type);
} else if (type->isPointerType() || type->isLValueReferenceType() ||
type->isRValueReferenceType()) {
clang::QualType pointee_type = type->getPointeeType();