Document symbol bag class and methods

This commit is contained in:
2025-08-23 00:48:58 +02:00
parent 809e41bcc3
commit fd3729ba16
13 changed files with 294 additions and 117 deletions

View File

@@ -87,7 +87,7 @@ namespace elna::gcc
tree definition_tree = build_decl(UNKNOWN_LOCATION, TYPE_DECL,
get_identifier(identifier.c_str()), type);
TREE_PUBLIC(definition_tree) = true;
TREE_PUBLIC(definition_tree) = 1;
if (is_unique_type(type))
{
TYPE_NAME(type) = DECL_NAME(definition_tree);
@@ -235,7 +235,21 @@ namespace elna::gcc
}
DECL_ARGUMENTS(fndecl) = argument_chain;
TREE_ADDRESSABLE(fndecl) = 1;
DECL_EXTERNAL(fndecl) = info.symbols == nullptr;
DECL_EXTERNAL(fndecl) = info.is_extern();
}
tree declare_variable(const std::string& name, const boot::variable_info& info,
std::shared_ptr<symbol_table> symbols)
{
auto variable_type = get_inner_alias(info.symbol, symbols);
tree declaration_tree = build_decl(UNKNOWN_LOCATION, VAR_DECL, get_identifier(name.c_str()), variable_type);
TREE_ADDRESSABLE(declaration_tree) = 1;
DECL_EXTERNAL(declaration_tree) = info.is_extern;
symbols->enter(name, declaration_tree);
return declaration_tree;
}
void rewrite_symbol_table(std::shared_ptr<boot::symbol_table> info_table, std::shared_ptr<symbol_table> symbols)
@@ -250,6 +264,10 @@ namespace elna::gcc
handle_symbol(symbol_name, alias_type, symbols);
}
}
else if (auto variable_info = symbol_info->is_variable())
{
declare_variable(symbol_name, *variable_info, symbols);
}
else if (auto procedure_info = symbol_info->is_procedure())
{
declare_procedure(symbol_name, *procedure_info, symbols);

View File

@@ -311,7 +311,7 @@ namespace elna::gcc
void generic_visitor::visit(boot::procedure_declaration *definition)
{
tree fndecl = this->symbols->lookup(definition->identifier.identifier);
tree fndecl = this->symbols->lookup(definition->identifier.name);
TREE_PUBLIC(fndecl) = definition->identifier.exported;
if (!definition->body.has_value())
@@ -323,7 +323,7 @@ namespace elna::gcc
DECL_STRUCT_FUNCTION(fndecl)->language = ggc_cleared_alloc<language_function>();
enter_scope();
this->bag.enter(this->bag.lookup(definition->identifier.identifier)->is_procedure()->symbols);
this->bag.enter(this->bag.lookup(definition->identifier.name)->is_procedure()->scope);
tree argument_chain = DECL_ARGUMENTS(fndecl);
for (; argument_chain != NULL_TREE; argument_chain = TREE_CHAIN(argument_chain))
@@ -713,8 +713,8 @@ namespace elna::gcc
return;
}
tree definition_tree = build_decl(definition_location, CONST_DECL,
get_identifier(definition->identifier.identifier.c_str()), TREE_TYPE(this->current_expression));
auto result = this->symbols->enter(definition->identifier.identifier, definition_tree);
get_identifier(definition->identifier.name.c_str()), TREE_TYPE(this->current_expression));
auto result = this->symbols->enter(definition->identifier.name, definition_tree);
if (result)
{
@@ -733,29 +733,29 @@ namespace elna::gcc
else
{
error_at(definition_location, "Variable '%s' already declared in this scope",
definition->identifier.identifier.c_str());
definition->identifier.name.c_str());
}
this->current_expression = NULL_TREE;
}
void generic_visitor::visit(boot::type_declaration *declaration)
{
TREE_PUBLIC(this->symbols->lookup(declaration->identifier.identifier)) = declaration->identifier.exported;
TREE_PUBLIC(this->symbols->lookup(declaration->identifier.name)) = declaration->identifier.exported;
}
void generic_visitor::visit(boot::variable_declaration *declaration)
{
for (const auto& variable_identifier : declaration->identifiers)
{
this->current_expression = get_inner_alias(
this->bag.lookup(variable_identifier.identifier)->is_variable()->symbol,
this->symbols);
location_t declaration_location = get_location(&declaration->position());
tree declaration_tree = build_decl(declaration_location, VAR_DECL,
get_identifier(variable_identifier.identifier.c_str()), this->current_expression);
bool result = this->symbols->enter(variable_identifier.identifier, declaration_tree);
tree declaration_tree = this->symbols->lookup(variable_identifier.name);
if (declaration_tree == NULL_TREE)
{
auto variable_symbol = this->bag.lookup(variable_identifier.name)->is_variable();
declaration_tree = declare_variable(variable_identifier.name, *variable_symbol, this->symbols);
}
// Set initializer if given.
if (declaration->body != nullptr)
{
@@ -771,19 +771,14 @@ namespace elna::gcc
print_type(TREE_TYPE(this->current_expression)).c_str());
}
}
else if (POINTER_TYPE_P(this->current_expression))
else if (!declaration->is_extern && POINTER_TYPE_P(TREE_TYPE(declaration_tree)))
{
DECL_INITIAL(declaration_tree) = elna_pointer_nil_node;
}
DECL_EXTERNAL(declaration_tree) = declaration->is_extern;
TREE_PUBLIC(declaration_tree) = variable_identifier.exported;
this->current_expression = NULL_TREE;
if (!result)
{
error_at(declaration_location, "Variable '%s' already declared in this scope",
variable_identifier.identifier.c_str());
}
else if (lang_hooks.decls.global_bindings_p())
if (lang_hooks.decls.global_bindings_p())
{
TREE_STATIC(declaration_tree) = 1;
varpool_node::get_create(declaration_tree);

View File

@@ -66,7 +66,6 @@ using dependency_state = elna::boot::dependency_state<std::shared_ptr<elna::gcc:
static elna::boot::dependency elna_parse_file(dependency_state& state, const char *filename)
{
auto module_table = std::make_shared<elna::boot::symbol_table>(state.globals);
std::ifstream entry_point{ filename, std::ios::in };
if (!entry_point)
@@ -80,7 +79,7 @@ static elna::boot::dependency elna_parse_file(dependency_state& state, const cha
{
elna::gcc::report_errors(outcome.errors());
}
elna::boot::symbol_bag outcome_bag = elna::boot::symbol_bag{ std::move(outcome.unresolved), module_table };
elna::boot::symbol_bag outcome_bag = elna::boot::symbol_bag{ std::move(outcome.unresolved), state.globals };
for (const auto& sub_tree : outcome.tree->imports)
{
@@ -102,7 +101,7 @@ static elna::boot::dependency elna_parse_file(dependency_state& state, const cha
elna::gcc::report_errors(semantic_errors);
}
state.cache.insert({ filename, outcome_bag });
elna::gcc::rewrite_symbol_table(module_table, state.custom);
elna::gcc::rewrite_symbol_table(outcome_bag.leave(), state.custom);
linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
return outcome;