AST.cpp 10 KB

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