| 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.expr.*; |
| 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.om.Item; |
| 15 | import net.sf.saxon.om.SequenceIterator; |
| 16 | import net.sf.saxon.trace.ExpressionPresenter; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | |
| 19 | /** |
| 20 | * A compiled xsl:on-empty instruction. The instruction |
| 21 | * will always be a child of a ConditionalBlock. |
| 22 | */ |
| 23 | public class OnEmptyExpr extends UnaryExpression { |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Create the instruction |
| 28 | */ |
| 29 | public OnEmptyExpr(Expression base) { |
| 30 | super(base); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Ask whether this expression is an instruction. In XSLT streamability analysis this |
| 35 | * is used to distinguish constructs corresponding to XSLT instructions from other constructs. |
| 36 | * |
| 37 | * @return true if this construct originates as an XSLT instruction |
| 38 | */ |
| 39 | @Override |
| 40 | public boolean isInstruction() { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the usage (in terms of streamability analysis) of the single operand |
| 46 | * |
| 47 | * @return the operand usage |
| 48 | */ |
| 49 | @Override |
| 50 | protected OperandRole getOperandRole() { |
| 51 | return new OperandRole(0, OperandUsage.TRANSMISSION); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Determine the intrinsic dependencies of an expression, that is, those which are not derived |
| 56 | * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency |
| 57 | * on the context position, while (position()+1) does not. The default implementation |
| 58 | * of the method returns 0, indicating "no dependencies". |
| 59 | * |
| 60 | * @return a set of bit-significant flags identifying the "intrinsic" |
| 61 | * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty |
| 62 | */ |
| 63 | @Override |
| 64 | public int getIntrinsicDependencies() { |
| 65 | return StaticProperty.HAS_SIDE_EFFECTS; // suppress optimizations such as loop-lifting |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Ask whether common subexpressions found in the operands of this expression can |
| 70 | * be extracted and evaluated outside the expression itself. The result is irrelevant |
| 71 | * in the case of operands evaluated with a different focus, which will never be |
| 72 | * extracted in this way, even if they have no focus dependency. |
| 73 | * |
| 74 | * @return false for this kind of expression |
| 75 | */ |
| 76 | @Override |
| 77 | public boolean allowExtractingCommonSubexpressions() { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Type-check the expression. Default implementation for unary operators that accept |
| 83 | * any kind of operand |
| 84 | */ |
| 85 | |
| 86 | /*@NotNull*/ |
| 87 | public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 88 | getOperand().typeCheck(visitor, contextInfo); |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Perform optimisation of an expression and its subexpressions. |
| 94 | * <p>This method is called after all references to functions and variables have been resolved |
| 95 | * to the declaration of the function or variable, and after all type checking has been done.</p> |
| 96 | * |
| 97 | * @param visitor an expression visitor |
| 98 | * @param contextInfo the static type of "." at the point where this expression is invoked. |
| 99 | * The parameter is set to null if it is known statically that the context item will be undefined. |
| 100 | * If the type of the context item is not known statically, the argument is set to |
| 101 | * {@link net.sf.saxon.type.Type#ITEM_TYPE} |
| 102 | * @return the original expression, rewritten if appropriate to optimize execution |
| 103 | * @throws XPathException if an error is discovered during this phase |
| 104 | * (typically a type error) |
| 105 | */ |
| 106 | |
| 107 | /*@NotNull*/ |
| 108 | public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 109 | getOperand().optimize(visitor, contextInfo); |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /*@NotNull*/ |
| 115 | public Expression copy(RebindingMap rebindings) { |
| 116 | return new OnEmptyExpr(getBaseExpression().copy(rebindings)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 121 | * This method indicates which of these methods is provided directly. The other methods will always be available |
| 122 | * indirectly, using an implementation that relies on one of the other methods. |
| 123 | * |
| 124 | * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or |
| 125 | * {@link #PROCESS_METHOD} |
| 126 | */ |
| 127 | @Override |
| 128 | public int getImplementationMethod() { |
| 129 | return getBaseExpression().getImplementationMethod(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get a name identifying the kind of expression, in terms meaningful to a user. |
| 134 | * |
| 135 | * @return a name identifying the kind of expression, in terms meaningful to a user. |
| 136 | * The name will always be in the form of a lexical XML QName, and should match the name used |
| 137 | * in export() output displaying the expression. |
| 138 | */ |
| 139 | @Override |
| 140 | public String getExpressionName() { |
| 141 | return "onEmpty"; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Process the instruction, without returning any tail calls |
| 146 | * |
| 147 | * @param context The dynamic context, giving access to the current node, |
| 148 | * the current variables, etc. |
| 149 | * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs |
| 150 | */ |
| 151 | @Override |
| 152 | public void process(XPathContext context) throws XPathException { |
| 153 | getBaseExpression().process(context); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return an Iterator to iterate over the values of a sequence. The value of every |
| 158 | * expression can be regarded as a sequence, so this method is supported for all |
| 159 | * expressions. This default implementation handles iteration for expressions that |
| 160 | * return singleton values: for non-singleton expressions, the subclass must |
| 161 | * provide its own implementation. |
| 162 | * |
| 163 | * @param context supplies the context for evaluation |
| 164 | * @return a SequenceIterator that can be used to iterate over the result |
| 165 | * of the expression |
| 166 | * @throws net.sf.saxon.trans.XPathException if any dynamic error occurs evaluating the |
| 167 | * expression |
| 168 | */ |
| 169 | @Override |
| 170 | public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 171 | return getBaseExpression().iterate(context); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | public void export(ExpressionPresenter out) throws XPathException { |
| 177 | out.startElement("onEmpty", this); |
| 178 | getBaseExpression().export(out); |
| 179 | out.endElement(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 184 | * |
| 185 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 186 | * or null if there is no such class |
| 187 | */ |
| 188 | @Override |
| 189 | public String getStreamerName() { |
| 190 | return "OnEmpty"; |
| 191 | } |
| 192 | } |
| 193 |