Function.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. EnumeratorRef get_node_for_enumerator_value(StringView value);
  25. private:
  26. StringView m_filename;
  27. DiagnosticEngine m_diagnostic_engine;
  28. Vector<FunctionDefinitionRef> m_functions_to_compile;
  29. Vector<NonnullRefPtr<FunctionDeclaration>> m_declarations_owner;
  30. HashMap<StringView, FunctionDeclarationRef> m_function_index;
  31. HashMap<StringView, EnumeratorRef> m_enumerator_nodes;
  32. };
  33. struct FunctionArgument {
  34. StringView name;
  35. size_t optional_arguments_group;
  36. };
  37. class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
  38. public:
  39. FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments);
  40. virtual ~FunctionDeclaration() = default;
  41. TranslationUnitRef m_translation_unit = nullptr;
  42. StringView m_name;
  43. Vector<FunctionArgument> m_arguments;
  44. };
  45. class FunctionDefinition : public FunctionDeclaration {
  46. public:
  47. FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments);
  48. void reindex_ssa_variables();
  49. Tree m_ast;
  50. // Populates during reference resolving
  51. // NOTE: The hash map here is ordered since we do not want random hash changes to break our test
  52. // expectations (looking at you, SipHash).
  53. OrderedHashMap<StringView, NamedVariableDeclarationRef> m_local_variables;
  54. // Fields populate during CFG building
  55. NamedVariableDeclarationRef m_named_return_value;
  56. RefPtr<ControlFlowGraph> m_cfg;
  57. // Fields populate during SSA building
  58. Vector<SSAVariableDeclarationRef> m_ssa_arguments;
  59. SSAVariableDeclarationRef m_return_value;
  60. Vector<SSAVariableDeclarationRef> m_local_ssa_variables;
  61. };
  62. }