Skip to content

Commit 46d84be

Browse files
committed
Add 'isAsyncContext' to DeclContext
Querying the context for whether it is an async context is the easiest way to detect if the context is async. Many decl contexts can be checked at any point, but AbstractClosureExpr contexts must have been typechecked. If this is called on an AbstractClosureExpr before the types have been assigned, it may either crash or do bad things.
1 parent 1cfb8f0 commit 46d84be

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
296296
/// Returns the kind of context this is.
297297
DeclContextKind getContextKind() const;
298298

299+
/// Returns whether this context asynchronous
300+
bool isAsyncContext() const;
301+
299302
/// Returns whether this context has value semantics.
300303
bool hasValueSemantics() const;
301304

lib/AST/DeclContext.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,32 @@ bool DeclContext::isClassConstrainedProtocolExtension() const {
12041204
return false;
12051205
}
12061206

1207+
bool DeclContext::isAsyncContext() const {
1208+
switch (getContextKind()) {
1209+
case DeclContextKind::Initializer:
1210+
case DeclContextKind::TopLevelCodeDecl:
1211+
case DeclContextKind::EnumElementDecl:
1212+
case DeclContextKind::ExtensionDecl:
1213+
case DeclContextKind::SerializedLocal:
1214+
case DeclContextKind::Module:
1215+
case DeclContextKind::FileUnit:
1216+
case DeclContextKind::GenericTypeDecl:
1217+
return false;
1218+
case DeclContextKind::AbstractClosureExpr:
1219+
return cast<AbstractClosureExpr>(this)->isBodyAsync();
1220+
case DeclContextKind::AbstractFunctionDecl: {
1221+
const AbstractFunctionDecl *function = cast<AbstractFunctionDecl>(this);
1222+
return function->hasAsync();
1223+
}
1224+
case DeclContextKind::SubscriptDecl: {
1225+
AccessorDecl *getter =
1226+
cast<SubscriptDecl>(this)->getAccessor(AccessorKind::Get);
1227+
return getter != nullptr && getter->hasAsync();
1228+
}
1229+
}
1230+
llvm_unreachable("Unhandled DeclContextKind switch");
1231+
}
1232+
12071233
SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
12081234
switch (dc->getContextKind()) {
12091235
case DeclContextKind::Module:

0 commit comments

Comments
 (0)