KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 expr instanceof typeD
37  * @child Expr expr
38  * @child TypeD testTypeD
39  */

40
41 public class InstanceofExpr extends Expr {
42
43     public Type discoverType() {
44         return getTypeManager().booleanType;
45     }
46
47     public void unparse(CodeWriter writer) throws IOException JavaDoc {
48         writer.write(expr);
49         writer.write(" instanceof ");
50         writer.write(testTypeD);
51     }
52
53     public boolean isAlwaysFalse() {
54         return !expr.getType().isCoercableTo(testTypeD.getType());
55     }
56
57     public void checkSpec() {
58         if (!testTypeD.getType().isReferenceType()) {
59             testTypeD.showError("not a reference type");
60         }
61
62         if (isAlwaysFalse()) {
63             showError("Impossible for " + expr.getType().getString()
64                       + " to be instanceof " + testTypeD.getType().getString());
65         }
66     }
67
68     //INTRO from MovingWalker
69
public ASTObject postMove(MovingWalker walker) {
70         //XXX this might simplify too much
71
//XXX this is needed because some instanceof tests can become illegal when moved
72
//XXX (only illegal for Java source code, not bytecode)
73
return simplify();
74     }
75
76     // special simplify method...
77
public Expr simplify() {
78         //System.out.println("simplifying " + this.unparse());
79
if (expr.getType() == null) return this;
80
81         if (testTypeD.getType().isAssignableFrom(expr.getType())) {
82             return getAST().makeLiteral(true);
83         } else if (isAlwaysFalse()) {
84             return getAST().makeLiteral(false);
85         } else {
86             return this;
87         }
88     }
89
90     // ------------------------------
91
// bcg
92
protected void cgValue(CodeBuilder cb) {
93         expr.cgValue(cb);
94         testTypeD.getType().emitInstanceof(cb);
95     }
96
97     //BEGIN: Generated from @child and @property
98
protected Expr expr;
99     public Expr getExpr() { return expr; }
100     public void setExpr(Expr _expr) {
101         if (_expr != null) _expr.setParent(this);
102         expr = _expr;
103     }
104
105     protected TypeD testTypeD;
106     public TypeD getTestTypeD() { return testTypeD; }
107     public void setTestTypeD(TypeD _testTypeD) {
108         if (_testTypeD != null) _testTypeD.setParent(this);
109         testTypeD = _testTypeD;
110     }
111
112     public InstanceofExpr(SourceLocation location, Expr _expr, TypeD _testTypeD) {
113         super(location);
114         setExpr(_expr);
115         setTestTypeD(_testTypeD);
116     }
117     protected InstanceofExpr(SourceLocation source) {
118         super(source);
119     }
120
121     public ASTObject copyWalk(CopyWalker walker) {
122         InstanceofExpr ret = new InstanceofExpr(getSourceLocation());
123         ret.preCopy(walker, this);
124         if (expr != null) ret.setExpr( (Expr)walker.process(expr) );
125         if (testTypeD != null) ret.setTestTypeD( (TypeD)walker.process(testTypeD) );
126         return ret;
127     }
128
129     public ASTObject getChildAt(int childIndex) {
130         switch(childIndex) {
131         case 0: return expr;
132         case 1: return testTypeD;
133         default: return super.getChildAt(childIndex);
134         }
135     }
136      public String JavaDoc getChildNameAt(int childIndex) {
137         switch(childIndex) {
138         case 0: return "expr";
139         case 1: return "testTypeD";
140         default: return super.getChildNameAt(childIndex);
141         }
142     }
143      public void setChildAt(int childIndex, ASTObject child) {
144         switch(childIndex) {
145         case 0: setExpr((Expr)child); return;
146         case 1: setTestTypeD((TypeD)child); return;
147         default: super.setChildAt(childIndex, child); return;
148         }
149     }
150      public int getChildCount() {
151         return 2;
152     }
153
154     public String JavaDoc getDefaultDisplayName() {
155         return "InstanceofExpr()";
156     }
157
158     //END: Generated from @child and @property
159
}
160
Popular Tags