| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.*; |
| 11 |
import net.sf.saxon.om.GroundedValue; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.jiter.PairIterator; |
| 16 |
import net.sf.saxon.type.AtomicType; |
| 17 |
import net.sf.saxon.value.Cardinality; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class BinaryExpression extends Expression { |
| 28 |
|
| 29 |
private Operand lhs; |
| 30 |
private Operand rhs; |
| 31 |
protected int operator; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public BinaryExpression(Expression p0, int op, Expression p1) { |
| 42 |
operator = op; |
| 43 |
|
| 44 |
|
| 45 |
lhs = new Operand(this, p0, getOperandRole(0)); |
| 46 |
rhs = new Operand(this, p1, getOperandRole(1)); |
| 47 |
adoptChildExpression(p0); |
| 48 |
adoptChildExpression(p1); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
final public Iterable<Operand> operands() { |
| 53 |
return () -> new PairIterator<>(lhs, rhs); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected OperandRole getOperandRole(int arg) { |
| 64 |
return OperandRole.SINGLE_ATOMIC; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public Operand getLhs() { |
| 73 |
return lhs; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public final Expression getLhsExpression() { |
| 82 |
return lhs.getChildExpression(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void setLhsExpression(Expression child) { |
| 91 |
lhs.setChildExpression(child); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Operand getRhs() { |
| 100 |
return rhs; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public final Expression getRhsExpression() { |
| 109 |
return rhs.getChildExpression(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void setRhsExpression(Expression child) { |
| 118 |
rhs.setChildExpression(child); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 128 |
resetLocalStaticProperties(); |
| 129 |
lhs.typeCheck(visitor, contextInfo); |
| 130 |
rhs.typeCheck(visitor, contextInfo); |
| 131 |
|
| 132 |
|
| 133 |
try { |
| 134 |
if ((getLhsExpression() instanceof Literal) && (getRhsExpression() instanceof Literal)) { |
| 135 |
GroundedValue<?> v = evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(); |
| 136 |
return Literal.makeLiteral(v, this); |
| 137 |
} |
| 138 |
} catch (XPathException err) { |
| 139 |
|
| 140 |
|
| 141 |
} |
| 142 |
return this; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 162 |
lhs.optimize(visitor, contextItemType); |
| 163 |
rhs.optimize(visitor, contextItemType); |
| 164 |
|
| 165 |
|
| 166 |
try { |
| 167 |
if ((getLhsExpression() instanceof Literal) && (getRhsExpression() instanceof Literal)) { |
| 168 |
Item<?> item = evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext()); |
| 169 |
if (item != null) { |
| 170 |
GroundedValue<?> v = item.materialize(); |
| 171 |
return Literal.makeLiteral(v, this); |
| 172 |
} |
| 173 |
} |
| 174 |
} catch (XPathException err) { |
| 175 |
|
| 176 |
|
| 177 |
} |
| 178 |
return this; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void setFlattened(boolean flattened) { |
| 193 |
getLhsExpression().setFlattened(flattened); |
| 194 |
getRhsExpression().setFlattened(flattened); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public int getOperator() { |
| 204 |
return operator; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public int computeCardinality() { |
| 215 |
Expression lhs = getLhsExpression(); |
| 216 |
Expression rhs = getRhsExpression(); |
| 217 |
if (!Cardinality.allowsZero(lhs.getCardinality()) && |
| 218 |
lhs.getItemType() instanceof AtomicType && |
| 219 |
!Cardinality.allowsZero(rhs.getCardinality()) && |
| 220 |
rhs.getItemType() instanceof AtomicType) { |
| 221 |
return StaticProperty.EXACTLY_ONE; |
| 222 |
} else { |
| 223 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public int computeSpecialProperties() { |
| 235 |
int p = super.computeSpecialProperties(); |
| 236 |
return p | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
protected static boolean isCommutative(int operator) { |
| 247 |
return operator == Token.AND || |
| 248 |
operator == Token.OR || |
| 249 |
operator == Token.UNION || |
| 250 |
operator == Token.INTERSECT || |
| 251 |
operator == Token.PLUS || |
| 252 |
operator == Token.MULT || |
| 253 |
operator == Token.EQUALS || |
| 254 |
operator == Token.FEQ || |
| 255 |
operator == Token.NE || |
| 256 |
operator == Token.FNE; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
protected static boolean isAssociative(int operator) { |
| 267 |
return operator == Token.AND || |
| 268 |
operator == Token.OR || |
| 269 |
operator == Token.UNION || |
| 270 |
operator == Token.INTERSECT || |
| 271 |
operator == Token.PLUS || |
| 272 |
operator == Token.MULT; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
protected static boolean isInverse(int op1, int op2) { |
| 285 |
return op1 != op2 && op1 == Token.inverse(op2); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
@Override |
| 297 |
public int getImplementationMethod() { |
| 298 |
return EVALUATE_METHOD | ITERATE_METHOD; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public boolean equals(Object other) { |
| 306 |
if (other instanceof BinaryExpression && hasCompatibleStaticContext((Expression)other)) { |
| 307 |
BinaryExpression b = (BinaryExpression) other; |
| 308 |
Expression lhs1 = getLhsExpression(); |
| 309 |
Expression rhs1 = getRhsExpression(); |
| 310 |
Expression lhs2 = b.getLhsExpression(); |
| 311 |
Expression rhs2 = b.getRhsExpression(); |
| 312 |
if (operator == b.operator) { |
| 313 |
if (lhs1.isEqual(lhs2) && rhs1.isEqual(rhs2)) { |
| 314 |
return true; |
| 315 |
} |
| 316 |
if (isCommutative(operator) && lhs1.isEqual(rhs2) && rhs1.isEqual(lhs2)) { |
| 317 |
return true; |
| 318 |
} |
| 319 |
if (isAssociative(operator) && |
| 320 |
pairwiseEqual(flattenExpression(new ArrayList<>(4)), |
| 321 |
b.flattenExpression(new ArrayList<>(4)))) { |
| 322 |
return true; |
| 323 |
} |
| 324 |
} |
| 325 |
return isInverse(operator, b.operator) && lhs1.isEqual(rhs2) && rhs1.isEqual(lhs2); |
| 326 |
} |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
private List<Expression> flattenExpression(List<Expression> list) { |
| 340 |
if (getLhsExpression() instanceof BinaryExpression && |
| 341 |
((BinaryExpression) getLhsExpression()).operator == operator) { |
| 342 |
((BinaryExpression) getLhsExpression()).flattenExpression(list); |
| 343 |
} else { |
| 344 |
int h = getLhsExpression().hashCode(); |
| 345 |
list.add(getLhsExpression()); |
| 346 |
int i = list.size() - 1; |
| 347 |
while (i > 0 && h > list.get(i - 1).hashCode()) { |
| 348 |
list.set(i, list.get(i - 1)); |
| 349 |
list.set(i - 1, getLhsExpression()); |
| 350 |
i--; |
| 351 |
} |
| 352 |
} |
| 353 |
if (getRhsExpression() instanceof BinaryExpression && |
| 354 |
((BinaryExpression) getRhsExpression()).operator == operator) { |
| 355 |
((BinaryExpression) getRhsExpression()).flattenExpression(list); |
| 356 |
} else { |
| 357 |
int h = getRhsExpression().hashCode(); |
| 358 |
list.add(getRhsExpression()); |
| 359 |
int i = list.size() - 1; |
| 360 |
while (i > 0 && h > list.get(i - 1).hashCode()) { |
| 361 |
list.set(i, list.get(i - 1)); |
| 362 |
list.set(i - 1, getRhsExpression()); |
| 363 |
i--; |
| 364 |
} |
| 365 |
} |
| 366 |
return list; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
private boolean pairwiseEqual(List a, List b) { |
| 378 |
if (a.size() != b.size()) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
for (int i = 0; i < a.size(); i++) { |
| 382 |
if (!a.get(i).equals(b.get(i))) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
} |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public int computeHashCode() { |
| 395 |
|
| 396 |
|
| 397 |
int op = Math.min(operator, Token.inverse(operator)); |
| 398 |
return ("BinaryExpression " + op).hashCode() |
| 399 |
^ getLhsExpression().hashCode() |
| 400 |
^ getRhsExpression().hashCode(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public String toString() { |
| 412 |
return ExpressionTool.parenthesize(getLhsExpression()) + |
| 413 |
" " + displayOperator() + " " + |
| 414 |
ExpressionTool.parenthesize(getRhsExpression()); |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public String toShortString() { |
| 419 |
return parenthesize(getLhsExpression()) + " " + displayOperator() + " " + parenthesize(getRhsExpression()); |
| 420 |
} |
| 421 |
|
| 422 |
private String parenthesize(Expression operand) { |
| 423 |
String operandStr = operand.toShortString(); |
| 424 |
if (operand instanceof BinaryExpression && |
| 425 |
XPathParser.operatorPrecedence(((BinaryExpression) operand).operator) < XPathParser.operatorPrecedence(operator)) { |
| 426 |
operandStr = "(" + operandStr + ")"; |
| 427 |
} |
| 428 |
return operandStr; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public void export(ExpressionPresenter out) throws XPathException { |
| 439 |
out.startElement(tag(), this); |
| 440 |
out.emitAttribute("op", displayOperator()); |
| 441 |
explainExtraAttributes(out); |
| 442 |
getLhsExpression().export(out); |
| 443 |
getRhsExpression().export(out); |
| 444 |
out.endElement(); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
protected String tag() { |
| 453 |
return "operator"; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
protected void explainExtraAttributes(ExpressionPresenter out) { |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
protected String displayOperator() { |
| 473 |
return Token.tokens[operator]; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
} |
| 478 |
|