KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > ConstructorCallExpr


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.cst.*;
29
30 import org.aspectj.compiler.base.bcg.CodeBuilder;
31 import org.aspectj.compiler.base.bcg.Label;
32
33 import org.aspectj.compiler.base.LocalClassPass;
34
35 /**
36   * @grammar enclosingInstanceExpr.[this|super](args)
37   * @child Expr enclosingInstanceExpr
38   * @property boolean isSuper
39   * @child Exprs args
40   * @property Constructor constructor
41   */

42
43 public class ConstructorCallExpr extends Expr {
44     public ConstructorDec getConstructorDec() { return (ConstructorDec)constructor.getCorrespondingDec(); }
45     public void setConstructorDec(ConstructorDec md) {
46         setConstructor((Constructor)md.getCorrespondingSemanticObject());
47     }
48
49     public boolean inStaticContext() { return true; }
50
51
52     // ------------------------------
53
// INTRO from SpecCheckerPass
54

55     public void checkSpec() {
56         if (getEnclosingInstanceExpr() != null) {
57             NameType ty = (NameType) getConstructorDec().getDeclaringType();
58             if (! ty.isInner()) {
59                 showError("Class " + ty.getId() + " is not an inner class");
60             }
61             Type exprTy = getEnclosingInstanceExpr().getType();
62             NameType enclosingTy = ty.getEnclosingType();
63             if (! exprTy.isSubtypeOf(enclosingTy)) {
64                 showTypeError(exprTy, enclosingTy);
65             }
66         }
67         super.checkSpec();
68     }
69
70     // ------------------------------
71
// INTRO from FlowCheckerPass
72

73     public void walkFlow(FlowCheckerPass w) {
74         w.process(getEnclosingInstanceExpr());
75         w.process(getArgs());
76         if (getConstructor().getThrows() != null) {
77             TypeDs ts = getConstructor().getThrows();
78             for (int i = 0, len = ts.size(); i < len; i++) {
79                 w.setExns(w.getExns().add((NameType) ts.get(i).getType()));
80             }
81         }
82     }
83
84     //INTRO from ScopeWalker
85
public ASTObject postScope(ScopeWalker walker) {
86         if (constructor != null) return this;
87
88         Type myType = getDeclaringType();
89         if (isSuper) {
90             myType = myType.getTypeDec().getSuperClassType();
91         }
92         constructor = myType.getConstructor(this, args, true);
93         return this;
94     }
95
96     public Type discoverType() {
97         return getTypeManager().voidType;
98     }
99
100     public void fixForIntroductions() {
101         //System.out.println("fixing: " + constructor.toShortString() );
102
Expr extraArg = null;
103         if (constructor != null) extraArg = constructor.getExtraArgExpr();
104         //System.out.println("constructor " + constructor.toShortString() + ": " + extraArg);
105
if (extraArg != null) args.add(extraArg);
106     }
107
108
109     //INTRO from ASTFixerPass
110
public ASTObject postFixAST(ASTFixerPass fixer) {
111         fixForIntroductions();
112         return super.postFixAST(fixer);
113     }
114
115     public void unparse(CodeWriter writer) {
116         if (getEnclosingInstanceExpr() != null && isSuper) {
117             writer.write(getEnclosingInstanceExpr());
118             writer.write(".");
119         }
120         if (isSuper) {
121             writer.writeKeyword("super");
122         } else {
123             writer.writeKeyword("this");
124         }
125         writer.openParen('(');
126         writer.write(args);
127         writer.closeParen(')');
128         writer.closeStmt();
129     }
130
131     // ------------------------------
132
//INTRO from InnerAccessFixer
133

134     public ASTObject postInnerAccess(InnerAccessFixer w) {
135         ConstructorDec dec = getConstructorDec();
136         if (! w.isAccessible(dec, this)) {
137             // perhaps should be creating a cookie, but won't for now.
138
dec.getModifiers().setPrivate(false);
139         }
140         return this;
141     }
142
143     // ------------------------------
144
// INTRO: LocalClassPass.AnalysisWalker
145

146     public void walkAnalysis(LocalClassPass.AnalysisWalker walker) {
147         walker.inConstructorCall();
148         walk(walker);
149     }
150
151     // ------------------------------
152
// INTRO: LocalClassPass.ThreadingWalker
153

154     public void preThreading(LocalClassPass.ThreadingWalker walker) {
155         if (getConstructorDec() == null) {
156             this.showWarning("no constructor dec bound: " + this);
157             this.getParent().getParent().display(2);
158             return;
159         }
160         walker.addArgs(getArgs(),
161                        getConstructorDec().getDeclaringType().getTypeDec());
162     }
163
164     // ------------------------------
165
// INTRO from InnerInfoPass
166

167     public void walkInnerInfo(InnerInfoPass w) {
168         int context = w.inCCall();
169         super.walkInnerInfo(w);
170         w.restoreContext(context);
171     }
172
173     public void postInnerInfo(InnerInfoPass w) {
174         if (getConstructor() == null) return;
175         NameType target = (NameType) getConstructor().getDeclaringType();
176         if (w.isInner(target) && getEnclosingInstanceExpr() == null) {
177             NameType enclosingTarget = target.getEnclosingType();
178             if (! w.isAccessible(enclosingTarget)) {
179                 showError("no enclosing instance available");
180             } else {
181                 setEnclosingInstanceExpr(getAST()
182                                          .makeEnclosingPrimary(enclosingTarget,
183                                                                w.currentType()));
184             }
185         }
186     }
187
188     //------------------------------
189
// bcg
190
protected void cgValue(CodeBuilder cb) {
191         throw new RuntimeException JavaDoc("should only be called in effect context "
192                                    + this);
193     }
194     protected void cgEffect(CodeBuilder cb) {
195         cb.enterLocation(getSourceLocation());
196         Constructor dec = getInternalConstructor();
197         NameType declaringType = (NameType) dec.getDeclaringType();
198         cb.emitALOAD(0);
199         if (getEnclosingInstanceExpr() != null) {
200             getEnclosingInstanceExpr()
201                 .cgValue(cb, dec.getEnclosingInstanceFormal().getType());
202             if (! (getEnclosingInstanceExpr() instanceof ThisExpr)) {
203                 cb.emitDUP();
204                 cb.emitINVOKEVIRTUAL(getTypeManager().getObjectType(), "getClass",
205                                      "()Ljava/lang/Class;", 0);
206                 cb.emitPOP();
207             }
208         }
209         getArgs().cgValues(cb, dec.getFormals());
210         cb.emitINVOKESPECIAL(declaringType, "<init>",
211                              dec.getDescriptor(), dec.getStackDelta());
212     }
213     public Constructor getInternalConstructor() {
214         if (constructor != null) return constructor;
215
216         Type myType = getDeclaringType(); //getContext().getEnclosingTypeDec(this);
217
if (isSuper) {
218             myType = myType.getTypeDec().getSuperClassType();
219         }
220         constructor = myType.getConstructor(this, args, true);
221         return constructor;
222     }
223
224     // ------------------------------
225
// convenience constructors
226

227     public ConstructorCallExpr(SourceLocation source, boolean _isSuper, Exprs _args) {
228         this(source, null, _isSuper, _args, null);
229     }
230
231     public ConstructorCallExpr(SourceLocation location, boolean _isSuper,
232                                Exprs _args, Constructor _constructor) {
233         this(location, null, _isSuper, _args, _constructor);
234     }
235
236     //BEGIN: Generated from @child and @property
237
protected Expr enclosingInstanceExpr;
238     public Expr getEnclosingInstanceExpr() { return enclosingInstanceExpr; }
239     public void setEnclosingInstanceExpr(Expr _enclosingInstanceExpr) {
240         if (_enclosingInstanceExpr != null) _enclosingInstanceExpr.setParent(this);
241         enclosingInstanceExpr = _enclosingInstanceExpr;
242     }
243
244     protected boolean isSuper;
245     public boolean getIsSuper() { return isSuper; }
246     public void setIsSuper(boolean _isSuper) { isSuper = _isSuper; }
247
248     protected Exprs args;
249     public Exprs getArgs() { return args; }
250     public void setArgs(Exprs _args) {
251         if (_args != null) _args.setParent(this);
252         args = _args;
253     }
254
255     protected Constructor constructor;
256     public Constructor getConstructor() { return constructor; }
257     public void setConstructor(Constructor _constructor) { constructor = _constructor; }
258
259     public ConstructorCallExpr(SourceLocation location, Expr _enclosingInstanceExpr, boolean _isSuper, Exprs _args, Constructor _constructor) {
260         super(location);
261         setEnclosingInstanceExpr(_enclosingInstanceExpr);
262         setIsSuper(_isSuper);
263         setArgs(_args);
264         setConstructor(_constructor);
265     }
266     protected ConstructorCallExpr(SourceLocation source) {
267         super(source);
268     }
269
270     public ASTObject copyWalk(CopyWalker walker) {
271         ConstructorCallExpr ret = new ConstructorCallExpr(getSourceLocation());
272         ret.preCopy(walker, this);
273         if (enclosingInstanceExpr != null) ret.setEnclosingInstanceExpr( (Expr)walker.process(enclosingInstanceExpr) );
274         ret.isSuper = isSuper;
275         if (args != null) ret.setArgs( (Exprs)walker.process(args) );
276         ret.constructor = constructor;
277         return ret;
278     }
279
280     public ASTObject getChildAt(int childIndex) {
281         switch(childIndex) {
282         case 0: return enclosingInstanceExpr;
283         case 1: return args;
284         default: return super.getChildAt(childIndex);
285         }
286     }
287      public String JavaDoc getChildNameAt(int childIndex) {
288         switch(childIndex) {
289         case 0: return "enclosingInstanceExpr";
290         case 1: return "args";
291         default: return super.getChildNameAt(childIndex);
292         }
293     }
294      public void setChildAt(int childIndex, ASTObject child) {
295         switch(childIndex) {
296         case 0: setEnclosingInstanceExpr((Expr)child); return;
297         case 1: setArgs((Exprs)child); return;
298         default: super.setChildAt(childIndex, child); return;
299         }
300     }
301      public int getChildCount() {
302         return 2;
303     }
304
305     public String JavaDoc getDefaultDisplayName() {
306         return "ConstructorCallExpr(isSuper: "+isSuper+", "+"constructor: "+constructor+")";
307     }
308
309     //END: Generated from @child and @property
310
}
311
Popular Tags