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
)
23 ret
= get_base_type(sym
);
24 if (ret
&& ret
->type
== SYM_RESTRICT
)
25 return get_real_base_type(ret
);
29 int type_bits(struct symbol
*type
)
33 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
34 return bits_in_pointer
;
35 return type
->bit_size
;
38 int type_positive_bits(struct symbol
*type
)
42 if (type_unsigned(type
))
43 return type_bits(type
);
44 return type_bits(type
) - 1;
47 static struct symbol
*get_binop_type(struct expression
*expr
)
49 struct symbol
*left
, *right
;
51 left
= get_type(expr
->left
);
52 right
= get_type(expr
->right
);
57 if (left
->type
== SYM_PTR
|| left
->type
== SYM_ARRAY
)
59 if (right
->type
== SYM_PTR
|| right
->type
== SYM_ARRAY
)
62 if (expr
->op
== SPECIAL_LEFTSHIFT
||
63 expr
->op
== SPECIAL_RIGHTSHIFT
) {
64 if (type_positive_bits(left
) < 31)
69 if (type_positive_bits(left
) < 31 && type_positive_bits(right
) < 31)
72 if (type_positive_bits(left
) > type_positive_bits(right
))
77 static struct symbol
*get_type_symbol(struct expression
*expr
)
79 if (!expr
|| expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
82 return get_real_base_type(expr
->symbol
);
85 static struct symbol
*get_member_symbol(struct symbol_list
*symbol_list
, struct ident
*member
)
87 struct symbol
*tmp
, *sub
;
89 FOR_EACH_PTR(symbol_list
, tmp
) {
91 sub
= get_real_base_type(tmp
);
92 sub
= get_member_symbol(sub
->symbol_list
, member
);
97 if (tmp
->ident
== member
)
99 } END_FOR_EACH_PTR(tmp
);
104 static struct symbol
*get_symbol_from_deref(struct expression
*expr
)
106 struct ident
*member
;
109 if (!expr
|| expr
->type
!= EXPR_DEREF
)
112 member
= expr
->member
;
113 sym
= get_type(expr
->deref
);
115 // sm_msg("could not find struct type");
118 if (sym
->type
== SYM_PTR
)
119 sym
= get_real_base_type(sym
);
120 sym
= get_member_symbol(sym
->symbol_list
, member
);
123 return get_real_base_type(sym
);
126 static struct symbol
*get_return_type(struct expression
*expr
)
130 tmp
= get_type(expr
->fn
);
133 return get_real_base_type(tmp
);
136 static struct symbol
*get_expr_stmt_type(struct statement
*stmt
)
138 if (stmt
->type
!= STMT_COMPOUND
)
140 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
141 if (!stmt
|| stmt
->type
!= STMT_EXPRESSION
)
143 return get_type(stmt
->expression
);
146 static struct symbol
*get_select_type(struct expression
*expr
)
148 struct symbol
*one
, *two
;
150 one
= get_type(expr
->cond_true
);
151 two
= get_type(expr
->cond_false
);
155 * This is a hack. If the types are not equiv then we
156 * really don't know the type. But I think guessing is
159 if (type_positive_bits(one
) > type_positive_bits(two
))
164 struct symbol
*get_pointer_type(struct expression
*expr
)
168 sym
= get_type(expr
);
169 if (!sym
|| (sym
->type
!= SYM_PTR
&& sym
->type
!= SYM_ARRAY
))
171 return get_real_base_type(sym
);
174 static struct symbol
*fake_pointer_sym(struct expression
*expr
)
179 sym
= alloc_symbol(expr
->pos
, SYM_PTR
);
181 base
= get_type(expr
);
184 sym
->ctype
.base_type
= base
;
188 struct symbol
*get_type(struct expression
*expr
)
192 expr
= strip_parens(expr
);
194 switch (expr
->type
) {
196 return &string_ctype
;
198 return get_type_symbol(expr
);
200 return get_symbol_from_deref(expr
);
204 return fake_pointer_sym(expr
);
206 return get_pointer_type(expr
->unop
);
207 return get_type(expr
->unop
);
208 case EXPR_ASSIGNMENT
:
209 return get_type(expr
->left
);
211 case EXPR_FORCE_CAST
:
212 case EXPR_IMPLIED_CAST
:
213 return get_real_base_type(expr
->cast_type
);
216 return get_binop_type(expr
);
218 return get_return_type(expr
);
220 return get_expr_stmt_type(expr
->statement
);
221 case EXPR_CONDITIONAL
:
223 return get_select_type(expr
);
229 // sm_msg("unhandled type %d", expr->type);
235 int type_unsigned(struct symbol
*base_type
)
239 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
244 int type_signed(struct symbol
*base_type
)
248 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
253 int expr_unsigned(struct expression
*expr
)
257 sym
= get_type(expr
);
260 if (type_unsigned(sym
))
265 int returns_unsigned(struct symbol
*sym
)
269 sym
= get_base_type(sym
);
270 if (!sym
|| sym
->type
!= SYM_FN
)
272 sym
= get_base_type(sym
);
273 return type_unsigned(sym
);
276 int is_pointer(struct expression
*expr
)
280 sym
= get_type(expr
);
283 if (sym
== &string_ctype
)
285 if (sym
->type
== SYM_PTR
)
290 int returns_pointer(struct symbol
*sym
)
294 sym
= get_base_type(sym
);
295 if (!sym
|| sym
->type
!= SYM_FN
)
297 sym
= get_base_type(sym
);
298 if (sym
->type
== SYM_PTR
)
303 sval_t
sval_type_max(struct symbol
*base_type
)
307 ret
.value
= (~0ULL) >> 1;
308 ret
.type
= base_type
;
310 if (!base_type
|| !base_type
->bit_size
)
313 ret
.value
= (~0ULL) >> (64 - type_positive_bits(base_type
));
317 sval_t
sval_type_min(struct symbol
*base_type
)
321 if (!base_type
|| !base_type
->bit_size
)
322 base_type
= &llong_ctype
;
323 ret
.type
= base_type
;
325 if (type_unsigned(base_type
)) {
330 ret
.value
= (~0ULL) << type_positive_bits(base_type
);
335 int nr_bits(struct expression
*expr
)
339 type
= get_type(expr
);
342 return type_bits(type
);
345 int is_void_pointer(struct expression
*expr
)
349 type
= get_type(expr
);
350 if (!type
|| type
->type
!= SYM_PTR
)
352 type
= get_real_base_type(type
);
353 if (type
== &void_ctype
)
358 int is_char_pointer(struct expression
*expr
)
362 type
= get_type(expr
);
363 if (!type
|| type
->type
!= SYM_PTR
)
365 type
= get_real_base_type(type
);
366 if (type
== &char_ctype
)
371 int is_static(struct expression
*expr
)
377 name
= expr_to_str_sym(expr
, &sym
);
381 if (sym
->ctype
.modifiers
& MOD_STATIC
)
388 int types_equiv(struct symbol
*one
, struct symbol
*two
)
394 if (one
->type
!= two
->type
)
396 if (one
->type
== SYM_PTR
)
397 return types_equiv(get_real_base_type(one
), get_real_base_type(two
));
398 if (type_positive_bits(one
) != type_positive_bits(two
))
405 return !!(cur_func_sym
->ctype
.modifiers
& MOD_STATIC
);
408 const char *global_static()
410 if (cur_func_sym
->ctype
.modifiers
& MOD_STATIC
)
416 struct symbol
*cur_func_return_type(void)
420 sym
= get_real_base_type(cur_func_sym
);
421 if (!sym
|| sym
->type
!= SYM_FN
)
423 sym
= get_real_base_type(sym
);
427 struct symbol
*get_arg_type(struct expression
*fn
, int arg
)
429 struct symbol
*fn_type
;
431 struct symbol
*arg_type
;
434 fn_type
= get_type(fn
);
437 if (fn_type
->type
== SYM_PTR
)
438 fn_type
= get_real_base_type(fn_type
);
439 if (fn_type
->type
!= SYM_FN
)
443 FOR_EACH_PTR(fn_type
->arguments
, tmp
) {
444 arg_type
= get_real_base_type(tmp
);
449 } END_FOR_EACH_PTR(tmp
);
454 static struct symbol
*get_member_from_string(struct symbol_list
*symbol_list
, char *name
)
456 struct symbol
*tmp
, *sub
;
459 if (strncmp(name
, ".", 1) == 0)
461 if (strncmp(name
, "->", 2) == 0)
464 FOR_EACH_PTR(symbol_list
, tmp
) {
466 sub
= get_real_base_type(tmp
);
467 sub
= get_member_from_string(sub
->symbol_list
, name
);
473 if (strcmp(tmp
->ident
->name
, name
) == 0)
476 chunk_len
= strlen(tmp
->ident
->name
);
477 if (strncmp(tmp
->ident
->name
, name
, chunk_len
) == 0 &&
478 (name
[chunk_len
] == '.' || name
[chunk_len
] == '-')) {
479 sub
= get_real_base_type(tmp
);
480 return get_member_from_string(sub
->symbol_list
, name
+ chunk_len
);
483 } END_FOR_EACH_PTR(tmp
);
488 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
)
493 if (strcmp(key
, "$$") == 0)
494 return get_type(expr
);
496 if (strcmp(key
, "*$$") == 0) {
497 sym
= get_type(expr
);
498 if (!sym
|| sym
->type
!= SYM_PTR
)
500 return get_real_base_type(sym
);
503 name
= expr_to_str_sym(expr
, &sym
);
507 sym
= get_real_base_type(sym
);
508 if (sym
->type
== SYM_PTR
)
509 sym
= get_real_base_type(sym
);
512 sym
= get_member_from_string(sym
->symbol_list
, key
);
515 return get_real_base_type(sym
);