net.sf.saxon.expr

Class LetExpression

Implemented Interfaces:
Binding, EvaluableItem, InstructionInfoProvider, SequenceIterable, Serializable, SourceLocator, TailCallReturner
Known Direct Subclasses:
EagerLetExpression

public class LetExpression
extends Assignation
implements TailCallReturner

A LetExpression is modelled on the XQuery syntax let $x := expr return expr. This syntax is not available in the surface XPath language, but it is used internally in an optimized expression tree.

Field Summary

Fields inherited from class net.sf.saxon.expr.Assignation

action, requiredType, sequence, slotNumber, variableName

Fields inherited from class net.sf.saxon.expr.Expression

EVALUATE_METHOD, ITERATE_METHOD, PROCESS_METHOD, locationId, staticProperties

Constructor Summary

LetExpression()
Create a LetExpression

Method Summary

void
addReference(VariableReference v)
Register a variable reference that refers to the variable bound in this let expression
void
checkPermittedContents(SchemaType parentType, StaticContext env, boolean whole)
Check that any elements and attributes constructed or returned by this expression are acceptable in the content model of a given complex type.
int
computeCardinality()
Determine the static cardinality of the expression
int
computeSpecialProperties()
Get the static properties of this expression (other than its type).
Expression
copy()
Copy an expression.
protected ValueRepresentation
eval(XPathContext context)
Evaluate the variable.
Item
evaluateItem(XPathContext context)
Evaluate the expression as a singleton
void
explain(ExpressionPresenter out)
Diagnostic print of expression structure.
ItemType
getItemType(TypeHierarchy th)
Determine the data type of the items returned by the expression, if possible
int
getNominalReferenceCount()
Get the (nominal) count of the number of references to this variable
boolean
implementsStaticTypeCheck()
Determine whether this expression implements its own method for static type checking
boolean
isIndexedVariable()
Test whether the variable bound by this let expression should be indexable
SequenceIterator
iterate(XPathContext context)
Iterate over the result of the expression to return a sequence of items
EventIterator
iterateEvents(XPathContext context)
Iterate over the result of the expression to return a sequence of events
int
markTailFunctionCalls(StructuredQName qName, int arity)
Mark tail function calls
Expression
optimize(ExpressionVisitor visitor, ItemType contextItemType)
Perform optimisation of an expression and its subexpressions.
void
process(XPathContext context)
Process this expression as an instruction, writing results to the current outputter
TailCall
processLeavingTail(XPathContext context)
ProcessLeavingTail: called to do the real work of this instruction.
Expression
promote(PromotionOffer offer)
Promote this expression if possible
void
setIndexedVariable()
Indicate that the variable bound by this let expression should be indexable (because it is used in an appropriate filter expression)
Expression
staticTypeCheck(SequenceType req, boolean backwardsCompatible, RoleLocator role, ExpressionVisitor visitor)
Static type checking for let expressions is delegated to the expression itself, and is performed on the "action" expression, to allow further delegation to the branches of a conditional
Expression
typeCheck(ExpressionVisitor visitor, ItemType contextItemType)
Type-check the expression

Methods inherited from class net.sf.saxon.expr.Assignation

addToPathMap, evaluateVariable, extendBindingList, getAction, getLocalSlotNumber, getRequiredSlots, getRequiredType, getSequence, getVariableName, getVariableQName, isAssignable, isGlobal, iterateSubExpressions, promote, promoteWhereClause, refineTypeInformation, replaceSubExpression, setAction, setRequiredType, setSequence, setSlotNumber, setVariableQName, simplify, suppressValidation

Methods inherited from class net.sf.saxon.expr.Expression

addToPathMap, adoptChildExpression, checkPermittedContents, computeCardinality, computeDependencies, computeSpecialProperties, computeStaticProperties, copy, display, doPromotion, dynamicError, effectiveBooleanValue, evaluateAsString, evaluateItem, explain, explain, findParentOf, getCardinality, getColumnNumber, getConstructType, getContainer, getDependencies, getExecutable, getHostLanguage, getImplementationMethod, getInstructionInfo, getIntrinsicDependencies, getItemType, getLineNumber, getLocationId, getLocationProvider, getPublicId, getSlotsUsed, getSpecialProperties, getSystemId, hasLoopingSubexpression, implementsStaticTypeCheck, iterate, iterateEvents, iterateSubExpressions, markTailFunctionCalls, optimize, process, promote, replaceSubExpression, resetLocalStaticProperties, setContainer, setFiltered, setFlattened, setLocationId, simplify, staticTypeCheck, suppressValidation, toString, typeCheck, typeError

Constructor Details

LetExpression

public LetExpression()
Create a LetExpression

Method Details

addReference

public void addReference(VariableReference v)
Register a variable reference that refers to the variable bound in this let expression
Parameters:
v - the variable reference

checkPermittedContents

public void checkPermittedContents(SchemaType parentType,
                                   StaticContext env,
                                   boolean whole)
            throws XPathException
Check that any elements and attributes constructed or returned by this expression are acceptable in the content model of a given complex type. It's always OK to say yes, since the check will be repeated at run-time. The process of checking element and attribute constructors against the content model of a complex type also registers the type of content expected of those constructors, so the static validation can continue recursively.
Overrides:
checkPermittedContents in interface Expression

computeCardinality

