AST.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/Vector.h>
  11. #include <LibCrypto/BigFraction/BigFraction.h>
  12. #include "Forward.h"
  13. namespace JSSpecCompiler {
  14. template<typename T>
  15. RefPtr<T> as(NullableTree const& tree)
  16. {
  17. return dynamic_cast<T*>(tree.ptr());
  18. }
  19. class NodeSubtreePointer {
  20. public:
  21. NodeSubtreePointer(Tree* tree_ptr)
  22. : m_tree_ptr(tree_ptr)
  23. {
  24. }
  25. NodeSubtreePointer(NullableTree* tree_ptr)
  26. : m_tree_ptr(tree_ptr)
  27. {
  28. }
  29. NodeSubtreePointer(VariableRef* tree_ptr)
  30. : m_tree_ptr(tree_ptr)
  31. {
  32. }
  33. Tree get(Badge<RecursiveASTVisitor>);
  34. void replace_subtree(Badge<RecursiveASTVisitor>, NullableTree replacement);
  35. private:
  36. Variant<Tree*, NullableTree*, VariableRef*> m_tree_ptr;
  37. };
  38. class VariableDeclaration : public RefCounted<VariableDeclaration> {
  39. public:
  40. virtual ~VariableDeclaration() = default;
  41. };
  42. class NamedVariableDeclaration : public VariableDeclaration {
  43. public:
  44. NamedVariableDeclaration(StringView name)
  45. : m_name(name)
  46. {
  47. }
  48. StringView m_name;
  49. };
  50. class SSAVariableDeclaration : public VariableDeclaration {
  51. public:
  52. SSAVariableDeclaration(u64 version)
  53. : m_version(version)
  54. {
  55. }
  56. size_t m_index = 0;
  57. u64 m_version;
  58. };
  59. class Node : public RefCounted<Node> {
  60. public:
  61. virtual ~Node() = default;
  62. void format_tree(StringBuilder& builder);
  63. // For expressions, order must be the same as the evaluation order.
  64. virtual Vector<NodeSubtreePointer> subtrees() { return {}; }
  65. virtual bool is_list() const { return false; }
  66. virtual bool is_statement() { VERIFY_NOT_REACHED(); }
  67. protected:
  68. template<typename... Parameters>
  69. void dump_node(StringBuilder& builder, AK::CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters);
  70. virtual void dump_tree(StringBuilder& builder) = 0;
  71. };
  72. // Although both statements and expressions are allowed to return value, CFG building differentiates
  73. // between them. Expressions are not allowed to change control flow, while statements are. Special
  74. // handling required if a statement turns out to be a descendant of an expression. Roughly speaking,
  75. // from the CFG standpoint, something like `a = ({ b + ({ c }) }) + ({ d })` will look like
  76. // ```
  77. // auto tmp1 = c;
  78. // auto tmp2 = b + tmp1;
  79. // auto tmp3 = d;
  80. // a = tmp1 + tmp2;
  81. // ```.
  82. class Statement : public Node {
  83. public:
  84. bool is_statement() override { return true; }
  85. };
  86. class Expression : public Node {
  87. public:
  88. bool is_statement() override { return false; }
  89. };
  90. class ControlFlowOperator : public Statement {
  91. public:
  92. bool is_statement() override { return false; }
  93. virtual Vector<BasicBlockRef*> references() = 0;
  94. };
  95. class ErrorNode : public Expression {
  96. public:
  97. ErrorNode(StringView error = ""sv)
  98. : m_error(error)
  99. {
  100. }
  101. StringView m_error;
  102. protected:
  103. void dump_tree(StringBuilder& builder) override;
  104. };
  105. class WellKnownNode : public Expression {
  106. public:
  107. enum Type {
  108. False,
  109. Null,
  110. This,
  111. True,
  112. Undefined,
  113. // Update WellKnownNode::dump_tree after adding an entry here
  114. };
  115. WellKnownNode(Type type)
  116. : m_type(type)
  117. {
  118. }
  119. protected:
  120. void dump_tree(StringBuilder& builder) override;
  121. private:
  122. Type m_type;
  123. };
  124. inline Tree const error_tree = make_ref_counted<ErrorNode>();
  125. class ControlFlowFunctionReturn : public ControlFlowOperator {
  126. public:
  127. ControlFlowFunctionReturn(VariableRef return_value)
  128. : m_return_value(move(return_value))
  129. {
  130. }
  131. VariableRef m_return_value;
  132. Vector<NodeSubtreePointer> subtrees() override { return { { &m_return_value } }; }
  133. Vector<BasicBlockRef*> references() override { return {}; }
  134. protected:
  135. void dump_tree(StringBuilder& builder) override;
  136. };
  137. class ControlFlowJump : public ControlFlowOperator {
  138. public:
  139. ControlFlowJump(BasicBlockRef block)
  140. : m_block(block)
  141. {
  142. }
  143. Vector<BasicBlockRef*> references() override;
  144. BasicBlockRef m_block;
  145. protected:
  146. void dump_tree(StringBuilder& builder) override;
  147. };
  148. // This should be invalid enough to crash program on use.
  149. inline NonnullRefPtr<ControlFlowOperator> const invalid_continuation = make_ref_counted<ControlFlowJump>(nullptr);
  150. class ControlFlowBranch : public ControlFlowOperator {
  151. public:
  152. ControlFlowBranch(Tree condition, BasicBlockRef then, BasicBlockRef else_)
  153. : m_condition(move(condition))
  154. , m_then(then)
  155. , m_else(else_)
  156. {
  157. }
  158. Vector<NodeSubtreePointer> subtrees() override { return { { &m_condition } }; }
  159. Vector<BasicBlockRef*> references() override;
  160. Tree m_condition;
  161. BasicBlockRef m_then;
  162. BasicBlockRef m_else;
  163. protected:
  164. void dump_tree(StringBuilder& builder) override;
  165. };
  166. class MathematicalConstant : public Expression {
  167. public:
  168. MathematicalConstant(Crypto::BigFraction number)
  169. : m_number(number)
  170. {
  171. }
  172. protected:
  173. void dump_tree(StringBuilder& builder) override;
  174. private:
  175. Crypto::BigFraction m_number;
  176. };
  177. class StringLiteral : public Expression {
  178. public:
  179. StringLiteral(StringView literal)
  180. : m_literal(literal)
  181. {
  182. }
  183. StringView m_literal;
  184. protected:
  185. void dump_tree(StringBuilder& builder) override;
  186. };
  187. #define ENUMERATE_UNARY_OPERATORS(F) \
  188. F(Invalid) \
  189. F(AssertCompletion) \
  190. F(Minus) \
  191. F(ReturnIfAbrubt)
  192. #define ENUMERATE_BINARY_OPERATORS(F) \
  193. F(Invalid) \
  194. F(ArraySubscript) \
  195. F(Assignment) \
  196. F(Comma) \
  197. F(CompareEqual) \
  198. F(CompareGreater) \
  199. F(CompareLess) \
  200. F(CompareNotEqual) \
  201. F(Declaration) \
  202. F(Division) \
  203. F(MemberAccess) \
  204. F(Minus) \
  205. F(Multiplication) \
  206. F(Plus) \
  207. F(Power)
  208. #define NAME(name) name,
  209. #define STRINGIFY(name) #name##sv,
  210. enum class UnaryOperator {
  211. ENUMERATE_UNARY_OPERATORS(NAME)
  212. };
  213. inline constexpr StringView unary_operator_names[] = {
  214. ENUMERATE_UNARY_OPERATORS(STRINGIFY)
  215. };
  216. enum class BinaryOperator {
  217. #define NAME(name) name,
  218. ENUMERATE_BINARY_OPERATORS(NAME)
  219. };
  220. inline constexpr StringView binary_operator_names[] = {
  221. ENUMERATE_BINARY_OPERATORS(STRINGIFY)
  222. };
  223. #undef NAME
  224. #undef STRINGIFY
  225. class BinaryOperation : public Expression {
  226. public:
  227. BinaryOperation(BinaryOperator operation, Tree left, Tree right)
  228. : m_operation(operation)
  229. , m_left(move(left))
  230. , m_right(move(right))
  231. {
  232. }
  233. Vector<NodeSubtreePointer> subtrees() override;
  234. BinaryOperator m_operation;
  235. Tree m_left;
  236. Tree m_right;
  237. protected:
  238. void dump_tree(StringBuilder& builder) override;
  239. };
  240. class UnaryOperation : public Expression {
  241. public:
  242. UnaryOperation(UnaryOperator operation, Tree operand)
  243. : m_operation(operation)
  244. , m_operand(move(operand))
  245. {
  246. }
  247. Vector<NodeSubtreePointer> subtrees() override;
  248. UnaryOperator m_operation;
  249. Tree m_operand;
  250. protected:
  251. void dump_tree(StringBuilder& builder) override;
  252. };
  253. class IsOneOfOperation : public Expression {
  254. public:
  255. IsOneOfOperation(Tree operand, Vector<Tree>&& compare_values)
  256. : m_operand(move(operand))
  257. , m_compare_values(move(compare_values))
  258. {
  259. }
  260. Vector<NodeSubtreePointer> subtrees() override;
  261. Tree m_operand;
  262. Vector<Tree> m_compare_values;
  263. protected:
  264. void dump_tree(StringBuilder& builder) override;
  265. };
  266. class UnresolvedReference : public Expression {
  267. public:
  268. UnresolvedReference(StringView name)
  269. : m_name(name)
  270. {
  271. }
  272. StringView m_name;
  273. protected:
  274. void dump_tree(StringBuilder& builder) override;
  275. };
  276. class ReturnNode : public Node {
  277. public:
  278. ReturnNode(Tree return_value)
  279. : m_return_value(move(return_value))
  280. {
  281. }
  282. Vector<NodeSubtreePointer> subtrees() override;
  283. Tree m_return_value;
  284. protected:
  285. void dump_tree(StringBuilder& builder) override;
  286. };
  287. // Although assert might seems a good candidate for ControlFlowOperator, we are not interested in
  288. // tracking control flow after a failed assertion.
  289. class AssertExpression : public Expression {
  290. public:
  291. AssertExpression(Tree condition)
  292. : m_condition(move(condition))
  293. {
  294. }
  295. Vector<NodeSubtreePointer> subtrees() override;
  296. Tree m_condition;
  297. protected:
  298. void dump_tree(StringBuilder& builder) override;
  299. };
  300. class IfBranch : public Node {
  301. public:
  302. IfBranch(Tree condition, Tree branch)
  303. : m_condition(move(condition))
  304. , m_branch(move(branch))
  305. {
  306. }
  307. Vector<NodeSubtreePointer> subtrees() override;
  308. Tree m_condition;
  309. Tree m_branch;
  310. protected:
  311. void dump_tree(StringBuilder& builder) override;
  312. };
  313. class ElseIfBranch : public Node {
  314. public:
  315. ElseIfBranch(NullableTree condition, Tree branch)
  316. : m_condition(move(condition))
  317. , m_branch(move(branch))
  318. {
  319. }
  320. Vector<NodeSubtreePointer> subtrees() override;
  321. NullableTree m_condition;
  322. Tree m_branch;
  323. protected:
  324. void dump_tree(StringBuilder& builder) override;
  325. };
  326. class IfElseIfChain : public Statement {
  327. public:
  328. IfElseIfChain(Vector<Tree>&& conditions, Vector<Tree>&& branches, NullableTree else_branch)
  329. : m_conditions(move(conditions))
  330. , m_branches(move(branches))
  331. , m_else_branch(move(else_branch))
  332. {
  333. VERIFY(m_branches.size() == m_conditions.size());
  334. }
  335. Vector<NodeSubtreePointer> subtrees() override;
  336. // Excluding else branch, if one is present
  337. size_t branches_count() const { return m_branches.size(); }
  338. Vector<Tree> m_conditions;
  339. Vector<Tree> m_branches;
  340. NullableTree m_else_branch;
  341. protected:
  342. void dump_tree(StringBuilder& builder) override;
  343. };
  344. class TreeList : public Statement {
  345. public:
  346. TreeList(Vector<Tree>&& trees);
  347. Vector<NodeSubtreePointer> subtrees() override;
  348. bool is_list() const override { return true; }
  349. Vector<Tree> m_trees;
  350. protected:
  351. void dump_tree(StringBuilder& builder) override;
  352. };
  353. class RecordDirectListInitialization : public Expression {
  354. public:
  355. struct Argument {
  356. Tree name;
  357. Tree value;
  358. };
  359. RecordDirectListInitialization(Tree type_reference, Vector<Argument>&& arguments)
  360. : m_type_reference(move(type_reference))
  361. , m_arguments(move(arguments))
  362. {
  363. }
  364. Vector<NodeSubtreePointer> subtrees() override;
  365. Tree m_type_reference;
  366. Vector<Argument> m_arguments;
  367. protected:
  368. void dump_tree(StringBuilder& builder) override;
  369. };
  370. class FunctionCall : public Expression {
  371. public:
  372. FunctionCall(Tree name, Vector<Tree>&& arguments)
  373. : m_name(move(name))
  374. , m_arguments(move(arguments))
  375. {
  376. }
  377. Vector<NodeSubtreePointer> subtrees() override;
  378. Tree m_name;
  379. Vector<Tree> m_arguments;
  380. protected:
  381. void dump_tree(StringBuilder& builder) override;
  382. };
  383. class SlotName : public Expression {
  384. public:
  385. SlotName(StringView member_name)
  386. : m_member_name(member_name)
  387. {
  388. }
  389. StringView m_member_name;
  390. protected:
  391. void dump_tree(StringBuilder& builder) override;
  392. };
  393. class Variable : public Expression {
  394. public:
  395. Variable(NamedVariableDeclarationRef name)
  396. : m_name(move(name))
  397. {
  398. }
  399. NamedVariableDeclarationRef m_name;
  400. SSAVariableDeclarationRef m_ssa;
  401. String name() const;
  402. protected:
  403. void dump_tree(StringBuilder& builder) override;
  404. };
  405. class Enumerator : public Expression {
  406. public:
  407. Enumerator(Badge<TranslationUnit>, StringView value)
  408. : m_value(value)
  409. {
  410. }
  411. protected:
  412. void dump_tree(StringBuilder& builder) override;
  413. private:
  414. StringView m_value;
  415. };
  416. class FunctionPointer : public Expression {
  417. public:
  418. FunctionPointer(FunctionDeclarationRef declaration)
  419. : m_declaration(declaration)
  420. {
  421. }
  422. FunctionDeclarationRef m_declaration;
  423. protected:
  424. void dump_tree(StringBuilder& builder) override;
  425. };
  426. }
  427. namespace AK {
  428. template<>
  429. struct Formatter<JSSpecCompiler::Tree> : Formatter<StringView> {
  430. ErrorOr<void> format(FormatBuilder& builder, JSSpecCompiler::Tree const& tree)
  431. {
  432. tree->format_tree(builder.builder());
  433. return {};
  434. }
  435. };
  436. }