KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > coffer > ast > TrackedTypeNode_c


1 package polyglot.ext.coffer.ast;
2
3 import polyglot.ext.jl.ast.*;
4 import polyglot.ext.coffer.types.*;
5 import polyglot.ast.*;
6 import polyglot.types.*;
7 import polyglot.visit.*;
8 import polyglot.util.*;
9 import java.util.*;
10
11 /** An implementation of the <code>TrackedTypeNode</code> interface,
12  * a type node for a class instantiated with a key.
13  */

14 public class TrackedTypeNode_c extends TypeNode_c implements TrackedTypeNode
15 {
16     protected TypeNode base;
17     protected KeyNode key;
18
19     public TrackedTypeNode_c(Position pos, KeyNode key, TypeNode base) {
20     super(pos);
21         this.key = key;
22     this.base = base;
23     }
24
25     public TypeNode base() {
26     return this.base;
27     }
28
29     public TrackedTypeNode base(TypeNode base) {
30     TrackedTypeNode_c n = (TrackedTypeNode_c) copy();
31     n.base = base;
32     return n;
33     }
34
35     public KeyNode key() {
36     return this.key;
37     }
38
39     public TrackedTypeNode key(KeyNode key) {
40     TrackedTypeNode_c n = (TrackedTypeNode_c) copy();
41     n.key = key;
42     return n;
43     }
44
45     protected TrackedTypeNode_c reconstruct(TypeNode base, KeyNode key) {
46     if (base != this.base || key != this.key) {
47         TrackedTypeNode_c n = (TrackedTypeNode_c) copy();
48         n.base = base;
49         n.key = key;
50         return n;
51     }
52
53     return this;
54     }
55
56     public Node visitChildren(NodeVisitor v) {
57     TypeNode base = (TypeNode) visitChild(this.base, v);
58     KeyNode key = (KeyNode) visitChild(this.key, v);
59     return reconstruct(base, key);
60     }
61
62     public Node disambiguate(AmbiguityRemover sc) throws SemanticException {
63         CofferTypeSystem ts = (CofferTypeSystem) sc.typeSystem();
64     Type b = (Type) base.type();
65
66     if (! b.isCanonical()) {
67         throw new SemanticException(
68         "Cannot instantiate from a non-canonical type " + b);
69     }
70
71         if (! (b instanceof CofferClassType)) {
72         throw new SemanticException(
73         "Cannot instantiate from a non-polymorphic type " + b);
74     }
75
76     CofferClassType t = (CofferClassType) b;
77
78         Key key = this.key.key();
79
80         if (! key.isCanonical()) {
81         throw new SemanticException(
82         "Cannot instantiate from a non-canonical key " + key);
83     }
84
85         Key formal = t.key();
86         Map subst = new HashMap();
87         subst.put(formal, key);
88
89     return sc.nodeFactory().CanonicalTypeNode(position(),
90                                                   ts.subst(t, subst));
91     }
92
93     public Node typeCheck(TypeChecker tc) throws SemanticException {
94     throw new InternalCompilerError(position(),
95         "Cannot type check ambiguous node " + this + ".");
96     }
97
98     public Node exceptionCheck(ExceptionChecker ec) throws SemanticException {
99     throw new InternalCompilerError(position(),
100         "Cannot exception check ambiguous node " + this + ".");
101     }
102
103     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
104         w.write("tracked(");
105         w.write(key.toString());
106         w.write(") ");
107         print(base, w, tr);
108     }
109
110     public void translate(CodeWriter w, Translator tr) {
111     throw new InternalCompilerError(position(),
112         "Cannot translate ambiguous node " + this + ".");
113     }
114
115     public String JavaDoc toString() {
116         return "tracked(" + key + ") " + base;
117     }
118 }
119
Popular Tags