AST.cpp 16 KB

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