KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > param > types > ParamTypeSystem_c


1 package polyglot.ext.param.types;
2
3 import polyglot.ext.jl.types.TypeSystem_c;
4 import polyglot.types.*;
5 import polyglot.util.*;
6
7 import java.util.*;
8
9 /**
10  * Implementation of type system for parameterized types.
11  */

12 public abstract class ParamTypeSystem_c extends TypeSystem_c
13     implements ParamTypeSystem
14 {
15     /**
16      * Create a new mutable PClass.
17      *
18      * @param pos The position of the PClass.
19      */

20     public MuPClass mutablePClass(Position pos) {
21         return new MuPClass_c(this, pos);
22     }
23
24     /**
25      * Instantiate a parametric type on a list of actual parameters.
26      *
27      * @param pos The position of the instantiated type
28      * @param base The parameterized type
29      * @param actuals The list of actuals
30      *
31      * @throws SemanticException when the actuals do not agree with the formals
32      */

33     public ClassType instantiate(Position pos, PClass base, List actuals)
34         throws SemanticException
35     {
36         checkInstantiation(pos, base, actuals);
37         return uncheckedInstantiate(pos, base, actuals);
38     }
39
40     /**
41      * Check that an instantiation of a parametric type on a list of actual
42      * parameters is legal.
43      *
44      * @param pos The position of the instantiated type
45      * @param base The parameterized type
46      * @param actuals The list of actuals
47      *
48      * @throws SemanticException when the actuals do not agree with the formals
49      */

50     protected void checkInstantiation(Position pos, PClass base,
51         List actuals) throws SemanticException
52     {
53         if (base.formals().size() != actuals.size()) {
54             throw new SemanticException("Wrong number of actual parameters " +
55                                         "for instantiation of \"" +
56                                         base + "\".", pos);
57         }
58     }
59
60     /**
61      * Instantiate a parametric type on a list of actual parameters, but
62      * do not check that the instantiation is legal.
63      *
64      * @param pos The position of the instantiated type
65      * @param base The parameterized type
66      * @param actuals The list of actuals
67      */

68     protected ClassType uncheckedInstantiate(Position pos, PClass base,
69         List actuals)
70     {
71         Map substMap = new HashMap();
72         Iterator i = base.formals().iterator();
73         Iterator j = actuals.iterator();
74
75         while (i.hasNext() && j.hasNext()) {
76             Object JavaDoc formal = i.next();
77             Object JavaDoc actual = j.next();
78             substMap.put(formal, actual);
79         }
80
81         if (i.hasNext() || j.hasNext()) {
82             throw new InternalCompilerError("Wrong number of actual " +
83                 "parameters for instantiation " + "of \"" + base + "\".", pos);
84         }
85
86         Type inst = subst(base.clazz(), substMap, new HashMap());
87         if (!inst.isClass()) {
88             throw new InternalCompilerError("Instantiating a PClass "
89                 + "produced something other than a ClassType.", pos);
90         }
91         
92         return inst.toClass();
93     }
94
95     /**
96      * Apply a parameter substitution to a type.
97      *
98      * @param t The type on which we perform substitutions.
99      * @param substMap Map from formal parameters to actuals; the formals are
100      * not necessarily formals of <code>t</code>.
101      */

102     public Type subst(Type t, Map substMap) {
103         return subst(t, substMap, new HashMap());
104     }
105
106     /**
107      * Apply a parameter substitution to a type.
108      *
109      * @param t The type on which we perform substitutions.
110      * @param substMap Map from formal parameters to actuals; the formals are
111      * not necessarily formals of <code>t</code>.
112      * @param cache Cache of substitutions performed, implemented as a map from
113      * type to substituted type. This is passed in to ensure pointers to
114      * outer classes are substituted correctly.
115      */

116     public Type subst(Type t, Map substMap, Map cache) {
117         return subst(substMap, cache).substType(t);
118     }
119
120     /**
121      * Create a substitutor.
122      *
123      * @param substMap Map from formal parameters to actuals; the formals are
124      * not necessarily formals of <code>t</code>.
125      * @param cache Cache of substitutions performed, implemented as a map from
126      * type to substituted type. This is passed in to ensure pointers to
127      * outer classes are substituted correctly.
128      */

129     public Subst subst(Map substMap, Map cache) {
130         return new Subst_c(this, substMap, cache);
131     }
132 }
133
Popular Tags