1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003, 2004, 2005 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_statement (cxx_pretty_printer *, tree);
45 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
49 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
51 const char *p = pp_last_position_in_text (pp);
53 if (p != NULL && *p == c)
54 pp_cxx_whitespace (pp);
56 pp_base (pp)->padding = pp_none;
59 #define pp_cxx_storage_class_specifier(PP, T) \
60 pp_c_storage_class_specifier (pp_c_base (PP), T)
61 #define pp_cxx_expression_list(PP, T) \
62 pp_c_expression_list (pp_c_base (PP), T)
63 #define pp_cxx_space_for_pointer_operator(PP, T) \
64 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
65 #define pp_cxx_init_declarator(PP, T) \
66 pp_c_init_declarator (pp_c_base (PP), T)
67 #define pp_cxx_call_argument_list(PP, T) \
68 pp_c_call_argument_list (pp_c_base (PP), T)
71 pp_cxx_colon_colon (cxx_pretty_printer *pp)
74 pp_base (pp)->padding = pp_none;
78 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
80 pp_cxx_nonconsecutive_character (pp, '<');
84 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
86 pp_cxx_nonconsecutive_character (pp, '>');
90 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
92 pp_separate_with (pp, c);
93 pp_base (pp)->padding = pp_none;
99 is_destructor_name (tree name)
101 return name == complete_dtor_identifier
102 || name == base_dtor_identifier
103 || name == deleting_dtor_identifier;
106 /* conversion-function-id:
107 operator conversion-type-id
110 type-specifier-seq conversion-declarator(opt)
112 conversion-declarator:
113 ptr-operator conversion-declarator(opt) */
116 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
118 pp_cxx_identifier (pp, "operator");
119 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
123 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
125 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
126 pp_cxx_begin_template_argument_list (pp);
127 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
128 pp_cxx_end_template_argument_list (pp);
134 conversion-function-id
139 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
141 enum tree_code code = TREE_CODE (t);
145 pp_cxx_identifier (pp, "<return-value>");
162 case IDENTIFIER_NODE:
164 pp_cxx_identifier (pp, "<unnamed>");
165 else if (IDENTIFIER_TYPENAME_P (t))
166 pp_cxx_conversion_function_id (pp, t);
169 if (is_destructor_name (t))
172 /* FIXME: Why is this necessary? */
174 t = constructor_name (TREE_TYPE (t));
176 pp_cxx_tree_identifier (pp, t);
180 case TEMPLATE_ID_EXPR:
181 pp_cxx_template_id (pp, t);
185 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
191 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
194 case TEMPLATE_TYPE_PARM:
195 case TEMPLATE_TEMPLATE_PARM:
196 if (TYPE_IDENTIFIER (t))
197 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
199 pp_cxx_canonical_template_parameter (pp, t);
202 case TEMPLATE_PARM_INDEX:
203 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
207 pp_unsupported_tree (pp, t);
212 /* Pretty-print out the token sequence ":: template" in template codes
213 where it is needed to "inline declare" the (following) member as
214 a template. This situation arises when SCOPE of T is dependent
215 on template parameters. */
218 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
220 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
221 && TYPE_P (scope) && dependent_type_p (scope))
222 pp_cxx_identifier (pp, "template");
225 /* nested-name-specifier:
226 class-or-namespace-name :: nested-name-specifier(opt)
227 class-or-namespace-name :: template nested-name-specifier */
230 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
232 if (t != NULL && t != pp->enclosing_scope)
234 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
235 pp_cxx_nested_name_specifier (pp, scope);
236 pp_cxx_template_keyword_if_needed (pp, scope, t);
237 pp_cxx_unqualified_id (pp, t);
238 pp_cxx_colon_colon (pp);
243 nested-name-specifier template(opt) unqualified-id */
246 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
248 switch (TREE_CODE (t))
250 /* A pointer-to-member is always qualified. */
252 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
253 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
256 /* In Standard C++, functions cannot possibly be used as
257 nested-name-specifiers. However, there are situations where
258 is "makes sense" to output the surrounding function name for the
259 purpose of emphasizing on the scope kind. Just printing the
260 function name might not be sufficient as it may be overloaded; so,
261 we decorate the function with its signature too.
262 FIXME: This is probably the wrong pretty-printing for conversion
263 functions and some function templates. */
267 if (DECL_FUNCTION_MEMBER_P (t))
268 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
269 pp_cxx_unqualified_id
270 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
271 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
276 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
277 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
282 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
283 if (scope != pp->enclosing_scope)
285 pp_cxx_nested_name_specifier (pp, scope);
286 pp_cxx_template_keyword_if_needed (pp, scope, t);
288 pp_cxx_unqualified_id (pp, t);
299 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
301 if (TREE_CODE (t) == OVERLOAD)
303 if (DECL_P (t) && DECL_CONTEXT (t))
304 pp_cxx_qualified_id (pp, t);
306 pp_cxx_unqualified_id (pp, t);
309 /* primary-expression:
313 :: operator-function-id
319 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
321 switch (TREE_CODE (t))
326 pp_c_constant (pp_c_base (pp), t);
330 t = BASELINK_FUNCTIONS (t);
338 pp_cxx_id_expression (pp, t);
342 case TEMPLATE_TYPE_PARM:
343 case TEMPLATE_TEMPLATE_PARM:
344 case TEMPLATE_PARM_INDEX:
345 pp_cxx_unqualified_id (pp, t);
349 pp_c_primary_expression (pp_c_base (pp), t);
354 /* postfix-expression:
356 postfix-expression [ expression ]
357 postfix-expression ( expression-list(opt) )
358 simple-type-specifier ( expression-list(opt) )
359 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
360 typename ::(opt) nested-name-specifier template(opt)
361 template-id ( expression-list(opt) )
362 postfix-expression . template(opt) ::(opt) id-expression
363 postfix-expression -> template(opt) ::(opt) id-expression
364 postfix-expression . pseudo-destructor-name
365 postfix-expression -> pseudo-destructor-name
366 postfix-expression ++
367 postfix-expression --
368 dynamic_cast < type-id > ( expression )
369 static_cast < type-id > ( expression )
370 reinterpret_cast < type-id > ( expression )
371 const_cast < type-id > ( expression )
372 typeid ( expression )
373 typeif ( type-id ) */
376 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
378 enum tree_code code = TREE_CODE (t);
385 tree fun = TREE_OPERAND (t, 0);
386 tree args = TREE_OPERAND (t, 1);
387 tree saved_scope = pp->enclosing_scope;
389 if (TREE_CODE (fun) == ADDR_EXPR)
390 fun = TREE_OPERAND (fun, 0);
392 /* In templates, where there is no way to tell whether a given
393 call uses an actual member function. So the parser builds
394 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
395 instantiation time. */
396 if (TREE_CODE (fun) != FUNCTION_DECL)
398 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
400 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
401 ? TREE_OPERAND (t, 2)
404 while (TREE_CODE (object) == NOP_EXPR)
405 object = TREE_OPERAND (object, 0);
407 if (TREE_CODE (object) == ADDR_EXPR)
408 object = TREE_OPERAND (object, 0);
410 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
412 pp_cxx_postfix_expression (pp, object);
417 pp_cxx_postfix_expression (pp, object);
420 args = TREE_CHAIN (args);
421 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
424 pp_cxx_postfix_expression (pp, fun);
425 pp->enclosing_scope = saved_scope;
426 pp_cxx_call_argument_list (pp, args);
428 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
430 pp_cxx_separate_with (pp, ',');
431 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
444 pp_cxx_primary_expression (pp, t);
447 case DYNAMIC_CAST_EXPR:
448 case STATIC_CAST_EXPR:
449 case REINTERPRET_CAST_EXPR:
450 case CONST_CAST_EXPR:
451 if (code == DYNAMIC_CAST_EXPR)
452 pp_cxx_identifier (pp, "dynamic_cast");
453 else if (code == STATIC_CAST_EXPR)
454 pp_cxx_identifier (pp, "static_cast");
455 else if (code == REINTERPRET_CAST_EXPR)
456 pp_cxx_identifier (pp, "reinterpret_cast");
458 pp_cxx_identifier (pp, "const_cast");
459 pp_cxx_begin_template_argument_list (pp);
460 pp_cxx_type_id (pp, TREE_TYPE (t));
461 pp_cxx_end_template_argument_list (pp);
463 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
467 case EMPTY_CLASS_EXPR:
468 pp_cxx_type_id (pp, TREE_TYPE (t));
474 t = TREE_OPERAND (t, 0);
475 pp_cxx_identifier (pp, "typeid");
478 pp_cxx_type_id (pp, t);
480 pp_cxx_expression (pp, t);
484 case PSEUDO_DTOR_EXPR:
485 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
487 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
488 pp_cxx_colon_colon (pp);
490 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
494 pp_c_postfix_expression (pp_c_base (pp), t);
500 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
501 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
507 type-specifier-seq new-declarator(opt)
510 ptr-operator new-declarator(opt)
511 direct-new-declarator
513 direct-new-declarator
515 direct-new-declarator [ constant-expression ]
518 ( expression-list(opt) ) */
521 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
523 enum tree_code code = TREE_CODE (t);
528 if (NEW_EXPR_USE_GLOBAL (t))
529 pp_cxx_colon_colon (pp);
530 pp_cxx_identifier (pp, "new");
531 if (TREE_OPERAND (t, 0))
533 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
536 /* FIXME: array-types are built with one more element. */
537 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
538 if (TREE_OPERAND (t, 2))
541 t = TREE_OPERAND (t, 2);
542 if (TREE_CODE (t) == TREE_LIST)
543 pp_c_expression_list (pp_c_base (pp), t);
544 else if (t == void_zero_node)
545 ; /* OK, empty initializer list. */
547 pp_cxx_expression (pp, t);
553 pp_unsupported_tree (pp, t);
557 /* delete-expression:
558 ::(opt) delete cast-expression
559 ::(opt) delete [ ] cast-expression */
562 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
564 enum tree_code code = TREE_CODE (t);
568 case VEC_DELETE_EXPR:
569 if (DELETE_EXPR_USE_GLOBAL (t))
570 pp_cxx_colon_colon (pp);
571 pp_cxx_identifier (pp, "delete");
572 if (code == VEC_DELETE_EXPR)
574 pp_left_bracket (pp);
575 pp_right_bracket (pp);
577 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
581 pp_unsupported_tree (pp, t);
589 unary-operator cast-expression
590 sizeof unary-expression
595 unary-operator: one of
599 __alignof__ unary-expression
600 __alignof__ ( type-id ) */
603 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
605 enum tree_code code = TREE_CODE (t);
610 pp_cxx_new_expression (pp, t);
614 case VEC_DELETE_EXPR:
615 pp_cxx_delete_expression (pp, t);
619 pp_c_unary_expression (pp_c_base (pp), t);
626 ( type-id ) cast-expression */
629 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
631 switch (TREE_CODE (t))
634 pp_cxx_type_id (pp, TREE_TYPE (t));
635 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
639 pp_c_cast_expression (pp_c_base (pp), t);
646 pm-expression .* cast-expression
647 pm-expression ->* cast-expression */
650 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
652 switch (TREE_CODE (t))
654 /* Handle unfortunate OFFESET_REF overloading here. */
656 if (TYPE_P (TREE_OPERAND (t, 0)))
658 pp_cxx_qualified_id (pp, t);
661 /* Else fall through. */
664 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
667 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
672 pp_cxx_cast_expression (pp, t);
677 /* multiplicative-expression:
679 multiplicative-expression * pm-expression
680 multiplicative-expression / pm-expression
681 multiplicative-expression % pm-expression */
684 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
686 enum tree_code code = TREE_CODE (e);
692 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
694 if (code == MULT_EXPR)
696 else if (code == TRUNC_DIV_EXPR)
701 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
705 pp_cxx_pm_expression (pp, e);
710 /* conditional-expression:
711 logical-or-expression
712 logical-or-expression ? expression : assignment-expression */
715 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
717 if (TREE_CODE (e) == COND_EXPR)
719 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
723 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
725 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
728 pp_c_logical_or_expression (pp_c_base (pp), e);
731 /* Pretty-print a compound assignment operator token as indicated by T. */
734 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
738 switch (TREE_CODE (t))
761 op = tree_code_name[TREE_CODE (t)];
765 pp_cxx_identifier (pp, op);
769 /* assignment-expression:
770 conditional-expression
771 logical-or-expression assignment-operator assignment-expression
775 throw assignment-expression(opt)
777 assignment-operator: one of
778 = *= /= %= += -= >>= <<= &= ^= |= */
781 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
783 switch (TREE_CODE (e))
787 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
791 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
795 pp_cxx_identifier (pp, "throw");
796 if (TREE_OPERAND (e, 0))
797 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
801 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
802 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
803 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
807 pp_cxx_conditional_expression (pp, e);
813 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
815 switch (TREE_CODE (t))
820 pp_c_constant (pp_c_base (pp), t);
824 pp_cxx_unqualified_id (pp, t);
832 pp_cxx_qualified_id (pp, t);
844 case TEMPLATE_TYPE_PARM:
845 case TEMPLATE_PARM_INDEX:
846 case TEMPLATE_TEMPLATE_PARM:
847 pp_cxx_primary_expression (pp, t);
851 case DYNAMIC_CAST_EXPR:
852 case STATIC_CAST_EXPR:
853 case REINTERPRET_CAST_EXPR:
854 case CONST_CAST_EXPR:
858 case EMPTY_CLASS_EXPR:
860 case PSEUDO_DTOR_EXPR:
862 pp_cxx_postfix_expression (pp, t);
867 pp_cxx_new_expression (pp, t);
871 case VEC_DELETE_EXPR:
872 pp_cxx_delete_expression (pp, t);
876 pp_cxx_cast_expression (pp, t);
882 pp_cxx_pm_expression (pp, t);
888 pp_cxx_multiplicative_expression (pp, t);
892 pp_cxx_conditional_expression (pp, t);
899 pp_cxx_assignment_expression (pp, t);
902 case NON_DEPENDENT_EXPR:
903 case MUST_NOT_THROW_EXPR:
904 pp_cxx_expression (pp, t);
908 pp_c_expression (pp_c_base (pp), t);
916 /* function-specifier:
922 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
924 switch (TREE_CODE (t))
927 if (DECL_VIRTUAL_P (t))
928 pp_cxx_identifier (pp, "virtual");
929 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
930 pp_cxx_identifier (pp, "explicit");
932 pp_c_function_specifier (pp_c_base (pp), t);
939 /* decl-specifier-seq:
940 decl-specifier-seq(opt) decl-specifier
943 storage-class-specifier
950 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
952 switch (TREE_CODE (t))
958 pp_cxx_storage_class_specifier (pp, t);
959 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
963 pp_cxx_identifier (pp, "typedef");
964 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
968 if (TYPE_PTRMEMFUNC_P (t))
970 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
971 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
972 pp_cxx_whitespace (pp);
973 pp_cxx_ptr_operator (pp, t);
978 /* Constructors don't have return types. And conversion functions
979 do not have a type-specifier in their return types. */
980 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
981 pp_cxx_function_specifier (pp, t);
982 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
983 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
986 pp_c_declaration_specifiers (pp_c_base (pp), t);
991 /* simple-type-specifier:
992 ::(opt) nested-name-specifier(opt) type-name
993 ::(opt) nested-name-specifier(opt) template(opt) template-id
1007 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1009 switch (TREE_CODE (t))
1014 pp_cxx_qualified_id (pp, t);
1017 case TEMPLATE_TYPE_PARM:
1018 case TEMPLATE_TEMPLATE_PARM:
1019 case TEMPLATE_PARM_INDEX:
1020 pp_cxx_unqualified_id (pp, t);
1024 pp_cxx_identifier (pp, "typename");
1025 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1026 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1030 pp_c_type_specifier (pp_c_base (pp), t);
1035 /* type-specifier-seq:
1036 type-specifier type-specifier-seq(opt)
1039 simple-type-specifier
1042 elaborated-type-specifier
1046 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1048 switch (TREE_CODE (t))
1051 case TEMPLATE_TYPE_PARM:
1052 case TEMPLATE_TEMPLATE_PARM:
1054 case BOUND_TEMPLATE_TEMPLATE_PARM:
1055 pp_cxx_cv_qualifier_seq (pp, t);
1056 pp_cxx_simple_type_specifier (pp, t);
1060 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1061 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1062 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1066 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1067 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1072 * cv-qualifier-seq(opt)
1074 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1077 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1079 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1081 switch (TREE_CODE (t))
1083 case REFERENCE_TYPE:
1085 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1086 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1087 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1088 if (TREE_CODE (t) == POINTER_TYPE)
1091 pp_cxx_cv_qualifier_seq (pp, t);
1098 if (TYPE_PTRMEMFUNC_P (t))
1100 pp_cxx_left_paren (pp);
1101 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1106 if (TYPE_PTR_TO_MEMBER_P (t))
1108 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1109 pp_cxx_left_paren (pp);
1110 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1112 pp_cxx_cv_qualifier_seq (pp, t);
1115 /* else fall through. */
1118 pp_unsupported_tree (pp, t);
1124 pp_cxx_implicit_parameter_type (tree mf)
1126 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1130 parameter-declaration:
1131 decl-specifier-seq declarator
1132 decl-specifier-seq declarator = assignment-expression
1133 decl-specifier-seq abstract-declarator(opt)
1134 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1137 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1139 pp_cxx_decl_specifier_seq (pp, t);
1141 pp_cxx_abstract_declarator (pp, t);
1143 pp_cxx_declarator (pp, t);
1146 /* parameter-declaration-clause:
1147 parameter-declaration-list(opt) ...(opt)
1148 parameter-declaration-list , ...
1150 parameter-declaration-list:
1151 parameter-declaration
1152 parameter-declaration-list , parameter-declaration */
1155 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1157 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1159 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1160 const bool abstract = args == NULL
1161 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1164 /* Skip artificial parameter for nonstatic member functions. */
1165 if (TREE_CODE (t) == METHOD_TYPE)
1166 types = TREE_CHAIN (types);
1168 pp_cxx_left_paren (pp);
1169 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1172 pp_cxx_separate_with (pp, ',');
1174 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1175 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1177 pp_cxx_whitespace (pp);
1179 pp_cxx_whitespace (pp);
1180 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1183 pp_cxx_right_paren (pp);
1186 /* exception-specification:
1187 throw ( type-id-list(opt) )
1191 type-id-list , type-id */
1194 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1196 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1198 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1200 pp_cxx_identifier (pp, "throw");
1201 pp_cxx_left_paren (pp);
1202 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1204 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1205 if (TREE_CHAIN (ex_spec))
1206 pp_cxx_separate_with (pp, ',');
1208 pp_cxx_right_paren (pp);
1211 /* direct-declarator:
1213 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1214 exception-specification(opt)
1215 direct-declaration [ constant-expression(opt) ]
1219 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1221 switch (TREE_CODE (t))
1229 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1230 pp_cxx_id_expression (pp, DECL_NAME (t));
1232 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1236 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1237 pp_cxx_id_expression (pp, t);
1238 pp_cxx_parameter_declaration_clause (pp, t);
1240 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1242 pp_base (pp)->padding = pp_before;
1243 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1246 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1251 case TEMPLATE_TYPE_PARM:
1252 case TEMPLATE_PARM_INDEX:
1253 case TEMPLATE_TEMPLATE_PARM:
1257 pp_c_direct_declarator (pp_c_base (pp), t);
1264 ptr-operator declarator */
1267 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1269 pp_cxx_direct_declarator (pp, t);
1272 /* ctor-initializer:
1273 : mem-initializer-list
1275 mem-initializer-list:
1277 mem-initializer , mem-initializer-list
1280 mem-initializer-id ( expression-list(opt) )
1283 ::(opt) nested-name-specifier(opt) class-name
1287 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1289 t = TREE_OPERAND (t, 0);
1290 pp_cxx_whitespace (pp);
1292 pp_cxx_whitespace (pp);
1293 for (; t; t = TREE_CHAIN (t))
1295 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1296 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1298 pp_cxx_separate_with (pp, ',');
1302 /* function-definition:
1303 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1304 decl-specifier-seq(opt) declarator function-try-block */
1307 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1309 tree saved_scope = pp->enclosing_scope;
1310 pp_cxx_decl_specifier_seq (pp, t);
1311 pp_cxx_declarator (pp, t);
1312 pp_needs_newline (pp) = true;
1313 pp->enclosing_scope = DECL_CONTEXT (t);
1314 if (DECL_SAVED_TREE (t))
1315 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1318 pp_cxx_semicolon (pp);
1319 pp_needs_newline (pp) = true;
1322 pp->enclosing_scope = saved_scope;
1325 /* abstract-declarator:
1326 ptr-operator abstract-declarator(opt)
1327 direct-abstract-declarator */
1330 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1332 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1333 pp_cxx_right_paren (pp);
1334 else if (POINTER_TYPE_P (t))
1336 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1337 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1338 pp_cxx_right_paren (pp);
1341 pp_cxx_direct_abstract_declarator (pp, t);
1344 /* direct-abstract-declarator:
1345 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1346 cv-qualifier-seq(opt) exception-specification(opt)
1347 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1348 ( abstract-declarator ) */
1351 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1353 switch (TREE_CODE (t))
1355 case REFERENCE_TYPE:
1356 pp_cxx_abstract_declarator (pp, t);
1360 if (TYPE_PTRMEMFUNC_P (t))
1361 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1366 pp_cxx_parameter_declaration_clause (pp, t);
1367 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1368 if (TREE_CODE (t) == METHOD_TYPE)
1370 pp_base (pp)->padding = pp_before;
1371 pp_cxx_cv_qualifier_seq
1372 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1374 pp_cxx_exception_specification (pp, t);
1378 case TEMPLATE_TYPE_PARM:
1379 case TEMPLATE_TEMPLATE_PARM:
1380 case BOUND_TEMPLATE_TEMPLATE_PARM:
1381 case UNBOUND_CLASS_TEMPLATE:
1385 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1391 type-specifier-seq abstract-declarator(opt) */
1394 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1396 pp_flags saved_flags = pp_c_base (pp)->flags;
1397 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1399 switch (TREE_CODE (t))
1406 case BOUND_TEMPLATE_TEMPLATE_PARM:
1407 case UNBOUND_CLASS_TEMPLATE:
1408 case TEMPLATE_TEMPLATE_PARM:
1409 case TEMPLATE_TYPE_PARM:
1410 case TEMPLATE_PARM_INDEX:
1413 case TEMPLATE_ID_EXPR:
1414 pp_cxx_type_specifier_seq (pp, t);
1418 pp_c_type_id (pp_c_base (pp), t);
1422 pp_c_base (pp)->flags = saved_flags;
1425 /* template-argument-list:
1427 template-argument-list, template-argument
1430 assignment-expression
1435 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1440 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1442 tree arg = TREE_VEC_ELT (t, i);
1444 pp_cxx_separate_with (pp, ',');
1445 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1446 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1447 pp_cxx_type_id (pp, arg);
1449 pp_cxx_expression (pp, arg);
1455 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1457 t = DECL_EXPR_DECL (t);
1458 pp_cxx_type_specifier_seq (pp, t);
1460 pp_cxx_abstract_declarator (pp, t);
1462 pp_cxx_declarator (pp, t);
1468 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1470 switch (TREE_CODE (t))
1472 case CTOR_INITIALIZER:
1473 pp_cxx_ctor_initializer (pp, t);
1477 pp_cxx_identifier (pp, "using");
1478 pp_cxx_identifier (pp, "namespace");
1479 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1483 pp_cxx_identifier (pp, "using");
1484 pp_cxx_nested_name_specifier (pp, DECL_INITIAL (t));
1485 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1492 try compound-statement handler-seq */
1494 pp_maybe_newline_and_indent (pp, 0);
1495 pp_cxx_identifier (pp, "try");
1496 pp_newline_and_indent (pp, 3);
1497 pp_cxx_statement (pp, TRY_STMTS (t));
1498 pp_newline_and_indent (pp, -3);
1502 pp_cxx_statement (pp, TRY_HANDLERS (t));
1507 handler handler-seq(opt)
1510 catch ( exception-declaration ) compound-statement
1512 exception-declaration:
1513 type-specifier-seq declarator
1514 type-specifier-seq abstract-declarator
1517 pp_cxx_identifier (pp, "catch");
1518 pp_cxx_left_paren (pp);
1519 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1520 pp_cxx_right_paren (pp);
1521 pp_indentation (pp) += 3;
1522 pp_needs_newline (pp) = true;
1523 pp_cxx_statement (pp, HANDLER_BODY (t));
1524 pp_indentation (pp) -= 3;
1525 pp_needs_newline (pp) = true;
1528 /* selection-statement:
1529 if ( expression ) statement
1530 if ( expression ) statement else statement */
1532 pp_cxx_identifier (pp, "if");
1533 pp_cxx_whitespace (pp);
1534 pp_cxx_left_paren (pp);
1535 pp_cxx_expression (pp, IF_COND (t));
1536 pp_cxx_right_paren (pp);
1537 pp_newline_and_indent (pp, 2);
1538 pp_cxx_statement (pp, THEN_CLAUSE (t));
1539 pp_newline_and_indent (pp, -2);
1540 if (ELSE_CLAUSE (t))
1542 tree else_clause = ELSE_CLAUSE (t);
1543 pp_cxx_identifier (pp, "else");
1544 if (TREE_CODE (else_clause) == IF_STMT)
1545 pp_cxx_whitespace (pp);
1547 pp_newline_and_indent (pp, 2);
1548 pp_cxx_statement (pp, else_clause);
1549 if (TREE_CODE (else_clause) != IF_STMT)
1550 pp_newline_and_indent (pp, -2);
1555 pp_cxx_identifier (pp, "try");
1556 pp_newline_and_indent (pp, 2);
1557 pp_cxx_statement (pp, CLEANUP_BODY (t));
1558 pp_newline_and_indent (pp, -2);
1559 pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1560 pp_newline_and_indent (pp, 2);
1561 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1562 pp_newline_and_indent (pp, -2);
1566 pp_c_statement (pp_c_base (pp), t);
1571 /* original-namespace-definition:
1572 namespace identifier { namespace-body }
1574 As an edge case, we also handle unnamed namespace definition here. */
1577 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1579 pp_cxx_identifier (pp, "namespace");
1581 pp_cxx_unqualified_id (pp, t);
1582 pp_cxx_whitespace (pp);
1583 pp_cxx_left_brace (pp);
1584 /* We do not print the namespace-body. */
1585 pp_cxx_whitespace (pp);
1586 pp_cxx_right_brace (pp);
1592 namespace-alias-definition:
1593 namespace identifier = qualified-namespace-specifier ;
1595 qualified-namespace-specifier:
1596 ::(opt) nested-name-specifier(opt) namespace-name */
1599 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1601 pp_cxx_identifier (pp, "namespace");
1602 pp_cxx_unqualified_id (pp, t);
1603 pp_cxx_whitespace (pp);
1605 pp_cxx_whitespace (pp);
1606 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1607 pp_cxx_semicolon (pp);
1610 /* simple-declaration:
1611 decl-specifier-seq(opt) init-declarator-list(opt) */
1614 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1616 pp_cxx_decl_specifier_seq (pp, t);
1617 pp_cxx_init_declarator (pp, t);
1618 pp_cxx_semicolon (pp);
1619 pp_needs_newline (pp) = true;
1623 template-parameter-list:
1625 template-parameter-list , template-parameter */
1628 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1630 const int n = TREE_VEC_LENGTH (t);
1632 for (i = 0; i < n; ++i)
1635 pp_cxx_separate_with (pp, ',');
1636 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1640 /* template-parameter:
1642 parameter-declaration
1645 class identifier(opt)
1646 class identifier(op) = type-id
1647 typename identifier(opt)
1648 typename identifier(opt) = type-id
1649 template < template-parameter-list > class identifier(opt)
1650 template < template-parameter-list > class identifier(opt) = template-name
1654 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1656 tree parameter = TREE_VALUE (t);
1657 switch (TREE_CODE (parameter))
1660 pp_cxx_identifier (pp, "class");
1661 if (DECL_NAME (parameter))
1662 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1663 /* FIXME: Chech if we should print also default argument. */
1667 pp_cxx_parameter_declaration (pp, parameter);
1674 pp_unsupported_tree (pp, t);
1679 /* Pretty-print a template parameter in the canonical form
1680 "template-parameter-<level>-<position in parameter list>". */
1683 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1685 const enum tree_code code = TREE_CODE (parm);
1687 /* Brings type template parameters to the canonical forms. */
1688 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1689 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1690 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1692 pp_cxx_begin_template_argument_list (pp);
1693 pp_cxx_identifier (pp, "template-parameter-");
1694 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1696 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1697 pp_cxx_end_template_argument_list (pp);
1701 template-declaration:
1702 export(opt) template < template-parameter-list > declaration */
1705 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1707 tree tmpl = most_general_template (t);
1711 pp_maybe_newline_and_indent (pp, 0);
1712 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1714 pp_cxx_identifier (pp, "template");
1715 pp_cxx_begin_template_argument_list (pp);
1716 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1717 pp_cxx_end_template_argument_list (pp);
1718 pp_newline_and_indent (pp, 3);
1721 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1722 pp_cxx_function_definition (pp, t);
1724 pp_cxx_simple_declaration (pp, t);
1728 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1730 pp_unsupported_tree (pp, t);
1734 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1736 pp_unsupported_tree (pp, t);
1743 template-declaration
1744 explicit-instantiation
1745 explicit-specialization
1746 linkage-specification
1747 namespace-definition
1752 namespace-alias-definition
1756 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1758 if (!DECL_LANG_SPECIFIC (t))
1759 pp_cxx_simple_declaration (pp, t);
1760 else if (DECL_USE_TEMPLATE (t))
1761 switch (DECL_USE_TEMPLATE (t))
1764 pp_cxx_template_declaration (pp, t);
1768 pp_cxx_explicit_specialization (pp, t);
1772 pp_cxx_explicit_instantiation (pp, t);
1778 else switch (TREE_CODE (t))
1782 pp_cxx_simple_declaration (pp, t);
1786 if (DECL_SAVED_TREE (t))
1787 pp_cxx_function_definition (pp, t);
1789 pp_cxx_simple_declaration (pp, t);
1792 case NAMESPACE_DECL:
1793 if (DECL_NAMESPACE_ALIAS (t))
1794 pp_cxx_namespace_alias_definition (pp, t);
1796 pp_cxx_original_namespace_definition (pp, t);
1800 pp_unsupported_tree (pp, t);
1806 typedef c_pretty_print_fn pp_fun;
1808 /* Initialization of a C++ pretty-printer object. */
1811 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1813 pp_c_pretty_printer_init (pp_c_base (pp));
1814 pp_set_line_maximum_length (pp, 0);
1816 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1817 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1818 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1819 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1820 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1821 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1822 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1823 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1824 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1825 pp->c_base.direct_abstract_declarator =
1826 (pp_fun) pp_cxx_direct_abstract_declarator;
1827 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1829 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1831 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1832 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1833 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1834 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1835 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1836 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1837 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1838 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1839 pp->enclosing_scope = global_namespace;