Beispiel #1
0
            public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
            {
                _moduleClassSymbol ??= context.SemanticModel.Compilation
                .GetTypeByMetadataName(
                    "Finite.Commands.AttributedModel.Module");
                _groupAttributeSymbol ??= context.SemanticModel.Compilation
                .GetTypeByMetadataName(
                    "Finite.Commands.AttributedModel.GroupAttribute");
                _commandAttributeSymbol ??= context.SemanticModel.Compilation
                .GetTypeByMetadataName(
                    "Finite.Commands.AttributedModel.CommandAttribute");

                Debug.Assert(_moduleClassSymbol != null);
                Debug.Assert(_groupAttributeSymbol != null);
                Debug.Assert(_commandAttributeSymbol != null);

                if (
                    context.Node is ClassDeclarationSyntax classDeclaration &&
                    classDeclaration.BaseList is BaseListSyntax baseList &&
                    baseList.Types
                    .Any(n => IsModuleClass(n, context.SemanticModel,
                                            _moduleClassSymbol !)) &&
                    classDeclaration.AttributeLists is
                    SyntaxList <AttributeListSyntax> attributeLists &&
                    attributeLists.Any(
                        list => list.Attributes
                        .Any(
                            n => IsAttribute(n, context.SemanticModel,
                                             _groupAttributeSymbol !)))
                    )
                {
                    _classes.Add(classDeclaration);
                }
            public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
            {
                _remainderAttributeSymbol ??= context.SemanticModel.Compilation
                .GetTypeByMetadataName(
                    "Finite.Commands.AttributedModel.RemainderAttribute");

                Debug.Assert(_remainderAttributeSymbol != null);

                if (
                    context.Node is AttributeSyntax commandDataProviderAttr &&
                    IsAttribute(commandDataProviderAttr,
                                context.SemanticModel, _remainderAttributeSymbol !))
                {
                    var node = context.Node;
                    while (node != null)
                    {
                        if (node.Parent is ParameterSyntax parameter)
                        {
                            _parameters.Add(parameter);
                            break;
                        }

                        node = node.Parent;
                    }
                }
            }
        public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
        {
            if (this.ProjectDirectory == null && context.Node is ClassDeclarationSyntax)
            {
                var templateFinder = new CloudFormationTemplateFinder(_fileManager, _directoryManager);
                this.ProjectDirectory = templateFinder.DetermineProjectRootDirectory(context.Node.SyntaxTree.FilePath);
            }

            // any method with at least one attribute is a candidate of function generation
            if (context.Node is MethodDeclarationSyntax methodDeclarationSyntax && methodDeclarationSyntax.AttributeLists.Count > 0)
            {
                // Get the symbol being declared by the method, and keep it if its annotated
                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
                if (methodSymbol.GetAttributes().Any(attr => attr.AttributeClass.Name == nameof(LambdaFunctionAttribute)))
                {
                    LambdaMethods.Add(methodDeclarationSyntax);
                }
            }

            // any class with at least one attribute is a candidate of Startup class
            if (context.Node is ClassDeclarationSyntax classDeclarationSyntax && classDeclarationSyntax.AttributeLists.Count > 0)
            {
                // Get the symbol being declared by the class, and keep it if its annotated
                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);
                if (methodSymbol.GetAttributes().Any(attr => attr.AttributeClass.Name == nameof(LambdaStartupAttribute)))
                {
                    StartupClasses.Add(classDeclarationSyntax);
                }
            }
        }
