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