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))); 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 { get; } this.innerMembers.Add( SyntaxFactory.PropertyDeclaration(inefficiencyLoadType, nameof(IRecursiveParentWithLookupTable <IRecursiveType> .InefficiencyLoad)) .WithExplicitInterfaceSpecifier(explicitImplementation) .AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block(SyntaxFactory.ReturnStatement(Syntax.ThisDot(InefficiencyLoadFieldName)))))); // IReadOnlyCollection<TRecursiveType> IRecursiveParentWithLookupTable<TRecursiveType>.Children { get; } this.innerMembers.Add( SyntaxFactory.PropertyDeclaration( Syntax.IReadOnlyCollectionOf(this.applyTo.RecursiveType.TypeSyntax), nameof(IRecursiveParentWithLookupTable <IRecursiveType> .Children)) .WithExplicitInterfaceSpecifier(explicitImplementation) .AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block(SyntaxFactory.ReturnStatement(Syntax.ThisDot(this.applyTo.RecursiveField.NameAsProperty)))))); // ImmutableDictionary<IdentityFieldType, KeyValuePair<TRecursiveType, IdentityFieldType>> IRecursiveParentWithLookupTable<TRecursiveType>.LookupTable { get; } this.innerMembers.Add( SyntaxFactory.PropertyDeclaration( this.lookupTableType, nameof(IRecursiveParentWithLookupTable <IRecursiveType> .LookupTable)) .WithExplicitInterfaceSpecifier(explicitImplementation) .AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block(SyntaxFactory.ReturnStatement(Syntax.ThisDot(LookupTablePropertyName)))))); } 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))); // public System.Collections.Immutable.ImmutableStack<TRecursiveType> GetSpine(TRecursiveType descendent) { // return this.GetSpine<TRecursiveParent, TRecursiveType>(descendent); // } var descendentParameter = SyntaxFactory.IdentifierName("descendent"); this.innerMembers.Add( SyntaxFactory.MethodDeclaration(Syntax.ImmutableStackOf(this.applyTo.RecursiveType.TypeSyntax), GetSpineMethodName.Identifier) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) .AddParameterListParameters(SyntaxFactory.Parameter(descendentParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax)) .WithBody(SyntaxFactory.Block( SyntaxFactory.ReturnStatement( SyntaxFactory.InvocationExpression( Syntax.ThisDot( SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetSpine)) .AddTypeArgumentListArguments(this.applyTo.RecursiveParent.TypeSyntax, this.applyTo.RecursiveType.TypeSyntax)), SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Argument(descendentParameter)))))))); // public System.Collections.Immutable.ImmutableStack<TRecursiveType> GetSpine(uint identity) { // return this.GetSpine<TRecursiveParent, TRecursiveType>(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)) .WithBody(SyntaxFactory.Block( SyntaxFactory.ReturnStatement( SyntaxFactory.InvocationExpression( Syntax.ThisDot( SyntaxFactory.GenericName(nameof(RecursiveTypeExtensions.GetSpine)) .AddTypeArgumentListArguments(this.applyTo.RecursiveParent.TypeSyntax, this.applyTo.RecursiveType.TypeSyntax)), SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Argument(identityParameter)))))))); this.innerMembers.Add(this.CreateFindMethod()); } }