OSDN Git Service

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