Пример #1
0
        private static SyntaxNode Property(SyntaxNode node, Scope scope)
        {
            var field = node.AncestorsAndSelf()
                        .OfType <MemberDeclarationSyntax>()
                        .FirstOrDefault()
                        as FieldDeclarationSyntax;

            if (field == null)
            {
                //td: error, malformed property
                return(node);
            }

            if (field.Declaration.Variables.Count != 1)
            {
                //td: error, malformed property
                return(node);
            }

            var variable = field
                           .Declaration
                           .Variables[0];

            var initializer = variable.Initializer;
            var type        = field.Declaration.Type;

            if (type == null || type.IsMissing || type.ToString() == "property") //untyped
            {
                if (initializer != null)
                {
                    type = RoslynCompiler.ConstantType(initializer.Value);
                }
            }

            if (type == null)
            {
                type = RoslynCompiler.@object;
            }

            var property = _property
                           .WithIdentifier(variable.Identifier)
                           .WithType(type);

            if (!RoslynCompiler.HasVisibilityModifier(field.Modifiers))
            {
                property = property.AddModifiers(CSharp.Token(SyntaxKind.PublicKeyword));
            }

            var document = scope.GetDocument <SyntaxToken, SyntaxNode, SemanticModel>();

            //schedule the field replacement
            //td: coud be done in this pass with the right info from lexical
            document.change(field, RoslynCompiler.ReplaceNode(property));

            //must be initialized
            if (initializer != null)
            {
                var expr = (AssignmentExpressionSyntax)_assignment.Expression;
                document.change(field.Parent, RoslynCompiler
                                .AddInitializers(_assignment.WithExpression(expr
                                                                            .WithLeft(CSharp.IdentifierName(variable.Identifier))
                                                                            .WithRight(initializer.Value))));
            }

            return(node);
        }