private StatementSyntax MakeCollectionEqualsTest(
            string comparisonKindKey,
            ExpressionSyntax left,
            ExpressionSyntax right)
        {
            return(SyntaxFactory.IfStatement(
                       // if (!Object.ReferenceEquals(Prop, other.Prop))
                       SyntaxHelper.AreDifferentObjects(left, right),
                       SyntaxFactory.Block(
                           // if (Prop == null || other.Prop == null)
                           SyntaxFactory.IfStatement(
                               SyntaxFactory.BinaryExpression(
                                   SyntaxKind.LogicalOrExpression,
                                   SyntaxHelper.IsNull(left),
                                   SyntaxHelper.IsNull(right)),
                               SyntaxFactory.Block(SyntaxHelper.Return(false))),

                           // if (Prop.Count != other.Prop.Count)
                           SyntaxFactory.IfStatement(
                               SyntaxFactory.BinaryExpression(
                                   SyntaxKind.NotEqualsExpression,
                                   SyntaxFactory.MemberAccessExpression(
                                       SyntaxKind.SimpleMemberAccessExpression,
                                       left,
                                       SyntaxFactory.IdentifierName(CountPropertyName)),
                                   SyntaxFactory.MemberAccessExpression(
                                       SyntaxKind.SimpleMemberAccessExpression,
                                       right,
                                       SyntaxFactory.IdentifierName(CountPropertyName))),
                               SyntaxFactory.Block(SyntaxHelper.Return(false))),

                           CollectionIndexLoop(comparisonKindKey, left, right)
                           )));
        }
Example #2
0
 private MemberDeclarationSyntax GenerateVisitActualMethod()
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
                VisitActualMethodName)
            .AddModifiers(
                SyntaxFactory.Token(SyntaxKind.PublicKeyword),
                SyntaxFactory.Token(SyntaxKind.VirtualKeyword))
            .AddParameterListParameters(
                SyntaxFactory.Parameter(
                    SyntaxFactory.Identifier(NodeParameterName))
                .WithType(
                    SyntaxFactory.ParseTypeName(_nodeInterfaceName)))
            .AddBodyStatements(
                SyntaxFactory.IfStatement(
                    SyntaxHelper.IsNull(NodeParameterName),
                    SyntaxFactory.Block(
                        SyntaxFactory.ThrowStatement(
                            SyntaxFactory.ObjectCreationExpression(
                                SyntaxFactory.ParseTypeName("ArgumentNullException"),
                                SyntaxFactory.ArgumentList(
                                    SyntaxFactory.SingletonSeparatedList(
                                        SyntaxFactory.Argument(
                                            SyntaxFactory.LiteralExpression(
                                                SyntaxKind.StringLiteralExpression,
                                                SyntaxFactory.Literal(NodeParameterName))))),
                                default(InitializerExpressionSyntax))))),
                SyntaxFactory.SwitchStatement(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxFactory.IdentifierName(NodeParameterName),
                        SyntaxFactory.IdentifierName(_kindEnumName)))
                .AddSections(
                    GenerateVisitActualSwitchSections()))
            .WithLeadingTrivia(
                SyntaxHelper.MakeDocComment(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.RewritingVisitorVisitActualMethodSummary,
                        _schemaName),
                    Resources.RewritingVisitorVisitActualMethodReturns,
                    new Dictionary <string, string>
     {
         [NodeParameterName] = Resources.RewritingVisitorVisitActualMethodNodeParameter
     })));
 }
