AST.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. 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.extend(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 UnaryExpression::dump(FILE* output, size_t indent) const
  278. {
  279. ASTNode::dump(output, indent);
  280. const char* op_string = nullptr;
  281. switch (m_op) {
  282. case UnaryOp::BitwiseNot:
  283. op_string = "~";
  284. break;
  285. case UnaryOp::Not:
  286. op_string = "!";
  287. break;
  288. case UnaryOp::Plus:
  289. op_string = "+";
  290. break;
  291. case UnaryOp::Minus:
  292. op_string = "-";
  293. break;
  294. case UnaryOp::PlusPlus:
  295. op_string = "++";
  296. break;
  297. case UnaryOp::Address:
  298. op_string = "&";
  299. break;
  300. default:
  301. op_string = "<invalid>";
  302. }
  303. VERIFY(op_string);
  304. print_indent(output, indent + 1);
  305. outln(output, "{}", op_string);
  306. m_lhs->dump(output, indent + 1);
  307. }
  308. void BooleanLiteral::dump(FILE* output, size_t indent) const
  309. {
  310. ASTNode::dump(output, indent);
  311. print_indent(output, indent + 1);
  312. outln(output, "{}", m_value ? "true" : "false");
  313. }
  314. void Pointer::dump(FILE* output, size_t indent) const
  315. {
  316. ASTNode::dump(output, indent);
  317. if (!m_pointee.is_null()) {
  318. m_pointee->dump(output, indent + 1);
  319. }
  320. }
  321. void MemberExpression::dump(FILE* output, size_t indent) const
  322. {
  323. ASTNode::dump(output, indent);
  324. m_object->dump(output, indent + 1);
  325. m_property->dump(output, indent + 1);
  326. }
  327. void BlockStatement::dump(FILE* output, size_t indent) const
  328. {
  329. ASTNode::dump(output, indent);
  330. for (auto& statement : m_statements) {
  331. statement.dump(output, indent + 1);
  332. }
  333. }
  334. void ForStatement::dump(FILE* output, size_t indent) const
  335. {
  336. ASTNode::dump(output, indent);
  337. if (m_init)
  338. m_init->dump(output, indent + 1);
  339. if (m_test)
  340. m_test->dump(output, indent + 1);
  341. if (m_update)
  342. m_update->dump(output, indent + 1);
  343. if (m_body)
  344. m_body->dump(output, indent + 1);
  345. }
  346. NonnullRefPtrVector<Declaration> Statement::declarations() const
  347. {
  348. if (is_declaration()) {
  349. NonnullRefPtrVector<Declaration> vec;
  350. const auto& decl = static_cast<const Declaration&>(*this);
  351. vec.empend(const_cast<Declaration&>(decl));
  352. return vec;
  353. }
  354. return {};
  355. }
  356. NonnullRefPtrVector<Declaration> ForStatement::declarations() const
  357. {
  358. NonnullRefPtrVector<Declaration> declarations;
  359. if (m_init)
  360. declarations.extend(m_init->declarations());
  361. if (m_body)
  362. declarations.extend(m_body->declarations());
  363. return declarations;
  364. }
  365. NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
  366. {
  367. NonnullRefPtrVector<Declaration> declarations;
  368. for (auto& statement : m_statements) {
  369. declarations.extend(statement.declarations());
  370. }
  371. return declarations;
  372. }
  373. void IfStatement::dump(FILE* output, size_t indent) const
  374. {
  375. ASTNode::dump(output, indent);
  376. if (m_predicate) {
  377. print_indent(output, indent + 1);
  378. outln(output, "Predicate:");
  379. m_predicate->dump(output, indent + 1);
  380. }
  381. if (m_then) {
  382. print_indent(output, indent + 1);
  383. outln(output, "Then:");
  384. m_then->dump(output, indent + 1);
  385. }
  386. if (m_else) {
  387. print_indent(output, indent + 1);
  388. outln(output, "Else:");
  389. m_else->dump(output, indent + 1);
  390. }
  391. }
  392. NonnullRefPtrVector<Declaration> IfStatement::declarations() const
  393. {
  394. NonnullRefPtrVector<Declaration> declarations;
  395. if (m_predicate)
  396. declarations.extend(m_predicate->declarations());
  397. if (m_then)
  398. declarations.extend(m_then->declarations());
  399. if (m_else)
  400. declarations.extend(m_else->declarations());
  401. return declarations;
  402. }
  403. void NamespaceDeclaration::dump(FILE* output, size_t indent) const
  404. {
  405. ASTNode::dump(output, indent);
  406. print_indent(output, indent + 1);
  407. outln(output, "{}", m_name);
  408. for (auto& decl : m_declarations)
  409. decl.dump(output, indent + 1);
  410. }
  411. void NullPointerLiteral::dump(FILE* output, size_t indent) const
  412. {
  413. ASTNode::dump(output, indent);
  414. }
  415. void Name::dump(FILE* output, size_t indent) const
  416. {
  417. ASTNode::dump(output, indent);
  418. print_indent(output, indent);
  419. outln(output, "{}", full_name());
  420. }
  421. String Name::full_name() const
  422. {
  423. StringBuilder builder;
  424. if (!m_scope.is_empty()) {
  425. for (auto& scope : m_scope) {
  426. builder.appendff("{}::", scope.name());
  427. }
  428. }
  429. return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->name());
  430. }
  431. String TemplatizedName::full_name() const
  432. {
  433. StringBuilder name;
  434. name.append(Name::full_name());
  435. name.append('<');
  436. for (auto& type : m_template_arguments) {
  437. name.append(type.to_string());
  438. }
  439. name.append('>');
  440. return name.to_string();
  441. }
  442. void CppCastExpression::dump(FILE* output, size_t indent) const
  443. {
  444. ASTNode::dump(output, indent);
  445. print_indent(output, indent);
  446. outln(output, "{}", m_cast_type);
  447. print_indent(output, indent + 1);
  448. outln(output, "<");
  449. if (m_type)
  450. m_type->dump(output, indent + 1);
  451. print_indent(output, indent + 1);
  452. outln(output, ">");
  453. if (m_expression)
  454. m_expression->dump(output, indent + 1);
  455. }
  456. void SizeofExpression::dump(FILE* output, size_t indent) const
  457. {
  458. ASTNode::dump(output, indent);
  459. if (m_type)
  460. m_type->dump(output, indent + 1);
  461. }
  462. void BracedInitList::dump(FILE* output, size_t indent) const
  463. {
  464. ASTNode::dump(output, indent);
  465. for (auto& exp : m_expressions) {
  466. exp.dump(output, indent + 1);
  467. }
  468. }
  469. void CStyleCastExpression::dump(FILE* output, size_t indent) const
  470. {
  471. ASTNode::dump(output, indent);
  472. if (m_type)
  473. m_type->dump(output, indent + 1);
  474. if (m_expression)
  475. m_expression->dump(output, indent + 1);
  476. }
  477. void Constructor::dump(FILE* output, size_t indent) const
  478. {
  479. print_indent(output, indent);
  480. outln(output, "C'tor");
  481. print_indent(output, indent + 1);
  482. outln(output, "(");
  483. for (const auto& arg : parameters()) {
  484. arg.dump(output, indent + 1);
  485. }
  486. print_indent(output, indent + 1);
  487. outln(output, ")");
  488. if (definition()) {
  489. definition()->dump(output, indent + 1);
  490. }
  491. }
  492. void Destructor::dump(FILE* output, size_t indent) const
  493. {
  494. print_indent(output, indent);
  495. outln(output, "D'tor");
  496. print_indent(output, indent + 1);
  497. outln(output, "(");
  498. for (const auto& arg : parameters()) {
  499. arg.dump(output, indent + 1);
  500. }
  501. print_indent(output, indent + 1);
  502. outln(output, ")");
  503. if (definition()) {
  504. definition()->dump(output, indent + 1);
  505. }
  506. }
  507. }