*/
public Expression optimize(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException {
final Expression e = super.optimize(visitor, contextItemType);
final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
if (e != this) {
return e;
}
Optimizer opt = visitor.getConfiguration().getOptimizer();
operand0 = ExpressionTool.unsortedIfHomogeneous(opt, operand0);
operand1 = ExpressionTool.unsortedIfHomogeneous(opt, operand1);
// If the value can be determined from knowledge of one operand, precompute the result
if (operator == Token.AND && (
Literal.isConstantBoolean(operand0, false) || Literal.isConstantBoolean(operand1, false))) {
return new Literal(BooleanValue.FALSE);
}
if (operator == Token.OR && (
Literal.isConstantBoolean(operand0, true) || Literal.isConstantBoolean(operand1, true))) {
return new Literal(BooleanValue.TRUE);
}
// Rewrite (A and B) as (if (A) then B else false()). The benefit of this is that when B is a recursive
// function call, it is treated as a tail call (test qxmp290). To avoid disrupting other optimizations
// of "and" expressions (specifically, where clauses in FLWOR expressions), do this ONLY if B is a user
// function call (we can't tell if it's recursive), and it's not in a loop.
if (e == this && operator == Token.AND &&
operand1 instanceof UserFunctionCall &&
th.isSubType(operand1.getItemType(th), BuiltInAtomicType.BOOLEAN) &&
!visitor.isLoopingSubexpression(null)) {
Expression cond = Choose.makeConditional(operand0, operand1, Literal.makeLiteral(BooleanValue.FALSE));
ExpressionTool.copyLocationInfo(this, cond);
return cond;
}