Skip to content

Commit 224708d

Browse files
committed
[AST] TypeWrappers: Add request to retrieve type wrapper initializer
1 parent 92fbb3e commit 224708d

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,6 +3780,10 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
37803780
/// by the wrapper.
37813781
NominalTypeDecl *getTypeWrapperStorageDecl() const;
37823782

3783+
/// If this declaration is a type wrapper, retrieve
3784+
/// its required initializer - `init(storage:)`.
3785+
ConstructorDecl *getTypeWrapperInitializer() const;
3786+
37833787
/// Get a memberwise initializer that could be used to instantiate a
37843788
/// type wrapped type.
37853789
ConstructorDecl *getTypeWrappedTypeMemberwiseInitializer() const;

include/swift/AST/TypeCheckRequests.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,6 +3720,22 @@ class SynthesizeLocalVariableForTypeWrapperStorage
37203720
bool isCached() const { return true; }
37213721
};
37223722

3723+
class GetTypeWrapperInitializer
3724+
: public SimpleRequest<GetTypeWrapperInitializer,
3725+
ConstructorDecl *(NominalTypeDecl *),
3726+
RequestFlags::Cached> {
3727+
public:
3728+
using SimpleRequest::SimpleRequest;
3729+
3730+
private:
3731+
friend SimpleRequest;
3732+
3733+
ConstructorDecl *evaluate(Evaluator &evaluator, NominalTypeDecl *) const;
3734+
3735+
public:
3736+
bool isCached() const { return true; }
3737+
};
3738+
37233739
void simple_display(llvm::raw_ostream &out, ASTNode node);
37243740
void simple_display(llvm::raw_ostream &out, Type value);
37253741
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
@@ -437,3 +437,6 @@ SWIFT_REQUEST(TypeChecker, SynthesizeTypeWrappedTypeMemberwiseInitializerBody,
437437
SWIFT_REQUEST(TypeChecker, SynthesizeLocalVariableForTypeWrapperStorage,
438438
VarDecl *(ConstructorDecl *),
439439
Cached, NoLocationInfo)
440+
SWIFT_REQUEST(TypeChecker, GetTypeWrapperInitializer,
441+
ConstructorDecl *(NominalTypeDecl *),
442+
Cached, NoLocationInfo)

lib/Sema/TypeCheckTypeWrapper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,24 @@ VarDecl *SynthesizeLocalVariableForTypeWrapperStorage::evaluate(
512512
VarDecl::Introducer::Var);
513513
return PBD->getSingleVar();
514514
}
515+
516+
ConstructorDecl *NominalTypeDecl::getTypeWrapperInitializer() const {
517+
auto *mutableSelf = const_cast<NominalTypeDecl *>(this);
518+
return evaluateOrDefault(getASTContext().evaluator,
519+
GetTypeWrapperInitializer{mutableSelf}, nullptr);
520+
}
521+
522+
ConstructorDecl *
523+
GetTypeWrapperInitializer::evaluate(Evaluator &evaluator,
524+
NominalTypeDecl *typeWrapper) const {
525+
auto &ctx = typeWrapper->getASTContext();
526+
assert(typeWrapper->getAttrs().hasAttribute<TypeWrapperAttr>());
527+
528+
auto ctors = typeWrapper->lookupDirect(
529+
DeclName(ctx, DeclBaseName::createConstructor(), {ctx.Id_memberwise}));
530+
531+
if (ctors.size() != 1)
532+
return nullptr;
533+
534+
return cast<ConstructorDecl>(ctors.front());
535+
}

0 commit comments

Comments
 (0)