| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.instruct.*; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.om.NamespaceResolver; |
| 15 |
import net.sf.saxon.om.StandardNames; |
| 16 |
import net.sf.saxon.om.StructuredQName; |
| 17 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 18 |
import net.sf.saxon.trace.InstructionInfo; |
| 19 |
import net.sf.saxon.trace.LocationKind; |
| 20 |
import net.sf.saxon.trans.FunctionStreamability; |
| 21 |
import net.sf.saxon.trans.GlobalVariableManager; |
| 22 |
import net.sf.saxon.trans.SymbolicName; |
| 23 |
import net.sf.saxon.trans.XPathException; |
| 24 |
import net.sf.saxon.tree.jiter.PairIterator; |
| 25 |
import net.sf.saxon.value.SequenceType; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class XQueryFunction implements InstructionInfo, Declaration { |
| 36 |
private StructuredQName functionName; |
| 37 |
private List<UserFunctionParameter> arguments; |
| 38 |
private SequenceType resultType; |
| 39 |
private Expression body = null; |
| 40 |
private List<UserFunctionResolvable> references = new ArrayList<>(10); |
| 41 |
private Location location; |
| 42 |
private UserFunction compiledFunction = null; |
| 43 |
private boolean memoFunction; |
| 44 |
private NamespaceResolver namespaceResolver; |
| 45 |
private QueryModule staticContext; |
| 46 |
private boolean isUpdating = false; |
| 47 |
private AnnotationList annotations = AnnotationList.EMPTY; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public XQueryFunction() { |
| 54 |
arguments = new ArrayList<>(8); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public PackageData getPackageData() { |
| 62 |
return staticContext.getPackageData(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public void setFunctionName(StructuredQName name) { |
| 72 |
functionName = name; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public void addArgument(UserFunctionParameter argument) { |
| 82 |
arguments.add(argument); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public void setResultType(SequenceType resultType) { |
| 92 |
this.resultType = resultType; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public void setBody( Expression body) { |
| 102 |
this.body = body; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public Expression getBody() { |
| 116 |
return body; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void setLocation(Location location) { |
| 126 |
this.location = location; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public StructuredQName getFunctionName() { |
| 136 |
return functionName; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public String getDisplayName() { |
| 146 |
return functionName.getDisplayName(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public SymbolicName getIdentificationKey() { |
| 158 |
return new SymbolicName.F(functionName, arguments.size()); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static SymbolicName getIdentificationKey(StructuredQName qName, int arity) { |
| 170 |
return new SymbolicName.F(qName, arity); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public SequenceType getResultType() { |
| 180 |
return resultType; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public void setStaticContext(QueryModule env) { |
| 190 |
staticContext = env; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public StaticContext getStaticContext() { |
| 200 |
return staticContext; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public SequenceType[] getArgumentTypes() { |
| 211 |
SequenceType[] types = new SequenceType[arguments.size()]; |
| 212 |
for (int i = 0; i < arguments.size(); i++) { |
| 213 |
types[i] = arguments.get(i).getRequiredType(); |
| 214 |
} |
| 215 |
return types; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public UserFunctionParameter[] getParameterDefinitions() { |
| 225 |
UserFunctionParameter[] params = new UserFunctionParameter[arguments.size()]; |
| 226 |
return arguments.toArray(params); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public int getNumberOfArguments() { |
| 236 |
return arguments.size(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public void registerReference(UserFunctionResolvable ufc) { |
| 246 |
references.add(ufc); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setMemoFunction(boolean isMemoFunction) { |
| 257 |
memoFunction = isMemoFunction; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public boolean isMemoFunction() { |
| 267 |
return memoFunction; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public void setUpdating(boolean isUpdating) { |
| 277 |
this.isUpdating = isUpdating; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public boolean isUpdating() { |
| 287 |
return isUpdating; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public void setAnnotations(AnnotationList annotations) { |
| 297 |
this.annotations = annotations; |
| 298 |
if (compiledFunction != null) { |
| 299 |
compiledFunction.setAnnotations(annotations); |
| 300 |
} |
| 301 |
if (annotations.includes(Annotation.UPDATING)) { |
| 302 |
setUpdating(true); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public AnnotationList getAnnotations() { |
| 313 |
return annotations; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public boolean hasAnnotation(StructuredQName name) { |
| 322 |
return annotations.includes(name); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public boolean isPrivate() { |
| 333 |
return hasAnnotation(Annotation.PRIVATE); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public void compile() throws XPathException { |
| 344 |
Configuration config = staticContext.getConfiguration(); |
| 345 |
try { |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (compiledFunction == null) { |
| 353 |
SlotManager map = config.makeSlotManager(); |
| 354 |
UserFunctionParameter[] params = getParameterDefinitions(); |
| 355 |
for (int i = 0; i < params.length; i++) { |
| 356 |
params[i].setSlotNumber(i); |
| 357 |
map.allocateSlotNumber(params[i].getVariableQName()); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
RetainedStaticContext rsc = null; |
| 363 |
try { |
| 364 |
rsc = getStaticContext().makeRetainedStaticContext(); |
| 365 |
body.setRetainedStaticContext(rsc); |
| 366 |
|
| 367 |
ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); |
| 368 |
body = body.simplify().typeCheck(visitor, ContextItemStaticInfo.ABSENT); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
RoleDiagnostic role = |
| 375 |
new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, functionName.getDisplayName(), 0); |
| 376 |
|
| 377 |
body = config.getTypeChecker(false).staticTypeCheck(body, resultType, role, visitor); |
| 378 |
if (staticContext.getUserQueryContext().isCompileWithTracing()) { |
| 379 |
namespaceResolver = staticContext.getNamespaceResolver(); |
| 380 |
TraceExpression trace = new TraceExpression(body); |
| 381 |
trace.setConstructType(StandardNames.XSL_FUNCTION); |
| 382 |
trace.setObjectName(functionName); |
| 383 |
trace.setNamespaceResolver(staticContext.getNamespaceResolver()); |
| 384 |
trace.setLocation(location); |
| 385 |
body = trace; |
| 386 |
} |
| 387 |
} catch (XPathException e) { |
| 388 |
e.maybeSetLocation(this); |
| 389 |
if (e.isReportableStatically()) { |
| 390 |
throw e; |
| 391 |
} else { |
| 392 |
Expression newBody = new ErrorExpression(e); |
| 393 |
ExpressionTool.copyLocationInfo(body, newBody); |
| 394 |
body = newBody; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
compiledFunction = config.newUserFunction(memoFunction, FunctionStreamability.UNCLASSIFIED); |
| 399 |
compiledFunction.setRetainedStaticContext(rsc); |
| 400 |
compiledFunction.setPackageData(staticContext.getPackageData()); |
| 401 |
compiledFunction.setBody(body); |
| 402 |
compiledFunction.setFunctionName(functionName); |
| 403 |
compiledFunction.setParameterDefinitions(params); |
| 404 |
compiledFunction.setResultType(getResultType()); |
| 405 |
compiledFunction.setLineNumber(location.getLineNumber()); |
| 406 |
compiledFunction.setSystemId(location.getSystemId()); |
| 407 |
compiledFunction.setStackFrameMap(map); |
| 408 |
compiledFunction.setUpdating(isUpdating); |
| 409 |
compiledFunction.setAnnotations(annotations); |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
fixupReferences(); |
| 415 |
|
| 416 |
} catch (XPathException e) { |
| 417 |
e.maybeSetLocation(this); |
| 418 |
throw e; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public void optimize() throws XPathException { |
| 431 |
body.checkForUpdatingSubexpressions(); |
| 432 |
if (isUpdating) { |
| 433 |
if (ExpressionTool.isNotAllowedInUpdatingContext(body)) { |
| 434 |
XPathException err = new XPathException( |
| 435 |
"The body of an updating function must be an updating expression", "XUST0002"); |
| 436 |
err.setLocator(body.getLocation()); |
| 437 |
throw err; |
| 438 |
} |
| 439 |
} else { |
| 440 |
|
| 441 |
if (body.isUpdatingExpression()) { |
| 442 |
XPathException err = new XPathException( |
| 443 |
"The body of a non-updating function must be a non-updating expression", "XUST0001"); |
| 444 |
err.setLocator(body.getLocation()); |
| 445 |
throw err; |
| 446 |
} |
| 447 |
} |
| 448 |
ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); |
| 449 |
Configuration config = staticContext.getConfiguration(); |
| 450 |
Optimizer opt = visitor.obtainOptimizer(); |
| 451 |
int arity = arguments.size(); |
| 452 |
if (opt.isOptionSet(OptimizerOptions.MISCELLANEOUS)) { |
| 453 |
body = body.optimize(visitor, ContextItemStaticInfo.ABSENT); |
| 454 |
} |
| 455 |
body.setParentExpression(null); |
| 456 |
if (opt.isOptionSet(OptimizerOptions.LOOP_LIFTING)) { |
| 457 |
body = LoopLifter.process(body, visitor, ContextItemStaticInfo.ABSENT); |
| 458 |
} |
| 459 |
if (opt.isOptionSet(OptimizerOptions.EXTRACT_GLOBALS)) { |
| 460 |
final Executable exec = ((QueryModule)getStaticContext()).getExecutable(); |
| 461 |
GlobalVariableManager manager = new GlobalVariableManager() { |
| 462 |
public void addGlobalVariable(GlobalVariable variable) throws XPathException { |
| 463 |
PackageData pd = staticContext.getPackageData(); |
| 464 |
variable.setPackageData(pd); |
| 465 |
|
| 466 |
SlotManager sm = pd.getGlobalSlotManager(); |
| 467 |
int slot = sm.allocateSlotNumber(variable.getVariableQName()); |
| 468 |
variable.compile(exec, slot); |
| 469 |
pd.addGlobalVariable(variable); |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
public GlobalVariable getEquivalentVariable(Expression select) { |
| 474 |
return null; |
| 475 |
} |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
Expression b2 = opt.promoteExpressionsToGlobal(body, manager, visitor); |
| 480 |
if (b2 != null) { |
| 481 |
body = body.optimize(visitor, ContextItemStaticInfo.ABSENT); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if (opt.getOptimizerOptions().isSet(OptimizerOptions.TAIL_CALLS) && !isUpdating) { |
| 487 |
int tailCalls = ExpressionTool.markTailFunctionCalls(body, functionName, arity); |
| 488 |
if (tailCalls != 0) { |
| 489 |
compiledFunction.setBody(body); |
| 490 |
compiledFunction.setTailRecursive(tailCalls > 0, tailCalls > 1); |
| 491 |
body = new TailCallLoop(compiledFunction, body); |
| 492 |
} |
| 493 |
} |
| 494 |
compiledFunction.setBody(body); |
| 495 |
|
| 496 |
compiledFunction.computeEvaluationMode(); |
| 497 |
ExpressionTool.allocateSlots(body, arity, compiledFunction.getStackFrameMap()); |
| 498 |
if (config.isGenerateByteCode(Configuration.XQUERY)) { |
| 499 |
if (config.getCountDown() == 0) { |
| 500 |
ICompilerService compilerService = config.makeCompilerService(Configuration.XQUERY); |
| 501 |
Expression cbody = opt.compileToByteCode(compilerService, body, getFunctionName().getDisplayName(), |
| 502 |
Expression.PROCESS_METHOD | Expression.ITERATE_METHOD); |
| 503 |
if (cbody != null) { |
| 504 |
body = cbody; |
| 505 |
} |
| 506 |
} else { |
| 507 |
opt.injectByteCodeCandidates(body); |
| 508 |
body = opt.makeByteCodeCandidate(compiledFunction, body, getDisplayName(), |
| 509 |
Expression.PROCESS_METHOD | Expression.ITERATE_METHOD); |
| 510 |
} |
| 511 |
compiledFunction.setBody(body); |
| 512 |
compiledFunction.computeEvaluationMode(); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public void fixupReferences() { |
| 521 |
for (UserFunctionResolvable ufc : references) { |
| 522 |
ufc.setFunction(compiledFunction); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public void checkReferences(ExpressionVisitor visitor) throws XPathException { |
| 533 |
for (UserFunctionResolvable ufr : references) { |
| 534 |
if (ufr instanceof UserFunctionCall) { |
| 535 |
UserFunctionCall ufc = (UserFunctionCall) ufr; |
| 536 |
ufc.checkFunctionCall(compiledFunction, visitor); |
| 537 |
|
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
references = new ArrayList<>(0); |
| 543 |
|
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public void explain( ExpressionPresenter out) throws XPathException { |
| 552 |
out.startElement("declareFunction"); |
| 553 |
out.emitAttribute("name", functionName.getDisplayName()); |
| 554 |
out.emitAttribute("arity", "" + getNumberOfArguments()); |
| 555 |
if (compiledFunction == null) { |
| 556 |
out.emitAttribute("unreferenced", "true"); |
| 557 |
} else { |
| 558 |
if (compiledFunction.isMemoFunction()) { |
| 559 |
out.emitAttribute("memo", "true"); |
| 560 |
} |
| 561 |
out.emitAttribute("tailRecursive", compiledFunction.isTailRecursive() ? "true" : "false"); |
| 562 |
body.export(out); |
| 563 |
} |
| 564 |
out.endElement(); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public UserFunction getUserFunction() { |
| 575 |
return compiledFunction; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public int getConstructType() { |
| 584 |
return StandardNames.XSL_FUNCTION; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public StructuredQName getObjectName() { |
| 593 |
return functionName; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public String getSystemId() { |
| 605 |
return location.getSystemId(); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
public int getLineNumber() { |
| 615 |
return location.getLineNumber(); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
public String getPublicId() { |
| 627 |
return null; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public int getColumnNumber() { |
| 638 |
return -1; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
public Location saveLocation() { |
| 647 |
return this; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public NamespaceResolver getNamespaceResolver() { |
| 656 |
return namespaceResolver; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
public Object getProperty(String name) { |
| 670 |
if ("name".equals(name)) { |
| 671 |
return functionName.getDisplayName(); |
| 672 |
} else if ("as".equals(name)) { |
| 673 |
return resultType.toString(); |
| 674 |
} else { |
| 675 |
return null; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
public Iterator<String> getProperties() { |
| 687 |
return new PairIterator<>("name", "as"); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public int getHostLanguage() { |
| 697 |
return Configuration.XQUERY; |
| 698 |
} |
| 699 |
|
| 700 |
} |
| 701 |
|