Debian Codegraph @

net/sf/saxon/expr/SuppliedParameterReference.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.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.lib.StandardErrorListener;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.om.SequenceIterator;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.AnyItemType;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.value.SequenceType;
23
24 /**
25 * Supplied parameter reference: this is an internal expression used to refer to
26 * the value of the n'th parameter supplied on a template call or a call to an inline function.
27 * It is used within a type-checking expression designed to check the consistency
28 * of the supplied value with the required type. This type checking is all done
29 * at run-time, because the binding of apply-templates to actual template rules
30 * is entirely dynamic.
31 */
32
33 public class SuppliedParameterReference extends Expression {
34
35 int slotNumber;
36 SequenceType type;
37
38 /**
39 * Constructor
40 *
41 * @param slot identifies this parameter. The value -1 indicates that the value is to be obtained
42 * from the dynamic stack held in the context object.
43 */
44
45 public SuppliedParameterReference(int slot) {
46 slotNumber = slot;
47 }
48
49 /**
50 * Get the slot number
51 *
52 * @return the slot number
53 */
54
55 public int getSlotNumber() {
56 return slotNumber;
57 }
58
59 /**
60 * Set the type of the supplied value if known
61 *
62 * @param type of the supplied value
63 */
64
65 public void setSuppliedType(SequenceType type) {
66 this.type = type;
67 }
68
69 /*@NotNull*/
70 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
71 return this;
72 }
73
74 /*@NotNull*/
75 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
76 return this;
77 }
78
79 /**
80 * Determine the data type of the expression, if possible.
81 *
82 * @return Type.ITEM, because we don't know the type of the supplied value
83 * in advance.
84 */
85
86 /*@NotNull*/
87 public ItemType getItemType() {
88 if (type != null) {
89 return type.getPrimaryType();
90 } else {
91 return AnyItemType.getInstance();
92 }
93 }
94
95 /**
96 * Determine the intrinsic dependencies of an expression, that is, those which are not derived
97 * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
98 * on the context position, while (position()+1) does not. The default implementation
99 * of the method returns 0, indicating "no dependencies".
100 *
101 * @return a set of bit-significant flags identifying the "intrinsic"
102 * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
103 */
104
105 public int getIntrinsicDependencies() {
106 return StaticProperty.DEPENDS_ON_LOCAL_VARIABLES;
107 }
108
109 /**
110 * Get the static cardinality
111 *
112 * @return ZERO_OR_MORE, unless we know the type of the supplied value
113 * in advance.
114 */
115
116 public int computeCardinality() {
117 if (type != null) {
118 return type.getCardinality();
119 } else {
120 return StaticProperty.ALLOWS_ZERO_OR_MORE;
121 }
122 }
123
124 /**
125 * Copy an expression. This makes a deep copy.
126 *
127 * @return the copy of the original expression
128 * @param rebindings variables that need to be re-bound
129 */
130
131 /*@NotNull*/
132 public Expression copy(RebindingMap rebindings) {
133 SuppliedParameterReference exp = new SuppliedParameterReference(slotNumber);
134 ExpressionTool.copyLocationInfo(this, exp);
135 return exp;
136 }
137
138 /**
139 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
140 * This method indicates which of these methods is provided directly. The other methods will always be available
141 * indirectly, using an implementation that relies on one of the other methods.
142 *
143 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
144 * {@link #PROCESS_METHOD}
145 */
146 @Override
147 public int getImplementationMethod() {
148 return EVALUATE_METHOD | ITERATE_METHOD;
149 }
150
151 /**
152 * Get the value of this expression in a given context.
153 *
154 * @param c the XPathContext which contains the relevant variable bindings
155 * @return the value of the variable, if it is defined
156 */
157
158 public Sequence<?> evaluateVariable(XPathContext c) {
159 if (slotNumber == -1) {
160 return c.getStackFrame().popDynamicValue();
161 }
162 try {
163 return c.evaluateLocalVariable(slotNumber);
164 } catch (AssertionError e) {
165 StandardErrorListener.printStackTrace(c, c.getConfiguration().getLogger(), 2);
166 throw new AssertionError(e.getMessage() + ". No value has been set for parameter " + slotNumber);
167 }
168 }
169
170 /**
171 * Get the value of this expression in a given context.
172 *
173 * @param context the XPathContext which contains the relevant variable bindings
174 * @return the value of the variable, if it is defined
175 * @throws XPathException if the variable is undefined
176 */
177
178 /*@NotNull*/
179 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
180 return evaluateVariable(context).iterate();
181 }
182
183 /**
184 * Evaluate an expression as a single item. This always returns either a single Item or
185 * null (denoting the empty sequence). No conversion is done. This method should not be
186 * used unless the static type of the expression is a subtype of "item" or "item?": that is,
187 * it should not be called if the expression may return a sequence. There is no guarantee that
188 * this condition will be detected.
189 *
190 * @param context The context in which the expression is to be evaluated
191 * @return the node or atomic value that results from evaluating the
192 * expression; or null to indicate that the result is an empty
193 * sequence
194 * @throws net.sf.saxon.trans.XPathException
195 * if any dynamic error occurs evaluating the
196 * expression
197 */
198
199 public Item evaluateItem(XPathContext context) throws XPathException {
200 return evaluateVariable(context).head();
201 }
202
203 /**
204 * Get a name identifying the kind of expression, in terms meaningful to a user.
205 *
206 * @return a name identifying the kind of expression, in terms meaningful to a user.
207 * The name will always be in the form of a lexical XML QName, and should match the name used
208 * in export() output displaying the expression.
209 */
210 @Override
211 public String getExpressionName() {
212 return "supplied";
213 }
214
215
216 /**
217 * Diagnostic print of expression structure. The abstract expression tree
218 * is written to the supplied output destination.
219 */
220
221 public void export(ExpressionPresenter destination) throws XPathException {
222 destination.startElement("supplied", this);
223 destination.emitAttribute("slot", slotNumber + "");
224 destination.endElement();
225 }
226
227 /**
228 * The toString() method for an expression attempts to give a representation of the expression
229 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
230 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
231 *
232 * @return a representation of the expression as a string
233 */
234
235 public String toString() {
236 return "suppliedParam(" + slotNumber + ")";
237 }
238 }