private IEnumerable <TypeDeclarationSyntax> GetTopLevelType(CSharpSyntaxNode node)
        {
            var typeDecls  = node.ChildNodes().OfType <TypeDeclarationSyntax>();
            var namespaces = node.ChildNodes().OfType <NamespaceDeclarationSyntax>();

            return(typeDecls.Concat(namespaces.SelectMany(GetTopLevelType)));
        }
Exemple #2
0
        public static HybType GetReturnType(TypeResolver resolver, CSharpSyntaxNode node)
        {
            var candidates = new List <HybType>();

            // Collect
            foreach (var child in node.ChildNodes()
                     .OfType <ReturnStatementSyntax>())
            {
                var type = GetType(resolver, child.Expression);
                if (type != null)
                {
                    candidates.Add(type);
                }
            }

            // Deduct
            if (candidates.Count == 0)
            {
                return(HybTypeCache.Void);
            }

            var finalCandidate = candidates[0];

            foreach (var candidate in candidates)
            {
                if (candidate.IsCompiledType == false)
                {
                    finalCandidate = HybTypeCache.Object;
                }
            }

            return(finalCandidate);
        }
Exemple #3
0
        /// <remarks>These are tidied up so we can add as many GlobalImports as we want when building compilations</remarks>
        private CSharpSyntaxNode TidyUsings(CSharpSyntaxNode root)
        {
            var diagnostics  = _semanticModel.GetDiagnostics().ToList();
            var unusedUsings = diagnostics
                               .Where(d => d.Id == UnusedUsingDiagnosticId)
                               .Select(d => root.FindNode(d.Location.SourceSpan))
                               .OfType <UsingDirectiveSyntax>()
                               .ToList();

            var nodesWithUnresolvedTypes = diagnostics
                                           .Where(d => d.Id == UnresolvedTypeOrNamespaceDiagnosticId && d.Location.IsInSource)
                                           .Select(d => root.FindNode(d.Location.SourceSpan))
                                           .ToLookup(d => d.GetAncestor <UsingDirectiveSyntax>());

            unusedUsings = unusedUsings.Except(nodesWithUnresolvedTypes.Select(g => g.Key)).ToList();

            if (nodesWithUnresolvedTypes[null].Any() || !unusedUsings.Any())
            {
                return(root);
            }

            root = root.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepNoTrivia);
            var firstChild = root.ChildNodes().FirstOrDefault();

            if (firstChild != null)
            {
                root = root.ReplaceNode(firstChild,
                                        firstChild.WithLeadingTrivia(firstChild.GetLeadingTrivia()
                                                                     .SkipWhile(t => t.IsWhitespaceTrivia())));
            }

            return(root);
        }
        public IEnumerable <SyntaxNodeOrToken> QueryAll(CSharpSyntaxNode current, SyntaxQuery query)
        {
            if (current is CompilationUnitSyntax)
            {
                return(current.ChildNodes().SelectMany(c => QueryAll((CSharpSyntaxNode)c, query)));
            }

            switch (query.Axis)
            {
            case SyntaxQueryAxis.Self:
                if (MatchesIgnoringAxis(current, query))
                {
                    return(Enumerable.Repeat((SyntaxNodeOrToken)current, 1));
                }
                return(Enumerable.Empty <SyntaxNodeOrToken>());

            case SyntaxQueryAxis.Child:
                return(QueryAllChildrenOrDescendants(current, query, descendants: false));

            case SyntaxQueryAxis.Descendant:
                return(QueryAllChildrenOrDescendants(current, query, descendants: true));

            case SyntaxQueryAxis.Parent:
                if (MatchesIgnoringAxis(current.Parent, query))
                {
                    return(Enumerable.Repeat((SyntaxNodeOrToken)current.Parent, 1));
                }
                return(Enumerable.Empty <SyntaxNodeOrToken>());

            default:
                throw new ArgumentException($"Unsupported query axis: {query.Axis}.", nameof(query));
            }
        }
Exemple #5
0
 public override string GetCode(CSharpSyntaxNode Node)
 {
     if (Node.Kind() == SyntaxKind.MethodDeclaration)
     {
         string name = Node.ChildTokens().ToList()[Node.ChildTokens().Count() - 1].ToString();
         FullName += "." + name + ".";
         FullName  = string.Join(".", FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries));
         if (name == "Main")
         {
             MainPath = FullName;
         }
         string[] arr = Node.ChildTokens().ToList().Select(t => t.ToString()).ToArray();
         if (Node.HasChildKind(SyntaxKind.PredefinedType))
         {
             string T = Node.Kind2Child(SyntaxKind.PredefinedType).ChildTokens().ToList()[0].ToString();
             return((arr.Contains("public") ? "public: " : "") +
                    (arr.Contains("private") ? "private: " : "") +
                    (arr.Contains("static") ? "static " : "") + T + " " + name + Environment.NewLine);
         }
     }
     foreach (CSharpSyntaxNode node in Node.ChildNodes())
     {
         string code = GetCode(node);
         if (code != null)
         {
             return(code);
         }
     }
     return(null);
 }
