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