Example #3
0
        private MethodDeclarationSyntax GenerateVisitNullCheckedMethod()
        {
            TypeSyntax typeParameterType = SyntaxFactory.ParseTypeName(TypeParameterName);

            return(SyntaxFactory.MethodDeclaration(
                       typeParameterType,
                       VisitNullCheckedMethodName)
                   .AddModifiers(
                       SyntaxFactory.Token(SyntaxKind.PrivateKeyword))
                   .AddTypeParameterListParameters(
                       SyntaxFactory.TypeParameter(TypeParameterName))
                   .AddConstraintClauses(
                       SyntaxFactory.TypeParameterConstraintClause(
                           SyntaxFactory.IdentifierName(TypeParameterName),
                           SyntaxFactory.SeparatedList(
                               new TypeParameterConstraintSyntax[]
            {
                SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint),
                SyntaxFactory.TypeConstraint(
                    SyntaxFactory.ParseTypeName(_nodeInterfaceName))
            })))
                   .AddParameterListParameters(
                       SyntaxFactory.Parameter(SyntaxFactory.Identifier(NodeParameterName))
                       .WithType(typeParameterType))
                   .AddBodyStatements(
                       SyntaxFactory.IfStatement(
                           SyntaxHelper.IsNull(NodeParameterName),
                           SyntaxFactory.Block(
                               SyntaxFactory.ReturnStatement(
                                   SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)))),
                       SyntaxFactory.ReturnStatement(
                           SyntaxFactory.CastExpression(
                               typeParameterType,
                               SyntaxFactory.InvocationExpression(
                                   SyntaxFactory.IdentifierName(VisitMethodName),
                                   SyntaxFactory.ArgumentList(
                                       SyntaxFactory.SingletonSeparatedList(
                                           SyntaxFactory.Argument(
                                               SyntaxFactory.IdentifierName(NodeParameterName)))))))));
        }
        private IfStatementSyntax MakeDictionaryEqualsTest(
            string propertyInfoKey,
            ExpressionSyntax left,
            ExpressionSyntax right)
        {
            string dictionaryElementVariableName = _localVariableNameGenerator.GetNextCollectionElementVariableName();
            string otherPropertyVariableName     = _localVariableNameGenerator.GetNextCollectionElementVariableName();

            // Construct the key into the PropertyInfoDictionary so we can look up how
            // dictionary elements are to be compared.
            string     valuePropertyInfoKey = PropertyInfoDictionary.MakeDictionaryItemKeyName(propertyInfoKey);
            TypeSyntax dictionaryValueType  = _propertyInfoDictionary[valuePropertyInfoKey].Type;

            return(SyntaxFactory.IfStatement(
                       // if (!Object.ReferenceEquals(left, right))
                       SyntaxHelper.AreDifferentObjects(left, right),
                       SyntaxFactory.Block(
                           // if (left == null || right == null || left.Count != right.Count)
                           SyntaxFactory.IfStatement(
                               SyntaxFactory.BinaryExpression(
                                   SyntaxKind.LogicalOrExpression,
                                   SyntaxHelper.IsNull(left),
                                   SyntaxFactory.BinaryExpression(
                                       SyntaxKind.LogicalOrExpression,
                                       SyntaxHelper.IsNull(right),
                                       SyntaxFactory.BinaryExpression(
                                           SyntaxKind.NotEqualsExpression,
                                           SyntaxFactory.MemberAccessExpression(
                                               SyntaxKind.SimpleMemberAccessExpression,
                                               left,
                                               SyntaxFactory.IdentifierName(CountPropertyName)),
                                           SyntaxFactory.MemberAccessExpression(
                                               SyntaxKind.SimpleMemberAccessExpression,
                                               right,
                                               SyntaxFactory.IdentifierName(CountPropertyName))))),
                               // return false;
                               SyntaxFactory.Block(SyntaxHelper.Return(false))),
                           // foreach (var value_0 in left)
                           SyntaxFactory.ForEachStatement(
                               SyntaxHelper.Var(),
                               dictionaryElementVariableName,
                               left,
                               SyntaxFactory.Block(
                                   // var value_1;
                                   SyntaxFactory.LocalDeclarationStatement(
                                       default(SyntaxTokenList), // modifiers
                                       SyntaxFactory.VariableDeclaration(
                                           dictionaryValueType,
                                           SyntaxFactory.SingletonSeparatedList(
                                               SyntaxFactory.VariableDeclarator(otherPropertyVariableName)))),
                                   // if (!right.TryGetValue(value_0.Key, out value_1))
                                   SyntaxFactory.IfStatement(
                                       SyntaxFactory.PrefixUnaryExpression(
                                           SyntaxKind.LogicalNotExpression,
                                           SyntaxFactory.InvocationExpression(
                                               SyntaxFactory.MemberAccessExpression(
                                                   SyntaxKind.SimpleMemberAccessExpression,
                                                   right,
                                                   SyntaxFactory.IdentifierName("TryGetValue")),
                                               SyntaxFactory.ArgumentList(
                                                   SyntaxFactory.SeparatedList(
                                                       new ArgumentSyntax[]
            {
                SyntaxFactory.Argument(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxFactory.IdentifierName(dictionaryElementVariableName),
                        SyntaxFactory.IdentifierName(KeyPropertyName))),
                SyntaxFactory.Argument(
                    default(NameColonSyntax),
                    SyntaxFactory.Token(SyntaxKind.OutKeyword),
                    SyntaxFactory.IdentifierName(otherPropertyVariableName))
            })))),
                                       // return false;
                                       SyntaxFactory.Block(SyntaxHelper.Return(false))),

                                   GeneratePropertyComparison(
                                       valuePropertyInfoKey,
                                       SyntaxFactory.MemberAccessExpression(
                                           SyntaxKind.SimpleMemberAccessExpression,
                                           SyntaxFactory.IdentifierName(dictionaryElementVariableName),
                                           SyntaxFactory.IdentifierName(ValuePropertyName)),
                                       SyntaxFactory.IdentifierName(otherPropertyVariableName)))))));
        }