Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prohibit function parameter redefinition #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions shivyc/tree/nodes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Nodes in the AST which represent statements or declarations."""

import shivyc.ctypes as ctypes
import shivyc.il_cmds.control as control_cmds
import shivyc.il_cmds.value as value_cmds
Expand Down Expand Up @@ -479,7 +478,7 @@ def get_linkage(self, symbol_table, c):
def get_defined(self, symbol_table, c):
"""Determine whether this is a definition."""
if (c.is_global and self.storage in {None, self.STATIC}
and self.ctype.is_object() and not self.init):
and self.ctype.is_object() and not self.init):
return symbol_table.TENTATIVE
elif self.storage == self.EXTERN and not (self.init or self.body):
return symbol_table.UNDEFINED
Expand Down Expand Up @@ -598,9 +597,35 @@ def _generate_array_ctype(self, decl, prev_ctype):
else:
return ArrayCType(prev_ctype, None)

def _check_redefinition(self, decl):
"""
If identifier is redefinition inside function arguments, raise error
:param decl:declaration of function arguments
"""
# Save identifiers
identifiers = [decl_info.identifier for param
in decl.args for decl_info in
self.get_decl_infos(param)]

# Remove None inside identifiers
identifiers = [str(x) for x in identifiers if x is not None]

# Exit if no identifiers exist
if len(identifiers) == 0:
return

# Check duplicate identifier
for identifier in identifiers:
if identifiers.count(identifier) > 1:
err = f"redefinition of '{identifier}'"
raise CompilerError(err, self.r)

def _generate_func_ctype(self, decl, prev_ctype):
"""Generate a function ctype from a given a decl_node."""

# Prohibit redefinition of identifer
self._check_redefinition(decl)

# Prohibit storage class specifiers in parameters.
for param in decl.args:
decl_info = self.get_decl_infos(param)[0]
Expand Down
3 changes: 3 additions & 0 deletions tests/feature_tests/error_declaration.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ int var1;
// error: redeclared 'var1' with different linkage
static int var1;

// error: redefinition of 'a'
void repeat_param(int a, int a);

int main() {
// error: variable of incomplete type declared
void a;
Expand Down