Debian Codegraph @

net/sf/saxon/functions/PositionAndLast.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.functions;
9
10 import net.sf.saxon.expr.Callable;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.om.FocusIterator;
16 import net.sf.saxon.om.Function;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.trans.SymbolicName;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.ErrorType;
21 import net.sf.saxon.value.Int64Value;
22 import net.sf.saxon.value.IntegerValue;
23
24
25 public abstract class PositionAndLast extends ContextAccessorFunction {
26
27 private boolean contextPossiblyUndefined = true;
28
29 /**
30 * Get an estimate of the net cost of evaluating the function, excluding the cost of evaluating
31 * its arguments. The result is 0 for very simple functions like position() and exists(), 1 by
32 * default, and higher values for particularly expensive functions.
33 *
34 * @return the estimated cost
35 */
36 @Override
37 public int getNetCost() {
38 // There is a special reason for returning 0: it prevents a call being loop-lifted.
39 // Test WhereExpr026 and WhereExpr027 fail if position() and last() are loop-lifted,
40 // because they end up being lazily evaluated as part of a MemoClosure.
41 return 0;
42 }
43
44 /**
45 * Bind a context item to appear as part of the function's closure. If this method
46 * has been called, the supplied context item will be used in preference to the
47 * context item at the point where the function is actually called.
48 *
49 * @param context the context to which the function applies. Must not be null.
50 */
51 @Override
52 public Function bindContext(XPathContext context) {
53 Int64Value value;
54 try {
55 value = evaluateItem(context);
56 } catch (final XPathException e) {
57 // This happens when we do a dynamic lookup of position() or last() when there is no context item
58 SymbolicName.F name = new SymbolicName.F(getFunctionName(), getArity());
59 Callable callable = (context1, arguments) -> {
60 throw e;
61 };
62 return new CallableFunction(name, callable, getFunctionItemType());
63 }
64 ConstantFunction fn = new ConstantFunction(value);
65 fn.setDetails(getDetails());
66 fn.setRetainedStaticContext(getRetainedStaticContext());
67 return fn;
68 }
69
70 /**
71 * For an expression that returns an integer or a sequence of integers, get
72 * a lower and upper bound on the values of the integers that may be returned, from
73 * static analysis. The default implementation returns null, meaning "unknown" or
74 * "not applicable". Other implementations return an array of two IntegerValue objects,
75 * representing the lower and upper bounds respectively. The values
76 * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that
77 * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH
78 * are used to indicate values limited by the size of a string or the size of a sequence.
79 *
80 * @return the lower and upper bounds of integer values in the result, or null to indicate
81 * unknown or not applicable.
82 */
83 @Override
84 public IntegerValue[] getIntegerBounds() {
85 return new IntegerValue[]{Int64Value.PLUS_ONE, Expression.MAX_SEQUENCE_LENGTH};
86 }
87
88 @Override
89 public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression[] arguments) throws XPathException {
90 super.supplyTypeInformation(visitor, contextInfo, arguments);
91 if (contextInfo.getItemType() == ErrorType.getInstance()) {
92 XPathException err = new XPathException("The context item is absent at this point");
93 err.setErrorCode("XPDY0002");
94 throw err;
95 } else {
96 contextPossiblyUndefined = contextInfo.isPossiblyAbsent();
97 }
98 }
99
100 /**
101 * Ask whether the context item may possibly be undefined
102 *
103 * @return true if it might be undefined
104 */
105
106 public boolean isContextPossiblyUndefined() {
107 return contextPossiblyUndefined;
108 }
109
110 /**
111 * Evaluate in a general context
112 */
113
114 public abstract Int64Value evaluateItem(XPathContext c) throws XPathException;
115
116 /**
117 * Evaluate the expression
118 *
119 * @param context the dynamic evaluation context
120 * @param arguments the values of the arguments, supplied as Sequences
121 * @return the result of the evaluation, in the form of a Sequence
122 * @throws net.sf.saxon.trans.XPathException
123 * if a dynamic error occurs during the evaluation of the expression
124 */
125 public IntegerValue call(XPathContext context, Sequence[] arguments) throws XPathException {
126 return evaluateItem(context);
127 }
128
129
130 public static class Position extends PositionAndLast {
131 public Int64Value evaluateItem(XPathContext c) throws XPathException {
132 FocusIterator currentIterator = c.getCurrentIterator();
133 if (currentIterator == null) {
134 XPathException e = new XPathException("The context item is absent, so position() is undefined");
135 e.setXPathContext(c);
136 e.setErrorCode("XPDY0002");
137 throw e;
138 }
139 return Int64Value.makeIntegerValue(currentIterator.position());
140 }
141
142 public String getCompilerName() {
143 return "PositionCompiler";
144 }
145
146 }
147
148 public static class Last extends PositionAndLast {
149 public Int64Value evaluateItem(XPathContext c) throws XPathException {
150 return Int64Value.makeIntegerValue(c.getLast());
151 }
152
153 public String getCompilerName() {
154 return "LastCompiler";
155 }
156
157 public String getStreamerName() {
158 return "Last";
159 }
160 }
161 }
162