public int computeCardinality()
Determine the static cardinality of the expression
Overrides:
computeCardinality in interface Expression

computeSpecialProperties

public int computeSpecialProperties()
Get the static properties of this expression (other than its type). The result is bit-signficant. These properties are used for optimizations. In general, if property bit is set, it is true, but if it is unset, the value is unknown.
Overrides:
computeSpecialProperties in interface Expression

copy

public Expression copy()
Copy an expression. This makes a deep copy.
Overrides:
copy in interface Expression
Returns:
the copy of the original expression

eval

protected ValueRepresentation eval(XPathContext context)
            throws XPathException
Evaluate the variable.
Parameters:
context - the dynamic evaluation context
Returns:
the result of evaluating the expression that is bound to the variable

evaluateItem

public Item evaluateItem(XPathContext context)
            throws XPathException
Evaluate the expression as a singleton
Specified by:
evaluateItem in interface EvaluableItem
Overrides:
evaluateItem in interface Expression

explain

public void explain(ExpressionPresenter out)
Diagnostic print of expression structure. The abstract expression tree is written to the supplied output destination.
Overrides:
explain in interface Expression

getItemType

public ItemType getItemType(TypeHierarchy th)
Determine the data type of the items returned by the expression, if possible
Overrides:
getItemType in interface Expression
Parameters:
th - the type hierarchy cache
Returns:
one of the values Type.STRING, Type.BOOLEAN, Type.NUMBER, Type.NODE, or Type.ITEM (meaning not known in advance)

getNominalReferenceCount

public int getNominalReferenceCount()
Get the (nominal) count of the number of references to this variable
Returns:
zero if there are no references, one if there is a single reference that is not in a loop, some higher number if there are multiple references (or a single reference in a loop), or the special value @link RangeVariable#FILTERED} if there are any references in filter expressions that require searching.

implementsStaticTypeCheck

public boolean implementsStaticTypeCheck()
Determine whether this expression implements its own method for static type checking
Overrides:
implementsStaticTypeCheck in interface Expression
Returns:
true - this expression has a non-trivial implementation of the staticTypeCheck() method

isIndexedVariable

public boolean isIndexedVariable()
Test whether the variable bound by this let expression should be indexable
Returns:
true if the variable should be indexable

iterate

public SequenceIterator iterate(XPathContext context)
            throws XPathException
Iterate over the result of the expression to return a sequence of items
Specified by:
iterate in interface SequenceIterable
Overrides:
iterate in interface Expression

iterateEvents

public EventIterator iterateEvents(XPathContext context)
            throws XPathException
Iterate over the result of the expression to return a sequence of events
Overrides:
iterateEvents in interface Expression

markTailFunctionCalls

public int markTailFunctionCalls(StructuredQName qName,
                                 int arity)
Mark tail function calls
Overrides:
markTailFunctionCalls in interface Expression

optimize

public Expression optimize(ExpressionVisitor visitor,
                           ItemType contextItemType)
            throws XPathException
Perform optimisation of an expression and its subexpressions.

This method is called after all references to functions and variables have been resolved to the declaration of the function or variable, and after all type checking has been done.

Overrides:
optimize in interface Expression
Parameters:
visitor - an expression visitor
contextItemType - the static type of "." at the point where this expression is invoked. The parameter is set to null if it is known statically that the context item will be undefined. If the type of the context item is not known statically, the argument is set to Type.ITEM_TYPE
Returns:
the original expression, rewritten if appropriate to optimize execution
Throws:
XPathException - if an error is discovered during this phase (typically a type error)

process

public void process(XPathContext context)
            throws XPathException
Process this expression as an instruction, writing results to the current outputter
Overrides:
process in interface Expression

processLeavingTail

public TailCall processLeavingTail(XPathContext context)
            throws XPathException
ProcessLeavingTail: called to do the real work of this instruction. The results of the instruction are written to the current Receiver, which can be obtained via the Controller.
Specified by:
processLeavingTail in interface TailCallReturner
Parameters:
context - The dynamic context of the transformation, giving access to the current node, the current variables, etc.
Returns:
null if the instruction has completed execution; or a TailCall indicating a function call or template call that is delegated to the caller, to be made after the stack has been unwound so as to save stack space.

promote

public Expression promote(PromotionOffer offer)
            throws XPathException
Promote this expression if possible
Overrides:
promote in interface Assignation

setIndexedVariable

public void setIndexedVariable()
Indicate that the variable bound by this let expression should be indexable (because it is used in an appropriate filter expression)

staticTypeCheck

public Expression staticTypeCheck(SequenceType req,
                                  boolean backwardsCompatible,
                                  RoleLocator role,
                                  ExpressionVisitor visitor)
            throws XPathException
Static type checking for let expressions is delegated to the expression itself, and is performed on the "action" expression, to allow further delegation to the branches of a conditional
Overrides:
staticTypeCheck in interface Expression
Parameters:
req - the required type
backwardsCompatible - true if backwards compatibility mode applies
role - the role of the expression in relation to the required type
visitor - an expression visitor
Returns:
the expression after type checking (perhaps augmented with dynamic type checking code)
Throws:
XPathException - if failures occur, for example if the static type of one branch of the conditional is incompatible with the required type

typeCheck

public Expression typeCheck(ExpressionVisitor visitor,
                            ItemType contextItemType)
            throws XPathException
Type-check the expression
Overrides:
typeCheck in interface Expression