Exemplo n.º 1
0
            private void ImplementSortedChildrenInterface()
            {
                this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.GetTypeSyntax(typeof(IRecursiveParentWithSortedChildren))));

                // int IRecursiveParentWithSortedChildren.Compare(IRecursiveType first, IRecursiveType second)
                var firstParameterName  = SyntaxFactory.IdentifierName("first");
                var secondParameterName = SyntaxFactory.IdentifierName("second");

                this.innerMembers.Add(SyntaxFactory.MethodDeclaration(
                                          SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
                                          nameof(IRecursiveParentWithSortedChildren.Compare))
                                      .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(SyntaxFactory.IdentifierName(nameof(IRecursiveParentWithSortedChildren))))
                                      .AddParameterListParameters(
                                          SyntaxFactory.Parameter(firstParameterName.Identifier).WithType(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
                                          SyntaxFactory.Parameter(secondParameterName.Identifier).WithType(Syntax.GetTypeSyntax(typeof(IRecursiveType))))
                                      .WithBody(SyntaxFactory.Block(
                                                    // return this.Children.KeyComparer.Compare((<#= templateType.RecursiveType.TypeName #>)first, (<#= templateType.RecursiveType.TypeName #>)second);
                                                    SyntaxFactory.ReturnStatement(
                                                        SyntaxFactory.InvocationExpression(
                                                            SyntaxFactory.MemberAccessExpression(
                                                                SyntaxKind.SimpleMemberAccessExpression,
                                                                SyntaxFactory.MemberAccessExpression(
                                                                    SyntaxKind.SimpleMemberAccessExpression,
                                                                    Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name.ToPascalCase())),
                                                                    SyntaxFactory.IdentifierName(nameof(ImmutableSortedSet <int> .KeyComparer))),
                                                                SyntaxFactory.IdentifierName(nameof(IComparer <int> .Compare))),
                                                            SyntaxFactory.ArgumentList(Syntax.JoinSyntaxNodes(SyntaxKind.CommaToken,
                                                                                                              SyntaxFactory.Argument(SyntaxFactory.CastExpression(this.generator.applyToMetaType.RecursiveType.TypeSyntax, firstParameterName)),
                                                                                                              SyntaxFactory.Argument(SyntaxFactory.CastExpression(this.generator.applyToMetaType.RecursiveType.TypeSyntax, secondParameterName)))))))));
            }
Exemplo n.º 2
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))
                       .AddAttributeLists(PureAttributeList)
                       .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)ImmutableObjectGraph.RecursiveTypeExtensions.ReplaceDescendent(this, spine, ImmutableStack.Create(replacement), spineIncludesDeletedElement: false).Peek();
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.CastExpression(
                                             this.applyTo.TypeSyntax,
                                             SyntaxFactory.InvocationExpression(
                                                 SyntaxFactory.MemberAccessExpression(
                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                     SyntaxFactory.InvocationExpression(
                                                         SyntaxFactory.MemberAccessExpression(
                                                             SyntaxKind.SimpleMemberAccessExpression,
                                                             Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                                             ReplaceDescendentMethodName))
                                                     .AddArgumentListArguments(
                                                         SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                                         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()))))));
            }
Exemplo n.º 3
0
            protected override void GenerateCore()
            {
                this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.GetTypeSyntax(typeof(IRecursiveType))));

                //// uint IRecursiveType.Identity => this.Identity;
                this.innerMembers.Add(SyntaxFactory.PropertyDeclaration(
                                          IdentityFieldTypeSyntax,
                                          nameof(IRecursiveType.Identity))
                                      .WithExplicitInterfaceSpecifier(
                                          SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveType))))
                                      .WithExpressionBody(
                                          SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(IdentityPropertyName)))
                                      .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                                      .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));
            }
