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