Beispiel #4
0
            public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
            {
                TypeDeclarationSyntax syntax;
                ISymbol symbol;

                if (context.Node is ClassDeclarationSyntax classDeclarationSyntax)
                {
                    syntax = classDeclarationSyntax;
                }
                else if (context.Node is StructDeclarationSyntax
                         structDeclarationSyntax)
                {
                    syntax = structDeclarationSyntax;
                }
                else
                {
                    return;
                }

                symbol = context.SemanticModel.GetDeclaredSymbol(syntax);
                if (symbol is not INamedTypeSymbol namedTypeSymbol)
                {
                    return;
                }

                this.g_.CheckType(context, syntax, namedTypeSymbol);
            }
        private bool TryGetSymbol(GeneratorSyntaxContext context, out ISymbol symbol)
        {
            if (context.Node is ClassDeclarationSyntax classDeclarationSyntax &&
                classDeclarationSyntax.AttributeLists.Count > 0)
            {
                if (context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax) is ISymbol candidate)
                {
                    if (candidate.GetAttribute(attributeFullName) is not null)
                    {
                        symbol = candidate;
                        return(true);
                    }
                }
            }

            if (context.Node is ConstructorDeclarationSyntax ctorDeclarationSyntax &&
                ctorDeclarationSyntax.AttributeLists.Count > 0)
            {
                if (context.SemanticModel.GetDeclaredSymbol(ctorDeclarationSyntax) is ISymbol candidate)
                {
                    if (candidate.GetAttribute(attributeFullName) is not null)
                    {
                        symbol = candidate;
                        return(true);
                    }
                }
            }

            symbol = default;
            return(false);
        }
            public void VisitTree(SyntaxNode root, EntryState state, SemanticModel?model, CancellationToken cancellationToken)
            {
                if (state == EntryState.Removed)
                {
                    // mark both syntax *and* transform nodes removed
                    _filterTable.RemoveEntries();
                    _transformTable.RemoveEntries();
                }
                else
                {
                    Debug.Assert(model is object);

                    // get the syntax nodes from cache, or a syntax walk using the filter
                    ImmutableArray <SyntaxNode> nodes;
                    if (state != EntryState.Cached || !_filterTable.TryUseCachedEntries(out nodes))
                    {
                        nodes = IncrementalGeneratorSyntaxWalker.GetFilteredNodes(root, _owner._filterFunc, cancellationToken);
                        _filterTable.AddEntries(nodes, EntryState.Added);
                    }

                    // now, using the obtained syntax nodes, run the transform
                    foreach (var node in nodes)
                    {
                        var value       = new GeneratorSyntaxContext(node, model);
                        var transformed = ImmutableArray.Create(_owner._transformFunc(value, cancellationToken));

                        if (state == EntryState.Added || !_transformTable.TryModifyEntries(transformed, _owner._comparer))
                        {
                            _transformTable.AddEntries(transformed, EntryState.Added);
                        }
                    }
                }
            }
Beispiel #7
0
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is ClassDeclarationSyntax cds)
     {
         ClassDeclarations.Add((cds, context.SemanticModel));
     }
 }
Beispiel #8
0
        public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
        {
            if (context.Node is PropertyDeclarationSyntax propertyDeclarationSyntax &&
                propertyDeclarationSyntax.AttributeLists.Any())
            {
                ClassDeclarationSyntax classDeclarationSyntax = (ClassDeclarationSyntax)propertyDeclarationSyntax.Parent;

                if (!classDeclarationSyntax.Modifiers.Any(k => k.IsKind(SyntaxKind.PartialKeyword)))
                {
                    return;
                }

                IPropertySymbol propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);

                INamedTypeSymbol @class = propertySymbol.ContainingType;

                if (@class.GetMembers().Any(m => m.Name == "SetParametersAsync"))
                {
                    return;
                }

                if (propertySymbol.GetAttributes().Any(ad => ad.AttributeClass.ToDisplayString() == "Microsoft.AspNetCore.Components.ParameterAttribute" || ad.AttributeClass.ToDisplayString() == "Microsoft.AspNetCore.Components.CascadingParameterAttribute"))
                {
                    Properties.Add(propertySymbol);
                }
            }
        }
            internal static ClassDeclarationSyntax?GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
            {
                var methodDeclarationSyntax = (MethodDeclarationSyntax)context.Node;

                foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
                {
                    foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
                    {
                        IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol;
                        if (attributeSymbol == null)
                        {
                            continue;
                        }

                        INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
                        string           fullName = attributeContainingTypeSymbol.ToDisplayString();

                        if (fullName == LoggerMessageAttribute)
                        {
                            return(methodDeclarationSyntax.Parent as ClassDeclarationSyntax);
                        }
                    }
                }

                return(null);
            }
