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
Branches
No related tags found
No related merge requests found
...@@ -16,7 +16,8 @@ aspect ProgramToScopeTree { ...@@ -16,7 +16,8 @@ aspect ProgramToScopeTree {
// collect all scopes // collect all scopes
TypeDecl contributes scope() to ASTNode.scope() for containingScope(); TypeDecl contributes scope() to ASTNode.scope() for containingScope();
Block 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 // collect all elements
Declarator contributes asDeclaration() to ASTNode.scope() for containingScope(); Declarator contributes asDeclaration() to ASTNode.scope() for containingScope();
...@@ -39,6 +40,18 @@ aspect ProgramToScopeTree { ...@@ -39,6 +40,18 @@ aspect ProgramToScopeTree {
return scope; 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() { syn lazy JavaDeclaration Declarator.asDeclaration() {
JavaDeclaration decl = new JavaDeclaration(getID()); JavaDeclaration decl = new JavaDeclaration(getID());
decl.setDeclarator(this); decl.setDeclarator(this);
...@@ -55,7 +68,8 @@ aspect ProgramToScopeTree { ...@@ -55,7 +68,8 @@ aspect ProgramToScopeTree {
aspect ScopeGenerationAttributes { 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(); inh lazy ASTNode ASTNode.containingScope();
// contained in scope: // contained in scope:
eq Program.getChild().containingScope() = this; eq Program.getChild().containingScope() = this;
...@@ -67,5 +81,7 @@ aspect ScopeGenerationAttributes { ...@@ -67,5 +81,7 @@ aspect ScopeGenerationAttributes {
eq MethodDecl.getParameter().containingScope() = getBlock(); eq MethodDecl.getParameter().containingScope() = getBlock();
eq ConstructorDecl.getParameter().containingScope() = getBlock(); eq ConstructorDecl.getParameter().containingScope() = getBlock();
eq TryWithResources.getResource().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; ...@@ -7,11 +7,11 @@ rel TypeDeclScope.typeDecl -> TypeDecl;
BlockScope : Scope; BlockScope : Scope;
rel BlockScope.block -> Block; rel BlockScope.block -> Block;
// MethodDeclScope : Scope; ForStmtScope : Scope;
// rel MethodDeclScope.methodDecl -> MethodDecl; rel ForStmtScope.forStmt -> ForStmt;
// ConstructorDeclScope : Scope; EnhancedForStmtScope : Scope;
// rel ConstructorDeclScope.constructorDecl -> ConstructorDecl; rel EnhancedForStmtScope.enhancedForStmt -> EnhancedForStmt;
JavaDeclaration : Declaration; JavaDeclaration : Declaration;
rel JavaDeclaration.declarator -> Declarator; rel JavaDeclaration.declarator -> Declarator;
......
...@@ -20,6 +20,12 @@ public abstract class ClassA { ...@@ -20,6 +20,12 @@ public abstract class ClassA {
int localVarA = 3; int localVarA = 3;
} }
class Local {
{
for (int localVarA = 0; localVarA < 10; localVarA++) System.out.println(localVarA);
}
}
// this is shadowed (over two levels, not forbidden) // this is shadowed (over two levels, not forbidden)
int fieldA; int fieldA;
...@@ -32,13 +38,6 @@ public abstract class ClassA { ...@@ -32,13 +38,6 @@ public abstract class ClassA {
) { /* do stuff */ } catch (java.io.IOException e) {/* do stuff */} ) { /* 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) // this does not appear as a scope (and, more importantly, the parameters are not added anywhere else)
public abstract void methodNameB(int parameterForAbstractMethodB); 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