AST.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. dbgprintf(" ");
  33. }
  34. void ASTNode::dump(size_t indent) const
  35. {
  36. print_indent(indent);
  37. dbgprintf("%s[%lu:%lu->%lu:%lu]\n", 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. dbgprintf("%s\n", m_name.to_string().characters());
  53. }
  54. print_indent(indent + 1);
  55. dbgprintf("(\n");
  56. for (const auto& arg : m_parameters) {
  57. arg.dump(indent + 1);
  58. }
  59. print_indent(indent + 1);
  60. dbgprintf(")\n");
  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. dbgprintf("%s\n", m_name.to_string().characters());
  78. }
  79. void Parameter::dump(size_t indent) const
  80. {
  81. ASTNode::dump(indent);
  82. if (!m_name.is_null()) {
  83. print_indent(indent);
  84. dbgprintf("%s\n", m_name.to_string().characters());
  85. }
  86. m_type->dump(indent + 1);
  87. // print_indent(indent);
  88. // dbgprintf("%s [%s]\n", m_name.is_null() ? "" : m_name.to_string().characters(), m_type->name().to_string().characters());
  89. }
  90. void FunctionDefinition::dump(size_t indent) const
  91. {
  92. ASTNode::dump(indent);
  93. print_indent(indent);
  94. dbgprintf("{\n");
  95. for (const auto& statement : m_statements) {
  96. statement.dump(indent + 1);
  97. }
  98. print_indent(indent);
  99. dbgprintf("}\n");
  100. }
  101. NonnullRefPtrVector<Declaration> FunctionDefinition::declarations() const
  102. {
  103. NonnullRefPtrVector<Declaration> declarations;
  104. for (auto& statement : m_statements) {
  105. declarations.append(statement.declarations());
  106. }
  107. return declarations;
  108. }
  109. void VariableDeclaration::dump(size_t indent) const
  110. {
  111. ASTNode::dump(indent);
  112. m_type->dump(indent + 1);
  113. print_indent(indent + 1);
  114. dbgprintf("%s\n", m_name.to_string().characters());
  115. if (m_initial_value)
  116. m_initial_value->dump(indent + 1);
  117. }
  118. void Identifier::dump(size_t indent) const
  119. {
  120. ASTNode::dump(indent);
  121. print_indent(indent);
  122. dbgprintf("%s\n", m_name.to_string().characters());
  123. }
  124. void NumericLiteral::dump(size_t indent) const
  125. {
  126. ASTNode::dump(indent);
  127. print_indent(indent);
  128. dbgprintf("%s\n", m_value.to_string().characters());
  129. }
  130. void BinaryExpression::dump(size_t indent) const
  131. {
  132. ASTNode::dump(indent);
  133. const char* op_string = nullptr;
  134. switch (m_op) {
  135. case BinaryOp::Addition:
  136. op_string = "+";
  137. break;
  138. case BinaryOp::Subtraction:
  139. op_string = "-";
  140. break;
  141. case BinaryOp::Multiplication:
  142. op_string = "*";
  143. break;
  144. case BinaryOp::Division:
  145. op_string = "/";
  146. break;
  147. case BinaryOp::Modulo:
  148. op_string = "%";
  149. break;
  150. case BinaryOp::GreaterThan:
  151. op_string = ">";
  152. break;
  153. case BinaryOp::GreaterThanEquals:
  154. op_string = ">=";
  155. break;
  156. case BinaryOp::LessThan:
  157. op_string = "<";
  158. break;
  159. case BinaryOp::LessThanEquals:
  160. op_string = "<=";
  161. break;
  162. case BinaryOp::BitwiseAnd:
  163. op_string = "&";
  164. break;
  165. case BinaryOp::BitwiseOr:
  166. op_string = "|";
  167. break;
  168. case BinaryOp::BitwiseXor:
  169. op_string = "^";
  170. break;
  171. case BinaryOp::LeftShift:
  172. op_string = "<<";
  173. break;
  174. case BinaryOp::RightShift:
  175. op_string = ">>";
  176. break;
  177. }
  178. m_lhs->dump(indent + 1);
  179. print_indent(indent + 1);
  180. ASSERT(op_string);
  181. dbgprintf("%s\n", op_string);
  182. m_rhs->dump(indent + 1);
  183. }
  184. void AssignmentExpression::dump(size_t indent) const
  185. {
  186. ASTNode::dump(indent);
  187. const char* op_string = nullptr;
  188. switch (m_op) {
  189. case AssignmentOp::Assignment:
  190. op_string = "=";
  191. break;
  192. case AssignmentOp::AdditionAssignment:
  193. op_string = "+=";
  194. break;
  195. case AssignmentOp::SubtractionAssignment:
  196. op_string = "-=";
  197. break;
  198. }
  199. m_lhs->dump(indent + 1);
  200. print_indent(indent + 1);
  201. ASSERT(op_string);
  202. dbgprintf("%s\n", op_string);
  203. m_rhs->dump(indent + 1);
  204. }
  205. void FunctionCall::dump(size_t indent) const
  206. {
  207. ASTNode::dump(indent);
  208. print_indent(indent);
  209. dbgprintf("%s\n", m_name.to_string().characters());
  210. for (const auto& arg : m_arguments) {
  211. arg.dump(indent + 1);
  212. }
  213. }
  214. void StringLiteral::dump(size_t indent) const
  215. {
  216. ASTNode::dump(indent);
  217. print_indent(indent + 1);
  218. dbgprintf("%s\n", m_value.to_string().characters());
  219. }
  220. void ReturnStatement::dump(size_t indent) const
  221. {
  222. ASTNode::dump(indent);
  223. m_value->dump(indent + 1);
  224. }
  225. void EnumDeclaration::dump(size_t indent) const
  226. {
  227. ASTNode::dump(indent);
  228. print_indent(indent);
  229. dbgprintf("%s\n", m_name.to_string().characters());
  230. for (auto& entry : m_entries) {
  231. print_indent(indent + 1);
  232. dbgprintf("%s\n", entry.to_string().characters());
  233. }
  234. }
  235. void StructOrClassDeclaration::dump(size_t indent) const
  236. {
  237. ASTNode::dump(indent);
  238. print_indent(indent);
  239. dbgprintf("%s\n", m_name.to_string().characters());
  240. for (auto& member : m_members) {
  241. member.dump(indent + 1);
  242. }
  243. }
  244. void MemberDeclaration::dump(size_t indent) const
  245. {
  246. ASTNode::dump(indent);
  247. m_type->dump(indent + 1);
  248. print_indent(indent + 1);
  249. dbgprintf("%s\n", m_name.to_string().characters());
  250. if (m_initial_value) {
  251. m_initial_value->dump(indent + 2);
  252. }
  253. }
  254. void UnaryExpression::dump(size_t indent) const
  255. {
  256. ASTNode::dump(indent);
  257. const char* op_string = nullptr;
  258. switch (m_op) {
  259. case UnaryOp::BitwiseNot:
  260. op_string = "~";
  261. break;
  262. case UnaryOp::Not:
  263. op_string = "!";
  264. break;
  265. case UnaryOp::Plus:
  266. op_string = "+";
  267. break;
  268. case UnaryOp::Minus:
  269. op_string = "-";
  270. break;
  271. case UnaryOp::PlusPlus:
  272. op_string = "++";
  273. break;
  274. default:
  275. op_string = "<invalid>";
  276. }
  277. ASSERT(op_string);
  278. print_indent(indent + 1);
  279. dbgprintf("%s\n", op_string);
  280. m_lhs->dump(indent + 1);
  281. }
  282. void BooleanLiteral::dump(size_t indent) const
  283. {
  284. ASTNode::dump(indent);
  285. print_indent(indent + 1);
  286. dbgprintf("%s\n", m_value ? "true" : "false");
  287. }
  288. void Pointer::dump(size_t indent) const
  289. {
  290. ASTNode::dump(indent);
  291. if (!m_pointee.is_null()) {
  292. m_pointee->dump(indent + 1);
  293. }
  294. }
  295. void MemberExpression::dump(size_t indent) const
  296. {
  297. ASTNode::dump(indent);
  298. m_object->dump(indent + 1);
  299. m_property->dump(indent + 1);
  300. }
  301. void BlockStatement::dump(size_t indent) const
  302. {
  303. ASTNode::dump(indent);
  304. for (auto& statement : m_statements) {
  305. statement.dump(indent + 1);
  306. }
  307. }
  308. void ForStatement::dump(size_t indent) const
  309. {
  310. ASTNode::dump(indent);
  311. if (m_init)
  312. m_init->dump(indent + 1);
  313. if (m_test)
  314. m_test->dump(indent + 1);
  315. if (m_update)
  316. m_update->dump(indent + 1);
  317. if (m_body)
  318. m_body->dump(indent + 1);
  319. }
  320. NonnullRefPtrVector<Declaration> Statement::declarations() const
  321. {
  322. if (is_declaration()) {
  323. NonnullRefPtrVector<Declaration> vec;
  324. const auto& decl = static_cast<const Declaration&>(*this);
  325. vec.empend(const_cast<Declaration&>(decl));
  326. return vec;
  327. }
  328. return {};
  329. }
  330. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  331. {
  332. auto declarations = m_init->declarations();
  333. declarations.append(m_body->declarations());
  334. return declarations;
  335. }
  336. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  337. {
  338. NonnullRefPtrVector<Declaration> declarations;
  339. for (auto& statement : m_statements) {
  340. declarations.append(statement.declarations());
  341. }
  342. return declarations;
  343. }
  344. }