/// <summary> /// calls to Enum.Parse get re-written as calls to our special Parse methods on each enum. We assume the first /// parameter to Enum.Parse is a a typeof() /// </summary> private static void WriteEnumParse(OutputWriter writer, InvocationExpressionSyntax invocationExpression) { var args = invocationExpression.ArgumentList.Arguments; if (args.Count < 2 || args.Count > 3) { throw new Exception("Expected 2-3 args to Enum.Parse"); } if (args.Count == 3 && (!(args[2].Expression is LiteralExpressionSyntax) || args[2].Expression.As <LiteralExpressionSyntax>().ToString() != "false")) { throw new NotImplementedException("Case-insensitive Enum.Parse is not supported " + Utility.Descriptor(invocationExpression)); } if (!(args[0].Expression is TypeOfExpressionSyntax)) { throw new Exception("Expected a typeof() expression as the first parameter of Enum.Parse " + Utility.Descriptor(invocationExpression)); } var type = TypeProcessor.GetTypeInfo(args[0].Expression.As <TypeOfExpressionSyntax>().Type).Type; //ModelExtensions.GetTypeInfo(Program.GetModel(invocationExpression), args[0].Expression.As<TypeOfExpressionSyntax>().Type).Type; writer.Write(type.ContainingNamespace.FullNameWithDot()); writer.Write(WriteType.TypeName((INamedTypeSymbol)type)); writer.Write(".Parse("); Core.Write(writer, args[1].Expression); writer.Write(")"); }
public static string GetNameD(this ITypeSymbol type, bool removeGenerics = true) { var convertType = TypeProcessor.ConvertType(type, false, false, false); var name = convertType.RemoveFromStartOfString(type.ContainingNamespace.FullName(true, false) + "."); return(removeGenerics ? Regex.Replace(name, @" ?!\(.*?\)", string.Empty) : name); }
private static void WriteDelegateAssignment(OutputWriter writer, ITypeSymbol convertedType, bool isstaticdelegate, ExpressionSyntax value) { var typeString = TypeProcessor.ConvertType(convertedType); if (convertedType.TypeKind == TypeKind.TypeParameter) { writer.Write(" __TypeNew!(" + typeString + ")("); } else { writer.Write("new " + typeString + "("); } var isStatic = isstaticdelegate; // if (isStatic) // writer.Write("__ToDelegate("); MemberUtilities.WriteMethodPointer(writer, value); // if (isStatic) // writer.Write(")"); writer.Write(")"); return; }
private static void WriteThreadStatic(OutputWriter writer, VariableDeclaratorSyntax declaration, EventFieldDeclarationSyntax field) { var type = TypeProcessor.ConvertType(field.Declaration.Type); throw new NotImplementedException("ThreadStatic"); }
public static string ConvertPInvokeType(ITypeSymbol returnType, PInvokeMode mode = PInvokeMode.None) { var dType = TypeProcessor.ConvertType(returnType); if (returnType.TypeKind == TypeKind.Delegate) { return(GetDelegateRawType(dType)); // var dlg =returnType.OriginalDefinition as IMethodSymbol; // if (dlg.Parameters.Length == 0) //// return "() => " + TryConvertType (dlg.ReturnType); //// else //// return "(" + string.Join (", ", dlg.Parameters.ToList ().Select (o => TryConvertType (o.Type))) + ") => " + TryConvertType (dlg.ReturnType); // // // // return TypeProcessor.ConvertType (dlg.ReturnType) + " delegate" + // dlg.Parameters.ToList ().Select (o => ConvertPInvokeType (o.Type)) + ")"; // return dType.RemoveFromStartOfString("Delegate!(").SubstringBeforeLast(')'); } //TODO this should become a class with different options like CharSet etc .. switch (dType) { case "String": return("char *"); case "Array_T!(String)": return("char**"); } return(dType); }
private static bool ReturnsVoid(SyntaxNode node) { while (node != null) { var method = node as MethodDeclarationSyntax; if (method != null) { return(method.ReturnType.ToString() == "void"); } var prop = node as PropertyDeclarationSyntax; if (prop != null) { return(prop.Type.ToString() == "void"); } var lambda1 = node as ParenthesizedLambdaExpressionSyntax; var lambda2 = node as SimpleLambdaExpressionSyntax; if (lambda1 != null || lambda2 != null) { var lambda = lambda1 == null ? lambda2 : (ExpressionSyntax)lambda1; var methodSymbol = TypeProcessor.GetTypeInfo(lambda) .ConvertedType.As <INamedTypeSymbol>() .DelegateInvokeMethod.As <IMethodSymbol>(); return(methodSymbol.ReturnsVoid); } node = node.Parent; } throw new Exception("Node not in a body"); }
//TODO should enum be light class of static members ? or just a plain enum ? (using plain enum for now) public static void Go(OutputWriter writer, IEnumerable <EnumMemberDeclarationSyntax> allChildren) { // writer.IsInterface = true; writer.Write("enum "); writer.Write(Context.Instance.TypeName); writer.Write(":" + TypeProcessor.ConvertType(Context.Instance.Type.EnumUnderlyingType) + "\r\n"); writer.OpenBrace(); long lastEnumValue = 0; var children = allChildren.ToArray(); var values = children.Select( o => new { Syntax = o, Value = o.EqualsValue != null ? o.EqualsValue.Value.ToString() : null }) .ToList(); foreach (var value in values) { writer.WriteLine( WriteIdentifierName.TransformIdentifier(value.Syntax.Identifier.ValueText) + (value.Value != null ? " = " + value.Value : "") + ","); } // writer.WriteLine("final val " + WriteIdentifierName.TransformIdentifier(value.Syntax.Identifier.ValueText) + ":Int = " + value.Value + ";"); // writer.WriteLine(); // writer.WriteLine(@"def ToString(n:java.lang.Integer):String = if (n == null) """" else ToString(n.intValue());"); // // writer.WriteLine("def ToString(e:Int):String ="); // writer.WriteOpenBrace(); // writer.WriteLine("return e match"); // writer.WriteOpenBrace(); // // foreach (var value in values) // writer.WriteLine("case " + value.Value + " => \"" + value.Syntax.Identifier.ValueText + "\";"); // // writer.WriteCloseBrace(); // writer.WriteCloseBrace(); // // writer.WriteLine(); // writer.WriteLine("def Parse(s:String):Int ="); // writer.WriteOpenBrace(); // writer.WriteLine("return s match"); // writer.WriteOpenBrace(); // // foreach (var value in values) // writer.WriteLine("case \"" + value.Syntax.Identifier.ValueText + "\" | \"" + value.Value + "\" => " + value.Value + ";"); // // writer.WriteCloseBrace(); // writer.WriteCloseBrace(); // // writer.WriteLine(); // writer.WriteIndent(); // writer.Write("final val Values:Array[Int] = Array("); // writer.Write(string.Join(", ", values.Select(o => o.Value.ToString()))); // writer.Write(");\r\n"); writer.CloseBrace(); writer.Write(";"); }
public static void Go(OutputWriter writer, TypeOfExpressionSyntax expression) { writer.Write("__TypeOf!("); TypeProcessor.AddUsedType(TypeProcessor.GetTypeInfo(expression.Type).Type); writer.Write(TypeProcessor.ConvertType(expression.Type)); writer.Write(")"); }
private static bool WriteConverter(OutputWriter writer, ITypeSymbol type, ITypeSymbol convertedType, ExpressionSyntax value) { bool useType = true; var correctConverter = type.GetImplicitCoversionOp(convertedType, type, true); if (correctConverter == null) { useType = false; correctConverter = convertedType.GetImplicitCoversionOp(convertedType, type, true); } if (correctConverter != null) { if (useType) { writer.Write(TypeProcessor.ConvertType(type) + "." + "op_Implicit_" + TypeProcessor.ConvertType(correctConverter.ReturnType, false, true, false).Replace(".", "_")); } else { writer.Write(TypeProcessor.ConvertType(convertedType) + "." + "op_Implicit_" + TypeProcessor.ConvertType(correctConverter.ReturnType, false, true, false).Replace(".", "_")); } writer.Write("("); Core.Write(writer, value); writer.Write(")"); return(true); } return(false); }
public static void Go(OutputWriter writer, ImplicitArrayCreationExpressionSyntax array) { var ti = TypeProcessor.GetTypeInfo(array); var t = ti.Type; if (ti.ConvertedType != null && !(ti.ConvertedType == t)) // Alot of times we are using covariance here { t = ti.ConvertedType; } var ptr = !t.As <IArrayTypeSymbol>().ElementType.IsValueType; // ? "" : ""; var elementType = t.As <IArrayTypeSymbol>().ElementType; var type = TypeProcessor.ConvertType(elementType); var typeString = "Array_T!(" + type + ")"; var tempWriter = new TempWriter(); tempWriter.Indent = writer.Indent; if (elementType.TypeKind == TypeKind.TypeParameter) { tempWriter.Write(" __TypeNew!(" + typeString + ")(["); } else { tempWriter.Write("new " + typeString + "("); } //__ARRAY var variableDeclarationSyntax = array.Parent.Parent.Parent as VariableDeclarationSyntax; // var argumentSyntax = array.Parent as ArgumentSyntax; if (variableDeclarationSyntax != null) { var atype = variableDeclarationSyntax.Type; array.Initializer.WriteArrayInitializer(tempWriter, atype); } else { array.Initializer.WriteArrayInitializer(tempWriter, t); } tempWriter.Write(")"); var tempString = tempWriter.ToString(); var oldString = tempString; tempString = tempString.Replace("new " + typeString + "(__CC!(" + type + "[])(", "__ARRAY!(" + type + ")("); if (tempString != oldString) { tempString = tempString.RemoveFromEndOfString(")"); } writer.Write(tempString); }
private static string GetMethodConstraints(MethodDeclarationSyntax method) { string constraints = ""; if (method.ConstraintClauses.Count > 0) { constraints += (" if ("); bool isFirst = true; foreach (var constraint in method.ConstraintClauses) { foreach (var condition in constraint.Constraints) { string dlangCondition = condition.ToString(); if (dlangCondition == "new()") // haven't got around to this yet { continue; } if (dlangCondition == "class") // TODO: is there a better way to do this ? { dlangCondition = "NObject"; } if (dlangCondition == "struct") { constraints += ((isFirst ? "" : "&&") + " !is(" + constraint.Name + " : NObject)"); } else { var constraintName = WriteIdentifierName.TransformIdentifier(constraint.Name.Identifier.Text); if (condition is TypeConstraintSyntax) { var type = TypeProcessor.GetTypeInfo((condition as TypeConstraintSyntax).Type).Type; if (type.TypeKind == TypeKind.Interface) { constraintName = "__BoxesTo!(" + constraintName + ")"; } constraints += ((isFirst ? "" : "&&") + " is(" + constraintName + " : " + TypeProcessor.ConvertType(type, true, true, true) + ")"); } else { //TODO: fix this up better constraints += ((isFirst ? "" : "&&") + " is(" + constraintName + " : " + dlangCondition.Replace("<", "!(").Replace(">", ")") + ")"); } } isFirst = false; // Console.WriteLine (condition); } } constraints += (")"); } return(constraints); }
public static AttributeSyntax GetAttribute(this SyntaxNode node, INamedTypeSymbol name) { AttributeSyntax attr = null; if (node is BaseMethodDeclarationSyntax || node is BaseTypeDeclarationSyntax || node is BaseFieldDeclarationSyntax) { var list = (node is BaseMethodDeclarationSyntax ? node.As <BaseMethodDeclarationSyntax>().AttributeLists : node is BaseTypeDeclarationSyntax ? node.As <BaseTypeDeclarationSyntax>().AttributeLists : node.As <BaseFieldDeclarationSyntax>().AttributeLists).ToList(); if (list.Count > 0) { attr = list.SelectMany(o => o.Attributes) .SingleOrDefault(o => TypeProcessor.TryGetTypeSymbol(o) == name); if (attr != null) { return(attr); } } } return(null); }
public static void Go(OutputWriter writer, SizeOfExpressionSyntax expression) { var type = TypeProcessor.GetTypeInfo(expression.Type); //Use dTypes writer.Write("" + TypeProcessor.ConvertType(type.Type) + ".sizeof"); // writer.Write(SizeOf(type.Type).ToString()); }
public static void WriteIt(OutputWriter writer, SyntaxToken operatorToken, CSharpSyntaxNode rightExpression, CSharpSyntaxNode leftExpression) { if (operatorToken.IsKind(SyntaxKind.AsKeyword)) { writer.Write("AsCast!("); writer.Write(TypeProcessor.ConvertType(rightExpression)); writer.Write(")("); Core.Write(writer, leftExpression); writer.Write(")"); } else if (operatorToken.IsKind(SyntaxKind.IsKeyword)) // isCast { var leftSymbolType = TypeProcessor.GetTypeInfo(leftExpression); var rightSymbolType = TypeProcessor.GetTypeInfo(rightExpression); if (leftSymbolType.Type.IsValueType) { writer.Write("IsCast!(Boxed!("); writer.Write(TypeProcessor.ConvertType(rightExpression)); writer.Write("))"); writer.Write("("); Core.Write(writer, leftExpression); writer.Write(")"); } else if (rightSymbolType.Type.IsValueType) { writer.Write("IsCast!(Boxed!("); writer.Write(TypeProcessor.ConvertType(rightExpression)); writer.Write("))"); writer.Write("("); Core.Write(writer, leftExpression); writer.Write(")"); } else { writer.Write("(IsCast!("); writer.Write(TypeProcessor.ConvertType(rightExpression)); writer.Write(")("); Core.Write(writer, leftExpression); writer.Write("))"); } } else if (operatorToken.IsKind(SyntaxKind.QuestionQuestionToken)) { writer.Write("(("); Core.Write(writer, leftExpression); writer.Write(")!is null?("); Core.Write(writer, leftExpression); writer.Write("):("); Core.Write(writer, rightExpression); writer.Write("))"); } else { ProcessExpression(writer, operatorToken, rightExpression, leftExpression); } }
public static string GetParameterList(ImmutableArray <IParameterSymbol> paramlist) { var list = ""; // list+= ("("); var firstParam = true; foreach (var parameter in paramlist) { var refKeyword = ""; if (parameter.RefKind == RefKind.Ref) //.Any (SyntaxKind.OutKeyword)) { refKeyword = " ref "; } if (parameter.RefKind == RefKind.Out) { refKeyword = " out "; } // if (parameter.Modifiers.Any (SyntaxKind.InKeyword)) // refKeyword = " in "; // bool isRef = parameter.Modifiers.Any (SyntaxKind.OutKeyword) || parameter.Modifiers.Any (SyntaxKind.RefKeyword);// || parameter.Modifiers.Any (SyntaxKind.InKeyword); if (firstParam) { firstParam = false; } else { list += (", "); } // var localSymbol = TypeProcessor.GetTypeInfo (parameter.Type); // var ptr = (localSymbol.Type != null && !(localSymbol.Type.IsValueType || localSymbol.Type.TypeKind == TypeKind.TypeParameter)); // if (!isRef) { // var s = TypeProcessor.ConvertType(parameter.Type) + " " + ptr; // var s = TypeProcessor.ConvertType (parameter.Type) + " "; // writer.Write (s); // } // else { // var s = "" + TypeProcessor.ConvertType(parameter.Type) + ptr + "& "; var type = TypeProcessor.ConvertType(parameter.Type); list += (refKeyword + type + " "); // Program.RefOutSymbols.TryAdd (TypeProcessor.GetDeclaredSymbol (parameter), null); //TODO: clean this completely out, not at all needed for Dlang // } list += (WriteIdentifierName.TransformIdentifier(parameter.Name)); // if (parameter.Default != null) //TODO: centralize this, not needed for pinvoke though // { // writer.Write (" = "); // Core.Write (writer, parameter.Default.Value); // } } // list+= (") "); return(list); }
public static string GetFullNameD(this ITypeSymbol typeInfo, bool includeNamespace = true) { return(TypeProcessor.ConvertType(typeInfo, false)); // (typeInfo.ContainingNamespace != null && includeNamespace ? (typeInfo.ContainingNamespace.FullName() + ".") : "") + // (typeInfo.ContainingType != null ? (typeInfo.ContainingType.FullName() + "_") : "") + (((typeInfo as INamedTypeSymbol) != null) ? // string.Join("_", // (new string[] { typeInfo.Name }).Union<string>( // ((INamedTypeSymbol)typeInfo).TypeParameters.Select(o => o.ToString()))) : typeInfo.Name); }
private static void WriteBox(OutputWriter writer, ITypeSymbol type, ExpressionSyntax value) { writer.Write("BOX!(" + TypeProcessor.ConvertType(type) + ")("); if (type.TypeKind == TypeKind.Enum) { writer.Write("cast({0})", TypeProcessor.ConvertType(type)); } Core.Write(writer, value); writer.Write(")"); }
public static string GetParameterListAsString(ParameterListSyntax list, bool includeTypes = true) { var writer = new TempWriter(); // Temp Writer writer.Write("("); var firstParam = true; foreach (var parameter in list.Parameters) { var refKeyword = ""; if (parameter.Modifiers.Any(SyntaxKind.RefKeyword)) { refKeyword = " ref "; } if (parameter.Modifiers.Any(SyntaxKind.OutKeyword)) { refKeyword = " out "; } if (parameter.Modifiers.Any(SyntaxKind.InKeyword)) { refKeyword = " in "; } bool isRef = parameter.Modifiers.Any(SyntaxKind.OutKeyword) || parameter.Modifiers.Any(SyntaxKind.RefKeyword); // || parameter.Modifiers.Any (SyntaxKind.InKeyword); if (firstParam) { firstParam = false; } else { writer.Write(", "); } var s = refKeyword + TypeProcessor.ConvertType(parameter.Type) + " "; if (includeTypes) { writer.Write(s); } writer.Write(WriteIdentifierName.TransformIdentifier(parameter.Identifier.ValueText)); if (parameter.Default == null) { continue; } writer.Write(" = "); Core.Write(writer, parameter.Default.Value); } writer.Write(")"); return(writer.ToString()); }
public static void Go(OutputWriter writer, AnonymousMethodExpressionSyntax expression) { if (expression.ParameterList != null) { Go(writer, expression.ParameterList.Parameters, expression.Block, TypeProcessor.GetTypeInfo(expression)); } else { Go(writer, new SeparatedSyntaxList <ParameterSyntax>(), expression.Block, TypeProcessor.GetTypeInfo(expression)); } }
private static List <FieldDeclarationSyntax> SortFields(List <FieldDeclarationSyntax> fields) { if (fields.Count == 0) { return(fields); } var dependencies = fields.ToDictionary( o => TypeProcessor.GetDeclaredSymbol(o.Declaration.Variables.First()).As <IFieldSymbol>(), o => new { Syntax = o, Dependicies = new List <IFieldSymbol>() }); foreach (var dep in dependencies) { foreach ( var fieldDepend in dep.Value.Syntax.DescendantNodes() .OfType <ExpressionSyntax>() .Select(o => TypeProcessor.GetSymbolInfo(o).Symbol) .OfType <IFieldSymbol>()) { if (dependencies.ContainsKey(fieldDepend)) { dep.Value.Dependicies.Add(fieldDepend); } } } var ret = new List <FieldDeclarationSyntax>(); var symbolsAdded = new HashSet <IFieldSymbol>(); while (dependencies.Count > 0) { foreach (var dep in dependencies.ToList()) { for (int i = 0; i < dep.Value.Dependicies.Count; i++) { if (symbolsAdded.Contains(dep.Value.Dependicies[i])) { dep.Value.Dependicies.RemoveAt(i--); } } if (dep.Value.Dependicies.Count == 0) { ret.Add(dep.Value.Syntax); symbolsAdded.Add(dep.Key); dependencies.Remove(dep.Key); } } } return(ret); }
public static void Go(OutputWriter writer, StackAllocArrayCreationExpressionSyntax @as) { //import core.stdc.stdlib; //@as.Type; //auto ptr = cast(wchar*)alloca(wchar.sizeof * len); var arrayType = TypeProcessor.GetTypeInfo(@as.Type); var elementType = TypeProcessor.GetTypeInfo(((ArrayTypeSyntax)@as.Type).ElementType); var elementTypeName = TypeProcessor.ConvertType(elementType.Type); var size = Core.WriteString(((ArrayTypeSyntax)@as.Type).RankSpecifiers[0].Sizes[0]); writer.Write("cast(" + elementTypeName + "*) core.stdc.stdlib.alloca(" + elementTypeName + ".sizeof * " + size + ")"); }
public void Write(OutputWriter writer) { if (StringOpt != null) { writer.Write(StringOpt); } else { if (ArgumentOpt.NameColon != null) { Core.Write(writer, ArgumentOpt.NameColon.Name); writer.Write(" = "); } var symbol = TypeProcessor.GetSymbolInfo(ArgumentOpt.Expression); var type = TypeProcessor.GetTypeInfo(ArgumentOpt.Expression); if (symbol.Symbol != null && type.ConvertedType != null && symbol.Symbol.Kind == SymbolKind.Method && type.ConvertedType.TypeKind == TypeKind.Delegate) { var typeString = TypeProcessor.ConvertType(type.ConvertedType); var createNew = !(ArgumentOpt.Parent.Parent is ObjectCreationExpressionSyntax); //Ugly hack if (createNew) { if (type.ConvertedType.TypeKind == TypeKind.TypeParameter) { writer.Write(" __TypeNew!(" + typeString + ")("); } else { writer.Write("new " + typeString + "("); } } var isStatic = symbol.Symbol.IsStatic; // if (isStatic) //Not Needed, delegate class now supports functions directly // writer.Write("__ToDelegate("); MemberUtilities.WriteMethodPointer(writer, ArgumentOpt.Expression); // if (isStatic) // writer.Write(")"); if (createNew) { writer.Write(")"); } return; } Core.Write(writer, ArgumentOpt.Expression); } }
private static bool IsAmbiguousType(ITypeSymbol type) { switch (TypeProcessor.GenericTypeName(type)) { case "System.UInt16": case "System.UInt32": case "System.UInt64": return(true); default: return(false); } }
public static void Go(OutputWriter writer, ConversionOperatorDeclarationSyntax method) { //TODO: Need to find a way of dealing with this ... Quickly moving towards the idea of compile/decompile using ILSPY // if (method.ImplicitOrExplicitKeyword.RawKind != (decimal) SyntaxKind.ExplicitKeyword) // throw new Exception("Implicit cast operators are not supported " + Utility.Descriptor(method)); var temp = new TempWriter(); // temp.WriteIndent(); temp.Write("public static " + TypeProcessor.ConvertType(method.Type)); temp.Write(" " + ((method.ImplicitOrExplicitKeyword.RawKind != (decimal)SyntaxKind.ExplicitKeyword) ? ("op_Implicit_" + TypeProcessor.ConvertType(method.Type, false, true, false).Replace(".", "_")) : "op_Explicit")); //writer.Write(" op_Explicit"); // writer.Write(TypeProcessor.ConvertType(method.Type)); temp.Write("("); bool firstParam = true; foreach (var param in method.ParameterList.Parameters) { if (firstParam) { firstParam = false; } else { temp.Write(", "); } temp.Write(TypeProcessor.ConvertType(param.Type) + " "); temp.Write(WriteIdentifierName.TransformIdentifier(param.Identifier.Text)); } temp.Write(")"); writer.WriteLine(temp.ToString()); writer.OpenBrace(); foreach (var statement in method.Body.Statements) { Core.Write(writer, statement); } writer.CloseBrace(); }
/// <summary> /// Determines if the passed symbol is used in any ref or out clauses /// </summary> private static bool UsedAsRef(VariableDeclaratorSyntax variable, ISymbol symbol) { SyntaxNode node = variable; BlockSyntax scope; do { scope = (node = node.Parent) as BlockSyntax; }while (scope == null); return(scope.DescendantNodes().OfType <InvocationExpressionSyntax>() .SelectMany(o => o.ArgumentList.Arguments) .Where(o => o.RefOrOutKeyword.RawKind != (decimal)SyntaxKind.None) .Any(o => TypeProcessor.GetSymbolInfo(o.Expression).Symbol == symbol)); }
private static void WriteEnumGetValues(OutputWriter writer, InvocationExpressionSyntax invocationExpression) { if (!(invocationExpression.ArgumentList.Arguments[0].Expression is TypeOfExpressionSyntax)) { throw new Exception("Expected a typeof() expression as the first parameter of Enum.GetValues " + Utility.Descriptor(invocationExpression)); } // var type = ModelExtensions.GetTypeInfo(Program.GetModel(invocationExpression), invocationExpression.ArgumentList.Arguments[0].Expression.As<TypeOfExpressionSyntax>().Type).Type; var type = TypeProcessor.GetTypeInfo( invocationExpression.ArgumentList.Arguments[0].Expression.As <TypeOfExpressionSyntax>().Type).Type; writer.Write(type.ContainingNamespace.FullNameWithDot()); writer.Write(WriteType.TypeName((INamedTypeSymbol)type)); writer.Write(".Values"); }
public static void Go(OutputWriter writer, UsingStatementSyntax usingStatement) { var expression = usingStatement.Expression; writer.WriteLine("//using block ... " + usingStatement.Declaration); writer.OpenBrace(); //Ensure the using statement is a local variable - we can't deal with things we can't reliably repeat in the finally block var resource = Utility.TryGetIdentifier(expression); // if (resource == null) // throw new Exception("Using statements must reference a local variable. " + Utility.Descriptor(usingStatement)); var variables = new SeparatedSyntaxList <VariableDeclaratorSyntax>();//.Select(o => o.Identifier.ValueText); if (usingStatement.Declaration != null) { Core.Write(writer, usingStatement.Declaration); variables = usingStatement.Declaration.Variables; } writer.WriteLine("try"); Core.WriteStatementAsBlock(writer, usingStatement.Statement); writer.WriteLine("finally"); writer.OpenBrace(); foreach (var variable in variables) { var typeInfo = TypeProcessor.GetTypeInfo(usingStatement.Declaration.Type); if (!typeInfo.Type.IsValueType) { writer.WriteLine("if(" + variable.Identifier.Text + " !is null)"); } else if (typeInfo.Type.Name == "Nullable") { writer.WriteLine("if(" + variable.Identifier.Text + ".HasValue)"); } writer.WriteLine(variable.Identifier.Text + ".Dispose(cast(IDisposable)null);"); } if (resource != null) { writer.WriteLine("if(" + resource + " !is null)"); writer.WriteLine(resource + ".Dispose(cast(IDisposable)null);"); } writer.CloseBrace(); writer.CloseBrace(); }
public static void Go(OutputWriter writer, TryStatementSyntax tryStatement) { writer.WriteLine("try"); Core.Write(writer, tryStatement.Block); var catches = tryStatement.Catches.Where(o => Program.DoNotWrite.ContainsKey(o) == false).ToList(); if (catches.Count > 0) { foreach (var catchClause in catches) { if (catchClause.Declaration == null) { writer.WriteLine("catch(Exception __ex)"); } else { writer.WriteLine("catch(" + TypeProcessor.ConvertType(catchClause.Declaration.Type) + " " + (string.IsNullOrWhiteSpace(catchClause.Declaration.Identifier.Text) ? "__ex" : WriteIdentifierName.TransformIdentifier( catchClause.Declaration.Identifier.Text)) + ")"); } writer.OpenBrace(); Core.WriteBlock(writer, catchClause.Block, false); // foreach (var statement in catchClause.Block.Statements) // Core.Write(writer, statement); writer.CloseBrace(); } } if (tryStatement.Finally != null) { writer.WriteLine("finally"); // Core.Write(writer, tryStatement.Finally.Block); writer.OpenBrace(); Core.WriteBlock(writer, tryStatement.Finally.Block, false); // foreach (var statement in catchClause.Block.Statements) // Core.Write(writer, statement); writer.CloseBrace(); } }
public static void Go(OutputWriter writer, ImplicitArrayCreationExpressionSyntax array) { var t = TypeProcessor.GetTypeInfo(array).Type; var ptr = !t.As <IArrayTypeSymbol>().ElementType.IsValueType; // ? "" : ""; var elementType = t.As <IArrayTypeSymbol>().ElementType; var type = TypeProcessor.ConvertType(elementType); var typeString = "Array_T!(" + type + ")"; if (elementType.TypeKind == TypeKind.TypeParameter) { writer.Write(" __TypeNew!(" + typeString + ")(["); } else { writer.Write("new " + typeString + "("); } var variableDeclarationSyntax = array.Parent.Parent.Parent as VariableDeclarationSyntax; if (variableDeclarationSyntax != null) { var atype = variableDeclarationSyntax.Type; array.Initializer.WriteArrayInitializer(writer, atype); } else { array.Initializer.WriteArrayInitializer(writer); } // array.Initializer.WriteArrayInitializer(writer,); // bool first = true; // foreach (var expression in array.Initializer.Expressions) // { // if (first) // first = false; // else // writer.Write(","); // // Core.Write(writer, expression); // } writer.Write(")"); }
public static void WriteMember(OutputWriter writer, ExpressionSyntax expression) { var symbol = TypeProcessor.GetSymbolInfo(expression).Symbol; if (symbol is INamedTypeSymbol) { var str = TypeProcessor.ConvertType(symbol.As <INamedTypeSymbol>()); if (str == "Array_T") { // Array is the only special case, otherwise generics have to be specialized to access static members str = "Array"; } writer.Write(str); } else { Core.Write(writer, expression); } }