1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.base.JavaCompiler; 29 import org.aspectj.compiler.base.CodeWriter; 30 import java.io.IOException ; 31 32 import org.aspectj.compiler.base.bcg.CodeBuilder; 33 import org.aspectj.compiler.base.bcg.Label; 34 35 39 40 public class ReturnStmt extends Stmt { 41 42 public void checkSpec() { 43 CodeDec codeDec = getEnclosingCodeDec(); 48 if (codeDec instanceof InitializerDec) { 50 showError("A return statement may not appear in an initializer block"); 51 return; 52 } 53 Type resultTy = codeDec.getResultType(); 54 Expr expr = getExpr(); 55 if (expr == null) { 56 if (! resultTy.isEquivalent(getTypeManager().voidType)) { 57 showError("missing return value"); 58 } 59 } else if (resultTy instanceof VoidType) { 60 showError("cannot return a value from method whose result type is void"); 61 } 62 } 64 65 68 public void walkFlow(FlowCheckerPass w) { 69 if (getExpr() != null) { 70 w.process(getExpr()); 71 } 72 w.doReturn(); 73 w.setLive(false); 74 w.setVars(w.getAllVars()); 75 } 76 77 80 public void walkCleanup(ByteCodeCleanupPass w) { 81 w.setLive(false); 82 } 83 84 87 public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) { 88 Expr expr = getExpr(); 89 if (expr == null) return this; 90 CodeDec codeDec = getEnclosingCodeDec(); 91 Type resultTy = codeDec.getResultType(); 92 if (! expr.isAssignableTo(resultTy)) { 94 expr.showTypeError(expr.getType(), resultTy); 95 } else if (resultTy.isPrimitive() && resultTy != expr.getType()) { 96 setExpr((Expr)getAST().forceCast(resultTy, expr).setSource(this)); 97 } else { 98 setExpr((Expr)getAST().makeCast(resultTy, expr).setSource(this)); 99 } 100 return this; 101 } 102 103 public void unparse(CodeWriter writer) throws IOException { 104 writer.writeKeyword("return"); 105 106 if (expr != null) { 107 writer.requiredSpace(); 108 writer.write(expr); 109 } 110 111 writer.closeStmt(); 112 } 113 114 122 123 126 protected void cgStmt(CodeBuilder cb) { 127 if (expr != null) { 128 expr.cgValue(cb); 130 cb.doReturn(expr.getType()); 131 } else { 132 cb.doReturn(getTypeManager().voidType); 133 } 134 } 135 136 protected Expr expr; 138 public Expr getExpr() { return expr; } 139 public void setExpr(Expr _expr) { 140 if (_expr != null) _expr.setParent(this); 141 expr = _expr; 142 } 143 144 public ReturnStmt(SourceLocation location, Expr _expr) { 145 super(location); 146 setExpr(_expr); 147 } 148 protected ReturnStmt(SourceLocation source) { 149 super(source); 150 } 151 152 public ASTObject copyWalk(CopyWalker walker) { 153 ReturnStmt ret = new ReturnStmt(getSourceLocation()); 154 ret.preCopy(walker, this); 155 if (expr != null) ret.setExpr( (Expr)walker.process(expr) ); 156 return ret; 157 } 158 159 public ASTObject getChildAt(int childIndex) { 160 switch(childIndex) { 161 case 0: return expr; 162 default: return super.getChildAt(childIndex); 163 } 164 } 165 public String getChildNameAt(int childIndex) { 166 switch(childIndex) { 167 case 0: return "expr"; 168 default: return super.getChildNameAt(childIndex); 169 } 170 } 171 public void setChildAt(int childIndex, ASTObject child) { 172 switch(childIndex) { 173 case 0: setExpr((Expr)child); return; 174 default: super.setChildAt(childIndex, child); return; 175 } 176 } 177 public int getChildCount() { 178 return 1; 179 } 180 181 public String getDefaultDisplayName() { 182 return "ReturnStmt()"; 183 } 184 185 } 187 188 189 | Popular Tags |