| 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.instruct; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.*; |
| 12 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 | import net.sf.saxon.expr.parser.Location; |
| 14 | import net.sf.saxon.om.Item; |
| 15 | import net.sf.saxon.om.SequenceIterator; |
| 16 | import net.sf.saxon.om.StandardNames; |
| 17 | import net.sf.saxon.trace.LocationKind; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 20 | import net.sf.saxon.tree.iter.SingletonIterator; |
| 21 | import net.sf.saxon.type.ItemType; |
| 22 | import net.sf.saxon.type.Type; |
| 23 | |
| 24 | import javax.xml.transform.SourceLocator; |
| 25 | |
| 26 | /** |
| 27 | * Abstract superclass for all instructions in the compiled stylesheet. |
| 28 | * This represents a compiled instruction, and as such, the minimum information is |
| 29 | * retained from the original stylesheet. <br> |
| 30 | * Note: this class implements SourceLocator: that is, it can identify where in the stylesheet |
| 31 | * the source instruction was located. |
| 32 | */ |
| 33 | |
| 34 | public abstract class Instruction extends Expression implements TailCallReturner { |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | */ |
| 39 | |
| 40 | public Instruction() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 45 | * This method indicates which of these methods is prefered. For instructions this is the process() method. |
| 46 | */ |
| 47 | |
| 48 | public int getImplementationMethod() { |
| 49 | return Expression.PROCESS_METHOD; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean isInstruction() { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the namecode of the instruction for use in diagnostics |
| 59 | * |
| 60 | * @return a code identifying the instruction: typically but not always |
| 61 | * the fingerprint of a name in the XSLT namespace |
| 62 | */ |
| 63 | |
| 64 | public int getInstructionNameCode() { |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 70 | * |
| 71 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 72 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 73 | * in explain() output displaying the expression. |
| 74 | */ |
| 75 | |
| 76 | public String getExpressionName() { |
| 77 | int code = getInstructionNameCode(); |
| 78 | if (code >= 0 & code < 1024) { |
| 79 | return StandardNames.getDisplayName(code); |
| 80 | } else { |
| 81 | return super.getExpressionName(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the item type of the items returned by evaluating this instruction |
| 87 | * |
| 88 | * @return the static item type of the instruction |
| 89 | */ |
| 90 | |
| 91 | /*@NotNull*/ |
| 92 | public ItemType getItemType() { |
| 93 | return Type.ITEM_TYPE; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the cardinality of the sequence returned by evaluating this instruction |
| 98 | * |
| 99 | * @return the static cardinality |
| 100 | */ |
| 101 | |
| 102 | public int computeCardinality() { |
| 103 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public abstract Iterable<Operand> operands(); |
| 108 | |
| 109 | /** |
| 110 | * ProcessLeavingTail: called to do the real work of this instruction. This method |
| 111 | * must be implemented in each subclass. The results of the instruction are written |
| 112 | * to the current Receiver, which can be obtained via the Controller. |
| 113 | * |
| 114 | * @param context The dynamic context of the transformation, giving access to the current node, |
| 115 | * the current variables, etc. |
| 116 | * @return null if the instruction has completed execution; or a TailCall indicating |
| 117 | * a function call or template call that is delegated to the caller, to be made after the stack has |
| 118 | * been unwound so as to save stack space. |
| 119 | */ |
| 120 | |
| 121 | public abstract TailCall processLeavingTail(XPathContext context) throws XPathException; |
| 122 | |
| 123 | /** |
| 124 | * Process the instruction, without returning any tail calls |
| 125 | * |
| 126 | * @param context The dynamic context, giving access to the current node, |
| 127 | * the current variables, etc. |
| 128 | */ |
| 129 | |
| 130 | public void process(XPathContext context) throws XPathException { |
| 131 | try { |
| 132 | TailCall tc = processLeavingTail(context); |
| 133 | while (tc != null) { |
| 134 | tc = tc.processLeavingTail(); |
| 135 | } |
| 136 | } catch (XPathException err) { |
| 137 | err.maybeSetFailingExpression(this); |
| 138 | err.maybeSetContext(context); |
| 139 | throw err; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get a SourceLocator identifying the location of this instruction |
| 145 | * |
| 146 | * @return the location of this instruction in the source stylesheet or query |
| 147 | */ |
| 148 | |
| 149 | public SourceLocator getSourceLocator() { |
| 150 | return getLocation(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Construct an exception with diagnostic information. Note that this method |
| 155 | * returns the exception, it does not throw it: that is up to the caller. |
| 156 | * |
| 157 | * @param loc the location of the error |
| 158 | * @param error The exception containing information about the error |
| 159 | * @param context The controller of the transformation |
| 160 | * @return an exception based on the supplied exception, but with location information |
| 161 | * added relating to this instruction |
| 162 | */ |
| 163 | |
| 164 | protected static XPathException dynamicError(Location loc, XPathException error, XPathContext context) { |
| 165 | if (error instanceof TerminationException) { |
| 166 | return error; |
| 167 | } |
| 168 | error.maybeSetLocation(loc); |
| 169 | error.maybeSetContext(context); |
| 170 | return error; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Assemble a ParameterSet. Method used by instructions that have xsl:with-param |
| 175 | * children. This method is used for the non-tunnel parameters. |
| 176 | * |
| 177 | * @param context the XPath dynamic context |
| 178 | * @param actualParams the set of with-param parameters that specify tunnel="no" |
| 179 | * @return a ParameterSet |
| 180 | * @throws net.sf.saxon.trans.XPathException |
| 181 | * if an error occurs evaluating one of the |
| 182 | * parameters |
| 183 | */ |
| 184 | |
| 185 | public static ParameterSet assembleParams(XPathContext context, |
| 186 | WithParam[] actualParams) |
| 187 | throws XPathException { |
| 188 | if (actualParams == null || actualParams.length == 0) { |
| 189 | return null; |
| 190 | } |
| 191 | ParameterSet params = new ParameterSet(actualParams.length); |
| 192 | for (WithParam actualParam : actualParams) { |
| 193 | params.put(actualParam.getVariableQName(), |
| 194 | actualParam.getSelectValue(context), |
| 195 | actualParam.isTypeChecked()); |
| 196 | } |
| 197 | return params; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Assemble a ParameterSet. Method used by instructions that have xsl:with-param |
| 202 | * children. This method is used for the tunnel parameters. |
| 203 | * |
| 204 | * @param context the XPath dynamic context |
| 205 | * @param actualParams the set of with-param parameters that specify tunnel="yes" |
| 206 | * @return a ParameterSet |
| 207 | * @throws net.sf.saxon.trans.XPathException |
| 208 | * if an error occurs evaluating one of the |
| 209 | * tunnel parameters |
| 210 | */ |
| 211 | |
| 212 | public static ParameterSet assembleTunnelParams(XPathContext context, |
| 213 | WithParam[] actualParams) |
| 214 | throws XPathException { |
| 215 | ParameterSet existingParams = context.getTunnelParameters(); |
| 216 | if (existingParams == null) { |
| 217 | return assembleParams(context, actualParams); |
| 218 | } |
| 219 | if (actualParams == null || actualParams.length == 0) { |
| 220 | return existingParams; |
| 221 | } |
| 222 | |
| 223 | ParameterSet newParams = new ParameterSet(existingParams, actualParams.length); |
| 224 | for (WithParam actualParam : actualParams) { |
| 225 | newParams.put(actualParam.getVariableQName(), |
| 226 | actualParam.getSelectValue(context), |
| 227 | false); |
| 228 | } |
| 229 | return newParams; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Get the static properties of this expression (other than its type). The result is |
| 234 | * bit-signficant. These properties are used for optimizations. In general, if |
| 235 | * property bit is set, it is true, but if it is unset, the value is unknown. |
| 236 | * |
| 237 | * @return a set of flags indicating static properties of this expression |
| 238 | */ |
| 239 | |
| 240 | public int computeSpecialProperties() { |
| 241 | int p = super.computeSpecialProperties(); |
| 242 | if (alwaysCreatesNewNodes()) { |
| 243 | p |= StaticProperty.ALL_NODES_NEWLY_CREATED; |
| 244 | } |
| 245 | if (mayCreateNewNodes()) { |
| 246 | return p; |
| 247 | } else { |
| 248 | return p | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Return the estimated cost of evaluating an expression. This is a very crude measure based |
| 254 | * on the syntactic form of the expression (we have no knowledge of data values). We take |
| 255 | * the cost of evaluating a simple scalar comparison or arithmetic expression as 1 (one), |
| 256 | * and we assume that a sequence has length 5. The resulting estimates may be used, for |
| 257 | * example, to reorder the predicates in a filter expression so cheaper predicates are |
| 258 | * evaluated first. |
| 259 | */ |
| 260 | @Override |
| 261 | public int getNetCost() { |
| 262 | return 20; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Ask whether this instruction potentially creates and returns new nodes. |
| 267 | * Returns true if some branches or dynamic conditions result in new nodes being created. |
| 268 | * This implementation returns a default value of false |
| 269 | * |
| 270 | * @return true if the instruction creates new nodes under some input conditions |
| 271 | * (or if it can't be proved that it doesn't) |
| 272 | */ |
| 273 | |
| 274 | public boolean mayCreateNewNodes() { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Ask whether it is guaranteed that every item in the result of this instruction |
| 280 | * is a newly created node. |
| 281 | * |
| 282 | * @return true if result of the instruction is always either an empty sequence or |
| 283 | * a sequence consisting entirely of newly created nodes |
| 284 | */ |
| 285 | |
| 286 | public boolean alwaysCreatesNewNodes() { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Helper method for {@link #mayCreateNewNodes} - returns true if any operand creates new nodes |
| 292 | * @return true if any operand creates new nodes |
| 293 | */ |
| 294 | |
| 295 | protected final boolean someOperandCreatesNewNodes() { |
| 296 | for (Operand o : operands()) { |
| 297 | Expression child = o.getChildExpression(); |
| 298 | int props = child.getSpecialProperties(); |
| 299 | if ((props & StaticProperty.NO_NODES_NEWLY_CREATED) == 0) { |
| 300 | return true; |
| 301 | } |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Evaluate an expression as a single item. This always returns either a single Item or |
| 309 | * null (denoting the empty sequence). No conversion is done. This method should not be |
| 310 | * used unless the static type of the expression is a subtype of "item" or "item?": that is, |
| 311 | * it should not be called if the expression may return a sequence. There is no guarantee that |
| 312 | * this condition will be detected. |
| 313 | * |
| 314 | * @param context The context in which the expression is to be evaluated |
| 315 | * @return the node or atomic value that results from evaluating the |
| 316 | * expression; or null to indicate that the result is an empty |
| 317 | * sequence |
| 318 | * @throws XPathException if any dynamic error occurs evaluating the |
| 319 | * expression |
| 320 | */ |
| 321 | |
| 322 | public Item<?> evaluateItem(XPathContext context) throws XPathException { |
| 323 | int m = getImplementationMethod(); |
| 324 | if ((m & EVALUATE_METHOD) != 0) { |
| 325 | throw new AssertionError( |
| 326 | "evaluateItem() is not implemented in the subclass " + getClass()); |
| 327 | } else if ((m & ITERATE_METHOD) != 0) { |
| 328 | return iterate(context).next(); |
| 329 | } else { |
| 330 | return ExpressionTool.getItemFromProcessMethod(this, context); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Return an Iterator to iterate over the values of a sequence. The value of every |
| 336 | * expression can be regarded as a sequence, so this method is supported for all |
| 337 | * expressions. This default implementation handles iteration for expressions that |
| 338 | * return singleton values: for non-singleton expressions, the subclass must |
| 339 | * provide its own implementation. |
| 340 | * |
| 341 | * @param context supplies the context for evaluation |
| 342 | * @return a SequenceIterator that can be used to iterate over the result |
| 343 | * of the expression |
| 344 | * @throws XPathException if any dynamic error occurs evaluating the |
| 345 | * expression |
| 346 | */ |
| 347 | |
| 348 | /*@NotNull*/ |
| 349 | public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 350 | int m = getImplementationMethod(); |
| 351 | if ((m & EVALUATE_METHOD) != 0) { |
| 352 | Item<?> item = evaluateItem(context); |
| 353 | if (item == null) { |
| 354 | return EmptyIterator.emptyIterator(); |
| 355 | } else { |
| 356 | return SingletonIterator.makeIterator(item); |
| 357 | } |
| 358 | } else if ((m & ITERATE_METHOD) != 0) { |
| 359 | throw new AssertionError("iterate() is not implemented in the subclass " + getClass()); |
| 360 | } else { |
| 361 | return ExpressionTool.getIteratorFromProcessMethod(this, context); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Evaluate an expression as a String. This function must only be called in contexts |
| 367 | * where it is known that the expression will return a single string (or where an empty sequence |
| 368 | * is to be treated as a zero-length string). Implementations should not attempt to convert |
| 369 | * the result to a string, other than converting () to "". This method is used mainly to |
| 370 | * evaluate expressions produced by compiling an attribute value template. |
| 371 | * |
| 372 | * @param context The context in which the expression is to be evaluated |
| 373 | * @return the value of the expression, evaluated in the current context. |
| 374 | * The expression must return a string or (); if the value of the |
| 375 | * expression is (), this method returns "". |
| 376 | * @throws net.sf.saxon.trans.XPathException |
| 377 | * if any dynamic error occurs evaluating the |
| 378 | * expression |
| 379 | * @throws java.lang.ClassCastException if the result type of the |
| 380 | * expression is not xs:string? |
| 381 | */ |
| 382 | |
| 383 | public final CharSequence evaluateAsString(XPathContext context) throws XPathException { |
| 384 | Item item = evaluateItem(context); |
| 385 | if (item == null) { |
| 386 | return ""; |
| 387 | } else { |
| 388 | return item.getStringValue(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Get the type of this expression for use in tracing and diagnostics |
| 394 | * |
| 395 | * @return the type of expression, as enumerated in class {@link LocationKind} |
| 396 | */ |
| 397 | |
| 398 | public int getConstructType() { |
| 399 | return getInstructionNameCode(); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Establish whether this is an XSLT instruction or an XQuery instruction |
| 404 | * (used to produce appropriate diagnostics) |
| 405 | * |
| 406 | * @return true for XSLT, false for XQuery |
| 407 | */ |
| 408 | public boolean isXSLT() { |
| 409 | return getPackageData().getHostLanguage() == Configuration.XSLT; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | } |
| 414 | |
| 415 |