OSDN Git Service

5ef84fe521796c72f1596b09563ab2db7c23a560
[pf3gnuchains/gcc-fork.git] / gcc / cp / cxx-pretty-print.c
1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2    Copyright (C) 2003, 2004, 2005, 2007, 2008,
3    2009 Free Software Foundation, Inc.
4    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "intl.h"
28 #include "cxx-pretty-print.h"
29 #include "cp-tree.h"
30 #include "toplev.h"
31
32 /* Translate if being used for diagnostics, but not for dump files or
33    __PRETTY_FUNCTION.  */
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
35
36 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
37 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
38 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
39 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree);
40 static void pp_cxx_expression (cxx_pretty_printer *, tree);
41 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
42 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
43 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
44 static void pp_cxx_type_id (cxx_pretty_printer *, tree);
45 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree);
46 static void pp_cxx_declarator (cxx_pretty_printer *, tree);
47 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
48 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree);
49 static void pp_cxx_statement (cxx_pretty_printer *, tree);
50 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
51 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
52 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
53 \f
54
55 static inline void
56 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
57 {
58   const char *p = pp_last_position_in_text (pp);
59
60   if (p != NULL && *p == c)
61     pp_cxx_whitespace (pp);
62   pp_character (pp, c);
63   pp_base (pp)->padding = pp_none;
64 }
65
66 #define pp_cxx_storage_class_specifier(PP, T) \
67    pp_c_storage_class_specifier (pp_c_base (PP), T)
68 #define pp_cxx_expression_list(PP, T)    \
69    pp_c_expression_list (pp_c_base (PP), T)
70 #define pp_cxx_space_for_pointer_operator(PP, T)  \
71    pp_c_space_for_pointer_operator (pp_c_base (PP), T)
72 #define pp_cxx_init_declarator(PP, T)    \
73    pp_c_init_declarator (pp_c_base (PP), T)
74 #define pp_cxx_call_argument_list(PP, T) \
75    pp_c_call_argument_list (pp_c_base (PP), T)
76
77 void
78 pp_cxx_colon_colon (cxx_pretty_printer *pp)
79 {
80   pp_colon_colon (pp);
81   pp_base (pp)->padding = pp_none;
82 }
83
84 void
85 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
86 {
87   pp_cxx_nonconsecutive_character (pp, '<');
88 }
89
90 void
91 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
92 {
93   pp_cxx_nonconsecutive_character (pp, '>');
94 }
95
96 void
97 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
98 {
99   pp_separate_with (pp, c);
100   pp_base (pp)->padding = pp_none;
101 }
102
103 /* Expressions.  */
104
105 static inline bool
106 is_destructor_name (tree name)
107 {
108   return name == complete_dtor_identifier
109     || name == base_dtor_identifier
110     || name == deleting_dtor_identifier;
111 }
112
113 /* conversion-function-id:
114       operator conversion-type-id
115
116    conversion-type-id:
117       type-specifier-seq conversion-declarator(opt)
118
119    conversion-declarator:
120       ptr-operator conversion-declarator(opt)  */
121
122 static inline void
123 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
124 {
125   pp_cxx_ws_string (pp, "operator");
126   pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
127 }
128
129 static inline void
130 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
131 {
132   pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
133   pp_cxx_begin_template_argument_list (pp);
134   pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
135   pp_cxx_end_template_argument_list (pp);
136 }
137
138 /* Prints the unqualified part of the id-expression T.
139
140    unqualified-id:
141      identifier
142      operator-function-id
143      conversion-function-id
144      ~ class-name
145      template-id  */
146
147 static void
148 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
149 {
150   enum tree_code code = TREE_CODE (t);
151   switch (code)
152     {
153     case RESULT_DECL:
154       pp_cxx_ws_string (pp, M_("<return-value>"));
155       break;
156
157     case OVERLOAD:
158       t = OVL_CURRENT (t);
159     case VAR_DECL:
160     case PARM_DECL:
161     case CONST_DECL:
162     case TYPE_DECL:
163     case FUNCTION_DECL:
164     case NAMESPACE_DECL:
165     case FIELD_DECL:
166     case LABEL_DECL:
167     case USING_DECL:
168     case TEMPLATE_DECL:
169       t = DECL_NAME (t);
170
171     case IDENTIFIER_NODE:
172       if (t == NULL)
173         pp_cxx_ws_string (pp, M_("<unnamed>"));
174       else if (IDENTIFIER_TYPENAME_P (t))
175         pp_cxx_conversion_function_id (pp, t);
176       else
177         {
178           if (is_destructor_name (t))
179             {
180               pp_complement (pp);
181               /* FIXME: Why is this necessary? */
182               if (TREE_TYPE (t))
183                 t = constructor_name (TREE_TYPE (t));
184             }
185           pp_cxx_tree_identifier (pp, t);
186         }
187       break;
188
189     case TEMPLATE_ID_EXPR:
190       pp_cxx_template_id (pp, t);
191       break;
192
193     case BASELINK:
194       pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
195       break;
196
197     case RECORD_TYPE:
198     case UNION_TYPE:
199     case ENUMERAL_TYPE:
200     case TYPENAME_TYPE:
201     case UNBOUND_CLASS_TEMPLATE:
202       pp_cxx_unqualified_id (pp, TYPE_NAME (t));
203       if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
204         {
205           pp_cxx_begin_template_argument_list (pp);
206           pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS
207                                                  (CLASSTYPE_TI_ARGS (t)));
208           pp_cxx_end_template_argument_list (pp);
209         }
210       break;
211
212     case BIT_NOT_EXPR:
213       pp_cxx_complement (pp);
214       pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
215       break;
216
217     case TEMPLATE_TYPE_PARM:
218     case TEMPLATE_TEMPLATE_PARM:
219       if (TYPE_IDENTIFIER (t))
220         pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
221       else
222         pp_cxx_canonical_template_parameter (pp, t);
223       break;
224
225     case TEMPLATE_PARM_INDEX:
226       pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
227       break;
228
229     case BOUND_TEMPLATE_TEMPLATE_PARM:
230       pp_cxx_cv_qualifier_seq (pp, t);
231       pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
232       pp_cxx_begin_template_argument_list (pp);
233       pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t));
234       pp_cxx_end_template_argument_list (pp);
235       break;
236
237     default:
238       pp_unsupported_tree (pp, t);
239       break;
240     }
241 }
242
243 /* Pretty-print out the token sequence ":: template" in template codes
244    where it is needed to "inline declare" the (following) member as
245    a template.  This situation arises when SCOPE of T is dependent
246    on template parameters.  */
247
248 static inline void
249 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
250 {
251   if (TREE_CODE (t) == TEMPLATE_ID_EXPR
252       && TYPE_P (scope) && dependent_type_p (scope))
253     pp_cxx_ws_string (pp, "template");
254 }
255
256 /* nested-name-specifier:
257       class-or-namespace-name :: nested-name-specifier(opt)
258       class-or-namespace-name :: template nested-name-specifier   */
259
260 static void
261 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
262 {
263   if (t != NULL && t != pp->enclosing_scope)
264     {
265       tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
266       pp_cxx_nested_name_specifier (pp, scope);
267       pp_cxx_template_keyword_if_needed (pp, scope, t);
268       pp_cxx_unqualified_id (pp, t);
269       pp_cxx_colon_colon (pp);
270     }
271 }
272
273 /* qualified-id:
274       nested-name-specifier template(opt) unqualified-id  */
275
276 static void
277 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
278 {
279   switch (TREE_CODE (t))
280     {
281       /* A pointer-to-member is always qualified.  */
282     case PTRMEM_CST:
283       pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
284       pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
285       break;
286
287       /* In Standard C++, functions cannot possibly be used as
288          nested-name-specifiers.  However, there are situations where
289          is "makes sense" to output the surrounding function name for the
290          purpose of emphasizing on the scope kind.  Just printing the
291          function name might not be sufficient as it may be overloaded; so,
292          we decorate the function with its signature too.
293          FIXME:  This is probably the wrong pretty-printing for conversion
294          functions and some function templates.  */
295     case OVERLOAD:
296       t = OVL_CURRENT (t);
297     case FUNCTION_DECL:
298       if (DECL_FUNCTION_MEMBER_P (t))
299         pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
300       pp_cxx_unqualified_id
301         (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
302       pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
303       break;
304
305     case OFFSET_REF:
306     case SCOPE_REF:
307       pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
308       pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
309       break;
310
311     default:
312       {
313         tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
314         if (scope != pp->enclosing_scope)
315           {
316             pp_cxx_nested_name_specifier (pp, scope);
317             pp_cxx_template_keyword_if_needed (pp, scope, t);
318           }
319         pp_cxx_unqualified_id (pp, t);
320       }
321       break;
322     }
323 }
324
325
326 static void
327 pp_cxx_constant (cxx_pretty_printer *pp, tree t)
328 {
329   switch (TREE_CODE (t))
330     {
331     case STRING_CST:
332       {
333         const bool in_parens = PAREN_STRING_LITERAL_P (t);
334         if (in_parens)
335           pp_cxx_left_paren (pp);
336         pp_c_constant (pp_c_base (pp), t);
337         if (in_parens)
338           pp_cxx_right_paren (pp);
339       }
340       break;
341
342     default:
343       pp_c_constant (pp_c_base (pp), t);
344       break;
345     }
346 }
347
348 /* id-expression:
349       unqualified-id
350       qualified-id   */
351
352 static inline void
353 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
354 {
355   if (TREE_CODE (t) == OVERLOAD)
356     t = OVL_CURRENT (t);
357   if (DECL_P (t) && DECL_CONTEXT (t))
358     pp_cxx_qualified_id (pp, t);
359   else
360     pp_cxx_unqualified_id (pp, t);
361 }
362
363 /* primary-expression:
364      literal
365      this
366      :: identifier
367      :: operator-function-id
368      :: qualifier-id
369      ( expression )
370      id-expression   
371
372    GNU Extensions:
373      __builtin_va_arg ( assignment-expression , type-id )
374      __builtin_offsetof ( type-id, offsetof-expression )
375
376      __has_nothrow_assign ( type-id )   
377      __has_nothrow_constructor ( type-id )
378      __has_nothrow_copy ( type-id )
379      __has_trivial_assign ( type-id )   
380      __has_trivial_constructor ( type-id )
381      __has_trivial_copy ( type-id )
382      __has_trivial_destructor ( type-id )
383      __has_virtual_destructor ( type-id )     
384      __is_abstract ( type-id )
385      __is_base_of ( type-id , type-id )
386      __is_class ( type-id )
387      __is_convertible_to ( type-id , type-id )     
388      __is_empty ( type-id )
389      __is_enum ( type-id )
390      __is_pod ( type-id )
391      __is_polymorphic ( type-id )
392      __is_union ( type-id )  */
393
394 static void
395 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
396 {
397   switch (TREE_CODE (t))
398     {
399     case INTEGER_CST:
400     case REAL_CST:
401     case COMPLEX_CST:
402     case STRING_CST:
403       pp_cxx_constant (pp, t);
404       break;
405
406     case BASELINK:
407       t = BASELINK_FUNCTIONS (t);
408     case VAR_DECL:
409     case PARM_DECL:
410     case FIELD_DECL:
411     case FUNCTION_DECL:
412     case OVERLOAD:
413     case CONST_DECL:
414     case TEMPLATE_DECL:
415       pp_cxx_id_expression (pp, t);
416       break;
417
418     case RESULT_DECL:
419     case TEMPLATE_TYPE_PARM:
420     case TEMPLATE_TEMPLATE_PARM:
421     case TEMPLATE_PARM_INDEX:
422       pp_cxx_unqualified_id (pp, t);
423       break;
424
425     case STMT_EXPR:
426       pp_cxx_left_paren (pp);
427       pp_cxx_statement (pp, STMT_EXPR_STMT (t));
428       pp_cxx_right_paren (pp);
429       break;
430
431     case TRAIT_EXPR:
432       pp_cxx_trait_expression (pp, t);
433       break;
434
435     case VA_ARG_EXPR:
436       pp_cxx_va_arg_expression (pp, t);
437       break;
438
439     case OFFSETOF_EXPR:
440       pp_cxx_offsetof_expression (pp, t);
441       break;
442
443     default:
444       pp_c_primary_expression (pp_c_base (pp), t);
445       break;
446     }
447 }
448
449 /* postfix-expression:
450      primary-expression
451      postfix-expression [ expression ]
452      postfix-expression ( expression-list(opt) )
453      simple-type-specifier ( expression-list(opt) )
454      typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
455      typename ::(opt) nested-name-specifier template(opt)
456                                        template-id ( expression-list(opt) )
457      postfix-expression . template(opt) ::(opt) id-expression
458      postfix-expression -> template(opt) ::(opt) id-expression
459      postfix-expression . pseudo-destructor-name
460      postfix-expression -> pseudo-destructor-name
461      postfix-expression ++
462      postfix-expression --
463      dynamic_cast < type-id > ( expression )
464      static_cast < type-id > ( expression )
465      reinterpret_cast < type-id > ( expression )
466      const_cast < type-id > ( expression )
467      typeid ( expression )
468      typeid ( type-id )  */
469
470 static void
471 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
472 {
473   enum tree_code code = TREE_CODE (t);
474
475   switch (code)
476     {
477     case AGGR_INIT_EXPR:
478     case CALL_EXPR:
479       {
480         tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
481                                            : CALL_EXPR_FN (t));
482         tree saved_scope = pp->enclosing_scope;
483         bool skipfirst = false;
484         tree arg;
485
486         if (TREE_CODE (fun) == ADDR_EXPR)
487           fun = TREE_OPERAND (fun, 0);
488
489         /* In templates, where there is no way to tell whether a given
490            call uses an actual member function.  So the parser builds
491            FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
492            instantiation time.  */
493         if (TREE_CODE (fun) != FUNCTION_DECL)
494           ;
495         else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
496           {
497             tree object = (code == AGGR_INIT_EXPR
498                            ? (AGGR_INIT_VIA_CTOR_P (t)
499                               ? AGGR_INIT_EXPR_SLOT (t)
500                               : AGGR_INIT_EXPR_ARG (t, 0))
501                            : CALL_EXPR_ARG (t, 0));
502
503             while (TREE_CODE (object) == NOP_EXPR)
504               object = TREE_OPERAND (object, 0);
505
506             if (TREE_CODE (object) == ADDR_EXPR)
507               object = TREE_OPERAND (object, 0);
508
509             if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
510               {
511                 pp_cxx_postfix_expression (pp, object);
512                 pp_cxx_dot (pp);
513               }
514             else
515               {
516                 pp_cxx_postfix_expression (pp, object);
517                 pp_cxx_arrow (pp);
518               }
519             skipfirst = true;
520             pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
521           }
522
523         pp_cxx_postfix_expression (pp, fun);
524         pp->enclosing_scope = saved_scope;
525         pp_cxx_left_paren (pp);
526         if (code == AGGR_INIT_EXPR)
527           {
528             aggr_init_expr_arg_iterator iter;
529             FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
530               {
531                 if (skipfirst)
532                   skipfirst = false;
533                 else
534                   {
535                     pp_cxx_expression (pp, arg);
536                     if (more_aggr_init_expr_args_p (&iter))
537                       pp_cxx_separate_with (pp, ',');
538                   }
539               }
540           }
541         else
542           {
543             call_expr_arg_iterator iter;
544             FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
545               {
546                 if (skipfirst)
547                   skipfirst = false;
548                 else
549                   {
550                     pp_cxx_expression (pp, arg);
551                     if (more_call_expr_args_p (&iter))
552                       pp_cxx_separate_with (pp, ',');
553                   }
554               }
555           }
556         pp_cxx_right_paren (pp);
557       }
558       if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
559         {
560           pp_cxx_separate_with (pp, ',');
561           pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t));
562         }
563       break;
564
565     case BASELINK:
566     case VAR_DECL:
567     case PARM_DECL:
568     case FIELD_DECL:
569     case FUNCTION_DECL:
570     case OVERLOAD:
571     case CONST_DECL:
572     case TEMPLATE_DECL:
573     case RESULT_DECL:
574       pp_cxx_primary_expression (pp, t);
575       break;
576
577     case DYNAMIC_CAST_EXPR:
578     case STATIC_CAST_EXPR:
579     case REINTERPRET_CAST_EXPR:
580     case CONST_CAST_EXPR:
581       if (code == DYNAMIC_CAST_EXPR)
582         pp_cxx_ws_string (pp, "dynamic_cast");
583       else if (code == STATIC_CAST_EXPR)
584         pp_cxx_ws_string (pp, "static_cast");
585       else if (code == REINTERPRET_CAST_EXPR)
586         pp_cxx_ws_string (pp, "reinterpret_cast");
587       else
588         pp_cxx_ws_string (pp, "const_cast");
589       pp_cxx_begin_template_argument_list (pp);
590       pp_cxx_type_id (pp, TREE_TYPE (t));
591       pp_cxx_end_template_argument_list (pp);
592       pp_left_paren (pp);
593       pp_cxx_expression (pp, TREE_OPERAND (t, 0));
594       pp_right_paren (pp);
595       break;
596
597     case EMPTY_CLASS_EXPR:
598       pp_cxx_type_id (pp, TREE_TYPE (t));
599       pp_left_paren (pp);
600       pp_right_paren (pp);
601       break;
602
603     case TYPEID_EXPR:
604       pp_cxx_typeid_expression (pp, t);
605       break;
606
607     case PSEUDO_DTOR_EXPR:
608       pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
609       pp_cxx_dot (pp);
610       pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
611       pp_cxx_colon_colon (pp);
612       pp_complement (pp);
613       pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
614       break;
615
616     case ARROW_EXPR:
617       pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
618       pp_cxx_arrow (pp);
619       break;
620
621     default:
622       pp_c_postfix_expression (pp_c_base (pp), t);
623       break;
624     }
625 }
626
627 /* new-expression:
628       ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
629       ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
630
631    new-placement:
632       ( expression-list )
633
634    new-type-id:
635       type-specifier-seq new-declarator(opt)
636
637    new-declarator:
638       ptr-operator new-declarator(opt)
639       direct-new-declarator
640
641    direct-new-declarator
642       [ expression ]
643       direct-new-declarator [ constant-expression ]
644
645    new-initializer:
646       ( expression-list(opt) )  */
647
648 static void
649 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
650 {
651   enum tree_code code = TREE_CODE (t);
652   tree type = TREE_OPERAND (t, 1);
653   tree init = TREE_OPERAND (t, 2);
654   switch (code)
655     {
656     case NEW_EXPR:
657     case VEC_NEW_EXPR:
658       if (NEW_EXPR_USE_GLOBAL (t))
659         pp_cxx_colon_colon (pp);
660       pp_cxx_ws_string (pp, "new");
661       if (TREE_OPERAND (t, 0))
662         {
663           pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
664           pp_space (pp);
665         }
666       if (TREE_CODE (type) == ARRAY_REF)
667         type = build_cplus_array_type
668           (TREE_OPERAND (type, 0),
669            build_index_type (fold_build2_loc (input_location,
670                                           MINUS_EXPR, integer_type_node,
671                                           TREE_OPERAND (type, 1),
672                                           integer_one_node)));
673       pp_cxx_type_id (pp, type);
674       if (init)
675         {
676           pp_left_paren (pp);
677           if (TREE_CODE (init) == TREE_LIST)
678             pp_c_expression_list (pp_c_base (pp), init);
679           else if (init == void_zero_node)
680             ;                   /* OK, empty initializer list.  */
681           else
682             pp_cxx_expression (pp, init);
683           pp_right_paren (pp);
684         }
685       break;
686
687     default:
688       pp_unsupported_tree (pp, t);
689     }
690 }
691
692 /* delete-expression:
693       ::(opt) delete cast-expression
694       ::(opt) delete [ ] cast-expression   */
695
696 static void
697 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
698 {
699   enum tree_code code = TREE_CODE (t);
700   switch (code)
701     {
702     case DELETE_EXPR:
703     case VEC_DELETE_EXPR:
704       if (DELETE_EXPR_USE_GLOBAL (t))
705         pp_cxx_colon_colon (pp);
706       pp_cxx_ws_string (pp, "delete");
707       pp_space (pp);
708       if (code == VEC_DELETE_EXPR
709           || DELETE_EXPR_USE_VEC (t))
710         {
711           pp_left_bracket (pp);
712           pp_right_bracket (pp);
713           pp_space (pp);
714         }
715       pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
716       break;
717
718     default:
719       pp_unsupported_tree (pp, t);
720     }
721 }
722
723 /* unary-expression:
724       postfix-expression
725       ++ cast-expression
726       -- cast-expression
727       unary-operator cast-expression
728       sizeof unary-expression
729       sizeof ( type-id )
730       sizeof ... ( identifier )
731       new-expression
732       delete-expression
733
734    unary-operator: one of
735       *   &   +   -  !
736
737    GNU extensions:
738       __alignof__ unary-expression
739       __alignof__ ( type-id )  */
740
741 static void
742 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
743 {
744   enum tree_code code = TREE_CODE (t);
745   switch (code)
746     {
747     case NEW_EXPR:
748     case VEC_NEW_EXPR:
749       pp_cxx_new_expression (pp, t);
750       break;
751
752     case DELETE_EXPR:
753     case VEC_DELETE_EXPR:
754       pp_cxx_delete_expression (pp, t);
755       break;
756
757     case SIZEOF_EXPR:
758       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
759         {
760           pp_cxx_ws_string (pp, "sizeof");
761           pp_cxx_ws_string (pp, "...");
762           pp_cxx_whitespace (pp);
763           pp_cxx_left_paren (pp);
764           if (TYPE_P (TREE_OPERAND (t, 0)))
765             pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
766           else
767             pp_unary_expression (pp, TREE_OPERAND (t, 0));
768           pp_cxx_right_paren (pp);
769           break;
770         }
771       /* Fall through  */
772
773     case ALIGNOF_EXPR:
774       pp_cxx_ws_string (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
775       pp_cxx_whitespace (pp);
776       if (TYPE_P (TREE_OPERAND (t, 0)))
777         {
778           pp_cxx_left_paren (pp);
779           pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
780           pp_cxx_right_paren (pp);
781         }
782       else
783         pp_unary_expression (pp, TREE_OPERAND (t, 0));
784       break;
785
786     case UNARY_PLUS_EXPR:
787       pp_plus (pp);
788       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
789       break;
790
791     default:
792       pp_c_unary_expression (pp_c_base (pp), t);
793       break;
794     }
795 }
796
797 /* cast-expression:
798       unary-expression
799       ( type-id ) cast-expression  */
800
801 static void
802 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
803 {
804   switch (TREE_CODE (t))
805     {
806     case CAST_EXPR:
807       pp_cxx_type_id (pp, TREE_TYPE (t));
808       pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
809       break;
810
811     default:
812       pp_c_cast_expression (pp_c_base (pp), t);
813       break;
814     }
815 }
816
817 /* pm-expression:
818       cast-expression
819       pm-expression .* cast-expression
820       pm-expression ->* cast-expression  */
821
822 static void
823 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
824 {
825   switch (TREE_CODE (t))
826     {
827       /* Handle unfortunate OFFSET_REF overloading here.  */
828     case OFFSET_REF:
829       if (TYPE_P (TREE_OPERAND (t, 0)))
830         {
831           pp_cxx_qualified_id (pp, t);
832           break;
833         }
834       /* Else fall through.  */
835     case MEMBER_REF:
836     case DOTSTAR_EXPR:
837       pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
838       if (TREE_CODE (t) == MEMBER_REF)
839         pp_cxx_arrow (pp);
840       else
841         pp_cxx_dot (pp);
842       pp_star(pp);
843       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
844       break;
845
846
847     default:
848       pp_cxx_cast_expression (pp, t);
849       break;
850     }
851 }
852
853 /* multiplicative-expression:
854       pm-expression
855       multiplicative-expression * pm-expression
856       multiplicative-expression / pm-expression
857       multiplicative-expression % pm-expression  */
858
859 static void
860 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
861 {
862   enum tree_code code = TREE_CODE (e);
863   switch (code)
864     {
865     case MULT_EXPR:
866     case TRUNC_DIV_EXPR:
867     case TRUNC_MOD_EXPR:
868       pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
869       pp_space (pp);
870       if (code == MULT_EXPR)
871         pp_star (pp);
872       else if (code == TRUNC_DIV_EXPR)
873         pp_slash (pp);
874       else
875         pp_modulo (pp);
876       pp_space (pp);
877       pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
878       break;
879
880     default:
881       pp_cxx_pm_expression (pp, e);
882       break;
883     }
884 }
885
886 /* conditional-expression:
887       logical-or-expression
888       logical-or-expression ?  expression  : assignment-expression  */
889
890 static void
891 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
892 {
893   if (TREE_CODE (e) == COND_EXPR)
894     {
895       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
896       pp_space (pp);
897       pp_question (pp);
898       pp_space (pp);
899       pp_cxx_expression (pp, TREE_OPERAND (e, 1));
900       pp_space (pp);
901       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
902     }
903   else
904     pp_c_logical_or_expression (pp_c_base (pp), e);
905 }
906
907 /* Pretty-print a compound assignment operator token as indicated by T.  */
908
909 static void
910 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
911 {
912   const char *op;
913
914   switch (TREE_CODE (t))
915     {
916     case NOP_EXPR:
917       op = "=";
918       break;
919
920     case PLUS_EXPR:
921       op = "+=";
922       break;
923
924     case MINUS_EXPR:
925       op = "-=";
926       break;
927
928     case TRUNC_DIV_EXPR:
929       op = "/=";
930       break;
931
932     case TRUNC_MOD_EXPR:
933       op = "%=";
934       break;
935
936     default:
937       op = tree_code_name[TREE_CODE (t)];
938       break;
939     }
940
941   pp_cxx_ws_string (pp, op);
942 }
943
944
945 /* assignment-expression:
946       conditional-expression
947       logical-or-expression assignment-operator assignment-expression
948       throw-expression
949
950    throw-expression:
951        throw assignment-expression(opt)
952
953    assignment-operator: one of
954       =    *=    /=    %=    +=    -=    >>=    <<=    &=    ^=    |=  */
955
956 static void
957 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
958 {
959   switch (TREE_CODE (e))
960     {
961     case MODIFY_EXPR:
962     case INIT_EXPR:
963       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
964       pp_space (pp);
965       pp_equal (pp);
966       pp_space (pp);
967       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
968       break;
969
970     case THROW_EXPR:
971       pp_cxx_ws_string (pp, "throw");
972       if (TREE_OPERAND (e, 0))
973         pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
974       break;
975
976     case MODOP_EXPR:
977       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
978       pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
979       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
980       break;
981
982     default:
983       pp_cxx_conditional_expression (pp, e);
984       break;
985     }
986 }
987
988 static void
989 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
990 {
991   switch (TREE_CODE (t))
992     {
993     case STRING_CST:
994     case INTEGER_CST:
995     case REAL_CST:
996     case COMPLEX_CST:
997       pp_cxx_constant (pp, t);
998       break;
999
1000     case RESULT_DECL:
1001       pp_cxx_unqualified_id (pp, t);
1002       break;
1003
1004 #if 0
1005     case OFFSET_REF:
1006 #endif
1007     case SCOPE_REF:
1008     case PTRMEM_CST:
1009       pp_cxx_qualified_id (pp, t);
1010       break;
1011
1012     case OVERLOAD:
1013       t = OVL_CURRENT (t);
1014     case VAR_DECL:
1015     case PARM_DECL:
1016     case FIELD_DECL:
1017     case CONST_DECL:
1018     case FUNCTION_DECL:
1019     case BASELINK:
1020     case TEMPLATE_DECL:
1021     case TEMPLATE_TYPE_PARM:
1022     case TEMPLATE_PARM_INDEX:
1023     case TEMPLATE_TEMPLATE_PARM:
1024     case STMT_EXPR:
1025       pp_cxx_primary_expression (pp, t);
1026       break;
1027
1028     case CALL_EXPR:
1029     case DYNAMIC_CAST_EXPR:
1030     case STATIC_CAST_EXPR:
1031     case REINTERPRET_CAST_EXPR:
1032     case CONST_CAST_EXPR:
1033 #if 0
1034     case MEMBER_REF:
1035 #endif
1036     case EMPTY_CLASS_EXPR:
1037     case TYPEID_EXPR:
1038     case PSEUDO_DTOR_EXPR:
1039     case AGGR_INIT_EXPR:
1040     case ARROW_EXPR:
1041       pp_cxx_postfix_expression (pp, t);
1042       break;
1043
1044     case NEW_EXPR:
1045     case VEC_NEW_EXPR:
1046       pp_cxx_new_expression (pp, t);
1047       break;
1048
1049     case DELETE_EXPR:
1050     case VEC_DELETE_EXPR:
1051       pp_cxx_delete_expression (pp, t);
1052       break;
1053
1054     case SIZEOF_EXPR:
1055     case ALIGNOF_EXPR:
1056       pp_cxx_unary_expression (pp, t);
1057       break;
1058
1059     case CAST_EXPR:
1060       pp_cxx_cast_expression (pp, t);
1061       break;
1062
1063     case OFFSET_REF:
1064     case MEMBER_REF:
1065     case DOTSTAR_EXPR:
1066       pp_cxx_pm_expression (pp, t);
1067       break;
1068
1069     case MULT_EXPR:
1070     case TRUNC_DIV_EXPR:
1071     case TRUNC_MOD_EXPR:
1072       pp_cxx_multiplicative_expression (pp, t);
1073       break;
1074
1075     case COND_EXPR:
1076       pp_cxx_conditional_expression (pp, t);
1077       break;
1078
1079     case MODIFY_EXPR:
1080     case INIT_EXPR:
1081     case THROW_EXPR:
1082     case MODOP_EXPR:
1083       pp_cxx_assignment_expression (pp, t);
1084       break;
1085
1086     case NON_DEPENDENT_EXPR:
1087     case MUST_NOT_THROW_EXPR:
1088       pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1089       break;
1090
1091     case EXPR_PACK_EXPANSION:
1092       pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t));
1093       pp_cxx_ws_string (pp, "...");
1094       break;
1095
1096     case TEMPLATE_ID_EXPR:
1097       pp_cxx_template_id (pp, t);
1098       break;
1099
1100     case NONTYPE_ARGUMENT_PACK:
1101       {
1102         tree args = ARGUMENT_PACK_ARGS (t);
1103         int i, len = TREE_VEC_LENGTH (args);
1104         for (i = 0; i < len; ++i)
1105           {
1106             if (i > 0)
1107               pp_cxx_separate_with (pp, ',');
1108             pp_cxx_expression (pp, TREE_VEC_ELT (args, i));
1109           }
1110       }
1111       break;
1112
1113     default:
1114       pp_c_expression (pp_c_base (pp), t);
1115       break;
1116     }
1117 }
1118
1119
1120 /* Declarations.  */
1121
1122 /* function-specifier:
1123       inline
1124       virtual
1125       explicit   */
1126
1127 static void
1128 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1129 {
1130   switch (TREE_CODE (t))
1131     {
1132     case FUNCTION_DECL:
1133       if (DECL_VIRTUAL_P (t))
1134         pp_cxx_ws_string (pp, "virtual");
1135       else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1136         pp_cxx_ws_string (pp, "explicit");
1137       else
1138         pp_c_function_specifier (pp_c_base (pp), t);
1139
1140     default:
1141       break;
1142     }
1143 }
1144
1145 /* decl-specifier-seq:
1146       decl-specifier-seq(opt) decl-specifier
1147
1148    decl-specifier:
1149       storage-class-specifier
1150       type-specifier
1151       function-specifier
1152       friend
1153       typedef  */
1154
1155 static void
1156 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1157 {
1158   switch (TREE_CODE (t))
1159     {
1160     case VAR_DECL:
1161     case PARM_DECL:
1162     case CONST_DECL:
1163     case FIELD_DECL:
1164       pp_cxx_storage_class_specifier (pp, t);
1165       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1166       break;
1167
1168     case TYPE_DECL:
1169       pp_cxx_ws_string (pp, "typedef");
1170       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1171       break;
1172
1173     case RECORD_TYPE:
1174       if (TYPE_PTRMEMFUNC_P (t))
1175         {
1176           tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1177           pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1178           pp_cxx_whitespace (pp);
1179           pp_cxx_ptr_operator (pp, t);
1180         }
1181       break;
1182
1183     case FUNCTION_DECL:
1184       /* Constructors don't have return types.  And conversion functions
1185          do not have a type-specifier in their return types.  */
1186       if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1187         pp_cxx_function_specifier (pp, t);
1188       else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1189         pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1190       else
1191         default:
1192       pp_c_declaration_specifiers (pp_c_base (pp), t);
1193       break;
1194     }
1195 }
1196
1197 /* simple-type-specifier:
1198       ::(opt) nested-name-specifier(opt) type-name
1199       ::(opt) nested-name-specifier(opt) template(opt) template-id
1200       char
1201       wchar_t
1202       bool
1203       short
1204       int
1205       long
1206       signed
1207       unsigned
1208       float
1209       double
1210       void  */
1211
1212 static void
1213 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1214 {
1215   switch (TREE_CODE (t))
1216     {
1217     case RECORD_TYPE:
1218     case UNION_TYPE:
1219     case ENUMERAL_TYPE:
1220       pp_cxx_qualified_id (pp, t);
1221       break;
1222
1223     case TEMPLATE_TYPE_PARM:
1224     case TEMPLATE_TEMPLATE_PARM:
1225     case TEMPLATE_PARM_INDEX:
1226       pp_cxx_unqualified_id (pp, t);
1227       break;
1228
1229     case TYPENAME_TYPE:
1230       pp_cxx_ws_string (pp, "typename");
1231       pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1232       pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1233       break;
1234
1235     default:
1236       pp_c_type_specifier (pp_c_base (pp), t);
1237       break;
1238     }
1239 }
1240
1241 /* type-specifier-seq:
1242       type-specifier type-specifier-seq(opt)
1243
1244    type-specifier:
1245       simple-type-specifier
1246       class-specifier
1247       enum-specifier
1248       elaborated-type-specifier
1249       cv-qualifier   */
1250
1251 static void
1252 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1253 {
1254   switch (TREE_CODE (t))
1255     {
1256     case TEMPLATE_DECL:
1257     case TEMPLATE_TYPE_PARM:
1258     case TEMPLATE_TEMPLATE_PARM:
1259     case TYPE_DECL:
1260     case BOUND_TEMPLATE_TEMPLATE_PARM:
1261       pp_cxx_cv_qualifier_seq (pp, t);
1262       pp_cxx_simple_type_specifier (pp, t);
1263       break;
1264
1265     case METHOD_TYPE:
1266       pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1267       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1268       pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1269       break;
1270
1271     case DECLTYPE_TYPE:
1272       pp_cxx_ws_string (pp, "decltype");
1273       pp_cxx_left_paren (pp);
1274       pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t));
1275       pp_cxx_right_paren (pp);
1276       break;
1277
1278     default:
1279       if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1280         pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1281     }
1282 }
1283
1284 /* ptr-operator:
1285       * cv-qualifier-seq(opt)
1286       &
1287       ::(opt) nested-name-specifier * cv-qualifier-seq(opt)  */
1288
1289 static void
1290 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1291 {
1292   if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1293     t = TREE_TYPE (t);
1294   switch (TREE_CODE (t))
1295     {
1296     case REFERENCE_TYPE:
1297     case POINTER_TYPE:
1298       if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1299           || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1300         pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1301       if (TREE_CODE (t) == POINTER_TYPE)
1302         {
1303           pp_star (pp);
1304           pp_cxx_cv_qualifier_seq (pp, t);
1305         }
1306       else
1307         pp_ampersand (pp);
1308       break;
1309
1310     case RECORD_TYPE:
1311       if (TYPE_PTRMEMFUNC_P (t))
1312         {
1313           pp_cxx_left_paren (pp);
1314           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1315           pp_star (pp);
1316           break;
1317         }
1318     case OFFSET_TYPE:
1319       if (TYPE_PTR_TO_MEMBER_P (t))
1320         {
1321           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1322             pp_cxx_left_paren (pp);
1323           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1324           pp_star (pp);
1325           pp_cxx_cv_qualifier_seq (pp, t);
1326           break;
1327         }
1328       /* else fall through.  */
1329
1330     default:
1331       pp_unsupported_tree (pp, t);
1332       break;
1333     }
1334 }
1335
1336 static inline tree
1337 pp_cxx_implicit_parameter_type (tree mf)
1338 {
1339   return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1340 }
1341
1342 /*
1343    parameter-declaration:
1344       decl-specifier-seq declarator
1345       decl-specifier-seq declarator = assignment-expression
1346       decl-specifier-seq abstract-declarator(opt)
1347       decl-specifier-seq abstract-declarator(opt) assignment-expression  */
1348
1349 static inline void
1350 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1351 {
1352   pp_cxx_decl_specifier_seq (pp, t);
1353   if (TYPE_P (t))
1354     pp_cxx_abstract_declarator (pp, t);
1355   else
1356     pp_cxx_declarator (pp, t);
1357 }
1358
1359 /* parameter-declaration-clause:
1360       parameter-declaration-list(opt) ...(opt)
1361       parameter-declaration-list , ...
1362
1363    parameter-declaration-list:
1364       parameter-declaration
1365       parameter-declaration-list , parameter-declaration  */
1366
1367 static void
1368 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1369 {
1370   tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1371   tree types =
1372     TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1373   const bool abstract = args == NULL
1374     || pp_c_base (pp)->flags & pp_c_flag_abstract;
1375   bool first = true;
1376
1377   /* Skip artificial parameter for nonstatic member functions.  */
1378   if (TREE_CODE (t) == METHOD_TYPE)
1379     types = TREE_CHAIN (types);
1380
1381   pp_cxx_left_paren (pp);
1382   for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1383     {
1384       if (!first)
1385         pp_cxx_separate_with (pp, ',');
1386       first = false;
1387       pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1388       if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1389         {
1390           pp_cxx_whitespace (pp);
1391           pp_equal (pp);
1392           pp_cxx_whitespace (pp);
1393           pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1394         }
1395     }
1396   pp_cxx_right_paren (pp);
1397 }
1398
1399 /* exception-specification:
1400       throw ( type-id-list(opt) )
1401
1402    type-id-list
1403       type-id
1404       type-id-list , type-id   */
1405
1406 static void
1407 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1408 {
1409   tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1410   bool need_comma = false;
1411
1412   if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1413     return;
1414   pp_cxx_ws_string (pp, "throw");
1415   pp_cxx_left_paren (pp);
1416   for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1417     {
1418       tree type = TREE_VALUE (ex_spec);
1419       tree argpack = NULL_TREE;
1420       int i, len = 1;
1421
1422       if (ARGUMENT_PACK_P (type))
1423         {
1424           argpack = ARGUMENT_PACK_ARGS (type);
1425           len = TREE_VEC_LENGTH (argpack);
1426         }
1427
1428       for (i = 0; i < len; ++i)
1429         {
1430           if (argpack)
1431             type = TREE_VEC_ELT (argpack, i);
1432
1433           if (need_comma)
1434             pp_cxx_separate_with (pp, ',');
1435           else
1436             need_comma = true;
1437
1438           pp_cxx_type_id (pp, type);
1439         }
1440     }
1441   pp_cxx_right_paren (pp);
1442 }
1443
1444 /* direct-declarator:
1445       declarator-id
1446       direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1447                                             exception-specification(opt)
1448       direct-declaration [ constant-expression(opt) ]
1449       ( declarator )  */
1450
1451 static void
1452 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1453 {
1454   switch (TREE_CODE (t))
1455     {
1456     case VAR_DECL:
1457     case PARM_DECL:
1458     case CONST_DECL:
1459     case FIELD_DECL:
1460       if (DECL_NAME (t))
1461         {
1462           pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1463
1464           if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t))
1465               || template_parameter_pack_p (t))
1466             /* A function parameter pack or non-type template
1467                parameter pack.  */
1468             pp_cxx_ws_string (pp, "...");
1469                       
1470           pp_cxx_id_expression (pp, DECL_NAME (t));
1471         }
1472       pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1473       break;
1474
1475     case FUNCTION_DECL:
1476       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1477       pp_cxx_id_expression (pp, t);
1478       pp_cxx_parameter_declaration_clause (pp, t);
1479
1480       if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1481         {
1482           pp_base (pp)->padding = pp_before;
1483           pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1484         }
1485
1486       pp_cxx_exception_specification (pp, TREE_TYPE (t));
1487       break;
1488
1489     case TYPENAME_TYPE:
1490     case TEMPLATE_DECL:
1491     case TEMPLATE_TYPE_PARM:
1492     case TEMPLATE_PARM_INDEX:
1493     case TEMPLATE_TEMPLATE_PARM:
1494       break;
1495
1496     default:
1497       pp_c_direct_declarator (pp_c_base (pp), t);
1498       break;
1499     }
1500 }
1501
1502 /* declarator:
1503    direct-declarator
1504    ptr-operator declarator  */
1505
1506 static void
1507 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1508 {
1509   pp_cxx_direct_declarator (pp, t);
1510 }
1511
1512 /* ctor-initializer:
1513       : mem-initializer-list
1514
1515    mem-initializer-list:
1516       mem-initializer
1517       mem-initializer , mem-initializer-list
1518
1519    mem-initializer:
1520       mem-initializer-id ( expression-list(opt) )
1521
1522    mem-initializer-id:
1523       ::(opt) nested-name-specifier(opt) class-name
1524       identifier   */
1525
1526 static void
1527 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1528 {
1529   t = TREE_OPERAND (t, 0);
1530   pp_cxx_whitespace (pp);
1531   pp_colon (pp);
1532   pp_cxx_whitespace (pp);
1533   for (; t; t = TREE_CHAIN (t))
1534     {
1535       tree purpose = TREE_PURPOSE (t);
1536       bool is_pack = PACK_EXPANSION_P (purpose);
1537
1538       if (is_pack)
1539         pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose));
1540       else
1541         pp_cxx_primary_expression (pp, purpose);
1542       pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1543       if (is_pack)
1544         pp_cxx_ws_string (pp, "...");
1545       if (TREE_CHAIN (t))
1546         pp_cxx_separate_with (pp, ',');
1547     }
1548 }
1549
1550 /* function-definition:
1551       decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1552       decl-specifier-seq(opt) declarator function-try-block  */
1553
1554 static void
1555 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1556 {
1557   tree saved_scope = pp->enclosing_scope;
1558   pp_cxx_decl_specifier_seq (pp, t);
1559   pp_cxx_declarator (pp, t);
1560   pp_needs_newline (pp) = true;
1561   pp->enclosing_scope = DECL_CONTEXT (t);
1562   if (DECL_SAVED_TREE (t))
1563     pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1564   else
1565     {
1566       pp_cxx_semicolon (pp);
1567       pp_needs_newline (pp) = true;
1568     }
1569   pp_flush (pp);
1570   pp->enclosing_scope = saved_scope;
1571 }
1572
1573 /* abstract-declarator:
1574       ptr-operator abstract-declarator(opt)
1575       direct-abstract-declarator  */
1576
1577 static void
1578 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1579 {
1580   if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1581     pp_cxx_right_paren (pp);
1582   else if (POINTER_TYPE_P (t))
1583     {
1584       if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1585           || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1586         pp_cxx_right_paren (pp);
1587       t = TREE_TYPE (t);
1588     }
1589   pp_cxx_direct_abstract_declarator (pp, t);
1590 }
1591
1592 /* direct-abstract-declarator:
1593       direct-abstract-declarator(opt) ( parameter-declaration-clause )
1594                            cv-qualifier-seq(opt) exception-specification(opt)
1595       direct-abstract-declarator(opt) [ constant-expression(opt) ]
1596       ( abstract-declarator )  */
1597
1598 static void
1599 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1600 {
1601   switch (TREE_CODE (t))
1602     {
1603     case REFERENCE_TYPE:
1604       pp_cxx_abstract_declarator (pp, t);
1605       break;
1606
1607     case RECORD_TYPE:
1608       if (TYPE_PTRMEMFUNC_P (t))
1609         pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1610       break;
1611
1612     case METHOD_TYPE:
1613     case FUNCTION_TYPE:
1614       pp_cxx_parameter_declaration_clause (pp, t);
1615       pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1616       if (TREE_CODE (t) == METHOD_TYPE)
1617         {
1618           pp_base (pp)->padding = pp_before;
1619           pp_cxx_cv_qualifier_seq
1620             (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1621         }
1622       pp_cxx_exception_specification (pp, t);
1623       break;
1624
1625     case TYPENAME_TYPE:
1626     case TEMPLATE_TYPE_PARM:
1627     case TEMPLATE_TEMPLATE_PARM:
1628     case BOUND_TEMPLATE_TEMPLATE_PARM:
1629     case UNBOUND_CLASS_TEMPLATE:
1630       break;
1631
1632     default:
1633       pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1634       break;
1635     }
1636 }
1637
1638 /* type-id:
1639      type-specifier-seq abstract-declarator(opt) */
1640
1641 static void
1642 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1643 {
1644   pp_flags saved_flags = pp_c_base (pp)->flags;
1645   pp_c_base (pp)->flags |= pp_c_flag_abstract;
1646
1647   switch (TREE_CODE (t))
1648     {
1649     case TYPE_DECL:
1650     case UNION_TYPE:
1651     case RECORD_TYPE:
1652     case ENUMERAL_TYPE:
1653     case TYPENAME_TYPE:
1654     case BOUND_TEMPLATE_TEMPLATE_PARM:
1655     case UNBOUND_CLASS_TEMPLATE:
1656     case TEMPLATE_TEMPLATE_PARM:
1657     case TEMPLATE_TYPE_PARM:
1658     case TEMPLATE_PARM_INDEX:
1659     case TEMPLATE_DECL:
1660     case TYPEOF_TYPE:
1661     case DECLTYPE_TYPE:
1662     case TEMPLATE_ID_EXPR:
1663       pp_cxx_type_specifier_seq (pp, t);
1664       break;
1665
1666     case TYPE_PACK_EXPANSION:
1667       pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t));
1668       pp_cxx_ws_string (pp, "...");
1669       break;
1670
1671     default:
1672       pp_c_type_id (pp_c_base (pp), t);
1673       break;
1674     }
1675
1676   pp_c_base (pp)->flags = saved_flags;
1677 }
1678
1679 /* template-argument-list:
1680       template-argument ...(opt)
1681       template-argument-list, template-argument ...(opt)
1682
1683    template-argument:
1684       assignment-expression
1685       type-id
1686       template-name  */
1687
1688 static void
1689 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1690 {
1691   int i;
1692   bool need_comma = false;
1693
1694   if (t == NULL)
1695     return;
1696   for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1697     {
1698       tree arg = TREE_VEC_ELT (t, i);
1699       tree argpack = NULL_TREE;
1700       int idx, len = 1;
1701
1702       if (ARGUMENT_PACK_P (arg))
1703         {
1704           argpack = ARGUMENT_PACK_ARGS (arg);
1705           len = TREE_VEC_LENGTH (argpack);
1706         }
1707
1708       for (idx = 0; idx < len; idx++)
1709         {
1710           if (argpack)
1711             arg = TREE_VEC_ELT (argpack, idx);
1712           
1713           if (need_comma)
1714             pp_cxx_separate_with (pp, ',');
1715           else
1716             need_comma = true;
1717
1718           if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1719                                && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1720             pp_cxx_type_id (pp, arg);
1721           else
1722             pp_cxx_expression (pp, arg);
1723         }
1724     }
1725 }
1726
1727
1728 static void
1729 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1730 {
1731   t = DECL_EXPR_DECL (t);
1732   pp_cxx_type_specifier_seq (pp, t);
1733   if (TYPE_P (t))
1734     pp_cxx_abstract_declarator (pp, t);
1735   else
1736     pp_cxx_declarator (pp, t);
1737 }
1738
1739 /* Statements.  */
1740
1741 static void
1742 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1743 {
1744   switch (TREE_CODE (t))
1745     {
1746     case CTOR_INITIALIZER:
1747       pp_cxx_ctor_initializer (pp, t);
1748       break;
1749
1750     case USING_STMT:
1751       pp_cxx_ws_string (pp, "using");
1752       pp_cxx_ws_string (pp, "namespace");
1753       if (DECL_CONTEXT (t))
1754         pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1755       pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1756       break;
1757
1758     case USING_DECL:
1759       pp_cxx_ws_string (pp, "using");
1760       pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1761       pp_cxx_unqualified_id (pp, DECL_NAME (t));
1762       break;
1763
1764     case EH_SPEC_BLOCK:
1765       break;
1766
1767       /* try-block:
1768             try compound-statement handler-seq  */
1769     case TRY_BLOCK:
1770       pp_maybe_newline_and_indent (pp, 0);
1771       pp_cxx_ws_string (pp, "try");
1772       pp_newline_and_indent (pp, 3);
1773       pp_cxx_statement (pp, TRY_STMTS (t));
1774       pp_newline_and_indent (pp, -3);
1775       if (CLEANUP_P (t))
1776         ;
1777       else
1778         pp_cxx_statement (pp, TRY_HANDLERS (t));
1779       break;
1780
1781       /*
1782          handler-seq:
1783             handler handler-seq(opt)
1784
1785          handler:
1786          catch ( exception-declaration ) compound-statement
1787
1788          exception-declaration:
1789             type-specifier-seq declarator
1790             type-specifier-seq abstract-declarator
1791             ...   */
1792     case HANDLER:
1793       pp_cxx_ws_string (pp, "catch");
1794       pp_cxx_left_paren (pp);
1795       pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1796       pp_cxx_right_paren (pp);
1797       pp_indentation (pp) += 3;
1798       pp_needs_newline (pp) = true;
1799       pp_cxx_statement (pp, HANDLER_BODY (t));
1800       pp_indentation (pp) -= 3;
1801       pp_needs_newline (pp) = true;
1802       break;
1803
1804       /* selection-statement:
1805             if ( expression ) statement
1806             if ( expression ) statement else statement  */
1807     case IF_STMT:
1808       pp_cxx_ws_string (pp, "if");
1809       pp_cxx_whitespace (pp);
1810       pp_cxx_left_paren (pp);
1811       pp_cxx_expression (pp, IF_COND (t));
1812       pp_cxx_right_paren (pp);
1813       pp_newline_and_indent (pp, 2);
1814       pp_cxx_statement (pp, THEN_CLAUSE (t));
1815       pp_newline_and_indent (pp, -2);
1816       if (ELSE_CLAUSE (t))
1817         {
1818           tree else_clause = ELSE_CLAUSE (t);
1819           pp_cxx_ws_string (pp, "else");
1820           if (TREE_CODE (else_clause) == IF_STMT)
1821             pp_cxx_whitespace (pp);
1822           else
1823             pp_newline_and_indent (pp, 2);
1824           pp_cxx_statement (pp, else_clause);
1825           if (TREE_CODE (else_clause) != IF_STMT)
1826             pp_newline_and_indent (pp, -2);
1827         }
1828       break;
1829
1830     case SWITCH_STMT:
1831       pp_cxx_ws_string (pp, "switch");
1832       pp_space (pp);
1833       pp_cxx_left_paren (pp);
1834       pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1835       pp_cxx_right_paren (pp);
1836       pp_indentation (pp) += 3;
1837       pp_needs_newline (pp) = true;
1838       pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1839       pp_newline_and_indent (pp, -3);
1840       break;
1841
1842       /* iteration-statement:
1843             while ( expression ) statement
1844             do statement while ( expression ) ;
1845             for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1846             for ( declaration expression(opt) ; expression(opt) ) statement  */
1847     case WHILE_STMT:
1848       pp_cxx_ws_string (pp, "while");
1849       pp_space (pp);
1850       pp_cxx_left_paren (pp);
1851       pp_cxx_expression (pp, WHILE_COND (t));
1852       pp_cxx_right_paren (pp);
1853       pp_newline_and_indent (pp, 3);
1854       pp_cxx_statement (pp, WHILE_BODY (t));
1855       pp_indentation (pp) -= 3;
1856       pp_needs_newline (pp) = true;
1857       break;
1858
1859     case DO_STMT:
1860       pp_cxx_ws_string (pp, "do");
1861       pp_newline_and_indent (pp, 3);
1862       pp_cxx_statement (pp, DO_BODY (t));
1863       pp_newline_and_indent (pp, -3);
1864       pp_cxx_ws_string (pp, "while");
1865       pp_space (pp);
1866       pp_cxx_left_paren (pp);
1867       pp_cxx_expression (pp, DO_COND (t));
1868       pp_cxx_right_paren (pp);
1869       pp_cxx_semicolon (pp);
1870       pp_needs_newline (pp) = true;
1871       break;
1872
1873     case FOR_STMT:
1874       pp_cxx_ws_string (pp, "for");
1875       pp_space (pp);
1876       pp_cxx_left_paren (pp);
1877       if (FOR_INIT_STMT (t))
1878         pp_cxx_statement (pp, FOR_INIT_STMT (t));
1879       else
1880         pp_cxx_semicolon (pp);
1881       pp_needs_newline (pp) = false;
1882       pp_cxx_whitespace (pp);
1883       if (FOR_COND (t))
1884         pp_cxx_expression (pp, FOR_COND (t));
1885       pp_cxx_semicolon (pp);
1886       pp_needs_newline (pp) = false;
1887       pp_cxx_whitespace (pp);
1888       if (FOR_EXPR (t))
1889         pp_cxx_expression (pp, FOR_EXPR (t));
1890       pp_cxx_right_paren (pp);
1891       pp_newline_and_indent (pp, 3);
1892       pp_cxx_statement (pp, FOR_BODY (t));
1893       pp_indentation (pp) -= 3;
1894       pp_needs_newline (pp) = true;
1895       break;
1896
1897       /* jump-statement:
1898             goto identifier;
1899             continue ;
1900             return expression(opt) ;  */
1901     case BREAK_STMT:
1902     case CONTINUE_STMT:
1903       pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1904       pp_cxx_semicolon (pp);
1905       pp_needs_newline (pp) = true;
1906       break;
1907
1908       /* expression-statement:
1909             expression(opt) ;  */
1910     case EXPR_STMT:
1911       pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1912       pp_cxx_semicolon (pp);
1913       pp_needs_newline (pp) = true;
1914       break;
1915
1916     case CLEANUP_STMT:
1917       pp_cxx_ws_string (pp, "try");
1918       pp_newline_and_indent (pp, 2);
1919       pp_cxx_statement (pp, CLEANUP_BODY (t));
1920       pp_newline_and_indent (pp, -2);
1921       pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1922       pp_newline_and_indent (pp, 2);
1923       pp_cxx_statement (pp, CLEANUP_EXPR (t));
1924       pp_newline_and_indent (pp, -2);
1925       break;
1926
1927     case STATIC_ASSERT:
1928       pp_cxx_declaration (pp, t);
1929       break;
1930
1931     default:
1932       pp_c_statement (pp_c_base (pp), t);
1933       break;
1934     }
1935 }
1936
1937 /* original-namespace-definition:
1938       namespace identifier { namespace-body }
1939
1940   As an edge case, we also handle unnamed namespace definition here.  */
1941
1942 static void
1943 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1944 {
1945   pp_cxx_ws_string (pp, "namespace");
1946   if (DECL_CONTEXT (t))
1947     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1948   if (DECL_NAME (t))
1949     pp_cxx_unqualified_id (pp, t);
1950   pp_cxx_whitespace (pp);
1951   pp_cxx_left_brace (pp);
1952   /* We do not print the namespace-body.  */
1953   pp_cxx_whitespace (pp);
1954   pp_cxx_right_brace (pp);
1955 }
1956
1957 /* namespace-alias:
1958       identifier
1959
1960    namespace-alias-definition:
1961       namespace identifier = qualified-namespace-specifier ;
1962
1963    qualified-namespace-specifier:
1964       ::(opt) nested-name-specifier(opt) namespace-name   */
1965
1966 static void
1967 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1968 {
1969   pp_cxx_ws_string (pp, "namespace");
1970   if (DECL_CONTEXT (t))
1971     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1972   pp_cxx_unqualified_id (pp, t);
1973   pp_cxx_whitespace (pp);
1974   pp_equal (pp);
1975   pp_cxx_whitespace (pp);
1976   if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1977     pp_cxx_nested_name_specifier (pp,
1978                                   DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1979   pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1980   pp_cxx_semicolon (pp);
1981 }
1982
1983 /* simple-declaration:
1984       decl-specifier-seq(opt) init-declarator-list(opt)  */
1985
1986 static void
1987 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1988 {
1989   pp_cxx_decl_specifier_seq (pp, t);
1990   pp_cxx_init_declarator (pp, t);
1991   pp_cxx_semicolon (pp);
1992   pp_needs_newline (pp) = true;
1993 }
1994
1995 /*
1996   template-parameter-list:
1997      template-parameter
1998      template-parameter-list , template-parameter  */
1999
2000 static inline void
2001 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
2002 {
2003   const int n = TREE_VEC_LENGTH (t);
2004   int i;
2005   for (i = 0; i < n; ++i)
2006     {
2007       if (i)
2008         pp_cxx_separate_with (pp, ',');
2009       pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2010     }
2011 }
2012
2013 /* template-parameter:
2014       type-parameter
2015       parameter-declaration
2016
2017    type-parameter:
2018      class ...(opt) identifier(opt)
2019      class identifier(opt) = type-id
2020      typename identifier(opt)
2021      typename ...(opt) identifier(opt) = type-id
2022      template < template-parameter-list > class ...(opt) identifier(opt)
2023      template < template-parameter-list > class identifier(opt) = template-name  */
2024
2025 static void
2026 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2027 {
2028   tree parameter =  TREE_VALUE (t);
2029   switch (TREE_CODE (parameter))
2030     {
2031     case TYPE_DECL:
2032       pp_cxx_ws_string (pp, "class");
2033       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2034         pp_cxx_ws_string (pp, "...");
2035       if (DECL_NAME (parameter))
2036         pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2037       /* FIXME: Check if we should print also default argument.  */
2038       break;
2039
2040     case PARM_DECL:
2041       pp_cxx_parameter_declaration (pp, parameter);
2042       break;
2043
2044     case TEMPLATE_DECL:
2045       break;
2046
2047     default:
2048       pp_unsupported_tree (pp, t);
2049       break;
2050     }
2051 }
2052
2053 /* Pretty-print a template parameter in the canonical form
2054    "template-parameter-<level>-<position in parameter list>".  */
2055
2056 void
2057 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2058 {
2059   const enum tree_code code = TREE_CODE (parm);
2060
2061   /* Brings type template parameters to the canonical forms.  */
2062   if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2063       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2064     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2065
2066   pp_cxx_begin_template_argument_list (pp);
2067   pp_cxx_ws_string (pp, M_("template-parameter-"));
2068   pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2069   pp_minus (pp);
2070   pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2071   pp_cxx_end_template_argument_list (pp);
2072 }
2073
2074 /*
2075   template-declaration:
2076      export(opt) template < template-parameter-list > declaration   */
2077
2078 static void
2079 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2080 {
2081   tree tmpl = most_general_template (t);
2082   tree level;
2083   int i = 0;
2084
2085   pp_maybe_newline_and_indent (pp, 0);
2086   for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2087     {
2088       pp_cxx_ws_string (pp, "template");
2089       pp_cxx_begin_template_argument_list (pp);
2090       pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2091       pp_cxx_end_template_argument_list (pp);
2092       pp_newline_and_indent (pp, 3);
2093       i += 3;
2094     }
2095   if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2096     pp_cxx_function_definition (pp, t);
2097   else
2098     pp_cxx_simple_declaration (pp, t);
2099 }
2100
2101 static void
2102 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2103 {
2104   pp_unsupported_tree (pp, t);
2105 }
2106
2107 static void
2108 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2109 {
2110   pp_unsupported_tree (pp, t);
2111 }
2112
2113 /*
2114     declaration:
2115        block-declaration
2116        function-definition
2117        template-declaration
2118        explicit-instantiation
2119        explicit-specialization
2120        linkage-specification
2121        namespace-definition
2122
2123     block-declaration:
2124        simple-declaration
2125        asm-definition
2126        namespace-alias-definition
2127        using-declaration
2128        using-directive
2129        static_assert-declaration */
2130 void
2131 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
2132 {
2133   if (TREE_CODE (t) == STATIC_ASSERT)
2134     {
2135       pp_cxx_ws_string (pp, "static_assert");
2136       pp_cxx_left_paren (pp);
2137       pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
2138       pp_cxx_separate_with (pp, ',');
2139       pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
2140       pp_cxx_right_paren (pp);
2141     }
2142   else if (!DECL_LANG_SPECIFIC (t))
2143     pp_cxx_simple_declaration (pp, t);
2144   else if (DECL_USE_TEMPLATE (t))
2145     switch (DECL_USE_TEMPLATE (t))
2146       {
2147       case 1:
2148         pp_cxx_template_declaration (pp, t);
2149         break;
2150
2151       case 2:
2152         pp_cxx_explicit_specialization (pp, t);
2153         break;
2154
2155       case 3:
2156         pp_cxx_explicit_instantiation (pp, t);
2157         break;
2158
2159       default:
2160         break;
2161       }
2162   else switch (TREE_CODE (t))
2163     {
2164     case VAR_DECL:
2165     case TYPE_DECL:
2166       pp_cxx_simple_declaration (pp, t);
2167       break;
2168
2169     case FUNCTION_DECL:
2170       if (DECL_SAVED_TREE (t))
2171         pp_cxx_function_definition (pp, t);
2172       else
2173         pp_cxx_simple_declaration (pp, t);
2174       break;
2175
2176     case NAMESPACE_DECL:
2177       if (DECL_NAMESPACE_ALIAS (t))
2178         pp_cxx_namespace_alias_definition (pp, t);
2179       else
2180         pp_cxx_original_namespace_definition (pp, t);
2181       break;
2182
2183     default:
2184       pp_unsupported_tree (pp, t);
2185       break;
2186     }
2187 }
2188
2189 static void
2190 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2191 {
2192   t = TREE_OPERAND (t, 0);
2193   pp_cxx_ws_string (pp, "typeid");
2194   pp_cxx_left_paren (pp);
2195   if (TYPE_P (t))
2196     pp_cxx_type_id (pp, t);
2197   else
2198     pp_cxx_expression (pp, t);
2199   pp_cxx_right_paren (pp);
2200 }
2201
2202 void
2203 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2204 {
2205   pp_cxx_ws_string (pp, "va_arg");
2206   pp_cxx_left_paren (pp);
2207   pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0));
2208   pp_cxx_separate_with (pp, ',');
2209   pp_cxx_type_id (pp, TREE_TYPE (t));
2210   pp_cxx_right_paren (pp);
2211 }
2212
2213 static bool
2214 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2215 {
2216   switch (TREE_CODE (t))
2217     {
2218     case ARROW_EXPR:
2219       if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2220           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2221         {
2222           pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2223           pp_cxx_separate_with (pp, ',');
2224           return true;
2225         }
2226       return false;
2227     case COMPONENT_REF:
2228       if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2229         return false;
2230       if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2231         pp_cxx_dot (pp);
2232       pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2233       return true;
2234     case ARRAY_REF:
2235       if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2236         return false;
2237       pp_left_bracket (pp);
2238       pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2239       pp_right_bracket (pp);
2240       return true;
2241     default:
2242       return false;
2243     }
2244 }
2245
2246 void
2247 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2248 {
2249   pp_cxx_ws_string (pp, "offsetof");
2250   pp_cxx_left_paren (pp);
2251   if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2252     pp_cxx_expression (pp, TREE_OPERAND (t, 0));
2253   pp_cxx_right_paren (pp);
2254 }
2255
2256 void
2257 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2258 {
2259   cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2260
2261   switch (kind)
2262     {
2263     case CPTK_HAS_NOTHROW_ASSIGN:
2264       pp_cxx_ws_string (pp, "__has_nothrow_assign");
2265       break;
2266     case CPTK_HAS_TRIVIAL_ASSIGN:
2267       pp_cxx_ws_string (pp, "__has_trivial_assign");
2268       break;
2269     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2270       pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2271       break;
2272     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2273       pp_cxx_ws_string (pp, "__has_trivial_constructor");
2274       break;
2275     case CPTK_HAS_NOTHROW_COPY:
2276       pp_cxx_ws_string (pp, "__has_nothrow_copy");
2277       break;
2278     case CPTK_HAS_TRIVIAL_COPY:
2279       pp_cxx_ws_string (pp, "__has_trivial_copy");
2280       break;
2281     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2282       pp_cxx_ws_string (pp, "__has_trivial_destructor");
2283       break;
2284     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2285       pp_cxx_ws_string (pp, "__has_virtual_destructor");
2286       break;
2287     case CPTK_IS_ABSTRACT:
2288       pp_cxx_ws_string (pp, "__is_abstract");
2289       break;
2290     case CPTK_IS_BASE_OF:
2291       pp_cxx_ws_string (pp, "__is_base_of");
2292       break;
2293     case CPTK_IS_CLASS:
2294       pp_cxx_ws_string (pp, "__is_class");
2295       break;
2296     case CPTK_IS_CONVERTIBLE_TO:
2297       pp_cxx_ws_string (pp, "__is_convertible_to");
2298       break;
2299     case CPTK_IS_EMPTY:
2300       pp_cxx_ws_string (pp, "__is_empty");
2301       break;
2302     case CPTK_IS_ENUM:
2303       pp_cxx_ws_string (pp, "__is_enum");
2304       break;
2305     case CPTK_IS_POD:
2306       pp_cxx_ws_string (pp, "__is_pod");
2307       break;
2308     case CPTK_IS_POLYMORPHIC:
2309       pp_cxx_ws_string (pp, "__is_polymorphic");
2310       break;
2311     case CPTK_IS_STD_LAYOUT:
2312       pp_cxx_ws_string (pp, "__is_std_layout");
2313       break;
2314     case CPTK_IS_TRIVIAL:
2315       pp_cxx_ws_string (pp, "__is_trivial");
2316       break;
2317     case CPTK_IS_UNION:
2318       pp_cxx_ws_string (pp, "__is_union");
2319       break;
2320
2321     default:
2322       gcc_unreachable ();
2323     }
2324
2325   pp_cxx_left_paren (pp);
2326   pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t));
2327
2328   if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2329     {
2330       pp_cxx_separate_with (pp, ',');
2331       pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t));
2332     }
2333
2334   pp_cxx_right_paren (pp);
2335 }
2336 \f
2337 typedef c_pretty_print_fn pp_fun;
2338
2339 /* Initialization of a C++ pretty-printer object.  */
2340
2341 void
2342 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
2343 {
2344   pp_c_pretty_printer_init (pp_c_base (pp));
2345   pp_set_line_maximum_length (pp, 0);
2346
2347   pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
2348   pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2349   pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
2350   pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2351   pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
2352   pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2353   pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2354   pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
2355   pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2356   pp->c_base.direct_abstract_declarator =
2357     (pp_fun) pp_cxx_direct_abstract_declarator;
2358   pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
2359
2360   /* pp->c_base.statement = (pp_fun) pp_cxx_statement;  */
2361
2362   pp->c_base.constant = (pp_fun) pp_cxx_constant;
2363   pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
2364   pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
2365   pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2366   pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
2367   pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2368   pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2369   pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2370   pp->c_base.expression = (pp_fun) pp_cxx_expression;
2371   pp->enclosing_scope = global_namespace;
2372 }