AST.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "AST.h"
  27. namespace Cpp {
  28. static void print_indent(int indent)
  29. {
  30. for (int i = 0; i < indent * 2; ++i)
  31. out(" ");
  32. }
  33. void ASTNode::dump(size_t indent) const
  34. {
  35. print_indent(indent);
  36. outln("{}[{}:{}->{}:{}]", class_name(), start().line, start().column, end().line, end().column);
  37. }
  38. void TranslationUnit::dump(size_t indent) const
  39. {
  40. ASTNode::dump(indent);
  41. for (const auto& child : m_declarations) {
  42. child.dump(indent + 1);
  43. }
  44. }
  45. void FunctionDeclaration::dump(size_t indent) const
  46. {
  47. ASTNode::dump(indent);
  48. String qualifiers_string;
  49. if (!m_qualifiers.is_empty()) {
  50. print_indent(indent + 1);
  51. outln("[{}]", String::join(" ", m_qualifiers));
  52. }
  53. m_return_type->dump(indent + 1);
  54. if (!m_name.is_null()) {
  55. print_indent(indent + 1);
  56. outln("{}", m_name);
  57. }
  58. print_indent(indent + 1);
  59. outln("(");
  60. for (const auto& arg : m_parameters) {
  61. arg.dump(indent + 1);
  62. }
  63. print_indent(indent + 1);
  64. outln(")");
  65. if (!m_definition.is_null()) {
  66. m_definition->dump(indent + 1);
  67. }
  68. }
  69. NonnullRefPtrVector<Declaration> FunctionDeclaration::declarations() const
  70. {
  71. NonnullRefPtrVector<Declaration> declarations;
  72. for (auto& arg : m_parameters) {
  73. declarations.append(arg);
  74. }
  75. return declarations;
  76. }
  77. void Type::dump(size_t indent) const
  78. {
  79. ASTNode::dump(indent);
  80. print_indent(indent + 1);
  81. String qualifiers_string;
  82. if (!m_qualifiers.is_empty())
  83. qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
  84. outln("{}{}", qualifiers_string, m_name.is_null() ? "" : m_name->full_name());
  85. }
  86. void Parameter::dump(size_t indent) const
  87. {
  88. ASTNode::dump(indent);
  89. if (m_is_ellipsis) {
  90. print_indent(indent + 1);
  91. outln("...");
  92. }
  93. if (!m_name.is_null()) {
  94. print_indent(indent);
  95. outln("{}", m_name);
  96. }
  97. if (m_type)
  98. m_type->dump(indent + 1);
  99. }
  100. void FunctionDefinition::dump(size_t indent) const
  101. {
  102. ASTNode::dump(indent);
  103. print_indent(indent);
  104. outln("{{");
  105. for (const auto& statement : m_statements) {
  106. statement.dump(indent + 1);
  107. }
  108. print_indent(indent);
  109. outln("}}");
  110. }
  111. NonnullRefPtrVector<Declaration> FunctionDefinition::declarations() const
  112. {
  113. NonnullRefPtrVector<Declaration> declarations;
  114. for (auto& statement : m_statements) {
  115. declarations.append(statement.declarations());
  116. }
  117. return declarations;
  118. }
  119. void VariableDeclaration::dump(size_t indent) const
  120. {
  121. ASTNode::dump(indent);
  122. if (m_type)
  123. m_type->dump(indent + 1);
  124. print_indent(indent + 1);
  125. outln("{}", m_name);
  126. if (m_initial_value)
  127. m_initial_value->dump(indent + 1);
  128. }
  129. void Identifier::dump(size_t indent) const
  130. {
  131. ASTNode::dump(indent);
  132. print_indent(indent);
  133. outln("{}", m_name);
  134. }
  135. void NumericLiteral::dump(size_t indent) const
  136. {
  137. ASTNode::dump(indent);
  138. print_indent(indent);
  139. outln("{}", m_value);
  140. }
  141. void BinaryExpression::dump(size_t indent) const
  142. {
  143. ASTNode::dump(indent);
  144. const char* op_string = nullptr;
  145. switch (m_op) {
  146. case BinaryOp::Addition:
  147. op_string = "+";
  148. break;
  149. case BinaryOp::Subtraction:
  150. op_string = "-";
  151. break;
  152. case BinaryOp::Multiplication:
  153. op_string = "*";
  154. break;
  155. case BinaryOp::Division:
  156. op_string = "/";
  157. break;
  158. case BinaryOp::Modulo:
  159. op_string = "%";
  160. break;
  161. case BinaryOp::GreaterThan:
  162. op_string = ">";
  163. break;
  164. case BinaryOp::GreaterThanEquals:
  165. op_string = ">=";
  166. break;
  167. case BinaryOp::LessThan:
  168. op_string = "<";
  169. break;
  170. case BinaryOp::LessThanEquals:
  171. op_string = "<=";
  172. break;
  173. case BinaryOp::BitwiseAnd:
  174. op_string = "&";
  175. break;
  176. case BinaryOp::BitwiseOr:
  177. op_string = "|";
  178. break;
  179. case BinaryOp::BitwiseXor:
  180. op_string = "^";
  181. break;
  182. case BinaryOp::LeftShift:
  183. op_string = "<<";
  184. break;
  185. case BinaryOp::RightShift:
  186. op_string = ">>";
  187. break;
  188. case BinaryOp::EqualsEquals:
  189. op_string = "==";
  190. break;
  191. case BinaryOp::NotEqual:
  192. op_string = "!=";
  193. break;
  194. case BinaryOp::LogicalOr:
  195. op_string = "||";
  196. break;
  197. case BinaryOp::LogicalAnd:
  198. op_string = "&&";
  199. break;
  200. case BinaryOp::Arrow:
  201. op_string = "->";
  202. break;
  203. }
  204. m_lhs->dump(indent + 1);
  205. print_indent(indent + 1);
  206. VERIFY(op_string);
  207. outln("{}", op_string);
  208. m_rhs->dump(indent + 1);
  209. }
  210. void AssignmentExpression::dump(size_t indent) const
  211. {
  212. ASTNode::dump(indent);
  213. const char* op_string = nullptr;
  214. switch (m_op) {
  215. case AssignmentOp::Assignment:
  216. op_string = "=";
  217. break;
  218. case AssignmentOp::AdditionAssignment:
  219. op_string = "+=";
  220. break;
  221. case AssignmentOp::SubtractionAssignment:
  222. op_string = "-=";
  223. break;
  224. }
  225. m_lhs->dump(indent + 1);
  226. print_indent(indent + 1);
  227. VERIFY(op_string);
  228. outln("{}", op_string);
  229. m_rhs->dump(indent + 1);
  230. }
  231. void FunctionCall::dump(size_t indent) const
  232. {
  233. ASTNode::dump(indent);
  234. print_indent(indent);
  235. outln("{}", m_name->full_name());
  236. for (const auto& arg : m_arguments) {
  237. arg.dump(indent + 1);
  238. }
  239. }
  240. void StringLiteral::dump(size_t indent) const
  241. {
  242. ASTNode::dump(indent);
  243. print_indent(indent + 1);
  244. outln("{}", m_value);
  245. }
  246. void ReturnStatement::dump(size_t indent) const
  247. {
  248. ASTNode::dump(indent);
  249. if (m_value)
  250. m_value->dump(indent + 1);
  251. }
  252. void EnumDeclaration::dump(size_t indent) const
  253. {
  254. ASTNode::dump(indent);
  255. print_indent(indent);
  256. outln("{}", m_name);
  257. for (auto& entry : m_entries) {
  258. print_indent(indent + 1);
  259. outln("{}", entry);
  260. }
  261. }
  262. void StructOrClassDeclaration::dump(size_t indent) const
  263. {
  264. ASTNode::dump(indent);
  265. print_indent(indent);
  266. outln("{}", m_name);
  267. for (auto& member : m_members) {
  268. member.dump(indent + 1);
  269. }
  270. }
  271. void MemberDeclaration::dump(size_t indent) const
  272. {
  273. ASTNode::dump(indent);
  274. m_type->dump(indent + 1);
  275. print_indent(indent + 1);
  276. outln("{}", m_name);
  277. if (m_initial_value) {
  278. m_initial_value->dump(indent + 2);
  279. }
  280. }
  281. void UnaryExpression::dump(size_t indent) const
  282. {
  283. ASTNode::dump(indent);
  284. const char* op_string = nullptr;
  285. switch (m_op) {
  286. case UnaryOp::BitwiseNot:
  287. op_string = "~";
  288. break;
  289. case UnaryOp::Not:
  290. op_string = "!";
  291. break;
  292. case UnaryOp::Plus:
  293. op_string = "+";
  294. break;
  295. case UnaryOp::Minus:
  296. op_string = "-";
  297. break;
  298. case UnaryOp::PlusPlus:
  299. op_string = "++";
  300. break;
  301. case UnaryOp::Address:
  302. op_string = "&";
  303. break;
  304. default:
  305. op_string = "<invalid>";
  306. }
  307. VERIFY(op_string);
  308. print_indent(indent + 1);
  309. outln("{}", op_string);
  310. m_lhs->dump(indent + 1);
  311. }
  312. void BooleanLiteral::dump(size_t indent) const
  313. {
  314. ASTNode::dump(indent);
  315. print_indent(indent + 1);
  316. outln("{}", m_value ? "true" : "false");
  317. }
  318. void Pointer::dump(size_t indent) const
  319. {
  320. ASTNode::dump(indent);
  321. if (!m_pointee.is_null()) {
  322. m_pointee->dump(indent + 1);
  323. }
  324. }
  325. void MemberExpression::dump(size_t indent) const
  326. {
  327. ASTNode::dump(indent);
  328. m_object->dump(indent + 1);
  329. m_property->dump(indent + 1);
  330. }
  331. void BlockStatement::dump(size_t indent) const
  332. {
  333. ASTNode::dump(indent);
  334. for (auto& statement : m_statements) {
  335. statement.dump(indent + 1);
  336. }
  337. }
  338. void ForStatement::dump(size_t indent) const
  339. {
  340. ASTNode::dump(indent);
  341. if (m_init)
  342. m_init->dump(indent + 1);
  343. if (m_test)
  344. m_test->dump(indent + 1);
  345. if (m_update)
  346. m_update->dump(indent + 1);
  347. if (m_body)
  348. m_body->dump(indent + 1);
  349. }
  350. NonnullRefPtrVector<Declaration> Statement::declarations() const
  351. {
  352. if (is_declaration()) {
  353. NonnullRefPtrVector<Declaration> vec;
  354. const auto& decl = static_cast<const Declaration&>(*this);
  355. vec.empend(const_cast<Declaration&>(decl));
  356. return vec;
  357. }
  358. return {};
  359. }
  360. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  361. {
  362. NonnullRefPtrVector<Declaration> declarations;
  363. if (m_init)
  364. declarations.append(m_init->declarations());
  365. if (m_body)
  366. declarations.append(m_body->declarations());
  367. return declarations;
  368. }
  369. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  370. {
  371. NonnullRefPtrVector<Declaration> declarations;
  372. for (auto& statement : m_statements) {
  373. declarations.append(statement.declarations());
  374. }
  375. return declarations;
  376. }
  377. void IfStatement::dump(size_t indent) const
  378. {
  379. ASTNode::dump(indent);
  380. if (m_predicate) {
  381. print_indent(indent + 1);
  382. outln("Predicate:");
  383. m_predicate->dump(indent + 1);
  384. }
  385. if (m_then) {
  386. print_indent(indent + 1);
  387. outln("Then:");
  388. m_then->dump(indent + 1);
  389. }
  390. if (m_else) {
  391. print_indent(indent + 1);
  392. outln("Else:");
  393. m_else->dump(indent + 1);
  394. }
  395. }
  396. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  397. {
  398. NonnullRefPtrVector<Declaration> declarations;
  399. if (m_predicate)
  400. declarations.append(m_predicate->declarations());
  401. if (m_then)
  402. declarations.append(m_then->declarations());
  403. if (m_else)
  404. declarations.append(m_else->declarations());
  405. return declarations;
  406. }
  407. void NamespaceDeclaration::dump(size_t indent) const
  408. {
  409. ASTNode::dump(indent);
  410. print_indent(indent + 1);
  411. outln("{}", m_name);
  412. for (auto& decl : m_declarations)
  413. decl.dump(indent + 1);
  414. }
  415. void NullPointerLiteral::dump(size_t indent) const
  416. {
  417. ASTNode::dump(indent);
  418. }
  419. void TemplatizedType::dump(size_t indent) const
  420. {
  421. ASTNode::dump(indent);
  422. String qualifiers_string;
  423. if (!m_qualifiers.is_empty())
  424. qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
  425. print_indent(indent + 1);
  426. outln("{}{}", qualifiers_string, m_name);
  427. print_indent(indent + 1);
  428. outln("<");
  429. for (auto& arg : m_template_arguments) {
  430. arg.dump(indent + 1);
  431. }
  432. print_indent(indent + 1);
  433. outln(">");
  434. }
  435. void Name::dump(size_t indent) const
  436. {
  437. ASTNode::dump(indent);
  438. print_indent(indent);
  439. outln("{}", full_name());
  440. }
  441. String Name::full_name() const
  442. {
  443. StringBuilder builder;
  444. if (!m_scope.is_empty()) {
  445. for (auto& scope : m_scope) {
  446. builder.appendff("{}::", scope.m_name);
  447. }
  448. }
  449. return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->m_name);
  450. }
  451. void TemplatizedFunctionCall::dump(size_t indent) const
  452. {
  453. ASTNode::dump(indent);
  454. print_indent(indent);
  455. outln("{}", m_name->full_name());
  456. print_indent(indent + 1);
  457. outln("<");
  458. for (auto& arg : m_template_arguments) {
  459. arg.dump(indent + 1);
  460. }
  461. print_indent(indent + 1);
  462. outln(">");
  463. for (const auto& arg : m_arguments) {
  464. arg.dump(indent + 1);
  465. }
  466. }
  467. void CppCastExpression::dump(size_t indent) const
  468. {
  469. ASTNode::dump(indent);
  470. print_indent(indent);
  471. outln("{}", m_cast_type);
  472. print_indent(indent + 1);
  473. outln("<");
  474. if (m_type)
  475. m_type->dump(indent + 1);
  476. print_indent(indent + 1);
  477. outln(">");
  478. if (m_expression)
  479. m_expression->dump(indent + 1);
  480. }
  481. void SizeofExpression::dump(size_t indent) const
  482. {
  483. ASTNode::dump(indent);
  484. if (m_type)
  485. m_type->dump(indent + 1);
  486. }
  487. void BracedInitList::dump(size_t indent) const
  488. {
  489. ASTNode::dump(indent);
  490. for (auto& exp : m_expressions) {
  491. exp.dump(indent + 1);
  492. }
  493. }
  494. void CStyleCastExpression::dump(size_t indent) const
  495. {
  496. ASTNode::dump(indent);
  497. if (m_type)
  498. m_type->dump(indent + 1);
  499. if (m_expression)
  500. m_expression->dump(indent + 1);
  501. }
  502. }