1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008,
3 2009, 2010 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"
29 #include "tree-pretty-print.h"
31 /* Translate if being used for diagnostics, but not for dump files or
33 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
35 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
36 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
37 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
38 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree);
39 static void pp_cxx_expression (cxx_pretty_printer *, tree);
40 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
41 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
42 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
43 static void pp_cxx_type_id (cxx_pretty_printer *, tree);
44 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree);
45 static void pp_cxx_declarator (cxx_pretty_printer *, tree);
46 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
47 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree);
48 static void pp_cxx_statement (cxx_pretty_printer *, tree);
49 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
50 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
51 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
55 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
57 const char *p = pp_last_position_in_text (pp);
59 if (p != NULL && *p == c)
60 pp_cxx_whitespace (pp);
62 pp_base (pp)->padding = pp_none;
65 #define pp_cxx_storage_class_specifier(PP, T) \
66 pp_c_storage_class_specifier (pp_c_base (PP), T)
67 #define pp_cxx_expression_list(PP, T) \
68 pp_c_expression_list (pp_c_base (PP), T)
69 #define pp_cxx_space_for_pointer_operator(PP, T) \
70 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
71 #define pp_cxx_init_declarator(PP, T) \
72 pp_c_init_declarator (pp_c_base (PP), T)
73 #define pp_cxx_call_argument_list(PP, T) \
74 pp_c_call_argument_list (pp_c_base (PP), T)
77 pp_cxx_colon_colon (cxx_pretty_printer *pp)
80 pp_base (pp)->padding = pp_none;
84 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
86 pp_cxx_nonconsecutive_character (pp, '<');
90 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
92 pp_cxx_nonconsecutive_character (pp, '>');
96 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
98 pp_separate_with (pp, c);
99 pp_base (pp)->padding = pp_none;
105 is_destructor_name (tree name)
107 return name == complete_dtor_identifier
108 || name == base_dtor_identifier
109 || name == deleting_dtor_identifier;
112 /* conversion-function-id:
113 operator conversion-type-id
116 type-specifier-seq conversion-declarator(opt)
118 conversion-declarator:
119 ptr-operator conversion-declarator(opt) */
122 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
124 pp_cxx_ws_string (pp, "operator");
125 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
129 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
131 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
132 pp_cxx_begin_template_argument_list (pp);
133 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
134 pp_cxx_end_template_argument_list (pp);
137 /* Prints the unqualified part of the id-expression T.
142 conversion-function-id
147 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
149 enum tree_code code = TREE_CODE (t);
153 pp_cxx_ws_string (pp, M_("<return-value>"));
170 case IDENTIFIER_NODE:
172 pp_cxx_ws_string (pp, M_("<unnamed>"));
173 else if (IDENTIFIER_TYPENAME_P (t))
174 pp_cxx_conversion_function_id (pp, t);
177 if (is_destructor_name (t))
180 /* FIXME: Why is this necessary? */
182 t = constructor_name (TREE_TYPE (t));
184 pp_cxx_tree_identifier (pp, t);
188 case TEMPLATE_ID_EXPR:
189 pp_cxx_template_id (pp, t);
193 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
200 case UNBOUND_CLASS_TEMPLATE:
201 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
202 if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
204 pp_cxx_begin_template_argument_list (pp);
205 pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS
206 (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 (!SCOPE_FILE_SCOPE_P (t) && 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 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
344 pp_string (pp, "nullptr");
347 /* else fall through. */
350 pp_c_constant (pp_c_base (pp), t);
360 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
362 if (TREE_CODE (t) == OVERLOAD)
364 if (DECL_P (t) && DECL_CONTEXT (t))
365 pp_cxx_qualified_id (pp, t);
367 pp_cxx_unqualified_id (pp, t);
370 /* primary-expression:
374 :: operator-function-id
380 __builtin_va_arg ( assignment-expression , type-id )
381 __builtin_offsetof ( type-id, offsetof-expression )
383 __has_nothrow_assign ( type-id )
384 __has_nothrow_constructor ( type-id )
385 __has_nothrow_copy ( type-id )
386 __has_trivial_assign ( type-id )
387 __has_trivial_constructor ( type-id )
388 __has_trivial_copy ( type-id )
389 __has_trivial_destructor ( type-id )
390 __has_virtual_destructor ( type-id )
391 __is_abstract ( type-id )
392 __is_base_of ( type-id , type-id )
393 __is_class ( type-id )
394 __is_convertible_to ( type-id , type-id )
395 __is_empty ( type-id )
396 __is_enum ( type-id )
398 __is_polymorphic ( type-id )
399 __is_union ( type-id ) */
402 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
404 switch (TREE_CODE (t))
410 pp_cxx_constant (pp, t);
414 t = BASELINK_FUNCTIONS (t);
422 pp_cxx_id_expression (pp, t);
426 case TEMPLATE_TYPE_PARM:
427 case TEMPLATE_TEMPLATE_PARM:
428 case TEMPLATE_PARM_INDEX:
429 pp_cxx_unqualified_id (pp, t);
433 pp_cxx_left_paren (pp);
434 pp_cxx_statement (pp, STMT_EXPR_STMT (t));
435 pp_cxx_right_paren (pp);
439 pp_cxx_trait_expression (pp, t);
443 pp_cxx_va_arg_expression (pp, t);
447 pp_cxx_offsetof_expression (pp, t);
451 pp_c_primary_expression (pp_c_base (pp), t);
456 /* postfix-expression:
458 postfix-expression [ expression ]
459 postfix-expression ( expression-list(opt) )
460 simple-type-specifier ( expression-list(opt) )
461 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
462 typename ::(opt) nested-name-specifier template(opt)
463 template-id ( expression-list(opt) )
464 postfix-expression . template(opt) ::(opt) id-expression
465 postfix-expression -> template(opt) ::(opt) id-expression
466 postfix-expression . pseudo-destructor-name
467 postfix-expression -> pseudo-destructor-name
468 postfix-expression ++
469 postfix-expression --
470 dynamic_cast < type-id > ( expression )
471 static_cast < type-id > ( expression )
472 reinterpret_cast < type-id > ( expression )
473 const_cast < type-id > ( expression )
474 typeid ( expression )
475 typeid ( type-id ) */
478 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
480 enum tree_code code = TREE_CODE (t);
487 tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
489 tree saved_scope = pp->enclosing_scope;
490 bool skipfirst = false;
493 if (TREE_CODE (fun) == ADDR_EXPR)
494 fun = TREE_OPERAND (fun, 0);
496 /* In templates, where there is no way to tell whether a given
497 call uses an actual member function. So the parser builds
498 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
499 instantiation time. */
500 if (TREE_CODE (fun) != FUNCTION_DECL)
502 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
504 tree object = (code == AGGR_INIT_EXPR
505 ? (AGGR_INIT_VIA_CTOR_P (t)
506 ? AGGR_INIT_EXPR_SLOT (t)
507 : AGGR_INIT_EXPR_ARG (t, 0))
508 : CALL_EXPR_ARG (t, 0));
510 while (TREE_CODE (object) == NOP_EXPR)
511 object = TREE_OPERAND (object, 0);
513 if (TREE_CODE (object) == ADDR_EXPR)
514 object = TREE_OPERAND (object, 0);
516 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
518 pp_cxx_postfix_expression (pp, object);
523 pp_cxx_postfix_expression (pp, object);
527 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
530 pp_cxx_postfix_expression (pp, fun);
531 pp->enclosing_scope = saved_scope;
532 pp_cxx_left_paren (pp);
533 if (code == AGGR_INIT_EXPR)
535 aggr_init_expr_arg_iterator iter;
536 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
542 pp_cxx_expression (pp, arg);
543 if (more_aggr_init_expr_args_p (&iter))
544 pp_cxx_separate_with (pp, ',');
550 call_expr_arg_iterator iter;
551 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
557 pp_cxx_expression (pp, arg);
558 if (more_call_expr_args_p (&iter))
559 pp_cxx_separate_with (pp, ',');
563 pp_cxx_right_paren (pp);
565 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
567 pp_cxx_separate_with (pp, ',');
568 pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t));
581 pp_cxx_primary_expression (pp, t);
584 case DYNAMIC_CAST_EXPR:
585 case STATIC_CAST_EXPR:
586 case REINTERPRET_CAST_EXPR:
587 case CONST_CAST_EXPR:
588 if (code == DYNAMIC_CAST_EXPR)
589 pp_cxx_ws_string (pp, "dynamic_cast");
590 else if (code == STATIC_CAST_EXPR)
591 pp_cxx_ws_string (pp, "static_cast");
592 else if (code == REINTERPRET_CAST_EXPR)
593 pp_cxx_ws_string (pp, "reinterpret_cast");
595 pp_cxx_ws_string (pp, "const_cast");
596 pp_cxx_begin_template_argument_list (pp);
597 pp_cxx_type_id (pp, TREE_TYPE (t));
598 pp_cxx_end_template_argument_list (pp);
600 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
604 case EMPTY_CLASS_EXPR:
605 pp_cxx_type_id (pp, TREE_TYPE (t));
611 pp_cxx_typeid_expression (pp, t);
614 case PSEUDO_DTOR_EXPR:
615 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
617 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
618 pp_cxx_colon_colon (pp);
620 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
624 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
629 pp_c_postfix_expression (pp_c_base (pp), t);
635 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
636 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
642 type-specifier-seq new-declarator(opt)
645 ptr-operator new-declarator(opt)
646 direct-new-declarator
648 direct-new-declarator
650 direct-new-declarator [ constant-expression ]
653 ( expression-list(opt) ) */
656 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
658 enum tree_code code = TREE_CODE (t);
659 tree type = TREE_OPERAND (t, 1);
660 tree init = TREE_OPERAND (t, 2);
665 if (NEW_EXPR_USE_GLOBAL (t))
666 pp_cxx_colon_colon (pp);
667 pp_cxx_ws_string (pp, "new");
668 if (TREE_OPERAND (t, 0))
670 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
673 if (TREE_CODE (type) == ARRAY_REF)
674 type = build_cplus_array_type
675 (TREE_OPERAND (type, 0),
676 build_index_type (fold_build2_loc (input_location,
677 MINUS_EXPR, integer_type_node,
678 TREE_OPERAND (type, 1),
680 pp_cxx_type_id (pp, type);
684 if (TREE_CODE (init) == TREE_LIST)
685 pp_c_expression_list (pp_c_base (pp), init);
686 else if (init == void_zero_node)
687 ; /* OK, empty initializer list. */
689 pp_cxx_expression (pp, init);
695 pp_unsupported_tree (pp, t);
699 /* delete-expression:
700 ::(opt) delete cast-expression
701 ::(opt) delete [ ] cast-expression */
704 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
706 enum tree_code code = TREE_CODE (t);
710 case VEC_DELETE_EXPR:
711 if (DELETE_EXPR_USE_GLOBAL (t))
712 pp_cxx_colon_colon (pp);
713 pp_cxx_ws_string (pp, "delete");
715 if (code == VEC_DELETE_EXPR
716 || DELETE_EXPR_USE_VEC (t))
718 pp_left_bracket (pp);
719 pp_right_bracket (pp);
722 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
726 pp_unsupported_tree (pp, t);
734 unary-operator cast-expression
735 sizeof unary-expression
737 sizeof ... ( identifier )
741 unary-operator: one of
745 __alignof__ unary-expression
746 __alignof__ ( type-id ) */
749 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
751 enum tree_code code = TREE_CODE (t);
756 pp_cxx_new_expression (pp, t);
760 case VEC_DELETE_EXPR:
761 pp_cxx_delete_expression (pp, t);
765 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
767 pp_cxx_ws_string (pp, "sizeof");
768 pp_cxx_ws_string (pp, "...");
769 pp_cxx_whitespace (pp);
770 pp_cxx_left_paren (pp);
771 if (TYPE_P (TREE_OPERAND (t, 0)))
772 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
774 pp_unary_expression (pp, TREE_OPERAND (t, 0));
775 pp_cxx_right_paren (pp);
781 pp_cxx_ws_string (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
782 pp_cxx_whitespace (pp);
783 if (TYPE_P (TREE_OPERAND (t, 0)))
785 pp_cxx_left_paren (pp);
786 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
787 pp_cxx_right_paren (pp);
790 pp_unary_expression (pp, TREE_OPERAND (t, 0));
794 pp_cxx_ws_string (pp, "@encode");
795 pp_cxx_whitespace (pp);
796 pp_cxx_left_paren (pp);
797 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
798 pp_cxx_right_paren (pp);
802 pp_cxx_ws_string (pp, "noexcept");
803 pp_cxx_whitespace (pp);
804 pp_cxx_left_paren (pp);
805 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
806 pp_cxx_right_paren (pp);
809 case UNARY_PLUS_EXPR:
811 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
815 pp_c_unary_expression (pp_c_base (pp), t);
822 ( type-id ) cast-expression */
825 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
827 switch (TREE_CODE (t))
830 pp_cxx_type_id (pp, TREE_TYPE (t));
831 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
835 pp_c_cast_expression (pp_c_base (pp), t);
842 pm-expression .* cast-expression
843 pm-expression ->* cast-expression */
846 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
848 switch (TREE_CODE (t))
850 /* Handle unfortunate OFFSET_REF overloading here. */
852 if (TYPE_P (TREE_OPERAND (t, 0)))
854 pp_cxx_qualified_id (pp, t);
857 /* Else fall through. */
860 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
861 if (TREE_CODE (t) == MEMBER_REF)
866 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
871 pp_cxx_cast_expression (pp, t);
876 /* multiplicative-expression:
878 multiplicative-expression * pm-expression
879 multiplicative-expression / pm-expression
880 multiplicative-expression % pm-expression */
883 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
885 enum tree_code code = TREE_CODE (e);
891 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
893 if (code == MULT_EXPR)
895 else if (code == TRUNC_DIV_EXPR)
900 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
904 pp_cxx_pm_expression (pp, e);
909 /* conditional-expression:
910 logical-or-expression
911 logical-or-expression ? expression : assignment-expression */
914 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
916 if (TREE_CODE (e) == COND_EXPR)
918 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
922 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
924 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
927 pp_c_logical_or_expression (pp_c_base (pp), e);
930 /* Pretty-print a compound assignment operator token as indicated by T. */
933 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
937 switch (TREE_CODE (t))
960 op = tree_code_name[TREE_CODE (t)];
964 pp_cxx_ws_string (pp, op);
968 /* assignment-expression:
969 conditional-expression
970 logical-or-expression assignment-operator assignment-expression
974 throw assignment-expression(opt)
976 assignment-operator: one of
977 = *= /= %= += -= >>= <<= &= ^= |= */
980 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
982 switch (TREE_CODE (e))
986 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
990 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
994 pp_cxx_ws_string (pp, "throw");
995 if (TREE_OPERAND (e, 0))
996 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
1000 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
1001 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
1002 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
1006 pp_cxx_conditional_expression (pp, e);
1012 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
1014 switch (TREE_CODE (t))
1020 pp_cxx_constant (pp, t);
1024 pp_cxx_unqualified_id (pp, t);
1032 pp_cxx_qualified_id (pp, t);
1036 t = OVL_CURRENT (t);
1044 case TEMPLATE_TYPE_PARM:
1045 case TEMPLATE_PARM_INDEX:
1046 case TEMPLATE_TEMPLATE_PARM:
1048 pp_cxx_primary_expression (pp, t);
1052 case DYNAMIC_CAST_EXPR:
1053 case STATIC_CAST_EXPR:
1054 case REINTERPRET_CAST_EXPR:
1055 case CONST_CAST_EXPR:
1059 case EMPTY_CLASS_EXPR:
1061 case PSEUDO_DTOR_EXPR:
1062 case AGGR_INIT_EXPR:
1064 pp_cxx_postfix_expression (pp, t);
1069 pp_cxx_new_expression (pp, t);
1073 case VEC_DELETE_EXPR:
1074 pp_cxx_delete_expression (pp, t);
1080 pp_cxx_unary_expression (pp, t);
1084 pp_cxx_cast_expression (pp, t);
1090 pp_cxx_pm_expression (pp, t);
1094 case TRUNC_DIV_EXPR:
1095 case TRUNC_MOD_EXPR:
1096 pp_cxx_multiplicative_expression (pp, t);
1100 pp_cxx_conditional_expression (pp, t);
1107 pp_cxx_assignment_expression (pp, t);
1110 case NON_DEPENDENT_EXPR:
1111 case MUST_NOT_THROW_EXPR:
1112 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1115 case EXPR_PACK_EXPANSION:
1116 pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t));
1117 pp_cxx_ws_string (pp, "...");
1120 case TEMPLATE_ID_EXPR:
1121 pp_cxx_template_id (pp, t);
1124 case NONTYPE_ARGUMENT_PACK:
1126 tree args = ARGUMENT_PACK_ARGS (t);
1127 int i, len = TREE_VEC_LENGTH (args);
1128 for (i = 0; i < len; ++i)
1131 pp_cxx_separate_with (pp, ',');
1132 pp_cxx_expression (pp, TREE_VEC_ELT (args, i));
1138 pp_c_expression (pp_c_base (pp), t);
1146 /* function-specifier:
1152 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1154 switch (TREE_CODE (t))
1157 if (DECL_VIRTUAL_P (t))
1158 pp_cxx_ws_string (pp, "virtual");
1159 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1160 pp_cxx_ws_string (pp, "explicit");
1162 pp_c_function_specifier (pp_c_base (pp), t);
1169 /* decl-specifier-seq:
1170 decl-specifier-seq(opt) decl-specifier
1173 storage-class-specifier
1180 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1182 switch (TREE_CODE (t))
1188 pp_cxx_storage_class_specifier (pp, t);
1189 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1193 pp_cxx_ws_string (pp, "typedef");
1194 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1198 /* Constructors don't have return types. And conversion functions
1199 do not have a type-specifier in their return types. */
1200 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1201 pp_cxx_function_specifier (pp, t);
1202 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1203 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1206 pp_c_declaration_specifiers (pp_c_base (pp), t);
1211 /* simple-type-specifier:
1212 ::(opt) nested-name-specifier(opt) type-name
1213 ::(opt) nested-name-specifier(opt) template(opt) template-id
1227 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1229 switch (TREE_CODE (t))
1234 pp_cxx_qualified_id (pp, t);
1237 case TEMPLATE_TYPE_PARM:
1238 case TEMPLATE_TEMPLATE_PARM:
1239 case TEMPLATE_PARM_INDEX:
1240 pp_cxx_unqualified_id (pp, t);
1244 pp_cxx_ws_string (pp, "typename");
1245 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1246 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1250 pp_c_type_specifier (pp_c_base (pp), t);
1255 /* type-specifier-seq:
1256 type-specifier type-specifier-seq(opt)
1259 simple-type-specifier
1262 elaborated-type-specifier
1266 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1268 switch (TREE_CODE (t))
1271 case TEMPLATE_TYPE_PARM:
1272 case TEMPLATE_TEMPLATE_PARM:
1274 case BOUND_TEMPLATE_TEMPLATE_PARM:
1275 pp_cxx_cv_qualifier_seq (pp, t);
1276 pp_cxx_simple_type_specifier (pp, t);
1280 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1281 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1282 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1286 pp_cxx_ws_string (pp, "decltype");
1287 pp_cxx_left_paren (pp);
1288 pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t));
1289 pp_cxx_right_paren (pp);
1293 if (TYPE_PTRMEMFUNC_P (t))
1295 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1296 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1297 pp_cxx_whitespace (pp);
1298 pp_cxx_ptr_operator (pp, t);
1301 /* else fall through */
1304 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1305 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1310 * cv-qualifier-seq(opt)
1312 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1315 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1317 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1319 switch (TREE_CODE (t))
1321 case REFERENCE_TYPE:
1323 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1324 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1325 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1326 if (TREE_CODE (t) == POINTER_TYPE)
1329 pp_cxx_cv_qualifier_seq (pp, t);
1336 if (TYPE_PTRMEMFUNC_P (t))
1338 pp_cxx_left_paren (pp);
1339 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1344 if (TYPE_PTR_TO_MEMBER_P (t))
1346 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1347 pp_cxx_left_paren (pp);
1348 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1350 pp_cxx_cv_qualifier_seq (pp, t);
1353 /* else fall through. */
1356 pp_unsupported_tree (pp, t);
1362 pp_cxx_implicit_parameter_type (tree mf)
1364 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1368 parameter-declaration:
1369 decl-specifier-seq declarator
1370 decl-specifier-seq declarator = assignment-expression
1371 decl-specifier-seq abstract-declarator(opt)
1372 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1375 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1377 pp_cxx_decl_specifier_seq (pp, t);
1379 pp_cxx_abstract_declarator (pp, t);
1381 pp_cxx_declarator (pp, t);
1384 /* parameter-declaration-clause:
1385 parameter-declaration-list(opt) ...(opt)
1386 parameter-declaration-list , ...
1388 parameter-declaration-list:
1389 parameter-declaration
1390 parameter-declaration-list , parameter-declaration */
1393 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1395 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1397 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1398 const bool abstract = args == NULL
1399 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1402 /* Skip artificial parameter for nonstatic member functions. */
1403 if (TREE_CODE (t) == METHOD_TYPE)
1404 types = TREE_CHAIN (types);
1406 pp_cxx_left_paren (pp);
1407 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1410 pp_cxx_separate_with (pp, ',');
1412 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1413 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1415 pp_cxx_whitespace (pp);
1417 pp_cxx_whitespace (pp);
1418 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1421 pp_cxx_right_paren (pp);
1424 /* exception-specification:
1425 throw ( type-id-list(opt) )
1429 type-id-list , type-id */
1432 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1434 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1435 bool need_comma = false;
1437 if (ex_spec == NULL)
1439 if (TREE_PURPOSE (ex_spec))
1441 pp_cxx_ws_string (pp, "noexcept");
1442 pp_cxx_whitespace (pp);
1443 pp_cxx_left_paren (pp);
1444 pp_cxx_expression (pp, TREE_PURPOSE (ex_spec));
1445 pp_cxx_right_paren (pp);
1448 pp_cxx_ws_string (pp, "throw");
1449 pp_cxx_left_paren (pp);
1450 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1452 tree type = TREE_VALUE (ex_spec);
1453 tree argpack = NULL_TREE;
1456 if (ARGUMENT_PACK_P (type))
1458 argpack = ARGUMENT_PACK_ARGS (type);
1459 len = TREE_VEC_LENGTH (argpack);
1462 for (i = 0; i < len; ++i)
1465 type = TREE_VEC_ELT (argpack, i);
1468 pp_cxx_separate_with (pp, ',');
1472 pp_cxx_type_id (pp, type);
1475 pp_cxx_right_paren (pp);
1478 /* direct-declarator:
1480 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1481 exception-specification(opt)
1482 direct-declaration [ constant-expression(opt) ]
1486 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1488 switch (TREE_CODE (t))
1496 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1498 if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t))
1499 || template_parameter_pack_p (t))
1500 /* A function parameter pack or non-type template
1502 pp_cxx_ws_string (pp, "...");
1504 pp_cxx_id_expression (pp, DECL_NAME (t));
1506 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1510 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1511 pp_cxx_id_expression (pp, t);
1512 pp_cxx_parameter_declaration_clause (pp, t);
1514 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1516 pp_base (pp)->padding = pp_before;
1517 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1520 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1525 case TEMPLATE_TYPE_PARM:
1526 case TEMPLATE_PARM_INDEX:
1527 case TEMPLATE_TEMPLATE_PARM:
1531 pp_c_direct_declarator (pp_c_base (pp), t);
1538 ptr-operator declarator */
1541 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1543 pp_cxx_direct_declarator (pp, t);
1546 /* ctor-initializer:
1547 : mem-initializer-list
1549 mem-initializer-list:
1551 mem-initializer , mem-initializer-list
1554 mem-initializer-id ( expression-list(opt) )
1557 ::(opt) nested-name-specifier(opt) class-name
1561 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1563 t = TREE_OPERAND (t, 0);
1564 pp_cxx_whitespace (pp);
1566 pp_cxx_whitespace (pp);
1567 for (; t; t = TREE_CHAIN (t))
1569 tree purpose = TREE_PURPOSE (t);
1570 bool is_pack = PACK_EXPANSION_P (purpose);
1573 pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose));
1575 pp_cxx_primary_expression (pp, purpose);
1576 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1578 pp_cxx_ws_string (pp, "...");
1580 pp_cxx_separate_with (pp, ',');
1584 /* function-definition:
1585 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1586 decl-specifier-seq(opt) declarator function-try-block */
1589 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1591 tree saved_scope = pp->enclosing_scope;
1592 pp_cxx_decl_specifier_seq (pp, t);
1593 pp_cxx_declarator (pp, t);
1594 pp_needs_newline (pp) = true;
1595 pp->enclosing_scope = DECL_CONTEXT (t);
1596 if (DECL_SAVED_TREE (t))
1597 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1600 pp_cxx_semicolon (pp);
1601 pp_needs_newline (pp) = true;
1604 pp->enclosing_scope = saved_scope;
1607 /* abstract-declarator:
1608 ptr-operator abstract-declarator(opt)
1609 direct-abstract-declarator */
1612 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1614 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1615 pp_cxx_right_paren (pp);
1616 else if (POINTER_TYPE_P (t))
1618 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1619 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1620 pp_cxx_right_paren (pp);
1623 pp_cxx_direct_abstract_declarator (pp, t);
1626 /* direct-abstract-declarator:
1627 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1628 cv-qualifier-seq(opt) exception-specification(opt)
1629 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1630 ( abstract-declarator ) */
1633 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1635 switch (TREE_CODE (t))
1637 case REFERENCE_TYPE:
1638 pp_cxx_abstract_declarator (pp, t);
1642 if (TYPE_PTRMEMFUNC_P (t))
1643 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1648 pp_cxx_parameter_declaration_clause (pp, t);
1649 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1650 if (TREE_CODE (t) == METHOD_TYPE)
1652 pp_base (pp)->padding = pp_before;
1653 pp_cxx_cv_qualifier_seq
1654 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1656 pp_cxx_exception_specification (pp, t);
1660 case TEMPLATE_TYPE_PARM:
1661 case TEMPLATE_TEMPLATE_PARM:
1662 case BOUND_TEMPLATE_TEMPLATE_PARM:
1663 case UNBOUND_CLASS_TEMPLATE:
1667 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1673 type-specifier-seq abstract-declarator(opt) */
1676 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1678 pp_flags saved_flags = pp_c_base (pp)->flags;
1679 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1681 switch (TREE_CODE (t))
1688 case BOUND_TEMPLATE_TEMPLATE_PARM:
1689 case UNBOUND_CLASS_TEMPLATE:
1690 case TEMPLATE_TEMPLATE_PARM:
1691 case TEMPLATE_TYPE_PARM:
1692 case TEMPLATE_PARM_INDEX:
1696 case TEMPLATE_ID_EXPR:
1697 pp_cxx_type_specifier_seq (pp, t);
1700 case TYPE_PACK_EXPANSION:
1701 pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t));
1702 pp_cxx_ws_string (pp, "...");
1706 pp_c_type_id (pp_c_base (pp), t);
1710 pp_c_base (pp)->flags = saved_flags;
1713 /* template-argument-list:
1714 template-argument ...(opt)
1715 template-argument-list, template-argument ...(opt)
1718 assignment-expression
1723 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1726 bool need_comma = false;
1730 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1732 tree arg = TREE_VEC_ELT (t, i);
1733 tree argpack = NULL_TREE;
1736 if (ARGUMENT_PACK_P (arg))
1738 argpack = ARGUMENT_PACK_ARGS (arg);
1739 len = TREE_VEC_LENGTH (argpack);
1742 for (idx = 0; idx < len; idx++)
1745 arg = TREE_VEC_ELT (argpack, idx);
1748 pp_cxx_separate_with (pp, ',');
1752 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1753 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1754 pp_cxx_type_id (pp, arg);
1756 pp_cxx_expression (pp, arg);
1763 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1765 t = DECL_EXPR_DECL (t);
1766 pp_cxx_type_specifier_seq (pp, t);
1768 pp_cxx_abstract_declarator (pp, t);
1770 pp_cxx_declarator (pp, t);
1776 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1778 switch (TREE_CODE (t))
1780 case CTOR_INITIALIZER:
1781 pp_cxx_ctor_initializer (pp, t);
1785 pp_cxx_ws_string (pp, "using");
1786 pp_cxx_ws_string (pp, "namespace");
1787 if (DECL_CONTEXT (t))
1788 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1789 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1793 pp_cxx_ws_string (pp, "using");
1794 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1795 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1802 try compound-statement handler-seq */
1804 pp_maybe_newline_and_indent (pp, 0);
1805 pp_cxx_ws_string (pp, "try");
1806 pp_newline_and_indent (pp, 3);
1807 pp_cxx_statement (pp, TRY_STMTS (t));
1808 pp_newline_and_indent (pp, -3);
1812 pp_cxx_statement (pp, TRY_HANDLERS (t));
1817 handler handler-seq(opt)
1820 catch ( exception-declaration ) compound-statement
1822 exception-declaration:
1823 type-specifier-seq declarator
1824 type-specifier-seq abstract-declarator
1827 pp_cxx_ws_string (pp, "catch");
1828 pp_cxx_left_paren (pp);
1829 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1830 pp_cxx_right_paren (pp);
1831 pp_indentation (pp) += 3;
1832 pp_needs_newline (pp) = true;
1833 pp_cxx_statement (pp, HANDLER_BODY (t));
1834 pp_indentation (pp) -= 3;
1835 pp_needs_newline (pp) = true;
1838 /* selection-statement:
1839 if ( expression ) statement
1840 if ( expression ) statement else statement */
1842 pp_cxx_ws_string (pp, "if");
1843 pp_cxx_whitespace (pp);
1844 pp_cxx_left_paren (pp);
1845 pp_cxx_expression (pp, IF_COND (t));
1846 pp_cxx_right_paren (pp);
1847 pp_newline_and_indent (pp, 2);
1848 pp_cxx_statement (pp, THEN_CLAUSE (t));
1849 pp_newline_and_indent (pp, -2);
1850 if (ELSE_CLAUSE (t))
1852 tree else_clause = ELSE_CLAUSE (t);
1853 pp_cxx_ws_string (pp, "else");
1854 if (TREE_CODE (else_clause) == IF_STMT)
1855 pp_cxx_whitespace (pp);
1857 pp_newline_and_indent (pp, 2);
1858 pp_cxx_statement (pp, else_clause);
1859 if (TREE_CODE (else_clause) != IF_STMT)
1860 pp_newline_and_indent (pp, -2);
1865 pp_cxx_ws_string (pp, "switch");
1867 pp_cxx_left_paren (pp);
1868 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1869 pp_cxx_right_paren (pp);
1870 pp_indentation (pp) += 3;
1871 pp_needs_newline (pp) = true;
1872 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1873 pp_newline_and_indent (pp, -3);
1876 /* iteration-statement:
1877 while ( expression ) statement
1878 do statement while ( expression ) ;
1879 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1880 for ( declaration expression(opt) ; expression(opt) ) statement */
1882 pp_cxx_ws_string (pp, "while");
1884 pp_cxx_left_paren (pp);
1885 pp_cxx_expression (pp, WHILE_COND (t));
1886 pp_cxx_right_paren (pp);
1887 pp_newline_and_indent (pp, 3);
1888 pp_cxx_statement (pp, WHILE_BODY (t));
1889 pp_indentation (pp) -= 3;
1890 pp_needs_newline (pp) = true;
1894 pp_cxx_ws_string (pp, "do");
1895 pp_newline_and_indent (pp, 3);
1896 pp_cxx_statement (pp, DO_BODY (t));
1897 pp_newline_and_indent (pp, -3);
1898 pp_cxx_ws_string (pp, "while");
1900 pp_cxx_left_paren (pp);
1901 pp_cxx_expression (pp, DO_COND (t));
1902 pp_cxx_right_paren (pp);
1903 pp_cxx_semicolon (pp);
1904 pp_needs_newline (pp) = true;
1908 pp_cxx_ws_string (pp, "for");
1910 pp_cxx_left_paren (pp);
1911 if (FOR_INIT_STMT (t))
1912 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1914 pp_cxx_semicolon (pp);
1915 pp_needs_newline (pp) = false;
1916 pp_cxx_whitespace (pp);
1918 pp_cxx_expression (pp, FOR_COND (t));
1919 pp_cxx_semicolon (pp);
1920 pp_needs_newline (pp) = false;
1921 pp_cxx_whitespace (pp);
1923 pp_cxx_expression (pp, FOR_EXPR (t));
1924 pp_cxx_right_paren (pp);
1925 pp_newline_and_indent (pp, 3);
1926 pp_cxx_statement (pp, FOR_BODY (t));
1927 pp_indentation (pp) -= 3;
1928 pp_needs_newline (pp) = true;
1931 case RANGE_FOR_STMT:
1932 pp_cxx_ws_string (pp, "for");
1934 pp_cxx_left_paren (pp);
1935 pp_cxx_statement (pp, RANGE_FOR_DECL (t));
1937 pp_needs_newline (pp) = false;
1940 pp_cxx_statement (pp, RANGE_FOR_EXPR (t));
1941 pp_cxx_right_paren (pp);
1942 pp_newline_and_indent (pp, 3);
1943 pp_cxx_statement (pp, FOR_BODY (t));
1944 pp_indentation (pp) -= 3;
1945 pp_needs_newline (pp) = true;
1951 return expression(opt) ; */
1954 pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1955 pp_cxx_semicolon (pp);
1956 pp_needs_newline (pp) = true;
1959 /* expression-statement:
1960 expression(opt) ; */
1962 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1963 pp_cxx_semicolon (pp);
1964 pp_needs_newline (pp) = true;
1968 pp_cxx_ws_string (pp, "try");
1969 pp_newline_and_indent (pp, 2);
1970 pp_cxx_statement (pp, CLEANUP_BODY (t));
1971 pp_newline_and_indent (pp, -2);
1972 pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1973 pp_newline_and_indent (pp, 2);
1974 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1975 pp_newline_and_indent (pp, -2);
1979 pp_cxx_declaration (pp, t);
1983 pp_c_statement (pp_c_base (pp), t);
1988 /* original-namespace-definition:
1989 namespace identifier { namespace-body }
1991 As an edge case, we also handle unnamed namespace definition here. */
1994 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1996 pp_cxx_ws_string (pp, "namespace");
1997 if (DECL_CONTEXT (t))
1998 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2000 pp_cxx_unqualified_id (pp, t);
2001 pp_cxx_whitespace (pp);
2002 pp_cxx_left_brace (pp);
2003 /* We do not print the namespace-body. */
2004 pp_cxx_whitespace (pp);
2005 pp_cxx_right_brace (pp);
2011 namespace-alias-definition:
2012 namespace identifier = qualified-namespace-specifier ;
2014 qualified-namespace-specifier:
2015 ::(opt) nested-name-specifier(opt) namespace-name */
2018 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
2020 pp_cxx_ws_string (pp, "namespace");
2021 if (DECL_CONTEXT (t))
2022 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2023 pp_cxx_unqualified_id (pp, t);
2024 pp_cxx_whitespace (pp);
2026 pp_cxx_whitespace (pp);
2027 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
2028 pp_cxx_nested_name_specifier (pp,
2029 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
2030 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
2031 pp_cxx_semicolon (pp);
2034 /* simple-declaration:
2035 decl-specifier-seq(opt) init-declarator-list(opt) */
2038 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
2040 pp_cxx_decl_specifier_seq (pp, t);
2041 pp_cxx_init_declarator (pp, t);
2042 pp_cxx_semicolon (pp);
2043 pp_needs_newline (pp) = true;
2047 template-parameter-list:
2049 template-parameter-list , template-parameter */
2052 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
2054 const int n = TREE_VEC_LENGTH (t);
2056 for (i = 0; i < n; ++i)
2059 pp_cxx_separate_with (pp, ',');
2060 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2064 /* template-parameter:
2066 parameter-declaration
2069 class ...(opt) identifier(opt)
2070 class identifier(opt) = type-id
2071 typename identifier(opt)
2072 typename ...(opt) identifier(opt) = type-id
2073 template < template-parameter-list > class ...(opt) identifier(opt)
2074 template < template-parameter-list > class identifier(opt) = template-name */
2077 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2079 tree parameter = TREE_VALUE (t);
2080 switch (TREE_CODE (parameter))
2083 pp_cxx_ws_string (pp, "class");
2084 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2085 pp_cxx_ws_string (pp, "...");
2086 if (DECL_NAME (parameter))
2087 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2088 /* FIXME: Check if we should print also default argument. */
2092 pp_cxx_parameter_declaration (pp, parameter);
2099 pp_unsupported_tree (pp, t);
2104 /* Pretty-print a template parameter in the canonical form
2105 "template-parameter-<level>-<position in parameter list>". */
2108 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2110 const enum tree_code code = TREE_CODE (parm);
2112 /* Brings type template parameters to the canonical forms. */
2113 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2114 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2115 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2117 pp_cxx_begin_template_argument_list (pp);
2118 pp_cxx_ws_string (pp, M_("template-parameter-"));
2119 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2121 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2122 pp_cxx_end_template_argument_list (pp);
2126 template-declaration:
2127 export(opt) template < template-parameter-list > declaration */
2130 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2132 tree tmpl = most_general_template (t);
2136 pp_maybe_newline_and_indent (pp, 0);
2137 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2139 pp_cxx_ws_string (pp, "template");
2140 pp_cxx_begin_template_argument_list (pp);
2141 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2142 pp_cxx_end_template_argument_list (pp);
2143 pp_newline_and_indent (pp, 3);
2146 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2147 pp_cxx_function_definition (pp, t);
2149 pp_cxx_simple_declaration (pp, t);
2153 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2155 pp_unsupported_tree (pp, t);
2159 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2161 pp_unsupported_tree (pp, t);
2168 template-declaration
2169 explicit-instantiation
2170 explicit-specialization
2171 linkage-specification
2172 namespace-definition
2177 namespace-alias-definition
2180 static_assert-declaration */
2182 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
2184 if (TREE_CODE (t) == STATIC_ASSERT)
2186 pp_cxx_ws_string (pp, "static_assert");
2187 pp_cxx_left_paren (pp);
2188 pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
2189 pp_cxx_separate_with (pp, ',');
2190 pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
2191 pp_cxx_right_paren (pp);
2193 else if (!DECL_LANG_SPECIFIC (t))
2194 pp_cxx_simple_declaration (pp, t);
2195 else if (DECL_USE_TEMPLATE (t))
2196 switch (DECL_USE_TEMPLATE (t))
2199 pp_cxx_template_declaration (pp, t);
2203 pp_cxx_explicit_specialization (pp, t);
2207 pp_cxx_explicit_instantiation (pp, t);
2213 else switch (TREE_CODE (t))
2217 pp_cxx_simple_declaration (pp, t);
2221 if (DECL_SAVED_TREE (t))
2222 pp_cxx_function_definition (pp, t);
2224 pp_cxx_simple_declaration (pp, t);
2227 case NAMESPACE_DECL:
2228 if (DECL_NAMESPACE_ALIAS (t))
2229 pp_cxx_namespace_alias_definition (pp, t);
2231 pp_cxx_original_namespace_definition (pp, t);
2235 pp_unsupported_tree (pp, t);
2241 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2243 t = TREE_OPERAND (t, 0);
2244 pp_cxx_ws_string (pp, "typeid");
2245 pp_cxx_left_paren (pp);
2247 pp_cxx_type_id (pp, t);
2249 pp_cxx_expression (pp, t);
2250 pp_cxx_right_paren (pp);
2254 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2256 pp_cxx_ws_string (pp, "va_arg");
2257 pp_cxx_left_paren (pp);
2258 pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0));
2259 pp_cxx_separate_with (pp, ',');
2260 pp_cxx_type_id (pp, TREE_TYPE (t));
2261 pp_cxx_right_paren (pp);
2265 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2267 switch (TREE_CODE (t))
2270 if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2271 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2273 pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2274 pp_cxx_separate_with (pp, ',');
2279 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2281 if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2283 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2286 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2288 pp_left_bracket (pp);
2289 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2290 pp_right_bracket (pp);
2298 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2300 pp_cxx_ws_string (pp, "offsetof");
2301 pp_cxx_left_paren (pp);
2302 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2303 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
2304 pp_cxx_right_paren (pp);
2308 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2310 cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2314 case CPTK_HAS_NOTHROW_ASSIGN:
2315 pp_cxx_ws_string (pp, "__has_nothrow_assign");
2317 case CPTK_HAS_TRIVIAL_ASSIGN:
2318 pp_cxx_ws_string (pp, "__has_trivial_assign");
2320 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2321 pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2323 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2324 pp_cxx_ws_string (pp, "__has_trivial_constructor");
2326 case CPTK_HAS_NOTHROW_COPY:
2327 pp_cxx_ws_string (pp, "__has_nothrow_copy");
2329 case CPTK_HAS_TRIVIAL_COPY:
2330 pp_cxx_ws_string (pp, "__has_trivial_copy");
2332 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2333 pp_cxx_ws_string (pp, "__has_trivial_destructor");
2335 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2336 pp_cxx_ws_string (pp, "__has_virtual_destructor");
2338 case CPTK_IS_ABSTRACT:
2339 pp_cxx_ws_string (pp, "__is_abstract");
2341 case CPTK_IS_BASE_OF:
2342 pp_cxx_ws_string (pp, "__is_base_of");
2345 pp_cxx_ws_string (pp, "__is_class");
2347 case CPTK_IS_CONVERTIBLE_TO:
2348 pp_cxx_ws_string (pp, "__is_convertible_to");
2351 pp_cxx_ws_string (pp, "__is_empty");
2354 pp_cxx_ws_string (pp, "__is_enum");
2357 pp_cxx_ws_string (pp, "__is_pod");
2359 case CPTK_IS_POLYMORPHIC:
2360 pp_cxx_ws_string (pp, "__is_polymorphic");
2362 case CPTK_IS_STD_LAYOUT:
2363 pp_cxx_ws_string (pp, "__is_std_layout");
2365 case CPTK_IS_TRIVIAL:
2366 pp_cxx_ws_string (pp, "__is_trivial");
2369 pp_cxx_ws_string (pp, "__is_union");
2371 case CPTK_IS_LITERAL_TYPE:
2372 pp_cxx_ws_string (pp, "__is_literal_type");
2379 pp_cxx_left_paren (pp);
2380 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t));
2382 if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2384 pp_cxx_separate_with (pp, ',');
2385 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t));
2388 pp_cxx_right_paren (pp);
2391 typedef c_pretty_print_fn pp_fun;
2393 /* Initialization of a C++ pretty-printer object. */
2396 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
2398 pp_c_pretty_printer_init (pp_c_base (pp));
2399 pp_set_line_maximum_length (pp, 0);
2401 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
2402 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2403 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
2404 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2405 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
2406 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2407 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2408 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
2409 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2410 pp->c_base.direct_abstract_declarator =
2411 (pp_fun) pp_cxx_direct_abstract_declarator;
2412 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
2414 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2416 pp->c_base.constant = (pp_fun) pp_cxx_constant;
2417 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
2418 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
2419 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2420 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
2421 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2422 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2423 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2424 pp->c_base.expression = (pp_fun) pp_cxx_expression;
2425 pp->enclosing_scope = global_namespace;