Import a stripped down version of gcc-4.1.1
[dragonfly.git] / contrib / gcc-4.1 / gcc / cp / cxx-pretty-print.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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"
30
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);
47 \f
48
49 static inline void
50 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
51 {
52   const char *p = pp_last_position_in_text (pp);
53
54   if (p != NULL && *p == c)
55     pp_cxx_whitespace (pp);
56   pp_character (pp, c);
57   pp_base (pp)->padding = pp_none;
58 }
59
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)
70
71 void
72 pp_cxx_colon_colon (cxx_pretty_printer *pp)
73 {
74   pp_colon_colon (pp);
75   pp_base (pp)->padding = pp_none;
76 }
77
78 void
79 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
80 {
81   pp_cxx_nonconsecutive_character (pp, '<');
82 }
83
84 void
85 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
86 {
87   pp_cxx_nonconsecutive_character (pp, '>');
88 }
89
90 void
91 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
92 {
93   pp_separate_with (pp, c);
94   pp_base (pp)->padding = pp_none;
95 }
96
97 /* Expressions.  */
98
99 static inline bool
100 is_destructor_name (tree name)
101 {
102   return name == complete_dtor_identifier
103     || name == base_dtor_identifier
104     || name == deleting_dtor_identifier;
105 }
106
107 /* conversion-function-id:
108       operator conversion-type-id
109
110    conversion-type-id:
111       type-specifier-seq conversion-declarator(opt)
112
113    conversion-declarator:
114       ptr-operator conversion-declarator(opt)  */
115
116 static inline void
117 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
118 {
119   pp_cxx_identifier (pp, "operator");
120   pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
121 }
122
123 static inline void
124 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
125 {
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);
130 }
131
132 /* unqualified-id:
133      identifier
134      operator-function-id
135      conversion-function-id
136      ~ class-name
137      template-id  */
138
139 static void
140 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
141 {
142   enum tree_code code = TREE_CODE (t);
143   switch (code)
144     {
145     case RESULT_DECL:
146       pp_cxx_identifier (pp, "<return-value>");
147       break;
148
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);
162
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
169         {
170           if (is_destructor_name (t))
171             {
172               pp_complement (pp);
173               /* FIXME: Why is this necessary? */
174               if (TREE_TYPE (t))
175                 t = constructor_name (TREE_TYPE (t));
176             }
177           pp_cxx_tree_identifier (pp, t);
178         }
179       break;
180
181     case TEMPLATE_ID_EXPR:
182       pp_cxx_template_id (pp, t);
183       break;
184
185     case BASELINK:
186       pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
187       break;
188
189     case RECORD_TYPE:
190     case UNION_TYPE:
191     case ENUMERAL_TYPE:
192       pp_cxx_unqualified_id (pp, TYPE_NAME (t));
193       break;
194
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;
202
203     case TEMPLATE_PARM_INDEX:
204       pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
205       break;
206
207     default:
208       pp_unsupported_tree (pp, t);
209       break;
210     }
211 }
212
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.  */
217
218 static inline void
219 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
220 {
221   if (TREE_CODE (t) == TEMPLATE_ID_EXPR
222       && TYPE_P (scope) && dependent_type_p (scope))
223     pp_cxx_identifier (pp, "template");
224 }
225
226 /* nested-name-specifier:
227       class-or-namespace-name :: nested-name-specifier(opt)
228       class-or-namespace-name :: template nested-name-specifier   */
229
230 static void
231 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
232 {
233   if (t != NULL && t != pp->enclosing_scope)
234     {
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);
240     }
241 }
242
243 /* qualified-id:
244       nested-name-specifier template(opt) unqualified-id  */
245
246 static void
247 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
248 {
249   switch (TREE_CODE (t))
250     {
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;
256
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;
274
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;
280
281     default:
282       {
283         tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
284         if (scope != pp->enclosing_scope)
285           {
286             pp_cxx_nested_name_specifier (pp, scope);
287             pp_cxx_template_keyword_if_needed (pp, scope, t);
288           }
289         pp_cxx_unqualified_id (pp, t);
290       }
291       break;
292     }
293 }
294
295 /* id-expression:
296       unqualified-id
297       qualified-id   */
298
299 static inline void
300 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
301 {
302   if (TREE_CODE (t) == OVERLOAD)
303     t = OVL_CURRENT (t);
304   if (DECL_P (t) && DECL_CONTEXT (t))
305     pp_cxx_qualified_id (pp, t);
306   else
307     pp_cxx_unqualified_id (pp, t);
308 }
309
310 /* primary-expression:
311      literal
312      this
313      :: identifier
314      :: operator-function-id
315      :: qualifier-id
316      ( expression )
317      id-expression   */
318
319 static void
320 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
321 {
322   switch (TREE_CODE (t))
323     {
324     case STRING_CST:
325     case INTEGER_CST:
326     case REAL_CST:
327       pp_c_constant (pp_c_base (pp), t);
328       break;
329
330     case BASELINK:
331       t = BASELINK_FUNCTIONS (t);
332     case VAR_DECL:
333     case PARM_DECL:
334     case FIELD_DECL:
335     case FUNCTION_DECL:
336     case OVERLOAD:
337     case CONST_DECL:
338     case TEMPLATE_DECL:
339       pp_cxx_id_expression (pp, t);
340       break;
341
342     case RESULT_DECL:
343     case TEMPLATE_TYPE_PARM:
344     case TEMPLATE_TEMPLATE_PARM:
345     case TEMPLATE_PARM_INDEX:
346       pp_cxx_unqualified_id (pp, t);
347       break;
348
349     case STMT_EXPR:
350       pp_cxx_left_paren (pp);
351       pp_cxx_statement (pp, STMT_EXPR_STMT (t));
352       pp_cxx_right_paren (pp);
353       break;
354
355     default:
356       pp_c_primary_expression (pp_c_base (pp), t);
357       break;
358     }
359 }
360
361 /* postfix-expression:
362      primary-expression
363      postfix-expression [ expression ]
364      postfix-expression ( expression-list(opt) )
365      simple-type-specifier ( expression-list(opt) )
366      typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
367      typename ::(opt) nested-name-specifier template(opt)
368                                        template-id ( expression-list(opt) )
369      postfix-expression . template(opt) ::(opt) id-expression
370      postfix-expression -> template(opt) ::(opt) id-expression
371      postfix-expression . pseudo-destructor-name
372      postfix-expression -> pseudo-destructor-name
373      postfix-expression ++
374      postfix-expression --
375      dynamic_cast < type-id > ( expression )
376      static_cast < type-id > ( expression )
377      reinterpret_cast < type-id > ( expression )
378      const_cast < type-id > ( expression )
379      typeid ( expression )
380      typeif ( type-id )  */
381
382 static void
383 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
384 {
385   enum tree_code code = TREE_CODE (t);
386
387   switch (code)
388     {
389     case AGGR_INIT_EXPR:
390     case CALL_EXPR:
391       {
392         tree fun = TREE_OPERAND (t, 0);
393         tree args = TREE_OPERAND (t, 1);
394         tree saved_scope = pp->enclosing_scope;
395
396         if (TREE_CODE (fun) == ADDR_EXPR)
397           fun = TREE_OPERAND (fun, 0);
398
399         /* In templates, where there is no way to tell whether a given
400            call uses an actual member function.  So the parser builds
401            FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
402            instantiation time.  */
403         if (TREE_CODE (fun) != FUNCTION_DECL)
404           ;
405         else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
406           {
407             tree object = code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t)
408               ? TREE_OPERAND (t, 2)
409               : TREE_VALUE (args);
410
411             while (TREE_CODE (object) == NOP_EXPR)
412               object = TREE_OPERAND (object, 0);
413
414             if (TREE_CODE (object) == ADDR_EXPR)
415               object = TREE_OPERAND (object, 0);
416
417             if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
418               {
419                 pp_cxx_postfix_expression (pp, object);
420                 pp_cxx_dot (pp);
421               }
422             else
423               {
424                 pp_cxx_postfix_expression (pp, object);
425                 pp_cxx_arrow (pp);
426               }
427             args = TREE_CHAIN (args);
428             pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
429           }
430
431         pp_cxx_postfix_expression (pp, fun);
432         pp->enclosing_scope = saved_scope;
433         pp_cxx_call_argument_list (pp, args);
434       }
435       if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
436         {
437           pp_cxx_separate_with (pp, ',');
438           pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 2));
439         }
440       break;
441
442     case BASELINK:
443     case VAR_DECL:
444     case PARM_DECL:
445     case FIELD_DECL:
446     case FUNCTION_DECL:
447     case OVERLOAD:
448     case CONST_DECL:
449     case TEMPLATE_DECL:
450     case RESULT_DECL:
451       pp_cxx_primary_expression (pp, t);
452       break;
453
454     case DYNAMIC_CAST_EXPR:
455     case STATIC_CAST_EXPR:
456     case REINTERPRET_CAST_EXPR:
457     case CONST_CAST_EXPR:
458       if (code == DYNAMIC_CAST_EXPR)
459         pp_cxx_identifier (pp, "dynamic_cast");
460       else if (code == STATIC_CAST_EXPR)
461         pp_cxx_identifier (pp, "static_cast");
462       else if (code == REINTERPRET_CAST_EXPR)
463         pp_cxx_identifier (pp, "reinterpret_cast");
464       else
465         pp_cxx_identifier (pp, "const_cast");
466       pp_cxx_begin_template_argument_list (pp);
467       pp_cxx_type_id (pp, TREE_TYPE (t));
468       pp_cxx_end_template_argument_list (pp);
469       pp_left_paren (pp);
470       pp_cxx_expression (pp, TREE_OPERAND (t, 0));
471       pp_right_paren (pp);
472       break;
473
474     case EMPTY_CLASS_EXPR:
475       pp_cxx_type_id (pp, TREE_TYPE (t));
476       pp_left_paren (pp);
477       pp_right_paren (pp);
478       break;
479
480     case TYPEID_EXPR:
481       t = TREE_OPERAND (t, 0);
482       pp_cxx_identifier (pp, "typeid");
483       pp_left_paren (pp);
484       if (TYPE_P (t))
485         pp_cxx_type_id (pp, t);
486       else
487         pp_cxx_expression (pp, t);
488       pp_right_paren (pp);
489       break;
490
491     case PSEUDO_DTOR_EXPR:
492       pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
493       pp_cxx_dot (pp);
494       pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
495       pp_cxx_colon_colon (pp);
496       pp_complement (pp);
497       pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
498       break;
499
500     case ARROW_EXPR:
501       pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
502       pp_cxx_arrow (pp);
503       break;
504
505     default:
506       pp_c_postfix_expression (pp_c_base (pp), t);
507       break;
508     }
509 }
510
511 /* new-expression:
512       ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
513       ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
514
515    new-placement:
516       ( expression-list )
517
518    new-type-id:
519       type-specifier-seq new-declarator(opt)
520
521    new-declarator:
522       ptr-operator new-declarator(opt)
523       direct-new-declarator
524
525    direct-new-declarator
526       [ expression ]
527       direct-new-declarator [ constant-expression ]
528
529    new-initializer:
530       ( expression-list(opt) )  */
531
532 static void
533 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
534 {
535   enum tree_code code = TREE_CODE (t);
536   switch (code)
537     {
538     case NEW_EXPR:
539     case VEC_NEW_EXPR:
540       if (NEW_EXPR_USE_GLOBAL (t))
541         pp_cxx_colon_colon (pp);
542       pp_cxx_identifier (pp, "new");
543       if (TREE_OPERAND (t, 0))
544         {
545           pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
546           pp_space (pp);
547         }
548       /* FIXME: array-types are built with one more element.  */
549       pp_cxx_type_id (pp, TREE_OPERAND (t, 1));
550       if (TREE_OPERAND (t, 2))
551         {
552           pp_left_paren (pp);
553           t = TREE_OPERAND (t, 2);
554           if (TREE_CODE (t) == TREE_LIST)
555             pp_c_expression_list (pp_c_base (pp), t);
556           else if (t == void_zero_node)
557             ;                   /* OK, empty initializer list.  */
558           else
559             pp_cxx_expression (pp, t);
560           pp_right_paren (pp);
561         }
562       break;
563
564     default:
565       pp_unsupported_tree (pp, t);
566     }
567 }
568
569 /* delete-expression:
570       ::(opt) delete cast-expression
571       ::(opt) delete [ ] cast-expression   */
572
573 static void
574 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
575 {
576   enum tree_code code = TREE_CODE (t);
577   switch (code)
578     {
579     case DELETE_EXPR:
580     case VEC_DELETE_EXPR:
581       if (DELETE_EXPR_USE_GLOBAL (t))
582         pp_cxx_colon_colon (pp);
583       pp_cxx_identifier (pp, "delete");
584       if (code == VEC_DELETE_EXPR)
585         {
586           pp_left_bracket (pp);
587           pp_right_bracket (pp);
588         }
589       pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
590       break;
591
592     default:
593       pp_unsupported_tree (pp, t);
594     }
595 }
596
597 /* unary-expression:
598       postfix-expression
599       ++ cast-expression
600       -- cast-expression
601       unary-operator cast-expression
602       sizeof unary-expression
603       sizeof ( type-id )
604       new-expression
605       delete-expression
606
607    unary-operator: one of
608       *   &   +   -  !
609
610    GNU extensions:
611       __alignof__ unary-expression
612       __alignof__ ( type-id )  */
613
614 static void
615 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
616 {
617   enum tree_code code = TREE_CODE (t);
618   switch (code)
619     {
620     case NEW_EXPR:
621     case VEC_NEW_EXPR:
622       pp_cxx_new_expression (pp, t);
623       break;
624
625     case DELETE_EXPR:
626     case VEC_DELETE_EXPR:
627       pp_cxx_delete_expression (pp, t);
628       break;
629
630     case SIZEOF_EXPR:
631     case ALIGNOF_EXPR:
632       pp_cxx_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
633       pp_cxx_whitespace (pp);
634       if (TYPE_P (TREE_OPERAND (t, 0)))
635         {
636           pp_cxx_left_paren (pp);
637           pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
638           pp_cxx_right_paren (pp);
639         }
640       else
641         pp_unary_expression (pp, TREE_OPERAND (t, 0));
642       break;
643
644     case UNARY_PLUS_EXPR:
645       pp_plus (pp);
646       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
647       break;
648
649     default:
650       pp_c_unary_expression (pp_c_base (pp), t);
651       break;
652     }
653 }
654
655 /* cast-expression:
656       unary-expression
657       ( type-id ) cast-expression  */
658
659 static void
660 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
661 {
662   switch (TREE_CODE (t))
663     {
664     case CAST_EXPR:
665       pp_cxx_type_id (pp, TREE_TYPE (t));
666       pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
667       break;
668
669     default:
670       pp_c_cast_expression (pp_c_base (pp), t);
671       break;
672     }
673 }
674
675 /* pm-expression:
676       cast-expression
677       pm-expression .* cast-expression
678       pm-expression ->* cast-expression  */
679
680 static void
681 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
682 {
683   switch (TREE_CODE (t))
684     {
685       /* Handle unfortunate OFFESET_REF overloading here.  */
686     case OFFSET_REF:
687       if (TYPE_P (TREE_OPERAND (t, 0)))
688         {
689           pp_cxx_qualified_id (pp, t);
690           break;
691         }
692       /* Else fall through.  */
693     case MEMBER_REF:
694     case DOTSTAR_EXPR:
695       pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
696       pp_cxx_dot (pp);
697       pp_star(pp);
698       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
699       break;
700
701
702     default:
703       pp_cxx_cast_expression (pp, t);
704       break;
705     }
706 }
707
708 /* multiplicative-expression:
709       pm-expression
710       multiplicative-expression * pm-expression
711       multiplicative-expression / pm-expression
712       multiplicative-expression % pm-expression  */
713
714 static void
715 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
716 {
717   enum tree_code code = TREE_CODE (e);
718   switch (code)
719     {
720     case MULT_EXPR:
721     case TRUNC_DIV_EXPR:
722     case TRUNC_MOD_EXPR:
723       pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
724       pp_space (pp);
725       if (code == MULT_EXPR)
726         pp_star (pp);
727       else if (code == TRUNC_DIV_EXPR)
728         pp_slash (pp);
729       else
730         pp_modulo (pp);
731       pp_space (pp);
732       pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
733       break;
734
735     default:
736       pp_cxx_pm_expression (pp, e);
737       break;
738     }
739 }
740
741 /* conditional-expression:
742       logical-or-expression
743       logical-or-expression ?  expression  : assignment-expression  */
744
745 static void
746 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
747 {
748   if (TREE_CODE (e) == COND_EXPR)
749     {
750       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
751       pp_space (pp);
752       pp_question (pp);
753       pp_space (pp);
754       pp_cxx_expression (pp, TREE_OPERAND (e, 1));
755       pp_space (pp);
756       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
757     }
758   else
759     pp_c_logical_or_expression (pp_c_base (pp), e);
760 }
761
762 /* Pretty-print a compound assignment operator token as indicated by T.  */
763
764 static void
765 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
766 {
767   const char *op;
768
769   switch (TREE_CODE (t))
770     {
771     case NOP_EXPR:
772       op = "=";
773       break;
774
775     case PLUS_EXPR:
776       op = "+=";
777       break;
778
779     case MINUS_EXPR:
780       op = "-=";
781       break;
782
783     case TRUNC_DIV_EXPR:
784       op = "/=";
785       break;
786
787     case TRUNC_MOD_EXPR:
788       op = "%=";
789       break;
790
791     default:
792       op = tree_code_name[TREE_CODE (t)];
793       break;
794     }
795
796   pp_cxx_identifier (pp, op);
797 }
798
799
800 /* assignment-expression:
801       conditional-expression
802       logical-or-expression assignment-operator assignment-expression
803       throw-expression
804
805    throw-expression:
806        throw assignment-expression(opt)
807
808    assignment-operator: one of
809       =    *=    /=    %=    +=    -=    >>=    <<=    &=    ^=    |=  */
810
811 static void
812 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
813 {
814   switch (TREE_CODE (e))
815     {
816     case MODIFY_EXPR:
817     case INIT_EXPR:
818       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
819       pp_space (pp);
820       pp_equal (pp);
821       pp_space (pp);
822       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
823       break;
824
825     case THROW_EXPR:
826       pp_cxx_identifier (pp, "throw");
827       if (TREE_OPERAND (e, 0))
828         pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
829       break;
830
831     case MODOP_EXPR:
832       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
833       pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
834       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
835       break;
836
837     default:
838       pp_cxx_conditional_expression (pp, e);
839       break;
840     }
841 }
842
843 static void
844 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
845 {
846   switch (TREE_CODE (t))
847     {
848     case STRING_CST:
849     case INTEGER_CST:
850     case REAL_CST:
851       pp_c_constant (pp_c_base (pp), t);
852       break;
853
854     case RESULT_DECL:
855       pp_cxx_unqualified_id (pp, t);
856       break;
857
858 #if 0
859     case OFFSET_REF:
860 #endif
861     case SCOPE_REF:
862     case PTRMEM_CST:
863       pp_cxx_qualified_id (pp, t);
864       break;
865
866     case OVERLOAD:
867       t = OVL_CURRENT (t);
868     case VAR_DECL:
869     case PARM_DECL:
870     case FIELD_DECL:
871     case CONST_DECL:
872     case FUNCTION_DECL:
873     case BASELINK:
874     case TEMPLATE_DECL:
875     case TEMPLATE_TYPE_PARM:
876     case TEMPLATE_PARM_INDEX:
877     case TEMPLATE_TEMPLATE_PARM:
878     case STMT_EXPR:
879       pp_cxx_primary_expression (pp, t);
880       break;
881
882     case CALL_EXPR:
883     case DYNAMIC_CAST_EXPR:
884     case STATIC_CAST_EXPR:
885     case REINTERPRET_CAST_EXPR:
886     case CONST_CAST_EXPR:
887 #if 0
888     case MEMBER_REF:
889 #endif
890     case EMPTY_CLASS_EXPR:
891     case TYPEID_EXPR:
892     case PSEUDO_DTOR_EXPR:
893     case AGGR_INIT_EXPR:
894     case ARROW_EXPR:
895       pp_cxx_postfix_expression (pp, t);
896       break;
897
898     case NEW_EXPR:
899     case VEC_NEW_EXPR:
900       pp_cxx_new_expression (pp, t);
901       break;
902
903     case DELETE_EXPR:
904     case VEC_DELETE_EXPR:
905       pp_cxx_delete_expression (pp, t);
906       break;
907
908     case SIZEOF_EXPR:
909     case ALIGNOF_EXPR:
910       pp_cxx_unary_expression (pp, t);
911       break;
912
913     case CAST_EXPR:
914       pp_cxx_cast_expression (pp, t);
915       break;
916
917     case OFFSET_REF:
918     case MEMBER_REF:
919     case DOTSTAR_EXPR:
920       pp_cxx_pm_expression (pp, t);
921       break;
922
923     case MULT_EXPR:
924     case TRUNC_DIV_EXPR:
925     case TRUNC_MOD_EXPR:
926       pp_cxx_multiplicative_expression (pp, t);
927       break;
928
929     case COND_EXPR:
930       pp_cxx_conditional_expression (pp, t);
931       break;
932
933     case MODIFY_EXPR:
934     case INIT_EXPR:
935     case THROW_EXPR:
936     case MODOP_EXPR:
937       pp_cxx_assignment_expression (pp, t);
938       break;
939
940     case NON_DEPENDENT_EXPR:
941     case MUST_NOT_THROW_EXPR:
942       pp_cxx_expression (pp, t);
943       break;
944
945     default:
946       pp_c_expression (pp_c_base (pp), t);
947       break;
948     }
949 }
950
951
952 /* Declarations.  */
953
954 /* function-specifier:
955       inline
956       virtual
957       explicit   */
958
959 static void
960 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
961 {
962   switch (TREE_CODE (t))
963     {
964     case FUNCTION_DECL:
965       if (DECL_VIRTUAL_P (t))
966         pp_cxx_identifier (pp, "virtual");
967       else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
968         pp_cxx_identifier (pp, "explicit");
969       else
970         pp_c_function_specifier (pp_c_base (pp), t);
971
972     default:
973       break;
974     }
975 }
976
977 /* decl-specifier-seq:
978       decl-specifier-seq(opt) decl-specifier
979
980    decl-specifier:
981       storage-class-specifier
982       type-specifier
983       function-specifier
984       friend
985       typedef  */
986
987 static void
988 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
989 {
990   switch (TREE_CODE (t))
991     {
992     case VAR_DECL:
993     case PARM_DECL:
994     case CONST_DECL:
995     case FIELD_DECL:
996       pp_cxx_storage_class_specifier (pp, t);
997       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
998       break;
999
1000     case TYPE_DECL:
1001       pp_cxx_identifier (pp, "typedef");
1002       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1003       break;
1004
1005     case RECORD_TYPE:
1006       if (TYPE_PTRMEMFUNC_P (t))
1007         {
1008           tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1009           pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1010           pp_cxx_whitespace (pp);
1011           pp_cxx_ptr_operator (pp, t);
1012         }
1013       break;
1014
1015     case FUNCTION_DECL:
1016       /* Constructors don't have return types.  And conversion functions
1017          do not have a type-specifier in their return types.  */
1018       if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1019         pp_cxx_function_specifier (pp, t);
1020       else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1021         pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1022       else
1023         default:
1024       pp_c_declaration_specifiers (pp_c_base (pp), t);
1025       break;
1026     }
1027 }
1028
1029 /* simple-type-specifier:
1030       ::(opt) nested-name-specifier(opt) type-name
1031       ::(opt) nested-name-specifier(opt) template(opt) template-id
1032       char
1033       wchar_t
1034       bool
1035       short
1036       int
1037       long
1038       signed
1039       unsigned
1040       float
1041       double
1042       void  */
1043
1044 static void
1045 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1046 {
1047   switch (TREE_CODE (t))
1048     {
1049     case RECORD_TYPE:
1050     case UNION_TYPE:
1051     case ENUMERAL_TYPE:
1052       pp_cxx_qualified_id (pp, t);
1053       break;
1054
1055     case TEMPLATE_TYPE_PARM:
1056     case TEMPLATE_TEMPLATE_PARM:
1057     case TEMPLATE_PARM_INDEX:
1058       pp_cxx_unqualified_id (pp, t);
1059       break;
1060
1061     case TYPENAME_TYPE:
1062       pp_cxx_identifier (pp, "typename");
1063       pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1064       pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1065       break;
1066
1067     default:
1068       pp_c_type_specifier (pp_c_base (pp), t);
1069       break;
1070     }
1071 }
1072
1073 /* type-specifier-seq:
1074       type-specifier type-specifier-seq(opt)
1075
1076    type-specifier:
1077       simple-type-specifier
1078       class-specifier
1079       enum-specifier
1080       elaborated-type-specifier
1081       cv-qualifier   */
1082
1083 static void
1084 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1085 {
1086   switch (TREE_CODE (t))
1087     {
1088     case TEMPLATE_DECL:
1089     case TEMPLATE_TYPE_PARM:
1090     case TEMPLATE_TEMPLATE_PARM:
1091     case TYPE_DECL:
1092     case BOUND_TEMPLATE_TEMPLATE_PARM:
1093       pp_cxx_cv_qualifier_seq (pp, t);
1094       pp_cxx_simple_type_specifier (pp, t);
1095       break;
1096
1097     case METHOD_TYPE:
1098       pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1099       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1100       pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1101       break;
1102
1103     default:
1104       if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1105         pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1106     }
1107 }
1108
1109 /* ptr-operator:
1110       * cv-qualifier-seq(opt)
1111       &
1112       ::(opt) nested-name-specifier * cv-qualifier-seq(opt)  */
1113
1114 static void
1115 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1116 {
1117   if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1118     t = TREE_TYPE (t);
1119   switch (TREE_CODE (t))
1120     {
1121     case REFERENCE_TYPE:
1122     case POINTER_TYPE:
1123       if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1124           || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1125         pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1126       if (TREE_CODE (t) == POINTER_TYPE)
1127         {
1128           pp_star (pp);
1129           pp_cxx_cv_qualifier_seq (pp, t);
1130         }
1131       else
1132         pp_ampersand (pp);
1133       break;
1134
1135     case RECORD_TYPE:
1136       if (TYPE_PTRMEMFUNC_P (t))
1137         {
1138           pp_cxx_left_paren (pp);
1139           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1140           pp_star (pp);
1141           break;
1142         }
1143     case OFFSET_TYPE:
1144       if (TYPE_PTR_TO_MEMBER_P (t))
1145         {
1146           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1147             pp_cxx_left_paren (pp);
1148           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1149           pp_star (pp);
1150           pp_cxx_cv_qualifier_seq (pp, t);
1151           break;
1152         }
1153       /* else fall through.  */
1154
1155     default:
1156       pp_unsupported_tree (pp, t);
1157       break;
1158     }
1159 }
1160
1161 static inline tree
1162 pp_cxx_implicit_parameter_type (tree mf)
1163 {
1164   return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1165 }
1166
1167 /*
1168    parameter-declaration:
1169       decl-specifier-seq declarator
1170       decl-specifier-seq declarator = assignment-expression
1171       decl-specifier-seq abstract-declarator(opt)
1172       decl-specifier-seq abstract-declarator(opt) assignment-expression  */
1173
1174 static inline void
1175 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1176 {
1177   pp_cxx_decl_specifier_seq (pp, t);
1178   if (TYPE_P (t))
1179     pp_cxx_abstract_declarator (pp, t);
1180   else
1181     pp_cxx_declarator (pp, t);
1182 }
1183
1184 /* parameter-declaration-clause:
1185       parameter-declaration-list(opt) ...(opt)
1186       parameter-declaration-list , ...
1187
1188    parameter-declaration-list:
1189       parameter-declaration
1190       parameter-declaration-list , parameter-declaration  */
1191
1192 static void
1193 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1194 {
1195   tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1196   tree types =
1197     TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1198   const bool abstract = args == NULL
1199     || pp_c_base (pp)->flags & pp_c_flag_abstract;
1200   bool first = true;
1201
1202   /* Skip artificial parameter for nonstatic member functions.  */
1203   if (TREE_CODE (t) == METHOD_TYPE)
1204     types = TREE_CHAIN (types);
1205
1206   pp_cxx_left_paren (pp);
1207   for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1208     {
1209       if (!first)
1210         pp_cxx_separate_with (pp, ',');
1211       first = false;
1212       pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1213       if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1214         {
1215           pp_cxx_whitespace (pp);
1216           pp_equal (pp);
1217           pp_cxx_whitespace (pp);
1218           pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1219         }
1220     }
1221   pp_cxx_right_paren (pp);
1222 }
1223
1224 /* exception-specification:
1225       throw ( type-id-list(opt) )
1226
1227    type-id-list
1228       type-id
1229       type-id-list , type-id   */
1230
1231 static void
1232 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1233 {
1234   tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1235
1236   if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1237     return;
1238   pp_cxx_identifier (pp, "throw");
1239   pp_cxx_left_paren (pp);
1240   for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1241     {
1242       pp_cxx_type_id (pp, TREE_VALUE (ex_spec));
1243       if (TREE_CHAIN (ex_spec))
1244         pp_cxx_separate_with (pp, ',');
1245     }
1246   pp_cxx_right_paren (pp);
1247 }
1248
1249 /* direct-declarator:
1250       declarator-id
1251       direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1252                                             exception-specification(opt)
1253       direct-declaration [ constant-expression(opt) ]
1254       ( declarator )  */
1255
1256 static void
1257 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1258 {
1259   switch (TREE_CODE (t))
1260     {
1261     case VAR_DECL:
1262     case PARM_DECL:
1263     case CONST_DECL:
1264     case FIELD_DECL:
1265       if (DECL_NAME (t))
1266         {
1267           pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1268           pp_cxx_id_expression (pp, DECL_NAME (t));
1269         }
1270       pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1271       break;
1272
1273     case FUNCTION_DECL:
1274       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1275       pp_cxx_id_expression (pp, t);
1276       pp_cxx_parameter_declaration_clause (pp, t);
1277
1278       if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1279         {
1280           pp_base (pp)->padding = pp_before;
1281           pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1282         }
1283
1284       pp_cxx_exception_specification (pp, TREE_TYPE (t));
1285       break;
1286
1287     case TYPENAME_TYPE:
1288     case TEMPLATE_DECL:
1289     case TEMPLATE_TYPE_PARM:
1290     case TEMPLATE_PARM_INDEX:
1291     case TEMPLATE_TEMPLATE_PARM:
1292       break;
1293
1294     default:
1295       pp_c_direct_declarator (pp_c_base (pp), t);
1296       break;
1297     }
1298 }
1299
1300 /* declarator:
1301    direct-declarator
1302    ptr-operator declarator  */
1303
1304 static void
1305 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1306 {
1307   pp_cxx_direct_declarator (pp, t);
1308 }
1309
1310 /* ctor-initializer:
1311       : mem-initializer-list
1312
1313    mem-initializer-list:
1314       mem-initializer
1315       mem-initializer , mem-initializer-list
1316
1317    mem-initializer:
1318       mem-initializer-id ( expression-list(opt) )
1319
1320    mem-initializer-id:
1321       ::(opt) nested-name-specifier(opt) class-name
1322       identifier   */
1323
1324 static void
1325 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1326 {
1327   t = TREE_OPERAND (t, 0);
1328   pp_cxx_whitespace (pp);
1329   pp_colon (pp);
1330   pp_cxx_whitespace (pp);
1331   for (; t; t = TREE_CHAIN (t))
1332     {
1333       pp_cxx_primary_expression (pp, TREE_PURPOSE (t));
1334       pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1335       if (TREE_CHAIN (t))
1336         pp_cxx_separate_with (pp, ',');
1337     }
1338 }
1339
1340 /* function-definition:
1341       decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1342       decl-specifier-seq(opt) declarator function-try-block  */
1343
1344 static void
1345 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1346 {
1347   tree saved_scope = pp->enclosing_scope;
1348   pp_cxx_decl_specifier_seq (pp, t);
1349   pp_cxx_declarator (pp, t);
1350   pp_needs_newline (pp) = true;
1351   pp->enclosing_scope = DECL_CONTEXT (t);
1352   if (DECL_SAVED_TREE (t))
1353     pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1354   else
1355     {
1356       pp_cxx_semicolon (pp);
1357       pp_needs_newline (pp) = true;
1358     }
1359   pp_flush (pp);
1360   pp->enclosing_scope = saved_scope;
1361 }
1362
1363 /* abstract-declarator:
1364       ptr-operator abstract-declarator(opt)
1365       direct-abstract-declarator  */
1366
1367 static void
1368 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1369 {
1370   if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1371     pp_cxx_right_paren (pp);
1372   else if (POINTER_TYPE_P (t))
1373     {
1374       if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1375           || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1376         pp_cxx_right_paren (pp);
1377       t = TREE_TYPE (t);
1378     }
1379   pp_cxx_direct_abstract_declarator (pp, t);
1380 }
1381
1382 /* direct-abstract-declarator:
1383       direct-abstract-declarator(opt) ( parameter-declaration-clause )
1384                            cv-qualifier-seq(opt) exception-specification(opt)
1385       direct-abstract-declarator(opt) [ constant-expression(opt) ]
1386       ( abstract-declarator )  */
1387
1388 static void
1389 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1390 {
1391   switch (TREE_CODE (t))
1392     {
1393     case REFERENCE_TYPE:
1394       pp_cxx_abstract_declarator (pp, t);
1395       break;
1396
1397     case RECORD_TYPE:
1398       if (TYPE_PTRMEMFUNC_P (t))
1399         pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1400       break;
1401
1402     case METHOD_TYPE:
1403     case FUNCTION_TYPE:
1404       pp_cxx_parameter_declaration_clause (pp, t);
1405       pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1406       if (TREE_CODE (t) == METHOD_TYPE)
1407         {
1408           pp_base (pp)->padding = pp_before;
1409           pp_cxx_cv_qualifier_seq
1410             (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1411         }
1412       pp_cxx_exception_specification (pp, t);
1413       break;
1414
1415     case TYPENAME_TYPE:
1416     case TEMPLATE_TYPE_PARM:
1417     case TEMPLATE_TEMPLATE_PARM:
1418     case BOUND_TEMPLATE_TEMPLATE_PARM:
1419     case UNBOUND_CLASS_TEMPLATE:
1420       break;
1421
1422     default:
1423       pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1424       break;
1425     }
1426 }
1427
1428 /* type-id:
1429      type-specifier-seq abstract-declarator(opt) */
1430
1431 static void
1432 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1433 {
1434   pp_flags saved_flags = pp_c_base (pp)->flags;
1435   pp_c_base (pp)->flags |= pp_c_flag_abstract;
1436
1437   switch (TREE_CODE (t))
1438     {
1439     case TYPE_DECL:
1440     case UNION_TYPE:
1441     case RECORD_TYPE:
1442     case ENUMERAL_TYPE:
1443     case TYPENAME_TYPE:
1444     case BOUND_TEMPLATE_TEMPLATE_PARM:
1445     case UNBOUND_CLASS_TEMPLATE:
1446     case TEMPLATE_TEMPLATE_PARM:
1447     case TEMPLATE_TYPE_PARM:
1448     case TEMPLATE_PARM_INDEX:
1449     case TEMPLATE_DECL:
1450     case TYPEOF_TYPE:
1451     case TEMPLATE_ID_EXPR:
1452       pp_cxx_type_specifier_seq (pp, t);
1453       break;
1454
1455     default:
1456       pp_c_type_id (pp_c_base (pp), t);
1457       break;
1458     }
1459
1460   pp_c_base (pp)->flags = saved_flags;
1461 }
1462
1463 /* template-argument-list:
1464       template-argument
1465       template-argument-list, template-argument
1466
1467    template-argument:
1468       assignment-expression
1469       type-id
1470       template-name   */
1471
1472 static void
1473 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1474 {
1475   int i;
1476   if (t == NULL)
1477     return;
1478   for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1479     {
1480       tree arg = TREE_VEC_ELT (t, i);
1481       if (i != 0)
1482         pp_cxx_separate_with (pp, ',');
1483       if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1484                            && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1485         pp_cxx_type_id (pp, arg);
1486       else
1487         pp_cxx_expression (pp, arg);
1488     }
1489 }
1490
1491
1492 static void
1493 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1494 {
1495   t = DECL_EXPR_DECL (t);
1496   pp_cxx_type_specifier_seq (pp, t);
1497   if (TYPE_P (t))
1498     pp_cxx_abstract_declarator (pp, t);
1499   else
1500     pp_cxx_declarator (pp, t);
1501 }
1502
1503 /* Statements.  */
1504
1505 static void
1506 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1507 {
1508   switch (TREE_CODE (t))
1509     {
1510     case CTOR_INITIALIZER:
1511       pp_cxx_ctor_initializer (pp, t);
1512       break;
1513
1514     case USING_STMT:
1515       pp_cxx_identifier (pp, "using");
1516       pp_cxx_identifier (pp, "namespace");
1517       if (DECL_CONTEXT (t))
1518         pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1519       pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1520       break;
1521
1522     case USING_DECL:
1523       pp_cxx_identifier (pp, "using");
1524       pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1525       pp_cxx_unqualified_id (pp, DECL_NAME (t));
1526       break;
1527
1528     case EH_SPEC_BLOCK:
1529       break;
1530
1531       /* try-block:
1532             try compound-statement handler-seq  */
1533     case TRY_BLOCK:
1534       pp_maybe_newline_and_indent (pp, 0);
1535       pp_cxx_identifier (pp, "try");
1536       pp_newline_and_indent (pp, 3);
1537       pp_cxx_statement (pp, TRY_STMTS (t));
1538       pp_newline_and_indent (pp, -3);
1539       if (CLEANUP_P (t))
1540         ;
1541       else
1542         pp_cxx_statement (pp, TRY_HANDLERS (t));
1543       break;
1544
1545       /*
1546          handler-seq:
1547             handler handler-seq(opt)
1548
1549          handler:
1550          catch ( exception-declaration ) compound-statement
1551
1552          exception-declaration:
1553             type-specifier-seq declarator
1554             type-specifier-seq abstract-declarator
1555             ...   */
1556     case HANDLER:
1557       pp_cxx_identifier (pp, "catch");
1558       pp_cxx_left_paren (pp);
1559       pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1560       pp_cxx_right_paren (pp);
1561       pp_indentation (pp) += 3;
1562       pp_needs_newline (pp) = true;
1563       pp_cxx_statement (pp, HANDLER_BODY (t));
1564       pp_indentation (pp) -= 3;
1565       pp_needs_newline (pp) = true;
1566       break;
1567
1568       /* selection-statement:
1569             if ( expression ) statement
1570             if ( expression ) statement else statement  */
1571     case IF_STMT:
1572       pp_cxx_identifier (pp, "if");
1573       pp_cxx_whitespace (pp);
1574       pp_cxx_left_paren (pp);
1575       pp_cxx_expression (pp, IF_COND (t));
1576       pp_cxx_right_paren (pp);
1577       pp_newline_and_indent (pp, 2);
1578       pp_cxx_statement (pp, THEN_CLAUSE (t));
1579       pp_newline_and_indent (pp, -2);
1580       if (ELSE_CLAUSE (t))
1581         {
1582           tree else_clause = ELSE_CLAUSE (t);
1583           pp_cxx_identifier (pp, "else");
1584           if (TREE_CODE (else_clause) == IF_STMT)
1585             pp_cxx_whitespace (pp);
1586           else
1587             pp_newline_and_indent (pp, 2);
1588           pp_cxx_statement (pp, else_clause);
1589           if (TREE_CODE (else_clause) != IF_STMT)
1590             pp_newline_and_indent (pp, -2);
1591         }
1592       break;
1593
1594     case SWITCH_STMT:
1595       pp_cxx_identifier (pp, "switch");
1596       pp_space (pp);
1597       pp_cxx_left_paren (pp);
1598       pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1599       pp_cxx_right_paren (pp);
1600       pp_indentation (pp) += 3;
1601       pp_needs_newline (pp) = true;
1602       pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1603       pp_newline_and_indent (pp, -3);
1604       break;
1605
1606       /* iteration-statement:
1607             while ( expression ) statement
1608             do statement while ( expression ) ;
1609             for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1610             for ( declaration expression(opt) ; expression(opt) ) statement  */
1611     case WHILE_STMT:
1612       pp_cxx_identifier (pp, "while");
1613       pp_space (pp);
1614       pp_cxx_left_paren (pp);
1615       pp_cxx_expression (pp, WHILE_COND (t));
1616       pp_cxx_right_paren (pp);
1617       pp_newline_and_indent (pp, 3);
1618       pp_cxx_statement (pp, WHILE_BODY (t));
1619       pp_indentation (pp) -= 3;
1620       pp_needs_newline (pp) = true;
1621       break;
1622
1623     case DO_STMT:
1624       pp_cxx_identifier (pp, "do");
1625       pp_newline_and_indent (pp, 3);
1626       pp_cxx_statement (pp, DO_BODY (t));
1627       pp_newline_and_indent (pp, -3);
1628       pp_cxx_identifier (pp, "while");
1629       pp_space (pp);
1630       pp_cxx_left_paren (pp);
1631       pp_cxx_expression (pp, DO_COND (t));
1632       pp_cxx_right_paren (pp);
1633       pp_cxx_semicolon (pp);
1634       pp_needs_newline (pp) = true;
1635       break;
1636
1637     case FOR_STMT:
1638       pp_cxx_identifier (pp, "for");
1639       pp_space (pp);
1640       pp_cxx_left_paren (pp);
1641       if (FOR_INIT_STMT (t))
1642         pp_cxx_statement (pp, FOR_INIT_STMT (t));
1643       else
1644         pp_cxx_semicolon (pp);
1645       pp_needs_newline (pp) = false;
1646       pp_cxx_whitespace (pp);
1647       if (FOR_COND (t))
1648         pp_cxx_expression (pp, FOR_COND (t));
1649       pp_cxx_semicolon (pp);
1650       pp_needs_newline (pp) = false;
1651       pp_cxx_whitespace (pp);
1652       if (FOR_EXPR (t))
1653         pp_cxx_expression (pp, FOR_EXPR (t));
1654       pp_cxx_right_paren (pp);
1655       pp_newline_and_indent (pp, 3);
1656       pp_cxx_statement (pp, FOR_BODY (t));
1657       pp_indentation (pp) -= 3;
1658       pp_needs_newline (pp) = true;
1659       break;
1660
1661       /* jump-statement:
1662             goto identifier;
1663             continue ;
1664             return expression(opt) ;  */
1665     case BREAK_STMT:
1666     case CONTINUE_STMT:
1667       pp_identifier (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1668       pp_cxx_semicolon (pp);
1669       pp_needs_newline (pp) = true;
1670       break;
1671
1672       /* expression-statement:
1673             expression(opt) ;  */
1674     case EXPR_STMT:
1675       pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1676       pp_cxx_semicolon (pp);
1677       pp_needs_newline (pp) = true;
1678       break;
1679
1680     case CLEANUP_STMT:
1681       pp_cxx_identifier (pp, "try");
1682       pp_newline_and_indent (pp, 2);
1683       pp_cxx_statement (pp, CLEANUP_BODY (t));
1684       pp_newline_and_indent (pp, -2);
1685       pp_cxx_identifier (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1686       pp_newline_and_indent (pp, 2);
1687       pp_cxx_statement (pp, CLEANUP_EXPR (t));
1688       pp_newline_and_indent (pp, -2);
1689       break;
1690
1691     default:
1692       pp_c_statement (pp_c_base (pp), t);
1693       break;
1694     }
1695 }
1696
1697 /* original-namespace-definition:
1698       namespace identifier { namespace-body }
1699
1700   As an edge case, we also handle unnamed namespace definition here.  */
1701
1702 static void
1703 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1704 {
1705   pp_cxx_identifier (pp, "namespace");
1706   if (DECL_CONTEXT (t))
1707     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1708   if (DECL_NAME (t))
1709     pp_cxx_unqualified_id (pp, t);
1710   pp_cxx_whitespace (pp);
1711   pp_cxx_left_brace (pp);
1712   /* We do not print the namespace-body.  */
1713   pp_cxx_whitespace (pp);
1714   pp_cxx_right_brace (pp);
1715 }
1716
1717 /* namespace-alias:
1718       identifier
1719
1720    namespace-alias-definition:
1721       namespace identifier = qualified-namespace-specifier ;
1722
1723    qualified-namespace-specifier:
1724       ::(opt) nested-name-specifier(opt) namespace-name   */
1725
1726 static void
1727 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1728 {
1729   pp_cxx_identifier (pp, "namespace");
1730   if (DECL_CONTEXT (t))
1731     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1732   pp_cxx_unqualified_id (pp, t);
1733   pp_cxx_whitespace (pp);
1734   pp_equal (pp);
1735   pp_cxx_whitespace (pp);
1736   if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1737     pp_cxx_nested_name_specifier (pp, 
1738                                   DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1739   pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1740   pp_cxx_semicolon (pp);
1741 }
1742
1743 /* simple-declaration:
1744       decl-specifier-seq(opt) init-declarator-list(opt)  */
1745
1746 static void
1747 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1748 {
1749   pp_cxx_decl_specifier_seq (pp, t);
1750   pp_cxx_init_declarator (pp, t);
1751   pp_cxx_semicolon (pp);
1752   pp_needs_newline (pp) = true;
1753 }
1754
1755 /*
1756   template-parameter-list:
1757      template-parameter
1758      template-parameter-list , template-parameter  */
1759
1760 static inline void
1761 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1762 {
1763   const int n = TREE_VEC_LENGTH (t);
1764   int i;
1765   for (i = 0; i < n; ++i)
1766     {
1767       if (i)
1768         pp_cxx_separate_with (pp, ',');
1769       pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
1770     }
1771 }
1772
1773 /* template-parameter:
1774       type-parameter
1775       parameter-declaration
1776
1777    type-parameter:
1778      class identifier(opt)
1779      class identifier(op) = type-id
1780      typename identifier(opt)
1781      typename identifier(opt) = type-id
1782      template < template-parameter-list > class identifier(opt)
1783      template < template-parameter-list > class identifier(opt) = template-name
1784 */
1785
1786 static void
1787 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
1788 {
1789   tree parameter =  TREE_VALUE (t);
1790   switch (TREE_CODE (parameter))
1791     {
1792     case TYPE_DECL:
1793       pp_cxx_identifier (pp, "class");
1794       if (DECL_NAME (parameter))
1795         pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
1796       /* FIXME: Chech if we should print also default argument.  */
1797       break;
1798
1799     case PARM_DECL:
1800       pp_cxx_parameter_declaration (pp, parameter);
1801       break;
1802
1803     case TEMPLATE_DECL:
1804       break;
1805
1806     default:
1807       pp_unsupported_tree (pp, t);
1808       break;
1809     }
1810 }
1811
1812 /* Pretty-print a template parameter in the canonical form
1813    "template-parameter-<level>-<position in parameter list>".  */
1814
1815 void
1816 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
1817 {
1818   const enum tree_code code = TREE_CODE (parm);
1819
1820   /* Brings type template parameters to the canonical forms.  */
1821   if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
1822       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1823     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
1824
1825   pp_cxx_begin_template_argument_list (pp);
1826   pp_cxx_identifier (pp, "template-parameter-");
1827   pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
1828   pp_minus (pp);
1829   pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
1830   pp_cxx_end_template_argument_list (pp);
1831 }
1832
1833 /*
1834   template-declaration:
1835      export(opt) template < template-parameter-list > declaration   */
1836
1837 static void
1838 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
1839 {
1840   tree tmpl = most_general_template (t);
1841   tree level;
1842   int i = 0;
1843
1844   pp_maybe_newline_and_indent (pp, 0);
1845   for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
1846     {
1847       pp_cxx_identifier (pp, "template");
1848       pp_cxx_begin_template_argument_list (pp);
1849       pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
1850       pp_cxx_end_template_argument_list (pp);
1851       pp_newline_and_indent (pp, 3);
1852       i += 3;
1853     }
1854   if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
1855     pp_cxx_function_definition (pp, t);
1856   else
1857     pp_cxx_simple_declaration (pp, t);
1858 }
1859
1860 static void
1861 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
1862 {
1863   pp_unsupported_tree (pp, t);
1864 }
1865
1866 static void
1867 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
1868 {
1869   pp_unsupported_tree (pp, t);
1870 }
1871
1872 /*
1873     declaration:
1874        block-declaration
1875        function-definition
1876        template-declaration
1877        explicit-instantiation
1878        explicit-specialization
1879        linkage-specification
1880        namespace-definition
1881
1882     block-declaration:
1883        simple-declaration
1884        asm-definition
1885        namespace-alias-definition
1886        using-declaration
1887        using-directive  */
1888 void
1889 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
1890 {
1891   if (!DECL_LANG_SPECIFIC (t))
1892     pp_cxx_simple_declaration (pp, t);
1893   else if (DECL_USE_TEMPLATE (t))
1894     switch (DECL_USE_TEMPLATE (t))
1895       {
1896       case 1:
1897         pp_cxx_template_declaration (pp, t);
1898         break;
1899
1900       case 2:
1901         pp_cxx_explicit_specialization (pp, t);
1902         break;
1903
1904       case 3:
1905         pp_cxx_explicit_instantiation (pp, t);
1906         break;
1907
1908       default:
1909         break;
1910       }
1911   else switch (TREE_CODE (t))
1912     {
1913     case VAR_DECL:
1914     case TYPE_DECL:
1915       pp_cxx_simple_declaration (pp, t);
1916       break;
1917
1918     case FUNCTION_DECL:
1919       if (DECL_SAVED_TREE (t))
1920         pp_cxx_function_definition (pp, t);
1921       else
1922         pp_cxx_simple_declaration (pp, t);
1923       break;
1924
1925     case NAMESPACE_DECL:
1926       if (DECL_NAMESPACE_ALIAS (t))
1927         pp_cxx_namespace_alias_definition (pp, t);
1928       else
1929         pp_cxx_original_namespace_definition (pp, t);
1930       break;
1931
1932     default:
1933       pp_unsupported_tree (pp, t);
1934       break;
1935     }
1936 }
1937
1938 \f
1939 typedef c_pretty_print_fn pp_fun;
1940
1941 /* Initialization of a C++ pretty-printer object.  */
1942
1943 void
1944 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
1945 {
1946   pp_c_pretty_printer_init (pp_c_base (pp));
1947   pp_set_line_maximum_length (pp, 0);
1948
1949   pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
1950   pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
1951   pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
1952   pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
1953   pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
1954   pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
1955   pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
1956   pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
1957   pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
1958   pp->c_base.direct_abstract_declarator =
1959     (pp_fun) pp_cxx_direct_abstract_declarator;
1960   pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
1961
1962   /* pp->c_base.statement = (pp_fun) pp_cxx_statement;  */
1963
1964   pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
1965   pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
1966   pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
1967   pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
1968   pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
1969   pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
1970   pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
1971   pp->c_base.expression = (pp_fun) pp_cxx_expression;
1972   pp->enclosing_scope = global_namespace;
1973 }