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