| 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.Controller; |
| 11 | import net.sf.saxon.expr.*; |
| 12 | import net.sf.saxon.expr.parser.Location; |
| 13 | import net.sf.saxon.expr.parser.RebindingMap; |
| 14 | import net.sf.saxon.lib.TraceListener; |
| 15 | import net.sf.saxon.om.Item; |
| 16 | import net.sf.saxon.om.NamespaceResolver; |
| 17 | import net.sf.saxon.om.SequenceIterator; |
| 18 | import net.sf.saxon.om.StructuredQName; |
| 19 | import net.sf.saxon.trace.ExpressionPresenter; |
| 20 | import net.sf.saxon.trace.InstructionInfo; |
| 21 | import net.sf.saxon.trace.LocationKind; |
| 22 | import net.sf.saxon.trans.XPathException; |
| 23 | import net.sf.saxon.type.ItemType; |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Iterator; |
| 27 | |
| 28 | /** |
| 29 | * A wrapper expression used to trace expressions in XPath and XQuery. |
| 30 | */ |
| 31 | |
| 32 | public class TraceExpression extends Instruction implements InstructionInfo { |
| 33 | |
| 34 | private Operand baseOp; |
| 35 | private StructuredQName objectName; |
| 36 | private int constructType; |
| 37 | /*@Nullable*/ private NamespaceResolver namespaceResolver = null; |
| 38 | private HashMap<String, Object> properties = new HashMap<String, Object>(10); |
| 39 | |
| 40 | /** |
| 41 | * Create a trace expression that traces execution of a given child expression |
| 42 | * |
| 43 | * @param child the expression to be traced. This will be available to the TraceListener |
| 44 | * as the value of the "expression" property of the InstructionInfo. |
| 45 | */ |
| 46 | public TraceExpression(Expression child) { |
| 47 | baseOp = new Operand(this, child, OperandRole.SAME_FOCUS_ACTION); |
| 48 | setProperty("expression", child); |
| 49 | } |
| 50 | |
| 51 | public Expression getChild() { |
| 52 | return baseOp.getChildExpression(); |
| 53 | } |
| 54 | |
| 55 | public void setChild(Expression child) { |
| 56 | baseOp.setChildExpression(child); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Iterable<Operand> operands() { |
| 61 | return baseOp; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the type of construct. This will generally be a constant |
| 66 | * in class {@link LocationKind} |
| 67 | * |
| 68 | * @param type an integer code for the type of construct being traced |
| 69 | */ |
| 70 | |
| 71 | public void setConstructType(int type) { |
| 72 | constructType = type; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the construct type. This will generally be a constant |
| 77 | * in class {@link LocationKind} |
| 78 | */ |
| 79 | public int getConstructType() { |
| 80 | return constructType; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the namespace context for the instruction being traced. This is needed if the |
| 85 | * tracelistener wants to evaluate XPath expressions in the context of the current instruction |
| 86 | * |
| 87 | * @param resolver The namespace resolver, or null if none is needed |
| 88 | */ |
| 89 | |
| 90 | public void setNamespaceResolver(/*@Nullable*/ NamespaceResolver resolver) { |
| 91 | namespaceResolver = resolver; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the namespace resolver to supply the namespace context of the instruction |
| 96 | * that is being traced |
| 97 | * |
| 98 | * @return the namespace resolver, or null if none is in use |
| 99 | */ |
| 100 | |
| 101 | /*@Nullable*/ |
| 102 | public NamespaceResolver getNamespaceResolver() { |
| 103 | return namespaceResolver; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set a name identifying the object of the expression, for example a function name, template name, |
| 108 | * variable name, key name, element name, etc. This is used only where the name is known statically. |
| 109 | * |
| 110 | * @param qName the name of the object, or null if not applicable |
| 111 | */ |
| 112 | |
| 113 | public void setObjectName(StructuredQName qName) { |
| 114 | objectName = qName; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get a name identifying the object of the expression, for example a function name, template name, |
| 119 | * variable name, key name, element name, etc. This is used only where the name is known statically. |
| 120 | * |
| 121 | * @return the name of the object, or null if not applicable |
| 122 | */ |
| 123 | |
| 124 | public StructuredQName getObjectName() { |
| 125 | return objectName; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set a named property of the instruction/expression |
| 130 | * |
| 131 | * @param name the name of the property |
| 132 | * @param value the value of the property |
| 133 | */ |
| 134 | |
| 135 | public void setProperty(String name, Object value) { |
| 136 | properties.put(name, value); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get a named property of the instruction/expression |
| 141 | * |
| 142 | * @param name the name of the property |
| 143 | * @return the value of the property |
| 144 | */ |
| 145 | |
| 146 | public Object getProperty(String name) { |
| 147 | return properties.get(name); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get an iterator over all the properties available. The values returned by the iterator |
| 152 | * will be of type String, and each string can be supplied as input to the getProperty() |
| 153 | * method to retrieve the value of the property. |
| 154 | * |
| 155 | * @return an iterator over the properties |
| 156 | */ |
| 157 | |
| 158 | public Iterator<String> getProperties() { |
| 159 | return properties.keySet().iterator(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 164 | * |
| 165 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 166 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 167 | * in explain() output displaying the expression. |
| 168 | */ |
| 169 | @Override |
| 170 | public String getExpressionName() { |
| 171 | return "trace"; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 176 | * |
| 177 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 178 | * or null if there is no such class |
| 179 | */ |
| 180 | @Override |
| 181 | public String getStreamerName() { |
| 182 | return "TraceExpr"; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the InstructionInfo details about the construct. This is to satisfy the InstructionInfoProvider |
| 187 | * interface. |
| 188 | * |
| 189 | * @return the instruction details |
| 190 | */ |
| 191 | |
| 192 | public InstructionInfo getInstructionInfo() { |
| 193 | return this; |
| 194 | } |
| 195 | |
| 196 | /*@NotNull*/ |
| 197 | public Expression copy(RebindingMap rebindings) { |
| 198 | TraceExpression t = new TraceExpression(getChild().copy(rebindings)); |
| 199 | t.setLocation(getLocation()); // Bug 3034 |
| 200 | t.objectName = objectName; |
| 201 | t.namespaceResolver = namespaceResolver; |
| 202 | t.constructType = constructType; |
| 203 | return t; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Determine whether this is an updating expression as defined in the XQuery update specification |
| 208 | * |
| 209 | * @return true if this is an updating expression |
| 210 | */ |
| 211 | |
| 212 | @Override |
| 213 | public boolean isUpdatingExpression() { |
| 214 | return getChild().isUpdatingExpression(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Determine whether this is a vacuous expression as defined in the XQuery update specification |
| 219 | * |
| 220 | * @return true if this expression is vacuous |
| 221 | */ |
| 222 | |
| 223 | @Override |
| 224 | public boolean isVacuousExpression() { |
| 225 | return getChild().isVacuousExpression(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Check to ensure that this expression does not contain any inappropriate updating subexpressions. |
| 230 | * This check is overridden for those expressions that permit updating subexpressions. |
| 231 | * |
| 232 | * @throws net.sf.saxon.trans.XPathException |
| 233 | * if the expression has a non-permitted updating subexpression |
| 234 | */ |
| 235 | |
| 236 | @Override |
| 237 | public void checkForUpdatingSubexpressions() throws XPathException { |
| 238 | getChild().checkForUpdatingSubexpressions(); |
| 239 | } |
| 240 | |
| 241 | public int getImplementationMethod() { |
| 242 | return getChild().getImplementationMethod(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Execute this instruction, with the possibility of returning tail calls if there are any. |
| 247 | * This outputs the trace information via the registered TraceListener, |
| 248 | * and invokes the instruction being traced. |
| 249 | * |
| 250 | * @param context the dynamic execution context |
| 251 | * @return either null, or a tail call that the caller must invoke on return |
| 252 | * @throws net.sf.saxon.trans.XPathException |
| 253 | * |
| 254 | */ |
| 255 | public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 256 | Controller controller = context.getController(); |
| 257 | assert controller != null; |
| 258 | TraceListener listener = controller.getTraceListener(); |
| 259 | if (controller.isTracing()) { |
| 260 | assert listener != null; |
| 261 | listener.enter(getInstructionInfo(), context); |
| 262 | } |
| 263 | // Don't attempt tail call optimization when tracing, the results are too confusing |
| 264 | getChild().process(context); |
| 265 | if (controller.isTracing()) { |
| 266 | assert listener != null; |
| 267 | listener.leave(getInstructionInfo()); |
| 268 | } |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get the item type of the items returned by evaluating this instruction |
| 274 | * |
| 275 | * @return the static item type of the instruction |
| 276 | */ |
| 277 | |
| 278 | /*@NotNull*/ |
| 279 | public ItemType getItemType() { |
| 280 | return getChild().getItemType(); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Determine the static cardinality of the expression. This establishes how many items |
| 285 | * there will be in the result of the expression, at compile time (i.e., without |
| 286 | * actually evaluating the result. |
| 287 | * |
| 288 | * @return one of the values Cardinality.ONE_OR_MORE, |
| 289 | * Cardinality.ZERO_OR_MORE, Cardinality.EXACTLY_ONE, |
| 290 | * Cardinality.ZERO_OR_ONE, Cardinality.EMPTY. This default |
| 291 | * implementation returns ZERO_OR_MORE (which effectively gives no |
| 292 | * information). |
| 293 | */ |
| 294 | |
| 295 | public int getCardinality() { |
| 296 | return getChild().getCardinality(); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Determine which aspects of the context the expression depends on. The result is |
| 301 | * a bitwise-or'ed value composed from constants such as {@link net.sf.saxon.expr.StaticProperty#DEPENDS_ON_CONTEXT_ITEM} and |
| 302 | * {@link net.sf.saxon.expr.StaticProperty#DEPENDS_ON_CURRENT_ITEM}. The default implementation combines the intrinsic |
| 303 | * dependencies of this expression with the dependencies of the subexpressions, |
| 304 | * computed recursively. This is overridden for expressions such as FilterExpression |
| 305 | * where a subexpression's dependencies are not necessarily inherited by the parent |
| 306 | * expression. |
| 307 | * |
| 308 | * @return a set of bit-significant flags identifying the dependencies of |
| 309 | * the expression |
| 310 | */ |
| 311 | |
| 312 | public int getDependencies() { |
| 313 | return getChild().getDependencies(); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Determine whether this instruction potentially creates new nodes. |
| 318 | */ |
| 319 | |
| 320 | public final boolean mayCreateNewNodes() { |
| 321 | return !getChild().hasSpecialProperty(StaticProperty.NO_NODES_NEWLY_CREATED); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Return the estimated cost of evaluating an expression. For a TraceExpression we return zero, |
| 326 | * because ideally we don't want trace expressions to affect optimization decisions. |
| 327 | * @return zero |
| 328 | */ |
| 329 | @Override |
| 330 | public int getNetCost() { |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Evaluate an expression as a single item. This always returns either a single Item or |
| 336 | * null (denoting the empty sequence). No conversion is done. This method should not be |
| 337 | * used unless the static type of the expression is a subtype of "item" or "item?": that is, |
| 338 | * it should not be called if the expression may return a sequence. There is no guarantee that |
| 339 | * this condition will be detected. |
| 340 | * |
| 341 | * @param context The context in which the expression is to be evaluated |
| 342 | * @return the node or atomic value that results from evaluating the |
| 343 | * expression; or null to indicate that the result is an empty |
| 344 | * sequence |
| 345 | * @throws net.sf.saxon.trans.XPathException |
| 346 | * if any dynamic error occurs evaluating the |
| 347 | * expression |
| 348 | */ |
| 349 | |
| 350 | public Item evaluateItem(XPathContext context) throws XPathException { |
| 351 | Controller controller = context.getController(); |
| 352 | assert controller != null; |
| 353 | if (controller.isTracing()) { |
| 354 | controller.getTraceListener().enter(getInstructionInfo(), context); |
| 355 | } |
| 356 | Item result = getChild().evaluateItem(context); |
| 357 | if (controller.isTracing()) { |
| 358 | controller.getTraceListener().leave(getInstructionInfo()); |
| 359 | } |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Return an Iterator to iterate over the values of a sequence. The value of every |
| 365 | * expression can be regarded as a sequence, so this method is supported for all |
| 366 | * expressions. This default implementation handles iteration for expressions that |
| 367 | * return singleton values: for non-singleton expressions, the subclass must |
| 368 | * provide its own implementation. |
| 369 | * |
| 370 | * @param context supplies the context for evaluation |
| 371 | * @return a SequenceIterator that can be used to iterate over the result |
| 372 | * of the expression |
| 373 | * @throws net.sf.saxon.trans.XPathException |
| 374 | * if any dynamic error occurs evaluating the |
| 375 | * expression |
| 376 | */ |
| 377 | |
| 378 | /*@NotNull*/ |
| 379 | public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 380 | Controller controller = context.getController(); |
| 381 | assert controller != null; |
| 382 | if (controller.isTracing()) { |
| 383 | controller.getTraceListener().enter(getInstructionInfo(), context); |
| 384 | } |
| 385 | SequenceIterator result = getChild().iterate(context); |
| 386 | if (controller.isTracing()) { |
| 387 | controller.getTraceListener().leave(getInstructionInfo()); |
| 388 | } |
| 389 | return result; |
| 390 | } |
| 391 | |
| 392 | public int getInstructionNameCode() { |
| 393 | if (getChild() instanceof Instruction) { |
| 394 | return ((Instruction) getChild()).getInstructionNameCode(); |
| 395 | } else { |
| 396 | return -1; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Diagnostic print of expression structure. The abstract expression tree |
| 402 | * is written to the supplied output destination. |
| 403 | */ |
| 404 | |
| 405 | public void export(ExpressionPresenter out) throws XPathException { |
| 406 | getChild().export(out); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Evaluate an updating expression, adding the results to a Pending Update List. |
| 411 | * The default implementation of this method, which is used for non-updating expressions, |
| 412 | * throws an UnsupportedOperationException |
| 413 | * |
| 414 | * @param context the XPath dynamic evaluation context |
| 415 | * @param pul the pending update list to which the results should be written |
| 416 | */ |
| 417 | |
| 418 | public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException { |
| 419 | Controller controller = context.getController(); |
| 420 | assert controller != null; |
| 421 | if (controller.isTracing()) { |
| 422 | controller.getTraceListener().enter(getInstructionInfo(), context); |
| 423 | } |
| 424 | getChild().evaluatePendingUpdates(context, pul); |
| 425 | if (controller.isTracing()) { |
| 426 | controller.getTraceListener().leave(getInstructionInfo()); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | public String getSystemId() { |
| 431 | return getLocation().getSystemId(); |
| 432 | } |
| 433 | |
| 434 | public int getColumnNumber() { |
| 435 | return getLocation().getColumnNumber(); |
| 436 | } |
| 437 | |
| 438 | public String getPublicId() { |
| 439 | return getLocation().getPublicId(); |
| 440 | } |
| 441 | |
| 442 | public int getLineNumber() { |
| 443 | return getLocation().getLineNumber(); |
| 444 | } |
| 445 | |
| 446 | public Location saveLocation() { |
| 447 | return this; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Produce a short string identifying the expression for use in error messages |
| 452 | * |
| 453 | * @return a short string, sufficient to identify the expression |
| 454 | */ |
| 455 | @Override |
| 456 | public String toShortString() { |
| 457 | return getChild().toShortString(); |
| 458 | } |
| 459 | } |
| 460 |