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 org.aspectj.compiler.base.cst.*; 32 import org.aspectj.compiler.base.bcg.CodeBuilder; 33 34 import java.util.*; 35 36 37 43 public abstract class UnopExpr extends Expr { 44 45 protected void bindNamesSelf() { 46 } 48 49 public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) { 50 return tryToFold(); 51 } 52 53 private ASTObject tryToFold() { 54 if (getLiftType().isAnyType()) return this; 55 if (rand1 instanceof LiteralExpr) { 56 return halfFold(getLiftType(), (LiteralExpr)rand1).setSource(this); 57 } else { 58 return this; 59 } 60 } 61 62 abstract protected Type getLiftType(); 63 64 66 abstract protected LiteralExpr halfFold(Type type, LiteralExpr lit); 67 68 public static UnopExpr build(SourceLocation source, String op, Expr rand1) { 69 if (op.equals("+")) { 70 return new PlusOpExpr(source, op, rand1); 71 } else if (op.equals("-")) { 72 return new MinusOpExpr(source, op, rand1); 73 } else if (op.equals("~")) { 74 return new BitNotOpExpr(source, op, rand1); 75 } else if (op.equals("!")) { 76 return new LogNotOpExpr(source, op, rand1); 77 } else { 78 throw new RuntimeException ("strange unary op: " + op); 79 } 80 } 81 82 protected Type discoverType() { 83 return getRand1().getType(); 85 } 86 87 public void unparse(CodeWriter writer) { 88 writer.write(getOp()); 89 writer.write(getRand1()); 90 } 91 92 public void showOperatorTypeError(Type a) { 93 if (a.isAnyType()) return; 94 showError("operator " + op + " cannot be applied to " + a.getString()); 95 } 96 97 100 protected void cgEffect(CodeBuilder cb) { 101 rand1.cgEffect(cb); 102 } 103 104 protected String op; 106 public String getOp() { return op; } 107 public void setOp(String _op) { op = _op; } 108 109 protected Expr rand1; 110 public Expr getRand1() { return rand1; } 111 public void setRand1(Expr _rand1) { 112 if (_rand1 != null) _rand1.setParent(this); 113 rand1 = _rand1; 114 } 115 116 public UnopExpr(SourceLocation location, String _op, Expr _rand1) { 117 super(location); 118 setOp(_op); 119 setRand1(_rand1); 120 } 121 protected UnopExpr(SourceLocation source) { 122 super(source); 123 } 124 125 public ASTObject getChildAt(int childIndex) { 126 switch(childIndex) { 127 case 0: return rand1; 128 default: return super.getChildAt(childIndex); 129 } 130 } 131 public String getChildNameAt(int childIndex) { 132 switch(childIndex) { 133 case 0: return "rand1"; 134 default: return super.getChildNameAt(childIndex); 135 } 136 } 137 public void setChildAt(int childIndex, ASTObject child) { 138 switch(childIndex) { 139 case 0: setRand1((Expr)child); return; 140 default: super.setChildAt(childIndex, child); return; 141 } 142 } 143 public int getChildCount() { 144 return 1; 145 } 146 147 public String getDefaultDisplayName() { 148 return "UnopExpr(op: "+op+")"; 149 } 150 151 } 153 | Popular Tags |