OSDN Git Service

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