From 6e540ceaa9b1577be6be5e5e617ecc38c76112e2 Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Tue, 24 Dec 2019 11:03:11 +0100
Subject: [PATCH] simplify (and correct) scope analysis

---
 .../src/main/jastadd/ProgramToScopeTree.jrag  | 26 ++++++++-----------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag
index cb38283..d36f2cb 100644
--- a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag
+++ b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag
@@ -16,12 +16,11 @@ 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
 
   // collect all elements
-  // TODO exclude declarators not in statements (for, enhancedfor, try)
   Declarator contributes asDeclaration() to ASTNode.scope() for containingScope();
-  ResourceDeclaration contributes asDeclaration() to ASTNode.scope() for succeedingScope();
-  ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for succeedingScope();
+  ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for containingScope();
 
   /** fallback attribute to ensure every AST element could pontentially be a scope */
   syn Scope ASTNode.asScope() {
@@ -56,20 +55,17 @@ aspect ProgramToScopeTree {
 
 aspect ScopeGenerationAttributes {
 
-  /** determine the scope an AST element is contained in */
+  /** 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;
   eq TypeDecl.getChild().containingScope() = this;
   eq Block.getChild().containingScope() = this;
-
-  /** determine the scope an AST element belongs to (but is not technically contained in) */
-  inh ASTNode ParameterDeclaration.succeedingScope();
-  eq LambdaExpr.getLambdaParameters().succeedingScope() = getLambdaBody().isBlockBody()? ((BlockLambdaBody)getLambdaBody()).getBlock() : null;
-  eq BasicCatch.getParameter().succeedingScope() = getBlock();
-  eq MethodDecl.getParameter().succeedingScope() = getBlock();
-  eq ConstructorDecl.getParameter().succeedingScope() = getBlock();
-  inh ASTNode ResourceDeclaration.succeedingScope();
-  eq TryWithResources.getResource().succeedingScope() = getBlock();
-  eq EnhancedForStmt.getVariableDecl().succeedingScope() = (getStmt() instanceof Block) ? getStmt() : null;
-  eq VarDeclStmt.getDeclarator().succeedingScope() = null;
+  // belonging to scope
+  eq LambdaExpr.getLambdaParameters().containingScope() = getLambdaBody().isBlockBody()? ((BlockLambdaBody)getLambdaBody()).getBlock() : null;
+  eq BasicCatch.getParameter().containingScope() = getBlock();
+  eq MethodDecl.getParameter().containingScope() = getBlock();
+  eq ConstructorDecl.getParameter().containingScope() = getBlock();
+  eq TryWithResources.getResource().containingScope() = getBlock();
+  eq EnhancedForStmt.getVariableDecl().containingScope() = (getStmt() instanceof Block) ? getStmt() : null;
 }
-- 
GitLab