View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: LogicalExpr.java,v 1.2.4.1 2005/09/01 16:03:31 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.GOTO;
27  import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
28  import com.sun.org.apache.bcel.internal.generic.InstructionList;
29  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
30  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
33  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
34  
35  /**
36   * @author Jacek Ambroziak
37   * @author Santiago Pericas-Geertsen
38   * @author Morten Jorgensen
39   */
40  final class LogicalExpr extends Expression {
41  
42      public static final int OR  = 0;
43      public static final int AND = 1;
44  
45      private final int  _op;     // operator
46      private Expression _left;   // first operand
47      private Expression _right;  // second operand
48  
49      private static final String[] Ops = { "or", "and" };
50  
51      /**
52       * Creates a new logical expression - either OR or AND. Note that the
53       * left- and right-hand side expressions can also be logical expressions,
54       * thus creating logical trees representing structures such as
55       * (a and (b or c) and d), etc...
56       */
57      public LogicalExpr(int op, Expression left, Expression right) {
58          _op = op;
59          (_left = left).setParent(this);
60          (_right = right).setParent(this);
61      }
62  
63      /**
64       * Returns true if this expressions contains a call to position(). This is
65       * needed for context changes in node steps containing multiple predicates.
66       */
67      public boolean hasPositionCall() {
68          return (_left.hasPositionCall() || _right.hasPositionCall());
69      }
70  
71      /**
72       * Returns true if this expressions contains a call to last()
73       */
74      public boolean hasLastCall() {
75              return (_left.hasLastCall() || _right.hasLastCall());
76      }
77  
78      /**
79       * Returns an object representing the compile-time evaluation
80       * of an expression. We are only using this for function-available
81       * and element-available at this time.
82       */
83      public Object evaluateAtCompileTime() {
84          final Object leftb = _left.evaluateAtCompileTime();
85          final Object rightb = _right.evaluateAtCompileTime();
86  
87          // Return null if we can't evaluate at compile time
88          if (leftb == null || rightb == null) {
89              return null;
90          }
91  
92          if (_op == AND) {
93              return (leftb == Boolean.TRUE && rightb == Boolean.TRUE) ?
94                  Boolean.TRUE : Boolean.FALSE;
95          }
96          else {
97              return (leftb == Boolean.TRUE || rightb == Boolean.TRUE) ?
98                  Boolean.TRUE : Boolean.FALSE;
99          }
100     }
101 
102     /**
103      * Returns this logical expression's operator - OR or AND represented
104      * by 0 and 1 respectively.
105      */
106     public int getOp() {
107         return(_op);
108     }
109 
110     /**
111      * Override the SyntaxTreeNode.setParser() method to make sure that the
112      * parser is set for sub-expressions
113      */
114     public void setParser(Parser parser) {
115         super.setParser(parser);
116         _left.setParser(parser);
117         _right.setParser(parser);
118     }
119 
120     /**
121      * Returns a string describing this expression
122      */
123     public String toString() {
124         return Ops[_op] + '(' + _left + ", " + _right + ')';
125     }
126 
127     /**
128      * Type-check this expression, and possibly child expressions.
129      */
130     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
131         // Get the left and right operand types
132         Type tleft = _left.typeCheck(stable);
133         Type tright = _right.typeCheck(stable);
134 
135         // Check if the operator supports the two operand types
136         MethodType wantType = new MethodType(Type.Void, tleft, tright);
137         MethodType haveType = lookupPrimop(stable, Ops[_op], wantType);
138 
139         // Yes, the operation is supported
140         if (haveType != null) {
141             // Check if left-hand side operand must be type casted
142             Type arg1 = (Type)haveType.argsType().elementAt(0);
143             if (!arg1.identicalTo(tleft))
144                 _left = new CastExpr(_left, arg1);
145             // Check if right-hand side operand must be type casted
146             Type arg2 = (Type) haveType.argsType().elementAt(1);
147             if (!arg2.identicalTo(tright))
148                 _right = new CastExpr(_right, arg1);
149             // Return the result type for the operator we will use
150             return _type = haveType.resultType();
151         }
152         throw new TypeCheckError(this);
153     }
154 
155     /**
156      * Compile the expression - leave boolean expression on stack
157      */
158     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
159         translateDesynthesized(classGen, methodGen);
160         synthesize(classGen, methodGen);
161     }
162 
163     /**
164      * Compile expression and update true/false-lists
165      */
166     public void translateDesynthesized(ClassGenerator classGen,
167                                        MethodGenerator methodGen) {
168 
169         final InstructionList il = methodGen.getInstructionList();
170         final SyntaxTreeNode parent = getParent();
171 
172         // Compile AND-expression
173         if (_op == AND) {
174 
175             // Translate left hand side - must be true
176             _left.translateDesynthesized(classGen, methodGen);
177 
178             // Need this for chaining any OR-expression children
179             InstructionHandle middle = il.append(NOP);
180 
181             // Translate left right side - must be true
182             _right.translateDesynthesized(classGen, methodGen);
183 
184             // Need this for chaining any OR-expression children
185             InstructionHandle after = il.append(NOP);
186 
187             // Append child expression false-lists to our false-list
188             _falseList.append(_right._falseList.append(_left._falseList));
189 
190             // Special case for OR-expression as a left child of AND.
191             // The true-list of OR must point to second clause of AND.
192             if ((_left instanceof LogicalExpr) &&
193                 (((LogicalExpr)_left).getOp() == OR)) {
194                 _left.backPatchTrueList(middle);
195             }
196             else if (_left instanceof NotCall) {
197                 _left.backPatchTrueList(middle);
198             }
199             else {
200                 _trueList.append(_left._trueList);
201             }
202 
203             // Special case for OR-expression as a right child of AND
204             // The true-list of OR must point to true-list of AND.
205             if ((_right instanceof LogicalExpr) &&
206                 (((LogicalExpr)_right).getOp() == OR)) {
207                 _right.backPatchTrueList(after);
208             }
209             else if (_right instanceof NotCall) {
210                 _right.backPatchTrueList(after);
211             }
212             else {
213                 _trueList.append(_right._trueList);
214             }
215         }
216         // Compile OR-expression
217         else {
218             // Translate left-hand side expression and produce true/false list
219             _left.translateDesynthesized(classGen, methodGen);
220 
221             // This GOTO is used to skip over the code for the last test
222             // in the case where the the first test succeeds
223             InstructionHandle ih = il.append(new GOTO(null));
224 
225             // Translate right-hand side expression and produce true/false list
226             _right.translateDesynthesized(classGen, methodGen);
227 
228             _left._trueList.backPatch(ih);
229             _left._falseList.backPatch(ih.getNext());
230 
231             _falseList.append(_right._falseList);
232             _trueList.add(ih).append(_right._trueList);
233         }
234     }
235 }