Skip to content

Commit cc1d5c1

Browse files
committed
[IDE] Move typeCheckContext() to ExprContextAnalysis.cpp
NFC
1 parent 2fbd3fe commit cc1d5c1

File tree

3 files changed

+69
-56
lines changed

3 files changed

+69
-56
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,52 +1345,6 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13451345
/// to the \c Consumer.
13461346
bool DeliveredResults = false;
13471347

1348-
void typeCheckContext(DeclContext *DC) {
1349-
// Nothing to type check in module context.
1350-
if (DC->isModuleScopeContext())
1351-
return;
1352-
1353-
typeCheckContext(DC->getParent());
1354-
1355-
// Type-check this context.
1356-
switch (DC->getContextKind()) {
1357-
case DeclContextKind::AbstractClosureExpr:
1358-
case DeclContextKind::Initializer:
1359-
case DeclContextKind::Module:
1360-
case DeclContextKind::SerializedLocal:
1361-
case DeclContextKind::TopLevelCodeDecl:
1362-
// Nothing to do for these.
1363-
break;
1364-
1365-
case DeclContextKind::AbstractFunctionDecl: {
1366-
auto *AFD = cast<AbstractFunctionDecl>(DC);
1367-
1368-
// FIXME: This shouldn't be necessary, but we crash otherwise.
1369-
if (auto *AD = dyn_cast<AccessorDecl>(AFD))
1370-
typeCheckCompletionDecl(AD->getStorage());
1371-
1372-
typeCheckAbstractFunctionBodyUntil(
1373-
AFD,
1374-
P.Context.SourceMgr.getCodeCompletionLoc());
1375-
break;
1376-
}
1377-
1378-
case DeclContextKind::ExtensionDecl:
1379-
typeCheckCompletionDecl(cast<ExtensionDecl>(DC));
1380-
break;
1381-
1382-
case DeclContextKind::GenericTypeDecl:
1383-
typeCheckCompletionDecl(cast<GenericTypeDecl>(DC));
1384-
break;
1385-
1386-
case DeclContextKind::FileUnit:
1387-
llvm_unreachable("module scope context handled above");
1388-
1389-
case DeclContextKind::SubscriptDecl:
1390-
typeCheckCompletionDecl(cast<SubscriptDecl>(DC));
1391-
break;
1392-
}
1393-
}
13941348

13951349
Optional<std::pair<Type, ConcreteDeclRef>> typeCheckParsedExpr() {
13961350
assert(ParsedExpr && "should have an expression");
@@ -4905,16 +4859,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
49054859
CurDeclContext = DC;
49064860
}
49074861

4908-
// The only time we have to explicitly check a TopLevelCodeDecl
4909-
// is when we're directly inside of one. In this case,
4910-
// performTypeChecking() did not type check it for us.
4911-
auto *DC = CurDeclContext;
4912-
while (isa<AbstractClosureExpr>(DC))
4913-
DC = DC->getParent();
4914-
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(DC))
4915-
typeCheckTopLevelCodeDecl(TLCD);
4916-
else
4917-
typeCheckContext(CurDeclContext);
4862+
typeCheckContextUntil(
4863+
CurDeclContext,
4864+
CurDeclContext->getASTContext().SourceMgr.getCodeCompletionLoc());
49184865

49194866
Optional<Type> ExprType;
49204867
ConcreteDeclRef ReferencedDecl = nullptr;

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,68 @@
3030
using namespace swift;
3131
using namespace ide;
3232

33+
//===----------------------------------------------------------------------===//
34+
// typeCheckContextUntil(DeclContext, SourceLoc)
35+
//===----------------------------------------------------------------------===//
36+
37+
namespace {
38+
void typeCheckContextImpl(DeclContext *DC, SourceLoc Loc) {
39+
// Nothing to type check in module context.
40+
if (DC->isModuleScopeContext())
41+
return;
42+
43+
typeCheckContextImpl(DC->getParent(), Loc);
44+
45+
// Type-check this context.
46+
switch (DC->getContextKind()) {
47+
case DeclContextKind::AbstractClosureExpr:
48+
case DeclContextKind::Initializer:
49+
case DeclContextKind::Module:
50+
case DeclContextKind::SerializedLocal:
51+
case DeclContextKind::TopLevelCodeDecl:
52+
// Nothing to do for these.
53+
break;
54+
55+
case DeclContextKind::AbstractFunctionDecl: {
56+
auto *AFD = cast<AbstractFunctionDecl>(DC);
57+
58+
// FIXME: This shouldn't be necessary, but we crash otherwise.
59+
if (auto *AD = dyn_cast<AccessorDecl>(AFD))
60+
typeCheckCompletionDecl(AD->getStorage());
61+
62+
typeCheckAbstractFunctionBodyUntil(AFD, Loc);
63+
break;
64+
}
65+
66+
case DeclContextKind::ExtensionDecl:
67+
typeCheckCompletionDecl(cast<ExtensionDecl>(DC));
68+
break;
69+
70+
case DeclContextKind::GenericTypeDecl:
71+
typeCheckCompletionDecl(cast<GenericTypeDecl>(DC));
72+
break;
73+
74+
case DeclContextKind::FileUnit:
75+
llvm_unreachable("module scope context handled above");
76+
77+
case DeclContextKind::SubscriptDecl:
78+
typeCheckCompletionDecl(cast<SubscriptDecl>(DC));
79+
break;
80+
}
81+
}
82+
} // anonymous namespace
83+
84+
void swift::ide::typeCheckContextUntil(DeclContext *DC, SourceLoc Loc) {
85+
// The only time we have to explicitly check a TopLevelCodeDecl
86+
// is when we're directly inside of one. In this case,
87+
// performTypeChecking() did not type check it for us.
88+
while (isa<AbstractClosureExpr>(DC))
89+
DC = DC->getParent();
90+
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(DC))
91+
typeCheckTopLevelCodeDecl(TLCD);
92+
else
93+
typeCheckContextImpl(DC, Loc);
94+
}
3395

3496
//===----------------------------------------------------------------------===//
3597
// getReturnTypeFromContext(DeclContext)

lib/IDE/ExprContextAnalysis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class AnyFunctionType;
2525

2626
namespace ide {
2727

28+
/// Type check parent contexts of the given decl context, and the body of the
29+
/// given context until \c Loc if the context is a function body.
30+
void typeCheckContextUntil(DeclContext *DC, SourceLoc Loc);
31+
2832
/// Returns expected return type of the given decl context.
2933
/// \p DC should be an \c AbstractFunctionDecl or an \c AbstractClosureExpr.
3034
Type getReturnTypeFromContext(const DeclContext *DC);

0 commit comments

Comments
 (0)