if (node instanceof CompilationUnit) {
return scopeForCompilationUnit((CompilationUnit) node);
}
AstNode parent = node.getParent();
if (parent == null) {
throw new AnalysisException("Cannot create scope: node is not part of a CompilationUnit");
}
Scope scope = scopeForAstNode(parent);
if (node instanceof ClassDeclaration) {
ClassElement element = ((ClassDeclaration) node).getElement();
if (element == null) {
throw new AnalysisException("Cannot build a scope for an unresolved class");
}
scope = new ClassScope(new TypeParameterScope(scope, element), element);
} else if (node instanceof ClassTypeAlias) {
ClassElement element = ((ClassTypeAlias) node).getElement();
if (element == null) {
throw new AnalysisException("Cannot build a scope for an unresolved class type alias");
}
scope = new ClassScope(new TypeParameterScope(scope, element), element);
} else if (node instanceof ConstructorDeclaration) {
ConstructorElement element = ((ConstructorDeclaration) node).getElement();
if (element == null) {
throw new AnalysisException("Cannot build a scope for an unresolved constructor");
}
FunctionScope functionScope = new FunctionScope(scope, element);
functionScope.defineParameters();
scope = functionScope;
} else if (node instanceof FunctionDeclaration) {
ExecutableElement element = ((FunctionDeclaration) node).getElement();
if (element == null) {
throw new AnalysisException("Cannot build a scope for an unresolved function");
}
FunctionScope functionScope = new FunctionScope(scope, element);
functionScope.defineParameters();
scope = functionScope;
} else if (node instanceof FunctionTypeAlias) {
scope = new FunctionTypeScope(scope, ((FunctionTypeAlias) node).getElement());
} else if (node instanceof MethodDeclaration) {
ExecutableElement element = ((MethodDeclaration) node).getElement();
if (element == null) {
throw new AnalysisException("Cannot build a scope for an unresolved method");
}
FunctionScope functionScope = new FunctionScope(scope, element);
functionScope.defineParameters();
scope = functionScope;
}