2 * sparse/smatch_types.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * The idea here is that you have an expression and you
12 * want to know what the type is for that.
17 struct symbol
*get_real_base_type(struct symbol
*sym
)
21 ret
= get_base_type(sym
);
22 if (ret
&& ret
->type
== SYM_RESTRICT
)
23 return get_real_base_type(ret
);
27 static struct symbol
*get_binop_type(struct expression
*expr
)
29 struct symbol
*left
, *right
;
30 sval_t left_max
, right_max
, int_max
;
32 left
= get_type(expr
->left
);
33 right
= get_type(expr
->right
);
38 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
40 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
43 left_max
= sval_type_max(left
);
44 right_max
= sval_type_max(right
);
45 int_max
= sval_type_max(&int_ctype
);
47 if (expr
->op
== SPECIAL_LEFTSHIFT
||
48 expr
->op
== SPECIAL_RIGHTSHIFT
) {
49 if (sval_cmp(left_max
, int_max
) < 0)
54 if (sval_cmp(left_max
, int_max
) < 0 &&
55 sval_cmp(right_max
, int_max
) < 0)
58 if (sval_cmp(right_max
, left_max
) > 0)
63 static struct symbol
*get_type_symbol(struct expression
*expr
)
65 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
68 return get_real_base_type(expr
->symbol
);
71 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
74 struct symbol
*struct_sym
;
77 if (!expr
|| expr
->type
!= EXPR_DEREF
)
80 member
= expr
->member
;
81 struct_sym
= get_type(expr
->deref
);
83 // sm_msg("could not find struct type");
86 if (struct_sym
->type
== SYM_PTR
)
87 struct_sym
= get_real_base_type(struct_sym
);
88 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
89 if (tmp
->ident
== member
)
90 return get_real_base_type(tmp
);
91 } END_FOR_EACH_PTR(tmp
);
95 static struct symbol
*get_return_type(struct expression
*expr
)
99 tmp
= get_type(expr
->fn
);
102 return get_real_base_type(tmp
);
105 struct symbol
*get_pointer_type(struct expression
*expr
)
109 sym
= get_type(expr
);
110 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
112 return get_real_base_type(sym
);
115 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
120 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
122 base
= get_type(expr
);
125 sym
->ctype
.base_type
= base
;
129 struct symbol
*get_type(struct expression
*expr
)
133 expr
= strip_parens(expr
);
135 switch (expr
->type
) {
137 return get_type_symbol(expr
);
139 return get_symbol_from_deref(expr
);
142 return fake_pointer_sym(expr
);
144 return get_pointer_type(expr
->unop
);
145 return get_type(expr
->unop
);
146 case EXPR_ASSIGNMENT
:
147 return get_type(expr
->left
);
149 case EXPR_FORCE_CAST
:
150 case EXPR_IMPLIED_CAST
:
151 return get_real_base_type(expr
->cast_type
);
153 return get_binop_type(expr
);
155 return get_return_type(expr
);
163 // sm_msg("unhandled type %d", expr->type);
170 int type_unsigned(struct symbol
*base_type
)
174 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
179 int expr_unsigned(struct expression
*expr
)
183 sym
= get_type(expr
);
186 if (type_unsigned(sym
))
191 int returns_unsigned(struct symbol
*sym
)
195 sym
= get_base_type(sym
);
196 if (!sym
|| sym
->type
!= SYM_FN
)
198 sym
= get_base_type(sym
);
199 return type_unsigned(sym
);
202 int is_pointer(struct expression
*expr
)
206 sym
= get_type(expr
);
209 if (sym
->type
== SYM_PTR
)
214 int returns_pointer(struct symbol
*sym
)
218 sym
= get_base_type(sym
);
219 if (!sym
|| sym
->type
!= SYM_FN
)
221 sym
= get_base_type(sym
);
222 if (sym
->type
== SYM_PTR
)
227 sval_t
sval_type_max(struct symbol
*base_type
)
231 ret
.value
= (~0ULL) >> 1;
232 ret
.type
= base_type
;
234 if (!base_type
|| !base_type
->bit_size
)
237 if (type_unsigned(base_type
))
238 ret
.value
= (~0ULL) >> (64 - base_type
->bit_size
);
240 ret
.value
= (~0ULL) >> (64 - (base_type
->bit_size
- 1));
245 sval_t
sval_type_min(struct symbol
*base_type
)
249 if (!base_type
|| !base_type
->bit_size
)
250 base_type
= &llong_ctype
;
251 ret
.type
= base_type
;
253 if (type_unsigned(base_type
)) {
258 ret
.value
= (~0ULL) << (base_type
->bit_size
- 1);
263 int nr_bits(struct expression
*expr
)
267 type
= get_type(expr
);
270 return type
->bit_size
;
273 int is_static(struct expression
*expr
)
279 name
= get_variable_from_expr_complex(expr
, &sym
);
283 if (sym
->ctype
.modifiers
& MOD_STATIC
)
290 const char *global_static()
292 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)