AST.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. }
  201. m_lhs->dump(indent + 1);
  202. print_indent(indent + 1);
  203. VERIFY(op_string);
  204. outln("{}", op_string);
  205. m_rhs->dump(indent + 1);
  206. }
  207. void AssignmentExpression::dump(size_t indent) const
  208. {
  209. ASTNode::dump(indent);
  210. const char* op_string = nullptr;
  211. switch (m_op) {
  212. case AssignmentOp::Assignment:
  213. op_string = "=";
  214. break;
  215. case AssignmentOp::AdditionAssignment:
  216. op_string = "+=";
  217. break;
  218. case AssignmentOp::SubtractionAssignment:
  219. op_string = "-=";
  220. break;
  221. }
  222. m_lhs->dump(indent + 1);
  223. print_indent(indent + 1);
  224. VERIFY(op_string);
  225. outln("{}", op_string);
  226. m_rhs->dump(indent + 1);
  227. }
  228. void FunctionCall::dump(size_t indent) const
  229. {
  230. ASTNode::dump(indent);
  231. print_indent(indent);
  232. outln("{}", m_name->full_name());
  233. for (const auto& arg : m_arguments) {
  234. arg.dump(indent + 1);
  235. }
  236. }
  237. void StringLiteral::dump(size_t indent) const
  238. {
  239. ASTNode::dump(indent);
  240. print_indent(indent + 1);
  241. outln("{}", m_value);
  242. }
  243. void ReturnStatement::dump(size_t indent) const
  244. {
  245. ASTNode::dump(indent);
  246. if (m_value)
  247. m_value->dump(indent + 1);
  248. }
  249. void EnumDeclaration::dump(size_t indent) const
  250. {
  251. ASTNode::dump(indent);
  252. print_indent(indent);
  253. outln("{}", m_name);
  254. for (auto& entry : m_entries) {
  255. print_indent(indent + 1);
  256. outln("{}", entry);
  257. }
  258. }
  259. void StructOrClassDeclaration::dump(size_t indent) const
  260. {
  261. ASTNode::dump(indent);
  262. print_indent(indent);
  263. outln("{}", m_name);
  264. for (auto& member : m_members) {
  265. member.dump(indent + 1);
  266. }
  267. }
  268. void MemberDeclaration::dump(size_t indent) const
  269. {
  270. ASTNode::dump(indent);
  271. m_type->dump(indent + 1);
  272. print_indent(indent + 1);
  273. outln("{}", m_name);
  274. if (m_initial_value) {
  275. m_initial_value->dump(indent + 2);
  276. }
  277. }
  278. void UnaryExpression::dump(size_t indent) const
  279. {
  280. ASTNode::dump(indent);
  281. const char* op_string = nullptr;
  282. switch (m_op) {
  283. case UnaryOp::BitwiseNot:
  284. op_string = "~";
  285. break;
  286. case UnaryOp::Not:
  287. op_string = "!";
  288. break;
  289. case UnaryOp::Plus:
  290. op_string = "+";
  291. break;
  292. case UnaryOp::Minus:
  293. op_string = "-";
  294. break;
  295. case UnaryOp::PlusPlus:
  296. op_string = "++";
  297. break;
  298. case UnaryOp::Address:
  299. op_string = "&";
  300. break;
  301. default:
  302. op_string = "<invalid>";
  303. }
  304. VERIFY(op_string);
  305. print_indent(indent + 1);
  306. outln("{}", op_string);
  307. m_lhs->dump(indent + 1);
  308. }
  309. void BooleanLiteral::dump(size_t indent) const
  310. {
  311. ASTNode::dump(indent);
  312. print_indent(indent + 1);
  313. outln("{}", m_value ? "true" : "false");
  314. }
  315. void Pointer::dump(size_t indent) const
  316. {
  317. ASTNode::dump(indent);
  318. if (!m_pointee.is_null()) {
  319. m_pointee->dump(indent + 1);
  320. }
  321. }
  322. void MemberExpression::dump(size_t indent) const
  323. {
  324. ASTNode::dump(indent);
  325. m_object->dump(indent + 1);
  326. m_property->dump(indent + 1);
  327. }
  328. void BlockStatement::dump(size_t indent) const
  329. {
  330. ASTNode::dump(indent);
  331. for (auto& statement : m_statements) {
  332. statement.dump(indent + 1);
  333. }
  334. }
  335. void ForStatement::dump(size_t indent) const
  336. {
  337. ASTNode::dump(indent);
  338. if (m_init)
  339. m_init->dump(indent + 1);
  340. if (m_test)
  341. m_test->dump(indent + 1);
  342. if (m_update)
  343. m_update->dump(indent + 1);
  344. if (m_body)
  345. m_body->dump(indent + 1);
  346. }
  347. NonnullRefPtrVector<Declaration> Statement::declarations() const
  348. {
  349. if (is_declaration()) {
  350. NonnullRefPtrVector<Declaration> vec;
  351. const auto& decl = static_cast<const Declaration&>(*this);
  352. vec.empend(const_cast<Declaration&>(decl));
  353. return vec;
  354. }
  355. return {};
  356. }
  357. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  358. {
  359. NonnullRefPtrVector<Declaration> declarations;
  360. if (m_init)
  361. declarations.append(m_init->declarations());
  362. if (m_body)
  363. declarations.append(m_body->declarations());
  364. return declarations;
  365. }
  366. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  367. {
  368. NonnullRefPtrVector<Declaration> declarations;
  369. for (auto& statement : m_statements) {
  370. declarations.append(statement.declarations());
  371. }
  372. return declarations;
  373. }
  374. void IfStatement::dump(size_t indent) const
  375. {
  376. ASTNode::dump(indent);
  377. if (m_predicate) {
  378. print_indent(indent + 1);
  379. outln("Predicate:");
  380. m_predicate->dump(indent + 1);
  381. }
  382. if (m_then) {
  383. print_indent(indent + 1);
  384. outln("Then:");
  385. m_then->dump(indent + 1);
  386. }
  387. if (m_else) {
  388. print_indent(indent + 1);
  389. outln("Else:");
  390. m_else->dump(indent + 1);
  391. }
  392. }
  393. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  394. {
  395. NonnullRefPtrVector<Declaration> declarations;
  396. if (m_predicate)
  397. declarations.append(m_predicate->declarations());
  398. if (m_then)
  399. declarations.append(m_then->declarations());
  400. if (m_else)
  401. declarations.append(m_else->declarations());
  402. return declarations;
  403. }
  404. void NamespaceDeclaration::dump(size_t indent) const
  405. {
  406. ASTNode::dump(indent);
  407. print_indent(indent + 1);
  408. outln("{}", m_name);
  409. for (auto& decl : m_declarations)
  410. decl.dump(indent + 1);
  411. }
  412. void NullPointerLiteral::dump(size_t indent) const
  413. {
  414. ASTNode::dump(indent);
  415. }
  416. void TemplatizedType::dump(size_t indent) const
  417. {
  418. ASTNode::dump(indent);
  419. String qualifiers_string;
  420. if (!m_qualifiers.is_empty())
  421. qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
  422. print_indent(indent + 1);
  423. outln("{}{}", qualifiers_string, m_name);
  424. print_indent(indent + 1);
  425. outln("<");
  426. for (auto& arg : m_template_arguments) {
  427. arg.dump(indent + 1);
  428. }
  429. print_indent(indent + 1);
  430. outln(">");
  431. }
  432. void Name::dump(size_t indent) const
  433. {
  434. ASTNode::dump(indent);
  435. print_indent(indent);
  436. outln("{}", full_name());
  437. }
  438. String Name::full_name() const
  439. {
  440. StringBuilder builder;
  441. if (!m_scope.is_empty()) {
  442. for (auto& scope : m_scope) {
  443. builder.appendff("{}::", scope.m_name);
  444. }
  445. }
  446. return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->m_name);
  447. }
  448. void TemplatizedFunctionCall::dump(size_t indent) const
  449. {
  450. ASTNode::dump(indent);
  451. print_indent(indent);
  452. outln("{}", m_name->full_name());
  453. print_indent(indent + 1);
  454. outln("<");
  455. for (auto& arg : m_template_arguments) {
  456. arg.dump(indent + 1);
  457. }
  458. print_indent(indent + 1);
  459. outln(">");
  460. for (const auto& arg : m_arguments) {
  461. arg.dump(indent + 1);
  462. }
  463. }
  464. void CppCastExpression::dump(size_t indent) const
  465. {
  466. ASTNode::dump(indent);
  467. print_indent(indent);
  468. outln("{}", m_cast_type);
  469. print_indent(indent + 1);
  470. outln("<");
  471. if (m_type)
  472. m_type->dump(indent + 1);
  473. print_indent(indent + 1);
  474. outln(">");
  475. if (m_expression)
  476. m_expression->dump(indent + 1);
  477. }
  478. void SizeofExpression::dump(size_t indent) const
  479. {
  480. ASTNode::dump(indent);
  481. if(m_type)
  482. m_type->dump(indent+1);
  483. }
  484. }