CppASTConverter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Parser/CppASTConverter.h"
  7. #include "Function.h"
  8. #include "Parser/SpecParser.h"
  9. namespace JSSpecCompiler {
  10. NonnullRefPtr<FunctionDefinition> CppASTConverter::convert()
  11. {
  12. StringView name = m_function->name()->full_name();
  13. Vector<Tree> toplevel_statements;
  14. for (auto const& statement : m_function->definition()->statements()) {
  15. auto maybe_tree = as_nullable_tree(statement);
  16. if (maybe_tree)
  17. toplevel_statements.append(maybe_tree.release_nonnull());
  18. }
  19. auto tree = make_ref_counted<TreeList>(move(toplevel_statements));
  20. return make_ref_counted<FunctionDefinition>(name, tree);
  21. }
  22. template<>
  23. NullableTree CppASTConverter::convert_node(Cpp::VariableDeclaration const& variable_declaration)
  24. {
  25. static Tree variable_declaration_present_error
  26. = make_ref_counted<ErrorNode>("Encountered variable declaration with initial value"sv);
  27. if (variable_declaration.initial_value() != nullptr)
  28. return variable_declaration_present_error;
  29. return nullptr;
  30. }
  31. template<>
  32. NullableTree CppASTConverter::convert_node(Cpp::ReturnStatement const& return_statement)
  33. {
  34. return make_ref_counted<ReturnNode>(as_tree(return_statement.value()));
  35. }
  36. template<>
  37. NullableTree CppASTConverter::convert_node(Cpp::FunctionCall const& function_call)
  38. {
  39. Vector<Tree> arguments;
  40. for (auto const& argument : function_call.arguments())
  41. arguments.append(as_tree(argument));
  42. return make_ref_counted<FunctionCall>(as_tree(function_call.callee()), move(arguments));
  43. }
  44. template<>
  45. NullableTree CppASTConverter::convert_node(Cpp::Name const& name)
  46. {
  47. return make_ref_counted<UnresolvedReference>(name.full_name());
  48. }
  49. template<>
  50. NullableTree CppASTConverter::convert_node(Cpp::IfStatement const& if_statement)
  51. {
  52. // NOTE: This is so complicated since we probably want to test IfBranchMergingPass, which
  53. // expects standalone `IfBranch` and `ElseIfBranch` nodes.
  54. Vector<Tree> trees;
  55. Cpp::IfStatement const* current = &if_statement;
  56. while (true) {
  57. auto predicate = as_tree(current->predicate());
  58. auto then_branch = as_possibly_empty_tree(current->then_statement());
  59. if (trees.is_empty())
  60. trees.append(make_ref_counted<IfBranch>(predicate, then_branch));
  61. else
  62. trees.append(make_ref_counted<ElseIfBranch>(predicate, then_branch));
  63. auto else_statement = dynamic_cast<Cpp::IfStatement const*>(current->else_statement());
  64. if (else_statement)
  65. current = else_statement;
  66. else
  67. break;
  68. }
  69. auto else_statement = current->else_statement();
  70. if (else_statement)
  71. trees.append(make_ref_counted<ElseIfBranch>(
  72. nullptr, as_possibly_empty_tree(else_statement)));
  73. return make_ref_counted<TreeList>(move(trees));
  74. }
  75. template<>
  76. NullableTree CppASTConverter::convert_node(Cpp::BlockStatement const& block)
  77. {
  78. Vector<Tree> statements;
  79. for (auto const& statement : block.statements()) {
  80. auto maybe_tree = as_nullable_tree(statement);
  81. if (maybe_tree)
  82. statements.append(maybe_tree.release_nonnull());
  83. }
  84. return make_ref_counted<TreeList>(move(statements));
  85. }
  86. template<>
  87. NullableTree CppASTConverter::convert_node(Cpp::AssignmentExpression const& assignment)
  88. {
  89. // NOTE: Later stages of the compilation process basically treat `BinaryOperator::Declaration`
  90. // the same as `BinaryOperator::Assignment`, so variable shadowing is impossible. The only
  91. // difference in their semantics is that "declarations" define names of local variables.
  92. // Since we are effectively ignoring actual C++ variable declarations, we need to define
  93. // locals somewhere else. Using "declarations" instead of "assignments" here does this job
  94. // cleanly.
  95. return make_ref_counted<BinaryOperation>(
  96. BinaryOperator::Declaration, as_tree(assignment.lhs()), as_tree(assignment.rhs()));
  97. }
  98. template<>
  99. NullableTree CppASTConverter::convert_node(Cpp::NumericLiteral const& literal)
  100. {
  101. // TODO: Numerical literals are not limited to i64.
  102. return make_ref_counted<MathematicalConstant>(literal.value().to_int<i64>().value());
  103. }
  104. template<>
  105. NullableTree CppASTConverter::convert_node(Cpp::StringLiteral const& literal)
  106. {
  107. return make_ref_counted<StringLiteral>(literal.value());
  108. }
  109. template<>
  110. NullableTree CppASTConverter::convert_node(Cpp::BinaryExpression const& expression)
  111. {
  112. static constexpr auto operator_translation = []() consteval {
  113. Array<BinaryOperator, to_underlying(Cpp::BinaryOp::Arrow) + 1> table;
  114. #define ASSIGN_TRANSLATION(cpp_name, our_name) \
  115. table[to_underlying(Cpp::BinaryOp::cpp_name)] = BinaryOperator::our_name
  116. ASSIGN_TRANSLATION(Addition, Plus);
  117. ASSIGN_TRANSLATION(Subtraction, Minus);
  118. ASSIGN_TRANSLATION(Multiplication, Multiplication);
  119. ASSIGN_TRANSLATION(Division, Division);
  120. ASSIGN_TRANSLATION(Modulo, Invalid);
  121. ASSIGN_TRANSLATION(GreaterThan, CompareGreater);
  122. ASSIGN_TRANSLATION(GreaterThanEquals, Invalid);
  123. ASSIGN_TRANSLATION(LessThan, CompareLess);
  124. ASSIGN_TRANSLATION(LessThanEquals, Invalid);
  125. ASSIGN_TRANSLATION(BitwiseAnd, Invalid);
  126. ASSIGN_TRANSLATION(BitwiseOr, Invalid);
  127. ASSIGN_TRANSLATION(BitwiseXor, Invalid);
  128. ASSIGN_TRANSLATION(LeftShift, Invalid);
  129. ASSIGN_TRANSLATION(RightShift, Invalid);
  130. ASSIGN_TRANSLATION(EqualsEquals, CompareEqual);
  131. ASSIGN_TRANSLATION(NotEqual, CompareNotEqual);
  132. ASSIGN_TRANSLATION(LogicalOr, Invalid);
  133. ASSIGN_TRANSLATION(LogicalAnd, Invalid);
  134. ASSIGN_TRANSLATION(Arrow, Invalid);
  135. #undef ASSIGN_TRANSLATION
  136. return table;
  137. }();
  138. auto translated_operator = operator_translation[to_underlying(expression.op())];
  139. // TODO: Print nicer error.
  140. VERIFY(translated_operator != BinaryOperator::Invalid);
  141. return make_ref_counted<BinaryOperation>(translated_operator, as_tree(expression.lhs()), as_tree(expression.rhs()));
  142. }
  143. NullableTree CppASTConverter::as_nullable_tree(Cpp::Statement const* statement)
  144. {
  145. static Tree unknown_ast_node_error
  146. = make_ref_counted<ErrorNode>("Encountered unknown C++ AST node"sv);
  147. Optional<NullableTree> result;
  148. auto dispatch_convert_if_one_of = [&]<typename... Ts> {
  149. (([&]<typename T> {
  150. if (result.has_value())
  151. return;
  152. auto casted_ptr = dynamic_cast<T const*>(statement);
  153. if (casted_ptr != nullptr)
  154. result = convert_node<T>(*casted_ptr);
  155. }).template operator()<Ts>(),
  156. ...);
  157. };
  158. dispatch_convert_if_one_of.operator()<
  159. Cpp::VariableDeclaration,
  160. Cpp::ReturnStatement,
  161. Cpp::FunctionCall,
  162. Cpp::Name,
  163. Cpp::IfStatement,
  164. Cpp::BlockStatement,
  165. Cpp::AssignmentExpression,
  166. Cpp::NumericLiteral,
  167. Cpp::StringLiteral,
  168. Cpp::BinaryExpression>();
  169. if (result.has_value())
  170. return *result;
  171. return unknown_ast_node_error;
  172. }
  173. Tree CppASTConverter::as_tree(Cpp::Statement const* statement)
  174. {
  175. static Tree empty_tree_error
  176. = make_ref_counted<ErrorNode>("AST conversion unexpectedly produced empty tree"sv);
  177. auto result = as_nullable_tree(statement);
  178. if (result)
  179. return result.release_nonnull();
  180. return empty_tree_error;
  181. }
  182. Tree CppASTConverter::as_possibly_empty_tree(Cpp::Statement const* statement)
  183. {
  184. auto result = as_nullable_tree(statement);
  185. if (result)
  186. return result.release_nonnull();
  187. return make_ref_counted<TreeList>(Vector<Tree> {});
  188. }
  189. }