2023-08-20 15:05:48 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Function.h"
|
|
|
|
|
#include "AST/AST.h"
|
2023-08-19 15:24:50 -03:00
|
|
|
#include "Compiler/ControlFlowGraph.h"
|
2023-08-20 15:05:48 -03:00
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
2024-01-18 23:08:50 -03:00
|
|
|
TranslationUnit::TranslationUnit(StringView filename)
|
|
|
|
|
: m_filename(filename)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TranslationUnit::~TranslationUnit() = default;
|
|
|
|
|
|
2023-10-02 14:34:00 -03:00
|
|
|
void TranslationUnit::adopt_declaration(NonnullRefPtr<FunctionDeclaration>&& declaration)
|
2023-09-19 11:29:57 -03:00
|
|
|
{
|
2023-10-02 14:34:00 -03:00
|
|
|
declaration->m_translation_unit = this;
|
2024-01-18 23:08:50 -03:00
|
|
|
m_function_index.set(declaration->m_name, declaration.ptr());
|
|
|
|
|
m_declarations_owner.append(move(declaration));
|
2023-10-02 14:34:00 -03:00
|
|
|
}
|
2023-09-19 11:29:57 -03:00
|
|
|
|
2024-01-18 23:08:50 -03:00
|
|
|
FunctionDefinitionRef TranslationUnit::adopt_function(NonnullRefPtr<FunctionDefinition>&& definition)
|
2023-10-02 14:34:00 -03:00
|
|
|
{
|
2024-01-18 23:08:50 -03:00
|
|
|
FunctionDefinitionRef result = definition.ptr();
|
|
|
|
|
m_functions_to_compile.append(result);
|
|
|
|
|
adopt_declaration(definition);
|
2023-09-19 11:29:57 -03:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 23:08:50 -03:00
|
|
|
FunctionDeclarationRef TranslationUnit::find_declaration_by_name(StringView name) const
|
|
|
|
|
{
|
|
|
|
|
auto it = m_function_index.find(name);
|
|
|
|
|
if (it == m_function_index.end())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return it->value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 01:02:35 -03:00
|
|
|
FunctionDeclaration::FunctionDeclaration(StringView name, Vector<FunctionArgument>&& arguments)
|
2023-09-19 11:29:57 -03:00
|
|
|
: m_name(name)
|
2024-01-16 01:02:35 -03:00
|
|
|
, m_arguments(arguments)
|
2023-09-19 11:29:57 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 01:02:35 -03:00
|
|
|
FunctionDefinition::FunctionDefinition(StringView name, Tree ast, Vector<FunctionArgument>&& arguments)
|
|
|
|
|
: FunctionDeclaration(name, move(arguments))
|
2023-08-20 15:05:48 -03:00
|
|
|
, m_ast(move(ast))
|
2023-10-21 22:18:58 -03:00
|
|
|
, m_named_return_value(make_ref_counted<NamedVariableDeclaration>("$return"sv))
|
2023-08-20 15:05:48 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 23:32:10 -03:00
|
|
|
void FunctionDefinition::reindex_ssa_variables()
|
|
|
|
|
{
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
for (auto const& var : m_local_ssa_variables)
|
|
|
|
|
var->m_index = index++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 15:05:48 -03:00
|
|
|
}
|