Use colon instead of as to cast

This commit is contained in:
2025-02-14 08:05:03 +01:00
parent c564847c6b
commit ee4ebf64b9
10 changed files with 488 additions and 290 deletions

View File

@ -41,7 +41,8 @@ namespace boot
less_equal,
greater_equal,
disjunction,
conjunction
conjunction,
exclusive_disjunction
};
enum class unary_operator
@ -57,7 +58,7 @@ namespace boot
class type_definition;
class call_expression;
class cast_expression;
class size_of_expression;
class type_expression;
class assign_statement;
class if_statement;
class while_statement;
@ -67,11 +68,11 @@ namespace boot
class program;
class binary_expression;
class unary_expression;
class basic_type_expression;
class array_type_expression;
class pointer_type_expression;
class record_type_expression;
class union_type_expression;
class basic_type;
class array_type;
class pointer_type;
class record_type;
class union_type;
class variable_expression;
class array_access_expression;
class field_access_expression;
@ -91,7 +92,7 @@ namespace boot
virtual void visit(type_definition *) = 0;
virtual void visit(call_expression *) = 0;
virtual void visit(cast_expression *) = 0;
virtual void visit(size_of_expression *) = 0;
virtual void visit(type_expression *) = 0;
virtual void visit(call_statement *) = 0;
virtual void visit(assign_statement *) = 0;
virtual void visit(if_statement *) = 0;
@ -102,11 +103,11 @@ namespace boot
virtual void visit(program *) = 0;
virtual void visit(binary_expression *) = 0;
virtual void visit(unary_expression *) = 0;
virtual void visit(basic_type_expression *) = 0;
virtual void visit(array_type_expression *) = 0;
virtual void visit(pointer_type_expression *) = 0;
virtual void visit(record_type_expression *) = 0;
virtual void visit(union_type_expression *) = 0;
virtual void visit(basic_type *) = 0;
virtual void visit(array_type *) = 0;
virtual void visit(pointer_type *) = 0;
virtual void visit(record_type *) = 0;
virtual void visit(union_type *) = 0;
virtual void visit(variable_expression *) = 0;
virtual void visit(array_access_expression *) = 0;
virtual void visit(field_access_expression *is_field_access) = 0;
@ -131,7 +132,7 @@ namespace boot
virtual void visit(type_definition *definition) override;
virtual void visit(call_expression *expression) override;
virtual void visit(cast_expression *expression) override;
virtual void visit(size_of_expression *expression) override;
virtual void visit(type_expression *expression) override;
virtual void visit(call_statement *statement) override;
virtual void visit(assign_statement *statement) override;
virtual void visit(if_statement *) override;
@ -142,11 +143,11 @@ namespace boot
virtual void visit(program *program) override;
virtual void visit(binary_expression *expression) override;
virtual void visit(unary_expression *expression) override;
virtual void visit(basic_type_expression *) override;
virtual void visit(array_type_expression *expression) override;
virtual void visit(pointer_type_expression *) override;
virtual void visit(record_type_expression *expression) override;
virtual void visit(union_type_expression *expression) override;
virtual void visit(basic_type *) override;
virtual void visit(array_type *expression) override;
virtual void visit(pointer_type *) override;
virtual void visit(record_type *expression) override;
virtual void visit(union_type *expression) override;
virtual void visit(variable_expression *) override;
virtual void visit(array_access_expression *expression) override;
virtual void visit(field_access_expression *expression) override;
@ -217,23 +218,23 @@ namespace boot
/**
* Some type expression.
*/
class type_expression : public node
class top_type : public node
{
public:
virtual basic_type_expression *is_basic();
virtual array_type_expression *is_array();
virtual pointer_type_expression *is_pointer();
virtual record_type_expression *is_record();
virtual union_type_expression *is_union();
virtual basic_type *is_basic();
virtual array_type *is_array();
virtual pointer_type *is_pointer();
virtual record_type *is_record();
virtual union_type *is_union();
protected:
type_expression(const struct position position);
top_type(const struct position position);
};
/**
* Expression defining a basic type.
*/
class basic_type_expression final : public type_expression
class basic_type : public top_type
{
const std::string m_name;
@ -242,76 +243,76 @@ namespace boot
* \param position Source code position.
* \param name Type name.
*/
basic_type_expression(const struct position position, const std::string& name);
basic_type(const struct position position, const std::string& name);
virtual void accept(parser_visitor *visitor) override;
const std::string& base_name();
basic_type_expression *is_basic() override;
basic_type *is_basic() override;
};
class array_type_expression final : public type_expression
class array_type : public top_type
{
type_expression *m_base;
top_type *m_base;
public:
const std::uint32_t size;
array_type_expression(const struct position position, type_expression *base, const std::uint32_t size);
array_type(const struct position position, top_type*base, const std::uint32_t size);
virtual void accept(parser_visitor *visitor) override;
type_expression& base();
top_type& base();
array_type_expression *is_array() override;
array_type *is_array() override;
virtual ~array_type_expression() override;
virtual ~array_type() override;
};
class pointer_type_expression final : public type_expression
class pointer_type : public top_type
{
type_expression *m_base;
top_type *m_base;
public:
pointer_type_expression(const struct position position, type_expression *base);
pointer_type(const struct position position, top_type *base);
virtual void accept(parser_visitor *visitor) override;
type_expression& base();
top_type& base();
pointer_type_expression *is_pointer() override;
pointer_type *is_pointer() override;
virtual ~pointer_type_expression() override;
virtual ~pointer_type() override;
};
using field_t = std::pair<std::string, type_expression *>;
using field_t = std::pair<std::string, top_type *>;
using fields_t = std::vector<field_t>;
class composite_type_expression : public type_expression
class composite_type : public top_type
{
protected:
composite_type_expression(const struct position position, fields_t&& fields);
composite_type(const struct position position, fields_t&& fields);
public:
fields_t fields;
virtual ~composite_type_expression() override;
virtual ~composite_type() override;
};
class record_type_expression final : public composite_type_expression
class record_type : public composite_type
{
public:
record_type_expression(const struct position position, fields_t&& fields);
record_type(const struct position position, fields_t&& fields);
virtual void accept(parser_visitor *visitor) override;
record_type_expression *is_record() override;
record_type *is_record() override;
};
class union_type_expression final : public composite_type_expression
class union_type : public composite_type
{
public:
union_type_expression(const struct position position, fields_t&& fields);
union_type(const struct position position, fields_t&& fields);
virtual void accept(parser_visitor *visitor) override;
union_type_expression *is_union() override;
union_type *is_union() override;
};
/**
@ -319,14 +320,14 @@ namespace boot
*/
class variable_declaration : public definition
{
type_expression *m_type;
top_type *m_type;
public:
variable_declaration(const struct position position, const std::string& identifier,
const bool exported, type_expression *type);
const bool exported, top_type *type);
virtual void accept(parser_visitor *visitor) override;
type_expression& type();
top_type& variable_type();
virtual ~variable_declaration() override;
};
@ -367,17 +368,17 @@ namespace boot
*/
class procedure_definition : public definition
{
type_expression *m_return_type{ nullptr };
top_type *m_return_type{ nullptr };
block *m_body{ nullptr };
public:
std::vector<variable_declaration *> parameters;
procedure_definition(const struct position position, const std::string& identifier,
const bool exported, type_expression *return_type = nullptr);
const bool exported, top_type *return_type = nullptr);
virtual void accept(parser_visitor *visitor) override;
type_expression *return_type();
top_type *return_type();
block *body();
procedure_definition *add_body(block *procedure_body);
@ -390,14 +391,14 @@ namespace boot
*/
class type_definition : public definition
{
type_expression *m_body;
top_type *m_body;
public:
type_definition(const struct position position, const std::string& identifier,
const bool exported, type_expression *expression);
const bool exported, top_type *expression);
virtual void accept(parser_visitor *visitor) override;
type_expression& body();
top_type& body();
virtual ~type_definition() override;
};
@ -429,33 +430,33 @@ namespace boot
*/
class cast_expression : public expression
{
type_expression *m_target;
top_type *m_target;
expression *m_value;
public:
cast_expression(const struct position position, type_expression *target, expression *value);
cast_expression(const struct position position, top_type *target, expression *value);
virtual void accept(parser_visitor *visitor) override;
type_expression& target();
top_type& target();
expression& value();
virtual ~cast_expression() override;
};
/**
* sizeOf operator.
* Type inside an expression.
*/
class size_of_expression : public expression
class type_expression : public expression
{
type_expression *m_body;
top_type *m_body;
public:
size_of_expression(const struct position position, type_expression *body);
type_expression(const struct position position, top_type *body);
virtual void accept(parser_visitor *visitor) override;
type_expression& body();
top_type& body();
virtual ~size_of_expression() override;
virtual ~type_expression() override;
};
class call_statement : public statement
@ -662,11 +663,11 @@ namespace boot
template<typename T>
class number_literal : public literal
{
T m_value;
public:
T value;
number_literal(const struct position position, const T& value)
: literal(position), m_value(value)
: literal(position), value(value)
{
}
@ -674,11 +675,6 @@ namespace boot
{
visitor->visit(this);
}
const T& number() const
{
return m_value;
}
};
class defer_statement : public statement

