Debian Codegraph @

net/sf/saxon/expr/StaticFunctionCall.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.OriginalFunction;
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.functions.SystemFunction;
15 import net.sf.saxon.om.Function;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.type.UType;
23
24 /**
25 * A call to a function that is known statically. This is a stricter definition than "static function
26 * call" in the XPath grammar, because it excludes calls that might be re-bound to a different function
27 * as a result of XSLT overrides in a different package, calls to functions that hold dynamic context
28 * information in their closure, and so on.
29 */
30 public class StaticFunctionCall extends FunctionCall implements Callable {
31
32 private Function target;
33
34 public StaticFunctionCall(Function target, Expression[] arguments) {
35 if (target.getArity() != arguments.length) {
36 throw new IllegalArgumentException("Function call to " + target.getFunctionName() + " with wrong number of arguments (" + arguments.length + ")");
37 }
38 this.target = target;
39 setOperanda(arguments, target.getOperandRoles());
40 }
41
42 /**
43 * Get the target function to be called
44 *
45 * @return the target function
46 */
47
48 public Function getTargetFunction() {
49 return target;
50 }
51
52
53 /**
54 * Get the target function to be called
55 *
56 * @param context the dynamic evaluation context (not used in this implementation)
57 * @return the target function
58 */
59 @Override
60 public Function getTargetFunction(XPathContext context) {
61 return getTargetFunction();
62 }
63
64 /**
65 * Get the qualified of the function being called
66 *
67 * @return the qualified name
68 */
69 @Override
70 public StructuredQName getFunctionName() {
71 return target.getFunctionName();
72 }
73
74 /**
75 * Ask whether this expression is a call on a particular function
76 *
77 * @param function the implementation class of the function in question
78 * @return true if the expression is a call on the function
79 */
80
81 public boolean isCallOn(Class<? extends SystemFunction> function) {
82 return function.isAssignableFrom(target.getClass());
83 }
84
85 /**
86 * Type-check the expression. This also calls preEvaluate() to evaluate the function
87 * if all the arguments are constant; functions that do not require this behavior
88 * can override the preEvaluate method.
89 *
90 * @param visitor the expression visitor
91 * @param contextInfo information about the type of the context item
92 */
93 @Override
94 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
95 checkFunctionCall(target, visitor);
96 return super.typeCheck(visitor, contextInfo);
97 }
98
99 /**
100 * Copy an expression. This makes a deep copy.
101 *
102 * @return the copy of the original expression
103 * @param rebindings variables that need to be re-bound
104 */
105 @Override
106 public Expression copy(RebindingMap rebindings) {
107 Expression[] args = new Expression[getArity()];
108 for (int i=0; i<args.length; i++) {
109 args[i] = getArg(i).copy(rebindings);
110 }
111 return new StaticFunctionCall(target, args);
112 }
113
114 /**
115 * Compute the static cardinality of this expression
116 *
117 * @return the computed cardinality, as one of the values {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_ONE},
118 * {@link net.sf.saxon.expr.StaticProperty#EXACTLY_ONE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ONE_OR_MORE},
119 * {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_MORE}
120 */
121 @Override
122 protected int computeCardinality() {
123 return target.getFunctionItemType().getResultType().getCardinality();
124 }
125
126 /**
127 * Determine the data type of the expression, if possible. All expression return
128 * sequences, in general; this method determines the type of the items within the
129 * sequence, assuming that (a) this is known in advance, and (b) it is the same for
130 * all items in the sequence.
131 * <p>This method should always return a result, though it may be the best approximation
132 * that is available at the time.</p>
133 *
134 * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER,
135 * Type.NODE, or Type.ITEM (meaning not known at compile time)
136 */
137 @Override
138 public ItemType getItemType() {
139 return target.getFunctionItemType().getResultType().getPrimaryType();
140 }
141
142 /**
143 * Get the static type of the expression as a UType, following precisely the type
144 * inference rules defined in the XSLT 3.0 specification.
145 *
146 * @param contextItemType the static type of the context item
147 * @return the static item type of the expression according to the XSLT 3.0 defined rules
148 */
149 @Override
150 public UType getStaticUType(UType contextItemType) {
151 UType result = getItemType().getUType();
152 for (Operand o : operands()) {
153 if (o.getUsage() == OperandUsage.TRANSMISSION) {
154 result = result.intersection(o.getChildExpression().getStaticUType(contextItemType));
155 }
156 }
157 return result;
158 }
159
160 /**
161 * Call the Callable.
162 *
163 * @param context the dynamic evaluation context
164 * @param arguments the values of the arguments, supplied as Sequences.
165 * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to
166 * call it only once; if the value is required more than once, it should first be converted
167 * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd
168 * SequenceTool.toGroundedValue().</p>
169 * <p>If the expected value is a single item, the item should be obtained by calling
170 * Sequence.head(): it cannot be assumed that the item will be passed as an instance of
171 * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p>
172 * <p>It is the caller's responsibility to perform any type conversions required
173 * to convert arguments to the type expected by the callee. An exception is where
174 * this Callable is explicitly an argument-converting wrapper around the original
175 * Callable.</p>
176 * @return the result of the evaluation, in the form of a Sequence. It is the responsibility
177 * of the callee to ensure that the type of result conforms to the expected result type.
178 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs during the evaluation of the expression
179 */
180 public Sequence<?> call(XPathContext context, Sequence<?>[] arguments) throws XPathException {
181 return target.call(context, arguments);
182 }
183
184 /**
185 * Get a name identifying the kind of expression, in terms meaningful to a user.
186 *
187 * @return a name identifying the kind of expression, in terms meaningful to a user.
188 * The name will always be in the form of a lexical XML QName, and should match the name used
189 * in explain() output displaying the expression.
190 */
191 @Override
192 public String getExpressionName() {
193 return "staticFunctionCall";
194 }
195
196 /**
197 * Serialized output of expression structure. The abstract expression tree
198 * is written to the supplied output destination.
199 *
200 * @param out destination of the SEF output
201 */
202 @Override
203 public void export(ExpressionPresenter out) throws XPathException {
204 if (target instanceof OriginalFunction) {
205 ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions();
206 OriginalFunction pf = (OriginalFunction) target;
207 out.startElement("origFC", this);
208 out.emitAttribute("name", pf.getFunctionName());
209 out.emitAttribute("pack", options.packageMap.get(pf.getComponent().getContainingPackage()) + "");
210
211 for (Operand o : operands()) {
212 o.getChildExpression().export(out);
213 }
214 out.endElement();
215 } else if (target instanceof UnionCastableFunction) {
216 // Bug 2611. Bug 3822.
217 out.startElement("castable", this);
218 out.emitAttribute("emptiable", ((UnionCastableFunction) target).isAllowEmpty() ? "1" : "0");
219 out.emitAttribute("as", ((UnionCastableFunction) target).getTargetType().toExportString());
220 for (Operand o : operands()) {
221 o.getChildExpression().export(out);
222 }
223 out.endElement();
224 } else if (target instanceof ListCastableFunction) {
225 // Bug 2611. Bug 3822.
226 out.startElement("castable", this);
227 out.emitAttribute("emptiable", ((ListCastableFunction) target).isAllowEmpty() ? "1" : "0");
228 out.emitAttribute("as", ((ListCastableFunction) target).getTargetType().getStructuredQName());
229 for (Operand o : operands()) {
230 o.getChildExpression().export(out);
231 }
232 out.endElement();
233 } else if (target instanceof UnionConstructorFunction) {
234 // Bug 2611.
235 out.startElement("cast", this);
236 out.emitAttribute("emptiable", ((UnionConstructorFunction) target).isAllowEmpty() ? "1" : "0");
237 out.emitAttribute("as", ((UnionConstructorFunction) target).getTargetType().toExportString());
238 for (Operand o : operands()) {
239 o.getChildExpression().export(out);
240 }
241 out.endElement();
242 } else if (target instanceof ListConstructorFunction) {
243 // Bug 2611.
244 out.startElement("cast", this);
245 out.emitAttribute("emptiable", ((ListConstructorFunction) target).isAllowEmpty() ? "1" : "0");
246 out.emitAttribute("as", ((ListConstructorFunction) target).getTargetType().getStructuredQName());
247 for (Operand o : operands()) {
248 o.getChildExpression().export(out);
249 }
250 out.endElement();
251 } else {
252 super.export(out);
253 }
254 }
255
256
257 }
258