Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / cp / cxx-pretty-print.c
blob43cf5819bf06cafb64d579edb13bff5741e2c798
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 case UNBOUND_CLASS_TEMPLATE:
208 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
209 break;
211 default:
212 pp_unsupported_tree (pp, t);
213 break;
217 /* Pretty-print out the token sequence ":: template" in template codes
218 where it is needed to "inline declare" the (following) member as
219 a template. This situation arises when SCOPE of T is dependent
220 on template parameters. */
222 static inline void
223 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
225 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
226 && TYPE_P (scope) && dependent_type_p (scope))
227 pp_cxx_identifier (pp, "template");
230 /* nested-name-specifier:
231 class-or-namespace-name :: nested-name-specifier(opt)
232 class-or-namespace-name :: template nested-name-specifier */
234 static void
235 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
237 if (t != NULL && t != pp->enclosing_scope)
239 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
240 pp_cxx_nested_name_specifier (pp, scope);
241 pp_cxx_template_keyword_if_needed (pp, scope, t);
242 pp_cxx_unqualified_id (pp, t);
243 pp_cxx_colon_colon (pp);
247 /* qualified-id:
248 nested-name-specifier template(opt) unqualified-id */
250 static void
251 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
253 switch (TREE_CODE (t))
255 /* A pointer-to-member is always qualified. */
256 case PTRMEM_CST:
257 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
258 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
259 break;
261 /* In Standard C++, functions cannot possibly be used as
262 nested-name-specifiers. However, there are situations where
263 is "makes sense" to output the surrounding function name for the
264 purpose of emphasizing on the scope kind. Just printing the
265 function name might not be sufficient as it may be overloaded; so,
266 we decorate the function with its signature too.
267 FIXME: This is probably the wrong pretty-printing for conversion
268 functions and some function templates. */
269 case OVERLOAD:
270 t = OVL_CURRENT (t);
271 case FUNCTION_DECL:
272 if (DECL_FUNCTION_MEMBER_P (t))
273 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
274 pp_cxx_unqualified_id
275 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
276 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
277 break;
279 case OFFSET_REF:
280 case SCOPE_REF:
281 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
282 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
283 break;
285 default:
287 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
288 if (scope != pp->enclosing_scope)
290 pp_cxx_nested_name_specifier (pp, scope);
291 pp_cxx_template_keyword_if_needed (pp, scope, t);
293 pp_cxx_unqualified_id (pp, t);
295 break;
299 /* id-expression:
300 unqualified-id
301 qualified-id */
303 static inline void
304 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
306 if (TREE_CODE (t) == OVERLOAD)
307 t = OVL_CURRENT (t);
308 if (DECL_P (t) && DECL_CONTEXT (t))
309 pp_cxx_qualified_id (pp, t);
310 else
311 pp_cxx_unqualified_id (pp, t);
314 /* primary-expression:
315 literal
316 this
317 :: identifier
318 :: operator-function-id
319 :: qualifier-id
320 ( expression )
321 id-expression */
323 static void
324 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
326 switch (TREE_CODE (t))
328 case STRING_CST:
329 case INTEGER_CST:
330 case REAL_CST:
331 pp_c_constant (pp_c_base (pp), t);
332 break;
334 case BASELINK:
335 t = BASELINK_FUNCTIONS (t);
336 case VAR_DECL:
337 case PARM_DECL:
338 case FIELD_DECL:
339 case FUNCTION_DECL:
340 case OVERLOAD:
341 case CONST_DECL:
342 case TEMPLATE_DECL:
343 pp_cxx_id_expression (pp, t);
344 break;
346 case RESULT_DECL:
347 case TEMPLATE_TYPE_PARM:
348 case TEMPLATE_TEMPLATE_PARM:
349 case TEMPLATE_PARM_INDEX:
350 pp_cxx_unqualified_id (pp, t);
351 break;
353 case STMT_EXPR:
354 pp_cxx_left_paren (pp);
355 pp_cxx_statement (pp, STMT_EXPR_STMT (t));
356 pp_cxx_right_paren (pp);
357 break;
359 default:
360 pp_c_primary_expression (pp_c_base (pp), t);
361 break;
365 /* postfix-expression:
366 primary-expression
367 postfix-expression [ expression ]
368 postfix-expression ( expression-list(opt) )
369 simple-type-specifier ( expression-list(opt) )
370 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
371 typename ::(opt) nested-name-specifier template(opt)
372 template-id ( expression-list(opt) )
373 postfix-expression . template(opt) ::(opt) id-expression
374 postfix-expression -> template(opt) ::(opt) id-expression
375 postfix-expression . pseudo-destructor-name
376 postfix-expression -> pseudo-destructor-name
377 postfix-expression ++
378 postfix-expression --
379 dynamic_cast < type-id > ( expression )
380 static_cast < type-id > ( expression )
381 reinterpret_cast < type-id > ( expression )
382 const_cast < type-id > ( expression )
383 typeid ( expression )
384 typeif ( type-id ) */
386 static void
387 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
389 enum tree_code code = TREE_CODE (t);
391 switch (code)
393 case AGGR_INIT_EXPR:
394 case CALL_EXPR:
396 tree fun = TREE_OPERAND (t, 0);
397 tree args = TREE_OPERAND (t, 1);
398 tree saved_scope = pp->enclosing_scope;
400 if (TREE_CODE (fun) == ADDR_EXPR)
401 fun = TREE_OPERAND (fun, 0);
403 /* In templates, where there is no way to tell whether a given
404 call uses an actual member function. So the parser builds
405 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
406 instantiation time. */
407 if (TREE_CODE (fun) != FUNCTION_DECL)
409 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
411 tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
412 ? TREE_OPERAND (t, 2)
413 : TREE_VALUE (args);
415 while (TREE_CODE (object) == NOP_EXPR)
416 object = TREE_OPERAND (object, 0);
418 if (TREE_CODE (object) == ADDR_EXPR)
419 object = TREE_OPERAND (object, 0);
421 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
423 pp_cxx_postfix_expression (pp, object);
424 pp_cxx_dot (pp);
426 else
428 pp_cxx_postfix_expression (pp, object);
429 pp_cxx_arrow (pp);
431 args = TREE_CHAIN (args);
432 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
435 pp_cxx_postfix_expression (pp, fun);
436 pp->enclosing_scope = saved_scope;
437 pp_cxx_call_argument_list (pp, args);
439 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
441 pp_cxx_separate_with (pp, ',');
442 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
444 break;
446 case BASELINK:
447 case VAR_DECL:
448 case PARM_DECL:
449 case FIELD_DECL:
450 case FUNCTION_DECL:
451 case OVERLOAD:
452 case CONST_DECL:
453 case TEMPLATE_DECL:
454 case RESULT_DECL:
455 pp_cxx_primary_expression (pp, t);
456 break;
458 case DYNAMIC_CAST_EXPR:
459 case STATIC_CAST_EXPR:
460 case REINTERPRET_CAST_EXPR:
461 case CONST_CAST_EXPR:
462 if (code == DYNAMIC_CAST_EXPR)
463 pp_cxx_identifier (pp, "dynamic_cast");
464 else if (code == STATIC_CAST_EXPR)
465 pp_cxx_identifier (pp, "static_cast");
466 else if (code == REINTERPRET_CAST_EXPR)
467 pp_cxx_identifier (pp, "reinterpret_cast");
468 else
469 pp_cxx_identifier (pp, "const_cast");
470 pp_cxx_begin_template_argument_list (pp);
471 pp_cxx_type_id (pp, TREE_TYPE (t));
472 pp_cxx_end_template_argument_list (pp);
473 pp_left_paren (pp);
474 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
475 pp_right_paren (pp);
476 break;
478 case EMPTY_CLASS_EXPR:
479 pp_cxx_type_id (pp, TREE_TYPE (t));
480 pp_left_paren (pp);
481 pp_right_paren (pp);
482 break;
484 case TYPEID_EXPR:
485 t = TREE_OPERAND (t, 0);
486 pp_cxx_identifier (pp, "typeid");
487 pp_left_paren (pp);
488 if (TYPE_P (t))
489 pp_cxx_type_id (pp, t);
490 else
491 pp_cxx_expression (pp, t);
492 pp_right_paren (pp);
493 break;
495 case PSEUDO_DTOR_EXPR:
496 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
497 pp_cxx_dot (pp);
498 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
499 pp_cxx_colon_colon (pp);
500 pp_complement (pp);
501 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
502 break;
504 case ARROW_EXPR:
505 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
506 pp_cxx_arrow (pp);
507 break;
509 default:
510 pp_c_postfix_expression (pp_c_base (pp), t);
511 break;
515 /* new-expression:
516 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
517 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
519 new-placement:
520 ( expression-list )
522 new-type-id:
523 type-specifier-seq new-declarator(opt)
525 new-declarator:
526 ptr-operator new-declarator(opt)
527 direct-new-declarator
529 direct-new-declarator
530 [ expression ]
531 direct-new-declarator [ constant-expression ]
533 new-initializer:
534 ( expression-list(opt) ) */
536 static void
537 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
539 enum tree_code code = TREE_CODE (t);
540 switch (code)
542 case NEW_EXPR:
543 case VEC_NEW_EXPR:
544 if (NEW_EXPR_USE_GLOBAL (t))
545 pp_cxx_colon_colon (pp);
546 pp_cxx_identifier (pp, "new");
547 if (TREE_OPERAND (t, 0))
549 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
550 pp_space (pp);
552 /* FIXME: array-types are built with one more element. */
553 pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
554 if (TREE_OPERAND (t, 2))
556 pp_left_paren (pp);
557 t = TREE_OPERAND (t, 2);
558 if (TREE_CODE (t) == TREE_LIST)
559 pp_c_expression_list (pp_c_base (pp), t);
560 else if (t == void_zero_node)
561 ; /* OK, empty initializer list. */
562 else
563 pp_cxx_expression (pp, t);
564 pp_right_paren (pp);
566 break;
568 default:
569 pp_unsupported_tree (pp, t);
573 /* delete-expression:
574 ::(opt) delete cast-expression
575 ::(opt) delete [ ] cast-expression */
577 static void
578 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
580 enum tree_code code = TREE_CODE (t);
581 switch (code)
583 case DELETE_EXPR:
584 case VEC_DELETE_EXPR:
585 if (DELETE_EXPR_USE_GLOBAL (t))
586 pp_cxx_colon_colon (pp);
587 pp_cxx_identifier (pp, "delete");
588 if (code == VEC_DELETE_EXPR)
590 pp_left_bracket (pp);
591 pp_right_bracket (pp);
593 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
594 break;
596 default:
597 pp_unsupported_tree (pp, t);
601 /* unary-expression:
602 postfix-expression
603 ++ cast-expression
604 -- cast-expression
605 unary-operator cast-expression
606 sizeof unary-expression
607 sizeof ( type-id )
608 new-expression
609 delete-expression
611 unary-operator: one of
612 * & + - !
614 GNU extensions:
615 __alignof__ unary-expression
616 __alignof__ ( type-id ) */
618 static void
619 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
621 enum tree_code code = TREE_CODE (t);
622 switch (code)
624 case NEW_EXPR:
625 case VEC_NEW_EXPR:
626 pp_cxx_new_expression (pp, t);
627 break;
629 case DELETE_EXPR:
630 case VEC_DELETE_EXPR:
631 pp_cxx_delete_expression (pp, t);
632 break;
634 case SIZEOF_EXPR:
635 case ALIGNOF_EXPR:
636 pp_cxx_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
637 pp_cxx_whitespace (pp);
638 if (TYPE_P (TREE_OPERAND (t, 0)))
640 pp_cxx_left_paren (pp);
641 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
642 pp_cxx_right_paren (pp);
644 else
645 pp_unary_expression (pp, TREE_OPERAND (t, 0));
646 break;
648 case UNARY_PLUS_EXPR:
649 pp_plus (pp);
650 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
651 break;
653 default:
654 pp_c_unary_expression (pp_c_base (pp), t);
655 break;
659 /* cast-expression:
660 unary-expression
661 ( type-id ) cast-expression */
663 static void
664 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
666 switch (TREE_CODE (t))
668 case CAST_EXPR:
669 pp_cxx_type_id (pp, TREE_TYPE (t));
670 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
671 break;
673 default:
674 pp_c_cast_expression (pp_c_base (pp), t);
675 break;
679 /* pm-expression:
680 cast-expression
681 pm-expression .* cast-expression
682 pm-expression ->* cast-expression */
684 static void
685 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
687 switch (TREE_CODE (t))
689 /* Handle unfortunate OFFESET_REF overloading here. */
690 case OFFSET_REF:
691 if (TYPE_P (TREE_OPERAND (t, 0)))
693 pp_cxx_qualified_id (pp, t);
694 break;
696 /* Else fall through. */
697 case MEMBER_REF:
698 case DOTSTAR_EXPR:
699 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
700 pp_cxx_dot (pp);
701 pp_star(pp);
702 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
703 break;
706 default:
707 pp_cxx_cast_expression (pp, t);
708 break;
712 /* multiplicative-expression:
713 pm-expression
714 multiplicative-expression * pm-expression
715 multiplicative-expression / pm-expression
716 multiplicative-expression % pm-expression */
718 static void
719 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
721 enum tree_code code = TREE_CODE (e);
722 switch (code)
724 case MULT_EXPR:
725 case TRUNC_DIV_EXPR:
726 case TRUNC_MOD_EXPR:
727 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
728 pp_space (pp);
729 if (code == MULT_EXPR)
730 pp_star (pp);
731 else if (code == TRUNC_DIV_EXPR)
732 pp_slash (pp);
733 else
734 pp_modulo (pp);
735 pp_space (pp);
736 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
737 break;
739 default:
740 pp_cxx_pm_expression (pp, e);
741 break;
745 /* conditional-expression:
746 logical-or-expression
747 logical-or-expression ? expression : assignment-expression */
749 static void
750 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
752 if (TREE_CODE (e) == COND_EXPR)
754 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
755 pp_space (pp);
756 pp_question (pp);
757 pp_space (pp);
758 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
759 pp_space (pp);
760 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
762 else
763 pp_c_logical_or_expression (pp_c_base (pp), e);
766 /* Pretty-print a compound assignment operator token as indicated by T. */
768 static void
769 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
771 const char *op;
773 switch (TREE_CODE (t))
775 case NOP_EXPR:
776 op = "=";
777 break;
779 case PLUS_EXPR:
780 op = "+=";
781 break;
783 case MINUS_EXPR:
784 op = "-=";
785 break;
787 case TRUNC_DIV_EXPR:
788 op = "/=";
789 break;
791 case TRUNC_MOD_EXPR:
792 op = "%=";
793 break;
795 default:
796 op = tree_code_name[TREE_CODE (t)];
797 break;
800 pp_cxx_identifier (pp, op);
804 /* assignment-expression:
805 conditional-expression
806 logical-or-expression assignment-operator assignment-expression
807 throw-expression
809 throw-expression:
810 throw assignment-expression(opt)
812 assignment-operator: one of
813 = *= /= %= += -= >>= <<= &= ^= |= */
815 static void
816 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
818 switch (TREE_CODE (e))
820 case MODIFY_EXPR:
821 case INIT_EXPR:
822 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
823 pp_space (pp);
824 pp_equal (pp);
825 pp_space (pp);
826 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
827 break;
829 case THROW_EXPR:
830 pp_cxx_identifier (pp, "throw");
831 if (TREE_OPERAND (e, 0))
832 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
833 break;
835 case MODOP_EXPR:
836 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
837 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
838 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
839 break;
841 default:
842 pp_cxx_conditional_expression (pp, e);
843 break;
847 static void
848 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
850 switch (TREE_CODE (t))
852 case STRING_CST:
853 case INTEGER_CST:
854 case REAL_CST:
855 pp_c_constant (pp_c_base (pp), t);
856 break;
858 case RESULT_DECL:
859 pp_cxx_unqualified_id (pp, t);
860 break;
862 #if 0
863 case OFFSET_REF:
864 #endif
865 case SCOPE_REF:
866 case PTRMEM_CST:
867 pp_cxx_qualified_id (pp, t);
868 break;
870 case OVERLOAD:
871 t = OVL_CURRENT (t);
872 case VAR_DECL:
873 case PARM_DECL:
874 case FIELD_DECL:
875 case CONST_DECL:
876 case FUNCTION_DECL:
877 case BASELINK:
878 case TEMPLATE_DECL:
879 case TEMPLATE_TYPE_PARM:
880 case TEMPLATE_PARM_INDEX:
881 case TEMPLATE_TEMPLATE_PARM:
882 case STMT_EXPR:
883 pp_cxx_primary_expression (pp, t);
884 break;
886 case CALL_EXPR:
887 case DYNAMIC_CAST_EXPR:
888 case STATIC_CAST_EXPR:
889 case REINTERPRET_CAST_EXPR:
890 case CONST_CAST_EXPR:
891 #if 0
892 case MEMBER_REF:
893 #endif
894 case EMPTY_CLASS_EXPR:
895 case TYPEID_EXPR:
896 case PSEUDO_DTOR_EXPR:
897 case AGGR_INIT_EXPR:
898 case ARROW_EXPR:
899 pp_cxx_postfix_expression (pp, t);
900 break;
902 case NEW_EXPR:
903 case VEC_NEW_EXPR:
904 pp_cxx_new_expression (pp, t);
905 break;
907 case DELETE_EXPR:
908 case VEC_DELETE_EXPR:
909 pp_cxx_delete_expression (pp, t);
910 break;
912 case SIZEOF_EXPR:
913 case ALIGNOF_EXPR:
914 pp_cxx_unary_expression (pp, t);
915 break;
917 case CAST_EXPR:
918 pp_cxx_cast_expression (pp, t);
919 break;
921 case OFFSET_REF:
922 case MEMBER_REF:
923 case DOTSTAR_EXPR:
924 pp_cxx_pm_expression (pp, t);
925 break;
927 case MULT_EXPR:
928 case TRUNC_DIV_EXPR:
929 case TRUNC_MOD_EXPR:
930 pp_cxx_multiplicative_expression (pp, t);
931 break;
933 case COND_EXPR:
934 pp_cxx_conditional_expression (pp, t);
935 break;
937 case MODIFY_EXPR:
938 case INIT_EXPR:
939 case THROW_EXPR:
940 case MODOP_EXPR:
941 pp_cxx_assignment_expression (pp, t);
942 break;
944 case NON_DEPENDENT_EXPR:
945 case MUST_NOT_THROW_EXPR:
946 pp_cxx_expression (pp, t);
947 break;
949 default:
950 pp_c_expression (pp_c_base (pp), t);
951 break;
956 /* Declarations. */
958 /* function-specifier:
959 inline
960 virtual
961 explicit */
963 static void
964 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
966 switch (TREE_CODE (t))
968 case FUNCTION_DECL:
969 if (DECL_VIRTUAL_P (t))
970 pp_cxx_identifier (pp, "virtual");
971 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
972 pp_cxx_identifier (pp, "explicit");
973 else
974 pp_c_function_specifier (pp_c_base (pp), t);
976 default:
977 break;
981 /* decl-specifier-seq:
982 decl-specifier-seq(opt) decl-specifier
984 decl-specifier:
985 storage-class-specifier
986 type-specifier
987 function-specifier
988 friend
989 typedef */
991 static void
992 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
994 switch (TREE_CODE (t))
996 case VAR_DECL:
997 case PARM_DECL:
998 case CONST_DECL:
999 case FIELD_DECL:
1000 pp_cxx_storage_class_specifier (pp, t);
1001 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1002 break;
1004 case TYPE_DECL:
1005 pp_cxx_identifier (pp, "typedef");
1006 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1007 break;
1009 case RECORD_TYPE:
1010 if (TYPE_PTRMEMFUNC_P (t))
1012 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1013 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1014 pp_cxx_whitespace (pp);
1015 pp_cxx_ptr_operator (pp, t);
1017 break;
1019 case FUNCTION_DECL:
1020 /* Constructors don't have return types. And conversion functions
1021 do not have a type-specifier in their return types. */
1022 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1023 pp_cxx_function_specifier (pp, t);
1024 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1025 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1026 else
1027 default:
1028 pp_c_declaration_specifiers (pp_c_base (pp), t);
1029 break;
1033 /* simple-type-specifier:
1034 ::(opt) nested-name-specifier(opt) type-name
1035 ::(opt) nested-name-specifier(opt) template(opt) template-id
1036 char
1037 wchar_t
1038 bool
1039 short
1041 long
1042 signed
1043 unsigned
1044 float
1045 double
1046 void */
1048 static void
1049 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1051 switch (TREE_CODE (t))
1053 case RECORD_TYPE:
1054 case UNION_TYPE:
1055 case ENUMERAL_TYPE:
1056 pp_cxx_qualified_id (pp, t);
1057 break;
1059 case TEMPLATE_TYPE_PARM:
1060 case TEMPLATE_TEMPLATE_PARM:
1061 case TEMPLATE_PARM_INDEX:
1062 pp_cxx_unqualified_id (pp, t);
1063 break;
1065 case TYPENAME_TYPE:
1066 pp_cxx_identifier (pp, "typename");
1067 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1068 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1069 break;
1071 default:
1072 pp_c_type_specifier (pp_c_base (pp), t);
1073 break;
1077 /* type-specifier-seq:
1078 type-specifier type-specifier-seq(opt)
1080 type-specifier:
1081 simple-type-specifier
1082 class-specifier
1083 enum-specifier
1084 elaborated-type-specifier
1085 cv-qualifier */
1087 static void
1088 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1090 switch (TREE_CODE (t))
1092 case TEMPLATE_DECL:
1093 case TEMPLATE_TYPE_PARM:
1094 case TEMPLATE_TEMPLATE_PARM:
1095 case TYPE_DECL:
1096 case BOUND_TEMPLATE_TEMPLATE_PARM:
1097 pp_cxx_cv_qualifier_seq (pp, t);
1098 pp_cxx_simple_type_specifier (pp, t);
1099 break;
1101 case METHOD_TYPE:
1102 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1103 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1104 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1105 break;
1107 default:
1108 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1109 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1113 /* ptr-operator:
1114 * cv-qualifier-seq(opt)
1116 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1118 static void
1119 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1121 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1122 t = TREE_TYPE (t);
1123 switch (TREE_CODE (t))
1125 case REFERENCE_TYPE:
1126 case POINTER_TYPE:
1127 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1128 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1129 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1130 if (TREE_CODE (t) == POINTER_TYPE)
1132 pp_star (pp);
1133 pp_cxx_cv_qualifier_seq (pp, t);
1135 else
1136 pp_ampersand (pp);
1137 break;
1139 case RECORD_TYPE:
1140 if (TYPE_PTRMEMFUNC_P (t))
1142 pp_cxx_left_paren (pp);
1143 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1144 pp_star (pp);
1145 break;
1147 case OFFSET_TYPE:
1148 if (TYPE_PTR_TO_MEMBER_P (t))
1150 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1151 pp_cxx_left_paren (pp);
1152 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1153 pp_star (pp);
1154 pp_cxx_cv_qualifier_seq (pp, t);
1155 break;
1157 /* else fall through. */
1159 default:
1160 pp_unsupported_tree (pp, t);
1161 break;
1165 static inline tree
1166 pp_cxx_implicit_parameter_type (tree mf)
1168 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1172 parameter-declaration:
1173 decl-specifier-seq declarator
1174 decl-specifier-seq declarator = assignment-expression
1175 decl-specifier-seq abstract-declarator(opt)
1176 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1178 static inline void
1179 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1181 pp_cxx_decl_specifier_seq (pp, t);
1182 if (TYPE_P (t))
1183 pp_cxx_abstract_declarator (pp, t);
1184 else
1185 pp_cxx_declarator (pp, t);
1188 /* parameter-declaration-clause:
1189 parameter-declaration-list(opt) ...(opt)
1190 parameter-declaration-list , ...
1192 parameter-declaration-list:
1193 parameter-declaration
1194 parameter-declaration-list , parameter-declaration */
1196 static void
1197 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1199 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1200 tree types =
1201 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1202 const bool abstract = args == NULL
1203 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1204 bool first = true;
1206 /* Skip artificial parameter for nonstatic member functions. */
1207 if (TREE_CODE (t) == METHOD_TYPE)
1208 types = TREE_CHAIN (types);
1210 pp_cxx_left_paren (pp);
1211 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1213 if (!first)
1214 pp_cxx_separate_with (pp, ',');
1215 first = false;
1216 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1217 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1219 pp_cxx_whitespace (pp);
1220 pp_equal (pp);
1221 pp_cxx_whitespace (pp);
1222 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1225 pp_cxx_right_paren (pp);
1228 /* exception-specification:
1229 throw ( type-id-list(opt) )
1231 type-id-list
1232 type-id
1233 type-id-list , type-id */
1235 static void
1236 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1238 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1240 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1241 return;
1242 pp_cxx_identifier (pp, "throw");
1243 pp_cxx_left_paren (pp);
1244 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1246 pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1247 if (TREE_CHAIN (ex_spec))
1248 pp_cxx_separate_with (pp, ',');
1250 pp_cxx_right_paren (pp);
1253 /* direct-declarator:
1254 declarator-id
1255 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1256 exception-specification(opt)
1257 direct-declaration [ constant-expression(opt) ]
1258 ( declarator ) */
1260 static void
1261 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1263 switch (TREE_CODE (t))
1265 case VAR_DECL:
1266 case PARM_DECL:
1267 case CONST_DECL:
1268 case FIELD_DECL:
1269 if (DECL_NAME (t))
1271 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1272 pp_cxx_id_expression (pp, DECL_NAME (t));
1274 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1275 break;
1277 case FUNCTION_DECL:
1278 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1279 pp_cxx_id_expression (pp, t);
1280 pp_cxx_parameter_declaration_clause (pp, t);
1282 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1284 pp_base (pp)->padding = pp_before;
1285 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1288 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1289 break;
1291 case TYPENAME_TYPE:
1292 case TEMPLATE_DECL:
1293 case TEMPLATE_TYPE_PARM:
1294 case TEMPLATE_PARM_INDEX:
1295 case TEMPLATE_TEMPLATE_PARM:
1296 break;
1298 default:
1299 pp_c_direct_declarator (pp_c_base (pp), t);
1300 break;
1304 /* declarator:
1305 direct-declarator
1306 ptr-operator declarator */
1308 static void
1309 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1311 pp_cxx_direct_declarator (pp, t);
1314 /* ctor-initializer:
1315 : mem-initializer-list
1317 mem-initializer-list:
1318 mem-initializer
1319 mem-initializer , mem-initializer-list
1321 mem-initializer:
1322 mem-initializer-id ( expression-list(opt) )
1324 mem-initializer-id:
1325 ::(opt) nested-name-specifier(opt) class-name
1326 identifier */
1328 static void
1329 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1331 t = TREE_OPERAND (t, 0);
1332 pp_cxx_whitespace (pp);
1333 pp_colon (pp);
1334 pp_cxx_whitespace (pp);
1335 for (; t; t = TREE_CHAIN (t))
1337 pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1338 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1339 if (TREE_CHAIN (t))
1340 pp_cxx_separate_with (pp, ',');
1344 /* function-definition:
1345 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1346 decl-specifier-seq(opt) declarator function-try-block */
1348 static void
1349 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1351 tree saved_scope = pp->enclosing_scope;
1352 pp_cxx_decl_specifier_seq (pp, t);
1353 pp_cxx_declarator (pp, t);
1354 pp_needs_newline (pp) = true;
1355 pp->enclosing_scope = DECL_CONTEXT (t);
1356 if (DECL_SAVED_TREE (t))
1357 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1358 else
1360 pp_cxx_semicolon (pp);
1361 pp_needs_newline (pp) = true;
1363 pp_flush (pp);
1364 pp->enclosing_scope = saved_scope;
1367 /* abstract-declarator:
1368 ptr-operator abstract-declarator(opt)
1369 direct-abstract-declarator */
1371 static void
1372 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1374 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1375 pp_cxx_right_paren (pp);
1376 else if (POINTER_TYPE_P (t))
1378 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1379 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1380 pp_cxx_right_paren (pp);
1381 t = TREE_TYPE (t);
1383 pp_cxx_direct_abstract_declarator (pp, t);
1386 /* direct-abstract-declarator:
1387 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1388 cv-qualifier-seq(opt) exception-specification(opt)
1389 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1390 ( abstract-declarator ) */
1392 static void
1393 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1395 switch (TREE_CODE (t))
1397 case REFERENCE_TYPE:
1398 pp_cxx_abstract_declarator (pp, t);
1399 break;
1401 case RECORD_TYPE:
1402 if (TYPE_PTRMEMFUNC_P (t))
1403 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1404 break;
1406 case METHOD_TYPE:
1407 case FUNCTION_TYPE:
1408 pp_cxx_parameter_declaration_clause (pp, t);
1409 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1410 if (TREE_CODE (t) == METHOD_TYPE)
1412 pp_base (pp)->padding = pp_before;
1413 pp_cxx_cv_qualifier_seq
1414 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1416 pp_cxx_exception_specification (pp, t);
1417 break;
1419 case TYPENAME_TYPE:
1420 case TEMPLATE_TYPE_PARM:
1421 case TEMPLATE_TEMPLATE_PARM:
1422 case BOUND_TEMPLATE_TEMPLATE_PARM:
1423 case UNBOUND_CLASS_TEMPLATE:
1424 break;
1426 default:
1427 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1428 break;
1432 /* type-id:
1433 type-specifier-seq abstract-declarator(opt) */
1435 static void
1436 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1438 pp_flags saved_flags = pp_c_base (pp)->flags;
1439 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1441 switch (TREE_CODE (t))
1443 case TYPE_DECL:
1444 case UNION_TYPE:
1445 case RECORD_TYPE:
1446 case ENUMERAL_TYPE:
1447 case TYPENAME_TYPE:
1448 case BOUND_TEMPLATE_TEMPLATE_PARM:
1449 case UNBOUND_CLASS_TEMPLATE:
1450 case TEMPLATE_TEMPLATE_PARM:
1451 case TEMPLATE_TYPE_PARM:
1452 case TEMPLATE_PARM_INDEX:
1453 case TEMPLATE_DECL:
1454 case TYPEOF_TYPE:
1455 case TEMPLATE_ID_EXPR:
1456 pp_cxx_type_specifier_seq (pp, t);
1457 break;
1459 default:
1460 pp_c_type_id (pp_c_base (pp), t);
1461 break;
1464 pp_c_base (pp)->flags = saved_flags;
1467 /* template-argument-list:
1468 template-argument
1469 template-argument-list, template-argument
1471 template-argument:
1472 assignment-expression
1473 type-id
1474 template-name */
1476 static void
1477 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1479 int i;
1480 if (t == NULL)
1481 return;
1482 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1484 tree arg = TREE_VEC_ELT (t, i);
1485 if (i != 0)
1486 pp_cxx_separate_with (pp, ',');
1487 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1488 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1489 pp_cxx_type_id (pp, arg);
1490 else
1491 pp_cxx_expression (pp, arg);
1496 static void
1497 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1499 t = DECL_EXPR_DECL (t);
1500 pp_cxx_type_specifier_seq (pp, t);
1501 if (TYPE_P (t))
1502 pp_cxx_abstract_declarator (pp, t);
1503 else
1504 pp_cxx_declarator (pp, t);
1507 /* Statements. */
1509 static void
1510 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1512 switch (TREE_CODE (t))
1514 case CTOR_INITIALIZER:
1515 pp_cxx_ctor_initializer (pp, t);
1516 break;
1518 case USING_STMT:
1519 pp_cxx_identifier (pp, "using");
1520 pp_cxx_identifier (pp, "namespace");
1521 if (DECL_CONTEXT (t))
1522 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1523 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1524 break;
1526 case USING_DECL:
1527 pp_cxx_identifier (pp, "using");
1528 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1529 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1530 break;
1532 case EH_SPEC_BLOCK:
1533 break;
1535 /* try-block:
1536 try compound-statement handler-seq */
1537 case TRY_BLOCK:
1538 pp_maybe_newline_and_indent (pp, 0);
1539 pp_cxx_identifier (pp, "try");
1540 pp_newline_and_indent (pp, 3);
1541 pp_cxx_statement (pp, TRY_STMTS (t));
1542 pp_newline_and_indent (pp, -3);
1543 if (CLEANUP_P (t))
1545 else
1546 pp_cxx_statement (pp, TRY_HANDLERS (t));
1547 break;
1550 handler-seq:
1551 handler handler-seq(opt)
1553 handler:
1554 catch ( exception-declaration ) compound-statement
1556 exception-declaration:
1557 type-specifier-seq declarator
1558 type-specifier-seq abstract-declarator
1559 ... */
1560 case HANDLER:
1561 pp_cxx_identifier (pp, "catch");
1562 pp_cxx_left_paren (pp);
1563 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1564 pp_cxx_right_paren (pp);
1565 pp_indentation (pp) += 3;
1566 pp_needs_newline (pp) = true;
1567 pp_cxx_statement (pp, HANDLER_BODY (t));
1568 pp_indentation (pp) -= 3;
1569 pp_needs_newline (pp) = true;
1570 break;
1572 /* selection-statement:
1573 if ( expression ) statement
1574 if ( expression ) statement else statement */
1575 case IF_STMT:
1576 pp_cxx_identifier (pp, "if");
1577 pp_cxx_whitespace (pp);
1578 pp_cxx_left_paren (pp);
1579 pp_cxx_expression (pp, IF_COND (t));
1580 pp_cxx_right_paren (pp);
1581 pp_newline_and_indent (pp, 2);
1582 pp_cxx_statement (pp, THEN_CLAUSE (t));
1583 pp_newline_and_indent (pp, -2);
1584 if (ELSE_CLAUSE (t))
1586 tree else_clause = ELSE_CLAUSE (t);
1587 pp_cxx_identifier (pp, "else");
1588 if (TREE_CODE (else_clause) == IF_STMT)
1589 pp_cxx_whitespace (pp);
1590 else
1591 pp_newline_and_indent (pp, 2);
1592 pp_cxx_statement (pp, else_clause);
1593 if (TREE_CODE (else_clause) != IF_STMT)
1594 pp_newline_and_indent (pp, -2);
1596 break;
1598 case SWITCH_STMT:
1599 pp_cxx_identifier (pp, "switch");
1600 pp_space (pp);
1601 pp_cxx_left_paren (pp);
1602 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1603 pp_cxx_right_paren (pp);
1604 pp_indentation (pp) += 3;
1605 pp_needs_newline (pp) = true;
1606 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1607 pp_newline_and_indent (pp, -3);
1608 break;
1610 /* iteration-statement:
1611 while ( expression ) statement
1612 do statement while ( expression ) ;
1613 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1614 for ( declaration expression(opt) ; expression(opt) ) statement */
1615 case WHILE_STMT:
1616 pp_cxx_identifier (pp, "while");
1617 pp_space (pp);
1618 pp_cxx_left_paren (pp);
1619 pp_cxx_expression (pp, WHILE_COND (t));
1620 pp_cxx_right_paren (pp);
1621 pp_newline_and_indent (pp, 3);
1622 pp_cxx_statement (pp, WHILE_BODY (t));
1623 pp_indentation (pp) -= 3;
1624 pp_needs_newline (pp) = true;
1625 break;
1627 case DO_STMT:
1628 pp_cxx_identifier (pp, "do");
1629 pp_newline_and_indent (pp, 3);
1630 pp_cxx_statement (pp, DO_BODY (t));
1631 pp_newline_and_indent (pp, -3);
1632 pp_cxx_identifier (pp, "while");
1633 pp_space (pp);
1634 pp_cxx_left_paren (pp);
1635 pp_cxx_expression (pp, DO_COND (t));
1636 pp_cxx_right_paren (pp);
1637 pp_cxx_semicolon (pp);
1638 pp_needs_newline (pp) = true;
1639 break;
1641 case FOR_STMT:
1642 pp_cxx_identifier (pp, "for");
1643 pp_space (pp);
1644 pp_cxx_left_paren (pp);
1645 if (FOR_INIT_STMT (t))
1646 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1647 else
1648 pp_cxx_semicolon (pp);
1649 pp_needs_newline (pp) = false;
1650 pp_cxx_whitespace (pp);
1651 if (FOR_COND (t))
1652 pp_cxx_expression (pp, FOR_COND (t));
1653 pp_cxx_semicolon (pp);
1654 pp_needs_newline (pp) = false;
1655 pp_cxx_whitespace (pp);
1656 if (FOR_EXPR (t))
1657 pp_cxx_expression (pp, FOR_EXPR (t));
1658 pp_cxx_right_paren (pp);
1659 pp_newline_and_indent (pp, 3);
1660 pp_cxx_statement (pp, FOR_BODY (t));
1661 pp_indentation (pp) -= 3;
1662 pp_needs_newline (pp) = true;
1663 break;
1665 /* jump-statement:
1666 goto identifier;
1667 continue ;
1668 return expression(opt) ; */
1669 case BREAK_STMT:
1670 case CONTINUE_STMT:
1671 pp_identifier (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1672 pp_cxx_semicolon (pp);
1673 pp_needs_newline (pp) = true;
1674 break;
1676 /* expression-statement:
1677 expression(opt) ; */
1678 case EXPR_STMT:
1679 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1680 pp_cxx_semicolon (pp);
1681 pp_needs_newline (pp) = true;
1682 break;
1684 case CLEANUP_STMT:
1685 pp_cxx_identifier (pp, "try");
1686 pp_newline_and_indent (pp, 2);
1687 pp_cxx_statement (pp, CLEANUP_BODY (t));
1688 pp_newline_and_indent (pp, -2);
1689 pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1690 pp_newline_and_indent (pp, 2);
1691 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1692 pp_newline_and_indent (pp, -2);
1693 break;
1695 default:
1696 pp_c_statement (pp_c_base (pp), t);
1697 break;
1701 /* original-namespace-definition:
1702 namespace identifier { namespace-body }
1704 As an edge case, we also handle unnamed namespace definition here. */
1706 static void
1707 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1709 pp_cxx_identifier (pp, "namespace");
1710 if (DECL_CONTEXT (t))
1711 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1712 if (DECL_NAME (t))
1713 pp_cxx_unqualified_id (pp, t);
1714 pp_cxx_whitespace (pp);
1715 pp_cxx_left_brace (pp);
1716 /* We do not print the namespace-body. */
1717 pp_cxx_whitespace (pp);
1718 pp_cxx_right_brace (pp);
1721 /* namespace-alias:
1722 identifier
1724 namespace-alias-definition:
1725 namespace identifier = qualified-namespace-specifier ;
1727 qualified-namespace-specifier:
1728 ::(opt) nested-name-specifier(opt) namespace-name */
1730 static void
1731 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1733 pp_cxx_identifier (pp, "namespace");
1734 if (DECL_CONTEXT (t))
1735 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1736 pp_cxx_unqualified_id (pp, t);
1737 pp_cxx_whitespace (pp);
1738 pp_equal (pp);
1739 pp_cxx_whitespace (pp);
1740 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1741 pp_cxx_nested_name_specifier (pp,
1742 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1743 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1744 pp_cxx_semicolon (pp);
1747 /* simple-declaration:
1748 decl-specifier-seq(opt) init-declarator-list(opt) */
1750 static void
1751 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1753 pp_cxx_decl_specifier_seq (pp, t);
1754 pp_cxx_init_declarator (pp, t);
1755 pp_cxx_semicolon (pp);
1756 pp_needs_newline (pp) = true;
1760 template-parameter-list:
1761 template-parameter
1762 template-parameter-list , template-parameter */
1764 static inline void
1765 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1767 const int n = TREE_VEC_LENGTH (t);
1768 int i;
1769 for (i = 0; i < n; ++i)
1771 if (i)
1772 pp_cxx_separate_with (pp, ',');
1773 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1777 /* template-parameter:
1778 type-parameter
1779 parameter-declaration
1781 type-parameter:
1782 class identifier(opt)
1783 class identifier(op) = type-id
1784 typename identifier(opt)
1785 typename identifier(opt) = type-id
1786 template < template-parameter-list > class identifier(opt)
1787 template < template-parameter-list > class identifier(opt) = template-name
1790 static void
1791 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1793 tree parameter = TREE_VALUE (t);
1794 switch (TREE_CODE (parameter))
1796 case TYPE_DECL:
1797 pp_cxx_identifier (pp, "class");
1798 if (DECL_NAME (parameter))
1799 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1800 /* FIXME: Chech if we should print also default argument. */
1801 break;
1803 case PARM_DECL:
1804 pp_cxx_parameter_declaration (pp, parameter);
1805 break;
1807 case TEMPLATE_DECL:
1808 break;
1810 default:
1811 pp_unsupported_tree (pp, t);
1812 break;
1816 /* Pretty-print a template parameter in the canonical form
1817 "template-parameter-<level>-<position in parameter list>". */
1819 void
1820 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1822 const enum tree_code code = TREE_CODE (parm);
1824 /* Brings type template parameters to the canonical forms. */
1825 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1826 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1827 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1829 pp_cxx_begin_template_argument_list (pp);
1830 pp_cxx_identifier (pp, "template-parameter-");
1831 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1832 pp_minus (pp);
1833 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1834 pp_cxx_end_template_argument_list (pp);
1838 template-declaration:
1839 export(opt) template < template-parameter-list > declaration */
1841 static void
1842 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1844 tree tmpl = most_general_template (t);
1845 tree level;
1846 int i = 0;
1848 pp_maybe_newline_and_indent (pp, 0);
1849 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1851 pp_cxx_identifier (pp, "template");
1852 pp_cxx_begin_template_argument_list (pp);
1853 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1854 pp_cxx_end_template_argument_list (pp);
1855 pp_newline_and_indent (pp, 3);
1856 i += 3;
1858 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1859 pp_cxx_function_definition (pp, t);
1860 else
1861 pp_cxx_simple_declaration (pp, t);
1864 static void
1865 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1867 pp_unsupported_tree (pp, t);
1870 static void
1871 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1873 pp_unsupported_tree (pp, t);
1877 declaration:
1878 block-declaration
1879 function-definition
1880 template-declaration
1881 explicit-instantiation
1882 explicit-specialization
1883 linkage-specification
1884 namespace-definition
1886 block-declaration:
1887 simple-declaration
1888 asm-definition
1889 namespace-alias-definition
1890 using-declaration
1891 using-directive */
1892 void
1893 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1895 if (!DECL_LANG_SPECIFIC (t))
1896 pp_cxx_simple_declaration (pp, t);
1897 else if (DECL_USE_TEMPLATE (t))
1898 switch (DECL_USE_TEMPLATE (t))
1900 case 1:
1901 pp_cxx_template_declaration (pp, t);
1902 break;
1904 case 2:
1905 pp_cxx_explicit_specialization (pp, t);
1906 break;
1908 case 3:
1909 pp_cxx_explicit_instantiation (pp, t);
1910 break;
1912 default:
1913 break;
1915 else switch (TREE_CODE (t))
1917 case VAR_DECL:
1918 case TYPE_DECL:
1919 pp_cxx_simple_declaration (pp, t);
1920 break;
1922 case FUNCTION_DECL:
1923 if (DECL_SAVED_TREE (t))
1924 pp_cxx_function_definition (pp, t);
1925 else
1926 pp_cxx_simple_declaration (pp, t);
1927 break;
1929 case NAMESPACE_DECL:
1930 if (DECL_NAMESPACE_ALIAS (t))
1931 pp_cxx_namespace_alias_definition (pp, t);
1932 else
1933 pp_cxx_original_namespace_definition (pp, t);
1934 break;
1936 default:
1937 pp_unsupported_tree (pp, t);
1938 break;
1943 typedef c_pretty_print_fn pp_fun;
1945 /* Initialization of a C++ pretty-printer object. */
1947 void
1948 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1950 pp_c_pretty_printer_init (pp_c_base (pp));
1951 pp_set_line_maximum_length (pp, 0);
1953 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1954 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1955 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1956 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1957 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1958 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1959 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1960 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1961 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1962 pp->c_base.direct_abstract_declarator =
1963 (pp_fun) pp_cxx_direct_abstract_declarator;
1964 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1966 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
1968 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1969 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1970 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1971 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1972 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1973 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1974 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1975 pp->c_base.expression = (pp_fun) pp_cxx_expression;
1976 pp->enclosing_scope = global_namespace;