1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "cxx-pretty-print.h"
28 #include "tree-pretty-print.h"
30 static void pp_cxx_unqualified_id (cxx_pretty_printer
*, tree
);
31 static void pp_cxx_nested_name_specifier (cxx_pretty_printer
*, tree
);
32 static void pp_cxx_qualified_id (cxx_pretty_printer
*, tree
);
33 static void pp_cxx_template_argument_list (cxx_pretty_printer
*, tree
);
34 static void pp_cxx_type_specifier_seq (cxx_pretty_printer
*, tree
);
35 static void pp_cxx_ptr_operator (cxx_pretty_printer
*, tree
);
36 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*, tree
);
37 static void pp_cxx_template_parameter (cxx_pretty_printer
*, tree
);
38 static void pp_cxx_cast_expression (cxx_pretty_printer
*, tree
);
39 static void pp_cxx_typeid_expression (cxx_pretty_printer
*, tree
);
43 pp_cxx_nonconsecutive_character (cxx_pretty_printer
*pp
, int c
)
45 const char *p
= pp_last_position_in_text (pp
);
47 if (p
!= NULL
&& *p
== c
)
48 pp_cxx_whitespace (pp
);
50 pp
->padding
= pp_none
;
53 #define pp_cxx_expression_list(PP, T) \
54 pp_c_expression_list (PP, T)
55 #define pp_cxx_space_for_pointer_operator(PP, T) \
56 pp_c_space_for_pointer_operator (PP, T)
57 #define pp_cxx_init_declarator(PP, T) \
58 pp_c_init_declarator (PP, T)
59 #define pp_cxx_call_argument_list(PP, T) \
60 pp_c_call_argument_list (PP, T)
63 pp_cxx_colon_colon (cxx_pretty_printer
*pp
)
66 pp
->padding
= pp_none
;
70 pp_cxx_begin_template_argument_list (cxx_pretty_printer
*pp
)
72 pp_cxx_nonconsecutive_character (pp
, '<');
76 pp_cxx_end_template_argument_list (cxx_pretty_printer
*pp
)
78 pp_cxx_nonconsecutive_character (pp
, '>');
82 pp_cxx_separate_with (cxx_pretty_printer
*pp
, int c
)
84 pp_separate_with (pp
, c
);
85 pp
->padding
= pp_none
;
91 is_destructor_name (tree name
)
93 return name
== complete_dtor_identifier
94 || name
== base_dtor_identifier
95 || name
== deleting_dtor_identifier
;
98 /* conversion-function-id:
99 operator conversion-type-id
102 type-specifier-seq conversion-declarator(opt)
104 conversion-declarator:
105 ptr-operator conversion-declarator(opt) */
108 pp_cxx_conversion_function_id (cxx_pretty_printer
*pp
, tree t
)
110 pp_cxx_ws_string (pp
, "operator");
111 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
115 pp_cxx_template_id (cxx_pretty_printer
*pp
, tree t
)
117 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
118 pp_cxx_begin_template_argument_list (pp
);
119 pp_cxx_template_argument_list (pp
, TREE_OPERAND (t
, 1));
120 pp_cxx_end_template_argument_list (pp
);
123 /* Prints the unqualified part of the id-expression T.
128 conversion-function-id
133 pp_cxx_unqualified_id (cxx_pretty_printer
*pp
, tree t
)
135 enum tree_code code
= TREE_CODE (t
);
139 pp
->translate_string ("<return-value>");
156 case IDENTIFIER_NODE
:
158 pp
->translate_string ("<unnamed>");
159 else if (IDENTIFIER_TYPENAME_P (t
))
160 pp_cxx_conversion_function_id (pp
, t
);
163 if (is_destructor_name (t
))
166 /* FIXME: Why is this necessary? */
168 t
= constructor_name (TREE_TYPE (t
));
170 pp_cxx_tree_identifier (pp
, t
);
174 case TEMPLATE_ID_EXPR
:
175 pp_cxx_template_id (pp
, t
);
179 pp_cxx_unqualified_id (pp
, BASELINK_FUNCTIONS (t
));
186 case UNBOUND_CLASS_TEMPLATE
:
187 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
188 if (CLASS_TYPE_P (t
) && CLASSTYPE_USE_TEMPLATE (t
))
190 pp_cxx_begin_template_argument_list (pp
);
191 pp_cxx_template_argument_list (pp
, INNERMOST_TEMPLATE_ARGS
192 (CLASSTYPE_TI_ARGS (t
)));
193 pp_cxx_end_template_argument_list (pp
);
198 pp_cxx_complement (pp
);
199 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
202 case TEMPLATE_TYPE_PARM
:
203 case TEMPLATE_TEMPLATE_PARM
:
204 if (TYPE_IDENTIFIER (t
))
205 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
207 pp_cxx_canonical_template_parameter (pp
, t
);
210 case TEMPLATE_PARM_INDEX
:
211 pp_cxx_unqualified_id (pp
, TEMPLATE_PARM_DECL (t
));
214 case BOUND_TEMPLATE_TEMPLATE_PARM
:
215 pp_cxx_cv_qualifier_seq (pp
, t
);
216 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
217 pp_cxx_begin_template_argument_list (pp
);
218 pp_cxx_template_argument_list (pp
, TYPE_TI_ARGS (t
));
219 pp_cxx_end_template_argument_list (pp
);
223 pp_unsupported_tree (pp
, t
);
228 /* Pretty-print out the token sequence ":: template" in template codes
229 where it is needed to "inline declare" the (following) member as
230 a template. This situation arises when SCOPE of T is dependent
231 on template parameters. */
234 pp_cxx_template_keyword_if_needed (cxx_pretty_printer
*pp
, tree scope
, tree t
)
236 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
237 && TYPE_P (scope
) && dependent_type_p (scope
))
238 pp_cxx_ws_string (pp
, "template");
241 /* nested-name-specifier:
242 class-or-namespace-name :: nested-name-specifier(opt)
243 class-or-namespace-name :: template nested-name-specifier */
246 pp_cxx_nested_name_specifier (cxx_pretty_printer
*pp
, tree t
)
248 if (!SCOPE_FILE_SCOPE_P (t
) && t
!= pp
->enclosing_scope
)
250 tree scope
= get_containing_scope (t
);
251 pp_cxx_nested_name_specifier (pp
, scope
);
252 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
253 pp_cxx_unqualified_id (pp
, t
);
254 pp_cxx_colon_colon (pp
);
259 nested-name-specifier template(opt) unqualified-id */
262 pp_cxx_qualified_id (cxx_pretty_printer
*pp
, tree t
)
264 switch (TREE_CODE (t
))
266 /* A pointer-to-member is always qualified. */
268 pp_cxx_nested_name_specifier (pp
, PTRMEM_CST_CLASS (t
));
269 pp_cxx_unqualified_id (pp
, PTRMEM_CST_MEMBER (t
));
272 /* In Standard C++, functions cannot possibly be used as
273 nested-name-specifiers. However, there are situations where
274 is "makes sense" to output the surrounding function name for the
275 purpose of emphasizing on the scope kind. Just printing the
276 function name might not be sufficient as it may be overloaded; so,
277 we decorate the function with its signature too.
278 FIXME: This is probably the wrong pretty-printing for conversion
279 functions and some function templates. */
283 if (DECL_FUNCTION_MEMBER_P (t
))
284 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
285 pp_cxx_unqualified_id
286 (pp
, DECL_CONSTRUCTOR_P (t
) ? DECL_CONTEXT (t
) : t
);
287 pp_cxx_parameter_declaration_clause (pp
, TREE_TYPE (t
));
292 pp_cxx_nested_name_specifier (pp
, TREE_OPERAND (t
, 0));
293 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 1));
298 tree scope
= get_containing_scope (t
);
299 if (scope
!= pp
->enclosing_scope
)
301 pp_cxx_nested_name_specifier (pp
, scope
);
302 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
304 pp_cxx_unqualified_id (pp
, t
);
312 cxx_pretty_printer::constant (tree t
)
314 switch (TREE_CODE (t
))
318 const bool in_parens
= PAREN_STRING_LITERAL_P (t
);
320 pp_cxx_left_paren (this);
321 c_pretty_printer::constant (t
);
323 pp_cxx_right_paren (this);
328 if (NULLPTR_TYPE_P (TREE_TYPE (t
)))
330 pp_string (this, "nullptr");
333 /* else fall through. */
336 c_pretty_printer::constant (t
);
346 cxx_pretty_printer::id_expression (tree t
)
348 if (TREE_CODE (t
) == OVERLOAD
)
350 if (DECL_P (t
) && DECL_CONTEXT (t
))
351 pp_cxx_qualified_id (this, t
);
353 pp_cxx_unqualified_id (this, t
);
356 /* user-defined literal:
360 pp_cxx_userdef_literal (cxx_pretty_printer
*pp
, tree t
)
362 pp
->constant (USERDEF_LITERAL_VALUE (t
));
363 pp
->id_expression (USERDEF_LITERAL_SUFFIX_ID (t
));
367 /* primary-expression:
371 :: operator-function-id
377 __builtin_va_arg ( assignment-expression , type-id )
378 __builtin_offsetof ( type-id, offsetof-expression )
380 __has_nothrow_assign ( type-id )
381 __has_nothrow_constructor ( type-id )
382 __has_nothrow_copy ( type-id )
383 __has_trivial_assign ( type-id )
384 __has_trivial_constructor ( type-id )
385 __has_trivial_copy ( type-id )
386 __has_trivial_destructor ( type-id )
387 __has_virtual_destructor ( type-id )
388 __is_abstract ( type-id )
389 __is_base_of ( type-id , type-id )
390 __is_class ( type-id )
391 __is_convertible_to ( type-id , type-id )
392 __is_empty ( type-id )
393 __is_enum ( type-id )
394 __is_literal_type ( type-id )
396 __is_polymorphic ( type-id )
397 __is_std_layout ( type-id )
398 __is_trivial ( type-id )
399 __is_union ( type-id ) */
402 cxx_pretty_printer::primary_expression (tree t
)
404 switch (TREE_CODE (t
))
413 case USERDEF_LITERAL
:
414 pp_cxx_userdef_literal (this, t
);
418 t
= BASELINK_FUNCTIONS (t
);
430 case TEMPLATE_TYPE_PARM
:
431 case TEMPLATE_TEMPLATE_PARM
:
432 case TEMPLATE_PARM_INDEX
:
433 pp_cxx_unqualified_id (this, t
);
437 pp_cxx_left_paren (this);
438 statement (STMT_EXPR_STMT (t
));
439 pp_cxx_right_paren (this);
443 pp_cxx_trait_expression (this, t
);
447 pp_cxx_va_arg_expression (this, t
);
451 pp_cxx_offsetof_expression (this, t
);
455 c_pretty_printer::primary_expression (t
);
460 /* postfix-expression:
462 postfix-expression [ expression ]
463 postfix-expression ( expression-list(opt) )
464 simple-type-specifier ( expression-list(opt) )
465 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
466 typename ::(opt) nested-name-specifier template(opt)
467 template-id ( expression-list(opt) )
468 postfix-expression . template(opt) ::(opt) id-expression
469 postfix-expression -> template(opt) ::(opt) id-expression
470 postfix-expression . pseudo-destructor-name
471 postfix-expression -> pseudo-destructor-name
472 postfix-expression ++
473 postfix-expression --
474 dynamic_cast < type-id > ( expression )
475 static_cast < type-id > ( expression )
476 reinterpret_cast < type-id > ( expression )
477 const_cast < type-id > ( expression )
478 typeid ( expression )
479 typeid ( type-id ) */
482 cxx_pretty_printer::postfix_expression (tree t
)
484 enum tree_code code
= TREE_CODE (t
);
491 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
493 tree saved_scope
= enclosing_scope
;
494 bool skipfirst
= false;
497 if (TREE_CODE (fun
) == ADDR_EXPR
)
498 fun
= TREE_OPERAND (fun
, 0);
500 /* In templates, where there is no way to tell whether a given
501 call uses an actual member function. So the parser builds
502 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
503 instantiation time. */
504 if (TREE_CODE (fun
) != FUNCTION_DECL
)
506 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
508 tree object
= (code
== AGGR_INIT_EXPR
509 ? (AGGR_INIT_VIA_CTOR_P (t
)
510 ? AGGR_INIT_EXPR_SLOT (t
)
511 : AGGR_INIT_EXPR_ARG (t
, 0))
512 : CALL_EXPR_ARG (t
, 0));
514 while (TREE_CODE (object
) == NOP_EXPR
)
515 object
= TREE_OPERAND (object
, 0);
517 if (TREE_CODE (object
) == ADDR_EXPR
)
518 object
= TREE_OPERAND (object
, 0);
520 if (!TYPE_PTR_P (TREE_TYPE (object
)))
522 postfix_expression (object
);
527 postfix_expression (object
);
531 enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
534 postfix_expression (fun
);
535 enclosing_scope
= saved_scope
;
536 pp_cxx_left_paren (this);
537 if (code
== AGGR_INIT_EXPR
)
539 aggr_init_expr_arg_iterator iter
;
540 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
547 if (more_aggr_init_expr_args_p (&iter
))
548 pp_cxx_separate_with (this, ',');
554 call_expr_arg_iterator iter
;
555 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
562 if (more_call_expr_args_p (&iter
))
563 pp_cxx_separate_with (this, ',');
567 pp_cxx_right_paren (this);
569 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
571 pp_cxx_separate_with (this, ',');
572 postfix_expression (AGGR_INIT_EXPR_SLOT (t
));
585 primary_expression (t
);
588 case DYNAMIC_CAST_EXPR
:
589 case STATIC_CAST_EXPR
:
590 case REINTERPRET_CAST_EXPR
:
591 case CONST_CAST_EXPR
:
592 if (code
== DYNAMIC_CAST_EXPR
)
593 pp_cxx_ws_string (this, "dynamic_cast");
594 else if (code
== STATIC_CAST_EXPR
)
595 pp_cxx_ws_string (this, "static_cast");
596 else if (code
== REINTERPRET_CAST_EXPR
)
597 pp_cxx_ws_string (this, "reinterpret_cast");
599 pp_cxx_ws_string (this, "const_cast");
600 pp_cxx_begin_template_argument_list (this);
601 type_id (TREE_TYPE (t
));
602 pp_cxx_end_template_argument_list (this);
603 pp_left_paren (this);
604 expression (TREE_OPERAND (t
, 0));
605 pp_right_paren (this);
608 case EMPTY_CLASS_EXPR
:
609 type_id (TREE_TYPE (t
));
610 pp_left_paren (this);
611 pp_right_paren (this);
615 pp_cxx_typeid_expression (this, t
);
618 case PSEUDO_DTOR_EXPR
:
619 postfix_expression (TREE_OPERAND (t
, 0));
621 pp_cxx_qualified_id (this, TREE_OPERAND (t
, 1));
622 pp_cxx_colon_colon (this);
623 pp_complement (this);
624 pp_cxx_unqualified_id (this, TREE_OPERAND (t
, 2));
628 postfix_expression (TREE_OPERAND (t
, 0));
633 c_pretty_printer::postfix_expression (t
);
639 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
640 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
646 type-specifier-seq new-declarator(opt)
649 ptr-operator new-declarator(opt)
650 direct-new-declarator
652 direct-new-declarator
654 direct-new-declarator [ constant-expression ]
657 ( expression-list(opt) ) */
660 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
662 enum tree_code code
= TREE_CODE (t
);
663 tree type
= TREE_OPERAND (t
, 1);
664 tree init
= TREE_OPERAND (t
, 2);
669 if (NEW_EXPR_USE_GLOBAL (t
))
670 pp_cxx_colon_colon (pp
);
671 pp_cxx_ws_string (pp
, "new");
672 if (TREE_OPERAND (t
, 0))
674 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
677 if (TREE_CODE (type
) == ARRAY_REF
)
678 type
= build_cplus_array_type
679 (TREE_OPERAND (type
, 0),
680 build_index_type (fold_build2_loc (input_location
,
681 MINUS_EXPR
, integer_type_node
,
682 TREE_OPERAND (type
, 1),
688 if (TREE_CODE (init
) == TREE_LIST
)
689 pp_c_expression_list (pp
, init
);
690 else if (init
== void_zero_node
)
691 ; /* OK, empty initializer list. */
693 pp
->expression (init
);
699 pp_unsupported_tree (pp
, t
);
703 /* delete-expression:
704 ::(opt) delete cast-expression
705 ::(opt) delete [ ] cast-expression */
708 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
710 enum tree_code code
= TREE_CODE (t
);
714 case VEC_DELETE_EXPR
:
715 if (DELETE_EXPR_USE_GLOBAL (t
))
716 pp_cxx_colon_colon (pp
);
717 pp_cxx_ws_string (pp
, "delete");
719 if (code
== VEC_DELETE_EXPR
720 || DELETE_EXPR_USE_VEC (t
))
722 pp_left_bracket (pp
);
723 pp_right_bracket (pp
);
726 pp_c_cast_expression (pp
, TREE_OPERAND (t
, 0));
730 pp_unsupported_tree (pp
, t
);
738 unary-operator cast-expression
739 sizeof unary-expression
741 sizeof ... ( identifier )
745 unary-operator: one of
749 __alignof__ unary-expression
750 __alignof__ ( type-id ) */
753 cxx_pretty_printer::unary_expression (tree t
)
755 enum tree_code code
= TREE_CODE (t
);
760 pp_cxx_new_expression (this, t
);
764 case VEC_DELETE_EXPR
:
765 pp_cxx_delete_expression (this, t
);
769 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
771 pp_cxx_ws_string (this, "sizeof");
772 pp_cxx_ws_string (this, "...");
773 pp_cxx_whitespace (this);
774 pp_cxx_left_paren (this);
775 if (TYPE_P (TREE_OPERAND (t
, 0)))
776 type_id (TREE_OPERAND (t
, 0));
778 unary_expression (TREE_OPERAND (t
, 0));
779 pp_cxx_right_paren (this);
785 pp_cxx_ws_string (this, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
786 pp_cxx_whitespace (this);
787 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
789 pp_cxx_left_paren (this);
790 type_id (TREE_TYPE (TREE_OPERAND (t
, 0)));
791 pp_cxx_right_paren (this);
793 else if (TYPE_P (TREE_OPERAND (t
, 0)))
795 pp_cxx_left_paren (this);
796 type_id (TREE_OPERAND (t
, 0));
797 pp_cxx_right_paren (this);
800 unary_expression (TREE_OPERAND (t
, 0));
804 pp_cxx_ws_string (this, "@encode");
805 pp_cxx_whitespace (this);
806 pp_cxx_left_paren (this);
807 type_id (TREE_OPERAND (t
, 0));
808 pp_cxx_right_paren (this);
812 pp_cxx_ws_string (this, "noexcept");
813 pp_cxx_whitespace (this);
814 pp_cxx_left_paren (this);
815 expression (TREE_OPERAND (t
, 0));
816 pp_cxx_right_paren (this);
819 case UNARY_PLUS_EXPR
:
821 pp_cxx_cast_expression (this, TREE_OPERAND (t
, 0));
825 c_pretty_printer::unary_expression (t
);
832 ( type-id ) cast-expression */
835 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
837 switch (TREE_CODE (t
))
840 case IMPLICIT_CONV_EXPR
:
841 pp
->type_id (TREE_TYPE (t
));
842 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
846 pp_c_cast_expression (pp
, t
);
853 pm-expression .* cast-expression
854 pm-expression ->* cast-expression */
857 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
859 switch (TREE_CODE (t
))
861 /* Handle unfortunate OFFSET_REF overloading here. */
863 if (TYPE_P (TREE_OPERAND (t
, 0)))
865 pp_cxx_qualified_id (pp
, t
);
868 /* Else fall through. */
871 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
872 if (TREE_CODE (t
) == MEMBER_REF
)
877 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
882 pp_cxx_cast_expression (pp
, t
);
887 /* multiplicative-expression:
889 multiplicative-expression * pm-expression
890 multiplicative-expression / pm-expression
891 multiplicative-expression % pm-expression */
894 cxx_pretty_printer::multiplicative_expression (tree e
)
896 enum tree_code code
= TREE_CODE (e
);
902 multiplicative_expression (TREE_OPERAND (e
, 0));
904 if (code
== MULT_EXPR
)
906 else if (code
== TRUNC_DIV_EXPR
)
911 pp_cxx_pm_expression (this, TREE_OPERAND (e
, 1));
915 pp_cxx_pm_expression (this, e
);
920 /* conditional-expression:
921 logical-or-expression
922 logical-or-expression ? expression : assignment-expression */
925 cxx_pretty_printer::conditional_expression (tree e
)
927 if (TREE_CODE (e
) == COND_EXPR
)
929 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
933 expression (TREE_OPERAND (e
, 1));
935 assignment_expression (TREE_OPERAND (e
, 2));
938 pp_c_logical_or_expression (this, e
);
941 /* Pretty-print a compound assignment operator token as indicated by T. */
944 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
948 switch (TREE_CODE (t
))
971 op
= tree_code_name
[TREE_CODE (t
)];
975 pp_cxx_ws_string (pp
, op
);
979 /* assignment-expression:
980 conditional-expression
981 logical-or-expression assignment-operator assignment-expression
985 throw assignment-expression(opt)
987 assignment-operator: one of
988 = *= /= %= += -= >>= <<= &= ^= |= */
991 cxx_pretty_printer::assignment_expression (tree e
)
993 switch (TREE_CODE (e
))
997 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1001 assignment_expression (TREE_OPERAND (e
, 1));
1005 pp_cxx_ws_string (this, "throw");
1006 if (TREE_OPERAND (e
, 0))
1007 assignment_expression (TREE_OPERAND (e
, 0));
1011 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1012 pp_cxx_assignment_operator (this, TREE_OPERAND (e
, 1));
1013 assignment_expression (TREE_OPERAND (e
, 2));
1017 conditional_expression (e
);
1023 cxx_pretty_printer::expression (tree t
)
1025 switch (TREE_CODE (t
))
1034 case USERDEF_LITERAL
:
1035 pp_cxx_userdef_literal (this, t
);
1039 pp_cxx_unqualified_id (this, t
);
1047 pp_cxx_qualified_id (this, t
);
1051 t
= OVL_CURRENT (t
);
1059 case TEMPLATE_TYPE_PARM
:
1060 case TEMPLATE_PARM_INDEX
:
1061 case TEMPLATE_TEMPLATE_PARM
:
1063 primary_expression (t
);
1067 case DYNAMIC_CAST_EXPR
:
1068 case STATIC_CAST_EXPR
:
1069 case REINTERPRET_CAST_EXPR
:
1070 case CONST_CAST_EXPR
:
1074 case EMPTY_CLASS_EXPR
:
1076 case PSEUDO_DTOR_EXPR
:
1077 case AGGR_INIT_EXPR
:
1079 postfix_expression (t
);
1084 pp_cxx_new_expression (this, t
);
1088 case VEC_DELETE_EXPR
:
1089 pp_cxx_delete_expression (this, t
);
1095 unary_expression (t
);
1099 case IMPLICIT_CONV_EXPR
:
1100 pp_cxx_cast_expression (this, t
);
1106 pp_cxx_pm_expression (this, t
);
1110 case TRUNC_DIV_EXPR
:
1111 case TRUNC_MOD_EXPR
:
1112 multiplicative_expression (t
);
1116 conditional_expression (t
);
1123 assignment_expression (t
);
1126 case NON_DEPENDENT_EXPR
:
1127 case MUST_NOT_THROW_EXPR
:
1128 expression (TREE_OPERAND (t
, 0));
1131 case EXPR_PACK_EXPANSION
:
1132 expression (PACK_EXPANSION_PATTERN (t
));
1133 pp_cxx_ws_string (this, "...");
1136 case TEMPLATE_ID_EXPR
:
1137 pp_cxx_template_id (this, t
);
1140 case NONTYPE_ARGUMENT_PACK
:
1142 tree args
= ARGUMENT_PACK_ARGS (t
);
1143 int i
, len
= TREE_VEC_LENGTH (args
);
1144 for (i
= 0; i
< len
; ++i
)
1147 pp_cxx_separate_with (this, ',');
1148 expression (TREE_VEC_ELT (args
, i
));
1154 pp_cxx_ws_string (this, "<lambda>");
1158 pp_cxx_left_paren (this);
1159 expression (TREE_OPERAND (t
, 0));
1160 pp_cxx_right_paren (this);
1164 c_pretty_printer::expression (t
);
1172 /* function-specifier:
1178 cxx_pretty_printer::function_specifier (tree t
)
1180 switch (TREE_CODE (t
))
1183 if (DECL_VIRTUAL_P (t
))
1184 pp_cxx_ws_string (this, "virtual");
1185 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1186 pp_cxx_ws_string (this, "explicit");
1188 c_pretty_printer::function_specifier (t
);
1195 /* decl-specifier-seq:
1196 decl-specifier-seq(opt) decl-specifier
1199 storage-class-specifier
1206 cxx_pretty_printer::declaration_specifiers (tree t
)
1208 switch (TREE_CODE (t
))
1214 storage_class_specifier (t
);
1215 declaration_specifiers (TREE_TYPE (t
));
1219 pp_cxx_ws_string (this, "typedef");
1220 declaration_specifiers (TREE_TYPE (t
));
1224 /* Constructors don't have return types. And conversion functions
1225 do not have a type-specifier in their return types. */
1226 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1227 function_specifier (t
);
1228 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1229 declaration_specifiers (TREE_TYPE (TREE_TYPE (t
)));
1232 c_pretty_printer::declaration_specifiers (t
);
1237 /* simple-type-specifier:
1238 ::(opt) nested-name-specifier(opt) type-name
1239 ::(opt) nested-name-specifier(opt) template(opt) template-id
1253 pp_cxx_simple_type_specifier (cxx_pretty_printer
*pp
, tree t
)
1255 switch (TREE_CODE (t
))
1260 pp_cxx_qualified_id (pp
, t
);
1263 case TEMPLATE_TYPE_PARM
:
1264 case TEMPLATE_TEMPLATE_PARM
:
1265 case TEMPLATE_PARM_INDEX
:
1266 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1267 pp_cxx_unqualified_id (pp
, t
);
1271 pp_cxx_ws_string (pp
, "typename");
1272 pp_cxx_nested_name_specifier (pp
, TYPE_CONTEXT (t
));
1273 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
1277 pp_c_type_specifier (pp
, t
);
1282 /* type-specifier-seq:
1283 type-specifier type-specifier-seq(opt)
1286 simple-type-specifier
1289 elaborated-type-specifier
1293 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1295 switch (TREE_CODE (t
))
1298 case TEMPLATE_TYPE_PARM
:
1299 case TEMPLATE_TEMPLATE_PARM
:
1301 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1302 pp_cxx_cv_qualifier_seq (pp
, t
);
1303 pp_cxx_simple_type_specifier (pp
, t
);
1307 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1308 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1309 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1313 pp_cxx_ws_string (pp
, "decltype");
1314 pp_cxx_left_paren (pp
);
1315 pp
->expression (DECLTYPE_TYPE_EXPR (t
));
1316 pp_cxx_right_paren (pp
);
1320 if (TYPE_PTRMEMFUNC_P (t
))
1322 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1323 pp
->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm
)));
1324 pp_cxx_whitespace (pp
);
1325 pp_cxx_ptr_operator (pp
, t
);
1328 /* else fall through */
1331 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1332 pp_c_specifier_qualifier_list (pp
, t
);
1337 * cv-qualifier-seq(opt)
1339 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1342 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1344 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1346 switch (TREE_CODE (t
))
1348 case REFERENCE_TYPE
:
1350 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t
)))
1351 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1352 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1356 pp_cxx_cv_qualifier_seq (pp
, t
);
1363 if (TYPE_PTRMEMFUNC_P (t
))
1365 pp_cxx_left_paren (pp
);
1366 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1371 if (TYPE_PTRMEM_P (t
))
1373 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1374 pp_cxx_left_paren (pp
);
1375 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1377 pp_cxx_cv_qualifier_seq (pp
, t
);
1380 /* else fall through. */
1383 pp_unsupported_tree (pp
, t
);
1389 pp_cxx_implicit_parameter_type (tree mf
)
1391 return class_of_this_parm (TREE_TYPE (mf
));
1395 parameter-declaration:
1396 decl-specifier-seq declarator
1397 decl-specifier-seq declarator = assignment-expression
1398 decl-specifier-seq abstract-declarator(opt)
1399 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1402 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1404 pp
->declaration_specifiers (t
);
1406 pp
->abstract_declarator (t
);
1411 /* parameter-declaration-clause:
1412 parameter-declaration-list(opt) ...(opt)
1413 parameter-declaration-list , ...
1415 parameter-declaration-list:
1416 parameter-declaration
1417 parameter-declaration-list , parameter-declaration */
1420 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1422 tree args
= TYPE_P (t
) ? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1424 TYPE_P (t
) ? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1425 const bool abstract
= args
== NULL
|| pp
->flags
& pp_c_flag_abstract
;
1428 /* Skip artificial parameter for nonstatic member functions. */
1429 if (TREE_CODE (t
) == METHOD_TYPE
)
1430 types
= TREE_CHAIN (types
);
1432 pp_cxx_left_paren (pp
);
1433 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1436 pp_cxx_separate_with (pp
, ',');
1438 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1439 if (!abstract
&& pp
->flags
& pp_cxx_flag_default_argument
)
1441 pp_cxx_whitespace (pp
);
1443 pp_cxx_whitespace (pp
);
1444 pp
->assignment_expression (TREE_PURPOSE (types
));
1447 pp_cxx_right_paren (pp
);
1450 /* exception-specification:
1451 throw ( type-id-list(opt) )
1455 type-id-list , type-id */
1458 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1460 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1461 bool need_comma
= false;
1463 if (ex_spec
== NULL
)
1465 if (TREE_PURPOSE (ex_spec
))
1467 pp_cxx_ws_string (pp
, "noexcept");
1468 pp_cxx_whitespace (pp
);
1469 pp_cxx_left_paren (pp
);
1470 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec
))
1471 pp_cxx_ws_string (pp
, "<uninstantiated>");
1473 pp
->expression (TREE_PURPOSE (ex_spec
));
1474 pp_cxx_right_paren (pp
);
1477 pp_cxx_ws_string (pp
, "throw");
1478 pp_cxx_left_paren (pp
);
1479 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1481 tree type
= TREE_VALUE (ex_spec
);
1482 tree argpack
= NULL_TREE
;
1485 if (ARGUMENT_PACK_P (type
))
1487 argpack
= ARGUMENT_PACK_ARGS (type
);
1488 len
= TREE_VEC_LENGTH (argpack
);
1491 for (i
= 0; i
< len
; ++i
)
1494 type
= TREE_VEC_ELT (argpack
, i
);
1497 pp_cxx_separate_with (pp
, ',');
1504 pp_cxx_right_paren (pp
);
1507 /* direct-declarator:
1509 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1510 exception-specification(opt)
1511 direct-declaration [ constant-expression(opt) ]
1515 cxx_pretty_printer::direct_declarator (tree t
)
1517 switch (TREE_CODE (t
))
1525 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t
));
1527 if ((TREE_CODE (t
) == PARM_DECL
&& FUNCTION_PARAMETER_PACK_P (t
))
1528 || template_parameter_pack_p (t
))
1529 /* A function parameter pack or non-type template
1531 pp_cxx_ws_string (this, "...");
1533 id_expression (DECL_NAME (t
));
1535 abstract_declarator (TREE_TYPE (t
));
1539 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
1541 pp_cxx_parameter_declaration_clause (this, t
);
1543 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1545 padding
= pp_before
;
1546 pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t
));
1549 pp_cxx_exception_specification (this, TREE_TYPE (t
));
1554 case TEMPLATE_TYPE_PARM
:
1555 case TEMPLATE_PARM_INDEX
:
1556 case TEMPLATE_TEMPLATE_PARM
:
1560 c_pretty_printer::direct_declarator (t
);
1567 ptr-operator declarator */
1570 cxx_pretty_printer::declarator (tree t
)
1572 direct_declarator (t
);
1575 /* ctor-initializer:
1576 : mem-initializer-list
1578 mem-initializer-list:
1580 mem-initializer , mem-initializer-list
1583 mem-initializer-id ( expression-list(opt) )
1586 ::(opt) nested-name-specifier(opt) class-name
1590 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1592 t
= TREE_OPERAND (t
, 0);
1593 pp_cxx_whitespace (pp
);
1595 pp_cxx_whitespace (pp
);
1596 for (; t
; t
= TREE_CHAIN (t
))
1598 tree purpose
= TREE_PURPOSE (t
);
1599 bool is_pack
= PACK_EXPANSION_P (purpose
);
1602 pp
->primary_expression (PACK_EXPANSION_PATTERN (purpose
));
1604 pp
->primary_expression (purpose
);
1605 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1607 pp_cxx_ws_string (pp
, "...");
1609 pp_cxx_separate_with (pp
, ',');
1613 /* function-definition:
1614 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1615 decl-specifier-seq(opt) declarator function-try-block */
1618 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1620 tree saved_scope
= pp
->enclosing_scope
;
1621 pp
->declaration_specifiers (t
);
1623 pp_needs_newline (pp
) = true;
1624 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1625 if (DECL_SAVED_TREE (t
))
1626 pp
->statement (DECL_SAVED_TREE (t
));
1628 pp_cxx_semicolon (pp
);
1629 pp_newline_and_flush (pp
);
1630 pp
->enclosing_scope
= saved_scope
;
1633 /* abstract-declarator:
1634 ptr-operator abstract-declarator(opt)
1635 direct-abstract-declarator */
1638 cxx_pretty_printer::abstract_declarator (tree t
)
1640 if (TYPE_PTRMEM_P (t
))
1641 pp_cxx_right_paren (this);
1642 else if (POINTER_TYPE_P (t
))
1644 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1645 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1646 pp_cxx_right_paren (this);
1649 direct_abstract_declarator (t
);
1652 /* direct-abstract-declarator:
1653 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1654 cv-qualifier-seq(opt) exception-specification(opt)
1655 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1656 ( abstract-declarator ) */
1659 cxx_pretty_printer::direct_abstract_declarator (tree t
)
1661 switch (TREE_CODE (t
))
1663 case REFERENCE_TYPE
:
1664 abstract_declarator (t
);
1668 if (TYPE_PTRMEMFUNC_P (t
))
1669 direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1674 pp_cxx_parameter_declaration_clause (this, t
);
1675 direct_abstract_declarator (TREE_TYPE (t
));
1676 if (TREE_CODE (t
) == METHOD_TYPE
)
1678 padding
= pp_before
;
1679 pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t
));
1681 pp_cxx_exception_specification (this, t
);
1685 case TEMPLATE_TYPE_PARM
:
1686 case TEMPLATE_TEMPLATE_PARM
:
1687 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1688 case UNBOUND_CLASS_TEMPLATE
:
1692 c_pretty_printer::direct_abstract_declarator (t
);
1698 type-specifier-seq abstract-declarator(opt) */
1701 cxx_pretty_printer::type_id (tree t
)
1703 pp_flags saved_flags
= flags
;
1704 flags
|= pp_c_flag_abstract
;
1706 switch (TREE_CODE (t
))
1713 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1714 case UNBOUND_CLASS_TEMPLATE
:
1715 case TEMPLATE_TEMPLATE_PARM
:
1716 case TEMPLATE_TYPE_PARM
:
1717 case TEMPLATE_PARM_INDEX
:
1720 case UNDERLYING_TYPE
:
1722 case TEMPLATE_ID_EXPR
:
1723 pp_cxx_type_specifier_seq (this, t
);
1726 case TYPE_PACK_EXPANSION
:
1727 type_id (PACK_EXPANSION_PATTERN (t
));
1728 pp_cxx_ws_string (this, "...");
1732 c_pretty_printer::type_id (t
);
1736 flags
= saved_flags
;
1739 /* template-argument-list:
1740 template-argument ...(opt)
1741 template-argument-list, template-argument ...(opt)
1744 assignment-expression
1749 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1752 bool need_comma
= false;
1756 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1758 tree arg
= TREE_VEC_ELT (t
, i
);
1759 tree argpack
= NULL_TREE
;
1762 if (ARGUMENT_PACK_P (arg
))
1764 argpack
= ARGUMENT_PACK_ARGS (arg
);
1765 len
= TREE_VEC_LENGTH (argpack
);
1768 for (idx
= 0; idx
< len
; idx
++)
1771 arg
= TREE_VEC_ELT (argpack
, idx
);
1774 pp_cxx_separate_with (pp
, ',');
1778 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1779 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1782 pp
->expression (arg
);
1789 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1791 t
= DECL_EXPR_DECL (t
);
1792 pp_cxx_type_specifier_seq (pp
, t
);
1794 pp
->abstract_declarator (t
);
1802 cxx_pretty_printer::statement (tree t
)
1804 switch (TREE_CODE (t
))
1806 case CTOR_INITIALIZER
:
1807 pp_cxx_ctor_initializer (this, t
);
1811 pp_cxx_ws_string (this, "using");
1812 pp_cxx_ws_string (this, "namespace");
1813 if (DECL_CONTEXT (t
))
1814 pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t
));
1815 pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t
));
1819 pp_cxx_ws_string (this, "using");
1820 pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t
));
1821 pp_cxx_unqualified_id (this, DECL_NAME (t
));
1828 try compound-statement handler-seq */
1830 pp_maybe_newline_and_indent (this, 0);
1831 pp_cxx_ws_string (this, "try");
1832 pp_newline_and_indent (this, 3);
1833 statement (TRY_STMTS (t
));
1834 pp_newline_and_indent (this, -3);
1838 statement (TRY_HANDLERS (t
));
1843 handler handler-seq(opt)
1846 catch ( exception-declaration ) compound-statement
1848 exception-declaration:
1849 type-specifier-seq declarator
1850 type-specifier-seq abstract-declarator
1853 pp_cxx_ws_string (this, "catch");
1854 pp_cxx_left_paren (this);
1855 pp_cxx_exception_declaration (this, HANDLER_PARMS (t
));
1856 pp_cxx_right_paren (this);
1857 pp_indentation (this) += 3;
1858 pp_needs_newline (this) = true;
1859 statement (HANDLER_BODY (t
));
1860 pp_indentation (this) -= 3;
1861 pp_needs_newline (this) = true;
1864 /* selection-statement:
1865 if ( expression ) statement
1866 if ( expression ) statement else statement */
1868 pp_cxx_ws_string (this, "if");
1869 pp_cxx_whitespace (this);
1870 pp_cxx_left_paren (this);
1871 expression (IF_COND (t
));
1872 pp_cxx_right_paren (this);
1873 pp_newline_and_indent (this, 2);
1874 statement (THEN_CLAUSE (t
));
1875 pp_newline_and_indent (this, -2);
1876 if (ELSE_CLAUSE (t
))
1878 tree else_clause
= ELSE_CLAUSE (t
);
1879 pp_cxx_ws_string (this, "else");
1880 if (TREE_CODE (else_clause
) == IF_STMT
)
1881 pp_cxx_whitespace (this);
1883 pp_newline_and_indent (this, 2);
1884 statement (else_clause
);
1885 if (TREE_CODE (else_clause
) != IF_STMT
)
1886 pp_newline_and_indent (this, -2);
1891 pp_cxx_ws_string (this, "switch");
1893 pp_cxx_left_paren (this);
1894 expression (SWITCH_STMT_COND (t
));
1895 pp_cxx_right_paren (this);
1896 pp_indentation (this) += 3;
1897 pp_needs_newline (this) = true;
1898 statement (SWITCH_STMT_BODY (t
));
1899 pp_newline_and_indent (this, -3);
1902 /* iteration-statement:
1903 while ( expression ) statement
1904 do statement while ( expression ) ;
1905 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1906 for ( declaration expression(opt) ; expression(opt) ) statement */
1908 pp_cxx_ws_string (this, "while");
1910 pp_cxx_left_paren (this);
1911 expression (WHILE_COND (t
));
1912 pp_cxx_right_paren (this);
1913 pp_newline_and_indent (this, 3);
1914 statement (WHILE_BODY (t
));
1915 pp_indentation (this) -= 3;
1916 pp_needs_newline (this) = true;
1920 pp_cxx_ws_string (this, "do");
1921 pp_newline_and_indent (this, 3);
1922 statement (DO_BODY (t
));
1923 pp_newline_and_indent (this, -3);
1924 pp_cxx_ws_string (this, "while");
1926 pp_cxx_left_paren (this);
1927 expression (DO_COND (t
));
1928 pp_cxx_right_paren (this);
1929 pp_cxx_semicolon (this);
1930 pp_needs_newline (this) = true;
1934 pp_cxx_ws_string (this, "for");
1936 pp_cxx_left_paren (this);
1937 if (FOR_INIT_STMT (t
))
1938 statement (FOR_INIT_STMT (t
));
1940 pp_cxx_semicolon (this);
1941 pp_needs_newline (this) = false;
1942 pp_cxx_whitespace (this);
1944 expression (FOR_COND (t
));
1945 pp_cxx_semicolon (this);
1946 pp_needs_newline (this) = false;
1947 pp_cxx_whitespace (this);
1949 expression (FOR_EXPR (t
));
1950 pp_cxx_right_paren (this);
1951 pp_newline_and_indent (this, 3);
1952 statement (FOR_BODY (t
));
1953 pp_indentation (this) -= 3;
1954 pp_needs_newline (this) = true;
1957 case RANGE_FOR_STMT
:
1958 pp_cxx_ws_string (this, "for");
1960 pp_cxx_left_paren (this);
1961 statement (RANGE_FOR_DECL (t
));
1963 pp_needs_newline (this) = false;
1966 statement (RANGE_FOR_EXPR (t
));
1967 pp_cxx_right_paren (this);
1968 pp_newline_and_indent (this, 3);
1969 statement (FOR_BODY (t
));
1970 pp_indentation (this) -= 3;
1971 pp_needs_newline (this) = true;
1977 return expression(opt) ; */
1980 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
1981 pp_cxx_semicolon (this);
1982 pp_needs_newline (this) = true;
1985 /* expression-statement:
1986 expression(opt) ; */
1988 expression (EXPR_STMT_EXPR (t
));
1989 pp_cxx_semicolon (this);
1990 pp_needs_newline (this) = true;
1994 pp_cxx_ws_string (this, "try");
1995 pp_newline_and_indent (this, 2);
1996 statement (CLEANUP_BODY (t
));
1997 pp_newline_and_indent (this, -2);
1998 pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
1999 pp_newline_and_indent (this, 2);
2000 statement (CLEANUP_EXPR (t
));
2001 pp_newline_and_indent (this, -2);
2009 c_pretty_printer::statement (t
);
2014 /* original-namespace-definition:
2015 namespace identifier { namespace-body }
2017 As an edge case, we also handle unnamed namespace definition here. */
2020 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
2022 pp_cxx_ws_string (pp
, "namespace");
2023 if (DECL_CONTEXT (t
))
2024 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2026 pp_cxx_unqualified_id (pp
, t
);
2027 pp_cxx_whitespace (pp
);
2028 pp_cxx_left_brace (pp
);
2029 /* We do not print the namespace-body. */
2030 pp_cxx_whitespace (pp
);
2031 pp_cxx_right_brace (pp
);
2037 namespace-alias-definition:
2038 namespace identifier = qualified-namespace-specifier ;
2040 qualified-namespace-specifier:
2041 ::(opt) nested-name-specifier(opt) namespace-name */
2044 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
2046 pp_cxx_ws_string (pp
, "namespace");
2047 if (DECL_CONTEXT (t
))
2048 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2049 pp_cxx_unqualified_id (pp
, t
);
2050 pp_cxx_whitespace (pp
);
2052 pp_cxx_whitespace (pp
);
2053 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
2054 pp_cxx_nested_name_specifier (pp
,
2055 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
2056 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
2057 pp_cxx_semicolon (pp
);
2060 /* simple-declaration:
2061 decl-specifier-seq(opt) init-declarator-list(opt) */
2064 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
2066 pp
->declaration_specifiers (t
);
2067 pp_cxx_init_declarator (pp
, t
);
2068 pp_cxx_semicolon (pp
);
2069 pp_needs_newline (pp
) = true;
2073 template-parameter-list:
2075 template-parameter-list , template-parameter */
2078 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
2080 const int n
= TREE_VEC_LENGTH (t
);
2082 for (i
= 0; i
< n
; ++i
)
2085 pp_cxx_separate_with (pp
, ',');
2086 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2090 /* template-parameter:
2092 parameter-declaration
2095 class ...(opt) identifier(opt)
2096 class identifier(opt) = type-id
2097 typename identifier(opt)
2098 typename ...(opt) identifier(opt) = type-id
2099 template < template-parameter-list > class ...(opt) identifier(opt)
2100 template < template-parameter-list > class identifier(opt) = template-name */
2103 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2105 tree parameter
= TREE_VALUE (t
);
2106 switch (TREE_CODE (parameter
))
2109 pp_cxx_ws_string (pp
, "class");
2110 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2111 pp_cxx_ws_string (pp
, "...");
2112 if (DECL_NAME (parameter
))
2113 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2114 /* FIXME: Check if we should print also default argument. */
2118 pp_cxx_parameter_declaration (pp
, parameter
);
2125 pp_unsupported_tree (pp
, t
);
2130 /* Pretty-print a template parameter in the canonical form
2131 "template-parameter-<level>-<position in parameter list>". */
2134 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2136 const enum tree_code code
= TREE_CODE (parm
);
2138 /* Brings type template parameters to the canonical forms. */
2139 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2140 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2141 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2143 pp_cxx_begin_template_argument_list (pp
);
2144 pp
->translate_string ("template-parameter-");
2145 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2147 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2148 pp_cxx_end_template_argument_list (pp
);
2152 template-declaration:
2153 export(opt) template < template-parameter-list > declaration */
2156 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2158 tree tmpl
= most_general_template (t
);
2161 pp_maybe_newline_and_indent (pp
, 0);
2162 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2164 pp_cxx_ws_string (pp
, "template");
2165 pp_cxx_begin_template_argument_list (pp
);
2166 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2167 pp_cxx_end_template_argument_list (pp
);
2168 pp_newline_and_indent (pp
, 3);
2170 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2171 pp_cxx_function_definition (pp
, t
);
2173 pp_cxx_simple_declaration (pp
, t
);
2177 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2179 pp_unsupported_tree (pp
, t
);
2183 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2185 pp_unsupported_tree (pp
, t
);
2192 template-declaration
2193 explicit-instantiation
2194 explicit-specialization
2195 linkage-specification
2196 namespace-definition
2201 namespace-alias-definition
2204 static_assert-declaration */
2206 cxx_pretty_printer::declaration (tree t
)
2208 if (TREE_CODE (t
) == STATIC_ASSERT
)
2210 pp_cxx_ws_string (this, "static_assert");
2211 pp_cxx_left_paren (this);
2212 expression (STATIC_ASSERT_CONDITION (t
));
2213 pp_cxx_separate_with (this, ',');
2214 expression (STATIC_ASSERT_MESSAGE (t
));
2215 pp_cxx_right_paren (this);
2217 else if (!DECL_LANG_SPECIFIC (t
))
2218 pp_cxx_simple_declaration (this, t
);
2219 else if (DECL_USE_TEMPLATE (t
))
2220 switch (DECL_USE_TEMPLATE (t
))
2223 pp_cxx_template_declaration (this, t
);
2227 pp_cxx_explicit_specialization (this, t
);
2231 pp_cxx_explicit_instantiation (this, t
);
2237 else switch (TREE_CODE (t
))
2241 pp_cxx_simple_declaration (this, t
);
2245 if (DECL_SAVED_TREE (t
))
2246 pp_cxx_function_definition (this, t
);
2248 pp_cxx_simple_declaration (this, t
);
2251 case NAMESPACE_DECL
:
2252 if (DECL_NAMESPACE_ALIAS (t
))
2253 pp_cxx_namespace_alias_definition (this, t
);
2255 pp_cxx_original_namespace_definition (this, t
);
2259 pp_unsupported_tree (this, t
);
2265 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2267 t
= TREE_OPERAND (t
, 0);
2268 pp_cxx_ws_string (pp
, "typeid");
2269 pp_cxx_left_paren (pp
);
2274 pp_cxx_right_paren (pp
);
2278 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2280 pp_cxx_ws_string (pp
, "va_arg");
2281 pp_cxx_left_paren (pp
);
2282 pp
->assignment_expression (TREE_OPERAND (t
, 0));
2283 pp_cxx_separate_with (pp
, ',');
2284 pp
->type_id (TREE_TYPE (t
));
2285 pp_cxx_right_paren (pp
);
2289 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2291 switch (TREE_CODE (t
))
2294 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2295 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2297 pp
->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2298 pp_cxx_separate_with (pp
, ',');
2303 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2305 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2307 pp
->expression (TREE_OPERAND (t
, 1));
2310 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2312 pp_left_bracket (pp
);
2313 pp
->expression (TREE_OPERAND (t
, 1));
2314 pp_right_bracket (pp
);
2322 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2324 pp_cxx_ws_string (pp
, "offsetof");
2325 pp_cxx_left_paren (pp
);
2326 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2327 pp
->expression (TREE_OPERAND (t
, 0));
2328 pp_cxx_right_paren (pp
);
2332 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2334 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2338 case CPTK_HAS_NOTHROW_ASSIGN
:
2339 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2341 case CPTK_HAS_TRIVIAL_ASSIGN
:
2342 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2344 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2345 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2347 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2348 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2350 case CPTK_HAS_NOTHROW_COPY
:
2351 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2353 case CPTK_HAS_TRIVIAL_COPY
:
2354 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2356 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2357 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2359 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2360 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2362 case CPTK_IS_ABSTRACT
:
2363 pp_cxx_ws_string (pp
, "__is_abstract");
2365 case CPTK_IS_BASE_OF
:
2366 pp_cxx_ws_string (pp
, "__is_base_of");
2369 pp_cxx_ws_string (pp
, "__is_class");
2371 case CPTK_IS_CONVERTIBLE_TO
:
2372 pp_cxx_ws_string (pp
, "__is_convertible_to");
2375 pp_cxx_ws_string (pp
, "__is_empty");
2378 pp_cxx_ws_string (pp
, "__is_enum");
2381 pp_cxx_ws_string (pp
, "__is_final");
2384 pp_cxx_ws_string (pp
, "__is_pod");
2386 case CPTK_IS_POLYMORPHIC
:
2387 pp_cxx_ws_string (pp
, "__is_polymorphic");
2389 case CPTK_IS_STD_LAYOUT
:
2390 pp_cxx_ws_string (pp
, "__is_std_layout");
2392 case CPTK_IS_TRIVIAL
:
2393 pp_cxx_ws_string (pp
, "__is_trivial");
2396 pp_cxx_ws_string (pp
, "__is_union");
2398 case CPTK_IS_LITERAL_TYPE
:
2399 pp_cxx_ws_string (pp
, "__is_literal_type");
2406 pp_cxx_left_paren (pp
);
2407 pp
->type_id (TRAIT_EXPR_TYPE1 (t
));
2409 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2411 pp_cxx_separate_with (pp
, ',');
2412 pp
->type_id (TRAIT_EXPR_TYPE2 (t
));
2415 pp_cxx_right_paren (pp
);
2418 typedef c_pretty_print_fn pp_fun
;
2420 /* Initialization of a C++ pretty-printer object. */
2422 cxx_pretty_printer::cxx_pretty_printer ()
2423 : c_pretty_printer (),
2424 enclosing_scope (global_namespace
)
2426 pp_set_line_maximum_length (this, 0);
2428 type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2429 parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;
2430 simple_type_specifier
= (pp_fun
) pp_cxx_simple_type_specifier
;