1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003, 2004 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
27 #include "cxx-pretty-print.h"
31 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
32 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
33 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
34 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree);
35 static void pp_cxx_expression (cxx_pretty_printer *, tree);
36 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
37 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
38 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
39 static void pp_cxx_type_id (cxx_pretty_printer *, tree);
40 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree);
41 static void pp_cxx_declarator (cxx_pretty_printer *, tree);
42 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
43 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree);
44 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
48 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
50 const char *p = pp_last_position_in_text (pp);
52 if (p != NULL && *p == c)
53 pp_cxx_whitespace (pp);
55 pp_base (pp)->padding = pp_none;
58 #define pp_cxx_storage_class_specifier(PP, T) \
59 pp_c_storage_class_specifier (pp_c_base (PP), T)
60 #define pp_cxx_expression_list(PP, T) \
61 pp_c_expression_list (pp_c_base (PP), T)
62 #define pp_cxx_space_for_pointer_operator(PP, T) \
63 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
64 #define pp_cxx_init_declarator(PP, T) \
65 pp_c_init_declarator (pp_c_base (PP), T)
66 #define pp_cxx_call_argument_list(PP, T) \
67 pp_c_call_argument_list (pp_c_base (PP), T)
70 pp_cxx_colon_colon (cxx_pretty_printer *pp)
73 pp_base (pp)->padding = pp_none;
77 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
79 pp_cxx_nonconsecutive_character (pp, '<');
83 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
85 pp_cxx_nonconsecutive_character (pp, '>');
89 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
91 pp_separate_with (pp, c);
92 pp_base (pp)->padding = pp_none;
98 is_destructor_name (tree name)
100 return name == complete_dtor_identifier
101 || name == base_dtor_identifier
102 || name == deleting_dtor_identifier;
105 /* conversion-function-id:
106 operator conversion-type-id
109 type-specifier-seq conversion-declarator(opt)
111 conversion-declarator:
112 ptr-operator conversion-declarator(opt) */
115 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
117 pp_cxx_identifier (pp, "operator");
118 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
122 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
124 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
125 pp_cxx_begin_template_argument_list (pp);
126 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
127 pp_cxx_end_template_argument_list (pp);
133 conversion-function-id
138 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
140 enum tree_code code = TREE_CODE (t);
144 pp_cxx_identifier (pp, "<return-value>");
161 case IDENTIFIER_NODE:
163 pp_cxx_identifier (pp, "<unnamed>");
164 else if (IDENTIFIER_TYPENAME_P (t))
165 pp_cxx_conversion_function_id (pp, t);
168 if (is_destructor_name (t))
171 /* FIXME: Why is this necessary? */
173 t = constructor_name (TREE_TYPE (t));
175 pp_cxx_tree_identifier (pp, t);
179 case TEMPLATE_ID_EXPR:
180 pp_cxx_template_id (pp, t);
186 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
189 case TEMPLATE_TYPE_PARM:
190 case TEMPLATE_TEMPLATE_PARM:
191 if (TYPE_IDENTIFIER (t))
192 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
194 pp_cxx_canonical_template_parameter (pp, t);
197 case TEMPLATE_PARM_INDEX:
198 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
202 pp_unsupported_tree (pp, t);
207 /* Pretty-print out the token sequence ":: template" in template codes
208 where it is needed to "inline declare" the (following) member as
209 a template. This situation arises when SCOPE of T is dependent
210 on template parameters. */
213 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
215 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
216 && TYPE_P (scope) && dependent_type_p (scope))
217 pp_cxx_identifier (pp, "template");
220 /* nested-name-specifier:
221 class-or-namespace-name :: nested-name-specifier(opt)
222 class-or-namespace-name :: template nested-name-specifier */
225 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
227 if (t != NULL && t != pp->enclosing_scope)
229 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
230 pp_cxx_nested_name_specifier (pp, scope);
231 pp_cxx_template_keyword_if_needed (pp, scope, t);
232 pp_cxx_unqualified_id (pp, t);
233 pp_cxx_colon_colon (pp);
238 nested-name-specifier template(opt) unqualified-id */
241 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
243 switch (TREE_CODE (t))
245 /* A pointer-to-member is always qualified. */
247 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
248 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
251 /* In Standard C++, functions cannot possibly be used as
252 nested-name-specifiers. However, there are situations where
253 is "makes sense" to output the surrounding function name for the
254 purpose of emphasizing on the scope kind. Just printing the
255 function name might not be sufficient as it may be overloaded; so,
256 we decorate the function with its signature too.
257 FIXME: This is probably the wrong pretty-printing for conversion
258 functions and some function templates. */
262 if (DECL_FUNCTION_MEMBER_P (t))
263 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
264 pp_cxx_unqualified_id
265 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
266 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
271 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
272 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
277 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
278 if (scope != pp->enclosing_scope)
280 pp_cxx_nested_name_specifier (pp, scope);
281 pp_cxx_template_keyword_if_needed (pp, scope, t);
283 pp_cxx_unqualified_id (pp, t);
294 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
296 if (TREE_CODE (t) == OVERLOAD)
298 if (DECL_P (t) && DECL_CONTEXT (t))
299 pp_cxx_qualified_id (pp, t);
301 pp_cxx_unqualified_id (pp, t);
304 /* primary-expression:
308 :: operator-function-id
314 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
316 switch (TREE_CODE (t))
321 pp_c_constant (pp_c_base (pp), t);
325 t = BASELINK_FUNCTIONS (t);
333 pp_cxx_id_expression (pp, t);
337 case TEMPLATE_TYPE_PARM:
338 case TEMPLATE_TEMPLATE_PARM:
339 case TEMPLATE_PARM_INDEX:
340 pp_cxx_unqualified_id (pp, t);
344 pp_c_primary_expression (pp_c_base (pp), t);
349 /* postfix-expression:
351 postfix-expression [ expression ]
352 postfix-expression ( expression-list(opt) )
353 simple-type-specifier ( expression-list(opt) )
354 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
355 typename ::(opt) nested-name-specifier template(opt)
356 template-id ( expression-list(opt) )
357 postfix-expression . template(opt) ::(opt) id-expression
358 postfix-expression -> template(opt) ::(opt) id-expression
359 postfix-expression . pseudo-destructor-name
360 postfix-expression -> pseudo-destructor-name
361 postfix-expression ++
362 postfix-expression --
363 dynamic_cast < type-id > ( expression )
364 static_cast < type-id > ( expression )
365 reinterpret_cast < type-id > ( expression )
366 const_cast < type-id > ( expression )
367 typeid ( expression )
368 typeif ( type-id ) */
371 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
373 enum tree_code code = TREE_CODE (t);
380 tree fun = TREE_OPERAND (t, 0);
381 tree args = TREE_OPERAND (t, 1);
382 tree saved_scope = pp->enclosing_scope;
384 if (TREE_CODE (fun) == ADDR_EXPR)
385 fun = TREE_OPERAND (fun, 0);
387 /* In templates, where there is no way to tell whether a given
388 call uses an actual member function. So the parser builds
389 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
390 instantiation time. */
391 if (TREE_CODE (fun) != FUNCTION_DECL)
393 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
395 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
396 ? TREE_OPERAND (t, 2)
399 while (TREE_CODE (object) == NOP_EXPR)
400 object = TREE_OPERAND (object, 0);
402 if (TREE_CODE (object) == ADDR_EXPR)
403 object = TREE_OPERAND (object, 0);
405 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
407 pp_cxx_postfix_expression (pp, object);
412 pp_cxx_postfix_expression (pp, object);
415 args = TREE_CHAIN (args);
416 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
419 pp_cxx_postfix_expression (pp, fun);
420 pp->enclosing_scope = saved_scope;
421 pp_cxx_call_argument_list (pp, args);
423 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
425 pp_cxx_separate_with (pp, ',');
426 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
439 pp_cxx_primary_expression (pp, t);
442 case DYNAMIC_CAST_EXPR:
443 case STATIC_CAST_EXPR:
444 case REINTERPRET_CAST_EXPR:
445 case CONST_CAST_EXPR:
446 if (code == DYNAMIC_CAST_EXPR)
447 pp_cxx_identifier (pp, "dynamic_cast");
448 else if (code == STATIC_CAST_EXPR)
449 pp_cxx_identifier (pp, "static_cast");
450 else if (code == REINTERPRET_CAST_EXPR)
451 pp_cxx_identifier (pp, "reinterpret_cast");
453 pp_cxx_identifier (pp, "const_cast");
454 pp_cxx_begin_template_argument_list (pp);
455 pp_cxx_type_id (pp, TREE_TYPE (t));
456 pp_cxx_end_template_argument_list (pp);
458 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
462 case EMPTY_CLASS_EXPR:
463 pp_cxx_type_id (pp, TREE_TYPE (t));
469 t = TREE_OPERAND (t, 0);
470 pp_cxx_identifier (pp, "typeid");
473 pp_cxx_type_id (pp, t);
475 pp_cxx_expression (pp, t);
479 case PSEUDO_DTOR_EXPR:
480 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
482 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
483 pp_cxx_colon_colon (pp);
485 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
489 pp_c_postfix_expression (pp_c_base (pp), t);
495 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
496 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
502 type-specifier-seq new-declarator(opt)
505 ptr-operator new-declarator(opt)
506 direct-new-declarator
508 direct-new-declarator
510 direct-new-declarator [ constant-expression ]
513 ( expression-list(opt) ) */
516 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
518 enum tree_code code = TREE_CODE (t);
523 if (NEW_EXPR_USE_GLOBAL (t))
524 pp_cxx_colon_colon (pp);
525 pp_cxx_identifier (pp, "new");
526 if (TREE_OPERAND (t, 0))
528 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
531 /* FIXME: array-types are built with one more element. */
532 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
533 if (TREE_OPERAND (t, 2))
536 t = TREE_OPERAND (t, 2);
537 if (TREE_CODE (t) == TREE_LIST)
538 pp_c_expression_list (pp_c_base (pp), t);
539 else if (t == void_zero_node)
540 ; /* OK, empty initializer list. */
542 pp_cxx_expression (pp, t);
548 pp_unsupported_tree (pp, t);
552 /* delete-expression:
553 ::(opt) delete cast-expression
554 ::(opt) delete [ ] cast-expression */
557 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
559 enum tree_code code = TREE_CODE (t);
563 case VEC_DELETE_EXPR:
564 if (DELETE_EXPR_USE_GLOBAL (t))
565 pp_cxx_colon_colon (pp);
566 pp_cxx_identifier (pp, "delete");
567 if (code == VEC_DELETE_EXPR)
569 pp_left_bracket (pp);
570 pp_right_bracket (pp);
572 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
576 pp_unsupported_tree (pp, t);
584 unary-operator cast-expression
585 sizeof unary-expression
590 unary-operator: one of
594 __alignof__ unary-expression
595 __alignof__ ( type-id ) */
598 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
600 enum tree_code code = TREE_CODE (t);
605 pp_cxx_new_expression (pp, t);
609 case VEC_DELETE_EXPR:
610 pp_cxx_delete_expression (pp, t);
614 pp_c_unary_expression (pp_c_base (pp), t);
621 ( type-id ) cast-expression */
624 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
626 switch (TREE_CODE (t))
629 pp_cxx_type_id (pp, TREE_TYPE (t));
630 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
634 pp_c_cast_expression (pp_c_base (pp), t);
641 pm-expression .* cast-expression
642 pm-expression ->* cast-expression */
645 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
647 switch (TREE_CODE (t))
649 /* Handle unfortunate OFFESET_REF overloading here. */
651 if (TYPE_P (TREE_OPERAND (t, 0)))
653 pp_cxx_qualified_id (pp, t);
656 /* Else fall through. */
659 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
662 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
667 pp_cxx_cast_expression (pp, t);
672 /* multiplicative-expression:
674 multiplicative-expression * pm-expression
675 multiplicative-expression / pm-expression
676 multiplicative-expression % pm-expression */
679 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
681 enum tree_code code = TREE_CODE (e);
687 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
689 if (code == MULT_EXPR)
691 else if (code == TRUNC_DIV_EXPR)
696 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
700 pp_cxx_pm_expression (pp, e);
705 /* conditional-expression:
706 logical-or-expression
707 logical-or-expression ? expression : assignment-expression */
710 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
712 if (TREE_CODE (e) == COND_EXPR)
714 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
718 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
720 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
723 pp_c_logical_or_expression (pp_c_base (pp), e);
726 /* Pretty-print a compound assignment operator token as indicated by T. */
729 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
733 switch (TREE_CODE (t))
756 op = tree_code_name[TREE_CODE (t)];
760 pp_cxx_identifier (pp, op);
764 /* assignment-expression:
765 conditional-expression
766 logical-or-expression assignment-operator assignment-expression
770 throw assignment-expression(opt)
772 assignment-operator: one of
773 = *= /= %= += -= >>= <<= &= ^= |= */
776 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
778 switch (TREE_CODE (e))
782 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
786 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
790 pp_cxx_identifier (pp, "throw");
791 if (TREE_OPERAND (e, 0))
792 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
796 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
797 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
798 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
802 pp_cxx_conditional_expression (pp, e);
808 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
810 switch (TREE_CODE (t))
815 pp_c_constant (pp_c_base (pp), t);
819 pp_cxx_unqualified_id (pp, t);
827 pp_cxx_qualified_id (pp, t);
839 case TEMPLATE_TYPE_PARM:
840 case TEMPLATE_PARM_INDEX:
841 case TEMPLATE_TEMPLATE_PARM:
842 pp_cxx_primary_expression (pp, t);
846 case DYNAMIC_CAST_EXPR:
847 case STATIC_CAST_EXPR:
848 case REINTERPRET_CAST_EXPR:
849 case CONST_CAST_EXPR:
853 case EMPTY_CLASS_EXPR:
855 case PSEUDO_DTOR_EXPR:
857 pp_cxx_postfix_expression (pp, t);
862 pp_cxx_new_expression (pp, t);
866 case VEC_DELETE_EXPR:
867 pp_cxx_delete_expression (pp, t);
871 pp_cxx_cast_expression (pp, t);
877 pp_cxx_pm_expression (pp, t);
883 pp_cxx_multiplicative_expression (pp, t);
887 pp_cxx_conditional_expression (pp, t);
894 pp_cxx_assignment_expression (pp, t);
897 case NON_DEPENDENT_EXPR:
898 case MUST_NOT_THROW_EXPR:
899 pp_cxx_expression (pp, t);
903 pp_c_expression (pp_c_base (pp), t);
911 /* function-specifier:
917 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
919 switch (TREE_CODE (t))
922 if (DECL_VIRTUAL_P (t))
923 pp_cxx_identifier (pp, "virtual");
924 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
925 pp_cxx_identifier (pp, "explicit");
927 pp_c_function_specifier (pp_c_base (pp), t);
934 /* decl-specifier-seq:
935 decl-specifier-seq(opt) decl-specifier
938 storage-class-specifier
945 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
947 switch (TREE_CODE (t))
953 pp_cxx_storage_class_specifier (pp, t);
954 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
958 pp_cxx_identifier (pp, "typedef");
959 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
963 if (TYPE_PTRMEMFUNC_P (t))
965 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
966 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
967 pp_cxx_whitespace (pp);
968 pp_cxx_ptr_operator (pp, t);
973 /* Constructors don't have return types. And conversion functions
974 do not have a type-specifier in their return types. */
975 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
976 pp_cxx_function_specifier (pp, t);
977 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
978 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
981 pp_c_declaration_specifiers (pp_c_base (pp), t);
986 /* simple-type-specifier:
987 ::(opt) nested-name-specifier(opt) type-name
988 ::(opt) nested-name-specifier(opt) template(opt) template-id
1002 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1004 switch (TREE_CODE (t))
1009 pp_cxx_qualified_id (pp, t);
1012 case TEMPLATE_TYPE_PARM:
1013 case TEMPLATE_TEMPLATE_PARM:
1014 case TEMPLATE_PARM_INDEX:
1015 pp_cxx_unqualified_id (pp, t);
1019 pp_cxx_identifier (pp, "typename");
1020 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1021 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1025 pp_c_type_specifier (pp_c_base (pp), t);
1030 /* type-specifier-seq:
1031 type-specifier type-specifier-seq(opt)
1034 simple-type-specifier
1037 elaborated-type-specifier
1041 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1043 switch (TREE_CODE (t))
1046 case TEMPLATE_TYPE_PARM:
1047 case TEMPLATE_TEMPLATE_PARM:
1049 case BOUND_TEMPLATE_TEMPLATE_PARM:
1050 pp_cxx_cv_qualifier_seq (pp, t);
1051 pp_cxx_simple_type_specifier (pp, t);
1055 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1056 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1057 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1061 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1062 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1067 * cv-qualifier-seq(opt)
1069 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1072 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1074 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1076 switch (TREE_CODE (t))
1078 case REFERENCE_TYPE:
1080 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1081 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1082 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1083 if (TREE_CODE (t) == POINTER_TYPE)
1086 pp_cxx_cv_qualifier_seq (pp, t);
1093 if (TYPE_PTRMEMFUNC_P (t))
1095 pp_cxx_left_paren (pp);
1096 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1101 if (TYPE_PTR_TO_MEMBER_P (t))
1103 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1105 pp_cxx_cv_qualifier_seq (pp, t);
1108 /* else fall through. */
1111 pp_unsupported_tree (pp, t);
1117 pp_cxx_implicit_parameter_type (tree mf)
1119 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1123 parameter-declaration:
1124 decl-specifier-seq declarator
1125 decl-specifier-seq declarator = assignment-expression
1126 decl-specifier-seq abstract-declarator(opt)
1127 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1130 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1132 pp_cxx_decl_specifier_seq (pp, t);
1134 pp_cxx_abstract_declarator (pp, t);
1136 pp_cxx_declarator (pp, t);
1139 /* parameter-declaration-clause:
1140 parameter-declaration-list(opt) ...(opt)
1141 parameter-declaration-list , ...
1143 parameter-declaration-list:
1144 parameter-declaration
1145 parameter-declaration-list , parameter-declaration */
1148 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1150 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1151 tree types = TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1152 const bool abstract = args == NULL
1153 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1156 /* Skip artificial parameter for nonstatic member functions. */
1157 if (TREE_CODE (t) == METHOD_TYPE)
1158 types = TREE_CHAIN (types);
1160 pp_cxx_left_paren (pp);
1161 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1164 pp_cxx_separate_with (pp, ',');
1166 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1167 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1169 pp_cxx_whitespace (pp);
1171 pp_cxx_whitespace (pp);
1172 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1175 pp_cxx_right_paren (pp);
1178 /* exception-specification:
1179 throw ( type-id-list(opt) )
1183 type-id-list , type-id */
1186 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1188 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1190 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1192 pp_cxx_identifier (pp, "throw");
1193 pp_cxx_left_paren (pp);
1194 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1196 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1197 if (TREE_CHAIN (ex_spec))
1198 pp_cxx_separate_with (pp, ',');
1200 pp_cxx_right_paren (pp);
1203 /* direct-declarator:
1205 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1206 exception-specification(opt)
1207 direct-declaration [ constant-expression(opt) ]
1211 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1213 switch (TREE_CODE (t))
1221 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1222 pp_cxx_id_expression (pp, DECL_NAME (t));
1224 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1228 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1229 pp_cxx_id_expression (pp, t);
1230 pp_cxx_parameter_declaration_clause (pp, t);
1232 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1234 pp_base (pp)->padding = pp_before;
1235 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1238 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1243 case TEMPLATE_TYPE_PARM:
1244 case TEMPLATE_PARM_INDEX:
1245 case TEMPLATE_TEMPLATE_PARM:
1249 pp_c_direct_declarator (pp_c_base (pp), t);
1256 ptr-operator declarator */
1259 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1261 pp_cxx_direct_declarator (pp, t);
1264 /* ctor-initializer:
1265 : mem-initializer-list
1267 mem-initializer-list:
1269 mem-initializer , mem-initializer-list
1272 mem-initializer-id ( expression-list(opt) )
1275 ::(opt) nested-name-specifier(opt) class-name
1279 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1281 t = TREE_OPERAND (t, 0);
1282 pp_cxx_whitespace (pp);
1284 pp_cxx_whitespace (pp);
1285 for (; t; t = TREE_CHAIN (t))
1287 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1288 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1290 pp_cxx_separate_with (pp, ',');
1294 /* function-definition:
1295 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1296 decl-specifier-seq(opt) declarator function-try-block */
1299 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1301 tree saved_scope = pp->enclosing_scope;
1302 pp_cxx_decl_specifier_seq (pp, t);
1303 pp_cxx_declarator (pp, t);
1304 pp_needs_newline (pp) = true;
1305 pp->enclosing_scope = DECL_CONTEXT (t);
1306 if (DECL_SAVED_TREE (t))
1307 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1310 pp_cxx_semicolon (pp);
1311 pp_needs_newline (pp) = true;
1314 pp->enclosing_scope = saved_scope;
1317 /* abstract-declarator:
1318 ptr-operator abstract-declarator(opt)
1319 direct-abstract-declarator */
1322 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1324 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1325 pp_cxx_right_paren (pp);
1326 else if (POINTER_TYPE_P (t))
1328 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1329 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1330 pp_cxx_right_paren (pp);
1333 pp_cxx_direct_abstract_declarator (pp, t);
1336 /* direct-abstract-declarator:
1337 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1338 cv-qualifier-seq(opt) exception-specification(opt)
1339 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1340 ( abstract-declarator ) */
1343 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1345 switch (TREE_CODE (t))
1347 case REFERENCE_TYPE:
1348 pp_cxx_abstract_declarator (pp, t);
1352 if (TYPE_PTRMEMFUNC_P (t))
1353 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1358 pp_cxx_parameter_declaration_clause (pp, t);
1359 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1360 if (TREE_CODE (t) == METHOD_TYPE)
1362 pp_base (pp)->padding = pp_before;
1363 pp_cxx_cv_qualifier_seq
1364 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1366 pp_cxx_exception_specification (pp, t);
1370 case TEMPLATE_TYPE_PARM:
1371 case TEMPLATE_TEMPLATE_PARM:
1372 case BOUND_TEMPLATE_TEMPLATE_PARM:
1373 case UNBOUND_CLASS_TEMPLATE:
1377 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1383 type-specifier-seq abstract-declarator(opt) */
1386 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1388 pp_flags saved_flags = pp_c_base (pp)->flags;
1389 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1391 switch (TREE_CODE (t))
1398 case BOUND_TEMPLATE_TEMPLATE_PARM:
1399 case UNBOUND_CLASS_TEMPLATE:
1400 case TEMPLATE_TEMPLATE_PARM:
1401 case TEMPLATE_TYPE_PARM:
1402 case TEMPLATE_PARM_INDEX:
1405 case TEMPLATE_ID_EXPR:
1406 pp_cxx_type_specifier_seq (pp, t);
1410 pp_c_type_id (pp_c_base (pp), t);
1414 pp_c_base (pp)->flags = saved_flags;
1417 /* template-argument-list:
1419 template-argument-list, template-argument
1422 assignment-expression
1427 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1432 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1434 tree arg = TREE_VEC_ELT (t, i);
1436 pp_cxx_separate_with (pp, ',');
1437 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1438 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1439 pp_cxx_type_id (pp, arg);
1441 pp_cxx_expression (pp, arg);
1447 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1449 t = DECL_EXPR_DECL (t);
1450 pp_cxx_type_specifier_seq (pp, t);
1452 pp_cxx_abstract_declarator (pp, t);
1454 pp_cxx_declarator (pp, t);
1460 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1462 switch (TREE_CODE (t))
1464 case CTOR_INITIALIZER:
1465 pp_cxx_ctor_initializer (pp, t);
1469 pp_cxx_identifier (pp, "using");
1470 pp_cxx_identifier (pp, "namespace");
1471 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1475 pp_cxx_identifier (pp, "using");
1476 pp_cxx_nested_name_specifier (pp, DECL_INITIAL (t));
1477 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1484 try compound-statement handler-seq */
1486 pp_maybe_newline_and_indent (pp, 0);
1487 pp_cxx_identifier (pp, "try");
1488 pp_newline_and_indent (pp, 3);
1489 pp_cxx_statement (pp, TRY_STMTS (t));
1490 pp_newline_and_indent (pp, -3);
1494 pp_cxx_statement (pp, TRY_HANDLERS (t));
1499 handler handler-seq(opt)
1502 catch ( exception-declaration ) compound-statement
1504 exception-declaration:
1505 type-specifier-seq declarator
1506 type-specifier-seq abstract-declarator
1509 pp_cxx_identifier (pp, "catch");
1510 pp_cxx_left_paren (pp);
1511 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1512 pp_cxx_right_paren (pp);
1513 pp_indentation (pp) += 3;
1514 pp_needs_newline (pp) = true;
1515 pp_cxx_statement (pp, HANDLER_BODY (t));
1516 pp_indentation (pp) -= 3;
1517 pp_needs_newline (pp) = true;
1520 /* selection-statement:
1521 if ( expression ) statement
1522 if ( expression ) statement else statement */
1524 pp_cxx_identifier (pp, "if");
1525 pp_cxx_whitespace (pp);
1526 pp_cxx_left_paren (pp);
1527 pp_cxx_expression (pp, IF_COND (t));
1528 pp_cxx_right_paren (pp);
1529 pp_newline_and_indent (pp, 2);
1530 pp_cxx_statement (pp, THEN_CLAUSE (t));
1531 pp_newline_and_indent (pp, -2);
1532 if (ELSE_CLAUSE (t))
1534 tree else_clause = ELSE_CLAUSE (t);
1535 pp_cxx_identifier (pp, "else");
1536 if (TREE_CODE (else_clause) == IF_STMT)
1537 pp_cxx_whitespace (pp);
1539 pp_newline_and_indent (pp, 2);
1540 pp_cxx_statement (pp, else_clause);
1541 if (TREE_CODE (else_clause) != IF_STMT)
1542 pp_newline_and_indent (pp, -2);
1547 pp_cxx_identifier (pp, "try");
1548 pp_newline_and_indent (pp, 2);
1549 pp_cxx_statement (pp, CLEANUP_BODY (t));
1550 pp_newline_and_indent (pp, -2);
1551 pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1552 pp_newline_and_indent (pp, 2);
1553 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1554 pp_newline_and_indent (pp, -2);
1558 pp_c_statement (pp_c_base (pp), t);
1563 /* original-namespace-definition:
1564 namespace identifier { namespace-body }
1566 As an edge case, we also handle unnamed namespace definition here. */
1569 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1571 pp_cxx_identifier (pp, "namespace");
1573 pp_cxx_unqualified_id (pp, t);
1574 pp_cxx_whitespace (pp);
1575 pp_cxx_left_brace (pp);
1576 /* We do not print the namespace-body. */
1577 pp_cxx_whitespace (pp);
1578 pp_cxx_right_brace (pp);
1584 namespace-alias-definition:
1585 namespace identifier = qualified-namespace-specifier ;
1587 qualified-namespace-specifier:
1588 ::(opt) nested-name-specifier(opt) namespace-name */
1591 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1593 pp_cxx_identifier (pp, "namespace");
1594 pp_cxx_unqualified_id (pp, t);
1595 pp_cxx_whitespace (pp);
1597 pp_cxx_whitespace (pp);
1598 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1599 pp_cxx_semicolon (pp);
1602 /* simple-declaration:
1603 decl-specifier-seq(opt) init-declarator-list(opt) */
1606 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1608 pp_cxx_decl_specifier_seq (pp, t);
1609 pp_cxx_init_declarator (pp, t);
1610 pp_cxx_semicolon (pp);
1611 pp_needs_newline (pp) = true;
1615 template-parameter-list:
1617 template-parameter-list , template-parameter */
1620 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1622 const int n = TREE_VEC_LENGTH (t);
1624 for (i = 0; i < n; ++i)
1627 pp_cxx_separate_with (pp, ',');
1628 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1632 /* template-parameter:
1634 parameter-declaration
1637 class identifier(opt)
1638 class identifier(op) = type-id
1639 typename identifier(opt)
1640 typename identifier(opt) = type-id
1641 template < template-parameter-list > class identifier(opt)
1642 template < template-parameter-list > class identifier(opt) = template-name
1646 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1648 tree parameter = TREE_VALUE (t);
1649 switch (TREE_CODE (parameter))
1652 pp_cxx_identifier (pp, "class");
1653 if (DECL_NAME (parameter))
1654 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1655 /* FIXME: Chech if we should print also default argument. */
1659 pp_cxx_parameter_declaration (pp, parameter);
1666 pp_unsupported_tree (pp, t);
1671 /* Pretty-print a template parameter in the canonical form
1672 "template-parameter-<level>-<position in parameter list>". */
1675 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1677 const enum tree_code code = TREE_CODE (parm);
1679 /* Brings type template parameters to the canonical forms. */
1680 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1681 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1682 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1684 pp_cxx_begin_template_argument_list (pp);
1685 pp_cxx_identifier (pp, "template-parameter-");
1686 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1688 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1689 pp_cxx_end_template_argument_list (pp);
1693 template-declaration:
1694 export(opt) template < template-parameter-list > declaration */
1697 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1699 tree tmpl = most_general_template (t);
1703 pp_maybe_newline_and_indent (pp, 0);
1704 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1706 pp_cxx_identifier (pp, "template");
1707 pp_cxx_begin_template_argument_list (pp);
1708 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1709 pp_cxx_end_template_argument_list (pp);
1710 pp_newline_and_indent (pp, 3);
1713 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1714 pp_cxx_function_definition (pp, t);
1716 pp_cxx_simple_declaration (pp, t);
1720 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1722 pp_unsupported_tree (pp, t);
1726 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1728 pp_unsupported_tree (pp, t);
1735 template-declaration
1736 explicit-instantiation
1737 explicit-specialization
1738 linkage-specification
1739 namespace-definition
1744 namespace-alias-definition
1748 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1750 if (!DECL_LANG_SPECIFIC (t))
1751 pp_cxx_simple_declaration (pp, t);
1752 else if (DECL_USE_TEMPLATE (t))
1753 switch (DECL_USE_TEMPLATE (t))
1756 pp_cxx_template_declaration (pp, t);
1760 pp_cxx_explicit_specialization (pp, t);
1764 pp_cxx_explicit_instantiation (pp, t);
1770 else switch (TREE_CODE (t))
1774 pp_cxx_simple_declaration (pp, t);
1778 if (DECL_SAVED_TREE (t))
1779 pp_cxx_function_definition (pp, t);
1781 pp_cxx_simple_declaration (pp, t);
1784 case NAMESPACE_DECL:
1785 if (DECL_NAMESPACE_ALIAS (t))
1786 pp_cxx_namespace_alias_definition (pp, t);
1788 pp_cxx_original_namespace_definition (pp, t);
1792 pp_unsupported_tree (pp, t);
1798 typedef c_pretty_print_fn pp_fun;
1800 /* Initialization of a C++ pretty-printer object. */
1803 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1805 pp_c_pretty_printer_init (pp_c_base (pp));
1806 pp_set_line_maximum_length (pp, 0);
1808 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1809 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1810 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1811 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1812 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1813 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1814 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1815 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1816 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1817 pp->c_base.direct_abstract_declarator =
1818 (pp_fun) pp_cxx_direct_abstract_declarator;
1819 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1821 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1823 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1824 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1825 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1826 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1827 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1828 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1829 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1830 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1831 pp->enclosing_scope = global_namespace;