Fix cleanup location for try_finally_expr.
[platform/upstream/linaro-gcc.git] / gcc / cp / error.c
1 /* Call-backs for C++ error reporting.
2    This code is non-reentrant.
3    Copyright (C) 1993-2016 Free Software Foundation, Inc.
4    This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "cp-tree.h"
24 #include "stringpool.h"
25 #include "tree-diagnostic.h"
26 #include "langhooks-def.h"
27 #include "intl.h"
28 #include "cxx-pretty-print.h"
29 #include "tree-pretty-print.h"
30 #include "c-family/c-objc.h"
31 #include "ubsan.h"
32 #include "internal-fn.h"
33
34 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
35 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
36
37 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
38    dump C++ ASTs as strings. It is mostly used only by the various
39    tree -> string functions that are occasionally called from the
40    debugger or by the front-end for things like
41    __PRETTY_FUNCTION__.  */
42 static cxx_pretty_printer actual_pretty_printer;
43 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
44
45 /* Translate if being used for diagnostics, but not for dump files or
46    __PRETTY_FUNCTION.  */
47 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
48
49 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
50
51 static const char *args_to_string (tree, int);
52 static const char *assop_to_string (enum tree_code);
53 static const char *code_to_string (enum tree_code);
54 static const char *cv_to_string (tree, int);
55 static const char *decl_to_string (tree, int);
56 static const char *expr_to_string (tree);
57 static const char *fndecl_to_string (tree, int);
58 static const char *op_to_string (enum tree_code);
59 static const char *parm_to_string (int);
60 static const char *type_to_string (tree, int);
61
62 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
63 static void dump_type (cxx_pretty_printer *, tree, int);
64 static void dump_typename (cxx_pretty_printer *, tree, int);
65 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
66 static void dump_decl (cxx_pretty_printer *, tree, int);
67 static void dump_template_decl (cxx_pretty_printer *, tree, int);
68 static void dump_function_decl (cxx_pretty_printer *, tree, int);
69 static void dump_expr (cxx_pretty_printer *, tree, int);
70 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
71 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
72 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
73 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
74 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
75 static void dump_function_name (cxx_pretty_printer *, tree, int);
76 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
77 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
78 static void dump_expr_list (cxx_pretty_printer *, tree, int);
79 static void dump_global_iord (cxx_pretty_printer *, tree);
80 static void dump_parameters (cxx_pretty_printer *, tree, int);
81 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
82 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
83 static void dump_template_argument (cxx_pretty_printer *, tree, int);
84 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
85 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
86 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
87                                     vec<tree, va_gc> *);
88 static void dump_scope (cxx_pretty_printer *, tree, int);
89 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
90 static int get_non_default_template_args_count (tree, int);
91 static const char *function_category (tree);
92 static void maybe_print_constexpr_context (diagnostic_context *);
93 static void maybe_print_instantiation_context (diagnostic_context *);
94 static void print_instantiation_full_context (diagnostic_context *);
95 static void print_instantiation_partial_context (diagnostic_context *,
96                                                  struct tinst_level *,
97                                                  location_t);
98 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
100
101 static bool cp_printer (pretty_printer *, text_info *, const char *,
102                         int, bool, bool, bool);
103
104 /* CONTEXT->printer is a basic pretty printer that was constructed
105    presumably by diagnostic_initialize(), called early in the
106    compiler's initialization process (in general_init) Before the FE
107    is initialized.  This (C++) FE-specific diagnostic initializer is
108    thus replacing the basic pretty printer with one that has C++-aware
109    capacities.  */
110
111 void
112 cxx_initialize_diagnostics (diagnostic_context *context)
113 {
114   pretty_printer *base = context->printer;
115   cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
116   context->printer = new (pp) cxx_pretty_printer ();
117
118   /* It is safe to free this object because it was previously XNEW()'d.  */
119   base->~pretty_printer ();
120   XDELETE (base);
121
122   c_common_diagnostics_set_defaults (context);
123   diagnostic_starter (context) = cp_diagnostic_starter;
124   /* diagnostic_finalizer is already c_diagnostic_finalizer.  */
125   diagnostic_format_decoder (context) = cp_printer;
126 }
127
128 /* Dump a scope, if deemed necessary.  */
129
130 static void
131 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
132 {
133   int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
134
135   if (scope == NULL_TREE)
136     return;
137
138   if (TREE_CODE (scope) == NAMESPACE_DECL)
139     {
140       if (scope != global_namespace)
141         {
142           dump_decl (pp, scope, f);
143           pp_cxx_colon_colon (pp);
144         }
145     }
146   else if (AGGREGATE_TYPE_P (scope))
147     {
148       dump_type (pp, scope, f);
149       pp_cxx_colon_colon (pp);
150     }
151   else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
152     {
153       dump_function_decl (pp, scope, f);
154       pp_cxx_colon_colon (pp);
155     }
156 }
157
158 /* Dump the template ARGument under control of FLAGS.  */
159
160 static void
161 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
162 {
163   if (ARGUMENT_PACK_P (arg))
164     dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
165                                  /* No default args in argument packs.  */
166                                  flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
167   else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
168     dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
169   else
170     {
171       if (TREE_CODE (arg) == TREE_LIST)
172         arg = TREE_VALUE (arg);
173
174       dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
175     }
176 }
177
178 /* Count the number of template arguments ARGS whose value does not
179    match the (optional) default template parameter in PARAMS  */
180
181 static int
182 get_non_default_template_args_count (tree args, int flags)
183 {
184   int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
185
186   if (/* We use this flag when generating debug information.  We don't
187          want to expand templates at this point, for this may generate
188          new decls, which gets decl counts out of sync, which may in
189          turn cause codegen differences between compilations with and
190          without -g.  */
191       (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
192       || !flag_pretty_templates)
193     return n;
194
195   return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
196 }
197
198 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
199    of FLAGS.  */
200
201 static void
202 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
203 {
204   int n = get_non_default_template_args_count (args, flags);
205   int need_comma = 0;
206   int i;
207
208   for (i = 0; i < n; ++i)
209     {
210       tree arg = TREE_VEC_ELT (args, i);
211
212       /* Only print a comma if we know there is an argument coming. In
213          the case of an empty template argument pack, no actual
214          argument will be printed.  */
215       if (need_comma
216           && (!ARGUMENT_PACK_P (arg)
217               || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
218         pp_separate_with_comma (pp);
219
220       dump_template_argument (pp, arg, flags);
221       need_comma = 1;
222     }
223 }
224
225 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS.  */
226
227 static void
228 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
229 {
230   tree p;
231   tree a;
232
233   if (parm == error_mark_node)
234    return;
235
236   p = TREE_VALUE (parm);
237   a = TREE_PURPOSE (parm);
238
239   if (TREE_CODE (p) == TYPE_DECL)
240     {
241       if (flags & TFF_DECL_SPECIFIERS)
242         {
243           pp_cxx_ws_string (pp, "class");
244           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
245             pp_cxx_ws_string (pp, "...");
246           if (DECL_NAME (p))
247             pp_cxx_tree_identifier (pp, DECL_NAME (p));
248         }
249       else if (DECL_NAME (p))
250         pp_cxx_tree_identifier (pp, DECL_NAME (p));
251       else
252         pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
253     }
254   else
255     dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
256
257   if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
258     {
259       pp_cxx_whitespace (pp);
260       pp_equal (pp);
261       pp_cxx_whitespace (pp);
262       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
263         dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
264       else
265         dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
266     }
267 }
268
269 /* Dump, under control of FLAGS, a template-parameter-list binding.
270    PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
271    TREE_VEC.  */
272
273 static void
274 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
275                         vec<tree, va_gc> *typenames)
276 {
277   bool need_semicolon = false;
278   int i;
279   tree t;
280
281   while (parms)
282     {
283       tree p = TREE_VALUE (parms);
284       int lvl = TMPL_PARMS_DEPTH (parms);
285       int arg_idx = 0;
286       int i;
287       tree lvl_args = NULL_TREE;
288
289       /* Don't crash if we had an invalid argument list.  */
290       if (TMPL_ARGS_DEPTH (args) >= lvl)
291         lvl_args = TMPL_ARGS_LEVEL (args, lvl);
292
293       for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
294         {
295           tree arg = NULL_TREE;
296
297           /* Don't crash if we had an invalid argument list.  */
298           if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
299             arg = TREE_VEC_ELT (lvl_args, arg_idx);
300
301           if (need_semicolon)
302             pp_separate_with_semicolon (pp);
303           dump_template_parameter (pp, TREE_VEC_ELT (p, i),
304                                    TFF_PLAIN_IDENTIFIER);
305           pp_cxx_whitespace (pp);
306           pp_equal (pp);
307           pp_cxx_whitespace (pp);
308           if (arg)
309             {
310               if (ARGUMENT_PACK_P (arg))
311                 pp_cxx_left_brace (pp);
312               dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
313               if (ARGUMENT_PACK_P (arg))
314                 pp_cxx_right_brace (pp);
315             }
316           else
317             pp_string (pp, M_("<missing>"));
318
319           ++arg_idx;
320           need_semicolon = true;
321         }
322
323       parms = TREE_CHAIN (parms);
324     }
325
326   /* Don't bother with typenames for a partial instantiation.  */
327   if (vec_safe_is_empty (typenames) || uses_template_parms (args))
328     return;
329
330   /* Don't try to print typenames when we're processing a clone.  */
331   if (current_function_decl
332       && !DECL_LANG_SPECIFIC (current_function_decl))
333     return;
334
335   /* Don't try to do this once cgraph starts throwing away front-end
336      information.  */
337   if (at_eof >= 2)
338     return;
339
340   FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
341     {
342       if (need_semicolon)
343         pp_separate_with_semicolon (pp);
344       dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
345       pp_cxx_whitespace (pp);
346       pp_equal (pp);
347       pp_cxx_whitespace (pp);
348       push_deferring_access_checks (dk_no_check);
349       t = tsubst (t, args, tf_none, NULL_TREE);
350       pop_deferring_access_checks ();
351       /* Strip typedefs.  We can't just use TFF_CHASE_TYPEDEF because
352          pp_simple_type_specifier doesn't know about it.  */
353       t = strip_typedefs (t);
354       dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
355     }
356 }
357
358 /* Dump a human-readable equivalent of the alias template
359    specialization of T.  */
360
361 static void
362 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
363 {
364   tree name;
365
366   gcc_assert (alias_template_specialization_p (t));
367
368   if (!(flags & TFF_UNQUALIFIED_NAME))
369     dump_scope (pp, CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
370   name = TYPE_IDENTIFIER (t);
371   pp_cxx_tree_identifier (pp, name);
372   dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
373                        /*primary=*/false,
374                        flags & ~TFF_TEMPLATE_HEADER);
375 }
376
377 /* Dump a human-readable equivalent of TYPE.  FLAGS controls the
378    format.  */
379
380 static void
381 dump_type (cxx_pretty_printer *pp, tree t, int flags)
382 {
383   if (t == NULL_TREE)
384     return;
385
386   /* Don't print e.g. "struct mytypedef".  */
387   if (TYPE_P (t) && typedef_variant_p (t))
388     {
389       tree decl = TYPE_NAME (t);
390       if ((flags & TFF_CHASE_TYPEDEF)
391                || DECL_SELF_REFERENCE_P (decl)
392                || (!flag_pretty_templates
393                    && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
394         t = strip_typedefs (t);
395       else if (alias_template_specialization_p (t))
396         {
397           dump_alias_template_specialization (pp, t, flags);
398           return;
399         }
400       else if (same_type_p (t, TREE_TYPE (decl)))
401         t = decl;
402       else
403         {
404           pp_cxx_cv_qualifier_seq (pp, t);
405           pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
406           return;
407         }
408     }
409
410   if (TYPE_PTRMEMFUNC_P (t))
411     goto offset_type;
412
413   switch (TREE_CODE (t))
414     {
415     case LANG_TYPE:
416       if (t == init_list_type_node)
417         pp_string (pp, M_("<brace-enclosed initializer list>"));
418       else if (t == unknown_type_node)
419         pp_string (pp, M_("<unresolved overloaded function type>"));
420       else
421         {
422           pp_cxx_cv_qualifier_seq (pp, t);
423           pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
424         }
425       break;
426
427     case TREE_LIST:
428       /* A list of function parms.  */
429       dump_parameters (pp, t, flags);
430       break;
431
432     case IDENTIFIER_NODE:
433       pp_cxx_tree_identifier (pp, t);
434       break;
435
436     case TREE_BINFO:
437       dump_type (pp, BINFO_TYPE (t), flags);
438       break;
439
440     case RECORD_TYPE:
441     case UNION_TYPE:
442     case ENUMERAL_TYPE:
443       dump_aggr_type (pp, t, flags);
444       break;
445
446     case TYPE_DECL:
447       if (flags & TFF_CHASE_TYPEDEF)
448         {
449           dump_type (pp, DECL_ORIGINAL_TYPE (t)
450                      ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
451           break;
452         }
453       /* Else fall through.  */
454
455     case TEMPLATE_DECL:
456     case NAMESPACE_DECL:
457       dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
458       break;
459
460     case INTEGER_TYPE:
461     case REAL_TYPE:
462     case VOID_TYPE:
463     case BOOLEAN_TYPE:
464     case COMPLEX_TYPE:
465     case VECTOR_TYPE:
466     case FIXED_POINT_TYPE:
467       pp_type_specifier_seq (pp, t);
468       break;
469
470     case TEMPLATE_TEMPLATE_PARM:
471       /* For parameters inside template signature.  */
472       if (TYPE_IDENTIFIER (t))
473         pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
474       else
475         pp_cxx_canonical_template_parameter (pp, t);
476       break;
477
478     case BOUND_TEMPLATE_TEMPLATE_PARM:
479       {
480         tree args = TYPE_TI_ARGS (t);
481         pp_cxx_cv_qualifier_seq (pp, t);
482         pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
483         pp_cxx_begin_template_argument_list (pp);
484         dump_template_argument_list (pp, args, flags);
485         pp_cxx_end_template_argument_list (pp);
486       }
487       break;
488
489     case TEMPLATE_TYPE_PARM:
490       pp_cxx_cv_qualifier_seq (pp, t);
491       if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
492         pp_cxx_constrained_type_spec (pp, c);
493       else if (TYPE_IDENTIFIER (t))
494         pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
495       else
496         pp_cxx_canonical_template_parameter
497           (pp, TEMPLATE_TYPE_PARM_INDEX (t));
498       break;
499
500       /* This is not always necessary for pointers and such, but doing this
501          reduces code size.  */
502     case ARRAY_TYPE:
503     case POINTER_TYPE:
504     case REFERENCE_TYPE:
505     case OFFSET_TYPE:
506     offset_type:
507     case FUNCTION_TYPE:
508     case METHOD_TYPE:
509     {
510       dump_type_prefix (pp, t, flags);
511       dump_type_suffix (pp, t, flags);
512       break;
513     }
514     case TYPENAME_TYPE:
515       if (! (flags & TFF_CHASE_TYPEDEF)
516           && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
517         {
518           dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
519           break;
520         }
521       pp_cxx_cv_qualifier_seq (pp, t);
522       pp_cxx_ws_string (pp,
523                          TYPENAME_IS_ENUM_P (t) ? "enum"
524                          : TYPENAME_IS_CLASS_P (t) ? "class"
525                          : "typename");
526       dump_typename (pp, t, flags);
527       break;
528
529     case UNBOUND_CLASS_TEMPLATE:
530       if (! (flags & TFF_UNQUALIFIED_NAME))
531         {
532           dump_type (pp, TYPE_CONTEXT (t), flags);
533           pp_cxx_colon_colon (pp);
534         }
535       pp_cxx_ws_string (pp, "template");
536       dump_type (pp, TYPE_IDENTIFIER (t), flags);
537       break;
538
539     case TYPEOF_TYPE:
540       pp_cxx_ws_string (pp, "__typeof__");
541       pp_cxx_whitespace (pp);
542       pp_cxx_left_paren (pp);
543       dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
544       pp_cxx_right_paren (pp);
545       break;
546
547     case UNDERLYING_TYPE:
548       pp_cxx_ws_string (pp, "__underlying_type");
549       pp_cxx_whitespace (pp);
550       pp_cxx_left_paren (pp);
551       dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
552       pp_cxx_right_paren (pp);
553       break;
554
555     case TYPE_PACK_EXPANSION:
556       dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
557       pp_cxx_ws_string (pp, "...");
558       break;
559
560     case TYPE_ARGUMENT_PACK:
561       dump_template_argument (pp, t, flags);
562       break;
563
564     case DECLTYPE_TYPE:
565       pp_cxx_ws_string (pp, "decltype");
566       pp_cxx_whitespace (pp);
567       pp_cxx_left_paren (pp);
568       dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
569       pp_cxx_right_paren (pp);
570       break;
571
572     case NULLPTR_TYPE:
573       pp_string (pp, "std::nullptr_t");
574       break;
575
576     default:
577       pp_unsupported_tree (pp, t);
578       /* Fall through to error.  */
579
580     case ERROR_MARK:
581       pp_string (pp, M_("<type error>"));
582       break;
583     }
584 }
585
586 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
587    a TYPENAME_TYPE.  */
588
589 static void
590 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
591 {
592   tree ctx = TYPE_CONTEXT (t);
593
594   if (TREE_CODE (ctx) == TYPENAME_TYPE)
595     dump_typename (pp, ctx, flags);
596   else
597     dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
598   pp_cxx_colon_colon (pp);
599   dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
600 }
601
602 /* Return the name of the supplied aggregate, or enumeral type.  */
603
604 const char *
605 class_key_or_enum_as_string (tree t)
606 {
607   if (TREE_CODE (t) == ENUMERAL_TYPE) 
608     {
609       if (SCOPED_ENUM_P (t))
610         return "enum class";
611       else
612         return "enum";
613     }
614   else if (TREE_CODE (t) == UNION_TYPE)
615     return "union";
616   else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
617     return "class";
618   else
619     return "struct";
620 }
621
622 /* Print out a class declaration T under the control of FLAGS,
623    in the form `class foo'.  */
624
625 static void
626 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
627 {
628   tree name;
629   const char *variety = class_key_or_enum_as_string (t);
630   int typdef = 0;
631   int tmplate = 0;
632
633   pp_cxx_cv_qualifier_seq (pp, t);
634
635   if (flags & TFF_CLASS_KEY_OR_ENUM)
636     pp_cxx_ws_string (pp, variety);
637
638   name = TYPE_NAME (t);
639
640   if (name)
641     {
642       typdef = (!DECL_ARTIFICIAL (name)
643                 /* An alias specialization is not considered to be a
644                    typedef.  */
645                 && !alias_template_specialization_p (t));
646
647       if ((typdef
648            && ((flags & TFF_CHASE_TYPEDEF)
649                || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
650                    && DECL_TEMPLATE_INFO (name))))
651           || DECL_SELF_REFERENCE_P (name))
652         {
653           t = TYPE_MAIN_VARIANT (t);
654           name = TYPE_NAME (t);
655           typdef = 0;
656         }
657
658       tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
659                 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
660                 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
661                     || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
662       
663       if (! (flags & TFF_UNQUALIFIED_NAME))
664         dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
665       flags &= ~TFF_UNQUALIFIED_NAME;
666       if (tmplate)
667         {
668           /* Because the template names are mangled, we have to locate
669              the most general template, and use that name.  */
670           tree tpl = TYPE_TI_TEMPLATE (t);
671
672           while (DECL_TEMPLATE_INFO (tpl))
673             tpl = DECL_TI_TEMPLATE (tpl);
674           name = tpl;
675         }
676       name = DECL_NAME (name);
677     }
678
679   if (name == 0 || anon_aggrname_p (name))
680     {
681       if (flags & TFF_CLASS_KEY_OR_ENUM)
682         pp_string (pp, M_("<anonymous>"));
683       else
684         pp_printf (pp, M_("<anonymous %s>"), variety);
685     }
686   else if (LAMBDA_TYPE_P (t))
687     {
688       /* A lambda's "type" is essentially its signature.  */
689       pp_string (pp, M_("<lambda"));
690       if (lambda_function (t))
691         dump_parameters (pp,
692                          FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
693                          flags);
694       pp_greater (pp);
695     }
696   else
697     pp_cxx_tree_identifier (pp, name);
698   if (tmplate)
699     dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
700                          !CLASSTYPE_USE_TEMPLATE (t),
701                          flags & ~TFF_TEMPLATE_HEADER);
702 }
703
704 /* Dump into the obstack the initial part of the output for a given type.
705    This is necessary when dealing with things like functions returning
706    functions.  Examples:
707
708    return type of `int (* fee ())()': pointer -> function -> int.  Both
709    pointer (and reference and offset) and function (and member) types must
710    deal with prefix and suffix.
711
712    Arrays must also do this for DECL nodes, like int a[], and for things like
713    int *[]&.  */
714
715 static void
716 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
717 {
718   if (TYPE_PTRMEMFUNC_P (t))
719     {
720       t = TYPE_PTRMEMFUNC_FN_TYPE (t);
721       goto offset_type;
722     }
723
724   switch (TREE_CODE (t))
725     {
726     case POINTER_TYPE:
727     case REFERENCE_TYPE:
728       {
729         tree sub = TREE_TYPE (t);
730
731         dump_type_prefix (pp, sub, flags);
732         if (TREE_CODE (sub) == ARRAY_TYPE
733             || TREE_CODE (sub) == FUNCTION_TYPE)
734           {
735             pp_cxx_whitespace (pp);
736             pp_cxx_left_paren (pp);
737             pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
738           }
739         if (TYPE_PTR_P (t))
740           pp_star (pp);
741         else if (TREE_CODE (t) == REFERENCE_TYPE)
742         {
743           if (TYPE_REF_IS_RVALUE (t))
744             pp_ampersand_ampersand (pp);
745           else
746             pp_ampersand (pp);
747         }
748         pp->padding = pp_before;
749         pp_cxx_cv_qualifier_seq (pp, t);
750       }
751       break;
752
753     case OFFSET_TYPE:
754     offset_type:
755       dump_type_prefix (pp, TREE_TYPE (t), flags);
756       if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
757         {
758           pp_maybe_space (pp);
759           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
760              pp_cxx_left_paren (pp);
761           dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
762           pp_cxx_colon_colon (pp);
763         }
764       pp_cxx_star (pp);
765       pp_cxx_cv_qualifier_seq (pp, t);
766       pp->padding = pp_before;
767       break;
768
769       /* This can be reached without a pointer when dealing with
770          templates, e.g. std::is_function.  */
771     case FUNCTION_TYPE:
772       dump_type_prefix (pp, TREE_TYPE (t), flags);
773       break;
774
775     case METHOD_TYPE:
776       dump_type_prefix (pp, TREE_TYPE (t), flags);
777       pp_maybe_space (pp);
778       pp_cxx_left_paren (pp);
779       dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
780       pp_cxx_colon_colon (pp);
781       break;
782
783     case ARRAY_TYPE:
784       dump_type_prefix (pp, TREE_TYPE (t), flags);
785       break;
786
787     case ENUMERAL_TYPE:
788     case IDENTIFIER_NODE:
789     case INTEGER_TYPE:
790     case BOOLEAN_TYPE:
791     case REAL_TYPE:
792     case RECORD_TYPE:
793     case TEMPLATE_TYPE_PARM:
794     case TEMPLATE_TEMPLATE_PARM:
795     case BOUND_TEMPLATE_TEMPLATE_PARM:
796     case TREE_LIST:
797     case TYPE_DECL:
798     case TREE_VEC:
799     case UNION_TYPE:
800     case LANG_TYPE:
801     case VOID_TYPE:
802     case TYPENAME_TYPE:
803     case COMPLEX_TYPE:
804     case VECTOR_TYPE:
805     case TYPEOF_TYPE:
806     case UNDERLYING_TYPE:
807     case DECLTYPE_TYPE:
808     case TYPE_PACK_EXPANSION:
809     case FIXED_POINT_TYPE:
810     case NULLPTR_TYPE:
811       dump_type (pp, t, flags);
812       pp->padding = pp_before;
813       break;
814
815     default:
816       pp_unsupported_tree (pp, t);
817       /* fall through.  */
818     case ERROR_MARK:
819       pp_string (pp, M_("<typeprefixerror>"));
820       break;
821     }
822 }
823
824 /* Dump the suffix of type T, under control of FLAGS.  This is the part
825    which appears after the identifier (or function parms).  */
826
827 static void
828 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
829 {
830   if (TYPE_PTRMEMFUNC_P (t))
831     t = TYPE_PTRMEMFUNC_FN_TYPE (t);
832
833   switch (TREE_CODE (t))
834     {
835     case POINTER_TYPE:
836     case REFERENCE_TYPE:
837     case OFFSET_TYPE:
838       if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
839           || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
840         pp_cxx_right_paren (pp);
841       if (TREE_CODE (t) == POINTER_TYPE)
842         flags |= TFF_POINTER;
843       dump_type_suffix (pp, TREE_TYPE (t), flags);
844       break;
845
846     case FUNCTION_TYPE:
847     case METHOD_TYPE:
848       {
849         tree arg;
850         if (TREE_CODE (t) == METHOD_TYPE)
851           /* Can only be reached through a pointer.  */
852           pp_cxx_right_paren (pp);
853         arg = TYPE_ARG_TYPES (t);
854         if (TREE_CODE (t) == METHOD_TYPE)
855           arg = TREE_CHAIN (arg);
856
857         /* Function pointers don't have default args.  Not in standard C++,
858            anyway; they may in g++, but we'll just pretend otherwise.  */
859         dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
860
861         pp->padding = pp_before;
862         pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
863                               TREE_CODE (t) == FUNCTION_TYPE
864                               && (flags & TFF_POINTER));
865         dump_ref_qualifier (pp, t, flags);
866         if (tx_safe_fn_type_p (t))
867           pp_cxx_ws_string (pp, "transaction_safe");
868         dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
869         dump_type_suffix (pp, TREE_TYPE (t), flags);
870         break;
871       }
872
873     case ARRAY_TYPE:
874       pp_maybe_space (pp);
875       pp_cxx_left_bracket (pp);
876       if (tree dtype = TYPE_DOMAIN (t))
877         {
878           tree max = TYPE_MAX_VALUE (dtype);
879           /* Zero-length arrays have an upper bound of SIZE_MAX.  */
880           if (integer_all_onesp (max))
881             pp_character (pp, '0');
882           else if (tree_fits_shwi_p (max))
883             pp_wide_integer (pp, tree_to_shwi (max) + 1);
884           else
885             {
886               STRIP_NOPS (max);
887               if (TREE_CODE (max) == SAVE_EXPR)
888                 max = TREE_OPERAND (max, 0);
889               if (TREE_CODE (max) == MINUS_EXPR
890                   || TREE_CODE (max) == PLUS_EXPR)
891                 {
892                   max = TREE_OPERAND (max, 0);
893                   while (CONVERT_EXPR_P (max))
894                     max = TREE_OPERAND (max, 0);
895                 }
896               else
897                 max = fold_build2_loc (input_location,
898                                        PLUS_EXPR, dtype, max,
899                                        build_int_cst (dtype, 1));
900               dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
901             }
902         }
903       pp_cxx_right_bracket (pp);
904       dump_type_suffix (pp, TREE_TYPE (t), flags);
905       break;
906
907     case ENUMERAL_TYPE:
908     case IDENTIFIER_NODE:
909     case INTEGER_TYPE:
910     case BOOLEAN_TYPE:
911     case REAL_TYPE:
912     case RECORD_TYPE:
913     case TEMPLATE_TYPE_PARM:
914     case TEMPLATE_TEMPLATE_PARM:
915     case BOUND_TEMPLATE_TEMPLATE_PARM:
916     case TREE_LIST:
917     case TYPE_DECL:
918     case TREE_VEC:
919     case UNION_TYPE:
920     case LANG_TYPE:
921     case VOID_TYPE:
922     case TYPENAME_TYPE:
923     case COMPLEX_TYPE:
924     case VECTOR_TYPE:
925     case TYPEOF_TYPE:
926     case UNDERLYING_TYPE:
927     case DECLTYPE_TYPE:
928     case TYPE_PACK_EXPANSION:
929     case FIXED_POINT_TYPE:
930     case NULLPTR_TYPE:
931       break;
932
933     default:
934       pp_unsupported_tree (pp, t);
935     case ERROR_MARK:
936       /* Don't mark it here, we should have already done in
937          dump_type_prefix.  */
938       break;
939     }
940 }
941
942 static void
943 dump_global_iord (cxx_pretty_printer *pp, tree t)
944 {
945   const char *p = NULL;
946
947   if (DECL_GLOBAL_CTOR_P (t))
948     p = M_("(static initializers for %s)");
949   else if (DECL_GLOBAL_DTOR_P (t))
950     p = M_("(static destructors for %s)");
951   else
952     gcc_unreachable ();
953
954   pp_printf (pp, p, DECL_SOURCE_FILE (t));
955 }
956
957 static void
958 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
959 {
960   if (flags & TFF_DECL_SPECIFIERS)
961     {
962       if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
963         {
964           if (DECL_LANG_SPECIFIC (t) && DECL_DECLARED_CONCEPT_P (t))
965             pp_cxx_ws_string (pp, "concept");
966           else
967             pp_cxx_ws_string (pp, "constexpr");
968         }
969       dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
970       pp_maybe_space (pp);
971     }
972   if (! (flags & TFF_UNQUALIFIED_NAME)
973       && TREE_CODE (t) != PARM_DECL
974       && (!DECL_INITIAL (t)
975           || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
976     dump_scope (pp, CP_DECL_CONTEXT (t), flags);
977   flags &= ~TFF_UNQUALIFIED_NAME;
978   if ((flags & TFF_DECL_SPECIFIERS)
979       && DECL_TEMPLATE_PARM_P (t) 
980       && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
981     pp_string (pp, "...");
982   if (DECL_NAME (t))
983     {
984       if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
985         {
986           pp_less (pp);
987           pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
988           pp_string (pp, " capture>");
989         }
990       else
991         dump_decl (pp, DECL_NAME (t), flags);
992     }
993   else
994     pp_string (pp, M_("<anonymous>"));
995   if (flags & TFF_DECL_SPECIFIERS)
996     dump_type_suffix (pp, type, flags);
997 }
998
999 /* Dump a human readable string for the decl T under control of FLAGS.  */
1000
1001 static void
1002 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1003 {
1004   if (t == NULL_TREE)
1005     return;
1006
1007   /* If doing Objective-C++, give Objective-C a chance to demangle
1008      Objective-C method names.  */
1009   if (c_dialect_objc ())
1010     {
1011       const char *demangled = objc_maybe_printable_name (t, flags);
1012       if (demangled)
1013         {
1014           pp_string (pp, demangled);
1015           return;
1016         }
1017     }
1018
1019   switch (TREE_CODE (t))
1020     {
1021     case TYPE_DECL:
1022       /* Don't say 'typedef class A' */
1023       if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1024         {
1025           if ((flags & TFF_DECL_SPECIFIERS)
1026               && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1027             {
1028               /* Say `class T' not just `T'.  */
1029               pp_cxx_ws_string (pp, "class");
1030
1031               /* Emit the `...' for a parameter pack.  */
1032               if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1033                 pp_cxx_ws_string (pp, "...");
1034             }
1035
1036           dump_type (pp, TREE_TYPE (t), flags);
1037           break;
1038         }
1039       if (TYPE_DECL_ALIAS_P (t)
1040           && (flags & TFF_DECL_SPECIFIERS
1041               || flags & TFF_CLASS_KEY_OR_ENUM))
1042         {
1043           pp_cxx_ws_string (pp, "using");
1044           dump_decl (pp, DECL_NAME (t), flags);
1045           pp_cxx_whitespace (pp);
1046           pp_cxx_ws_string (pp, "=");
1047           pp_cxx_whitespace (pp);
1048           dump_type (pp, DECL_ORIGINAL_TYPE (t), flags);
1049           break;
1050         }
1051       if ((flags & TFF_DECL_SPECIFIERS)
1052           && !DECL_SELF_REFERENCE_P (t))
1053         pp_cxx_ws_string (pp, "typedef");
1054       dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1055                         ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1056                         flags);
1057       break;
1058
1059     case VAR_DECL:
1060       if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1061         {
1062           pp_string (pp, M_("vtable for "));
1063           gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1064           dump_type (pp, DECL_CONTEXT (t), flags);
1065           break;
1066         }
1067       /* Else fall through.  */
1068     case FIELD_DECL:
1069     case PARM_DECL:
1070       dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1071
1072       /* Handle variable template specializations.  */
1073       if (VAR_P (t)
1074           && DECL_LANG_SPECIFIC (t)
1075           && DECL_TEMPLATE_INFO (t)
1076           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1077         {
1078           pp_cxx_begin_template_argument_list (pp);
1079           tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1080           dump_template_argument_list (pp, args, flags);
1081           pp_cxx_end_template_argument_list (pp);
1082         }
1083       break;
1084
1085     case RESULT_DECL:
1086       pp_string (pp, M_("<return value> "));
1087       dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1088       break;
1089
1090     case NAMESPACE_DECL:
1091       if (flags & TFF_DECL_SPECIFIERS)
1092         pp->declaration (t);
1093       else
1094         {
1095           if (! (flags & TFF_UNQUALIFIED_NAME))
1096             dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1097           flags &= ~TFF_UNQUALIFIED_NAME;
1098           if (DECL_NAME (t) == NULL_TREE)
1099             {
1100               if (!(pp->flags & pp_c_flag_gnu_v3))
1101                 pp_cxx_ws_string (pp, M_("{anonymous}"));
1102               else
1103                 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1104             }
1105           else
1106             pp_cxx_tree_identifier (pp, DECL_NAME (t));
1107         }
1108       break;
1109
1110     case SCOPE_REF:
1111       dump_type (pp, TREE_OPERAND (t, 0), flags);
1112       pp_colon_colon (pp);
1113       dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1114       break;
1115
1116     case ARRAY_REF:
1117       dump_decl (pp, TREE_OPERAND (t, 0), flags);
1118       pp_cxx_left_bracket (pp);
1119       dump_decl (pp, TREE_OPERAND (t, 1), flags);
1120       pp_cxx_right_bracket (pp);
1121       break;
1122
1123     case ARRAY_NOTATION_REF:
1124       dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1125       pp_cxx_left_bracket (pp);
1126       dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1127       pp_colon (pp);
1128       dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1129       pp_colon (pp);
1130       dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1131       pp_cxx_right_bracket (pp);
1132       break;
1133
1134       /* So that we can do dump_decl on an aggr type.  */
1135     case RECORD_TYPE:
1136     case UNION_TYPE:
1137     case ENUMERAL_TYPE:
1138       dump_type (pp, t, flags);
1139       break;
1140
1141     case BIT_NOT_EXPR:
1142       /* This is a pseudo destructor call which has not been folded into
1143          a PSEUDO_DTOR_EXPR yet.  */
1144       pp_cxx_complement (pp);
1145       dump_type (pp, TREE_OPERAND (t, 0), flags);
1146       break;
1147
1148     case TYPE_EXPR:
1149       gcc_unreachable ();
1150       break;
1151
1152       /* These special cases are duplicated here so that other functions
1153          can feed identifiers to error and get them demangled properly.  */
1154     case IDENTIFIER_NODE:
1155       if (IDENTIFIER_TYPENAME_P (t))
1156         {
1157           pp_cxx_ws_string (pp, "operator");
1158           /* Not exactly IDENTIFIER_TYPE_VALUE.  */
1159           dump_type (pp, TREE_TYPE (t), flags);
1160           break;
1161         }
1162       else
1163         pp_cxx_tree_identifier (pp, t);
1164       break;
1165
1166     case OVERLOAD:
1167       if (OVL_CHAIN (t))
1168         {
1169           t = OVL_CURRENT (t);
1170           if (DECL_CLASS_SCOPE_P (t))
1171             {
1172               dump_type (pp, DECL_CONTEXT (t), flags);
1173               pp_cxx_colon_colon (pp);
1174             }
1175           else if (!DECL_FILE_SCOPE_P (t))
1176             {
1177               dump_decl (pp, DECL_CONTEXT (t), flags);
1178               pp_cxx_colon_colon (pp);
1179             }
1180           dump_decl (pp, DECL_NAME (t), flags);
1181           break;
1182         }
1183
1184       /* If there's only one function, just treat it like an ordinary
1185          FUNCTION_DECL.  */
1186       t = OVL_CURRENT (t);
1187       /* Fall through.  */
1188
1189     case FUNCTION_DECL:
1190       if (! DECL_LANG_SPECIFIC (t))
1191         {
1192           if (DECL_ABSTRACT_ORIGIN (t))
1193             dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1194           else
1195             pp_string (pp, M_("<built-in>"));
1196         }
1197       else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1198         dump_global_iord (pp, t);
1199       else
1200         dump_function_decl (pp, t, flags);
1201       break;
1202
1203     case TEMPLATE_DECL:
1204       dump_template_decl (pp, t, flags);
1205       break;
1206
1207     case TEMPLATE_ID_EXPR:
1208       {
1209         tree name = TREE_OPERAND (t, 0);
1210         tree args = TREE_OPERAND (t, 1);
1211
1212         if (is_overloaded_fn (name))
1213           name = get_first_fn (name);
1214         if (DECL_P (name))
1215           name = DECL_NAME (name);
1216         dump_decl (pp, name, flags);
1217         pp_cxx_begin_template_argument_list (pp);
1218         if (args == error_mark_node)
1219           pp_string (pp, M_("<template arguments error>"));
1220         else if (args)
1221           dump_template_argument_list
1222             (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1223         pp_cxx_end_template_argument_list (pp);
1224       }
1225       break;
1226
1227     case LABEL_DECL:
1228       pp_cxx_tree_identifier (pp, DECL_NAME (t));
1229       break;
1230
1231     case CONST_DECL:
1232       if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1233           || (DECL_INITIAL (t) &&
1234               TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1235         dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1236       else if (DECL_NAME (t))
1237         dump_decl (pp, DECL_NAME (t), flags);
1238       else if (DECL_INITIAL (t))
1239         dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1240       else
1241         pp_string (pp, M_("<enumerator>"));
1242       break;
1243
1244     case USING_DECL:
1245       pp_cxx_ws_string (pp, "using");
1246       dump_type (pp, USING_DECL_SCOPE (t), flags);
1247       pp_cxx_colon_colon (pp);
1248       dump_decl (pp, DECL_NAME (t), flags);
1249       break;
1250
1251     case STATIC_ASSERT:
1252       pp->declaration (t);
1253       break;
1254
1255     case BASELINK:
1256       dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1257       break;
1258
1259     case NON_DEPENDENT_EXPR:
1260       dump_expr (pp, t, flags);
1261       break;
1262
1263     case TEMPLATE_TYPE_PARM:
1264       if (flags & TFF_DECL_SPECIFIERS)
1265         pp->declaration (t);
1266       else
1267         pp->type_id (t);
1268       break;
1269
1270     case UNBOUND_CLASS_TEMPLATE:
1271     case TYPE_PACK_EXPANSION:
1272     case TREE_BINFO:
1273       dump_type (pp, t, flags);
1274       break;
1275
1276     default:
1277       pp_unsupported_tree (pp, t);
1278       /* Fall through to error.  */
1279
1280     case ERROR_MARK:
1281       pp_string (pp, M_("<declaration error>"));
1282       break;
1283     }
1284 }
1285
1286 /* Dump a template declaration T under control of FLAGS. This means the
1287    'template <...> leaders plus the 'class X' or 'void fn(...)' part.  */
1288
1289 static void
1290 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1291 {
1292   tree orig_parms = DECL_TEMPLATE_PARMS (t);
1293   tree parms;
1294   int i;
1295
1296   if (flags & TFF_TEMPLATE_HEADER)
1297     {
1298       for (parms = orig_parms = nreverse (orig_parms);
1299            parms;
1300            parms = TREE_CHAIN (parms))
1301         {
1302           tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1303           int len = TREE_VEC_LENGTH (inner_parms);
1304
1305           if (len == 0)
1306             {
1307               /* Skip over the dummy template levels of a template template
1308                  parm.  */
1309               gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1310               continue;
1311             }
1312
1313           pp_cxx_ws_string (pp, "template");
1314           pp_cxx_begin_template_argument_list (pp);
1315
1316           /* If we've shown the template prefix, we'd better show the
1317              parameters' and decl's type too.  */
1318             flags |= TFF_DECL_SPECIFIERS;
1319
1320           for (i = 0; i < len; i++)
1321             {
1322               if (i)
1323                 pp_separate_with_comma (pp);
1324               dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1325                                        flags);
1326             }
1327           pp_cxx_end_template_argument_list (pp);
1328           pp_cxx_whitespace (pp);
1329         }
1330       nreverse(orig_parms);
1331
1332       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1333         {
1334           /* Say `template<arg> class TT' not just `template<arg> TT'.  */
1335           pp_cxx_ws_string (pp, "class");
1336
1337           /* If this is a parameter pack, print the ellipsis.  */
1338           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1339             pp_cxx_ws_string (pp, "...");
1340         }
1341
1342       /* Only print the requirements if we're also printing
1343          the template header.  */
1344       if (flag_concepts)
1345         if (tree ci = get_constraints (t))
1346           if (check_constraint_info (ci))
1347             if (tree reqs = CI_TEMPLATE_REQS (ci))
1348               {
1349                 pp_cxx_requires_clause (pp, reqs);
1350                 pp_cxx_whitespace (pp);
1351               }
1352     }
1353
1354
1355   if (DECL_CLASS_TEMPLATE_P (t))
1356     dump_type (pp, TREE_TYPE (t),
1357                ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1358                 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1359   else if (DECL_TEMPLATE_RESULT (t)
1360            && (VAR_P (DECL_TEMPLATE_RESULT (t))
1361                /* Alias template.  */
1362                || DECL_TYPE_TEMPLATE_P (t)))
1363     dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1364   else
1365     {
1366       gcc_assert (TREE_TYPE (t));
1367       switch (NEXT_CODE (t))
1368         {
1369         case METHOD_TYPE:
1370         case FUNCTION_TYPE:
1371           dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1372           break;
1373         default:
1374           /* This case can occur with some invalid code.  */
1375           dump_type (pp, TREE_TYPE (t),
1376                      (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1377                      | (flags & TFF_DECL_SPECIFIERS
1378                         ? TFF_CLASS_KEY_OR_ENUM : 0));
1379         }
1380     }
1381 }
1382
1383 /* find_typenames looks through the type of the function template T
1384    and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1385    it finds.  */
1386
1387 struct find_typenames_t
1388 {
1389   hash_set<tree> *p_set;
1390   vec<tree, va_gc> *typenames;
1391 };
1392
1393 static tree
1394 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1395 {
1396   struct find_typenames_t *d = (struct find_typenames_t *)data;
1397   tree mv = NULL_TREE;
1398
1399   if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1400     /* Add the type of the typedef without any additional cv-quals.  */
1401     mv = TREE_TYPE (TYPE_NAME (*tp));
1402   else if (TREE_CODE (*tp) == TYPENAME_TYPE
1403            || TREE_CODE (*tp) == DECLTYPE_TYPE)
1404     /* Add the typename without any cv-qualifiers.  */
1405     mv = TYPE_MAIN_VARIANT (*tp);
1406
1407   if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1408     {
1409       /* Don't mess with parameter packs since we don't remember
1410          the pack expansion context for a particular typename.  */
1411       *walk_subtrees = false;
1412       return NULL_TREE;
1413     }
1414
1415   if (mv && (mv == *tp || !d->p_set->add (mv)))
1416     vec_safe_push (d->typenames, mv);
1417
1418   /* Search into class template arguments, which cp_walk_subtrees
1419      doesn't do.  */
1420   if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1421     cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1422                   data, d->p_set);
1423
1424   return NULL_TREE;
1425 }
1426
1427 static vec<tree, va_gc> *
1428 find_typenames (tree t)
1429 {
1430   struct find_typenames_t ft;
1431   ft.p_set = new hash_set<tree>;
1432   ft.typenames = NULL;
1433   cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1434                 find_typenames_r, &ft, ft.p_set);
1435   delete ft.p_set;
1436   return ft.typenames;
1437 }
1438
1439 /* Output the "[with ...]" clause for a template instantiation T iff
1440    TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable.  T may be NULL if
1441    formatting a deduction/substitution diagnostic rather than an
1442    instantiation.  */
1443
1444 static void
1445 dump_substitution (cxx_pretty_printer *pp,
1446                    tree t, tree template_parms, tree template_args,
1447                    int flags)
1448 {
1449   if (template_parms != NULL_TREE && template_args != NULL_TREE
1450       && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1451     {
1452       vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1453       pp_cxx_whitespace (pp);
1454       pp_cxx_left_bracket (pp);
1455       pp->translate_string ("with");
1456       pp_cxx_whitespace (pp);
1457       dump_template_bindings (pp, template_parms, template_args, typenames);
1458       pp_cxx_right_bracket (pp);
1459     }
1460 }
1461
1462 /* Dump the lambda function FN including its 'mutable' qualifier and any
1463    template bindings.  */
1464
1465 static void
1466 dump_lambda_function (cxx_pretty_printer *pp,
1467                       tree fn, tree template_parms, tree template_args,
1468                       int flags)
1469 {
1470   /* A lambda's signature is essentially its "type".  */
1471   dump_type (pp, DECL_CONTEXT (fn), flags);
1472   if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1473     {
1474       pp->padding = pp_before;
1475       pp_c_ws_string (pp, "mutable");
1476     }
1477   dump_substitution (pp, fn, template_parms, template_args, flags);
1478 }
1479
1480 /* Pretty print a function decl. There are several ways we want to print a
1481    function declaration. The TFF_ bits in FLAGS tells us how to behave.
1482    As error can only apply the '#' flag once to give 0 and 1 for V, there
1483    is %D which doesn't print the throw specs, and %F which does.  */
1484
1485 static void
1486 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1487 {
1488   tree fntype;
1489   tree parmtypes;
1490   tree cname = NULL_TREE;
1491   tree template_args = NULL_TREE;
1492   tree template_parms = NULL_TREE;
1493   int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1494   int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1495   tree exceptions;
1496
1497   flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1498   if (TREE_CODE (t) == TEMPLATE_DECL)
1499     t = DECL_TEMPLATE_RESULT (t);
1500
1501   /* Save the exceptions, in case t is a specialization and we are
1502      emitting an error about incompatible specifications.  */
1503   exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1504
1505   /* Pretty print template instantiations only.  */
1506   if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1507       && flag_pretty_templates)
1508     {
1509       tree tmpl;
1510
1511       template_args = DECL_TI_ARGS (t);
1512       tmpl = most_general_template (t);
1513       if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1514         {
1515           template_parms = DECL_TEMPLATE_PARMS (tmpl);
1516           t = tmpl;
1517         }
1518     }
1519
1520   if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1521     return dump_lambda_function (pp, t, template_parms, template_args, flags);
1522
1523   fntype = TREE_TYPE (t);
1524   parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1525
1526   if (DECL_CLASS_SCOPE_P (t))
1527     cname = DECL_CONTEXT (t);
1528   /* This is for partially instantiated template methods.  */
1529   else if (TREE_CODE (fntype) == METHOD_TYPE)
1530     cname = TREE_TYPE (TREE_VALUE (parmtypes));
1531
1532   if (flags & TFF_DECL_SPECIFIERS)
1533     {
1534       if (DECL_STATIC_FUNCTION_P (t))
1535         pp_cxx_ws_string (pp, "static");
1536       else if (DECL_VIRTUAL_P (t))
1537         pp_cxx_ws_string (pp, "virtual");
1538
1539       if (DECL_DECLARED_CONSTEXPR_P (t))
1540         {
1541           if (DECL_DECLARED_CONCEPT_P (t))
1542             pp_cxx_ws_string (pp, "concept");
1543           else
1544             pp_cxx_ws_string (pp, "constexpr");
1545         }
1546     }
1547
1548   /* Print the return type?  */
1549   if (show_return)
1550     show_return = !DECL_CONV_FN_P (t)  && !DECL_CONSTRUCTOR_P (t)
1551                   && !DECL_DESTRUCTOR_P (t);
1552   if (show_return)
1553     {
1554       tree ret = fndecl_declared_return_type (t);
1555       dump_type_prefix (pp, ret, flags);
1556     }
1557
1558   /* Print the function name.  */
1559   if (!do_outer_scope)
1560     /* Nothing.  */;
1561   else if (cname)
1562     {
1563       dump_type (pp, cname, flags);
1564       pp_cxx_colon_colon (pp);
1565     }
1566   else
1567     dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1568
1569   dump_function_name (pp, t, flags);
1570
1571   if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1572     {
1573       dump_parameters (pp, parmtypes, flags);
1574
1575       if (TREE_CODE (fntype) == METHOD_TYPE)
1576         {
1577           pp->padding = pp_before;
1578           pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1579           dump_ref_qualifier (pp, fntype, flags);
1580         }
1581
1582       if (tx_safe_fn_type_p (fntype))
1583         {
1584           pp->padding = pp_before;
1585           pp_cxx_ws_string (pp, "transaction_safe");
1586         }
1587
1588       if (flags & TFF_EXCEPTION_SPECIFICATION)
1589         {
1590           pp->padding = pp_before;
1591           dump_exception_spec (pp, exceptions, flags);
1592         }
1593
1594       if (show_return)
1595         dump_type_suffix (pp, TREE_TYPE (fntype), flags);
1596
1597       if (flag_concepts)
1598         if (tree ci = get_constraints (t))
1599           if (tree reqs = CI_DECLARATOR_REQS (ci))
1600             pp_cxx_requires_clause (pp, reqs);
1601
1602       dump_substitution (pp, t, template_parms, template_args, flags);
1603     }
1604   else if (template_args)
1605     {
1606       bool need_comma = false;
1607       int i;
1608       pp_cxx_begin_template_argument_list (pp);
1609       template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1610       for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1611         {
1612           tree arg = TREE_VEC_ELT (template_args, i);
1613           if (need_comma)
1614             pp_separate_with_comma (pp);
1615           if (ARGUMENT_PACK_P (arg))
1616             pp_cxx_left_brace (pp);
1617           dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1618           if (ARGUMENT_PACK_P (arg))
1619             pp_cxx_right_brace (pp);
1620           need_comma = true;
1621         }
1622       pp_cxx_end_template_argument_list (pp);
1623     }
1624 }
1625
1626 /* Print a parameter list. If this is for a member function, the
1627    member object ptr (and any other hidden args) should have
1628    already been removed.  */
1629
1630 static void
1631 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1632 {
1633   int first = 1;
1634   flags &= ~TFF_SCOPE;
1635   pp_cxx_left_paren (pp);
1636
1637   for (first = 1; parmtypes != void_list_node;
1638        parmtypes = TREE_CHAIN (parmtypes))
1639     {
1640       if (!first)
1641         pp_separate_with_comma (pp);
1642       first = 0;
1643       if (!parmtypes)
1644         {
1645           pp_cxx_ws_string (pp, "...");
1646           break;
1647         }
1648
1649       dump_type (pp, TREE_VALUE (parmtypes), flags);
1650
1651       if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1652         {
1653           pp_cxx_whitespace (pp);
1654           pp_equal (pp);
1655           pp_cxx_whitespace (pp);
1656           dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1657         }
1658     }
1659
1660   pp_cxx_right_paren (pp);
1661 }
1662
1663 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1664
1665 static void
1666 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1667 {
1668   if (FUNCTION_REF_QUALIFIED (t))
1669     {
1670       pp->padding = pp_before;
1671       if (FUNCTION_RVALUE_QUALIFIED (t))
1672         pp_cxx_ws_string (pp, "&&");
1673       else
1674         pp_cxx_ws_string (pp, "&");
1675     }
1676 }
1677
1678 /* Print an exception specification. T is the exception specification.  */
1679
1680 static void
1681 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1682 {
1683   if (t && TREE_PURPOSE (t))
1684     {
1685       pp_cxx_ws_string (pp, "noexcept");
1686       if (!integer_onep (TREE_PURPOSE (t)))
1687         {
1688           pp_cxx_whitespace (pp);
1689           pp_cxx_left_paren (pp);
1690           if (DEFERRED_NOEXCEPT_SPEC_P (t))
1691             pp_cxx_ws_string (pp, "<uninstantiated>");
1692           else
1693             dump_expr (pp, TREE_PURPOSE (t), flags);
1694           pp_cxx_right_paren (pp);
1695         }
1696     }
1697   else if (t)
1698     {
1699       pp_cxx_ws_string (pp, "throw");
1700       pp_cxx_whitespace (pp);
1701       pp_cxx_left_paren (pp);
1702       if (TREE_VALUE (t) != NULL_TREE)
1703         while (1)
1704           {
1705             dump_type (pp, TREE_VALUE (t), flags);
1706             t = TREE_CHAIN (t);
1707             if (!t)
1708               break;
1709             pp_separate_with_comma (pp);
1710           }
1711       pp_cxx_right_paren (pp);
1712     }
1713 }
1714
1715 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1716    and destructors properly.  */
1717
1718 static void
1719 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1720 {
1721   tree name = DECL_NAME (t);
1722
1723   /* We can get here with a decl that was synthesized by language-
1724      independent machinery (e.g. coverage.c) in which case it won't
1725      have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1726      will crash.  In this case it is safe just to print out the
1727      literal name.  */
1728   if (!DECL_LANG_SPECIFIC (t))
1729     {
1730       pp_cxx_tree_identifier (pp, name);
1731       return;
1732     }
1733
1734   if (TREE_CODE (t) == TEMPLATE_DECL)
1735     t = DECL_TEMPLATE_RESULT (t);
1736
1737   /* Don't let the user see __comp_ctor et al.  */
1738   if (DECL_CONSTRUCTOR_P (t)
1739       || DECL_DESTRUCTOR_P (t))
1740     {
1741       if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1742         name = get_identifier ("<lambda>");
1743       else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1744         name = get_identifier ("<constructor>");
1745       else
1746         name = constructor_name (DECL_CONTEXT (t));
1747     }
1748
1749   if (DECL_DESTRUCTOR_P (t))
1750     {
1751       pp_cxx_complement (pp);
1752       dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1753     }
1754   else if (DECL_CONV_FN_P (t))
1755     {
1756       /* This cannot use the hack that the operator's return
1757          type is stashed off of its name because it may be
1758          used for error reporting.  In the case of conflicting
1759          declarations, both will have the same name, yet
1760          the types will be different, hence the TREE_TYPE field
1761          of the first name will be clobbered by the second.  */
1762       pp_cxx_ws_string (pp, "operator");
1763       dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1764     }
1765   else if (name && IDENTIFIER_OPNAME_P (name))
1766     pp_cxx_tree_identifier (pp, name);
1767   else if (name && UDLIT_OPER_P (name))
1768     pp_cxx_tree_identifier (pp, name);
1769   else
1770     dump_decl (pp, name, flags);
1771
1772   if (DECL_TEMPLATE_INFO (t)
1773       && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1774       && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1775           || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1776     dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1777                          flags);
1778 }
1779
1780 /* Dump the template parameters from the template info INFO under control of
1781    FLAGS. PRIMARY indicates whether this is a primary template decl, or
1782    specialization (partial or complete). For partial specializations we show
1783    the specialized parameter values. For a primary template we show no
1784    decoration.  */
1785
1786 static void
1787 dump_template_parms (cxx_pretty_printer *pp, tree info,
1788                      int primary, int flags)
1789 {
1790   tree args = info ? TI_ARGS (info) : NULL_TREE;
1791
1792   if (primary && flags & TFF_TEMPLATE_NAME)
1793     return;
1794   flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1795   pp_cxx_begin_template_argument_list (pp);
1796
1797   /* Be careful only to print things when we have them, so as not
1798      to crash producing error messages.  */
1799   if (args && !primary)
1800     {
1801       int len, ix;
1802       len = get_non_default_template_args_count (args, flags);
1803
1804       args = INNERMOST_TEMPLATE_ARGS (args);
1805       for (ix = 0; ix != len; ix++)
1806         {
1807           tree arg = TREE_VEC_ELT (args, ix);
1808
1809           /* Only print a comma if we know there is an argument coming. In
1810              the case of an empty template argument pack, no actual
1811              argument will be printed.  */
1812           if (ix
1813               && (!ARGUMENT_PACK_P (arg)
1814                   || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1815             pp_separate_with_comma (pp);
1816           
1817           if (!arg)
1818             pp_string (pp, M_("<template parameter error>"));
1819           else
1820             dump_template_argument (pp, arg, flags);
1821         }
1822     }
1823   else if (primary)
1824     {
1825       tree tpl = TI_TEMPLATE (info);
1826       tree parms = DECL_TEMPLATE_PARMS (tpl);
1827       int len, ix;
1828
1829       parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1830       len = parms ? TREE_VEC_LENGTH (parms) : 0;
1831
1832       for (ix = 0; ix != len; ix++)
1833         {
1834           tree parm;
1835
1836           if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1837             {
1838               pp_string (pp, M_("<template parameter error>"));
1839               continue;
1840             }
1841
1842           parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1843
1844           if (ix)
1845             pp_separate_with_comma (pp);
1846
1847           dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1848         }
1849     }
1850   pp_cxx_end_template_argument_list (pp);
1851 }
1852
1853 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1854    flags FLAGS.  Skip over the first argument if SKIPFIRST is true.  */
1855
1856 static void
1857 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1858 {
1859   tree arg;
1860   call_expr_arg_iterator iter;
1861   
1862   pp_cxx_left_paren (pp);
1863   FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1864     {
1865       if (skipfirst)
1866         skipfirst = false;
1867       else
1868         {
1869           dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1870           if (more_call_expr_args_p (&iter))
1871             pp_separate_with_comma (pp);
1872         }
1873     }
1874   pp_cxx_right_paren (pp);
1875 }
1876
1877 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1878    using flags FLAGS.  Skip over the first argument if SKIPFIRST is
1879    true.  */
1880
1881 static void
1882 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1883                           bool skipfirst)
1884 {
1885   tree arg;
1886   aggr_init_expr_arg_iterator iter;
1887   
1888   pp_cxx_left_paren (pp);
1889   FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1890     {
1891       if (skipfirst)
1892         skipfirst = false;
1893       else
1894         {
1895           dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1896           if (more_aggr_init_expr_args_p (&iter))
1897             pp_separate_with_comma (pp);
1898         }
1899     }
1900   pp_cxx_right_paren (pp);
1901 }
1902
1903 /* Print out a list of initializers (subr of dump_expr).  */
1904
1905 static void
1906 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
1907 {
1908   while (l)
1909     {
1910       dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1911       l = TREE_CHAIN (l);
1912       if (l)
1913         pp_separate_with_comma (pp);
1914     }
1915 }
1916
1917 /* Print out a vector of initializers (subr of dump_expr).  */
1918
1919 static void
1920 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
1921                     int flags)
1922 {
1923   unsigned HOST_WIDE_INT idx;
1924   tree value;
1925
1926   FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1927     {
1928       dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
1929       if (idx != v->length () - 1)
1930         pp_separate_with_comma (pp);
1931     }
1932 }
1933
1934
1935 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1936    function.  Resolve it to a close relative -- in the sense of static
1937    type -- variant being overridden.  That is close to what was written in
1938    the source code.  Subroutine of dump_expr.  */
1939
1940 static tree
1941 resolve_virtual_fun_from_obj_type_ref (tree ref)
1942 {
1943   tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1944   HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
1945   tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1946   while (index)
1947     {
1948       fun = TREE_CHAIN (fun);
1949       index -= (TARGET_VTABLE_USES_DESCRIPTORS
1950                 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1951     }
1952
1953   return BV_FN (fun);
1954 }
1955
1956 /* Print out an expression E under control of FLAGS.  */
1957
1958 static void
1959 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
1960 {
1961   tree op;
1962
1963   if (t == 0)
1964     return;
1965
1966   if (STATEMENT_CLASS_P (t))
1967     {
1968       pp_cxx_ws_string (pp, M_("<statement>"));
1969       return;
1970     }
1971
1972   switch (TREE_CODE (t))
1973     {
1974     case VAR_DECL:
1975     case PARM_DECL:
1976     case FIELD_DECL:
1977     case CONST_DECL:
1978     case FUNCTION_DECL:
1979     case TEMPLATE_DECL:
1980     case NAMESPACE_DECL:
1981     case LABEL_DECL:
1982     case OVERLOAD:
1983     case TYPE_DECL:
1984     case IDENTIFIER_NODE:
1985       dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1986                                     |TFF_TEMPLATE_HEADER))
1987                          | TFF_NO_FUNCTION_ARGUMENTS));
1988       break;
1989
1990     case SSA_NAME:
1991       if (SSA_NAME_VAR (t)
1992           && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1993         dump_expr (pp, SSA_NAME_VAR (t), flags);
1994       else
1995         pp_cxx_ws_string (pp, M_("<unknown>"));
1996       break;
1997
1998     case VOID_CST:
1999     case INTEGER_CST:
2000     case REAL_CST:
2001     case STRING_CST:
2002     case COMPLEX_CST:
2003       pp->constant (t);
2004       break;
2005
2006     case USERDEF_LITERAL:
2007       pp_cxx_userdef_literal (pp, t);
2008       break;
2009
2010     case THROW_EXPR:
2011       /* While waiting for caret diagnostics, avoid printing
2012          __cxa_allocate_exception, __cxa_throw, and the like.  */
2013       pp_cxx_ws_string (pp, M_("<throw-expression>"));
2014       break;
2015
2016     case PTRMEM_CST:
2017       pp_ampersand (pp);
2018       dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2019       pp_cxx_colon_colon (pp);
2020       pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2021       break;
2022
2023     case COMPOUND_EXPR:
2024       pp_cxx_left_paren (pp);
2025       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2026       pp_separate_with_comma (pp);
2027       dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2028       pp_cxx_right_paren (pp);
2029       break;
2030
2031     case COND_EXPR:
2032       pp_cxx_left_paren (pp);
2033       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2034       pp_string (pp, " ? ");
2035       dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2036       pp_string (pp, " : ");
2037       dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2038       pp_cxx_right_paren (pp);
2039       break;
2040
2041     case SAVE_EXPR:
2042       if (TREE_HAS_CONSTRUCTOR (t))
2043         {
2044           pp_cxx_ws_string (pp, "new");
2045           pp_cxx_whitespace (pp);
2046           dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2047         }
2048       else
2049         dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2050       break;
2051
2052     case AGGR_INIT_EXPR:
2053       {
2054         tree fn = NULL_TREE;
2055
2056         if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2057           fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2058
2059         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2060           {
2061             if (DECL_CONSTRUCTOR_P (fn))
2062               dump_type (pp, DECL_CONTEXT (fn), flags);
2063             else
2064               dump_decl (pp, fn, 0);
2065           }
2066         else
2067           dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2068       }
2069       dump_aggr_init_expr_args (pp, t, flags, true);
2070       break;
2071
2072     case CALL_EXPR:
2073       {
2074         tree fn = CALL_EXPR_FN (t);
2075         bool skipfirst = false;
2076
2077         /* Deal with internal functions.  */
2078         if (fn == NULL_TREE)
2079           {
2080             pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2081             dump_call_expr_args (pp, t, flags, skipfirst);
2082             break;
2083           }
2084
2085         if (TREE_CODE (fn) == ADDR_EXPR)
2086           fn = TREE_OPERAND (fn, 0);
2087
2088         /* Nobody is interested in seeing the guts of vcalls.  */
2089         if (TREE_CODE (fn) == OBJ_TYPE_REF)
2090           fn = resolve_virtual_fun_from_obj_type_ref (fn);
2091
2092         if (TREE_TYPE (fn) != NULL_TREE
2093             && NEXT_CODE (fn) == METHOD_TYPE
2094             && call_expr_nargs (t))
2095           {
2096             tree ob = CALL_EXPR_ARG (t, 0);
2097             if (TREE_CODE (ob) == ADDR_EXPR)
2098               {
2099                 dump_expr (pp, TREE_OPERAND (ob, 0),
2100                            flags | TFF_EXPR_IN_PARENS);
2101                 pp_cxx_dot (pp);
2102               }
2103             else if (TREE_CODE (ob) != PARM_DECL
2104                      || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
2105               {
2106                 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2107                 pp_cxx_arrow (pp);
2108               }
2109             skipfirst = true;
2110           }
2111         if (flag_sanitize & SANITIZE_UNDEFINED
2112             && is_ubsan_builtin_p (fn))
2113           {
2114             pp_string (cxx_pp, M_("<ubsan routine call>"));
2115             break;
2116           }
2117         dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2118         dump_call_expr_args (pp, t, flags, skipfirst);
2119       }
2120       break;
2121
2122     case TARGET_EXPR:
2123       /* Note that this only works for G++ target exprs.  If somebody
2124          builds a general TARGET_EXPR, there's no way to represent that
2125          it initializes anything other that the parameter slot for the
2126          default argument.  Note we may have cleared out the first
2127          operand in expand_expr, so don't go killing ourselves.  */
2128       if (TREE_OPERAND (t, 1))
2129         dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2130       break;
2131
2132     case POINTER_PLUS_EXPR:
2133       dump_binary_op (pp, "+", t, flags);
2134       break;
2135
2136     case INIT_EXPR:
2137     case MODIFY_EXPR:
2138       dump_binary_op (pp, assignment_operator_name_info[NOP_EXPR].name,
2139                       t, flags);
2140       break;
2141
2142     case PLUS_EXPR:
2143     case MINUS_EXPR:
2144     case MULT_EXPR:
2145     case TRUNC_DIV_EXPR:
2146     case TRUNC_MOD_EXPR:
2147     case MIN_EXPR:
2148     case MAX_EXPR:
2149     case LSHIFT_EXPR:
2150     case RSHIFT_EXPR:
2151     case BIT_IOR_EXPR:
2152     case BIT_XOR_EXPR:
2153     case BIT_AND_EXPR:
2154     case TRUTH_ANDIF_EXPR:
2155     case TRUTH_ORIF_EXPR:
2156     case LT_EXPR:
2157     case LE_EXPR:
2158     case GT_EXPR:
2159     case GE_EXPR:
2160     case EQ_EXPR:
2161     case NE_EXPR:
2162     case EXACT_DIV_EXPR:
2163       dump_binary_op (pp, operator_name_info[TREE_CODE (t)].name, t, flags);
2164       break;
2165
2166     case CEIL_DIV_EXPR:
2167     case FLOOR_DIV_EXPR:
2168     case ROUND_DIV_EXPR:
2169     case RDIV_EXPR:
2170       dump_binary_op (pp, "/", t, flags);
2171       break;
2172
2173     case CEIL_MOD_EXPR:
2174     case FLOOR_MOD_EXPR:
2175     case ROUND_MOD_EXPR:
2176       dump_binary_op (pp, "%", t, flags);
2177       break;
2178
2179     case COMPONENT_REF:
2180       {
2181         tree ob = TREE_OPERAND (t, 0);
2182         if (INDIRECT_REF_P (ob))
2183           {
2184             ob = TREE_OPERAND (ob, 0);
2185             if (TREE_CODE (ob) != PARM_DECL
2186                 || (DECL_NAME (ob)
2187                     && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
2188               {
2189                 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2190                 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2191                   pp_cxx_dot (pp);
2192                 else
2193                   pp_cxx_arrow (pp);
2194               }
2195           }
2196         else
2197           {
2198             dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2199             pp_cxx_dot (pp);
2200           }
2201         dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2202       }
2203       break;
2204
2205     case ARRAY_REF:
2206       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2207       pp_cxx_left_bracket (pp);
2208       dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2209       pp_cxx_right_bracket (pp);
2210       break;
2211
2212     case ARRAY_NOTATION_REF:
2213       dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2214       pp_cxx_left_bracket (pp);
2215       dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2216       pp_colon (pp);
2217       dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2218       pp_colon (pp);
2219       dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2220       pp_cxx_right_bracket (pp);
2221       break;
2222
2223     case UNARY_PLUS_EXPR:
2224       dump_unary_op (pp, "+", t, flags);
2225       break;
2226
2227     case ADDR_EXPR:
2228       if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2229           || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2230           /* An ADDR_EXPR can have reference type.  In that case, we
2231              shouldn't print the `&' doing so indicates to the user
2232              that the expression has pointer type.  */
2233           || (TREE_TYPE (t)
2234               && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2235         dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2236       else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2237         dump_unary_op (pp, "&&", t, flags);
2238       else
2239         dump_unary_op (pp, "&", t, flags);
2240       break;
2241
2242     case INDIRECT_REF:
2243       if (TREE_HAS_CONSTRUCTOR (t))
2244         {
2245           t = TREE_OPERAND (t, 0);
2246           gcc_assert (TREE_CODE (t) == CALL_EXPR);
2247           dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2248           dump_call_expr_args (pp, t, flags, true);
2249         }
2250       else
2251         {
2252           if (TREE_OPERAND (t,0) != NULL_TREE
2253               && TREE_TYPE (TREE_OPERAND (t, 0))
2254               && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2255             dump_expr (pp, TREE_OPERAND (t, 0), flags);
2256           else
2257             dump_unary_op (pp, "*", t, flags);
2258         }
2259       break;
2260
2261     case MEM_REF:
2262       if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2263           && integer_zerop (TREE_OPERAND (t, 1)))
2264         dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2265       else
2266         {
2267           pp_cxx_star (pp);
2268           if (!integer_zerop (TREE_OPERAND (t, 1)))
2269             {
2270               pp_cxx_left_paren (pp);
2271               if (!integer_onep (TYPE_SIZE_UNIT
2272                                  (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2273                 {
2274                   pp_cxx_left_paren (pp);
2275                   dump_type (pp, ptr_type_node, flags);
2276                   pp_cxx_right_paren (pp);
2277                 }
2278             }
2279           dump_expr (pp, TREE_OPERAND (t, 0), flags);
2280           if (!integer_zerop (TREE_OPERAND (t, 1)))
2281             {
2282               pp_cxx_ws_string (pp, "+");
2283               dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2284                          flags);
2285               pp_cxx_right_paren (pp);
2286             }
2287         }
2288       break;
2289
2290     case NEGATE_EXPR:
2291     case BIT_NOT_EXPR:
2292     case TRUTH_NOT_EXPR:
2293     case PREDECREMENT_EXPR:
2294     case PREINCREMENT_EXPR:
2295       dump_unary_op (pp, operator_name_info [TREE_CODE (t)].name, t, flags);
2296       break;
2297
2298     case POSTDECREMENT_EXPR:
2299     case POSTINCREMENT_EXPR:
2300       pp_cxx_left_paren (pp);
2301       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2302       pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2303       pp_cxx_right_paren (pp);
2304       break;
2305
2306     case NON_LVALUE_EXPR:
2307       /* FIXME: This is a KLUDGE workaround for a parsing problem.  There
2308          should be another level of INDIRECT_REF so that I don't have to do
2309          this.  */
2310       if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2311         {
2312           tree next = TREE_TYPE (TREE_TYPE (t));
2313
2314           while (TYPE_PTR_P (next))
2315             next = TREE_TYPE (next);
2316
2317           if (TREE_CODE (next) == FUNCTION_TYPE)
2318             {
2319               if (flags & TFF_EXPR_IN_PARENS)
2320                 pp_cxx_left_paren (pp);
2321               pp_cxx_star (pp);
2322               dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2323               if (flags & TFF_EXPR_IN_PARENS)
2324                 pp_cxx_right_paren (pp);
2325               break;
2326             }
2327           /* Else fall through.  */
2328         }
2329       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2330       break;
2331
2332     CASE_CONVERT:
2333     case IMPLICIT_CONV_EXPR:
2334     case VIEW_CONVERT_EXPR:
2335       {
2336         tree op = TREE_OPERAND (t, 0);
2337         tree ttype = TREE_TYPE (t);
2338         tree optype = TREE_TYPE (op);
2339
2340         if (TREE_CODE (ttype) != TREE_CODE (optype)
2341             && POINTER_TYPE_P (ttype)
2342             && POINTER_TYPE_P (optype)
2343             && same_type_p (TREE_TYPE (optype),
2344                             TREE_TYPE (ttype)))
2345           {
2346             if (TREE_CODE (ttype) == REFERENCE_TYPE)
2347               {
2348                 STRIP_NOPS (op);
2349                 if (TREE_CODE (op) == ADDR_EXPR)
2350                   dump_expr (pp, TREE_OPERAND (op, 0), flags);
2351                 else
2352                   dump_unary_op (pp, "*", t, flags);
2353               }
2354             else
2355               dump_unary_op (pp, "&", t, flags);
2356           }
2357         else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2358           {
2359             /* It is a cast, but we cannot tell whether it is a
2360                reinterpret or static cast. Use the C style notation.  */
2361             if (flags & TFF_EXPR_IN_PARENS)
2362               pp_cxx_left_paren (pp);
2363             pp_cxx_left_paren (pp);
2364             dump_type (pp, TREE_TYPE (t), flags);
2365             pp_cxx_right_paren (pp);
2366             dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2367             if (flags & TFF_EXPR_IN_PARENS)
2368               pp_cxx_right_paren (pp);
2369           }
2370         else
2371           dump_expr (pp, op, flags);
2372         break;
2373       }
2374
2375     case CONSTRUCTOR:
2376       if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2377         {
2378           tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2379
2380           if (integer_zerop (idx))
2381             {
2382               /* A NULL pointer-to-member constant.  */
2383               pp_cxx_left_paren (pp);
2384               pp_cxx_left_paren (pp);
2385               dump_type (pp, TREE_TYPE (t), flags);
2386               pp_cxx_right_paren (pp);
2387               pp_character (pp, '0');
2388               pp_cxx_right_paren (pp);
2389               break;
2390             }
2391           else if (tree_fits_shwi_p (idx))
2392             {
2393               tree virtuals;
2394               unsigned HOST_WIDE_INT n;
2395
2396               t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2397               t = TYPE_METHOD_BASETYPE (t);
2398               virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2399
2400               n = tree_to_shwi (idx);
2401
2402               /* Map vtable index back one, to allow for the null pointer to
2403                  member.  */
2404               --n;
2405
2406               while (n > 0 && virtuals)
2407                 {
2408                   --n;
2409                   virtuals = TREE_CHAIN (virtuals);
2410                 }
2411               if (virtuals)
2412                 {
2413                   dump_expr (pp, BV_FN (virtuals),
2414                              flags | TFF_EXPR_IN_PARENS);
2415                   break;
2416                 }
2417             }
2418         }
2419       if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2420         pp_string (pp, "<lambda closure object>");
2421       if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2422         {
2423           dump_type (pp, TREE_TYPE (t), 0);
2424           pp_cxx_left_paren (pp);
2425           pp_cxx_right_paren (pp);
2426         }
2427       else
2428         {
2429           if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2430             dump_type (pp, TREE_TYPE (t), 0);
2431           pp_cxx_left_brace (pp);
2432           dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2433           pp_cxx_right_brace (pp);
2434         }
2435
2436       break;
2437
2438     case OFFSET_REF:
2439       {
2440         tree ob = TREE_OPERAND (t, 0);
2441         if (is_dummy_object (ob))
2442           {
2443             t = TREE_OPERAND (t, 1);
2444             if (TREE_CODE (t) == FUNCTION_DECL)
2445               /* A::f */
2446               dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2447             else if (BASELINK_P (t))
2448               dump_expr (pp, OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2449                          flags | TFF_EXPR_IN_PARENS);
2450             else
2451               dump_decl (pp, t, flags);
2452           }
2453         else
2454           {
2455             if (INDIRECT_REF_P (ob))
2456               {
2457                 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2458                 pp_cxx_arrow (pp);
2459                 pp_cxx_star (pp);
2460               }
2461             else
2462               {
2463                 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2464                 pp_cxx_dot (pp);
2465                 pp_cxx_star (pp);
2466               }
2467             dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2468           }
2469         break;
2470       }
2471
2472     case TEMPLATE_PARM_INDEX:
2473       dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2474       break;
2475
2476     case CAST_EXPR:
2477       if (TREE_OPERAND (t, 0) == NULL_TREE
2478           || TREE_CHAIN (TREE_OPERAND (t, 0)))
2479         {
2480           dump_type (pp, TREE_TYPE (t), flags);
2481           pp_cxx_left_paren (pp);
2482           dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2483           pp_cxx_right_paren (pp);
2484         }
2485       else
2486         {
2487           pp_cxx_left_paren (pp);
2488           dump_type (pp, TREE_TYPE (t), flags);
2489           pp_cxx_right_paren (pp);
2490           pp_cxx_left_paren (pp);
2491           dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2492           pp_cxx_right_paren (pp);
2493         }
2494       break;
2495
2496     case STATIC_CAST_EXPR:
2497       pp_cxx_ws_string (pp, "static_cast");
2498       goto cast;
2499     case REINTERPRET_CAST_EXPR:
2500       pp_cxx_ws_string (pp, "reinterpret_cast");
2501       goto cast;
2502     case CONST_CAST_EXPR:
2503       pp_cxx_ws_string (pp, "const_cast");
2504       goto cast;
2505     case DYNAMIC_CAST_EXPR:
2506       pp_cxx_ws_string (pp, "dynamic_cast");
2507     cast:
2508       pp_cxx_begin_template_argument_list (pp);
2509       dump_type (pp, TREE_TYPE (t), flags);
2510       pp_cxx_end_template_argument_list (pp);
2511       pp_cxx_left_paren (pp);
2512       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2513       pp_cxx_right_paren (pp);
2514       break;
2515
2516     case ARROW_EXPR:
2517       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2518       pp_cxx_arrow (pp);
2519       break;
2520
2521     case SIZEOF_EXPR:
2522     case ALIGNOF_EXPR:
2523       if (TREE_CODE (t) == SIZEOF_EXPR)
2524         pp_cxx_ws_string (pp, "sizeof");
2525       else
2526         {
2527           gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2528           pp_cxx_ws_string (pp, "__alignof__");
2529         }
2530       op = TREE_OPERAND (t, 0);
2531       if (PACK_EXPANSION_P (op))
2532         {
2533           pp_string (pp, "...");
2534           op = PACK_EXPANSION_PATTERN (op);
2535         }
2536       pp_cxx_whitespace (pp);
2537       pp_cxx_left_paren (pp);
2538       if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2539         dump_type (pp, TREE_TYPE (op), flags);
2540       else if (TYPE_P (TREE_OPERAND (t, 0)))
2541         dump_type (pp, op, flags);
2542       else
2543         dump_expr (pp, op, flags);
2544       pp_cxx_right_paren (pp);
2545       break;
2546
2547     case AT_ENCODE_EXPR:
2548       pp_cxx_ws_string (pp, "@encode");
2549       pp_cxx_whitespace (pp);
2550       pp_cxx_left_paren (pp);
2551       dump_type (pp, TREE_OPERAND (t, 0), flags);
2552       pp_cxx_right_paren (pp);
2553       break;
2554
2555     case NOEXCEPT_EXPR:
2556       pp_cxx_ws_string (pp, "noexcept");
2557       pp_cxx_whitespace (pp);
2558       pp_cxx_left_paren (pp);
2559       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2560       pp_cxx_right_paren (pp);
2561       break;
2562
2563     case REALPART_EXPR:
2564     case IMAGPART_EXPR:
2565       pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2566       pp_cxx_whitespace (pp);
2567       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2568       break;
2569
2570     case DEFAULT_ARG:
2571       pp_string (pp, M_("<unparsed>"));
2572       break;
2573
2574     case TRY_CATCH_EXPR:
2575     case WITH_CLEANUP_EXPR:
2576     case CLEANUP_POINT_EXPR:
2577       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2578       break;
2579
2580     case PSEUDO_DTOR_EXPR:
2581       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2582       pp_cxx_dot (pp);
2583       if (TREE_OPERAND (t, 1))
2584         {
2585           dump_type (pp, TREE_OPERAND (t, 1), flags);
2586           pp_cxx_colon_colon (pp);
2587         }
2588       pp_cxx_complement (pp);
2589       dump_type (pp, TREE_OPERAND (t, 2), flags);
2590       break;
2591
2592     case TEMPLATE_ID_EXPR:
2593       dump_decl (pp, t, flags);
2594       break;
2595
2596     case BIND_EXPR:
2597     case STMT_EXPR:
2598     case EXPR_STMT:
2599     case STATEMENT_LIST:
2600     case STATEMENT_LIST_END:
2601       /* We don't yet have a way of dumping statements in a
2602          human-readable format.  */
2603       pp_string (pp, "({...})");
2604       break;
2605
2606     case LOOP_EXPR:
2607       pp_string (pp, "while (1) { ");
2608       dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2609       pp_cxx_right_brace (pp);
2610       break;
2611
2612     case EXIT_EXPR:
2613       pp_string (pp, "if (");
2614       dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2615       pp_string (pp, ") break; ");
2616       break;
2617
2618     case BASELINK:
2619       dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2620       break;
2621
2622     case EMPTY_CLASS_EXPR:
2623       dump_type (pp, TREE_TYPE (t), flags);
2624       pp_cxx_left_paren (pp);
2625       pp_cxx_right_paren (pp);
2626       break;
2627
2628     case NON_DEPENDENT_EXPR:
2629       dump_expr (pp, TREE_OPERAND (t, 0), flags);
2630       break;
2631
2632     case ARGUMENT_PACK_SELECT:
2633       dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2634       break;
2635
2636     case RECORD_TYPE:
2637     case UNION_TYPE:
2638     case ENUMERAL_TYPE:
2639     case REAL_TYPE:
2640     case VOID_TYPE:
2641     case BOOLEAN_TYPE:
2642     case INTEGER_TYPE:
2643     case COMPLEX_TYPE:
2644     case VECTOR_TYPE:
2645       pp_type_specifier_seq (pp, t);
2646       break;
2647
2648     case TYPENAME_TYPE:
2649       /* We get here when we want to print a dependent type as an
2650          id-expression, without any disambiguator decoration.  */
2651       pp->id_expression (t);
2652       break;
2653
2654     case TEMPLATE_TYPE_PARM:
2655     case TEMPLATE_TEMPLATE_PARM:
2656     case BOUND_TEMPLATE_TEMPLATE_PARM:
2657       dump_type (pp, t, flags);
2658       break;
2659
2660     case TRAIT_EXPR:
2661       pp_cxx_trait_expression (pp, t);
2662       break;
2663
2664     case VA_ARG_EXPR:
2665       pp_cxx_va_arg_expression (pp, t);
2666       break;
2667
2668     case OFFSETOF_EXPR:
2669       pp_cxx_offsetof_expression (pp, t);
2670       break;
2671
2672     case SCOPE_REF:
2673       dump_decl (pp, t, flags);
2674       break;
2675
2676     case EXPR_PACK_EXPANSION:
2677     case UNARY_LEFT_FOLD_EXPR:
2678     case UNARY_RIGHT_FOLD_EXPR:
2679     case BINARY_LEFT_FOLD_EXPR:
2680     case BINARY_RIGHT_FOLD_EXPR:
2681     case TYPEID_EXPR:
2682     case MEMBER_REF:
2683     case DOTSTAR_EXPR:
2684     case NEW_EXPR:
2685     case VEC_NEW_EXPR:
2686     case DELETE_EXPR:
2687     case VEC_DELETE_EXPR:
2688     case MODOP_EXPR:
2689     case ABS_EXPR:
2690     case CONJ_EXPR:
2691     case VECTOR_CST:
2692     case FIXED_CST:
2693     case UNORDERED_EXPR:
2694     case ORDERED_EXPR:
2695     case UNLT_EXPR:
2696     case UNLE_EXPR:
2697     case UNGT_EXPR:
2698     case UNGE_EXPR:
2699     case UNEQ_EXPR:
2700     case LTGT_EXPR:
2701     case COMPLEX_EXPR:
2702     case BIT_FIELD_REF:
2703     case FIX_TRUNC_EXPR:
2704     case FLOAT_EXPR:
2705       pp->expression (t);
2706       break;
2707
2708     case TRUTH_AND_EXPR:
2709     case TRUTH_OR_EXPR:
2710     case TRUTH_XOR_EXPR:
2711       if (flags & TFF_EXPR_IN_PARENS)
2712         pp_cxx_left_paren (pp);
2713       pp->expression (t);
2714       if (flags & TFF_EXPR_IN_PARENS)
2715         pp_cxx_right_paren (pp);
2716       break;
2717
2718     case OBJ_TYPE_REF:
2719       dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2720       break;
2721
2722     case LAMBDA_EXPR:
2723       pp_string (pp, M_("<lambda>"));
2724       break;
2725
2726     case PAREN_EXPR:
2727       pp_cxx_left_paren (pp);
2728       dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2729       pp_cxx_right_paren (pp);
2730       break;
2731
2732     case REQUIRES_EXPR:
2733       pp_cxx_requires_expr (cxx_pp, t);
2734       break;
2735
2736     case SIMPLE_REQ:
2737       pp_cxx_simple_requirement (cxx_pp, t);
2738       break;
2739
2740     case TYPE_REQ:
2741       pp_cxx_type_requirement (cxx_pp, t);
2742       break;
2743
2744     case COMPOUND_REQ:
2745       pp_cxx_compound_requirement (cxx_pp, t);
2746       break;
2747
2748     case NESTED_REQ:
2749       pp_cxx_nested_requirement (cxx_pp, t);
2750       break;
2751
2752     case PRED_CONSTR:
2753     case CHECK_CONSTR:
2754     case EXPR_CONSTR:
2755     case TYPE_CONSTR:
2756     case ICONV_CONSTR:
2757     case DEDUCT_CONSTR:
2758     case EXCEPT_CONSTR:
2759     case PARM_CONSTR:
2760     case CONJ_CONSTR:
2761     case DISJ_CONSTR:
2762       pp_cxx_constraint (cxx_pp, t);
2763       break;
2764
2765     case PLACEHOLDER_EXPR:
2766       pp_string (pp, M_("*this"));
2767       break;
2768
2769       /*  This list is incomplete, but should suffice for now.
2770           It is very important that `sorry' does not call
2771           `report_error_function'.  That could cause an infinite loop.  */
2772     default:
2773       pp_unsupported_tree (pp, t);
2774       /* fall through to ERROR_MARK...  */
2775     case ERROR_MARK:
2776       pp_string (pp, M_("<expression error>"));
2777       break;
2778     }
2779 }
2780
2781 static void
2782 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2783                 int flags)
2784 {
2785   pp_cxx_left_paren (pp);
2786   dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2787   pp_cxx_whitespace (pp);
2788   if (opstring)
2789     pp_cxx_ws_string (pp, opstring);
2790   else
2791     pp_string (pp, M_("<unknown operator>"));
2792   pp_cxx_whitespace (pp);
2793   dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2794   pp_cxx_right_paren (pp);
2795 }
2796
2797 static void
2798 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2799 {
2800   if (flags & TFF_EXPR_IN_PARENS)
2801     pp_cxx_left_paren (pp);
2802   pp_cxx_ws_string (pp, opstring);
2803   dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2804   if (flags & TFF_EXPR_IN_PARENS)
2805     pp_cxx_right_paren (pp);
2806 }
2807
2808 static void
2809 reinit_cxx_pp (void)
2810 {
2811   pp_clear_output_area (cxx_pp);
2812   cxx_pp->padding = pp_none;
2813   pp_indentation (cxx_pp) = 0;
2814   pp_needs_newline (cxx_pp) = false;
2815   cxx_pp->enclosing_scope = current_function_decl;
2816 }
2817
2818 /* Same as pp_formatted_text, except the return string is a separate
2819    copy and has a GGC storage duration, e.g. an indefinite lifetime.  */
2820
2821 inline const char *
2822 pp_ggc_formatted_text (pretty_printer *pp)
2823 {
2824   return ggc_strdup (pp_formatted_text (pp));
2825 }
2826
2827 /* Exported interface to stringifying types, exprs and decls under TFF_*
2828    control.  */
2829
2830 const char *
2831 type_as_string (tree typ, int flags)
2832 {
2833   reinit_cxx_pp ();
2834   pp_translate_identifiers (cxx_pp) = false;
2835   dump_type (cxx_pp, typ, flags);
2836   return pp_ggc_formatted_text (cxx_pp);
2837 }
2838
2839 const char *
2840 type_as_string_translate (tree typ, int flags)
2841 {
2842   reinit_cxx_pp ();
2843   dump_type (cxx_pp, typ, flags);
2844   return pp_ggc_formatted_text (cxx_pp);
2845 }
2846
2847 const char *
2848 expr_as_string (tree decl, int flags)
2849 {
2850   reinit_cxx_pp ();
2851   pp_translate_identifiers (cxx_pp) = false;
2852   dump_expr (cxx_pp, decl, flags);
2853   return pp_ggc_formatted_text (cxx_pp);
2854 }
2855
2856 /* Wrap decl_as_string with options appropriate for dwarf.  */
2857
2858 const char *
2859 decl_as_dwarf_string (tree decl, int flags)
2860 {
2861   const char *name;
2862   /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2863      here will be adequate to get the desired behavior.  */
2864   cxx_pp->flags |= pp_c_flag_gnu_v3;
2865   name = decl_as_string (decl, flags);
2866   /* Subsequent calls to the pretty printer shouldn't use this style.  */
2867   cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2868   return name;
2869 }
2870
2871 const char *
2872 decl_as_string (tree decl, int flags)
2873 {
2874   reinit_cxx_pp ();
2875   pp_translate_identifiers (cxx_pp) = false;
2876   dump_decl (cxx_pp, decl, flags);
2877   return pp_ggc_formatted_text (cxx_pp);
2878 }
2879
2880 const char *
2881 decl_as_string_translate (tree decl, int flags)
2882 {
2883   reinit_cxx_pp ();
2884   dump_decl (cxx_pp, decl, flags);
2885   return pp_ggc_formatted_text (cxx_pp);
2886 }
2887
2888 /* Wrap lang_decl_name with options appropriate for dwarf.  */
2889
2890 const char *
2891 lang_decl_dwarf_name (tree decl, int v, bool translate)
2892 {
2893   const char *name;
2894   /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2895      here will be adequate to get the desired behavior.  */
2896   cxx_pp->flags |= pp_c_flag_gnu_v3;
2897   name = lang_decl_name (decl, v, translate);
2898   /* Subsequent calls to the pretty printer shouldn't use this style.  */
2899   cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2900   return name;
2901 }
2902
2903 /* Generate the three forms of printable names for cxx_printable_name.  */
2904
2905 const char *
2906 lang_decl_name (tree decl, int v, bool translate)
2907 {
2908   if (v >= 2)
2909     return (translate
2910             ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2911             : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2912
2913   reinit_cxx_pp ();
2914   pp_translate_identifiers (cxx_pp) = translate;
2915   if (v == 1
2916       && (DECL_CLASS_SCOPE_P (decl)
2917           || (DECL_NAMESPACE_SCOPE_P (decl)
2918               && CP_DECL_CONTEXT (decl) != global_namespace)))
2919     {
2920       dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2921       pp_cxx_colon_colon (cxx_pp);
2922     }
2923
2924   if (TREE_CODE (decl) == FUNCTION_DECL)
2925     dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
2926   else if ((DECL_NAME (decl) == NULL_TREE)
2927            && TREE_CODE (decl) == NAMESPACE_DECL)
2928     dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2929   else
2930     dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2931
2932   return pp_ggc_formatted_text (cxx_pp);
2933 }
2934
2935 /* Return the location of a tree passed to %+ formats.  */
2936
2937 location_t
2938 location_of (tree t)
2939 {
2940   if (TYPE_P (t))
2941     {
2942       t = TYPE_MAIN_DECL (t);
2943       if (t == NULL_TREE)
2944         return input_location;
2945     }
2946   else if (TREE_CODE (t) == OVERLOAD)
2947     t = OVL_FUNCTION (t);
2948
2949   if (DECL_P (t))
2950     return DECL_SOURCE_LOCATION (t);
2951   return EXPR_LOC_OR_LOC (t, input_location);
2952 }
2953
2954 /* Now the interfaces from error et al to dump_type et al. Each takes an
2955    on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2956    function.  */
2957
2958 static const char *
2959 decl_to_string (tree decl, int verbose)
2960 {
2961   int flags = 0;
2962
2963   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2964       || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2965     flags = TFF_CLASS_KEY_OR_ENUM;
2966   if (verbose)
2967     flags |= TFF_DECL_SPECIFIERS;
2968   else if (TREE_CODE (decl) == FUNCTION_DECL)
2969     flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2970   flags |= TFF_TEMPLATE_HEADER;
2971
2972   reinit_cxx_pp ();
2973   dump_decl (cxx_pp, decl, flags);
2974   return pp_ggc_formatted_text (cxx_pp);
2975 }
2976
2977 static const char *
2978 expr_to_string (tree decl)
2979 {
2980   reinit_cxx_pp ();
2981   dump_expr (cxx_pp, decl, 0);
2982   return pp_ggc_formatted_text (cxx_pp);
2983 }
2984
2985 static const char *
2986 fndecl_to_string (tree fndecl, int verbose)
2987 {
2988   int flags;
2989
2990   flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2991     | TFF_TEMPLATE_HEADER;
2992   if (verbose)
2993     flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2994   reinit_cxx_pp ();
2995   dump_decl (cxx_pp, fndecl, flags);
2996   return pp_ggc_formatted_text (cxx_pp);
2997 }
2998
2999
3000 static const char *
3001 code_to_string (enum tree_code c)
3002 {
3003   return get_tree_code_name (c);
3004 }
3005
3006 const char *
3007 language_to_string (enum languages c)
3008 {
3009   switch (c)
3010     {
3011     case lang_c:
3012       return "C";
3013
3014     case lang_cplusplus:
3015       return "C++";
3016
3017     case lang_java:
3018       return "Java";
3019
3020     default:
3021       gcc_unreachable ();
3022     }
3023   return NULL;
3024 }
3025
3026 /* Return the proper printed version of a parameter to a C++ function.  */
3027
3028 static const char *
3029 parm_to_string (int p)
3030 {
3031   reinit_cxx_pp ();
3032   if (p < 0)
3033     pp_string (cxx_pp, "'this'");
3034   else
3035     pp_decimal_int (cxx_pp, p + 1);
3036   return pp_ggc_formatted_text (cxx_pp);
3037 }
3038
3039 static const char *
3040 op_to_string (enum tree_code p)
3041 {
3042   tree id = operator_name_info[p].identifier;
3043   return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3044 }
3045
3046 static const char *
3047 type_to_string (tree typ, int verbose)
3048 {
3049   int flags = 0;
3050   if (verbose)
3051     flags |= TFF_CLASS_KEY_OR_ENUM;
3052   flags |= TFF_TEMPLATE_HEADER;
3053
3054   reinit_cxx_pp ();
3055   dump_type (cxx_pp, typ, flags);
3056   /* If we're printing a type that involves typedefs, also print the
3057      stripped version.  But sometimes the stripped version looks
3058      exactly the same, so we don't want it after all.  To avoid printing
3059      it in that case, we play ugly obstack games.  */
3060   if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3061       && !uses_template_parms (typ))
3062     {
3063       int aka_start, aka_len; char *p;
3064       struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3065       /* Remember the end of the initial dump.  */
3066       int len = obstack_object_size (ob);
3067       tree aka = strip_typedefs (typ);
3068       pp_string (cxx_pp, " {aka");
3069       pp_cxx_whitespace (cxx_pp);
3070       /* And remember the start of the aka dump.  */
3071       aka_start = obstack_object_size (ob);
3072       dump_type (cxx_pp, aka, flags);
3073       aka_len = obstack_object_size (ob) - aka_start;
3074       pp_right_brace (cxx_pp);
3075       p = (char*)obstack_base (ob);
3076       /* If they are identical, cut off the aka with a NUL.  */
3077       if (len == aka_len && memcmp (p, p+aka_start, len) == 0)
3078         p[len] = '\0';
3079     }
3080   return pp_ggc_formatted_text (cxx_pp);
3081 }
3082
3083 static const char *
3084 assop_to_string (enum tree_code p)
3085 {
3086   tree id = assignment_operator_name_info[(int) p].identifier;
3087   return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
3088 }
3089
3090 static const char *
3091 args_to_string (tree p, int verbose)
3092 {
3093   int flags = 0;
3094   if (verbose)
3095     flags |= TFF_CLASS_KEY_OR_ENUM;
3096
3097   if (p == NULL_TREE)
3098     return "";
3099
3100   if (TYPE_P (TREE_VALUE (p)))
3101     return type_as_string_translate (p, flags);
3102
3103   reinit_cxx_pp ();
3104   for (; p; p = TREE_CHAIN (p))
3105     {
3106       if (TREE_VALUE (p) == null_node)
3107         pp_cxx_ws_string (cxx_pp, "NULL");
3108       else
3109         dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3110       if (TREE_CHAIN (p))
3111         pp_separate_with_comma (cxx_pp);
3112     }
3113   return pp_ggc_formatted_text (cxx_pp);
3114 }
3115
3116 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype).  P
3117    is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3118    arguments.  */
3119
3120 static const char *
3121 subst_to_string (tree p)
3122 {
3123   tree decl = TREE_PURPOSE (p);
3124   tree targs = TREE_VALUE (p);
3125   tree tparms = DECL_TEMPLATE_PARMS (decl);
3126   int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3127                |TFF_NO_TEMPLATE_BINDINGS);
3128
3129   if (p == NULL_TREE)
3130     return "";
3131
3132   reinit_cxx_pp ();
3133   dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3134   dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3135   return pp_ggc_formatted_text (cxx_pp);
3136 }
3137
3138 static const char *
3139 cv_to_string (tree p, int v)
3140 {
3141   reinit_cxx_pp ();
3142   cxx_pp->padding = v ? pp_before : pp_none;
3143   pp_cxx_cv_qualifier_seq (cxx_pp, p);
3144   return pp_ggc_formatted_text (cxx_pp);
3145 }
3146
3147 static const char *
3148 eh_spec_to_string (tree p, int /*v*/)
3149 {
3150   int flags = 0;
3151   reinit_cxx_pp ();
3152   dump_exception_spec (cxx_pp, p, flags);
3153   return pp_ggc_formatted_text (cxx_pp);
3154 }
3155
3156 /* Langhook for print_error_function.  */
3157 void
3158 cxx_print_error_function (diagnostic_context *context, const char *file,
3159                           diagnostic_info *diagnostic)
3160 {
3161   lhd_print_error_function (context, file, diagnostic);
3162   pp_set_prefix (context->printer, file);
3163   maybe_print_instantiation_context (context);
3164 }
3165
3166 static void
3167 cp_diagnostic_starter (diagnostic_context *context,
3168                        diagnostic_info *diagnostic)
3169 {
3170   diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3171   cp_print_error_function (context, diagnostic);
3172   maybe_print_instantiation_context (context);
3173   maybe_print_constexpr_context (context);
3174   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3175                                                                  diagnostic));
3176 }
3177
3178 /* Print current function onto BUFFER, in the process of reporting
3179    a diagnostic message.  Called from cp_diagnostic_starter.  */
3180 static void
3181 cp_print_error_function (diagnostic_context *context,
3182                          diagnostic_info *diagnostic)
3183 {
3184   /* If we are in an instantiation context, current_function_decl is likely
3185      to be wrong, so just rely on print_instantiation_full_context.  */
3186   if (current_instantiation ())
3187     return;
3188   if (diagnostic_last_function_changed (context, diagnostic))
3189     {
3190       const char *old_prefix = context->printer->prefix;
3191       const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3192       tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3193       char *new_prefix = (file && abstract_origin == NULL)
3194                          ? file_name_as_prefix (context, file) : NULL;
3195
3196       pp_set_prefix (context->printer, new_prefix);
3197
3198       if (current_function_decl == NULL)
3199         pp_string (context->printer, _("At global scope:"));
3200       else
3201         {
3202           tree fndecl, ao;
3203
3204           if (abstract_origin)
3205             {
3206               ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3207               while (TREE_CODE (ao) == BLOCK
3208                      && BLOCK_ABSTRACT_ORIGIN (ao)
3209                      && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3210                 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3211               gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3212               fndecl = ao;
3213             }
3214           else
3215             fndecl = current_function_decl;
3216
3217           pp_printf (context->printer, function_category (fndecl),
3218                      cxx_printable_name_translate (fndecl, 2));
3219
3220           while (abstract_origin)
3221             {
3222               location_t *locus;
3223               tree block = abstract_origin;
3224
3225               locus = &BLOCK_SOURCE_LOCATION (block);
3226               fndecl = NULL;
3227               block = BLOCK_SUPERCONTEXT (block);
3228               while (block && TREE_CODE (block) == BLOCK
3229                      && BLOCK_ABSTRACT_ORIGIN (block))
3230                 {
3231                   ao = BLOCK_ABSTRACT_ORIGIN (block);
3232
3233                   while (TREE_CODE (ao) == BLOCK
3234                          && BLOCK_ABSTRACT_ORIGIN (ao)
3235                          && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3236                     ao = BLOCK_ABSTRACT_ORIGIN (ao);
3237
3238                   if (TREE_CODE (ao) == FUNCTION_DECL)
3239                     {
3240                       fndecl = ao;
3241                       break;
3242                     }
3243                   else if (TREE_CODE (ao) != BLOCK)
3244                     break;
3245
3246                   block = BLOCK_SUPERCONTEXT (block);
3247                 }
3248               if (fndecl)
3249                 abstract_origin = block;
3250               else
3251                 {
3252                   while (block && TREE_CODE (block) == BLOCK)
3253                     block = BLOCK_SUPERCONTEXT (block);
3254
3255                   if (block && TREE_CODE (block) == FUNCTION_DECL)
3256                     fndecl = block;
3257                   abstract_origin = NULL;
3258                 }
3259               if (fndecl)
3260                 {
3261                   expanded_location s = expand_location (*locus);
3262                   pp_character (context->printer, ',');
3263                   pp_newline (context->printer);
3264                   if (s.file != NULL)
3265                     {
3266                       if (context->show_column && s.column != 0)
3267                         pp_printf (context->printer,
3268                                    _("    inlined from %qs at %r%s:%d:%d%R"),
3269                                    cxx_printable_name_translate (fndecl, 2),
3270                                    "locus", s.file, s.line, s.column);
3271                       else
3272                         pp_printf (context->printer,
3273                                    _("    inlined from %qs at %r%s:%d%R"),
3274                                    cxx_printable_name_translate (fndecl, 2),
3275                                    "locus", s.file, s.line);
3276
3277                     }
3278                   else
3279                     pp_printf (context->printer, _("    inlined from %qs"),
3280                                cxx_printable_name_translate (fndecl, 2));
3281                 }
3282             }
3283           pp_character (context->printer, ':');
3284         }
3285       pp_newline (context->printer);
3286
3287       diagnostic_set_last_function (context, diagnostic);
3288       pp_destroy_prefix (context->printer);
3289       context->printer->prefix = old_prefix;
3290     }
3291 }
3292
3293 /* Returns a description of FUNCTION using standard terminology.  The
3294    result is a format string of the form "In CATEGORY %qs".  */
3295 static const char *
3296 function_category (tree fn)
3297 {
3298   /* We can get called from the middle-end for diagnostics of function
3299      clones.  Make sure we have language specific information before
3300      dereferencing it.  */
3301   if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3302       && DECL_FUNCTION_MEMBER_P (fn))
3303     {
3304       if (DECL_STATIC_FUNCTION_P (fn))
3305         return _("In static member function %qs");
3306       else if (DECL_COPY_CONSTRUCTOR_P (fn))
3307         return _("In copy constructor %qs");
3308       else if (DECL_CONSTRUCTOR_P (fn))
3309         return _("In constructor %qs");
3310       else if (DECL_DESTRUCTOR_P (fn))
3311         return _("In destructor %qs");
3312       else if (LAMBDA_FUNCTION_P (fn))
3313         return _("In lambda function");
3314       else
3315         return _("In member function %qs");
3316     }
3317   else
3318     return _("In function %qs");
3319 }
3320
3321 /* Report the full context of a current template instantiation,
3322    onto BUFFER.  */
3323 static void
3324 print_instantiation_full_context (diagnostic_context *context)
3325 {
3326   struct tinst_level *p = current_instantiation ();
3327   location_t location = input_location;
3328
3329   if (p)
3330     {
3331       pp_verbatim (context->printer,
3332                    TREE_CODE (p->decl) == TREE_LIST
3333                    ? _("%s: In substitution of %qS:\n")
3334                    : _("%s: In instantiation of %q#D:\n"),
3335                    LOCATION_FILE (location),
3336                    p->decl);
3337
3338       location = p->locus;
3339       p = p->next;
3340     }
3341
3342   print_instantiation_partial_context (context, p, location);
3343 }
3344
3345 /* Helper function of print_instantiation_partial_context() that
3346    prints a single line of instantiation context.  */
3347
3348 static void
3349 print_instantiation_partial_context_line (diagnostic_context *context,
3350                                           const struct tinst_level *t,
3351                                           location_t loc, bool recursive_p)
3352 {
3353   if (loc == UNKNOWN_LOCATION)
3354     return;
3355
3356   expanded_location xloc = expand_location (loc);
3357
3358   if (context->show_column)
3359     pp_verbatim (context->printer, _("%r%s:%d:%d:%R   "),
3360                  "locus", xloc.file, xloc.line, xloc.column);
3361   else
3362     pp_verbatim (context->printer, _("%r%s:%d:%R   "),
3363                  "locus", xloc.file, xloc.line);
3364
3365   if (t != NULL)
3366     {
3367       if (TREE_CODE (t->decl) == TREE_LIST)
3368         pp_verbatim (context->printer,
3369                      recursive_p
3370                      ? _("recursively required by substitution of %qS\n")
3371                      : _("required by substitution of %qS\n"),
3372                      t->decl);
3373       else
3374         pp_verbatim (context->printer,
3375                      recursive_p
3376                      ? _("recursively required from %q#D\n")
3377                      : _("required from %q#D\n"),
3378                      t->decl);
3379     }
3380   else
3381     {
3382       pp_verbatim (context->printer,
3383                    recursive_p
3384                    ? _("recursively required from here\n")
3385                    : _("required from here\n"));
3386     }
3387 }
3388
3389 /* Same as print_instantiation_full_context but less verbose.  */
3390
3391 static void
3392 print_instantiation_partial_context (diagnostic_context *context,
3393                                      struct tinst_level *t0, location_t loc)
3394 {
3395   struct tinst_level *t;
3396   int n_total = 0;
3397   int n;
3398   location_t prev_loc = loc;
3399
3400   for (t = t0; t != NULL; t = t->next)
3401     if (prev_loc != t->locus)
3402       {
3403         prev_loc = t->locus;
3404         n_total++;
3405       }
3406
3407   t = t0;
3408
3409   if (template_backtrace_limit
3410       && n_total > template_backtrace_limit) 
3411     {
3412       int skip = n_total - template_backtrace_limit;
3413       int head = template_backtrace_limit / 2;
3414
3415       /* Avoid skipping just 1.  If so, skip 2.  */
3416       if (skip == 1)
3417        {
3418          skip = 2;
3419          head = (template_backtrace_limit - 1) / 2;
3420        }
3421      
3422       for (n = 0; n < head; n++)
3423         {
3424           gcc_assert (t != NULL);
3425           if (loc != t->locus)
3426             print_instantiation_partial_context_line (context, t, loc,
3427                                                       /*recursive_p=*/false);
3428           loc = t->locus;
3429           t = t->next;
3430         }
3431       if (t != NULL && skip > 0)
3432         {
3433           expanded_location xloc;
3434           xloc = expand_location (loc);
3435           if (context->show_column)
3436             pp_verbatim (context->printer,
3437                          _("%r%s:%d:%d:%R   [ skipping %d instantiation "
3438                            "contexts, use -ftemplate-backtrace-limit=0 to "
3439                            "disable ]\n"),
3440                          "locus", xloc.file, xloc.line, xloc.column, skip);
3441           else
3442             pp_verbatim (context->printer,
3443                          _("%r%s:%d:%R   [ skipping %d instantiation "
3444                            "contexts, use -ftemplate-backtrace-limit=0 to "
3445                            "disable ]\n"),
3446                          "locus", xloc.file, xloc.line, skip);
3447           
3448           do {
3449             loc = t->locus;
3450             t = t->next;
3451           } while (t != NULL && --skip > 0);
3452         }
3453     }
3454   
3455   while (t != NULL)
3456     {
3457       while (t->next != NULL && t->locus == t->next->locus)
3458         {
3459           loc = t->locus;
3460           t = t->next;
3461         }
3462       print_instantiation_partial_context_line (context, t, loc,
3463                                                 t->locus == loc);
3464       loc = t->locus;
3465       t = t->next;
3466     }
3467   print_instantiation_partial_context_line (context, NULL, loc,
3468                                             /*recursive_p=*/false);
3469 }
3470
3471 /* Called from cp_thing to print the template context for an error.  */
3472 static void
3473 maybe_print_instantiation_context (diagnostic_context *context)
3474 {
3475   if (!problematic_instantiation_changed () || current_instantiation () == 0)
3476     return;
3477
3478   record_last_problematic_instantiation ();
3479   print_instantiation_full_context (context);
3480 }
3481 \f
3482 /* Report what constexpr call(s) we're trying to expand, if any.  */
3483
3484 void
3485 maybe_print_constexpr_context (diagnostic_context *context)
3486 {
3487   vec<tree> call_stack = cx_error_context ();
3488   unsigned ix;
3489   tree t;
3490
3491   FOR_EACH_VEC_ELT (call_stack, ix, t)
3492     {
3493       expanded_location xloc = expand_location (EXPR_LOCATION (t));
3494       const char *s = expr_as_string (t, 0);
3495       if (context->show_column)
3496         pp_verbatim (context->printer,
3497                      _("%r%s:%d:%d:%R   in constexpr expansion of %qs"),
3498                      "locus", xloc.file, xloc.line, xloc.column, s);
3499       else
3500         pp_verbatim (context->printer,
3501                      _("%r%s:%d:%R   in constexpr expansion of %qs"),
3502                      "locus", xloc.file, xloc.line, s);
3503       pp_newline (context->printer);
3504     }
3505 }
3506 \f
3507 /* Called from output_format -- during diagnostic message processing --
3508    to handle C++ specific format specifier with the following meanings:
3509    %A   function argument-list.
3510    %C   tree code.
3511    %D   declaration.
3512    %E   expression.
3513    %F   function declaration.
3514    %L   language as used in extern "lang".
3515    %O   binary operator.
3516    %P   function parameter whose position is indicated by an integer.
3517    %Q   assignment operator.
3518    %S   substitution (template + args)
3519    %T   type.
3520    %V   cv-qualifier.
3521    %X   exception-specification.  */
3522 static bool
3523 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3524             int precision, bool wide, bool set_locus, bool verbose)
3525 {
3526   const char *result;
3527   tree t = NULL;
3528 #define next_tree    (t = va_arg (*text->args_ptr, tree))
3529 #define next_tcode   ((enum tree_code) va_arg (*text->args_ptr, int))
3530 #define next_lang    ((enum languages) va_arg (*text->args_ptr, int))
3531 #define next_int     va_arg (*text->args_ptr, int)
3532
3533   if (precision != 0 || wide)
3534     return false;
3535
3536   switch (*spec)
3537     {
3538     case 'A': result = args_to_string (next_tree, verbose);     break;
3539     case 'C': result = code_to_string (next_tcode);             break;
3540     case 'D':
3541       {
3542         tree temp = next_tree;
3543         if (VAR_P (temp)
3544             && DECL_HAS_DEBUG_EXPR_P (temp))
3545           {
3546             temp = DECL_DEBUG_EXPR (temp);
3547             if (!DECL_P (temp))
3548               {
3549                 result = expr_to_string (temp);
3550                 break;
3551               }
3552           }
3553         result = decl_to_string (temp, verbose);
3554       }
3555       break;
3556     case 'E': result = expr_to_string (next_tree);              break;
3557     case 'F': result = fndecl_to_string (next_tree, verbose);   break;
3558     case 'L': result = language_to_string (next_lang);          break;
3559     case 'O': result = op_to_string (next_tcode);               break;
3560     case 'P': result = parm_to_string (next_int);               break;
3561     case 'Q': result = assop_to_string (next_tcode);            break;
3562     case 'S': result = subst_to_string (next_tree);             break;
3563     case 'T': result = type_to_string (next_tree, verbose);     break;
3564     case 'V': result = cv_to_string (next_tree, verbose);       break;
3565     case 'X': result = eh_spec_to_string (next_tree, verbose);  break;
3566
3567     case 'K':
3568       percent_K_format (text);
3569       return true;
3570
3571     default:
3572       return false;
3573     }
3574
3575   pp_string (pp, result);
3576   if (set_locus && t != NULL)
3577     text->set_location (0, location_of (t), true);
3578   return true;
3579 #undef next_tree
3580 #undef next_tcode
3581 #undef next_lang
3582 #undef next_int
3583 }
3584 \f
3585 /* Warn about the use of C++0x features when appropriate.  */
3586 void
3587 maybe_warn_cpp0x (cpp0x_warn_str str)
3588 {
3589   if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
3590     /* We really want to suppress this warning in system headers,
3591        because libstdc++ uses variadic templates even when we aren't
3592        in C++0x mode. */
3593     switch (str)
3594       {
3595       case CPP0X_INITIALIZER_LISTS:
3596         pedwarn (input_location, 0, 
3597                  "extended initializer lists "
3598                  "only available with -std=c++11 or -std=gnu++11");
3599         break;
3600       case CPP0X_EXPLICIT_CONVERSION:
3601         pedwarn (input_location, 0,
3602                  "explicit conversion operators "
3603                  "only available with -std=c++11 or -std=gnu++11");
3604         break;
3605       case CPP0X_VARIADIC_TEMPLATES:
3606         pedwarn (input_location, 0,
3607                  "variadic templates "
3608                  "only available with -std=c++11 or -std=gnu++11");
3609         break;
3610       case CPP0X_LAMBDA_EXPR:
3611         pedwarn (input_location, 0,
3612                  "lambda expressions "
3613                   "only available with -std=c++11 or -std=gnu++11");
3614         break;
3615       case CPP0X_AUTO:
3616         pedwarn (input_location, 0,
3617                  "C++11 auto only available with -std=c++11 or -std=gnu++11");
3618         break;
3619       case CPP0X_SCOPED_ENUMS:
3620         pedwarn (input_location, 0,
3621                  "scoped enums only available with -std=c++11 or -std=gnu++11");
3622         break;
3623       case CPP0X_DEFAULTED_DELETED:
3624         pedwarn (input_location, 0,
3625                  "defaulted and deleted functions "
3626                  "only available with -std=c++11 or -std=gnu++11");
3627         break;
3628       case CPP0X_INLINE_NAMESPACES:
3629         pedwarn (input_location, OPT_Wpedantic,
3630                  "inline namespaces "
3631                  "only available with -std=c++11 or -std=gnu++11");
3632         break;
3633       case CPP0X_OVERRIDE_CONTROLS:
3634         pedwarn (input_location, 0,
3635                  "override controls (override/final) "
3636                  "only available with -std=c++11 or -std=gnu++11");
3637         break;
3638       case CPP0X_NSDMI:
3639         pedwarn (input_location, 0,
3640                  "non-static data member initializers "
3641                  "only available with -std=c++11 or -std=gnu++11");
3642         break;
3643       case CPP0X_USER_DEFINED_LITERALS:
3644         pedwarn (input_location, 0,
3645                  "user-defined literals "
3646                  "only available with -std=c++11 or -std=gnu++11");
3647         break;
3648       case CPP0X_DELEGATING_CTORS:
3649         pedwarn (input_location, 0,
3650                  "delegating constructors "
3651                  "only available with -std=c++11 or -std=gnu++11");
3652         break;
3653       case CPP0X_INHERITING_CTORS:
3654         pedwarn (input_location, 0,
3655                  "inheriting constructors "
3656                  "only available with -std=c++11 or -std=gnu++11");
3657         break;
3658       case CPP0X_ATTRIBUTES:
3659         pedwarn (input_location, 0,
3660                  "c++11 attributes "
3661                  "only available with -std=c++11 or -std=gnu++11");
3662         break;
3663       case CPP0X_REF_QUALIFIER:
3664         pedwarn (input_location, 0,
3665                  "ref-qualifiers "
3666                  "only available with -std=c++11 or -std=gnu++11");
3667         break;
3668       default:
3669         gcc_unreachable ();
3670       }
3671 }
3672
3673 /* Warn about the use of variadic templates when appropriate.  */
3674 void
3675 maybe_warn_variadic_templates (void)
3676 {
3677   maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3678 }
3679
3680
3681 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3682    option OPT with text GMSGID.  Use this function to report
3683    diagnostics for constructs that are invalid C++98, but valid
3684    C++0x.  */
3685 bool
3686 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3687 {
3688   diagnostic_info diagnostic;
3689   va_list ap;
3690   bool ret;
3691   rich_location richloc (line_table, location);
3692
3693   va_start (ap, gmsgid);
3694   diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
3695                        (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3696   diagnostic.option_index = opt;
3697   ret = report_diagnostic (&diagnostic);
3698   va_end (ap);
3699   return ret;
3700 }
3701
3702 /* Issue a diagnostic that NAME cannot be found in SCOPE.  DECL is what
3703    we found when we tried to do the lookup.  LOCATION is the location of
3704    the NAME identifier.  */
3705
3706 void
3707 qualified_name_lookup_error (tree scope, tree name,
3708                              tree decl, location_t location)
3709 {
3710   if (scope == error_mark_node)
3711     ; /* We already complained.  */
3712   else if (TYPE_P (scope))
3713     {
3714       if (!COMPLETE_TYPE_P (scope))
3715         error_at (location, "incomplete type %qT used in nested name specifier",
3716                   scope);
3717       else if (TREE_CODE (decl) == TREE_LIST)
3718         {
3719           error_at (location, "reference to %<%T::%D%> is ambiguous",
3720                     scope, name);
3721           print_candidates (decl);
3722         }
3723       else
3724         error_at (location, "%qD is not a member of %qT", name, scope);
3725     }
3726   else if (scope != global_namespace)
3727     {
3728       error_at (location, "%qD is not a member of %qD", name, scope);
3729       suggest_alternatives_for (location, name);
3730     }
3731   else
3732     {
3733       error_at (location, "%<::%D%> has not been declared", name);
3734       suggest_alternatives_for (location, name);
3735     }
3736 }