01: package org.acm.seguin.pmd.symboltable;
02:
03: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
04: import net.sourceforge.jrefactory.ast.ASTPrimaryExpression;
05: import net.sourceforge.jrefactory.parser.ChildrenVisitor;
06:
07: import java.util.Iterator;
08:
09: public class SymbolFacade extends ChildrenVisitor {
10:
11: public void initializeWith(ASTCompilationUnit node) {
12: // first, traverse the AST and create all the scopes
13: BasicScopeCreationVisitor sc = new BasicScopeCreationVisitor(
14: new BasicScopeFactory());
15: node.jjtAccept(sc, null);
16:
17: // traverse the AST and pick up all the declarations
18: DeclarationFinder df = new DeclarationFinder();
19: node.jjtAccept(df, null);
20:
21: // finally, traverse the AST and pick up all the name occurrences
22: node.jjtAccept(this , null);
23: }
24:
25: public Object visit(ASTPrimaryExpression node, Object data) {
26: NameOccurrences qualifiedNames = new NameOccurrences(node);
27: NameDeclaration decl = null;
28: for (Iterator i = qualifiedNames.iterator(); i.hasNext();) {
29: NameOccurrence occ = (NameOccurrence) i.next();
30: Search search = new Search(occ);
31: if (decl == null) {
32: // doing the first name lookup
33: search.execute();
34: decl = search.getResult();
35: if (decl == null) {
36: // we can't find it, so just give up
37: // when we decide searches across compilation units like a compiler would, we'll
38: // force this to either find a symbol or throw a "cannot resolve symbol" Exception
39: break;
40: }
41: } else {
42: // now we've got a scope we're starting with, so work from there
43: search.execute(decl.getScope());
44: decl = search.getResult();
45: }
46: }
47: return super.visit(node, data);
48: }
49:
50: }
|