Beispiel #1
0
        public static void WriteBlock(ScalaWriter writer, BlockSyntax block, bool writeBraces = true)
        {
            if (writeBraces)
            {
                writer.WriteOpenBrace();
            }

            foreach (var statement in block.Statements)
            {
                Write(writer, statement);
            }

            TriviaProcessor.ProcessTrivias(writer, block.DescendantTrivia());

            if (writeBraces)
            {
                writer.WriteCloseBrace();
            }
        }
Beispiel #2
0
        public static void WriteStatementAsBlock(ScalaWriter writer, StatementSyntax statement, bool writeBraces = true)
        {
            if (statement is BlockSyntax)
            {
                WriteBlock(writer, statement.As <BlockSyntax>(), writeBraces);
            }
            else
            {
                if (writeBraces)
                {
                    writer.WriteOpenBrace();
                }

                Core.Write(writer, statement);
                TriviaProcessor.ProcessTrivias(writer, statement.DescendantTrivia());

                if (writeBraces)
                {
                    writer.WriteCloseBrace();
                }
            }
        }
Beispiel #3
0
        public static void Go(ScalaWriter writer, MethodDeclarationSyntax method)
        {
            if (method.Modifiers.Any(SyntaxKind.PartialKeyword) && method.Body == null)
            {
                //We only want to render out one of the two partial methods.  If there's another, skip this one.
                if (TypeState.Instance.Partials.SelectMany(o => o.Syntax.As <ClassDeclarationSyntax>().Members)
                    .OfType <MethodDeclarationSyntax>()
                    .Except(method)
                    .Where(o => o.Identifier.ValueText == method.Identifier.ValueText)
                    .Any())
                {
                    return;
                }
            }

            var methodSymbol = Program.GetModel(method).GetDeclaredSymbol(method);


            if (method.Identifier.ValueText == "GetEnumerator")
            {
                WriteGetEnumeratorFunction(writer, method, methodSymbol);
                return;
            }


            writer.WriteIndent();

            if (ShouldUseOverrideKeyword(method, methodSymbol))
            {
                writer.Write("override ");
            }
            if (method.Modifiers.Any(SyntaxKind.PrivateKeyword))
            {
                writer.Write("private ");
            }

            writer.Write("def ");
            var methodName = OverloadResolver.MethodName(methodSymbol);

            if (methodName == "ToString")
            {
                methodName = "toString";
            }
            else if (methodName == "Equals")
            {
                methodName = "equals";
            }
            else if (methodName == "GetHashCode")
            {
                methodName = "hashCode";
            }
            else if (methodName == "Main")
            {
                methodName = "main";
            }

            writer.Write(methodName);

            if (method.TypeParameterList != null)
            {
                writer.Write("[");
                writer.Write(string.Join(", ", method.TypeParameterList.Parameters.Select(o => TypeParameter(o, methodSymbol, method))));
                writer.Write("]");
            }

            writer.Write("(");

            var firstParam = true;

            foreach (var parameter in method.ParameterList.Parameters)
            {
                bool isRef = parameter.Modifiers.Any(SyntaxKind.OutKeyword) || parameter.Modifiers.Any(SyntaxKind.RefKeyword);

                if (firstParam)
                {
                    firstParam = false;
                }
                else
                {
                    writer.Write(", ");
                }

                writer.Write(WriteIdentifierName.TransformIdentifier(parameter.Identifier.ValueText));

                if (isRef)
                {
                    writer.Write(":CsRef[");
                    writer.Write(TypeProcessor.ConvertType(parameter.Type));
                    writer.Write("]");

                    Program.RefOutSymbols.TryAdd(Program.GetModel(method).GetDeclaredSymbol(parameter), null);
                }
                else
                {
                    writer.Write(TypeProcessor.ConvertTypeWithColon(parameter.Type));
                }

                if (parameter.Default != null)
                {
                    writer.Write(" = ");
                    Core.Write(writer, parameter.Default.Value);
                }
            }

            writer.Write(")");
            bool returnsVoid = method.ReturnType.ToString() == "void";

            if (!returnsVoid)
            {
                writer.Write(TypeProcessor.ConvertTypeWithColon(method.ReturnType));
            }

            if (method.Modifiers.Any(SyntaxKind.AbstractKeyword) || method.Parent is InterfaceDeclarationSyntax)
            {
                writer.Write(";\r\n");
            }
            else
            {
                if (!returnsVoid)
                {
                    writer.Write(" =");
                }

                writer.Write("\r\n");
                writer.WriteOpenBrace();

                if (method.Body != null)
                {
                    foreach (var statement in method.Body.Statements)
                    {
                        Core.Write(writer, statement);
                    }

                    TriviaProcessor.ProcessTrivias(writer, method.Body.DescendantTrivia());
                }

                writer.WriteCloseBrace();
            }
        }
        private static void Go(ScalaWriter writer, IEnumerable <ParameterSyntax> parameters, SyntaxNode body, TypeInfo type)
        {
            var methodSymbol = type.ConvertedType.As <INamedTypeSymbol>().DelegateInvokeMethod.As <IMethodSymbol>();

            writer.Write("(");

            for (int pi = 0; pi < parameters.Count(); pi++)
            {
                var parameter = parameters.ElementAt(pi);
                if (pi > 0)
                {
                    writer.Write(", ");
                }

                writer.Write(WriteIdentifierName.TransformIdentifier(parameter.Identifier.ValueText));
                if (parameter.Type != null)
                {
                    writer.Write(TypeProcessor.ConvertTypeWithColon(parameter.Type));
                }
                else
                {
                    writer.Write(TypeProcessor.ConvertTypeWithColon(methodSymbol.Parameters[pi].Type));
                }
            }

            writer.Write(") => ");

            bool returnsVoid = methodSymbol.ReturnType.ToString() == "void";

            if (body is BlockSyntax)
            {
                writer.Write("\r\n");
                writer.WriteOpenBrace();

                var statements = body.As <BlockSyntax>().Statements;

                var lastStatement = statements.LastOrDefault() as ReturnStatementSyntax;

                var returnStatements = FindReturnStatements(body);

                if (returnStatements.Count > 0 && (lastStatement == null || returnStatements.Except(lastStatement).Any()))
                {
                    //Lambda has branching returns.  We must use a breakable block since scala can't return from a lambda like C# can
                    TypeState.Instance.InLambdaBreakable++;

                    writer.WriteLine("val __lambdabreak = new Breaks;");

                    if (!returnsVoid)
                    {
                        writer.WriteIndent();
                        writer.Write("var __lambdareturn:");
                        writer.Write(TypeProcessor.ConvertType(methodSymbol.ReturnType));
                        writer.Write(" = ");
                        writer.Write(TypeProcessor.DefaultValue(methodSymbol.ReturnType));
                        writer.Write(";\r\n");
                    }

                    writer.WriteLine("__lambdabreak.breakable");
                    writer.WriteOpenBrace();

                    foreach (var statement in statements)
                    {
                        if (statement == lastStatement && !returnsVoid)
                        {
                            //Manually write it so we avoid the final break that WriteReturnStatement does
                            writer.WriteIndent();
                            writer.Write("__lambdareturn = ");
                            Core.Write(writer, lastStatement.Expression);
                            writer.Write(";\r\n");
                        }
                        else
                        {
                            Core.Write(writer, statement);
                        }
                    }

                    TriviaProcessor.ProcessTrivias(writer, body.DescendantTrivia());

                    writer.WriteCloseBrace();

                    if (!returnsVoid)
                    {
                        writer.WriteLine("__lambdareturn;");
                    }

                    TypeState.Instance.InLambdaBreakable--;
                }
                else
                {
                    foreach (var statement in statements)
                    {
                        if (statement == lastStatement)
                        {
                            writer.WriteIndent();
                            Core.Write(writer, lastStatement.Expression);
                            writer.Write(";\r\n");
                        }
                        else
                        {
                            Core.Write(writer, statement);
                        }
                    }
                }

                writer.Indent--;
                writer.WriteIndent();
                writer.Write("}");
            }
            else
            {
                writer.Write(" { ");
                Core.Write(writer, body);
                writer.Write("; }");
            }

            if (!returnsVoid)
            {
                writer.Write(TypeProcessor.ConvertTypeWithColon(methodSymbol.ReturnType));
            }
        }