Package net.sf.saxon.expr.instruct
Class GlobalVariable
java.lang.Object
net.sf.saxon.expr.instruct.Actor
net.sf.saxon.expr.instruct.GlobalVariable
- All Implemented Interfaces:
SourceLocator
,Binding
,ContextOriginator
,ExpressionOwner
,Locatable
,Declaration
,Location
,Traceable
,TraceableComponent
,Locator
- Direct Known Subclasses:
GlobalParam
,UndeclaredVariable
public class GlobalVariable
extends Actor
implements Binding, Declaration, TraceableComponent, ContextOriginator
A compiled global variable in a stylesheet or query.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected GroundedValue
actuallyEvaluate
(XPathContext context, Component target) Evaluate the global variable, and save its value for use in subsequent references.void
addReference
(VariableReference ref, boolean isLoopingReference) Register a variable reference that refers to the variable bound in this expressionprotected static void
checkCircularity
(GlobalVariable var, XPathContext context) Get the variable that is immediately dependent on this one, and register the dependency, so that circularities can be detected across threads.void
compile
(Executable exec, int slot) Create a compiled representation of this global variable.int
Count the references to this variableevaluateVariable
(XPathContext context) Evaluate the variableevaluateVariable
(XPathContext context, Component target) Evaluate the variablevoid
export
(ExpressionPresenter presenter) Diagnostic print of expression structure.void
gatherProperties
(BiConsumer<String, Object> consumer) Get the properties of this object to be included in trace messages, by supplying the property values to a supplied consumer functionint
Get the slot number allocated to this variable in the BinderyGet a description of the variable for use in diagnosticsprotected String
getFlags()
Get the host language (XSLT, XQuery, XPath) used to implement the code in this containerIf the variable is bound to an integer, get the minimum and maximum possible values.int
If this is a local variable held on the local stack frame, return the corresponding slot number.Get a name identifying the object of the expression, for example a function name, template name, variable name, key name, element name, etc.Get the original declaration of this variablegetProperty
(String name) Get the value of a particular property of the instruction.Get the required type of this variablegetSelectValue
(XPathContext context, Component target) Evaluate the variable.Get the symbolic name of the componentGet a string used to identify this kind of component when used in tracing outputGet the original declaration of this variable, or its original declaration, transitivelyGet the name of the variablevoid
init
(Expression select, StructuredQName qName) Initialize the properties of the variablefinal boolean
Test whether it is permitted to assign to the variable using the saxon:assign extension element.boolean
isGlobal()
Is this a global variable?boolean
Ask whether this is an indexed variableboolean
Ask whether this global variable is privateboolean
Ask whether this variable represents a required parameterboolean
isStatic()
Ask whether this variable is declared to be staticboolean
isUnused()
Ask whether this variable is unused.Iterate over the references to this variablevoid
lookForCycles
(IndexedStack<Object> referees, XQueryFunctionLibrary globalFunctionLibrary) Check for cycles in this variable definitionvoid
Register a variable reference that refers to this global variablevoid
setAssignable
(boolean assignable) Indicate whether this variable is assignable using saxon:assignvoid
setBinderySlotNumber
(int s) Set the slot number of this variable in the Binderyvoid
The expression that initializes a global variable may itself use local variables.void
Mark this as an indexed variable, to allow fast searchingvoid
Say that this (XQuery) variable is a copy of some originally declared variable.void
setPrivate
(boolean b) Say whether this global variable is privatevoid
setRequiredParam
(boolean requiredParam) Indicate that this variable represents a required parametervoid
setRequiredType
(SequenceType required) Set the required type of this variablevoid
setStatic
(boolean declaredStatic) Say whether this variable is declared to be staticvoid
setUnused
(boolean unused) Say whether this variable is unused.void
Set the name of the variablevoid
typeCheck
(ExpressionVisitor visitor) Type check the compiled representation of this global variableMethods inherited from class net.sf.saxon.expr.instruct.Actor
allocateAllBindingSlots, allocateBindingSlotsRecursive, getBody, getChildExpression, getColumnNumber, getComponentName, getDeclaredVisibility, getDeclaringComponent, getLineNumber, getLocation, getPackageData, getPublicId, getRetainedStaticContext, getStackFrameMap, getSystemId, isExportable, makeDeclaringComponent, obtainDeclaringComponent, process, saveLocation, setBody, setChildExpression, setColumnNumber, setDeclaredVisibility, setDeclaringComponent, setLineNumber, setPackageData, setRetainedStaticContext, setStackFrameMap, setSystemId
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.sf.saxon.expr.Locatable
getLocation
Methods inherited from interface net.sf.saxon.trace.TraceableComponent
getBody, setBody
-
Field Details
-
references
-
-
Constructor Details
-
GlobalVariable
public GlobalVariable()Create a global variable
-
-
Method Details
-
init
Initialize the properties of the variable- Parameters:
select
- the expression to which the variable is boundqName
- the name of the variable
-
getSymbolicName
Get the symbolic name of the component- Specified by:
getSymbolicName
in classActor
- Returns:
- the symbolic name
-
getTracingTag
Description copied from class:Actor
Get a string used to identify this kind of component when used in tracing output- Specified by:
getTracingTag
in interfaceTraceableComponent
- Overrides:
getTracingTag
in classActor
- Returns:
- a diagnostic string used to identify the component when tracing
-
gatherProperties
Get the properties of this object to be included in trace messages, by supplying the property values to a supplied consumer function- Specified by:
gatherProperties
in interfaceTraceable
- Parameters:
consumer
- the function to which the properties should be supplied, as (property name, value) pairs.
-
setStatic
public void setStatic(boolean declaredStatic) Say whether this variable is declared to be static- Parameters:
declaredStatic
- true if the variable is declared with static="yes"
-
isStatic
public boolean isStatic()Ask whether this variable is declared to be static- Returns:
- true if the variable is declared with static="yes"
-
setRequiredType
Set the required type of this variable- Parameters:
required
- the required type
-
getRequiredType
Get the required type of this variable- Specified by:
getRequiredType
in interfaceBinding
- Returns:
- the required type
-
setOriginalVariable
Say that this (XQuery) variable is a copy of some originally declared variable. It's a separate variable when imported into another module, but it retains the link to the original.- Parameters:
var
- the variable in the imported module from which this variable is derived
-
getOriginalVariable
Get the original declaration of this variable- Returns:
- the variable in the imported module from which this variable is derived
-
getUltimateOriginalVariable
Get the original declaration of this variable, or its original declaration, transitively- Returns:
- the real variable declaration in some transitively imported module from which this variable is ultimately derived
-
setUnused
public void setUnused(boolean unused) Say whether this variable is unused. Normally, unused variables are not compiled. However, in XSLT with debugging enabled (that is, with compileWithTracing set), dummy unused variables are created in respect of global variables that are declared but never referenced. These variables are included in the list of variables known to the Executable, but they are never evaluated, and do not have slot numbers allocated in the bindery.- Parameters:
unused
- set to true if this global variable is to be marked as unused.
-
isUnused
public boolean isUnused()Ask whether this variable is unused. Normally, unused variables are not compiled. However, in XSLT with debugging enabled (that is, with compileWithTracing set), dummy unused variables are created in respect of global variables that are declared but never referenced. These variables are included in the list of variables known to the Executable, but they are never evaluated, and do not have slot numbers allocated in the bindery.- Returns:
- true if this global variable is marked as unused.
-
isPrivate
public boolean isPrivate()Ask whether this global variable is private- Returns:
- true if this variable is private
-
setPrivate
public void setPrivate(boolean b) Say whether this global variable is private- Parameters:
b
- true if this variable is external
-
setAssignable
public void setAssignable(boolean assignable) Indicate whether this variable is assignable using saxon:assign- Parameters:
assignable
- true if this variable is assignable
-
isAssignable
public final boolean isAssignable()Test whether it is permitted to assign to the variable using the saxon:assign extension element. This will only be true if the extra attribute saxon:assignable="yes" is present.- Specified by:
isAssignable
in interfaceBinding
- Returns:
- true if the binding is assignable
-
getObjectName
Get a name identifying the object of the expression, for example a function name, template name, variable name, key name, element name, etc. This is used only where the name is known statically.- Specified by:
getObjectName
in interfaceTraceable
- Returns:
- the QName of the object declared or manipulated by this instruction or expression
-
getProperty
Get the value of a particular property of the instruction. Properties of XSLT instructions are generally known by the name of the stylesheet attribute that defines them.- Overrides:
getProperty
in classActor
- Parameters:
name
- The name of the required property- Returns:
- The value of the requested property, or null if the property is not available
-
getHostLanguage
Get the host language (XSLT, XQuery, XPath) used to implement the code in this container- Returns:
- typically
HostLanguage.XSLT
orHostLanguage.XQUERY
-
setIndexedVariable
public void setIndexedVariable()Mark this as an indexed variable, to allow fast searching -
isIndexedVariable
public boolean isIndexedVariable()Ask whether this is an indexed variable- Returns:
- true if this variable is indexed
-
setContainsLocals
The expression that initializes a global variable may itself use local variables. In this case a stack frame needs to be allocated while evaluating the global variable- Parameters:
map
- The stack frame map for local variables used while evaluating this global variable.
-
isGlobal
public boolean isGlobal()Is this a global variable? -
registerReference
Register a variable reference that refers to this global variable- Parameters:
ref
- the variable reference
-
iterateReferences
Iterate over the references to this variable- Returns:
- an iterator over the references: returns objects of class
VariableReference
-
countReferences
public int countReferences()Count the references to this variable- Returns:
- the number of references
-
getBinderySlotNumber
public int getBinderySlotNumber()Get the slot number allocated to this variable in the Bindery- Returns:
- the slot number, that is the position allocated to the variable within the Bindery
-
setBinderySlotNumber
public void setBinderySlotNumber(int s) Set the slot number of this variable in the Bindery- Parameters:
s
- the slot number, that is, the position allocated to this variable within the Bindery
-
setRequiredParam
public void setRequiredParam(boolean requiredParam) Indicate that this variable represents a required parameter- Parameters:
requiredParam
- true if this is a required parameter
-
isRequiredParam
public boolean isRequiredParam()Ask whether this variable represents a required parameter- Returns:
- true if this variable represents a required parameter
-
compile
Create a compiled representation of this global variable. Used in XQuery only.- Parameters:
exec
- the executableslot
- the slot number allocated to this variable- Throws:
XPathException
- if compile-time errors are found.
-
typeCheck
Type check the compiled representation of this global variable- Parameters:
visitor
- an expression visitor- Throws:
XPathException
- if compile-time errors are found.
-
lookForCycles
public void lookForCycles(IndexedStack<Object> referees, XQueryFunctionLibrary globalFunctionLibrary) throws XPathException Check for cycles in this variable definition- Parameters:
referees
- the calls leading up to this one; it's an error if this variable is on the stack, because that means it calls itself directly or indirectly. The stack may contain variable definitions (GlobalVariable objects) and user-defined functions (UserFunction objects). It will never contain the same object more than once.globalFunctionLibrary
- the library containing all global functions- Throws:
XPathException
- if cycles are found
-
getSelectValue
Evaluate the variable. That is, get the value of the select expression if present or the content of the element otherwise, either as a tree or as a sequence- Parameters:
context
- the dynamic evaluation contexttarget
- the component representing the variable to be evaluated- Returns:
- the value of the variable
- Throws:
XPathException
- if a dynamic error occurs during evaluation
-
evaluateVariable
Evaluate the variable- Specified by:
evaluateVariable
in interfaceBinding
- Parameters:
context
- the XPath dynamic evaluation context- Returns:
- the result of evaluating the variable
- Throws:
XPathException
- if an error occurs while evaluating the variable
-
evaluateVariable
Evaluate the variable- Parameters:
context
- the XPath dynamic contexttarget
- the component representing this variable (in the context of a package where it is used)- Returns:
- the value of the variable
- Throws:
XPathException
- if evaluation of the variable fails with a dynamic error
-
actuallyEvaluate
protected GroundedValue actuallyEvaluate(XPathContext context, Component target) throws XPathException Evaluate the global variable, and save its value for use in subsequent references.- Parameters:
context
- the XPath dynamic contexttarget
- the component representing this variable (in the context of a package where it is used)- Returns:
- the value of the variable
- Throws:
XPathException
- if evaluation fails
-
checkCircularity
protected static void checkCircularity(GlobalVariable var, XPathContext context) throws XPathException Get the variable that is immediately dependent on this one, and register the dependency, so that circularities can be detected across threads. This relies on the fact that when the initialiser for variable X contains a reference to variable Y, then when Y is evaluated, a stack frame will be found on the context stack representing the evaluation of X. We don't set a dependency from X to Y if the value of Y was already available in the Bindery; it's not needed, because in this case we know that evaluation of Y is unproblematic, and can't lead to any circularities.- Parameters:
var
- the global variable or parameter being evaluatedcontext
- the dynamic evaluation context- Throws:
XPathException
- if a cycle of dependencies is found
-
getIntegerBoundsForVariable
If the variable is bound to an integer, get the minimum and maximum possible values. Return null if unknown or not applicable- Specified by:
getIntegerBoundsForVariable
in interfaceBinding
- Returns:
- a pair of integers containing the minimum and maximum values for the integer value; or null if the value is not an integer or the range is unknown
-
getLocalSlotNumber
public int getLocalSlotNumber()If this is a local variable held on the local stack frame, return the corresponding slot number. In other cases, return -1.- Returns:
- the slot number on the local stack frame
-
setVariableQName
Set the name of the variable- Parameters:
s
- the name of the variable (a QName)
-
getVariableQName
Get the name of the variable- Specified by:
getVariableQName
in interfaceBinding
- Returns:
- the name of the variable, as a structured QName
-
getDescription
Get a description of the variable for use in diagnostics- Returns:
- a suitable description
-
addReference
Register a variable reference that refers to the variable bound in this expression- Specified by:
addReference
in interfaceBinding
- Parameters:
ref
- the variable reference to be registeredisLoopingReference
- - true if the reference occurs within a loop, such as the predicate of a filter expression
-
export
Diagnostic print of expression structure. The abstract expression tree is written to the supplied outputstream.- Specified by:
export
in classActor
- Parameters:
presenter
- the expression presenter used to display the structure- Throws:
XPathException
- if things go wrong, for example an I/O failure
-
getFlags
-