Пример #1
0
        public StaticField GenerateNewField(FuzzType type)
        {
            type = type ?? Types.PickType();

            string      name  = "s_" + (++_counter);
            StaticField field = new StaticField(new VariableIdentifier(type, name), LiteralGenerator.GenLiteral(Random, type));

            _fields.Add(field);
            return(field);
        }
Пример #2
0
        /// <summary>
        /// If a line only has a directive or block statement, then including it in the output
        /// will result in an empty line (possibly with some white space). To make the output
        /// cleaner, we need to suppress these empty lines. To suppress the empty lines, search
        /// through StatementGenerators looking for lines that consist of only white space,
        /// directives and blocks. When we find such a line, we delete all the literals, thus
        /// deleting the white space and the newline.
        /// </summary>
        protected void OptimizeNewlines()
        {
            int iLineStart = 0;

            for (int i = 0; i < statementGenerators.Count; i++)
            {
                if (statementGenerators[i].GetType() == typeof(LiteralGenerator))
                {
                    LiteralGenerator generatorLiteral = (LiteralGenerator)statementGenerators[i];
                    if (generatorLiteral.Text.Contains("\r\n"))
                    {
                        int iLineEnd = i; // Current "line" runs from iLineStartGenerator to iLineEndGenerator
                        if (iLineStart == iLineEnd)
                        {
                            iLineStart++;
                            continue;
                        }
                        bool suppressLine = true;
                        for (int iCheckGenerator = iLineStart; iCheckGenerator <= iLineEnd; iCheckGenerator++)
                        {
                            if (statementGenerators[iCheckGenerator].GetType() == typeof(ExpressionGenerator))
                            {
                                // Expressions always cause output
                                suppressLine = false;
                                break;
                            }
                            else if (statementGenerators[iCheckGenerator].GetType() == typeof(LiteralGenerator))
                            {
                                LiteralGenerator checkLiteral = (LiteralGenerator)statementGenerators[iCheckGenerator];
                                if (checkLiteral.Text.Trim() != "")
                                {
                                    suppressLine = false;
                                    break;
                                }
                            }
                        }
                        if (suppressLine)
                        {
                            // All the literals between iLineStartGenerator and iLineEndGenerator are all white space, so delete them
                            for (int iDeleteGenerator = iLineEnd; iDeleteGenerator >= iLineStart; iDeleteGenerator--)
                            {
                                if (statementGenerators[iDeleteGenerator].GetType() == typeof(LiteralGenerator))
                                {
                                    statementGenerators.RemoveAt(iDeleteGenerator);
                                    iLineEnd--;
                                }
                            }
                        }
                        iLineStart = iLineEnd + 1;
                        i          = iLineEnd;
                    }
                }
            }
        }
Пример #3
0
        private string GenerateLambdaExpression(List <ParameterSyntax> parameters, INamedTypeSymbol typeSymbol, CSharpSyntaxNode body, SemanticModel semanticModel)
        {
            var           delegateInfo = GetDelegateInfo(typeSymbol, semanticModel);
            string        delegateType = delegateInfo.JavaDelegateType;
            TypeReference returnType   = delegateInfo.ReturnType;

            bool isFunc = delegateInfo.IsFunc;

            var generatedParameters = new List <Var>();

            for (int i = 0; i < parameters.Count; i++)
            {
                var parameter = parameters[i];
                var typeArgumentForParameter = typeSymbol.TypeArguments[i];
                var typeReference            = TypeReferenceGenerator.GenerateTypeReference(typeArgumentForParameter, semanticModel, isInGenericContext: true);
                generatedParameters.Add(new Var
                {
                    Name = parameter.Identifier.ValueText,
                    Type = typeReference
                });
            }
            var statements = new List <string>();

            if (body is BlockSyntax)
            {
                foreach (var statement in (body as BlockSyntax).Statements)
                {
                    string generatedStatement = StatementGenerator.Generate(statement, semanticModel);
                    statements.Add(generatedStatement);
                }
            }
            else if (body is InvocationExpressionSyntax)
            {
                string generatedInvocationExpression = GenerateInvocationExpression(body as InvocationExpressionSyntax,
                                                                                    semanticModel);
                string statement = generatedInvocationExpression + ";";
                if (!Equals(returnType, JavaTypeReferences.Void))
                {
                    statement = "return " + statement;
                }
                statements.Add(statement);
            }
            else if (body is LiteralExpressionSyntax)
            {
                string statement = "return " + LiteralGenerator.Generate(body as LiteralExpressionSyntax, semanticModel) + ";";
                statements.Add(statement);
            }
            else if (body is BinaryExpressionSyntax)
            {
                string statement = "return " + GenerateExpression(body as BinaryExpressionSyntax, semanticModel) + ";";
                statements.Add(statement);
            }
            return(BuildLambdaExpression(delegateType, generatedParameters, returnType, statements, semanticModel));
        }
