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