2007-02-15 Sandra Loosemore <sandra@codesourcery.com>
[official-gcc.git] / gcc / cp / cxx-pretty-print.c
blob5c13362df9545a10e090c5cc7a6021aeb33c44c3
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 = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
416 : CALL_EXPR_FN (t));
417 tree saved_scope = pp->enclosing_scope;
418 bool skipfirst = false;
419 tree arg;
421 if (TREE_CODE (fun) == ADDR_EXPR)
422 fun = TREE_OPERAND (fun, 0);
424 /* In templates, where there is no way to tell whether a given
425 call uses an actual member function. So the parser builds
426 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
427 instantiation time. */
428 if (TREE_CODE (fun) != FUNCTION_DECL)
430 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
432 tree object = (code == AGGR_INIT_EXPR
433 ? (AGGR_INIT_VIA_CTOR_P (t)
434 ? AGGR_INIT_EXPR_SLOT (t)
435 : AGGR_INIT_EXPR_ARG (t, 0))
436 : CALL_EXPR_ARG (t, 0));
438 while (TREE_CODE (object) == NOP_EXPR)
439 object = TREE_OPERAND (object, 0);
441 if (TREE_CODE (object) == ADDR_EXPR)
442 object = TREE_OPERAND (object, 0);
444 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
446 pp_cxx_postfix_expression (pp, object);
447 pp_cxx_dot (pp);
449 else
451 pp_cxx_postfix_expression (pp, object);
452 pp_cxx_arrow (pp);
454 skipfirst = true;
455 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
458 pp_cxx_postfix_expression (pp, fun);
459 pp->enclosing_scope = saved_scope;
460 pp_cxx_left_paren (pp);
461 if (code == AGGR_INIT_EXPR)
463 aggr_init_expr_arg_iterator iter;
464 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
466 if (skipfirst)
467 skipfirst = false;
468 else
470 pp_cxx_expression (pp, arg);
471 if (more_aggr_init_expr_args_p (&iter))
472 pp_cxx_separate_with (pp, ',');
476 else
478 call_expr_arg_iterator iter;
479 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
481 if (skipfirst)
482 skipfirst = false;
483 else
485 pp_cxx_expression (pp, arg);
486 if (more_call_expr_args_p (&iter))
487 pp_cxx_separate_with (pp, ',');
491 pp_cxx_right_paren (pp);
493 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
495 pp_cxx_separate_with (pp, ',');
496 pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t));
498 break;
500 case BASELINK:
501 case VAR_DECL:
502 case PARM_DECL:
503 case FIELD_DECL:
504 case FUNCTION_DECL:
505 case OVERLOAD:
506 case CONST_DECL:
507 case TEMPLATE_DECL:
508 case RESULT_DECL:
509 pp_cxx_primary_expression (pp, t);
510 break;
512 case DYNAMIC_CAST_EXPR:
513 case STATIC_CAST_EXPR:
514 case REINTERPRET_CAST_EXPR:
515 case CONST_CAST_EXPR:
516 if (code == DYNAMIC_CAST_EXPR)
517 pp_cxx_identifier (pp, "dynamic_cast");
518 else if (code == STATIC_CAST_EXPR)
519 pp_cxx_identifier (pp, "static_cast");
520 else if (code == REINTERPRET_CAST_EXPR)
521 pp_cxx_identifier (pp, "reinterpret_cast");
522 else
523 pp_cxx_identifier (pp, "const_cast");
524 pp_cxx_begin_template_argument_list (pp);
525 pp_cxx_type_id (pp, TREE_TYPE (t));
526 pp_cxx_end_template_argument_list (pp);
527 pp_left_paren (pp);
528 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
529 pp_right_paren (pp);
530 break;
532 case EMPTY_CLASS_EXPR:
533 pp_cxx_type_id (pp, TREE_TYPE (t));
534 pp_left_paren (pp);
535 pp_right_paren (pp);
536 break;
538 case TYPEID_EXPR:
539 t = TREE_OPERAND (t, 0);
540 pp_cxx_identifier (pp, "typeid");
541 pp_left_paren (pp);
542 if (TYPE_P (t))
543 pp_cxx_type_id (pp, t);
544 else
545 pp_cxx_expression (pp, t);
546 pp_right_paren (pp);
547 break;
549 case PSEUDO_DTOR_EXPR:
550 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
551 pp_cxx_dot (pp);
552 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
553 pp_cxx_colon_colon (pp);
554 pp_complement (pp);
555 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
556 break;
558 case ARROW_EXPR:
559 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
560 pp_cxx_arrow (pp);
561 break;
563 default:
564 pp_c_postfix_expression (pp_c_base (pp), t);
565 break;
569 /* new-expression:
570 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
571 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
573 new-placement:
574 ( expression-list )
576 new-type-id:
577 type-specifier-seq new-declarator(opt)
579 new-declarator:
580 ptr-operator new-declarator(opt)
581 direct-new-declarator
583 direct-new-declarator
584 [ expression ]
585 direct-new-declarator [ constant-expression ]
587 new-initializer:
588 ( expression-list(opt) ) */
590 static void
591 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
593 enum tree_code code = TREE_CODE (t);
594 switch (code)
596 case NEW_EXPR:
597 case VEC_NEW_EXPR:
598 if (NEW_EXPR_USE_GLOBAL (t))
599 pp_cxx_colon_colon (pp);
600 pp_cxx_identifier (pp, "new");
601 if (TREE_OPERAND (t, 0))
603 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
604 pp_space (pp);
606 /* FIXME: array-types are built with one more element. */
607 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
608 if (TREE_OPERAND (t, 2))
610 pp_left_paren (pp);
611 t = TREE_OPERAND (t, 2);
612 if (TREE_CODE (t) == TREE_LIST)
613 pp_c_expression_list (pp_c_base (pp), t);
614 else if (t == void_zero_node)
615 ; /* OK, empty initializer list. */
616 else
617 pp_cxx_expression (pp, t);
618 pp_right_paren (pp);
620 break;
622 default:
623 pp_unsupported_tree (pp, t);
627 /* delete-expression:
628 ::(opt) delete cast-expression
629 ::(opt) delete [ ] cast-expression */
631 static void
632 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
634 enum tree_code code = TREE_CODE (t);
635 switch (code)
637 case DELETE_EXPR:
638 case VEC_DELETE_EXPR:
639 if (DELETE_EXPR_USE_GLOBAL (t))
640 pp_cxx_colon_colon (pp);
641 pp_cxx_identifier (pp, "delete");
642 if (code == VEC_DELETE_EXPR)
644 pp_left_bracket (pp);
645 pp_right_bracket (pp);
647 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
648 break;
650 default:
651 pp_unsupported_tree (pp, t);
655 /* unary-expression:
656 postfix-expression
657 ++ cast-expression
658 -- cast-expression
659 unary-operator cast-expression
660 sizeof unary-expression
661 sizeof ( type-id )
662 new-expression
663 delete-expression
665 unary-operator: one of
666 * & + - !
668 GNU extensions:
669 __alignof__ unary-expression
670 __alignof__ ( type-id ) */
672 static void
673 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
675 enum tree_code code = TREE_CODE (t);
676 switch (code)
678 case NEW_EXPR:
679 case VEC_NEW_EXPR:
680 pp_cxx_new_expression (pp, t);
681 break;
683 case DELETE_EXPR:
684 case VEC_DELETE_EXPR:
685 pp_cxx_delete_expression (pp, t);
686 break;
688 case SIZEOF_EXPR:
689 case ALIGNOF_EXPR:
690 pp_cxx_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
691 pp_cxx_whitespace (pp);
692 if (TYPE_P (TREE_OPERAND (t, 0)))
694 pp_cxx_left_paren (pp);
695 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
696 pp_cxx_right_paren (pp);
698 else
699 pp_unary_expression (pp, TREE_OPERAND (t, 0));
700 break;
702 case UNARY_PLUS_EXPR:
703 pp_plus (pp);
704 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
705 break;
707 default:
708 pp_c_unary_expression (pp_c_base (pp), t);
709 break;
713 /* cast-expression:
714 unary-expression
715 ( type-id ) cast-expression */
717 static void
718 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
720 switch (TREE_CODE (t))
722 case CAST_EXPR:
723 pp_cxx_type_id (pp, TREE_TYPE (t));
724 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
725 break;
727 default:
728 pp_c_cast_expression (pp_c_base (pp), t);
729 break;
733 /* pm-expression:
734 cast-expression
735 pm-expression .* cast-expression
736 pm-expression ->* cast-expression */
738 static void
739 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
741 switch (TREE_CODE (t))
743 /* Handle unfortunate OFFESET_REF overloading here. */
744 case OFFSET_REF:
745 if (TYPE_P (TREE_OPERAND (t, 0)))
747 pp_cxx_qualified_id (pp, t);
748 break;
750 /* Else fall through. */
751 case MEMBER_REF:
752 case DOTSTAR_EXPR:
753 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
754 pp_cxx_dot (pp);
755 pp_star(pp);
756 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
757 break;
760 default:
761 pp_cxx_cast_expression (pp, t);
762 break;
766 /* multiplicative-expression:
767 pm-expression
768 multiplicative-expression * pm-expression
769 multiplicative-expression / pm-expression
770 multiplicative-expression % pm-expression */
772 static void
773 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
775 enum tree_code code = TREE_CODE (e);
776 switch (code)
778 case MULT_EXPR:
779 case TRUNC_DIV_EXPR:
780 case TRUNC_MOD_EXPR:
781 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
782 pp_space (pp);
783 if (code == MULT_EXPR)
784 pp_star (pp);
785 else if (code == TRUNC_DIV_EXPR)
786 pp_slash (pp);
787 else
788 pp_modulo (pp);
789 pp_space (pp);
790 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
791 break;
793 default:
794 pp_cxx_pm_expression (pp, e);
795 break;
799 /* conditional-expression:
800 logical-or-expression
801 logical-or-expression ? expression : assignment-expression */
803 static void
804 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
806 if (TREE_CODE (e) == COND_EXPR)
808 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
809 pp_space (pp);
810 pp_question (pp);
811 pp_space (pp);
812 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
813 pp_space (pp);
814 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
816 else
817 pp_c_logical_or_expression (pp_c_base (pp), e);
820 /* Pretty-print a compound assignment operator token as indicated by T. */
822 static void
823 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
825 const char *op;
827 switch (TREE_CODE (t))
829 case NOP_EXPR:
830 op = "=";
831 break;
833 case PLUS_EXPR:
834 op = "+=";
835 break;
837 case MINUS_EXPR:
838 op = "-=";
839 break;
841 case TRUNC_DIV_EXPR:
842 op = "/=";
843 break;
845 case TRUNC_MOD_EXPR:
846 op = "%=";
847 break;
849 default:
850 op = tree_code_name[TREE_CODE (t)];
851 break;
854 pp_cxx_identifier (pp, op);
858 /* assignment-expression:
859 conditional-expression
860 logical-or-expression assignment-operator assignment-expression
861 throw-expression
863 throw-expression:
864 throw assignment-expression(opt)
866 assignment-operator: one of
867 = *= /= %= += -= >>= <<= &= ^= |= */
869 static void
870 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
872 switch (TREE_CODE (e))
874 case MODIFY_EXPR:
875 case INIT_EXPR:
876 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
877 pp_space (pp);
878 pp_equal (pp);
879 pp_space (pp);
880 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
881 break;
883 case THROW_EXPR:
884 pp_cxx_identifier (pp, "throw");
885 if (TREE_OPERAND (e, 0))
886 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
887 break;
889 case MODOP_EXPR:
890 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
891 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
892 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
893 break;
895 default:
896 pp_cxx_conditional_expression (pp, e);
897 break;
901 static void
902 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
904 switch (TREE_CODE (t))
906 case STRING_CST:
907 case INTEGER_CST:
908 case REAL_CST:
909 pp_cxx_constant (pp, t);
910 break;
912 case RESULT_DECL:
913 pp_cxx_unqualified_id (pp, t);
914 break;
916 #if 0
917 case OFFSET_REF:
918 #endif
919 case SCOPE_REF:
920 case PTRMEM_CST:
921 pp_cxx_qualified_id (pp, t);
922 break;
924 case OVERLOAD:
925 t = OVL_CURRENT (t);
926 case VAR_DECL:
927 case PARM_DECL:
928 case FIELD_DECL:
929 case CONST_DECL:
930 case FUNCTION_DECL:
931 case BASELINK:
932 case TEMPLATE_DECL:
933 case TEMPLATE_TYPE_PARM:
934 case TEMPLATE_PARM_INDEX:
935 case TEMPLATE_TEMPLATE_PARM:
936 case STMT_EXPR:
937 pp_cxx_primary_expression (pp, t);
938 break;
940 case CALL_EXPR:
941 case DYNAMIC_CAST_EXPR:
942 case STATIC_CAST_EXPR:
943 case REINTERPRET_CAST_EXPR:
944 case CONST_CAST_EXPR:
945 #if 0
946 case MEMBER_REF:
947 #endif
948 case EMPTY_CLASS_EXPR:
949 case TYPEID_EXPR:
950 case PSEUDO_DTOR_EXPR:
951 case AGGR_INIT_EXPR:
952 case ARROW_EXPR:
953 pp_cxx_postfix_expression (pp, t);
954 break;
956 case NEW_EXPR:
957 case VEC_NEW_EXPR:
958 pp_cxx_new_expression (pp, t);
959 break;
961 case DELETE_EXPR:
962 case VEC_DELETE_EXPR:
963 pp_cxx_delete_expression (pp, t);
964 break;
966 case SIZEOF_EXPR:
967 case ALIGNOF_EXPR:
968 pp_cxx_unary_expression (pp, t);
969 break;
971 case CAST_EXPR:
972 pp_cxx_cast_expression (pp, t);
973 break;
975 case OFFSET_REF:
976 case MEMBER_REF:
977 case DOTSTAR_EXPR:
978 pp_cxx_pm_expression (pp, t);
979 break;
981 case MULT_EXPR:
982 case TRUNC_DIV_EXPR:
983 case TRUNC_MOD_EXPR:
984 pp_cxx_multiplicative_expression (pp, t);
985 break;
987 case COND_EXPR:
988 pp_cxx_conditional_expression (pp, t);
989 break;
991 case MODIFY_EXPR:
992 case INIT_EXPR:
993 case THROW_EXPR:
994 case MODOP_EXPR:
995 pp_cxx_assignment_expression (pp, t);
996 break;
998 case NON_DEPENDENT_EXPR:
999 case MUST_NOT_THROW_EXPR:
1000 pp_cxx_expression (pp, t);
1001 break;
1003 default:
1004 pp_c_expression (pp_c_base (pp), t);
1005 break;
1010 /* Declarations. */
1012 /* function-specifier:
1013 inline
1014 virtual
1015 explicit */
1017 static void
1018 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1020 switch (TREE_CODE (t))
1022 case FUNCTION_DECL:
1023 if (DECL_VIRTUAL_P (t))
1024 pp_cxx_identifier (pp, "virtual");
1025 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1026 pp_cxx_identifier (pp, "explicit");
1027 else
1028 pp_c_function_specifier (pp_c_base (pp), t);
1030 default:
1031 break;
1035 /* decl-specifier-seq:
1036 decl-specifier-seq(opt) decl-specifier
1038 decl-specifier:
1039 storage-class-specifier
1040 type-specifier
1041 function-specifier
1042 friend
1043 typedef */
1045 static void
1046 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1048 switch (TREE_CODE (t))
1050 case VAR_DECL:
1051 case PARM_DECL:
1052 case CONST_DECL:
1053 case FIELD_DECL:
1054 pp_cxx_storage_class_specifier (pp, t);
1055 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1056 break;
1058 case TYPE_DECL:
1059 pp_cxx_identifier (pp, "typedef");
1060 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1061 break;
1063 case RECORD_TYPE:
1064 if (TYPE_PTRMEMFUNC_P (t))
1066 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1067 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1068 pp_cxx_whitespace (pp);
1069 pp_cxx_ptr_operator (pp, t);
1071 break;
1073 case FUNCTION_DECL:
1074 /* Constructors don't have return types. And conversion functions
1075 do not have a type-specifier in their return types. */
1076 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1077 pp_cxx_function_specifier (pp, t);
1078 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1079 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1080 else
1081 default:
1082 pp_c_declaration_specifiers (pp_c_base (pp), t);
1083 break;
1087 /* simple-type-specifier:
1088 ::(opt) nested-name-specifier(opt) type-name
1089 ::(opt) nested-name-specifier(opt) template(opt) template-id
1090 char
1091 wchar_t
1092 bool
1093 short
1095 long
1096 signed
1097 unsigned
1098 float
1099 double
1100 void */
1102 static void
1103 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1105 switch (TREE_CODE (t))
1107 case RECORD_TYPE:
1108 case UNION_TYPE:
1109 case ENUMERAL_TYPE:
1110 pp_cxx_qualified_id (pp, t);
1111 break;
1113 case TEMPLATE_TYPE_PARM:
1114 case TEMPLATE_TEMPLATE_PARM:
1115 case TEMPLATE_PARM_INDEX:
1116 pp_cxx_unqualified_id (pp, t);
1117 break;
1119 case TYPENAME_TYPE:
1120 pp_cxx_identifier (pp, "typename");
1121 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1122 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1123 break;
1125 default:
1126 pp_c_type_specifier (pp_c_base (pp), t);
1127 break;
1131 /* type-specifier-seq:
1132 type-specifier type-specifier-seq(opt)
1134 type-specifier:
1135 simple-type-specifier
1136 class-specifier
1137 enum-specifier
1138 elaborated-type-specifier
1139 cv-qualifier */
1141 static void
1142 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1144 switch (TREE_CODE (t))
1146 case TEMPLATE_DECL:
1147 case TEMPLATE_TYPE_PARM:
1148 case TEMPLATE_TEMPLATE_PARM:
1149 case TYPE_DECL:
1150 case BOUND_TEMPLATE_TEMPLATE_PARM:
1151 pp_cxx_cv_qualifier_seq (pp, t);
1152 pp_cxx_simple_type_specifier (pp, t);
1153 break;
1155 case METHOD_TYPE:
1156 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1157 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1158 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1159 break;
1161 default:
1162 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1163 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1167 /* ptr-operator:
1168 * cv-qualifier-seq(opt)
1170 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1172 static void
1173 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1175 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1176 t = TREE_TYPE (t);
1177 switch (TREE_CODE (t))
1179 case REFERENCE_TYPE:
1180 case POINTER_TYPE:
1181 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1182 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1183 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1184 if (TREE_CODE (t) == POINTER_TYPE)
1186 pp_star (pp);
1187 pp_cxx_cv_qualifier_seq (pp, t);
1189 else
1190 pp_ampersand (pp);
1191 break;
1193 case RECORD_TYPE:
1194 if (TYPE_PTRMEMFUNC_P (t))
1196 pp_cxx_left_paren (pp);
1197 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1198 pp_star (pp);
1199 break;
1201 case OFFSET_TYPE:
1202 if (TYPE_PTR_TO_MEMBER_P (t))
1204 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1205 pp_cxx_left_paren (pp);
1206 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1207 pp_star (pp);
1208 pp_cxx_cv_qualifier_seq (pp, t);
1209 break;
1211 /* else fall through. */
1213 default:
1214 pp_unsupported_tree (pp, t);
1215 break;
1219 static inline tree
1220 pp_cxx_implicit_parameter_type (tree mf)
1222 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1226 parameter-declaration:
1227 decl-specifier-seq declarator
1228 decl-specifier-seq declarator = assignment-expression
1229 decl-specifier-seq abstract-declarator(opt)
1230 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1232 static inline void
1233 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1235 pp_cxx_decl_specifier_seq (pp, t);
1236 if (TYPE_P (t))
1237 pp_cxx_abstract_declarator (pp, t);
1238 else
1239 pp_cxx_declarator (pp, t);
1242 /* parameter-declaration-clause:
1243 parameter-declaration-list(opt) ...(opt)
1244 parameter-declaration-list , ...
1246 parameter-declaration-list:
1247 parameter-declaration
1248 parameter-declaration-list , parameter-declaration */
1250 static void
1251 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1253 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1254 tree types =
1255 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1256 const bool abstract = args == NULL
1257 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1258 bool first = true;
1260 /* Skip artificial parameter for nonstatic member functions. */
1261 if (TREE_CODE (t) == METHOD_TYPE)
1262 types = TREE_CHAIN (types);
1264 pp_cxx_left_paren (pp);
1265 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1267 if (!first)
1268 pp_cxx_separate_with (pp, ',');
1269 first = false;
1270 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1271 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1273 pp_cxx_whitespace (pp);
1274 pp_equal (pp);
1275 pp_cxx_whitespace (pp);
1276 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1279 pp_cxx_right_paren (pp);
1282 /* exception-specification:
1283 throw ( type-id-list(opt) )
1285 type-id-list
1286 type-id
1287 type-id-list , type-id */
1289 static void
1290 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1292 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1294 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1295 return;
1296 pp_cxx_identifier (pp, "throw");
1297 pp_cxx_left_paren (pp);
1298 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1300 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1301 if (TREE_CHAIN (ex_spec))
1302 pp_cxx_separate_with (pp, ',');
1304 pp_cxx_right_paren (pp);
1307 /* direct-declarator:
1308 declarator-id
1309 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1310 exception-specification(opt)
1311 direct-declaration [ constant-expression(opt) ]
1312 ( declarator ) */
1314 static void
1315 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1317 switch (TREE_CODE (t))
1319 case VAR_DECL:
1320 case PARM_DECL:
1321 case CONST_DECL:
1322 case FIELD_DECL:
1323 if (DECL_NAME (t))
1325 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1326 pp_cxx_id_expression (pp, DECL_NAME (t));
1328 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1329 break;
1331 case FUNCTION_DECL:
1332 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1333 pp_cxx_id_expression (pp, t);
1334 pp_cxx_parameter_declaration_clause (pp, t);
1336 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1338 pp_base (pp)->padding = pp_before;
1339 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1342 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1343 break;
1345 case TYPENAME_TYPE:
1346 case TEMPLATE_DECL:
1347 case TEMPLATE_TYPE_PARM:
1348 case TEMPLATE_PARM_INDEX:
1349 case TEMPLATE_TEMPLATE_PARM:
1350 break;
1352 default:
1353 pp_c_direct_declarator (pp_c_base (pp), t);
1354 break;
1358 /* declarator:
1359 direct-declarator
1360 ptr-operator declarator */
1362 static void
1363 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1365 pp_cxx_direct_declarator (pp, t);
1368 /* ctor-initializer:
1369 : mem-initializer-list
1371 mem-initializer-list:
1372 mem-initializer
1373 mem-initializer , mem-initializer-list
1375 mem-initializer:
1376 mem-initializer-id ( expression-list(opt) )
1378 mem-initializer-id:
1379 ::(opt) nested-name-specifier(opt) class-name
1380 identifier */
1382 static void
1383 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1385 t = TREE_OPERAND (t, 0);
1386 pp_cxx_whitespace (pp);
1387 pp_colon (pp);
1388 pp_cxx_whitespace (pp);
1389 for (; t; t = TREE_CHAIN (t))
1391 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1392 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1393 if (TREE_CHAIN (t))
1394 pp_cxx_separate_with (pp, ',');
1398 /* function-definition:
1399 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1400 decl-specifier-seq(opt) declarator function-try-block */
1402 static void
1403 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1405 tree saved_scope = pp->enclosing_scope;
1406 pp_cxx_decl_specifier_seq (pp, t);
1407 pp_cxx_declarator (pp, t);
1408 pp_needs_newline (pp) = true;
1409 pp->enclosing_scope = DECL_CONTEXT (t);
1410 if (DECL_SAVED_TREE (t))
1411 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1412 else
1414 pp_cxx_semicolon (pp);
1415 pp_needs_newline (pp) = true;
1417 pp_flush (pp);
1418 pp->enclosing_scope = saved_scope;
1421 /* abstract-declarator:
1422 ptr-operator abstract-declarator(opt)
1423 direct-abstract-declarator */
1425 static void
1426 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1428 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1429 pp_cxx_right_paren (pp);
1430 else if (POINTER_TYPE_P (t))
1432 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1433 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1434 pp_cxx_right_paren (pp);
1435 t = TREE_TYPE (t);
1437 pp_cxx_direct_abstract_declarator (pp, t);
1440 /* direct-abstract-declarator:
1441 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1442 cv-qualifier-seq(opt) exception-specification(opt)
1443 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1444 ( abstract-declarator ) */
1446 static void
1447 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1449 switch (TREE_CODE (t))
1451 case REFERENCE_TYPE:
1452 pp_cxx_abstract_declarator (pp, t);
1453 break;
1455 case RECORD_TYPE:
1456 if (TYPE_PTRMEMFUNC_P (t))
1457 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1458 break;
1460 case METHOD_TYPE:
1461 case FUNCTION_TYPE:
1462 pp_cxx_parameter_declaration_clause (pp, t);
1463 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1464 if (TREE_CODE (t) == METHOD_TYPE)
1466 pp_base (pp)->padding = pp_before;
1467 pp_cxx_cv_qualifier_seq
1468 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1470 pp_cxx_exception_specification (pp, t);
1471 break;
1473 case TYPENAME_TYPE:
1474 case TEMPLATE_TYPE_PARM:
1475 case TEMPLATE_TEMPLATE_PARM:
1476 case BOUND_TEMPLATE_TEMPLATE_PARM:
1477 case UNBOUND_CLASS_TEMPLATE:
1478 break;
1480 default:
1481 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1482 break;
1486 /* type-id:
1487 type-specifier-seq abstract-declarator(opt) */
1489 static void
1490 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1492 pp_flags saved_flags = pp_c_base (pp)->flags;
1493 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1495 switch (TREE_CODE (t))
1497 case TYPE_DECL:
1498 case UNION_TYPE:
1499 case RECORD_TYPE:
1500 case ENUMERAL_TYPE:
1501 case TYPENAME_TYPE:
1502 case BOUND_TEMPLATE_TEMPLATE_PARM:
1503 case UNBOUND_CLASS_TEMPLATE:
1504 case TEMPLATE_TEMPLATE_PARM:
1505 case TEMPLATE_TYPE_PARM:
1506 case TEMPLATE_PARM_INDEX:
1507 case TEMPLATE_DECL:
1508 case TYPEOF_TYPE:
1509 case TEMPLATE_ID_EXPR:
1510 pp_cxx_type_specifier_seq (pp, t);
1511 break;
1513 default:
1514 pp_c_type_id (pp_c_base (pp), t);
1515 break;
1518 pp_c_base (pp)->flags = saved_flags;
1521 /* template-argument-list:
1522 template-argument
1523 template-argument-list, template-argument
1525 template-argument:
1526 assignment-expression
1527 type-id
1528 template-name */
1530 static void
1531 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1533 int i;
1534 if (t == NULL)
1535 return;
1536 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1538 tree arg = TREE_VEC_ELT (t, i);
1539 if (i != 0)
1540 pp_cxx_separate_with (pp, ',');
1541 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1542 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1543 pp_cxx_type_id (pp, arg);
1544 else
1545 pp_cxx_expression (pp, arg);
1550 static void
1551 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1553 t = DECL_EXPR_DECL (t);
1554 pp_cxx_type_specifier_seq (pp, t);
1555 if (TYPE_P (t))
1556 pp_cxx_abstract_declarator (pp, t);
1557 else
1558 pp_cxx_declarator (pp, t);
1561 /* Statements. */
1563 static void
1564 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1566 switch (TREE_CODE (t))
1568 case CTOR_INITIALIZER:
1569 pp_cxx_ctor_initializer (pp, t);
1570 break;
1572 case USING_STMT:
1573 pp_cxx_identifier (pp, "using");
1574 pp_cxx_identifier (pp, "namespace");
1575 if (DECL_CONTEXT (t))
1576 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1577 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1578 break;
1580 case USING_DECL:
1581 pp_cxx_identifier (pp, "using");
1582 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1583 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1584 break;
1586 case EH_SPEC_BLOCK:
1587 break;
1589 /* try-block:
1590 try compound-statement handler-seq */
1591 case TRY_BLOCK:
1592 pp_maybe_newline_and_indent (pp, 0);
1593 pp_cxx_identifier (pp, "try");
1594 pp_newline_and_indent (pp, 3);
1595 pp_cxx_statement (pp, TRY_STMTS (t));
1596 pp_newline_and_indent (pp, -3);
1597 if (CLEANUP_P (t))
1599 else
1600 pp_cxx_statement (pp, TRY_HANDLERS (t));
1601 break;
1604 handler-seq:
1605 handler handler-seq(opt)
1607 handler:
1608 catch ( exception-declaration ) compound-statement
1610 exception-declaration:
1611 type-specifier-seq declarator
1612 type-specifier-seq abstract-declarator
1613 ... */
1614 case HANDLER:
1615 pp_cxx_identifier (pp, "catch");
1616 pp_cxx_left_paren (pp);
1617 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1618 pp_cxx_right_paren (pp);
1619 pp_indentation (pp) += 3;
1620 pp_needs_newline (pp) = true;
1621 pp_cxx_statement (pp, HANDLER_BODY (t));
1622 pp_indentation (pp) -= 3;
1623 pp_needs_newline (pp) = true;
1624 break;
1626 /* selection-statement:
1627 if ( expression ) statement
1628 if ( expression ) statement else statement */
1629 case IF_STMT:
1630 pp_cxx_identifier (pp, "if");
1631 pp_cxx_whitespace (pp);
1632 pp_cxx_left_paren (pp);
1633 pp_cxx_expression (pp, IF_COND (t));
1634 pp_cxx_right_paren (pp);
1635 pp_newline_and_indent (pp, 2);
1636 pp_cxx_statement (pp, THEN_CLAUSE (t));
1637 pp_newline_and_indent (pp, -2);
1638 if (ELSE_CLAUSE (t))
1640 tree else_clause = ELSE_CLAUSE (t);
1641 pp_cxx_identifier (pp, "else");
1642 if (TREE_CODE (else_clause) == IF_STMT)
1643 pp_cxx_whitespace (pp);
1644 else
1645 pp_newline_and_indent (pp, 2);
1646 pp_cxx_statement (pp, else_clause);
1647 if (TREE_CODE (else_clause) != IF_STMT)
1648 pp_newline_and_indent (pp, -2);
1650 break;
1652 case SWITCH_STMT:
1653 pp_cxx_identifier (pp, "switch");
1654 pp_space (pp);
1655 pp_cxx_left_paren (pp);
1656 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1657 pp_cxx_right_paren (pp);
1658 pp_indentation (pp) += 3;
1659 pp_needs_newline (pp) = true;
1660 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1661 pp_newline_and_indent (pp, -3);
1662 break;
1664 /* iteration-statement:
1665 while ( expression ) statement
1666 do statement while ( expression ) ;
1667 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1668 for ( declaration expression(opt) ; expression(opt) ) statement */
1669 case WHILE_STMT:
1670 pp_cxx_identifier (pp, "while");
1671 pp_space (pp);
1672 pp_cxx_left_paren (pp);
1673 pp_cxx_expression (pp, WHILE_COND (t));
1674 pp_cxx_right_paren (pp);
1675 pp_newline_and_indent (pp, 3);
1676 pp_cxx_statement (pp, WHILE_BODY (t));
1677 pp_indentation (pp) -= 3;
1678 pp_needs_newline (pp) = true;
1679 break;
1681 case DO_STMT:
1682 pp_cxx_identifier (pp, "do");
1683 pp_newline_and_indent (pp, 3);
1684 pp_cxx_statement (pp, DO_BODY (t));
1685 pp_newline_and_indent (pp, -3);
1686 pp_cxx_identifier (pp, "while");
1687 pp_space (pp);
1688 pp_cxx_left_paren (pp);
1689 pp_cxx_expression (pp, DO_COND (t));
1690 pp_cxx_right_paren (pp);
1691 pp_cxx_semicolon (pp);
1692 pp_needs_newline (pp) = true;
1693 break;
1695 case FOR_STMT:
1696 pp_cxx_identifier (pp, "for");
1697 pp_space (pp);
1698 pp_cxx_left_paren (pp);
1699 if (FOR_INIT_STMT (t))
1700 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1701 else
1702 pp_cxx_semicolon (pp);
1703 pp_needs_newline (pp) = false;
1704 pp_cxx_whitespace (pp);
1705 if (FOR_COND (t))
1706 pp_cxx_expression (pp, FOR_COND (t));
1707 pp_cxx_semicolon (pp);
1708 pp_needs_newline (pp) = false;
1709 pp_cxx_whitespace (pp);
1710 if (FOR_EXPR (t))
1711 pp_cxx_expression (pp, FOR_EXPR (t));
1712 pp_cxx_right_paren (pp);
1713 pp_newline_and_indent (pp, 3);
1714 pp_cxx_statement (pp, FOR_BODY (t));
1715 pp_indentation (pp) -= 3;
1716 pp_needs_newline (pp) = true;
1717 break;
1719 /* jump-statement:
1720 goto identifier;
1721 continue ;
1722 return expression(opt) ; */
1723 case BREAK_STMT:
1724 case CONTINUE_STMT:
1725 pp_identifier (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1726 pp_cxx_semicolon (pp);
1727 pp_needs_newline (pp) = true;
1728 break;
1730 /* expression-statement:
1731 expression(opt) ; */
1732 case EXPR_STMT:
1733 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1734 pp_cxx_semicolon (pp);
1735 pp_needs_newline (pp) = true;
1736 break;
1738 case CLEANUP_STMT:
1739 pp_cxx_identifier (pp, "try");
1740 pp_newline_and_indent (pp, 2);
1741 pp_cxx_statement (pp, CLEANUP_BODY (t));
1742 pp_newline_and_indent (pp, -2);
1743 pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1744 pp_newline_and_indent (pp, 2);
1745 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1746 pp_newline_and_indent (pp, -2);
1747 break;
1749 case STATIC_ASSERT:
1750 pp_cxx_declaration (pp, t);
1751 break;
1753 default:
1754 pp_c_statement (pp_c_base (pp), t);
1755 break;
1759 /* original-namespace-definition:
1760 namespace identifier { namespace-body }
1762 As an edge case, we also handle unnamed namespace definition here. */
1764 static void
1765 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1767 pp_cxx_identifier (pp, "namespace");
1768 if (DECL_CONTEXT (t))
1769 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1770 if (DECL_NAME (t))
1771 pp_cxx_unqualified_id (pp, t);
1772 pp_cxx_whitespace (pp);
1773 pp_cxx_left_brace (pp);
1774 /* We do not print the namespace-body. */
1775 pp_cxx_whitespace (pp);
1776 pp_cxx_right_brace (pp);
1779 /* namespace-alias:
1780 identifier
1782 namespace-alias-definition:
1783 namespace identifier = qualified-namespace-specifier ;
1785 qualified-namespace-specifier:
1786 ::(opt) nested-name-specifier(opt) namespace-name */
1788 static void
1789 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1791 pp_cxx_identifier (pp, "namespace");
1792 if (DECL_CONTEXT (t))
1793 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1794 pp_cxx_unqualified_id (pp, t);
1795 pp_cxx_whitespace (pp);
1796 pp_equal (pp);
1797 pp_cxx_whitespace (pp);
1798 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1799 pp_cxx_nested_name_specifier (pp,
1800 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1801 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1802 pp_cxx_semicolon (pp);
1805 /* simple-declaration:
1806 decl-specifier-seq(opt) init-declarator-list(opt) */
1808 static void
1809 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1811 pp_cxx_decl_specifier_seq (pp, t);
1812 pp_cxx_init_declarator (pp, t);
1813 pp_cxx_semicolon (pp);
1814 pp_needs_newline (pp) = true;
1818 template-parameter-list:
1819 template-parameter
1820 template-parameter-list , template-parameter */
1822 static inline void
1823 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1825 const int n = TREE_VEC_LENGTH (t);
1826 int i;
1827 for (i = 0; i < n; ++i)
1829 if (i)
1830 pp_cxx_separate_with (pp, ',');
1831 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1835 /* template-parameter:
1836 type-parameter
1837 parameter-declaration
1839 type-parameter:
1840 class identifier(opt)
1841 class identifier(op) = type-id
1842 typename identifier(opt)
1843 typename identifier(opt) = type-id
1844 template < template-parameter-list > class identifier(opt)
1845 template < template-parameter-list > class identifier(opt) = template-name */
1847 static void
1848 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1850 tree parameter = TREE_VALUE (t);
1851 switch (TREE_CODE (parameter))
1853 case TYPE_DECL:
1854 pp_cxx_identifier (pp, "class");
1855 if (DECL_NAME (parameter))
1856 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1857 /* FIXME: Chech if we should print also default argument. */
1858 break;
1860 case PARM_DECL:
1861 pp_cxx_parameter_declaration (pp, parameter);
1862 break;
1864 case TEMPLATE_DECL:
1865 break;
1867 default:
1868 pp_unsupported_tree (pp, t);
1869 break;
1873 /* Pretty-print a template parameter in the canonical form
1874 "template-parameter-<level>-<position in parameter list>". */
1876 void
1877 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1879 const enum tree_code code = TREE_CODE (parm);
1881 /* Brings type template parameters to the canonical forms. */
1882 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1883 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1884 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1886 pp_cxx_begin_template_argument_list (pp);
1887 pp_cxx_identifier (pp, "template-parameter-");
1888 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1889 pp_minus (pp);
1890 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1891 pp_cxx_end_template_argument_list (pp);
1895 template-declaration:
1896 export(opt) template < template-parameter-list > declaration */
1898 static void
1899 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1901 tree tmpl = most_general_template (t);
1902 tree level;
1903 int i = 0;
1905 pp_maybe_newline_and_indent (pp, 0);
1906 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1908 pp_cxx_identifier (pp, "template");
1909 pp_cxx_begin_template_argument_list (pp);
1910 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1911 pp_cxx_end_template_argument_list (pp);
1912 pp_newline_and_indent (pp, 3);
1913 i += 3;
1915 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1916 pp_cxx_function_definition (pp, t);
1917 else
1918 pp_cxx_simple_declaration (pp, t);
1921 static void
1922 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1924 pp_unsupported_tree (pp, t);
1927 static void
1928 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1930 pp_unsupported_tree (pp, t);
1934 declaration:
1935 block-declaration
1936 function-definition
1937 template-declaration
1938 explicit-instantiation
1939 explicit-specialization
1940 linkage-specification
1941 namespace-definition
1943 block-declaration:
1944 simple-declaration
1945 asm-definition
1946 namespace-alias-definition
1947 using-declaration
1948 using-directive
1949 static_assert-declaration */
1950 void
1951 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1953 if (TREE_CODE (t) == STATIC_ASSERT)
1955 pp_cxx_identifier (pp, "static_assert");
1956 pp_cxx_left_paren (pp);
1957 pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
1958 pp_cxx_separate_with (pp, ',');
1959 pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
1960 pp_cxx_right_paren (pp);
1962 else if (!DECL_LANG_SPECIFIC (t))
1963 pp_cxx_simple_declaration (pp, t);
1964 else if (DECL_USE_TEMPLATE (t))
1965 switch (DECL_USE_TEMPLATE (t))
1967 case 1:
1968 pp_cxx_template_declaration (pp, t);
1969 break;
1971 case 2:
1972 pp_cxx_explicit_specialization (pp, t);
1973 break;
1975 case 3:
1976 pp_cxx_explicit_instantiation (pp, t);
1977 break;
1979 default:
1980 break;
1982 else switch (TREE_CODE (t))
1984 case VAR_DECL:
1985 case TYPE_DECL:
1986 pp_cxx_simple_declaration (pp, t);
1987 break;
1989 case FUNCTION_DECL:
1990 if (DECL_SAVED_TREE (t))
1991 pp_cxx_function_definition (pp, t);
1992 else
1993 pp_cxx_simple_declaration (pp, t);
1994 break;
1996 case NAMESPACE_DECL:
1997 if (DECL_NAMESPACE_ALIAS (t))
1998 pp_cxx_namespace_alias_definition (pp, t);
1999 else
2000 pp_cxx_original_namespace_definition (pp, t);
2001 break;
2003 default:
2004 pp_unsupported_tree (pp, t);
2005 break;
2010 typedef c_pretty_print_fn pp_fun;
2012 /* Initialization of a C++ pretty-printer object. */
2014 void
2015 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
2017 pp_c_pretty_printer_init (pp_c_base (pp));
2018 pp_set_line_maximum_length (pp, 0);
2020 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
2021 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2022 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
2023 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2024 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
2025 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2026 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2027 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
2028 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2029 pp->c_base.direct_abstract_declarator =
2030 (pp_fun) pp_cxx_direct_abstract_declarator;
2031 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
2033 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2035 pp->c_base.constant = (pp_fun) pp_cxx_constant;
2036 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
2037 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
2038 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2039 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
2040 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2041 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2042 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2043 pp->c_base.expression = (pp_fun) pp_cxx_expression;
2044 pp->enclosing_scope = global_namespace;