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