KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > LogicalExpr


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: LogicalExpr.java,v 1.14 2004/02/16 22:24:28 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.GOTO;
23 import org.apache.bcel.generic.InstructionHandle;
24 import org.apache.bcel.generic.InstructionList;
25 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
26 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
27 import org.apache.xalan.xsltc.compiler.util.MethodType;
28 import org.apache.xalan.xsltc.compiler.util.Type;
29 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
30
31 /**
32  * @author Jacek Ambroziak
33  * @author Santiago Pericas-Geertsen
34  * @author Morten Jorgensen
35  */

36 final class LogicalExpr extends Expression {
37
38     public static final int OR = 0;
39     public static final int AND = 1;
40     
41     private final int _op; // operator
42
private Expression _left; // first operand
43
private Expression _right; // second operand
44

45     private static final String JavaDoc[] Ops = { "or", "and" };
46
47     /**
48      * Creates a new logical expression - either OR or AND. Note that the
49      * left- and right-hand side expressions can also be logical expressions,
50      * thus creating logical trees representing structures such as
51      * (a and (b or c) and d), etc...
52      */

53     public LogicalExpr(int op, Expression left, Expression right) {
54     _op = op;
55     (_left = left).setParent(this);
56     (_right = right).setParent(this);
57     }
58
59     /**
60      * Returns true if this expressions contains a call to position(). This is
61      * needed for context changes in node steps containing multiple predicates.
62      */

63     public boolean hasPositionCall() {
64     return (_left.hasPositionCall() || _right.hasPositionCall());
65     }
66
67     /**
68      * Returns true if this expressions contains a call to last()
69      */

70     public boolean hasLastCall() {
71             return (_left.hasLastCall() || _right.hasLastCall());
72     }
73     
74     /**
75      * Returns an object representing the compile-time evaluation
76      * of an expression. We are only using this for function-available
77      * and element-available at this time.
78      */

79     public Object JavaDoc evaluateAtCompileTime() {
80     final Object JavaDoc leftb = _left.evaluateAtCompileTime();
81     final Object JavaDoc rightb = _right.evaluateAtCompileTime();
82
83     // Return null if we can't evaluate at compile time
84
if (leftb == null || rightb == null) {
85         return null;
86     }
87
88     if (_op == AND) {
89         return (leftb == Boolean.TRUE && rightb == Boolean.TRUE) ?
90         Boolean.TRUE : Boolean.FALSE;
91     }
92     else {
93         return (leftb == Boolean.TRUE || rightb == Boolean.TRUE) ?
94         Boolean.TRUE : Boolean.FALSE;
95     }
96     }
97
98     /**
99      * Returns this logical expression's operator - OR or AND represented
100      * by 0 and 1 respectively.
101      */

102     public int getOp() {
103     return(_op);
104     }
105
106     /**
107      * Override the SyntaxTreeNode.setParser() method to make sure that the
108      * parser is set for sub-expressions
109      */

110     public void setParser(Parser parser) {
111     super.setParser(parser);
112     _left.setParser(parser);
113     _right.setParser(parser);
114     }
115
116     /**
117      * Returns a string describing this expression
118      */

119     public String JavaDoc toString() {
120     return Ops[_op] + '(' + _left + ", " + _right + ')';
121     }
122
123     /**
124      * Type-check this expression, and possibly child expressions.
125      */

126     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
127     // Get the left and right operand types
128
Type tleft = _left.typeCheck(stable);
129     Type tright = _right.typeCheck(stable);
130
131     // Check if the operator supports the two operand types
132
MethodType wantType = new MethodType(Type.Void, tleft, tright);
133     MethodType haveType = lookupPrimop(stable, Ops[_op], wantType);
134
135     // Yes, the operation is supported
136
if (haveType != null) {
137         // Check if left-hand side operand must be type casted
138
Type arg1 = (Type)haveType.argsType().elementAt(0);
139         if (!arg1.identicalTo(tleft))
140         _left = new CastExpr(_left, arg1);
141         // Check if right-hand side operand must be type casted
142
Type arg2 = (Type) haveType.argsType().elementAt(1);
143         if (!arg2.identicalTo(tright))
144         _right = new CastExpr(_right, arg1);
145         // Return the result type for the operator we will use
146
return _type = haveType.resultType();
147     }
148     throw new TypeCheckError(this);
149     }
150
151     /**
152      * Compile the expression - leave boolean expression on stack
153      */

154     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
155     translateDesynthesized(classGen, methodGen);
156     synthesize(classGen, methodGen);
157     }
158
159     /**
160      * Compile expression and update true/false-lists
161      */

162     public void translateDesynthesized(ClassGenerator classGen,
163                        MethodGenerator methodGen) {
164
165     final InstructionList il = methodGen.getInstructionList();
166     final SyntaxTreeNode parent = getParent();
167
168     // Compile AND-expression
169
if (_op == AND) {
170
171         // Translate left hand side - must be true
172
_left.translateDesynthesized(classGen, methodGen);
173
174         // Need this for chaining any OR-expression children
175
InstructionHandle middle = il.append(NOP);
176
177         // Translate left right side - must be true
178
_right.translateDesynthesized(classGen, methodGen);
179
180         // Need this for chaining any OR-expression children
181
InstructionHandle after = il.append(NOP);
182
183         // Append child expression false-lists to our false-list
184
_falseList.append(_right._falseList.append(_left._falseList));
185
186         // Special case for OR-expression as a left child of AND.
187
// The true-list of OR must point to second clause of AND.
188
if ((_left instanceof LogicalExpr) &&
189         (((LogicalExpr)_left).getOp() == OR)) {
190         _left.backPatchTrueList(middle);
191         }
192         else if (_left instanceof NotCall) {
193         _left.backPatchTrueList(middle);
194         }
195         else {
196         _trueList.append(_left._trueList);
197         }
198
199         // Special case for OR-expression as a right child of AND
200
// The true-list of OR must point to true-list of AND.
201
if ((_right instanceof LogicalExpr) &&
202         (((LogicalExpr)_right).getOp() == OR)) {
203         _right.backPatchTrueList(after);
204         }
205         else if (_right instanceof NotCall) {
206         _right.backPatchTrueList(after);
207         }
208         else {
209         _trueList.append(_right._trueList);
210         }
211     }
212     // Compile OR-expression
213
else {
214         // Translate left-hand side expression and produce true/false list
215
_left.translateDesynthesized(classGen, methodGen);
216
217         // This GOTO is used to skip over the code for the last test
218
// in the case where the the first test succeeds
219
InstructionHandle ih = il.append(new GOTO(null));
220
221         // Translate right-hand side expression and produce true/false list
222
_right.translateDesynthesized(classGen, methodGen);
223
224         _left._trueList.backPatch(ih);
225         _left._falseList.backPatch(ih.getNext());
226             
227         _falseList.append(_right._falseList);
228         _trueList.add(ih).append(_right._trueList);
229     }
230     }
231 }
232
Popular Tags