コード例 #1
0
        public ExplicitBindExpressionFactory(
            ITypeInfoContainer typeInfoContainer,
            SemanticModelDecorator semanticModel,
            ConstructorArgumentFromSyntaxExtractor extractor,
            ConstructorArgumentDetector constructorArgumentDetector
            )
        {
            if (typeInfoContainer is null)
            {
                throw new ArgumentNullException(nameof(typeInfoContainer));
            }

            if (semanticModel is null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (extractor is null)
            {
                throw new ArgumentNullException(nameof(extractor));
            }

            if (constructorArgumentDetector is null)
            {
                throw new ArgumentNullException(nameof(constructorArgumentDetector));
            }

            _typeInfoContainer           = typeInfoContainer;
            _semanticModel               = semanticModel;
            _extractor                   = extractor;
            _constructorArgumentDetector = constructorArgumentDetector;
        }
コード例 #2
0
ファイル: RoslynHelper.cs プロジェクト: lsoft/DpdtInject
        public static bool TryGetCompileTimeString(
            this ExpressionSyntax expression,
            SemanticModelDecorator semanticModel,
            [NotNullWhen(true)] out string?result
            )
        {
            if (expression is null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (semanticModel is null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (expression is LiteralExpressionSyntax literal)
            {
                result = literal.Token.ValueText;
                return(true);
            }

            var constant = semanticModel.GetConstantValue(expression);

            if (constant.HasValue)
            {
                result = constant.Value !.ToString() !;
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #3
0
        public ConstantParsedBindExpression(
            SemanticModelDecorator semanticModel,
            ExpressionStatementSyntax expressionNode,
            List <Tuple <InvocationExpressionSyntax, IMethodSymbol> > invocationSymbols
            ) : base(BindScopeEnum.Constant, invocationSymbols)
        {
            if (semanticModel is null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (expressionNode is null)
            {
                throw new ArgumentNullException(nameof(expressionNode));
            }

            if (invocationSymbols is null)
            {
                throw new ArgumentNullException(nameof(invocationSymbols));
            }

            _semanticModel     = semanticModel;
            _expressionNode    = expressionNode;
            _invocationSymbols = invocationSymbols;

            var constantClause = DetermineArgumentSubClause(
                invocationSymbols,
                typeof(IToOrConstantBinding).GetMethod(nameof(IToOrConstantBinding.WithConstScope), BindingFlags.Public | BindingFlags.Instance) !
                );

            if (constantClause is null)
            {
                throw new DpdtException(
                          DpdtExceptionTypeEnum.InternalError,
                          $"Cannot find constant clause"
                          );
            }

            _constantClause = constantClause;

            _from = invocationSymbols.First(
                s => s.Item2.ContainingType.ToGlobalDisplayString() == typeof(DefaultCluster).ToGlobalDisplayString() && s.Item2.Name == DefaultCluster.BindMethodName
                );
            _fromTypes = _from.Item2.TypeArguments;

            if (_fromTypes.Any(t => t is IDynamicTypeSymbol))
            {
                throw new DpdtException(
                          DpdtExceptionTypeEnum.IncorrectBinding_IncorrectFrom,
                          $"Dynamic cannot be used as bind from type"
                          );
            }

            _constScope = invocationSymbols.First(
                s => s.Item2.ContainingType.ToGlobalDisplayString() == typeof(IToOrConstantBinding).ToGlobalDisplayString() && s.Item2.Name == nameof(IToOrConstantBinding.WithConstScope)
                );

            //euristric if constant is a dynamic actually
            if (_constScope.Item2.TypeArguments[0].TypeKind == TypeKind.TypeParameter)
            {
                throw new DpdtException(
                          DpdtExceptionTypeEnum.IncorrectBinding_IncorrectFrom,
                          $"Dynamic cannot be used as bind from type"
                          );
            }

            _whenArgumentClause = DetermineArgumentSubClause(
                invocationSymbols,
                typeof(IConditionalBinding).GetMethod(nameof(IConditionalBinding.When), BindingFlags.Public | BindingFlags.Instance) !,
                typeof(IConstantConditionalBinding).GetMethod(nameof(IConstantConditionalBinding.When), BindingFlags.Public | BindingFlags.Instance) !
                );
        }
コード例 #4
0
        public IReadOnlyList <ClusterMethodBindings> DoExtraction(
            ITypeInfoContainer typeInfoContainer
            )
        {
            if (typeInfoContainer is null)
            {
                throw new ArgumentNullException(nameof(typeInfoContainer));
            }

            var scanner = new DefaultTypeScanner(
                );

            var clusterTypes = scanner.ScanForClusterTypes(
                typeInfoContainer
                );

            var stepResults = new List <ClusterMethodBindings>();

            for (var clusterTypeIndex = 0; clusterTypeIndex < clusterTypes.Count; clusterTypeIndex++)
            {
                var clusterType = clusterTypes[clusterTypeIndex];

                clusterType.ScanForRequiredSyntaxes(
                    out List <MethodDeclarationSyntax> bindMethodSyntaxes,
                    out List <CompilationUnitSyntax> compilationUnitSyntaxes
                    );

                if (compilationUnitSyntaxes.Count == 0)
                {
                    throw new DpdtException(
                              DpdtExceptionTypeEnum.InternalError,
                              $"Unknown problem to access to compilation unit syntax"
                              );
                }

                var semanticModels   = new List <SemanticModel>();
                var moduleUnitUsings = new List <UsingDirectiveSyntax>();
                foreach (var compilationUnitSyntax in compilationUnitSyntaxes)
                {
                    var moduleUnitUsing = compilationUnitSyntax
                                          .DescendantNodes()
                                          .OfType <UsingDirectiveSyntax>()
                                          .ToList();
                    moduleUnitUsings.AddRange(moduleUnitUsing);

                    var semanticModel = typeInfoContainer.GetSemanticModel(compilationUnitSyntax.SyntaxTree);
                    semanticModels.Add(semanticModel);
                }

                var semanticModelDecorator = new SemanticModelDecorator(
                    semanticModels
                    );

                var stepResult = new ClusterMethodBindings(
                    clusterType,
                    moduleUnitUsings
                    );

                foreach (var bindMethodSyntax in bindMethodSyntaxes)
                {
                    var bindExtractor =
                        new BindClauseExtractor(
                            semanticModelDecorator,
                            new DetermineBindExpressionFactory(
                                new ExplicitBindExpressionFactory(
                                    typeInfoContainer,
                                    semanticModelDecorator,
                                    new ConstructorArgumentFromSyntaxExtractor(
                                        semanticModelDecorator
                                        ),
                                    new ConstructorArgumentDetector(
                                        BindConstructorChooser.Instance
                                        )
                                    ),
                                new ConventionalBindExpressionFactory(
                                    typeInfoContainer,
                                    new ConstructorArgumentFromSyntaxExtractor(
                                        semanticModelDecorator
                                        ),
                                    new ConstructorArgumentDetector(
                                        BindConstructorChooser.Instance
                                        ),
                                    _diagnosticReporter
                                    )
                                )
                            );

                    bindExtractor.Visit(bindMethodSyntax);

                    stepResult.AddMethodBindings(
                        bindMethodSyntax,
                        bindExtractor.BindingContainers
                        );
                }

                stepResults.Add(stepResult);
            }

            return(stepResults);
        }