Debian Codegraph @

net/sf/saxon/expr/IdentityComparison.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.expr.sort.GlobalOrderComparer;
12 import net.sf.saxon.om.NodeInfo;
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.UType;
17 import net.sf.saxon.value.BooleanValue;
18 import net.sf.saxon.value.Cardinality;
19 import net.sf.saxon.value.SequenceType;
20
21
22 /**
23 * IdentityComparison: a boolean expression that compares two nodes
24 * for equals, not-equals, greater-than or less-than based on identity and
25 * document ordering
26 */
27
28 public final class IdentityComparison extends BinaryExpression {
29
30 private boolean generateIdEmulation = false;
31 // this flag is set if an "X is Y" or "X isnot Y" comparison is being used
32 // to emulate generate-id(X) = / != generate-id(Y). The handling of an empty
33 // sequence in the two cases is different.
34
35 /**
36 * Create an identity comparison identifying the two operands and the operator
37 *
38 * @param p1 the left-hand operand
39 * @param op the operator, as a token returned by the Tokenizer (e.g. Token.LT)
40 * @param p2 the right-hand operand
41 */
42
43 public IdentityComparison(Expression p1, int op, Expression p2) {
44 super(p1, op, p2);
45 }
46
47 /**
48 * Set flag to indicate different empty-sequence behavior when emulating
49 * comparison of two generate-id's. This is relevant when operands evaluate
50 * to an empty sequence; if both are empty, the "is" operator returns (), while the
51 * generate-id() comparison compares two empty strings, which returns true; if one is
52 * empty, the "is" operator returns (), while the generate-id() comparison returns false.
53 *
54 * @param flag true if this function is being used to compare generate-id() output
55 */
56
57 public void setGenerateIdEmulation(boolean flag) {
58 generateIdEmulation = flag;
59 }
60
61 /**
62 * Test the flag that indicates different empty-sequence behavior when emulating
63 * comparison of two generate-id's
64 *
65 * @return true if this function is being used to compare generate-id() output
66 */
67
68 public boolean isGenerateIdEmulation() {
69 return generateIdEmulation;
70 }
71
72 /**
73 * Type-check the expression
74 */
75
76 /*@NotNull*/
77 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
78
79 getLhs().typeCheck(visitor, contextInfo);
80 getRhs().typeCheck(visitor, contextInfo);
81
82 if (!generateIdEmulation) {
83 if (Literal.isEmptySequence(getLhsExpression()) || Literal.isEmptySequence(getRhsExpression())) {
84 return Literal.makeEmptySequence();
85 }
86 }
87
88 RoleDiagnostic role0 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, Token.tokens[operator], 0);
89 TypeChecker tc = visitor.getConfiguration().getTypeChecker(false);
90 //role0.setSourceLocator(this);
91 setLhsExpression(tc.staticTypeCheck(getLhsExpression(), SequenceType.OPTIONAL_NODE, role0, visitor));
92
93 RoleDiagnostic role1 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, Token.tokens[operator], 1);
94 //role1.setSourceLocator(this);
95 setRhsExpression(tc.staticTypeCheck(getRhsExpression(), SequenceType.OPTIONAL_NODE, role1, visitor));
96
97 if (!Cardinality.allowsZero(getLhsExpression().getCardinality()) && !Cardinality.allowsZero(getRhsExpression().getCardinality())) {
98 generateIdEmulation = false;
99 // because the flag only makes a difference if one of the operands evaluates to ()
100 }
101 return this;
102 }
103
104 /**
105 * Perform optimisation of an expression and its subexpressions.
106 * <p>This method is called after all references to functions and variables have been resolved
107 * to the declaration of the function or variable, and after all type checking has been done.</p>
108 *
109 * @param visitor an expression visitor
110 * @param contextItemType the static type of "." at the point where this expression is invoked.
111 * The parameter is set to null if it is known statically that the context item will be undefined.
112 * If the type of the context item is not known statically, the argument is set to
113 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
114 * @return the original expression, rewritten if appropriate to optimize execution
115 * @throws XPathException if an error is discovered during this phase
116 * (typically a type error)
117 */
118
119 /*@NotNull*/
120 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
121 Expression r = super.optimize(visitor, contextItemType);
122 if (r != this) {
123 if (!generateIdEmulation) {
124 if (Literal.isEmptySequence(getLhsExpression()) || Literal.isEmptySequence(getRhsExpression())) {
125 return Literal.makeEmptySequence();
126 }
127 }
128 }
129 return r;
130 }
131
132 @Override
133 protected OperandRole getOperandRole(int arg) {
134 return OperandRole.INSPECT;
135 }
136
137
138 /**
139 * Copy an expression. This makes a deep copy.
140 *
141 * @return the copy of the original expression
142 * @param rebindings
143 */
144
145 /*@NotNull*/
146 public Expression copy(RebindingMap rebindings) {
147 IdentityComparison ic = new IdentityComparison(getLhsExpression().copy(rebindings), operator, getRhsExpression().copy(rebindings));
148 ExpressionTool.copyLocationInfo(this, ic);
149 ic.generateIdEmulation = generateIdEmulation;
150 return ic;
151 }
152
153 /**
154 * Get the element name used to identify this expression in exported expression format
155 *
156 * @return the element name used to identify this expression
157 */
158 @Override
159 protected String tag() {
160 switch (operator) {
161 case Token.IS: return "is";
162 case Token.PRECEDES: return "precedes";
163 case Token.FOLLOWS: return "follows";
164 default: return "?";
165 }
166 }
167
168 /**
169 * Evaluate the expression
170 */
171
172 /*@Nullable*/
173 public BooleanValue evaluateItem(XPathContext context) throws XPathException {
174 NodeInfo node0 = getNode(getLhsExpression(), context);
175 if (node0 == null) {
176 if (generateIdEmulation) {
177 return BooleanValue.get(getNode(getRhsExpression(), context) == null);
178 }
179 return null;
180 }
181
182 NodeInfo node1 = getNode(getRhsExpression(), context);
183 if (node1 == null) {
184 if (generateIdEmulation) {
185 return BooleanValue.FALSE;
186 }
187 return null;
188 }
189
190 return BooleanValue.get(compareIdentity(node0, node1));
191 }
192
193 public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
194 NodeInfo node0 = getNode(getLhsExpression(), context);
195 if (node0 == null) {
196 return generateIdEmulation && getNode(getRhsExpression(), context) == null;
197 }
198
199 NodeInfo node1 = getNode(getRhsExpression(), context);
200 return node1 != null && compareIdentity(node0, node1);
201
202 }
203
204 private boolean compareIdentity(NodeInfo node0, NodeInfo node1) {
205
206 switch (operator) {
207 case Token.IS:
208 return node0.equals(node1);
209 case Token.PRECEDES:
210 return GlobalOrderComparer.getInstance().compare(node0, node1) < 0;
211 case Token.FOLLOWS:
212 return GlobalOrderComparer.getInstance().compare(node0, node1) > 0;
213 default:
214 throw new UnsupportedOperationException("Unknown node identity test");
215 }
216 }
217
218 private static NodeInfo getNode(Expression exp, XPathContext c) throws XPathException {
219 return (NodeInfo) exp.evaluateItem(c);
220 }
221
222
223 /**
224 * Determine the data type of the expression
225 *
226 * @return Type.BOOLEAN
227 */
228
229 /*@NotNull*/
230 public ItemType getItemType() {
231 return BuiltInAtomicType.BOOLEAN;
232 }
233
234 /**
235 * Get the static type of the expression as a UType, following precisely the type
236 * inference rules defined in the XSLT 3.0 specification.
237 *
238 * @return the static item type of the expression according to the XSLT 3.0 defined rules
239 * @param contextItemType
240 */
241 @Override
242 public UType getStaticUType(UType contextItemType) {
243 return UType.BOOLEAN;
244 }
245
246 /**
247 * Get a name identifying the kind of expression, in terms meaningful to a user.
248 *
249 * @return a name identifying the kind of expression, in terms meaningful to a user.
250 * The name will always be in the form of a lexical XML QName, and should match the name used
251 * in export() output displaying the expression.
252 */
253 @Override
254 public String getExpressionName() {
255 return "nodeComparison";
256 }
257
258
259 }
260