Beispiel #10
0
 public DefinitionExtractionContext(GeneratorSyntaxContext context)
 {
     _context = context;
     _autoSerializableAttributeSymbol  = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoSerializableAttribute).FullName);
     _autoSerializedAttributeSymbol    = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoSerializedAttribute).FullName);
     _autoNonSerializedAttributeSymbol = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoNonSerializedAttribute).FullName);
     _mobileObjectInterfaceSymbol      = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Csla.Serialization.Mobile.IMobileObject).FullName);
 }
        INamedTypeSymbol?GetMockedType(GeneratorSyntaxContext context)
        {
            var candidateGeneric = SyntaxHelpers.GetMockSymbol(context.Node);
            var semanticModel    = context.SemanticModel;

            return(candidateGeneric != null
                ? syntaxHelpers.GetMockedType(candidateGeneric, semanticModel)
                : null);
        }
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     // find all valid attributes
     if (context.Node is AttributeSyntax attributeSyntax &&
         attributeSyntax.ArgumentList?.Arguments.Count == 1 &&
         context.SemanticModel.GetTypeInfo(attributeSyntax).Type?.ToDisplayString() == "NServiceBusEndpointNameAttribute")
     {
         EndpointName = context.SemanticModel.GetConstantValue(attributeSyntax.ArgumentList.Arguments[0].Expression).ToString();
     }
 }
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is AttributeSyntax attrib &&
         attrib.ArgumentList?.Arguments.Count == 1 &&
         context.SemanticModel.GetTypeInfo(attrib).Type?.ToDisplayString() == "ImportStandardControlAttribute")
     {
         string interfaceFullTypeName = context.SemanticModel.GetConstantValue(attrib.ArgumentList.Arguments[0].Expression).ToString();
         InterfacesToGenerate.Add(interfaceFullTypeName);
     }
 }
 /// <inheritdoc/>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is MethodDeclarationSyntax methodDeclaration &&
         context.SemanticModel.GetDeclaredSymbol(methodDeclaration) is IMethodSymbol methodSymbol &&
         context.SemanticModel.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.Input.ICommandAttribute") is INamedTypeSymbol iCommandSymbol &&
         methodSymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, iCommandSymbol)))
     {
         this.gatheredInfo.Add(new Item(methodDeclaration.GetLeadingTrivia(), methodSymbol));
     }
 }
Beispiel #15
0
        private ITypeSymbol GetTypeSymbols(GeneratorSyntaxContext context, CancellationToken cancellationToken)
        {
            var decl = (ClassDeclarationSyntax)context.Node;

            if (context.SemanticModel.GetDeclaredSymbol(decl, cancellationToken) is ITypeSymbol typeSymbol)
            {
                return(typeSymbol);
            }

            return(null);
        }
Beispiel #16
0
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (IsSyntaxTargetForGeneration(context.Node))
     {
         ClassDeclarationSyntax classSyntax = GetSemanticTargetForGeneration(context);
         if (classSyntax != null)
         {
             ClassDeclarations.Add(classSyntax);
         }
     }
 }
        //public List<UnionDefinition> Definitions { get; } = new List<UnionDefinition>();

        public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
        {
            if (!(context.Node is StructDeclarationSyntax dec) ||
                dec.AttributeLists.Count <= 0)
            {
                return;
            }

            //if (UnionDefinition.TryCreate(context, dec, out var def))
            //    this.Definitions.Add(def);
        }
