1351280Sdim//===- Nodes.cpp ----------------------------------------------*- C++ -*-=====//
2351280Sdim//
3351280Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4351280Sdim// See https://llvm.org/LICENSE.txt for license information.
5351280Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6351280Sdim//
7351280Sdim//===----------------------------------------------------------------------===//
8351280Sdim#include "clang/Tooling/Syntax/Nodes.h"
9351280Sdim#include "clang/Basic/TokenKinds.h"
10351280Sdim
11351280Sdimusing namespace clang;
12351280Sdim
13351280Sdimllvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeKind K) {
14351280Sdim  switch (K) {
15351280Sdim  case NodeKind::Leaf:
16351280Sdim    return OS << "Leaf";
17351280Sdim  case NodeKind::TranslationUnit:
18351280Sdim    return OS << "TranslationUnit";
19360784Sdim  case NodeKind::UnknownExpression:
20360784Sdim    return OS << "UnknownExpression";
21360784Sdim  case NodeKind::UnknownStatement:
22360784Sdim    return OS << "UnknownStatement";
23360784Sdim  case NodeKind::DeclarationStatement:
24360784Sdim    return OS << "DeclarationStatement";
25360784Sdim  case NodeKind::EmptyStatement:
26360784Sdim    return OS << "EmptyStatement";
27360784Sdim  case NodeKind::SwitchStatement:
28360784Sdim    return OS << "SwitchStatement";
29360784Sdim  case NodeKind::CaseStatement:
30360784Sdim    return OS << "CaseStatement";
31360784Sdim  case NodeKind::DefaultStatement:
32360784Sdim    return OS << "DefaultStatement";
33360784Sdim  case NodeKind::IfStatement:
34360784Sdim    return OS << "IfStatement";
35360784Sdim  case NodeKind::ForStatement:
36360784Sdim    return OS << "ForStatement";
37360784Sdim  case NodeKind::WhileStatement:
38360784Sdim    return OS << "WhileStatement";
39360784Sdim  case NodeKind::ContinueStatement:
40360784Sdim    return OS << "ContinueStatement";
41360784Sdim  case NodeKind::BreakStatement:
42360784Sdim    return OS << "BreakStatement";
43360784Sdim  case NodeKind::ReturnStatement:
44360784Sdim    return OS << "ReturnStatement";
45360784Sdim  case NodeKind::RangeBasedForStatement:
46360784Sdim    return OS << "RangeBasedForStatement";
47360784Sdim  case NodeKind::ExpressionStatement:
48360784Sdim    return OS << "ExpressionStatement";
49351280Sdim  case NodeKind::CompoundStatement:
50351280Sdim    return OS << "CompoundStatement";
51360784Sdim  case NodeKind::UnknownDeclaration:
52360784Sdim    return OS << "UnknownDeclaration";
53360784Sdim  case NodeKind::EmptyDeclaration:
54360784Sdim    return OS << "EmptyDeclaration";
55360784Sdim  case NodeKind::StaticAssertDeclaration:
56360784Sdim    return OS << "StaticAssertDeclaration";
57360784Sdim  case NodeKind::LinkageSpecificationDeclaration:
58360784Sdim    return OS << "LinkageSpecificationDeclaration";
59360784Sdim  case NodeKind::SimpleDeclaration:
60360784Sdim    return OS << "SimpleDeclaration";
61360784Sdim  case NodeKind::NamespaceDefinition:
62360784Sdim    return OS << "NamespaceDefinition";
63360784Sdim  case NodeKind::NamespaceAliasDefinition:
64360784Sdim    return OS << "NamespaceAliasDefinition";
65360784Sdim  case NodeKind::UsingNamespaceDirective:
66360784Sdim    return OS << "UsingNamespaceDirective";
67360784Sdim  case NodeKind::UsingDeclaration:
68360784Sdim    return OS << "UsingDeclaration";
69360784Sdim  case NodeKind::TypeAliasDeclaration:
70360784Sdim    return OS << "TypeAliasDeclaration";
71351280Sdim  }
72351280Sdim  llvm_unreachable("unknown node kind");
73351280Sdim}
74351280Sdim
75360784Sdimllvm::raw_ostream &syntax::operator<<(llvm::raw_ostream &OS, NodeRole R) {
76360784Sdim  switch (R) {
77360784Sdim  case syntax::NodeRole::Detached:
78360784Sdim    return OS << "Detached";
79360784Sdim  case syntax::NodeRole::Unknown:
80360784Sdim    return OS << "Unknown";
81360784Sdim  case syntax::NodeRole::OpenParen:
82360784Sdim    return OS << "OpenParen";
83360784Sdim  case syntax::NodeRole::CloseParen:
84360784Sdim    return OS << "CloseParen";
85360784Sdim  case syntax::NodeRole::IntroducerKeyword:
86360784Sdim    return OS << "IntroducerKeyword";
87360784Sdim  case syntax::NodeRole::BodyStatement:
88360784Sdim    return OS << "BodyStatement";
89360784Sdim  case syntax::NodeRole::CaseStatement_value:
90360784Sdim    return OS << "CaseStatement_value";
91360784Sdim  case syntax::NodeRole::IfStatement_thenStatement:
92360784Sdim    return OS << "IfStatement_thenStatement";
93360784Sdim  case syntax::NodeRole::IfStatement_elseKeyword:
94360784Sdim    return OS << "IfStatement_elseKeyword";
95360784Sdim  case syntax::NodeRole::IfStatement_elseStatement:
96360784Sdim    return OS << "IfStatement_elseStatement";
97360784Sdim  case syntax::NodeRole::ReturnStatement_value:
98360784Sdim    return OS << "ReturnStatement_value";
99360784Sdim  case syntax::NodeRole::ExpressionStatement_expression:
100360784Sdim    return OS << "ExpressionStatement_expression";
101360784Sdim  case syntax::NodeRole::CompoundStatement_statement:
102360784Sdim    return OS << "CompoundStatement_statement";
103360784Sdim  case syntax::NodeRole::StaticAssertDeclaration_condition:
104360784Sdim    return OS << "StaticAssertDeclaration_condition";
105360784Sdim  case syntax::NodeRole::StaticAssertDeclaration_message:
106360784Sdim    return OS << "StaticAssertDeclaration_message";
107360784Sdim  }
108360784Sdim  llvm_unreachable("invalid role");
109360784Sdim}
110360784Sdim
111360784Sdimsyntax::Leaf *syntax::SwitchStatement::switchKeyword() {
112360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
113360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
114360784Sdim}
115360784Sdim
116360784Sdimsyntax::Statement *syntax::SwitchStatement::body() {
117360784Sdim  return llvm::cast_or_null<syntax::Statement>(
118360784Sdim      findChild(syntax::NodeRole::BodyStatement));
119360784Sdim}
120360784Sdim
121360784Sdimsyntax::Leaf *syntax::CaseStatement::caseKeyword() {
122360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
123360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
124360784Sdim}
125360784Sdim
126360784Sdimsyntax::Expression *syntax::CaseStatement::value() {
127360784Sdim  return llvm::cast_or_null<syntax::Expression>(
128360784Sdim      findChild(syntax::NodeRole::CaseStatement_value));
129360784Sdim}
130360784Sdim
131360784Sdimsyntax::Statement *syntax::CaseStatement::body() {
132360784Sdim  return llvm::cast_or_null<syntax::Statement>(
133360784Sdim      findChild(syntax::NodeRole::BodyStatement));
134360784Sdim}
135360784Sdim
136360784Sdimsyntax::Leaf *syntax::DefaultStatement::defaultKeyword() {
137360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
138360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
139360784Sdim}
140360784Sdim
141360784Sdimsyntax::Statement *syntax::DefaultStatement::body() {
142360784Sdim  return llvm::cast_or_null<syntax::Statement>(
143360784Sdim      findChild(syntax::NodeRole::BodyStatement));
144360784Sdim}
145360784Sdim
146360784Sdimsyntax::Leaf *syntax::IfStatement::ifKeyword() {
147360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
148360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
149360784Sdim}
150360784Sdim
151360784Sdimsyntax::Statement *syntax::IfStatement::thenStatement() {
152360784Sdim  return llvm::cast_or_null<syntax::Statement>(
153360784Sdim      findChild(syntax::NodeRole::IfStatement_thenStatement));
154360784Sdim}
155360784Sdim
156360784Sdimsyntax::Leaf *syntax::IfStatement::elseKeyword() {
157360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
158360784Sdim      findChild(syntax::NodeRole::IfStatement_elseKeyword));
159360784Sdim}
160360784Sdim
161360784Sdimsyntax::Statement *syntax::IfStatement::elseStatement() {
162360784Sdim  return llvm::cast_or_null<syntax::Statement>(
163360784Sdim      findChild(syntax::NodeRole::IfStatement_elseStatement));
164360784Sdim}
165360784Sdim
166360784Sdimsyntax::Leaf *syntax::ForStatement::forKeyword() {
167360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
168360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
169360784Sdim}
170360784Sdim
171360784Sdimsyntax::Statement *syntax::ForStatement::body() {
172360784Sdim  return llvm::cast_or_null<syntax::Statement>(
173360784Sdim      findChild(syntax::NodeRole::BodyStatement));
174360784Sdim}
175360784Sdim
176360784Sdimsyntax::Leaf *syntax::WhileStatement::whileKeyword() {
177360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
178360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
179360784Sdim}
180360784Sdim
181360784Sdimsyntax::Statement *syntax::WhileStatement::body() {
182360784Sdim  return llvm::cast_or_null<syntax::Statement>(
183360784Sdim      findChild(syntax::NodeRole::BodyStatement));
184360784Sdim}
185360784Sdim
186360784Sdimsyntax::Leaf *syntax::ContinueStatement::continueKeyword() {
187360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
188360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
189360784Sdim}
190360784Sdim
191360784Sdimsyntax::Leaf *syntax::BreakStatement::breakKeyword() {
192360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
193360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
194360784Sdim}
195360784Sdim
196360784Sdimsyntax::Leaf *syntax::ReturnStatement::returnKeyword() {
197360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
198360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
199360784Sdim}
200360784Sdim
201360784Sdimsyntax::Expression *syntax::ReturnStatement::value() {
202360784Sdim  return llvm::cast_or_null<syntax::Expression>(
203360784Sdim      findChild(syntax::NodeRole::ReturnStatement_value));
204360784Sdim}
205360784Sdim
206360784Sdimsyntax::Leaf *syntax::RangeBasedForStatement::forKeyword() {
207360784Sdim  return llvm::cast_or_null<syntax::Leaf>(
208360784Sdim      findChild(syntax::NodeRole::IntroducerKeyword));
209360784Sdim}
210360784Sdim
211360784Sdimsyntax::Statement *syntax::RangeBasedForStatement::body() {
212360784Sdim  return llvm::cast_or_null<syntax::Statement>(
213360784Sdim      findChild(syntax::NodeRole::BodyStatement));
214360784Sdim}
215360784Sdim
216360784Sdimsyntax::Expression *syntax::ExpressionStatement::expression() {
217360784Sdim  return llvm::cast_or_null<syntax::Expression>(
218360784Sdim      findChild(syntax::NodeRole::ExpressionStatement_expression));
219360784Sdim}
220360784Sdim
221351280Sdimsyntax::Leaf *syntax::CompoundStatement::lbrace() {
222351280Sdim  return llvm::cast_or_null<syntax::Leaf>(
223360784Sdim      findChild(syntax::NodeRole::OpenParen));
224351280Sdim}
225351280Sdim
226360784Sdimstd::vector<syntax::Statement *> syntax::CompoundStatement::statements() {
227360784Sdim  std::vector<syntax::Statement *> Children;
228360784Sdim  for (auto *C = firstChild(); C; C = C->nextSibling()) {
229360784Sdim    if (C->role() == syntax::NodeRole::CompoundStatement_statement)
230360784Sdim      Children.push_back(llvm::cast<syntax::Statement>(C));
231360784Sdim  }
232360784Sdim  return Children;
233360784Sdim}
234360784Sdim
235351280Sdimsyntax::Leaf *syntax::CompoundStatement::rbrace() {
236351280Sdim  return llvm::cast_or_null<syntax::Leaf>(
237360784Sdim      findChild(syntax::NodeRole::CloseParen));
238351280Sdim}
239360784Sdim
240360784Sdimsyntax::Expression *syntax::StaticAssertDeclaration::condition() {
241360784Sdim  return llvm::cast_or_null<syntax::Expression>(
242360784Sdim      findChild(syntax::NodeRole::StaticAssertDeclaration_condition));
243360784Sdim}
244360784Sdim
245360784Sdimsyntax::Expression *syntax::StaticAssertDeclaration::message() {
246360784Sdim  return llvm::cast_or_null<syntax::Expression>(
247360784Sdim      findChild(syntax::NodeRole::StaticAssertDeclaration_message));
248360784Sdim}
249