Skip to content

Commit c62ec0c

Browse files
authored
SourceKit: Introduce a new enum class to describe the kind of resolved sema token kind. NFC (#7657)
1 parent 86cc11a commit c62ec0c

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

include/swift/IDE/Utils.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@ class XMLEscapingPrinter : public StreamPrinter {
143143
void printXML(StringRef Text);
144144
};
145145

146+
enum class SemaTokenKind {
147+
Invalid,
148+
ValueRef,
149+
ModuleRef,
150+
StmtStart,
151+
};
152+
146153
struct SemaToken {
154+
SemaTokenKind Kind = SemaTokenKind::Invalid;
147155
ValueDecl *ValueD = nullptr;
148156
TypeDecl *CtorTyRef = nullptr;
149157
ExtensionDecl *ExtTyRef = nullptr;
@@ -154,17 +162,20 @@ struct SemaToken {
154162
Type Ty;
155163
DeclContext *DC = nullptr;
156164
Type ContainerType;
165+
Stmt *TrailingStmt = nullptr;
157166

158167
SemaToken() = default;
159168
SemaToken(ValueDecl *ValueD, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
160169
SourceLoc Loc, bool IsRef, Type Ty, Type ContainerType) :
161-
ValueD(ValueD), CtorTyRef(CtorTyRef), ExtTyRef(ExtTyRef), Loc(Loc),
162-
IsRef(IsRef), Ty(Ty), DC(ValueD->getDeclContext()),
163-
ContainerType(ContainerType) {}
164-
SemaToken(ModuleEntity Mod, SourceLoc Loc) : Mod(Mod), Loc(Loc) { }
165-
166-
bool isValid() const { return ValueD != nullptr || Mod; }
167-
bool isInvalid() const { return !isValid(); }
170+
Kind(SemaTokenKind::ValueRef), ValueD(ValueD), CtorTyRef(CtorTyRef),
171+
ExtTyRef(ExtTyRef), Loc(Loc), IsRef(IsRef), Ty(Ty),
172+
DC(ValueD->getDeclContext()), ContainerType(ContainerType) {}
173+
SemaToken(ModuleEntity Mod, SourceLoc Loc) : Kind(SemaTokenKind::ModuleRef),
174+
Mod(Mod), Loc(Loc) { }
175+
SemaToken(Stmt *TrailingStmt) : Kind(SemaTokenKind::StmtStart),
176+
TrailingStmt(TrailingStmt) {}
177+
bool isValid() const { return !isInvalid(); }
178+
bool isInvalid() const { return Kind == SemaTokenKind::Invalid; }
168179
};
169180

170181
class SemaLocResolver : public SourceEntityWalker {
@@ -196,6 +207,7 @@ class SemaLocResolver : public SourceEntityWalker {
196207
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
197208
SourceLoc Loc, bool IsRef, Type Ty = Type());
198209
bool tryResolve(ModuleEntity Mod, SourceLoc Loc);
210+
bool tryResolve(Stmt *St);
199211
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
200212
bool IsOpenBracket) override;
201213
};

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ bool SemaLocResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
9191
return false;
9292
}
9393

94+
bool SemaLocResolver::tryResolve(Stmt *St) {
95+
if (auto *LST = dyn_cast<LabeledStmt>(St)) {
96+
if (LST->getStartLoc() == LocToResolve) {
97+
SemaTok = { St };
98+
return true;
99+
}
100+
}
101+
if (auto *CS = dyn_cast<CaseStmt>(St)) {
102+
if (CS->getStartLoc() == LocToResolve) {
103+
SemaTok = { St };
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
94110
bool SemaLocResolver::visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
95111
bool IsOpenBracket) {
96112
// We should treat both open and close brackets equally
@@ -133,7 +149,7 @@ bool SemaLocResolver::walkToStmtPre(Stmt *S) {
133149
// non-implicit Stmts (fix Stmts created for lazy vars).
134150
if (!S->isImplicit() && !rangeContainsLoc(S->getSourceRange()))
135151
return false;
136-
return true;
152+
return !tryResolve(S);
137153
}
138154

139155
bool SemaLocResolver::walkToStmtPost(Stmt *S) {

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,12 @@ static void resolveCursor(SwiftLangSupport &Lang,
10751075
CompilerInvocation CompInvok;
10761076
ASTInvok->applyTo(CompInvok);
10771077

1078-
if (SemaTok.Mod) {
1078+
switch (SemaTok.Kind) {
1079+
case SemaTokenKind::ModuleRef:
10791080
passCursorInfoForModule(SemaTok.Mod, Lang.getIFaceGenContexts(),
10801081
CompInvok, Receiver);
1081-
} else {
1082+
return;
1083+
case SemaTokenKind::ValueRef: {
10821084
ValueDecl *VD = SemaTok.CtorTyRef ? SemaTok.CtorTyRef : SemaTok.ValueD;
10831085
bool Failed = passCursorInfoForDecl(VD, MainModule,
10841086
SemaTok.ContainerType,
@@ -1095,6 +1097,15 @@ static void resolveCursor(SwiftLangSupport &Lang,
10951097
Receiver({});
10961098
}
10971099
}
1100+
return;
1101+
}
1102+
case SemaTokenKind::StmtStart: {
1103+
Receiver({});
1104+
return;
1105+
}
1106+
case SemaTokenKind::Invalid: {
1107+
llvm_unreachable("bad sema token kind");
1108+
}
10981109
}
10991110
}
11001111

@@ -1169,9 +1180,11 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
11691180
CompilerInvocation CompInvok;
11701181
ASTInvok->applyTo(CompInvok);
11711182

1172-
if (SemaTok.Mod) {
1183+
switch(SemaTok.Kind) {
1184+
case SemaTokenKind::ModuleRef:
1185+
return;
11731186

1174-
} else {
1187+
case SemaTokenKind::ValueRef: {
11751188
bool Failed = passNameInfoForDecl(SemaTok.ValueD, Input, Receiver);
11761189
if (Failed) {
11771190
if (!getPreviousASTSnaps().empty()) {
@@ -1182,6 +1195,14 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
11821195
Receiver({});
11831196
}
11841197
}
1198+
return;
1199+
}
1200+
case SemaTokenKind::StmtStart: {
1201+
Receiver({});
1202+
return;
1203+
}
1204+
case SemaTokenKind::Invalid:
1205+
llvm_unreachable("bad sema token kind.");
11851206
}
11861207
}
11871208

0 commit comments

Comments
 (0)