| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.*; |
| 12 |
import net.sf.saxon.om.Sequence; |
| 13 |
import net.sf.saxon.om.StandardNames; |
| 14 |
import net.sf.saxon.om.StructuredQName; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.ErrorType; |
| 18 |
import net.sf.saxon.type.ItemType; |
| 19 |
import net.sf.saxon.value.IntegerValue; |
| 20 |
import net.sf.saxon.value.SequenceType; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public final class LocalParam extends Instruction implements LocalBinding { |
| 35 |
|
| 36 |
private Operand conversionOp = null; |
| 37 |
private Evaluator conversionEvaluator = null; |
| 38 |
|
| 39 |
private static final int REQUIRED = 4; |
| 40 |
private static final int TUNNEL = 8; |
| 41 |
private static final int IMPLICITLY_REQUIRED = 16; |
| 42 |
|
| 43 |
|
| 44 |
private byte properties = 0; |
| 45 |
private Operand selectOp = null; |
| 46 |
protected StructuredQName variableQName; |
| 47 |
private SequenceType requiredType; |
| 48 |
protected int slotNumber = -999; |
| 49 |
protected int referenceCount = 10; |
| 50 |
protected Evaluator evaluator = null; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public void setSelectExpression(Expression select) { |
| 61 |
if (select != null) { |
| 62 |
if (selectOp == null) { |
| 63 |
selectOp = new Operand(this, select, OperandRole.NAVIGATE); |
| 64 |
} else { |
| 65 |
selectOp.setChildExpression(select); |
| 66 |
} |
| 67 |
} else { |
| 68 |
selectOp = null; |
| 69 |
} |
| 70 |
evaluator = null; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public Expression getSelectExpression() { |
| 80 |
return selectOp == null ? null : selectOp.getChildExpression(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void setRequiredType(SequenceType required) { |
| 90 |
requiredType = required; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public SequenceType getRequiredType() { |
| 100 |
return requiredType; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public void setRequiredParam(boolean requiredParam) { |
| 110 |
if (requiredParam) { |
| 111 |
properties |= REQUIRED; |
| 112 |
} else { |
| 113 |
properties &= ~REQUIRED; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void setImplicitlyRequiredParam(boolean requiredParam) { |
| 125 |
if (requiredParam) { |
| 126 |
properties |= IMPLICITLY_REQUIRED; |
| 127 |
} else { |
| 128 |
properties &= ~IMPLICITLY_REQUIRED; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void setTunnel(boolean tunnel) { |
| 139 |
if (tunnel) { |
| 140 |
properties |= TUNNEL; |
| 141 |
} else { |
| 142 |
properties &= ~TUNNEL; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void setReferenceCount(int refCount) { |
| 153 |
referenceCount = refCount; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public int getCardinality() { |
| 181 |
return StaticProperty.EMPTY; |
| 182 |
} |
| 183 |
|
| 184 |
public boolean isAssignable() { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
public boolean isGlobal() { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public int getLocalSlotNumber() { |
| 198 |
return slotNumber; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public final boolean isRequiredParam() { |
| 208 |
return (properties & REQUIRED) != 0; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public final boolean isImplicitlyRequiredParam() { |
| 219 |
return (properties & IMPLICITLY_REQUIRED) != 0; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public final boolean isTunnelParam() { |
| 229 |
return (properties & TUNNEL) != 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 234 |
Expression e2 = super.typeCheck(visitor, contextItemType); |
| 235 |
if (e2 != this) { |
| 236 |
return e2; |
| 237 |
} |
| 238 |
checkAgainstRequiredType(visitor); |
| 239 |
return this; |
| 240 |
} |
| 241 |
|
| 242 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 243 |
Expression e2 = super.optimize(visitor, contextItemType); |
| 244 |
if (e2 != this) { |
| 245 |
return e2; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
return this; |
| 251 |
} |
| 252 |
|
| 253 |
public void computeEvaluationMode() { |
| 254 |
if (getSelectExpression() != null) { |
| 255 |
if (referenceCount == FilterExpression.FILTERED) { |
| 256 |
evaluator = Evaluator.MAKE_INDEXED_VARIABLE; |
| 257 |
} else { |
| 258 |
evaluator = ExpressionTool.lazyEvaluator(getSelectExpression(), referenceCount > 1); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public LocalParam copy(RebindingMap rebindings) { |
| 273 |
LocalParam p2 = new LocalParam(); |
| 274 |
if (conversionOp != null) { |
| 275 |
assert getConversion() != null; |
| 276 |
p2.setConversion(getConversion().copy(rebindings)); |
| 277 |
} |
| 278 |
p2.conversionEvaluator = conversionEvaluator; |
| 279 |
p2.properties = properties; |
| 280 |
if (selectOp != null) { |
| 281 |
assert getSelectExpression() != null; |
| 282 |
p2.setSelectExpression(getSelectExpression().copy(rebindings)); |
| 283 |
} |
| 284 |
p2.variableQName = variableQName; |
| 285 |
p2.requiredType = requiredType; |
| 286 |
p2.slotNumber = slotNumber; |
| 287 |
p2.referenceCount = referenceCount; |
| 288 |
p2.evaluator = evaluator; |
| 289 |
return p2; |
| 290 |
} |
| 291 |
|
| 292 |
public void addReference(VariableReference ref, boolean isLoopingReference) { |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public void checkAgainstRequiredType(ExpressionVisitor visitor) |
| 304 |
throws XPathException { |
| 305 |
|
| 306 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.VARIABLE, variableQName.getDisplayName(), 0); |
| 307 |
|
| 308 |
SequenceType r = requiredType; |
| 309 |
Expression select = getSelectExpression(); |
| 310 |
if (r != null && select != null) { |
| 311 |
|
| 312 |
select = visitor.getConfiguration().getTypeChecker(false).staticTypeCheck(select, requiredType, role, visitor); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public Sequence<?> getSelectValue(XPathContext context) throws XPathException { |
| 329 |
Expression select = getSelectExpression(); |
| 330 |
if (select == null) { |
| 331 |
throw new AssertionError("Internal error: No select expression"); |
| 332 |
|
| 333 |
} else if (select instanceof Literal) { |
| 334 |
|
| 335 |
return ((Literal)select).getValue(); |
| 336 |
} else { |
| 337 |
|
| 338 |
|
| 339 |
int savedOutputState = context.getTemporaryOutputState(); |
| 340 |
context.setTemporaryOutputState(StandardNames.XSL_WITH_PARAM); |
| 341 |
Sequence<?> result = evaluator.evaluate(select, context); |
| 342 |
context.setTemporaryOutputState(savedOutputState); |
| 343 |
return result; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public int getSlotNumber() { |
| 354 |
return slotNumber; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void setSlotNumber(int s) { |
| 364 |
slotNumber = s; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public void setVariableQName(StructuredQName s) { |
| 374 |
variableQName = s; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public StructuredQName getVariableQName() { |
| 384 |
return variableQName; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public void setConversion(Expression convertor) { |
| 398 |
if (convertor != null) { |
| 399 |
if (conversionOp == null) { |
| 400 |
conversionOp = new Operand(this, convertor, OperandRole.SINGLE_ATOMIC); |
| 401 |
} |
| 402 |
conversionEvaluator = ExpressionTool.eagerEvaluator(convertor); |
| 403 |
} else { |
| 404 |
conversionOp = null; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public Expression getConversion() { |
| 417 |
return conversionOp == null ? null : conversionOp.getChildExpression(); |
| 418 |
} |
| 419 |
|
| 420 |
public int getConversionEvaluationMode() { |
| 421 |
return conversionEvaluator.getCode(); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public int getInstructionNameCode() { |
| 430 |
return StandardNames.XSL_PARAM; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
@Override |
| 442 |
public Iterable<Operand> operands() { |
| 443 |
return operandSparseList(selectOp, conversionOp); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 456 |
int wasSupplied = context.useLocalParameter(variableQName, slotNumber, isTunnelParam()); |
| 457 |
switch (wasSupplied) { |
| 458 |
case ParameterSet.SUPPLIED_AND_CHECKED: |
| 459 |
|
| 460 |
break; |
| 461 |
|
| 462 |
case ParameterSet.SUPPLIED: |
| 463 |
|
| 464 |
|
| 465 |
if (conversionOp != null) { |
| 466 |
context.setLocalVariable(slotNumber, |
| 467 |
conversionEvaluator.evaluate(getConversion(), context)); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
} |
| 472 |
break; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
case ParameterSet.NOT_SUPPLIED: |
| 478 |
if (isRequiredParam() || isImplicitlyRequiredParam()) { |
| 479 |
String name = "$" + getVariableQName().getDisplayName(); |
| 480 |
int suppliedAsTunnel = context.useLocalParameter(variableQName, slotNumber, !isTunnelParam()); |
| 481 |
String message = "No value supplied for required parameter " + name; |
| 482 |
if (isImplicitlyRequiredParam()) { |
| 483 |
message += ". A value is required because " + |
| 484 |
"the default value is not a valid instance of the required type"; |
| 485 |
} |
| 486 |
if (suppliedAsTunnel != ParameterSet.NOT_SUPPLIED) { |
| 487 |
if (isTunnelParam()) { |
| 488 |
message += ". A non-tunnel parameter with this name was supplied, but a tunnel parameter is required"; |
| 489 |
} else { |
| 490 |
message += ". A tunnel parameter with this name was supplied, but a non-tunnel parameter is required"; |
| 491 |
} |
| 492 |
} |
| 493 |
XPathException e = new XPathException(message); |
| 494 |
e.setXPathContext(context); |
| 495 |
e.setErrorCode("XTDE0700"); |
| 496 |
throw e; |
| 497 |
} |
| 498 |
context.setLocalVariable(slotNumber, getSelectValue(context)); |
| 499 |
} |
| 500 |
return null; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public IntegerValue[] getIntegerBoundsForVariable() { |
| 509 |
return null; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public Sequence<?> evaluateVariable(XPathContext c) { |
| 517 |
return c.evaluateLocalVariable(slotNumber); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public boolean isCompatible(LocalParam other) { |
| 528 |
return getVariableQName().equals(other.getVariableQName()) && |
| 529 |
getRequiredType().equals(other.getRequiredType()) && |
| 530 |
isTunnelParam() == other.isTunnelParam(); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
@Override |
| 539 |
public boolean isLiftable(boolean forStreaming) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
@Override |
| 552 |
public boolean hasVariableBinding(Binding binding) { |
| 553 |
return this == binding; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
@Override |
| 563 |
public ItemType getItemType() { |
| 564 |
return ErrorType.getInstance(); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
@Override |
| 573 |
public int computeCardinality() { |
| 574 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
@Override |
| 585 |
public int computeSpecialProperties() { |
| 586 |
return StaticProperty.HAS_SIDE_EFFECTS; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
@Override |
| 596 |
public boolean mayCreateNewNodes() { |
| 597 |
return false; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
@Override |
| 608 |
public String getExpressionName() { |
| 609 |
return "param"; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
@Override |
| 618 |
public String toShortString() { |
| 619 |
return "$" + getVariableQName().getDisplayName(); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
public void export(ExpressionPresenter out) throws XPathException { |
| 628 |
out.startElement("param", this); |
| 629 |
out.emitAttribute("name", getVariableQName()); |
| 630 |
out.emitAttribute("slot", "" + getSlotNumber()); |
| 631 |
String flags = getFlags(); |
| 632 |
if (!flags.isEmpty()) { |
| 633 |
out.emitAttribute("flags", flags); |
| 634 |
} |
| 635 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions(); |
| 636 |
if (getRequiredType() != SequenceType.ANY_SEQUENCE) { |
| 637 |
out.emitAttribute("as", getRequiredType().toExportString()); |
| 638 |
if (options.target.equals("JS") && options.targetVersion == 2) { |
| 639 |
out.emitAttribute("asJ", getRequiredType().toExportString2()); |
| 640 |
} |
| 641 |
} |
| 642 |
if (getSelectExpression() != null) { |
| 643 |
out.setChildRole("select"); |
| 644 |
getSelectExpression().export(out); |
| 645 |
} |
| 646 |
Expression conversion = getConversion(); |
| 647 |
if (conversion != null) { |
| 648 |
out.setChildRole("conversion"); |
| 649 |
conversion.export(out); |
| 650 |
} |
| 651 |
out.endElement(); |
| 652 |
} |
| 653 |
|
| 654 |
private String getFlags() { |
| 655 |
String flags = ""; |
| 656 |
if (isTunnelParam()) { |
| 657 |
flags += "t"; |
| 658 |
} |
| 659 |
if (isRequiredParam()) { |
| 660 |
flags += "r"; |
| 661 |
} |
| 662 |
if (isImplicitlyRequiredParam()) { |
| 663 |
flags += "i"; |
| 664 |
} |
| 665 |
return flags; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
@Override |
| 672 |
public void setIndexedVariable() {} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
@Override |
| 680 |
public boolean isIndexedVariable() { |
| 681 |
return false; |
| 682 |
} |
| 683 |
} |
| 684 |
|