AST.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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.extend(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 NamedType::to_string() const
  66. {
  67. String qualifiers_string;
  68. if (!qualifiers().is_empty())
  69. qualifiers_string = String::formatted("[{}] ", String::join(" ", qualifiers()));
  70. String name;
  71. if (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. String Reference::to_string() const
  87. {
  88. if (!m_referenced_type)
  89. return {};
  90. StringBuilder builder;
  91. builder.append(m_referenced_type->to_string());
  92. if (m_kind == Kind::Lvalue)
  93. builder.append("&");
  94. else
  95. builder.append("&&");
  96. return builder.to_string();
  97. }
  98. String FunctionType::to_string() const
  99. {
  100. StringBuilder builder;
  101. builder.append(m_return_type->to_string());
  102. builder.append("(");
  103. bool first = true;
  104. for (auto& parameter : m_parameters) {
  105. if (first)
  106. first = false;
  107. else
  108. builder.append(", ");
  109. builder.append(parameter.type()->to_string());
  110. if (!parameter.name().is_empty()) {
  111. builder.append(" ");
  112. builder.append(parameter.name());
  113. }
  114. }
  115. builder.append(")");
  116. return builder.to_string();
  117. }
  118. void Parameter::dump(FILE* output, size_t indent) const
  119. {
  120. ASTNode::dump(output, indent);
  121. if (m_is_ellipsis) {
  122. print_indent(output, indent + 1);
  123. outln(output, "...");
  124. }
  125. if (!m_name.is_null()) {
  126. print_indent(output, indent);
  127. outln(output, "{}", m_name);
  128. }
  129. if (m_type)
  130. m_type->dump(output, indent + 1);
  131. }
  132. void FunctionDefinition::dump(FILE* output, size_t indent) const
  133. {
  134. ASTNode::dump(output, indent);
  135. print_indent(output, indent);
  136. outln(output, "{{");
  137. for (const auto& statement : m_statements) {
  138. statement.dump(output, indent + 1);
  139. }
  140. print_indent(output, indent);
  141. outln(output, "}}");
  142. }
  143. NonnullRefPtrVector<Declaration> FunctionDefinition::declarations() const
  144. {
  145. NonnullRefPtrVector<Declaration> declarations;
  146. for (auto& statement : m_statements) {
  147. declarations.extend(statement.declarations());
  148. }
  149. return declarations;
  150. }
  151. void VariableDeclaration::dump(FILE* output, size_t indent) const
  152. {
  153. ASTNode::dump(output, indent);
  154. if (m_type)
  155. m_type->dump(output, indent + 1);
  156. print_indent(output, indent + 1);
  157. outln(output, "{}", m_name);
  158. if (m_initial_value)
  159. m_initial_value->dump(output, indent + 1);
  160. }
  161. void Identifier::dump(FILE* output, size_t indent) const
  162. {
  163. ASTNode::dump(output, indent);
  164. print_indent(output, indent);
  165. outln(output, "{}", m_name);
  166. }
  167. void NumericLiteral::dump(FILE* output, size_t indent) const
  168. {
  169. ASTNode::dump(output, indent);
  170. print_indent(output, indent);
  171. outln(output, "{}", m_value);
  172. }
  173. void BinaryExpression::dump(FILE* output, size_t indent) const
  174. {
  175. ASTNode::dump(output, indent);
  176. const char* op_string = nullptr;
  177. switch (m_op) {
  178. case BinaryOp::Addition:
  179. op_string = "+";
  180. break;
  181. case BinaryOp::Subtraction:
  182. op_string = "-";
  183. break;
  184. case BinaryOp::Multiplication:
  185. op_string = "*";
  186. break;
  187. case BinaryOp::Division:
  188. op_string = "/";
  189. break;
  190. case BinaryOp::Modulo:
  191. op_string = "%";
  192. break;
  193. case BinaryOp::GreaterThan:
  194. op_string = ">";
  195. break;
  196. case BinaryOp::GreaterThanEquals:
  197. op_string = ">=";
  198. break;
  199. case BinaryOp::LessThan:
  200. op_string = "<";
  201. break;
  202. case BinaryOp::LessThanEquals:
  203. op_string = "<=";
  204. break;
  205. case BinaryOp::BitwiseAnd:
  206. op_string = "&";
  207. break;
  208. case BinaryOp::BitwiseOr:
  209. op_string = "|";
  210. break;
  211. case BinaryOp::BitwiseXor:
  212. op_string = "^";
  213. break;
  214. case BinaryOp::LeftShift:
  215. op_string = "<<";
  216. break;
  217. case BinaryOp::RightShift:
  218. op_string = ">>";
  219. break;
  220. case BinaryOp::EqualsEquals:
  221. op_string = "==";
  222. break;
  223. case BinaryOp::NotEqual:
  224. op_string = "!=";
  225. break;
  226. case BinaryOp::LogicalOr:
  227. op_string = "||";
  228. break;
  229. case BinaryOp::LogicalAnd:
  230. op_string = "&&";
  231. break;
  232. case BinaryOp::Arrow:
  233. op_string = "->";
  234. break;
  235. }
  236. m_lhs->dump(output, indent + 1);
  237. print_indent(output, indent + 1);
  238. VERIFY(op_string);
  239. outln(output, "{}", op_string);
  240. m_rhs->dump(output, indent + 1);
  241. }
  242. void AssignmentExpression::dump(FILE* output, size_t indent) const
  243. {
  244. ASTNode::dump(output, indent);
  245. const char* op_string = nullptr;
  246. switch (m_op) {
  247. case AssignmentOp::Assignment:
  248. op_string = "=";
  249. break;
  250. case AssignmentOp::AdditionAssignment:
  251. op_string = "+=";
  252. break;
  253. case AssignmentOp::SubtractionAssignment:
  254. op_string = "-=";
  255. break;
  256. }
  257. m_lhs->dump(output, indent + 1);
  258. print_indent(output, indent + 1);
  259. VERIFY(op_string);
  260. outln(output, "{}", op_string);
  261. m_rhs->dump(output, indent + 1);
  262. }
  263. void FunctionCall::dump(FILE* output, size_t indent) const
  264. {
  265. ASTNode::dump(output, indent);
  266. m_callee->dump(output, indent + 1);
  267. for (const auto& arg : m_arguments) {
  268. arg.dump(output, indent + 1);
  269. }
  270. }
  271. void StringLiteral::dump(FILE* output, size_t indent) const
  272. {
  273. ASTNode::dump(output, indent);
  274. print_indent(output, indent + 1);
  275. outln(output, "{}", m_value);
  276. }
  277. void ReturnStatement::dump(FILE* output, size_t indent) const
  278. {
  279. ASTNode::dump(output, indent);
  280. if (m_value)
  281. m_value->dump(output, indent + 1);
  282. }
  283. void EnumDeclaration::dump(FILE* output, size_t indent) const
  284. {
  285. ASTNode::dump(output, indent);
  286. print_indent(output, indent);
  287. outln(output, "{}", m_name);
  288. for (auto& entry : m_entries) {
  289. print_indent(output, indent + 1);
  290. outln(output, "{}", entry.name);
  291. if (entry.value)
  292. entry.value->dump(output, indent + 2);
  293. }
  294. }
  295. void StructOrClassDeclaration::dump(FILE* output, size_t indent) const
  296. {
  297. ASTNode::dump(output, indent);
  298. print_indent(output, indent);
  299. outln(output, "{}", m_name);
  300. for (auto& member : m_members) {
  301. member.dump(output, indent + 1);
  302. }
  303. }
  304. NonnullRefPtrVector<Declaration> StructOrClassDeclaration::declarations() const
  305. {
  306. NonnullRefPtrVector<Declaration> declarations;
  307. for (auto& member : m_members)
  308. declarations.append(member);
  309. return declarations;
  310. }
  311. void UnaryExpression::dump(FILE* output, size_t indent) const
  312. {
  313. ASTNode::dump(output, indent);
  314. const char* op_string = nullptr;
  315. switch (m_op) {
  316. case UnaryOp::BitwiseNot:
  317. op_string = "~";
  318. break;
  319. case UnaryOp::Not:
  320. op_string = "!";
  321. break;
  322. case UnaryOp::Plus:
  323. op_string = "+";
  324. break;
  325. case UnaryOp::Minus:
  326. op_string = "-";
  327. break;
  328. case UnaryOp::PlusPlus:
  329. op_string = "++";
  330. break;
  331. case UnaryOp::Address:
  332. op_string = "&";
  333. break;
  334. default:
  335. op_string = "<invalid>";
  336. }
  337. VERIFY(op_string);
  338. print_indent(output, indent + 1);
  339. outln(output, "{}", op_string);
  340. m_lhs->dump(output, indent + 1);
  341. }
  342. void BooleanLiteral::dump(FILE* output, size_t indent) const
  343. {
  344. ASTNode::dump(output, indent);
  345. print_indent(output, indent + 1);
  346. outln(output, "{}", m_value ? "true" : "false");
  347. }
  348. void Pointer::dump(FILE* output, size_t indent) const
  349. {
  350. ASTNode::dump(output, indent);
  351. if (!m_pointee.is_null()) {
  352. m_pointee->dump(output, indent + 1);
  353. }
  354. }
  355. void Reference::dump(FILE* output, size_t indent) const
  356. {
  357. ASTNode::dump(output, indent);
  358. print_indent(output, indent + 1);
  359. outln(output, "{}", m_kind == Kind::Lvalue ? "&" : "&&");
  360. if (!m_referenced_type.is_null()) {
  361. m_referenced_type->dump(output, indent + 1);
  362. }
  363. }
  364. void FunctionType::dump(FILE* output, size_t indent) const
  365. {
  366. ASTNode::dump(output, indent);
  367. if (m_return_type)
  368. m_return_type->dump(output, indent + 1);
  369. print_indent(output, indent + 1);
  370. outln("(");
  371. for (auto& parameter : m_parameters)
  372. parameter.dump(output, indent + 2);
  373. print_indent(output, indent + 1);
  374. outln(")");
  375. }
  376. void MemberExpression::dump(FILE* output, size_t indent) const
  377. {
  378. ASTNode::dump(output, indent);
  379. m_object->dump(output, indent + 1);
  380. m_property->dump(output, indent + 1);
  381. }
  382. void BlockStatement::dump(FILE* output, size_t indent) const
  383. {
  384. ASTNode::dump(output, indent);
  385. for (auto& statement : m_statements) {
  386. statement.dump(output, indent + 1);
  387. }
  388. }
  389. void ForStatement::dump(FILE* output, size_t indent) const
  390. {
  391. ASTNode::dump(output, indent);
  392. if (m_init)
  393. m_init->dump(output, indent + 1);
  394. if (m_test)
  395. m_test->dump(output, indent + 1);
  396. if (m_update)
  397. m_update->dump(output, indent + 1);
  398. if (m_body)
  399. m_body->dump(output, indent + 1);
  400. }
  401. NonnullRefPtrVector<Declaration> Statement::declarations() const
  402. {
  403. if (is_declaration()) {
  404. NonnullRefPtrVector<Declaration> vec;
  405. const auto& decl = static_cast<const Declaration&>(*this);
  406. vec.empend(const_cast<Declaration&>(decl));
  407. return vec;
  408. }
  409. return {};
  410. }
  411. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  412. {
  413. NonnullRefPtrVector<Declaration> declarations;
  414. if (m_init)
  415. declarations.extend(m_init->declarations());
  416. if (m_body)
  417. declarations.extend(m_body->declarations());
  418. return declarations;
  419. }
  420. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  421. {
  422. NonnullRefPtrVector<Declaration> declarations;
  423. for (auto& statement : m_statements) {
  424. declarations.extend(statement.declarations());
  425. }
  426. return declarations;
  427. }
  428. void IfStatement::dump(FILE* output, size_t indent) const
  429. {
  430. ASTNode::dump(output, indent);
  431. if (m_predicate) {
  432. print_indent(output, indent + 1);
  433. outln(output, "Predicate:");
  434. m_predicate->dump(output, indent + 1);
  435. }
  436. if (m_then) {
  437. print_indent(output, indent + 1);
  438. outln(output, "Then:");
  439. m_then->dump(output, indent + 1);
  440. }
  441. if (m_else) {
  442. print_indent(output, indent + 1);
  443. outln(output, "Else:");
  444. m_else->dump(output, indent + 1);
  445. }
  446. }
  447. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  448. {
  449. NonnullRefPtrVector<Declaration> declarations;
  450. if (m_predicate)
  451. declarations.extend(m_predicate->declarations());
  452. if (m_then)
  453. declarations.extend(m_then->declarations());
  454. if (m_else)
  455. declarations.extend(m_else->declarations());
  456. return declarations;
  457. }
  458. void NamespaceDeclaration::dump(FILE* output, size_t indent) const
  459. {
  460. ASTNode::dump(output, indent);
  461. print_indent(output, indent + 1);
  462. outln(output, "{}", m_name);
  463. for (auto& decl : m_declarations)
  464. decl.dump(output, indent + 1);
  465. }
  466. void NullPointerLiteral::dump(FILE* output, size_t indent) const
  467. {
  468. ASTNode::dump(output, indent);
  469. }
  470. void Name::dump(FILE* output, size_t indent) const
  471. {
  472. ASTNode::dump(output, indent);
  473. print_indent(output, indent);
  474. outln(output, "{}", full_name());
  475. }
  476. String Name::full_name() const
  477. {
  478. StringBuilder builder;
  479. if (!m_scope.is_empty()) {
  480. for (auto& scope : m_scope) {
  481. builder.appendff("{}::", scope.name());
  482. }
  483. }
  484. return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->name());
  485. }
  486. String TemplatizedName::full_name() const
  487. {
  488. StringBuilder name;
  489. name.append(Name::full_name());
  490. name.append('<');
  491. for (auto& type : m_template_arguments) {
  492. name.append(type.to_string());
  493. }
  494. name.append('>');
  495. return name.to_string();
  496. }
  497. void CppCastExpression::dump(FILE* output, size_t indent) const
  498. {
  499. ASTNode::dump(output, indent);
  500. print_indent(output, indent);
  501. outln(output, "{}", m_cast_type);
  502. print_indent(output, indent + 1);
  503. outln(output, "<");
  504. if (m_type)
  505. m_type->dump(output, indent + 1);
  506. print_indent(output, indent + 1);
  507. outln(output, ">");
  508. if (m_expression)
  509. m_expression->dump(output, indent + 1);
  510. }
  511. void SizeofExpression::dump(FILE* output, size_t indent) const
  512. {
  513. ASTNode::dump(output, indent);
  514. if (m_type)
  515. m_type->dump(output, indent + 1);
  516. }
  517. void BracedInitList::dump(FILE* output, size_t indent) const
  518. {
  519. ASTNode::dump(output, indent);
  520. for (auto& exp : m_expressions) {
  521. exp.dump(output, indent + 1);
  522. }
  523. }
  524. void CStyleCastExpression::dump(FILE* output, size_t indent) const
  525. {
  526. ASTNode::dump(output, indent);
  527. if (m_type)
  528. m_type->dump(output, indent + 1);
  529. if (m_expression)
  530. m_expression->dump(output, indent + 1);
  531. }
  532. void Constructor::dump(FILE* output, size_t indent) const
  533. {
  534. print_indent(output, indent);
  535. outln(output, "C'tor");
  536. print_indent(output, indent + 1);
  537. outln(output, "(");
  538. for (const auto& arg : parameters()) {
  539. arg.dump(output, indent + 1);
  540. }
  541. print_indent(output, indent + 1);
  542. outln(output, ")");
  543. if (definition()) {
  544. definition()->dump(output, indent + 1);
  545. }
  546. }
  547. void Destructor::dump(FILE* output, size_t indent) const
  548. {
  549. print_indent(output, indent);
  550. outln(output, "D'tor");
  551. print_indent(output, indent + 1);
  552. outln(output, "(");
  553. for (const auto& arg : parameters()) {
  554. arg.dump(output, indent + 1);
  555. }
  556. print_indent(output, indent + 1);
  557. outln(output, ")");
  558. if (definition()) {
  559. definition()->dump(output, indent + 1);
  560. }
  561. }
  562. }