Пример #4
0
 private ExpressionSyntax GenLiteral(FuzzType type)
 {
     return(LiteralGenerator.GenLiteral(Random, type));
 }
Пример #5
0
        public string GenerateExpression(ExpressionSyntax expressionSyntax, SemanticModel semanticModel)
        {
            if (expressionSyntax is ObjectCreationExpressionSyntax)
            {
                return(GenerateObjectCreationExpression(expressionSyntax as ObjectCreationExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is AssignmentExpressionSyntax)
            {
                return(GenerateAssignmentExpression(expressionSyntax as AssignmentExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is LiteralExpressionSyntax)
            {
                return(LiteralGenerator.Generate(expressionSyntax as LiteralExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is IdentifierNameSyntax)
            {
                return(GenerateIdentifierExpression(expressionSyntax as IdentifierNameSyntax, semanticModel));
            }
            if (expressionSyntax is BinaryExpressionSyntax)
            {
                var    binaryExpressionSyntax = (expressionSyntax as BinaryExpressionSyntax);
                string leftExpression         = GenerateExpression(binaryExpressionSyntax.Left, semanticModel);
                string rightExpression        = GenerateExpression(binaryExpressionSyntax.Right, semanticModel);
                string operatorString         = binaryExpressionSyntax.OperatorToken.Text;

                var leftExpressionType = (INamedTypeSymbol)ModelExtensions.GetTypeInfo(semanticModel, binaryExpressionSyntax.Left).Type;
                if (CustomBinaryExpressionHelper.IsCustom(leftExpressionType, operatorString))
                {
                    return(CustomBinaryExpressionHelper.Generate(leftExpressionType, leftExpression, operatorString,
                                                                 rightExpression, semanticModel));
                }
                else
                {
                    return(leftExpression + " " + operatorString + " " + rightExpression);
                }
            }
            if (expressionSyntax is PostfixUnaryExpressionSyntax)
            {
                return(GeneratePostfixUnaryExpression(expressionSyntax as PostfixUnaryExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is PrefixUnaryExpressionSyntax)
            {
                return(GeneratePrefixUnaryExpression(expressionSyntax as PrefixUnaryExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is ParenthesizedExpressionSyntax)
            {
                return("("
                       + GenerateExpression((expressionSyntax as ParenthesizedExpressionSyntax).Expression, semanticModel)
                       + ")");
            }
            if (expressionSyntax is MemberAccessExpressionSyntax)
            {
                return(GenerateMemberAccessExpression(expressionSyntax as MemberAccessExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is ElementAccessExpressionSyntax)
            {
                return(GenerateElementAccessExpression(expressionSyntax as ElementAccessExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is InvocationExpressionSyntax)
            {
                return(GenerateInvocationExpression(expressionSyntax as InvocationExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is ParenthesizedLambdaExpressionSyntax)
            {
                return(GenerateParenthesizedLambdaExpression(expressionSyntax as ParenthesizedLambdaExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is SimpleLambdaExpressionSyntax)
            {
                return(GenerateSimpleLambdaExpression(expressionSyntax as SimpleLambdaExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is CastExpressionSyntax)
            {
                return(GenerateCastExpression(expressionSyntax as CastExpressionSyntax, semanticModel));
            }
            if (expressionSyntax is ThisExpressionSyntax)
            {
                return(GenerateThisExpression(expressionSyntax as ThisExpressionSyntax, semanticModel));
            }
            throw new NotImplementedException();
        }