2 * Copyright (C) 2013 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
20 ALLOCATOR(var_sym
, "var_sym structs");
22 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
)
26 tmp
= __alloc_var_sym(0);
27 tmp
->var
= alloc_string(var
);
32 struct var_sym_list
*expr_to_vsl(struct expression
*expr
)
34 struct var_sym_list
*ret
= NULL
;
38 expr
= strip_expr(expr
);
42 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
43 return expr_to_vsl(expr
->unop
);
45 if (expr
->type
== EXPR_BINOP
||
46 expr
->type
== EXPR_LOGICAL
||
47 expr
->type
== EXPR_COMPARE
) {
48 struct var_sym_list
*left
, *right
;
50 left
= expr_to_vsl(expr
->left
);
51 right
= expr_to_vsl(expr
->right
);
52 ret
= combine_var_sym_lists(left
, right
);
53 free_var_syms_and_list(&left
);
54 free_var_syms_and_list(&right
);
57 var
= expr_to_var_sym(expr
, &sym
);
62 add_var_sym(&ret
, var
, sym
);
66 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
70 if (in_var_sym_list(*list
, var
, sym
))
72 tmp
= alloc_var_sym(var
, sym
);
73 add_ptr_list(list
, tmp
);
76 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
)
81 var
= expr_to_var_sym(expr
, &sym
);
84 add_var_sym(list
, var
, sym
);
89 static void free_var_sym(struct var_sym
*vs
)
95 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
99 FOR_EACH_PTR(*list
, tmp
) {
100 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0) {
101 DELETE_CURRENT_PTR(tmp
);
105 } END_FOR_EACH_PTR(tmp
);
108 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
)
112 FOR_EACH_PTR(list
, tmp
) {
113 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0)
115 } END_FOR_EACH_PTR(tmp
);
119 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
)
121 struct var_sym
*tmp
, *clone_vs
;
122 struct var_sym_list
*to_vsl
= NULL
;
124 FOR_EACH_PTR(from_vsl
, tmp
) {
125 clone_vs
= alloc_var_sym(tmp
->var
, tmp
->sym
);
126 add_ptr_list(&to_vsl
, clone_vs
);
127 } END_FOR_EACH_PTR(tmp
);
131 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
)
135 FOR_EACH_PTR(src
, tmp
) {
136 add_var_sym(dest
, tmp
->var
, tmp
->sym
);
137 } END_FOR_EACH_PTR(tmp
);
140 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
)
142 struct var_sym_list
*to_vsl
;
144 to_vsl
= clone_var_sym_list(one
);
145 merge_var_sym_list(&to_vsl
, two
);
149 void free_var_sym_list(struct var_sym_list
**list
)
151 __free_ptr_list((struct ptr_list
**)list
);
154 void free_var_syms_and_list(struct var_sym_list
**list
)
158 FOR_EACH_PTR(*list
, tmp
) {
160 } END_FOR_EACH_PTR(tmp
);
161 free_var_sym_list(list
);