c++: Detect deduction guide redeclaration [PR97099]
[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 NEGATE_EXPR:
2404 case BIT_NOT_EXPR:
2405 case TRUTH_NOT_EXPR:
2406 case PREDECREMENT_EXPR:
2407 case PREINCREMENT_EXPR:
2408 dump_unary_op (pp, OVL_OP_INFO (false, TREE_CODE (t))->name, t, flags);
2409 break;
2410
2411 case POSTDECREMENT_EXPR:
2412 case POSTINCREMENT_EXPR:
2413 pp_cxx_left_paren (pp);
2414 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2415 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2416 pp_cxx_right_paren (pp);
2417 break;
2418
2419 case NON_LVALUE_EXPR:
2420 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2421 should be another level of INDIRECT_REF so that I don't have to do
2422 this. */
2423 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2424 {
2425 tree next = TREE_TYPE (TREE_TYPE (t));
2426
2427 while (TYPE_PTR_P (next))
2428 next = TREE_TYPE (next);
2429
2430 if (TREE_CODE (next) == FUNCTION_TYPE)
2431 {
2432 if (flags & TFF_EXPR_IN_PARENS)
2433 pp_cxx_left_paren (pp);
2434 pp_cxx_star (pp);
2435 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2436 if (flags & TFF_EXPR_IN_PARENS)
2437 pp_cxx_right_paren (pp);
2438 break;
2439 }
2440 /* Else fall through. */
2441 }
2442 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2443 break;
2444
2445 CASE_CONVERT:
2446 case IMPLICIT_CONV_EXPR:
2447 case VIEW_CONVERT_EXPR:
2448 {
2449 tree op = TREE_OPERAND (t, 0);
2450 tree ttype = TREE_TYPE (t);
2451 tree optype = TREE_TYPE (op);
2452
2453 if (TREE_CODE (ttype) != TREE_CODE (optype)
2454 && INDIRECT_TYPE_P (ttype)
2455 && INDIRECT_TYPE_P (optype)
2456 && same_type_p (TREE_TYPE (optype),
2457 TREE_TYPE (ttype)))
2458 {
2459 if (TYPE_REF_P (ttype))
2460 {
2461 STRIP_NOPS (op);
2462 if (TREE_CODE (op) == ADDR_EXPR)
2463 dump_expr (pp, TREE_OPERAND (op, 0), flags);
2464 else
2465 dump_unary_op (pp, "*", t, flags);
2466 }
2467 else
2468 dump_unary_op (pp, "&", t, flags);
2469 }
2470 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2471 {
2472 /* It is a cast, but we cannot tell whether it is a
2473 reinterpret or static cast. Use the C style notation. */
2474 if (flags & TFF_EXPR_IN_PARENS)
2475 pp_cxx_left_paren (pp);
2476 pp_cxx_left_paren (pp);
2477 dump_type (pp, TREE_TYPE (t), flags);
2478 pp_cxx_right_paren (pp);
2479 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2480 if (flags & TFF_EXPR_IN_PARENS)
2481 pp_cxx_right_paren (pp);
2482 }
2483 else
2484 dump_expr (pp, op, flags);
2485 break;
2486 }
2487
2488 case CONSTRUCTOR:
2489 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2490 {
2491 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2492
2493 if (integer_zerop (idx))
2494 {
2495 /* A NULL pointer-to-member constant. */
2496 pp_cxx_left_paren (pp);
2497 pp_cxx_left_paren (pp);
2498 dump_type (pp, TREE_TYPE (t), flags);
2499 pp_cxx_right_paren (pp);
2500 pp_character (pp, '0');
2501 pp_cxx_right_paren (pp);
2502 break;
2503 }
2504 else if (tree_fits_shwi_p (idx))
2505 {
2506 tree virtuals;
2507 unsigned HOST_WIDE_INT n;
2508
2509 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2510 t = TYPE_METHOD_BASETYPE (t);
2511 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2512
2513 n = tree_to_shwi (idx);
2514
2515 /* Map vtable index back one, to allow for the null pointer to
2516 member. */
2517 --n;
2518
2519 while (n > 0 && virtuals)
2520 {
2521 --n;
2522 virtuals = TREE_CHAIN (virtuals);
2523 }
2524 if (virtuals)
2525 {
2526 dump_expr (pp, BV_FN (virtuals),
2527 flags | TFF_EXPR_IN_PARENS);
2528 break;
2529 }
2530 }
2531 }
2532 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2533 pp_string (pp, "<lambda closure object>");
2534 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2535 {
2536 dump_type (pp, TREE_TYPE (t), 0);
2537 pp_cxx_left_paren (pp);
2538 pp_cxx_right_paren (pp);
2539 }
2540 else
2541 {
2542 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2543 dump_type (pp, TREE_TYPE (t), 0);
2544 pp_cxx_left_brace (pp);
2545 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2546 pp_cxx_right_brace (pp);
2547 }
2548
2549 break;
2550
2551 case OFFSET_REF:
2552 {
2553 tree ob = TREE_OPERAND (t, 0);
2554 if (is_dummy_object (ob))
2555 {
2556 t = TREE_OPERAND (t, 1);
2557 if (TREE_CODE (t) == FUNCTION_DECL)
2558 /* A::f */
2559 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2560 else if (BASELINK_P (t))
2561 dump_expr (pp, OVL_FIRST (BASELINK_FUNCTIONS (t)),
2562 flags | TFF_EXPR_IN_PARENS);
2563 else
2564 dump_decl (pp, t, flags);
2565 }
2566 else
2567 {
2568 if (INDIRECT_REF_P (ob))
2569 {
2570 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2571 pp_cxx_arrow (pp);
2572 pp_cxx_star (pp);
2573 }
2574 else
2575 {
2576 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2577 pp_cxx_dot (pp);
2578 pp_cxx_star (pp);
2579 }
2580 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2581 }
2582 break;
2583 }
2584
2585 case TEMPLATE_PARM_INDEX:
2586 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2587 break;
2588
2589 case CAST_EXPR:
2590 if (TREE_OPERAND (t, 0) == NULL_TREE
2591 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2592 {
2593 dump_type (pp, TREE_TYPE (t), flags);
2594 pp_cxx_left_paren (pp);
2595 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2596 pp_cxx_right_paren (pp);
2597 }
2598 else
2599 {
2600 pp_cxx_left_paren (pp);
2601 dump_type (pp, TREE_TYPE (t), flags);
2602 pp_cxx_right_paren (pp);
2603 pp_cxx_left_paren (pp);
2604 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2605 pp_cxx_right_paren (pp);
2606 }
2607 break;
2608
2609 case STATIC_CAST_EXPR:
2610 pp_cxx_ws_string (pp, "static_cast");
2611 goto cast;
2612 case REINTERPRET_CAST_EXPR:
2613 pp_cxx_ws_string (pp, "reinterpret_cast");
2614 goto cast;
2615 case CONST_CAST_EXPR:
2616 pp_cxx_ws_string (pp, "const_cast");
2617 goto cast;
2618 case DYNAMIC_CAST_EXPR:
2619 pp_cxx_ws_string (pp, "dynamic_cast");
2620 cast:
2621 pp_cxx_begin_template_argument_list (pp);
2622 dump_type (pp, TREE_TYPE (t), flags);
2623 pp_cxx_end_template_argument_list (pp);
2624 pp_cxx_left_paren (pp);
2625 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2626 pp_cxx_right_paren (pp);
2627 break;
2628
2629 case ARROW_EXPR:
2630 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2631 pp_cxx_arrow (pp);
2632 break;
2633
2634 case SIZEOF_EXPR:
2635 case ALIGNOF_EXPR:
2636 if (TREE_CODE (t) == SIZEOF_EXPR)
2637 pp_cxx_ws_string (pp, "sizeof");
2638 else
2639 {
2640 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2641 pp_cxx_ws_string (pp, "__alignof__");
2642 }
2643 op = TREE_OPERAND (t, 0);
2644 if (PACK_EXPANSION_P (op))
2645 {
2646 pp_string (pp, "...");
2647 op = PACK_EXPANSION_PATTERN (op);
2648 }
2649 pp_cxx_whitespace (pp);
2650 pp_cxx_left_paren (pp);
2651 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2652 dump_type (pp, TREE_TYPE (op), flags);
2653 else if (TYPE_P (TREE_OPERAND (t, 0)))
2654 dump_type (pp, op, flags);
2655 else
2656 dump_expr (pp, op, flags);
2657 pp_cxx_right_paren (pp);
2658 break;
2659
2660 case AT_ENCODE_EXPR:
2661 pp_cxx_ws_string (pp, "@encode");
2662 pp_cxx_whitespace (pp);
2663 pp_cxx_left_paren (pp);
2664 dump_type (pp, TREE_OPERAND (t, 0), flags);
2665 pp_cxx_right_paren (pp);
2666 break;
2667
2668 case NOEXCEPT_EXPR:
2669 pp_cxx_ws_string (pp, "noexcept");
2670 pp_cxx_whitespace (pp);
2671 pp_cxx_left_paren (pp);
2672 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2673 pp_cxx_right_paren (pp);
2674 break;
2675
2676 case REALPART_EXPR:
2677 case IMAGPART_EXPR:
2678 pp_cxx_ws_string (pp, OVL_OP_INFO (false, TREE_CODE (t))->name);
2679 pp_cxx_whitespace (pp);
2680 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2681 break;
2682
2683 case DEFERRED_PARSE:
2684 pp_string (pp, M_("<unparsed>"));
2685 break;
2686
2687 case TRY_CATCH_EXPR:
2688 case CLEANUP_POINT_EXPR:
2689 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2690 break;
2691
2692 case PSEUDO_DTOR_EXPR:
2693 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2694 pp_cxx_dot (pp);
2695 if (TREE_OPERAND (t, 1))
2696 {
2697 dump_type (pp, TREE_OPERAND (t, 1), flags);
2698 pp_cxx_colon_colon (pp);
2699 }
2700 pp_cxx_complement (pp);
2701 dump_type (pp, TREE_OPERAND (t, 2), flags);
2702 break;
2703
2704 case TEMPLATE_ID_EXPR:
2705 dump_decl (pp, t, flags);
2706 break;
2707
2708 case BIND_EXPR:
2709 case STMT_EXPR:
2710 case EXPR_STMT:
2711 case STATEMENT_LIST:
2712 /* We don't yet have a way of dumping statements in a
2713 human-readable format. */
2714 pp_string (pp, "({...})");
2715 break;
2716
2717 case LOOP_EXPR:
2718 pp_string (pp, "while (1) { ");
2719 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2720 pp_cxx_right_brace (pp);
2721 break;
2722
2723 case EXIT_EXPR:
2724 pp_string (pp, "if (");
2725 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2726 pp_string (pp, ") break; ");
2727 break;
2728
2729 case BASELINK:
2730 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2731 break;
2732
2733 case EMPTY_CLASS_EXPR:
2734 dump_type (pp, TREE_TYPE (t), flags);
2735 pp_cxx_left_paren (pp);
2736 pp_cxx_right_paren (pp);
2737 break;
2738
2739 case NON_DEPENDENT_EXPR:
2740 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2741 break;
2742
2743 case ARGUMENT_PACK_SELECT:
2744 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2745 break;
2746
2747 case RECORD_TYPE:
2748 case UNION_TYPE:
2749 case ENUMERAL_TYPE:
2750 case REAL_TYPE:
2751 case VOID_TYPE:
2752 case BOOLEAN_TYPE:
2753 case INTEGER_TYPE:
2754 case COMPLEX_TYPE:
2755 case VECTOR_TYPE:
2756 case DECLTYPE_TYPE:
2757 pp_type_specifier_seq (pp, t);
2758 break;
2759
2760 case TYPENAME_TYPE:
2761 /* We get here when we want to print a dependent type as an
2762 id-expression, without any disambiguator decoration. */
2763 pp->id_expression (t);
2764 break;
2765
2766 case TEMPLATE_TYPE_PARM:
2767 case TEMPLATE_TEMPLATE_PARM:
2768 case BOUND_TEMPLATE_TEMPLATE_PARM:
2769 dump_type (pp, t, flags);
2770 break;
2771
2772 case TRAIT_EXPR:
2773 pp_cxx_trait_expression (pp, t);
2774 break;
2775
2776 case VA_ARG_EXPR:
2777 pp_cxx_va_arg_expression (pp, t);
2778 break;
2779
2780 case OFFSETOF_EXPR:
2781 pp_cxx_offsetof_expression (pp, t);
2782 break;
2783
2784 case ADDRESSOF_EXPR:
2785 pp_cxx_addressof_expression (pp, t);
2786 break;
2787
2788 case SCOPE_REF:
2789 dump_decl (pp, t, flags);
2790 break;
2791
2792 case EXPR_PACK_EXPANSION:
2793 case UNARY_LEFT_FOLD_EXPR:
2794 case UNARY_RIGHT_FOLD_EXPR:
2795 case BINARY_LEFT_FOLD_EXPR:
2796 case BINARY_RIGHT_FOLD_EXPR:
2797 case TYPEID_EXPR:
2798 case MEMBER_REF:
2799 case DOTSTAR_EXPR:
2800 case NEW_EXPR:
2801 case VEC_NEW_EXPR:
2802 case DELETE_EXPR:
2803 case VEC_DELETE_EXPR:
2804 case MODOP_EXPR:
2805 case ABS_EXPR:
2806 case ABSU_EXPR:
2807 case CONJ_EXPR:
2808 case VECTOR_CST:
2809 case FIXED_CST:
2810 case UNORDERED_EXPR:
2811 case ORDERED_EXPR:
2812 case UNLT_EXPR:
2813 case UNLE_EXPR:
2814 case UNGT_EXPR:
2815 case UNGE_EXPR:
2816 case UNEQ_EXPR:
2817 case LTGT_EXPR:
2818 case COMPLEX_EXPR:
2819 case BIT_FIELD_REF:
2820 case FIX_TRUNC_EXPR:
2821 case FLOAT_EXPR:
2822 pp->expression (t);
2823 break;
2824
2825 case TRUTH_AND_EXPR:
2826 case TRUTH_OR_EXPR:
2827 case TRUTH_XOR_EXPR:
2828 if (flags & TFF_EXPR_IN_PARENS)
2829 pp_cxx_left_paren (pp);
2830 pp->expression (t);
2831 if (flags & TFF_EXPR_IN_PARENS)
2832 pp_cxx_right_paren (pp);
2833 break;
2834
2835 case OBJ_TYPE_REF:
2836 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2837 break;
2838
2839 case LAMBDA_EXPR:
2840 pp_string (pp, M_("<lambda>"));
2841 break;
2842
2843 case PAREN_EXPR:
2844 pp_cxx_left_paren (pp);
2845 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2846 pp_cxx_right_paren (pp);
2847 break;
2848
2849 case REQUIRES_EXPR:
2850 pp_cxx_requires_expr (cxx_pp, t);
2851 break;
2852
2853 case SIMPLE_REQ:
2854 pp_cxx_simple_requirement (cxx_pp, t);
2855 break;
2856
2857 case TYPE_REQ:
2858 pp_cxx_type_requirement (cxx_pp, t);
2859 break;
2860
2861 case COMPOUND_REQ:
2862 pp_cxx_compound_requirement (cxx_pp, t);
2863 break;
2864
2865 case NESTED_REQ:
2866 pp_cxx_nested_requirement (cxx_pp, t);
2867 break;
2868
2869 case ATOMIC_CONSTR:
2870 case CHECK_CONSTR:
2871 case CONJ_CONSTR:
2872 case DISJ_CONSTR:
2873 {
2874 pp_cxx_constraint (cxx_pp, t);
2875 break;
2876 }
2877
2878 case PLACEHOLDER_EXPR:
2879 pp_string (pp, M_("*this"));
2880 break;
2881
2882 case TREE_LIST:
2883 dump_expr_list (pp, t, flags);
2884 break;
2885
2886 /* This list is incomplete, but should suffice for now.
2887 It is very important that `sorry' does not call
2888 `report_error_function'. That could cause an infinite loop. */
2889 default:
2890 pp_unsupported_tree (pp, t);
2891 /* Fall through. */
2892 case ERROR_MARK:
2893 pp_string (pp, M_("<expression error>"));
2894 break;
2895 }
2896 }
2897
2898 static void
2899 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2900 int flags)
2901 {
2902 pp_cxx_left_paren (pp);
2903 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2904 pp_cxx_whitespace (pp);
2905 if (opstring)
2906 pp_cxx_ws_string (pp, opstring);
2907 else
2908 pp_string (pp, M_("<unknown operator>"));
2909 pp_cxx_whitespace (pp);
2910 tree op1 = TREE_OPERAND (t, 1);
2911 if (TREE_CODE (t) == POINTER_PLUS_EXPR
2912 && TREE_CODE (op1) == INTEGER_CST
2913 && tree_int_cst_sign_bit (op1))
2914 /* A pointer minus an integer is represented internally as plus a very
2915 large number, don't expose that to users. */
2916 op1 = convert (ssizetype, op1);
2917 dump_expr (pp, op1, flags | TFF_EXPR_IN_PARENS);
2918 pp_cxx_right_paren (pp);
2919 }
2920
2921 static void
2922 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2923 {
2924 if (flags & TFF_EXPR_IN_PARENS)
2925 pp_cxx_left_paren (pp);
2926 pp_cxx_ws_string (pp, opstring);
2927 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2928 if (flags & TFF_EXPR_IN_PARENS)
2929 pp_cxx_right_paren (pp);
2930 }
2931
2932 static void
2933 reinit_cxx_pp (void)
2934 {
2935 pp_clear_output_area (cxx_pp);
2936 cxx_pp->padding = pp_none;
2937 pp_indentation (cxx_pp) = 0;
2938 pp_needs_newline (cxx_pp) = false;
2939 cxx_pp->enclosing_scope = current_function_decl;
2940 }
2941
2942 /* Same as pp_formatted_text, except the return string is a separate
2943 copy and has a GGC storage duration, e.g. an indefinite lifetime. */
2944
2945 inline const char *
2946 pp_ggc_formatted_text (pretty_printer *pp)
2947 {
2948 return ggc_strdup (pp_formatted_text (pp));
2949 }
2950
2951 /* Exported interface to stringifying types, exprs and decls under TFF_*
2952 control. */
2953
2954 const char *
2955 type_as_string (tree typ, int flags)
2956 {
2957 reinit_cxx_pp ();
2958 pp_translate_identifiers (cxx_pp) = false;
2959 dump_type (cxx_pp, typ, flags);
2960 return pp_ggc_formatted_text (cxx_pp);
2961 }
2962
2963 const char *
2964 type_as_string_translate (tree typ, int flags)
2965 {
2966 reinit_cxx_pp ();
2967 dump_type (cxx_pp, typ, flags);
2968 return pp_ggc_formatted_text (cxx_pp);
2969 }
2970
2971 const char *
2972 expr_as_string (tree decl, int flags)
2973 {
2974 reinit_cxx_pp ();
2975 pp_translate_identifiers (cxx_pp) = false;
2976 dump_expr (cxx_pp, decl, flags);
2977 return pp_ggc_formatted_text (cxx_pp);
2978 }
2979
2980 /* Wrap decl_as_string with options appropriate for dwarf. */
2981
2982 const char *
2983 decl_as_dwarf_string (tree decl, int flags)
2984 {
2985 const char *name;
2986 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2987 here will be adequate to get the desired behavior. */
2988 cxx_pp->flags |= pp_c_flag_gnu_v3;
2989 name = decl_as_string (decl, flags);
2990 /* Subsequent calls to the pretty printer shouldn't use this style. */
2991 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2992 return name;
2993 }
2994
2995 const char *
2996 decl_as_string (tree decl, int flags)
2997 {
2998 reinit_cxx_pp ();
2999 pp_translate_identifiers (cxx_pp) = false;
3000 dump_decl (cxx_pp, decl, flags);
3001 return pp_ggc_formatted_text (cxx_pp);
3002 }
3003
3004 const char *
3005 decl_as_string_translate (tree decl, int flags)
3006 {
3007 reinit_cxx_pp ();
3008 dump_decl (cxx_pp, decl, flags);
3009 return pp_ggc_formatted_text (cxx_pp);
3010 }
3011
3012 /* Wrap lang_decl_name with options appropriate for dwarf. */
3013
3014 const char *
3015 lang_decl_dwarf_name (tree decl, int v, bool translate)
3016 {
3017 const char *name;
3018 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
3019 here will be adequate to get the desired behavior. */
3020 cxx_pp->flags |= pp_c_flag_gnu_v3;
3021 name = lang_decl_name (decl, v, translate);
3022 /* Subsequent calls to the pretty printer shouldn't use this style. */
3023 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
3024 return name;
3025 }
3026
3027 /* Generate the three forms of printable names for cxx_printable_name. */
3028
3029 const char *
3030 lang_decl_name (tree decl, int v, bool translate)
3031 {
3032 if (v >= 2)
3033 return (translate
3034 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
3035 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
3036
3037 reinit_cxx_pp ();
3038 pp_translate_identifiers (cxx_pp) = translate;
3039 if (v == 1
3040 && (DECL_CLASS_SCOPE_P (decl)
3041 || (DECL_NAMESPACE_SCOPE_P (decl)
3042 && CP_DECL_CONTEXT (decl) != global_namespace)))
3043 {
3044 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
3045 pp_cxx_colon_colon (cxx_pp);
3046 }
3047
3048 if (TREE_CODE (decl) == FUNCTION_DECL)
3049 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
3050 else if ((DECL_NAME (decl) == NULL_TREE)
3051 && TREE_CODE (decl) == NAMESPACE_DECL)
3052 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
3053 else
3054 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
3055
3056 return pp_ggc_formatted_text (cxx_pp);
3057 }
3058
3059 /* Return the location of a tree passed to %+ formats. */
3060
3061 location_t
3062 location_of (tree t)
3063 {
3064 if (TYPE_P (t))
3065 {
3066 t = TYPE_MAIN_DECL (t);
3067 if (t == NULL_TREE)
3068 return input_location;
3069 }
3070 else if (TREE_CODE (t) == OVERLOAD)
3071 t = OVL_FIRST (t);
3072
3073 if (DECL_P (t))
3074 return DECL_SOURCE_LOCATION (t);
3075 if (TREE_CODE (t) == DEFERRED_PARSE)
3076 return defparse_location (t);
3077 return cp_expr_loc_or_input_loc (t);
3078 }
3079
3080 /* Now the interfaces from error et al to dump_type et al. Each takes an
3081 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
3082 function. */
3083
3084 static const char *
3085 decl_to_string (tree decl, int verbose)
3086 {
3087 int flags = 0;
3088
3089 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
3090 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
3091 flags = TFF_CLASS_KEY_OR_ENUM;
3092 if (verbose)
3093 flags |= TFF_DECL_SPECIFIERS;
3094 else if (TREE_CODE (decl) == FUNCTION_DECL)
3095 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
3096 flags |= TFF_TEMPLATE_HEADER;
3097
3098 reinit_cxx_pp ();
3099 dump_decl (cxx_pp, decl, flags);
3100 return pp_ggc_formatted_text (cxx_pp);
3101 }
3102
3103 const char *
3104 expr_to_string (tree decl)
3105 {
3106 reinit_cxx_pp ();
3107 dump_expr (cxx_pp, decl, 0);
3108 return pp_ggc_formatted_text (cxx_pp);
3109 }
3110
3111 static const char *
3112 fndecl_to_string (tree fndecl, int verbose)
3113 {
3114 int flags;
3115
3116 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
3117 | TFF_TEMPLATE_HEADER;
3118 if (verbose)
3119 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
3120 reinit_cxx_pp ();
3121 dump_decl (cxx_pp, fndecl, flags);
3122 return pp_ggc_formatted_text (cxx_pp);
3123 }
3124
3125
3126 static const char *
3127 code_to_string (enum tree_code c)
3128 {
3129 return get_tree_code_name (c);
3130 }
3131
3132 const char *
3133 language_to_string (enum languages c)
3134 {
3135 switch (c)
3136 {
3137 case lang_c:
3138 return "C";
3139
3140 case lang_cplusplus:
3141 return "C++";
3142
3143 default:
3144 gcc_unreachable ();
3145 }
3146 return NULL;
3147 }
3148
3149 /* Return the proper printed version of a parameter to a C++ function. */
3150
3151 static const char *
3152 parm_to_string (int p)
3153 {
3154 reinit_cxx_pp ();
3155 if (p < 0)
3156 pp_string (cxx_pp, "'this'");
3157 else
3158 pp_decimal_int (cxx_pp, p + 1);
3159 return pp_ggc_formatted_text (cxx_pp);
3160 }
3161
3162 static const char *
3163 op_to_string (bool assop, enum tree_code p)
3164 {
3165 tree id = ovl_op_identifier (assop, p);
3166 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
3167 }
3168
3169 /* Return a GC-allocated representation of type TYP, with verbosity VERBOSE.
3170
3171 If QUOTE is non-NULL and if *QUOTE is true, then quotes are added to the
3172 string in appropriate places, and *QUOTE is written to with false
3173 to suppress pp_format's trailing close quote so that e.g.
3174 foo_typedef {aka underlying_foo} {enum}
3175 can be printed by "%qT" as:
3176 `foo_typedef' {aka `underlying_foo'} {enum}
3177 rather than:
3178 `foo_typedef {aka underlying_foo} {enum}'
3179 When adding such quotes, if POSTPROCESSED is true (for handling %H and %I)
3180 then a leading open quote will be added, whereas if POSTPROCESSED is false
3181 (for handling %T) then any leading quote has already been added by
3182 pp_format, or is not needed due to QUOTE being NULL (for template arguments
3183 within %H and %I).
3184
3185 SHOW_COLOR is used to determine the colorization of any quotes that
3186 are added. */
3187
3188 static const char *
3189 type_to_string (tree typ, int verbose, bool postprocessed, bool *quote,
3190 bool show_color)
3191 {
3192 int flags = 0;
3193 if (verbose)
3194 flags |= TFF_CLASS_KEY_OR_ENUM;
3195 flags |= TFF_TEMPLATE_HEADER;
3196
3197 reinit_cxx_pp ();
3198
3199 if (postprocessed && quote && *quote)
3200 pp_begin_quote (cxx_pp, show_color);
3201
3202 struct obstack *ob = pp_buffer (cxx_pp)->obstack;
3203 int type_start, type_len;
3204 type_start = obstack_object_size (ob);
3205
3206 dump_type (cxx_pp, typ, flags);
3207
3208 /* Remember the end of the initial dump. */
3209 type_len = obstack_object_size (ob) - type_start;
3210
3211 /* If we're printing a type that involves typedefs, also print the
3212 stripped version. But sometimes the stripped version looks
3213 exactly the same, so we don't want it after all. To avoid printing
3214 it in that case, we play ugly obstack games. */
3215 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
3216 && !uses_template_parms (typ))
3217 {
3218 int aka_start, aka_len; char *p;
3219 tree aka = strip_typedefs (typ, NULL, STF_USER_VISIBLE);
3220 if (quote && *quote)
3221 pp_end_quote (cxx_pp, show_color);
3222 pp_string (cxx_pp, " {aka");
3223 pp_cxx_whitespace (cxx_pp);
3224 if (quote && *quote)
3225 pp_begin_quote (cxx_pp, show_color);
3226 /* And remember the start of the aka dump. */
3227 aka_start = obstack_object_size (ob);
3228 dump_type (cxx_pp, aka, flags);
3229 aka_len = obstack_object_size (ob) - aka_start;
3230 if (quote && *quote)
3231 pp_end_quote (cxx_pp, show_color);
3232 pp_right_brace (cxx_pp);
3233 p = (char*)obstack_base (ob);
3234 /* If they are identical, cut off the aka by unwinding the obstack. */
3235 if (type_len == aka_len
3236 && memcmp (p + type_start, p+aka_start, type_len) == 0)
3237 {
3238 /* We can't add a '\0' here, since we may be adding a closing quote
3239 below, and it would be hidden by the '\0'.
3240 Instead, manually unwind the current object within the obstack
3241 so that the insertion point is at the end of the type, before
3242 the "' {aka". */
3243 int delta = type_start + type_len - obstack_object_size (ob);
3244 gcc_assert (delta <= 0);
3245 obstack_blank_fast (ob, delta);
3246 }
3247 else
3248 if (quote)
3249 /* No further closing quotes are needed. */
3250 *quote = false;
3251 }
3252
3253 if (quote && *quote)
3254 {
3255 pp_end_quote (cxx_pp, show_color);
3256 *quote = false;
3257 }
3258 return pp_ggc_formatted_text (cxx_pp);
3259 }
3260
3261 static const char *
3262 args_to_string (tree p, int verbose)
3263 {
3264 int flags = 0;
3265 if (verbose)
3266 flags |= TFF_CLASS_KEY_OR_ENUM;
3267
3268 if (p == NULL_TREE)
3269 return "";
3270
3271 if (TYPE_P (TREE_VALUE (p)))
3272 return type_as_string_translate (p, flags);
3273
3274 reinit_cxx_pp ();
3275 for (; p; p = TREE_CHAIN (p))
3276 {
3277 if (null_node_p (TREE_VALUE (p)))
3278 pp_cxx_ws_string (cxx_pp, "NULL");
3279 else
3280 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
3281 if (TREE_CHAIN (p))
3282 pp_separate_with_comma (cxx_pp);
3283 }
3284 return pp_ggc_formatted_text (cxx_pp);
3285 }
3286
3287 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
3288 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
3289 arguments. */
3290
3291 static const char *
3292 subst_to_string (tree p)
3293 {
3294 tree decl = TREE_PURPOSE (p);
3295 tree targs = TREE_VALUE (p);
3296 tree tparms = DECL_TEMPLATE_PARMS (decl);
3297 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
3298 |TFF_NO_TEMPLATE_BINDINGS);
3299
3300 if (p == NULL_TREE)
3301 return "";
3302
3303 reinit_cxx_pp ();
3304 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
3305 dump_substitution (cxx_pp, NULL, tparms, targs, /*flags=*/0);
3306 return pp_ggc_formatted_text (cxx_pp);
3307 }
3308
3309 static const char *
3310 cv_to_string (tree p, int v)
3311 {
3312 reinit_cxx_pp ();
3313 cxx_pp->padding = v ? pp_before : pp_none;
3314 pp_cxx_cv_qualifier_seq (cxx_pp, p);
3315 return pp_ggc_formatted_text (cxx_pp);
3316 }
3317
3318 static const char *
3319 eh_spec_to_string (tree p, int /*v*/)
3320 {
3321 int flags = 0;
3322 reinit_cxx_pp ();
3323 dump_exception_spec (cxx_pp, p, flags);
3324 return pp_ggc_formatted_text (cxx_pp);
3325 }
3326
3327 /* Langhook for print_error_function. */
3328 void
3329 cxx_print_error_function (diagnostic_context *context, const char *file,
3330 diagnostic_info *diagnostic)
3331 {
3332 char *prefix;
3333 if (file)
3334 prefix = xstrdup (file);
3335 else
3336 prefix = NULL;
3337 lhd_print_error_function (context, file, diagnostic);
3338 pp_set_prefix (context->printer, prefix);
3339 maybe_print_instantiation_context (context);
3340 }
3341
3342 static void
3343 cp_diagnostic_starter (diagnostic_context *context,
3344 diagnostic_info *diagnostic)
3345 {
3346 diagnostic_report_current_module (context, diagnostic_location (diagnostic));
3347 cp_print_error_function (context, diagnostic);
3348 maybe_print_instantiation_context (context);
3349 maybe_print_constexpr_context (context);
3350 maybe_print_constraint_context (context);
3351 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
3352 diagnostic));
3353 }
3354
3355 /* Print current function onto BUFFER, in the process of reporting
3356 a diagnostic message. Called from cp_diagnostic_starter. */
3357 static void
3358 cp_print_error_function (diagnostic_context *context,
3359 diagnostic_info *diagnostic)
3360 {
3361 /* If we are in an instantiation context, current_function_decl is likely
3362 to be wrong, so just rely on print_instantiation_full_context. */
3363 if (current_instantiation ())
3364 return;
3365 /* The above is true for constraint satisfaction also. */
3366 if (current_failed_constraint)
3367 return;
3368 if (diagnostic_last_function_changed (context, diagnostic))
3369 {
3370 char *old_prefix = pp_take_prefix (context->printer);
3371 const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
3372 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3373 char *new_prefix = (file && abstract_origin == NULL)
3374 ? file_name_as_prefix (context, file) : NULL;
3375
3376 pp_set_prefix (context->printer, new_prefix);
3377
3378 if (current_function_decl == NULL)
3379 pp_string (context->printer, _("At global scope:"));
3380 else
3381 {
3382 tree fndecl, ao;
3383
3384 if (abstract_origin)
3385 {
3386 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3387 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3388 fndecl = ao;
3389 }
3390 else
3391 fndecl = current_function_decl;
3392
3393 pp_printf (context->printer, function_category (fndecl),
3394 cxx_printable_name_translate (fndecl, 2));
3395
3396 while (abstract_origin)
3397 {
3398 location_t *locus;
3399 tree block = abstract_origin;
3400
3401 locus = &BLOCK_SOURCE_LOCATION (block);
3402 fndecl = NULL;
3403 block = BLOCK_SUPERCONTEXT (block);
3404 while (block && TREE_CODE (block) == BLOCK
3405 && BLOCK_ABSTRACT_ORIGIN (block))
3406 {
3407 ao = BLOCK_ABSTRACT_ORIGIN (block);
3408 if (TREE_CODE (ao) == FUNCTION_DECL)
3409 {
3410 fndecl = ao;
3411 break;
3412 }
3413 else if (TREE_CODE (ao) != BLOCK)
3414 break;
3415
3416 block = BLOCK_SUPERCONTEXT (block);
3417 }
3418 if (fndecl)
3419 abstract_origin = block;
3420 else
3421 {
3422 while (block && TREE_CODE (block) == BLOCK)
3423 block = BLOCK_SUPERCONTEXT (block);
3424
3425 if (block && TREE_CODE (block) == FUNCTION_DECL)
3426 fndecl = block;
3427 abstract_origin = NULL;
3428 }
3429 if (fndecl)
3430 {
3431 expanded_location s = expand_location (*locus);
3432 pp_character (context->printer, ',');
3433 pp_newline (context->printer);
3434 if (s.file != NULL)
3435 {
3436 if (context->show_column && s.column != 0)
3437 pp_printf (context->printer,
3438 _(" inlined from %qs at %r%s:%d:%d%R"),
3439 cxx_printable_name_translate (fndecl, 2),
3440 "locus", s.file, s.line, s.column);
3441 else
3442 pp_printf (context->printer,
3443 _(" inlined from %qs at %r%s:%d%R"),
3444 cxx_printable_name_translate (fndecl, 2),
3445 "locus", s.file, s.line);
3446
3447 }
3448 else
3449 pp_printf (context->printer, _(" inlined from %qs"),
3450 cxx_printable_name_translate (fndecl, 2));
3451 }
3452 }
3453 pp_character (context->printer, ':');
3454 }
3455 pp_newline (context->printer);
3456
3457 diagnostic_set_last_function (context, diagnostic);
3458 pp_destroy_prefix (context->printer);
3459 context->printer->prefix = old_prefix;
3460 }
3461 }
3462
3463 /* Returns a description of FUNCTION using standard terminology. The
3464 result is a format string of the form "In CATEGORY %qs". */
3465 static const char *
3466 function_category (tree fn)
3467 {
3468 /* We can get called from the middle-end for diagnostics of function
3469 clones. Make sure we have language specific information before
3470 dereferencing it. */
3471 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3472 && DECL_FUNCTION_MEMBER_P (fn))
3473 {
3474 if (DECL_STATIC_FUNCTION_P (fn))
3475 return _("In static member function %qs");
3476 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3477 return _("In copy constructor %qs");
3478 else if (DECL_CONSTRUCTOR_P (fn))
3479 return _("In constructor %qs");
3480 else if (DECL_DESTRUCTOR_P (fn))
3481 return _("In destructor %qs");
3482 else if (LAMBDA_FUNCTION_P (fn))
3483 return _("In lambda function");
3484 else
3485 return _("In member function %qs");
3486 }
3487 else
3488 return _("In function %qs");
3489 }
3490
3491 /* Report the full context of a current template instantiation,
3492 onto BUFFER. */
3493 static void
3494 print_instantiation_full_context (diagnostic_context *context)
3495 {
3496 struct tinst_level *p = current_instantiation ();
3497 location_t location = input_location;
3498
3499 if (p)
3500 {
3501 pp_verbatim (context->printer,
3502 p->list_p ()
3503 ? _("%s: In substitution of %qS:\n")
3504 : _("%s: In instantiation of %q#D:\n"),
3505 LOCATION_FILE (location),
3506 p->get_node ());
3507
3508 location = p->locus;
3509 p = p->next;
3510 }
3511
3512 print_instantiation_partial_context (context, p, location);
3513 }
3514
3515 /* Helper function of print_instantiation_partial_context() that
3516 prints a single line of instantiation context. */
3517
3518 static void
3519 print_instantiation_partial_context_line (diagnostic_context *context,
3520 struct tinst_level *t,
3521 location_t loc, bool recursive_p)
3522 {
3523 if (loc == UNKNOWN_LOCATION)
3524 return;
3525
3526 expanded_location xloc = expand_location (loc);
3527
3528 if (context->show_column)
3529 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3530 "locus", xloc.file, xloc.line, xloc.column);
3531 else
3532 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3533 "locus", xloc.file, xloc.line);
3534
3535 if (t != NULL)
3536 {
3537 if (t->list_p ())
3538 pp_verbatim (context->printer,
3539 recursive_p
3540 ? _("recursively required by substitution of %qS\n")
3541 : _("required by substitution of %qS\n"),
3542 t->get_node ());
3543 else
3544 pp_verbatim (context->printer,
3545 recursive_p
3546 ? _("recursively required from %q#D\n")
3547 : _("required from %q#D\n"),
3548 t->get_node ());
3549 }
3550 else
3551 {
3552 pp_verbatim (context->printer,
3553 recursive_p
3554 ? _("recursively required from here\n")
3555 : _("required from here\n"));
3556 }
3557 }
3558
3559 /* Same as print_instantiation_full_context but less verbose. */
3560
3561 static void
3562 print_instantiation_partial_context (diagnostic_context *context,
3563 struct tinst_level *t0, location_t loc)
3564 {
3565 struct tinst_level *t;
3566 int n_total = 0;
3567 int n;
3568 location_t prev_loc = loc;
3569
3570 for (t = t0; t != NULL; t = t->next)
3571 if (prev_loc != t->locus)
3572 {
3573 prev_loc = t->locus;
3574 n_total++;
3575 }
3576
3577 t = t0;
3578
3579 if (template_backtrace_limit
3580 && n_total > template_backtrace_limit)
3581 {
3582 int skip = n_total - template_backtrace_limit;
3583 int head = template_backtrace_limit / 2;
3584
3585 /* Avoid skipping just 1. If so, skip 2. */
3586 if (skip == 1)
3587 {
3588 skip = 2;
3589 head = (template_backtrace_limit - 1) / 2;
3590 }
3591
3592 for (n = 0; n < head; n++)
3593 {
3594 gcc_assert (t != NULL);
3595 if (loc != t->locus)
3596 print_instantiation_partial_context_line (context, t, loc,
3597 /*recursive_p=*/false);
3598 loc = t->locus;
3599 t = t->next;
3600 }
3601 if (t != NULL && skip > 0)
3602 {
3603 expanded_location xloc;
3604 xloc = expand_location (loc);
3605 if (context->show_column)
3606 pp_verbatim (context->printer,
3607 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3608 "contexts, use -ftemplate-backtrace-limit=0 to "
3609 "disable ]\n"),
3610 "locus", xloc.file, xloc.line, xloc.column, skip);
3611 else
3612 pp_verbatim (context->printer,
3613 _("%r%s:%d:%R [ skipping %d instantiation "
3614 "contexts, use -ftemplate-backtrace-limit=0 to "
3615 "disable ]\n"),
3616 "locus", xloc.file, xloc.line, skip);
3617
3618 do {
3619 loc = t->locus;
3620 t = t->next;
3621 } while (t != NULL && --skip > 0);
3622 }
3623 }
3624
3625 while (t != NULL)
3626 {
3627 while (t->next != NULL && t->locus == t->next->locus)
3628 {
3629 loc = t->locus;
3630 t = t->next;
3631 }
3632 print_instantiation_partial_context_line (context, t, loc,
3633 t->locus == loc);
3634 loc = t->locus;
3635 t = t->next;
3636 }
3637 print_instantiation_partial_context_line (context, NULL, loc,
3638 /*recursive_p=*/false);
3639 }
3640
3641 /* Called from cp_thing to print the template context for an error. */
3642 static void
3643 maybe_print_instantiation_context (diagnostic_context *context)
3644 {
3645 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3646 return;
3647
3648 record_last_problematic_instantiation ();
3649 print_instantiation_full_context (context);
3650 }
3651 \f
3652 /* Report what constexpr call(s) we're trying to expand, if any. */
3653
3654 void
3655 maybe_print_constexpr_context (diagnostic_context *context)
3656 {
3657 vec<tree> call_stack = cx_error_context ();
3658 unsigned ix;
3659 tree t;
3660
3661 FOR_EACH_VEC_ELT (call_stack, ix, t)
3662 {
3663 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3664 const char *s = expr_as_string (t, 0);
3665 if (context->show_column)
3666 pp_verbatim (context->printer,
3667 _("%r%s:%d:%d:%R in %<constexpr%> expansion of %qs"),
3668 "locus", xloc.file, xloc.line, xloc.column, s);
3669 else
3670 pp_verbatim (context->printer,
3671 _("%r%s:%d:%R in %<constexpr%> expansion of %qs"),
3672 "locus", xloc.file, xloc.line, s);
3673 pp_newline (context->printer);
3674 }
3675 }
3676 \f
3677
3678 static void
3679 print_location (diagnostic_context *context, location_t loc)
3680 {
3681 expanded_location xloc = expand_location (loc);
3682 if (context->show_column)
3683 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3684 "locus", xloc.file, xloc.line, xloc.column);
3685 else
3686 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3687 "locus", xloc.file, xloc.line);
3688 }
3689
3690 static void
3691 print_constrained_decl_info (diagnostic_context *context, tree decl)
3692 {
3693 print_location (context, DECL_SOURCE_LOCATION (decl));
3694 pp_verbatim (context->printer, "required by the constraints of %q#D\n", decl);
3695 }
3696
3697 static void
3698 print_concept_check_info (diagnostic_context *context, tree expr, tree map, tree args)
3699 {
3700 gcc_assert (concept_check_p (expr));
3701
3702 tree id = unpack_concept_check (expr);
3703 tree tmpl = TREE_OPERAND (id, 0);
3704 if (OVL_P (tmpl))
3705 tmpl = OVL_FIRST (tmpl);
3706
3707 print_location (context, DECL_SOURCE_LOCATION (tmpl));
3708
3709 cxx_pretty_printer *pp = (cxx_pretty_printer *)context->printer;
3710 pp_verbatim (pp, "required for the satisfaction of %qE", expr);
3711 if (map && map != error_mark_node)
3712 {
3713 tree subst_map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3714 pp_cxx_parameter_mapping (pp, (subst_map != error_mark_node
3715 ? subst_map : map));
3716 }
3717 pp_newline (pp);
3718 }
3719
3720 /* Diagnose the entry point into the satisfaction error. Returns the next
3721 context, if any. */
3722
3723 static tree
3724 print_constraint_context_head (diagnostic_context *context, tree cxt, tree args)
3725 {
3726 tree src = TREE_VALUE (cxt);
3727 if (!src)
3728 {
3729 print_location (context, input_location);
3730 pp_verbatim (context->printer, "required for constraint satisfaction\n");
3731 return NULL_TREE;
3732 }
3733 if (DECL_P (src))
3734 {
3735 print_constrained_decl_info (context, src);
3736 return NULL_TREE;
3737 }
3738 else
3739 {
3740 print_concept_check_info (context, src, TREE_PURPOSE (cxt), args);
3741 return TREE_CHAIN (cxt);
3742 }
3743 }
3744
3745 static void
3746 print_requires_expression_info (diagnostic_context *context, tree constr, tree args)
3747 {
3748
3749 tree expr = ATOMIC_CONSTR_EXPR (constr);
3750 tree map = ATOMIC_CONSTR_MAP (constr);
3751 map = tsubst_parameter_mapping (map, args, tf_none, NULL_TREE);
3752 if (map == error_mark_node)
3753 return;
3754
3755 print_location (context, cp_expr_loc_or_input_loc (expr));
3756 pp_verbatim (context->printer, "in requirements ");
3757
3758 tree parms = TREE_OPERAND (expr, 0);
3759 if (parms)
3760 pp_verbatim (context->printer, "with ");
3761 while (parms)
3762 {
3763 pp_verbatim (context->printer, "%q#D", parms);
3764 if (TREE_CHAIN (parms))
3765 pp_separate_with_comma ((cxx_pretty_printer *)context->printer);
3766 parms = TREE_CHAIN (parms);
3767 }
3768 pp_cxx_parameter_mapping ((cxx_pretty_printer *)context->printer, map);
3769
3770 pp_verbatim (context->printer, "\n");
3771 }
3772
3773 void
3774 maybe_print_single_constraint_context (diagnostic_context *context, tree failed)
3775 {
3776 if (!failed)
3777 return;
3778
3779 tree constr = TREE_VALUE (failed);
3780 if (!constr || constr == error_mark_node)
3781 return;
3782 tree cxt = CONSTR_CONTEXT (constr);
3783 if (!cxt)
3784 return;
3785 tree args = TREE_PURPOSE (failed);
3786
3787 /* Print the stack of requirements. */
3788 cxt = print_constraint_context_head (context, cxt, args);
3789 while (cxt && !DECL_P (TREE_VALUE (cxt)))
3790 {
3791 tree expr = TREE_VALUE (cxt);
3792 tree map = TREE_PURPOSE (cxt);
3793 print_concept_check_info (context, expr, map, args);
3794 cxt = TREE_CHAIN (cxt);
3795 }
3796
3797 /* For certain constraints, we can provide additional context. */
3798 if (TREE_CODE (constr) == ATOMIC_CONSTR
3799 && TREE_CODE (ATOMIC_CONSTR_EXPR (constr)) == REQUIRES_EXPR)
3800 print_requires_expression_info (context, constr, args);
3801 }
3802
3803 void
3804 maybe_print_constraint_context (diagnostic_context *context)
3805 {
3806 if (!current_failed_constraint)
3807 return;
3808
3809 tree cur = current_failed_constraint;
3810
3811 /* Recursively print nested contexts. */
3812 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3813 if (current_failed_constraint)
3814 maybe_print_constraint_context (context);
3815
3816 /* Print this context. */
3817 maybe_print_single_constraint_context (context, cur);
3818 }
3819
3820 /* Return true iff TYPE_A and TYPE_B are template types that are
3821 meaningful to compare. */
3822
3823 static bool
3824 comparable_template_types_p (tree type_a, tree type_b)
3825 {
3826 if (!CLASS_TYPE_P (type_a))
3827 return false;
3828 if (!CLASS_TYPE_P (type_b))
3829 return false;
3830
3831 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3832 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3833 if (!tinfo_a || !tinfo_b)
3834 return false;
3835
3836 return TI_TEMPLATE (tinfo_a) == TI_TEMPLATE (tinfo_b);
3837 }
3838
3839 /* Start a new line indented by SPC spaces on PP. */
3840
3841 static void
3842 newline_and_indent (pretty_printer *pp, int spc)
3843 {
3844 pp_newline (pp);
3845 for (int i = 0; i < spc; i++)
3846 pp_space (pp);
3847 }
3848
3849 /* Generate a GC-allocated string for ARG, an expression or type. */
3850
3851 static const char *
3852 arg_to_string (tree arg, bool verbose)
3853 {
3854 if (TYPE_P (arg))
3855 return type_to_string (arg, verbose, true, NULL, false);
3856 else
3857 return expr_to_string (arg);
3858 }
3859
3860 /* Subroutine to type_to_string_with_compare and
3861 print_template_tree_comparison.
3862
3863 Print a representation of ARG (an expression or type) to PP,
3864 colorizing it as "type-diff" if PP->show_color. */
3865
3866 static void
3867 print_nonequal_arg (pretty_printer *pp, tree arg, bool verbose)
3868 {
3869 pp_printf (pp, "%r%s%R",
3870 "type-diff",
3871 (arg
3872 ? arg_to_string (arg, verbose)
3873 : G_("(no argument)")));
3874 }
3875
3876 /* Recursively print template TYPE_A to PP, as compared to template TYPE_B.
3877
3878 The types must satisfy comparable_template_types_p.
3879
3880 If INDENT is 0, then this is equivalent to type_to_string (TYPE_A), but
3881 potentially colorizing/eliding in comparison with TYPE_B.
3882
3883 For example given types:
3884 vector<map<int,double>>
3885 and
3886 vector<map<int,float>>
3887 then the result on PP would be:
3888 vector<map<[...],double>>
3889 with type elision, and:
3890 vector<map<int,double>>
3891 without type elision.
3892
3893 In both cases the parts of TYPE that differ from PEER will be colorized
3894 if pp_show_color (pp) is true. In the above example, this would be
3895 "double".
3896
3897 If INDENT is non-zero, then the types are printed in a tree-like form
3898 which shows both types. In the above example, the result on PP would be:
3899
3900 vector<
3901 map<
3902 [...],
3903 [double != float]>>
3904
3905 and without type-elision would be:
3906
3907 vector<
3908 map<
3909 int,
3910 [double != float]>>
3911
3912 As before, the differing parts of the types are colorized if
3913 pp_show_color (pp) is true ("double" and "float" in this example).
3914
3915 Template arguments in which both types are using the default arguments
3916 are not printed; if at least one of the two types is using a non-default
3917 argument, then that argument is printed (or both arguments for the
3918 tree-like print format). */
3919
3920 static void
3921 print_template_differences (pretty_printer *pp, tree type_a, tree type_b,
3922 bool verbose, int indent)
3923 {
3924 if (indent)
3925 newline_and_indent (pp, indent);
3926
3927 tree tinfo_a = TYPE_TEMPLATE_INFO (type_a);
3928 tree tinfo_b = TYPE_TEMPLATE_INFO (type_b);
3929
3930 pp_printf (pp, "%s<",
3931 IDENTIFIER_POINTER (DECL_NAME (TI_TEMPLATE (tinfo_a))));
3932
3933 tree args_a = TI_ARGS (tinfo_a);
3934 tree args_b = TI_ARGS (tinfo_b);
3935 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3936 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3937 int flags = 0;
3938 int len_a = get_non_default_template_args_count (args_a, flags);
3939 args_a = INNERMOST_TEMPLATE_ARGS (args_a);
3940 int len_b = get_non_default_template_args_count (args_b, flags);
3941 args_b = INNERMOST_TEMPLATE_ARGS (args_b);
3942 /* Determine the maximum range of args for which non-default template args
3943 were used; beyond this, only default args (if any) were used, and so
3944 they will be equal from this point onwards.
3945 One of the two peers might have used default arguments within this
3946 range, but the other will be using non-default arguments, and so
3947 it's more readable to print both within this range, to highlight
3948 the differences. */
3949 int len_max = MAX (len_a, len_b);
3950 gcc_assert (TREE_CODE (args_a) == TREE_VEC);
3951 gcc_assert (TREE_CODE (args_b) == TREE_VEC);
3952 for (int idx = 0; idx < len_max; idx++)
3953 {
3954 if (idx)
3955 pp_character (pp, ',');
3956
3957 tree arg_a = TREE_VEC_ELT (args_a, idx);
3958 tree arg_b = TREE_VEC_ELT (args_b, idx);
3959 if (arg_a == arg_b)
3960 {
3961 if (indent)
3962 newline_and_indent (pp, indent + 2);
3963 /* Can do elision here, printing "[...]". */
3964 if (flag_elide_type)
3965 pp_string (pp, G_("[...]"));
3966 else
3967 pp_string (pp, arg_to_string (arg_a, verbose));
3968 }
3969 else
3970 {
3971 int new_indent = indent ? indent + 2 : 0;
3972 if (comparable_template_types_p (arg_a, arg_b))
3973 print_template_differences (pp, arg_a, arg_b, verbose, new_indent);
3974 else
3975 if (indent)
3976 {
3977 newline_and_indent (pp, indent + 2);
3978 pp_character (pp, '[');
3979 print_nonequal_arg (pp, arg_a, verbose);
3980 pp_string (pp, " != ");
3981 print_nonequal_arg (pp, arg_b, verbose);
3982 pp_character (pp, ']');
3983 }
3984 else
3985 print_nonequal_arg (pp, arg_a, verbose);
3986 }
3987 }
3988 pp_printf (pp, ">");
3989 }
3990
3991 /* As type_to_string, but for a template, potentially colorizing/eliding
3992 in comparison with PEER.
3993 For example, if TYPE is map<int,double> and PEER is map<int,int>,
3994 then the resulting string would be:
3995 map<[...],double>
3996 with type elision, and:
3997 map<int,double>
3998 without type elision.
3999
4000 In both cases the parts of TYPE that differ from PEER will be colorized
4001 if SHOW_COLOR is true. In the above example, this would be "double".
4002
4003 Template arguments in which both types are using the default arguments
4004 are not printed; if at least one of the two types is using a non-default
4005 argument, then both arguments are printed.
4006
4007 The resulting string is in a GC-allocated buffer. */
4008
4009 static const char *
4010 type_to_string_with_compare (tree type, tree peer, bool verbose,
4011 bool show_color)
4012 {
4013 pretty_printer inner_pp;
4014 pretty_printer *pp = &inner_pp;
4015 pp_show_color (pp) = show_color;
4016
4017 print_template_differences (pp, type, peer, verbose, 0);
4018 return pp_ggc_formatted_text (pp);
4019 }
4020
4021 /* Recursively print a tree-like comparison of TYPE_A and TYPE_B to PP,
4022 indented by INDENT spaces.
4023
4024 For example given types:
4025
4026 vector<map<int,double>>
4027
4028 and
4029
4030 vector<map<double,float>>
4031
4032 the output with type elision would be:
4033
4034 vector<
4035 map<
4036 [...],
4037 [double != float]>>
4038
4039 and without type-elision would be:
4040
4041 vector<
4042 map<
4043 int,
4044 [double != float]>>
4045
4046 TYPE_A and TYPE_B must both be comparable template types
4047 (as per comparable_template_types_p).
4048
4049 Template arguments in which both types are using the default arguments
4050 are not printed; if at least one of the two types is using a non-default
4051 argument, then both arguments are printed. */
4052
4053 static void
4054 print_template_tree_comparison (pretty_printer *pp, tree type_a, tree type_b,
4055 bool verbose, int indent)
4056 {
4057 print_template_differences (pp, type_a, type_b, verbose, indent);
4058 }
4059
4060 /* Subroutine for use in a format_postprocessor::handle
4061 implementation. Adds a chunk to the end of
4062 formatted output, so that it will be printed
4063 by pp_output_formatted_text. */
4064
4065 static void
4066 append_formatted_chunk (pretty_printer *pp, const char *content)
4067 {
4068 output_buffer *buffer = pp_buffer (pp);
4069 struct chunk_info *chunk_array = buffer->cur_chunk_array;
4070 const char **args = chunk_array->args;
4071
4072 unsigned int chunk_idx;
4073 for (chunk_idx = 0; args[chunk_idx]; chunk_idx++)
4074 ;
4075 args[chunk_idx++] = content;
4076 args[chunk_idx] = NULL;
4077 }
4078
4079 /* Create a copy of CONTENT, with quotes added, and,
4080 potentially, with colorization.
4081 No escaped is performed on CONTENT.
4082 The result is in a GC-allocated buffer. */
4083
4084 static const char *
4085 add_quotes (const char *content, bool show_color)
4086 {
4087 pretty_printer tmp_pp;
4088 pp_show_color (&tmp_pp) = show_color;
4089
4090 /* We have to use "%<%s%>" rather than "%qs" here in order to avoid
4091 quoting colorization bytes within the results. */
4092 pp_printf (&tmp_pp, "%<%s%>", content);
4093
4094 return pp_ggc_formatted_text (&tmp_pp);
4095 }
4096
4097 /* If we had %H and %I, and hence deferred printing them,
4098 print them now, storing the result into the chunk_info
4099 for pp_format. Quote them if 'q' was provided.
4100 Also print the difference in tree form, adding it as
4101 an additional chunk. */
4102
4103 void
4104 cxx_format_postprocessor::handle (pretty_printer *pp)
4105 {
4106 /* If we have one of %H and %I, the other should have
4107 been present. */
4108 if (m_type_a.m_tree || m_type_b.m_tree)
4109 {
4110 /* Avoid reentrancy issues by working with a copy of
4111 m_type_a and m_type_b, resetting them now. */
4112 deferred_printed_type type_a = m_type_a;
4113 deferred_printed_type type_b = m_type_b;
4114 m_type_a = deferred_printed_type ();
4115 m_type_b = deferred_printed_type ();
4116
4117 gcc_assert (type_a.m_buffer_ptr);
4118 gcc_assert (type_b.m_buffer_ptr);
4119
4120 bool show_color = pp_show_color (pp);
4121
4122 const char *type_a_text;
4123 const char *type_b_text;
4124
4125 if (comparable_template_types_p (type_a.m_tree, type_b.m_tree))
4126 {
4127 type_a_text
4128 = type_to_string_with_compare (type_a.m_tree, type_b.m_tree,
4129 type_a.m_verbose, show_color);
4130 type_b_text
4131 = type_to_string_with_compare (type_b.m_tree, type_a.m_tree,
4132 type_b.m_verbose, show_color);
4133
4134 if (flag_diagnostics_show_template_tree)
4135 {
4136 pretty_printer inner_pp;
4137 pp_show_color (&inner_pp) = pp_show_color (pp);
4138 print_template_tree_comparison
4139 (&inner_pp, type_a.m_tree, type_b.m_tree, type_a.m_verbose, 2);
4140 append_formatted_chunk (pp, pp_ggc_formatted_text (&inner_pp));
4141 }
4142 }
4143 else
4144 {
4145 /* If the types were not comparable (or if only one of %H/%I was
4146 provided), they are printed normally, and no difference tree
4147 is printed. */
4148 type_a_text = type_to_string (type_a.m_tree, type_a.m_verbose,
4149 true, &type_a.m_quote, show_color);
4150 type_b_text = type_to_string (type_b.m_tree, type_b.m_verbose,
4151 true, &type_b.m_quote, show_color);
4152 }
4153
4154 if (type_a.m_quote)
4155 type_a_text = add_quotes (type_a_text, show_color);
4156 *type_a.m_buffer_ptr = type_a_text;
4157
4158 if (type_b.m_quote)
4159 type_b_text = add_quotes (type_b_text, show_color);
4160 *type_b.m_buffer_ptr = type_b_text;
4161 }
4162 }
4163
4164 /* Subroutine for handling %H and %I, to support i18n of messages like:
4165
4166 error_at (loc, "could not convert %qE from %qH to %qI",
4167 expr, type_a, type_b);
4168
4169 so that we can print things like:
4170
4171 could not convert 'foo' from 'map<int,double>' to 'map<int,int>'
4172
4173 and, with type-elision:
4174
4175 could not convert 'foo' from 'map<[...],double>' to 'map<[...],int>'
4176
4177 (with color-coding of the differences between the types).
4178
4179 The %H and %I format codes are peers: both must be present,
4180 and they affect each other. Hence to handle them, we must
4181 delay printing until we have both, deferring the printing to
4182 pretty_printer's m_format_postprocessor hook.
4183
4184 This is called in phase 2 of pp_format, when it is accumulating
4185 a series of formatted chunks. We stash the location of the chunk
4186 we're meant to have written to, so that we can write to it in the
4187 m_format_postprocessor hook.
4188
4189 We also need to stash whether a 'q' prefix was provided (the QUOTE
4190 param) so that we can add the quotes when writing out the delayed
4191 chunk. */
4192
4193 static void
4194 defer_phase_2_of_type_diff (deferred_printed_type *deferred,
4195 tree type, const char **buffer_ptr,
4196 bool verbose, bool quote)
4197 {
4198 gcc_assert (deferred->m_tree == NULL_TREE);
4199 gcc_assert (deferred->m_buffer_ptr == NULL);
4200 *deferred = deferred_printed_type (type, buffer_ptr, verbose, quote);
4201 }
4202
4203
4204 /* Called from output_format -- during diagnostic message processing --
4205 to handle C++ specific format specifier with the following meanings:
4206 %A function argument-list.
4207 %C tree code.
4208 %D declaration.
4209 %E expression.
4210 %F function declaration.
4211 %G gcall *
4212 %H type difference (from).
4213 %I type difference (to).
4214 %K tree
4215 %L language as used in extern "lang".
4216 %O binary operator.
4217 %P function parameter whose position is indicated by an integer.
4218 %Q assignment operator.
4219 %S substitution (template + args)
4220 %T type.
4221 %V cv-qualifier.
4222 %X exception-specification. */
4223 static bool
4224 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
4225 int precision, bool wide, bool set_locus, bool verbose,
4226 bool *quoted, const char **buffer_ptr)
4227 {
4228 gcc_assert (pp->m_format_postprocessor);
4229 cxx_format_postprocessor *postprocessor
4230 = static_cast <cxx_format_postprocessor *> (pp->m_format_postprocessor);
4231
4232 const char *result;
4233 tree t = NULL;
4234 #define next_tree (t = va_arg (*text->args_ptr, tree))
4235 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
4236 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
4237 #define next_int va_arg (*text->args_ptr, int)
4238
4239 if (precision != 0 || wide)
4240 return false;
4241
4242 switch (*spec)
4243 {
4244 case 'A': result = args_to_string (next_tree, verbose); break;
4245 case 'C': result = code_to_string (next_tcode); break;
4246 case 'D':
4247 {
4248 tree temp = next_tree;
4249 if (VAR_P (temp)
4250 && DECL_HAS_DEBUG_EXPR_P (temp))
4251 {
4252 temp = DECL_DEBUG_EXPR (temp);
4253 if (!DECL_P (temp))
4254 {
4255 result = expr_to_string (temp);
4256 break;
4257 }
4258 }
4259 result = decl_to_string (temp, verbose);
4260 }
4261 break;
4262 case 'E': result = expr_to_string (next_tree); break;
4263 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4264 case 'G':
4265 percent_G_format (text);
4266 return true;
4267 case 'H':
4268 defer_phase_2_of_type_diff (&postprocessor->m_type_a, next_tree,
4269 buffer_ptr, verbose, *quoted);
4270 return true;
4271 case 'I':
4272 defer_phase_2_of_type_diff (&postprocessor->m_type_b, next_tree,
4273 buffer_ptr, verbose, *quoted);
4274 return true;
4275 case 'K':
4276 t = va_arg (*text->args_ptr, tree);
4277 percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t));
4278 return true;
4279 case 'L': result = language_to_string (next_lang); break;
4280 case 'O': result = op_to_string (false, next_tcode); break;
4281 case 'P': result = parm_to_string (next_int); break;
4282 case 'Q': result = op_to_string (true, next_tcode); break;
4283 case 'S': result = subst_to_string (next_tree); break;
4284 case 'T':
4285 {
4286 result = type_to_string (next_tree, verbose, false, quoted,
4287 pp_show_color (pp));
4288 }
4289 break;
4290 case 'V': result = cv_to_string (next_tree, verbose); break;
4291 case 'X': result = eh_spec_to_string (next_tree, verbose); break;
4292
4293 default:
4294 return false;
4295 }
4296
4297 pp_string (pp, result);
4298 if (set_locus && t != NULL)
4299 text->set_location (0, location_of (t), SHOW_RANGE_WITH_CARET);
4300 return true;
4301 #undef next_tree
4302 #undef next_tcode
4303 #undef next_lang
4304 #undef next_int
4305 }
4306 \f
4307 /* Warn about the use of C++0x features when appropriate. */
4308 void
4309 maybe_warn_cpp0x (cpp0x_warn_str str)
4310 {
4311 if (cxx_dialect == cxx98)
4312 switch (str)
4313 {
4314 case CPP0X_INITIALIZER_LISTS:
4315 pedwarn (input_location, 0,
4316 "extended initializer lists "
4317 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4318 break;
4319 case CPP0X_EXPLICIT_CONVERSION:
4320 pedwarn (input_location, 0,
4321 "explicit conversion operators "
4322 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4323 break;
4324 case CPP0X_VARIADIC_TEMPLATES:
4325 pedwarn (input_location, 0,
4326 "variadic templates "
4327 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4328 break;
4329 case CPP0X_LAMBDA_EXPR:
4330 pedwarn (input_location, 0,
4331 "lambda expressions "
4332 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4333 break;
4334 case CPP0X_AUTO:
4335 pedwarn (input_location, 0,
4336 "C++11 auto only available with %<-std=c++11%> or "
4337 "%<-std=gnu++11%>");
4338 break;
4339 case CPP0X_SCOPED_ENUMS:
4340 pedwarn (input_location, 0,
4341 "scoped enums only available with %<-std=c++11%> or "
4342 "%<-std=gnu++11%>");
4343 break;
4344 case CPP0X_DEFAULTED_DELETED:
4345 pedwarn (input_location, 0,
4346 "defaulted and deleted functions "
4347 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4348 break;
4349 case CPP0X_INLINE_NAMESPACES:
4350 pedwarn (input_location, OPT_Wpedantic,
4351 "inline namespaces "
4352 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4353 break;
4354 case CPP0X_OVERRIDE_CONTROLS:
4355 pedwarn (input_location, 0,
4356 "override controls (override/final) "
4357 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4358 break;
4359 case CPP0X_NSDMI:
4360 pedwarn (input_location, 0,
4361 "non-static data member initializers "
4362 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4363 break;
4364 case CPP0X_USER_DEFINED_LITERALS:
4365 pedwarn (input_location, 0,
4366 "user-defined literals "
4367 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4368 break;
4369 case CPP0X_DELEGATING_CTORS:
4370 pedwarn (input_location, 0,
4371 "delegating constructors "
4372 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4373 break;
4374 case CPP0X_INHERITING_CTORS:
4375 pedwarn (input_location, 0,
4376 "inheriting constructors "
4377 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4378 break;
4379 case CPP0X_ATTRIBUTES:
4380 pedwarn (input_location, 0,
4381 "c++11 attributes "
4382 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4383 break;
4384 case CPP0X_REF_QUALIFIER:
4385 pedwarn (input_location, 0,
4386 "ref-qualifiers "
4387 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
4388 break;
4389 default:
4390 gcc_unreachable ();
4391 }
4392 }
4393
4394 /* Warn about the use of variadic templates when appropriate. */
4395 void
4396 maybe_warn_variadic_templates (void)
4397 {
4398 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
4399 }
4400
4401
4402 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
4403 option OPT with text GMSGID. Use this function to report
4404 diagnostics for constructs that are invalid C++98, but valid
4405 C++0x. */
4406 bool
4407 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
4408 {
4409 diagnostic_info diagnostic;
4410 va_list ap;
4411 bool ret;
4412 rich_location richloc (line_table, location);
4413
4414 va_start (ap, gmsgid);
4415 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
4416 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
4417 diagnostic.option_index = opt;
4418 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
4419 va_end (ap);
4420 return ret;
4421 }
4422
4423 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
4424 we found when we tried to do the lookup. LOCATION is the location of
4425 the NAME identifier. */
4426
4427 void
4428 qualified_name_lookup_error (tree scope, tree name,
4429 tree decl, location_t location)
4430 {
4431 if (scope == error_mark_node)
4432 ; /* We already complained. */
4433 else if (TYPE_P (scope))
4434 {
4435 if (!COMPLETE_TYPE_P (scope))
4436 error_at (location, "incomplete type %qT used in nested name specifier",
4437 scope);
4438 else if (TREE_CODE (decl) == TREE_LIST)
4439 {
4440 error_at (location, "reference to %<%T::%D%> is ambiguous",
4441 scope, name);
4442 print_candidates (decl);
4443 }
4444 else
4445 {
4446 name_hint hint;
4447 if (SCOPED_ENUM_P (scope) && TREE_CODE (name) == IDENTIFIER_NODE)
4448 hint = suggest_alternative_in_scoped_enum (name, scope);
4449 if (const char *suggestion = hint.suggestion ())
4450 {
4451 gcc_rich_location richloc (location);
4452 richloc.add_fixit_replace (suggestion);
4453 error_at (&richloc,
4454 "%qD is not a member of %qT; did you mean %qs?",
4455 name, scope, suggestion);
4456 }
4457 else
4458 error_at (location, "%qD is not a member of %qT", name, scope);
4459 }
4460 }
4461 else if (scope != global_namespace)
4462 {
4463 auto_diagnostic_group d;
4464 bool emit_fixit = true;
4465 name_hint hint
4466 = suggest_alternative_in_explicit_scope (location, name, scope);
4467 if (!hint)
4468 {
4469 hint = suggest_alternatives_in_other_namespaces (location, name);
4470 /* "location" is just the location of the name, not of the explicit
4471 scope, and it's not easy to get at the latter, so we can't issue
4472 fix-it hints for the suggestion. */
4473 emit_fixit = false;
4474 }
4475 if (const char *suggestion = hint.suggestion ())
4476 {
4477 gcc_rich_location richloc (location);
4478 if (emit_fixit)
4479 richloc.add_fixit_replace (suggestion);
4480 error_at (&richloc, "%qD is not a member of %qD; did you mean %qs?",
4481 name, scope, suggestion);
4482 }
4483 else
4484 error_at (location, "%qD is not a member of %qD", name, scope);
4485 }
4486 else
4487 {
4488 auto_diagnostic_group d;
4489 name_hint hint = suggest_alternatives_for (location, name, true);
4490 if (const char *suggestion = hint.suggestion ())
4491 {
4492 gcc_rich_location richloc (location);
4493 richloc.add_fixit_replace (suggestion);
4494 error_at (&richloc,
4495 "%<::%D%> has not been declared; did you mean %qs?",
4496 name, suggestion);
4497 }
4498 else
4499 error_at (location, "%<::%D%> has not been declared", name);
4500 }
4501 }
4502
4503 /* C++-specific implementation of range_label::get_text () vfunc for
4504 range_label_for_type_mismatch.
4505
4506 Compare with print_template_differences above. */
4507
4508 label_text
4509 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
4510 {
4511 if (m_labelled_type == NULL_TREE)
4512 return label_text::borrow (NULL);
4513
4514 const bool verbose = false;
4515 const bool show_color = false;
4516
4517 const char *result;
4518 if (m_other_type
4519 && comparable_template_types_p (m_labelled_type, m_other_type))
4520 result = type_to_string_with_compare (m_labelled_type, m_other_type,
4521 verbose, show_color);
4522 else
4523 result = type_to_string (m_labelled_type, verbose, true, NULL, show_color);
4524
4525 /* Both of the above return GC-allocated buffers, so the caller mustn't
4526 free them. */
4527 return label_text::borrow (result);
4528 }