Ejemplo n.º 1
0
        private ConstructorDeclarationSyntax GenerateConstructor()
        {
            const string CoreLocal   = "core";
            const string ParentLocal = "parent";

            return
                (ConstructorDeclaration(
                     Descriptor.GetNodeTypeName())
                 .MutateIf(IsAbstract, x => x.AddModifiers(SyntaxKind.ProtectedKeyword))
                 .AddModifiers(SyntaxKind.InternalKeyword)
                 .AddParameterListParameters(
                     Parameter(
                         Identifier(CoreLocal))
                     .WithType(Descriptor.CoreType),
                     Parameter(
                         Identifier(ParentLocal))
                     .WithType(
                         IdentifierName(Names.SourceNode)))
                 .WithInitializer(
                     ConstructorInitializer(SyntaxKind.BaseConstructorInitializer)
                     .AddArgumentListArguments(
                         GetBaseArguments()))
                 .AddBodyStatements(
                     GetBodyStatements()));

            IEnumerable <ArgumentSyntax> GetBaseArguments()
            {
                if (IsDerived)
                {
                    yield return(Argument(IdentifierName(CoreLocal)));
                }
                yield return(Argument(IdentifierName(ParentLocal)));
            }

            IEnumerable <StatementSyntax> GetBodyStatements()
            {
                yield return
                    (CorePropertyIdentifierName
                     .Assign(
                         IdentifierName(CoreLocal))
                     .AsStatement());

                foreach (var entry in Descriptor.DeclaredEntries.Where(x => !x.IsSimple))
                {
                    yield return(CreateNotSimpleInitialization(entry));
                }
            }