KickJava   Java API By Example, From Geeks To Geeks.

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


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.BlockScope;
29
30 import org.aspectj.compiler.base.bcg.CodeBuilder;
31 import org.aspectj.compiler.base.bcg.Label;
32
33 /**
34   * @grammar modifiers typeD id initializer
35   * @child Modifiers modifiers
36   * @child TypeD typeD
37   * @property String id
38   * @child Expr initializer
39   *
40   */

41
42 public class VarDec extends Dec {
43     public String JavaDoc getKind() { return "var"; }
44
45     public void checkSpec() {
46         if (typeD.getType().isVoid()) {
47             typeD.showError("void type not allowed here");
48         }
49     }
50
51     public boolean isBlank() {
52         return (getInitializer() == null);
53     }
54
55     // ------------------------------
56
// INTRO from FlowCheckerPass
57

58     public void walkFlow(FlowCheckerPass w) {
59         w.setVars(w.getVars().addUnassigned(this));
60         // some duplicated code from AssignExpr
61
if (getInitializer() != null) {
62             w.process(getInitializer());
63             FlowCheckerPass.Vars v = w.getVars();
64             if (isFinal() && ! v.isDefinitelyUnassigned(this)) {
65                 w.showVarError(this, this,
66                                "Final variable " + getId() +
67                                " may already be assigned");
68             }
69             w.setVars(v.addAssigned(this));
70         }
71     }
72
73     // ------------------------------
74
// INTRO from AssignmentCheckerPass
75

76     public void preAssignmentCheck(AssignmentCheckerPass walker) {
77         Expr rhs = getInitializer();
78         if (rhs != null && rhs instanceof ArrayInitializer) {
79             ArrayInitializer ai = (ArrayInitializer) rhs;
80             Type ty = getTypeD().getType();
81             if (! (ty instanceof ArrayType)) {
82                 this.showError("Not of array type");
83                 setInitializer(null);
84             } else {
85                 ArrayType arrayType = (ArrayType) ty;
86                 setInitializer(getAST().makeNewArray(arrayType, ai));
87             }
88         }
89     }
90
91     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
92         this.checkAssignmentType();
93         return this;
94     }
95
96     protected void checkAssignmentType() {
97         Type ty = getType();
98         Expr init = getInitializer();
99         if (init != null && ! init.isAssignableTo(ty)) {
100             showTypeError(init.getType(), ty);
101         }
102     }
103
104     //INTRO from ScopeWalker
105
public void preScope(ScopeWalker walker) {
106         walker.addVarDec(this);
107     }
108
109     //XXX not doing this is a great way to check parents
110
public boolean isIntroduced() {
111         if (isField()) return super.isIntroduced();
112         else return false;
113     }
114
115     public boolean isField() {
116         return false;
117     }
118
119     public boolean inStaticContext() { return getParent().inStaticContext(); }
120
121     public Type getType() {
122         return getTypeD().getType();
123     }
124
125     public Expr getExpr() {
126         return null; //new VarExpr(id);
127
}
128
129
130     public String JavaDoc toShortString() { return getTypeD().toShortString() + " " + getId(); }
131
132     public void unparse(CodeWriter writer, boolean isStmt, boolean showType) {
133         if (showType) {
134             writeModifiers(writer);
135             writer.write(typeD);
136             writer.requiredSpace();
137             writer.write(getBytecodeId());
138         } else {
139             writer.write(getBytecodeId());
140         }
141
142         if (!writer.isOnlySignatures() && initializer != null) {
143             writer.writeOp("=");
144             if (initializer instanceof Exprs) writer.write("{ ");
145             writer.write(initializer);
146             if (initializer instanceof Exprs) writer.write(" }");
147         }
148         if (!writer.isOnlySignatures() && isStmt) writer.closeStmt();
149
150     }
151
152     public void unparse(CodeWriter writer) {
153         unparse(writer, true, true);
154     }
155
156     // ------------------------------
157
// Intro: FrameLocPass
158

