AbstractFunctionDecl *DeclContext::getInnermostMethodContext() { DeclContext *result = this; while (true) { switch (result->getContextKind()) { case DeclContextKind::AbstractClosureExpr: case DeclContextKind::Initializer: case DeclContextKind::SerializedLocal: // Look through closures, initial values. result = result->getParent(); continue; case DeclContextKind::AbstractFunctionDecl: { // If this function is a method, we found our result. auto func = dyn_cast<AbstractFunctionDecl>(result); if (func->getDeclContext()->isTypeContext()) return func; // This function isn't a method; look through it. result = func->getDeclContext(); continue; } case DeclContextKind::ExtensionDecl: case DeclContextKind::FileUnit: case DeclContextKind::Module: case DeclContextKind::NominalTypeDecl: case DeclContextKind::TopLevelCodeDecl: case DeclContextKind::SubscriptDecl: // Not in a method context. return nullptr; } } }
Decl *DeclContext::getInnermostDeclarationDeclContext() { DeclContext *DC = this; while (DC) { switch (DC->getContextKind()) { case DeclContextKind::AbstractClosureExpr: case DeclContextKind::Initializer: case DeclContextKind::SerializedLocal: case DeclContextKind::Module: case DeclContextKind::FileUnit: break; case DeclContextKind::TopLevelCodeDecl: return cast<TopLevelCodeDecl>(DC); case DeclContextKind::AbstractFunctionDecl: return cast<AbstractFunctionDecl>(DC); case DeclContextKind::SubscriptDecl: return cast<SubscriptDecl>(DC); case DeclContextKind::NominalTypeDecl: return cast<NominalTypeDecl>(DC); case DeclContextKind::ExtensionDecl: return cast<ExtensionDecl>(DC); } DC = DC->getParent(); } return nullptr; }
DeclContext *DeclContext::getInnermostTypeContext() { DeclContext *Result = this; while (true) { switch (Result->getContextKind()) { case DeclContextKind::AbstractClosureExpr: case DeclContextKind::Initializer: case DeclContextKind::TopLevelCodeDecl: case DeclContextKind::AbstractFunctionDecl: case DeclContextKind::SerializedLocal: Result = Result->getParent(); continue; case DeclContextKind::Module: case DeclContextKind::FileUnit: return nullptr; case DeclContextKind::ExtensionDecl: case DeclContextKind::NominalTypeDecl: return Result; } } }