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
, INNERMOST_TEMPLATE_ARGS
207 (CLASSTYPE_TI_ARGS (t
)));
208 pp_cxx_end_template_argument_list (pp
);
213 pp_cxx_complement (pp
);
214 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
217 case TEMPLATE_TYPE_PARM
:
218 case TEMPLATE_TEMPLATE_PARM
:
219 if (TYPE_IDENTIFIER (t
))
220 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
222 pp_cxx_canonical_template_parameter (pp
, t
);
225 case TEMPLATE_PARM_INDEX
:
226 pp_cxx_unqualified_id (pp
, TEMPLATE_PARM_DECL (t
));
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
);
238 pp_unsupported_tree (pp
, t
);
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. */
249 pp_cxx_template_keyword_if_needed (cxx_pretty_printer
*pp
, tree scope
, tree t
)
251 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
252 && TYPE_P (scope
) && dependent_type_p (scope
))
253 pp_cxx_ws_string (pp
, "template");
256 /* nested-name-specifier:
257 class-or-namespace-name :: nested-name-specifier(opt)
258 class-or-namespace-name :: template nested-name-specifier */
261 pp_cxx_nested_name_specifier (cxx_pretty_printer
*pp
, tree t
)
263 if (t
!= NULL
&& t
!= pp
->enclosing_scope
)
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
);
274 nested-name-specifier template(opt) unqualified-id */
277 pp_cxx_qualified_id (cxx_pretty_printer
*pp
, tree t
)
279 switch (TREE_CODE (t
))
281 /* A pointer-to-member is always qualified. */
283 pp_cxx_nested_name_specifier (pp
, PTRMEM_CST_CLASS (t
));
284 pp_cxx_unqualified_id (pp
, PTRMEM_CST_MEMBER (t
));
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. */
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
));
307 pp_cxx_nested_name_specifier (pp
, TREE_OPERAND (t
, 0));
308 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 1));
313 tree scope
= TYPE_P (t
) ? TYPE_CONTEXT (t
) : DECL_CONTEXT (t
);
314 if (scope
!= pp
->enclosing_scope
)
316 pp_cxx_nested_name_specifier (pp
, scope
);
317 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
319 pp_cxx_unqualified_id (pp
, t
);
327 pp_cxx_constant (cxx_pretty_printer
*pp
, tree t
)
329 switch (TREE_CODE (t
))
333 const bool in_parens
= PAREN_STRING_LITERAL_P (t
);
335 pp_cxx_left_paren (pp
);
336 pp_c_constant (pp_c_base (pp
), t
);
338 pp_cxx_right_paren (pp
);
343 if (TREE_CODE (TREE_TYPE (t
)) == NULLPTR_TYPE
)
345 pp_string (pp
, "nullptr");
348 /* else fall through. */
351 pp_c_constant (pp_c_base (pp
), t
);
361 pp_cxx_id_expression (cxx_pretty_printer
*pp
, tree t
)
363 if (TREE_CODE (t
) == OVERLOAD
)
365 if (DECL_P (t
) && DECL_CONTEXT (t
))
366 pp_cxx_qualified_id (pp
, t
);
368 pp_cxx_unqualified_id (pp
, t
);
371 /* primary-expression:
375 :: operator-function-id
381 __builtin_va_arg ( assignment-expression , type-id )
382 __builtin_offsetof ( type-id, offsetof-expression )
384 __has_nothrow_assign ( type-id )
385 __has_nothrow_constructor ( type-id )
386 __has_nothrow_copy ( type-id )
387 __has_trivial_assign ( type-id )
388 __has_trivial_constructor ( type-id )
389 __has_trivial_copy ( type-id )
390 __has_trivial_destructor ( type-id )
391 __has_virtual_destructor ( type-id )
392 __is_abstract ( type-id )
393 __is_base_of ( type-id , type-id )
394 __is_class ( type-id )
395 __is_convertible_to ( type-id , type-id )
396 __is_empty ( type-id )
397 __is_enum ( type-id )
399 __is_polymorphic ( type-id )
400 __is_union ( type-id ) */
403 pp_cxx_primary_expression (cxx_pretty_printer
*pp
, tree t
)
405 switch (TREE_CODE (t
))
411 pp_cxx_constant (pp
, t
);
415 t
= BASELINK_FUNCTIONS (t
);
423 pp_cxx_id_expression (pp
, t
);
427 case TEMPLATE_TYPE_PARM
:
428 case TEMPLATE_TEMPLATE_PARM
:
429 case TEMPLATE_PARM_INDEX
:
430 pp_cxx_unqualified_id (pp
, t
);
434 pp_cxx_left_paren (pp
);
435 pp_cxx_statement (pp
, STMT_EXPR_STMT (t
));
436 pp_cxx_right_paren (pp
);
440 pp_cxx_trait_expression (pp
, t
);
444 pp_cxx_va_arg_expression (pp
, t
);
448 pp_cxx_offsetof_expression (pp
, t
);
452 pp_c_primary_expression (pp_c_base (pp
), t
);
457 /* postfix-expression:
459 postfix-expression [ expression ]
460 postfix-expression ( expression-list(opt) )
461 simple-type-specifier ( expression-list(opt) )
462 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
463 typename ::(opt) nested-name-specifier template(opt)
464 template-id ( expression-list(opt) )
465 postfix-expression . template(opt) ::(opt) id-expression
466 postfix-expression -> template(opt) ::(opt) id-expression
467 postfix-expression . pseudo-destructor-name
468 postfix-expression -> pseudo-destructor-name
469 postfix-expression ++
470 postfix-expression --
471 dynamic_cast < type-id > ( expression )
472 static_cast < type-id > ( expression )
473 reinterpret_cast < type-id > ( expression )
474 const_cast < type-id > ( expression )
475 typeid ( expression )
476 typeid ( type-id ) */
479 pp_cxx_postfix_expression (cxx_pretty_printer
*pp
, tree t
)
481 enum tree_code code
= TREE_CODE (t
);
488 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
490 tree saved_scope
= pp
->enclosing_scope
;
491 bool skipfirst
= false;
494 if (TREE_CODE (fun
) == ADDR_EXPR
)
495 fun
= TREE_OPERAND (fun
, 0);
497 /* In templates, where there is no way to tell whether a given
498 call uses an actual member function. So the parser builds
499 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
500 instantiation time. */
501 if (TREE_CODE (fun
) != FUNCTION_DECL
)
503 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
505 tree object
= (code
== AGGR_INIT_EXPR
506 ? (AGGR_INIT_VIA_CTOR_P (t
)
507 ? AGGR_INIT_EXPR_SLOT (t
)
508 : AGGR_INIT_EXPR_ARG (t
, 0))
509 : CALL_EXPR_ARG (t
, 0));
511 while (TREE_CODE (object
) == NOP_EXPR
)
512 object
= TREE_OPERAND (object
, 0);
514 if (TREE_CODE (object
) == ADDR_EXPR
)
515 object
= TREE_OPERAND (object
, 0);
517 if (TREE_CODE (TREE_TYPE (object
)) != POINTER_TYPE
)
519 pp_cxx_postfix_expression (pp
, object
);
524 pp_cxx_postfix_expression (pp
, object
);
528 pp
->enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
531 pp_cxx_postfix_expression (pp
, fun
);
532 pp
->enclosing_scope
= saved_scope
;
533 pp_cxx_left_paren (pp
);
534 if (code
== AGGR_INIT_EXPR
)
536 aggr_init_expr_arg_iterator iter
;
537 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
543 pp_cxx_expression (pp
, arg
);
544 if (more_aggr_init_expr_args_p (&iter
))
545 pp_cxx_separate_with (pp
, ',');
551 call_expr_arg_iterator iter
;
552 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
558 pp_cxx_expression (pp
, arg
);
559 if (more_call_expr_args_p (&iter
))
560 pp_cxx_separate_with (pp
, ',');
564 pp_cxx_right_paren (pp
);
566 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
568 pp_cxx_separate_with (pp
, ',');
569 pp_cxx_postfix_expression (pp
, AGGR_INIT_EXPR_SLOT (t
));
582 pp_cxx_primary_expression (pp
, t
);
585 case DYNAMIC_CAST_EXPR
:
586 case STATIC_CAST_EXPR
:
587 case REINTERPRET_CAST_EXPR
:
588 case CONST_CAST_EXPR
:
589 if (code
== DYNAMIC_CAST_EXPR
)
590 pp_cxx_ws_string (pp
, "dynamic_cast");
591 else if (code
== STATIC_CAST_EXPR
)
592 pp_cxx_ws_string (pp
, "static_cast");
593 else if (code
== REINTERPRET_CAST_EXPR
)
594 pp_cxx_ws_string (pp
, "reinterpret_cast");
596 pp_cxx_ws_string (pp
, "const_cast");
597 pp_cxx_begin_template_argument_list (pp
);
598 pp_cxx_type_id (pp
, TREE_TYPE (t
));
599 pp_cxx_end_template_argument_list (pp
);
601 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
605 case EMPTY_CLASS_EXPR
:
606 pp_cxx_type_id (pp
, TREE_TYPE (t
));
612 pp_cxx_typeid_expression (pp
, t
);
615 case PSEUDO_DTOR_EXPR
:
616 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
618 pp_cxx_qualified_id (pp
, TREE_OPERAND (t
, 1));
619 pp_cxx_colon_colon (pp
);
621 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 2));
625 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
630 pp_c_postfix_expression (pp_c_base (pp
), t
);
636 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
637 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
643 type-specifier-seq new-declarator(opt)
646 ptr-operator new-declarator(opt)
647 direct-new-declarator
649 direct-new-declarator
651 direct-new-declarator [ constant-expression ]
654 ( expression-list(opt) ) */
657 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
659 enum tree_code code
= TREE_CODE (t
);
660 tree type
= TREE_OPERAND (t
, 1);
661 tree init
= TREE_OPERAND (t
, 2);
666 if (NEW_EXPR_USE_GLOBAL (t
))
667 pp_cxx_colon_colon (pp
);
668 pp_cxx_ws_string (pp
, "new");
669 if (TREE_OPERAND (t
, 0))
671 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
674 if (TREE_CODE (type
) == ARRAY_REF
)
675 type
= build_cplus_array_type
676 (TREE_OPERAND (type
, 0),
677 build_index_type (fold_build2_loc (input_location
,
678 MINUS_EXPR
, integer_type_node
,
679 TREE_OPERAND (type
, 1),
681 pp_cxx_type_id (pp
, type
);
685 if (TREE_CODE (init
) == TREE_LIST
)
686 pp_c_expression_list (pp_c_base (pp
), init
);
687 else if (init
== void_zero_node
)
688 ; /* OK, empty initializer list. */
690 pp_cxx_expression (pp
, init
);
696 pp_unsupported_tree (pp
, t
);
700 /* delete-expression:
701 ::(opt) delete cast-expression
702 ::(opt) delete [ ] cast-expression */
705 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
707 enum tree_code code
= TREE_CODE (t
);
711 case VEC_DELETE_EXPR
:
712 if (DELETE_EXPR_USE_GLOBAL (t
))
713 pp_cxx_colon_colon (pp
);
714 pp_cxx_ws_string (pp
, "delete");
716 if (code
== VEC_DELETE_EXPR
717 || DELETE_EXPR_USE_VEC (t
))
719 pp_left_bracket (pp
);
720 pp_right_bracket (pp
);
723 pp_c_cast_expression (pp_c_base (pp
), TREE_OPERAND (t
, 0));
727 pp_unsupported_tree (pp
, t
);
735 unary-operator cast-expression
736 sizeof unary-expression
738 sizeof ... ( identifier )
742 unary-operator: one of
746 __alignof__ unary-expression
747 __alignof__ ( type-id ) */
750 pp_cxx_unary_expression (cxx_pretty_printer
*pp
, tree t
)
752 enum tree_code code
= TREE_CODE (t
);
757 pp_cxx_new_expression (pp
, t
);
761 case VEC_DELETE_EXPR
:
762 pp_cxx_delete_expression (pp
, t
);
766 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
768 pp_cxx_ws_string (pp
, "sizeof");
769 pp_cxx_ws_string (pp
, "...");
770 pp_cxx_whitespace (pp
);
771 pp_cxx_left_paren (pp
);
772 if (TYPE_P (TREE_OPERAND (t
, 0)))
773 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
775 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
776 pp_cxx_right_paren (pp
);
782 pp_cxx_ws_string (pp
, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
783 pp_cxx_whitespace (pp
);
784 if (TYPE_P (TREE_OPERAND (t
, 0)))
786 pp_cxx_left_paren (pp
);
787 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
788 pp_cxx_right_paren (pp
);
791 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
794 case UNARY_PLUS_EXPR
:
796 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 0));
800 pp_c_unary_expression (pp_c_base (pp
), t
);
807 ( type-id ) cast-expression */
810 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
812 switch (TREE_CODE (t
))
815 pp_cxx_type_id (pp
, TREE_TYPE (t
));
816 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
820 pp_c_cast_expression (pp_c_base (pp
), t
);
827 pm-expression .* cast-expression
828 pm-expression ->* cast-expression */
831 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
833 switch (TREE_CODE (t
))
835 /* Handle unfortunate OFFSET_REF overloading here. */
837 if (TYPE_P (TREE_OPERAND (t
, 0)))
839 pp_cxx_qualified_id (pp
, t
);
842 /* Else fall through. */
845 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
846 if (TREE_CODE (t
) == MEMBER_REF
)
851 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
856 pp_cxx_cast_expression (pp
, t
);
861 /* multiplicative-expression:
863 multiplicative-expression * pm-expression
864 multiplicative-expression / pm-expression
865 multiplicative-expression % pm-expression */
868 pp_cxx_multiplicative_expression (cxx_pretty_printer
*pp
, tree e
)
870 enum tree_code code
= TREE_CODE (e
);
876 pp_cxx_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
878 if (code
== MULT_EXPR
)
880 else if (code
== TRUNC_DIV_EXPR
)
885 pp_cxx_pm_expression (pp
, TREE_OPERAND (e
, 1));
889 pp_cxx_pm_expression (pp
, e
);
894 /* conditional-expression:
895 logical-or-expression
896 logical-or-expression ? expression : assignment-expression */
899 pp_cxx_conditional_expression (cxx_pretty_printer
*pp
, tree e
)
901 if (TREE_CODE (e
) == COND_EXPR
)
903 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
907 pp_cxx_expression (pp
, TREE_OPERAND (e
, 1));
909 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
912 pp_c_logical_or_expression (pp_c_base (pp
), e
);
915 /* Pretty-print a compound assignment operator token as indicated by T. */
918 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
922 switch (TREE_CODE (t
))
945 op
= tree_code_name
[TREE_CODE (t
)];
949 pp_cxx_ws_string (pp
, op
);
953 /* assignment-expression:
954 conditional-expression
955 logical-or-expression assignment-operator assignment-expression
959 throw assignment-expression(opt)
961 assignment-operator: one of
962 = *= /= %= += -= >>= <<= &= ^= |= */
965 pp_cxx_assignment_expression (cxx_pretty_printer
*pp
, tree e
)
967 switch (TREE_CODE (e
))
971 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
975 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 1));
979 pp_cxx_ws_string (pp
, "throw");
980 if (TREE_OPERAND (e
, 0))
981 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 0));
985 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
986 pp_cxx_assignment_operator (pp
, TREE_OPERAND (e
, 1));
987 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
991 pp_cxx_conditional_expression (pp
, e
);
997 pp_cxx_expression (cxx_pretty_printer
*pp
, tree t
)
999 switch (TREE_CODE (t
))
1005 pp_cxx_constant (pp
, t
);
1009 pp_cxx_unqualified_id (pp
, t
);
1017 pp_cxx_qualified_id (pp
, t
);
1021 t
= OVL_CURRENT (t
);
1029 case TEMPLATE_TYPE_PARM
:
1030 case TEMPLATE_PARM_INDEX
:
1031 case TEMPLATE_TEMPLATE_PARM
:
1033 pp_cxx_primary_expression (pp
, t
);
1037 case DYNAMIC_CAST_EXPR
:
1038 case STATIC_CAST_EXPR
:
1039 case REINTERPRET_CAST_EXPR
:
1040 case CONST_CAST_EXPR
:
1044 case EMPTY_CLASS_EXPR
:
1046 case PSEUDO_DTOR_EXPR
:
1047 case AGGR_INIT_EXPR
:
1049 pp_cxx_postfix_expression (pp
, t
);
1054 pp_cxx_new_expression (pp
, t
);
1058 case VEC_DELETE_EXPR
:
1059 pp_cxx_delete_expression (pp
, t
);
1064 pp_cxx_unary_expression (pp
, t
);
1068 pp_cxx_cast_expression (pp
, t
);
1074 pp_cxx_pm_expression (pp
, t
);
1078 case TRUNC_DIV_EXPR
:
1079 case TRUNC_MOD_EXPR
:
1080 pp_cxx_multiplicative_expression (pp
, t
);
1084 pp_cxx_conditional_expression (pp
, t
);
1091 pp_cxx_assignment_expression (pp
, t
);
1094 case NON_DEPENDENT_EXPR
:
1095 case MUST_NOT_THROW_EXPR
:
1096 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
1099 case EXPR_PACK_EXPANSION
:
1100 pp_cxx_expression (pp
, PACK_EXPANSION_PATTERN (t
));
1101 pp_cxx_ws_string (pp
, "...");
1104 case TEMPLATE_ID_EXPR
:
1105 pp_cxx_template_id (pp
, t
);
1108 case NONTYPE_ARGUMENT_PACK
:
1110 tree args
= ARGUMENT_PACK_ARGS (t
);
1111 int i
, len
= TREE_VEC_LENGTH (args
);
1112 for (i
= 0; i
< len
; ++i
)
1115 pp_cxx_separate_with (pp
, ',');
1116 pp_cxx_expression (pp
, TREE_VEC_ELT (args
, i
));
1122 pp_c_expression (pp_c_base (pp
), t
);
1130 /* function-specifier:
1136 pp_cxx_function_specifier (cxx_pretty_printer
*pp
, tree t
)
1138 switch (TREE_CODE (t
))
1141 if (DECL_VIRTUAL_P (t
))
1142 pp_cxx_ws_string (pp
, "virtual");
1143 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1144 pp_cxx_ws_string (pp
, "explicit");
1146 pp_c_function_specifier (pp_c_base (pp
), t
);
1153 /* decl-specifier-seq:
1154 decl-specifier-seq(opt) decl-specifier
1157 storage-class-specifier
1164 pp_cxx_decl_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1166 switch (TREE_CODE (t
))
1172 pp_cxx_storage_class_specifier (pp
, t
);
1173 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1177 pp_cxx_ws_string (pp
, "typedef");
1178 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1182 /* Constructors don't have return types. And conversion functions
1183 do not have a type-specifier in their return types. */
1184 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1185 pp_cxx_function_specifier (pp
, t
);
1186 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1187 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (t
)));
1190 pp_c_declaration_specifiers (pp_c_base (pp
), t
);
1195 /* simple-type-specifier:
1196 ::(opt) nested-name-specifier(opt) type-name
1197 ::(opt) nested-name-specifier(opt) template(opt) template-id
1211 pp_cxx_simple_type_specifier (cxx_pretty_printer
*pp
, tree t
)
1213 switch (TREE_CODE (t
))
1218 pp_cxx_qualified_id (pp
, t
);
1221 case TEMPLATE_TYPE_PARM
:
1222 case TEMPLATE_TEMPLATE_PARM
:
1223 case TEMPLATE_PARM_INDEX
:
1224 pp_cxx_unqualified_id (pp
, t
);
1228 pp_cxx_ws_string (pp
, "typename");
1229 pp_cxx_nested_name_specifier (pp
, TYPE_CONTEXT (t
));
1230 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
1234 pp_c_type_specifier (pp_c_base (pp
), t
);
1239 /* type-specifier-seq:
1240 type-specifier type-specifier-seq(opt)
1243 simple-type-specifier
1246 elaborated-type-specifier
1250 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1252 switch (TREE_CODE (t
))
1255 case TEMPLATE_TYPE_PARM
:
1256 case TEMPLATE_TEMPLATE_PARM
:
1258 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1259 pp_cxx_cv_qualifier_seq (pp
, t
);
1260 pp_cxx_simple_type_specifier (pp
, t
);
1264 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1265 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1266 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1270 pp_cxx_ws_string (pp
, "decltype");
1271 pp_cxx_left_paren (pp
);
1272 pp_cxx_expression (pp
, DECLTYPE_TYPE_EXPR (t
));
1273 pp_cxx_right_paren (pp
);
1277 if (TYPE_PTRMEMFUNC_P (t
))
1279 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1280 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (pfm
)));
1281 pp_cxx_whitespace (pp
);
1282 pp_cxx_ptr_operator (pp
, t
);
1285 /* else fall through */
1288 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1289 pp_c_specifier_qualifier_list (pp_c_base (pp
), t
);
1294 * cv-qualifier-seq(opt)
1296 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1299 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1301 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1303 switch (TREE_CODE (t
))
1305 case REFERENCE_TYPE
:
1307 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
1308 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t
)))
1309 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1310 if (TREE_CODE (t
) == POINTER_TYPE
)
1313 pp_cxx_cv_qualifier_seq (pp
, t
);
1320 if (TYPE_PTRMEMFUNC_P (t
))
1322 pp_cxx_left_paren (pp
);
1323 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1328 if (TYPE_PTR_TO_MEMBER_P (t
))
1330 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1331 pp_cxx_left_paren (pp
);
1332 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1334 pp_cxx_cv_qualifier_seq (pp
, t
);
1337 /* else fall through. */
1340 pp_unsupported_tree (pp
, t
);
1346 pp_cxx_implicit_parameter_type (tree mf
)
1348 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf
))));
1352 parameter-declaration:
1353 decl-specifier-seq declarator
1354 decl-specifier-seq declarator = assignment-expression
1355 decl-specifier-seq abstract-declarator(opt)
1356 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1359 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1361 pp_cxx_decl_specifier_seq (pp
, t
);
1363 pp_cxx_abstract_declarator (pp
, t
);
1365 pp_cxx_declarator (pp
, t
);
1368 /* parameter-declaration-clause:
1369 parameter-declaration-list(opt) ...(opt)
1370 parameter-declaration-list , ...
1372 parameter-declaration-list:
1373 parameter-declaration
1374 parameter-declaration-list , parameter-declaration */
1377 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1379 tree args
= TYPE_P (t
) ? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1381 TYPE_P (t
) ? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1382 const bool abstract
= args
== NULL
1383 || pp_c_base (pp
)->flags
& pp_c_flag_abstract
;
1386 /* Skip artificial parameter for nonstatic member functions. */
1387 if (TREE_CODE (t
) == METHOD_TYPE
)
1388 types
= TREE_CHAIN (types
);
1390 pp_cxx_left_paren (pp
);
1391 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1394 pp_cxx_separate_with (pp
, ',');
1396 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1397 if (!abstract
&& pp_c_base (pp
)->flags
& pp_cxx_flag_default_argument
)
1399 pp_cxx_whitespace (pp
);
1401 pp_cxx_whitespace (pp
);
1402 pp_cxx_assignment_expression (pp
, TREE_PURPOSE (types
));
1405 pp_cxx_right_paren (pp
);
1408 /* exception-specification:
1409 throw ( type-id-list(opt) )
1413 type-id-list , type-id */
1416 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1418 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1419 bool need_comma
= false;
1421 if (!TYPE_NOTHROW_P (t
) && ex_spec
== NULL
)
1423 pp_cxx_ws_string (pp
, "throw");
1424 pp_cxx_left_paren (pp
);
1425 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1427 tree type
= TREE_VALUE (ex_spec
);
1428 tree argpack
= NULL_TREE
;
1431 if (ARGUMENT_PACK_P (type
))
1433 argpack
= ARGUMENT_PACK_ARGS (type
);
1434 len
= TREE_VEC_LENGTH (argpack
);
1437 for (i
= 0; i
< len
; ++i
)
1440 type
= TREE_VEC_ELT (argpack
, i
);
1443 pp_cxx_separate_with (pp
, ',');
1447 pp_cxx_type_id (pp
, type
);
1450 pp_cxx_right_paren (pp
);
1453 /* direct-declarator:
1455 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1456 exception-specification(opt)
1457 direct-declaration [ constant-expression(opt) ]
1461 pp_cxx_direct_declarator (cxx_pretty_printer
*pp
, tree t
)
1463 switch (TREE_CODE (t
))
1471 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1473 if ((TREE_CODE (t
) == PARM_DECL
&& FUNCTION_PARAMETER_PACK_P (t
))
1474 || template_parameter_pack_p (t
))
1475 /* A function parameter pack or non-type template
1477 pp_cxx_ws_string (pp
, "...");
1479 pp_cxx_id_expression (pp
, DECL_NAME (t
));
1481 pp_cxx_abstract_declarator (pp
, TREE_TYPE (t
));
1485 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
1486 pp_cxx_id_expression (pp
, t
);
1487 pp_cxx_parameter_declaration_clause (pp
, t
);
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1491 pp_base (pp
)->padding
= pp_before
;
1492 pp_cxx_cv_qualifier_seq (pp
, pp_cxx_implicit_parameter_type (t
));
1495 pp_cxx_exception_specification (pp
, TREE_TYPE (t
));
1500 case TEMPLATE_TYPE_PARM
:
1501 case TEMPLATE_PARM_INDEX
:
1502 case TEMPLATE_TEMPLATE_PARM
:
1506 pp_c_direct_declarator (pp_c_base (pp
), t
);
1513 ptr-operator declarator */
1516 pp_cxx_declarator (cxx_pretty_printer
*pp
, tree t
)
1518 pp_cxx_direct_declarator (pp
, t
);
1521 /* ctor-initializer:
1522 : mem-initializer-list
1524 mem-initializer-list:
1526 mem-initializer , mem-initializer-list
1529 mem-initializer-id ( expression-list(opt) )
1532 ::(opt) nested-name-specifier(opt) class-name
1536 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1538 t
= TREE_OPERAND (t
, 0);
1539 pp_cxx_whitespace (pp
);
1541 pp_cxx_whitespace (pp
);
1542 for (; t
; t
= TREE_CHAIN (t
))
1544 tree purpose
= TREE_PURPOSE (t
);
1545 bool is_pack
= PACK_EXPANSION_P (purpose
);
1548 pp_cxx_primary_expression (pp
, PACK_EXPANSION_PATTERN (purpose
));
1550 pp_cxx_primary_expression (pp
, purpose
);
1551 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1553 pp_cxx_ws_string (pp
, "...");
1555 pp_cxx_separate_with (pp
, ',');
1559 /* function-definition:
1560 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1561 decl-specifier-seq(opt) declarator function-try-block */
1564 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1566 tree saved_scope
= pp
->enclosing_scope
;
1567 pp_cxx_decl_specifier_seq (pp
, t
);
1568 pp_cxx_declarator (pp
, t
);
1569 pp_needs_newline (pp
) = true;
1570 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1571 if (DECL_SAVED_TREE (t
))
1572 pp_cxx_statement (pp
, DECL_SAVED_TREE (t
));
1575 pp_cxx_semicolon (pp
);
1576 pp_needs_newline (pp
) = true;
1579 pp
->enclosing_scope
= saved_scope
;
1582 /* abstract-declarator:
1583 ptr-operator abstract-declarator(opt)
1584 direct-abstract-declarator */
1587 pp_cxx_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1589 if (TYPE_PTRMEM_P (t
) || TYPE_PTRMEMFUNC_P (t
))
1590 pp_cxx_right_paren (pp
);
1591 else if (POINTER_TYPE_P (t
))
1593 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1594 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1595 pp_cxx_right_paren (pp
);
1598 pp_cxx_direct_abstract_declarator (pp
, t
);
1601 /* direct-abstract-declarator:
1602 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1603 cv-qualifier-seq(opt) exception-specification(opt)
1604 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1605 ( abstract-declarator ) */
1608 pp_cxx_direct_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1610 switch (TREE_CODE (t
))
1612 case REFERENCE_TYPE
:
1613 pp_cxx_abstract_declarator (pp
, t
);
1617 if (TYPE_PTRMEMFUNC_P (t
))
1618 pp_cxx_direct_abstract_declarator (pp
, TYPE_PTRMEMFUNC_FN_TYPE (t
));
1623 pp_cxx_parameter_declaration_clause (pp
, t
);
1624 pp_cxx_direct_abstract_declarator (pp
, TREE_TYPE (t
));
1625 if (TREE_CODE (t
) == METHOD_TYPE
)
1627 pp_base (pp
)->padding
= pp_before
;
1628 pp_cxx_cv_qualifier_seq
1629 (pp
, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))));
1631 pp_cxx_exception_specification (pp
, t
);
1635 case TEMPLATE_TYPE_PARM
:
1636 case TEMPLATE_TEMPLATE_PARM
:
1637 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1638 case UNBOUND_CLASS_TEMPLATE
:
1642 pp_c_direct_abstract_declarator (pp_c_base (pp
), t
);
1648 type-specifier-seq abstract-declarator(opt) */
1651 pp_cxx_type_id (cxx_pretty_printer
*pp
, tree t
)
1653 pp_flags saved_flags
= pp_c_base (pp
)->flags
;
1654 pp_c_base (pp
)->flags
|= pp_c_flag_abstract
;
1656 switch (TREE_CODE (t
))
1663 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1664 case UNBOUND_CLASS_TEMPLATE
:
1665 case TEMPLATE_TEMPLATE_PARM
:
1666 case TEMPLATE_TYPE_PARM
:
1667 case TEMPLATE_PARM_INDEX
:
1671 case TEMPLATE_ID_EXPR
:
1672 pp_cxx_type_specifier_seq (pp
, t
);
1675 case TYPE_PACK_EXPANSION
:
1676 pp_cxx_type_id (pp
, PACK_EXPANSION_PATTERN (t
));
1677 pp_cxx_ws_string (pp
, "...");
1681 pp_c_type_id (pp_c_base (pp
), t
);
1685 pp_c_base (pp
)->flags
= saved_flags
;
1688 /* template-argument-list:
1689 template-argument ...(opt)
1690 template-argument-list, template-argument ...(opt)
1693 assignment-expression
1698 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1701 bool need_comma
= false;
1705 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1707 tree arg
= TREE_VEC_ELT (t
, i
);
1708 tree argpack
= NULL_TREE
;
1711 if (ARGUMENT_PACK_P (arg
))
1713 argpack
= ARGUMENT_PACK_ARGS (arg
);
1714 len
= TREE_VEC_LENGTH (argpack
);
1717 for (idx
= 0; idx
< len
; idx
++)
1720 arg
= TREE_VEC_ELT (argpack
, idx
);
1723 pp_cxx_separate_with (pp
, ',');
1727 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1728 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1729 pp_cxx_type_id (pp
, arg
);
1731 pp_cxx_expression (pp
, arg
);
1738 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1740 t
= DECL_EXPR_DECL (t
);
1741 pp_cxx_type_specifier_seq (pp
, t
);
1743 pp_cxx_abstract_declarator (pp
, t
);
1745 pp_cxx_declarator (pp
, t
);
1751 pp_cxx_statement (cxx_pretty_printer
*pp
, tree t
)
1753 switch (TREE_CODE (t
))
1755 case CTOR_INITIALIZER
:
1756 pp_cxx_ctor_initializer (pp
, t
);
1760 pp_cxx_ws_string (pp
, "using");
1761 pp_cxx_ws_string (pp
, "namespace");
1762 if (DECL_CONTEXT (t
))
1763 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1764 pp_cxx_qualified_id (pp
, USING_STMT_NAMESPACE (t
));
1768 pp_cxx_ws_string (pp
, "using");
1769 pp_cxx_nested_name_specifier (pp
, USING_DECL_SCOPE (t
));
1770 pp_cxx_unqualified_id (pp
, DECL_NAME (t
));
1777 try compound-statement handler-seq */
1779 pp_maybe_newline_and_indent (pp
, 0);
1780 pp_cxx_ws_string (pp
, "try");
1781 pp_newline_and_indent (pp
, 3);
1782 pp_cxx_statement (pp
, TRY_STMTS (t
));
1783 pp_newline_and_indent (pp
, -3);
1787 pp_cxx_statement (pp
, TRY_HANDLERS (t
));
1792 handler handler-seq(opt)
1795 catch ( exception-declaration ) compound-statement
1797 exception-declaration:
1798 type-specifier-seq declarator
1799 type-specifier-seq abstract-declarator
1802 pp_cxx_ws_string (pp
, "catch");
1803 pp_cxx_left_paren (pp
);
1804 pp_cxx_exception_declaration (pp
, HANDLER_PARMS (t
));
1805 pp_cxx_right_paren (pp
);
1806 pp_indentation (pp
) += 3;
1807 pp_needs_newline (pp
) = true;
1808 pp_cxx_statement (pp
, HANDLER_BODY (t
));
1809 pp_indentation (pp
) -= 3;
1810 pp_needs_newline (pp
) = true;
1813 /* selection-statement:
1814 if ( expression ) statement
1815 if ( expression ) statement else statement */
1817 pp_cxx_ws_string (pp
, "if");
1818 pp_cxx_whitespace (pp
);
1819 pp_cxx_left_paren (pp
);
1820 pp_cxx_expression (pp
, IF_COND (t
));
1821 pp_cxx_right_paren (pp
);
1822 pp_newline_and_indent (pp
, 2);
1823 pp_cxx_statement (pp
, THEN_CLAUSE (t
));
1824 pp_newline_and_indent (pp
, -2);
1825 if (ELSE_CLAUSE (t
))
1827 tree else_clause
= ELSE_CLAUSE (t
);
1828 pp_cxx_ws_string (pp
, "else");
1829 if (TREE_CODE (else_clause
) == IF_STMT
)
1830 pp_cxx_whitespace (pp
);
1832 pp_newline_and_indent (pp
, 2);
1833 pp_cxx_statement (pp
, else_clause
);
1834 if (TREE_CODE (else_clause
) != IF_STMT
)
1835 pp_newline_and_indent (pp
, -2);
1840 pp_cxx_ws_string (pp
, "switch");
1842 pp_cxx_left_paren (pp
);
1843 pp_cxx_expression (pp
, SWITCH_STMT_COND (t
));
1844 pp_cxx_right_paren (pp
);
1845 pp_indentation (pp
) += 3;
1846 pp_needs_newline (pp
) = true;
1847 pp_cxx_statement (pp
, SWITCH_STMT_BODY (t
));
1848 pp_newline_and_indent (pp
, -3);
1851 /* iteration-statement:
1852 while ( expression ) statement
1853 do statement while ( expression ) ;
1854 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1855 for ( declaration expression(opt) ; expression(opt) ) statement */
1857 pp_cxx_ws_string (pp
, "while");
1859 pp_cxx_left_paren (pp
);
1860 pp_cxx_expression (pp
, WHILE_COND (t
));
1861 pp_cxx_right_paren (pp
);
1862 pp_newline_and_indent (pp
, 3);
1863 pp_cxx_statement (pp
, WHILE_BODY (t
));
1864 pp_indentation (pp
) -= 3;
1865 pp_needs_newline (pp
) = true;
1869 pp_cxx_ws_string (pp
, "do");
1870 pp_newline_and_indent (pp
, 3);
1871 pp_cxx_statement (pp
, DO_BODY (t
));
1872 pp_newline_and_indent (pp
, -3);
1873 pp_cxx_ws_string (pp
, "while");
1875 pp_cxx_left_paren (pp
);
1876 pp_cxx_expression (pp
, DO_COND (t
));
1877 pp_cxx_right_paren (pp
);
1878 pp_cxx_semicolon (pp
);
1879 pp_needs_newline (pp
) = true;
1883 pp_cxx_ws_string (pp
, "for");
1885 pp_cxx_left_paren (pp
);
1886 if (FOR_INIT_STMT (t
))
1887 pp_cxx_statement (pp
, FOR_INIT_STMT (t
));
1889 pp_cxx_semicolon (pp
);
1890 pp_needs_newline (pp
) = false;
1891 pp_cxx_whitespace (pp
);
1893 pp_cxx_expression (pp
, FOR_COND (t
));
1894 pp_cxx_semicolon (pp
);
1895 pp_needs_newline (pp
) = false;
1896 pp_cxx_whitespace (pp
);
1898 pp_cxx_expression (pp
, FOR_EXPR (t
));
1899 pp_cxx_right_paren (pp
);
1900 pp_newline_and_indent (pp
, 3);
1901 pp_cxx_statement (pp
, FOR_BODY (t
));
1902 pp_indentation (pp
) -= 3;
1903 pp_needs_newline (pp
) = true;
1909 return expression(opt) ; */
1912 pp_string (pp
, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
1913 pp_cxx_semicolon (pp
);
1914 pp_needs_newline (pp
) = true;
1917 /* expression-statement:
1918 expression(opt) ; */
1920 pp_cxx_expression (pp
, EXPR_STMT_EXPR (t
));
1921 pp_cxx_semicolon (pp
);
1922 pp_needs_newline (pp
) = true;
1926 pp_cxx_ws_string (pp
, "try");
1927 pp_newline_and_indent (pp
, 2);
1928 pp_cxx_statement (pp
, CLEANUP_BODY (t
));
1929 pp_newline_and_indent (pp
, -2);
1930 pp_cxx_ws_string (pp
, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
1931 pp_newline_and_indent (pp
, 2);
1932 pp_cxx_statement (pp
, CLEANUP_EXPR (t
));
1933 pp_newline_and_indent (pp
, -2);
1937 pp_cxx_declaration (pp
, t
);
1941 pp_c_statement (pp_c_base (pp
), t
);
1946 /* original-namespace-definition:
1947 namespace identifier { namespace-body }
1949 As an edge case, we also handle unnamed namespace definition here. */
1952 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
1954 pp_cxx_ws_string (pp
, "namespace");
1955 if (DECL_CONTEXT (t
))
1956 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1958 pp_cxx_unqualified_id (pp
, t
);
1959 pp_cxx_whitespace (pp
);
1960 pp_cxx_left_brace (pp
);
1961 /* We do not print the namespace-body. */
1962 pp_cxx_whitespace (pp
);
1963 pp_cxx_right_brace (pp
);
1969 namespace-alias-definition:
1970 namespace identifier = qualified-namespace-specifier ;
1972 qualified-namespace-specifier:
1973 ::(opt) nested-name-specifier(opt) namespace-name */
1976 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
1978 pp_cxx_ws_string (pp
, "namespace");
1979 if (DECL_CONTEXT (t
))
1980 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1981 pp_cxx_unqualified_id (pp
, t
);
1982 pp_cxx_whitespace (pp
);
1984 pp_cxx_whitespace (pp
);
1985 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
1986 pp_cxx_nested_name_specifier (pp
,
1987 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
1988 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
1989 pp_cxx_semicolon (pp
);
1992 /* simple-declaration:
1993 decl-specifier-seq(opt) init-declarator-list(opt) */
1996 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
1998 pp_cxx_decl_specifier_seq (pp
, t
);
1999 pp_cxx_init_declarator (pp
, t
);
2000 pp_cxx_semicolon (pp
);
2001 pp_needs_newline (pp
) = true;
2005 template-parameter-list:
2007 template-parameter-list , template-parameter */
2010 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
2012 const int n
= TREE_VEC_LENGTH (t
);
2014 for (i
= 0; i
< n
; ++i
)
2017 pp_cxx_separate_with (pp
, ',');
2018 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2022 /* template-parameter:
2024 parameter-declaration
2027 class ...(opt) identifier(opt)
2028 class identifier(opt) = type-id
2029 typename identifier(opt)
2030 typename ...(opt) identifier(opt) = type-id
2031 template < template-parameter-list > class ...(opt) identifier(opt)
2032 template < template-parameter-list > class identifier(opt) = template-name */
2035 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2037 tree parameter
= TREE_VALUE (t
);
2038 switch (TREE_CODE (parameter
))
2041 pp_cxx_ws_string (pp
, "class");
2042 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2043 pp_cxx_ws_string (pp
, "...");
2044 if (DECL_NAME (parameter
))
2045 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2046 /* FIXME: Check if we should print also default argument. */
2050 pp_cxx_parameter_declaration (pp
, parameter
);
2057 pp_unsupported_tree (pp
, t
);
2062 /* Pretty-print a template parameter in the canonical form
2063 "template-parameter-<level>-<position in parameter list>". */
2066 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2068 const enum tree_code code
= TREE_CODE (parm
);
2070 /* Brings type template parameters to the canonical forms. */
2071 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2072 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2073 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2075 pp_cxx_begin_template_argument_list (pp
);
2076 pp_cxx_ws_string (pp
, M_("template-parameter-"));
2077 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2079 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2080 pp_cxx_end_template_argument_list (pp
);
2084 template-declaration:
2085 export(opt) template < template-parameter-list > declaration */
2088 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2090 tree tmpl
= most_general_template (t
);
2094 pp_maybe_newline_and_indent (pp
, 0);
2095 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2097 pp_cxx_ws_string (pp
, "template");
2098 pp_cxx_begin_template_argument_list (pp
);
2099 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2100 pp_cxx_end_template_argument_list (pp
);
2101 pp_newline_and_indent (pp
, 3);
2104 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2105 pp_cxx_function_definition (pp
, t
);
2107 pp_cxx_simple_declaration (pp
, t
);
2111 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2113 pp_unsupported_tree (pp
, t
);
2117 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2119 pp_unsupported_tree (pp
, t
);
2126 template-declaration
2127 explicit-instantiation
2128 explicit-specialization
2129 linkage-specification
2130 namespace-definition
2135 namespace-alias-definition
2138 static_assert-declaration */
2140 pp_cxx_declaration (cxx_pretty_printer
*pp
, tree t
)
2142 if (TREE_CODE (t
) == STATIC_ASSERT
)
2144 pp_cxx_ws_string (pp
, "static_assert");
2145 pp_cxx_left_paren (pp
);
2146 pp_cxx_expression (pp
, STATIC_ASSERT_CONDITION (t
));
2147 pp_cxx_separate_with (pp
, ',');
2148 pp_cxx_expression (pp
, STATIC_ASSERT_MESSAGE (t
));
2149 pp_cxx_right_paren (pp
);
2151 else if (!DECL_LANG_SPECIFIC (t
))
2152 pp_cxx_simple_declaration (pp
, t
);
2153 else if (DECL_USE_TEMPLATE (t
))
2154 switch (DECL_USE_TEMPLATE (t
))
2157 pp_cxx_template_declaration (pp
, t
);
2161 pp_cxx_explicit_specialization (pp
, t
);
2165 pp_cxx_explicit_instantiation (pp
, t
);
2171 else switch (TREE_CODE (t
))
2175 pp_cxx_simple_declaration (pp
, t
);
2179 if (DECL_SAVED_TREE (t
))
2180 pp_cxx_function_definition (pp
, t
);
2182 pp_cxx_simple_declaration (pp
, t
);
2185 case NAMESPACE_DECL
:
2186 if (DECL_NAMESPACE_ALIAS (t
))
2187 pp_cxx_namespace_alias_definition (pp
, t
);
2189 pp_cxx_original_namespace_definition (pp
, t
);
2193 pp_unsupported_tree (pp
, t
);
2199 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2201 t
= TREE_OPERAND (t
, 0);
2202 pp_cxx_ws_string (pp
, "typeid");
2203 pp_cxx_left_paren (pp
);
2205 pp_cxx_type_id (pp
, t
);
2207 pp_cxx_expression (pp
, t
);
2208 pp_cxx_right_paren (pp
);
2212 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2214 pp_cxx_ws_string (pp
, "va_arg");
2215 pp_cxx_left_paren (pp
);
2216 pp_cxx_assignment_expression (pp
, TREE_OPERAND (t
, 0));
2217 pp_cxx_separate_with (pp
, ',');
2218 pp_cxx_type_id (pp
, TREE_TYPE (t
));
2219 pp_cxx_right_paren (pp
);
2223 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2225 switch (TREE_CODE (t
))
2228 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2229 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2231 pp_cxx_type_id (pp
, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2232 pp_cxx_separate_with (pp
, ',');
2237 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2239 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2241 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2244 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2246 pp_left_bracket (pp
);
2247 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2248 pp_right_bracket (pp
);
2256 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2258 pp_cxx_ws_string (pp
, "offsetof");
2259 pp_cxx_left_paren (pp
);
2260 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2261 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
2262 pp_cxx_right_paren (pp
);
2266 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2268 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2272 case CPTK_HAS_NOTHROW_ASSIGN
:
2273 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2275 case CPTK_HAS_TRIVIAL_ASSIGN
:
2276 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2278 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2279 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2281 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2282 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2284 case CPTK_HAS_NOTHROW_COPY
:
2285 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2287 case CPTK_HAS_TRIVIAL_COPY
:
2288 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2290 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2291 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2293 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2294 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2296 case CPTK_IS_ABSTRACT
:
2297 pp_cxx_ws_string (pp
, "__is_abstract");
2299 case CPTK_IS_BASE_OF
:
2300 pp_cxx_ws_string (pp
, "__is_base_of");
2303 pp_cxx_ws_string (pp
, "__is_class");
2305 case CPTK_IS_CONVERTIBLE_TO
:
2306 pp_cxx_ws_string (pp
, "__is_convertible_to");
2309 pp_cxx_ws_string (pp
, "__is_empty");
2312 pp_cxx_ws_string (pp
, "__is_enum");
2315 pp_cxx_ws_string (pp
, "__is_pod");
2317 case CPTK_IS_POLYMORPHIC
:
2318 pp_cxx_ws_string (pp
, "__is_polymorphic");
2320 case CPTK_IS_STD_LAYOUT
:
2321 pp_cxx_ws_string (pp
, "__is_std_layout");
2323 case CPTK_IS_TRIVIAL
:
2324 pp_cxx_ws_string (pp
, "__is_trivial");
2327 pp_cxx_ws_string (pp
, "__is_union");
2334 pp_cxx_left_paren (pp
);
2335 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE1 (t
));
2337 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2339 pp_cxx_separate_with (pp
, ',');
2340 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE2 (t
));
2343 pp_cxx_right_paren (pp
);
2346 typedef c_pretty_print_fn pp_fun
;
2348 /* Initialization of a C++ pretty-printer object. */
2351 pp_cxx_pretty_printer_init (cxx_pretty_printer
*pp
)
2353 pp_c_pretty_printer_init (pp_c_base (pp
));
2354 pp_set_line_maximum_length (pp
, 0);
2356 pp
->c_base
.declaration
= (pp_fun
) pp_cxx_declaration
;
2357 pp
->c_base
.declaration_specifiers
= (pp_fun
) pp_cxx_decl_specifier_seq
;
2358 pp
->c_base
.function_specifier
= (pp_fun
) pp_cxx_function_specifier
;
2359 pp
->c_base
.type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2360 pp
->c_base
.declarator
= (pp_fun
) pp_cxx_declarator
;
2361 pp
->c_base
.direct_declarator
= (pp_fun
) pp_cxx_direct_declarator
;
2362 pp
->c_base
.parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;
2363 pp
->c_base
.type_id
= (pp_fun
) pp_cxx_type_id
;
2364 pp
->c_base
.abstract_declarator
= (pp_fun
) pp_cxx_abstract_declarator
;
2365 pp
->c_base
.direct_abstract_declarator
=
2366 (pp_fun
) pp_cxx_direct_abstract_declarator
;
2367 pp
->c_base
.simple_type_specifier
= (pp_fun
)pp_cxx_simple_type_specifier
;
2369 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2371 pp
->c_base
.constant
= (pp_fun
) pp_cxx_constant
;
2372 pp
->c_base
.id_expression
= (pp_fun
) pp_cxx_id_expression
;
2373 pp
->c_base
.primary_expression
= (pp_fun
) pp_cxx_primary_expression
;
2374 pp
->c_base
.postfix_expression
= (pp_fun
) pp_cxx_postfix_expression
;
2375 pp
->c_base
.unary_expression
= (pp_fun
) pp_cxx_unary_expression
;
2376 pp
->c_base
.multiplicative_expression
= (pp_fun
) pp_cxx_multiplicative_expression
;
2377 pp
->c_base
.conditional_expression
= (pp_fun
) pp_cxx_conditional_expression
;
2378 pp
->c_base
.assignment_expression
= (pp_fun
) pp_cxx_assignment_expression
;
2379 pp
->c_base
.expression
= (pp_fun
) pp_cxx_expression
;
2380 pp
->enclosing_scope
= global_namespace
;