Skip to content

[AST] Move generic parameter logic for accessor decl from parser to GenericParamListRequest #61429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 20 additions & 34 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7133,32 +7133,24 @@ class AccessorDecl final : public FuncDecl {
AccessorKind accessorKind, AbstractStorageDecl *storage,
SourceLoc staticLoc, StaticSpellingKind staticSpelling,
bool async, SourceLoc asyncLoc, bool throws, SourceLoc throwsLoc,
bool hasImplicitSelfDecl, GenericParamList *genericParams,
DeclContext *parent)
: FuncDecl(DeclKind::Accessor,
staticLoc, staticSpelling, /*func loc*/ declLoc,
/*name*/ Identifier(), /*name loc*/ declLoc,
async, asyncLoc, throws, throwsLoc,
hasImplicitSelfDecl, genericParams, parent),
AccessorKeywordLoc(accessorKeywordLoc),
Storage(storage) {
bool hasImplicitSelfDecl, DeclContext *parent)
: FuncDecl(DeclKind::Accessor, staticLoc, staticSpelling,
/*func loc*/ declLoc,
/*name*/ Identifier(), /*name loc*/ declLoc, async, asyncLoc,
throws, throwsLoc, hasImplicitSelfDecl,
/*genericParams*/ nullptr, parent),
AccessorKeywordLoc(accessorKeywordLoc), Storage(storage) {
assert(!async || accessorKind == AccessorKind::Get
&& "only get accessors can be async");
Bits.AccessorDecl.AccessorKind = unsigned(accessorKind);
}

static AccessorDecl *createImpl(ASTContext &ctx,
SourceLoc declLoc,
SourceLoc accessorKeywordLoc,
AccessorKind accessorKind,
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool async, SourceLoc asyncLoc,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
DeclContext *parent,
ClangNode clangNode);
static AccessorDecl *
createImpl(ASTContext &ctx, SourceLoc declLoc, SourceLoc accessorKeywordLoc,
AccessorKind accessorKind, AbstractStorageDecl *storage,
SourceLoc staticLoc, StaticSpellingKind staticSpelling, bool async,
SourceLoc asyncLoc, bool throws, SourceLoc throwsLoc,
DeclContext *parent, ClangNode clangNode);

