* @return the expression after type-checking (potentially modified to add run-time
* checks and/or conversions)
*/
public Expression typeCheck(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException {
final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
start = visitor.typeCheck(start, contextItemType);
adoptChildExpression(start);
Expression filter2 = visitor.typeCheck(filter, start.getItemType(th));
if (filter2 != filter) {
filter = filter2;
adoptChildExpression(filter2);
}
if (Literal.isConstantOne(filter)) {
return new FirstItemExpression(start);
}
if (filter instanceof Last) {
return new LastItemExpression(start);
}
// determine whether the filter always evaluates to a single boolean
filterIsSingletonBoolean =
filter.getCardinality() == StaticProperty.EXACTLY_ONE &&
filter.getItemType(th).equals(BuiltInAtomicType.BOOLEAN);
// determine whether the filter evaluates to a single number, where the number will be the same for
// all values in the sequence
filterIsIndependentNumeric =
th.isSubType(filter.getItemType(th), BuiltInAtomicType.NUMERIC) &&
(filter.getDependencies() &
(StaticProperty.DEPENDS_ON_CONTEXT_ITEM | StaticProperty.DEPENDS_ON_POSITION)) == 0 &&
!Cardinality.allowsMany(filter.getCardinality());
visitor.resetStaticProperties();
return this;