Skip to content
Snippets Groups Projects
Commit bb2b83c5 authored by Johannes Mey's avatar Johannes Mey
Browse files

create different scopes in a class for different field visibilities

parent e3d23f73
No related branches found
No related tags found
No related merge requests found
...@@ -4,43 +4,42 @@ aspect ProgramToScopeTree { ...@@ -4,43 +4,42 @@ aspect ProgramToScopeTree {
tree.setProgram(this); tree.setProgram(this);
for (CompilationUnit compilationUnit : getCompilationUnitList()) for (CompilationUnit compilationUnit : getCompilationUnitList())
for (TypeDecl typeDecl : compilationUnit.getTypeDeclList()) for (TypeDecl typeDecl : compilationUnit.getTypeDeclList()) {
if (typeDecl.isClassDecl()) {
tree.addElement(((ClassDecl)typeDecl).publicScope());
} else {
tree.addElement(typeDecl.scope()); tree.addElement(typeDecl.scope());
}
}
return tree; return tree;
} }
/** a relational nta collection attribute to compute the scope tree */ /** a relational nta collection attribute to compute the scope tree */
coll Scope ASTNode.scope() [asScope()] with addElement root Program; 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 // collect all scopes
TypeDecl contributes scope() to ASTNode.scope() for containingScope(); TypeDecl contributes scope() when !isClassDecl() to ASTNode.scope() for containingScope();
ClassDecl contributes publicScope() to ASTNode.scope() for containingScope();
Block contributes scope() to ASTNode.scope() for containingScope(); Block contributes scope() to ASTNode.scope() for containingScope();
ForStmt contributes scope() when !(getStmt() instanceof Block) 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(); 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() 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();
ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for containingScope(); ParameterDeclaration contributes asDeclaration() to ASTNode.scope() for containingScope();
Collection<Declaration> ClassDecl.createSuperClassFieldDeclarators() { syn lazy ScopeTree Program.asScopeTree() {
ArrayList<Declaration> result = new ArrayList<>(); ScopeTree tree = new ScopeTree();
TypeDecl supertype = superclass(); tree.setProgram(this);
System.out.println("supertype " + supertype.getID()); return tree;
while (supertype.isClassDecl() && supertype != unknownType()) {
for (BodyDecl bodyDecl : supertype.getBodyDeclList()) {
if (bodyDecl instanceof FieldDecl) {
for (FieldDeclarator declarator : ((FieldDecl)bodyDecl).getDeclaratorList()) {
JavaDeclaration declaration = new JavaDeclaration(declarator.getID());
declaration.setDeclarator(declarator);
result.add(declaration);
}
}
}
supertype = ((ClassDecl)supertype).superclass();
}
return result;
} }
/** fallback attribute to ensure every AST element could pontentially be a scope */ /** fallback attribute to ensure every AST element could pontentially be a scope */
...@@ -51,11 +50,27 @@ aspect ProgramToScopeTree { ...@@ -51,11 +50,27 @@ aspect ProgramToScopeTree {
syn lazy TypeDeclScope TypeDecl.asScope() { syn lazy TypeDeclScope TypeDecl.asScope() {
TypeDeclScope scope = new TypeDeclScope(); TypeDeclScope scope = new TypeDeclScope();
scope.setTypeDecl(this); scope.setTypeDecl(this);
if (isClassDecl()) { return scope;
for (Declaration declaration : ((ClassDecl)this).createSuperClassFieldDeclarators()) { }
scope.addElement(declaration);
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());
return scope; return scope;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment