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