Skip to content

Commit 0c85520

Browse files
committed
---
yaml --- r: 347007 b: refs/heads/master c: 26bc5fd h: refs/heads/master i: 347005: 6ab8527 347003: 24716ce 346999: 0710ae5 346991: f3b0021 346975: 29bdb8e 346943: 540dfee 346879: 2cbf856
1 parent f3a3942 commit 0c85520

File tree

10 files changed

+213
-42
lines changed

10 files changed

+213
-42
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: e461434a1cfb7d96bb22f6d0785ad5ad789de08d
2+
refs/heads/master: 26bc5fd4f4d768af5b01028d92929ce6c1be1fca
33
refs/heads/master-next: 203b3026584ecad859eb328b2e12490099409cd5
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea

trunk/include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ struct PrintOptions {
275275
/// '@sil_weak', '@sil_unmanaged'.
276276
bool PrintStorageRepresentationAttrs = false;
277277

278+
/// Whether to print 'static' or 'class' on static decls.
279+
bool PrintStaticKeyword = true;
280+
278281
/// Whether to print 'override' keyword on overridden decls.
279282
bool PrintOverrideKeyword = true;
280283

trunk/include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class CodeCompletionCallbacks {
174174
/// Complete at the beginning of member of a nominal decl member -- no tokens
175175
/// provided by user.
176176
virtual void completeNominalMemberBeginning(
177-
SmallVectorImpl<StringRef> &Keywords) {};
177+
SmallVectorImpl<StringRef> &Keywords, SourceLoc introducerLoc) {};
178178

179179
/// Complete at the beginning of accessor in a accessor block.
180180
virtual void completeAccessorBeginning() {};

trunk/lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
24112411
printAttributes(decl);
24122412
printAccess(decl);
24132413
if (!Options.SkipIntroducerKeywords) {
2414-
if (decl->isStatic())
2414+
if (decl->isStatic() && Options.PrintStaticKeyword)
24152415
printStaticKeyword(decl->getCorrectStaticSpelling());
24162416
if (decl->getKind() == DeclKind::Var
24172417
|| Options.PrintParameterSpecifiers) {
@@ -2687,7 +2687,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26872687
printSourceRange(Range, Ctx);
26882688
} else {
26892689
if (!Options.SkipIntroducerKeywords) {
2690-
if (decl->isStatic())
2690+
if (decl->isStatic() && Options.PrintStaticKeyword)
26912691
printStaticKeyword(decl->getCorrectStaticSpelling());
26922692
if (decl->isMutating() && !decl->getAttrs().hasAttribute<MutatingAttr>()) {
26932693
Printer.printKeyword("mutating", Options, " ");

trunk/lib/IDE/CodeCompletion.cpp

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
12501250
Optional<StmtKind> ParentStmtKind;
12511251

12521252
SmallVector<StringRef, 3> ParsedKeywords;
1253+
SourceLoc introducerLoc;
12531254

12541255
std::vector<std::pair<std::string, bool>> SubModuleNameVisibilityPairs;
12551256

@@ -1348,7 +1349,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13481349
void completeDeclAttrParam(DeclAttrKind DK, int Index) override;
13491350
void completeInPrecedenceGroup(SyntaxKind SK) override;
13501351
void completeNominalMemberBeginning(
1351-
SmallVectorImpl<StringRef> &Keywords) override;
1352+
SmallVectorImpl<StringRef> &Keywords, SourceLoc introducerLoc) override;
13521353
void completeAccessorBeginning() override;
13531354

13541355
void completePoundAvailablePlatform() override;
@@ -3858,9 +3859,11 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
38583859

38593860
class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
38603861
CodeCompletionResultSink &Sink;
3862+
ASTContext &Ctx;
38613863
const DeclContext *CurrDeclContext;
38623864
LazyResolver *TypeResolver;
38633865
SmallVectorImpl<StringRef> &ParsedKeywords;
3866+
SourceLoc introducerLoc;
38643867

38653868
bool hasFuncIntroducer = false;
38663869
bool hasVarIntroducer = false;
@@ -3869,13 +3872,15 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
38693872
bool hasAccessModifier = false;
38703873
bool hasOverride = false;
38713874
bool hasOverridabilityModifier = false;
3875+
bool hasStaticOrClass = false;
38723876

38733877
public:
38743878
CompletionOverrideLookup(CodeCompletionResultSink &Sink, ASTContext &Ctx,
38753879
const DeclContext *CurrDeclContext,
3876-
SmallVectorImpl<StringRef> &ParsedKeywords)
3877-
: Sink(Sink),
3878-
CurrDeclContext(CurrDeclContext), ParsedKeywords(ParsedKeywords) {
3880+
SmallVectorImpl<StringRef> &ParsedKeywords,
3881+
SourceLoc introducerLoc)
3882+
: Sink(Sink), Ctx(Ctx), CurrDeclContext(CurrDeclContext),
3883+
ParsedKeywords(ParsedKeywords), introducerLoc(introducerLoc) {
38793884
(void)createTypeChecker(Ctx);
38803885
TypeResolver = Ctx.getLazyResolver();
38813886

@@ -3893,6 +3898,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
38933898
hasOverride = isKeywordSpecified("override");
38943899
hasOverridabilityModifier = isKeywordSpecified("final") ||
38953900
isKeywordSpecified("open");
3901+
hasStaticOrClass = isKeywordSpecified(getTokenText(tok::kw_class)) ||
3902+
isKeywordSpecified(getTokenText(tok::kw_static));
38963903
}
38973904

38983905
bool isKeywordSpecified(StringRef Word) {
@@ -3942,19 +3949,28 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
39423949
Options.setBaseType(transformType);
39433950
Options.PrintImplicitAttrs = false;
39443951
Options.ExclusiveAttrList.push_back(TAK_escaping);
3952+
Options.ExclusiveAttrList.push_back(TAK_autoclosure);
39453953
Options.PrintOverrideKeyword = false;
39463954
Options.PrintPropertyAccessors = false;
3955+
Options.PrintStaticKeyword = !hasStaticOrClass;
39473956
VD->print(Printer, Options);
39483957
NameOffset = Printer.NameOffset.getValue();
39493958
}
39503959

39513960
if (!hasDeclIntroducer && !hasAccessModifier)
39523961
addAccessControl(VD, Builder);
39533962

3954-
// FIXME: if we're missing 'override', but have the decl introducer we
3955-
// should delete it and re-add both in the correct order.
3956-
if (!hasDeclIntroducer && missingOverride(Reason))
3957-
Builder.addOverrideKeyword();
3963+
if (missingOverride(Reason)) {
3964+
if (!hasDeclIntroducer)
3965+
Builder.addOverrideKeyword();
3966+
else {
3967+
auto dist = Ctx.SourceMgr.getByteDistance(
3968+
introducerLoc, Ctx.SourceMgr.getCodeCompletionLoc());
3969+
Builder.setNumBytesToErase(dist);
3970+
Builder.addOverrideKeyword();
3971+
Builder.addDeclIntroducer(DeclStr.str().substr(0, NameOffset));
3972+
}
3973+
}
39583974

39593975
if (!hasDeclIntroducer)
39603976
Builder.addDeclIntroducer(DeclStr.str().substr(0, NameOffset));
@@ -4050,7 +4066,9 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40504066
if (D->shouldHideFromEditor())
40514067
return;
40524068

4053-
if (D->getAttrs().hasAttribute<FinalAttr>())
4069+
if (D->getAttrs().hasAttribute<FinalAttr>() ||
4070+
// A 'class' member with an initial value cannot be overriden either.
4071+
(D->isStatic() && D->getAttrs().hasAttribute<HasInitialValueAttr>()))
40544072
return;
40554073

40564074
if (!D->hasInterfaceType())
@@ -4060,6 +4078,15 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40604078
hasVarIntroducer ||
40614079
hasTypealiasIntroducer;
40624080

4081+
if (hasStaticOrClass && !D->isStatic())
4082+
return;
4083+
4084+
// As per the current convention, only instance members are
4085+
// suggested if an introducer is not accompanied by a 'static' or
4086+
// 'class' modifier.
4087+
if (hasIntroducer && !hasStaticOrClass && D->isStatic())
4088+
return;
4089+
40634090
if (auto *FD = dyn_cast<FuncDecl>(D)) {
40644091
// We cannot override operators as members.
40654092
if (FD->isBinaryOperator() || FD->isUnaryOperator())
@@ -4083,7 +4110,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40834110
if (auto *CD = dyn_cast<ConstructorDecl>(D)) {
40844111
if (!isa<ProtocolDecl>(CD->getDeclContext()))
40854112
return;
4086-
if (hasIntroducer || hasOverride || hasOverridabilityModifier)
4113+
if (hasIntroducer || hasOverride || hasOverridabilityModifier ||
4114+
hasStaticOrClass)
40874115
return;
40884116
if (CD->isRequired() || CD->isDesignatedInit())
40894117
addConstructor(CD, Reason);
@@ -4093,7 +4121,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40934121

40944122
void addDesignatedInitializers(NominalTypeDecl *NTD) {
40954123
if (hasFuncIntroducer || hasVarIntroducer || hasTypealiasIntroducer ||
4096-
hasOverridabilityModifier)
4124+
hasOverridabilityModifier || hasStaticOrClass)
40974125
return;
40984126

40994127
const auto *CD = dyn_cast<ClassDecl>(NTD);
@@ -4116,7 +4144,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
41164144
void addAssociatedTypes(NominalTypeDecl *NTD) {
41174145
if (!hasTypealiasIntroducer &&
41184146
(hasFuncIntroducer || hasVarIntroducer || hasInitializerModifier ||
4119-
hasOverride || hasOverridabilityModifier))
4147+
hasOverride || hasOverridabilityModifier || hasStaticOrClass))
41204148
return;
41214149

41224150
for (auto Conformance : NTD->getAllConformances()) {
@@ -4147,9 +4175,11 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
41474175
Type CurrTy = CurrDeclContext->getSelfTypeInContext();
41484176
auto *NTD = CurrDeclContext->getSelfNominalTypeDecl();
41494177
if (CurrTy && !CurrTy->is<ErrorType>()) {
4150-
lookupVisibleMemberDecls(*this, CurrTy, CurrDeclContext,
4178+
// Look for overridable static members too.
4179+
Type Meta = MetatypeType::get(CurrTy);
4180+
lookupVisibleMemberDecls(*this, Meta, CurrDeclContext,
41514181
TypeResolver,
4152-
/*includeInstanceMembers=*/false);
4182+
/*includeInstanceMembers=*/true);
41534183
addDesignatedInitializers(NTD);
41544184
addAssociatedTypes(NTD);
41554185
}
@@ -4471,8 +4501,9 @@ void CodeCompletionCallbacksImpl::completeGenericParams(TypeLoc TL) {
44714501
}
44724502

44734503
void CodeCompletionCallbacksImpl::completeNominalMemberBeginning(
4474-
SmallVectorImpl<StringRef> &Keywords) {
4504+
SmallVectorImpl<StringRef> &Keywords, SourceLoc introducerLoc) {
44754505
assert(!InEnumElementRawValue);
4506+
this->introducerLoc = introducerLoc;
44764507
ParsedKeywords.clear();
44774508
ParsedKeywords.append(Keywords.begin(), Keywords.end());
44784509
Kind = CompletionKind::NominalMemberBeginning;
@@ -4651,10 +4682,11 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
46514682
break;
46524683

46534684
case CompletionKind::NominalMemberBeginning: {
4654-
bool HasDeclIntroducer = llvm::find_if(ParsedKeywords, [](const StringRef kw) {
4685+
bool HasDeclIntroducer = llvm::find_if(ParsedKeywords,
4686+
[this](const StringRef kw) {
46554687
return llvm::StringSwitch<bool>(kw)
46564688
.Case("associatedtype", true)
4657-
.Case("class", true)
4689+
.Case("class", !CurDeclContext || !isa<ClassDecl>(CurDeclContext))
46584690
.Case("deinit", true)
46594691
.Case("enum", true)
46604692
.Case("extension", true)
@@ -5027,7 +5059,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
50275059
case CompletionKind::NominalMemberBeginning: {
50285060
CompletionOverrideLookup OverrideLookup(CompletionContext.getResultSink(),
50295061
P.Context, CurDeclContext,
5030-
ParsedKeywords);
5062+
ParsedKeywords, introducerLoc);
50315063
OverrideLookup.getOverrideCompletions(SourceLoc());
50325064
break;
50335065
}

trunk/lib/Parse/ParseDecl.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,9 +2281,18 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes,
22812281
{
22822282
BacktrackingScope Scope(*this);
22832283
consumeToken(tok::kw_class);
2284-
if (!isStartOfDecl())
2284+
// When followed by an 'override' or CC token inside a class,
2285+
// treat 'class' as a modifier; in the case of a following CC
2286+
// token, we cannot be sure there is no intention to override
2287+
// or witness something static.
2288+
if (isStartOfDecl() || (isa<ClassDecl>(CurDeclContext) &&
2289+
(Tok.is(tok::code_complete) ||
2290+
Tok.getRawText().equals("override")))) {
2291+
/* We're OK */
2292+
} else {
22852293
// This 'class' is a real ClassDecl introducer.
22862294
break;
2295+
}
22872296
}
22882297
if (StaticLoc.isValid()) {
22892298
diagnose(Tok, diag::decl_already_static,
@@ -2932,22 +2941,30 @@ Parser::parseDecl(ParseDeclOptions Flags,
29322941
// specified so that we do not duplicate them in code completion
29332942
// strings.
29342943
SmallVector<StringRef, 3> Keywords;
2944+
SourceLoc introducerLoc;
29352945
switch (OrigTok.getKind()) {
29362946
case tok::kw_func:
29372947
case tok::kw_subscript:
29382948
case tok::kw_var:
29392949
case tok::kw_let:
29402950
case tok::kw_typealias:
29412951
Keywords.push_back(OrigTok.getText());
2952+
introducerLoc = OrigTok.getLoc();
29422953
break;
29432954
default:
29442955
// Other tokens are already accounted for.
29452956
break;
29462957
}
2958+
if (StaticSpelling == StaticSpellingKind::KeywordStatic) {
2959+
Keywords.push_back(getTokenText(tok::kw_static));
2960+
} else if (StaticSpelling == StaticSpellingKind::KeywordClass) {
2961+
Keywords.push_back(getTokenText(tok::kw_class));
2962+
}
29472963
for (auto attr : Attributes) {
29482964
Keywords.push_back(attr->getAttrName());
29492965
}
2950-
CodeCompletion->completeNominalMemberBeginning(Keywords);
2966+
CodeCompletion->completeNominalMemberBeginning(Keywords,
2967+
introducerLoc);
29512968
}
29522969

29532970
DeclResult = makeParserCodeCompletionStatus();

trunk/test/IDE/complete_declname.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MyCls {
4343
class MySub : MyCls {
4444
func #^METHODNAME_OVERRIDE^#
4545
// METHODNAME_OVERRIDE: Begin completions, 1 items
46-
// METHODNAME_OVERRIDE-NEXT: Decl[InstanceMethod]/Super: foo() {|}; name=foo()
46+
// METHODNAME_OVERRIDE-NEXT: Decl[InstanceMethod]/Super/Erase[5]: override func foo() {|}; name=foo()
4747
// METHODNAME_OVERRIDE-NEXT: End completions
4848
}
4949

0 commit comments

Comments
 (0)