Function.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/HashMap.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/StringView.h>
  11. #include "DiagnosticEngine.h"
  12. #include "Forward.h"
  13. namespace JSSpecCompiler {
  14. class TranslationUnit {
  15. public:
  16. TranslationUnit(StringView filename);
  17. ~TranslationUnit();
  18. void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
  19. FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
  20. FunctionDeclarationRef find_declaration_by_name(StringView name) const;
  21. StringView filename() const { return m_filename; }
  22. DiagnosticEngine& diag() { return m_diagnostic_engine; }
  23. Vector<FunctionDefinitionRef> functions_to_compile() const { return m_functions_to_compile; }
  24. private:
  25. StringView m_filename;
  26. DiagnosticEngine m_diagnostic_engine;
  27. Vector<FunctionDefinitionRef> m_functions_to_compile;
  28. Vector<NonnullRefPtr<FunctionDeclaration>> m_declarations_owner;
  29. HashMap<StringView, FunctionDeclarationRef> m_function_index;
  30. };
  31. struct FunctionArgument {
  32. StringView name;
  33. };
  34. class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
  35. public:
  36. FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments);
  37. virtual ~FunctionDeclaration() = default;
  38. TranslationUnitRef m_translation_unit = nullptr;
  39. StringView m_name;
  40. Vector<FunctionArgument> m_arguments;
  41. };
  42. class FunctionDefinition : public FunctionDeclaration {
  43. public:
  44. FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments);
  45. void reindex_ssa_variables();
  46. Tree m_ast;
  47. // Populates during reference resolving
  48. // NOTE: The hash map here is ordered since we do not want random hash changes to break our test
  49. // expectations (looking at you, SipHash).
  50. OrderedHashMap<StringView, NamedVariableDeclarationRef> m_local_variables;
  51. // Fields populate during CFG building
  52. NamedVariableDeclarationRef m_named_return_value;
  53. RefPtr<ControlFlowGraph> m_cfg;
  54. // Fields populate during SSA building
  55. Vector<SSAVariableDeclarationRef> m_ssa_arguments;
  56. SSAVariableDeclarationRef m_return_value;
  57. Vector<SSAVariableDeclarationRef> m_local_ssa_variables;
  58. };
  59. }