Пример #1
0
        public NoticeResponse(ReadOnlyMemory <byte> bytes, ArrayPool <ErrorOrNoticeResponseField> arrayPool)
        {
            var reader = new SequenceReader <byte>(new ReadOnlySequence <byte>(bytes));

            reader.TryRead(out MessageType);
            reader.TryReadBigEndian(out Length);

            if (Length > sizeof(byte) + sizeof(int) + sizeof(byte))
            {
                using (var list = new ValueListBuilder <ErrorOrNoticeResponseField>(Span <ErrorOrNoticeResponseField> .Empty, arrayPool))
                {
                    while (true)
                    {
                        var field = new ErrorOrNoticeResponseField(ref reader);
                        if (field.Type == FieldCodes.Termination)
                        {
                            break;
                        }
                        list.Append(field);
                    }

                    Fields = list.AsSpan().ToArray();
                }
            }
            else
            {
                Fields = Array.Empty <ErrorOrNoticeResponseField>();
            }
        }
Пример #2
0
        public override void AddAttributeTargets(SyntaxNode node, ref ValueListBuilder <SyntaxNode> targets)
        {
            var attributeList = (AttributeListSyntax)node;
            var container     = attributeList.Parent;

            Debug.Assert(container != null);

            // For fields/events, the attribute applies to all the variables declared.
            if (container is FieldDeclarationSyntax field)
            {
                foreach (var variable in field.Declaration.Variables)
                {
                    targets.Append(variable);
                }
            }
            else if (container is EventFieldDeclarationSyntax ev)
            {
                foreach (var variable in ev.Declaration.Variables)
                {
                    targets.Append(variable);
                }
            }
            else
            {
                targets.Append(container);
            }
        }
Пример #3
0
 private RegexWriter(RegexTree tree, Span <int> emittedSpan, Span <int> intStackSpan)
 {
     _tree        = tree;
     _emitted     = new ValueListBuilder <int>(emittedSpan);
     _intStack    = new ValueListBuilder <int>(intStackSpan);
     _stringTable = new Dictionary <string, int>();
     _trackCount  = 0;
 }
Пример #4
0
 private RegexFCD(Span <int> intStack)
 {
     _fcStack         = new List <RegexFC>(StackBufferSize);
     _intStack        = new ValueListBuilder <int>(intStack);
     _failed          = false;
     _skipchild       = false;
     _skipAllChildren = false;
 }
