OSDN Git Service

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