| 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.LocalBinding; |
| 11 | import net.sf.saxon.expr.VariableReference; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.om.Item; |
| 14 | import net.sf.saxon.om.Sequence; |
| 15 | import net.sf.saxon.om.StructuredQName; |
| 16 | import net.sf.saxon.trans.FunctionStreamability; |
| 17 | import net.sf.saxon.value.IntegerValue; |
| 18 | import net.sf.saxon.value.SequenceType; |
| 19 | |
| 20 | /** |
| 21 | * Run-time object representing a formal argument to a user-defined function |
| 22 | */ |
| 23 | public class UserFunctionParameter implements LocalBinding { |
| 24 | |
| 25 | private SequenceType requiredType; |
| 26 | private StructuredQName variableQName; |
| 27 | private int slotNumber; |
| 28 | private int referenceCount = 999; |
| 29 | // The initial value is deliberately set to indicate "many" so that it will be assumed a parameter |
| 30 | // is referenced repeatedly until proved otherwise |
| 31 | private boolean isIndexed = false; |
| 32 | private FunctionStreamability functionStreamability = FunctionStreamability.UNCLASSIFIED; |
| 33 | |
| 34 | /** |
| 35 | * Create a UserFunctionParameter |
| 36 | */ |
| 37 | |
| 38 | public UserFunctionParameter() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Indicate whether the binding is local or global. A global binding is one that has a fixed |
| 43 | * value for the life of a query or transformation; any other binding is local. |
| 44 | * |
| 45 | * @return false (always) |
| 46 | */ |
| 47 | |
| 48 | public final boolean isGlobal() { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Test whether it is permitted to assign to the variable using the saxon:assign |
| 54 | * extension element. This will only be for an XSLT global variable where the extra |
| 55 | * attribute saxon:assignable="yes" is present. |
| 56 | * |
| 57 | * @return false (always) |
| 58 | */ |
| 59 | |
| 60 | public final boolean isAssignable() { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the slot number to be used by this parameter |
| 66 | * |
| 67 | * @param slot the slot number, that is, the position of the parameter value within the local stack frame |
| 68 | */ |
| 69 | |
| 70 | public void setSlotNumber(int slot) { |
| 71 | slotNumber = slot; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * If this is a local variable held on the local stack frame, return the corresponding slot number. |
| 76 | * In other cases, return -1. |
| 77 | * |
| 78 | * @return the slot number, indicating the position of the parameter on the local stack frame |
| 79 | */ |
| 80 | |
| 81 | public int getLocalSlotNumber() { |
| 82 | return slotNumber; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Set the required type of this function parameter |
| 87 | * |
| 88 | * @param type the declared type of the parameter |
| 89 | */ |
| 90 | |
| 91 | public void setRequiredType(SequenceType type) { |
| 92 | requiredType = type; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the required type of this function parameter |
| 97 | * |
| 98 | * @return the declared type of the parameter |
| 99 | */ |
| 100 | |
| 101 | public SequenceType getRequiredType() { |
| 102 | return requiredType; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * If the variable is bound to an integer, get the minimum and maximum possible values. |
| 107 | * Return null if unknown or not applicable |
| 108 | */ |
| 109 | /*@Nullable*/ |
| 110 | public IntegerValue[] getIntegerBoundsForVariable() { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set the name of this parameter |
| 116 | * |
| 117 | * @param name the name of the parameter |
| 118 | */ |
| 119 | |
| 120 | public void setVariableQName(StructuredQName name) { |
| 121 | variableQName = name; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the name of this parameter |
| 126 | * |
| 127 | * @return the name of this parameter |
| 128 | */ |
| 129 | |
| 130 | public StructuredQName getVariableQName() { |
| 131 | return variableQName; |
| 132 | } |
| 133 | |
| 134 | public void addReference(VariableReference ref, boolean isLoopingReference) { |
| 135 | |
| 136 | } |
| 137 | |
| 138 | // /** |
| 139 | // * Set the (nominal) number of references within the function body to this parameter, where a reference |
| 140 | // * inside a loop is counted as multiple references |
| 141 | // * |
| 142 | // * @param count the nominal number of references |
| 143 | // */ |
| 144 | // |
| 145 | // public void setReferenceCount(int count) { |
| 146 | // referenceCount = count; |
| 147 | // } |
| 148 | |
| 149 | /** |
| 150 | * Get the (nominal) number of references within the function body to this parameter, where a reference |
| 151 | * inside a loop is counted as multiple references |
| 152 | * |
| 153 | * @return the nominal number of references |
| 154 | */ |
| 155 | |
| 156 | public int getReferenceCount() { |
| 157 | return referenceCount; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Indicate that this parameter requires (or does not require) support for indexing |
| 162 | * |
| 163 | * @param indexed true if support for indexing is required. This will be set if the parameter |
| 164 | * is used in a filter expression such as $param[@a = 17] |
| 165 | */ |
| 166 | |
| 167 | public void setIndexedVariable(boolean indexed) { |
| 168 | isIndexed = indexed; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void setIndexedVariable() { |
| 173 | setIndexedVariable(true); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Ask whether this parameter requires support for indexing |
| 178 | * |
| 179 | * @return true if support for indexing is required. This will be set if the parameter |
| 180 | * is used in a filter expression such as $param[@a = 17] |
| 181 | */ |
| 182 | |
| 183 | public boolean isIndexedVariable() { |
| 184 | return isIndexed; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Evaluate this function parameter |
| 189 | * |
| 190 | * @param context the XPath dynamic context |
| 191 | * @return the value of the parameter |
| 192 | */ |
| 193 | |
| 194 | public Sequence<?> evaluateVariable(XPathContext context) { |
| 195 | return context.evaluateLocalVariable(slotNumber); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * If this is the first argument of a streamable stylesheet function, |
| 200 | * set the streamability category |
| 201 | * @param ability the streamability category |
| 202 | */ |
| 203 | |
| 204 | public void setFunctionStreamability(FunctionStreamability ability) { |
| 205 | this.functionStreamability = ability; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * If this is the first argument of a streamable stylesheet function, |
| 210 | * get the streamability category; otherwise return {@link FunctionStreamability#UNCLASSIFIED} |
| 211 | * @return the streamability category |
| 212 | */ |
| 213 | |
| 214 | public FunctionStreamability getFunctionStreamability() { |
| 215 | return functionStreamability; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | } |
| 220 |