Commit adbfb3a0 authored by Sergio Costas's avatar Sergio Costas

Moved the function's return type into its own pointer, to allow to return...

Moved the function's return type into its own pointer, to allow to return function pointers and check the type
parent 86d04ba9
......@@ -574,7 +574,7 @@ class crust(object):
function_data = self._find_function(thread_status, statement.name, statement.line)
function_data_base = self._find_variable(thread_status, statement.name, statement.line)
function_status = function_data_base["value"]
has_ellipsis = function_data[0]["ellipsis"]
has_ellipsis = function_data_base["ellipsis"]
if function_data[0]["crust"]:
retval_type = self.TYPE_CRUST
......@@ -1055,11 +1055,11 @@ class crust(object):
counter += 1
if counter > 1:
self._add_error(thread_status, self.MSG_CRITICAL, "The function '{:s}', at line {:d}, has more than one RECYCLE parameter", node.name, node.line)
if (counter != 0) and (not self._check_statement_is_crust(node)):
if (counter != 0) and (not self._check_statement_is_crust(node.return_value[0])):
self._add_error(thread_status, self.MSG_CRITICAL, "The function '{:s}', at line {:d}, has a RECYCLE parameter, but doesn't return a CRUST pointer", node.name, node.line)
if not node.t_typedef:
# the first parameter is the return value
parameters = [ self._get_variable_properties(thread_status, node, node.pointer, False, True) ]
parameters = [ self._get_variable_properties(thread_status, node.return_value[0], node.return_value[0].pointer, False, True) ]
if node.function_params is not None:
for parameter in node.function_params:
parameters.append( self._get_variable_properties(thread_status, parameter, parameter.pointer, True) )
......@@ -1069,6 +1069,7 @@ class crust(object):
tmpparams["value"] = crust.VALUE_FUNCTION
tmpparams["function_params"] = parameters
tmpparams["name"] = node.name
tmpparams["ellipsis"] = node.t_ellipsis
thread_status["variables"][0][node.name] = tmpparams
continue
if node.type == "CRUST_DEBUG":
......
......@@ -82,16 +82,15 @@ class tokens(object):
335:'FUNCTION_CALL',
336:'FUNCTION_DEFINITION',
337:'FUNCTION_DECLARATION',
338:'FUNCTION_POINTER_DECLARATION',
339:'BRACKETS_CONTAINER',
340:'LABEL',
341:'END_CASE',
342:'END_DEFAULT',
343:'END_SWITCH',
344:'START_BLOCK',
345:'END_BLOCK',
346:'TYPECAST',
347:'TYPENAME_IDENTIFIER',
338:'BRACKETS_CONTAINER',
339:'LABEL',
340:'END_CASE',
341:'END_DEFAULT',
342:'END_SWITCH',
343:'START_BLOCK',
344:'END_BLOCK',
345:'TYPECAST',
346:'TYPENAME_IDENTIFIER',
'IDENTIFIER':258,
'CONSTANT':259,
'STRING_LITERAL':260,
......@@ -172,16 +171,15 @@ class tokens(object):
'FUNCTION_CALL':335,
'FUNCTION_DEFINITION':336,
'FUNCTION_DECLARATION':337,
'FUNCTION_POINTER_DECLARATION':338,
'BRACKETS_CONTAINER':339,
'LABEL':340,
'END_CASE':341,
'END_DEFAULT':342,
'END_SWITCH':343,
'START_BLOCK':344,
'END_BLOCK':345,
'TYPECAST':346,
'TYPENAME_IDENTIFIER':347,
'BRACKETS_CONTAINER':338,
'LABEL':339,
'END_CASE':340,
'END_DEFAULT':341,
'END_SWITCH':342,
'START_BLOCK':343,
'END_BLOCK':344,
'TYPECAST':345,
'TYPENAME_IDENTIFIER':346,
}
@staticmethod
......
......@@ -32,6 +32,7 @@ struct AST * new_leaf(int type, struct AST *template) {
ast->condition = NULL;
ast->next = NULL;
ast->function_params = NULL;
ast->return_value = NULL;
ast->struct_data = NULL;
ast->bitfield = NULL;
ast->enum_data = NULL;
......@@ -128,6 +129,9 @@ void mix_ast_leaves(struct AST *base, struct AST *new_leaf) {
if (base->function_params == NULL) {
base->function_params = copy_leaf(new_leaf->function_params,true);
}
if (base->return_value == NULL) {
base->return_value = copy_leaf(new_leaf->return_value,false);
}
//base->t_ |= new_leaf->t_;
}
......@@ -255,6 +259,7 @@ struct AST *copy_leaf(struct AST *leaf, bool deep) {
RESET_ELEMENT(for_ch2)
RESET_ELEMENT(for_ch3)
COPY_ELEMENT(function_params,true)
COPY_ELEMENT(return_value,false)
return (tmp);
}
......@@ -339,6 +344,7 @@ void check_tree(struct AST *tree) {
check_tree(tree->for_ch3);
check_tree(tree->right);
check_tree(tree->function_params);
check_tree(tree->return_value);
}
}
......@@ -356,6 +362,7 @@ void free_tree(struct AST *tree) {
free_tree(tree->right);
free_tree(tree->condition);
free_tree(tree->function_params);
free_tree(tree->return_value);
free_tree(tree->bitfield);
free_tree(tree->struct_data);
free_tree(tree->enum_data);
......
......@@ -59,6 +59,7 @@ struct AST {
struct AST *condition;
struct AST *function_params;
struct AST *return_value;
struct AST *bitfield;
struct AST *struct_data;
struct AST *enum_data;
......
......@@ -45,7 +45,6 @@ void add_type_element(char *);
%token FUNCTION_CALL
%token FUNCTION_DEFINITION
%token FUNCTION_DECLARATION
%token FUNCTION_POINTER_DECLARATION
%token BRACKETS_CONTAINER
%token LABEL
%token END_CASE
......@@ -319,11 +318,16 @@ declaration
free_tree($2); }
| declaration_specifiers init_declarator_list ';' { $$ = $2;
for(tmpleaf = $2; tmpleaf != NULL; tmpleaf = tmpleaf->next) {
if ((tmpleaf->type != FUNCTION_DECLARATION) &&
(tmpleaf->type != FUNCTION_POINTER_DECLARATION)) {
if (tmpleaf->type != FUNCTION_DECLARATION) {
tmpleaf->type = VARIABLE_DEFINITION;
mix_ast_leaves(tmpleaf,$1);
} else {
tmpleaf->return_value = copy_leaf($1,false);
tmpleaf->t_typedef = $1->t_typedef;
free(tmpleaf->return_value->name);
tmpleaf->return_value->name = strdup($2->name);
tmpleaf->return_value->pointer += $2->pointer;
}
mix_ast_leaves(tmpleaf,$1);
if (tmpleaf->t_typedef) {
check_typedef(tmpleaf);
}
......@@ -872,15 +876,13 @@ external_declaration
function_definition
: declaration_specifiers declarator declaration_list compound_statement { $$ = $1; show_error(__LINE__, $$); }
| declaration_specifiers declarator compound_statement { $$ = $1;
| declaration_specifiers declarator compound_statement { $$ = $2;
free($1->name);
$1->name = strdup($2->name);
$$->type = FUNCTION_DEFINITION;
$$->pointer += $2->pointer;
free($$->name);
$$->name = strdup($2->name);
$$->function_params = $2->function_params;
$2->function_params = NULL;
$$->child1 = $3;
free_tree($2); }
$$->return_value = $1;
$$->return_value->pointer += $2->pointer;
$$->child1 = $3; }
;
declaration_list
......
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