Exemplo n.º 4
0
 protected MethodDeclarationSyntax CreateFindMethod()
 {
     // public TRecursiveType Find(uint identity)
     return(SyntaxFactory.MethodDeclaration(this.applyTo.RecursiveType.TypeSyntax, FindMethodName.Identifier)
            .AddParameterListParameters(RequiredIdentityParameter)
            .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
            .AddAttributeLists(PureAttributeList)
            .WithBody(SyntaxFactory.Block(
                          // return ImmutableObjectGraph.RecursiveTypeExtensions.Find<TRecursiveParent, TRecursiveType>(this, identity);
                          SyntaxFactory.ReturnStatement(
                              SyntaxFactory.InvocationExpression(
                                  SyntaxFactory.MemberAccessExpression(
                                      SyntaxKind.SimpleMemberAccessExpression,
                                      Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                      SyntaxFactory.GenericName(FindMethodName.Identifier).AddTypeArgumentListArguments(
                                          this.applyTo.RecursiveParent.TypeSyntax,
                                          this.applyTo.RecursiveType.TypeSyntax)))
                              .AddArgumentListArguments(
                                  SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                  SyntaxFactory.Argument(IdentityParameterName))))));
 }
            private MethodDeclarationSyntax CreateSyncImmediateChildToCurrentVersionMethod()
            {
                var childParameterName  = SyntaxFactory.IdentifierName("child");
                var childType           = GetFullyQualifiedSymbolName(this.generator.applyToMetaType.RecursiveField.ElementType);
                var currentValueVarName = SyntaxFactory.IdentifierName("currentValue");

                return(SyntaxFactory.MethodDeclaration(
                           childType,
                           SyncImmediateChildToCurrentVersionMethodName.Identifier)
                       .AddParameterListParameters(SyntaxFactory.Parameter(childParameterName.Identifier).WithType(childType))
                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword))
                       .WithBody(SyntaxFactory.Block(
                                     // ElementTypeName currentValue;
                                     SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(
                                                                                 childType,
                                                                                 SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(currentValueVarName.Identifier)))),
                                     // if (!this.TryFindImmediateChild(child.<#= templateType.RequiredIdentityField.NamePascalCase #>, out currentValue)) {
                                     SyntaxFactory.IfStatement(
                                         SyntaxFactory.PrefixUnaryExpression(
                                             SyntaxKind.LogicalNotExpression,
                                             SyntaxFactory.InvocationExpression(
                                                 Syntax.ThisDot(SyntaxFactory.IdentifierName(nameof(RecursiveTypeExtensions.TryFindImmediateChild))),
                                                 SyntaxFactory.ArgumentList(Syntax.JoinSyntaxNodes(
                                                                                SyntaxKind.CommaToken,
                                                                                SyntaxFactory.Argument(
                                                                                    SyntaxFactory.MemberAccessExpression(
                                                                                        SyntaxKind.SimpleMemberAccessExpression,
                                                                                        childParameterName,
                                                                                        IdentityPropertyName)),
                                                                                SyntaxFactory.Argument(
                                                                                    null,
                                                                                    SyntaxFactory.Token(SyntaxKind.OutKeyword),
                                                                                    currentValueVarName))))),
                                         SyntaxFactory.Block(
                                             SyntaxFactory.ThrowStatement(SyntaxFactory.ObjectCreationExpression(
                                                                              Syntax.GetTypeSyntax(typeof(ArgumentException)),
                                                                              SyntaxFactory.ArgumentList(),
                                                                              null)))),
                                     SyntaxFactory.ReturnStatement(currentValueVarName))));
            }
