How type inference and annotation work in FontoXPath
masterFontoXPath uses a type inference system to annotate the Abstract Syntax Tree (AST) of XPath or XQuery scripts. This process, triggered by the annotateAST function, performs a depth-first, pre-order traversal to determine the type of each node and inserts a SequenceType into the AST as an attribute named type.
Key Mechanisms
- Function Resolution: Function references are resolved by extracting the
QNameand performing a lookup in theStaticContextto determine arity, name, and return type. - Variable Resolution: The
AnnotationContextmanages variable scopes (pushing and popping scopes duringflworexpressions) to track variable types. - Namespace Resolution: Prefixes are resolved using the
namespaceResolveranddefaultFunctionNamespaceURIprovided duringevaluateXPathorparseScript, as well as namespaces defined in the XQueryprolog.
Indeterminate Types
If a type cannot be determined during annotation (e.g., due to runtime dependency or unsupported functions), the node is annotated with the fallback item()* type:
{ type: ValueType.ITEM, mult: SequenceMultiplicity.ZERO_OR_MORE }.
Nodes that always receive this fallback include:
ContextItemExprDynamicFunctionInvocationExprUnaryLookup