Type check the return statement
This commit is contained in:
@ -36,11 +36,20 @@ along with GCC; see the file COPYING3. If not see
|
||||
|
||||
namespace elna::gcc
|
||||
{
|
||||
tree get_inner_alias(const boot::type& type, std::shared_ptr<symbol_table> symbols)
|
||||
tree get_inner_alias(const boot::type& type, std::shared_ptr<symbol_table> symbols,
|
||||
std::unordered_map<std::string, tree>& unresolved)
|
||||
{
|
||||
if (auto reference = type.get<boot::primitive_type>())
|
||||
{
|
||||
return symbols->lookup(reference->identifier);
|
||||
auto looked_up = unresolved.find(reference->identifier);
|
||||
if (looked_up == unresolved.cend())
|
||||
{
|
||||
return symbols->lookup(reference->identifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
return looked_up->second;
|
||||
}
|
||||
}
|
||||
else if (auto reference = type.get<boot::record_type>())
|
||||
{
|
||||
@ -52,7 +61,7 @@ namespace elna::gcc
|
||||
}
|
||||
else if (auto reference = type.get<boot::pointer_type>())
|
||||
{
|
||||
return build_pointer_type_for_mode(get_inner_alias(reference->base, symbols), VOIDmode, true);
|
||||
return build_pointer_type_for_mode(get_inner_alias(reference->base, symbols, unresolved), VOIDmode, true);
|
||||
}
|
||||
else if (auto reference = type.get<boot::array_type>())
|
||||
{
|
||||
@ -60,30 +69,31 @@ namespace elna::gcc
|
||||
tree upper_bound = build_int_cst_type(integer_type_node, reference->size);
|
||||
tree range_type = build_range_type(integer_type_node, lower_bound, upper_bound);
|
||||
|
||||
return build_array_type(get_inner_alias(reference->base, symbols), range_type);
|
||||
return build_array_type(get_inner_alias(reference->base, symbols, unresolved), range_type);
|
||||
}
|
||||
else if (auto reference = type.get<boot::alias_type>())
|
||||
{
|
||||
return handle_symbol(reference->name, reference->reference, symbols);
|
||||
return handle_symbol(reference->name, reference->reference, symbols, unresolved);
|
||||
}
|
||||
return error_mark_node;
|
||||
}
|
||||
|
||||
tree handle_symbol(const std::string& symbol_name, const boot::type& type, std::shared_ptr<symbol_table> symbols)
|
||||
tree handle_symbol(const std::string& symbol_name, const boot::type& type,
|
||||
std::shared_ptr<symbol_table> symbols, std::unordered_map<std::string, tree>& unresolved)
|
||||
{
|
||||
auto looked_up = symbols->lookup(symbol_name);
|
||||
|
||||
if (looked_up == NULL_TREE)
|
||||
{
|
||||
looked_up = get_inner_alias(type, symbols);
|
||||
symbols->enter(symbol_name, looked_up);
|
||||
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,
|
||||
std::unique_ptr<boot::program>& ast, std::shared_ptr<boot::symbol_table> info_table,
|
||||
std::shared_ptr<symbol_table> symbols)
|
||||
std::shared_ptr<symbol_table> symbols, std::unordered_map<std::string, tree>& unresolved)
|
||||
{
|
||||
boot::declaration_visitor declaration_visitor(path, info_table);
|
||||
|
||||
@ -93,15 +103,16 @@ namespace elna::gcc
|
||||
{
|
||||
for (auto& [symbol_name, symbol_info] : declaration_visitor.unresolved)
|
||||
{
|
||||
handle_symbol(symbol_name, boot::type(symbol_info), symbols);
|
||||
handle_symbol(symbol_name, boot::type(symbol_info), symbols, unresolved);
|
||||
}
|
||||
}
|
||||
return std::move(declaration_visitor.errors());
|
||||
}
|
||||
|
||||
generic_visitor::generic_visitor(std::shared_ptr<symbol_table> symbol_table)
|
||||
generic_visitor::generic_visitor(std::shared_ptr<symbol_table> symbol_table,
|
||||
std::unordered_map<std::string, tree>&& unresolved)
|
||||
: symbols(symbol_table), unresolved(std::move(unresolved))
|
||||
{
|
||||
this->symbols = symbol_table;
|
||||
}
|
||||
|
||||
void generic_visitor::build_procedure_call(location_t call_location,
|
||||
@ -763,8 +774,7 @@ namespace elna::gcc
|
||||
}
|
||||
else
|
||||
{
|
||||
error_at(definition_location,
|
||||
"variable '%s' already declared in this scope",
|
||||
error_at(definition_location, "Variable '%s' already declared in this scope",
|
||||
definition->identifier.c_str());
|
||||
}
|
||||
this->current_expression = NULL_TREE;
|
||||
@ -773,12 +783,13 @@ namespace elna::gcc
|
||||
void generic_visitor::visit(boot::type_definition *definition)
|
||||
{
|
||||
location_t definition_location = get_location(&definition->position());
|
||||
this->current_expression = this->symbols->lookup(definition->identifier);
|
||||
this->current_expression = this->unresolved.at(definition->identifier);
|
||||
definition->body().accept(this);
|
||||
|
||||
tree definition_tree = build_decl(definition_location, TYPE_DECL,
|
||||
get_identifier(definition->identifier.c_str()), this->current_expression);
|
||||
auto result = this->symbols->enter(definition->identifier, this->current_expression);
|
||||
gcc_assert(result);
|
||||
|
||||
TREE_PUBLIC(definition_tree) = definition->exported;
|
||||
TYPE_NAME(this->current_expression) = get_identifier(definition->identifier.c_str());
|
||||
@ -1225,29 +1236,61 @@ namespace elna::gcc
|
||||
void generic_visitor::visit(boot::return_statement *statement)
|
||||
{
|
||||
boot::expression *return_expression = statement->return_expression();
|
||||
location_t statement_position = get_location(&statement->position());
|
||||
tree set_result{ NULL_TREE };
|
||||
tree return_type = TREE_TYPE(TREE_TYPE(current_function_decl));
|
||||
|
||||
if (return_expression == nullptr)
|
||||
if (TREE_THIS_VOLATILE(current_function_decl) == 1)
|
||||
{
|
||||
error_at(statement_position, "This procedure is not allowed to return");
|
||||
return;
|
||||
}
|
||||
return_expression->accept(this);
|
||||
|
||||
tree set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(current_function_decl),
|
||||
this->current_expression);
|
||||
tree return_stmt = build1(RETURN_EXPR, void_type_node, set_result);
|
||||
append_statement(return_stmt);
|
||||
if (return_expression != nullptr)
|
||||
{
|
||||
return_expression->accept(this);
|
||||
|
||||
set_result = build2(INIT_EXPR, void_type_node, DECL_RESULT(current_function_decl),
|
||||
this->current_expression);
|
||||
}
|
||||
if (return_type == void_type_node && set_result != NULL_TREE)
|
||||
{
|
||||
error_at(statement_position, "Proper procedure is not allowed to return a value");
|
||||
}
|
||||
else if (return_type != void_type_node && set_result == NULL_TREE)
|
||||
{
|
||||
error_at(statement_position, "Procedure is expected to return a value of type '%s'",
|
||||
print_type(return_type).c_str());
|
||||
}
|
||||
else if (return_type != void_type_node && !is_assignable_from(return_type, this->current_expression))
|
||||
{
|
||||
error_at(statement_position, "Cannot return '%s' from a procedure returning '%s'",
|
||||
print_type(return_type).c_str(),
|
||||
print_type(TREE_TYPE(this->current_expression)).c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
tree return_stmt = build1_loc(statement_position, RETURN_EXPR, void_type_node, set_result);
|
||||
append_statement(return_stmt);
|
||||
}
|
||||
this->current_expression = NULL_TREE;
|
||||
}
|
||||
|
||||
void generic_visitor::visit(boot::primitive_type_expression *type)
|
||||
{
|
||||
tree symbol = this->symbols->lookup(type->name);
|
||||
auto looked_up = this->unresolved.find(type->name);
|
||||
tree symbol;
|
||||
|
||||
if (looked_up == this->unresolved.cend())
|
||||
{
|
||||
symbol = this->symbols->lookup(type->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
symbol = looked_up->second;
|
||||
}
|
||||
if (symbol == NULL_TREE || !TYPE_P(symbol))
|
||||
{
|
||||
error_at(get_location(&type->position()),
|
||||
"type '%s' not declared", type->name.c_str());
|
||||
error_at(get_location(&type->position()), "Type '%s' not declared", type->name.c_str());
|
||||
|
||||
this->current_expression = error_mark_node;
|
||||
}
|
||||
|
@ -85,12 +85,14 @@ static void elna_parse_file(const char *filename)
|
||||
{
|
||||
std::shared_ptr<elna::boot::symbol_table> info_table = elna::boot::builtin_symbol_table();
|
||||
std::shared_ptr<elna::gcc::symbol_table> symbol_table = elna::gcc::builtin_symbol_table();
|
||||
std::unordered_map<std::string, tree> unresolved;
|
||||
|
||||
auto semantic_errors = elna::gcc::do_semantic_analysis(filename, driver.tree, info_table, symbol_table);
|
||||
auto semantic_errors = elna::gcc::do_semantic_analysis(filename, driver.tree,
|
||||
info_table, symbol_table, unresolved);
|
||||
|
||||
if (semantic_errors.empty())
|
||||
{
|
||||
elna::gcc::generic_visitor generic_visitor{ symbol_table };
|
||||
elna::gcc::generic_visitor generic_visitor{ symbol_table, std::move(unresolved) };
|
||||
generic_visitor.visit(driver.tree.get());
|
||||
}
|
||||
else
|
||||
|
Reference in New Issue
Block a user