OSDN Git Service

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