1 /* Implementation of subroutines for the GNU C++ pretty-printer. 2 Copyright (C) 2003-2013 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 "tm.h" 25 #include "intl.h" 26 #include "cp-tree.h" 27 #include "cxx-pretty-print.h" 28 #include "tree-pretty-print.h" 29 30 /* Translate if being used for diagnostics, but not for dump files or 31 __PRETTY_FUNCTION. */ 32 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid)) 33 34 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree); 35 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree); 36 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree); 37 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree); 38 static void pp_cxx_expression (cxx_pretty_printer *, tree); 39 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree); 40 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree); 41 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree); 42 static void pp_cxx_type_id (cxx_pretty_printer *, tree); 43 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree); 44 static void pp_cxx_declarator (cxx_pretty_printer *, tree); 45 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree); 46 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree); 47 static void pp_cxx_statement (cxx_pretty_printer *, tree); 48 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree); 49 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree); 50 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree); 51 52 53 static inline void 54 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c) 55 { 56 const char *p = pp_last_position_in_text (pp); 57 58 if (p != NULL && *p == c) 59 pp_cxx_whitespace (pp); 60 pp_character (pp, c); 61 pp_base (pp)->padding = pp_none; 62 } 63 64 #define pp_cxx_storage_class_specifier(PP, T) \ 65 pp_c_storage_class_specifier (pp_c_base (PP), T) 66 #define pp_cxx_expression_list(PP, T) \ 67 pp_c_expression_list (pp_c_base (PP), T) 68 #define pp_cxx_space_for_pointer_operator(PP, T) \ 69 pp_c_space_for_pointer_operator (pp_c_base (PP), T) 70 #define pp_cxx_init_declarator(PP, T) \ 71 pp_c_init_declarator (pp_c_base (PP), T) 72 #define pp_cxx_call_argument_list(PP, T) \ 73 pp_c_call_argument_list (pp_c_base (PP), T) 74 75 void 76 pp_cxx_colon_colon (cxx_pretty_printer *pp) 77 { 78 pp_colon_colon (pp); 79 pp_base (pp)->padding = pp_none; 80 } 81 82 void 83 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp) 84 { 85 pp_cxx_nonconsecutive_character (pp, '<'); 86 } 87 88 void 89 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp) 90 { 91 pp_cxx_nonconsecutive_character (pp, '>'); 92 } 93 94 void 95 pp_cxx_separate_with (cxx_pretty_printer *pp, int c) 96 { 97 pp_separate_with (pp, c); 98 pp_base (pp)->padding = pp_none; 99 } 100 101 /* Expressions. */ 102 103 static inline bool 104 is_destructor_name (tree name) 105 { 106 return name == complete_dtor_identifier 107 || name == base_dtor_identifier 108 || name == deleting_dtor_identifier; 109 } 110 111 /* conversion-function-id: 112 operator conversion-type-id 113 114 conversion-type-id: 115 type-specifier-seq conversion-declarator(opt) 116 117 conversion-declarator: 118 ptr-operator conversion-declarator(opt) */ 119 120 static inline void 121 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t) 122 { 123 pp_cxx_ws_string (pp, "operator"); 124 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); 125 } 126 127 static inline void 128 pp_cxx_template_id (cxx_pretty_printer *pp, tree t) 129 { 130 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0)); 131 pp_cxx_begin_template_argument_list (pp); 132 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1)); 133 pp_cxx_end_template_argument_list (pp); 134 } 135 136 /* Prints the unqualified part of the id-expression T. 137 138 unqualified-id: 139 identifier 140 operator-function-id 141 conversion-function-id 142 ~ class-name 143 template-id */ 144 145 static void 146 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t) 147 { 148 enum tree_code code = TREE_CODE (t); 149 switch (code) 150 { 151 case RESULT_DECL: 152 pp_cxx_ws_string (pp, M_("<return-value>")); 153 break; 154 155 case OVERLOAD: 156 t = OVL_CURRENT (t); 157 case VAR_DECL: 158 case PARM_DECL: 159 case CONST_DECL: 160 case TYPE_DECL: 161 case FUNCTION_DECL: 162 case NAMESPACE_DECL: 163 case FIELD_DECL: 164 case LABEL_DECL: 165 case USING_DECL: 166 case TEMPLATE_DECL: 167 t = DECL_NAME (t); 168 169 case IDENTIFIER_NODE: 170 if (t == NULL) 171 pp_cxx_ws_string (pp, M_("<unnamed>")); 172 else if (IDENTIFIER_TYPENAME_P (t)) 173 pp_cxx_conversion_function_id (pp, t); 174 else 175 { 176 if (is_destructor_name (t)) 177 { 178 pp_complement (pp); 179 /* FIXME: Why is this necessary? */ 180 if (TREE_TYPE (t)) 181 t = constructor_name (TREE_TYPE (t)); 182 } 183 pp_cxx_tree_identifier (pp, t); 184 } 185 break; 186 187 case TEMPLATE_ID_EXPR: 188 pp_cxx_template_id (pp, t); 189 break; 190 191 case BASELINK: 192 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t)); 193 break; 194 195 case RECORD_TYPE: 196 case UNION_TYPE: 197 case ENUMERAL_TYPE: 198 case TYPENAME_TYPE: 199 case UNBOUND_CLASS_TEMPLATE: 200 pp_cxx_unqualified_id (pp, TYPE_NAME (t)); 201 if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t)) 202 { 203 pp_cxx_begin_template_argument_list (pp); 204 pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS 205 (CLASSTYPE_TI_ARGS (t))); 206 pp_cxx_end_template_argument_list (pp); 207 } 208 break; 209 210 case BIT_NOT_EXPR: 211 pp_cxx_complement (pp); 212 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0)); 213 break; 214 215 case TEMPLATE_TYPE_PARM: 216 case TEMPLATE_TEMPLATE_PARM: 217 if (TYPE_IDENTIFIER (t)) 218 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t)); 219 else 220 pp_cxx_canonical_template_parameter (pp, t); 221 break; 222 223 case TEMPLATE_PARM_INDEX: 224 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t)); 225 break; 226 227 case BOUND_TEMPLATE_TEMPLATE_PARM: 228 pp_cxx_cv_qualifier_seq (pp, t); 229 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t)); 230 pp_cxx_begin_template_argument_list (pp); 231 pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t)); 232 pp_cxx_end_template_argument_list (pp); 233 break; 234 235 default: 236 pp_unsupported_tree (pp, t); 237 break; 238 } 239 } 240 241 /* Pretty-print out the token sequence ":: template" in template codes 242 where it is needed to "inline declare" the (following) member as 243 a template. This situation arises when SCOPE of T is dependent 244 on template parameters. */ 245 246 static inline void 247 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t) 248 { 249 if (TREE_CODE (t) == TEMPLATE_ID_EXPR 250 && TYPE_P (scope) && dependent_type_p (scope)) 251 pp_cxx_ws_string (pp, "template"); 252 } 253 254 /* nested-name-specifier: 255 class-or-namespace-name :: nested-name-specifier(opt) 256 class-or-namespace-name :: template nested-name-specifier */ 257 258 static void 259 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t) 260 { 261 if (!SCOPE_FILE_SCOPE_P (t) && t != pp->enclosing_scope) 262 { 263 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); 264 pp_cxx_nested_name_specifier (pp, scope); 265 pp_cxx_template_keyword_if_needed (pp, scope, t); 266 pp_cxx_unqualified_id (pp, t); 267 pp_cxx_colon_colon (pp); 268 } 269 } 270 271 /* qualified-id: 272 nested-name-specifier template(opt) unqualified-id */ 273 274 static void 275 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t) 276 { 277 switch (TREE_CODE (t)) 278 { 279 /* A pointer-to-member is always qualified. */ 280 case PTRMEM_CST: 281 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t)); 282 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t)); 283 break; 284 285 /* In Standard C++, functions cannot possibly be used as 286 nested-name-specifiers. However, there are situations where 287 is "makes sense" to output the surrounding function name for the 288 purpose of emphasizing on the scope kind. Just printing the 289 function name might not be sufficient as it may be overloaded; so, 290 we decorate the function with its signature too. 291 FIXME: This is probably the wrong pretty-printing for conversion 292 functions and some function templates. */ 293 case OVERLOAD: 294 t = OVL_CURRENT (t); 295 case FUNCTION_DECL: 296 if (DECL_FUNCTION_MEMBER_P (t)) 297 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); 298 pp_cxx_unqualified_id 299 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t); 300 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t)); 301 break; 302 303 case OFFSET_REF: 304 case SCOPE_REF: 305 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0)); 306 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1)); 307 break; 308 309 default: 310 { 311 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t); 312 if (scope != pp->enclosing_scope) 313 { 314 pp_cxx_nested_name_specifier (pp, scope); 315 pp_cxx_template_keyword_if_needed (pp, scope, t); 316 } 317 pp_cxx_unqualified_id (pp, t); 318 } 319 break; 320 } 321 } 322 323 324 static void 325 pp_cxx_constant (cxx_pretty_printer *pp, tree t) 326 { 327 switch (TREE_CODE (t)) 328 { 329 case STRING_CST: 330 { 331 const bool in_parens = PAREN_STRING_LITERAL_P (t); 332 if (in_parens) 333 pp_cxx_left_paren (pp); 334 pp_c_constant (pp_c_base (pp), t); 335 if (in_parens) 336 pp_cxx_right_paren (pp); 337 } 338 break; 339 340 case INTEGER_CST: 341 if (NULLPTR_TYPE_P (TREE_TYPE (t))) 342 { 343 pp_string (pp, "nullptr"); 344 break; 345 } 346 /* else fall through. */ 347 348 default: 349 pp_c_constant (pp_c_base (pp), t); 350 break; 351 } 352 } 353 354 /* id-expression: 355 unqualified-id 356 qualified-id */ 357 358 static inline void 359 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t) 360 { 361 if (TREE_CODE (t) == OVERLOAD) 362 t = OVL_CURRENT (t); 363 if (DECL_P (t) && DECL_CONTEXT (t)) 364 pp_cxx_qualified_id (pp, t); 365 else 366 pp_cxx_unqualified_id (pp, t); 367 } 368 369 /* user-defined literal: 370 literal ud-suffix */ 371 372 void 373 pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t) 374 { 375 pp_cxx_constant (pp, USERDEF_LITERAL_VALUE (t)); 376 pp_cxx_id_expression (pp, USERDEF_LITERAL_SUFFIX_ID (t)); 377 } 378 379 380 /* primary-expression: 381 literal 382 this 383 :: identifier 384 :: operator-function-id 385 :: qualifier-id 386 ( expression ) 387 id-expression 388 389 GNU Extensions: 390 __builtin_va_arg ( assignment-expression , type-id ) 391 __builtin_offsetof ( type-id, offsetof-expression ) 392 393 __has_nothrow_assign ( type-id ) 394 __has_nothrow_constructor ( type-id ) 395 __has_nothrow_copy ( type-id ) 396 __has_trivial_assign ( type-id ) 397 __has_trivial_constructor ( type-id ) 398 __has_trivial_copy ( type-id ) 399 __has_trivial_destructor ( type-id ) 400 __has_virtual_destructor ( type-id ) 401 __is_abstract ( type-id ) 402 __is_base_of ( type-id , type-id ) 403 __is_class ( type-id ) 404 __is_convertible_to ( type-id , type-id ) 405 __is_empty ( type-id ) 406 __is_enum ( type-id ) 407 __is_literal_type ( type-id ) 408 __is_pod ( type-id ) 409 __is_polymorphic ( type-id ) 410 __is_std_layout ( type-id ) 411 __is_trivial ( type-id ) 412 __is_union ( type-id ) */ 413 414 static void 415 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t) 416 { 417 switch (TREE_CODE (t)) 418 { 419 case INTEGER_CST: 420 case REAL_CST: 421 case COMPLEX_CST: 422 case STRING_CST: 423 pp_cxx_constant (pp, t); 424 break; 425 426 case USERDEF_LITERAL: 427 pp_cxx_userdef_literal (pp, t); 428 break; 429 430 case BASELINK: 431 t = BASELINK_FUNCTIONS (t); 432 case VAR_DECL: 433 case PARM_DECL: 434 case FIELD_DECL: 435 case FUNCTION_DECL: 436 case OVERLOAD: 437 case CONST_DECL: 438 case TEMPLATE_DECL: 439 pp_cxx_id_expression (pp, t); 440 break; 441 442 case RESULT_DECL: 443 case TEMPLATE_TYPE_PARM: 444 case TEMPLATE_TEMPLATE_PARM: 445 case TEMPLATE_PARM_INDEX: 446 pp_cxx_unqualified_id (pp, t); 447 break; 448 449 case STMT_EXPR: 450 pp_cxx_left_paren (pp); 451 pp_cxx_statement (pp, STMT_EXPR_STMT (t)); 452 pp_cxx_right_paren (pp); 453 break; 454 455 case TRAIT_EXPR: 456 pp_cxx_trait_expression (pp, t); 457 break; 458 459 case VA_ARG_EXPR: 460 pp_cxx_va_arg_expression (pp, t); 461 break; 462 463 case OFFSETOF_EXPR: 464 pp_cxx_offsetof_expression (pp, t); 465 break; 466 467 default: 468 pp_c_primary_expression (pp_c_base (pp), t); 469 break; 470 } 471 } 472 473 /* postfix-expression: 474 primary-expression 475 postfix-expression [ expression ] 476 postfix-expression ( expression-list(opt) ) 477 simple-type-specifier ( expression-list(opt) ) 478 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) ) 479 typename ::(opt) nested-name-specifier template(opt) 480 template-id ( expression-list(opt) ) 481 postfix-expression . template(opt) ::(opt) id-expression 482 postfix-expression -> template(opt) ::(opt) id-expression 483 postfix-expression . pseudo-destructor-name 484 postfix-expression -> pseudo-destructor-name 485 postfix-expression ++ 486 postfix-expression -- 487 dynamic_cast < type-id > ( expression ) 488 static_cast < type-id > ( expression ) 489 reinterpret_cast < type-id > ( expression ) 490 const_cast < type-id > ( expression ) 491 typeid ( expression ) 492 typeid ( type-id ) */ 493 494 static void 495 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t) 496 { 497 enum tree_code code = TREE_CODE (t); 498 499 switch (code) 500 { 501 case AGGR_INIT_EXPR: 502 case CALL_EXPR: 503 { 504 tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t) 505 : CALL_EXPR_FN (t)); 506 tree saved_scope = pp->enclosing_scope; 507 bool skipfirst = false; 508 tree arg; 509 510 if (TREE_CODE (fun) == ADDR_EXPR) 511 fun = TREE_OPERAND (fun, 0); 512 513 /* In templates, where there is no way to tell whether a given 514 call uses an actual member function. So the parser builds 515 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until 516 instantiation time. */ 517 if (TREE_CODE (fun) != FUNCTION_DECL) 518 ; 519 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)) 520 { 521 tree object = (code == AGGR_INIT_EXPR 522 ? (AGGR_INIT_VIA_CTOR_P (t) 523 ? AGGR_INIT_EXPR_SLOT (t) 524 : AGGR_INIT_EXPR_ARG (t, 0)) 525 : CALL_EXPR_ARG (t, 0)); 526 527 while (TREE_CODE (object) == NOP_EXPR) 528 object = TREE_OPERAND (object, 0); 529 530 if (TREE_CODE (object) == ADDR_EXPR) 531 object = TREE_OPERAND (object, 0); 532 533 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE) 534 { 535 pp_cxx_postfix_expression (pp, object); 536 pp_cxx_dot (pp); 537 } 538 else 539 { 540 pp_cxx_postfix_expression (pp, object); 541 pp_cxx_arrow (pp); 542 } 543 skipfirst = true; 544 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object)); 545 } 546 547 pp_cxx_postfix_expression (pp, fun); 548 pp->enclosing_scope = saved_scope; 549 pp_cxx_left_paren (pp); 550 if (code == AGGR_INIT_EXPR) 551 { 552 aggr_init_expr_arg_iterator iter; 553 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t) 554 { 555 if (skipfirst) 556 skipfirst = false; 557 else 558 { 559 pp_cxx_expression (pp, arg); 560 if (more_aggr_init_expr_args_p (&iter)) 561 pp_cxx_separate_with (pp, ','); 562 } 563 } 564 } 565 else 566 { 567 call_expr_arg_iterator iter; 568 FOR_EACH_CALL_EXPR_ARG (arg, iter, t) 569 { 570 if (skipfirst) 571 skipfirst = false; 572 else 573 { 574 pp_cxx_expression (pp, arg); 575 if (more_call_expr_args_p (&iter)) 576 pp_cxx_separate_with (pp, ','); 577 } 578 } 579 } 580 pp_cxx_right_paren (pp); 581 } 582 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)) 583 { 584 pp_cxx_separate_with (pp, ','); 585 pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t)); 586 } 587 break; 588 589 case BASELINK: 590 case VAR_DECL: 591 case PARM_DECL: 592 case FIELD_DECL: 593 case FUNCTION_DECL: 594 case OVERLOAD: 595 case CONST_DECL: 596 case TEMPLATE_DECL: 597 case RESULT_DECL: 598 pp_cxx_primary_expression (pp, t); 599 break; 600 601 case DYNAMIC_CAST_EXPR: 602 case STATIC_CAST_EXPR: 603 case REINTERPRET_CAST_EXPR: 604 case CONST_CAST_EXPR: 605 if (code == DYNAMIC_CAST_EXPR) 606 pp_cxx_ws_string (pp, "dynamic_cast"); 607 else if (code == STATIC_CAST_EXPR) 608 pp_cxx_ws_string (pp, "static_cast"); 609 else if (code == REINTERPRET_CAST_EXPR) 610 pp_cxx_ws_string (pp, "reinterpret_cast"); 611 else 612 pp_cxx_ws_string (pp, "const_cast"); 613 pp_cxx_begin_template_argument_list (pp); 614 pp_cxx_type_id (pp, TREE_TYPE (t)); 615 pp_cxx_end_template_argument_list (pp); 616 pp_left_paren (pp); 617 pp_cxx_expression (pp, TREE_OPERAND (t, 0)); 618 pp_right_paren (pp); 619 break; 620 621 case EMPTY_CLASS_EXPR: 622 pp_cxx_type_id (pp, TREE_TYPE (t)); 623 pp_left_paren (pp); 624 pp_right_paren (pp); 625 break; 626 627 case TYPEID_EXPR: 628 pp_cxx_typeid_expression (pp, t); 629 break; 630 631 case PSEUDO_DTOR_EXPR: 632 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0)); 633 pp_cxx_dot (pp); 634 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1)); 635 pp_cxx_colon_colon (pp); 636 pp_complement (pp); 637 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2)); 638 break; 639 640 case ARROW_EXPR: 641 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0)); 642 pp_cxx_arrow (pp); 643 break; 644 645 default: 646 pp_c_postfix_expression (pp_c_base (pp), t); 647 break; 648 } 649 } 650 651 /* new-expression: 652 ::(opt) new new-placement(opt) new-type-id new-initializer(opt) 653 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt) 654 655 new-placement: 656 ( expression-list ) 657 658 new-type-id: 659 type-specifier-seq new-declarator(opt) 660 661 new-declarator: 662 ptr-operator new-declarator(opt) 663 direct-new-declarator 664 665 direct-new-declarator 666 [ expression ] 667 direct-new-declarator [ constant-expression ] 668 669 new-initializer: 670 ( expression-list(opt) ) */ 671 672 static void 673 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t) 674 { 675 enum tree_code code = TREE_CODE (t); 676 tree type = TREE_OPERAND (t, 1); 677 tree init = TREE_OPERAND (t, 2); 678 switch (code) 679 { 680 case NEW_EXPR: 681 case VEC_NEW_EXPR: 682 if (NEW_EXPR_USE_GLOBAL (t)) 683 pp_cxx_colon_colon (pp); 684 pp_cxx_ws_string (pp, "new"); 685 if (TREE_OPERAND (t, 0)) 686 { 687 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); 688 pp_space (pp); 689 } 690 if (TREE_CODE (type) == ARRAY_REF) 691 type = build_cplus_array_type 692 (TREE_OPERAND (type, 0), 693 build_index_type (fold_build2_loc (input_location, 694 MINUS_EXPR, integer_type_node, 695 TREE_OPERAND (type, 1), 696 integer_one_node))); 697 pp_cxx_type_id (pp, type); 698 if (init) 699 { 700 pp_left_paren (pp); 701 if (TREE_CODE (init) == TREE_LIST) 702 pp_c_expression_list (pp_c_base (pp), init); 703 else if (init == void_zero_node) 704 ; /* OK, empty initializer list. */ 705 else 706 pp_cxx_expression (pp, init); 707 pp_right_paren (pp); 708 } 709 break; 710 711 default: 712 pp_unsupported_tree (pp, t); 713 } 714 } 715 716 /* delete-expression: 717 ::(opt) delete cast-expression 718 ::(opt) delete [ ] cast-expression */ 719 720 static void 721 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t) 722 { 723 enum tree_code code = TREE_CODE (t); 724 switch (code) 725 { 726 case DELETE_EXPR: 727 case VEC_DELETE_EXPR: 728 if (DELETE_EXPR_USE_GLOBAL (t)) 729 pp_cxx_colon_colon (pp); 730 pp_cxx_ws_string (pp, "delete"); 731 pp_space (pp); 732 if (code == VEC_DELETE_EXPR 733 || DELETE_EXPR_USE_VEC (t)) 734 { 735 pp_left_bracket (pp); 736 pp_right_bracket (pp); 737 pp_space (pp); 738 } 739 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0)); 740 break; 741 742 default: 743 pp_unsupported_tree (pp, t); 744 } 745 } 746 747 /* unary-expression: 748 postfix-expression 749 ++ cast-expression 750 -- cast-expression 751 unary-operator cast-expression 752 sizeof unary-expression 753 sizeof ( type-id ) 754 sizeof ... ( identifier ) 755 new-expression 756 delete-expression 757 758 unary-operator: one of 759 * & + - ! 760 761 GNU extensions: 762 __alignof__ unary-expression 763 __alignof__ ( type-id ) */ 764 765 static void 766 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t) 767 { 768 enum tree_code code = TREE_CODE (t); 769 switch (code) 770 { 771 case NEW_EXPR: 772 case VEC_NEW_EXPR: 773 pp_cxx_new_expression (pp, t); 774 break; 775 776 case DELETE_EXPR: 777 case VEC_DELETE_EXPR: 778 pp_cxx_delete_expression (pp, t); 779 break; 780 781 case SIZEOF_EXPR: 782 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))) 783 { 784 pp_cxx_ws_string (pp, "sizeof"); 785 pp_cxx_ws_string (pp, "..."); 786 pp_cxx_whitespace (pp); 787 pp_cxx_left_paren (pp); 788 if (TYPE_P (TREE_OPERAND (t, 0))) 789 pp_cxx_type_id (pp, TREE_OPERAND (t, 0)); 790 else 791 pp_unary_expression (pp, TREE_OPERAND (t, 0)); 792 pp_cxx_right_paren (pp); 793 break; 794 } 795 /* Fall through */ 796 797 case ALIGNOF_EXPR: 798 pp_cxx_ws_string (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__"); 799 pp_cxx_whitespace (pp); 800 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t)) 801 { 802 pp_cxx_left_paren (pp); 803 pp_cxx_type_id (pp, TREE_TYPE (TREE_OPERAND (t, 0))); 804 pp_cxx_right_paren (pp); 805 } 806 else if (TYPE_P (TREE_OPERAND (t, 0))) 807 { 808 pp_cxx_left_paren (pp); 809 pp_cxx_type_id (pp, TREE_OPERAND (t, 0)); 810 pp_cxx_right_paren (pp); 811 } 812 else 813 pp_unary_expression (pp, TREE_OPERAND (t, 0)); 814 break; 815 816 case AT_ENCODE_EXPR: 817 pp_cxx_ws_string (pp, "@encode"); 818 pp_cxx_whitespace (pp); 819 pp_cxx_left_paren (pp); 820 pp_cxx_type_id (pp, TREE_OPERAND (t, 0)); 821 pp_cxx_right_paren (pp); 822 break; 823 824 case NOEXCEPT_EXPR: 825 pp_cxx_ws_string (pp, "noexcept"); 826 pp_cxx_whitespace (pp); 827 pp_cxx_left_paren (pp); 828 pp_cxx_expression (pp, TREE_OPERAND (t, 0)); 829 pp_cxx_right_paren (pp); 830 break; 831 832 case UNARY_PLUS_EXPR: 833 pp_plus (pp); 834 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0)); 835 break; 836 837 default: 838 pp_c_unary_expression (pp_c_base (pp), t); 839 break; 840 } 841 } 842 843 /* cast-expression: 844 unary-expression 845 ( type-id ) cast-expression */ 846 847 static void 848 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t) 849 { 850 switch (TREE_CODE (t)) 851 { 852 case CAST_EXPR: 853 case IMPLICIT_CONV_EXPR: 854 pp_cxx_type_id (pp, TREE_TYPE (t)); 855 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0)); 856 break; 857 858 default: 859 pp_c_cast_expression (pp_c_base (pp), t); 860 break; 861 } 862 } 863 864 /* pm-expression: 865 cast-expression 866 pm-expression .* cast-expression 867 pm-expression ->* cast-expression */ 868 869 static void 870 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t) 871 { 872 switch (TREE_CODE (t)) 873 { 874 /* Handle unfortunate OFFSET_REF overloading here. */ 875 case OFFSET_REF: 876 if (TYPE_P (TREE_OPERAND (t, 0))) 877 { 878 pp_cxx_qualified_id (pp, t); 879 break; 880 } 881 /* Else fall through. */ 882 case MEMBER_REF: 883 case DOTSTAR_EXPR: 884 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0)); 885 if (TREE_CODE (t) == MEMBER_REF) 886 pp_cxx_arrow (pp); 887 else 888 pp_cxx_dot (pp); 889 pp_star(pp); 890 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1)); 891 break; 892 893 894 default: 895 pp_cxx_cast_expression (pp, t); 896 break; 897 } 898 } 899 900 /* multiplicative-expression: 901 pm-expression 902 multiplicative-expression * pm-expression 903 multiplicative-expression / pm-expression 904 multiplicative-expression % pm-expression */ 905 906 static void 907 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e) 908 { 909 enum tree_code code = TREE_CODE (e); 910 switch (code) 911 { 912 case MULT_EXPR: 913 case TRUNC_DIV_EXPR: 914 case TRUNC_MOD_EXPR: 915 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0)); 916 pp_space (pp); 917 if (code == MULT_EXPR) 918 pp_star (pp); 919 else if (code == TRUNC_DIV_EXPR) 920 pp_slash (pp); 921 else 922 pp_modulo (pp); 923 pp_space (pp); 924 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1)); 925 break; 926 927 default: 928 pp_cxx_pm_expression (pp, e); 929 break; 930 } 931 } 932 933 /* conditional-expression: 934 logical-or-expression 935 logical-or-expression ? expression : assignment-expression */ 936 937 static void 938 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e) 939 { 940 if (TREE_CODE (e) == COND_EXPR) 941 { 942 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); 943 pp_space (pp); 944 pp_question (pp); 945 pp_space (pp); 946 pp_cxx_expression (pp, TREE_OPERAND (e, 1)); 947 pp_space (pp); 948 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); 949 } 950 else 951 pp_c_logical_or_expression (pp_c_base (pp), e); 952 } 953 954 /* Pretty-print a compound assignment operator token as indicated by T. */ 955 956 static void 957 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t) 958 { 959 const char *op; 960 961 switch (TREE_CODE (t)) 962 { 963 case NOP_EXPR: 964 op = "="; 965 break; 966 967 case PLUS_EXPR: 968 op = "+="; 969 break; 970 971 case MINUS_EXPR: 972 op = "-="; 973 break; 974 975 case TRUNC_DIV_EXPR: 976 op = "/="; 977 break; 978 979 case TRUNC_MOD_EXPR: 980 op = "%="; 981 break; 982 983 default: 984 op = tree_code_name[TREE_CODE (t)]; 985 break; 986 } 987 988 pp_cxx_ws_string (pp, op); 989 } 990 991 992 /* assignment-expression: 993 conditional-expression 994 logical-or-expression assignment-operator assignment-expression 995 throw-expression 996 997 throw-expression: 998 throw assignment-expression(opt) 999 1000 assignment-operator: one of 1001 = *= /= %= += -= >>= <<= &= ^= |= */ 1002 1003 static void 1004 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e) 1005 { 1006 switch (TREE_CODE (e)) 1007 { 1008 case MODIFY_EXPR: 1009 case INIT_EXPR: 1010 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); 1011 pp_space (pp); 1012 pp_equal (pp); 1013 pp_space (pp); 1014 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1)); 1015 break; 1016 1017 case THROW_EXPR: 1018 pp_cxx_ws_string (pp, "throw"); 1019 if (TREE_OPERAND (e, 0)) 1020 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0)); 1021 break; 1022 1023 case MODOP_EXPR: 1024 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0)); 1025 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1)); 1026 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2)); 1027 break; 1028 1029 default: 1030 pp_cxx_conditional_expression (pp, e); 1031 break; 1032 } 1033 } 1034 1035 static void 1036 pp_cxx_expression (cxx_pretty_printer *pp, tree t) 1037 { 1038 switch (TREE_CODE (t)) 1039 { 1040 case STRING_CST: 1041 case INTEGER_CST: 1042 case REAL_CST: 1043 case COMPLEX_CST: 1044 pp_cxx_constant (pp, t); 1045 break; 1046 1047 case USERDEF_LITERAL: 1048 pp_cxx_userdef_literal (pp, t); 1049 break; 1050 1051 case RESULT_DECL: 1052 pp_cxx_unqualified_id (pp, t); 1053 break; 1054 1055 #if 0 1056 case OFFSET_REF: 1057 #endif 1058 case SCOPE_REF: 1059 case PTRMEM_CST: 1060 pp_cxx_qualified_id (pp, t); 1061 break; 1062 1063 case OVERLOAD: 1064 t = OVL_CURRENT (t); 1065 case VAR_DECL: 1066 case PARM_DECL: 1067 case FIELD_DECL: 1068 case CONST_DECL: 1069 case FUNCTION_DECL: 1070 case BASELINK: 1071 case TEMPLATE_DECL: 1072 case TEMPLATE_TYPE_PARM: 1073 case TEMPLATE_PARM_INDEX: 1074 case TEMPLATE_TEMPLATE_PARM: 1075 case STMT_EXPR: 1076 pp_cxx_primary_expression (pp, t); 1077 break; 1078 1079 case CALL_EXPR: 1080 case DYNAMIC_CAST_EXPR: 1081 case STATIC_CAST_EXPR: 1082 case REINTERPRET_CAST_EXPR: 1083 case CONST_CAST_EXPR: 1084 #if 0 1085 case MEMBER_REF: 1086 #endif 1087 case EMPTY_CLASS_EXPR: 1088 case TYPEID_EXPR: 1089 case PSEUDO_DTOR_EXPR: 1090 case AGGR_INIT_EXPR: 1091 case ARROW_EXPR: 1092 pp_cxx_postfix_expression (pp, t); 1093 break; 1094 1095 case NEW_EXPR: 1096 case VEC_NEW_EXPR: 1097 pp_cxx_new_expression (pp, t); 1098 break; 1099 1100 case DELETE_EXPR: 1101 case VEC_DELETE_EXPR: 1102 pp_cxx_delete_expression (pp, t); 1103 break; 1104 1105 case SIZEOF_EXPR: 1106 case ALIGNOF_EXPR: 1107 case NOEXCEPT_EXPR: 1108 pp_cxx_unary_expression (pp, t); 1109 break; 1110 1111 case CAST_EXPR: 1112 case IMPLICIT_CONV_EXPR: 1113 pp_cxx_cast_expression (pp, t); 1114 break; 1115 1116 case OFFSET_REF: 1117 case MEMBER_REF: 1118 case DOTSTAR_EXPR: 1119 pp_cxx_pm_expression (pp, t); 1120 break; 1121 1122 case MULT_EXPR: 1123 case TRUNC_DIV_EXPR: 1124 case TRUNC_MOD_EXPR: 1125 pp_cxx_multiplicative_expression (pp, t); 1126 break; 1127 1128 case COND_EXPR: 1129 pp_cxx_conditional_expression (pp, t); 1130 break; 1131 1132 case MODIFY_EXPR: 1133 case INIT_EXPR: 1134 case THROW_EXPR: 1135 case MODOP_EXPR: 1136 pp_cxx_assignment_expression (pp, t); 1137 break; 1138 1139 case NON_DEPENDENT_EXPR: 1140 case MUST_NOT_THROW_EXPR: 1141 pp_cxx_expression (pp, TREE_OPERAND (t, 0)); 1142 break; 1143 1144 case EXPR_PACK_EXPANSION: 1145 pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t)); 1146 pp_cxx_ws_string (pp, "..."); 1147 break; 1148 1149 case TEMPLATE_ID_EXPR: 1150 pp_cxx_template_id (pp, t); 1151 break; 1152 1153 case NONTYPE_ARGUMENT_PACK: 1154 { 1155 tree args = ARGUMENT_PACK_ARGS (t); 1156 int i, len = TREE_VEC_LENGTH (args); 1157 for (i = 0; i < len; ++i) 1158 { 1159 if (i > 0) 1160 pp_cxx_separate_with (pp, ','); 1161 pp_cxx_expression (pp, TREE_VEC_ELT (args, i)); 1162 } 1163 } 1164 break; 1165 1166 case LAMBDA_EXPR: 1167 pp_cxx_ws_string (pp, "<lambda>"); 1168 break; 1169 1170 default: 1171 pp_c_expression (pp_c_base (pp), t); 1172 break; 1173 } 1174 } 1175 1176 1177 /* Declarations. */ 1178 1179 /* function-specifier: 1180 inline 1181 virtual 1182 explicit */ 1183 1184 static void 1185 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t) 1186 { 1187 switch (TREE_CODE (t)) 1188 { 1189 case FUNCTION_DECL: 1190 if (DECL_VIRTUAL_P (t)) 1191 pp_cxx_ws_string (pp, "virtual"); 1192 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t)) 1193 pp_cxx_ws_string (pp, "explicit"); 1194 else 1195 pp_c_function_specifier (pp_c_base (pp), t); 1196 1197 default: 1198 break; 1199 } 1200 } 1201 1202 /* decl-specifier-seq: 1203 decl-specifier-seq(opt) decl-specifier 1204 1205 decl-specifier: 1206 storage-class-specifier 1207 type-specifier 1208 function-specifier 1209 friend 1210 typedef */ 1211 1212 static void 1213 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t) 1214 { 1215 switch (TREE_CODE (t)) 1216 { 1217 case VAR_DECL: 1218 case PARM_DECL: 1219 case CONST_DECL: 1220 case FIELD_DECL: 1221 pp_cxx_storage_class_specifier (pp, t); 1222 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); 1223 break; 1224 1225 case TYPE_DECL: 1226 pp_cxx_ws_string (pp, "typedef"); 1227 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t)); 1228 break; 1229 1230 case FUNCTION_DECL: 1231 /* Constructors don't have return types. And conversion functions 1232 do not have a type-specifier in their return types. */ 1233 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t)) 1234 pp_cxx_function_specifier (pp, t); 1235 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) 1236 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t))); 1237 else 1238 default: 1239 pp_c_declaration_specifiers (pp_c_base (pp), t); 1240 break; 1241 } 1242 } 1243 1244 /* simple-type-specifier: 1245 ::(opt) nested-name-specifier(opt) type-name 1246 ::(opt) nested-name-specifier(opt) template(opt) template-id 1247 char 1248 wchar_t 1249 bool 1250 short 1251 int 1252 long 1253 signed 1254 unsigned 1255 float 1256 double 1257 void */ 1258 1259 static void 1260 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t) 1261 { 1262 switch (TREE_CODE (t)) 1263 { 1264 case RECORD_TYPE: 1265 case UNION_TYPE: 1266 case ENUMERAL_TYPE: 1267 pp_cxx_qualified_id (pp, t); 1268 break; 1269 1270 case TEMPLATE_TYPE_PARM: 1271 case TEMPLATE_TEMPLATE_PARM: 1272 case TEMPLATE_PARM_INDEX: 1273 case BOUND_TEMPLATE_TEMPLATE_PARM: 1274 pp_cxx_unqualified_id (pp, t); 1275 break; 1276 1277 case TYPENAME_TYPE: 1278 pp_cxx_ws_string (pp, "typename"); 1279 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t)); 1280 pp_cxx_unqualified_id (pp, TYPE_NAME (t)); 1281 break; 1282 1283 default: 1284 pp_c_type_specifier (pp_c_base (pp), t); 1285 break; 1286 } 1287 } 1288 1289 /* type-specifier-seq: 1290 type-specifier type-specifier-seq(opt) 1291 1292 type-specifier: 1293 simple-type-specifier 1294 class-specifier 1295 enum-specifier 1296 elaborated-type-specifier 1297 cv-qualifier */ 1298 1299 static void 1300 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t) 1301 { 1302 switch (TREE_CODE (t)) 1303 { 1304 case TEMPLATE_DECL: 1305 case TEMPLATE_TYPE_PARM: 1306 case TEMPLATE_TEMPLATE_PARM: 1307 case TYPE_DECL: 1308 case BOUND_TEMPLATE_TEMPLATE_PARM: 1309 pp_cxx_cv_qualifier_seq (pp, t); 1310 pp_cxx_simple_type_specifier (pp, t); 1311 break; 1312 1313 case METHOD_TYPE: 1314 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t)); 1315 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); 1316 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t)); 1317 break; 1318 1319 case DECLTYPE_TYPE: 1320 pp_cxx_ws_string (pp, "decltype"); 1321 pp_cxx_left_paren (pp); 1322 pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t)); 1323 pp_cxx_right_paren (pp); 1324 break; 1325 1326 case RECORD_TYPE: 1327 if (TYPE_PTRMEMFUNC_P (t)) 1328 { 1329 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t); 1330 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm))); 1331 pp_cxx_whitespace (pp); 1332 pp_cxx_ptr_operator (pp, t); 1333 break; 1334 } 1335 /* else fall through */ 1336 1337 default: 1338 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t))) 1339 pp_c_specifier_qualifier_list (pp_c_base (pp), t); 1340 } 1341 } 1342 1343 /* ptr-operator: 1344 * cv-qualifier-seq(opt) 1345 & 1346 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */ 1347 1348 static void 1349 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t) 1350 { 1351 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL) 1352 t = TREE_TYPE (t); 1353 switch (TREE_CODE (t)) 1354 { 1355 case REFERENCE_TYPE: 1356 case POINTER_TYPE: 1357 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t))) 1358 pp_cxx_ptr_operator (pp, TREE_TYPE (t)); 1359 pp_c_attributes_display (pp_c_base (pp), 1360 TYPE_ATTRIBUTES (TREE_TYPE (t))); 1361 if (TREE_CODE (t) == POINTER_TYPE) 1362 { 1363 pp_star (pp); 1364 pp_cxx_cv_qualifier_seq (pp, t); 1365 } 1366 else 1367 pp_ampersand (pp); 1368 break; 1369 1370 case RECORD_TYPE: 1371 if (TYPE_PTRMEMFUNC_P (t)) 1372 { 1373 pp_cxx_left_paren (pp); 1374 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t)); 1375 pp_star (pp); 1376 break; 1377 } 1378 case OFFSET_TYPE: 1379 if (TYPE_PTRMEM_P (t)) 1380 { 1381 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) 1382 pp_cxx_left_paren (pp); 1383 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t)); 1384 pp_star (pp); 1385 pp_cxx_cv_qualifier_seq (pp, t); 1386 break; 1387 } 1388 /* else fall through. */ 1389 1390 default: 1391 pp_unsupported_tree (pp, t); 1392 break; 1393 } 1394 } 1395 1396 static inline tree 1397 pp_cxx_implicit_parameter_type (tree mf) 1398 { 1399 return class_of_this_parm (TREE_TYPE (mf)); 1400 } 1401 1402 /* 1403 parameter-declaration: 1404 decl-specifier-seq declarator 1405 decl-specifier-seq declarator = assignment-expression 1406 decl-specifier-seq abstract-declarator(opt) 1407 decl-specifier-seq abstract-declarator(opt) assignment-expression */ 1408 1409 static inline void 1410 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t) 1411 { 1412 pp_cxx_decl_specifier_seq (pp, t); 1413 if (TYPE_P (t)) 1414 pp_cxx_abstract_declarator (pp, t); 1415 else 1416 pp_cxx_declarator (pp, t); 1417 } 1418 1419 /* parameter-declaration-clause: 1420 parameter-declaration-list(opt) ...(opt) 1421 parameter-declaration-list , ... 1422 1423 parameter-declaration-list: 1424 parameter-declaration 1425 parameter-declaration-list , parameter-declaration */ 1426 1427 static void 1428 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t) 1429 { 1430 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t); 1431 tree types = 1432 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t); 1433 const bool abstract = args == NULL 1434 || pp_c_base (pp)->flags & pp_c_flag_abstract; 1435 bool first = true; 1436 1437 /* Skip artificial parameter for nonstatic member functions. */ 1438 if (TREE_CODE (t) == METHOD_TYPE) 1439 types = TREE_CHAIN (types); 1440 1441 pp_cxx_left_paren (pp); 1442 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types)) 1443 { 1444 if (!first) 1445 pp_cxx_separate_with (pp, ','); 1446 first = false; 1447 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args); 1448 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument) 1449 { 1450 pp_cxx_whitespace (pp); 1451 pp_equal (pp); 1452 pp_cxx_whitespace (pp); 1453 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types)); 1454 } 1455 } 1456 pp_cxx_right_paren (pp); 1457 } 1458 1459 /* exception-specification: 1460 throw ( type-id-list(opt) ) 1461 1462 type-id-list 1463 type-id 1464 type-id-list , type-id */ 1465 1466 static void 1467 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t) 1468 { 1469 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t); 1470 bool need_comma = false; 1471 1472 if (ex_spec == NULL) 1473 return; 1474 if (TREE_PURPOSE (ex_spec)) 1475 { 1476 pp_cxx_ws_string (pp, "noexcept"); 1477 pp_cxx_whitespace (pp); 1478 pp_cxx_left_paren (pp); 1479 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec)) 1480 pp_cxx_ws_string (pp, "<uninstantiated>"); 1481 else 1482 pp_cxx_expression (pp, TREE_PURPOSE (ex_spec)); 1483 pp_cxx_right_paren (pp); 1484 return; 1485 } 1486 pp_cxx_ws_string (pp, "throw"); 1487 pp_cxx_left_paren (pp); 1488 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec)) 1489 { 1490 tree type = TREE_VALUE (ex_spec); 1491 tree argpack = NULL_TREE; 1492 int i, len = 1; 1493 1494 if (ARGUMENT_PACK_P (type)) 1495 { 1496 argpack = ARGUMENT_PACK_ARGS (type); 1497 len = TREE_VEC_LENGTH (argpack); 1498 } 1499 1500 for (i = 0; i < len; ++i) 1501 { 1502 if (argpack) 1503 type = TREE_VEC_ELT (argpack, i); 1504 1505 if (need_comma) 1506 pp_cxx_separate_with (pp, ','); 1507 else 1508 need_comma = true; 1509 1510 pp_cxx_type_id (pp, type); 1511 } 1512 } 1513 pp_cxx_right_paren (pp); 1514 } 1515 1516 /* direct-declarator: 1517 declarator-id 1518 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt) 1519 exception-specification(opt) 1520 direct-declaration [ constant-expression(opt) ] 1521 ( declarator ) */ 1522 1523 static void 1524 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t) 1525 { 1526 switch (TREE_CODE (t)) 1527 { 1528 case VAR_DECL: 1529 case PARM_DECL: 1530 case CONST_DECL: 1531 case FIELD_DECL: 1532 if (DECL_NAME (t)) 1533 { 1534 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t)); 1535 1536 if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t)) 1537 || template_parameter_pack_p (t)) 1538 /* A function parameter pack or non-type template 1539 parameter pack. */ 1540 pp_cxx_ws_string (pp, "..."); 1541 1542 pp_cxx_id_expression (pp, DECL_NAME (t)); 1543 } 1544 pp_cxx_abstract_declarator (pp, TREE_TYPE (t)); 1545 break; 1546 1547 case FUNCTION_DECL: 1548 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t))); 1549 pp_cxx_id_expression (pp, t); 1550 pp_cxx_parameter_declaration_clause (pp, t); 1551 1552 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t)) 1553 { 1554 pp_base (pp)->padding = pp_before; 1555 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t)); 1556 } 1557 1558 pp_cxx_exception_specification (pp, TREE_TYPE (t)); 1559 break; 1560 1561 case TYPENAME_TYPE: 1562 case TEMPLATE_DECL: 1563 case TEMPLATE_TYPE_PARM: 1564 case TEMPLATE_PARM_INDEX: 1565 case TEMPLATE_TEMPLATE_PARM: 1566 break; 1567 1568 default: 1569 pp_c_direct_declarator (pp_c_base (pp), t); 1570 break; 1571 } 1572 } 1573 1574 /* declarator: 1575 direct-declarator 1576 ptr-operator declarator */ 1577 1578 static void 1579 pp_cxx_declarator (cxx_pretty_printer *pp, tree t) 1580 { 1581 pp_cxx_direct_declarator (pp, t); 1582 } 1583 1584 /* ctor-initializer: 1585 : mem-initializer-list 1586 1587 mem-initializer-list: 1588 mem-initializer 1589 mem-initializer , mem-initializer-list 1590 1591 mem-initializer: 1592 mem-initializer-id ( expression-list(opt) ) 1593 1594 mem-initializer-id: 1595 ::(opt) nested-name-specifier(opt) class-name 1596 identifier */ 1597 1598 static void 1599 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t) 1600 { 1601 t = TREE_OPERAND (t, 0); 1602 pp_cxx_whitespace (pp); 1603 pp_colon (pp); 1604 pp_cxx_whitespace (pp); 1605 for (; t; t = TREE_CHAIN (t)) 1606 { 1607 tree purpose = TREE_PURPOSE (t); 1608 bool is_pack = PACK_EXPANSION_P (purpose); 1609 1610 if (is_pack) 1611 pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose)); 1612 else 1613 pp_cxx_primary_expression (pp, purpose); 1614 pp_cxx_call_argument_list (pp, TREE_VALUE (t)); 1615 if (is_pack) 1616 pp_cxx_ws_string (pp, "..."); 1617 if (TREE_CHAIN (t)) 1618 pp_cxx_separate_with (pp, ','); 1619 } 1620 } 1621 1622 /* function-definition: 1623 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body 1624 decl-specifier-seq(opt) declarator function-try-block */ 1625 1626 static void 1627 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t) 1628 { 1629 tree saved_scope = pp->enclosing_scope; 1630 pp_cxx_decl_specifier_seq (pp, t); 1631 pp_cxx_declarator (pp, t); 1632 pp_needs_newline (pp) = true; 1633 pp->enclosing_scope = DECL_CONTEXT (t); 1634 if (DECL_SAVED_TREE (t)) 1635 pp_cxx_statement (pp, DECL_SAVED_TREE (t)); 1636 else 1637 pp_cxx_semicolon (pp); 1638 pp_newline_and_flush (pp); 1639 pp->enclosing_scope = saved_scope; 1640 } 1641 1642 /* abstract-declarator: 1643 ptr-operator abstract-declarator(opt) 1644 direct-abstract-declarator */ 1645 1646 static void 1647 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t) 1648 { 1649 if (TYPE_PTRMEM_P (t)) 1650 pp_cxx_right_paren (pp); 1651 else if (POINTER_TYPE_P (t)) 1652 { 1653 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE 1654 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE) 1655 pp_cxx_right_paren (pp); 1656 t = TREE_TYPE (t); 1657 } 1658 pp_cxx_direct_abstract_declarator (pp, t); 1659 } 1660 1661 /* direct-abstract-declarator: 1662 direct-abstract-declarator(opt) ( parameter-declaration-clause ) 1663 cv-qualifier-seq(opt) exception-specification(opt) 1664 direct-abstract-declarator(opt) [ constant-expression(opt) ] 1665 ( abstract-declarator ) */ 1666 1667 static void 1668 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t) 1669 { 1670 switch (TREE_CODE (t)) 1671 { 1672 case REFERENCE_TYPE: 1673 pp_cxx_abstract_declarator (pp, t); 1674 break; 1675 1676 case RECORD_TYPE: 1677 if (TYPE_PTRMEMFUNC_P (t)) 1678 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t)); 1679 break; 1680 1681 case METHOD_TYPE: 1682 case FUNCTION_TYPE: 1683 pp_cxx_parameter_declaration_clause (pp, t); 1684 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t)); 1685 if (TREE_CODE (t) == METHOD_TYPE) 1686 { 1687 pp_base (pp)->padding = pp_before; 1688 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (t)); 1689 } 1690 pp_cxx_exception_specification (pp, t); 1691 break; 1692 1693 case TYPENAME_TYPE: 1694 case TEMPLATE_TYPE_PARM: 1695 case TEMPLATE_TEMPLATE_PARM: 1696 case BOUND_TEMPLATE_TEMPLATE_PARM: 1697 case UNBOUND_CLASS_TEMPLATE: 1698 break; 1699 1700 default: 1701 pp_c_direct_abstract_declarator (pp_c_base (pp), t); 1702 break; 1703 } 1704 } 1705 1706 /* type-id: 1707 type-specifier-seq abstract-declarator(opt) */ 1708 1709 static void 1710 pp_cxx_type_id (cxx_pretty_printer *pp, tree t) 1711 { 1712 pp_flags saved_flags = pp_c_base (pp)->flags; 1713 pp_c_base (pp)->flags |= pp_c_flag_abstract; 1714 1715 switch (TREE_CODE (t)) 1716 { 1717 case TYPE_DECL: 1718 case UNION_TYPE: 1719 case RECORD_TYPE: 1720 case ENUMERAL_TYPE: 1721 case TYPENAME_TYPE: 1722 case BOUND_TEMPLATE_TEMPLATE_PARM: 1723 case UNBOUND_CLASS_TEMPLATE: 1724 case TEMPLATE_TEMPLATE_PARM: 1725 case TEMPLATE_TYPE_PARM: 1726 case TEMPLATE_PARM_INDEX: 1727 case TEMPLATE_DECL: 1728 case TYPEOF_TYPE: 1729 case UNDERLYING_TYPE: 1730 case DECLTYPE_TYPE: 1731 case TEMPLATE_ID_EXPR: 1732 pp_cxx_type_specifier_seq (pp, t); 1733 break; 1734 1735 case TYPE_PACK_EXPANSION: 1736 pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t)); 1737 pp_cxx_ws_string (pp, "..."); 1738 break; 1739 1740 default: 1741 pp_c_type_id (pp_c_base (pp), t); 1742 break; 1743 } 1744 1745 pp_c_base (pp)->flags = saved_flags; 1746 } 1747 1748 /* template-argument-list: 1749 template-argument ...(opt) 1750 template-argument-list, template-argument ...(opt) 1751 1752 template-argument: 1753 assignment-expression 1754 type-id 1755 template-name */ 1756 1757 static void 1758 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t) 1759 { 1760 int i; 1761 bool need_comma = false; 1762 1763 if (t == NULL) 1764 return; 1765 for (i = 0; i < TREE_VEC_LENGTH (t); ++i) 1766 { 1767 tree arg = TREE_VEC_ELT (t, i); 1768 tree argpack = NULL_TREE; 1769 int idx, len = 1; 1770 1771 if (ARGUMENT_PACK_P (arg)) 1772 { 1773 argpack = ARGUMENT_PACK_ARGS (arg); 1774 len = TREE_VEC_LENGTH (argpack); 1775 } 1776 1777 for (idx = 0; idx < len; idx++) 1778 { 1779 if (argpack) 1780 arg = TREE_VEC_ELT (argpack, idx); 1781 1782 if (need_comma) 1783 pp_cxx_separate_with (pp, ','); 1784 else 1785 need_comma = true; 1786 1787 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL 1788 && TYPE_P (DECL_TEMPLATE_RESULT (arg)))) 1789 pp_cxx_type_id (pp, arg); 1790 else 1791 pp_cxx_expression (pp, arg); 1792 } 1793 } 1794 } 1795 1796 1797 static void 1798 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t) 1799 { 1800 t = DECL_EXPR_DECL (t); 1801 pp_cxx_type_specifier_seq (pp, t); 1802 if (TYPE_P (t)) 1803 pp_cxx_abstract_declarator (pp, t); 1804 else 1805 pp_cxx_declarator (pp, t); 1806 } 1807 1808 /* Statements. */ 1809 1810 static void 1811 pp_cxx_statement (cxx_pretty_printer *pp, tree t) 1812 { 1813 switch (TREE_CODE (t)) 1814 { 1815 case CTOR_INITIALIZER: 1816 pp_cxx_ctor_initializer (pp, t); 1817 break; 1818 1819 case USING_STMT: 1820 pp_cxx_ws_string (pp, "using"); 1821 pp_cxx_ws_string (pp, "namespace"); 1822 if (DECL_CONTEXT (t)) 1823 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); 1824 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t)); 1825 break; 1826 1827 case USING_DECL: 1828 pp_cxx_ws_string (pp, "using"); 1829 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t)); 1830 pp_cxx_unqualified_id (pp, DECL_NAME (t)); 1831 break; 1832 1833 case EH_SPEC_BLOCK: 1834 break; 1835 1836 /* try-block: 1837 try compound-statement handler-seq */ 1838 case TRY_BLOCK: 1839 pp_maybe_newline_and_indent (pp, 0); 1840 pp_cxx_ws_string (pp, "try"); 1841 pp_newline_and_indent (pp, 3); 1842 pp_cxx_statement (pp, TRY_STMTS (t)); 1843 pp_newline_and_indent (pp, -3); 1844 if (CLEANUP_P (t)) 1845 ; 1846 else 1847 pp_cxx_statement (pp, TRY_HANDLERS (t)); 1848 break; 1849 1850 /* 1851 handler-seq: 1852 handler handler-seq(opt) 1853 1854 handler: 1855 catch ( exception-declaration ) compound-statement 1856 1857 exception-declaration: 1858 type-specifier-seq declarator 1859 type-specifier-seq abstract-declarator 1860 ... */ 1861 case HANDLER: 1862 pp_cxx_ws_string (pp, "catch"); 1863 pp_cxx_left_paren (pp); 1864 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t)); 1865 pp_cxx_right_paren (pp); 1866 pp_indentation (pp) += 3; 1867 pp_needs_newline (pp) = true; 1868 pp_cxx_statement (pp, HANDLER_BODY (t)); 1869 pp_indentation (pp) -= 3; 1870 pp_needs_newline (pp) = true; 1871 break; 1872 1873 /* selection-statement: 1874 if ( expression ) statement 1875 if ( expression ) statement else statement */ 1876 case IF_STMT: 1877 pp_cxx_ws_string (pp, "if"); 1878 pp_cxx_whitespace (pp); 1879 pp_cxx_left_paren (pp); 1880 pp_cxx_expression (pp, IF_COND (t)); 1881 pp_cxx_right_paren (pp); 1882 pp_newline_and_indent (pp, 2); 1883 pp_cxx_statement (pp, THEN_CLAUSE (t)); 1884 pp_newline_and_indent (pp, -2); 1885 if (ELSE_CLAUSE (t)) 1886 { 1887 tree else_clause = ELSE_CLAUSE (t); 1888 pp_cxx_ws_string (pp, "else"); 1889 if (TREE_CODE (else_clause) == IF_STMT) 1890 pp_cxx_whitespace (pp); 1891 else 1892 pp_newline_and_indent (pp, 2); 1893 pp_cxx_statement (pp, else_clause); 1894 if (TREE_CODE (else_clause) != IF_STMT) 1895 pp_newline_and_indent (pp, -2); 1896 } 1897 break; 1898 1899 case SWITCH_STMT: 1900 pp_cxx_ws_string (pp, "switch"); 1901 pp_space (pp); 1902 pp_cxx_left_paren (pp); 1903 pp_cxx_expression (pp, SWITCH_STMT_COND (t)); 1904 pp_cxx_right_paren (pp); 1905 pp_indentation (pp) += 3; 1906 pp_needs_newline (pp) = true; 1907 pp_cxx_statement (pp, SWITCH_STMT_BODY (t)); 1908 pp_newline_and_indent (pp, -3); 1909 break; 1910 1911 /* iteration-statement: 1912 while ( expression ) statement 1913 do statement while ( expression ) ; 1914 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement 1915 for ( declaration expression(opt) ; expression(opt) ) statement */ 1916 case WHILE_STMT: 1917 pp_cxx_ws_string (pp, "while"); 1918 pp_space (pp); 1919 pp_cxx_left_paren (pp); 1920 pp_cxx_expression (pp, WHILE_COND (t)); 1921 pp_cxx_right_paren (pp); 1922 pp_newline_and_indent (pp, 3); 1923 pp_cxx_statement (pp, WHILE_BODY (t)); 1924 pp_indentation (pp) -= 3; 1925 pp_needs_newline (pp) = true; 1926 break; 1927 1928 case DO_STMT: 1929 pp_cxx_ws_string (pp, "do"); 1930 pp_newline_and_indent (pp, 3); 1931 pp_cxx_statement (pp, DO_BODY (t)); 1932 pp_newline_and_indent (pp, -3); 1933 pp_cxx_ws_string (pp, "while"); 1934 pp_space (pp); 1935 pp_cxx_left_paren (pp); 1936 pp_cxx_expression (pp, DO_COND (t)); 1937 pp_cxx_right_paren (pp); 1938 pp_cxx_semicolon (pp); 1939 pp_needs_newline (pp) = true; 1940 break; 1941 1942 case FOR_STMT: 1943 pp_cxx_ws_string (pp, "for"); 1944 pp_space (pp); 1945 pp_cxx_left_paren (pp); 1946 if (FOR_INIT_STMT (t)) 1947 pp_cxx_statement (pp, FOR_INIT_STMT (t)); 1948 else 1949 pp_cxx_semicolon (pp); 1950 pp_needs_newline (pp) = false; 1951 pp_cxx_whitespace (pp); 1952 if (FOR_COND (t)) 1953 pp_cxx_expression (pp, FOR_COND (t)); 1954 pp_cxx_semicolon (pp); 1955 pp_needs_newline (pp) = false; 1956 pp_cxx_whitespace (pp); 1957 if (FOR_EXPR (t)) 1958 pp_cxx_expression (pp, FOR_EXPR (t)); 1959 pp_cxx_right_paren (pp); 1960 pp_newline_and_indent (pp, 3); 1961 pp_cxx_statement (pp, FOR_BODY (t)); 1962 pp_indentation (pp) -= 3; 1963 pp_needs_newline (pp) = true; 1964 break; 1965 1966 case RANGE_FOR_STMT: 1967 pp_cxx_ws_string (pp, "for"); 1968 pp_space (pp); 1969 pp_cxx_left_paren (pp); 1970 pp_cxx_statement (pp, RANGE_FOR_DECL (t)); 1971 pp_space (pp); 1972 pp_needs_newline (pp) = false; 1973 pp_colon (pp); 1974 pp_space (pp); 1975 pp_cxx_statement (pp, RANGE_FOR_EXPR (t)); 1976 pp_cxx_right_paren (pp); 1977 pp_newline_and_indent (pp, 3); 1978 pp_cxx_statement (pp, FOR_BODY (t)); 1979 pp_indentation (pp) -= 3; 1980 pp_needs_newline (pp) = true; 1981 break; 1982 1983 /* jump-statement: 1984 goto identifier; 1985 continue ; 1986 return expression(opt) ; */ 1987 case BREAK_STMT: 1988 case CONTINUE_STMT: 1989 pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue"); 1990 pp_cxx_semicolon (pp); 1991 pp_needs_newline (pp) = true; 1992 break; 1993 1994 /* expression-statement: 1995 expression(opt) ; */ 1996 case EXPR_STMT: 1997 pp_cxx_expression (pp, EXPR_STMT_EXPR (t)); 1998 pp_cxx_semicolon (pp); 1999 pp_needs_newline (pp) = true; 2000 break; 2001 2002 case CLEANUP_STMT: 2003 pp_cxx_ws_string (pp, "try"); 2004 pp_newline_and_indent (pp, 2); 2005 pp_cxx_statement (pp, CLEANUP_BODY (t)); 2006 pp_newline_and_indent (pp, -2); 2007 pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally"); 2008 pp_newline_and_indent (pp, 2); 2009 pp_cxx_statement (pp, CLEANUP_EXPR (t)); 2010 pp_newline_and_indent (pp, -2); 2011 break; 2012 2013 case STATIC_ASSERT: 2014 pp_cxx_declaration (pp, t); 2015 break; 2016 2017 default: 2018 pp_c_statement (pp_c_base (pp), t); 2019 break; 2020 } 2021 } 2022 2023 /* original-namespace-definition: 2024 namespace identifier { namespace-body } 2025 2026 As an edge case, we also handle unnamed namespace definition here. */ 2027 2028 static void 2029 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t) 2030 { 2031 pp_cxx_ws_string (pp, "namespace"); 2032 if (DECL_CONTEXT (t)) 2033 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); 2034 if (DECL_NAME (t)) 2035 pp_cxx_unqualified_id (pp, t); 2036 pp_cxx_whitespace (pp); 2037 pp_cxx_left_brace (pp); 2038 /* We do not print the namespace-body. */ 2039 pp_cxx_whitespace (pp); 2040 pp_cxx_right_brace (pp); 2041 } 2042 2043 /* namespace-alias: 2044 identifier 2045 2046 namespace-alias-definition: 2047 namespace identifier = qualified-namespace-specifier ; 2048 2049 qualified-namespace-specifier: 2050 ::(opt) nested-name-specifier(opt) namespace-name */ 2051 2052 static void 2053 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t) 2054 { 2055 pp_cxx_ws_string (pp, "namespace"); 2056 if (DECL_CONTEXT (t)) 2057 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t)); 2058 pp_cxx_unqualified_id (pp, t); 2059 pp_cxx_whitespace (pp); 2060 pp_equal (pp); 2061 pp_cxx_whitespace (pp); 2062 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t))) 2063 pp_cxx_nested_name_specifier (pp, 2064 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t))); 2065 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t)); 2066 pp_cxx_semicolon (pp); 2067 } 2068 2069 /* simple-declaration: 2070 decl-specifier-seq(opt) init-declarator-list(opt) */ 2071 2072 static void 2073 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t) 2074 { 2075 pp_cxx_decl_specifier_seq (pp, t); 2076 pp_cxx_init_declarator (pp, t); 2077 pp_cxx_semicolon (pp); 2078 pp_needs_newline (pp) = true; 2079 } 2080 2081 /* 2082 template-parameter-list: 2083 template-parameter 2084 template-parameter-list , template-parameter */ 2085 2086 static inline void 2087 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t) 2088 { 2089 const int n = TREE_VEC_LENGTH (t); 2090 int i; 2091 for (i = 0; i < n; ++i) 2092 { 2093 if (i) 2094 pp_cxx_separate_with (pp, ','); 2095 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i)); 2096 } 2097 } 2098 2099 /* template-parameter: 2100 type-parameter 2101 parameter-declaration 2102 2103 type-parameter: 2104 class ...(opt) identifier(opt) 2105 class identifier(opt) = type-id 2106 typename identifier(opt) 2107 typename ...(opt) identifier(opt) = type-id 2108 template < template-parameter-list > class ...(opt) identifier(opt) 2109 template < template-parameter-list > class identifier(opt) = template-name */ 2110 2111 static void 2112 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t) 2113 { 2114 tree parameter = TREE_VALUE (t); 2115 switch (TREE_CODE (parameter)) 2116 { 2117 case TYPE_DECL: 2118 pp_cxx_ws_string (pp, "class"); 2119 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t))) 2120 pp_cxx_ws_string (pp, "..."); 2121 if (DECL_NAME (parameter)) 2122 pp_cxx_tree_identifier (pp, DECL_NAME (parameter)); 2123 /* FIXME: Check if we should print also default argument. */ 2124 break; 2125 2126 case PARM_DECL: 2127 pp_cxx_parameter_declaration (pp, parameter); 2128 break; 2129 2130 case TEMPLATE_DECL: 2131 break; 2132 2133 default: 2134 pp_unsupported_tree (pp, t); 2135 break; 2136 } 2137 } 2138 2139 /* Pretty-print a template parameter in the canonical form 2140 "template-parameter-<level>-<position in parameter list>". */ 2141 2142 void 2143 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm) 2144 { 2145 const enum tree_code code = TREE_CODE (parm); 2146 2147 /* Brings type template parameters to the canonical forms. */ 2148 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM 2149 || code == BOUND_TEMPLATE_TEMPLATE_PARM) 2150 parm = TEMPLATE_TYPE_PARM_INDEX (parm); 2151 2152 pp_cxx_begin_template_argument_list (pp); 2153 pp_cxx_ws_string (pp, M_("template-parameter-")); 2154 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm)); 2155 pp_minus (pp); 2156 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1); 2157 pp_cxx_end_template_argument_list (pp); 2158 } 2159 2160 /* 2161 template-declaration: 2162 export(opt) template < template-parameter-list > declaration */ 2163 2164 static void 2165 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t) 2166 { 2167 tree tmpl = most_general_template (t); 2168 tree level; 2169 2170 pp_maybe_newline_and_indent (pp, 0); 2171 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level)) 2172 { 2173 pp_cxx_ws_string (pp, "template"); 2174 pp_cxx_begin_template_argument_list (pp); 2175 pp_cxx_template_parameter_list (pp, TREE_VALUE (level)); 2176 pp_cxx_end_template_argument_list (pp); 2177 pp_newline_and_indent (pp, 3); 2178 } 2179 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t)) 2180 pp_cxx_function_definition (pp, t); 2181 else 2182 pp_cxx_simple_declaration (pp, t); 2183 } 2184 2185 static void 2186 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t) 2187 { 2188 pp_unsupported_tree (pp, t); 2189 } 2190 2191 static void 2192 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t) 2193 { 2194 pp_unsupported_tree (pp, t); 2195 } 2196 2197 /* 2198 declaration: 2199 block-declaration 2200 function-definition 2201 template-declaration 2202 explicit-instantiation 2203 explicit-specialization 2204 linkage-specification 2205 namespace-definition 2206 2207 block-declaration: 2208 simple-declaration 2209 asm-definition 2210 namespace-alias-definition 2211 using-declaration 2212 using-directive 2213 static_assert-declaration */ 2214 void 2215 pp_cxx_declaration (cxx_pretty_printer *pp, tree t) 2216 { 2217 if (TREE_CODE (t) == STATIC_ASSERT) 2218 { 2219 pp_cxx_ws_string (pp, "static_assert"); 2220 pp_cxx_left_paren (pp); 2221 pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t)); 2222 pp_cxx_separate_with (pp, ','); 2223 pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t)); 2224 pp_cxx_right_paren (pp); 2225 } 2226 else if (!DECL_LANG_SPECIFIC (t)) 2227 pp_cxx_simple_declaration (pp, t); 2228 else if (DECL_USE_TEMPLATE (t)) 2229 switch (DECL_USE_TEMPLATE (t)) 2230 { 2231 case 1: 2232 pp_cxx_template_declaration (pp, t); 2233 break; 2234 2235 case 2: 2236 pp_cxx_explicit_specialization (pp, t); 2237 break; 2238 2239 case 3: 2240 pp_cxx_explicit_instantiation (pp, t); 2241 break; 2242 2243 default: 2244 break; 2245 } 2246 else switch (TREE_CODE (t)) 2247 { 2248 case VAR_DECL: 2249 case TYPE_DECL: 2250 pp_cxx_simple_declaration (pp, t); 2251 break; 2252 2253 case FUNCTION_DECL: 2254 if (DECL_SAVED_TREE (t)) 2255 pp_cxx_function_definition (pp, t); 2256 else 2257 pp_cxx_simple_declaration (pp, t); 2258 break; 2259 2260 case NAMESPACE_DECL: 2261 if (DECL_NAMESPACE_ALIAS (t)) 2262 pp_cxx_namespace_alias_definition (pp, t); 2263 else 2264 pp_cxx_original_namespace_definition (pp, t); 2265 break; 2266 2267 default: 2268 pp_unsupported_tree (pp, t); 2269 break; 2270 } 2271 } 2272 2273 static void 2274 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t) 2275 { 2276 t = TREE_OPERAND (t, 0); 2277 pp_cxx_ws_string (pp, "typeid"); 2278 pp_cxx_left_paren (pp); 2279 if (TYPE_P (t)) 2280 pp_cxx_type_id (pp, t); 2281 else 2282 pp_cxx_expression (pp, t); 2283 pp_cxx_right_paren (pp); 2284 } 2285 2286 void 2287 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t) 2288 { 2289 pp_cxx_ws_string (pp, "va_arg"); 2290 pp_cxx_left_paren (pp); 2291 pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0)); 2292 pp_cxx_separate_with (pp, ','); 2293 pp_cxx_type_id (pp, TREE_TYPE (t)); 2294 pp_cxx_right_paren (pp); 2295 } 2296 2297 static bool 2298 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t) 2299 { 2300 switch (TREE_CODE (t)) 2301 { 2302 case ARROW_EXPR: 2303 if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR 2304 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) 2305 { 2306 pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0)))); 2307 pp_cxx_separate_with (pp, ','); 2308 return true; 2309 } 2310 return false; 2311 case COMPONENT_REF: 2312 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0))) 2313 return false; 2314 if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR) 2315 pp_cxx_dot (pp); 2316 pp_cxx_expression (pp, TREE_OPERAND (t, 1)); 2317 return true; 2318 case ARRAY_REF: 2319 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0))) 2320 return false; 2321 pp_left_bracket (pp); 2322 pp_cxx_expression (pp, TREE_OPERAND (t, 1)); 2323 pp_right_bracket (pp); 2324 return true; 2325 default: 2326 return false; 2327 } 2328 } 2329 2330 void 2331 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t) 2332 { 2333 pp_cxx_ws_string (pp, "offsetof"); 2334 pp_cxx_left_paren (pp); 2335 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0))) 2336 pp_cxx_expression (pp, TREE_OPERAND (t, 0)); 2337 pp_cxx_right_paren (pp); 2338 } 2339 2340 void 2341 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t) 2342 { 2343 cp_trait_kind kind = TRAIT_EXPR_KIND (t); 2344 2345 switch (kind) 2346 { 2347 case CPTK_HAS_NOTHROW_ASSIGN: 2348 pp_cxx_ws_string (pp, "__has_nothrow_assign"); 2349 break; 2350 case CPTK_HAS_TRIVIAL_ASSIGN: 2351 pp_cxx_ws_string (pp, "__has_trivial_assign"); 2352 break; 2353 case CPTK_HAS_NOTHROW_CONSTRUCTOR: 2354 pp_cxx_ws_string (pp, "__has_nothrow_constructor"); 2355 break; 2356 case CPTK_HAS_TRIVIAL_CONSTRUCTOR: 2357 pp_cxx_ws_string (pp, "__has_trivial_constructor"); 2358 break; 2359 case CPTK_HAS_NOTHROW_COPY: 2360 pp_cxx_ws_string (pp, "__has_nothrow_copy"); 2361 break; 2362 case CPTK_HAS_TRIVIAL_COPY: 2363 pp_cxx_ws_string (pp, "__has_trivial_copy"); 2364 break; 2365 case CPTK_HAS_TRIVIAL_DESTRUCTOR: 2366 pp_cxx_ws_string (pp, "__has_trivial_destructor"); 2367 break; 2368 case CPTK_HAS_VIRTUAL_DESTRUCTOR: 2369 pp_cxx_ws_string (pp, "__has_virtual_destructor"); 2370 break; 2371 case CPTK_IS_ABSTRACT: 2372 pp_cxx_ws_string (pp, "__is_abstract"); 2373 break; 2374 case CPTK_IS_BASE_OF: 2375 pp_cxx_ws_string (pp, "__is_base_of"); 2376 break; 2377 case CPTK_IS_CLASS: 2378 pp_cxx_ws_string (pp, "__is_class"); 2379 break; 2380 case CPTK_IS_CONVERTIBLE_TO: 2381 pp_cxx_ws_string (pp, "__is_convertible_to"); 2382 break; 2383 case CPTK_IS_EMPTY: 2384 pp_cxx_ws_string (pp, "__is_empty"); 2385 break; 2386 case CPTK_IS_ENUM: 2387 pp_cxx_ws_string (pp, "__is_enum"); 2388 break; 2389 case CPTK_IS_FINAL: 2390 pp_cxx_ws_string (pp, "__is_final"); 2391 break; 2392 case CPTK_IS_POD: 2393 pp_cxx_ws_string (pp, "__is_pod"); 2394 break; 2395 case CPTK_IS_POLYMORPHIC: 2396 pp_cxx_ws_string (pp, "__is_polymorphic"); 2397 break; 2398 case CPTK_IS_STD_LAYOUT: 2399 pp_cxx_ws_string (pp, "__is_std_layout"); 2400 break; 2401 case CPTK_IS_TRIVIAL: 2402 pp_cxx_ws_string (pp, "__is_trivial"); 2403 break; 2404 case CPTK_IS_UNION: 2405 pp_cxx_ws_string (pp, "__is_union"); 2406 break; 2407 case CPTK_IS_LITERAL_TYPE: 2408 pp_cxx_ws_string (pp, "__is_literal_type"); 2409 break; 2410 2411 default: 2412 gcc_unreachable (); 2413 } 2414 2415 pp_cxx_left_paren (pp); 2416 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t)); 2417 2418 if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO) 2419 { 2420 pp_cxx_separate_with (pp, ','); 2421 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t)); 2422 } 2423 2424 pp_cxx_right_paren (pp); 2425 } 2426 2427 typedef c_pretty_print_fn pp_fun; 2428 2429 /* Initialization of a C++ pretty-printer object. */ 2430 2431 void 2432 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp) 2433 { 2434 pp_c_pretty_printer_init (pp_c_base (pp)); 2435 pp_set_line_maximum_length (pp, 0); 2436 2437 pp->c_base.declaration = (pp_fun) pp_cxx_declaration; 2438 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq; 2439 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier; 2440 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq; 2441 pp->c_base.declarator = (pp_fun) pp_cxx_declarator; 2442 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator; 2443 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause; 2444 pp->c_base.type_id = (pp_fun) pp_cxx_type_id; 2445 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator; 2446 pp->c_base.direct_abstract_declarator = 2447 (pp_fun) pp_cxx_direct_abstract_declarator; 2448 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier; 2449 2450 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */ 2451 2452 pp->c_base.constant = (pp_fun) pp_cxx_constant; 2453 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression; 2454 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression; 2455 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression; 2456 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression; 2457 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression; 2458 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression; 2459 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression; 2460 pp->c_base.expression = (pp_fun) pp_cxx_expression; 2461 pp->enclosing_scope = global_namespace; 2462 } 2463