Skip to content

Commit d61fbdb

Browse files
committed
Fix the checking of type and function's redefinition.
1 parent 85a0fad commit d61fbdb

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

semantic.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ static list_t formal_type_list(list_t params, int pos)
7575

7676
static void trans_funcs_decl(ast_decl_t decl)
7777
{
78-
list_t p;
78+
list_t p, q;
79+
80+
/* Check for function redefinitions. */
81+
for (p = decl->u.funcs; p && p->next; p = p->next)
82+
for (q = p->next; q; q = q->next)
83+
{
84+
ast_func_t func = q->data;
85+
if (((ast_func_t) p->data)->name == func->name)
86+
em_error(func->pos,
87+
"function '%s' redefined",
88+
sym_name(func->name));
89+
}
7990

8091
/* Enter function prototypes into symbol table. */
8192
for (p = decl->u.funcs; p; p = p->next)
@@ -121,7 +132,16 @@ static void trans_funcs_decl(ast_decl_t decl)
121132

122133
static void trans_types_decl(ast_decl_t decl)
123134
{
124-
list_t p;
135+
list_t p, q;
136+
137+
/* Check for type redefinitions. */
138+
for (p = decl->u.types; p && p->next; p = p->next)
139+
for (q = p->next; q; q = q->next)
140+
{
141+
ast_nametype_t nt = q->data;
142+
if (((ast_nametype_t) p->data)->name == nt->name)
143+
em_error(decl->pos, "type '%s' redefined", sym_name(nt->name));
144+
}
125145

126146
/* Enter type placeholder into symbol table. */
127147
for (p = decl->u.types; p; p = p->next)

0 commit comments

Comments
 (0)