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_template_parameter (cxx_pretty_printer
*, tree
);
37 static void pp_cxx_cast_expression (cxx_pretty_printer
*, tree
);
38 static void pp_cxx_typeid_expression (cxx_pretty_printer
*, tree
);
42 pp_cxx_nonconsecutive_character (cxx_pretty_printer
*pp
, int c
)
44 const char *p
= pp_last_position_in_text (pp
);
46 if (p
!= NULL
&& *p
== c
)
47 pp_cxx_whitespace (pp
);
49 pp
->padding
= pp_none
;
52 #define pp_cxx_expression_list(PP, T) \
53 pp_c_expression_list (PP, T)
54 #define pp_cxx_space_for_pointer_operator(PP, T) \
55 pp_c_space_for_pointer_operator (PP, T)
56 #define pp_cxx_init_declarator(PP, T) \
57 pp_c_init_declarator (PP, T)
58 #define pp_cxx_call_argument_list(PP, T) \
59 pp_c_call_argument_list (PP, T)
62 pp_cxx_colon_colon (cxx_pretty_printer
*pp
)
65 pp
->padding
= pp_none
;
69 pp_cxx_begin_template_argument_list (cxx_pretty_printer
*pp
)
71 pp_cxx_nonconsecutive_character (pp
, '<');
75 pp_cxx_end_template_argument_list (cxx_pretty_printer
*pp
)
77 pp_cxx_nonconsecutive_character (pp
, '>');
81 pp_cxx_separate_with (cxx_pretty_printer
*pp
, int c
)
83 pp_separate_with (pp
, c
);
84 pp
->padding
= pp_none
;
90 is_destructor_name (tree name
)
92 return name
== complete_dtor_identifier
93 || name
== base_dtor_identifier
94 || name
== deleting_dtor_identifier
;
97 /* conversion-function-id:
98 operator conversion-type-id
101 type-specifier-seq conversion-declarator(opt)
103 conversion-declarator:
104 ptr-operator conversion-declarator(opt) */
107 pp_cxx_conversion_function_id (cxx_pretty_printer
*pp
, tree t
)
109 pp_cxx_ws_string (pp
, "operator");
110 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
114 pp_cxx_template_id (cxx_pretty_printer
*pp
, tree t
)
116 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
117 pp_cxx_begin_template_argument_list (pp
);
118 pp_cxx_template_argument_list (pp
, TREE_OPERAND (t
, 1));
119 pp_cxx_end_template_argument_list (pp
);
122 /* Prints the unqualified part of the id-expression T.
127 conversion-function-id
132 pp_cxx_unqualified_id (cxx_pretty_printer
*pp
, tree t
)
134 enum tree_code code
= TREE_CODE (t
);
138 pp
->translate_string ("<return-value>");
155 case IDENTIFIER_NODE
:
157 pp
->translate_string ("<unnamed>");
158 else if (IDENTIFIER_TYPENAME_P (t
))
159 pp_cxx_conversion_function_id (pp
, t
);
162 if (is_destructor_name (t
))
165 /* FIXME: Why is this necessary? */
167 t
= constructor_name (TREE_TYPE (t
));
169 pp_cxx_tree_identifier (pp
, t
);
173 case TEMPLATE_ID_EXPR
:
174 pp_cxx_template_id (pp
, t
);
178 pp_cxx_unqualified_id (pp
, BASELINK_FUNCTIONS (t
));
185 case UNBOUND_CLASS_TEMPLATE
:
186 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
187 if (CLASS_TYPE_P (t
) && CLASSTYPE_USE_TEMPLATE (t
))
189 pp_cxx_begin_template_argument_list (pp
);
190 pp_cxx_template_argument_list (pp
, INNERMOST_TEMPLATE_ARGS
191 (CLASSTYPE_TI_ARGS (t
)));
192 pp_cxx_end_template_argument_list (pp
);
197 pp_cxx_complement (pp
);
198 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
201 case TEMPLATE_TYPE_PARM
:
202 case TEMPLATE_TEMPLATE_PARM
:
203 if (TYPE_IDENTIFIER (t
))
204 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
206 pp_cxx_canonical_template_parameter (pp
, t
);
209 case TEMPLATE_PARM_INDEX
:
210 pp_cxx_unqualified_id (pp
, TEMPLATE_PARM_DECL (t
));
213 case BOUND_TEMPLATE_TEMPLATE_PARM
:
214 pp_cxx_cv_qualifier_seq (pp
, t
);
215 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
216 pp_cxx_begin_template_argument_list (pp
);
217 pp_cxx_template_argument_list (pp
, TYPE_TI_ARGS (t
));
218 pp_cxx_end_template_argument_list (pp
);
222 pp_unsupported_tree (pp
, t
);
227 /* Pretty-print out the token sequence ":: template" in template codes
228 where it is needed to "inline declare" the (following) member as
229 a template. This situation arises when SCOPE of T is dependent
230 on template parameters. */
233 pp_cxx_template_keyword_if_needed (cxx_pretty_printer
*pp
, tree scope
, tree t
)
235 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
236 && TYPE_P (scope
) && dependent_type_p (scope
))
237 pp_cxx_ws_string (pp
, "template");
240 /* nested-name-specifier:
241 class-or-namespace-name :: nested-name-specifier(opt)
242 class-or-namespace-name :: template nested-name-specifier */
245 pp_cxx_nested_name_specifier (cxx_pretty_printer
*pp
, tree t
)
247 if (!SCOPE_FILE_SCOPE_P (t
) && t
!= pp
->enclosing_scope
)
249 tree scope
= get_containing_scope (t
);
250 pp_cxx_nested_name_specifier (pp
, scope
);
251 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
252 pp_cxx_unqualified_id (pp
, t
);
253 pp_cxx_colon_colon (pp
);
258 nested-name-specifier template(opt) unqualified-id */
261 pp_cxx_qualified_id (cxx_pretty_printer
*pp
, tree t
)
263 switch (TREE_CODE (t
))
265 /* A pointer-to-member is always qualified. */
267 pp_cxx_nested_name_specifier (pp
, PTRMEM_CST_CLASS (t
));
268 pp_cxx_unqualified_id (pp
, PTRMEM_CST_MEMBER (t
));
271 /* In Standard C++, functions cannot possibly be used as
272 nested-name-specifiers. However, there are situations where
273 is "makes sense" to output the surrounding function name for the
274 purpose of emphasizing on the scope kind. Just printing the
275 function name might not be sufficient as it may be overloaded; so,
276 we decorate the function with its signature too.
277 FIXME: This is probably the wrong pretty-printing for conversion
278 functions and some function templates. */
282 if (DECL_FUNCTION_MEMBER_P (t
))
283 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
284 pp_cxx_unqualified_id
285 (pp
, DECL_CONSTRUCTOR_P (t
) ? DECL_CONTEXT (t
) : t
);
286 pp_cxx_parameter_declaration_clause (pp
, TREE_TYPE (t
));
291 pp_cxx_nested_name_specifier (pp
, TREE_OPERAND (t
, 0));
292 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 1));
297 tree scope
= get_containing_scope (t
);
298 if (scope
!= pp
->enclosing_scope
)
300 pp_cxx_nested_name_specifier (pp
, scope
);
301 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
303 pp_cxx_unqualified_id (pp
, t
);
311 cxx_pretty_printer::constant (tree t
)
313 switch (TREE_CODE (t
))
317 const bool in_parens
= PAREN_STRING_LITERAL_P (t
);
319 pp_cxx_left_paren (this);
320 c_pretty_printer::constant (t
);
322 pp_cxx_right_paren (this);
327 if (NULLPTR_TYPE_P (TREE_TYPE (t
)))
329 pp_string (this, "nullptr");
332 /* else fall through. */
335 c_pretty_printer::constant (t
);
345 cxx_pretty_printer::id_expression (tree t
)
347 if (TREE_CODE (t
) == OVERLOAD
)
349 if (DECL_P (t
) && DECL_CONTEXT (t
))
350 pp_cxx_qualified_id (this, t
);
352 pp_cxx_unqualified_id (this, t
);
355 /* user-defined literal:
359 pp_cxx_userdef_literal (cxx_pretty_printer
*pp
, tree t
)
361 pp
->constant (USERDEF_LITERAL_VALUE (t
));
362 pp
->id_expression (USERDEF_LITERAL_SUFFIX_ID (t
));
366 /* primary-expression:
370 :: operator-function-id
376 __builtin_va_arg ( assignment-expression , type-id )
377 __builtin_offsetof ( type-id, offsetof-expression )
379 __has_nothrow_assign ( type-id )
380 __has_nothrow_constructor ( type-id )
381 __has_nothrow_copy ( type-id )
382 __has_trivial_assign ( type-id )
383 __has_trivial_constructor ( type-id )
384 __has_trivial_copy ( type-id )
385 __has_trivial_destructor ( type-id )
386 __has_virtual_destructor ( type-id )
387 __is_abstract ( type-id )
388 __is_base_of ( type-id , type-id )
389 __is_class ( type-id )
390 __is_convertible_to ( type-id , 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
))
412 case USERDEF_LITERAL
:
413 pp_cxx_userdef_literal (this, t
);
417 t
= BASELINK_FUNCTIONS (t
);
429 case TEMPLATE_TYPE_PARM
:
430 case TEMPLATE_TEMPLATE_PARM
:
431 case TEMPLATE_PARM_INDEX
:
432 pp_cxx_unqualified_id (this, t
);
436 pp_cxx_left_paren (this);
437 statement (STMT_EXPR_STMT (t
));
438 pp_cxx_right_paren (this);
442 pp_cxx_trait_expression (this, t
);
446 pp_cxx_va_arg_expression (this, t
);
450 pp_cxx_offsetof_expression (this, t
);
454 pp_cxx_requires_expr (this, t
);
458 pp_cxx_expr_requirement (this, t
);
462 pp_cxx_type_requirement (this, t
);
466 pp_cxx_nested_requirement (this, t
);
470 pp_cxx_validexpr_expr (this, t
);
474 pp_cxx_validtype_expr (this, t
);
478 pp_cxx_constexpr_expr (this, t
);
482 c_pretty_printer::primary_expression (t
);
487 /* postfix-expression:
489 postfix-expression [ expression ]
490 postfix-expression ( expression-list(opt) )
491 simple-type-specifier ( expression-list(opt) )
492 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
493 typename ::(opt) nested-name-specifier template(opt)
494 template-id ( expression-list(opt) )
495 postfix-expression . template(opt) ::(opt) id-expression
496 postfix-expression -> template(opt) ::(opt) id-expression
497 postfix-expression . pseudo-destructor-name
498 postfix-expression -> pseudo-destructor-name
499 postfix-expression ++
500 postfix-expression --
501 dynamic_cast < type-id > ( expression )
502 static_cast < type-id > ( expression )
503 reinterpret_cast < type-id > ( expression )
504 const_cast < type-id > ( expression )
505 typeid ( expression )
506 typeid ( type-id ) */
509 cxx_pretty_printer::postfix_expression (tree t
)
511 enum tree_code code
= TREE_CODE (t
);
518 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
520 tree saved_scope
= enclosing_scope
;
521 bool skipfirst
= false;
524 if (TREE_CODE (fun
) == ADDR_EXPR
)
525 fun
= TREE_OPERAND (fun
, 0);
527 /* In templates, where there is no way to tell whether a given
528 call uses an actual member function. So the parser builds
529 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
530 instantiation time. */
531 if (TREE_CODE (fun
) != FUNCTION_DECL
)
533 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
535 tree object
= (code
== AGGR_INIT_EXPR
536 ? (AGGR_INIT_VIA_CTOR_P (t
)
537 ? AGGR_INIT_EXPR_SLOT (t
)
538 : AGGR_INIT_EXPR_ARG (t
, 0))
539 : CALL_EXPR_ARG (t
, 0));
541 while (TREE_CODE (object
) == NOP_EXPR
)
542 object
= TREE_OPERAND (object
, 0);
544 if (TREE_CODE (object
) == ADDR_EXPR
)
545 object
= TREE_OPERAND (object
, 0);
547 if (!TYPE_PTR_P (TREE_TYPE (object
)))
549 postfix_expression (object
);
554 postfix_expression (object
);
558 enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
561 postfix_expression (fun
);
562 enclosing_scope
= saved_scope
;
563 pp_cxx_left_paren (this);
564 if (code
== AGGR_INIT_EXPR
)
566 aggr_init_expr_arg_iterator iter
;
567 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
574 if (more_aggr_init_expr_args_p (&iter
))
575 pp_cxx_separate_with (this, ',');
581 call_expr_arg_iterator iter
;
582 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
589 if (more_call_expr_args_p (&iter
))
590 pp_cxx_separate_with (this, ',');
594 pp_cxx_right_paren (this);
596 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
598 pp_cxx_separate_with (this, ',');
599 postfix_expression (AGGR_INIT_EXPR_SLOT (t
));
612 primary_expression (t
);
615 case DYNAMIC_CAST_EXPR
:
616 case STATIC_CAST_EXPR
:
617 case REINTERPRET_CAST_EXPR
:
618 case CONST_CAST_EXPR
:
619 if (code
== DYNAMIC_CAST_EXPR
)
620 pp_cxx_ws_string (this, "dynamic_cast");
621 else if (code
== STATIC_CAST_EXPR
)
622 pp_cxx_ws_string (this, "static_cast");
623 else if (code
== REINTERPRET_CAST_EXPR
)
624 pp_cxx_ws_string (this, "reinterpret_cast");
626 pp_cxx_ws_string (this, "const_cast");
627 pp_cxx_begin_template_argument_list (this);
628 type_id (TREE_TYPE (t
));
629 pp_cxx_end_template_argument_list (this);
630 pp_left_paren (this);
631 expression (TREE_OPERAND (t
, 0));
632 pp_right_paren (this);
635 case EMPTY_CLASS_EXPR
:
636 type_id (TREE_TYPE (t
));
637 pp_left_paren (this);
638 pp_right_paren (this);
642 pp_cxx_typeid_expression (this, t
);
645 case PSEUDO_DTOR_EXPR
:
646 postfix_expression (TREE_OPERAND (t
, 0));
648 if (TREE_OPERAND (t
, 1))
650 pp_cxx_qualified_id (this, TREE_OPERAND (t
, 1));
651 pp_cxx_colon_colon (this);
653 pp_complement (this);
654 pp_cxx_unqualified_id (this, TREE_OPERAND (t
, 2));
658 postfix_expression (TREE_OPERAND (t
, 0));
663 c_pretty_printer::postfix_expression (t
);
669 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
670 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
676 type-specifier-seq new-declarator(opt)
679 ptr-operator new-declarator(opt)
680 direct-new-declarator
682 direct-new-declarator
684 direct-new-declarator [ constant-expression ]
687 ( expression-list(opt) ) */
690 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
692 enum tree_code code
= TREE_CODE (t
);
693 tree type
= TREE_OPERAND (t
, 1);
694 tree init
= TREE_OPERAND (t
, 2);
699 if (NEW_EXPR_USE_GLOBAL (t
))
700 pp_cxx_colon_colon (pp
);
701 pp_cxx_ws_string (pp
, "new");
702 if (TREE_OPERAND (t
, 0))
704 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
707 if (TREE_CODE (type
) == ARRAY_REF
)
708 type
= build_cplus_array_type
709 (TREE_OPERAND (type
, 0),
710 build_index_type (fold_build2_loc (input_location
,
711 MINUS_EXPR
, integer_type_node
,
712 TREE_OPERAND (type
, 1),
718 if (TREE_CODE (init
) == TREE_LIST
)
719 pp_c_expression_list (pp
, init
);
720 else if (init
== void_zero_node
)
721 ; /* OK, empty initializer list. */
723 pp
->expression (init
);
729 pp_unsupported_tree (pp
, t
);
733 /* delete-expression:
734 ::(opt) delete cast-expression
735 ::(opt) delete [ ] cast-expression */
738 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
740 enum tree_code code
= TREE_CODE (t
);
744 case VEC_DELETE_EXPR
:
745 if (DELETE_EXPR_USE_GLOBAL (t
))
746 pp_cxx_colon_colon (pp
);
747 pp_cxx_ws_string (pp
, "delete");
749 if (code
== VEC_DELETE_EXPR
750 || DELETE_EXPR_USE_VEC (t
))
752 pp_left_bracket (pp
);
753 pp_right_bracket (pp
);
756 pp_c_cast_expression (pp
, TREE_OPERAND (t
, 0));
760 pp_unsupported_tree (pp
, t
);
768 unary-operator cast-expression
769 sizeof unary-expression
771 sizeof ... ( identifier )
775 unary-operator: one of
779 __alignof__ unary-expression
780 __alignof__ ( type-id ) */
783 cxx_pretty_printer::unary_expression (tree t
)
785 enum tree_code code
= TREE_CODE (t
);
790 pp_cxx_new_expression (this, t
);
794 case VEC_DELETE_EXPR
:
795 pp_cxx_delete_expression (this, t
);
799 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
801 pp_cxx_ws_string (this, "sizeof");
802 pp_cxx_ws_string (this, "...");
803 pp_cxx_whitespace (this);
804 pp_cxx_left_paren (this);
805 if (TYPE_P (TREE_OPERAND (t
, 0)))
806 type_id (TREE_OPERAND (t
, 0));
808 unary_expression (TREE_OPERAND (t
, 0));
809 pp_cxx_right_paren (this);
815 pp_cxx_ws_string (this, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
816 pp_cxx_whitespace (this);
817 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
819 pp_cxx_left_paren (this);
820 type_id (TREE_TYPE (TREE_OPERAND (t
, 0)));
821 pp_cxx_right_paren (this);
823 else if (TYPE_P (TREE_OPERAND (t
, 0)))
825 pp_cxx_left_paren (this);
826 type_id (TREE_OPERAND (t
, 0));
827 pp_cxx_right_paren (this);
830 unary_expression (TREE_OPERAND (t
, 0));
834 pp_cxx_ws_string (this, "@encode");
835 pp_cxx_whitespace (this);
836 pp_cxx_left_paren (this);
837 type_id (TREE_OPERAND (t
, 0));
838 pp_cxx_right_paren (this);
842 pp_cxx_ws_string (this, "noexcept");
843 pp_cxx_whitespace (this);
844 pp_cxx_left_paren (this);
845 expression (TREE_OPERAND (t
, 0));
846 pp_cxx_right_paren (this);
849 case UNARY_PLUS_EXPR
:
851 pp_cxx_cast_expression (this, TREE_OPERAND (t
, 0));
855 c_pretty_printer::unary_expression (t
);
862 ( type-id ) cast-expression */
865 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
867 switch (TREE_CODE (t
))
870 case IMPLICIT_CONV_EXPR
:
871 pp
->type_id (TREE_TYPE (t
));
872 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
876 pp_c_cast_expression (pp
, t
);
883 pm-expression .* cast-expression
884 pm-expression ->* cast-expression */
887 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
889 switch (TREE_CODE (t
))
891 /* Handle unfortunate OFFSET_REF overloading here. */
893 if (TYPE_P (TREE_OPERAND (t
, 0)))
895 pp_cxx_qualified_id (pp
, t
);
898 /* Else fall through. */
901 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
902 if (TREE_CODE (t
) == MEMBER_REF
)
907 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
912 pp_cxx_cast_expression (pp
, t
);
917 /* multiplicative-expression:
919 multiplicative-expression * pm-expression
920 multiplicative-expression / pm-expression
921 multiplicative-expression % pm-expression */
924 cxx_pretty_printer::multiplicative_expression (tree e
)
926 enum tree_code code
= TREE_CODE (e
);
932 multiplicative_expression (TREE_OPERAND (e
, 0));
934 if (code
== MULT_EXPR
)
936 else if (code
== TRUNC_DIV_EXPR
)
941 pp_cxx_pm_expression (this, TREE_OPERAND (e
, 1));
945 pp_cxx_pm_expression (this, e
);
950 /* conditional-expression:
951 logical-or-expression
952 logical-or-expression ? expression : assignment-expression */
955 cxx_pretty_printer::conditional_expression (tree e
)
957 if (TREE_CODE (e
) == COND_EXPR
)
959 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
963 expression (TREE_OPERAND (e
, 1));
965 assignment_expression (TREE_OPERAND (e
, 2));
968 pp_c_logical_or_expression (this, e
);
971 /* Pretty-print a compound assignment operator token as indicated by T. */
974 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
978 switch (TREE_CODE (t
))
1001 op
= get_tree_code_name (TREE_CODE (t
));
1005 pp_cxx_ws_string (pp
, op
);
1009 /* assignment-expression:
1010 conditional-expression
1011 logical-or-expression assignment-operator assignment-expression
1015 throw assignment-expression(opt)
1017 assignment-operator: one of
1018 = *= /= %= += -= >>= <<= &= ^= |= */
1021 cxx_pretty_printer::assignment_expression (tree e
)
1023 switch (TREE_CODE (e
))
1027 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1031 assignment_expression (TREE_OPERAND (e
, 1));
1035 pp_cxx_ws_string (this, "throw");
1036 if (TREE_OPERAND (e
, 0))
1037 assignment_expression (TREE_OPERAND (e
, 0));
1041 pp_c_logical_or_expression (this, TREE_OPERAND (e
, 0));
1042 pp_cxx_assignment_operator (this, TREE_OPERAND (e
, 1));
1043 assignment_expression (TREE_OPERAND (e
, 2));
1047 conditional_expression (e
);
1053 cxx_pretty_printer::expression (tree t
)
1055 switch (TREE_CODE (t
))
1064 case USERDEF_LITERAL
:
1065 pp_cxx_userdef_literal (this, t
);
1069 pp_cxx_unqualified_id (this, t
);
1077 pp_cxx_qualified_id (this, t
);
1081 t
= OVL_CURRENT (t
);
1089 case TEMPLATE_TYPE_PARM
:
1090 case TEMPLATE_PARM_INDEX
:
1091 case TEMPLATE_TEMPLATE_PARM
:
1097 primary_expression (t
);
1101 case DYNAMIC_CAST_EXPR
:
1102 case STATIC_CAST_EXPR
:
1103 case REINTERPRET_CAST_EXPR
:
1104 case CONST_CAST_EXPR
:
1108 case EMPTY_CLASS_EXPR
:
1110 case PSEUDO_DTOR_EXPR
:
1111 case AGGR_INIT_EXPR
:
1113 postfix_expression (t
);
1118 pp_cxx_new_expression (this, t
);
1122 case VEC_DELETE_EXPR
:
1123 pp_cxx_delete_expression (this, t
);
1129 unary_expression (t
);
1133 case IMPLICIT_CONV_EXPR
:
1134 pp_cxx_cast_expression (this, t
);
1140 pp_cxx_pm_expression (this, t
);
1144 case TRUNC_DIV_EXPR
:
1145 case TRUNC_MOD_EXPR
:
1146 multiplicative_expression (t
);
1150 conditional_expression (t
);
1157 assignment_expression (t
);
1160 case NON_DEPENDENT_EXPR
:
1161 case MUST_NOT_THROW_EXPR
:
1162 expression (TREE_OPERAND (t
, 0));
1165 case EXPR_PACK_EXPANSION
:
1166 expression (PACK_EXPANSION_PATTERN (t
));
1167 pp_cxx_ws_string (this, "...");
1170 case TEMPLATE_ID_EXPR
:
1171 pp_cxx_template_id (this, t
);
1174 case NONTYPE_ARGUMENT_PACK
:
1176 tree args
= ARGUMENT_PACK_ARGS (t
);
1177 int i
, len
= TREE_VEC_LENGTH (args
);
1178 for (i
= 0; i
< len
; ++i
)
1181 pp_cxx_separate_with (this, ',');
1182 expression (TREE_VEC_ELT (args
, i
));
1188 pp_cxx_ws_string (this, "<lambda>");
1192 pp_cxx_left_paren (this);
1193 expression (TREE_OPERAND (t
, 0));
1194 pp_cxx_right_paren (this);
1198 c_pretty_printer::expression (t
);
1206 /* function-specifier:
1212 cxx_pretty_printer::function_specifier (tree t
)
1214 switch (TREE_CODE (t
))
1217 if (DECL_VIRTUAL_P (t
))
1218 pp_cxx_ws_string (this, "virtual");
1219 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1220 pp_cxx_ws_string (this, "explicit");
1222 c_pretty_printer::function_specifier (t
);
1229 /* decl-specifier-seq:
1230 decl-specifier-seq(opt) decl-specifier
1233 storage-class-specifier
1240 cxx_pretty_printer::declaration_specifiers (tree t
)
1242 switch (TREE_CODE (t
))
1248 storage_class_specifier (t
);
1249 declaration_specifiers (TREE_TYPE (t
));
1253 pp_cxx_ws_string (this, "typedef");
1254 declaration_specifiers (TREE_TYPE (t
));
1258 /* Constructors don't have return types. And conversion functions
1259 do not have a type-specifier in their return types. */
1260 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1261 function_specifier (t
);
1262 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1263 declaration_specifiers (TREE_TYPE (TREE_TYPE (t
)));
1266 c_pretty_printer::declaration_specifiers (t
);
1271 /* simple-type-specifier:
1272 ::(opt) nested-name-specifier(opt) type-name
1273 ::(opt) nested-name-specifier(opt) template(opt) template-id
1287 cxx_pretty_printer::simple_type_specifier (tree t
)
1289 switch (TREE_CODE (t
))
1294 pp_cxx_qualified_id (this, t
);
1297 case TEMPLATE_TYPE_PARM
:
1298 case TEMPLATE_TEMPLATE_PARM
:
1299 case TEMPLATE_PARM_INDEX
:
1300 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1301 pp_cxx_unqualified_id (this, t
);
1305 pp_cxx_ws_string (this, "typename");
1306 pp_cxx_nested_name_specifier (this, TYPE_CONTEXT (t
));
1307 pp_cxx_unqualified_id (this, TYPE_NAME (t
));
1311 c_pretty_printer::simple_type_specifier (t
);
1316 /* type-specifier-seq:
1317 type-specifier type-specifier-seq(opt)
1320 simple-type-specifier
1323 elaborated-type-specifier
1327 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1329 switch (TREE_CODE (t
))
1332 case TEMPLATE_TYPE_PARM
:
1333 case TEMPLATE_TEMPLATE_PARM
:
1335 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1336 pp_cxx_cv_qualifier_seq (pp
, t
);
1337 pp
->simple_type_specifier (t
);
1341 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1342 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1343 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1347 pp_cxx_ws_string (pp
, "decltype");
1348 pp_cxx_left_paren (pp
);
1349 pp
->expression (DECLTYPE_TYPE_EXPR (t
));
1350 pp_cxx_right_paren (pp
);
1354 if (TYPE_PTRMEMFUNC_P (t
))
1356 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1357 pp
->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm
)));
1358 pp_cxx_whitespace (pp
);
1359 pp_cxx_ptr_operator (pp
, t
);
1362 /* else fall through */
1365 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1366 pp_c_specifier_qualifier_list (pp
, t
);
1371 * cv-qualifier-seq(opt)
1373 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1376 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1378 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1380 switch (TREE_CODE (t
))
1382 case REFERENCE_TYPE
:
1384 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t
)))
1385 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1386 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (TREE_TYPE (t
)));
1390 pp_cxx_cv_qualifier_seq (pp
, t
);
1397 if (TYPE_PTRMEMFUNC_P (t
))
1399 pp_cxx_left_paren (pp
);
1400 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1405 if (TYPE_PTRMEM_P (t
))
1407 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1408 pp_cxx_left_paren (pp
);
1409 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1411 pp_cxx_cv_qualifier_seq (pp
, t
);
1414 /* else fall through. */
1417 pp_unsupported_tree (pp
, t
);
1423 pp_cxx_implicit_parameter_type (tree mf
)
1425 return class_of_this_parm (TREE_TYPE (mf
));
1429 parameter-declaration:
1430 decl-specifier-seq declarator
1431 decl-specifier-seq declarator = assignment-expression
1432 decl-specifier-seq abstract-declarator(opt)
1433 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1436 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1438 pp
->declaration_specifiers (t
);
1440 pp
->abstract_declarator (t
);
1445 /* parameter-declaration-clause:
1446 parameter-declaration-list(opt) ...(opt)
1447 parameter-declaration-list , ...
1449 parameter-declaration-list:
1450 parameter-declaration
1451 parameter-declaration-list , parameter-declaration */
1454 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1460 // For a requires clause or the explicit printing of a parameter list
1461 // we expect T to be a TREE_LIST of PARM_DECLs. Otherwise, the list of
1462 // args and types are taken from the function decl T.
1463 if (TREE_CODE (t
) == TREE_LIST
)
1465 args
= TREE_VALUE (t
);
1466 types
= TREE_VALUE (t
);
1471 bool type_p
= TYPE_P (t
);
1472 args
= type_p
? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1473 types
= type_p
? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1474 abstract
= args
== NULL
|| pp
->flags
& pp_c_flag_abstract
;
1478 /* Skip artificial parameter for nonstatic member functions. */
1479 if (TREE_CODE (t
) == METHOD_TYPE
)
1480 types
= TREE_CHAIN (types
);
1482 pp_cxx_left_paren (pp
);
1483 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1486 pp_cxx_separate_with (pp
, ',');
1488 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1489 if (!abstract
&& pp
->flags
& pp_cxx_flag_default_argument
)
1491 pp_cxx_whitespace (pp
);
1493 pp_cxx_whitespace (pp
);
1494 pp
->assignment_expression (TREE_PURPOSE (types
));
1497 pp_cxx_right_paren (pp
);
1500 /* exception-specification:
1501 throw ( type-id-list(opt) )
1505 type-id-list , type-id */
1508 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1510 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1511 bool need_comma
= false;
1513 if (ex_spec
== NULL
)
1515 if (TREE_PURPOSE (ex_spec
))
1517 pp_cxx_ws_string (pp
, "noexcept");
1518 pp_cxx_whitespace (pp
);
1519 pp_cxx_left_paren (pp
);
1520 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec
))
1521 pp_cxx_ws_string (pp
, "<uninstantiated>");
1523 pp
->expression (TREE_PURPOSE (ex_spec
));
1524 pp_cxx_right_paren (pp
);
1527 pp_cxx_ws_string (pp
, "throw");
1528 pp_cxx_left_paren (pp
);
1529 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1531 tree type
= TREE_VALUE (ex_spec
);
1532 tree argpack
= NULL_TREE
;
1535 if (ARGUMENT_PACK_P (type
))
1537 argpack
= ARGUMENT_PACK_ARGS (type
);
1538 len
= TREE_VEC_LENGTH (argpack
);
1541 for (i
= 0; i
< len
; ++i
)
1544 type
= TREE_VEC_ELT (argpack
, i
);
1547 pp_cxx_separate_with (pp
, ',');
1554 pp_cxx_right_paren (pp
);
1557 /* direct-declarator:
1559 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1560 exception-specification(opt)
1561 direct-declaration [ constant-expression(opt) ]
1565 cxx_pretty_printer::direct_declarator (tree t
)
1567 switch (TREE_CODE (t
))
1575 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t
));
1577 if ((TREE_CODE (t
) == PARM_DECL
&& DECL_PACK_P (t
))
1578 || template_parameter_pack_p (t
))
1579 /* A function parameter pack or non-type template
1581 pp_cxx_ws_string (this, "...");
1583 id_expression (DECL_NAME (t
));
1585 abstract_declarator (TREE_TYPE (t
));
1589 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t
)));
1591 pp_cxx_parameter_declaration_clause (this, t
);
1593 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1595 padding
= pp_before
;
1596 pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t
));
1599 pp_cxx_exception_specification (this, TREE_TYPE (t
));
1604 case TEMPLATE_TYPE_PARM
:
1605 case TEMPLATE_PARM_INDEX
:
1606 case TEMPLATE_TEMPLATE_PARM
:
1610 c_pretty_printer::direct_declarator (t
);
1617 ptr-operator declarator */
1620 cxx_pretty_printer::declarator (tree t
)
1622 direct_declarator (t
);
1625 /* ctor-initializer:
1626 : mem-initializer-list
1628 mem-initializer-list:
1630 mem-initializer , mem-initializer-list
1633 mem-initializer-id ( expression-list(opt) )
1636 ::(opt) nested-name-specifier(opt) class-name
1640 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1642 t
= TREE_OPERAND (t
, 0);
1643 pp_cxx_whitespace (pp
);
1645 pp_cxx_whitespace (pp
);
1646 for (; t
; t
= TREE_CHAIN (t
))
1648 tree purpose
= TREE_PURPOSE (t
);
1649 bool is_pack
= PACK_EXPANSION_P (purpose
);
1652 pp
->primary_expression (PACK_EXPANSION_PATTERN (purpose
));
1654 pp
->primary_expression (purpose
);
1655 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1657 pp_cxx_ws_string (pp
, "...");
1659 pp_cxx_separate_with (pp
, ',');
1663 /* function-definition:
1664 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1665 decl-specifier-seq(opt) declarator function-try-block */
1668 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1670 tree saved_scope
= pp
->enclosing_scope
;
1671 pp
->declaration_specifiers (t
);
1673 pp_needs_newline (pp
) = true;
1674 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1675 if (DECL_SAVED_TREE (t
))
1676 pp
->statement (DECL_SAVED_TREE (t
));
1678 pp_cxx_semicolon (pp
);
1679 pp_newline_and_flush (pp
);
1680 pp
->enclosing_scope
= saved_scope
;
1683 /* abstract-declarator:
1684 ptr-operator abstract-declarator(opt)
1685 direct-abstract-declarator */
1688 cxx_pretty_printer::abstract_declarator (tree t
)
1690 if (TYPE_PTRMEM_P (t
))
1691 pp_cxx_right_paren (this);
1692 else if (POINTER_TYPE_P (t
))
1694 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1695 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1696 pp_cxx_right_paren (this);
1699 direct_abstract_declarator (t
);
1702 /* direct-abstract-declarator:
1703 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1704 cv-qualifier-seq(opt) exception-specification(opt)
1705 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1706 ( abstract-declarator ) */
1709 cxx_pretty_printer::direct_abstract_declarator (tree t
)
1711 switch (TREE_CODE (t
))
1713 case REFERENCE_TYPE
:
1714 abstract_declarator (t
);
1718 if (TYPE_PTRMEMFUNC_P (t
))
1719 direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1724 pp_cxx_parameter_declaration_clause (this, t
);
1725 direct_abstract_declarator (TREE_TYPE (t
));
1726 if (TREE_CODE (t
) == METHOD_TYPE
)
1728 padding
= pp_before
;
1729 pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t
));
1731 pp_cxx_exception_specification (this, t
);
1735 case TEMPLATE_TYPE_PARM
:
1736 case TEMPLATE_TEMPLATE_PARM
:
1737 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1738 case UNBOUND_CLASS_TEMPLATE
:
1742 c_pretty_printer::direct_abstract_declarator (t
);
1748 type-specifier-seq abstract-declarator(opt) */
1751 cxx_pretty_printer::type_id (tree t
)
1753 pp_flags saved_flags
= flags
;
1754 flags
|= pp_c_flag_abstract
;
1756 switch (TREE_CODE (t
))
1763 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1764 case UNBOUND_CLASS_TEMPLATE
:
1765 case TEMPLATE_TEMPLATE_PARM
:
1766 case TEMPLATE_TYPE_PARM
:
1767 case TEMPLATE_PARM_INDEX
:
1770 case UNDERLYING_TYPE
:
1772 case TEMPLATE_ID_EXPR
:
1773 pp_cxx_type_specifier_seq (this, t
);
1776 case TYPE_PACK_EXPANSION
:
1777 type_id (PACK_EXPANSION_PATTERN (t
));
1778 pp_cxx_ws_string (this, "...");
1782 c_pretty_printer::type_id (t
);
1786 flags
= saved_flags
;
1789 /* template-argument-list:
1790 template-argument ...(opt)
1791 template-argument-list, template-argument ...(opt)
1794 assignment-expression
1799 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1802 bool need_comma
= false;
1806 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1808 tree arg
= TREE_VEC_ELT (t
, i
);
1809 tree argpack
= NULL_TREE
;
1812 if (ARGUMENT_PACK_P (arg
))
1814 argpack
= ARGUMENT_PACK_ARGS (arg
);
1815 len
= TREE_VEC_LENGTH (argpack
);
1818 for (idx
= 0; idx
< len
; idx
++)
1821 arg
= TREE_VEC_ELT (argpack
, idx
);
1824 pp_cxx_separate_with (pp
, ',');
1828 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1829 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1832 pp
->expression (arg
);
1839 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1841 t
= DECL_EXPR_DECL (t
);
1842 pp_cxx_type_specifier_seq (pp
, t
);
1844 pp
->abstract_declarator (t
);
1852 cxx_pretty_printer::statement (tree t
)
1854 switch (TREE_CODE (t
))
1856 case CTOR_INITIALIZER
:
1857 pp_cxx_ctor_initializer (this, t
);
1861 pp_cxx_ws_string (this, "using");
1862 pp_cxx_ws_string (this, "namespace");
1863 if (DECL_CONTEXT (t
))
1864 pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t
));
1865 pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t
));
1869 pp_cxx_ws_string (this, "using");
1870 pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t
));
1871 pp_cxx_unqualified_id (this, DECL_NAME (t
));
1878 try compound-statement handler-seq */
1880 pp_maybe_newline_and_indent (this, 0);
1881 pp_cxx_ws_string (this, "try");
1882 pp_newline_and_indent (this, 3);
1883 statement (TRY_STMTS (t
));
1884 pp_newline_and_indent (this, -3);
1888 statement (TRY_HANDLERS (t
));
1893 handler handler-seq(opt)
1896 catch ( exception-declaration ) compound-statement
1898 exception-declaration:
1899 type-specifier-seq declarator
1900 type-specifier-seq abstract-declarator
1903 pp_cxx_ws_string (this, "catch");
1904 pp_cxx_left_paren (this);
1905 pp_cxx_exception_declaration (this, HANDLER_PARMS (t
));
1906 pp_cxx_right_paren (this);
1907 pp_indentation (this) += 3;
1908 pp_needs_newline (this) = true;
1909 statement (HANDLER_BODY (t
));
1910 pp_indentation (this) -= 3;
1911 pp_needs_newline (this) = true;
1914 /* selection-statement:
1915 if ( expression ) statement
1916 if ( expression ) statement else statement */
1918 pp_cxx_ws_string (this, "if");
1919 pp_cxx_whitespace (this);
1920 pp_cxx_left_paren (this);
1921 expression (IF_COND (t
));
1922 pp_cxx_right_paren (this);
1923 pp_newline_and_indent (this, 2);
1924 statement (THEN_CLAUSE (t
));
1925 pp_newline_and_indent (this, -2);
1926 if (ELSE_CLAUSE (t
))
1928 tree else_clause
= ELSE_CLAUSE (t
);
1929 pp_cxx_ws_string (this, "else");
1930 if (TREE_CODE (else_clause
) == IF_STMT
)
1931 pp_cxx_whitespace (this);
1933 pp_newline_and_indent (this, 2);
1934 statement (else_clause
);
1935 if (TREE_CODE (else_clause
) != IF_STMT
)
1936 pp_newline_and_indent (this, -2);
1941 pp_cxx_ws_string (this, "switch");
1943 pp_cxx_left_paren (this);
1944 expression (SWITCH_STMT_COND (t
));
1945 pp_cxx_right_paren (this);
1946 pp_indentation (this) += 3;
1947 pp_needs_newline (this) = true;
1948 statement (SWITCH_STMT_BODY (t
));
1949 pp_newline_and_indent (this, -3);
1952 /* iteration-statement:
1953 while ( expression ) statement
1954 do statement while ( expression ) ;
1955 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1956 for ( declaration expression(opt) ; expression(opt) ) statement */
1958 pp_cxx_ws_string (this, "while");
1960 pp_cxx_left_paren (this);
1961 expression (WHILE_COND (t
));
1962 pp_cxx_right_paren (this);
1963 pp_newline_and_indent (this, 3);
1964 statement (WHILE_BODY (t
));
1965 pp_indentation (this) -= 3;
1966 pp_needs_newline (this) = true;
1970 pp_cxx_ws_string (this, "do");
1971 pp_newline_and_indent (this, 3);
1972 statement (DO_BODY (t
));
1973 pp_newline_and_indent (this, -3);
1974 pp_cxx_ws_string (this, "while");
1976 pp_cxx_left_paren (this);
1977 expression (DO_COND (t
));
1978 pp_cxx_right_paren (this);
1979 pp_cxx_semicolon (this);
1980 pp_needs_newline (this) = true;
1984 pp_cxx_ws_string (this, "for");
1986 pp_cxx_left_paren (this);
1987 if (FOR_INIT_STMT (t
))
1988 statement (FOR_INIT_STMT (t
));
1990 pp_cxx_semicolon (this);
1991 pp_needs_newline (this) = false;
1992 pp_cxx_whitespace (this);
1994 expression (FOR_COND (t
));
1995 pp_cxx_semicolon (this);
1996 pp_needs_newline (this) = false;
1997 pp_cxx_whitespace (this);
1999 expression (FOR_EXPR (t
));
2000 pp_cxx_right_paren (this);
2001 pp_newline_and_indent (this, 3);
2002 statement (FOR_BODY (t
));
2003 pp_indentation (this) -= 3;
2004 pp_needs_newline (this) = true;
2007 case RANGE_FOR_STMT
:
2008 pp_cxx_ws_string (this, "for");
2010 pp_cxx_left_paren (this);
2011 statement (RANGE_FOR_DECL (t
));
2013 pp_needs_newline (this) = false;
2016 statement (RANGE_FOR_EXPR (t
));
2017 pp_cxx_right_paren (this);
2018 pp_newline_and_indent (this, 3);
2019 statement (FOR_BODY (t
));
2020 pp_indentation (this) -= 3;
2021 pp_needs_newline (this) = true;
2027 return expression(opt) ; */
2030 pp_string (this, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
2031 pp_cxx_semicolon (this);
2032 pp_needs_newline (this) = true;
2035 /* expression-statement:
2036 expression(opt) ; */
2038 expression (EXPR_STMT_EXPR (t
));
2039 pp_cxx_semicolon (this);
2040 pp_needs_newline (this) = true;
2044 pp_cxx_ws_string (this, "try");
2045 pp_newline_and_indent (this, 2);
2046 statement (CLEANUP_BODY (t
));
2047 pp_newline_and_indent (this, -2);
2048 pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
2049 pp_newline_and_indent (this, 2);
2050 statement (CLEANUP_EXPR (t
));
2051 pp_newline_and_indent (this, -2);
2059 c_pretty_printer::statement (t
);
2064 /* original-namespace-definition:
2065 namespace identifier { namespace-body }
2067 As an edge case, we also handle unnamed namespace definition here. */
2070 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
2072 pp_cxx_ws_string (pp
, "namespace");
2073 if (DECL_CONTEXT (t
))
2074 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2076 pp_cxx_unqualified_id (pp
, t
);
2077 pp_cxx_whitespace (pp
);
2078 pp_cxx_left_brace (pp
);
2079 /* We do not print the namespace-body. */
2080 pp_cxx_whitespace (pp
);
2081 pp_cxx_right_brace (pp
);
2087 namespace-alias-definition:
2088 namespace identifier = qualified-namespace-specifier ;
2090 qualified-namespace-specifier:
2091 ::(opt) nested-name-specifier(opt) namespace-name */
2094 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
2096 pp_cxx_ws_string (pp
, "namespace");
2097 if (DECL_CONTEXT (t
))
2098 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
2099 pp_cxx_unqualified_id (pp
, t
);
2100 pp_cxx_whitespace (pp
);
2102 pp_cxx_whitespace (pp
);
2103 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
2104 pp_cxx_nested_name_specifier (pp
,
2105 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
2106 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
2107 pp_cxx_semicolon (pp
);
2110 /* simple-declaration:
2111 decl-specifier-seq(opt) init-declarator-list(opt) */
2114 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
2116 pp
->declaration_specifiers (t
);
2117 pp_cxx_init_declarator (pp
, t
);
2118 pp_cxx_semicolon (pp
);
2119 pp_needs_newline (pp
) = true;
2123 template-parameter-list:
2125 template-parameter-list , template-parameter */
2128 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
2130 const int n
= TREE_VEC_LENGTH (t
);
2132 for (i
= 0; i
< n
; ++i
)
2135 pp_cxx_separate_with (pp
, ',');
2136 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2140 /* template-parameter:
2142 parameter-declaration
2145 class ...(opt) identifier(opt)
2146 class identifier(opt) = type-id
2147 typename identifier(opt)
2148 typename ...(opt) identifier(opt) = type-id
2149 template < template-parameter-list > class ...(opt) identifier(opt)
2150 template < template-parameter-list > class identifier(opt) = template-name */
2153 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2155 tree parameter
= TREE_VALUE (t
);
2156 switch (TREE_CODE (parameter
))
2159 pp_cxx_ws_string (pp
, "class");
2160 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2161 pp_cxx_ws_string (pp
, "...");
2162 if (DECL_NAME (parameter
))
2163 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2164 /* FIXME: Check if we should print also default argument. */
2168 pp_cxx_parameter_declaration (pp
, parameter
);
2175 pp_unsupported_tree (pp
, t
);
2180 /* Pretty-print a template parameter in the canonical form
2181 "template-parameter-<level>-<position in parameter list>". */
2184 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2186 const enum tree_code code
= TREE_CODE (parm
);
2188 /* Brings type template parameters to the canonical forms. */
2189 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2190 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2191 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2193 pp_cxx_begin_template_argument_list (pp
);
2194 pp
->translate_string ("template-parameter-");
2195 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2197 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2198 pp_cxx_end_template_argument_list (pp
);
2202 template-declaration:
2203 export(opt) template < template-parameter-list > declaration */
2206 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2208 tree tmpl
= most_general_template (t
);
2211 pp_maybe_newline_and_indent (pp
, 0);
2212 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2214 pp_cxx_ws_string (pp
, "template");
2215 pp_cxx_begin_template_argument_list (pp
);
2216 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2217 pp_cxx_end_template_argument_list (pp
);
2218 pp_newline_and_indent (pp
, 3);
2221 if (tree c
= DECL_CONSTRAINTS (t
))
2223 pp_cxx_ws_string (pp
, "requires");
2224 pp
->expression (CI_REQUIREMENTS (c
));
2225 pp_newline_and_indent (pp
, 6);
2228 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2229 pp_cxx_function_definition (pp
, t
);
2231 pp_cxx_simple_declaration (pp
, t
);
2235 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2237 pp_unsupported_tree (pp
, t
);
2241 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2243 pp_unsupported_tree (pp
, t
);
2250 template-declaration
2251 explicit-instantiation
2252 explicit-specialization
2253 linkage-specification
2254 namespace-definition
2259 namespace-alias-definition
2262 static_assert-declaration */
2264 cxx_pretty_printer::declaration (tree t
)
2266 if (TREE_CODE (t
) == STATIC_ASSERT
)
2268 pp_cxx_ws_string (this, "static_assert");
2269 pp_cxx_left_paren (this);
2270 expression (STATIC_ASSERT_CONDITION (t
));
2271 pp_cxx_separate_with (this, ',');
2272 expression (STATIC_ASSERT_MESSAGE (t
));
2273 pp_cxx_right_paren (this);
2275 else if (!DECL_LANG_SPECIFIC (t
))
2276 pp_cxx_simple_declaration (this, t
);
2277 else if (DECL_USE_TEMPLATE (t
))
2278 switch (DECL_USE_TEMPLATE (t
))
2281 pp_cxx_template_declaration (this, t
);
2285 pp_cxx_explicit_specialization (this, t
);
2289 pp_cxx_explicit_instantiation (this, t
);
2295 else switch (TREE_CODE (t
))
2299 pp_cxx_simple_declaration (this, t
);
2303 if (DECL_SAVED_TREE (t
))
2304 pp_cxx_function_definition (this, t
);
2306 pp_cxx_simple_declaration (this, t
);
2309 case NAMESPACE_DECL
:
2310 if (DECL_NAMESPACE_ALIAS (t
))
2311 pp_cxx_namespace_alias_definition (this, t
);
2313 pp_cxx_original_namespace_definition (this, t
);
2317 pp_unsupported_tree (this, t
);
2323 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2325 t
= TREE_OPERAND (t
, 0);
2326 pp_cxx_ws_string (pp
, "typeid");
2327 pp_cxx_left_paren (pp
);
2332 pp_cxx_right_paren (pp
);
2336 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2338 pp_cxx_ws_string (pp
, "va_arg");
2339 pp_cxx_left_paren (pp
);
2340 pp
->assignment_expression (TREE_OPERAND (t
, 0));
2341 pp_cxx_separate_with (pp
, ',');
2342 pp
->type_id (TREE_TYPE (t
));
2343 pp_cxx_right_paren (pp
);
2347 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2349 switch (TREE_CODE (t
))
2352 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2353 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2355 pp
->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2356 pp_cxx_separate_with (pp
, ',');
2361 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2363 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2365 pp
->expression (TREE_OPERAND (t
, 1));
2368 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2370 pp_left_bracket (pp
);
2371 pp
->expression (TREE_OPERAND (t
, 1));
2372 pp_right_bracket (pp
);
2380 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2382 pp_cxx_ws_string (pp
, "offsetof");
2383 pp_cxx_left_paren (pp
);
2384 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2385 pp
->expression (TREE_OPERAND (t
, 0));
2386 pp_cxx_right_paren (pp
);
2390 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2392 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2396 case CPTK_HAS_NOTHROW_ASSIGN
:
2397 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2399 case CPTK_HAS_TRIVIAL_ASSIGN
:
2400 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2402 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2403 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2405 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2406 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2408 case CPTK_HAS_NOTHROW_COPY
:
2409 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2411 case CPTK_HAS_TRIVIAL_COPY
:
2412 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2414 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2415 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2417 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2418 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2420 case CPTK_IS_ABSTRACT
:
2421 pp_cxx_ws_string (pp
, "__is_abstract");
2423 case CPTK_IS_BASE_OF
:
2424 pp_cxx_ws_string (pp
, "__is_base_of");
2427 pp_cxx_ws_string (pp
, "__is_class");
2429 case CPTK_IS_CONVERTIBLE_TO
:
2430 pp_cxx_ws_string (pp
, "__is_convertible_to");
2433 pp_cxx_ws_string (pp
, "__is_empty");
2436 pp_cxx_ws_string (pp
, "__is_enum");
2439 pp_cxx_ws_string (pp
, "__is_final");
2442 pp_cxx_ws_string (pp
, "__is_pod");
2444 case CPTK_IS_POLYMORPHIC
:
2445 pp_cxx_ws_string (pp
, "__is_polymorphic");
2447 case CPTK_IS_SAME_AS
:
2448 pp_cxx_ws_string (pp
, "__is_same_as");
2450 case CPTK_IS_STD_LAYOUT
:
2451 pp_cxx_ws_string (pp
, "__is_std_layout");
2453 case CPTK_IS_TRIVIAL
:
2454 pp_cxx_ws_string (pp
, "__is_trivial");
2457 pp_cxx_ws_string (pp
, "__is_union");
2459 case CPTK_IS_LITERAL_TYPE
:
2460 pp_cxx_ws_string (pp
, "__is_literal_type");
2467 pp_cxx_left_paren (pp
);
2468 pp
->type_id (TRAIT_EXPR_TYPE1 (t
));
2470 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2472 pp_cxx_separate_with (pp
, ',');
2473 pp
->type_id (TRAIT_EXPR_TYPE2 (t
));
2476 pp_cxx_right_paren (pp
);
2479 // requirement-list:
2481 // requirement-list ';' requirement[opt]
2484 // simple-requirement
2485 // compound-requirement
2487 // nested-requirement
2489 pp_cxx_requirement_list (cxx_pretty_printer
*pp
, tree t
)
2493 pp_newline_and_indent (pp
, n
);
2494 pp
->expression (TREE_VALUE (t
));
2498 pp_newline_and_indent (pp
, -3);
2501 // requirement-body:
2502 // '{' requirement-list '}'
2504 pp_cxx_requirement_body (cxx_pretty_printer
*pp
, tree t
)
2506 pp_cxx_left_brace (pp
);
2507 pp_cxx_requirement_list (pp
, t
);
2508 pp_cxx_right_brace (pp
);
2511 // requires-expression:
2512 // 'requires' requirement-parameter-list requirement-body
2514 pp_cxx_requires_expr (cxx_pretty_printer
*pp
, tree t
)
2516 pp_cxx_ws_string (pp
, "requires");
2518 pp_cxx_parameter_declaration_clause (pp
, TREE_OPERAND (t
, 0));
2520 pp_cxx_requirement_body (pp
, TREE_OPERAND (t
, 1));
2523 // constraint-specifier:
2527 pp_cxx_constraint_specifier (cxx_pretty_printer
*pp
, tree t
)
2529 if (TREE_CODE (t
) == NOEXCEPT_EXPR
)
2530 pp_cxx_ws_string (pp
, "noexcept");
2531 else if (TREE_CODE (t
) == CONSTEXPR_EXPR
)
2532 pp_cxx_ws_string (pp
, "constexpr");
2537 // compound-requirement:
2538 // '{' expression '}' trailing-constraint-specifiers
2540 // trailing-constraint-specifiers:
2541 // constraint-specifiers-seq[opt] result-type-requirement[opt]
2543 // result-type-requirement:
2546 pp_cxx_compound_requirement (cxx_pretty_printer
*pp
, tree t
)
2548 // Get the expression requirement.
2549 tree ereq
= TREE_OPERAND (t
, 0);
2551 // Find the tree node containing the result type requirement.
2552 // Note that validtype requirements are implicit.
2553 tree treq
= TREE_CHAIN (ereq
);
2554 if (TREE_CODE (TREE_VALUE (treq
)) == VALIDTYPE_EXPR
)
2555 treq
= TREE_CHAIN (treq
);
2557 // Find tree nodes for any additional constraint specifiers.
2558 tree spec1
= TREE_CHAIN (treq
);
2559 tree spec2
= spec1
? TREE_CHAIN (spec1
) : NULL_TREE
;
2561 // Pretty print the {expr} requirement
2562 tree expr
= TREE_OPERAND (TREE_VALUE (ereq
), 0);
2563 pp_cxx_left_brace (pp
);
2564 pp
->expression (expr
);
2565 pp_cxx_right_brace (pp
);
2567 // Pretty constraint specifiers, if any.
2571 pp_cxx_constraint_specifier (pp
, TREE_VALUE (spec1
));
2573 pp_cxx_constraint_specifier (pp
, TREE_VALUE (spec2
));
2576 // Pretty print the '-> type-id' part of the expression.
2577 // Note that treq will contain a TRAIT_EXPR.
2580 tree type
= TRAIT_EXPR_TYPE2 (TREE_VALUE (treq
));
2588 // simple-requirement:
2591 pp_cxx_simple_requirement (cxx_pretty_printer
*pp
, tree t
)
2593 tree req
= TREE_OPERAND (t
, 0);
2594 pp
->expression (TREE_OPERAND (req
, 0));
2598 pp_cxx_expr_requirement (cxx_pretty_printer
*pp
, tree t
)
2600 tree reqs
= TREE_OPERAND (t
, 0);
2601 if (TREE_CODE (reqs
) == TREE_LIST
)
2602 pp_cxx_compound_requirement (pp
, t
);
2604 pp_cxx_simple_requirement (pp
, t
);
2605 pp_cxx_semicolon (pp
);
2608 // type-requirement:
2611 pp_cxx_type_requirement (cxx_pretty_printer
*pp
, tree t
)
2613 tree req
= TREE_OPERAND (t
, 0);
2614 pp
->type_id (TREE_OPERAND (req
, 0));
2615 pp_cxx_semicolon (pp
);
2618 // nested requirement:
2619 // 'requires' logical-or-expression
2621 pp_cxx_nested_requirement (cxx_pretty_printer
*pp
, tree t
)
2623 pp_cxx_ws_string (pp
, "requires");
2624 pp
->expression (TREE_OPERAND (t
, 0));
2625 pp_cxx_semicolon (pp
);
2629 pp_cxx_validexpr_expr (cxx_pretty_printer
*pp
, tree t
)
2631 pp_cxx_ws_string (pp
, "__is_valid_expr");
2632 pp_cxx_left_paren (pp
);
2633 pp
->expression (TREE_OPERAND (t
, 0));
2634 pp_cxx_right_paren (pp
);
2638 pp_cxx_validtype_expr (cxx_pretty_printer
*pp
, tree t
)
2640 pp_cxx_ws_string (pp
, "__is_valid_expr");
2641 pp_cxx_left_paren (pp
);
2642 pp
->type_id(TREE_OPERAND (t
, 0));
2643 pp_cxx_right_paren (pp
);
2647 pp_cxx_constexpr_expr (cxx_pretty_printer
*pp
, tree t
)
2649 pp_cxx_ws_string (pp
, "__is_valid_expr");
2650 pp_cxx_left_paren (pp
);
2651 pp
->expression (TREE_OPERAND (t
, 0));
2652 pp_cxx_right_paren (pp
);
2656 typedef c_pretty_print_fn pp_fun
;
2658 /* Initialization of a C++ pretty-printer object. */
2660 cxx_pretty_printer::cxx_pretty_printer ()
2661 : c_pretty_printer (),
2662 enclosing_scope (global_namespace
)
2664 pp_set_line_maximum_length (this, 0);
2666 type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2667 parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;