Daily bump.
[official-gcc.git] / gcc / cp / error.c
blob012a4ecddf44661f12d8df1d5e474614b5de3d39
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2021 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 maybe_print_constraint_context (diagnostic_context *);
102 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
103 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
105 static bool cp_printer (pretty_printer *, text_info *, const char *,
106 int, bool, bool, bool, bool *, const char **);
108 /* Struct for handling %H or %I, which require delaying printing the
109 type until a postprocessing stage. */
111 class deferred_printed_type
113 public:
114 deferred_printed_type ()
115 : m_tree (NULL_TREE), m_buffer_ptr (NULL), m_verbose (false), m_quote (false)
118 deferred_printed_type (tree type, const char **buffer_ptr, bool verbose,
119 bool quote)
120 : m_tree (type), m_buffer_ptr (buffer_ptr), m_verbose (verbose),
121 m_quote (quote)
123 gcc_assert (type);
124 gcc_assert (buffer_ptr);
127 /* The tree is not GTY-marked: they are only non-NULL within a
128 call to pp_format. */
129 tree m_tree;
130 const char **m_buffer_ptr;
131 bool m_verbose;
132 bool m_quote;
135 /* Subclass of format_postprocessor for the C++ frontend.
136 This handles the %H and %I formatting codes, printing them
137 in a postprocessing phase (since they affect each other). */
139 class cxx_format_postprocessor : public format_postprocessor
141 public:
142 cxx_format_postprocessor ()
143 : m_type_a (), m_type_b ()
146 format_postprocessor *clone() const FINAL OVERRIDE
148 return new cxx_format_postprocessor ();
151 void handle (pretty_printer *pp) FINAL OVERRIDE;
153 deferred_printed_type m_type_a;
154 deferred_printed_type m_type_b;
157 /* CONTEXT->printer is a basic pretty printer that was constructed
158 presumably by diagnostic_initialize(), called early in the
159 compiler's initialization process (in general_init) Before the FE
160 is initialized. This (C++) FE-specific diagnostic initializer is
161 thus replacing the basic pretty printer with one that has C++-aware
162 capacities. */
164 void
165 cxx_initialize_diagnostics (diagnostic_context *context)
167 pretty_printer *base = context->printer;
168 cxx_pretty_printer *pp = XNEW (cxx_pretty_printer);
169 context->printer = new (pp) cxx_pretty_printer ();
171 /* It is safe to free this object because it was previously XNEW()'d. */
172 base->~pretty_printer ();
173 XDELETE (base);
175 c_common_diagnostics_set_defaults (context);
176 diagnostic_starter (context) = cp_diagnostic_starter;
177 /* diagnostic_finalizer is already c_diagnostic_finalizer. */
178 diagnostic_format_decoder (context) = cp_printer;
179 pp->m_format_postprocessor = new cxx_format_postprocessor ();
182 /* Dump an '@module' name suffix for DECL, if any. */
184 static void
185 dump_module_suffix (cxx_pretty_printer *pp, tree decl)
187 if (!modules_p ())
188 return;
190 if (!DECL_CONTEXT (decl))
191 return;
193 if (TREE_CODE (decl) != CONST_DECL
194 || !UNSCOPED_ENUM_P (DECL_CONTEXT (decl)))
196 if (!DECL_NAMESPACE_SCOPE_P (decl))
197 return;
199 if (TREE_CODE (decl) == NAMESPACE_DECL
200 && !DECL_NAMESPACE_ALIAS (decl)
201 && (TREE_PUBLIC (decl) || !TREE_PUBLIC (CP_DECL_CONTEXT (decl))))
202 return;
205 if (unsigned m = get_originating_module (decl))
206 if (const char *n = module_name (m, false))
208 pp_character (pp, '@');
209 pp->padding = pp_none;
210 pp_string (pp, n);
214 /* Dump a scope, if deemed necessary. */
216 static void
217 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
219 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
221 if (scope == NULL_TREE)
222 return;
224 /* Enum values within an unscoped enum will be CONST_DECL with an
225 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
226 ENUMERAL_TYPE, so as to print any enclosing namespace. */
227 if (UNSCOPED_ENUM_P (scope))
228 scope = CP_TYPE_CONTEXT (scope);
230 if (TREE_CODE (scope) == NAMESPACE_DECL)
232 if (scope != global_namespace)
234 dump_decl (pp, scope, f);
235 pp_cxx_colon_colon (pp);
238 else if (AGGREGATE_TYPE_P (scope)
239 || SCOPED_ENUM_P (scope))
241 dump_type (pp, scope, f);
242 pp_cxx_colon_colon (pp);
244 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
246 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
247 pp_cxx_colon_colon (pp);
251 /* Dump the template ARGument under control of FLAGS. */
253 static void
254 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
256 if (ARGUMENT_PACK_P (arg))
257 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
258 /* No default args in argument packs. */
259 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
260 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
261 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
262 else
264 if (TREE_CODE (arg) == TREE_LIST)
265 arg = TREE_VALUE (arg);
267 /* Strip implicit conversions. */
268 while (CONVERT_EXPR_P (arg))
269 arg = TREE_OPERAND (arg, 0);
271 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
275 /* Count the number of template arguments ARGS whose value does not
276 match the (optional) default template parameter in PARAMS */
278 static int
279 get_non_default_template_args_count (tree args, int flags)
281 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
283 if (/* We use this flag when generating debug information. We don't
284 want to expand templates at this point, for this may generate
285 new decls, which gets decl counts out of sync, which may in
286 turn cause codegen differences between compilations with and
287 without -g. */
288 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
289 || !flag_pretty_templates)
290 return n;
292 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
295 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
296 of FLAGS. */
298 static void
299 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
301 int n = get_non_default_template_args_count (args, flags);
302 int need_comma = 0;
303 int i;
305 for (i = 0; i < n; ++i)
307 tree arg = TREE_VEC_ELT (args, i);
309 /* Only print a comma if we know there is an argument coming. In
310 the case of an empty template argument pack, no actual
311 argument will be printed. */
312 if (need_comma
313 && (!ARGUMENT_PACK_P (arg)
314 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
315 pp_separate_with_comma (pp);
317 dump_template_argument (pp, arg, flags);
318 need_comma = 1;
322 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
324 static void
325 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
327 tree p;
328 tree a;
330 if (parm == error_mark_node)
331 return;
333 p = TREE_VALUE (parm);
334 a = TREE_PURPOSE (parm);
336 if (TREE_CODE (p) == TYPE_DECL)
338 if (flags & TFF_DECL_SPECIFIERS)
340 pp_cxx_ws_string (pp, "class");
341 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
342 pp_cxx_ws_string (pp, "...");
343 if (DECL_NAME (p))
344 pp_cxx_tree_identifier (pp, DECL_NAME (p));
346 else if (DECL_NAME (p))
347 pp_cxx_tree_identifier (pp, DECL_NAME (p));
348 else
349 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
351 else
352 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
354 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
356 pp_cxx_whitespace (pp);
357 pp_equal (pp);
358 pp_cxx_whitespace (pp);
359 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
360 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
361 else
362 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
366 /* Dump, under control of FLAGS, a template-parameter-list binding.
367 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
368 TREE_VEC. */
370 static void
371 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
372 vec<tree, va_gc> *typenames)
374 /* Print "[with" and ']', conditional on whether anything is printed at all.
375 This is tied to whether a semicolon is needed to separate multiple template
376 parameters. */
377 struct prepost_semicolon
379 cxx_pretty_printer *pp;
380 bool need_semicolon;
382 void operator() ()
384 if (need_semicolon)
385 pp_separate_with_semicolon (pp);
386 else
388 pp_cxx_whitespace (pp);
389 pp_cxx_left_bracket (pp);
390 pp->translate_string ("with");
391 pp_cxx_whitespace (pp);
392 need_semicolon = true;
396 ~prepost_semicolon ()
398 if (need_semicolon)
399 pp_cxx_right_bracket (pp);
401 } semicolon_or_introducer = {pp, false};
403 int i;
404 tree t;
406 while (parms)
408 tree p = TREE_VALUE (parms);
409 int lvl = TMPL_PARMS_DEPTH (parms);
410 int arg_idx = 0;
411 int i;
412 tree lvl_args = NULL_TREE;
414 /* Don't crash if we had an invalid argument list. */
415 if (TMPL_ARGS_DEPTH (args) >= lvl)
416 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
418 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
420 tree arg = NULL_TREE;
422 /* Don't crash if we had an invalid argument list. */
423 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
424 arg = TREE_VEC_ELT (lvl_args, arg_idx);
426 tree parm_i = TREE_VEC_ELT (p, i);
427 /* If the template argument repeats the template parameter (T = T),
428 skip the parameter.*/
429 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
430 && TREE_CODE (parm_i) == TREE_LIST
431 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
432 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
433 == TEMPLATE_TYPE_PARM
434 && DECL_NAME (TREE_VALUE (parm_i))
435 == DECL_NAME (TREE_CHAIN (arg)))
436 continue;
438 semicolon_or_introducer ();
439 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
440 pp_cxx_whitespace (pp);
441 pp_equal (pp);
442 pp_cxx_whitespace (pp);
443 if (arg)
445 if (ARGUMENT_PACK_P (arg))
446 pp_cxx_left_brace (pp);
447 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
448 if (ARGUMENT_PACK_P (arg))
449 pp_cxx_right_brace (pp);
451 else
452 pp_string (pp, M_("<missing>"));
454 ++arg_idx;
457 parms = TREE_CHAIN (parms);
460 /* Don't bother with typenames for a partial instantiation. */
461 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
462 return;
464 /* Don't try to print typenames when we're processing a clone. */
465 if (current_function_decl
466 && !DECL_LANG_SPECIFIC (current_function_decl))
467 return;
469 /* Don't try to do this once cgraph starts throwing away front-end
470 information. */
471 if (at_eof >= 2)
472 return;
474 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
476 semicolon_or_introducer ();
477 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
478 pp_cxx_whitespace (pp);
479 pp_equal (pp);
480 pp_cxx_whitespace (pp);
481 push_deferring_access_checks (dk_no_check);
482 t = tsubst (t, args, tf_none, NULL_TREE);
483 pop_deferring_access_checks ();
484 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
485 pp_simple_type_specifier doesn't know about it. */
486 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
487 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
491 /* Dump a human-readable equivalent of the alias template
492 specialization of T. */
494 static void
495 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
497 gcc_assert (alias_template_specialization_p (t, nt_opaque));
499 tree decl = TYPE_NAME (t);
500 if (!(flags & TFF_UNQUALIFIED_NAME))
501 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
502 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
503 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
504 /*primary=*/false,
505 flags & ~TFF_TEMPLATE_HEADER);
508 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
509 format. */
511 static void
512 dump_type (cxx_pretty_printer *pp, tree t, int flags)
514 if (t == NULL_TREE)
515 return;
517 /* Don't print e.g. "struct mytypedef". */
518 if (TYPE_P (t) && typedef_variant_p (t))
520 tree decl = TYPE_NAME (t);
521 if ((flags & TFF_CHASE_TYPEDEF)
522 || DECL_SELF_REFERENCE_P (decl)
523 || (!flag_pretty_templates
524 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
526 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
527 ? STF_USER_VISIBLE : 0);
528 t = strip_typedefs (t, NULL, stf_flags);
530 else if (alias_template_specialization_p (t, nt_opaque))
532 dump_alias_template_specialization (pp, t, flags);
533 return;
535 else if (same_type_p (t, TREE_TYPE (decl)))
536 t = decl;
537 else
539 pp_cxx_cv_qualifier_seq (pp, t);
540 if (! (flags & TFF_UNQUALIFIED_NAME))
541 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
542 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
543 return;
547 if (TYPE_PTRMEMFUNC_P (t))
548 goto offset_type;
550 switch (TREE_CODE (t))
552 case LANG_TYPE:
553 if (t == init_list_type_node)
554 pp_string (pp, M_("<brace-enclosed initializer list>"));
555 else if (t == unknown_type_node)
556 pp_string (pp, M_("<unresolved overloaded function type>"));
557 else
559 pp_cxx_cv_qualifier_seq (pp, t);
560 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
562 break;
564 case TREE_LIST:
565 /* A list of function parms. */
566 dump_parameters (pp, t, flags);
567 break;
569 case IDENTIFIER_NODE:
570 pp_cxx_tree_identifier (pp, t);
571 break;
573 case TREE_BINFO:
574 dump_type (pp, BINFO_TYPE (t), flags);
575 break;
577 case RECORD_TYPE:
578 case UNION_TYPE:
579 case ENUMERAL_TYPE:
580 dump_aggr_type (pp, t, flags);
581 break;
583 case TYPE_DECL:
584 if (flags & TFF_CHASE_TYPEDEF)
586 dump_type (pp, DECL_ORIGINAL_TYPE (t)
587 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
588 break;
590 /* Fall through. */
592 case TEMPLATE_DECL:
593 case NAMESPACE_DECL:
594 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
595 break;
597 case INTEGER_TYPE:
598 case REAL_TYPE:
599 case VOID_TYPE:
600 case OPAQUE_TYPE:
601 case BOOLEAN_TYPE:
602 case COMPLEX_TYPE:
603 case VECTOR_TYPE:
604 case FIXED_POINT_TYPE:
605 pp_type_specifier_seq (pp, t);
606 break;
608 case TEMPLATE_TEMPLATE_PARM:
609 /* For parameters inside template signature. */
610 if (TYPE_IDENTIFIER (t))
611 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
612 else
613 pp_cxx_canonical_template_parameter (pp, t);
614 break;
616 case BOUND_TEMPLATE_TEMPLATE_PARM:
618 tree args = TYPE_TI_ARGS (t);
619 pp_cxx_cv_qualifier_seq (pp, t);
620 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
621 pp_cxx_begin_template_argument_list (pp);
622 dump_template_argument_list (pp, args, flags);
623 pp_cxx_end_template_argument_list (pp);
625 break;
627 case TEMPLATE_TYPE_PARM:
628 pp_cxx_cv_qualifier_seq (pp, t);
629 if (template_placeholder_p (t))
631 t = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
632 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
633 pp_string (pp, "<...auto...>");
635 else if (TYPE_IDENTIFIER (t))
636 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
637 else
638 pp_cxx_canonical_template_parameter
639 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
640 /* If this is a constrained placeholder, add the requirements. */
641 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
642 pp_cxx_constrained_type_spec (pp, c);
643 break;
645 /* This is not always necessary for pointers and such, but doing this
646 reduces code size. */
647 case ARRAY_TYPE:
648 case POINTER_TYPE:
649 case REFERENCE_TYPE:
650 case OFFSET_TYPE:
651 offset_type:
652 case FUNCTION_TYPE:
653 case METHOD_TYPE:
655 dump_type_prefix (pp, t, flags);
656 dump_type_suffix (pp, t, flags);
657 break;
659 case TYPENAME_TYPE:
660 if (! (flags & TFF_CHASE_TYPEDEF)
661 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
663 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
664 break;
666 pp_cxx_cv_qualifier_seq (pp, t);
667 pp_cxx_ws_string (pp,
668 TYPENAME_IS_ENUM_P (t) ? "enum"
669 : TYPENAME_IS_CLASS_P (t) ? "class"
670 : "typename");
671 dump_typename (pp, t, flags);
672 break;
674 case UNBOUND_CLASS_TEMPLATE:
675 if (! (flags & TFF_UNQUALIFIED_NAME))
677 dump_type (pp, TYPE_CONTEXT (t), flags);
678 pp_cxx_colon_colon (pp);
680 pp_cxx_ws_string (pp, "template");
681 dump_type (pp, TYPE_IDENTIFIER (t), flags);
682 break;
684 case TYPEOF_TYPE:
685 pp_cxx_ws_string (pp, "__typeof__");
686 pp_cxx_whitespace (pp);
687 pp_cxx_left_paren (pp);
688 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
689 pp_cxx_right_paren (pp);
690 break;
692 case UNDERLYING_TYPE:
693 pp_cxx_ws_string (pp, "__underlying_type");
694 pp_cxx_whitespace (pp);
695 pp_cxx_left_paren (pp);
696 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
697 pp_cxx_right_paren (pp);
698 break;
700 case TYPE_PACK_EXPANSION:
701 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
702 pp_cxx_ws_string (pp, "...");
703 break;
705 case TYPE_ARGUMENT_PACK:
706 dump_template_argument (pp, t, flags);
707 break;
709 case DECLTYPE_TYPE:
710 pp_cxx_ws_string (pp, "decltype");
711 pp_cxx_whitespace (pp);
712 pp_cxx_left_paren (pp);
713 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
714 pp_cxx_right_paren (pp);
715 break;
717 case NULLPTR_TYPE:
718 pp_string (pp, "std::nullptr_t");
719 break;
721 default:
722 pp_unsupported_tree (pp, t);
723 /* Fall through. */
725 case ERROR_MARK:
726 pp_string (pp, M_("<type error>"));
727 break;
731 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
732 a TYPENAME_TYPE. */
734 static void
735 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
737 tree ctx = TYPE_CONTEXT (t);
739 if (TREE_CODE (ctx) == TYPENAME_TYPE)
740 dump_typename (pp, ctx, flags);
741 else
742 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
743 pp_cxx_colon_colon (pp);
744 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
747 /* Return the name of the supplied aggregate, or enumeral type. */
749 const char *
750 class_key_or_enum_as_string (tree t)
752 if (TREE_CODE (t) == ENUMERAL_TYPE)
754 if (SCOPED_ENUM_P (t))
755 return "enum class";
756 else
757 return "enum";
759 else if (TREE_CODE (t) == UNION_TYPE)
760 return "union";
761 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
762 return "class";
763 else
764 return "struct";
767 /* Print out a class declaration T under the control of FLAGS,
768 in the form `class foo'. */
770 static void
771 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
773 const char *variety = class_key_or_enum_as_string (t);
774 int typdef = 0;
775 int tmplate = 0;
777 pp_cxx_cv_qualifier_seq (pp, t);
779 if (flags & TFF_CLASS_KEY_OR_ENUM)
780 pp_cxx_ws_string (pp, variety);
782 tree decl = TYPE_NAME (t);
784 if (decl)
786 typdef = (!DECL_ARTIFICIAL (decl)
787 /* An alias specialization is not considered to be a
788 typedef. */
789 && !alias_template_specialization_p (t, nt_opaque));
791 if ((typdef
792 && ((flags & TFF_CHASE_TYPEDEF)
793 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
794 && DECL_TEMPLATE_INFO (decl))))
795 || DECL_SELF_REFERENCE_P (decl))
797 t = TYPE_MAIN_VARIANT (t);
798 decl = TYPE_NAME (t);
799 typdef = 0;
802 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
803 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
804 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
805 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
807 if (! (flags & TFF_UNQUALIFIED_NAME))
808 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
809 flags &= ~TFF_UNQUALIFIED_NAME;
810 if (tmplate)
812 /* Because the template names are mangled, we have to locate
813 the most general template, and use that name. */
814 tree tpl = TYPE_TI_TEMPLATE (t);
816 while (DECL_TEMPLATE_INFO (tpl))
817 tpl = DECL_TI_TEMPLATE (tpl);
818 decl = tpl;
822 if (LAMBDA_TYPE_P (t))
824 /* A lambda's "type" is essentially its signature. */
825 pp_string (pp, M_("<lambda"));
826 if (lambda_function (t))
827 dump_parameters (pp,
828 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
829 flags);
830 pp_greater (pp);
832 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
834 if (flags & TFF_CLASS_KEY_OR_ENUM)
835 pp_string (pp, M_("<unnamed>"));
836 else
837 pp_printf (pp, M_("<unnamed %s>"), variety);
839 else
840 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
842 dump_module_suffix (pp, decl);
844 if (tmplate)
845 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
846 !CLASSTYPE_USE_TEMPLATE (t),
847 flags & ~TFF_TEMPLATE_HEADER);
850 /* Dump into the obstack the initial part of the output for a given type.
851 This is necessary when dealing with things like functions returning
852 functions. Examples:
854 return type of `int (* fee ())()': pointer -> function -> int. Both
855 pointer (and reference and offset) and function (and member) types must
856 deal with prefix and suffix.
858 Arrays must also do this for DECL nodes, like int a[], and for things like
859 int *[]&. */
861 static void
862 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
864 if (TYPE_PTRMEMFUNC_P (t))
866 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
867 goto offset_type;
870 switch (TREE_CODE (t))
872 case POINTER_TYPE:
873 case REFERENCE_TYPE:
875 tree sub = TREE_TYPE (t);
877 dump_type_prefix (pp, sub, flags);
878 if (TREE_CODE (sub) == ARRAY_TYPE
879 || TREE_CODE (sub) == FUNCTION_TYPE)
881 pp_cxx_whitespace (pp);
882 pp_cxx_left_paren (pp);
883 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
885 if (TYPE_PTR_P (t))
886 pp_star (pp);
887 else if (TYPE_REF_P (t))
889 if (TYPE_REF_IS_RVALUE (t))
890 pp_ampersand_ampersand (pp);
891 else
892 pp_ampersand (pp);
894 pp->padding = pp_before;
895 pp_cxx_cv_qualifier_seq (pp, t);
897 break;
899 case OFFSET_TYPE:
900 offset_type:
901 dump_type_prefix (pp, TREE_TYPE (t), flags);
902 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
904 pp_maybe_space (pp);
905 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
906 pp_cxx_left_paren (pp);
907 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
908 pp_cxx_colon_colon (pp);
910 pp_cxx_star (pp);
911 pp_cxx_cv_qualifier_seq (pp, t);
912 pp->padding = pp_before;
913 break;
915 /* This can be reached without a pointer when dealing with
916 templates, e.g. std::is_function. */
917 case FUNCTION_TYPE:
918 dump_type_prefix (pp, TREE_TYPE (t), flags);
919 break;
921 case METHOD_TYPE:
922 dump_type_prefix (pp, TREE_TYPE (t), flags);
923 pp_maybe_space (pp);
924 pp_cxx_left_paren (pp);
925 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
926 pp_cxx_colon_colon (pp);
927 break;
929 case ARRAY_TYPE:
930 dump_type_prefix (pp, TREE_TYPE (t), flags);
931 break;
933 case ENUMERAL_TYPE:
934 case IDENTIFIER_NODE:
935 case INTEGER_TYPE:
936 case BOOLEAN_TYPE:
937 case REAL_TYPE:
938 case RECORD_TYPE:
939 case TEMPLATE_TYPE_PARM:
940 case TEMPLATE_TEMPLATE_PARM:
941 case BOUND_TEMPLATE_TEMPLATE_PARM:
942 case TREE_LIST:
943 case TYPE_DECL:
944 case TREE_VEC:
945 case UNION_TYPE:
946 case LANG_TYPE:
947 case VOID_TYPE:
948 case OPAQUE_TYPE:
949 case TYPENAME_TYPE:
950 case COMPLEX_TYPE:
951 case VECTOR_TYPE:
952 case TYPEOF_TYPE:
953 case UNDERLYING_TYPE:
954 case DECLTYPE_TYPE:
955 case TYPE_PACK_EXPANSION:
956 case FIXED_POINT_TYPE:
957 case NULLPTR_TYPE:
958 dump_type (pp, t, flags);
959 pp->padding = pp_before;
960 break;
962 default:
963 pp_unsupported_tree (pp, t);
964 /* fall through. */
965 case ERROR_MARK:
966 pp_string (pp, M_("<typeprefixerror>"));
967 break;
971 /* Dump the suffix of type T, under control of FLAGS. This is the part
972 which appears after the identifier (or function parms). */
974 static void
975 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
977 if (TYPE_PTRMEMFUNC_P (t))
978 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
980 switch (TREE_CODE (t))
982 case POINTER_TYPE:
983 case REFERENCE_TYPE:
984 case OFFSET_TYPE:
985 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
986 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
987 pp_cxx_right_paren (pp);
988 if (TREE_CODE (t) == POINTER_TYPE)
989 flags |= TFF_POINTER;
990 dump_type_suffix (pp, TREE_TYPE (t), flags);
991 break;
993 case FUNCTION_TYPE:
994 case METHOD_TYPE:
996 tree arg;
997 if (TREE_CODE (t) == METHOD_TYPE)
998 /* Can only be reached through a pointer. */
999 pp_cxx_right_paren (pp);
1000 arg = TYPE_ARG_TYPES (t);
1001 if (TREE_CODE (t) == METHOD_TYPE)
1002 arg = TREE_CHAIN (arg);
1004 /* Function pointers don't have default args. Not in standard C++,
1005 anyway; they may in g++, but we'll just pretend otherwise. */
1006 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1008 pp->padding = pp_before;
1009 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1010 TREE_CODE (t) == FUNCTION_TYPE
1011 && (flags & TFF_POINTER));
1012 dump_ref_qualifier (pp, t, flags);
1013 if (tx_safe_fn_type_p (t))
1014 pp_cxx_ws_string (pp, "transaction_safe");
1015 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1016 dump_type_suffix (pp, TREE_TYPE (t), flags);
1017 break;
1020 case ARRAY_TYPE:
1021 pp_maybe_space (pp);
1022 pp_cxx_left_bracket (pp);
1023 if (tree dtype = TYPE_DOMAIN (t))
1025 tree max = TYPE_MAX_VALUE (dtype);
1026 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1027 in C++. Handle both since the type might be constructed by
1028 the middle end and end up here as a result of a warning (see
1029 PR c++/97201). */
1030 if (!max || integer_all_onesp (max))
1031 pp_character (pp, '0');
1032 else if (tree_fits_shwi_p (max))
1033 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1034 else
1036 STRIP_NOPS (max);
1037 if (TREE_CODE (max) == SAVE_EXPR)
1038 max = TREE_OPERAND (max, 0);
1039 if (TREE_CODE (max) == MINUS_EXPR
1040 || TREE_CODE (max) == PLUS_EXPR)
1042 max = TREE_OPERAND (max, 0);
1043 while (CONVERT_EXPR_P (max))
1044 max = TREE_OPERAND (max, 0);
1046 else
1047 max = fold_build2_loc (input_location,
1048 PLUS_EXPR, dtype, max,
1049 build_int_cst (dtype, 1));
1050 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1053 pp_cxx_right_bracket (pp);
1054 dump_type_suffix (pp, TREE_TYPE (t), flags);
1055 break;
1057 case ENUMERAL_TYPE:
1058 case IDENTIFIER_NODE:
1059 case INTEGER_TYPE:
1060 case BOOLEAN_TYPE:
1061 case REAL_TYPE:
1062 case RECORD_TYPE:
1063 case TEMPLATE_TYPE_PARM:
1064 case TEMPLATE_TEMPLATE_PARM:
1065 case BOUND_TEMPLATE_TEMPLATE_PARM:
1066 case TREE_LIST:
1067 case TYPE_DECL:
1068 case TREE_VEC:
1069 case UNION_TYPE:
1070 case LANG_TYPE:
1071 case VOID_TYPE:
1072 case OPAQUE_TYPE:
1073 case TYPENAME_TYPE:
1074 case COMPLEX_TYPE:
1075 case VECTOR_TYPE:
1076 case TYPEOF_TYPE:
1077 case UNDERLYING_TYPE:
1078 case DECLTYPE_TYPE:
1079 case TYPE_PACK_EXPANSION:
1080 case FIXED_POINT_TYPE:
1081 case NULLPTR_TYPE:
1082 break;
1084 default:
1085 pp_unsupported_tree (pp, t);
1086 case ERROR_MARK:
1087 /* Don't mark it here, we should have already done in
1088 dump_type_prefix. */
1089 break;
1093 static void
1094 dump_global_iord (cxx_pretty_printer *pp, tree t)
1096 const char *p = NULL;
1098 if (DECL_GLOBAL_CTOR_P (t))
1099 p = M_("(static initializers for %s)");
1100 else if (DECL_GLOBAL_DTOR_P (t))
1101 p = M_("(static destructors for %s)");
1102 else
1103 gcc_unreachable ();
1105 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1108 static void
1109 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1111 if (template_parm_object_p (t))
1112 return dump_expr (pp, DECL_INITIAL (t), flags);
1114 if (flags & TFF_DECL_SPECIFIERS)
1116 if (concept_definition_p (t))
1117 pp_cxx_ws_string (pp, "concept");
1118 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1119 pp_cxx_ws_string (pp, "constexpr");
1121 if (!standard_concept_p (t))
1122 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1123 pp_maybe_space (pp);
1125 if (! (flags & TFF_UNQUALIFIED_NAME)
1126 && TREE_CODE (t) != PARM_DECL
1127 && (!DECL_INITIAL (t)
1128 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1129 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1130 flags &= ~TFF_UNQUALIFIED_NAME;
1131 if ((flags & TFF_DECL_SPECIFIERS)
1132 && DECL_TEMPLATE_PARM_P (t)
1133 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1134 pp_string (pp, "...");
1135 if (DECL_NAME (t))
1137 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1139 pp_less (pp);
1140 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1141 pp_string (pp, " capture>");
1143 else
1144 dump_decl (pp, DECL_NAME (t), flags);
1146 else if (DECL_DECOMPOSITION_P (t))
1147 pp_string (pp, M_("<structured bindings>"));
1148 else
1149 pp_string (pp, M_("<anonymous>"));
1151 dump_module_suffix (pp, t);
1153 if (flags & TFF_DECL_SPECIFIERS)
1154 dump_type_suffix (pp, type, flags);
1157 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1159 static void
1160 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1162 /* These special cases are duplicated here so that other functions
1163 can feed identifiers to error and get them demangled properly. */
1164 if (IDENTIFIER_CONV_OP_P (t))
1166 pp_cxx_ws_string (pp, "operator");
1167 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1168 dump_type (pp, TREE_TYPE (t), flags);
1169 return;
1171 if (dguide_name_p (t))
1173 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1174 TFF_UNQUALIFIED_NAME);
1175 return;
1178 const char *str = IDENTIFIER_POINTER (t);
1179 if (startswith (str, "_ZGR"))
1181 pp_cxx_ws_string (pp, "<temporary>");
1182 return;
1185 pp_cxx_tree_identifier (pp, t);
1188 /* Dump a human readable string for the decl T under control of FLAGS. */
1190 static void
1191 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1193 if (t == NULL_TREE)
1194 return;
1196 /* If doing Objective-C++, give Objective-C a chance to demangle
1197 Objective-C method names. */
1198 if (c_dialect_objc ())
1200 const char *demangled = objc_maybe_printable_name (t, flags);
1201 if (demangled)
1203 pp_string (pp, demangled);
1204 return;
1208 switch (TREE_CODE (t))
1210 case TYPE_DECL:
1211 /* Don't say 'typedef class A' */
1212 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1214 if ((flags & TFF_DECL_SPECIFIERS)
1215 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1217 /* Say `class T' not just `T'. */
1218 pp_cxx_ws_string (pp, "class");
1220 /* Emit the `...' for a parameter pack. */
1221 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1222 pp_cxx_ws_string (pp, "...");
1225 dump_type (pp, TREE_TYPE (t), flags);
1226 break;
1228 if (TYPE_DECL_ALIAS_P (t)
1229 && (flags & TFF_DECL_SPECIFIERS
1230 || flags & TFF_CLASS_KEY_OR_ENUM))
1232 pp_cxx_ws_string (pp, "using");
1233 dump_decl (pp, DECL_NAME (t), flags);
1234 pp_cxx_whitespace (pp);
1235 pp_cxx_ws_string (pp, "=");
1236 pp_cxx_whitespace (pp);
1237 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1238 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1239 flags);
1240 break;
1242 if ((flags & TFF_DECL_SPECIFIERS)
1243 && !DECL_SELF_REFERENCE_P (t))
1244 pp_cxx_ws_string (pp, "typedef");
1245 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1246 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1247 flags);
1248 break;
1250 case VAR_DECL:
1251 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1253 pp_string (pp, M_("vtable for "));
1254 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1255 dump_type (pp, DECL_CONTEXT (t), flags);
1256 break;
1258 /* Fall through. */
1259 case FIELD_DECL:
1260 case PARM_DECL:
1261 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1263 /* Handle variable template specializations. */
1264 if (VAR_P (t)
1265 && DECL_LANG_SPECIFIC (t)
1266 && DECL_TEMPLATE_INFO (t)
1267 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1269 pp_cxx_begin_template_argument_list (pp);
1270 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1271 dump_template_argument_list (pp, args, flags);
1272 pp_cxx_end_template_argument_list (pp);
1274 break;
1276 case RESULT_DECL:
1277 pp_string (pp, M_("<return value> "));
1278 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1279 break;
1281 case NAMESPACE_DECL:
1282 if (flags & TFF_DECL_SPECIFIERS)
1283 pp->declaration (t);
1284 else
1286 if (! (flags & TFF_UNQUALIFIED_NAME))
1287 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1288 flags &= ~TFF_UNQUALIFIED_NAME;
1289 if (DECL_NAME (t) == NULL_TREE)
1291 if (!(pp->flags & pp_c_flag_gnu_v3))
1292 pp_cxx_ws_string (pp, M_("{anonymous}"));
1293 else
1294 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1296 else
1297 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1299 break;
1301 case SCOPE_REF:
1302 dump_type (pp, TREE_OPERAND (t, 0), flags);
1303 pp_cxx_colon_colon (pp);
1304 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1305 break;
1307 case ARRAY_REF:
1308 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1309 pp_cxx_left_bracket (pp);
1310 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1311 pp_cxx_right_bracket (pp);
1312 break;
1314 /* So that we can do dump_decl on an aggr type. */
1315 case RECORD_TYPE:
1316 case UNION_TYPE:
1317 case ENUMERAL_TYPE:
1318 dump_type (pp, t, flags);
1319 break;
1321 case BIT_NOT_EXPR:
1322 /* This is a pseudo destructor call which has not been folded into
1323 a PSEUDO_DTOR_EXPR yet. */
1324 pp_cxx_complement (pp);
1325 dump_type (pp, TREE_OPERAND (t, 0), flags);
1326 break;
1328 case TYPE_EXPR:
1329 gcc_unreachable ();
1330 break;
1332 case IDENTIFIER_NODE:
1333 dump_decl_name (pp, t, flags);
1334 break;
1336 case OVERLOAD:
1337 if (!OVL_SINGLE_P (t))
1339 tree ctx = ovl_scope (t);
1340 if (ctx != global_namespace)
1342 if (TYPE_P (ctx))
1343 dump_type (pp, ctx, flags);
1344 else
1345 dump_decl (pp, ctx, flags);
1346 pp_cxx_colon_colon (pp);
1348 dump_decl (pp, OVL_NAME (t), flags);
1349 break;
1352 /* If there's only one function, just treat it like an ordinary
1353 FUNCTION_DECL. */
1354 t = OVL_FIRST (t);
1355 /* Fall through. */
1357 case FUNCTION_DECL:
1358 if (! DECL_LANG_SPECIFIC (t))
1360 if (DECL_ABSTRACT_ORIGIN (t)
1361 && DECL_ABSTRACT_ORIGIN (t) != t)
1362 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1363 else
1364 dump_function_name (pp, t, flags);
1366 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1367 dump_global_iord (pp, t);
1368 else
1369 dump_function_decl (pp, t, flags);
1370 break;
1372 case TEMPLATE_DECL:
1373 dump_template_decl (pp, t, flags);
1374 break;
1376 case CONCEPT_DECL:
1377 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1378 break;
1380 case WILDCARD_DECL:
1381 pp_string (pp, "<wildcard>");
1382 break;
1384 case TEMPLATE_ID_EXPR:
1386 tree name = TREE_OPERAND (t, 0);
1387 tree args = TREE_OPERAND (t, 1);
1389 if (!identifier_p (name))
1390 name = OVL_NAME (name);
1391 dump_decl (pp, name, flags);
1392 pp_cxx_begin_template_argument_list (pp);
1393 if (args == error_mark_node)
1394 pp_string (pp, M_("<template arguments error>"));
1395 else if (args)
1396 dump_template_argument_list
1397 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1398 pp_cxx_end_template_argument_list (pp);
1400 break;
1402 case LABEL_DECL:
1403 if (DECL_NAME (t))
1404 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1405 else
1406 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1407 break;
1409 case CONST_DECL:
1410 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1411 || (DECL_INITIAL (t) &&
1412 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1413 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1414 else if (DECL_NAME (t))
1415 dump_decl (pp, DECL_NAME (t), flags);
1416 else if (DECL_INITIAL (t))
1417 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1418 else
1419 pp_string (pp, M_("<enumerator>"));
1420 break;
1422 case USING_DECL:
1424 pp_cxx_ws_string (pp, "using");
1425 tree scope = USING_DECL_SCOPE (t);
1426 bool variadic = false;
1427 if (PACK_EXPANSION_P (scope))
1429 scope = PACK_EXPANSION_PATTERN (scope);
1430 variadic = true;
1432 dump_type (pp, scope, flags);
1433 pp_cxx_colon_colon (pp);
1434 dump_decl (pp, DECL_NAME (t), flags);
1435 if (variadic)
1436 pp_cxx_ws_string (pp, "...");
1438 break;
1440 case STATIC_ASSERT:
1441 pp->declaration (t);
1442 break;
1444 case BASELINK:
1445 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1446 break;
1448 case NON_DEPENDENT_EXPR:
1449 dump_expr (pp, t, flags);
1450 break;
1452 case TEMPLATE_TYPE_PARM:
1453 if (flags & TFF_DECL_SPECIFIERS)
1454 pp->declaration (t);
1455 else
1456 pp->type_id (t);
1457 break;
1459 case UNBOUND_CLASS_TEMPLATE:
1460 case TYPE_PACK_EXPANSION:
1461 case TREE_BINFO:
1462 dump_type (pp, t, flags);
1463 break;
1465 default:
1466 pp_unsupported_tree (pp, t);
1467 /* Fall through. */
1469 case ERROR_MARK:
1470 pp_string (pp, M_("<declaration error>"));
1471 break;
1475 /* Dump a template declaration T under control of FLAGS. This means the
1476 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1478 static void
1479 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1481 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1482 tree parms;
1483 int i;
1485 if (flags & TFF_TEMPLATE_HEADER)
1487 for (parms = orig_parms = nreverse (orig_parms);
1488 parms;
1489 parms = TREE_CHAIN (parms))
1491 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1492 int len = TREE_VEC_LENGTH (inner_parms);
1494 if (len == 0)
1496 /* Skip over the dummy template levels of a template template
1497 parm. */
1498 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1499 continue;
1502 pp_cxx_ws_string (pp, "template");
1503 pp_cxx_begin_template_argument_list (pp);
1505 /* If we've shown the template prefix, we'd better show the
1506 parameters' and decl's type too. */
1507 flags |= TFF_DECL_SPECIFIERS;
1509 for (i = 0; i < len; i++)
1511 if (i)
1512 pp_separate_with_comma (pp);
1513 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1514 flags);
1516 pp_cxx_end_template_argument_list (pp);
1517 pp_cxx_whitespace (pp);
1519 nreverse(orig_parms);
1521 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1523 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1524 pp_cxx_ws_string (pp, "class");
1526 /* If this is a parameter pack, print the ellipsis. */
1527 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1528 pp_cxx_ws_string (pp, "...");
1531 /* Only print the requirements if we're also printing
1532 the template header. */
1533 if (flag_concepts)
1534 if (tree ci = get_constraints (t))
1535 if (check_constraint_info (ci))
1536 if (tree reqs = CI_TEMPLATE_REQS (ci))
1538 pp_cxx_requires_clause (pp, reqs);
1539 pp_cxx_whitespace (pp);
1544 if (DECL_CLASS_TEMPLATE_P (t))
1545 dump_type (pp, TREE_TYPE (t),
1546 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1547 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1548 else if (DECL_TEMPLATE_RESULT (t)
1549 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1550 /* Alias template. */
1551 || DECL_TYPE_TEMPLATE_P (t)
1552 /* Concept definition. &*/
1553 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1554 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1555 else
1557 gcc_assert (TREE_TYPE (t));
1558 switch (NEXT_CODE (t))
1560 case METHOD_TYPE:
1561 case FUNCTION_TYPE:
1562 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1563 break;
1564 default:
1565 /* This case can occur with some invalid code. */
1566 dump_type (pp, TREE_TYPE (t),
1567 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1568 | (flags & TFF_DECL_SPECIFIERS
1569 ? TFF_CLASS_KEY_OR_ENUM : 0));
1574 /* find_typenames looks through the type of the function template T
1575 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1576 it finds. */
1578 struct find_typenames_t
1580 hash_set<tree> *p_set;
1581 vec<tree, va_gc> *typenames;
1584 static tree
1585 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1587 struct find_typenames_t *d = (struct find_typenames_t *)data;
1588 tree mv = NULL_TREE;
1590 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1591 /* Add the type of the typedef without any additional cv-quals. */
1592 mv = TREE_TYPE (TYPE_NAME (*tp));
1593 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1594 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1595 /* Add the typename without any cv-qualifiers. */
1596 mv = TYPE_MAIN_VARIANT (*tp);
1598 if (PACK_EXPANSION_P (*tp))
1600 /* Don't mess with parameter packs since we don't remember
1601 the pack expansion context for a particular typename. */
1602 *walk_subtrees = false;
1603 return NULL_TREE;
1606 if (mv && (mv == *tp || !d->p_set->add (mv)))
1607 vec_safe_push (d->typenames, mv);
1609 return NULL_TREE;
1612 static vec<tree, va_gc> *
1613 find_typenames (tree t)
1615 struct find_typenames_t ft;
1616 ft.p_set = new hash_set<tree>;
1617 ft.typenames = NULL;
1618 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1619 find_typenames_r, &ft, ft.p_set);
1620 delete ft.p_set;
1621 return ft.typenames;
1624 /* Output the "[with ...]" clause for a template instantiation T iff
1625 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1626 formatting a deduction/substitution diagnostic rather than an
1627 instantiation. */
1629 static void
1630 dump_substitution (cxx_pretty_printer *pp,
1631 tree t, tree template_parms, tree template_args,
1632 int flags)
1634 if (template_parms != NULL_TREE && template_args != NULL_TREE
1635 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1637 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1638 dump_template_bindings (pp, template_parms, template_args, typenames);
1642 /* Dump the lambda function FN including its 'mutable' qualifier and any
1643 template bindings. */
1645 static void
1646 dump_lambda_function (cxx_pretty_printer *pp,
1647 tree fn, tree template_parms, tree template_args,
1648 int flags)
1650 /* A lambda's signature is essentially its "type". */
1651 dump_type (pp, DECL_CONTEXT (fn), flags);
1652 if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn))) & TYPE_QUAL_CONST))
1654 pp->padding = pp_before;
1655 pp_c_ws_string (pp, "mutable");
1657 dump_substitution (pp, fn, template_parms, template_args, flags);
1660 /* Pretty print a function decl. There are several ways we want to print a
1661 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1662 As error can only apply the '#' flag once to give 0 and 1 for V, there
1663 is %D which doesn't print the throw specs, and %F which does. */
1665 static void
1666 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1668 tree fntype;
1669 tree parmtypes;
1670 tree cname = NULL_TREE;
1671 tree template_args = NULL_TREE;
1672 tree template_parms = NULL_TREE;
1673 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1674 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1675 tree exceptions;
1676 bool constexpr_p;
1677 tree ret = NULL_TREE;
1679 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1680 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1681 if (TREE_CODE (t) == TEMPLATE_DECL)
1682 t = DECL_TEMPLATE_RESULT (t);
1684 /* Save the exceptions, in case t is a specialization and we are
1685 emitting an error about incompatible specifications. */
1686 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1688 /* Likewise for the constexpr specifier, in case t is a specialization. */
1689 constexpr_p = DECL_DECLARED_CONSTEXPR_P (t);
1691 /* Pretty print template instantiations only. */
1692 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1693 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1694 && flag_pretty_templates)
1696 tree tmpl;
1698 template_args = DECL_TI_ARGS (t);
1699 tmpl = most_general_template (t);
1700 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1702 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1703 t = tmpl;
1707 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1708 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1710 fntype = TREE_TYPE (t);
1711 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1713 if (DECL_CLASS_SCOPE_P (t))
1714 cname = DECL_CONTEXT (t);
1715 /* This is for partially instantiated template methods. */
1716 else if (TREE_CODE (fntype) == METHOD_TYPE)
1717 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1719 if (flags & TFF_DECL_SPECIFIERS)
1721 if (DECL_STATIC_FUNCTION_P (t))
1722 pp_cxx_ws_string (pp, "static");
1723 else if (DECL_VIRTUAL_P (t))
1724 pp_cxx_ws_string (pp, "virtual");
1726 if (constexpr_p)
1728 if (DECL_DECLARED_CONCEPT_P (t))
1729 pp_cxx_ws_string (pp, "concept");
1730 else if (DECL_IMMEDIATE_FUNCTION_P (t))
1731 pp_cxx_ws_string (pp, "consteval");
1732 else
1733 pp_cxx_ws_string (pp, "constexpr");
1737 /* Print the return type? */
1738 if (show_return)
1739 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1740 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1741 if (show_return)
1743 ret = fndecl_declared_return_type (t);
1744 dump_type_prefix (pp, ret, flags);
1747 /* Print the function name. */
1748 if (!do_outer_scope)
1749 /* Nothing. */;
1750 else if (cname)
1752 dump_type (pp, cname, flags);
1753 pp_cxx_colon_colon (pp);
1755 else
1756 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1758 dump_function_name (pp, t, dump_function_name_flags);
1760 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1762 dump_parameters (pp, parmtypes, flags);
1764 if (TREE_CODE (fntype) == METHOD_TYPE)
1766 pp->padding = pp_before;
1767 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1768 dump_ref_qualifier (pp, fntype, flags);
1771 if (tx_safe_fn_type_p (fntype))
1773 pp->padding = pp_before;
1774 pp_cxx_ws_string (pp, "transaction_safe");
1777 if (flags & TFF_EXCEPTION_SPECIFICATION)
1779 pp->padding = pp_before;
1780 dump_exception_spec (pp, exceptions, flags);
1783 if (show_return)
1784 dump_type_suffix (pp, ret, flags);
1785 else if (deduction_guide_p (t))
1787 pp_cxx_ws_string (pp, "->");
1788 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1791 if (flag_concepts)
1792 if (tree ci = get_constraints (t))
1793 if (tree reqs = CI_DECLARATOR_REQS (ci))
1794 pp_cxx_requires_clause (pp, reqs);
1796 dump_substitution (pp, t, template_parms, template_args, flags);
1798 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1800 pp_cxx_ws_string (pp, "[inherited from");
1801 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1802 pp_character (pp, ']');
1805 else if (template_args)
1807 bool need_comma = false;
1808 int i;
1809 pp_cxx_begin_template_argument_list (pp);
1810 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1811 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1813 tree arg = TREE_VEC_ELT (template_args, i);
1814 if (need_comma)
1815 pp_separate_with_comma (pp);
1816 if (ARGUMENT_PACK_P (arg))
1817 pp_cxx_left_brace (pp);
1818 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1819 if (ARGUMENT_PACK_P (arg))
1820 pp_cxx_right_brace (pp);
1821 need_comma = true;
1823 pp_cxx_end_template_argument_list (pp);
1827 /* Print a parameter list. If this is for a member function, the
1828 member object ptr (and any other hidden args) should have
1829 already been removed. */
1831 static void
1832 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1834 int first = 1;
1835 flags &= ~TFF_SCOPE;
1836 pp_cxx_left_paren (pp);
1838 for (first = 1; parmtypes != void_list_node;
1839 parmtypes = TREE_CHAIN (parmtypes))
1841 if (!first)
1842 pp_separate_with_comma (pp);
1843 first = 0;
1844 if (!parmtypes)
1846 pp_cxx_ws_string (pp, "...");
1847 break;
1850 dump_type (pp, TREE_VALUE (parmtypes), flags);
1852 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1854 pp_cxx_whitespace (pp);
1855 pp_equal (pp);
1856 pp_cxx_whitespace (pp);
1857 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1861 pp_cxx_right_paren (pp);
1864 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1866 static void
1867 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1869 if (FUNCTION_REF_QUALIFIED (t))
1871 pp->padding = pp_before;
1872 if (FUNCTION_RVALUE_QUALIFIED (t))
1873 pp_cxx_ws_string (pp, "&&");
1874 else
1875 pp_cxx_ws_string (pp, "&");
1879 /* Print an exception specification. T is the exception specification. */
1881 static void
1882 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1884 if (t && TREE_PURPOSE (t))
1886 pp_cxx_ws_string (pp, "noexcept");
1887 if (!integer_onep (TREE_PURPOSE (t)))
1889 pp_cxx_whitespace (pp);
1890 pp_cxx_left_paren (pp);
1891 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1892 pp_cxx_ws_string (pp, "<uninstantiated>");
1893 else
1894 dump_expr (pp, TREE_PURPOSE (t), flags);
1895 pp_cxx_right_paren (pp);
1898 else if (t)
1900 pp_cxx_ws_string (pp, "throw");
1901 pp_cxx_whitespace (pp);
1902 pp_cxx_left_paren (pp);
1903 if (TREE_VALUE (t) != NULL_TREE)
1904 while (1)
1906 dump_type (pp, TREE_VALUE (t), flags);
1907 t = TREE_CHAIN (t);
1908 if (!t)
1909 break;
1910 pp_separate_with_comma (pp);
1912 pp_cxx_right_paren (pp);
1916 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1917 and destructors properly. */
1919 static void
1920 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1922 tree name = DECL_NAME (t);
1924 /* We can get here with a decl that was synthesized by language-
1925 independent machinery (e.g. coverage.c) in which case it won't
1926 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1927 will crash. In this case it is safe just to print out the
1928 literal name. */
1929 if (!DECL_LANG_SPECIFIC (t))
1931 pp_cxx_tree_identifier (pp, name);
1932 return;
1935 if (TREE_CODE (t) == TEMPLATE_DECL)
1936 t = DECL_TEMPLATE_RESULT (t);
1938 /* Don't let the user see __comp_ctor et al. */
1939 if (DECL_CONSTRUCTOR_P (t)
1940 || DECL_DESTRUCTOR_P (t))
1942 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1943 name = get_identifier ("<lambda>");
1944 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
1945 name = get_identifier ("<constructor>");
1946 else
1947 name = constructor_name (DECL_CONTEXT (t));
1950 if (DECL_DESTRUCTOR_P (t))
1952 pp_cxx_complement (pp);
1953 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1955 else if (DECL_CONV_FN_P (t))
1957 /* This cannot use the hack that the operator's return
1958 type is stashed off of its name because it may be
1959 used for error reporting. In the case of conflicting
1960 declarations, both will have the same name, yet
1961 the types will be different, hence the TREE_TYPE field
1962 of the first name will be clobbered by the second. */
1963 pp_cxx_ws_string (pp, "operator");
1964 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1966 else
1967 dump_decl (pp, name, flags);
1969 dump_module_suffix (pp, t);
1971 if (DECL_TEMPLATE_INFO (t)
1972 && !(flags & TFF_TEMPLATE_NAME)
1973 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1974 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1975 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1976 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1977 flags);
1980 /* Dump the template parameters from the template info INFO under control of
1981 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1982 specialization (partial or complete). For partial specializations we show
1983 the specialized parameter values. For a primary template we show no
1984 decoration. */
1986 static void
1987 dump_template_parms (cxx_pretty_printer *pp, tree info,
1988 int primary, int flags)
1990 tree args = info ? TI_ARGS (info) : NULL_TREE;
1992 if (primary && flags & TFF_TEMPLATE_NAME)
1993 return;
1994 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1995 pp_cxx_begin_template_argument_list (pp);
1997 /* Be careful only to print things when we have them, so as not
1998 to crash producing error messages. */
1999 if (args && !primary)
2001 int len, ix;
2002 len = get_non_default_template_args_count (args, flags);
2004 args = INNERMOST_TEMPLATE_ARGS (args);
2005 for (ix = 0; ix != len; ix++)
2007 tree arg = TREE_VEC_ELT (args, ix);
2009 /* Only print a comma if we know there is an argument coming. In
2010 the case of an empty template argument pack, no actual
2011 argument will be printed. */
2012 if (ix
2013 && (!ARGUMENT_PACK_P (arg)
2014 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2015 pp_separate_with_comma (pp);
2017 if (!arg)
2018 pp_string (pp, M_("<template parameter error>"));
2019 else
2020 dump_template_argument (pp, arg, flags);
2023 else if (primary)
2025 tree tpl = TI_TEMPLATE (info);
2026 tree parms = DECL_TEMPLATE_PARMS (tpl);
2027 int len, ix;
2029 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2030 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2032 for (ix = 0; ix != len; ix++)
2034 tree parm;
2036 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2038 pp_string (pp, M_("<template parameter error>"));
2039 continue;
2042 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2044 if (ix)
2045 pp_separate_with_comma (pp);
2047 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2050 pp_cxx_end_template_argument_list (pp);
2053 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2054 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2056 static void
2057 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2059 tree arg;
2060 call_expr_arg_iterator iter;
2062 pp_cxx_left_paren (pp);
2063 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
2065 if (skipfirst)
2066 skipfirst = false;
2067 else
2069 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2070 if (more_call_expr_args_p (&iter))
2071 pp_separate_with_comma (pp);
2074 pp_cxx_right_paren (pp);
2077 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
2078 using flags FLAGS. Skip over the first argument if SKIPFIRST is
2079 true. */
2081 static void
2082 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
2083 bool skipfirst)
2085 tree arg;
2086 aggr_init_expr_arg_iterator iter;
2088 pp_cxx_left_paren (pp);
2089 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
2091 if (skipfirst)
2092 skipfirst = false;
2093 else
2095 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2096 if (more_aggr_init_expr_args_p (&iter))
2097 pp_separate_with_comma (pp);
2100 pp_cxx_right_paren (pp);
2103 /* Print out a list of initializers (subr of dump_expr). */
2105 static void
2106 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2108 while (l)
2110 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2111 l = TREE_CHAIN (l);
2112 if (l)
2113 pp_separate_with_comma (pp);
2117 /* Print out a vector of initializers (subr of dump_expr). */
2119 static void
2120 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2121 int flags)
2123 unsigned HOST_WIDE_INT idx;
2124 tree value;
2126 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2128 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2129 if (idx != v->length () - 1)
2130 pp_separate_with_comma (pp);
2135 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2136 function. Resolve it to a close relative -- in the sense of static
2137 type -- variant being overridden. That is close to what was written in
2138 the source code. Subroutine of dump_expr. */
2140 static tree
2141 resolve_virtual_fun_from_obj_type_ref (tree ref)
2143 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
2144 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2145 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2146 while (index)
2148 fun = TREE_CHAIN (fun);
2149 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2150 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2153 return BV_FN (fun);
2156 /* Print out an expression E under control of FLAGS. */
2158 static void
2159 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2161 tree op;
2163 if (t == 0)
2164 return;
2166 if (STATEMENT_CLASS_P (t))
2168 pp_cxx_ws_string (pp, M_("<statement>"));
2169 return;
2172 switch (TREE_CODE (t))
2174 case VAR_DECL:
2175 case PARM_DECL:
2176 case FIELD_DECL:
2177 case CONST_DECL:
2178 case FUNCTION_DECL:
2179 case TEMPLATE_DECL:
2180 case NAMESPACE_DECL:
2181 case LABEL_DECL:
2182 case WILDCARD_DECL:
2183 case OVERLOAD:
2184 case TYPE_DECL:
2185 case IDENTIFIER_NODE:
2186 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2187 |TFF_TEMPLATE_HEADER))
2188 | TFF_NO_TEMPLATE_BINDINGS
2189 | TFF_NO_FUNCTION_ARGUMENTS));
2190 break;
2192 case SSA_NAME:
2193 if (SSA_NAME_VAR (t)
2194 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2195 dump_expr (pp, SSA_NAME_VAR (t), flags);
2196 else
2197 pp_cxx_ws_string (pp, M_("<unknown>"));
2198 break;
2200 case VOID_CST:
2201 case INTEGER_CST:
2202 case REAL_CST:
2203 case STRING_CST:
2204 case COMPLEX_CST:
2205 pp->constant (t);
2206 break;
2208 case USERDEF_LITERAL:
2209 pp_cxx_userdef_literal (pp, t);
2210 break;
2212 case THROW_EXPR:
2213 /* While waiting for caret diagnostics, avoid printing
2214 __cxa_allocate_exception, __cxa_throw, and the like. */
2215 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2216 break;
2218 case PTRMEM_CST:
2219 pp_ampersand (pp);
2220 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2221 pp_cxx_colon_colon (pp);
2222 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2223 break;
2225 case COMPOUND_EXPR:
2226 pp_cxx_left_paren (pp);
2227 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2228 pp_separate_with_comma (pp);
2229 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2230 pp_cxx_right_paren (pp);
2231 break;
2233 case COND_EXPR:
2234 case VEC_COND_EXPR:
2235 pp_cxx_left_paren (pp);
2236 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2237 pp_string (pp, " ? ");
2238 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2239 pp_string (pp, " : ");
2240 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2241 pp_cxx_right_paren (pp);
2242 break;
2244 case SAVE_EXPR:
2245 if (TREE_HAS_CONSTRUCTOR (t))
2247 pp_cxx_ws_string (pp, "new");
2248 pp_cxx_whitespace (pp);
2249 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2251 else
2252 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2253 break;
2255 case AGGR_INIT_EXPR:
2257 tree fn = NULL_TREE;
2259 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
2260 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
2262 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
2264 if (DECL_CONSTRUCTOR_P (fn))
2265 dump_type (pp, DECL_CONTEXT (fn), flags);
2266 else
2267 dump_decl (pp, fn, 0);
2269 else
2270 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
2272 dump_aggr_init_expr_args (pp, t, flags, true);
2273 break;
2275 case CALL_EXPR:
2277 tree fn = CALL_EXPR_FN (t);
2278 bool skipfirst = false;
2280 /* Deal with internal functions. */
2281 if (fn == NULL_TREE)
2283 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2284 dump_call_expr_args (pp, t, flags, skipfirst);
2285 break;
2288 if (TREE_CODE (fn) == ADDR_EXPR)
2289 fn = TREE_OPERAND (fn, 0);
2291 /* Nobody is interested in seeing the guts of vcalls. */
2292 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2293 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2295 if (TREE_TYPE (fn) != NULL_TREE
2296 && NEXT_CODE (fn) == METHOD_TYPE
2297 && call_expr_nargs (t))
2299 tree ob = CALL_EXPR_ARG (t, 0);
2300 if (TREE_CODE (ob) == ADDR_EXPR)
2302 dump_expr (pp, TREE_OPERAND (ob, 0),
2303 flags | TFF_EXPR_IN_PARENS);
2304 pp_cxx_dot (pp);
2306 else if (!is_this_parameter (ob))
2308 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2309 pp_cxx_arrow (pp);
2311 skipfirst = true;
2313 if (flag_sanitize & SANITIZE_UNDEFINED
2314 && is_ubsan_builtin_p (fn))
2316 pp_string (cxx_pp, M_("<ubsan routine call>"));
2317 break;
2319 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2320 dump_call_expr_args (pp, t, flags, skipfirst);
2322 break;
2324 case TARGET_EXPR:
2325 /* Note that this only works for G++ target exprs. If somebody
2326 builds a general TARGET_EXPR, there's no way to represent that
2327 it initializes anything other that the parameter slot for the
2328 default argument. Note we may have cleared out the first
2329 operand in expand_expr, so don't go killing ourselves. */
2330 if (TREE_OPERAND (t, 1))
2331 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2332 break;
2334 case POINTER_PLUS_EXPR:
2335 dump_binary_op (pp, "+", t, flags);
2336 break;
2338 case POINTER_DIFF_EXPR:
2339 dump_binary_op (pp, "-", t, flags);
2340 break;
2342 case INIT_EXPR:
2343 case MODIFY_EXPR:
2344 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2345 break;
2347 case PLUS_EXPR:
2348 case MINUS_EXPR:
2349 case MULT_EXPR:
2350 case TRUNC_DIV_EXPR:
2351 case TRUNC_MOD_EXPR:
2352 case MIN_EXPR:
2353 case MAX_EXPR:
2354 case LSHIFT_EXPR:
2355 case RSHIFT_EXPR:
2356 case BIT_IOR_EXPR:
2357 case BIT_XOR_EXPR:
2358 case BIT_AND_EXPR:
2359 case TRUTH_ANDIF_EXPR:
2360 case TRUTH_ORIF_EXPR:
2361 case LT_EXPR:
2362 case LE_EXPR:
2363 case GT_EXPR:
2364 case GE_EXPR:
2365 case EQ_EXPR:
2366 case NE_EXPR:
2367 case SPACESHIP_EXPR:
2368 case EXACT_DIV_EXPR:
2369 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2370 break;
2372 case CEIL_DIV_EXPR:
2373 case FLOOR_DIV_EXPR:
2374 case ROUND_DIV_EXPR:
2375 case RDIV_EXPR:
2376 dump_binary_op (pp, "/", t, flags);
2377 break;
2379 case CEIL_MOD_EXPR:
2380 case FLOOR_MOD_EXPR:
2381 case ROUND_MOD_EXPR:
2382 dump_binary_op (pp, "%", t, flags);
2383 break;
2385 case COMPONENT_REF:
2387 tree ob = TREE_OPERAND (t, 0);
2388 if (INDIRECT_REF_P (ob))
2390 ob = TREE_OPERAND (ob, 0);
2391 if (!is_this_parameter (ob)
2392 && !is_dummy_object (ob))
2394 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2395 if (TYPE_REF_P (TREE_TYPE (ob)))
2396 pp_cxx_dot (pp);
2397 else
2398 pp_cxx_arrow (pp);
2401 else
2403 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2404 if (TREE_CODE (ob) != ARROW_EXPR)
2405 pp_cxx_dot (pp);
2407 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2409 break;
2411 case ARRAY_REF:
2412 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2413 pp_cxx_left_bracket (pp);
2414 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2415 pp_cxx_right_bracket (pp);
2416 break;
2418 case UNARY_PLUS_EXPR:
2419 dump_unary_op (pp, "+", t, flags);
2420 break;
2422 case ADDR_EXPR:
2423 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2424 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2425 /* An ADDR_EXPR can have reference type. In that case, we
2426 shouldn't print the `&' doing so indicates to the user
2427 that the expression has pointer type. */
2428 || (TREE_TYPE (t)
2429 && TYPE_REF_P (TREE_TYPE (t))))
2430 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2431 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2432 dump_unary_op (pp, "&&", t, flags);
2433 else
2434 dump_unary_op (pp, "&", t, flags);
2435 break;
2437 case INDIRECT_REF:
2438 if (TREE_HAS_CONSTRUCTOR (t))
2440 t = TREE_OPERAND (t, 0);
2441 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2442 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2443 dump_call_expr_args (pp, t, flags, true);
2445 else
2447 if (TREE_OPERAND (t,0) != NULL_TREE
2448 && TREE_TYPE (TREE_OPERAND (t, 0))
2449 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2450 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2451 else
2452 dump_unary_op (pp, "*", t, flags);
2454 break;
2456 case MEM_REF:
2457 /* Delegate to the base "C" pretty printer. */
2458 pp->c_pretty_printer::unary_expression (t);
2459 break;
2461 case TARGET_MEM_REF:
2462 /* TARGET_MEM_REF can't appear directly from source, but can appear
2463 during late GIMPLE optimizations and through late diagnostic we might
2464 need to support it. Print it as dereferencing of a pointer after
2465 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2466 pointer to single byte types, so
2467 *(type *)((char *) ptr + step * index + index2) if all the operands
2468 are present and the casts are needed. */
2469 pp_cxx_star (pp);
2470 pp_cxx_left_paren (pp);
2471 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2472 || !integer_onep (TYPE_SIZE_UNIT
2473 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2475 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2476 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2478 pp_cxx_left_paren (pp);
2479 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2481 else
2483 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2484 pp_cxx_right_paren (pp);
2485 pp_cxx_left_paren (pp);
2486 pp_cxx_left_paren (pp);
2487 dump_type (pp, build_pointer_type (char_type_node), flags);
2489 pp_cxx_right_paren (pp);
2491 else if (!same_type_p (TREE_TYPE (t),
2492 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2494 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2495 pp_cxx_right_paren (pp);
2496 pp_cxx_left_paren (pp);
2498 dump_expr (pp, TMR_BASE (t), flags);
2499 if (TMR_STEP (t) && TMR_INDEX (t))
2501 pp_cxx_ws_string (pp, "+");
2502 dump_expr (pp, TMR_INDEX (t), flags);
2503 pp_cxx_ws_string (pp, "*");
2504 dump_expr (pp, TMR_STEP (t), flags);
2506 if (TMR_INDEX2 (t))
2508 pp_cxx_ws_string (pp, "+");
2509 dump_expr (pp, TMR_INDEX2 (t), flags);
2511 if (!integer_zerop (TMR_OFFSET (t)))
2513 pp_cxx_ws_string (pp, "+");
2514 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2516 pp_cxx_right_paren (pp);
2517 break;
2519 case NEGATE_EXPR:
2520 case BIT_NOT_EXPR:
2521 case TRUTH_NOT_EXPR:
2522 case PREDECREMENT_EXPR:
2523 case PREINCREMENT_EXPR:
2524 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2525 break;
2527 case POSTDECREMENT_EXPR:
2528 case POSTINCREMENT_EXPR:
2529 pp_cxx_left_paren (pp);
2530 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2531 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2532 pp_cxx_right_paren (pp);
2533 break;
2535 case NON_LVALUE_EXPR:
2536 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2537 should be another level of INDIRECT_REF so that I don't have to do
2538 this. */
2539 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2541 tree next = TREE_TYPE (TREE_TYPE (t));
2543 while (TYPE_PTR_P (next))
2544 next = TREE_TYPE (next);
2546 if (TREE_CODE (next) == FUNCTION_TYPE)
2548 if (flags & TFF_EXPR_IN_PARENS)
2549 pp_cxx_left_paren (pp);
2550 pp_cxx_star (pp);
2551 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2552 if (flags & TFF_EXPR_IN_PARENS)
2553 pp_cxx_right_paren (pp);
2554 break;
2556 /* Else fall through. */
2558 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2559 break;
2561 CASE_CONVERT:
2562 case IMPLICIT_CONV_EXPR:
2563 case VIEW_CONVERT_EXPR:
2565 tree op = TREE_OPERAND (t, 0);
2566 tree ttype = TREE_TYPE (t);
2567 tree optype = TREE_TYPE (op);
2569 if (TREE_CODE (ttype) != TREE_CODE (optype)
2570 && INDIRECT_TYPE_P (ttype)
2571 && INDIRECT_TYPE_P (optype)
2572 && same_type_p (TREE_TYPE (optype),
2573 TREE_TYPE (ttype)))
2575 if (TYPE_REF_P (ttype))
2577 STRIP_NOPS (op);
2578 if (TREE_CODE (op) == ADDR_EXPR)
2579 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2580 else
2581 dump_unary_op (pp, "*", t, flags);
2583 else
2584 dump_unary_op (pp, "&", t, flags);
2586 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2588 /* It is a cast, but we cannot tell whether it is a
2589 reinterpret or static cast. Use the C style notation. */
2590 if (flags & TFF_EXPR_IN_PARENS)
2591 pp_cxx_left_paren (pp);
2592 pp_cxx_left_paren (pp);
2593 dump_type (pp, TREE_TYPE (t), flags);
2594 pp_cxx_right_paren (pp);
2595 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2596 if (flags & TFF_EXPR_IN_PARENS)
2597 pp_cxx_right_paren (pp);
2599 else
2600 dump_expr (pp, op, flags);
2601 break;
2604 case CONSTRUCTOR:
2605 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2607 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2609 if (integer_zerop (idx))
2611 /* A NULL pointer-to-member constant. */
2612 pp_cxx_left_paren (pp);
2613 pp_cxx_left_paren (pp);
2614 dump_type (pp, TREE_TYPE (t), flags);
2615 pp_cxx_right_paren (pp);
2616 pp_character (pp, '0');
2617 pp_cxx_right_paren (pp);
2618 break;
2620 else if (tree_fits_shwi_p (idx))
2622 tree virtuals;
2623 unsigned HOST_WIDE_INT n;
2625 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2626 t = TYPE_METHOD_BASETYPE (t);
2627 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2629 n = tree_to_shwi (idx);
2631 /* Map vtable index back one, to allow for the null pointer to
2632 member. */
2633 --n;
2635 while (n > 0 && virtuals)
2637 --n;
2638 virtuals = TREE_CHAIN (virtuals);
2640 if (virtuals)
2642 dump_expr (pp, BV_FN (virtuals),
2643 flags | TFF_EXPR_IN_PARENS);
2644 break;
2648 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2649 pp_string (pp, "<lambda closure object>");
2650 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2652 dump_type (pp, TREE_TYPE (t), 0);
2653 pp_cxx_left_paren (pp);
2654 pp_cxx_right_paren (pp);
2656 else
2658 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2659 dump_type (pp, TREE_TYPE (t), 0);
2660 pp_cxx_left_brace (pp);
2661 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2662 pp_cxx_right_brace (pp);
2665 break;
2667 case OFFSET_REF:
2669 tree ob = TREE_OPERAND (t, 0);
2670 if (is_dummy_object (ob))
2672 t = TREE_OPERAND (t, 1);
2673 if (TREE_CODE (t) == FUNCTION_DECL)
2674 /* A::f */
2675 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2676 else if (BASELINK_P (t))
2677 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2678 flags | TFF_EXPR_IN_PARENS);
2679 else
2680 dump_decl (pp, t, flags);
2682 else
2684 if (INDIRECT_REF_P (ob))
2686 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2687 pp_cxx_arrow (pp);
2688 pp_cxx_star (pp);
2690 else
2692 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2693 pp_cxx_dot (pp);
2694 pp_cxx_star (pp);
2696 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2698 break;
2701 case TEMPLATE_PARM_INDEX:
2702 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2703 break;
2705 case CAST_EXPR:
2706 if (TREE_OPERAND (t, 0) == NULL_TREE
2707 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2709 dump_type (pp, TREE_TYPE (t), flags);
2710 pp_cxx_left_paren (pp);
2711 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2712 pp_cxx_right_paren (pp);
2714 else
2716 pp_cxx_left_paren (pp);
2717 dump_type (pp, TREE_TYPE (t), flags);
2718 pp_cxx_right_paren (pp);
2719 pp_cxx_left_paren (pp);
2720 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2721 pp_cxx_right_paren (pp);
2723 break;
2725 case STATIC_CAST_EXPR:
2726 pp_cxx_ws_string (pp, "static_cast");
2727 goto cast;
2728 case REINTERPRET_CAST_EXPR:
2729 pp_cxx_ws_string (pp, "reinterpret_cast");
2730 goto cast;
2731 case CONST_CAST_EXPR:
2732 pp_cxx_ws_string (pp, "const_cast");
2733 goto cast;
2734 case DYNAMIC_CAST_EXPR:
2735 pp_cxx_ws_string (pp, "dynamic_cast");
2736 cast:
2737 pp_cxx_begin_template_argument_list (pp);
2738 dump_type (pp, TREE_TYPE (t), flags);
2739 pp_cxx_end_template_argument_list (pp);
2740 pp_cxx_left_paren (pp);
2741 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2742 pp_cxx_right_paren (pp);
2743 break;
2745 case ARROW_EXPR:
2746 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2747 pp_cxx_arrow (pp);
2748 break;
2750 case SIZEOF_EXPR:
2751 case ALIGNOF_EXPR:
2752 if (TREE_CODE (t) == SIZEOF_EXPR)
2753 pp_cxx_ws_string (pp, "sizeof");
2754 else
2756 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2757 pp_cxx_ws_string (pp, "__alignof__");
2759 op = TREE_OPERAND (t, 0);
2760 if (PACK_EXPANSION_P (op))
2762 pp_string (pp, "...");
2763 op = PACK_EXPANSION_PATTERN (op);
2765 pp_cxx_whitespace (pp);
2766 pp_cxx_left_paren (pp);
2767 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2768 dump_type (pp, TREE_TYPE (op), flags);
2769 else if (TYPE_P (TREE_OPERAND (t, 0)))
2770 dump_type (pp, op, flags);
2771 else
2772 dump_expr (pp, op, flags);
2773 pp_cxx_right_paren (pp);
2774 break;
2776 case AT_ENCODE_EXPR:
2777 pp_cxx_ws_string (pp, "@encode");
2778 pp_cxx_whitespace (pp);
2779 pp_cxx_left_paren (pp);
2780 dump_type (pp, TREE_OPERAND (t, 0), flags);
2781 pp_cxx_right_paren (pp);
2782 break;
2784 case NOEXCEPT_EXPR:
2785 pp_cxx_ws_string (pp, "noexcept");
2786 pp_cxx_whitespace (pp);
2787 pp_cxx_left_paren (pp);
2788 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2789 pp_cxx_right_paren (pp);
2790 break;
2792 case REALPART_EXPR:
2793 case IMAGPART_EXPR:
2794 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2795 pp_cxx_whitespace (pp);
2796 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2797 break;
2799 case DEFERRED_PARSE:
2800 pp_string (pp, M_("<unparsed>"));
2801 break;
2803 case TRY_CATCH_EXPR:
2804 case CLEANUP_POINT_EXPR:
2805 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2806 break;
2808 case PSEUDO_DTOR_EXPR:
2809 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2810 pp_cxx_dot (pp);
2811 if (TREE_OPERAND (t, 1))
2813 dump_type (pp, TREE_OPERAND (t, 1), flags);
2814 pp_cxx_colon_colon (pp);
2816 pp_cxx_complement (pp);
2817 dump_type (pp, TREE_OPERAND (t, 2), flags);
2818 break;
2820 case TEMPLATE_ID_EXPR:
2821 dump_decl (pp, t, flags);
2822 break;
2824 case BIND_EXPR:
2825 case STMT_EXPR:
2826 case EXPR_STMT:
2827 case STATEMENT_LIST:
2828 /* We don't yet have a way of dumping statements in a
2829 human-readable format. */
2830 pp_string (pp, "({...})");
2831 break;
2833 case LOOP_EXPR:
2834 pp_string (pp, "while (1) { ");
2835 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2836 pp_cxx_right_brace (pp);
2837 break;
2839 case EXIT_EXPR:
2840 pp_string (pp, "if (");
2841 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2842 pp_string (pp, ") break; ");
2843 break;
2845 case BASELINK:
2846 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2847 break;
2849 case EMPTY_CLASS_EXPR:
2850 dump_type (pp, TREE_TYPE (t), flags);
2851 pp_cxx_left_paren (pp);
2852 pp_cxx_right_paren (pp);
2853 break;
2855 case NON_DEPENDENT_EXPR:
2856 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2857 break;
2859 case ARGUMENT_PACK_SELECT:
2860 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2861 break;
2863 case RECORD_TYPE:
2864 case UNION_TYPE:
2865 case ENUMERAL_TYPE:
2866 case REAL_TYPE:
2867 case VOID_TYPE:
2868 case OPAQUE_TYPE:
2869 case BOOLEAN_TYPE:
2870 case INTEGER_TYPE:
2871 case COMPLEX_TYPE:
2872 case VECTOR_TYPE:
2873 case DECLTYPE_TYPE:
2874 pp_type_specifier_seq (pp, t);
2875 break;
2877 case TYPENAME_TYPE:
2878 /* We get here when we want to print a dependent type as an
2879 id-expression, without any disambiguator decoration. */
2880 pp->id_expression (t);
2881 break;
2883 case TEMPLATE_TYPE_PARM:
2884 case TEMPLATE_TEMPLATE_PARM:
2885 case BOUND_TEMPLATE_TEMPLATE_PARM:
2886 dump_type (pp, t, flags);
2887 break;
2889 case TRAIT_EXPR:
2890 pp_cxx_trait_expression (pp, t);
2891 break;
2893 case VA_ARG_EXPR:
2894 pp_cxx_va_arg_expression (pp, t);
2895 break;
2897 case OFFSETOF_EXPR:
2898 pp_cxx_offsetof_expression (pp, t);
2899 break;
2901 case ADDRESSOF_EXPR:
2902 pp_cxx_addressof_expression (pp, t);
2903 break;
2905 case SCOPE_REF:
2906 dump_decl (pp, t, flags);
2907 break;
2909 case EXPR_PACK_EXPANSION:
2910 case UNARY_LEFT_FOLD_EXPR:
2911 case UNARY_RIGHT_FOLD_EXPR:
2912 case BINARY_LEFT_FOLD_EXPR:
2913 case BINARY_RIGHT_FOLD_EXPR:
2914 case TYPEID_EXPR:
2915 case MEMBER_REF:
2916 case DOTSTAR_EXPR:
2917 case NEW_EXPR:
2918 case VEC_NEW_EXPR:
2919 case DELETE_EXPR:
2920 case VEC_DELETE_EXPR:
2921 case MODOP_EXPR:
2922 case ABS_EXPR:
2923 case ABSU_EXPR:
2924 case CONJ_EXPR:
2925 case VECTOR_CST:
2926 case FIXED_CST:
2927 case UNORDERED_EXPR:
2928 case ORDERED_EXPR:
2929 case UNLT_EXPR:
2930 case UNLE_EXPR:
2931 case UNGT_EXPR:
2932 case UNGE_EXPR:
2933 case UNEQ_EXPR:
2934 case LTGT_EXPR:
2935 case COMPLEX_EXPR:
2936 case BIT_FIELD_REF:
2937 case FIX_TRUNC_EXPR:
2938 case FLOAT_EXPR:
2939 pp->expression (t);
2940 break;
2942 case TRUTH_AND_EXPR:
2943 case TRUTH_OR_EXPR:
2944 case TRUTH_XOR_EXPR:
2945 if (flags & TFF_EXPR_IN_PARENS)
2946 pp_cxx_left_paren (pp);
2947 pp->expression (t);
2948 if (flags & TFF_EXPR_IN_PARENS)
2949 pp_cxx_right_paren (pp);
2950 break;
2952 case OBJ_TYPE_REF:
2953 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2954 break;
2956 case LAMBDA_EXPR:
2957 pp_string (pp, M_("<lambda>"));
2958 break;
2960 case PAREN_EXPR:
2961 pp_cxx_left_paren (pp);
2962 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2963 pp_cxx_right_paren (pp);
2964 break;
2966 case REQUIRES_EXPR:
2967 pp_cxx_requires_expr (cxx_pp, t);
2968 break;
2970 case SIMPLE_REQ:
2971 pp_cxx_simple_requirement (cxx_pp, t);
2972 break;
2974 case TYPE_REQ:
2975 pp_cxx_type_requirement (cxx_pp, t);
2976 break;
2978 case COMPOUND_REQ:
2979 pp_cxx_compound_requirement (cxx_pp, t);
2980 break;
2982 case NESTED_REQ:
2983 pp_cxx_nested_requirement (cxx_pp, t);
2984 break;
2986 case ATOMIC_CONSTR:
2987 case CHECK_CONSTR:
2988 case CONJ_CONSTR:
2989 case DISJ_CONSTR:
2991 pp_cxx_constraint (cxx_pp, t);
2992 break;
2995 case PLACEHOLDER_EXPR:
2996 pp_string (pp, M_("*this"));
2997 break;
2999 case TREE_LIST:
3000 dump_expr_list (pp, t, flags);
3001 break;
3003 /* This list is incomplete, but should suffice for now.
3004 It is very important that `sorry' does not call
3005 `report_error_function'. That could cause an infinite loop. */
3006 default:
3007 pp_unsupported_tree (pp, t);
3008 /* Fall through. */
3009 case ERROR_MARK:
3010 pp_string (pp, M_("<expression error>"));
3011 break;
3015 static void
3016 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3017 int flags)
3019 pp_cxx_left_paren (pp);
3020 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3021 pp_cxx_whitespace (pp);
3022 if (opstring)
3023 pp_cxx_ws_string (pp, opstring);
3024 else
3025 pp_string (pp, M_("<unknown operator>"));
3026 pp_cxx_whitespace (pp);
3027 tree op1 = TREE_OPERAND (t, 1);
3028 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3029 && TREE_CODE (op1) == INTEGER_CST
3030 && tree_int_cst_sign_bit (op1))
3031 /* A pointer minus an integer is represented internally as plus a very
3032 large number, don't expose that to users. */
3033 op1 = convert (ssizetype, op1);
3034 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3035 pp_cxx_right_paren (pp);
3038 static void
3039 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3041 if (flags & TFF_EXPR_IN_PARENS)
3042 pp_cxx_left_paren (pp);
3043 pp_cxx_ws_string (pp, opstring);
3044 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3045 if (flags & TFF_EXPR_IN_PARENS)
3046 pp_cxx_right_paren (pp);
3049 static void
3050 reinit_cxx_pp (void)
3052 pp_clear_output_area (cxx_pp);
3053 cxx_pp->padding = pp_none;
3054 pp_indentation (cxx_pp) = 0;
3055 pp_needs_newline (cxx_pp) = false;
3056 cxx_pp->enclosing_scope = current_function_decl;
3059 /* Same as pp_formatted_text, except the return string is a separate
3060 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3062 inline const char *
3063 pp_ggc_formatted_text (pretty_printer *pp)
3065 return ggc_strdup (pp_formatted_text (pp));
3068 /* Exported interface to stringifying types, exprs and decls under TFF_*
3069 control. */
3071 const char *
3072 type_as_string (tree typ, int flags)
3074 reinit_cxx_pp ();
3075 pp_translate_identifiers (cxx_pp) = false;
3076 dump_type (cxx_pp, typ, flags);
3077 return pp_ggc_formatted_text (cxx_pp);
3080 const char *
3081 type_as_string_translate (tree typ, int flags)
3083 reinit_cxx_pp ();
3084 dump_type (cxx_pp, typ, flags);
3085 return pp_ggc_formatted_text (cxx_pp);
3088 const char *
3089 expr_as_string (tree decl, int flags)
3091 reinit_cxx_pp ();
3092 pp_translate_identifiers (cxx_pp) = false;
3093 dump_expr (cxx_pp, decl, flags);
3094 return pp_ggc_formatted_text (cxx_pp);
3097 /* Wrap decl_as_string with options appropriate for dwarf. */
3099 const char *
3100 decl_as_dwarf_string (tree decl, int flags)
3102 const char *name;
3103 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3104 here will be adequate to get the desired behavior. */
3105 cxx_pp->flags |= pp_c_flag_gnu_v3;
3106 name = decl_as_string (decl, flags);
3107 /* Subsequent calls to the pretty printer shouldn't use this style. */
3108 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3109 return name;
3112 const char *
3113 decl_as_string (tree decl, int flags)
3115 reinit_cxx_pp ();
3116 pp_translate_identifiers (cxx_pp) = false;
3117 dump_decl (cxx_pp, decl, flags);
3118 return pp_ggc_formatted_text (cxx_pp);
3121 const char *
3122 decl_as_string_translate (tree decl, int flags)
3124 reinit_cxx_pp ();
3125 dump_decl (cxx_pp, decl, flags);
3126 return pp_ggc_formatted_text (cxx_pp);
3129 /* Wrap lang_decl_name with options appropriate for dwarf. */
3131 const char *
3132 lang_decl_dwarf_name (tree decl, int v, bool translate)
3134 const char *name;
3135 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3136 here will be adequate to get the desired behavior. */
3137 cxx_pp->flags |= pp_c_flag_gnu_v3;
3138 name = lang_decl_name (decl, v, translate);
3139 /* Subsequent calls to the pretty printer shouldn't use this style. */
3140 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3141 return name;
3144 /* Generate the three forms of printable names for cxx_printable_name. */
3146 const char *
3147 lang_decl_name (tree decl, int v, bool translate)
3149 if (v >= 2)
3150 return (translate
3151 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3152 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3154 reinit_cxx_pp ();
3155 pp_translate_identifiers (cxx_pp) = translate;
3156 if (v == 1
3157 && (DECL_CLASS_SCOPE_P (decl)
3158 || (DECL_NAMESPACE_SCOPE_P (decl)
3159 && CP_DECL_CONTEXT (decl) != global_namespace)))
3161 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3162 pp_cxx_colon_colon (cxx_pp);
3165 if (TREE_CODE (decl) == FUNCTION_DECL)
3166 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3167 else if ((DECL_NAME (decl) == NULL_TREE)
3168 && TREE_CODE (decl) == NAMESPACE_DECL)
3169 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3170 else
3171 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3173 return pp_ggc_formatted_text (cxx_pp);
3176 /* Return the location of a tree passed to %+ formats. */
3178 location_t
3179 location_of (tree t)
3181 if (TYPE_P (t))
3183 t = TYPE_MAIN_DECL (t);
3184 if (t == NULL_TREE)
3185 return input_location;
3187 else if (TREE_CODE (t) == OVERLOAD)
3188 t = OVL_FIRST (t);
3190 if (DECL_P (t))
3191 return DECL_SOURCE_LOCATION (t);
3192 if (TREE_CODE (t) == DEFERRED_PARSE)
3193 return defparse_location (t);
3194 return cp_expr_loc_or_input_loc (t);
3197 /* Now the interfaces from error et al to dump_type et al. Each takes an
3198 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3199 function. */
3201 static const char *
3202 decl_to_string (tree decl, int verbose)
3204 int flags = 0;
3206 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3207 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3208 flags = TFF_CLASS_KEY_OR_ENUM;
3209 if (verbose)
3210 flags |= TFF_DECL_SPECIFIERS;
3211 else if (TREE_CODE (decl) == FUNCTION_DECL)
3212 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3213 flags |= TFF_TEMPLATE_HEADER;
3215 reinit_cxx_pp ();
3216 dump_decl (cxx_pp, decl, flags);
3217 return pp_ggc_formatted_text (cxx_pp);
3220 const char *
3221 expr_to_string (tree decl)
3223 reinit_cxx_pp ();
3224 dump_expr (cxx_pp, decl, 0);
3225 return pp_ggc_formatted_text (cxx_pp);
3228 static const char *
3229 fndecl_to_string (tree fndecl, int verbose)
3231 int flags;
3233 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3234 | TFF_TEMPLATE_HEADER;
3235 if (verbose)
3236 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3237 reinit_cxx_pp ();
3238 dump_decl (cxx_pp, fndecl, flags);
3239 return pp_ggc_formatted_text (cxx_pp);
3243 static const char *
3244 code_to_string (enum tree_code c)
3246 return get_tree_code_name (c);
3249 const char *
3250 language_to_string (enum languages c)
3252 switch (c)
3254 case lang_c:
3255 return "C";
3257 case lang_cplusplus:
3258 return "C++";
3260 default:
3261 gcc_unreachable ();
3263 return NULL;
3266 /* Return the proper printed version of a parameter to a C++ function. */
3268 static const char *
3269 parm_to_string (int p)
3271 reinit_cxx_pp ();
3272 if (p < 0)
3273 pp_string (cxx_pp, "'this'");
3274 else
3275 pp_decimal_int (cxx_pp, p + 1);
3276 return pp_ggc_formatted_text (cxx_pp);
3279 static const char *
3280 op_to_string (bool assop, enum tree_code p)
3282 tree id = ovl_op_identifier (assop, p);
3283 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3286 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3288 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3289 string in appropriate places, and *QUOTE is written to with false
3290 to suppress pp_format's trailing close quote so that e.g.
3291 foo_typedef {aka underlying_foo} {enum}
3292 can be printed by "%qT" as:
3293 `foo_typedef' {aka `underlying_foo'} {enum}
3294 rather than:
3295 `foo_typedef {aka underlying_foo} {enum}'
3296 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3297 then a leading open quote will be added, whereas if POSTPROCESSED is false
3298 (for handling %T) then any leading quote has already been added by
3299 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3300 within %H and %I).
3302 SHOW_COLOR is used to determine the colorization of any quotes that
3303 are added. */
3305 static const char *
3306 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3307 bool show_color)
3309 int flags = 0;
3310 if (verbose)
3311 flags |= TFF_CLASS_KEY_OR_ENUM;
3312 flags |= TFF_TEMPLATE_HEADER;
3314 reinit_cxx_pp ();
3316 if (postprocessed && quote && *quote)
3317 pp_begin_quote (cxx_pp, show_color);
3319 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3320 int type_start, type_len;
3321 type_start = obstack_object_size (ob);
3323 dump_type (cxx_pp, typ, flags);
3325 /* Remember the end of the initial dump. */
3326 type_len = obstack_object_size (ob) - type_start;
3328 /* If we're printing a type that involves typedefs, also print the
3329 stripped version. But sometimes the stripped version looks
3330 exactly the same, so we don't want it after all. To avoid printing
3331 it in that case, we play ugly obstack games. */
3332 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3333 && !uses_template_parms (typ))
3335 int aka_start, aka_len; char *p;
3336 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3337 if (quote && *quote)
3338 pp_end_quote (cxx_pp, show_color);
3339 pp_string (cxx_pp, " {aka");
3340 pp_cxx_whitespace (cxx_pp);
3341 if (quote && *quote)
3342 pp_begin_quote (cxx_pp, show_color);
3343 /* And remember the start of the aka dump. */
3344 aka_start = obstack_object_size (ob);
3345 dump_type (cxx_pp, aka, flags);
3346 aka_len = obstack_object_size (ob) - aka_start;
3347 if (quote && *quote)
3348 pp_end_quote (cxx_pp, show_color);
3349 pp_right_brace (cxx_pp);
3350 p = (char*)obstack_base (ob);
3351 /* If they are identical, cut off the aka by unwinding the obstack. */
3352 if (type_len == aka_len
3353 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3355 /* We can't add a '\0' here, since we may be adding a closing quote
3356 below, and it would be hidden by the '\0'.
3357 Instead, manually unwind the current object within the obstack
3358 so that the insertion point is at the end of the type, before
3359 the "' {aka". */
3360 int delta = type_start + type_len - obstack_object_size (ob);
3361 gcc_assert (delta <= 0);
3362 obstack_blank_fast (ob, delta);
3364 else
3365 if (quote)
3366 /* No further closing quotes are needed. */
3367 *quote = false;
3370 if (quote && *quote)
3372 pp_end_quote (cxx_pp, show_color);
3373 *quote = false;
3375 return pp_ggc_formatted_text (cxx_pp);
3378 static const char *
3379 args_to_string (tree p, int verbose)
3381 int flags = 0;
3382 if (verbose)
3383 flags |= TFF_CLASS_KEY_OR_ENUM;
3385 if (p == NULL_TREE)
3386 return "";
3388 if (TYPE_P (TREE_VALUE (p)))
3389 return type_as_string_translate (p, flags);
3391 reinit_cxx_pp ();
3392 for (; p; p = TREE_CHAIN (p))
3394 if (null_node_p (TREE_VALUE (p)))
3395 pp_cxx_ws_string (cxx_pp, "NULL");
3396 else
3397 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3398 if (TREE_CHAIN (p))
3399 pp_separate_with_comma (cxx_pp);
3401 return pp_ggc_formatted_text (cxx_pp);
3404 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3405 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3406 arguments. */
3408 static const char *
3409 subst_to_string (tree p)
3411 tree decl = TREE_PURPOSE (p);
3412 tree targs = TREE_VALUE (p);
3413 tree tparms = DECL_TEMPLATE_PARMS (decl);
3414 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3415 |TFF_NO_TEMPLATE_BINDINGS);
3417 if (p == NULL_TREE)
3418 return "";
3420 reinit_cxx_pp ();
3421 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3422 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3423 return pp_ggc_formatted_text (cxx_pp);
3426 static const char *
3427 cv_to_string (tree p, int v)
3429 reinit_cxx_pp ();
3430 cxx_pp->padding = v ? pp_before : pp_none;
3431 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3432 return pp_ggc_formatted_text (cxx_pp);
3435 static const char *
3436 eh_spec_to_string (tree p, int /*v*/)
3438 int flags = 0;
3439 reinit_cxx_pp ();
3440 dump_exception_spec (cxx_pp, p, flags);
3441 return pp_ggc_formatted_text (cxx_pp);
3444 /* Langhook for print_error_function. */
3445 void
3446 cxx_print_error_function (diagnostic_context *context, const char *file,
3447 diagnostic_info *diagnostic)
3449 char *prefix;
3450 if (file)
3451 prefix = xstrdup (file);
3452 else
3453 prefix = NULL;
3454 lhd_print_error_function (context, file, diagnostic);
3455 pp_set_prefix (context->printer, prefix);
3456 maybe_print_instantiation_context (context);
3459 static void
3460 cp_diagnostic_starter (diagnostic_context *context,
3461 diagnostic_info *diagnostic)
3463 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3464 cp_print_error_function (context, diagnostic);
3465 maybe_print_instantiation_context (context);
3466 maybe_print_constexpr_context (context);
3467 maybe_print_constraint_context (context);
3468 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3469 diagnostic));
3472 /* Print current function onto BUFFER, in the process of reporting
3473 a diagnostic message. Called from cp_diagnostic_starter. */
3474 static void
3475 cp_print_error_function (diagnostic_context *context,
3476 diagnostic_info *diagnostic)
3478 /* If we are in an instantiation context, current_function_decl is likely
3479 to be wrong, so just rely on print_instantiation_full_context. */
3480 if (current_instantiation ())
3481 return;
3482 /* The above is true for constraint satisfaction also. */
3483 if (current_failed_constraint)
3484 return;
3485 if (diagnostic_last_function_changed (context, diagnostic))
3487 char *old_prefix = pp_take_prefix (context->printer);
3488 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3489 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3490 char *new_prefix = (file && abstract_origin == NULL)
3491 ? file_name_as_prefix (context, file) : NULL;
3493 pp_set_prefix (context->printer, new_prefix);
3495 if (current_function_decl == NULL)
3496 pp_string (context->printer, _("At global scope:"));
3497 else
3499 tree fndecl, ao;
3501 if (abstract_origin)
3503 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3504 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3505 fndecl = ao;
3507 else
3508 fndecl = current_function_decl;
3510 pp_printf (context->printer, function_category (fndecl),
3511 cxx_printable_name_translate (fndecl, 2));
3513 while (abstract_origin)
3515 location_t *locus;
3516 tree block = abstract_origin;
3518 locus = &BLOCK_SOURCE_LOCATION (block);
3519 fndecl = NULL;
3520 block = BLOCK_SUPERCONTEXT (block);
3521 while (block && TREE_CODE (block) == BLOCK
3522 && BLOCK_ABSTRACT_ORIGIN (block))
3524 ao = BLOCK_ABSTRACT_ORIGIN (block);
3525 if (TREE_CODE (ao) == FUNCTION_DECL)
3527 fndecl = ao;
3528 break;
3530 else if (TREE_CODE (ao) != BLOCK)
3531 break;
3533 block = BLOCK_SUPERCONTEXT (block);
3535 if (fndecl)
3536 abstract_origin = block;
3537 else
3539 while (block && TREE_CODE (block) == BLOCK)
3540 block = BLOCK_SUPERCONTEXT (block);
3542 if (block && TREE_CODE (block) == FUNCTION_DECL)
3543 fndecl = block;
3544 abstract_origin = NULL;
3546 if (fndecl)
3548 expanded_location s = expand_location (*locus);
3549 pp_character (context->printer, ',');
3550 pp_newline (context->printer);
3551 if (s.file != NULL)
3553 if (context->show_column && s.column != 0)
3554 pp_printf (context->printer,
3555 _(" inlined from %qs at %r%s:%d:%d%R"),
3556 cxx_printable_name_translate (fndecl, 2),
3557 "locus", s.file, s.line, s.column);
3558 else
3559 pp_printf (context->printer,
3560 _(" inlined from %qs at %r%s:%d%R"),
3561 cxx_printable_name_translate (fndecl, 2),
3562 "locus", s.file, s.line);
3565 else
3566 pp_printf (context->printer, _(" inlined from %qs"),
3567 cxx_printable_name_translate (fndecl, 2));
3570 pp_character (context->printer, ':');
3572 pp_newline (context->printer);
3574 diagnostic_set_last_function (context, diagnostic);
3575 pp_destroy_prefix (context->printer);
3576 context->printer->prefix = old_prefix;
3580 /* Returns a description of FUNCTION using standard terminology. The
3581 result is a format string of the form "In CATEGORY %qs". */
3582 static const char *
3583 function_category (tree fn)
3585 /* We can get called from the middle-end for diagnostics of function
3586 clones. Make sure we have language specific information before
3587 dereferencing it. */
3588 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3589 && DECL_FUNCTION_MEMBER_P (fn))
3591 if (DECL_STATIC_FUNCTION_P (fn))
3592 return _("In static member function %qs");
3593 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3594 return _("In copy constructor %qs");
3595 else if (DECL_CONSTRUCTOR_P (fn))
3596 return _("In constructor %qs");
3597 else if (DECL_DESTRUCTOR_P (fn))
3598 return _("In destructor %qs");
3599 else if (LAMBDA_FUNCTION_P (fn))
3600 return _("In lambda function");
3601 else
3602 return _("In member function %qs");
3604 else
3605 return _("In function %qs");
3608 /* Disable warnings about missing quoting in GCC diagnostics for
3609 the pp_verbatim calls. Their format strings deliberately don't
3610 follow GCC diagnostic conventions. */
3611 #if __GNUC__ >= 10
3612 # pragma GCC diagnostic push
3613 # pragma GCC diagnostic ignored "-Wformat-diag"
3614 #endif
3616 /* Report the full context of a current template instantiation,
3617 onto BUFFER. */
3618 static void
3619 print_instantiation_full_context (diagnostic_context *context)
3621 struct tinst_level *p = current_instantiation ();
3622 location_t location = input_location;
3624 if (p)
3626 pp_verbatim (context->printer,
3627 p->list_p ()
3628 ? _("%s: In substitution of %qS:\n")
3629 : _("%s: In instantiation of %q#D:\n"),
3630 LOCATION_FILE (location),
3631 p->get_node ());
3633 location = p->locus;
3634 p = p->next;
3637 print_instantiation_partial_context (context, p, location);
3640 /* Helper function of print_instantiation_partial_context() that
3641 prints a single line of instantiation context. */
3643 static void
3644 print_instantiation_partial_context_line (diagnostic_context *context,
3645 struct tinst_level *t,
3646 location_t loc, bool recursive_p)
3648 if (loc == UNKNOWN_LOCATION)
3649 return;
3651 expanded_location xloc = expand_location (loc);
3653 if (context->show_column)
3654 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3655 "locus", xloc.file, xloc.line, xloc.column);
3656 else
3657 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3658 "locus", xloc.file, xloc.line);
3660 if (t != NULL)
3662 if (t->list_p ())
3663 pp_verbatim (context->printer,
3664 recursive_p
3665 ? _("recursively required by substitution of %qS\n")
3666 : _("required by substitution of %qS\n"),
3667 t->get_node ());
3668 else
3669 pp_verbatim (context->printer,
3670 recursive_p
3671 ? _("recursively required from %q#D\n")
3672 : _("required from %q#D\n"),
3673 t->get_node ());
3675 else
3677 pp_verbatim (context->printer,
3678 recursive_p
3679 ? _("recursively required from here\n")
3680 : _("required from here\n"));
3684 /* Same as print_instantiation_full_context but less verbose. */
3686 static void
3687 print_instantiation_partial_context (diagnostic_context *context,
3688 struct tinst_level *t0, location_t loc)
3690 struct tinst_level *t;
3691 int n_total = 0;
3692 int n;
3693 location_t prev_loc = loc;
3695 for (t = t0; t != NULL; t = t->next)
3696 if (prev_loc != t->locus)
3698 prev_loc = t->locus;
3699 n_total++;
3702 t = t0;
3704 if (template_backtrace_limit
3705 && n_total > template_backtrace_limit)
3707 int skip = n_total - template_backtrace_limit;
3708 int head = template_backtrace_limit / 2;
3710 /* Avoid skipping just 1. If so, skip 2. */
3711 if (skip == 1)
3713 skip = 2;
3714 head = (template_backtrace_limit - 1) / 2;
3717 for (n = 0; n < head; n++)
3719 gcc_assert (t != NULL);
3720 if (loc != t->locus)
3721 print_instantiation_partial_context_line (context, t, loc,
3722 /*recursive_p=*/false);
3723 loc = t->locus;
3724 t = t->next;
3726 if (t != NULL && skip > 0)
3728 expanded_location xloc;
3729 xloc = expand_location (loc);
3730 if (context->show_column)
3731 pp_verbatim (context->printer,
3732 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3733 "contexts, use -ftemplate-backtrace-limit=0 to "
3734 "disable ]\n"),
3735 "locus", xloc.file, xloc.line, xloc.column, skip);
3736 else
3737 pp_verbatim (context->printer,
3738 _("%r%s:%d:%R [ skipping %d instantiation "
3739 "contexts, use -ftemplate-backtrace-limit=0 to "
3740 "disable ]\n"),
3741 "locus", xloc.file, xloc.line, skip);
3743 do {
3744 loc = t->locus;
3745 t = t->next;
3746 } while (t != NULL && --skip > 0);
3750 while (t != NULL)
3752 while (t->next != NULL && t->locus == t->next->locus)
3754 loc = t->locus;
3755 t = t->next;
3757 print_instantiation_partial_context_line (context, t, loc,
3758 t->locus == loc);
3759 loc = t->locus;
3760 t = t->next;
3762 print_instantiation_partial_context_line (context, NULL, loc,
3763 /*recursive_p=*/false);
3766 /* Called from cp_thing to print the template context for an error. */
3767 static void
3768 maybe_print_instantiation_context (diagnostic_context *context)
3770 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3771 return;
3773 record_last_problematic_instantiation ();
3774 print_instantiation_full_context (context);
3777 /* Report what constexpr call(s) we're trying to expand, if any. */
3779 void
3780 maybe_print_constexpr_context (diagnostic_context *context)
3782 vec<tree> call_stack = cx_error_context ();
3783 unsigned ix;
3784 tree t;
3786 FOR_EACH_VEC_ELT (call_stack, ix, t)
3788 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3789 const char *s = expr_as_string (t, 0);
3790 if (context->show_column)
3791 pp_verbatim (context->printer,
3792 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3793 "locus", xloc.file, xloc.line, xloc.column, s);
3794 else
3795 pp_verbatim (context->printer,
3796 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3797 "locus", xloc.file, xloc.line, s);
3798 pp_newline (context->printer);
3803 static void
3804 print_location (diagnostic_context *context, location_t loc)
3806 expanded_location xloc = expand_location (loc);
3807 if (context->show_column)
3808 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3809 "locus", xloc.file, xloc.line, xloc.column);
3810 else
3811 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3812 "locus", xloc.file, xloc.line);
3815 static void
3816 print_constrained_decl_info (diagnostic_context *context, tree decl)
3818 print_location (context, DECL_SOURCE_LOCATION (decl));
3819 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3822 static void
3823 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3825 gcc_assert (concept_check_p (expr));
3827 tree id = unpack_concept_check (expr);
3828 tree tmpl = TREE_OPERAND (id, 0);
3829 if (OVL_P (tmpl))
3830 tmpl = OVL_FIRST (tmpl);
3832 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3834 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3835 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3836 if (map && map != error_mark_node)
3838 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3839 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3840 ? subst_map : map));
3842 pp_newline (pp);
3845 /* Diagnose the entry point into the satisfaction error. Returns the next
3846 context, if any. */
3848 static tree
3849 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3851 tree src = TREE_VALUE (cxt);
3852 if (!src)
3854 print_location (context, input_location);
3855 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3856 return NULL_TREE;
3858 if (DECL_P (src))
3860 print_constrained_decl_info (context, src);
3861 return NULL_TREE;
3863 else
3865 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3866 return TREE_CHAIN (cxt);
3870 static void
3871 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3874 tree expr = ATOMIC_CONSTR_EXPR (constr);
3875 tree map = ATOMIC_CONSTR_MAP (constr);
3876 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3877 if (map == error_mark_node)
3878 return;
3880 print_location (context, cp_expr_loc_or_input_loc (expr));
3881 pp_verbatim (context->printer, "in requirements ");
3883 tree parms = TREE_OPERAND (expr, 0);
3884 if (parms)
3885 pp_verbatim (context->printer, "with ");
3886 while (parms)
3888 pp_verbatim (context->printer, "%q#D", parms);
3889 if (TREE_CHAIN (parms))
3890 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
3891 parms = TREE_CHAIN (parms);
3893 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
3895 pp_verbatim (context->printer, "\n");
3898 void
3899 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
3901 if (!failed)
3902 return;
3904 tree constr = TREE_VALUE (failed);
3905 if (!constr || constr == error_mark_node)
3906 return;
3907 tree cxt = CONSTR_CONTEXT (constr);
3908 if (!cxt)
3909 return;
3910 tree args = TREE_PURPOSE (failed);
3912 /* Print the stack of requirements. */
3913 cxt = print_constraint_context_head (context, cxt, args);
3914 while (cxt && !DECL_P (TREE_VALUE (cxt)))
3916 tree expr = TREE_VALUE (cxt);
3917 tree map = TREE_PURPOSE (cxt);
3918 print_concept_check_info (context, expr, map, args);
3919 cxt = TREE_CHAIN (cxt);
3922 /* For certain constraints, we can provide additional context. */
3923 if (TREE_CODE (constr) == ATOMIC_CONSTR
3924 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
3925 print_requires_expression_info (context, constr, args);
3928 void
3929 maybe_print_constraint_context (diagnostic_context *context)
3931 if (!current_failed_constraint)
3932 return;
3934 tree cur = current_failed_constraint;
3936 /* Recursively print nested contexts. */
3937 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3938 if (current_failed_constraint)
3939 maybe_print_constraint_context (context);
3941 /* Print this context. */
3942 maybe_print_single_constraint_context (context, cur);
3945 /* Return true iff TYPE_A and TYPE_B are template types that are
3946 meaningful to compare. */
3948 static bool
3949 comparable_template_types_p (tree type_a, tree type_b)
3951 if (!CLASS_TYPE_P (type_a))
3952 return false;
3953 if (!CLASS_TYPE_P (type_b))
3954 return false;
3956 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3957 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3958 if (!tinfo_a || !tinfo_b)
3959 return false;
3961 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3964 /* Start a new line indented by SPC spaces on PP. */
3966 static void
3967 newline_and_indent (pretty_printer *pp, int spc)
3969 pp_newline (pp);
3970 for (int i = 0; i < spc; i++)
3971 pp_space (pp);
3974 /* Generate a GC-allocated string for ARG, an expression or type. */
3976 static const char *
3977 arg_to_string (tree arg, bool verbose)
3979 if (TYPE_P (arg))
3980 return type_to_string (arg, verbose, true, NULL, false);
3981 else
3982 return expr_to_string (arg);
3985 /* Subroutine to type_to_string_with_compare and
3986 print_template_tree_comparison.
3988 Print a representation of ARG (an expression or type) to PP,
3989 colorizing it as "type-diff" if PP->show_color. */
3991 static void
3992 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3994 pp_printf (pp, "%r%s%R",
3995 "type-diff",
3996 (arg
3997 ? arg_to_string (arg, verbose)
3998 : G_("(no argument)")));
4001 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4003 The types must satisfy comparable_template_types_p.
4005 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4006 potentially colorizing/eliding in comparison with TYPE_B.
4008 For example given types:
4009 vector<map<int,double>>
4011 vector<map<int,float>>
4012 then the result on PP would be:
4013 vector<map<[...],double>>
4014 with type elision, and:
4015 vector<map<int,double>>
4016 without type elision.
4018 In both cases the parts of TYPE that differ from PEER will be colorized
4019 if pp_show_color (pp) is true. In the above example, this would be
4020 "double".
4022 If INDENT is non-zero, then the types are printed in a tree-like form
4023 which shows both types. In the above example, the result on PP would be:
4025 vector<
4026 map<
4027 [...],
4028 [double != float]>>
4030 and without type-elision would be:
4032 vector<
4033 map<
4034 int,
4035 [double != float]>>
4037 As before, the differing parts of the types are colorized if
4038 pp_show_color (pp) is true ("double" and "float" in this example).
4040 Template arguments in which both types are using the default arguments
4041 are not printed; if at least one of the two types is using a non-default
4042 argument, then that argument is printed (or both arguments for the
4043 tree-like print format). */
4045 static void
4046 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4047 bool verbose, int indent)
4049 if (indent)
4050 newline_and_indent (pp, indent);
4052 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4053 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4055 pp_printf (pp, "%s<",
4056 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4058 tree args_a = TI_ARGS (tinfo_a);
4059 tree args_b = TI_ARGS (tinfo_b);
4060 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4061 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4062 int flags = 0;
4063 int len_a = get_non_default_template_args_count (args_a, flags);
4064 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4065 int len_b = get_non_default_template_args_count (args_b, flags);
4066 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4067 /* Determine the maximum range of args for which non-default template args
4068 were used; beyond this, only default args (if any) were used, and so
4069 they will be equal from this point onwards.
4070 One of the two peers might have used default arguments within this
4071 range, but the other will be using non-default arguments, and so
4072 it's more readable to print both within this range, to highlight
4073 the differences. */
4074 int len_max = MAX (len_a, len_b);
4075 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4076 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4077 for (int idx = 0; idx < len_max; idx++)
4079 if (idx)
4080 pp_character (pp, ',');
4082 tree arg_a = TREE_VEC_ELT (args_a, idx);
4083 tree arg_b = TREE_VEC_ELT (args_b, idx);
4084 if (arg_a == arg_b)
4086 if (indent)
4087 newline_and_indent (pp, indent + 2);
4088 /* Can do elision here, printing "[...]". */
4089 if (flag_elide_type)
4090 pp_string (pp, G_("[...]"));
4091 else
4092 pp_string (pp, arg_to_string (arg_a, verbose));
4094 else
4096 int new_indent = indent ? indent + 2 : 0;
4097 if (comparable_template_types_p (arg_a, arg_b))
4098 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
4099 else
4100 if (indent)
4102 newline_and_indent (pp, indent + 2);
4103 pp_character (pp, '[');
4104 print_nonequal_arg (pp, arg_a, verbose);
4105 pp_string (pp, " != ");
4106 print_nonequal_arg (pp, arg_b, verbose);
4107 pp_character (pp, ']');
4109 else
4110 print_nonequal_arg (pp, arg_a, verbose);
4113 pp_printf (pp, ">");
4116 /* As type_to_string, but for a template, potentially colorizing/eliding
4117 in comparison with PEER.
4118 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4119 then the resulting string would be:
4120 map<[...],double>
4121 with type elision, and:
4122 map<int,double>
4123 without type elision.
4125 In both cases the parts of TYPE that differ from PEER will be colorized
4126 if SHOW_COLOR is true. In the above example, this would be "double".
4128 Template arguments in which both types are using the default arguments
4129 are not printed; if at least one of the two types is using a non-default
4130 argument, then both arguments are printed.
4132 The resulting string is in a GC-allocated buffer. */
4134 static const char *
4135 type_to_string_with_compare (tree type, tree peer, bool verbose,
4136 bool show_color)
4138 pretty_printer inner_pp;
4139 pretty_printer *pp = &inner_pp;
4140 pp_show_color (pp) = show_color;
4142 print_template_differences (pp, type, peer, verbose, 0);
4143 return pp_ggc_formatted_text (pp);
4146 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4147 indented by INDENT spaces.
4149 For example given types:
4151 vector<map<int,double>>
4155 vector<map<double,float>>
4157 the output with type elision would be:
4159 vector<
4160 map<
4161 [...],
4162 [double != float]>>
4164 and without type-elision would be:
4166 vector<
4167 map<
4168 int,
4169 [double != float]>>
4171 TYPE_A and TYPE_B must both be comparable template types
4172 (as per comparable_template_types_p).
4174 Template arguments in which both types are using the default arguments
4175 are not printed; if at least one of the two types is using a non-default
4176 argument, then both arguments are printed. */
4178 static void
4179 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4180 bool verbose, int indent)
4182 print_template_differences (pp, type_a, type_b, verbose, indent);
4185 /* Subroutine for use in a format_postprocessor::handle
4186 implementation. Adds a chunk to the end of
4187 formatted output, so that it will be printed
4188 by pp_output_formatted_text. */
4190 static void
4191 append_formatted_chunk (pretty_printer *pp, const char *content)
4193 output_buffer *buffer = pp_buffer (pp);
4194 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4195 const char **args = chunk_array->args;
4197 unsigned int chunk_idx;
4198 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4200 args[chunk_idx++] = content;
4201 args[chunk_idx] = NULL;
4204 /* Create a copy of CONTENT, with quotes added, and,
4205 potentially, with colorization.
4206 No escaped is performed on CONTENT.
4207 The result is in a GC-allocated buffer. */
4209 static const char *
4210 add_quotes (const char *content, bool show_color)
4212 pretty_printer tmp_pp;
4213 pp_show_color (&tmp_pp) = show_color;
4215 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4216 quoting colorization bytes within the results and using either
4217 pp_quote or pp_begin_quote doesn't work the same. */
4218 pp_printf (&tmp_pp, "%<%s%>", content);
4220 return pp_ggc_formatted_text (&tmp_pp);
4223 #if __GNUC__ >= 10
4224 # pragma GCC diagnostic pop
4225 #endif
4227 /* If we had %H and %I, and hence deferred printing them,
4228 print them now, storing the result into the chunk_info
4229 for pp_format. Quote them if 'q' was provided.
4230 Also print the difference in tree form, adding it as
4231 an additional chunk. */
4233 void
4234 cxx_format_postprocessor::handle (pretty_printer *pp)
4236 /* If we have one of %H and %I, the other should have
4237 been present. */
4238 if (m_type_a.m_tree || m_type_b.m_tree)
4240 /* Avoid reentrancy issues by working with a copy of
4241 m_type_a and m_type_b, resetting them now. */
4242 deferred_printed_type type_a = m_type_a;
4243 deferred_printed_type type_b = m_type_b;
4244 m_type_a = deferred_printed_type ();
4245 m_type_b = deferred_printed_type ();
4247 gcc_assert (type_a.m_buffer_ptr);
4248 gcc_assert (type_b.m_buffer_ptr);
4250 bool show_color = pp_show_color (pp);
4252 const char *type_a_text;
4253 const char *type_b_text;
4255 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4257 type_a_text
4258 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4259 type_a.m_verbose, show_color);
4260 type_b_text
4261 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4262 type_b.m_verbose, show_color);
4264 if (flag_diagnostics_show_template_tree)
4266 pretty_printer inner_pp;
4267 pp_show_color (&inner_pp) = pp_show_color (pp);
4268 print_template_tree_comparison
4269 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4270 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4273 else
4275 /* If the types were not comparable (or if only one of %H/%I was
4276 provided), they are printed normally, and no difference tree
4277 is printed. */
4278 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4279 true, &type_a.m_quote, show_color);
4280 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4281 true, &type_b.m_quote, show_color);
4284 if (type_a.m_quote)
4285 type_a_text = add_quotes (type_a_text, show_color);
4286 *type_a.m_buffer_ptr = type_a_text;
4288 if (type_b.m_quote)
4289 type_b_text = add_quotes (type_b_text, show_color);
4290 *type_b.m_buffer_ptr = type_b_text;
4294 /* Subroutine for handling %H and %I, to support i18n of messages like:
4296 error_at (loc, "could not convert %qE from %qH to %qI",
4297 expr, type_a, type_b);
4299 so that we can print things like:
4301 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4303 and, with type-elision:
4305 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4307 (with color-coding of the differences between the types).
4309 The %H and %I format codes are peers: both must be present,
4310 and they affect each other. Hence to handle them, we must
4311 delay printing until we have both, deferring the printing to
4312 pretty_printer's m_format_postprocessor hook.
4314 This is called in phase 2 of pp_format, when it is accumulating
4315 a series of formatted chunks. We stash the location of the chunk
4316 we're meant to have written to, so that we can write to it in the
4317 m_format_postprocessor hook.
4319 We also need to stash whether a 'q' prefix was provided (the QUOTE
4320 param) so that we can add the quotes when writing out the delayed
4321 chunk. */
4323 static void
4324 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4325 tree type, const char **buffer_ptr,
4326 bool verbose, bool quote)
4328 gcc_assert (deferred->m_tree == NULL_TREE);
4329 gcc_assert (deferred->m_buffer_ptr == NULL);
4330 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4334 /* Called from output_format -- during diagnostic message processing --
4335 to handle C++ specific format specifier with the following meanings:
4336 %A function argument-list.
4337 %C tree code.
4338 %D declaration.
4339 %E expression.
4340 %F function declaration.
4341 %H type difference (from).
4342 %I type difference (to).
4343 %L language as used in extern "lang".
4344 %O binary operator.
4345 %P function parameter whose position is indicated by an integer.
4346 %Q assignment operator.
4347 %S substitution (template + args)
4348 %T type.
4349 %V cv-qualifier.
4350 %X exception-specification. */
4351 static bool
4352 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4353 int precision, bool wide, bool set_locus, bool verbose,
4354 bool *quoted, const char **buffer_ptr)
4356 gcc_assert (pp->m_format_postprocessor);
4357 cxx_format_postprocessor *postprocessor
4358 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4360 const char *result;
4361 tree t = NULL;
4362 #define next_tree (t = va_arg (*text->args_ptr, tree))
4363 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4364 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4365 #define next_int va_arg (*text->args_ptr, int)
4367 if (precision != 0 || wide)
4368 return false;
4370 switch (*spec)
4372 case 'A': result = args_to_string (next_tree, verbose); break;
4373 case 'C': result = code_to_string (next_tcode); break;
4374 case 'D':
4376 tree temp = next_tree;
4377 if (VAR_P (temp)
4378 && DECL_HAS_DEBUG_EXPR_P (temp))
4380 temp = DECL_DEBUG_EXPR (temp);
4381 if (!DECL_P (temp))
4383 result = expr_to_string (temp);
4384 break;
4387 result = decl_to_string (temp, verbose);
4389 break;
4390 case 'E': result = expr_to_string (next_tree); break;
4391 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4392 case 'H':
4393 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4394 buffer_ptr, verbose, *quoted);
4395 return true;
4396 case 'I':
4397 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4398 buffer_ptr, verbose, *quoted);
4399 return true;
4400 case 'L': result = language_to_string (next_lang); break;
4401 case 'O': result = op_to_string (false, next_tcode); break;
4402 case 'P': result = parm_to_string (next_int); break;
4403 case 'Q': result = op_to_string (true, next_tcode); break;
4404 case 'S': result = subst_to_string (next_tree); break;
4405 case 'T':
4407 result = type_to_string (next_tree, verbose, false, quoted,
4408 pp_show_color (pp));
4410 break;
4411 case 'V': result = cv_to_string (next_tree, verbose); break;
4412 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4414 default:
4415 return false;
4418 pp_string (pp, result);
4419 if (set_locus && t != NULL)
4420 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4421 return true;
4422 #undef next_tree
4423 #undef next_tcode
4424 #undef next_lang
4425 #undef next_int
4428 /* Warn about the use of C++0x features when appropriate. */
4429 void
4430 maybe_warn_cpp0x (cpp0x_warn_str str)
4432 if (cxx_dialect == cxx98)
4433 switch (str)
4435 case CPP0X_INITIALIZER_LISTS:
4436 pedwarn (input_location, OPT_Wc__11_extensions,
4437 "extended initializer lists "
4438 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4439 break;
4440 case CPP0X_EXPLICIT_CONVERSION:
4441 pedwarn (input_location, OPT_Wc__11_extensions,
4442 "explicit conversion operators "
4443 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4444 break;
4445 case CPP0X_VARIADIC_TEMPLATES:
4446 pedwarn (input_location, OPT_Wc__11_extensions,
4447 "variadic templates "
4448 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4449 break;
4450 case CPP0X_LAMBDA_EXPR:
4451 pedwarn (input_location, OPT_Wc__11_extensions,
4452 "lambda expressions "
4453 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4454 break;
4455 case CPP0X_AUTO:
4456 pedwarn (input_location, OPT_Wc__11_extensions,
4457 "C++11 auto only available with %<-std=c++11%> or "
4458 "%<-std=gnu++11%>");
4459 break;
4460 case CPP0X_SCOPED_ENUMS:
4461 pedwarn (input_location, OPT_Wc__11_extensions,
4462 "scoped enums only available with %<-std=c++11%> or "
4463 "%<-std=gnu++11%>");
4464 break;
4465 case CPP0X_DEFAULTED_DELETED:
4466 pedwarn (input_location, OPT_Wc__11_extensions,
4467 "defaulted and deleted functions "
4468 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4469 break;
4470 case CPP0X_INLINE_NAMESPACES:
4471 if (pedantic)
4472 pedwarn (input_location, OPT_Wc__11_extensions,
4473 "inline namespaces "
4474 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4475 break;
4476 case CPP0X_OVERRIDE_CONTROLS:
4477 pedwarn (input_location, OPT_Wc__11_extensions,
4478 "override controls (override/final) "
4479 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4480 break;
4481 case CPP0X_NSDMI:
4482 pedwarn (input_location, OPT_Wc__11_extensions,
4483 "non-static data member initializers "
4484 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4485 break;
4486 case CPP0X_USER_DEFINED_LITERALS:
4487 pedwarn (input_location, OPT_Wc__11_extensions,
4488 "user-defined literals "
4489 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4490 break;
4491 case CPP0X_DELEGATING_CTORS:
4492 pedwarn (input_location, OPT_Wc__11_extensions,
4493 "delegating constructors "
4494 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4495 break;
4496 case CPP0X_INHERITING_CTORS:
4497 pedwarn (input_location, OPT_Wc__11_extensions,
4498 "inheriting constructors "
4499 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4500 break;
4501 case CPP0X_ATTRIBUTES:
4502 pedwarn (input_location, OPT_Wc__11_extensions,
4503 "C++11 attributes "
4504 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4505 break;
4506 case CPP0X_REF_QUALIFIER:
4507 pedwarn (input_location, OPT_Wc__11_extensions,
4508 "ref-qualifiers "
4509 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4510 break;
4511 default:
4512 gcc_unreachable ();
4516 /* Warn about the use of variadic templates when appropriate. */
4517 void
4518 maybe_warn_variadic_templates (void)
4520 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4524 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4525 option OPT with text GMSGID. Use this function to report
4526 diagnostics for constructs that are invalid C++98, but valid
4527 C++0x. */
4528 bool
4529 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4531 diagnostic_info diagnostic;
4532 va_list ap;
4533 bool ret;
4534 rich_location richloc (line_table, location);
4536 va_start (ap, gmsgid);
4537 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4538 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4539 diagnostic.option_index = opt;
4540 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4541 va_end (ap);
4542 return ret;
4545 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4546 we found when we tried to do the lookup. LOCATION is the location of
4547 the NAME identifier. */
4549 void
4550 qualified_name_lookup_error (tree scope, tree name,
4551 tree decl, location_t location)
4553 if (scope == error_mark_node)
4554 ; /* We already complained. */
4555 else if (TYPE_P (scope))
4557 if (!COMPLETE_TYPE_P (scope))
4558 error_at (location, "incomplete type %qT used in nested name specifier",
4559 scope);
4560 else if (TREE_CODE (decl) == TREE_LIST)
4562 error_at (location, "reference to %<%T::%D%> is ambiguous",
4563 scope, name);
4564 print_candidates (decl);
4566 else
4568 name_hint hint;
4569 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4570 hint = suggest_alternative_in_scoped_enum (name, scope);
4571 if (const char *suggestion = hint.suggestion ())
4573 gcc_rich_location richloc (location);
4574 richloc.add_fixit_replace (suggestion);
4575 error_at (&richloc,
4576 "%qD is not a member of %qT; did you mean %qs?",
4577 name, scope, suggestion);
4579 else
4580 error_at (location, "%qD is not a member of %qT", name, scope);
4583 else if (scope != global_namespace)
4585 auto_diagnostic_group d;
4586 bool emit_fixit = true;
4587 name_hint hint
4588 = suggest_alternative_in_explicit_scope (location, name, scope);
4589 if (!hint)
4591 hint = suggest_alternatives_in_other_namespaces (location, name);
4592 /* "location" is just the location of the name, not of the explicit
4593 scope, and it's not easy to get at the latter, so we can't issue
4594 fix-it hints for the suggestion. */
4595 emit_fixit = false;
4597 if (const char *suggestion = hint.suggestion ())
4599 gcc_rich_location richloc (location);
4600 if (emit_fixit)
4601 richloc.add_fixit_replace (suggestion);
4602 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4603 name, scope, suggestion);
4605 else
4606 error_at (location, "%qD is not a member of %qD", name, scope);
4608 else
4610 auto_diagnostic_group d;
4611 name_hint hint = suggest_alternatives_for (location, name, true);
4612 if (const char *suggestion = hint.suggestion ())
4614 gcc_rich_location richloc (location);
4615 richloc.add_fixit_replace (suggestion);
4616 error_at (&richloc,
4617 "%<::%D%> has not been declared; did you mean %qs?",
4618 name, suggestion);
4620 else
4621 error_at (location, "%<::%D%> has not been declared", name);
4625 /* C++-specific implementation of range_label::get_text () vfunc for
4626 range_label_for_type_mismatch.
4628 Compare with print_template_differences above. */
4630 label_text
4631 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4633 if (m_labelled_type == NULL_TREE)
4634 return label_text::borrow (NULL);
4636 const bool verbose = false;
4637 const bool show_color = false;
4639 const char *result;
4640 if (m_other_type
4641 && comparable_template_types_p (m_labelled_type, m_other_type))
4642 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4643 verbose, show_color);
4644 else
4645 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4647 /* Both of the above return GC-allocated buffers, so the caller mustn't
4648 free them. */
4649 return label_text::borrow (result);