Commit 30fdec92 authored by Sergio Costas's avatar Sergio Costas

Now keeps the original type when using typedefs to define variables and parameters

parent 8cf2e7f0
......@@ -8,6 +8,14 @@
struct AST *types_list = NULL;
struct AST *full_tree = NULL;
char *mystrdup(char *data) {
if (data == NULL) {
return NULL;
} else {
return strdup(data);
}
}
struct AST * new_leaf(int type, struct AST *template) {
struct AST *ast = (struct AST *)malloc(sizeof(struct AST));
......@@ -15,13 +23,15 @@ struct AST * new_leaf(int type, struct AST *template) {
ast->type = type;
if (template != NULL) {
ast->filename = strdup(template->filename);
ast->filename = mystrdup(template->filename);
ast->line = template->line;
} else {
ast->filename = NULL;
ast->line = -1;
}
ast->deftype = NULL;
ast->data = NULL;
ast->right = NULL;
......@@ -85,54 +95,57 @@ struct AST * new_leaf_char(int type, char *data) {
element->data = data;
}
void mix_ast_leaves(struct AST *base, struct AST *new_leaf) {
base->pointer += new_leaf->pointer;
base->t_const |= new_leaf->t_const;
base->t_restrict |= new_leaf->t_restrict;
base->t_volatile |= new_leaf->t_volatile;
base->t_void |= new_leaf->t_void;
base->t_char |= new_leaf->t_char;
base->t_short |= new_leaf->t_short;
base->t_int |= new_leaf->t_int;
if (base->t_long && new_leaf->t_long) {
void mix_ast_leaves(struct AST *base, struct AST *origin_leaf) {
base->pointer += origin_leaf->pointer;
base->t_const |= origin_leaf->t_const;
base->t_restrict |= origin_leaf->t_restrict;
base->t_volatile |= origin_leaf->t_volatile;
base->t_void |= origin_leaf->t_void;
base->t_char |= origin_leaf->t_char;
base->t_short |= origin_leaf->t_short;
base->t_int |= origin_leaf->t_int;
if (base->t_long && origin_leaf->t_long) {
base->t_long = false;
base->t_longlong = true;
} else {
base->t_long |= new_leaf->t_long;
base->t_long |= origin_leaf->t_long;
}
base->t_float |= new_leaf->t_float;
base->t_double |= new_leaf->t_double;
base->t_signed |= new_leaf->t_signed;
base->t_unsigned |= new_leaf->t_unsigned;
base->t_bool |= new_leaf->t_bool;
base->t_complex |= new_leaf->t_complex;
base->t_imaginary |= new_leaf->t_imaginary;
base->t_struct |= new_leaf->t_struct;
base->t_union |= new_leaf->t_union;
base->t_enum |= new_leaf->t_enum;
base->t_extern |= new_leaf->t_extern;
base->t_typedef |= new_leaf->t_typedef;
base->t_static |= new_leaf->t_static;
base->t_auto |= new_leaf->t_auto;
base->t_register |= new_leaf->t_register;
base->t_inline |= new_leaf->t_inline;
base->function |= new_leaf->function;
base->t_crust |= new_leaf->t_crust;
base->t_crust_borrow |= new_leaf->t_crust_borrow;
base->t_crust_recycle |= new_leaf->t_crust_recycle;
base->t_crust_alias |= new_leaf->t_crust_alias;
base->t_crust_no_zero |= new_leaf->t_crust_no_zero;
base->t_null |= new_leaf->t_null;
base->t_ellipsis |= new_leaf->t_ellipsis;
base->t_label |= new_leaf->t_label;
base->t_float |= origin_leaf->t_float;
base->t_double |= origin_leaf->t_double;
base->t_signed |= origin_leaf->t_signed;
base->t_unsigned |= origin_leaf->t_unsigned;
base->t_bool |= origin_leaf->t_bool;
base->t_complex |= origin_leaf->t_complex;
base->t_imaginary |= origin_leaf->t_imaginary;
base->t_struct |= origin_leaf->t_struct;
base->t_union |= origin_leaf->t_union;
base->t_enum |= origin_leaf->t_enum;
base->t_extern |= origin_leaf->t_extern;
base->t_typedef |= origin_leaf->t_typedef;
base->t_static |= origin_leaf->t_static;
base->t_auto |= origin_leaf->t_auto;
base->t_register |= origin_leaf->t_register;
base->t_inline |= origin_leaf->t_inline;
base->function |= origin_leaf->function;
base->t_crust |= origin_leaf->t_crust;
base->t_crust_borrow |= origin_leaf->t_crust_borrow;
base->t_crust_recycle |= origin_leaf->t_crust_recycle;
base->t_crust_alias |= origin_leaf->t_crust_alias;
base->t_crust_no_zero |= origin_leaf->t_crust_no_zero;
base->t_null |= origin_leaf->t_null;
base->t_ellipsis |= origin_leaf->t_ellipsis;
base->t_label |= origin_leaf->t_label;
if (base->function_params == NULL) {
base->function_params = copy_leaf(new_leaf->function_params,true);
base->function_params = copy_leaf(origin_leaf->function_params,true);
}
if (base->return_value == NULL) {
base->return_value = copy_leaf(new_leaf->return_value,false);
base->return_value = copy_leaf(origin_leaf->return_value,false);
}
//base->t_ |= new_leaf->t_;
if (base->deftype == NULL) {
base->deftype = mystrdup(origin_leaf->deftype);
}
//base->t_ |= origin_leaf->t_;
}
void copy_ast_data(struct AST *base, struct AST *new_leaf) {
......@@ -228,7 +241,7 @@ struct AST *find_type(char *type) {
#define COPY_ELEMENT(ELEMENT,DEEP) if (leaf->ELEMENT != NULL) {tmp->ELEMENT = copy_leaf(leaf->ELEMENT,DEEP);}
#define RESET_ELEMENT(ELEMENT) tmp->ELEMENT = NULL;
#define COPY_STRING(ELEMENT) if (leaf->ELEMENT != NULL) {tmp->ELEMENT = strdup(leaf->ELEMENT);}
#define COPY_STRING(ELEMENT) if (leaf->ELEMENT != NULL) {tmp->ELEMENT = mystrdup(leaf->ELEMENT);}
struct AST *copy_leaf(struct AST *leaf, bool deep) {
if (leaf == NULL) {
......@@ -239,6 +252,7 @@ struct AST *copy_leaf(struct AST *leaf, bool deep) {
COPY_STRING(data)
COPY_STRING(name)
COPY_STRING(filename)
COPY_STRING(deftype)
if (deep) {
COPY_ELEMENT(next,true);
} else {
......@@ -355,6 +369,7 @@ void free_tree(struct AST *tree) {
free(tree->filename);
free(tree->data);
free(tree->name);
free(tree->deftype);
free_tree(tree->child1);
free_tree(tree->child2);
free_tree(tree->child3);
......@@ -386,7 +401,7 @@ struct AST* parse_data(bool show_debug, char *filename, void *data, int size) {
do_verbose = show_debug ? 1 : 0;
types_list = NULL;
full_tree = NULL;
current_file = strdup(filename);
current_file = mystrdup(filename);
yyin = fmemopen(data,size,"r");
if (yyparse() == 0) {
return full_tree;
......@@ -406,7 +421,7 @@ int main(int argc,char **argv) {
int retval = -1;
types_list = NULL;
full_tree = NULL;
current_file = strdup(filename);
current_file = mystrdup(filename);
yyin = fopen(filename,"r");
......
......@@ -6,6 +6,8 @@ struct AST {
int line;
char *filename;
char *data;
char *name;
char *deftype;
int pointer;
bool t_const;
......@@ -44,8 +46,6 @@ struct AST {
bool t_ellipsis;
bool t_label;
char *name;
long long int intval;
double floatval;
......@@ -101,6 +101,7 @@ struct AST *transform_leaf(struct AST *leaf, int newtype);
void free_tree(struct AST *tree);
void free_all();
struct AST *copy_leaf(struct AST *leaf,bool deep);
char *mystrdup(char *data);
void check_typedef(struct AST *type);
......
......@@ -63,7 +63,7 @@ start_unit
primary_expression
: IDENTIFIER { $$ = $1;
$1->name = strdup($1->data);
$1->name = mystrdup($1->data);
}
| CONSTANT { $$ = $1; }
| FCONSTANT { $$ = $1; }
......@@ -322,7 +322,7 @@ declaration
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->name = mystrdup($2->name);
tmpleaf->return_value->pointer += $2->pointer;
}
if (tmpleaf->t_typedef) {
......@@ -388,13 +388,13 @@ type_specifier
| IMAGINARY { $$ = $1; $$->type = TYPE_SPECIFIER; }
| struct_or_union_specifier { $$ = $1; $$->type = TYPE_SPECIFIER; }
| enum_specifier { $$ = $1; $$->type = TYPE_SPECIFIER; }
| TYPE_NAME { $$ = $1; $$->type = TYPE_SPECIFIER; }
| TYPE_NAME { $$ = $1; $$->type = TYPE_SPECIFIER; $$->deftype = mystrdup($$->data); }
;
struct_or_union_specifier
: struct_or_union IDENTIFIER '{' struct_declaration_list '}' { $$ = $1;
free($$->name);
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
$$->struct_data = $4;
free_tree($2);
free_tree($3);
......@@ -405,7 +405,7 @@ struct_or_union_specifier
free_tree($4); }
| struct_or_union IDENTIFIER { $$ = $1;
free($$->name);
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
free_tree($2); }
;
......@@ -464,7 +464,7 @@ enum_specifier
free_tree($4); }
| ENUM IDENTIFIER '{' enumerator_list '}' { $$ = $1;
free($$->name);
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
$$->enum_data = $4;
free_tree($2);
free_tree($3);
......@@ -476,7 +476,7 @@ enum_specifier
free_tree($5); }
| ENUM IDENTIFIER '{' enumerator_list ',' '}' { $$ = $1;
free($$->name);
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
$$->enum_data = $4;
free_tree($2);
free_tree($3);
......@@ -484,7 +484,7 @@ enum_specifier
free_tree($6); }
| ENUM IDENTIFIER { $$ = $1;
free($$->name);
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
free_tree($2); }
;
......@@ -498,10 +498,10 @@ enumerator_list
enumerator
: IDENTIFIER { $$ = $1;
free($$->name);
$$->name = strdup($1->data); }
$$->name = mystrdup($1->data); }
| IDENTIFIER '=' constant_expression { $$ = $1;
free($$->name);
$$->name = strdup($1->data);
$$->name = mystrdup($1->data);
$$->assignment = $3;
free_tree($2); }
;
......@@ -531,7 +531,7 @@ declarator
direct_declarator
: IDENTIFIER { $$ = $1;
free($$->name);
$$->name = strdup($1->data); }
$$->name = mystrdup($1->data); }
| '(' declarator ')' { $$ = $2; // definition of a pointer to function
if ($$->pointer == 1) {
$$->function = true;
......@@ -623,7 +623,7 @@ parameter_declaration
: declaration_specifiers declarator { $$ = $1;
mix_ast_leaves($$,$2);
free($$->name);
$$->name = strdup($2->name);
$$->name = mystrdup($2->name);
free_tree($2); }
| declaration_specifiers abstract_declarator { $$ = $1;
if ($2->type == '*') {
......@@ -725,7 +725,7 @@ statement
labeled_statement
: IDENTIFIER ':' statement { $$ = $1;
free($$->name);
$$->name = strdup($1->data);
$$->name = mystrdup($1->data);
$$->type = LABEL;
append_next($$,$3);
free_tree($2); }
......@@ -857,7 +857,7 @@ iteration_statement
jump_statement
: GOTO IDENTIFIER ';' { $$ = $1;
$$->name = strdup($2->data);
$$->name = mystrdup($2->data);
free_tree($2);
free_tree($3); }
| CONTINUE ';' { $$ = $1;
......@@ -886,7 +886,7 @@ function_definition
: declaration_specifiers declarator declaration_list compound_statement { $$ = $1; show_error(__LINE__, $$); }
| declaration_specifiers declarator compound_statement { $$ = $2;
free($1->name);
$1->name = strdup($2->name);
$1->name = mystrdup($2->name);
$$->type = FUNCTION_DEFINITION;
$$->return_value = $1;
$$->return_value->pointer += $2->pointer;
......
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