AST.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #include "AK/LogStream.h"
  28. namespace Cpp {
  29. static void print_indent(int indent)
  30. {
  31. for (int i = 0; i < indent * 2; ++i)
  32. out(" ");
  33. }
  34. void ASTNode::dump(size_t indent) const
  35. {
  36. print_indent(indent);
  37. outln("{}[{}:{}->{}:{}]", class_name(), start().line, start().column, end().line, end().column);
  38. }
  39. void TranslationUnit::dump(size_t indent) const
  40. {
  41. ASTNode::dump(indent);
  42. for (const auto& child : m_children) {
  43. child.dump(indent + 1);
  44. }
  45. }
  46. void FunctionDeclaration::dump(size_t indent) const
  47. {
  48. ASTNode::dump(indent);
  49. m_return_type->dump(indent + 1);
  50. if (!m_name.is_null()) {
  51. print_indent(indent + 1);
  52. outln("{}", m_name);
  53. }
  54. print_indent(indent + 1);
  55. outln("(");
  56. for (const auto& arg : m_parameters) {
  57. arg.dump(indent + 1);
  58. }
  59. print_indent(indent + 1);
  60. outln(")");
  61. if (!m_definition.is_null()) {
  62. m_definition->dump(indent + 1);
  63. }
  64. }
  65. NonnullRefPtrVector<Declaration> FunctionDeclaration::declarations() const
  66. {
  67. NonnullRefPtrVector<Declaration> declarations;
  68. for (auto& arg : m_parameters) {
  69. declarations.append(arg);
  70. }
  71. return declarations;
  72. }
  73. void Type::dump(size_t indent) const
  74. {
  75. ASTNode::dump(indent);
  76. print_indent(indent + 1);
  77. String qualifiers_string;
  78. if(!m_qualifiers.is_empty())
  79. qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
  80. outln("{}{}", qualifiers_string, m_name);
  81. }
  82. void Parameter::dump(size_t indent) const
  83. {
  84. ASTNode::dump(indent);
  85. if (!m_name.is_null()) {
  86. print_indent(indent);
  87. outln("{}", m_name);
  88. }
  89. m_type->dump(indent + 1);
  90. }
  91. void FunctionDefinition::dump(size_t indent) const
  92. {
  93. ASTNode::dump(indent);
  94. print_indent(indent);
  95. outln("{{");
  96. for (const auto& statement : m_statements) {
  97. statement.dump(indent + 1);
  98. }
  99. print_indent(indent);
  100. outln("}}");
  101. }
  102. NonnullRefPtrVector<Declaration> FunctionDefinition::declarations() const
  103. {
  104. NonnullRefPtrVector<Declaration> declarations;
  105. for (auto& statement : m_statements) {
  106. declarations.append(statement.declarations());
  107. }
  108. return declarations;
  109. }
  110. void VariableDeclaration::dump(size_t indent) const
  111. {
  112. ASTNode::dump(indent);
  113. m_type->dump(indent + 1);
  114. print_indent(indent + 1);
  115. outln("{}", m_name);
  116. if (m_initial_value)
  117. m_initial_value->dump(indent + 1);
  118. }
  119. void Identifier::dump(size_t indent) const
  120. {
  121. ASTNode::dump(indent);
  122. print_indent(indent);
  123. outln("{}", m_name);
  124. }
  125. void NumericLiteral::dump(size_t indent) const
  126. {
  127. ASTNode::dump(indent);
  128. print_indent(indent);
  129. outln("{}", m_value);
  130. }
  131. void BinaryExpression::dump(size_t indent) const
  132. {
  133. ASTNode::dump(indent);
  134. const char* op_string = nullptr;
  135. switch (m_op) {
  136. case BinaryOp::Addition:
  137. op_string = "+";
  138. break;
  139. case BinaryOp::Subtraction:
  140. op_string = "-";
  141. break;
  142. case BinaryOp::Multiplication:
  143. op_string = "*";
  144. break;
  145. case BinaryOp::Division:
  146. op_string = "/";
  147. break;
  148. case BinaryOp::Modulo:
  149. op_string = "%";
  150. break;
  151. case BinaryOp::GreaterThan:
  152. op_string = ">";
  153. break;
  154. case BinaryOp::GreaterThanEquals:
  155. op_string = ">=";
  156. break;
  157. case BinaryOp::LessThan:
  158. op_string = "<";
  159. break;
  160. case BinaryOp::LessThanEquals:
  161. op_string = "<=";
  162. break;
  163. case BinaryOp::BitwiseAnd:
  164. op_string = "&";
  165. break;
  166. case BinaryOp::BitwiseOr:
  167. op_string = "|";
  168. break;
  169. case BinaryOp::BitwiseXor:
  170. op_string = "^";
  171. break;
  172. case BinaryOp::LeftShift:
  173. op_string = "<<";
  174. break;
  175. case BinaryOp::RightShift:
  176. op_string = ">>";
  177. break;
  178. }
  179. m_lhs->dump(indent + 1);
  180. print_indent(indent + 1);
  181. VERIFY(op_string);
  182. outln("{}", op_string);
  183. m_rhs->dump(indent + 1);
  184. }
  185. void AssignmentExpression::dump(size_t indent) const
  186. {
  187. ASTNode::dump(indent);
  188. const char* op_string = nullptr;
  189. switch (m_op) {
  190. case AssignmentOp::Assignment:
  191. op_string = "=";
  192. break;
  193. case AssignmentOp::AdditionAssignment:
  194. op_string = "+=";
  195. break;
  196. case AssignmentOp::SubtractionAssignment:
  197. op_string = "-=";
  198. break;
  199. }
  200. m_lhs->dump(indent + 1);
  201. print_indent(indent + 1);
  202. VERIFY(op_string);
  203. outln("{}", op_string);
  204. m_rhs->dump(indent + 1);
  205. }
  206. void FunctionCall::dump(size_t indent) const
  207. {
  208. ASTNode::dump(indent);
  209. print_indent(indent);
  210. outln("{}", m_name);
  211. for (const auto& arg : m_arguments) {
  212. arg.dump(indent + 1);
  213. }
  214. }
  215. void StringLiteral::dump(size_t indent) const
  216. {
  217. ASTNode::dump(indent);
  218. print_indent(indent + 1);
  219. outln("{}", m_value);
  220. }
  221. void ReturnStatement::dump(size_t indent) const
  222. {
  223. ASTNode::dump(indent);
  224. m_value->dump(indent + 1);
  225. }
  226. void EnumDeclaration::dump(size_t indent) const
  227. {
  228. ASTNode::dump(indent);
  229. print_indent(indent);
  230. outln("{}", m_name);
  231. for (auto& entry : m_entries) {
  232. print_indent(indent + 1);
  233. outln("{}", entry);
  234. }
  235. }
  236. void StructOrClassDeclaration::dump(size_t indent) const
  237. {
  238. ASTNode::dump(indent);
  239. print_indent(indent);
  240. outln("{}", m_name);
  241. for (auto& member : m_members) {
  242. member.dump(indent + 1);
  243. }
  244. }
  245. void MemberDeclaration::dump(size_t indent) const
  246. {
  247. ASTNode::dump(indent);
  248. m_type->dump(indent + 1);
  249. print_indent(indent + 1);
  250. outln("{}", m_name);
  251. if (m_initial_value) {
  252. m_initial_value->dump(indent + 2);
  253. }
  254. }
  255. void UnaryExpression::dump(size_t indent) const
  256. {
  257. ASTNode::dump(indent);
  258. const char* op_string = nullptr;
  259. switch (m_op) {
  260. case UnaryOp::BitwiseNot:
  261. op_string = "~";
  262. break;
  263. case UnaryOp::Not:
  264. op_string = "!";
  265. break;
  266. case UnaryOp::Plus:
  267. op_string = "+";
  268. break;
  269. case UnaryOp::Minus:
  270. op_string = "-";
  271. break;
  272. case UnaryOp::PlusPlus:
  273. op_string = "++";
  274. break;
  275. default:
  276. op_string = "<invalid>";
  277. }
  278. VERIFY(op_string);
  279. print_indent(indent + 1);
  280. outln("{}", op_string);
  281. m_lhs->dump(indent + 1);
  282. }
  283. void BooleanLiteral::dump(size_t indent) const
  284. {
  285. ASTNode::dump(indent);
  286. print_indent(indent + 1);
  287. outln("{}", m_value ? "true" : "false");
  288. }
  289. void Pointer::dump(size_t indent) const
  290. {
  291. ASTNode::dump(indent);
  292. if (!m_pointee.is_null()) {
  293. m_pointee->dump(indent + 1);
  294. }
  295. }
  296. void MemberExpression::dump(size_t indent) const
  297. {
  298. ASTNode::dump(indent);
  299. m_object->dump(indent + 1);
  300. m_property->dump(indent + 1);
  301. }
  302. void BlockStatement::dump(size_t indent) const
  303. {
  304. ASTNode::dump(indent);
  305. for (auto& statement : m_statements) {
  306. statement.dump(indent + 1);
  307. }
  308. }
  309. void ForStatement::dump(size_t indent) const
  310. {
  311. ASTNode::dump(indent);
  312. if (m_init)
  313. m_init->dump(indent + 1);
  314. if (m_test)
  315. m_test->dump(indent + 1);
  316. if (m_update)
  317. m_update->dump(indent + 1);
  318. if (m_body)
  319. m_body->dump(indent + 1);
  320. }
  321. NonnullRefPtrVector<Declaration> Statement::declarations() const
  322. {
  323. if (is_declaration()) {
  324. NonnullRefPtrVector<Declaration> vec;
  325. const auto& decl = static_cast<const Declaration&>(*this);
  326. vec.empend(const_cast<Declaration&>(decl));
  327. return vec;
  328. }
  329. return {};
  330. }
  331. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  332. {
  333. auto declarations = m_init->declarations();
  334. declarations.append(m_body->declarations());
  335. return declarations;
  336. }
  337. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  338. {
  339. NonnullRefPtrVector<Declaration> declarations;
  340. for (auto& statement : m_statements) {
  341. declarations.append(statement.declarations());
  342. }
  343. return declarations;
  344. }
  345. void IfStatement::dump(size_t indent) const
  346. {
  347. ASTNode::dump(indent);
  348. if (m_predicate) {
  349. print_indent(indent + 1);
  350. outln("Predicate:");
  351. m_predicate->dump(indent + 1);
  352. }
  353. if (m_then) {
  354. print_indent(indent + 1);
  355. outln("Then:");
  356. m_then->dump(indent + 1);
  357. }
  358. if (m_else) {
  359. print_indent(indent + 1);
  360. outln("Else:");
  361. m_else->dump(indent + 1);
  362. }
  363. }
  364. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  365. {
  366. NonnullRefPtrVector<Declaration> declarations;
  367. declarations.append(m_predicate->declarations());
  368. declarations.append(m_then->declarations());
  369. declarations.append(m_else->declarations());
  370. return declarations;
  371. }
  372. }