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