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 expression
*unop
;
35 struct var_sym_list
*ret
= NULL
;
39 expr
= strip_expr(expr
);
43 if ((expr
->type
== EXPR_PREOP
&& expr
->op
== '*')) {
44 unop
= strip_expr(expr
->unop
);
46 if (unop
->type
== EXPR_SYMBOL
)
48 return expr_to_vsl(unop
);
51 if (expr
->type
== EXPR_BINOP
||
52 expr
->type
== EXPR_LOGICAL
||
53 expr
->type
== EXPR_COMPARE
) {
54 struct var_sym_list
*left
, *right
;
56 left
= expr_to_vsl(expr
->left
);
57 right
= expr_to_vsl(expr
->right
);
58 ret
= combine_var_sym_lists(left
, right
);
59 free_var_syms_and_list(&left
);
60 free_var_syms_and_list(&right
);
64 if (expr
->type
== EXPR_DEREF
)
65 return expr_to_vsl(expr
->deref
);
68 var
= expr_to_var_sym(expr
, &sym
);
73 add_var_sym(&ret
, var
, sym
);
77 int cmp_var_sym(const struct var_sym
*a
, const struct var_sym
*b
)
88 ret
= strcmp(a
->var
, b
->var
);
94 if (!b
->sym
&& a
->sym
)
96 if (!a
->sym
&& b
->sym
)
106 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
108 struct var_sym
*tmp
, *new;
110 if (in_var_sym_list(*list
, var
, sym
))
112 new = alloc_var_sym(var
, sym
);
114 FOR_EACH_PTR(*list
, tmp
) {
115 if (cmp_var_sym(tmp
, new) < 0)
117 else if (cmp_var_sym(tmp
, new) == 0) {
120 INSERT_CURRENT(new, tmp
);
123 } END_FOR_EACH_PTR(tmp
);
124 add_ptr_list(list
, new);
127 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
)
132 var
= expr_to_var_sym(expr
, &sym
);
135 add_var_sym(list
, var
, sym
);
140 static void free_var_sym(struct var_sym
*vs
)
142 free_string(vs
->var
);
146 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
150 FOR_EACH_PTR(*list
, tmp
) {
151 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0) {
152 DELETE_CURRENT_PTR(tmp
);
156 } END_FOR_EACH_PTR(tmp
);
159 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
)
163 FOR_EACH_PTR(list
, tmp
) {
164 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0)
166 } END_FOR_EACH_PTR(tmp
);
170 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
)
172 struct var_sym
*tmp
, *clone_vs
;
173 struct var_sym_list
*to_vsl
= NULL
;
175 FOR_EACH_PTR(from_vsl
, tmp
) {
176 clone_vs
= alloc_var_sym(tmp
->var
, tmp
->sym
);
177 add_ptr_list(&to_vsl
, clone_vs
);
178 } END_FOR_EACH_PTR(tmp
);
182 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
)
186 FOR_EACH_PTR(src
, tmp
) {
187 add_var_sym(dest
, tmp
->var
, tmp
->sym
);
188 } END_FOR_EACH_PTR(tmp
);
191 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
)
193 struct var_sym_list
*to_vsl
;
195 to_vsl
= clone_var_sym_list(one
);
196 merge_var_sym_list(&to_vsl
, two
);
200 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
)
202 struct var_sym
*one_tmp
, *two_tmp
;
207 if (ptr_list_size((struct ptr_list
*)one
) != ptr_list_size((struct ptr_list
*)two
))
210 PREPARE_PTR_LIST(one
, one_tmp
);
211 PREPARE_PTR_LIST(two
, two_tmp
);
213 if (!one_tmp
&& !two_tmp
)
215 if (one_tmp
->sym
!= two_tmp
->sym
)
217 if (strcmp(one_tmp
->var
, two_tmp
->var
) != 0)
219 NEXT_PTR_LIST(one_tmp
);
220 NEXT_PTR_LIST(two_tmp
);
222 FINISH_PTR_LIST(two_tmp
);
223 FINISH_PTR_LIST(one_tmp
);
228 void free_var_sym_list(struct var_sym_list
**list
)
230 __free_ptr_list((struct ptr_list
**)list
);
233 void free_var_syms_and_list(struct var_sym_list
**list
)
237 FOR_EACH_PTR(*list
, tmp
) {
239 } END_FOR_EACH_PTR(tmp
);
240 free_var_sym_list(list
);