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
;
31 left
= get_type(expr
->left
);
32 right
= get_type(expr
->right
);
37 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
39 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
42 if (expr
->op
== SPECIAL_LEFTSHIFT
||
43 expr
->op
== SPECIAL_RIGHTSHIFT
) {
44 if (type_max(left
) < type_max(&int_ctype
))
49 if (type_max(left
) < type_max(&int_ctype
) &&
50 type_max(right
) < type_max(&int_ctype
))
53 if (type_max(right
) > type_max(left
))
58 static struct symbol
*get_type_symbol(struct expression
*expr
)
60 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
63 return get_real_base_type(expr
->symbol
);
66 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
69 struct symbol
*struct_sym
;
72 if (!expr
|| expr
->type
!= EXPR_DEREF
)
75 member
= expr
->member
;
76 struct_sym
= get_type(expr
->deref
);
78 // sm_msg("could not find struct type");
81 if (struct_sym
->type
== SYM_PTR
)
82 struct_sym
= get_real_base_type(struct_sym
);
83 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
84 if (tmp
->ident
== member
)
85 return get_real_base_type(tmp
);
86 } END_FOR_EACH_PTR(tmp
);
90 static struct symbol
*get_return_type(struct expression
*expr
)
94 tmp
= get_type(expr
->fn
);
97 return get_real_base_type(tmp
);
100 struct symbol
*get_pointer_type(struct expression
*expr
)
104 sym
= get_type(expr
);
105 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
107 return get_real_base_type(sym
);
110 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
115 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
117 base
= get_type(expr
);
120 sym
->ctype
.base_type
= base
;
124 struct symbol
*get_type(struct expression
*expr
)
128 expr
= strip_parens(expr
);
130 switch (expr
->type
) {
132 return get_type_symbol(expr
);
134 return get_symbol_from_deref(expr
);
137 return fake_pointer_sym(expr
);
139 return get_pointer_type(expr
->unop
);
140 return get_type(expr
->unop
);
142 case EXPR_FORCE_CAST
:
143 case EXPR_IMPLIED_CAST
:
144 return get_real_base_type(expr
->cast_type
);
146 return get_binop_type(expr
);
148 return get_return_type(expr
);
153 // sm_msg("unhandled type %d", expr->type);
160 int type_unsigned(struct symbol
*base_type
)
164 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
169 int expr_unsigned(struct expression
*expr
)
173 sym
= get_type(expr
);
176 if (type_unsigned(sym
))
181 int returns_unsigned(struct symbol
*sym
)
185 sym
= get_base_type(sym
);
186 if (!sym
|| sym
->type
!= SYM_FN
)
188 sym
= get_base_type(sym
);
189 return type_unsigned(sym
);
192 int is_pointer(struct expression
*expr
)
196 sym
= get_type(expr
);
199 if (sym
->type
== SYM_PTR
)
204 int returns_pointer(struct symbol
*sym
)
208 sym
= get_base_type(sym
);
209 if (!sym
|| sym
->type
!= SYM_FN
)
211 sym
= get_base_type(sym
);
212 if (sym
->type
== SYM_PTR
)
217 long long type_max(struct symbol
*base_type
)
219 long long ret
= whole_range
.max
;
222 if (!base_type
|| !base_type
->bit_size
)
224 bits
= base_type
->bit_size
;
227 if (!type_unsigned(base_type
))
233 long long type_min(struct symbol
*base_type
)
235 long long ret
= whole_range
.min
;
238 if (!base_type
|| !base_type
->bit_size
)
240 if (type_unsigned(base_type
))
242 ret
= whole_range
.max
;
243 bits
= base_type
->bit_size
- 1;
248 int nr_bits(struct expression
*expr
)
252 type
= get_type(expr
);
255 return type
->bit_size
;
258 int is_static(struct expression
*expr
)
264 name
= get_variable_from_expr_complex(expr
, &sym
);
268 if (sym
->ctype
.modifiers
& MOD_STATIC
)
275 const char *global_static()
277 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)