1 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 41 42 public class VarDec extends Dec { 43 public String 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 58 public void walkFlow(FlowCheckerPass w) { 59 w.setVars(w.getVars().addUnassigned(this)); 60 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 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 public void preScope(ScopeWalker walker) { 106 walker.addVarDec(this); 107 } 108 109 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; } 128 129 130 public String 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 159 public void walkFrameLoc(FrameLocPass walker) { 160 setFrameLoc(walker.allocate(getType().getSlotCount())); 161 } 162 163 166 public String 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 184 public VarDec(SourceLocation location, TypeD typeD, String id, Expr initializer) { 185 this(location,new Modifiers(location,0), typeD, id, initializer); 186 } 187 188 189 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 id; 205 public String getId() { return id; } 206 public void setId(String _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 _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 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 getDefaultDisplayName() { 265 return "VarDec(id: "+id+")"; 266 } 267 268 } 270 | Popular Tags |