Debian Codegraph @

gcc/cp/error.cc

raw
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 /* For use with name_hint. */
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "stringpool.h"
26 #include "tree-diagnostic.h"
27 #include "diagnostic-color.h"
28 #include "langhooks-def.h"
29 #include "intl.h"
30 #include "cxx-pretty-print.h"
31 #include "tree-pretty-print.h"
32 #include "tree-pretty-print-markup.h"
33 #include "gimple-pretty-print.h"
34 #include "c-family/c-objc.h"
35 #include "ubsan.h"
36 #include "internal-fn.h"
37 #include "c-family/c-type-mismatch.h"
38 #include "cp-name-hint.h"
39 #include "attribs.h"
40 #include "pretty-print-format-impl.h"
41 #include "make-unique.h"
42 #include "diagnostic-format-text.h"
43
44 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
45 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
46
47 /* cxx_pp is a C++ front-end-specific pretty printer: this is where we
48 dump C++ ASTs as strings. It is mostly used only by the various
49 tree -> string functions that are occasionally called from the
50 debugger or by the front-end for things like
51 __PRETTY_FUNCTION__. */
52 static cxx_pretty_printer actual_pretty_printer;
53 static cxx_pretty_printer * const cxx_pp = &actual_pretty_printer;
54
55 /* Translate if being used for diagnostics, but not for dump files or
56 __PRETTY_FUNCTION. */
57 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
58
59 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
60
61 static const char *args_to_string (tree, int);
62 static const char *code_to_string (enum tree_code);
63 static const char *cv_to_string (tree, int);
64 static const char *decl_to_string (tree, int, bool);
65 static const char *fndecl_to_string (tree, int);
66 static const char *op_to_string (bool, enum tree_code);
67 static const char *parm_to_string (int);
68 static const char *type_to_string (tree, int, bool, bool *, bool,
69 const char * = nullptr);
70
71 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
72 static void dump_type (cxx_pretty_printer *, tree, int);
73 static void dump_typename (cxx_pretty_printer *, tree, int);
74 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
75 static void dump_decl (cxx_pretty_printer *, tree, int);
76 static void dump_template_decl (cxx_pretty_printer *, tree, int);
77 static void dump_function_decl (cxx_pretty_printer *, tree, int);
78 static void dump_expr (cxx_pretty_printer *, tree, int);
79 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
80 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
81 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
82 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
83 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
84 static void dump_function_name (cxx_pretty_printer *, tree, int);
85 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
86 static void dump_expr_list (cxx_pretty_printer *, tree, int);
87 static void dump_global_iord (cxx_pretty_printer *, tree);
88 static void dump_parameters (cxx_pretty_printer *, tree, int);
89 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
90 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
91 static void dump_template_argument (cxx_pretty_printer *, tree, int);
92 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
93 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
94 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
95 vec<tree, va_gc> *);
96 static void dump_scope (cxx_pretty_printer *, tree, int);
97 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
98 static int get_non_default_template_args_count (tree, int);
99 static const char *function_category (tree);
100 static void maybe_print_constexpr_context (diagnostic_text_output_format &);
101 static void maybe_print_instantiation_context (diagnostic_text_output_format &);
102 static void print_instantiation_full_context (diagnostic_text_output_format &);
103 static void print_instantiation_partial_context (diagnostic_text_output_format &,
104 struct tinst_level *,
105 location_t);
106 static void maybe_print_constraint_context (diagnostic_text_output_format &);
107 static void cp_diagnostic_text_starter (diagnostic_text_output_format &,
108 const diagnostic_info *);
109 static void cp_print_error_function (diagnostic_text_output_format &,
110 const diagnostic_info *);
111
112 static bool cp_printer (pretty_printer *, text_info *, const char *,
113 int, bool, bool, bool, bool *, pp_token_list &);
114
115 /* Color names for highlighting "%qH" vs "%qI" values,
116 and ranges corresponding to them. */
117 const char *const highlight_colors::percent_h = "highlight-a";
118 const char *const highlight_colors::percent_i = "highlight-b";
119
120 /* Struct for handling %H or %I, which require delaying printing the
121 type until a postprocessing stage. */
122
123 class deferred_printed_type
124 {
125 public:
126 deferred_printed_type ()
127 : m_tree (NULL_TREE),
128 m_printed_text (),
129 m_token_list (nullptr),
130 m_verbose (false), m_quote (false)
131 {}
132
133 deferred_printed_type (tree type,
134 pp_token_list &token_list,
135 bool verbose,
136 bool quote)
137 : m_tree (type),
138 m_printed_text (),
139 m_token_list (&token_list),
140 m_verbose (verbose),
141 m_quote (quote)
142 {
143 gcc_assert (type);
144 }
145
146 void set_text_for_token_list (const char *text, bool quote)
147 {
148 /* Replace the contents of m_token_list with a text token for TEXT,
149 possibly wrapped by BEGIN_QUOTE/END_QUOTE (if QUOTE is true).
150 This allows us to ignore any {BEGIN,END}_QUOTE tokens added
151 by %qH and %qI, and instead use the quoting from type_to_string,
152 and its logic for "aka". */
153 while (m_token_list->m_first)
154 m_token_list->pop_front ();
155
156 if (quote)
157 m_token_list->push_back<pp_token_begin_quote> ();
158
159 // TEXT is gc-allocated, so we can borrow it
160 m_token_list->push_back_text (label_text::borrow (text));
161
162 if (quote)
163 m_token_list->push_back<pp_token_end_quote> ();
164 }
165
166 /* The tree is not GTY-marked: they are only non-NULL within a
167 call to pp_format. */
168 tree m_tree;
169 label_text m_printed_text;
170 pp_token_list *m_token_list;
171 bool m_verbose;
172 bool m_quote;
173 };
174
175 /* Subclass of format_postprocessor for the C++ frontend.
176 This handles the %H and %I formatting codes, printing them
177 in a postprocessing phase (since they affect each other). */
178
179 class cxx_format_postprocessor : public format_postprocessor
180 {
181 public:
182 cxx_format_postprocessor ()
183 : m_type_a (), m_type_b ()
184 {}
185
186 format_postprocessor *clone() const final override
187 {
188 return new cxx_format_postprocessor ();
189 }
190
191 void handle (pretty_printer *pp) final override;
192
193 deferred_printed_type m_type_a;
194 deferred_printed_type m_type_b;
195 };
196
197 /* Return the in-scope template that's currently being parsed, or
198 NULL_TREE otherwise. */
199
200 static tree
201 get_current_template ()
202 {
203 if (scope_chain && in_template_context && !current_instantiation ())
204 if (tree ti = get_template_info (current_scope ()))
205 {
206 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)) && TI_PARTIAL_INFO (ti))
207 ti = TI_PARTIAL_INFO (ti);
208 return TI_TEMPLATE (ti);
209 }
210
211 return NULL_TREE;
212 }
213
214 /* A map from TEMPLATE_DECLs that we've determined to be erroneous
215 at parse time to the location of the first error within. */
216
217 erroneous_templates_t *erroneous_templates;
218
219 /* Callback function diagnostic_context::m_adjust_diagnostic_info.
220
221 Errors issued when parsing a template are automatically treated like
222 permerrors associated with the -Wtemplate-body flag and can be
223 downgraded into warnings accordingly, in which case we'll still
224 issue an error if we later need to instantiate the template. */
225
226 static void
227 cp_adjust_diagnostic_info (diagnostic_context *context,
228 diagnostic_info *diagnostic)
229 {
230 if (diagnostic->kind == DK_ERROR)
231 if (tree tmpl = get_current_template ())
232 {
233 diagnostic->option_id = OPT_Wtemplate_body;
234
235 if (context->m_permissive)
236 diagnostic->kind = DK_WARNING;
237
238 bool existed;
239 location_t &error_loc
240 = hash_map_safe_get_or_insert<true> (erroneous_templates,
241 tmpl, &existed);
242 if (!existed)
243 /* Remember that this template had a parse-time error so
244 that we'll ensure a hard error has been issued upon
245 its instantiation. */
246 error_loc = diagnostic->richloc->get_loc ();
247 }
248 }
249
250 /* A generalization of seen_error which also returns true if we've
251 permissively downgraded an error to a warning inside a template. */
252
253 bool
254 cp_seen_error ()
255 {
256 if ((seen_error) ())
257 return true;
258
259 if (erroneous_templates)
260 if (tree tmpl = get_current_template ())
261 if (erroneous_templates->get (tmpl))
262 return true;
263
264 return false;
265 }
266
267 /* CONTEXT->printer is a basic pretty printer that was constructed
268 presumably by diagnostic_initialize(), called early in the
269 compiler's initialization process (in general_init) Before the FE
270 is initialized. This (C++) FE-specific diagnostic initializer is
271 thus replacing the basic pretty printer with one that has C++-aware
272 capacities. */
273
274 void
275 cxx_initialize_diagnostics (diagnostic_context *context)
276 {
277 cxx_pretty_printer *pp = new cxx_pretty_printer ();
278 pp_format_postprocessor (pp) = new cxx_format_postprocessor ();
279 context->set_pretty_printer (std::unique_ptr<pretty_printer> (pp));
280
281 c_common_diagnostics_set_defaults (context);
282 diagnostic_text_starter (context) = cp_diagnostic_text_starter;
283 /* diagnostic_finalizer is already c_diagnostic_text_finalizer. */
284 context->set_format_decoder (cp_printer);
285 context->m_adjust_diagnostic_info = cp_adjust_diagnostic_info;
286 }
287
288 /* Dump an '@module' name suffix for DECL, if it's attached to an import. */
289
290 static void
291 dump_module_suffix (cxx_pretty_printer *pp, tree decl)
292 {
293 if (!modules_p ())
294 return;
295
296 if (!DECL_CONTEXT (decl))
297 return;
298
299 if (TREE_CODE (decl) != CONST_DECL
300 || !UNSCOPED_ENUM_P (DECL_CONTEXT (decl)))
301 {
302 if (!DECL_NAMESPACE_SCOPE_P (decl))
303 return;
304
305 if (TREE_CODE (decl) == NAMESPACE_DECL
306 && !DECL_NAMESPACE_ALIAS (decl)
307 && (TREE_PUBLIC (decl) || !TREE_PUBLIC (CP_DECL_CONTEXT (decl))))
308 return;
309 }
310
311 int m = get_originating_module (decl, /*global=-1*/true);
312 if (m > 0)
313 if (const char *n = module_name (m, false))
314 {
315 pp_character (pp, '@');
316 pp->set_padding (pp_none);
317 pp_string (pp, n);
318 }
319 }
320
321 /* The scope of the declaration we're currently printing, to avoid redundantly
322 dumping the same scope on parameter types. */
323 static tree current_dump_scope;
324
325 /* Dump a scope, if deemed necessary. */
326
327 static void
328 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
329 {
330 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
331
332 if (scope == NULL_TREE || scope == current_dump_scope)
333 return;
334
335 /* Enum values within an unscoped enum will be CONST_DECL with an
336 ENUMERAL_TYPE as their "scope". Use CP_TYPE_CONTEXT of the
337 ENUMERAL_TYPE, so as to print any enclosing namespace. */
338 if (UNSCOPED_ENUM_P (scope))
339 scope = CP_TYPE_CONTEXT (scope);
340
341 if (TREE_CODE (scope) == NAMESPACE_DECL)
342 {
343 if (scope != global_namespace)
344 {
345 dump_decl (pp, scope, f);
346 pp_cxx_colon_colon (pp);
347 }
348 }
349 else if (AGGREGATE_TYPE_P (scope)
350 || SCOPED_ENUM_P (scope))
351 {
352 dump_type (pp, scope, f);
353 pp_cxx_colon_colon (pp);
354 }
355 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
356 {
357 dump_function_decl (pp, scope, f | TFF_NO_TEMPLATE_BINDINGS);
358 pp_cxx_colon_colon (pp);
359 }
360 }
361
362 /* Dump the template ARGument under control of FLAGS. */
363
364 static void
365 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
366 {
367 if (ARGUMENT_PACK_P (arg))
368 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
369 /* No default args in argument packs. */
370 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
371 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
372 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
373 else
374 {
375 if (TREE_CODE (arg) == TREE_LIST)
376 arg = TREE_VALUE (arg);
377
378 /* Strip implicit conversions. */
379 while (CONVERT_EXPR_P (arg))
380 arg = TREE_OPERAND (arg, 0);
381
382 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
383 }
384 }
385
386 /* Count the number of template arguments ARGS whose value does not
387 match the (optional) default template parameter in PARAMS */
388
389 static int
390 get_non_default_template_args_count (tree args, int flags)
391 {
392 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
393
394 if (/* We use this flag when generating debug information. We don't
395 want to expand templates at this point, for this may generate
396 new decls, which gets decl counts out of sync, which may in
397 turn cause codegen differences between compilations with and
398 without -g. */
399 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
400 || !flag_pretty_templates)
401 return n;
402
403 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
404 }
405
406 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
407 of FLAGS. */
408
409 static void
410 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
411 {
412 int n = get_non_default_template_args_count (args, flags);
413 int need_comma = 0;
414 int i;
415
416 for (i = 0; i < n; ++i)
417 {
418 tree arg = TREE_VEC_ELT (args, i);
419
420 /* Only print a comma if we know there is an argument coming. In
421 the case of an empty template argument pack, no actual
422 argument will be printed. */
423 if (need_comma
424 && (!ARGUMENT_PACK_P (arg)
425 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
426 pp_separate_with_comma (pp);
427
428 dump_template_argument (pp, arg, flags);
429 need_comma = 1;
430 }
431 }
432
433 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
434
435 static void
436 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
437 {
438 tree p;
439 tree a;
440
441 if (parm == error_mark_node)
442 return;
443
444 p = TREE_VALUE (parm);
445 a = TREE_PURPOSE (parm);
446
447 if (TREE_CODE (p) == TYPE_DECL)
448 {
449 if (flags & TFF_DECL_SPECIFIERS)
450 {
451 pp_cxx_ws_string (pp, "class");
452 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
453 pp_cxx_ws_string (pp, "...");
454 if (DECL_NAME (p))
455 pp_cxx_tree_identifier (pp, DECL_NAME (p));
456 }
457 else if (DECL_NAME (p))
458 pp_cxx_tree_identifier (pp, DECL_NAME (p));
459 else
460 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
461 }
462 else
463 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
464
465 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
466 {
467 pp_cxx_whitespace (pp);
468 pp_equal (pp);
469 pp_cxx_whitespace (pp);
470 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
471 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
472 else
473 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
474 }
475 }
476
477 /* Dump, under control of FLAGS, a template-parameter-list binding.
478 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
479 TREE_VEC. */
480
481 static void
482 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
483 vec<tree, va_gc> *typenames)
484 {
485 /* Print "[with" and ']', conditional on whether anything is printed at all.
486 This is tied to whether a semicolon is needed to separate multiple template
487 parameters. */
488 struct prepost_semicolon
489 {
490 cxx_pretty_printer *pp;
491 bool need_semicolon;
492
493 void operator() ()
494 {
495 if (need_semicolon)
496 pp_separate_with_semicolon (pp);
497 else
498 {
499 pp_cxx_whitespace (pp);
500 pp_string (pp, colorize_start (pp_show_color (pp), "targs"));
501 pp_cxx_left_bracket (pp);
502 pp->translate_string ("with");
503 pp_cxx_whitespace (pp);
504 need_semicolon = true;
505 }
506 }
507
508 ~prepost_semicolon ()
509 {
510 if (need_semicolon)
511 {
512 pp_cxx_right_bracket (pp);
513 pp_string (pp, colorize_stop (pp_show_color (pp)));
514 }
515 }
516 } semicolon_or_introducer = {pp, false};
517
518 int i;
519 tree t;
520
521 while (parms)
522 {
523 tree p = TREE_VALUE (parms);
524 int lvl = TMPL_PARMS_DEPTH (parms);
525 int arg_idx = 0;
526 int i;
527 tree lvl_args = NULL_TREE;
528
529 /* Don't crash if we had an invalid argument list. */
530 if (TMPL_ARGS_DEPTH (args) >= lvl)
531 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
532
533 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
534 {
535 tree arg = NULL_TREE;
536
537 /* Don't crash if we had an invalid argument list. */
538 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
539 arg = TREE_VEC_ELT (lvl_args, arg_idx);
540
541 tree parm_i = TREE_VEC_ELT (p, i);
542 /* If the template argument repeats the template parameter (T = T),
543 skip the parameter.*/
544 if (arg && TREE_CODE (arg) == TEMPLATE_TYPE_PARM
545 && TREE_CODE (parm_i) == TREE_LIST
546 && TREE_CODE (TREE_VALUE (parm_i)) == TYPE_DECL
547 && TREE_CODE (TREE_TYPE (TREE_VALUE (parm_i)))
548 == TEMPLATE_TYPE_PARM
549 && DECL_NAME (TREE_VALUE (parm_i))
550 == DECL_NAME (TREE_CHAIN (arg)))
551 continue;
552
553 semicolon_or_introducer ();
554 dump_template_parameter (pp, parm_i, TFF_PLAIN_IDENTIFIER);
555 pp_cxx_whitespace (pp);
556 pp_equal (pp);
557 pp_cxx_whitespace (pp);
558 if (arg)
559 {
560 if (ARGUMENT_PACK_P (arg))
561 pp_cxx_left_brace (pp);
562 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
563 if (ARGUMENT_PACK_P (arg))
564 pp_cxx_right_brace (pp);
565 }
566 else
567 pp_string (pp, M_("<missing>"));
568
569 ++arg_idx;
570 }
571
572 parms = TREE_CHAIN (parms);
573 }
574
575 /* Don't bother with typenames for a partial instantiation. */
576 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
577 return;
578
579 /* Don't try to print typenames when we're processing a clone. */
580 if (current_function_decl
581 && !DECL_LANG_SPECIFIC (current_function_decl))
582 return;
583
584 /* Don't try to do this once cgraph starts throwing away front-end
585 information. */
586 if (at_eof >= 3)
587 return;
588
589 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
590 {
591 semicolon_or_introducer ();
592 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
593 pp_cxx_whitespace (pp);
594 pp_equal (pp);
595 pp_cxx_whitespace (pp);
596 push_deferring_access_checks (dk_no_check);
597 t = tsubst (t, args, tf_none, NULL_TREE);
598 pop_deferring_access_checks ();
599 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
600 pp_simple_type_specifier doesn't know about it. */
601 t = strip_typedefs (t, NULL, STF_USER_VISIBLE);
602 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
603 }
604 }
605
606 /* Dump a human-readable equivalent of the alias template
607 specialization of T. */
608
609 static void
610 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
611 {
612 gcc_assert (alias_template_specialization_p (t, nt_opaque));
613
614 tree decl = TYPE_NAME (t);
615 if (!(flags & TFF_UNQUALIFIED_NAME))
616 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
617 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
618 dump_template_parms (pp, DECL_TEMPLATE_INFO (decl),
619 /*primary=*/false,
620 flags & ~TFF_TEMPLATE_HEADER);
621 }
622
623 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
624 format. */
625
626 static void
627 dump_type (cxx_pretty_printer *pp, tree t, int flags)
628 {
629 if (t == NULL_TREE)
630 return;
631
632 /* Don't print e.g. "struct mytypedef". */
633 if (TYPE_P (t) && typedef_variant_p (t))
634 {
635 tree decl = TYPE_NAME (t);
636 if ((flags & TFF_CHASE_TYPEDEF)
637 || DECL_SELF_REFERENCE_P (decl)
638 || (!flag_pretty_templates
639 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
640 {
641 unsigned int stf_flags = (!(pp->flags & pp_c_flag_gnu_v3)
642 ? STF_USER_VISIBLE : 0);
643 t = strip_typedefs (t, NULL, stf_flags);
644 }
645 else if (alias_template_specialization_p (t, nt_opaque))
646 {
647 dump_alias_template_specialization (pp, t, flags);
648 return;
649 }
650 else if (same_type_p (t, TREE_TYPE (decl)))
651 t = decl;
652 else
653 {
654 pp_cxx_cv_qualifier_seq (pp, t);
655 if (! (flags & TFF_UNQUALIFIED_NAME))
656 dump_scope (pp, CP_DECL_CONTEXT (decl), flags);
657 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
658 return;
659 }
660 }
661
662 if (TYPE_PTRMEMFUNC_P (t))
663 goto offset_type;
664
665 switch (TREE_CODE (t))
666 {
667 case LANG_TYPE:
668 if (t == init_list_type_node)
669 pp_string (pp, M_("<brace-enclosed initializer list>"));
670 else if (t == unknown_type_node)
671 pp_string (pp, M_("<unresolved overloaded function type>"));
672 else
673 {
674 pp_cxx_cv_qualifier_seq (pp, t);
675 if (tree id = TYPE_IDENTIFIER (t))
676 pp_cxx_tree_identifier (pp, id);
677 }
678 break;
679
680 case TREE_LIST:
681 /* A list of function parms. */
682 dump_parameters (pp, t, flags);
683 break;
684
685 case IDENTIFIER_NODE:
686 pp_cxx_tree_identifier (pp, t);
687 break;
688
689 case TREE_BINFO:
690 dump_type (pp, BINFO_TYPE (t), flags);
691 break;
692
693 case RECORD_TYPE:
694 case UNION_TYPE:
695 case ENUMERAL_TYPE:
696 dump_aggr_type (pp, t, flags);
697 break;
698
699 case TYPE_DECL:
700 if (flags & TFF_CHASE_TYPEDEF)
701 {
702 dump_type (pp, DECL_ORIGINAL_TYPE (t)
703 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
704 break;
705 }
706 /* Fall through. */
707
708 case TEMPLATE_DECL:
709 case NAMESPACE_DECL:
710 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
711 break;
712
713 case INTEGER_TYPE:
714 case REAL_TYPE:
715 case VOID_TYPE:
716 case OPAQUE_TYPE:
717 case BOOLEAN_TYPE:
718 case COMPLEX_TYPE:
719 case VECTOR_TYPE:
720 case FIXED_POINT_TYPE:
721 pp_type_specifier_seq (pp, t);
722 break;
723
724 case TEMPLATE_TEMPLATE_PARM:
725 /* For parameters inside template signature. */
726 if (TYPE_IDENTIFIER (t))
727 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
728 else
729 pp_cxx_canonical_template_parameter (pp, t);
730 break;
731
732 case BOUND_TEMPLATE_TEMPLATE_PARM:
733 {
734 tree args = TYPE_TI_ARGS (t);
735 pp_cxx_cv_qualifier_seq (pp, t);
736 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
737 pp_cxx_begin_template_argument_list (pp);
738 dump_template_argument_list (pp, args, flags);
739 pp_cxx_end_template_argument_list (pp);
740 }
741 break;
742
743 case TEMPLATE_TYPE_PARM:
744 pp_cxx_cv_qualifier_seq (pp, t);
745 if (template_placeholder_p (t))
746 {
747 tree tmpl = TREE_TYPE (CLASS_PLACEHOLDER_TEMPLATE (t));
748 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (tmpl));
749 pp_string (pp, "<...auto...>");
750 }
751 else if (TYPE_IDENTIFIER (t))
752 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
753 else
754 pp_cxx_canonical_template_parameter
755 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
756 /* If this is a constrained placeholder, add the requirements. */
757 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
758 pp_cxx_constrained_type_spec (pp, c);
759 break;
760
761 /* This is not always necessary for pointers and such, but doing this
762 reduces code size. */
763 case ARRAY_TYPE:
764 case POINTER_TYPE:
765 case REFERENCE_TYPE:
766 case OFFSET_TYPE:
767 offset_type:
768 case FUNCTION_TYPE:
769 case METHOD_TYPE:
770 {
771 dump_type_prefix (pp, t, flags);
772 dump_type_suffix (pp, t, flags);
773 break;
774 }
775 case TYPENAME_TYPE:
776 if (! (flags & TFF_CHASE_TYPEDEF)
777 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
778 {
779 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
780 break;
781 }
782 pp_cxx_cv_qualifier_seq (pp, t);
783 pp_cxx_ws_string (pp,
784 TYPENAME_IS_ENUM_P (t) ? "enum"
785 : TYPENAME_IS_CLASS_P (t) ? "class"
786 : "typename");
787 dump_typename (pp, t, flags);
788 break;
789
790 case UNBOUND_CLASS_TEMPLATE:
791 if (! (flags & TFF_UNQUALIFIED_NAME))
792 {
793 dump_type (pp, TYPE_CONTEXT (t), flags);
794 pp_cxx_colon_colon (pp);
795 }
796 pp_cxx_ws_string (pp, "template");
797 dump_type (pp, TYPE_IDENTIFIER (t), flags);
798 break;
799
800 case TYPEOF_TYPE:
801 pp_cxx_ws_string (pp, "__typeof__");
802 pp_cxx_whitespace (pp);
803 pp_cxx_left_paren (pp);
804 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
805 pp_cxx_right_paren (pp);
806 break;
807
808 case TRAIT_TYPE:
809 pp_cxx_trait (pp, t);
810 break;
811
812 case TYPE_PACK_EXPANSION:
813 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
814 pp_cxx_ws_string (pp, "...");
815 break;
816
817 case PACK_INDEX_TYPE:
818 dump_type (pp, PACK_INDEX_PACK (t), flags);
819 pp_cxx_left_bracket (pp);
820 dump_expr (pp, PACK_INDEX_INDEX (t), flags & ~TFF_EXPR_IN_PARENS);
821 pp_cxx_right_bracket (pp);
822 break;
823
824 case TYPE_ARGUMENT_PACK:
825 dump_template_argument (pp, t, flags);
826 break;
827
828 case DECLTYPE_TYPE:
829 pp_cxx_ws_string (pp, "decltype");
830 pp_cxx_whitespace (pp);
831 pp_cxx_left_paren (pp);
832 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
833 pp_cxx_right_paren (pp);
834 break;
835
836 case NULLPTR_TYPE:
837 pp_cxx_ws_string (pp, "std::nullptr_t");
838 break;
839
840 default:
841 pp_unsupported_tree (pp, t);
842 /* Fall through. */
843
844 case ERROR_MARK:
845 pp_string (pp, M_("<type error>"));
846 break;
847 }
848 }
849
850 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
851 a TYPENAME_TYPE. */
852
853 static void
854 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
855 {
856 tree ctx = TYPE_CONTEXT (t);
857
858 if (TREE_CODE (ctx) == TYPENAME_TYPE)
859 dump_typename (pp, ctx, flags);
860 else
861 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
862 pp_cxx_colon_colon (pp);
863 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
864 }
865
866 /* Return the name of the supplied aggregate, or enumeral type. */
867
868 const char *
869 class_key_or_enum_as_string (tree t)
870 {
871 if (TREE_CODE (t) == ENUMERAL_TYPE)
872 {
873 if (SCOPED_ENUM_P (t))
874 return "enum class";
875 else
876 return "enum";
877 }
878 else if (TREE_CODE (t) == UNION_TYPE)
879 return "union";
880 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
881 return "class";
882 else
883 return "struct";
884 }
885
886 /* Disable warnings about missing quoting in GCC diagnostics for
887 the pp_verbatim call. Their format strings deliberately don't
888 follow GCC diagnostic conventions. */
889 #if __GNUC__ >= 10
890 #pragma GCC diagnostic push
891 #pragma GCC diagnostic ignored "-Wformat-diag"
892 #endif
893
894 /* Print out a class declaration T under the control of FLAGS,
895 in the form `class foo'. */
896
897 static void
898 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
899 {
900 const char *variety = class_key_or_enum_as_string (t);
901 int typdef = 0;
902 int tmplate = 0;
903
904 pp_cxx_cv_qualifier_seq (pp, t);
905
906 if (flags & TFF_CLASS_KEY_OR_ENUM)
907 pp_cxx_ws_string (pp, variety);
908
909 tree decl = TYPE_NAME (t);
910
911 if (decl)
912 {
913 typdef = (!DECL_ARTIFICIAL (decl)
914 /* An alias specialization is not considered to be a
915 typedef. */
916 && !alias_template_specialization_p (t, nt_opaque));
917
918 if ((typdef
919 && ((flags & TFF_CHASE_TYPEDEF)
920 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (decl)
921 && DECL_TEMPLATE_INFO (decl))))
922 || DECL_SELF_REFERENCE_P (decl))
923 {
924 t = TYPE_MAIN_VARIANT (t);
925 decl = TYPE_NAME (t);
926 typdef = 0;
927 }
928
929 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
930 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
931 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
932 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
933
934 if (! (flags & TFF_UNQUALIFIED_NAME))
935 dump_scope (pp, CP_DECL_CONTEXT (decl), flags | TFF_SCOPE);
936 flags &= ~TFF_UNQUALIFIED_NAME;
937 if (tmplate)
938 {
939 /* Because the template names are mangled, we have to locate
940 the most general template, and use that name. */
941 tree tpl = TYPE_TI_TEMPLATE (t);
942
943 while (DECL_TEMPLATE_INFO (tpl))
944 tpl = DECL_TI_TEMPLATE (tpl);
945 decl = tpl;
946 }
947 }
948
949 if (LAMBDA_TYPE_P (t))
950 {
951 /* A lambda's "type" is essentially its signature. */
952 pp_string (pp, M_("<lambda"));
953 tree const fn = lambda_function (t);
954 if (fn)
955 {
956 int const parm_flags
957 = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? TFF_XOBJ_FUNC | flags
958 : flags;
959 dump_parameters (pp, FUNCTION_FIRST_USER_PARMTYPE (fn), parm_flags);
960 }
961 pp_greater (pp);
962 }
963 else if (!decl || IDENTIFIER_ANON_P (DECL_NAME (decl)))
964 {
965 if (flags & TFF_CLASS_KEY_OR_ENUM)
966 pp_string (pp, M_("<unnamed>"));
967 else
968 pp_printf (pp, M_("<unnamed %s>"), variety);
969 }
970 else
971 pp_cxx_tree_identifier (pp, DECL_NAME (decl));
972
973 dump_module_suffix (pp, decl);
974
975 if (tmplate)
976 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
977 !CLASSTYPE_USE_TEMPLATE (t),
978 flags & ~TFF_TEMPLATE_HEADER);
979 }
980
981 #if __GNUC__ >= 10
982 #pragma GCC diagnostic pop
983 #endif
984
985 /* Dump into the obstack the initial part of the output for a given type.
986 This is necessary when dealing with things like functions returning
987 functions. Examples:
988
989 return type of `int (* fee ())()': pointer -> function -> int. Both
990 pointer (and reference and offset) and function (and member) types must
991 deal with prefix and suffix.
992
993 Arrays must also do this for DECL nodes, like int a[], and for things like
994 int *[]&. */
995
996 static void
997 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
998 {
999 if (TYPE_PTRMEMFUNC_P (t))
1000 {
1001 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
1002 goto offset_type;
1003 }
1004
1005 switch (TREE_CODE (t))
1006 {
1007 case POINTER_TYPE:
1008 case REFERENCE_TYPE:
1009 {
1010 tree sub = TREE_TYPE (t);
1011
1012 dump_type_prefix (pp, sub, flags);
1013 if (TREE_CODE (sub) == ARRAY_TYPE
1014 || TREE_CODE (sub) == FUNCTION_TYPE)
1015 {
1016 pp_cxx_whitespace (pp);
1017 pp_cxx_left_paren (pp);
1018 /* If we're dealing with the GNU form of attributes, print this:
1019 void (__attribute__((noreturn)) *f) ();
1020 If it is the standard [[]] attribute, we'll print the attribute
1021 in dump_type_suffix. */
1022 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (sub)))
1023 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
1024 }
1025 if (TYPE_PTR_P (t))
1026 pp_star (pp);
1027 else if (TYPE_REF_P (t))
1028 {
1029 if (TYPE_REF_IS_RVALUE (t))
1030 pp_ampersand_ampersand (pp);
1031 else
1032 pp_ampersand (pp);
1033 }
1034 pp->set_padding (pp_before);
1035 pp_cxx_cv_qualifier_seq (pp, t);
1036 }
1037 break;
1038
1039 case OFFSET_TYPE:
1040 offset_type:
1041 dump_type_prefix (pp, TREE_TYPE (t), flags);
1042 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
1043 {
1044 pp_maybe_space (pp);
1045 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1046 pp_cxx_left_paren (pp);
1047 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
1048 pp_cxx_colon_colon (pp);
1049 }
1050 pp_cxx_star (pp);
1051 pp_cxx_cv_qualifier_seq (pp, t);
1052 pp->set_padding (pp_before);
1053 break;
1054
1055 /* This can be reached without a pointer when dealing with
1056 templates, e.g. std::is_function. */
1057 case FUNCTION_TYPE:
1058 dump_type_prefix (pp, TREE_TYPE (t), flags);
1059 break;
1060
1061 case METHOD_TYPE:
1062 dump_type_prefix (pp, TREE_TYPE (t), flags);
1063 pp_maybe_space (pp);
1064 pp_cxx_left_paren (pp);
1065 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
1066 pp_cxx_colon_colon (pp);
1067 break;
1068
1069 case ARRAY_TYPE:
1070 dump_type_prefix (pp, TREE_TYPE (t), flags);
1071 break;
1072
1073 case ENUMERAL_TYPE:
1074 case IDENTIFIER_NODE:
1075 case INTEGER_TYPE:
1076 case BOOLEAN_TYPE:
1077 case REAL_TYPE:
1078 case RECORD_TYPE:
1079 case TEMPLATE_TYPE_PARM:
1080 case TEMPLATE_TEMPLATE_PARM:
1081 case BOUND_TEMPLATE_TEMPLATE_PARM:
1082 case TREE_LIST:
1083 case TYPE_DECL:
1084 case TREE_VEC:
1085 case UNION_TYPE:
1086 case LANG_TYPE:
1087 case VOID_TYPE:
1088 case OPAQUE_TYPE:
1089 case TYPENAME_TYPE:
1090 case COMPLEX_TYPE:
1091 case VECTOR_TYPE:
1092 case TYPEOF_TYPE:
1093 case TRAIT_TYPE:
1094 case DECLTYPE_TYPE:
1095 case TYPE_PACK_EXPANSION:
1096 case FIXED_POINT_TYPE:
1097 case NULLPTR_TYPE:
1098 case PACK_INDEX_TYPE:
1099 dump_type (pp, t, flags);
1100 pp->set_padding (pp_before);
1101 break;
1102
1103 default:
1104 pp_unsupported_tree (pp, t);
1105 /* fall through. */
1106 case ERROR_MARK:
1107 pp_string (pp, M_("<typeprefixerror>"));
1108 break;
1109 }
1110 }
1111
1112 /* Dump the suffix of type T, under control of FLAGS. This is the part
1113 which appears after the identifier (or function parms). */
1114
1115 static void
1116 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
1117 {
1118 if (TYPE_PTRMEMFUNC_P (t))
1119 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
1120
1121 switch (TREE_CODE (t))
1122 {
1123 case POINTER_TYPE:
1124 case REFERENCE_TYPE:
1125 case OFFSET_TYPE:
1126 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1127 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1128 pp_cxx_right_paren (pp);
1129 if (TREE_CODE (t) == POINTER_TYPE)
1130 flags |= TFF_POINTER;
1131 dump_type_suffix (pp, TREE_TYPE (t), flags);
1132 break;
1133
1134 case FUNCTION_TYPE:
1135 case METHOD_TYPE:
1136 {
1137 tree arg;
1138 if (TREE_CODE (t) == METHOD_TYPE)
1139 /* Can only be reached through a pointer. */
1140 pp_cxx_right_paren (pp);
1141 arg = TYPE_ARG_TYPES (t);
1142 if (TREE_CODE (t) == METHOD_TYPE)
1143 arg = TREE_CHAIN (arg);
1144
1145 /* Function pointers don't have default args. Not in standard C++,
1146 anyway; they may in g++, but we'll just pretend otherwise. */
1147 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
1148
1149 pp->set_padding (pp_before);
1150 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t),
1151 TREE_CODE (t) == FUNCTION_TYPE
1152 && (flags & TFF_POINTER));
1153 dump_ref_qualifier (pp, t, flags);
1154 if (tx_safe_fn_type_p (t))
1155 pp_cxx_ws_string (pp, "transaction_safe");
1156 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
1157 /* If this is the standard [[]] attribute, print
1158 void (*)() [[noreturn]]; */
1159 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t)))
1160 {
1161 pp_space (pp);
1162 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (t));
1163 pp->set_padding (pp_before);
1164 }
1165 dump_type_suffix (pp, TREE_TYPE (t), flags);
1166 break;
1167 }
1168
1169 case ARRAY_TYPE:
1170 pp_maybe_space (pp);
1171 pp_cxx_left_bracket (pp);
1172 if (tree dtype = TYPE_DOMAIN (t))
1173 {
1174 tree max = TYPE_MAX_VALUE (dtype);
1175 /* Zero-length arrays have a null upper bound in C and SIZE_MAX
1176 in C++. Handle both since the type might be constructed by
1177 the middle end and end up here as a result of a warning (see
1178 PR c++/97201). */
1179 if (!max || integer_all_onesp (max))
1180 pp_character (pp, '0');
1181 else if (tree_fits_shwi_p (max))
1182 pp_wide_integer (pp, tree_to_shwi (max) + 1);
1183 else
1184 {
1185 STRIP_NOPS (max);
1186 if (TREE_CODE (max) == SAVE_EXPR)
1187 max = TREE_OPERAND (max, 0);
1188 if (TREE_CODE (max) == MINUS_EXPR
1189 || TREE_CODE (max) == PLUS_EXPR)
1190 {
1191 max = TREE_OPERAND (max, 0);
1192 while (CONVERT_EXPR_P (max))
1193 max = TREE_OPERAND (max, 0);
1194 }
1195 else
1196 max = fold_build2_loc (input_location,
1197 PLUS_EXPR, dtype, max,
1198 build_int_cst (dtype, 1));
1199 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
1200 }
1201 }
1202 pp_cxx_right_bracket (pp);
1203 dump_type_suffix (pp, TREE_TYPE (t), flags);
1204 break;
1205
1206 case ENUMERAL_TYPE:
1207 case IDENTIFIER_NODE:
1208 case INTEGER_TYPE:
1209 case BOOLEAN_TYPE:
1210 case REAL_TYPE:
1211 case RECORD_TYPE:
1212 case TEMPLATE_TYPE_PARM:
1213 case TEMPLATE_TEMPLATE_PARM:
1214 case BOUND_TEMPLATE_TEMPLATE_PARM:
1215 case TREE_LIST:
1216 case TYPE_DECL:
1217 case TREE_VEC:
1218 case UNION_TYPE:
1219 case LANG_TYPE:
1220 case VOID_TYPE:
1221 case OPAQUE_TYPE:
1222 case TYPENAME_TYPE:
1223 case COMPLEX_TYPE:
1224 case VECTOR_TYPE:
1225 case TYPEOF_TYPE:
1226 case TRAIT_TYPE:
1227 case DECLTYPE_TYPE:
1228 case TYPE_PACK_EXPANSION:
1229 case FIXED_POINT_TYPE:
1230 case NULLPTR_TYPE:
1231 case PACK_INDEX_TYPE:
1232 break;
1233
1234 default:
1235 pp_unsupported_tree (pp, t);
1236 case ERROR_MARK:
1237 /* Don't mark it here, we should have already done in
1238 dump_type_prefix. */
1239 break;
1240 }
1241 }
1242
1243 static void
1244 dump_global_iord (cxx_pretty_printer *pp, tree t)
1245 {
1246 const char *p = NULL;
1247
1248 if (DECL_GLOBAL_CTOR_P (t))
1249 p = M_("(static initializers for %s)");
1250 else if (DECL_GLOBAL_DTOR_P (t))
1251 p = M_("(static destructors for %s)");
1252 else
1253 gcc_unreachable ();
1254
1255 pp_printf (pp, p, DECL_SOURCE_FILE (t));
1256 }
1257
1258 static void
1259 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
1260 {
1261 if (VAR_P (t) && DECL_NTTP_OBJECT_P (t))
1262 return dump_expr (pp, DECL_INITIAL (t), flags);
1263
1264 if (flags & TFF_DECL_SPECIFIERS)
1265 {
1266 if (concept_definition_p (t))
1267 pp_cxx_ws_string (pp, "concept");
1268 else if (VAR_P (t) && DECL_DECLARED_CONSTEXPR_P (t))
1269 pp_cxx_ws_string (pp, "constexpr");
1270
1271 if (!concept_definition_p (t))
1272 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
1273 pp_maybe_space (pp);
1274 }
1275 if (! (flags & TFF_UNQUALIFIED_NAME)
1276 && TREE_CODE (t) != PARM_DECL
1277 && (!DECL_INITIAL (t)
1278 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
1279 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1280 flags &= ~TFF_UNQUALIFIED_NAME;
1281 if ((flags & TFF_DECL_SPECIFIERS)
1282 && DECL_TEMPLATE_PARM_P (t)
1283 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
1284 pp_string (pp, "...");
1285 if (DECL_NAME (t))
1286 {
1287 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
1288 {
1289 pp_less (pp);
1290 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
1291 pp_string (pp, " capture>");
1292 }
1293 else
1294 dump_decl (pp, DECL_NAME (t), flags);
1295 }
1296 else if (DECL_DECOMPOSITION_P (t))
1297 pp_string (pp, M_("<structured bindings>"));
1298 else if (TREE_CODE (t) == FIELD_DECL && DECL_FIELD_IS_BASE (t))
1299 dump_type (pp, TREE_TYPE (t), flags);
1300 else
1301 pp_string (pp, M_("<anonymous>"));
1302
1303 dump_module_suffix (pp, t);
1304
1305 if (flags & TFF_DECL_SPECIFIERS)
1306 dump_type_suffix (pp, type, flags);
1307 }
1308
1309 class colorize_guard
1310 {
1311 bool colorize;
1312 cxx_pretty_printer *pp;
1313 public:
1314 colorize_guard (bool _colorize, cxx_pretty_printer *pp, const char *name)
1315 : colorize (_colorize && pp_show_color (pp)), pp (pp)
1316 {
1317 pp_string (pp, colorize_start (colorize, name));
1318 }
1319 ~colorize_guard ()
1320 {
1321 pp_string (pp, colorize_stop (colorize));
1322 }
1323 };
1324
1325 /* Print an IDENTIFIER_NODE that is the name of a declaration. */
1326
1327 static void
1328 dump_decl_name (cxx_pretty_printer *pp, tree t, int flags)
1329 {
1330 /* These special cases are duplicated here so that other functions
1331 can feed identifiers to error and get them demangled properly. */
1332 if (IDENTIFIER_CONV_OP_P (t))
1333 {
1334 pp_cxx_ws_string (pp, "operator");
1335 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1336 dump_type (pp, TREE_TYPE (t), flags);
1337 return;
1338 }
1339 if (dguide_name_p (t))
1340 {
1341 dump_decl (pp, CLASSTYPE_TI_TEMPLATE (TREE_TYPE (t)),
1342 TFF_UNQUALIFIED_NAME);
1343 return;
1344 }
1345
1346 const char *str = IDENTIFIER_POINTER (t);
1347 if (startswith (str, "_ZGR"))
1348 {
1349 pp_cxx_ws_string (pp, "<temporary>");
1350 return;
1351 }
1352
1353 pp_cxx_tree_identifier (pp, t);
1354 }
1355
1356 /* Dump a human readable string for the decl T under control of FLAGS. */
1357
1358 static void
1359 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
1360 {
1361 if (t == NULL_TREE)
1362 return;
1363
1364 /* If doing Objective-C++, give Objective-C a chance to demangle
1365 Objective-C method names. */
1366 if (c_dialect_objc ())
1367 {
1368 const char *demangled = objc_maybe_printable_name (t, flags);
1369 if (demangled)
1370 {
1371 pp_string (pp, demangled);
1372 return;
1373 }
1374 }
1375
1376 switch (TREE_CODE (t))
1377 {
1378 case TYPE_DECL:
1379 /* Don't say 'typedef class A' */
1380 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
1381 {
1382 if ((flags & TFF_DECL_SPECIFIERS)
1383 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
1384 {
1385 /* Say `class T' not just `T'. */
1386 pp_cxx_ws_string (pp, "class");
1387
1388 /* Emit the `...' for a parameter pack. */
1389 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1390 pp_cxx_ws_string (pp, "...");
1391 }
1392
1393 dump_type (pp, TREE_TYPE (t), flags);
1394 break;
1395 }
1396 if (TYPE_DECL_ALIAS_P (t)
1397 && (flags & TFF_DECL_SPECIFIERS
1398 || flags & TFF_CLASS_KEY_OR_ENUM))
1399 {
1400 pp_cxx_ws_string (pp, "using");
1401 if (! (flags & TFF_UNQUALIFIED_NAME))
1402 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1403 dump_decl (pp, DECL_NAME (t), flags);
1404 pp_cxx_whitespace (pp);
1405 pp_cxx_ws_string (pp, "=");
1406 pp_cxx_whitespace (pp);
1407 dump_type (pp, (DECL_ORIGINAL_TYPE (t)
1408 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t)),
1409 flags);
1410 break;
1411 }
1412 if ((flags & TFF_DECL_SPECIFIERS)
1413 && !DECL_SELF_REFERENCE_P (t))
1414 pp_cxx_ws_string (pp, "typedef");
1415 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1416 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1417 flags);
1418 break;
1419
1420 case VAR_DECL:
1421 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1422 {
1423 pp_string (pp, M_("vtable for "));
1424 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1425 dump_type (pp, DECL_CONTEXT (t), flags);
1426 break;
1427 }
1428 /* Fall through. */
1429 case FIELD_DECL:
1430 case PARM_DECL:
1431 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1432
1433 /* Handle variable template specializations. */
1434 if (VAR_P (t)
1435 && DECL_LANG_SPECIFIC (t)
1436 && DECL_TEMPLATE_INFO (t)
1437 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1438 {
1439 pp_cxx_begin_template_argument_list (pp);
1440 tree args = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1441 dump_template_argument_list (pp, args, flags);
1442 pp_cxx_end_template_argument_list (pp);
1443 }
1444 break;
1445
1446 case RESULT_DECL:
1447 pp_string (pp, M_("<return value> "));
1448 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1449 break;
1450
1451 case NAMESPACE_DECL:
1452 if (flags & TFF_DECL_SPECIFIERS)
1453 pp->declaration (t);
1454 else
1455 {
1456 if (! (flags & TFF_UNQUALIFIED_NAME))
1457 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1458 flags &= ~TFF_UNQUALIFIED_NAME;
1459 if (DECL_NAME (t) == NULL_TREE)
1460 {
1461 if (!(pp->flags & pp_c_flag_gnu_v3))
1462 pp_cxx_ws_string (pp, M_("{anonymous}"));
1463 else
1464 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1465 }
1466 else
1467 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1468 }
1469 break;
1470
1471 case SCOPE_REF:
1472 dump_type (pp, TREE_OPERAND (t, 0), flags);
1473 pp_cxx_colon_colon (pp);
1474 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1475 break;
1476
1477 case ARRAY_REF:
1478 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1479 pp_cxx_left_bracket (pp);
1480 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1481 pp_cxx_right_bracket (pp);
1482 break;
1483
1484 /* So that we can do dump_decl on an aggr type. */
1485 case RECORD_TYPE:
1486 case UNION_TYPE:
1487 case ENUMERAL_TYPE:
1488 dump_type (pp, t, flags);
1489 break;
1490
1491 case BIT_NOT_EXPR:
1492 /* This is a pseudo destructor call which has not been folded into
1493 a PSEUDO_DTOR_EXPR yet. */
1494 pp_cxx_complement (pp);
1495 dump_type (pp, TREE_OPERAND (t, 0), flags);
1496 break;
1497
1498 case TYPE_EXPR:
1499 gcc_unreachable ();
1500 break;
1501
1502 case IDENTIFIER_NODE:
1503 dump_decl_name (pp, t, flags);
1504 break;
1505
1506 case OVERLOAD:
1507 if (!OVL_SINGLE_P (t))
1508 {
1509 tree ctx = ovl_scope (t);
1510 if (ctx != global_namespace)
1511 {
1512 if (TYPE_P (ctx))
1513 dump_type (pp, ctx, flags);
1514 else
1515 dump_decl (pp, ctx, flags);
1516 pp_cxx_colon_colon (pp);
1517 }
1518 dump_decl (pp, OVL_NAME (t), flags);
1519 break;
1520 }
1521
1522 /* If there's only one function, dump that. */
1523 return dump_decl (pp, OVL_FIRST (t), flags);
1524
1525 case FUNCTION_DECL:
1526 if (! DECL_LANG_SPECIFIC (t))
1527 {
1528 if (DECL_ABSTRACT_ORIGIN (t)
1529 && DECL_ABSTRACT_ORIGIN (t) != t)
1530 dump_decl (pp, DECL_ABSTRACT_ORIGIN (t), flags);
1531 else
1532 dump_function_name (pp, t, flags);
1533 }
1534 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1535 dump_global_iord (pp, t);
1536 else
1537 dump_function_decl (pp, t, flags);
1538 break;
1539
1540 case TEMPLATE_DECL:
1541 dump_template_decl (pp, t, flags);
1542 break;
1543
1544 case CONCEPT_DECL:
1545 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1546 break;
1547
1548 case TEMPLATE_ID_EXPR:
1549 {
1550 tree name = TREE_OPERAND (t, 0);
1551 tree args = TREE_OPERAND (t, 1);
1552
1553 if (!identifier_p (name))
1554 name = OVL_NAME (name);
1555 dump_decl (pp, name, flags);
1556 pp_cxx_begin_template_argument_list (pp);
1557 if (args == error_mark_node)
1558 pp_string (pp, M_("<template arguments error>"));
1559 else if (args)
1560 dump_template_argument_list
1561 (pp, args, flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
1562 pp_cxx_end_template_argument_list (pp);
1563 }
1564 break;
1565
1566 case LABEL_DECL:
1567 if (DECL_NAME (t))
1568 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1569 else
1570 dump_generic_node (pp, t, 0, TDF_SLIM, false);
1571 break;
1572
1573 case CONST_DECL:
1574 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1575 || (DECL_INITIAL (t) &&
1576 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1577 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1578 else if (DECL_NAME (t))
1579 dump_decl (pp, DECL_NAME (t), flags);
1580 else if (DECL_INITIAL (t))
1581 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1582 else
1583 pp_string (pp, M_("<enumerator>"));
1584 break;
1585
1586 case USING_DECL:
1587 {
1588 if (flags & TFF_DECL_SPECIFIERS)
1589 pp_cxx_ws_string (pp, "using");
1590 bool variadic = false;
1591 if (!(flags & TFF_UNQUALIFIED_NAME))
1592 {
1593 tree scope = USING_DECL_SCOPE (t);
1594 tree name = DECL_NAME (t);
1595 if (PACK_EXPANSION_P (scope))
1596 {
1597 scope = PACK_EXPANSION_PATTERN (scope);
1598 variadic = true;
1599 }
1600 if (identifier_p (name)
1601 && IDENTIFIER_CONV_OP_P (name)
1602 && PACK_EXPANSION_P (TREE_TYPE (name)))
1603 {
1604 name = make_conv_op_name (PACK_EXPANSION_PATTERN
1605 (TREE_TYPE (name)));
1606 variadic = true;
1607 }
1608 dump_type (pp, scope, flags);
1609 pp_cxx_colon_colon (pp);
1610 }
1611 dump_decl (pp, DECL_NAME (t), flags);
1612 if (variadic)
1613 pp_cxx_ws_string (pp, "...");
1614 }
1615 break;
1616
1617 case STATIC_ASSERT:
1618 pp->declaration (t);
1619 break;
1620
1621 case BASELINK:
1622 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1623 break;
1624
1625 case TEMPLATE_TYPE_PARM:
1626 if (flags & TFF_DECL_SPECIFIERS)
1627 pp->declaration (t);
1628 else
1629 pp->type_id (t);
1630 break;
1631
1632 case UNBOUND_CLASS_TEMPLATE:
1633 case TYPE_PACK_EXPANSION:
1634 case TREE_BINFO:
1635 dump_type (pp, t, flags);
1636 break;
1637
1638 default:
1639 pp_unsupported_tree (pp, t);
1640 /* Fall through. */
1641
1642 case ERROR_MARK:
1643 pp_string (pp, M_("<declaration error>"));
1644 break;
1645 }
1646 }
1647
1648 /* Dump a template declaration T under control of FLAGS. This means the
1649 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1650
1651 static void
1652 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1653 {
1654 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1655 tree parms;
1656 int i;
1657
1658 if (flags & TFF_TEMPLATE_HEADER)
1659 {
1660 for (parms = orig_parms = nreverse (orig_parms);
1661 parms;
1662 parms = TREE_CHAIN (parms))
1663 {
1664 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1665 int len = TREE_VEC_LENGTH (inner_parms);
1666
1667 if (len == 0)
1668 {
1669 /* Skip over the dummy template levels of a template template
1670 parm. */
1671 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TEMPLATE_PARM);
1672 continue;
1673 }
1674
1675 pp_cxx_ws_string (pp, "template");
1676 pp_cxx_begin_template_argument_list (pp);
1677
1678 /* If we've shown the template prefix, we'd better show the
1679 parameters' and decl's type too. */
1680 flags |= TFF_DECL_SPECIFIERS;
1681
1682 for (i = 0; i < len; i++)
1683 {
1684 if (i)
1685 pp_separate_with_comma (pp);
1686 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1687 flags);
1688 }
1689 pp_cxx_end_template_argument_list (pp);
1690 pp_cxx_whitespace (pp);
1691 }
1692 nreverse(orig_parms);
1693
1694 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1695 {
1696 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1697 pp_cxx_ws_string (pp, "class");
1698
1699 /* If this is a parameter pack, print the ellipsis. */
1700 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1701 pp_cxx_ws_string (pp, "...");
1702 }
1703
1704 /* Only print the requirements if we're also printing
1705 the template header. */
1706 if (flag_concepts)
1707 if (tree ci = get_constraints (t))
1708 if (check_constraint_info (ci))
1709 if (tree reqs = CI_TEMPLATE_REQS (ci))
1710 {
1711 pp_cxx_requires_clause (pp, reqs);
1712 pp_cxx_whitespace (pp);
1713 }
1714 }
1715
1716
1717 if (DECL_CLASS_TEMPLATE_P (t))
1718 dump_type (pp, TREE_TYPE (t),
1719 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1720 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1721 else if (DECL_TEMPLATE_RESULT (t)
1722 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1723 /* Alias template. */
1724 || DECL_TYPE_TEMPLATE_P (t)
1725 /* Concept definition. &*/
1726 || TREE_CODE (DECL_TEMPLATE_RESULT (t)) == CONCEPT_DECL))
1727 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1728 else
1729 {
1730 gcc_assert (TREE_TYPE (t));
1731 switch (NEXT_CODE (t))
1732 {
1733 case METHOD_TYPE:
1734 case FUNCTION_TYPE:
1735 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1736 break;
1737 default:
1738 /* This case can occur with some invalid code. */
1739 dump_type (pp, TREE_TYPE (t),
1740 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1741 | (flags & TFF_DECL_SPECIFIERS
1742 ? TFF_CLASS_KEY_OR_ENUM : 0));
1743 }
1744 }
1745 }
1746
1747 /* find_typenames looks through the type of the function template T
1748 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1749 it finds. */
1750
1751 struct find_typenames_t
1752 {
1753 hash_set<tree> *p_set;
1754 vec<tree, va_gc> *typenames;
1755 };
1756
1757 static tree
1758 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1759 {
1760 struct find_typenames_t *d = (struct find_typenames_t *)data;
1761 tree mv = NULL_TREE;
1762
1763 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1764 /* Add the type of the typedef without any additional cv-quals. */
1765 mv = TREE_TYPE (TYPE_NAME (*tp));
1766 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1767 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1768 /* Add the typename without any cv-qualifiers. */
1769 mv = TYPE_MAIN_VARIANT (*tp);
1770
1771 if (PACK_EXPANSION_P (*tp))
1772 {
1773 /* Don't mess with parameter packs since we don't remember
1774 the pack expansion context for a particular typename. */
1775 *walk_subtrees = false;
1776 return NULL_TREE;
1777 }
1778
1779 if (mv && (mv == *tp || !d->p_set->add (mv)))
1780 vec_safe_push (d->typenames, mv);
1781
1782 return NULL_TREE;
1783 }
1784
1785 static vec<tree, va_gc> *
1786 find_typenames (tree t)
1787 {
1788 struct find_typenames_t ft;
1789 ft.p_set = new hash_set<tree>;
1790 ft.typenames = NULL;
1791 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1792 find_typenames_r, &ft, ft.p_set);
1793 delete ft.p_set;
1794 return ft.typenames;
1795 }
1796
1797 /* Output the "[with ...]" clause for a template instantiation T iff
1798 TEMPLATE_PARMS, TEMPLATE_ARGS and FLAGS are suitable. T may be NULL if
1799 formatting a deduction/substitution diagnostic rather than an
1800 instantiation. */
1801
1802 static void
1803 dump_substitution (cxx_pretty_printer *pp,
1804 tree t, tree template_parms, tree template_args,
1805 int flags)
1806 {
1807 if (template_parms != NULL_TREE && template_args != NULL_TREE
1808 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1809 {
1810 vec<tree, va_gc> *typenames = t ? find_typenames (t) : NULL;
1811 dump_template_bindings (pp, template_parms, template_args, typenames);
1812 }
1813 }
1814
1815 /* Dump the lambda function FN including its 'mutable' qualifier and any
1816 template bindings. */
1817
1818 static void
1819 dump_lambda_function (cxx_pretty_printer *pp,
1820 tree fn, tree template_parms, tree template_args,
1821 int flags)
1822 {
1823 /* A lambda's signature is essentially its "type". */
1824 dump_type (pp, DECL_CONTEXT (fn), flags);
1825 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
1826 /* Early escape. */;
1827 else if (TREE_CODE (TREE_TYPE (fn)) == FUNCTION_TYPE)
1828 {
1829 pp->set_padding (pp_before);
1830 pp_c_ws_string (pp, "static");
1831 }
1832 else if (!(TYPE_QUALS (class_of_this_parm (TREE_TYPE (fn)))
1833 & TYPE_QUAL_CONST))
1834 {
1835 pp->set_padding (pp_before);
1836 pp_c_ws_string (pp, "mutable");
1837 }
1838 dump_substitution (pp, fn, template_parms, template_args, flags);
1839 }
1840
1841 /* Pretty print a function decl. There are several ways we want to print a
1842 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1843 As error can only apply the '#' flag once to give 0 and 1 for V, there
1844 is %D which doesn't print the throw specs, and %F which does. */
1845
1846 static void
1847 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1848 {
1849 tree fntype;
1850 tree parmtypes;
1851 tree cname = NULL_TREE;
1852 tree template_args = NULL_TREE;
1853 tree template_parms = NULL_TREE;
1854 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1855 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1856 tree exceptions;
1857 bool constexpr_p;
1858 tree ret = NULL_TREE;
1859
1860 int dump_function_name_flags = flags & ~TFF_UNQUALIFIED_NAME;
1861 flags = dump_function_name_flags & ~TFF_TEMPLATE_NAME;
1862 if (TREE_CODE (t) == TEMPLATE_DECL)
1863 t = DECL_TEMPLATE_RESULT (t);
1864
1865 /* Save the exceptions, in case t is a specialization and we are
1866 emitting an error about incompatible specifications. */
1867 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1868
1869 /* Likewise for the constexpr specifier, in case t is a specialization. */
1870 constexpr_p = (DECL_DECLARED_CONSTEXPR_P (t)
1871 && !decl_implicit_constexpr_p (t));
1872
1873 /* Pretty print template instantiations only. */
1874 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1875 && !(flags & TFF_NO_TEMPLATE_BINDINGS)
1876 && flag_pretty_templates)
1877 {
1878 tree tmpl;
1879
1880 template_args = DECL_TI_ARGS (t);
1881 tmpl = most_general_template (t);
1882 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1883 {
1884 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1885 t = tmpl;
1886 }
1887 }
1888
1889 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1890 return dump_lambda_function (pp, t, template_parms, template_args, flags);
1891
1892 fntype = TREE_TYPE (t);
1893 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1894
1895 if (DECL_CLASS_SCOPE_P (t))
1896 cname = DECL_CONTEXT (t);
1897 /* This is for partially instantiated template methods. */
1898 else if (TREE_CODE (fntype) == METHOD_TYPE)
1899 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1900
1901 if (flags & TFF_DECL_SPECIFIERS)
1902 {
1903 if (DECL_STATIC_FUNCTION_P (t))
1904 pp_cxx_ws_string (pp, "static");
1905 else if (DECL_VIRTUAL_P (t))
1906 pp_cxx_ws_string (pp, "virtual");
1907
1908 if (constexpr_p)
1909 {
1910 if (DECL_IMMEDIATE_FUNCTION_P (t))
1911 pp_cxx_ws_string (pp, "consteval");
1912 else
1913 pp_cxx_ws_string (pp, "constexpr");
1914 }
1915 }
1916
1917 /* Print the return type? */
1918 if (show_return)
1919 show_return = (!DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1920 && !DECL_DESTRUCTOR_P (t) && !deduction_guide_p (t));
1921 if (show_return)
1922 {
1923 ret = fndecl_declared_return_type (t);
1924 dump_type_prefix (pp, ret, flags);
1925 }
1926
1927 /* Print the function name. */
1928 if (!do_outer_scope)
1929 /* Nothing. */;
1930 else if (cname)
1931 {
1932 dump_type (pp, cname, flags);
1933 pp_cxx_colon_colon (pp);
1934 }
1935 else
1936 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1937
1938 /* Name lookup for the rest of the function declarator is implicitly in the
1939 scope of the function, so avoid printing redundant scope qualifiers. */
1940 auto cds = make_temp_override (current_dump_scope, CP_DECL_CONTEXT (t));
1941
1942 dump_function_name (pp, t, dump_function_name_flags);
1943
1944 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1945 {
1946 int const parm_flags
1947 = DECL_XOBJ_MEMBER_FUNCTION_P (t) ? TFF_XOBJ_FUNC | flags : flags;
1948 dump_parameters (pp, parmtypes, parm_flags);
1949
1950 if (TREE_CODE (fntype) == METHOD_TYPE)
1951 {
1952 pp->set_padding (pp_before);
1953 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1954 dump_ref_qualifier (pp, fntype, flags);
1955 }
1956
1957 if (tx_safe_fn_type_p (fntype))
1958 {
1959 pp->set_padding (pp_before);
1960 pp_cxx_ws_string (pp, "transaction_safe");
1961 }
1962
1963 if (flags & TFF_EXCEPTION_SPECIFICATION)
1964 {
1965 pp->set_padding (pp_before);
1966 dump_exception_spec (pp, exceptions, flags);
1967 }
1968
1969 if (show_return)
1970 dump_type_suffix (pp, ret, flags);
1971 else if (deduction_guide_p (t))
1972 {
1973 pp->set_padding (pp_before);
1974 pp_cxx_ws_string (pp, "->");
1975 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1976 }
1977
1978 if (flag_concepts)
1979 if (tree ci = get_constraints (t))
1980 if (tree reqs = CI_DECLARATOR_REQS (ci))
1981 pp_cxx_requires_clause (pp, reqs);
1982
1983 dump_substitution (pp, t, template_parms, template_args, flags);
1984
1985 if (tree base = DECL_INHERITED_CTOR_BASE (t))
1986 {
1987 pp_cxx_ws_string (pp, "[inherited from");
1988 dump_type (pp, base, TFF_PLAIN_IDENTIFIER);
1989 pp_character (pp, ']');
1990 }
1991 }
1992 else if (template_args)
1993 {
1994 bool need_comma = false;
1995 int i;
1996 pp_cxx_begin_template_argument_list (pp);
1997 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1998 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1999 {
2000 tree arg = TREE_VEC_ELT (template_args, i);
2001 if (need_comma)
2002 pp_separate_with_comma (pp);
2003 if (ARGUMENT_PACK_P (arg))
2004 pp_cxx_left_brace (pp);
2005 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
2006 if (ARGUMENT_PACK_P (arg))
2007 pp_cxx_right_brace (pp);
2008 need_comma = true;
2009 }
2010 pp_cxx_end_template_argument_list (pp);
2011 }
2012 }
2013
2014 /* Print a parameter list. If this is for a member function, the
2015 member object ptr (and any other hidden args) should have
2016 already been removed. */
2017
2018 static void
2019 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
2020 {
2021 int first = 1;
2022 flags &= ~TFF_SCOPE;
2023 pp_cxx_left_paren (pp);
2024
2025 for (first = 1; parmtypes != void_list_node;
2026 parmtypes = TREE_CHAIN (parmtypes))
2027 {
2028 if (first && flags & TFF_XOBJ_FUNC)
2029 pp_string (pp, "this ");
2030 if (!first)
2031 pp_separate_with_comma (pp);
2032 first = 0;
2033 if (!parmtypes)
2034 {
2035 pp_cxx_ws_string (pp, "...");
2036 break;
2037 }
2038
2039 dump_type (pp, TREE_VALUE (parmtypes), flags);
2040
2041 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
2042 {
2043 pp_cxx_whitespace (pp);
2044 pp_equal (pp);
2045 pp_cxx_whitespace (pp);
2046 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
2047 }
2048 }
2049
2050 pp_cxx_right_paren (pp);
2051 }
2052
2053 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
2054
2055 static void
2056 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
2057 {
2058 if (FUNCTION_REF_QUALIFIED (t))
2059 {
2060 pp->set_padding (pp_before);
2061 if (FUNCTION_RVALUE_QUALIFIED (t))
2062 pp_cxx_ws_string (pp, "&&");
2063 else
2064 pp_cxx_ws_string (pp, "&");
2065 }
2066 }
2067
2068 /* Print an exception specification. T is the exception specification. */
2069
2070 static void
2071 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
2072 {
2073 if (t && TREE_PURPOSE (t))
2074 {
2075 pp_cxx_ws_string (pp, "noexcept");
2076 if (!integer_onep (TREE_PURPOSE (t)))
2077 {
2078 pp_cxx_whitespace (pp);
2079 pp_cxx_left_paren (pp);
2080 if (DEFERRED_NOEXCEPT_SPEC_P (t))
2081 pp_cxx_ws_string (pp, "<uninstantiated>");
2082 else
2083 dump_expr (pp, TREE_PURPOSE (t), flags);
2084 pp_cxx_right_paren (pp);
2085 }
2086 }
2087 else if (t)
2088 {
2089 pp_cxx_ws_string (pp, "throw");
2090 pp_cxx_whitespace (pp);
2091 pp_cxx_left_paren (pp);
2092 if (TREE_VALUE (t) != NULL_TREE)
2093 while (1)
2094 {
2095 dump_type (pp, TREE_VALUE (t), flags);
2096 t = TREE_CHAIN (t);
2097 if (!t)
2098 break;
2099 pp_separate_with_comma (pp);
2100 }
2101 pp_cxx_right_paren (pp);
2102 }
2103 }
2104
2105 /* Handle the function name for a FUNCTION_DECL node, grokking operators
2106 and destructors properly. */
2107
2108 static void
2109 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
2110 {
2111 /* Only colorize when we're printing something before the name; in
2112 particular, not when printing a CALL_EXPR. */
2113 bool colorize = flags & (TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE
2114 | TFF_TEMPLATE_HEADER);
2115
2116 colorize_guard g (colorize, pp, "fnname");
2117
2118 tree name = DECL_NAME (t);
2119
2120 /* We can get here with a decl that was synthesized by language-
2121 independent machinery (e.g. coverage.cc) in which case it won't
2122 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
2123 will crash. In this case it is safe just to print out the
2124 literal name. */
2125 if (!DECL_LANG_SPECIFIC (t))
2126 {
2127 pp_cxx_tree_identifier (pp, name);
2128 return;
2129 }
2130
2131 if (TREE_CODE (t) == TEMPLATE_DECL)
2132 t = DECL_TEMPLATE_RESULT (t);
2133
2134 /* Don't let the user see __comp_ctor et al. */
2135 if (DECL_CONSTRUCTOR_P (t)
2136 || DECL_DESTRUCTOR_P (t))
2137 {
2138 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
2139 name = get_identifier ("<lambda>");
2140 else if (TYPE_UNNAMED_P (DECL_CONTEXT (t)))
2141 name = get_identifier ("<constructor>");
2142 else
2143 name = constructor_name (DECL_CONTEXT (t));
2144 }
2145
2146 if (DECL_DESTRUCTOR_P (t))
2147 {
2148 pp_cxx_complement (pp);
2149 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
2150 }
2151 else if (DECL_CONV_FN_P (t))
2152 {
2153 /* This cannot use the hack that the operator's return
2154 type is stashed off of its name because it may be
2155 used for error reporting. In the case of conflicting
2156 declarations, both will have the same name, yet
2157 the types will be different, hence the TREE_TYPE field
2158 of the first name will be clobbered by the second. */
2159 pp_cxx_ws_string (pp, "operator");
2160 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2161 }
2162 else
2163 dump_decl (pp, name, flags);
2164
2165 dump_module_suffix (pp, t);
2166
2167 if (DECL_TEMPLATE_INFO (t)
2168 && !(flags & TFF_TEMPLATE_NAME)
2169 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
2170 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
2171 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
2172 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
2173 flags);
2174 }
2175
2176 /* Dump the template parameters from the template info INFO under control of
2177 FLAGS. PRIMARY indicates whether this is a primary template decl, or
2178 specialization (partial or complete). For partial specializations we show
2179 the specialized parameter values. For a primary template we show no
2180 decoration. */
2181
2182 static void
2183 dump_template_parms (cxx_pretty_printer *pp, tree info,
2184 int primary, int flags)
2185 {
2186 tree args = info ? TI_ARGS (info) : NULL_TREE;
2187
2188 if (primary && flags & TFF_TEMPLATE_NAME)
2189 return;
2190 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
2191 pp_cxx_begin_template_argument_list (pp);
2192
2193 /* Be careful only to print things when we have them, so as not
2194 to crash producing error messages. */
2195 if (args && !primary)
2196 {
2197 int len, ix;
2198 len = get_non_default_template_args_count (args, flags);
2199
2200 args = INNERMOST_TEMPLATE_ARGS (args);
2201 for (ix = 0; ix != len; ix++)
2202 {
2203 tree arg = TREE_VEC_ELT (args, ix);
2204
2205 /* Only print a comma if we know there is an argument coming. In
2206 the case of an empty template argument pack, no actual
2207 argument will be printed. */
2208 if (ix
2209 && (!ARGUMENT_PACK_P (arg)
2210 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
2211 pp_separate_with_comma (pp);
2212
2213 if (!arg)
2214 pp_string (pp, M_("<template parameter error>"));
2215 else
2216 dump_template_argument (pp, arg, flags);
2217 }
2218 }
2219 else if (primary)
2220 {
2221 tree tpl = TI_TEMPLATE (info);
2222 tree parms = DECL_TEMPLATE_PARMS (tpl);
2223 int len, ix;
2224
2225 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
2226 len = parms ? TREE_VEC_LENGTH (parms) : 0;
2227
2228 for (ix = 0; ix != len; ix++)
2229 {
2230 tree parm;
2231
2232 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
2233 {
2234 pp_string (pp, M_("<template parameter error>"));
2235 continue;
2236 }
2237
2238 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
2239
2240 if (ix)
2241 pp_separate_with_comma (pp);
2242
2243 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
2244 }
2245 }
2246 pp_cxx_end_template_argument_list (pp);
2247 }
2248
2249 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
2250 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
2251
2252 static void
2253 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
2254 {
2255 const int len = call_expr_nargs (t);
2256
2257 pp_cxx_left_paren (pp);
2258 for (int i = skipfirst; i < len; ++i)
2259 {
2260 tree arg = get_nth_callarg (t, i);
2261 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
2262 if (i + 1 < len)
2263 pp_separate_with_comma (pp);
2264 }
2265 pp_cxx_right_paren (pp);
2266 }
2267
2268 /* Print out a list of initializers (subr of dump_expr). */
2269
2270 static void
2271 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
2272 {
2273 while (l)
2274 {
2275 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
2276 l = TREE_CHAIN (l);
2277 if (l)
2278 pp_separate_with_comma (pp);
2279 }
2280 }
2281
2282 /* Print out a vector of initializers (subr of dump_expr). */
2283
2284 static void
2285 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
2286 int flags)
2287 {
2288 unsigned HOST_WIDE_INT idx;
2289 tree value;
2290
2291 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
2292 {
2293 if (TREE_CODE (value) == RAW_DATA_CST)
2294 for (unsigned i = 0; i < (unsigned) RAW_DATA_LENGTH (value); ++i)
2295 {
2296 if (TYPE_UNSIGNED (TREE_TYPE (value))
2297 || TYPE_PRECISION (TREE_TYPE (value)) > CHAR_BIT)
2298 pp_decimal_int (pp, RAW_DATA_UCHAR_ELT (value, i));
2299 else
2300 pp_decimal_int (pp, RAW_DATA_SCHAR_ELT (value, i));
2301 if (i == RAW_DATA_LENGTH (value) - 1U)
2302 break;
2303 else if (i == 9 && RAW_DATA_LENGTH (value) > 20)
2304 {
2305 pp_string (pp, ", ..., ");
2306 i = RAW_DATA_LENGTH (value) - 11;
2307 }
2308 else
2309 pp_separate_with_comma (pp);
2310 }
2311 else
2312 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
2313 if (idx != v->length () - 1)
2314 pp_separate_with_comma (pp);
2315 }
2316 }
2317
2318
2319 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
2320 function. Resolve it to a close relative -- in the sense of static
2321 type -- variant being overridden. That is close to what was written in
2322 the source code. Subroutine of dump_expr. */
2323
2324 static tree
2325 resolve_virtual_fun_from_obj_type_ref (tree ref)
2326 {
2327 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_TOKEN (ref));
2328 HOST_WIDE_INT index = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2329 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
2330 while (index)
2331 {
2332 fun = TREE_CHAIN (fun);
2333 index -= (TARGET_VTABLE_USES_DESCRIPTORS
2334 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
2335 }
2336
2337 return BV_FN (fun);
2338 }
2339
2340 /* Print out an expression E under control of FLAGS. */
2341
2342 static void
2343 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
2344 {
2345 tree op;
2346
2347 if (t == 0)
2348 return;
2349
2350 if (STATEMENT_CLASS_P (t))
2351 {
2352 pp_cxx_ws_string (pp, M_("<statement>"));
2353 return;
2354 }
2355
2356 switch (TREE_CODE (t))
2357 {
2358 case VAR_DECL:
2359 case PARM_DECL:
2360 case FIELD_DECL:
2361 case CONST_DECL:
2362 case FUNCTION_DECL:
2363 case TEMPLATE_DECL:
2364 case NAMESPACE_DECL:
2365 case LABEL_DECL:
2366 case OVERLOAD:
2367 case TYPE_DECL:
2368 case USING_DECL:
2369 case IDENTIFIER_NODE:
2370 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
2371 |TFF_TEMPLATE_HEADER))
2372 | TFF_NO_TEMPLATE_BINDINGS
2373 | TFF_NO_FUNCTION_ARGUMENTS));
2374 break;
2375
2376 case SSA_NAME:
2377 if (SSA_NAME_VAR (t)
2378 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
2379 dump_expr (pp, SSA_NAME_VAR (t), flags);
2380 else
2381 pp_cxx_ws_string (pp, M_("<unknown>"));
2382 break;
2383
2384 case VOID_CST:
2385 case INTEGER_CST:
2386 case REAL_CST:
2387 case STRING_CST:
2388 case COMPLEX_CST:
2389 pp->constant (t);
2390 break;
2391
2392 case USERDEF_LITERAL:
2393 pp_cxx_userdef_literal (pp, t);
2394 break;
2395
2396 case THROW_EXPR:
2397 /* While waiting for caret diagnostics, avoid printing
2398 __cxa_allocate_exception, __cxa_throw, and the like. */
2399 pp_cxx_ws_string (pp, M_("<throw-expression>"));
2400 break;
2401
2402 case PTRMEM_CST:
2403 pp_ampersand (pp);
2404 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
2405 pp_cxx_colon_colon (pp);
2406 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
2407 break;
2408
2409 case COMPOUND_EXPR:
2410 pp_cxx_left_paren (pp);
2411 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2412 pp_separate_with_comma (pp);
2413 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2414 pp_cxx_right_paren (pp);
2415 break;
2416
2417 case COND_EXPR:
2418 case VEC_COND_EXPR:
2419 pp_cxx_left_paren (pp);
2420 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2421 pp_string (pp, " ? ");
2422 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2423 pp_string (pp, " : ");
2424 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
2425 pp_cxx_right_paren (pp);
2426 break;
2427
2428 case SAVE_EXPR:
2429 if (TREE_HAS_CONSTRUCTOR (t))
2430 {
2431 pp_cxx_ws_string (pp, "new");
2432 pp_cxx_whitespace (pp);
2433 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
2434 }
2435 else
2436 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2437 break;
2438
2439 case AGGR_INIT_EXPR:
2440 case CALL_EXPR:
2441 {
2442 tree fn = cp_get_callee (t);
2443 bool skipfirst = false;
2444
2445 /* Deal with internal functions. */
2446 if (fn == NULL_TREE)
2447 {
2448 pp_string (pp, internal_fn_name (CALL_EXPR_IFN (t)));
2449 dump_call_expr_args (pp, t, flags, skipfirst);
2450 break;
2451 }
2452
2453 if (TREE_CODE (fn) == ADDR_EXPR)
2454 fn = TREE_OPERAND (fn, 0);
2455
2456 /* Nobody is interested in seeing the guts of vcalls. */
2457 if (TREE_CODE (fn) == OBJ_TYPE_REF)
2458 fn = resolve_virtual_fun_from_obj_type_ref (fn);
2459
2460 if (TREE_TYPE (fn) != NULL_TREE
2461 && NEXT_CODE (fn) == METHOD_TYPE
2462 && call_expr_nargs (t))
2463 {
2464 tree ob = get_nth_callarg (t, 0);
2465 if (is_dummy_object (ob))
2466 /* Don't print dummy object. */;
2467 else if (TREE_CODE (ob) == ADDR_EXPR)
2468 {
2469 dump_expr (pp, TREE_OPERAND (ob, 0),
2470 flags | TFF_EXPR_IN_PARENS);
2471 pp_cxx_dot (pp);
2472 }
2473 else if (!is_this_parameter (ob))
2474 {
2475 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2476 pp_cxx_arrow (pp);
2477 }
2478 skipfirst = true;
2479 }
2480 if (flag_sanitize & SANITIZE_UNDEFINED
2481 && is_ubsan_builtin_p (fn))
2482 {
2483 pp_string (cxx_pp, M_("<ubsan routine call>"));
2484 break;
2485 }
2486
2487 if (TREE_CODE (fn) == FUNCTION_DECL
2488 && DECL_CONSTRUCTOR_P (fn)
2489 && is_dummy_object (get_nth_callarg (t, 0)))
2490 dump_type (pp, DECL_CONTEXT (fn), flags);
2491 else
2492 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
2493 dump_call_expr_args (pp, t, flags, skipfirst);
2494 }
2495 break;
2496
2497 case TARGET_EXPR:
2498 /* Note that this only works for G++ target exprs. If somebody
2499 builds a general TARGET_EXPR, there's no way to represent that
2500 it initializes anything other that the parameter slot for the
2501 default argument. Note we may have cleared out the first
2502 operand in expand_expr, so don't go killing ourselves. */
2503 if (TARGET_EXPR_INITIAL (t))
2504 dump_expr (pp, TARGET_EXPR_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
2505 break;
2506
2507 case POINTER_PLUS_EXPR:
2508 dump_binary_op (pp, "+", t, flags);
2509 break;
2510
2511 case POINTER_DIFF_EXPR:
2512 dump_binary_op (pp, "-", t, flags);
2513 break;
2514
2515 case INIT_EXPR:
2516 case MODIFY_EXPR:
2517 dump_binary_op (pp, OVL_OP_INFO (true, NOP_EXPR)->name, t, flags);
2518 break;
2519
2520 case PLUS_EXPR:
2521 case MINUS_EXPR:
2522 case MULT_EXPR:
2523 case TRUNC_DIV_EXPR:
2524 case TRUNC_MOD_EXPR:
2525 case MIN_EXPR:
2526 case MAX_EXPR:
2527 case LSHIFT_EXPR:
2528 case RSHIFT_EXPR:
2529 case BIT_IOR_EXPR:
2530 case BIT_XOR_EXPR:
2531 case BIT_AND_EXPR:
2532 case TRUTH_ANDIF_EXPR:
2533 case TRUTH_ORIF_EXPR:
2534 case LT_EXPR:
2535 case LE_EXPR:
2536 case GT_EXPR:
2537 case GE_EXPR:
2538 case EQ_EXPR:
2539 case NE_EXPR:
2540 case SPACESHIP_EXPR:
2541 case EXACT_DIV_EXPR:
2542 dump_binary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2543 break;
2544
2545 case CEIL_DIV_EXPR:
2546 case FLOOR_DIV_EXPR:
2547 case ROUND_DIV_EXPR:
2548 case RDIV_EXPR:
2549 dump_binary_op (pp, "/", t, flags);
2550 break;
2551
2552 case CEIL_MOD_EXPR:
2553 case FLOOR_MOD_EXPR:
2554 case ROUND_MOD_EXPR:
2555 dump_binary_op (pp, "%", t, flags);
2556 break;
2557
2558 case COMPONENT_REF:
2559 {
2560 tree ob = TREE_OPERAND (t, 0);
2561 if (INDIRECT_REF_P (ob))
2562 {
2563 ob = TREE_OPERAND (ob, 0);
2564 if (!is_this_parameter (ob)
2565 && !is_dummy_object (ob))
2566 {
2567 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2568 if (TYPE_REF_P (TREE_TYPE (ob)))
2569 pp_cxx_dot (pp);
2570 else
2571 pp_cxx_arrow (pp);
2572 }
2573 }
2574 else
2575 {
2576 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2577 if (TREE_CODE (ob) != ARROW_EXPR)
2578 pp_cxx_dot (pp);
2579 }
2580 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2581 }
2582 break;
2583
2584 case ARRAY_REF:
2585 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2586 pp_cxx_left_bracket (pp);
2587 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2588 pp_cxx_right_bracket (pp);
2589 break;
2590
2591 case OMP_ARRAY_SECTION:
2592 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2593 pp_cxx_left_bracket (pp);
2594 dump_expr (pp, TREE_OPERAND (t, 1), flags);
2595 pp_colon (pp);
2596 dump_expr (pp, TREE_OPERAND (t, 2), flags);
2597 pp_cxx_right_bracket (pp);
2598 break;
2599
2600 case UNARY_PLUS_EXPR:
2601 dump_unary_op (pp, "+", t, flags);
2602 break;
2603
2604 case ADDR_EXPR:
2605 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2606 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2607 /* An ADDR_EXPR can have reference type. In that case, we
2608 shouldn't print the `&' doing so indicates to the user
2609 that the expression has pointer type. */
2610 || (TREE_TYPE (t)
2611 && TYPE_REF_P (TREE_TYPE (t))))
2612 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2613 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2614 dump_unary_op (pp, "&&", t, flags);
2615 else
2616 dump_unary_op (pp, "&", t, flags);
2617 break;
2618
2619 case INDIRECT_REF:
2620 if (TREE_HAS_CONSTRUCTOR (t))
2621 {
2622 t = TREE_OPERAND (t, 0);
2623 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2624 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2625 dump_call_expr_args (pp, t, flags, true);
2626 }
2627 else if (is_stub_object (t))
2628 {
2629 pp_string (pp, "std::declval<");
2630 if (lvalue_p (t)) /* T& */
2631 dump_type (pp, TREE_TYPE (STRIP_REFERENCE_REF (t)), flags);
2632 else /* T */
2633 dump_type (pp, TREE_TYPE (t), flags);
2634 pp_string (pp, ">()");
2635 }
2636 else
2637 {
2638 if (TREE_OPERAND (t,0) != NULL_TREE
2639 && TREE_TYPE (TREE_OPERAND (t, 0))
2640 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2641 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2642 else
2643 dump_unary_op (pp, "*", t, flags);
2644 }
2645 break;
2646
2647 case MEM_REF:
2648 /* Delegate to the base "C" pretty printer. */
2649 pp->c_pretty_printer::unary_expression (t);
2650 break;
2651
2652 case TARGET_MEM_REF:
2653 /* TARGET_MEM_REF can't appear directly from source, but can appear
2654 during late GIMPLE optimizations and through late diagnostic we might
2655 need to support it. Print it as dereferencing of a pointer after
2656 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2657 pointer to single byte types, so
2658 *(type *)((char *) ptr + step * index + index2) if all the operands
2659 are present and the casts are needed. */
2660 pp_cxx_star (pp);
2661 pp_cxx_left_paren (pp);
2662 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (t)))) == NULL_TREE
2663 || !integer_onep (TYPE_SIZE_UNIT
2664 (TREE_TYPE (TREE_TYPE (TMR_BASE (t))))))
2665 {
2666 if (TYPE_SIZE_UNIT (TREE_TYPE (t))
2667 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (t))))
2668 {
2669 pp_cxx_left_paren (pp);
2670 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2671 }
2672 else
2673 {
2674 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2675 pp_cxx_right_paren (pp);
2676 pp_cxx_left_paren (pp);
2677 pp_cxx_left_paren (pp);
2678 dump_type (pp, build_pointer_type (char_type_node), flags);
2679 }
2680 pp_cxx_right_paren (pp);
2681 }
2682 else if (!same_type_p (TREE_TYPE (t),
2683 TREE_TYPE (TREE_TYPE (TMR_BASE (t)))))
2684 {
2685 dump_type (pp, build_pointer_type (TREE_TYPE (t)), flags);
2686 pp_cxx_right_paren (pp);
2687 pp_cxx_left_paren (pp);
2688 }
2689 dump_expr (pp, TMR_BASE (t), flags);
2690 if (TMR_STEP (t) && TMR_INDEX (t))
2691 {
2692 pp_cxx_ws_string (pp, "+");
2693 dump_expr (pp, TMR_INDEX (t), flags);
2694 pp_cxx_ws_string (pp, "*");
2695 dump_expr (pp, TMR_STEP (t), flags);
2696 }
2697 if (TMR_INDEX2 (t))
2698 {
2699 pp_cxx_ws_string (pp, "+");
2700 dump_expr (pp, TMR_INDEX2 (t), flags);
2701 }
2702 if (!integer_zerop (TMR_OFFSET (t)))
2703 {
2704 pp_cxx_ws_string (pp, "+");
2705 dump_expr (pp, fold_convert (ssizetype, TMR_OFFSET (t)), flags);
2706 }
2707 pp_cxx_right_paren (pp);
2708 break;
2709
2710 case NEGATE_EXPR:
2711 case BIT_NOT_EXPR:
2712 case TRUTH_NOT_EXPR:
2713 case PREDECREMENT_EXPR:
2714 case PREINCREMENT_EXPR:
2715 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2716 break;
2717
2718 case POSTDECREMENT_EXPR:
2719 case POSTINCREMENT_EXPR:
2720 pp_cxx_left_paren (pp);
2721 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2722 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2723 pp_cxx_right_paren (pp);
2724 break;
2725
2726 case NON_LVALUE_EXPR:
2727 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2728 should be another level of INDIRECT_REF so that I don't have to do
2729 this. */
2730 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2731 {
2732 tree next = TREE_TYPE (TREE_TYPE (t));
2733
2734 while (TYPE_PTR_P (next))
2735 next = TREE_TYPE (next);
2736
2737 if (TREE_CODE (next) == FUNCTION_TYPE)
2738 {
2739 if (flags & TFF_EXPR_IN_PARENS)
2740 pp_cxx_left_paren (pp);
2741 pp_cxx_star (pp);
2742 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2743 if (flags & TFF_EXPR_IN_PARENS)
2744 pp_cxx_right_paren (pp);
2745 break;
2746 }
2747 /* Else fall through. */
2748 }
2749 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2750 break;
2751
2752 CASE_CONVERT:
2753 case IMPLICIT_CONV_EXPR:
2754 case VIEW_CONVERT_EXPR:
2755 case EXCESS_PRECISION_EXPR:
2756 {
2757 tree op = TREE_OPERAND (t, 0);
2758
2759 if (location_wrapper_p (t))
2760 {
2761 dump_expr (pp, op, flags);
2762 break;
2763 }
2764
2765 tree ttype = TREE_TYPE (t);
2766 tree optype = TREE_TYPE (op);
2767 if (!optype)
2768 optype = unknown_type_node;
2769
2770 if (TREE_CODE (ttype) != TREE_CODE (optype)
2771 && INDIRECT_TYPE_P (ttype)
2772 && INDIRECT_TYPE_P (optype)
2773 && same_type_p (TREE_TYPE (optype),
2774 TREE_TYPE (ttype)))
2775 {
2776 if (TYPE_REF_P (ttype))
2777 {
2778 STRIP_NOPS (op);
2779 if (TREE_CODE (op) == ADDR_EXPR)
2780 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2781 else
2782 dump_unary_op (pp, "*", t, flags);
2783 }
2784 else
2785 dump_unary_op (pp, "&", t, flags);
2786 }
2787 else if (!same_type_p (optype, ttype))
2788 {
2789 /* It is a cast, but we cannot tell whether it is a
2790 reinterpret or static cast. Use the C style notation. */
2791 if (flags & TFF_EXPR_IN_PARENS)
2792 pp_cxx_left_paren (pp);
2793 pp_cxx_left_paren (pp);
2794 dump_type (pp, TREE_TYPE (t), flags);
2795 pp_cxx_right_paren (pp);
2796 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2797 if (flags & TFF_EXPR_IN_PARENS)
2798 pp_cxx_right_paren (pp);
2799 }
2800 else
2801 dump_expr (pp, op, flags);
2802 break;
2803 }
2804
2805 case CONSTRUCTOR:
2806 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2807 {
2808 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2809
2810 if (integer_zerop (idx))
2811 {
2812 /* A NULL pointer-to-member constant. */
2813 pp_cxx_left_paren (pp);
2814 pp_cxx_left_paren (pp);
2815 dump_type (pp, TREE_TYPE (t), flags);
2816 pp_cxx_right_paren (pp);
2817 pp_character (pp, '0');
2818 pp_cxx_right_paren (pp);
2819 break;
2820 }
2821 else if (tree_fits_shwi_p (idx))
2822 {
2823 tree virtuals;
2824 unsigned HOST_WIDE_INT n;
2825
2826 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2827 t = TYPE_METHOD_BASETYPE (t);
2828 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2829
2830 n = tree_to_shwi (idx);
2831
2832 /* Map vtable index back one, to allow for the null pointer to
2833 member. */
2834 --n;
2835
2836 while (n > 0 && virtuals)
2837 {
2838 --n;
2839 virtuals = TREE_CHAIN (virtuals);
2840 }
2841 if (virtuals)
2842 {
2843 dump_expr (pp, BV_FN (virtuals),
2844 flags | TFF_EXPR_IN_PARENS);
2845 break;
2846 }
2847 }
2848 }
2849 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2850 pp_string (pp, "<lambda closure object>");
2851 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2852 {
2853 dump_type (pp, TREE_TYPE (t), 0);
2854 pp_cxx_left_paren (pp);
2855 pp_cxx_right_paren (pp);
2856 }
2857 else
2858 {
2859 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2860 dump_type (pp, TREE_TYPE (t), 0);
2861 pp_cxx_left_brace (pp);
2862 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2863 pp_cxx_right_brace (pp);
2864 }
2865
2866 break;
2867
2868 case OFFSET_REF:
2869 {
2870 tree ob = TREE_OPERAND (t, 0);
2871 if (is_dummy_object (ob))
2872 {
2873 t = TREE_OPERAND (t, 1);
2874 if (TREE_CODE (t) == FUNCTION_DECL)
2875 /* A::f */
2876 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2877 else if (BASELINK_P (t))
2878 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2879 flags | TFF_EXPR_IN_PARENS);
2880 else
2881 dump_decl (pp, t, flags);
2882 }
2883 else
2884 {
2885 if (INDIRECT_REF_P (ob))
2886 {
2887 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2888 pp_cxx_arrow (pp);
2889 pp_cxx_star (pp);
2890 }
2891 else
2892 {
2893 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2894 pp_cxx_dot (pp);
2895 pp_cxx_star (pp);
2896 }
2897 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2898 }
2899 break;
2900 }
2901
2902 case TEMPLATE_PARM_INDEX:
2903 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2904 break;
2905
2906 case CAST_EXPR:
2907 if (TREE_OPERAND (t, 0) == NULL_TREE
2908 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2909 {
2910 dump_type (pp, TREE_TYPE (t), flags);
2911 pp_cxx_left_paren (pp);
2912 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2913 pp_cxx_right_paren (pp);
2914 }
2915 else
2916 {
2917 pp_cxx_left_paren (pp);
2918 dump_type (pp, TREE_TYPE (t), flags);
2919 pp_cxx_right_paren (pp);
2920 pp_cxx_left_paren (pp);
2921 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2922 pp_cxx_right_paren (pp);
2923 }
2924 break;
2925
2926 case STATIC_CAST_EXPR:
2927 pp_cxx_ws_string (pp, "static_cast");
2928 goto cast;
2929 case REINTERPRET_CAST_EXPR:
2930 pp_cxx_ws_string (pp, "reinterpret_cast");
2931 goto cast;
2932 case CONST_CAST_EXPR:
2933 pp_cxx_ws_string (pp, "const_cast");
2934 goto cast;
2935 case DYNAMIC_CAST_EXPR:
2936 pp_cxx_ws_string (pp, "dynamic_cast");
2937 cast:
2938 pp_cxx_begin_template_argument_list (pp);
2939 dump_type (pp, TREE_TYPE (t), flags);
2940 pp_cxx_end_template_argument_list (pp);
2941 pp_cxx_left_paren (pp);
2942 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2943 pp_cxx_right_paren (pp);
2944 break;
2945
2946 case ARROW_EXPR:
2947 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2948 pp_cxx_arrow (pp);
2949 break;
2950
2951 case SIZEOF_EXPR:
2952 case ALIGNOF_EXPR:
2953 if (TREE_CODE (t) == SIZEOF_EXPR)
2954 pp_cxx_ws_string (pp, "sizeof");
2955 else if (ALIGNOF_EXPR_STD_P (t))
2956 pp_cxx_ws_string (pp, "alignof");
2957 else
2958 pp_cxx_ws_string (pp, "__alignof__");
2959 op = TREE_OPERAND (t, 0);
2960 if (PACK_EXPANSION_P (op))
2961 {
2962 pp_string (pp, "...");
2963 op = PACK_EXPANSION_PATTERN (op);
2964 }
2965 pp_cxx_whitespace (pp);
2966 pp_cxx_left_paren (pp);
2967 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2968 dump_type (pp, TREE_TYPE (op), flags);
2969 else if (TYPE_P (TREE_OPERAND (t, 0)))
2970 dump_type (pp, op, flags);
2971 else
2972 dump_expr (pp, op, flags);
2973 pp_cxx_right_paren (pp);
2974 break;
2975
2976 case AT_ENCODE_EXPR:
2977 pp_cxx_ws_string (pp, "@encode");
2978 pp_cxx_whitespace (pp);
2979 pp_cxx_left_paren (pp);
2980 dump_type (pp, TREE_OPERAND (t, 0), flags);
2981 pp_cxx_right_paren (pp);
2982 break;
2983
2984 case NOEXCEPT_EXPR:
2985 pp_cxx_ws_string (pp, "noexcept");
2986 pp_cxx_whitespace (pp);
2987 pp_cxx_left_paren (pp);
2988 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2989 pp_cxx_right_paren (pp);
2990 break;
2991
2992 case REALPART_EXPR:
2993 case IMAGPART_EXPR:
2994 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2995 pp_cxx_whitespace (pp);
2996 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2997 break;
2998
2999 case DEFERRED_PARSE:
3000 pp_string (pp, M_("<unparsed>"));
3001 break;
3002
3003 case TRY_CATCH_EXPR:
3004 case CLEANUP_POINT_EXPR:
3005 dump_expr (pp, TREE_OPERAND (t, 0), flags);
3006 break;
3007
3008 case PSEUDO_DTOR_EXPR:
3009 dump_expr (pp, TREE_OPERAND (t, 0), flags);
3010 pp_cxx_dot (pp);
3011 if (TREE_OPERAND (t, 1))
3012 {
3013 dump_type (pp, TREE_OPERAND (t, 1), flags);
3014 pp_cxx_colon_colon (pp);
3015 }
3016 pp_cxx_complement (pp);
3017 dump_type (pp, TREE_OPERAND (t, 2), flags);
3018 break;
3019
3020 case TEMPLATE_ID_EXPR:
3021 dump_decl (pp, t, flags);
3022 break;
3023
3024 case BIND_EXPR:
3025 case STMT_EXPR:
3026 case EXPR_STMT:
3027 case STATEMENT_LIST:
3028 /* We don't yet have a way of dumping statements in a
3029 human-readable format. */
3030 pp_string (pp, "({...})");
3031 break;
3032
3033 case LOOP_EXPR:
3034 pp_string (pp, "while (1) { ");
3035 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3036 pp_cxx_right_brace (pp);
3037 break;
3038
3039 case EXIT_EXPR:
3040 pp_string (pp, "if (");
3041 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3042 pp_string (pp, ") break; ");
3043 break;
3044
3045 case BASELINK:
3046 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
3047 break;
3048
3049 case EMPTY_CLASS_EXPR:
3050 dump_type (pp, TREE_TYPE (t), flags);
3051 pp_cxx_left_paren (pp);
3052 pp_cxx_right_paren (pp);
3053 break;
3054
3055 case ARGUMENT_PACK_SELECT:
3056 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
3057 break;
3058
3059 case RECORD_TYPE:
3060 case UNION_TYPE:
3061 case ENUMERAL_TYPE:
3062 case REAL_TYPE:
3063 case VOID_TYPE:
3064 case OPAQUE_TYPE:
3065 case BOOLEAN_TYPE:
3066 case INTEGER_TYPE:
3067 case COMPLEX_TYPE:
3068 case VECTOR_TYPE:
3069 case DECLTYPE_TYPE:
3070 pp_type_specifier_seq (pp, t);
3071 break;
3072
3073 case TYPENAME_TYPE:
3074 /* We get here when we want to print a dependent type as an
3075 id-expression, without any disambiguator decoration. */
3076 pp->id_expression (t);
3077 break;
3078
3079 case TEMPLATE_TYPE_PARM:
3080 case TEMPLATE_TEMPLATE_PARM:
3081 case BOUND_TEMPLATE_TEMPLATE_PARM:
3082 dump_type (pp, t, flags);
3083 break;
3084
3085 case TRAIT_EXPR:
3086 pp_cxx_trait (pp, t);
3087 break;
3088
3089 case VA_ARG_EXPR:
3090 pp_cxx_va_arg_expression (pp, t);
3091 break;
3092
3093 case OFFSETOF_EXPR:
3094 pp_cxx_offsetof_expression (pp, t);
3095 break;
3096
3097 case ADDRESSOF_EXPR:
3098 pp_cxx_addressof_expression (pp, t);
3099 break;
3100
3101 case SCOPE_REF:
3102 dump_decl (pp, t, flags);
3103 break;
3104
3105 case EXPR_PACK_EXPANSION:
3106 case UNARY_LEFT_FOLD_EXPR:
3107 case UNARY_RIGHT_FOLD_EXPR:
3108 case BINARY_LEFT_FOLD_EXPR:
3109 case BINARY_RIGHT_FOLD_EXPR:
3110 case TYPEID_EXPR:
3111 case MEMBER_REF:
3112 case DOTSTAR_EXPR:
3113 case NEW_EXPR:
3114 case VEC_NEW_EXPR:
3115 case DELETE_EXPR:
3116 case VEC_DELETE_EXPR:
3117 case MODOP_EXPR:
3118 case ABS_EXPR:
3119 case ABSU_EXPR:
3120 case CONJ_EXPR:
3121 case VECTOR_CST:
3122 case FIXED_CST:
3123 case UNORDERED_EXPR:
3124 case ORDERED_EXPR:
3125 case UNLT_EXPR:
3126 case UNLE_EXPR:
3127 case UNGT_EXPR:
3128 case UNGE_EXPR:
3129 case UNEQ_EXPR:
3130 case LTGT_EXPR:
3131 case COMPLEX_EXPR:
3132 case BIT_FIELD_REF:
3133 case FIX_TRUNC_EXPR:
3134 case FLOAT_EXPR:
3135 pp->expression (t);
3136 break;
3137
3138 case PACK_INDEX_EXPR:
3139 pp->expression (PACK_INDEX_PACK (t));
3140 pp_cxx_left_bracket (pp);
3141 pp->expression (PACK_INDEX_INDEX (t));
3142 pp_cxx_right_bracket (pp);
3143 break;
3144
3145 case TRUTH_AND_EXPR:
3146 case TRUTH_OR_EXPR:
3147 case TRUTH_XOR_EXPR:
3148 if (flags & TFF_EXPR_IN_PARENS)
3149 pp_cxx_left_paren (pp);
3150 pp->expression (t);
3151 if (flags & TFF_EXPR_IN_PARENS)
3152 pp_cxx_right_paren (pp);
3153 break;
3154
3155 case OBJ_TYPE_REF:
3156 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
3157 break;
3158
3159 case LAMBDA_EXPR:
3160 pp_string (pp, M_("<lambda>"));
3161 break;
3162
3163 case PAREN_EXPR:
3164 pp_cxx_left_paren (pp);
3165 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3166 pp_cxx_right_paren (pp);
3167 break;
3168
3169 case REQUIRES_EXPR:
3170 pp_cxx_requires_expr (cxx_pp, t);
3171 break;
3172
3173 case SIMPLE_REQ:
3174 pp_cxx_simple_requirement (cxx_pp, t);
3175 break;
3176
3177 case TYPE_REQ:
3178 pp_cxx_type_requirement (cxx_pp, t);
3179 break;
3180
3181 case COMPOUND_REQ:
3182 pp_cxx_compound_requirement (cxx_pp, t);
3183 break;
3184
3185 case NESTED_REQ:
3186 pp_cxx_nested_requirement (cxx_pp, t);
3187 break;
3188
3189 case ATOMIC_CONSTR:
3190 case CONJ_CONSTR:
3191 case DISJ_CONSTR:
3192 {
3193 pp_cxx_constraint (cxx_pp, t);
3194 break;
3195 }
3196
3197 case PLACEHOLDER_EXPR:
3198 pp_string (pp, M_("*this"));
3199 break;
3200
3201 case TREE_LIST:
3202 dump_expr_list (pp, t, flags);
3203 break;
3204
3205 case NONTYPE_ARGUMENT_PACK:
3206 {
3207 tree args = ARGUMENT_PACK_ARGS (t);
3208 int len = TREE_VEC_LENGTH (args);
3209 pp_cxx_left_brace (pp);
3210 for (int i = 0; i < len; ++i)
3211 {
3212 if (i > 0)
3213 pp_separate_with_comma (pp);
3214 dump_expr (pp, TREE_VEC_ELT (args, i), flags);
3215 }
3216 pp_cxx_right_brace (pp);
3217 break;
3218 }
3219
3220 case CO_AWAIT_EXPR:
3221 pp_cxx_ws_string (pp, "co_await");
3222 pp_cxx_whitespace (pp);
3223 dump_expr (pp, TREE_OPERAND (t, 0), flags);
3224 break;
3225
3226 case CO_YIELD_EXPR:
3227 pp_cxx_ws_string (pp, "co_yield");
3228 pp_cxx_whitespace (pp);
3229 dump_expr (pp, TREE_OPERAND (t, 0), flags);
3230 break;
3231
3232 case CO_RETURN_EXPR:
3233 pp_cxx_ws_string (pp, "co_return");
3234 if (TREE_OPERAND (t, 0))
3235 {
3236 pp_cxx_whitespace (pp);
3237 dump_expr (pp, TREE_OPERAND (t, 0), flags);
3238 }
3239 break;
3240
3241 /* This list is incomplete, but should suffice for now.
3242 It is very important that `sorry' does not call
3243 `report_error_function'. That could cause an infinite loop. */
3244 default:
3245 pp_unsupported_tree (pp, t);
3246 /* Fall through. */
3247 case ERROR_MARK:
3248 pp_string (pp, M_("<expression error>"));
3249 break;
3250 }
3251 }
3252
3253 static void
3254 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
3255 int flags)
3256 {
3257 pp_cxx_left_paren (pp);
3258 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
3259 pp_cxx_whitespace (pp);
3260 if (opstring)
3261 pp_cxx_ws_string (pp, opstring);
3262 else
3263 pp_string (pp, M_("<unknown operator>"));
3264 pp_cxx_whitespace (pp);
3265 tree op1 = TREE_OPERAND (t, 1);
3266 if (TREE_CODE (t) == POINTER_PLUS_EXPR
3267 && TREE_CODE (op1) == INTEGER_CST
3268 && tree_int_cst_sign_bit (op1))
3269 /* A pointer minus an integer is represented internally as plus a very
3270 large number, don't expose that to users. */
3271 op1 = convert (ssizetype, op1);
3272 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
3273 pp_cxx_right_paren (pp);
3274 }
3275
3276 static void
3277 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
3278 {
3279 if (flags & TFF_EXPR_IN_PARENS)
3280 pp_cxx_left_paren (pp);
3281 pp_cxx_ws_string (pp, opstring);
3282 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
3283 if (flags & TFF_EXPR_IN_PARENS)
3284 pp_cxx_right_paren (pp);
3285 }
3286
3287 static void
3288 reinit_cxx_pp (void)
3289 {
3290 pp_clear_output_area (cxx_pp);
3291 cxx_pp->set_padding (pp_none);
3292 pp_indentation (cxx_pp) = 0;
3293 pp_needs_newline (cxx_pp) = false;
3294 pp_show_color (cxx_pp) = false;
3295 cxx_pp->enclosing_scope = current_function_decl;
3296 }
3297
3298 /* Same as pp_formatted_text, except the return string is a separate
3299 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
3300
3301 inline const char *
3302 pp_ggc_formatted_text (pretty_printer *pp)
3303 {
3304 return ggc_strdup (pp_formatted_text (pp));
3305 }
3306
3307 /* Exported interface to stringifying types, exprs and decls under TFF_*
3308 control. */
3309
3310 const char *
3311 type_as_string (tree typ, int flags)
3312 {
3313 reinit_cxx_pp ();
3314 pp_translate_identifiers (cxx_pp) = false;
3315 dump_type (cxx_pp, typ, flags);
3316 return pp_ggc_formatted_text (cxx_pp);
3317 }
3318
3319 const char *
3320 type_as_string_translate (tree typ, int flags)
3321 {
3322 reinit_cxx_pp ();
3323 dump_type (cxx_pp, typ, flags);
3324 return pp_ggc_formatted_text (cxx_pp);
3325 }
3326
3327 const char *
3328 expr_as_string (tree decl, int flags)
3329 {
3330 reinit_cxx_pp ();
3331 pp_translate_identifiers (cxx_pp) = false;
3332 dump_expr (cxx_pp, decl, flags);
3333 return pp_ggc_formatted_text (cxx_pp);
3334 }
3335
3336 /* Wrap decl_as_string with options appropriate for dwarf. */
3337
3338 const char *
3339 decl_as_dwarf_string (tree decl, int flags)
3340 {
3341 const char *name;
3342 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3343 here will be adequate to get the desired behavior. */
3344 cxx_pp->flags |= pp_c_flag_gnu_v3;
3345 name = decl_as_string (decl, flags);
3346 /* Subsequent calls to the pretty printer shouldn't use this style. */
3347 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3348 return name;
3349 }
3350
3351 const char *
3352 decl_as_string (tree decl, int flags)
3353 {
3354 reinit_cxx_pp ();
3355 pp_translate_identifiers (cxx_pp) = false;
3356 dump_decl (cxx_pp, decl, flags);
3357 return pp_ggc_formatted_text (cxx_pp);
3358 }
3359
3360 const char *
3361 decl_as_string_translate (tree decl, int flags)
3362 {
3363 reinit_cxx_pp ();
3364 dump_decl (cxx_pp, decl, flags);
3365 return pp_ggc_formatted_text (cxx_pp);
3366 }
3367
3368 /* Wrap lang_decl_name with options appropriate for dwarf. */
3369
3370 const char *
3371 lang_decl_dwarf_name (tree decl, int v, bool translate)
3372 {
3373 const char *name;
3374 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3375 here will be adequate to get the desired behavior. */
3376 cxx_pp->flags |= pp_c_flag_gnu_v3;
3377 name = lang_decl_name (decl, v, translate);
3378 /* Subsequent calls to the pretty printer shouldn't use this style. */
3379 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3380 return name;
3381 }
3382
3383 /* Generate the three forms of printable names for cxx_printable_name. */
3384
3385 const char *
3386 lang_decl_name (tree decl, int v, bool translate)
3387 {
3388 if (v >= 2)
3389 return (translate
3390 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3391 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3392
3393 reinit_cxx_pp ();
3394 pp_translate_identifiers (cxx_pp) = translate;
3395 if (v == 1
3396 && (DECL_CLASS_SCOPE_P (decl)
3397 || (DECL_NAMESPACE_SCOPE_P (decl)
3398 && CP_DECL_CONTEXT (decl) != global_namespace)))
3399 {
3400 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3401 pp_cxx_colon_colon (cxx_pp);
3402 }
3403
3404 if (TREE_CODE (decl) == FUNCTION_DECL)
3405 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3406 else if ((DECL_NAME (decl) == NULL_TREE)
3407 && TREE_CODE (decl) == NAMESPACE_DECL)
3408 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3409 else
3410 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3411
3412 return pp_ggc_formatted_text (cxx_pp);
3413 }
3414
3415 /* Return the location of a tree passed to %+ formats. */
3416
3417 location_t
3418 location_of (tree t)
3419 {
3420 if (TYPE_P (t))
3421 {
3422 t = TYPE_MAIN_DECL (t);
3423 if (t == NULL_TREE)
3424 return input_location;
3425 }
3426 else if (TREE_CODE (t) == OVERLOAD)
3427 t = (OVL_FIRST (t) != conv_op_marker ? OVL_FIRST (t)
3428 : OVL_FIRST (OVL_CHAIN (t)));
3429
3430 if (DECL_P (t))
3431 return DECL_SOURCE_LOCATION (t);
3432 if (TREE_CODE (t) == DEFERRED_PARSE)
3433 return defparse_location (t);
3434 return cp_expr_loc_or_input_loc (t);
3435 }
3436
3437 /* Now the interfaces from error et al to dump_type et al. Each takes an
3438 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3439 function. */
3440
3441 static const char *
3442 decl_to_string (tree decl, int verbose, bool show_color)
3443 {
3444 int flags = 0;
3445
3446 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3447 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3448 flags = TFF_CLASS_KEY_OR_ENUM;
3449 if (verbose)
3450 flags |= TFF_DECL_SPECIFIERS;
3451 else if (TREE_CODE (decl) == FUNCTION_DECL)
3452 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3453 flags |= TFF_TEMPLATE_HEADER;
3454
3455 reinit_cxx_pp ();
3456 pp_show_color (cxx_pp) = show_color;
3457 dump_decl (cxx_pp, decl, flags);
3458 return pp_ggc_formatted_text (cxx_pp);
3459 }
3460
3461 const char *
3462 expr_to_string (tree decl)
3463 {
3464 reinit_cxx_pp ();
3465 dump_expr (cxx_pp, decl, 0);
3466 return pp_ggc_formatted_text (cxx_pp);
3467 }
3468
3469 static const char *
3470 fndecl_to_string (tree fndecl, int verbose)
3471 {
3472 int flags;
3473
3474 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3475 | TFF_TEMPLATE_HEADER;
3476 if (verbose)
3477 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3478 reinit_cxx_pp ();
3479 dump_decl (cxx_pp, fndecl, flags);
3480 return pp_ggc_formatted_text (cxx_pp);
3481 }
3482
3483
3484 static const char *
3485 code_to_string (enum tree_code c)
3486 {
3487 return get_tree_code_name (c);
3488 }
3489
3490 const char *
3491 language_to_string (enum languages c)
3492 {
3493 switch (c)
3494 {
3495 case lang_c:
3496 return "C";
3497
3498 case lang_cplusplus:
3499 return "C++";
3500
3501 default:
3502 gcc_unreachable ();
3503 }
3504 return NULL;
3505 }
3506
3507 /* Return the proper printed version of a parameter to a C++ function. */
3508
3509 static const char *
3510 parm_to_string (int p)
3511 {
3512 reinit_cxx_pp ();
3513 if (p < 0)
3514 pp_string (cxx_pp, "'this'");
3515 else
3516 pp_decimal_int (cxx_pp, p + 1);
3517 return pp_ggc_formatted_text (cxx_pp);
3518 }
3519
3520 static const char *
3521 op_to_string (bool assop, enum tree_code p)
3522 {
3523 tree id = ovl_op_identifier (assop, p);
3524 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3525 }
3526
3527 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3528
3529 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3530 string in appropriate places, and *QUOTE is written to with false
3531 to suppress pp_format's trailing close quote so that e.g.
3532 foo_typedef {aka underlying_foo} {enum}
3533 can be printed by "%qT" as:
3534 `foo_typedef' {aka `underlying_foo'} {enum}
3535 rather than:
3536 `foo_typedef {aka underlying_foo} {enum}'
3537 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3538 then a leading open quote will be added, whereas if POSTPROCESSED is false
3539 (for handling %T) then any leading quote has already been added by
3540 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3541 within %H and %I).
3542
3543 SHOW_COLOR and HIGHLIGHT_COLOR are used to determine the colorization of
3544 any quotes that are added. */
3545
3546 static const char *
3547 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3548 bool show_color, const char *highlight_color)
3549 {
3550 int flags = 0;
3551 if (verbose)
3552 flags |= TFF_CLASS_KEY_OR_ENUM;
3553 flags |= TFF_TEMPLATE_HEADER;
3554
3555 reinit_cxx_pp ();
3556 pp_show_color (cxx_pp) = show_color;
3557
3558 if (postprocessed && quote && *quote)
3559 {
3560 pp_begin_quote (cxx_pp, show_color);
3561 if (show_color && highlight_color)
3562 pp_string (cxx_pp, colorize_start (show_color, highlight_color));
3563 }
3564
3565 struct obstack *ob = pp_buffer (cxx_pp)->m_obstack;
3566 int type_start, type_len;
3567 type_start = obstack_object_size (ob);
3568
3569 dump_type (cxx_pp, typ, flags);
3570
3571 /* Remember the end of the initial dump. */
3572 type_len = obstack_object_size (ob) - type_start;
3573
3574 /* If we're printing a type that involves typedefs, also print the
3575 stripped version. But sometimes the stripped version looks
3576 exactly the same, so we don't want it after all. To avoid printing
3577 it in that case, we play ugly obstack games. */
3578 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3579 && !uses_template_parms (typ))
3580 {
3581 int aka_start, aka_len; char *p;
3582 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3583 if (quote && *quote)
3584 pp_end_quote (cxx_pp, show_color);
3585 pp_string (cxx_pp, " {aka");
3586 pp_cxx_whitespace (cxx_pp);
3587 if (quote && *quote)
3588 pp_begin_quote (cxx_pp, show_color);
3589 if (highlight_color)
3590 pp_string (cxx_pp, colorize_start (show_color, highlight_color));
3591 /* And remember the start of the aka dump. */
3592 aka_start = obstack_object_size (ob);
3593 dump_type (cxx_pp, aka, flags);
3594 aka_len = obstack_object_size (ob) - aka_start;
3595 if (quote && *quote)
3596 pp_end_quote (cxx_pp, show_color);
3597 pp_right_brace (cxx_pp);
3598 p = (char*)obstack_base (ob);
3599 /* If they are identical, cut off the aka by unwinding the obstack. */
3600 if (type_len == aka_len
3601 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3602 {
3603 /* We can't add a '\0' here, since we may be adding a closing quote
3604 below, and it would be hidden by the '\0'.
3605 Instead, manually unwind the current object within the obstack
3606 so that the insertion point is at the end of the type, before
3607 the "' {aka". */
3608 int delta = type_start + type_len - obstack_object_size (ob);
3609 gcc_assert (delta <= 0);
3610 obstack_blank_fast (ob, delta);
3611 }
3612 else
3613 if (quote)
3614 /* No further closing quotes are needed. */
3615 *quote = false;
3616 }
3617
3618 if (quote && *quote)
3619 {
3620 if (show_color && highlight_color)
3621 pp_string (cxx_pp, colorize_stop (show_color));
3622 pp_end_quote (cxx_pp, show_color);
3623 *quote = false;
3624 }
3625 return pp_ggc_formatted_text (cxx_pp);
3626 }
3627
3628 static const char *
3629 args_to_string (tree p, int verbose)
3630 {
3631 int flags = 0;
3632 if (verbose)
3633 flags |= TFF_CLASS_KEY_OR_ENUM;
3634
3635 if (p == NULL_TREE)
3636 return "";
3637
3638 if (TYPE_P (TREE_VALUE (p)))
3639 return type_as_string_translate (p, flags);
3640
3641 reinit_cxx_pp ();
3642 for (; p; p = TREE_CHAIN (p))
3643 {
3644 if (null_node_p (TREE_VALUE (p)))
3645 pp_cxx_ws_string (cxx_pp, "NULL");
3646 else
3647 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3648 if (TREE_CHAIN (p))
3649 pp_separate_with_comma (cxx_pp);
3650 }
3651 return pp_ggc_formatted_text (cxx_pp);
3652 }
3653
3654 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3655 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3656 arguments. */
3657
3658 static const char *
3659 subst_to_string (tree p, bool show_color)
3660 {
3661 tree decl = TREE_PURPOSE (p);
3662 tree targs = TREE_VALUE (p);
3663 tree tparms = DECL_TEMPLATE_PARMS (decl);
3664 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3665 |TFF_NO_TEMPLATE_BINDINGS);
3666
3667 if (p == NULL_TREE)
3668 return "";
3669
3670 reinit_cxx_pp ();
3671 pp_show_color (cxx_pp) = show_color;
3672 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3673 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3674 return pp_ggc_formatted_text (cxx_pp);
3675 }
3676
3677 static const char *
3678 cv_to_string (tree p, int v)
3679 {
3680 reinit_cxx_pp ();
3681 cxx_pp->set_padding (v ? pp_before : pp_none);
3682 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3683 return pp_ggc_formatted_text (cxx_pp);
3684 }
3685
3686 static const char *
3687 eh_spec_to_string (tree p, int /*v*/)
3688 {
3689 int flags = 0;
3690 reinit_cxx_pp ();
3691 dump_exception_spec (cxx_pp, p, flags);
3692 return pp_ggc_formatted_text (cxx_pp);
3693 }
3694
3695 /* Langhook for print_error_function. */
3696 void
3697 cxx_print_error_function (diagnostic_text_output_format &text_output,
3698 const char *file,
3699 const diagnostic_info *diagnostic)
3700 {
3701 char *prefix;
3702 if (file)
3703 prefix = xstrdup (file);
3704 else
3705 prefix = NULL;
3706 lhd_print_error_function (text_output, file, diagnostic);
3707
3708 pp_set_prefix (text_output.get_printer (), prefix);
3709 maybe_print_instantiation_context (text_output);
3710 }
3711
3712 static void
3713 cp_diagnostic_text_starter (diagnostic_text_output_format &text_output,
3714 const diagnostic_info *diagnostic)
3715 {
3716 pp_set_prefix (text_output.get_printer (),
3717 text_output.build_indent_prefix (true));
3718 text_output.report_current_module (diagnostic_location (diagnostic));
3719 cp_print_error_function (text_output, diagnostic);
3720 maybe_print_instantiation_context (text_output);
3721 maybe_print_constexpr_context (text_output);
3722 maybe_print_constraint_context (text_output);
3723 pp_set_prefix (text_output.get_printer (),
3724 text_output.build_prefix (*diagnostic));
3725 }
3726
3727 /* Print current function onto BUFFER, in the process of reporting
3728 a diagnostic message. Called from cp_diagnostic_starter. */
3729 static void
3730 cp_print_error_function (diagnostic_text_output_format &text_output,
3731 const diagnostic_info *diagnostic)
3732 {
3733 /* If we are in an instantiation context, current_function_decl is likely
3734 to be wrong, so just rely on print_instantiation_full_context. */
3735 if (current_instantiation ())
3736 return;
3737 /* The above is true for constraint satisfaction also. */
3738 if (current_failed_constraint)
3739 return;
3740 diagnostic_context *const context = &text_output.get_context ();
3741 if (diagnostic_last_function_changed (context, diagnostic))
3742 {
3743 pretty_printer *const pp = text_output.get_printer ();
3744 char *old_prefix = pp_take_prefix (pp);
3745 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3746 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3747 char *new_prefix = (file && abstract_origin == NULL)
3748 ? text_output.file_name_as_prefix (file) : NULL;
3749
3750 pp_set_prefix (pp, new_prefix);
3751
3752 if (current_function_decl == NULL)
3753 pp_string (pp, _("At global scope:"));
3754 else
3755 {
3756 tree fndecl, ao;
3757
3758 if (abstract_origin)
3759 {
3760 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3761 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3762 fndecl = ao;
3763 }
3764 else
3765 fndecl = current_function_decl;
3766
3767 pp_printf (pp, function_category (fndecl),
3768 fndecl);
3769
3770 while (abstract_origin)
3771 {
3772 location_t *locus;
3773 tree block = abstract_origin;
3774
3775 locus = &BLOCK_SOURCE_LOCATION (block);
3776 fndecl = NULL;
3777 block = BLOCK_SUPERCONTEXT (block);
3778 while (block && TREE_CODE (block) == BLOCK
3779 && BLOCK_ABSTRACT_ORIGIN (block))
3780 {
3781 ao = BLOCK_ABSTRACT_ORIGIN (block);
3782 if (TREE_CODE (ao) == FUNCTION_DECL)
3783 {
3784 fndecl = ao;
3785 break;
3786 }
3787 else if (TREE_CODE (ao) != BLOCK)
3788 break;
3789
3790 block = BLOCK_SUPERCONTEXT (block);
3791 }
3792 if (fndecl)
3793 abstract_origin = block;
3794 else
3795 {
3796 while (block && TREE_CODE (block) == BLOCK)
3797 block = BLOCK_SUPERCONTEXT (block);
3798
3799 if (block && TREE_CODE (block) == FUNCTION_DECL)
3800 fndecl = block;
3801 abstract_origin = NULL;
3802 }
3803 if (fndecl)
3804 {
3805 expanded_location s = expand_location (*locus);
3806 pp_character (pp, ',');
3807 pp_newline (pp);
3808 if (s.file != NULL)
3809 {
3810 if (text_output.show_column_p () && s.column != 0)
3811 pp_printf (pp,
3812 G_(" inlined from %qD at %r%s:%d:%d%R"),
3813 fndecl,
3814 "locus", s.file, s.line, s.column);
3815 else
3816 pp_printf (pp,
3817 G_(" inlined from %qD at %r%s:%d%R"),
3818 fndecl,
3819 "locus", s.file, s.line);
3820
3821 }
3822 else
3823 pp_printf (pp, G_(" inlined from %qD"),
3824 fndecl);
3825 }
3826 }
3827 pp_character (pp, ':');
3828 }
3829 pp_newline (pp);
3830
3831 diagnostic_set_last_function (context, diagnostic);
3832 pp->set_prefix (old_prefix);
3833 }
3834 }
3835
3836 /* Returns a description of FUNCTION using standard terminology. The
3837 result is a format string of the form "In CATEGORY %qD". */
3838
3839 static const char *
3840 function_category (tree fn)
3841 {
3842 /* We can get called from the middle-end for diagnostics of function
3843 clones. Make sure we have language specific information before
3844 dereferencing it. */
3845 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3846 && DECL_FUNCTION_MEMBER_P (fn))
3847 {
3848 if (DECL_STATIC_FUNCTION_P (fn))
3849 return G_("In static member function %qD");
3850 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3851 return G_("In copy constructor %qD");
3852 else if (DECL_CONSTRUCTOR_P (fn))
3853 return G_("In constructor %qD");
3854 else if (DECL_DESTRUCTOR_P (fn))
3855 return G_("In destructor %qD");
3856 else if (LAMBDA_FUNCTION_P (fn))
3857 return G_("In lambda function");
3858 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3859 return G_("In explicit object member function %qD");
3860 else
3861 return G_("In member function %qD");
3862 }
3863 else
3864 return G_("In function %qD");
3865 }
3866
3867 /* Disable warnings about missing quoting in GCC diagnostics for
3868 the pp_verbatim calls. Their format strings deliberately don't
3869 follow GCC diagnostic conventions. */
3870 #if __GNUC__ >= 10
3871 #pragma GCC diagnostic push
3872 #pragma GCC diagnostic ignored "-Wformat-diag"
3873 #endif
3874
3875 /* Report the full context of a current template instantiation,
3876 onto BUFFER. */
3877 static void
3878 print_instantiation_full_context (diagnostic_text_output_format &text_output)
3879 {
3880 struct tinst_level *p = current_instantiation ();
3881 location_t location = input_location;
3882
3883 if (p)
3884 {
3885 bool show_file
3886 = ((!text_output.show_nesting_p ())
3887 || text_output.show_locations_in_nesting_p ());
3888 char *indent = text_output.build_indent_prefix (true);
3889 pp_verbatim (text_output.get_printer (),
3890 p->list_p ()
3891 ? G_("%s%s%sIn substitution of %qS:\n")
3892 : G_("%s%s%sIn instantiation of %q#D:\n"),
3893 indent,
3894 show_file ? LOCATION_FILE (location) : "",
3895 show_file ? ": " : "",
3896 p->get_node ());
3897 free (indent);
3898 location = p->locus;
3899 p = p->next;
3900 }
3901
3902 print_instantiation_partial_context (text_output, p, location);
3903 }
3904
3905 static void
3906 print_location (diagnostic_text_output_format &text_output,
3907 location_t loc)
3908 {
3909 expanded_location xloc = expand_location (loc);
3910 pretty_printer *const pp = text_output.get_printer ();
3911 if (text_output.show_column_p ())
3912 pp_verbatim (pp, G_("%r%s:%d:%d:%R "),
3913 "locus", xloc.file, xloc.line, xloc.column);
3914 else
3915 pp_verbatim (pp, G_("%r%s:%d:%R "),
3916 "locus", xloc.file, xloc.line);
3917 }
3918
3919 /* A RAII class for use when emitting a line of contextual information
3920 via pp_verbatim to a diagnostic_text_output_format to add before/after
3921 behaviors to the pp_verbatim calls.
3922
3923 If the text output has show_nesting_p (), then the ctor prints
3924 leading indentation and a bullet point, and the dtor prints
3925 the location on a new line, and calls diagnostic_show_locus, both
3926 with indentation (and no bullet point).
3927
3928 Otherwise (when the text output has !show_nesting_p), the ctor prints
3929 the location as leading information on the same line, and the
3930 dtor optionally calls diagnostic_show_locus. */
3931
3932 class auto_context_line
3933 {
3934 public:
3935 auto_context_line (diagnostic_text_output_format &text_output,
3936 location_t loc,
3937 bool show_locus = false)
3938 : m_text_output (text_output),
3939 m_loc (loc),
3940 m_show_locus (show_locus)
3941 {
3942 char *indent = m_text_output.build_indent_prefix (true);
3943 pp_verbatim (m_text_output.get_printer (), indent);
3944 free (indent);
3945 if (!m_text_output.show_nesting_p ())
3946 print_location (m_text_output, m_loc);
3947 }
3948 ~auto_context_line ()
3949 {
3950 pretty_printer *const pp = m_text_output.get_printer ();
3951 if (m_text_output.show_nesting_p ())
3952 {
3953 if (m_text_output.show_locations_in_nesting_p ())
3954 {
3955 char *indent = m_text_output.build_indent_prefix (false);
3956 pp_verbatim (pp, indent);
3957 print_location (m_text_output, m_loc);
3958 pp_newline (pp);
3959
3960 char *saved_prefix = pp_take_prefix (pp);
3961 pp_set_prefix (pp, indent);
3962 gcc_rich_location rich_loc (m_loc);
3963 diagnostic_show_locus (&m_text_output.get_context (),
3964 m_text_output.get_source_printing_options (),
3965 &rich_loc,
3966 DK_NOTE, pp);
3967 pp_set_prefix (pp, saved_prefix);
3968 }
3969 }
3970 else if (m_show_locus)
3971 {
3972 char *saved_prefix = pp_take_prefix (pp);
3973 pp_set_prefix (pp, nullptr);
3974 gcc_rich_location rich_loc (m_loc);
3975 diagnostic_show_locus (&m_text_output.get_context (),
3976 m_text_output.get_source_printing_options (),
3977 &rich_loc,
3978 DK_NOTE, pp);
3979 pp_set_prefix (pp, saved_prefix);
3980 }
3981 }
3982 private:
3983 diagnostic_text_output_format &m_text_output;
3984 location_t m_loc;
3985 bool m_show_locus;
3986 };
3987
3988 /* Helper function of print_instantiation_partial_context() that
3989 prints a single line of instantiation context. */
3990
3991 static void
3992 print_instantiation_partial_context_line (diagnostic_text_output_format &text_output,
3993 struct tinst_level *t,
3994 location_t loc, bool recursive_p)
3995 {
3996 if (loc == UNKNOWN_LOCATION)
3997 return;
3998
3999 auto_context_line sentinel (text_output, loc, true);
4000
4001 pretty_printer *const pp = text_output.get_printer ();
4002
4003 if (t != NULL)
4004 {
4005 if (t->list_p ())
4006 pp_verbatim (pp,
4007 recursive_p
4008 ? G_("recursively required by substitution of %qS\n")
4009 : G_("required by substitution of %qS\n"),
4010 t->get_node ());
4011 else
4012 pp_verbatim (pp,
4013 recursive_p
4014 ? G_("recursively required from %q#D\n")
4015 : G_("required from %q#D\n"),
4016 t->get_node ());
4017 }
4018 else
4019 {
4020 pp_verbatim (pp,
4021 recursive_p
4022 ? G_("recursively required from here\n")
4023 : G_("required from here\n"));
4024 }
4025 }
4026
4027 /* Same as print_instantiation_full_context but less verbose. */
4028
4029 static void
4030 print_instantiation_partial_context (diagnostic_text_output_format &text_output,
4031 struct tinst_level *t0, location_t loc)
4032 {
4033 struct tinst_level *t;
4034 int n_total = 0;
4035 int n;
4036 location_t prev_loc = loc;
4037
4038 for (t = t0; t != NULL; t = t->next)
4039 if (prev_loc != t->locus)
4040 {
4041 prev_loc = t->locus;
4042 n_total++;
4043 }
4044
4045 t = t0;
4046
4047 if (template_backtrace_limit
4048 && n_total > template_backtrace_limit)
4049 {
4050 int skip = n_total - template_backtrace_limit;
4051 int head = template_backtrace_limit / 2;
4052
4053 /* Avoid skipping just 1. If so, skip 2. */
4054 if (skip == 1)
4055 {
4056 skip = 2;
4057 head = (template_backtrace_limit - 1) / 2;
4058 }
4059
4060 for (n = 0; n < head; n++)
4061 {
4062 gcc_assert (t != NULL);
4063 if (loc != t->locus)
4064 print_instantiation_partial_context_line (text_output, t, loc,
4065 /*recursive_p=*/false);
4066 loc = t->locus;
4067 t = t->next;
4068 }
4069 if (t != NULL && skip > 0)
4070 {
4071 auto_context_line sentinel (text_output, loc);
4072 pp_verbatim (text_output.get_printer (),
4073 G_("[ skipping %d instantiation contexts,"
4074 " use -ftemplate-backtrace-limit=0 to disable ]\n"),
4075 skip);
4076 do {
4077 loc = t->locus;
4078 t = t->next;
4079 } while (t != NULL && --skip > 0);
4080 }
4081 }
4082
4083 while (t != NULL)
4084 {
4085 while (t->next != NULL && t->locus == t->next->locus)
4086 {
4087 loc = t->locus;
4088 t = t->next;
4089 }
4090 print_instantiation_partial_context_line (text_output, t, loc,
4091 t->locus == loc);
4092 loc = t->locus;
4093 t = t->next;
4094 }
4095 print_instantiation_partial_context_line (text_output, NULL, loc,
4096 /*recursive_p=*/false);
4097 }
4098
4099 /* Called from cp_thing to print the template context for an error. */
4100 static void
4101 maybe_print_instantiation_context (diagnostic_text_output_format &text_output)
4102 {
4103 if (!problematic_instantiation_changed () || current_instantiation () == 0)
4104 return;
4105
4106 record_last_problematic_instantiation ();
4107 print_instantiation_full_context (text_output);
4108 }
4109
4110 /* Report what constexpr call(s) we're trying to expand, if any. */
4111
4112 void
4113 maybe_print_constexpr_context (diagnostic_text_output_format &text_output)
4114 {
4115 vec<tree> call_stack = cx_error_context ();
4116 unsigned ix;
4117 tree t;
4118
4119 FOR_EACH_VEC_ELT (call_stack, ix, t)
4120 {
4121 const char *s = expr_as_string (t, 0);
4122 pretty_printer *const pp = text_output.get_printer ();
4123 auto_context_line sentinel (text_output, EXPR_LOCATION (t));
4124 pp_verbatim (pp,
4125 G_("in %<constexpr%> expansion of %qs"),
4126 s);
4127 pp_newline (pp);
4128 }
4129 }
4130
4131
4132 static void
4133 print_constrained_decl_info (diagnostic_text_output_format &text_output,
4134 tree decl)
4135 {
4136 auto_context_line sentinel (text_output, DECL_SOURCE_LOCATION (decl));
4137 pretty_printer *const pp = text_output.get_printer ();
4138 pp_verbatim (pp, G_("required by the constraints of %q#D\n"), decl);
4139 }
4140
4141 static void
4142 print_concept_check_info (diagnostic_text_output_format &text_output,
4143 tree expr, tree map, tree args)
4144 {
4145 gcc_assert (concept_check_p (expr));
4146
4147 tree tmpl = TREE_OPERAND (expr, 0);
4148
4149 auto_context_line sentinel (text_output, DECL_SOURCE_LOCATION (tmpl));
4150
4151 cxx_pretty_printer *const pp
4152 = (cxx_pretty_printer *)text_output.get_printer ();
4153 pp_verbatim (pp, G_("required for the satisfaction of %qE"), expr);
4154 if (map && map != error_mark_node)
4155 {
4156 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
4157 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
4158 ? subst_map : map));
4159 }
4160 pp_newline (pp);
4161 }
4162
4163 /* Diagnose the entry point into the satisfaction error. Returns the next
4164 context, if any. */
4165
4166 static tree
4167 print_constraint_context_head (diagnostic_text_output_format &text_output,
4168 tree cxt, tree args)
4169 {
4170 tree src = TREE_VALUE (cxt);
4171 if (!src)
4172 {
4173 auto_context_line sentinel (text_output, input_location);
4174 pretty_printer *const pp = text_output.get_printer ();
4175 pp_verbatim (pp, G_("required for constraint satisfaction\n"));
4176 return NULL_TREE;
4177 }
4178 if (DECL_P (src))
4179 {
4180 print_constrained_decl_info (text_output, src);
4181 return NULL_TREE;
4182 }
4183 else
4184 {
4185 print_concept_check_info (text_output, src, TREE_PURPOSE (cxt), args);
4186 return TREE_CHAIN (cxt);
4187 }
4188 }
4189
4190 static void
4191 print_requires_expression_info (diagnostic_text_output_format &text_output,
4192 tree constr, tree args)
4193 {
4194
4195 tree expr = ATOMIC_CONSTR_EXPR (constr);
4196 tree map = ATOMIC_CONSTR_MAP (constr);
4197 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
4198 if (map == error_mark_node)
4199 return;
4200
4201 auto_context_line sentinel (text_output, cp_expr_loc_or_input_loc (expr));
4202 cxx_pretty_printer *const pp
4203 = static_cast <cxx_pretty_printer *> (text_output.get_printer ());
4204
4205 tree parms = TREE_OPERAND (expr, 0);
4206 pp_verbatim (pp, parms ? G_("in requirements with ")
4207 : G_("in requirements "));
4208 while (parms)
4209 {
4210 pp_verbatim (pp, "%q#D", parms);
4211 if (TREE_CHAIN (parms))
4212 pp_separate_with_comma (pp);
4213 parms = TREE_CHAIN (parms);
4214 }
4215 pp_cxx_parameter_mapping (pp, map);
4216
4217 pp_verbatim (pp, "\n");
4218 }
4219
4220 void
4221 maybe_print_single_constraint_context (diagnostic_text_output_format &text_output,
4222 tree failed)
4223 {
4224 if (!failed)
4225 return;
4226
4227 tree constr = TREE_VALUE (failed);
4228 if (!constr || constr == error_mark_node)
4229 return;
4230 tree cxt = CONSTR_CONTEXT (constr);
4231 if (!cxt)
4232 return;
4233 tree args = TREE_PURPOSE (failed);
4234
4235 /* Print the stack of requirements. */
4236 cxt = print_constraint_context_head (text_output, cxt, args);
4237 while (cxt && !DECL_P (TREE_VALUE (cxt)))
4238 {
4239 tree expr = TREE_VALUE (cxt);
4240 tree map = TREE_PURPOSE (cxt);
4241 print_concept_check_info (text_output, expr, map, args);
4242 cxt = TREE_CHAIN (cxt);
4243 }
4244
4245 /* For certain constraints, we can provide additional context. */
4246 if (TREE_CODE (constr) == ATOMIC_CONSTR
4247 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
4248 print_requires_expression_info (text_output, constr, args);
4249 }
4250
4251 void
4252 maybe_print_constraint_context (diagnostic_text_output_format &text_output)
4253 {
4254 if (!current_failed_constraint)
4255 return;
4256
4257 tree cur = current_failed_constraint;
4258
4259 /* Recursively print nested contexts. */
4260 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
4261 if (current_failed_constraint)
4262 maybe_print_constraint_context (text_output);
4263
4264 /* Print this context. */
4265 maybe_print_single_constraint_context (text_output, cur);
4266 }
4267
4268 /* Return true iff TYPE_A and TYPE_B are template types that are
4269 meaningful to compare. */
4270
4271 static bool
4272 comparable_template_types_p (tree type_a, tree type_b)
4273 {
4274 if (!CLASS_TYPE_P (type_a))
4275 return false;
4276 if (!CLASS_TYPE_P (type_b))
4277 return false;
4278
4279 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4280 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4281 if (!tinfo_a || !tinfo_b)
4282 return false;
4283
4284 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
4285 }
4286
4287 /* Start a new line indented by SPC spaces on PP. */
4288
4289 static void
4290 newline_and_indent (pretty_printer *pp, int spc)
4291 {
4292 pp_newline (pp);
4293 for (int i = 0; i < spc; i++)
4294 pp_space (pp);
4295 }
4296
4297 /* Generate a GC-allocated string for ARG, an expression or type. */
4298
4299 static const char *
4300 arg_to_string (tree arg, bool verbose)
4301 {
4302 if (TYPE_P (arg))
4303 return type_to_string (arg, verbose, true, NULL, false);
4304 else
4305 return expr_to_string (arg);
4306 }
4307
4308 /* Subroutine to type_to_string_with_compare and
4309 print_template_tree_comparison.
4310
4311 Print a representation of ARG (an expression or type) to PP,
4312 colorizing it if PP->show_color, using HIGHLIGHT_COLOR,
4313 or "type-diff" if the latter is NULL. */
4314
4315 static void
4316 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose,
4317 const char *highlight_color)
4318 {
4319 if (!highlight_color)
4320 highlight_color = "type-diff";
4321 pp_printf (pp, "%r%s%R",
4322 highlight_color,
4323 (arg
4324 ? arg_to_string (arg, verbose)
4325 : G_("(no argument)")));
4326 }
4327
4328 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
4329
4330 The types must satisfy comparable_template_types_p.
4331
4332 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
4333 potentially colorizing/eliding in comparison with TYPE_B.
4334
4335 For example given types:
4336 vector<map<int,double>>
4337 and
4338 vector<map<int,float>>
4339 then the result on PP would be:
4340 vector<map<[...],double>>
4341 with type elision, and:
4342 vector<map<int,double>>
4343 without type elision.
4344
4345 In both cases the parts of TYPE that differ from PEER will be colorized
4346 if pp_show_color (pp) is true. In the above example, this would be
4347 "double".
4348
4349 If INDENT is non-zero, then the types are printed in a tree-like form
4350 which shows both types. In the above example, the result on PP would be:
4351
4352 vector<
4353 map<
4354 [...],
4355 [double != float]>>
4356
4357 and without type-elision would be:
4358
4359 vector<
4360 map<
4361 int,
4362 [double != float]>>
4363
4364 As before, the differing parts of the types are colorized if
4365 pp_show_color (pp) is true ("double" and "float" in this example).
4366
4367 Template arguments in which both types are using the default arguments
4368 are not printed; if at least one of the two types is using a non-default
4369 argument, then that argument is printed (or both arguments for the
4370 tree-like print format). */
4371
4372 static void
4373 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
4374 bool verbose, int indent,
4375 const char *highlight_color_a,
4376 const char *highlight_color_b)
4377 {
4378 if (indent)
4379 newline_and_indent (pp, indent);
4380
4381 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
4382 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
4383
4384 pp_printf (pp, "%s<",
4385 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
4386
4387 tree args_a = TI_ARGS (tinfo_a);
4388 tree args_b = TI_ARGS (tinfo_b);
4389 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4390 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4391 int flags = 0;
4392 int len_a = get_non_default_template_args_count (args_a, flags);
4393 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
4394 int len_b = get_non_default_template_args_count (args_b, flags);
4395 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
4396 /* Determine the maximum range of args for which non-default template args
4397 were used; beyond this, only default args (if any) were used, and so
4398 they will be equal from this point onwards.
4399 One of the two peers might have used default arguments within this
4400 range, but the other will be using non-default arguments, and so
4401 it's more readable to print both within this range, to highlight
4402 the differences. */
4403 int len_max = MAX (len_a, len_b);
4404 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
4405 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
4406 for (int idx = 0; idx < len_max; idx++)
4407 {
4408 if (idx)
4409 pp_character (pp, ',');
4410
4411 tree arg_a = TREE_VEC_ELT (args_a, idx);
4412 tree arg_b = TREE_VEC_ELT (args_b, idx);
4413 if (arg_a == arg_b)
4414 {
4415 if (indent)
4416 newline_and_indent (pp, indent + 2);
4417 /* Can do elision here, printing "[...]". */
4418 if (flag_elide_type)
4419 pp_string (pp, G_("[...]"));
4420 else
4421 pp_string (pp, arg_to_string (arg_a, verbose));
4422 }
4423 else
4424 {
4425 int new_indent = indent ? indent + 2 : 0;
4426 if (comparable_template_types_p (arg_a, arg_b))
4427 print_template_differences (pp, arg_a, arg_b, verbose, new_indent,
4428 highlight_color_a, highlight_color_b);
4429 else
4430 if (indent)
4431 {
4432 newline_and_indent (pp, indent + 2);
4433 pp_character (pp, '[');
4434 print_nonequal_arg (pp, arg_a, verbose, highlight_color_a);
4435 pp_string (pp, " != ");
4436 print_nonequal_arg (pp, arg_b, verbose, highlight_color_b);
4437 pp_character (pp, ']');
4438 }
4439 else
4440 print_nonequal_arg (pp, arg_a, verbose, highlight_color_a);
4441 }
4442 }
4443 pp_printf (pp, ">");
4444 }
4445
4446 /* As type_to_string, but for a template, potentially colorizing/eliding
4447 in comparison with PEER.
4448 For example, if TYPE is map<int,double> and PEER is map<int,int>,
4449 then the resulting string would be:
4450 map<[...],double>
4451 with type elision, and:
4452 map<int,double>
4453 without type elision.
4454
4455 In both cases the parts of TYPE that differ from PEER will be colorized
4456 if SHOW_COLOR is true. In the above example, this would be "double".
4457
4458 Template arguments in which both types are using the default arguments
4459 are not printed; if at least one of the two types is using a non-default
4460 argument, then both arguments are printed.
4461
4462 The resulting string is in a GC-allocated buffer. */
4463
4464 static const char *
4465 type_to_string_with_compare (tree type, tree peer, bool verbose,
4466 bool show_color,
4467 const char *this_highlight_color,
4468 const char *peer_highlight_color)
4469 {
4470 pretty_printer inner_pp;
4471 pretty_printer *pp = &inner_pp;
4472 pp_show_color (pp) = show_color;
4473
4474 print_template_differences (pp, type, peer, verbose, 0,
4475 this_highlight_color, peer_highlight_color);
4476 return pp_ggc_formatted_text (pp);
4477 }
4478
4479 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4480 indented by INDENT spaces.
4481
4482 For example given types:
4483
4484 vector<map<int,double>>
4485
4486 and
4487
4488 vector<map<double,float>>
4489
4490 the output with type elision would be:
4491
4492 vector<
4493 map<
4494 [...],
4495 [double != float]>>
4496
4497 and without type-elision would be:
4498
4499 vector<
4500 map<
4501 int,
4502 [double != float]>>
4503
4504 TYPE_A and TYPE_B must both be comparable template types
4505 (as per comparable_template_types_p).
4506
4507 Template arguments in which both types are using the default arguments
4508 are not printed; if at least one of the two types is using a non-default
4509 argument, then both arguments are printed. */
4510
4511 static void
4512 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4513 bool verbose, int indent,
4514 const char *highlight_color_a,
4515 const char *highlight_color_b)
4516 {
4517 print_template_differences (pp, type_a, type_b, verbose, indent,
4518 highlight_color_a,
4519 highlight_color_b);
4520 }
4521
4522 /* Subroutine for use in a format_postprocessor::handle
4523 implementation. Adds a chunk to the end of
4524 formatted output, so that it will be printed
4525 by pp_output_formatted_text. */
4526
4527 static void
4528 append_formatted_chunk (pretty_printer *pp, const char *content)
4529 {
4530 output_buffer *buffer = pp_buffer (pp);
4531 pp_formatted_chunks *chunk_array = buffer->m_cur_formatted_chunks;
4532 chunk_array->append_formatted_chunk (buffer->m_chunk_obstack, content);
4533 }
4534
4535 #if __GNUC__ >= 10
4536 #pragma GCC diagnostic pop
4537 #endif
4538
4539 /* If we had %H and %I, and hence deferred printing them,
4540 print them now, storing the result into custom_token_value
4541 for the custom pp_token. Quote them if 'q' was provided.
4542 Also print the difference in tree form, adding it as
4543 an additional chunk. */
4544
4545 void
4546 cxx_format_postprocessor::handle (pretty_printer *pp)
4547 {
4548 /* If we have one of %H and %I, the other should have
4549 been present. */
4550 if (m_type_a.m_tree || m_type_b.m_tree)
4551 {
4552 const bool show_highlight_colors = pp_show_highlight_colors (pp);
4553 const char *percent_h
4554 = show_highlight_colors ? highlight_colors::percent_h : nullptr;
4555 const char *percent_i
4556 = show_highlight_colors ? highlight_colors::percent_i : nullptr;
4557 /* Avoid reentrancy issues by working with a copy of
4558 m_type_a and m_type_b, resetting them now. */
4559 deferred_printed_type type_a = std::move (m_type_a);
4560 deferred_printed_type type_b = std::move (m_type_b);
4561 m_type_a = deferred_printed_type ();
4562 m_type_b = deferred_printed_type ();
4563
4564 gcc_assert (type_a.m_token_list);
4565 gcc_assert (type_b.m_token_list);
4566
4567 bool show_color = pp_show_color (pp);
4568
4569 const char *type_a_text;
4570 const char *type_b_text;
4571
4572 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4573 {
4574 type_a_text = type_to_string_with_compare
4575 (type_a.m_tree, type_b.m_tree,
4576 type_a.m_verbose, show_color,
4577 percent_h, percent_i);
4578 type_b_text = type_to_string_with_compare
4579 (type_b.m_tree, type_a.m_tree,
4580 type_b.m_verbose, show_color,
4581 percent_i, percent_h);
4582
4583 if (flag_diagnostics_show_template_tree)
4584 {
4585 pretty_printer inner_pp;
4586 pp_show_color (&inner_pp) = pp_show_color (pp);
4587 print_template_tree_comparison
4588 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2,
4589 percent_h, percent_i);
4590 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4591 }
4592 }
4593 else
4594 {
4595 /* If the types were not comparable (or if only one of %H/%I was
4596 provided), they are printed normally, and no difference tree
4597 is printed. */
4598 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4599 true, &type_a.m_quote, show_color,
4600 percent_h);
4601 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4602 true, &type_b.m_quote, show_color,
4603 percent_i);
4604 }
4605
4606 type_a.set_text_for_token_list (type_a_text, type_a.m_quote);
4607 type_b.set_text_for_token_list (type_b_text, type_b.m_quote);
4608 }
4609 }
4610
4611 /* Subroutine for handling %H and %I, to support i18n of messages like:
4612
4613 error_at (loc, "could not convert %qE from %qH to %qI",
4614 expr, type_a, type_b);
4615
4616 so that we can print things like:
4617
4618 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4619
4620 and, with type-elision:
4621
4622 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4623
4624 (with color-coding of the differences between the types).
4625
4626 The %H and %I format codes are peers: both must be present,
4627 and they affect each other. Hence to handle them, we must
4628 delay printing until we have both, deferring the printing to
4629 pretty_printer's m_format_postprocessor hook.
4630
4631 This is called in phase 2 of pp_format, when it is accumulating
4632 a series of pp_token lists. Since we have to interact with the
4633 fiddly quoting logic for "aka", we store the pp_token_list *
4634 and in the m_format_postprocessor hook we generate text for the type
4635 (possibly with quotes and colors), then replace all tokens in that token list
4636 (such as [BEGIN_QUOTE, END_QUOTE]) with a text token containing the
4637 freshly generated text.
4638
4639 We also need to stash whether a 'q' prefix was provided (the QUOTE
4640 param) so that we can add the quotes when writing out the delayed
4641 chunk. */
4642
4643 static void
4644 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4645 tree type,
4646 pp_token_list &formatted_token_list,
4647 bool verbose, bool quote)
4648 {
4649 gcc_assert (deferred->m_tree == NULL_TREE);
4650 *deferred = deferred_printed_type (type, formatted_token_list,
4651 verbose, quote);
4652 }
4653
4654 /* Implementation of pp_markup::element_quoted_type::print_type
4655 for C++/ObjC++. */
4656
4657 void
4658 pp_markup::element_quoted_type::print_type (pp_markup::context &ctxt)
4659 {
4660 const char *highlight_color
4661 = pp_show_highlight_colors (&ctxt.m_pp) ? m_highlight_color : nullptr;
4662 const char *result
4663 = type_to_string (m_type, false, false, &ctxt.m_quoted,
4664 pp_show_color (&ctxt.m_pp), highlight_color);
4665 pp_string (&ctxt.m_pp, result);
4666 }
4667
4668 /* Called from output_format -- during diagnostic message processing --
4669 to handle C++ specific format specifier with the following meanings:
4670 %A function argument-list.
4671 %C tree code.
4672 %D declaration.
4673 %E expression.
4674 %F function declaration.
4675 %H type difference (from).
4676 %I type difference (to).
4677 %L language as used in extern "lang".
4678 %O binary operator.
4679 %P function parameter whose position is indicated by an integer.
4680 %Q assignment operator.
4681 %S substitution (template + args)
4682 %T type.
4683 %V cv-qualifier.
4684 %X exception-specification. */
4685 static bool
4686 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4687 int precision, bool wide, bool set_locus, bool verbose,
4688 bool *quoted, pp_token_list &formatted_token_list)
4689 {
4690 gcc_assert (pp_format_postprocessor (pp));
4691 cxx_format_postprocessor *postprocessor
4692 = static_cast <cxx_format_postprocessor *> (pp_format_postprocessor (pp));
4693
4694 const char *result;
4695 tree t = NULL;
4696 #define next_tree (t = va_arg (*text->m_args_ptr, tree))
4697 #define next_tcode ((enum tree_code) va_arg (*text->m_args_ptr, int))
4698 #define next_lang ((enum languages) va_arg (*text->m_args_ptr, int))
4699 #define next_int va_arg (*text->m_args_ptr, int)
4700
4701 if (precision != 0 || wide)
4702 return false;
4703
4704 switch (*spec)
4705 {
4706 case 'A': result = args_to_string (next_tree, verbose); break;
4707 case 'C': result = code_to_string (next_tcode); break;
4708 case 'D':
4709 {
4710 tree temp = next_tree;
4711 if (VAR_P (temp)
4712 && DECL_HAS_DEBUG_EXPR_P (temp))
4713 {
4714 temp = DECL_DEBUG_EXPR (temp);
4715 if (!DECL_P (temp))
4716 {
4717 result = expr_to_string (temp);
4718 break;
4719 }
4720 }
4721 result = decl_to_string (temp, verbose, pp_show_color (pp));
4722 }
4723 break;
4724 case 'E': result = expr_to_string (next_tree); break;
4725 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4726 case 'H':
4727 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4728 formatted_token_list, verbose, *quoted);
4729 return true;
4730 case 'I':
4731 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4732 formatted_token_list, verbose, *quoted);
4733 return true;
4734 case 'L': result = language_to_string (next_lang); break;
4735 case 'O': result = op_to_string (false, next_tcode); break;
4736 case 'P': result = parm_to_string (next_int); break;
4737 case 'Q': result = op_to_string (true, next_tcode); break;
4738 case 'S': result = subst_to_string (next_tree, pp_show_color (pp)); break;
4739 case 'T':
4740 {
4741 result = type_to_string (next_tree, verbose, false, quoted,
4742 pp_show_color (pp));
4743 }
4744 break;
4745 case 'V': result = cv_to_string (next_tree, verbose); break;
4746 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4747
4748 default:
4749 return false;
4750 }
4751
4752 pp_string (pp, result);
4753 if (set_locus && t != NULL)
4754 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4755 return true;
4756 #undef next_tree
4757 #undef next_tcode
4758 #undef next_lang
4759 #undef next_int
4760 }
4761
4762 /* Warn about the use of C++0x features when appropriate. */
4763 void
4764 maybe_warn_cpp0x (cpp0x_warn_str str, location_t loc/*=input_location*/)
4765 {
4766 if (cxx_dialect == cxx98)
4767 switch (str)
4768 {
4769 case CPP0X_INITIALIZER_LISTS:
4770 pedwarn (loc, OPT_Wc__11_extensions,
4771 "extended initializer lists "
4772 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4773 break;
4774 case CPP0X_EXPLICIT_CONVERSION:
4775 pedwarn (loc, OPT_Wc__11_extensions,
4776 "explicit conversion operators "
4777 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4778 break;
4779 case CPP0X_VARIADIC_TEMPLATES:
4780 pedwarn (loc, OPT_Wc__11_extensions,
4781 "variadic templates "
4782 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4783 break;
4784 case CPP0X_LAMBDA_EXPR:
4785 pedwarn (loc, OPT_Wc__11_extensions,
4786 "lambda expressions "
4787 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4788 break;
4789 case CPP0X_AUTO:
4790 pedwarn (loc, OPT_Wc__11_extensions,
4791 "C++11 auto only available with %<-std=c++11%> or "
4792 "%<-std=gnu++11%>");
4793 break;
4794 case CPP0X_SCOPED_ENUMS:
4795 pedwarn (loc, OPT_Wc__11_extensions,
4796 "scoped enums only available with %<-std=c++11%> or "
4797 "%<-std=gnu++11%>");
4798 break;
4799 case CPP0X_DEFAULTED_DELETED:
4800 pedwarn (loc, OPT_Wc__11_extensions,
4801 "defaulted and deleted functions "
4802 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4803 break;
4804 case CPP0X_INLINE_NAMESPACES:
4805 if (pedantic)
4806 pedwarn (loc, OPT_Wc__11_extensions,
4807 "inline namespaces "
4808 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4809 break;
4810 case CPP0X_OVERRIDE_CONTROLS:
4811 pedwarn (loc, OPT_Wc__11_extensions,
4812 "override controls (override/final) "
4813 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4814 break;
4815 case CPP0X_NSDMI:
4816 pedwarn (loc, OPT_Wc__11_extensions,
4817 "non-static data member initializers "
4818 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4819 break;
4820 case CPP0X_USER_DEFINED_LITERALS:
4821 pedwarn (loc, OPT_Wc__11_extensions,
4822 "user-defined literals "
4823 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4824 break;
4825 case CPP0X_DELEGATING_CTORS:
4826 pedwarn (loc, OPT_Wc__11_extensions,
4827 "delegating constructors "
4828 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4829 break;
4830 case CPP0X_INHERITING_CTORS:
4831 pedwarn (loc, OPT_Wc__11_extensions,
4832 "inheriting constructors "
4833 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4834 break;
4835 case CPP0X_ATTRIBUTES:
4836 if (pedantic)
4837 pedwarn (loc, OPT_Wc__11_extensions,
4838 "C++11 attributes "
4839 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4840 break;
4841 case CPP0X_REF_QUALIFIER:
4842 pedwarn (loc, OPT_Wc__11_extensions,
4843 "ref-qualifiers "
4844 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4845 break;
4846 default:
4847 gcc_unreachable ();
4848 }
4849 }
4850
4851 /* Warn about the use of variadic templates when appropriate. */
4852 void
4853 maybe_warn_variadic_templates (void)
4854 {
4855 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4856 }
4857
4858
4859 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4860 option OPTION_ID with text GMSGID. Use this function to report
4861 diagnostics for constructs that are invalid C++98, but valid
4862 C++0x. */
4863 bool
4864 pedwarn_cxx98 (location_t location,
4865 diagnostic_option_id option_id,
4866 const char *gmsgid, ...)
4867 {
4868 diagnostic_info diagnostic;
4869 va_list ap;
4870 bool ret;
4871 rich_location richloc (line_table, location);
4872
4873 va_start (ap, gmsgid);
4874 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4875 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4876 diagnostic.option_id = option_id;
4877 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4878 va_end (ap);
4879 return ret;
4880 }
4881
4882 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4883 we found when we tried to do the lookup. LOCATION is the location of
4884 the NAME identifier. */
4885
4886 void
4887 qualified_name_lookup_error (tree scope, tree name,
4888 tree decl, location_t location)
4889 {
4890 if (scope == error_mark_node)
4891 ; /* We already complained. */
4892 else if (TYPE_P (scope))
4893 {
4894 if (!COMPLETE_TYPE_P (scope)
4895 && !currently_open_class (scope))
4896 error_at (location, "incomplete type %qT used in nested name specifier",
4897 scope);
4898 else if (TREE_CODE (decl) == TREE_LIST)
4899 {
4900 auto_diagnostic_group d;
4901 error_at (location, "reference to %<%T::%D%> is ambiguous",
4902 scope, name);
4903 print_candidates (decl);
4904 }
4905 else
4906 {
4907 auto_diagnostic_group d;
4908 name_hint hint;
4909 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4910 hint = suggest_alternative_in_scoped_enum (name, scope);
4911 if (const char *suggestion = hint.suggestion ())
4912 {
4913 gcc_rich_location richloc (location);
4914 richloc.add_fixit_replace (suggestion);
4915 error_at (&richloc,
4916 "%qD is not a member of %qT; did you mean %qs?",
4917 name, scope, suggestion);
4918 }
4919 else
4920 error_at (location, "%qD is not a member of %qT", name, scope);
4921 }
4922 }
4923 else if (scope != global_namespace)
4924 {
4925 auto_diagnostic_group d;
4926 bool emit_fixit = true;
4927 name_hint hint
4928 = suggest_alternative_in_explicit_scope (location, name, scope);
4929 if (!hint)
4930 {
4931 hint = suggest_alternatives_in_other_namespaces (location, name);
4932 /* "location" is just the location of the name, not of the explicit
4933 scope, and it's not easy to get at the latter, so we can't issue
4934 fix-it hints for the suggestion. */
4935 emit_fixit = false;
4936 }
4937 if (const char *suggestion = hint.suggestion ())
4938 {
4939 gcc_rich_location richloc (location);
4940 if (emit_fixit)
4941 richloc.add_fixit_replace (suggestion);
4942 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4943 name, scope, suggestion);
4944 }
4945 else
4946 error_at (location, "%qD is not a member of %qD", name, scope);
4947 }
4948 else
4949 {
4950 auto_diagnostic_group d;
4951 name_hint hint = suggest_alternatives_for (location, name, true);
4952 if (const char *suggestion = hint.suggestion ())
4953 {
4954 gcc_rich_location richloc (location);
4955 richloc.add_fixit_replace (suggestion);
4956 error_at (&richloc,
4957 "%<::%D%> has not been declared; did you mean %qs?",
4958 name, suggestion);
4959 }
4960 else
4961 error_at (location, "%<::%D%> has not been declared", name);
4962 }
4963 }
4964
4965 /* C++-specific implementation of range_label::get_text () vfunc for
4966 range_label_for_type_mismatch.
4967
4968 Compare with print_template_differences above. */
4969
4970 label_text
4971 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4972 {
4973 if (m_labelled_type == NULL_TREE)
4974 return label_text::borrow (NULL);
4975
4976 const bool verbose = false;
4977 const bool show_color = false;
4978
4979 const char *result;
4980 if (m_other_type
4981 && comparable_template_types_p (m_labelled_type, m_other_type))
4982 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4983 verbose, show_color,
4984 nullptr, nullptr);
4985 else
4986 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4987
4988 /* Both of the above return GC-allocated buffers, so the caller mustn't
4989 free them. */
4990 return label_text::borrow (result);
4991 }