static PropertyDeclarationSyntax CreateProperty(CoreDescriptor.Entry entry) { var propertyType = entry is CoreDescriptor.CollectionEntry collectionEntry ? QualifiedName(collectionEntry.CollectionTypeParameter, IdentifierName(Names.FastSerializationEnumerable)) : entry is CoreDescriptor.ComplexEntry complexEntry ? QualifiedName(complexEntry.Type, IdentifierName(Names.FastSerializationProxy)) : entry.Type; return (PropertyDeclaration(propertyType, entry.Identifier) .AddAttributeLists(entry.AttributeLists) .AddModifiers(SyntaxKind.PublicKeyword) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithExpressionBodyFull( IdentifierName(PropertyName) .MemberAccess(entry.IdentifierName) .MutateIf( entry.IsComplex, x => x.MemberAccess( IdentifierName(Names.ToSerializationProxy)) .InvokeWithArguments())), AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithExpressionBodyFull( ThrowExpression( ObjectCreationExpression( ParseTypeName(Names.NotSupportedExceptionFull)) .WithArgumentList( ArgumentList()))))); }
static StatementSyntax CreateAssignment(CoreDescriptor.Entry entry) { return (ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, entry.CamelCaseIdentifierName, entry.IdentifierName))); }
static ArgumentSyntax CreateCollectionArgument(CoreDescriptor.Entry entry) { return (Argument( entry.IdentifierName .MemberAccess( IdentifierName(Names.ToImmutableRecursive)) .InvokeWithArguments())); }
static ArgumentSyntax CreateComplexArgument(CoreDescriptor.Entry entry) { return (Argument( entry.IdentifierName .ConditionalMemberAccess( IdentifierName(Names.ToImmutable)) .InvokeWithArguments())); }
static StatementSyntax CreateCtorAssignment(CoreDescriptor.Entry entry) { return (ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ThisExpression() .MemberAccess(entry.IdentifierName), entry.CamelCaseIdentifierName))); }
static StatementSyntax CreateNotSimpleInitialization(CoreDescriptor.Entry entry) { return (CorePropertyIdentifierName .MemberAccess(entry.IdentifierName) .MemberAccess( IdentifierName(entry.IsCollection ? Names.ToListNode : Names.ToNode)) .InvokeWithArguments( ThisExpression()) .AssignTo(entry.IdentifierName) .AsStatement()); }
static StatementSyntax CreateLocalDeclaration(CoreDescriptor.Entry entry, ExpressionSyntax initializerExpression) { return (LocalDeclarationStatement( VariableDeclaration( IdentifierName("var")) .AddVariables( VariableDeclarator( entry.CamelCaseIdentifier) .WithInitializer( EqualsValueClause(initializerExpression))))); }
static ParameterSyntax CreateParameter(CoreDescriptor.Entry entry) { var type = entry is CoreDescriptor.CollectionEntry collectionEntry ? collectionEntry.GetListNodeTypeIdentifierName() : entry is CoreDescriptor.ComplexEntry complexEntry ? complexEntry.GetNodeTypeIdentifierName() : entry.Type; return (Parameter(entry.CamelCaseIdentifier) .WithType(type) .AddModifiers(SyntaxKind.OutKeyword)); }
static StatementSyntax CreateChildVisitStatement(CoreDescriptor.Entry entry) { var targetType = entry is CoreDescriptor.CollectionEntry collectionEntry ? collectionEntry.GetListNodeTypeIdentifierName() : ((CoreDescriptor.ComplexEntry)entry).GetNodeTypeIdentifierName(); return (CreateLocalDeclaration( entry, IdentifierName(Names.Visit) .InvokeWithArguments( IdentifierName(Node) .MemberAccess(entry.IdentifierName)) .Cast(targetType))); }
static PropertyDeclarationSyntax CreateSpecifiedProperty(CoreDescriptor.Entry entry) { return (PropertyDeclaration( PredefinedType( Token(SyntaxKind.BoolKeyword)), Identifier(entry.Identifier.Text + Names.SpecifiedSuffix)) .AddAttributeLists( AttributeList( SingletonSeparatedList( Attribute( ParseName(Names.XmlIgnoreQualified))))) .AddModifiers(SyntaxKind.PublicKeyword) .WithExpressionBodyFull( BinaryExpression( SyntaxKind.NotEqualsExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, entry.IdentifierName, IdentifierName(Names.Count)), LiteralExpression( SyntaxKind.NumericLiteralExpression, Literal(0))))); }
private IEnumerable <MemberDeclarationSyntax> GetPropertyMembers(CoreDescriptor.Entry entry) { return(entry is CoreDescriptor.CollectionEntry collectionEntry ? CreateArrayProperty() : entry is CoreDescriptor.ComplexEntry complexEntry ? CreateComplexProperty() : CreateSimpleProperty()); IEnumerable <PropertyDeclarationSyntax> CreateSimpleProperty() { yield return (PropertyDeclaration(entry.Type, entry.Identifier) .AddAttributeLists(entry.AttributeLists) .AddModifiers(SyntaxKind.PublicKeyword) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonTokenDefault(), AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonTokenDefault())); } IEnumerable <PropertyDeclarationSyntax> CreateComplexProperty() { yield return (PropertyDeclaration( complexEntry.Type.ToNestedBuilderType(), entry.Identifier) .AddAttributeLists(entry.AttributeLists) .AddModifiers(SyntaxKind.PublicKeyword) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonTokenDefault(), AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonTokenDefault())); } IEnumerable <MemberDeclarationSyntax> CreateArrayProperty() { var propertyName = collectionEntry.Identifier.ValueText; var fieldName = $"_{char.ToLowerInvariant(propertyName[0])}{propertyName.Substring(1)}"; yield return (FieldDeclaration( VariableDeclaration( collectionEntry.ToListOfBuilderType()) .AddVariables( VariableDeclarator(fieldName))) .AddModifiers(SyntaxKind.PrivateKeyword)); yield return (PropertyDeclaration( collectionEntry.ToListOfBuilderType(), collectionEntry.Identifier) .AddAttributeLists(entry.AttributeLists) .AddModifiers(SyntaxKind.PublicKeyword) .AddAccessorListAccessors( CreateGetter(), CreateSetter())); AccessorDeclarationSyntax CreateGetter() { return (AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithExpressionBodyFull( BinaryExpression( SyntaxKind.CoalesceExpression, IdentifierName(fieldName), ParenthesizedExpression( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(fieldName), ObjectCreationExpression( collectionEntry.ToListOfBuilderType()) .AddArgumentListArguments()))))); } AccessorDeclarationSyntax CreateSetter() { return (AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithExpressionBodyFull( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(fieldName), IdentifierName("value")))); } } }