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