AST.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2023, Volodymyr V. <vvmposeydon@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "AST.h"
  8. namespace GLSL {
  9. static ErrorOr<void> print_indent(AK::Stream& output, int indent)
  10. {
  11. for (int i = 0; i < indent * 2; ++i)
  12. TRY(output.write_some(" "sv.bytes()));
  13. return {};
  14. }
  15. ErrorOr<void> ASTNode::dump(AK::Stream& output, size_t indent) const
  16. {
  17. TRY(print_indent(output, indent));
  18. TRY(output.write_formatted("{}[{}:{}->{}:{}]\n", class_name(), start().line, start().column, end().line, end().column));
  19. return {};
  20. }
  21. ErrorOr<void> TranslationUnit::dump(AK::Stream& output, size_t indent) const
  22. {
  23. TRY(ASTNode::dump(output, indent));
  24. for (auto const& child : m_declarations) {
  25. TRY(child->dump(output, indent + 1));
  26. }
  27. return {};
  28. }
  29. ErrorOr<void> FunctionDeclaration::dump(AK::Stream& output, size_t indent) const
  30. {
  31. TRY(ASTNode::dump(output, indent));
  32. TRY(m_return_type->dump(output, indent + 1));
  33. if (!m_name.is_null()) {
  34. TRY(print_indent(output, indent + 1));
  35. TRY(output.write_formatted("{}\n", m_name->name()));
  36. }
  37. TRY(print_indent(output, indent + 1));
  38. TRY(output.write_formatted("(\n"));
  39. for (auto const& arg : m_parameters) {
  40. TRY(arg->dump(output, indent + 1));
  41. }
  42. TRY(print_indent(output, indent + 1));
  43. TRY(output.write_formatted(")\n"));
  44. if (!m_definition.is_null()) {
  45. TRY(m_definition->dump(output, indent + 1));
  46. }
  47. return {};
  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. ErrorOr<void> Type::dump(AK::Stream& output, size_t indent) const
  60. {
  61. TRY(ASTNode::dump(output, indent));
  62. TRY(print_indent(output, indent + 1));
  63. StringBuilder qualifiers_string;
  64. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Const).has_value())
  65. qualifiers_string.append("const "sv);
  66. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::In).has_value())
  67. qualifiers_string.append("in "sv);
  68. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Out).has_value())
  69. qualifiers_string.append("out "sv);
  70. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Inout).has_value())
  71. qualifiers_string.append("inout "sv);
  72. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Centroid).has_value())
  73. qualifiers_string.append("centroid "sv);
  74. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Patch).has_value())
  75. qualifiers_string.append("patch "sv);
  76. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Sample).has_value())
  77. qualifiers_string.append("sample "sv);
  78. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Uniform).has_value())
  79. qualifiers_string.append("uniform "sv);
  80. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Buffer).has_value())
  81. qualifiers_string.append("buffer "sv);
  82. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Shared).has_value())
  83. qualifiers_string.append("shared "sv);
  84. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Coherent).has_value())
  85. qualifiers_string.append("coherent "sv);
  86. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Volatile).has_value())
  87. qualifiers_string.append("volatile "sv);
  88. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Restrict).has_value())
  89. qualifiers_string.append("restrict "sv);
  90. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Readonly).has_value())
  91. qualifiers_string.append("readonly "sv);
  92. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Writeonly).has_value())
  93. qualifiers_string.append("writeonly "sv);
  94. if (m_storage_qualifiers.find_first_index(StorageTypeQualifier::Subroutine).has_value())
  95. qualifiers_string.append("subroutine "sv);
  96. TRY(output.write_formatted("{}{}\n", qualifiers_string.string_view(), m_name->name()));
  97. return {};
  98. }
  99. ErrorOr<void> Parameter::dump(AK::Stream& output, size_t indent) const
  100. {
  101. TRY(ASTNode::dump(output, indent));
  102. if (!m_name.is_null()) {
  103. TRY(print_indent(output, indent));
  104. TRY(output.write_formatted("{}\n", m_name->name()));
  105. }
  106. if (m_type)
  107. TRY(m_type->dump(output, indent + 1));
  108. return {};
  109. }
  110. ErrorOr<void> FunctionDefinition::dump(AK::Stream& output, size_t indent) const
  111. {
  112. TRY(ASTNode::dump(output, indent));
  113. TRY(print_indent(output, indent));
  114. TRY(output.write_formatted("{{\n"));
  115. for (auto const& statement : m_statements) {
  116. TRY(statement->dump(output, indent + 1));
  117. }
  118. TRY(print_indent(output, indent));
  119. TRY(output.write_formatted("}}\n"));
  120. return {};
  121. }
  122. Vector<NonnullRefPtr<Declaration const>> FunctionDefinition::declarations() const
  123. {
  124. Vector<NonnullRefPtr<Declaration const>> declarations;
  125. for (auto& statement : m_statements) {
  126. declarations.extend(statement->declarations());
  127. }
  128. return declarations;
  129. }
  130. ErrorOr<void> VariableDeclaration::dump(AK::Stream& output, size_t indent) const
  131. {
  132. TRY(ASTNode::dump(output, indent));
  133. if (m_type)
  134. TRY(m_type->dump(output, indent + 1));
  135. TRY(print_indent(output, indent + 1));
  136. TRY(output.write_formatted("{}\n", m_name->name()));
  137. if (m_initial_value)
  138. TRY(m_initial_value->dump(output, indent + 1));
  139. return {};
  140. }
  141. ErrorOr<void> NumericLiteral::dump(AK::Stream& output, size_t indent) const
  142. {
  143. TRY(ASTNode::dump(output, indent));
  144. TRY(print_indent(output, indent + 1));
  145. TRY(output.write_formatted("{}\n", m_value));
  146. return {};
  147. }
  148. ErrorOr<void> BinaryExpression::dump(AK::Stream& output, size_t indent) const
  149. {
  150. TRY(ASTNode::dump(output, indent));
  151. char const* op_string = nullptr;
  152. switch (m_op) {
  153. case BinaryOp::Addition:
  154. op_string = "+";
  155. break;
  156. case BinaryOp::Subtraction:
  157. op_string = "-";
  158. break;
  159. case BinaryOp::Multiplication:
  160. op_string = "*";
  161. break;
  162. case BinaryOp::Division:
  163. op_string = "/";
  164. break;
  165. case BinaryOp::Modulo:
  166. op_string = "%";
  167. break;
  168. case BinaryOp::GreaterThan:
  169. op_string = ">";
  170. break;
  171. case BinaryOp::GreaterThanEquals:
  172. op_string = ">=";
  173. break;
  174. case BinaryOp::LessThan:
  175. op_string = "<";
  176. break;
  177. case BinaryOp::LessThanEquals:
  178. op_string = "<=";
  179. break;
  180. case BinaryOp::BitwiseAnd:
  181. op_string = "&";
  182. break;
  183. case BinaryOp::BitwiseOr:
  184. op_string = "|";
  185. break;
  186. case BinaryOp::BitwiseXor:
  187. op_string = "^";
  188. break;
  189. case BinaryOp::LeftShift:
  190. op_string = "<<";
  191. break;
  192. case BinaryOp::RightShift:
  193. op_string = ">>";
  194. break;
  195. case BinaryOp::EqualsEquals:
  196. op_string = "==";
  197. break;
  198. case BinaryOp::NotEqual:
  199. op_string = "!=";
  200. break;
  201. case BinaryOp::LogicalOr:
  202. op_string = "||";
  203. break;
  204. case BinaryOp::LogicalXor:
  205. op_string = "^^";
  206. break;
  207. case BinaryOp::LogicalAnd:
  208. op_string = "&&";
  209. break;
  210. case BinaryOp::Assignment:
  211. op_string = "=";
  212. break;
  213. case BinaryOp::AdditionAssignment:
  214. op_string = "+=";
  215. break;
  216. case BinaryOp::SubtractionAssignment:
  217. op_string = "-=";
  218. break;
  219. case BinaryOp::MultiplicationAssignment:
  220. op_string = "*=";
  221. break;
  222. case BinaryOp::DivisionAssignment:
  223. op_string = "/=";
  224. break;
  225. case BinaryOp::ModuloAssignment:
  226. op_string = "%=";
  227. break;
  228. case BinaryOp::AndAssignment:
  229. op_string = "&=";
  230. break;
  231. case BinaryOp::OrAssignment:
  232. op_string = "|=";
  233. break;
  234. case BinaryOp::XorAssignment:
  235. op_string = "^=";
  236. break;
  237. case BinaryOp::LeftShiftAssignment:
  238. op_string = "<<=";
  239. break;
  240. case BinaryOp::RightShiftAssignment:
  241. op_string = ">>=";
  242. break;
  243. }
  244. TRY(m_lhs->dump(output, indent + 1));
  245. TRY(print_indent(output, indent + 1));
  246. VERIFY(op_string);
  247. TRY(output.write_formatted("{}\n", op_string));
  248. TRY(m_rhs->dump(output, indent + 1));
  249. return {};
  250. }
  251. ErrorOr<void> FunctionCall::dump(AK::Stream& output, size_t indent) const
  252. {
  253. TRY(ASTNode::dump(output, indent));
  254. TRY(m_callee->dump(output, indent + 1));
  255. TRY(print_indent(output, indent + 1));
  256. TRY(output.write_formatted("(\n"));
  257. for (auto const& arg : m_arguments) {
  258. TRY(arg->dump(output, indent + 1));
  259. }
  260. TRY(print_indent(output, indent + 1));
  261. TRY(output.write_formatted(")\n"));
  262. return {};
  263. }
  264. ErrorOr<void> StringLiteral::dump(AK::Stream& output, size_t indent) const
  265. {
  266. TRY(ASTNode::dump(output, indent));
  267. TRY(print_indent(output, indent + 1));
  268. TRY(output.write_formatted("{}\n", m_value));
  269. return {};
  270. }
  271. ErrorOr<void> ReturnStatement::dump(AK::Stream& output, size_t indent) const
  272. {
  273. TRY(ASTNode::dump(output, indent));
  274. if (m_value)
  275. TRY(m_value->dump(output, indent + 1));
  276. return {};
  277. }
  278. ErrorOr<void> StructDeclaration::dump(AK::Stream& output, size_t indent) const
  279. {
  280. TRY(ASTNode::dump(output, indent));
  281. TRY(print_indent(output, indent + 1));
  282. TRY(output.write_formatted("{}\n", m_name->name()));
  283. for (auto& member : m_members) {
  284. TRY(member->dump(output, indent + 1));
  285. }
  286. return {};
  287. }
  288. Vector<NonnullRefPtr<Declaration const>> StructDeclaration::declarations() const
  289. {
  290. Vector<NonnullRefPtr<Declaration const>> declarations;
  291. for (auto& member : m_members)
  292. declarations.append(member);
  293. return declarations;
  294. }
  295. ErrorOr<void> UnaryExpression::dump(AK::Stream& output, size_t indent) const
  296. {
  297. TRY(ASTNode::dump(output, indent));
  298. char const* op_string = nullptr;
  299. switch (m_op) {
  300. case UnaryOp::BitwiseNot:
  301. op_string = "~";
  302. break;
  303. case UnaryOp::Not:
  304. op_string = "!";
  305. break;
  306. case UnaryOp::Plus:
  307. op_string = "+";
  308. break;
  309. case UnaryOp::Minus:
  310. op_string = "-";
  311. break;
  312. case UnaryOp::PlusPlus:
  313. op_string = "++";
  314. break;
  315. case UnaryOp::MinusMinus:
  316. op_string = "--";
  317. break;
  318. default:
  319. op_string = "<invalid>";
  320. }
  321. VERIFY(op_string);
  322. TRY(print_indent(output, indent + 1));
  323. TRY(output.write_formatted("{} {}\n", m_is_postfix ? "postfix"sv : "prefix"sv, op_string));
  324. TRY(m_lhs->dump(output, indent + 1));
  325. return {};
  326. }
  327. ErrorOr<void> BooleanLiteral::dump(AK::Stream& output, size_t indent) const
  328. {
  329. TRY(ASTNode::dump(output, indent));
  330. TRY(print_indent(output, indent + 1));
  331. TRY(output.write_formatted("{}\n", m_value ? "true" : "false"));
  332. return {};
  333. }
  334. ErrorOr<void> MemberExpression::dump(AK::Stream& output, size_t indent) const
  335. {
  336. TRY(ASTNode::dump(output, indent));
  337. TRY(m_object->dump(output, indent + 1));
  338. TRY(m_property->dump(output, indent + 1));
  339. return {};
  340. }
  341. ErrorOr<void> ArrayElementExpression::dump(AK::Stream& output, size_t indent) const
  342. {
  343. TRY(ASTNode::dump(output, indent));
  344. TRY(m_array->dump(output, indent + 1));
  345. TRY(print_indent(output, indent + 1));
  346. TRY(output.write_formatted("[\n"));
  347. TRY(m_index->dump(output, indent + 1));
  348. TRY(print_indent(output, indent + 1));
  349. TRY(output.write_formatted("]\n"));
  350. return {};
  351. }
  352. ErrorOr<void> BlockStatement::dump(AK::Stream& output, size_t indent) const
  353. {
  354. TRY(ASTNode::dump(output, indent));
  355. for (auto& statement : m_statements) {
  356. TRY(statement->dump(output, indent + 1));
  357. }
  358. return {};
  359. }
  360. ErrorOr<void> ForStatement::dump(AK::Stream& output, size_t indent) const
  361. {
  362. TRY(ASTNode::dump(output, indent));
  363. if (m_init) {
  364. TRY(print_indent(output, indent));
  365. TRY(output.write_formatted("Initializer:\n"));
  366. TRY(m_init->dump(output, indent + 1));
  367. }
  368. if (m_test) {
  369. TRY(print_indent(output, indent));
  370. TRY(output.write_formatted("Test expression:\n"));
  371. TRY(m_test->dump(output, indent + 1));
  372. }
  373. if (m_update) {
  374. TRY(print_indent(output, indent));
  375. TRY(output.write_formatted("Update expression:\n"));
  376. TRY(m_update->dump(output, indent + 1));
  377. }
  378. if (m_body) {
  379. TRY(print_indent(output, indent));
  380. TRY(output.write_formatted("Body:\n"));
  381. TRY(m_body->dump(output, indent + 1));
  382. }
  383. return {};
  384. }
  385. Vector<NonnullRefPtr<Declaration const>> Statement::declarations() const
  386. {
  387. if (is_declaration()) {
  388. Vector<NonnullRefPtr<Declaration const>> vec;
  389. auto const& decl = static_cast<Declaration const&>(*this);
  390. vec.empend(const_cast<Declaration&>(decl));
  391. return vec;
  392. }
  393. return {};
  394. }
  395. Vector<NonnullRefPtr<Declaration const>> ForStatement::declarations() const
  396. {
  397. Vector<NonnullRefPtr<Declaration const>> declarations;
  398. if (m_init)
  399. declarations.extend(m_init->declarations());
  400. if (m_body)
  401. declarations.extend(m_body->declarations());
  402. return declarations;
  403. }
  404. Vector<NonnullRefPtr<Declaration const>> BlockStatement::declarations() const
  405. {
  406. Vector<NonnullRefPtr<Declaration const>> declarations;
  407. for (auto& statement : m_statements) {
  408. declarations.extend(statement->declarations());
  409. }
  410. return declarations;
  411. }
  412. ErrorOr<void> IfStatement::dump(AK::Stream& output, size_t indent) const
  413. {
  414. TRY(ASTNode::dump(output, indent));
  415. if (m_predicate) {
  416. TRY(print_indent(output, indent + 1));
  417. TRY(output.write_formatted("Predicate:\n"));
  418. TRY(m_predicate->dump(output, indent + 1));
  419. }
  420. if (m_then) {
  421. TRY(print_indent(output, indent + 1));
  422. TRY(output.write_formatted("Then:\n"));
  423. TRY(m_then->dump(output, indent + 1));
  424. }
  425. if (m_else) {
  426. TRY(print_indent(output, indent + 1));
  427. TRY(output.write_formatted("Else:\n"));
  428. TRY(m_else->dump(output, indent + 1));
  429. }
  430. return {};
  431. }
  432. Vector<NonnullRefPtr<Declaration const>> IfStatement::declarations() const
  433. {
  434. Vector<NonnullRefPtr<Declaration const>> declarations;
  435. if (m_predicate)
  436. declarations.extend(m_predicate->declarations());
  437. if (m_then)
  438. declarations.extend(m_then->declarations());
  439. if (m_else)
  440. declarations.extend(m_else->declarations());
  441. return declarations;
  442. }
  443. ErrorOr<void> Name::dump(AK::Stream& output, size_t indent) const
  444. {
  445. TRY(ASTNode::dump(output, indent));
  446. TRY(print_indent(output, indent + 1));
  447. TRY(output.write_formatted("{}\n", name()));
  448. return {};
  449. }
  450. ErrorOr<void> SizedName::dump(AK::Stream& output, size_t indent) const
  451. {
  452. TRY(Name::dump(output, indent));
  453. TRY(print_indent(output, indent + 1));
  454. StringBuilder dimension_info;
  455. for (auto const& dim : m_dimensions) {
  456. dimension_info.append('[');
  457. dimension_info.append(dim);
  458. dimension_info.append(']');
  459. }
  460. if (dimension_info.is_empty()) {
  461. dimension_info.append("[]"sv);
  462. }
  463. TRY(output.write_formatted("{}\n", dimension_info.string_view()));
  464. return {};
  465. }
  466. }