Exemplo n.º 6
0
            private void ImplementOrderedChildrenInterface()
            {
                // We only need to declare this interface if the children are not sorted,
                // since sorted children merit a derived interface making this redundant.
                if (!this.generator.applyToMetaType.ChildrenAreSorted)
                {
                    this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.GetTypeSyntax(typeof(IRecursiveParentWithOrderedChildren))));
                }

                // IReadOnlyList<IRecursiveType> IRecursiveParentWithOrderedChildren.Children => this.children;
                this.innerMembers.Add(SyntaxFactory.PropertyDeclaration(
                                          Syntax.IReadOnlyListOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
                                          nameof(IRecursiveParentWithOrderedChildren.Children))
                                      .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(SyntaxFactory.IdentifierName(nameof(IRecursiveParentWithOrderedChildren))))
                                      .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name))))
                                      .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                                      .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                // int IRecursiveParentWithOrderedChildren.IndexOf(IRecursiveType value)
                var valueParameterName = SyntaxFactory.IdentifierName("value");

                this.innerMembers.Add(SyntaxFactory.MethodDeclaration(
                                          SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
                                          nameof(IRecursiveParentWithOrderedChildren.IndexOf))
                                      .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(SyntaxFactory.IdentifierName(nameof(IRecursiveParentWithOrderedChildren))))
                                      .AddParameterListParameters(SyntaxFactory.Parameter(valueParameterName.Identifier).WithType(Syntax.GetTypeSyntax(typeof(IRecursiveType))))
                                      .WithBody(SyntaxFactory.Block(
                                                    // return this.Children.IndexOf((<#= templateType.RecursiveType.TypeName #>)value);
                                                    SyntaxFactory.ReturnStatement(
                                                        SyntaxFactory.InvocationExpression(
                                                            SyntaxFactory.MemberAccessExpression(
                                                                SyntaxKind.SimpleMemberAccessExpression,
                                                                Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name.ToPascalCase())),
                                                                SyntaxFactory.IdentifierName(nameof(IList <int> .IndexOf))),
                                                            SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(
                                                                                           SyntaxFactory.Argument(
                                                                                               SyntaxFactory.CastExpression(
                                                                                                   this.generator.applyToMetaType.RecursiveType.TypeSyntax,
                                                                                                   valueParameterName)))))))));
            }
Exemplo n.º 7
0
            private void ImplementIEnumerableInterfaces()
            {
                this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.IEnumerableOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax)));

                // return ((IEnumerable<RecursiveType>)this.<#=templateType.RecursiveField.NameCamelCase#>).GetEnumerator();
                var body = SyntaxFactory.Block(
                    SyntaxFactory.ReturnStatement(
                        SyntaxFactory.InvocationExpression(
                            SyntaxFactory.MemberAccessExpression(
                                SyntaxKind.SimpleMemberAccessExpression,
                                SyntaxFactory.ParenthesizedExpression(
                                    SyntaxFactory.CastExpression(
                                        Syntax.IEnumerableOf(GetFullyQualifiedSymbolName(this.generator.applyToMetaType.RecursiveField.ElementType)),
                                        Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name)))),
                                SyntaxFactory.IdentifierName(nameof(IEnumerable <int> .GetEnumerator))),
                            SyntaxFactory.ArgumentList())));

                // public System.Collections.Generic.IEnumerator<RecursiveType> GetEnumerator()
                this.innerMembers.Add(
                    SyntaxFactory.MethodDeclaration(
                        Syntax.IEnumeratorOf(GetFullyQualifiedSymbolName(this.generator.applyToMetaType.RecursiveField.ElementType)),
                        nameof(IEnumerable <int> .GetEnumerator))
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                    .WithBody(body));

                // System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
                this.innerMembers.Add(
                    SyntaxFactory.MethodDeclaration(
                        Syntax.GetTypeSyntax(typeof(System.Collections.IEnumerator)),
                        nameof(IEnumerable <int> .GetEnumerator))
                    .WithExplicitInterfaceSpecifier(
                        SyntaxFactory.ExplicitInterfaceSpecifier(
                            SyntaxFactory.QualifiedName(
                                SyntaxFactory.QualifiedName(
                                    SyntaxFactory.IdentifierName(nameof(System)),
                                    SyntaxFactory.IdentifierName(nameof(System.Collections))),
                                SyntaxFactory.IdentifierName(nameof(System.Collections.IEnumerable)))))
                    .WithBody(body));
            }
