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);
183 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
186 case TEMPLATE_TYPE_PARM:
188 case TEMPLATE_PARM_INDEX:
189 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
193 pp_unsupported_tree (pp, t);
199 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
201 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
202 && TYPE_P (scope) && dependent_type_p (scope))
203 pp_cxx_identifier (pp, "template");
206 /* nested-name-specifier:
207 class-or-namespace-name :: nested-name-specifier(opt)
208 class-or-namespace-name :: template nested-name-specifier */
210 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
212 if (t != NULL && t != pp->enclosing_scope)
214 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
215 pp_cxx_nested_name_specifier (pp, scope);
216 pp_cxx_template_keyword_if_needed (pp, scope, t);
217 pp_cxx_unqualified_id (pp, t);
218 pp_cxx_colon_colon (pp);
223 nested-name-specifier template(opt) unqualified-id */
225 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
227 switch (TREE_CODE (t))
230 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
231 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
237 if (DECL_FUNCTION_MEMBER_P (t))
238 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
239 pp_cxx_unqualified_id
240 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
245 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
246 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
251 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
252 if (scope != pp->enclosing_scope)
254 pp_cxx_nested_name_specifier (pp, scope);
255 pp_cxx_template_keyword_if_needed (pp, scope, t);
257 pp_cxx_unqualified_id (pp, t);
267 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
269 if (TREE_CODE (t) == OVERLOAD)
271 if ((TREE_CODE (t) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (t))
272 || (pp_c_base (pp)->flags
273 & (pp_cxx_flag_qualified_id | pp_cxx_flag_global_scope)))
274 pp_cxx_qualified_id (pp, t);
276 pp_cxx_unqualified_id (pp, t);
279 /* primary-expression:
283 :: operator-function-id
288 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
290 switch (TREE_CODE (t))
295 pp_c_constant (pp_c_base (pp), t);
299 t = BASELINK_FUNCTIONS (t);
307 pp_cxx_id_expression (pp, t);
311 case TEMPLATE_TYPE_PARM:
312 case TEMPLATE_PARM_INDEX:
313 pp_cxx_unqualified_id (pp, t);
317 pp_c_primary_expression (pp_c_base (pp), t);
322 /* postfix-expression:
324 postfix-expression [ expression ]
325 postfix-expression ( expression-list(opt) )
326 simple-type-specifier ( expression-list(opt) )
327 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
328 typename ::(opt) nested-name-specifier template(opt)
329 template-id ( expression-list(opt) )
330 postfix-expression . template(opt) ::(opt) id-expression
331 postfix-expression -> template(opt) ::(opt) id-expression
332 postfix-expression . pseudo-destructor-name
333 postfix-expression -> pseudo-destructor-name
334 postfix-expression ++
335 postfix-expression --
336 dynamic_cast < type-id > ( expression )
337 static_cast < type-id > ( expression )
338 reinterpret_cast < type-id > ( expression )
339 const_cast < type-id > ( expression )
340 typeid ( expression )
341 typeif ( type-id ) */
344 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
346 enum tree_code code = TREE_CODE (t);
353 tree fun = TREE_OPERAND (t, 0);
354 tree args = TREE_OPERAND (t, 1);
355 tree saved_scope = pp->enclosing_scope;
357 if (TREE_CODE (fun) == ADDR_EXPR)
358 fun = TREE_OPERAND (fun, 0);
360 /* In templates, where there is no way to tell whether a given
361 call uses an actual member function. So the parser builds
362 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
363 instantiation time. */
364 if (TREE_CODE (fun) != FUNCTION_DECL)
366 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
368 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
369 ? TREE_OPERAND (t, 2)
372 while (TREE_CODE (object) == NOP_EXPR)
373 object = TREE_OPERAND (object, 0);
375 if (TREE_CODE (object) == ADDR_EXPR)
376 object = TREE_OPERAND (object, 0);
378 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
380 pp_cxx_postfix_expression (pp, object);
385 pp_cxx_postfix_expression (pp, object);
388 args = TREE_CHAIN (args);
389 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
392 pp_cxx_postfix_expression (pp, fun);
393 pp->enclosing_scope = saved_scope;
394 pp_cxx_call_argument_list (pp, args);
396 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
398 pp_separate_with (pp, ',');
399 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
412 pp_cxx_primary_expression (pp, t);
415 case DYNAMIC_CAST_EXPR:
416 case STATIC_CAST_EXPR:
417 case REINTERPRET_CAST_EXPR:
418 case CONST_CAST_EXPR:
419 if (code == DYNAMIC_CAST_EXPR)
420 pp_identifier (pp, "dynamic_cast");
421 else if (code == STATIC_CAST_EXPR)
422 pp_identifier (pp, "static_cast");
423 else if (code == REINTERPRET_CAST_EXPR)
424 pp_identifier (pp, "reinterpret_cast");
426 pp_identifier (pp, "const_cast");
427 pp_cxx_begin_template_argument_list (pp);
428 pp_cxx_type_id (pp, TREE_TYPE (t));
429 pp_cxx_end_template_argument_list (pp);
431 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
435 case EMPTY_CLASS_EXPR:
436 pp_cxx_type_id (pp, TREE_TYPE (t));
442 t = TREE_OPERAND (t, 0);
443 pp_cxx_identifier (pp, "typeid");
446 pp_cxx_type_id (pp, t);
448 pp_cxx_expression (pp, t);
452 case PSEUDO_DTOR_EXPR:
453 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
455 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
456 pp_cxx_colon_colon (pp);
458 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
462 pp_c_postfix_expression (pp_c_base (pp), t);
468 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
469 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
475 type-specifier-seq new-declarator(opt)
478 ptr-operator new-declarator(opt)
479 direct-new-declarator
481 direct-new-declarator
483 direct-new-declarator [ constant-expression ]
486 ( expression-list(opt) ) */
488 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
490 enum tree_code code = TREE_CODE (t);
495 if (NEW_EXPR_USE_GLOBAL (t))
496 pp_cxx_colon_colon (pp);
497 pp_cxx_identifier (pp, "new");
498 if (TREE_OPERAND (t, 0))
500 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
503 /* FIXME: array-types are built with one more element. */
504 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
505 if (TREE_OPERAND (t, 2))
508 t = TREE_OPERAND (t, 2);
509 if (TREE_CODE (t) == TREE_LIST)
510 pp_c_expression_list (pp_c_base (pp), t);
511 else if (t == void_zero_node)
512 ; /* OK, empty initializer list. */
514 pp_cxx_expression (pp, t);
520 pp_unsupported_tree (pp, t);
524 /* delete-expression:
525 ::(opt) delete cast-expression
526 ::(opt) delete [ ] cast-expression */
528 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
530 enum tree_code code = TREE_CODE (t);
534 case VEC_DELETE_EXPR:
535 if (DELETE_EXPR_USE_GLOBAL (t))
536 pp_cxx_colon_colon (pp);
537 pp_cxx_identifier (pp, "delete");
538 if (code == VEC_DELETE_EXPR)
540 pp_left_bracket (pp);
541 pp_right_bracket (pp);
543 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
547 pp_unsupported_tree (pp, t);
555 unary-operator cast-expression
556 sizeof unary-expression
561 unary-operator: one of
565 __alignof__ unary-expression
566 __alignof__ ( type-id ) */
568 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
570 enum tree_code code = TREE_CODE (t);
575 pp_cxx_new_expression (pp, t);
579 case VEC_DELETE_EXPR:
580 pp_cxx_delete_expression (pp, t);
584 pp_c_unary_expression (pp_c_base (pp), t);
591 ( type-id ) cast-expression */
593 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
595 switch (TREE_CODE (t))
598 pp_cxx_type_id (pp, TREE_TYPE (t));
599 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
603 pp_c_cast_expression (pp_c_base (pp), t);
610 pm-expression .* cast-expression
611 pm-expression ->* cast-expression */
613 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
615 switch (TREE_CODE (t))
617 /* Handle unfortunate OFFESET_REF overloading here. */
619 if (TYPE_P (TREE_OPERAND (t, 0)))
621 pp_cxx_qualified_id (pp, t);
624 /* else fall through */
627 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
630 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
635 pp_cxx_cast_expression (pp, t);
640 /* multiplicative-expression:
642 multiplicative-expression * pm-expression
643 multiplicative-expression / pm-expression
644 multiplicative-expression % pm-expression */
646 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
648 enum tree_code code = TREE_CODE (e);
654 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
656 if (code == MULT_EXPR)
658 else if (code == TRUNC_DIV_EXPR)
663 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
667 pp_cxx_pm_expression (pp, e);
672 /* conditional-expression:
673 logical-or-expression
674 logical-or-expression ? expression : assignment-expression */
676 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
678 if (TREE_CODE (e) == COND_EXPR)
680 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
684 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
686 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
689 pp_c_logical_or_expression (pp_c_base (pp), e);
693 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
697 switch (TREE_CODE (t))
720 op = tree_code_name[TREE_CODE (t)];
724 pp_cxx_identifier (pp, op);
728 /* assignment-expression:
729 conditional-expression
730 logical-or-expression assignment-operator assignment-expression
734 throw assignment-expression(opt)
736 assignment-operator: one of
737 = *= /= %= += -= >>= <<= &= ^= |= */
739 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
741 switch (TREE_CODE (e))
745 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
749 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
753 pp_cxx_identifier (pp, "throw");
754 if (TREE_OPERAND (e, 0))
755 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
759 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
760 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
761 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
765 pp_cxx_conditional_expression (pp, e);
771 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
773 switch (TREE_CODE (t))
778 pp_c_constant (pp_c_base (pp), t);
782 pp_cxx_unqualified_id (pp, t);
790 pp_cxx_qualified_id (pp, t);
802 case TEMPLATE_TYPE_PARM:
803 case TEMPLATE_PARM_INDEX:
804 pp_cxx_primary_expression (pp, t);
808 case DYNAMIC_CAST_EXPR:
809 case STATIC_CAST_EXPR:
810 case REINTERPRET_CAST_EXPR:
811 case CONST_CAST_EXPR:
815 case EMPTY_CLASS_EXPR:
817 case PSEUDO_DTOR_EXPR:
819 pp_cxx_postfix_expression (pp, t);
824 pp_cxx_new_expression (pp, t);
828 case VEC_DELETE_EXPR:
829 pp_cxx_delete_expression (pp, t);
833 pp_cxx_cast_expression (pp, t);
839 pp_cxx_pm_expression (pp, t);
845 pp_cxx_multiplicative_expression (pp, t);
849 pp_cxx_conditional_expression (pp, t);
856 pp_cxx_assignment_expression (pp, t);
860 pp_c_expression (pp_c_base (pp), t);
868 /* function-specifier:
873 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
875 switch (TREE_CODE (t))
878 if (DECL_VIRTUAL_P (t))
879 pp_cxx_identifier (pp, "virtual");
880 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
881 pp_cxx_identifier (pp, "explicit");
883 pp_c_function_specifier (pp_c_base (pp), t);
890 /* decl-specifier-seq:
891 decl-specifier-seq(opt) decl-specifier
894 storage-class-specifier
900 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
902 switch (TREE_CODE (t))
908 pp_cxx_storage_class_specifier (pp, t);
909 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
913 pp_cxx_identifier (pp, "typedef");
914 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
918 if (TYPE_PTRMEMFUNC_P (t))
920 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
921 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
922 pp_cxx_whitespace (pp);
923 pp_cxx_ptr_operator (pp, t);
928 /* Constructors don't have return types. And conversion functions
929 do not have a type-specifier in their return types. */
930 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
931 pp_cxx_function_specifier (pp, t);
932 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
933 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
936 pp_c_declaration_specifiers (pp_c_base (pp), t);
941 /* simple-type-specifier:
942 ::(opt) nested-name-specifier(opt) type-name
943 ::(opt) nested-name-specifier(opt) template(opt) template-id
956 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
958 switch (TREE_CODE (t))
963 pp_cxx_qualified_id (pp, t);
966 case TEMPLATE_TYPE_PARM:
967 case TEMPLATE_PARM_INDEX:
968 pp_cxx_unqualified_id (pp, t);
972 pp_cxx_identifier (pp, "typename");
973 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
974 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
978 pp_c_type_specifier (pp_c_base (pp), t);
983 /* type-specifier-seq:
984 type-specifier type-specifier-seq(opt)
987 simple-type-specifier
990 elaborated-type-specifier
994 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
996 switch (TREE_CODE (t))
999 case TEMPLATE_TYPE_PARM:
1001 case BOUND_TEMPLATE_TEMPLATE_PARM:
1002 pp_c_type_qualifier_list (pp_c_base (pp), t);
1003 pp_cxx_simple_type_specifier (pp, t);
1007 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1008 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1009 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1013 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1014 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1019 * cv-qualifier-seq(opt)
1021 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1024 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1026 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1028 switch (TREE_CODE (t))
1030 case REFERENCE_TYPE:
1032 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1033 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1034 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1035 if (TREE_CODE (t) == POINTER_TYPE)
1038 pp_cxx_cv_qualifier_seq (pp, t);
1045 if (TYPE_PTRMEMFUNC_P (t))
1047 pp_cxx_left_paren (pp);
1048 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1053 if (TYPE_PTR_TO_MEMBER_P (t))
1055 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1057 pp_cxx_cv_qualifier_seq (pp, t);
1060 /* else fall trhough. */
1063 pp_unsupported_tree (pp, t);
1069 pp_cxx_implicit_parameter_type (tree mf)
1071 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1075 parameter-declaration:
1076 decl-specifier-seq declarator
1077 decl-specifier-seq declarator = assignment-expression
1078 decl-specifier-seq abstract-declarator(opt)
1079 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1081 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1083 pp_cxx_decl_specifier_seq (pp, t);
1085 pp_cxx_abstract_declarator (pp, t);
1087 pp_cxx_declarator (pp, t);
1090 /* parameter-declaration-clause:
1091 parameter-declaration-list(opt) ...(opt)
1092 parameter-declaration-list , ...
1094 parameter-declaration-list:
1095 parameter-declaration
1096 parameter-declaration-list , parameter-declaration */
1098 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1100 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1101 tree types = TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1102 const bool abstract = args == NULL
1103 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1106 /* Skip artificial parameter for nonstatic member functions. */
1107 if (TREE_CODE (t) == METHOD_TYPE)
1108 types = TREE_CHAIN (types);
1110 pp_cxx_left_paren (pp);
1111 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1114 pp_separate_with (pp, ',');
1116 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1117 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1119 pp_cxx_whitespace (pp);
1121 pp_cxx_whitespace (pp);
1122 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1125 pp_cxx_right_paren (pp);
1128 /* exception-specification:
1129 throw ( type-id-list(opt) )
1133 type-id-list , type-id */
1135 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1137 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1139 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1141 pp_cxx_identifier (pp, "throw");
1142 pp_cxx_left_paren (pp);
1143 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1145 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1146 if (TREE_CHAIN (ex_spec))
1147 pp_separate_with (pp, ',');
1149 pp_cxx_right_paren (pp);
1152 /* direct-declarator:
1154 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1155 exception-specification(opt)
1156 direct-declaration [ constant-expression(opt) ]
1159 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1161 switch (TREE_CODE (t))
1169 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1170 pp_cxx_id_expression (pp, DECL_NAME (t));
1172 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1176 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1177 pp_cxx_id_expression (pp, t);
1178 pp_cxx_parameter_declaration_clause (pp, t);
1180 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1182 pp_base (pp)->padding = pp_before;
1183 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1186 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1191 case TEMPLATE_TYPE_PARM:
1192 case TEMPLATE_PARM_INDEX:
1196 pp_c_direct_declarator (pp_c_base (pp), t);
1203 ptr-operator declarator */
1205 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1207 pp_cxx_direct_declarator (pp, t);
1210 /* ctor-initializer:
1211 : mem-initializer-list
1213 mem-initializer-list:
1215 mem-initializer , mem-initializer-list
1218 mem-initializer-id ( expression-list(opt) )
1221 ::(opt) nested-name-specifier(opt) class-name
1224 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1226 t = TREE_OPERAND (t, 0);
1227 pp_cxx_whitespace (pp);
1229 pp_cxx_whitespace (pp);
1230 for (; t; t = TREE_CHAIN (t))
1232 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1233 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1235 pp_separate_with (pp, ',');
1239 /* function-definition:
1240 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1241 decl-specifier-seq(opt) declarator function-try-block */
1244 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1246 tree saved_scope = pp->enclosing_scope;
1247 pp_cxx_decl_specifier_seq (pp, t);
1248 pp_cxx_declarator (pp, t);
1249 pp_needs_newline (pp) = true;
1250 pp->enclosing_scope = DECL_CONTEXT (t);
1251 if (DECL_SAVED_TREE (t))
1253 tree body = DECL_SAVED_TREE (t);
1254 if (TREE_CODE (body) == COMPOUND_STMT
1255 && TREE_CODE (COMPOUND_BODY (body)) == CTOR_INITIALIZER)
1257 body = COMPOUND_BODY (body);
1258 pp_cxx_ctor_initializer (pp, body);
1259 body = TREE_CHAIN (body);
1261 pp_cxx_statement (pp, body);
1265 pp_cxx_semicolon (pp);
1266 pp_needs_newline (pp) = true;
1269 pp->enclosing_scope = saved_scope;
1272 /* abstract-declarator:
1273 ptr-operator abstract-declarator(opt)
1274 direct-abstract-declarator */
1276 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1278 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1279 pp_cxx_right_paren (pp);
1280 else if (POINTER_TYPE_P (t))
1282 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1283 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1284 pp_cxx_right_paren (pp);
1287 pp_cxx_direct_abstract_declarator (pp, t);
1290 /* direct-abstract-declarator:
1291 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1292 cv-quafilier-seq(opt) exception-specification(opt)
1293 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1294 ( abstract-declarator ) */
1296 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1298 switch (TREE_CODE (t))
1300 case REFERENCE_TYPE:
1301 pp_cxx_abstract_declarator (pp, t);
1305 if (TYPE_PTRMEMFUNC_P (t))
1306 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1311 pp_cxx_parameter_declaration_clause (pp, t);
1312 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1313 if (TREE_CODE (t) == METHOD_TYPE)
1315 pp_base (pp)->padding = pp_before;
1316 pp_cxx_cv_qualifier_seq
1317 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1319 pp_cxx_exception_specification (pp, t);
1323 case TEMPLATE_TYPE_PARM:
1324 case TEMPLATE_TEMPLATE_PARM:
1325 case BOUND_TEMPLATE_TEMPLATE_PARM:
1326 case UNBOUND_CLASS_TEMPLATE:
1330 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1336 type-specifier-seq abstract-declarator(opt) */
1338 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1340 pp_flags saved_flags = pp_c_base (pp)->flags;
1341 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1343 switch (TREE_CODE (t))
1350 case BOUND_TEMPLATE_TEMPLATE_PARM:
1351 case UNBOUND_CLASS_TEMPLATE:
1352 case TEMPLATE_TEMPLATE_PARM:
1353 case TEMPLATE_TYPE_PARM:
1354 case TEMPLATE_PARM_INDEX:
1357 case TEMPLATE_ID_EXPR:
1358 /* FIXME: Should be pp_cxx_type_specifier_seq. */
1359 pp_cxx_type_specifier_seq (pp, t);
1360 pp_cxx_declarator (pp, t);
1364 pp_c_type_id (pp_c_base (pp), t);
1368 pp_c_base (pp)->flags = saved_flags;
1371 /* template-argument-list:
1373 template-argument-list, template-argument
1376 assignment-expression
1380 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1385 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1387 tree arg = TREE_VEC_ELT (t, i);
1389 pp_separate_with (pp, ',');
1390 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1391 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1392 pp_cxx_type_id (pp, arg);
1394 pp_cxx_expression (pp, arg);
1400 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1402 t = DECL_STMT_DECL (t);
1403 pp_cxx_type_specifier_seq (pp, t);
1405 pp_cxx_abstract_declarator (pp, t);
1407 pp_cxx_declarator (pp, t);
1413 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1415 switch (TREE_CODE (t))
1418 pp_cxx_identifier (pp, "using");
1419 pp_cxx_identifier (pp, "namespace");
1420 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1424 pp_cxx_identifier (pp, "using");
1425 pp_cxx_nested_name_specifier (pp, DECL_INITIAL (t));
1426 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1433 try compound-statement handler-seq */
1435 pp_maybe_newline_and_indent (pp, 0);
1436 pp_cxx_identifier (pp, "try");
1437 pp_newline_and_indent (pp, 3);
1438 pp_cxx_statement (pp, TRY_STMTS (t));
1439 pp_newline_and_indent (pp, -3);
1443 pp_cxx_statement (pp, TRY_HANDLERS (t));
1448 handler handler-seq(opt)
1451 catch ( exception-declaration ) compound-statement
1453 exception-declaration:
1454 type-specifier-seq declarator
1455 type-specifier-seq abstract-declarator
1458 pp_cxx_identifier (pp, "catch");
1459 pp_cxx_left_paren (pp);
1460 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1461 pp_cxx_right_paren (pp);
1462 pp_indentation (pp) += 3;
1463 pp_needs_newline (pp) = true;
1464 pp_cxx_statement (pp, HANDLER_BODY (t));
1465 pp_indentation (pp) -= 3;
1466 pp_needs_newline (pp) = true;
1470 pp_c_statement (pp_c_base (pp), t);
1475 /* original-namespace-definition:
1476 namespace identifier { namespace-body }
1478 As an edge case, we also handle unnamed namespace definition here. */
1481 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1483 pp_cxx_identifier (pp, "namespace");
1484 if (DECL_NAME (t) != anonymous_namespace_name)
1485 pp_cxx_unqualified_id (pp, t);
1486 pp_cxx_whitespace (pp);
1487 pp_cxx_left_brace (pp);
1488 /* We do not print the namespace-body. */
1489 pp_cxx_whitespace (pp);
1490 pp_cxx_right_brace (pp);
1496 namespace-alias-definition:
1497 namespace identifier = qualified-namespace-specifier ;
1499 qualified-namespace-specifier:
1500 ::(opt) nested-name-specifier(opt) namespace-name */
1503 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1505 pp_cxx_identifier (pp, "namespace");
1506 pp_cxx_unqualified_id (pp, t);
1507 pp_cxx_whitespace (pp);
1509 pp_cxx_whitespace (pp);
1510 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1511 pp_cxx_semicolon (pp);
1514 /* simple-declaration:
1515 decl-specifier-seq(opt) init-declarator-list(opt) */
1517 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1519 pp_cxx_decl_specifier_seq (pp, t);
1520 pp_cxx_init_declarator (pp, t);
1521 pp_cxx_semicolon (pp);
1522 pp_needs_newline (pp) = true;
1526 template-parameter-list:
1528 template-parameter-list , template-parameter */
1531 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1533 const int n = TREE_VEC_LENGTH (t);
1535 for (i = 0; i < n; ++i)
1538 pp_separate_with (pp, ',');
1539 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1543 /* template-parameter:
1545 parameter-declaration
1548 class identifier(opt)
1549 class identifier(op) = type-id
1550 typename identifier(opt)
1551 typename identifier(opt) = type-id
1552 template < template-parameter-list > class identifier(opt)
1553 template < template-parameter-list > class identifier(opt) = template-name
1556 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1558 tree parameter = TREE_VALUE (t);
1559 switch (TREE_CODE (parameter))
1562 pp_cxx_identifier (pp, "class");
1563 if (DECL_NAME (parameter))
1564 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1565 /* FIXME: Chech if we should print also default argument. */
1569 pp_cxx_parameter_declaration (pp, parameter);
1576 pp_unsupported_tree (pp, t);
1581 /* Pretty-print a template parameter in the canonical form
1582 "template-parameter-<level>-<position in parameter list>". */
1585 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1587 const enum tree_code code = TREE_CODE (parm);
1589 /* Brings type template parameters to the canonical forms */
1590 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1591 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1592 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1594 pp_cxx_begin_template_argument_list (pp);
1595 pp_cxx_identifier (pp, "template-parameter-");
1596 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1598 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1599 pp_cxx_end_template_argument_list (pp);
1603 template-declaration:
1604 export(opt) template < template-parameter-list > declaration */
1606 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1608 tree tmpl = most_general_template (t);
1612 pp_maybe_newline_and_indent (pp, 0);
1613 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1615 pp_cxx_identifier (pp, "template");
1616 pp_cxx_begin_template_argument_list (pp);
1617 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1618 pp_cxx_end_template_argument_list (pp);
1619 pp_newline_and_indent (pp, 3);
1622 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1623 pp_cxx_function_definition (pp, t);
1625 pp_cxx_simple_declaration (pp, t);
1629 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1631 pp_unsupported_tree (pp, t);
1635 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1637 pp_unsupported_tree (pp, t);
1644 template-declaration
1645 explicit-instantiation
1646 explicit-specialization
1647 linkage-specification
1648 namespace-definition
1653 namespace-alias-definition
1657 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1659 if (!DECL_LANG_SPECIFIC (t))
1660 pp_cxx_simple_declaration (pp, t);
1661 else if (DECL_USE_TEMPLATE (t))
1662 switch (DECL_USE_TEMPLATE (t))
1665 pp_cxx_template_declaration (pp, t);
1669 pp_cxx_explicit_specialization (pp, t);
1673 pp_cxx_explicit_instantiation (pp, t);
1679 else switch (TREE_CODE (t))
1683 pp_cxx_simple_declaration (pp, t);
1687 if (DECL_SAVED_TREE (t))
1688 pp_cxx_function_definition (pp, t);
1690 pp_cxx_simple_declaration (pp, t);
1693 case NAMESPACE_DECL:
1694 if (DECL_NAMESPACE_ALIAS (t))
1695 pp_cxx_namespace_alias_definition (pp, t);
1697 pp_cxx_original_namespace_definition (pp, t);
1701 pp_unsupported_tree (pp, t);
1707 typedef c_pretty_print_fn pp_fun;
1710 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1712 pp_c_pretty_printer_init (pp_c_base (pp));
1713 pp_set_line_maximum_length (pp, 0);
1715 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1716 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1717 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1718 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1719 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1720 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1721 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1722 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1723 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1724 pp->c_base.direct_abstract_declarator =
1725 (pp_fun) pp_cxx_direct_abstract_declarator;
1726 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1728 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1730 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1731 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1732 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1733 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1734 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1735 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1736 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1737 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1738 pp->enclosing_scope = global_namespace;