Debian Codegraph @

net/sf/saxon/expr/BooleanExpression.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.parser.*;
11 import net.sf.saxon.functions.BooleanFn;
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.ItemType;
16 import net.sf.saxon.type.TypeHierarchy;
17 import net.sf.saxon.type.UType;
18 import net.sf.saxon.value.BooleanValue;
19
20 import java.util.List;
21
22
23 /**
24 * Boolean expression: two truth values combined using AND or OR.
25 */
26
27 public abstract class BooleanExpression extends BinaryExpression implements Negatable {
28
29 /**
30 * Construct a boolean expression
31 *
32 * @param p1 the first operand
33 * @param operator one of {@link Token#AND} or {@link Token#OR}
34 * @param p2 the second operand
35 */
36
37 public BooleanExpression(Expression p1, int operator, Expression p2) {
38 super(p1, operator, p2);
39 }
40
41
42 /**
43 * Get a name identifying the kind of expression, in terms meaningful to a user.
44 *
45 * @return a name identifying the kind of expression, in terms meaningful to a user.
46 * The name will always be in the form of a lexical XML QName, and should match the name used
47 * in explain() output displaying the expression.
48 */
49 @Override
50 public String getExpressionName() {
51 return Token.tokens[getOperator()] + "-expression";
52 }
53
54 /*@NotNull*/
55 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
56
57 getLhs().typeCheck(visitor, contextInfo);
58 getRhs().typeCheck(visitor, contextInfo);
59
60 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
61 XPathException err0 = TypeChecker.ebvError(getLhsExpression(), th);
62 if (err0 != null) {
63 err0.setLocator(getLocation());
64 throw err0;
65 }
66 XPathException err1 = TypeChecker.ebvError(getRhsExpression(), th);
67 if (err1 != null) {
68 err1.setLocator(getLocation());
69 throw err1;
70 }
71 // Precompute the EBV of any constant operand
72 if (getLhsExpression() instanceof Literal && !(((Literal) getLhsExpression()).getValue() instanceof BooleanValue)) {
73 setLhsExpression(Literal.makeLiteral(
74 BooleanValue.get(getLhsExpression().effectiveBooleanValue(visitor.makeDynamicContext())), this));
75 }
76 if (getRhsExpression() instanceof Literal && !(((Literal) getRhsExpression()).getValue() instanceof BooleanValue)) {
77 setRhsExpression(Literal.makeLiteral(
78 BooleanValue.get(getRhsExpression().effectiveBooleanValue(visitor.makeDynamicContext())), this));
79 }
80 return preEvaluate();
81 }
82
83 /**
84 * Determine the static cardinality. Returns [1..1]
85 */
86
87 public int computeCardinality() {
88 return StaticProperty.EXACTLY_ONE;
89 }
90
91 /**
92 * Perform optimisation of an expression and its subexpressions.
93 * <p>This method is called after all references to functions and variables have been resolved
94 * to the declaration of the function or variable, and after all type checking has been done.</p>
95 *
96 * @param visitor an expression visitor
97 * @param contextItemType the static type of "." at the point where this expression is invoked.
98 * The parameter is set to null if it is known statically that the context item will be undefined.
99 * If the type of the context item is not known statically, the argument is set to
100 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
101 * @return the original expression, rewritten if appropriate to optimize execution
102 * @throws XPathException if an error is discovered during this phase
103 * (typically a type error)
104 */
105
106 /*@NotNull*/
107 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
108
109 optimizeChildren(visitor, contextItemType);
110
111 boolean forStreaming = visitor.isOptimizeForStreaming();
112 setLhsExpression(ExpressionTool.unsortedIfHomogeneous(getLhsExpression(), forStreaming));
113 setRhsExpression(ExpressionTool.unsortedIfHomogeneous(getRhsExpression(), forStreaming));
114
115 Expression op0 = BooleanFn.rewriteEffectiveBooleanValue(getLhsExpression(), visitor, contextItemType);
116 if (op0 != null) {
117 setLhsExpression(op0);
118 }
119 Expression op1 = BooleanFn.rewriteEffectiveBooleanValue(getRhsExpression(), visitor, contextItemType);
120 if (op1 != null) {
121 setRhsExpression(op1);
122 }
123 return preEvaluate();
124 }
125
126 /**
127 * Evaluate the expression statically if either or both operands are literals. For example,
128 * (true() or X) returns true().
129 * @return a boolean literal if the expression can be evaluated now, or the original expression
130 * otherwise.
131 */
132
133 protected abstract Expression preEvaluate();
134
135 protected Expression forceToBoolean(Expression in) {
136 if (in.getItemType() == BuiltInAtomicType.BOOLEAN && in.getCardinality() == StaticProperty.ALLOWS_ONE) {
137 return in;
138 } else {
139 return SystemFunction.makeCall("boolean", getRetainedStaticContext(), in);
140 }
141 }
142
143 /**
144 * Check whether this specific instance of the expression is negatable
145 *
146 * @return true if it is
147 * @param th the type hierarchy, if needed
148 */
149
150 public boolean isNegatable(TypeHierarchy th) {
151 return true;
152 }
153
154 /**
155 * Return the negation of this boolean expression, that is, an expression that returns true
156 * when this expression returns false, and vice versa
157 *
158 * @return the negation of this expression
159 */
160
161 public abstract Expression negate();
162
163 /**
164 * Evaluate the expression
165 */
166
167 public BooleanValue evaluateItem(XPathContext context) throws XPathException {
168 return BooleanValue.get(effectiveBooleanValue(context));
169 }
170
171 /**
172 * Evaluate as a boolean.
173 */
174
175 public abstract boolean effectiveBooleanValue(XPathContext c) throws XPathException;
176
177 /**
178 * Determine the data type of the expression
179 *
180 * @return BuiltInAtomicType.BOOLEAN
181 */
182
183 /*@NotNull*/
184 public ItemType getItemType() {
185 return BuiltInAtomicType.BOOLEAN;
186 }
187
188 /**
189 * Get the static type of the expression as a UType, following precisely the type
190 * inference rules defined in the XSLT 3.0 specification.
191 *
192 * @return the static item type of the expression according to the XSLT 3.0 defined rules
193 * @param contextItemType
194 */
195 @Override
196 public UType getStaticUType(UType contextItemType) {
197 return UType.BOOLEAN;
198 }
199
200 /**
201 * Construct a list containing the "anded" subexpressions of an expression:
202 * if the expression is (A and B and C), this returns (A, B, C).
203 *
204 * @param exp the expression to be decomposed
205 * @param list the list to which the subexpressions are to be added.
206 */
207
208 public static void listAndComponents(Expression exp, List<Expression> list) {
209 if (exp instanceof BooleanExpression && ((BooleanExpression) exp).getOperator() == Token.AND) {
210 for (Operand o : exp.operands()) {
211 listAndComponents(o.getChildExpression(), list);
212 }
213 } else {
214 list.add(exp);
215 }
216 }
217
218 @Override
219 protected OperandRole getOperandRole(int arg) {
220 return OperandRole.INSPECT;
221 }
222
223
224 }
225