@@ -588,22 +588,10 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
588588 StrCat (keyword::kMut );
589589 }
590590 StrCat (name, token::kColon );
591-
592- bool is_parm_with_default_value = false ;
593- if (auto parm = clang::dyn_cast<clang::ParmVarDecl>(decl)) {
594- is_parm_with_default_value = HasUsableDefaultArg (parm);
595- }
596-
597- if (is_parm_with_default_value) {
598- StrCat (" Option<" );
599- }
600591 {
601592 PushLazyType lazy (*this , IsGlobalVar (decl) && LazyStaticInit ());
602593 Convert (qual_type);
603594 }
604- if (is_parm_with_default_value) {
605- StrCat (' >' );
606- }
607595 return true ;
608596}
609597
@@ -1028,9 +1016,9 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) {
10281016 !type->isReferenceType () ? " value"
10291017 : is_move || type.getNonReferenceType ().isConstQualified () ? " ptr"
10301018 : " mut_ptr" ;
1031- StrCat (std::format (
1032- " #[{}({}, {}, {}{})] " , is_move ? " cpp_from_move" : " cpp_from" , source,
1033- GetCtorName (ctor), param-> hasDefaultArg () ? " some_ " : " " , kind));
1019+ StrCat (std::format (" #[{}({}, {}, {})] " ,
1020+ is_move ? " cpp_from_move" : " cpp_from" , source,
1021+ GetCtorName (ctor) , kind));
10341022 });
10351023 std::unordered_set<std::string> into_types;
10361024 ForEachMethod (decl, [&](clang::CXXMethodDecl *method) {
@@ -1310,41 +1298,6 @@ bool Converter::VisitFieldDecl(clang::FieldDecl *decl) {
13101298 return false ;
13111299}
13121300
1313- std::string
1314- Converter::EmitDefaultArgStorage (const clang::ParmVarDecl *param,
1315- const clang::MaterializeTemporaryExpr *temp) {
1316- auto storage = std::format (" __{}_default" , GetNamedDeclAsString (param));
1317- StrCat (std::format (" let mut {} : Option<{}> = None" , storage,
1318- ToString (temp->getType ().getUnqualifiedType ())),
1319- token::kSemiColon );
1320- return storage;
1321- }
1322-
1323- void Converter::EmitFunctionPreamble (clang::FunctionDecl *decl) {
1324- for (auto *param : DefinitionParameters (decl)) {
1325- if (!HasUsableDefaultArg (param)) {
1326- continue ;
1327- }
1328- auto name = GetNamedDeclAsString (param);
1329- auto type = ToString (param->getType ());
1330- std::string value;
1331- if (auto *temp = GetDefaultArgTemporary (param)) {
1332- auto storage = EmitDefaultArgStorage (param, temp);
1333- value = std::format (" {}.insert({}) as {}" , storage,
1334- ToString (temp->getSubExpr ()), type);
1335- } else if (param->getType ()->isReferenceType ()) {
1336- Buffer buf (*this );
1337- ConvertVarInit (param->getType (), param->getDefaultArg ());
1338- value = std::move (buf).str ();
1339- } else {
1340- value = ToString (param->getDefaultArg ());
1341- }
1342- StrCat (std::format (" let mut {} : {} = {}.unwrap_or_else(|| {} {{ {} }})" ,
1343- name, type, name, keyword_unsafe_, value),
1344- token::kSemiColon );
1345- }
1346- }
1347-
13481301void Converter::ConvertDeclContext (clang::DeclContext *decl) {
13491302 for (auto *child : decl->decls ()) {
13501303 if (IsInMainFile (child) || !decl_ids_.contains (GetID (child))) {
@@ -2133,6 +2086,9 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) {
21332086
21342087 for (unsigned i = 0 ; i < num_named_params && i < num_args; ++i) {
21352088 auto *arg = expr->getArg (i + arg_begin);
2089+ if (auto *default_arg = clang::dyn_cast<clang::CXXDefaultArgExpr>(arg)) {
2090+ arg = default_arg->getExpr ();
2091+ }
21362092 CallArg ca{
21372093 .param_name =
21382094 function && !function->getParamDecl (i)->getName ().empty ()
@@ -2141,8 +2097,6 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) {
21412097 .param_type = function ? function->getParamDecl (i)->getType ()
21422098 : proto->getParamType (i),
21432099 .expr = arg,
2144- .has_default =
2145- function && HasUsableDefaultArg (function->getParamDecl (i)),
21462100 .kind = (IsLiteral (arg) || info.is_libc_passthrough ) ? Kind::Inline
21472101 : Kind::Hoisted,
21482102 };
@@ -2246,33 +2200,19 @@ void Converter::EmitArgList(const CallInfo &info) {
22462200
22472201 for (unsigned i = 0 ; i < info.args .size (); i++) {
22482202 const auto &ca = info.args [i];
2249-
2250- if (ca.has_default && clang::isa<clang::CXXDefaultArgExpr>(ca.expr )) {
2251- StrCat (" None" , token::kComma );
2252- continue ;
2253- }
2254-
2255- if (ca.has_default ) {
2256- StrCat (" Some" );
2257- }
2258-
2259- {
2260- PushParen push (*this , ca.has_default );
2261- switch (ca.kind ) {
2262- case Kind::Hoisted:
2263- StrCat (ca.param_name );
2264- break ;
2265- case Kind::Materialized:
2266- StrCat (ca.ref_temp_name );
2267- break ;
2268- case Kind::Inline:
2269- ConvertParamTy (ca.param_type , ca.expr );
2270- if (info.is_libc_passthrough ) {
2271- StrCat (
2272- std::format (" as {}" , Mapper::GetParamType (*this , info.expr , i)));
2273- }
2274- break ;
2203+ switch (ca.kind ) {
2204+ case Kind::Hoisted:
2205+ StrCat (ca.param_name );
2206+ break ;
2207+ case Kind::Materialized:
2208+ StrCat (ca.ref_temp_name );
2209+ break ;
2210+ case Kind::Inline:
2211+ ConvertParamTy (ca.param_type , ca.expr );
2212+ if (info.is_libc_passthrough ) {
2213+ StrCat (std::format (" as {}" , Mapper::GetParamType (*this , info.expr , i)));
22752214 }
2215+ break ;
22762216 }
22772217
22782218 StrCat (token::kComma );
@@ -4110,31 +4050,15 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) {
41104050 GetCtorName (ctor));
41114051 PushParen paren (*this );
41124052
4113- unsigned arg_idx = 0 ;
4114- for (unsigned param_idx = 0 ; param_idx < ctor->getNumParams (); ++param_idx) {
4115- auto param = ctor->getParamDecl (param_idx);
4116- auto param_type = param->getType ();
4117- bool has_default = HasUsableDefaultArg (param);
4118-
4119- if (arg_idx < expr->getNumArgs () &&
4120- clang::isa<clang::CXXDefaultArgExpr>(expr->getArg (arg_idx))) {
4121- assert (has_default);
4122- ++arg_idx;
4123- StrCat (" None" , token::kComma );
4124- continue ;
4053+ for (unsigned i = 0 ; i < ctor->getNumParams (); ++i) {
4054+ assert (i < expr->getNumArgs ());
4055+ clang::Expr *arg = expr->getArg (i);
4056+ if (auto *default_arg = clang::dyn_cast<clang::CXXDefaultArgExpr>(arg)) {
4057+ arg = default_arg->getExpr ();
41254058 }
4126-
4127- if (arg_idx < expr->getNumArgs ()) {
4128- clang::Expr *arg = expr->getArg (arg_idx++);
4059+ {
41294060 PushBrace brace (*this );
4130- if (has_default) {
4131- StrCat (" Some" );
4132- }
4133- PushParen some (*this , has_default);
4134- ConvertVarInit (param_type, arg);
4135- } else {
4136- assert (has_default);
4137- StrCat (" None" );
4061+ ConvertVarInit (ctor->getParamDecl (i)->getType (), arg);
41384062 }
41394063 StrCat (token::kComma );
41404064 }
@@ -5215,8 +5139,13 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) {
52155139 auto *cxx = clang::dyn_cast<clang::CXXRecordDecl>(decl);
52165140 if (auto *default_ctor =
52175141 cxx ? GetUserDefinedDefaultConstructor (cxx) : nullptr ) {
5142+ std::vector<clang::Expr *> args;
5143+ for (auto *param : default_ctor->parameters ()) {
5144+ args.push_back (clang::CXXDefaultArgExpr::Create (
5145+ ctx_, param->getLocation (), param, nullptr , default_ctor));
5146+ }
52185147 Convert (MakeConstructExpr (ctx_, ctx_.getCanonicalTagType (decl),
5219- default_ctor, {} ));
5148+ default_ctor, args ));
52205149 } else {
52215150 EmitDefaultStructLiteral (decl);
52225151 }
0 commit comments