AST.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 (auto const& 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. ByteString qualifiers_string;
  29. if (!m_qualifiers.is_empty()) {
  30. print_indent(output, indent + 1);
  31. outln(output, "[{}]", ByteString::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 (auto const& 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. Vector<NonnullRefPtr<Declaration const>> FunctionDeclaration::declarations() const
  50. {
  51. Vector<NonnullRefPtr<Declaration const>> 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_byte_string());
  64. }
  65. ByteString NamedType::to_byte_string() const
  66. {
  67. ByteString qualifiers_string;
  68. if (!qualifiers().is_empty())
  69. qualifiers_string = ByteString::formatted("[{}] ", ByteString::join(' ', qualifiers()));
  70. ByteString name;
  71. if (is_auto())
  72. name = "auto";
  73. else
  74. name = m_name.is_null() ? ""sv : m_name->full_name();
  75. return ByteString::formatted("{}{}", qualifiers_string, name);
  76. }
  77. ByteString Pointer::to_byte_string() const
  78. {
  79. if (!m_pointee)
  80. return {};
  81. StringBuilder builder;
  82. builder.append(m_pointee->to_byte_string());
  83. builder.append('*');
  84. return builder.to_byte_string();
  85. }
  86. ByteString Reference::to_byte_string() const
  87. {
  88. if (!m_referenced_type)
  89. return {};
  90. StringBuilder builder;
  91. builder.append(m_referenced_type->to_byte_string());
  92. if (m_kind == Kind::Lvalue)
  93. builder.append('&');
  94. else
  95. builder.append("&&"sv);
  96. return builder.to_byte_string();
  97. }
  98. ByteString FunctionType::to_byte_string() const
  99. {
  100. StringBuilder builder;
  101. builder.append(m_return_type->to_byte_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(", "sv);
  109. if (parameter->type())
  110. builder.append(parameter->type()->to_byte_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_byte_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 (auto const& statement : m_statements) {
  139. statement->dump(output, indent + 1);
  140. }
  141. print_indent(output, indent);
  142. outln(output, "}}");
  143. }
  144. Vector<NonnullRefPtr<Declaration const>> FunctionDefinition::declarations() const
  145. {
  146. Vector<NonnullRefPtr<Declaration const>> 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. char const* 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. char const* 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 (auto const& 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. if (!m_baseclasses.is_empty()) {
  302. print_indent(output, indent + 1);
  303. outln(output, ":");
  304. for (size_t i = 0; i < m_baseclasses.size(); ++i) {
  305. auto& baseclass = m_baseclasses[i];
  306. baseclass->dump(output, indent + 1);
  307. if (i < m_baseclasses.size() - 1) {
  308. print_indent(output, indent + 1);
  309. outln(output, ",");
  310. }
  311. }
  312. }
  313. outln(output, "");
  314. for (auto& member : m_members) {
  315. member->dump(output, indent + 1);
  316. }
  317. }
  318. Vector<NonnullRefPtr<Declaration const>> StructOrClassDeclaration::declarations() const
  319. {
  320. Vector<NonnullRefPtr<Declaration const>> declarations;
  321. for (auto& member : m_members)
  322. declarations.append(member);
  323. return declarations;
  324. }
  325. void UnaryExpression::dump(FILE* output, size_t indent) const
  326. {
  327. ASTNode::dump(output, indent);
  328. char const* op_string = nullptr;
  329. switch (m_op) {
  330. case UnaryOp::BitwiseNot:
  331. op_string = "~";
  332. break;
  333. case UnaryOp::Not:
  334. op_string = "!";
  335. break;
  336. case UnaryOp::Plus:
  337. op_string = "+";
  338. break;
  339. case UnaryOp::Minus:
  340. op_string = "-";
  341. break;
  342. case UnaryOp::PlusPlus:
  343. op_string = "++";
  344. break;
  345. case UnaryOp::Address:
  346. op_string = "&";
  347. break;
  348. default:
  349. op_string = "<invalid>";
  350. }
  351. VERIFY(op_string);
  352. print_indent(output, indent + 1);
  353. outln(output, "{}", op_string);
  354. m_lhs->dump(output, indent + 1);
  355. }
  356. void BooleanLiteral::dump(FILE* output, size_t indent) const
  357. {
  358. ASTNode::dump(output, indent);
  359. print_indent(output, indent + 1);
  360. outln(output, "{}", m_value ? "true" : "false");
  361. }
  362. void Pointer::dump(FILE* output, size_t indent) const
  363. {
  364. ASTNode::dump(output, indent);
  365. if (!m_pointee.is_null()) {
  366. m_pointee->dump(output, indent + 1);
  367. }
  368. }
  369. void Reference::dump(FILE* output, size_t indent) const
  370. {
  371. ASTNode::dump(output, indent);
  372. print_indent(output, indent + 1);
  373. outln(output, "{}", m_kind == Kind::Lvalue ? "&" : "&&");
  374. if (!m_referenced_type.is_null()) {
  375. m_referenced_type->dump(output, indent + 1);
  376. }
  377. }
  378. void FunctionType::dump(FILE* output, size_t indent) const
  379. {
  380. ASTNode::dump(output, indent);
  381. if (m_return_type)
  382. m_return_type->dump(output, indent + 1);
  383. print_indent(output, indent + 1);
  384. outln("(");
  385. for (auto& parameter : m_parameters)
  386. parameter->dump(output, indent + 2);
  387. print_indent(output, indent + 1);
  388. outln(")");
  389. }
  390. void MemberExpression::dump(FILE* output, size_t indent) const
  391. {
  392. ASTNode::dump(output, indent);
  393. m_object->dump(output, indent + 1);
  394. m_property->dump(output, indent + 1);
  395. }
  396. void BlockStatement::dump(FILE* output, size_t indent) const
  397. {
  398. ASTNode::dump(output, indent);
  399. for (auto& statement : m_statements) {
  400. statement->dump(output, indent + 1);
  401. }
  402. }
  403. void ForStatement::dump(FILE* output, size_t indent) const
  404. {
  405. ASTNode::dump(output, indent);
  406. if (m_init)
  407. m_init->dump(output, indent + 1);
  408. if (m_test)
  409. m_test->dump(output, indent + 1);
  410. if (m_update)
  411. m_update->dump(output, indent + 1);
  412. if (m_body)
  413. m_body->dump(output, indent + 1);
  414. }
  415. Vector<NonnullRefPtr<Declaration const>> Statement::declarations() const
  416. {
  417. if (is_declaration()) {
  418. Vector<NonnullRefPtr<Declaration const>> vec;
  419. auto const& decl = static_cast<Declaration const&>(*this);
  420. vec.empend(const_cast<Declaration&>(decl));
  421. return vec;
  422. }
  423. return {};
  424. }
  425. Vector<NonnullRefPtr<Declaration const>> ForStatement::declarations() const
  426. {
  427. Vector<NonnullRefPtr<Declaration const>> declarations;
  428. if (m_init)
  429. declarations.extend(m_init->declarations());
  430. if (m_body)
  431. declarations.extend(m_body->declarations());
  432. return declarations;
  433. }
  434. Vector<NonnullRefPtr<Declaration const>> BlockStatement::declarations() const
  435. {
  436. Vector<NonnullRefPtr<Declaration const>> declarations;
  437. for (auto& statement : m_statements) {
  438. declarations.extend(statement->declarations());
  439. }
  440. return declarations;
  441. }
  442. void IfStatement::dump(FILE* output, size_t indent) const
  443. {
  444. ASTNode::dump(output, indent);
  445. if (m_predicate) {
  446. print_indent(output, indent + 1);
  447. outln(output, "Predicate:");
  448. m_predicate->dump(output, indent + 1);
  449. }
  450. if (m_then) {
  451. print_indent(output, indent + 1);
  452. outln(output, "Then:");
  453. m_then->dump(output, indent + 1);
  454. }
  455. if (m_else) {
  456. print_indent(output, indent + 1);
  457. outln(output, "Else:");
  458. m_else->dump(output, indent + 1);
  459. }
  460. }
  461. Vector<NonnullRefPtr<Declaration const>> IfStatement::declarations() const
  462. {
  463. Vector<NonnullRefPtr<Declaration const>> declarations;
  464. if (m_predicate)
  465. declarations.extend(m_predicate->declarations());
  466. if (m_then)
  467. declarations.extend(m_then->declarations());
  468. if (m_else)
  469. declarations.extend(m_else->declarations());
  470. return declarations;
  471. }
  472. void NamespaceDeclaration::dump(FILE* output, size_t indent) const
  473. {
  474. ASTNode::dump(output, indent);
  475. print_indent(output, indent + 1);
  476. outln(output, "{}", full_name());
  477. for (auto& decl : m_declarations)
  478. decl->dump(output, indent + 1);
  479. }
  480. void NullPointerLiteral::dump(FILE* output, size_t indent) const
  481. {
  482. ASTNode::dump(output, indent);
  483. }
  484. void Name::dump(FILE* output, size_t indent) const
  485. {
  486. ASTNode::dump(output, indent);
  487. print_indent(output, indent);
  488. outln(output, "{}", full_name());
  489. }
  490. StringView Name::full_name() const
  491. {
  492. if (m_full_name.has_value())
  493. return *m_full_name;
  494. StringBuilder builder;
  495. if (!m_scope.is_empty()) {
  496. for (auto& scope : m_scope) {
  497. builder.appendff("{}::", scope->name());
  498. }
  499. }
  500. m_full_name = ByteString::formatted("{}{}", builder.to_byte_string(), m_name.is_null() ? ""sv : m_name->name());
  501. return *m_full_name;
  502. }
  503. StringView TemplatizedName::full_name() const
  504. {
  505. if (m_full_name.has_value())
  506. return *m_full_name;
  507. StringBuilder name;
  508. name.append(Name::full_name());
  509. name.append('<');
  510. for (auto& type : m_template_arguments) {
  511. name.append(type->to_byte_string());
  512. }
  513. name.append('>');
  514. m_full_name = name.to_byte_string();
  515. return *m_full_name;
  516. }
  517. void SizedName::dump(FILE* output, size_t indent) const
  518. {
  519. Name::dump(output, indent);
  520. print_indent(output, indent + 1);
  521. StringBuilder dimension_info;
  522. for (auto const& dim : m_dimensions) {
  523. dimension_info.append('[');
  524. dimension_info.append(dim);
  525. dimension_info.append(']');
  526. }
  527. if (dimension_info.is_empty()) {
  528. dimension_info.append("[]"sv);
  529. }
  530. outln(output, "{}", dimension_info.to_byte_string());
  531. }
  532. void CppCastExpression::dump(FILE* output, size_t indent) const
  533. {
  534. ASTNode::dump(output, indent);
  535. print_indent(output, indent);
  536. outln(output, "{}", m_cast_type);
  537. print_indent(output, indent + 1);
  538. outln(output, "<");
  539. if (m_type)
  540. m_type->dump(output, indent + 1);
  541. print_indent(output, indent + 1);
  542. outln(output, ">");
  543. if (m_expression)
  544. m_expression->dump(output, indent + 1);
  545. }
  546. void SizeofExpression::dump(FILE* output, size_t indent) const
  547. {
  548. ASTNode::dump(output, indent);
  549. if (m_type)
  550. m_type->dump(output, indent + 1);
  551. }
  552. void BracedInitList::dump(FILE* output, size_t indent) const
  553. {
  554. ASTNode::dump(output, indent);
  555. for (auto& exp : m_expressions) {
  556. exp->dump(output, indent + 1);
  557. }
  558. }
  559. void CStyleCastExpression::dump(FILE* output, size_t indent) const
  560. {
  561. ASTNode::dump(output, indent);
  562. if (m_type)
  563. m_type->dump(output, indent + 1);
  564. if (m_expression)
  565. m_expression->dump(output, indent + 1);
  566. }
  567. void Constructor::dump(FILE* output, size_t indent) const
  568. {
  569. print_indent(output, indent);
  570. outln(output, "C'tor");
  571. print_indent(output, indent + 1);
  572. outln(output, "(");
  573. for (auto const& arg : parameters()) {
  574. arg->dump(output, indent + 1);
  575. }
  576. print_indent(output, indent + 1);
  577. outln(output, ")");
  578. if (definition()) {
  579. definition()->dump(output, indent + 1);
  580. }
  581. }
  582. void Destructor::dump(FILE* output, size_t indent) const
  583. {
  584. print_indent(output, indent);
  585. outln(output, "D'tor");
  586. print_indent(output, indent + 1);
  587. outln(output, "(");
  588. for (auto const& arg : parameters()) {
  589. arg->dump(output, indent + 1);
  590. }
  591. print_indent(output, indent + 1);
  592. outln(output, ")");
  593. if (definition()) {
  594. definition()->dump(output, indent + 1);
  595. }
  596. }
  597. StringView Declaration::full_name() const
  598. {
  599. if (!m_full_name.has_value()) {
  600. if (m_name)
  601. m_full_name = m_name->full_name();
  602. else
  603. m_full_name = ByteString::empty();
  604. }
  605. return *m_full_name;
  606. }
  607. void UsingNamespaceDeclaration::dump(FILE* output, size_t indent) const
  608. {
  609. ASTNode::dump(output, indent);
  610. print_indent(output, indent + 1);
  611. outln(output, "{}", full_name());
  612. }
  613. void TypedefDeclaration::dump(FILE* output, size_t indent) const
  614. {
  615. ASTNode::dump(output, indent);
  616. print_indent(output, indent + 1);
  617. outln(output, "{}", full_name());
  618. if (m_alias)
  619. m_alias->dump(output, indent + 1);
  620. }
  621. }