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