Print this page
12257 resync smatch to 0.6.1-rc1-il-4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_type.c
+++ new/usr/src/tools/smatch/src/smatch_type.c
1 1 /*
2 2 * Copyright (C) 2009 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * The idea here is that you have an expression and you
20 20 * want to know what the type is for that.
21 21 */
22 22
23 23 #include "smatch.h"
24 24 #include "smatch_slist.h"
25 25
26 26 struct symbol *get_real_base_type(struct symbol *sym)
27 27 {
28 28 struct symbol *ret;
29 29
30 30 if (!sym)
31 31 return NULL;
32 32 if (sym->type == SYM_BASETYPE)
33 33 return sym;
34 34 ret = get_base_type(sym);
35 35 if (!ret)
36 36 return NULL;
37 37 if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
38 38 return get_real_base_type(ret);
39 39 return ret;
40 40 }
41 41
42 42 int type_bytes(struct symbol *type)
43 43 {
44 44 int bits;
45 45
46 46 if (type && type->type == SYM_ARRAY)
47 47 return array_bytes(type);
48 48
49 49 bits = type_bits(type);
50 50 if (bits < 0)
51 51 return 0;
52 52 return bits_to_bytes(bits);
53 53 }
54 54
55 55 int array_bytes(struct symbol *type)
56 56 {
57 57 if (!type || type->type != SYM_ARRAY)
58 58 return 0;
59 59 return bits_to_bytes(type->bit_size);
60 60 }
61 61
62 62 static struct symbol *get_binop_type(struct expression *expr)
63 63 {
64 64 struct symbol *left, *right;
65 65
66 66 left = get_type(expr->left);
67 67 if (!left)
68 68 return NULL;
69 69
70 70 if (expr->op == SPECIAL_LEFTSHIFT ||
71 71 expr->op == SPECIAL_RIGHTSHIFT) {
72 72 if (type_positive_bits(left) < 31)
73 73 return &int_ctype;
74 74 return left;
75 75 }
76 76 right = get_type(expr->right);
77 77 if (!right)
78 78 return NULL;
79 79
80 80 if (type_is_fp(left)) {
81 81 if (type_is_fp(right)) {
82 82 if (type_bits(left) > type_bits(right))
83 83 return left;
84 84 return right;
85 85 }
86 86 return left;
87 87 }
88 88
89 89 if (type_is_fp(right)) {
90 90 if (type_is_fp(left)) {
91 91 if (type_bits(right) > type_bits(left))
92 92 return right;
93 93 return left;
94 94 }
95 95 return right;
96 96 }
97 97
98 98 if (expr->op == '-' &&
99 99 (is_ptr_type(left) && is_ptr_type(right)))
100 100 return ssize_t_ctype;
101 101
102 102 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
103 103 return left;
104 104 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
105 105 return right;
106 106
107 107 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
108 108 return &int_ctype;
109 109
110 110 if (type_positive_bits(left) > type_positive_bits(right))
111 111 return left;
112 112 return right;
113 113 }
114 114
115 115 static struct symbol *get_type_symbol(struct expression *expr)
116 116 {
117 117 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
118 118 return NULL;
119 119
120 120 return get_real_base_type(expr->symbol);
121 121 }
122 122
123 123 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
124 124 {
125 125 struct symbol *tmp, *sub;
126 126
127 127 FOR_EACH_PTR(symbol_list, tmp) {
128 128 if (!tmp->ident) {
129 129 sub = get_real_base_type(tmp);
130 130 sub = get_member_symbol(sub->symbol_list, member);
131 131 if (sub)
132 132 return sub;
133 133 continue;
134 134 }
135 135 if (tmp->ident == member)
136 136 return tmp;
137 137 } END_FOR_EACH_PTR(tmp);
138 138
139 139 return NULL;
140 140 }
141 141
142 142 static struct symbol *get_symbol_from_deref(struct expression *expr)
143 143 {
144 144 struct ident *member;
145 145 struct symbol *sym;
146 146
147 147 if (!expr || expr->type != EXPR_DEREF)
148 148 return NULL;
149 149
150 150 member = expr->member;
151 151 sym = get_type(expr->deref);
152 152 if (!sym) {
153 153 // sm_msg("could not find struct type");
154 154 return NULL;
155 155 }
156 156 if (sym->type == SYM_PTR)
157 157 sym = get_real_base_type(sym);
158 158 sym = get_member_symbol(sym->symbol_list, member);
159 159 if (!sym)
160 160 return NULL;
161 161 return get_real_base_type(sym);
162 162 }
163 163
164 164 static struct symbol *handle__builtin_choose_expr(struct expression *expr)
165 165 {
166 166 struct expression *const_expr, *expr1, *expr2;
167 167 sval_t sval;
168 168
169 169 const_expr = get_argument_from_call_expr(expr->args, 0);
170 170 expr1 = get_argument_from_call_expr(expr->args, 1);
171 171 expr2 = get_argument_from_call_expr(expr->args, 2);
172 172
173 173 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
174 174 return NULL;
175 175 if (sval.value)
176 176 return get_type(expr1);
177 177 else
178 178 return get_type(expr2);
179 179 }
180 180
181 181 static struct symbol *get_return_type(struct expression *expr)
182 182 {
183 183 struct symbol *tmp;
184 184
185 185 if (sym_name_is("__builtin_choose_expr", expr->fn))
186 186 return handle__builtin_choose_expr(expr);
187 187
188 188 tmp = get_type(expr->fn);
189 189 if (!tmp)
190 190 return NULL;
191 191 /* this is to handle __builtin_constant_p() */
192 192 if (tmp->type != SYM_FN)
193 193 tmp = get_base_type(tmp);
194 194 return get_real_base_type(tmp);
195 195 }
196 196
197 197 static struct symbol *get_expr_stmt_type(struct statement *stmt)
198 198 {
199 199 if (stmt->type != STMT_COMPOUND)
200 200 return NULL;
201 201 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
202 202 if (stmt->type == STMT_LABEL)
203 203 stmt = stmt->label_statement;
204 204 if (stmt->type != STMT_EXPRESSION)
205 205 return NULL;
206 206 return get_type(stmt->expression);
207 207 }
208 208
209 209 static struct symbol *get_select_type(struct expression *expr)
210 210 {
211 211 struct symbol *one, *two;
212 212
213 213 one = get_type(expr->cond_true);
214 214 two = get_type(expr->cond_false);
215 215 if (!one || !two)
216 216 return NULL;
217 217 /*
218 218 * This is a hack. If the types are not equiv then we
219 219 * really don't know the type. But I think guessing is
220 220 * probably Ok here.
221 221 */
222 222 if (type_positive_bits(one) > type_positive_bits(two))
223 223 return one;
224 224 return two;
225 225 }
226 226
227 227 struct symbol *get_pointer_type(struct expression *expr)
228 228 {
229 229 struct symbol *sym;
230 230
231 231 sym = get_type(expr);
232 232 if (!sym)
233 233 return NULL;
234 234 if (sym->type == SYM_NODE) {
235 235 sym = get_real_base_type(sym);
236 236 if (!sym)
237 237 return NULL;
238 238 }
239 239 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
240 240 return NULL;
241 241 return get_real_base_type(sym);
242 242 }
243 243
244 244 static struct symbol *fake_pointer_sym(struct expression *expr)
245 245 {
246 246 struct symbol *sym;
247 247 struct symbol *base;
248 248
249 249 sym = alloc_symbol(expr->pos, SYM_PTR);
250 250 expr = expr->unop;
251 251 base = get_type(expr);
252 252 if (!base)
253 253 return NULL;
254 254 sym->ctype.base_type = base;
255 255 return sym;
256 256 }
257 257
258 258 static struct symbol *get_type_helper(struct expression *expr)
259 259 {
260 260 struct symbol *ret;
261 261
262 262 expr = strip_parens(expr);
263 263 if (!expr)
264 264 return NULL;
265 265
266 266 if (expr->ctype)
267 267 return expr->ctype;
268 268
269 269 switch (expr->type) {
270 270 case EXPR_STRING:
271 271 ret = &string_ctype;
272 272 break;
273 273 case EXPR_SYMBOL:
274 274 ret = get_type_symbol(expr);
275 275 break;
276 276 case EXPR_DEREF:
277 277 ret = get_symbol_from_deref(expr);
278 278 break;
279 279 case EXPR_PREOP:
280 280 case EXPR_POSTOP:
281 281 if (expr->op == '&')
282 282 ret = fake_pointer_sym(expr);
283 283 else if (expr->op == '*')
284 284 ret = get_pointer_type(expr->unop);
285 285 else
286 286 ret = get_type(expr->unop);
287 287 break;
288 288 case EXPR_ASSIGNMENT:
289 289 ret = get_type(expr->left);
290 290 break;
291 291 case EXPR_CAST:
292 292 case EXPR_FORCE_CAST:
293 293 case EXPR_IMPLIED_CAST:
294 294 ret = get_real_base_type(expr->cast_type);
295 295 break;
296 296 case EXPR_COMPARE:
297 297 case EXPR_BINOP:
298 298 ret = get_binop_type(expr);
299 299 break;
300 300 case EXPR_CALL:
301 301 ret = get_return_type(expr);
302 302 break;
303 303 case EXPR_STATEMENT:
304 304 ret = get_expr_stmt_type(expr->statement);
305 305 break;
306 306 case EXPR_CONDITIONAL:
307 307 case EXPR_SELECT:
308 308 ret = get_select_type(expr);
309 309 break;
310 310 case EXPR_SIZEOF:
311 311 ret = &ulong_ctype;
312 312 break;
313 313 case EXPR_LOGICAL:
314 314 ret = &int_ctype;
315 315 break;
316 316 case EXPR_OFFSETOF:
317 317 ret = &ulong_ctype;
318 318 break;
319 319 default:
320 320 return NULL;
321 321 }
322 322
323 323 if (ret && ret->type == SYM_TYPEOF)
324 324 ret = get_type(ret->initializer);
325 325
326 326 expr->ctype = ret;
327 327 return ret;
328 328 }
329 329
330 330 static struct symbol *get_final_type_helper(struct expression *expr)
331 331 {
332 332 /*
333 333 * The problem is that I wrote a bunch of Smatch to think that
334 334 * you could do get_type() on an expression and it would give
335 335 * you what the comparison was type promoted to. This is wrong
336 336 * but fixing it is a big of work... Hence this horrible hack.
337 337 *
338 338 */
339 339
340 340 expr = strip_parens(expr);
341 341 if (!expr)
342 342 return NULL;
343 343
344 344 if (expr->type == EXPR_COMPARE)
345 345 return &int_ctype;
346 346
347 347 return NULL;
348 348 }
349 349
350 350 struct symbol *get_type(struct expression *expr)
351 351 {
352 352 return get_type_helper(expr);
353 353 }
354 354
355 355 struct symbol *get_final_type(struct expression *expr)
356 356 {
357 357 struct symbol *ret;
358 358
359 359 ret = get_final_type_helper(expr);
360 360 if (ret)
361 361 return ret;
362 362 return get_type_helper(expr);
363 363 }
364 364
365 365 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right)
366 366 {
367 367 struct symbol *ret = &int_ctype;
368 368
369 369 if (type_positive_bits(left) > type_positive_bits(ret))
370 370 ret = left;
371 371 if (type_positive_bits(right) > type_positive_bits(ret))
372 372 ret = right;
373 373
374 374 if (type_is_ptr(left))
375 375 ret = left;
376 376 if (type_is_ptr(right))
377 377 ret = right;
378 378
379 379 return ret;
380 380 }
381 381
382 382 int type_signed(struct symbol *base_type)
383 383 {
384 384 if (!base_type)
385 385 return 0;
386 386 if (base_type->ctype.modifiers & MOD_SIGNED)
387 387 return 1;
388 388 return 0;
389 389 }
390 390
391 391 int expr_unsigned(struct expression *expr)
392 392 {
393 393 struct symbol *sym;
394 394
395 395 sym = get_type(expr);
396 396 if (!sym)
397 397 return 0;
398 398 if (type_unsigned(sym))
399 399 return 1;
400 400 return 0;
401 401 }
402 402
403 403 int expr_signed(struct expression *expr)
404 404 {
405 405 struct symbol *sym;
406 406
407 407 sym = get_type(expr);
408 408 if (!sym)
409 409 return 0;
410 410 if (type_signed(sym))
411 411 return 1;
412 412 return 0;
413 413 }
414 414
415 415 int returns_unsigned(struct symbol *sym)
416 416 {
417 417 if (!sym)
418 418 return 0;
419 419 sym = get_base_type(sym);
420 420 if (!sym || sym->type != SYM_FN)
421 421 return 0;
422 422 sym = get_base_type(sym);
423 423 return type_unsigned(sym);
424 424 }
425 425
426 426 int is_pointer(struct expression *expr)
427 427 {
428 428 return type_is_ptr(get_type(expr));
429 429 }
430 430
431 431 int returns_pointer(struct symbol *sym)
432 432 {
433 433 if (!sym)
434 434 return 0;
435 435 sym = get_base_type(sym);
436 436 if (!sym || sym->type != SYM_FN)
437 437 return 0;
438 438 sym = get_base_type(sym);
439 439 if (sym && sym->type == SYM_PTR)
440 440 return 1;
441 441 return 0;
442 442 }
443 443
444 444 static sval_t fp_max(struct symbol *type)
445 445 {
446 446 sval_t ret = { .type = type };
447 447
448 448 if (type == &float_ctype)
449 449 ret.fvalue = FLT_MAX;
450 450 else if (type == &double_ctype)
451 451 ret.dvalue = DBL_MAX;
452 452 else
453 453 ret.ldvalue = LDBL_MAX;
454 454
455 455 return ret;
456 456 }
457 457
458 458 sval_t sval_type_max(struct symbol *base_type)
459 459 {
460 460 sval_t ret;
461 461
462 462 if (type_is_fp(base_type))
463 463 return fp_max(base_type);
464 464
465 465 if (!base_type || !type_bits(base_type))
466 466 base_type = &llong_ctype;
467 467 ret.type = base_type;
468 468
469 469 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
470 470 return ret;
471 471 }
472 472
473 473 static sval_t fp_min(struct symbol *type)
474 474 {
475 475 sval_t ret = { .type = type };
476 476
477 477 if (type == &float_ctype)
478 478 ret.fvalue = -FLT_MAX;
479 479 else if (type == &double_ctype)
480 480 ret.dvalue = -DBL_MAX;
481 481 else
482 482 ret.ldvalue = -LDBL_MAX;
483 483
484 484 return ret;
485 485 }
486 486
487 487 sval_t sval_type_min(struct symbol *base_type)
488 488 {
489 489 sval_t ret;
490 490
491 491 if (type_is_fp(base_type))
492 492 return fp_min(base_type);
493 493
494 494 if (!base_type || !type_bits(base_type))
495 495 base_type = &llong_ctype;
496 496 ret.type = base_type;
497 497
498 498 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
499 499 ret.value = 0;
500 500 return ret;
501 501 }
502 502
503 503 ret.value = (~0ULL) << type_positive_bits(base_type);
504 504
505 505 return ret;
506 506 }
507 507
508 508 int nr_bits(struct expression *expr)
509 509 {
510 510 struct symbol *type;
511 511
512 512 type = get_type(expr);
513 513 if (!type)
514 514 return 0;
515 515 return type_bits(type);
516 516 }
517 517
518 518 int is_void_pointer(struct expression *expr)
519 519 {
520 520 struct symbol *type;
521 521
522 522 type = get_type(expr);
523 523 if (!type || type->type != SYM_PTR)
524 524 return 0;
525 525 type = get_real_base_type(type);
526 526 if (type == &void_ctype)
527 527 return 1;
528 528 return 0;
529 529 }
530 530
531 531 int is_char_pointer(struct expression *expr)
532 532 {
533 533 struct symbol *type;
534 534
535 535 type = get_type(expr);
536 536 if (!type || type->type != SYM_PTR)
537 537 return 0;
538 538 type = get_real_base_type(type);
539 539 if (type == &char_ctype)
540 540 return 1;
541 541 return 0;
542 542 }
543 543
544 544 int is_string(struct expression *expr)
545 545 {
546 546 expr = strip_expr(expr);
547 547 if (!expr || expr->type != EXPR_STRING)
548 548 return 0;
549 549 if (expr->string)
550 550 return 1;
551 551 return 0;
552 552 }
553 553
554 554 int is_static(struct expression *expr)
555 555 {
556 556 char *name;
557 557 struct symbol *sym;
558 558 int ret = 0;
559 559
560 560 name = expr_to_str_sym(expr, &sym);
561 561 if (!name || !sym)
562 562 goto free;
563 563
564 564 if (sym->ctype.modifiers & MOD_STATIC)
565 565 ret = 1;
566 566 free:
567 567 free_string(name);
568 568 return ret;
569 569 }
570 570
571 571 bool is_local_variable(struct expression *expr)
572 572 {
573 573 struct symbol *sym;
574 574
575 575 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
576 576 return false;
577 577 sym = expr->symbol;
578 578 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
579 579 return true;
580 580 return false;
581 581 }
582 582
583 583 int types_equiv(struct symbol *one, struct symbol *two)
584 584 {
585 585 if (!one && !two)
586 586 return 1;
587 587 if (!one || !two)
588 588 return 0;
589 589 if (one->type != two->type)
590 590 return 0;
591 591 if (one->type == SYM_PTR)
592 592 return types_equiv(get_real_base_type(one), get_real_base_type(two));
593 593 if (type_positive_bits(one) != type_positive_bits(two))
594 594 return 0;
595 595 return 1;
596 596 }
597 597
598 598 int fn_static(void)
599 599 {
600 600 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
601 601 }
602 602
603 603 const char *global_static(void)
604 604 {
605 605 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
606 606 return "static";
607 607 else
608 608 return "global";
609 609 }
610 610
611 611 struct symbol *cur_func_return_type(void)
612 612 {
613 613 struct symbol *sym;
614 614
615 615 sym = get_real_base_type(cur_func_sym);
616 616 if (!sym || sym->type != SYM_FN)
617 617 return NULL;
618 618 sym = get_real_base_type(sym);
619 619 return sym;
620 620 }
621 621
622 622 struct symbol *get_arg_type(struct expression *fn, int arg)
623 623 {
624 624 struct symbol *fn_type;
625 625 struct symbol *tmp;
626 626 struct symbol *arg_type;
627 627 int i;
628 628
629 629 fn_type = get_type(fn);
630 630 if (!fn_type)
631 631 return NULL;
632 632 if (fn_type->type == SYM_PTR)
633 633 fn_type = get_real_base_type(fn_type);
634 634 if (fn_type->type != SYM_FN)
635 635 return NULL;
636 636
637 637 i = 0;
638 638 FOR_EACH_PTR(fn_type->arguments, tmp) {
639 639 arg_type = get_real_base_type(tmp);
640 640 if (i == arg) {
641 641 return arg_type;
642 642 }
643 643 i++;
644 644 } END_FOR_EACH_PTR(tmp);
645 645
646 646 return NULL;
647 647 }
648 648
649 649 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
650 650 {
651 651 struct symbol *tmp, *sub;
652 652 int chunk_len;
653 653
654 654 if (strncmp(name, ".", 1) == 0)
655 655 name += 1;
656 656 else if (strncmp(name, "->", 2) == 0)
657 657 name += 2;
658 658
659 659 FOR_EACH_PTR(symbol_list, tmp) {
660 660 if (!tmp->ident) {
661 661 sub = get_real_base_type(tmp);
662 662 sub = get_member_from_string(sub->symbol_list, name);
663 663 if (sub)
664 664 return sub;
665 665 continue;
666 666 }
667 667
668 668 if (strcmp(tmp->ident->name, name) == 0)
669 669 return tmp;
670 670
671 671 chunk_len = tmp->ident->len;
672 672 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
673 673 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
674 674 sub = get_real_base_type(tmp);
675 675 if (sub->type == SYM_PTR)
676 676 sub = get_real_base_type(sub);
677 677 return get_member_from_string(sub->symbol_list, name + chunk_len);
678 678 }
679 679
680 680 } END_FOR_EACH_PTR(tmp);
681 681
682 682 return NULL;
683 683 }
684 684
685 685 struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
686 686 {
687 687 struct symbol *sym;
688 688
689 689 if (strcmp(key, "$") == 0)
690 690 return get_type(expr);
691 691
692 692 if (strcmp(key, "*$") == 0) {
693 693 sym = get_type(expr);
694 694 if (!sym || sym->type != SYM_PTR)
695 695 return NULL;
696 696 return get_real_base_type(sym);
697 697 }
698 698
699 699 sym = get_type(expr);
700 700 if (!sym)
701 701 return NULL;
702 702 if (sym->type == SYM_PTR)
703 703 sym = get_real_base_type(sym);
704 704
705 705 key = key + 1;
706 706 sym = get_member_from_string(sym->symbol_list, key);
707 707 if (!sym)
708 708 return NULL;
709 709 return get_real_base_type(sym);
710 710 }
711 711
712 712 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key)
713 713 {
714 714 struct symbol *type;
715 715
716 716 if (!key)
717 717 return NULL;
718 718 if (strcmp(key, "$") == 0)
719 719 return get_arg_type(fn, param);
720 720 if (strcmp(key, "*$") == 0) {
721 721 type = get_arg_type(fn, param);
722 722 if (!type || type->type != SYM_PTR)
723 723 return NULL;
724 724 return get_real_base_type(type);
725 725 }
726 726 return get_member_type_from_key(arg, key);
727 727 }
728 728
729 729 int is_struct(struct expression *expr)
730 730 {
731 731 struct symbol *type;
732 732
733 733 type = get_type(expr);
734 734 if (type && type->type == SYM_STRUCT)
735 735 return 1;
736 736 return 0;
737 737 }
738 738
739 739 static struct {
740 740 struct symbol *sym;
741 741 const char *name;
742 742 } base_types[] = {
743 743 {&bool_ctype, "bool"},
744 744 {&void_ctype, "void"},
745 745 {&type_ctype, "type"},
746 746 {&char_ctype, "char"},
747 747 {&schar_ctype, "schar"},
748 748 {&uchar_ctype, "uchar"},
749 749 {&short_ctype, "short"},
750 750 {&sshort_ctype, "sshort"},
751 751 {&ushort_ctype, "ushort"},
752 752 {&int_ctype, "int"},
753 753 {&sint_ctype, "sint"},
754 754 {&uint_ctype, "uint"},
755 755 {&long_ctype, "long"},
756 756 {&slong_ctype, "slong"},
757 757 {&ulong_ctype, "ulong"},
758 758 {&llong_ctype, "llong"},
759 759 {&sllong_ctype, "sllong"},
760 760 {&ullong_ctype, "ullong"},
761 761 {&lllong_ctype, "lllong"},
762 762 {&slllong_ctype, "slllong"},
763 763 {&ulllong_ctype, "ulllong"},
764 764 {&float_ctype, "float"},
765 765 {&double_ctype, "double"},
766 766 {&ldouble_ctype, "ldouble"},
767 767 {&string_ctype, "string"},
768 768 {&ptr_ctype, "ptr"},
769 769 {&lazy_ptr_ctype, "lazy_ptr"},
770 770 {&incomplete_ctype, "incomplete"},
771 771 {&label_ctype, "label"},
772 772 {&bad_ctype, "bad"},
773 773 {&null_ctype, "null"},
774 774 };
775 775
776 776 static const char *base_type_str(struct symbol *sym)
777 777 {
778 778 int i;
779 779
780 780 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
781 781 if (sym == base_types[i].sym)
↓ open down ↓ |
781 lines elided |
↑ open up ↑ |
782 782 return base_types[i].name;
783 783 }
784 784 return "<unknown>";
785 785 }
786 786
787 787 static int type_str_helper(char *buf, int size, struct symbol *type)
788 788 {
789 789 int n;
790 790
791 791 if (!type)
792 - return snprintf(buf, size, "<unknown>");
792 + return snprintf(buf, size, "<null type>");
793 793
794 794 if (type->type == SYM_BASETYPE) {
795 795 return snprintf(buf, size, "%s", base_type_str(type));
796 796 } else if (type->type == SYM_PTR) {
797 797 type = get_real_base_type(type);
798 798 n = type_str_helper(buf, size, type);
799 799 if (n > size)
800 800 return n;
801 801 return n + snprintf(buf + n, size - n, "*");
802 802 } else if (type->type == SYM_ARRAY) {
803 803 type = get_real_base_type(type);
804 804 n = type_str_helper(buf, size, type);
805 805 if (n > size)
806 806 return n;
807 807 return n + snprintf(buf + n, size - n, "[]");
808 808 } else if (type->type == SYM_STRUCT) {
809 809 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
810 810 } else if (type->type == SYM_UNION) {
811 811 if (type->ident)
812 812 return snprintf(buf, size, "union %s", type->ident->name);
813 813 else
814 814 return snprintf(buf, size, "anonymous union");
815 815 } else if (type->type == SYM_FN) {
816 816 struct symbol *arg, *return_type, *arg_type;
817 817 int i;
818 818
819 819 return_type = get_real_base_type(type);
820 820 n = type_str_helper(buf, size, return_type);
821 821 if (n > size)
822 822 return n;
823 823 n += snprintf(buf + n, size - n, "(*)(");
824 824 if (n > size)
825 825 return n;
826 826
827 827 i = 0;
828 828 FOR_EACH_PTR(type->arguments, arg) {
829 829 if (i++)
830 830 n += snprintf(buf + n, size - n, ", ");
831 831 if (n > size)
832 832 return n;
833 833 arg_type = get_real_base_type(arg);
834 834 n += type_str_helper(buf + n, size - n, arg_type);
835 835 if (n > size)
836 836 return n;
837 837 } END_FOR_EACH_PTR(arg);
838 838
839 839 return n + snprintf(buf + n, size - n, ")");
840 840 } else if (type->type == SYM_NODE) {
841 841 n = snprintf(buf, size, "node {");
842 842 if (n > size)
843 843 return n;
844 844 type = get_real_base_type(type);
845 845 n += type_str_helper(buf + n, size - n, type);
846 846 if (n > size)
847 847 return n;
848 848 return n + snprintf(buf + n, size - n, "}");
849 849 } else if (type->type == SYM_ENUM) {
850 850 return snprintf(buf, size, "enum %s", type->ident ? type->ident->name : "<unknown>");
851 851 } else {
852 852 return snprintf(buf, size, "<type %d>", type->type);
853 853 }
854 854 }
855 855
856 856 char *type_to_str(struct symbol *type)
857 857 {
858 858 static char buf[256];
859 859
860 860 buf[0] = '\0';
861 861 type_str_helper(buf, sizeof(buf), type);
862 862 return buf;
863 863 }
↓ open down ↓ |
61 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX