helper: improve get_member_name() for anonymous structs
[smatch.git] / smatch_type.c
blob011110cf84bfda7cc3a5bdc7079b121ef9d8e35b
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 if (sym->type == SYM_BASETYPE)
33 return sym;
34 ret = get_base_type(sym);
35 if (!ret)
36 return NULL;
37 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
38 return get_real_base_type(ret);
39 return ret;
42 int type_bytes(struct symbol *type)
44 int bits;
46 if (type && type->type == SYM_ARRAY)
47 return array_bytes(type);
49 bits = type_bits(type);
50 if (bits < 0)
51 return 0;
52 return bits_to_bytes(bits);
55 int array_bytes(struct symbol *type)
57 if (!type || type->type != SYM_ARRAY)
58 return 0;
59 return bits_to_bytes(type->bit_size);
62 static struct symbol *get_binop_type(struct expression *expr)
64 struct symbol *left, *right;
66 left = get_type(expr->left);
67 if (!left)
68 return NULL;
70 if (expr->op == SPECIAL_LEFTSHIFT ||
71 expr->op == SPECIAL_RIGHTSHIFT) {
72 if (type_positive_bits(left) < 31)
73 return &int_ctype;
74 return left;
76 right = get_type(expr->right);
77 if (!right)
78 return NULL;
80 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
81 return left;
82 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
83 return right;
85 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
86 return &int_ctype;
88 if (type_positive_bits(left) > type_positive_bits(right))
89 return left;
90 return right;
93 static struct symbol *get_type_symbol(struct expression *expr)
95 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
96 return NULL;
98 return get_real_base_type(expr->symbol);
101 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
103 struct symbol *tmp, *sub;
105 FOR_EACH_PTR(symbol_list, tmp) {
106 if (!tmp->ident) {
107 sub = get_real_base_type(tmp);
108 sub = get_member_symbol(sub->symbol_list, member);
109 if (sub)
110 return sub;
111 continue;
113 if (tmp->ident == member)
114 return tmp;
115 } END_FOR_EACH_PTR(tmp);
117 return NULL;
120 static struct symbol *get_symbol_from_deref(struct expression *expr)
122 struct ident *member;
123 struct symbol *sym;
125 if (!expr || expr->type != EXPR_DEREF)
126 return NULL;
128 member = expr->member;
129 sym = get_type(expr->deref);
130 if (!sym) {
131 // sm_msg("could not find struct type");
132 return NULL;
134 if (sym->type == SYM_PTR)
135 sym = get_real_base_type(sym);
136 sym = get_member_symbol(sym->symbol_list, member);
137 if (!sym)
138 return NULL;
139 return get_real_base_type(sym);
142 static struct symbol *get_return_type(struct expression *expr)
144 struct symbol *tmp;
146 tmp = get_type(expr->fn);
147 if (!tmp)
148 return NULL;
149 /* this is to handle __builtin_constant_p() */
150 if (tmp->type != SYM_FN)
151 tmp = get_base_type(tmp);
152 return get_real_base_type(tmp);
155 static struct symbol *get_expr_stmt_type(struct statement *stmt)
157 if (stmt->type != STMT_COMPOUND)
158 return NULL;
159 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
160 if (stmt->type == STMT_LABEL)
161 stmt = stmt->label_statement;
162 if (stmt->type != STMT_EXPRESSION)
163 return NULL;
164 return get_type(stmt->expression);
167 static struct symbol *get_select_type(struct expression *expr)
169 struct symbol *one, *two;
171 one = get_type(expr->cond_true);
172 two = get_type(expr->cond_false);
173 if (!one || !two)
174 return NULL;
176 * This is a hack. If the types are not equiv then we
177 * really don't know the type. But I think guessing is
178 * probably Ok here.
180 if (type_positive_bits(one) > type_positive_bits(two))
181 return one;
182 return two;
185 struct symbol *get_pointer_type(struct expression *expr)
187 struct symbol *sym;
189 sym = get_type(expr);
190 if (!sym)
191 return NULL;
192 if (sym->type == SYM_NODE) {
193 sym = get_real_base_type(sym);
194 if (!sym)
195 return NULL;
197 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
198 return NULL;
199 return get_real_base_type(sym);
202 static struct symbol *fake_pointer_sym(struct expression *expr)
204 struct symbol *sym;
205 struct symbol *base;
207 sym = alloc_symbol(expr->pos, SYM_PTR);
208 expr = expr->unop;
209 base = get_type(expr);
210 if (!base)
211 return NULL;
212 sym->ctype.base_type = base;
213 return sym;
216 static struct symbol *get_type_helper(struct expression *expr)
218 struct symbol *ret;
220 expr = strip_parens(expr);
221 if (!expr)
222 return NULL;
224 if (expr->ctype)
225 return expr->ctype;
227 switch (expr->type) {
228 case EXPR_STRING:
229 ret = &string_ctype;
230 break;
231 case EXPR_SYMBOL:
232 ret = get_type_symbol(expr);
233 break;
234 case EXPR_DEREF:
235 ret = get_symbol_from_deref(expr);
236 break;
237 case EXPR_PREOP:
238 case EXPR_POSTOP:
239 if (expr->op == '&')
240 ret = fake_pointer_sym(expr);
241 else if (expr->op == '*')
242 ret = get_pointer_type(expr->unop);
243 else
244 ret = get_type(expr->unop);
245 break;
246 case EXPR_ASSIGNMENT:
247 ret = get_type(expr->left);
248 break;
249 case EXPR_CAST:
250 case EXPR_FORCE_CAST:
251 case EXPR_IMPLIED_CAST:
252 ret = get_real_base_type(expr->cast_type);
253 break;
254 case EXPR_COMPARE:
255 case EXPR_BINOP:
256 ret = get_binop_type(expr);
257 break;
258 case EXPR_CALL:
259 ret = get_return_type(expr);
260 break;
261 case EXPR_STATEMENT:
262 ret = get_expr_stmt_type(expr->statement);
263 break;
264 case EXPR_CONDITIONAL:
265 case EXPR_SELECT:
266 ret = get_select_type(expr);
267 break;
268 case EXPR_SIZEOF:
269 ret = &ulong_ctype;
270 break;
271 case EXPR_LOGICAL:
272 ret = &int_ctype;
273 break;
274 default:
275 return NULL;
278 if (ret && ret->type == SYM_TYPEOF)
279 ret = get_type(ret->initializer);
281 expr->ctype = ret;
282 return ret;
285 static struct symbol *get_final_type_helper(struct expression *expr)
288 * I'm not totally positive I understand types...
290 * So, when you're doing pointer math, and you do a subtraction, then
291 * the sval_binop() and whatever need to know the type of the pointer
292 * so they can figure out the alignment. But the result is going to be
293 * and ssize_t. So get_operation_type() gives you the pointer type
294 * and get_type() gives you ssize_t.
296 * Most of the time the operation type and the final type are the same
297 * but this just handles the few places where they are different.
301 expr = strip_parens(expr);
302 if (!expr)
303 return NULL;
305 switch (expr->type) {
306 case EXPR_COMPARE:
307 return &int_ctype;
308 case EXPR_BINOP: {
309 struct symbol *left, *right;
311 if (expr->op != '-')
312 return NULL;
314 left = get_type(expr->left);
315 right = get_type(expr->right);
316 if (type_is_ptr(left) || type_is_ptr(right))
317 return ssize_t_ctype;
321 return NULL;
324 struct symbol *get_type(struct expression *expr)
326 return get_type_helper(expr);
329 struct symbol *get_final_type(struct expression *expr)
331 struct symbol *ret;
333 ret = get_final_type_helper(expr);
334 if (ret)
335 return ret;
336 return get_type_helper(expr);
339 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
341 struct symbol *ret = &int_ctype;
343 if (type_positive_bits(left) > type_positive_bits(ret))
344 ret = left;
345 if (type_positive_bits(right) > type_positive_bits(ret))
346 ret = right;
348 if (type_is_ptr(left))
349 ret = left;
350 if (type_is_ptr(right))
351 ret = right;
353 return ret;
356 int type_signed(struct symbol *base_type)
358 if (!base_type)
359 return 0;
360 if (base_type->ctype.modifiers & MOD_SIGNED)
361 return 1;
362 return 0;
365 int expr_unsigned(struct expression *expr)
367 struct symbol *sym;
369 sym = get_type(expr);
370 if (!sym)
371 return 0;
372 if (type_unsigned(sym))
373 return 1;
374 return 0;
377 int expr_signed(struct expression *expr)
379 struct symbol *sym;
381 sym = get_type(expr);
382 if (!sym)
383 return 0;
384 if (type_signed(sym))
385 return 1;
386 return 0;
389 int returns_unsigned(struct symbol *sym)
391 if (!sym)
392 return 0;
393 sym = get_base_type(sym);
394 if (!sym || sym->type != SYM_FN)
395 return 0;
396 sym = get_base_type(sym);
397 return type_unsigned(sym);
400 int is_pointer(struct expression *expr)
402 struct symbol *sym;
404 sym = get_type(expr);
405 if (!sym)
406 return 0;
407 if (sym == &string_ctype)
408 return 0;
409 if (sym->type == SYM_PTR)
410 return 1;
411 return 0;
414 int returns_pointer(struct symbol *sym)
416 if (!sym)
417 return 0;
418 sym = get_base_type(sym);
419 if (!sym || sym->type != SYM_FN)
420 return 0;
421 sym = get_base_type(sym);
422 if (sym->type == SYM_PTR)
423 return 1;
424 return 0;
427 sval_t sval_type_max(struct symbol *base_type)
429 sval_t ret;
431 if (!base_type || !type_bits(base_type))
432 base_type = &llong_ctype;
433 ret.type = base_type;
435 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
436 return ret;
439 sval_t sval_type_min(struct symbol *base_type)
441 sval_t ret;
443 if (!base_type || !type_bits(base_type))
444 base_type = &llong_ctype;
445 ret.type = base_type;
447 if (type_unsigned(base_type)) {
448 ret.value = 0;
449 return ret;
452 ret.value = (~0ULL) << type_positive_bits(base_type);
454 return ret;
457 int nr_bits(struct expression *expr)
459 struct symbol *type;
461 type = get_type(expr);
462 if (!type)
463 return 0;
464 return type_bits(type);
467 int is_void_pointer(struct expression *expr)
469 struct symbol *type;
471 type = get_type(expr);
472 if (!type || type->type != SYM_PTR)
473 return 0;
474 type = get_real_base_type(type);
475 if (type == &void_ctype)
476 return 1;
477 return 0;
480 int is_char_pointer(struct expression *expr)
482 struct symbol *type;
484 type = get_type(expr);
485 if (!type || type->type != SYM_PTR)
486 return 0;
487 type = get_real_base_type(type);
488 if (type == &char_ctype)
489 return 1;
490 return 0;
493 int is_string(struct expression *expr)
495 expr = strip_expr(expr);
496 if (!expr || expr->type != EXPR_STRING)
497 return 0;
498 if (expr->string)
499 return 1;
500 return 0;
503 int is_static(struct expression *expr)
505 char *name;
506 struct symbol *sym;
507 int ret = 0;
509 name = expr_to_str_sym(expr, &sym);
510 if (!name || !sym)
511 goto free;
513 if (sym->ctype.modifiers & MOD_STATIC)
514 ret = 1;
515 free:
516 free_string(name);
517 return ret;
520 int is_local_variable(struct expression *expr)
522 struct symbol *sym;
523 char *name;
525 name = expr_to_var_sym(expr, &sym);
526 free_string(name);
527 if (!sym || !sym->scope || !sym->scope->token || !cur_func_sym)
528 return 0;
529 if (cmp_pos(sym->scope->token->pos, cur_func_sym->pos) < 0)
530 return 0;
531 if (is_static(expr))
532 return 0;
533 return 1;
536 int types_equiv(struct symbol *one, struct symbol *two)
538 if (!one && !two)
539 return 1;
540 if (!one || !two)
541 return 0;
542 if (one->type != two->type)
543 return 0;
544 if (one->type == SYM_PTR)
545 return types_equiv(get_real_base_type(one), get_real_base_type(two));
546 if (type_positive_bits(one) != type_positive_bits(two))
547 return 0;
548 return 1;
551 int fn_static(void)
553 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
556 const char *global_static(void)
558 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
559 return "static";
560 else
561 return "global";
564 struct symbol *cur_func_return_type(void)
566 struct symbol *sym;
568 sym = get_real_base_type(cur_func_sym);
569 if (!sym || sym->type != SYM_FN)
570 return NULL;
571 sym = get_real_base_type(sym);
572 return sym;
575 struct symbol *get_arg_type(struct expression *fn, int arg)
577 struct symbol *fn_type;
578 struct symbol *tmp;
579 struct symbol *arg_type;
580 int i;
582 fn_type = get_type(fn);
583 if (!fn_type)
584 return NULL;
585 if (fn_type->type == SYM_PTR)
586 fn_type = get_real_base_type(fn_type);
587 if (fn_type->type != SYM_FN)
588 return NULL;
590 i = 0;
591 FOR_EACH_PTR(fn_type->arguments, tmp) {
592 arg_type = get_real_base_type(tmp);
593 if (i == arg) {
594 return arg_type;
596 i++;
597 } END_FOR_EACH_PTR(tmp);
599 return NULL;
602 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
604 struct symbol *tmp, *sub;
605 int chunk_len;
607 if (strncmp(name, ".", 1) == 0)
608 name += 1;
609 else if (strncmp(name, "->", 2) == 0)
610 name += 2;
612 FOR_EACH_PTR(symbol_list, tmp) {
613 if (!tmp->ident) {
614 sub = get_real_base_type(tmp);
615 sub = get_member_from_string(sub->symbol_list, name);
616 if (sub)
617 return sub;
618 continue;
621 if (strcmp(tmp->ident->name, name) == 0)
622 return tmp;
624 chunk_len = tmp->ident->len;
625 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
626 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
627 sub = get_real_base_type(tmp);
628 if (sub->type == SYM_PTR)
629 sub = get_real_base_type(sub);
630 return get_member_from_string(sub->symbol_list, name + chunk_len);
633 } END_FOR_EACH_PTR(tmp);
635 return NULL;
638 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
640 struct symbol *sym;
642 if (strcmp(key, "$") == 0)
643 return get_type(expr);
645 if (strcmp(key, "*$") == 0) {
646 sym = get_type(expr);
647 if (!sym || sym->type != SYM_PTR)
648 return NULL;
649 return get_real_base_type(sym);
652 sym = get_type(expr);
653 if (!sym)
654 return NULL;
655 if (sym->type == SYM_PTR)
656 sym = get_real_base_type(sym);
658 key = key + 1;
659 sym = get_member_from_string(sym->symbol_list, key);
660 if (!sym)
661 return NULL;
662 return get_real_base_type(sym);
665 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
667 struct symbol *type;
669 if (!key)
670 return NULL;
671 if (strcmp(key, "$") == 0)
672 return get_arg_type(fn, param);
673 if (strcmp(key, "*$") == 0) {
674 type = get_arg_type(fn, param);
675 if (!type || type->type != SYM_PTR)
676 return NULL;
677 return get_real_base_type(type);
679 return get_member_type_from_key(arg, key);
682 int is_struct(struct expression *expr)
684 struct symbol *type;
686 type = get_type(expr);
687 if (type && type->type == SYM_STRUCT)
688 return 1;
689 return 0;
692 static struct {
693 struct symbol *sym;
694 const char *name;
695 } base_types[] = {
696 {&bool_ctype, "bool"},
697 {&void_ctype, "void"},
698 {&type_ctype, "type"},
699 {&char_ctype, "char"},
700 {&schar_ctype, "schar"},
701 {&uchar_ctype, "uchar"},
702 {&short_ctype, "short"},
703 {&sshort_ctype, "sshort"},
704 {&ushort_ctype, "ushort"},
705 {&int_ctype, "int"},
706 {&sint_ctype, "sint"},
707 {&uint_ctype, "uint"},
708 {&long_ctype, "long"},
709 {&slong_ctype, "slong"},
710 {&ulong_ctype, "ulong"},
711 {&llong_ctype, "llong"},
712 {&sllong_ctype, "sllong"},
713 {&ullong_ctype, "ullong"},
714 {&lllong_ctype, "lllong"},
715 {&slllong_ctype, "slllong"},
716 {&ulllong_ctype, "ulllong"},
717 {&float_ctype, "float"},
718 {&double_ctype, "double"},
719 {&ldouble_ctype, "ldouble"},
720 {&string_ctype, "string"},
721 {&ptr_ctype, "ptr"},
722 {&lazy_ptr_ctype, "lazy_ptr"},
723 {&incomplete_ctype, "incomplete"},
724 {&label_ctype, "label"},
725 {&bad_ctype, "bad"},
726 {&null_ctype, "null"},
729 static const char *base_type_str(struct symbol *sym)
731 int i;
733 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
734 if (sym == base_types[i].sym)
735 return base_types[i].name;
737 return "<unknown>";
740 static int type_str_helper(char *buf, int size, struct symbol *type)
742 int n;
744 if (!type)
745 return snprintf(buf, size, "<unknown>");
747 if (type->type == SYM_BASETYPE) {
748 return snprintf(buf, size, "%s", base_type_str(type));
749 } else if (type->type == SYM_PTR) {
750 type = get_real_base_type(type);
751 n = type_str_helper(buf, size, type);
752 if (n > size)
753 return n;
754 return n + snprintf(buf + n, size - n, "*");
755 } else if (type->type == SYM_ARRAY) {
756 type = get_real_base_type(type);
757 n = type_str_helper(buf, size, type);
758 if (n > size)
759 return n;
760 return n + snprintf(buf + n, size - n, "[]");
761 } else if (type->type == SYM_STRUCT) {
762 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
763 } else if (type->type == SYM_UNION) {
764 if (type->ident)
765 return snprintf(buf, size, "union %s", type->ident->name);
766 else
767 return snprintf(buf, size, "anonymous union");
768 } else if (type->type == SYM_FN) {
769 struct symbol *arg, *return_type, *arg_type;
770 int i;
772 return_type = get_real_base_type(type);
773 n = type_str_helper(buf, size, return_type);
774 if (n > size)
775 return n;
776 n += snprintf(buf + n, size - n, "(*)(");
777 if (n > size)
778 return n;
780 i = 0;
781 FOR_EACH_PTR(type->arguments, arg) {
782 if (i++)
783 n += snprintf(buf + n, size - n, ", ");
784 if (n > size)
785 return n;
786 arg_type = get_real_base_type(arg);
787 n += type_str_helper(buf + n, size - n, arg_type);
788 if (n > size)
789 return n;
790 } END_FOR_EACH_PTR(arg);
792 return n + snprintf(buf + n, size - n, ")");
793 } else if (type->type == SYM_NODE) {
794 n = snprintf(buf, size, "node {");
795 if (n > size)
796 return n;
797 type = get_real_base_type(type);
798 n += type_str_helper(buf + n, size - n, type);
799 if (n > size)
800 return n;
801 return n + snprintf(buf + n, size - n, "}");
802 } else {
803 return snprintf(buf, size, "<type %d>", type->type);
807 char *type_to_str(struct symbol *type)
809 static char buf[256];
811 buf[0] = '\0';
812 type_str_helper(buf, sizeof(buf), type);
813 return buf;