public static void Go(HaxeWriter writer, ObjectCreationExpressionSyntax expression) { if (expression.ArgumentList == null) { throw new Exception("Types must be initialized with parenthesis. Object initialization syntax is not supported. " + Utility.Descriptor(expression)); } var model = Program.GetModel(expression); var type = model.GetTypeInfo(expression).Type; if (type.SpecialType == SpecialType.System_Object) { //new object() results in the CsObject type being made. This is only really useful for locking writer.Write("new CsObject()"); } else { var methodSymbol = model.GetSymbolInfo(expression).Symbol.As <IMethodSymbol>(); var translateOpt = MethodTranslation.Get(methodSymbol); writer.Write("new "); writer.Write(TypeProcessor.ConvertType(expression.Type)); writer.Write("("); bool first = true; foreach (var param in TranslateParameters(translateOpt, WriteInvocationExpression.SortArguments(methodSymbol, expression.ArgumentList.Arguments, expression), expression)) { if (first) { first = false; } else { writer.Write(", "); } param.Write(writer); } writer.Write(")"); } }
private static void Factory(HaxeWriter writer, SyntaxNode node) { if (node is MethodDeclarationSyntax) { WriteMethod.Go(writer, node.As <MethodDeclarationSyntax>()); } else if (node is PropertyDeclarationSyntax) { WriteProperty.Go(writer, node.As <PropertyDeclarationSyntax>()); } else if (node is FieldDeclarationSyntax) { WriteField.Go(writer, node.As <FieldDeclarationSyntax>()); } else if (node is ConstructorDeclarationSyntax) { WriteConstructor.Go(writer, node.As <ConstructorDeclarationSyntax>()); } else if (node is ExpressionStatementSyntax) { WriteStatement(writer, node.As <ExpressionStatementSyntax>()); } else if (node is LocalDeclarationStatementSyntax) { WriteLocalDeclaration.Go(writer, node.As <LocalDeclarationStatementSyntax>()); } else if (node is BlockSyntax) { WriteBlock(writer, node.As <BlockSyntax>()); } else if (node is InvocationExpressionSyntax) { WriteInvocationExpression.Go(writer, node.As <InvocationExpressionSyntax>()); } else if (node is LiteralExpressionSyntax) { WriteLiteralExpression.Go(writer, node.As <LiteralExpressionSyntax>()); } else if (node is IdentifierNameSyntax) { WriteIdentifierName.Go(writer, node.As <IdentifierNameSyntax>()); } else if (node is ImplicitArrayCreationExpressionSyntax) { WriteArrayCreationExpression.Go(writer, node.As <ImplicitArrayCreationExpressionSyntax>()); } else if (node is ArrayCreationExpressionSyntax) { WriteArrayCreationExpression.Go(writer, node.As <ArrayCreationExpressionSyntax>()); } else if (node is MemberAccessExpressionSyntax) { WriteMemberAccessExpression.Go(writer, node.As <MemberAccessExpressionSyntax>()); } else if (node is ParenthesizedLambdaExpressionSyntax) { WriteLambdaExpression.Go(writer, node.As <ParenthesizedLambdaExpressionSyntax>()); } else if (node is SimpleLambdaExpressionSyntax) { WriteLambdaExpression.Go(writer, node.As <SimpleLambdaExpressionSyntax>()); } else if (node is ReturnStatementSyntax) { WriteReturnStatement.Go(writer, node.As <ReturnStatementSyntax>()); } else if (node is ObjectCreationExpressionSyntax) { WriteObjectCreationExpression.Go(writer, node.As <ObjectCreationExpressionSyntax>()); } else if (node is ElementAccessExpressionSyntax) { WriteElementAccessExpression.Go(writer, node.As <ElementAccessExpressionSyntax>()); } else if (node is ForEachStatementSyntax) { WriteForEachStatement.Go(writer, node.As <ForEachStatementSyntax>()); } else if (node is IfStatementSyntax) { WriteIfStatement.Go(writer, node.As <IfStatementSyntax>()); } else if (node is BinaryExpressionSyntax) { WriteBinaryExpression.Go(writer, node.As <BinaryExpressionSyntax>()); } else if (node is ConditionalExpressionSyntax) { WriteConditionalExpression.Go(writer, node.As <ConditionalExpressionSyntax>()); } else if (node is BaseExpressionSyntax) { WriteBaseExpression.Go(writer, node.As <BaseExpressionSyntax>()); } else if (node is ThisExpressionSyntax) { WriteThisExpression.Go(writer, node.As <ThisExpressionSyntax>()); } else if (node is CastExpressionSyntax) { WriteCastExpression.Go(writer, node.As <CastExpressionSyntax>()); } else if (node is ThrowStatementSyntax) { WriteThrowStatement.Go(writer, node.As <ThrowStatementSyntax>()); } else if (node is ThrowExpressionSyntax) { WriteThrowStatement.GoExpression(writer, node.As <ThrowExpressionSyntax>()); } else if (node is PrefixUnaryExpressionSyntax) { WriteUnaryExpression.Go(writer, node.As <PrefixUnaryExpressionSyntax>()); } else if (node is PostfixUnaryExpressionSyntax) { WriteUnaryExpression.Go(writer, node.As <PostfixUnaryExpressionSyntax>()); } else if (node is EqualsValueClauseSyntax) { WriteEqualsValueClause.Go(writer, node.As <EqualsValueClauseSyntax>()); } else if (node is ForStatementSyntax) { WriteForStatement.Go(writer, node.As <ForStatementSyntax>()); } else if (node is WhileStatementSyntax) { WriteWhileStatement.Go(writer, node.As <WhileStatementSyntax>()); } else if (node is BreakStatementSyntax) { WriteBreakStatement.Go(writer, node.As <BreakStatementSyntax>()); } else if (node is DoStatementSyntax) { WriteDoStatement.Go(writer, node.As <DoStatementSyntax>()); } else if (node is SwitchStatementSyntax) { WriteSwitchStatement.Go(writer, node.As <SwitchStatementSyntax>()); } else if (node is TryStatementSyntax) { WriteTryStatement.Go(writer, node.As <TryStatementSyntax>()); } else if (node is UsingStatementSyntax) { WriteUsingStatement.Go(writer, node.As <UsingStatementSyntax>()); } else if (node is ParenthesizedExpressionSyntax) { WriteParenthesizedExpression.Go(writer, node.As <ParenthesizedExpressionSyntax>()); } else if (node is LockStatementSyntax) { WriteLockStatement.Go(writer, node.As <LockStatementSyntax>()); } else if (node is ContinueStatementSyntax) { WriteContinueStatement.Go(writer, node.As <ContinueStatementSyntax>()); } else if (node is TypeOfExpressionSyntax) { WriteTypeOfExpression.Go(writer, node.As <TypeOfExpressionSyntax>()); } else if (node is AnonymousObjectCreationExpressionSyntax) { WriteAnonymousObjectCreationExpression.Go(writer, node.As <AnonymousObjectCreationExpressionSyntax>()); } else if (node is EmptyStatementSyntax) { return; //ignore empty statements } else if (node is DelegateDeclarationSyntax) { return; //don't write delegates - we convert them to types directly } else if (node is EventFieldDeclarationSyntax) { WriteEventFieldDeclaration.Go(writer, node.As <EventFieldDeclarationSyntax>()); } else if (node is DefaultExpressionSyntax) { WriteDefaultExpression.Go(writer, node.As <DefaultExpressionSyntax>()); } else if (node is GenericNameSyntax) { WriteGenericName.Go(writer, node.As <GenericNameSyntax>()); } else if (node is ConversionOperatorDeclarationSyntax) { WriteConversionOperatorDeclaration.Go(writer, node.As <ConversionOperatorDeclarationSyntax>()); } else if (node is AssignmentExpressionSyntax) { WriteAssignmentExpressionSyntax.Go(writer, node.As <AssignmentExpressionSyntax>()); } else if (node is OperatorDeclarationSyntax) { WriteMethod.WriteOperatorDeclaration(writer, node.As <OperatorDeclarationSyntax>()); } else if (node is IndexerDeclarationSyntax) { WriteMethod.WriteIndexerDeclaration(writer, node.As <IndexerDeclarationSyntax>()); } else { throw new NotImplementedException(node.GetType().Name + " is not supported. " + Utility.Descriptor(node)); } }
public static void Go(HaxeWriter writer, ObjectCreationExpressionSyntax expression) { if (expression.ArgumentList == null || expression.Initializer != null) { throw new Exception("Object initialization syntax is not supported. " + Utility.Descriptor(expression)); } var model = Program.GetModel(expression); var type = model.GetTypeInfo(expression).Type; var methodSymbolUntyped = model.GetSymbolInfo(expression).Symbol; if (methodSymbolUntyped == null) { throw new Exception("methodSymbolUntyped is null"); } var methodSymbol = (IMethodSymbol)methodSymbolUntyped; if (type.SpecialType == SpecialType.System_DateTime && expression.ArgumentList.Arguments.Count == 1) { throw new Exception("You cannot use the DateTime constructor with one argument (ticks). .net Ticks and Haxe Ticks have different meanings, so this would result in problems. " + Utility.Descriptor(expression)); } if (type.SpecialType == SpecialType.System_Object) { //new object() results in the CsObject type being made. This is only really useful for locking writer.Write("new CsObject()"); } else if (type.SpecialType == SpecialType.System_String) { //new String() writer.Write("Cs2Hx.NewString("); bool first = true; foreach (var param in WriteInvocationExpression.SortArguments(methodSymbol, expression.ArgumentList.Arguments, expression, false)) { if (first) { first = false; } else { writer.Write(", "); } param.Write(writer); } writer.Write(")"); } else { var translateOpt = MethodTranslation.Get(methodSymbol); var convertedType = TypeProcessor.ConvertType(expression.Type); if (convertedType == "String") { //Normally, writing "new String(" in C# will fall under the above check which calls Cs2Hx.NewString. However, if a translation changes a type into a string, such as with guids, it falls here. It's important not to ever write "new String(" in haxe since that makes copies of strings which don't compare properly with ==. So just embed the string straight. Core.Write(writer, expression.ArgumentList.Arguments.Single().Expression); } else { writer.Write("new "); writer.Write(convertedType); writer.Write("("); bool first = true; foreach (var param in TranslateParameters(translateOpt, WriteInvocationExpression.SortArguments(methodSymbol, expression.ArgumentList.Arguments, expression, false), expression)) { if (first) { first = false; } else { writer.Write(", "); } param.Write(writer); } writer.Write(")"); } } }