예제 #1
0
        private static void FunctionType(SyntaxNode node, SemanticModel model, Scope scope)
        {
            TypeSyntax newType = null;

            if (node is GenericNameSyntax)
            {
                var generic = node as GenericNameSyntax;
                if (generic.Identifier.ToString() == "function")
                {
                    List <TypeSyntax> arguments  = new List <TypeSyntax>();
                    TypeSyntax        returnType = null;

                    bool first = true;
                    foreach (var arg in generic.TypeArgumentList.Arguments)
                    {
                        if (first)
                        {
                            first = false;
                            if (arg.ToString() != "void")
                            {
                                returnType = arg;
                            }
                        }
                        else
                        {
                            arguments.Add(arg);
                        }
                    }

                    if (returnType == null)
                    {
                        newType = generic
                                  .WithIdentifier(CSharp.Identifier("Action"))
                                  .WithTypeArgumentList(CSharp.TypeArgumentList(CSharp.SeparatedList(
                                                                                    arguments)));
                    }
                    else
                    {
                        newType = generic
                                  .WithIdentifier(CSharp.Identifier("Func"))
                                  .WithTypeArgumentList(CSharp.TypeArgumentList(CSharp.SeparatedList(
                                                                                    arguments
                                                                                    .Union(new[] { returnType }))));
                    }
                }
            }
            else if (node.ToString() == "function")
            {
                newType = CSharp.ParseTypeName("Action");
            }

            if (newType != null)
            {
                var document = scope.GetDocument <SyntaxToken, SyntaxNode, SemanticModel>();
                document.change(node, RoslynCompiler.ReplaceNode(newType));
            }
        }
예제 #2
0
파일: TypeDef.cs 프로젝트: zihotki/Excess
        private static void FixMissingType(SyntaxNode node, Scope scope)
        {
            var type = node
                       .Ancestors()
                       .OfType <TypeDeclarationSyntax>()
                       .FirstOrDefault();

            if (type != null)
            {
                var typeScope = scope.GetScope <SyntaxToken, SyntaxNode, SemanticModel>(type);
                if (typeScope != null)
                {
                    SyntaxNode realType = typeScope.get <SyntaxNode>("__tdef" + node.ToString());
                    if (realType != null)
                    {
                        realType = RoslynCompiler.Mark(realType); //make sure not to duplicate nodes

                        var document = scope.GetDocument <SyntaxToken, SyntaxNode, SemanticModel>();
                        document.change(node, RoslynCompiler.ReplaceNode(realType));
                    }
                }
            }
        }
예제 #3
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);
        }