Esempio n. 1
0
        private IExpression ConvertArrayCreation(ArrayCreationExpressionSyntax creationExpression)
        {
            var ixe       = new XArrayCreateExpression();
            var arrayType = (IArrayTypeSymbol)model.GetTypeInfo(creationExpression).Type;

            ixe.Type = ConvertTypeReference(arrayType.ElementType);

            if (creationExpression.Initializer == null)
            {
                foreach (var size in creationExpression.Type.RankSpecifiers.First().Sizes)
                {
                    ixe.Dimensions.Add(ConvertExpression(size));
                }
                return(ixe);
            }

            var arrayInitSyntax = creationExpression.Initializer;

            while (arrayInitSyntax != null)
            {
                ixe.Dimensions.Add(new XLiteralExpression {
                    Value = arrayInitSyntax.Expressions.Count
                });
                if (arrayInitSyntax.Expressions.Count == 0)
                {
                    break;
                }
                var firstExpr = arrayInitSyntax.Expressions.First();
                arrayInitSyntax = firstExpr as InitializerExpressionSyntax;
            }
            var initializers = creationExpression.Initializer.Expressions.Select(expr => ConvertExpression(expr)).ToList();

            ixe.Initializer = new XBlockExpression();
            ixe.Initializer.Expressions.AddRange(initializers);
            return(ixe);
        }
Esempio n. 2
0
        private IExpression ConvertInvocation(InvocationExpressionSyntax invocationExpression)
        {
            var c = model.GetConstantValue(invocationExpression);

            if (c.HasValue)
            {
                // compile time constant
                // including nameof(thing)
                return(ConvertConstantExpr(invocationExpression));
            }

            var methodExpression = invocationExpression.Expression;
            var argumentList     = invocationExpression.ArgumentList.Arguments;
            var convertedArgs    = argumentList.Select(arg => ConvertExpression(arg.Expression)).ToList();

            if (argumentList.Any(arg => arg.NameColon != null))
            {
                throw new NotSupportedException("No support yet for named parameters");
            }

            if (model.GetSymbolInfo(invocationExpression).Symbol is IMethodSymbol methodSymbol &&
                methodSymbol.Parameters.Length > 0)
            {
                var lastParam = methodSymbol.Parameters.Last();
                if (lastParam.IsParams && lastParam.Type is IArrayTypeSymbol paramsType)
                {
                    IExpression[] paramsParameters = null;
                    if (argumentList.Count() == methodSymbol.Parameters.Count())
                    {
                        // one element or array supplied?
                        var ti = model.GetTypeInfo(argumentList.Last().Expression);
                        if (!(ti.Type is IArrayTypeSymbol))
                        {
                            paramsParameters = new[] { convertedArgs.Last() };
                        }
                    }
                    else
                    {
                        paramsParameters = convertedArgs.Skip(methodSymbol.Parameters.Length - 1).ToArray();
                    }
                    if (paramsParameters != null)
                    {
                        var ixe = new XArrayCreateExpression();
                        ixe.Type = ConvertTypeReference(paramsType.ElementType);
                        ixe.Dimensions.Add(new XLiteralExpression {
                            Value = paramsParameters.Length
                        });
                        ixe.Initializer = new XBlockExpression();
                        ixe.Initializer.Expressions.AddRange(paramsParameters);

                        convertedArgs.RemoveRange(convertedArgs.Count - paramsParameters.Length, paramsParameters.Length);
                        convertedArgs.Add(ixe);
                    }
                }
            }

            var imie = Builder.MethodInvkExpr();

            imie.Arguments.AddRange(convertedArgs);
            imie.Method = (IMethodReferenceExpression)ConvertExpression(methodExpression);
            return(imie);
        }