1 //===- Synthesis.cpp ------------------------------------------*- C++ -*-=====// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #include "clang/Basic/TokenKinds.h" 9 #include "clang/Tooling/Syntax/BuildTree.h" 10 #include "clang/Tooling/Syntax/Tree.h" 11 12 using namespace clang; 13 14 /// Exposes private syntax tree APIs required to implement node synthesis. 15 /// Should not be used for anything else. 16 class clang::syntax::FactoryImpl { 17 public: 18 static void setCanModify(syntax::Node *N) { N->CanModify = true; } 19 20 static void prependChildLowLevel(syntax::Tree *T, syntax::Node *Child, 21 syntax::NodeRole R) { 22 T->prependChildLowLevel(Child, R); 23 } 24 25 static std::pair<FileID, ArrayRef<Token>> 26 lexBuffer(syntax::Arena &A, std::unique_ptr<llvm::MemoryBuffer> Buffer) { 27 return A.lexBuffer(std::move(Buffer)); 28 } 29 }; 30 31 syntax::Leaf *clang::syntax::createLeaf(syntax::Arena &A, tok::TokenKind K, 32 StringRef Spelling) { 33 auto Tokens = 34 FactoryImpl::lexBuffer(A, llvm::MemoryBuffer::getMemBuffer(Spelling)) 35 .second; 36 assert(Tokens.size() == 1); 37 assert(Tokens.front().kind() == K && 38 "spelling is not lexed into the expected kind of token"); 39 40 auto *Leaf = new (A.getAllocator()) syntax::Leaf(Tokens.begin()); 41 syntax::FactoryImpl::setCanModify(Leaf); 42 Leaf->assertInvariants(); 43 return Leaf; 44 } 45 46 syntax::Leaf *clang::syntax::createLeaf(syntax::Arena &A, tok::TokenKind K) { 47 const auto *Spelling = tok::getPunctuatorSpelling(K); 48 if (!Spelling) 49 Spelling = tok::getKeywordSpelling(K); 50 assert(Spelling && 51 "Cannot infer the spelling of the token from its token kind."); 52 return createLeaf(A, K, Spelling); 53 } 54 55 namespace { 56 // Allocates the concrete syntax `Tree` according to its `NodeKind`. 57 syntax::Tree *allocateTree(syntax::Arena &A, syntax::NodeKind Kind) { 58 switch (Kind) { 59 case syntax::NodeKind::Leaf: 60 assert(false); 61 break; 62 case syntax::NodeKind::TranslationUnit: 63 return new (A.getAllocator()) syntax::TranslationUnit; 64 case syntax::NodeKind::UnknownExpression: 65 return new (A.getAllocator()) syntax::UnknownExpression; 66 case syntax::NodeKind::ParenExpression: 67 return new (A.getAllocator()) syntax::ParenExpression; 68 case syntax::NodeKind::ThisExpression: 69 return new (A.getAllocator()) syntax::ThisExpression; 70 case syntax::NodeKind::IntegerLiteralExpression: 71 return new (A.getAllocator()) syntax::IntegerLiteralExpression; 72 case syntax::NodeKind::CharacterLiteralExpression: 73 return new (A.getAllocator()) syntax::CharacterLiteralExpression; 74 case syntax::NodeKind::FloatingLiteralExpression: 75 return new (A.getAllocator()) syntax::FloatingLiteralExpression; 76 case syntax::NodeKind::StringLiteralExpression: 77 return new (A.getAllocator()) syntax::StringLiteralExpression; 78 case syntax::NodeKind::BoolLiteralExpression: 79 return new (A.getAllocator()) syntax::BoolLiteralExpression; 80 case syntax::NodeKind::CxxNullPtrExpression: 81 return new (A.getAllocator()) syntax::CxxNullPtrExpression; 82 case syntax::NodeKind::IntegerUserDefinedLiteralExpression: 83 return new (A.getAllocator()) syntax::IntegerUserDefinedLiteralExpression; 84 case syntax::NodeKind::FloatUserDefinedLiteralExpression: 85 return new (A.getAllocator()) syntax::FloatUserDefinedLiteralExpression; 86 case syntax::NodeKind::CharUserDefinedLiteralExpression: 87 return new (A.getAllocator()) syntax::CharUserDefinedLiteralExpression; 88 case syntax::NodeKind::StringUserDefinedLiteralExpression: 89 return new (A.getAllocator()) syntax::StringUserDefinedLiteralExpression; 90 case syntax::NodeKind::PrefixUnaryOperatorExpression: 91 return new (A.getAllocator()) syntax::PrefixUnaryOperatorExpression; 92 case syntax::NodeKind::PostfixUnaryOperatorExpression: 93 return new (A.getAllocator()) syntax::PostfixUnaryOperatorExpression; 94 case syntax::NodeKind::BinaryOperatorExpression: 95 return new (A.getAllocator()) syntax::BinaryOperatorExpression; 96 case syntax::NodeKind::UnqualifiedId: 97 return new (A.getAllocator()) syntax::UnqualifiedId; 98 case syntax::NodeKind::IdExpression: 99 return new (A.getAllocator()) syntax::IdExpression; 100 case syntax::NodeKind::CallExpression: 101 return new (A.getAllocator()) syntax::CallExpression; 102 case syntax::NodeKind::UnknownStatement: 103 return new (A.getAllocator()) syntax::UnknownStatement; 104 case syntax::NodeKind::DeclarationStatement: 105 return new (A.getAllocator()) syntax::DeclarationStatement; 106 case syntax::NodeKind::EmptyStatement: 107 return new (A.getAllocator()) syntax::EmptyStatement; 108 case syntax::NodeKind::SwitchStatement: 109 return new (A.getAllocator()) syntax::SwitchStatement; 110 case syntax::NodeKind::CaseStatement: 111 return new (A.getAllocator()) syntax::CaseStatement; 112 case syntax::NodeKind::DefaultStatement: 113 return new (A.getAllocator()) syntax::DefaultStatement; 114 case syntax::NodeKind::IfStatement: 115 return new (A.getAllocator()) syntax::IfStatement; 116 case syntax::NodeKind::ForStatement: 117 return new (A.getAllocator()) syntax::ForStatement; 118 case syntax::NodeKind::WhileStatement: 119 return new (A.getAllocator()) syntax::WhileStatement; 120 case syntax::NodeKind::ContinueStatement: 121 return new (A.getAllocator()) syntax::ContinueStatement; 122 case syntax::NodeKind::BreakStatement: 123 return new (A.getAllocator()) syntax::BreakStatement; 124 case syntax::NodeKind::ReturnStatement: 125 return new (A.getAllocator()) syntax::ReturnStatement; 126 case syntax::NodeKind::RangeBasedForStatement: 127 return new (A.getAllocator()) syntax::RangeBasedForStatement; 128 case syntax::NodeKind::ExpressionStatement: 129 return new (A.getAllocator()) syntax::ExpressionStatement; 130 case syntax::NodeKind::CompoundStatement: 131 return new (A.getAllocator()) syntax::CompoundStatement; 132 case syntax::NodeKind::UnknownDeclaration: 133 return new (A.getAllocator()) syntax::UnknownDeclaration; 134 case syntax::NodeKind::EmptyDeclaration: 135 return new (A.getAllocator()) syntax::EmptyDeclaration; 136 case syntax::NodeKind::StaticAssertDeclaration: 137 return new (A.getAllocator()) syntax::StaticAssertDeclaration; 138 case syntax::NodeKind::LinkageSpecificationDeclaration: 139 return new (A.getAllocator()) syntax::LinkageSpecificationDeclaration; 140 case syntax::NodeKind::SimpleDeclaration: 141 return new (A.getAllocator()) syntax::SimpleDeclaration; 142 case syntax::NodeKind::TemplateDeclaration: 143 return new (A.getAllocator()) syntax::TemplateDeclaration; 144 case syntax::NodeKind::ExplicitTemplateInstantiation: 145 return new (A.getAllocator()) syntax::ExplicitTemplateInstantiation; 146 case syntax::NodeKind::NamespaceDefinition: 147 return new (A.getAllocator()) syntax::NamespaceDefinition; 148 case syntax::NodeKind::NamespaceAliasDefinition: 149 return new (A.getAllocator()) syntax::NamespaceAliasDefinition; 150 case syntax::NodeKind::UsingNamespaceDirective: 151 return new (A.getAllocator()) syntax::UsingNamespaceDirective; 152 case syntax::NodeKind::UsingDeclaration: 153 return new (A.getAllocator()) syntax::UsingDeclaration; 154 case syntax::NodeKind::TypeAliasDeclaration: 155 return new (A.getAllocator()) syntax::TypeAliasDeclaration; 156 case syntax::NodeKind::SimpleDeclarator: 157 return new (A.getAllocator()) syntax::SimpleDeclarator; 158 case syntax::NodeKind::ParenDeclarator: 159 return new (A.getAllocator()) syntax::ParenDeclarator; 160 case syntax::NodeKind::ArraySubscript: 161 return new (A.getAllocator()) syntax::ArraySubscript; 162 case syntax::NodeKind::TrailingReturnType: 163 return new (A.getAllocator()) syntax::TrailingReturnType; 164 case syntax::NodeKind::ParametersAndQualifiers: 165 return new (A.getAllocator()) syntax::ParametersAndQualifiers; 166 case syntax::NodeKind::MemberPointer: 167 return new (A.getAllocator()) syntax::MemberPointer; 168 case syntax::NodeKind::GlobalNameSpecifier: 169 return new (A.getAllocator()) syntax::GlobalNameSpecifier; 170 case syntax::NodeKind::DecltypeNameSpecifier: 171 return new (A.getAllocator()) syntax::DecltypeNameSpecifier; 172 case syntax::NodeKind::IdentifierNameSpecifier: 173 return new (A.getAllocator()) syntax::IdentifierNameSpecifier; 174 case syntax::NodeKind::SimpleTemplateNameSpecifier: 175 return new (A.getAllocator()) syntax::SimpleTemplateNameSpecifier; 176 case syntax::NodeKind::NestedNameSpecifier: 177 return new (A.getAllocator()) syntax::NestedNameSpecifier; 178 case syntax::NodeKind::MemberExpression: 179 return new (A.getAllocator()) syntax::MemberExpression; 180 case syntax::NodeKind::CallArguments: 181 return new (A.getAllocator()) syntax::CallArguments; 182 case syntax::NodeKind::ParameterDeclarationList: 183 return new (A.getAllocator()) syntax::ParameterDeclarationList; 184 } 185 llvm_unreachable("unknown node kind"); 186 } 187 } // namespace 188 189 syntax::Tree *clang::syntax::createTree( 190 syntax::Arena &A, 191 std::vector<std::pair<syntax::Node *, syntax::NodeRole>> Children, 192 syntax::NodeKind K) { 193 auto *T = allocateTree(A, K); 194 FactoryImpl::setCanModify(T); 195 for (auto ChildIt = Children.rbegin(); ChildIt != Children.rend(); 196 std::advance(ChildIt, 1)) 197 FactoryImpl::prependChildLowLevel(T, ChildIt->first, ChildIt->second); 198 199 T->assertInvariants(); 200 return T; 201 } 202 203 syntax::EmptyStatement *clang::syntax::createEmptyStatement(syntax::Arena &A) { 204 return cast<EmptyStatement>( 205 createTree(A, {{createLeaf(A, tok::semi), NodeRole::Unknown}}, 206 NodeKind::EmptyStatement)); 207 } 208