diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index d36f2cb13090843d59689191f2a324b0bd0d1ed2..497d5573defce694727b09f90e59cba06895d145 100644 --- a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag +++ b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag @@ -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; } diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.relast b/scope4j/src/main/jastadd/ProgramToScopeTree.relast index ea1efaa85d26e54ac7c9148c65f6c60f3e429e69..a1d84e0153fe227e592e61c3a456e9553462c79e 100644 --- a/scope4j/src/main/jastadd/ProgramToScopeTree.relast +++ b/scope4j/src/main/jastadd/ProgramToScopeTree.relast @@ -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; diff --git a/testprograms/simpleScope/A.java b/testprograms/simpleScope/A.java index 4a67af48b44a952c603201ef2ff4aeb597b55cde..d16692427bf3cd8423166c9ec809fd9bd0887c27 100644 --- a/testprograms/simpleScope/A.java +++ b/testprograms/simpleScope/A.java @@ -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); }