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 static 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
);
125 // sm_msg("unhandled type %d", expr->type);
132 int type_unsigned(struct symbol
*base_type
)
136 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
141 int returns_unsigned(struct symbol
*sym
)
145 sym
= get_base_type(sym
);
146 if (!sym
|| sym
->type
!= SYM_FN
)
148 sym
= get_base_type(sym
);
149 return type_unsigned(sym
);
152 int returns_pointer(struct symbol
*sym
)
156 sym
= get_base_type(sym
);
157 if (!sym
|| sym
->type
!= SYM_FN
)
159 sym
= get_base_type(sym
);
160 if (sym
->type
== SYM_PTR
)
165 long long type_max(struct symbol
*base_type
)
167 long long ret
= whole_range
.max
;
170 if (!base_type
|| !base_type
->bit_size
)
172 bits
= base_type
->bit_size
;
175 if (!type_unsigned(base_type
))
181 long long type_min(struct symbol
*base_type
)
183 long long ret
= whole_range
.min
;
186 if (!base_type
|| !base_type
->bit_size
)
188 if (type_unsigned(base_type
))
190 ret
= whole_range
.max
;
191 bits
= base_type
->bit_size
- 1;