KickJava   Java API By Example, From Geeks To Geeks.

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


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.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
33 import org.aspectj.compiler.base.bcg.CodeBuilder;
34 import org.aspectj.compiler.base.bcg.Label;
35
36 /**
37   * @grammar (typeD)expr
38   * @child TypeD typeD
39   * @child Expr expr
40   */

41
42 public class CastExpr extends Expr {
43
44     public boolean canBeCopied() {
45         return expr.isUltimatelyLiteral();
46     }
47     
48     public boolean isUltimatelyLiteral() {
49         return expr.isUltimatelyLiteral();
50     }
51
52
53     public boolean isPossibleCast() {
54         if (expr.getType() == null || typeD.getType() == null) return true;
55         return expr.getType().isCoercableTo(typeD.getType());
56     }
57
58     //INTRO from MovingWalker
59
/*
60     public ASTObject postMove(MovingWalker walker) {
61         //XXX this might simplify too much
62         //XXX this is needed because some casts can become illegal when moved
63         //XXX (only illegal for Java source code, not bytecode)
64         if (walker.hasToType() && !isPossibleCast()) {
65             //XXX this should ensure that an exception is throw
66             return getAST().forceCast(getType(), getAST().makeNull());
67         }
68         return this;
69     }
70     */

71
72     public void checkSpec() {
73         Type exprType = expr.getType();
74         Type castType = typeD.getType();
75         setType(castType);
76         if (!isPossibleCast()) {
77             showError("Can't convert from " + exprType.getString()
78                       + " to " + castType.getString());
79         }
80     }
81
82     public Type discoverType() {
83         return typeD.getType();
84     }
85
86     public void unparse(CodeWriter writer) {
87         writer.write('(');
88         writer.write(typeD);
89         writer.write(')');
90
91         if (isSynthetic() && !isPossibleCast()) {
92             //XXX this should ensure that an exception is throw
93
writer.write("null");
94             return;
95         }
96
97         //!!! this should be done in a cleaner and more general way
98
if (isSynthetic() && !(expr instanceof ParenExpr)) {
99             writer.write('(');
100             writer.write(expr);
101             writer.write(')');
102         } else {
103             writer.write(expr);
104         }
105     }
106
107     // ------------------------------
108
// bcg
109
protected void cgValue(CodeBuilder cb) {
110         expr.cgValue(cb);
111         expr.getType().emitCast(cb, typeD.getType());
112     }
113
114     // ------------------------------
115
// INTRO from AssignmentCheckerPass
116

117     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
118         checkSpec();
119         if (expr instanceof LiteralExpr && ! getTypeD().getType().isObject()) {
120             return getTypeD().getType().foldCast((LiteralExpr) expr);
121         } else {
122             return this;
123         }
124     }
125
126     //BEGIN: Generated from @child and @property
127
protected TypeD typeD;
128     public TypeD getTypeD() { return typeD; }
129     public void setTypeD(TypeD _typeD) {
130         if (_typeD != null) _typeD.setParent(this);
131         typeD = _typeD;
132     }
133
134     protected Expr expr;
135     public Expr getExpr() { return expr; }
136     public void setExpr(Expr _expr) {
137         if (_expr != null) _expr.setParent(this);
138         expr = _expr;
139     }
140
141     public CastExpr(SourceLocation location, TypeD _typeD, Expr _expr) {
142         super(location);
143         setTypeD(_typeD);
144         setExpr(_expr);
145     }
146     protected CastExpr(SourceLocation source) {
147         super(source);
148     }
149
150     public ASTObject copyWalk(CopyWalker walker) {
151         CastExpr ret = new CastExpr(getSourceLocation());
152         ret.preCopy(walker, this);
153         if (typeD != null) ret.setTypeD( (TypeD)walker.process(typeD) );
154         if (expr != null) ret.setExpr( (Expr)walker.process(expr) );
155         return ret;
156     }
157
158     public ASTObject getChildAt(int childIndex) {
159         switch(childIndex) {
160         case 0: return typeD;
161         case 1: return expr;
162         default: return super.getChildAt(childIndex);
163         }
164     }
165      public String JavaDoc getChildNameAt(int childIndex) {
166         switch(childIndex) {
167         case 0: return "typeD";
168         case 1: return "expr";
169         default: return super.getChildNameAt(childIndex);
170         }
171     }
172      public void setChildAt(int childIndex, ASTObject child) {
173         switch(childIndex) {
174         case 0: setTypeD((TypeD)child); return;
175         case 1: setExpr((Expr)child); return;
176         default: super.setChildAt(childIndex, child); return;
177         }
178     }
179      public int getChildCount() {
180         return 2;
181     }
182
183     public String JavaDoc getDefaultDisplayName() {
184         return "CastExpr()";
185     }
186
187     //END: Generated from @child and @property
188
}
189
Popular Tags