예제 #1
0
            private MethodDeclarationSyntax CreateReplaceDescendentDifferentIdentityMethod()
            {
                var currentParameter     = SyntaxFactory.IdentifierName("current");
                var replacementParameter = SyntaxFactory.IdentifierName("replacement");
                var spineVar             = SyntaxFactory.IdentifierName("spine");

                // public TemplateType ReplaceDescendent(TRecursiveType current, TRecursiveType replacement) {
                return(SyntaxFactory.MethodDeclaration(
                           this.applyTo.TypeSyntax,
                           ReplaceDescendentMethodName.Identifier)
                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                       .AddParameterListParameters(
                           SyntaxFactory.Parameter(currentParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax),
                           SyntaxFactory.Parameter(replacementParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
                       .WithBody(SyntaxFactory.Block(
                                     // var spine = this.GetSpine(current);
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
                                                                                 SyntaxFactory.VariableDeclarator(spineVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                           SyntaxFactory.InvocationExpression(Syntax.ThisDot(FastSpineGen.GetSpineMethodName))
                                                                                                                                                           .AddArgumentListArguments(
                                                                                                                                                               SyntaxFactory.Argument(currentParameter)))))),
                                     // if (spine.IsEmpty) {
                                     SyntaxFactory.IfStatement(
                                         SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, spineVar, SyntaxFactory.IdentifierName(nameof(ImmutableStack <int> .IsEmpty))),
                                         //     // The descendent was not found.
                                         //     throw new System.ArgumentException("Old value not found");
                                         SyntaxFactory.Block(
                                             SyntaxFactory.ThrowStatement(
                                                 SyntaxFactory.ObjectCreationExpression(Syntax.GetTypeSyntax(typeof(ArgumentException))).AddArgumentListArguments(
                                                     SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Old value not found."))))))),
                                     // return (TemplateType)this.ReplaceDescendent(spine, ImmutableStack.Create(replacement), spineIncludesDeletedElement: false).Peek();
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.CastExpression(
                                             this.applyTo.TypeSyntax,
                                             SyntaxFactory.InvocationExpression(
                                                 SyntaxFactory.MemberAccessExpression(
                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                     SyntaxFactory.InvocationExpression(Syntax.ThisDot(ReplaceDescendentMethodName))
                                                     .AddArgumentListArguments(
                                                         SyntaxFactory.Argument(spineVar),
                                                         SyntaxFactory.Argument(SyntaxFactory.InvocationExpression(Syntax.CreateImmutableStack()).AddArgumentListArguments(
                                                                                    SyntaxFactory.Argument(replacementParameter))),
                                                         SyntaxFactory.Argument(SyntaxFactory.NameColon("spineIncludesDeletedElement"), NoneToken, SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression))
                                                         ),
                                                     SyntaxFactory.IdentifierName(nameof(ImmutableStack <int> .Peek))),
                                                 SyntaxFactory.ArgumentList()))))));
            }
