Debian Codegraph @

net/sf/saxon/expr/RangeExpression.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.om.GroundedValue;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trace.ExpressionPresenter;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.BuiltInAtomicType;
17 import net.sf.saxon.type.ItemType;
18 import net.sf.saxon.type.UType;
19 import net.sf.saxon.value.*;
20
21
22 /**
23 * A RangeExpression is an expression that represents an integer sequence as
24 * a pair of end-points (for example "x to y").
25 * If the end-points are equal, the sequence is of length one.
26 * <p>From Saxon 7.8, the sequence must be ascending; if the end-point is less
27 * than the start-point, an empty sequence is returned. This is to allow
28 * expressions of the form "for $i in 1 to count($seq) return ...." </p>
29 */
30
31 public class RangeExpression extends BinaryExpression {
32
33 /**
34 * Construct a RangeExpression
35 * @param start expression that computes the start of the range
36 * @param end expression that computes the end of the range
37 */
38
39 public RangeExpression(Expression start, Expression end) {
40 super(start, Token.TO, end);
41 }
42
43 /**
44 * Type-check the expression
45 */
46
47 /*@NotNull*/
48 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
49 getLhs().typeCheck(visitor, contextInfo);
50 getRhs().typeCheck(visitor, contextInfo);
51
52 boolean backCompat = visitor.getStaticContext().isInBackwardsCompatibleMode();
53 TypeChecker tc = visitor.getConfiguration().getTypeChecker(backCompat);
54 RoleDiagnostic role0 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, "to", 0);
55 setLhsExpression(tc.staticTypeCheck(
56 getLhsExpression(), SequenceType.OPTIONAL_INTEGER, role0, visitor));
57
58 RoleDiagnostic role1 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, "to", 1);
59 setRhsExpression(tc.staticTypeCheck(
60 getRhsExpression(), SequenceType.OPTIONAL_INTEGER, role1, visitor));
61
62 return makeConstantRange();
63 }
64
65 /**
66 * Perform optimisation of an expression and its subexpressions.
67 * <p>This method is called after all references to functions and variables have been resolved
68 * to the declaration of the function or variable, and after all type checking has been done.</p>
69 *
70 * @param visitor an expression visitor
71 * @param contextInfo the static type of "." at the point where this expression is invoked.
72 * The parameter is set to null if it is known statically that the context item will be undefined.
73 * If the type of the context item is not known statically, the argument is set to
74 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
75 * @return the original expression, rewritten if appropriate to optimize execution
76 * @throws XPathException if an error is discovered during this phase
77 * (typically a type error)
78 */
79
80 /*@NotNull*/
81 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
82 getLhs().optimize(visitor, contextInfo);
83 getRhs().optimize(visitor, contextInfo);
84 return makeConstantRange();
85
86 }
87
88 private Expression makeConstantRange() throws XPathException {
89 if (getLhsExpression() instanceof Literal && getRhsExpression() instanceof Literal) {
90 GroundedValue v0 = ((Literal) getLhsExpression()).getValue();
91 GroundedValue v1 = ((Literal) getRhsExpression()).getValue();
92 if (v0 instanceof Int64Value && v1 instanceof Int64Value) {
93 long i0 = ((Int64Value) v0).longValue();
94 long i1 = ((Int64Value) v1).longValue();
95 Literal result;
96 if (i0 > i1) {
97 result = Literal.makeEmptySequence();
98 } else if (i0 == i1) {
99 result = Literal.makeLiteral(Int64Value.makeIntegerValue(i0), this);
100 } else {
101 if (i1 - i0 > Integer.MAX_VALUE) {
102 throw new XPathException("Maximum length of sequence in Saxon is " + Integer.MAX_VALUE, "XPDY0130");
103 }
104 result = Literal.makeLiteral(new IntegerRange(i0, i1), this);
105 }
106 ExpressionTool.copyLocationInfo(this, result);
107 return result;
108 }
109 }
110 return this;
111 }
112
113
114 /**
115 * Get the data type of the items returned
116 */
117
118 /*@NotNull*/
119 public ItemType getItemType() {
120 return BuiltInAtomicType.INTEGER;
121 }
122
123 /**
124 * Get the static type of the expression as a UType, following precisely the type
125 * inference rules defined in the XSLT 3.0 specification.
126 *
127 * @return the static item type of the expression according to the XSLT 3.0 defined rules
128 * @param contextItemType
129 */
130 @Override
131 public UType getStaticUType(UType contextItemType) {
132 return UType.DECIMAL;
133 }
134
135 /**
136 * Determine the static cardinality
137 */
138
139 public int computeCardinality() {
140 return StaticProperty.ALLOWS_ZERO_OR_MORE;
141 }
142
143 /**
144 * For an expression that returns an integer or a sequence of integers, get
145 * a lower and upper bound on the values of the integers that may be returned, from
146 * static analysis. The default implementation returns null, meaning "unknown" or
147 * "not applicable". Other implementations return an array of two IntegerValue objects,
148 * representing the lower and upper bounds respectively. The values
149 * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that
150 * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH
151 * are used to indicate values limited by the size of a string or the size of a sequence.
152 *
153 * @return the lower and upper bounds of integer values in the result, or null to indicate
154 * unknown or not applicable.
155 */
156 /*@Nullable*/
157 @Override
158 public IntegerValue[] getIntegerBounds() {
159 IntegerValue[] start = getLhsExpression().getIntegerBounds();
160 IntegerValue[] end = getLhsExpression().getIntegerBounds();
161 if (start == null || end == null) {
162 return null;
163 } else {
164 // range is from the smallest possible start value to the largest possible end value
165 return new IntegerValue[]{start[0], end[1]};
166 }
167 }
168
169 /**
170 * Copy an expression. This makes a deep copy.
171 *
172 * @return the copy of the original expression
173 * @param rebindings
174 */
175
176 /*@NotNull*/
177 public Expression copy(RebindingMap rebindings) {
178 RangeExpression exp = new RangeExpression(getLhsExpression().copy(rebindings), getRhsExpression().copy(rebindings));
179 ExpressionTool.copyLocationInfo(this, exp);
180 return exp;
181 }
182
183 /**
184 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
185 * This method indicates which of these methods is provided directly. The other methods will always be available
186 * indirectly, using an implementation that relies on one of the other methods.
187 *
188 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
189 * {@link #PROCESS_METHOD}
190 */
191 @Override
192 public int getImplementationMethod() {
193 return ITERATE_METHOD;
194 }
195
196 /**
197 * Get a name identifying the kind of expression, in terms meaningful to a user.
198 *
199 * @return a name identifying the kind of expression, in terms meaningful to a user.
200 * The name will always be in the form of a lexical XML QName, and should match the name used
201 * in export() output displaying the expression.
202 */
203 @Override
204 public String getExpressionName() {
205 return "range";
206 }
207
208 /**
209 * Diagnostic print of expression structure. The abstract expression tree
210 * is written to the supplied output destination.
211 *
212 * @param out the output destination for the displayed expression tree
213 */
214 @Override
215 public void export(ExpressionPresenter out) throws XPathException {
216 out.startElement("to", this);
217 getLhsExpression().export(out);
218 getRhsExpression().export(out);
219 out.endElement();
220 }
221
222 /**
223 * Return an iteration over the sequence
224 */
225
226 /*@NotNull*/
227 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
228 IntegerValue av1 = (IntegerValue) getLhsExpression().evaluateItem(context);
229 IntegerValue av2 = (IntegerValue) getRhsExpression().evaluateItem(context);
230 return RangeIterator.makeRangeIterator(av1, av2);
231 }
232
233 }
234