Exemple #6
0
        private static void AST2CPP(CSharpSyntaxNode ROOT, ref string CPP_SRC)
        {
            CodeBlock b = null;

            if (debug)
            {
                for (int j = 0; j <= level; j++)
                {
                    CPP_SRC += "\t";
                }
                CPP_SRC += ROOT.Kind().ToString() + " = [" + string.Join(", ", ROOT.ChildTokens().ToList()) + "]" + Environment.NewLine;
            }
            else
            {
                List <CodeBlock> codeBlocks = new List <CodeBlock>();
                foreach (Type type in Assembly.GetExecutingAssembly().
                         GetTypes().Where(t => t != typeof(CodeBlock) && t.IsSubclassOf(typeof(CodeBlock))))
                {
                    codeBlocks.Add((CodeBlock)Activator.CreateInstance(type, false));
                }
                foreach (CodeBlock codeBlock in codeBlocks)
                {
                    if (codeBlock.IsMatch(ROOT))
                    {
                        CPP_SRC += codeBlock.GetCode(ROOT);
                        b        = codeBlock;
                    }
                }
            }
            if (!debug && b != null)
            {
                CPP_SRC += b.GetBody().Split(new string[] { "%BODY%" }, StringSplitOptions.None)[0];
            }
            level++;
            foreach (CSharpSyntaxNode node in ROOT.ChildNodes())
            {
                AST2CPP(node, ref CPP_SRC);
            }
            level--;
            if (!debug && b != null)
            {
                CPP_SRC += b.GetBody().Split(new string[] { "%BODY%" }, StringSplitOptions.None)[1];
            }
            if (b != null && b.GetBody().ToCharArray().Contains('{') && b.GetBody().ToCharArray().Contains('}'))
            {
                List <string> lst = FullName.Split('.').ToList();
                if (lst.Any())
                {
                    lst.RemoveAt(lst.Count - 1);
                }
                if (!debug)
                {
                    FullName = string.Join(".", lst);
                }
            }
        }
Exemple #7
0
        void VisitNode(CSharpSyntaxNode node)
        {
            if (node == null)
                return;

            node.Accept(Visitor);

            var children = node.ChildNodes();
            if (children != null)
            {
                foreach (var child in children)
                    VisitNode(child as CSharpSyntaxNode);
            }
        }
Exemple #8
0
        void VisitNode(CSharpSyntaxNode node)
        {
            if (node == null)
            {
                return;
            }

            node.Accept(Visitor);

            var children = node.ChildNodes();

            if (children != null)
            {
                foreach (var child in children)
                {
                    VisitNode(child as CSharpSyntaxNode);
                }
            }
        }
Exemple #9
0
 public override string GetCode(CSharpSyntaxNode Node)
 {
     if (Node.Kind() == SyntaxKind.ClassDeclaration)
     {
         string name = Node.ChildTokens().ToList()[Node.ChildTokens().Count() - 3].ToString();
         FullName += "." + name + ".";
         FullName  = string.Join(".", FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries));
         string[] arr = Node.ChildTokens().ToList().Select(t => t.ToString()).ToArray();
         return((arr.Contains("static") ? "static " : "") + "class " + name + Environment.NewLine);
     }
     foreach (CSharpSyntaxNode node in Node.ChildNodes())
     {
         string code = GetCode(node);
         if (code != null)
         {
             return(code);
         }
     }
     return(null);
 }
Exemple #10
0
 public override string GetCode(CSharpSyntaxNode Node)
 {
     if (Node.Kind() == SyntaxKind.StringLiteralExpression)
     {
         foreach (SyntaxToken token in Node.ChildTokens())
         {
             if (token.ToString().StartsWith("\"#"))
             {
                 return(token.ToString().Substring(1, token.ToString().Length - 2) + Environment.NewLine);
             }
         }
     }
     foreach (CSharpSyntaxNode node in Node.ChildNodes())
     {
         string code = GetCode(node);
         if (code != null)
         {
             return(code);
         }
     }
     return(null);
 }
Exemple #11
0
        private void _ConvertChildNodes(CSharpSyntaxNode syntaxNode)
        {
            foreach (var childNode in syntaxNode.ChildNodes())
            {
                CSharpSyntaxNode node = (CSharpSyntaxNode)childNode;

                if (node.Parent.Kind() == SyntaxKind.Block && node.Parent != _blockSyntax)
                {
                    continue;
                }
                else
                {
                    NodeBase convertedNode = SyntaxNodeConverter.ConvertSyntaxNode(node, _model);

                    if (convertedNode != null)
                    {
                        _childNodes.Add(convertedNode);
                    }

                    _ConvertChildNodes(node);
                }
            }
        }
Exemple #12
0
 public override string GetCode(CSharpSyntaxNode Node)
 {
     if (Node.Kind() == SyntaxKind.NamespaceDeclaration)
     {
         if (Node.HasChildKind(SyntaxKind.IdentifierName))
         {
             FullName += "." + Node.Kind2Child(SyntaxKind.IdentifierName).
                         ChildTokens().ToList()[0].ToString() + ".";
             FullName = string.Join(".", FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries));
             return("namespace " +
                    Node.Kind2Child(SyntaxKind.IdentifierName).
                    ChildTokens().ToList()[0].ToString() + Environment.NewLine);
         }
     }
     foreach (CSharpSyntaxNode node in Node.ChildNodes())
     {
         string code = GetCode(node);
         if (code != null)
         {
             return(code);
         }
     }
     return(null);
 }