2023-08-20 15:05:48 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/RefPtr.h>
|
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
|
2024-01-18 23:09:53 -03:00
|
|
|
#include "DiagnosticEngine.h"
|
2023-08-20 15:05:48 -03:00
|
|
|
#include "Forward.h"
|
|
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
2024-01-18 23:08:50 -03:00
|
|
|
class TranslationUnit {
|
|
|
|
|
public:
|
|
|
|
|
TranslationUnit(StringView filename);
|
|
|
|
|
~TranslationUnit();
|
|
|
|
|
|
2023-10-02 14:34:00 -03:00
|
|
|
void adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration);
|
|
|
|
|
FunctionDefinitionRef adopt_function(NonnullRefPtr<FunctionDefinition>&& definition);
|
2023-09-19 11:29:57 -03:00
|
|
|
|
2024-01-18 23:08:50 -03:00
|
|
|
FunctionDeclarationRef find_declaration_by_name(StringView name) const;
|
|
|
|
|
|
|
|
|
|
StringView filename() const { return m_filename; }
|
2024-01-18 23:09:53 -03:00
|
|
|
DiagnosticEngine& diag() { return m_diagnostic_engine; }
|
2024-01-18 23:08:50 -03:00
|
|
|
Vector<FunctionDefinitionRef> functions_to_compile() const { return m_functions_to_compile; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StringView m_filename;
|
2024-01-18 23:09:53 -03:00
|
|
|
DiagnosticEngine m_diagnostic_engine;
|
2024-01-18 23:08:50 -03:00
|
|
|
Vector<FunctionDefinitionRef> m_functions_to_compile;
|
|
|
|
|
Vector<NonnullRefPtr<FunctionDeclaration>> m_declarations_owner;
|
|
|
|
|
HashMap<StringView, FunctionDeclarationRef> m_function_index;
|
2023-08-20 15:05:48 -03:00
|
|
|
};
|
|
|
|
|
|
2024-01-16 01:02:35 -03:00
|
|
|
struct FunctionArgument {
|
|
|
|
|
StringView name;
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-19 11:29:57 -03:00
|
|
|
class FunctionDeclaration : public RefCounted<FunctionDeclaration> {
|
2023-08-20 15:05:48 -03:00
|
|
|
public:
|
2024-01-16 01:02:35 -03:00
|
|
|
FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments);
|
2023-08-20 15:05:48 -03:00
|
|
|
|
2023-09-19 11:29:57 -03:00
|
|
|
virtual ~FunctionDeclaration() = default;
|
|
|
|
|
|
2023-09-29 20:43:19 -03:00
|
|
|
TranslationUnitRef m_translation_unit = nullptr;
|
2023-08-20 15:05:48 -03:00
|
|
|
StringView m_name;
|
2024-01-16 01:02:35 -03:00
|
|
|
Vector<FunctionArgument> m_arguments;
|
2023-09-19 11:29:57 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FunctionDefinition : public FunctionDeclaration {
|
|
|
|
|
public:
|
2024-01-16 01:02:35 -03:00
|
|
|
FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments);
|
2023-09-19 11:29:57 -03:00
|
|
|
|
2023-10-01 23:32:10 -03:00
|
|
|
void reindex_ssa_variables();
|
|
|
|
|
|
2023-08-20 15:05:48 -03:00
|
|
|
Tree m_ast;
|
2023-10-21 22:18:58 -03:00
|
|
|
|
|
|
|
|
// Populates during reference resolving
|
2023-10-01 23:32:10 -03:00
|
|
|
// NOTE: The hash map here is ordered since we do not want random hash changes to break our test
|
|
|
|
|
// expectations (looking at you, SipHash).
|
|
|
|
|
OrderedHashMap<StringView, NamedVariableDeclarationRef> m_local_variables;
|
2023-10-21 22:18:58 -03:00
|
|
|
|
|
|
|
|
// Fields populate during CFG building
|
|
|
|
|
NamedVariableDeclarationRef m_named_return_value;
|
2023-08-19 15:24:50 -03:00
|
|
|
RefPtr<ControlFlowGraph> m_cfg;
|
2023-10-21 22:18:58 -03:00
|
|
|
|
|
|
|
|
// Fields populate during SSA building
|
2024-01-16 01:02:35 -03:00
|
|
|
Vector<SSAVariableDeclarationRef> m_ssa_arguments;
|
2023-10-21 22:18:58 -03:00
|
|
|
SSAVariableDeclarationRef m_return_value;
|
|
|
|
|
Vector<SSAVariableDeclarationRef> m_local_ssa_variables;
|
2023-08-20 15:05:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|