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_type_symbol(struct expression
*expr
)
29 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
32 return get_real_base_type(expr
->symbol
);
35 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
38 struct symbol
*struct_sym
;
41 if (!expr
|| expr
->type
!= EXPR_DEREF
)
44 member
= expr
->member
;
45 struct_sym
= get_type(expr
->deref
);
47 // sm_msg("could not find struct type");
50 if (struct_sym
->type
== SYM_PTR
)
51 struct_sym
= get_real_base_type(struct_sym
);
52 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
53 if (tmp
->ident
== member
)
54 return get_real_base_type(tmp
);
55 } END_FOR_EACH_PTR(tmp
);
59 static struct symbol
*get_return_type(struct expression
*expr
)
63 tmp
= get_type(expr
->fn
);
66 return get_real_base_type(tmp
);
69 struct symbol
*get_pointer_type(struct expression
*expr
)
74 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
76 return get_real_base_type(sym
);
79 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
84 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
86 base
= get_type(expr
);
89 sym
->ctype
.base_type
= base
;
93 struct symbol
*get_type(struct expression
*expr
)
99 expr
= strip_parens(expr
);
101 switch (expr
->type
) {
103 return get_type_symbol(expr
);
105 return get_symbol_from_deref(expr
);
108 return fake_pointer_sym(expr
);
110 return get_pointer_type(expr
->unop
);
111 return get_type(expr
->unop
);
113 case EXPR_FORCE_CAST
:
114 case EXPR_IMPLIED_CAST
:
115 return get_real_base_type(expr
->cast_type
);
119 tmp
= get_type(expr
->left
);
122 return get_return_type(expr
);
127 // sm_msg("unhandled type %d", expr->type);
134 int type_unsigned(struct symbol
*base_type
)
138 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
143 int expr_unsigned(struct expression
*expr
)
147 sym
= get_type(expr
);
150 if (type_unsigned(sym
))
155 int returns_unsigned(struct symbol
*sym
)
159 sym
= get_base_type(sym
);
160 if (!sym
|| sym
->type
!= SYM_FN
)
162 sym
= get_base_type(sym
);
163 return type_unsigned(sym
);
166 int is_pointer(struct expression
*expr
)
170 sym
= get_type(expr
);
173 if (sym
->type
== SYM_PTR
)
178 int returns_pointer(struct symbol
*sym
)
182 sym
= get_base_type(sym
);
183 if (!sym
|| sym
->type
!= SYM_FN
)
185 sym
= get_base_type(sym
);
186 if (sym
->type
== SYM_PTR
)
191 long long type_max(struct symbol
*base_type
)
193 long long ret
= whole_range
.max
;
196 if (!base_type
|| !base_type
->bit_size
)
198 bits
= base_type
->bit_size
;
201 if (!type_unsigned(base_type
))
207 long long type_min(struct symbol
*base_type
)
209 long long ret
= whole_range
.min
;
212 if (!base_type
|| !base_type
->bit_size
)
214 if (type_unsigned(base_type
))
216 ret
= whole_range
.max
;
217 bits
= base_type
->bit_size
- 1;
222 int nr_bits(struct expression
*expr
)
226 type
= get_type(expr
);
229 return type
->bit_size
;
232 int is_static(struct expression
*expr
)
238 name
= get_variable_from_expr_complex(expr
, &sym
);
242 if (sym
->ctype
.modifiers
& MOD_STATIC
)
249 const char *global_static()
251 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)