Debian Codegraph @

net/sf/saxon/expr/OrExpression.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.SystemFunction;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.BooleanValue;
14
15 public class OrExpression extends BooleanExpression {
16
17
18 /**
19 * Construct a boolean OR expression
20 *
21 * @param p1 the first operand
22 * @param p2 the second operand
23 */
24
25 public OrExpression(Expression p1, Expression p2) {
26 super(p1, Token.OR, p2);
27 }
28
29 @Override
30 protected Expression preEvaluate() {
31 if (Literal.hasEffectiveBooleanValue(getLhsExpression(), true) || Literal.hasEffectiveBooleanValue(getRhsExpression(), true)) {
32 // A or true() => true()
33 // true() or B => true()
34 return Literal.makeLiteral(BooleanValue.TRUE, this);
35 } else if (Literal.hasEffectiveBooleanValue(getLhsExpression(), false)) {
36 // false() or B => B
37 return forceToBoolean(getRhsExpression());
38 } else if (Literal.hasEffectiveBooleanValue(getRhsExpression(), false)) {
39 // A or false() => A
40 return forceToBoolean(getLhsExpression());
41 } else {
42 return this;
43 }
44 }
45
46 /**
47 * Perform optimisation of an expression and its subexpressions.
48 * <p>This method is called after all references to functions and variables have been resolved
49 * to the declaration of the function or variable, and after all type checking has been done.</p>
50 *
51 * @param visitor an expression visitor
52 * @param contextItemType the static type of "." at the point where this expression is invoked.
53 * The parameter is set to null if it is known statically that the context item will be undefined.
54 * If the type of the context item is not known statically, the argument is set to
55 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
56 * @return the original expression, rewritten if appropriate to optimize execution
57 * @throws XPathException if an error is discovered during this phase
58 * (typically a type error)
59 */
60
61 /*@NotNull*/
62 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
63
64 final Expression e = super.optimize(visitor, contextItemType);
65 if (e != this) {
66 return e;
67 }
68
69 // If this is a top-level or-expression then try to replace multiple branches with a general comparison
70 if (!(getParentExpression() instanceof OrExpression)) {
71 final Expression e2 = visitor.obtainOptimizer().tryGeneralComparison(visitor, contextItemType, this);
72 if (e2 != null && e2 != this) {
73 return e2;
74 }
75 }
76
77 return this;
78 }
79
80 @Override
81 public int getCost() {
82 // Assume the RHS will be evaluated 50% of the time
83 return getLhsExpression().getCost() + getRhsExpression().getCost() / 2;
84 }
85
86 /**
87 * Copy an expression. This makes a deep copy.
88 *
89 * @return the copy of the original expression
90 * @param rebindings
91 */
92
93 /*@NotNull*/
94 public Expression copy(RebindingMap rebindings) {
95 OrExpression exp = new OrExpression(getLhsExpression().copy(rebindings), getRhsExpression().copy(rebindings));
96 ExpressionTool.copyLocationInfo(this, exp);
97 return exp;
98 }
99
100
101 /**
102 * Return the negation of this boolean expression, that is, an expression that returns true
103 * when this expression returns false, and vice versa
104 *
105 * @return the negation of this expression
106 */
107
108 public Expression negate() {
109 // Apply de Morgan's laws
110 // not(A or B) => not(A) and not(B)
111 Expression not0 = SystemFunction.makeCall("not", getRetainedStaticContext(), getLhsExpression());
112 Expression not1 = SystemFunction.makeCall("not", getRetainedStaticContext(), getRhsExpression());
113 AndExpression result = new AndExpression(not0, not1);
114 ExpressionTool.copyLocationInfo(this, result);
115 return result;
116 }
117
118 /**
119 * Get the element name used to identify this expression in exported expression format
120 *
121 * @return the element name used to identify this expression
122 */
123 @Override
124 protected String tag() {
125 return "or";
126 }
127
128 /**
129 * Evaluate as a boolean.
130 */
131
132 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
133 return getLhsExpression().effectiveBooleanValue(c) || getRhsExpression().effectiveBooleanValue(c);
134 }
135
136
137 }
138