AST.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "AST.h"
  7. namespace Cpp {
  8. static void print_indent(FILE* output, int indent)
  9. {
  10. for (int i = 0; i < indent * 2; ++i)
  11. out(output, " ");
  12. }
  13. void ASTNode::dump(FILE* output, size_t indent) const
  14. {
  15. print_indent(output, indent);
  16. outln(output, "{}[{}:{}->{}:{}]", class_name(), start().line, start().column, end().line, end().column);
  17. }
  18. void TranslationUnit::dump(FILE* output, size_t indent) const
  19. {
  20. ASTNode::dump(output, indent);
  21. for (const auto& child : m_declarations) {
  22. child.dump(output, indent + 1);
  23. }
  24. }
  25. void FunctionDeclaration::dump(FILE* output, size_t indent) const
  26. {
  27. ASTNode::dump(output, indent);
  28. String qualifiers_string;
  29. if (!m_qualifiers.is_empty()) {
  30. print_indent(output, indent + 1);
  31. outln(output, "[{}]", String::join(" ", m_qualifiers));
  32. }
  33. m_return_type->dump(output, indent + 1);
  34. if (!m_name.is_null()) {
  35. print_indent(output, indent + 1);
  36. outln(output, "{}", m_name);
  37. }
  38. print_indent(output, indent + 1);
  39. outln(output, "(");
  40. for (const auto& arg : m_parameters) {
  41. arg.dump(output, indent + 1);
  42. }
  43. print_indent(output, indent + 1);
  44. outln(output, ")");
  45. if (!m_definition.is_null()) {
  46. m_definition->dump(output, indent + 1);
  47. }
  48. }
  49. NonnullRefPtrVector<Declaration> FunctionDeclaration::declarations() const
  50. {
  51. NonnullRefPtrVector<Declaration> declarations;
  52. for (auto& arg : m_parameters) {
  53. declarations.append(arg);
  54. }
  55. if (m_definition)
  56. declarations.append(m_definition->declarations());
  57. return declarations;
  58. }
  59. void Type::dump(FILE* output, size_t indent) const
  60. {
  61. ASTNode::dump(output, indent);
  62. print_indent(output, indent + 1);
  63. outln(output, "{}", to_string());
  64. }
  65. String Type::to_string() const
  66. {
  67. String qualifiers_string;
  68. if (!m_qualifiers.is_empty())
  69. qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
  70. String name;
  71. if (m_is_auto)
  72. name = "auto";
  73. else
  74. name = m_name.is_null() ? "" : m_name->full_name();
  75. return String::formatted("{}{}", qualifiers_string, name);
  76. }
  77. String Pointer::to_string() const
  78. {
  79. if (!m_pointee)
  80. return {};
  81. StringBuilder builder;
  82. builder.append(m_pointee->to_string());
  83. builder.append("*");
  84. return builder.to_string();
  85. }
  86. void Parameter::dump(FILE* output, size_t indent) const
  87. {
  88. ASTNode::dump(output, indent);
  89. if (m_is_ellipsis) {
  90. print_indent(output, indent + 1);
  91. outln(output, "...");
  92. }
  93. if (!m_name.is_null()) {
  94. print_indent(output, indent);
  95. outln(output, "{}", m_name);
  96. }
  97. if (m_type)
  98. m_type->dump(output, indent + 1);
  99. }
  100. void FunctionDefinition::dump(FILE* output, size_t indent) const
  101. {
  102. ASTNode::dump(output, indent);
  103. print_indent(output, indent);
  104. outln(output, "{{");
  105. for (const auto& statement : m_statements) {
  106. statement.dump(output, indent + 1);
  107. }
  108. print_indent(output, indent);
  109. outln(output, "}}");
  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(FILE* output, size_t indent) const
  120. {
  121. ASTNode::dump(output, indent);
  122. if (m_type)
  123. m_type->dump(output, indent + 1);
  124. print_indent(output, indent + 1);
  125. outln(output, "{}", m_name);
  126. if (m_initial_value)
  127. m_initial_value->dump(output, indent + 1);
  128. }
  129. void Identifier::dump(FILE* output, size_t indent) const
  130. {
  131. ASTNode::dump(output, indent);
  132. print_indent(output, indent);
  133. outln(output, "{}", m_name);
  134. }
  135. void NumericLiteral::dump(FILE* output, size_t indent) const
  136. {
  137. ASTNode::dump(output, indent);
  138. print_indent(output, indent);
  139. outln(output, "{}", m_value);
  140. }
  141. void BinaryExpression::dump(FILE* output, size_t indent) const
  142. {
  143. ASTNode::dump(output, 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(output, indent + 1);
  205. print_indent(output, indent + 1);
  206. VERIFY(op_string);
  207. outln(output, "{}", op_string);
  208. m_rhs->dump(output, indent + 1);
  209. }
  210. void AssignmentExpression::dump(FILE* output, size_t indent) const
  211. {
  212. ASTNode::dump(output, 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(output, indent + 1);
  226. print_indent(output, indent + 1);
  227. VERIFY(op_string);
  228. outln(output, "{}", op_string);
  229. m_rhs->dump(output, indent + 1);
  230. }
  231. void FunctionCall::dump(FILE* output, size_t indent) const
  232. {
  233. ASTNode::dump(output, indent);
  234. m_callee->dump(output, indent + 1);
  235. for (const auto& arg : m_arguments) {
  236. arg.dump(output, indent + 1);
  237. }
  238. }
  239. void StringLiteral::dump(FILE* output, size_t indent) const
  240. {
  241. ASTNode::dump(output, indent);
  242. print_indent(output, indent + 1);
  243. outln(output, "{}", m_value);
  244. }
  245. void ReturnStatement::dump(FILE* output, size_t indent) const
  246. {
  247. ASTNode::dump(output, indent);
  248. if (m_value)
  249. m_value->dump(output, indent + 1);
  250. }
  251. void EnumDeclaration::dump(FILE* output, size_t indent) const
  252. {
  253. ASTNode::dump(output, indent);
  254. print_indent(output, indent);
  255. outln(output, "{}", m_name);
  256. for (auto& entry : m_entries) {
  257. print_indent(output, indent + 1);
  258. outln(output, "{}", entry);
  259. }
  260. }
  261. void StructOrClassDeclaration::dump(FILE* output, size_t indent) const
  262. {
  263. ASTNode::dump(output, indent);
  264. print_indent(output, indent);
  265. outln(output, "{}", m_name);
  266. for (auto& member : m_members) {
  267. member.dump(output, indent + 1);
  268. }
  269. }
  270. NonnullRefPtrVector<Declaration> StructOrClassDeclaration::declarations() const
  271. {
  272. NonnullRefPtrVector<Declaration> declarations;
  273. for (auto& member : m_members)
  274. declarations.append(member);
  275. return declarations;
  276. }
  277. void MemberDeclaration::dump(FILE* output, size_t indent) const
  278. {
  279. ASTNode::dump(output, indent);
  280. m_type->dump(output, indent + 1);
  281. print_indent(output, indent + 1);
  282. outln(output, "{}", m_name);
  283. if (m_initial_value) {
  284. m_initial_value->dump(output, indent + 2);
  285. }
  286. }
  287. void UnaryExpression::dump(FILE* output, size_t indent) const
  288. {
  289. ASTNode::dump(output, indent);
  290. const char* op_string = nullptr;
  291. switch (m_op) {
  292. case UnaryOp::BitwiseNot:
  293. op_string = "~";
  294. break;
  295. case UnaryOp::Not:
  296. op_string = "!";
  297. break;
  298. case UnaryOp::Plus:
  299. op_string = "+";
  300. break;
  301. case UnaryOp::Minus:
  302. op_string = "-";
  303. break;
  304. case UnaryOp::PlusPlus:
  305. op_string = "++";
  306. break;
  307. case UnaryOp::Address:
  308. op_string = "&";
  309. break;
  310. default:
  311. op_string = "<invalid>";
  312. }
  313. VERIFY(op_string);
  314. print_indent(output, indent + 1);
  315. outln(output, "{}", op_string);
  316. m_lhs->dump(output, indent + 1);
  317. }
  318. void BooleanLiteral::dump(FILE* output, size_t indent) const
  319. {
  320. ASTNode::dump(output, indent);
  321. print_indent(output, indent + 1);
  322. outln(output, "{}", m_value ? "true" : "false");
  323. }
  324. void Pointer::dump(FILE* output, size_t indent) const
  325. {
  326. ASTNode::dump(output, indent);
  327. if (!m_pointee.is_null()) {
  328. m_pointee->dump(output, indent + 1);
  329. }
  330. }
  331. void MemberExpression::dump(FILE* output, size_t indent) const
  332. {
  333. ASTNode::dump(output, indent);
  334. m_object->dump(output, indent + 1);
  335. m_property->dump(output, indent + 1);
  336. }
  337. void BlockStatement::dump(FILE* output, size_t indent) const
  338. {
  339. ASTNode::dump(output, indent);
  340. for (auto& statement : m_statements) {
  341. statement.dump(output, indent + 1);
  342. }
  343. }
  344. void ForStatement::dump(FILE* output, size_t indent) const
  345. {
  346. ASTNode::dump(output, indent);
  347. if (m_init)
  348. m_init->dump(output, indent + 1);
  349. if (m_test)
  350. m_test->dump(output, indent + 1);
  351. if (m_update)
  352. m_update->dump(output, indent + 1);
  353. if (m_body)
  354. m_body->dump(output, indent + 1);
  355. }
  356. NonnullRefPtrVector<Declaration> Statement::declarations() const
  357. {
  358. if (is_declaration()) {
  359. NonnullRefPtrVector<Declaration> vec;
  360. const auto& decl = static_cast<const Declaration&>(*this);
  361. vec.empend(const_cast<Declaration&>(decl));
  362. return vec;
  363. }
  364. return {};
  365. }
  366. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  367. {
  368. NonnullRefPtrVector<Declaration> declarations;
  369. if (m_init)
  370. declarations.append(m_init->declarations());
  371. if (m_body)
  372. declarations.append(m_body->declarations());
  373. return declarations;
  374. }
  375. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  376. {
  377. NonnullRefPtrVector<Declaration> declarations;
  378. for (auto& statement : m_statements) {
  379. declarations.append(statement.declarations());
  380. }
  381. return declarations;
  382. }
  383. void IfStatement::dump(FILE* output, size_t indent) const
  384. {
  385. ASTNode::dump(output, indent);
  386. if (m_predicate) {
  387. print_indent(output, indent + 1);
  388. outln(output, "Predicate:");
  389. m_predicate->dump(output, indent + 1);
  390. }
  391. if (m_then) {
  392. print_indent(output, indent + 1);
  393. outln(output, "Then:");
  394. m_then->dump(output, indent + 1);
  395. }
  396. if (m_else) {
  397. print_indent(output, indent + 1);
  398. outln(output, "Else:");
  399. m_else->dump(output, indent + 1);
  400. }
  401. }
  402. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  403. {
  404. NonnullRefPtrVector<Declaration> declarations;
  405. if (m_predicate)
  406. declarations.append(m_predicate->declarations());
  407. if (m_then)
  408. declarations.append(m_then->declarations());
  409. if (m_else)
  410. declarations.append(m_else->declarations());
  411. return declarations;
  412. }
  413. void NamespaceDeclaration::dump(FILE* output, size_t indent) const
  414. {
  415. ASTNode::dump(output, indent);
  416. print_indent(output, indent + 1);
  417. outln(output, "{}", m_name);
  418. for (auto& decl : m_declarations)
  419. decl.dump(output, indent + 1);
  420. }
  421. void NullPointerLiteral::dump(FILE* output, size_t indent) const
  422. {
  423. ASTNode::dump(output, indent);
  424. }
  425. void Name::dump(FILE* output, size_t indent) const
  426. {
  427. ASTNode::dump(output, indent);
  428. print_indent(output, indent);
  429. outln(output, "{}", full_name());
  430. }
  431. String Name::full_name() const
  432. {
  433. StringBuilder builder;
  434. if (!m_scope.is_empty()) {
  435. for (auto& scope : m_scope) {
  436. builder.appendff("{}::", scope.m_name);
  437. }
  438. }
  439. return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->m_name);
  440. }
  441. String TemplatizedName::full_name() const
  442. {
  443. StringBuilder name;
  444. name.append(Name::full_name());
  445. name.append('<');
  446. for (auto& type : m_template_arguments) {
  447. name.append(type.to_string());
  448. }
  449. name.append('>');
  450. return name.to_string();
  451. }
  452. void CppCastExpression::dump(FILE* output, size_t indent) const
  453. {
  454. ASTNode::dump(output, indent);
  455. print_indent(output, indent);
  456. outln(output, "{}", m_cast_type);
  457. print_indent(output, indent + 1);
  458. outln(output, "<");
  459. if (m_type)
  460. m_type->dump(output, indent + 1);
  461. print_indent(output, indent + 1);
  462. outln(output, ">");
  463. if (m_expression)
  464. m_expression->dump(output, indent + 1);
  465. }
  466. void SizeofExpression::dump(FILE* output, size_t indent) const
  467. {
  468. ASTNode::dump(output, indent);
  469. if (m_type)
  470. m_type->dump(output, indent + 1);
  471. }
  472. void BracedInitList::dump(FILE* output, size_t indent) const
  473. {
  474. ASTNode::dump(output, indent);
  475. for (auto& exp : m_expressions) {
  476. exp.dump(output, indent + 1);
  477. }
  478. }
  479. void CStyleCastExpression::dump(FILE* output, size_t indent) const
  480. {
  481. ASTNode::dump(output, indent);
  482. if (m_type)
  483. m_type->dump(output, indent + 1);
  484. if (m_expression)
  485. m_expression->dump(output, indent + 1);
  486. }
  487. }