KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > coffer > types > CofferSubst_c


1 package polyglot.ext.coffer.types;
2
3 import polyglot.types.*;
4 import polyglot.ext.param.types.*;
5 import polyglot.util.*;
6 import java.util.*;
7
8 public class CofferSubst_c extends Subst_c implements CofferSubst
9 {
10     public CofferSubst_c(CofferTypeSystem ts, Map subst, Map cache) {
11         super(ts, subst, cache);
12
13         for (Iterator i = entries(); i.hasNext(); ) {
14             Map.Entry e = (Map.Entry) i.next();
15             if (e.getKey() instanceof Key && e.getValue() instanceof Key)
16                 continue;
17             throw new InternalCompilerError("bad map: " + subst);
18         }
19     }
20
21     public Key get(Key formal) {
22         return (Key) subst.get(formal);
23     }
24
25     public void put(Key formal, Key actual) {
26         subst.put(formal, actual);
27     }
28
29     ////////////////////////////////////////////////////////////////
30
// Override substitution methods to handle Coffer constructs
31

32     public ClassType substClassType(ClassType t) {
33         // Don't bother trying to substitute into a non-Coffer class.
34
if (! (t instanceof CofferClassType)) {
35             return t;
36         }
37
38         return new CofferSubstClassType_c((CofferTypeSystem) ts, t.position(),
39                                          (CofferClassType) t, this);
40     }
41
42     public MethodInstance substMethod(MethodInstance mi) {
43         mi = super.substMethod(mi);
44
45         if (mi instanceof CofferMethodInstance) {
46             CofferMethodInstance vmi = (CofferMethodInstance) mi;
47
48             vmi = (CofferMethodInstance) vmi.entryKeys(substKeySet(vmi.entryKeys()));
49             vmi = (CofferMethodInstance) vmi.returnKeys(substKeySet(vmi.returnKeys()));
50             vmi = (CofferMethodInstance) vmi.throwConstraints(substThrowConstraintList(vmi.throwConstraints()));
51
52             mi = vmi;
53         }
54
55         return mi;
56     }
57
58     public ConstructorInstance substConstructor(ConstructorInstance ci) {
59         ci = super.substConstructor(ci);
60
61         if (ci instanceof CofferConstructorInstance) {
62             CofferConstructorInstance vci = (CofferConstructorInstance) ci;
63
64             vci = (CofferConstructorInstance) vci.entryKeys(substKeySet(vci.entryKeys()));
65             vci = (CofferConstructorInstance) vci.returnKeys(substKeySet(vci.returnKeys()));
66             vci = (CofferConstructorInstance) vci.throwConstraints(substThrowConstraintList(vci.throwConstraints()));
67
68             ci = vci;
69         }
70
71         return ci;
72     }
73
74     ////////////////////////////////////////////////////////////////
75
// Substitution methods for Coffer constructs
76

77     public class ConstraintXform implements Transformation {
78         public Object JavaDoc transform(Object JavaDoc o) {
79             return substThrowConstraint((ThrowConstraint) o);
80         }
81     }
82
83     public List substThrowConstraintList(List l) {
84         return new CachingTransformingList(l, new ConstraintXform());
85     }
86
87     public ThrowConstraint substThrowConstraint(ThrowConstraint c) {
88         if (c == null) {
89             return null;
90         }
91
92         CofferTypeSystem ts = (CofferTypeSystem) this.ts;
93         ThrowConstraint c2;
94
95         Type t = substType(c.throwType());
96         KeySet k = substKeySet(c.keys());
97
98         if (t != c.throwType() || k != c.keys()) {
99             return c.throwType(t).keys(k);
100         }
101
102         return c;
103     }
104
105     public KeySet substKeySet(KeySet keys) {
106         if (keys == null) {
107             return null;
108         }
109
110         boolean changed = false;
111
112         CofferTypeSystem ts = (CofferTypeSystem) this.ts;
113         KeySet newKeys = ts.emptyKeySet(keys.position());
114         for (Iterator i = keys.iterator(); i.hasNext(); ) {
115             Key key = (Key) i.next();
116             Key key2 = substKey(key);
117             if (key != key2)
118                 changed = true;
119             newKeys = newKeys.add(substKey(key));
120         }
121
122         if (! changed)
123             newKeys = keys;
124
125         return newKeys;
126     }
127
128     public Key substKey(Key key) {
129     if (key == null) {
130         return null;
131     }
132
133         Key newKey = (Key) subst.get(key);
134
135         if (newKey != null) {
136             return newKey;
137         }
138
139         return key;
140     }
141 }
142
Popular Tags