KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > visit > AmbiguityRemover


1 package polyglot.visit;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Set JavaDoc;
6 import java.util.Stack JavaDoc;
7
8 import polyglot.ast.Node;
9 import polyglot.ast.NodeFactory;
10 import polyglot.frontend.Job;
11 import polyglot.main.Report;
12 import polyglot.types.*;
13 import polyglot.util.Enum;
14
15 /**
16  * A visitor which traverses the AST and remove ambiguities found in fields,
17  * method signatures and the code itself.
18  */

19 public class AmbiguityRemover extends ContextVisitor
20 {
21     public static class Kind extends Enum JavaDoc {
22         protected Kind(String JavaDoc name) {
23             super(name);
24         }
25     }
26
27     public static final Kind SUPER = new Kind("disam-super");
28     public static final Kind SIGNATURES = new Kind("disam-sigs");
29     public static final Kind FIELDS = new Kind("disam-fields");
30     public static final Kind ALL = new Kind("disam-all");
31
32     protected Kind kind;
33
34     public AmbiguityRemover(Job job, TypeSystem ts, NodeFactory nf, Kind kind) {
35         super(job, ts, nf);
36         this.kind = kind;
37     }
38
39     public Kind kind() {
40         return kind;
41     }
42
43     protected NodeVisitor enterCall(Node n) throws SemanticException {
44         if (Report.should_report(Report.visit, 2))
45         Report.report(2, ">> " + kind + "::enter " + n);
46         NodeVisitor v = n.del().disambiguateEnter(this);
47         if (Report.should_report(Report.visit, 2))
48         Report.report(2, "<< " + kind + "::enter " + n + " -> " + v);
49         return v;
50     }
51
52     protected Node leaveCall(Node old, Node n, NodeVisitor v) throws SemanticException {
53         if (Report.should_report(Report.visit, 2))
54         Report.report(2, ">> " + kind + "::leave " + n);
55         Node m = n.del().disambiguate((AmbiguityRemover) v);
56         if (Report.should_report(Report.visit, 2))
57         Report.report(2, "<< " + kind + "::leave " + n + " -> " + m);
58         return m;
59     }
60     
61     /**
62      * Add dependencies for the job to the super classes and interface classes
63      * of <code>ct</code>.
64      */

65     public void addSuperDependencies(ClassType ct) {
66         // track which classtypes we've seen, since it may be the
67
// case that the class types are (incorrectly) circular.
68
Set JavaDoc seen = new HashSet JavaDoc();
69             
70         Stack JavaDoc s = new Stack JavaDoc();
71         s.push(ct);
72         while (! s.isEmpty()) {
73             Type t = (Type) s.pop();
74             if (t.isClass()) {
75                 ClassType classt = t.toClass();
76                 
77                 if (seen.contains(classt)) {
78                     continue;
79                 }
80                 else {
81                     seen.add(classt);
82                 }
83                 
84                 // add a dependency if its a parsed class type.
85
if (classt instanceof ParsedClassType) {
86                     this.job().extensionInfo().addDependencyToCurrentJob(
87                                       ((ParsedClassType)classt).fromSource());
88                 }
89                 
90                 // add all the interfaces to the stack.
91
for (Iterator JavaDoc i = classt.interfaces().iterator(); i.hasNext(); ) {
92                     s.push(i.next());
93                 }
94     
95                 // add the superType to the stack.
96
if (classt.superType() != null) {
97                     s.push(classt.superType());
98                 }
99             }
100         }
101
102     }
103
104     public String JavaDoc toString() {
105     return super.toString() + "(" + kind + ")";
106     }
107 }
108
Popular Tags