1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003-2015 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_empty ( type-id )
392 __is_enum ( type-id )
393 __is_literal_type ( type-id )
395 __is_polymorphic ( type-id )
396 __is_std_layout ( type-id )
397 __is_trivial ( type-id )
398 __is_union ( type-id ) */
401 cxx_pretty_printer::primary_expression (tree t
)
403 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 pp_cxx_requires_expr (this, t
);
459 c_pretty_printer::primary_expression (t
);
464 /* postfix-expression:
466 postfix-expression [ expression ]
467 postfix-expression ( expression-list(opt) )
468 simple-type-specifier ( expression-list(opt) )
469 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
470 typename ::(opt) nested-name-specifier template(opt)
471 template-id ( expression-list(opt) )
472 postfix-expression . template(opt) ::(opt) id-expression
473 postfix-expression -> template(opt) ::(opt) id-expression
474 postfix-expression . pseudo-destructor-name
475 postfix-expression -> pseudo-destructor-name
476 postfix-expression ++
477 postfix-expression --
478 dynamic_cast < type-id > ( expression )
479 static_cast < type-id > ( expression )
480 reinterpret_cast < type-id > ( expression )
481 const_cast < type-id > ( expression )
482 typeid ( expression )
483 typeid ( type-id ) */
486 cxx_pretty_printer::postfix_expression (tree t
)
488 enum tree_code code
= TREE_CODE (t
);
495 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
497 tree saved_scope
= enclosing_scope
;
498 bool skipfirst
= false;
501 if (TREE_CODE (fun
) == ADDR_EXPR
)
502 fun
= TREE_OPERAND (fun
, 0);
504 /* In templates, where there is no way to tell whether a given
505 call uses an actual member function. So the parser builds
506 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
507 instantiation time. */
508 if (TREE_CODE (fun
) != FUNCTION_DECL
)
510 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
512 tree object
= (code
== AGGR_INIT_EXPR
513 ? (AGGR_INIT_VIA_CTOR_P (t
)
514 ? AGGR_INIT_EXPR_SLOT (t
)
515 : AGGR_INIT_EXPR_ARG (t
, 0))
516 : CALL_EXPR_ARG (t
, 0));
518 while (TREE_CODE (object
) == NOP_EXPR
)
519 object
= TREE_OPERAND (object
, 0);
521 if (TREE_CODE (object
) == ADDR_EXPR
)
522 object
= TREE_OPERAND (object
, 0);
524 if (!TYPE_PTR_P (TREE_TYPE (object
)))
526 postfix_expression (object
);
531 postfix_expression (object
);
535 enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
538 postfix_expression (fun
);
539 enclosing_scope
= saved_scope
;
540 pp_cxx_left_paren (this);
541 if (code
== AGGR_INIT_EXPR
)
543 aggr_init_expr_arg_iterator iter
;
544 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
551 if (more_aggr_init_expr_args_p (&iter
))
552 pp_cxx_separate_with (this, ',');
558 call_expr_arg_iterator iter
;
559 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
566 if (more_call_expr_args_p (&iter
))
567 pp_cxx_separate_with (this, ',');
571 pp_cxx_right_paren (this);
573 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
575 pp_cxx_separate_with (this, ',');
576 postfix_expression (AGGR_INIT_EXPR_SLOT (t
));
589 primary_expression (t
);
592 case DYNAMIC_CAST_EXPR
:
593 case STATIC_CAST_EXPR
:
594 case REINTERPRET_CAST_EXPR
:
595 case CONST_CAST_EXPR
:
596 if (code
== DYNAMIC_CAST_EXPR
)
597 pp_cxx_ws_string (this, "dynamic_cast");
598 else if (code
== STATIC_CAST_EXPR
)
599 pp_cxx_ws_string (this, "static_cast");
600 else if (code
== REINTERPRET_CAST_EXPR
)
601 pp_cxx_ws_string (this, "reinterpret_cast");
603 pp_cxx_ws_string (this, "const_cast");
604 pp_cxx_begin_template_argument_list (this);
605 type_id (TREE_TYPE (t
));
606 pp_cxx_end_template_argument_list (this);
607 pp_left_paren (this);
608 expression (TREE_OPERAND (t
, 0));
609 pp_right_paren (this);
612 case EMPTY_CLASS_EXPR
:
613 type_id (TREE_TYPE (t
));
614 pp_left_paren (this);
615 pp_right_paren (this);
619 pp_cxx_typeid_expression (this, t
);
622 case PSEUDO_DTOR_EXPR
:
623 postfix_expression (TREE_OPERAND (t
, 0));
625 if (TREE_OPERAND (t
, 1))
627 pp_cxx_qualified_id (this, TREE_OPERAND (t
, 1));
628 pp_cxx_colon_colon (this);
630 pp_complement (this);
631 pp_cxx_unqualified_id (this, TREE_OPERAND (t
, 2));
635 postfix_expression (TREE_OPERAND (t
, 0));
640 c_pretty_printer::postfix_expression (t
);
646 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
647 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
653 type-specifier-seq new-declarator(opt)
656 ptr-operator new-declarator(opt)
657 direct-new-declarator
659 direct-new-declarator
661 direct-new-declarator [ constant-expression ]
664 ( expression-list(opt) ) */
667 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
669 enum tree_code code
= TREE_CODE (t
);
670 tree type
= TREE_OPERAND (t
, 1);
671 tree init
= TREE_OPERAND (t
, 2);
676 if (NEW_EXPR_USE_GLOBAL (t
))
677 pp_cxx_colon_colon (pp
);
678 pp_cxx_ws_string (pp
, "new");
679 if (TREE_OPERAND (t
, 0))
681 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
684 if (TREE_CODE (type
) == ARRAY_REF
)
685 type
= build_cplus_array_type
686 (TREE_OPERAND (type
, 0),
687 build_index_type (fold_build2_loc (input_location
,
688 MINUS_EXPR
, integer_type_node
,
689 TREE_OPERAND (type
, 1),
695 if (TREE_CODE (init
) == TREE_LIST
)
696 pp_c_expression_list (pp
, init
);
697 else if (init
== void_node
)
698 ; /* OK, empty initializer list. */
700 pp
->expression (init
);
706 pp_unsupported_tree (pp
, t
);
710 /* delete-expression:
711 ::(opt) delete cast-expression
712 ::(opt) delete [ ] cast-expression */
715 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
717 enum tree_code code
= TREE_CODE (t
);
721 case VEC_DELETE_EXPR
:
722 if (DELETE_EXPR_USE_GLOBAL (t
))
723 pp_cxx_colon_colon (pp
);
724 pp_cxx_ws_string (pp
, "delete");
726 if (code
== VEC_DELETE_EXPR
727 || DELETE_EXPR_USE_VEC (t
))
729 pp_left_bracket (pp
);
730 pp_right_bracket (pp
);
733 pp_c_cast_expression (pp
, TREE_OPERAND (t
, 0));
737 pp_unsupported_tree (pp
, t
);
745 unary-operator cast-expression
746 sizeof unary-expression
748 sizeof ... ( identifier )
752 unary-operator: one of
756 __alignof__ unary-expression
757 __alignof__ ( type-id ) */
760 cxx_pretty_printer::unary_expression (tree t
)
762 enum tree_code code
= TREE_CODE (t
);
767 pp_cxx_new_expression (this, t
);
771 case VEC_DELETE_EXPR
:
772 pp_cxx_delete_expression (this, t
);
776 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
778 pp_cxx_ws_string (this, "sizeof");
779 pp_cxx_ws_string (this, "...");
780 pp_cxx_whitespace (this);
781 pp_cxx_left_paren (this);
782 if (TYPE_P (TREE_OPERAND (t
, 0)))
783 type_id (TREE_OPERAND (t
, 0));
785 unary_expression (TREE_OPERAND (t
, 0));
786 pp_cxx_right_paren (this);
792 pp_cxx_ws_string (this, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
793 pp_cxx_whitespace (this);
794 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
796 pp_cxx_left_paren (this);
797 type_id (TREE_TYPE (TREE_OPERAND (t
, 0)));
798 pp_cxx_right_paren (this);
800 else if (TYPE_P (TREE_OPERAND (t
, 0)))
802 pp_cxx_left_paren (this);
803 type_id (TREE_OPERAND (t
, 0));
804 pp_cxx_right_paren (this);
807 unary_expression (TREE_OPERAND (t
, 0));
811 pp_cxx_ws_string (this, "@encode");
812 pp_cxx_whitespace (this);
813 pp_cxx_left_paren (this);
814 type_id (TREE_OPERAND (t
, 0));
815 pp_cxx_right_paren (this);
819 pp_cxx_ws_string (this, "noexcept");
820 pp_cxx_whitespace (this);
821 pp_cxx_left_paren (this);
822 expression (TREE_OPERAND (t
, 0));
823 pp_cxx_right_paren (this);
826 case UNARY_PLUS_EXPR
:
828 pp_cxx_cast_expression (this, TREE_OPERAND (t
, 0));
832 c_pretty_printer::unary_expression (t
);
839 ( type-id ) cast-expression */
842 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
844 switch (TREE_CODE (t
))
847 case IMPLICIT_CONV_EXPR
:
848 pp
->type_id (TREE_TYPE (t
));
849 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
853 pp_c_cast_expression (pp
, t
);
860 pm-expression .* cast-expression
861 pm-expression ->* cast-expression */
864 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
866 switch (TREE_CODE (t
))
868 /* Handle unfortunate OFFSET_REF overloading here. */
870 if (TYPE_P (TREE_OPERAND (t
, 0)))
872 pp_cxx_qualified_id (pp
, t
);
875 /* Else fall through. */
878 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
879 if (TREE_CODE (t
) == MEMBER_REF
)
884 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
889 pp_cxx_cast_expression (pp
, t
);
894 /* multiplicative-expression:
896 multiplicative-expression * pm-expression
897 multiplicative-expression / pm-expression
898 multiplicative-expression % pm-expression */
901 cxx_pretty_printer::multiplicative_expression (tree e
)
903 enum tree_code code
= TREE_CODE (e
);
909 multiplicative_expression (TREE_OPERAND (e
, 0));
911 if (code
== MULT_EXPR
)
913 else if (code
== TRUNC_DIV_EXPR
)
918 pp_cxx_pm_expression (this, TREE_OPERAND (e
, 1));
922 pp_cxx_pm_expression (this, e
);
927 /* conditional-expression:
928 logical-or-expression
929 logical-or-expression ? expression : assignment-expression */
932 cxx_pretty_printer::conditional_expression (tree e
)
934 if (TREE_CODE (e
) == COND_EXPR
)
936 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
940 expression (TREE_OPERAND (e
, 1));
942 assignment_expression (TREE_OPERAND (e
, 2));
945 pp_c_logical_or_expression (this, e
);
948 /* Pretty-print a compound assignment operator token as indicated by T. */
951 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
955 switch (TREE_CODE (t
))
978 op
= get_tree_code_name (TREE_CODE (t
));
982 pp_cxx_ws_string (pp
, op
);
986 /* assignment-expression:
987 conditional-expression
988 logical-or-expression assignment-operator assignment-expression
992 throw assignment-expression(opt)
994 assignment-operator: one of
995 = *= /= %= += -= >>= <<= &= ^= |= */
998 cxx_pretty_printer::assignment_expression (tree e
)
1000 switch (TREE_CODE (e
))
1004 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1008 assignment_expression (TREE_OPERAND (e
, 1));
1012 pp_cxx_ws_string (this, "throw");
1013 if (TREE_OPERAND (e
, 0))
1014 assignment_expression (TREE_OPERAND (e
, 0));
1018 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1019 pp_cxx_assignment_operator (this, TREE_OPERAND (e
, 1));
1020 assignment_expression (TREE_OPERAND (e
, 2));
1024 conditional_expression (e
);
1030 cxx_pretty_printer::expression (tree t
)
1032 switch (TREE_CODE (t
))
1042 case USERDEF_LITERAL
:
1043 pp_cxx_userdef_literal (this, t
);
1047 pp_cxx_unqualified_id (this, t
);
1055 pp_cxx_qualified_id (this, t
);
1059 t
= OVL_CURRENT (t
);
1067 case TEMPLATE_TYPE_PARM
:
1068 case TEMPLATE_PARM_INDEX
:
1069 case TEMPLATE_TEMPLATE_PARM
:
1072 primary_expression (t
);
1076 case DYNAMIC_CAST_EXPR
:
1077 case STATIC_CAST_EXPR
:
1078 case REINTERPRET_CAST_EXPR
:
1079 case CONST_CAST_EXPR
:
1083 case EMPTY_CLASS_EXPR
:
1085 case PSEUDO_DTOR_EXPR
:
1086 case AGGR_INIT_EXPR
:
1088 postfix_expression (t
);
1093 pp_cxx_new_expression (this, t
);
1097 case VEC_DELETE_EXPR
:
1098 pp_cxx_delete_expression (this, t
);
1104 unary_expression (t
);
1108 case IMPLICIT_CONV_EXPR
:
1109 pp_cxx_cast_expression (this, t
);
1115 pp_cxx_pm_expression (this, t
);
1119 case TRUNC_DIV_EXPR
:
1120 case TRUNC_MOD_EXPR
:
1121 multiplicative_expression (t
);
1125 conditional_expression (t
);
1132 assignment_expression (t
);
1135 case NON_DEPENDENT_EXPR
:
1136 case MUST_NOT_THROW_EXPR
:
1137 expression (TREE_OPERAND (t
, 0));
1140 case EXPR_PACK_EXPANSION
:
1141 expression (PACK_EXPANSION_PATTERN (t
));
1142 pp_cxx_ws_string (this, "...");
1145 case TEMPLATE_ID_EXPR
:
1146 pp_cxx_template_id (this, t
);
1149 case NONTYPE_ARGUMENT_PACK
:
1151 tree args
= ARGUMENT_PACK_ARGS (t
);
1152 int i
, len
= TREE_VEC_LENGTH (args
);
1153 for (i
= 0; i
< len
; ++i
)
1156 pp_cxx_separate_with (this, ',');
1157 expression (TREE_VEC_ELT (args
, i
));
1163 pp_cxx_ws_string (this, "<lambda>");
1167 pp_cxx_trait_expression (this, t
);
1179 pp_cxx_constraint (this, t
);
1183 pp_cxx_left_paren (this);
1184 expression (TREE_OPERAND (t
, 0));
1185 pp_cxx_right_paren (this);
1189 c_pretty_printer::expression (t
);
1197 /* function-specifier:
1203 cxx_pretty_printer::function_specifier (tree t
)
1205 switch (TREE_CODE (t
))
1208 if (DECL_VIRTUAL_P (t
))
1209 pp_cxx_ws_string (this, "virtual");
1210 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1211 pp_cxx_ws_string (this, "explicit");
1213 c_pretty_printer::function_specifier (t
);
1220 /* decl-specifier-seq:
1221 decl-specifier-seq(opt) decl-specifier
1224 storage-class-specifier
1231 cxx_pretty_printer::declaration_specifiers (tree t
)
1233 switch (TREE_CODE (t
))
1239 storage_class_specifier (t
);
1240 declaration_specifiers (TREE_TYPE (t
));
1244 pp_cxx_ws_string (this, "typedef");
1245 declaration_specifiers (TREE_TYPE (t
));
1249 /* Constructors don't have return types. And conversion functions
1250 do not have a type-specifier in their return types. */
1251 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1252 function_specifier (t
);
1253 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1254 declaration_specifiers (TREE_TYPE (TREE_TYPE (t
)));
1257 c_pretty_printer::declaration_specifiers (t
);
1262 /* simple-type-specifier:
1263 ::(opt) nested-name-specifier(opt) type-name
1264 ::(opt) nested-name-specifier(opt) template(opt) template-id
1278 cxx_pretty_printer::simple_type_specifier (tree t
)
1280 switch (TREE_CODE (t
))
1285 pp_cxx_qualified_id (this, t
);
1288 case TEMPLATE_TYPE_PARM
:
1289 case TEMPLATE_TEMPLATE_PARM
:
1290 case TEMPLATE_PARM_INDEX
:
1291 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1292 pp_cxx_unqualified_id (this, t
);
1296 pp_cxx_ws_string (this, "typename");
1297 pp_cxx_nested_name_specifier (this, TYPE_CONTEXT (t
));
1298 pp_cxx_unqualified_id (this, TYPE_NAME (t
));
1302 c_pretty_printer::simple_type_specifier (t
);
1307 /* type-specifier-seq:
1308 type-specifier type-specifier-seq(opt)
1311 simple-type-specifier
1314 elaborated-type-specifier
1318 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1320 switch (TREE_CODE (t
))
1323 case TEMPLATE_TYPE_PARM
:
1324 case TEMPLATE_TEMPLATE_PARM
:
1326 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1327 pp_cxx_cv_qualifier_seq (pp
, t
);
1328 pp
->simple_type_specifier (t
);
1332 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1333 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1334 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1338 pp_cxx_ws_string (pp
, "decltype");
1339 pp_cxx_left_paren (pp
);
1340 pp
->expression (DECLTYPE_TYPE_EXPR (t
));
1341 pp_cxx_right_paren (pp
);
1345 if (TYPE_PTRMEMFUNC_P (t
))
1347 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1348 pp
->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm
)));
1349 pp_cxx_whitespace (pp
);
1350 pp_cxx_ptr_operator (pp
, t
);
1353 /* else fall through */
1356 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1357 pp_c_specifier_qualifier_list (pp
, t
);
1362 * cv-qualifier-seq(opt)
1364 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1367 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1369 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1371 switch (TREE_CODE (t
))
1373 case REFERENCE_TYPE
:
1375 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t
)))
1376 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1377 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1381 pp_cxx_cv_qualifier_seq (pp
, t
);
1388 if (TYPE_PTRMEMFUNC_P (t
))
1390 pp_cxx_left_paren (pp
);
1391 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1396 if (TYPE_PTRMEM_P (t
))
1398 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1399 pp_cxx_left_paren (pp
);
1400 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1402 pp_cxx_cv_qualifier_seq (pp
, t
);
1405 /* else fall through. */
1408 pp_unsupported_tree (pp
, t
);
1414 pp_cxx_implicit_parameter_type (tree mf
)
1416 return class_of_this_parm (TREE_TYPE (mf
));
1420 parameter-declaration:
1421 decl-specifier-seq declarator
1422 decl-specifier-seq declarator = assignment-expression
1423 decl-specifier-seq abstract-declarator(opt)
1424 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1427 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1429 pp
->declaration_specifiers (t
);
1431 pp
->abstract_declarator (t
);
1436 /* parameter-declaration-clause:
1437 parameter-declaration-list(opt) ...(opt)
1438 parameter-declaration-list , ...
1440 parameter-declaration-list:
1441 parameter-declaration
1442 parameter-declaration-list , parameter-declaration */
1445 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1451 // For a requires clause or the explicit printing of a parameter list
1452 // we expect T to be a chain of PARM_DECLs. Otherwise, the list of
1453 // args and types are taken from the function decl T.
1454 if (TREE_CODE (t
) == PARM_DECL
)
1462 bool type_p
= TYPE_P (t
);
1463 args
= type_p
? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1464 types
= type_p
? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1465 abstract
= args
== NULL
|| pp
->flags
& pp_c_flag_abstract
;
1469 /* Skip artificial parameter for nonstatic member functions. */
1470 if (TREE_CODE (t
) == METHOD_TYPE
)
1471 types
= TREE_CHAIN (types
);
1473 pp_cxx_left_paren (pp
);
1474 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1477 pp_cxx_separate_with (pp
, ',');
1479 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1480 if (!abstract
&& pp
->flags
& pp_cxx_flag_default_argument
)
1482 pp_cxx_whitespace (pp
);
1484 pp_cxx_whitespace (pp
);
1485 pp
->assignment_expression (TREE_PURPOSE (types
));
1488 pp_cxx_right_paren (pp
);
1491 /* exception-specification:
1492 throw ( type-id-list(opt) )
1496 type-id-list , type-id */
1499 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1501 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1502 bool need_comma
= false;
1504 if (ex_spec
== NULL
)
1506 if (TREE_PURPOSE (ex_spec
))
1508 pp_cxx_ws_string (pp
, "noexcept");
1509 pp_cxx_whitespace (pp
);
1510 pp_cxx_left_paren (pp
);
1511 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec
))
1512 pp_cxx_ws_string (pp
, "<uninstantiated>");
1514 pp
->expression (TREE_PURPOSE (ex_spec
));
1515 pp_cxx_right_paren (pp
);
1518 pp_cxx_ws_string (pp
, "throw");
1519 pp_cxx_left_paren (pp
);
1520 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1522 tree type
= TREE_VALUE (ex_spec
);
1523 tree argpack
= NULL_TREE
;
1526 if (ARGUMENT_PACK_P (type
))
1528 argpack
= ARGUMENT_PACK_ARGS (type
);
1529 len
= TREE_VEC_LENGTH (argpack
);
1532 for (i
= 0; i
< len
; ++i
)
1535 type
= TREE_VEC_ELT (argpack
, i
);
1538 pp_cxx_separate_with (pp
, ',');
1545 pp_cxx_right_paren (pp
);
1548 /* direct-declarator:
1550 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1551 exception-specification(opt)
1552 direct-declaration [ constant-expression(opt) ]
1556 cxx_pretty_printer::direct_declarator (tree t
)
1558 switch (TREE_CODE (t
))
1566 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t
));
1568 if ((TREE_CODE (t
) == PARM_DECL
&& DECL_PACK_P (t
))
1569 || template_parameter_pack_p (t
))
1570 /* A function parameter pack or non-type template
1572 pp_cxx_ws_string (this, "...");
1574 id_expression (DECL_NAME (t
));
1576 abstract_declarator (TREE_TYPE (t
));
1580 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
1582 pp_cxx_parameter_declaration_clause (this, t
);
1584 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1586 padding
= pp_before
;
1587 pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t
));
1590 pp_cxx_exception_specification (this, TREE_TYPE (t
));
1595 case TEMPLATE_TYPE_PARM
:
1596 case TEMPLATE_PARM_INDEX
:
1597 case TEMPLATE_TEMPLATE_PARM
:
1601 c_pretty_printer::direct_declarator (t
);
1608 ptr-operator declarator */
1611 cxx_pretty_printer::declarator (tree t
)
1613 direct_declarator (t
);
1615 // Print a requires clause.
1617 if (tree ci
= get_constraints (t
))
1618 if (tree reqs
= CI_DECLARATOR_REQS (ci
))
1619 pp_cxx_requires_clause (this, reqs
);
1622 /* ctor-initializer:
1623 : mem-initializer-list
1625 mem-initializer-list:
1627 mem-initializer , mem-initializer-list
1630 mem-initializer-id ( expression-list(opt) )
1633 ::(opt) nested-name-specifier(opt) class-name
1637 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1639 t
= TREE_OPERAND (t
, 0);
1640 pp_cxx_whitespace (pp
);
1642 pp_cxx_whitespace (pp
);
1643 for (; t
; t
= TREE_CHAIN (t
))
1645 tree purpose
= TREE_PURPOSE (t
);
1646 bool is_pack
= PACK_EXPANSION_P (purpose
);
1649 pp
->primary_expression (PACK_EXPANSION_PATTERN (purpose
));
1651 pp
->primary_expression (purpose
);
1652 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1654 pp_cxx_ws_string (pp
, "...");
1656 pp_cxx_separate_with (pp
, ',');
1660 /* function-definition:
1661 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1662 decl-specifier-seq(opt) declarator function-try-block */
1665 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1667 tree saved_scope
= pp
->enclosing_scope
;
1668 pp
->declaration_specifiers (t
);
1670 pp_needs_newline (pp
) = true;
1671 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1672 if (DECL_SAVED_TREE (t
))
1673 pp
->statement (DECL_SAVED_TREE (t
));
1675 pp_cxx_semicolon (pp
);
1676 pp_newline_and_flush (pp
);
1677 pp
->enclosing_scope
= saved_scope
;
1680 /* abstract-declarator:
1681 ptr-operator abstract-declarator(opt)
1682 direct-abstract-declarator */
1685 cxx_pretty_printer::abstract_declarator (tree t
)
1687 if (TYPE_PTRMEM_P (t
))
1688 pp_cxx_right_paren (this);
1689 else if (POINTER_TYPE_P (t
))
1691 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1692 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1693 pp_cxx_right_paren (this);
1696 direct_abstract_declarator (t
);
1699 /* direct-abstract-declarator:
1700 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1701 cv-qualifier-seq(opt) exception-specification(opt)
1702 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1703 ( abstract-declarator ) */
1706 cxx_pretty_printer::direct_abstract_declarator (tree t
)
1708 switch (TREE_CODE (t
))
1710 case REFERENCE_TYPE
:
1711 abstract_declarator (t
);
1715 if (TYPE_PTRMEMFUNC_P (t
))
1716 direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1721 pp_cxx_parameter_declaration_clause (this, t
);
1722 direct_abstract_declarator (TREE_TYPE (t
));
1723 if (TREE_CODE (t
) == METHOD_TYPE
)
1725 padding
= pp_before
;
1726 pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t
));
1728 pp_cxx_exception_specification (this, t
);
1732 case TEMPLATE_TYPE_PARM
:
1733 case TEMPLATE_TEMPLATE_PARM
:
1734 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1735 case UNBOUND_CLASS_TEMPLATE
:
1739 c_pretty_printer::direct_abstract_declarator (t
);
1745 type-specifier-seq abstract-declarator(opt) */
1748 cxx_pretty_printer::type_id (tree t
)
1750 pp_flags saved_flags
= flags
;
1751 flags
|= pp_c_flag_abstract
;
1753 switch (TREE_CODE (t
))
1760 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1761 case UNBOUND_CLASS_TEMPLATE
:
1762 case TEMPLATE_TEMPLATE_PARM
:
1763 case TEMPLATE_TYPE_PARM
:
1764 case TEMPLATE_PARM_INDEX
:
1767 case UNDERLYING_TYPE
:
1769 case TEMPLATE_ID_EXPR
:
1770 pp_cxx_type_specifier_seq (this, t
);
1773 case TYPE_PACK_EXPANSION
:
1774 type_id (PACK_EXPANSION_PATTERN (t
));
1775 pp_cxx_ws_string (this, "...");
1779 c_pretty_printer::type_id (t
);
1783 flags
= saved_flags
;
1786 /* template-argument-list:
1787 template-argument ...(opt)
1788 template-argument-list, template-argument ...(opt)
1791 assignment-expression
1796 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1799 bool need_comma
= false;
1803 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1805 tree arg
= TREE_VEC_ELT (t
, i
);
1806 tree argpack
= NULL_TREE
;
1809 if (ARGUMENT_PACK_P (arg
))
1811 argpack
= ARGUMENT_PACK_ARGS (arg
);
1812 len
= TREE_VEC_LENGTH (argpack
);
1815 for (idx
= 0; idx
< len
; idx
++)
1818 arg
= TREE_VEC_ELT (argpack
, idx
);
1821 pp_cxx_separate_with (pp
, ',');
1825 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1826 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1829 pp
->expression (arg
);
1836 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1838 t
= DECL_EXPR_DECL (t
);
1839 pp_cxx_type_specifier_seq (pp
, t
);
1841 pp
->abstract_declarator (t
);
1849 cxx_pretty_printer::statement (tree t
)
1851 switch (TREE_CODE (t
))
1853 case CTOR_INITIALIZER
:
1854 pp_cxx_ctor_initializer (this, t
);
1858 pp_cxx_ws_string (this, "using");
1859 pp_cxx_ws_string (this, "namespace");
1860 if (DECL_CONTEXT (t
))
1861 pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t
));
1862 pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t
));
1866 pp_cxx_ws_string (this, "using");
1867 pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t
));
1868 pp_cxx_unqualified_id (this, DECL_NAME (t
));
1875 try compound-statement handler-seq */
1877 pp_maybe_newline_and_indent (this, 0);
1878 pp_cxx_ws_string (this, "try");
1879 pp_newline_and_indent (this, 3);
1880 statement (TRY_STMTS (t
));
1881 pp_newline_and_indent (this, -3);
1885 statement (TRY_HANDLERS (t
));
1890 handler handler-seq(opt)
1893 catch ( exception-declaration ) compound-statement
1895 exception-declaration:
1896 type-specifier-seq declarator
1897 type-specifier-seq abstract-declarator
1900 pp_cxx_ws_string (this, "catch");
1901 pp_cxx_left_paren (this);
1902 pp_cxx_exception_declaration (this, HANDLER_PARMS (t
));
1903 pp_cxx_right_paren (this);
1904 pp_indentation (this) += 3;
1905 pp_needs_newline (this) = true;
1906 statement (HANDLER_BODY (t
));
1907 pp_indentation (this) -= 3;
1908 pp_needs_newline (this) = true;
1911 /* selection-statement:
1912 if ( expression ) statement
1913 if ( expression ) statement else statement */
1915 pp_cxx_ws_string (this, "if");
1916 pp_cxx_whitespace (this);
1917 pp_cxx_left_paren (this);
1918 expression (IF_COND (t
));
1919 pp_cxx_right_paren (this);
1920 pp_newline_and_indent (this, 2);
1921 statement (THEN_CLAUSE (t
));
1922 pp_newline_and_indent (this, -2);
1923 if (ELSE_CLAUSE (t
))
1925 tree else_clause
= ELSE_CLAUSE (t
);
1926 pp_cxx_ws_string (this, "else");
1927 if (TREE_CODE (else_clause
) == IF_STMT
)
1928 pp_cxx_whitespace (this);
1930 pp_newline_and_indent (this, 2);
1931 statement (else_clause
);
1932 if (TREE_CODE (else_clause
) != IF_STMT
)
1933 pp_newline_and_indent (this, -2);
1938 pp_cxx_ws_string (this, "switch");
1940 pp_cxx_left_paren (this);
1941 expression (SWITCH_STMT_COND (t
));
1942 pp_cxx_right_paren (this);
1943 pp_indentation (this) += 3;
1944 pp_needs_newline (this) = true;
1945 statement (SWITCH_STMT_BODY (t
));
1946 pp_newline_and_indent (this, -3);
1949 /* iteration-statement:
1950 while ( expression ) statement
1951 do statement while ( expression ) ;
1952 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1953 for ( declaration expression(opt) ; expression(opt) ) statement */
1955 pp_cxx_ws_string (this, "while");
1957 pp_cxx_left_paren (this);
1958 expression (WHILE_COND (t
));
1959 pp_cxx_right_paren (this);
1960 pp_newline_and_indent (this, 3);
1961 statement (WHILE_BODY (t
));
1962 pp_indentation (this) -= 3;
1963 pp_needs_newline (this) = true;
1967 pp_cxx_ws_string (this, "do");
1968 pp_newline_and_indent (this, 3);
1969 statement (DO_BODY (t
));
1970 pp_newline_and_indent (this, -3);
1971 pp_cxx_ws_string (this, "while");
1973 pp_cxx_left_paren (this);
1974 expression (DO_COND (t
));
1975 pp_cxx_right_paren (this);
1976 pp_cxx_semicolon (this);
1977 pp_needs_newline (this) = true;
1981 pp_cxx_ws_string (this, "for");
1983 pp_cxx_left_paren (this);
1984 if (FOR_INIT_STMT (t
))
1985 statement (FOR_INIT_STMT (t
));
1987 pp_cxx_semicolon (this);
1988 pp_needs_newline (this) = false;
1989 pp_cxx_whitespace (this);
1991 expression (FOR_COND (t
));
1992 pp_cxx_semicolon (this);
1993 pp_needs_newline (this) = false;
1994 pp_cxx_whitespace (this);
1996 expression (FOR_EXPR (t
));
1997 pp_cxx_right_paren (this);
1998 pp_newline_and_indent (this, 3);
1999 statement (FOR_BODY (t
));
2000 pp_indentation (this) -= 3;
2001 pp_needs_newline (this) = true;
2004 case RANGE_FOR_STMT
:
2005 pp_cxx_ws_string (this, "for");
2007 pp_cxx_left_paren (this);
2008 statement (RANGE_FOR_DECL (t
));
2010 pp_needs_newline (this) = false;
2013 statement (RANGE_FOR_EXPR (t
));
2014 pp_cxx_right_paren (this);
2015 pp_newline_and_indent (this, 3);
2016 statement (FOR_BODY (t
));
2017 pp_indentation (this) -= 3;
2018 pp_needs_newline (this) = true;
2024 return expression(opt) ; */
2027 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2028 pp_cxx_semicolon (this);
2029 pp_needs_newline (this) = true;
2032 /* expression-statement:
2033 expression(opt) ; */
2035 expression (EXPR_STMT_EXPR (t
));
2036 pp_cxx_semicolon (this);
2037 pp_needs_newline (this) = true;
2041 pp_cxx_ws_string (this, "try");
2042 pp_newline_and_indent (this, 2);
2043 statement (CLEANUP_BODY (t
));
2044 pp_newline_and_indent (this, -2);
2045 pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
2046 pp_newline_and_indent (this, 2);
2047 statement (CLEANUP_EXPR (t
));
2048 pp_newline_and_indent (this, -2);
2056 c_pretty_printer::statement (t
);
2061 /* original-namespace-definition:
2062 namespace identifier { namespace-body }
2064 As an edge case, we also handle unnamed namespace definition here. */
2067 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
2069 pp_cxx_ws_string (pp
, "namespace");
2070 if (DECL_CONTEXT (t
))
2071 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2073 pp_cxx_unqualified_id (pp
, t
);
2074 pp_cxx_whitespace (pp
);
2075 pp_cxx_left_brace (pp
);
2076 /* We do not print the namespace-body. */
2077 pp_cxx_whitespace (pp
);
2078 pp_cxx_right_brace (pp
);
2084 namespace-alias-definition:
2085 namespace identifier = qualified-namespace-specifier ;
2087 qualified-namespace-specifier:
2088 ::(opt) nested-name-specifier(opt) namespace-name */
2091 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
2093 pp_cxx_ws_string (pp
, "namespace");
2094 if (DECL_CONTEXT (t
))
2095 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2096 pp_cxx_unqualified_id (pp
, t
);
2097 pp_cxx_whitespace (pp
);
2099 pp_cxx_whitespace (pp
);
2100 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
2101 pp_cxx_nested_name_specifier (pp
,
2102 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
2103 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
2104 pp_cxx_semicolon (pp
);
2107 /* simple-declaration:
2108 decl-specifier-seq(opt) init-declarator-list(opt) */
2111 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
2113 pp
->declaration_specifiers (t
);
2114 pp_cxx_init_declarator (pp
, t
);
2115 pp_cxx_semicolon (pp
);
2116 pp_needs_newline (pp
) = true;
2120 template-parameter-list:
2122 template-parameter-list , template-parameter */
2125 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
2127 const int n
= TREE_VEC_LENGTH (t
);
2129 for (i
= 0; i
< n
; ++i
)
2132 pp_cxx_separate_with (pp
, ',');
2133 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2137 /* template-parameter:
2139 parameter-declaration
2142 class ...(opt) identifier(opt)
2143 class identifier(opt) = type-id
2144 typename identifier(opt)
2145 typename ...(opt) identifier(opt) = type-id
2146 template < template-parameter-list > class ...(opt) identifier(opt)
2147 template < template-parameter-list > class identifier(opt) = template-name */
2150 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2152 tree parameter
= TREE_VALUE (t
);
2153 switch (TREE_CODE (parameter
))
2156 pp_cxx_ws_string (pp
, "class");
2157 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2158 pp_cxx_ws_string (pp
, "...");
2159 if (DECL_NAME (parameter
))
2160 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2161 /* FIXME: Check if we should print also default argument. */
2165 pp_cxx_parameter_declaration (pp
, parameter
);
2172 pp_unsupported_tree (pp
, t
);
2177 /* Pretty-print a template parameter in the canonical form
2178 "template-parameter-<level>-<position in parameter list>". */
2181 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2183 const enum tree_code code
= TREE_CODE (parm
);
2185 /* Brings type template parameters to the canonical forms. */
2186 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2187 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2188 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2190 pp_cxx_begin_template_argument_list (pp
);
2191 pp
->translate_string ("template-parameter-");
2192 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2194 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2195 pp_cxx_end_template_argument_list (pp
);
2199 template-declaration:
2200 export(opt) template < template-parameter-list > declaration
2204 template-declaration:
2205 export(opt) template < template-parameter-list >
2206 requires-clause(opt) declaration */
2209 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2211 tree tmpl
= most_general_template (t
);
2214 pp_maybe_newline_and_indent (pp
, 0);
2215 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2217 pp_cxx_ws_string (pp
, "template");
2218 pp_cxx_begin_template_argument_list (pp
);
2219 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2220 pp_cxx_end_template_argument_list (pp
);
2221 pp_newline_and_indent (pp
, 3);
2225 if (tree ci
= get_constraints (t
))
2226 if (tree reqs
= CI_TEMPLATE_REQS (ci
))
2228 pp_cxx_requires_clause (pp
, reqs
);
2229 pp_newline_and_indent (pp
, 6);
2232 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2233 pp_cxx_function_definition (pp
, t
);
2235 pp_cxx_simple_declaration (pp
, t
);
2239 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2241 pp_unsupported_tree (pp
, t
);
2245 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2247 pp_unsupported_tree (pp
, t
);
2254 template-declaration
2255 explicit-instantiation
2256 explicit-specialization
2257 linkage-specification
2258 namespace-definition
2263 namespace-alias-definition
2266 static_assert-declaration */
2268 cxx_pretty_printer::declaration (tree t
)
2270 if (TREE_CODE (t
) == STATIC_ASSERT
)
2272 pp_cxx_ws_string (this, "static_assert");
2273 pp_cxx_left_paren (this);
2274 expression (STATIC_ASSERT_CONDITION (t
));
2275 pp_cxx_separate_with (this, ',');
2276 expression (STATIC_ASSERT_MESSAGE (t
));
2277 pp_cxx_right_paren (this);
2279 else if (!DECL_LANG_SPECIFIC (t
))
2280 pp_cxx_simple_declaration (this, t
);
2281 else if (DECL_USE_TEMPLATE (t
))
2282 switch (DECL_USE_TEMPLATE (t
))
2285 pp_cxx_template_declaration (this, t
);
2289 pp_cxx_explicit_specialization (this, t
);
2293 pp_cxx_explicit_instantiation (this, t
);
2299 else switch (TREE_CODE (t
))
2303 pp_cxx_simple_declaration (this, t
);
2307 if (DECL_SAVED_TREE (t
))
2308 pp_cxx_function_definition (this, t
);
2310 pp_cxx_simple_declaration (this, t
);
2313 case NAMESPACE_DECL
:
2314 if (DECL_NAMESPACE_ALIAS (t
))
2315 pp_cxx_namespace_alias_definition (this, t
);
2317 pp_cxx_original_namespace_definition (this, t
);
2321 pp_unsupported_tree (this, t
);
2327 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2329 t
= TREE_OPERAND (t
, 0);
2330 pp_cxx_ws_string (pp
, "typeid");
2331 pp_cxx_left_paren (pp
);
2336 pp_cxx_right_paren (pp
);
2340 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2342 pp_cxx_ws_string (pp
, "va_arg");
2343 pp_cxx_left_paren (pp
);
2344 pp
->assignment_expression (TREE_OPERAND (t
, 0));
2345 pp_cxx_separate_with (pp
, ',');
2346 pp
->type_id (TREE_TYPE (t
));
2347 pp_cxx_right_paren (pp
);
2351 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2353 switch (TREE_CODE (t
))
2356 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2357 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2359 pp
->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2360 pp_cxx_separate_with (pp
, ',');
2365 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2367 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2369 pp
->expression (TREE_OPERAND (t
, 1));
2372 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2374 pp_left_bracket (pp
);
2375 pp
->expression (TREE_OPERAND (t
, 1));
2376 pp_right_bracket (pp
);
2384 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2386 pp_cxx_ws_string (pp
, "offsetof");
2387 pp_cxx_left_paren (pp
);
2388 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2389 pp
->expression (TREE_OPERAND (t
, 0));
2390 pp_cxx_right_paren (pp
);
2394 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2396 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2400 case CPTK_HAS_NOTHROW_ASSIGN
:
2401 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2403 case CPTK_HAS_TRIVIAL_ASSIGN
:
2404 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2406 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2407 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2409 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2410 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2412 case CPTK_HAS_NOTHROW_COPY
:
2413 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2415 case CPTK_HAS_TRIVIAL_COPY
:
2416 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2418 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2419 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2421 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2422 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2424 case CPTK_IS_ABSTRACT
:
2425 pp_cxx_ws_string (pp
, "__is_abstract");
2427 case CPTK_IS_BASE_OF
:
2428 pp_cxx_ws_string (pp
, "__is_base_of");
2431 pp_cxx_ws_string (pp
, "__is_class");
2434 pp_cxx_ws_string (pp
, "__is_empty");
2437 pp_cxx_ws_string (pp
, "__is_enum");
2440 pp_cxx_ws_string (pp
, "__is_final");
2443 pp_cxx_ws_string (pp
, "__is_pod");
2445 case CPTK_IS_POLYMORPHIC
:
2446 pp_cxx_ws_string (pp
, "__is_polymorphic");
2448 case CPTK_IS_SAME_AS
:
2449 pp_cxx_ws_string (pp
, "__is_same_as");
2451 case CPTK_IS_STD_LAYOUT
:
2452 pp_cxx_ws_string (pp
, "__is_std_layout");
2454 case CPTK_IS_TRIVIAL
:
2455 pp_cxx_ws_string (pp
, "__is_trivial");
2457 case CPTK_IS_TRIVIALLY_ASSIGNABLE
:
2458 pp_cxx_ws_string (pp
, "__is_trivially_assignable");
2460 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
:
2461 pp_cxx_ws_string (pp
, "__is_trivially_constructible");
2463 case CPTK_IS_TRIVIALLY_COPYABLE
:
2464 pp_cxx_ws_string (pp
, "__is_trivially_copyable");
2467 pp_cxx_ws_string (pp
, "__is_union");
2469 case CPTK_IS_LITERAL_TYPE
:
2470 pp_cxx_ws_string (pp
, "__is_literal_type");
2477 pp_cxx_left_paren (pp
);
2478 pp
->type_id (TRAIT_EXPR_TYPE1 (t
));
2480 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_SAME_AS
)
2482 pp_cxx_separate_with (pp
, ',');
2483 pp
->type_id (TRAIT_EXPR_TYPE2 (t
));
2486 pp_cxx_right_paren (pp
);
2490 // 'requires' logical-or-expression
2492 pp_cxx_requires_clause (cxx_pretty_printer
*pp
, tree t
)
2496 pp
->padding
= pp_before
;
2497 pp_cxx_ws_string (pp
, "requires");
2504 compound-requirement
2506 nested-requirement */
2508 pp_cxx_requirement (cxx_pretty_printer
*pp
, tree t
)
2510 switch (TREE_CODE (t
))
2513 pp_cxx_simple_requirement (pp
, t
);
2517 pp_cxx_type_requirement (pp
, t
);
2521 pp_cxx_compound_requirement (pp
, t
);
2525 pp_cxx_nested_requirement (pp
, t
);
2533 // requirement-list:
2535 // requirement-list ';' requirement[opt]
2538 pp_cxx_requirement_list (cxx_pretty_printer
*pp
, tree t
)
2540 for (; t
; t
= TREE_CHAIN (t
))
2541 pp_cxx_requirement (pp
, TREE_VALUE (t
));
2544 // requirement-body:
2545 // '{' requirement-list '}'
2547 pp_cxx_requirement_body (cxx_pretty_printer
*pp
, tree t
)
2549 pp_cxx_left_brace (pp
);
2550 pp_cxx_requirement_list (pp
, t
);
2551 pp_cxx_right_brace (pp
);
2554 // requires-expression:
2555 // 'requires' requirement-parameter-list requirement-body
2557 pp_cxx_requires_expr (cxx_pretty_printer
*pp
, tree t
)
2559 pp_string (pp
, "requires");
2560 if (tree parms
= TREE_OPERAND (t
, 0))
2562 pp_cxx_parameter_declaration_clause (pp
, parms
);
2563 pp_cxx_whitespace (pp
);
2565 pp_cxx_requirement_body (pp
, TREE_OPERAND (t
, 1));
2568 /* simple-requirement:
2571 pp_cxx_simple_requirement (cxx_pretty_printer
*pp
, tree t
)
2573 pp
->expression (TREE_OPERAND (t
, 0));
2574 pp_cxx_semicolon (pp
);
2577 /* type-requirement:
2578 typename type-name ';' */
2580 pp_cxx_type_requirement (cxx_pretty_printer
*pp
, tree t
)
2582 pp
->type_id (TREE_OPERAND (t
, 0));
2583 pp_cxx_semicolon (pp
);
2586 /* compound-requirement:
2587 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] */
2589 pp_cxx_compound_requirement (cxx_pretty_printer
*pp
, tree t
)
2591 pp_cxx_left_brace (pp
);
2592 pp
->expression (TREE_OPERAND (t
, 0));
2593 pp_cxx_right_brace (pp
);
2595 if (COMPOUND_REQ_NOEXCEPT_P (t
))
2596 pp_cxx_ws_string (pp
, "noexcept");
2598 if (tree type
= TREE_OPERAND (t
, 1))
2600 pp_cxx_ws_string (pp
, "->");
2605 /* nested requirement:
2606 'requires' constraint-expression */
2608 pp_cxx_nested_requirement (cxx_pretty_printer
*pp
, tree t
)
2610 pp_cxx_ws_string (pp
, "requires");
2611 pp
->expression (TREE_OPERAND (t
, 0));
2612 pp_cxx_semicolon (pp
);
2616 pp_cxx_predicate_constraint (cxx_pretty_printer
*pp
, tree t
)
2618 pp_string (pp
, "predicate");
2620 pp
->expression (TREE_OPERAND (t
, 0));
2621 pp_right_paren (pp
);
2625 pp_cxx_expression_constraint (cxx_pretty_printer
*pp
, tree t
)
2627 pp_string (pp
, "valid_expr");
2629 pp
->expression (TREE_OPERAND (t
, 0));
2630 pp_right_paren (pp
);
2634 pp_cxx_type_constraint (cxx_pretty_printer
*pp
, tree t
)
2636 pp_string (pp
, "valid_type");
2638 pp
->type_id (TREE_OPERAND (t
, 0));
2639 pp_right_paren (pp
);
2643 pp_cxx_implicit_conversion_constraint (cxx_pretty_printer
*pp
, tree t
)
2645 pp_string (pp
, "convertible");
2647 pp
->expression (ICONV_CONSTR_EXPR (t
));
2648 pp_cxx_separate_with (pp
, ',');
2649 pp
->expression (ICONV_CONSTR_TYPE (t
));
2650 pp_right_paren (pp
);
2654 pp_cxx_argument_deduction_constraint (cxx_pretty_printer
*pp
, tree t
)
2656 pp_string (pp
, "deducible");
2658 pp
->expression (DEDUCT_CONSTR_EXPR (t
));
2659 pp_cxx_separate_with (pp
, ',');
2660 pp
->expression (DEDUCT_CONSTR_PATTERN (t
));
2661 pp_right_paren (pp
);
2665 pp_cxx_exception_constraint (cxx_pretty_printer
*pp
, tree t
)
2667 pp_cxx_ws_string (pp
, "noexcept");
2669 pp
->expression (TREE_OPERAND (t
, 0));
2670 pp_right_paren (pp
);
2674 pp_cxx_parameterized_constraint (cxx_pretty_printer
*pp
, tree t
)
2677 pp_string (pp
, "forall");
2678 if (tree parms
= PARM_CONSTR_PARMS (t
))
2681 pp_cxx_parameter_declaration_clause (pp
, parms
);
2682 pp_cxx_whitespace (pp
);
2684 pp_cxx_constraint (pp
, PARM_CONSTR_OPERAND (t
));
2685 pp_right_paren (pp
);
2689 pp_cxx_conjunction (cxx_pretty_printer
*pp
, tree t
)
2691 pp_cxx_constraint (pp
, TREE_OPERAND (t
, 0));
2692 pp_string (pp
, " and ");
2693 pp_cxx_constraint (pp
, TREE_OPERAND (t
, 1));
2697 pp_cxx_disjunction (cxx_pretty_printer
*pp
, tree t
)
2699 pp_cxx_constraint (pp
, TREE_OPERAND (t
, 0));
2700 pp_string (pp
, " or ");
2701 pp_cxx_constraint (pp
, TREE_OPERAND (t
, 1));
2705 pp_cxx_constraint (cxx_pretty_printer
*pp
, tree t
)
2707 if (t
== error_mark_node
)
2708 return pp
->expression (t
);
2710 switch (TREE_CODE (t
))
2713 pp_cxx_predicate_constraint (pp
, t
);
2717 pp_cxx_expression_constraint (pp
, t
);
2721 pp_cxx_type_constraint (pp
, t
);
2725 pp_cxx_implicit_conversion_constraint (pp
, t
);
2729 pp_cxx_argument_deduction_constraint (pp
, t
);
2733 pp_cxx_exception_constraint (pp
, t
);
2737 pp_cxx_parameterized_constraint (pp
, t
);
2741 pp_cxx_conjunction (pp
, t
);
2745 pp_cxx_disjunction (pp
, t
);
2755 typedef c_pretty_print_fn pp_fun
;
2757 /* Initialization of a C++ pretty-printer object. */
2759 cxx_pretty_printer::cxx_pretty_printer ()
2760 : c_pretty_printer (),
2761 enclosing_scope (global_namespace
)
2763 type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2764 parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;