Exemplo n.º 1
0
        internal static GreenNode?TryGetNode(int kind, GreenNode?child1, GreenNode?child2, GreenNode?child3, GreenNode.NodeFlags flags, out int hash)
        {
            if (CanBeCached(child1, child2, child3))
            {
                GreenStats.ItemCacheable();

                int h   = hash = GetCacheHash(kind, flags, child1, child2, child3);
                int idx = h & CacheMask;
                var e   = s_cache[idx];
                if (e.hash == h && e.node != null && e.node.IsCacheEquivalent(kind, flags, child1, child2, child3))
                {
                    GreenStats.CacheHit();
                    return(e.node);
                }
            }
            else
            {
                hash = -1;
            }

            return(null);
        }
Exemplo n.º 2
0
        private static int GetCacheHash(int kind, GreenNode.NodeFlags flags, GreenNode?child1, GreenNode?child2, GreenNode?child3)
        {
            int code = (int)(flags) ^ kind;

            if (child1 != null)
            {
                code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child1), code);
            }
            if (child2 != null)
            {
                code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child2), code);
            }
            if (child3 != null)
            {
                code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child3), code);
            }

            // ensure nonnegative hash
            return(code & Int32.MaxValue);
        }
Exemplo n.º 3
0
 private static bool CanBeCached(GreenNode?child1, GreenNode?child2, GreenNode?child3)
 {
     return(CanBeCached(child1) && CanBeCached(child2) && CanBeCached(child3));
 }
Exemplo n.º 4
0
 internal static GreenNode?TryGetNode(int kind, GreenNode?child1, GreenNode?child2, GreenNode?child3, out int hash)
 {
     return(TryGetNode(kind, child1, child2, child3, GetDefaultNodeFlags(), out hash));
 }
Exemplo n.º 5
0
 internal Entry(int hash, GreenNode node)
 {
     this.hash = hash;
     this.node = node;
 }
Exemplo n.º 6
0
 private static bool CanBeCached(GreenNode?child1)
 {
     return(child1 == null || child1.IsCacheable);
 }
Exemplo n.º 7
0
 internal SyntaxToken(GreenNode?token)
     : this()
 {
     RoslynDebug.Assert(token == null || token.IsToken, "token must be a token");
     Node = token;
 }
Exemplo n.º 8
0
            public bool MoveNext()
            {
                if (_node != null)
                {
                    if (_list != null)
                    {
                        _listIndex++;

                        if (_listIndex < _list.SlotCount)
                        {
                            _currentChild = _list.GetSlot(_listIndex);
                            return(true);
                        }

                        _list      = null;
                        _listIndex = -1;
                    }

                    while (true)
                    {
                        _childIndex++;

                        if (_childIndex == _node.SlotCount)
                        {
                            break;
                        }

                        var child = _node.GetSlot(_childIndex);
                        if (child == null)
                        {
                            continue;
                        }

                        if (child.RawKind == GreenNode.ListKind)
                        {
                            _list = child;
                            _listIndex++;

                            if (_listIndex < _list.SlotCount)
                            {
                                _currentChild = _list.GetSlot(_listIndex);
                                return(true);
                            }
                            else
                            {
                                _list      = null;
                                _listIndex = -1;
                                continue;
                            }
                        }
                        else
                        {
                            _currentChild = child;
                        }

                        return(true);
                    }
                }

                _currentChild = null;
                return(false);
            }
Exemplo n.º 9
0
 internal static SyntaxList <T> ToGreenList <T>(this GreenNode?node) where T : GreenNode
 {
     return(new SyntaxList <T>(node));
 }
Exemplo n.º 10
0
 internal Reversed(GreenNode?node)
 {
     _node = node;
 }
Exemplo n.º 11
0
        /// <summary>
        /// Locate the node or token that is a child of the given <see cref="SyntaxNode"/> and contains the given position.
        /// </summary>
        /// <param name="node">The <see cref="SyntaxNode"/> to search.</param>
        /// <param name="targetPosition">The position.</param>
        /// <returns>The node or token that spans the given position.</returns>
        /// <remarks>
        /// Assumes that <paramref name="targetPosition"/> is within the span of <paramref name="node"/>.
        /// </remarks>
        internal static SyntaxNodeOrToken ChildThatContainsPosition(SyntaxNode node, int targetPosition)
        {
            // The targetPosition must already be within this node
            Debug.Assert(node.FullSpan.Contains(targetPosition));

            GreenNode?green    = node.Green;
            var       position = node.Position;
            var       index    = 0;

            Debug.Assert(!green.IsList);

            // Find the green node that spans the target position.
            // We will be skipping whole slots here so we will not loop for long
            // The max possible number of slots is 11 (TypeDeclarationSyntax)
            // and typically much less than that
            int slot;

            for (slot = 0; ; slot++)
            {
                GreenNode?greenChild = green.GetSlot(slot);
                if (greenChild != null)
                {
                    var endPosition = position + greenChild.FullWidth;
                    if (targetPosition < endPosition)
                    {
                        // Descend into the child element
                        green = greenChild;
                        break;
                    }

                    position = endPosition;
                    index   += Occupancy(greenChild);
                }
            }

            // Realize the red node (if any)
            var red = node.GetNodeSlot(slot);

            if (!green.IsList)
            {
                // This is a single node or token.
                // If it is a node, we are done.
                if (red != null)
                {
                    return(red);
                }

                // Otherwise will have to make a token with current green and position
            }
            else
            {
                slot = green.FindSlotIndexContainingOffset(targetPosition - position);

                // Realize the red node (if any)
                if (red != null)
                {
                    // It is a red list of nodes (separated or not)
                    red = red.GetNodeSlot(slot);
                    if (red != null)
                    {
                        return(red);
                    }

                    // Must be a separator
                }

                // Otherwise we have a token.
                position += green.GetSlotOffset(slot);
                green     = green.GetSlot(slot);

                // Since we can't have "lists of lists", the Occupancy calculation for
                // child elements in a list is simple.
                index += slot;
            }

            // Make a token with current child and position.
            return(new SyntaxNodeOrToken(node, green, position, index));
        }
Exemplo n.º 12
0
 public override SyntaxToken TokenWithTrailingTrivia(GreenNode?trivia)
 => new SyntaxIdentifierWithTrailingTrivia(_name, trivia, GetDiagnostics(), GetAnnotations());
Exemplo n.º 13
0
 public override SyntaxToken TokenWithTrailingTrivia(GreenNode?trivia) =>
 new SyntaxTokenWithValueAndTrivia <T>(Kind, _text, _value, null, trivia, GetDiagnostics(), GetAnnotations());
Exemplo n.º 14
0
 public override SyntaxToken TokenWithLeadingTrivia(GreenNode?trivia)
 => new SyntaxIdentifierWithTrivia(_contextualKind, _name, trivia, _trailing, GetDiagnostics(), GetAnnotations());
Exemplo n.º 15
0
 internal ChildSyntaxList(GreenNode node)
 {
     _node  = node;
     _count = -1;
 }