101 lines
3.4 KiB
C++
101 lines
3.4 KiB
C++
// This Source Code Form is subject to the terms of the Mozilla Public License
|
|
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
|
// obtain one at http://mozilla.org/MPL/2.0/.
|
|
#pragma once
|
|
|
|
#include <list>
|
|
#include "elna/source/ast.h"
|
|
#include "elna/source/symbol_table.h"
|
|
|
|
namespace elna
|
|
{
|
|
namespace source
|
|
{
|
|
class name_analysis_visitor final : public empty_visitor
|
|
{
|
|
std::shared_ptr<symbol_table> table;
|
|
const char *filename;
|
|
std::list<std::unique_ptr<error>> m_errors;
|
|
const std::size_t pointer_size;
|
|
|
|
std::shared_ptr<const type> convert_declaration_type(const type_expression& ast_type) const;
|
|
|
|
public:
|
|
/**
|
|
* \param table Symbol table.
|
|
* \param path Source filename.
|
|
* \param target_pointer_size Pointer size on the target platform.
|
|
*/
|
|
name_analysis_visitor(std::shared_ptr<symbol_table> table, const char *filename,
|
|
const std::size_t target_pointer_size);
|
|
|
|
/**
|
|
* \return Collected errors.
|
|
*/
|
|
const std::list<std::unique_ptr<error>>& errors() const noexcept;
|
|
|
|
void visit(constant_definition *definition) override;
|
|
void visit(declaration *declaration) override;
|
|
void visit(program *program) override;
|
|
void visit(procedure_definition *procedure) override;
|
|
};
|
|
|
|
/**
|
|
* Visitor which allocates registers and stack space for variables and
|
|
* parameters.
|
|
*/
|
|
class allocator_visitor final : public empty_visitor
|
|
{
|
|
std::ptrdiff_t local_offset;
|
|
std::ptrdiff_t argument_offset;
|
|
std::shared_ptr<symbol_table> table;
|
|
|
|
public:
|
|
allocator_visitor(std::shared_ptr<symbol_table> table);
|
|
|
|
void visit(declaration *declaration) override;
|
|
void visit(program *program) override;
|
|
void visit(procedure_definition *procedure) override;
|
|
void visit(call_statement *statement) override;
|
|
};
|
|
|
|
/**
|
|
* This visitor performs the type checking.
|
|
*/
|
|
class type_analysis_visitor final : public empty_visitor
|
|
{
|
|
std::shared_ptr<symbol_table> table;
|
|
const char *filename;
|
|
const std::size_t pointer_size;
|
|
std::list<std::unique_ptr<error>> m_errors;
|
|
|
|
public:
|
|
/**
|
|
* \param table Symbol table.
|
|
* \param path Source filename.
|
|
* \param target_pointer_size Pointer size on the target platform.
|
|
*/
|
|
type_analysis_visitor(std::shared_ptr<symbol_table> table, const char *filename,
|
|
const std::size_t target_pointer_size);
|
|
|
|
/**
|
|
* \return Collected errors.
|
|
*/
|
|
const std::list<std::unique_ptr<error>>& errors() const noexcept;
|
|
|
|
void visit(program *program) override;
|
|
void visit(procedure_definition *procedure) override;
|
|
void visit(integer_literal *literal) override;
|
|
void visit(boolean_literal *literal) override;
|
|
void visit(variable_expression *expression) override;
|
|
void visit(unary_expression *expression) override;
|
|
void visit(binary_expression *expression) override;
|
|
void visit(call_statement *statement) override;
|
|
void visit(constant_definition *definition) override;
|
|
void visit(while_statement *statement) override;
|
|
void visit(if_statement *statement) override;
|
|
void visit(assign_statement *statement) override;
|
|
};
|
|
}
|
|
}
|