1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008,
3 2009 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "cxx-pretty-print.h"
32 /* Translate if being used for diagnostics, but not for dump files or
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
36 static void pp_cxx_unqualified_id (cxx_pretty_printer
*, tree
);
37 static void pp_cxx_nested_name_specifier (cxx_pretty_printer
*, tree
);
38 static void pp_cxx_qualified_id (cxx_pretty_printer
*, tree
);
39 static void pp_cxx_assignment_expression (cxx_pretty_printer
*, tree
);
40 static void pp_cxx_expression (cxx_pretty_printer
*, tree
);
41 static void pp_cxx_template_argument_list (cxx_pretty_printer
*, tree
);
42 static void pp_cxx_type_specifier_seq (cxx_pretty_printer
*, tree
);
43 static void pp_cxx_ptr_operator (cxx_pretty_printer
*, tree
);
44 static void pp_cxx_type_id (cxx_pretty_printer
*, tree
);
45 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer
*, tree
);
46 static void pp_cxx_declarator (cxx_pretty_printer
*, tree
);
47 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*, tree
);
48 static void pp_cxx_abstract_declarator (cxx_pretty_printer
*, tree
);
49 static void pp_cxx_statement (cxx_pretty_printer
*, tree
);
50 static void pp_cxx_template_parameter (cxx_pretty_printer
*, tree
);
51 static void pp_cxx_cast_expression (cxx_pretty_printer
*, tree
);
52 static void pp_cxx_typeid_expression (cxx_pretty_printer
*, tree
);
56 pp_cxx_nonconsecutive_character (cxx_pretty_printer
*pp
, int c
)
58 const char *p
= pp_last_position_in_text (pp
);
60 if (p
!= NULL
&& *p
== c
)
61 pp_cxx_whitespace (pp
);
63 pp_base (pp
)->padding
= pp_none
;
66 #define pp_cxx_storage_class_specifier(PP, T) \
67 pp_c_storage_class_specifier (pp_c_base (PP), T)
68 #define pp_cxx_expression_list(PP, T) \
69 pp_c_expression_list (pp_c_base (PP), T)
70 #define pp_cxx_space_for_pointer_operator(PP, T) \
71 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
72 #define pp_cxx_init_declarator(PP, T) \
73 pp_c_init_declarator (pp_c_base (PP), T)
74 #define pp_cxx_call_argument_list(PP, T) \
75 pp_c_call_argument_list (pp_c_base (PP), T)
78 pp_cxx_colon_colon (cxx_pretty_printer
*pp
)
81 pp_base (pp
)->padding
= pp_none
;
85 pp_cxx_begin_template_argument_list (cxx_pretty_printer
*pp
)
87 pp_cxx_nonconsecutive_character (pp
, '<');
91 pp_cxx_end_template_argument_list (cxx_pretty_printer
*pp
)
93 pp_cxx_nonconsecutive_character (pp
, '>');
97 pp_cxx_separate_with (cxx_pretty_printer
*pp
, int c
)
99 pp_separate_with (pp
, c
);
100 pp_base (pp
)->padding
= pp_none
;
106 is_destructor_name (tree name
)
108 return name
== complete_dtor_identifier
109 || name
== base_dtor_identifier
110 || name
== deleting_dtor_identifier
;
113 /* conversion-function-id:
114 operator conversion-type-id
117 type-specifier-seq conversion-declarator(opt)
119 conversion-declarator:
120 ptr-operator conversion-declarator(opt) */
123 pp_cxx_conversion_function_id (cxx_pretty_printer
*pp
, tree t
)
125 pp_cxx_ws_string (pp
, "operator");
126 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
130 pp_cxx_template_id (cxx_pretty_printer
*pp
, tree t
)
132 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
133 pp_cxx_begin_template_argument_list (pp
);
134 pp_cxx_template_argument_list (pp
, TREE_OPERAND (t
, 1));
135 pp_cxx_end_template_argument_list (pp
);
138 /* Prints the unqualified part of the id-expression T.
143 conversion-function-id
148 pp_cxx_unqualified_id (cxx_pretty_printer
*pp
, tree t
)
150 enum tree_code code
= TREE_CODE (t
);
154 pp_cxx_ws_string (pp
, M_("<return-value>"));
171 case IDENTIFIER_NODE
:
173 pp_cxx_ws_string (pp
, M_("<unnamed>"));
174 else if (IDENTIFIER_TYPENAME_P (t
))
175 pp_cxx_conversion_function_id (pp
, t
);
178 if (is_destructor_name (t
))
181 /* FIXME: Why is this necessary? */
183 t
= constructor_name (TREE_TYPE (t
));
185 pp_cxx_tree_identifier (pp
, t
);
189 case TEMPLATE_ID_EXPR
:
190 pp_cxx_template_id (pp
, t
);
194 pp_cxx_unqualified_id (pp
, BASELINK_FUNCTIONS (t
));
201 case UNBOUND_CLASS_TEMPLATE
:
202 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
206 pp_cxx_complement (pp
);
207 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 0));
210 case TEMPLATE_TYPE_PARM
:
211 case TEMPLATE_TEMPLATE_PARM
:
212 if (TYPE_IDENTIFIER (t
))
213 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
215 pp_cxx_canonical_template_parameter (pp
, t
);
218 case TEMPLATE_PARM_INDEX
:
219 pp_cxx_unqualified_id (pp
, TEMPLATE_PARM_DECL (t
));
222 case BOUND_TEMPLATE_TEMPLATE_PARM
:
223 pp_cxx_cv_qualifier_seq (pp
, t
);
224 pp_cxx_unqualified_id (pp
, TYPE_IDENTIFIER (t
));
225 pp_cxx_begin_template_argument_list (pp
);
226 pp_cxx_template_argument_list (pp
, TYPE_TI_ARGS (t
));
227 pp_cxx_end_template_argument_list (pp
);
231 pp_unsupported_tree (pp
, t
);
236 /* Pretty-print out the token sequence ":: template" in template codes
237 where it is needed to "inline declare" the (following) member as
238 a template. This situation arises when SCOPE of T is dependent
239 on template parameters. */
242 pp_cxx_template_keyword_if_needed (cxx_pretty_printer
*pp
, tree scope
, tree t
)
244 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
245 && TYPE_P (scope
) && dependent_type_p (scope
))
246 pp_cxx_ws_string (pp
, "template");
249 /* nested-name-specifier:
250 class-or-namespace-name :: nested-name-specifier(opt)
251 class-or-namespace-name :: template nested-name-specifier */
254 pp_cxx_nested_name_specifier (cxx_pretty_printer
*pp
, tree t
)
256 if (t
!= NULL
&& t
!= pp
->enclosing_scope
)
258 tree scope
= TYPE_P (t
) ? TYPE_CONTEXT (t
) : DECL_CONTEXT (t
);
259 pp_cxx_nested_name_specifier (pp
, scope
);
260 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
261 pp_cxx_unqualified_id (pp
, t
);
262 pp_cxx_colon_colon (pp
);
267 nested-name-specifier template(opt) unqualified-id */
270 pp_cxx_qualified_id (cxx_pretty_printer
*pp
, tree t
)
272 switch (TREE_CODE (t
))
274 /* A pointer-to-member is always qualified. */
276 pp_cxx_nested_name_specifier (pp
, PTRMEM_CST_CLASS (t
));
277 pp_cxx_unqualified_id (pp
, PTRMEM_CST_MEMBER (t
));
280 /* In Standard C++, functions cannot possibly be used as
281 nested-name-specifiers. However, there are situations where
282 is "makes sense" to output the surrounding function name for the
283 purpose of emphasizing on the scope kind. Just printing the
284 function name might not be sufficient as it may be overloaded; so,
285 we decorate the function with its signature too.
286 FIXME: This is probably the wrong pretty-printing for conversion
287 functions and some function templates. */
291 if (DECL_FUNCTION_MEMBER_P (t
))
292 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
293 pp_cxx_unqualified_id
294 (pp
, DECL_CONSTRUCTOR_P (t
) ? DECL_CONTEXT (t
) : t
);
295 pp_cxx_parameter_declaration_clause (pp
, TREE_TYPE (t
));
300 pp_cxx_nested_name_specifier (pp
, TREE_OPERAND (t
, 0));
301 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 1));
306 tree scope
= TYPE_P (t
) ? TYPE_CONTEXT (t
) : DECL_CONTEXT (t
);
307 if (scope
!= pp
->enclosing_scope
)
309 pp_cxx_nested_name_specifier (pp
, scope
);
310 pp_cxx_template_keyword_if_needed (pp
, scope
, t
);
312 pp_cxx_unqualified_id (pp
, t
);
320 pp_cxx_constant (cxx_pretty_printer
*pp
, tree t
)
322 switch (TREE_CODE (t
))
326 const bool in_parens
= PAREN_STRING_LITERAL_P (t
);
328 pp_cxx_left_paren (pp
);
329 pp_c_constant (pp_c_base (pp
), t
);
331 pp_cxx_right_paren (pp
);
336 pp_c_constant (pp_c_base (pp
), t
);
346 pp_cxx_id_expression (cxx_pretty_printer
*pp
, tree t
)
348 if (TREE_CODE (t
) == OVERLOAD
)
350 if (DECL_P (t
) && DECL_CONTEXT (t
))
351 pp_cxx_qualified_id (pp
, t
);
353 pp_cxx_unqualified_id (pp
, t
);
356 /* primary-expression:
360 :: operator-function-id
366 __builtin_va_arg ( assignment-expression , type-id )
367 __builtin_offsetof ( type-id, offsetof-expression )
369 __has_nothrow_assign ( type-id )
370 __has_nothrow_constructor ( type-id )
371 __has_nothrow_copy ( type-id )
372 __has_trivial_assign ( type-id )
373 __has_trivial_constructor ( type-id )
374 __has_trivial_copy ( type-id )
375 __has_trivial_destructor ( type-id )
376 __has_virtual_destructor ( type-id )
377 __is_abstract ( type-id )
378 __is_base_of ( type-id , type-id )
379 __is_class ( type-id )
380 __is_convertible_to ( type-id , type-id )
381 __is_empty ( type-id )
382 __is_enum ( type-id )
384 __is_polymorphic ( type-id )
385 __is_union ( type-id ) */
388 pp_cxx_primary_expression (cxx_pretty_printer
*pp
, tree t
)
390 switch (TREE_CODE (t
))
396 pp_cxx_constant (pp
, t
);
400 t
= BASELINK_FUNCTIONS (t
);
408 pp_cxx_id_expression (pp
, t
);
412 case TEMPLATE_TYPE_PARM
:
413 case TEMPLATE_TEMPLATE_PARM
:
414 case TEMPLATE_PARM_INDEX
:
415 pp_cxx_unqualified_id (pp
, t
);
419 pp_cxx_left_paren (pp
);
420 pp_cxx_statement (pp
, STMT_EXPR_STMT (t
));
421 pp_cxx_right_paren (pp
);
425 pp_cxx_trait_expression (pp
, t
);
429 pp_cxx_va_arg_expression (pp
, t
);
433 pp_cxx_offsetof_expression (pp
, t
);
437 pp_c_primary_expression (pp_c_base (pp
), t
);
442 /* postfix-expression:
444 postfix-expression [ expression ]
445 postfix-expression ( expression-list(opt) )
446 simple-type-specifier ( expression-list(opt) )
447 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
448 typename ::(opt) nested-name-specifier template(opt)
449 template-id ( expression-list(opt) )
450 postfix-expression . template(opt) ::(opt) id-expression
451 postfix-expression -> template(opt) ::(opt) id-expression
452 postfix-expression . pseudo-destructor-name
453 postfix-expression -> pseudo-destructor-name
454 postfix-expression ++
455 postfix-expression --
456 dynamic_cast < type-id > ( expression )
457 static_cast < type-id > ( expression )
458 reinterpret_cast < type-id > ( expression )
459 const_cast < type-id > ( expression )
460 typeid ( expression )
461 typeid ( type-id ) */
464 pp_cxx_postfix_expression (cxx_pretty_printer
*pp
, tree t
)
466 enum tree_code code
= TREE_CODE (t
);
473 tree fun
= (code
== AGGR_INIT_EXPR
? AGGR_INIT_EXPR_FN (t
)
475 tree saved_scope
= pp
->enclosing_scope
;
476 bool skipfirst
= false;
479 if (TREE_CODE (fun
) == ADDR_EXPR
)
480 fun
= TREE_OPERAND (fun
, 0);
482 /* In templates, where there is no way to tell whether a given
483 call uses an actual member function. So the parser builds
484 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
485 instantiation time. */
486 if (TREE_CODE (fun
) != FUNCTION_DECL
)
488 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
))
490 tree object
= (code
== AGGR_INIT_EXPR
491 ? (AGGR_INIT_VIA_CTOR_P (t
)
492 ? AGGR_INIT_EXPR_SLOT (t
)
493 : AGGR_INIT_EXPR_ARG (t
, 0))
494 : CALL_EXPR_ARG (t
, 0));
496 while (TREE_CODE (object
) == NOP_EXPR
)
497 object
= TREE_OPERAND (object
, 0);
499 if (TREE_CODE (object
) == ADDR_EXPR
)
500 object
= TREE_OPERAND (object
, 0);
502 if (TREE_CODE (TREE_TYPE (object
)) != POINTER_TYPE
)
504 pp_cxx_postfix_expression (pp
, object
);
509 pp_cxx_postfix_expression (pp
, object
);
513 pp
->enclosing_scope
= strip_pointer_operator (TREE_TYPE (object
));
516 pp_cxx_postfix_expression (pp
, fun
);
517 pp
->enclosing_scope
= saved_scope
;
518 pp_cxx_left_paren (pp
);
519 if (code
== AGGR_INIT_EXPR
)
521 aggr_init_expr_arg_iterator iter
;
522 FOR_EACH_AGGR_INIT_EXPR_ARG (arg
, iter
, t
)
528 pp_cxx_expression (pp
, arg
);
529 if (more_aggr_init_expr_args_p (&iter
))
530 pp_cxx_separate_with (pp
, ',');
536 call_expr_arg_iterator iter
;
537 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
543 pp_cxx_expression (pp
, arg
);
544 if (more_call_expr_args_p (&iter
))
545 pp_cxx_separate_with (pp
, ',');
549 pp_cxx_right_paren (pp
);
551 if (code
== AGGR_INIT_EXPR
&& AGGR_INIT_VIA_CTOR_P (t
))
553 pp_cxx_separate_with (pp
, ',');
554 pp_cxx_postfix_expression (pp
, AGGR_INIT_EXPR_SLOT (t
));
567 pp_cxx_primary_expression (pp
, t
);
570 case DYNAMIC_CAST_EXPR
:
571 case STATIC_CAST_EXPR
:
572 case REINTERPRET_CAST_EXPR
:
573 case CONST_CAST_EXPR
:
574 if (code
== DYNAMIC_CAST_EXPR
)
575 pp_cxx_ws_string (pp
, "dynamic_cast");
576 else if (code
== STATIC_CAST_EXPR
)
577 pp_cxx_ws_string (pp
, "static_cast");
578 else if (code
== REINTERPRET_CAST_EXPR
)
579 pp_cxx_ws_string (pp
, "reinterpret_cast");
581 pp_cxx_ws_string (pp
, "const_cast");
582 pp_cxx_begin_template_argument_list (pp
);
583 pp_cxx_type_id (pp
, TREE_TYPE (t
));
584 pp_cxx_end_template_argument_list (pp
);
586 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
590 case EMPTY_CLASS_EXPR
:
591 pp_cxx_type_id (pp
, TREE_TYPE (t
));
597 pp_cxx_typeid_expression (pp
, t
);
600 case PSEUDO_DTOR_EXPR
:
601 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
603 pp_cxx_qualified_id (pp
, TREE_OPERAND (t
, 1));
604 pp_cxx_colon_colon (pp
);
606 pp_cxx_unqualified_id (pp
, TREE_OPERAND (t
, 2));
610 pp_cxx_postfix_expression (pp
, TREE_OPERAND (t
, 0));
615 pp_c_postfix_expression (pp_c_base (pp
), t
);
621 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
622 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
628 type-specifier-seq new-declarator(opt)
631 ptr-operator new-declarator(opt)
632 direct-new-declarator
634 direct-new-declarator
636 direct-new-declarator [ constant-expression ]
639 ( expression-list(opt) ) */
642 pp_cxx_new_expression (cxx_pretty_printer
*pp
, tree t
)
644 enum tree_code code
= TREE_CODE (t
);
645 tree type
= TREE_OPERAND (t
, 1);
646 tree init
= TREE_OPERAND (t
, 2);
651 if (NEW_EXPR_USE_GLOBAL (t
))
652 pp_cxx_colon_colon (pp
);
653 pp_cxx_ws_string (pp
, "new");
654 if (TREE_OPERAND (t
, 0))
656 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
659 if (TREE_CODE (type
) == ARRAY_REF
)
660 type
= build_cplus_array_type
661 (TREE_OPERAND (type
, 0),
662 build_index_type (fold_build2_loc (input_location
,
663 MINUS_EXPR
, integer_type_node
,
664 TREE_OPERAND (type
, 1),
666 pp_cxx_type_id (pp
, type
);
670 if (TREE_CODE (init
) == TREE_LIST
)
671 pp_c_expression_list (pp_c_base (pp
), init
);
672 else if (init
== void_zero_node
)
673 ; /* OK, empty initializer list. */
675 pp_cxx_expression (pp
, init
);
681 pp_unsupported_tree (pp
, t
);
685 /* delete-expression:
686 ::(opt) delete cast-expression
687 ::(opt) delete [ ] cast-expression */
690 pp_cxx_delete_expression (cxx_pretty_printer
*pp
, tree t
)
692 enum tree_code code
= TREE_CODE (t
);
696 case VEC_DELETE_EXPR
:
697 if (DELETE_EXPR_USE_GLOBAL (t
))
698 pp_cxx_colon_colon (pp
);
699 pp_cxx_ws_string (pp
, "delete");
701 if (code
== VEC_DELETE_EXPR
702 || DELETE_EXPR_USE_VEC (t
))
704 pp_left_bracket (pp
);
705 pp_right_bracket (pp
);
708 pp_c_cast_expression (pp_c_base (pp
), TREE_OPERAND (t
, 0));
712 pp_unsupported_tree (pp
, t
);
720 unary-operator cast-expression
721 sizeof unary-expression
723 sizeof ... ( identifier )
727 unary-operator: one of
731 __alignof__ unary-expression
732 __alignof__ ( type-id ) */
735 pp_cxx_unary_expression (cxx_pretty_printer
*pp
, tree t
)
737 enum tree_code code
= TREE_CODE (t
);
742 pp_cxx_new_expression (pp
, t
);
746 case VEC_DELETE_EXPR
:
747 pp_cxx_delete_expression (pp
, t
);
751 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
753 pp_cxx_ws_string (pp
, "sizeof");
754 pp_cxx_ws_string (pp
, "...");
755 pp_cxx_whitespace (pp
);
756 pp_cxx_left_paren (pp
);
757 if (TYPE_P (TREE_OPERAND (t
, 0)))
758 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
760 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
761 pp_cxx_right_paren (pp
);
767 pp_cxx_ws_string (pp
, code
== SIZEOF_EXPR
? "sizeof" : "__alignof__");
768 pp_cxx_whitespace (pp
);
769 if (TYPE_P (TREE_OPERAND (t
, 0)))
771 pp_cxx_left_paren (pp
);
772 pp_cxx_type_id (pp
, TREE_OPERAND (t
, 0));
773 pp_cxx_right_paren (pp
);
776 pp_unary_expression (pp
, TREE_OPERAND (t
, 0));
779 case UNARY_PLUS_EXPR
:
781 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 0));
785 pp_c_unary_expression (pp_c_base (pp
), t
);
792 ( type-id ) cast-expression */
795 pp_cxx_cast_expression (cxx_pretty_printer
*pp
, tree t
)
797 switch (TREE_CODE (t
))
800 pp_cxx_type_id (pp
, TREE_TYPE (t
));
801 pp_cxx_call_argument_list (pp
, TREE_OPERAND (t
, 0));
805 pp_c_cast_expression (pp_c_base (pp
), t
);
812 pm-expression .* cast-expression
813 pm-expression ->* cast-expression */
816 pp_cxx_pm_expression (cxx_pretty_printer
*pp
, tree t
)
818 switch (TREE_CODE (t
))
820 /* Handle unfortunate OFFSET_REF overloading here. */
822 if (TYPE_P (TREE_OPERAND (t
, 0)))
824 pp_cxx_qualified_id (pp
, t
);
827 /* Else fall through. */
830 pp_cxx_pm_expression (pp
, TREE_OPERAND (t
, 0));
831 if (TREE_CODE (t
) == MEMBER_REF
)
836 pp_cxx_cast_expression (pp
, TREE_OPERAND (t
, 1));
841 pp_cxx_cast_expression (pp
, t
);
846 /* multiplicative-expression:
848 multiplicative-expression * pm-expression
849 multiplicative-expression / pm-expression
850 multiplicative-expression % pm-expression */
853 pp_cxx_multiplicative_expression (cxx_pretty_printer
*pp
, tree e
)
855 enum tree_code code
= TREE_CODE (e
);
861 pp_cxx_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
863 if (code
== MULT_EXPR
)
865 else if (code
== TRUNC_DIV_EXPR
)
870 pp_cxx_pm_expression (pp
, TREE_OPERAND (e
, 1));
874 pp_cxx_pm_expression (pp
, e
);
879 /* conditional-expression:
880 logical-or-expression
881 logical-or-expression ? expression : assignment-expression */
884 pp_cxx_conditional_expression (cxx_pretty_printer
*pp
, tree e
)
886 if (TREE_CODE (e
) == COND_EXPR
)
888 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
892 pp_cxx_expression (pp
, TREE_OPERAND (e
, 1));
894 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
897 pp_c_logical_or_expression (pp_c_base (pp
), e
);
900 /* Pretty-print a compound assignment operator token as indicated by T. */
903 pp_cxx_assignment_operator (cxx_pretty_printer
*pp
, tree t
)
907 switch (TREE_CODE (t
))
930 op
= tree_code_name
[TREE_CODE (t
)];
934 pp_cxx_ws_string (pp
, op
);
938 /* assignment-expression:
939 conditional-expression
940 logical-or-expression assignment-operator assignment-expression
944 throw assignment-expression(opt)
946 assignment-operator: one of
947 = *= /= %= += -= >>= <<= &= ^= |= */
950 pp_cxx_assignment_expression (cxx_pretty_printer
*pp
, tree e
)
952 switch (TREE_CODE (e
))
956 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
960 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 1));
964 pp_cxx_ws_string (pp
, "throw");
965 if (TREE_OPERAND (e
, 0))
966 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 0));
970 pp_c_logical_or_expression (pp_c_base (pp
), TREE_OPERAND (e
, 0));
971 pp_cxx_assignment_operator (pp
, TREE_OPERAND (e
, 1));
972 pp_cxx_assignment_expression (pp
, TREE_OPERAND (e
, 2));
976 pp_cxx_conditional_expression (pp
, e
);
982 pp_cxx_expression (cxx_pretty_printer
*pp
, tree t
)
984 switch (TREE_CODE (t
))
990 pp_cxx_constant (pp
, t
);
994 pp_cxx_unqualified_id (pp
, t
);
1002 pp_cxx_qualified_id (pp
, t
);
1006 t
= OVL_CURRENT (t
);
1014 case TEMPLATE_TYPE_PARM
:
1015 case TEMPLATE_PARM_INDEX
:
1016 case TEMPLATE_TEMPLATE_PARM
:
1018 pp_cxx_primary_expression (pp
, t
);
1022 case DYNAMIC_CAST_EXPR
:
1023 case STATIC_CAST_EXPR
:
1024 case REINTERPRET_CAST_EXPR
:
1025 case CONST_CAST_EXPR
:
1029 case EMPTY_CLASS_EXPR
:
1031 case PSEUDO_DTOR_EXPR
:
1032 case AGGR_INIT_EXPR
:
1034 pp_cxx_postfix_expression (pp
, t
);
1039 pp_cxx_new_expression (pp
, t
);
1043 case VEC_DELETE_EXPR
:
1044 pp_cxx_delete_expression (pp
, t
);
1049 pp_cxx_unary_expression (pp
, t
);
1053 pp_cxx_cast_expression (pp
, t
);
1059 pp_cxx_pm_expression (pp
, t
);
1063 case TRUNC_DIV_EXPR
:
1064 case TRUNC_MOD_EXPR
:
1065 pp_cxx_multiplicative_expression (pp
, t
);
1069 pp_cxx_conditional_expression (pp
, t
);
1076 pp_cxx_assignment_expression (pp
, t
);
1079 case NON_DEPENDENT_EXPR
:
1080 case MUST_NOT_THROW_EXPR
:
1081 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
1084 case EXPR_PACK_EXPANSION
:
1085 pp_cxx_expression (pp
, PACK_EXPANSION_PATTERN (t
));
1086 pp_cxx_ws_string (pp
, "...");
1089 case NONTYPE_ARGUMENT_PACK
:
1091 tree args
= ARGUMENT_PACK_ARGS (t
);
1092 int i
, len
= TREE_VEC_LENGTH (args
);
1093 for (i
= 0; i
< len
; ++i
)
1096 pp_cxx_separate_with (pp
, ',');
1097 pp_cxx_expression (pp
, TREE_VEC_ELT (args
, i
));
1103 pp_c_expression (pp_c_base (pp
), t
);
1111 /* function-specifier:
1117 pp_cxx_function_specifier (cxx_pretty_printer
*pp
, tree t
)
1119 switch (TREE_CODE (t
))
1122 if (DECL_VIRTUAL_P (t
))
1123 pp_cxx_ws_string (pp
, "virtual");
1124 else if (DECL_CONSTRUCTOR_P (t
) && DECL_NONCONVERTING_P (t
))
1125 pp_cxx_ws_string (pp
, "explicit");
1127 pp_c_function_specifier (pp_c_base (pp
), t
);
1134 /* decl-specifier-seq:
1135 decl-specifier-seq(opt) decl-specifier
1138 storage-class-specifier
1145 pp_cxx_decl_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1147 switch (TREE_CODE (t
))
1153 pp_cxx_storage_class_specifier (pp
, t
);
1154 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1158 pp_cxx_ws_string (pp
, "typedef");
1159 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (t
));
1163 if (TYPE_PTRMEMFUNC_P (t
))
1165 tree pfm
= TYPE_PTRMEMFUNC_FN_TYPE (t
);
1166 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (pfm
)));
1167 pp_cxx_whitespace (pp
);
1168 pp_cxx_ptr_operator (pp
, t
);
1173 /* Constructors don't have return types. And conversion functions
1174 do not have a type-specifier in their return types. */
1175 if (DECL_CONSTRUCTOR_P (t
) || DECL_CONV_FN_P (t
))
1176 pp_cxx_function_specifier (pp
, t
);
1177 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1178 pp_cxx_decl_specifier_seq (pp
, TREE_TYPE (TREE_TYPE (t
)));
1181 pp_c_declaration_specifiers (pp_c_base (pp
), t
);
1186 /* simple-type-specifier:
1187 ::(opt) nested-name-specifier(opt) type-name
1188 ::(opt) nested-name-specifier(opt) template(opt) template-id
1202 pp_cxx_simple_type_specifier (cxx_pretty_printer
*pp
, tree t
)
1204 switch (TREE_CODE (t
))
1209 pp_cxx_qualified_id (pp
, t
);
1212 case TEMPLATE_TYPE_PARM
:
1213 case TEMPLATE_TEMPLATE_PARM
:
1214 case TEMPLATE_PARM_INDEX
:
1215 pp_cxx_unqualified_id (pp
, t
);
1219 pp_cxx_ws_string (pp
, "typename");
1220 pp_cxx_nested_name_specifier (pp
, TYPE_CONTEXT (t
));
1221 pp_cxx_unqualified_id (pp
, TYPE_NAME (t
));
1225 pp_c_type_specifier (pp_c_base (pp
), t
);
1230 /* type-specifier-seq:
1231 type-specifier type-specifier-seq(opt)
1234 simple-type-specifier
1237 elaborated-type-specifier
1241 pp_cxx_type_specifier_seq (cxx_pretty_printer
*pp
, tree t
)
1243 switch (TREE_CODE (t
))
1246 case TEMPLATE_TYPE_PARM
:
1247 case TEMPLATE_TEMPLATE_PARM
:
1249 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1250 pp_cxx_cv_qualifier_seq (pp
, t
);
1251 pp_cxx_simple_type_specifier (pp
, t
);
1255 pp_cxx_type_specifier_seq (pp
, TREE_TYPE (t
));
1256 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1257 pp_cxx_nested_name_specifier (pp
, TYPE_METHOD_BASETYPE (t
));
1261 pp_cxx_ws_string (pp
, "decltype");
1262 pp_cxx_left_paren (pp
);
1263 pp_cxx_expression (pp
, DECLTYPE_TYPE_EXPR (t
));
1264 pp_cxx_right_paren (pp
);
1268 if (!(TREE_CODE (t
) == FUNCTION_DECL
&& DECL_CONSTRUCTOR_P (t
)))
1269 pp_c_specifier_qualifier_list (pp_c_base (pp
), t
);
1274 * cv-qualifier-seq(opt)
1276 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1279 pp_cxx_ptr_operator (cxx_pretty_printer
*pp
, tree t
)
1281 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
1283 switch (TREE_CODE (t
))
1285 case REFERENCE_TYPE
:
1287 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
1288 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t
)))
1289 pp_cxx_ptr_operator (pp
, TREE_TYPE (t
));
1290 if (TREE_CODE (t
) == POINTER_TYPE
)
1293 pp_cxx_cv_qualifier_seq (pp
, t
);
1300 if (TYPE_PTRMEMFUNC_P (t
))
1302 pp_cxx_left_paren (pp
);
1303 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEMFUNC_OBJECT_TYPE (t
));
1308 if (TYPE_PTR_TO_MEMBER_P (t
))
1310 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
1311 pp_cxx_left_paren (pp
);
1312 pp_cxx_nested_name_specifier (pp
, TYPE_PTRMEM_CLASS_TYPE (t
));
1314 pp_cxx_cv_qualifier_seq (pp
, t
);
1317 /* else fall through. */
1320 pp_unsupported_tree (pp
, t
);
1326 pp_cxx_implicit_parameter_type (tree mf
)
1328 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf
))));
1332 parameter-declaration:
1333 decl-specifier-seq declarator
1334 decl-specifier-seq declarator = assignment-expression
1335 decl-specifier-seq abstract-declarator(opt)
1336 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1339 pp_cxx_parameter_declaration (cxx_pretty_printer
*pp
, tree t
)
1341 pp_cxx_decl_specifier_seq (pp
, t
);
1343 pp_cxx_abstract_declarator (pp
, t
);
1345 pp_cxx_declarator (pp
, t
);
1348 /* parameter-declaration-clause:
1349 parameter-declaration-list(opt) ...(opt)
1350 parameter-declaration-list , ...
1352 parameter-declaration-list:
1353 parameter-declaration
1354 parameter-declaration-list , parameter-declaration */
1357 pp_cxx_parameter_declaration_clause (cxx_pretty_printer
*pp
, tree t
)
1359 tree args
= TYPE_P (t
) ? NULL
: FUNCTION_FIRST_USER_PARM (t
);
1361 TYPE_P (t
) ? TYPE_ARG_TYPES (t
) : FUNCTION_FIRST_USER_PARMTYPE (t
);
1362 const bool abstract
= args
== NULL
1363 || pp_c_base (pp
)->flags
& pp_c_flag_abstract
;
1366 /* Skip artificial parameter for nonstatic member functions. */
1367 if (TREE_CODE (t
) == METHOD_TYPE
)
1368 types
= TREE_CHAIN (types
);
1370 pp_cxx_left_paren (pp
);
1371 for (; args
; args
= TREE_CHAIN (args
), types
= TREE_CHAIN (types
))
1374 pp_cxx_separate_with (pp
, ',');
1376 pp_cxx_parameter_declaration (pp
, abstract
? TREE_VALUE (types
) : args
);
1377 if (!abstract
&& pp_c_base (pp
)->flags
& pp_cxx_flag_default_argument
)
1379 pp_cxx_whitespace (pp
);
1381 pp_cxx_whitespace (pp
);
1382 pp_cxx_assignment_expression (pp
, TREE_PURPOSE (types
));
1385 pp_cxx_right_paren (pp
);
1388 /* exception-specification:
1389 throw ( type-id-list(opt) )
1393 type-id-list , type-id */
1396 pp_cxx_exception_specification (cxx_pretty_printer
*pp
, tree t
)
1398 tree ex_spec
= TYPE_RAISES_EXCEPTIONS (t
);
1399 bool need_comma
= false;
1401 if (!TYPE_NOTHROW_P (t
) && ex_spec
== NULL
)
1403 pp_cxx_ws_string (pp
, "throw");
1404 pp_cxx_left_paren (pp
);
1405 for (; ex_spec
&& TREE_VALUE (ex_spec
); ex_spec
= TREE_CHAIN (ex_spec
))
1407 tree type
= TREE_VALUE (ex_spec
);
1408 tree argpack
= NULL_TREE
;
1411 if (ARGUMENT_PACK_P (type
))
1413 argpack
= ARGUMENT_PACK_ARGS (type
);
1414 len
= TREE_VEC_LENGTH (argpack
);
1417 for (i
= 0; i
< len
; ++i
)
1420 type
= TREE_VEC_ELT (argpack
, i
);
1423 pp_cxx_separate_with (pp
, ',');
1427 pp_cxx_type_id (pp
, type
);
1430 pp_cxx_right_paren (pp
);
1433 /* direct-declarator:
1435 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1436 exception-specification(opt)
1437 direct-declaration [ constant-expression(opt) ]
1441 pp_cxx_direct_declarator (cxx_pretty_printer
*pp
, tree t
)
1443 switch (TREE_CODE (t
))
1451 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (t
));
1453 if ((TREE_CODE (t
) == PARM_DECL
&& FUNCTION_PARAMETER_PACK_P (t
))
1454 || template_parameter_pack_p (t
))
1455 /* A function parameter pack or non-type template
1457 pp_cxx_ws_string (pp
, "...");
1459 pp_cxx_id_expression (pp
, DECL_NAME (t
));
1461 pp_cxx_abstract_declarator (pp
, TREE_TYPE (t
));
1465 pp_cxx_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
1466 pp_cxx_id_expression (pp
, t
);
1467 pp_cxx_parameter_declaration_clause (pp
, t
);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t
))
1471 pp_base (pp
)->padding
= pp_before
;
1472 pp_cxx_cv_qualifier_seq (pp
, pp_cxx_implicit_parameter_type (t
));
1475 pp_cxx_exception_specification (pp
, TREE_TYPE (t
));
1480 case TEMPLATE_TYPE_PARM
:
1481 case TEMPLATE_PARM_INDEX
:
1482 case TEMPLATE_TEMPLATE_PARM
:
1486 pp_c_direct_declarator (pp_c_base (pp
), t
);
1493 ptr-operator declarator */
1496 pp_cxx_declarator (cxx_pretty_printer
*pp
, tree t
)
1498 pp_cxx_direct_declarator (pp
, t
);
1501 /* ctor-initializer:
1502 : mem-initializer-list
1504 mem-initializer-list:
1506 mem-initializer , mem-initializer-list
1509 mem-initializer-id ( expression-list(opt) )
1512 ::(opt) nested-name-specifier(opt) class-name
1516 pp_cxx_ctor_initializer (cxx_pretty_printer
*pp
, tree t
)
1518 t
= TREE_OPERAND (t
, 0);
1519 pp_cxx_whitespace (pp
);
1521 pp_cxx_whitespace (pp
);
1522 for (; t
; t
= TREE_CHAIN (t
))
1524 tree purpose
= TREE_PURPOSE (t
);
1525 bool is_pack
= PACK_EXPANSION_P (purpose
);
1528 pp_cxx_primary_expression (pp
, PACK_EXPANSION_PATTERN (purpose
));
1530 pp_cxx_primary_expression (pp
, purpose
);
1531 pp_cxx_call_argument_list (pp
, TREE_VALUE (t
));
1533 pp_cxx_ws_string (pp
, "...");
1535 pp_cxx_separate_with (pp
, ',');
1539 /* function-definition:
1540 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1541 decl-specifier-seq(opt) declarator function-try-block */
1544 pp_cxx_function_definition (cxx_pretty_printer
*pp
, tree t
)
1546 tree saved_scope
= pp
->enclosing_scope
;
1547 pp_cxx_decl_specifier_seq (pp
, t
);
1548 pp_cxx_declarator (pp
, t
);
1549 pp_needs_newline (pp
) = true;
1550 pp
->enclosing_scope
= DECL_CONTEXT (t
);
1551 if (DECL_SAVED_TREE (t
))
1552 pp_cxx_statement (pp
, DECL_SAVED_TREE (t
));
1555 pp_cxx_semicolon (pp
);
1556 pp_needs_newline (pp
) = true;
1559 pp
->enclosing_scope
= saved_scope
;
1562 /* abstract-declarator:
1563 ptr-operator abstract-declarator(opt)
1564 direct-abstract-declarator */
1567 pp_cxx_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1569 if (TYPE_PTRMEM_P (t
) || TYPE_PTRMEMFUNC_P (t
))
1570 pp_cxx_right_paren (pp
);
1571 else if (POINTER_TYPE_P (t
))
1573 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
1574 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
1575 pp_cxx_right_paren (pp
);
1578 pp_cxx_direct_abstract_declarator (pp
, t
);
1581 /* direct-abstract-declarator:
1582 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1583 cv-qualifier-seq(opt) exception-specification(opt)
1584 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1585 ( abstract-declarator ) */
1588 pp_cxx_direct_abstract_declarator (cxx_pretty_printer
*pp
, tree t
)
1590 switch (TREE_CODE (t
))
1592 case REFERENCE_TYPE
:
1593 pp_cxx_abstract_declarator (pp
, t
);
1597 if (TYPE_PTRMEMFUNC_P (t
))
1598 pp_cxx_direct_abstract_declarator (pp
, TYPE_PTRMEMFUNC_FN_TYPE (t
));
1603 pp_cxx_parameter_declaration_clause (pp
, t
);
1604 pp_cxx_direct_abstract_declarator (pp
, TREE_TYPE (t
));
1605 if (TREE_CODE (t
) == METHOD_TYPE
)
1607 pp_base (pp
)->padding
= pp_before
;
1608 pp_cxx_cv_qualifier_seq
1609 (pp
, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))));
1611 pp_cxx_exception_specification (pp
, t
);
1615 case TEMPLATE_TYPE_PARM
:
1616 case TEMPLATE_TEMPLATE_PARM
:
1617 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1618 case UNBOUND_CLASS_TEMPLATE
:
1622 pp_c_direct_abstract_declarator (pp_c_base (pp
), t
);
1628 type-specifier-seq abstract-declarator(opt) */
1631 pp_cxx_type_id (cxx_pretty_printer
*pp
, tree t
)
1633 pp_flags saved_flags
= pp_c_base (pp
)->flags
;
1634 pp_c_base (pp
)->flags
|= pp_c_flag_abstract
;
1636 switch (TREE_CODE (t
))
1643 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1644 case UNBOUND_CLASS_TEMPLATE
:
1645 case TEMPLATE_TEMPLATE_PARM
:
1646 case TEMPLATE_TYPE_PARM
:
1647 case TEMPLATE_PARM_INDEX
:
1651 case TEMPLATE_ID_EXPR
:
1652 pp_cxx_type_specifier_seq (pp
, t
);
1655 case TYPE_PACK_EXPANSION
:
1656 pp_cxx_type_id (pp
, PACK_EXPANSION_PATTERN (t
));
1657 pp_cxx_ws_string (pp
, "...");
1661 pp_c_type_id (pp_c_base (pp
), t
);
1665 pp_c_base (pp
)->flags
= saved_flags
;
1668 /* template-argument-list:
1669 template-argument ...(opt)
1670 template-argument-list, template-argument ...(opt)
1673 assignment-expression
1678 pp_cxx_template_argument_list (cxx_pretty_printer
*pp
, tree t
)
1681 bool need_comma
= false;
1685 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
1687 tree arg
= TREE_VEC_ELT (t
, i
);
1688 tree argpack
= NULL_TREE
;
1691 if (ARGUMENT_PACK_P (arg
))
1693 argpack
= ARGUMENT_PACK_ARGS (arg
);
1694 len
= TREE_VEC_LENGTH (argpack
);
1697 for (idx
= 0; idx
< len
; idx
++)
1700 arg
= TREE_VEC_ELT (argpack
, idx
);
1703 pp_cxx_separate_with (pp
, ',');
1707 if (TYPE_P (arg
) || (TREE_CODE (arg
) == TEMPLATE_DECL
1708 && TYPE_P (DECL_TEMPLATE_RESULT (arg
))))
1709 pp_cxx_type_id (pp
, arg
);
1711 pp_cxx_expression (pp
, arg
);
1718 pp_cxx_exception_declaration (cxx_pretty_printer
*pp
, tree t
)
1720 t
= DECL_EXPR_DECL (t
);
1721 pp_cxx_type_specifier_seq (pp
, t
);
1723 pp_cxx_abstract_declarator (pp
, t
);
1725 pp_cxx_declarator (pp
, t
);
1731 pp_cxx_statement (cxx_pretty_printer
*pp
, tree t
)
1733 switch (TREE_CODE (t
))
1735 case CTOR_INITIALIZER
:
1736 pp_cxx_ctor_initializer (pp
, t
);
1740 pp_cxx_ws_string (pp
, "using");
1741 pp_cxx_ws_string (pp
, "namespace");
1742 if (DECL_CONTEXT (t
))
1743 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1744 pp_cxx_qualified_id (pp
, USING_STMT_NAMESPACE (t
));
1748 pp_cxx_ws_string (pp
, "using");
1749 pp_cxx_nested_name_specifier (pp
, USING_DECL_SCOPE (t
));
1750 pp_cxx_unqualified_id (pp
, DECL_NAME (t
));
1757 try compound-statement handler-seq */
1759 pp_maybe_newline_and_indent (pp
, 0);
1760 pp_cxx_ws_string (pp
, "try");
1761 pp_newline_and_indent (pp
, 3);
1762 pp_cxx_statement (pp
, TRY_STMTS (t
));
1763 pp_newline_and_indent (pp
, -3);
1767 pp_cxx_statement (pp
, TRY_HANDLERS (t
));
1772 handler handler-seq(opt)
1775 catch ( exception-declaration ) compound-statement
1777 exception-declaration:
1778 type-specifier-seq declarator
1779 type-specifier-seq abstract-declarator
1782 pp_cxx_ws_string (pp
, "catch");
1783 pp_cxx_left_paren (pp
);
1784 pp_cxx_exception_declaration (pp
, HANDLER_PARMS (t
));
1785 pp_cxx_right_paren (pp
);
1786 pp_indentation (pp
) += 3;
1787 pp_needs_newline (pp
) = true;
1788 pp_cxx_statement (pp
, HANDLER_BODY (t
));
1789 pp_indentation (pp
) -= 3;
1790 pp_needs_newline (pp
) = true;
1793 /* selection-statement:
1794 if ( expression ) statement
1795 if ( expression ) statement else statement */
1797 pp_cxx_ws_string (pp
, "if");
1798 pp_cxx_whitespace (pp
);
1799 pp_cxx_left_paren (pp
);
1800 pp_cxx_expression (pp
, IF_COND (t
));
1801 pp_cxx_right_paren (pp
);
1802 pp_newline_and_indent (pp
, 2);
1803 pp_cxx_statement (pp
, THEN_CLAUSE (t
));
1804 pp_newline_and_indent (pp
, -2);
1805 if (ELSE_CLAUSE (t
))
1807 tree else_clause
= ELSE_CLAUSE (t
);
1808 pp_cxx_ws_string (pp
, "else");
1809 if (TREE_CODE (else_clause
) == IF_STMT
)
1810 pp_cxx_whitespace (pp
);
1812 pp_newline_and_indent (pp
, 2);
1813 pp_cxx_statement (pp
, else_clause
);
1814 if (TREE_CODE (else_clause
) != IF_STMT
)
1815 pp_newline_and_indent (pp
, -2);
1820 pp_cxx_ws_string (pp
, "switch");
1822 pp_cxx_left_paren (pp
);
1823 pp_cxx_expression (pp
, SWITCH_STMT_COND (t
));
1824 pp_cxx_right_paren (pp
);
1825 pp_indentation (pp
) += 3;
1826 pp_needs_newline (pp
) = true;
1827 pp_cxx_statement (pp
, SWITCH_STMT_BODY (t
));
1828 pp_newline_and_indent (pp
, -3);
1831 /* iteration-statement:
1832 while ( expression ) statement
1833 do statement while ( expression ) ;
1834 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1835 for ( declaration expression(opt) ; expression(opt) ) statement */
1837 pp_cxx_ws_string (pp
, "while");
1839 pp_cxx_left_paren (pp
);
1840 pp_cxx_expression (pp
, WHILE_COND (t
));
1841 pp_cxx_right_paren (pp
);
1842 pp_newline_and_indent (pp
, 3);
1843 pp_cxx_statement (pp
, WHILE_BODY (t
));
1844 pp_indentation (pp
) -= 3;
1845 pp_needs_newline (pp
) = true;
1849 pp_cxx_ws_string (pp
, "do");
1850 pp_newline_and_indent (pp
, 3);
1851 pp_cxx_statement (pp
, DO_BODY (t
));
1852 pp_newline_and_indent (pp
, -3);
1853 pp_cxx_ws_string (pp
, "while");
1855 pp_cxx_left_paren (pp
);
1856 pp_cxx_expression (pp
, DO_COND (t
));
1857 pp_cxx_right_paren (pp
);
1858 pp_cxx_semicolon (pp
);
1859 pp_needs_newline (pp
) = true;
1863 pp_cxx_ws_string (pp
, "for");
1865 pp_cxx_left_paren (pp
);
1866 if (FOR_INIT_STMT (t
))
1867 pp_cxx_statement (pp
, FOR_INIT_STMT (t
));
1869 pp_cxx_semicolon (pp
);
1870 pp_needs_newline (pp
) = false;
1871 pp_cxx_whitespace (pp
);
1873 pp_cxx_expression (pp
, FOR_COND (t
));
1874 pp_cxx_semicolon (pp
);
1875 pp_needs_newline (pp
) = false;
1876 pp_cxx_whitespace (pp
);
1878 pp_cxx_expression (pp
, FOR_EXPR (t
));
1879 pp_cxx_right_paren (pp
);
1880 pp_newline_and_indent (pp
, 3);
1881 pp_cxx_statement (pp
, FOR_BODY (t
));
1882 pp_indentation (pp
) -= 3;
1883 pp_needs_newline (pp
) = true;
1889 return expression(opt) ; */
1892 pp_string (pp
, TREE_CODE (t
) == BREAK_STMT
? "break" : "continue");
1893 pp_cxx_semicolon (pp
);
1894 pp_needs_newline (pp
) = true;
1897 /* expression-statement:
1898 expression(opt) ; */
1900 pp_cxx_expression (pp
, EXPR_STMT_EXPR (t
));
1901 pp_cxx_semicolon (pp
);
1902 pp_needs_newline (pp
) = true;
1906 pp_cxx_ws_string (pp
, "try");
1907 pp_newline_and_indent (pp
, 2);
1908 pp_cxx_statement (pp
, CLEANUP_BODY (t
));
1909 pp_newline_and_indent (pp
, -2);
1910 pp_cxx_ws_string (pp
, CLEANUP_EH_ONLY (t
) ? "catch" : "finally");
1911 pp_newline_and_indent (pp
, 2);
1912 pp_cxx_statement (pp
, CLEANUP_EXPR (t
));
1913 pp_newline_and_indent (pp
, -2);
1917 pp_cxx_declaration (pp
, t
);
1921 pp_c_statement (pp_c_base (pp
), t
);
1926 /* original-namespace-definition:
1927 namespace identifier { namespace-body }
1929 As an edge case, we also handle unnamed namespace definition here. */
1932 pp_cxx_original_namespace_definition (cxx_pretty_printer
*pp
, tree t
)
1934 pp_cxx_ws_string (pp
, "namespace");
1935 if (DECL_CONTEXT (t
))
1936 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1938 pp_cxx_unqualified_id (pp
, t
);
1939 pp_cxx_whitespace (pp
);
1940 pp_cxx_left_brace (pp
);
1941 /* We do not print the namespace-body. */
1942 pp_cxx_whitespace (pp
);
1943 pp_cxx_right_brace (pp
);
1949 namespace-alias-definition:
1950 namespace identifier = qualified-namespace-specifier ;
1952 qualified-namespace-specifier:
1953 ::(opt) nested-name-specifier(opt) namespace-name */
1956 pp_cxx_namespace_alias_definition (cxx_pretty_printer
*pp
, tree t
)
1958 pp_cxx_ws_string (pp
, "namespace");
1959 if (DECL_CONTEXT (t
))
1960 pp_cxx_nested_name_specifier (pp
, DECL_CONTEXT (t
));
1961 pp_cxx_unqualified_id (pp
, t
);
1962 pp_cxx_whitespace (pp
);
1964 pp_cxx_whitespace (pp
);
1965 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)))
1966 pp_cxx_nested_name_specifier (pp
,
1967 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t
)));
1968 pp_cxx_qualified_id (pp
, DECL_NAMESPACE_ALIAS (t
));
1969 pp_cxx_semicolon (pp
);
1972 /* simple-declaration:
1973 decl-specifier-seq(opt) init-declarator-list(opt) */
1976 pp_cxx_simple_declaration (cxx_pretty_printer
*pp
, tree t
)
1978 pp_cxx_decl_specifier_seq (pp
, t
);
1979 pp_cxx_init_declarator (pp
, t
);
1980 pp_cxx_semicolon (pp
);
1981 pp_needs_newline (pp
) = true;
1985 template-parameter-list:
1987 template-parameter-list , template-parameter */
1990 pp_cxx_template_parameter_list (cxx_pretty_printer
*pp
, tree t
)
1992 const int n
= TREE_VEC_LENGTH (t
);
1994 for (i
= 0; i
< n
; ++i
)
1997 pp_cxx_separate_with (pp
, ',');
1998 pp_cxx_template_parameter (pp
, TREE_VEC_ELT (t
, i
));
2002 /* template-parameter:
2004 parameter-declaration
2007 class ...(opt) identifier(opt)
2008 class identifier(opt) = type-id
2009 typename identifier(opt)
2010 typename ...(opt) identifier(opt) = type-id
2011 template < template-parameter-list > class ...(opt) identifier(opt)
2012 template < template-parameter-list > class identifier(opt) = template-name */
2015 pp_cxx_template_parameter (cxx_pretty_printer
*pp
, tree t
)
2017 tree parameter
= TREE_VALUE (t
);
2018 switch (TREE_CODE (parameter
))
2021 pp_cxx_ws_string (pp
, "class");
2022 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2023 pp_cxx_ws_string (pp
, "...");
2024 if (DECL_NAME (parameter
))
2025 pp_cxx_tree_identifier (pp
, DECL_NAME (parameter
));
2026 /* FIXME: Check if we should print also default argument. */
2030 pp_cxx_parameter_declaration (pp
, parameter
);
2037 pp_unsupported_tree (pp
, t
);
2042 /* Pretty-print a template parameter in the canonical form
2043 "template-parameter-<level>-<position in parameter list>". */
2046 pp_cxx_canonical_template_parameter (cxx_pretty_printer
*pp
, tree parm
)
2048 const enum tree_code code
= TREE_CODE (parm
);
2050 /* Brings type template parameters to the canonical forms. */
2051 if (code
== TEMPLATE_TYPE_PARM
|| code
== TEMPLATE_TEMPLATE_PARM
2052 || code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
2053 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
2055 pp_cxx_begin_template_argument_list (pp
);
2056 pp_cxx_ws_string (pp
, M_("template-parameter-"));
2057 pp_wide_integer (pp
, TEMPLATE_PARM_LEVEL (parm
));
2059 pp_wide_integer (pp
, TEMPLATE_PARM_IDX (parm
) + 1);
2060 pp_cxx_end_template_argument_list (pp
);
2064 template-declaration:
2065 export(opt) template < template-parameter-list > declaration */
2068 pp_cxx_template_declaration (cxx_pretty_printer
*pp
, tree t
)
2070 tree tmpl
= most_general_template (t
);
2074 pp_maybe_newline_and_indent (pp
, 0);
2075 for (level
= DECL_TEMPLATE_PARMS (tmpl
); level
; level
= TREE_CHAIN (level
))
2077 pp_cxx_ws_string (pp
, "template");
2078 pp_cxx_begin_template_argument_list (pp
);
2079 pp_cxx_template_parameter_list (pp
, TREE_VALUE (level
));
2080 pp_cxx_end_template_argument_list (pp
);
2081 pp_newline_and_indent (pp
, 3);
2084 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_SAVED_TREE (t
))
2085 pp_cxx_function_definition (pp
, t
);
2087 pp_cxx_simple_declaration (pp
, t
);
2091 pp_cxx_explicit_specialization (cxx_pretty_printer
*pp
, tree t
)
2093 pp_unsupported_tree (pp
, t
);
2097 pp_cxx_explicit_instantiation (cxx_pretty_printer
*pp
, tree t
)
2099 pp_unsupported_tree (pp
, t
);
2106 template-declaration
2107 explicit-instantiation
2108 explicit-specialization
2109 linkage-specification
2110 namespace-definition
2115 namespace-alias-definition
2118 static_assert-declaration */
2120 pp_cxx_declaration (cxx_pretty_printer
*pp
, tree t
)
2122 if (TREE_CODE (t
) == STATIC_ASSERT
)
2124 pp_cxx_ws_string (pp
, "static_assert");
2125 pp_cxx_left_paren (pp
);
2126 pp_cxx_expression (pp
, STATIC_ASSERT_CONDITION (t
));
2127 pp_cxx_separate_with (pp
, ',');
2128 pp_cxx_expression (pp
, STATIC_ASSERT_MESSAGE (t
));
2129 pp_cxx_right_paren (pp
);
2131 else if (!DECL_LANG_SPECIFIC (t
))
2132 pp_cxx_simple_declaration (pp
, t
);
2133 else if (DECL_USE_TEMPLATE (t
))
2134 switch (DECL_USE_TEMPLATE (t
))
2137 pp_cxx_template_declaration (pp
, t
);
2141 pp_cxx_explicit_specialization (pp
, t
);
2145 pp_cxx_explicit_instantiation (pp
, t
);
2151 else switch (TREE_CODE (t
))
2155 pp_cxx_simple_declaration (pp
, t
);
2159 if (DECL_SAVED_TREE (t
))
2160 pp_cxx_function_definition (pp
, t
);
2162 pp_cxx_simple_declaration (pp
, t
);
2165 case NAMESPACE_DECL
:
2166 if (DECL_NAMESPACE_ALIAS (t
))
2167 pp_cxx_namespace_alias_definition (pp
, t
);
2169 pp_cxx_original_namespace_definition (pp
, t
);
2173 pp_unsupported_tree (pp
, t
);
2179 pp_cxx_typeid_expression (cxx_pretty_printer
*pp
, tree t
)
2181 t
= TREE_OPERAND (t
, 0);
2182 pp_cxx_ws_string (pp
, "typeid");
2183 pp_cxx_left_paren (pp
);
2185 pp_cxx_type_id (pp
, t
);
2187 pp_cxx_expression (pp
, t
);
2188 pp_cxx_right_paren (pp
);
2192 pp_cxx_va_arg_expression (cxx_pretty_printer
*pp
, tree t
)
2194 pp_cxx_ws_string (pp
, "va_arg");
2195 pp_cxx_left_paren (pp
);
2196 pp_cxx_assignment_expression (pp
, TREE_OPERAND (t
, 0));
2197 pp_cxx_separate_with (pp
, ',');
2198 pp_cxx_type_id (pp
, TREE_TYPE (t
));
2199 pp_cxx_right_paren (pp
);
2203 pp_cxx_offsetof_expression_1 (cxx_pretty_printer
*pp
, tree t
)
2205 switch (TREE_CODE (t
))
2208 if (TREE_CODE (TREE_OPERAND (t
, 0)) == STATIC_CAST_EXPR
2209 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
2211 pp_cxx_type_id (pp
, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t
, 0))));
2212 pp_cxx_separate_with (pp
, ',');
2217 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2219 if (TREE_CODE (TREE_OPERAND (t
, 0)) != ARROW_EXPR
)
2221 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2224 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2226 pp_left_bracket (pp
);
2227 pp_cxx_expression (pp
, TREE_OPERAND (t
, 1));
2228 pp_right_bracket (pp
);
2236 pp_cxx_offsetof_expression (cxx_pretty_printer
*pp
, tree t
)
2238 pp_cxx_ws_string (pp
, "offsetof");
2239 pp_cxx_left_paren (pp
);
2240 if (!pp_cxx_offsetof_expression_1 (pp
, TREE_OPERAND (t
, 0)))
2241 pp_cxx_expression (pp
, TREE_OPERAND (t
, 0));
2242 pp_cxx_right_paren (pp
);
2246 pp_cxx_trait_expression (cxx_pretty_printer
*pp
, tree t
)
2248 cp_trait_kind kind
= TRAIT_EXPR_KIND (t
);
2252 case CPTK_HAS_NOTHROW_ASSIGN
:
2253 pp_cxx_ws_string (pp
, "__has_nothrow_assign");
2255 case CPTK_HAS_TRIVIAL_ASSIGN
:
2256 pp_cxx_ws_string (pp
, "__has_trivial_assign");
2258 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2259 pp_cxx_ws_string (pp
, "__has_nothrow_constructor");
2261 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2262 pp_cxx_ws_string (pp
, "__has_trivial_constructor");
2264 case CPTK_HAS_NOTHROW_COPY
:
2265 pp_cxx_ws_string (pp
, "__has_nothrow_copy");
2267 case CPTK_HAS_TRIVIAL_COPY
:
2268 pp_cxx_ws_string (pp
, "__has_trivial_copy");
2270 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2271 pp_cxx_ws_string (pp
, "__has_trivial_destructor");
2273 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2274 pp_cxx_ws_string (pp
, "__has_virtual_destructor");
2276 case CPTK_IS_ABSTRACT
:
2277 pp_cxx_ws_string (pp
, "__is_abstract");
2279 case CPTK_IS_BASE_OF
:
2280 pp_cxx_ws_string (pp
, "__is_base_of");
2283 pp_cxx_ws_string (pp
, "__is_class");
2285 case CPTK_IS_CONVERTIBLE_TO
:
2286 pp_cxx_ws_string (pp
, "__is_convertible_to");
2289 pp_cxx_ws_string (pp
, "__is_empty");
2292 pp_cxx_ws_string (pp
, "__is_enum");
2295 pp_cxx_ws_string (pp
, "__is_pod");
2297 case CPTK_IS_POLYMORPHIC
:
2298 pp_cxx_ws_string (pp
, "__is_polymorphic");
2300 case CPTK_IS_STD_LAYOUT
:
2301 pp_cxx_ws_string (pp
, "__is_std_layout");
2303 case CPTK_IS_TRIVIAL
:
2304 pp_cxx_ws_string (pp
, "__is_trivial");
2307 pp_cxx_ws_string (pp
, "__is_union");
2314 pp_cxx_left_paren (pp
);
2315 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE1 (t
));
2317 if (kind
== CPTK_IS_BASE_OF
|| kind
== CPTK_IS_CONVERTIBLE_TO
)
2319 pp_cxx_separate_with (pp
, ',');
2320 pp_cxx_type_id (pp
, TRAIT_EXPR_TYPE2 (t
));
2323 pp_cxx_right_paren (pp
);
2326 typedef c_pretty_print_fn pp_fun
;
2328 /* Initialization of a C++ pretty-printer object. */
2331 pp_cxx_pretty_printer_init (cxx_pretty_printer
*pp
)
2333 pp_c_pretty_printer_init (pp_c_base (pp
));
2334 pp_set_line_maximum_length (pp
, 0);
2336 pp
->c_base
.declaration
= (pp_fun
) pp_cxx_declaration
;
2337 pp
->c_base
.declaration_specifiers
= (pp_fun
) pp_cxx_decl_specifier_seq
;
2338 pp
->c_base
.function_specifier
= (pp_fun
) pp_cxx_function_specifier
;
2339 pp
->c_base
.type_specifier_seq
= (pp_fun
) pp_cxx_type_specifier_seq
;
2340 pp
->c_base
.declarator
= (pp_fun
) pp_cxx_declarator
;
2341 pp
->c_base
.direct_declarator
= (pp_fun
) pp_cxx_direct_declarator
;
2342 pp
->c_base
.parameter_list
= (pp_fun
) pp_cxx_parameter_declaration_clause
;
2343 pp
->c_base
.type_id
= (pp_fun
) pp_cxx_type_id
;
2344 pp
->c_base
.abstract_declarator
= (pp_fun
) pp_cxx_abstract_declarator
;
2345 pp
->c_base
.direct_abstract_declarator
=
2346 (pp_fun
) pp_cxx_direct_abstract_declarator
;
2347 pp
->c_base
.simple_type_specifier
= (pp_fun
)pp_cxx_simple_type_specifier
;
2349 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2351 pp
->c_base
.constant
= (pp_fun
) pp_cxx_constant
;
2352 pp
->c_base
.id_expression
= (pp_fun
) pp_cxx_id_expression
;
2353 pp
->c_base
.primary_expression
= (pp_fun
) pp_cxx_primary_expression
;
2354 pp
->c_base
.postfix_expression
= (pp_fun
) pp_cxx_postfix_expression
;
2355 pp
->c_base
.unary_expression
= (pp_fun
) pp_cxx_unary_expression
;
2356 pp
->c_base
.multiplicative_expression
= (pp_fun
) pp_cxx_multiplicative_expression
;
2357 pp
->c_base
.conditional_expression
= (pp_fun
) pp_cxx_conditional_expression
;
2358 pp
->c_base
.assignment_expression
= (pp_fun
) pp_cxx_assignment_expression
;
2359 pp
->c_base
.expression
= (pp_fun
) pp_cxx_expression
;
2360 pp
->enclosing_scope
= global_namespace
;