1 //===- Nodes.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/Tooling/Syntax/Nodes.h" 9 #include "clang/Basic/TokenKinds.h" 10 11 using namespace clang; 12 13 llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) { 14 switch (K) { 15 case NodeKind::Leaf: 16 return OS << "Leaf"; 17 case NodeKind::TranslationUnit: 18 return OS << "TranslationUnit"; 19 case NodeKind::UnknownExpression: 20 return OS << "UnknownExpression"; 21 case NodeKind::UnknownStatement: 22 return OS << "UnknownStatement"; 23 case NodeKind::DeclarationStatement: 24 return OS << "DeclarationStatement"; 25 case NodeKind::EmptyStatement: 26 return OS << "EmptyStatement"; 27 case NodeKind::SwitchStatement: 28 return OS << "SwitchStatement"; 29 case NodeKind::CaseStatement: 30 return OS << "CaseStatement"; 31 case NodeKind::DefaultStatement: 32 return OS << "DefaultStatement"; 33 case NodeKind::IfStatement: 34 return OS << "IfStatement"; 35 case NodeKind::ForStatement: 36 return OS << "ForStatement"; 37 case NodeKind::WhileStatement: 38 return OS << "WhileStatement"; 39 case NodeKind::ContinueStatement: 40 return OS << "ContinueStatement"; 41 case NodeKind::BreakStatement: 42 return OS << "BreakStatement"; 43 case NodeKind::ReturnStatement: 44 return OS << "ReturnStatement"; 45 case NodeKind::RangeBasedForStatement: 46 return OS << "RangeBasedForStatement"; 47 case NodeKind::ExpressionStatement: 48 return OS << "ExpressionStatement"; 49 case NodeKind::CompoundStatement: 50 return OS << "CompoundStatement"; 51 case NodeKind::UnknownDeclaration: 52 return OS << "UnknownDeclaration"; 53 case NodeKind::EmptyDeclaration: 54 return OS << "EmptyDeclaration"; 55 case NodeKind::StaticAssertDeclaration: 56 return OS << "StaticAssertDeclaration"; 57 case NodeKind::LinkageSpecificationDeclaration: 58 return OS << "LinkageSpecificationDeclaration"; 59 case NodeKind::SimpleDeclaration: 60 return OS << "SimpleDeclaration"; 61 case NodeKind::NamespaceDefinition: 62 return OS << "NamespaceDefinition"; 63 case NodeKind::NamespaceAliasDefinition: 64 return OS << "NamespaceAliasDefinition"; 65 case NodeKind::UsingNamespaceDirective: 66 return OS << "UsingNamespaceDirective"; 67 case NodeKind::UsingDeclaration: 68 return OS << "UsingDeclaration"; 69 case NodeKind::TypeAliasDeclaration: 70 return OS << "TypeAliasDeclaration"; 71 } 72 llvm_unreachable("unknown node kind"); 73 } 74 75 llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) { 76 switch (R) { 77 case syntax::NodeRole::Detached: 78 return OS << "Detached"; 79 case syntax::NodeRole::Unknown: 80 return OS << "Unknown"; 81 case syntax::NodeRole::OpenParen: 82 return OS << "OpenParen"; 83 case syntax::NodeRole::CloseParen: 84 return OS << "CloseParen"; 85 case syntax::NodeRole::IntroducerKeyword: 86 return OS << "IntroducerKeyword"; 87 case syntax::NodeRole::BodyStatement: 88 return OS << "BodyStatement"; 89 case syntax::NodeRole::CaseStatement_value: 90 return OS << "CaseStatement_value"; 91 case syntax::NodeRole::IfStatement_thenStatement: 92 return OS << "IfStatement_thenStatement"; 93 case syntax::NodeRole::IfStatement_elseKeyword: 94 return OS << "IfStatement_elseKeyword"; 95 case syntax::NodeRole::IfStatement_elseStatement: 96 return OS << "IfStatement_elseStatement"; 97 case syntax::NodeRole::ReturnStatement_value: 98 return OS << "ReturnStatement_value"; 99 case syntax::NodeRole::ExpressionStatement_expression: 100 return OS << "ExpressionStatement_expression"; 101 case syntax::NodeRole::CompoundStatement_statement: 102 return OS << "CompoundStatement_statement"; 103 case syntax::NodeRole::StaticAssertDeclaration_condition: 104 return OS << "StaticAssertDeclaration_condition"; 105 case syntax::NodeRole::StaticAssertDeclaration_message: 106 return OS << "StaticAssertDeclaration_message"; 107 } 108 llvm_unreachable("invalid role"); 109 } 110 111 syntax::Leaf *syntax::SwitchStatement::switchKeyword() { 112 return llvm::cast_or_null<syntax::Leaf>( 113 findChild(syntax::NodeRole::IntroducerKeyword)); 114 } 115 116 syntax::Statement *syntax::SwitchStatement::body() { 117 return llvm::cast_or_null<syntax::Statement>( 118 findChild(syntax::NodeRole::BodyStatement)); 119 } 120 121 syntax::Leaf *syntax::CaseStatement::caseKeyword() { 122 return llvm::cast_or_null<syntax::Leaf>( 123 findChild(syntax::NodeRole::IntroducerKeyword)); 124 } 125 126 syntax::Expression *syntax::CaseStatement::value() { 127 return llvm::cast_or_null<syntax::Expression>( 128 findChild(syntax::NodeRole::CaseStatement_value)); 129 } 130 131 syntax::Statement *syntax::CaseStatement::body() { 132 return llvm::cast_or_null<syntax::Statement>( 133 findChild(syntax::NodeRole::BodyStatement)); 134 } 135 136 syntax::Leaf *syntax::DefaultStatement::defaultKeyword() { 137 return llvm::cast_or_null<syntax::Leaf>( 138 findChild(syntax::NodeRole::IntroducerKeyword)); 139 } 140 141 syntax::Statement *syntax::DefaultStatement::body() { 142 return llvm::cast_or_null<syntax::Statement>( 143 findChild(syntax::NodeRole::BodyStatement)); 144 } 145 146 syntax::Leaf *syntax::IfStatement::ifKeyword() { 147 return llvm::cast_or_null<syntax::Leaf>( 148 findChild(syntax::NodeRole::IntroducerKeyword)); 149 } 150 151 syntax::Statement *syntax::IfStatement::thenStatement() { 152 return llvm::cast_or_null<syntax::Statement>( 153 findChild(syntax::NodeRole::IfStatement_thenStatement)); 154 } 155 156 syntax::Leaf *syntax::IfStatement::elseKeyword() { 157 return llvm::cast_or_null<syntax::Leaf>( 158 findChild(syntax::NodeRole::IfStatement_elseKeyword)); 159 } 160 161 syntax::Statement *syntax::IfStatement::elseStatement() { 162 return llvm::cast_or_null<syntax::Statement>( 163 findChild(syntax::NodeRole::IfStatement_elseStatement)); 164 } 165 166 syntax::Leaf *syntax::ForStatement::forKeyword() { 167 return llvm::cast_or_null<syntax::Leaf>( 168 findChild(syntax::NodeRole::IntroducerKeyword)); 169 } 170 171 syntax::Statement *syntax::ForStatement::body() { 172 return llvm::cast_or_null<syntax::Statement>( 173 findChild(syntax::NodeRole::BodyStatement)); 174 } 175 176 syntax::Leaf *syntax::WhileStatement::whileKeyword() { 177 return llvm::cast_or_null<syntax::Leaf>( 178 findChild(syntax::NodeRole::IntroducerKeyword)); 179 } 180 181 syntax::Statement *syntax::WhileStatement::body() { 182 return llvm::cast_or_null<syntax::Statement>( 183 findChild(syntax::NodeRole::BodyStatement)); 184 } 185 186 syntax::Leaf *syntax::ContinueStatement::continueKeyword() { 187 return llvm::cast_or_null<syntax::Leaf>( 188 findChild(syntax::NodeRole::IntroducerKeyword)); 189 } 190 191 syntax::Leaf *syntax::BreakStatement::breakKeyword() { 192 return llvm::cast_or_null<syntax::Leaf>( 193 findChild(syntax::NodeRole::IntroducerKeyword)); 194 } 195 196 syntax::Leaf *syntax::ReturnStatement::returnKeyword() { 197 return llvm::cast_or_null<syntax::Leaf>( 198 findChild(syntax::NodeRole::IntroducerKeyword)); 199 } 200 201 syntax::Expression *syntax::ReturnStatement::value() { 202 return llvm::cast_or_null<syntax::Expression>( 203 findChild(syntax::NodeRole::ReturnStatement_value)); 204 } 205 206 syntax::Leaf *syntax::RangeBasedForStatement::forKeyword() { 207 return llvm::cast_or_null<syntax::Leaf>( 208 findChild(syntax::NodeRole::IntroducerKeyword)); 209 } 210 211 syntax::Statement *syntax::RangeBasedForStatement::body() { 212 return llvm::cast_or_null<syntax::Statement>( 213 findChild(syntax::NodeRole::BodyStatement)); 214 } 215 216 syntax::Expression *syntax::ExpressionStatement::expression() { 217 return llvm::cast_or_null<syntax::Expression>( 218 findChild(syntax::NodeRole::ExpressionStatement_expression)); 219 } 220 221 syntax::Leaf *syntax::CompoundStatement::lbrace() { 222 return llvm::cast_or_null<syntax::Leaf>( 223 findChild(syntax::NodeRole::OpenParen)); 224 } 225 226 std::vector<syntax::Statement *> syntax::CompoundStatement::statements() { 227 std::vector<syntax::Statement *> Children; 228 for (auto *C = firstChild(); C; C = C->nextSibling()) { 229 if (C->role() == syntax::NodeRole::CompoundStatement_statement) 230 Children.push_back(llvm::cast<syntax::Statement>(C)); 231 } 232 return Children; 233 } 234 235 syntax::Leaf *syntax::CompoundStatement::rbrace() { 236 return llvm::cast_or_null<syntax::Leaf>( 237 findChild(syntax::NodeRole::CloseParen)); 238 } 239 240 syntax::Expression *syntax::StaticAssertDeclaration::condition() { 241 return llvm::cast_or_null<syntax::Expression>( 242 findChild(syntax::NodeRole::StaticAssertDeclaration_condition)); 243 } 244 245 syntax::Expression *syntax::StaticAssertDeclaration::message() { 246 return llvm::cast_or_null<syntax::Expression>( 247 findChild(syntax::NodeRole::StaticAssertDeclaration_message)); 248 } 249