diff --git a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag index b7af23bbd86947137cbf626d5eff22676ee9e0a3..3bdad9b7e2ea11387a30f27e88ec07398a82b380 100644 --- a/scope4j/src/main/jastadd/ProgramToScopeTree.jrag +++ b/scope4j/src/main/jastadd/ProgramToScopeTree.jrag @@ -2,36 +2,34 @@ aspect ProgramToScopeTree { coll ScopeTree Program.scopeTree() [asScopeTree()] with addElement root Program; TypeDecl contributes scope() when !isClassDecl() to Program.scopeTree(); - ClassDecl contributes publicScope() when !isInnerType() && !superclass().compilationUnit().fromSource() to Program.scopeTree(); + ClassDecl contributes protectedScope() when !isInnerType() && !superclass().compilationUnit().fromSource() to Program.scopeTree(); /** a relational nta collection attribute to compute the scope tree */ coll Scope ASTNode.scope() [asScope()] with addElement root Program; - coll TypeDeclScope ClassDecl.packageScope() [asPackageScope()] with addElement root Program; coll TypeDeclScope ClassDecl.protectedScope() [asProtectedScope()] with addElement root Program; - coll TypeDeclScope ClassDecl.publicScope() [asPublicScope()] with addElement root Program; // collect all scopes TypeDecl contributes scope() when !isClassDecl() to ASTNode.scope() for containingScope(); - ClassDecl contributes publicScope() when isInnerClass() to ASTNode.scope() for containingScope(); - ClassDecl contributes publicScope() - when superclass().compilationUnit().fromSource() && !isInnerClass() && accessibleFromPackage(containingScope().hostPackage()) - to ClassDecl.packageScope() + ClassDecl contributes protectedScope() + when !(!isInnerClass() && superclass().isClassDecl() && superclass().compilationUnit().fromSource()) + to ASTNode.scope() for containingScope(); - ClassDecl contributes publicScope() - when superclass().compilationUnit().fromSource() && !isInnerClass() && !accessibleFromPackage(containingScope().hostPackage()) + ClassDecl contributes protectedScope() + when !isInnerClass() && superclass().isClassDecl() && superclass().compilationUnit().fromSource() to ClassDecl.protectedScope() - for containingScope(); + for superclass(); Block contributes scope() to ASTNode.scope() for containingScope(); 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() when !isField() || isPrivate() to ASTNode.scope() for containingScope(); - Declarator contributes asDeclaration() when isField() && !(isPrivate() || isProtected() || isPublic()) to ClassDecl.packageScope() for containingScope(); - Declarator contributes asDeclaration() when isField() && isProtected() to ClassDecl.protectedScope() for containingScope(); - Declarator contributes asDeclaration() when isField() && isPublic() to ClassDecl.publicScope() for containingScope(); + Declarator contributes asDeclaration() when isField() && !isPrivate() to ClassDecl.protectedScope() for containingScope(); ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for containingScope(); + // if it was not for the single line in asProtectedScope(), the rest of this aspect could have been generated + // automatically, which would have been much nicer! + syn lazy ScopeTree Program.asScopeTree() { ScopeTree tree = new ScopeTree(); tree.setProgram(this); @@ -49,26 +47,16 @@ aspect ProgramToScopeTree { return scope; } - syn lazy TypeDeclScope ClassDecl.asPublicScope() { - TypeDeclScope scope = new TypeDeclScope(); - scope.setTypeDecl(this); - scope.addElement(protectedScope()); - return scope; - } - syn lazy TypeDeclScope ClassDecl.asProtectedScope() { TypeDeclScope scope = new TypeDeclScope(); scope.setTypeDecl(this); - scope.addElement(packageScope()); - return scope; - } - - syn lazy TypeDeclScope ClassDecl.asPackageScope() { - TypeDeclScope scope = new TypeDeclScope(); - scope.setTypeDecl(this); - scope.addElement(scope()); + scope.addElement(scope()); // this irregular statement is necessary because of either a bug or a limitation in JastAdd collections return scope; } + // the following commented statement unfortunately does not work, because during the contribution phase, the information + // which target should be contributed to is lost if there are multiple targets. Maybe I got it wrong, but I tried many things + // and JastAdd always wants to also add the contribution made in line 17ff to this target as well. + // ClassDecl contributes scope() to ClassDecl.protectedScope() for this; syn lazy BlockScope Block.asScope() { BlockScope scope = new BlockScope(); @@ -124,14 +112,6 @@ aspect ScopeGenerationAttributes { 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; - // belonging to visibility scope - eq CompilationUnit.getTypeDecl(int i).containingScope() { - if (getTypeDecl(i).isClassDecl()) { - TypeDecl superclass = ((ClassDecl)getTypeDecl(i)).superclass(); - return superclass.compilationUnit().fromSource() ? superclass : this; - } - return this; - } // allow host package to be called from all AST nodes inh String ASTNode.hostPackage();