| 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.flwor; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.*; |
| 12 | import net.sf.saxon.expr.parser.*; |
| 13 | import net.sf.saxon.trace.ExpressionPresenter; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | import java.util.List; |
| 17 | |
| 18 | /** |
| 19 | * A "Clause" refers specifically to one of the clauses of a FLWOR expression, for example the "for" |
| 20 | * clause, the "let" clause, the "where" or "order by" clause. (The "return" clause, however, is not |
| 21 | * modelled as a Clause). |
| 22 | */ |
| 23 | public abstract class Clause { |
| 24 | |
| 25 | public static final int FOR = 0; |
| 26 | public static final int LET = 1; |
| 27 | public static final int WINDOW = 2; |
| 28 | public static final int GROUPBYCLAUSE = 3; |
| 29 | public static final int COUNT = 4; |
| 30 | public static final int ORDERBYCLAUSE = 5; |
| 31 | public static final int WHERE = 6; |
| 32 | public static final int TRACE = 7; |
| 33 | |
| 34 | private Location location; |
| 35 | private PackageData packageData; |
| 36 | private boolean repeated; |
| 37 | |
| 38 | /** |
| 39 | * Get the location, which can be used to determine |
| 40 | * the system ID and line number of the clause |
| 41 | * |
| 42 | * @return the location |
| 43 | */ |
| 44 | public Location getLocation() { |
| 45 | return location; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set the location, which can be used to determine |
| 50 | * the system ID and line number of the clause |
| 51 | * |
| 52 | * @param locationId the location |
| 53 | */ |
| 54 | |
| 55 | public void setLocation(Location locationId) { |
| 56 | this.location = locationId; |
| 57 | } |
| 58 | |
| 59 | public void setPackageData(PackageData pd) { |
| 60 | this.packageData = pd; |
| 61 | } |
| 62 | |
| 63 | public PackageData getPackageData() { |
| 64 | return packageData; |
| 65 | } |
| 66 | |
| 67 | public Configuration getConfiguration() { |
| 68 | return packageData.getConfiguration(); |
| 69 | } |
| 70 | |
| 71 | public void setRepeated(boolean repeated) { |
| 72 | this.repeated = repeated; |
| 73 | } |
| 74 | |
| 75 | public boolean isRepeated() { |
| 76 | return repeated; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Create a copy of this clause |
| 81 | * |
| 82 | * @param flwor the new FLWORExpression to contain the copied clause |
| 83 | * @param rebindings |
| 84 | * @return the copied clause |
| 85 | */ |
| 86 | |
| 87 | public abstract Clause copy(FLWORExpression flwor, RebindingMap rebindings); |
| 88 | |
| 89 | /** |
| 90 | * Optimize any expressions contained within this clause |
| 91 | * |
| 92 | * @param visitor the ExpressionVisitor, providing access to static context information |
| 93 | * @param contextItemType the type of the context item |
| 94 | * @throws XPathException if any error is detected |
| 95 | */ |
| 96 | public void optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Type-check any expression contained within this clause |
| 101 | * |
| 102 | * |
| 103 | * @param visitor the ExpressionVisitor, providing access to static context information |
| 104 | * @param contextInfo static information about the dynamic context |
| 105 | * @throws XPathException if any error is detected |
| 106 | */ |
| 107 | |
| 108 | public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get a pull-mode tuple stream that implements the functionality of this clause, taking its |
| 113 | * input from another tuple stream which this clause modifies |
| 114 | * |
| 115 | * @param base the input tuple stream |
| 116 | * @param context the dynamic evaluation context |
| 117 | * @return the output tuple stream |
| 118 | */ |
| 119 | |
| 120 | public abstract TuplePull getPullStream(TuplePull base, XPathContext context); |
| 121 | |
| 122 | /** |
| 123 | * Get a push-mode tuple stream that implements the functionality of this clause, supplying its |
| 124 | * output to another tuple stream |
| 125 | * |
| 126 | * @param destination the output tuple stream |
| 127 | * @param context the dynamic evaluation context |
| 128 | * @return the push tuple stream that implements the functionality of this clause of the FLWOR |
| 129 | * expression |
| 130 | */ |
| 131 | |
| 132 | public abstract TuplePush getPushStream(TuplePush destination, XPathContext context); |
| 133 | |
| 134 | /** |
| 135 | * Process the subexpressions of this clause |
| 136 | * |
| 137 | * @param processor the expression processor used to process the subexpressions |
| 138 | * @throws XPathException if any error is detected |
| 139 | */ |
| 140 | |
| 141 | public abstract void processOperands(OperandProcessor processor) throws XPathException; |
| 142 | |
| 143 | /** |
| 144 | * Diagnostic print of expression structure. The abstract expression tree |
| 145 | * is written to the supplied output destination. |
| 146 | * |
| 147 | * @param out the expression presenter used to display the structure |
| 148 | */ |
| 149 | |
| 150 | public abstract void explain(ExpressionPresenter out) throws XPathException; |
| 151 | |
| 152 | /** |
| 153 | * Get the variables bound by this clause |
| 154 | * |
| 155 | * @return the variable bindings |
| 156 | */ |
| 157 | |
| 158 | public LocalVariableBinding[] getRangeVariables() { |
| 159 | return new LocalVariableBinding[0]; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Build a list of all references to a variables declared in this clause |
| 164 | * |
| 165 | * @param visitor the expression visitor |
| 166 | * @param binding a variable declared in this clause |
| 167 | * @param refs the list of variable references, initially empty, to which the method will append |
| 168 | */ |
| 169 | |
| 170 | public void gatherVariableReferences(final ExpressionVisitor visitor, Binding binding, List<VariableReference> refs) { |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Determine whether the clause contains a reference to a local variable binding that cannot be inlined |
| 175 | * |
| 176 | * @param binding the binding for the local variable in question |
| 177 | * @return true if this clause uses the variable in a way that does not permit inlining |
| 178 | */ |
| 179 | |
| 180 | public boolean containsNonInlineableVariableReference(Binding binding) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Supply improved type information to the expressions that contain references to the variables declared in this clause |
| 186 | * |
| 187 | * @param visitor the expression visitor |
| 188 | * @param references the list of variable references |
| 189 | * @param returnExpr the expression in the return clause |
| 190 | */ |
| 191 | |
| 192 | public void refineVariableType(final ExpressionVisitor visitor, List<VariableReference> references, Expression returnExpr) { |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Collect information about the navigation paths followed by this clause, for document projection purposes |
| 197 | * |
| 198 | * @param pathMap the path map in which the data is to be collected |
| 199 | * @param pathMapNodeSet the path map node set representing the paths to the context item |
| 200 | */ |
| 201 | |
| 202 | public abstract void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet); |
| 203 | |
| 204 | /** |
| 205 | * Get a keyword identifying what kind of clause this is |
| 206 | * |
| 207 | * @return the kind of clause |
| 208 | */ |
| 209 | |
| 210 | public abstract int getClauseKey(); |
| 211 | |
| 212 | /** |
| 213 | * Get a short string representation of the clause |
| 214 | * @return a recognizable string |
| 215 | */ |
| 216 | |
| 217 | public String toShortString() { |
| 218 | return toString(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 |