159     public void walkFrameLoc(FrameLocPass walker) {
160         setFrameLoc(walker.allocate(getType().getSlotCount()));
161     }
162
163     // ------------------------------
164
// bcg
165

166     public String JavaDoc getDescriptor() {
167         return getType().getDescriptor();
168     }
169     protected void cgStmt(CodeBuilder cb) {
170         cb.enterVar(this);
171         if (initializer != null) {
172             Type ty = getType();
173             initializer.cgValue(cb, ty);
174             ty.emitStore(cb, frameLoc);
175         }
176     }
177     private int frameLoc;
178     public int getFrameLoc() { return frameLoc; }
179     void setFrameLoc(int frameLoc) { this.frameLoc = frameLoc; }
180
181     // ------------------------------
182
// convenience constructors
183

184     public VarDec(SourceLocation location, TypeD typeD, String JavaDoc id, Expr initializer) {
185         this(location,new Modifiers(location,0), typeD, id, initializer);
186     }
187
188
189     //BEGIN: Generated from @child and @property
190
protected Modifiers modifiers;
191     public Modifiers getModifiers() { return modifiers; }
192     public void setModifiers(Modifiers _modifiers) {
193         if (_modifiers != null) _modifiers.setParent(this);
194         modifiers = _modifiers;
195     }
196
197     protected TypeD typeD;
198     public TypeD getTypeD() { return typeD; }
199     public void setTypeD(TypeD _typeD) {
200         if (_typeD != null) _typeD.setParent(this);
201         typeD = _typeD;
202     }
203
204     protected String JavaDoc id;
205     public String JavaDoc getId() { return id; }
206     public void setId(String JavaDoc _id) { id = _id; }
207
208     protected Expr initializer;
209     public Expr getInitializer() { return initializer; }
210     public void setInitializer(Expr _initializer) {
211         if (_initializer != null) _initializer.setParent(this);
212         initializer = _initializer;
213     }
214
215     public VarDec(SourceLocation location, Modifiers _modifiers, TypeD _typeD, String JavaDoc _id, Expr _initializer) {
216         super(location);
217         setModifiers(_modifiers);
218         setTypeD(_typeD);
219         setId(_id);
220         setInitializer(_initializer);
221     }
222     protected VarDec(SourceLocation source) {
223         super(source);
224     }
225
226     public ASTObject copyWalk(CopyWalker walker) {
227         VarDec ret = new VarDec(getSourceLocation());
228         ret.preCopy(walker, this);
229         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
230         if (typeD != null) ret.setTypeD( (TypeD)walker.process(typeD) );
231         ret.id = id;
232         if (initializer != null) ret.setInitializer( (Expr)walker.process(initializer) );
233         return ret;
234     }
235
236     public ASTObject getChildAt(int childIndex) {
237         switch(childIndex) {
238         case 0: return modifiers;
239         case 1: return typeD;
240         case 2: return initializer;
241         default: return super.getChildAt(childIndex);
242         }
243     }
244      public String JavaDoc getChildNameAt(int childIndex) {
245         switch(childIndex) {
246         case 0: return "modifiers";
247         case 1: return "typeD";
248         case 2: return "initializer";
249         default: return super.getChildNameAt(childIndex);
250         }
251     }
252      public void setChildAt(int childIndex, ASTObject child) {
253         switch(childIndex) {
254         case 0: setModifiers((Modifiers)child); return;
255         case 1: setTypeD((TypeD)child); return;
256         case 2: setInitializer((Expr)child); return;
257         default: super.setChildAt(childIndex, child); return;
258         }
259     }
260      public int getChildCount() {
261         return 3;
262     }
263
264     public String JavaDoc getDefaultDisplayName() {
265         return "VarDec(id: "+id+")";
266     }
267
268     //END: Generated from @child and @property
269
}
270
Popular Tags