1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.AssignmentCheckerPass; 28 import org.aspectj.compiler.base.*; 29 import org.aspectj.compiler.crosscuts.AccessFixer; 30 31 import java.util.*; 32 33 import org.aspectj.compiler.base.bcg.CodeBuilder; 34 import org.aspectj.compiler.base.bcg.Label; 35 36 42 public abstract class AssignExpr extends BangExpr { 43 44 public ASTObject fixAccessToFieldSet(FieldAccessExpr expr) { 46 return makeInPlaceSet(expr, getOp(), getRhs()); 47 } 48 49 public void unparse(CodeWriter writer) { 50 writer.write(lhs); 51 writer.writeOp(op + "="); 52 writer.write(rhs); 53 } 54 55 public Expr makeReference() { 56 if (lhs instanceof VarExpr) return lhs.makeReference(); 57 return super.makeReference(); 58 } 59 60 63 public void walkFlow(FlowCheckerPass w) { 65 w.process(getLhs()); 66 w.process(getRhs()); 67 if (getLhs() instanceof VarExpr) { 68 VarExpr lhs = (VarExpr)getLhs(); 69 VarDec dec = lhs.getVarDec(); 70 if (dec.isFinal() 71 && ((! dec.isBlank()) 72 || (! w.getVars().isDefinitelyUnassigned(dec)))) { 73 w.showVarError(this, dec, "Final variable " + dec.getId() + 74 " already has a value"); 75 } 76 } else if (getLhs() instanceof FieldAccessExpr) { 77 FieldAccessExpr lhs = (FieldAccessExpr) getLhs(); 78 FieldDec dec = lhs.getFieldDec(); 79 if (dec.isFinal() 80 && ((! dec.isBlank()) 81 || (! w.getVars().isDefinitelyUnassigned(dec)))) { 82 w.showVarError(this, dec, "Final field " + dec.getId() + 83 " already has a value"); 84 } 85 } 86 } 87 88 91 public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) { 92 Expr lhs = getLhs(); 94 Type lhsType = lhs.getType(); 95 Type rhsType = rhs.getType(); 96 97 return this; 98 } 99 100 103 public ASTObject postInnerAccess(InnerAccessFixer w) { 104 if (! (getLhs() instanceof FieldAccessExpr)) return this; 105 106 FieldAccessExpr lhs = (FieldAccessExpr) getLhs(); 107 Expr q = lhs.getExpr(); 108 FieldDec dec = lhs.getFieldDec(); 109 if (w.isAccessible(dec, q)) return this; 110 111 final AST ast = getAST(); 112 Type qType = q.getType(); 113 Expr rhs = getRhs(); 114 MethodDec newMethodDec = w.getAccessMethod(qType, dec, getOp(), this); 115 Exprs newArgs = ast.makeExprs(rhs); 116 Expr newExpr = w.makeOutsidePrimary(dec.isStatic(), newArgs, q); 117 return ast.makeCall(newMethodDec, newExpr, newArgs); 118 } 119 120 public MethodDec buildAccessMethod(InnerAccessFixer w) { 122 final AST ast = getAST(); 123 FieldAccessExpr lhs = (FieldAccessExpr) getLhs(); 124 Expr q = lhs.getExpr(); 125 Type qType = q.getType(); 126 FieldDec dec = lhs.getFieldDec(); 127 Type fieldType = dec.getType(); 128 129 FormalDec valFormal = ast.makeFormal(fieldType, "val$"); 130 Formals newFormals = ast.makeFormals(valFormal); 131 Expr newExpr = w.makeInsidePrimary(dec.isStatic(), newFormals, qType); 132 133 return 134 w.makeAccessMethod(fieldType, 135 newFormals, 136 ast.makeSet(ast.makeGet(newExpr, dec), 137 getOp(), 138 ast.makeVar(valFormal))); 139 } 140 141 144 public static AssignExpr build(SourceLocation source, 145 AssignableExpr rand1, String op, Expr rand2) { 146 op = op.intern(); 147 if (op == "") { 148 return new BasicAssignExpr(source, rand1, op, rand2); 149 } else if (op == "+") { 150 return new AddAssignExpr(source, rand1, op, rand2); 151 } else if (op == "*" || op == "/" || op == "%" || op == "-") { 152 return new NumericAssignExpr(source, rand1, op, rand2); 153 } else if (op == "&" || op == "|" || op == "^") { 154 return new BitwiseAssignExpr(source, rand1, op, rand2); 155 } else if (op == "<<" || op == ">>" || op == ">>>") { 156 return new ShiftAssignExpr(source, rand1, op, rand2); 157 } else { 158 throw new RuntimeException ("bad op " + op); 159 } 160 } 161 162 protected String op; 164 public String getOp() { return op; } 165 public void setOp(String _op) { op = _op; } 166 167 protected Expr rhs; 168 public Expr getRhs() { return rhs; } 169 public void setRhs(Expr _rhs) { 170 if (_rhs != null) _rhs.setParent(this); 171 rhs = _rhs; 172 } 173 174 public AssignExpr(SourceLocation location, AssignableExpr _lhs, String _op, Expr _rhs) { 175 super(location, _lhs); 176 setOp(_op); 177 setRhs(_rhs); 178 } 179 protected AssignExpr(SourceLocation source) { 180 super(source); 181 } 182 183 public ASTObject getChildAt(int childIndex) { 184 switch(childIndex) { 185 case 1: return rhs; 186 default: return super.getChildAt(childIndex); 187 } 188 } 189 public String getChildNameAt(int childIndex) { 190 switch(childIndex) { 191 case 1: return "rhs"; 192 default: return super.getChildNameAt(childIndex); 193 } 194 } 195 public void setChildAt(int childIndex, ASTObject child) { 196 switch(childIndex) { 197 case 1: setRhs((Expr)child); return; 198 default: super.setChildAt(childIndex, child); return; 199 } 200 } 201 public int getChildCount() { 202 return 2; 203 } 204 205 public String getDefaultDisplayName() { 206 return "AssignExpr(op: "+op+")"; 207 } 208 209 } 211 | Popular Tags |