@@ -458,110 +458,6 @@ void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
458458 }
459459}
460460
461- void addBuiltinTypes (Model model) {
462- assert (ctx_);
463-
464- auto add_scalar_rule = [&](const std::string &cxx, const std::string &rust,
465- const std::string &initializer = {}) {
466- auto plain = TranslationRule::TypeRule::Plain (rust);
467- plain.initializer = initializer;
468- std::vector<std::string> derives = {" Copy" , " Clone" , " Default" ,
469- " Debug" , " PartialEq" , " PartialOrd" };
470- if (!(rust == " f32" || rust == " f64" )) {
471- derives.insert (derives.end (), {" Eq" , " Ord" , " Hash" });
472- }
473- plain.type_info .derives = std::move (derives);
474- AddTypeRule (cxx, TranslationRule::TypeRule (plain));
475- AddTypeRule (" const " + cxx, std::move (plain));
476-
477- switch (model) {
478- case Model::kUnsafe :
479- AddTypeRule (cxx + " *" ,
480- TranslationRule::TypeRule::UnsafePtr (" *mut " + rust));
481- AddTypeRule (" const " + cxx + " *" ,
482- TranslationRule::TypeRule::UnsafePtr (" *const " + rust));
483- break ;
484- case Model::kRefCount :
485- AddTypeRule (cxx + " *" , TranslationRule::TypeRule::RefcountPtr (
486- " Ptr::<" + rust + " >" ));
487- AddTypeRule (" const " + cxx + " *" , TranslationRule::TypeRule::RefcountPtr (
488- " Ptr::<" + rust + " >" ));
489- break ;
490- }
491- };
492-
493- auto add_builtin_rule = [&](clang::QualType qt, const std::string &rust) {
494- add_scalar_rule (ToString (qt), rust);
495- };
496-
497- auto add_size_rules = [&](clang::QualType size_type,
498- std::initializer_list<const char *> aliases,
499- const std::string &rust) {
500- auto initializer = " 0_" + rust;
501- for (const char *alias : aliases) {
502- add_scalar_rule (alias, rust, initializer);
503- }
504- if (const auto *predef = clang::dyn_cast<clang::PredefinedSugarType>(
505- size_type.getTypePtr ())) {
506- add_scalar_rule (predef->getIdentifier ()->getName ().str (), rust,
507- initializer);
508- }
509- };
510-
511- auto build_rust_type = [&](clang::QualType qt) {
512- unsigned bits = ctx_->getTypeSize (qt);
513- char sign = qt->isSignedIntegerType () ? ' i' : ' u' ;
514- return std::format (" {}{}" , sign, bits);
515- };
516-
517- // Misc
518- add_builtin_rule (ctx_->BoolTy , " bool" );
519- add_builtin_rule (ctx_->FloatTy , " f32" );
520- add_builtin_rule (ctx_->DoubleTy , " f64" );
521-
522- switch (model) {
523- case Model::kUnsafe :
524- AddTypeRule (ToString (ctx_->VoidTy ) + " *" ,
525- TranslationRule::TypeRule::UnsafePtr (" *mut ::libc::c_void" ));
526- AddTypeRule (" const " + ToString (ctx_->VoidTy ) + " *" ,
527- TranslationRule::TypeRule::UnsafePtr (" *const ::libc::c_void" ));
528- break ;
529- case Model::kRefCount :
530- AddTypeRule (ToString (ctx_->VoidTy ) + " *" ,
531- TranslationRule::TypeRule::RefcountPtr (" AnyPtr" ));
532- AddTypeRule (" const " + ToString (ctx_->VoidTy ) + " *" ,
533- TranslationRule::TypeRule::RefcountPtr (" AnyPtr" ));
534- break ;
535- }
536-
537- // Char
538- switch (model) {
539- case Model::kUnsafe :
540- add_builtin_rule (ctx_->CharTy , " libc::c_char" );
541- break ;
542- case Model::kRefCount :
543- add_builtin_rule (ctx_->CharTy , " u8" );
544- break ;
545- }
546- add_builtin_rule (ctx_->SignedCharTy , " i8" );
547- add_builtin_rule (ctx_->UnsignedCharTy , " u8" );
548-
549- // Integers
550- add_builtin_rule (ctx_->ShortTy , build_rust_type (ctx_->ShortTy ));
551- add_builtin_rule (ctx_->UnsignedShortTy ,
552- build_rust_type (ctx_->UnsignedShortTy ));
553- add_builtin_rule (ctx_->IntTy , build_rust_type (ctx_->IntTy ));
554- add_builtin_rule (ctx_->UnsignedIntTy , build_rust_type (ctx_->UnsignedIntTy ));
555- add_builtin_rule (ctx_->LongTy , build_rust_type (ctx_->LongTy ));
556- add_builtin_rule (ctx_->UnsignedLongTy , build_rust_type (ctx_->UnsignedLongTy ));
557- add_builtin_rule (ctx_->LongLongTy , build_rust_type (ctx_->LongLongTy ));
558- add_builtin_rule (ctx_->UnsignedLongLongTy ,
559- build_rust_type (ctx_->UnsignedLongLongTy ));
560-
561- add_size_rules (ctx_->getSizeType (), {" size_t" , " size_type" }, " usize" );
562- add_size_rules (ctx_->getSignedSizeType (), {" ssize_t" }, " isize" );
563- }
564-
565461clang::QualType normalizeQualType (clang::QualType qual_type) {
566462 assert (ctx_);
567463
@@ -862,7 +758,10 @@ std::string ToString(clang::QualType qual_type, ScalarSugar sugar) {
862758 } else if (const auto *ptr = t->getAs <clang::PointerType>()) {
863759 auto pointee = ptr->getPointeeType ();
864760 auto canonical = pointee.getCanonicalType ().getDesugaredType (*ctx_);
865- if (Map (pointee) == Map (canonical)) {
761+ bool builtin_alias = canonical->isBuiltinType () &&
762+ (pointee->getAs <clang::TypedefType>() ||
763+ pointee->getAs <clang::PredefinedSugarType>());
764+ if (!builtin_alias && Map (pointee) == Map (canonical)) {
866765 pointee = canonical;
867766 }
868767 std::string out;
@@ -1123,7 +1022,6 @@ void LoadTranslationRules(Model model, clang::ASTContext &ctx,
11231022 }
11241023 translation_rules_loaded_ = true ;
11251024
1126- addBuiltinTypes (model);
11271025 addRulesFromDirectory (rules_dir, model);
11281026
11291027#if 0
0 commit comments