Merge from mainline
[official-gcc.git] / gcc / cp / cxx-pretty-print.c
blob43fbe2b3bfa02b9bb1cdafa789e21b54c166a01e
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
10 version.
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
15 for more details.
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "cxx-pretty-print.h"
28 #include "cp-tree.h"
29 #include "toplev.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);
46 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
49 static inline void
50 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
52 const char *p = pp_last_position_in_text (pp);
54 if (p != NULL && *p == c)
55 pp_cxx_whitespace (pp);
56 pp_character (pp, c);
57 pp_base (pp)->padding = pp_none;
60 #define pp_cxx_storage_class_specifier(PP, T) \
61 pp_c_storage_class_specifier (pp_c_base (PP), T)
62 #define pp_cxx_expression_list(PP, T) \
63 pp_c_expression_list (pp_c_base (PP), T)
64 #define pp_cxx_space_for_pointer_operator(PP, T) \
65 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
66 #define pp_cxx_init_declarator(PP, T) \
67 pp_c_init_declarator (pp_c_base (PP), T)
68 #define pp_cxx_call_argument_list(PP, T) \
69 pp_c_call_argument_list (pp_c_base (PP), T)
71 void
72 pp_cxx_colon_colon (cxx_pretty_printer *pp)
74 pp_colon_colon (pp);
75 pp_base (pp)->padding = pp_none;
78 void
79 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
81 pp_cxx_nonconsecutive_character (pp, '<');
84 void
85 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
87 pp_cxx_nonconsecutive_character (pp, '>');
90 void
91 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
93 pp_separate_with (pp, c);
94 pp_base (pp)->padding = pp_none;
97 /* Expressions. */
99 static inline bool
100 is_destructor_name (tree name)
102 return name == complete_dtor_identifier
103 || name == base_dtor_identifier
104 || name == deleting_dtor_identifier;
107 /* conversion-function-id:
108 operator conversion-type-id
110 conversion-type-id:
111 type-specifier-seq conversion-declarator(opt)
113 conversion-declarator:
114 ptr-operator conversion-declarator(opt) */
116 static inline void
117 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
119 pp_cxx_identifier (pp, "operator");
120 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
123 static inline void
124 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
126 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
127 pp_cxx_begin_template_argument_list (pp);
128 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
129 pp_cxx_end_template_argument_list (pp);
132 /* unqualified-id:
133 identifier
134 operator-function-id
135 conversion-function-id
136 ~ class-name
137 template-id */
139 static void
140 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
142 enum tree_code code = TREE_CODE (t);
143 switch (code)
145 case RESULT_DECL:
146 pp_cxx_identifier (pp, "<return-value>");
147 break;
149 case OVERLOAD:
150 t = OVL_CURRENT (t);
151 case VAR_DECL:
152 case PARM_DECL:
153 case CONST_DECL:
154 case TYPE_DECL:
155 case FUNCTION_DECL:
156 case NAMESPACE_DECL:
157 case FIELD_DECL:
158 case LABEL_DECL:
159 case USING_DECL:
160 case TEMPLATE_DECL:
161 t = DECL_NAME (t);
163 case IDENTIFIER_NODE:
164 if (t == NULL)
165 pp_cxx_identifier (pp, "<unnamed>");
166 else if (IDENTIFIER_TYPENAME_P (t))
167 pp_cxx_conversion_function_id (pp, t);
168 else
170 if (is_destructor_name (t))
172 pp_complement (pp);
173 /* FIXME: Why is this necessary? */
174 if (TREE_TYPE (t))
175 t = constructor_name (TREE_TYPE (t));
177 pp_cxx_tree_identifier (pp, t);
179 break;
181 case TEMPLATE_ID_EXPR:
182 pp_cxx_template_id (pp, t);
183 break;
185 case BASELINK:
186 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
187 break;
189 case RECORD_TYPE:
190 case UNION_TYPE:
191 case ENUMERAL_TYPE:
192 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
193 break;
195 case TEMPLATE_TYPE_PARM:
196 case TEMPLATE_TEMPLATE_PARM:
197 if (TYPE_IDENTIFIER (t))
198 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
199 else
200 pp_cxx_canonical_template_parameter (pp, t);
201 break;
203 case TEMPLATE_PARM_INDEX:
204 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
205 break;
207 default:
208 pp_unsupported_tree (pp, t);
209 break;
213 /* Pretty-print out the token sequence ":: template" in template codes
214 where it is needed to "inline declare" the (following) member as
215 a template. This situation arises when SCOPE of T is dependent
216 on template parameters. */
218 static inline void
219 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
221 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
222 && TYPE_P (scope) && dependent_type_p (scope))
223 pp_cxx_identifier (pp, "template");
226 /* nested-name-specifier:
227 class-or-namespace-name :: nested-name-specifier(opt)
228 class-or-namespace-name :: template nested-name-specifier */
230 static void
231 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
233 if (t != NULL && t != pp->enclosing_scope)
235 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
236 pp_cxx_nested_name_specifier (pp, scope);
237 pp_cxx_template_keyword_if_needed (pp, scope, t);
238 pp_cxx_unqualified_id (pp, t);
239 pp_cxx_colon_colon (pp);
243 /* qualified-id:
244 nested-name-specifier template(opt) unqualified-id */
246 static void
247 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
249 switch (TREE_CODE (t))
251 /* A pointer-to-member is always qualified. */
252 case PTRMEM_CST:
253 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
254 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
255 break;
257 /* In Standard C++, functions cannot possibly be used as
258 nested-name-specifiers. However, there are situations where
259 is "makes sense" to output the surrounding function name for the
260 purpose of emphasizing on the scope kind. Just printing the
261 function name might not be sufficient as it may be overloaded; so,
262 we decorate the function with its signature too.
263 FIXME: This is probably the wrong pretty-printing for conversion
264 functions and some function templates. */
265 case OVERLOAD:
266 t = OVL_CURRENT (t);
267 case FUNCTION_DECL:
268 if (DECL_FUNCTION_MEMBER_P (t))
269 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
270 pp_cxx_unqualified_id
271 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
272 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
273 break;
275 case OFFSET_REF:
276 case SCOPE_REF:
277 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
278 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
279 break;
281 default:
283 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
284 if (scope != pp->enclosing_scope)
286 pp_cxx_nested_name_specifier (pp, scope);
287 pp_cxx_template_keyword_if_needed (pp, scope, t);
289 pp_cxx_unqualified_id (pp, t);
291 break;
296 static void
297 pp_cxx_constant (cxx_pretty_printer *pp, tree t)
299 switch (TREE_CODE (t))
301 case STRING_CST:
303 const bool in_parens = PAREN_STRING_LITERAL_P (t);
304 if (in_parens)
305 pp_cxx_left_paren (pp);
306 pp_c_constant (pp_c_base (pp), t);
307 if (in_parens)
308 pp_cxx_right_paren (pp);
310 break;
312 default:
313 pp_c_constant (pp_c_base (pp), t);
314 break;
318 /* id-expression:
319 unqualified-id
320 qualified-id */
322 static inline void
323 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
325 if (TREE_CODE (t) == OVERLOAD)
326 t = OVL_CURRENT (t);
327 if (DECL_P (t) && DECL_CONTEXT (t))
328 pp_cxx_qualified_id (pp, t);
329 else
330 pp_cxx_unqualified_id (pp, t);
333 /* primary-expression:
334 literal
335 this
336 :: identifier
337 :: operator-function-id
338 :: qualifier-id
339 ( expression )
340 id-expression */
342 static void
343 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
345 switch (TREE_CODE (t))
347 case INTEGER_CST:
348 case REAL_CST:
349 case STRING_CST:
350 pp_cxx_constant (pp, t);
351 break;
353 case BASELINK:
354 t = BASELINK_FUNCTIONS (t);
355 case VAR_DECL:
356 case PARM_DECL:
357 case FIELD_DECL:
358 case FUNCTION_DECL:
359 case OVERLOAD:
360 case CONST_DECL:
361 case TEMPLATE_DECL:
362 pp_cxx_id_expression (pp, t);
363 break;
365 case RESULT_DECL:
366 case TEMPLATE_TYPE_PARM:
367 case TEMPLATE_TEMPLATE_PARM:
368 case TEMPLATE_PARM_INDEX:
369 pp_cxx_unqualified_id (pp, t);
370 break;
372 case STMT_EXPR:
373 pp_cxx_left_paren (pp);
374 pp_cxx_statement (pp, STMT_EXPR_STMT (t));
375 pp_cxx_right_paren (pp);
376 break;
378 default:
379 pp_c_primary_expression (pp_c_base (pp), t);
380 break;
384 /* postfix-expression:
385 primary-expression
386 postfix-expression [ expression ]
387 postfix-expression ( expression-list(opt) )
388 simple-type-specifier ( expression-list(opt) )
389 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
390 typename ::(opt) nested-name-specifier template(opt)
391 template-id ( expression-list(opt) )
392 postfix-expression . template(opt) ::(opt) id-expression
393 postfix-expression -> template(opt) ::(opt) id-expression
394 postfix-expression . pseudo-destructor-name
395 postfix-expression -> pseudo-destructor-name
396 postfix-expression ++
397 postfix-expression --
398 dynamic_cast < type-id > ( expression )
399 static_cast < type-id > ( expression )
400 reinterpret_cast < type-id > ( expression )
401 const_cast < type-id > ( expression )
402 typeid ( expression )
403 typeif ( type-id ) */
405 static void
406 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
408 enum tree_code code = TREE_CODE (t);
410 switch (code)
412 case AGGR_INIT_EXPR:
413 case CALL_EXPR:
415 tree fun = TREE_OPERAND (t, 0);
416 tree args = TREE_OPERAND (t, 1);
417 tree saved_scope = pp->enclosing_scope;
419 if (TREE_CODE (fun) == ADDR_EXPR)
420 fun = TREE_OPERAND (fun, 0);
422 /* In templates, where there is no way to tell whether a given
423 call uses an actual member function. So the parser builds
424 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
425 instantiation time. */
426 if (TREE_CODE (fun) != FUNCTION_DECL)
428 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
430 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
431 ? TREE_OPERAND (t, 2)
432 : TREE_VALUE (args);
434 while (TREE_CODE (object) == NOP_EXPR)
435 object = TREE_OPERAND (object, 0);
437 if (TREE_CODE (object) == ADDR_EXPR)
438 object = TREE_OPERAND (object, 0);
440 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
442 pp_cxx_postfix_expression (pp, object);
443 pp_cxx_dot (pp);
445 else
447 pp_cxx_postfix_expression (pp, object);
448 pp_cxx_arrow (pp);
450 args = TREE_CHAIN (args);
451 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
454 pp_cxx_postfix_expression (pp, fun);
455 pp->enclosing_scope = saved_scope;
456 pp_cxx_call_argument_list (pp, args);
458 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
460 pp_cxx_separate_with (pp, ',');
461 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
463 break;
465 case BASELINK:
466 case VAR_DECL:
467 case PARM_DECL:
468 case FIELD_DECL:
469 case FUNCTION_DECL:
470 case OVERLOAD:
471 case CONST_DECL:
472 case TEMPLATE_DECL:
473 case RESULT_DECL:
474 pp_cxx_primary_expression (pp, t);
475 break;
477 case DYNAMIC_CAST_EXPR:
478 case STATIC_CAST_EXPR:
479 case REINTERPRET_CAST_EXPR:
480 case CONST_CAST_EXPR:
481 if (code == DYNAMIC_CAST_EXPR)
482 pp_cxx_identifier (pp, "dynamic_cast");
483 else if (code == STATIC_CAST_EXPR)
484 pp_cxx_identifier (pp, "static_cast");
485 else if (code == REINTERPRET_CAST_EXPR)
486 pp_cxx_identifier (pp, "reinterpret_cast");
487 else
488 pp_cxx_identifier (pp, "const_cast");
489 pp_cxx_begin_template_argument_list (pp);
490 pp_cxx_type_id (pp, TREE_TYPE (t));
491 pp_cxx_end_template_argument_list (pp);
492 pp_left_paren (pp);
493 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
494 pp_right_paren (pp);
495 break;
497 case EMPTY_CLASS_EXPR:
498 pp_cxx_type_id (pp, TREE_TYPE (t));
499 pp_left_paren (pp);
500 pp_right_paren (pp);
501 break;
503 case TYPEID_EXPR:
504 t = TREE_OPERAND (t, 0);
505 pp_cxx_identifier (pp, "typeid");
506 pp_left_paren (pp);
507 if (TYPE_P (t))
508 pp_cxx_type_id (pp, t);
509 else
510 pp_cxx_expression (pp, t);
511 pp_right_paren (pp);
512 break;
514 case PSEUDO_DTOR_EXPR:
515 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
516 pp_cxx_dot (pp);
517 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
518 pp_cxx_colon_colon (pp);
519 pp_complement (pp);
520 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
521 break;
523 case ARROW_EXPR:
524 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
525 pp_cxx_arrow (pp);
526 break;
528 default:
529 pp_c_postfix_expression (pp_c_base (pp), t);
530 break;
534 /* new-expression:
535 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
536 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
538 new-placement:
539 ( expression-list )
541 new-type-id:
542 type-specifier-seq new-declarator(opt)
544 new-declarator:
545 ptr-operator new-declarator(opt)
546 direct-new-declarator
548 direct-new-declarator
549 [ expression ]
550 direct-new-declarator [ constant-expression ]
552 new-initializer:
553 ( expression-list(opt) ) */
555 static void
556 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
558 enum tree_code code = TREE_CODE (t);
559 switch (code)
561 case NEW_EXPR:
562 case VEC_NEW_EXPR:
563 if (NEW_EXPR_USE_GLOBAL (t))
564 pp_cxx_colon_colon (pp);
565 pp_cxx_identifier (pp, "new");
566 if (TREE_OPERAND (t, 0))
568 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
569 pp_space (pp);
571 /* FIXME: array-types are built with one more element. */
572 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
573 if (TREE_OPERAND (t, 2))
575 pp_left_paren (pp);
576 t = TREE_OPERAND (t, 2);
577 if (TREE_CODE (t) == TREE_LIST)
578 pp_c_expression_list (pp_c_base (pp), t);
579 else if (t == void_zero_node)
580 ; /* OK, empty initializer list. */
581 else
582 pp_cxx_expression (pp, t);
583 pp_right_paren (pp);
585 break;
587 default:
588 pp_unsupported_tree (pp, t);
592 /* delete-expression:
593 ::(opt) delete cast-expression
594 ::(opt) delete [ ] cast-expression */
596 static void
597 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
599 enum tree_code code = TREE_CODE (t);
600 switch (code)
602 case DELETE_EXPR:
603 case VEC_DELETE_EXPR:
604 if (DELETE_EXPR_USE_GLOBAL (t))
605 pp_cxx_colon_colon (pp);
606 pp_cxx_identifier (pp, "delete");
607 if (code == VEC_DELETE_EXPR)
609 pp_left_bracket (pp);
610 pp_right_bracket (pp);
612 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
613 break;
615 default:
616 pp_unsupported_tree (pp, t);
620 /* unary-expression:
621 postfix-expression
622 ++ cast-expression
623 -- cast-expression
624 unary-operator cast-expression
625 sizeof unary-expression
626 sizeof ( type-id )
627 new-expression
628 delete-expression
630 unary-operator: one of
631 * & + - !
633 GNU extensions:
634 __alignof__ unary-expression
635 __alignof__ ( type-id ) */
637 static void
638 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
640 enum tree_code code = TREE_CODE (t);
641 switch (code)
643 case NEW_EXPR:
644 case VEC_NEW_EXPR:
645 pp_cxx_new_expression (pp, t);
646 break;
648 case DELETE_EXPR:
649 case VEC_DELETE_EXPR:
650 pp_cxx_delete_expression (pp, t);
651 break;
653 case SIZEOF_EXPR:
654 case ALIGNOF_EXPR:
655 pp_cxx_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
656 pp_cxx_whitespace (pp);
657 if (TYPE_P (TREE_OPERAND (t, 0)))
659 pp_cxx_left_paren (pp);
660 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
661 pp_cxx_right_paren (pp);
663 else
664 pp_unary_expression (pp, TREE_OPERAND (t, 0));
665 break;
667 case UNARY_PLUS_EXPR:
668 pp_plus (pp);
669 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
670 break;
672 default:
673 pp_c_unary_expression (pp_c_base (pp), t);
674 break;
678 /* cast-expression:
679 unary-expression
680 ( type-id ) cast-expression */
682 static void
683 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
685 switch (TREE_CODE (t))
687 case CAST_EXPR:
688 pp_cxx_type_id (pp, TREE_TYPE (t));
689 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
690 break;
692 default:
693 pp_c_cast_expression (pp_c_base (pp), t);
694 break;
698 /* pm-expression:
699 cast-expression
700 pm-expression .* cast-expression
701 pm-expression ->* cast-expression */
703 static void
704 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
706 switch (TREE_CODE (t))
708 /* Handle unfortunate OFFESET_REF overloading here. */
709 case OFFSET_REF:
710 if (TYPE_P (TREE_OPERAND (t, 0)))
712 pp_cxx_qualified_id (pp, t);
713 break;
715 /* Else fall through. */
716 case MEMBER_REF:
717 case DOTSTAR_EXPR:
718 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
719 pp_cxx_dot (pp);
720 pp_star(pp);
721 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
722 break;
725 default:
726 pp_cxx_cast_expression (pp, t);
727 break;
731 /* multiplicative-expression:
732 pm-expression
733 multiplicative-expression * pm-expression
734 multiplicative-expression / pm-expression
735 multiplicative-expression % pm-expression */
737 static void
738 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
740 enum tree_code code = TREE_CODE (e);
741 switch (code)
743 case MULT_EXPR:
744 case TRUNC_DIV_EXPR:
745 case TRUNC_MOD_EXPR:
746 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
747 pp_space (pp);
748 if (code == MULT_EXPR)
749 pp_star (pp);
750 else if (code == TRUNC_DIV_EXPR)
751 pp_slash (pp);
752 else
753 pp_modulo (pp);
754 pp_space (pp);
755 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
756 break;
758 default:
759 pp_cxx_pm_expression (pp, e);
760 break;
764 /* conditional-expression:
765 logical-or-expression
766 logical-or-expression ? expression : assignment-expression */
768 static void
769 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
771 if (TREE_CODE (e) == COND_EXPR)
773 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
774 pp_space (pp);
775 pp_question (pp);
776 pp_space (pp);
777 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
778 pp_space (pp);
779 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
781 else
782 pp_c_logical_or_expression (pp_c_base (pp), e);
785 /* Pretty-print a compound assignment operator token as indicated by T. */
787 static void
788 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
790 const char *op;
792 switch (TREE_CODE (t))
794 case NOP_EXPR:
795 op = "=";
796 break;
798 case PLUS_EXPR:
799 op = "+=";
800 break;
802 case MINUS_EXPR:
803 op = "-=";
804 break;
806 case TRUNC_DIV_EXPR:
807 op = "/=";
808 break;
810 case TRUNC_MOD_EXPR:
811 op = "%=";
812 break;
814 default:
815 op = tree_code_name[TREE_CODE (t)];
816 break;
819 pp_cxx_identifier (pp, op);
823 /* assignment-expression:
824 conditional-expression
825 logical-or-expression assignment-operator assignment-expression
826 throw-expression
828 throw-expression:
829 throw assignment-expression(opt)
831 assignment-operator: one of
832 = *= /= %= += -= >>= <<= &= ^= |= */
834 static void
835 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
837 switch (TREE_CODE (e))
839 case MODIFY_EXPR:
840 case INIT_EXPR:
841 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
842 pp_space (pp);
843 pp_equal (pp);
844 pp_space (pp);
845 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
846 break;
848 case THROW_EXPR:
849 pp_cxx_identifier (pp, "throw");
850 if (TREE_OPERAND (e, 0))
851 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
852 break;
854 case MODOP_EXPR:
855 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
856 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
857 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
858 break;
860 default:
861 pp_cxx_conditional_expression (pp, e);
862 break;
866 static void
867 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
869 switch (TREE_CODE (t))
871 case STRING_CST:
872 case INTEGER_CST:
873 case REAL_CST:
874 pp_cxx_constant (pp, t);
875 break;
877 case RESULT_DECL:
878 pp_cxx_unqualified_id (pp, t);
879 break;
881 #if 0
882 case OFFSET_REF:
883 #endif
884 case SCOPE_REF:
885 case PTRMEM_CST:
886 pp_cxx_qualified_id (pp, t);
887 break;
889 case OVERLOAD:
890 t = OVL_CURRENT (t);
891 case VAR_DECL:
892 case PARM_DECL:
893 case FIELD_DECL:
894 case CONST_DECL:
895 case FUNCTION_DECL:
896 case BASELINK:
897 case TEMPLATE_DECL:
898 case TEMPLATE_TYPE_PARM:
899 case TEMPLATE_PARM_INDEX:
900 case TEMPLATE_TEMPLATE_PARM:
901 case STMT_EXPR:
902 pp_cxx_primary_expression (pp, t);
903 break;
905 case CALL_EXPR:
906 case DYNAMIC_CAST_EXPR:
907 case STATIC_CAST_EXPR:
908 case REINTERPRET_CAST_EXPR:
909 case CONST_CAST_EXPR:
910 #if 0
911 case MEMBER_REF:
912 #endif
913 case EMPTY_CLASS_EXPR:
914 case TYPEID_EXPR:
915 case PSEUDO_DTOR_EXPR:
916 case AGGR_INIT_EXPR:
917 case ARROW_EXPR:
918 pp_cxx_postfix_expression (pp, t);
919 break;
921 case NEW_EXPR:
922 case VEC_NEW_EXPR:
923 pp_cxx_new_expression (pp, t);
924 break;
926 case DELETE_EXPR:
927 case VEC_DELETE_EXPR:
928 pp_cxx_delete_expression (pp, t);
929 break;
931 case SIZEOF_EXPR:
932 case ALIGNOF_EXPR:
933 pp_cxx_unary_expression (pp, t);
934 break;
936 case CAST_EXPR:
937 pp_cxx_cast_expression (pp, t);
938 break;
940 case OFFSET_REF:
941 case MEMBER_REF:
942 case DOTSTAR_EXPR:
943 pp_cxx_pm_expression (pp, t);
944 break;
946 case MULT_EXPR:
947 case TRUNC_DIV_EXPR:
948 case TRUNC_MOD_EXPR:
949 pp_cxx_multiplicative_expression (pp, t);
950 break;
952 case COND_EXPR:
953 pp_cxx_conditional_expression (pp, t);
954 break;
956 case MODIFY_EXPR:
957 case INIT_EXPR:
958 case THROW_EXPR:
959 case MODOP_EXPR:
960 pp_cxx_assignment_expression (pp, t);
961 break;
963 case NON_DEPENDENT_EXPR:
964 case MUST_NOT_THROW_EXPR:
965 pp_cxx_expression (pp, t);
966 break;
968 default:
969 pp_c_expression (pp_c_base (pp), t);
970 break;
975 /* Declarations. */
977 /* function-specifier:
978 inline
979 virtual
980 explicit */
982 static void
983 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
985 switch (TREE_CODE (t))
987 case FUNCTION_DECL:
988 if (DECL_VIRTUAL_P (t))
989 pp_cxx_identifier (pp, "virtual");
990 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
991 pp_cxx_identifier (pp, "explicit");
992 else
993 pp_c_function_specifier (pp_c_base (pp), t);
995 default:
996 break;
1000 /* decl-specifier-seq:
1001 decl-specifier-seq(opt) decl-specifier
1003 decl-specifier:
1004 storage-class-specifier
1005 type-specifier
1006 function-specifier
1007 friend
1008 typedef */
1010 static void
1011 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1013 switch (TREE_CODE (t))
1015 case VAR_DECL:
1016 case PARM_DECL:
1017 case CONST_DECL:
1018 case FIELD_DECL:
1019 pp_cxx_storage_class_specifier (pp, t);
1020 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1021 break;
1023 case TYPE_DECL:
1024 pp_cxx_identifier (pp, "typedef");
1025 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1026 break;
1028 case RECORD_TYPE:
1029 if (TYPE_PTRMEMFUNC_P (t))
1031 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1032 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1033 pp_cxx_whitespace (pp);
1034 pp_cxx_ptr_operator (pp, t);
1036 break;
1038 case FUNCTION_DECL:
1039 /* Constructors don't have return types. And conversion functions
1040 do not have a type-specifier in their return types. */
1041 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1042 pp_cxx_function_specifier (pp, t);
1043 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1044 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1045 else
1046 default:
1047 pp_c_declaration_specifiers (pp_c_base (pp), t);
1048 break;
1052 /* simple-type-specifier:
1053 ::(opt) nested-name-specifier(opt) type-name
1054 ::(opt) nested-name-specifier(opt) template(opt) template-id
1055 char
1056 wchar_t
1057 bool
1058 short
1060 long
1061 signed
1062 unsigned
1063 float
1064 double
1065 void */
1067 static void
1068 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1070 switch (TREE_CODE (t))
1072 case RECORD_TYPE:
1073 case UNION_TYPE:
1074 case ENUMERAL_TYPE:
1075 pp_cxx_qualified_id (pp, t);
1076 break;
1078 case TEMPLATE_TYPE_PARM:
1079 case TEMPLATE_TEMPLATE_PARM:
1080 case TEMPLATE_PARM_INDEX:
1081 pp_cxx_unqualified_id (pp, t);
1082 break;
1084 case TYPENAME_TYPE:
1085 pp_cxx_identifier (pp, "typename");
1086 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1087 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1088 break;
1090 default:
1091 pp_c_type_specifier (pp_c_base (pp), t);
1092 break;
1096 /* type-specifier-seq:
1097 type-specifier type-specifier-seq(opt)
1099 type-specifier:
1100 simple-type-specifier
1101 class-specifier
1102 enum-specifier
1103 elaborated-type-specifier
1104 cv-qualifier */
1106 static void
1107 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1109 switch (TREE_CODE (t))
1111 case TEMPLATE_DECL:
1112 case TEMPLATE_TYPE_PARM:
1113 case TEMPLATE_TEMPLATE_PARM:
1114 case TYPE_DECL:
1115 case BOUND_TEMPLATE_TEMPLATE_PARM:
1116 pp_cxx_cv_qualifier_seq (pp, t);
1117 pp_cxx_simple_type_specifier (pp, t);
1118 break;
1120 case METHOD_TYPE:
1121 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1122 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1123 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1124 break;
1126 default:
1127 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1128 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1132 /* ptr-operator:
1133 * cv-qualifier-seq(opt)
1135 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1137 static void
1138 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1140 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1141 t = TREE_TYPE (t);
1142 switch (TREE_CODE (t))
1144 case REFERENCE_TYPE:
1145 case POINTER_TYPE:
1146 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1147 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1148 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1149 if (TREE_CODE (t) == POINTER_TYPE)
1151 pp_star (pp);
1152 pp_cxx_cv_qualifier_seq (pp, t);
1154 else
1155 pp_ampersand (pp);
1156 break;
1158 case RECORD_TYPE:
1159 if (TYPE_PTRMEMFUNC_P (t))
1161 pp_cxx_left_paren (pp);
1162 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1163 pp_star (pp);
1164 break;
1166 case OFFSET_TYPE:
1167 if (TYPE_PTR_TO_MEMBER_P (t))
1169 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1170 pp_cxx_left_paren (pp);
1171 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1172 pp_star (pp);
1173 pp_cxx_cv_qualifier_seq (pp, t);
1174 break;
1176 /* else fall through. */
1178 default:
1179 pp_unsupported_tree (pp, t);
1180 break;
1184 static inline tree
1185 pp_cxx_implicit_parameter_type (tree mf)
1187 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1191 parameter-declaration:
1192 decl-specifier-seq declarator
1193 decl-specifier-seq declarator = assignment-expression
1194 decl-specifier-seq abstract-declarator(opt)
1195 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1197 static inline void
1198 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1200 pp_cxx_decl_specifier_seq (pp, t);
1201 if (TYPE_P (t))
1202 pp_cxx_abstract_declarator (pp, t);
1203 else
1204 pp_cxx_declarator (pp, t);
1207 /* parameter-declaration-clause:
1208 parameter-declaration-list(opt) ...(opt)
1209 parameter-declaration-list , ...
1211 parameter-declaration-list:
1212 parameter-declaration
1213 parameter-declaration-list , parameter-declaration */
1215 static void
1216 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1218 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1219 tree types =
1220 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1221 const bool abstract = args == NULL
1222 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1223 bool first = true;
1225 /* Skip artificial parameter for nonstatic member functions. */
1226 if (TREE_CODE (t) == METHOD_TYPE)
1227 types = TREE_CHAIN (types);
1229 pp_cxx_left_paren (pp);
1230 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1232 if (!first)
1233 pp_cxx_separate_with (pp, ',');
1234 first = false;
1235 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1236 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1238 pp_cxx_whitespace (pp);
1239 pp_equal (pp);
1240 pp_cxx_whitespace (pp);
1241 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1244 pp_cxx_right_paren (pp);
1247 /* exception-specification:
1248 throw ( type-id-list(opt) )
1250 type-id-list
1251 type-id
1252 type-id-list , type-id */
1254 static void
1255 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1257 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1259 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1260 return;
1261 pp_cxx_identifier (pp, "throw");
1262 pp_cxx_left_paren (pp);
1263 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1265 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1266 if (TREE_CHAIN (ex_spec))
1267 pp_cxx_separate_with (pp, ',');
1269 pp_cxx_right_paren (pp);
1272 /* direct-declarator:
1273 declarator-id
1274 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1275 exception-specification(opt)
1276 direct-declaration [ constant-expression(opt) ]
1277 ( declarator ) */
1279 static void
1280 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1282 switch (TREE_CODE (t))
1284 case VAR_DECL:
1285 case PARM_DECL:
1286 case CONST_DECL:
1287 case FIELD_DECL:
1288 if (DECL_NAME (t))
1290 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1291 pp_cxx_id_expression (pp, DECL_NAME (t));
1293 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1294 break;
1296 case FUNCTION_DECL:
1297 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1298 pp_cxx_id_expression (pp, t);
1299 pp_cxx_parameter_declaration_clause (pp, t);
1301 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1303 pp_base (pp)->padding = pp_before;
1304 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1307 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1308 break;
1310 case TYPENAME_TYPE:
1311 case TEMPLATE_DECL:
1312 case TEMPLATE_TYPE_PARM:
1313 case TEMPLATE_PARM_INDEX:
1314 case TEMPLATE_TEMPLATE_PARM:
1315 break;
1317 default:
1318 pp_c_direct_declarator (pp_c_base (pp), t);
1319 break;
1323 /* declarator:
1324 direct-declarator
1325 ptr-operator declarator */
1327 static void
1328 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1330 pp_cxx_direct_declarator (pp, t);
1333 /* ctor-initializer:
1334 : mem-initializer-list
1336 mem-initializer-list:
1337 mem-initializer
1338 mem-initializer , mem-initializer-list
1340 mem-initializer:
1341 mem-initializer-id ( expression-list(opt) )
1343 mem-initializer-id:
1344 ::(opt) nested-name-specifier(opt) class-name
1345 identifier */
1347 static void
1348 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1350 t = TREE_OPERAND (t, 0);
1351 pp_cxx_whitespace (pp);
1352 pp_colon (pp);
1353 pp_cxx_whitespace (pp);
1354 for (; t; t = TREE_CHAIN (t))
1356 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1357 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1358 if (TREE_CHAIN (t))
1359 pp_cxx_separate_with (pp, ',');
1363 /* function-definition:
1364 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1365 decl-specifier-seq(opt) declarator function-try-block */
1367 static void
1368 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1370 tree saved_scope = pp->enclosing_scope;
1371 pp_cxx_decl_specifier_seq (pp, t);
1372 pp_cxx_declarator (pp, t);
1373 pp_needs_newline (pp) = true;
1374 pp->enclosing_scope = DECL_CONTEXT (t);
1375 if (DECL_SAVED_TREE (t))
1376 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1377 else
1379 pp_cxx_semicolon (pp);
1380 pp_needs_newline (pp) = true;
1382 pp_flush (pp);
1383 pp->enclosing_scope = saved_scope;
1386 /* abstract-declarator:
1387 ptr-operator abstract-declarator(opt)
1388 direct-abstract-declarator */
1390 static void
1391 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1393 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1394 pp_cxx_right_paren (pp);
1395 else if (POINTER_TYPE_P (t))
1397 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1398 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1399 pp_cxx_right_paren (pp);
1400 t = TREE_TYPE (t);
1402 pp_cxx_direct_abstract_declarator (pp, t);
1405 /* direct-abstract-declarator:
1406 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1407 cv-qualifier-seq(opt) exception-specification(opt)
1408 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1409 ( abstract-declarator ) */
1411 static void
1412 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1414 switch (TREE_CODE (t))
1416 case REFERENCE_TYPE:
1417 pp_cxx_abstract_declarator (pp, t);
1418 break;
1420 case RECORD_TYPE:
1421 if (TYPE_PTRMEMFUNC_P (t))
1422 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1423 break;
1425 case METHOD_TYPE:
1426 case FUNCTION_TYPE:
1427 pp_cxx_parameter_declaration_clause (pp, t);
1428 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1429 if (TREE_CODE (t) == METHOD_TYPE)
1431 pp_base (pp)->padding = pp_before;
1432 pp_cxx_cv_qualifier_seq
1433 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1435 pp_cxx_exception_specification (pp, t);
1436 break;
1438 case TYPENAME_TYPE:
1439 case TEMPLATE_TYPE_PARM:
1440 case TEMPLATE_TEMPLATE_PARM:
1441 case BOUND_TEMPLATE_TEMPLATE_PARM:
1442 case UNBOUND_CLASS_TEMPLATE:
1443 break;
1445 default:
1446 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1447 break;
1451 /* type-id:
1452 type-specifier-seq abstract-declarator(opt) */
1454 static void
1455 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1457 pp_flags saved_flags = pp_c_base (pp)->flags;
1458 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1460 switch (TREE_CODE (t))
1462 case TYPE_DECL:
1463 case UNION_TYPE:
1464 case RECORD_TYPE:
1465 case ENUMERAL_TYPE:
1466 case TYPENAME_TYPE:
1467 case BOUND_TEMPLATE_TEMPLATE_PARM:
1468 case UNBOUND_CLASS_TEMPLATE:
1469 case TEMPLATE_TEMPLATE_PARM:
1470 case TEMPLATE_TYPE_PARM:
1471 case TEMPLATE_PARM_INDEX:
1472 case TEMPLATE_DECL:
1473 case TYPEOF_TYPE:
1474 case TEMPLATE_ID_EXPR:
1475 pp_cxx_type_specifier_seq (pp, t);
1476 break;
1478 default:
1479 pp_c_type_id (pp_c_base (pp), t);
1480 break;
1483 pp_c_base (pp)->flags = saved_flags;
1486 /* template-argument-list:
1487 template-argument
1488 template-argument-list, template-argument
1490 template-argument:
1491 assignment-expression
1492 type-id
1493 template-name */
1495 static void
1496 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1498 int i;
1499 if (t == NULL)
1500 return;
1501 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1503 tree arg = TREE_VEC_ELT (t, i);
1504 if (i != 0)
1505 pp_cxx_separate_with (pp, ',');
1506 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1507 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1508 pp_cxx_type_id (pp, arg);
1509 else
1510 pp_cxx_expression (pp, arg);
1515 static void
1516 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1518 t = DECL_EXPR_DECL (t);
1519 pp_cxx_type_specifier_seq (pp, t);
1520 if (TYPE_P (t))
1521 pp_cxx_abstract_declarator (pp, t);
1522 else
1523 pp_cxx_declarator (pp, t);
1526 /* Statements. */
1528 static void
1529 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1531 switch (TREE_CODE (t))
1533 case CTOR_INITIALIZER:
1534 pp_cxx_ctor_initializer (pp, t);
1535 break;
1537 case USING_STMT:
1538 pp_cxx_identifier (pp, "using");
1539 pp_cxx_identifier (pp, "namespace");
1540 if (DECL_CONTEXT (t))
1541 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1542 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1543 break;
1545 case USING_DECL:
1546 pp_cxx_identifier (pp, "using");
1547 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1548 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1549 break;
1551 case EH_SPEC_BLOCK:
1552 break;
1554 /* try-block:
1555 try compound-statement handler-seq */
1556 case TRY_BLOCK:
1557 pp_maybe_newline_and_indent (pp, 0);
1558 pp_cxx_identifier (pp, "try");
1559 pp_newline_and_indent (pp, 3);
1560 pp_cxx_statement (pp, TRY_STMTS (t));
1561 pp_newline_and_indent (pp, -3);
1562 if (CLEANUP_P (t))
1564 else
1565 pp_cxx_statement (pp, TRY_HANDLERS (t));
1566 break;
1569 handler-seq:
1570 handler handler-seq(opt)
1572 handler:
1573 catch ( exception-declaration ) compound-statement
1575 exception-declaration:
1576 type-specifier-seq declarator
1577 type-specifier-seq abstract-declarator
1578 ... */
1579 case HANDLER:
1580 pp_cxx_identifier (pp, "catch");
1581 pp_cxx_left_paren (pp);
1582 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1583 pp_cxx_right_paren (pp);
1584 pp_indentation (pp) += 3;
1585 pp_needs_newline (pp) = true;
1586 pp_cxx_statement (pp, HANDLER_BODY (t));
1587 pp_indentation (pp) -= 3;
1588 pp_needs_newline (pp) = true;
1589 break;
1591 /* selection-statement:
1592 if ( expression ) statement
1593 if ( expression ) statement else statement */
1594 case IF_STMT:
1595 pp_cxx_identifier (pp, "if");
1596 pp_cxx_whitespace (pp);
1597 pp_cxx_left_paren (pp);
1598 pp_cxx_expression (pp, IF_COND (t));
1599 pp_cxx_right_paren (pp);
1600 pp_newline_and_indent (pp, 2);
1601 pp_cxx_statement (pp, THEN_CLAUSE (t));
1602 pp_newline_and_indent (pp, -2);
1603 if (ELSE_CLAUSE (t))
1605 tree else_clause = ELSE_CLAUSE (t);
1606 pp_cxx_identifier (pp, "else");
1607 if (TREE_CODE (else_clause) == IF_STMT)
1608 pp_cxx_whitespace (pp);
1609 else
1610 pp_newline_and_indent (pp, 2);
1611 pp_cxx_statement (pp, else_clause);
1612 if (TREE_CODE (else_clause) != IF_STMT)
1613 pp_newline_and_indent (pp, -2);
1615 break;
1617 case SWITCH_STMT:
1618 pp_cxx_identifier (pp, "switch");
1619 pp_space (pp);
1620 pp_cxx_left_paren (pp);
1621 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1622 pp_cxx_right_paren (pp);
1623 pp_indentation (pp) += 3;
1624 pp_needs_newline (pp) = true;
1625 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1626 pp_newline_and_indent (pp, -3);
1627 break;
1629 /* iteration-statement:
1630 while ( expression ) statement
1631 do statement while ( expression ) ;
1632 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1633 for ( declaration expression(opt) ; expression(opt) ) statement */
1634 case WHILE_STMT:
1635 pp_cxx_identifier (pp, "while");
1636 pp_space (pp);
1637 pp_cxx_left_paren (pp);
1638 pp_cxx_expression (pp, WHILE_COND (t));
1639 pp_cxx_right_paren (pp);
1640 pp_newline_and_indent (pp, 3);
1641 pp_cxx_statement (pp, WHILE_BODY (t));
1642 pp_indentation (pp) -= 3;
1643 pp_needs_newline (pp) = true;
1644 break;
1646 case DO_STMT:
1647 pp_cxx_identifier (pp, "do");
1648 pp_newline_and_indent (pp, 3);
1649 pp_cxx_statement (pp, DO_BODY (t));
1650 pp_newline_and_indent (pp, -3);
1651 pp_cxx_identifier (pp, "while");
1652 pp_space (pp);
1653 pp_cxx_left_paren (pp);
1654 pp_cxx_expression (pp, DO_COND (t));
1655 pp_cxx_right_paren (pp);
1656 pp_cxx_semicolon (pp);
1657 pp_needs_newline (pp) = true;
1658 break;
1660 case FOR_STMT:
1661 pp_cxx_identifier (pp, "for");
1662 pp_space (pp);
1663 pp_cxx_left_paren (pp);
1664 if (FOR_INIT_STMT (t))
1665 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1666 else
1667 pp_cxx_semicolon (pp);
1668 pp_needs_newline (pp) = false;
1669 pp_cxx_whitespace (pp);
1670 if (FOR_COND (t))
1671 pp_cxx_expression (pp, FOR_COND (t));
1672 pp_cxx_semicolon (pp);
1673 pp_needs_newline (pp) = false;
1674 pp_cxx_whitespace (pp);
1675 if (FOR_EXPR (t))
1676 pp_cxx_expression (pp, FOR_EXPR (t));
1677 pp_cxx_right_paren (pp);
1678 pp_newline_and_indent (pp, 3);
1679 pp_cxx_statement (pp, FOR_BODY (t));
1680 pp_indentation (pp) -= 3;
1681 pp_needs_newline (pp) = true;
1682 break;
1684 /* jump-statement:
1685 goto identifier;
1686 continue ;
1687 return expression(opt) ; */
1688 case BREAK_STMT:
1689 case CONTINUE_STMT:
1690 pp_identifier (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1691 pp_cxx_semicolon (pp);
1692 pp_needs_newline (pp) = true;
1693 break;
1695 /* expression-statement:
1696 expression(opt) ; */
1697 case EXPR_STMT:
1698 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1699 pp_cxx_semicolon (pp);
1700 pp_needs_newline (pp) = true;
1701 break;
1703 case CLEANUP_STMT:
1704 pp_cxx_identifier (pp, "try");
1705 pp_newline_and_indent (pp, 2);
1706 pp_cxx_statement (pp, CLEANUP_BODY (t));
1707 pp_newline_and_indent (pp, -2);
1708 pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1709 pp_newline_and_indent (pp, 2);
1710 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1711 pp_newline_and_indent (pp, -2);
1712 break;
1714 default:
1715 pp_c_statement (pp_c_base (pp), t);
1716 break;
1720 /* original-namespace-definition:
1721 namespace identifier { namespace-body }
1723 As an edge case, we also handle unnamed namespace definition here. */
1725 static void
1726 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1728 pp_cxx_identifier (pp, "namespace");
1729 if (DECL_CONTEXT (t))
1730 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1731 if (DECL_NAME (t))
1732 pp_cxx_unqualified_id (pp, t);
1733 pp_cxx_whitespace (pp);
1734 pp_cxx_left_brace (pp);
1735 /* We do not print the namespace-body. */
1736 pp_cxx_whitespace (pp);
1737 pp_cxx_right_brace (pp);
1740 /* namespace-alias:
1741 identifier
1743 namespace-alias-definition:
1744 namespace identifier = qualified-namespace-specifier ;
1746 qualified-namespace-specifier:
1747 ::(opt) nested-name-specifier(opt) namespace-name */
1749 static void
1750 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1752 pp_cxx_identifier (pp, "namespace");
1753 if (DECL_CONTEXT (t))
1754 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1755 pp_cxx_unqualified_id (pp, t);
1756 pp_cxx_whitespace (pp);
1757 pp_equal (pp);
1758 pp_cxx_whitespace (pp);
1759 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1760 pp_cxx_nested_name_specifier (pp,
1761 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1762 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1763 pp_cxx_semicolon (pp);
1766 /* simple-declaration:
1767 decl-specifier-seq(opt) init-declarator-list(opt) */
1769 static void
1770 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1772 pp_cxx_decl_specifier_seq (pp, t);
1773 pp_cxx_init_declarator (pp, t);
1774 pp_cxx_semicolon (pp);
1775 pp_needs_newline (pp) = true;
1779 template-parameter-list:
1780 template-parameter
1781 template-parameter-list , template-parameter */
1783 static inline void
1784 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1786 const int n = TREE_VEC_LENGTH (t);
1787 int i;
1788 for (i = 0; i < n; ++i)
1790 if (i)
1791 pp_cxx_separate_with (pp, ',');
1792 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1796 /* template-parameter:
1797 type-parameter
1798 parameter-declaration
1800 type-parameter:
1801 class identifier(opt)
1802 class identifier(op) = type-id
1803 typename identifier(opt)
1804 typename identifier(opt) = type-id
1805 template < template-parameter-list > class identifier(opt)
1806 template < template-parameter-list > class identifier(opt) = template-name
1809 static void
1810 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1812 tree parameter = TREE_VALUE (t);
1813 switch (TREE_CODE (parameter))
1815 case TYPE_DECL:
1816 pp_cxx_identifier (pp, "class");
1817 if (DECL_NAME (parameter))
1818 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1819 /* FIXME: Chech if we should print also default argument. */
1820 break;
1822 case PARM_DECL:
1823 pp_cxx_parameter_declaration (pp, parameter);
1824 break;
1826 case TEMPLATE_DECL:
1827 break;
1829 default:
1830 pp_unsupported_tree (pp, t);
1831 break;
1835 /* Pretty-print a template parameter in the canonical form
1836 "template-parameter-<level>-<position in parameter list>". */
1838 void
1839 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1841 const enum tree_code code = TREE_CODE (parm);
1843 /* Brings type template parameters to the canonical forms. */
1844 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1845 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1846 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1848 pp_cxx_begin_template_argument_list (pp);
1849 pp_cxx_identifier (pp, "template-parameter-");
1850 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1851 pp_minus (pp);
1852 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1853 pp_cxx_end_template_argument_list (pp);
1857 template-declaration:
1858 export(opt) template < template-parameter-list > declaration */
1860 static void
1861 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1863 tree tmpl = most_general_template (t);
1864 tree level;
1865 int i = 0;
1867 pp_maybe_newline_and_indent (pp, 0);
1868 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1870 pp_cxx_identifier (pp, "template");
1871 pp_cxx_begin_template_argument_list (pp);
1872 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1873 pp_cxx_end_template_argument_list (pp);
1874 pp_newline_and_indent (pp, 3);
1875 i += 3;
1877 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1878 pp_cxx_function_definition (pp, t);
1879 else
1880 pp_cxx_simple_declaration (pp, t);
1883 static void
1884 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1886 pp_unsupported_tree (pp, t);
1889 static void
1890 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1892 pp_unsupported_tree (pp, t);
1896 declaration:
1897 block-declaration
1898 function-definition
1899 template-declaration
1900 explicit-instantiation
1901 explicit-specialization
1902 linkage-specification
1903 namespace-definition
1905 block-declaration:
1906 simple-declaration
1907 asm-definition
1908 namespace-alias-definition
1909 using-declaration
1910 using-directive */
1911 void
1912 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1914 if (!DECL_LANG_SPECIFIC (t))
1915 pp_cxx_simple_declaration (pp, t);
1916 else if (DECL_USE_TEMPLATE (t))
1917 switch (DECL_USE_TEMPLATE (t))
1919 case 1:
1920 pp_cxx_template_declaration (pp, t);
1921 break;
1923 case 2:
1924 pp_cxx_explicit_specialization (pp, t);
1925 break;
1927 case 3:
1928 pp_cxx_explicit_instantiation (pp, t);
1929 break;
1931 default:
1932 break;
1934 else switch (TREE_CODE (t))
1936 case VAR_DECL:
1937 case TYPE_DECL:
1938 pp_cxx_simple_declaration (pp, t);
1939 break;
1941 case FUNCTION_DECL:
1942 if (DECL_SAVED_TREE (t))
1943 pp_cxx_function_definition (pp, t);
1944 else
1945 pp_cxx_simple_declaration (pp, t);
1946 break;
1948 case NAMESPACE_DECL:
1949 if (DECL_NAMESPACE_ALIAS (t))
1950 pp_cxx_namespace_alias_definition (pp, t);
1951 else
1952 pp_cxx_original_namespace_definition (pp, t);
1953 break;
1955 default:
1956 pp_unsupported_tree (pp, t);
1957 break;
1962 typedef c_pretty_print_fn pp_fun;
1964 /* Initialization of a C++ pretty-printer object. */
1966 void
1967 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1969 pp_c_pretty_printer_init (pp_c_base (pp));
1970 pp_set_line_maximum_length (pp, 0);
1972 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1973 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1974 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1975 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1976 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1977 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1978 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1979 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1980 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1981 pp->c_base.direct_abstract_declarator =
1982 (pp_fun) pp_cxx_direct_abstract_declarator;
1983 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1985 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1987 pp->c_base.constant = (pp_fun) pp_cxx_constant;
1988 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1989 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1990 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1991 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1992 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1993 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1994 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1995 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1996 pp->enclosing_scope = global_namespace;