10b57cec5SDimitry Andric //===- BuildTree.h - build syntax trees -----------------------*- C++ -*-=====// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // Functions to construct a syntax tree from an AST. 90b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1004eeddc0SDimitry Andric #ifndef LLVM_CLANG_TOOLING_SYNTAX_BUILDTREE_H 1104eeddc0SDimitry Andric #define LLVM_CLANG_TOOLING_SYNTAX_BUILDTREE_H 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/AST/Decl.h" 14480093f4SDimitry Andric #include "clang/Basic/TokenKinds.h" 150b57cec5SDimitry Andric #include "clang/Tooling/Syntax/Nodes.h" 16*fcaf7f86SDimitry Andric #include "clang/Tooling/Syntax/TokenBufferTokenManager.h" 17480093f4SDimitry Andric #include "clang/Tooling/Syntax/Tree.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace clang { 200b57cec5SDimitry Andric namespace syntax { 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric /// Build a syntax tree for the main file. 23e8d8bef9SDimitry Andric /// This usually covers the whole TranslationUnitDecl, but can be restricted by 24e8d8bef9SDimitry Andric /// the ASTContext's traversal scope. 25*fcaf7f86SDimitry Andric syntax::TranslationUnit * 26*fcaf7f86SDimitry Andric buildSyntaxTree(Arena &A, TokenBufferTokenManager &TBTM, ASTContext &Context); 27480093f4SDimitry Andric 28480093f4SDimitry Andric // Create syntax trees from subtrees not backed by the source code. 29480093f4SDimitry Andric 30e8d8bef9SDimitry Andric // Synthesis of Leafs 31e8d8bef9SDimitry Andric /// Create `Leaf` from token with `Spelling` and assert it has the desired 32e8d8bef9SDimitry Andric /// `TokenKind`. 33*fcaf7f86SDimitry Andric syntax::Leaf *createLeaf(syntax::Arena &A, TokenBufferTokenManager &TBTM, 34*fcaf7f86SDimitry Andric tok::TokenKind K, StringRef Spelling); 35480093f4SDimitry Andric 36e8d8bef9SDimitry Andric /// Infer the token spelling from its `TokenKind`, then create `Leaf` from 37e8d8bef9SDimitry Andric /// this token 38*fcaf7f86SDimitry Andric syntax::Leaf *createLeaf(syntax::Arena &A, TokenBufferTokenManager &TBTM, 39*fcaf7f86SDimitry Andric tok::TokenKind K); 40e8d8bef9SDimitry Andric 41e8d8bef9SDimitry Andric // Synthesis of Trees 42e8d8bef9SDimitry Andric /// Creates the concrete syntax node according to the specified `NodeKind` `K`. 43e8d8bef9SDimitry Andric /// Returns it as a pointer to the base class `Tree`. 44e8d8bef9SDimitry Andric syntax::Tree * 45e8d8bef9SDimitry Andric createTree(syntax::Arena &A, 46e8d8bef9SDimitry Andric ArrayRef<std::pair<syntax::Node *, syntax::NodeRole>> Children, 47e8d8bef9SDimitry Andric syntax::NodeKind K); 48e8d8bef9SDimitry Andric 49e8d8bef9SDimitry Andric // Synthesis of Syntax Nodes 50*fcaf7f86SDimitry Andric syntax::EmptyStatement *createEmptyStatement(syntax::Arena &A, 51*fcaf7f86SDimitry Andric TokenBufferTokenManager &TBTM); 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric /// Creates a completely independent copy of `N` with its macros expanded. 54e8d8bef9SDimitry Andric /// 55e8d8bef9SDimitry Andric /// The copy is: 56e8d8bef9SDimitry Andric /// * Detached, i.e. `Parent == NextSibling == nullptr` and 57e8d8bef9SDimitry Andric /// `Role == Detached`. 58e8d8bef9SDimitry Andric /// * Synthesized, i.e. `Original == false`. 59*fcaf7f86SDimitry Andric syntax::Node *deepCopyExpandingMacros(syntax::Arena &A, 60*fcaf7f86SDimitry Andric TokenBufferTokenManager &TBTM, 61*fcaf7f86SDimitry Andric const syntax::Node *N); 620b57cec5SDimitry Andric } // namespace syntax 630b57cec5SDimitry Andric } // namespace clang 640b57cec5SDimitry Andric #endif 65