Beispiel #18
0
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (Parser.IsSyntaxTargetForGeneration(context.Node))
     {
         ClassDeclarationSyntax classSyntax = Parser.GetSemanticTargetForGeneration(context);
         if (classSyntax != null)
         {
             (ClassDeclarationSyntaxList ??= new List <ClassDeclarationSyntax>()).Add(classSyntax);
         }
     }
 }
        private static MethodDeclarationSyntax?SyntaxProviderTransform(GeneratorSyntaxContext context, CancellationToken cancellationToken)
        {
            MethodDeclarationSyntax?method = (MethodDeclarationSyntax)context.Node;

            if (DoesReturnString(method, context.SemanticModel, cancellationToken) &&
                HasHelloWorldAttribute(method, context.SemanticModel, cancellationToken))
            {
                return(method);
            }

            return(null);
        }
 /// <inheritdoc/>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is ClassDeclarationSyntax classDeclaration &&
         context.SemanticModel.GetDeclaredSymbol(classDeclaration) is INamedTypeSymbol {
         IsGenericType : false
     } classSymbol&&
         context.SemanticModel.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.Messaging.IRecipient`1") is INamedTypeSymbol iRecipientSymbol &&
         classSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i.OriginalDefinition, iRecipientSymbol)))
     {
         this.gatheredInfo.Add(classSymbol);
     }
 }
Beispiel #21
0
 /// <inheritdoc/>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is MethodDeclarationSyntax {
         AttributeLists : { Count : > 0 }
     } methodDeclaration&&
         context.SemanticModel.GetDeclaredSymbol(methodDeclaration) is IMethodSymbol methodSymbol &&
         context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(ShaderMethodAttribute).FullName) is INamedTypeSymbol attributeSymbol &&
         methodSymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)))
     {
         this.gatheredInfo.Add(new Item(methodDeclaration, methodSymbol));
     }
 }
Beispiel #22
0
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is ClassDeclarationSyntax classDeclaration && context.SemanticModel.GetDeclaredSymbol(context.Node) is INamedTypeSymbol classSymbol)
     {
         var hasAttribute = classSymbol.GetAttributes().Any((a) => a.AttributeClass.ToDisplayString() == "AzureBot.CommandLine.GeneratedCommandAttribute");
         var isPartial    = classDeclaration.Modifiers.Any((mod) => mod.IsKind(SyntaxKind.PartialKeyword));
         if (hasAttribute && isPartial)
         {
             _classes.Add(classSymbol);
         }
     }
 }
 /// <inheritdoc/>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is ClassDeclarationSyntax classDeclaration &&
         context.SemanticModel.GetDeclaredSymbol(classDeclaration) is INamedTypeSymbol {
         IsGenericType : false
     } classSymbol&&
         context.SemanticModel.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableValidator") is INamedTypeSymbol validatorSymbol &&
         classSymbol.InheritsFrom(validatorSymbol))
     {
         this.gatheredInfo.Add(classSymbol);
     }
 }
Beispiel #24
0
            public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
            {
                // find all valid mustache attributes
                if (context.Node is AttributeSyntax attrib &&
                    attrib.ArgumentList?.Arguments.Count == 3 &&
                    context.SemanticModel.GetTypeInfo(attrib).Type?.ToDisplayString() == "MustacheAttribute")
                {
                    string name     = context.SemanticModel.GetConstantValue(attrib.ArgumentList.Arguments[0].Expression).ToString();
                    string template = context.SemanticModel.GetConstantValue(attrib.ArgumentList.Arguments[1].Expression).ToString();
                    string hash     = context.SemanticModel.GetConstantValue(attrib.ArgumentList.Arguments[2].Expression).ToString();

                    TemplateInfo.Add((name, template, hash));
                }
            }
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is InterfaceDeclarationSyntax interfaceDeclarationSyntax && interfaceDeclarationSyntax.AttributeLists.Count > 0)
     {
         var declaredSymbol = context.SemanticModel.GetDeclaredSymbol(interfaceDeclarationSyntax);
         if (declaredSymbol is INamedTypeSymbol namedTypeSymbol)
         {
             if (namedTypeSymbol.GetAttributes().Any(ad => ad.AttributeClass.ToDisplayString().Equals(_attributeName)))
             {
                 AutoImplementationInterfaces.Add(namedTypeSymbol);
             }
         }
     }
 }
        INamedTypeSymbol?ConvertToDelegate(GeneratorSyntaxContext context)
        {
            var mockedType = GetMockedType(context);

            if (mockedType != null)
            {
                var mtbt = mockedType.BaseType;
                if (mtbt != null && mtbt.ToDisplayString(SymbolDisplayFormats.Namespace) == multicastDelegateNameSpaceAndName)
                {
                    return(mockedType);
                }
            }
            return(null);
        }
        INamedTypeSymbol?ConvertToInterface(GeneratorSyntaxContext context)
        {
            var mockedType = GetMockedType(context);

            if (mockedType != null)
            {
                var mtbt = mockedType.BaseType;
                if (mtbt == null)
                {
                    return(mockedType);
                }
            }
            return(null);
        }
Beispiel #28
0
 /// <inheritdoc/>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is StructDeclarationSyntax {
         AttributeLists : { Count : > 0 }
     } structDeclaration&&
         context.SemanticModel.GetDeclaredSymbol(structDeclaration) is INamedTypeSymbol structSymbol &&
         context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(AutoConstructorAttribute).FullName) is INamedTypeSymbol attributeSymbol &&
         structSymbol.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)) is AttributeData attributeData &&
         attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
         syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax)
     {
         this.gatheredInfo.Add(new Item(structDeclaration, structSymbol, attributeSyntax, attributeData));
     }
 }
Beispiel #29
0
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     if (context.Node is ClassDeclarationSyntax {
         AttributeLists : { Count : > 0 }
     } classDeclarationSyntax)
     {
         var symbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax);
         if (symbol !.GetAttributes().Any(x =>
                                          x.AttributeClass !.ToDisplayString() == ExcelProviderAttribute.TypeFullName))
         {
             CandidateClasses.Add(classDeclarationSyntax);
         }
     }
 }
Beispiel #30
0
 /// <summary>
 /// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
 /// </summary>
 public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
 {
     // any field with at least one attribute is a candidate for property generation
     if (context.Node is FieldDeclarationSyntax fieldDeclarationSyntax &&
         fieldDeclarationSyntax.AttributeLists.Count > 0)
     {
         foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
         {
             // Get the symbol being declared by the field, and keep it if its annotated
             IFieldSymbol fieldSymbol = context.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol;
             if (fieldSymbol.GetAttributes().Any(ad => ad.AttributeClass.ToDisplayString() == "Comet.AutoNotifyAttribute"))
             {
                 Fields.Add(fieldSymbol);
             }
         }
     }
 }