1 /* Subroutines common to both C and C++ pretty-printers. 2 Copyright (C) 2002-2018 Free Software Foundation, Inc. 3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify it under 8 the terms of the GNU General Public License as published by the Free 9 Software Foundation; either version 3, or (at your option) any later 10 version. 11 12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "c-pretty-print.h" 25 #include "diagnostic.h" 26 #include "stor-layout.h" 27 #include "stringpool.h" 28 #include "attribs.h" 29 #include "intl.h" 30 #include "tree-pretty-print.h" 31 #include "selftest.h" 32 33 /* The pretty-printer code is primarily designed to closely follow 34 (GNU) C and C++ grammars. That is to be contrasted with spaghetti 35 codes we used to have in the past. Following a structured 36 approach (preferably the official grammars) is believed to make it 37 much easier to add extensions and nifty pretty-printing effects that 38 takes expression or declaration contexts into account. */ 39 40 41 #define pp_c_maybe_whitespace(PP) \ 42 do { \ 43 if ((PP)->padding == pp_before) \ 44 pp_c_whitespace (PP); \ 45 } while (0) 46 47 /* literal */ 48 static void pp_c_char (c_pretty_printer *, int); 49 50 /* postfix-expression */ 51 static void pp_c_initializer_list (c_pretty_printer *, tree); 52 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree); 53 54 static void pp_c_additive_expression (c_pretty_printer *, tree); 55 static void pp_c_shift_expression (c_pretty_printer *, tree); 56 static void pp_c_relational_expression (c_pretty_printer *, tree); 57 static void pp_c_equality_expression (c_pretty_printer *, tree); 58 static void pp_c_and_expression (c_pretty_printer *, tree); 59 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree); 60 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree); 61 static void pp_c_logical_and_expression (c_pretty_printer *, tree); 62 63 /* declarations. */ 64 65 66 /* Helper functions. */ 67 68 void 69 pp_c_whitespace (c_pretty_printer *pp) 70 { 71 pp_space (pp); 72 pp->padding = pp_none; 73 } 74 75 void 76 pp_c_left_paren (c_pretty_printer *pp) 77 { 78 pp_left_paren (pp); 79 pp->padding = pp_none; 80 } 81 82 void 83 pp_c_right_paren (c_pretty_printer *pp) 84 { 85 pp_right_paren (pp); 86 pp->padding = pp_none; 87 } 88 89 void 90 pp_c_left_brace (c_pretty_printer *pp) 91 { 92 pp_left_brace (pp); 93 pp->padding = pp_none; 94 } 95 96 void 97 pp_c_right_brace (c_pretty_printer *pp) 98 { 99 pp_right_brace (pp); 100 pp->padding = pp_none; 101 } 102 103 void 104 pp_c_left_bracket (c_pretty_printer *pp) 105 { 106 pp_left_bracket (pp); 107 pp->padding = pp_none; 108 } 109 110 void 111 pp_c_right_bracket (c_pretty_printer *pp) 112 { 113 pp_right_bracket (pp); 114 pp->padding = pp_none; 115 } 116 117 void 118 pp_c_dot (c_pretty_printer *pp) 119 { 120 pp_dot (pp); 121 pp->padding = pp_none; 122 } 123 124 void 125 pp_c_ampersand (c_pretty_printer *pp) 126 { 127 pp_ampersand (pp); 128 pp->padding = pp_none; 129 } 130 131 void 132 pp_c_star (c_pretty_printer *pp) 133 { 134 pp_star (pp); 135 pp->padding = pp_none; 136 } 137 138 void 139 pp_c_arrow (c_pretty_printer *pp) 140 { 141 pp_arrow (pp); 142 pp->padding = pp_none; 143 } 144 145 void 146 pp_c_semicolon (c_pretty_printer *pp) 147 { 148 pp_semicolon (pp); 149 pp->padding = pp_none; 150 } 151 152 void 153 pp_c_complement (c_pretty_printer *pp) 154 { 155 pp_complement (pp); 156 pp->padding = pp_none; 157 } 158 159 void 160 pp_c_exclamation (c_pretty_printer *pp) 161 { 162 pp_exclamation (pp); 163 pp->padding = pp_none; 164 } 165 166 /* Print out the external representation of QUALIFIERS. */ 167 168 void 169 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type) 170 { 171 const char *p = pp_last_position_in_text (pp); 172 173 if (!qualifiers) 174 return; 175 176 /* The C programming language does not have references, but it is much 177 simpler to handle those here rather than going through the same 178 logic in the C++ pretty-printer. */ 179 if (p != NULL && (*p == '*' || *p == '&')) 180 pp_c_whitespace (pp); 181 182 if (qualifiers & TYPE_QUAL_ATOMIC) 183 pp_c_ws_string (pp, "_Atomic"); 184 if (qualifiers & TYPE_QUAL_CONST) 185 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const"); 186 if (qualifiers & TYPE_QUAL_VOLATILE) 187 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile"); 188 if (qualifiers & TYPE_QUAL_RESTRICT) 189 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx () 190 ? "restrict" : "__restrict__")); 191 } 192 193 /* Pretty-print T using the type-cast notation '( type-name )'. */ 194 195 static void 196 pp_c_type_cast (c_pretty_printer *pp, tree t) 197 { 198 pp_c_left_paren (pp); 199 pp->type_id (t); 200 pp_c_right_paren (pp); 201 } 202 203 /* We're about to pretty-print a pointer type as indicated by T. 204 Output a whitespace, if needed, preparing for subsequent output. */ 205 206 void 207 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t) 208 { 209 if (POINTER_TYPE_P (t)) 210 { 211 tree pointee = strip_pointer_operator (TREE_TYPE (t)); 212 if (TREE_CODE (pointee) != ARRAY_TYPE 213 && TREE_CODE (pointee) != FUNCTION_TYPE) 214 pp_c_whitespace (pp); 215 } 216 } 217 218 219 /* Declarations. */ 220 221 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the 222 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier 223 of its type. Take care of possible extensions. 224 225 type-qualifier-list: 226 type-qualifier 227 type-qualifier-list type-qualifier 228 229 type-qualifier: 230 const 231 restrict -- C99 232 __restrict__ -- GNU C 233 address-space-qualifier -- GNU C 234 volatile 235 _Atomic -- C11 236 237 address-space-qualifier: 238 identifier -- GNU C */ 239 240 void 241 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t) 242 { 243 int qualifiers; 244 245 if (!t || t == error_mark_node) 246 return; 247 248 if (!TYPE_P (t)) 249 t = TREE_TYPE (t); 250 251 qualifiers = TYPE_QUALS (t); 252 pp_c_cv_qualifiers (pp, qualifiers, 253 TREE_CODE (t) == FUNCTION_TYPE); 254 255 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t))) 256 { 257 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t)); 258 pp_c_identifier (pp, as); 259 } 260 } 261 262 /* pointer: 263 * type-qualifier-list(opt) 264 * type-qualifier-list(opt) pointer */ 265 266 static void 267 pp_c_pointer (c_pretty_printer *pp, tree t) 268 { 269 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL) 270 t = TREE_TYPE (t); 271 switch (TREE_CODE (t)) 272 { 273 case POINTER_TYPE: 274 /* It is easier to handle C++ reference types here. */ 275 case REFERENCE_TYPE: 276 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE) 277 pp_c_pointer (pp, TREE_TYPE (t)); 278 if (TREE_CODE (t) == POINTER_TYPE) 279 pp_c_star (pp); 280 else 281 pp_c_ampersand (pp); 282 pp_c_type_qualifier_list (pp, t); 283 break; 284 285 /* ??? This node is now in GENERIC and so shouldn't be here. But 286 we'll fix that later. */ 287 case DECL_EXPR: 288 pp->declaration (DECL_EXPR_DECL (t)); 289 pp_needs_newline (pp) = true; 290 break; 291 292 default: 293 pp_unsupported_tree (pp, t); 294 } 295 } 296 297 /* simple-type-specifier: 298 type-specifier 299 300 type-specifier: 301 void 302 char 303 short 304 int 305 long 306 float 307 double 308 signed 309 unsigned 310 _Bool -- C99 311 _Complex -- C99 312 _Imaginary -- C99 313 struct-or-union-specifier 314 enum-specifier 315 typedef-name. 316 317 GNU extensions. 318 simple-type-specifier: 319 __complex__ 320 __vector__ */ 321 322 void 323 c_pretty_printer::simple_type_specifier (tree t) 324 { 325 const enum tree_code code = TREE_CODE (t); 326 switch (code) 327 { 328 case ERROR_MARK: 329 translate_string ("<type-error>"); 330 break; 331 332 case IDENTIFIER_NODE: 333 pp_c_identifier (this, IDENTIFIER_POINTER (t)); 334 break; 335 336 case VOID_TYPE: 337 case BOOLEAN_TYPE: 338 case INTEGER_TYPE: 339 case REAL_TYPE: 340 case FIXED_POINT_TYPE: 341 if (TYPE_NAME (t)) 342 { 343 t = TYPE_NAME (t); 344 simple_type_specifier (t); 345 } 346 else 347 { 348 int prec = TYPE_PRECISION (t); 349 tree common_t; 350 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t))) 351 common_t = c_common_type_for_mode (TYPE_MODE (t), 352 TYPE_SATURATING (t)); 353 else 354 common_t = c_common_type_for_mode (TYPE_MODE (t), 355 TYPE_UNSIGNED (t)); 356 if (common_t && TYPE_NAME (common_t)) 357 { 358 simple_type_specifier (common_t); 359 if (TYPE_PRECISION (common_t) != prec) 360 { 361 pp_colon (this); 362 pp_decimal_int (this, prec); 363 } 364 } 365 else 366 { 367 switch (code) 368 { 369 case INTEGER_TYPE: 370 translate_string (TYPE_UNSIGNED (t) 371 ? "<unnamed-unsigned:" 372 : "<unnamed-signed:"); 373 break; 374 case REAL_TYPE: 375 translate_string ("<unnamed-float:"); 376 break; 377 case FIXED_POINT_TYPE: 378 translate_string ("<unnamed-fixed:"); 379 break; 380 default: 381 gcc_unreachable (); 382 } 383 pp_decimal_int (this, prec); 384 pp_greater (this); 385 } 386 } 387 break; 388 389 case TYPE_DECL: 390 if (DECL_NAME (t)) 391 id_expression (t); 392 else 393 translate_string ("<typedef-error>"); 394 break; 395 396 case UNION_TYPE: 397 case RECORD_TYPE: 398 case ENUMERAL_TYPE: 399 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL) 400 /* Don't decorate the type if this is a typedef name. */; 401 else if (code == UNION_TYPE) 402 pp_c_ws_string (this, "union"); 403 else if (code == RECORD_TYPE) 404 pp_c_ws_string (this, "struct"); 405 else if (code == ENUMERAL_TYPE) 406 pp_c_ws_string (this, "enum"); 407 else 408 translate_string ("<tag-error>"); 409 410 if (TYPE_NAME (t)) 411 id_expression (TYPE_NAME (t)); 412 else 413 translate_string ("<anonymous>"); 414 break; 415 416 default: 417 pp_unsupported_tree (this, t); 418 break; 419 } 420 } 421 422 /* specifier-qualifier-list: 423 type-specifier specifier-qualifier-list-opt 424 type-qualifier specifier-qualifier-list-opt 425 426 427 Implementation note: Because of the non-linearities in array or 428 function declarations, this routine prints not just the 429 specifier-qualifier-list of such entities or types of such entities, 430 but also the 'pointer' production part of their declarators. The 431 remaining part is done by declarator() or abstract_declarator(). */ 432 433 void 434 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t) 435 { 436 const enum tree_code code = TREE_CODE (t); 437 438 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE) 439 pp_c_type_qualifier_list (pp, t); 440 switch (code) 441 { 442 case REFERENCE_TYPE: 443 case POINTER_TYPE: 444 { 445 /* Get the types-specifier of this type. */ 446 tree pointee = strip_pointer_operator (TREE_TYPE (t)); 447 pp_c_specifier_qualifier_list (pp, pointee); 448 if (TREE_CODE (pointee) == ARRAY_TYPE 449 || TREE_CODE (pointee) == FUNCTION_TYPE) 450 { 451 pp_c_whitespace (pp); 452 pp_c_left_paren (pp); 453 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee)); 454 } 455 else if (!c_dialect_cxx ()) 456 pp_c_whitespace (pp); 457 pp_ptr_operator (pp, t); 458 } 459 break; 460 461 case FUNCTION_TYPE: 462 case ARRAY_TYPE: 463 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t)); 464 break; 465 466 case VECTOR_TYPE: 467 case COMPLEX_TYPE: 468 if (code == COMPLEX_TYPE) 469 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx () 470 ? "_Complex" : "__complex__")); 471 else if (code == VECTOR_TYPE) 472 { 473 pp_c_ws_string (pp, "__vector"); 474 pp_c_left_paren (pp); 475 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t)); 476 pp_c_right_paren (pp); 477 pp_c_whitespace (pp); 478 } 479 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t)); 480 break; 481 482 default: 483 pp->simple_type_specifier (t); 484 break; 485 } 486 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE) 487 pp_c_type_qualifier_list (pp, t); 488 } 489 490 /* parameter-type-list: 491 parameter-list 492 parameter-list , ... 493 494 parameter-list: 495 parameter-declaration 496 parameter-list , parameter-declaration 497 498 parameter-declaration: 499 declaration-specifiers declarator 500 declaration-specifiers abstract-declarator(opt) */ 501 502 void 503 pp_c_parameter_type_list (c_pretty_printer *pp, tree t) 504 { 505 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract); 506 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t); 507 pp_c_left_paren (pp); 508 if (parms == void_list_node) 509 pp_c_ws_string (pp, "void"); 510 else 511 { 512 bool first = true; 513 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms)) 514 { 515 if (!first) 516 pp_separate_with (pp, ','); 517 first = false; 518 pp->declaration_specifiers 519 (want_parm_decl ? parms : TREE_VALUE (parms)); 520 if (want_parm_decl) 521 pp->declarator (parms); 522 else 523 pp->abstract_declarator (TREE_VALUE (parms)); 524 } 525 if (!first && !parms) 526 { 527 pp_separate_with (pp, ','); 528 pp_c_ws_string (pp, "..."); 529 } 530 } 531 pp_c_right_paren (pp); 532 } 533 534 /* abstract-declarator: 535 pointer 536 pointer(opt) direct-abstract-declarator */ 537 538 void 539 c_pretty_printer::abstract_declarator (tree t) 540 { 541 if (TREE_CODE (t) == POINTER_TYPE) 542 { 543 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE 544 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) 545 pp_c_right_paren (this); 546 t = TREE_TYPE (t); 547 } 548 549 direct_abstract_declarator (t); 550 } 551 552 /* direct-abstract-declarator: 553 ( abstract-declarator ) 554 direct-abstract-declarator(opt) [ assignment-expression(opt) ] 555 direct-abstract-declarator(opt) [ * ] 556 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */ 557 558 void 559 c_pretty_printer::direct_abstract_declarator (tree t) 560 { 561 switch (TREE_CODE (t)) 562 { 563 case POINTER_TYPE: 564 abstract_declarator (t); 565 break; 566 567 case FUNCTION_TYPE: 568 pp_c_parameter_type_list (this, t); 569 direct_abstract_declarator (TREE_TYPE (t)); 570 break; 571 572 case ARRAY_TYPE: 573 pp_c_left_bracket (this); 574 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t))) 575 { 576 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t)); 577 tree type = TREE_TYPE (maxval); 578 579 if (tree_fits_shwi_p (maxval)) 580 pp_wide_integer (this, tree_to_shwi (maxval) + 1); 581 else 582 expression (fold_build2 (PLUS_EXPR, type, maxval, 583 build_int_cst (type, 1))); 584 } 585 pp_c_right_bracket (this); 586 direct_abstract_declarator (TREE_TYPE (t)); 587 break; 588 589 case IDENTIFIER_NODE: 590 case VOID_TYPE: 591 case BOOLEAN_TYPE: 592 case INTEGER_TYPE: 593 case REAL_TYPE: 594 case FIXED_POINT_TYPE: 595 case ENUMERAL_TYPE: 596 case RECORD_TYPE: 597 case UNION_TYPE: 598 case VECTOR_TYPE: 599 case COMPLEX_TYPE: 600 case TYPE_DECL: 601 break; 602 603 default: 604 pp_unsupported_tree (this, t); 605 break; 606 } 607 } 608 609 /* type-name: 610 specifier-qualifier-list abstract-declarator(opt) */ 611 612 void 613 c_pretty_printer::type_id (tree t) 614 { 615 pp_c_specifier_qualifier_list (this, t); 616 abstract_declarator (t); 617 } 618 619 /* storage-class-specifier: 620 typedef 621 extern 622 static 623 auto 624 register */ 625 626 void 627 c_pretty_printer::storage_class_specifier (tree t) 628 { 629 if (TREE_CODE (t) == TYPE_DECL) 630 pp_c_ws_string (this, "typedef"); 631 else if (DECL_P (t)) 632 { 633 if (DECL_REGISTER (t)) 634 pp_c_ws_string (this, "register"); 635 else if (TREE_STATIC (t) && VAR_P (t)) 636 pp_c_ws_string (this, "static"); 637 } 638 } 639 640 /* function-specifier: 641 inline */ 642 643 void 644 c_pretty_printer::function_specifier (tree t) 645 { 646 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t)) 647 pp_c_ws_string (this, "inline"); 648 } 649 650 /* declaration-specifiers: 651 storage-class-specifier declaration-specifiers(opt) 652 type-specifier declaration-specifiers(opt) 653 type-qualifier declaration-specifiers(opt) 654 function-specifier declaration-specifiers(opt) */ 655 656 void 657 c_pretty_printer::declaration_specifiers (tree t) 658 { 659 storage_class_specifier (t); 660 function_specifier (t); 661 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t); 662 } 663 664 /* direct-declarator 665 identifier 666 ( declarator ) 667 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ] 668 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)] 669 direct-declarator [ type-qualifier-list static assignment-expression ] 670 direct-declarator [ type-qualifier-list * ] 671 direct-declarator ( parameter-type-list ) 672 direct-declarator ( identifier-list(opt) ) */ 673 674 void 675 c_pretty_printer::direct_declarator (tree t) 676 { 677 switch (TREE_CODE (t)) 678 { 679 case VAR_DECL: 680 case PARM_DECL: 681 case TYPE_DECL: 682 case FIELD_DECL: 683 case LABEL_DECL: 684 pp_c_space_for_pointer_operator (this, TREE_TYPE (t)); 685 pp_c_tree_decl_identifier (this, t); 686 break; 687 688 case ARRAY_TYPE: 689 case POINTER_TYPE: 690 abstract_declarator (TREE_TYPE (t)); 691 break; 692 693 case FUNCTION_TYPE: 694 pp_parameter_list (this, t); 695 abstract_declarator (TREE_TYPE (t)); 696 break; 697 698 case FUNCTION_DECL: 699 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t))); 700 pp_c_tree_decl_identifier (this, t); 701 if (flags & pp_c_flag_abstract) 702 abstract_declarator (TREE_TYPE (t)); 703 else 704 { 705 pp_parameter_list (this, t); 706 abstract_declarator (TREE_TYPE (TREE_TYPE (t))); 707 } 708 break; 709 710 case INTEGER_TYPE: 711 case REAL_TYPE: 712 case FIXED_POINT_TYPE: 713 case ENUMERAL_TYPE: 714 case UNION_TYPE: 715 case RECORD_TYPE: 716 break; 717 718 default: 719 pp_unsupported_tree (this, t); 720 break; 721 } 722 } 723 724 725 /* declarator: 726 pointer(opt) direct-declarator */ 727 728 void 729 c_pretty_printer::declarator (tree t) 730 { 731 switch (TREE_CODE (t)) 732 { 733 case INTEGER_TYPE: 734 case REAL_TYPE: 735 case FIXED_POINT_TYPE: 736 case ENUMERAL_TYPE: 737 case UNION_TYPE: 738 case RECORD_TYPE: 739 break; 740 741 case VAR_DECL: 742 case PARM_DECL: 743 case FIELD_DECL: 744 case ARRAY_TYPE: 745 case FUNCTION_TYPE: 746 case FUNCTION_DECL: 747 case TYPE_DECL: 748 direct_declarator (t); 749 break; 750 751 752 default: 753 pp_unsupported_tree (this, t); 754 break; 755 } 756 } 757 758 /* declaration: 759 declaration-specifiers init-declarator-list(opt) ; */ 760 761 void 762 c_pretty_printer::declaration (tree t) 763 { 764 declaration_specifiers (t); 765 pp_c_init_declarator (this, t); 766 } 767 768 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */ 769 770 void 771 pp_c_attributes (c_pretty_printer *pp, tree attributes) 772 { 773 if (attributes == NULL_TREE) 774 return; 775 776 pp_c_ws_string (pp, "__attribute__"); 777 pp_c_left_paren (pp); 778 pp_c_left_paren (pp); 779 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes)) 780 { 781 pp_tree_identifier (pp, TREE_PURPOSE (attributes)); 782 if (TREE_VALUE (attributes)) 783 pp_c_call_argument_list (pp, TREE_VALUE (attributes)); 784 785 if (TREE_CHAIN (attributes)) 786 pp_separate_with (pp, ','); 787 } 788 pp_c_right_paren (pp); 789 pp_c_right_paren (pp); 790 } 791 792 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes 793 marked to be displayed on disgnostic. */ 794 795 void 796 pp_c_attributes_display (c_pretty_printer *pp, tree a) 797 { 798 bool is_first = true; 799 800 if (a == NULL_TREE) 801 return; 802 803 for (; a != NULL_TREE; a = TREE_CHAIN (a)) 804 { 805 const struct attribute_spec *as; 806 as = lookup_attribute_spec (TREE_PURPOSE (a)); 807 if (!as || as->affects_type_identity == false) 808 continue; 809 if (c_dialect_cxx () 810 && !strcmp ("transaction_safe", as->name)) 811 /* In C++ transaction_safe is printed at the end of the declarator. */ 812 continue; 813 if (is_first) 814 { 815 pp_c_ws_string (pp, "__attribute__"); 816 pp_c_left_paren (pp); 817 pp_c_left_paren (pp); 818 is_first = false; 819 } 820 else 821 { 822 pp_separate_with (pp, ','); 823 } 824 pp_tree_identifier (pp, TREE_PURPOSE (a)); 825 if (TREE_VALUE (a)) 826 pp_c_call_argument_list (pp, TREE_VALUE (a)); 827 } 828 829 if (!is_first) 830 { 831 pp_c_right_paren (pp); 832 pp_c_right_paren (pp); 833 pp_c_whitespace (pp); 834 } 835 } 836 837 /* function-definition: 838 declaration-specifiers declarator compound-statement */ 839 840 void 841 pp_c_function_definition (c_pretty_printer *pp, tree t) 842 { 843 pp->declaration_specifiers (t); 844 pp->declarator (t); 845 pp_needs_newline (pp) = true; 846 pp->statement (DECL_SAVED_TREE (t)); 847 pp_newline_and_flush (pp); 848 } 849 850 851 /* Expressions. */ 852 853 /* Print out a c-char. This is called solely for characters which are 854 in the *target* execution character set. We ought to convert them 855 back to the *host* execution character set before printing, but we 856 have no way to do this at present. A decent compromise is to print 857 all characters as if they were in the host execution character set, 858 and not attempt to recover any named escape characters, but render 859 all unprintables as octal escapes. If the host and target character 860 sets are the same, this produces relatively readable output. If they 861 are not the same, strings may appear as gibberish, but that's okay 862 (in fact, it may well be what the reader wants, e.g. if they are looking 863 to see if conversion to the target character set happened correctly). 864 865 A special case: we need to prefix \, ", and ' with backslashes. It is 866 correct to do so for the *host*'s \, ", and ', because the rest of the 867 file appears in the host character set. */ 868 869 static void 870 pp_c_char (c_pretty_printer *pp, int c) 871 { 872 if (ISPRINT (c)) 873 { 874 switch (c) 875 { 876 case '\\': pp_string (pp, "\\\\"); break; 877 case '\'': pp_string (pp, "\\\'"); break; 878 case '\"': pp_string (pp, "\\\""); break; 879 default: pp_character (pp, c); 880 } 881 } 882 else 883 pp_scalar (pp, "\\%03o", (unsigned) c); 884 } 885 886 /* Print out a STRING literal. */ 887 888 void 889 pp_c_string_literal (c_pretty_printer *pp, tree s) 890 { 891 const char *p = TREE_STRING_POINTER (s); 892 int n = TREE_STRING_LENGTH (s) - 1; 893 int i; 894 pp_doublequote (pp); 895 for (i = 0; i < n; ++i) 896 pp_c_char (pp, p[i]); 897 pp_doublequote (pp); 898 } 899 900 /* Pretty-print a VOID_CST (void_node). */ 901 902 static void 903 pp_c_void_constant (c_pretty_printer *pp) 904 { 905 pp_c_type_cast (pp, void_type_node); 906 pp_string (pp, "0"); 907 } 908 909 /* Pretty-print an INTEGER literal. */ 910 911 static void 912 pp_c_integer_constant (c_pretty_printer *pp, tree i) 913 { 914 if (tree_fits_shwi_p (i)) 915 pp_wide_integer (pp, tree_to_shwi (i)); 916 else if (tree_fits_uhwi_p (i)) 917 pp_unsigned_wide_integer (pp, tree_to_uhwi (i)); 918 else 919 { 920 wide_int wi = wi::to_wide (i); 921 922 if (wi::lt_p (wi::to_wide (i), 0, TYPE_SIGN (TREE_TYPE (i)))) 923 { 924 pp_minus (pp); 925 wi = -wi; 926 } 927 print_hex (wi, pp_buffer (pp)->digit_buffer); 928 pp_string (pp, pp_buffer (pp)->digit_buffer); 929 } 930 } 931 932 /* Print out a CHARACTER literal. */ 933 934 static void 935 pp_c_character_constant (c_pretty_printer *pp, tree c) 936 { 937 pp_quote (pp); 938 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c)); 939 pp_quote (pp); 940 } 941 942 /* Print out a BOOLEAN literal. */ 943 944 static void 945 pp_c_bool_constant (c_pretty_printer *pp, tree b) 946 { 947 if (b == boolean_false_node) 948 { 949 if (c_dialect_cxx ()) 950 pp_c_ws_string (pp, "false"); 951 else if (flag_isoc99) 952 pp_c_ws_string (pp, "_False"); 953 else 954 pp_unsupported_tree (pp, b); 955 } 956 else if (b == boolean_true_node) 957 { 958 if (c_dialect_cxx ()) 959 pp_c_ws_string (pp, "true"); 960 else if (flag_isoc99) 961 pp_c_ws_string (pp, "_True"); 962 else 963 pp_unsupported_tree (pp, b); 964 } 965 else if (TREE_CODE (b) == INTEGER_CST) 966 pp_c_integer_constant (pp, b); 967 else 968 pp_unsupported_tree (pp, b); 969 } 970 971 /* Attempt to print out an ENUMERATOR. Return true on success. Else return 972 false; that means the value was obtained by a cast, in which case 973 print out the type-id part of the cast-expression -- the casted value 974 is then printed by pp_c_integer_literal. */ 975 976 static bool 977 pp_c_enumeration_constant (c_pretty_printer *pp, tree e) 978 { 979 bool value_is_named = true; 980 tree type = TREE_TYPE (e); 981 tree value; 982 983 /* Find the name of this constant. */ 984 for (value = TYPE_VALUES (type); 985 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e); 986 value = TREE_CHAIN (value)) 987 ; 988 989 if (value != NULL_TREE) 990 pp->id_expression (TREE_PURPOSE (value)); 991 else 992 { 993 /* Value must have been cast. */ 994 pp_c_type_cast (pp, type); 995 value_is_named = false; 996 } 997 998 return value_is_named; 999 } 1000 1001 /* Print out a REAL value as a decimal-floating-constant. */ 1002 1003 static void 1004 pp_c_floating_constant (c_pretty_printer *pp, tree r) 1005 { 1006 const struct real_format *fmt 1007 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r))); 1008 1009 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r); 1010 bool is_decimal = floating_cst.decimal; 1011 1012 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates 1013 log10(2) to 7 significant digits. */ 1014 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136); 1015 1016 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r), 1017 sizeof (pp_buffer (pp)->digit_buffer), 1018 max_digits10, 1); 1019 1020 pp_string (pp, pp_buffer(pp)->digit_buffer); 1021 if (TREE_TYPE (r) == float_type_node) 1022 pp_character (pp, 'f'); 1023 else if (TREE_TYPE (r) == long_double_type_node) 1024 pp_character (pp, 'l'); 1025 else if (TREE_TYPE (r) == dfloat128_type_node) 1026 pp_string (pp, "dl"); 1027 else if (TREE_TYPE (r) == dfloat64_type_node) 1028 pp_string (pp, "dd"); 1029 else if (TREE_TYPE (r) == dfloat32_type_node) 1030 pp_string (pp, "df"); 1031 else if (TREE_TYPE (r) != double_type_node) 1032 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++) 1033 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i)) 1034 { 1035 pp_character (pp, 'f'); 1036 pp_decimal_int (pp, floatn_nx_types[i].n); 1037 if (floatn_nx_types[i].extended) 1038 pp_character (pp, 'x'); 1039 break; 1040 } 1041 } 1042 1043 /* Print out a FIXED value as a decimal-floating-constant. */ 1044 1045 static void 1046 pp_c_fixed_constant (c_pretty_printer *pp, tree r) 1047 { 1048 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r), 1049 sizeof (pp_buffer (pp)->digit_buffer)); 1050 pp_string (pp, pp_buffer(pp)->digit_buffer); 1051 } 1052 1053 /* Pretty-print a compound literal expression. GNU extensions include 1054 vector constants. */ 1055 1056 static void 1057 pp_c_compound_literal (c_pretty_printer *pp, tree e) 1058 { 1059 tree type = TREE_TYPE (e); 1060 pp_c_type_cast (pp, type); 1061 1062 switch (TREE_CODE (type)) 1063 { 1064 case RECORD_TYPE: 1065 case UNION_TYPE: 1066 case ARRAY_TYPE: 1067 case VECTOR_TYPE: 1068 case COMPLEX_TYPE: 1069 pp_c_brace_enclosed_initializer_list (pp, e); 1070 break; 1071 1072 default: 1073 pp_unsupported_tree (pp, e); 1074 break; 1075 } 1076 } 1077 1078 /* Pretty-print a COMPLEX_EXPR expression. */ 1079 1080 static void 1081 pp_c_complex_expr (c_pretty_printer *pp, tree e) 1082 { 1083 /* Handle a few common special cases, otherwise fallback 1084 to printing it as compound literal. */ 1085 tree type = TREE_TYPE (e); 1086 tree realexpr = TREE_OPERAND (e, 0); 1087 tree imagexpr = TREE_OPERAND (e, 1); 1088 1089 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */ 1090 if (TREE_CODE (realexpr) == NOP_EXPR 1091 && TREE_CODE (imagexpr) == NOP_EXPR 1092 && TREE_TYPE (realexpr) == TREE_TYPE (type) 1093 && TREE_TYPE (imagexpr) == TREE_TYPE (type) 1094 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR 1095 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR 1096 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0) 1097 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0)) 1098 { 1099 pp_c_type_cast (pp, type); 1100 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)); 1101 return; 1102 } 1103 1104 /* Cast of an scalar expression to COMPLEX_TYPE. */ 1105 if ((integer_zerop (imagexpr) || real_zerop (imagexpr)) 1106 && TREE_TYPE (realexpr) == TREE_TYPE (type)) 1107 { 1108 pp_c_type_cast (pp, type); 1109 if (TREE_CODE (realexpr) == NOP_EXPR) 1110 realexpr = TREE_OPERAND (realexpr, 0); 1111 pp->expression (realexpr); 1112 return; 1113 } 1114 1115 pp_c_compound_literal (pp, e); 1116 } 1117 1118 /* constant: 1119 integer-constant 1120 floating-constant 1121 fixed-point-constant 1122 enumeration-constant 1123 character-constant */ 1124 1125 void 1126 c_pretty_printer::constant (tree e) 1127 { 1128 const enum tree_code code = TREE_CODE (e); 1129 1130 switch (code) 1131 { 1132 case VOID_CST: 1133 pp_c_void_constant (this); 1134 break; 1135 1136 case INTEGER_CST: 1137 { 1138 tree type = TREE_TYPE (e); 1139 if (type == boolean_type_node) 1140 pp_c_bool_constant (this, e); 1141 else if (type == char_type_node) 1142 pp_c_character_constant (this, e); 1143 else if (TREE_CODE (type) == ENUMERAL_TYPE 1144 && pp_c_enumeration_constant (this, e)) 1145 ; 1146 else 1147 pp_c_integer_constant (this, e); 1148 } 1149 break; 1150 1151 case REAL_CST: 1152 pp_c_floating_constant (this, e); 1153 break; 1154 1155 case FIXED_CST: 1156 pp_c_fixed_constant (this, e); 1157 break; 1158 1159 case STRING_CST: 1160 pp_c_string_literal (this, e); 1161 break; 1162 1163 case COMPLEX_CST: 1164 /* Sometimes, we are confused and we think a complex literal 1165 is a constant. Such thing is a compound literal which 1166 grammatically belongs to postfix-expr production. */ 1167 pp_c_compound_literal (this, e); 1168 break; 1169 1170 default: 1171 pp_unsupported_tree (this, e); 1172 break; 1173 } 1174 } 1175 1176 /* Pretty-print a string such as an identifier, without changing its 1177 encoding, preceded by whitespace is necessary. */ 1178 1179 void 1180 pp_c_ws_string (c_pretty_printer *pp, const char *str) 1181 { 1182 pp_c_maybe_whitespace (pp); 1183 pp_string (pp, str); 1184 pp->padding = pp_before; 1185 } 1186 1187 void 1188 c_pretty_printer::translate_string (const char *gmsgid) 1189 { 1190 if (pp_translate_identifiers (this)) 1191 pp_c_ws_string (this, _(gmsgid)); 1192 else 1193 pp_c_ws_string (this, gmsgid); 1194 } 1195 1196 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences 1197 that need converting to the locale encoding, preceded by whitespace 1198 is necessary. */ 1199 1200 void 1201 pp_c_identifier (c_pretty_printer *pp, const char *id) 1202 { 1203 pp_c_maybe_whitespace (pp); 1204 pp_identifier (pp, id); 1205 pp->padding = pp_before; 1206 } 1207 1208 /* Pretty-print a C primary-expression. 1209 primary-expression: 1210 identifier 1211 constant 1212 string-literal 1213 ( expression ) */ 1214 1215 void 1216 c_pretty_printer::primary_expression (tree e) 1217 { 1218 switch (TREE_CODE (e)) 1219 { 1220 case VAR_DECL: 1221 case PARM_DECL: 1222 case FIELD_DECL: 1223 case CONST_DECL: 1224 case FUNCTION_DECL: 1225 case LABEL_DECL: 1226 pp_c_tree_decl_identifier (this, e); 1227 break; 1228 1229 case IDENTIFIER_NODE: 1230 pp_c_tree_identifier (this, e); 1231 break; 1232 1233 case ERROR_MARK: 1234 translate_string ("<erroneous-expression>"); 1235 break; 1236 1237 case RESULT_DECL: 1238 translate_string ("<return-value>"); 1239 break; 1240 1241 case VOID_CST: 1242 case INTEGER_CST: 1243 case REAL_CST: 1244 case FIXED_CST: 1245 case STRING_CST: 1246 constant (e); 1247 break; 1248 1249 case TARGET_EXPR: 1250 pp_c_ws_string (this, "__builtin_memcpy"); 1251 pp_c_left_paren (this); 1252 pp_ampersand (this); 1253 primary_expression (TREE_OPERAND (e, 0)); 1254 pp_separate_with (this, ','); 1255 pp_ampersand (this); 1256 initializer (TREE_OPERAND (e, 1)); 1257 if (TREE_OPERAND (e, 2)) 1258 { 1259 pp_separate_with (this, ','); 1260 expression (TREE_OPERAND (e, 2)); 1261 } 1262 pp_c_right_paren (this); 1263 break; 1264 1265 default: 1266 /* FIXME: Make sure we won't get into an infinite loop. */ 1267 pp_c_left_paren (this); 1268 expression (e); 1269 pp_c_right_paren (this); 1270 break; 1271 } 1272 } 1273 1274 /* Print out a C initializer -- also support C compound-literals. 1275 initializer: 1276 assignment-expression: 1277 { initializer-list } 1278 { initializer-list , } */ 1279 1280 void 1281 c_pretty_printer::initializer (tree e) 1282 { 1283 if (TREE_CODE (e) == CONSTRUCTOR) 1284 pp_c_brace_enclosed_initializer_list (this, e); 1285 else 1286 expression (e); 1287 } 1288 1289 /* init-declarator: 1290 declarator: 1291 declarator = initializer */ 1292 1293 void 1294 pp_c_init_declarator (c_pretty_printer *pp, tree t) 1295 { 1296 pp->declarator (t); 1297 /* We don't want to output function definitions here. There are handled 1298 elsewhere (and the syntactic form is bogus anyway). */ 1299 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t)) 1300 { 1301 tree init = DECL_INITIAL (t); 1302 /* This C++ bit is handled here because it is easier to do so. 1303 In templates, the C++ parser builds a TREE_LIST for a 1304 direct-initialization; the TREE_PURPOSE is the variable to 1305 initialize and the TREE_VALUE is the initializer. */ 1306 if (TREE_CODE (init) == TREE_LIST) 1307 { 1308 pp_c_left_paren (pp); 1309 pp->expression (TREE_VALUE (init)); 1310 pp_right_paren (pp); 1311 } 1312 else 1313 { 1314 pp_space (pp); 1315 pp_equal (pp); 1316 pp_space (pp); 1317 pp->initializer (init); 1318 } 1319 } 1320 } 1321 1322 /* initializer-list: 1323 designation(opt) initializer 1324 initializer-list , designation(opt) initializer 1325 1326 designation: 1327 designator-list = 1328 1329 designator-list: 1330 designator 1331 designator-list designator 1332 1333 designator: 1334 [ constant-expression ] 1335 identifier */ 1336 1337 static void 1338 pp_c_initializer_list (c_pretty_printer *pp, tree e) 1339 { 1340 tree type = TREE_TYPE (e); 1341 const enum tree_code code = TREE_CODE (type); 1342 1343 if (TREE_CODE (e) == CONSTRUCTOR) 1344 { 1345 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e)); 1346 return; 1347 } 1348 1349 switch (code) 1350 { 1351 case RECORD_TYPE: 1352 case UNION_TYPE: 1353 case ARRAY_TYPE: 1354 { 1355 tree init = TREE_OPERAND (e, 0); 1356 for (; init != NULL_TREE; init = TREE_CHAIN (init)) 1357 { 1358 if (code == RECORD_TYPE || code == UNION_TYPE) 1359 { 1360 pp_c_dot (pp); 1361 pp->primary_expression (TREE_PURPOSE (init)); 1362 } 1363 else 1364 { 1365 pp_c_left_bracket (pp); 1366 if (TREE_PURPOSE (init)) 1367 pp->constant (TREE_PURPOSE (init)); 1368 pp_c_right_bracket (pp); 1369 } 1370 pp_c_whitespace (pp); 1371 pp_equal (pp); 1372 pp_c_whitespace (pp); 1373 pp->initializer (TREE_VALUE (init)); 1374 if (TREE_CHAIN (init)) 1375 pp_separate_with (pp, ','); 1376 } 1377 } 1378 return; 1379 1380 case VECTOR_TYPE: 1381 if (TREE_CODE (e) == VECTOR_CST) 1382 { 1383 /* We don't create variable-length VECTOR_CSTs. */ 1384 unsigned int nunits = VECTOR_CST_NELTS (e).to_constant (); 1385 for (unsigned int i = 0; i < nunits; ++i) 1386 { 1387 if (i > 0) 1388 pp_separate_with (pp, ','); 1389 pp->expression (VECTOR_CST_ELT (e, i)); 1390 } 1391 } 1392 else 1393 break; 1394 return; 1395 1396 case COMPLEX_TYPE: 1397 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR) 1398 { 1399 const bool cst = TREE_CODE (e) == COMPLEX_CST; 1400 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0)); 1401 pp_separate_with (pp, ','); 1402 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1)); 1403 } 1404 else 1405 break; 1406 return; 1407 1408 default: 1409 break; 1410 } 1411 1412 pp_unsupported_tree (pp, type); 1413 } 1414 1415 /* Pretty-print a brace-enclosed initializer-list. */ 1416 1417 static void 1418 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l) 1419 { 1420 pp_c_left_brace (pp); 1421 pp_c_initializer_list (pp, l); 1422 pp_c_right_brace (pp); 1423 } 1424 1425 1426 /* This is a convenient function, used to bridge gap between C and C++ 1427 grammars. 1428 1429 id-expression: 1430 identifier */ 1431 1432 void 1433 c_pretty_printer::id_expression (tree t) 1434 { 1435 switch (TREE_CODE (t)) 1436 { 1437 case VAR_DECL: 1438 case PARM_DECL: 1439 case CONST_DECL: 1440 case TYPE_DECL: 1441 case FUNCTION_DECL: 1442 case FIELD_DECL: 1443 case LABEL_DECL: 1444 pp_c_tree_decl_identifier (this, t); 1445 break; 1446 1447 case IDENTIFIER_NODE: 1448 pp_c_tree_identifier (this, t); 1449 break; 1450 1451 default: 1452 pp_unsupported_tree (this, t); 1453 break; 1454 } 1455 } 1456 1457 /* postfix-expression: 1458 primary-expression 1459 postfix-expression [ expression ] 1460 postfix-expression ( argument-expression-list(opt) ) 1461 postfix-expression . identifier 1462 postfix-expression -> identifier 1463 postfix-expression ++ 1464 postfix-expression -- 1465 ( type-name ) { initializer-list } 1466 ( type-name ) { initializer-list , } */ 1467 1468 void 1469 c_pretty_printer::postfix_expression (tree e) 1470 { 1471 enum tree_code code = TREE_CODE (e); 1472 switch (code) 1473 { 1474 case POSTINCREMENT_EXPR: 1475 case POSTDECREMENT_EXPR: 1476 postfix_expression (TREE_OPERAND (e, 0)); 1477 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--"); 1478 break; 1479 1480 case ARRAY_REF: 1481 postfix_expression (TREE_OPERAND (e, 0)); 1482 pp_c_left_bracket (this); 1483 expression (TREE_OPERAND (e, 1)); 1484 pp_c_right_bracket (this); 1485 break; 1486 1487 case CALL_EXPR: 1488 { 1489 call_expr_arg_iterator iter; 1490 tree arg; 1491 postfix_expression (CALL_EXPR_FN (e)); 1492 pp_c_left_paren (this); 1493 FOR_EACH_CALL_EXPR_ARG (arg, iter, e) 1494 { 1495 expression (arg); 1496 if (more_call_expr_args_p (&iter)) 1497 pp_separate_with (this, ','); 1498 } 1499 pp_c_right_paren (this); 1500 break; 1501 } 1502 1503 case UNORDERED_EXPR: 1504 pp_c_ws_string (this, flag_isoc99 1505 ? "isunordered" 1506 : "__builtin_isunordered"); 1507 goto two_args_fun; 1508 1509 case ORDERED_EXPR: 1510 pp_c_ws_string (this, flag_isoc99 1511 ? "!isunordered" 1512 : "!__builtin_isunordered"); 1513 goto two_args_fun; 1514 1515 case UNLT_EXPR: 1516 pp_c_ws_string (this, flag_isoc99 1517 ? "!isgreaterequal" 1518 : "!__builtin_isgreaterequal"); 1519 goto two_args_fun; 1520 1521 case UNLE_EXPR: 1522 pp_c_ws_string (this, flag_isoc99 1523 ? "!isgreater" 1524 : "!__builtin_isgreater"); 1525 goto two_args_fun; 1526 1527 case UNGT_EXPR: 1528 pp_c_ws_string (this, flag_isoc99 1529 ? "!islessequal" 1530 : "!__builtin_islessequal"); 1531 goto two_args_fun; 1532 1533 case UNGE_EXPR: 1534 pp_c_ws_string (this, flag_isoc99 1535 ? "!isless" 1536 : "!__builtin_isless"); 1537 goto two_args_fun; 1538 1539 case UNEQ_EXPR: 1540 pp_c_ws_string (this, flag_isoc99 1541 ? "!islessgreater" 1542 : "!__builtin_islessgreater"); 1543 goto two_args_fun; 1544 1545 case LTGT_EXPR: 1546 pp_c_ws_string (this, flag_isoc99 1547 ? "islessgreater" 1548 : "__builtin_islessgreater"); 1549 goto two_args_fun; 1550 1551 case MAX_EXPR: 1552 pp_c_ws_string (this, "max"); 1553 goto two_args_fun; 1554 1555 case MIN_EXPR: 1556 pp_c_ws_string (this, "min"); 1557 goto two_args_fun; 1558 1559 two_args_fun: 1560 pp_c_left_paren (this); 1561 expression (TREE_OPERAND (e, 0)); 1562 pp_separate_with (this, ','); 1563 expression (TREE_OPERAND (e, 1)); 1564 pp_c_right_paren (this); 1565 break; 1566 1567 case ABS_EXPR: 1568 pp_c_ws_string (this, "__builtin_abs"); 1569 pp_c_left_paren (this); 1570 expression (TREE_OPERAND (e, 0)); 1571 pp_c_right_paren (this); 1572 break; 1573 1574 case COMPONENT_REF: 1575 { 1576 tree object = TREE_OPERAND (e, 0); 1577 if (INDIRECT_REF_P (object)) 1578 { 1579 postfix_expression (TREE_OPERAND (object, 0)); 1580 pp_c_arrow (this); 1581 } 1582 else 1583 { 1584 postfix_expression (object); 1585 pp_c_dot (this); 1586 } 1587 expression (TREE_OPERAND (e, 1)); 1588 } 1589 break; 1590 1591 case BIT_FIELD_REF: 1592 { 1593 tree type = TREE_TYPE (e); 1594 1595 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type); 1596 if (type 1597 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1))) 1598 { 1599 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2)); 1600 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type)); 1601 if ((bitpos % size) == 0) 1602 { 1603 pp_c_left_paren (this); 1604 pp_c_left_paren (this); 1605 type_id (type); 1606 pp_c_star (this); 1607 pp_c_right_paren (this); 1608 pp_c_ampersand (this); 1609 expression (TREE_OPERAND (e, 0)); 1610 pp_c_right_paren (this); 1611 pp_c_left_bracket (this); 1612 pp_wide_integer (this, bitpos / size); 1613 pp_c_right_bracket (this); 1614 break; 1615 } 1616 } 1617 pp_unsupported_tree (this, e); 1618 } 1619 break; 1620 1621 case MEM_REF: 1622 expression (e); 1623 break; 1624 1625 case COMPLEX_CST: 1626 case VECTOR_CST: 1627 pp_c_compound_literal (this, e); 1628 break; 1629 1630 case COMPLEX_EXPR: 1631 pp_c_complex_expr (this, e); 1632 break; 1633 1634 case COMPOUND_LITERAL_EXPR: 1635 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e)); 1636 /* Fall through. */ 1637 case CONSTRUCTOR: 1638 initializer (e); 1639 break; 1640 1641 case VA_ARG_EXPR: 1642 pp_c_ws_string (this, "__builtin_va_arg"); 1643 pp_c_left_paren (this); 1644 assignment_expression (TREE_OPERAND (e, 0)); 1645 pp_separate_with (this, ','); 1646 type_id (TREE_TYPE (e)); 1647 pp_c_right_paren (this); 1648 break; 1649 1650 case ADDR_EXPR: 1651 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL) 1652 { 1653 id_expression (TREE_OPERAND (e, 0)); 1654 break; 1655 } 1656 /* fall through. */ 1657 1658 default: 1659 primary_expression (e); 1660 break; 1661 } 1662 } 1663 1664 /* Print out an expression-list; E is expected to be a TREE_LIST. */ 1665 1666 void 1667 pp_c_expression_list (c_pretty_printer *pp, tree e) 1668 { 1669 for (; e != NULL_TREE; e = TREE_CHAIN (e)) 1670 { 1671 pp->expression (TREE_VALUE (e)); 1672 if (TREE_CHAIN (e)) 1673 pp_separate_with (pp, ','); 1674 } 1675 } 1676 1677 /* Print out V, which contains the elements of a constructor. */ 1678 1679 void 1680 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v) 1681 { 1682 unsigned HOST_WIDE_INT ix; 1683 tree value; 1684 1685 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value) 1686 { 1687 pp->expression (value); 1688 if (ix != vec_safe_length (v) - 1) 1689 pp_separate_with (pp, ','); 1690 } 1691 } 1692 1693 /* Print out an expression-list in parens, as if it were the argument 1694 list to a function. */ 1695 1696 void 1697 pp_c_call_argument_list (c_pretty_printer *pp, tree t) 1698 { 1699 pp_c_left_paren (pp); 1700 if (t && TREE_CODE (t) == TREE_LIST) 1701 pp_c_expression_list (pp, t); 1702 pp_c_right_paren (pp); 1703 } 1704 1705 /* unary-expression: 1706 postfix-expression 1707 ++ cast-expression 1708 -- cast-expression 1709 unary-operator cast-expression 1710 sizeof unary-expression 1711 sizeof ( type-id ) 1712 1713 unary-operator: one of 1714 * & + - ! ~ 1715 1716 GNU extensions. 1717 unary-expression: 1718 __alignof__ unary-expression 1719 __alignof__ ( type-id ) 1720 __real__ unary-expression 1721 __imag__ unary-expression */ 1722 1723 void 1724 c_pretty_printer::unary_expression (tree e) 1725 { 1726 enum tree_code code = TREE_CODE (e); 1727 switch (code) 1728 { 1729 case PREINCREMENT_EXPR: 1730 case PREDECREMENT_EXPR: 1731 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--"); 1732 unary_expression (TREE_OPERAND (e, 0)); 1733 break; 1734 1735 case ADDR_EXPR: 1736 case INDIRECT_REF: 1737 case NEGATE_EXPR: 1738 case BIT_NOT_EXPR: 1739 case TRUTH_NOT_EXPR: 1740 case CONJ_EXPR: 1741 /* String literal are used by address. */ 1742 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST) 1743 pp_ampersand (this); 1744 else if (code == INDIRECT_REF) 1745 { 1746 tree type = TREE_TYPE (TREE_OPERAND (e, 0)); 1747 if (type && TREE_CODE (type) == REFERENCE_TYPE) 1748 /* Reference decay is implicit, don't print anything. */; 1749 else 1750 pp_c_star (this); 1751 } 1752 else if (code == NEGATE_EXPR) 1753 pp_minus (this); 1754 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR) 1755 pp_complement (this); 1756 else if (code == TRUTH_NOT_EXPR) 1757 pp_exclamation (this); 1758 pp_c_cast_expression (this, TREE_OPERAND (e, 0)); 1759 break; 1760 1761 case MEM_REF: 1762 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR 1763 && integer_zerop (TREE_OPERAND (e, 1))) 1764 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0)); 1765 else 1766 { 1767 pp_c_star (this); 1768 if (!integer_zerop (TREE_OPERAND (e, 1))) 1769 { 1770 pp_c_left_paren (this); 1771 if (!integer_onep (TYPE_SIZE_UNIT 1772 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0)))))) 1773 pp_c_type_cast (this, ptr_type_node); 1774 } 1775 pp_c_cast_expression (this, TREE_OPERAND (e, 0)); 1776 if (!integer_zerop (TREE_OPERAND (e, 1))) 1777 { 1778 pp_plus (this); 1779 pp_c_integer_constant (this, 1780 fold_convert (ssizetype, 1781 TREE_OPERAND (e, 1))); 1782 pp_c_right_paren (this); 1783 } 1784 } 1785 break; 1786 1787 case REALPART_EXPR: 1788 case IMAGPART_EXPR: 1789 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__"); 1790 pp_c_whitespace (this); 1791 unary_expression (TREE_OPERAND (e, 0)); 1792 break; 1793 1794 default: 1795 postfix_expression (e); 1796 break; 1797 } 1798 } 1799 1800 /* cast-expression: 1801 unary-expression 1802 ( type-name ) cast-expression */ 1803 1804 void 1805 pp_c_cast_expression (c_pretty_printer *pp, tree e) 1806 { 1807 switch (TREE_CODE (e)) 1808 { 1809 case FLOAT_EXPR: 1810 case FIX_TRUNC_EXPR: 1811 CASE_CONVERT: 1812 case VIEW_CONVERT_EXPR: 1813 if (!location_wrapper_p (e)) 1814 pp_c_type_cast (pp, TREE_TYPE (e)); 1815 pp_c_cast_expression (pp, TREE_OPERAND (e, 0)); 1816 break; 1817 1818 default: 1819 pp->unary_expression (e); 1820 } 1821 } 1822 1823 /* multiplicative-expression: 1824 cast-expression 1825 multiplicative-expression * cast-expression 1826 multiplicative-expression / cast-expression 1827 multiplicative-expression % cast-expression */ 1828 1829 void 1830 c_pretty_printer::multiplicative_expression (tree e) 1831 { 1832 enum tree_code code = TREE_CODE (e); 1833 switch (code) 1834 { 1835 case MULT_EXPR: 1836 case TRUNC_DIV_EXPR: 1837 case TRUNC_MOD_EXPR: 1838 case EXACT_DIV_EXPR: 1839 case RDIV_EXPR: 1840 multiplicative_expression (TREE_OPERAND (e, 0)); 1841 pp_c_whitespace (this); 1842 if (code == MULT_EXPR) 1843 pp_c_star (this); 1844 else if (code != TRUNC_MOD_EXPR) 1845 pp_slash (this); 1846 else 1847 pp_modulo (this); 1848 pp_c_whitespace (this); 1849 pp_c_cast_expression (this, TREE_OPERAND (e, 1)); 1850 break; 1851 1852 default: 1853 pp_c_cast_expression (this, e); 1854 break; 1855 } 1856 } 1857 1858 /* additive-expression: 1859 multiplicative-expression 1860 additive-expression + multiplicative-expression 1861 additive-expression - multiplicative-expression */ 1862 1863 static void 1864 pp_c_additive_expression (c_pretty_printer *pp, tree e) 1865 { 1866 enum tree_code code = TREE_CODE (e); 1867 switch (code) 1868 { 1869 case POINTER_PLUS_EXPR: 1870 case PLUS_EXPR: 1871 case POINTER_DIFF_EXPR: 1872 case MINUS_EXPR: 1873 pp_c_additive_expression (pp, TREE_OPERAND (e, 0)); 1874 pp_c_whitespace (pp); 1875 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) 1876 pp_plus (pp); 1877 else 1878 pp_minus (pp); 1879 pp_c_whitespace (pp); 1880 pp->multiplicative_expression (TREE_OPERAND (e, 1)); 1881 break; 1882 1883 default: 1884 pp->multiplicative_expression (e); 1885 break; 1886 } 1887 } 1888 1889 /* additive-expression: 1890 additive-expression 1891 shift-expression << additive-expression 1892 shift-expression >> additive-expression */ 1893 1894 static void 1895 pp_c_shift_expression (c_pretty_printer *pp, tree e) 1896 { 1897 enum tree_code code = TREE_CODE (e); 1898 switch (code) 1899 { 1900 case LSHIFT_EXPR: 1901 case RSHIFT_EXPR: 1902 case LROTATE_EXPR: 1903 case RROTATE_EXPR: 1904 pp_c_shift_expression (pp, TREE_OPERAND (e, 0)); 1905 pp_c_whitespace (pp); 1906 pp_string (pp, code == LSHIFT_EXPR ? "<<" : 1907 code == RSHIFT_EXPR ? ">>" : 1908 code == LROTATE_EXPR ? "<<<" : ">>>"); 1909 pp_c_whitespace (pp); 1910 pp_c_additive_expression (pp, TREE_OPERAND (e, 1)); 1911 break; 1912 1913 default: 1914 pp_c_additive_expression (pp, e); 1915 } 1916 } 1917 1918 /* relational-expression: 1919 shift-expression 1920 relational-expression < shift-expression 1921 relational-expression > shift-expression 1922 relational-expression <= shift-expression 1923 relational-expression >= shift-expression */ 1924 1925 static void 1926 pp_c_relational_expression (c_pretty_printer *pp, tree e) 1927 { 1928 enum tree_code code = TREE_CODE (e); 1929 switch (code) 1930 { 1931 case LT_EXPR: 1932 case GT_EXPR: 1933 case LE_EXPR: 1934 case GE_EXPR: 1935 pp_c_relational_expression (pp, TREE_OPERAND (e, 0)); 1936 pp_c_whitespace (pp); 1937 if (code == LT_EXPR) 1938 pp_less (pp); 1939 else if (code == GT_EXPR) 1940 pp_greater (pp); 1941 else if (code == LE_EXPR) 1942 pp_less_equal (pp); 1943 else if (code == GE_EXPR) 1944 pp_greater_equal (pp); 1945 pp_c_whitespace (pp); 1946 pp_c_shift_expression (pp, TREE_OPERAND (e, 1)); 1947 break; 1948 1949 default: 1950 pp_c_shift_expression (pp, e); 1951 break; 1952 } 1953 } 1954 1955 /* equality-expression: 1956 relational-expression 1957 equality-expression == relational-expression 1958 equality-equality != relational-expression */ 1959 1960 static void 1961 pp_c_equality_expression (c_pretty_printer *pp, tree e) 1962 { 1963 enum tree_code code = TREE_CODE (e); 1964 switch (code) 1965 { 1966 case EQ_EXPR: 1967 case NE_EXPR: 1968 pp_c_equality_expression (pp, TREE_OPERAND (e, 0)); 1969 pp_c_whitespace (pp); 1970 pp_string (pp, code == EQ_EXPR ? "==" : "!="); 1971 pp_c_whitespace (pp); 1972 pp_c_relational_expression (pp, TREE_OPERAND (e, 1)); 1973 break; 1974 1975 default: 1976 pp_c_relational_expression (pp, e); 1977 break; 1978 } 1979 } 1980 1981 /* AND-expression: 1982 equality-expression 1983 AND-expression & equality-equality */ 1984 1985 static void 1986 pp_c_and_expression (c_pretty_printer *pp, tree e) 1987 { 1988 if (TREE_CODE (e) == BIT_AND_EXPR) 1989 { 1990 pp_c_and_expression (pp, TREE_OPERAND (e, 0)); 1991 pp_c_whitespace (pp); 1992 pp_ampersand (pp); 1993 pp_c_whitespace (pp); 1994 pp_c_equality_expression (pp, TREE_OPERAND (e, 1)); 1995 } 1996 else 1997 pp_c_equality_expression (pp, e); 1998 } 1999 2000 /* exclusive-OR-expression: 2001 AND-expression 2002 exclusive-OR-expression ^ AND-expression */ 2003 2004 static void 2005 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e) 2006 { 2007 if (TREE_CODE (e) == BIT_XOR_EXPR 2008 || TREE_CODE (e) == TRUTH_XOR_EXPR) 2009 { 2010 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0)); 2011 if (TREE_CODE (e) == BIT_XOR_EXPR) 2012 pp_c_maybe_whitespace (pp); 2013 else 2014 pp_c_whitespace (pp); 2015 pp_carret (pp); 2016 pp_c_whitespace (pp); 2017 pp_c_and_expression (pp, TREE_OPERAND (e, 1)); 2018 } 2019 else 2020 pp_c_and_expression (pp, e); 2021 } 2022 2023 /* inclusive-OR-expression: 2024 exclusive-OR-expression 2025 inclusive-OR-expression | exclusive-OR-expression */ 2026 2027 static void 2028 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e) 2029 { 2030 if (TREE_CODE (e) == BIT_IOR_EXPR) 2031 { 2032 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0)); 2033 pp_c_whitespace (pp); 2034 pp_bar (pp); 2035 pp_c_whitespace (pp); 2036 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1)); 2037 } 2038 else 2039 pp_c_exclusive_or_expression (pp, e); 2040 } 2041 2042 /* logical-AND-expression: 2043 inclusive-OR-expression 2044 logical-AND-expression && inclusive-OR-expression */ 2045 2046 static void 2047 pp_c_logical_and_expression (c_pretty_printer *pp, tree e) 2048 { 2049 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR 2050 || TREE_CODE (e) == TRUTH_AND_EXPR) 2051 { 2052 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0)); 2053 pp_c_whitespace (pp); 2054 pp_ampersand_ampersand (pp); 2055 pp_c_whitespace (pp); 2056 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1)); 2057 } 2058 else 2059 pp_c_inclusive_or_expression (pp, e); 2060 } 2061 2062 /* logical-OR-expression: 2063 logical-AND-expression 2064 logical-OR-expression || logical-AND-expression */ 2065 2066 void 2067 pp_c_logical_or_expression (c_pretty_printer *pp, tree e) 2068 { 2069 if (TREE_CODE (e) == TRUTH_ORIF_EXPR 2070 || TREE_CODE (e) == TRUTH_OR_EXPR) 2071 { 2072 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0)); 2073 pp_c_whitespace (pp); 2074 pp_bar_bar (pp); 2075 pp_c_whitespace (pp); 2076 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1)); 2077 } 2078 else 2079 pp_c_logical_and_expression (pp, e); 2080 } 2081 2082 /* conditional-expression: 2083 logical-OR-expression 2084 logical-OR-expression ? expression : conditional-expression */ 2085 2086 void 2087 c_pretty_printer::conditional_expression (tree e) 2088 { 2089 if (TREE_CODE (e) == COND_EXPR) 2090 { 2091 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0)); 2092 pp_c_whitespace (this); 2093 pp_question (this); 2094 pp_c_whitespace (this); 2095 expression (TREE_OPERAND (e, 1)); 2096 pp_c_whitespace (this); 2097 pp_colon (this); 2098 pp_c_whitespace (this); 2099 conditional_expression (TREE_OPERAND (e, 2)); 2100 } 2101 else 2102 pp_c_logical_or_expression (this, e); 2103 } 2104 2105 2106 /* assignment-expression: 2107 conditional-expression 2108 unary-expression assignment-operator assignment-expression 2109 2110 assignment-expression: one of 2111 = *= /= %= += -= >>= <<= &= ^= |= */ 2112 2113 void 2114 c_pretty_printer::assignment_expression (tree e) 2115 { 2116 if (TREE_CODE (e) == MODIFY_EXPR 2117 || TREE_CODE (e) == INIT_EXPR) 2118 { 2119 unary_expression (TREE_OPERAND (e, 0)); 2120 pp_c_whitespace (this); 2121 pp_equal (this); 2122 pp_space (this); 2123 expression (TREE_OPERAND (e, 1)); 2124 } 2125 else 2126 conditional_expression (e); 2127 } 2128 2129 /* expression: 2130 assignment-expression 2131 expression , assignment-expression 2132 2133 Implementation note: instead of going through the usual recursion 2134 chain, I take the liberty of dispatching nodes to the appropriate 2135 functions. This makes some redundancy, but it worths it. That also 2136 prevents a possible infinite recursion between primary_expression () 2137 and expression (). */ 2138 2139 void 2140 c_pretty_printer::expression (tree e) 2141 { 2142 switch (TREE_CODE (e)) 2143 { 2144 case VOID_CST: 2145 pp_c_void_constant (this); 2146 break; 2147 2148 case INTEGER_CST: 2149 pp_c_integer_constant (this, e); 2150 break; 2151 2152 case REAL_CST: 2153 pp_c_floating_constant (this, e); 2154 break; 2155 2156 case FIXED_CST: 2157 pp_c_fixed_constant (this, e); 2158 break; 2159 2160 case STRING_CST: 2161 pp_c_string_literal (this, e); 2162 break; 2163 2164 case IDENTIFIER_NODE: 2165 case FUNCTION_DECL: 2166 case VAR_DECL: 2167 case CONST_DECL: 2168 case PARM_DECL: 2169 case RESULT_DECL: 2170 case FIELD_DECL: 2171 case LABEL_DECL: 2172 case ERROR_MARK: 2173 primary_expression (e); 2174 break; 2175 2176 case SSA_NAME: 2177 if (SSA_NAME_VAR (e) 2178 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e))) 2179 expression (SSA_NAME_VAR (e)); 2180 else 2181 translate_string ("<unknown>"); 2182 break; 2183 2184 case POSTINCREMENT_EXPR: 2185 case POSTDECREMENT_EXPR: 2186 case ARRAY_REF: 2187 case CALL_EXPR: 2188 case COMPONENT_REF: 2189 case BIT_FIELD_REF: 2190 case COMPLEX_CST: 2191 case COMPLEX_EXPR: 2192 case VECTOR_CST: 2193 case ORDERED_EXPR: 2194 case UNORDERED_EXPR: 2195 case LTGT_EXPR: 2196 case UNEQ_EXPR: 2197 case UNLE_EXPR: 2198 case UNLT_EXPR: 2199 case UNGE_EXPR: 2200 case UNGT_EXPR: 2201 case MAX_EXPR: 2202 case MIN_EXPR: 2203 case ABS_EXPR: 2204 case CONSTRUCTOR: 2205 case COMPOUND_LITERAL_EXPR: 2206 case VA_ARG_EXPR: 2207 postfix_expression (e); 2208 break; 2209 2210 case CONJ_EXPR: 2211 case ADDR_EXPR: 2212 case INDIRECT_REF: 2213 case MEM_REF: 2214 case NEGATE_EXPR: 2215 case BIT_NOT_EXPR: 2216 case TRUTH_NOT_EXPR: 2217 case PREINCREMENT_EXPR: 2218 case PREDECREMENT_EXPR: 2219 case REALPART_EXPR: 2220 case IMAGPART_EXPR: 2221 unary_expression (e); 2222 break; 2223 2224 case FLOAT_EXPR: 2225 case FIX_TRUNC_EXPR: 2226 CASE_CONVERT: 2227 case VIEW_CONVERT_EXPR: 2228 pp_c_cast_expression (this, e); 2229 break; 2230 2231 case MULT_EXPR: 2232 case TRUNC_MOD_EXPR: 2233 case TRUNC_DIV_EXPR: 2234 case EXACT_DIV_EXPR: 2235 case RDIV_EXPR: 2236 multiplicative_expression (e); 2237 break; 2238 2239 case LSHIFT_EXPR: 2240 case RSHIFT_EXPR: 2241 case LROTATE_EXPR: 2242 case RROTATE_EXPR: 2243 pp_c_shift_expression (this, e); 2244 break; 2245 2246 case LT_EXPR: 2247 case GT_EXPR: 2248 case LE_EXPR: 2249 case GE_EXPR: 2250 pp_c_relational_expression (this, e); 2251 break; 2252 2253 case BIT_AND_EXPR: 2254 pp_c_and_expression (this, e); 2255 break; 2256 2257 case BIT_XOR_EXPR: 2258 case TRUTH_XOR_EXPR: 2259 pp_c_exclusive_or_expression (this, e); 2260 break; 2261 2262 case BIT_IOR_EXPR: 2263 pp_c_inclusive_or_expression (this, e); 2264 break; 2265 2266 case TRUTH_ANDIF_EXPR: 2267 case TRUTH_AND_EXPR: 2268 pp_c_logical_and_expression (this, e); 2269 break; 2270 2271 case TRUTH_ORIF_EXPR: 2272 case TRUTH_OR_EXPR: 2273 pp_c_logical_or_expression (this, e); 2274 break; 2275 2276 case EQ_EXPR: 2277 case NE_EXPR: 2278 pp_c_equality_expression (this, e); 2279 break; 2280 2281 case COND_EXPR: 2282 conditional_expression (e); 2283 break; 2284 2285 case POINTER_PLUS_EXPR: 2286 case PLUS_EXPR: 2287 case POINTER_DIFF_EXPR: 2288 case MINUS_EXPR: 2289 pp_c_additive_expression (this, e); 2290 break; 2291 2292 case MODIFY_EXPR: 2293 case INIT_EXPR: 2294 assignment_expression (e); 2295 break; 2296 2297 case COMPOUND_EXPR: 2298 pp_c_left_paren (this); 2299 expression (TREE_OPERAND (e, 0)); 2300 pp_separate_with (this, ','); 2301 assignment_expression (TREE_OPERAND (e, 1)); 2302 pp_c_right_paren (this); 2303 break; 2304 2305 case NON_LVALUE_EXPR: 2306 case SAVE_EXPR: 2307 expression (TREE_OPERAND (e, 0)); 2308 break; 2309 2310 case TARGET_EXPR: 2311 postfix_expression (TREE_OPERAND (e, 1)); 2312 break; 2313 2314 case BIND_EXPR: 2315 case GOTO_EXPR: 2316 /* We don't yet have a way of dumping statements in a 2317 human-readable format. */ 2318 pp_string (this, "({...})"); 2319 break; 2320 2321 case C_MAYBE_CONST_EXPR: 2322 expression (C_MAYBE_CONST_EXPR_EXPR (e)); 2323 break; 2324 2325 default: 2326 pp_unsupported_tree (this, e); 2327 break; 2328 } 2329 } 2330 2331 2332 2333 /* Statements. */ 2334 2335 void 2336 c_pretty_printer::statement (tree stmt) 2337 { 2338 if (stmt == NULL) 2339 return; 2340 2341 if (pp_needs_newline (this)) 2342 pp_newline_and_indent (this, 0); 2343 2344 dump_generic_node (this, stmt, pp_indentation (this), 0, true); 2345 } 2346 2347 2348 /* Initialize the PRETTY-PRINTER for handling C codes. */ 2349 2350 c_pretty_printer::c_pretty_printer () 2351 : pretty_printer (), 2352 offset_list (), 2353 flags () 2354 { 2355 type_specifier_seq = pp_c_specifier_qualifier_list; 2356 ptr_operator = pp_c_pointer; 2357 parameter_list = pp_c_parameter_type_list; 2358 } 2359 2360 2361 /* Print the tree T in full, on file FILE. */ 2362 2363 void 2364 print_c_tree (FILE *file, tree t) 2365 { 2366 c_pretty_printer pp; 2367 2368 pp_needs_newline (&pp) = true; 2369 pp.buffer->stream = file; 2370 pp.statement (t); 2371 pp_newline_and_flush (&pp); 2372 } 2373 2374 /* Print the tree T in full, on stderr. */ 2375 2376 DEBUG_FUNCTION void 2377 debug_c_tree (tree t) 2378 { 2379 print_c_tree (stderr, t); 2380 fputc ('\n', stderr); 2381 } 2382 2383 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made 2384 up of T's memory address. */ 2385 2386 void 2387 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t) 2388 { 2389 const char *name; 2390 2391 gcc_assert (DECL_P (t)); 2392 2393 if (DECL_NAME (t)) 2394 name = IDENTIFIER_POINTER (DECL_NAME (t)); 2395 else 2396 { 2397 static char xname[8]; 2398 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t) 2399 & 0xffff))); 2400 name = xname; 2401 } 2402 2403 pp_c_identifier (pp, name); 2404 } 2405 2406 #if CHECKING_P 2407 2408 namespace selftest { 2409 2410 /* Selftests for pretty-printing trees. */ 2411 2412 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using 2413 LOC as the effective location for any failures. */ 2414 2415 static void 2416 assert_c_pretty_printer_output (const location &loc, const char *expected, 2417 tree expr) 2418 { 2419 c_pretty_printer pp; 2420 pp.expression (expr); 2421 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp)); 2422 } 2423 2424 /* Helper function for calling assert_c_pretty_printer_output. 2425 This is to avoid having to write SELFTEST_LOCATION. */ 2426 2427 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \ 2428 SELFTEST_BEGIN_STMT \ 2429 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \ 2430 (EXPECTED), \ 2431 (EXPR)); \ 2432 SELFTEST_END_STMT 2433 2434 /* Verify that location wrappers don't show up in pretty-printed output. */ 2435 2436 static void 2437 test_location_wrappers () 2438 { 2439 /* VAR_DECL. */ 2440 tree id = get_identifier ("foo"); 2441 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id, 2442 integer_type_node); 2443 tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION); 2444 ASSERT_NE (wrapped_decl, decl); 2445 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl); 2446 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl); 2447 2448 /* INTEGER_CST. */ 2449 tree int_cst = build_int_cst (integer_type_node, 42); 2450 tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION); 2451 ASSERT_NE (wrapped_cst, int_cst); 2452 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst); 2453 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst); 2454 } 2455 2456 /* Run all of the selftests within this file. */ 2457 2458 void 2459 c_pretty_print_c_tests () 2460 { 2461 test_location_wrappers (); 2462 } 2463 2464 } // namespace selftest 2465 2466 #endif /* CHECKING_P */ 2467