Debian Codegraph @

net/sf/saxon/expr/TailCallLoop.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.instruct.UserFunction;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.ItemType;
17
18 /**
19 * A TailCallLoop wraps the body of a function that contains tail-recursive function calls. On completion
20 * of the "real" body of the function it tests whether the function has executed a tail call, and if so,
21 * iterates to evaluate the tail call.
22 */
23
24 public final class TailCallLoop extends UnaryExpression {
25
26 UserFunction containingFunction;
27
28 /**
29 * Constructor - create a TailCallLoop
30 *
31 * @param function the function in which this tail call loop appears
32 * @param body the function body (before wrapping in the tail call loop)
33 */
34
35 public TailCallLoop(UserFunction function, Expression body) {
36 super(body);
37 containingFunction = function;
38 }
39
40 /**
41 * Get the containing function
42 *
43 * @return the containing function
44 */
45
46 public UserFunction getContainingFunction() {
47 return containingFunction;
48 }
49
50 /**
51 * Type-check the expression
52 */
53
54 /*@NotNull*/
55 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
56 getOperand().typeCheck(visitor, contextInfo);
57 return this;
58 }
59
60 /**
61 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
62 * This method indicates which of these methods is provided. This implementation provides both iterate() and
63 * process() methods natively.
64 */
65
66 public int getImplementationMethod() {
67 return getBaseExpression().getImplementationMethod();
68 }
69
70 @Override
71 protected OperandRole getOperandRole() {
72 return OperandRole.SAME_FOCUS_ACTION;
73 }
74
75 /**
76 * Copy an expression. This makes a deep copy.
77 *
78 * @return the copy of the original expression
79 * @param rebindings Variables whose binding needs to change
80 */
81
82 /*@NotNull*/
83 public Expression copy(RebindingMap rebindings) {
84 throw new UnsupportedOperationException("TailCallLoop.copy()");
85 /*TailCallLoop e2 = new TailCallLoop(containingFunction);
86 e2.setBaseExpression(getBaseExpression().copy());
87 return e2;*/
88 }
89
90 /**
91 * Iterate over the sequence of values
92 */
93
94 /*@NotNull*/
95 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
96 final XPathContextMajor cm = (XPathContextMajor) context;
97 while (true) {
98 SequenceIterator<?> iter = getBaseExpression().iterate(cm);
99 GroundedValue<?> extent = iter.materialize();
100 TailCallInfo tail = cm.getTailCallInfo();
101 if (tail == null) {
102 return extent.iterate();
103 } else {
104 UserFunction target = establishTargetFunction(tail, cm);
105 if (target != containingFunction) {
106 return tailCallDifferentFunction(target, cm).iterate();
107 }
108 // otherwise, loop round to execute the tail call
109 }
110 }
111 }
112
113 /**
114 * Evaluate as an Item.
115 */
116
117 public Item evaluateItem(XPathContext context) throws XPathException {
118 final XPathContextMajor cm = (XPathContextMajor) context;
119 while (true) {
120 Item item = getBaseExpression().evaluateItem(context);
121
122 TailCallInfo tail = cm.getTailCallInfo();
123 if (tail == null) {
124 return item;
125 } else {
126 UserFunction target = establishTargetFunction(tail, cm);
127 if (target != containingFunction) {
128 return tailCallDifferentFunction(target, cm).head();
129 }
130 // otherwise, loop round to execute the tail call
131 }
132 }
133 }
134
135 private UserFunction establishTargetFunction(TailCallInfo tail, XPathContextMajor cm) {
136 if (tail instanceof TailCallFunction) {
137 return ((TailCallFunction) tail).function;
138 } else if (tail instanceof TailCallComponent) {
139 Component targetComponent = ((TailCallComponent) tail).component;
140 cm.setCurrentComponent(targetComponent);
141 return (UserFunction) targetComponent.getActor();
142 } else {
143 throw new AssertionError();
144 }
145 }
146
147 /**
148 * Process the function body
149 *
150 * @param context The dynamic context, giving access to the current node,
151 * the current variables, etc.
152 */
153
154 public void process(XPathContext context) throws XPathException {
155 final XPathContextMajor cm = (XPathContextMajor) context;
156 Expression operand = getBaseExpression();
157 while (true) {
158 operand.process(context);
159 TailCallInfo tail = cm.getTailCallInfo();
160 if (tail == null) {
161 return;
162 } else {
163 UserFunction target = establishTargetFunction(tail, cm);
164 if (target != containingFunction) {
165 SequenceTool.process(tailCallDifferentFunction(target, cm), cm, operand.getLocation());
166 return;
167 }
168 // otherwise, loop round to execute the tail call
169 }
170 }
171 }
172
173 /**
174 * Make a tail call on a different function. This reuses the context object and the stack frame array
175 * where possible, but it does consume some Java stack space. It's still worth it, because we don't use
176 * as much stack as we would if we didn't return down to the TailCallLoop level.
177 *
178 * @param userFunction the function to be called
179 * @param cm the dynamic context
180 * @return the result of calling the other function
181 * @throws XPathException if the called function fails
182 */
183
184 /*@Nullable*/
185 private Sequence<?> tailCallDifferentFunction(UserFunction userFunction, XPathContextMajor cm) throws XPathException {
186 cm.resetStackFrameMap(userFunction.getStackFrameMap(), userFunction.getArity());
187 try {
188 return userFunction.getEvaluator().evaluate(userFunction.getBody(), cm);
189 } catch (XPathException err) {
190 err.maybeSetLocation(getLocation());
191 err.maybeSetContext(cm);
192 throw err;
193 }
194 }
195
196
197 /**
198 * Determine the data type of the items returned by the expression
199 */
200 public ItemType getItemType() {
201 return getBaseExpression().getItemType();
202 }
203
204 /**
205 * Give a string representation of the expression name for use in diagnostics
206 *
207 * @return the expression name, as a string
208 */
209
210 public String getExpressionName() {
211 return "tailCallLoop";
212 }
213
214
215
216 public interface TailCallInfo {}
217
218 protected static class TailCallComponent implements TailCallInfo {
219 public Component component;
220 public UserFunction function;
221 }
222
223 protected static class TailCallFunction implements TailCallInfo {
224 public UserFunction function;
225 }
226
227
228
229
230 }
231