Rename AST types to type expressions

This commit is contained in:
2025-03-02 23:35:07 +01:00
parent 09f204bd16
commit c5930285bf
13 changed files with 462 additions and 280 deletions

View File

@ -23,215 +23,167 @@ namespace boot
{
void empty_visitor::visit(variable_declaration *)
{
__builtin_unreachable();
}
void empty_visitor::visit(constant_definition *definition)
void empty_visitor::visit(constant_definition *)
{
definition->body().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(procedure_definition *definition)
void empty_visitor::visit(procedure_definition *)
{
definition->heading().accept(this);
if (definition->body != nullptr)
{
definition->body->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(type_definition *definition)
void empty_visitor::visit(type_definition *)
{
definition->body().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(procedure_call *call)
void empty_visitor::visit(procedure_call *)
{
for (expression *const argument : call->arguments)
{
argument->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(traits_expression *trait)
void empty_visitor::visit(traits_expression *)
{
trait->type().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(cast_expression *expression)
void empty_visitor::visit(cast_expression *)
{
expression->target().accept(this);
expression->value().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(assign_statement *statement)
void empty_visitor::visit(assign_statement *)
{
statement->rvalue().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(if_statement *statement)
void empty_visitor::visit(if_statement *)
{
statement->body().prerequisite().accept(this);
for (const auto body_statement : statement->body().statements)
{
body_statement->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(while_statement *statement)
void empty_visitor::visit(while_statement *)
{
statement->body().prerequisite().accept(this);
for (const auto body_statement : statement->body().statements)
{
body_statement->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(return_statement *statement)
void empty_visitor::visit(return_statement *)
{
expression *return_expression = statement->return_expression();
if (return_expression != nullptr)
{
return_expression->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(defer_statement *defer)
void empty_visitor::visit(defer_statement *)
{
for (statement *const body_statement : defer->statements)
{
body_statement->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(block *block)
void empty_visitor::visit(block *)
{
for (constant_definition *const constant : block->constants)
{
constant->accept(this);
}
for (variable_declaration *const variable : block->variables)
{
variable->accept(this);
}
for (statement *const body_statement : block->body)
{
body_statement->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(program *program)
void empty_visitor::visit(program *)
{
visit(reinterpret_cast<block *>(program));
for (type_definition *const type : program->types)
{
type->accept(this);
}
for (procedure_definition *const procedure : program->procedures)
{
procedure->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(binary_expression *expression)
void empty_visitor::visit(binary_expression *)
{
expression->lhs().accept(this);
expression->rhs().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(unary_expression *expression)
void empty_visitor::visit(unary_expression *)
{
expression->operand().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(basic_type *)
void empty_visitor::visit(primitive_type_expression *)
{
__builtin_unreachable();
}
void empty_visitor::visit(array_type *expression)
void empty_visitor::visit(array_type_expression *)
{
expression->base().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(pointer_type *expression)
void empty_visitor::visit(pointer_type_expression *)
{
expression->base().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(record_type *expression)
void empty_visitor::visit(record_type_expression *)
{
for (auto& field : expression->fields)
{
field.second->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(union_type *expression)
void empty_visitor::visit(union_type_expression *)
{
for (auto& field : expression->fields)
{
field.second->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(procedure_type *expression)
void empty_visitor::visit(procedure_type_expression *)
{
for (auto parameter : expression->parameters)
{
parameter->accept(this);
}
if (expression->return_type != nullptr)
{
expression->return_type->accept(this);
}
__builtin_unreachable();
}
void empty_visitor::visit(variable_expression *)
{
__builtin_unreachable();
}
void empty_visitor::visit(array_access_expression *expression)
void empty_visitor::visit(array_access_expression *)
{
expression->base().accept(this);
expression->index().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(field_access_expression *expression)
void empty_visitor::visit(field_access_expression *)
{
expression->base().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(dereference_expression *expression)
void empty_visitor::visit(dereference_expression *)
{
expression->base().accept(this);
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<std::int32_t> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<std::uint32_t> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<double> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<bool> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<unsigned char> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<std::nullptr_t> *)
{
__builtin_unreachable();
}
void empty_visitor::visit(number_literal<std::string> *)
{
__builtin_unreachable();
}
node::node(const struct position position)
@ -252,78 +204,130 @@ namespace boot
{
}
top_type::top_type(const struct position position)
type_expression::type_expression(const struct position position)
: node(position)
{
}
basic_type::basic_type(const struct position position, const std::string& name)
: top_type(position), m_name(name)
std::shared_ptr<primitive_type_expression> type_expression::is_primitive()
{
return nullptr;
}
std::shared_ptr<array_type_expression> type_expression::is_array()
{
return nullptr;
}
std::shared_ptr<pointer_type_expression> type_expression::is_pointer()
{
return nullptr;
}
std::shared_ptr<record_type_expression> type_expression::is_record()
{
return nullptr;
}
std::shared_ptr<union_type_expression> type_expression::is_union()
{
return nullptr;
}
std::shared_ptr<procedure_type_expression> type_expression::is_procedure()
{
return nullptr;
}
primitive_type_expression::primitive_type_expression(const struct position position, const std::string& name)
: type_expression(position), name(name)
{
}
void basic_type::accept(parser_visitor *visitor)
void primitive_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
const std::string& basic_type::base_name()
std::shared_ptr<primitive_type_expression> primitive_type_expression::is_primitive()
{
return m_name;
return std::static_pointer_cast<primitive_type_expression>(shared_from_this());
}
array_type::array_type(const struct position position, std::shared_ptr<top_type> base, const std::uint32_t size)
: top_type(position), m_base(base), size(size)
array_type_expression::array_type_expression(const struct position position,
std::shared_ptr<type_expression> base, const std::uint32_t size)
: type_expression(position), m_base(base), size(size)
{
}
void array_type::accept(parser_visitor *visitor)
void array_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
top_type& array_type::base()
std::shared_ptr<array_type_expression> array_type_expression::is_array()
{
return std::static_pointer_cast<array_type_expression>(shared_from_this());
}
type_expression& array_type_expression::base()
{
return *m_base;
}
pointer_type::pointer_type(const struct position position, std::shared_ptr<top_type> base)
: top_type(position), m_base(base)
pointer_type_expression::pointer_type_expression(const struct position position,
std::shared_ptr<type_expression> base)
: type_expression(position), m_base(base)
{
}
void pointer_type::accept(parser_visitor *visitor)
void pointer_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
top_type& pointer_type::base()
std::shared_ptr<pointer_type_expression> pointer_type_expression::is_pointer()
{
return std::static_pointer_cast<pointer_type_expression>(shared_from_this());
}
type_expression& pointer_type_expression::base()
{
return *m_base;
}
record_type::record_type(const struct position position, fields_t&& fields)
: top_type(position), fields(std::move(fields))
record_type_expression::record_type_expression(const struct position position, fields_t&& fields)
: type_expression(position), fields(std::move(fields))
{
}
void record_type::accept(parser_visitor *visitor)
void record_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
union_type::union_type(const struct position position, fields_t&& fields)
: top_type(position), fields(std::move(fields))
std::shared_ptr<record_type_expression> record_type_expression::is_record()
{
return std::static_pointer_cast<record_type_expression>(shared_from_this());
}
union_type_expression::union_type_expression(const struct position position, fields_t&& fields)
: type_expression(position), fields(std::move(fields))
{
}
void union_type::accept(parser_visitor *visitor)
void union_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
std::shared_ptr<union_type_expression> union_type_expression::is_union()
{
return std::static_pointer_cast<union_type_expression>(shared_from_this());
}
variable_declaration::variable_declaration(const struct position position, const std::string& identifier,
std::shared_ptr<top_type> type, const bool exported)
std::shared_ptr<type_expression> type, const bool exported)
: definition(position, identifier, exported), m_type(type)
{
}
@ -333,7 +337,7 @@ namespace boot
visitor->visit(this);
}
top_type& variable_declaration::variable_type()
type_expression& variable_declaration::variable_type()
{
return *m_type;
}
@ -364,22 +368,23 @@ namespace boot
delete m_body;
}
procedure_type::procedure_type(const struct position position, std::shared_ptr<top_type> return_type)
: top_type(position), return_type(return_type), no_return(false)
procedure_type_expression::procedure_type_expression(const struct position position,
std::shared_ptr<type_expression> return_type)
: type_expression(position), return_type(return_type), no_return(false)
{
}
procedure_type::procedure_type(const struct position position, no_return_t)
: top_type(position), return_type(nullptr), no_return(true)
procedure_type_expression::procedure_type_expression(const struct position position, no_return_t)
: type_expression(position), return_type(nullptr), no_return(true)
{
}
void procedure_type::accept(parser_visitor *visitor)
void procedure_type_expression::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
procedure_type::~procedure_type()
procedure_type_expression::~procedure_type_expression()
{
for (auto parameter : this->parameters)
{
@ -388,7 +393,7 @@ namespace boot
}
procedure_definition::procedure_definition(const struct position position, const std::string& identifier,
const bool exported, std::shared_ptr<procedure_type> heading, block *body)
const bool exported, std::shared_ptr<procedure_type_expression> heading, block *body)
: definition(position, identifier, exported), m_heading(heading), body(body)
{
}
@ -398,7 +403,12 @@ namespace boot
visitor->visit(this);
}
procedure_type& procedure_definition::heading()
std::shared_ptr<procedure_type_expression> procedure_type_expression::is_procedure()
{
return std::static_pointer_cast<procedure_type_expression>(shared_from_this());
}
procedure_type_expression& procedure_definition::heading()
{
return *m_heading;
}
@ -409,7 +419,7 @@ namespace boot
}
type_definition::type_definition(const struct position position, const std::string& identifier,
const bool exported, std::shared_ptr<top_type> body)
const bool exported, std::shared_ptr<type_expression> body)
: definition(position, identifier, exported), m_body(body)
{
}
@ -419,7 +429,7 @@ namespace boot
visitor->visit(this);
}
top_type& type_definition::body()
type_expression& type_definition::body()
{
return *m_body;
}
@ -685,7 +695,7 @@ namespace boot
}
cast_expression::cast_expression(const struct position position,
std::shared_ptr<top_type> target, expression *value)
std::shared_ptr<type_expression> target, expression *value)
: node(position), m_target(target), m_value(value)
{
}
@ -695,7 +705,7 @@ namespace boot
visitor->visit(this);
}
top_type& cast_expression::target()
type_expression& cast_expression::target()
{
return *m_target;
}
@ -711,7 +721,7 @@ namespace boot
}
traits_expression::traits_expression(const struct position position,
const std::string& name, std::shared_ptr<top_type> type)
const std::string& name, std::shared_ptr<type_expression> type)
: node(position), m_type(type), name(name)
{
}
@ -721,7 +731,7 @@ namespace boot
visitor->visit(this);
}
top_type& traits_expression::type()
type_expression& traits_expression::type()
{
return *m_type;
}

View File

@ -125,7 +125,7 @@ along with GCC; see the file COPYING3. If not see
%type <std::vector<elna::boot::variable_declaration *>> variable_declarations variable_part variable_declaration
formal_parameters formal_parameter_list;
%type <elna::boot::variable_declaration *> formal_parameter
%type <std::shared_ptr<elna::boot::top_type>> type_expression;
%type <std::shared_ptr<elna::boot::type_expression>> type_expression;
%type <elna::boot::traits_expression *> traits_expression;
%type <elna::boot::expression *> expression operand;
%type <elna::boot::unary_expression *> unary_expression;
@ -140,13 +140,14 @@ along with GCC; see the file COPYING3. If not see
%type <elna::boot::statement *> statement;
%type <std::vector<elna::boot::statement *>> statements;
%type <elna::boot::procedure_definition *> procedure_definition;
%type <std::shared_ptr<elna::boot::procedure_type>> procedure_heading;
%type <std::shared_ptr<elna::boot::procedure_type_expression>> procedure_heading;
%type <std::vector<elna::boot::procedure_definition *>> procedure_definitions procedure_part;
%type <elna::boot::type_definition *> type_definition;
%type <std::vector<elna::boot::type_definition *>> type_definitions type_part;
%type <elna::boot::block *> block;
%type <elna::boot::field_t> field_declaration;
%type <std::vector<std::pair<std::string, std::shared_ptr<elna::boot::top_type>>>> optional_fields required_fields;
%type <std::vector<std::pair<std::string, std::shared_ptr<elna::boot::type_expression>>>>
optional_fields required_fields;
%type <std::vector<elna::boot::conditional_statements *>> elsif_then_statements elsif_do_statements;
%type <elna::boot::cast_expression *> cast_expression;
%type <elna::boot::defer_statement *> defer_statement;
@ -193,17 +194,18 @@ identifier_definitions:
procedure_heading:
formal_parameter_list
{
$$ = std::make_shared<elna::boot::procedure_type>(elna::boot::make_position(@1));
$$ = std::make_shared<elna::boot::procedure_type_expression>(elna::boot::make_position(@1));
std::swap($1, $$->parameters);
}
| formal_parameter_list "->" "!"
{
$$ = std::make_shared<elna::boot::procedure_type>(elna::boot::make_position(@1), elna::boot::no_return);
$$ = std::make_shared<elna::boot::procedure_type_expression>(elna::boot::make_position(@1),
elna::boot::no_return);
std::swap($1, $$->parameters);
}
| formal_parameter_list "->" type_expression
{
$$ = std::make_shared<elna::boot::procedure_type>(elna::boot::make_position(@1), $3);
$$ = std::make_shared<elna::boot::procedure_type_expression>(elna::boot::make_position(@1), $3);
std::swap($1, $$->parameters);
}
procedure_definition:
@ -484,19 +486,19 @@ optional_fields:
type_expression:
"[" INTEGER "]" type_expression
{
$$ = std::make_shared<elna::boot::array_type>(elna::boot::make_position(@1), $4, $2);
$$ = std::make_shared<elna::boot::array_type_expression>(elna::boot::make_position(@1), $4, $2);
}
| "^" type_expression
{
$$ = std::make_shared<elna::boot::pointer_type>(elna::boot::make_position(@1), $2);
$$ = std::make_shared<elna::boot::pointer_type_expression>(elna::boot::make_position(@1), $2);
}
| "record" optional_fields "end"
{
$$ = std::make_shared<elna::boot::record_type>(elna::boot::make_position(@1), std::move($2));
$$ = std::make_shared<elna::boot::record_type_expression>(elna::boot::make_position(@1), std::move($2));
}
| "union" required_fields "end"
{
$$ = std::make_shared<elna::boot::union_type>(elna::boot::make_position(@1), std::move($2));
$$ = std::make_shared<elna::boot::union_type_expression>(elna::boot::make_position(@1), std::move($2));
}
| "proc" procedure_heading
{
@ -504,7 +506,7 @@ type_expression:
}
| IDENTIFIER
{
$$ = std::make_shared<elna::boot::basic_type>(elna::boot::make_position(@1), $1);
$$ = std::make_shared<elna::boot::primitive_type_expression>(elna::boot::make_position(@1), $1);
}
variable_declaration: identifier_definitions ":" type_expression
{

33
boot/semantic.cc Normal file
View File

@ -0,0 +1,33 @@
/* Semantic analysis visitors.
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "elna/boot/semantic.h"
namespace elna
{
namespace boot
{
declaration_visitor::declaration_visitor(std::shared_ptr<symbol_table> table)
: table(table)
{
}
void declaration_visitor::visit(program *program)
{
}
}
}

37
boot/symbol.cc Normal file
View File

@ -0,0 +1,37 @@
/* Symbol definitions.
Copyright (C) 2025 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "elna/boot/symbol.h"
namespace elna
{
namespace boot
{
type::~type()
{
}
info::~info()
{
}
type_info::type_info(const std::string& name)
: name(name)
{
}
}
}