Exemplo n.º 8
0
            private MethodDeclarationSyntax CreateGetParentMethod()
            {
                // public TRecursiveParent GetParent(TRecursiveType descendent)
                var descendentParam = SyntaxFactory.IdentifierName("descendent");

                return(SyntaxFactory.MethodDeclaration(this.applyTo.RecursiveParent.TypeSyntax, GetParentMethodName.Identifier)
                       .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                       .AddAttributeLists(PureAttributeList)
                       .AddParameterListParameters(SyntaxFactory.Parameter(descendentParam.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
                       .WithBody(SyntaxFactory.Block(
                                     // return ImmutableObjectGraph.RecursiveTypeExtensions.GetParent<TRecursiveParent, TRecursiveType>(this, descendent);
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.InvocationExpression(
                                             SyntaxFactory.MemberAccessExpression(
                                                 SyntaxKind.SimpleMemberAccessExpression,
                                                 Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                                 SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetParent))
                                                 .AddTypeArgumentListArguments(this.applyTo.RecursiveParent.TypeSyntax, this.applyTo.RecursiveType.TypeSyntax)))
                                         .AddArgumentListArguments(
                                             SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                             SyntaxFactory.Argument(descendentParam))))));
            }
Exemplo n.º 9
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))
                       .AddAttributeLists(PureAttributeList)
                       .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)ImmutableObjectGraph.RecursiveTypeExtensions.ReplaceDescendent(this, spine, newSpine, spineIncludesDeletedElement: true).Peek();
                                     SyntaxFactory.ReturnStatement(
                                         SyntaxFactory.CastExpression(
                                             this.applyTo.RecursiveParent.TypeSyntax,
                                             SyntaxFactory.InvocationExpression(
                                                 SyntaxFactory.MemberAccessExpression(
                                                     SyntaxKind.SimpleMemberAccessExpression,
                                                     SyntaxFactory.InvocationExpression(
                                                         SyntaxFactory.MemberAccessExpression(
                                                             SyntaxKind.SimpleMemberAccessExpression,
                                                             Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                                             ReplaceDescendentMethodName))
                                                     .AddArgumentListArguments(
                                                         SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                                         SyntaxFactory.Argument(spineVar),
                                                         SyntaxFactory.Argument(newSpineVar),
                                                         SyntaxFactory.Argument(SyntaxFactory.NameColon("spineIncludesDeletedElement"), NoneToken, SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression))
                                                         ),
                                                     SyntaxFactory.IdentifierName(nameof(ImmutableStack <int> .Peek))),
                                                 SyntaxFactory.ArgumentList()))))));
            }
