| 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.expr.FilterExpression; |
| 11 | import net.sf.saxon.expr.LocalBinding; |
| 12 | import net.sf.saxon.expr.VariableReference; |
| 13 | import net.sf.saxon.expr.XPathContext; |
| 14 | import net.sf.saxon.om.Item; |
| 15 | import net.sf.saxon.om.Sequence; |
| 16 | import net.sf.saxon.om.StructuredQName; |
| 17 | import net.sf.saxon.value.IntegerValue; |
| 18 | import net.sf.saxon.value.SequenceType; |
| 19 | |
| 20 | /** |
| 21 | * Represents the defining occurrence of a variable declared within a FLWOR expression, |
| 22 | * for example the $p in "for $x at $p in ...". Also used for the variables bound to the context |
| 23 | * item in a pattern that uses current() |
| 24 | */ |
| 25 | |
| 26 | public class LocalVariableBinding implements LocalBinding { |
| 27 | |
| 28 | private StructuredQName variableName; |
| 29 | private SequenceType requiredType; |
| 30 | private int slotNumber = -999; |
| 31 | private int refCount = 0; |
| 32 | |
| 33 | /** |
| 34 | * Create a LocalVariableBinding |
| 35 | * |
| 36 | * @param name the name of the variable |
| 37 | * @param type the static type of the variable |
| 38 | */ |
| 39 | |
| 40 | public LocalVariableBinding(StructuredQName name, SequenceType type) { |
| 41 | variableName = name; |
| 42 | requiredType = type; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Make a copy of this LocalVariableBinding (except for the slot number) |
| 47 | * |
| 48 | * @return a copy of the binding |
| 49 | */ |
| 50 | |
| 51 | public LocalVariableBinding copy() { |
| 52 | LocalVariableBinding lb2 = new LocalVariableBinding(variableName, requiredType); |
| 53 | lb2.slotNumber = slotNumber; |
| 54 | lb2.refCount = refCount; |
| 55 | return lb2; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the name of the variable |
| 60 | * |
| 61 | * @return the name of the variable |
| 62 | */ |
| 63 | |
| 64 | public StructuredQName getVariableQName() { |
| 65 | return variableName; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set the required or inferred type of the variable |
| 70 | * |
| 71 | * @param type the required or inferred type |
| 72 | */ |
| 73 | |
| 74 | public void setRequiredType(SequenceType type) { |
| 75 | requiredType = type; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the required type (declared type) of the variable |
| 80 | * |
| 81 | * @return the required type |
| 82 | */ |
| 83 | |
| 84 | public SequenceType getRequiredType() { |
| 85 | return requiredType; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * If the variable is bound to an integer, get the minimum and maximum possible values. |
| 90 | * Return null if unknown or not applicable |
| 91 | */ |
| 92 | public IntegerValue[] getIntegerBoundsForVariable() { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the (nominal) count of the number of references to this variable |
| 98 | * |
| 99 | * @return zero if there are no references, one if there is a single reference that is not in |
| 100 | * a loop, some higher number if there are multiple references (or a single reference in a loop), |
| 101 | * or the special value @link RangeVariable#FILTERED} if there are any references |
| 102 | * in filter expressions that require searching. |
| 103 | */ |
| 104 | |
| 105 | public int getNominalReferenceCount() { |
| 106 | return refCount; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Register a variable reference that refers to the variable bound in this expression |
| 111 | * |
| 112 | * @param ref |
| 113 | * @param isLoopingReference - true if the reference occurs within a loop, such as the predicate |
| 114 | * of a filter expression |
| 115 | */ |
| 116 | |
| 117 | public void addReference(VariableReference ref, boolean isLoopingReference) { |
| 118 | if (refCount != FilterExpression.FILTERED) { |
| 119 | refCount += (isLoopingReference ? 10 : 1); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Indicate that the variable bound by this let expression should be indexable |
| 125 | * (because it is used in an appropriate filter expression) |
| 126 | */ |
| 127 | |
| 128 | public void setIndexedVariable() { |
| 129 | refCount = FilterExpression.FILTERED; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean isIndexedVariable() { |
| 134 | return refCount == FilterExpression.FILTERED; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set the name of the variable |
| 139 | * |
| 140 | * @param variableName the name of the variable |
| 141 | */ |
| 142 | |
| 143 | public void setVariableQName(StructuredQName variableName) { |
| 144 | this.variableName = variableName; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set the slot number for the range variable |
| 149 | * |
| 150 | * @param nr the slot number to be used |
| 151 | */ |
| 152 | |
| 153 | public void setSlotNumber(int nr) { |
| 154 | slotNumber = nr; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * If this is a local variable held on the local stack frame, return the corresponding slot number. |
| 159 | * In other cases, return -1. |
| 160 | */ |
| 161 | |
| 162 | public int getLocalSlotNumber() { |
| 163 | return slotNumber; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get the value of the range variable |
| 168 | */ |
| 169 | |
| 170 | public Sequence<?> evaluateVariable(XPathContext context) { |
| 171 | return context.evaluateLocalVariable(slotNumber); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Test whether it is permitted to assign to the variable using the saxon:assign |
| 176 | * extension element. This will only be for an XSLT global variable where the extra |
| 177 | * attribute saxon:assignable="yes" is present. |
| 178 | * |
| 179 | * @return true if the binding is assignable |
| 180 | */ |
| 181 | |
| 182 | public boolean isAssignable() { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Indicate whether the binding is local or global. A global binding is one that has a fixed |
| 188 | * value for the life of a query or transformation; any other binding is local. |
| 189 | * |
| 190 | * @return true if the binding is global |
| 191 | */ |
| 192 | |
| 193 | public boolean isGlobal() { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | } |
| 199 |