C++: simplify output from suggest_alternatives_for
[official-gcc.git] / gcc / cp / error.c
blobfa115aa211ce5363cf4aa1dd152d0a130ed829af
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2018 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 /* For use with name_hint. */
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "stringpool.h"
27 #include "tree-diagnostic.h"
28 #include "langhooks-def.h"
29 #include "intl.h"
30 #include "cxx-pretty-print.h"
31 #include "tree-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "c-family/c-objc.h"
34 #include "ubsan.h"
35 #include "internal-fn.h"
36 #include "gcc-rich-location.h"
37 #include "cp-name-hint.h"
39 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
40 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
42 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
43 dump C++ ASTs as strings. It is mostly used only by the various
44 tree -> string functions that are occasionally called from the
45 debugger or by the front-end for things like
46 __PRETTY_FUNCTION__. */
47 static cxx_pretty_printer actual_pretty_printer;
48 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
50 /* Translate if being used for diagnostics, but not for dump files or
51 __PRETTY_FUNCTION. */
52 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
54 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
56 static const char *args_to_string (tree, int);
57 static const char *code_to_string (enum tree_code);
58 static const char *cv_to_string (tree, int);
59 static const char *decl_to_string (tree, int);
60 static const char *fndecl_to_string (tree, int);
61 static const char *op_to_string (bool, enum tree_code);
62 static const char *parm_to_string (int);
63 static const char *type_to_string (tree, int, bool, bool *, bool);
65 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
66 static void dump_type (cxx_pretty_printer *, tree, int);
67 static void dump_typename (cxx_pretty_printer *, tree, int);
68 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
69 static void dump_decl (cxx_pretty_printer *, tree, int);
70 static void dump_template_decl (cxx_pretty_printer *, tree, int);
71 static void dump_function_decl (cxx_pretty_printer *, tree, int);
72 static void dump_expr (cxx_pretty_printer *, tree, int);
73 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
74 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
75 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
76 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
77 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
78 static void dump_function_name (cxx_pretty_printer *, tree, int);
79 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
80 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
81 static void dump_expr_list (cxx_pretty_printer *, tree, int);
82 static void dump_global_iord (cxx_pretty_printer *, tree);
83 static void dump_parameters (cxx_pretty_printer *, tree, int);
84 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
85 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
86 static void dump_template_argument (cxx_pretty_printer *, tree, int);
87 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
88 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
89 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
90 vec<tree, va_gc> *);
91 static void dump_scope (cxx_pretty_printer *, tree, int);
92 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
93 static int get_non_default_template_args_count (tree, int);
94 static const char *function_category (tree);
95 static void maybe_print_constexpr_context (diagnostic_context *);
96 static void maybe_print_instantiation_context (diagnostic_context *);
97 static void print_instantiation_full_context (diagnostic_context *);
98 static void print_instantiation_partial_context (diagnostic_context *,
99 struct tinst_level *,
100 location_t);
101 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
102 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
104 static bool cp_printer (pretty_printer *, text_info *, const char *,
105 int, bool, bool, bool, bool *, const char **);
107 /* Struct for handling %H or %I, which require delaying printing the
108 type until a postprocessing stage. */
110 struct deferred_printed_type
112 deferred_printed_type ()
113 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
116 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
117 bool quote)
118 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
119 m_quote (quote)
121 gcc_assert (type);
122 gcc_assert (buffer_ptr);
125 /* The tree is not GTY-marked: they are only non-NULL within a
126 call to pp_format. */
127 tree m_tree;
128 const char **m_buffer_ptr;
129 bool m_verbose;
130 bool m_quote;
133 /* Subclass of format_postprocessor for the C++ frontend.
134 This handles the %H and %I formatting codes, printing them
135 in a postprocessing phase (since they affect each other). */
137 class cxx_format_postprocessor : public format_postprocessor
139 public:
140 cxx_format_postprocessor ()
141 : m_type_a (), m_type_b ()
144 void handle (pretty_printer *pp) FINAL OVERRIDE;
146 deferred_printed_type m_type_a;
147 deferred_printed_type m_type_b;
150 /* CONTEXT->printer is a basic pretty printer that was constructed
151 presumably by diagnostic_initialize(), called early in the
152 compiler's initialization process (in general_init) Before the FE
153 is initialized. This (C++) FE-specific diagnostic initializer is
154 thus replacing the basic pretty printer with one that has C++-aware
155 capacities. */
157 void
158 cxx_initialize_diagnostics (diagnostic_context *context)
160 pretty_printer *base = context->printer;
161 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
162 context->printer = new (pp) cxx_pretty_printer ();
164 /* It is safe to free this object because it was previously XNEW()'d. */
165 base->~pretty_printer ();
166 XDELETE (base);
168 c_common_diagnostics_set_defaults (context);
169 diagnostic_starter (context) = cp_diagnostic_starter;
170 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
171 diagnostic_format_decoder (context) = cp_printer;
172 pp->m_format_postprocessor = new cxx_format_postprocessor ();
175 /* Dump a scope, if deemed necessary. */
177 static void
178 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
180 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
182 if (scope == NULL_TREE)
183 return;
185 if (TREE_CODE (scope) == NAMESPACE_DECL)
187 if (scope != global_namespace)
189 dump_decl (pp, scope, f);
190 pp_cxx_colon_colon (pp);
193 else if (AGGREGATE_TYPE_P (scope))
195 dump_type (pp, scope, f);
196 pp_cxx_colon_colon (pp);
198 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
200 dump_function_decl (pp, scope, f);
201 pp_cxx_colon_colon (pp);
205 /* Dump the template ARGument under control of FLAGS. */
207 static void
208 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
210 if (ARGUMENT_PACK_P (arg))
211 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
212 /* No default args in argument packs. */
213 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
214 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
215 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
216 else
218 if (TREE_CODE (arg) == TREE_LIST)
219 arg = TREE_VALUE (arg);
221 /* Strip implicit conversions. */
222 while (CONVERT_EXPR_P (arg))
223 arg = TREE_OPERAND (arg, 0);
225 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
229 /* Count the number of template arguments ARGS whose value does not
230 match the (optional) default template parameter in PARAMS */
232 static int
233 get_non_default_template_args_count (tree args, int flags)
235 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
237 if (/* We use this flag when generating debug information. We don't
238 want to expand templates at this point, for this may generate
239 new decls, which gets decl counts out of sync, which may in
240 turn cause codegen differences between compilations with and
241 without -g. */
242 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
243 || !flag_pretty_templates)
244 return n;
246 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
249 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
250 of FLAGS. */
252 static void
253 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
255 int n = get_non_default_template_args_count (args, flags);
256 int need_comma = 0;
257 int i;
259 for (i = 0; i < n; ++i)
261 tree arg = TREE_VEC_ELT (args, i);
263 /* Only print a comma if we know there is an argument coming. In
264 the case of an empty template argument pack, no actual
265 argument will be printed. */
266 if (need_comma
267 && (!ARGUMENT_PACK_P (arg)
268 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
269 pp_separate_with_comma (pp);
271 dump_template_argument (pp, arg, flags);
272 need_comma = 1;
276 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
278 static void
279 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
281 tree p;
282 tree a;
284 if (parm == error_mark_node)
285 return;
287 p = TREE_VALUE (parm);
288 a = TREE_PURPOSE (parm);
290 if (TREE_CODE (p) == TYPE_DECL)
292 if (flags & TFF_DECL_SPECIFIERS)
294 pp_cxx_ws_string (pp, "class");
295 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
296 pp_cxx_ws_string (pp, "...");
297 if (DECL_NAME (p))
298 pp_cxx_tree_identifier (pp, DECL_NAME (p));
300 else if (DECL_NAME (p))
301 pp_cxx_tree_identifier (pp, DECL_NAME (p));
302 else
303 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
305 else
306 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
308 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
310 pp_cxx_whitespace (pp);
311 pp_equal (pp);
312 pp_cxx_whitespace (pp);
313 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
314 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
315 else
316 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
320 /* Dump, under control of FLAGS, a template-parameter-list binding.
321 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
322 TREE_VEC. */
324 static void
325 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
326 vec<tree, va_gc> *typenames)
328 bool need_semicolon = false;
329 int i;
330 tree t;
332 while (parms)
334 tree p = TREE_VALUE (parms);
335 int lvl = TMPL_PARMS_DEPTH (parms);
336 int arg_idx = 0;
337 int i;
338 tree lvl_args = NULL_TREE;
340 /* Don't crash if we had an invalid argument list. */
341 if (TMPL_ARGS_DEPTH (args) >= lvl)
342 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
344 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
346 tree arg = NULL_TREE;
348 /* Don't crash if we had an invalid argument list. */
349 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
350 arg = TREE_VEC_ELT (lvl_args, arg_idx);
352 if (need_semicolon)
353 pp_separate_with_semicolon (pp);
354 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
355 TFF_PLAIN_IDENTIFIER);
356 pp_cxx_whitespace (pp);
357 pp_equal (pp);
358 pp_cxx_whitespace (pp);
359 if (arg)
361 if (ARGUMENT_PACK_P (arg))
362 pp_cxx_left_brace (pp);
363 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
364 if (ARGUMENT_PACK_P (arg))
365 pp_cxx_right_brace (pp);
367 else
368 pp_string (pp, M_("<missing>"));
370 ++arg_idx;
371 need_semicolon = true;
374 parms = TREE_CHAIN (parms);
377 /* Don't bother with typenames for a partial instantiation. */
378 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
379 return;
381 /* Don't try to print typenames when we're processing a clone. */
382 if (current_function_decl
383 && !DECL_LANG_SPECIFIC (current_function_decl))
384 return;
386 /* Don't try to do this once cgraph starts throwing away front-end
387 information. */
388 if (at_eof >= 2)
389 return;
391 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
393 if (need_semicolon)
394 pp_separate_with_semicolon (pp);
395 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
396 pp_cxx_whitespace (pp);
397 pp_equal (pp);
398 pp_cxx_whitespace (pp);
399 push_deferring_access_checks (dk_no_check);
400 t = tsubst (t, args, tf_none, NULL_TREE);
401 pop_deferring_access_checks ();
402 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
403 pp_simple_type_specifier doesn't know about it. */
404 t = strip_typedefs (t);
405 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
409 /* Dump a human-readable equivalent of the alias template
410 specialization of T. */
412 static void
413 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
415 gcc_assert (alias_template_specialization_p (t));
417 tree decl = TYPE_NAME (t);
418 if (!(flags & TFF_UNQUALIFIED_NAME))
419 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
420 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
421 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
422 /*primary=*/false,
423 flags & ~TFF_TEMPLATE_HEADER);
426 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
427 format. */
429 static void
430 dump_type (cxx_pretty_printer *pp, tree t, int flags)
432 if (t == NULL_TREE)
433 return;
435 /* Don't print e.g. "struct mytypedef". */
436 if (TYPE_P (t) && typedef_variant_p (t))
438 tree decl = TYPE_NAME (t);
439 if ((flags & TFF_CHASE_TYPEDEF)
440 || DECL_SELF_REFERENCE_P (decl)
441 || (!flag_pretty_templates
442 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
443 t = strip_typedefs (t);
444 else if (alias_template_specialization_p (t))
446 dump_alias_template_specialization (pp, t, flags);
447 return;
449 else if (same_type_p (t, TREE_TYPE (decl)))
450 t = decl;
451 else
453 pp_cxx_cv_qualifier_seq (pp, t);
454 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
455 return;
459 if (TYPE_PTRMEMFUNC_P (t))
460 goto offset_type;
462 switch (TREE_CODE (t))
464 case LANG_TYPE:
465 if (t == init_list_type_node)
466 pp_string (pp, M_("<brace-enclosed initializer list>"));
467 else if (t == unknown_type_node)
468 pp_string (pp, M_("<unresolved overloaded function type>"));
469 else
471 pp_cxx_cv_qualifier_seq (pp, t);
472 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
474 break;
476 case TREE_LIST:
477 /* A list of function parms. */
478 dump_parameters (pp, t, flags);
479 break;
481 case IDENTIFIER_NODE:
482 pp_cxx_tree_identifier (pp, t);
483 break;
485 case TREE_BINFO:
486 dump_type (pp, BINFO_TYPE (t), flags);
487 break;
489 case RECORD_TYPE:
490 case UNION_TYPE:
491 case ENUMERAL_TYPE:
492 dump_aggr_type (pp, t, flags);
493 break;
495 case TYPE_DECL:
496 if (flags & TFF_CHASE_TYPEDEF)
498 dump_type (pp, DECL_ORIGINAL_TYPE (t)
499 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
500 break;
502 /* Fall through. */
504 case TEMPLATE_DECL:
505 case NAMESPACE_DECL:
506 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
507 break;
509 case INTEGER_TYPE:
510 case REAL_TYPE:
511 case VOID_TYPE:
512 case BOOLEAN_TYPE:
513 case COMPLEX_TYPE:
514 case VECTOR_TYPE:
515 case FIXED_POINT_TYPE:
516 pp_type_specifier_seq (pp, t);
517 break;
519 case TEMPLATE_TEMPLATE_PARM:
520 /* For parameters inside template signature. */
521 if (TYPE_IDENTIFIER (t))
522 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
523 else
524 pp_cxx_canonical_template_parameter (pp, t);
525 break;
527 case BOUND_TEMPLATE_TEMPLATE_PARM:
529 tree args = TYPE_TI_ARGS (t);
530 pp_cxx_cv_qualifier_seq (pp, t);
531 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
532 pp_cxx_begin_template_argument_list (pp);
533 dump_template_argument_list (pp, args, flags);
534 pp_cxx_end_template_argument_list (pp);
536 break;
538 case TEMPLATE_TYPE_PARM:
539 pp_cxx_cv_qualifier_seq (pp, t);
540 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
541 pp_cxx_constrained_type_spec (pp, c);
542 else if (TYPE_IDENTIFIER (t))
543 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
544 else
545 pp_cxx_canonical_template_parameter
546 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
547 break;
549 /* This is not always necessary for pointers and such, but doing this
550 reduces code size. */
551 case ARRAY_TYPE:
552 case POINTER_TYPE:
553 case REFERENCE_TYPE:
554 case OFFSET_TYPE:
555 offset_type:
556 case FUNCTION_TYPE:
557 case METHOD_TYPE:
559 dump_type_prefix (pp, t, flags);
560 dump_type_suffix (pp, t, flags);
561 break;
563 case TYPENAME_TYPE:
564 if (! (flags & TFF_CHASE_TYPEDEF)
565 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
567 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
568 break;
570 pp_cxx_cv_qualifier_seq (pp, t);
571 pp_cxx_ws_string (pp,
572 TYPENAME_IS_ENUM_P (t) ? "enum"
573 : TYPENAME_IS_CLASS_P (t) ? "class"
574 : "typename");
575 dump_typename (pp, t, flags);
576 break;
578 case UNBOUND_CLASS_TEMPLATE:
579 if (! (flags & TFF_UNQUALIFIED_NAME))
581 dump_type (pp, TYPE_CONTEXT (t), flags);
582 pp_cxx_colon_colon (pp);
584 pp_cxx_ws_string (pp, "template");
585 dump_type (pp, TYPE_IDENTIFIER (t), flags);
586 break;
588 case TYPEOF_TYPE:
589 pp_cxx_ws_string (pp, "__typeof__");
590 pp_cxx_whitespace (pp);
591 pp_cxx_left_paren (pp);
592 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
593 pp_cxx_right_paren (pp);
594 break;
596 case UNDERLYING_TYPE:
597 pp_cxx_ws_string (pp, "__underlying_type");
598 pp_cxx_whitespace (pp);
599 pp_cxx_left_paren (pp);
600 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
601 pp_cxx_right_paren (pp);
602 break;
604 case TYPE_PACK_EXPANSION:
605 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
606 pp_cxx_ws_string (pp, "...");
607 break;
609 case TYPE_ARGUMENT_PACK:
610 dump_template_argument (pp, t, flags);
611 break;
613 case DECLTYPE_TYPE:
614 pp_cxx_ws_string (pp, "decltype");
615 pp_cxx_whitespace (pp);
616 pp_cxx_left_paren (pp);
617 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
618 pp_cxx_right_paren (pp);
619 break;
621 case NULLPTR_TYPE:
622 pp_string (pp, "std::nullptr_t");
623 break;
625 default:
626 pp_unsupported_tree (pp, t);
627 /* Fall through. */
629 case ERROR_MARK:
630 pp_string (pp, M_("<type error>"));
631 break;
635 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
636 a TYPENAME_TYPE. */
638 static void
639 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
641 tree ctx = TYPE_CONTEXT (t);
643 if (TREE_CODE (ctx) == TYPENAME_TYPE)
644 dump_typename (pp, ctx, flags);
645 else
646 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
647 pp_cxx_colon_colon (pp);
648 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
651 /* Return the name of the supplied aggregate, or enumeral type. */
653 const char *
654 class_key_or_enum_as_string (tree t)
656 if (TREE_CODE (t) == ENUMERAL_TYPE)
658 if (SCOPED_ENUM_P (t))
659 return "enum class";
660 else
661 return "enum";
663 else if (TREE_CODE (t) == UNION_TYPE)
664 return "union";
665 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
666 return "class";
667 else
668 return "struct";
671 /* Print out a class declaration T under the control of FLAGS,
672 in the form `class foo'. */
674 static void
675 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
677 tree name;
678 const char *variety = class_key_or_enum_as_string (t);
679 int typdef = 0;
680 int tmplate = 0;
682 pp_cxx_cv_qualifier_seq (pp, t);
684 if (flags & TFF_CLASS_KEY_OR_ENUM)
685 pp_cxx_ws_string (pp, variety);
687 name = TYPE_NAME (t);
689 if (name)
691 typdef = (!DECL_ARTIFICIAL (name)
692 /* An alias specialization is not considered to be a
693 typedef. */
694 && !alias_template_specialization_p (t));
696 if ((typdef
697 && ((flags & TFF_CHASE_TYPEDEF)
698 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
699 && DECL_TEMPLATE_INFO (name))))
700 || DECL_SELF_REFERENCE_P (name))
702 t = TYPE_MAIN_VARIANT (t);
703 name = TYPE_NAME (t);
704 typdef = 0;
707 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
708 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
709 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
710 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
712 if (! (flags & TFF_UNQUALIFIED_NAME))
713 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
714 flags &= ~TFF_UNQUALIFIED_NAME;
715 if (tmplate)
717 /* Because the template names are mangled, we have to locate
718 the most general template, and use that name. */
719 tree tpl = TYPE_TI_TEMPLATE (t);
721 while (DECL_TEMPLATE_INFO (tpl))
722 tpl = DECL_TI_TEMPLATE (tpl);
723 name = tpl;
725 name = DECL_NAME (name);
728 if (name == 0 || anon_aggrname_p (name))
730 if (flags & TFF_CLASS_KEY_OR_ENUM)
731 pp_string (pp, M_("<unnamed>"));
732 else
733 pp_printf (pp, M_("<unnamed %s>"), variety);
735 else if (LAMBDA_TYPE_P (t))
737 /* A lambda's "type" is essentially its signature. */
738 pp_string (pp, M_("<lambda"));
739 if (lambda_function (t))
740 dump_parameters (pp,
741 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
742 flags);
743 pp_greater (pp);
745 else
746 pp_cxx_tree_identifier (pp, name);
747 if (tmplate)
748 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
749 !CLASSTYPE_USE_TEMPLATE (t),
750 flags & ~TFF_TEMPLATE_HEADER);
753 /* Dump into the obstack the initial part of the output for a given type.
754 This is necessary when dealing with things like functions returning
755 functions. Examples:
757 return type of `int (* fee ())()': pointer -> function -> int. Both
758 pointer (and reference and offset) and function (and member) types must
759 deal with prefix and suffix.
761 Arrays must also do this for DECL nodes, like int a[], and for things like
762 int *[]&. */
764 static void
765 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
767 if (TYPE_PTRMEMFUNC_P (t))
769 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
770 goto offset_type;
773 switch (TREE_CODE (t))
775 case POINTER_TYPE:
776 case REFERENCE_TYPE:
778 tree sub = TREE_TYPE (t);
780 dump_type_prefix (pp, sub, flags);
781 if (TREE_CODE (sub) == ARRAY_TYPE
782 || TREE_CODE (sub) == FUNCTION_TYPE)
784 pp_cxx_whitespace (pp);
785 pp_cxx_left_paren (pp);
786 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
788 if (TYPE_PTR_P (t))
789 pp_star (pp);
790 else if (TYPE_REF_P (t))
792 if (TYPE_REF_IS_RVALUE (t))
793 pp_ampersand_ampersand (pp);
794 else
795 pp_ampersand (pp);
797 pp->padding = pp_before;
798 pp_cxx_cv_qualifier_seq (pp, t);
800 break;
802 case OFFSET_TYPE:
803 offset_type:
804 dump_type_prefix (pp, TREE_TYPE (t), flags);
805 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
807 pp_maybe_space (pp);
808 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
809 pp_cxx_left_paren (pp);
810 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
811 pp_cxx_colon_colon (pp);
813 pp_cxx_star (pp);
814 pp_cxx_cv_qualifier_seq (pp, t);
815 pp->padding = pp_before;
816 break;
818 /* This can be reached without a pointer when dealing with
819 templates, e.g. std::is_function. */
820 case FUNCTION_TYPE:
821 dump_type_prefix (pp, TREE_TYPE (t), flags);
822 break;
824 case METHOD_TYPE:
825 dump_type_prefix (pp, TREE_TYPE (t), flags);
826 pp_maybe_space (pp);
827 pp_cxx_left_paren (pp);
828 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
829 pp_cxx_colon_colon (pp);
830 break;
832 case ARRAY_TYPE:
833 dump_type_prefix (pp, TREE_TYPE (t), flags);
834 break;
836 case ENUMERAL_TYPE:
837 case IDENTIFIER_NODE:
838 case INTEGER_TYPE:
839 case BOOLEAN_TYPE:
840 case REAL_TYPE:
841 case RECORD_TYPE:
842 case TEMPLATE_TYPE_PARM:
843 case TEMPLATE_TEMPLATE_PARM:
844 case BOUND_TEMPLATE_TEMPLATE_PARM:
845 case TREE_LIST:
846 case TYPE_DECL:
847 case TREE_VEC:
848 case UNION_TYPE:
849 case LANG_TYPE:
850 case VOID_TYPE:
851 case TYPENAME_TYPE:
852 case COMPLEX_TYPE:
853 case VECTOR_TYPE:
854 case TYPEOF_TYPE:
855 case UNDERLYING_TYPE:
856 case DECLTYPE_TYPE:
857 case TYPE_PACK_EXPANSION:
858 case FIXED_POINT_TYPE:
859 case NULLPTR_TYPE:
860 dump_type (pp, t, flags);
861 pp->padding = pp_before;
862 break;
864 default:
865 pp_unsupported_tree (pp, t);
866 /* fall through. */
867 case ERROR_MARK:
868 pp_string (pp, M_("<typeprefixerror>"));
869 break;
873 /* Dump the suffix of type T, under control of FLAGS. This is the part
874 which appears after the identifier (or function parms). */
876 static void
877 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
879 if (TYPE_PTRMEMFUNC_P (t))
880 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
882 switch (TREE_CODE (t))
884 case POINTER_TYPE:
885 case REFERENCE_TYPE:
886 case OFFSET_TYPE:
887 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
888 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
889 pp_cxx_right_paren (pp);
890 if (TREE_CODE (t) == POINTER_TYPE)
891 flags |= TFF_POINTER;
892 dump_type_suffix (pp, TREE_TYPE (t), flags);
893 break;
895 case FUNCTION_TYPE:
896 case METHOD_TYPE:
898 tree arg;
899 if (TREE_CODE (t) == METHOD_TYPE)
900 /* Can only be reached through a pointer. */
901 pp_cxx_right_paren (pp);
902 arg = TYPE_ARG_TYPES (t);
903 if (TREE_CODE (t) == METHOD_TYPE)
904 arg = TREE_CHAIN (arg);
906 /* Function pointers don't have default args. Not in standard C++,
907 anyway; they may in g++, but we'll just pretend otherwise. */
908 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
910 pp->padding = pp_before;
911 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
912 TREE_CODE (t) == FUNCTION_TYPE
913 && (flags & TFF_POINTER));
914 dump_ref_qualifier (pp, t, flags);
915 if (tx_safe_fn_type_p (t))
916 pp_cxx_ws_string (pp, "transaction_safe");
917 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
918 dump_type_suffix (pp, TREE_TYPE (t), flags);
919 break;
922 case ARRAY_TYPE:
923 pp_maybe_space (pp);
924 pp_cxx_left_bracket (pp);
925 if (tree dtype = TYPE_DOMAIN (t))
927 tree max = TYPE_MAX_VALUE (dtype);
928 /* Zero-length arrays have an upper bound of SIZE_MAX. */
929 if (integer_all_onesp (max))
930 pp_character (pp, '0');
931 else if (tree_fits_shwi_p (max))
932 pp_wide_integer (pp, tree_to_shwi (max) + 1);
933 else
935 STRIP_NOPS (max);
936 if (TREE_CODE (max) == SAVE_EXPR)
937 max = TREE_OPERAND (max, 0);
938 if (TREE_CODE (max) == MINUS_EXPR
939 || TREE_CODE (max) == PLUS_EXPR)
941 max = TREE_OPERAND (max, 0);
942 while (CONVERT_EXPR_P (max))
943 max = TREE_OPERAND (max, 0);
945 else
946 max = fold_build2_loc (input_location,
947 PLUS_EXPR, dtype, max,
948 build_int_cst (dtype, 1));
949 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
952 pp_cxx_right_bracket (pp);
953 dump_type_suffix (pp, TREE_TYPE (t), flags);
954 break;
956 case ENUMERAL_TYPE:
957 case IDENTIFIER_NODE:
958 case INTEGER_TYPE:
959 case BOOLEAN_TYPE:
960 case REAL_TYPE:
961 case RECORD_TYPE:
962 case TEMPLATE_TYPE_PARM:
963 case TEMPLATE_TEMPLATE_PARM:
964 case BOUND_TEMPLATE_TEMPLATE_PARM:
965 case TREE_LIST:
966 case TYPE_DECL:
967 case TREE_VEC:
968 case UNION_TYPE:
969 case LANG_TYPE:
970 case VOID_TYPE:
971 case TYPENAME_TYPE:
972 case COMPLEX_TYPE:
973 case VECTOR_TYPE:
974 case TYPEOF_TYPE:
975 case UNDERLYING_TYPE:
976 case DECLTYPE_TYPE:
977 case TYPE_PACK_EXPANSION:
978 case FIXED_POINT_TYPE:
979 case NULLPTR_TYPE:
980 break;
982 default:
983 pp_unsupported_tree (pp, t);
984 case ERROR_MARK:
985 /* Don't mark it here, we should have already done in
986 dump_type_prefix. */
987 break;
991 static void
992 dump_global_iord (cxx_pretty_printer *pp, tree t)
994 const char *p = NULL;
996 if (DECL_GLOBAL_CTOR_P (t))
997 p = M_("(static initializers for %s)");
998 else if (DECL_GLOBAL_DTOR_P (t))
999 p = M_("(static destructors for %s)");
1000 else
1001 gcc_unreachable ();
1003 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1006 static void
1007 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1009 if (flags & TFF_DECL_SPECIFIERS)
1011 if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1013 if (DECL_LANG_SPECIFIC (t) && DECL_DECLARED_CONCEPT_P (t))
1014 pp_cxx_ws_string (pp, "concept");
1015 else
1016 pp_cxx_ws_string (pp, "constexpr");
1018 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1019 pp_maybe_space (pp);
1021 if (! (flags & TFF_UNQUALIFIED_NAME)
1022 && TREE_CODE (t) != PARM_DECL
1023 && (!DECL_INITIAL (t)
1024 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1025 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1026 flags &= ~TFF_UNQUALIFIED_NAME;
1027 if ((flags & TFF_DECL_SPECIFIERS)
1028 && DECL_TEMPLATE_PARM_P (t)
1029 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1030 pp_string (pp, "...");
1031 if (DECL_NAME (t))
1033 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1035 pp_less (pp);
1036 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1037 pp_string (pp, " capture>");
1039 else
1040 dump_decl (pp, DECL_NAME (t), flags);
1042 else if (DECL_DECOMPOSITION_P (t))
1043 pp_string (pp, M_("<structured bindings>"));
1044 else
1045 pp_string (pp, M_("<anonymous>"));
1046 if (flags & TFF_DECL_SPECIFIERS)
1047 dump_type_suffix (pp, type, flags);
1050 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1052 static void
1053 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1055 /* These special cases are duplicated here so that other functions
1056 can feed identifiers to error and get them demangled properly. */
1057 if (IDENTIFIER_CONV_OP_P (t))
1059 pp_cxx_ws_string (pp, "operator");
1060 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1061 dump_type (pp, TREE_TYPE (t), flags);
1062 return;
1064 if (dguide_name_p (t))
1066 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1067 TFF_UNQUALIFIED_NAME);
1068 return;
1071 const char *str = IDENTIFIER_POINTER (t);
1072 if (!strncmp (str, "_ZGR", 3))
1074 pp_cxx_ws_string (pp, "<temporary>");
1075 return;
1078 pp_cxx_tree_identifier (pp, t);
1081 /* Dump a human readable string for the decl T under control of FLAGS. */
1083 static void
1084 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1086 if (t == NULL_TREE)
1087 return;
1089 /* If doing Objective-C++, give Objective-C a chance to demangle
1090 Objective-C method names. */
1091 if (c_dialect_objc ())
1093 const char *demangled = objc_maybe_printable_name (t, flags);
1094 if (demangled)
1096 pp_string (pp, demangled);
1097 return;
1101 switch (TREE_CODE (t))
1103 case TYPE_DECL:
1104 /* Don't say 'typedef class A' */
1105 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1107 if ((flags & TFF_DECL_SPECIFIERS)
1108 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1110 /* Say `class T' not just `T'. */
1111 pp_cxx_ws_string (pp, "class");
1113 /* Emit the `...' for a parameter pack. */
1114 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1115 pp_cxx_ws_string (pp, "...");
1118 dump_type (pp, TREE_TYPE (t), flags);
1119 break;
1121 if (TYPE_DECL_ALIAS_P (t)
1122 && (flags & TFF_DECL_SPECIFIERS
1123 || flags & TFF_CLASS_KEY_OR_ENUM))
1125 pp_cxx_ws_string (pp, "using");
1126 dump_decl (pp, DECL_NAME (t), flags);
1127 pp_cxx_whitespace (pp);
1128 pp_cxx_ws_string (pp, "=");
1129 pp_cxx_whitespace (pp);
1130 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1131 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1132 flags);
1133 break;
1135 if ((flags & TFF_DECL_SPECIFIERS)
1136 && !DECL_SELF_REFERENCE_P (t))
1137 pp_cxx_ws_string (pp, "typedef");
1138 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1139 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1140 flags);
1141 break;
1143 case VAR_DECL:
1144 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1146 pp_string (pp, M_("vtable for "));
1147 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1148 dump_type (pp, DECL_CONTEXT (t), flags);
1149 break;
1151 /* Fall through. */
1152 case FIELD_DECL:
1153 case PARM_DECL:
1154 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1156 /* Handle variable template specializations. */
1157 if (VAR_P (t)
1158 && DECL_LANG_SPECIFIC (t)
1159 && DECL_TEMPLATE_INFO (t)
1160 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1162 pp_cxx_begin_template_argument_list (pp);
1163 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1164 dump_template_argument_list (pp, args, flags);
1165 pp_cxx_end_template_argument_list (pp);
1167 break;
1169 case RESULT_DECL:
1170 pp_string (pp, M_("<return value> "));
1171 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1172 break;
1174 case NAMESPACE_DECL:
1175 if (flags & TFF_DECL_SPECIFIERS)
1176 pp->declaration (t);
1177 else
1179 if (! (flags & TFF_UNQUALIFIED_NAME))
1180 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1181 flags &= ~TFF_UNQUALIFIED_NAME;
1182 if (DECL_NAME (t) == NULL_TREE)
1184 if (!(pp->flags & pp_c_flag_gnu_v3))
1185 pp_cxx_ws_string (pp, M_("{anonymous}"));
1186 else
1187 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1189 else
1190 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1192 break;
1194 case SCOPE_REF:
1195 dump_type (pp, TREE_OPERAND (t, 0), flags);
1196 pp_cxx_colon_colon (pp);
1197 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1198 break;
1200 case ARRAY_REF:
1201 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1202 pp_cxx_left_bracket (pp);
1203 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1204 pp_cxx_right_bracket (pp);
1205 break;
1207 /* So that we can do dump_decl on an aggr type. */
1208 case RECORD_TYPE:
1209 case UNION_TYPE:
1210 case ENUMERAL_TYPE:
1211 dump_type (pp, t, flags);
1212 break;
1214 case BIT_NOT_EXPR:
1215 /* This is a pseudo destructor call which has not been folded into
1216 a PSEUDO_DTOR_EXPR yet. */
1217 pp_cxx_complement (pp);
1218 dump_type (pp, TREE_OPERAND (t, 0), flags);
1219 break;
1221 case TYPE_EXPR:
1222 gcc_unreachable ();
1223 break;
1225 case IDENTIFIER_NODE:
1226 dump_decl_name (pp, t, flags);
1227 break;
1229 case OVERLOAD:
1230 if (!OVL_SINGLE_P (t))
1232 tree ctx = ovl_scope (t);
1233 if (ctx != global_namespace)
1235 if (TYPE_P (ctx))
1236 dump_type (pp, ctx, flags);
1237 else
1238 dump_decl (pp, ctx, flags);
1239 pp_cxx_colon_colon (pp);
1241 dump_decl (pp, OVL_NAME (t), flags);
1242 break;
1245 /* If there's only one function, just treat it like an ordinary
1246 FUNCTION_DECL. */
1247 t = OVL_FIRST (t);
1248 /* Fall through. */
1250 case FUNCTION_DECL:
1251 if (! DECL_LANG_SPECIFIC (t))
1253 if (DECL_ABSTRACT_ORIGIN (t)
1254 && DECL_ABSTRACT_ORIGIN (t) != t)
1255 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1256 else
1257 dump_function_name (pp, t, flags);
1259 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1260 dump_global_iord (pp, t);
1261 else
1262 dump_function_decl (pp, t, flags);
1263 break;
1265 case TEMPLATE_DECL:
1266 dump_template_decl (pp, t, flags);
1267 break;
1269 case TEMPLATE_ID_EXPR:
1271 tree name = TREE_OPERAND (t, 0);
1272 tree args = TREE_OPERAND (t, 1);
1274 if (!identifier_p (name))
1275 name = OVL_NAME (name);
1276 dump_decl (pp, name, flags);
1277 pp_cxx_begin_template_argument_list (pp);
1278 if (args == error_mark_node)
1279 pp_string (pp, M_("<template arguments error>"));
1280 else if (args)
1281 dump_template_argument_list
1282 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1283 pp_cxx_end_template_argument_list (pp);
1285 break;
1287 case LABEL_DECL:
1288 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1289 break;
1291 case CONST_DECL:
1292 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1293 || (DECL_INITIAL (t) &&
1294 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1295 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1296 else if (DECL_NAME (t))
1297 dump_decl (pp, DECL_NAME (t), flags);
1298 else if (DECL_INITIAL (t))
1299 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1300 else
1301 pp_string (pp, M_("<enumerator>"));
1302 break;
1304 case USING_DECL:
1306 pp_cxx_ws_string (pp, "using");
1307 tree scope = USING_DECL_SCOPE (t);
1308 bool variadic = false;
1309 if (PACK_EXPANSION_P (scope))
1311 scope = PACK_EXPANSION_PATTERN (scope);
1312 variadic = true;
1314 dump_type (pp, scope, flags);
1315 pp_cxx_colon_colon (pp);
1316 dump_decl (pp, DECL_NAME (t), flags);
1317 if (variadic)
1318 pp_cxx_ws_string (pp, "...");
1320 break;
1322 case STATIC_ASSERT:
1323 pp->declaration (t);
1324 break;
1326 case BASELINK:
1327 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1328 break;
1330 case NON_DEPENDENT_EXPR:
1331 dump_expr (pp, t, flags);
1332 break;
1334 case TEMPLATE_TYPE_PARM:
1335 if (flags & TFF_DECL_SPECIFIERS)
1336 pp->declaration (t);
1337 else
1338 pp->type_id (t);
1339 break;
1341 case UNBOUND_CLASS_TEMPLATE:
1342 case TYPE_PACK_EXPANSION:
1343 case TREE_BINFO:
1344 dump_type (pp, t, flags);
1345 break;
1347 default:
1348 pp_unsupported_tree (pp, t);
1349 /* Fall through. */
1351 case ERROR_MARK:
1352 pp_string (pp, M_("<declaration error>"));
1353 break;
1357 /* Dump a template declaration T under control of FLAGS. This means the
1358 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1360 static void
1361 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1363 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1364 tree parms;
1365 int i;
1367 if (flags & TFF_TEMPLATE_HEADER)
1369 for (parms = orig_parms = nreverse (orig_parms);
1370 parms;
1371 parms = TREE_CHAIN (parms))
1373 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1374 int len = TREE_VEC_LENGTH (inner_parms);
1376 if (len == 0)
1378 /* Skip over the dummy template levels of a template template
1379 parm. */
1380 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1381 continue;
1384 pp_cxx_ws_string (pp, "template");
1385 pp_cxx_begin_template_argument_list (pp);
1387 /* If we've shown the template prefix, we'd better show the
1388 parameters' and decl's type too. */
1389 flags |= TFF_DECL_SPECIFIERS;
1391 for (i = 0; i < len; i++)
1393 if (i)
1394 pp_separate_with_comma (pp);
1395 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1396 flags);
1398 pp_cxx_end_template_argument_list (pp);
1399 pp_cxx_whitespace (pp);
1401 nreverse(orig_parms);
1403 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1405 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1406 pp_cxx_ws_string (pp, "class");
1408 /* If this is a parameter pack, print the ellipsis. */
1409 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1410 pp_cxx_ws_string (pp, "...");
1413 /* Only print the requirements if we're also printing
1414 the template header. */
1415 if (flag_concepts)
1416 if (tree ci = get_constraints (t))
1417 if (check_constraint_info (ci))
1418 if (tree reqs = CI_TEMPLATE_REQS (ci))
1420 pp_cxx_requires_clause (pp, reqs);
1421 pp_cxx_whitespace (pp);
1426 if (DECL_CLASS_TEMPLATE_P (t))
1427 dump_type (pp, TREE_TYPE (t),
1428 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1429 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1430 else if (DECL_TEMPLATE_RESULT (t)
1431 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1432 /* Alias template. */
1433 || DECL_TYPE_TEMPLATE_P (t)))
1434 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1435 else
1437 gcc_assert (TREE_TYPE (t));
1438 switch (NEXT_CODE (t))
1440 case METHOD_TYPE:
1441 case FUNCTION_TYPE:
1442 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1443 break;
1444 default:
1445 /* This case can occur with some invalid code. */
1446 dump_type (pp, TREE_TYPE (t),
1447 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1448 | (flags & TFF_DECL_SPECIFIERS
1449 ? TFF_CLASS_KEY_OR_ENUM : 0));
1454 /* find_typenames looks through the type of the function template T
1455 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1456 it finds. */
1458 struct find_typenames_t
1460 hash_set<tree> *p_set;
1461 vec<tree, va_gc> *typenames;
1464 static tree
1465 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1467 struct find_typenames_t *d = (struct find_typenames_t *)data;
1468 tree mv = NULL_TREE;
1470 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1471 /* Add the type of the typedef without any additional cv-quals. */
1472 mv = TREE_TYPE (TYPE_NAME (*tp));
1473 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1474 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1475 /* Add the typename without any cv-qualifiers. */
1476 mv = TYPE_MAIN_VARIANT (*tp);
1478 if (PACK_EXPANSION_P (*tp))
1480 /* Don't mess with parameter packs since we don't remember
1481 the pack expansion context for a particular typename. */
1482 *walk_subtrees = false;
1483 return NULL_TREE;
1486 if (mv && (mv == *tp || !d->p_set->add (mv)))
1487 vec_safe_push (d->typenames, mv);
1489 /* Search into class template arguments, which cp_walk_subtrees
1490 doesn't do. */
1491 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1492 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1493 data, d->p_set);
1495 return NULL_TREE;
1498 static vec<tree, va_gc> *
1499 find_typenames (tree t)
1501 struct find_typenames_t ft;
1502 ft.p_set = new hash_set<tree>;
1503 ft.typenames = NULL;
1504 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1505 find_typenames_r, &ft, ft.p_set);
1506 delete ft.p_set;
1507 return ft.typenames;
1510 /* Output the "[with ...]" clause for a template instantiation T iff
1511 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1512 formatting a deduction/substitution diagnostic rather than an
1513 instantiation. */
1515 static void
1516 dump_substitution (cxx_pretty_printer *pp,
1517 tree t, tree template_parms, tree template_args,
1518 int flags)
1520 if (template_parms != NULL_TREE && template_args != NULL_TREE
1521 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1523 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1524 pp_cxx_whitespace (pp);
1525 pp_cxx_left_bracket (pp);
1526 pp->translate_string ("with");
1527 pp_cxx_whitespace (pp);
1528 dump_template_bindings (pp, template_parms, template_args, typenames);
1529 pp_cxx_right_bracket (pp);
1533 /* Dump the lambda function FN including its 'mutable' qualifier and any
1534 template bindings. */
1536 static void
1537 dump_lambda_function (cxx_pretty_printer *pp,
1538 tree fn, tree template_parms, tree template_args,
1539 int flags)
1541 /* A lambda's signature is essentially its "type". */
1542 dump_type (pp, DECL_CONTEXT (fn), flags);
1543 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1545 pp->padding = pp_before;
1546 pp_c_ws_string (pp, "mutable");
1548 dump_substitution (pp, fn, template_parms, template_args, flags);
1551 /* Pretty print a function decl. There are several ways we want to print a
1552 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1553 As error can only apply the '#' flag once to give 0 and 1 for V, there
1554 is %D which doesn't print the throw specs, and %F which does. */
1556 static void
1557 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1559 tree fntype;
1560 tree parmtypes;
1561 tree cname = NULL_TREE;
1562 tree template_args = NULL_TREE;
1563 tree template_parms = NULL_TREE;
1564 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1565 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1566 tree exceptions;
1567 bool constexpr_p;
1568 tree ret = NULL_TREE;
1570 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1571 if (TREE_CODE (t) == TEMPLATE_DECL)
1572 t = DECL_TEMPLATE_RESULT (t);
1574 /* Save the exceptions, in case t is a specialization and we are
1575 emitting an error about incompatible specifications. */
1576 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1578 /* Likewise for the constexpr specifier, in case t is a specialization. */
1579 constexpr_p = DECL_DECLARED_CONSTEXPR_P (t);
1581 /* Pretty print template instantiations only. */
1582 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1583 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1584 && flag_pretty_templates)
1586 tree tmpl;
1588 template_args = DECL_TI_ARGS (t);
1589 tmpl = most_general_template (t);
1590 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1592 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1593 t = tmpl;
1597 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1598 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1600 fntype = TREE_TYPE (t);
1601 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1603 if (DECL_CLASS_SCOPE_P (t))
1604 cname = DECL_CONTEXT (t);
1605 /* This is for partially instantiated template methods. */
1606 else if (TREE_CODE (fntype) == METHOD_TYPE)
1607 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1609 if (flags & TFF_DECL_SPECIFIERS)
1611 if (DECL_STATIC_FUNCTION_P (t))
1612 pp_cxx_ws_string (pp, "static");
1613 else if (DECL_VIRTUAL_P (t))
1614 pp_cxx_ws_string (pp, "virtual");
1616 if (constexpr_p)
1618 if (DECL_DECLARED_CONCEPT_P (t))
1619 pp_cxx_ws_string (pp, "concept");
1620 else
1621 pp_cxx_ws_string (pp, "constexpr");
1625 /* Print the return type? */
1626 if (show_return)
1627 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1628 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1629 if (show_return)
1631 ret = fndecl_declared_return_type (t);
1632 dump_type_prefix (pp, ret, flags);
1635 /* Print the function name. */
1636 if (!do_outer_scope)
1637 /* Nothing. */;
1638 else if (cname)
1640 dump_type (pp, cname, flags);
1641 pp_cxx_colon_colon (pp);
1643 else
1644 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1646 dump_function_name (pp, t, flags);
1648 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1650 dump_parameters (pp, parmtypes, flags);
1652 if (TREE_CODE (fntype) == METHOD_TYPE)
1654 pp->padding = pp_before;
1655 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1656 dump_ref_qualifier (pp, fntype, flags);
1659 if (tx_safe_fn_type_p (fntype))
1661 pp->padding = pp_before;
1662 pp_cxx_ws_string (pp, "transaction_safe");
1665 if (flags & TFF_EXCEPTION_SPECIFICATION)
1667 pp->padding = pp_before;
1668 dump_exception_spec (pp, exceptions, flags);
1671 if (show_return)
1672 dump_type_suffix (pp, ret, flags);
1673 else if (deduction_guide_p (t))
1675 pp_cxx_ws_string (pp, "->");
1676 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1679 if (flag_concepts)
1680 if (tree ci = get_constraints (t))
1681 if (tree reqs = CI_DECLARATOR_REQS (ci))
1682 pp_cxx_requires_clause (pp, reqs);
1684 dump_substitution (pp, t, template_parms, template_args, flags);
1686 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1688 pp_cxx_ws_string (pp, "[inherited from");
1689 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1690 pp_character (pp, ']');
1693 else if (template_args)
1695 bool need_comma = false;
1696 int i;
1697 pp_cxx_begin_template_argument_list (pp);
1698 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1699 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1701 tree arg = TREE_VEC_ELT (template_args, i);
1702 if (need_comma)
1703 pp_separate_with_comma (pp);
1704 if (ARGUMENT_PACK_P (arg))
1705 pp_cxx_left_brace (pp);
1706 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1707 if (ARGUMENT_PACK_P (arg))
1708 pp_cxx_right_brace (pp);
1709 need_comma = true;
1711 pp_cxx_end_template_argument_list (pp);
1715 /* Print a parameter list. If this is for a member function, the
1716 member object ptr (and any other hidden args) should have
1717 already been removed. */
1719 static void
1720 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1722 int first = 1;
1723 flags &= ~TFF_SCOPE;
1724 pp_cxx_left_paren (pp);
1726 for (first = 1; parmtypes != void_list_node;
1727 parmtypes = TREE_CHAIN (parmtypes))
1729 if (!first)
1730 pp_separate_with_comma (pp);
1731 first = 0;
1732 if (!parmtypes)
1734 pp_cxx_ws_string (pp, "...");
1735 break;
1738 dump_type (pp, TREE_VALUE (parmtypes), flags);
1740 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1742 pp_cxx_whitespace (pp);
1743 pp_equal (pp);
1744 pp_cxx_whitespace (pp);
1745 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1749 pp_cxx_right_paren (pp);
1752 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1754 static void
1755 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1757 if (FUNCTION_REF_QUALIFIED (t))
1759 pp->padding = pp_before;
1760 if (FUNCTION_RVALUE_QUALIFIED (t))
1761 pp_cxx_ws_string (pp, "&&");
1762 else
1763 pp_cxx_ws_string (pp, "&");
1767 /* Print an exception specification. T is the exception specification. */
1769 static void
1770 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1772 if (t && TREE_PURPOSE (t))
1774 pp_cxx_ws_string (pp, "noexcept");
1775 if (!integer_onep (TREE_PURPOSE (t)))
1777 pp_cxx_whitespace (pp);
1778 pp_cxx_left_paren (pp);
1779 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1780 pp_cxx_ws_string (pp, "<uninstantiated>");
1781 else
1782 dump_expr (pp, TREE_PURPOSE (t), flags);
1783 pp_cxx_right_paren (pp);
1786 else if (t)
1788 pp_cxx_ws_string (pp, "throw");
1789 pp_cxx_whitespace (pp);
1790 pp_cxx_left_paren (pp);
1791 if (TREE_VALUE (t) != NULL_TREE)
1792 while (1)
1794 dump_type (pp, TREE_VALUE (t), flags);
1795 t = TREE_CHAIN (t);
1796 if (!t)
1797 break;
1798 pp_separate_with_comma (pp);
1800 pp_cxx_right_paren (pp);
1804 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1805 and destructors properly. */
1807 static void
1808 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1810 tree name = DECL_NAME (t);
1812 /* We can get here with a decl that was synthesized by language-
1813 independent machinery (e.g. coverage.c) in which case it won't
1814 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1815 will crash. In this case it is safe just to print out the
1816 literal name. */
1817 if (!DECL_LANG_SPECIFIC (t))
1819 pp_cxx_tree_identifier (pp, name);
1820 return;
1823 if (TREE_CODE (t) == TEMPLATE_DECL)
1824 t = DECL_TEMPLATE_RESULT (t);
1826 /* Don't let the user see __comp_ctor et al. */
1827 if (DECL_CONSTRUCTOR_P (t)
1828 || DECL_DESTRUCTOR_P (t))
1830 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1831 name = get_identifier ("<lambda>");
1832 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1833 name = get_identifier ("<constructor>");
1834 else
1835 name = constructor_name (DECL_CONTEXT (t));
1838 if (DECL_DESTRUCTOR_P (t))
1840 pp_cxx_complement (pp);
1841 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1843 else if (DECL_CONV_FN_P (t))
1845 /* This cannot use the hack that the operator's return
1846 type is stashed off of its name because it may be
1847 used for error reporting. In the case of conflicting
1848 declarations, both will have the same name, yet
1849 the types will be different, hence the TREE_TYPE field
1850 of the first name will be clobbered by the second. */
1851 pp_cxx_ws_string (pp, "operator");
1852 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1854 else
1855 dump_decl (pp, name, flags);
1857 if (DECL_TEMPLATE_INFO (t)
1858 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1859 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1860 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1861 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1862 flags);
1865 /* Dump the template parameters from the template info INFO under control of
1866 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1867 specialization (partial or complete). For partial specializations we show
1868 the specialized parameter values. For a primary template we show no
1869 decoration. */
1871 static void
1872 dump_template_parms (cxx_pretty_printer *pp, tree info,
1873 int primary, int flags)
1875 tree args = info ? TI_ARGS (info) : NULL_TREE;
1877 if (primary && flags & TFF_TEMPLATE_NAME)
1878 return;
1879 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1880 pp_cxx_begin_template_argument_list (pp);
1882 /* Be careful only to print things when we have them, so as not
1883 to crash producing error messages. */
1884 if (args && !primary)
1886 int len, ix;
1887 len = get_non_default_template_args_count (args, flags);
1889 args = INNERMOST_TEMPLATE_ARGS (args);
1890 for (ix = 0; ix != len; ix++)
1892 tree arg = TREE_VEC_ELT (args, ix);
1894 /* Only print a comma if we know there is an argument coming. In
1895 the case of an empty template argument pack, no actual
1896 argument will be printed. */
1897 if (ix
1898 && (!ARGUMENT_PACK_P (arg)
1899 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1900 pp_separate_with_comma (pp);
1902 if (!arg)
1903 pp_string (pp, M_("<template parameter error>"));
1904 else
1905 dump_template_argument (pp, arg, flags);
1908 else if (primary)
1910 tree tpl = TI_TEMPLATE (info);
1911 tree parms = DECL_TEMPLATE_PARMS (tpl);
1912 int len, ix;
1914 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1915 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1917 for (ix = 0; ix != len; ix++)
1919 tree parm;
1921 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1923 pp_string (pp, M_("<template parameter error>"));
1924 continue;
1927 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1929 if (ix)
1930 pp_separate_with_comma (pp);
1932 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1935 pp_cxx_end_template_argument_list (pp);
1938 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1939 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1941 static void
1942 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1944 tree arg;
1945 call_expr_arg_iterator iter;
1947 pp_cxx_left_paren (pp);
1948 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1950 if (skipfirst)
1951 skipfirst = false;
1952 else
1954 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1955 if (more_call_expr_args_p (&iter))
1956 pp_separate_with_comma (pp);
1959 pp_cxx_right_paren (pp);
1962 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1963 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1964 true. */
1966 static void
1967 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1968 bool skipfirst)
1970 tree arg;
1971 aggr_init_expr_arg_iterator iter;
1973 pp_cxx_left_paren (pp);
1974 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1976 if (skipfirst)
1977 skipfirst = false;
1978 else
1980 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1981 if (more_aggr_init_expr_args_p (&iter))
1982 pp_separate_with_comma (pp);
1985 pp_cxx_right_paren (pp);
1988 /* Print out a list of initializers (subr of dump_expr). */
1990 static void
1991 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
1993 while (l)
1995 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1996 l = TREE_CHAIN (l);
1997 if (l)
1998 pp_separate_with_comma (pp);
2002 /* Print out a vector of initializers (subr of dump_expr). */
2004 static void
2005 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2006 int flags)
2008 unsigned HOST_WIDE_INT idx;
2009 tree value;
2011 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2013 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2014 if (idx != v->length () - 1)
2015 pp_separate_with_comma (pp);
2020 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2021 function. Resolve it to a close relative -- in the sense of static
2022 type -- variant being overridden. That is close to what was written in
2023 the source code. Subroutine of dump_expr. */
2025 static tree
2026 resolve_virtual_fun_from_obj_type_ref (tree ref)
2028 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2029 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2030 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2031 while (index)
2033 fun = TREE_CHAIN (fun);
2034 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2035 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2038 return BV_FN (fun);
2041 /* Print out an expression E under control of FLAGS. */
2043 static void
2044 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2046 tree op;
2048 if (t == 0)
2049 return;
2051 if (STATEMENT_CLASS_P (t))
2053 pp_cxx_ws_string (pp, M_("<statement>"));
2054 return;
2057 switch (TREE_CODE (t))
2059 case VAR_DECL:
2060 case PARM_DECL:
2061 case FIELD_DECL:
2062 case CONST_DECL:
2063 case FUNCTION_DECL:
2064 case TEMPLATE_DECL:
2065 case NAMESPACE_DECL:
2066 case LABEL_DECL:
2067 case OVERLOAD:
2068 case TYPE_DECL:
2069 case IDENTIFIER_NODE:
2070 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2071 |TFF_TEMPLATE_HEADER))
2072 | TFF_NO_TEMPLATE_BINDINGS
2073 | TFF_NO_FUNCTION_ARGUMENTS));
2074 break;
2076 case SSA_NAME:
2077 if (SSA_NAME_VAR (t)
2078 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2079 dump_expr (pp, SSA_NAME_VAR (t), flags);
2080 else
2081 pp_cxx_ws_string (pp, M_("<unknown>"));
2082 break;
2084 case VOID_CST:
2085 case INTEGER_CST:
2086 case REAL_CST:
2087 case STRING_CST:
2088 case COMPLEX_CST:
2089 pp->constant (t);
2090 break;
2092 case USERDEF_LITERAL:
2093 pp_cxx_userdef_literal (pp, t);
2094 break;
2096 case THROW_EXPR:
2097 /* While waiting for caret diagnostics, avoid printing
2098 __cxa_allocate_exception, __cxa_throw, and the like. */
2099 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2100 break;
2102 case PTRMEM_CST:
2103 pp_ampersand (pp);
2104 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2105 pp_cxx_colon_colon (pp);
2106 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2107 break;
2109 case COMPOUND_EXPR:
2110 pp_cxx_left_paren (pp);
2111 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2112 pp_separate_with_comma (pp);
2113 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2114 pp_cxx_right_paren (pp);
2115 break;
2117 case COND_EXPR:
2118 case VEC_COND_EXPR:
2119 pp_cxx_left_paren (pp);
2120 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2121 pp_string (pp, " ? ");
2122 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2123 pp_string (pp, " : ");
2124 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2125 pp_cxx_right_paren (pp);
2126 break;
2128 case SAVE_EXPR:
2129 if (TREE_HAS_CONSTRUCTOR (t))
2131 pp_cxx_ws_string (pp, "new");
2132 pp_cxx_whitespace (pp);
2133 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2135 else
2136 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2137 break;
2139 case AGGR_INIT_EXPR:
2141 tree fn = NULL_TREE;
2143 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2144 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2146 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2148 if (DECL_CONSTRUCTOR_P (fn))
2149 dump_type (pp, DECL_CONTEXT (fn), flags);
2150 else
2151 dump_decl (pp, fn, 0);
2153 else
2154 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2156 dump_aggr_init_expr_args (pp, t, flags, true);
2157 break;
2159 case CALL_EXPR:
2161 tree fn = CALL_EXPR_FN (t);
2162 bool skipfirst = false;
2164 /* Deal with internal functions. */
2165 if (fn == NULL_TREE)
2167 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2168 dump_call_expr_args (pp, t, flags, skipfirst);
2169 break;
2172 if (TREE_CODE (fn) == ADDR_EXPR)
2173 fn = TREE_OPERAND (fn, 0);
2175 /* Nobody is interested in seeing the guts of vcalls. */
2176 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2177 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2179 if (TREE_TYPE (fn) != NULL_TREE
2180 && NEXT_CODE (fn) == METHOD_TYPE
2181 && call_expr_nargs (t))
2183 tree ob = CALL_EXPR_ARG (t, 0);
2184 if (TREE_CODE (ob) == ADDR_EXPR)
2186 dump_expr (pp, TREE_OPERAND (ob, 0),
2187 flags | TFF_EXPR_IN_PARENS);
2188 pp_cxx_dot (pp);
2190 else if (!is_this_parameter (ob))
2192 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2193 pp_cxx_arrow (pp);
2195 skipfirst = true;
2197 if (flag_sanitize & SANITIZE_UNDEFINED
2198 && is_ubsan_builtin_p (fn))
2200 pp_string (cxx_pp, M_("<ubsan routine call>"));
2201 break;
2203 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2204 dump_call_expr_args (pp, t, flags, skipfirst);
2206 break;
2208 case TARGET_EXPR:
2209 /* Note that this only works for G++ target exprs. If somebody
2210 builds a general TARGET_EXPR, there's no way to represent that
2211 it initializes anything other that the parameter slot for the
2212 default argument. Note we may have cleared out the first
2213 operand in expand_expr, so don't go killing ourselves. */
2214 if (TREE_OPERAND (t, 1))
2215 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2216 break;
2218 case POINTER_PLUS_EXPR:
2219 dump_binary_op (pp, "+", t, flags);
2220 break;
2222 case POINTER_DIFF_EXPR:
2223 dump_binary_op (pp, "-", t, flags);
2224 break;
2226 case INIT_EXPR:
2227 case MODIFY_EXPR:
2228 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2229 break;
2231 case PLUS_EXPR:
2232 case MINUS_EXPR:
2233 case MULT_EXPR:
2234 case TRUNC_DIV_EXPR:
2235 case TRUNC_MOD_EXPR:
2236 case MIN_EXPR:
2237 case MAX_EXPR:
2238 case LSHIFT_EXPR:
2239 case RSHIFT_EXPR:
2240 case BIT_IOR_EXPR:
2241 case BIT_XOR_EXPR:
2242 case BIT_AND_EXPR:
2243 case TRUTH_ANDIF_EXPR:
2244 case TRUTH_ORIF_EXPR:
2245 case LT_EXPR:
2246 case LE_EXPR:
2247 case GT_EXPR:
2248 case GE_EXPR:
2249 case EQ_EXPR:
2250 case NE_EXPR:
2251 case EXACT_DIV_EXPR:
2252 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2253 break;
2255 case CEIL_DIV_EXPR:
2256 case FLOOR_DIV_EXPR:
2257 case ROUND_DIV_EXPR:
2258 case RDIV_EXPR:
2259 dump_binary_op (pp, "/", t, flags);
2260 break;
2262 case CEIL_MOD_EXPR:
2263 case FLOOR_MOD_EXPR:
2264 case ROUND_MOD_EXPR:
2265 dump_binary_op (pp, "%", t, flags);
2266 break;
2268 case COMPONENT_REF:
2270 tree ob = TREE_OPERAND (t, 0);
2271 if (INDIRECT_REF_P (ob))
2273 ob = TREE_OPERAND (ob, 0);
2274 if (!is_this_parameter (ob))
2276 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2277 if (TYPE_REF_P (TREE_TYPE (ob)))
2278 pp_cxx_dot (pp);
2279 else
2280 pp_cxx_arrow (pp);
2283 else
2285 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2286 if (TREE_CODE (ob) != ARROW_EXPR)
2287 pp_cxx_dot (pp);
2289 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2291 break;
2293 case ARRAY_REF:
2294 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2295 pp_cxx_left_bracket (pp);
2296 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2297 pp_cxx_right_bracket (pp);
2298 break;
2300 case UNARY_PLUS_EXPR:
2301 dump_unary_op (pp, "+", t, flags);
2302 break;
2304 case ADDR_EXPR:
2305 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2306 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2307 /* An ADDR_EXPR can have reference type. In that case, we
2308 shouldn't print the `&' doing so indicates to the user
2309 that the expression has pointer type. */
2310 || (TREE_TYPE (t)
2311 && TYPE_REF_P (TREE_TYPE (t))))
2312 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2313 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2314 dump_unary_op (pp, "&&", t, flags);
2315 else
2316 dump_unary_op (pp, "&", t, flags);
2317 break;
2319 case INDIRECT_REF:
2320 if (TREE_HAS_CONSTRUCTOR (t))
2322 t = TREE_OPERAND (t, 0);
2323 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2324 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2325 dump_call_expr_args (pp, t, flags, true);
2327 else
2329 if (TREE_OPERAND (t,0) != NULL_TREE
2330 && TREE_TYPE (TREE_OPERAND (t, 0))
2331 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2332 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2333 else
2334 dump_unary_op (pp, "*", t, flags);
2336 break;
2338 case MEM_REF:
2339 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2340 && integer_zerop (TREE_OPERAND (t, 1)))
2341 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2342 else
2344 pp_cxx_star (pp);
2345 if (!integer_zerop (TREE_OPERAND (t, 1)))
2347 pp_cxx_left_paren (pp);
2348 if (!integer_onep (TYPE_SIZE_UNIT
2349 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2351 pp_cxx_left_paren (pp);
2352 dump_type (pp, ptr_type_node, flags);
2353 pp_cxx_right_paren (pp);
2356 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2357 if (!integer_zerop (TREE_OPERAND (t, 1)))
2359 pp_cxx_ws_string (pp, "+");
2360 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2361 flags);
2362 pp_cxx_right_paren (pp);
2365 break;
2367 case NEGATE_EXPR:
2368 case BIT_NOT_EXPR:
2369 case TRUTH_NOT_EXPR:
2370 case PREDECREMENT_EXPR:
2371 case PREINCREMENT_EXPR:
2372 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2373 break;
2375 case POSTDECREMENT_EXPR:
2376 case POSTINCREMENT_EXPR:
2377 pp_cxx_left_paren (pp);
2378 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2379 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2380 pp_cxx_right_paren (pp);
2381 break;
2383 case NON_LVALUE_EXPR:
2384 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2385 should be another level of INDIRECT_REF so that I don't have to do
2386 this. */
2387 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2389 tree next = TREE_TYPE (TREE_TYPE (t));
2391 while (TYPE_PTR_P (next))
2392 next = TREE_TYPE (next);
2394 if (TREE_CODE (next) == FUNCTION_TYPE)
2396 if (flags & TFF_EXPR_IN_PARENS)
2397 pp_cxx_left_paren (pp);
2398 pp_cxx_star (pp);
2399 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2400 if (flags & TFF_EXPR_IN_PARENS)
2401 pp_cxx_right_paren (pp);
2402 break;
2404 /* Else fall through. */
2406 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2407 break;
2409 CASE_CONVERT:
2410 case IMPLICIT_CONV_EXPR:
2411 case VIEW_CONVERT_EXPR:
2413 tree op = TREE_OPERAND (t, 0);
2414 tree ttype = TREE_TYPE (t);
2415 tree optype = TREE_TYPE (op);
2417 if (TREE_CODE (ttype) != TREE_CODE (optype)
2418 && INDIRECT_TYPE_P (ttype)
2419 && INDIRECT_TYPE_P (optype)
2420 && same_type_p (TREE_TYPE (optype),
2421 TREE_TYPE (ttype)))
2423 if (TYPE_REF_P (ttype))
2425 STRIP_NOPS (op);
2426 if (TREE_CODE (op) == ADDR_EXPR)
2427 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2428 else
2429 dump_unary_op (pp, "*", t, flags);
2431 else
2432 dump_unary_op (pp, "&", t, flags);
2434 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2436 /* It is a cast, but we cannot tell whether it is a
2437 reinterpret or static cast. Use the C style notation. */
2438 if (flags & TFF_EXPR_IN_PARENS)
2439 pp_cxx_left_paren (pp);
2440 pp_cxx_left_paren (pp);
2441 dump_type (pp, TREE_TYPE (t), flags);
2442 pp_cxx_right_paren (pp);
2443 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2444 if (flags & TFF_EXPR_IN_PARENS)
2445 pp_cxx_right_paren (pp);
2447 else
2448 dump_expr (pp, op, flags);
2449 break;
2452 case CONSTRUCTOR:
2453 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2455 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2457 if (integer_zerop (idx))
2459 /* A NULL pointer-to-member constant. */
2460 pp_cxx_left_paren (pp);
2461 pp_cxx_left_paren (pp);
2462 dump_type (pp, TREE_TYPE (t), flags);
2463 pp_cxx_right_paren (pp);
2464 pp_character (pp, '0');
2465 pp_cxx_right_paren (pp);
2466 break;
2468 else if (tree_fits_shwi_p (idx))
2470 tree virtuals;
2471 unsigned HOST_WIDE_INT n;
2473 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2474 t = TYPE_METHOD_BASETYPE (t);
2475 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2477 n = tree_to_shwi (idx);
2479 /* Map vtable index back one, to allow for the null pointer to
2480 member. */
2481 --n;
2483 while (n > 0 && virtuals)
2485 --n;
2486 virtuals = TREE_CHAIN (virtuals);
2488 if (virtuals)
2490 dump_expr (pp, BV_FN (virtuals),
2491 flags | TFF_EXPR_IN_PARENS);
2492 break;
2496 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2497 pp_string (pp, "<lambda closure object>");
2498 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2500 dump_type (pp, TREE_TYPE (t), 0);
2501 pp_cxx_left_paren (pp);
2502 pp_cxx_right_paren (pp);
2504 else
2506 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2507 dump_type (pp, TREE_TYPE (t), 0);
2508 pp_cxx_left_brace (pp);
2509 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2510 pp_cxx_right_brace (pp);
2513 break;
2515 case OFFSET_REF:
2517 tree ob = TREE_OPERAND (t, 0);
2518 if (is_dummy_object (ob))
2520 t = TREE_OPERAND (t, 1);
2521 if (TREE_CODE (t) == FUNCTION_DECL)
2522 /* A::f */
2523 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2524 else if (BASELINK_P (t))
2525 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2526 flags | TFF_EXPR_IN_PARENS);
2527 else
2528 dump_decl (pp, t, flags);
2530 else
2532 if (INDIRECT_REF_P (ob))
2534 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2535 pp_cxx_arrow (pp);
2536 pp_cxx_star (pp);
2538 else
2540 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2541 pp_cxx_dot (pp);
2542 pp_cxx_star (pp);
2544 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2546 break;
2549 case TEMPLATE_PARM_INDEX:
2550 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2551 break;
2553 case CAST_EXPR:
2554 if (TREE_OPERAND (t, 0) == NULL_TREE
2555 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2557 dump_type (pp, TREE_TYPE (t), flags);
2558 pp_cxx_left_paren (pp);
2559 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2560 pp_cxx_right_paren (pp);
2562 else
2564 pp_cxx_left_paren (pp);
2565 dump_type (pp, TREE_TYPE (t), flags);
2566 pp_cxx_right_paren (pp);
2567 pp_cxx_left_paren (pp);
2568 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2569 pp_cxx_right_paren (pp);
2571 break;
2573 case STATIC_CAST_EXPR:
2574 pp_cxx_ws_string (pp, "static_cast");
2575 goto cast;
2576 case REINTERPRET_CAST_EXPR:
2577 pp_cxx_ws_string (pp, "reinterpret_cast");
2578 goto cast;
2579 case CONST_CAST_EXPR:
2580 pp_cxx_ws_string (pp, "const_cast");
2581 goto cast;
2582 case DYNAMIC_CAST_EXPR:
2583 pp_cxx_ws_string (pp, "dynamic_cast");
2584 cast:
2585 pp_cxx_begin_template_argument_list (pp);
2586 dump_type (pp, TREE_TYPE (t), flags);
2587 pp_cxx_end_template_argument_list (pp);
2588 pp_cxx_left_paren (pp);
2589 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2590 pp_cxx_right_paren (pp);
2591 break;
2593 case ARROW_EXPR:
2594 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2595 pp_cxx_arrow (pp);
2596 break;
2598 case SIZEOF_EXPR:
2599 case ALIGNOF_EXPR:
2600 if (TREE_CODE (t) == SIZEOF_EXPR)
2601 pp_cxx_ws_string (pp, "sizeof");
2602 else
2604 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2605 pp_cxx_ws_string (pp, "__alignof__");
2607 op = TREE_OPERAND (t, 0);
2608 if (PACK_EXPANSION_P (op))
2610 pp_string (pp, "...");
2611 op = PACK_EXPANSION_PATTERN (op);
2613 pp_cxx_whitespace (pp);
2614 pp_cxx_left_paren (pp);
2615 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2616 dump_type (pp, TREE_TYPE (op), flags);
2617 else if (TYPE_P (TREE_OPERAND (t, 0)))
2618 dump_type (pp, op, flags);
2619 else
2620 dump_expr (pp, op, flags);
2621 pp_cxx_right_paren (pp);
2622 break;
2624 case AT_ENCODE_EXPR:
2625 pp_cxx_ws_string (pp, "@encode");
2626 pp_cxx_whitespace (pp);
2627 pp_cxx_left_paren (pp);
2628 dump_type (pp, TREE_OPERAND (t, 0), flags);
2629 pp_cxx_right_paren (pp);
2630 break;
2632 case NOEXCEPT_EXPR:
2633 pp_cxx_ws_string (pp, "noexcept");
2634 pp_cxx_whitespace (pp);
2635 pp_cxx_left_paren (pp);
2636 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2637 pp_cxx_right_paren (pp);
2638 break;
2640 case REALPART_EXPR:
2641 case IMAGPART_EXPR:
2642 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2643 pp_cxx_whitespace (pp);
2644 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2645 break;
2647 case DEFAULT_ARG:
2648 pp_string (pp, M_("<unparsed>"));
2649 break;
2651 case TRY_CATCH_EXPR:
2652 case CLEANUP_POINT_EXPR:
2653 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2654 break;
2656 case PSEUDO_DTOR_EXPR:
2657 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2658 pp_cxx_dot (pp);
2659 if (TREE_OPERAND (t, 1))
2661 dump_type (pp, TREE_OPERAND (t, 1), flags);
2662 pp_cxx_colon_colon (pp);
2664 pp_cxx_complement (pp);
2665 dump_type (pp, TREE_OPERAND (t, 2), flags);
2666 break;
2668 case TEMPLATE_ID_EXPR:
2669 dump_decl (pp, t, flags);
2670 break;
2672 case BIND_EXPR:
2673 case STMT_EXPR:
2674 case EXPR_STMT:
2675 case STATEMENT_LIST:
2676 /* We don't yet have a way of dumping statements in a
2677 human-readable format. */
2678 pp_string (pp, "({...})");
2679 break;
2681 case LOOP_EXPR:
2682 pp_string (pp, "while (1) { ");
2683 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2684 pp_cxx_right_brace (pp);
2685 break;
2687 case EXIT_EXPR:
2688 pp_string (pp, "if (");
2689 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2690 pp_string (pp, ") break; ");
2691 break;
2693 case BASELINK:
2694 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2695 break;
2697 case EMPTY_CLASS_EXPR:
2698 dump_type (pp, TREE_TYPE (t), flags);
2699 pp_cxx_left_paren (pp);
2700 pp_cxx_right_paren (pp);
2701 break;
2703 case NON_DEPENDENT_EXPR:
2704 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2705 break;
2707 case ARGUMENT_PACK_SELECT:
2708 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2709 break;
2711 case RECORD_TYPE:
2712 case UNION_TYPE:
2713 case ENUMERAL_TYPE:
2714 case REAL_TYPE:
2715 case VOID_TYPE:
2716 case BOOLEAN_TYPE:
2717 case INTEGER_TYPE:
2718 case COMPLEX_TYPE:
2719 case VECTOR_TYPE:
2720 case DECLTYPE_TYPE:
2721 pp_type_specifier_seq (pp, t);
2722 break;
2724 case TYPENAME_TYPE:
2725 /* We get here when we want to print a dependent type as an
2726 id-expression, without any disambiguator decoration. */
2727 pp->id_expression (t);
2728 break;
2730 case TEMPLATE_TYPE_PARM:
2731 case TEMPLATE_TEMPLATE_PARM:
2732 case BOUND_TEMPLATE_TEMPLATE_PARM:
2733 dump_type (pp, t, flags);
2734 break;
2736 case TRAIT_EXPR:
2737 pp_cxx_trait_expression (pp, t);
2738 break;
2740 case VA_ARG_EXPR:
2741 pp_cxx_va_arg_expression (pp, t);
2742 break;
2744 case OFFSETOF_EXPR:
2745 pp_cxx_offsetof_expression (pp, t);
2746 break;
2748 case ADDRESSOF_EXPR:
2749 pp_cxx_addressof_expression (pp, t);
2750 break;
2752 case SCOPE_REF:
2753 dump_decl (pp, t, flags);
2754 break;
2756 case EXPR_PACK_EXPANSION:
2757 case UNARY_LEFT_FOLD_EXPR:
2758 case UNARY_RIGHT_FOLD_EXPR:
2759 case BINARY_LEFT_FOLD_EXPR:
2760 case BINARY_RIGHT_FOLD_EXPR:
2761 case TYPEID_EXPR:
2762 case MEMBER_REF:
2763 case DOTSTAR_EXPR:
2764 case NEW_EXPR:
2765 case VEC_NEW_EXPR:
2766 case DELETE_EXPR:
2767 case VEC_DELETE_EXPR:
2768 case MODOP_EXPR:
2769 case ABS_EXPR:
2770 case ABSU_EXPR:
2771 case CONJ_EXPR:
2772 case VECTOR_CST:
2773 case FIXED_CST:
2774 case UNORDERED_EXPR:
2775 case ORDERED_EXPR:
2776 case UNLT_EXPR:
2777 case UNLE_EXPR:
2778 case UNGT_EXPR:
2779 case UNGE_EXPR:
2780 case UNEQ_EXPR:
2781 case LTGT_EXPR:
2782 case COMPLEX_EXPR:
2783 case BIT_FIELD_REF:
2784 case FIX_TRUNC_EXPR:
2785 case FLOAT_EXPR:
2786 pp->expression (t);
2787 break;
2789 case TRUTH_AND_EXPR:
2790 case TRUTH_OR_EXPR:
2791 case TRUTH_XOR_EXPR:
2792 if (flags & TFF_EXPR_IN_PARENS)
2793 pp_cxx_left_paren (pp);
2794 pp->expression (t);
2795 if (flags & TFF_EXPR_IN_PARENS)
2796 pp_cxx_right_paren (pp);
2797 break;
2799 case OBJ_TYPE_REF:
2800 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2801 break;
2803 case LAMBDA_EXPR:
2804 pp_string (pp, M_("<lambda>"));
2805 break;
2807 case PAREN_EXPR:
2808 pp_cxx_left_paren (pp);
2809 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2810 pp_cxx_right_paren (pp);
2811 break;
2813 case REQUIRES_EXPR:
2814 pp_cxx_requires_expr (cxx_pp, t);
2815 break;
2817 case SIMPLE_REQ:
2818 pp_cxx_simple_requirement (cxx_pp, t);
2819 break;
2821 case TYPE_REQ:
2822 pp_cxx_type_requirement (cxx_pp, t);
2823 break;
2825 case COMPOUND_REQ:
2826 pp_cxx_compound_requirement (cxx_pp, t);
2827 break;
2829 case NESTED_REQ:
2830 pp_cxx_nested_requirement (cxx_pp, t);
2831 break;
2833 case PRED_CONSTR:
2834 case CHECK_CONSTR:
2835 case EXPR_CONSTR:
2836 case TYPE_CONSTR:
2837 case ICONV_CONSTR:
2838 case DEDUCT_CONSTR:
2839 case EXCEPT_CONSTR:
2840 case PARM_CONSTR:
2841 case CONJ_CONSTR:
2842 case DISJ_CONSTR:
2843 pp_cxx_constraint (cxx_pp, t);
2844 break;
2846 case PLACEHOLDER_EXPR:
2847 pp_string (pp, M_("*this"));
2848 break;
2850 case TREE_LIST:
2851 dump_expr_list (pp, t, flags);
2852 break;
2854 /* This list is incomplete, but should suffice for now.
2855 It is very important that `sorry' does not call
2856 `report_error_function'. That could cause an infinite loop. */
2857 default:
2858 pp_unsupported_tree (pp, t);
2859 /* Fall through. */
2860 case ERROR_MARK:
2861 pp_string (pp, M_("<expression error>"));
2862 break;
2866 static void
2867 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2868 int flags)
2870 pp_cxx_left_paren (pp);
2871 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2872 pp_cxx_whitespace (pp);
2873 if (opstring)
2874 pp_cxx_ws_string (pp, opstring);
2875 else
2876 pp_string (pp, M_("<unknown operator>"));
2877 pp_cxx_whitespace (pp);
2878 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2879 pp_cxx_right_paren (pp);
2882 static void
2883 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2885 if (flags & TFF_EXPR_IN_PARENS)
2886 pp_cxx_left_paren (pp);
2887 pp_cxx_ws_string (pp, opstring);
2888 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2889 if (flags & TFF_EXPR_IN_PARENS)
2890 pp_cxx_right_paren (pp);
2893 static void
2894 reinit_cxx_pp (void)
2896 pp_clear_output_area (cxx_pp);
2897 cxx_pp->padding = pp_none;
2898 pp_indentation (cxx_pp) = 0;
2899 pp_needs_newline (cxx_pp) = false;
2900 cxx_pp->enclosing_scope = current_function_decl;
2903 /* Same as pp_formatted_text, except the return string is a separate
2904 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2906 inline const char *
2907 pp_ggc_formatted_text (pretty_printer *pp)
2909 return ggc_strdup (pp_formatted_text (pp));
2912 /* Exported interface to stringifying types, exprs and decls under TFF_*
2913 control. */
2915 const char *
2916 type_as_string (tree typ, int flags)
2918 reinit_cxx_pp ();
2919 pp_translate_identifiers (cxx_pp) = false;
2920 dump_type (cxx_pp, typ, flags);
2921 return pp_ggc_formatted_text (cxx_pp);
2924 const char *
2925 type_as_string_translate (tree typ, int flags)
2927 reinit_cxx_pp ();
2928 dump_type (cxx_pp, typ, flags);
2929 return pp_ggc_formatted_text (cxx_pp);
2932 const char *
2933 expr_as_string (tree decl, int flags)
2935 reinit_cxx_pp ();
2936 pp_translate_identifiers (cxx_pp) = false;
2937 dump_expr (cxx_pp, decl, flags);
2938 return pp_ggc_formatted_text (cxx_pp);
2941 /* Wrap decl_as_string with options appropriate for dwarf. */
2943 const char *
2944 decl_as_dwarf_string (tree decl, int flags)
2946 const char *name;
2947 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2948 here will be adequate to get the desired behavior. */
2949 cxx_pp->flags |= pp_c_flag_gnu_v3;
2950 name = decl_as_string (decl, flags);
2951 /* Subsequent calls to the pretty printer shouldn't use this style. */
2952 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2953 return name;
2956 const char *
2957 decl_as_string (tree decl, int flags)
2959 reinit_cxx_pp ();
2960 pp_translate_identifiers (cxx_pp) = false;
2961 dump_decl (cxx_pp, decl, flags);
2962 return pp_ggc_formatted_text (cxx_pp);
2965 const char *
2966 decl_as_string_translate (tree decl, int flags)
2968 reinit_cxx_pp ();
2969 dump_decl (cxx_pp, decl, flags);
2970 return pp_ggc_formatted_text (cxx_pp);
2973 /* Wrap lang_decl_name with options appropriate for dwarf. */
2975 const char *
2976 lang_decl_dwarf_name (tree decl, int v, bool translate)
2978 const char *name;
2979 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2980 here will be adequate to get the desired behavior. */
2981 cxx_pp->flags |= pp_c_flag_gnu_v3;
2982 name = lang_decl_name (decl, v, translate);
2983 /* Subsequent calls to the pretty printer shouldn't use this style. */
2984 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2985 return name;
2988 /* Generate the three forms of printable names for cxx_printable_name. */
2990 const char *
2991 lang_decl_name (tree decl, int v, bool translate)
2993 if (v >= 2)
2994 return (translate
2995 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2996 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2998 reinit_cxx_pp ();
2999 pp_translate_identifiers (cxx_pp) = translate;
3000 if (v == 1
3001 && (DECL_CLASS_SCOPE_P (decl)
3002 || (DECL_NAMESPACE_SCOPE_P (decl)
3003 && CP_DECL_CONTEXT (decl) != global_namespace)))
3005 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3006 pp_cxx_colon_colon (cxx_pp);
3009 if (TREE_CODE (decl) == FUNCTION_DECL)
3010 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3011 else if ((DECL_NAME (decl) == NULL_TREE)
3012 && TREE_CODE (decl) == NAMESPACE_DECL)
3013 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3014 else
3015 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3017 return pp_ggc_formatted_text (cxx_pp);
3020 /* Return the location of a tree passed to %+ formats. */
3022 location_t
3023 location_of (tree t)
3025 if (TYPE_P (t))
3027 t = TYPE_MAIN_DECL (t);
3028 if (t == NULL_TREE)
3029 return input_location;
3031 else if (TREE_CODE (t) == OVERLOAD)
3032 t = OVL_FIRST (t);
3034 if (DECL_P (t))
3035 return DECL_SOURCE_LOCATION (t);
3036 if (TREE_CODE (t) == DEFAULT_ARG)
3037 return defarg_location (t);
3038 return cp_expr_loc_or_loc (t, input_location);
3041 /* Now the interfaces from error et al to dump_type et al. Each takes an
3042 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3043 function. */
3045 static const char *
3046 decl_to_string (tree decl, int verbose)
3048 int flags = 0;
3050 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3051 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3052 flags = TFF_CLASS_KEY_OR_ENUM;
3053 if (verbose)
3054 flags |= TFF_DECL_SPECIFIERS;
3055 else if (TREE_CODE (decl) == FUNCTION_DECL)
3056 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3057 flags |= TFF_TEMPLATE_HEADER;
3059 reinit_cxx_pp ();
3060 dump_decl (cxx_pp, decl, flags);
3061 return pp_ggc_formatted_text (cxx_pp);
3064 const char *
3065 expr_to_string (tree decl)
3067 reinit_cxx_pp ();
3068 dump_expr (cxx_pp, decl, 0);
3069 return pp_ggc_formatted_text (cxx_pp);
3072 static const char *
3073 fndecl_to_string (tree fndecl, int verbose)
3075 int flags;
3077 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3078 | TFF_TEMPLATE_HEADER;
3079 if (verbose)
3080 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3081 reinit_cxx_pp ();
3082 dump_decl (cxx_pp, fndecl, flags);
3083 return pp_ggc_formatted_text (cxx_pp);
3087 static const char *
3088 code_to_string (enum tree_code c)
3090 return get_tree_code_name (c);
3093 const char *
3094 language_to_string (enum languages c)
3096 switch (c)
3098 case lang_c:
3099 return "C";
3101 case lang_cplusplus:
3102 return "C++";
3104 default:
3105 gcc_unreachable ();
3107 return NULL;
3110 /* Return the proper printed version of a parameter to a C++ function. */
3112 static const char *
3113 parm_to_string (int p)
3115 reinit_cxx_pp ();
3116 if (p < 0)
3117 pp_string (cxx_pp, "'this'");
3118 else
3119 pp_decimal_int (cxx_pp, p + 1);
3120 return pp_ggc_formatted_text (cxx_pp);
3123 static const char *
3124 op_to_string (bool assop, enum tree_code p)
3126 tree id = ovl_op_identifier (assop, p);
3127 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3130 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3132 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3133 string in appropriate places, and *QUOTE is written to with false
3134 to suppress pp_format's trailing close quote so that e.g.
3135 foo_typedef {aka underlying_foo} {enum}
3136 can be printed by "%qT" as:
3137 `foo_typedef' {aka `underlying_foo'} {enum}
3138 rather than:
3139 `foo_typedef {aka underlying_foo} {enum}'
3140 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3141 then a leading open quote will be added, whereas if POSTPROCESSED is false
3142 (for handling %T) then any leading quote has already been added by
3143 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3144 within %H and %I).
3146 SHOW_COLOR is used to determine the colorization of any quotes that
3147 are added. */
3149 static const char *
3150 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3151 bool show_color)
3153 int flags = 0;
3154 if (verbose)
3155 flags |= TFF_CLASS_KEY_OR_ENUM;
3156 flags |= TFF_TEMPLATE_HEADER;
3158 reinit_cxx_pp ();
3160 if (postprocessed && quote && *quote)
3161 pp_begin_quote (cxx_pp, show_color);
3163 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3164 int type_start, type_len;
3165 type_start = obstack_object_size (ob);
3167 dump_type (cxx_pp, typ, flags);
3169 /* Remember the end of the initial dump. */
3170 type_len = obstack_object_size (ob) - type_start;
3172 /* If we're printing a type that involves typedefs, also print the
3173 stripped version. But sometimes the stripped version looks
3174 exactly the same, so we don't want it after all. To avoid printing
3175 it in that case, we play ugly obstack games. */
3176 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3177 && !uses_template_parms (typ))
3179 int aka_start, aka_len; char *p;
3180 tree aka = strip_typedefs (typ);
3181 if (quote && *quote)
3182 pp_end_quote (cxx_pp, show_color);
3183 pp_string (cxx_pp, " {aka");
3184 pp_cxx_whitespace (cxx_pp);
3185 if (quote && *quote)
3186 pp_begin_quote (cxx_pp, show_color);
3187 /* And remember the start of the aka dump. */
3188 aka_start = obstack_object_size (ob);
3189 dump_type (cxx_pp, aka, flags);
3190 aka_len = obstack_object_size (ob) - aka_start;
3191 if (quote && *quote)
3192 pp_end_quote (cxx_pp, show_color);
3193 pp_right_brace (cxx_pp);
3194 p = (char*)obstack_base (ob);
3195 /* If they are identical, cut off the aka by unwinding the obstack. */
3196 if (type_len == aka_len
3197 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3199 /* We can't add a '\0' here, since we may be adding a closing quote
3200 below, and it would be hidden by the '\0'.
3201 Instead, manually unwind the current object within the obstack
3202 so that the insertion point is at the end of the type, before
3203 the "' {aka". */
3204 int delta = type_start + type_len - obstack_object_size (ob);
3205 gcc_assert (delta <= 0);
3206 obstack_blank_fast (ob, delta);
3208 else
3209 if (quote)
3210 /* No further closing quotes are needed. */
3211 *quote = false;
3214 if (quote && *quote)
3216 pp_end_quote (cxx_pp, show_color);
3217 *quote = false;
3219 return pp_ggc_formatted_text (cxx_pp);
3222 static const char *
3223 args_to_string (tree p, int verbose)
3225 int flags = 0;
3226 if (verbose)
3227 flags |= TFF_CLASS_KEY_OR_ENUM;
3229 if (p == NULL_TREE)
3230 return "";
3232 if (TYPE_P (TREE_VALUE (p)))
3233 return type_as_string_translate (p, flags);
3235 reinit_cxx_pp ();
3236 for (; p; p = TREE_CHAIN (p))
3238 if (null_node_p (TREE_VALUE (p)))
3239 pp_cxx_ws_string (cxx_pp, "NULL");
3240 else
3241 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3242 if (TREE_CHAIN (p))
3243 pp_separate_with_comma (cxx_pp);
3245 return pp_ggc_formatted_text (cxx_pp);
3248 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3249 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3250 arguments. */
3252 static const char *
3253 subst_to_string (tree p)
3255 tree decl = TREE_PURPOSE (p);
3256 tree targs = TREE_VALUE (p);
3257 tree tparms = DECL_TEMPLATE_PARMS (decl);
3258 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3259 |TFF_NO_TEMPLATE_BINDINGS);
3261 if (p == NULL_TREE)
3262 return "";
3264 reinit_cxx_pp ();
3265 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3266 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3267 return pp_ggc_formatted_text (cxx_pp);
3270 static const char *
3271 cv_to_string (tree p, int v)
3273 reinit_cxx_pp ();
3274 cxx_pp->padding = v ? pp_before : pp_none;
3275 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3276 return pp_ggc_formatted_text (cxx_pp);
3279 static const char *
3280 eh_spec_to_string (tree p, int /*v*/)
3282 int flags = 0;
3283 reinit_cxx_pp ();
3284 dump_exception_spec (cxx_pp, p, flags);
3285 return pp_ggc_formatted_text (cxx_pp);
3288 /* Langhook for print_error_function. */
3289 void
3290 cxx_print_error_function (diagnostic_context *context, const char *file,
3291 diagnostic_info *diagnostic)
3293 char *prefix;
3294 if (file)
3295 prefix = xstrdup (file);
3296 else
3297 prefix = NULL;
3298 lhd_print_error_function (context, file, diagnostic);
3299 pp_set_prefix (context->printer, prefix);
3300 maybe_print_instantiation_context (context);
3303 static void
3304 cp_diagnostic_starter (diagnostic_context *context,
3305 diagnostic_info *diagnostic)
3307 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3308 cp_print_error_function (context, diagnostic);
3309 maybe_print_instantiation_context (context);
3310 maybe_print_constexpr_context (context);
3311 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3312 diagnostic));
3315 /* Print current function onto BUFFER, in the process of reporting
3316 a diagnostic message. Called from cp_diagnostic_starter. */
3317 static void
3318 cp_print_error_function (diagnostic_context *context,
3319 diagnostic_info *diagnostic)
3321 /* If we are in an instantiation context, current_function_decl is likely
3322 to be wrong, so just rely on print_instantiation_full_context. */
3323 if (current_instantiation ())
3324 return;
3325 if (diagnostic_last_function_changed (context, diagnostic))
3327 char *old_prefix = pp_take_prefix (context->printer);
3328 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3329 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3330 char *new_prefix = (file && abstract_origin == NULL)
3331 ? file_name_as_prefix (context, file) : NULL;
3333 pp_set_prefix (context->printer, new_prefix);
3335 if (current_function_decl == NULL)
3336 pp_string (context->printer, _("At global scope:"));
3337 else
3339 tree fndecl, ao;
3341 if (abstract_origin)
3343 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3344 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3345 fndecl = ao;
3347 else
3348 fndecl = current_function_decl;
3350 pp_printf (context->printer, function_category (fndecl),
3351 cxx_printable_name_translate (fndecl, 2));
3353 while (abstract_origin)
3355 location_t *locus;
3356 tree block = abstract_origin;
3358 locus = &BLOCK_SOURCE_LOCATION (block);
3359 fndecl = NULL;
3360 block = BLOCK_SUPERCONTEXT (block);
3361 while (block && TREE_CODE (block) == BLOCK
3362 && BLOCK_ABSTRACT_ORIGIN (block))
3364 ao = BLOCK_ABSTRACT_ORIGIN (block);
3365 if (TREE_CODE (ao) == FUNCTION_DECL)
3367 fndecl = ao;
3368 break;
3370 else if (TREE_CODE (ao) != BLOCK)
3371 break;
3373 block = BLOCK_SUPERCONTEXT (block);
3375 if (fndecl)
3376 abstract_origin = block;
3377 else
3379 while (block && TREE_CODE (block) == BLOCK)
3380 block = BLOCK_SUPERCONTEXT (block);
3382 if (block && TREE_CODE (block) == FUNCTION_DECL)
3383 fndecl = block;
3384 abstract_origin = NULL;
3386 if (fndecl)
3388 expanded_location s = expand_location (*locus);
3389 pp_character (context->printer, ',');
3390 pp_newline (context->printer);
3391 if (s.file != NULL)
3393 if (context->show_column && s.column != 0)
3394 pp_printf (context->printer,
3395 _(" inlined from %qs at %r%s:%d:%d%R"),
3396 cxx_printable_name_translate (fndecl, 2),
3397 "locus", s.file, s.line, s.column);
3398 else
3399 pp_printf (context->printer,
3400 _(" inlined from %qs at %r%s:%d%R"),
3401 cxx_printable_name_translate (fndecl, 2),
3402 "locus", s.file, s.line);
3405 else
3406 pp_printf (context->printer, _(" inlined from %qs"),
3407 cxx_printable_name_translate (fndecl, 2));
3410 pp_character (context->printer, ':');
3412 pp_newline (context->printer);
3414 diagnostic_set_last_function (context, diagnostic);
3415 pp_destroy_prefix (context->printer);
3416 context->printer->prefix = old_prefix;
3420 /* Returns a description of FUNCTION using standard terminology. The
3421 result is a format string of the form "In CATEGORY %qs". */
3422 static const char *
3423 function_category (tree fn)
3425 /* We can get called from the middle-end for diagnostics of function
3426 clones. Make sure we have language specific information before
3427 dereferencing it. */
3428 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3429 && DECL_FUNCTION_MEMBER_P (fn))
3431 if (DECL_STATIC_FUNCTION_P (fn))
3432 return _("In static member function %qs");
3433 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3434 return _("In copy constructor %qs");
3435 else if (DECL_CONSTRUCTOR_P (fn))
3436 return _("In constructor %qs");
3437 else if (DECL_DESTRUCTOR_P (fn))
3438 return _("In destructor %qs");
3439 else if (LAMBDA_FUNCTION_P (fn))
3440 return _("In lambda function");
3441 else
3442 return _("In member function %qs");
3444 else
3445 return _("In function %qs");
3448 /* Report the full context of a current template instantiation,
3449 onto BUFFER. */
3450 static void
3451 print_instantiation_full_context (diagnostic_context *context)
3453 struct tinst_level *p = current_instantiation ();
3454 location_t location = input_location;
3456 if (p)
3458 pp_verbatim (context->printer,
3459 p->list_p ()
3460 ? _("%s: In substitution of %qS:\n")
3461 : _("%s: In instantiation of %q#D:\n"),
3462 LOCATION_FILE (location),
3463 p->get_node ());
3465 location = p->locus;
3466 p = p->next;
3469 print_instantiation_partial_context (context, p, location);
3472 /* Helper function of print_instantiation_partial_context() that
3473 prints a single line of instantiation context. */
3475 static void
3476 print_instantiation_partial_context_line (diagnostic_context *context,
3477 struct tinst_level *t,
3478 location_t loc, bool recursive_p)
3480 if (loc == UNKNOWN_LOCATION)
3481 return;
3483 expanded_location xloc = expand_location (loc);
3485 if (context->show_column)
3486 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3487 "locus", xloc.file, xloc.line, xloc.column);
3488 else
3489 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3490 "locus", xloc.file, xloc.line);
3492 if (t != NULL)
3494 if (t->list_p ())
3495 pp_verbatim (context->printer,
3496 recursive_p
3497 ? _("recursively required by substitution of %qS\n")
3498 : _("required by substitution of %qS\n"),
3499 t->get_node ());
3500 else
3501 pp_verbatim (context->printer,
3502 recursive_p
3503 ? _("recursively required from %q#D\n")
3504 : _("required from %q#D\n"),
3505 t->get_node ());
3507 else
3509 pp_verbatim (context->printer,
3510 recursive_p
3511 ? _("recursively required from here\n")
3512 : _("required from here\n"));
3516 /* Same as print_instantiation_full_context but less verbose. */
3518 static void
3519 print_instantiation_partial_context (diagnostic_context *context,
3520 struct tinst_level *t0, location_t loc)
3522 struct tinst_level *t;
3523 int n_total = 0;
3524 int n;
3525 location_t prev_loc = loc;
3527 for (t = t0; t != NULL; t = t->next)
3528 if (prev_loc != t->locus)
3530 prev_loc = t->locus;
3531 n_total++;
3534 t = t0;
3536 if (template_backtrace_limit
3537 && n_total > template_backtrace_limit)
3539 int skip = n_total - template_backtrace_limit;
3540 int head = template_backtrace_limit / 2;
3542 /* Avoid skipping just 1. If so, skip 2. */
3543 if (skip == 1)
3545 skip = 2;
3546 head = (template_backtrace_limit - 1) / 2;
3549 for (n = 0; n < head; n++)
3551 gcc_assert (t != NULL);
3552 if (loc != t->locus)
3553 print_instantiation_partial_context_line (context, t, loc,
3554 /*recursive_p=*/false);
3555 loc = t->locus;
3556 t = t->next;
3558 if (t != NULL && skip > 0)
3560 expanded_location xloc;
3561 xloc = expand_location (loc);
3562 if (context->show_column)
3563 pp_verbatim (context->printer,
3564 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3565 "contexts, use -ftemplate-backtrace-limit=0 to "
3566 "disable ]\n"),
3567 "locus", xloc.file, xloc.line, xloc.column, skip);
3568 else
3569 pp_verbatim (context->printer,
3570 _("%r%s:%d:%R [ skipping %d instantiation "
3571 "contexts, use -ftemplate-backtrace-limit=0 to "
3572 "disable ]\n"),
3573 "locus", xloc.file, xloc.line, skip);
3575 do {
3576 loc = t->locus;
3577 t = t->next;
3578 } while (t != NULL && --skip > 0);
3582 while (t != NULL)
3584 while (t->next != NULL && t->locus == t->next->locus)
3586 loc = t->locus;
3587 t = t->next;
3589 print_instantiation_partial_context_line (context, t, loc,
3590 t->locus == loc);
3591 loc = t->locus;
3592 t = t->next;
3594 print_instantiation_partial_context_line (context, NULL, loc,
3595 /*recursive_p=*/false);
3598 /* Called from cp_thing to print the template context for an error. */
3599 static void
3600 maybe_print_instantiation_context (diagnostic_context *context)
3602 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3603 return;
3605 record_last_problematic_instantiation ();
3606 print_instantiation_full_context (context);
3609 /* Report what constexpr call(s) we're trying to expand, if any. */
3611 void
3612 maybe_print_constexpr_context (diagnostic_context *context)
3614 vec<tree> call_stack = cx_error_context ();
3615 unsigned ix;
3616 tree t;
3618 FOR_EACH_VEC_ELT (call_stack, ix, t)
3620 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3621 const char *s = expr_as_string (t, 0);
3622 if (context->show_column)
3623 pp_verbatim (context->printer,
3624 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3625 "locus", xloc.file, xloc.line, xloc.column, s);
3626 else
3627 pp_verbatim (context->printer,
3628 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3629 "locus", xloc.file, xloc.line, s);
3630 pp_newline (context->printer);
3635 /* Return true iff TYPE_A and TYPE_B are template types that are
3636 meaningful to compare. */
3638 static bool
3639 comparable_template_types_p (tree type_a, tree type_b)
3641 if (!CLASS_TYPE_P (type_a))
3642 return false;
3643 if (!CLASS_TYPE_P (type_b))
3644 return false;
3646 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3647 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3648 if (!tinfo_a || !tinfo_b)
3649 return false;
3651 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3654 /* Start a new line indented by SPC spaces on PP. */
3656 static void
3657 newline_and_indent (pretty_printer *pp, int spc)
3659 pp_newline (pp);
3660 for (int i = 0; i < spc; i++)
3661 pp_space (pp);
3664 /* Generate a GC-allocated string for ARG, an expression or type. */
3666 static const char *
3667 arg_to_string (tree arg, bool verbose)
3669 if (TYPE_P (arg))
3670 return type_to_string (arg, verbose, true, NULL, false);
3671 else
3672 return expr_to_string (arg);
3675 /* Subroutine to type_to_string_with_compare and
3676 print_template_tree_comparison.
3678 Print a representation of ARG (an expression or type) to PP,
3679 colorizing it as "type-diff" if PP->show_color. */
3681 static void
3682 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3684 pp_printf (pp, "%r%s%R",
3685 "type-diff",
3686 (arg
3687 ? arg_to_string (arg, verbose)
3688 : G_("(no argument)")));
3691 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3693 The types must satisfy comparable_template_types_p.
3695 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3696 potentially colorizing/eliding in comparison with TYPE_B.
3698 For example given types:
3699 vector<map<int,double>>
3701 vector<map<int,float>>
3702 then the result on PP would be:
3703 vector<map<[...],double>>
3704 with type elision, and:
3705 vector<map<int,double>>
3706 without type elision.
3708 In both cases the parts of TYPE that differ from PEER will be colorized
3709 if pp_show_color (pp) is true. In the above example, this would be
3710 "double".
3712 If INDENT is non-zero, then the types are printed in a tree-like form
3713 which shows both types. In the above example, the result on PP would be:
3715 vector<
3716 map<
3717 [...],
3718 [double != float]>>
3720 and without type-elision would be:
3722 vector<
3723 map<
3724 int,
3725 [double != float]>>
3727 As before, the differing parts of the types are colorized if
3728 pp_show_color (pp) is true ("double" and "float" in this example).
3730 Template arguments in which both types are using the default arguments
3731 are not printed; if at least one of the two types is using a non-default
3732 argument, then that argument is printed (or both arguments for the
3733 tree-like print format). */
3735 static void
3736 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
3737 bool verbose, int indent)
3739 if (indent)
3740 newline_and_indent (pp, indent);
3742 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3743 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3745 pp_printf (pp, "%s<",
3746 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
3748 tree args_a = TI_ARGS (tinfo_a);
3749 tree args_b = TI_ARGS (tinfo_b);
3750 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3751 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3752 int flags = 0;
3753 int len_a = get_non_default_template_args_count (args_a, flags);
3754 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
3755 int len_b = get_non_default_template_args_count (args_b, flags);
3756 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
3757 /* Determine the maximum range of args for which non-default template args
3758 were used; beyond this, only default args (if any) were used, and so
3759 they will be equal from this point onwards.
3760 One of the two peers might have used default arguments within this
3761 range, but the other will be using non-default arguments, and so
3762 it's more readable to print both within this range, to highlight
3763 the differences. */
3764 int len_max = MAX (len_a, len_b);
3765 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3766 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3767 for (int idx = 0; idx < len_max; idx++)
3769 if (idx)
3770 pp_character (pp, ',');
3772 tree arg_a = TREE_VEC_ELT (args_a, idx);
3773 tree arg_b = TREE_VEC_ELT (args_b, idx);
3774 if (arg_a == arg_b)
3776 if (indent)
3777 newline_and_indent (pp, indent + 2);
3778 /* Can do elision here, printing "[...]". */
3779 if (flag_elide_type)
3780 pp_string (pp, G_("[...]"));
3781 else
3782 pp_string (pp, arg_to_string (arg_a, verbose));
3784 else
3786 int new_indent = indent ? indent + 2 : 0;
3787 if (comparable_template_types_p (arg_a, arg_b))
3788 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
3789 else
3790 if (indent)
3792 newline_and_indent (pp, indent + 2);
3793 pp_character (pp, '[');
3794 print_nonequal_arg (pp, arg_a, verbose);
3795 pp_string (pp, " != ");
3796 print_nonequal_arg (pp, arg_b, verbose);
3797 pp_character (pp, ']');
3799 else
3800 print_nonequal_arg (pp, arg_a, verbose);
3803 pp_printf (pp, ">");
3806 /* As type_to_string, but for a template, potentially colorizing/eliding
3807 in comparison with PEER.
3808 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3809 then the resulting string would be:
3810 map<[...],double>
3811 with type elision, and:
3812 map<int,double>
3813 without type elision.
3815 In both cases the parts of TYPE that differ from PEER will be colorized
3816 if SHOW_COLOR is true. In the above example, this would be "double".
3818 Template arguments in which both types are using the default arguments
3819 are not printed; if at least one of the two types is using a non-default
3820 argument, then both arguments are printed.
3822 The resulting string is in a GC-allocated buffer. */
3824 static const char *
3825 type_to_string_with_compare (tree type, tree peer, bool verbose,
3826 bool show_color)
3828 pretty_printer inner_pp;
3829 pretty_printer *pp = &inner_pp;
3830 pp_show_color (pp) = show_color;
3832 print_template_differences (pp, type, peer, verbose, 0);
3833 return pp_ggc_formatted_text (pp);
3836 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
3837 indented by INDENT spaces.
3839 For example given types:
3841 vector<map<int,double>>
3845 vector<map<double,float>>
3847 the output with type elision would be:
3849 vector<
3850 map<
3851 [...],
3852 [double != float]>>
3854 and without type-elision would be:
3856 vector<
3857 map<
3858 int,
3859 [double != float]>>
3861 TYPE_A and TYPE_B must both be comparable template types
3862 (as per comparable_template_types_p).
3864 Template arguments in which both types are using the default arguments
3865 are not printed; if at least one of the two types is using a non-default
3866 argument, then both arguments are printed. */
3868 static void
3869 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
3870 bool verbose, int indent)
3872 print_template_differences (pp, type_a, type_b, verbose, indent);
3875 /* Subroutine for use in a format_postprocessor::handle
3876 implementation. Adds a chunk to the end of
3877 formatted output, so that it will be printed
3878 by pp_output_formatted_text. */
3880 static void
3881 append_formatted_chunk (pretty_printer *pp, const char *content)
3883 output_buffer *buffer = pp_buffer (pp);
3884 struct chunk_info *chunk_array = buffer->cur_chunk_array;
3885 const char **args = chunk_array->args;
3887 unsigned int chunk_idx;
3888 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
3890 args[chunk_idx++] = content;
3891 args[chunk_idx] = NULL;
3894 /* Create a copy of CONTENT, with quotes added, and,
3895 potentially, with colorization.
3896 No escaped is performed on CONTENT.
3897 The result is in a GC-allocated buffer. */
3899 static const char *
3900 add_quotes (const char *content, bool show_color)
3902 pretty_printer tmp_pp;
3903 pp_show_color (&tmp_pp) = show_color;
3905 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
3906 quoting colorization bytes within the results. */
3907 pp_printf (&tmp_pp, "%<%s%>", content);
3909 return pp_ggc_formatted_text (&tmp_pp);
3912 /* If we had %H and %I, and hence deferred printing them,
3913 print them now, storing the result into the chunk_info
3914 for pp_format. Quote them if 'q' was provided.
3915 Also print the difference in tree form, adding it as
3916 an additional chunk. */
3918 void
3919 cxx_format_postprocessor::handle (pretty_printer *pp)
3921 /* If we have one of %H and %I, the other should have
3922 been present. */
3923 if (m_type_a.m_tree || m_type_b.m_tree)
3925 /* Avoid reentrancy issues by working with a copy of
3926 m_type_a and m_type_b, resetting them now. */
3927 deferred_printed_type type_a = m_type_a;
3928 deferred_printed_type type_b = m_type_b;
3929 m_type_a = deferred_printed_type ();
3930 m_type_b = deferred_printed_type ();
3932 gcc_assert (type_a.m_buffer_ptr);
3933 gcc_assert (type_b.m_buffer_ptr);
3935 bool show_color = pp_show_color (pp);
3937 const char *type_a_text;
3938 const char *type_b_text;
3940 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
3942 type_a_text
3943 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
3944 type_a.m_verbose, show_color);
3945 type_b_text
3946 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
3947 type_b.m_verbose, show_color);
3949 if (flag_diagnostics_show_template_tree)
3951 pretty_printer inner_pp;
3952 pp_show_color (&inner_pp) = pp_show_color (pp);
3953 print_template_tree_comparison
3954 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
3955 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
3958 else
3960 /* If the types were not comparable (or if only one of %H/%I was
3961 provided), they are printed normally, and no difference tree
3962 is printed. */
3963 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
3964 true, &type_a.m_quote, show_color);
3965 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
3966 true, &type_b.m_quote, show_color);
3969 if (type_a.m_quote)
3970 type_a_text = add_quotes (type_a_text, show_color);
3971 *type_a.m_buffer_ptr = type_a_text;
3973 if (type_b.m_quote)
3974 type_b_text = add_quotes (type_b_text, show_color);
3975 *type_b.m_buffer_ptr = type_b_text;
3979 /* Subroutine for handling %H and %I, to support i18n of messages like:
3981 error_at (loc, "could not convert %qE from %qH to %qI",
3982 expr, type_a, type_b);
3984 so that we can print things like:
3986 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
3988 and, with type-elision:
3990 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
3992 (with color-coding of the differences between the types).
3994 The %H and %I format codes are peers: both must be present,
3995 and they affect each other. Hence to handle them, we must
3996 delay printing until we have both, deferring the printing to
3997 pretty_printer's m_format_postprocessor hook.
3999 This is called in phase 2 of pp_format, when it is accumulating
4000 a series of formatted chunks. We stash the location of the chunk
4001 we're meant to have written to, so that we can write to it in the
4002 m_format_postprocessor hook.
4004 We also need to stash whether a 'q' prefix was provided (the QUOTE
4005 param) so that we can add the quotes when writing out the delayed
4006 chunk. */
4008 static void
4009 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4010 tree type, const char **buffer_ptr,
4011 bool verbose, bool quote)
4013 gcc_assert (deferred->m_tree == NULL_TREE);
4014 gcc_assert (deferred->m_buffer_ptr == NULL);
4015 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4019 /* Called from output_format -- during diagnostic message processing --
4020 to handle C++ specific format specifier with the following meanings:
4021 %A function argument-list.
4022 %C tree code.
4023 %D declaration.
4024 %E expression.
4025 %F function declaration.
4026 %G gcall *
4027 %H type difference (from).
4028 %I type difference (to).
4029 %K tree
4030 %L language as used in extern "lang".
4031 %O binary operator.
4032 %P function parameter whose position is indicated by an integer.
4033 %Q assignment operator.
4034 %S substitution (template + args)
4035 %T type.
4036 %V cv-qualifier.
4037 %X exception-specification. */
4038 static bool
4039 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4040 int precision, bool wide, bool set_locus, bool verbose,
4041 bool *quoted, const char **buffer_ptr)
4043 gcc_assert (pp->m_format_postprocessor);
4044 cxx_format_postprocessor *postprocessor
4045 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4047 const char *result;
4048 tree t = NULL;
4049 #define next_tree (t = va_arg (*text->args_ptr, tree))
4050 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4051 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4052 #define next_int va_arg (*text->args_ptr, int)
4054 if (precision != 0 || wide)
4055 return false;
4057 switch (*spec)
4059 case 'A': result = args_to_string (next_tree, verbose); break;
4060 case 'C': result = code_to_string (next_tcode); break;
4061 case 'D':
4063 tree temp = next_tree;
4064 if (VAR_P (temp)
4065 && DECL_HAS_DEBUG_EXPR_P (temp))
4067 temp = DECL_DEBUG_EXPR (temp);
4068 if (!DECL_P (temp))
4070 result = expr_to_string (temp);
4071 break;
4074 result = decl_to_string (temp, verbose);
4076 break;
4077 case 'E': result = expr_to_string (next_tree); break;
4078 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4079 case 'G':
4080 percent_G_format (text);
4081 return true;
4082 case 'H':
4083 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4084 buffer_ptr, verbose, *quoted);
4085 return true;
4086 case 'I':
4087 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4088 buffer_ptr, verbose, *quoted);
4089 return true;
4090 case 'K':
4091 t = va_arg (*text->args_ptr, tree);
4092 percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t));
4093 return true;
4094 case 'L': result = language_to_string (next_lang); break;
4095 case 'O': result = op_to_string (false, next_tcode); break;
4096 case 'P': result = parm_to_string (next_int); break;
4097 case 'Q': result = op_to_string (true, next_tcode); break;
4098 case 'S': result = subst_to_string (next_tree); break;
4099 case 'T':
4101 result = type_to_string (next_tree, verbose, false, quoted,
4102 pp_show_color (pp));
4104 break;
4105 case 'V': result = cv_to_string (next_tree, verbose); break;
4106 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4108 default:
4109 return false;
4112 pp_string (pp, result);
4113 if (set_locus && t != NULL)
4114 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4115 return true;
4116 #undef next_tree
4117 #undef next_tcode
4118 #undef next_lang
4119 #undef next_int
4122 /* Warn about the use of C++0x features when appropriate. */
4123 void
4124 maybe_warn_cpp0x (cpp0x_warn_str str)
4126 if ((cxx_dialect == cxx98) && !in_system_header_at (input_location))
4127 /* We really want to suppress this warning in system headers,
4128 because libstdc++ uses variadic templates even when we aren't
4129 in C++0x mode. */
4130 switch (str)
4132 case CPP0X_INITIALIZER_LISTS:
4133 pedwarn (input_location, 0,
4134 "extended initializer lists "
4135 "only available with -std=c++11 or -std=gnu++11");
4136 break;
4137 case CPP0X_EXPLICIT_CONVERSION:
4138 pedwarn (input_location, 0,
4139 "explicit conversion operators "
4140 "only available with -std=c++11 or -std=gnu++11");
4141 break;
4142 case CPP0X_VARIADIC_TEMPLATES:
4143 pedwarn (input_location, 0,
4144 "variadic templates "
4145 "only available with -std=c++11 or -std=gnu++11");
4146 break;
4147 case CPP0X_LAMBDA_EXPR:
4148 pedwarn (input_location, 0,
4149 "lambda expressions "
4150 "only available with -std=c++11 or -std=gnu++11");
4151 break;
4152 case CPP0X_AUTO:
4153 pedwarn (input_location, 0,
4154 "C++11 auto only available with -std=c++11 or -std=gnu++11");
4155 break;
4156 case CPP0X_SCOPED_ENUMS:
4157 pedwarn (input_location, 0,
4158 "scoped enums only available with -std=c++11 or -std=gnu++11");
4159 break;
4160 case CPP0X_DEFAULTED_DELETED:
4161 pedwarn (input_location, 0,
4162 "defaulted and deleted functions "
4163 "only available with -std=c++11 or -std=gnu++11");
4164 break;
4165 case CPP0X_INLINE_NAMESPACES:
4166 pedwarn (input_location, OPT_Wpedantic,
4167 "inline namespaces "
4168 "only available with -std=c++11 or -std=gnu++11");
4169 break;
4170 case CPP0X_OVERRIDE_CONTROLS:
4171 pedwarn (input_location, 0,
4172 "override controls (override/final) "
4173 "only available with -std=c++11 or -std=gnu++11");
4174 break;
4175 case CPP0X_NSDMI:
4176 pedwarn (input_location, 0,
4177 "non-static data member initializers "
4178 "only available with -std=c++11 or -std=gnu++11");
4179 break;
4180 case CPP0X_USER_DEFINED_LITERALS:
4181 pedwarn (input_location, 0,
4182 "user-defined literals "
4183 "only available with -std=c++11 or -std=gnu++11");
4184 break;
4185 case CPP0X_DELEGATING_CTORS:
4186 pedwarn (input_location, 0,
4187 "delegating constructors "
4188 "only available with -std=c++11 or -std=gnu++11");
4189 break;
4190 case CPP0X_INHERITING_CTORS:
4191 pedwarn (input_location, 0,
4192 "inheriting constructors "
4193 "only available with -std=c++11 or -std=gnu++11");
4194 break;
4195 case CPP0X_ATTRIBUTES:
4196 pedwarn (input_location, 0,
4197 "c++11 attributes "
4198 "only available with -std=c++11 or -std=gnu++11");
4199 break;
4200 case CPP0X_REF_QUALIFIER:
4201 pedwarn (input_location, 0,
4202 "ref-qualifiers "
4203 "only available with -std=c++11 or -std=gnu++11");
4204 break;
4205 default:
4206 gcc_unreachable ();
4210 /* Warn about the use of variadic templates when appropriate. */
4211 void
4212 maybe_warn_variadic_templates (void)
4214 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4218 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4219 option OPT with text GMSGID. Use this function to report
4220 diagnostics for constructs that are invalid C++98, but valid
4221 C++0x. */
4222 bool
4223 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4225 diagnostic_info diagnostic;
4226 va_list ap;
4227 bool ret;
4228 rich_location richloc (line_table, location);
4230 va_start (ap, gmsgid);
4231 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4232 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4233 diagnostic.option_index = opt;
4234 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4235 va_end (ap);
4236 return ret;
4239 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4240 we found when we tried to do the lookup. LOCATION is the location of
4241 the NAME identifier. */
4243 void
4244 qualified_name_lookup_error (tree scope, tree name,
4245 tree decl, location_t location)
4247 if (scope == error_mark_node)
4248 ; /* We already complained. */
4249 else if (TYPE_P (scope))
4251 if (!COMPLETE_TYPE_P (scope))
4252 error_at (location, "incomplete type %qT used in nested name specifier",
4253 scope);
4254 else if (TREE_CODE (decl) == TREE_LIST)
4256 error_at (location, "reference to %<%T::%D%> is ambiguous",
4257 scope, name);
4258 print_candidates (decl);
4260 else
4261 error_at (location, "%qD is not a member of %qT", name, scope);
4263 else if (scope != global_namespace)
4265 auto_diagnostic_group d;
4266 bool emit_fixit = true;
4267 name_hint hint
4268 = suggest_alternative_in_explicit_scope (location, name, scope);
4269 if (!hint)
4271 hint = suggest_alternatives_in_other_namespaces (location, name);
4272 /* "location" is just the location of the name, not of the explicit
4273 scope, and it's not easy to get at the latter, so we can't issue
4274 fix-it hints for the suggestion. */
4275 emit_fixit = false;
4277 if (const char *suggestion = hint.suggestion ())
4279 gcc_rich_location richloc (location);
4280 if (emit_fixit)
4281 richloc.add_fixit_replace (suggestion);
4282 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4283 name, scope, suggestion);
4285 else
4286 error_at (location, "%qD is not a member of %qD", name, scope);
4288 else
4290 auto_diagnostic_group d;
4291 name_hint hint = suggest_alternatives_for (location, name, true);
4292 if (const char *suggestion = hint.suggestion ())
4294 gcc_rich_location richloc (location);
4295 richloc.add_fixit_replace (suggestion);
4296 error_at (&richloc,
4297 "%<::%D%> has not been declared; did you mean %qs?",
4298 name, suggestion);
4300 else
4301 error_at (location, "%<::%D%> has not been declared", name);
4305 /* C++-specific implementation of range_label::get_text () vfunc for
4306 range_label_for_type_mismatch.
4308 Compare with print_template_differences above. */
4310 label_text
4311 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4313 if (m_labelled_type == NULL_TREE)
4314 return label_text (NULL, false);
4316 const bool verbose = false;
4317 const bool show_color = false;
4319 const char *result;
4320 if (m_other_type
4321 && comparable_template_types_p (m_labelled_type, m_other_type))
4322 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4323 verbose, show_color);
4324 else
4325 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4327 /* Both of the above return GC-allocated buffers, so the caller mustn't
4328 free them. */
4329 return label_text (const_cast <char *> (result), false);