[Sema] TypeWrappers: convert variable init expr into initializer default

All of the stored properties are wrapped which means that their
initializers are subsummed and moved to the synthesized `init`
as default arguments.
This commit is contained in:
Pavel Yaskevich
2022-08-01 16:38:49 -07:00
parent a3b54308d2
commit 39b1566240
4 changed files with 90 additions and 0 deletions

View File

@@ -188,6 +188,28 @@ static void maybeAddMemberwiseDefaultArg(ParamDecl *arg, VarDecl *var,
arg->setDefaultArgumentKind(DefaultArgumentKind::StoredProperty);
}
static void maybeAddTypeWrapperDefaultArg(ParamDecl *arg, VarDecl *var,
ASTContext &ctx) {
assert(var->isAccessedViaTypeWrapper());
if (!var->getParentPattern()->getSingleVar())
return;
auto *PBD = var->getParentPatternBinding();
auto *initExpr = PBD->getInit(/*index=*/0);
if (!initExpr)
return;
// Type wrapper variables are never initialized directly,
// initialization expression (if any) becomes an default
// argument of the initializer synthesized by the type wrapper.
PBD->setInitializerSubsumed(/*index=*/0);
arg->setDefaultExpr(initExpr, /*isTypeChecked=*/false);
arg->setDefaultArgumentKind(DefaultArgumentKind::Normal);
}
/// Describes the kind of implicit constructor that will be
/// generated.
enum class ImplicitConstructorKind {
@@ -339,6 +361,8 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
arg->setInterfaceType(var->getValueInterfaceType());
arg->setImplicit();
maybeAddTypeWrapperDefaultArg(arg, var, ctx);
params.push_back(arg);
}
}