KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > ReturnStmt


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

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 JavaDoc;
31
32 import org.aspectj.compiler.base.bcg.CodeBuilder;
33 import org.aspectj.compiler.base.bcg.Label;
34
35 /**
36  * @grammar return (expr)?;
37  * @child Expr expr
38  */

39
40 public class ReturnStmt extends Stmt {
41
42     public void checkSpec() {
43         //if (getOptions().bcg) {
44
// need to find out why this doesn't work in general. tests/new/AdviceOnPrivileged.java
45
//this.getParent().display(0);
46
//System.err.println("" + this + this.hashCode());
47
CodeDec codeDec = getEnclosingCodeDec();
48             //System.err.println("done");
49
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         //}
63
}
64
65     // ------------------------------
66
// INTRO from FlowCheckerPass
67

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     // ------------------------------
78
// INTRO from ByteCodeCleanupPass
79

80     public void walkCleanup(ByteCodeCleanupPass w) {
81         w.setLive(false);
82     }
83
84     // ------------------------------
85
// INTRO from AssignmentCheckerPass
86

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         //System.out.println(">>>" + codeDec + ", " + resultTy);
93
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 JavaDoc {
104         writer.writeKeyword("return");
105
106         if (expr != null) {
107             writer.requiredSpace();
108             writer.write(expr);
109         }
110
111         writer.closeStmt();
112     }
113
114     /*
115     public void parseStmt(Parser parser) {
116         if (parser.peekToken(SEMICOLON)) {
117             expr = parser.parseExpr();
118         }
119         parser.eatTopToken(SEMICOLON);
120     }
121     */

122
123     // ------------------------------
124
// bcg
125

126     protected void cgStmt(CodeBuilder cb) {
127         if (expr != null) {
128             // assumes we've already cast the return expr to the right type.
129
expr.cgValue(cb);
130             cb.doReturn(expr.getType());
131         } else {
132             cb.doReturn(getTypeManager().voidType);
133         }
134     }
135
136     //BEGIN: Generated from @child and @property
137
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 JavaDoc 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 JavaDoc getDefaultDisplayName() {
182         return "ReturnStmt()";
183     }
184
185     //END: Generated from @child and @property
186
}
187
188
189
Popular Tags