private IEnumerable <SwitchSectionSyntax> BuildSwitchStatements() { //Every type that is a child type in the same assembly foreach (PolymorphicTypeInfo typeInfo in GetPolymorphicChildTypes()) { yield return(SwitchSection() .WithLabels ( SingletonList <SwitchLabelSyntax> ( CaseSwitchLabel ( CreateSwitchCase(typeInfo) ) .WithKeyword ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.CaseKeyword, TriviaList ( Space ) ) ) .WithColonToken ( Token ( TriviaList(), SyntaxKind.ColonToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) ) .WithStatements ( SingletonList <StatementSyntax> ( ReturnStatement ( ObjectCreationExpression ( CreateChildTypeIdentifier(typeInfo.ChildType) ) .WithNewKeyword ( Token ( TriviaList(), SyntaxKind.NewKeyword, TriviaList ( Space ) ) ) .WithArgumentList ( ArgumentList() ) ) .WithReturnKeyword ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.ReturnKeyword, TriviaList ( Space ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) )); } if (TypeSymbol.HasAttributeExact <DefaultChildAttribute>()) { //TODO: We need a way to WARN consumers of the library about this. //We cannot emit a creation for default child if they're not marked as serializable via the WireDataContract //This will produce code that dangerously recurrs into a stack overflow since base WireMessage won't be overrided. ITypeSymbol childTypeSymbol = (ITypeSymbol)TypeSymbol.GetAttributeExact <DefaultChildAttribute>().ConstructorArguments.First().Value; if (childTypeSymbol.HasAttributeLike <WireDataContractAttribute>()) { yield return(SwitchSection() .WithLabels ( SingletonList <SwitchLabelSyntax> ( DefaultSwitchLabel() .WithKeyword ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.DefaultKeyword, TriviaList() ) ) .WithColonToken ( Token ( TriviaList(), SyntaxKind.ColonToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) ) .WithStatements ( SingletonList <StatementSyntax> ( ReturnStatement ( ObjectCreationExpression ( IdentifierName(childTypeSymbol.Name) ) .WithNewKeyword ( Token ( TriviaList(), SyntaxKind.NewKeyword, TriviaList ( Space ) ) ) .WithArgumentList ( ArgumentList() ) ) .WithReturnKeyword ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.ReturnKeyword, TriviaList ( Space ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) )); } else { yield return(EmitNoDefaultChildThrow()); } } else { yield return(EmitNoDefaultChildThrow()); } }
public PolymorphicSerializerImplementationCompilationUnitEmitter([NotNull] INamedTypeSymbol typeSymbol, [NotNull] Compilation compilationUnit) : base(typeSymbol) { CompilationUnit = compilationUnit ?? throw new ArgumentNullException(nameof(compilationUnit)); PolymorphicKeySizeType = InternalEnumExtensions.ParseFull <PrimitiveSizeType>(TypeSymbol.GetAttributeExact <WireDataContractAttribute>().ConstructorArguments.First().ToCSharpString(), true); }