Function.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
  32. public:
  33. FunctionDeclaration(StringView name);
  34. virtual ~FunctionDeclaration() = default;
  35. TranslationUnitRef m_translation_unit = nullptr;
  36. StringView m_name;
  37. };
  38. class FunctionDefinition : public FunctionDeclaration {
  39. public:
  40. FunctionDefinition(StringView name, Tree ast, Vector<StringView>&& argument_names);
  41. void reindex_ssa_variables();
  42. Tree m_ast;
  43. Vector<StringView> m_argument_names;
  44. // Populates during reference resolving
  45. // NOTE: The hash map here is ordered since we do not want random hash changes to break our test
  46. // expectations (looking at you, SipHash).
  47. OrderedHashMap<StringView, NamedVariableDeclarationRef> m_local_variables;
  48. // Fields populate during CFG building
  49. NamedVariableDeclarationRef m_named_return_value;
  50. RefPtr<ControlFlowGraph> m_cfg;
  51. // Fields populate during SSA building
  52. Vector<SSAVariableDeclarationRef> m_arguments;
  53. SSAVariableDeclarationRef m_return_value;
  54. Vector<SSAVariableDeclarationRef> m_local_ssa_variables;
  55. };
  56. }