type: include smatch_slist.h to prevent a segfault
[smatch.git] / smatch_type.c
blob183e14facf5e03e700fa5b80ce8dd0a93ac4de99
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The idea here is that you have an expression and you
20 * want to know what the type is for that.
23 #include "smatch.h"
24 #include "smatch_slist.h"
26 struct symbol *get_real_base_type(struct symbol *sym)
28 struct symbol *ret;
30 if (!sym)
31 return NULL;
32 ret = get_base_type(sym);
33 if (!ret)
34 return NULL;
35 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
36 return get_real_base_type(ret);
37 return ret;
40 int type_bits(struct symbol *type)
42 if (!type)
43 return 0;
44 if (type->type == SYM_PTR) /* Sparse doesn't set this for &pointers */
45 return bits_in_pointer;
46 if (!type->examined)
47 examine_symbol_type(type);
48 return type->bit_size;
51 int type_bytes(struct symbol *type)
53 int bits = type_bits(type);
55 if (bits < 0)
56 return 0;
57 return bits_to_bytes(bits);
60 int type_positive_bits(struct symbol *type)
62 if (!type)
63 return 0;
64 if (type_unsigned(type))
65 return type_bits(type);
66 return type_bits(type) - 1;
69 static struct symbol *get_binop_type(struct expression *expr)
71 struct symbol *left, *right;
73 left = get_type(expr->left);
74 right = get_type(expr->right);
76 if (!left || !right)
77 return NULL;
79 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
80 return left;
81 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
82 return right;
84 if (expr->op == SPECIAL_LEFTSHIFT ||
85 expr->op == SPECIAL_RIGHTSHIFT) {
86 if (type_positive_bits(left) < 31)
87 return &int_ctype;
88 return left;
91 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
92 return &int_ctype;
94 if (type_positive_bits(left) > type_positive_bits(right))
95 return left;
96 return right;
99 static struct symbol *get_type_symbol(struct expression *expr)
101 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
102 return NULL;
104 return get_real_base_type(expr->symbol);
107 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
109 struct symbol *tmp, *sub;
111 FOR_EACH_PTR(symbol_list, tmp) {
112 if (!tmp->ident) {
113 sub = get_real_base_type(tmp);
114 sub = get_member_symbol(sub->symbol_list, member);
115 if (sub)
116 return sub;
117 continue;
119 if (tmp->ident == member)
120 return tmp;
121 } END_FOR_EACH_PTR(tmp);
123 return NULL;
126 static struct symbol *get_symbol_from_deref(struct expression *expr)
128 struct ident *member;
129 struct symbol *sym;
131 if (!expr || expr->type != EXPR_DEREF)
132 return NULL;
134 member = expr->member;
135 sym = get_type(expr->deref);
136 if (!sym) {
137 // sm_msg("could not find struct type");
138 return NULL;
140 if (sym->type == SYM_PTR)
141 sym = get_real_base_type(sym);
142 sym = get_member_symbol(sym->symbol_list, member);
143 if (!sym)
144 return NULL;
145 return get_real_base_type(sym);
148 static struct symbol *get_return_type(struct expression *expr)
150 struct symbol *tmp;
152 tmp = get_type(expr->fn);
153 if (!tmp)
154 return NULL;
155 return get_real_base_type(tmp);
158 static struct symbol *get_expr_stmt_type(struct statement *stmt)
160 if (stmt->type != STMT_COMPOUND)
161 return NULL;
162 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
163 if (!stmt || stmt->type != STMT_EXPRESSION)
164 return NULL;
165 return get_type(stmt->expression);
168 static struct symbol *get_select_type(struct expression *expr)
170 struct symbol *one, *two;
172 one = get_type(expr->cond_true);
173 two = get_type(expr->cond_false);
174 if (!one || !two)
175 return NULL;
177 * This is a hack. If the types are not equiv then we
178 * really don't know the type. But I think guessing is
179 * probably Ok here.
181 if (type_positive_bits(one) > type_positive_bits(two))
182 return one;
183 return two;
186 struct symbol *get_pointer_type(struct expression *expr)
188 struct symbol *sym;
190 sym = get_type(expr);
191 if (!sym)
192 return NULL;
193 if (sym->type == SYM_NODE) {
194 sym = get_real_base_type(sym);
195 if (!sym)
196 return NULL;
198 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
199 return NULL;
200 return get_real_base_type(sym);
203 static struct symbol *fake_pointer_sym(struct expression *expr)
205 struct symbol *sym;
206 struct symbol *base;
208 sym = alloc_symbol(expr->pos, SYM_PTR);
209 expr = expr->unop;
210 base = get_type(expr);
211 if (!base)
212 return NULL;
213 sym->ctype.base_type = base;
214 return sym;
217 struct symbol *get_type(struct expression *expr)
219 if (!expr)
220 return NULL;
221 expr = strip_parens(expr);
223 switch (expr->type) {
224 case EXPR_STRING:
225 return &string_ctype;
226 case EXPR_SYMBOL:
227 return get_type_symbol(expr);
228 case EXPR_DEREF:
229 return get_symbol_from_deref(expr);
230 case EXPR_PREOP:
231 case EXPR_POSTOP:
232 if (expr->op == '&')
233 return fake_pointer_sym(expr);
234 if (expr->op == '*')
235 return get_pointer_type(expr->unop);
236 return get_type(expr->unop);
237 case EXPR_ASSIGNMENT:
238 return get_type(expr->left);
239 case EXPR_CAST:
240 case EXPR_FORCE_CAST:
241 case EXPR_IMPLIED_CAST:
242 return get_real_base_type(expr->cast_type);
243 case EXPR_COMPARE:
244 case EXPR_BINOP:
245 return get_binop_type(expr);
246 case EXPR_CALL:
247 return get_return_type(expr);
248 case EXPR_STATEMENT:
249 return get_expr_stmt_type(expr->statement);
250 case EXPR_CONDITIONAL:
251 case EXPR_SELECT:
252 return get_select_type(expr);
253 case EXPR_SIZEOF:
254 return &ulong_ctype;
255 case EXPR_LOGICAL:
256 return &int_ctype;
257 default:
258 // sm_msg("unhandled type %d", expr->type);
259 return expr->ctype;
261 return NULL;
264 int type_unsigned(struct symbol *base_type)
266 if (!base_type)
267 return 0;
268 if (base_type->ctype.modifiers & MOD_UNSIGNED)
269 return 1;
270 return 0;
273 int type_signed(struct symbol *base_type)
275 if (!base_type)
276 return 0;
277 if (base_type->ctype.modifiers & MOD_SIGNED)
278 return 1;
279 return 0;
282 int expr_unsigned(struct expression *expr)
284 struct symbol *sym;
286 sym = get_type(expr);
287 if (!sym)
288 return 0;
289 if (type_unsigned(sym))
290 return 1;
291 return 0;
294 int expr_signed(struct expression *expr)
296 struct symbol *sym;
298 sym = get_type(expr);
299 if (!sym)
300 return 0;
301 if (type_signed(sym))
302 return 1;
303 return 0;
306 int returns_unsigned(struct symbol *sym)
308 if (!sym)
309 return 0;
310 sym = get_base_type(sym);
311 if (!sym || sym->type != SYM_FN)
312 return 0;
313 sym = get_base_type(sym);
314 return type_unsigned(sym);
317 int is_pointer(struct expression *expr)
319 struct symbol *sym;
321 sym = get_type(expr);
322 if (!sym)
323 return 0;
324 if (sym == &string_ctype)
325 return 0;
326 if (sym->type == SYM_PTR)
327 return 1;
328 return 0;
331 int returns_pointer(struct symbol *sym)
333 if (!sym)
334 return 0;
335 sym = get_base_type(sym);
336 if (!sym || sym->type != SYM_FN)
337 return 0;
338 sym = get_base_type(sym);
339 if (sym->type == SYM_PTR)
340 return 1;
341 return 0;
344 sval_t sval_type_max(struct symbol *base_type)
346 sval_t ret;
348 if (!base_type || !type_bits(base_type))
349 base_type = &llong_ctype;
350 ret.type = base_type;
352 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
353 return ret;
356 sval_t sval_type_min(struct symbol *base_type)
358 sval_t ret;
360 if (!base_type || !type_bits(base_type))
361 base_type = &llong_ctype;
362 ret.type = base_type;
364 if (type_unsigned(base_type)) {
365 ret.value = 0;
366 return ret;
369 ret.value = (~0ULL) << type_positive_bits(base_type);
371 return ret;
374 int nr_bits(struct expression *expr)
376 struct symbol *type;
378 type = get_type(expr);
379 if (!type)
380 return 0;
381 return type_bits(type);
384 int is_void_pointer(struct expression *expr)
386 struct symbol *type;
388 type = get_type(expr);
389 if (!type || type->type != SYM_PTR)
390 return 0;
391 type = get_real_base_type(type);
392 if (type == &void_ctype)
393 return 1;
394 return 0;
397 int is_char_pointer(struct expression *expr)
399 struct symbol *type;
401 type = get_type(expr);
402 if (!type || type->type != SYM_PTR)
403 return 0;
404 type = get_real_base_type(type);
405 if (type == &char_ctype)
406 return 1;
407 return 0;
410 int is_string(struct expression *expr)
412 expr = strip_expr(expr);
413 if (!expr || expr->type != EXPR_STRING)
414 return 0;
415 if (expr->string)
416 return 1;
417 return 0;
420 int is_static(struct expression *expr)
422 char *name;
423 struct symbol *sym;
424 int ret = 0;
426 name = expr_to_str_sym(expr, &sym);
427 if (!name || !sym)
428 goto free;
430 if (sym->ctype.modifiers & MOD_STATIC)
431 ret = 1;
432 free:
433 free_string(name);
434 return ret;
437 int types_equiv(struct symbol *one, struct symbol *two)
439 if (!one && !two)
440 return 1;
441 if (!one || !two)
442 return 0;
443 if (one->type != two->type)
444 return 0;
445 if (one->type == SYM_PTR)
446 return types_equiv(get_real_base_type(one), get_real_base_type(two));
447 if (type_positive_bits(one) != type_positive_bits(two))
448 return 0;
449 return 1;
452 int fn_static(void)
454 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
457 const char *global_static(void)
459 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
460 return "static";
461 else
462 return "global";
465 struct symbol *cur_func_return_type(void)
467 struct symbol *sym;
469 sym = get_real_base_type(cur_func_sym);
470 if (!sym || sym->type != SYM_FN)
471 return NULL;
472 sym = get_real_base_type(sym);
473 return sym;
476 struct symbol *get_arg_type(struct expression *fn, int arg)
478 struct symbol *fn_type;
479 struct symbol *tmp;
480 struct symbol *arg_type;
481 int i;
483 fn_type = get_type(fn);
484 if (!fn_type)
485 return NULL;
486 if (fn_type->type == SYM_PTR)
487 fn_type = get_real_base_type(fn_type);
488 if (fn_type->type != SYM_FN)
489 return NULL;
491 i = 0;
492 FOR_EACH_PTR(fn_type->arguments, tmp) {
493 arg_type = get_real_base_type(tmp);
494 if (i == arg) {
495 return arg_type;
497 i++;
498 } END_FOR_EACH_PTR(tmp);
500 return NULL;
503 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, char *name)
505 struct symbol *tmp, *sub;
506 int chunk_len;
508 if (strncmp(name, ".", 1) == 0)
509 name += 1;
510 if (strncmp(name, "->", 2) == 0)
511 name += 2;
513 FOR_EACH_PTR(symbol_list, tmp) {
514 if (!tmp->ident) {
515 sub = get_real_base_type(tmp);
516 sub = get_member_from_string(sub->symbol_list, name);
517 if (sub)
518 return sub;
519 continue;
522 if (strcmp(tmp->ident->name, name) == 0)
523 return tmp;
525 chunk_len = strlen(tmp->ident->name);
526 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
527 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
528 sub = get_real_base_type(tmp);
529 return get_member_from_string(sub->symbol_list, name + chunk_len);
532 } END_FOR_EACH_PTR(tmp);
534 return NULL;
537 struct symbol *get_member_type_from_key(struct expression *expr, char *key)
539 struct symbol *sym;
540 char *name;
542 if (strcmp(key, "$") == 0)
543 return get_type(expr);
545 if (strcmp(key, "*$") == 0) {
546 sym = get_type(expr);
547 if (!sym || sym->type != SYM_PTR)
548 return NULL;
549 return get_real_base_type(sym);
552 name = expr_to_str_sym(expr, &sym);
553 free_string(name);
554 if (!sym)
555 return NULL;
556 sym = get_real_base_type(sym);
557 if (sym->type == SYM_PTR)
558 sym = get_real_base_type(sym);
560 key = key + 1;
561 sym = get_member_from_string(sym->symbol_list, key);
562 if (!sym)
563 return NULL;
564 return get_real_base_type(sym);
567 int is_struct(struct expression *expr)
569 struct symbol *type;
571 type = get_type(expr);
572 if (type && type->type == SYM_STRUCT)
573 return 1;
574 return 0;
577 static struct {
578 struct symbol *sym;
579 const char *name;
580 } base_types[] = {
581 {&bool_ctype, "bool"},
582 {&void_ctype, "void"},
583 {&type_ctype, "type"},
584 {&char_ctype, "char"},
585 {&schar_ctype, "schar"},
586 {&uchar_ctype, "uchar"},
587 {&short_ctype, "short"},
588 {&sshort_ctype, "sshort"},
589 {&ushort_ctype, "ushort"},
590 {&int_ctype, "int"},
591 {&sint_ctype, "sint"},
592 {&uint_ctype, "uint"},
593 {&long_ctype, "long"},
594 {&slong_ctype, "slong"},
595 {&ulong_ctype, "ulong"},
596 {&llong_ctype, "llong"},
597 {&sllong_ctype, "sllong"},
598 {&ullong_ctype, "ullong"},
599 {&lllong_ctype, "lllong"},
600 {&slllong_ctype, "slllong"},
601 {&ulllong_ctype, "ulllong"},
602 {&float_ctype, "float"},
603 {&double_ctype, "double"},
604 {&ldouble_ctype, "ldouble"},
605 {&string_ctype, "string"},
606 {&ptr_ctype, "ptr"},
607 {&lazy_ptr_ctype, "lazy_ptr"},
608 {&incomplete_ctype, "incomplete"},
609 {&label_ctype, "label"},
610 {&bad_ctype, "bad"},
611 {&null_ctype, "null"},
614 static const char *base_type_str(struct symbol *sym)
616 int i;
618 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
619 if (sym == base_types[i].sym)
620 return base_types[i].name;
622 return "<unknown>";
625 static int type_str_helper(char *buf, int size, struct symbol *type)
627 int n;
629 if (!type)
630 return snprintf(buf, size, "<unknown>");
632 if (type->type == SYM_BASETYPE) {
633 return snprintf(buf, size, base_type_str(type));
634 } else if (type->type == SYM_PTR) {
635 type = get_real_base_type(type);
636 n = type_str_helper(buf, size, type);
637 if (n > size)
638 return n;
639 return n + snprintf(buf + n, size - n, "*");
640 } else if (type->type == SYM_STRUCT) {
641 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
642 } else if (type->type == SYM_UNION) {
643 if (type->ident)
644 return snprintf(buf, size, "union %s", type->ident->name);
645 else
646 return snprintf(buf, size, "anonymous union");
647 } else if (type->type == SYM_FN) {
648 struct symbol *arg, *return_type, *arg_type;
649 int i;
651 return_type = get_real_base_type(type);
652 n = type_str_helper(buf, size, return_type);
653 if (n > size)
654 return n;
655 n += snprintf(buf + n, size - n, "(*)(");
656 if (n > size)
657 return n;
659 i = 0;
660 FOR_EACH_PTR(type->arguments, arg) {
661 if (i++)
662 n += snprintf(buf + n, size - n, ", ");
663 if (n > size)
664 return n;
665 arg_type = get_real_base_type(arg);
666 n += type_str_helper(buf + n, size - n, arg_type);
667 if (n > size)
668 return n;
669 } END_FOR_EACH_PTR(arg);
671 return n + snprintf(buf + n, size - n, ")");
672 } else if (type->type == SYM_NODE) {
673 n = snprintf(buf, size, "node {");
674 if (n > size)
675 return n;
676 type = get_real_base_type(type);
677 n += type_str_helper(buf + n, size - n, type);
678 if (n > size)
679 return n;
680 return n + snprintf(buf + n, size - n, "}");
681 } else {
682 return snprintf(buf, size, "<type %d>", type->type);
686 char *type_to_str(struct symbol *type)
688 static char buf[256];
690 buf[0] = '\0';
691 type_str_helper(buf, sizeof(buf), type);
692 return alloc_sname(buf);