Move type definitions to the program node

This commit is contained in:
2025-01-12 10:35:24 +01:00
parent 7985704981
commit b45b00a3f6
11 changed files with 236 additions and 82 deletions

View File

@ -30,7 +30,6 @@ elna_OBJS = \
elna/lexer.o \
elna/parser.o \
elna/result.o \
elna/types.o \
$(END)
elna1$(exeext): attribs.o $(elna_OBJS) $(BACKEND) $(LIBDEPS)

View File

@ -15,9 +15,14 @@ namespace elna
{
namespace gcc
{
generic_visitor::generic_visitor()
{
this->symbol_map = std::make_shared<source::symbol_table<tree>>();
}
void generic_visitor::visit(source::call_statement *statement)
{
auto symbol = this->symbol_map.find(statement->name());
auto symbol = this->symbol_map->lookup(statement->name());
if (statement->name() == "writei")
{
@ -80,10 +85,10 @@ namespace gcc
append_to_statement_list(stmt, &this->current_statements);
this->current_expression = NULL_TREE;
}
else if (symbol != this->symbol_map.end())
else if (symbol)
{
tree fndecl_type = build_function_type_list(integer_type_node, NULL_TREE);
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), symbol->second);
tree printf_fn = build1(ADDR_EXPR, build_pointer_type(fndecl_type), symbol->payload);
tree stmt = build_call_nary(integer_type_node, printf_fn, 0);
@ -100,7 +105,7 @@ namespace gcc
void generic_visitor::visit(source::program *program)
{
for (const auto& constant : program->definitions())
for (const auto& constant : program->type_definitions)
{
constant->accept(this);
}
@ -153,7 +158,7 @@ namespace gcc
definition->parameters().size(), parameter_types);
this->main_fndecl = build_fn_decl(definition->identifier().c_str(), declaration_type);
this->symbol_map.insert({ definition->identifier(), this->main_fndecl });
this->symbol_map->enter(definition->identifier(), source::make_info(this->main_fndecl));
tree resdecl = build_decl(UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, integer_type_node);
DECL_CONTEXT(resdecl) = this->main_fndecl;
@ -186,6 +191,7 @@ namespace gcc
{
this->current_statements = alloc_stmt_list();
this->variable_chain = tree_chain();
this->symbol_map = std::make_shared<source::symbol_table<tree>>(this->symbol_map);
}
tree_symbol_mapping generic_visitor::leave_scope()
@ -194,6 +200,7 @@ namespace gcc
NULL_TREE, NULL_TREE, NULL_TREE);
tree bind_expr = build3(BIND_EXPR, void_type_node, variable_chain.head(),
this->current_statements, new_block);
this->symbol_map = this->symbol_map->scope();
return tree_symbol_mapping{ bind_expr, new_block };
}
@ -344,9 +351,9 @@ namespace gcc
location_t definition_location = get_location(&definition->position());
tree definition_tree = build_decl(definition_location, CONST_DECL,
get_identifier(definition->identifier().c_str()), integer_type_node);
auto result = this->symbol_map.insert({ definition->identifier(), definition_tree });
auto result = this->symbol_map->enter(definition->identifier(), source::make_info(definition_tree));
if (result.second)
if (result)
{
definition->body().accept(this);
@ -378,9 +385,9 @@ namespace gcc
location_t definition_location = get_location(&definition->position());
tree definition_tree = build_decl(definition_location, TYPE_DECL,
get_identifier(definition->identifier().c_str()), tree_type);
auto result = this->symbol_map.insert({ definition->identifier(), definition_tree });
auto result = this->symbol_map->enter(definition->identifier(), source::make_info(definition_tree));
if (result.second)
if (result)
{
DECL_CONTEXT(definition_tree) = this->main_fndecl;
variable_chain.append(definition_tree);
@ -421,11 +428,11 @@ namespace gcc
{
return elna_string_type_node;
}
auto symbol = this->symbol_map.find(basic_type->base_name());
auto symbol = this->symbol_map->lookup(basic_type->base_name());
if (symbol != this->symbol_map.end())
if (symbol)
{
return TREE_TYPE(symbol->second);
return TREE_TYPE(symbol->payload);
}
error_at(get_location(&basic_type->position()),
"type '%s' not declared", basic_type->base_name().c_str());
@ -499,9 +506,9 @@ namespace gcc
auto declaration_location = get_location(&declaration->position());
tree declaration_tree = build_decl(declaration_location, VAR_DECL,
get_identifier(declaration->identifier().c_str()), declaration_type);
auto result = this->symbol_map.insert({ declaration->identifier(), declaration_tree });
auto result = this->symbol_map->enter(declaration->identifier(), source::make_info(declaration_tree));
if (result.second)
if (result)
{
DECL_CONTEXT(declaration_tree) = this->main_fndecl;
variable_chain.append(declaration_tree);
@ -520,9 +527,9 @@ namespace gcc
void generic_visitor::visit(source::variable_expression *expression)
{
auto symbol = this->symbol_map.find(expression->name());
auto symbol = this->symbol_map->lookup(expression->name());
if (symbol == this->symbol_map.end())
if (!symbol)
{
error_at(get_location(&expression->position()),
"variable '%s' not declared in the current scope",
@ -530,7 +537,7 @@ namespace gcc
this->current_expression = error_mark_node;
return;
}
this->current_expression = symbol->second;
this->current_expression = symbol->payload;
}
void generic_visitor::visit(source::array_access_expression *expression)

View File

@ -1,3 +1,6 @@
/* gcc-src/gcc/config/lang-specs.in */
{".elna", "@elna", 0, 1, 0},
{"@elna", "elna1 %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}", 0, 1, 0},
/* gcc/gcc.cc */
{".elna", "@elna", nullptr, 0, 0},
{"@elna",
"elna1 %{!Q:-quiet} \
%i %{!fsyntax-only:%(invoke_as)}",
nullptr, 0, 0},