| 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.type; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.ItemChecker; |
| 12 | import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 13 | import net.sf.saxon.om.Function; |
| 14 | import net.sf.saxon.om.Item; |
| 15 | import net.sf.saxon.query.AnnotationList; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.value.SequenceType; |
| 18 | |
| 19 | /** |
| 20 | * An ItemType representing the type function(*). Subtypes represent function items with more specific |
| 21 | * type signatures. |
| 22 | * <p>Note that although this class has a singleton instance representing the type <code>function(*)</code>, |
| 23 | * there are also likely to be instances of subclasses representing more specific function types.</p> |
| 24 | */ |
| 25 | public class AnyFunctionType implements FunctionItemType { |
| 26 | |
| 27 | /*@NotNull*/ public final static AnyFunctionType ANY_FUNCTION = new AnyFunctionType(); |
| 28 | |
| 29 | /** |
| 30 | * Get the singular instance of this type (Note however that subtypes of this type |
| 31 | * may have any number of instances) |
| 32 | * |
| 33 | * @return the singular instance of this type |
| 34 | */ |
| 35 | |
| 36 | /*@NotNull*/ |
| 37 | public static AnyFunctionType getInstance() { |
| 38 | return ANY_FUNCTION; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item |
| 43 | * types. |
| 44 | * |
| 45 | * @return the smallest UType that subsumes this item type |
| 46 | */ |
| 47 | public UType getUType() { |
| 48 | return UType.FUNCTION; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Determine whether this item type is an atomic type |
| 53 | * |
| 54 | * @return true if this is ANY_ATOMIC_TYPE or a subtype thereof |
| 55 | */ |
| 56 | public boolean isAtomicType() { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Determine whether this item type is atomic (that is, whether it can ONLY match |
| 62 | * atomic values) |
| 63 | * |
| 64 | * @return true if this is ANY_ATOMIC_TYPE or a subtype thereof |
| 65 | */ |
| 66 | |
| 67 | public boolean isPlainType() { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Ask whether this function item type is a map type. In this case function coercion (to the map type) |
| 73 | * will never succeed. |
| 74 | * |
| 75 | * @return true if this FunctionItemType is a map type |
| 76 | */ |
| 77 | public boolean isMapType() { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Ask whether this function item type is an array type. In this case function coercion (to the array type) |
| 83 | * will never succeed. |
| 84 | * |
| 85 | * @return true if this FunctionItemType is an array type |
| 86 | */ |
| 87 | |
| 88 | public boolean isArrayType() { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get an alphabetic code representing the type, or at any rate, the nearest built-in type |
| 94 | * from which this type is derived. The codes are designed so that for any two built-in types |
| 95 | * A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B. |
| 96 | * |
| 97 | * @return the alphacode for the nearest containing built-in type |
| 98 | */ |
| 99 | @Override |
| 100 | public String getAlphaCode() { |
| 101 | return "F"; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the argument types of the function |
| 106 | * |
| 107 | * @return the argument types, as an array of SequenceTypes, or null if this is the generic function |
| 108 | * type function(*) |
| 109 | */ |
| 110 | /*@Nullable*/ |
| 111 | public SequenceType[] getArgumentTypes() { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the list of annotation assertions defined on this function item type. |
| 117 | * @return the list of annotation assertions, or an empty list if there are none |
| 118 | */ |
| 119 | |
| 120 | public AnnotationList getAnnotationAssertions() { |
| 121 | return AnnotationList.EMPTY; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test whether a given item conforms to this type |
| 126 | * |
| 127 | * |
| 128 | * |
| 129 | * @param item The item to be tested |
| 130 | * @param th |
| 131 | * @return true if the item is an instance of this type; false otherwise |
| 132 | */ |
| 133 | public boolean matches(Item item, TypeHierarchy th) throws XPathException { |
| 134 | return item instanceof Function; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get the primitive item type corresponding to this item type. For item(), |
| 139 | * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds, |
| 140 | * it is the value representing the node kind, for example Type.ELEMENT. |
| 141 | * For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER. |
| 142 | * For other atomic types it is the primitive type as defined in XML Schema, |
| 143 | * except that integer, xs:dayTimeDuration, and xs:yearMonthDuration |
| 144 | * are considered to be primitive types. For function items it is the singular |
| 145 | * instance FunctionItemType.getInstance(). |
| 146 | */ |
| 147 | |
| 148 | /*@NotNull*/ |
| 149 | public final ItemType getPrimitiveItemType() { |
| 150 | return ANY_FUNCTION; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the primitive type corresponding to this item type. For item(), |
| 155 | * this is Type.ITEM. For node(), it is Type.NODE. For specific node kinds, |
| 156 | * it is the value representing the node kind, for example Type.ELEMENT. |
| 157 | * For anyAtomicValue it is Type.ATOMIC_VALUE. For numeric it is Type.NUMBER. |
| 158 | * For other atomic types it is the primitive type as defined in XML Schema, |
| 159 | * except that INTEGER is considered to be a primitive type. |
| 160 | */ |
| 161 | |
| 162 | public final int getPrimitiveType() { |
| 163 | return Type.FUNCTION; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Produce a representation of this type name for use in error messages. |
| 168 | * |
| 169 | * @return a string representation of the type, in notation resembling but not necessarily |
| 170 | * identical to XPath syntax |
| 171 | */ |
| 172 | |
| 173 | public String toString() { |
| 174 | return "function(*)"; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the item type of the atomic values that will be produced when an item |
| 179 | * of this type is atomized |
| 180 | * |
| 181 | * @return the item type of the atomic values that will be produced when an item |
| 182 | * of this type is atomized |
| 183 | */ |
| 184 | |
| 185 | /*@NotNull*/ |
| 186 | public PlainType getAtomizedItemType() { |
| 187 | return null; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Ask whether values of this type are atomizable |
| 192 | * |
| 193 | * @return true if some or all instances of this type can be successfully atomized; false |
| 194 | * * if no instances of this type can be atomized |
| 195 | * @param th The type hierarchy cache |
| 196 | */ |
| 197 | |
| 198 | public boolean isAtomizable(TypeHierarchy th) { |
| 199 | return true; // arrays can be atomized |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Determine the relationship of one function item type to another |
| 204 | * |
| 205 | * @return for example {@link TypeHierarchy#SUBSUMES}, {@link TypeHierarchy#SAME_TYPE} |
| 206 | */ |
| 207 | |
| 208 | public int relationship(FunctionItemType other, TypeHierarchy th) { |
| 209 | if (other == this) { |
| 210 | return TypeHierarchy.SAME_TYPE; |
| 211 | } else { |
| 212 | return TypeHierarchy.SUBSUMES; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Create an expression whose effect is to apply function coercion to coerce a function from this type to another type |
| 218 | * |
| 219 | * @param exp the expression that delivers the supplied sequence of function items (the ones in need of coercion) |
| 220 | * @param role information for use in diagnostics |
| 221 | * @return the sequence of coerced functions, each on a function that calls the corresponding original function |
| 222 | * after checking the parameters |
| 223 | */ |
| 224 | |
| 225 | public Expression makeFunctionSequenceCoercer(Expression exp, RoleDiagnostic role) |
| 226 | throws XPathException { |
| 227 | return new ItemChecker(exp, this, role); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get the result type |
| 232 | * |
| 233 | * @return the result type |
| 234 | */ |
| 235 | |
| 236 | public SequenceType getResultType() { |
| 237 | return SequenceType.ANY_SEQUENCE; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Generate Javascript code to test whether an item conforms to this item type |
| 242 | * |
| 243 | * @return a Javascript instruction or sequence of instructions, which can be used as the body |
| 244 | * of a Javascript function, and which returns a boolean indication whether the value of the |
| 245 | * variable "item" is an instance of this item type. |
| 246 | * @throws XPathException if JS code cannot be generated for this item type, for example because |
| 247 | * the test is schema-aware. |
| 248 | * @param knownToBe |
| 249 | * @param targetVersion |
| 250 | */ |
| 251 | @Override |
| 252 | public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 253 | return "return SaxonJS.U.isMap(item) || SaxonJS.U.isArray(item);"; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Generate Javascript code to convert a supplied Javascript value to this item type, |
| 258 | * if conversion is possible, or throw an error otherwise. |
| 259 | * |
| 260 | * @param errorCode the error to be thrown if conversion is not possible |
| 261 | * @param targetVersion |
| 262 | * @return a Javascript instruction or sequence of instructions, which can be used as the body |
| 263 | * of a Javascript function, and which returns the result of conversion to this type, or throws |
| 264 | * an error if conversion is not possible. The variable "val" will hold the supplied Javascript |
| 265 | * value. |
| 266 | */ |
| 267 | public String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) throws XPathException { |
| 268 | return "if (typeof val == 'object') {return val;} else {throw SaxonJS.XError('Cannot convert supplied JS value to a map or array');}"; |
| 269 | } |
| 270 | } |
| 271 |