Skip to content
Snippets Groups Projects
Commit a5dc053f authored by Johannes Mey's avatar Johannes Mey
Browse files

support for and enhanced for statement scopes

parent 6e540cea
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,8 @@ aspect ProgramToScopeTree {
// collect all scopes
TypeDecl contributes scope() to ASTNode.scope() for containingScope();
Block contributes scope() to ASTNode.scope() for containingScope();
// for and enhanced-for create a scope even if they do not contain a block
ForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope();
EnhancedForStmt contributes scope() when !(getStmt() instanceof Block) to ASTNode.scope() for containingScope();
// collect all elements
Declarator contributes asDeclaration() to ASTNode.scope() for containingScope();
......@@ -39,6 +40,18 @@ aspect ProgramToScopeTree {
return scope;
}
syn lazy ForStmtScope ForStmt.asScope() {
ForStmtScope scope = new ForStmtScope();
scope.setForStmt(this);
return scope;
}
syn lazy EnhancedForStmtScope EnhancedForStmt.asScope() {
EnhancedForStmtScope scope = new EnhancedForStmtScope();
scope.setEnhancedForStmt(this);
return scope;
}
syn lazy JavaDeclaration Declarator.asDeclaration() {
JavaDeclaration decl = new JavaDeclaration(getID());
decl.setDeclarator(this);
......@@ -55,7 +68,8 @@ aspect ProgramToScopeTree {
aspect ScopeGenerationAttributes {
/** determine the scope an AST element is contained in or belongs to */
// TODO this is only correct for declarations, not for all AST nodes!
/** determine the scope an AST element is contained in or belongs to.*/
inh lazy ASTNode ASTNode.containingScope();
// contained in scope:
eq Program.getChild().containingScope() = this;
......@@ -67,5 +81,7 @@ aspect ScopeGenerationAttributes {
eq MethodDecl.getParameter().containingScope() = getBlock();
eq ConstructorDecl.getParameter().containingScope() = getBlock();
eq TryWithResources.getResource().containingScope() = getBlock();
eq EnhancedForStmt.getVariableDecl().containingScope() = (getStmt() instanceof Block) ? getStmt() : null;
eq EnhancedForStmt.getVariableDecl().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
eq ForStmt.getInitStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
eq ForStmt.getUpdateStmt().containingScope() = (getStmt() instanceof Block) ? getStmt() : this;
}
......@@ -7,11 +7,11 @@ rel TypeDeclScope.typeDecl -> TypeDecl;
BlockScope : Scope;
rel BlockScope.block -> Block;
// MethodDeclScope : Scope;
// rel MethodDeclScope.methodDecl -> MethodDecl;
ForStmtScope : Scope;
rel ForStmtScope.forStmt -> ForStmt;
// ConstructorDeclScope : Scope;
// rel ConstructorDeclScope.constructorDecl -> ConstructorDecl;
EnhancedForStmtScope : Scope;
rel EnhancedForStmtScope.enhancedForStmt -> EnhancedForStmt;
JavaDeclaration : Declaration;
rel JavaDeclaration.declarator -> Declarator;
......
......@@ -20,6 +20,12 @@ public abstract class ClassA {
int localVarA = 3;
}
class Local {
{
for (int localVarA = 0; localVarA < 10; localVarA++) System.out.println(localVarA);
}
}
// this is shadowed (over two levels, not forbidden)
int fieldA;
......@@ -32,13 +38,6 @@ public abstract class ClassA {
) { /* do stuff */ } catch (java.io.IOException e) {/* do stuff */}
}
int i;
class Local {
{
for (int i = 0; i < 10; i++) System.out.println(i);
}
}
// this does not appear as a scope (and, more importantly, the parameters are not added anywhere else)
public abstract void methodNameB(int parameterForAbstractMethodB);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment