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::ParenExpression: 22 return OS << "ParenExpression"; 23 case NodeKind::IntegerLiteralExpression: 24 return OS << "IntegerLiteralExpression"; 25 case NodeKind::CharacterLiteralExpression: 26 return OS << "CharacterLiteralExpression"; 27 case NodeKind::FloatingLiteralExpression: 28 return OS << "FloatingLiteralExpression"; 29 case NodeKind::StringLiteralExpression: 30 return OS << "StringLiteralExpression"; 31 case NodeKind::BoolLiteralExpression: 32 return OS << "BoolLiteralExpression"; 33 case NodeKind::CxxNullPtrExpression: 34 return OS << "CxxNullPtrExpression"; 35 case NodeKind::IntegerUserDefinedLiteralExpression: 36 return OS << "IntegerUserDefinedLiteralExpression"; 37 case NodeKind::FloatUserDefinedLiteralExpression: 38 return OS << "FloatUserDefinedLiteralExpression"; 39 case NodeKind::CharUserDefinedLiteralExpression: 40 return OS << "CharUserDefinedLiteralExpression"; 41 case NodeKind::StringUserDefinedLiteralExpression: 42 return OS << "StringUserDefinedLiteralExpression"; 43 case NodeKind::PrefixUnaryOperatorExpression: 44 return OS << "PrefixUnaryOperatorExpression"; 45 case NodeKind::PostfixUnaryOperatorExpression: 46 return OS << "PostfixUnaryOperatorExpression"; 47 case NodeKind::BinaryOperatorExpression: 48 return OS << "BinaryOperatorExpression"; 49 case NodeKind::UnqualifiedId: 50 return OS << "UnqualifiedId"; 51 case NodeKind::IdExpression: 52 return OS << "IdExpression"; 53 case NodeKind::UnknownStatement: 54 return OS << "UnknownStatement"; 55 case NodeKind::DeclarationStatement: 56 return OS << "DeclarationStatement"; 57 case NodeKind::EmptyStatement: 58 return OS << "EmptyStatement"; 59 case NodeKind::SwitchStatement: 60 return OS << "SwitchStatement"; 61 case NodeKind::CaseStatement: 62 return OS << "CaseStatement"; 63 case NodeKind::DefaultStatement: 64 return OS << "DefaultStatement"; 65 case NodeKind::IfStatement: 66 return OS << "IfStatement"; 67 case NodeKind::ForStatement: 68 return OS << "ForStatement"; 69 case NodeKind::WhileStatement: 70 return OS << "WhileStatement"; 71 case NodeKind::ContinueStatement: 72 return OS << "ContinueStatement"; 73 case NodeKind::BreakStatement: 74 return OS << "BreakStatement"; 75 case NodeKind::ReturnStatement: 76 return OS << "ReturnStatement"; 77 case NodeKind::RangeBasedForStatement: 78 return OS << "RangeBasedForStatement"; 79 case NodeKind::ExpressionStatement: 80 return OS << "ExpressionStatement"; 81 case NodeKind::CompoundStatement: 82 return OS << "CompoundStatement"; 83 case NodeKind::UnknownDeclaration: 84 return OS << "UnknownDeclaration"; 85 case NodeKind::EmptyDeclaration: 86 return OS << "EmptyDeclaration"; 87 case NodeKind::StaticAssertDeclaration: 88 return OS << "StaticAssertDeclaration"; 89 case NodeKind::LinkageSpecificationDeclaration: 90 return OS << "LinkageSpecificationDeclaration"; 91 case NodeKind::SimpleDeclaration: 92 return OS << "SimpleDeclaration"; 93 case NodeKind::TemplateDeclaration: 94 return OS << "TemplateDeclaration"; 95 case NodeKind::ExplicitTemplateInstantiation: 96 return OS << "ExplicitTemplateInstantiation"; 97 case NodeKind::NamespaceDefinition: 98 return OS << "NamespaceDefinition"; 99 case NodeKind::NamespaceAliasDefinition: 100 return OS << "NamespaceAliasDefinition"; 101 case NodeKind::UsingNamespaceDirective: 102 return OS << "UsingNamespaceDirective"; 103 case NodeKind::UsingDeclaration: 104 return OS << "UsingDeclaration"; 105 case NodeKind::TypeAliasDeclaration: 106 return OS << "TypeAliasDeclaration"; 107 case NodeKind::SimpleDeclarator: 108 return OS << "SimpleDeclarator"; 109 case NodeKind::ParenDeclarator: 110 return OS << "ParenDeclarator"; 111 case NodeKind::ArraySubscript: 112 return OS << "ArraySubscript"; 113 case NodeKind::TrailingReturnType: 114 return OS << "TrailingReturnType"; 115 case NodeKind::ParametersAndQualifiers: 116 return OS << "ParametersAndQualifiers"; 117 case NodeKind::MemberPointer: 118 return OS << "MemberPointer"; 119 case NodeKind::NameSpecifier: 120 return OS << "NameSpecifier"; 121 case NodeKind::NestedNameSpecifier: 122 return OS << "NestedNameSpecifier"; 123 } 124 llvm_unreachable("unknown node kind"); 125 } 126 127 llvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) { 128 switch (R) { 129 case syntax::NodeRole::Detached: 130 return OS << "Detached"; 131 case syntax::NodeRole::Unknown: 132 return OS << "Unknown"; 133 case syntax::NodeRole::OpenParen: 134 return OS << "OpenParen"; 135 case syntax::NodeRole::CloseParen: 136 return OS << "CloseParen"; 137 case syntax::NodeRole::IntroducerKeyword: 138 return OS << "IntroducerKeyword"; 139 case syntax::NodeRole::LiteralToken: 140 return OS << "LiteralToken"; 141 case syntax::NodeRole::ArrowToken: 142 return OS << "ArrowToken"; 143 case syntax::NodeRole::ExternKeyword: 144 return OS << "ExternKeyword"; 145 case syntax::NodeRole::BodyStatement: 146 return OS << "BodyStatement"; 147 case syntax::NodeRole::CaseStatement_value: 148 return OS << "CaseStatement_value"; 149 case syntax::NodeRole::IfStatement_thenStatement: 150 return OS << "IfStatement_thenStatement"; 151 case syntax::NodeRole::IfStatement_elseKeyword: 152 return OS << "IfStatement_elseKeyword"; 153 case syntax::NodeRole::IfStatement_elseStatement: 154 return OS << "IfStatement_elseStatement"; 155 case syntax::NodeRole::OperatorExpression_operatorToken: 156 return OS << "OperatorExpression_operatorToken"; 157 case syntax::NodeRole::UnaryOperatorExpression_operand: 158 return OS << "UnaryOperatorExpression_operand"; 159 case syntax::NodeRole::BinaryOperatorExpression_leftHandSide: 160 return OS << "BinaryOperatorExpression_leftHandSide"; 161 case syntax::NodeRole::BinaryOperatorExpression_rightHandSide: 162 return OS << "BinaryOperatorExpression_rightHandSide"; 163 case syntax::NodeRole::ReturnStatement_value: 164 return OS << "ReturnStatement_value"; 165 case syntax::NodeRole::ExpressionStatement_expression: 166 return OS << "ExpressionStatement_expression"; 167 case syntax::NodeRole::CompoundStatement_statement: 168 return OS << "CompoundStatement_statement"; 169 case syntax::NodeRole::StaticAssertDeclaration_condition: 170 return OS << "StaticAssertDeclaration_condition"; 171 case syntax::NodeRole::StaticAssertDeclaration_message: 172 return OS << "StaticAssertDeclaration_message"; 173 case syntax::NodeRole::SimpleDeclaration_declarator: 174 return OS << "SimpleDeclaration_declarator"; 175 case syntax::NodeRole::TemplateDeclaration_declaration: 176 return OS << "TemplateDeclaration_declaration"; 177 case syntax::NodeRole::ExplicitTemplateInstantiation_declaration: 178 return OS << "ExplicitTemplateInstantiation_declaration"; 179 case syntax::NodeRole::ArraySubscript_sizeExpression: 180 return OS << "ArraySubscript_sizeExpression"; 181 case syntax::NodeRole::TrailingReturnType_declarator: 182 return OS << "TrailingReturnType_declarator"; 183 case syntax::NodeRole::ParametersAndQualifiers_parameter: 184 return OS << "ParametersAndQualifiers_parameter"; 185 case syntax::NodeRole::ParametersAndQualifiers_trailingReturn: 186 return OS << "ParametersAndQualifiers_trailingReturn"; 187 case syntax::NodeRole::IdExpression_id: 188 return OS << "IdExpression_id"; 189 case syntax::NodeRole::IdExpression_qualifier: 190 return OS << "IdExpression_qualifier"; 191 case syntax::NodeRole::NestedNameSpecifier_specifier: 192 return OS << "NestedNameSpecifier_specifier"; 193 case syntax::NodeRole::ParenExpression_subExpression: 194 return OS << "ParenExpression_subExpression"; 195 } 196 llvm_unreachable("invalid role"); 197 } 198 199 std::vector<syntax::NameSpecifier *> syntax::NestedNameSpecifier::specifiers() { 200 std::vector<syntax::NameSpecifier *> Children; 201 for (auto *C = firstChild(); C; C = C->nextSibling()) { 202 assert(C->role() == syntax::NodeRole::NestedNameSpecifier_specifier); 203 Children.push_back(llvm::cast<syntax::NameSpecifier>(C)); 204 } 205 return Children; 206 } 207 208 syntax::NestedNameSpecifier *syntax::IdExpression::qualifier() { 209 return llvm::cast_or_null<syntax::NestedNameSpecifier>( 210 findChild(syntax::NodeRole::IdExpression_qualifier)); 211 } 212 213 syntax::UnqualifiedId *syntax::IdExpression::unqualifiedId() { 214 return llvm::cast_or_null<syntax::UnqualifiedId>( 215 findChild(syntax::NodeRole::IdExpression_id)); 216 } 217 218 syntax::Leaf *syntax::ParenExpression::openParen() { 219 return llvm::cast_or_null<syntax::Leaf>( 220 findChild(syntax::NodeRole::OpenParen)); 221 } 222 223 syntax::Expression *syntax::ParenExpression::subExpression() { 224 return llvm::cast_or_null<syntax::Expression>( 225 findChild(syntax::NodeRole::ParenExpression_subExpression)); 226 } 227 228 syntax::Leaf *syntax::ParenExpression::closeParen() { 229 return llvm::cast_or_null<syntax::Leaf>( 230 findChild(syntax::NodeRole::CloseParen)); 231 } 232 233 syntax::Leaf *syntax::IntegerLiteralExpression::literalToken() { 234 return llvm::cast_or_null<syntax::Leaf>( 235 findChild(syntax::NodeRole::LiteralToken)); 236 } 237 238 syntax::Leaf *syntax::CharacterLiteralExpression::literalToken() { 239 return llvm::cast_or_null<syntax::Leaf>( 240 findChild(syntax::NodeRole::LiteralToken)); 241 } 242 243 syntax::Leaf *syntax::FloatingLiteralExpression::literalToken() { 244 return llvm::cast_or_null<syntax::Leaf>( 245 findChild(syntax::NodeRole::LiteralToken)); 246 } 247 248 syntax::Leaf *syntax::StringLiteralExpression::literalToken() { 249 return llvm::cast_or_null<syntax::Leaf>( 250 findChild(syntax::NodeRole::LiteralToken)); 251 } 252 253 syntax::Leaf *syntax::BoolLiteralExpression::literalToken() { 254 return llvm::cast_or_null<syntax::Leaf>( 255 findChild(syntax::NodeRole::LiteralToken)); 256 } 257 258 syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() { 259 return llvm::cast_or_null<syntax::Leaf>( 260 findChild(syntax::NodeRole::LiteralToken)); 261 } 262 263 syntax::Leaf *syntax::UserDefinedLiteralExpression::literalToken() { 264 return llvm::cast_or_null<syntax::Leaf>( 265 findChild(syntax::NodeRole::LiteralToken)); 266 } 267 268 syntax::Expression *syntax::BinaryOperatorExpression::lhs() { 269 return llvm::cast_or_null<syntax::Expression>( 270 findChild(syntax::NodeRole::BinaryOperatorExpression_leftHandSide)); 271 } 272 273 syntax::Leaf *syntax::UnaryOperatorExpression::operatorToken() { 274 return llvm::cast_or_null<syntax::Leaf>( 275 findChild(syntax::NodeRole::OperatorExpression_operatorToken)); 276 } 277 278 syntax::Expression *syntax::UnaryOperatorExpression::operand() { 279 return llvm::cast_or_null<syntax::Expression>( 280 findChild(syntax::NodeRole::UnaryOperatorExpression_operand)); 281 } 282 283 syntax::Leaf *syntax::BinaryOperatorExpression::operatorToken() { 284 return llvm::cast_or_null<syntax::Leaf>( 285 findChild(syntax::NodeRole::OperatorExpression_operatorToken)); 286 } 287 288 syntax::Expression *syntax::BinaryOperatorExpression::rhs() { 289 return llvm::cast_or_null<syntax::Expression>( 290 findChild(syntax::NodeRole::BinaryOperatorExpression_rightHandSide)); 291 } 292 293 syntax::Leaf *syntax::SwitchStatement::switchKeyword() { 294 return llvm::cast_or_null<syntax::Leaf>( 295 findChild(syntax::NodeRole::IntroducerKeyword)); 296 } 297 298 syntax::Statement *syntax::SwitchStatement::body() { 299 return llvm::cast_or_null<syntax::Statement>( 300 findChild(syntax::NodeRole::BodyStatement)); 301 } 302 303 syntax::Leaf *syntax::CaseStatement::caseKeyword() { 304 return llvm::cast_or_null<syntax::Leaf>( 305 findChild(syntax::NodeRole::IntroducerKeyword)); 306 } 307 308 syntax::Expression *syntax::CaseStatement::value() { 309 return llvm::cast_or_null<syntax::Expression>( 310 findChild(syntax::NodeRole::CaseStatement_value)); 311 } 312 313 syntax::Statement *syntax::CaseStatement::body() { 314 return llvm::cast_or_null<syntax::Statement>( 315 findChild(syntax::NodeRole::BodyStatement)); 316 } 317 318 syntax::Leaf *syntax::DefaultStatement::defaultKeyword() { 319 return llvm::cast_or_null<syntax::Leaf>( 320 findChild(syntax::NodeRole::IntroducerKeyword)); 321 } 322 323 syntax::Statement *syntax::DefaultStatement::body() { 324 return llvm::cast_or_null<syntax::Statement>( 325 findChild(syntax::NodeRole::BodyStatement)); 326 } 327 328 syntax::Leaf *syntax::IfStatement::ifKeyword() { 329 return llvm::cast_or_null<syntax::Leaf>( 330 findChild(syntax::NodeRole::IntroducerKeyword)); 331 } 332 333 syntax::Statement *syntax::IfStatement::thenStatement() { 334 return llvm::cast_or_null<syntax::Statement>( 335 findChild(syntax::NodeRole::IfStatement_thenStatement)); 336 } 337 338 syntax::Leaf *syntax::IfStatement::elseKeyword() { 339 return llvm::cast_or_null<syntax::Leaf>( 340 findChild(syntax::NodeRole::IfStatement_elseKeyword)); 341 } 342 343 syntax::Statement *syntax::IfStatement::elseStatement() { 344 return llvm::cast_or_null<syntax::Statement>( 345 findChild(syntax::NodeRole::IfStatement_elseStatement)); 346 } 347 348 syntax::Leaf *syntax::ForStatement::forKeyword() { 349 return llvm::cast_or_null<syntax::Leaf>( 350 findChild(syntax::NodeRole::IntroducerKeyword)); 351 } 352 353 syntax::Statement *syntax::ForStatement::body() { 354 return llvm::cast_or_null<syntax::Statement>( 355 findChild(syntax::NodeRole::BodyStatement)); 356 } 357 358 syntax::Leaf *syntax::WhileStatement::whileKeyword() { 359 return llvm::cast_or_null<syntax::Leaf>( 360 findChild(syntax::NodeRole::IntroducerKeyword)); 361 } 362 363 syntax::Statement *syntax::WhileStatement::body() { 364 return llvm::cast_or_null<syntax::Statement>( 365 findChild(syntax::NodeRole::BodyStatement)); 366 } 367 368 syntax::Leaf *syntax::ContinueStatement::continueKeyword() { 369 return llvm::cast_or_null<syntax::Leaf>( 370 findChild(syntax::NodeRole::IntroducerKeyword)); 371 } 372 373 syntax::Leaf *syntax::BreakStatement::breakKeyword() { 374 return llvm::cast_or_null<syntax::Leaf>( 375 findChild(syntax::NodeRole::IntroducerKeyword)); 376 } 377 378 syntax::Leaf *syntax::ReturnStatement::returnKeyword() { 379 return llvm::cast_or_null<syntax::Leaf>( 380 findChild(syntax::NodeRole::IntroducerKeyword)); 381 } 382 383 syntax::Expression *syntax::ReturnStatement::value() { 384 return llvm::cast_or_null<syntax::Expression>( 385 findChild(syntax::NodeRole::ReturnStatement_value)); 386 } 387 388 syntax::Leaf *syntax::RangeBasedForStatement::forKeyword() { 389 return llvm::cast_or_null<syntax::Leaf>( 390 findChild(syntax::NodeRole::IntroducerKeyword)); 391 } 392 393 syntax::Statement *syntax::RangeBasedForStatement::body() { 394 return llvm::cast_or_null<syntax::Statement>( 395 findChild(syntax::NodeRole::BodyStatement)); 396 } 397 398 syntax::Expression *syntax::ExpressionStatement::expression() { 399 return llvm::cast_or_null<syntax::Expression>( 400 findChild(syntax::NodeRole::ExpressionStatement_expression)); 401 } 402 403 syntax::Leaf *syntax::CompoundStatement::lbrace() { 404 return llvm::cast_or_null<syntax::Leaf>( 405 findChild(syntax::NodeRole::OpenParen)); 406 } 407 408 std::vector<syntax::Statement *> syntax::CompoundStatement::statements() { 409 std::vector<syntax::Statement *> Children; 410 for (auto *C = firstChild(); C; C = C->nextSibling()) { 411 assert(C->role() == syntax::NodeRole::CompoundStatement_statement); 412 Children.push_back(llvm::cast<syntax::Statement>(C)); 413 } 414 return Children; 415 } 416 417 syntax::Leaf *syntax::CompoundStatement::rbrace() { 418 return llvm::cast_or_null<syntax::Leaf>( 419 findChild(syntax::NodeRole::CloseParen)); 420 } 421 422 syntax::Expression *syntax::StaticAssertDeclaration::condition() { 423 return llvm::cast_or_null<syntax::Expression>( 424 findChild(syntax::NodeRole::StaticAssertDeclaration_condition)); 425 } 426 427 syntax::Expression *syntax::StaticAssertDeclaration::message() { 428 return llvm::cast_or_null<syntax::Expression>( 429 findChild(syntax::NodeRole::StaticAssertDeclaration_message)); 430 } 431 432 std::vector<syntax::SimpleDeclarator *> 433 syntax::SimpleDeclaration::declarators() { 434 std::vector<syntax::SimpleDeclarator *> Children; 435 for (auto *C = firstChild(); C; C = C->nextSibling()) { 436 if (C->role() == syntax::NodeRole::SimpleDeclaration_declarator) 437 Children.push_back(llvm::cast<syntax::SimpleDeclarator>(C)); 438 } 439 return Children; 440 } 441 442 syntax::Leaf *syntax::TemplateDeclaration::templateKeyword() { 443 return llvm::cast_or_null<syntax::Leaf>( 444 findChild(syntax::NodeRole::IntroducerKeyword)); 445 } 446 447 syntax::Declaration *syntax::TemplateDeclaration::declaration() { 448 return llvm::cast_or_null<syntax::Declaration>( 449 findChild(syntax::NodeRole::TemplateDeclaration_declaration)); 450 } 451 452 syntax::Leaf *syntax::ExplicitTemplateInstantiation::templateKeyword() { 453 return llvm::cast_or_null<syntax::Leaf>( 454 findChild(syntax::NodeRole::IntroducerKeyword)); 455 } 456 457 syntax::Leaf *syntax::ExplicitTemplateInstantiation::externKeyword() { 458 return llvm::cast_or_null<syntax::Leaf>( 459 findChild(syntax::NodeRole::ExternKeyword)); 460 } 461 462 syntax::Declaration *syntax::ExplicitTemplateInstantiation::declaration() { 463 return llvm::cast_or_null<syntax::Declaration>( 464 findChild(syntax::NodeRole::ExplicitTemplateInstantiation_declaration)); 465 } 466 467 syntax::Leaf *syntax::ParenDeclarator::lparen() { 468 return llvm::cast_or_null<syntax::Leaf>( 469 findChild(syntax::NodeRole::OpenParen)); 470 } 471 472 syntax::Leaf *syntax::ParenDeclarator::rparen() { 473 return llvm::cast_or_null<syntax::Leaf>( 474 findChild(syntax::NodeRole::CloseParen)); 475 } 476 477 syntax::Leaf *syntax::ArraySubscript::lbracket() { 478 return llvm::cast_or_null<syntax::Leaf>( 479 findChild(syntax::NodeRole::OpenParen)); 480 } 481 482 syntax::Expression *syntax::ArraySubscript::sizeExpression() { 483 return llvm::cast_or_null<syntax::Expression>( 484 findChild(syntax::NodeRole::ArraySubscript_sizeExpression)); 485 } 486 487 syntax::Leaf *syntax::ArraySubscript::rbracket() { 488 return llvm::cast_or_null<syntax::Leaf>( 489 findChild(syntax::NodeRole::CloseParen)); 490 } 491 492 syntax::Leaf *syntax::TrailingReturnType::arrowToken() { 493 return llvm::cast_or_null<syntax::Leaf>( 494 findChild(syntax::NodeRole::ArrowToken)); 495 } 496 497 syntax::SimpleDeclarator *syntax::TrailingReturnType::declarator() { 498 return llvm::cast_or_null<syntax::SimpleDeclarator>( 499 findChild(syntax::NodeRole::TrailingReturnType_declarator)); 500 } 501 502 syntax::Leaf *syntax::ParametersAndQualifiers::lparen() { 503 return llvm::cast_or_null<syntax::Leaf>( 504 findChild(syntax::NodeRole::OpenParen)); 505 } 506 507 std::vector<syntax::SimpleDeclaration *> 508 syntax::ParametersAndQualifiers::parameters() { 509 std::vector<syntax::SimpleDeclaration *> Children; 510 for (auto *C = firstChild(); C; C = C->nextSibling()) { 511 if (C->role() == syntax::NodeRole::ParametersAndQualifiers_parameter) 512 Children.push_back(llvm::cast<syntax::SimpleDeclaration>(C)); 513 } 514 return Children; 515 } 516 517 syntax::Leaf *syntax::ParametersAndQualifiers::rparen() { 518 return llvm::cast_or_null<syntax::Leaf>( 519 findChild(syntax::NodeRole::CloseParen)); 520 } 521 522 syntax::TrailingReturnType *syntax::ParametersAndQualifiers::trailingReturn() { 523 return llvm::cast_or_null<syntax::TrailingReturnType>( 524 findChild(syntax::NodeRole::ParametersAndQualifiers_trailingReturn)); 525 } 526