Skip to content

Commit 20a52d2

Browse files
committed
[Sema] TypeWrappers/NFC: Move initializer synthesis into a request
1 parent ecc363d commit 20a52d2

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,6 +3655,22 @@ class SynthesizeTypeWrapperInitializer
36553655
bool isCached() const { return true; }
36563656
};
36573657

3658+
class SynthesizeTypeWrapperInitializerBody
3659+
: public SimpleRequest<SynthesizeTypeWrapperInitializerBody,
3660+
BraceStmt *(ConstructorDecl *),
3661+
RequestFlags::Cached> {
3662+
public:
3663+
using SimpleRequest::SimpleRequest;
3664+
3665+
private:
3666+
friend SimpleRequest;
3667+
3668+
BraceStmt *evaluate(Evaluator &evaluator, ConstructorDecl *) const;
3669+
3670+
public:
3671+
bool isCached() const { return true; }
3672+
};
3673+
36583674
void simple_display(llvm::raw_ostream &out, ASTNode node);
36593675
void simple_display(llvm::raw_ostream &out, Type value);
36603676
void simple_display(llvm::raw_ostream &out, const TypeRepr *TyR);

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,6 @@ SWIFT_REQUEST(TypeChecker, IsPropertyAccessedViaTypeWrapper,
426426
SWIFT_REQUEST(TypeChecker, SynthesizeTypeWrapperInitializer,
427427
ConstructorDecl *(NominalTypeDecl *),
428428
Cached, NoLocationInfo)
429+
SWIFT_REQUEST(TypeChecker, SynthesizeTypeWrapperInitializerBody,
430+
BraceStmt *(ConstructorDecl *),
431+
Cached, NoLocationInfo)

lib/Sema/CodeSynthesis.cpp

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,63 +1496,6 @@ void swift::addNonIsolatedToSynthesized(
14961496
value->getAttrs().add(new (ctx) NonisolatedAttr(/*isImplicit=*/true));
14971497
}
14981498

1499-
static std::pair<BraceStmt *, bool>
1500-
synthesizeTypeWrapperInitializerBody(AbstractFunctionDecl *fn, void *context) {
1501-
auto *ctor = cast<ConstructorDecl>(fn);
1502-
auto &ctx = ctor->getASTContext();
1503-
auto *parent = ctor->getDeclContext()->getSelfNominalTypeDecl();
1504-
1505-
// self.$_storage = .init(memberwise: $Storage(...))
1506-
auto *storageType =
1507-
evaluateOrDefault(ctx.evaluator, GetTypeWrapperStorage{parent}, nullptr);
1508-
assert(storageType);
1509-
1510-
auto *typeWrapperVar =
1511-
evaluateOrDefault(ctx.evaluator, GetTypeWrapperProperty{parent}, nullptr);
1512-
assert(typeWrapperVar);
1513-
1514-
auto *storageVarRef = UnresolvedDotExpr::createImplicit(
1515-
ctx,
1516-
new (ctx) DeclRefExpr({ctor->getImplicitSelfDecl()},
1517-
/*Loc=*/DeclNameLoc(), /*Implicit=*/true),
1518-
typeWrapperVar->getName());
1519-
1520-
SmallVector<Argument, 4> arguments;
1521-
{
1522-
for (auto *param : *ctor->getParameters()) {
1523-
arguments.push_back({/*labelLoc=*/SourceLoc(), param->getName(),
1524-
new (ctx) DeclRefExpr(param, /*Loc=*/DeclNameLoc(),
1525-
/*Implicit=*/true)});
1526-
}
1527-
}
1528-
1529-
auto *storageInit = CallExpr::createImplicit(
1530-
ctx,
1531-
TypeExpr::createImplicitForDecl(
1532-
/*Loc=*/DeclNameLoc(), storageType, ctor,
1533-
ctor->mapTypeIntoContext(storageType->getInterfaceType())),
1534-
ArgumentList::createImplicit(ctx, arguments));
1535-
1536-
auto *initRef = new (ctx) UnresolvedMemberExpr(
1537-
/*dotLoc=*/SourceLoc(), /*declNameLoc=*/DeclNameLoc(),
1538-
DeclNameRef::createConstructor(), /*implicit=*/true);
1539-
{ initRef->setFunctionRefKind(FunctionRefKind::DoubleApply); }
1540-
1541-
// .init($Storage(...))
1542-
Expr *typeWrapperInit = CallExpr::createImplicit(
1543-
ctx, initRef,
1544-
ArgumentList::forImplicitSingle(ctx, ctx.Id_memberwise, storageInit));
1545-
1546-
auto *assignment = new (ctx)
1547-
AssignExpr(storageVarRef, /*EqualLoc=*/SourceLoc(), typeWrapperInit,
1548-
/*Implicit=*/true);
1549-
1550-
return {BraceStmt::create(ctx, /*lbloc=*/ctor->getLoc(),
1551-
/*body=*/{assignment},
1552-
/*rbloc=*/ctor->getLoc(), /*implicit=*/true),
1553-
/*isTypeChecked=*/false};
1554-
}
1555-
15561499
ConstructorDecl *
15571500
SynthesizeTypeWrapperInitializer::evaluate(Evaluator &evaluator,
15581501
NominalTypeDecl *wrappedType) const {
@@ -1565,6 +1508,11 @@ SynthesizeTypeWrapperInitializer::evaluate(Evaluator &evaluator,
15651508
wrappedType, ImplicitConstructorKind::TypeWrapper, ctx);
15661509
wrappedType->addMember(ctor);
15671510

1568-
ctor->setBodySynthesizer(synthesizeTypeWrapperInitializerBody);
1511+
auto *body = evaluateOrDefault(
1512+
evaluator, SynthesizeTypeWrapperInitializerBody{ctor}, nullptr);
1513+
if (!body)
1514+
return nullptr;
1515+
1516+
ctor->setBody(body, AbstractFunctionDecl::BodyKind::Parsed);
15691517
return ctor;
15701518
}

lib/Sema/TypeCheckTypeWrapper.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,58 @@ bool IsPropertyAccessedViaTypeWrapper::evaluate(Evaluator &evaluator,
365365

366366
return true;
367367
}
368+
369+
BraceStmt *
370+
SynthesizeTypeWrapperInitializerBody::evaluate(Evaluator &evaluator,
371+
ConstructorDecl *ctor) const {
372+
auto &ctx = ctor->getASTContext();
373+
auto *parent = ctor->getDeclContext()->getSelfNominalTypeDecl();
374+
375+
// self.$_storage = .init(memberwise: $Storage(...))
376+
auto *storageType =
377+
evaluateOrDefault(ctx.evaluator, GetTypeWrapperStorage{parent}, nullptr);
378+
assert(storageType);
379+
380+
auto *typeWrapperVar = parent->getTypeWrapperProperty();
381+
assert(typeWrapperVar);
382+
383+
auto *storageVarRef = UnresolvedDotExpr::createImplicit(
384+
ctx,
385+
new (ctx) DeclRefExpr({ctor->getImplicitSelfDecl()},
386+
/*Loc=*/DeclNameLoc(), /*Implicit=*/true),
387+
typeWrapperVar->getName());
388+
389+
SmallVector<Argument, 4> arguments;
390+
{
391+
for (auto *param : *ctor->getParameters()) {
392+
arguments.push_back({/*labelLoc=*/SourceLoc(), param->getName(),
393+
new (ctx) DeclRefExpr(param, /*Loc=*/DeclNameLoc(),
394+
/*Implicit=*/true)});
395+
}
396+
}
397+
398+
auto *storageInit = CallExpr::createImplicit(
399+
ctx,
400+
TypeExpr::createImplicitForDecl(
401+
/*Loc=*/DeclNameLoc(), storageType, ctor,
402+
ctor->mapTypeIntoContext(storageType->getInterfaceType())),
403+
ArgumentList::createImplicit(ctx, arguments));
404+
405+
auto *initRef = new (ctx) UnresolvedMemberExpr(
406+
/*dotLoc=*/SourceLoc(), /*declNameLoc=*/DeclNameLoc(),
407+
DeclNameRef::createConstructor(), /*implicit=*/true);
408+
{ initRef->setFunctionRefKind(FunctionRefKind::DoubleApply); }
409+
410+
// .init($Storage(...))
411+
Expr *typeWrapperInit = CallExpr::createImplicit(
412+
ctx, initRef,
413+
ArgumentList::forImplicitSingle(ctx, ctx.Id_memberwise, storageInit));
414+
415+
auto *assignment = new (ctx)
416+
AssignExpr(storageVarRef, /*EqualLoc=*/SourceLoc(), typeWrapperInit,
417+
/*Implicit=*/true);
418+
419+
return BraceStmt::create(ctx, /*lbloc=*/ctor->getLoc(),
420+
/*body=*/{assignment},
421+
/*rbloc=*/ctor->getLoc(), /*implicit=*/true);
422+
}

0 commit comments

Comments
 (0)