db: move an assignment outside the loop
[smatch.git] / smatch_type.c
blob38c95a1755ac671196b78a4cc1d3c71ce4596e46
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 <ctype.h>
24 #include "smatch.h"
25 #include "smatch_slist.h"
27 struct symbol *get_real_base_type(struct symbol *sym)
29 struct symbol *ret;
31 if (!sym)
32 return NULL;
33 if (sym->type == SYM_BASETYPE)
34 return sym;
35 ret = get_base_type(sym);
36 if (!ret)
37 return NULL;
38 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
39 return get_real_base_type(ret);
40 return ret;
43 int type_bytes(struct symbol *type)
45 int bits;
47 if (type && type->type == SYM_ARRAY)
48 return array_bytes(type);
50 bits = type_bits(type);
51 if (bits < 0)
52 return 0;
53 return bits_to_bytes(bits);
56 int array_bytes(struct symbol *type)
58 if (!type || type->type != SYM_ARRAY)
59 return 0;
60 if (!type->array_size)
61 return 0;
62 return bits_to_bytes(type->bit_size);
65 static struct symbol *get_binop_type(struct expression *expr)
67 struct symbol *left, *right;
69 left = get_type(expr->left);
70 if (!left)
71 return NULL;
73 if (expr->op == SPECIAL_LEFTSHIFT ||
74 expr->op == SPECIAL_RIGHTSHIFT) {
75 if (type_positive_bits(left) < 31)
76 return &int_ctype;
77 return left;
79 right = get_type(expr->right);
80 if (!right)
81 return NULL;
83 if (type_is_fp(left)) {
84 if (type_is_fp(right)) {
85 if (type_bits(left) > type_bits(right))
86 return left;
87 return right;
89 return left;
92 if (type_is_fp(right)) {
93 if (type_is_fp(left)) {
94 if (type_bits(right) > type_bits(left))
95 return right;
96 return left;
98 return right;
101 if (expr->op == '-' &&
102 (is_ptr_type(left) && is_ptr_type(right)))
103 return ssize_t_ctype;
105 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
106 return left;
107 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
108 return right;
110 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
111 return &int_ctype;
113 if (type_positive_bits(left) > type_positive_bits(right))
114 return left;
115 return right;
118 static struct symbol *get_type_symbol(struct expression *expr)
120 struct symbol *type;
122 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
123 return NULL;
125 type = get_real_base_type(expr->symbol);
126 if (type == &autotype_ctype)
127 return get_type(expr->symbol->initializer);
128 return type;
131 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
133 struct symbol *tmp, *sub;
135 FOR_EACH_PTR(symbol_list, tmp) {
136 if (!tmp->ident) {
137 sub = get_real_base_type(tmp);
138 sub = get_member_symbol(sub->symbol_list, member);
139 if (sub)
140 return sub;
141 continue;
143 if (tmp->ident == member)
144 return tmp;
145 } END_FOR_EACH_PTR(tmp);
147 return NULL;
150 static struct symbol *get_symbol_from_deref(struct expression *expr)
152 struct ident *member;
153 struct symbol *sym;
155 if (!expr || expr->type != EXPR_DEREF)
156 return NULL;
158 member = expr->member;
159 sym = get_type(expr->deref);
160 if (!sym) {
161 // sm_msg("could not find struct type");
162 return NULL;
164 if (sym->type == SYM_PTR)
165 sym = get_real_base_type(sym);
166 sym = get_member_symbol(sym->symbol_list, member);
167 if (!sym)
168 return NULL;
169 return get_real_base_type(sym);
172 static struct symbol *handle__builtin_choose_expr(struct expression *expr)
174 struct expression *const_expr, *expr1, *expr2;
175 sval_t sval;
177 const_expr = get_argument_from_call_expr(expr->args, 0);
178 expr1 = get_argument_from_call_expr(expr->args, 1);
179 expr2 = get_argument_from_call_expr(expr->args, 2);
181 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
182 return NULL;
183 if (sval.value)
184 return get_type(expr1);
185 else
186 return get_type(expr2);
189 static struct symbol *get_return_type(struct expression *expr)
191 struct symbol *tmp;
193 if (sym_name_is("__builtin_choose_expr", expr->fn))
194 return handle__builtin_choose_expr(expr);
196 tmp = get_type(expr->fn);
197 if (!tmp)
198 return NULL;
199 /* this is to handle __builtin_constant_p() */
200 if (tmp->type != SYM_FN)
201 tmp = get_base_type(tmp);
202 return get_real_base_type(tmp);
205 static struct symbol *get_expr_stmt_type(struct statement *stmt)
207 if (stmt->type != STMT_COMPOUND)
208 return NULL;
209 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
210 if (stmt->type == STMT_LABEL)
211 stmt = stmt->label_statement;
212 if (stmt->type != STMT_EXPRESSION)
213 return NULL;
214 return get_type(stmt->expression);
217 static struct symbol *get_select_type(struct expression *expr)
219 struct symbol *one, *two;
221 one = get_type(expr->cond_true);
222 two = get_type(expr->cond_false);
223 if (!one || !two)
224 return NULL;
226 * This is a hack. If the types are not equiv then we
227 * really don't know the type. But I think guessing is
228 * probably Ok here.
230 if (type_positive_bits(one) > type_positive_bits(two))
231 return one;
232 return two;
235 struct symbol *get_pointer_type(struct expression *expr)
237 struct symbol *sym;
239 sym = get_type(expr);
240 if (!sym)
241 return NULL;
242 if (sym->type == SYM_NODE) {
243 sym = get_real_base_type(sym);
244 if (!sym)
245 return NULL;
247 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
248 return NULL;
249 return get_real_base_type(sym);
252 static struct symbol *fake_pointer_sym(struct expression *expr)
254 struct symbol *sym;
255 struct symbol *base;
257 sym = alloc_symbol(expr->pos, SYM_PTR);
258 expr = expr->unop;
259 base = get_type(expr);
260 if (!base)
261 return NULL;
262 sym->ctype.base_type = base;
263 return sym;
266 static struct symbol *get_type_helper(struct expression *expr)
268 struct symbol *ret;
270 expr = strip_parens(expr);
271 if (!expr)
272 return NULL;
274 if (expr->ctype)
275 return expr->ctype;
277 switch (expr->type) {
278 case EXPR_STRING:
279 ret = &string_ctype;
280 break;
281 case EXPR_SYMBOL:
282 ret = get_type_symbol(expr);
283 break;
284 case EXPR_DEREF:
285 ret = get_symbol_from_deref(expr);
286 break;
287 case EXPR_PREOP:
288 case EXPR_POSTOP:
289 if (expr->op == '&')
290 ret = fake_pointer_sym(expr);
291 else if (expr->op == '*')
292 ret = get_pointer_type(expr->unop);
293 else
294 ret = get_type(expr->unop);
295 break;
296 case EXPR_ASSIGNMENT:
297 ret = get_type(expr->left);
298 break;
299 case EXPR_CAST:
300 case EXPR_FORCE_CAST:
301 case EXPR_IMPLIED_CAST:
302 ret = get_real_base_type(expr->cast_type);
303 break;
304 case EXPR_COMPARE:
305 case EXPR_BINOP:
306 ret = get_binop_type(expr);
307 break;
308 case EXPR_CALL:
309 ret = get_return_type(expr);
310 break;
311 case EXPR_STATEMENT:
312 ret = get_expr_stmt_type(expr->statement);
313 break;
314 case EXPR_CONDITIONAL:
315 case EXPR_SELECT:
316 ret = get_select_type(expr);
317 break;
318 case EXPR_SIZEOF:
319 ret = &ulong_ctype;
320 break;
321 case EXPR_LOGICAL:
322 ret = &int_ctype;
323 break;
324 case EXPR_OFFSETOF:
325 ret = &ulong_ctype;
326 break;
327 case EXPR_GENERIC:
328 return get_type_helper(strip_Generic(expr));
329 default:
330 return NULL;
333 if (ret && ret->type == SYM_TYPEOF)
334 ret = get_type(ret->initializer);
336 expr->ctype = ret;
337 return ret;
340 static struct symbol *get_final_type_helper(struct expression *expr)
343 * The problem is that I wrote a bunch of Smatch to think that
344 * you could do get_type() on an expression and it would give
345 * you what the comparison was type promoted to. This is wrong
346 * but fixing it is a big of work... Hence this horrible hack.
350 expr = strip_parens(expr);
351 if (!expr)
352 return NULL;
354 if (expr->type == EXPR_COMPARE)
355 return &int_ctype;
356 if (expr->type == EXPR_PREOP && expr->op == '!')
357 return &int_ctype;
359 return NULL;
362 struct symbol *get_type(struct expression *expr)
364 return get_type_helper(expr);
367 struct symbol *get_comparison_type(struct expression *expr)
370 * Eventually we will probably have to figure out how to make get_type()
371 * return &int_ctype so let's create a helper function to transition to.
373 return get_type_helper(expr);
376 struct symbol *get_final_type(struct expression *expr)
378 struct symbol *ret;
380 ret = get_final_type_helper(expr);
381 if (ret)
382 return ret;
383 return get_type_helper(expr);
386 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
388 struct symbol *ret = &int_ctype;
390 if (type_positive_bits(left) > type_positive_bits(ret))
391 ret = left;
392 if (type_positive_bits(right) > type_positive_bits(ret))
393 ret = right;
395 if (type_is_ptr(left))
396 ret = left;
397 if (type_is_ptr(right))
398 ret = right;
400 return ret;
403 int type_signed(struct symbol *base_type)
405 if (!base_type)
406 return 0;
407 if (base_type->ctype.modifiers & MOD_SIGNED)
408 return 1;
409 return 0;
412 int expr_unsigned(struct expression *expr)
414 struct symbol *sym;
416 sym = get_type(expr);
417 if (!sym)
418 return 0;
419 if (type_unsigned(sym))
420 return 1;
421 return 0;
424 int expr_signed(struct expression *expr)
426 struct symbol *sym;
428 sym = get_type(expr);
429 if (!sym)
430 return 0;
431 if (type_signed(sym))
432 return 1;
433 return 0;
436 int returns_unsigned(struct symbol *sym)
438 if (!sym)
439 return 0;
440 sym = get_base_type(sym);
441 if (!sym || sym->type != SYM_FN)
442 return 0;
443 sym = get_base_type(sym);
444 return type_unsigned(sym);
447 int is_pointer(struct expression *expr)
449 return type_is_ptr(get_final_type(expr));
452 bool is_void_ptr(struct symbol *type)
454 if (!type)
455 return false;
456 if (type->type != SYM_PTR)
457 return false;
458 type = get_real_base_type(type);
460 return types_equiv(type, &void_ctype);
463 int returns_pointer(struct symbol *sym)
465 if (!sym)
466 return 0;
467 sym = get_base_type(sym);
468 if (!sym || sym->type != SYM_FN)
469 return 0;
470 sym = get_base_type(sym);
471 if (sym && sym->type == SYM_PTR)
472 return 1;
473 return 0;
476 static sval_t fp_max(struct symbol *type)
478 sval_t ret = { .type = type };
480 if (type == &float_ctype)
481 ret.fvalue = FLT_MAX;
482 else if (type == &double_ctype)
483 ret.dvalue = DBL_MAX;
484 else
485 ret.ldvalue = LDBL_MAX;
487 return ret;
490 sval_t sval_type_max(struct symbol *base_type)
492 sval_t ret;
494 if (type_is_fp(base_type))
495 return fp_max(base_type);
497 if (!base_type || !type_bits(base_type))
498 base_type = &llong_ctype;
499 ret.type = base_type;
501 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
502 return ret;
505 static sval_t fp_min(struct symbol *type)
507 sval_t ret = { .type = type };
509 if (type == &float_ctype)
510 ret.fvalue = -FLT_MAX;
511 else if (type == &double_ctype)
512 ret.dvalue = -DBL_MAX;
513 else
514 ret.ldvalue = -LDBL_MAX;
516 return ret;
519 sval_t sval_type_min(struct symbol *base_type)
521 sval_t ret;
523 if (type_is_fp(base_type))
524 return fp_min(base_type);
526 if (!base_type || !type_bits(base_type))
527 base_type = &llong_ctype;
528 ret.type = base_type;
530 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
531 ret.value = 0;
532 return ret;
535 ret.value = (~0ULL) << type_positive_bits(base_type);
537 return ret;
540 int nr_bits(struct expression *expr)
542 struct symbol *type;
544 type = get_type(expr);
545 if (!type)
546 return 0;
547 return type_bits(type);
550 int is_void_pointer(struct expression *expr)
552 struct symbol *type;
554 type = get_type(expr);
555 if (!type || type->type != SYM_PTR)
556 return 0;
557 type = get_real_base_type(type);
558 if (type == &void_ctype)
559 return 1;
560 return 0;
563 int is_char_pointer(struct expression *expr)
565 struct symbol *type;
567 type = get_type(expr);
568 if (!type || type->type != SYM_PTR)
569 return 0;
570 type = get_real_base_type(type);
571 if (type == &char_ctype)
572 return 1;
573 return 0;
576 int is_string(struct expression *expr)
578 expr = strip_expr(expr);
579 if (!expr || expr->type != EXPR_STRING)
580 return 0;
581 if (expr->string)
582 return 1;
583 return 0;
586 bool is_struct_ptr(struct symbol *type)
588 if (!type || type->type != SYM_PTR)
589 return false;
590 type = get_real_base_type(type);
591 if (!type || type->type != SYM_STRUCT)
592 return false;
593 return true;
596 int is_static(struct expression *expr)
598 char *name;
599 struct symbol *sym;
600 int ret = 0;
602 name = expr_to_str_sym(expr, &sym);
603 if (!name || !sym)
604 goto free;
606 if (sym->ctype.modifiers & MOD_STATIC)
607 ret = 1;
608 free:
609 free_string(name);
610 return ret;
613 static struct expression *get_symbol_expr(struct expression *expr)
615 if (!expr)
616 return NULL;
617 while (expr && expr->type == EXPR_DEREF && expr->op == '.')
618 expr = strip_expr(expr->deref);
619 return expr;
622 bool is_local_variable(struct expression *expr)
624 struct symbol *sym;
626 expr = get_symbol_expr(expr);
627 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
628 return false;
629 sym = expr->symbol;
630 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
631 return true;
632 return false;
635 int types_equiv(struct symbol *one, struct symbol *two)
637 if (!one && !two)
638 return 1;
639 if (!one || !two)
640 return 0;
641 if (one->type != two->type)
642 return 0;
643 if (one->type == SYM_PTR)
644 return types_equiv(get_real_base_type(one), get_real_base_type(two));
645 if (type_positive_bits(one) != type_positive_bits(two))
646 return 0;
647 return 1;
650 bool type_fits(struct symbol *type, struct symbol *test)
652 if (!type || !test)
653 return false;
655 if (type == test)
656 return true;
658 if (type_bits(test) > type_bits(type))
659 return false;
660 if (type_signed(test) && !type_signed(type))
661 return false;
662 if (type_positive_bits(test) > type_positive_bits(type))
663 return false;
664 return true;
667 int fn_static(void)
669 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
672 const char *global_static(void)
674 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
675 return "static";
676 else
677 return "global";
680 struct symbol *cur_func_return_type(void)
682 struct symbol *sym;
684 sym = get_real_base_type(cur_func_sym);
685 if (!sym || sym->type != SYM_FN)
686 return NULL;
687 sym = get_real_base_type(sym);
688 return sym;
691 struct symbol *get_arg_type(struct expression *fn, int arg)
693 struct symbol *fn_type;
694 struct symbol *tmp;
695 struct symbol *arg_type;
696 int i;
698 fn_type = get_type(fn);
699 if (!fn_type)
700 return NULL;
701 if (fn_type->type == SYM_PTR)
702 fn_type = get_real_base_type(fn_type);
703 if (fn_type->type != SYM_FN)
704 return NULL;
706 i = 0;
707 FOR_EACH_PTR(fn_type->arguments, tmp) {
708 arg_type = get_real_base_type(tmp);
709 if (i == arg) {
710 return arg_type;
712 i++;
713 } END_FOR_EACH_PTR(tmp);
715 return NULL;
718 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
720 struct symbol *tmp, *sub;
721 int chunk_len;
723 if (strncmp(name, ".", 1) == 0)
724 name += 1;
725 else if (strncmp(name, "->", 2) == 0)
726 name += 2;
728 FOR_EACH_PTR(symbol_list, tmp) {
729 if (!tmp->ident) {
730 sub = get_real_base_type(tmp);
731 sub = get_member_from_string(sub->symbol_list, name);
732 if (sub)
733 return sub;
734 continue;
737 if (strcmp(tmp->ident->name, name) == 0)
738 return tmp;
740 chunk_len = tmp->ident->len;
741 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
742 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
743 sub = get_real_base_type(tmp);
744 if (sub->type == SYM_PTR)
745 sub = get_real_base_type(sub);
746 return get_member_from_string(sub->symbol_list, name + chunk_len);
749 } END_FOR_EACH_PTR(tmp);
751 return NULL;
754 static struct symbol *get_type_from_container_of_key(struct expression *expr, const char *key)
756 char *new_key;
758 expr = map_container_of_to_simpler_expr_key(expr, key, &new_key);
759 if (!expr)
760 return NULL;
761 return get_member_type_from_key(expr, new_key);
764 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
766 struct symbol *sym;
767 int star = 0;
768 int i;
770 if (strcmp(key, "$") == 0)
771 return get_type(expr);
773 if (strcmp(key, "*$") == 0) {
774 sym = get_type(expr);
775 if (!sym)
776 return NULL;
777 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
778 return NULL;
779 return get_real_base_type(sym);
782 if (strstr(key, "<~$"))
783 return get_type_from_container_of_key(expr, key);
785 sym = get_type(expr);
786 if (!sym)
787 return NULL;
788 if (sym->type == SYM_PTR)
789 sym = get_real_base_type(sym);
791 while (*key == '*') {
792 key++;
793 star++;
796 if (*key != '$')
797 return NULL;
798 key++;
800 sym = get_member_from_string(sym->symbol_list, key);
801 if (!sym)
802 return NULL;
803 if (sym->type == SYM_RESTRICT || sym->type == SYM_NODE)
804 sym = get_real_base_type(sym);
805 for (i = 0; i < star; i++) {
806 if (!sym || sym->type != SYM_PTR)
807 return NULL;
808 sym = get_real_base_type(sym);
810 return sym;
813 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
815 struct symbol *type;
817 if (!key)
818 return NULL;
819 if (strcmp(key, "$") == 0)
820 return get_arg_type(fn, param);
821 if (strcmp(key, "*$") == 0) {
822 type = get_arg_type(fn, param);
823 if (!type || type->type != SYM_PTR)
824 return NULL;
825 return get_real_base_type(type);
827 return get_member_type_from_key(arg, key);
830 int is_struct(struct expression *expr)
832 struct symbol *type;
834 type = get_type(expr);
835 if (type && type->type == SYM_STRUCT)
836 return 1;
837 return 0;
840 static struct {
841 struct symbol *sym;
842 const char *name;
843 } base_types[] = {
844 {&bool_ctype, "bool"},
845 {&void_ctype, "void"},
846 {&type_ctype, "type"},
847 {&char_ctype, "char"},
848 {&schar_ctype, "schar"},
849 {&uchar_ctype, "uchar"},
850 {&short_ctype, "short"},
851 {&sshort_ctype, "sshort"},
852 {&ushort_ctype, "ushort"},
853 {&int_ctype, "int"},
854 {&sint_ctype, "sint"},
855 {&uint_ctype, "uint"},
856 {&long_ctype, "long"},
857 {&slong_ctype, "slong"},
858 {&ulong_ctype, "ulong"},
859 {&llong_ctype, "llong"},
860 {&sllong_ctype, "sllong"},
861 {&ullong_ctype, "ullong"},
862 {&int128_ctype, "lllong"},
863 {&sint128_ctype, "slllong"},
864 {&uint128_ctype, "ulllong"},
865 {&float_ctype, "float"},
866 {&double_ctype, "double"},
867 {&ldouble_ctype, "ldouble"},
868 {&string_ctype, "string"},
869 {&ptr_ctype, "ptr"},
870 {&lazy_ptr_ctype, "lazy_ptr"},
871 {&incomplete_ctype, "incomplete"},
872 {&label_ctype, "label"},
873 {&bad_ctype, "bad"},
874 {&null_ctype, "null"},
877 static const char *base_type_str(struct symbol *sym)
879 int i;
881 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
882 if (sym == base_types[i].sym)
883 return base_types[i].name;
885 return "<unknown>";
888 static int type_str_helper(char *buf, int size, struct symbol *type)
890 int n;
892 if (!type)
893 return snprintf(buf, size, "<null type>");
895 if (type->type == SYM_BASETYPE) {
896 return snprintf(buf, size, "%s", base_type_str(type));
897 } else if (type->type == SYM_PTR) {
898 type = get_real_base_type(type);
899 n = type_str_helper(buf, size, type);
900 if (n > size)
901 return n;
902 return n + snprintf(buf + n, size - n, "*");
903 } else if (type->type == SYM_ARRAY) {
904 type = get_real_base_type(type);
905 n = type_str_helper(buf, size, type);
906 if (n > size)
907 return n;
908 return n + snprintf(buf + n, size - n, "[]");
909 } else if (type->type == SYM_STRUCT) {
910 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
911 } else if (type->type == SYM_UNION) {
912 if (type->ident)
913 return snprintf(buf, size, "union %s", type->ident->name);
914 else
915 return snprintf(buf, size, "anonymous union");
916 } else if (type->type == SYM_FN) {
917 struct symbol *arg, *return_type, *arg_type;
918 int i;
920 return_type = get_real_base_type(type);
921 n = type_str_helper(buf, size, return_type);
922 if (n > size)
923 return n;
924 n += snprintf(buf + n, size - n, "(*)(");
925 if (n > size)
926 return n;
928 i = 0;
929 FOR_EACH_PTR(type->arguments, arg) {
930 if (i++)
931 n += snprintf(buf + n, size - n, ", ");
932 if (n > size)
933 return n;
934 arg_type = get_real_base_type(arg);
935 n += type_str_helper(buf + n, size - n, arg_type);
936 if (n > size)
937 return n;
938 } END_FOR_EACH_PTR(arg);
940 return n + snprintf(buf + n, size - n, ")");
941 } else if (type->type == SYM_NODE) {
942 n = snprintf(buf, size, "node {");
943 if (n > size)
944 return n;
945 type = get_real_base_type(type);
946 n += type_str_helper(buf + n, size - n, type);
947 if (n > size)
948 return n;
949 return n + snprintf(buf + n, size - n, "}");
950 } else if (type->type == SYM_ENUM) {
951 return snprintf(buf, size, "enum %s", type->ident ? type->ident->name : "<unknown>");
952 } else {
953 return snprintf(buf, size, "<type %d>", type->type);
957 char *type_to_str(struct symbol *type)
959 static char buf[256];
961 buf[0] = '\0';
962 type_str_helper(buf, sizeof(buf), type);
963 return alloc_sname(buf);