public static DelegateDeclarationSyntax AddUnmanagedFunctionPointerAttribute(this DelegateDeclarationSyntax member)
 {
     return(member.AddAttributeLists(
                AttributeList(
                    SingletonSeparatedList(
                        Attribute(IdentifierName(nameof(UnmanagedFunctionPointerAttribute)))
                        .AddArgumentListArguments(
                            AttributeArgument(MemberAccessExpression(
                                                  SyntaxKind.SimpleMemberAccessExpression,
                                                  IdentifierName(nameof(CallingConvention)),
                                                  IdentifierName(nameof(CallingConvention.Cdecl)))),
                            AttributeArgument(LiteralExpression(SyntaxKind.FalseLiteralExpression))
                            .WithNameEquals(NameEquals(nameof(UnmanagedFunctionPointerAttribute.BestFitMapping))),
                            AttributeArgument(LiteralExpression(SyntaxKind.FalseLiteralExpression))
                            .WithNameEquals(NameEquals(nameof(UnmanagedFunctionPointerAttribute.SetLastError))))))
                .WithAdditionalAnnotations(new SyntaxAnnotation(Annotations.Namespace,
                                                                InteropServices.ToFullString()))));
 }
Beispiel #2
0
        // Returns the name of the declared API if the node is the type of node that should have an XML doc comment
        // otherwise returns null;
        public string GetAPIForNode(SyntaxNode node)
        {
            switch (node.Kind())
            {
            case SyntaxKind.ConstructorDeclaration:
            {
                ConstructorDeclarationSyntax syntax = (ConstructorDeclarationSyntax)node;
                // assume only one field/identifier
                string text = syntax.Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.ConversionOperatorDeclaration:
            {
                ConversionOperatorDeclarationSyntax syntax = (ConversionOperatorDeclarationSyntax)node;
                // assume only one field/identifier
                string text = syntax.OperatorKeyword.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.DelegateDeclaration:
            {
                DelegateDeclarationSyntax syntax = (DelegateDeclarationSyntax)node;
                // assume only one field/identifier
                string text = syntax.Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.EnumDeclaration:
            {
                EnumDeclarationSyntax syntax = (EnumDeclarationSyntax)node;
                // assume only one field/identifier
                string text = syntax.Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.EventDeclaration:
            {
                /* EventDeclarationSyntax syntax = (EventDeclarationSyntax)node;
                 *
                 * string text = syntax.Identifier.Text;
                 * // for now assume only one match
                 * if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                 * {
                 *   var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                 *   // but which one is the right one?
                 *   var symbol = symbols.Single();
                 *   return symbol.GetDocumentationCommentId();
                 * }*/
                return(null);
            }

            case SyntaxKind.FieldDeclaration:
            {
                FieldDeclarationSyntax syntax = (FieldDeclarationSyntax)node;
                // assume only one field/identifier
                string text = syntax.Declaration.Variables.First().Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.IndexerDeclaration:
            {
                IndexerDeclarationSyntax syntax = (IndexerDeclarationSyntax)node;
                string text = syntax.ThisKeyword.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.InterfaceDeclaration:
            {
                InterfaceDeclarationSyntax syntax = (InterfaceDeclarationSyntax)node;
                string text = syntax.Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.MethodDeclaration:
            {
                MethodDeclarationSyntax methodDeclarationSyntax = (MethodDeclarationSyntax)node;
                string text       = methodDeclarationSyntax.Identifier.Text;
                var    parameters = methodDeclarationSyntax.ParameterList.Parameters;

                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);

                    // select the symbol whose declaring syntax reference matches the node's syntax reference.
                    //var symbol = symbols.Where(s => s.DeclaringSyntaxReferences.Contains(node.GetReference())).Single();

                    foreach (var symbol in symbols)
                    {
                        var references = symbol.DeclaringSyntaxReferences;
                        foreach (var reference in references)
                        {
                            SyntaxNode testNode = reference.GetSyntax();
                            if (testNode.Equals(node))
                            {
                                Console.WriteLine("Matched nodes.");
                            }
                        }
                    }

                    // find the one that corresponds to this syntax node.
                    //foreach (var symbol in symbols)
                    //{
                    //     IMethodSymbol methodsymbol = (IMethodSymbol)symbol;
                    //     symbol.DeclaringSyntaxReferences.Select(syntaxReference => syntaxReference == node.GetReference());
                    //
                    //}
                    return(symbols.First().GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.NamespaceDeclaration:     // doesn't work
            {
                NamespaceDeclarationSyntax syntax = (NamespaceDeclarationSyntax)node;
                NameSyntax nameSyntax             = syntax.Name;
                string     text = nameSyntax.ToFullString();
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.OperatorDeclaration:
            {
                OperatorDeclarationSyntax syntax = (OperatorDeclarationSyntax)node;
                // this won't work, it needs to be figured out.
                string text = syntax.OperatorKeyword.Text + syntax.OperatorToken.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.PropertyDeclaration:
            {
                PropertyDeclarationSyntax syntax = (PropertyDeclarationSyntax)node;
                string text = syntax.Identifier.Text;
                // for now assume only one match
                if (m_compilation.ContainsSymbolsWithName(symbolName => symbolName == text))
                {
                    var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                    // but which one is the right one?
                    var symbol = symbols.Single();
                    return(symbol.GetDocumentationCommentId());
                }
                return(null);
            }

            case SyntaxKind.ClassDeclaration:
            {
                Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax classSyntax = (ClassDeclarationSyntax)node;
                string text      = classSyntax.Identifier.Text;
                string valueText = classSyntax.Identifier.ValueText;
                // for now assume only one match
                var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                // but which one is the right one?
                var symbol = symbols.Single();
                return(symbol.GetDocumentationCommentId());
            }

            case SyntaxKind.StructDeclaration:
            {
                Microsoft.CodeAnalysis.CSharp.Syntax.StructDeclarationSyntax structSyntax = (StructDeclarationSyntax)node;
                string text      = structSyntax.Identifier.Text;
                string valueText = structSyntax.Identifier.ValueText;

                // for now assume only one match
                var symbols = m_compilation.GetSymbolsWithName(symbolName => symbolName == text);
                // but which one is the right one?
                var symbol = symbols.Single();
                return(symbol.GetDocumentationCommentId());
            }

            default:
                return(null);
            }


            //var types = m_compilation.GlobalNamespace.GetTypeMembers(name);
            //m_compilation.GetTypeByMetadataName()
            //string docCommentId = classSymbol.GetDocumentationCommentId();
            return("");
        }
        public static T AddAggressiveInlining <T>(this T member, bool fullyQualified = false) where T : MemberDeclarationSyntax
        {
            NameSyntax attributeType = IdentifierName(nameof(MethodImplAttribute));
            NameSyntax argumentType  = IdentifierName(nameof(MethodImplOptions));

            if (fullyQualified)
            {
                attributeType = QualifiedName(CompilerServices, (IdentifierNameSyntax)argumentType);
                argumentType  = QualifiedName(CompilerServices, (IdentifierNameSyntax)argumentType);
            }

            return((T)member.AddAttributeLists(
                       AttributeList(SingletonSeparatedList(
                                         Attribute(attributeType)
                                         .AddArgumentListArguments(AttributeArgument(MemberAccessExpression(
                                                                                         SyntaxKind.SimpleMemberAccessExpression,
                                                                                         argumentType,
                                                                                         IdentifierName(nameof(MethodImplOptions.AggressiveInlining))))))))
                   .WithAdditionalAnnotations(new SyntaxAnnotation(Annotations.Namespace, CompilerServices.ToFullString())));
        }