@@ -134,6 +134,10 @@ bool Converter::Convert(clang::QualType qual_type) {
134134 return TraverseType (qual_type);
135135}
136136
137+ std::string Converter::GetUnboxedTypeAsString (clang::QualType qual_type) {
138+ return ToString (qual_type);
139+ }
140+
137141bool Converter::ConvertMappedType (clang::QualType qual_type) {
138142 std::string type_as_string = Mapper::Map (*this , qual_type);
139143 if (type_as_string == token::kIgnoreRule ) {
@@ -181,7 +185,11 @@ bool Converter::VisitRecordType(clang::RecordType *type) {
181185 }
182186
183187 StrCat (GetRecordName (decl));
184- RuleRegistry::AddRuleForUserDefinedType (ctx_, decl);
188+ return false ;
189+ }
190+
191+ bool Converter::VisitEnumType (clang::EnumType *type) {
192+ StrCat (GetRecordName (type->getDecl ()));
185193 return false ;
186194}
187195
@@ -731,11 +739,19 @@ bool Converter::IsPassThroughRule(clang::Expr *expr) const {
731739 rule->body [0 ]);
732740}
733741
734- bool Converter::RecordDerivesCopy (const clang::RecordDecl *decl) const {
735- auto *derives =
736- RuleRegistry::MappedDerives (ctx_, ctx_.getCanonicalTagType (decl));
737- return derives &&
738- std::find (derives->begin (), derives->end (), " Copy" ) != derives->end ();
742+ bool Converter::RecordDerivesCopy (const clang::RecordDecl *decl) {
743+ if (auto rule =
744+ RuleRegistry::Search (ctx_, ctx_.getCanonicalTagType (decl)).first ) {
745+ auto &derives = rule->type_info .derives ;
746+ return std::find (derives.begin (), derives.end (), " Copy" ) != derives.end ();
747+ }
748+ decl = decl->getDefinition ();
749+ if (!decl) {
750+ return false ;
751+ }
752+ auto attrs = GetStructAttributes (decl);
753+ return std::find (attrs.begin (), attrs.end (), std::string_view (" Copy" )) !=
754+ attrs.end ();
739755}
740756
741757bool Converter::RecordHasCopyableFields (const clang::RecordDecl *decl) {
@@ -794,7 +810,6 @@ bool Converter::VisitRecordDecl(clang::RecordDecl *decl) {
794810 return false ;
795811 }
796812
797- RuleRegistry::AddRuleForUserDefinedType (ctx_, decl);
798813 EmitRustStructOrUnion (decl);
799814
800815 return false ;
@@ -861,9 +876,6 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) {
861876 EmitReprC (decl);
862877 }
863878 auto attrs = GetStructAttributes (decl);
864- RuleRegistry::SetDerives (
865- ctx_, ctx_.getCanonicalTagType (decl),
866- std::vector<std::string>(attrs.begin (), attrs.end ()));
867879 StrCat (" #[derive(" );
868880 for (auto *attr : attrs) {
869881 StrCat (attr, ' ,' );
@@ -961,9 +973,6 @@ void Converter::EmitReprC(clang::RecordDecl *decl) {
961973void Converter::EmitRustUnion (clang::RecordDecl *decl) {
962974 EmitReprC (decl);
963975 auto attrs = GetStructAttributes (decl);
964- RuleRegistry::SetDerives (
965- ctx_, ctx_.getCanonicalTagType (decl),
966- std::vector<std::string>(attrs.begin (), attrs.end ()));
967976 StrCat (" #[derive(" );
968977 for (auto *attr : attrs) {
969978 StrCat (attr, ' ,' );
@@ -987,7 +996,6 @@ void Converter::EmitRustUnion(clang::RecordDecl *decl) {
987996bool Converter::VisitCXXRecordDecl (clang::CXXRecordDecl *decl) {
988997 decl->dump (log ());
989998
990- RuleRegistry::AddRuleForUserDefinedType (ctx_, decl);
991999 if (!IsConvertibleCXXRecordDecl (decl)) {
9921000 return false ;
9931001 }
@@ -1943,7 +1951,7 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
19431951 }
19441952
19451953 auto ty = GetReturnTypeOfFunction (expr);
1946- auto ref = clang::dyn_cast <clang::ReferenceType>(ty );
1954+ auto ref = ty-> getAs <clang::ReferenceType>();
19471955
19481956 if (ref && !isAddrOf () && !isVoid ()) {
19491957 {
@@ -3198,6 +3206,10 @@ std::string Converter::ConvertDeclRef(clang::Expr *expr,
31983206
31993207 if (ShouldReplaceWithMappedBody (decl)) {
32003208 if (auto str = GetMappedAsString (expr); !str.empty ()) {
3209+ if (RuleRegistry::ReturnsReference (expr) && !isAddrOf ()) {
3210+ return std::format (" ({}{})" , GetPointerDerefPrefix (expr->getType ()),
3211+ str);
3212+ }
32013213 return str;
32023214 }
32033215 }
@@ -3950,7 +3962,6 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
39503962 if (Mapper::Contains (ctx_, ctx_.getCanonicalTagType (decl))) {
39513963 return false ;
39523964 }
3953- RuleRegistry::AddRuleForUserDefinedType (ctx_, decl);
39543965 auto name = GetRecordName (decl);
39553966 StrCat (std::format (" pub type {} = {};" , name,
39563967 GetUnsafeTypeAsString (decl->getIntegerType ())));
@@ -5316,13 +5327,10 @@ std::string
53165327Converter::ConvertInitFragment (clang::Expr *expr,
53175328 const std::vector<clang::Expr *> &all_args) {
53185329 const auto *tgt_ir = RuleRegistry::GetExprRule (ctx_, expr);
5319- assert (tgt_ir && tgt_ir->init_type .valid ());
5320- auto *callee = clang::cast<clang::CallExpr>(expr)->getDirectCallee ();
5321- assert (callee);
5322- auto type = GetSema ()
5323- .getTemplateInstantiationArgs (callee)(tgt_ir->init_type .depth ,
5324- tgt_ir->init_type .index )
5325- .getAsType ();
5330+ if (!tgt_ir || !tgt_ir->usesInit ()) {
5331+ llvm::report_fatal_error (" expression rule does not use Init" );
5332+ }
5333+ auto type = RuleRegistry::GetInitType (expr);
53265334
53275335 Buffer buf (*this );
53285336 ConvertConstructFromArgs (
0 commit comments