1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003-2014 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
))
414 case USERDEF_LITERAL
:
415 pp_cxx_userdef_literal (this, t
);
419 t
= BASELINK_FUNCTIONS (t
);
431 case TEMPLATE_TYPE_PARM
:
432 case TEMPLATE_TEMPLATE_PARM
:
433 case TEMPLATE_PARM_INDEX
:
434 pp_cxx_unqualified_id (this, t
);
438 pp_cxx_left_paren (this);
439 statement (STMT_EXPR_STMT (t
));
440 pp_cxx_right_paren (this);
444 pp_cxx_trait_expression (this, t
);
448 pp_cxx_va_arg_expression (this, t
);
452 pp_cxx_offsetof_expression (this, t
);
456 c_pretty_printer::primary_expression (t
);
461 /* postfix-expression:
463 postfix-expression [ expression ]
464 postfix-expression ( expression-list(opt) )
465 simple-type-specifier ( expression-list(opt) )
466 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
467 typename ::(opt) nested-name-specifier template(opt)
468 template-id ( expression-list(opt) )
469 postfix-expression . template(opt) ::(opt) id-expression
470 postfix-expression -> template(opt) ::(opt) id-expression
471 postfix-expression . pseudo-destructor-name
472 postfix-expression -> pseudo-destructor-name
473 postfix-expression ++
474 postfix-expression --
475 dynamic_cast < type-id > ( expression )
476 static_cast < type-id > ( expression )
477 reinterpret_cast < type-id > ( expression )
478 const_cast < type-id > ( expression )
479 typeid ( expression )
480 typeid ( type-id ) */
483 cxx_pretty_printer::postfix_expression (tree t
)
485 enum tree_code code
= TREE_CODE (t
);
492 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
494 tree saved_scope
= enclosing_scope
;
495 bool skipfirst
= false;
498 if (TREE_CODE (fun
) == ADDR_EXPR
)
499 fun
= TREE_OPERAND (fun
, 0);
501 /* In templates, where there is no way to tell whether a given
502 call uses an actual member function. So the parser builds
503 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
504 instantiation time. */
505 if (TREE_CODE (fun
) != FUNCTION_DECL
)
507 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
509 tree object
= (code
== AGGR_INIT_EXPR
510 ? (AGGR_INIT_VIA_CTOR_P (t
)
511 ? AGGR_INIT_EXPR_SLOT (t
)
512 : AGGR_INIT_EXPR_ARG (t
, 0))
513 : CALL_EXPR_ARG (t
, 0));
515 while (TREE_CODE (object
) == NOP_EXPR
)
516 object
= TREE_OPERAND (object
, 0);
518 if (TREE_CODE (object
) == ADDR_EXPR
)
519 object
= TREE_OPERAND (object
, 0);
521 if (!TYPE_PTR_P (TREE_TYPE (object
)))
523 postfix_expression (object
);
528 postfix_expression (object
);
532 enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
535 postfix_expression (fun
);
536 enclosing_scope
= saved_scope
;
537 pp_cxx_left_paren (this);
538 if (code
== AGGR_INIT_EXPR
)
540 aggr_init_expr_arg_iterator iter
;
541 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
548 if (more_aggr_init_expr_args_p (&iter
))
549 pp_cxx_separate_with (this, ',');
555 call_expr_arg_iterator iter
;
556 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
563 if (more_call_expr_args_p (&iter
))
564 pp_cxx_separate_with (this, ',');
568 pp_cxx_right_paren (this);
570 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
572 pp_cxx_separate_with (this, ',');
573 postfix_expression (AGGR_INIT_EXPR_SLOT (t
));
586 primary_expression (t
);
589 case DYNAMIC_CAST_EXPR
:
590 case STATIC_CAST_EXPR
:
591 case REINTERPRET_CAST_EXPR
:
592 case CONST_CAST_EXPR
:
593 if (code
== DYNAMIC_CAST_EXPR
)
594 pp_cxx_ws_string (this, "dynamic_cast");
595 else if (code
== STATIC_CAST_EXPR
)
596 pp_cxx_ws_string (this, "static_cast");
597 else if (code
== REINTERPRET_CAST_EXPR
)
598 pp_cxx_ws_string (this, "reinterpret_cast");
600 pp_cxx_ws_string (this, "const_cast");
601 pp_cxx_begin_template_argument_list (this);
602 type_id (TREE_TYPE (t
));
603 pp_cxx_end_template_argument_list (this);
604 pp_left_paren (this);
605 expression (TREE_OPERAND (t
, 0));
606 pp_right_paren (this);
609 case EMPTY_CLASS_EXPR
:
610 type_id (TREE_TYPE (t
));
611 pp_left_paren (this);
612 pp_right_paren (this);
616 pp_cxx_typeid_expression (this, t
);
619 case PSEUDO_DTOR_EXPR
:
620 postfix_expression (TREE_OPERAND (t
, 0));
622 if (TREE_OPERAND (t
, 1))
624 pp_cxx_qualified_id (this, TREE_OPERAND (t
, 1));
625 pp_cxx_colon_colon (this);
627 pp_complement (this);
628 pp_cxx_unqualified_id (this, TREE_OPERAND (t
, 2));
632 postfix_expression (TREE_OPERAND (t
, 0));
637 c_pretty_printer::postfix_expression (t
);
643 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
644 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
650 type-specifier-seq new-declarator(opt)
653 ptr-operator new-declarator(opt)
654 direct-new-declarator
656 direct-new-declarator
658 direct-new-declarator [ constant-expression ]
661 ( expression-list(opt) ) */
664 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
666 enum tree_code code
= TREE_CODE (t
);
667 tree type
= TREE_OPERAND (t
, 1);
668 tree init
= TREE_OPERAND (t
, 2);
673 if (NEW_EXPR_USE_GLOBAL (t
))
674 pp_cxx_colon_colon (pp
);
675 pp_cxx_ws_string (pp
, "new");
676 if (TREE_OPERAND (t
, 0))
678 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
681 if (TREE_CODE (type
) == ARRAY_REF
)
682 type
= build_cplus_array_type
683 (TREE_OPERAND (type
, 0),
684 build_index_type (fold_build2_loc (input_location
,
685 MINUS_EXPR
, integer_type_node
,
686 TREE_OPERAND (type
, 1),
692 if (TREE_CODE (init
) == TREE_LIST
)
693 pp_c_expression_list (pp
, init
);
694 else if (init
== void_node
)
695 ; /* OK, empty initializer list. */
697 pp
->expression (init
);
703 pp_unsupported_tree (pp
, t
);
707 /* delete-expression:
708 ::(opt) delete cast-expression
709 ::(opt) delete [ ] cast-expression */
712 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
714 enum tree_code code
= TREE_CODE (t
);
718 case VEC_DELETE_EXPR
:
719 if (DELETE_EXPR_USE_GLOBAL (t
))
720 pp_cxx_colon_colon (pp
);
721 pp_cxx_ws_string (pp
, "delete");
723 if (code
== VEC_DELETE_EXPR
724 || DELETE_EXPR_USE_VEC (t
))
726 pp_left_bracket (pp
);
727 pp_right_bracket (pp
);
730 pp_c_cast_expression (pp
, TREE_OPERAND (t
, 0));
734 pp_unsupported_tree (pp
, t
);
742 unary-operator cast-expression
743 sizeof unary-expression
745 sizeof ... ( identifier )
749 unary-operator: one of
753 __alignof__ unary-expression
754 __alignof__ ( type-id ) */
757 cxx_pretty_printer::unary_expression (tree t
)
759 enum tree_code code
= TREE_CODE (t
);
764 pp_cxx_new_expression (this, t
);
768 case VEC_DELETE_EXPR
:
769 pp_cxx_delete_expression (this, t
);
773 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
775 pp_cxx_ws_string (this, "sizeof");
776 pp_cxx_ws_string (this, "...");
777 pp_cxx_whitespace (this);
778 pp_cxx_left_paren (this);
779 if (TYPE_P (TREE_OPERAND (t
, 0)))
780 type_id (TREE_OPERAND (t
, 0));
782 unary_expression (TREE_OPERAND (t
, 0));
783 pp_cxx_right_paren (this);
789 pp_cxx_ws_string (this, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
790 pp_cxx_whitespace (this);
791 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
793 pp_cxx_left_paren (this);
794 type_id (TREE_TYPE (TREE_OPERAND (t
, 0)));
795 pp_cxx_right_paren (this);
797 else if (TYPE_P (TREE_OPERAND (t
, 0)))
799 pp_cxx_left_paren (this);
800 type_id (TREE_OPERAND (t
, 0));
801 pp_cxx_right_paren (this);
804 unary_expression (TREE_OPERAND (t
, 0));
808 pp_cxx_ws_string (this, "@encode");
809 pp_cxx_whitespace (this);
810 pp_cxx_left_paren (this);
811 type_id (TREE_OPERAND (t
, 0));
812 pp_cxx_right_paren (this);
816 pp_cxx_ws_string (this, "noexcept");
817 pp_cxx_whitespace (this);
818 pp_cxx_left_paren (this);
819 expression (TREE_OPERAND (t
, 0));
820 pp_cxx_right_paren (this);
823 case UNARY_PLUS_EXPR
:
825 pp_cxx_cast_expression (this, TREE_OPERAND (t
, 0));
829 c_pretty_printer::unary_expression (t
);
836 ( type-id ) cast-expression */
839 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
841 switch (TREE_CODE (t
))
844 case IMPLICIT_CONV_EXPR
:
845 pp
->type_id (TREE_TYPE (t
));
846 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
850 pp_c_cast_expression (pp
, t
);
857 pm-expression .* cast-expression
858 pm-expression ->* cast-expression */
861 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
863 switch (TREE_CODE (t
))
865 /* Handle unfortunate OFFSET_REF overloading here. */
867 if (TYPE_P (TREE_OPERAND (t
, 0)))
869 pp_cxx_qualified_id (pp
, t
);
872 /* Else fall through. */
875 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
876 if (TREE_CODE (t
) == MEMBER_REF
)
881 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
886 pp_cxx_cast_expression (pp
, t
);
891 /* multiplicative-expression:
893 multiplicative-expression * pm-expression
894 multiplicative-expression / pm-expression
895 multiplicative-expression % pm-expression */
898 cxx_pretty_printer::multiplicative_expression (tree e
)
900 enum tree_code code
= TREE_CODE (e
);
906 multiplicative_expression (TREE_OPERAND (e
, 0));
908 if (code
== MULT_EXPR
)
910 else if (code
== TRUNC_DIV_EXPR
)
915 pp_cxx_pm_expression (this, TREE_OPERAND (e
, 1));
919 pp_cxx_pm_expression (this, e
);
924 /* conditional-expression:
925 logical-or-expression
926 logical-or-expression ? expression : assignment-expression */
929 cxx_pretty_printer::conditional_expression (tree e
)
931 if (TREE_CODE (e
) == COND_EXPR
)
933 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
937 expression (TREE_OPERAND (e
, 1));
939 assignment_expression (TREE_OPERAND (e
, 2));
942 pp_c_logical_or_expression (this, e
);
945 /* Pretty-print a compound assignment operator token as indicated by T. */
948 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
952 switch (TREE_CODE (t
))
975 op
= get_tree_code_name (TREE_CODE (t
));
979 pp_cxx_ws_string (pp
, op
);
983 /* assignment-expression:
984 conditional-expression
985 logical-or-expression assignment-operator assignment-expression
989 throw assignment-expression(opt)
991 assignment-operator: one of
992 = *= /= %= += -= >>= <<= &= ^= |= */
995 cxx_pretty_printer::assignment_expression (tree e
)
997 switch (TREE_CODE (e
))
1001 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1005 assignment_expression (TREE_OPERAND (e
, 1));
1009 pp_cxx_ws_string (this, "throw");
1010 if (TREE_OPERAND (e
, 0))
1011 assignment_expression (TREE_OPERAND (e
, 0));
1015 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1016 pp_cxx_assignment_operator (this, TREE_OPERAND (e
, 1));
1017 assignment_expression (TREE_OPERAND (e
, 2));
1021 conditional_expression (e
);
1027 cxx_pretty_printer::expression (tree t
)
1029 switch (TREE_CODE (t
))
1039 case USERDEF_LITERAL
:
1040 pp_cxx_userdef_literal (this, t
);
1044 pp_cxx_unqualified_id (this, t
);
1052 pp_cxx_qualified_id (this, t
);
1056 t
= OVL_CURRENT (t
);
1064 case TEMPLATE_TYPE_PARM
:
1065 case TEMPLATE_PARM_INDEX
:
1066 case TEMPLATE_TEMPLATE_PARM
:
1068 primary_expression (t
);
1072 case DYNAMIC_CAST_EXPR
:
1073 case STATIC_CAST_EXPR
:
1074 case REINTERPRET_CAST_EXPR
:
1075 case CONST_CAST_EXPR
:
1079 case EMPTY_CLASS_EXPR
:
1081 case PSEUDO_DTOR_EXPR
:
1082 case AGGR_INIT_EXPR
:
1084 postfix_expression (t
);
1089 pp_cxx_new_expression (this, t
);
1093 case VEC_DELETE_EXPR
:
1094 pp_cxx_delete_expression (this, t
);
1100 unary_expression (t
);
1104 case IMPLICIT_CONV_EXPR
:
1105 pp_cxx_cast_expression (this, t
);
1111 pp_cxx_pm_expression (this, t
);
1115 case TRUNC_DIV_EXPR
:
1116 case TRUNC_MOD_EXPR
:
1117 multiplicative_expression (t
);
1121 conditional_expression (t
);
1128 assignment_expression (t
);
1131 case NON_DEPENDENT_EXPR
:
1132 case MUST_NOT_THROW_EXPR
:
1133 expression (TREE_OPERAND (t
, 0));
1136 case EXPR_PACK_EXPANSION
:
1137 expression (PACK_EXPANSION_PATTERN (t
));
1138 pp_cxx_ws_string (this, "...");
1141 case TEMPLATE_ID_EXPR
:
1142 pp_cxx_template_id (this, t
);
1145 case NONTYPE_ARGUMENT_PACK
:
1147 tree args
= ARGUMENT_PACK_ARGS (t
);
1148 int i
, len
= TREE_VEC_LENGTH (args
);
1149 for (i
= 0; i
< len
; ++i
)
1152 pp_cxx_separate_with (this, ',');
1153 expression (TREE_VEC_ELT (args
, i
));
1159 pp_cxx_ws_string (this, "<lambda>");
1163 pp_cxx_left_paren (this);
1164 expression (TREE_OPERAND (t
, 0));
1165 pp_cxx_right_paren (this);
1169 c_pretty_printer::expression (t
);
1177 /* function-specifier:
1183 cxx_pretty_printer::function_specifier (tree t
)
1185 switch (TREE_CODE (t
))
1188 if (DECL_VIRTUAL_P (t
))
1189 pp_cxx_ws_string (this, "virtual");
1190 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1191 pp_cxx_ws_string (this, "explicit");
1193 c_pretty_printer::function_specifier (t
);
1200 /* decl-specifier-seq:
1201 decl-specifier-seq(opt) decl-specifier
1204 storage-class-specifier
1211 cxx_pretty_printer::declaration_specifiers (tree t
)
1213 switch (TREE_CODE (t
))
1219 storage_class_specifier (t
);
1220 declaration_specifiers (TREE_TYPE (t
));
1224 pp_cxx_ws_string (this, "typedef");
1225 declaration_specifiers (TREE_TYPE (t
));
1229 /* Constructors don't have return types. And conversion functions
1230 do not have a type-specifier in their return types. */
1231 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1232 function_specifier (t
);
1233 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1234 declaration_specifiers (TREE_TYPE (TREE_TYPE (t
)));
1237 c_pretty_printer::declaration_specifiers (t
);
1242 /* simple-type-specifier:
1243 ::(opt) nested-name-specifier(opt) type-name
1244 ::(opt) nested-name-specifier(opt) template(opt) template-id
1258 cxx_pretty_printer::simple_type_specifier (tree t
)
1260 switch (TREE_CODE (t
))
1265 pp_cxx_qualified_id (this, t
);
1268 case TEMPLATE_TYPE_PARM
:
1269 case TEMPLATE_TEMPLATE_PARM
:
1270 case TEMPLATE_PARM_INDEX
:
1271 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1272 pp_cxx_unqualified_id (this, t
);
1276 pp_cxx_ws_string (this, "typename");
1277 pp_cxx_nested_name_specifier (this, TYPE_CONTEXT (t
));
1278 pp_cxx_unqualified_id (this, TYPE_NAME (t
));
1282 c_pretty_printer::simple_type_specifier (t
);
1287 /* type-specifier-seq:
1288 type-specifier type-specifier-seq(opt)
1291 simple-type-specifier
1294 elaborated-type-specifier
1298 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1300 switch (TREE_CODE (t
))
1303 case TEMPLATE_TYPE_PARM
:
1304 case TEMPLATE_TEMPLATE_PARM
:
1306 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1307 pp_cxx_cv_qualifier_seq (pp
, t
);
1308 pp
->simple_type_specifier (t
);
1312 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1313 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1314 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1318 pp_cxx_ws_string (pp
, "decltype");
1319 pp_cxx_left_paren (pp
);
1320 pp
->expression (DECLTYPE_TYPE_EXPR (t
));
1321 pp_cxx_right_paren (pp
);
1325 if (TYPE_PTRMEMFUNC_P (t
))
1327 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1328 pp
->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm
)));
1329 pp_cxx_whitespace (pp
);
1330 pp_cxx_ptr_operator (pp
, t
);
1333 /* else fall through */
1336 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1337 pp_c_specifier_qualifier_list (pp
, t
);
1342 * cv-qualifier-seq(opt)
1344 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1347 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1349 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1351 switch (TREE_CODE (t
))
1353 case REFERENCE_TYPE
:
1355 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t
)))
1356 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1357 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1361 pp_cxx_cv_qualifier_seq (pp
, t
);
1368 if (TYPE_PTRMEMFUNC_P (t
))
1370 pp_cxx_left_paren (pp
);
1371 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1376 if (TYPE_PTRMEM_P (t
))
1378 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1379 pp_cxx_left_paren (pp
);
1380 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1382 pp_cxx_cv_qualifier_seq (pp
, t
);
1385 /* else fall through. */
1388 pp_unsupported_tree (pp
, t
);
1394 pp_cxx_implicit_parameter_type (tree mf
)
1396 return class_of_this_parm (TREE_TYPE (mf
));
1400 parameter-declaration:
1401 decl-specifier-seq declarator
1402 decl-specifier-seq declarator = assignment-expression
1403 decl-specifier-seq abstract-declarator(opt)
1404 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1407 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1409 pp
->declaration_specifiers (t
);
1411 pp
->abstract_declarator (t
);
1416 /* parameter-declaration-clause:
1417 parameter-declaration-list(opt) ...(opt)
1418 parameter-declaration-list , ...
1420 parameter-declaration-list:
1421 parameter-declaration
1422 parameter-declaration-list , parameter-declaration */
1425 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1427 tree args
= TYPE_P (t
) ? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1429 TYPE_P (t
) ? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1430 const bool abstract
= args
== NULL
|| pp
->flags
& pp_c_flag_abstract
;
1433 /* Skip artificial parameter for nonstatic member functions. */
1434 if (TREE_CODE (t
) == METHOD_TYPE
)
1435 types
= TREE_CHAIN (types
);
1437 pp_cxx_left_paren (pp
);
1438 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1441 pp_cxx_separate_with (pp
, ',');
1443 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1444 if (!abstract
&& pp
->flags
& pp_cxx_flag_default_argument
)
1446 pp_cxx_whitespace (pp
);
1448 pp_cxx_whitespace (pp
);
1449 pp
->assignment_expression (TREE_PURPOSE (types
));
1452 pp_cxx_right_paren (pp
);
1455 /* exception-specification:
1456 throw ( type-id-list(opt) )
1460 type-id-list , type-id */
1463 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1465 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1466 bool need_comma
= false;
1468 if (ex_spec
== NULL
)
1470 if (TREE_PURPOSE (ex_spec
))
1472 pp_cxx_ws_string (pp
, "noexcept");
1473 pp_cxx_whitespace (pp
);
1474 pp_cxx_left_paren (pp
);
1475 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec
))
1476 pp_cxx_ws_string (pp
, "<uninstantiated>");
1478 pp
->expression (TREE_PURPOSE (ex_spec
));
1479 pp_cxx_right_paren (pp
);
1482 pp_cxx_ws_string (pp
, "throw");
1483 pp_cxx_left_paren (pp
);
1484 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1486 tree type
= TREE_VALUE (ex_spec
);
1487 tree argpack
= NULL_TREE
;
1490 if (ARGUMENT_PACK_P (type
))
1492 argpack
= ARGUMENT_PACK_ARGS (type
);
1493 len
= TREE_VEC_LENGTH (argpack
);
1496 for (i
= 0; i
< len
; ++i
)
1499 type
= TREE_VEC_ELT (argpack
, i
);
1502 pp_cxx_separate_with (pp
, ',');
1509 pp_cxx_right_paren (pp
);
1512 /* direct-declarator:
1514 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1515 exception-specification(opt)
1516 direct-declaration [ constant-expression(opt) ]
1520 cxx_pretty_printer::direct_declarator (tree t
)
1522 switch (TREE_CODE (t
))
1530 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t
));
1532 if ((TREE_CODE (t
) == PARM_DECL
&& DECL_PACK_P (t
))
1533 || template_parameter_pack_p (t
))
1534 /* A function parameter pack or non-type template
1536 pp_cxx_ws_string (this, "...");
1538 id_expression (DECL_NAME (t
));
1540 abstract_declarator (TREE_TYPE (t
));
1544 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
1546 pp_cxx_parameter_declaration_clause (this, t
);
1548 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1550 padding
= pp_before
;
1551 pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t
));
1554 pp_cxx_exception_specification (this, TREE_TYPE (t
));
1559 case TEMPLATE_TYPE_PARM
:
1560 case TEMPLATE_PARM_INDEX
:
1561 case TEMPLATE_TEMPLATE_PARM
:
1565 c_pretty_printer::direct_declarator (t
);
1572 ptr-operator declarator */
1575 cxx_pretty_printer::declarator (tree t
)
1577 direct_declarator (t
);
1580 /* ctor-initializer:
1581 : mem-initializer-list
1583 mem-initializer-list:
1585 mem-initializer , mem-initializer-list
1588 mem-initializer-id ( expression-list(opt) )
1591 ::(opt) nested-name-specifier(opt) class-name
1595 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1597 t
= TREE_OPERAND (t
, 0);
1598 pp_cxx_whitespace (pp
);
1600 pp_cxx_whitespace (pp
);
1601 for (; t
; t
= TREE_CHAIN (t
))
1603 tree purpose
= TREE_PURPOSE (t
);
1604 bool is_pack
= PACK_EXPANSION_P (purpose
);
1607 pp
->primary_expression (PACK_EXPANSION_PATTERN (purpose
));
1609 pp
->primary_expression (purpose
);
1610 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1612 pp_cxx_ws_string (pp
, "...");
1614 pp_cxx_separate_with (pp
, ',');
1618 /* function-definition:
1619 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1620 decl-specifier-seq(opt) declarator function-try-block */
1623 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1625 tree saved_scope
= pp
->enclosing_scope
;
1626 pp
->declaration_specifiers (t
);
1628 pp_needs_newline (pp
) = true;
1629 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1630 if (DECL_SAVED_TREE (t
))
1631 pp
->statement (DECL_SAVED_TREE (t
));
1633 pp_cxx_semicolon (pp
);
1634 pp_newline_and_flush (pp
);
1635 pp
->enclosing_scope
= saved_scope
;
1638 /* abstract-declarator:
1639 ptr-operator abstract-declarator(opt)
1640 direct-abstract-declarator */
1643 cxx_pretty_printer::abstract_declarator (tree t
)
1645 if (TYPE_PTRMEM_P (t
))
1646 pp_cxx_right_paren (this);
1647 else if (POINTER_TYPE_P (t
))
1649 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1650 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1651 pp_cxx_right_paren (this);
1654 direct_abstract_declarator (t
);
1657 /* direct-abstract-declarator:
1658 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1659 cv-qualifier-seq(opt) exception-specification(opt)
1660 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1661 ( abstract-declarator ) */
1664 cxx_pretty_printer::direct_abstract_declarator (tree t
)
1666 switch (TREE_CODE (t
))
1668 case REFERENCE_TYPE
:
1669 abstract_declarator (t
);
1673 if (TYPE_PTRMEMFUNC_P (t
))
1674 direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1679 pp_cxx_parameter_declaration_clause (this, t
);
1680 direct_abstract_declarator (TREE_TYPE (t
));
1681 if (TREE_CODE (t
) == METHOD_TYPE
)
1683 padding
= pp_before
;
1684 pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t
));
1686 pp_cxx_exception_specification (this, t
);
1690 case TEMPLATE_TYPE_PARM
:
1691 case TEMPLATE_TEMPLATE_PARM
:
1692 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1693 case UNBOUND_CLASS_TEMPLATE
:
1697 c_pretty_printer::direct_abstract_declarator (t
);
1703 type-specifier-seq abstract-declarator(opt) */
1706 cxx_pretty_printer::type_id (tree t
)
1708 pp_flags saved_flags
= flags
;
1709 flags
|= pp_c_flag_abstract
;
1711 switch (TREE_CODE (t
))
1718 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1719 case UNBOUND_CLASS_TEMPLATE
:
1720 case TEMPLATE_TEMPLATE_PARM
:
1721 case TEMPLATE_TYPE_PARM
:
1722 case TEMPLATE_PARM_INDEX
:
1725 case UNDERLYING_TYPE
:
1727 case TEMPLATE_ID_EXPR
:
1728 pp_cxx_type_specifier_seq (this, t
);
1731 case TYPE_PACK_EXPANSION
:
1732 type_id (PACK_EXPANSION_PATTERN (t
));
1733 pp_cxx_ws_string (this, "...");
1737 c_pretty_printer::type_id (t
);
1741 flags
= saved_flags
;
1744 /* template-argument-list:
1745 template-argument ...(opt)
1746 template-argument-list, template-argument ...(opt)
1749 assignment-expression
1754 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1757 bool need_comma
= false;
1761 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1763 tree arg
= TREE_VEC_ELT (t
, i
);
1764 tree argpack
= NULL_TREE
;
1767 if (ARGUMENT_PACK_P (arg
))
1769 argpack
= ARGUMENT_PACK_ARGS (arg
);
1770 len
= TREE_VEC_LENGTH (argpack
);
1773 for (idx
= 0; idx
< len
; idx
++)
1776 arg
= TREE_VEC_ELT (argpack
, idx
);
1779 pp_cxx_separate_with (pp
, ',');
1783 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1784 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1787 pp
->expression (arg
);
1794 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1796 t
= DECL_EXPR_DECL (t
);
1797 pp_cxx_type_specifier_seq (pp
, t
);
1799 pp
->abstract_declarator (t
);
1807 cxx_pretty_printer::statement (tree t
)
1809 switch (TREE_CODE (t
))
1811 case CTOR_INITIALIZER
:
1812 pp_cxx_ctor_initializer (this, t
);
1816 pp_cxx_ws_string (this, "using");
1817 pp_cxx_ws_string (this, "namespace");
1818 if (DECL_CONTEXT (t
))
1819 pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t
));
1820 pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t
));
1824 pp_cxx_ws_string (this, "using");
1825 pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t
));
1826 pp_cxx_unqualified_id (this, DECL_NAME (t
));
1833 try compound-statement handler-seq */
1835 pp_maybe_newline_and_indent (this, 0);
1836 pp_cxx_ws_string (this, "try");
1837 pp_newline_and_indent (this, 3);
1838 statement (TRY_STMTS (t
));
1839 pp_newline_and_indent (this, -3);
1843 statement (TRY_HANDLERS (t
));
1848 handler handler-seq(opt)
1851 catch ( exception-declaration ) compound-statement
1853 exception-declaration:
1854 type-specifier-seq declarator
1855 type-specifier-seq abstract-declarator
1858 pp_cxx_ws_string (this, "catch");
1859 pp_cxx_left_paren (this);
1860 pp_cxx_exception_declaration (this, HANDLER_PARMS (t
));
1861 pp_cxx_right_paren (this);
1862 pp_indentation (this) += 3;
1863 pp_needs_newline (this) = true;
1864 statement (HANDLER_BODY (t
));
1865 pp_indentation (this) -= 3;
1866 pp_needs_newline (this) = true;
1869 /* selection-statement:
1870 if ( expression ) statement
1871 if ( expression ) statement else statement */
1873 pp_cxx_ws_string (this, "if");
1874 pp_cxx_whitespace (this);
1875 pp_cxx_left_paren (this);
1876 expression (IF_COND (t
));
1877 pp_cxx_right_paren (this);
1878 pp_newline_and_indent (this, 2);
1879 statement (THEN_CLAUSE (t
));
1880 pp_newline_and_indent (this, -2);
1881 if (ELSE_CLAUSE (t
))
1883 tree else_clause
= ELSE_CLAUSE (t
);
1884 pp_cxx_ws_string (this, "else");
1885 if (TREE_CODE (else_clause
) == IF_STMT
)
1886 pp_cxx_whitespace (this);
1888 pp_newline_and_indent (this, 2);
1889 statement (else_clause
);
1890 if (TREE_CODE (else_clause
) != IF_STMT
)
1891 pp_newline_and_indent (this, -2);
1896 pp_cxx_ws_string (this, "switch");
1898 pp_cxx_left_paren (this);
1899 expression (SWITCH_STMT_COND (t
));
1900 pp_cxx_right_paren (this);
1901 pp_indentation (this) += 3;
1902 pp_needs_newline (this) = true;
1903 statement (SWITCH_STMT_BODY (t
));
1904 pp_newline_and_indent (this, -3);
1907 /* iteration-statement:
1908 while ( expression ) statement
1909 do statement while ( expression ) ;
1910 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1911 for ( declaration expression(opt) ; expression(opt) ) statement */
1913 pp_cxx_ws_string (this, "while");
1915 pp_cxx_left_paren (this);
1916 expression (WHILE_COND (t
));
1917 pp_cxx_right_paren (this);
1918 pp_newline_and_indent (this, 3);
1919 statement (WHILE_BODY (t
));
1920 pp_indentation (this) -= 3;
1921 pp_needs_newline (this) = true;
1925 pp_cxx_ws_string (this, "do");
1926 pp_newline_and_indent (this, 3);
1927 statement (DO_BODY (t
));
1928 pp_newline_and_indent (this, -3);
1929 pp_cxx_ws_string (this, "while");
1931 pp_cxx_left_paren (this);
1932 expression (DO_COND (t
));
1933 pp_cxx_right_paren (this);
1934 pp_cxx_semicolon (this);
1935 pp_needs_newline (this) = true;
1939 pp_cxx_ws_string (this, "for");
1941 pp_cxx_left_paren (this);
1942 if (FOR_INIT_STMT (t
))
1943 statement (FOR_INIT_STMT (t
));
1945 pp_cxx_semicolon (this);
1946 pp_needs_newline (this) = false;
1947 pp_cxx_whitespace (this);
1949 expression (FOR_COND (t
));
1950 pp_cxx_semicolon (this);
1951 pp_needs_newline (this) = false;
1952 pp_cxx_whitespace (this);
1954 expression (FOR_EXPR (t
));
1955 pp_cxx_right_paren (this);
1956 pp_newline_and_indent (this, 3);
1957 statement (FOR_BODY (t
));
1958 pp_indentation (this) -= 3;
1959 pp_needs_newline (this) = true;
1962 case RANGE_FOR_STMT
:
1963 pp_cxx_ws_string (this, "for");
1965 pp_cxx_left_paren (this);
1966 statement (RANGE_FOR_DECL (t
));
1968 pp_needs_newline (this) = false;
1971 statement (RANGE_FOR_EXPR (t
));
1972 pp_cxx_right_paren (this);
1973 pp_newline_and_indent (this, 3);
1974 statement (FOR_BODY (t
));
1975 pp_indentation (this) -= 3;
1976 pp_needs_newline (this) = true;
1982 return expression(opt) ; */
1985 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
1986 pp_cxx_semicolon (this);
1987 pp_needs_newline (this) = true;
1990 /* expression-statement:
1991 expression(opt) ; */
1993 expression (EXPR_STMT_EXPR (t
));
1994 pp_cxx_semicolon (this);
1995 pp_needs_newline (this) = true;
1999 pp_cxx_ws_string (this, "try");
2000 pp_newline_and_indent (this, 2);
2001 statement (CLEANUP_BODY (t
));
2002 pp_newline_and_indent (this, -2);
2003 pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
2004 pp_newline_and_indent (this, 2);
2005 statement (CLEANUP_EXPR (t
));
2006 pp_newline_and_indent (this, -2);
2014 c_pretty_printer::statement (t
);
2019 /* original-namespace-definition:
2020 namespace identifier { namespace-body }
2022 As an edge case, we also handle unnamed namespace definition here. */
2025 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
2027 pp_cxx_ws_string (pp
, "namespace");
2028 if (DECL_CONTEXT (t
))
2029 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2031 pp_cxx_unqualified_id (pp
, t
);
2032 pp_cxx_whitespace (pp
);
2033 pp_cxx_left_brace (pp
);
2034 /* We do not print the namespace-body. */
2035 pp_cxx_whitespace (pp
);
2036 pp_cxx_right_brace (pp
);
2042 namespace-alias-definition:
2043 namespace identifier = qualified-namespace-specifier ;
2045 qualified-namespace-specifier:
2046 ::(opt) nested-name-specifier(opt) namespace-name */
2049 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
2051 pp_cxx_ws_string (pp
, "namespace");
2052 if (DECL_CONTEXT (t
))
2053 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2054 pp_cxx_unqualified_id (pp
, t
);
2055 pp_cxx_whitespace (pp
);
2057 pp_cxx_whitespace (pp
);
2058 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
2059 pp_cxx_nested_name_specifier (pp
,
2060 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
2061 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
2062 pp_cxx_semicolon (pp
);
2065 /* simple-declaration:
2066 decl-specifier-seq(opt) init-declarator-list(opt) */
2069 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
2071 pp
->declaration_specifiers (t
);
2072 pp_cxx_init_declarator (pp
, t
);
2073 pp_cxx_semicolon (pp
);
2074 pp_needs_newline (pp
) = true;
2078 template-parameter-list:
2080 template-parameter-list , template-parameter */
2083 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
2085 const int n
= TREE_VEC_LENGTH (t
);
2087 for (i
= 0; i
< n
; ++i
)
2090 pp_cxx_separate_with (pp
, ',');
2091 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2095 /* template-parameter:
2097 parameter-declaration
2100 class ...(opt) identifier(opt)
2101 class identifier(opt) = type-id
2102 typename identifier(opt)
2103 typename ...(opt) identifier(opt) = type-id
2104 template < template-parameter-list > class ...(opt) identifier(opt)
2105 template < template-parameter-list > class identifier(opt) = template-name */
2108 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2110 tree parameter
= TREE_VALUE (t
);
2111 switch (TREE_CODE (parameter
))
2114 pp_cxx_ws_string (pp
, "class");
2115 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2116 pp_cxx_ws_string (pp
, "...");
2117 if (DECL_NAME (parameter
))
2118 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2119 /* FIXME: Check if we should print also default argument. */
2123 pp_cxx_parameter_declaration (pp
, parameter
);
2130 pp_unsupported_tree (pp
, t
);
2135 /* Pretty-print a template parameter in the canonical form
2136 "template-parameter-<level>-<position in parameter list>". */
2139 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2141 const enum tree_code code
= TREE_CODE (parm
);
2143 /* Brings type template parameters to the canonical forms. */
2144 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2145 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2146 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2148 pp_cxx_begin_template_argument_list (pp
);
2149 pp
->translate_string ("template-parameter-");
2150 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2152 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2153 pp_cxx_end_template_argument_list (pp
);
2157 template-declaration:
2158 export(opt) template < template-parameter-list > declaration */
2161 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2163 tree tmpl
= most_general_template (t
);
2166 pp_maybe_newline_and_indent (pp
, 0);
2167 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2169 pp_cxx_ws_string (pp
, "template");
2170 pp_cxx_begin_template_argument_list (pp
);
2171 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2172 pp_cxx_end_template_argument_list (pp
);
2173 pp_newline_and_indent (pp
, 3);
2175 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2176 pp_cxx_function_definition (pp
, t
);
2178 pp_cxx_simple_declaration (pp
, t
);
2182 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2184 pp_unsupported_tree (pp
, t
);
2188 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2190 pp_unsupported_tree (pp
, t
);
2197 template-declaration
2198 explicit-instantiation
2199 explicit-specialization
2200 linkage-specification
2201 namespace-definition
2206 namespace-alias-definition
2209 static_assert-declaration */
2211 cxx_pretty_printer::declaration (tree t
)
2213 if (TREE_CODE (t
) == STATIC_ASSERT
)
2215 pp_cxx_ws_string (this, "static_assert");
2216 pp_cxx_left_paren (this);
2217 expression (STATIC_ASSERT_CONDITION (t
));
2218 pp_cxx_separate_with (this, ',');
2219 expression (STATIC_ASSERT_MESSAGE (t
));
2220 pp_cxx_right_paren (this);
2222 else if (!DECL_LANG_SPECIFIC (t
))
2223 pp_cxx_simple_declaration (this, t
);
2224 else if (DECL_USE_TEMPLATE (t
))
2225 switch (DECL_USE_TEMPLATE (t
))
2228 pp_cxx_template_declaration (this, t
);
2232 pp_cxx_explicit_specialization (this, t
);
2236 pp_cxx_explicit_instantiation (this, t
);
2242 else switch (TREE_CODE (t
))
2246 pp_cxx_simple_declaration (this, t
);
2250 if (DECL_SAVED_TREE (t
))
2251 pp_cxx_function_definition (this, t
);
2253 pp_cxx_simple_declaration (this, t
);
2256 case NAMESPACE_DECL
:
2257 if (DECL_NAMESPACE_ALIAS (t
))
2258 pp_cxx_namespace_alias_definition (this, t
);
2260 pp_cxx_original_namespace_definition (this, t
);
2264 pp_unsupported_tree (this, t
);
2270 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2272 t
= TREE_OPERAND (t
, 0);
2273 pp_cxx_ws_string (pp
, "typeid");
2274 pp_cxx_left_paren (pp
);
2279 pp_cxx_right_paren (pp
);
2283 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2285 pp_cxx_ws_string (pp
, "va_arg");
2286 pp_cxx_left_paren (pp
);
2287 pp
->assignment_expression (TREE_OPERAND (t
, 0));
2288 pp_cxx_separate_with (pp
, ',');
2289 pp
->type_id (TREE_TYPE (t
));
2290 pp_cxx_right_paren (pp
);
2294 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2296 switch (TREE_CODE (t
))
2299 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2300 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2302 pp
->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2303 pp_cxx_separate_with (pp
, ',');
2308 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2310 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2312 pp
->expression (TREE_OPERAND (t
, 1));
2315 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2317 pp_left_bracket (pp
);
2318 pp
->expression (TREE_OPERAND (t
, 1));
2319 pp_right_bracket (pp
);
2327 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2329 pp_cxx_ws_string (pp
, "offsetof");
2330 pp_cxx_left_paren (pp
);
2331 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2332 pp
->expression (TREE_OPERAND (t
, 0));
2333 pp_cxx_right_paren (pp
);
2337 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2339 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2343 case CPTK_HAS_NOTHROW_ASSIGN
:
2344 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2346 case CPTK_HAS_TRIVIAL_ASSIGN
:
2347 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2349 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2350 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2352 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2353 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2355 case CPTK_HAS_NOTHROW_COPY
:
2356 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2358 case CPTK_HAS_TRIVIAL_COPY
:
2359 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2361 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2362 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2364 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2365 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2367 case CPTK_IS_ABSTRACT
:
2368 pp_cxx_ws_string (pp
, "__is_abstract");
2370 case CPTK_IS_BASE_OF
:
2371 pp_cxx_ws_string (pp
, "__is_base_of");
2374 pp_cxx_ws_string (pp
, "__is_class");
2376 case CPTK_IS_CONVERTIBLE_TO
:
2377 pp_cxx_ws_string (pp
, "__is_convertible_to");
2380 pp_cxx_ws_string (pp
, "__is_empty");
2383 pp_cxx_ws_string (pp
, "__is_enum");
2386 pp_cxx_ws_string (pp
, "__is_final");
2389 pp_cxx_ws_string (pp
, "__is_pod");
2391 case CPTK_IS_POLYMORPHIC
:
2392 pp_cxx_ws_string (pp
, "__is_polymorphic");
2394 case CPTK_IS_STD_LAYOUT
:
2395 pp_cxx_ws_string (pp
, "__is_std_layout");
2397 case CPTK_IS_TRIVIAL
:
2398 pp_cxx_ws_string (pp
, "__is_trivial");
2401 pp_cxx_ws_string (pp
, "__is_union");
2403 case CPTK_IS_LITERAL_TYPE
:
2404 pp_cxx_ws_string (pp
, "__is_literal_type");
2411 pp_cxx_left_paren (pp
);
2412 pp
->type_id (TRAIT_EXPR_TYPE1 (t
));
2414 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2416 pp_cxx_separate_with (pp
, ',');
2417 pp
->type_id (TRAIT_EXPR_TYPE2 (t
));
2420 pp_cxx_right_paren (pp
);
2423 typedef c_pretty_print_fn pp_fun
;
2425 /* Initialization of a C++ pretty-printer object. */
2427 cxx_pretty_printer::cxx_pretty_printer ()
2428 : c_pretty_printer (),
2429 enclosing_scope (global_namespace
)
2431 pp_set_line_maximum_length (this, 0);
2433 type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2434 parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;