| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.instruct.AnalyzeString; |
| 12 |
import net.sf.saxon.expr.oper.OperandArray; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.functions.*; |
| 15 |
import net.sf.saxon.functions.Error; |
| 16 |
import net.sf.saxon.functions.registry.BuiltInFunctionSet; |
| 17 |
import net.sf.saxon.lib.NamespaceConstant; |
| 18 |
import net.sf.saxon.ma.map.MapFunctionSet; |
| 19 |
import net.sf.saxon.om.Sequence; |
| 20 |
import net.sf.saxon.pattern.NodeSetPattern; |
| 21 |
import net.sf.saxon.pattern.Pattern; |
| 22 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 23 |
import net.sf.saxon.trans.XPathException; |
| 24 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 25 |
import net.sf.saxon.type.ItemType; |
| 26 |
import net.sf.saxon.type.TypeHierarchy; |
| 27 |
import net.sf.saxon.value.IntegerValue; |
| 28 |
|
| 29 |
import java.util.Arrays; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class SystemFunctionCall extends StaticFunctionCall implements Negatable { |
| 36 |
|
| 37 |
public Evaluator[] argumentEvaluators; |
| 38 |
|
| 39 |
public SystemFunctionCall(SystemFunction target, Expression[] arguments) { |
| 40 |
super(target, arguments); |
| 41 |
argumentEvaluators = new Evaluator[arguments.length]; |
| 42 |
Arrays.fill(argumentEvaluators, Evaluator.LAZY_SEQUENCE); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
@Override |
| 51 |
public void setRetainedStaticContext(RetainedStaticContext rsc) { |
| 52 |
super.setRetainedStaticContext(rsc); |
| 53 |
getTargetFunction().setRetainedStaticContext(rsc); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public Expression preEvaluate(ExpressionVisitor visitor) throws XPathException { |
| 68 |
SystemFunction target = getTargetFunction(); |
| 69 |
if ((target.getDetails().properties & BuiltInFunctionSet.LATE) == 0) { |
| 70 |
return super.preEvaluate(visitor); |
| 71 |
} else { |
| 72 |
|
| 73 |
return this; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@Override |
| 86 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 87 |
typeCheckChildren(visitor, contextInfo); |
| 88 |
checkFunctionCall(getTargetFunction(), visitor); |
| 89 |
|
| 90 |
getTargetFunction().supplyTypeInformation(visitor, contextInfo, getArguments()); |
| 91 |
if ((getTargetFunction().getDetails().properties & BuiltInFunctionSet.LATE) == 0) { |
| 92 |
return preEvaluateIfConstant(visitor); |
| 93 |
} |
| 94 |
allocateArgumentEvaluators(getArguments()); |
| 95 |
return this; |
| 96 |
} |
| 97 |
|
| 98 |
private void allocateArgumentEvaluators(Expression[] arguments) { |
| 99 |
for (int i = 0; i < arguments.length; i++) { |
| 100 |
Expression arg = arguments[i]; |
| 101 |
int cardinality = isCallOn(Concat.class) ? |
| 102 |
StaticProperty.ALLOWS_ZERO_OR_ONE : |
| 103 |
getTargetFunction().getDetails().argumentTypes[i].getCardinality(); |
| 104 |
if (arg instanceof Literal) { |
| 105 |
argumentEvaluators[i] = Evaluator.LITERAL; |
| 106 |
} else if (arg instanceof VariableReference) { |
| 107 |
argumentEvaluators[i] = Evaluator.VARIABLE; |
| 108 |
} else if (cardinality == StaticProperty.EXACTLY_ONE) { |
| 109 |
argumentEvaluators[i] = Evaluator.SINGLE_ITEM; |
| 110 |
} else if (cardinality == StaticProperty.ALLOWS_ZERO_OR_ONE) { |
| 111 |
argumentEvaluators[i] = Evaluator.OPTIONAL_ITEM; |
| 112 |
} else { |
| 113 |
argumentEvaluators[i] = Evaluator.LAZY_SEQUENCE; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public SystemFunction getTargetFunction() { |
| 120 |
return (SystemFunction) super.getTargetFunction(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@Override |
| 133 |
public int getIntrinsicDependencies() { |
| 134 |
int properties = getTargetFunction().getDetails().properties; |
| 135 |
int dep = 0; |
| 136 |
if ((properties & BuiltInFunctionSet.LATE) != 0) { |
| 137 |
dep = StaticProperty.DEPENDS_ON_RUNTIME_ENVIRONMENT; |
| 138 |
} |
| 139 |
if ((properties & BuiltInFunctionSet.FOCUS) != 0) { |
| 140 |
if ((properties & BuiltInFunctionSet.CDOC) != 0) { |
| 141 |
dep |= StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT; |
| 142 |
} |
| 143 |
if ((properties & BuiltInFunctionSet.CITEM) != 0) { |
| 144 |
dep |= StaticProperty.DEPENDS_ON_CONTEXT_ITEM; |
| 145 |
} |
| 146 |
if ((properties & BuiltInFunctionSet.POSN) != 0) { |
| 147 |
dep |= StaticProperty.DEPENDS_ON_POSITION; |
| 148 |
} |
| 149 |
if ((properties & BuiltInFunctionSet.LAST) != 0) { |
| 150 |
dep |= StaticProperty.DEPENDS_ON_LAST; |
| 151 |
} |
| 152 |
} |
| 153 |
if ((properties & BuiltInFunctionSet.BASE) != 0) { |
| 154 |
dep |= StaticProperty.DEPENDS_ON_STATIC_CONTEXT; |
| 155 |
} |
| 156 |
if ((properties & BuiltInFunctionSet.DCOLL) != 0) { |
| 157 |
dep |= StaticProperty.DEPENDS_ON_STATIC_CONTEXT; |
| 158 |
} |
| 159 |
if (isCallOn(RegexGroup.class) || isCallOn(CurrentMergeGroup.class) || isCallOn(CurrentMergeKey.class)) { |
| 160 |
dep |= StaticProperty.DEPENDS_ON_CURRENT_GROUP; |
| 161 |
} |
| 162 |
return dep; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@Override |
| 173 |
protected int computeCardinality() { |
| 174 |
return getTargetFunction().getCardinality(getArguments()); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
@Override |
| 186 |
protected int computeSpecialProperties() { |
| 187 |
return getTargetFunction().getSpecialProperties(getArguments()); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@Override |
| 199 |
public int getNetCost() { |
| 200 |
return getTargetFunction().getNetCost(); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public Expression getScopingExpression() { |
| 205 |
if (isCallOn(RegexGroup.class)) { |
| 206 |
Expression parent = getParentExpression(); |
| 207 |
while (parent != null) { |
| 208 |
if (parent instanceof AnalyzeString) { |
| 209 |
return parent; |
| 210 |
} |
| 211 |
parent = parent.getParentExpression(); |
| 212 |
} |
| 213 |
return null; |
| 214 |
} else { |
| 215 |
return super.getScopingExpression(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Override |
| 226 |
public boolean isLiftable(boolean forStreaming) { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
return super.isLiftable(forStreaming) && |
| 233 |
!isCallOn(CurrentMergeGroup.class) && !isCallOn(CurrentMergeKey.class) && |
| 234 |
(!forStreaming || !isCallOn(MapFunctionSet.MapEntry.class)); |
| 235 |
} |
| 236 |
|
| 237 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 238 |
Expression sf = super.optimize(visitor, contextInfo); |
| 239 |
if (sf == this) { |
| 240 |
|
| 241 |
|
| 242 |
Expression sfo = getTargetFunction().makeOptimizedFunctionCall(visitor, contextInfo, getArguments()); |
| 243 |
if (sfo != null) { |
| 244 |
sfo.setParentExpression(getParentExpression()); |
| 245 |
ExpressionTool.copyLocationInfo(this, sfo); |
| 246 |
if (sfo instanceof SystemFunctionCall) { |
| 247 |
((SystemFunctionCall) sfo).allocateArgumentEvaluators(((SystemFunctionCall) sfo).getArguments()); |
| 248 |
} |
| 249 |
return sfo; |
| 250 |
} |
| 251 |
} |
| 252 |
if (sf instanceof SystemFunctionCall) { |
| 253 |
|
| 254 |
BuiltInFunctionSet.Entry details = ((SystemFunctionCall) sf).getTargetFunction().getDetails(); |
| 255 |
if ((details.properties & BuiltInFunctionSet.UO) != 0) { |
| 256 |
|
| 257 |
setArg(0, getArg(0).unordered(true, visitor.isOptimizeForStreaming())); |
| 258 |
} |
| 259 |
if (getArity() <= details.resultIfEmpty.length) { |
| 260 |
|
| 261 |
for (int i = 0; i < getArity(); i++) { |
| 262 |
if (Literal.isEmptySequence(getArg(i)) && details.resultIfEmpty[i] != null) { |
| 263 |
return Literal.makeLiteral(details.resultIfEmpty[i].materialize(), this); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
((SystemFunctionCall) sf).allocateArgumentEvaluators(((SystemFunctionCall) sf).getArguments()); |
| 268 |
} |
| 269 |
return sf; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public boolean isVacuousExpression() { |
| 274 |
return isCallOn(Error.class); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
@Override |
| 289 |
public ItemType getItemType() { |
| 290 |
return getTargetFunction().getResultItemType(getArguments()); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
@Override |
| 299 |
public Expression copy(RebindingMap rebindings) { |
| 300 |
Expression[] args = new Expression[getArity()]; |
| 301 |
for (int i = 0; i < args.length; i++) { |
| 302 |
args[i] = getArg(i).copy(rebindings); |
| 303 |
} |
| 304 |
SystemFunction target = getTargetFunction(); |
| 305 |
if (target instanceof StatefulSystemFunction) { |
| 306 |
target = ((StatefulSystemFunction) target).copy(); |
| 307 |
} |
| 308 |
return target.makeFunctionCall(args); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public IntegerValue[] getIntegerBounds() { |
| 325 |
SystemFunction fn = getTargetFunction(); |
| 326 |
if ((fn.getDetails().properties & BuiltInFunctionSet.FILTER) != 0) { |
| 327 |
return getArg(0).getIntegerBounds(); |
| 328 |
} |
| 329 |
return fn.getIntegerBounds(); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public boolean isNegatable(TypeHierarchy th) { |
| 339 |
return isCallOn(NotFn.class) || isCallOn(BooleanFn.class) || isCallOn(Empty.class) || isCallOn(Exists.class); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public Expression negate() { |
| 349 |
SystemFunction fn = getTargetFunction(); |
| 350 |
if (fn instanceof NotFn) { |
| 351 |
Expression arg = getArg(0); |
| 352 |
if (arg.getItemType() == BuiltInAtomicType.BOOLEAN && arg.getCardinality() == StaticProperty.EXACTLY_ONE) { |
| 353 |
return arg; |
| 354 |
} else { |
| 355 |
return SystemFunction.makeCall("boolean", getRetainedStaticContext(), arg); |
| 356 |
} |
| 357 |
} else if (fn instanceof BooleanFn) { |
| 358 |
return SystemFunction.makeCall("not", getRetainedStaticContext(), getArg(0)); |
| 359 |
} else if (fn instanceof Exists) { |
| 360 |
return SystemFunction.makeCall("empty", getRetainedStaticContext(), getArg(0)); |
| 361 |
} else if (fn instanceof Empty) { |
| 362 |
return SystemFunction.makeCall("exists", getRetainedStaticContext(), getArg(0)); |
| 363 |
} |
| 364 |
throw new UnsupportedOperationException(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
@Override |
| 378 |
public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException { |
| 379 |
SystemFunction fn = getTargetFunction(); |
| 380 |
if (fn instanceof Reverse) { |
| 381 |
return getArg(0); |
| 382 |
} |
| 383 |
if (fn instanceof TreatFn) { |
| 384 |
setArg(0, getArg(0).unordered(retainAllNodes, forStreaming)); |
| 385 |
} |
| 386 |
return this; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
@Override |
| 409 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 410 |
if (isCallOn(Doc.class) || isCallOn(DocumentFn.class) || isCallOn(CollectionFn.class)) { |
| 411 |
getArg(0).addToPathMap(pathMap, pathMapNodeSet); |
| 412 |
return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 413 |
} else if (isCallOn(KeyFn.class)) { |
| 414 |
return ((KeyFn) getTargetFunction()).addToPathMap(pathMap, pathMapNodeSet); |
| 415 |
} else { |
| 416 |
return super.addToPathMap(pathMap, pathMapNodeSet); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
@Override |
| 428 |
public Pattern toPattern(Configuration config) throws XPathException { |
| 429 |
SystemFunction fn = getTargetFunction(); |
| 430 |
if (fn instanceof Root_1) { |
| 431 |
if (getArg(0) instanceof ContextItemExpression || |
| 432 |
(getArg(0) instanceof ItemChecker && |
| 433 |
((ItemChecker) getArg(0)).getBaseExpression() instanceof ContextItemExpression)) { |
| 434 |
return new NodeSetPattern(this); |
| 435 |
} |
| 436 |
} |
| 437 |
return super.toPattern(config); |
| 438 |
} |
| 439 |
|
| 440 |
public Sequence[] evaluateArguments(XPathContext context) throws XPathException { |
| 441 |
OperandArray operanda = getOperanda(); |
| 442 |
int numArgs = operanda.getNumberOfOperands(); |
| 443 |
Sequence[] actualArgs = new Sequence[numArgs]; |
| 444 |
for (int i = 0; i < numArgs; i++) { |
| 445 |
Expression exp = operanda.getOperandExpression(i); |
| 446 |
actualArgs[i] = argumentEvaluators[i].evaluate(exp, context); |
| 447 |
} |
| 448 |
return actualArgs; |
| 449 |
} |
| 450 |
|
| 451 |
@Override |
| 452 |
public void resetLocalStaticProperties() { |
| 453 |
super.resetLocalStaticProperties(); |
| 454 |
if (argumentEvaluators != null) { |
| 455 |
allocateArgumentEvaluators(getArguments()); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
@Override |
| 467 |
public String getExpressionName() { |
| 468 |
return "sysFuncCall"; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
@Override |
| 478 |
public void export(ExpressionPresenter out) throws XPathException { |
| 479 |
if (getFunctionName().hasURI(NamespaceConstant.FN)) { |
| 480 |
out.startElement("fn", this); |
| 481 |
out.emitAttribute("name", getFunctionName().getLocalPart()); |
| 482 |
getTargetFunction().exportAttributes(out); |
| 483 |
for (Operand o : operands()) { |
| 484 |
o.getChildExpression().export(out); |
| 485 |
} |
| 486 |
getTargetFunction().exportAdditionalArguments(this, out); |
| 487 |
out.endElement(); |
| 488 |
} else { |
| 489 |
|
| 490 |
out.startElement("ifCall", this); |
| 491 |
out.emitAttribute("name", getFunctionName()); |
| 492 |
out.emitAttribute("type", getTargetFunction().getFunctionItemType().getResultType().toExportString()); |
| 493 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions(); |
| 494 |
if (options.target != null && options.target.equals("JS") && options.targetVersion == 2) { |
| 495 |
out.emitAttribute("typeJ", getTargetFunction().getFunctionItemType().getResultType().toExportString2()); |
| 496 |
} |
| 497 |
getTargetFunction().exportAttributes(out); |
| 498 |
for (Operand o : operands()) { |
| 499 |
o.getChildExpression().export(out); |
| 500 |
} |
| 501 |
getTargetFunction().exportAdditionalArguments(this, out); |
| 502 |
out.endElement(); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public abstract static class Optimized extends SystemFunctionCall { |
| 512 |
|
| 513 |
public Optimized(SystemFunction target, Expression[] arguments) { |
| 514 |
super(target, arguments); |
| 515 |
} |
| 516 |
@Override |
| 517 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 518 |
return this; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
} |