1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.FlowCheckerPass; 28 import org.aspectj.compiler.base.AssignmentCheckerPass; 29 import org.aspectj.compiler.base.JavaCompiler; 30 import org.aspectj.compiler.base.CodeWriter; 31 import java.io.IOException ; 32 33 import java.util.*; 34 35 import org.aspectj.compiler.base.bcg.CodeBuilder; 36 import org.aspectj.compiler.base.bcg.Label; 37 38 43 44 47 public class NewArrayExpr extends NewExpr { 53 54 public Type discoverType() { 55 Exprs dimExprs = getDimExprs(); 57 ArrayInitializer inits = getInitializer(); 58 ArrayType type = (ArrayType)getTypeD().getType(); 59 60 if (initializer == null) { 61 final int dimExprCount = dimExprs.size(); 63 for (int i = 0; i < dimExprCount; i++) { 64 Expr dimExpr = dimExprs.get(i); 65 if (dimExpr instanceof EmptyExpr) break; 66 if (! dimExpr.isAssignableTo(getTypeManager().intType)) { 67 dimExpr.showTypeError(dimExpr.getType(), getTypeManager().intType); 68 } 69 } 70 } 71 return type; 72 } 73 74 public Exprs getArgs() { return new Exprs(getSourceLocation()); } 75 public CodeDec getCodeDec() { return null; } public Type getCalledType() { 77 return getTypeD().getType(); 78 } 79 80 81 public void unparse(CodeWriter writer) throws IOException { 82 ArrayType arrayType = (ArrayType)getTypeD().getType(); 83 Exprs exprs = getDimExprs(); 84 85 writer.writeKeyword("new"); 86 writer.requiredSpace(); 87 if (exprs == null) { 88 writer.write(arrayType.getString()); 89 writer.optionalSpace(); 90 writer.write(initializer); 91 } else { 92 Type componentType = arrayType.getComponentType(); 93 while (componentType instanceof ArrayType) { 94 componentType = ((ArrayType)componentType).getComponentType(); 95 } 96 writer.write(componentType.getString()); 97 98 for(int i=0; i<exprs.size(); i++) { 99 writer.write('['); writer.write(exprs.get(i)); writer.write(']'); 100 } 101 } 102 } 103 104 107 public void preAssignmentCheck(AssignmentCheckerPass walker) { 108 if (getInitializer() != null) { 109 Type componentType = ((ArrayType)getType()).getComponentType(); 110 if (componentType instanceof ArrayType) { 111 ArrayType arrayComponentType = (ArrayType) componentType; 112 Exprs exprs = initializer.getExprs(); 113 for (int i = exprs.size() - 1; i >= 0; i--) { 114 Expr expr = exprs.get(i); 115 if (expr instanceof ArrayInitializer) { 116 exprs.set(i, 117 getAST().makeNewArray(arrayComponentType, 118 (ArrayInitializer)expr)); 119 } 120 } 121 } 122 } 123 } 124 125 public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) { 126 if (getInitializer() != null) { 127 Type componentType = ((ArrayType)getType()).getComponentType(); 128 Exprs exprs = initializer.getExprs(); 129 for (int i = exprs.size() - 1; i >= 0; i--) { 130 Expr expr = exprs.get(i); 131 if (! expr.isAssignableTo(componentType)) { 132 expr.showTypeError(expr.getType(), componentType); 133 } 134 } 135 } 136 return this; 137 } 138 139 142 143 146 private void typeCheck(Type ty, Expr e) { 147 if (e instanceof ArrayInitializer) { 148 if (! (ty instanceof ArrayType)) { 150 showError("illegal initializer for " + ty.getString()); 151 } else { 152 Type componentTy = ((ArrayType)ty).getComponentType(); 153 Exprs exprs = ((ArrayInitializer)e).getExprs(); 154 if (exprs != null) { 156 int len = exprs.size(); 157 for (int i = 0; i < len; i++) { 158 Expr arrayInit = exprs.get(i); 159 typeCheck(componentTy, arrayInit); 160 } 161 } 162 } 163 } else { 164 } 165 } 166 167 protected void cgValue(CodeBuilder cb) { 170 ArrayType type = (ArrayType)getTypeD().getType(); 171 int dims = type.getArrayDimCount(); 172 Type compTy = type.getComponentType(); 173 174 if (initializer == null) { 175 int filledDims = 0; 177 for (int i = 0; i < dims; i++) { 178 Expr e = dimExprs.get(i); 179 if (e instanceof EmptyExpr) break; 180 e.cgValue(cb); 181 filledDims++; 182 } 183 if (dims == 1) { 184 compTy.emitNewarray(cb); 185 } else { 186 type.emitMultiNewarray(cb, filledDims); 187 } 188 } 189 else { 190 Exprs es = initializer.getExprs(); 193 int size = es.size(); 194 cb.emitIntConstant(size); 195 compTy.emitNewarray(cb); 196 for (int i = 0; i < size; i++) { 197 Expr e = es.get(i); 198 if (e.isConstantZero()) continue; 201 cb.emitDUP(); cb.emitIntConstant(i); 203 e.cgValue(cb, compTy); 204 compTy.emitAstore(cb); 205 } 206 } 207 } 208 209 protected ArrayInitializer initializer; 211 public ArrayInitializer getInitializer() { return initializer; } 212 public void setInitializer(ArrayInitializer _initializer) { 213 if (_initializer != null) _initializer.setParent(this); 214 initializer = _initializer; 215 } 216 217 protected Exprs dimExprs; 218 public Exprs getDimExprs() { return dimExprs; } 219 public void setDimExprs(Exprs _dimExprs) { 220 if (_dimExprs != null) _dimExprs.setParent(this); 221 dimExprs = _dimExprs; 222 } 223 224 public NewArrayExpr(SourceLocation location, TypeD _typeD, ArrayInitializer _initializer, Exprs _dimExprs) { 225 super(location, _typeD); 226 setInitializer(_initializer); 227 setDimExprs(_dimExprs); 228 } 229 protected NewArrayExpr(SourceLocation source) { 230 super(source); 231 } 232 233 public ASTObject copyWalk(CopyWalker walker) { 234 NewArrayExpr ret = new NewArrayExpr(getSourceLocation()); 235 ret.preCopy(walker, this); 236 if (typeD != null) ret.setTypeD( (TypeD)walker.process(typeD) ); 237 if (initializer != null) ret.setInitializer( (ArrayInitializer)walker.process(initializer) ); 238 if (dimExprs != null) ret.setDimExprs( (Exprs)walker.process(dimExprs) ); 239 return ret; 240 } 241 242 public ASTObject getChildAt(int childIndex) { 243 switch(childIndex) { 244 case 1: return initializer; 245 case 2: return dimExprs; 246 default: return super.getChildAt(childIndex); 247 } 248 } 249 public String getChildNameAt(int childIndex) { 250 switch(childIndex) { 251 case 1: return "initializer"; 252 case 2: return "dimExprs"; 253 default: return super.getChildNameAt(childIndex); 254 } 255 } 256 public void setChildAt(int childIndex, ASTObject child) { 257 switch(childIndex) { 258 case 1: setInitializer((ArrayInitializer)child); return; 259 case 2: setDimExprs((Exprs)child); return; 260 default: super.setChildAt(childIndex, child); return; 261 } 262 } 263 public int getChildCount() { 264 return 3; 265 } 266 267 public String getDefaultDisplayName() { 268 return "NewArrayExpr()"; 269 } 270 271 } 273 | Popular Tags |