Optional<bool> getCachedIsTransparent() const {
if (Bits.AccessorDecl.IsTransparentComputed)
Expand All @@ -7174,21 +7166,15 @@ class AccessorDecl final : public FuncDecl {
AbstractStorageDecl *storage,
StaticSpellingKind staticSpelling,
bool async, bool throws,
GenericParamList *genericParams,
Type fnRetType, DeclContext *parent);

static AccessorDecl *create(ASTContext &ctx, SourceLoc declLoc,
SourceLoc accessorKeywordLoc,
AccessorKind accessorKind,
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool async, SourceLoc asyncLoc,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
ParameterList *parameterList,
Type fnRetType, DeclContext *parent,
ClangNode clangNode = ClangNode());
static AccessorDecl *
create(ASTContext &ctx, SourceLoc declLoc, SourceLoc accessorKeywordLoc,
AccessorKind accessorKind, AbstractStorageDecl *storage,
SourceLoc staticLoc, StaticSpellingKind staticSpelling, bool async,
SourceLoc asyncLoc, bool throws, SourceLoc throwsLoc,
ParameterList *parameterList, Type fnRetType, DeclContext *parent,
ClangNode clangNode = ClangNode());

SourceLoc getAccessorKeywordLoc() const { return AccessorKeywordLoc; }

Expand Down
6 changes: 2 additions & 4 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1242,10 +1242,8 @@ class Parser {
bool HasLetOrVarKeyword = true);

struct ParsedAccessors;
ParserStatus parseGetSet(ParseDeclOptions Flags,
GenericParamList *GenericParams,
ParameterList *Indices, TypeRepr *ResultType,
ParsedAccessors &accessors,
ParserStatus parseGetSet(ParseDeclOptions Flags, ParameterList *Indices,
TypeRepr *ResultType, ParsedAccessors &accessors,
AbstractStorageDecl *storage, SourceLoc StaticLoc);
ParserResult<VarDecl> parseDeclVarGetSet(PatternBindingEntry &entry,
ParseDeclOptions Flags,
Expand Down
71 changes: 28 additions & 43 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8560,28 +8560,22 @@ bool FuncDecl::isStatic() const {
false);
}

AccessorDecl *AccessorDecl::createImpl(ASTContext &ctx,
SourceLoc declLoc,
SourceLoc accessorKeywordLoc,
AccessorKind accessorKind,
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool async, SourceLoc asyncLoc,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
DeclContext *parent,
ClangNode clangNode) {
AccessorDecl *AccessorDecl::createImpl(
ASTContext &ctx, SourceLoc declLoc, SourceLoc accessorKeywordLoc,
AccessorKind accessorKind, AbstractStorageDecl *storage,
SourceLoc staticLoc, StaticSpellingKind staticSpelling, bool async,
SourceLoc asyncLoc, bool throws, SourceLoc throwsLoc, DeclContext *parent,
ClangNode clangNode) {
bool hasImplicitSelfDecl = parent->isTypeContext();
size_t size = sizeof(AccessorDecl) + (hasImplicitSelfDecl
? sizeof(ParamDecl *)
: 0);
void *buffer = allocateMemoryForDecl<AccessorDecl>(ctx, size,
!clangNode.isNull());
auto D = ::new (buffer)
AccessorDecl(declLoc, accessorKeywordLoc, accessorKind,
storage, staticLoc, staticSpelling, async, asyncLoc, throws, throwsLoc,
hasImplicitSelfDecl, genericParams, parent);
AccessorDecl(declLoc, accessorKeywordLoc, accessorKind, storage,
staticLoc, staticSpelling, async, asyncLoc, throws,
throwsLoc, hasImplicitSelfDecl, parent);
if (clangNode)
D->setClangNode(clangNode);
if (hasImplicitSelfDecl)
Expand All @@ -8590,38 +8584,30 @@ AccessorDecl *AccessorDecl::createImpl(ASTContext &ctx,
return D;
}

AccessorDecl *
AccessorDecl::createDeserialized(ASTContext &ctx, AccessorKind accessorKind,
AbstractStorageDecl *storage,
StaticSpellingKind staticSpelling,
bool async, bool throws, GenericParamList *genericParams,
Type fnRetType, DeclContext *parent) {
AccessorDecl *AccessorDecl::createDeserialized(
ASTContext &ctx, AccessorKind accessorKind, AbstractStorageDecl *storage,
StaticSpellingKind staticSpelling, bool async, bool throws, Type fnRetType,
DeclContext *parent) {
assert(fnRetType && "Deserialized result type must not be null");
auto *const D = AccessorDecl::createImpl(
ctx, SourceLoc(), SourceLoc(), accessorKind, storage, SourceLoc(),
staticSpelling, async, SourceLoc(), throws, SourceLoc(), genericParams, parent, ClangNode());
staticSpelling, async, SourceLoc(), throws, SourceLoc(), parent,
ClangNode());
D->setResultInterfaceType(fnRetType);
return D;
}

AccessorDecl *AccessorDecl::create(ASTContext &ctx,
SourceLoc declLoc,
SourceLoc accessorKeywordLoc,
AccessorKind accessorKind,
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool async, SourceLoc asyncLoc,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
ParameterList * bodyParams,
Type fnRetType,
DeclContext *parent,
ClangNode clangNode) {
AccessorDecl *
AccessorDecl::create(ASTContext &ctx, SourceLoc declLoc,
SourceLoc accessorKeywordLoc, AccessorKind accessorKind,
AbstractStorageDecl *storage, SourceLoc staticLoc,
StaticSpellingKind staticSpelling, bool async,
SourceLoc asyncLoc, bool throws, SourceLoc throwsLoc,
ParameterList *bodyParams, Type fnRetType,
DeclContext *parent, ClangNode clangNode) {
auto *D = AccessorDecl::createImpl(
ctx, declLoc, accessorKeywordLoc, accessorKind, storage,
staticLoc, staticSpelling, async, asyncLoc, throws, throwsLoc,
genericParams, parent, clangNode);
ctx, declLoc, accessorKeywordLoc, accessorKind, storage, staticLoc,
staticSpelling, async, asyncLoc, throws, throwsLoc, parent, clangNode);
D->setParameters(bodyParams);
D->setResultInterfaceType(fnRetType);
return D;
Expand Down Expand Up @@ -9517,7 +9503,6 @@ bool ActorIsolation::isDistributedActor() const {
return getKind() == ActorInstance && getActor()->isDistributedActor();
}

BuiltinTupleDecl::BuiltinTupleDecl(Identifier Name,
DeclContext *Parent)
: NominalTypeDecl(DeclKind::BuiltinTuple, Parent, Name, SourceLoc(),
ArrayRef<InheritedEntry>(), nullptr) {}
BuiltinTupleDecl::BuiltinTupleDecl(Identifier Name, DeclContext *Parent)
: NominalTypeDecl(DeclKind::BuiltinTuple, Parent, Name, SourceLoc(),
ArrayRef<InheritedEntry>(), nullptr) {}
14 changes: 14 additions & 0 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,20 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
return result;
}

// AccessorDecl generic parameter list is the same of its storage
// context.
if (auto *AD = dyn_cast<AccessorDecl>(value)) {
auto *GC = AD->getStorage()->getAsGenericContext();
if (!GC)
return nullptr;

auto *GP = GC->getGenericParams();
if (!GP)
return nullptr;

return GP->clone(AD->getDeclContext());
}

auto parsedGenericParams = value->getParsedGenericParams();

// Create implicit generic parameters due to opaque parameters, if we need
Expand Down
9 changes: 3 additions & 6 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4775,9 +4775,7 @@ makeBaseClassMemberAccessors(DeclContext *declContext,
StaticSpellingKind::None, // TODO: we should handle static vars.
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/false,
/*ThrowsLoc=*/SourceLoc(),
/*GenericParams=*/nullptr, bodyParams, computedType,
declContext);
/*ThrowsLoc=*/SourceLoc(), bodyParams, computedType, declContext);
getterDecl->setIsTransparent(true);
getterDecl->setAccess(AccessLevel::Public);
getterDecl->setBodySynthesizer(synthesizeBaseClassFieldGetterBody,
Expand Down Expand Up @@ -4809,9 +4807,8 @@ makeBaseClassMemberAccessors(DeclContext *declContext,
StaticSpellingKind::None, // TODO: we should handle static vars.
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/false,
/*ThrowsLoc=*/SourceLoc(),
/*GenericParams=*/nullptr, setterBodyParams,
TupleType::getEmpty(ctx), declContext);
/*ThrowsLoc=*/SourceLoc(), setterBodyParams, TupleType::getEmpty(ctx),
declContext);
setterDecl->setIsTransparent(true);
setterDecl->setAccess(AccessLevel::Public);
setterDecl->setBodySynthesizer(synthesizeBaseClassFieldSetterBody,
Expand Down
38 changes: 16 additions & 22 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,13 @@ static FuncDecl *createFuncOrAccessor(ClangImporter::Implementation &impl,
ClangNode clangNode) {
FuncDecl *decl;
if (accessorInfo) {
decl = AccessorDecl::create(impl.SwiftContext, funcLoc,
/*accessorKeywordLoc*/ SourceLoc(),
accessorInfo->Kind, accessorInfo->Storage,
/*StaticLoc*/ SourceLoc(),
StaticSpellingKind::None,
async, /*AsyncLoc=*/SourceLoc(),
throws, /*ThrowsLoc=*/SourceLoc(),
genericParams, bodyParams,
resultTy, dc, clangNode);
decl = AccessorDecl::create(
impl.SwiftContext, funcLoc,
/*accessorKeywordLoc*/ SourceLoc(), accessorInfo->Kind,
accessorInfo->Storage,
/*StaticLoc*/ SourceLoc(), StaticSpellingKind::None, async,
/*AsyncLoc=*/SourceLoc(), throws, /*ThrowsLoc=*/SourceLoc(), bodyParams,
resultTy, dc, clangNode);
} else {
decl = FuncDecl::createImported(impl.SwiftContext, funcLoc, name, nameLoc,
async, throws, bodyParams, resultTy,
Expand Down Expand Up @@ -599,19 +597,15 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,

auto *params = ParameterList::createEmpty(C);

auto getterDecl = AccessorDecl::create(C,
/*FuncLoc=*/SourceLoc(),
/*AccessorKeywordLoc=*/SourceLoc(),
AccessorKind::Get,
errorDomainPropertyDecl,
/*StaticLoc=*/SourceLoc(),
StaticSpellingKind::None,
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/false,
/*ThrowsLoc=*/SourceLoc(),
/*GenericParams=*/nullptr,
params,
stringTy, swiftDecl);
auto getterDecl = AccessorDecl::create(
C,
/*FuncLoc=*/SourceLoc(),
/*AccessorKeywordLoc=*/SourceLoc(), AccessorKind::Get,
errorDomainPropertyDecl,
/*StaticLoc=*/SourceLoc(), StaticSpellingKind::None,
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/false,
/*ThrowsLoc=*/SourceLoc(), params, stringTy, swiftDecl);
getterDecl->setIsObjC(false);
getterDecl->setIsDynamic(false);
getterDecl->setIsTransparent(false);
Expand Down
Loading