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 returns_pointer(struct symbol
*sym
)
170 sym
= get_base_type(sym
);
171 if (!sym
|| sym
->type
!= SYM_FN
)
173 sym
= get_base_type(sym
);
174 if (sym
->type
== SYM_PTR
)
179 long long type_max(struct symbol
*base_type
)
181 long long ret
= whole_range
.max
;
184 if (!base_type
|| !base_type
->bit_size
)
186 bits
= base_type
->bit_size
;
189 if (!type_unsigned(base_type
))
195 long long type_min(struct symbol
*base_type
)
197 long long ret
= whole_range
.min
;
200 if (!base_type
|| !base_type
->bit_size
)
202 if (type_unsigned(base_type
))
204 ret
= whole_range
.max
;
205 bits
= base_type
->bit_size
- 1;
210 int nr_bits(struct expression
*expr
)
214 type
= get_type(expr
);
217 return type
->bit_size
;