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