public void SetFocus(QilIterator current) { if (current != null) { this.focusType = SingletonFocusType.Iterator; this.current = current; } else { this.focusType = SingletonFocusType.None; this.current = null; } }
private XmlQueryType FindFilterType(QilIterator variable, QilNode body) { XmlQueryType leftType; QilBinary binary; if (body.XmlType.TypeCode == XmlTypeCode.None) return XmlQueryTypeFactory.None; switch (body.NodeType) { case QilNodeType.False: return XmlQueryTypeFactory.Empty; case QilNodeType.IsType: // If testing the type of "variable", then filter type can be restricted if ((object) ((QilTargetType) body).Source == (object) variable) return XmlQueryTypeFactory.AtMost(((QilTargetType)body).TargetType, variable.Binding.XmlType.Cardinality); break; case QilNodeType.And: // Both And conditions can be used to restrict filter's type leftType = FindFilterType(variable, ((QilBinary) body).Left); if (leftType != null) return leftType; return FindFilterType(variable, ((QilBinary) body).Right); case QilNodeType.Eq: // Restrict cardinality if position($iterator) = $pos is found binary = (QilBinary) body; if (binary.Left.NodeType == QilNodeType.PositionOf) { if ((object) ((QilUnary) binary.Left).Child == (object) variable) return XmlQueryTypeFactory.AtMost(variable.Binding.XmlType, XmlQueryCardinality.ZeroOrOne); } break; } return null; }
//----------------------------------------------- // variables //----------------------------------------------- public XmlQueryType CheckFor(QilIterator node) { return node.Binding.XmlType.Prime; }
private QilNode CompileSingleKey(List<Key> defList, QilIterator key, QilIterator context) { Debug.Assert(defList != null && defList.Count > 0); QilList result = f.BaseFactory.Sequence(); QilNode keyRef = null; foreach (Key keyDef in defList) { keyRef = f.Invoke(keyDef.Function, f.ActualParameterList(context, key)); result.Add(keyRef); } return defList.Count == 1 ? keyRef : result; }
public void Sort(QilNode sortKeys) { if (sortKeys != null) { // If sorting is required, cache the input node-set to support last() within sort key expressions EnsureCache(); // The rest of the loop content must be compiled in the context of already sorted node-set _current = _f.For(_f.Sort(_current, sortKeys)); } }
public LoopFocus(XPathQilFactory f) { _f = f; _current = _cached = _last = null; }
protected override QilNode VisitLet(QilIterator n) { return NoReplace(n); }
//----------------------------------------------- // loops //----------------------------------------------- public QilNode Loop(QilIterator variable, QilNode body) { if (! debug) { //((Loop (For $Binding) ($Binding) ) => ($binding)) if (body == variable.Binding) { return body; } } return f.Loop(variable, body); }
/// <summary> /// True if the specified iterator is a global variable Let iterator. /// </summary> private bool IsGlobalVariable(QilIterator iter) { return this.qil.GlobalVariableList.Contains(iter); }
protected override QilNode VisitLet(QilIterator local0) { QilNode local1 = local0[0]; if ((( ( (local0).XmlType ).IsSingleton ) && (!( IsGlobalVariable(local0) ))) && (this[XmlILOptimization.NormalizeSingletonLet])) { if (AllowReplace(XmlILOptimization.NormalizeSingletonLet, local0)) { local0.NodeType = QilNodeType.For; VisitFor(local0); } } if (this[XmlILOptimization.AnnotateLet]) { if (AllowReplace(XmlILOptimization.AnnotateLet, local0)) { OptimizerPatterns.Inherit((QilNode) (local1), (QilNode) (local0), OptimizerPatternName.Step); OptimizerPatterns.Inherit((QilNode) (local1), (QilNode) (local0), OptimizerPatternName.IsDocOrderDistinct); OptimizerPatterns.Inherit((QilNode) (local1), (QilNode) (local0), OptimizerPatternName.SameDepth); } } return NoReplace(local0); }
public QilIterator Let(QilNode binding) { QilIterator n = new QilIterator(QilNodeType.Let, binding); n.XmlType = this.typeCheck.CheckLet(n); TraceNode(n); return n; }
/// <summary> /// Mark iterator variables as out-of-scope. /// </summary> private void EndBinding(QilIterator ndIter) { Debug.Assert(ndIter != null); // Variables go out of scope here if (_qil.IsDebug && ndIter.DebugName != null) _helper.DebugEndScope(); }
/// <summary> /// Bind values in the "ndLet" expression to a non-stack location that can later be referenced. /// </summary> public void StartLetBinding(QilIterator ndLet) { Debug.Assert(!ndLet.XmlType.IsSingleton); // Construct nested iterator StartNestedIterator(ndLet); // Allow base internal class to dispatch based on QilExpression node type NestedVisit(ndLet.Binding, GetItemStorageType(ndLet), !ndLet.XmlType.IsSingleton); // DebugInfo: Open variable scope // DebugInfo: Ensure that for variable is stored in a local and tag it with the user-defined name if (_qil.IsDebug && ndLet.DebugName != null) { _helper.DebugStartScope(); // Ensure that cache is stored in a local variable with a user-defined name _iterCurr.EnsureLocal("$$$cache"); } else { // Ensure that cache is not stored on the stack _iterCurr.EnsureNoStack("$$$cache"); } EndNestedIterator(ndLet); }
/// <summary> /// Bind values produced by the "ndFor" expression to a non-stack location that can later /// be referenced. /// </summary> private void StartForBinding(QilIterator ndFor, OptimizerPatterns patt) { LocalBuilder locPos = null; Debug.Assert(ndFor.XmlType.IsSingleton); // For expression iterator will be unnested as part of parent iterator if (_iterCurr.HasLabelNext) StartNestedIterator(ndFor.Binding, _iterCurr.GetLabelNext()); else StartNestedIterator(ndFor.Binding); if (patt.MatchesPattern(OptimizerPatternName.IsPositional)) { // Need to track loop index so initialize it to 0 before starting loop locPos = _helper.DeclareLocal("$$$pos", typeof(int)); _helper.Emit(OpCodes.Ldc_I4_0); _helper.Emit(OpCodes.Stloc, locPos); } // Allow base internal class to dispatch based on QilExpression node type Visit(ndFor.Binding); // DebugInfo: Open variable scope // DebugInfo: Ensure that for variable is stored in a local and tag it with the user-defined name if (_qil.IsDebug && ndFor.DebugName != null) { _helper.DebugStartScope(); // Ensure that values are stored in a local variable with a user-defined name _iterCurr.EnsureLocalNoCache("$$$for"); } else { // Ensure that values are not stored on the stack _iterCurr.EnsureNoStackNoCache("$$$for"); } if (patt.MatchesPattern(OptimizerPatternName.IsPositional)) { // Increment position _helper.Emit(OpCodes.Ldloc, locPos); _helper.Emit(OpCodes.Ldc_I4_1); _helper.Emit(OpCodes.Add); _helper.Emit(OpCodes.Stloc, locPos); if (patt.MatchesPattern(OptimizerPatternName.MaxPosition)) { // Short-circuit rest of loop if max position has already been reached _helper.Emit(OpCodes.Ldloc, locPos); _helper.LoadInteger((int)patt.GetArgument(OptimizerPatternArgument.MaxPosition)); _helper.Emit(OpCodes.Bgt, _iterCurr.ParentIterator.GetLabelNext()); } _iterCurr.LocalPosition = locPos; } EndNestedIterator(ndFor.Binding); _iterCurr.SetIterator(_iterNested); }
/// <summary> /// Generate code for a Let, For, or Parameter iterator. Bind iterated value to a variable. /// </summary> private void StartBinding(QilIterator ndIter) { OptimizerPatterns patt = OptimizerPatterns.Read(ndIter); Debug.Assert(ndIter != null); // DebugInfo: Sequence point just before generating code for the bound expression if (_qil.IsDebug && ndIter.SourceLine != null) _helper.DebugSequencePoint(ndIter.SourceLine); // Treat cardinality one Let iterators as if they were For iterators (no nesting necessary) if (ndIter.NodeType == QilNodeType.For || ndIter.XmlType.IsSingleton) { StartForBinding(ndIter, patt); } else { Debug.Assert(ndIter.NodeType == QilNodeType.Let || ndIter.NodeType == QilNodeType.Parameter); Debug.Assert(!patt.MatchesPattern(OptimizerPatternName.IsPositional)); // Bind Let values (nested iterator) to variable StartLetBinding(ndIter); } // Attach IteratorDescriptor to the iterator XmlILAnnotation.Write(ndIter).CachedIteratorDescriptor = _iterNested; }
public XmlQueryType CheckLet(QilIterator node) { return(node.Binding.XmlType); }
//----------------------------------------------- // sorting //----------------------------------------------- public QilNode Sort(QilIterator iter, QilNode keys) { return(_f.Sort(iter, keys)); }
protected override QilNode VisitFor(QilIterator n) { return(NoReplace(n)); }
//----------------------------------------------- // sorting //----------------------------------------------- public QilNode Sort(QilIterator iter, QilNode keys) { return f.Sort(iter, keys); }
protected override QilNode VisitLetReference(QilIterator n) { return(NoReplace(n)); }
protected virtual QilNode VisitLetReference(QilIterator n) { return n; }
private QilNode MatchPattern(QilNode pattern, QilIterator testNode) { QilList list; if (pattern.NodeType == QilNodeType.Error) { // Invalid pattern return pattern; } else if (pattern.NodeType == QilNodeType.Sequence) { list = (QilList)pattern; Debug.Assert(0 < list.Count, "Pattern should have at least one filter"); } else { list = _f.BaseFactory.Sequence(); list.Add(pattern); } QilNode result = _f.False(); for (int i = list.Count - 1; 0 <= i; i--) { QilLoop filter = (QilLoop)list[i]; _ptrnBuilder.AssertFilter(filter); result = _f.Or( _refReplacer.Replace(filter.Body, filter.Variable, testNode), result ); } return result; }
public void SetFocus(QilIterator current) { _current = current; _cached = _last = null; }
private QilNode MatchCountPattern(QilNode countPattern, QilIterator testNode) { /* If the 'count' attribute is not specified, then it defaults to the pattern that matches any node with the same node kind as the context node and, if the context node has an expanded-QName, with the same expanded-QName as the context node. */ if (countPattern != null) { return MatchPattern(countPattern, testNode); } else { QilNode current = GetCurrentNode(); QilNode result; XmlNodeKindFlags nodeKinds = current.XmlType.NodeKinds; // If node kind is not known, invoke a runtime function if ((nodeKinds & (nodeKinds - 1)) != 0) { return _f.InvokeIsSameNodeSort(testNode, current); } // Otherwise generate IsType check along with expanded QName check switch (nodeKinds) { case XmlNodeKindFlags.Document: return _f.IsType(testNode, T.Document); case XmlNodeKindFlags.Element: result = _f.IsType(testNode, T.Element); break; case XmlNodeKindFlags.Attribute: result = _f.IsType(testNode, T.Attribute); break; case XmlNodeKindFlags.Text: return _f.IsType(testNode, T.Text); case XmlNodeKindFlags.Comment: return _f.IsType(testNode, T.Comment); case XmlNodeKindFlags.PI: return _f.And(_f.IsType(testNode, T.PI), _f.Eq(_f.LocalNameOf(testNode), _f.LocalNameOf(current))); case XmlNodeKindFlags.Namespace: return _f.And(_f.IsType(testNode, T.Namespace), _f.Eq(_f.LocalNameOf(testNode), _f.LocalNameOf(current))); default: Debug.Fail("Unexpected NodeKind: " + nodeKinds.ToString()); return _f.False(); } // Elements and attributes have both local name and namespace URI return _f.And(result, _f.And( _f.Eq(_f.LocalNameOf(testNode), _f.LocalNameOf(current)), _f.Eq(_f.NamespaceUriOf(testNode), _f.NamespaceUriOf(GetCurrentNode())) )); } }
public SingletonFocus(XPathQilFactory f) { _f = f; _focusType = SingletonFocusType.None; _current = null; }
public void AddVariable(QilIterator let) { Debug.Assert(let.NodeType == QilNodeType.Let); _vars.Push(let); }
protected virtual QilNode VisitLetReference(QilIterator n) { return(n); }
public LoopFocus(XPathQilFactory f) { this.f = f; current = cached = last = null; }
public XmlQueryType CheckLet(QilIterator node) { return node.Binding.XmlType; }
public void SetFocus(QilIterator current) { this.current = current; cached = last = null; }
//----------------------------------------------- // variables //----------------------------------------------- public XmlQueryType CheckFor(QilIterator node) { return(node.Binding.XmlType.Prime); }
public SingletonFocus(XPathQilFactory f) { this.f = f; focusType = SingletonFocusType.None; current = null; }
public QilNode PositionOf(QilIterator expr) { return(_f.PositionOf(expr)); }
protected override QilNode VisitFor(QilIterator n) { return NoReplace(n); }
public QilNode PositionOf(QilIterator expr) { return f.PositionOf(expr); }
protected override QilNode VisitLetReference(QilIterator n) { return NoReplace(n); }
public QilNode Filter(QilIterator variable, QilNode expr) { if (! debug) { //((Filter (For $Binding) (True ) ) => ($binding)) if (expr.NodeType == QilNodeType.True) { return variable.Binding; } // The following optimization is not safe if the iterator has side effects //((Filter (For $Binding) (False) ) => (Sequence)) } return f.Filter(variable, expr); }
protected virtual QilNode VisitLet(QilIterator n) { return VisitChildren(n); }
protected virtual QilNode VisitFor(QilIterator n) { return(VisitChildren(n)); }
protected override QilNode VisitForReference(QilIterator n) { return NoReplace(n); }