From a5dc053fc7fc343ed294fe45a065189aaf772e32 Mon Sep 17 00:00:00 2001 From: Johannes Mey <johannes.mey@tu-dresden.de> Date: Tue, 24 Dec 2019 11:21:18 +0100 Subject: [PATCH] support for and enhanced for statement scopes --- .../src/main/jastadd/ProgramToScopeTree.jrag | 22 ++++++++++++++++--- .../main/jastadd/ProgramToScopeTree.relast | 8 +++---- testprograms/simpleScope/A.java | 13 +++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index d36f2cb..497d557 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 ea1efaa..a1d84e0 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 4a67af4..d166924 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); } -- GitLab