public static void Write(ScalaWriter writer, SyntaxNode node, bool isConst = false) { TriviaProcessor.ProcessNode(writer, node); if (Program.DoNotWrite.ContainsKey(node)) { return; } var exprOpt = node as ExpressionSyntax; string postFactory = null; if (exprOpt != null) { var type = Program.GetModel(node).GetTypeInfo(exprOpt); if (type.Type != null && type.Type.SpecialType == SpecialType.System_Byte && type.ConvertedType.SpecialType != SpecialType.System_Byte && Utility.IsNumeric(type.ConvertedType)) { //Bytes are signed in the JVM, so we have to take care when up-converting them writer.Write("System.CsScala.ByteTo"); writer.Write(TypeProcessor.ConvertType(type.ConvertedType)); writer.Write("("); postFactory = ")"; } } Factory(writer, node, isConst); writer.Write(postFactory); }
private static void Generate() { Console.WriteLine("Parsing..."); var sw = Stopwatch.StartNew(); if (!Directory.Exists(OutDir)) { Directory.CreateDirectory(OutDir); } var allTypes = Compilation.SyntaxTrees .SelectMany(o => o.GetRoot().DescendantNodes().OfType <BaseTypeDeclarationSyntax>()) .Select(o => new { Syntax = o, Symbol = GetModel(o).GetDeclaredSymbol(o), TypeName = WriteType.TypeName(GetModel(o).GetDeclaredSymbol(o)) }) .GroupBy(o => o.Symbol.ContainingNamespace.FullNameWithDot() + o.TypeName) .ToList(); Utility.Parallel(Compilation.SyntaxTrees.ToList(), tree => { foreach (var n in TriviaProcessor.DoNotWrite(tree)) { DoNotWrite.TryAdd(n, null); } //Init ClassTags foreach (var method in tree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>().Where(o => o.TypeParameterList != null)) { ClassTags.InitMethod(GetModel(method).GetDeclaredSymbol(method), method); } }); Console.WriteLine("Parsed in " + sw.Elapsed + ". Writing out scala..."); sw.Restart(); Compilation.SyntaxTrees.SelectMany(o => o.GetRoot().DescendantNodes().OfType <AnonymousObjectCreationExpressionSyntax>()) .Select(o => new { Syntax = o, Name = WriteAnonymousObjectCreationExpression.TypeName(o) }) .GroupBy(o => o.Name) .Parallel(o => WriteAnonymousObjectCreationExpression.WriteAnonymousType(o.First().Syntax)); allTypes.Parallel(type => { TypeState.Instance = new TypeState(); TypeState.Instance.TypeName = type.First().TypeName; TypeState.Instance.Partials = type.Select(o => new TypeState.SyntaxAndSymbol { Symbol = o.Symbol, Syntax = o.Syntax }) .Where(o => !DoNotWrite.ContainsKey(o.Syntax)) .ToList(); if (TypeState.Instance.Partials.Count > 0) { WriteType.Go(); } }); WriteConstructorBody.WriteConstructorsHelper(allTypes.SelectMany(o => o).Where(o => !DoNotWrite.ContainsKey(o.Syntax)).Select(o => o.Symbol)); Console.WriteLine("Scala written out in " + sw.Elapsed); }
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(); } }
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(); } } }
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)); } }