Check for duplicate fields in the declaration visitor

This commit is contained in:
2025-03-15 23:01:05 +01:00
parent fa73f14070
commit f6e0ead4fb
9 changed files with 88 additions and 151 deletions

View File

@ -15,8 +15,6 @@ 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 <set>
#include "elna/boot/semantic.h"
namespace elna
@ -110,57 +108,17 @@ namespace boot
this->current_type = type(std::make_shared<array_type>(this->current_type, type_expression->size));
}
bool declaration_visitor::build_composite_type(const std::vector<field_declaration>& declarations,
std::vector<type_field>& fields)
void declaration_visitor::visit(record_type_expression *)
{
std::set<std::string> field_names;
for (auto& field : declarations)
{
if (field_names.find(field.first) != field_names.cend())
{
add_error<already_declared_error>(field.first, this->input_file, field.second->position());
this->current_type = type();
return false;
}
field.second->accept(this);
if (!this->current_type.empty())
{
fields.push_back({ field.first, type(this->current_type) });
}
}
return true;
this->current_type = type(std::make_shared<record_type>());
}
void declaration_visitor::visit(record_type_expression *type_expression)
void declaration_visitor::visit(union_type_expression *)
{
auto type_definition = std::make_shared<record_type>();
if (build_composite_type(type_expression->fields, type_definition->fields))
{
this->current_type = type(type_definition);
}
else
{
this->current_type = type();
}
this->current_type = type(std::make_shared<union_type>());
}
void declaration_visitor::visit(union_type_expression *type_expression)
{
auto type_definition = std::make_shared<union_type>();
if (build_composite_type(type_expression->fields, type_definition->fields))
{
this->current_type = type(type_definition);
}
else
{
this->current_type = type();
}
}
void declaration_visitor::visit(procedure_type_expression *type_expression)
void declaration_visitor::visit(procedure_type_expression *)
{
}
}