This class performs optimizations that vary between different versions of the Saxon product.
The optimizer is obtained from the Saxon Configuration. This class is the version used in Saxon-B,
which in most cases does no optimization at all: the methods are provided so that they can be
overridden in Saxon-SA.
convertPathExpressionToKey
public Expression convertPathExpressionToKey(PathExpression pathExp,
ExpressionVisitor visitor)
throws XPathException
Examine a path expression to see whether it can be replaced by a call on the key() function;
if so, generate an appropriate key definition and return the call on key(). If not, return null.
pathExp
- The path expression to be converted.visitor
- The expression visitor
- the optimized expression, or null if no optimization is possible
convertToFilterExpression
public FilterExpression convertToFilterExpression(PathExpression pathExp,
TypeHierarchy th)
throws XPathException
Convert a path expression such as a/b/c[predicate] into a filter expression
of the form (a/b/c)[predicate]. This is possible whenever the predicate is non-positional.
The conversion is useful in the case where the path expression appears inside a loop,
where the predicate depends on the loop variable but a/b/c does not.
pathExp
- the path expression to be convertedth
- the type hierarchy cache
- the resulting filterexpression if conversion is possible, or null if not
extractGlobalVariables
public Expression extractGlobalVariables(Expression body,
ExpressionVisitor visitor)
throws XPathException
Extract subexpressions from the body of a function that can be evaluated
as global variables
body
- the body of the function
- a reference to the new global variable if a variable has been created, or null if not
getConfiguration
public Configuration getConfiguration()
Get the Saxon configuration object
isIndexableFilter
public int isIndexableFilter(Expression filter)
Test whether a filter predicate is indexable.
filter
- the predicate expression
- 0 if not indexable; +1 if the predicate is in the form expression=value; -1 if it is in
the form value=expression
makeClosure
public Value makeClosure(Expression expression,
int ref,
XPathContext context)
throws XPathException
Make a Closure, given the expected reference count
expression
- the expression to be evaluatedref
- the (nominal) number of times the value of the expression is requiredcontext
- the XPath dynamic evaluation context
makeConditionalDocumentSorter
public Expression makeConditionalDocumentSorter(DocumentSorter sorter,
PathExpression path)
Make a conditional document sorter. This optimization is attempted
when a DocumentSorter is wrapped around a path expression
sorter
- the document sorterpath
- the path expression
- the original sorter unchanged when no optimization is possible, which is always the
case in Saxon-B
makeGeneralComparison
public BinaryExpression makeGeneralComparison(Expression p0,
int op,
Expression p1,
boolean backwardsCompatible)
Create a GeneralComparison expression
p0
- the first operandop
- the operatorp1
- the second operandbackwardsCompatible
- true if XPath 1.0 backwards compatibility is in force
- the constructed expression
makeSequenceExtent
public ValueRepresentation makeSequenceExtent(Expression expression,
int ref,
XPathContext context)
throws XPathException
Make a SequenceExtent, given the expected reference count
expression
- the expression to be evaluatedref
- the (nominal) number of times the value of the expression is requiredcontext
- the XPath dynamic evaluation context
optimizeCopy
public Expression optimizeCopy(Expression select)
throws XPathException
Attempt to optimize a copy operation. Return null if no optimization is possible.
select
- the expression that selects the items to be copied
- null if no optimization is possible, or an expression that does an optimized
copy of these items otherwise
promoteExpressionsToGlobal
public Expression promoteExpressionsToGlobal(Expression body,
ExpressionVisitor visitor)
throws XPathException
Identify expressions within a function or template body that can be promoted to be
evaluated as global variables.
body
- the body of the template or functionvisitor
- the expression visitor
- the expression after subexpressions have been promoted to global variables
trace
public void trace(String message)
Trace optimization actions
message
- the message to be displayed
trace
public void trace(String message,
Expression exp)
Trace optimization actions
message
- the message to be displayedexp
- the expression after being rewritten
tryIndexedFilter
public Expression tryIndexedFilter(FilterExpression f,
ExpressionVisitor visitor,
boolean indexFirstOperand)
Try converting a filter expression to a call on the key function. Return the supplied
expression unchanged if not possible
f
- the filter expression to be convertedvisitor
- the expression visitor, which must be currently visiting the filter expression findexFirstOperand
- true if the first operand of the filter comparison is to be indexed;
false if it is the second operand
- the optimized expression, or the unchanged expression f if no optimization is possible
tryInlineFunctionCall
public Expression tryInlineFunctionCall(UserFunctionCall functionCall,
ExpressionVisitor visitor,
ItemType contextItemType)
Replace a function call by the body of the function, assuming all conditions for inlining
the function are satisfied
functionCall
- the functionCall expressionvisitor
- the expression visitorcontextItemType
- the context item type
- either the original expression unchanged, or an expression that consists of the inlined
function body, with all function parameters bound as required. In Saxon-B, function inlining is
not supported, so the original functionCall is always returned unchanged
trySwitch
public Expression trySwitch(Choose choose,
StaticContext env)
Try to convert a Choose expression into a switch
choose
- the Choose expressionenv
- the static context
- the result of optimizing this (the original expression if no optimization was possible)