Debian Codegraph @

net/sf/saxon/expr/AndExpression.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr;
9
10 import net.sf.saxon.expr.instruct.Choose;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.functions.SystemFunction;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.BuiltInAtomicType;
15 import net.sf.saxon.type.TypeHierarchy;
16 import net.sf.saxon.value.BooleanValue;
17
18 import java.util.Collection;
19
20 public class AndExpression extends BooleanExpression {
21
22 /**
23 * Construct a boolean AND expression
24 *
25 * @param p1 the first operand
26 * @param p2 the second operand
27 */
28
29 public AndExpression(Expression p1, Expression p2) {
30 super(p1, Token.AND, p2);
31 }
32
33 @Override
34 protected Expression preEvaluate() {
35 // If the value can be determined from knowledge of one operand, precompute the result
36
37 if (Literal.isConstantBoolean(getLhsExpression(), false) || Literal.isConstantBoolean(getRhsExpression(), false)) {
38 // A and false() => false()
39 // false() and B => false()
40 return Literal.makeLiteral(BooleanValue.FALSE, this);
41 } else if (Literal.hasEffectiveBooleanValue(getLhsExpression(), true)) {
42 // true() and B => B
43 return forceToBoolean(getRhsExpression());
44 } else if (Literal.hasEffectiveBooleanValue(getRhsExpression(), true)) {
45 // A and true() => A
46 return forceToBoolean(getLhsExpression());
47 } else {
48 return this;
49 }
50
51 }
52
53 /**
54 * Perform optimisation of an expression and its subexpressions.
55 * <p>This method is called after all references to functions and variables have been resolved
56 * to the declaration of the function or variable, and after all type checking has been done.</p>
57 *
58 * @param visitor an expression visitor
59 * @param contextInfo the static type of "." at the point where this expression is invoked.
60 * The parameter is set to null if it is known statically that the context item will be undefined.
61 * If the type of the context item is not known statically, the argument is set to
62 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
63 * @return the original expression, rewritten if appropriate to optimize execution
64 * @throws XPathException if an error is discovered during this phase
65 * (typically a type error)
66 */
67
68 /*@NotNull*/
69 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
70
71 Expression t = super.optimize(visitor, contextInfo);
72 if (t != this) {
73 return t;
74 }
75
76 // Rewrite (A and B) as (if (A) then B else false()). The benefit of this is that when B is a recursive
77 // function call, it is treated as a tail call (test qxmp290). To avoid disrupting other optimizations
78 // of "and" expressions (specifically, where clauses in FLWOR expressions), do this ONLY if B is a user
79 // function call (we can't tell if it's recursive), and it's not in a loop.
80
81 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
82 if ( getRhsExpression() instanceof UserFunctionCall &&
83 th.isSubType(getRhsExpression().getItemType(), BuiltInAtomicType.BOOLEAN) &&
84 !ExpressionTool.isLoopingSubexpression(this, null)) {
85 Expression cond = Choose.makeConditional(
86 getLhsExpression(), getRhsExpression(), Literal.makeLiteral(BooleanValue.FALSE, this));
87 ExpressionTool.copyLocationInfo(this, cond);
88 return cond;
89 }
90 return this;
91 }
92
93 @Override
94 public int getCost() {
95 // Assume the RHS will be evaluated 50% of the time
96 return getLhsExpression().getCost() + getRhsExpression().getCost() / 2;
97 }
98
99 /**
100 * Copy an expression. This makes a deep copy.
101 *
102 * @return the copy of the original expression
103 * @param rebindings
104 */
105
106 /*@NotNull*/
107 public Expression copy(RebindingMap rebindings) {
108 AndExpression a2 = new AndExpression(getLhsExpression().copy(rebindings), getRhsExpression().copy(rebindings));
109 ExpressionTool.copyLocationInfo(this, a2);
110 return a2;
111 }
112
113
114 /**
115 * Return the negation of this boolean expression, that is, an expression that returns true
116 * when this expression returns false, and vice versa
117 *
118 * @return the negation of this expression
119 */
120
121 public Expression negate() {
122 // Apply de Morgan's laws
123 // not(A and B) ==> not(A) or not(B)
124 Expression not0 = SystemFunction.makeCall("not", getRetainedStaticContext(), getLhsExpression());
125 Expression not1 = SystemFunction.makeCall("not", getRetainedStaticContext(), getRhsExpression());
126 return new OrExpression(not0, not1);
127
128 }
129
130 /**
131 * Get the element name used to identify this expression in exported expression format
132 *
133 * @return the element name used to identify this expression
134 */
135 @Override
136 protected String tag() {
137 return "and";
138 }
139
140 /**
141 * Evaluate as a boolean.
142 */
143
144 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
145 return getLhsExpression().effectiveBooleanValue(c) && getRhsExpression().effectiveBooleanValue(c);
146 }
147
148 /**
149 * Generate an 'and' tree over a set of expressions
150 * @param exprs the expressions to be "and'ed" together
151 * @return the root of the new expression tree
152 */
153 public static Expression distribute(Collection<Expression> exprs) {
154 Expression result = null;
155 if(exprs != null) {
156 boolean first = true;
157 for(Expression e: exprs) {
158 if(first){
159 first = false;
160 result = e;
161 } else {
162 result = new AndExpression(result,e);
163 }
164 }
165 }
166 return result;
167 }
168 }
169