예제 #2
0
            private MethodDeclarationSyntax CreateAddDescendentMethod()
            {
                var valueParameterName  = SyntaxFactory.IdentifierName("value");
                var parentParameterName = SyntaxFactory.IdentifierName("parent");
                var spineVar            = SyntaxFactory.IdentifierName("spine");
                var newParentVar        = SyntaxFactory.IdentifierName("newParent");
                var newSpineVar         = SyntaxFactory.IdentifierName("newSpine");

                return(SyntaxFactory.MethodDeclaration(
                           GetFullyQualifiedSymbolName(this.generator.applyToSymbol),
                           AddDescendentMethodName.Identifier)
                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                       .AddParameterListParameters(
                           SyntaxFactory.Parameter(valueParameterName.Identifier).WithType(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
                           SyntaxFactory.Parameter(parentParameterName.Identifier).WithType(this.generator.applyToMetaType.RecursiveParent.TypeSyntax))
                       .WithBody(SyntaxFactory.Block(
                                     // var spine = this.GetSpine(parent);
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
                                                                                 SyntaxFactory.VariableDeclarator(spineVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                           SyntaxFactory.InvocationExpression(Syntax.ThisDot(FastSpineGen.GetSpineMethodName))
                                                                                                                                                           .AddArgumentListArguments(SyntaxFactory.Argument(parentParameterName)))))),
                                     // var newParent = parent.With(children: parent.Children.Add(value));
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
                                                                                 SyntaxFactory.VariableDeclarator(newParentVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                               SyntaxFactory.InvocationExpression(
                                                                                                                                                                   SyntaxFactory.MemberAccessExpression(
                                                                                                                                                                       SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                                                                       parentParameterName,
                                                                                                                                                                       WithMethodName))
                                                                                                                                                               .AddArgumentListArguments(SyntaxFactory.Argument(
                                                                                                                                                                                             SyntaxFactory.NameColon(this.applyTo.RecursiveField.NameAsField),
                                                                                                                                                                                             NoneToken,
                                                                                                                                                                                             SyntaxFactory.InvocationExpression(
                                                                                                                                                                                                 SyntaxFactory.MemberAccessExpression(
                                                                                                                                                                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                                                                                                     SyntaxFactory.MemberAccessExpression(
                                                                                                                                                                                                         SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                                                                                                         parentParameterName,
                                                                                                                                                                                                         this.applyTo.RecursiveField.NameAsProperty),
                                                                                                                                                                                                     SyntaxFactory.IdentifierName(nameof(List <int> .Add))),
                                                                                                                                                                                                 SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Argument(valueParameterName)))))))))),
                                     // var newSpine = System.Collections.Immutable.ImmutableStack.Create(value, newParent);
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
                                                                                 SyntaxFactory.VariableDeclarator(newSpineVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                              SyntaxFactory.InvocationExpression(Syntax.CreateImmutableStack())
                                                                                                                                                              .AddArgumentListArguments(
                                                                                                                                                                  SyntaxFactory.Argument(valueParameterName),
                                                                                                                                                                  SyntaxFactory.Argument(newParentVar)))))),
                                     // return (ProjectElementContainer)RecursiveTypeExtensions.ReplaceDescendent(this, spine, newSpine, spineIncludesDeletedElement: false).Peek();
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.CastExpression(
                                             this.applyTo.TypeSyntax,
                                             SyntaxFactory.InvocationExpression(
                                                 SyntaxFactory.MemberAccessExpression(
                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                     SyntaxFactory.InvocationExpression(
                                                         SyntaxFactory.MemberAccessExpression(
                                                             SyntaxKind.SimpleMemberAccessExpression,
                                                             SyntaxFactory.QualifiedName(
                                                                 SyntaxFactory.IdentifierName(nameof(ImmutableObjectGraph)),
                                                                 SyntaxFactory.IdentifierName(nameof(RecursiveTypeExtensions))),
                                                             ReplaceDescendentMethodName))
                                                     .AddArgumentListArguments(
                                                         SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                                         SyntaxFactory.Argument(spineVar),
                                                         SyntaxFactory.Argument(newSpineVar),
                                                         SyntaxFactory.Argument(SyntaxFactory.NameColon("spineIncludesDeletedElement"), NoneToken, SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression))),
                                                     SyntaxFactory.IdentifierName(nameof(ImmutableStack <int> .Peek))),
                                                 SyntaxFactory.ArgumentList())))
                                     )));
            }
