OSDN Git Service

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