private static bool GenerateInitializerForFieldWithoutInitializer(TypeSyntax parentType) { //Determine if we need to write an initializer for this field which does not have an initializer. if (TypeProcessor.ValueToReference(parentType)) { return(true); } else { return(TypeProcessor.DefaultValue(TypeProcessor.ConvertType(parentType)) != "null"); } }
public static void Go(HaxeWriter writer, LocalDeclarationStatementSyntax declaration) { foreach (var variable in declaration.Declaration.Variables) { var symbol = Program.GetModel(declaration).GetDeclaredSymbol(variable); var isRef = UsedAsRef(variable, symbol); writer.WriteIndent(); writer.Write("var "); writer.Write(variable.Identifier.ValueText); if (isRef) { var typeStr = TypeProcessor.ConvertType(declaration.Declaration.Type); writer.Write(":CsRef<"); writer.Write(typeStr); writer.Write(">"); Program.RefOutSymbols.TryAdd(symbol, null); writer.Write(" = new CsRef<"); writer.Write(typeStr); writer.Write(">("); if (variable.Initializer == null) { writer.Write(TypeProcessor.DefaultValue(typeStr)); } else { Core.Write(writer, variable.Initializer.As <EqualsValueClauseSyntax>().Value); } writer.Write(")"); } else { writer.Write(TypeProcessor.ConvertTypeWithColon(declaration.Declaration.Type)); if (variable.Initializer != null) { writer.Write(" = "); Core.Write(writer, variable.Initializer.As <EqualsValueClauseSyntax>().Value); } } writer.Write(";\r\n"); } }
public static void Go(HaxeWriter writer, SyntaxTokenList modifiers, string name, TypeSyntax type, EqualsValueClauseSyntax initializerOpt = null) { writer.WriteIndent(); var isConst = IsConst(modifiers, initializerOpt, type); WriteFieldModifiers(writer, modifiers); if (isConst) { writer.Write("inline "); } writer.Write("var "); writer.Write(name); writer.Write(TypeProcessor.ConvertTypeWithColon(type)); if (initializerOpt != null) { writer.Write(" = "); Core.Write(writer, initializerOpt.Value); } else if (GenerateInitializerForFieldWithoutInitializer(type)) { writer.Write(" = "); if (TypeProcessor.ValueToReference(type)) { writer.Write("new "); writer.Write(TypeProcessor.ConvertType(type)); writer.Write("()"); } else { writer.Write(TypeProcessor.DefaultValue(TypeProcessor.ConvertType(type))); } } writer.Write(";"); writer.WriteLine(); }
public static void Go(HaxeWriter writer, PropertyDeclarationSyntax property) { var propertySymbol = Program.GetModel(property).GetDeclaredSymbol(property); var isInterface = propertySymbol.ContainingType.TypeKind == TypeKind.Interface; bool isAutoProperty = false; Action <AccessorDeclarationSyntax, bool> writeRegion = (region, get) => { writer.WriteIndent(); if (property.Modifiers.Any(SyntaxKind.OverrideKeyword)) { writer.Write("override "); } if (property.Modifiers.Any(SyntaxKind.PublicKeyword) || property.Modifiers.Any(SyntaxKind.ProtectedKeyword) || property.Modifiers.Any(SyntaxKind.InternalKeyword)) { writer.Write("public "); } if (property.Modifiers.Any(SyntaxKind.PrivateKeyword)) { writer.Write("private "); } if (property.Modifiers.Any(SyntaxKind.StaticKeyword)) { writer.Write("static "); } writer.Write("function "); writer.Write(get ? "get_" : "set_"); writer.Write(property.Identifier.ValueText); string type = TypeProcessor.ConvertType(property.Type); if (get) { writer.Write("():" + type); } else { writer.Write("(value:" + type + "):" + type); } var isAbstract = property.Modifiers.Any(SyntaxKind.AbstractKeyword); writer.WriteLine(); writer.WriteOpenBrace(); if (isAbstract) { writer.WriteLine("return throw new Exception(\"Abstract item called\");"); } else { if (region.Body == null) { //When we leave the body off in C#, it resolves to an automatic property. isAutoProperty = true; if (get) { writer.WriteLine("return __autoProp_" + property.Identifier.ValueText + ";"); } else { writer.WriteLine("__autoProp_" + property.Identifier.Value + " = value;"); } } else { foreach (var statement in region.Body.As <BlockSyntax>().Statements) { Core.Write(writer, statement); } } if (!get) { //all haXe property setters must return a value. writer.WriteLine("return value;"); } } writer.WriteCloseBrace(); writer.WriteLine(); }; var getter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.GetKeyword); var setter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.SetKeyword); if (getter == null && setter == null) { throw new Exception("Property must have either a get or a set"); } if (!property.Modifiers.Any(SyntaxKind.OverrideKeyword)) { //Write the property declaration. Overridden properties don't need this. writer.WriteIndent(); if (property.Modifiers.Any(SyntaxKind.PublicKeyword) || property.Modifiers.Any(SyntaxKind.InternalKeyword)) { writer.Write("public "); } if (property.Modifiers.Any(SyntaxKind.StaticKeyword)) { writer.Write("static "); } writer.Write("var "); writer.Write(property.Identifier.ValueText); writer.Write("("); if (getter != null) { writer.Write("get_" + property.Identifier.ValueText); } else { writer.Write("never"); } writer.Write(", "); if (setter != null) { writer.Write("set_" + property.Identifier.ValueText); } else { writer.Write("never"); } writer.Write("):"); writer.Write(TypeProcessor.ConvertType(property.Type)); writer.Write(";\r\n"); } if (!isInterface) //interfaces get only the property decl, never the functions { if (getter != null) { writeRegion(getter, true); } if (setter != null) { writeRegion(setter, false); } } if (isAutoProperty) { writer.WriteLine("var __autoProp_" + property.Identifier.ValueText + TypeProcessor.ConvertTypeWithColon(property.Type) + " = " + TypeProcessor.DefaultValue(TypeProcessor.ConvertType(property.Type)) + ";"); } }
public static void WriteInstanceConstructor(HaxeWriter writer, ConstructorDeclarationSyntax ctorOpt) { writer.WriteIndent(); writer.Write("public function new("); if (ctorOpt != null) { var firstParameter = true; foreach (var parameter in ctorOpt.ParameterList.Parameters) { if (firstParameter) { firstParameter = false; } else { writer.Write(", "); } writer.Write(parameter.Identifier.ValueText); writer.Write(TypeProcessor.ConvertTypeWithColon(parameter.Type)); if (parameter.Default != null) { writer.Write(" = "); Core.Write(writer, parameter.Default.Value); } } } writer.Write(")\r\n"); writer.WriteOpenBrace(); if (!TypeState.Instance.DerivesFromObject) { if (ctorOpt == null || ctorOpt.Initializer == null) { writer.WriteLine("super();"); } else { if (ctorOpt.Initializer.ThisOrBaseKeyword.ToString() != "base") { throw new Exception("Constructor overloading not supported " + Utility.Descriptor(ctorOpt)); } writer.WriteIndent(); writer.Write("super("); bool first = true; foreach (var init in ctorOpt.Initializer.ArgumentList.Arguments) { if (first) { first = false; } else { writer.Write(", "); } Core.Write(writer, init.Expression); } writer.Write(");\r\n"); } } foreach (var field in TypeState.Instance.AllMembers .OfType <BaseFieldDeclarationSyntax>() .Where(o => !o.Modifiers.Any(SyntaxKind.StaticKeyword)) .SelectMany(o => o.Declaration.Variables) .Where(o => (o.Initializer != null && !WriteField.IsConst(o.Parent.Parent.As <BaseFieldDeclarationSyntax>().Modifiers, o.Initializer, o.Parent.As <VariableDeclarationSyntax>().Type)) || (o.Initializer == null && GenerateInitializerForFieldWithoutInitializer(o.Parent.As <VariableDeclarationSyntax>().Type)) || o.Parent.Parent is EventFieldDeclarationSyntax)) { var parentType = field.Parent.As <VariableDeclarationSyntax>().Type; writer.WriteIndent(); writer.Write(field.Identifier.ValueText); writer.Write(" = "); if (field.Parent.Parent is EventFieldDeclarationSyntax) { writer.Write("new CsEvent<"); writer.Write(TypeProcessor.ConvertType(parentType)); writer.Write(">()"); } else if (field.Initializer == null) { if (TypeProcessor.ValueToReference(parentType)) { writer.Write("new "); writer.Write(TypeProcessor.ConvertType(parentType)); writer.Write("()"); } else { writer.Write(TypeProcessor.DefaultValue(TypeProcessor.ConvertType(parentType))); } } else { Core.Write(writer, field.Initializer.Value); } writer.Write(";\r\n"); } if (ctorOpt != null && ctorOpt.Body != null) { foreach (var statement in ctorOpt.Body.As <BlockSyntax>().Statements) { Core.Write(writer, statement); } TriviaProcessor.ProcessTrivias(writer, ctorOpt.Body.DescendantTrivia()); } writer.WriteCloseBrace(); }
public static void WriteStaticConstructor(HaxeWriter writer, ConstructorDeclarationSyntax staticConstructorOpt) { var staticFieldsNeedingInitialization = TypeState.Instance.AllMembers .OfType <BaseFieldDeclarationSyntax>() .Where(o => o.Modifiers.Any(SyntaxKind.StaticKeyword)) .SelectMany(o => o.Declaration.Variables) .Where(o => (o.Initializer != null && !WriteField.IsConst(o.Parent.Parent.As <BaseFieldDeclarationSyntax>().Modifiers, o.Initializer, o.Parent.As <VariableDeclarationSyntax>().Type)) || (o.Initializer == null && GenerateInitializerForFieldWithoutInitializer(o.Parent.As <VariableDeclarationSyntax>().Type)) || o.Parent.Parent is EventFieldDeclarationSyntax) .ToList(); if (staticConstructorOpt == null && staticFieldsNeedingInitialization.Count == 0) { return; //No static constructor needed } writer.WriteLine("public static function cctor():Void"); writer.WriteOpenBrace(); foreach (var field in staticFieldsNeedingInitialization) { var parentType = field.Parent.As <VariableDeclarationSyntax>().Type; writer.WriteIndent(); writer.Write(field.Identifier.ValueText); writer.Write(" = "); if (field.Parent.Parent is EventFieldDeclarationSyntax) { writer.Write("new CsEvent<"); writer.Write(TypeProcessor.ConvertType(parentType)); writer.Write(">()"); } else if (field.Initializer == null) { if (TypeProcessor.ValueToReference(parentType)) { writer.Write("new "); writer.Write(TypeProcessor.ConvertType(parentType)); writer.Write("()"); } else { writer.Write(TypeProcessor.DefaultValue(TypeProcessor.ConvertType(parentType))); } } else { Core.Write(writer, field.Initializer.As <EqualsValueClauseSyntax>().Value); } writer.Write(";\r\n"); } if (staticConstructorOpt != null && staticConstructorOpt.Body != null) { foreach (var statement in staticConstructorOpt.Body.As <BlockSyntax>().Statements) { Core.Write(writer, statement); } } writer.WriteCloseBrace(); StaticConstructors.Add(TypeState.Instance.TypeName); }
public static void Go(HaxeWriter writer, PropertyDeclarationSyntax property) { Action <AccessorDeclarationSyntax, bool> writeRegion = (region, get) => { writer.WriteIndent(); if (property.Modifiers.Any(SyntaxKind.OverrideKeyword)) { writer.Write("override "); } if (property.Modifiers.Any(SyntaxKind.PublicKeyword) || property.Modifiers.Any(SyntaxKind.ProtectedKeyword) || property.Modifiers.Any(SyntaxKind.InternalKeyword)) { writer.Write("public "); } if (property.Modifiers.Any(SyntaxKind.PrivateKeyword)) { writer.Write("private "); } if (property.Modifiers.Any(SyntaxKind.StaticKeyword)) { writer.Write("static "); } writer.Write("function "); writer.Write(get ? "get_" : "set_"); writer.Write(property.Identifier.ValueText); string type = TypeProcessor.ConvertType(property.Type); if (get) { writer.Write("():" + type); } else { writer.Write("(value:" + type + "):" + type); } writer.WriteLine(); writer.WriteOpenBrace(); if (property.Modifiers.Any(SyntaxKind.AbstractKeyword)) { writer.WriteLine("return throw new Exception(\"Abstract item called\");"); } else if (region.Body == null) { throw new NotImplementedException("Properties in interfaces are not supported " + Utility.Descriptor(region)); } else { foreach (var statement in region.Body.As <BlockSyntax>().Statements) { Core.Write(writer, statement); } if (!get) { //Unfortunately, all haXe property setters must return a value. writer.WriteLine("return " + TypeProcessor.DefaultValue(type) + ";"); } } writer.WriteCloseBrace(); writer.WriteLine(); }; var getter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.GetKeyword); var setter = property.AccessorList.Accessors.SingleOrDefault(o => o.Keyword.Kind() == SyntaxKind.SetKeyword); if (getter == null && setter == null) { throw new Exception("Property must have either a get or a set"); } if (getter != null && setter != null && setter.Body == null && getter.Body == null) { //Both get and set are null, which means this is an automatic property. This is the equivilant of a field in haxe. WriteField.Go(writer, property.Modifiers, property.Identifier.ValueText, property.Type); } else { if (!property.Modifiers.Any(SyntaxKind.OverrideKeyword)) { //Write the property declaration. Overridden properties don't need this. writer.WriteIndent(); if (property.Modifiers.Any(SyntaxKind.PublicKeyword) || property.Modifiers.Any(SyntaxKind.InternalKeyword)) { writer.Write("public "); } if (property.Modifiers.Any(SyntaxKind.StaticKeyword)) { writer.Write("static "); } writer.Write("var "); writer.Write(property.Identifier.ValueText); writer.Write("("); if (getter != null) { writer.Write("get_" + property.Identifier.ValueText); } else { writer.Write("never"); } writer.Write(", "); if (setter != null) { writer.Write("set_" + property.Identifier.ValueText); } else { writer.Write("never"); } writer.Write("):"); writer.Write(TypeProcessor.ConvertType(property.Type)); writer.Write(";\r\n"); } if (getter != null) { writeRegion(getter, true); } if (setter != null) { writeRegion(setter, false); } } }