OSDN Git Service

2010-09-29 Richard Guenther <rguenther@suse.de>
[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 NOEXCEPT_EXPR:
795       pp_cxx_ws_string (pp, "noexcept");
796       pp_cxx_whitespace (pp);
797       pp_cxx_left_paren (pp);
798       pp_cxx_expression (pp, TREE_OPERAND (t, 0));
799       pp_cxx_right_paren (pp);
800       break;
801
802     case UNARY_PLUS_EXPR:
803       pp_plus (pp);
804       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
805       break;
806
807     default:
808       pp_c_unary_expression (pp_c_base (pp), t);
809       break;
810     }
811 }
812
813 /* cast-expression:
814       unary-expression
815       ( type-id ) cast-expression  */
816
817 static void
818 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
819 {
820   switch (TREE_CODE (t))
821     {
822     case CAST_EXPR:
823       pp_cxx_type_id (pp, TREE_TYPE (t));
824       pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
825       break;
826
827     default:
828       pp_c_cast_expression (pp_c_base (pp), t);
829       break;
830     }
831 }
832
833 /* pm-expression:
834       cast-expression
835       pm-expression .* cast-expression
836       pm-expression ->* cast-expression  */
837
838 static void
839 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
840 {
841   switch (TREE_CODE (t))
842     {
843       /* Handle unfortunate OFFSET_REF overloading here.  */
844     case OFFSET_REF:
845       if (TYPE_P (TREE_OPERAND (t, 0)))
846         {
847           pp_cxx_qualified_id (pp, t);
848           break;
849         }
850       /* Else fall through.  */
851     case MEMBER_REF:
852     case DOTSTAR_EXPR:
853       pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
854       if (TREE_CODE (t) == MEMBER_REF)
855         pp_cxx_arrow (pp);
856       else
857         pp_cxx_dot (pp);
858       pp_star(pp);
859       pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
860       break;
861
862
863     default:
864       pp_cxx_cast_expression (pp, t);
865       break;
866     }
867 }
868
869 /* multiplicative-expression:
870       pm-expression
871       multiplicative-expression * pm-expression
872       multiplicative-expression / pm-expression
873       multiplicative-expression % pm-expression  */
874
875 static void
876 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
877 {
878   enum tree_code code = TREE_CODE (e);
879   switch (code)
880     {
881     case MULT_EXPR:
882     case TRUNC_DIV_EXPR:
883     case TRUNC_MOD_EXPR:
884       pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
885       pp_space (pp);
886       if (code == MULT_EXPR)
887         pp_star (pp);
888       else if (code == TRUNC_DIV_EXPR)
889         pp_slash (pp);
890       else
891         pp_modulo (pp);
892       pp_space (pp);
893       pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
894       break;
895
896     default:
897       pp_cxx_pm_expression (pp, e);
898       break;
899     }
900 }
901
902 /* conditional-expression:
903       logical-or-expression
904       logical-or-expression ?  expression  : assignment-expression  */
905
906 static void
907 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
908 {
909   if (TREE_CODE (e) == COND_EXPR)
910     {
911       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
912       pp_space (pp);
913       pp_question (pp);
914       pp_space (pp);
915       pp_cxx_expression (pp, TREE_OPERAND (e, 1));
916       pp_space (pp);
917       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
918     }
919   else
920     pp_c_logical_or_expression (pp_c_base (pp), e);
921 }
922
923 /* Pretty-print a compound assignment operator token as indicated by T.  */
924
925 static void
926 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
927 {
928   const char *op;
929
930   switch (TREE_CODE (t))
931     {
932     case NOP_EXPR:
933       op = "=";
934       break;
935
936     case PLUS_EXPR:
937       op = "+=";
938       break;
939
940     case MINUS_EXPR:
941       op = "-=";
942       break;
943
944     case TRUNC_DIV_EXPR:
945       op = "/=";
946       break;
947
948     case TRUNC_MOD_EXPR:
949       op = "%=";
950       break;
951
952     default:
953       op = tree_code_name[TREE_CODE (t)];
954       break;
955     }
956
957   pp_cxx_ws_string (pp, op);
958 }
959
960
961 /* assignment-expression:
962       conditional-expression
963       logical-or-expression assignment-operator assignment-expression
964       throw-expression
965
966    throw-expression:
967        throw assignment-expression(opt)
968
969    assignment-operator: one of
970       =    *=    /=    %=    +=    -=    >>=    <<=    &=    ^=    |=  */
971
972 static void
973 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
974 {
975   switch (TREE_CODE (e))
976     {
977     case MODIFY_EXPR:
978     case INIT_EXPR:
979       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
980       pp_space (pp);
981       pp_equal (pp);
982       pp_space (pp);
983       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
984       break;
985
986     case THROW_EXPR:
987       pp_cxx_ws_string (pp, "throw");
988       if (TREE_OPERAND (e, 0))
989         pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
990       break;
991
992     case MODOP_EXPR:
993       pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
994       pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
995       pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
996       break;
997
998     default:
999       pp_cxx_conditional_expression (pp, e);
1000       break;
1001     }
1002 }
1003
1004 static void
1005 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
1006 {
1007   switch (TREE_CODE (t))
1008     {
1009     case STRING_CST:
1010     case INTEGER_CST:
1011     case REAL_CST:
1012     case COMPLEX_CST:
1013       pp_cxx_constant (pp, t);
1014       break;
1015
1016     case RESULT_DECL:
1017       pp_cxx_unqualified_id (pp, t);
1018       break;
1019
1020 #if 0
1021     case OFFSET_REF:
1022 #endif
1023     case SCOPE_REF:
1024     case PTRMEM_CST:
1025       pp_cxx_qualified_id (pp, t);
1026       break;
1027
1028     case OVERLOAD:
1029       t = OVL_CURRENT (t);
1030     case VAR_DECL:
1031     case PARM_DECL:
1032     case FIELD_DECL:
1033     case CONST_DECL:
1034     case FUNCTION_DECL:
1035     case BASELINK:
1036     case TEMPLATE_DECL:
1037     case TEMPLATE_TYPE_PARM:
1038     case TEMPLATE_PARM_INDEX:
1039     case TEMPLATE_TEMPLATE_PARM:
1040     case STMT_EXPR:
1041       pp_cxx_primary_expression (pp, t);
1042       break;
1043
1044     case CALL_EXPR:
1045     case DYNAMIC_CAST_EXPR:
1046     case STATIC_CAST_EXPR:
1047     case REINTERPRET_CAST_EXPR:
1048     case CONST_CAST_EXPR:
1049 #if 0
1050     case MEMBER_REF:
1051 #endif
1052     case EMPTY_CLASS_EXPR:
1053     case TYPEID_EXPR:
1054     case PSEUDO_DTOR_EXPR:
1055     case AGGR_INIT_EXPR:
1056     case ARROW_EXPR:
1057       pp_cxx_postfix_expression (pp, t);
1058       break;
1059
1060     case NEW_EXPR:
1061     case VEC_NEW_EXPR:
1062       pp_cxx_new_expression (pp, t);
1063       break;
1064
1065     case DELETE_EXPR:
1066     case VEC_DELETE_EXPR:
1067       pp_cxx_delete_expression (pp, t);
1068       break;
1069
1070     case SIZEOF_EXPR:
1071     case ALIGNOF_EXPR:
1072     case NOEXCEPT_EXPR:
1073       pp_cxx_unary_expression (pp, t);
1074       break;
1075
1076     case CAST_EXPR:
1077       pp_cxx_cast_expression (pp, t);
1078       break;
1079
1080     case OFFSET_REF:
1081     case MEMBER_REF:
1082     case DOTSTAR_EXPR:
1083       pp_cxx_pm_expression (pp, t);
1084       break;
1085
1086     case MULT_EXPR:
1087     case TRUNC_DIV_EXPR:
1088     case TRUNC_MOD_EXPR:
1089       pp_cxx_multiplicative_expression (pp, t);
1090       break;
1091
1092     case COND_EXPR:
1093       pp_cxx_conditional_expression (pp, t);
1094       break;
1095
1096     case MODIFY_EXPR:
1097     case INIT_EXPR:
1098     case THROW_EXPR:
1099     case MODOP_EXPR:
1100       pp_cxx_assignment_expression (pp, t);
1101       break;
1102
1103     case NON_DEPENDENT_EXPR:
1104     case MUST_NOT_THROW_EXPR:
1105       pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1106       break;
1107
1108     case EXPR_PACK_EXPANSION:
1109       pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t));
1110       pp_cxx_ws_string (pp, "...");
1111       break;
1112
1113     case TEMPLATE_ID_EXPR:
1114       pp_cxx_template_id (pp, t);
1115       break;
1116
1117     case NONTYPE_ARGUMENT_PACK:
1118       {
1119         tree args = ARGUMENT_PACK_ARGS (t);
1120         int i, len = TREE_VEC_LENGTH (args);
1121         for (i = 0; i < len; ++i)
1122           {
1123             if (i > 0)
1124               pp_cxx_separate_with (pp, ',');
1125             pp_cxx_expression (pp, TREE_VEC_ELT (args, i));
1126           }
1127       }
1128       break;
1129
1130     default:
1131       pp_c_expression (pp_c_base (pp), t);
1132       break;
1133     }
1134 }
1135
1136
1137 /* Declarations.  */
1138
1139 /* function-specifier:
1140       inline
1141       virtual
1142       explicit   */
1143
1144 static void
1145 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1146 {
1147   switch (TREE_CODE (t))
1148     {
1149     case FUNCTION_DECL:
1150       if (DECL_VIRTUAL_P (t))
1151         pp_cxx_ws_string (pp, "virtual");
1152       else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1153         pp_cxx_ws_string (pp, "explicit");
1154       else
1155         pp_c_function_specifier (pp_c_base (pp), t);
1156
1157     default:
1158       break;
1159     }
1160 }
1161
1162 /* decl-specifier-seq:
1163       decl-specifier-seq(opt) decl-specifier
1164
1165    decl-specifier:
1166       storage-class-specifier
1167       type-specifier
1168       function-specifier
1169       friend
1170       typedef  */
1171
1172 static void
1173 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1174 {
1175   switch (TREE_CODE (t))
1176     {
1177     case VAR_DECL:
1178     case PARM_DECL:
1179     case CONST_DECL:
1180     case FIELD_DECL:
1181       pp_cxx_storage_class_specifier (pp, t);
1182       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1183       break;
1184
1185     case TYPE_DECL:
1186       pp_cxx_ws_string (pp, "typedef");
1187       pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1188       break;
1189
1190     case FUNCTION_DECL:
1191       /* Constructors don't have return types.  And conversion functions
1192          do not have a type-specifier in their return types.  */
1193       if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1194         pp_cxx_function_specifier (pp, t);
1195       else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1196         pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1197       else
1198         default:
1199       pp_c_declaration_specifiers (pp_c_base (pp), t);
1200       break;
1201     }
1202 }
1203
1204 /* simple-type-specifier:
1205       ::(opt) nested-name-specifier(opt) type-name
1206       ::(opt) nested-name-specifier(opt) template(opt) template-id
1207       char
1208       wchar_t
1209       bool
1210       short
1211       int
1212       long
1213       signed
1214       unsigned
1215       float
1216       double
1217       void  */
1218
1219 static void
1220 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1221 {
1222   switch (TREE_CODE (t))
1223     {
1224     case RECORD_TYPE:
1225     case UNION_TYPE:
1226     case ENUMERAL_TYPE:
1227       pp_cxx_qualified_id (pp, t);
1228       break;
1229
1230     case TEMPLATE_TYPE_PARM:
1231     case TEMPLATE_TEMPLATE_PARM:
1232     case TEMPLATE_PARM_INDEX:
1233       pp_cxx_unqualified_id (pp, t);
1234       break;
1235
1236     case TYPENAME_TYPE:
1237       pp_cxx_ws_string (pp, "typename");
1238       pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1239       pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1240       break;
1241
1242     default:
1243       pp_c_type_specifier (pp_c_base (pp), t);
1244       break;
1245     }
1246 }
1247
1248 /* type-specifier-seq:
1249       type-specifier type-specifier-seq(opt)
1250
1251    type-specifier:
1252       simple-type-specifier
1253       class-specifier
1254       enum-specifier
1255       elaborated-type-specifier
1256       cv-qualifier   */
1257
1258 static void
1259 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1260 {
1261   switch (TREE_CODE (t))
1262     {
1263     case TEMPLATE_DECL:
1264     case TEMPLATE_TYPE_PARM:
1265     case TEMPLATE_TEMPLATE_PARM:
1266     case TYPE_DECL:
1267     case BOUND_TEMPLATE_TEMPLATE_PARM:
1268       pp_cxx_cv_qualifier_seq (pp, t);
1269       pp_cxx_simple_type_specifier (pp, t);
1270       break;
1271
1272     case METHOD_TYPE:
1273       pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1274       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1275       pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1276       break;
1277
1278     case DECLTYPE_TYPE:
1279       pp_cxx_ws_string (pp, "decltype");
1280       pp_cxx_left_paren (pp);
1281       pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t));
1282       pp_cxx_right_paren (pp);
1283       break;
1284
1285     case RECORD_TYPE:
1286       if (TYPE_PTRMEMFUNC_P (t))
1287         {
1288           tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1289           pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1290           pp_cxx_whitespace (pp);
1291           pp_cxx_ptr_operator (pp, t);
1292           break;
1293         }
1294       /* else fall through */
1295
1296     default:
1297       if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1298         pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1299     }
1300 }
1301
1302 /* ptr-operator:
1303       * cv-qualifier-seq(opt)
1304       &
1305       ::(opt) nested-name-specifier * cv-qualifier-seq(opt)  */
1306
1307 static void
1308 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1309 {
1310   if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1311     t = TREE_TYPE (t);
1312   switch (TREE_CODE (t))
1313     {
1314     case REFERENCE_TYPE:
1315     case POINTER_TYPE:
1316       if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1317           || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1318         pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1319       if (TREE_CODE (t) == POINTER_TYPE)
1320         {
1321           pp_star (pp);
1322           pp_cxx_cv_qualifier_seq (pp, t);
1323         }
1324       else
1325         pp_ampersand (pp);
1326       break;
1327
1328     case RECORD_TYPE:
1329       if (TYPE_PTRMEMFUNC_P (t))
1330         {
1331           pp_cxx_left_paren (pp);
1332           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1333           pp_star (pp);
1334           break;
1335         }
1336     case OFFSET_TYPE:
1337       if (TYPE_PTR_TO_MEMBER_P (t))
1338         {
1339           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1340             pp_cxx_left_paren (pp);
1341           pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1342           pp_star (pp);
1343           pp_cxx_cv_qualifier_seq (pp, t);
1344           break;
1345         }
1346       /* else fall through.  */
1347
1348     default:
1349       pp_unsupported_tree (pp, t);
1350       break;
1351     }
1352 }
1353
1354 static inline tree
1355 pp_cxx_implicit_parameter_type (tree mf)
1356 {
1357   return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1358 }
1359
1360 /*
1361    parameter-declaration:
1362       decl-specifier-seq declarator
1363       decl-specifier-seq declarator = assignment-expression
1364       decl-specifier-seq abstract-declarator(opt)
1365       decl-specifier-seq abstract-declarator(opt) assignment-expression  */
1366
1367 static inline void
1368 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1369 {
1370   pp_cxx_decl_specifier_seq (pp, t);
1371   if (TYPE_P (t))
1372     pp_cxx_abstract_declarator (pp, t);
1373   else
1374     pp_cxx_declarator (pp, t);
1375 }
1376
1377 /* parameter-declaration-clause:
1378       parameter-declaration-list(opt) ...(opt)
1379       parameter-declaration-list , ...
1380
1381    parameter-declaration-list:
1382       parameter-declaration
1383       parameter-declaration-list , parameter-declaration  */
1384
1385 static void
1386 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1387 {
1388   tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1389   tree types =
1390     TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1391   const bool abstract = args == NULL
1392     || pp_c_base (pp)->flags & pp_c_flag_abstract;
1393   bool first = true;
1394
1395   /* Skip artificial parameter for nonstatic member functions.  */
1396   if (TREE_CODE (t) == METHOD_TYPE)
1397     types = TREE_CHAIN (types);
1398
1399   pp_cxx_left_paren (pp);
1400   for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1401     {
1402       if (!first)
1403         pp_cxx_separate_with (pp, ',');
1404       first = false;
1405       pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1406       if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1407         {
1408           pp_cxx_whitespace (pp);
1409           pp_equal (pp);
1410           pp_cxx_whitespace (pp);
1411           pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1412         }
1413     }
1414   pp_cxx_right_paren (pp);
1415 }
1416
1417 /* exception-specification:
1418       throw ( type-id-list(opt) )
1419
1420    type-id-list
1421       type-id
1422       type-id-list , type-id   */
1423
1424 static void
1425 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1426 {
1427   tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1428   bool need_comma = false;
1429
1430   if (ex_spec == NULL)
1431     return;
1432   if (TREE_PURPOSE (ex_spec))
1433     {
1434       pp_cxx_ws_string (pp, "noexcept");
1435       pp_cxx_whitespace (pp);
1436       pp_cxx_left_paren (pp);
1437       pp_cxx_expression (pp, TREE_PURPOSE (ex_spec));
1438       pp_cxx_right_paren (pp);
1439       return;
1440     }
1441   pp_cxx_ws_string (pp, "throw");
1442   pp_cxx_left_paren (pp);
1443   for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1444     {
1445       tree type = TREE_VALUE (ex_spec);
1446       tree argpack = NULL_TREE;
1447       int i, len = 1;
1448
1449       if (ARGUMENT_PACK_P (type))
1450         {
1451           argpack = ARGUMENT_PACK_ARGS (type);
1452           len = TREE_VEC_LENGTH (argpack);
1453         }
1454
1455       for (i = 0; i < len; ++i)
1456         {
1457           if (argpack)
1458             type = TREE_VEC_ELT (argpack, i);
1459
1460           if (need_comma)
1461             pp_cxx_separate_with (pp, ',');
1462           else
1463             need_comma = true;
1464
1465           pp_cxx_type_id (pp, type);
1466         }
1467     }
1468   pp_cxx_right_paren (pp);
1469 }
1470
1471 /* direct-declarator:
1472       declarator-id
1473       direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1474                                             exception-specification(opt)
1475       direct-declaration [ constant-expression(opt) ]
1476       ( declarator )  */
1477
1478 static void
1479 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1480 {
1481   switch (TREE_CODE (t))
1482     {
1483     case VAR_DECL:
1484     case PARM_DECL:
1485     case CONST_DECL:
1486     case FIELD_DECL:
1487       if (DECL_NAME (t))
1488         {
1489           pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1490
1491           if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t))
1492               || template_parameter_pack_p (t))
1493             /* A function parameter pack or non-type template
1494                parameter pack.  */
1495             pp_cxx_ws_string (pp, "...");
1496                       
1497           pp_cxx_id_expression (pp, DECL_NAME (t));
1498         }
1499       pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1500       break;
1501
1502     case FUNCTION_DECL:
1503       pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1504       pp_cxx_id_expression (pp, t);
1505       pp_cxx_parameter_declaration_clause (pp, t);
1506
1507       if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1508         {
1509           pp_base (pp)->padding = pp_before;
1510           pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1511         }
1512
1513       pp_cxx_exception_specification (pp, TREE_TYPE (t));
1514       break;
1515
1516     case TYPENAME_TYPE:
1517     case TEMPLATE_DECL:
1518     case TEMPLATE_TYPE_PARM:
1519     case TEMPLATE_PARM_INDEX:
1520     case TEMPLATE_TEMPLATE_PARM:
1521       break;
1522
1523     default:
1524       pp_c_direct_declarator (pp_c_base (pp), t);
1525       break;
1526     }
1527 }
1528
1529 /* declarator:
1530    direct-declarator
1531    ptr-operator declarator  */
1532
1533 static void
1534 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1535 {
1536   pp_cxx_direct_declarator (pp, t);
1537 }
1538
1539 /* ctor-initializer:
1540       : mem-initializer-list
1541
1542    mem-initializer-list:
1543       mem-initializer
1544       mem-initializer , mem-initializer-list
1545
1546    mem-initializer:
1547       mem-initializer-id ( expression-list(opt) )
1548
1549    mem-initializer-id:
1550       ::(opt) nested-name-specifier(opt) class-name
1551       identifier   */
1552
1553 static void
1554 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1555 {
1556   t = TREE_OPERAND (t, 0);
1557   pp_cxx_whitespace (pp);
1558   pp_colon (pp);
1559   pp_cxx_whitespace (pp);
1560   for (; t; t = TREE_CHAIN (t))
1561     {
1562       tree purpose = TREE_PURPOSE (t);
1563       bool is_pack = PACK_EXPANSION_P (purpose);
1564
1565       if (is_pack)
1566         pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose));
1567       else
1568         pp_cxx_primary_expression (pp, purpose);
1569       pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1570       if (is_pack)
1571         pp_cxx_ws_string (pp, "...");
1572       if (TREE_CHAIN (t))
1573         pp_cxx_separate_with (pp, ',');
1574     }
1575 }
1576
1577 /* function-definition:
1578       decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1579       decl-specifier-seq(opt) declarator function-try-block  */
1580
1581 static void
1582 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1583 {
1584   tree saved_scope = pp->enclosing_scope;
1585   pp_cxx_decl_specifier_seq (pp, t);
1586   pp_cxx_declarator (pp, t);
1587   pp_needs_newline (pp) = true;
1588   pp->enclosing_scope = DECL_CONTEXT (t);
1589   if (DECL_SAVED_TREE (t))
1590     pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1591   else
1592     {
1593       pp_cxx_semicolon (pp);
1594       pp_needs_newline (pp) = true;
1595     }
1596   pp_flush (pp);
1597   pp->enclosing_scope = saved_scope;
1598 }
1599
1600 /* abstract-declarator:
1601       ptr-operator abstract-declarator(opt)
1602       direct-abstract-declarator  */
1603
1604 static void
1605 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1606 {
1607   if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1608     pp_cxx_right_paren (pp);
1609   else if (POINTER_TYPE_P (t))
1610     {
1611       if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1612           || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1613         pp_cxx_right_paren (pp);
1614       t = TREE_TYPE (t);
1615     }
1616   pp_cxx_direct_abstract_declarator (pp, t);
1617 }
1618
1619 /* direct-abstract-declarator:
1620       direct-abstract-declarator(opt) ( parameter-declaration-clause )
1621                            cv-qualifier-seq(opt) exception-specification(opt)
1622       direct-abstract-declarator(opt) [ constant-expression(opt) ]
1623       ( abstract-declarator )  */
1624
1625 static void
1626 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1627 {
1628   switch (TREE_CODE (t))
1629     {
1630     case REFERENCE_TYPE:
1631       pp_cxx_abstract_declarator (pp, t);
1632       break;
1633
1634     case RECORD_TYPE:
1635       if (TYPE_PTRMEMFUNC_P (t))
1636         pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1637       break;
1638
1639     case METHOD_TYPE:
1640     case FUNCTION_TYPE:
1641       pp_cxx_parameter_declaration_clause (pp, t);
1642       pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1643       if (TREE_CODE (t) == METHOD_TYPE)
1644         {
1645           pp_base (pp)->padding = pp_before;
1646           pp_cxx_cv_qualifier_seq
1647             (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1648         }
1649       pp_cxx_exception_specification (pp, t);
1650       break;
1651
1652     case TYPENAME_TYPE:
1653     case TEMPLATE_TYPE_PARM:
1654     case TEMPLATE_TEMPLATE_PARM:
1655     case BOUND_TEMPLATE_TEMPLATE_PARM:
1656     case UNBOUND_CLASS_TEMPLATE:
1657       break;
1658
1659     default:
1660       pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1661       break;
1662     }
1663 }
1664
1665 /* type-id:
1666      type-specifier-seq abstract-declarator(opt) */
1667
1668 static void
1669 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1670 {
1671   pp_flags saved_flags = pp_c_base (pp)->flags;
1672   pp_c_base (pp)->flags |= pp_c_flag_abstract;
1673
1674   switch (TREE_CODE (t))
1675     {
1676     case TYPE_DECL:
1677     case UNION_TYPE:
1678     case RECORD_TYPE:
1679     case ENUMERAL_TYPE:
1680     case TYPENAME_TYPE:
1681     case BOUND_TEMPLATE_TEMPLATE_PARM:
1682     case UNBOUND_CLASS_TEMPLATE:
1683     case TEMPLATE_TEMPLATE_PARM:
1684     case TEMPLATE_TYPE_PARM:
1685     case TEMPLATE_PARM_INDEX:
1686     case TEMPLATE_DECL:
1687     case TYPEOF_TYPE:
1688     case DECLTYPE_TYPE:
1689     case TEMPLATE_ID_EXPR:
1690       pp_cxx_type_specifier_seq (pp, t);
1691       break;
1692
1693     case TYPE_PACK_EXPANSION:
1694       pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t));
1695       pp_cxx_ws_string (pp, "...");
1696       break;
1697
1698     default:
1699       pp_c_type_id (pp_c_base (pp), t);
1700       break;
1701     }
1702
1703   pp_c_base (pp)->flags = saved_flags;
1704 }
1705
1706 /* template-argument-list:
1707       template-argument ...(opt)
1708       template-argument-list, template-argument ...(opt)
1709
1710    template-argument:
1711       assignment-expression
1712       type-id
1713       template-name  */
1714
1715 static void
1716 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1717 {
1718   int i;
1719   bool need_comma = false;
1720
1721   if (t == NULL)
1722     return;
1723   for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1724     {
1725       tree arg = TREE_VEC_ELT (t, i);
1726       tree argpack = NULL_TREE;
1727       int idx, len = 1;
1728
1729       if (ARGUMENT_PACK_P (arg))
1730         {
1731           argpack = ARGUMENT_PACK_ARGS (arg);
1732           len = TREE_VEC_LENGTH (argpack);
1733         }
1734
1735       for (idx = 0; idx < len; idx++)
1736         {
1737           if (argpack)
1738             arg = TREE_VEC_ELT (argpack, idx);
1739           
1740           if (need_comma)
1741             pp_cxx_separate_with (pp, ',');
1742           else
1743             need_comma = true;
1744
1745           if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1746                                && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1747             pp_cxx_type_id (pp, arg);
1748           else
1749             pp_cxx_expression (pp, arg);
1750         }
1751     }
1752 }
1753
1754
1755 static void
1756 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1757 {
1758   t = DECL_EXPR_DECL (t);
1759   pp_cxx_type_specifier_seq (pp, t);
1760   if (TYPE_P (t))
1761     pp_cxx_abstract_declarator (pp, t);
1762   else
1763     pp_cxx_declarator (pp, t);
1764 }
1765
1766 /* Statements.  */
1767
1768 static void
1769 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1770 {
1771   switch (TREE_CODE (t))
1772     {
1773     case CTOR_INITIALIZER:
1774       pp_cxx_ctor_initializer (pp, t);
1775       break;
1776
1777     case USING_STMT:
1778       pp_cxx_ws_string (pp, "using");
1779       pp_cxx_ws_string (pp, "namespace");
1780       if (DECL_CONTEXT (t))
1781         pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1782       pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1783       break;
1784
1785     case USING_DECL:
1786       pp_cxx_ws_string (pp, "using");
1787       pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1788       pp_cxx_unqualified_id (pp, DECL_NAME (t));
1789       break;
1790
1791     case EH_SPEC_BLOCK:
1792       break;
1793
1794       /* try-block:
1795             try compound-statement handler-seq  */
1796     case TRY_BLOCK:
1797       pp_maybe_newline_and_indent (pp, 0);
1798       pp_cxx_ws_string (pp, "try");
1799       pp_newline_and_indent (pp, 3);
1800       pp_cxx_statement (pp, TRY_STMTS (t));
1801       pp_newline_and_indent (pp, -3);
1802       if (CLEANUP_P (t))
1803         ;
1804       else
1805         pp_cxx_statement (pp, TRY_HANDLERS (t));
1806       break;
1807
1808       /*
1809          handler-seq:
1810             handler handler-seq(opt)
1811
1812          handler:
1813          catch ( exception-declaration ) compound-statement
1814
1815          exception-declaration:
1816             type-specifier-seq declarator
1817             type-specifier-seq abstract-declarator
1818             ...   */
1819     case HANDLER:
1820       pp_cxx_ws_string (pp, "catch");
1821       pp_cxx_left_paren (pp);
1822       pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1823       pp_cxx_right_paren (pp);
1824       pp_indentation (pp) += 3;
1825       pp_needs_newline (pp) = true;
1826       pp_cxx_statement (pp, HANDLER_BODY (t));
1827       pp_indentation (pp) -= 3;
1828       pp_needs_newline (pp) = true;
1829       break;
1830
1831       /* selection-statement:
1832             if ( expression ) statement
1833             if ( expression ) statement else statement  */
1834     case IF_STMT:
1835       pp_cxx_ws_string (pp, "if");
1836       pp_cxx_whitespace (pp);
1837       pp_cxx_left_paren (pp);
1838       pp_cxx_expression (pp, IF_COND (t));
1839       pp_cxx_right_paren (pp);
1840       pp_newline_and_indent (pp, 2);
1841       pp_cxx_statement (pp, THEN_CLAUSE (t));
1842       pp_newline_and_indent (pp, -2);
1843       if (ELSE_CLAUSE (t))
1844         {
1845           tree else_clause = ELSE_CLAUSE (t);
1846           pp_cxx_ws_string (pp, "else");
1847           if (TREE_CODE (else_clause) == IF_STMT)
1848             pp_cxx_whitespace (pp);
1849           else
1850             pp_newline_and_indent (pp, 2);
1851           pp_cxx_statement (pp, else_clause);
1852           if (TREE_CODE (else_clause) != IF_STMT)
1853             pp_newline_and_indent (pp, -2);
1854         }
1855       break;
1856
1857     case SWITCH_STMT:
1858       pp_cxx_ws_string (pp, "switch");
1859       pp_space (pp);
1860       pp_cxx_left_paren (pp);
1861       pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1862       pp_cxx_right_paren (pp);
1863       pp_indentation (pp) += 3;
1864       pp_needs_newline (pp) = true;
1865       pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1866       pp_newline_and_indent (pp, -3);
1867       break;
1868
1869       /* iteration-statement:
1870             while ( expression ) statement
1871             do statement while ( expression ) ;
1872             for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1873             for ( declaration expression(opt) ; expression(opt) ) statement  */
1874     case WHILE_STMT:
1875       pp_cxx_ws_string (pp, "while");
1876       pp_space (pp);
1877       pp_cxx_left_paren (pp);
1878       pp_cxx_expression (pp, WHILE_COND (t));
1879       pp_cxx_right_paren (pp);
1880       pp_newline_and_indent (pp, 3);
1881       pp_cxx_statement (pp, WHILE_BODY (t));
1882       pp_indentation (pp) -= 3;
1883       pp_needs_newline (pp) = true;
1884       break;
1885
1886     case DO_STMT:
1887       pp_cxx_ws_string (pp, "do");
1888       pp_newline_and_indent (pp, 3);
1889       pp_cxx_statement (pp, DO_BODY (t));
1890       pp_newline_and_indent (pp, -3);
1891       pp_cxx_ws_string (pp, "while");
1892       pp_space (pp);
1893       pp_cxx_left_paren (pp);
1894       pp_cxx_expression (pp, DO_COND (t));
1895       pp_cxx_right_paren (pp);
1896       pp_cxx_semicolon (pp);
1897       pp_needs_newline (pp) = true;
1898       break;
1899
1900     case FOR_STMT:
1901       pp_cxx_ws_string (pp, "for");
1902       pp_space (pp);
1903       pp_cxx_left_paren (pp);
1904       if (FOR_INIT_STMT (t))
1905         pp_cxx_statement (pp, FOR_INIT_STMT (t));
1906       else
1907         pp_cxx_semicolon (pp);
1908       pp_needs_newline (pp) = false;
1909       pp_cxx_whitespace (pp);
1910       if (FOR_COND (t))
1911         pp_cxx_expression (pp, FOR_COND (t));
1912       pp_cxx_semicolon (pp);
1913       pp_needs_newline (pp) = false;
1914       pp_cxx_whitespace (pp);
1915       if (FOR_EXPR (t))
1916         pp_cxx_expression (pp, FOR_EXPR (t));
1917       pp_cxx_right_paren (pp);
1918       pp_newline_and_indent (pp, 3);
1919       pp_cxx_statement (pp, FOR_BODY (t));
1920       pp_indentation (pp) -= 3;
1921       pp_needs_newline (pp) = true;
1922       break;
1923
1924     case RANGE_FOR_STMT:
1925       pp_cxx_ws_string (pp, "for");
1926       pp_space (pp);
1927       pp_cxx_left_paren (pp);
1928       pp_cxx_statement (pp, RANGE_FOR_DECL (t));
1929       pp_space (pp);
1930       pp_needs_newline (pp) = false;
1931       pp_colon (pp);
1932       pp_space (pp);
1933       pp_cxx_statement (pp, RANGE_FOR_EXPR (t));
1934       pp_cxx_right_paren (pp);
1935       pp_newline_and_indent (pp, 3);
1936       pp_cxx_statement (pp, FOR_BODY (t));
1937       pp_indentation (pp) -= 3;
1938       pp_needs_newline (pp) = true;
1939       break;
1940
1941       /* jump-statement:
1942             goto identifier;
1943             continue ;
1944             return expression(opt) ;  */
1945     case BREAK_STMT:
1946     case CONTINUE_STMT:
1947       pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1948       pp_cxx_semicolon (pp);
1949       pp_needs_newline (pp) = true;
1950       break;
1951
1952       /* expression-statement:
1953             expression(opt) ;  */
1954     case EXPR_STMT:
1955       pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1956       pp_cxx_semicolon (pp);
1957       pp_needs_newline (pp) = true;
1958       break;
1959
1960     case CLEANUP_STMT:
1961       pp_cxx_ws_string (pp, "try");
1962       pp_newline_and_indent (pp, 2);
1963       pp_cxx_statement (pp, CLEANUP_BODY (t));
1964       pp_newline_and_indent (pp, -2);
1965       pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1966       pp_newline_and_indent (pp, 2);
1967       pp_cxx_statement (pp, CLEANUP_EXPR (t));
1968       pp_newline_and_indent (pp, -2);
1969       break;
1970
1971     case STATIC_ASSERT:
1972       pp_cxx_declaration (pp, t);
1973       break;
1974
1975     default:
1976       pp_c_statement (pp_c_base (pp), t);
1977       break;
1978     }
1979 }
1980
1981 /* original-namespace-definition:
1982       namespace identifier { namespace-body }
1983
1984   As an edge case, we also handle unnamed namespace definition here.  */
1985
1986 static void
1987 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1988 {
1989   pp_cxx_ws_string (pp, "namespace");
1990   if (DECL_CONTEXT (t))
1991     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1992   if (DECL_NAME (t))
1993     pp_cxx_unqualified_id (pp, t);
1994   pp_cxx_whitespace (pp);
1995   pp_cxx_left_brace (pp);
1996   /* We do not print the namespace-body.  */
1997   pp_cxx_whitespace (pp);
1998   pp_cxx_right_brace (pp);
1999 }
2000
2001 /* namespace-alias:
2002       identifier
2003
2004    namespace-alias-definition:
2005       namespace identifier = qualified-namespace-specifier ;
2006
2007    qualified-namespace-specifier:
2008       ::(opt) nested-name-specifier(opt) namespace-name   */
2009
2010 static void
2011 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
2012 {
2013   pp_cxx_ws_string (pp, "namespace");
2014   if (DECL_CONTEXT (t))
2015     pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2016   pp_cxx_unqualified_id (pp, t);
2017   pp_cxx_whitespace (pp);
2018   pp_equal (pp);
2019   pp_cxx_whitespace (pp);
2020   if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
2021     pp_cxx_nested_name_specifier (pp,
2022                                   DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
2023   pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
2024   pp_cxx_semicolon (pp);
2025 }
2026
2027 /* simple-declaration:
2028       decl-specifier-seq(opt) init-declarator-list(opt)  */
2029
2030 static void
2031 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
2032 {
2033   pp_cxx_decl_specifier_seq (pp, t);
2034   pp_cxx_init_declarator (pp, t);
2035   pp_cxx_semicolon (pp);
2036   pp_needs_newline (pp) = true;
2037 }
2038
2039 /*
2040   template-parameter-list:
2041      template-parameter
2042      template-parameter-list , template-parameter  */
2043
2044 static inline void
2045 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
2046 {
2047   const int n = TREE_VEC_LENGTH (t);
2048   int i;
2049   for (i = 0; i < n; ++i)
2050     {
2051       if (i)
2052         pp_cxx_separate_with (pp, ',');
2053       pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2054     }
2055 }
2056
2057 /* template-parameter:
2058       type-parameter
2059       parameter-declaration
2060
2061    type-parameter:
2062      class ...(opt) identifier(opt)
2063      class identifier(opt) = type-id
2064      typename identifier(opt)
2065      typename ...(opt) identifier(opt) = type-id
2066      template < template-parameter-list > class ...(opt) identifier(opt)
2067      template < template-parameter-list > class identifier(opt) = template-name  */
2068
2069 static void
2070 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2071 {
2072   tree parameter =  TREE_VALUE (t);
2073   switch (TREE_CODE (parameter))
2074     {
2075     case TYPE_DECL:
2076       pp_cxx_ws_string (pp, "class");
2077       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2078         pp_cxx_ws_string (pp, "...");
2079       if (DECL_NAME (parameter))
2080         pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2081       /* FIXME: Check if we should print also default argument.  */
2082       break;
2083
2084     case PARM_DECL:
2085       pp_cxx_parameter_declaration (pp, parameter);
2086       break;
2087
2088     case TEMPLATE_DECL:
2089       break;
2090
2091     default:
2092       pp_unsupported_tree (pp, t);
2093       break;
2094     }
2095 }
2096
2097 /* Pretty-print a template parameter in the canonical form
2098    "template-parameter-<level>-<position in parameter list>".  */
2099
2100 void
2101 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2102 {
2103   const enum tree_code code = TREE_CODE (parm);
2104
2105   /* Brings type template parameters to the canonical forms.  */
2106   if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2107       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2108     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2109
2110   pp_cxx_begin_template_argument_list (pp);
2111   pp_cxx_ws_string (pp, M_("template-parameter-"));
2112   pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2113   pp_minus (pp);
2114   pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2115   pp_cxx_end_template_argument_list (pp);
2116 }
2117
2118 /*
2119   template-declaration:
2120      export(opt) template < template-parameter-list > declaration   */
2121
2122 static void
2123 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2124 {
2125   tree tmpl = most_general_template (t);
2126   tree level;
2127   int i = 0;
2128
2129   pp_maybe_newline_and_indent (pp, 0);
2130   for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2131     {
2132       pp_cxx_ws_string (pp, "template");
2133       pp_cxx_begin_template_argument_list (pp);
2134       pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2135       pp_cxx_end_template_argument_list (pp);
2136       pp_newline_and_indent (pp, 3);
2137       i += 3;
2138     }
2139   if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2140     pp_cxx_function_definition (pp, t);
2141   else
2142     pp_cxx_simple_declaration (pp, t);
2143 }
2144
2145 static void
2146 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2147 {
2148   pp_unsupported_tree (pp, t);
2149 }
2150
2151 static void
2152 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2153 {
2154   pp_unsupported_tree (pp, t);
2155 }
2156
2157 /*
2158     declaration:
2159        block-declaration
2160        function-definition
2161        template-declaration
2162        explicit-instantiation
2163        explicit-specialization
2164        linkage-specification
2165        namespace-definition
2166
2167     block-declaration:
2168        simple-declaration
2169        asm-definition
2170        namespace-alias-definition
2171        using-declaration
2172        using-directive
2173        static_assert-declaration */
2174 void
2175 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
2176 {
2177   if (TREE_CODE (t) == STATIC_ASSERT)
2178     {
2179       pp_cxx_ws_string (pp, "static_assert");
2180       pp_cxx_left_paren (pp);
2181       pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
2182       pp_cxx_separate_with (pp, ',');
2183       pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
2184       pp_cxx_right_paren (pp);
2185     }
2186   else if (!DECL_LANG_SPECIFIC (t))
2187     pp_cxx_simple_declaration (pp, t);
2188   else if (DECL_USE_TEMPLATE (t))
2189     switch (DECL_USE_TEMPLATE (t))
2190       {
2191       case 1:
2192         pp_cxx_template_declaration (pp, t);
2193         break;
2194
2195       case 2:
2196         pp_cxx_explicit_specialization (pp, t);
2197         break;
2198
2199       case 3:
2200         pp_cxx_explicit_instantiation (pp, t);
2201         break;
2202
2203       default:
2204         break;
2205       }
2206   else switch (TREE_CODE (t))
2207     {
2208     case VAR_DECL:
2209     case TYPE_DECL:
2210       pp_cxx_simple_declaration (pp, t);
2211       break;
2212
2213     case FUNCTION_DECL:
2214       if (DECL_SAVED_TREE (t))
2215         pp_cxx_function_definition (pp, t);
2216       else
2217         pp_cxx_simple_declaration (pp, t);
2218       break;
2219
2220     case NAMESPACE_DECL:
2221       if (DECL_NAMESPACE_ALIAS (t))
2222         pp_cxx_namespace_alias_definition (pp, t);
2223       else
2224         pp_cxx_original_namespace_definition (pp, t);
2225       break;
2226
2227     default:
2228       pp_unsupported_tree (pp, t);
2229       break;
2230     }
2231 }
2232
2233 static void
2234 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2235 {
2236   t = TREE_OPERAND (t, 0);
2237   pp_cxx_ws_string (pp, "typeid");
2238   pp_cxx_left_paren (pp);
2239   if (TYPE_P (t))
2240     pp_cxx_type_id (pp, t);
2241   else
2242     pp_cxx_expression (pp, t);
2243   pp_cxx_right_paren (pp);
2244 }
2245
2246 void
2247 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2248 {
2249   pp_cxx_ws_string (pp, "va_arg");
2250   pp_cxx_left_paren (pp);
2251   pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0));
2252   pp_cxx_separate_with (pp, ',');
2253   pp_cxx_type_id (pp, TREE_TYPE (t));
2254   pp_cxx_right_paren (pp);
2255 }
2256
2257 static bool
2258 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2259 {
2260   switch (TREE_CODE (t))
2261     {
2262     case ARROW_EXPR:
2263       if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2264           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2265         {
2266           pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2267           pp_cxx_separate_with (pp, ',');
2268           return true;
2269         }
2270       return false;
2271     case COMPONENT_REF:
2272       if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2273         return false;
2274       if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2275         pp_cxx_dot (pp);
2276       pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2277       return true;
2278     case ARRAY_REF:
2279       if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2280         return false;
2281       pp_left_bracket (pp);
2282       pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2283       pp_right_bracket (pp);
2284       return true;
2285     default:
2286       return false;
2287     }
2288 }
2289
2290 void
2291 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2292 {
2293   pp_cxx_ws_string (pp, "offsetof");
2294   pp_cxx_left_paren (pp);
2295   if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2296     pp_cxx_expression (pp, TREE_OPERAND (t, 0));
2297   pp_cxx_right_paren (pp);
2298 }
2299
2300 void
2301 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2302 {
2303   cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2304
2305   switch (kind)
2306     {
2307     case CPTK_HAS_NOTHROW_ASSIGN:
2308       pp_cxx_ws_string (pp, "__has_nothrow_assign");
2309       break;
2310     case CPTK_HAS_TRIVIAL_ASSIGN:
2311       pp_cxx_ws_string (pp, "__has_trivial_assign");
2312       break;
2313     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2314       pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2315       break;
2316     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2317       pp_cxx_ws_string (pp, "__has_trivial_constructor");
2318       break;
2319     case CPTK_HAS_NOTHROW_COPY:
2320       pp_cxx_ws_string (pp, "__has_nothrow_copy");
2321       break;
2322     case CPTK_HAS_TRIVIAL_COPY:
2323       pp_cxx_ws_string (pp, "__has_trivial_copy");
2324       break;
2325     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2326       pp_cxx_ws_string (pp, "__has_trivial_destructor");
2327       break;
2328     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2329       pp_cxx_ws_string (pp, "__has_virtual_destructor");
2330       break;
2331     case CPTK_IS_ABSTRACT:
2332       pp_cxx_ws_string (pp, "__is_abstract");
2333       break;
2334     case CPTK_IS_BASE_OF:
2335       pp_cxx_ws_string (pp, "__is_base_of");
2336       break;
2337     case CPTK_IS_CLASS:
2338       pp_cxx_ws_string (pp, "__is_class");
2339       break;
2340     case CPTK_IS_CONVERTIBLE_TO:
2341       pp_cxx_ws_string (pp, "__is_convertible_to");
2342       break;
2343     case CPTK_IS_EMPTY:
2344       pp_cxx_ws_string (pp, "__is_empty");
2345       break;
2346     case CPTK_IS_ENUM:
2347       pp_cxx_ws_string (pp, "__is_enum");
2348       break;
2349     case CPTK_IS_POD:
2350       pp_cxx_ws_string (pp, "__is_pod");
2351       break;
2352     case CPTK_IS_POLYMORPHIC:
2353       pp_cxx_ws_string (pp, "__is_polymorphic");
2354       break;
2355     case CPTK_IS_STD_LAYOUT:
2356       pp_cxx_ws_string (pp, "__is_std_layout");
2357       break;
2358     case CPTK_IS_TRIVIAL:
2359       pp_cxx_ws_string (pp, "__is_trivial");
2360       break;
2361     case CPTK_IS_UNION:
2362       pp_cxx_ws_string (pp, "__is_union");
2363       break;
2364
2365     default:
2366       gcc_unreachable ();
2367     }
2368
2369   pp_cxx_left_paren (pp);
2370   pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t));
2371
2372   if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2373     {
2374       pp_cxx_separate_with (pp, ',');
2375       pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t));
2376     }
2377
2378   pp_cxx_right_paren (pp);
2379 }
2380 \f
2381 typedef c_pretty_print_fn pp_fun;
2382
2383 /* Initialization of a C++ pretty-printer object.  */
2384
2385 void
2386 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
2387 {
2388   pp_c_pretty_printer_init (pp_c_base (pp));
2389   pp_set_line_maximum_length (pp, 0);
2390
2391   pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
2392   pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2393   pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
2394   pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2395   pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
2396   pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2397   pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2398   pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
2399   pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2400   pp->c_base.direct_abstract_declarator =
2401     (pp_fun) pp_cxx_direct_abstract_declarator;
2402   pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
2403
2404   /* pp->c_base.statement = (pp_fun) pp_cxx_statement;  */
2405
2406   pp->c_base.constant = (pp_fun) pp_cxx_constant;
2407   pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
2408   pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
2409   pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2410   pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
2411   pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2412   pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2413   pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2414   pp->c_base.expression = (pp_fun) pp_cxx_expression;
2415   pp->enclosing_scope = global_namespace;
2416 }