AST.h 11 KB

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