Commit f65c96aa authored by Sergio Costas's avatar Sergio Costas

Now compares the signatures of the return values and the parameters when...

Now compares the signatures of the return values and the parameters when assigning a function pointer to a function pointer

Fixed some tests
parent 9f039abb
......@@ -392,6 +392,18 @@ class crust(object):
return False
def _compare_types(self, var1, var2):
if var1["crust"] != var2["crust"]:
return False
if var1["borrowed"] != var2["borrowed"]:
return False
if var1["recycle"] != var2["recycle"]:
return False
if var1["pointer"] != var2["pointer"]:
return False
return True
def _eval_statement(self, statement, var_container, first = False):
""" Evaluates an statement recursively.
@params:
......@@ -513,6 +525,13 @@ class crust(object):
# we only check if the param types and number are fine if there are crust-type parameters involved
if len(orig_var["function_params"]) != len(dest_data["function_params"]):
self._add_error(v["varlist"], self.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but they have a different number of parameters", orig_var["name"], dest_data["name"], statement.line)
else:
for c in range(len(orig_var["function_params"])):
if not self._compare_types(orig_var["function_params"][c],dest_data["function_params"][c]):
if c == 0:
self._add_error(v["varlist"], self.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but the return types differ", orig_var["name"], dest_data["name"], statement.line)
else:
self._add_error(v["varlist"], self.MSG_ERROR, "Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but parameter {:d} differs", orig_var["name"], dest_data["name"], statement.line,c)
else:
# we are assigning the result of a function call
ret_value = orig_var["function_params"][0]
......
......@@ -427,5 +427,14 @@ class Test(unittest.TestCase):
def test107DifferenParameterNumberCrust(self):
self._generic_test("unitest/test107.c",[ (crust.crust.MSG_ERROR,"Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but they have a different number of parameters","function","test1",10) ])
def test108DifferenParameterTypes(self):
self._generic_test("unitest/test108.c",[ (crust.crust.MSG_ERROR,"Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but parameter {:d} differs","function","test1",10,1) ])
def test109DifferenParameterTypes(self):
self._generic_test("unitest/test109.c",[ (crust.crust.MSG_ERROR,"Trying to assign the function '{:s}' to the function pointer '{:s}' at line {:d}, but the return types differ","function","test1",10) ])
def test110ParameterTypesMatch(self):
self._all_fine_test("unitest/test110.c")
if __name__ == '__main__':
unittest.main()
......@@ -7,6 +7,6 @@ typedef int (*prueba)(int);
int function(crust_t, int);
void main() {
prueba test1 = function;
prueba test1 = function; // ERROR: the number of parameters is different, and there are crust-type parameters involved (the first parameter)
__crust_disable__
}
typedef __crust_t__ unsigned char *crust_t;
typedef int (*prueba)(unsigned char *, int);
#define NULL ((void *)0)
int function(crust_t, int);
void main() {
prueba test1 = function; // ERROR: the first parameter doesn't match between both function definitions
__crust_disable__
}
typedef __crust_t__ unsigned char *crust_t;
typedef int (*prueba)(crust_t, int);
#define NULL ((void *)0)
crust_t function(crust_t, int);
void main() {
prueba test1 = function; // ERROR: the first parameter doesn't match between both function definitions
__crust_disable__
}
typedef __crust_t__ unsigned char *crust_t;
typedef int (*prueba)(crust_t, int);
#define NULL ((void *)0)
int function(crust_t, int);
void main() {
prueba test1 = function; // All fine, because both function signatures match
__crust_disable__
}
......@@ -6,6 +6,6 @@ void test_function(crust_t __crust_borrow__ param) {
}
void main(crust_t __crust_borrow__ param2) {
void (*test2)(crust_t param3);
void (*test2)(crust_t __crust_borrow__ param3);
test2 = test_function; // this is legal
}
......@@ -5,7 +5,7 @@ typedef __crust_t__ unsigned char *crust_t;
void test_function(crust_t __crust_borrow__ param) {
}
void (*test2)(crust_t param);
void (*test2)(crust_t __crust_borrow__ param);
void main(crust_t __crust_borrow__ param) {
test2 = test_function; // this is legal
......
......@@ -4,7 +4,7 @@ typedef char* (*prueba)(crust_t);
#define NULL ((void *)0)
void main(crust_t param1) {
char* main(crust_t param1) {
prueba test1 = main;
(*test1)(NULL,NULL); // ERROR: calling a function pointer with an incorrect number of parameters
__crust_disable__
......
......@@ -2,7 +2,7 @@ typedef __crust_t__ unsigned char *crust_t;
#define NULL ((void *)0)
void main(crust_t param1) {
char* main(crust_t param1) {
char* (*test1)(crust_t) = main;
(*test1)(NULL,NULL); // ERROR: calling a function pointer with incorrect number of parameters
__crust_disable__
......
......@@ -4,7 +4,7 @@ typedef char* (*prueba)(crust_t);
#define NULL ((void *)0)
void main(crust_t param1) {
char* main(crust_t param1) {
prueba test1 = main;
int b = 1;
(*test1)(b); // ERROR: calling a function pointer with an incorrect type of parameters
......
......@@ -2,7 +2,7 @@ typedef __crust_t__ unsigned char *crust_t;
#define NULL ((void *)0)
void main(crust_t param1) {
char* main(crust_t param1) {
char* (*test1)(crust_t) = main;
int b = 1;
(*test1)(b); // ERROR: calling a function pointer with incorrect type of parameters
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment