1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003 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_abstract_declarator (cxx_pretty_printer *, tree);
43 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
45 #define pp_cxx_whitespace(PP) pp_c_whitespace (pp_c_base (PP))
46 #define pp_cxx_left_paren(PP) pp_c_left_paren (pp_c_base (PP))
47 #define pp_cxx_right_paren(PP) pp_c_right_paren (pp_c_base (PP))
48 #define pp_cxx_left_brace(PP) pp_c_left_brace (pp_c_base (PP))
49 #define pp_cxx_right_brace(PP) pp_c_right_brace (pp_c_base (PP))
50 #define pp_cxx_dot(PP) pp_c_dot (pp_c_base (PP))
51 #define pp_cxx_arrow(PP) pp_c_arrow (pp_c_base (PP))
52 #define pp_cxx_semicolon(PP) pp_c_semicolon (pp_c_base (PP))
55 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
57 const char *p = pp_last_position_in_text (pp);
59 if (p != NULL && *p == c)
60 pp_cxx_whitespace (pp);
62 pp_base (pp)->padding = pp_none;
65 #define pp_cxx_begin_template_argument_list(PP) \
66 pp_cxx_nonconsecutive_character (PP, '<')
67 #define pp_cxx_end_template_argument_list(PP) \
68 pp_cxx_nonconsecutive_character (PP, '>')
70 #define pp_cxx_identifier(PP, ID) pp_c_identifier (pp_c_base (PP), ID)
71 #define pp_cxx_tree_identifier(PP, T) pp_c_tree_identifier (pp_c_base (PP), T)
73 #define pp_cxx_cv_qualifier_seq(PP, T) \
74 pp_c_type_qualifier_list (pp_c_base (PP), T)
75 #define pp_cxx_storage_class_specifier(PP, T) \
76 pp_c_storage_class_specifier (pp_c_base (PP), T)
77 #define pp_cxx_expression_list(PP, T) \
78 pp_c_expression_list (pp_c_base (PP), T)
79 #define pp_cxx_space_for_pointer_operator(PP, T) \
80 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
81 #define pp_cxx_init_declarator(PP, T) \
82 pp_c_init_declarator (pp_c_base (PP), T)
83 #define pp_cxx_call_argument_list(PP, T) \
84 pp_c_call_argument_list (pp_c_base (PP), T)
87 pp_cxx_colon_colon (cxx_pretty_printer *pp)
90 pp_base (pp)->padding = pp_none;
97 is_destructor_name (tree name)
99 return name == complete_dtor_identifier
100 || name == base_dtor_identifier
101 || name == deleting_dtor_identifier;
104 /* conversion-function-id:
105 operator conversion-type-id
108 type-specifier-seq conversion-declarator(opt)
110 conversion-declarator:
111 ptr-operator conversion-declarator(opt) */
113 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
115 pp_cxx_identifier (pp, "operator");
116 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
120 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
122 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
123 pp_cxx_begin_template_argument_list (pp);
124 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
125 pp_cxx_end_template_argument_list (pp);
131 conversion-function-id
135 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
137 enum tree_code code = TREE_CODE (t);
141 pp_cxx_identifier (pp, "<return-value>");
158 case IDENTIFIER_NODE:
160 pp_cxx_identifier (pp, "<anonymous>");
161 else if (IDENTIFIER_TYPENAME_P (t))
162 pp_cxx_conversion_function_id (pp, t);
165 if (is_destructor_name (t))
168 /* FIXME: Why is this necessary? */
170 t = constructor_name (TREE_TYPE (t));
172 pp_cxx_tree_identifier (pp, t);
176 case TEMPLATE_ID_EXPR:
177 pp_cxx_template_id (pp, t);
181 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
187 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
190 case TEMPLATE_TYPE_PARM:
192 case TEMPLATE_PARM_INDEX:
193 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
197 pp_unsupported_tree (pp, t);
203 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
205 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
206 && TYPE_P (scope) && dependent_type_p (scope))
207 pp_cxx_identifier (pp, "template");
210 /* nested-name-specifier:
211 class-or-namespace-name :: nested-name-specifier(opt)
212 class-or-namespace-name :: template nested-name-specifier */
214 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
216 if (t != NULL && t != pp->enclosing_scope)
218 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
219 pp_cxx_nested_name_specifier (pp, scope);
220 pp_cxx_template_keyword_if_needed (pp, scope, t);
221 pp_cxx_unqualified_id (pp, t);
222 pp_cxx_colon_colon (pp);
227 nested-name-specifier template(opt) unqualified-id */
229 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
231 switch (TREE_CODE (t))
234 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
235 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
241 if (DECL_FUNCTION_MEMBER_P (t))
242 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
243 pp_cxx_unqualified_id
244 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
249 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
250 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
255 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
256 if (scope != pp->enclosing_scope)
258 pp_cxx_nested_name_specifier (pp, scope);
259 pp_cxx_template_keyword_if_needed (pp, scope, t);
261 pp_cxx_unqualified_id (pp, t);
271 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
273 if (TREE_CODE (t) == OVERLOAD)
275 if (DECL_P (t) && DECL_CONTEXT (t))
276 pp_cxx_qualified_id (pp, t);
278 pp_cxx_unqualified_id (pp, t);
281 /* primary-expression:
285 :: operator-function-id
290 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
292 switch (TREE_CODE (t))
297 pp_c_constant (pp_c_base (pp), t);
301 t = BASELINK_FUNCTIONS (t);
309 pp_cxx_id_expression (pp, t);
313 case TEMPLATE_TYPE_PARM:
314 case TEMPLATE_PARM_INDEX:
315 pp_cxx_unqualified_id (pp, t);
319 pp_c_primary_expression (pp_c_base (pp), t);
324 /* postfix-expression:
326 postfix-expression [ expression ]
327 postfix-expression ( expression-list(opt) )
328 simple-type-specifier ( expression-list(opt) )
329 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
330 typename ::(opt) nested-name-specifier template(opt)
331 template-id ( expression-list(opt) )
332 postfix-expression . template(opt) ::(opt) id-expression
333 postfix-expression -> template(opt) ::(opt) id-expression
334 postfix-expression . pseudo-destructor-name
335 postfix-expression -> pseudo-destructor-name
336 postfix-expression ++
337 postfix-expression --
338 dynamic_cast < type-id > ( expression )
339 static_cast < type-id > ( expression )
340 reinterpret_cast < type-id > ( expression )
341 const_cast < type-id > ( expression )
342 typeid ( expression )
343 typeif ( type-id ) */
346 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
348 enum tree_code code = TREE_CODE (t);
355 tree fun = TREE_OPERAND (t, 0);
356 tree args = TREE_OPERAND (t, 1);
357 tree saved_scope = pp->enclosing_scope;
359 if (TREE_CODE (fun) == ADDR_EXPR)
360 fun = TREE_OPERAND (fun, 0);
362 /* In templates, where there is no way to tell whether a given
363 call uses an actual member function. So the parser builds
364 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
365 instantiation time. */
366 if (TREE_CODE (fun) != FUNCTION_DECL)
368 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
370 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
371 ? TREE_OPERAND (t, 2)
374 while (TREE_CODE (object) == NOP_EXPR)
375 object = TREE_OPERAND (object, 0);
377 if (TREE_CODE (object) == ADDR_EXPR)
378 object = TREE_OPERAND (object, 0);
380 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
382 pp_cxx_postfix_expression (pp, object);
387 pp_cxx_postfix_expression (pp, object);
390 args = TREE_CHAIN (args);
391 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
394 pp_cxx_postfix_expression (pp, fun);
395 pp->enclosing_scope = saved_scope;
396 pp_cxx_call_argument_list (pp, args);
398 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
400 pp_separate_with (pp, ',');
401 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
414 pp_cxx_primary_expression (pp, t);
417 case DYNAMIC_CAST_EXPR:
418 case STATIC_CAST_EXPR:
419 case REINTERPRET_CAST_EXPR:
420 case CONST_CAST_EXPR:
421 if (code == DYNAMIC_CAST_EXPR)
422 pp_identifier (pp, "dynamic_cast");
423 else if (code == STATIC_CAST_EXPR)
424 pp_identifier (pp, "static_cast");
425 else if (code == REINTERPRET_CAST_EXPR)
426 pp_identifier (pp, "reinterpret_cast");
428 pp_identifier (pp, "const_cast");
429 pp_cxx_begin_template_argument_list (pp);
430 pp_cxx_type_id (pp, TREE_TYPE (t));
431 pp_cxx_end_template_argument_list (pp);
433 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
437 case EMPTY_CLASS_EXPR:
438 pp_cxx_type_id (pp, TREE_TYPE (t));
444 t = TREE_OPERAND (t, 0);
445 pp_cxx_identifier (pp, "typeid");
448 pp_cxx_type_id (pp, t);
450 pp_cxx_expression (pp, t);
454 case PSEUDO_DTOR_EXPR:
455 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
457 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
458 pp_cxx_colon_colon (pp);
460 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
464 pp_c_postfix_expression (pp_c_base (pp), t);
470 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
471 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
477 type-specifier-seq new-declarator(opt)
480 ptr-operator new-declarator(opt)
481 direct-new-declarator
483 direct-new-declarator
485 direct-new-declarator [ constant-expression ]
488 ( expression-list(opt) ) */
490 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
492 enum tree_code code = TREE_CODE (t);
497 if (NEW_EXPR_USE_GLOBAL (t))
498 pp_cxx_colon_colon (pp);
499 pp_cxx_identifier (pp, "new");
500 if (TREE_OPERAND (t, 0))
502 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
505 /* FIXME: array-types are built with one more element. */
506 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
507 if (TREE_OPERAND (t, 2))
510 t = TREE_OPERAND (t, 2);
511 if (TREE_CODE (t) == TREE_LIST)
512 pp_c_expression_list (pp_c_base (pp), t);
513 else if (t == void_zero_node)
514 ; /* OK, empty initializer list. */
516 pp_cxx_expression (pp, t);
522 pp_unsupported_tree (pp, t);
526 /* delete-expression:
527 ::(opt) delete cast-expression
528 ::(opt) delete [ ] cast-expression */
530 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
532 enum tree_code code = TREE_CODE (t);
536 case VEC_DELETE_EXPR:
537 if (DELETE_EXPR_USE_GLOBAL (t))
538 pp_cxx_colon_colon (pp);
539 pp_cxx_identifier (pp, "delete");
540 if (code == VEC_DELETE_EXPR)
542 pp_left_bracket (pp);
543 pp_right_bracket (pp);
545 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
549 pp_unsupported_tree (pp, t);
557 unary-operator cast-expression
558 sizeof unary-expression
563 unary-operator: one of
567 __alignof__ unary-expression
568 __alignof__ ( type-id ) */
570 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
572 enum tree_code code = TREE_CODE (t);
577 pp_cxx_new_expression (pp, t);
581 case VEC_DELETE_EXPR:
582 pp_cxx_delete_expression (pp, t);
586 pp_c_unary_expression (pp_c_base (pp), t);
593 ( type-id ) cast-expression */
595 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
597 switch (TREE_CODE (t))
600 pp_cxx_type_id (pp, TREE_TYPE (t));
601 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
605 pp_c_cast_expression (pp_c_base (pp), t);
612 pm-expression .* cast-expression
613 pm-expression ->* cast-expression */
615 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
617 switch (TREE_CODE (t))
619 /* Handle unfortunate OFFESET_REF overloading here. */
621 if (TYPE_P (TREE_OPERAND (t, 0)))
623 pp_cxx_qualified_id (pp, t);
626 /* Else fall through. */
629 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
632 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
637 pp_cxx_cast_expression (pp, t);
642 /* multiplicative-expression:
644 multiplicative-expression * pm-expression
645 multiplicative-expression / pm-expression
646 multiplicative-expression % pm-expression */
648 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
650 enum tree_code code = TREE_CODE (e);
656 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
658 if (code == MULT_EXPR)
660 else if (code == TRUNC_DIV_EXPR)
665 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
669 pp_cxx_pm_expression (pp, e);
674 /* conditional-expression:
675 logical-or-expression
676 logical-or-expression ? expression : assignment-expression */
678 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
680 if (TREE_CODE (e) == COND_EXPR)
682 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
686 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
688 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
691 pp_c_logical_or_expression (pp_c_base (pp), e);
695 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
699 switch (TREE_CODE (t))
722 op = tree_code_name[TREE_CODE (t)];
726 pp_cxx_identifier (pp, op);
730 /* assignment-expression:
731 conditional-expression
732 logical-or-expression assignment-operator assignment-expression
736 throw assignment-expression(opt)
738 assignment-operator: one of
739 = *= /= %= += -= >>= <<= &= ^= |= */
741 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
743 switch (TREE_CODE (e))
747 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
751 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
755 pp_cxx_identifier (pp, "throw");
756 if (TREE_OPERAND (e, 0))
757 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
761 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
762 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
763 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
767 pp_cxx_conditional_expression (pp, e);
773 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
775 switch (TREE_CODE (t))
780 pp_c_constant (pp_c_base (pp), t);
784 pp_cxx_unqualified_id (pp, t);
792 pp_cxx_qualified_id (pp, t);
804 case TEMPLATE_TYPE_PARM:
805 case TEMPLATE_PARM_INDEX:
806 pp_cxx_primary_expression (pp, t);
810 case DYNAMIC_CAST_EXPR:
811 case STATIC_CAST_EXPR:
812 case REINTERPRET_CAST_EXPR:
813 case CONST_CAST_EXPR:
817 case EMPTY_CLASS_EXPR:
819 case PSEUDO_DTOR_EXPR:
821 pp_cxx_postfix_expression (pp, t);
826 pp_cxx_new_expression (pp, t);
830 case VEC_DELETE_EXPR:
831 pp_cxx_delete_expression (pp, t);
835 pp_cxx_cast_expression (pp, t);
841 pp_cxx_pm_expression (pp, t);
847 pp_cxx_multiplicative_expression (pp, t);
851 pp_cxx_conditional_expression (pp, t);
858 pp_cxx_assignment_expression (pp, t);
862 pp_c_expression (pp_c_base (pp), t);
870 /* function-specifier:
875 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
877 switch (TREE_CODE (t))
880 if (DECL_VIRTUAL_P (t))
881 pp_cxx_identifier (pp, "virtual");
882 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
883 pp_cxx_identifier (pp, "explicit");
885 pp_c_function_specifier (pp_c_base (pp), t);
892 /* decl-specifier-seq:
893 decl-specifier-seq(opt) decl-specifier
896 storage-class-specifier
902 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
904 switch (TREE_CODE (t))
910 pp_cxx_storage_class_specifier (pp, t);
911 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
915 pp_cxx_identifier (pp, "typedef");
916 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
920 if (TYPE_PTRMEMFUNC_P (t))
922 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
923 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
924 pp_cxx_whitespace (pp);
925 pp_cxx_ptr_operator (pp, t);
930 /* Constructors don't have return types. And conversion functions
931 do not have a type-specifier in their return types. */
932 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
933 pp_cxx_function_specifier (pp, t);
934 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
935 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
938 pp_c_declaration_specifiers (pp_c_base (pp), t);
943 /* simple-type-specifier:
944 ::(opt) nested-name-specifier(opt) type-name
945 ::(opt) nested-name-specifier(opt) template(opt) template-id
958 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
960 switch (TREE_CODE (t))
965 pp_cxx_qualified_id (pp, t);
968 case TEMPLATE_TYPE_PARM:
969 case TEMPLATE_PARM_INDEX:
970 pp_cxx_unqualified_id (pp, t);
974 pp_cxx_identifier (pp, "typename");
975 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
976 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
980 pp_c_type_specifier (pp_c_base (pp), t);
985 /* type-specifier-seq:
986 type-specifier type-specifier-seq(opt)
989 simple-type-specifier
992 elaborated-type-specifier
996 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
998 switch (TREE_CODE (t))
1001 case TEMPLATE_TYPE_PARM:
1003 case BOUND_TEMPLATE_TEMPLATE_PARM:
1004 pp_c_type_qualifier_list (pp_c_base (pp), t);
1005 pp_cxx_simple_type_specifier (pp, t);
1009 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1010 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1011 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1015 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1016 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1021 * cv-qualifier-seq(opt)
1023 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1026 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1028 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1030 switch (TREE_CODE (t))
1032 case REFERENCE_TYPE:
1034 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1035 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1036 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1037 if (TREE_CODE (t) == POINTER_TYPE)
1040 pp_cxx_cv_qualifier_seq (pp, t);
1047 if (TYPE_PTRMEMFUNC_P (t))
1049 pp_cxx_left_paren (pp);
1050 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1055 if (TYPE_PTR_TO_MEMBER_P (t))
1057 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1059 pp_cxx_cv_qualifier_seq (pp, t);
1062 /* else fall through. */
1065 pp_unsupported_tree (pp, t);
1071 pp_cxx_implicit_parameter_type (tree mf)
1073 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1077 parameter-declaration:
1078 decl-specifier-seq declarator
1079 decl-specifier-seq declarator = assignment-expression
1080 decl-specifier-seq abstract-declarator(opt)
1081 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1083 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1085 pp_cxx_decl_specifier_seq (pp, t);
1087 pp_cxx_abstract_declarator (pp, t);
1089 pp_cxx_declarator (pp, t);
1092 /* parameter-declaration-clause:
1093 parameter-declaration-list(opt) ...(opt)
1094 parameter-declaration-list , ...
1096 parameter-declaration-list:
1097 parameter-declaration
1098 parameter-declaration-list , parameter-declaration */
1100 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1102 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1103 tree types = TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1104 const bool abstract = args == NULL
1105 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1108 /* Skip artificial parameter for nonstatic member functions. */
1109 if (TREE_CODE (t) == METHOD_TYPE)
1110 types = TREE_CHAIN (types);
1112 pp_cxx_left_paren (pp);
1113 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1116 pp_separate_with (pp, ',');
1118 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1119 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1121 pp_cxx_whitespace (pp);
1123 pp_cxx_whitespace (pp);
1124 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1127 pp_cxx_right_paren (pp);
1130 /* exception-specification:
1131 throw ( type-id-list(opt) )
1135 type-id-list , type-id */
1137 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1139 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1141 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1143 pp_cxx_identifier (pp, "throw");
1144 pp_cxx_left_paren (pp);
1145 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1147 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1148 if (TREE_CHAIN (ex_spec))
1149 pp_separate_with (pp, ',');
1151 pp_cxx_right_paren (pp);
1154 /* direct-declarator:
1156 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1157 exception-specification(opt)
1158 direct-declaration [ constant-expression(opt) ]
1161 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1163 switch (TREE_CODE (t))
1171 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1172 pp_cxx_id_expression (pp, DECL_NAME (t));
1174 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1178 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1179 pp_cxx_id_expression (pp, t);
1180 pp_cxx_parameter_declaration_clause (pp, t);
1182 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1184 pp_base (pp)->padding = pp_before;
1185 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1188 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1193 case TEMPLATE_TYPE_PARM:
1194 case TEMPLATE_PARM_INDEX:
1198 pp_c_direct_declarator (pp_c_base (pp), t);
1205 ptr-operator declarator */
1207 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1209 pp_cxx_direct_declarator (pp, t);
1212 /* ctor-initializer:
1213 : mem-initializer-list
1215 mem-initializer-list:
1217 mem-initializer , mem-initializer-list
1220 mem-initializer-id ( expression-list(opt) )
1223 ::(opt) nested-name-specifier(opt) class-name
1226 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1228 t = TREE_OPERAND (t, 0);
1229 pp_cxx_whitespace (pp);
1231 pp_cxx_whitespace (pp);
1232 for (; t; t = TREE_CHAIN (t))
1234 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1235 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1237 pp_separate_with (pp, ',');
1241 /* function-definition:
1242 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1243 decl-specifier-seq(opt) declarator function-try-block */
1246 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1248 tree saved_scope = pp->enclosing_scope;
1249 pp_cxx_decl_specifier_seq (pp, t);
1250 pp_cxx_declarator (pp, t);
1251 pp_needs_newline (pp) = true;
1252 pp->enclosing_scope = DECL_CONTEXT (t);
1253 if (DECL_SAVED_TREE (t))
1255 tree body = DECL_SAVED_TREE (t);
1256 if (TREE_CODE (body) == COMPOUND_STMT
1257 && TREE_CODE (COMPOUND_BODY (body)) == CTOR_INITIALIZER)
1259 body = COMPOUND_BODY (body);
1260 pp_cxx_ctor_initializer (pp, body);
1261 body = TREE_CHAIN (body);
1263 pp_cxx_statement (pp, body);
1267 pp_cxx_semicolon (pp);
1268 pp_needs_newline (pp) = true;
1271 pp->enclosing_scope = saved_scope;
1274 /* abstract-declarator:
1275 ptr-operator abstract-declarator(opt)
1276 direct-abstract-declarator */
1278 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1280 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1281 pp_cxx_right_paren (pp);
1282 else if (POINTER_TYPE_P (t))
1284 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1285 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1286 pp_cxx_right_paren (pp);
1289 pp_cxx_direct_abstract_declarator (pp, t);
1292 /* direct-abstract-declarator:
1293 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1294 cv-qualifier-seq(opt) exception-specification(opt)
1295 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1296 ( abstract-declarator ) */
1298 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1300 switch (TREE_CODE (t))
1302 case REFERENCE_TYPE:
1303 pp_cxx_abstract_declarator (pp, t);
1307 if (TYPE_PTRMEMFUNC_P (t))
1308 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1313 pp_cxx_parameter_declaration_clause (pp, t);
1314 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1315 if (TREE_CODE (t) == METHOD_TYPE)
1317 pp_base (pp)->padding = pp_before;
1318 pp_cxx_cv_qualifier_seq
1319 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1321 pp_cxx_exception_specification (pp, t);
1325 case TEMPLATE_TYPE_PARM:
1326 case TEMPLATE_TEMPLATE_PARM:
1327 case BOUND_TEMPLATE_TEMPLATE_PARM:
1328 case UNBOUND_CLASS_TEMPLATE:
1332 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1338 type-specifier-seq abstract-declarator(opt) */
1340 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1342 pp_flags saved_flags = pp_c_base (pp)->flags;
1343 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1345 switch (TREE_CODE (t))
1352 case BOUND_TEMPLATE_TEMPLATE_PARM:
1353 case UNBOUND_CLASS_TEMPLATE:
1354 case TEMPLATE_TEMPLATE_PARM:
1355 case TEMPLATE_TYPE_PARM:
1356 case TEMPLATE_PARM_INDEX:
1359 case TEMPLATE_ID_EXPR:
1360 /* FIXME: Should be pp_cxx_type_specifier_seq. */
1361 pp_cxx_type_specifier_seq (pp, t);
1362 pp_cxx_declarator (pp, t);
1366 pp_c_type_id (pp_c_base (pp), t);
1370 pp_c_base (pp)->flags = saved_flags;
1373 /* template-argument-list:
1375 template-argument-list, template-argument
1378 assignment-expression
1382 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1387 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1389 tree arg = TREE_VEC_ELT (t, i);
1391 pp_separate_with (pp, ',');
1392 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1393 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1394 pp_cxx_type_id (pp, arg);
1396 pp_cxx_expression (pp, arg);
1402 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1404 t = DECL_STMT_DECL (t);
1405 pp_cxx_type_specifier_seq (pp, t);
1407 pp_cxx_abstract_declarator (pp, t);
1409 pp_cxx_declarator (pp, t);
1415 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1417 switch (TREE_CODE (t))
1420 pp_cxx_identifier (pp, "using");
1421 pp_cxx_identifier (pp, "namespace");
1422 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1426 pp_cxx_identifier (pp, "using");
1427 pp_cxx_nested_name_specifier (pp, DECL_INITIAL (t));
1428 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1435 try compound-statement handler-seq */
1437 pp_maybe_newline_and_indent (pp, 0);
1438 pp_cxx_identifier (pp, "try");
1439 pp_newline_and_indent (pp, 3);
1440 pp_cxx_statement (pp, TRY_STMTS (t));
1441 pp_newline_and_indent (pp, -3);
1445 pp_cxx_statement (pp, TRY_HANDLERS (t));
1450 handler handler-seq(opt)
1453 catch ( exception-declaration ) compound-statement
1455 exception-declaration:
1456 type-specifier-seq declarator
1457 type-specifier-seq abstract-declarator
1460 pp_cxx_identifier (pp, "catch");
1461 pp_cxx_left_paren (pp);
1462 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1463 pp_cxx_right_paren (pp);
1464 pp_indentation (pp) += 3;
1465 pp_needs_newline (pp) = true;
1466 pp_cxx_statement (pp, HANDLER_BODY (t));
1467 pp_indentation (pp) -= 3;
1468 pp_needs_newline (pp) = true;
1472 pp_c_statement (pp_c_base (pp), t);
1477 /* original-namespace-definition:
1478 namespace identifier { namespace-body }
1480 As an edge case, we also handle unnamed namespace definition here. */
1483 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1485 pp_cxx_identifier (pp, "namespace");
1487 pp_cxx_unqualified_id (pp, t);
1488 pp_cxx_whitespace (pp);
1489 pp_cxx_left_brace (pp);
1490 /* We do not print the namespace-body. */
1491 pp_cxx_whitespace (pp);
1492 pp_cxx_right_brace (pp);
1498 namespace-alias-definition:
1499 namespace identifier = qualified-namespace-specifier ;
1501 qualified-namespace-specifier:
1502 ::(opt) nested-name-specifier(opt) namespace-name */
1505 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1507 pp_cxx_identifier (pp, "namespace");
1508 pp_cxx_unqualified_id (pp, t);
1509 pp_cxx_whitespace (pp);
1511 pp_cxx_whitespace (pp);
1512 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1513 pp_cxx_semicolon (pp);
1516 /* simple-declaration:
1517 decl-specifier-seq(opt) init-declarator-list(opt) */
1519 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1521 pp_cxx_decl_specifier_seq (pp, t);
1522 pp_cxx_init_declarator (pp, t);
1523 pp_cxx_semicolon (pp);
1524 pp_needs_newline (pp) = true;
1528 template-parameter-list:
1530 template-parameter-list , template-parameter */
1533 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1535 const int n = TREE_VEC_LENGTH (t);
1537 for (i = 0; i < n; ++i)
1540 pp_separate_with (pp, ',');
1541 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1545 /* template-parameter:
1547 parameter-declaration
1550 class identifier(opt)
1551 class identifier(op) = type-id
1552 typename identifier(opt)
1553 typename identifier(opt) = type-id
1554 template < template-parameter-list > class identifier(opt)
1555 template < template-parameter-list > class identifier(opt) = template-name
1558 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1560 tree parameter = TREE_VALUE (t);
1561 switch (TREE_CODE (parameter))
1564 pp_cxx_identifier (pp, "class");
1565 if (DECL_NAME (parameter))
1566 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1567 /* FIXME: Chech if we should print also default argument. */
1571 pp_cxx_parameter_declaration (pp, parameter);
1578 pp_unsupported_tree (pp, t);
1583 /* Pretty-print a template parameter in the canonical form
1584 "template-parameter-<level>-<position in parameter list>". */
1587 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1589 const enum tree_code code = TREE_CODE (parm);
1591 /* Brings type template parameters to the canonical forms. */
1592 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1593 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1594 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1596 pp_cxx_begin_template_argument_list (pp);
1597 pp_cxx_identifier (pp, "template-parameter-");
1598 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1600 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1601 pp_cxx_end_template_argument_list (pp);
1605 template-declaration:
1606 export(opt) template < template-parameter-list > declaration */
1608 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1610 tree tmpl = most_general_template (t);
1614 pp_maybe_newline_and_indent (pp, 0);
1615 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1617 pp_cxx_identifier (pp, "template");
1618 pp_cxx_begin_template_argument_list (pp);
1619 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1620 pp_cxx_end_template_argument_list (pp);
1621 pp_newline_and_indent (pp, 3);
1624 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1625 pp_cxx_function_definition (pp, t);
1627 pp_cxx_simple_declaration (pp, t);
1631 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1633 pp_unsupported_tree (pp, t);
1637 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1639 pp_unsupported_tree (pp, t);
1646 template-declaration
1647 explicit-instantiation
1648 explicit-specialization
1649 linkage-specification
1650 namespace-definition
1655 namespace-alias-definition
1659 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1661 if (!DECL_LANG_SPECIFIC (t))
1662 pp_cxx_simple_declaration (pp, t);
1663 else if (DECL_USE_TEMPLATE (t))
1664 switch (DECL_USE_TEMPLATE (t))
1667 pp_cxx_template_declaration (pp, t);
1671 pp_cxx_explicit_specialization (pp, t);
1675 pp_cxx_explicit_instantiation (pp, t);
1681 else switch (TREE_CODE (t))
1685 pp_cxx_simple_declaration (pp, t);
1689 if (DECL_SAVED_TREE (t))
1690 pp_cxx_function_definition (pp, t);
1692 pp_cxx_simple_declaration (pp, t);
1695 case NAMESPACE_DECL:
1696 if (DECL_NAMESPACE_ALIAS (t))
1697 pp_cxx_namespace_alias_definition (pp, t);
1699 pp_cxx_original_namespace_definition (pp, t);
1703 pp_unsupported_tree (pp, t);
1709 typedef c_pretty_print_fn pp_fun;
1712 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1714 pp_c_pretty_printer_init (pp_c_base (pp));
1715 pp_set_line_maximum_length (pp, 0);
1717 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1718 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1719 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1720 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1721 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1722 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1723 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1724 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1725 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1726 pp->c_base.direct_abstract_declarator =
1727 (pp_fun) pp_cxx_direct_abstract_declarator;
1728 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1730 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1732 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1733 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1734 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1735 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1736 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1737 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1738 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1739 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1740 pp->enclosing_scope = global_namespace;