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 (type_max(left
) < type_max(&int_ctype
) &&
43 type_max(right
) < type_max(&int_ctype
))
46 if (type_max(right
) > type_max(left
))
51 static struct symbol
*get_type_symbol(struct expression
*expr
)
53 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
56 return get_real_base_type(expr
->symbol
);
59 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
62 struct symbol
*struct_sym
;
65 if (!expr
|| expr
->type
!= EXPR_DEREF
)
68 member
= expr
->member
;
69 struct_sym
= get_type(expr
->deref
);
71 // sm_msg("could not find struct type");
74 if (struct_sym
->type
== SYM_PTR
)
75 struct_sym
= get_real_base_type(struct_sym
);
76 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
77 if (tmp
->ident
== member
)
78 return get_real_base_type(tmp
);
79 } END_FOR_EACH_PTR(tmp
);
83 static struct symbol
*get_return_type(struct expression
*expr
)
87 tmp
= get_type(expr
->fn
);
90 return get_real_base_type(tmp
);
93 struct symbol
*get_pointer_type(struct expression
*expr
)
98 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
100 return get_real_base_type(sym
);
103 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
108 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
110 base
= get_type(expr
);
113 sym
->ctype
.base_type
= base
;
117 struct symbol
*get_type(struct expression
*expr
)
121 expr
= strip_parens(expr
);
123 switch (expr
->type
) {
125 return get_type_symbol(expr
);
127 return get_symbol_from_deref(expr
);
130 return fake_pointer_sym(expr
);
132 return get_pointer_type(expr
->unop
);
133 return get_type(expr
->unop
);
135 case EXPR_FORCE_CAST
:
136 case EXPR_IMPLIED_CAST
:
137 return get_real_base_type(expr
->cast_type
);
139 return get_binop_type(expr
);
141 return get_return_type(expr
);
146 // sm_msg("unhandled type %d", expr->type);
153 int type_unsigned(struct symbol
*base_type
)
157 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
162 int expr_unsigned(struct expression
*expr
)
166 sym
= get_type(expr
);
169 if (type_unsigned(sym
))
174 int returns_unsigned(struct symbol
*sym
)
178 sym
= get_base_type(sym
);
179 if (!sym
|| sym
->type
!= SYM_FN
)
181 sym
= get_base_type(sym
);
182 return type_unsigned(sym
);
185 int is_pointer(struct expression
*expr
)
189 sym
= get_type(expr
);
192 if (sym
->type
== SYM_PTR
)
197 int returns_pointer(struct symbol
*sym
)
201 sym
= get_base_type(sym
);
202 if (!sym
|| sym
->type
!= SYM_FN
)
204 sym
= get_base_type(sym
);
205 if (sym
->type
== SYM_PTR
)
210 long long type_max(struct symbol
*base_type
)
212 long long ret
= whole_range
.max
;
215 if (!base_type
|| !base_type
->bit_size
)
217 bits
= base_type
->bit_size
;
220 if (!type_unsigned(base_type
))
226 long long type_min(struct symbol
*base_type
)
228 long long ret
= whole_range
.min
;
231 if (!base_type
|| !base_type
->bit_size
)
233 if (type_unsigned(base_type
))
235 ret
= whole_range
.max
;
236 bits
= base_type
->bit_size
- 1;
241 int nr_bits(struct expression
*expr
)
245 type
= get_type(expr
);
248 return type
->bit_size
;
251 int is_static(struct expression
*expr
)
257 name
= get_variable_from_expr_complex(expr
, &sym
);
261 if (sym
->ctype
.modifiers
& MOD_STATIC
)
268 const char *global_static()
270 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)