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>
6 This file is part of GCC.
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
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
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/>. */
24 #include "coretypes.h"
28 #include "cxx-pretty-print.h"
32 /* Translate if being used for diagnostics, but not for dump files or
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
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
);
56 pp_cxx_nonconsecutive_character (cxx_pretty_printer
*pp
, int c
)
58 const char *p
= pp_last_position_in_text (pp
);
60 if (p
!= NULL
&& *p
== c
)
61 pp_cxx_whitespace (pp
);
63 pp_base (pp
)->padding
= pp_none
;
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)
78 pp_cxx_colon_colon (cxx_pretty_printer
*pp
)
81 pp_base (pp
)->padding
= pp_none
;
85 pp_cxx_begin_template_argument_list (cxx_pretty_printer
*pp
)
87 pp_cxx_nonconsecutive_character (pp
, '<');
91 pp_cxx_end_template_argument_list (cxx_pretty_printer
*pp
)
93 pp_cxx_nonconsecutive_character (pp
, '>');
97 pp_cxx_separate_with (cxx_pretty_printer
*pp
, int c
)
99 pp_separate_with (pp
, c
);
100 pp_base (pp
)->padding
= pp_none
;
106 is_destructor_name (tree name
)
108 return name
== complete_dtor_identifier
109 || name
== base_dtor_identifier
110 || name
== deleting_dtor_identifier
;
113 /* conversion-function-id:
114 operator conversion-type-id
117 type-specifier-seq conversion-declarator(opt)
119 conversion-declarator:
120 ptr-operator conversion-declarator(opt) */
123 pp_cxx_conversion_function_id (cxx_pretty_printer
*pp
, tree t
)
125 pp_cxx_ws_string (pp
, "operator");
126 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
130 pp_cxx_template_id (cxx_pretty_printer
*pp
, tree t
)
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
);
138 /* Prints the unqualified part of the id-expression T.
143 conversion-function-id
148 pp_cxx_unqualified_id (cxx_pretty_printer
*pp
, tree t
)
150 enum tree_code code
= TREE_CODE (t
);
154 pp_cxx_ws_string (pp
, M_("<return-value>"));
171 case IDENTIFIER_NODE
:
173 pp_cxx_ws_string (pp
, M_("<unnamed>"));
174 else if (IDENTIFIER_TYPENAME_P (t
))
175 pp_cxx_conversion_function_id (pp
, t
);
178 if (is_destructor_name (t
))
181 /* FIXME: Why is this necessary? */
183 t
= constructor_name (TREE_TYPE (t
));
185 pp_cxx_tree_identifier (pp
, t
);
189 case TEMPLATE_ID_EXPR
:
190 pp_cxx_template_id (pp
, t
);
194 pp_cxx_unqualified_id (pp
, BASELINK_FUNCTIONS (t
));
201 case UNBOUND_CLASS_TEMPLATE
:
202 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
203 if (CLASS_TYPE_P (t
) && CLASSTYPE_USE_TEMPLATE (t
))
205 pp_cxx_begin_template_argument_list (pp
);
206 pp_cxx_template_argument_list (pp
, CLASSTYPE_TI_ARGS (t
));
207 pp_cxx_end_template_argument_list (pp
);
212 pp_cxx_complement (pp
);
213 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
216 case TEMPLATE_TYPE_PARM
:
217 case TEMPLATE_TEMPLATE_PARM
:
218 if (TYPE_IDENTIFIER (t
))
219 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
221 pp_cxx_canonical_template_parameter (pp
, t
);
224 case TEMPLATE_PARM_INDEX
:
225 pp_cxx_unqualified_id (pp
, TEMPLATE_PARM_DECL (t
));
228 case BOUND_TEMPLATE_TEMPLATE_PARM
:
229 pp_cxx_cv_qualifier_seq (pp
, t
);
230 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
231 pp_cxx_begin_template_argument_list (pp
);
232 pp_cxx_template_argument_list (pp
, TYPE_TI_ARGS (t
));
233 pp_cxx_end_template_argument_list (pp
);
237 pp_unsupported_tree (pp
, t
);
242 /* Pretty-print out the token sequence ":: template" in template codes
243 where it is needed to "inline declare" the (following) member as
244 a template. This situation arises when SCOPE of T is dependent
245 on template parameters. */
248 pp_cxx_template_keyword_if_needed (cxx_pretty_printer
*pp
, tree scope
, tree t
)
250 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
251 && TYPE_P (scope
) && dependent_type_p (scope
))
252 pp_cxx_ws_string (pp
, "template");
255 /* nested-name-specifier:
256 class-or-namespace-name :: nested-name-specifier(opt)
257 class-or-namespace-name :: template nested-name-specifier */
260 pp_cxx_nested_name_specifier (cxx_pretty_printer
*pp
, tree t
)
262 if (t
!= NULL
&& t
!= pp
->enclosing_scope
)
264 tree scope
= TYPE_P (t
) ? TYPE_CONTEXT (t
) : DECL_CONTEXT (t
);
265 pp_cxx_nested_name_specifier (pp
, scope
);
266 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
267 pp_cxx_unqualified_id (pp
, t
);
268 pp_cxx_colon_colon (pp
);
273 nested-name-specifier template(opt) unqualified-id */
276 pp_cxx_qualified_id (cxx_pretty_printer
*pp
, tree t
)
278 switch (TREE_CODE (t
))
280 /* A pointer-to-member is always qualified. */
282 pp_cxx_nested_name_specifier (pp
, PTRMEM_CST_CLASS (t
));
283 pp_cxx_unqualified_id (pp
, PTRMEM_CST_MEMBER (t
));
286 /* In Standard C++, functions cannot possibly be used as
287 nested-name-specifiers. However, there are situations where
288 is "makes sense" to output the surrounding function name for the
289 purpose of emphasizing on the scope kind. Just printing the
290 function name might not be sufficient as it may be overloaded; so,
291 we decorate the function with its signature too.
292 FIXME: This is probably the wrong pretty-printing for conversion
293 functions and some function templates. */
297 if (DECL_FUNCTION_MEMBER_P (t
))
298 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
299 pp_cxx_unqualified_id
300 (pp
, DECL_CONSTRUCTOR_P (t
) ? DECL_CONTEXT (t
) : t
);
301 pp_cxx_parameter_declaration_clause (pp
, TREE_TYPE (t
));
306 pp_cxx_nested_name_specifier (pp
, TREE_OPERAND (t
, 0));
307 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 1));
312 tree scope
= TYPE_P (t
) ? TYPE_CONTEXT (t
) : DECL_CONTEXT (t
);
313 if (scope
!= pp
->enclosing_scope
)
315 pp_cxx_nested_name_specifier (pp
, scope
);
316 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
318 pp_cxx_unqualified_id (pp
, t
);
326 pp_cxx_constant (cxx_pretty_printer
*pp
, tree t
)
328 switch (TREE_CODE (t
))
332 const bool in_parens
= PAREN_STRING_LITERAL_P (t
);
334 pp_cxx_left_paren (pp
);
335 pp_c_constant (pp_c_base (pp
), t
);
337 pp_cxx_right_paren (pp
);
342 pp_c_constant (pp_c_base (pp
), t
);
352 pp_cxx_id_expression (cxx_pretty_printer
*pp
, tree t
)
354 if (TREE_CODE (t
) == OVERLOAD
)
356 if (DECL_P (t
) && DECL_CONTEXT (t
))
357 pp_cxx_qualified_id (pp
, t
);
359 pp_cxx_unqualified_id (pp
, t
);
362 /* primary-expression:
366 :: operator-function-id
372 __builtin_va_arg ( assignment-expression , type-id )
373 __builtin_offsetof ( type-id, offsetof-expression )
375 __has_nothrow_assign ( type-id )
376 __has_nothrow_constructor ( type-id )
377 __has_nothrow_copy ( type-id )
378 __has_trivial_assign ( type-id )
379 __has_trivial_constructor ( type-id )
380 __has_trivial_copy ( type-id )
381 __has_trivial_destructor ( type-id )
382 __has_virtual_destructor ( type-id )
383 __is_abstract ( type-id )
384 __is_base_of ( type-id , type-id )
385 __is_class ( type-id )
386 __is_convertible_to ( type-id , type-id )
387 __is_empty ( type-id )
388 __is_enum ( type-id )
390 __is_polymorphic ( type-id )
391 __is_union ( type-id ) */
394 pp_cxx_primary_expression (cxx_pretty_printer
*pp
, tree t
)
396 switch (TREE_CODE (t
))
402 pp_cxx_constant (pp
, t
);
406 t
= BASELINK_FUNCTIONS (t
);
414 pp_cxx_id_expression (pp
, t
);
418 case TEMPLATE_TYPE_PARM
:
419 case TEMPLATE_TEMPLATE_PARM
:
420 case TEMPLATE_PARM_INDEX
:
421 pp_cxx_unqualified_id (pp
, t
);
425 pp_cxx_left_paren (pp
);
426 pp_cxx_statement (pp
, STMT_EXPR_STMT (t
));
427 pp_cxx_right_paren (pp
);
431 pp_cxx_trait_expression (pp
, t
);
435 pp_cxx_va_arg_expression (pp
, t
);
439 pp_cxx_offsetof_expression (pp
, t
);
443 pp_c_primary_expression (pp_c_base (pp
), t
);
448 /* postfix-expression:
450 postfix-expression [ expression ]
451 postfix-expression ( expression-list(opt) )
452 simple-type-specifier ( expression-list(opt) )
453 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
454 typename ::(opt) nested-name-specifier template(opt)
455 template-id ( expression-list(opt) )
456 postfix-expression . template(opt) ::(opt) id-expression
457 postfix-expression -> template(opt) ::(opt) id-expression
458 postfix-expression . pseudo-destructor-name
459 postfix-expression -> pseudo-destructor-name
460 postfix-expression ++
461 postfix-expression --
462 dynamic_cast < type-id > ( expression )
463 static_cast < type-id > ( expression )
464 reinterpret_cast < type-id > ( expression )
465 const_cast < type-id > ( expression )
466 typeid ( expression )
467 typeid ( type-id ) */
470 pp_cxx_postfix_expression (cxx_pretty_printer
*pp
, tree t
)
472 enum tree_code code
= TREE_CODE (t
);
479 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
481 tree saved_scope
= pp
->enclosing_scope
;
482 bool skipfirst
= false;
485 if (TREE_CODE (fun
) == ADDR_EXPR
)
486 fun
= TREE_OPERAND (fun
, 0);
488 /* In templates, where there is no way to tell whether a given
489 call uses an actual member function. So the parser builds
490 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
491 instantiation time. */
492 if (TREE_CODE (fun
) != FUNCTION_DECL
)
494 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
496 tree object
= (code
== AGGR_INIT_EXPR
497 ? (AGGR_INIT_VIA_CTOR_P (t
)
498 ? AGGR_INIT_EXPR_SLOT (t
)
499 : AGGR_INIT_EXPR_ARG (t
, 0))
500 : CALL_EXPR_ARG (t
, 0));
502 while (TREE_CODE (object
) == NOP_EXPR
)
503 object
= TREE_OPERAND (object
, 0);
505 if (TREE_CODE (object
) == ADDR_EXPR
)
506 object
= TREE_OPERAND (object
, 0);
508 if (TREE_CODE (TREE_TYPE (object
)) != POINTER_TYPE
)
510 pp_cxx_postfix_expression (pp
, object
);
515 pp_cxx_postfix_expression (pp
, object
);
519 pp
->enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
522 pp_cxx_postfix_expression (pp
, fun
);
523 pp
->enclosing_scope
= saved_scope
;
524 pp_cxx_left_paren (pp
);
525 if (code
== AGGR_INIT_EXPR
)
527 aggr_init_expr_arg_iterator iter
;
528 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
534 pp_cxx_expression (pp
, arg
);
535 if (more_aggr_init_expr_args_p (&iter
))
536 pp_cxx_separate_with (pp
, ',');
542 call_expr_arg_iterator iter
;
543 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
549 pp_cxx_expression (pp
, arg
);
550 if (more_call_expr_args_p (&iter
))
551 pp_cxx_separate_with (pp
, ',');
555 pp_cxx_right_paren (pp
);
557 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
559 pp_cxx_separate_with (pp
, ',');
560 pp_cxx_postfix_expression (pp
, AGGR_INIT_EXPR_SLOT (t
));
573 pp_cxx_primary_expression (pp
, t
);
576 case DYNAMIC_CAST_EXPR
:
577 case STATIC_CAST_EXPR
:
578 case REINTERPRET_CAST_EXPR
:
579 case CONST_CAST_EXPR
:
580 if (code
== DYNAMIC_CAST_EXPR
)
581 pp_cxx_ws_string (pp
, "dynamic_cast");
582 else if (code
== STATIC_CAST_EXPR
)
583 pp_cxx_ws_string (pp
, "static_cast");
584 else if (code
== REINTERPRET_CAST_EXPR
)
585 pp_cxx_ws_string (pp
, "reinterpret_cast");
587 pp_cxx_ws_string (pp
, "const_cast");
588 pp_cxx_begin_template_argument_list (pp
);
589 pp_cxx_type_id (pp
, TREE_TYPE (t
));
590 pp_cxx_end_template_argument_list (pp
);
592 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
596 case EMPTY_CLASS_EXPR
:
597 pp_cxx_type_id (pp
, TREE_TYPE (t
));
603 pp_cxx_typeid_expression (pp
, t
);
606 case PSEUDO_DTOR_EXPR
:
607 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
609 pp_cxx_qualified_id (pp
, TREE_OPERAND (t
, 1));
610 pp_cxx_colon_colon (pp
);
612 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 2));
616 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
621 pp_c_postfix_expression (pp_c_base (pp
), t
);
627 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
628 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
634 type-specifier-seq new-declarator(opt)
637 ptr-operator new-declarator(opt)
638 direct-new-declarator
640 direct-new-declarator
642 direct-new-declarator [ constant-expression ]
645 ( expression-list(opt) ) */
648 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
650 enum tree_code code
= TREE_CODE (t
);
651 tree type
= TREE_OPERAND (t
, 1);
652 tree init
= TREE_OPERAND (t
, 2);
657 if (NEW_EXPR_USE_GLOBAL (t
))
658 pp_cxx_colon_colon (pp
);
659 pp_cxx_ws_string (pp
, "new");
660 if (TREE_OPERAND (t
, 0))
662 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
665 if (TREE_CODE (type
) == ARRAY_REF
)
666 type
= build_cplus_array_type
667 (TREE_OPERAND (type
, 0),
668 build_index_type (fold_build2_loc (input_location
,
669 MINUS_EXPR
, integer_type_node
,
670 TREE_OPERAND (type
, 1),
672 pp_cxx_type_id (pp
, type
);
676 if (TREE_CODE (init
) == TREE_LIST
)
677 pp_c_expression_list (pp_c_base (pp
), init
);
678 else if (init
== void_zero_node
)
679 ; /* OK, empty initializer list. */
681 pp_cxx_expression (pp
, init
);
687 pp_unsupported_tree (pp
, t
);
691 /* delete-expression:
692 ::(opt) delete cast-expression
693 ::(opt) delete [ ] cast-expression */
696 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
698 enum tree_code code
= TREE_CODE (t
);
702 case VEC_DELETE_EXPR
:
703 if (DELETE_EXPR_USE_GLOBAL (t
))
704 pp_cxx_colon_colon (pp
);
705 pp_cxx_ws_string (pp
, "delete");
707 if (code
== VEC_DELETE_EXPR
708 || DELETE_EXPR_USE_VEC (t
))
710 pp_left_bracket (pp
);
711 pp_right_bracket (pp
);
714 pp_c_cast_expression (pp_c_base (pp
), TREE_OPERAND (t
, 0));
718 pp_unsupported_tree (pp
, t
);
726 unary-operator cast-expression
727 sizeof unary-expression
729 sizeof ... ( identifier )
733 unary-operator: one of
737 __alignof__ unary-expression
738 __alignof__ ( type-id ) */
741 pp_cxx_unary_expression (cxx_pretty_printer
*pp
, tree t
)
743 enum tree_code code
= TREE_CODE (t
);
748 pp_cxx_new_expression (pp
, t
);
752 case VEC_DELETE_EXPR
:
753 pp_cxx_delete_expression (pp
, t
);
757 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
759 pp_cxx_ws_string (pp
, "sizeof");
760 pp_cxx_ws_string (pp
, "...");
761 pp_cxx_whitespace (pp
);
762 pp_cxx_left_paren (pp
);
763 if (TYPE_P (TREE_OPERAND (t
, 0)))
764 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
766 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
767 pp_cxx_right_paren (pp
);
773 pp_cxx_ws_string (pp
, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
774 pp_cxx_whitespace (pp
);
775 if (TYPE_P (TREE_OPERAND (t
, 0)))
777 pp_cxx_left_paren (pp
);
778 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
779 pp_cxx_right_paren (pp
);
782 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
785 case UNARY_PLUS_EXPR
:
787 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 0));
791 pp_c_unary_expression (pp_c_base (pp
), t
);
798 ( type-id ) cast-expression */
801 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
803 switch (TREE_CODE (t
))
806 pp_cxx_type_id (pp
, TREE_TYPE (t
));
807 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
811 pp_c_cast_expression (pp_c_base (pp
), t
);
818 pm-expression .* cast-expression
819 pm-expression ->* cast-expression */
822 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
824 switch (TREE_CODE (t
))
826 /* Handle unfortunate OFFSET_REF overloading here. */
828 if (TYPE_P (TREE_OPERAND (t
, 0)))
830 pp_cxx_qualified_id (pp
, t
);
833 /* Else fall through. */
836 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
837 if (TREE_CODE (t
) == MEMBER_REF
)
842 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
847 pp_cxx_cast_expression (pp
, t
);
852 /* multiplicative-expression:
854 multiplicative-expression * pm-expression
855 multiplicative-expression / pm-expression
856 multiplicative-expression % pm-expression */
859 pp_cxx_multiplicative_expression (cxx_pretty_printer
*pp
, tree e
)
861 enum tree_code code
= TREE_CODE (e
);
867 pp_cxx_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
869 if (code
== MULT_EXPR
)
871 else if (code
== TRUNC_DIV_EXPR
)
876 pp_cxx_pm_expression (pp
, TREE_OPERAND (e
, 1));
880 pp_cxx_pm_expression (pp
, e
);
885 /* conditional-expression:
886 logical-or-expression
887 logical-or-expression ? expression : assignment-expression */
890 pp_cxx_conditional_expression (cxx_pretty_printer
*pp
, tree e
)
892 if (TREE_CODE (e
) == COND_EXPR
)
894 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
898 pp_cxx_expression (pp
, TREE_OPERAND (e
, 1));
900 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
903 pp_c_logical_or_expression (pp_c_base (pp
), e
);
906 /* Pretty-print a compound assignment operator token as indicated by T. */
909 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
913 switch (TREE_CODE (t
))
936 op
= tree_code_name
[TREE_CODE (t
)];
940 pp_cxx_ws_string (pp
, op
);
944 /* assignment-expression:
945 conditional-expression
946 logical-or-expression assignment-operator assignment-expression
950 throw assignment-expression(opt)
952 assignment-operator: one of
953 = *= /= %= += -= >>= <<= &= ^= |= */
956 pp_cxx_assignment_expression (cxx_pretty_printer
*pp
, tree e
)
958 switch (TREE_CODE (e
))
962 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
966 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 1));
970 pp_cxx_ws_string (pp
, "throw");
971 if (TREE_OPERAND (e
, 0))
972 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 0));
976 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
977 pp_cxx_assignment_operator (pp
, TREE_OPERAND (e
, 1));
978 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
982 pp_cxx_conditional_expression (pp
, e
);
988 pp_cxx_expression (cxx_pretty_printer
*pp
, tree t
)
990 switch (TREE_CODE (t
))
996 pp_cxx_constant (pp
, t
);
1000 pp_cxx_unqualified_id (pp
, t
);
1008 pp_cxx_qualified_id (pp
, t
);
1012 t
= OVL_CURRENT (t
);
1020 case TEMPLATE_TYPE_PARM
:
1021 case TEMPLATE_PARM_INDEX
:
1022 case TEMPLATE_TEMPLATE_PARM
:
1024 pp_cxx_primary_expression (pp
, t
);
1028 case DYNAMIC_CAST_EXPR
:
1029 case STATIC_CAST_EXPR
:
1030 case REINTERPRET_CAST_EXPR
:
1031 case CONST_CAST_EXPR
:
1035 case EMPTY_CLASS_EXPR
:
1037 case PSEUDO_DTOR_EXPR
:
1038 case AGGR_INIT_EXPR
:
1040 pp_cxx_postfix_expression (pp
, t
);
1045 pp_cxx_new_expression (pp
, t
);
1049 case VEC_DELETE_EXPR
:
1050 pp_cxx_delete_expression (pp
, t
);
1055 pp_cxx_unary_expression (pp
, t
);
1059 pp_cxx_cast_expression (pp
, t
);
1065 pp_cxx_pm_expression (pp
, t
);
1069 case TRUNC_DIV_EXPR
:
1070 case TRUNC_MOD_EXPR
:
1071 pp_cxx_multiplicative_expression (pp
, t
);
1075 pp_cxx_conditional_expression (pp
, t
);
1082 pp_cxx_assignment_expression (pp
, t
);
1085 case NON_DEPENDENT_EXPR
:
1086 case MUST_NOT_THROW_EXPR
:
1087 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
1090 case EXPR_PACK_EXPANSION
:
1091 pp_cxx_expression (pp
, PACK_EXPANSION_PATTERN (t
));
1092 pp_cxx_ws_string (pp
, "...");
1095 case NONTYPE_ARGUMENT_PACK
:
1097 tree args
= ARGUMENT_PACK_ARGS (t
);
1098 int i
, len
= TREE_VEC_LENGTH (args
);
1099 for (i
= 0; i
< len
; ++i
)
1102 pp_cxx_separate_with (pp
, ',');
1103 pp_cxx_expression (pp
, TREE_VEC_ELT (args
, i
));
1109 pp_c_expression (pp_c_base (pp
), t
);
1117 /* function-specifier:
1123 pp_cxx_function_specifier (cxx_pretty_printer
*pp
, tree t
)
1125 switch (TREE_CODE (t
))
1128 if (DECL_VIRTUAL_P (t
))
1129 pp_cxx_ws_string (pp
, "virtual");
1130 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1131 pp_cxx_ws_string (pp
, "explicit");
1133 pp_c_function_specifier (pp_c_base (pp
), t
);
1140 /* decl-specifier-seq:
1141 decl-specifier-seq(opt) decl-specifier
1144 storage-class-specifier
1151 pp_cxx_decl_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1153 switch (TREE_CODE (t
))
1159 pp_cxx_storage_class_specifier (pp
, t
);
1160 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1164 pp_cxx_ws_string (pp
, "typedef");
1165 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1169 if (TYPE_PTRMEMFUNC_P (t
))
1171 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1172 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (pfm
)));
1173 pp_cxx_whitespace (pp
);
1174 pp_cxx_ptr_operator (pp
, t
);
1179 /* Constructors don't have return types. And conversion functions
1180 do not have a type-specifier in their return types. */
1181 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1182 pp_cxx_function_specifier (pp
, t
);
1183 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1184 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (t
)));
1187 pp_c_declaration_specifiers (pp_c_base (pp
), t
);
1192 /* simple-type-specifier:
1193 ::(opt) nested-name-specifier(opt) type-name
1194 ::(opt) nested-name-specifier(opt) template(opt) template-id
1208 pp_cxx_simple_type_specifier (cxx_pretty_printer
*pp
, tree t
)
1210 switch (TREE_CODE (t
))
1215 pp_cxx_qualified_id (pp
, t
);
1218 case TEMPLATE_TYPE_PARM
:
1219 case TEMPLATE_TEMPLATE_PARM
:
1220 case TEMPLATE_PARM_INDEX
:
1221 pp_cxx_unqualified_id (pp
, t
);
1225 pp_cxx_ws_string (pp
, "typename");
1226 pp_cxx_nested_name_specifier (pp
, TYPE_CONTEXT (t
));
1227 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
1231 pp_c_type_specifier (pp_c_base (pp
), t
);
1236 /* type-specifier-seq:
1237 type-specifier type-specifier-seq(opt)
1240 simple-type-specifier
1243 elaborated-type-specifier
1247 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1249 switch (TREE_CODE (t
))
1252 case TEMPLATE_TYPE_PARM
:
1253 case TEMPLATE_TEMPLATE_PARM
:
1255 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1256 pp_cxx_cv_qualifier_seq (pp
, t
);
1257 pp_cxx_simple_type_specifier (pp
, t
);
1261 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1262 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1263 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1267 pp_cxx_ws_string (pp
, "decltype");
1268 pp_cxx_left_paren (pp
);
1269 pp_cxx_expression (pp
, DECLTYPE_TYPE_EXPR (t
));
1270 pp_cxx_right_paren (pp
);
1274 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1275 pp_c_specifier_qualifier_list (pp_c_base (pp
), t
);
1280 * cv-qualifier-seq(opt)
1282 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1285 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1287 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1289 switch (TREE_CODE (t
))
1291 case REFERENCE_TYPE
:
1293 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
1294 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t
)))
1295 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1296 if (TREE_CODE (t
) == POINTER_TYPE
)
1299 pp_cxx_cv_qualifier_seq (pp
, t
);
1306 if (TYPE_PTRMEMFUNC_P (t
))
1308 pp_cxx_left_paren (pp
);
1309 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1314 if (TYPE_PTR_TO_MEMBER_P (t
))
1316 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1317 pp_cxx_left_paren (pp
);
1318 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1320 pp_cxx_cv_qualifier_seq (pp
, t
);
1323 /* else fall through. */
1326 pp_unsupported_tree (pp
, t
);
1332 pp_cxx_implicit_parameter_type (tree mf
)
1334 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf
))));
1338 parameter-declaration:
1339 decl-specifier-seq declarator
1340 decl-specifier-seq declarator = assignment-expression
1341 decl-specifier-seq abstract-declarator(opt)
1342 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1345 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1347 pp_cxx_decl_specifier_seq (pp
, t
);
1349 pp_cxx_abstract_declarator (pp
, t
);
1351 pp_cxx_declarator (pp
, t
);
1354 /* parameter-declaration-clause:
1355 parameter-declaration-list(opt) ...(opt)
1356 parameter-declaration-list , ...
1358 parameter-declaration-list:
1359 parameter-declaration
1360 parameter-declaration-list , parameter-declaration */
1363 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1365 tree args
= TYPE_P (t
) ? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1367 TYPE_P (t
) ? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1368 const bool abstract
= args
== NULL
1369 || pp_c_base (pp
)->flags
& pp_c_flag_abstract
;
1372 /* Skip artificial parameter for nonstatic member functions. */
1373 if (TREE_CODE (t
) == METHOD_TYPE
)
1374 types
= TREE_CHAIN (types
);
1376 pp_cxx_left_paren (pp
);
1377 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1380 pp_cxx_separate_with (pp
, ',');
1382 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1383 if (!abstract
&& pp_c_base (pp
)->flags
& pp_cxx_flag_default_argument
)
1385 pp_cxx_whitespace (pp
);
1387 pp_cxx_whitespace (pp
);
1388 pp_cxx_assignment_expression (pp
, TREE_PURPOSE (types
));
1391 pp_cxx_right_paren (pp
);
1394 /* exception-specification:
1395 throw ( type-id-list(opt) )
1399 type-id-list , type-id */
1402 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1404 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1405 bool need_comma
= false;
1407 if (!TYPE_NOTHROW_P (t
) && ex_spec
== NULL
)
1409 pp_cxx_ws_string (pp
, "throw");
1410 pp_cxx_left_paren (pp
);
1411 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1413 tree type
= TREE_VALUE (ex_spec
);
1414 tree argpack
= NULL_TREE
;
1417 if (ARGUMENT_PACK_P (type
))
1419 argpack
= ARGUMENT_PACK_ARGS (type
);
1420 len
= TREE_VEC_LENGTH (argpack
);
1423 for (i
= 0; i
< len
; ++i
)
1426 type
= TREE_VEC_ELT (argpack
, i
);
1429 pp_cxx_separate_with (pp
, ',');
1433 pp_cxx_type_id (pp
, type
);
1436 pp_cxx_right_paren (pp
);
1439 /* direct-declarator:
1441 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1442 exception-specification(opt)
1443 direct-declaration [ constant-expression(opt) ]
1447 pp_cxx_direct_declarator (cxx_pretty_printer
*pp
, tree t
)
1449 switch (TREE_CODE (t
))
1457 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1459 if ((TREE_CODE (t
) == PARM_DECL
&& FUNCTION_PARAMETER_PACK_P (t
))
1460 || template_parameter_pack_p (t
))
1461 /* A function parameter pack or non-type template
1463 pp_cxx_ws_string (pp
, "...");
1465 pp_cxx_id_expression (pp
, DECL_NAME (t
));
1467 pp_cxx_abstract_declarator (pp
, TREE_TYPE (t
));
1471 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
1472 pp_cxx_id_expression (pp
, t
);
1473 pp_cxx_parameter_declaration_clause (pp
, t
);
1475 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1477 pp_base (pp
)->padding
= pp_before
;
1478 pp_cxx_cv_qualifier_seq (pp
, pp_cxx_implicit_parameter_type (t
));
1481 pp_cxx_exception_specification (pp
, TREE_TYPE (t
));
1486 case TEMPLATE_TYPE_PARM
:
1487 case TEMPLATE_PARM_INDEX
:
1488 case TEMPLATE_TEMPLATE_PARM
:
1492 pp_c_direct_declarator (pp_c_base (pp
), t
);
1499 ptr-operator declarator */
1502 pp_cxx_declarator (cxx_pretty_printer
*pp
, tree t
)
1504 pp_cxx_direct_declarator (pp
, t
);
1507 /* ctor-initializer:
1508 : mem-initializer-list
1510 mem-initializer-list:
1512 mem-initializer , mem-initializer-list
1515 mem-initializer-id ( expression-list(opt) )
1518 ::(opt) nested-name-specifier(opt) class-name
1522 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1524 t
= TREE_OPERAND (t
, 0);
1525 pp_cxx_whitespace (pp
);
1527 pp_cxx_whitespace (pp
);
1528 for (; t
; t
= TREE_CHAIN (t
))
1530 tree purpose
= TREE_PURPOSE (t
);
1531 bool is_pack
= PACK_EXPANSION_P (purpose
);
1534 pp_cxx_primary_expression (pp
, PACK_EXPANSION_PATTERN (purpose
));
1536 pp_cxx_primary_expression (pp
, purpose
);
1537 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1539 pp_cxx_ws_string (pp
, "...");
1541 pp_cxx_separate_with (pp
, ',');
1545 /* function-definition:
1546 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1547 decl-specifier-seq(opt) declarator function-try-block */
1550 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1552 tree saved_scope
= pp
->enclosing_scope
;
1553 pp_cxx_decl_specifier_seq (pp
, t
);
1554 pp_cxx_declarator (pp
, t
);
1555 pp_needs_newline (pp
) = true;
1556 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1557 if (DECL_SAVED_TREE (t
))
1558 pp_cxx_statement (pp
, DECL_SAVED_TREE (t
));
1561 pp_cxx_semicolon (pp
);
1562 pp_needs_newline (pp
) = true;
1565 pp
->enclosing_scope
= saved_scope
;
1568 /* abstract-declarator:
1569 ptr-operator abstract-declarator(opt)
1570 direct-abstract-declarator */
1573 pp_cxx_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1575 if (TYPE_PTRMEM_P (t
) || TYPE_PTRMEMFUNC_P (t
))
1576 pp_cxx_right_paren (pp
);
1577 else if (POINTER_TYPE_P (t
))
1579 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1580 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1581 pp_cxx_right_paren (pp
);
1584 pp_cxx_direct_abstract_declarator (pp
, t
);
1587 /* direct-abstract-declarator:
1588 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1589 cv-qualifier-seq(opt) exception-specification(opt)
1590 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1591 ( abstract-declarator ) */
1594 pp_cxx_direct_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1596 switch (TREE_CODE (t
))
1598 case REFERENCE_TYPE
:
1599 pp_cxx_abstract_declarator (pp
, t
);
1603 if (TYPE_PTRMEMFUNC_P (t
))
1604 pp_cxx_direct_abstract_declarator (pp
, TYPE_PTRMEMFUNC_FN_TYPE (t
));
1609 pp_cxx_parameter_declaration_clause (pp
, t
);
1610 pp_cxx_direct_abstract_declarator (pp
, TREE_TYPE (t
));
1611 if (TREE_CODE (t
) == METHOD_TYPE
)
1613 pp_base (pp
)->padding
= pp_before
;
1614 pp_cxx_cv_qualifier_seq
1615 (pp
, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))));
1617 pp_cxx_exception_specification (pp
, t
);
1621 case TEMPLATE_TYPE_PARM
:
1622 case TEMPLATE_TEMPLATE_PARM
:
1623 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1624 case UNBOUND_CLASS_TEMPLATE
:
1628 pp_c_direct_abstract_declarator (pp_c_base (pp
), t
);
1634 type-specifier-seq abstract-declarator(opt) */
1637 pp_cxx_type_id (cxx_pretty_printer
*pp
, tree t
)
1639 pp_flags saved_flags
= pp_c_base (pp
)->flags
;
1640 pp_c_base (pp
)->flags
|= pp_c_flag_abstract
;
1642 switch (TREE_CODE (t
))
1649 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1650 case UNBOUND_CLASS_TEMPLATE
:
1651 case TEMPLATE_TEMPLATE_PARM
:
1652 case TEMPLATE_TYPE_PARM
:
1653 case TEMPLATE_PARM_INDEX
:
1657 case TEMPLATE_ID_EXPR
:
1658 pp_cxx_type_specifier_seq (pp
, t
);
1661 case TYPE_PACK_EXPANSION
:
1662 pp_cxx_type_id (pp
, PACK_EXPANSION_PATTERN (t
));
1663 pp_cxx_ws_string (pp
, "...");
1667 pp_c_type_id (pp_c_base (pp
), t
);
1671 pp_c_base (pp
)->flags
= saved_flags
;
1674 /* template-argument-list:
1675 template-argument ...(opt)
1676 template-argument-list, template-argument ...(opt)
1679 assignment-expression
1684 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1687 bool need_comma
= false;
1691 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1693 tree arg
= TREE_VEC_ELT (t
, i
);
1694 tree argpack
= NULL_TREE
;
1697 if (ARGUMENT_PACK_P (arg
))
1699 argpack
= ARGUMENT_PACK_ARGS (arg
);
1700 len
= TREE_VEC_LENGTH (argpack
);
1703 for (idx
= 0; idx
< len
; idx
++)
1706 arg
= TREE_VEC_ELT (argpack
, idx
);
1709 pp_cxx_separate_with (pp
, ',');
1713 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1714 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1715 pp_cxx_type_id (pp
, arg
);
1717 pp_cxx_expression (pp
, arg
);
1724 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1726 t
= DECL_EXPR_DECL (t
);
1727 pp_cxx_type_specifier_seq (pp
, t
);
1729 pp_cxx_abstract_declarator (pp
, t
);
1731 pp_cxx_declarator (pp
, t
);
1737 pp_cxx_statement (cxx_pretty_printer
*pp
, tree t
)
1739 switch (TREE_CODE (t
))
1741 case CTOR_INITIALIZER
:
1742 pp_cxx_ctor_initializer (pp
, t
);
1746 pp_cxx_ws_string (pp
, "using");
1747 pp_cxx_ws_string (pp
, "namespace");
1748 if (DECL_CONTEXT (t
))
1749 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1750 pp_cxx_qualified_id (pp
, USING_STMT_NAMESPACE (t
));
1754 pp_cxx_ws_string (pp
, "using");
1755 pp_cxx_nested_name_specifier (pp
, USING_DECL_SCOPE (t
));
1756 pp_cxx_unqualified_id (pp
, DECL_NAME (t
));
1763 try compound-statement handler-seq */
1765 pp_maybe_newline_and_indent (pp
, 0);
1766 pp_cxx_ws_string (pp
, "try");
1767 pp_newline_and_indent (pp
, 3);
1768 pp_cxx_statement (pp
, TRY_STMTS (t
));
1769 pp_newline_and_indent (pp
, -3);
1773 pp_cxx_statement (pp
, TRY_HANDLERS (t
));
1778 handler handler-seq(opt)
1781 catch ( exception-declaration ) compound-statement
1783 exception-declaration:
1784 type-specifier-seq declarator
1785 type-specifier-seq abstract-declarator
1788 pp_cxx_ws_string (pp
, "catch");
1789 pp_cxx_left_paren (pp
);
1790 pp_cxx_exception_declaration (pp
, HANDLER_PARMS (t
));
1791 pp_cxx_right_paren (pp
);
1792 pp_indentation (pp
) += 3;
1793 pp_needs_newline (pp
) = true;
1794 pp_cxx_statement (pp
, HANDLER_BODY (t
));
1795 pp_indentation (pp
) -= 3;
1796 pp_needs_newline (pp
) = true;
1799 /* selection-statement:
1800 if ( expression ) statement
1801 if ( expression ) statement else statement */
1803 pp_cxx_ws_string (pp
, "if");
1804 pp_cxx_whitespace (pp
);
1805 pp_cxx_left_paren (pp
);
1806 pp_cxx_expression (pp
, IF_COND (t
));
1807 pp_cxx_right_paren (pp
);
1808 pp_newline_and_indent (pp
, 2);
1809 pp_cxx_statement (pp
, THEN_CLAUSE (t
));
1810 pp_newline_and_indent (pp
, -2);
1811 if (ELSE_CLAUSE (t
))
1813 tree else_clause
= ELSE_CLAUSE (t
);
1814 pp_cxx_ws_string (pp
, "else");
1815 if (TREE_CODE (else_clause
) == IF_STMT
)
1816 pp_cxx_whitespace (pp
);
1818 pp_newline_and_indent (pp
, 2);
1819 pp_cxx_statement (pp
, else_clause
);
1820 if (TREE_CODE (else_clause
) != IF_STMT
)
1821 pp_newline_and_indent (pp
, -2);
1826 pp_cxx_ws_string (pp
, "switch");
1828 pp_cxx_left_paren (pp
);
1829 pp_cxx_expression (pp
, SWITCH_STMT_COND (t
));
1830 pp_cxx_right_paren (pp
);
1831 pp_indentation (pp
) += 3;
1832 pp_needs_newline (pp
) = true;
1833 pp_cxx_statement (pp
, SWITCH_STMT_BODY (t
));
1834 pp_newline_and_indent (pp
, -3);
1837 /* iteration-statement:
1838 while ( expression ) statement
1839 do statement while ( expression ) ;
1840 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1841 for ( declaration expression(opt) ; expression(opt) ) statement */
1843 pp_cxx_ws_string (pp
, "while");
1845 pp_cxx_left_paren (pp
);
1846 pp_cxx_expression (pp
, WHILE_COND (t
));
1847 pp_cxx_right_paren (pp
);
1848 pp_newline_and_indent (pp
, 3);
1849 pp_cxx_statement (pp
, WHILE_BODY (t
));
1850 pp_indentation (pp
) -= 3;
1851 pp_needs_newline (pp
) = true;
1855 pp_cxx_ws_string (pp
, "do");
1856 pp_newline_and_indent (pp
, 3);
1857 pp_cxx_statement (pp
, DO_BODY (t
));
1858 pp_newline_and_indent (pp
, -3);
1859 pp_cxx_ws_string (pp
, "while");
1861 pp_cxx_left_paren (pp
);
1862 pp_cxx_expression (pp
, DO_COND (t
));
1863 pp_cxx_right_paren (pp
);
1864 pp_cxx_semicolon (pp
);
1865 pp_needs_newline (pp
) = true;
1869 pp_cxx_ws_string (pp
, "for");
1871 pp_cxx_left_paren (pp
);
1872 if (FOR_INIT_STMT (t
))
1873 pp_cxx_statement (pp
, FOR_INIT_STMT (t
));
1875 pp_cxx_semicolon (pp
);
1876 pp_needs_newline (pp
) = false;
1877 pp_cxx_whitespace (pp
);
1879 pp_cxx_expression (pp
, FOR_COND (t
));
1880 pp_cxx_semicolon (pp
);
1881 pp_needs_newline (pp
) = false;
1882 pp_cxx_whitespace (pp
);
1884 pp_cxx_expression (pp
, FOR_EXPR (t
));
1885 pp_cxx_right_paren (pp
);
1886 pp_newline_and_indent (pp
, 3);
1887 pp_cxx_statement (pp
, FOR_BODY (t
));
1888 pp_indentation (pp
) -= 3;
1889 pp_needs_newline (pp
) = true;
1895 return expression(opt) ; */
1898 pp_string (pp
, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
1899 pp_cxx_semicolon (pp
);
1900 pp_needs_newline (pp
) = true;
1903 /* expression-statement:
1904 expression(opt) ; */
1906 pp_cxx_expression (pp
, EXPR_STMT_EXPR (t
));
1907 pp_cxx_semicolon (pp
);
1908 pp_needs_newline (pp
) = true;
1912 pp_cxx_ws_string (pp
, "try");
1913 pp_newline_and_indent (pp
, 2);
1914 pp_cxx_statement (pp
, CLEANUP_BODY (t
));
1915 pp_newline_and_indent (pp
, -2);
1916 pp_cxx_ws_string (pp
, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
1917 pp_newline_and_indent (pp
, 2);
1918 pp_cxx_statement (pp
, CLEANUP_EXPR (t
));
1919 pp_newline_and_indent (pp
, -2);
1923 pp_cxx_declaration (pp
, t
);
1927 pp_c_statement (pp_c_base (pp
), t
);
1932 /* original-namespace-definition:
1933 namespace identifier { namespace-body }
1935 As an edge case, we also handle unnamed namespace definition here. */
1938 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
1940 pp_cxx_ws_string (pp
, "namespace");
1941 if (DECL_CONTEXT (t
))
1942 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1944 pp_cxx_unqualified_id (pp
, t
);
1945 pp_cxx_whitespace (pp
);
1946 pp_cxx_left_brace (pp
);
1947 /* We do not print the namespace-body. */
1948 pp_cxx_whitespace (pp
);
1949 pp_cxx_right_brace (pp
);
1955 namespace-alias-definition:
1956 namespace identifier = qualified-namespace-specifier ;
1958 qualified-namespace-specifier:
1959 ::(opt) nested-name-specifier(opt) namespace-name */
1962 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
1964 pp_cxx_ws_string (pp
, "namespace");
1965 if (DECL_CONTEXT (t
))
1966 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1967 pp_cxx_unqualified_id (pp
, t
);
1968 pp_cxx_whitespace (pp
);
1970 pp_cxx_whitespace (pp
);
1971 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
1972 pp_cxx_nested_name_specifier (pp
,
1973 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
1974 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
1975 pp_cxx_semicolon (pp
);
1978 /* simple-declaration:
1979 decl-specifier-seq(opt) init-declarator-list(opt) */
1982 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
1984 pp_cxx_decl_specifier_seq (pp
, t
);
1985 pp_cxx_init_declarator (pp
, t
);
1986 pp_cxx_semicolon (pp
);
1987 pp_needs_newline (pp
) = true;
1991 template-parameter-list:
1993 template-parameter-list , template-parameter */
1996 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
1998 const int n
= TREE_VEC_LENGTH (t
);
2000 for (i
= 0; i
< n
; ++i
)
2003 pp_cxx_separate_with (pp
, ',');
2004 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2008 /* template-parameter:
2010 parameter-declaration
2013 class ...(opt) identifier(opt)
2014 class identifier(opt) = type-id
2015 typename identifier(opt)
2016 typename ...(opt) identifier(opt) = type-id
2017 template < template-parameter-list > class ...(opt) identifier(opt)
2018 template < template-parameter-list > class identifier(opt) = template-name */
2021 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2023 tree parameter
= TREE_VALUE (t
);
2024 switch (TREE_CODE (parameter
))
2027 pp_cxx_ws_string (pp
, "class");
2028 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2029 pp_cxx_ws_string (pp
, "...");
2030 if (DECL_NAME (parameter
))
2031 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2032 /* FIXME: Check if we should print also default argument. */
2036 pp_cxx_parameter_declaration (pp
, parameter
);
2043 pp_unsupported_tree (pp
, t
);
2048 /* Pretty-print a template parameter in the canonical form
2049 "template-parameter-<level>-<position in parameter list>". */
2052 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2054 const enum tree_code code
= TREE_CODE (parm
);
2056 /* Brings type template parameters to the canonical forms. */
2057 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2058 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2059 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2061 pp_cxx_begin_template_argument_list (pp
);
2062 pp_cxx_ws_string (pp
, M_("template-parameter-"));
2063 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2065 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2066 pp_cxx_end_template_argument_list (pp
);
2070 template-declaration:
2071 export(opt) template < template-parameter-list > declaration */
2074 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2076 tree tmpl
= most_general_template (t
);
2080 pp_maybe_newline_and_indent (pp
, 0);
2081 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2083 pp_cxx_ws_string (pp
, "template");
2084 pp_cxx_begin_template_argument_list (pp
);
2085 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2086 pp_cxx_end_template_argument_list (pp
);
2087 pp_newline_and_indent (pp
, 3);
2090 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2091 pp_cxx_function_definition (pp
, t
);
2093 pp_cxx_simple_declaration (pp
, t
);
2097 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2099 pp_unsupported_tree (pp
, t
);
2103 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2105 pp_unsupported_tree (pp
, t
);
2112 template-declaration
2113 explicit-instantiation
2114 explicit-specialization
2115 linkage-specification
2116 namespace-definition
2121 namespace-alias-definition
2124 static_assert-declaration */
2126 pp_cxx_declaration (cxx_pretty_printer
*pp
, tree t
)
2128 if (TREE_CODE (t
) == STATIC_ASSERT
)
2130 pp_cxx_ws_string (pp
, "static_assert");
2131 pp_cxx_left_paren (pp
);
2132 pp_cxx_expression (pp
, STATIC_ASSERT_CONDITION (t
));
2133 pp_cxx_separate_with (pp
, ',');
2134 pp_cxx_expression (pp
, STATIC_ASSERT_MESSAGE (t
));
2135 pp_cxx_right_paren (pp
);
2137 else if (!DECL_LANG_SPECIFIC (t
))
2138 pp_cxx_simple_declaration (pp
, t
);
2139 else if (DECL_USE_TEMPLATE (t
))
2140 switch (DECL_USE_TEMPLATE (t
))
2143 pp_cxx_template_declaration (pp
, t
);
2147 pp_cxx_explicit_specialization (pp
, t
);
2151 pp_cxx_explicit_instantiation (pp
, t
);
2157 else switch (TREE_CODE (t
))
2161 pp_cxx_simple_declaration (pp
, t
);
2165 if (DECL_SAVED_TREE (t
))
2166 pp_cxx_function_definition (pp
, t
);
2168 pp_cxx_simple_declaration (pp
, t
);
2171 case NAMESPACE_DECL
:
2172 if (DECL_NAMESPACE_ALIAS (t
))
2173 pp_cxx_namespace_alias_definition (pp
, t
);
2175 pp_cxx_original_namespace_definition (pp
, t
);
2179 pp_unsupported_tree (pp
, t
);
2185 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2187 t
= TREE_OPERAND (t
, 0);
2188 pp_cxx_ws_string (pp
, "typeid");
2189 pp_cxx_left_paren (pp
);
2191 pp_cxx_type_id (pp
, t
);
2193 pp_cxx_expression (pp
, t
);
2194 pp_cxx_right_paren (pp
);
2198 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2200 pp_cxx_ws_string (pp
, "va_arg");
2201 pp_cxx_left_paren (pp
);
2202 pp_cxx_assignment_expression (pp
, TREE_OPERAND (t
, 0));
2203 pp_cxx_separate_with (pp
, ',');
2204 pp_cxx_type_id (pp
, TREE_TYPE (t
));
2205 pp_cxx_right_paren (pp
);
2209 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2211 switch (TREE_CODE (t
))
2214 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2215 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2217 pp_cxx_type_id (pp
, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2218 pp_cxx_separate_with (pp
, ',');
2223 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2225 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2227 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2230 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2232 pp_left_bracket (pp
);
2233 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2234 pp_right_bracket (pp
);
2242 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2244 pp_cxx_ws_string (pp
, "offsetof");
2245 pp_cxx_left_paren (pp
);
2246 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2247 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
2248 pp_cxx_right_paren (pp
);
2252 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2254 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2258 case CPTK_HAS_NOTHROW_ASSIGN
:
2259 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2261 case CPTK_HAS_TRIVIAL_ASSIGN
:
2262 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2264 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2265 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2267 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2268 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2270 case CPTK_HAS_NOTHROW_COPY
:
2271 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2273 case CPTK_HAS_TRIVIAL_COPY
:
2274 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2276 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2277 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2279 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2280 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2282 case CPTK_IS_ABSTRACT
:
2283 pp_cxx_ws_string (pp
, "__is_abstract");
2285 case CPTK_IS_BASE_OF
:
2286 pp_cxx_ws_string (pp
, "__is_base_of");
2289 pp_cxx_ws_string (pp
, "__is_class");
2291 case CPTK_IS_CONVERTIBLE_TO
:
2292 pp_cxx_ws_string (pp
, "__is_convertible_to");
2295 pp_cxx_ws_string (pp
, "__is_empty");
2298 pp_cxx_ws_string (pp
, "__is_enum");
2301 pp_cxx_ws_string (pp
, "__is_pod");
2303 case CPTK_IS_POLYMORPHIC
:
2304 pp_cxx_ws_string (pp
, "__is_polymorphic");
2306 case CPTK_IS_STD_LAYOUT
:
2307 pp_cxx_ws_string (pp
, "__is_std_layout");
2309 case CPTK_IS_TRIVIAL
:
2310 pp_cxx_ws_string (pp
, "__is_trivial");
2313 pp_cxx_ws_string (pp
, "__is_union");
2320 pp_cxx_left_paren (pp
);
2321 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE1 (t
));
2323 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2325 pp_cxx_separate_with (pp
, ',');
2326 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE2 (t
));
2329 pp_cxx_right_paren (pp
);
2332 typedef c_pretty_print_fn pp_fun
;
2334 /* Initialization of a C++ pretty-printer object. */
2337 pp_cxx_pretty_printer_init (cxx_pretty_printer
*pp
)
2339 pp_c_pretty_printer_init (pp_c_base (pp
));
2340 pp_set_line_maximum_length (pp
, 0);
2342 pp
->c_base
.declaration
= (pp_fun
) pp_cxx_declaration
;
2343 pp
->c_base
.declaration_specifiers
= (pp_fun
) pp_cxx_decl_specifier_seq
;
2344 pp
->c_base
.function_specifier
= (pp_fun
) pp_cxx_function_specifier
;
2345 pp
->c_base
.type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2346 pp
->c_base
.declarator
= (pp_fun
) pp_cxx_declarator
;
2347 pp
->c_base
.direct_declarator
= (pp_fun
) pp_cxx_direct_declarator
;
2348 pp
->c_base
.parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;
2349 pp
->c_base
.type_id
= (pp_fun
) pp_cxx_type_id
;
2350 pp
->c_base
.abstract_declarator
= (pp_fun
) pp_cxx_abstract_declarator
;
2351 pp
->c_base
.direct_abstract_declarator
=
2352 (pp_fun
) pp_cxx_direct_abstract_declarator
;
2353 pp
->c_base
.simple_type_specifier
= (pp_fun
)pp_cxx_simple_type_specifier
;
2355 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2357 pp
->c_base
.constant
= (pp_fun
) pp_cxx_constant
;
2358 pp
->c_base
.id_expression
= (pp_fun
) pp_cxx_id_expression
;
2359 pp
->c_base
.primary_expression
= (pp_fun
) pp_cxx_primary_expression
;
2360 pp
->c_base
.postfix_expression
= (pp_fun
) pp_cxx_postfix_expression
;
2361 pp
->c_base
.unary_expression
= (pp_fun
) pp_cxx_unary_expression
;
2362 pp
->c_base
.multiplicative_expression
= (pp_fun
) pp_cxx_multiplicative_expression
;
2363 pp
->c_base
.conditional_expression
= (pp_fun
) pp_cxx_conditional_expression
;
2364 pp
->c_base
.assignment_expression
= (pp_fun
) pp_cxx_assignment_expression
;
2365 pp
->c_base
.expression
= (pp_fun
) pp_cxx_expression
;
2366 pp
->enclosing_scope
= global_namespace
;