KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.coffer.types;
2
3 import polyglot.ext.jl.types.*;
4 import polyglot.types.*;
5 import polyglot.util.*;
6 import java.util.*;
7
8 /** An implementation of the <code>CofferMethodInstance</code> interface.
9  */

10 public class CofferMethodInstance_c extends MethodInstance_c
11                                 implements CofferMethodInstance
12 {
13     protected KeySet entryKeys;
14     protected KeySet returnKeys;
15     protected List throwConstraints;
16
17     public CofferMethodInstance_c(CofferTypeSystem ts, Position pos,
18         ReferenceType container, Flags flags, Type returnType,
19         String JavaDoc name, List argTypes,
20             KeySet entryKeys, KeySet returnKeys, List throwConstraints)
21     {
22     super(ts, pos, container, flags, returnType, name, argTypes, Collections.EMPTY_LIST);
23
24         this.entryKeys = entryKeys;
25         this.returnKeys = returnKeys;
26         this.throwConstraints = TypedList.copyAndCheck(throwConstraints, ThrowConstraint.class, true);
27
28         if (entryKeys == null)
29             throw new InternalCompilerError("null entry keys for " + this);
30     }
31
32     public KeySet entryKeys() {
33     return entryKeys;
34     }
35
36     public KeySet returnKeys() {
37     return returnKeys;
38     }
39
40     public List throwConstraints() {
41         return throwConstraints;
42     }
43
44     public List throwTypes() {
45         return new CachingTransformingList(throwConstraints, new GetType());
46     }
47
48     public MethodInstance throwTypes(List throwTypes) {
49         Iterator i = throwTypes.iterator();
50         Iterator j = throwConstraints.iterator();
51
52         boolean changed = false;
53
54         List l = new LinkedList();
55
56         while (i.hasNext() && j.hasNext()) {
57             Type t = (Type) i.next();
58             ThrowConstraint c = (ThrowConstraint) j.next();
59             if (t != c.throwType()) {
60                 c = c.throwType(t);
61                 changed = true;
62             }
63
64             l.add(c);
65         }
66
67         if (i.hasNext() || j.hasNext()) {
68             throw new InternalCompilerError("unimplemented");
69         }
70
71         if (! changed) {
72             return this;
73         }
74
75         return (MethodInstance) throwConstraints(l);
76     }
77
78     public class GetType implements Transformation {
79         public Object JavaDoc transform(Object JavaDoc o) {
80             return ((ThrowConstraint) o).throwType();
81         }
82     }
83
84     public CofferProcedureInstance entryKeys(KeySet entryKeys) {
85     CofferMethodInstance_c n = (CofferMethodInstance_c) copy();
86         n.entryKeys = entryKeys;
87     return n;
88     }
89
90     public CofferProcedureInstance returnKeys(KeySet returnKeys) {
91     CofferMethodInstance_c n = (CofferMethodInstance_c) copy();
92         n.returnKeys = returnKeys;
93     return n;
94     }
95
96     public CofferProcedureInstance throwConstraints(List throwConstraints) {
97     CofferMethodInstance_c n = (CofferMethodInstance_c) copy();
98         n.throwConstraints = TypedList.copyAndCheck(throwConstraints, ThrowConstraint.class, true);
99     return n;
100     }
101
102     public boolean canOverrideImpl(MethodInstance mj, boolean quiet) throws SemanticException {
103         CofferMethodInstance mi = this;
104
105         KeySet e;
106         KeySet r;
107         List l;
108
109         if (mj instanceof CofferMethodInstance) {
110             e = ((CofferMethodInstance) mj).entryKeys();
111             r = ((CofferMethodInstance) mj).returnKeys();
112             l = ((CofferMethodInstance) mj).throwConstraints();
113         }
114         else {
115             CofferTypeSystem ts = (CofferTypeSystem) this.ts;
116             e = ts.emptyKeySet(position());
117             r = ts.emptyKeySet(position());
118             l = Collections.EMPTY_LIST;
119         }
120
121         // Can pass through more keys.
122
KeySet newKeys = entryKeys.removeAll(e);
123
124         if (! returnKeys.equals(r.addAll(newKeys))) {
125             if (quiet) return false;
126             throw new SemanticException(mi.signature() + " in " + mi.container() +
127                                         " cannot override " +
128                                         mj.signature() + " in " + mj.container() +
129                                         ";",
130                                         mi.position());
131         }
132
133         CONSTRAINTS:
134         for (Iterator i = throwConstraints.iterator(); i.hasNext(); ) {
135             ThrowConstraint c = (ThrowConstraint) i.next();
136
137             for (Iterator j = l.iterator(); j.hasNext(); ) {
138                 ThrowConstraint superC = (ThrowConstraint) j.next();
139
140                 if (superC.throwType().equals(c.throwType())) {
141                     if (! c.keys().equals(superC.keys().addAll(newKeys))) {
142                         if (quiet) return false;
143                         throw new SemanticException(mi.signature() + " in " + mi.container() +
144                                 " cannot override " +
145                                 mj.signature() + " in " + mj.container() +
146                                 ";",
147                                 mi.position());
148                     }
149                     continue CONSTRAINTS;
150                 }
151             }
152
153             if (! c.keys().equals(newKeys)) {
154                 if (quiet) return false;
155                 throw new SemanticException(mi.signature() + " in " + mi.container() +
156                         " cannot override " +
157                         mj.signature() + " in " + mj.container() +
158                         ";",
159                         mi.position());
160             }
161         }
162
163         return super.canOverrideImpl(mj, quiet);
164     }
165
166     public String JavaDoc toString() {
167         return super.toString() + " " + entryKeys + "->" + returnKeys +
168                                   " throws " + throwConstraints;
169     }
170 }
171
Popular Tags