Exemplo n.º 10
0
            protected override void GenerateCore()
            {
                if (this.applyTo.IsRecursiveParent)
                {
                    this.baseTypes.Add(SyntaxFactory.SimpleBaseType(this.IRecursiveParentWithChildReplacementType));
                    this.innerMembers.Add(this.CreateReplaceChildMethod());

                    // private readonly uint inefficiencyLoad;
                    var inefficiencyLoadType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UIntKeyword));
                    this.innerMembers.Add(SyntaxFactory.FieldDeclaration(
                                              SyntaxFactory.VariableDeclaration(inefficiencyLoadType)
                                              .AddVariables(SyntaxFactory.VariableDeclarator(InefficiencyLoadFieldName.Identifier)))
                                          .AddModifiers(
                                              SyntaxFactory.Token(SyntaxKind.PrivateKeyword),
                                              SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
                                          .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                    var interfaceType = SyntaxFactory.QualifiedName(
                        SyntaxFactory.IdentifierName(nameof(ImmutableObjectGraph)),
                        SyntaxFactory.GenericName(
                            SyntaxFactory.Identifier(nameof(IRecursiveParentWithLookupTable <IRecursiveType>)),
                            SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(this.applyTo.RecursiveType.TypeSyntax))));
                    this.baseTypes.Add(SyntaxFactory.SimpleBaseType(interfaceType));
                    var explicitImplementation = SyntaxFactory.ExplicitInterfaceSpecifier(interfaceType);

                    // uint IRecursiveParentWithLookupTable<TRecursiveType>.InefficiencyLoad => this.ineffiencyLoad;
                    this.innerMembers.Add(
                        SyntaxFactory.PropertyDeclaration(inefficiencyLoadType, nameof(IRecursiveParentWithLookupTable <IRecursiveType> .InefficiencyLoad))
                        .WithExplicitInterfaceSpecifier(explicitImplementation)
                        .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(InefficiencyLoadFieldName)))
                        .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                        .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                    // IReadOnlyCollection<TRecursiveType> IRecursiveParentWithLookupTable<TRecursiveType>.Children => this.recursiveField;
                    this.innerMembers.Add(
                        SyntaxFactory.PropertyDeclaration(
                            Syntax.IReadOnlyCollectionOf(this.applyTo.RecursiveType.TypeSyntax),
                            nameof(IRecursiveParentWithLookupTable <IRecursiveType> .Children))
                        .WithExplicitInterfaceSpecifier(explicitImplementation)
                        .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(this.applyTo.RecursiveField.NameAsProperty)))
                        .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                        .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                    // ImmutableDictionary<IdentityFieldType, KeyValuePair<TRecursiveType, IdentityFieldType>> IRecursiveParentWithLookupTable<TRecursiveType>.LookupTable => this.lookupTable;
                    this.innerMembers.Add(
                        SyntaxFactory.PropertyDeclaration(
                            this.lookupTableType,
                            nameof(IRecursiveParentWithLookupTable <IRecursiveType> .LookupTable))
                        .WithExplicitInterfaceSpecifier(explicitImplementation)
                        .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(LookupTablePropertyName)))
                        .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                        .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));
                }

                if (this.applyTo.IsRecursive)
                {
                    var lookupInitResultVarName = SyntaxFactory.IdentifierName("lookupInitResult");
                    this.additionalCtorStatements.AddRange(new StatementSyntax[] {
                        // var lookupInitResult = ImmutableObjectGraph.RecursiveTypeExtensions.LookupTable<TRecursiveType, TRecursiveParent>.Initialize(this, lookupTable);
                        SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(
                                                                    varType,
                                                                    SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(lookupInitResultVarName.Identifier)
                                                                                                         .WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                              SyntaxFactory.InvocationExpression(
                                                                                                                                  GetLookupTableHelperMember(nameof(LookupTableHelper.Initialize)),
                                                                                                                                  SyntaxFactory.ArgumentList(Syntax.JoinSyntaxNodes(
                                                                                                                                                                 SyntaxKind.CommaToken,
                                                                                                                                                                 SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                                                                                                                                                 SyntaxFactory.Argument(LookupTableFieldName))))))))),
                        // this.inefficiencyLoad = lookupInitResult.InefficiencyLoad;
                        SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(
                                                              SyntaxKind.SimpleAssignmentExpression,
                                                              Syntax.ThisDot(InefficiencyLoadFieldName),
                                                              SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, lookupInitResultVarName, SyntaxFactory.IdentifierName(nameof(LookupTableHelper.InitializeLookupResult.InefficiencyLoad))))),
                        // this.lookupTable = lookupInitResult.LookupTable;
                        SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(
                                                              SyntaxKind.SimpleAssignmentExpression,
                                                              Syntax.ThisDot(LookupTableFieldName),
                                                              SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, lookupInitResultVarName, SyntaxFactory.IdentifierName(nameof(LookupTableHelper.InitializeLookupResult.LookupTable)))))
                    });

                    this.innerMembers.Add(SyntaxFactory.PropertyDeclaration(
                                              this.lookupTableType,
                                              LookupTablePropertyName.Identifier)
                                          .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword))
                                          .AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration(
                                                                        SyntaxKind.GetAccessorDeclaration,
                                                                        SyntaxFactory.Block(
                                                                            SyntaxFactory.IfStatement(
                                                                                SyntaxFactory.BinaryExpression(
                                                                                    SyntaxKind.EqualsExpression,
                                                                                    Syntax.ThisDot(LookupTableFieldName),
                                                                                    GetLookupTableHelperMember(nameof(LookupTableHelper.LazySentinel))),
                                                                                SyntaxFactory.Block(
                                                                                    SyntaxFactory.ExpressionStatement(
                                                                                        SyntaxFactory.AssignmentExpression(
                                                                                            SyntaxKind.SimpleAssignmentExpression,
                                                                                            Syntax.ThisDot(LookupTableFieldName),
                                                                                            SyntaxFactory.InvocationExpression(
                                                                                                GetLookupTableHelperMember(nameof(LookupTableHelper.CreateLookupTable)),
                                                                                                SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(
                                                                                                                               SyntaxFactory.Argument(SyntaxFactory.ThisExpression())))))))),
                                                                            SyntaxFactory.ReturnStatement(Syntax.ThisDot(LookupTableFieldName))))));

                    // protected System.Collections.Immutable.ImmutableDictionary<System.UInt32, KeyValuePair<FileSystemEntry, System.UInt32>> lookupTable;
                    this.innerMembers.Add(SyntaxFactory.FieldDeclaration(
                                              SyntaxFactory.VariableDeclaration(this.lookupTableType)
                                              .AddVariables(SyntaxFactory.VariableDeclarator(LookupTableFieldName.Identifier)))
                                          .AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword))
                                          .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                    // public System.Collections.Immutable.ImmutableStack<TRecursiveType> GetSpine(TRecursiveType descendent) {
                    //  return ImmutableObjectGraph.RecursiveTypeExtensions.GetSpine<TRecursiveParent, TRecursiveType>(this, descendent);
                    // }
                    var descendentParameter = SyntaxFactory.IdentifierName("descendent");
                    this.innerMembers.Add(
                        SyntaxFactory.MethodDeclaration(Syntax.ImmutableStackOf(this.applyTo.RecursiveType.TypeSyntax), GetSpineMethodName.Identifier)
                        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                        .AddAttributeLists(PureAttributeList)
                        .AddParameterListParameters(SyntaxFactory.Parameter(descendentParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
                        .WithBody(SyntaxFactory.Block(
                                      SyntaxFactory.ReturnStatement(
                                          SyntaxFactory.InvocationExpression(
                                              SyntaxFactory.MemberAccessExpression(
                                                  SyntaxKind.SimpleMemberAccessExpression,
                                                  Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                                  SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetSpine))
                                                  .AddTypeArgumentListArguments(this.applyTo.RecursiveParent.TypeSyntax, this.applyTo.RecursiveType.TypeSyntax)))
                                          .AddArgumentListArguments(
                                              SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                              SyntaxFactory.Argument(descendentParameter))))));

                    // public System.Collections.Immutable.ImmutableStack<TRecursiveType> GetSpine(uint identity) {
                    //  return ImmutableObjectGraph.RecursiveTypeExtensions.GetSpine<TRecursiveParent, TRecursiveType>(this, identity);
                    // }
                    var identityParameter = SyntaxFactory.IdentifierName("identity");
                    this.innerMembers.Add(
                        SyntaxFactory.MethodDeclaration(Syntax.ImmutableStackOf(this.applyTo.RecursiveType.TypeSyntax), GetSpineMethodName.Identifier)
                        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                        .AddParameterListParameters(SyntaxFactory.Parameter(identityParameter.Identifier).WithType(IdentityFieldTypeSyntax))
                        .AddAttributeLists(PureAttributeList)
                        .WithBody(SyntaxFactory.Block(
                                      SyntaxFactory.ReturnStatement(
                                          SyntaxFactory.InvocationExpression(
                                              SyntaxFactory.MemberAccessExpression(
                                                  SyntaxKind.SimpleMemberAccessExpression,
                                                  Syntax.GetTypeSyntax(typeof(RecursiveTypeExtensions)),
                                                  SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetSpine))
                                                  .AddTypeArgumentListArguments(this.applyTo.RecursiveParent.TypeSyntax, this.applyTo.RecursiveType.TypeSyntax)))
                                          .AddArgumentListArguments(
                                              SyntaxFactory.Argument(SyntaxFactory.ThisExpression()),
                                              SyntaxFactory.Argument(identityParameter))))));

                    this.innerMembers.Add(this.CreateFindMethod());
                }
            }