Пример #5
0
 private RegexWriter(Span <int> emittedSpan, Span <int> intStackSpan)
 {
     _emitted     = new ValueListBuilder <int>(emittedSpan);
     _intStack    = new ValueListBuilder <int>(intStackSpan);
     _stringTable = new Dictionary <string, int>();
     _caps        = null;
     _trackCount  = 0;
 }
        private bool                              _resetMatchStartFound; // whether \K appeared in the pattern

        private RegexWriter(Span <int> emittedSpan, Span <int> intStackSpan)
        {
            _emitted     = new ValueListBuilder <int>(emittedSpan);
            _intStack    = new ValueListBuilder <int>(intStackSpan);
            _stringHash  = new Dictionary <string, int>();
            _stringTable = new List <string>();
            _caps        = null;
            _trackCount  = 0;

            _capPositions         = null;
            _resetMatchStartFound = false;
        }
        static ImmutableArray <AttributeData> getMatchingAttributes(
            SyntaxNode attributeTarget,
            ISymbol symbol,
            string fullyQualifiedMetadataName)
        {
            var targetSyntaxTree = attributeTarget.SyntaxTree;
            var result           = new ValueListBuilder <AttributeData>(Span <AttributeData> .Empty);

            try
            {
                addMatchingAttributes(ref result, symbol.GetAttributes());
                addMatchingAttributes(ref result, (symbol as IMethodSymbol)?.GetReturnTypeAttributes());

                if (symbol is IAssemblySymbol assemblySymbol)
                {
                    foreach (var module in assemblySymbol.Modules)
                    {
                        addMatchingAttributes(ref result, module.GetAttributes());
                    }
                }

                return(result.AsSpan().ToImmutableArray());
            }
            finally
            {
                result.Dispose();
            }

            void addMatchingAttributes(
                ref ValueListBuilder <AttributeData> result,
                ImmutableArray <AttributeData>?attributes)
            {
                if (!attributes.HasValue)
                {
                    return;
                }

                foreach (var attribute in attributes.Value)
                {
                    if (attribute.ApplicationSyntaxReference?.SyntaxTree == targetSyntaxTree &&
                        attribute.AttributeClass?.ToDisplayString(/*s_metadataDisplayFormat*/) == fullyQualifiedMetadataName)
                    {
                        result.Append(attribute);
                    }
                }
            }
        }
    /// <summary>
    /// Creates an <see cref="IncrementalValuesProvider{T}"/> that can provide a transform over all <see
    /// cref="SyntaxNode"/>s if that node has an attribute on it that binds to a <see cref="INamedTypeSymbol"/> with the
    /// same fully-qualified metadata as the provided <paramref name="fullyQualifiedMetadataName"/>. <paramref
    /// name="fullyQualifiedMetadataName"/> should be the fully-qualified, metadata name of the attribute, including the
    /// <c>Attribute</c> suffix.  For example <c>"System.CLSCompliantAttribute</c> for <see
    /// cref="System.CLSCompliantAttribute"/>.
    /// </summary>
    /// <param name="predicate">A function that determines if the given <see cref="SyntaxNode"/> attribute target (<see
    /// cref="GeneratorAttributeSyntaxContext.TargetNode"/>) should be transformed.  Nodes that do not pass this
    /// predicate will not have their attributes looked at at all.</param>
    /// <param name="transform">A function that performs the transform. This will only be passed nodes that return <see
    /// langword="true"/> for <paramref name="predicate"/> and which have a matching <see cref="AttributeData"/> whose
    /// <see cref="AttributeData.AttributeClass"/> has the same fully qualified, metadata name as <paramref
    /// name="fullyQualifiedMetadataName"/>.</param>
    public static IncrementalValuesProvider <T> ForAttributeWithMetadataName <T>(
        this SyntaxValueProvider @this,
        IncrementalGeneratorInitializationContext context,
        string fullyQualifiedMetadataName,
        Func <SyntaxNode, CancellationToken, bool> predicate,
        Func <GeneratorAttributeSyntaxContext, CancellationToken, T> transform)
    {
#if false
        // Deviation from roslyn.  We do not support attributes that are nested or generic.  That's ok as that's not a
        // scenario that ever arises in our generators.

        var metadataName = fullyQualifiedMetadataName.Contains('+')
            ? MetadataTypeName.FromFullName(fullyQualifiedMetadataName.Split(s_nestedTypeNameSeparators).Last())
            : MetadataTypeName.FromFullName(fullyQualifiedMetadataName);

        var nodesWithAttributesMatchingSimpleName = @this.ForAttributeWithSimpleName(context, metadataName.UnmangledTypeName, predicate);
#else
        var lastDotIndex = fullyQualifiedMetadataName.LastIndexOf('.');
        Debug.Assert(lastDotIndex > 0);
        var unmangledTypeName = fullyQualifiedMetadataName.Substring(lastDotIndex + 1);

        var nodesWithAttributesMatchingSimpleName = @this.ForAttributeWithSimpleName(context, unmangledTypeName, predicate);
#endif

        var compilationAndGroupedNodesProvider = nodesWithAttributesMatchingSimpleName
                                                 .Combine(context.CompilationProvider)
                                                 /*.WithTrackingName("compilationAndGroupedNodes_ForAttributeWithMetadataName")*/;

        var syntaxHelper  = CSharpSyntaxHelper.Instance;
        var finalProvider = compilationAndGroupedNodesProvider.SelectMany((tuple, cancellationToken) =>
        {
            var((syntaxTree, syntaxNodes), compilation) = tuple;
            Debug.Assert(syntaxNodes.All(n => n.SyntaxTree == syntaxTree));

            using var result = new ValueListBuilder <T>(Span <T> .Empty);
            if (!syntaxNodes.IsEmpty)
            {
                var semanticModel = compilation.GetSemanticModel(syntaxTree);

                foreach (var targetNode in syntaxNodes)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var targetSymbol =
                        targetNode is ICompilationUnitSyntax compilationUnit ? semanticModel.Compilation.Assembly :
                        syntaxHelper.IsLambdaExpression(targetNode) ? semanticModel.GetSymbolInfo(targetNode, cancellationToken).Symbol :
                        semanticModel.GetDeclaredSymbol(targetNode, cancellationToken);
                    if (targetSymbol is null)
                    {
                        continue;
                    }

                    var attributes = getMatchingAttributes(targetNode, targetSymbol, fullyQualifiedMetadataName);
                    if (attributes.Length > 0)
                    {
                        result.Append(transform(
                                          new GeneratorAttributeSyntaxContext(targetNode, targetSymbol, semanticModel, attributes),
                                          cancellationToken));
                    }
                }
            }

            return(result.AsSpan().ToImmutableArray());
        }) /*.WithTrackingName("result_ForAttributeWithMetadataName")*/;

        return(finalProvider);