Debian Codegraph @

net/sf/saxon/expr/instruct/NumberInstruction.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.instruct;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionTool;
13 import net.sf.saxon.expr.parser.ExpressionVisitor;
14 import net.sf.saxon.expr.parser.RebindingMap;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.NodeInfo;
17 import net.sf.saxon.om.SequenceIterator;
18 import net.sf.saxon.pattern.Pattern;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.ListIterator;
22 import net.sf.saxon.tree.util.Navigator;
23 import net.sf.saxon.type.BuiltInAtomicType;
24 import net.sf.saxon.type.ItemType;
25 import net.sf.saxon.type.Type;
26 import net.sf.saxon.value.Int64Value;
27 import net.sf.saxon.value.IntegerValue;
28 import net.sf.saxon.value.SequenceType;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 /**
34 * This instruction performs the node-counting function of the xsl:number instruction.
35 * It delivers as its result a sequence of integers. The parent expression is typically
36 * a {@link NumberSequenceFormatter} which takes this sequence of integers and performs
37 * the necessary formatting.
38 */
39
40 public class NumberInstruction extends Expression {
41
42 public static final int SINGLE = 0;
43 public static final int MULTI = 1;
44 public static final int ANY = 2;
45 public static final int SIMPLE = 3;
46 public static final String[] LEVEL_NAMES = new String[]{"single", "multi", "any", "simple"};
47
48 private Operand selectOp;
49
50 private int level;
51 private Operand countOp;
52 private Operand fromOp;
53 private boolean hasVariablesInPatterns = false;
54
55 /**
56 * Construct a NumberInstruction
57 *
58 * @param select the expression supplied in the select attribute
59 * @param level one of "single", "level", "multi"
60 * @param count the pattern supplied in the count attribute
61 * @param from the pattern supplied in the from attribute
62 */
63
64 public NumberInstruction(Expression select,
65 int level,
66 Pattern count,
67 Pattern from) {
68
69 assert select != null;
70
71 selectOp = new Operand(this, select, new OperandRole(0, OperandUsage.NAVIGATION, SequenceType.SINGLE_NODE));
72
73 this.level = level;
74 if (count != null) {
75 countOp = new Operand(this, count, OperandRole.INSPECT);
76 }
77 if (from != null) {
78 fromOp = new Operand(this, from, OperandRole.INSPECT);
79 }
80 this.hasVariablesInPatterns = Pattern.patternContainsVariable(count) || Pattern.patternContainsVariable(from);
81 }
82
83 /**
84 * Ask whether this expression is an instruction. In XSLT streamability analysis this
85 * is used to distinguish constructs corresponding to XSLT instructions from other constructs,
86 * typically XPath expressions.
87 *
88 * @return true -- this construct originates as an XSLT instruction
89 */
90 @Override
91 public boolean isInstruction() {
92 return true;
93 }
94
95 @Override
96 public Iterable<Operand> operands() {
97 return operandSparseList(selectOp, countOp, fromOp);
98 }
99
100 /**
101 * Get the level attribute
102 *
103 * @return the coded value of the level attribute
104 */
105
106 public int getLevel() {
107 return level;
108 }
109
110 /**
111 * Get the count pattern, if specified
112 *
113 * @return the count pattern if there is one, otherwise null
114 */
115
116 public Pattern getCount() {
117 return countOp == null ? null : (Pattern) countOp.getChildExpression();
118 }
119
120 /**
121 * Get the from pattern, if specified
122 *
123 * @return the from pattern if there is one, otherwise null
124 */
125
126 public Pattern getFrom() {
127 return fromOp == null ? null : (Pattern) fromOp.getChildExpression();
128 }
129
130 /**
131 * Get the select expression
132 *
133 * @return the select expression (which defaults to a ContextItemExpression)
134 */
135
136 public Expression getSelect() {
137 return selectOp.getChildExpression();
138 }
139
140 /**
141 * Copy an expression. This makes a deep copy.
142 *
143 * @param rebindings a mutable list of (old binding, new binding) pairs
144 * * that is used to update the bindings held in any
145 * * local variable references that are copied.
146 * @return the copy of the original expression
147 */
148
149 /*@NotNull*/
150 public Expression copy(RebindingMap rebindings) {
151 NumberInstruction exp = new NumberInstruction(copy(selectOp, rebindings), level,
152 copy(getCount(), rebindings),
153 copy(getFrom(), rebindings));
154 ExpressionTool.copyLocationInfo(this, exp);
155 return exp;
156 }
157
158 private Expression copy(Operand op, RebindingMap rebindings) {
159 return op == null ? null : op.getChildExpression().copy(rebindings);
160 }
161
162 private Pattern copy(Pattern op, RebindingMap rebindings) {
163 return op == null ? null : op.copy(rebindings);
164 }
165
166
167 /*@NotNull*/
168 public ItemType getItemType() {
169 return BuiltInAtomicType.INTEGER;
170 }
171
172 public int computeCardinality() {
173 switch (level) {
174 case SIMPLE:
175 case SINGLE:
176 case ANY:
177 return StaticProperty.ALLOWS_ZERO_OR_ONE;
178 case MULTI:
179 default:
180 return StaticProperty.ALLOWS_ZERO_OR_MORE;
181 }
182 }
183
184 /**
185 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
186 * This method indicates which of these methods is provided directly. The other methods will always be available
187 * indirectly, using an implementation that relies on one of the other methods.
188 *
189 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
190 * {@link #PROCESS_METHOD}
191 */
192 @Override
193 public int getImplementationMethod() {
194 return ITERATE_METHOD;
195 }
196
197 /**
198 * Perform optimisation of an expression and its subexpressions. This is the third and final
199 * phase of static optimization.
200 * <p>This method is called after all references to functions and variables have been resolved
201 * to the declaration of the function or variable, and after all type checking has been done.</p>
202 *
203 * @param visitor an expression visitor
204 * @param contextInfo the static type of "." at the point where this expression is invoked.
205 * The parameter is set to null if it is known statically that the context item will be undefined.
206 * If the type of the context item is not known statically, the argument is set to
207 * {@link Type#ITEM_TYPE}
208 * @return the original expression, rewritten if appropriate to optimize execution
209 * @throws XPathException if an error is discovered during this phase
210 * (typically a type error)
211 */
212 @Override
213 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
214 Expression e = super.optimize(visitor, contextInfo);
215 if (e != this) {
216 return e;
217 }
218 if ("EE".equals(getPackageData().getTargetEdition())) {
219 e = visitor.obtainOptimizer().optimizeNumberInstruction(this, contextInfo);
220 if (e != null) {
221 return e;
222 }
223 }
224 return this;
225 }
226
227 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
228 List<IntegerValue> vec = new ArrayList<>(1);
229 NodeInfo source = (NodeInfo) selectOp.getChildExpression().evaluateItem(context);
230
231 switch (level) {
232 case SIMPLE: {
233 long value = Navigator.getNumberSimple(source, context);
234 if (value != 0) {
235 vec.add(Int64Value.makeIntegerValue(value));
236 }
237 break;
238 }
239 case SINGLE: {
240 long value = Navigator.getNumberSingle(source, getCount(), getFrom(), context);
241 if (value != 0) {
242 vec.add(Int64Value.makeIntegerValue(value));
243 }
244 break;
245 }
246 case ANY: {
247 long value = Navigator.getNumberAny(this, source, getCount(), getFrom(), context, hasVariablesInPatterns);
248 if (value != 0) {
249 vec.add(Int64Value.makeIntegerValue(value));
250 }
251 break;
252 }
253 case MULTI: {
254 for (long n : Navigator.getNumberMulti(source, getCount(), getFrom(), context)) {
255 vec.add(Int64Value.makeIntegerValue(n));
256 }
257 break;
258 }
259 }
260
261 return new ListIterator<>(vec);
262 }
263
264 @Override
265 public String getExpressionName() {
266 return "xsl:number";
267 }
268
269 /**
270 * Diagnostic print of expression structure. The abstract expression tree
271 * is written to the supplied output destination.
272 */
273
274 public void export(ExpressionPresenter out) throws XPathException {
275 out.startElement("nodeNum", this);
276 out.emitAttribute("level", LEVEL_NAMES[level]);
277 out.setChildRole("select");
278 selectOp.getChildExpression().export(out);
279 if (countOp != null) {
280 out.setChildRole("count");
281 getCount().export(out);
282 }
283 if (fromOp != null) {
284 out.setChildRole("from");
285 getFrom().export(out);
286 }
287 out.endElement();
288 }
289 }
290