private static void WriteOneDelegate(OutputWriter outputWriter, Context.DelegateSyntaxAndSymbol first, bool fileExists) { Context.Instance.Namespace = first.Symbol.ContainingNamespace.FullName(); Context.Instance.Type = first.Symbol; TypeProcessor.ClearUsedTypes(); var mynamespace = Context.Instance.Type.ContainingNamespace.FullName().RemoveFromEndOfString(".Namespace"); // + "." + TypeState.Instance.TypeName; var myUsingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(mynamespace)); var SystemUsingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")); // Required as certain functions like boxing are in this namespace Context.Instance.UsingDeclarations = first.Syntax.Parent.DescendantNodes().OfType <UsingDirectiveSyntax>().ToArray() .Union(new[] { myUsingDirective, SystemUsingDirective }).ToArray(); OutputWriter writer = null; using ( writer = outputWriter == null ? new OutputWriter(Context.Instance.Namespace, Context.Instance.TypeName) : new TempWriter()) { if (outputWriter != null) { writer.WriteLine(); writer.Indent = outputWriter.Indent + 2; writer.WriteIndent(); } writer.FileExists = fileExists; WriteBcl.Go(writer); WriteStandardIncludes.Go(writer); //Look for generic arguments { List <TypeParameterSyntax> genericArgs = new List <TypeParameterSyntax>(); if (first.Syntax.TypeParameterList != null) { genericArgs = first.Syntax.TypeParameterList.Parameters.ToList(); } var name = WriteType.TypeName(Context.Instance.Type, false); //Context.Instance.TypeName; if (genericArgs.Count > 0) { name = "template " + name; name += ("("); name += (string.Join(" , ", genericArgs.Select(o => o))); name += (")"); writer.WriteLine(name); writer.OpenBrace(); writer.WriteLine("alias __Delegate!(" + TypeProcessor.ConvertType(first.Syntax.ReturnType) + " delegate" + WriteMethod.GetParameterListAsString(first.Syntax.ParameterList.Parameters) + ") " + WriteType.TypeName(Context.Instance.Type, false) + ";"); writer.CloseBrace(); } else { //Non-generic writer.WriteLine("alias __Delegate!(" + TypeProcessor.ConvertType(first.Syntax.ReturnType) + " delegate" + WriteMethod.GetParameterListAsString(first.Syntax.ParameterList.Parameters) + ") " + WriteType.TypeName(Context.Instance.Type, false) + ";"); } } if (outputWriter != null) { outputWriter.WriteLine(writer.ToString()); } } }
public static void WriteInstanceConstructor(OutputWriter writer, ConstructorDeclarationSyntax method, List <string> otherInits) { writer.WriteLine(); var accessmodifiers = ""; var isInterface = method.Parent is InterfaceDeclarationSyntax; if (method.Modifiers.Any(SyntaxKind.PublicKeyword) || method.Modifiers.Any(SyntaxKind.InternalKeyword) || method.Modifiers.Any(SyntaxKind.ProtectedKeyword) || method.Modifiers.Any(SyntaxKind.AbstractKeyword) || isInterface) { accessmodifiers += ("public "); } // Reflection cannot work with this, cant get address or set value //if (method.Modifiers.Any(SyntaxKind.PrivateKeyword)) // accessmodifiers += ("private "); if (method.Modifiers.Any(SyntaxKind.StaticKeyword)) { accessmodifiers += ("static "); } var constructorName = "this"; if (Context.Instance.Type.TypeKind == TypeKind.Struct) // Struct { constructorName = " void __init"; } writer.WriteLine(accessmodifiers + constructorName + WriteMethod.GetParameterListAsString(method.ParameterList.Parameters)); if (isInterface || method.Modifiers.Any(SyntaxKind.AbstractKeyword)) { writer.Write(" = 0;\r\n"); return; } //if (!returnsVoid) // writer.Write(" ="); // writer.Write("\r\n"); writer.OpenBrace(); if (otherInits != null) //We need to write the static initializers before anything else { foreach (var statement in otherInits) { var nodeString = statement; if (nodeString != null) { writer.WriteLine(nodeString); } } } if (method.Initializer != null) { //writer.Write(":"); Core.Write(writer, method.Initializer); writer.Write(";"); writer.WriteLine(); //";\r\n"); } if (method.Body != null) { foreach (var statement in method.Body.Statements) { Core.Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, method.Body.DescendantTrivia()); } writer.CloseBrace(); }
public static void Go(OutputWriter writer, OperatorDeclarationSyntax method) { var methodSymbol = (IMethodSymbol)TypeProcessor.GetDeclaredSymbol(method); var actualMethodName = OverloadResolver.MethodName(methodSymbol); writer.Write("\n"); var returnType = ""; if (method.ReturnType.ToString() == "void") { returnType = ("void "); } else { returnType = TypeProcessor.ConvertType(method.ReturnType) + " "; // writer.Write(returnType); } var methodName = ""; if (BinaryOperators.ContainsKey(actualMethodName)) { methodName = "opBinary"; var typeSymbolParam0 = TypeProcessor.GetTypeInfo(method.ParameterList.Parameters[0].Type); var typeSymbolParent = (methodSymbol.ContainingType); if (typeSymbolParam0.Type != typeSymbolParent) { methodName += "Right"; } } if (UnaryOperators.ContainsKey(actualMethodName)) { methodName = "opUnary"; } if (EqualsOperators.ContainsKey(actualMethodName)) { methodName = "opEquals"; } if (CmpOperators.ContainsKey(actualMethodName)) { methodName = "opCmp"; } if (AssignOperators.ContainsKey(actualMethodName)) { methodName = "opAssign"; } if (AssignOpOperators.ContainsKey(actualMethodName)) { methodName = "opOpAssign"; // need to remove = from the name } var paramType = method.ParameterList.Parameters[0]; if (method.ParameterList.Parameters.Count == 2) { if (methodName.EndsWith("Right")) { paramType = method.ParameterList.Parameters[0]; } else { paramType = method.ParameterList.Parameters[1]; } } var token = method.OperatorToken.Text; var methodBody = ""; var temp = new TempWriter(); foreach (var statement in method.Body.Statements) { Core.Write(temp, statement); } TriviaProcessor.ProcessTrivias(temp, method.Body.DescendantTrivia()); methodBody = temp.ToString(); if (methodName == "opOpAssign") { token = token.Substring(0, 1); } //We are going to have to rewrite this bit later ... for now all overloads are called directly /* if (methodName == "opBinary") * { * * writer.WriteLine("public final " + returnType + " " + methodName + * String.Format( * " (string _op) ({0} other)\r\n\tif(_op==\"{2}\")\r\n\t{{ \r\n\t\treturn {1}(this,other); \r\n\t}}\r\n\r\n", * TypeProcessor.ConvertType(paramType.Type), actualMethodName, token)); * * //Add Assignment operator if it doesn't exist * if (!methodSymbol.ContainingType.GetMembers(AssignOpOperators.FirstOrDefault(k => k.Value == token + "=").Key).Any()) * { * writer.WriteLine("public final " + returnType + " opOpAssign" + * String.Format( * " (string _op) ({0} other)\r\n\tif(_op==\"{2}\")\r\n\t{{ \r\n\t\treturn {1}(this,other); \r\n\t}}\r\n\r\n", * TypeProcessor.ConvertType(paramType.Type), actualMethodName, token)); * } * } * else if (methodName == "opUnary")//TODO unary operators are mostly going to be direct methodCalls * { * * writer.WriteLine("public final " + returnType + " " + methodName + * String.Format( * " (string _op) ()\r\n\tif(_op==\"{2}\")\r\n\t{{ \r\n\t\treturn {1}(this); \r\n\t}}\r\n\r\n", * TypeProcessor.ConvertType(paramType.Type), actualMethodName, token)); * // writer.WriteLine ("public final ref " + returnType + " " + methodName + * // String.Format ( * // " (string _op) ()\r\n\tif(_op==\"{2}\")\r\n\t{{ \r\n\t\t{3}\r\n\t}}\r\n\r\n", * //TypeProcessor.ConvertType (paramType.Type), actualMethodName, token, methodBody.Replace(method.ParameterList.Parameters[0].Identifier.ValueText,"this"))); * } * else * { * writer.WriteLine("public final " + returnType + " " + methodName + * String.Format( * " (string _op) ({0} other)\r\n\tif(_op==\"{2}\")\r\n\t{{ \r\n\t\treturn {1}(this); \r\n\t}}\r\n\r\n", * TypeProcessor.ConvertType(paramType.Type), actualMethodName, token)); * }*/ var @params = method.ParameterList.Parameters; writer.WriteLine("public static " + returnType + " " + actualMethodName + WriteMethod.GetParameterListAsString(method.ParameterList.Parameters)); writer.OpenBrace(); if (method.Body != null) { foreach (var statement in method.Body.Statements) { Core.Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, method.Body.DescendantTrivia()); } writer.CloseBrace(); }
public static void Go(OutputWriter writer, BasePropertyDeclarationSyntax property, bool isProxy = false) { writer.WriteLine(); //TODO, doesnt ref make things slower ?, though it makes proprties behave as in c# var isInterface = property.Parent is InterfaceDeclarationSyntax; var getter = property.AccessorList.Accessors.SingleOrDefault( o => o.Keyword.RawKind == (decimal)SyntaxKind.GetKeyword); var setter = property.AccessorList.Accessors.SingleOrDefault( o => o.Keyword.RawKind == (decimal)SyntaxKind.SetKeyword); var isYield = getter != null && getter.DescendantNodes().OfType <YieldStatementSyntax>().Any(); var isStatic = property.Modifiers.Any(k => k.IsKind(SyntaxKind.StaticKeyword)); ITypeSymbol iface; ISymbol[] proxies; var name = MemberUtilities.GetMethodName(property, ref isInterface, out iface, out proxies); var modifiers = property.Modifiers; var propertySymbol = (IPropertySymbol)TypeProcessor.GetDeclaredSymbol(property); var type = propertySymbol.Type; var acccessmodifiers = MemberUtilities.GetAccessModifiers(property, isInterface || propertySymbol.IsAbstract); var typeString = TypeProcessor.ConvertType(type); var hasGetter = getter != null; var getterHasBody = hasGetter && getter.Body != null; var hasSetter = setter != null; var setterHasBody = hasSetter && setter.Body != null; var indexerDeclarationSyntax = property as IndexerDeclarationSyntax; var isindexer = indexerDeclarationSyntax != null; string getterbody = null; if (getterHasBody) { getterbody = Core.WriteBlock(getter.Body, false, writer.Indent + 2); if (!isProxy && isYield) { var namedTypeSymbol = propertySymbol.Type as INamedTypeSymbol; if (namedTypeSymbol != null) { // var iteratortype = namedTypeSymbol.TypeArguments[0]; // getterbody=String.Format("return new __IteratorBlock!({0})(delegate(__IteratorBlock!({0}) __iter){{ {1} }});", // TypeProcessor.ConvertType(iteratortype),getterbody); var className = propertySymbol.GetYieldClassName() + ( (((INamedTypeSymbol)propertySymbol.Type).TypeArguments.Any() && ((INamedTypeSymbol)propertySymbol.Type).TypeArguments[0].TypeKind == TypeKind.TypeParameter) ? "__G" : ""); // writer.WriteLine(accessString + returnTypeString + methodSignatureString + @params2 + constraints); //writer.OpenBrace(); if (!propertySymbol.IsStatic) { getterbody = writer.WriteIndentToString() + ("return new " + className + "(this);"); } else { getterbody = writer.WriteIndentToString() + ("return new " + className + "();"); } } } } string setterbody = null; if (setterHasBody) { setterbody = Core.WriteString(setter.Body, false, writer.Indent + 2); if (isindexer) { setterbody += writer.WriteIndentToString() + "return value;"; } else { if (hasGetter) { setterbody += writer.WriteIndentToString() + "return " + name + ";"; } } } if (getter == null && setter == null) { throw new Exception("Property must have either a get or a set"); } string isOverride; var fieldName = WriteAutoFieldName(writer, name, modifiers, isInterface, hasGetter, getterHasBody, hasSetter, setterHasBody, typeString, out isOverride, (property is IndexerDeclarationSyntax)); BracketedParameterListSyntax @params = null; if (indexerDeclarationSyntax != null) { @params = indexerDeclarationSyntax.ParameterList; } string parameters = null; if (@params != null) { parameters = WriteMethod.GetParameterListAsString(@params.Parameters, iface: proxies == null ? iface : null, writebraces: false); } WriteGetter(writer, isProxy, hasGetter, acccessmodifiers, typeString, name, proxies == null ? iface : null, getterHasBody, modifiers, isInterface, fieldName, getterbody, parameters, indexerDeclarationSyntax != null); WriteSetter(writer, isProxy, hasSetter, acccessmodifiers, name, typeString, proxies == null ? iface : null, isOverride, setterHasBody, modifiers, isInterface, fieldName, setterbody, parameters, isindexer, hasGetter); // if (!isindexer && !isInterface) //TODO: Find a better solution // { // var fieldacccessmodifiers = acccessmodifiers.Replace ("abstract", "").Replace ("virtual","").Replace("override",""); // // writer.WriteLine(fieldacccessmodifiers + "__Property!(" + typeString + ")" + name + ";"); // if (isStatic) // { // var staticWriter = new TempWriter (); // // staticWriter.WriteLine (name + String.Format (" = __Property!(" + typeString + ")(__ToDelegate(&set{0}), __ToDelegate(&get{0}));", name)); // Context.Instance.StaticInits.Add (staticWriter.ToString ()); // } // else // { // var instanceWriter = new TempWriter (); // // instanceWriter.WriteLine (name + String.Format (" = __Property!(" + typeString + ")((&set{0}), (&get{0}));", name)); // Context.Instance.InstanceInits.Add (instanceWriter.ToString ()); // } // } if (proxies != null) { foreach (var proxy in proxies) { if (indexerDeclarationSyntax == null) { setterbody = writer.WriteIndentToString() + name + "=" + "value" + ";"; getterbody = writer.WriteIndentToString() + "return " + name + ";"; } else { string parameters2 = ""; if (@params != null) { parameters2 = WriteMethod.GetParameterListAsString(@params.Parameters, iface: null, includeTypes: false, writebraces: false); // parameters2 = WriteMethod.GetParameterListAsString(@params.Parameters, iface: proxies == null ? iface : null, writebraces: false); } setterbody = writer.WriteIndentToString() + "return opIndexAssign(value," + parameters2 + ");";// + "=" + "value" + ";"; getterbody = writer.WriteIndentToString() + "return opIndex(" + parameters2 + ");"; } parameters = null; if (@params != null) { parameters = WriteMethod.GetParameterListAsString(@params.Parameters, iface: proxy.ContainingType, writebraces: false); } WriteGetter(writer, isProxy, hasGetter, acccessmodifiers, typeString, name, proxy.ContainingType, getterHasBody, modifiers, isInterface, fieldName, getterbody, parameters, isindexer); WriteSetter(writer, isProxy, hasSetter, acccessmodifiers, name, typeString, proxy.ContainingType, isOverride, setterHasBody, modifiers, isInterface, fieldName, setterbody, parameters, isindexer, hasGetter); } } }
public static void WriteInstanceConstructor(OutputWriter writer, ConstructorDeclarationSyntax method) { var methodSymbol = (IMethodSymbol)TypeProcessor.GetDeclaredSymbol(method); var methodName = OverloadResolver.MethodName(methodSymbol); // var methodType = Program.GetModel(method).GetTypeInfo(method); //TODO: Improve partial class / method support 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 (Context.Instance.Partials.SelectMany(o => o.Syntax.As <ClassDeclarationSyntax>().Members) .OfType <ConstructorDeclarationSyntax>() .Except(method).Any(o => o.Identifier.ValueText == method.Identifier.ValueText)) { return; } } // if (method.Identifier.ValueText == "GetEnumerator") // return; //TODO: Support enumerator methods writer.WriteLine(); // writer.WriteIndent(); var accessmodifiers = ""; var isInterface = method.Parent is InterfaceDeclarationSyntax; if (method.Modifiers.Any(SyntaxKind.PublicKeyword) || method.Modifiers.Any(SyntaxKind.InternalKeyword) || method.Modifiers.Any(SyntaxKind.ProtectedKeyword) || method.Modifiers.Any(SyntaxKind.AbstractKeyword) || isInterface) { accessmodifiers += ("public "); } if (method.Modifiers.Any(SyntaxKind.PrivateKeyword)) { accessmodifiers += ("private "); } if (ShouldUseOverrideKeyword(method, methodSymbol)) { accessmodifiers += ("override "); } //D does not use the virtual keyword // if (method.Modifiers.Any(SyntaxKind.VirtualKeyword) || isInterface) // writer.Write("virtual "); //Need to improve performance by labling non virtual methods with "final" ... but we have to check the original definition of the method if (method.Modifiers.Any(SyntaxKind.StaticKeyword)) { accessmodifiers += ("static "); } if (isInterface) { // writer.IsInterface = true; } //Constructors in d dont have return types // writer.Write("void "); // writer.HeaderWriter.Write("void "); // writer.Write("this"); // 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(", "); // } // // // var localSymbol = TypeProcessor.GetTypeInfo(parameter.Type); // var ptr = (localSymbol.Type != null && !localSymbol.Type.IsValueType) ? "" : ""; // // if (!isRef) // { // var s = TypeProcessor.ConvertType(parameter.Type) + " " + ptr; // writer.Write(s); // } // else // { // // // // var s = " ref " + TypeProcessor.ConvertType(parameter.Type) + " "; // Refs in D are simple // // writer.Write(s); // // Program.RefOutSymbols.TryAdd(TypeProcessor.GetDeclaredSymbol(parameter), null); // } // // writer.Write(WriteIdentifierName.TransformIdentifier(parameter.Identifier.ValueText)); // // if (parameter.Default != null) // { // writer.Write(" = "); // Core.Write(writer, parameter.Default.Value); // } // } // // writer.Write(")"); writer.WriteLine(accessmodifiers + "this" + WriteMethod.GetParameterListAsString(method.ParameterList)); if (isInterface || method.Modifiers.Any(SyntaxKind.AbstractKeyword)) { writer.Write(" = 0;\r\n"); return; } //if (!returnsVoid) // writer.Write(" ="); // writer.Write("\r\n"); writer.OpenBrace(); if (method.Initializer != null) { //writer.Write(":"); Core.Write(writer, method.Initializer); writer.Write(";"); writer.WriteLine(); //";\r\n"); } if (method.Body != null) { foreach (var statement in method.Body.Statements) { Core.Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, method.Body.DescendantTrivia()); } writer.CloseBrace(); }
/* * template Action(T) { * alias void delegate(T obj) Action; * } * */ public static void Go() { var partials = Context.Instance.DelegatePartials; var first = partials.First(); Context.Instance.Namespace = first.Symbol.ContainingNamespace.FullName(); Context.Instance.Type = first.Symbol; TypeProcessor.ClearUsedTypes(); var mynamespace = Context.Instance.Type.ContainingNamespace.FullName().RemoveFromEndOfString(".Namespace"); // + "." + TypeState.Instance.TypeName; var myUsingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(mynamespace)); var SystemUsingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")); // Required as certain functions like boxing are in this namespace Context.Instance.UsingDeclarations = first.Syntax.Parent.DescendantNodes().OfType <UsingDirectiveSyntax>().ToArray() .Union(new[] { myUsingDirective, SystemUsingDirective }).ToArray(); using (var writer = new OutputWriter(Context.Instance.Namespace, Context.Instance.TypeName)) { var objectType = TypeProcessor.GetSemanticModel(partials.First().Syntax) .Compilation.GetTypeByMetadataName("System.Object"); WriteBcl.Go(writer); var @namespace = first.Symbol.ContainingNamespace.FullName(); WriteStandardIncludes.Go(writer); //Look for generic arguments //Look for generic arguments var genericArgs = partials.Select(o => o.Syntax) .Where(o => o.TypeParameterList != null) .SelectMany(o => o.TypeParameterList.Parameters) .ToList(); var name = Context.Instance.TypeName; if (genericArgs.Count > 0) { name = "template " + name; name += ("( "); name += (string.Join(" , ", genericArgs.Select(o => o))); name += (" )"); writer.Write(name); writer.Write("\r\n"); writer.OpenBrace(); writer.Indent++; writer.Write("alias Delegate!(" + TypeProcessor.ConvertType(first.Syntax.ReturnType) + " delegate" + WriteMethod.GetParameterListAsString(first.Syntax.ParameterList)); writer.Write(") " + Context.Instance.TypeName + ";"); writer.Indent--; writer.Write("\r\n"); writer.CloseBrace(); } else { //Non-generic writer.Write("alias Delegate!(" + TypeProcessor.ConvertType(first.Syntax.ReturnType) + " delegate" + WriteMethod.GetParameterListAsString(first.Syntax.ParameterList)); writer.Write(") " + Context.Instance.TypeName + ";"); } } }