예제 #3
0
            private MethodDeclarationSyntax CreateRemoveDescendentMethod()
            {
                var valueParameter = SyntaxFactory.IdentifierName("value");
                var spineVar       = SyntaxFactory.IdentifierName("spine");
                var spineListVar   = SyntaxFactory.IdentifierName("spineList");
                var parentVar      = SyntaxFactory.IdentifierName("parent");
                var newParentVar   = SyntaxFactory.IdentifierName("newParent");
                var newSpineVar    = SyntaxFactory.IdentifierName("newSpine");

                // public TemplateType RemoveDescendent(TRecursiveType value) {
                return(SyntaxFactory.MethodDeclaration(
                           this.applyTo.TypeSyntax,
                           RemoveDescendentMethodName.Identifier)
                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                       .AddParameterListParameters(
                           SyntaxFactory.Parameter(valueParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
                       .WithBody(SyntaxFactory.Block(
                                     // var spine = this.GetSpine(value);
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType)
                                                                             .AddVariables(SyntaxFactory.VariableDeclarator(spineVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                                     SyntaxFactory.InvocationExpression(Syntax.ThisDot(FastSpineGen.GetSpineMethodName)).AddArgumentListArguments(
                                                                                                                                                                         SyntaxFactory.Argument(valueParameter)))))),
                                     // var spineList = spine.ToList();
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType)
                                                                             .AddVariables(SyntaxFactory.VariableDeclarator(spineListVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                                         Syntax.ToList(spineVar))))),
                                     // var parent = (TRecursiveParent)spineList[spineList.Count - 2];
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType)
                                                                             .AddVariables(SyntaxFactory.VariableDeclarator(parentVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                                      SyntaxFactory.CastExpression(
                                                                                                                                                                          this.applyTo.RecursiveParent.TypeSyntax,
                                                                                                                                                                          SyntaxFactory.ElementAccessExpression(
                                                                                                                                                                              spineListVar,
                                                                                                                                                                              SyntaxFactory.BracketedArgumentList(
                                                                                                                                                                                  SyntaxFactory.SingletonSeparatedList(
                                                                                                                                                                                      SyntaxFactory.Argument(SyntaxFactory.BinaryExpression(
                                                                                                                                                                                                                 SyntaxKind.SubtractExpression,
                                                                                                                                                                                                                 SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, spineListVar, SyntaxFactory.IdentifierName(nameof(List <int> .Count))),
                                                                                                                                                                                                                 SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(2)))))))))))),
                                     // var newParent = parent.With(children: parent.Children.Remove(spineList[spineList.Count - 1]));
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType)
                                                                             .AddVariables(SyntaxFactory.VariableDeclarator(newParentVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                                         SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentVar, WithMethodName))
                                                                                                                                                                         .AddArgumentListArguments(
                                                                                                                                                                             SyntaxFactory.Argument(
                                                                                                                                                                                 SyntaxFactory.NameColon(this.applyTo.RecursiveField.NameAsField),
                                                                                                                                                                                 NoneToken,
                                                                                                                                                                                 SyntaxFactory.InvocationExpression(       // parent.Children.Remove(...)
                                                                                                                                                                                     SyntaxFactory.MemberAccessExpression( // parent.Children.Remove
                                                                                                                                                                                         SyntaxKind.SimpleMemberAccessExpression,
                                                                                                                                                                                         SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentVar, this.applyTo.RecursiveField.NameAsProperty),
                                                                                                                                                                                         SyntaxFactory.IdentifierName(nameof(List <int> .Remove))))
                                                                                                                                                                                 .AddArgumentListArguments(
                                                                                                                                                                                     SyntaxFactory.Argument(SyntaxFactory.ElementAccessExpression( // spineList[spineList.Count - 1]
                                                                                                                                                                                                                spineListVar,
                                                                                                                                                                                                                SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList(
                                                                                                                                                                                                                                                        SyntaxFactory.Argument( // spineList.Count - 1
                                                                                                                                                                                                                                                            SyntaxFactory.BinaryExpression(
                                                                                                                                                                                                                                                                SyntaxKind.SubtractExpression,
                                                                                                                                                                                                                                                                SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, spineListVar, SyntaxFactory.IdentifierName(nameof(List <int> .Count))),
                                                                                                                                                                                                                                                                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1))))))))))))))),
                                     // var newSpine = System.Collections.Immutable.ImmutableStack.Create<TRecursiveType>(newParent);
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType)
                                                                             .AddVariables(SyntaxFactory.VariableDeclarator(newSpineVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                                        SyntaxFactory.InvocationExpression(Syntax.CreateImmutableStack(this.applyTo.RecursiveType.TypeSyntax))
                                                                                                                                                                        .AddArgumentListArguments(SyntaxFactory.Argument(newParentVar)))))),
                                     // return (TRecursiveParent)this.ReplaceDescendent(spine, newSpine, spineIncludesDeletedElement: true).Peek();
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.CastExpression(
                                             this.applyTo.RecursiveParent.TypeSyntax,
                                             SyntaxFactory.InvocationExpression(
                                                 SyntaxFactory.MemberAccessExpression(
                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                     SyntaxFactory.InvocationExpression(Syntax.ThisDot(ReplaceDescendentMethodName))
                                                     .AddArgumentListArguments(
                                                         SyntaxFactory.Argument(spineVar),
                                                         SyntaxFactory.Argument(newSpineVar),
                                                         SyntaxFactory.Argument(SyntaxFactory.NameColon("spineIncludesDeletedElement"), NoneToken, SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression))
                                                         ),
                                                     SyntaxFactory.IdentifierName(nameof(ImmutableStack <int> .Peek))),
                                                 SyntaxFactory.ArgumentList()))))));
            }