clang  14.0.0git
Nodes.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
10 
11 using namespace clang;
12 
13 raw_ostream &syntax::operator<<(raw_ostream &OS, NodeKind K) {
14  switch (K) {
15 #define CONCRETE_NODE(Kind, Parent) \
16  case NodeKind::Kind: \
17  return OS << #Kind;
18 #include "clang/Tooling/Syntax/Nodes.inc"
19  }
20  llvm_unreachable("unknown node kind");
21 }
22 
23 raw_ostream &syntax::operator<<(raw_ostream &OS, NodeRole R) {
24  switch (R) {
26  return OS << "Detached";
28  return OS << "Unknown";
30  return OS << "OpenParen";
32  return OS << "CloseParen";
34  return OS << "IntroducerKeyword";
36  return OS << "LiteralToken";
38  return OS << "ArrowToken";
40  return OS << "ExternKeyword";
42  return OS << "TemplateKeyword";
44  return OS << "BodyStatement";
46  return OS << "ListElement";
48  return OS << "ListDelimiter";
50  return OS << "CaseValue";
52  return OS << "ReturnValue";
54  return OS << "ThenStatement";
56  return OS << "ElseKeyword";
58  return OS << "ElseStatement";
60  return OS << "OperatorToken";
62  return OS << "Operand";
64  return OS << "LeftHandSide";
66  return OS << "RightHandSide";
68  return OS << "Expression";
70  return OS << "Statement";
72  return OS << "Condition";
74  return OS << "Message";
76  return OS << "Declarator";
78  return OS << "Declaration";
80  return OS << "Size";
82  return OS << "Parameters";
84  return OS << "TrailingReturn";
86  return OS << "UnqualifiedId";
88  return OS << "Qualifier";
90  return OS << "SubExpression";
92  return OS << "Object";
94  return OS << "AccessToken";
96  return OS << "Member";
98  return OS << "Callee";
100  return OS << "Arguments";
102  return OS << "Declarators";
103  }
104  llvm_unreachable("invalid role");
105 }
106 
107 // We could have an interator in list to not pay memory costs of temporary
108 // vector
109 std::vector<syntax::NameSpecifier *>
111  auto SpecifiersAsNodes = getElementsAsNodes();
112  std::vector<syntax::NameSpecifier *> Children;
113  for (const auto &Element : SpecifiersAsNodes) {
114  Children.push_back(llvm::cast<syntax::NameSpecifier>(Element));
115  }
116  return Children;
117 }
118 
119 std::vector<syntax::List::ElementAndDelimiter<syntax::NameSpecifier>>
121  auto SpecifiersAsNodesAndDoubleColons = getElementsAsNodesAndDelimiters();
122  std::vector<syntax::List::ElementAndDelimiter<syntax::NameSpecifier>>
123  Children;
124  for (const auto &SpecifierAndDoubleColon : SpecifiersAsNodesAndDoubleColons) {
125  Children.push_back(
126  {llvm::cast<syntax::NameSpecifier>(SpecifierAndDoubleColon.element),
127  SpecifierAndDoubleColon.delimiter});
128  }
129  return Children;
130 }
131 
132 std::vector<syntax::Expression *> syntax::CallArguments::getArguments() {
133  auto ArgumentsAsNodes = getElementsAsNodes();
134  std::vector<syntax::Expression *> Children;
135  for (const auto &ArgumentAsNode : ArgumentsAsNodes) {
136  Children.push_back(llvm::cast<syntax::Expression>(ArgumentAsNode));
137  }
138  return Children;
139 }
140 
141 std::vector<syntax::List::ElementAndDelimiter<syntax::Expression>>
143  auto ArgumentsAsNodesAndCommas = getElementsAsNodesAndDelimiters();
144  std::vector<syntax::List::ElementAndDelimiter<syntax::Expression>> Children;
145  for (const auto &ArgumentAsNodeAndComma : ArgumentsAsNodesAndCommas) {
146  Children.push_back(
147  {llvm::cast<syntax::Expression>(ArgumentAsNodeAndComma.element),
148  ArgumentAsNodeAndComma.delimiter});
149  }
150  return Children;
151 }
152 
153 std::vector<syntax::SimpleDeclaration *>
155  auto ParametersAsNodes = getElementsAsNodes();
156  std::vector<syntax::SimpleDeclaration *> Children;
157  for (const auto &ParameterAsNode : ParametersAsNodes) {
158  Children.push_back(llvm::cast<syntax::SimpleDeclaration>(ParameterAsNode));
159  }
160  return Children;
161 }
162 
163 std::vector<syntax::List::ElementAndDelimiter<syntax::SimpleDeclaration>>
165  auto ParametersAsNodesAndCommas = getElementsAsNodesAndDelimiters();
166  std::vector<syntax::List::ElementAndDelimiter<syntax::SimpleDeclaration>>
167  Children;
168  for (const auto &ParameterAsNodeAndComma : ParametersAsNodesAndCommas) {
169  Children.push_back(
170  {llvm::cast<syntax::SimpleDeclaration>(ParameterAsNodeAndComma.element),
171  ParameterAsNodeAndComma.delimiter});
172  }
173  return Children;
174 }
175 
176 std::vector<syntax::SimpleDeclarator *>
178  auto DeclaratorsAsNodes = getElementsAsNodes();
179  std::vector<syntax::SimpleDeclarator *> Children;
180  for (const auto &DeclaratorAsNode : DeclaratorsAsNodes) {
181  Children.push_back(llvm::cast<syntax::SimpleDeclarator>(DeclaratorAsNode));
182  }
183  return Children;
184 }
185 
186 std::vector<syntax::List::ElementAndDelimiter<syntax::SimpleDeclarator>>
188  auto DeclaratorsAsNodesAndCommas = getElementsAsNodesAndDelimiters();
189  std::vector<syntax::List::ElementAndDelimiter<syntax::SimpleDeclarator>>
190  Children;
191  for (const auto &DeclaratorAsNodeAndComma : DeclaratorsAsNodesAndCommas) {
192  Children.push_back(
193  {llvm::cast<syntax::SimpleDeclarator>(DeclaratorAsNodeAndComma.element),
194  DeclaratorAsNodeAndComma.delimiter});
195  }
196  return Children;
197 }
198 
200  return cast_or_null<syntax::Expression>(
201  findChild(syntax::NodeRole::LeftHandSide));
202 }
203 
205  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OperatorToken));
206 }
207 
209  return cast_or_null<syntax::Expression>(findChild(syntax::NodeRole::Operand));
210 }
211 
213  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OperatorToken));
214 }
215 
217  return cast_or_null<syntax::Expression>(
219 }
220 
222  return cast_or_null<syntax::Leaf>(
224 }
225 
227  return cast_or_null<syntax::Statement>(
229 }
230 
232  return cast_or_null<syntax::Leaf>(
234 }
235 
236 syntax::Expression *syntax::CaseStatement::getCaseValue() {
237  return cast_or_null<syntax::Expression>(
238  findChild(syntax::NodeRole::CaseValue));
239 }
240 
242  return cast_or_null<syntax::Statement>(
244 }
245 
247  return cast_or_null<syntax::Leaf>(
249 }
250 
252  return cast_or_null<syntax::Statement>(
254 }
255 
257  return cast_or_null<syntax::Leaf>(
259 }
260 
262  return cast_or_null<syntax::Statement>(
264 }
265 
267  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::ElseKeyword));
268 }
269 
271  return cast_or_null<syntax::Statement>(
273 }
274 
276  return cast_or_null<syntax::Leaf>(
278 }
279 
281  return cast_or_null<syntax::Statement>(
283 }
284 
286  return cast_or_null<syntax::Leaf>(
288 }
289 
291  return cast_or_null<syntax::Statement>(
293 }
294 
296  return cast_or_null<syntax::Leaf>(
298 }
299 
301  return cast_or_null<syntax::Leaf>(
303 }
304 
306  return cast_or_null<syntax::Leaf>(
308 }
309 
311  return cast_or_null<syntax::Expression>(
312  findChild(syntax::NodeRole::ReturnValue));
313 }
314 
316  return cast_or_null<syntax::Leaf>(
318 }
319 
321  return cast_or_null<syntax::Statement>(
323 }
324 
326  return cast_or_null<syntax::Expression>(
327  findChild(syntax::NodeRole::Expression));
328 }
329 
331  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OpenParen));
332 }
333 
334 std::vector<syntax::Statement *> syntax::CompoundStatement::getStatements() {
335  std::vector<syntax::Statement *> Children;
336  for (auto *C = getFirstChild(); C; C = C->getNextSibling()) {
337  assert(C->getRole() == syntax::NodeRole::Statement);
338  Children.push_back(cast<syntax::Statement>(C));
339  }
340  return Children;
341 }
342 
344  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::CloseParen));
345 }
346 
348  return cast_or_null<syntax::Expression>(
349  findChild(syntax::NodeRole::Condition));
350 }
351 
353  return cast_or_null<syntax::Expression>(findChild(syntax::NodeRole::Message));
354 }
355 
356 std::vector<syntax::SimpleDeclarator *>
358  std::vector<syntax::SimpleDeclarator *> Children;
359  for (auto *C = getFirstChild(); C; C = C->getNextSibling()) {
360  if (C->getRole() == syntax::NodeRole::Declarator)
361  Children.push_back(cast<syntax::SimpleDeclarator>(C));
362  }
363  return Children;
364 }
365 
367  return cast_or_null<syntax::Leaf>(
369 }
370 
372  return cast_or_null<syntax::Declaration>(
373  findChild(syntax::NodeRole::Declaration));
374 }
375 
377  return cast_or_null<syntax::Leaf>(
379 }
380 
382  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::ExternKeyword));
383 }
384 
386  return cast_or_null<syntax::Declaration>(
387  findChild(syntax::NodeRole::Declaration));
388 }
389 
391  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OpenParen));
392 }
393 
395  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::CloseParen));
396 }
397 
399  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OpenParen));
400 }
401 
402 syntax::Expression *syntax::ArraySubscript::getSize() {
403  return cast_or_null<syntax::Expression>(findChild(syntax::NodeRole::Size));
404 }
405 
407  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::CloseParen));
408 }
409 
411  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::ArrowToken));
412 }
413 
415  return cast_or_null<syntax::SimpleDeclarator>(
416  findChild(syntax::NodeRole::Declarator));
417 }
418 
420  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::OpenParen));
421 }
422 
425  return cast_or_null<syntax::ParameterDeclarationList>(
426  findChild(syntax::NodeRole::Parameters));
427 }
428 
430  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::CloseParen));
431 }
432 
435  return cast_or_null<syntax::TrailingReturnType>(
437 }
438 
439 #define NODE(Kind, Parent) \
440  static_assert(sizeof(syntax::Kind) > 0, "Missing Node subclass definition");
441 #include "clang/Tooling/Syntax/Nodes.inc"
clang::syntax::ForStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:280
clang::syntax::WhileStatement::getWhileKeyword
Leaf * getWhileKeyword()
Definition: Nodes.cpp:285
clang::syntax::BinaryOperatorExpression::getRhs
Expression * getRhs()
Definition: Nodes.cpp:216
clang::syntax::ParametersAndQualifiers::getLparen
Leaf * getLparen()
Definition: Nodes.cpp:419
clang::syntax::ReturnStatement::getReturnKeyword
Leaf * getReturnKeyword()
Definition: Nodes.cpp:305
clang::syntax::NodeRole::ElseStatement
@ ElseStatement
clang::syntax::ParameterDeclarationList::getParameterDeclarations
std::vector< SimpleDeclaration * > getParameterDeclarations()
Definition: Nodes.cpp:154
clang::syntax::ExplicitTemplateInstantiation::getTemplateKeyword
Leaf * getTemplateKeyword()
Definition: Nodes.cpp:376
clang::syntax::NodeRole::RightHandSide
@ RightHandSide
clang::syntax::NodeRole::Expression
@ Expression
clang::syntax::NodeRole::UnqualifiedId
@ UnqualifiedId
clang::syntax::operator<<
raw_ostream & operator<<(raw_ostream &OS, NodeKind K)
For debugging purposes.
Definition: Nodes.cpp:13
clang::syntax::TrailingReturnType::getArrowToken
Leaf * getArrowToken()
Definition: Nodes.cpp:410
clang::syntax::RangeBasedForStatement::getForKeyword
Leaf * getForKeyword()
Definition: Nodes.cpp:315
clang::syntax::NodeRole::Detached
@ Detached
A node without a parent.
clang::syntax::ReturnStatement::getReturnValue
Expression * getReturnValue()
Definition: Nodes.cpp:310
clang::syntax::NodeRole::CloseParen
@ CloseParen
A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.
clang::syntax::ParameterDeclarationList::getParametersAndCommas
std::vector< List::ElementAndDelimiter< syntax::SimpleDeclaration > > getParametersAndCommas()
Definition: Nodes.cpp:164
clang::syntax::NestedNameSpecifier::getSpecifiersAndDoubleColons
std::vector< List::ElementAndDelimiter< syntax::NameSpecifier > > getSpecifiersAndDoubleColons()
Definition: Nodes.cpp:120
clang::syntax::IfStatement::getThenStatement
Statement * getThenStatement()
Definition: Nodes.cpp:261
clang::syntax::CompoundStatement::getRbrace
Leaf * getRbrace()
Definition: Nodes.cpp:343
clang::syntax::NodeRole::TrailingReturn
@ TrailingReturn
clang::syntax::NodeRole::Statement
@ Statement
clang::syntax::CaseStatement::getCaseValue
Expression * getCaseValue()
Definition: Nodes.cpp:236
clang::syntax::NodeRole::Object
@ Object
clang::syntax::TrailingReturnType::getDeclarator
SimpleDeclarator * getDeclarator()
Definition: Nodes.cpp:414
clang::syntax::NodeRole::SubExpression
@ SubExpression
clang::syntax::ForStatement::getForKeyword
Leaf * getForKeyword()
Definition: Nodes.cpp:275
TokenKinds.h
clang::syntax::NodeRole::ArrowToken
@ ArrowToken
Tokens or Keywords.
clang::syntax::NodeRole::Parameters
@ Parameters
clang::syntax::ExplicitTemplateInstantiation::getDeclaration
Declaration * getDeclaration()
Definition: Nodes.cpp:385
clang::syntax::CompoundStatement::getStatements
std::vector< Statement * > getStatements()
FIXME: use custom iterator instead of 'vector'.
Definition: Nodes.cpp:334
clang::syntax::NodeRole::ExternKeyword
@ ExternKeyword
clang::syntax::ParametersAndQualifiers::getTrailingReturn
TrailingReturnType * getTrailingReturn()
Definition: Nodes.cpp:434
clang::syntax::NodeRole::Callee
@ Callee
clang::syntax::IfStatement::getElseStatement
Statement * getElseStatement()
Definition: Nodes.cpp:270
clang::syntax::NodeRole::ReturnValue
@ ReturnValue
clang::syntax::NodeRole::Qualifier
@ Qualifier
clang::syntax::DefaultStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:251
clang::syntax::BinaryOperatorExpression::getOperatorToken
Leaf * getOperatorToken()
Definition: Nodes.cpp:212
clang::syntax::NodeRole::ListElement
@ ListElement
List API roles.
clang::syntax::BreakStatement::getBreakKeyword
Leaf * getBreakKeyword()
Definition: Nodes.cpp:300
clang::syntax::WhileStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:290
clang::syntax::List::getElementsAsNodes
std::vector< Node * > getElementsAsNodes()
Returns the elements of the list.
Definition: Tree.cpp:386
clang::syntax::NodeRole::ElseKeyword
@ ElseKeyword
clang::syntax::NodeRole::ThenStatement
@ ThenStatement
clang::syntax::UnaryOperatorExpression::getOperatorToken
Leaf * getOperatorToken()
Definition: Nodes.cpp:204
clang::syntax::NodeRole::Unknown
@ Unknown
Children of an unknown semantic nature, e.g. skipped tokens, comments.
clang::syntax::DeclaratorList::getDeclarators
std::vector< SimpleDeclarator * > getDeclarators()
Definition: Nodes.cpp:177
clang::syntax::ExpressionStatement::getExpression
Expression * getExpression()
Definition: Nodes.cpp:325
clang::syntax::UnaryOperatorExpression::getOperand
Expression * getOperand()
Definition: Nodes.cpp:208
clang::syntax::CallArguments::getArguments
std::vector< Expression * > getArguments()
Definition: Nodes.cpp:132
clang::syntax::IfStatement::getElseKeyword
Leaf * getElseKeyword()
Definition: Nodes.cpp:266
clang::syntax::NodeRole::Operand
@ Operand
clang::syntax::ArraySubscript::getSize
Expression * getSize()
Definition: Nodes.cpp:402
clang::syntax::NodeRole::CaseValue
@ CaseValue
clang::syntax::ParametersAndQualifiers::getParameters
ParameterDeclarationList * getParameters()
Definition: Nodes.cpp:424
clang::syntax::NodeRole::Size
@ Size
clang::syntax::NodeRole::ListDelimiter
@ ListDelimiter
clang::syntax::NodeRole::Declarator
@ Declarator
clang::syntax::BinaryOperatorExpression::getLhs
Expression * getLhs()
Definition: Nodes.cpp:199
clang::syntax::StaticAssertDeclaration::getCondition
Expression * getCondition()
Definition: Nodes.cpp:347
clang::syntax::IfStatement::getIfKeyword
Leaf * getIfKeyword()
Definition: Nodes.cpp:256
clang::syntax::SimpleDeclaration::getDeclarators
std::vector< SimpleDeclarator * > getDeclarators()
FIXME: use custom iterator instead of 'vector'.
Definition: Nodes.cpp:357
clang::syntax::NodeRole::Message
@ Message
clang::syntax::NodeRole::OpenParen
@ OpenParen
An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.
clang::syntax::CallArguments::getArgumentsAndCommas
std::vector< List::ElementAndDelimiter< Expression > > getArgumentsAndCommas()
Definition: Nodes.cpp:142
Nodes.h
clang::syntax::TemplateDeclaration::getDeclaration
Declaration * getDeclaration()
Definition: Nodes.cpp:371
clang::syntax::SimpleDeclarator
A top-level declarator without parentheses.
Definition: Nodes.h:499
clang::syntax::NodeRole::Member
@ Member
clang::syntax::Leaf
A leaf node points to a single token inside the expanded token stream.
Definition: Tree.h:157
clang::syntax::NodeRole::Declarators
@ Declarators
clang::syntax::NodeRole::Arguments
@ Arguments
clang::syntax::CaseStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:241
clang::syntax::NodeRole::OperatorToken
@ OperatorToken
clang::syntax::NodeRole
NodeRole
A relation between a parent and child node, e.g.
Definition: Nodes.h:59
clang::syntax::ContinueStatement::getContinueKeyword
Leaf * getContinueKeyword()
Definition: Nodes.cpp:295
clang
Definition: CalledOnceCheck.h:17
clang::syntax::NodeKind
NodeKind
A kind of a syntax node, used for implementing casts.
Definition: Nodes.h:37
clang::syntax::ExplicitTemplateInstantiation::getExternKeyword
Leaf * getExternKeyword()
Definition: Nodes.cpp:381
clang::syntax::NodeRole::IntroducerKeyword
@ IntroducerKeyword
A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.
clang::syntax::ArraySubscript::getLbracket
Leaf * getLbracket()
Definition: Nodes.cpp:398
clang::syntax::CompoundStatement::getLbrace
Leaf * getLbrace()
Definition: Nodes.cpp:330
clang::syntax::ParametersAndQualifiers::getRparen
Leaf * getRparen()
Definition: Nodes.cpp:429
clang::syntax::NodeRole::BodyStatement
@ BodyStatement
An inner statement for those that have only a single child of kind statement, e.g.
clang::syntax::TrailingReturnType
Trailing return type after the parameter list, including the arrow token.
Definition: Nodes.h:532
clang::syntax::NodeRole::LiteralToken
@ LiteralToken
A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
clang::syntax::NodeRole::LeftHandSide
@ LeftHandSide
clang::syntax::DeclaratorList::getDeclaratorsAndCommas
std::vector< List::ElementAndDelimiter< syntax::SimpleDeclarator > > getDeclaratorsAndCommas()
Definition: Nodes.cpp:187
clang::syntax::TemplateDeclaration::getTemplateKeyword
Leaf * getTemplateKeyword()
Definition: Nodes.cpp:366
clang::syntax::ParenDeclarator::getRparen
Leaf * getRparen()
Definition: Nodes.cpp:394
clang::syntax::NestedNameSpecifier::getSpecifiers
std::vector< NameSpecifier * > getSpecifiers()
Definition: Nodes.cpp:110
clang::syntax::Declaration
A declaration that can appear at the top-level.
Definition: Nodes.h:359
clang::syntax::CaseStatement::getCaseKeyword
Leaf * getCaseKeyword()
Definition: Nodes.cpp:231
clang::syntax::NodeRole::Condition
@ Condition
clang::syntax::ParenDeclarator::getLparen
Leaf * getLparen()
Definition: Nodes.cpp:390
clang::syntax::SwitchStatement::getSwitchKeyword
Leaf * getSwitchKeyword()
Definition: Nodes.cpp:221
clang::syntax::ParameterDeclarationList
Models a parameter-declaration-list which appears within parameters-and-qualifiers.
Definition: Nodes.h:545
clang::syntax::NodeRole::Declaration
@ Declaration
clang::syntax::RangeBasedForStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:320
clang::syntax::StaticAssertDeclaration::getMessage
Expression * getMessage()
Definition: Nodes.cpp:352
clang::syntax::Statement
An abstract node for C++ statements, e.g.
Definition: Nodes.h:214
clang::syntax::DefaultStatement::getDefaultKeyword
Leaf * getDefaultKeyword()
Definition: Nodes.cpp:246
clang::syntax::SwitchStatement::getBody
Statement * getBody()
Definition: Nodes.cpp:226
clang::syntax::ArraySubscript::getRbracket
Leaf * getRbracket()
Definition: Nodes.cpp:406
clang::syntax::NodeRole::AccessToken
@ AccessToken
clang::syntax::NodeRole::TemplateKeyword
@ TemplateKeyword