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. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.GOTO;
  21. import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  22. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  28. /**
  29. * @author Jacek Ambroziak
  30. * @author Santiago Pericas-Geertsen
  31. * @author Morten Jorgensen
  32. */
  33. final class LogicalExpr extends Expression {
  34. public static final int OR = 0;
  35. public static final int AND = 1;
  36. private final int _op; // operator
  37. private Expression _left; // first operand
  38. private Expression _right; // second operand
  39. private static final String[] Ops = { "or", "and" };
  40. /**
  41. * Creates a new logical expression - either OR or AND. Note that the
  42. * left- and right-hand side expressions can also be logical expressions,
  43. * thus creating logical trees representing structures such as
  44. * (a and (b or c) and d), etc...
  45. */
  46. public LogicalExpr(int op, Expression left, Expression right) {
  47. _op = op;
  48. (_left = left).setParent(this);
  49. (_right = right).setParent(this);
  50. }
  51. /**
  52. * Returns true if this expressions contains a call to position(). This is
  53. * needed for context changes in node steps containing multiple predicates.
  54. */
  55. public boolean hasPositionCall() {
  56. return (_left.hasPositionCall() || _right.hasPositionCall());
  57. }
  58. /**
  59. * Returns true if this expressions contains a call to last()
  60. */
  61. public boolean hasLastCall() {
  62. return (_left.hasLastCall() || _right.hasLastCall());
  63. }
  64. /**
  65. * Returns an object representing the compile-time evaluation
  66. * of an expression. We are only using this for function-available
  67. * and element-available at this time.
  68. */
  69. public Object evaluateAtCompileTime() {
  70. final Object leftb = _left.evaluateAtCompileTime();
  71. final Object rightb = _right.evaluateAtCompileTime();
  72. // Return null if we can't evaluate at compile time
  73. if (leftb == null || rightb == null) {
  74. return null;
  75. }
  76. if (_op == AND) {
  77. return (leftb == Boolean.TRUE && rightb == Boolean.TRUE) ?
  78. Boolean.TRUE : Boolean.FALSE;
  79. }
  80. else {
  81. return (leftb == Boolean.TRUE || rightb == Boolean.TRUE) ?
  82. Boolean.TRUE : Boolean.FALSE;
  83. }
  84. }
  85. /**
  86. * Returns this logical expression's operator - OR or AND represented
  87. * by 0 and 1 respectively.
  88. */
  89. public int getOp() {
  90. return(_op);
  91. }
  92. /**
  93. * Override the SyntaxTreeNode.setParser() method to make sure that the
  94. * parser is set for sub-expressions
  95. */
  96. public void setParser(Parser parser) {
  97. super.setParser(parser);
  98. _left.setParser(parser);
  99. _right.setParser(parser);
  100. }
  101. /**
  102. * Returns a string describing this expression
  103. */
  104. public String toString() {
  105. return Ops[_op] + '(' + _left + ", " + _right + ')';
  106. }
  107. /**
  108. * Type-check this expression, and possibly child expressions.
  109. */
  110. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  111. // Get the left and right operand types
  112. Type tleft = _left.typeCheck(stable);
  113. Type tright = _right.typeCheck(stable);
  114. // Check if the operator supports the two operand types
  115. MethodType wantType = new MethodType(Type.Void, tleft, tright);
  116. MethodType haveType = lookupPrimop(stable, Ops[_op], wantType);
  117. // Yes, the operation is supported
  118. if (haveType != null) {
  119. // Check if left-hand side operand must be type casted
  120. Type arg1 = (Type)haveType.argsType().elementAt(0);
  121. if (!arg1.identicalTo(tleft))
  122. _left = new CastExpr(_left, arg1);
  123. // Check if right-hand side operand must be type casted
  124. Type arg2 = (Type) haveType.argsType().elementAt(1);
  125. if (!arg2.identicalTo(tright))
  126. _right = new CastExpr(_right, arg1);
  127. // Return the result type for the operator we will use
  128. return _type = haveType.resultType();
  129. }
  130. throw new TypeCheckError(this);
  131. }
  132. /**
  133. * Compile the expression - leave boolean expression on stack
  134. */
  135. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  136. translateDesynthesized(classGen, methodGen);
  137. synthesize(classGen, methodGen);
  138. }
  139. /**
  140. * Compile expression and update true/false-lists
  141. */
  142. public void translateDesynthesized(ClassGenerator classGen,
  143. MethodGenerator methodGen) {
  144. final InstructionList il = methodGen.getInstructionList();
  145. final SyntaxTreeNode parent = getParent();
  146. // Compile AND-expression
  147. if (_op == AND) {
  148. // Translate left hand side - must be true
  149. _left.translateDesynthesized(classGen, methodGen);
  150. // Need this for chaining any OR-expression children
  151. InstructionHandle middle = il.append(NOP);
  152. // Translate left right side - must be true
  153. _right.translateDesynthesized(classGen, methodGen);
  154. // Need this for chaining any OR-expression children
  155. InstructionHandle after = il.append(NOP);
  156. // Append child expression false-lists to our false-list
  157. _falseList.append(_right._falseList.append(_left._falseList));
  158. // Special case for OR-expression as a left child of AND.
  159. // The true-list of OR must point to second clause of AND.
  160. if ((_left instanceof LogicalExpr) &&
  161. (((LogicalExpr)_left).getOp() == OR)) {
  162. _left.backPatchTrueList(middle);
  163. }
  164. else if (_left instanceof NotCall) {
  165. _left.backPatchTrueList(middle);
  166. }
  167. else {
  168. _trueList.append(_left._trueList);
  169. }
  170. // Special case for OR-expression as a right child of AND
  171. // The true-list of OR must point to true-list of AND.
  172. if ((_right instanceof LogicalExpr) &&
  173. (((LogicalExpr)_right).getOp() == OR)) {
  174. _right.backPatchTrueList(after);
  175. }
  176. else if (_right instanceof NotCall) {
  177. _right.backPatchTrueList(after);
  178. }
  179. else {
  180. _trueList.append(_right._trueList);
  181. }
  182. }
  183. // Compile OR-expression
  184. else {
  185. // Translate left-hand side expression and produce true/false list
  186. _left.translateDesynthesized(classGen, methodGen);
  187. // This GOTO is used to skip over the code for the last test
  188. // in the case where the the first test succeeds
  189. InstructionHandle ih = il.append(new GOTO(null));
  190. // Translate right-hand side expression and produce true/false list
  191. _right.translateDesynthesized(classGen, methodGen);
  192. _left._trueList.backPatch(ih);
  193. _left._falseList.backPatch(ih.getNext());
  194. _falseList.append(_right._falseList);
  195. _trueList.add(ih).append(_right._trueList);
  196. }
  197. }
  198. }