コード例 #1
0
ファイル: CodeGenerator.cs プロジェクト: kopffarben/Hagar
        private async Task <MetadataModel> GenerateMetadataModel(CancellationToken cancellationToken)
        {
            var metadataModel = new MetadataModel();

            foreach (var syntaxTree in this.compilation.SyntaxTrees)
            {
                var semanticModel = this.compilation.GetSemanticModel(syntaxTree, ignoreAccessibility: false);
                var rootNode      = await syntaxTree.GetRootAsync(cancellationToken);

                foreach (var node in GetTypeDeclarations(rootNode))
                {
                    var symbol = semanticModel.GetDeclaredSymbol(node);

                    bool ShouldGenerateSerializer(INamedTypeSymbol t)
                    {
                        if (!semanticModel.IsAccessible(0, t))
                        {
                            return(false);
                        }
                        if (this.HasAttribute(t, this.libraryTypes.GenerateSerializerAttribute, inherited: true) != null)
                        {
                            return(true);
                        }
                        if (this.generateSerializerAttributes != null)
                        {
                            foreach (var attr in this.generateSerializerAttributes)
                            {
                                if (this.HasAttribute(t, attr, inherited: true) != null)
                                {
                                    return(true);
                                }
                            }
                        }

                        return(false);
                    }

                    if (ShouldGenerateSerializer(symbol))
                    {
                        var typeDescription = new SerializableTypeDescription(semanticModel, symbol, this.GetDataMembers(symbol));
                        metadataModel.SerializableTypes.Add(typeDescription);
                    }

                    if (symbol.TypeKind == TypeKind.Interface)
                    {
                        var attribute = this.HasAttribute(
                            symbol,
                            this.libraryTypes.GenerateMethodSerializersAttribute,
                            inherited: true);
                        if (attribute != null)
                        {
                            var baseClass   = (INamedTypeSymbol)attribute.ConstructorArguments[0].Value;
                            var isExtension = (bool)attribute.ConstructorArguments[1].Value;
                            var description = new InvokableInterfaceDescription(
                                this.libraryTypes,
                                semanticModel,
                                symbol,
                                this.GetMethods(symbol),
                                baseClass,
                                isExtension);
                            metadataModel.InvokableInterfaces.Add(description);
                        }
                    }
                }
            }

            return(metadataModel);
        }
コード例 #2
0
ファイル: CodeGenerator.cs プロジェクト: swipswaps/Hagar
        private MetadataModel GenerateMetadataModel(CancellationToken cancellationToken)
        {
            var metadataModel = new MetadataModel();

            foreach (var syntaxTree in _compilation.SyntaxTrees)
            {
                var semanticModel = _compilation.GetSemanticModel(syntaxTree, ignoreAccessibility: false);
                var rootNode      = syntaxTree.GetRoot(cancellationToken);
                foreach (var node in GetTypeDeclarations(rootNode))
                {
                    var symbolRaw = semanticModel.GetDeclaredSymbol(node, cancellationToken: cancellationToken);
                    if (symbolRaw is not INamedTypeSymbol symbol)
                    {
                        continue;
                    }

                    bool ShouldGenerateSerializer(INamedTypeSymbol t)
                    {
                        if (!semanticModel.IsAccessible(0, t))
                        {
                            return(false);
                        }

                        if (HasAttribute(t, _libraryTypes.GenerateSerializerAttribute, inherited: true) != null)
                        {
                            return(true);
                        }

                        if (_generateSerializerAttributes != null)
                        {
                            foreach (var attr in _generateSerializerAttributes)
                            {
                                if (HasAttribute(t, attr, inherited: true) != null)
                                {
                                    return(true);
                                }
                            }
                        }

                        return(false);
                    }

                    if (GetWellKnownTypeId(symbol) is uint wellKnownTypeId)
                    {
                        metadataModel.WellKnownTypeIds.Add((symbol, wellKnownTypeId));
                    }

                    if (GetTypeAlias(symbol) is string typeAlias)
                    {
                        metadataModel.TypeAliases.Add((symbol, typeAlias));
                    }

                    if (ShouldGenerateSerializer(symbol))
                    {
                        var typeDescription = new SerializableTypeDescription(semanticModel, symbol, GetDataMembers(symbol), _libraryTypes);
                        metadataModel.SerializableTypes.Add(typeDescription);
                    }

                    if (symbol.TypeKind == TypeKind.Interface)
                    {
                        var attribute = HasAttribute(
                            symbol,
                            _libraryTypes.GenerateMethodSerializersAttribute,
                            inherited: true);
                        if (attribute != null)
                        {
                            var baseClass   = (INamedTypeSymbol)attribute.ConstructorArguments[0].Value;
                            var isExtension = (bool)attribute.ConstructorArguments[1].Value;
                            var description = new InvokableInterfaceDescription(
                                _libraryTypes,
                                semanticModel,
                                symbol,
                                GetMethods(symbol),
                                baseClass,
                                isExtension);
                            metadataModel.InvokableInterfaces.Add(description);
                        }
                    }

                    if ((symbol.TypeKind == TypeKind.Class || symbol.TypeKind == TypeKind.Struct) && !symbol.IsAbstract && (symbol.DeclaredAccessibility == Accessibility.Public || symbol.DeclaredAccessibility == Accessibility.Internal))
                    {
                        if (symbol.HasAttribute(_libraryTypes.RegisterSerializerAttribute))
                        {
                            metadataModel.DetectedSerializers.Add(symbol);
                        }

                        if (symbol.HasAttribute(_libraryTypes.RegisterActivatorAttribute))
                        {
                            metadataModel.DetectedActivators.Add(symbol);
                        }
                    }
                }
            }

            return(metadataModel);
        }