Debian Codegraph @

net/sf/saxon/expr/ConsumingOperand.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.ExpressionTool;
11 import net.sf.saxon.expr.parser.RebindingMap;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.LazySequence;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.ItemType;
19
20 /**
21 * This expression is used as a proxy for the consuming operand of an expression such as an
22 * arithmetic expression for which there is no explicit streaming support. The actual subexpression
23 * is retained, so that it is accessible to compile time operations that walk the expression tree;
24 * but at evaluation time, the subexpression is not evaluated in the conventional (pull) way,
25 * rather its value is pushed up the tree as a consequence of template inversion.
26 *
27 * Previously (until 9.6) a SuppliedParameterReference was used for the purpose; but this caused
28 * problems with allocation of local variable slots: see bug 2320.
29 */
30
31 public class ConsumingOperand extends UnaryExpression {
32
33
34
35 /**
36 * Constructor
37 *
38 * @param subExpression identifies the expression for which this is a proxy.
39 */
40
41 public ConsumingOperand(Expression subExpression) {
42 super(subExpression);
43 }
44
45 /**
46 * Get the usage (in terms of streamability analysis) of the single operand
47 *
48 * @return the operand usage
49 */
50 @Override
51 protected OperandRole getOperandRole() {
52 return new OperandRole(0, OperandUsage.ABSORPTION);
53 }
54
55 /**
56 * Determine the data type of the expression, if possible.
57 *
58 * @return Type.ITEM, because we don't know the type of the supplied value
59 * in advance.
60 */
61
62 /*@NotNull*/
63 public ItemType getItemType() {
64 return getBaseExpression().getItemType();
65 }
66
67 /**
68 * Determine the intrinsic dependencies of an expression, that is, those which are not derived
69 * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
70 * on the context position, while (position()+1) does not. The default implementation
71 * of the method returns 0, indicating "no dependencies".
72 *
73 * @return a set of bit-significant flags identifying the "intrinsic"
74 * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
75 */
76
77 public int getIntrinsicDependencies() {
78 return getBaseExpression().getIntrinsicDependencies();
79 }
80
81 /**
82 * Get the static cardinality
83 *
84 * @return ZERO_OR_MORE, unless we know the type of the supplied value
85 * in advance.
86 */
87
88 public int computeCardinality() {
89 return getBaseExpression().getCardinality();
90 }
91
92 /**
93 * Copy an expression. This makes a deep copy.
94 *
95 * @return the copy of the original expression
96 * @param rebindings
97 */
98
99 /*@NotNull*/
100 public Expression copy(RebindingMap rebindings) {
101 ConsumingOperand exp = new ConsumingOperand(getBaseExpression().copy(rebindings));
102 ExpressionTool.copyLocationInfo(this, exp);
103 return exp;
104 }
105
106 /**
107 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
108 * This method indicates which of these methods is provided directly. The other methods will always be available
109 * indirectly, using an implementation that relies on one of the other methods.
110 *
111 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
112 * {@link #PROCESS_METHOD}
113 */
114 @Override
115 public int getImplementationMethod() {
116 return EVALUATE_METHOD | ITERATE_METHOD;
117 }
118
119 /**
120 * Get the value of this expression in a given context.
121 *
122 * @param c the XPathContext which contains the relevant variable bindings
123 * @return the value of the variable, if it is defined
124 * @throws net.sf.saxon.trans.XPathException if the variable is undefined
125 */
126
127 public Sequence<?> evaluate(XPathContext c) throws XPathException {
128 if (c.getStackFrame().holdsDynamicValue()) {
129 return c.getStackFrame().popDynamicValue();
130 } else {
131 return new LazySequence<>(getBaseExpression().iterate(c));
132 }
133 }
134
135 /**
136 * Get the value of this expression in a given context.
137 *
138 * @param context the XPathContext which contains the relevant variable bindings
139 * @return the value of the variable, if it is defined
140 * @throws net.sf.saxon.trans.XPathException if the variable is undefined
141 */
142
143 /*@NotNull*/
144 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
145 return evaluate(context).iterate();
146 }
147
148 /**
149 * Evaluate an expression as a single item. This always returns either a single Item or
150 * null (denoting the empty sequence). No conversion is done. This method should not be
151 * used unless the static type of the expression is a subtype of "item" or "item?": that is,
152 * it should not be called if the expression may return a sequence. There is no guarantee that
153 * this condition will be detected.
154 *
155 * @param context The context in which the expression is to be evaluated
156 * @return the node or atomic value that results from evaluating the
157 * expression; or null to indicate that the result is an empty
158 * sequence
159 * @throws net.sf.saxon.trans.XPathException
160 * if any dynamic error occurs evaluating the
161 * expression
162 */
163
164 public Item evaluateItem(XPathContext context) throws XPathException {
165 return evaluate(context).head();
166 }
167
168 /**
169 * Get a name identifying the kind of expression, in terms meaningful to a user.
170 *
171 * @return a name identifying the kind of expression, in terms meaningful to a user.
172 * The name will always be in the form of a lexical XML QName, and should match the name used
173 * in export() output displaying the expression.
174 */
175 @Override
176 public String getExpressionName() {
177 return "consume";
178 }
179
180 /**
181 * Diagnostic print of expression structure. The abstract expression tree
182 * is written to the supplied output destination.
183 */
184
185 public void export(ExpressionPresenter destination) throws XPathException {
186 destination.startElement("consume", this);
187 getBaseExpression().export(destination);
188 destination.endElement();
189 }
190
191 /**
192 * The toString() method for an expression attempts to give a representation of the expression
193 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
194 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
195 *
196 * @return a representation of the expression as a string
197 */
198
199 public String toString() {
200 return "consume(" + getBaseExpression().toString() + ")";
201 }
202
203 @Override
204 public String toShortString() {
205 return "consume(" + getBaseExpression().toShortString() + ")";
206 }
207 }