Type check pointer dereferencing

This commit is contained in:
2024-05-16 21:01:11 +02:00
parent 30e84f1cdd
commit 3f13adcfe7
9 changed files with 185 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include <boost/core/noncopyable.hpp>
#include <memory>
#include <elna/source/lexer.hpp>
#include "elna/source/types.hpp"
namespace elna::source
{
@ -173,6 +174,7 @@ namespace elna::source
{
public:
std::shared_ptr<operand> place;
std::shared_ptr<const type> data_type;
protected:
/**

View File

@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <type_traits>
#include "elna/source/types.hpp"
namespace elna::source
{
@ -123,6 +124,32 @@ namespace elna::source
std::string what() const override;
};
struct type_mismatch final : public error
{
/**
* Kind of the operation on the type.
*/
enum class operation
{
dereference,
};
/**
* \param name Given type.
* \param kind Kind of the operation on the type.
* \param path Source file name.
* \param position Operation position.
*/
type_mismatch(std::shared_ptr<const type> got, operation kind, const std::filesystem::path& path,
const struct position position);
std::string what() const override;
private:
std::shared_ptr<const type> got;
operation kind;
};
template<typename T>
struct writer
{

View File

@ -7,12 +7,23 @@ namespace elna::source
{
class name_analysis_visitor final : public empty_visitor
{
std::shared_ptr<symbol_table> table = std::make_shared<symbol_table>();
std::shared_ptr<symbol_table> table;
const std::filesystem::path filename;
std::list<std::unique_ptr<error>> m_errors;
std::shared_ptr<const type> convert_declaration_type(const type_expression& ast_type) const;
public:
name_analysis_visitor(std::shared_ptr<symbol_table> table);
/**
* \param table Symbol table.
* \param path Source filename.
*/
name_analysis_visitor(std::shared_ptr<symbol_table> table, const std::filesystem::path& filename);
/**
* \return Collected errors.
*/
const std::list<std::unique_ptr<error>>& errors() const noexcept;
void visit(constant_definition *definition) override;
void visit(declaration *declaration) override;
@ -44,5 +55,29 @@ namespace elna::source
*/
class type_analysis_visitor final : public empty_visitor
{
std::shared_ptr<symbol_table> table;
const std::filesystem::path 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 std::filesystem::path& filename,
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 *definition) override;
void visit(integer_literal *literal) override;
void visit(boolean_literal *literal) override;
void visit(unary_expression *expression) override;
};
}

View File

@ -8,12 +8,23 @@ namespace elna::source
/**
* Type representation.
*/
struct type
class type
{
const std::size_t byte_size;
protected:
/**
* Constructor.
*
* \param byte_size The type size in bytes.
*/
explicit type(const std::size_t byte_size);
public:
/**
* \return The type size in bytes.
*/
virtual std::size_t size() const noexcept;
};
/**
@ -21,8 +32,15 @@ namespace elna::source
*/
struct primitive_type : public type
{
/// Type name.
const std::string type_name;
/**
* Constructor.
*
* \param type_name Type name.
* \param byte_size The type size in bytes.
*/
primitive_type(const std::string& type_name, const std::size_t byte_size);
};
@ -31,8 +49,15 @@ namespace elna::source
*/
struct pointer_type : public type
{
/// Pointer target type.
std::shared_ptr<const type> base_type;
/**
* Constructor.
*
* \param base_type Pointer target type.
* \param byte_size The type size in bytes.
*/
pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size);
};