Skip to content

Commit 0dc0b03

Browse files
authored
Merge pull request swiftlang#25173 from rintaro/sourcekit-propertywrapper
[SE-0258][SourceKit] Tools support for property wrapper
2 parents 59031b3 + c14779f commit 0dc0b03

15 files changed

+338
-117
lines changed

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class CodeCompletionCallbacks {
115115
}
116116
};
117117

118+
/// Set target decl for attribute if the CC token is in attribute of the decl.
119+
virtual void setAttrTargetDecl(Decl *D) {}
120+
118121
/// Complete the whole expression. This is a fallback that should
119122
/// produce results when more specific completion methods failed.
120123
virtual void completeExpr() {};
@@ -186,7 +189,7 @@ class CodeCompletionCallbacks {
186189
virtual void completeAccessorBeginning(CodeCompletionExpr *E) {};
187190

188191
/// Complete the keyword in attribute, for instance, @available.
189-
virtual void completeDeclAttrKeyword(Decl *D, bool Sil, bool Param) {};
192+
virtual void completeDeclAttrBeginning(bool Sil) {};
190193

191194
/// Complete the parameters in attribute, for instance, version specifier for
192195
/// @available.

include/swift/Parse/Parser.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,7 @@ class Parser {
904904
void setLocalDiscriminatorToParamList(ParameterList *PL);
905905

906906
/// Parse the optional attributes before a declaration.
907-
bool parseDeclAttributeList(DeclAttributes &Attributes,
908-
bool &FoundCodeCompletionToken);
907+
ParserStatus parseDeclAttributeList(DeclAttributes &Attributes);
909908

910909
/// Parse the optional modifiers before a declaration.
911910
bool parseDeclModifierList(DeclAttributes &Attributes, SourceLoc &StaticLoc,
@@ -941,7 +940,7 @@ class Parser {
941940
SourceLoc Loc);
942941

943942
/// Parse a specific attribute.
944-
bool parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc);
943+
ParserStatus parseDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc);
945944

946945
bool parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
947946
DeclAttrKind DK);

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
126126
// Attributes
127127
//===--------------------------------------------------------------------===//
128128
bool visitCustomAttributes(Decl *D) {
129-
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr>()) {
129+
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
130130
CustomAttr *mutableCustomAttr = const_cast<CustomAttr *>(customAttr);
131131
if (doIt(mutableCustomAttr->getTypeLoc()))
132132
return true;

lib/AST/Decl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ SourceRange Decl::getSourceRangeIncludingAttrs() const {
441441
}
442442

443443
// Attributes on VarDecl syntactically belong to PatternBindingDecl.
444-
if (isa<VarDecl>(this))
444+
if (isa<VarDecl>(this) && !isa<ParamDecl>(this))
445445
return Range;
446446

447447
// Attributes on PatternBindingDecls are attached to VarDecls in AST.
@@ -2758,6 +2758,11 @@ bool ValueDecl::shouldHideFromEditor() const {
27582758
if (getBaseName().isEditorPlaceholder())
27592759
return true;
27602760

2761+
// '$__' names are reserved by compiler internal.
2762+
if (!getBaseName().isSpecial() &&
2763+
getBaseName().getIdentifier().str().startswith("$__"))
2764+
return true;
2765+
27612766
return false;
27622767
}
27632768

lib/IDE/CodeCompletion.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,17 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13481348
Consumer(Consumer) {
13491349
}
13501350

1351+
void setAttrTargetDecl(Decl *D) override {
1352+
if (D == nullptr) {
1353+
AttTargetDK = None;
1354+
return;
1355+
}
1356+
auto DK = D->getKind();
1357+
if (DK == DeclKind::PatternBinding)
1358+
DK = DeclKind::Var;
1359+
AttTargetDK = DK;
1360+
}
1361+
13511362
void completeExpr() override;
13521363
void completeDotExpr(Expr *E, SourceLoc DotLoc) override;
13531364
void completeStmtOrExpr(CodeCompletionExpr *E) override;
@@ -1367,7 +1378,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13671378
void completeCaseStmtKeyword() override;
13681379
void completeCaseStmtBeginning() override;
13691380
void completeCaseStmtDotPrefix() override;
1370-
void completeDeclAttrKeyword(Decl *D, bool Sil, bool Param) override;
1381+
void completeDeclAttrBeginning(bool Sil) override;
13711382
void completeDeclAttrParam(DeclAttrKind DK, int Index) override;
13721383
void completeInPrecedenceGroup(SyntaxKind SK) override;
13731384
void completeNominalMemberBeginning(
@@ -4601,16 +4612,9 @@ void CodeCompletionCallbacksImpl::completeDeclAttrParam(DeclAttrKind DK,
46014612
CurDeclContext = P.CurDeclContext;
46024613
}
46034614

4604-
void CodeCompletionCallbacksImpl::completeDeclAttrKeyword(Decl *D,
4605-
bool Sil,
4606-
bool Param) {
4615+
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(bool Sil) {
46074616
Kind = CompletionKind::AttributeBegin;
46084617
IsInSil = Sil;
4609-
if (Param) {
4610-
AttTargetDK = DeclKind::Param;
4611-
} else if (D) {
4612-
AttTargetDK = D->getKind();
4613-
}
46144618
CurDeclContext = P.CurDeclContext;
46154619
}
46164620

@@ -5376,6 +5380,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
53765380

53775381
case CompletionKind::AttributeBegin: {
53785382
Lookup.getAttributeDeclCompletions(IsInSil, AttTargetDK);
5383+
5384+
// Provide any type name for property delegate.
5385+
if (!AttTargetDK || *AttTargetDK == DeclKind::Var)
5386+
Lookup.getTypeCompletionsInDeclContext(
5387+
P.Context.SourceMgr.getCodeCompletionLoc());
53795388
break;
53805389
}
53815390
case CompletionKind::AttributeDeclParen: {
@@ -5686,7 +5695,6 @@ void swift::ide::copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
56865695
if (R->getKind() != CodeCompletionResult::Declaration)
56875696
return false;
56885697
switch(R->getAssociatedDeclKind()) {
5689-
case CodeCompletionDeclKind::PrecedenceGroup:
56905698
case CodeCompletionDeclKind::Module:
56915699
case CodeCompletionDeclKind::Class:
56925700
case CodeCompletionDeclKind::Struct:
@@ -5696,6 +5704,7 @@ void swift::ide::copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
56965704
case CodeCompletionDeclKind::AssociatedType:
56975705
case CodeCompletionDeclKind::GenericTypeParam:
56985706
return true;
5707+
case CodeCompletionDeclKind::PrecedenceGroup:
56995708
case CodeCompletionDeclKind::EnumElement:
57005709
case CodeCompletionDeclKind::Constructor:
57015710
case CodeCompletionDeclKind::Destructor:

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ void typeCheckContextImpl(DeclContext *DC, SourceLoc Loc) {
5757

5858
case DeclContextKind::Initializer:
5959
if (auto *patternInit = dyn_cast<PatternBindingInitializer>(DC)) {
60-
auto *PBD = patternInit->getBinding();
61-
auto i = patternInit->getBindingIndex();
62-
if (PBD->getInit(i)) {
63-
PBD->getPattern(i)->forEachVariable([](VarDecl *VD) {
64-
typeCheckCompletionDecl(VD);
65-
});
66-
if (!PBD->isInitializerChecked(i))
67-
typeCheckPatternBinding(PBD, i);
60+
if (auto *PBD = patternInit->getBinding()) {
61+
auto i = patternInit->getBindingIndex();
62+
if (PBD->getInit(i)) {
63+
PBD->getPattern(i)->forEachVariable([](VarDecl *VD) {
64+
typeCheckCompletionDecl(VD);
65+
});
66+
if (!PBD->isInitializerChecked(i))
67+
typeCheckPatternBinding(PBD, i);
68+
}
6869
}
6970
}
7071
break;

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
10261026
ArrayRef<Token> Toks) {
10271027
if (!D)
10281028
return false;
1029-
if (isa<AvailableAttr>(D)) {
1029+
if (isa<AvailableAttr>(D) || isa<CustomAttr>(D)) {
10301030
unsigned I = 0;
10311031
for (; I < TokenNodes.size(); ++ I) {
10321032
auto Node = TokenNodes[I];

0 commit comments

Comments
 (0)