private static ExpressionSyntax VisitAnonymousClassCreationExpression(ConversionContext context, ObjectCreationExpr newExpr, List <BodyDeclaration> anonBody) { string baseTypeName = TypeHelper.ConvertType(newExpr.getType().getName()); string anonTypeName = string.Empty; for (int i = 0; i <= 100; i++) { if (i == 100) { throw new InvalidOperationException("Too many anonymous types"); } anonTypeName = string.Format("Anonymous{0}{1}", baseTypeName, i == 0 ? string.Empty : i.ToString()); if (!context.UsedAnonymousTypeNames.Contains(anonTypeName)) { context.UsedAnonymousTypeNames.Add(anonTypeName); break; // go with this one } } var classSyntax = SyntaxFactory.ClassDeclaration(anonTypeName) .AddModifiers( SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.SealedKeyword)) .WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList(new List <BaseTypeSyntax> { SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseTypeName)) }))); var parentField = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName(context.LastTypeName)).AddVariables(SyntaxFactory.VariableDeclarator("parent"))) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)); var ctorSyntax = SyntaxFactory.ConstructorDeclaration(anonTypeName) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) .AddParameterListParameters(SyntaxFactory.Parameter(SyntaxFactory.ParseToken("parent")).WithType(SyntaxFactory.ParseTypeName(context.LastTypeName))) .AddBodyStatements(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName("parent")), SyntaxFactory.IdentifierName("parent")))); classSyntax = classSyntax.AddMembers(ctorSyntax, parentField); foreach (var member in anonBody) { var memberSyntax = BodyDeclarationVisitor.VisitBodyDeclarationForClass(context, classSyntax, member); if (memberSyntax != null) { classSyntax = classSyntax.AddMembers(memberSyntax); } } context.PendingAnonymousTypes.Enqueue(classSyntax); var args = newExpr.getArgs().ToList <Expression>(); if (args == null || args.Count == 0) { return(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(anonTypeName)) .AddArgumentListArguments(SyntaxFactory.Argument(SyntaxFactory.ThisExpression()))); } var argSyntaxes = new List <ArgumentSyntax>(); argSyntaxes.Add(SyntaxFactory.Argument(SyntaxFactory.ThisExpression())); foreach (var arg in args) { var argSyntax = ExpressionVisitor.VisitExpression(context, arg); argSyntaxes.Add(SyntaxFactory.Argument(argSyntax)); } return(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.ParseTypeName(anonTypeName), SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(argSyntaxes, Enumerable.Repeat(SyntaxFactory.Token(SyntaxKind.CommaToken), argSyntaxes.Count - 1))), null)); }
public override ExpressionSyntax Visit(ConversionContext context, BinaryExpr binaryExpr) { var leftExpr = binaryExpr.getLeft(); var leftSyntax = ExpressionVisitor.VisitExpression(context, leftExpr); var rightExpr = binaryExpr.getRight(); var rightSyntax = ExpressionVisitor.VisitExpression(context, rightExpr); var op = binaryExpr.getOperator(); SyntaxKind kind = SyntaxKind.None; if (op == BinaryExpr.Operator.and) { kind = SyntaxKind.LogicalAndExpression; } else if (op == BinaryExpr.Operator.binAnd) { kind = SyntaxKind.BitwiseAndExpression; } else if (op == BinaryExpr.Operator.binOr) { kind = SyntaxKind.BitwiseOrExpression; } else if (op == BinaryExpr.Operator.divide) { kind = SyntaxKind.DivideExpression; } else if (op == BinaryExpr.Operator.equals) { kind = SyntaxKind.EqualsExpression; } else if (op == BinaryExpr.Operator.greater) { kind = SyntaxKind.GreaterThanExpression; } else if (op == BinaryExpr.Operator.greaterEquals) { kind = SyntaxKind.GreaterThanOrEqualExpression; } else if (op == BinaryExpr.Operator.less) { kind = SyntaxKind.LessThanExpression; } else if (op == BinaryExpr.Operator.lessEquals) { kind = SyntaxKind.LessThanOrEqualExpression; } else if (op == BinaryExpr.Operator.lShift) { kind = SyntaxKind.LeftShiftExpression; } else if (op == BinaryExpr.Operator.minus) { kind = SyntaxKind.SubtractExpression; } else if (op == BinaryExpr.Operator.notEquals) { kind = SyntaxKind.NotEqualsExpression; } else if (op == BinaryExpr.Operator.or) { kind = SyntaxKind.LogicalOrExpression; } else if (op == BinaryExpr.Operator.plus) { kind = SyntaxKind.AddExpression; } else if (op == BinaryExpr.Operator.remainder) { kind = SyntaxKind.ModuloExpression; } else if (op == BinaryExpr.Operator.rSignedShift) { kind = SyntaxKind.RightShiftExpression; } else if (op == BinaryExpr.Operator.rUnsignedShift) { kind = SyntaxKind.RightShiftExpression; context.Options.Warning("Use of unsigned right shift in original code; verify correctness.", binaryExpr.getBegin().line); } else if (op == BinaryExpr.Operator.times) { kind = SyntaxKind.MultiplyExpression; } else if (op == BinaryExpr.Operator.xor) { kind = SyntaxKind.ExclusiveOrExpression; } return(SyntaxFactory.BinaryExpression(kind, leftSyntax, rightSyntax)); }