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