gcc/
[official-gcc.git] / gcc / cp / error.c
blob3710046bbef13d170708a035e09cec67d1e96bcc
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "diagnostic.h"
31 #include "tree-diagnostic.h"
32 #include "langhooks-def.h"
33 #include "intl.h"
34 #include "cxx-pretty-print.h"
35 #include "tree-pretty-print.h"
36 #include "c-family/c-objc.h"
37 #include "ubsan.h"
38 #include "internal-fn.h"
40 #include <new> // For placement-new.
42 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
43 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
45 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
46 dump C++ ASTs as strings. It is mostly used only by the various
47 tree -> string functions that are occasionally called from the
48 debugger or by the front-end for things like
49 __PRETTY_FUNCTION__. */
50 static cxx_pretty_printer actual_pretty_printer;
51 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
53 /* Translate if being used for diagnostics, but not for dump files or
54 __PRETTY_FUNCTION. */
55 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
57 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
59 static const char *args_to_string (tree, int);
60 static const char *assop_to_string (enum tree_code);
61 static const char *code_to_string (enum tree_code);
62 static const char *cv_to_string (tree, int);
63 static const char *decl_to_string (tree, int);
64 static const char *expr_to_string (tree);
65 static const char *fndecl_to_string (tree, int);
66 static const char *op_to_string (enum tree_code);
67 static const char *parm_to_string (int);
68 static const char *type_to_string (tree, int);
70 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
71 static void dump_type (cxx_pretty_printer *, tree, int);
72 static void dump_typename (cxx_pretty_printer *, tree, int);
73 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
74 static void dump_decl (cxx_pretty_printer *, tree, int);
75 static void dump_template_decl (cxx_pretty_printer *, tree, int);
76 static void dump_function_decl (cxx_pretty_printer *, tree, int);
77 static void dump_expr (cxx_pretty_printer *, tree, int);
78 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
79 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
80 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
81 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
82 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
83 static void dump_function_name (cxx_pretty_printer *, tree, int);
84 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
85 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
86 static void dump_expr_list (cxx_pretty_printer *, tree, int);
87 static void dump_global_iord (cxx_pretty_printer *, tree);
88 static void dump_parameters (cxx_pretty_printer *, tree, int);
89 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
90 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
91 static void dump_template_argument (cxx_pretty_printer *, tree, int);
92 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
93 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
94 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
95 vec<tree, va_gc> *);
96 static void dump_scope (cxx_pretty_printer *, tree, int);
97 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
98 static int get_non_default_template_args_count (tree, int);
99 static const char *function_category (tree);
100 static void maybe_print_constexpr_context (diagnostic_context *);
101 static void maybe_print_instantiation_context (diagnostic_context *);
102 static void print_instantiation_full_context (diagnostic_context *);
103 static void print_instantiation_partial_context (diagnostic_context *,
104 struct tinst_level *,
105 location_t);
106 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
107 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
109 static bool cp_printer (pretty_printer *, text_info *, const char *,
110 int, bool, bool, bool);
112 /* CONTEXT->printer is a basic pretty printer that was constructed
113 presumably by diagnostic_initialize(), called early in the
114 compiler's initialization process (in general_init) Before the FE
115 is initialized. This (C++) FE-specific diagnostic initializer is
116 thus replacing the basic pretty printer with one that has C++-aware
117 capacities. */
119 void
120 cxx_initialize_diagnostics (diagnostic_context *context)
122 pretty_printer *base = context->printer;
123 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
124 context->printer = new (pp) cxx_pretty_printer ();
126 /* It is safe to free this object because it was previously XNEW()'d. */
127 base->~pretty_printer ();
128 XDELETE (base);
130 c_common_diagnostics_set_defaults (context);
131 diagnostic_starter (context) = cp_diagnostic_starter;
132 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
133 diagnostic_format_decoder (context) = cp_printer;
136 /* Dump a scope, if deemed necessary. */
138 static void
139 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
141 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
143 if (scope == NULL_TREE)
144 return;
146 if (TREE_CODE (scope) == NAMESPACE_DECL)
148 if (scope != global_namespace)
150 dump_decl (pp, scope, f);
151 pp_cxx_colon_colon (pp);
154 else if (AGGREGATE_TYPE_P (scope))
156 dump_type (pp, scope, f);
157 pp_cxx_colon_colon (pp);
159 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
161 dump_function_decl (pp, scope, f);
162 pp_cxx_colon_colon (pp);
166 /* Dump the template ARGument under control of FLAGS. */
168 static void
169 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
171 if (ARGUMENT_PACK_P (arg))
172 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
173 /* No default args in argument packs. */
174 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
175 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
176 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
177 else
179 if (TREE_CODE (arg) == TREE_LIST)
180 arg = TREE_VALUE (arg);
182 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
186 /* Count the number of template arguments ARGS whose value does not
187 match the (optional) default template parameter in PARAMS */
189 static int
190 get_non_default_template_args_count (tree args, int flags)
192 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
194 if (/* We use this flag when generating debug information. We don't
195 want to expand templates at this point, for this may generate
196 new decls, which gets decl counts out of sync, which may in
197 turn cause codegen differences between compilations with and
198 without -g. */
199 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
200 || !flag_pretty_templates)
201 return n;
203 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
206 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
207 of FLAGS. */
209 static void
210 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
212 int n = get_non_default_template_args_count (args, flags);
213 int need_comma = 0;
214 int i;
216 for (i = 0; i < n; ++i)
218 tree arg = TREE_VEC_ELT (args, i);
220 /* Only print a comma if we know there is an argument coming. In
221 the case of an empty template argument pack, no actual
222 argument will be printed. */
223 if (need_comma
224 && (!ARGUMENT_PACK_P (arg)
225 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
226 pp_separate_with_comma (pp);
228 dump_template_argument (pp, arg, flags);
229 need_comma = 1;
233 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
235 static void
236 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
238 tree p;
239 tree a;
241 if (parm == error_mark_node)
242 return;
244 p = TREE_VALUE (parm);
245 a = TREE_PURPOSE (parm);
247 if (TREE_CODE (p) == TYPE_DECL)
249 if (flags & TFF_DECL_SPECIFIERS)
251 pp_cxx_ws_string (pp, "class");
252 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
253 pp_cxx_ws_string (pp, "...");
254 if (DECL_NAME (p))
255 pp_cxx_tree_identifier (pp, DECL_NAME (p));
257 else if (DECL_NAME (p))
258 pp_cxx_tree_identifier (pp, DECL_NAME (p));
259 else
260 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
262 else
263 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
265 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
267 pp_cxx_whitespace (pp);
268 pp_equal (pp);
269 pp_cxx_whitespace (pp);
270 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
271 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
272 else
273 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
277 /* Dump, under control of FLAGS, a template-parameter-list binding.
278 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
279 TREE_VEC. */
281 static void
282 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
283 vec<tree, va_gc> *typenames)
285 bool need_semicolon = false;
286 int i;
287 tree t;
289 while (parms)
291 tree p = TREE_VALUE (parms);
292 int lvl = TMPL_PARMS_DEPTH (parms);
293 int arg_idx = 0;
294 int i;
295 tree lvl_args = NULL_TREE;
297 /* Don't crash if we had an invalid argument list. */
298 if (TMPL_ARGS_DEPTH (args) >= lvl)
299 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
301 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
303 tree arg = NULL_TREE;
305 /* Don't crash if we had an invalid argument list. */
306 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
307 arg = TREE_VEC_ELT (lvl_args, arg_idx);
309 if (need_semicolon)
310 pp_separate_with_semicolon (pp);
311 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
312 TFF_PLAIN_IDENTIFIER);
313 pp_cxx_whitespace (pp);
314 pp_equal (pp);
315 pp_cxx_whitespace (pp);
316 if (arg)
318 if (ARGUMENT_PACK_P (arg))
319 pp_cxx_left_brace (pp);
320 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
321 if (ARGUMENT_PACK_P (arg))
322 pp_cxx_right_brace (pp);
324 else
325 pp_string (pp, M_("<missing>"));
327 ++arg_idx;
328 need_semicolon = true;
331 parms = TREE_CHAIN (parms);
334 /* Don't bother with typenames for a partial instantiation. */
335 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
336 return;
338 /* Don't try to print typenames when we're processing a clone. */
339 if (current_function_decl
340 && !DECL_LANG_SPECIFIC (current_function_decl))
341 return;
343 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
345 if (need_semicolon)
346 pp_separate_with_semicolon (pp);
347 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
348 pp_cxx_whitespace (pp);
349 pp_equal (pp);
350 pp_cxx_whitespace (pp);
351 push_deferring_access_checks (dk_no_check);
352 t = tsubst (t, args, tf_none, NULL_TREE);
353 pop_deferring_access_checks ();
354 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
355 pp_simple_type_specifier doesn't know about it. */
356 t = strip_typedefs (t);
357 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
361 /* Dump a human-readable equivalent of the alias template
362 specialization of T. */
364 static void
365 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
367 tree name;
369 gcc_assert (alias_template_specialization_p (t));
371 if (!(flags & TFF_UNQUALIFIED_NAME))
372 dump_scope (pp, CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
373 name = TYPE_IDENTIFIER (t);
374 pp_cxx_tree_identifier (pp, name);
375 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
376 /*primary=*/false,
377 flags & ~TFF_TEMPLATE_HEADER);
380 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
381 format. */
383 static void
384 dump_type (cxx_pretty_printer *pp, tree t, int flags)
386 if (t == NULL_TREE)
387 return;
389 /* Don't print e.g. "struct mytypedef". */
390 if (TYPE_P (t) && typedef_variant_p (t))
392 tree decl = TYPE_NAME (t);
393 if ((flags & TFF_CHASE_TYPEDEF)
394 || DECL_SELF_REFERENCE_P (decl)
395 || (!flag_pretty_templates
396 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
397 t = strip_typedefs (t);
398 else if (alias_template_specialization_p (t))
400 dump_alias_template_specialization (pp, t, flags);
401 return;
403 else if (same_type_p (t, TREE_TYPE (decl)))
404 t = decl;
405 else
407 pp_cxx_cv_qualifier_seq (pp, t);
408 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
409 return;
413 if (TYPE_PTRMEMFUNC_P (t))
414 goto offset_type;
416 switch (TREE_CODE (t))
418 case LANG_TYPE:
419 if (t == init_list_type_node)
420 pp_string (pp, M_("<brace-enclosed initializer list>"));
421 else if (t == unknown_type_node)
422 pp_string (pp, M_("<unresolved overloaded function type>"));
423 else
425 pp_cxx_cv_qualifier_seq (pp, t);
426 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
428 break;
430 case TREE_LIST:
431 /* A list of function parms. */
432 dump_parameters (pp, t, flags);
433 break;
435 case IDENTIFIER_NODE:
436 pp_cxx_tree_identifier (pp, t);
437 break;
439 case TREE_BINFO:
440 dump_type (pp, BINFO_TYPE (t), flags);
441 break;
443 case RECORD_TYPE:
444 case UNION_TYPE:
445 case ENUMERAL_TYPE:
446 dump_aggr_type (pp, t, flags);
447 break;
449 case TYPE_DECL:
450 if (flags & TFF_CHASE_TYPEDEF)
452 dump_type (pp, DECL_ORIGINAL_TYPE (t)
453 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
454 break;
456 /* Else fall through. */
458 case TEMPLATE_DECL:
459 case NAMESPACE_DECL:
460 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
461 break;
463 case INTEGER_TYPE:
464 case REAL_TYPE:
465 case VOID_TYPE:
466 case BOOLEAN_TYPE:
467 case COMPLEX_TYPE:
468 case VECTOR_TYPE:
469 case FIXED_POINT_TYPE:
470 pp_type_specifier_seq (pp, t);
471 break;
473 case TEMPLATE_TEMPLATE_PARM:
474 /* For parameters inside template signature. */
475 if (TYPE_IDENTIFIER (t))
476 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
477 else
478 pp_cxx_canonical_template_parameter (pp, t);
479 break;
481 case BOUND_TEMPLATE_TEMPLATE_PARM:
483 tree args = TYPE_TI_ARGS (t);
484 pp_cxx_cv_qualifier_seq (pp, t);
485 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
486 pp_cxx_begin_template_argument_list (pp);
487 dump_template_argument_list (pp, args, flags);
488 pp_cxx_end_template_argument_list (pp);
490 break;
492 case TEMPLATE_TYPE_PARM:
493 pp_cxx_cv_qualifier_seq (pp, t);
494 if (TYPE_IDENTIFIER (t))
495 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
496 else
497 pp_cxx_canonical_template_parameter
498 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
499 break;
501 /* This is not always necessary for pointers and such, but doing this
502 reduces code size. */
503 case ARRAY_TYPE:
504 case POINTER_TYPE:
505 case REFERENCE_TYPE:
506 case OFFSET_TYPE:
507 offset_type:
508 case FUNCTION_TYPE:
509 case METHOD_TYPE:
511 dump_type_prefix (pp, t, flags);
512 dump_type_suffix (pp, t, flags);
513 break;
515 case TYPENAME_TYPE:
516 if (! (flags & TFF_CHASE_TYPEDEF)
517 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
519 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
520 break;
522 pp_cxx_cv_qualifier_seq (pp, t);
523 pp_cxx_ws_string (pp,
524 TYPENAME_IS_ENUM_P (t) ? "enum"
525 : TYPENAME_IS_CLASS_P (t) ? "class"
526 : "typename");
527 dump_typename (pp, t, flags);
528 break;
530 case UNBOUND_CLASS_TEMPLATE:
531 if (! (flags & TFF_UNQUALIFIED_NAME))
533 dump_type (pp, TYPE_CONTEXT (t), flags);
534 pp_cxx_colon_colon (pp);
536 pp_cxx_ws_string (pp, "template");
537 dump_type (pp, TYPE_IDENTIFIER (t), flags);
538 break;
540 case TYPEOF_TYPE:
541 pp_cxx_ws_string (pp, "__typeof__");
542 pp_cxx_whitespace (pp);
543 pp_cxx_left_paren (pp);
544 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
545 pp_cxx_right_paren (pp);
546 break;
548 case UNDERLYING_TYPE:
549 pp_cxx_ws_string (pp, "__underlying_type");
550 pp_cxx_whitespace (pp);
551 pp_cxx_left_paren (pp);
552 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
553 pp_cxx_right_paren (pp);
554 break;
556 case TYPE_PACK_EXPANSION:
557 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
558 pp_cxx_ws_string (pp, "...");
559 break;
561 case TYPE_ARGUMENT_PACK:
562 dump_template_argument (pp, t, flags);
563 break;
565 case DECLTYPE_TYPE:
566 pp_cxx_ws_string (pp, "decltype");
567 pp_cxx_whitespace (pp);
568 pp_cxx_left_paren (pp);
569 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
570 pp_cxx_right_paren (pp);
571 break;
573 case NULLPTR_TYPE:
574 pp_string (pp, "std::nullptr_t");
575 break;
577 default:
578 pp_unsupported_tree (pp, t);
579 /* Fall through to error. */
581 case ERROR_MARK:
582 pp_string (pp, M_("<type error>"));
583 break;
587 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
588 a TYPENAME_TYPE. */
590 static void
591 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
593 tree ctx = TYPE_CONTEXT (t);
595 if (TREE_CODE (ctx) == TYPENAME_TYPE)
596 dump_typename (pp, ctx, flags);
597 else
598 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
599 pp_cxx_colon_colon (pp);
600 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
603 /* Return the name of the supplied aggregate, or enumeral type. */
605 const char *
606 class_key_or_enum_as_string (tree t)
608 if (TREE_CODE (t) == ENUMERAL_TYPE)
610 if (SCOPED_ENUM_P (t))
611 return "enum class";
612 else
613 return "enum";
615 else if (TREE_CODE (t) == UNION_TYPE)
616 return "union";
617 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
618 return "class";
619 else
620 return "struct";
623 /* Print out a class declaration T under the control of FLAGS,
624 in the form `class foo'. */
626 static void
627 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
629 tree name;
630 const char *variety = class_key_or_enum_as_string (t);
631 int typdef = 0;
632 int tmplate = 0;
634 pp_cxx_cv_qualifier_seq (pp, t);
636 if (flags & TFF_CLASS_KEY_OR_ENUM)
637 pp_cxx_ws_string (pp, variety);
639 name = TYPE_NAME (t);
641 if (name)
643 typdef = (!DECL_ARTIFICIAL (name)
644 /* An alias specialization is not considered to be a
645 typedef. */
646 && !alias_template_specialization_p (t));
648 if ((typdef
649 && ((flags & TFF_CHASE_TYPEDEF)
650 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
651 && DECL_TEMPLATE_INFO (name))))
652 || DECL_SELF_REFERENCE_P (name))
654 t = TYPE_MAIN_VARIANT (t);
655 name = TYPE_NAME (t);
656 typdef = 0;
659 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
660 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
661 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
662 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
664 if (! (flags & TFF_UNQUALIFIED_NAME))
665 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
666 flags &= ~TFF_UNQUALIFIED_NAME;
667 if (tmplate)
669 /* Because the template names are mangled, we have to locate
670 the most general template, and use that name. */
671 tree tpl = TYPE_TI_TEMPLATE (t);
673 while (DECL_TEMPLATE_INFO (tpl))
674 tpl = DECL_TI_TEMPLATE (tpl);
675 name = tpl;
677 name = DECL_NAME (name);
680 if (name == 0 || anon_aggrname_p (name))
682 if (flags & TFF_CLASS_KEY_OR_ENUM)
683 pp_string (pp, M_("<anonymous>"));
684 else
685 pp_printf (pp, M_("<anonymous %s>"), variety);
687 else if (LAMBDA_TYPE_P (t))
689 /* A lambda's "type" is essentially its signature. */
690 pp_string (pp, M_("<lambda"));
691 if (lambda_function (t))
692 dump_parameters (pp,
693 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
694 flags);
695 pp_greater (pp);
697 else
698 pp_cxx_tree_identifier (pp, name);
699 if (tmplate)
700 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
701 !CLASSTYPE_USE_TEMPLATE (t),
702 flags & ~TFF_TEMPLATE_HEADER);
705 /* Dump into the obstack the initial part of the output for a given type.
706 This is necessary when dealing with things like functions returning
707 functions. Examples:
709 return type of `int (* fee ())()': pointer -> function -> int. Both
710 pointer (and reference and offset) and function (and member) types must
711 deal with prefix and suffix.
713 Arrays must also do this for DECL nodes, like int a[], and for things like
714 int *[]&. */
716 static void
717 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
719 if (TYPE_PTRMEMFUNC_P (t))
721 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
722 goto offset_type;
725 switch (TREE_CODE (t))
727 case POINTER_TYPE:
728 case REFERENCE_TYPE:
730 tree sub = TREE_TYPE (t);
732 dump_type_prefix (pp, sub, flags);
733 if (TREE_CODE (sub) == ARRAY_TYPE
734 || TREE_CODE (sub) == FUNCTION_TYPE)
736 pp_cxx_whitespace (pp);
737 pp_cxx_left_paren (pp);
738 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
740 if (TYPE_PTR_P (t))
741 pp_star (pp);
742 else if (TREE_CODE (t) == REFERENCE_TYPE)
744 if (TYPE_REF_IS_RVALUE (t))
745 pp_ampersand_ampersand (pp);
746 else
747 pp_ampersand (pp);
749 pp->padding = pp_before;
750 pp_cxx_cv_qualifier_seq (pp, t);
752 break;
754 case OFFSET_TYPE:
755 offset_type:
756 dump_type_prefix (pp, TREE_TYPE (t), flags);
757 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
759 pp_maybe_space (pp);
760 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
761 pp_cxx_left_paren (pp);
762 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
763 pp_cxx_colon_colon (pp);
765 pp_cxx_star (pp);
766 pp_cxx_cv_qualifier_seq (pp, t);
767 pp->padding = pp_before;
768 break;
770 /* This can be reached without a pointer when dealing with
771 templates, e.g. std::is_function. */
772 case FUNCTION_TYPE:
773 dump_type_prefix (pp, TREE_TYPE (t), flags);
774 break;
776 case METHOD_TYPE:
777 dump_type_prefix (pp, TREE_TYPE (t), flags);
778 pp_maybe_space (pp);
779 pp_cxx_left_paren (pp);
780 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
781 pp_cxx_colon_colon (pp);
782 break;
784 case ARRAY_TYPE:
785 dump_type_prefix (pp, TREE_TYPE (t), flags);
786 break;
788 case ENUMERAL_TYPE:
789 case IDENTIFIER_NODE:
790 case INTEGER_TYPE:
791 case BOOLEAN_TYPE:
792 case REAL_TYPE:
793 case RECORD_TYPE:
794 case TEMPLATE_TYPE_PARM:
795 case TEMPLATE_TEMPLATE_PARM:
796 case BOUND_TEMPLATE_TEMPLATE_PARM:
797 case TREE_LIST:
798 case TYPE_DECL:
799 case TREE_VEC:
800 case UNION_TYPE:
801 case LANG_TYPE:
802 case VOID_TYPE:
803 case TYPENAME_TYPE:
804 case COMPLEX_TYPE:
805 case VECTOR_TYPE:
806 case TYPEOF_TYPE:
807 case UNDERLYING_TYPE:
808 case DECLTYPE_TYPE:
809 case TYPE_PACK_EXPANSION:
810 case FIXED_POINT_TYPE:
811 case NULLPTR_TYPE:
812 dump_type (pp, t, flags);
813 pp->padding = pp_before;
814 break;
816 default:
817 pp_unsupported_tree (pp, t);
818 /* fall through. */
819 case ERROR_MARK:
820 pp_string (pp, M_("<typeprefixerror>"));
821 break;
825 /* Dump the suffix of type T, under control of FLAGS. This is the part
826 which appears after the identifier (or function parms). */
828 static void
829 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
831 if (TYPE_PTRMEMFUNC_P (t))
832 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
834 switch (TREE_CODE (t))
836 case POINTER_TYPE:
837 case REFERENCE_TYPE:
838 case OFFSET_TYPE:
839 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
840 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
841 pp_cxx_right_paren (pp);
842 if (TREE_CODE (t) == POINTER_TYPE)
843 flags |= TFF_POINTER;
844 dump_type_suffix (pp, TREE_TYPE (t), flags);
845 break;
847 case FUNCTION_TYPE:
848 case METHOD_TYPE:
850 tree arg;
851 if (TREE_CODE (t) == METHOD_TYPE)
852 /* Can only be reached through a pointer. */
853 pp_cxx_right_paren (pp);
854 arg = TYPE_ARG_TYPES (t);
855 if (TREE_CODE (t) == METHOD_TYPE)
856 arg = TREE_CHAIN (arg);
858 /* Function pointers don't have default args. Not in standard C++,
859 anyway; they may in g++, but we'll just pretend otherwise. */
860 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
862 pp->padding = pp_before;
863 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
864 TREE_CODE (t) == FUNCTION_TYPE
865 && (flags & TFF_POINTER));
866 dump_ref_qualifier (pp, t, flags);
867 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
868 dump_type_suffix (pp, TREE_TYPE (t), flags);
869 break;
872 case ARRAY_TYPE:
873 pp_maybe_space (pp);
874 pp_cxx_left_bracket (pp);
875 if (TYPE_DOMAIN (t))
877 tree dtype = TYPE_DOMAIN (t);
878 tree max = TYPE_MAX_VALUE (dtype);
879 if (integer_all_onesp (max))
880 pp_character (pp, '0');
881 else if (tree_fits_shwi_p (max))
882 pp_wide_integer (pp, tree_to_shwi (max) + 1);
883 else
885 STRIP_NOPS (max);
886 if (TREE_CODE (max) == SAVE_EXPR)
887 max = TREE_OPERAND (max, 0);
888 if (TREE_CODE (max) == MINUS_EXPR
889 || TREE_CODE (max) == PLUS_EXPR)
891 max = TREE_OPERAND (max, 0);
892 while (CONVERT_EXPR_P (max))
893 max = TREE_OPERAND (max, 0);
895 else
896 max = fold_build2_loc (input_location,
897 PLUS_EXPR, dtype, max,
898 build_int_cst (dtype, 1));
899 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
902 pp_cxx_right_bracket (pp);
903 dump_type_suffix (pp, TREE_TYPE (t), flags);
904 break;
906 case ENUMERAL_TYPE:
907 case IDENTIFIER_NODE:
908 case INTEGER_TYPE:
909 case BOOLEAN_TYPE:
910 case REAL_TYPE:
911 case RECORD_TYPE:
912 case TEMPLATE_TYPE_PARM:
913 case TEMPLATE_TEMPLATE_PARM:
914 case BOUND_TEMPLATE_TEMPLATE_PARM:
915 case TREE_LIST:
916 case TYPE_DECL:
917 case TREE_VEC:
918 case UNION_TYPE:
919 case LANG_TYPE:
920 case VOID_TYPE:
921 case TYPENAME_TYPE:
922 case COMPLEX_TYPE:
923 case VECTOR_TYPE:
924 case TYPEOF_TYPE:
925 case UNDERLYING_TYPE:
926 case DECLTYPE_TYPE:
927 case TYPE_PACK_EXPANSION:
928 case FIXED_POINT_TYPE:
929 case NULLPTR_TYPE:
930 break;
932 default:
933 pp_unsupported_tree (pp, t);
934 case ERROR_MARK:
935 /* Don't mark it here, we should have already done in
936 dump_type_prefix. */
937 break;
941 static void
942 dump_global_iord (cxx_pretty_printer *pp, tree t)
944 const char *p = NULL;
946 if (DECL_GLOBAL_CTOR_P (t))
947 p = M_("(static initializers for %s)");
948 else if (DECL_GLOBAL_DTOR_P (t))
949 p = M_("(static destructors for %s)");
950 else
951 gcc_unreachable ();
953 pp_printf (pp, p, LOCATION_FILE (input_location));
956 static void
957 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
959 if (flags & TFF_DECL_SPECIFIERS)
961 if (VAR_P (t)
962 && DECL_DECLARED_CONSTEXPR_P (t))
963 pp_cxx_ws_string (pp, "constexpr");
964 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
965 pp_maybe_space (pp);
967 if (! (flags & TFF_UNQUALIFIED_NAME)
968 && TREE_CODE (t) != PARM_DECL
969 && (!DECL_INITIAL (t)
970 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
971 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
972 flags &= ~TFF_UNQUALIFIED_NAME;
973 if ((flags & TFF_DECL_SPECIFIERS)
974 && DECL_TEMPLATE_PARM_P (t)
975 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
976 pp_string (pp, "...");
977 if (DECL_NAME (t))
979 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
981 pp_less (pp);
982 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
983 pp_string (pp, " capture>");
985 else
986 dump_decl (pp, DECL_NAME (t), flags);
988 else
989 pp_string (pp, M_("<anonymous>"));
990 if (flags & TFF_DECL_SPECIFIERS)
991 dump_type_suffix (pp, type, flags);
994 /* Dump a human readable string for the decl T under control of FLAGS. */
996 static void
997 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
999 if (t == NULL_TREE)
1000 return;
1002 /* If doing Objective-C++, give Objective-C a chance to demangle
1003 Objective-C method names. */
1004 if (c_dialect_objc ())
1006 const char *demangled = objc_maybe_printable_name (t, flags);
1007 if (demangled)
1009 pp_string (pp, demangled);
1010 return;
1014 switch (TREE_CODE (t))
1016 case TYPE_DECL:
1017 /* Don't say 'typedef class A' */
1018 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1020 if ((flags & TFF_DECL_SPECIFIERS)
1021 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1023 /* Say `class T' not just `T'. */
1024 pp_cxx_ws_string (pp, "class");
1026 /* Emit the `...' for a parameter pack. */
1027 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1028 pp_cxx_ws_string (pp, "...");
1031 dump_type (pp, TREE_TYPE (t), flags);
1032 break;
1034 if (TYPE_DECL_ALIAS_P (t)
1035 && (flags & TFF_DECL_SPECIFIERS
1036 || flags & TFF_CLASS_KEY_OR_ENUM))
1038 pp_cxx_ws_string (pp, "using");
1039 dump_decl (pp, DECL_NAME (t), flags);
1040 pp_cxx_whitespace (pp);
1041 pp_cxx_ws_string (pp, "=");
1042 pp_cxx_whitespace (pp);
1043 dump_type (pp, DECL_ORIGINAL_TYPE (t), flags);
1044 break;
1046 if ((flags & TFF_DECL_SPECIFIERS)
1047 && !DECL_SELF_REFERENCE_P (t))
1048 pp_cxx_ws_string (pp, "typedef");
1049 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1050 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1051 flags);
1052 break;
1054 case VAR_DECL:
1055 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1057 pp_string (pp, M_("vtable for "));
1058 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1059 dump_type (pp, DECL_CONTEXT (t), flags);
1060 break;
1062 /* Else fall through. */
1063 case FIELD_DECL:
1064 case PARM_DECL:
1065 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1067 /* Handle variable template specializations. */
1068 if (VAR_P (t)
1069 && DECL_LANG_SPECIFIC (t)
1070 && DECL_TEMPLATE_INFO (t)
1071 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1073 pp_cxx_begin_template_argument_list (pp);
1074 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1075 dump_template_argument_list (pp, args, flags);
1076 pp_cxx_end_template_argument_list (pp);
1078 break;
1080 case RESULT_DECL:
1081 pp_string (pp, M_("<return value> "));
1082 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1083 break;
1085 case NAMESPACE_DECL:
1086 if (flags & TFF_DECL_SPECIFIERS)
1087 pp->declaration (t);
1088 else
1090 if (! (flags & TFF_UNQUALIFIED_NAME))
1091 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1092 flags &= ~TFF_UNQUALIFIED_NAME;
1093 if (DECL_NAME (t) == NULL_TREE)
1095 if (!(pp->flags & pp_c_flag_gnu_v3))
1096 pp_cxx_ws_string (pp, M_("{anonymous}"));
1097 else
1098 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1100 else
1101 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1103 break;
1105 case SCOPE_REF:
1106 dump_type (pp, TREE_OPERAND (t, 0), flags);
1107 pp_colon_colon (pp);
1108 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1109 break;
1111 case ARRAY_REF:
1112 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1113 pp_cxx_left_bracket (pp);
1114 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1115 pp_cxx_right_bracket (pp);
1116 break;
1118 case ARRAY_NOTATION_REF:
1119 dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1120 pp_cxx_left_bracket (pp);
1121 dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1122 pp_colon (pp);
1123 dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1124 pp_colon (pp);
1125 dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1126 pp_cxx_right_bracket (pp);
1127 break;
1129 /* So that we can do dump_decl on an aggr type. */
1130 case RECORD_TYPE:
1131 case UNION_TYPE:
1132 case ENUMERAL_TYPE:
1133 dump_type (pp, t, flags);
1134 break;
1136 case BIT_NOT_EXPR:
1137 /* This is a pseudo destructor call which has not been folded into
1138 a PSEUDO_DTOR_EXPR yet. */
1139 pp_cxx_complement (pp);
1140 dump_type (pp, TREE_OPERAND (t, 0), flags);
1141 break;
1143 case TYPE_EXPR:
1144 gcc_unreachable ();
1145 break;
1147 /* These special cases are duplicated here so that other functions
1148 can feed identifiers to error and get them demangled properly. */
1149 case IDENTIFIER_NODE:
1150 if (IDENTIFIER_TYPENAME_P (t))
1152 pp_cxx_ws_string (pp, "operator");
1153 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1154 dump_type (pp, TREE_TYPE (t), flags);
1155 break;
1157 else
1158 pp_cxx_tree_identifier (pp, t);
1159 break;
1161 case OVERLOAD:
1162 if (OVL_CHAIN (t))
1164 t = OVL_CURRENT (t);
1165 if (DECL_CLASS_SCOPE_P (t))
1167 dump_type (pp, DECL_CONTEXT (t), flags);
1168 pp_cxx_colon_colon (pp);
1170 else if (!DECL_FILE_SCOPE_P (t))
1172 dump_decl (pp, DECL_CONTEXT (t), flags);
1173 pp_cxx_colon_colon (pp);
1175 dump_decl (pp, DECL_NAME (t), flags);
1176 break;
1179 /* If there's only one function, just treat it like an ordinary
1180 FUNCTION_DECL. */
1181 t = OVL_CURRENT (t);
1182 /* Fall through. */
1184 case FUNCTION_DECL:
1185 if (! DECL_LANG_SPECIFIC (t))
1187 if (DECL_ABSTRACT_ORIGIN (t))
1188 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1189 else
1190 pp_string (pp, M_("<built-in>"));
1192 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1193 dump_global_iord (pp, t);
1194 else
1195 dump_function_decl (pp, t, flags);
1196 break;
1198 case TEMPLATE_DECL:
1199 dump_template_decl (pp, t, flags);
1200 break;
1202 case TEMPLATE_ID_EXPR:
1204 tree name = TREE_OPERAND (t, 0);
1205 tree args = TREE_OPERAND (t, 1);
1207 if (is_overloaded_fn (name))
1208 name = get_first_fn (name);
1209 if (DECL_P (name))
1210 name = DECL_NAME (name);
1211 dump_decl (pp, name, flags);
1212 pp_cxx_begin_template_argument_list (pp);
1213 if (args == error_mark_node)
1214 pp_string (pp, M_("<template arguments error>"));
1215 else if (args)
1216 dump_template_argument_list (pp, args, flags);
1217 pp_cxx_end_template_argument_list (pp);
1219 break;
1221 case LABEL_DECL:
1222 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1223 break;
1225 case CONST_DECL:
1226 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1227 || (DECL_INITIAL (t) &&
1228 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1229 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1230 else if (DECL_NAME (t))
1231 dump_decl (pp, DECL_NAME (t), flags);
1232 else if (DECL_INITIAL (t))
1233 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1234 else
1235 pp_string (pp, M_("<enumerator>"));
1236 break;
1238 case USING_DECL:
1239 pp_cxx_ws_string (pp, "using");
1240 dump_type (pp, USING_DECL_SCOPE (t), flags);
1241 pp_cxx_colon_colon (pp);
1242 dump_decl (pp, DECL_NAME (t), flags);
1243 break;
1245 case STATIC_ASSERT:
1246 pp->declaration (t);
1247 break;
1249 case BASELINK:
1250 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1251 break;
1253 case NON_DEPENDENT_EXPR:
1254 dump_expr (pp, t, flags);
1255 break;
1257 case TEMPLATE_TYPE_PARM:
1258 if (flags & TFF_DECL_SPECIFIERS)
1259 pp->declaration (t);
1260 else
1261 pp->type_id (t);
1262 break;
1264 case UNBOUND_CLASS_TEMPLATE:
1265 case TYPE_PACK_EXPANSION:
1266 case TREE_BINFO:
1267 dump_type (pp, t, flags);
1268 break;
1270 default:
1271 pp_unsupported_tree (pp, t);
1272 /* Fall through to error. */
1274 case ERROR_MARK:
1275 pp_string (pp, M_("<declaration error>"));
1276 break;
1280 /* Dump a template declaration T under control of FLAGS. This means the
1281 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1283 static void
1284 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1286 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1287 tree parms;
1288 int i;
1290 if (flags & TFF_TEMPLATE_HEADER)
1292 for (parms = orig_parms = nreverse (orig_parms);
1293 parms;
1294 parms = TREE_CHAIN (parms))
1296 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1297 int len = TREE_VEC_LENGTH (inner_parms);
1299 pp_cxx_ws_string (pp, "template");
1300 pp_cxx_begin_template_argument_list (pp);
1302 /* If we've shown the template prefix, we'd better show the
1303 parameters' and decl's type too. */
1304 flags |= TFF_DECL_SPECIFIERS;
1306 for (i = 0; i < len; i++)
1308 if (i)
1309 pp_separate_with_comma (pp);
1310 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1311 flags);
1313 pp_cxx_end_template_argument_list (pp);
1314 pp_cxx_whitespace (pp);
1316 nreverse(orig_parms);
1318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1320 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1321 pp_cxx_ws_string (pp, "class");
1323 /* If this is a parameter pack, print the ellipsis. */
1324 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1325 pp_cxx_ws_string (pp, "...");
1329 if (DECL_CLASS_TEMPLATE_P (t))
1330 dump_type (pp, TREE_TYPE (t),
1331 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1332 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1333 else if (DECL_TEMPLATE_RESULT (t)
1334 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1335 /* Alias template. */
1336 || DECL_TYPE_TEMPLATE_P (t)))
1337 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1338 else
1340 gcc_assert (TREE_TYPE (t));
1341 switch (NEXT_CODE (t))
1343 case METHOD_TYPE:
1344 case FUNCTION_TYPE:
1345 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1346 break;
1347 default:
1348 /* This case can occur with some invalid code. */
1349 dump_type (pp, TREE_TYPE (t),
1350 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1351 | (flags & TFF_DECL_SPECIFIERS
1352 ? TFF_CLASS_KEY_OR_ENUM : 0));
1357 /* find_typenames looks through the type of the function template T
1358 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1359 it finds. */
1361 struct find_typenames_t
1363 hash_set<tree> *p_set;
1364 vec<tree, va_gc> *typenames;
1367 static tree
1368 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1370 struct find_typenames_t *d = (struct find_typenames_t *)data;
1371 tree mv = NULL_TREE;
1373 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1374 /* Add the type of the typedef without any additional cv-quals. */
1375 mv = TREE_TYPE (TYPE_NAME (*tp));
1376 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1377 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1378 /* Add the typename without any cv-qualifiers. */
1379 mv = TYPE_MAIN_VARIANT (*tp);
1381 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1383 /* Don't mess with parameter packs since we don't remember
1384 the pack expansion context for a particular typename. */
1385 *walk_subtrees = false;
1386 return NULL_TREE;
1389 if (mv && (mv == *tp || !d->p_set->add (mv)))
1390 vec_safe_push (d->typenames, mv);
1392 /* Search into class template arguments, which cp_walk_subtrees
1393 doesn't do. */
1394 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1395 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1396 data, d->p_set);
1398 return NULL_TREE;
1401 static vec<tree, va_gc> *
1402 find_typenames (tree t)
1404 struct find_typenames_t ft;
1405 ft.p_set = new hash_set<tree>;
1406 ft.typenames = NULL;
1407 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1408 find_typenames_r, &ft, ft.p_set);
1409 delete ft.p_set;
1410 return ft.typenames;
1413 /* Output the "[with ...]" clause for a template instantiation T iff
1414 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1415 formatting a deduction/substitution diagnostic rather than an
1416 instantiation. */
1418 static void
1419 dump_substitution (cxx_pretty_printer *pp,
1420 tree t, tree template_parms, tree template_args,
1421 int flags)
1423 if (template_parms != NULL_TREE && template_args != NULL_TREE
1424 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1426 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1427 pp_cxx_whitespace (pp);
1428 pp_cxx_left_bracket (pp);
1429 pp->translate_string ("with");
1430 pp_cxx_whitespace (pp);
1431 dump_template_bindings (pp, template_parms, template_args, typenames);
1432 pp_cxx_right_bracket (pp);
1436 /* Dump the lambda function FN including its 'mutable' qualifier and any
1437 template bindings. */
1439 static void
1440 dump_lambda_function (cxx_pretty_printer *pp,
1441 tree fn, tree template_parms, tree template_args,
1442 int flags)
1444 /* A lambda's signature is essentially its "type". */
1445 dump_type (pp, DECL_CONTEXT (fn), flags);
1446 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1448 pp->padding = pp_before;
1449 pp_c_ws_string (pp, "mutable");
1451 dump_substitution (pp, fn, template_parms, template_args, flags);
1454 /* Pretty print a function decl. There are several ways we want to print a
1455 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1456 As error can only apply the '#' flag once to give 0 and 1 for V, there
1457 is %D which doesn't print the throw specs, and %F which does. */
1459 static void
1460 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1462 tree fntype;
1463 tree parmtypes;
1464 tree cname = NULL_TREE;
1465 tree template_args = NULL_TREE;
1466 tree template_parms = NULL_TREE;
1467 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1468 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1469 tree exceptions;
1471 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1472 if (TREE_CODE (t) == TEMPLATE_DECL)
1473 t = DECL_TEMPLATE_RESULT (t);
1475 /* Save the exceptions, in case t is a specialization and we are
1476 emitting an error about incompatible specifications. */
1477 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1479 /* Pretty print template instantiations only. */
1480 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1481 && flag_pretty_templates)
1483 tree tmpl;
1485 template_args = DECL_TI_ARGS (t);
1486 tmpl = most_general_template (t);
1487 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1489 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1490 t = tmpl;
1494 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1495 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1497 fntype = TREE_TYPE (t);
1498 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1500 if (DECL_CLASS_SCOPE_P (t))
1501 cname = DECL_CONTEXT (t);
1502 /* This is for partially instantiated template methods. */
1503 else if (TREE_CODE (fntype) == METHOD_TYPE)
1504 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1506 if (flags & TFF_DECL_SPECIFIERS)
1508 if (DECL_STATIC_FUNCTION_P (t))
1509 pp_cxx_ws_string (pp, "static");
1510 else if (DECL_VIRTUAL_P (t))
1511 pp_cxx_ws_string (pp, "virtual");
1513 if (DECL_DECLARED_CONSTEXPR_P (t))
1514 pp_cxx_ws_string (pp, "constexpr");
1517 /* Print the return type? */
1518 if (show_return)
1519 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1520 && !DECL_DESTRUCTOR_P (t);
1521 if (show_return)
1523 tree ret = fndecl_declared_return_type (t);
1524 dump_type_prefix (pp, ret, flags);
1527 /* Print the function name. */
1528 if (!do_outer_scope)
1529 /* Nothing. */;
1530 else if (cname)
1532 dump_type (pp, cname, flags);
1533 pp_cxx_colon_colon (pp);
1535 else
1536 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1538 dump_function_name (pp, t, flags);
1540 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1542 dump_parameters (pp, parmtypes, flags);
1544 if (TREE_CODE (fntype) == METHOD_TYPE)
1546 pp->padding = pp_before;
1547 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1548 dump_ref_qualifier (pp, fntype, flags);
1551 if (flags & TFF_EXCEPTION_SPECIFICATION)
1553 pp->padding = pp_before;
1554 dump_exception_spec (pp, exceptions, flags);
1557 if (show_return)
1558 dump_type_suffix (pp, TREE_TYPE (fntype), flags);
1560 dump_substitution (pp, t, template_parms, template_args, flags);
1562 else if (template_args)
1564 bool need_comma = false;
1565 int i;
1566 pp_cxx_begin_template_argument_list (pp);
1567 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1568 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1570 tree arg = TREE_VEC_ELT (template_args, i);
1571 if (need_comma)
1572 pp_separate_with_comma (pp);
1573 if (ARGUMENT_PACK_P (arg))
1574 pp_cxx_left_brace (pp);
1575 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1576 if (ARGUMENT_PACK_P (arg))
1577 pp_cxx_right_brace (pp);
1578 need_comma = true;
1580 pp_cxx_end_template_argument_list (pp);
1584 /* Print a parameter list. If this is for a member function, the
1585 member object ptr (and any other hidden args) should have
1586 already been removed. */
1588 static void
1589 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1591 int first = 1;
1592 flags &= ~TFF_SCOPE;
1593 pp_cxx_left_paren (pp);
1595 for (first = 1; parmtypes != void_list_node;
1596 parmtypes = TREE_CHAIN (parmtypes))
1598 if (!first)
1599 pp_separate_with_comma (pp);
1600 first = 0;
1601 if (!parmtypes)
1603 pp_cxx_ws_string (pp, "...");
1604 break;
1607 dump_type (pp, TREE_VALUE (parmtypes), flags);
1609 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1611 pp_cxx_whitespace (pp);
1612 pp_equal (pp);
1613 pp_cxx_whitespace (pp);
1614 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1618 pp_cxx_right_paren (pp);
1621 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1623 static void
1624 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1626 if (FUNCTION_REF_QUALIFIED (t))
1628 pp->padding = pp_before;
1629 if (FUNCTION_RVALUE_QUALIFIED (t))
1630 pp_cxx_ws_string (pp, "&&");
1631 else
1632 pp_cxx_ws_string (pp, "&");
1636 /* Print an exception specification. T is the exception specification. */
1638 static void
1639 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1641 if (t && TREE_PURPOSE (t))
1643 pp_cxx_ws_string (pp, "noexcept");
1644 if (!integer_onep (TREE_PURPOSE (t)))
1646 pp_cxx_whitespace (pp);
1647 pp_cxx_left_paren (pp);
1648 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1649 pp_cxx_ws_string (pp, "<uninstantiated>");
1650 else
1651 dump_expr (pp, TREE_PURPOSE (t), flags);
1652 pp_cxx_right_paren (pp);
1655 else if (t)
1657 pp_cxx_ws_string (pp, "throw");
1658 pp_cxx_whitespace (pp);
1659 pp_cxx_left_paren (pp);
1660 if (TREE_VALUE (t) != NULL_TREE)
1661 while (1)
1663 dump_type (pp, TREE_VALUE (t), flags);
1664 t = TREE_CHAIN (t);
1665 if (!t)
1666 break;
1667 pp_separate_with_comma (pp);
1669 pp_cxx_right_paren (pp);
1673 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1674 and destructors properly. */
1676 static void
1677 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1679 tree name = DECL_NAME (t);
1681 /* We can get here with a decl that was synthesized by language-
1682 independent machinery (e.g. coverage.c) in which case it won't
1683 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1684 will crash. In this case it is safe just to print out the
1685 literal name. */
1686 if (!DECL_LANG_SPECIFIC (t))
1688 pp_cxx_tree_identifier (pp, name);
1689 return;
1692 if (TREE_CODE (t) == TEMPLATE_DECL)
1693 t = DECL_TEMPLATE_RESULT (t);
1695 /* Don't let the user see __comp_ctor et al. */
1696 if (DECL_CONSTRUCTOR_P (t)
1697 || DECL_DESTRUCTOR_P (t))
1699 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1700 name = get_identifier ("<lambda>");
1701 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1702 name = get_identifier ("<constructor>");
1703 else
1704 name = constructor_name (DECL_CONTEXT (t));
1707 if (DECL_DESTRUCTOR_P (t))
1709 pp_cxx_complement (pp);
1710 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1712 else if (DECL_CONV_FN_P (t))
1714 /* This cannot use the hack that the operator's return
1715 type is stashed off of its name because it may be
1716 used for error reporting. In the case of conflicting
1717 declarations, both will have the same name, yet
1718 the types will be different, hence the TREE_TYPE field
1719 of the first name will be clobbered by the second. */
1720 pp_cxx_ws_string (pp, "operator");
1721 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1723 else if (name && IDENTIFIER_OPNAME_P (name))
1724 pp_cxx_tree_identifier (pp, name);
1725 else if (name && UDLIT_OPER_P (name))
1726 pp_cxx_tree_identifier (pp, name);
1727 else
1728 dump_decl (pp, name, flags);
1730 if (DECL_TEMPLATE_INFO (t)
1731 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1732 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1733 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1734 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1735 flags);
1738 /* Dump the template parameters from the template info INFO under control of
1739 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1740 specialization (partial or complete). For partial specializations we show
1741 the specialized parameter values. For a primary template we show no
1742 decoration. */
1744 static void
1745 dump_template_parms (cxx_pretty_printer *pp, tree info,
1746 int primary, int flags)
1748 tree args = info ? TI_ARGS (info) : NULL_TREE;
1750 if (primary && flags & TFF_TEMPLATE_NAME)
1751 return;
1752 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1753 pp_cxx_begin_template_argument_list (pp);
1755 /* Be careful only to print things when we have them, so as not
1756 to crash producing error messages. */
1757 if (args && !primary)
1759 int len, ix;
1760 len = get_non_default_template_args_count (args, flags);
1762 args = INNERMOST_TEMPLATE_ARGS (args);
1763 for (ix = 0; ix != len; ix++)
1765 tree arg = TREE_VEC_ELT (args, ix);
1767 /* Only print a comma if we know there is an argument coming. In
1768 the case of an empty template argument pack, no actual
1769 argument will be printed. */
1770 if (ix
1771 && (!ARGUMENT_PACK_P (arg)
1772 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1773 pp_separate_with_comma (pp);
1775 if (!arg)
1776 pp_string (pp, M_("<template parameter error>"));
1777 else
1778 dump_template_argument (pp, arg, flags);
1781 else if (primary)
1783 tree tpl = TI_TEMPLATE (info);
1784 tree parms = DECL_TEMPLATE_PARMS (tpl);
1785 int len, ix;
1787 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1788 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1790 for (ix = 0; ix != len; ix++)
1792 tree parm;
1794 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1796 pp_string (pp, M_("<template parameter error>"));
1797 continue;
1800 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1802 if (ix)
1803 pp_separate_with_comma (pp);
1805 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1808 pp_cxx_end_template_argument_list (pp);
1811 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1812 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1814 static void
1815 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1817 tree arg;
1818 call_expr_arg_iterator iter;
1820 pp_cxx_left_paren (pp);
1821 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1823 if (skipfirst)
1824 skipfirst = false;
1825 else
1827 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1828 if (more_call_expr_args_p (&iter))
1829 pp_separate_with_comma (pp);
1832 pp_cxx_right_paren (pp);
1835 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1836 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1837 true. */
1839 static void
1840 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1841 bool skipfirst)
1843 tree arg;
1844 aggr_init_expr_arg_iterator iter;
1846 pp_cxx_left_paren (pp);
1847 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1849 if (skipfirst)
1850 skipfirst = false;
1851 else
1853 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1854 if (more_aggr_init_expr_args_p (&iter))
1855 pp_separate_with_comma (pp);
1858 pp_cxx_right_paren (pp);
1861 /* Print out a list of initializers (subr of dump_expr). */
1863 static void
1864 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
1866 while (l)
1868 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1869 l = TREE_CHAIN (l);
1870 if (l)
1871 pp_separate_with_comma (pp);
1875 /* Print out a vector of initializers (subr of dump_expr). */
1877 static void
1878 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
1879 int flags)
1881 unsigned HOST_WIDE_INT idx;
1882 tree value;
1884 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1886 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
1887 if (idx != v->length () - 1)
1888 pp_separate_with_comma (pp);
1893 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1894 function. Resolve it to a close relative -- in the sense of static
1895 type -- variant being overridden. That is close to what was written in
1896 the source code. Subroutine of dump_expr. */
1898 static tree
1899 resolve_virtual_fun_from_obj_type_ref (tree ref)
1901 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1902 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
1903 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1904 while (index)
1906 fun = TREE_CHAIN (fun);
1907 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1908 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1911 return BV_FN (fun);
1914 /* Print out an expression E under control of FLAGS. */
1916 static void
1917 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
1919 tree op;
1921 if (t == 0)
1922 return;
1924 if (STATEMENT_CLASS_P (t))
1926 pp_cxx_ws_string (pp, M_("<statement>"));
1927 return;
1930 switch (TREE_CODE (t))
1932 case VAR_DECL:
1933 case PARM_DECL:
1934 case FIELD_DECL:
1935 case CONST_DECL:
1936 case FUNCTION_DECL:
1937 case TEMPLATE_DECL:
1938 case NAMESPACE_DECL:
1939 case LABEL_DECL:
1940 case OVERLOAD:
1941 case TYPE_DECL:
1942 case IDENTIFIER_NODE:
1943 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1944 |TFF_TEMPLATE_HEADER))
1945 | TFF_NO_FUNCTION_ARGUMENTS));
1946 break;
1948 case SSA_NAME:
1949 if (SSA_NAME_VAR (t)
1950 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1951 dump_expr (pp, SSA_NAME_VAR (t), flags);
1952 else
1953 pp_cxx_ws_string (pp, M_("<unknown>"));
1954 break;
1956 case VOID_CST:
1957 case INTEGER_CST:
1958 case REAL_CST:
1959 case STRING_CST:
1960 case COMPLEX_CST:
1961 pp->constant (t);
1962 break;
1964 case USERDEF_LITERAL:
1965 pp_cxx_userdef_literal (pp, t);
1966 break;
1968 case THROW_EXPR:
1969 /* While waiting for caret diagnostics, avoid printing
1970 __cxa_allocate_exception, __cxa_throw, and the like. */
1971 pp_cxx_ws_string (pp, M_("<throw-expression>"));
1972 break;
1974 case PTRMEM_CST:
1975 pp_ampersand (pp);
1976 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
1977 pp_cxx_colon_colon (pp);
1978 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1979 break;
1981 case COMPOUND_EXPR:
1982 pp_cxx_left_paren (pp);
1983 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1984 pp_separate_with_comma (pp);
1985 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1986 pp_cxx_right_paren (pp);
1987 break;
1989 case COND_EXPR:
1990 pp_cxx_left_paren (pp);
1991 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1992 pp_string (pp, " ? ");
1993 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1994 pp_string (pp, " : ");
1995 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1996 pp_cxx_right_paren (pp);
1997 break;
1999 case SAVE_EXPR:
2000 if (TREE_HAS_CONSTRUCTOR (t))
2002 pp_cxx_ws_string (pp, "new");
2003 pp_cxx_whitespace (pp);
2004 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2006 else
2007 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2008 break;
2010 case AGGR_INIT_EXPR:
2012 tree fn = NULL_TREE;
2014 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2015 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2017 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2019 if (DECL_CONSTRUCTOR_P (fn))
2020 dump_type (pp, DECL_CONTEXT (fn), flags);
2021 else
2022 dump_decl (pp, fn, 0);
2024 else
2025 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2027 dump_aggr_init_expr_args (pp, t, flags, true);
2028 break;
2030 case CALL_EXPR:
2032 tree fn = CALL_EXPR_FN (t);
2033 bool skipfirst = false;
2035 /* Deal with internal functions. */
2036 if (fn == NULL_TREE)
2038 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2039 dump_call_expr_args (pp, t, flags, skipfirst);
2040 break;
2043 if (TREE_CODE (fn) == ADDR_EXPR)
2044 fn = TREE_OPERAND (fn, 0);
2046 /* Nobody is interested in seeing the guts of vcalls. */
2047 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2048 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2050 if (TREE_TYPE (fn) != NULL_TREE
2051 && NEXT_CODE (fn) == METHOD_TYPE
2052 && call_expr_nargs (t))
2054 tree ob = CALL_EXPR_ARG (t, 0);
2055 if (TREE_CODE (ob) == ADDR_EXPR)
2057 dump_expr (pp, TREE_OPERAND (ob, 0),
2058 flags | TFF_EXPR_IN_PARENS);
2059 pp_cxx_dot (pp);
2061 else if (TREE_CODE (ob) != PARM_DECL
2062 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
2064 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2065 pp_cxx_arrow (pp);
2067 skipfirst = true;
2069 if (flag_sanitize & SANITIZE_UNDEFINED
2070 && is_ubsan_builtin_p (fn))
2072 pp_string (cxx_pp, M_("<ubsan routine call>"));
2073 break;
2075 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2076 dump_call_expr_args (pp, t, flags, skipfirst);
2078 break;
2080 case TARGET_EXPR:
2081 /* Note that this only works for G++ target exprs. If somebody
2082 builds a general TARGET_EXPR, there's no way to represent that
2083 it initializes anything other that the parameter slot for the
2084 default argument. Note we may have cleared out the first
2085 operand in expand_expr, so don't go killing ourselves. */
2086 if (TREE_OPERAND (t, 1))
2087 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2088 break;
2090 case POINTER_PLUS_EXPR:
2091 dump_binary_op (pp, "+", t, flags);
2092 break;
2094 case INIT_EXPR:
2095 case MODIFY_EXPR:
2096 dump_binary_op (pp, assignment_operator_name_info[NOP_EXPR].name,
2097 t, flags);
2098 break;
2100 case PLUS_EXPR:
2101 case MINUS_EXPR:
2102 case MULT_EXPR:
2103 case TRUNC_DIV_EXPR:
2104 case TRUNC_MOD_EXPR:
2105 case MIN_EXPR:
2106 case MAX_EXPR:
2107 case LSHIFT_EXPR:
2108 case RSHIFT_EXPR:
2109 case BIT_IOR_EXPR:
2110 case BIT_XOR_EXPR:
2111 case BIT_AND_EXPR:
2112 case TRUTH_ANDIF_EXPR:
2113 case TRUTH_ORIF_EXPR:
2114 case LT_EXPR:
2115 case LE_EXPR:
2116 case GT_EXPR:
2117 case GE_EXPR:
2118 case EQ_EXPR:
2119 case NE_EXPR:
2120 case EXACT_DIV_EXPR:
2121 dump_binary_op (pp, operator_name_info[TREE_CODE (t)].name, t, flags);
2122 break;
2124 case CEIL_DIV_EXPR:
2125 case FLOOR_DIV_EXPR:
2126 case ROUND_DIV_EXPR:
2127 case RDIV_EXPR:
2128 dump_binary_op (pp, "/", t, flags);
2129 break;
2131 case CEIL_MOD_EXPR:
2132 case FLOOR_MOD_EXPR:
2133 case ROUND_MOD_EXPR:
2134 dump_binary_op (pp, "%", t, flags);
2135 break;
2137 case COMPONENT_REF:
2139 tree ob = TREE_OPERAND (t, 0);
2140 if (INDIRECT_REF_P (ob))
2142 ob = TREE_OPERAND (ob, 0);
2143 if (TREE_CODE (ob) != PARM_DECL
2144 || (DECL_NAME (ob)
2145 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
2147 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2148 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2149 pp_cxx_dot (pp);
2150 else
2151 pp_cxx_arrow (pp);
2154 else
2156 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2157 pp_cxx_dot (pp);
2159 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2161 break;
2163 case ARRAY_REF:
2164 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2165 pp_cxx_left_bracket (pp);
2166 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2167 pp_cxx_right_bracket (pp);
2168 break;
2170 case ARRAY_NOTATION_REF:
2171 dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2172 pp_cxx_left_bracket (pp);
2173 dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2174 pp_colon (pp);
2175 dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2176 pp_colon (pp);
2177 dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2178 pp_cxx_right_bracket (pp);
2179 break;
2181 case UNARY_PLUS_EXPR:
2182 dump_unary_op (pp, "+", t, flags);
2183 break;
2185 case ADDR_EXPR:
2186 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2187 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2188 /* An ADDR_EXPR can have reference type. In that case, we
2189 shouldn't print the `&' doing so indicates to the user
2190 that the expression has pointer type. */
2191 || (TREE_TYPE (t)
2192 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2193 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2194 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2195 dump_unary_op (pp, "&&", t, flags);
2196 else
2197 dump_unary_op (pp, "&", t, flags);
2198 break;
2200 case INDIRECT_REF:
2201 if (TREE_HAS_CONSTRUCTOR (t))
2203 t = TREE_OPERAND (t, 0);
2204 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2205 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2206 dump_call_expr_args (pp, t, flags, true);
2208 else
2210 if (TREE_OPERAND (t,0) != NULL_TREE
2211 && TREE_TYPE (TREE_OPERAND (t, 0))
2212 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2213 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2214 else
2215 dump_unary_op (pp, "*", t, flags);
2217 break;
2219 case MEM_REF:
2220 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2221 && integer_zerop (TREE_OPERAND (t, 1)))
2222 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2223 else
2225 pp_cxx_star (pp);
2226 if (!integer_zerop (TREE_OPERAND (t, 1)))
2228 pp_cxx_left_paren (pp);
2229 if (!integer_onep (TYPE_SIZE_UNIT
2230 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2232 pp_cxx_left_paren (pp);
2233 dump_type (pp, ptr_type_node, flags);
2234 pp_cxx_right_paren (pp);
2237 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2238 if (!integer_zerop (TREE_OPERAND (t, 1)))
2240 pp_cxx_ws_string (pp, "+");
2241 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2242 flags);
2243 pp_cxx_right_paren (pp);
2246 break;
2248 case NEGATE_EXPR:
2249 case BIT_NOT_EXPR:
2250 case TRUTH_NOT_EXPR:
2251 case PREDECREMENT_EXPR:
2252 case PREINCREMENT_EXPR:
2253 dump_unary_op (pp, operator_name_info [TREE_CODE (t)].name, t, flags);
2254 break;
2256 case POSTDECREMENT_EXPR:
2257 case POSTINCREMENT_EXPR:
2258 pp_cxx_left_paren (pp);
2259 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2260 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2261 pp_cxx_right_paren (pp);
2262 break;
2264 case NON_LVALUE_EXPR:
2265 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2266 should be another level of INDIRECT_REF so that I don't have to do
2267 this. */
2268 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2270 tree next = TREE_TYPE (TREE_TYPE (t));
2272 while (TYPE_PTR_P (next))
2273 next = TREE_TYPE (next);
2275 if (TREE_CODE (next) == FUNCTION_TYPE)
2277 if (flags & TFF_EXPR_IN_PARENS)
2278 pp_cxx_left_paren (pp);
2279 pp_cxx_star (pp);
2280 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2281 if (flags & TFF_EXPR_IN_PARENS)
2282 pp_cxx_right_paren (pp);
2283 break;
2285 /* Else fall through. */
2287 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2288 break;
2290 CASE_CONVERT:
2291 case IMPLICIT_CONV_EXPR:
2292 case VIEW_CONVERT_EXPR:
2294 tree op = TREE_OPERAND (t, 0);
2295 tree ttype = TREE_TYPE (t);
2296 tree optype = TREE_TYPE (op);
2298 if (TREE_CODE (ttype) != TREE_CODE (optype)
2299 && POINTER_TYPE_P (ttype)
2300 && POINTER_TYPE_P (optype)
2301 && same_type_p (TREE_TYPE (optype),
2302 TREE_TYPE (ttype)))
2304 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2306 STRIP_NOPS (op);
2307 if (TREE_CODE (op) == ADDR_EXPR)
2308 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2309 else
2310 dump_unary_op (pp, "*", t, flags);
2312 else
2313 dump_unary_op (pp, "&", t, flags);
2315 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2317 /* It is a cast, but we cannot tell whether it is a
2318 reinterpret or static cast. Use the C style notation. */
2319 if (flags & TFF_EXPR_IN_PARENS)
2320 pp_cxx_left_paren (pp);
2321 pp_cxx_left_paren (pp);
2322 dump_type (pp, TREE_TYPE (t), flags);
2323 pp_cxx_right_paren (pp);
2324 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2325 if (flags & TFF_EXPR_IN_PARENS)
2326 pp_cxx_right_paren (pp);
2328 else
2329 dump_expr (pp, op, flags);
2330 break;
2333 case CONSTRUCTOR:
2334 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2336 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2338 if (integer_zerop (idx))
2340 /* A NULL pointer-to-member constant. */
2341 pp_cxx_left_paren (pp);
2342 pp_cxx_left_paren (pp);
2343 dump_type (pp, TREE_TYPE (t), flags);
2344 pp_cxx_right_paren (pp);
2345 pp_character (pp, '0');
2346 pp_cxx_right_paren (pp);
2347 break;
2349 else if (tree_fits_shwi_p (idx))
2351 tree virtuals;
2352 unsigned HOST_WIDE_INT n;
2354 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2355 t = TYPE_METHOD_BASETYPE (t);
2356 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2358 n = tree_to_shwi (idx);
2360 /* Map vtable index back one, to allow for the null pointer to
2361 member. */
2362 --n;
2364 while (n > 0 && virtuals)
2366 --n;
2367 virtuals = TREE_CHAIN (virtuals);
2369 if (virtuals)
2371 dump_expr (pp, BV_FN (virtuals),
2372 flags | TFF_EXPR_IN_PARENS);
2373 break;
2377 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2378 pp_string (pp, "<lambda closure object>");
2379 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2381 dump_type (pp, TREE_TYPE (t), 0);
2382 pp_cxx_left_paren (pp);
2383 pp_cxx_right_paren (pp);
2385 else
2387 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2388 dump_type (pp, TREE_TYPE (t), 0);
2389 pp_cxx_left_brace (pp);
2390 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2391 pp_cxx_right_brace (pp);
2394 break;
2396 case OFFSET_REF:
2398 tree ob = TREE_OPERAND (t, 0);
2399 if (is_dummy_object (ob))
2401 t = TREE_OPERAND (t, 1);
2402 if (TREE_CODE (t) == FUNCTION_DECL)
2403 /* A::f */
2404 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2405 else if (BASELINK_P (t))
2406 dump_expr (pp, OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2407 flags | TFF_EXPR_IN_PARENS);
2408 else
2409 dump_decl (pp, t, flags);
2411 else
2413 if (INDIRECT_REF_P (ob))
2415 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2416 pp_cxx_arrow (pp);
2417 pp_cxx_star (pp);
2419 else
2421 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2422 pp_cxx_dot (pp);
2423 pp_cxx_star (pp);
2425 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2427 break;
2430 case TEMPLATE_PARM_INDEX:
2431 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2432 break;
2434 case CAST_EXPR:
2435 if (TREE_OPERAND (t, 0) == NULL_TREE
2436 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2438 dump_type (pp, TREE_TYPE (t), flags);
2439 pp_cxx_left_paren (pp);
2440 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2441 pp_cxx_right_paren (pp);
2443 else
2445 pp_cxx_left_paren (pp);
2446 dump_type (pp, TREE_TYPE (t), flags);
2447 pp_cxx_right_paren (pp);
2448 pp_cxx_left_paren (pp);
2449 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2450 pp_cxx_right_paren (pp);
2452 break;
2454 case STATIC_CAST_EXPR:
2455 pp_cxx_ws_string (pp, "static_cast");
2456 goto cast;
2457 case REINTERPRET_CAST_EXPR:
2458 pp_cxx_ws_string (pp, "reinterpret_cast");
2459 goto cast;
2460 case CONST_CAST_EXPR:
2461 pp_cxx_ws_string (pp, "const_cast");
2462 goto cast;
2463 case DYNAMIC_CAST_EXPR:
2464 pp_cxx_ws_string (pp, "dynamic_cast");
2465 cast:
2466 pp_cxx_begin_template_argument_list (pp);
2467 dump_type (pp, TREE_TYPE (t), flags);
2468 pp_cxx_end_template_argument_list (pp);
2469 pp_cxx_left_paren (pp);
2470 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2471 pp_cxx_right_paren (pp);
2472 break;
2474 case ARROW_EXPR:
2475 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2476 pp_cxx_arrow (pp);
2477 break;
2479 case SIZEOF_EXPR:
2480 case ALIGNOF_EXPR:
2481 if (TREE_CODE (t) == SIZEOF_EXPR)
2482 pp_cxx_ws_string (pp, "sizeof");
2483 else
2485 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2486 pp_cxx_ws_string (pp, "__alignof__");
2488 op = TREE_OPERAND (t, 0);
2489 if (PACK_EXPANSION_P (op))
2491 pp_string (pp, "...");
2492 op = PACK_EXPANSION_PATTERN (op);
2494 pp_cxx_whitespace (pp);
2495 pp_cxx_left_paren (pp);
2496 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2497 dump_type (pp, TREE_TYPE (op), flags);
2498 else if (TYPE_P (TREE_OPERAND (t, 0)))
2499 dump_type (pp, op, flags);
2500 else
2501 dump_expr (pp, op, flags);
2502 pp_cxx_right_paren (pp);
2503 break;
2505 case AT_ENCODE_EXPR:
2506 pp_cxx_ws_string (pp, "@encode");
2507 pp_cxx_whitespace (pp);
2508 pp_cxx_left_paren (pp);
2509 dump_type (pp, TREE_OPERAND (t, 0), flags);
2510 pp_cxx_right_paren (pp);
2511 break;
2513 case NOEXCEPT_EXPR:
2514 pp_cxx_ws_string (pp, "noexcept");
2515 pp_cxx_whitespace (pp);
2516 pp_cxx_left_paren (pp);
2517 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2518 pp_cxx_right_paren (pp);
2519 break;
2521 case REALPART_EXPR:
2522 case IMAGPART_EXPR:
2523 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2524 pp_cxx_whitespace (pp);
2525 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2526 break;
2528 case DEFAULT_ARG:
2529 pp_string (pp, M_("<unparsed>"));
2530 break;
2532 case TRY_CATCH_EXPR:
2533 case WITH_CLEANUP_EXPR:
2534 case CLEANUP_POINT_EXPR:
2535 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2536 break;
2538 case PSEUDO_DTOR_EXPR:
2539 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2540 pp_cxx_dot (pp);
2541 if (TREE_OPERAND (t, 1))
2543 dump_type (pp, TREE_OPERAND (t, 1), flags);
2544 pp_cxx_colon_colon (pp);
2546 pp_cxx_complement (pp);
2547 dump_type (pp, TREE_OPERAND (t, 2), flags);
2548 break;
2550 case TEMPLATE_ID_EXPR:
2551 dump_decl (pp, t, flags);
2552 break;
2554 case BIND_EXPR:
2555 case STMT_EXPR:
2556 case EXPR_STMT:
2557 case STATEMENT_LIST:
2558 /* We don't yet have a way of dumping statements in a
2559 human-readable format. */
2560 pp_string (pp, "({...})");
2561 break;
2563 case LOOP_EXPR:
2564 pp_string (pp, "while (1) { ");
2565 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2566 pp_cxx_right_brace (pp);
2567 break;
2569 case EXIT_EXPR:
2570 pp_string (pp, "if (");
2571 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2572 pp_string (pp, ") break; ");
2573 break;
2575 case BASELINK:
2576 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2577 break;
2579 case EMPTY_CLASS_EXPR:
2580 dump_type (pp, TREE_TYPE (t), flags);
2581 pp_cxx_left_paren (pp);
2582 pp_cxx_right_paren (pp);
2583 break;
2585 case NON_DEPENDENT_EXPR:
2586 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2587 break;
2589 case ARGUMENT_PACK_SELECT:
2590 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2591 break;
2593 case RECORD_TYPE:
2594 case UNION_TYPE:
2595 case ENUMERAL_TYPE:
2596 case REAL_TYPE:
2597 case VOID_TYPE:
2598 case BOOLEAN_TYPE:
2599 case INTEGER_TYPE:
2600 case COMPLEX_TYPE:
2601 case VECTOR_TYPE:
2602 pp_type_specifier_seq (pp, t);
2603 break;
2605 case TYPENAME_TYPE:
2606 /* We get here when we want to print a dependent type as an
2607 id-expression, without any disambiguator decoration. */
2608 pp->id_expression (t);
2609 break;
2611 case TEMPLATE_TYPE_PARM:
2612 case TEMPLATE_TEMPLATE_PARM:
2613 case BOUND_TEMPLATE_TEMPLATE_PARM:
2614 dump_type (pp, t, flags);
2615 break;
2617 case TRAIT_EXPR:
2618 pp_cxx_trait_expression (pp, t);
2619 break;
2621 case VA_ARG_EXPR:
2622 pp_cxx_va_arg_expression (pp, t);
2623 break;
2625 case OFFSETOF_EXPR:
2626 pp_cxx_offsetof_expression (pp, t);
2627 break;
2629 case SCOPE_REF:
2630 dump_decl (pp, t, flags);
2631 break;
2633 case EXPR_PACK_EXPANSION:
2634 case TYPEID_EXPR:
2635 case MEMBER_REF:
2636 case DOTSTAR_EXPR:
2637 case NEW_EXPR:
2638 case VEC_NEW_EXPR:
2639 case DELETE_EXPR:
2640 case VEC_DELETE_EXPR:
2641 case MODOP_EXPR:
2642 case ABS_EXPR:
2643 case CONJ_EXPR:
2644 case VECTOR_CST:
2645 case FIXED_CST:
2646 case UNORDERED_EXPR:
2647 case ORDERED_EXPR:
2648 case UNLT_EXPR:
2649 case UNLE_EXPR:
2650 case UNGT_EXPR:
2651 case UNGE_EXPR:
2652 case UNEQ_EXPR:
2653 case LTGT_EXPR:
2654 case COMPLEX_EXPR:
2655 case BIT_FIELD_REF:
2656 case FIX_TRUNC_EXPR:
2657 case FLOAT_EXPR:
2658 pp->expression (t);
2659 break;
2661 case TRUTH_AND_EXPR:
2662 case TRUTH_OR_EXPR:
2663 case TRUTH_XOR_EXPR:
2664 if (flags & TFF_EXPR_IN_PARENS)
2665 pp_cxx_left_paren (pp);
2666 pp->expression (t);
2667 if (flags & TFF_EXPR_IN_PARENS)
2668 pp_cxx_right_paren (pp);
2669 break;
2671 case OBJ_TYPE_REF:
2672 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2673 break;
2675 case LAMBDA_EXPR:
2676 pp_string (pp, M_("<lambda>"));
2677 break;
2679 case PAREN_EXPR:
2680 pp_cxx_left_paren (pp);
2681 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2682 pp_cxx_right_paren (pp);
2683 break;
2685 case PLACEHOLDER_EXPR:
2686 pp_string (pp, M_("*this"));
2687 break;
2689 /* This list is incomplete, but should suffice for now.
2690 It is very important that `sorry' does not call
2691 `report_error_function'. That could cause an infinite loop. */
2692 default:
2693 pp_unsupported_tree (pp, t);
2694 /* fall through to ERROR_MARK... */
2695 case ERROR_MARK:
2696 pp_string (pp, M_("<expression error>"));
2697 break;
2701 static void
2702 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2703 int flags)
2705 pp_cxx_left_paren (pp);
2706 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2707 pp_cxx_whitespace (pp);
2708 if (opstring)
2709 pp_cxx_ws_string (pp, opstring);
2710 else
2711 pp_string (pp, M_("<unknown operator>"));
2712 pp_cxx_whitespace (pp);
2713 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2714 pp_cxx_right_paren (pp);
2717 static void
2718 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2720 if (flags & TFF_EXPR_IN_PARENS)
2721 pp_cxx_left_paren (pp);
2722 pp_cxx_ws_string (pp, opstring);
2723 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2724 if (flags & TFF_EXPR_IN_PARENS)
2725 pp_cxx_right_paren (pp);
2728 static void
2729 reinit_cxx_pp (void)
2731 pp_clear_output_area (cxx_pp);
2732 cxx_pp->padding = pp_none;
2733 pp_indentation (cxx_pp) = 0;
2734 pp_needs_newline (cxx_pp) = false;
2735 cxx_pp->enclosing_scope = current_function_decl;
2738 /* Same as pp_formatted_text, except the return string is a separate
2739 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2741 inline const char *
2742 pp_ggc_formatted_text (pretty_printer *pp)
2744 return ggc_strdup (pp_formatted_text (pp));
2747 /* Exported interface to stringifying types, exprs and decls under TFF_*
2748 control. */
2750 const char *
2751 type_as_string (tree typ, int flags)
2753 reinit_cxx_pp ();
2754 pp_translate_identifiers (cxx_pp) = false;
2755 dump_type (cxx_pp, typ, flags);
2756 return pp_ggc_formatted_text (cxx_pp);
2759 const char *
2760 type_as_string_translate (tree typ, int flags)
2762 reinit_cxx_pp ();
2763 dump_type (cxx_pp, typ, flags);
2764 return pp_ggc_formatted_text (cxx_pp);
2767 const char *
2768 expr_as_string (tree decl, int flags)
2770 reinit_cxx_pp ();
2771 pp_translate_identifiers (cxx_pp) = false;
2772 dump_expr (cxx_pp, decl, flags);
2773 return pp_ggc_formatted_text (cxx_pp);
2776 /* Wrap decl_as_string with options appropriate for dwarf. */
2778 const char *
2779 decl_as_dwarf_string (tree decl, int flags)
2781 const char *name;
2782 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2783 here will be adequate to get the desired behaviour. */
2784 cxx_pp->flags |= pp_c_flag_gnu_v3;
2785 name = decl_as_string (decl, flags);
2786 /* Subsequent calls to the pretty printer shouldn't use this style. */
2787 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2788 return name;
2791 const char *
2792 decl_as_string (tree decl, int flags)
2794 reinit_cxx_pp ();
2795 pp_translate_identifiers (cxx_pp) = false;
2796 dump_decl (cxx_pp, decl, flags);
2797 return pp_ggc_formatted_text (cxx_pp);
2800 const char *
2801 decl_as_string_translate (tree decl, int flags)
2803 reinit_cxx_pp ();
2804 dump_decl (cxx_pp, decl, flags);
2805 return pp_ggc_formatted_text (cxx_pp);
2808 /* Wrap lang_decl_name with options appropriate for dwarf. */
2810 const char *
2811 lang_decl_dwarf_name (tree decl, int v, bool translate)
2813 const char *name;
2814 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2815 here will be adequate to get the desired behaviour. */
2816 cxx_pp->flags |= pp_c_flag_gnu_v3;
2817 name = lang_decl_name (decl, v, translate);
2818 /* Subsequent calls to the pretty printer shouldn't use this style. */
2819 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2820 return name;
2823 /* Generate the three forms of printable names for cxx_printable_name. */
2825 const char *
2826 lang_decl_name (tree decl, int v, bool translate)
2828 if (v >= 2)
2829 return (translate
2830 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2831 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2833 reinit_cxx_pp ();
2834 pp_translate_identifiers (cxx_pp) = translate;
2835 if (v == 1
2836 && (DECL_CLASS_SCOPE_P (decl)
2837 || (DECL_NAMESPACE_SCOPE_P (decl)
2838 && CP_DECL_CONTEXT (decl) != global_namespace)))
2840 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2841 pp_cxx_colon_colon (cxx_pp);
2844 if (TREE_CODE (decl) == FUNCTION_DECL)
2845 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
2846 else if ((DECL_NAME (decl) == NULL_TREE)
2847 && TREE_CODE (decl) == NAMESPACE_DECL)
2848 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2849 else
2850 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2852 return pp_ggc_formatted_text (cxx_pp);
2855 /* Return the location of a tree passed to %+ formats. */
2857 location_t
2858 location_of (tree t)
2860 if (TYPE_P (t))
2862 t = TYPE_MAIN_DECL (t);
2863 if (t == NULL_TREE)
2864 return input_location;
2866 else if (TREE_CODE (t) == OVERLOAD)
2867 t = OVL_FUNCTION (t);
2869 if (DECL_P (t))
2870 return DECL_SOURCE_LOCATION (t);
2871 return EXPR_LOC_OR_LOC (t, input_location);
2874 /* Now the interfaces from error et al to dump_type et al. Each takes an
2875 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2876 function. */
2878 static const char *
2879 decl_to_string (tree decl, int verbose)
2881 int flags = 0;
2883 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2884 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2885 flags = TFF_CLASS_KEY_OR_ENUM;
2886 if (verbose)
2887 flags |= TFF_DECL_SPECIFIERS;
2888 else if (TREE_CODE (decl) == FUNCTION_DECL)
2889 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2890 flags |= TFF_TEMPLATE_HEADER;
2892 reinit_cxx_pp ();
2893 dump_decl (cxx_pp, decl, flags);
2894 return pp_ggc_formatted_text (cxx_pp);
2897 static const char *
2898 expr_to_string (tree decl)
2900 reinit_cxx_pp ();
2901 dump_expr (cxx_pp, decl, 0);
2902 return pp_ggc_formatted_text (cxx_pp);
2905 static const char *
2906 fndecl_to_string (tree fndecl, int verbose)
2908 int flags;
2910 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2911 | TFF_TEMPLATE_HEADER;
2912 if (verbose)
2913 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2914 reinit_cxx_pp ();
2915 dump_decl (cxx_pp, fndecl, flags);
2916 return pp_ggc_formatted_text (cxx_pp);
2920 static const char *
2921 code_to_string (enum tree_code c)
2923 return get_tree_code_name (c);
2926 const char *
2927 language_to_string (enum languages c)
2929 switch (c)
2931 case lang_c:
2932 return "C";
2934 case lang_cplusplus:
2935 return "C++";
2937 case lang_java:
2938 return "Java";
2940 default:
2941 gcc_unreachable ();
2943 return NULL;
2946 /* Return the proper printed version of a parameter to a C++ function. */
2948 static const char *
2949 parm_to_string (int p)
2951 reinit_cxx_pp ();
2952 if (p < 0)
2953 pp_string (cxx_pp, "'this'");
2954 else
2955 pp_decimal_int (cxx_pp, p + 1);
2956 return pp_ggc_formatted_text (cxx_pp);
2959 static const char *
2960 op_to_string (enum tree_code p)
2962 tree id = operator_name_info[p].identifier;
2963 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2966 static const char *
2967 type_to_string (tree typ, int verbose)
2969 int flags = 0;
2970 if (verbose)
2971 flags |= TFF_CLASS_KEY_OR_ENUM;
2972 flags |= TFF_TEMPLATE_HEADER;
2974 reinit_cxx_pp ();
2975 dump_type (cxx_pp, typ, flags);
2976 /* If we're printing a type that involves typedefs, also print the
2977 stripped version. But sometimes the stripped version looks
2978 exactly the same, so we don't want it after all. To avoid printing
2979 it in that case, we play ugly obstack games. */
2980 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2981 && !uses_template_parms (typ))
2983 int aka_start, aka_len; char *p;
2984 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
2985 /* Remember the end of the initial dump. */
2986 int len = obstack_object_size (ob);
2987 tree aka = strip_typedefs (typ);
2988 pp_string (cxx_pp, " {aka");
2989 pp_cxx_whitespace (cxx_pp);
2990 /* And remember the start of the aka dump. */
2991 aka_start = obstack_object_size (ob);
2992 dump_type (cxx_pp, aka, flags);
2993 aka_len = obstack_object_size (ob) - aka_start;
2994 pp_right_brace (cxx_pp);
2995 p = (char*)obstack_base (ob);
2996 /* If they are identical, cut off the aka with a NUL. */
2997 if (len == aka_len && memcmp (p, p+aka_start, len) == 0)
2998 p[len] = '\0';
3000 return pp_ggc_formatted_text (cxx_pp);
3003 static const char *
3004 assop_to_string (enum tree_code p)
3006 tree id = assignment_operator_name_info[(int) p].identifier;
3007 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
3010 static const char *
3011 args_to_string (tree p, int verbose)
3013 int flags = 0;
3014 if (verbose)
3015 flags |= TFF_CLASS_KEY_OR_ENUM;
3017 if (p == NULL_TREE)
3018 return "";
3020 if (TYPE_P (TREE_VALUE (p)))
3021 return type_as_string_translate (p, flags);
3023 reinit_cxx_pp ();
3024 for (; p; p = TREE_CHAIN (p))
3026 if (TREE_VALUE (p) == null_node)
3027 pp_cxx_ws_string (cxx_pp, "NULL");
3028 else
3029 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3030 if (TREE_CHAIN (p))
3031 pp_separate_with_comma (cxx_pp);
3033 return pp_ggc_formatted_text (cxx_pp);
3036 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3037 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3038 arguments. */
3040 static const char *
3041 subst_to_string (tree p)
3043 tree decl = TREE_PURPOSE (p);
3044 tree targs = TREE_VALUE (p);
3045 tree tparms = DECL_TEMPLATE_PARMS (decl);
3046 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3047 |TFF_NO_TEMPLATE_BINDINGS);
3049 if (p == NULL_TREE)
3050 return "";
3052 reinit_cxx_pp ();
3053 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3054 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3055 return pp_ggc_formatted_text (cxx_pp);
3058 static const char *
3059 cv_to_string (tree p, int v)
3061 reinit_cxx_pp ();
3062 cxx_pp->padding = v ? pp_before : pp_none;
3063 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3064 return pp_ggc_formatted_text (cxx_pp);
3067 static const char *
3068 eh_spec_to_string (tree p, int /*v*/)
3070 int flags = 0;
3071 reinit_cxx_pp ();
3072 dump_exception_spec (cxx_pp, p, flags);
3073 return pp_ggc_formatted_text (cxx_pp);
3076 /* Langhook for print_error_function. */
3077 void
3078 cxx_print_error_function (diagnostic_context *context, const char *file,
3079 diagnostic_info *diagnostic)
3081 lhd_print_error_function (context, file, diagnostic);
3082 pp_set_prefix (context->printer, file);
3083 maybe_print_instantiation_context (context);
3086 static void
3087 cp_diagnostic_starter (diagnostic_context *context,
3088 diagnostic_info *diagnostic)
3090 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3091 cp_print_error_function (context, diagnostic);
3092 maybe_print_instantiation_context (context);
3093 maybe_print_constexpr_context (context);
3094 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3095 diagnostic));
3098 /* Print current function onto BUFFER, in the process of reporting
3099 a diagnostic message. Called from cp_diagnostic_starter. */
3100 static void
3101 cp_print_error_function (diagnostic_context *context,
3102 diagnostic_info *diagnostic)
3104 /* If we are in an instantiation context, current_function_decl is likely
3105 to be wrong, so just rely on print_instantiation_full_context. */
3106 if (current_instantiation ())
3107 return;
3108 if (diagnostic_last_function_changed (context, diagnostic))
3110 const char *old_prefix = context->printer->prefix;
3111 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3112 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3113 char *new_prefix = (file && abstract_origin == NULL)
3114 ? file_name_as_prefix (context, file) : NULL;
3116 pp_set_prefix (context->printer, new_prefix);
3118 if (current_function_decl == NULL)
3119 pp_string (context->printer, _("At global scope:"));
3120 else
3122 tree fndecl, ao;
3124 if (abstract_origin)
3126 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3127 while (TREE_CODE (ao) == BLOCK
3128 && BLOCK_ABSTRACT_ORIGIN (ao)
3129 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3130 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3131 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3132 fndecl = ao;
3134 else
3135 fndecl = current_function_decl;
3137 pp_printf (context->printer, function_category (fndecl),
3138 cxx_printable_name_translate (fndecl, 2));
3140 while (abstract_origin)
3142 location_t *locus;
3143 tree block = abstract_origin;
3145 locus = &BLOCK_SOURCE_LOCATION (block);
3146 fndecl = NULL;
3147 block = BLOCK_SUPERCONTEXT (block);
3148 while (block && TREE_CODE (block) == BLOCK
3149 && BLOCK_ABSTRACT_ORIGIN (block))
3151 ao = BLOCK_ABSTRACT_ORIGIN (block);
3153 while (TREE_CODE (ao) == BLOCK
3154 && BLOCK_ABSTRACT_ORIGIN (ao)
3155 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3156 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3158 if (TREE_CODE (ao) == FUNCTION_DECL)
3160 fndecl = ao;
3161 break;
3163 else if (TREE_CODE (ao) != BLOCK)
3164 break;
3166 block = BLOCK_SUPERCONTEXT (block);
3168 if (fndecl)
3169 abstract_origin = block;
3170 else
3172 while (block && TREE_CODE (block) == BLOCK)
3173 block = BLOCK_SUPERCONTEXT (block);
3175 if (block && TREE_CODE (block) == FUNCTION_DECL)
3176 fndecl = block;
3177 abstract_origin = NULL;
3179 if (fndecl)
3181 expanded_location s = expand_location (*locus);
3182 pp_character (context->printer, ',');
3183 pp_newline (context->printer);
3184 if (s.file != NULL)
3186 if (context->show_column && s.column != 0)
3187 pp_printf (context->printer,
3188 _(" inlined from %qs at %r%s:%d:%d%R"),
3189 cxx_printable_name_translate (fndecl, 2),
3190 "locus", s.file, s.line, s.column);
3191 else
3192 pp_printf (context->printer,
3193 _(" inlined from %qs at %r%s:%d%R"),
3194 cxx_printable_name_translate (fndecl, 2),
3195 "locus", s.file, s.line);
3198 else
3199 pp_printf (context->printer, _(" inlined from %qs"),
3200 cxx_printable_name_translate (fndecl, 2));
3203 pp_character (context->printer, ':');
3205 pp_newline (context->printer);
3207 diagnostic_set_last_function (context, diagnostic);
3208 pp_destroy_prefix (context->printer);
3209 context->printer->prefix = old_prefix;
3213 /* Returns a description of FUNCTION using standard terminology. The
3214 result is a format string of the form "In CATEGORY %qs". */
3215 static const char *
3216 function_category (tree fn)
3218 /* We can get called from the middle-end for diagnostics of function
3219 clones. Make sure we have language specific information before
3220 dereferencing it. */
3221 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3222 && DECL_FUNCTION_MEMBER_P (fn))
3224 if (DECL_STATIC_FUNCTION_P (fn))
3225 return _("In static member function %qs");
3226 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3227 return _("In copy constructor %qs");
3228 else if (DECL_CONSTRUCTOR_P (fn))
3229 return _("In constructor %qs");
3230 else if (DECL_DESTRUCTOR_P (fn))
3231 return _("In destructor %qs");
3232 else if (LAMBDA_FUNCTION_P (fn))
3233 return _("In lambda function");
3234 else
3235 return _("In member function %qs");
3237 else
3238 return _("In function %qs");
3241 /* Report the full context of a current template instantiation,
3242 onto BUFFER. */
3243 static void
3244 print_instantiation_full_context (diagnostic_context *context)
3246 struct tinst_level *p = current_instantiation ();
3247 location_t location = input_location;
3249 if (p)
3251 pp_verbatim (context->printer,
3252 TREE_CODE (p->decl) == TREE_LIST
3253 ? _("%s: In substitution of %qS:\n")
3254 : _("%s: In instantiation of %q#D:\n"),
3255 LOCATION_FILE (location),
3256 p->decl);
3258 location = p->locus;
3259 p = p->next;
3262 print_instantiation_partial_context (context, p, location);
3265 /* Helper function of print_instantiation_partial_context() that
3266 prints a single line of instantiation context. */
3268 static void
3269 print_instantiation_partial_context_line (diagnostic_context *context,
3270 const struct tinst_level *t,
3271 location_t loc, bool recursive_p)
3273 if (loc == UNKNOWN_LOCATION)
3274 return;
3276 expanded_location xloc = expand_location (loc);
3278 if (context->show_column)
3279 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3280 "locus", xloc.file, xloc.line, xloc.column);
3281 else
3282 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3283 "locus", xloc.file, xloc.line);
3285 if (t != NULL)
3287 if (TREE_CODE (t->decl) == TREE_LIST)
3288 pp_verbatim (context->printer,
3289 recursive_p
3290 ? _("recursively required by substitution of %qS\n")
3291 : _("required by substitution of %qS\n"),
3292 t->decl);
3293 else
3294 pp_verbatim (context->printer,
3295 recursive_p
3296 ? _("recursively required from %q#D\n")
3297 : _("required from %q#D\n"),
3298 t->decl);
3300 else
3302 pp_verbatim (context->printer,
3303 recursive_p
3304 ? _("recursively required from here")
3305 : _("required from here"));
3309 /* Same as print_instantiation_full_context but less verbose. */
3311 static void
3312 print_instantiation_partial_context (diagnostic_context *context,
3313 struct tinst_level *t0, location_t loc)
3315 struct tinst_level *t;
3316 int n_total = 0;
3317 int n;
3318 location_t prev_loc = loc;
3320 for (t = t0; t != NULL; t = t->next)
3321 if (prev_loc != t->locus)
3323 prev_loc = t->locus;
3324 n_total++;
3327 t = t0;
3329 if (template_backtrace_limit
3330 && n_total > template_backtrace_limit)
3332 int skip = n_total - template_backtrace_limit;
3333 int head = template_backtrace_limit / 2;
3335 /* Avoid skipping just 1. If so, skip 2. */
3336 if (skip == 1)
3338 skip = 2;
3339 head = (template_backtrace_limit - 1) / 2;
3342 for (n = 0; n < head; n++)
3344 gcc_assert (t != NULL);
3345 if (loc != t->locus)
3346 print_instantiation_partial_context_line (context, t, loc,
3347 /*recursive_p=*/false);
3348 loc = t->locus;
3349 t = t->next;
3351 if (t != NULL && skip > 0)
3353 expanded_location xloc;
3354 xloc = expand_location (loc);
3355 if (context->show_column)
3356 pp_verbatim (context->printer,
3357 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3358 "contexts, use -ftemplate-backtrace-limit=0 to "
3359 "disable ]\n"),
3360 "locus", xloc.file, xloc.line, xloc.column, skip);
3361 else
3362 pp_verbatim (context->printer,
3363 _("%r%s:%d:%R [ skipping %d instantiation "
3364 "contexts, use -ftemplate-backtrace-limit=0 to "
3365 "disable ]\n"),
3366 "locus", xloc.file, xloc.line, skip);
3368 do {
3369 loc = t->locus;
3370 t = t->next;
3371 } while (t != NULL && --skip > 0);
3375 while (t != NULL)
3377 while (t->next != NULL && t->locus == t->next->locus)
3379 loc = t->locus;
3380 t = t->next;
3382 print_instantiation_partial_context_line (context, t, loc,
3383 t->locus == loc);
3384 loc = t->locus;
3385 t = t->next;
3387 print_instantiation_partial_context_line (context, NULL, loc,
3388 /*recursive_p=*/false);
3389 pp_newline (context->printer);
3392 /* Called from cp_thing to print the template context for an error. */
3393 static void
3394 maybe_print_instantiation_context (diagnostic_context *context)
3396 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3397 return;
3399 record_last_problematic_instantiation ();
3400 print_instantiation_full_context (context);
3403 /* Report what constexpr call(s) we're trying to expand, if any. */
3405 void
3406 maybe_print_constexpr_context (diagnostic_context *context)
3408 vec<tree> call_stack = cx_error_context ();
3409 unsigned ix;
3410 tree t;
3412 FOR_EACH_VEC_ELT (call_stack, ix, t)
3414 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3415 const char *s = expr_as_string (t, 0);
3416 if (context->show_column)
3417 pp_verbatim (context->printer,
3418 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3419 "locus", xloc.file, xloc.line, xloc.column, s);
3420 else
3421 pp_verbatim (context->printer,
3422 _("%r%s:%d:%R in constexpr expansion of %qs"),
3423 "locus", xloc.file, xloc.line, s);
3424 pp_newline (context->printer);
3428 /* Called from output_format -- during diagnostic message processing --
3429 to handle C++ specific format specifier with the following meanings:
3430 %A function argument-list.
3431 %C tree code.
3432 %D declaration.
3433 %E expression.
3434 %F function declaration.
3435 %L language as used in extern "lang".
3436 %O binary operator.
3437 %P function parameter whose position is indicated by an integer.
3438 %Q assignment operator.
3439 %S substitution (template + args)
3440 %T type.
3441 %V cv-qualifier.
3442 %X exception-specification. */
3443 static bool
3444 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3445 int precision, bool wide, bool set_locus, bool verbose)
3447 const char *result;
3448 tree t = NULL;
3449 #define next_tree (t = va_arg (*text->args_ptr, tree))
3450 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3451 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3452 #define next_int va_arg (*text->args_ptr, int)
3454 if (precision != 0 || wide)
3455 return false;
3457 switch (*spec)
3459 case 'A': result = args_to_string (next_tree, verbose); break;
3460 case 'C': result = code_to_string (next_tcode); break;
3461 case 'D':
3463 tree temp = next_tree;
3464 if (VAR_P (temp)
3465 && DECL_HAS_DEBUG_EXPR_P (temp))
3467 temp = DECL_DEBUG_EXPR (temp);
3468 if (!DECL_P (temp))
3470 result = expr_to_string (temp);
3471 break;
3474 result = decl_to_string (temp, verbose);
3476 break;
3477 case 'E': result = expr_to_string (next_tree); break;
3478 case 'F': result = fndecl_to_string (next_tree, verbose); break;
3479 case 'L': result = language_to_string (next_lang); break;
3480 case 'O': result = op_to_string (next_tcode); break;
3481 case 'P': result = parm_to_string (next_int); break;
3482 case 'Q': result = assop_to_string (next_tcode); break;
3483 case 'S': result = subst_to_string (next_tree); break;
3484 case 'T': result = type_to_string (next_tree, verbose); break;
3485 case 'V': result = cv_to_string (next_tree, verbose); break;
3486 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
3488 case 'K':
3489 percent_K_format (text);
3490 return true;
3492 default:
3493 return false;
3496 pp_string (pp, result);
3497 if (set_locus && t != NULL)
3498 text->set_location (0, location_of (t));
3499 return true;
3500 #undef next_tree
3501 #undef next_tcode
3502 #undef next_lang
3503 #undef next_int
3506 /* Warn about the use of C++0x features when appropriate. */
3507 void
3508 maybe_warn_cpp0x (cpp0x_warn_str str)
3510 if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
3511 /* We really want to suppress this warning in system headers,
3512 because libstdc++ uses variadic templates even when we aren't
3513 in C++0x mode. */
3514 switch (str)
3516 case CPP0X_INITIALIZER_LISTS:
3517 pedwarn (input_location, 0,
3518 "extended initializer lists "
3519 "only available with -std=c++11 or -std=gnu++11");
3520 break;
3521 case CPP0X_EXPLICIT_CONVERSION:
3522 pedwarn (input_location, 0,
3523 "explicit conversion operators "
3524 "only available with -std=c++11 or -std=gnu++11");
3525 break;
3526 case CPP0X_VARIADIC_TEMPLATES:
3527 pedwarn (input_location, 0,
3528 "variadic templates "
3529 "only available with -std=c++11 or -std=gnu++11");
3530 break;
3531 case CPP0X_LAMBDA_EXPR:
3532 pedwarn (input_location, 0,
3533 "lambda expressions "
3534 "only available with -std=c++11 or -std=gnu++11");
3535 break;
3536 case CPP0X_AUTO:
3537 pedwarn (input_location, 0,
3538 "C++11 auto only available with -std=c++11 or -std=gnu++11");
3539 break;
3540 case CPP0X_SCOPED_ENUMS:
3541 pedwarn (input_location, 0,
3542 "scoped enums only available with -std=c++11 or -std=gnu++11");
3543 break;
3544 case CPP0X_DEFAULTED_DELETED:
3545 pedwarn (input_location, 0,
3546 "defaulted and deleted functions "
3547 "only available with -std=c++11 or -std=gnu++11");
3548 break;
3549 case CPP0X_INLINE_NAMESPACES:
3550 pedwarn (input_location, OPT_Wpedantic,
3551 "inline namespaces "
3552 "only available with -std=c++11 or -std=gnu++11");
3553 break;
3554 case CPP0X_OVERRIDE_CONTROLS:
3555 pedwarn (input_location, 0,
3556 "override controls (override/final) "
3557 "only available with -std=c++11 or -std=gnu++11");
3558 break;
3559 case CPP0X_NSDMI:
3560 pedwarn (input_location, 0,
3561 "non-static data member initializers "
3562 "only available with -std=c++11 or -std=gnu++11");
3563 break;
3564 case CPP0X_USER_DEFINED_LITERALS:
3565 pedwarn (input_location, 0,
3566 "user-defined literals "
3567 "only available with -std=c++11 or -std=gnu++11");
3568 break;
3569 case CPP0X_DELEGATING_CTORS:
3570 pedwarn (input_location, 0,
3571 "delegating constructors "
3572 "only available with -std=c++11 or -std=gnu++11");
3573 break;
3574 case CPP0X_INHERITING_CTORS:
3575 pedwarn (input_location, 0,
3576 "inheriting constructors "
3577 "only available with -std=c++11 or -std=gnu++11");
3578 break;
3579 case CPP0X_ATTRIBUTES:
3580 pedwarn (input_location, 0,
3581 "c++11 attributes "
3582 "only available with -std=c++11 or -std=gnu++11");
3583 break;
3584 case CPP0X_REF_QUALIFIER:
3585 pedwarn (input_location, 0,
3586 "ref-qualifiers "
3587 "only available with -std=c++11 or -std=gnu++11");
3588 break;
3589 default:
3590 gcc_unreachable ();
3594 /* Warn about the use of variadic templates when appropriate. */
3595 void
3596 maybe_warn_variadic_templates (void)
3598 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3602 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3603 option OPT with text GMSGID. Use this function to report
3604 diagnostics for constructs that are invalid C++98, but valid
3605 C++0x. */
3606 bool
3607 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3609 diagnostic_info diagnostic;
3610 va_list ap;
3611 bool ret;
3613 va_start (ap, gmsgid);
3614 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3615 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3616 diagnostic.option_index = opt;
3617 ret = report_diagnostic (&diagnostic);
3618 va_end (ap);
3619 return ret;
3622 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3623 we found when we tried to do the lookup. LOCATION is the location of
3624 the NAME identifier. */
3626 void
3627 qualified_name_lookup_error (tree scope, tree name,
3628 tree decl, location_t location)
3630 if (scope == error_mark_node)
3631 ; /* We already complained. */
3632 else if (TYPE_P (scope))
3634 if (!COMPLETE_TYPE_P (scope))
3635 error_at (location, "incomplete type %qT used in nested name specifier",
3636 scope);
3637 else if (TREE_CODE (decl) == TREE_LIST)
3639 error_at (location, "reference to %<%T::%D%> is ambiguous",
3640 scope, name);
3641 print_candidates (decl);
3643 else
3644 error_at (location, "%qD is not a member of %qT", name, scope);
3646 else if (scope != global_namespace)
3648 error_at (location, "%qD is not a member of %qD", name, scope);
3649 suggest_alternatives_for (location, name);
3651 else
3653 error_at (location, "%<::%D%> has not been declared", name);
3654 suggest_alternatives_for (location, name);