public IEnumerable <SyntaxNode> GenerateImplementation(IMethodSymbol methodSymbol, SyntaxGenerator generator, SemanticModel semanticModel)
        {
            var mappingEngine = new MappingEngine(semanticModel, generator, methodSymbol.ContainingAssembly);
            var targetType    = methodSymbol.ReturnType;
            var newExpression = mappingEngine.MapExpression((ExpressionSyntax)generator.ThisExpression(), methodSymbol.ContainingType, targetType);

            return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) });
        }
Beispiel #2
0
        public IEnumerable <SyntaxNode> GenerateImplementation(IMethodSymbol methodSymbol, SyntaxGenerator generator,
                                                               SemanticModel semanticModel, MappingContext mappingContext)
        {
            var mappingEngine   = new MappingEngine(semanticModel, generator);
            var destinationType = new AnnotatedType(methodSymbol.ReturnType);
            var sourceType      = new AnnotatedType(methodSymbol.ContainingType);
            var newExpression   = mappingEngine.MapExpression((ExpressionSyntax)generator.ThisExpression(), sourceType, destinationType, mappingContext);

            return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) });
        }
Beispiel #3
0
        public IEnumerable <SyntaxNode> GenerateImplementation(IMethodSymbol methodSymbol, SyntaxGenerator generator,
                                                               SemanticModel semanticModel, MappingContext mappingContext)
        {
            var mappingEngine = new MappingEngine(semanticModel, generator);
            var source        = methodSymbol.Parameters[0];
            var targetType    = methodSymbol.ReturnType;
            var newExpression = mappingEngine.MapExpression((ExpressionSyntax)generator.IdentifierName(source.Name),
                                                            source.Type, targetType, mappingContext);

            return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) });
        }
        public async Task <IReadOnlyList <SyntaxNode> > GenerateImplementation(IMethodSymbol methodSymbol, SyntaxGenerator generator,
                                                                               SemanticModel semanticModel, MappingContext mappingContext)
        {
            var mappingEngine = new MappingEngine(semanticModel, generator);
            var source        = methodSymbol.Parameters[0];
            var sourceType    = new AnnotatedType(source.Type);
            var targetType    = new AnnotatedType(methodSymbol.ReturnType);
            var newExpression = await mappingEngine.MapExpression((ExpressionSyntax)generator.IdentifierName(source.Name), sourceType, targetType, mappingContext).ConfigureAwait(false);

            return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) });
        }
        public ArgumentListSyntax ToArgumentListSyntax(MappingEngine mappingEngine, MappingContext mappingContext, bool generateNamedParameters = true)
        {
            return(Matches.Aggregate(SyntaxFactory.ArgumentList(), (list, match) =>
            {
                if (match.Source?.Expression == null && match.Parameter.IsOptional)
                {
                    generateNamedParameters = true;
                    return list;
                }

                var parameterType = new AnnotatedType(match.Parameter.Type);
                var expression = mappingEngine.MapExpression(match.Source, parameterType, mappingContext)?.Expression ?? mappingEngine.CreateDefaultExpression(parameterType.Type);
                var argument = generateNamedParameters
                    ? SyntaxFactory.Argument(SyntaxFactory.NameColon(match.Parameter.Name), SyntaxFactory.Token(SyntaxKind.None), expression)
                    : SyntaxFactory.Argument(expression);
                return list.AddArguments(argument);
            }));
        }
        public async System.Threading.Tasks.Task <ArgumentListSyntax> ToArgumentListSyntaxAsync(MappingEngine mappingEngine, MappingContext mappingContext, bool generateNamedParameters = true)
        {
            var resultList = SyntaxFactory.ArgumentList();

            foreach (var match in Matches)
            {
                if (match.Source?.Expression == null && match.Parameter.IsOptional)
                {
                    generateNamedParameters = true;
                    continue;
                }

                var parameterType = new AnnotatedType(match.Parameter.Type);
                var mapExpression = await mappingEngine.MapExpression(match.Source, parameterType, mappingContext).ConfigureAwait(false);

                var expression = mapExpression?.Expression ?? mappingEngine.CreateDefaultExpression(parameterType.Type);
                var argument   = generateNamedParameters
                    ? SyntaxFactory.Argument(SyntaxFactory.NameColon(match.Parameter.Name), SyntaxFactory.Token(SyntaxKind.None), expression)
                    : SyntaxFactory.Argument(expression);
                resultList = resultList.AddArguments(argument);
            }

            return(resultList);
        }