Fix alias resolution with type declarations

This commit is contained in:
Eugen Wissner 2025-03-28 10:40:14 +01:00
parent d359056354
commit 413f23af4d
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
4 changed files with 30 additions and 35 deletions

View File

@ -524,11 +524,11 @@ variable_declaration: identifier_definitions ":" type_expression
}
}
variable_declarations:
variable_declaration variable_declarations
variable_declaration ";" variable_declarations
{
std::swap($$, $1);
$$.reserve($$.size() + $2.size());
$$.insert(std::end($$), std::begin($2), std::end($2));
$$.reserve($$.size() + $3.size());
$$.insert(std::end($$), std::begin($3), std::end($3));
}
| variable_declaration { std::swap($$, $1); }
variable_part:

View File

@ -41,14 +41,10 @@ namespace elna::gcc
{
if (auto reference = type.get<boot::primitive_type>())
{
auto looked_up = unresolved.find(reference->identifier);
if (looked_up == unresolved.cend())
auto looked_up = symbols->lookup(reference->identifier);
if (looked_up != NULL_TREE)
{
return symbols->lookup(reference->identifier);
}
else
{
return looked_up->second;
return TREE_TYPE(looked_up);
}
}
else if (auto reference = type.get<boot::record_type>())
@ -73,22 +69,20 @@ namespace elna::gcc
}
else if (auto reference = type.get<boot::alias_type>())
{
return handle_symbol(reference->name, reference->reference, symbols, unresolved);
return handle_symbol(reference->name, reference, symbols, unresolved);
}
return error_mark_node;
}
tree handle_symbol(const std::string& symbol_name, const boot::type& type,
tree handle_symbol(const std::string& symbol_name, std::shared_ptr<boot::alias_type> reference,
std::shared_ptr<symbol_table> symbols, std::unordered_map<std::string, tree>& unresolved)
{
auto looked_up = symbols->lookup(symbol_name);
auto looked_up = get_inner_alias(reference->reference, symbols, unresolved);
unresolved.insert({ symbol_name, looked_up });
if (looked_up == NULL_TREE)
{
looked_up = get_inner_alias(type, symbols, unresolved);
unresolved.insert({ symbol_name, looked_up });
}
return looked_up;
}
std::deque<std::unique_ptr<boot::error>> do_semantic_analysis(const char *path,
@ -103,7 +97,7 @@ namespace elna::gcc
{
for (auto& [symbol_name, symbol_info] : declaration_visitor.unresolved)
{
handle_symbol(symbol_name, boot::type(symbol_info), symbols, unresolved);
handle_symbol(symbol_name, symbol_info, symbols, unresolved);
}
}
return std::move(declaration_visitor.errors());

View File

@ -35,9 +35,10 @@ namespace elna::gcc
std::deque<std::unique_ptr<boot::error>> do_semantic_analysis(const char *path,
std::unique_ptr<boot::program>& ast, std::shared_ptr<boot::symbol_table> info_table,
std::shared_ptr<symbol_table> symbols, std::unordered_map<std::string, tree>& unresolved);
tree handle_symbol(const std::string& symbol_name, const boot::type& type,
tree handle_symbol(const std::string& symbol_name, std::shared_ptr<boot::alias_type> reference,
std::shared_ptr<symbol_table> symbols, std::unordered_map<std::string, tree>& unresolved);
class generic_visitor final : public boot::parser_visitor
{
tree current_expression{ NULL_TREE };

View File

@ -169,8 +169,8 @@ end
proc write_i(value: Int);
var
digit: Int
n: Word
digit: Int;
n: Word;
buffer: [10]Char
begin
n := 10u;
@ -282,7 +282,7 @@ end
proc read_source(filename: ^Char) -> ^SourceFile;
var
result: ^SourceFile
result: ^SourceFile;
file_handle: ^FILE
begin
file_handle := fopen(filename, "rb\0".ptr);
@ -473,9 +473,9 @@ end
proc lex_string(source_code: ^SourceCode, token_content: ^StringBuffer) -> Bool;
var
token_end, constructed_string: ^Char
token_length: Word
is_valid: Bool
token_end, constructed_string: ^Char;
token_length: Word;
is_valid: Bool;
next_char: Char
begin
is_valid := true;
@ -509,7 +509,7 @@ end
proc print_tokens(tokens: ^Token, tokens_size: Word);
var
current_token: ^Token
current_token: ^Token;
i: Word
begin
i := 0u;
@ -728,8 +728,8 @@ end
proc tokenize(source_code: SourceCode, tokens_size: ^Word) -> ^Token;
var
tokens, current_token: ^Token
first_char: Char
tokens, current_token: ^Token;
first_char: Char;
token_buffer: StringBuffer
begin
tokens_size^ := 0u;
@ -905,8 +905,8 @@ end
proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine;
var
parameter: ^^Char
i: Int
parameter: ^^Char;
i: Int;
result: ^CommandLine
begin
i := 1;
@ -946,10 +946,10 @@ end
proc process(argc: Int, argv: ^^Char) -> Int;
var
tokens: ^Token
tokens_size: Word
source_code: SourceCode
command_line: ^CommandLine
tokens: ^Token;
tokens_size: Word;
source_code: SourceCode;
command_line: ^CommandLine;
return_code: Int
begin
return_code := 0;