Debian Codegraph @

net/sf/saxon/expr/instruct/IterateInstr.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.event.PipelineConfiguration;
11 import net.sf.saxon.expr.*;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RebindingMap;
16 import net.sf.saxon.lib.TraceListener;
17 import net.sf.saxon.om.FocusIterator;
18 import net.sf.saxon.om.Item;
19 import net.sf.saxon.om.StandardNames;
20 import net.sf.saxon.trace.ExpressionPresenter;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.type.*;
23 import net.sf.saxon.value.SequenceType;
24
25 /**
26 * An IterateInstr is the compiled form of an xsl:iterate instruction
27 */
28
29 public final class IterateInstr extends Instruction implements ContextSwitchingExpression {
30
31 private Operand selectOp;
32 private Operand actionOp;
33 private Operand initiallyOp;
34 private Operand onCompletionOp;
35
36 /**
37 * Create an xsl:iterate instruction
38 *
39 * @param select the select expression
40 * @param initiallyExp the initialization of the xsl:param elements
41 * @param action the body of the xsl:iterate loop
42 * @param onCompletion the expression to be evaluated before final completion, may be null
43 */
44
45 public IterateInstr(Expression select, LocalParamBlock initiallyExp, Expression action, /*@Nullable*/ Expression onCompletion) {
46 if (onCompletion == null) {
47 onCompletion = Literal.makeEmptySequence();
48 }
49 selectOp = new Operand(this, select, OperandRole.FOCUS_CONTROLLING_SELECT);
50 actionOp = new Operand(this, action, OperandRole.FOCUS_CONTROLLED_ACTION);
51 initiallyOp = new Operand(this, initiallyExp,
52 new OperandRole(OperandRole.CONSTRAINED_CLASS, OperandUsage.NAVIGATION, SequenceType.ANY_SEQUENCE));
53 onCompletionOp = new Operand(this, onCompletion,
54 new OperandRole(OperandRole.USES_NEW_FOCUS, OperandUsage.TRANSMISSION));
55 }
56
57 public void setSelect(Expression select) {
58 selectOp.setChildExpression(select);
59 }
60
61 public LocalParamBlock getInitiallyExp() {
62 return (LocalParamBlock)initiallyOp.getChildExpression();
63 }
64
65 public void setInitiallyExp(LocalParamBlock initiallyExp) {
66 initiallyOp.setChildExpression(initiallyExp);
67 }
68
69 public void setAction(Expression action) {
70 actionOp.setChildExpression(action);
71 }
72
73 public Expression getOnCompletion() {
74 return onCompletionOp.getChildExpression();
75 }
76
77 public void setOnCompletion(Expression onCompletion) {
78 onCompletionOp.setChildExpression(onCompletion);
79 }
80
81 @Override
82 public Iterable<Operand> operands() {
83 return operandList(selectOp, actionOp, initiallyOp, onCompletionOp);
84 }
85
86
87
88 /**
89 * Get the namecode of the instruction for use in diagnostics
90 *
91 * @return a code identifying the instruction: typically but not always
92 * the fingerprint of a name in the XSLT namespace
93 */
94
95 @Override
96 public int getInstructionNameCode() {
97 return StandardNames.XSL_ITERATE;
98 }
99
100 /**
101 * Get the select expression (the select attribute of the xsl:iterate)
102 *
103 * @return the select expression
104 */
105
106 public Expression getSelectExpression() {
107 return selectOp.getChildExpression();
108 }
109
110
111 /**
112 * Get the action expression (the content of the xsl:iterate)
113 *
114 * @return the body of the xsl:iterate loop
115 */
116
117 public Expression getActionExpression() {
118 return actionOp.getChildExpression();
119 }
120
121 /**
122 * Type-check the expression
123 */
124
125 /*@NotNull*/
126 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
127
128 selectOp.typeCheck(visitor, contextInfo);
129 initiallyOp.typeCheck(visitor, contextInfo);
130
131 ItemType selectType = getSelectExpression().getItemType();
132 if (selectType == ErrorType.getInstance()) {
133 return Literal.makeEmptySequence();
134 }
135
136 ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo(getSelectExpression().getItemType(), false);
137 cit.setContextSettingExpression(getSelectExpression());
138 actionOp.typeCheck(visitor, cit);
139
140 onCompletionOp.typeCheck(visitor, ContextItemStaticInfo.ABSENT);
141
142 if (Literal.isEmptySequence(getSelectExpression())) {
143 return getSelectExpression();
144 }
145 if (Literal.isEmptySequence(getActionExpression())) {
146 return getActionExpression();
147 }
148 return this;
149 }
150
151 /*@NotNull*/
152 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
153
154 selectOp.optimize(visitor, contextInfo);
155 initiallyOp.optimize(visitor, contextInfo);
156
157 ContextItemStaticInfo cit2 = visitor.getConfiguration().makeContextItemStaticInfo(getSelectExpression().getItemType(), false);
158 cit2.setContextSettingExpression(getSelectExpression());
159 actionOp.optimize(visitor, cit2);
160
161 onCompletionOp.optimize(visitor, ContextItemStaticInfo.ABSENT);
162
163 if (Literal.isEmptySequence(getSelectExpression())) {
164 return getSelectExpression();
165 }
166 if (Literal.isEmptySequence(getActionExpression())) {
167 return getActionExpression();
168 }
169
170 return this;
171 }
172
173 /**
174 * Test whether it is possible to generate byte-code for the instruction.
175 * This is not possible unless any xsl:break or xsl:next-iteration instructions
176 * are part of the same compilation unit, which is not the case if they appear
177 * within a try-catch.
178 * @return true if the instruction does not contain an xsl:break or xsl:next-iteration instruction
179 * within a try/catch block
180 */
181
182 public boolean isCompilable() {
183 return !containsBreakOrNextIterationWithinTryCatch(this, false);
184 }
185
186 private static boolean containsBreakOrNextIterationWithinTryCatch(Expression exp, boolean withinTryCatch) {
187 if (exp instanceof BreakInstr || exp instanceof NextIteration) {
188 return withinTryCatch;
189 } else {
190 boolean found = false;
191 boolean inTryCatch = withinTryCatch || exp instanceof TryCatch;
192 for (Operand o : exp.operands()) {
193 if (containsBreakOrNextIterationWithinTryCatch(o.getChildExpression(), inTryCatch)) {
194 found = true;
195 break;
196 }
197 }
198 return found;
199 }
200 }
201
202 /**
203 * Determine the data type of the items returned by this expression
204 *
205 * @return the data type
206 */
207
208 /*@NotNull*/
209 public final ItemType getItemType() {
210 if (Literal.isEmptySequence(getOnCompletion())) {
211 return getActionExpression().getItemType();
212 } else {
213 TypeHierarchy th = getConfiguration().getTypeHierarchy();
214 return Type.getCommonSuperType(getActionExpression().getItemType(), getOnCompletion().getItemType(), th);
215 }
216 }
217
218 /**
219 * Determine whether this instruction creates new nodes.
220 * This implementation returns true if the "action" creates new nodes.
221 * (Nodes created by the condition can't contribute to the result).
222 */
223
224 public final boolean mayCreateNewNodes() {
225 return (getActionExpression().getSpecialProperties() &
226 getOnCompletion().getSpecialProperties() &
227 StaticProperty.NO_NODES_NEWLY_CREATED) == 0;
228 }
229
230 /**
231 * Ask whether this expression is, or contains, the binding of a given variable
232 *
233 * @param binding the variable binding
234 * @return true if this expression is the variable binding (for example a ForExpression
235 * or LetExpression) or if it is a FLWOR expression that binds the variable in one of its
236 * clauses.
237 */
238 @Override
239 public boolean hasVariableBinding(Binding binding) {
240 LocalParamBlock paramBlock = getInitiallyExp();
241 for (Operand o : paramBlock.operands()) {
242 LocalParam setter = (LocalParam)o.getChildExpression();
243 if (setter == binding) {
244 return true;
245 }
246 }
247 return false;
248 }
249
250 /**
251 * Get the (partial) name of a class that supports streaming of this kind of expression
252 *
253 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
254 * or null if there is no such class
255 */
256 @Override
257 public String getStreamerName() {
258 return "Iterate";
259 }
260
261 /**
262 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
263 * This method indicates which of these methods is provided. This implementation provides both iterate() and
264 * process() methods natively.
265 */
266
267 public int getImplementationMethod() {
268 return PROCESS_METHOD;
269 }
270
271 /**
272 * Check that any elements and attributes constructed or returned by this expression are acceptable
273 * in the content model of a given complex type. It's always OK to say yes, since the check will be
274 * repeated at run-time. The process of checking element and attribute constructors against the content
275 * model of a complex type also registers the type of content expected of those constructors, so the
276 * static validation can continue recursively.
277 */
278
279 public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException {
280 getActionExpression().checkPermittedContents(parentType, false);
281 getOnCompletion().checkPermittedContents(parentType, false);
282 }
283
284 /**
285 * Copy an expression. This makes a deep copy.
286 *
287 * @return the copy of the original expression
288 * @param rebindings a mutable list of (old binding, new binding) pairs
289 * that is used to update the bindings held in any
290 * local variable references that are copied.
291 */
292
293 /*@NotNull*/
294 public Expression copy(RebindingMap rebindings) {
295 IterateInstr exp = new IterateInstr(
296 getSelectExpression().copy(rebindings),
297 (LocalParamBlock) getInitiallyExp().copy(rebindings),
298 getActionExpression().copy(rebindings),
299 getOnCompletion().copy(rebindings));
300 ExpressionTool.copyLocationInfo(this, exp);
301 return exp;
302 }
303
304
305 public TailCall processLeavingTail(XPathContext context) throws XPathException {
306 XPathContextMajor c2 = context.newContext();
307 c2.setOrigin(this);
308 FocusIterator iter = c2.trackFocus(getSelectExpression().iterate(context));
309 c2.setCurrentTemplateRule(null);
310 PipelineConfiguration pipe = c2.getReceiver().getPipelineConfiguration();
311 pipe.setXPathContext(c2);
312
313 boolean tracing = context.getController().isTracing();
314 TraceListener listener = tracing ? context.getController().getTraceListener() : null;
315
316 getInitiallyExp().process(context);
317
318 while (true) {
319 Item item = iter.next();
320 if (item != null) {
321 if (tracing) {
322 listener.startCurrentItem(item);
323 }
324 getActionExpression().process(c2);
325 if (tracing) {
326 listener.endCurrentItem(item);
327 }
328 TailCallLoop.TailCallInfo comp = c2.getTailCallInfo();
329 if (comp == null) {
330 // no xsl:next-iteration or xsl:break was encountered; just loop around
331 } else if (comp instanceof BreakInstr) {
332 // indicates a xsl:break instruction was encountered: break the loop
333 //System.err.println("IterateInstr found xsl:break");
334 iter.close();
335 return null;
336 } else {
337 // a xsl:next-iteration instruction was encountered.
338 // It will have reset the parameters to the loop; we just need to loop round
339 }
340 } else {
341 // Execute on-completion instruction
342 XPathContextMinor c3 = context.newMinorContext();
343 c3.setCurrentIterator(null);
344 getOnCompletion().process(c3);
345 break;
346 }
347 }
348 pipe.setXPathContext(context);
349 return null;
350 }
351
352
353 /**
354 * Diagnostic print of expression structure. The abstract expression tree
355 * is written to the supplied output destination.
356 */
357
358 public void export(ExpressionPresenter out) throws XPathException {
359 out.startElement("iterate", this);
360 out.setChildRole("select");
361 getSelectExpression().export(out);
362 out.setChildRole("params");
363 getInitiallyExp().export(out);
364 if (!Literal.isEmptySequence(getOnCompletion())) {
365 out.setChildRole("on-completion");
366 getOnCompletion().export(out);
367 }
368 out.setChildRole("action");
369 getActionExpression().export(out);
370 out.endElement();
371 }
372
373
374
375 }
376
377
378