[Function builders] Add support for function builders on stored struct properties.

Implements SR-13188, which is part of the function builders proposal
under review.
This commit is contained in:
Doug Gregor
2020-09-26 21:54:28 -07:00
parent b03457ddf3
commit bec0aae7e5
5 changed files with 130 additions and 2 deletions

View File

@@ -261,6 +261,20 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
}
}
Type functionBuilderType= var->getFunctionBuilderType();
if (functionBuilderType) {
// If the variable's type is structurally a function type, use that
// type. Otherwise, form a non-escaping function type for the function
// parameter.
bool isStructuralFunctionType =
varInterfaceType->lookThroughAllOptionalTypes()
->is<AnyFunctionType>();
if (!isStructuralFunctionType) {
auto extInfo = ASTExtInfoBuilder().withNoEscape().build();
varInterfaceType = FunctionType::get({ }, varInterfaceType, extInfo);
}
}
// Create the parameter.
auto *arg = new (ctx)
ParamDecl(SourceLoc(), Loc,
@@ -273,6 +287,14 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
// Don't allow the parameter to accept temporary pointer conversions.
arg->setNonEphemeralIfPossible();
// Attach a function builder attribute if needed.
if (functionBuilderType) {
auto typeExpr = TypeExpr::createImplicit(functionBuilderType, ctx);
auto attr = CustomAttr::create(
ctx, SourceLoc(), typeExpr, /*implicit=*/true);
arg->getAttrs().add(attr);
}
maybeAddMemberwiseDefaultArg(arg, var, params.size(), ctx);
params.push_back(arg);