Exemplo n.º 11
0
            private void ImplementRecursiveParentInterface()
            {
                var irecursiveParentOfT = CreateIRecursiveParentOfTSyntax(GetFullyQualifiedSymbolName(this.generator.applyToMetaType.RecursiveType.TypeSymbol));

                this.baseTypes.Add(SyntaxFactory.SimpleBaseType(irecursiveParentOfT));

                // this.Children;
                var thisDotChildren = Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name.ToPascalCase()));

                // System.Collections.Generic.IReadOnlyCollection<IRecursiveType> IRecursiveParent.Children
                this.innerMembers.Add(
                    SyntaxFactory.PropertyDeclaration(
                        Syntax.GetTypeSyntax(typeof(IReadOnlyCollection <IRecursiveType>)),
                        nameof(IRecursiveParent.Children))
                    .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
                    .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
                    .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                    .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

                // public ParentedRecursiveType<TRecursiveParent, TRecursiveType> GetParentedNode(uint identity)
                this.innerMembers.Add(
                    SyntaxFactory.MethodDeclaration(
                        SyntaxFactory.GenericName(nameof(ParentedRecursiveType <IRecursiveParent <IRecursiveType>, IRecursiveType>)).AddTypeArgumentListArguments(
                            this.applyTo.RecursiveParent.TypeSyntax,
                            this.applyTo.RecursiveType.TypeSyntax),
                        nameof(IRecursiveParent.GetParentedNode))
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                    //.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
                    .AddParameterListParameters(RequiredIdentityParameter)
                    .WithBody(SyntaxFactory.Block(
                                  // return this.GetParentedNode<TRecursiveParent, TRecursiveType>(identity);
                                  SyntaxFactory.ReturnStatement(
                                      SyntaxFactory.InvocationExpression(
                                          Syntax.ThisDot(SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetParentedNode)).AddTypeArgumentListArguments(
                                                             this.applyTo.RecursiveParent.TypeSyntax,
                                                             this.applyTo.RecursiveType.TypeSyntax)))
                                      .AddArgumentListArguments(SyntaxFactory.Argument(IdentityParameterName))))));

                // ParentedRecursiveType<IRecursiveParent<IRecursiveType>, IRecursiveType> IRecursiveParent.GetParentedNode(<#= templateType.RequiredIdentityField.TypeName #> identity) {
                var parentedVar = SyntaxFactory.IdentifierName("parented");
                var returnType  = Syntax.GetTypeSyntax(typeof(ParentedRecursiveTypeNonGeneric));

                this.innerMembers.Add(
                    SyntaxFactory.MethodDeclaration(
                        returnType,
                        nameof(IRecursiveParent.GetParentedNode))
                    .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
                    .AddParameterListParameters(RequiredIdentityParameter)
                    .WithBody(SyntaxFactory.Block(
                                  // var parented = this.GetParentedNode(identity);
                                  SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
                                                                              SyntaxFactory.VariableDeclarator(parentedVar.Identifier).WithInitializer(SyntaxFactory.EqualsValueClause(
                                                                                                                                                           SyntaxFactory.InvocationExpression(Syntax.ThisDot(SyntaxFactory.IdentifierName(nameof(RecursiveTypeExtensions.GetParentedNode))))
                                                                                                                                                           .AddArgumentListArguments(SyntaxFactory.Argument(IdentityParameterName)))))),
                                  // return new ParentedRecursiveType<IRecursiveParent<IRecursiveType>, IRecursiveType>(parented.Value, parented.Parent);
                                  SyntaxFactory.ReturnStatement(SyntaxFactory.ObjectCreationExpression(returnType).AddArgumentListArguments(
                                                                    SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Value)))),
                                                                    SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Parent)))))))));

                ////System.Collections.Generic.IReadOnlyCollection<<#= templateType.RecursiveType.TypeName #>> IRecursiveParent<<#= templateType.RecursiveType.TypeName #>>.Children
                ////	=> return this.Children;
                this.innerMembers.Add(
                    SyntaxFactory.PropertyDeclaration(
                        Syntax.IReadOnlyCollectionOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
                        nameof(IRecursiveParent <IRecursiveType> .Children))
                    .WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(irecursiveParentOfT))
                    .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
                    .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                    .AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));
            }