View File

@ -40,7 +40,7 @@ namespace gcc
std::shared_ptr<boot::symbol_table<tree>> symbol_map;
tree build_label_decl(const char *name, location_t loc);
tree build_type(boot::type_expression& type);
tree build_type(boot::top_type& type);
void enter_scope();
tree leave_scope();
@ -53,9 +53,12 @@ namespace gcc
tree_code operator_code, tree left, tree right);
tree build_comparison_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
tree build_logic_operation(boot::binary_expression *expression,
tree_code operator_code, tree left, tree right);
tree build_bit_logic_operation(boot::binary_expression *expression, tree left, tree right);
tree build_equality_operation(boot::binary_expression *expression, tree left, tree right);
void build_procedure_call(location_t call_location,
tree symbol, const std::vector<boot::expression *>& arguments);
void build_record_call(location_t call_location,
tree symbol, const std::vector<boot::expression *>& arguments);
public:
generic_visitor(std::shared_ptr<boot::symbol_table<tree>> symbol_table);
@ -64,7 +67,7 @@ namespace gcc
void visit(boot::procedure_definition *definition) override;
void visit(boot::call_expression *expression) override;
void visit(boot::cast_expression *expression) override;
void visit(boot::size_of_expression *expression) override;
void visit(boot::type_expression *expression) override;
void visit(boot::number_literal<std::int32_t> *literal) override;
void visit(boot::number_literal<std::uint32_t> *literal) override;
void visit(boot::number_literal<double> *literal) override;

View File

@ -39,14 +39,26 @@ namespace gcc
bool is_array_type(tree type);
bool is_procedure_type(tree type);
bool is_void_type(tree type);
bool is_record_type(tree type);
/**
* \param type The type to evaluate.
* \return Whether the given type is record or union.
*/
bool is_aggregate_type(tree type);
/**
* \param lhs Left hand value.
* \param rhs Right hand value.
* \return Whether rhs can be assigned to lhs.
*/
bool are_compatible_pointers(tree lhs, tree rhs);
bool are_compatible_pointers(tree lhs_type, tree rhs);
/**
* \param assignee Assignee.
* \param assignee Assignment.
* \return Whether an expression assignment can be assigned to a variable of type assignee.
*/
bool is_assignable_from(tree assignee, tree assignment);
void append_statement(tree statement_tree);
void defer(tree statement_tree);