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