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 int type_bits(struct symbol
*type
)
31 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
32 return bits_in_pointer
;
33 return type
->bit_size
;
36 int type_positive_bits(struct symbol
*type
)
40 if (type_unsigned(type
))
41 return type_bits(type
);
42 return type_bits(type
) - 1;
45 static struct symbol
*get_binop_type(struct expression
*expr
)
47 struct symbol
*left
, *right
;
49 left
= get_type(expr
->left
);
50 right
= get_type(expr
->right
);
55 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
57 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
60 if (expr
->op
== SPECIAL_LEFTSHIFT
||
61 expr
->op
== SPECIAL_RIGHTSHIFT
) {
62 if (type_positive_bits(left
) < 31)
67 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
70 if (type_positive_bits(left
) > type_positive_bits(right
))
75 static struct symbol
*get_type_symbol(struct expression
*expr
)
77 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
80 return get_real_base_type(expr
->symbol
);
83 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
85 struct symbol
*tmp
, *sub
;
87 FOR_EACH_PTR(symbol_list
, tmp
) {
89 sub
= get_real_base_type(tmp
);
90 sub
= get_member_symbol(sub
->symbol_list
, member
);
95 if (tmp
->ident
== member
)
97 } END_FOR_EACH_PTR(tmp
);
102 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
104 struct ident
*member
;
107 if (!expr
|| expr
->type
!= EXPR_DEREF
)
110 member
= expr
->member
;
111 sym
= get_type(expr
->deref
);
113 // sm_msg("could not find struct type");
116 if (sym
->type
== SYM_PTR
)
117 sym
= get_real_base_type(sym
);
118 sym
= get_member_symbol(sym
->symbol_list
, member
);
121 return get_real_base_type(sym
);
124 static struct symbol
*get_return_type(struct expression
*expr
)
128 tmp
= get_type(expr
->fn
);
131 return get_real_base_type(tmp
);
134 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
136 if (stmt
->type
!= STMT_COMPOUND
)
138 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
139 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
141 return get_type(stmt
->expression
);
144 static struct symbol
*get_select_type(struct expression
*expr
)
146 struct symbol
*one
, *two
;
148 one
= get_type(expr
->cond_true
);
149 two
= get_type(expr
->cond_false
);
153 * This is a hack. If the types are not equiv then we
154 * really don't know the type. But I think guessing is
157 if (type_positive_bits(one
) > type_positive_bits(two
))
162 struct symbol
*get_pointer_type(struct expression
*expr
)
166 sym
= get_type(expr
);
167 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
169 return get_real_base_type(sym
);
172 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
177 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
179 base
= get_type(expr
);
182 sym
->ctype
.base_type
= base
;
186 struct symbol
*get_type(struct expression
*expr
)
190 expr
= strip_parens(expr
);
192 switch (expr
->type
) {
194 return &string_ctype
;
196 return get_type_symbol(expr
);
198 return get_symbol_from_deref(expr
);
202 return fake_pointer_sym(expr
);
204 return get_pointer_type(expr
->unop
);
205 return get_type(expr
->unop
);
206 case EXPR_ASSIGNMENT
:
207 return get_type(expr
->left
);
209 case EXPR_FORCE_CAST
:
210 case EXPR_IMPLIED_CAST
:
211 return get_real_base_type(expr
->cast_type
);
214 return get_binop_type(expr
);
216 return get_return_type(expr
);
218 return get_expr_stmt_type(expr
->statement
);
219 case EXPR_CONDITIONAL
:
221 return get_select_type(expr
);
227 // sm_msg("unhandled type %d", expr->type);
233 int type_unsigned(struct symbol
*base_type
)
237 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
242 int type_signed(struct symbol
*base_type
)
246 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
251 int expr_unsigned(struct expression
*expr
)
255 sym
= get_type(expr
);
258 if (type_unsigned(sym
))
263 int returns_unsigned(struct symbol
*sym
)
267 sym
= get_base_type(sym
);
268 if (!sym
|| sym
->type
!= SYM_FN
)
270 sym
= get_base_type(sym
);
271 return type_unsigned(sym
);
274 int is_pointer(struct expression
*expr
)
278 sym
= get_type(expr
);
281 if (sym
== &string_ctype
)
283 if (sym
->type
== SYM_PTR
)
288 int returns_pointer(struct symbol
*sym
)
292 sym
= get_base_type(sym
);
293 if (!sym
|| sym
->type
!= SYM_FN
)
295 sym
= get_base_type(sym
);
296 if (sym
->type
== SYM_PTR
)
301 sval_t
sval_type_max(struct symbol
*base_type
)
305 ret
.value
= (~0ULL) >> 1;
306 ret
.type
= base_type
;
308 if (!base_type
|| !base_type
->bit_size
)
311 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
315 sval_t
sval_type_min(struct symbol
*base_type
)
319 if (!base_type
|| !base_type
->bit_size
)
320 base_type
= &llong_ctype
;
321 ret
.type
= base_type
;
323 if (type_unsigned(base_type
)) {
328 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
333 int nr_bits(struct expression
*expr
)
337 type
= get_type(expr
);
340 return type_bits(type
);
343 int is_void_pointer(struct expression
*expr
)
347 type
= get_type(expr
);
348 if (!type
|| type
->type
!= SYM_PTR
)
350 type
= get_real_base_type(type
);
351 if (type
== &void_ctype
)
356 int is_char_pointer(struct expression
*expr
)
360 type
= get_type(expr
);
361 if (!type
|| type
->type
!= SYM_PTR
)
363 type
= get_real_base_type(type
);
364 if (type
== &char_ctype
)
369 int is_static(struct expression
*expr
)
375 name
= expr_to_str_sym(expr
, &sym
);
379 if (sym
->ctype
.modifiers
& MOD_STATIC
)
386 int types_equiv(struct symbol
*one
, struct symbol
*two
)
392 if (one
->type
!= two
->type
)
394 if (one
->type
== SYM_PTR
)
395 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
396 if (type_positive_bits(one
) != type_positive_bits(two
))
403 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
406 const char *global_static()
408 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
414 struct symbol
*cur_func_return_type(void)
418 sym
= get_real_base_type(cur_func_sym
);
419 if (!sym
|| sym
->type
!= SYM_FN
)
421 sym
= get_real_base_type(sym
);
425 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
427 struct symbol
*fn_type
;
429 struct symbol
*arg_type
;
432 fn_type
= get_type(fn
);
435 if (fn_type
->type
== SYM_PTR
)
436 fn_type
= get_real_base_type(fn_type
);
437 if (fn_type
->type
!= SYM_FN
)
441 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
442 arg_type
= get_real_base_type(tmp
);
447 } END_FOR_EACH_PTR(tmp
);
452 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
454 struct symbol
*tmp
, *sub
;
457 if (strncmp(name
, ".", 1) == 0)
459 if (strncmp(name
, "->", 2) == 0)
462 FOR_EACH_PTR(symbol_list
, tmp
) {
464 sub
= get_real_base_type(tmp
);
465 sub
= get_member_from_string(sub
->symbol_list
, name
);
471 if (strcmp(tmp
->ident
->name
, name
) == 0)
474 chunk_len
= strlen(tmp
->ident
->name
);
475 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
476 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
477 sub
= get_real_base_type(tmp
);
478 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
481 } END_FOR_EACH_PTR(tmp
);
486 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
491 if (strcmp(key
, "$$") == 0)
492 return get_type(expr
);
494 if (strcmp(key
, "*$$") == 0) {
495 sym
= get_type(expr
);
496 if (!sym
|| sym
->type
!= SYM_PTR
)
498 return get_real_base_type(sym
);
501 name
= expr_to_str_sym(expr
, &sym
);
505 sym
= get_real_base_type(sym
);
506 if (sym
->type
== SYM_PTR
)
507 sym
= get_real_base_type(sym
);
510 sym
= get_member_from_string(sym
->symbol_list
, key
);
513 return get_real_base_type(sym
);