]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/error.c
5741b1755da99176cfccbfff6779fe56efcacff5
[thirdparty/gcc.git] / gcc / cp / error.c
1 /* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "flags.h"
27 #include "diagnostic.h"
28 #include "tree-diagnostic.h"
29 #include "langhooks-def.h"
30 #include "intl.h"
31 #include "cxx-pretty-print.h"
32 #include "tree-pretty-print.h"
33 #include "pointer-set.h"
34 #include "c-family/c-objc.h"
35
36 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
37 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
38
39 /* The global buffer where we dump everything. It is there only for
40 transitional purpose. It is expected, in the near future, to be
41 completely removed. */
42 static cxx_pretty_printer scratch_pretty_printer;
43 #define cxx_pp (&scratch_pretty_printer)
44
45 /* Translate if being used for diagnostics, but not for dump files or
46 __PRETTY_FUNCTION. */
47 #define M_(msgid) (pp_translate_identifiers (cxx_pp) ? _(msgid) : (msgid))
48
49 # define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
50
51 static const char *args_to_string (tree, int);
52 static const char *assop_to_string (enum tree_code);
53 static const char *code_to_string (enum tree_code);
54 static const char *cv_to_string (tree, int);
55 static const char *decl_to_string (tree, int);
56 static const char *expr_to_string (tree);
57 static const char *fndecl_to_string (tree, int);
58 static const char *op_to_string (enum tree_code);
59 static const char *parm_to_string (int);
60 static const char *type_to_string (tree, int);
61
62 static void dump_alias_template_specialization (cxx_pretty_printer *, tree, int);
63 static void dump_type (cxx_pretty_printer *, tree, int);
64 static void dump_typename (cxx_pretty_printer *, tree, int);
65 static void dump_simple_decl (cxx_pretty_printer *, tree, tree, int);
66 static void dump_decl (cxx_pretty_printer *, tree, int);
67 static void dump_template_decl (cxx_pretty_printer *, tree, int);
68 static void dump_function_decl (cxx_pretty_printer *, tree, int);
69 static void dump_expr (cxx_pretty_printer *, tree, int);
70 static void dump_unary_op (cxx_pretty_printer *, const char *, tree, int);
71 static void dump_binary_op (cxx_pretty_printer *, const char *, tree, int);
72 static void dump_aggr_type (cxx_pretty_printer *, tree, int);
73 static void dump_type_prefix (cxx_pretty_printer *, tree, int);
74 static void dump_type_suffix (cxx_pretty_printer *, tree, int);
75 static void dump_function_name (cxx_pretty_printer *, tree, int);
76 static void dump_call_expr_args (cxx_pretty_printer *, tree, int, bool);
77 static void dump_aggr_init_expr_args (cxx_pretty_printer *, tree, int, bool);
78 static void dump_expr_list (cxx_pretty_printer *, tree, int);
79 static void dump_global_iord (cxx_pretty_printer *, tree);
80 static void dump_parameters (cxx_pretty_printer *, tree, int);
81 static void dump_ref_qualifier (cxx_pretty_printer *, tree, int);
82 static void dump_exception_spec (cxx_pretty_printer *, tree, int);
83 static void dump_template_argument (cxx_pretty_printer *, tree, int);
84 static void dump_template_argument_list (cxx_pretty_printer *, tree, int);
85 static void dump_template_parameter (cxx_pretty_printer *, tree, int);
86 static void dump_template_bindings (cxx_pretty_printer *, tree, tree,
87 vec<tree, va_gc> *);
88 static void dump_scope (cxx_pretty_printer *, tree, int);
89 static void dump_template_parms (cxx_pretty_printer *, tree, int, int);
90 static int get_non_default_template_args_count (tree, int);
91 static const char *function_category (tree);
92 static void maybe_print_constexpr_context (diagnostic_context *);
93 static void maybe_print_instantiation_context (diagnostic_context *);
94 static void print_instantiation_full_context (diagnostic_context *);
95 static void print_instantiation_partial_context (diagnostic_context *,
96 struct tinst_level *,
97 location_t);
98 static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
99 static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
100 static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
101
102 static bool cp_printer (pretty_printer *, text_info *, const char *,
103 int, bool, bool, bool);
104
105 void
106 init_error (void)
107 {
108 diagnostic_starter (global_dc) = cp_diagnostic_starter;
109 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
110 diagnostic_format_decoder (global_dc) = cp_printer;
111
112 pp_construct (cxx_pp, NULL, 0);
113 pp_cxx_pretty_printer_init (cxx_pp);
114 }
115
116 /* Dump a scope, if deemed necessary. */
117
118 static void
119 dump_scope (cxx_pretty_printer *pp, tree scope, int flags)
120 {
121 int f = flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF);
122
123 if (scope == NULL_TREE)
124 return;
125
126 if (TREE_CODE (scope) == NAMESPACE_DECL)
127 {
128 if (scope != global_namespace)
129 {
130 dump_decl (pp, scope, f);
131 pp_cxx_colon_colon (pp);
132 }
133 }
134 else if (AGGREGATE_TYPE_P (scope))
135 {
136 dump_type (pp, scope, f);
137 pp_cxx_colon_colon (pp);
138 }
139 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
140 {
141 dump_function_decl (pp, scope, f);
142 pp_cxx_colon_colon (pp);
143 }
144 }
145
146 /* Dump the template ARGument under control of FLAGS. */
147
148 static void
149 dump_template_argument (cxx_pretty_printer *pp, tree arg, int flags)
150 {
151 if (ARGUMENT_PACK_P (arg))
152 dump_template_argument_list (pp, ARGUMENT_PACK_ARGS (arg),
153 /* No default args in argument packs. */
154 flags|TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
155 else if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
156 dump_type (pp, arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
157 else
158 {
159 if (TREE_CODE (arg) == TREE_LIST)
160 arg = TREE_VALUE (arg);
161
162 dump_expr (pp, arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
163 }
164 }
165
166 /* Count the number of template arguments ARGS whose value does not
167 match the (optional) default template parameter in PARAMS */
168
169 static int
170 get_non_default_template_args_count (tree args, int flags)
171 {
172 int n = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_ARGS (args));
173
174 if (/* We use this flag when generating debug information. We don't
175 want to expand templates at this point, for this may generate
176 new decls, which gets decl counts out of sync, which may in
177 turn cause codegen differences between compilations with and
178 without -g. */
179 (flags & TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS) != 0
180 || !flag_pretty_templates)
181 return n;
182
183 return GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (INNERMOST_TEMPLATE_ARGS (args));
184 }
185
186 /* Dump a template-argument-list ARGS (always a TREE_VEC) under control
187 of FLAGS. */
188
189 static void
190 dump_template_argument_list (cxx_pretty_printer *pp, tree args, int flags)
191 {
192 int n = get_non_default_template_args_count (args, flags);
193 int need_comma = 0;
194 int i;
195
196 for (i = 0; i < n; ++i)
197 {
198 tree arg = TREE_VEC_ELT (args, i);
199
200 /* Only print a comma if we know there is an argument coming. In
201 the case of an empty template argument pack, no actual
202 argument will be printed. */
203 if (need_comma
204 && (!ARGUMENT_PACK_P (arg)
205 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
206 pp_separate_with_comma (pp);
207
208 dump_template_argument (pp, arg, flags);
209 need_comma = 1;
210 }
211 }
212
213 /* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
214
215 static void
216 dump_template_parameter (cxx_pretty_printer *pp, tree parm, int flags)
217 {
218 tree p;
219 tree a;
220
221 if (parm == error_mark_node)
222 return;
223
224 p = TREE_VALUE (parm);
225 a = TREE_PURPOSE (parm);
226
227 if (TREE_CODE (p) == TYPE_DECL)
228 {
229 if (flags & TFF_DECL_SPECIFIERS)
230 {
231 pp_cxx_ws_string (pp, "class");
232 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (p)))
233 pp_cxx_ws_string (pp, "...");
234 if (DECL_NAME (p))
235 pp_cxx_tree_identifier (pp, DECL_NAME (p));
236 }
237 else if (DECL_NAME (p))
238 pp_cxx_tree_identifier (pp, DECL_NAME (p));
239 else
240 pp_cxx_canonical_template_parameter (pp, TREE_TYPE (p));
241 }
242 else
243 dump_decl (pp, p, flags | TFF_DECL_SPECIFIERS);
244
245 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
246 {
247 pp_cxx_whitespace (pp);
248 pp_equal (pp);
249 pp_cxx_whitespace (pp);
250 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
251 dump_type (pp, a, flags & ~TFF_CHASE_TYPEDEF);
252 else
253 dump_expr (pp, a, flags | TFF_EXPR_IN_PARENS);
254 }
255 }
256
257 /* Dump, under control of FLAGS, a template-parameter-list binding.
258 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
259 TREE_VEC. */
260
261 static void
262 dump_template_bindings (cxx_pretty_printer *pp, tree parms, tree args,
263 vec<tree, va_gc> *typenames)
264 {
265 bool need_semicolon = false;
266 int i;
267 tree t;
268
269 while (parms)
270 {
271 tree p = TREE_VALUE (parms);
272 int lvl = TMPL_PARMS_DEPTH (parms);
273 int arg_idx = 0;
274 int i;
275 tree lvl_args = NULL_TREE;
276
277 /* Don't crash if we had an invalid argument list. */
278 if (TMPL_ARGS_DEPTH (args) >= lvl)
279 lvl_args = TMPL_ARGS_LEVEL (args, lvl);
280
281 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
282 {
283 tree arg = NULL_TREE;
284
285 /* Don't crash if we had an invalid argument list. */
286 if (lvl_args && NUM_TMPL_ARGS (lvl_args) > arg_idx)
287 arg = TREE_VEC_ELT (lvl_args, arg_idx);
288
289 if (need_semicolon)
290 pp_separate_with_semicolon (pp);
291 dump_template_parameter (pp, TREE_VEC_ELT (p, i),
292 TFF_PLAIN_IDENTIFIER);
293 pp_cxx_whitespace (pp);
294 pp_equal (pp);
295 pp_cxx_whitespace (pp);
296 if (arg)
297 {
298 if (ARGUMENT_PACK_P (arg))
299 pp_cxx_left_brace (pp);
300 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
301 if (ARGUMENT_PACK_P (arg))
302 pp_cxx_right_brace (pp);
303 }
304 else
305 pp_string (pp, M_("<missing>"));
306
307 ++arg_idx;
308 need_semicolon = true;
309 }
310
311 parms = TREE_CHAIN (parms);
312 }
313
314 /* Don't bother with typenames for a partial instantiation. */
315 if (vec_safe_is_empty (typenames) || uses_template_parms (args))
316 return;
317
318 FOR_EACH_VEC_SAFE_ELT (typenames, i, t)
319 {
320 if (need_semicolon)
321 pp_separate_with_semicolon (pp);
322 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
323 pp_cxx_whitespace (pp);
324 pp_equal (pp);
325 pp_cxx_whitespace (pp);
326 push_deferring_access_checks (dk_no_check);
327 t = tsubst (t, args, tf_none, NULL_TREE);
328 pop_deferring_access_checks ();
329 /* Strip typedefs. We can't just use TFF_CHASE_TYPEDEF because
330 pp_simple_type_specifier doesn't know about it. */
331 t = strip_typedefs (t);
332 dump_type (pp, t, TFF_PLAIN_IDENTIFIER);
333 }
334 }
335
336 /* Dump a human-readable equivalent of the alias template
337 specialization of T. */
338
339 static void
340 dump_alias_template_specialization (cxx_pretty_printer *pp, tree t, int flags)
341 {
342 tree name;
343
344 gcc_assert (alias_template_specialization_p (t));
345
346 if (!(flags & TFF_UNQUALIFIED_NAME))
347 dump_scope (pp, CP_DECL_CONTEXT (TYPE_NAME (t)), flags);
348 name = TYPE_IDENTIFIER (t);
349 pp_cxx_tree_identifier (pp, name);
350 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
351 /*primary=*/false,
352 flags & ~TFF_TEMPLATE_HEADER);
353 }
354
355 /* Dump a human-readable equivalent of TYPE. FLAGS controls the
356 format. */
357
358 static void
359 dump_type (cxx_pretty_printer *pp, tree t, int flags)
360 {
361 if (t == NULL_TREE)
362 return;
363
364 /* Don't print e.g. "struct mytypedef". */
365 if (TYPE_P (t) && typedef_variant_p (t))
366 {
367 tree decl = TYPE_NAME (t);
368 if ((flags & TFF_CHASE_TYPEDEF)
369 || DECL_SELF_REFERENCE_P (decl)
370 || (!flag_pretty_templates
371 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)))
372 t = strip_typedefs (t);
373 else if (alias_template_specialization_p (t))
374 {
375 dump_alias_template_specialization (pp, t, flags);
376 return;
377 }
378 else if (same_type_p (t, TREE_TYPE (decl)))
379 t = decl;
380 else
381 {
382 pp_cxx_cv_qualifier_seq (pp, t);
383 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
384 return;
385 }
386 }
387
388 if (TYPE_PTRMEMFUNC_P (t))
389 goto offset_type;
390
391 switch (TREE_CODE (t))
392 {
393 case LANG_TYPE:
394 if (t == init_list_type_node)
395 pp_string (pp, M_("<brace-enclosed initializer list>"));
396 else if (t == unknown_type_node)
397 pp_string (pp, M_("<unresolved overloaded function type>"));
398 else
399 {
400 pp_cxx_cv_qualifier_seq (pp, t);
401 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
402 }
403 break;
404
405 case TREE_LIST:
406 /* A list of function parms. */
407 dump_parameters (pp, t, flags);
408 break;
409
410 case IDENTIFIER_NODE:
411 pp_cxx_tree_identifier (pp, t);
412 break;
413
414 case TREE_BINFO:
415 dump_type (pp, BINFO_TYPE (t), flags);
416 break;
417
418 case RECORD_TYPE:
419 case UNION_TYPE:
420 case ENUMERAL_TYPE:
421 dump_aggr_type (pp, t, flags);
422 break;
423
424 case TYPE_DECL:
425 if (flags & TFF_CHASE_TYPEDEF)
426 {
427 dump_type (pp, DECL_ORIGINAL_TYPE (t)
428 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
429 break;
430 }
431 /* Else fall through. */
432
433 case TEMPLATE_DECL:
434 case NAMESPACE_DECL:
435 dump_decl (pp, t, flags & ~TFF_DECL_SPECIFIERS);
436 break;
437
438 case INTEGER_TYPE:
439 case REAL_TYPE:
440 case VOID_TYPE:
441 case BOOLEAN_TYPE:
442 case COMPLEX_TYPE:
443 case VECTOR_TYPE:
444 case FIXED_POINT_TYPE:
445 pp_type_specifier_seq (pp, t);
446 break;
447
448 case TEMPLATE_TEMPLATE_PARM:
449 /* For parameters inside template signature. */
450 if (TYPE_IDENTIFIER (t))
451 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
452 else
453 pp_cxx_canonical_template_parameter (pp, t);
454 break;
455
456 case BOUND_TEMPLATE_TEMPLATE_PARM:
457 {
458 tree args = TYPE_TI_ARGS (t);
459 pp_cxx_cv_qualifier_seq (pp, t);
460 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
461 pp_cxx_begin_template_argument_list (pp);
462 dump_template_argument_list (pp, args, flags);
463 pp_cxx_end_template_argument_list (pp);
464 }
465 break;
466
467 case TEMPLATE_TYPE_PARM:
468 pp_cxx_cv_qualifier_seq (pp, t);
469 if (TYPE_IDENTIFIER (t))
470 pp_cxx_tree_identifier (pp, TYPE_IDENTIFIER (t));
471 else
472 pp_cxx_canonical_template_parameter
473 (pp, TEMPLATE_TYPE_PARM_INDEX (t));
474 break;
475
476 /* This is not always necessary for pointers and such, but doing this
477 reduces code size. */
478 case ARRAY_TYPE:
479 case POINTER_TYPE:
480 case REFERENCE_TYPE:
481 case OFFSET_TYPE:
482 offset_type:
483 case FUNCTION_TYPE:
484 case METHOD_TYPE:
485 {
486 dump_type_prefix (pp, t, flags);
487 dump_type_suffix (pp, t, flags);
488 break;
489 }
490 case TYPENAME_TYPE:
491 if (! (flags & TFF_CHASE_TYPEDEF)
492 && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
493 {
494 dump_decl (pp, TYPE_NAME (t), TFF_PLAIN_IDENTIFIER);
495 break;
496 }
497 pp_cxx_cv_qualifier_seq (pp, t);
498 pp_cxx_ws_string (pp,
499 TYPENAME_IS_ENUM_P (t) ? "enum"
500 : TYPENAME_IS_CLASS_P (t) ? "class"
501 : "typename");
502 dump_typename (pp, t, flags);
503 break;
504
505 case UNBOUND_CLASS_TEMPLATE:
506 if (! (flags & TFF_UNQUALIFIED_NAME))
507 {
508 dump_type (pp, TYPE_CONTEXT (t), flags);
509 pp_cxx_colon_colon (pp);
510 }
511 pp_cxx_ws_string (pp, "template");
512 dump_type (pp, DECL_NAME (TYPE_NAME (t)), flags);
513 break;
514
515 case TYPEOF_TYPE:
516 pp_cxx_ws_string (pp, "__typeof__");
517 pp_cxx_whitespace (pp);
518 pp_cxx_left_paren (pp);
519 dump_expr (pp, TYPEOF_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
520 pp_cxx_right_paren (pp);
521 break;
522
523 case UNDERLYING_TYPE:
524 pp_cxx_ws_string (pp, "__underlying_type");
525 pp_cxx_whitespace (pp);
526 pp_cxx_left_paren (pp);
527 dump_expr (pp, UNDERLYING_TYPE_TYPE (t), flags & ~TFF_EXPR_IN_PARENS);
528 pp_cxx_right_paren (pp);
529 break;
530
531 case TYPE_PACK_EXPANSION:
532 dump_type (pp, PACK_EXPANSION_PATTERN (t), flags);
533 pp_cxx_ws_string (pp, "...");
534 break;
535
536 case TYPE_ARGUMENT_PACK:
537 dump_template_argument (pp, t, flags);
538 break;
539
540 case DECLTYPE_TYPE:
541 pp_cxx_ws_string (pp, "decltype");
542 pp_cxx_whitespace (pp);
543 pp_cxx_left_paren (pp);
544 dump_expr (pp, DECLTYPE_TYPE_EXPR (t), flags & ~TFF_EXPR_IN_PARENS);
545 pp_cxx_right_paren (pp);
546 break;
547
548 case NULLPTR_TYPE:
549 pp_string (pp, "std::nullptr_t");
550 break;
551
552 default:
553 pp_unsupported_tree (pp, t);
554 /* Fall through to error. */
555
556 case ERROR_MARK:
557 pp_string (pp, M_("<type error>"));
558 break;
559 }
560 }
561
562 /* Dump a TYPENAME_TYPE. We need to notice when the context is itself
563 a TYPENAME_TYPE. */
564
565 static void
566 dump_typename (cxx_pretty_printer *pp, tree t, int flags)
567 {
568 tree ctx = TYPE_CONTEXT (t);
569
570 if (TREE_CODE (ctx) == TYPENAME_TYPE)
571 dump_typename (pp, ctx, flags);
572 else
573 dump_type (pp, ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
574 pp_cxx_colon_colon (pp);
575 dump_decl (pp, TYPENAME_TYPE_FULLNAME (t), flags);
576 }
577
578 /* Return the name of the supplied aggregate, or enumeral type. */
579
580 const char *
581 class_key_or_enum_as_string (tree t)
582 {
583 if (TREE_CODE (t) == ENUMERAL_TYPE)
584 {
585 if (SCOPED_ENUM_P (t))
586 return "enum class";
587 else
588 return "enum";
589 }
590 else if (TREE_CODE (t) == UNION_TYPE)
591 return "union";
592 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
593 return "class";
594 else
595 return "struct";
596 }
597
598 /* Print out a class declaration T under the control of FLAGS,
599 in the form `class foo'. */
600
601 static void
602 dump_aggr_type (cxx_pretty_printer *pp, tree t, int flags)
603 {
604 tree name;
605 const char *variety = class_key_or_enum_as_string (t);
606 int typdef = 0;
607 int tmplate = 0;
608
609 pp_cxx_cv_qualifier_seq (pp, t);
610
611 if (flags & TFF_CLASS_KEY_OR_ENUM)
612 pp_cxx_ws_string (pp, variety);
613
614 name = TYPE_NAME (t);
615
616 if (name)
617 {
618 typdef = (!DECL_ARTIFICIAL (name)
619 /* An alias specialization is not considered to be a
620 typedef. */
621 && !alias_template_specialization_p (t));
622
623 if ((typdef
624 && ((flags & TFF_CHASE_TYPEDEF)
625 || (!flag_pretty_templates && DECL_LANG_SPECIFIC (name)
626 && DECL_TEMPLATE_INFO (name))))
627 || DECL_SELF_REFERENCE_P (name))
628 {
629 t = TYPE_MAIN_VARIANT (t);
630 name = TYPE_NAME (t);
631 typdef = 0;
632 }
633
634 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
635 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
636 && (TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
637 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
638
639 if (! (flags & TFF_UNQUALIFIED_NAME))
640 dump_scope (pp, CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
641 flags &= ~TFF_UNQUALIFIED_NAME;
642 if (tmplate)
643 {
644 /* Because the template names are mangled, we have to locate
645 the most general template, and use that name. */
646 tree tpl = TYPE_TI_TEMPLATE (t);
647
648 while (DECL_TEMPLATE_INFO (tpl))
649 tpl = DECL_TI_TEMPLATE (tpl);
650 name = tpl;
651 }
652 name = DECL_NAME (name);
653 }
654
655 if (name == 0 || ANON_AGGRNAME_P (name))
656 {
657 if (flags & TFF_CLASS_KEY_OR_ENUM)
658 pp_string (pp, M_("<anonymous>"));
659 else
660 pp_printf (pp, M_("<anonymous %s>"), variety);
661 }
662 else if (LAMBDA_TYPE_P (t))
663 {
664 /* A lambda's "type" is essentially its signature. */
665 pp_string (pp, M_("<lambda"));
666 if (lambda_function (t))
667 dump_parameters (pp,
668 FUNCTION_FIRST_USER_PARMTYPE (lambda_function (t)),
669 flags);
670 pp_greater (pp);
671 }
672 else
673 pp_cxx_tree_identifier (pp, name);
674 if (tmplate)
675 dump_template_parms (pp, TYPE_TEMPLATE_INFO (t),
676 !CLASSTYPE_USE_TEMPLATE (t),
677 flags & ~TFF_TEMPLATE_HEADER);
678 }
679
680 /* Dump into the obstack the initial part of the output for a given type.
681 This is necessary when dealing with things like functions returning
682 functions. Examples:
683
684 return type of `int (* fee ())()': pointer -> function -> int. Both
685 pointer (and reference and offset) and function (and member) types must
686 deal with prefix and suffix.
687
688 Arrays must also do this for DECL nodes, like int a[], and for things like
689 int *[]&. */
690
691 static void
692 dump_type_prefix (cxx_pretty_printer *pp, tree t, int flags)
693 {
694 if (TYPE_PTRMEMFUNC_P (t))
695 {
696 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
697 goto offset_type;
698 }
699
700 switch (TREE_CODE (t))
701 {
702 case POINTER_TYPE:
703 case REFERENCE_TYPE:
704 {
705 tree sub = TREE_TYPE (t);
706
707 dump_type_prefix (pp, sub, flags);
708 if (TREE_CODE (sub) == ARRAY_TYPE
709 || TREE_CODE (sub) == FUNCTION_TYPE)
710 {
711 pp_cxx_whitespace (pp);
712 pp_cxx_left_paren (pp);
713 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (sub));
714 }
715 if (TYPE_PTR_P (t))
716 pp_star (pp);
717 else if (TREE_CODE (t) == REFERENCE_TYPE)
718 {
719 if (TYPE_REF_IS_RVALUE (t))
720 pp_ampersand_ampersand (pp);
721 else
722 pp_ampersand (pp);
723 }
724 pp->padding = pp_before;
725 pp_cxx_cv_qualifier_seq (pp, t);
726 }
727 break;
728
729 case OFFSET_TYPE:
730 offset_type:
731 dump_type_prefix (pp, TREE_TYPE (t), flags);
732 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
733 {
734 pp_maybe_space (pp);
735 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
736 pp_cxx_left_paren (pp);
737 dump_type (pp, TYPE_OFFSET_BASETYPE (t), flags);
738 pp_cxx_colon_colon (pp);
739 }
740 pp_cxx_star (pp);
741 pp_cxx_cv_qualifier_seq (pp, t);
742 pp->padding = pp_before;
743 break;
744
745 /* This can be reached without a pointer when dealing with
746 templates, e.g. std::is_function. */
747 case FUNCTION_TYPE:
748 dump_type_prefix (pp, TREE_TYPE (t), flags);
749 break;
750
751 case METHOD_TYPE:
752 dump_type_prefix (pp, TREE_TYPE (t), flags);
753 pp_maybe_space (pp);
754 pp_cxx_left_paren (pp);
755 dump_aggr_type (pp, TYPE_METHOD_BASETYPE (t), flags);
756 pp_cxx_colon_colon (pp);
757 break;
758
759 case ARRAY_TYPE:
760 dump_type_prefix (pp, TREE_TYPE (t), flags);
761 break;
762
763 case ENUMERAL_TYPE:
764 case IDENTIFIER_NODE:
765 case INTEGER_TYPE:
766 case BOOLEAN_TYPE:
767 case REAL_TYPE:
768 case RECORD_TYPE:
769 case TEMPLATE_TYPE_PARM:
770 case TEMPLATE_TEMPLATE_PARM:
771 case BOUND_TEMPLATE_TEMPLATE_PARM:
772 case TREE_LIST:
773 case TYPE_DECL:
774 case TREE_VEC:
775 case UNION_TYPE:
776 case LANG_TYPE:
777 case VOID_TYPE:
778 case TYPENAME_TYPE:
779 case COMPLEX_TYPE:
780 case VECTOR_TYPE:
781 case TYPEOF_TYPE:
782 case UNDERLYING_TYPE:
783 case DECLTYPE_TYPE:
784 case TYPE_PACK_EXPANSION:
785 case FIXED_POINT_TYPE:
786 case NULLPTR_TYPE:
787 dump_type (pp, t, flags);
788 pp->padding = pp_before;
789 break;
790
791 default:
792 pp_unsupported_tree (pp, t);
793 /* fall through. */
794 case ERROR_MARK:
795 pp_string (pp, M_("<typeprefixerror>"));
796 break;
797 }
798 }
799
800 /* Dump the suffix of type T, under control of FLAGS. This is the part
801 which appears after the identifier (or function parms). */
802
803 static void
804 dump_type_suffix (cxx_pretty_printer *pp, tree t, int flags)
805 {
806 if (TYPE_PTRMEMFUNC_P (t))
807 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
808
809 switch (TREE_CODE (t))
810 {
811 case POINTER_TYPE:
812 case REFERENCE_TYPE:
813 case OFFSET_TYPE:
814 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
815 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
816 pp_cxx_right_paren (pp);
817 dump_type_suffix (pp, TREE_TYPE (t), flags);
818 break;
819
820 case FUNCTION_TYPE:
821 case METHOD_TYPE:
822 {
823 tree arg;
824 if (TREE_CODE (t) == METHOD_TYPE)
825 /* Can only be reached through a pointer. */
826 pp_cxx_right_paren (pp);
827 arg = TYPE_ARG_TYPES (t);
828 if (TREE_CODE (t) == METHOD_TYPE)
829 arg = TREE_CHAIN (arg);
830
831 /* Function pointers don't have default args. Not in standard C++,
832 anyway; they may in g++, but we'll just pretend otherwise. */
833 dump_parameters (pp, arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
834
835 pp->padding = pp_before;
836 pp_cxx_cv_qualifiers (pp, type_memfn_quals (t));
837 dump_ref_qualifier (pp, t, flags);
838 dump_exception_spec (pp, TYPE_RAISES_EXCEPTIONS (t), flags);
839 dump_type_suffix (pp, TREE_TYPE (t), flags);
840 break;
841 }
842
843 case ARRAY_TYPE:
844 pp_maybe_space (pp);
845 pp_cxx_left_bracket (pp);
846 if (TYPE_DOMAIN (t))
847 {
848 tree dtype = TYPE_DOMAIN (t);
849 tree max = TYPE_MAX_VALUE (dtype);
850 if (integer_all_onesp (max))
851 pp_character (pp, '0');
852 else if (host_integerp (max, 0))
853 pp_wide_integer (pp, tree_low_cst (max, 0) + 1);
854 else
855 {
856 STRIP_NOPS (max);
857 if (TREE_CODE (max) == SAVE_EXPR)
858 max = TREE_OPERAND (max, 0);
859 if (TREE_CODE (max) == MINUS_EXPR
860 || TREE_CODE (max) == PLUS_EXPR)
861 {
862 max = TREE_OPERAND (max, 0);
863 while (CONVERT_EXPR_P (max))
864 max = TREE_OPERAND (max, 0);
865 }
866 else
867 max = fold_build2_loc (input_location,
868 PLUS_EXPR, dtype, max,
869 build_int_cst (dtype, 1));
870 dump_expr (pp, max, flags & ~TFF_EXPR_IN_PARENS);
871 }
872 }
873 pp_cxx_right_bracket (pp);
874 dump_type_suffix (pp, TREE_TYPE (t), flags);
875 break;
876
877 case ENUMERAL_TYPE:
878 case IDENTIFIER_NODE:
879 case INTEGER_TYPE:
880 case BOOLEAN_TYPE:
881 case REAL_TYPE:
882 case RECORD_TYPE:
883 case TEMPLATE_TYPE_PARM:
884 case TEMPLATE_TEMPLATE_PARM:
885 case BOUND_TEMPLATE_TEMPLATE_PARM:
886 case TREE_LIST:
887 case TYPE_DECL:
888 case TREE_VEC:
889 case UNION_TYPE:
890 case LANG_TYPE:
891 case VOID_TYPE:
892 case TYPENAME_TYPE:
893 case COMPLEX_TYPE:
894 case VECTOR_TYPE:
895 case TYPEOF_TYPE:
896 case UNDERLYING_TYPE:
897 case DECLTYPE_TYPE:
898 case TYPE_PACK_EXPANSION:
899 case FIXED_POINT_TYPE:
900 case NULLPTR_TYPE:
901 break;
902
903 default:
904 pp_unsupported_tree (pp, t);
905 case ERROR_MARK:
906 /* Don't mark it here, we should have already done in
907 dump_type_prefix. */
908 break;
909 }
910 }
911
912 static void
913 dump_global_iord (cxx_pretty_printer *pp, tree t)
914 {
915 const char *p = NULL;
916
917 if (DECL_GLOBAL_CTOR_P (t))
918 p = M_("(static initializers for %s)");
919 else if (DECL_GLOBAL_DTOR_P (t))
920 p = M_("(static destructors for %s)");
921 else
922 gcc_unreachable ();
923
924 pp_printf (pp, p, input_filename);
925 }
926
927 static void
928 dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags)
929 {
930 if (flags & TFF_DECL_SPECIFIERS)
931 {
932 if (VAR_P (t)
933 && DECL_DECLARED_CONSTEXPR_P (t))
934 pp_cxx_ws_string (pp, "constexpr");
935 dump_type_prefix (pp, type, flags & ~TFF_UNQUALIFIED_NAME);
936 pp_maybe_space (pp);
937 }
938 if (! (flags & TFF_UNQUALIFIED_NAME)
939 && TREE_CODE (t) != PARM_DECL
940 && (!DECL_INITIAL (t)
941 || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX))
942 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
943 flags &= ~TFF_UNQUALIFIED_NAME;
944 if ((flags & TFF_DECL_SPECIFIERS)
945 && DECL_TEMPLATE_PARM_P (t)
946 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (t)))
947 pp_string (pp, "...");
948 if (DECL_NAME (t))
949 {
950 if (TREE_CODE (t) == FIELD_DECL && DECL_NORMAL_CAPTURE_P (t))
951 {
952 pp_less (pp);
953 pp_string (pp, IDENTIFIER_POINTER (DECL_NAME (t)) + 2);
954 pp_string (pp, " capture>");
955 }
956 else
957 dump_decl (pp, DECL_NAME (t), flags);
958 }
959 else
960 pp_string (pp, M_("<anonymous>"));
961 if (flags & TFF_DECL_SPECIFIERS)
962 dump_type_suffix (pp, type, flags);
963 }
964
965 /* Dump a human readable string for the decl T under control of FLAGS. */
966
967 static void
968 dump_decl (cxx_pretty_printer *pp, tree t, int flags)
969 {
970 if (t == NULL_TREE)
971 return;
972
973 /* If doing Objective-C++, give Objective-C a chance to demangle
974 Objective-C method names. */
975 if (c_dialect_objc ())
976 {
977 const char *demangled = objc_maybe_printable_name (t, flags);
978 if (demangled)
979 {
980 pp_string (pp, demangled);
981 return;
982 }
983 }
984
985 switch (TREE_CODE (t))
986 {
987 case TYPE_DECL:
988 /* Don't say 'typedef class A' */
989 if (DECL_ARTIFICIAL (t) && !DECL_SELF_REFERENCE_P (t))
990 {
991 if ((flags & TFF_DECL_SPECIFIERS)
992 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
993 {
994 /* Say `class T' not just `T'. */
995 pp_cxx_ws_string (pp, "class");
996
997 /* Emit the `...' for a parameter pack. */
998 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
999 pp_cxx_ws_string (pp, "...");
1000 }
1001
1002 dump_type (pp, TREE_TYPE (t), flags);
1003 break;
1004 }
1005 if (TYPE_DECL_ALIAS_P (t)
1006 && (flags & TFF_DECL_SPECIFIERS
1007 || flags & TFF_CLASS_KEY_OR_ENUM))
1008 {
1009 pp_cxx_ws_string (pp, "using");
1010 dump_decl (pp, DECL_NAME (t), flags);
1011 pp_cxx_whitespace (pp);
1012 pp_cxx_ws_string (pp, "=");
1013 pp_cxx_whitespace (pp);
1014 dump_type (pp, DECL_ORIGINAL_TYPE (t), flags);
1015 break;
1016 }
1017 if ((flags & TFF_DECL_SPECIFIERS)
1018 && !DECL_SELF_REFERENCE_P (t))
1019 pp_cxx_ws_string (pp, "typedef");
1020 dump_simple_decl (pp, t, DECL_ORIGINAL_TYPE (t)
1021 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
1022 flags);
1023 break;
1024
1025 case VAR_DECL:
1026 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
1027 {
1028 pp_string (pp, M_("vtable for "));
1029 gcc_assert (TYPE_P (DECL_CONTEXT (t)));
1030 dump_type (pp, DECL_CONTEXT (t), flags);
1031 break;
1032 }
1033 /* Else fall through. */
1034 case FIELD_DECL:
1035 case PARM_DECL:
1036 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1037 break;
1038
1039 case RESULT_DECL:
1040 pp_string (pp, M_("<return value> "));
1041 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1042 break;
1043
1044 case NAMESPACE_DECL:
1045 if (flags & TFF_DECL_SPECIFIERS)
1046 pp_cxx_declaration (pp, t);
1047 else
1048 {
1049 if (! (flags & TFF_UNQUALIFIED_NAME))
1050 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1051 flags &= ~TFF_UNQUALIFIED_NAME;
1052 if (DECL_NAME (t) == NULL_TREE)
1053 {
1054 if (!(pp->flags & pp_c_flag_gnu_v3))
1055 pp_cxx_ws_string (pp, M_("{anonymous}"));
1056 else
1057 pp_cxx_ws_string (pp, M_("(anonymous namespace)"));
1058 }
1059 else
1060 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1061 }
1062 break;
1063
1064 case SCOPE_REF:
1065 dump_type (pp, TREE_OPERAND (t, 0), flags);
1066 pp_colon_colon (pp);
1067 dump_decl (pp, TREE_OPERAND (t, 1), TFF_UNQUALIFIED_NAME);
1068 break;
1069
1070 case ARRAY_REF:
1071 dump_decl (pp, TREE_OPERAND (t, 0), flags);
1072 pp_cxx_left_bracket (pp);
1073 dump_decl (pp, TREE_OPERAND (t, 1), flags);
1074 pp_cxx_right_bracket (pp);
1075 break;
1076
1077 case ARRAY_NOTATION_REF:
1078 dump_decl (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
1079 pp_cxx_left_bracket (pp);
1080 dump_decl (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
1081 pp_colon (pp);
1082 dump_decl (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
1083 pp_colon (pp);
1084 dump_decl (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
1085 pp_cxx_right_bracket (pp);
1086 break;
1087
1088 /* So that we can do dump_decl on an aggr type. */
1089 case RECORD_TYPE:
1090 case UNION_TYPE:
1091 case ENUMERAL_TYPE:
1092 dump_type (pp, t, flags);
1093 break;
1094
1095 case BIT_NOT_EXPR:
1096 /* This is a pseudo destructor call which has not been folded into
1097 a PSEUDO_DTOR_EXPR yet. */
1098 pp_cxx_complement (pp);
1099 dump_type (pp, TREE_OPERAND (t, 0), flags);
1100 break;
1101
1102 case TYPE_EXPR:
1103 gcc_unreachable ();
1104 break;
1105
1106 /* These special cases are duplicated here so that other functions
1107 can feed identifiers to error and get them demangled properly. */
1108 case IDENTIFIER_NODE:
1109 if (IDENTIFIER_TYPENAME_P (t))
1110 {
1111 pp_cxx_ws_string (pp, "operator");
1112 /* Not exactly IDENTIFIER_TYPE_VALUE. */
1113 dump_type (pp, TREE_TYPE (t), flags);
1114 break;
1115 }
1116 else
1117 pp_cxx_tree_identifier (pp, t);
1118 break;
1119
1120 case OVERLOAD:
1121 if (OVL_CHAIN (t))
1122 {
1123 t = OVL_CURRENT (t);
1124 if (DECL_CLASS_SCOPE_P (t))
1125 {
1126 dump_type (pp, DECL_CONTEXT (t), flags);
1127 pp_cxx_colon_colon (pp);
1128 }
1129 else if (!DECL_FILE_SCOPE_P (t))
1130 {
1131 dump_decl (pp, DECL_CONTEXT (t), flags);
1132 pp_cxx_colon_colon (pp);
1133 }
1134 dump_decl (pp, DECL_NAME (t), flags);
1135 break;
1136 }
1137
1138 /* If there's only one function, just treat it like an ordinary
1139 FUNCTION_DECL. */
1140 t = OVL_CURRENT (t);
1141 /* Fall through. */
1142
1143 case FUNCTION_DECL:
1144 if (! DECL_LANG_SPECIFIC (t))
1145 pp_string (pp, M_("<built-in>"));
1146 else if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
1147 dump_global_iord (pp, t);
1148 else
1149 dump_function_decl (pp, t, flags);
1150 break;
1151
1152 case TEMPLATE_DECL:
1153 dump_template_decl (pp, t, flags);
1154 break;
1155
1156 case TEMPLATE_ID_EXPR:
1157 {
1158 tree name = TREE_OPERAND (t, 0);
1159 tree args = TREE_OPERAND (t, 1);
1160
1161 if (is_overloaded_fn (name))
1162 name = DECL_NAME (get_first_fn (name));
1163 dump_decl (pp, name, flags);
1164 pp_cxx_begin_template_argument_list (pp);
1165 if (args == error_mark_node)
1166 pp_string (pp, M_("<template arguments error>"));
1167 else if (args)
1168 dump_template_argument_list (pp, args, flags);
1169 pp_cxx_end_template_argument_list (pp);
1170 }
1171 break;
1172
1173 case LABEL_DECL:
1174 pp_cxx_tree_identifier (pp, DECL_NAME (t));
1175 break;
1176
1177 case CONST_DECL:
1178 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
1179 || (DECL_INITIAL (t) &&
1180 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
1181 dump_simple_decl (pp, t, TREE_TYPE (t), flags);
1182 else if (DECL_NAME (t))
1183 dump_decl (pp, DECL_NAME (t), flags);
1184 else if (DECL_INITIAL (t))
1185 dump_expr (pp, DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
1186 else
1187 pp_string (pp, M_("<enumerator>"));
1188 break;
1189
1190 case USING_DECL:
1191 pp_cxx_ws_string (pp, "using");
1192 dump_type (pp, USING_DECL_SCOPE (t), flags);
1193 pp_cxx_colon_colon (pp);
1194 dump_decl (pp, DECL_NAME (t), flags);
1195 break;
1196
1197 case STATIC_ASSERT:
1198 pp_cxx_declaration (pp, t);
1199 break;
1200
1201 case BASELINK:
1202 dump_decl (pp, BASELINK_FUNCTIONS (t), flags);
1203 break;
1204
1205 case NON_DEPENDENT_EXPR:
1206 dump_expr (pp, t, flags);
1207 break;
1208
1209 case TEMPLATE_TYPE_PARM:
1210 if (flags & TFF_DECL_SPECIFIERS)
1211 pp_cxx_declaration (pp, t);
1212 else
1213 pp_type_id (pp, t);
1214 break;
1215
1216 case UNBOUND_CLASS_TEMPLATE:
1217 case TYPE_PACK_EXPANSION:
1218 case TREE_BINFO:
1219 dump_type (pp, t, flags);
1220 break;
1221
1222 default:
1223 pp_unsupported_tree (pp, t);
1224 /* Fall through to error. */
1225
1226 case ERROR_MARK:
1227 pp_string (pp, M_("<declaration error>"));
1228 break;
1229 }
1230 }
1231
1232 /* Dump a template declaration T under control of FLAGS. This means the
1233 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
1234
1235 static void
1236 dump_template_decl (cxx_pretty_printer *pp, tree t, int flags)
1237 {
1238 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1239 tree parms;
1240 int i;
1241
1242 if (flags & TFF_TEMPLATE_HEADER)
1243 {
1244 for (parms = orig_parms = nreverse (orig_parms);
1245 parms;
1246 parms = TREE_CHAIN (parms))
1247 {
1248 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1249 int len = TREE_VEC_LENGTH (inner_parms);
1250
1251 pp_cxx_ws_string (pp, "template");
1252 pp_cxx_begin_template_argument_list (pp);
1253
1254 /* If we've shown the template prefix, we'd better show the
1255 parameters' and decl's type too. */
1256 flags |= TFF_DECL_SPECIFIERS;
1257
1258 for (i = 0; i < len; i++)
1259 {
1260 if (i)
1261 pp_separate_with_comma (pp);
1262 dump_template_parameter (pp, TREE_VEC_ELT (inner_parms, i),
1263 flags);
1264 }
1265 pp_cxx_end_template_argument_list (pp);
1266 pp_cxx_whitespace (pp);
1267 }
1268 nreverse(orig_parms);
1269
1270 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
1271 {
1272 /* Say `template<arg> class TT' not just `template<arg> TT'. */
1273 pp_cxx_ws_string (pp, "class");
1274
1275 /* If this is a parameter pack, print the ellipsis. */
1276 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
1277 pp_cxx_ws_string (pp, "...");
1278 }
1279 }
1280
1281 if (DECL_CLASS_TEMPLATE_P (t))
1282 dump_type (pp, TREE_TYPE (t),
1283 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1284 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
1285 else if (DECL_TEMPLATE_RESULT (t)
1286 && (VAR_P (DECL_TEMPLATE_RESULT (t))
1287 /* Alias template. */
1288 || DECL_TYPE_TEMPLATE_P (t)))
1289 dump_decl (pp, DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
1290 else
1291 {
1292 gcc_assert (TREE_TYPE (t));
1293 switch (NEXT_CODE (t))
1294 {
1295 case METHOD_TYPE:
1296 case FUNCTION_TYPE:
1297 dump_function_decl (pp, t, flags | TFF_TEMPLATE_NAME);
1298 break;
1299 default:
1300 /* This case can occur with some invalid code. */
1301 dump_type (pp, TREE_TYPE (t),
1302 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1303 | (flags & TFF_DECL_SPECIFIERS
1304 ? TFF_CLASS_KEY_OR_ENUM : 0));
1305 }
1306 }
1307 }
1308
1309 /* find_typenames looks through the type of the function template T
1310 and returns a vec containing any typedefs, decltypes or TYPENAME_TYPEs
1311 it finds. */
1312
1313 struct find_typenames_t
1314 {
1315 struct pointer_set_t *p_set;
1316 vec<tree, va_gc> *typenames;
1317 };
1318
1319 static tree
1320 find_typenames_r (tree *tp, int *walk_subtrees, void *data)
1321 {
1322 struct find_typenames_t *d = (struct find_typenames_t *)data;
1323 tree mv = NULL_TREE;
1324
1325 if (TYPE_P (*tp) && is_typedef_decl (TYPE_NAME (*tp)))
1326 /* Add the type of the typedef without any additional cv-quals. */
1327 mv = TREE_TYPE (TYPE_NAME (*tp));
1328 else if (TREE_CODE (*tp) == TYPENAME_TYPE
1329 || TREE_CODE (*tp) == DECLTYPE_TYPE)
1330 /* Add the typename without any cv-qualifiers. */
1331 mv = TYPE_MAIN_VARIANT (*tp);
1332
1333 if (TREE_CODE (*tp) == TYPE_PACK_EXPANSION)
1334 {
1335 /* Don't mess with parameter packs since we don't remember
1336 the pack expansion context for a particular typename. */
1337 *walk_subtrees = false;
1338 return NULL_TREE;
1339 }
1340
1341 if (mv && (mv == *tp || !pointer_set_insert (d->p_set, mv)))
1342 vec_safe_push (d->typenames, mv);
1343
1344 /* Search into class template arguments, which cp_walk_subtrees
1345 doesn't do. */
1346 if (CLASS_TYPE_P (*tp) && CLASSTYPE_TEMPLATE_INFO (*tp))
1347 cp_walk_tree (&CLASSTYPE_TI_ARGS (*tp), find_typenames_r,
1348 data, d->p_set);
1349
1350 return NULL_TREE;
1351 }
1352
1353 static vec<tree, va_gc> *
1354 find_typenames (tree t)
1355 {
1356 struct find_typenames_t ft;
1357 ft.p_set = pointer_set_create ();
1358 ft.typenames = NULL;
1359 cp_walk_tree (&TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
1360 find_typenames_r, &ft, ft.p_set);
1361 pointer_set_destroy (ft.p_set);
1362 return ft.typenames;
1363 }
1364
1365 /* Pretty print a function decl. There are several ways we want to print a
1366 function declaration. The TFF_ bits in FLAGS tells us how to behave.
1367 As error can only apply the '#' flag once to give 0 and 1 for V, there
1368 is %D which doesn't print the throw specs, and %F which does. */
1369
1370 static void
1371 dump_function_decl (cxx_pretty_printer *pp, tree t, int flags)
1372 {
1373 tree fntype;
1374 tree parmtypes;
1375 tree cname = NULL_TREE;
1376 tree template_args = NULL_TREE;
1377 tree template_parms = NULL_TREE;
1378 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
1379 int do_outer_scope = ! (flags & TFF_UNQUALIFIED_NAME);
1380 tree exceptions;
1381 vec<tree, va_gc> *typenames = NULL;
1382
1383 if (DECL_NAME (t) && LAMBDA_FUNCTION_P (t))
1384 {
1385 /* A lambda's signature is essentially its "type", so defer. */
1386 gcc_assert (LAMBDA_TYPE_P (DECL_CONTEXT (t)));
1387 dump_type (pp, DECL_CONTEXT (t), flags);
1388 return;
1389 }
1390
1391 flags &= ~(TFF_UNQUALIFIED_NAME | TFF_TEMPLATE_NAME);
1392 if (TREE_CODE (t) == TEMPLATE_DECL)
1393 t = DECL_TEMPLATE_RESULT (t);
1394
1395 /* Save the exceptions, in case t is a specialization and we are
1396 emitting an error about incompatible specifications. */
1397 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
1398
1399 /* Pretty print template instantiations only. */
1400 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t)
1401 && flag_pretty_templates)
1402 {
1403 tree tmpl;
1404
1405 template_args = DECL_TI_ARGS (t);
1406 tmpl = most_general_template (t);
1407 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1408 {
1409 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1410 t = tmpl;
1411 typenames = find_typenames (t);
1412 }
1413 }
1414
1415 fntype = TREE_TYPE (t);
1416 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
1417
1418 if (DECL_CLASS_SCOPE_P (t))
1419 cname = DECL_CONTEXT (t);
1420 /* This is for partially instantiated template methods. */
1421 else if (TREE_CODE (fntype) == METHOD_TYPE)
1422 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1423
1424 if (flags & TFF_DECL_SPECIFIERS)
1425 {
1426 if (DECL_STATIC_FUNCTION_P (t))
1427 pp_cxx_ws_string (pp, "static");
1428 else if (DECL_VIRTUAL_P (t))
1429 pp_cxx_ws_string (pp, "virtual");
1430
1431 if (DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (t)))
1432 pp_cxx_ws_string (pp, "constexpr");
1433 }
1434
1435 /* Print the return type? */
1436 if (show_return)
1437 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1438 && !DECL_DESTRUCTOR_P (t);
1439 if (show_return)
1440 {
1441 tree ret = fndecl_declared_return_type (t);
1442 dump_type_prefix (pp, ret, flags);
1443 }
1444
1445 /* Print the function name. */
1446 if (!do_outer_scope)
1447 /* Nothing. */;
1448 else if (cname)
1449 {
1450 dump_type (pp, cname, flags);
1451 pp_cxx_colon_colon (pp);
1452 }
1453 else
1454 dump_scope (pp, CP_DECL_CONTEXT (t), flags);
1455
1456 dump_function_name (pp, t, flags);
1457
1458 if (!(flags & TFF_NO_FUNCTION_ARGUMENTS))
1459 {
1460 dump_parameters (pp, parmtypes, flags);
1461
1462 if (TREE_CODE (fntype) == METHOD_TYPE)
1463 {
1464 pp->padding = pp_before;
1465 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (fntype));
1466 dump_ref_qualifier (pp, fntype, flags);
1467 }
1468
1469 if (flags & TFF_EXCEPTION_SPECIFICATION)
1470 {
1471 pp->padding = pp_before;
1472 dump_exception_spec (pp, exceptions, flags);
1473 }
1474
1475 if (show_return)
1476 dump_type_suffix (pp, TREE_TYPE (fntype), flags);
1477
1478 /* If T is a template instantiation, dump the parameter binding. */
1479 if (template_parms != NULL_TREE && template_args != NULL_TREE
1480 && !(flags & TFF_NO_TEMPLATE_BINDINGS))
1481 {
1482 pp_cxx_whitespace (pp);
1483 pp_cxx_left_bracket (pp);
1484 pp_cxx_ws_string (pp, M_("with"));
1485 pp_cxx_whitespace (pp);
1486 dump_template_bindings (pp, template_parms, template_args, typenames);
1487 pp_cxx_right_bracket (pp);
1488 }
1489 }
1490 else if (template_args)
1491 {
1492 bool need_comma = false;
1493 int i;
1494 pp_cxx_begin_template_argument_list (pp);
1495 template_args = INNERMOST_TEMPLATE_ARGS (template_args);
1496 for (i = 0; i < TREE_VEC_LENGTH (template_args); ++i)
1497 {
1498 tree arg = TREE_VEC_ELT (template_args, i);
1499 if (need_comma)
1500 pp_separate_with_comma (pp);
1501 if (ARGUMENT_PACK_P (arg))
1502 pp_cxx_left_brace (pp);
1503 dump_template_argument (pp, arg, TFF_PLAIN_IDENTIFIER);
1504 if (ARGUMENT_PACK_P (arg))
1505 pp_cxx_right_brace (pp);
1506 need_comma = true;
1507 }
1508 pp_cxx_end_template_argument_list (pp);
1509 }
1510 }
1511
1512 /* Print a parameter list. If this is for a member function, the
1513 member object ptr (and any other hidden args) should have
1514 already been removed. */
1515
1516 static void
1517 dump_parameters (cxx_pretty_printer *pp, tree parmtypes, int flags)
1518 {
1519 int first = 1;
1520 flags &= ~TFF_SCOPE;
1521 pp_cxx_left_paren (pp);
1522
1523 for (first = 1; parmtypes != void_list_node;
1524 parmtypes = TREE_CHAIN (parmtypes))
1525 {
1526 if (!first)
1527 pp_separate_with_comma (pp);
1528 first = 0;
1529 if (!parmtypes)
1530 {
1531 pp_cxx_ws_string (pp, "...");
1532 break;
1533 }
1534
1535 dump_type (pp, TREE_VALUE (parmtypes), flags);
1536
1537 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
1538 {
1539 pp_cxx_whitespace (pp);
1540 pp_equal (pp);
1541 pp_cxx_whitespace (pp);
1542 dump_expr (pp, TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
1543 }
1544 }
1545
1546 pp_cxx_right_paren (pp);
1547 }
1548
1549 /* Print ref-qualifier of a FUNCTION_TYPE or METHOD_TYPE. FLAGS are ignored. */
1550
1551 static void
1552 dump_ref_qualifier (cxx_pretty_printer *pp, tree t, int flags ATTRIBUTE_UNUSED)
1553 {
1554 if (FUNCTION_REF_QUALIFIED (t))
1555 {
1556 pp->padding = pp_before;
1557 if (FUNCTION_RVALUE_QUALIFIED (t))
1558 pp_cxx_ws_string (pp, "&&");
1559 else
1560 pp_cxx_ws_string (pp, "&");
1561 }
1562 }
1563
1564 /* Print an exception specification. T is the exception specification. */
1565
1566 static void
1567 dump_exception_spec (cxx_pretty_printer *pp, tree t, int flags)
1568 {
1569 if (t && TREE_PURPOSE (t))
1570 {
1571 pp_cxx_ws_string (pp, "noexcept");
1572 pp_cxx_whitespace (pp);
1573 pp_cxx_left_paren (pp);
1574 if (DEFERRED_NOEXCEPT_SPEC_P (t))
1575 pp_cxx_ws_string (pp, "<uninstantiated>");
1576 else
1577 dump_expr (pp, TREE_PURPOSE (t), flags);
1578 pp_cxx_right_paren (pp);
1579 }
1580 else if (t)
1581 {
1582 pp_cxx_ws_string (pp, "throw");
1583 pp_cxx_whitespace (pp);
1584 pp_cxx_left_paren (pp);
1585 if (TREE_VALUE (t) != NULL_TREE)
1586 while (1)
1587 {
1588 dump_type (pp, TREE_VALUE (t), flags);
1589 t = TREE_CHAIN (t);
1590 if (!t)
1591 break;
1592 pp_separate_with_comma (pp);
1593 }
1594 pp_cxx_right_paren (pp);
1595 }
1596 }
1597
1598 /* Handle the function name for a FUNCTION_DECL node, grokking operators
1599 and destructors properly. */
1600
1601 static void
1602 dump_function_name (cxx_pretty_printer *pp, tree t, int flags)
1603 {
1604 tree name = DECL_NAME (t);
1605
1606 /* We can get here with a decl that was synthesized by language-
1607 independent machinery (e.g. coverage.c) in which case it won't
1608 have a lang_specific structure attached and DECL_CONSTRUCTOR_P
1609 will crash. In this case it is safe just to print out the
1610 literal name. */
1611 if (!DECL_LANG_SPECIFIC (t))
1612 {
1613 pp_cxx_tree_identifier (pp, name);
1614 return;
1615 }
1616
1617 if (TREE_CODE (t) == TEMPLATE_DECL)
1618 t = DECL_TEMPLATE_RESULT (t);
1619
1620 /* Don't let the user see __comp_ctor et al. */
1621 if (DECL_CONSTRUCTOR_P (t)
1622 || DECL_DESTRUCTOR_P (t))
1623 {
1624 if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
1625 name = get_identifier ("<lambda>");
1626 else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
1627 name = get_identifier ("<constructor>");
1628 else
1629 name = constructor_name (DECL_CONTEXT (t));
1630 }
1631
1632 if (DECL_DESTRUCTOR_P (t))
1633 {
1634 pp_cxx_complement (pp);
1635 dump_decl (pp, name, TFF_PLAIN_IDENTIFIER);
1636 }
1637 else if (DECL_CONV_FN_P (t))
1638 {
1639 /* This cannot use the hack that the operator's return
1640 type is stashed off of its name because it may be
1641 used for error reporting. In the case of conflicting
1642 declarations, both will have the same name, yet
1643 the types will be different, hence the TREE_TYPE field
1644 of the first name will be clobbered by the second. */
1645 pp_cxx_ws_string (pp, "operator");
1646 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1647 }
1648 else if (name && IDENTIFIER_OPNAME_P (name))
1649 pp_cxx_tree_identifier (pp, name);
1650 else if (name && UDLIT_OPER_P (name))
1651 pp_cxx_tree_identifier (pp, name);
1652 else
1653 dump_decl (pp, name, flags);
1654
1655 if (DECL_TEMPLATE_INFO (t)
1656 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
1657 && (TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1658 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
1659 dump_template_parms (pp, DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t),
1660 flags);
1661 }
1662
1663 /* Dump the template parameters from the template info INFO under control of
1664 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1665 specialization (partial or complete). For partial specializations we show
1666 the specialized parameter values. For a primary template we show no
1667 decoration. */
1668
1669 static void
1670 dump_template_parms (cxx_pretty_printer *pp, tree info,
1671 int primary, int flags)
1672 {
1673 tree args = info ? TI_ARGS (info) : NULL_TREE;
1674
1675 if (primary && flags & TFF_TEMPLATE_NAME)
1676 return;
1677 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
1678 pp_cxx_begin_template_argument_list (pp);
1679
1680 /* Be careful only to print things when we have them, so as not
1681 to crash producing error messages. */
1682 if (args && !primary)
1683 {
1684 int len, ix;
1685 len = get_non_default_template_args_count (args, flags);
1686
1687 args = INNERMOST_TEMPLATE_ARGS (args);
1688 for (ix = 0; ix != len; ix++)
1689 {
1690 tree arg = TREE_VEC_ELT (args, ix);
1691
1692 /* Only print a comma if we know there is an argument coming. In
1693 the case of an empty template argument pack, no actual
1694 argument will be printed. */
1695 if (ix
1696 && (!ARGUMENT_PACK_P (arg)
1697 || TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) > 0))
1698 pp_separate_with_comma (pp);
1699
1700 if (!arg)
1701 pp_string (pp, M_("<template parameter error>"));
1702 else
1703 dump_template_argument (pp, arg, flags);
1704 }
1705 }
1706 else if (primary)
1707 {
1708 tree tpl = TI_TEMPLATE (info);
1709 tree parms = DECL_TEMPLATE_PARMS (tpl);
1710 int len, ix;
1711
1712 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1713 len = parms ? TREE_VEC_LENGTH (parms) : 0;
1714
1715 for (ix = 0; ix != len; ix++)
1716 {
1717 tree parm;
1718
1719 if (TREE_VEC_ELT (parms, ix) == error_mark_node)
1720 {
1721 pp_string (pp, M_("<template parameter error>"));
1722 continue;
1723 }
1724
1725 parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1726
1727 if (ix)
1728 pp_separate_with_comma (pp);
1729
1730 dump_decl (pp, parm, flags & ~TFF_DECL_SPECIFIERS);
1731 }
1732 }
1733 pp_cxx_end_template_argument_list (pp);
1734 }
1735
1736 /* Print out the arguments of CALL_EXPR T as a parenthesized list using
1737 flags FLAGS. Skip over the first argument if SKIPFIRST is true. */
1738
1739 static void
1740 dump_call_expr_args (cxx_pretty_printer *pp, tree t, int flags, bool skipfirst)
1741 {
1742 tree arg;
1743 call_expr_arg_iterator iter;
1744
1745 pp_cxx_left_paren (pp);
1746 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
1747 {
1748 if (skipfirst)
1749 skipfirst = false;
1750 else
1751 {
1752 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1753 if (more_call_expr_args_p (&iter))
1754 pp_separate_with_comma (pp);
1755 }
1756 }
1757 pp_cxx_right_paren (pp);
1758 }
1759
1760 /* Print out the arguments of AGGR_INIT_EXPR T as a parenthesized list
1761 using flags FLAGS. Skip over the first argument if SKIPFIRST is
1762 true. */
1763
1764 static void
1765 dump_aggr_init_expr_args (cxx_pretty_printer *pp, tree t, int flags,
1766 bool skipfirst)
1767 {
1768 tree arg;
1769 aggr_init_expr_arg_iterator iter;
1770
1771 pp_cxx_left_paren (pp);
1772 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
1773 {
1774 if (skipfirst)
1775 skipfirst = false;
1776 else
1777 {
1778 dump_expr (pp, arg, flags | TFF_EXPR_IN_PARENS);
1779 if (more_aggr_init_expr_args_p (&iter))
1780 pp_separate_with_comma (pp);
1781 }
1782 }
1783 pp_cxx_right_paren (pp);
1784 }
1785
1786 /* Print out a list of initializers (subr of dump_expr). */
1787
1788 static void
1789 dump_expr_list (cxx_pretty_printer *pp, tree l, int flags)
1790 {
1791 while (l)
1792 {
1793 dump_expr (pp, TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
1794 l = TREE_CHAIN (l);
1795 if (l)
1796 pp_separate_with_comma (pp);
1797 }
1798 }
1799
1800 /* Print out a vector of initializers (subr of dump_expr). */
1801
1802 static void
1803 dump_expr_init_vec (cxx_pretty_printer *pp, vec<constructor_elt, va_gc> *v,
1804 int flags)
1805 {
1806 unsigned HOST_WIDE_INT idx;
1807 tree value;
1808
1809 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1810 {
1811 dump_expr (pp, value, flags | TFF_EXPR_IN_PARENS);
1812 if (idx != v->length () - 1)
1813 pp_separate_with_comma (pp);
1814 }
1815 }
1816
1817
1818 /* We've gotten an indirect REFERENCE (an OBJ_TYPE_REF) to a virtual
1819 function. Resolve it to a close relative -- in the sense of static
1820 type -- variant being overridden. That is close to what was written in
1821 the source code. Subroutine of dump_expr. */
1822
1823 static tree
1824 resolve_virtual_fun_from_obj_type_ref (tree ref)
1825 {
1826 tree obj_type = TREE_TYPE (OBJ_TYPE_REF_OBJECT (ref));
1827 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
1828 tree fun = BINFO_VIRTUALS (TYPE_BINFO (TREE_TYPE (obj_type)));
1829 while (index)
1830 {
1831 fun = TREE_CHAIN (fun);
1832 index -= (TARGET_VTABLE_USES_DESCRIPTORS
1833 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1834 }
1835
1836 return BV_FN (fun);
1837 }
1838
1839 /* Print out an expression E under control of FLAGS. */
1840
1841 static void
1842 dump_expr (cxx_pretty_printer *pp, tree t, int flags)
1843 {
1844 tree op;
1845
1846 if (t == 0)
1847 return;
1848
1849 if (STATEMENT_CLASS_P (t))
1850 {
1851 pp_cxx_ws_string (pp, M_("<statement>"));
1852 return;
1853 }
1854
1855 switch (TREE_CODE (t))
1856 {
1857 case VAR_DECL:
1858 case PARM_DECL:
1859 case FIELD_DECL:
1860 case CONST_DECL:
1861 case FUNCTION_DECL:
1862 case TEMPLATE_DECL:
1863 case NAMESPACE_DECL:
1864 case LABEL_DECL:
1865 case OVERLOAD:
1866 case TYPE_DECL:
1867 case IDENTIFIER_NODE:
1868 dump_decl (pp, t, ((flags & ~(TFF_DECL_SPECIFIERS|TFF_RETURN_TYPE
1869 |TFF_TEMPLATE_HEADER))
1870 | TFF_NO_FUNCTION_ARGUMENTS));
1871 break;
1872
1873 case SSA_NAME:
1874 if (SSA_NAME_VAR (t)
1875 && !DECL_ARTIFICIAL (SSA_NAME_VAR (t)))
1876 dump_expr (pp, SSA_NAME_VAR (t), flags);
1877 else
1878 pp_cxx_ws_string (pp, M_("<unknown>"));
1879 break;
1880
1881 case INTEGER_CST:
1882 case REAL_CST:
1883 case STRING_CST:
1884 case COMPLEX_CST:
1885 pp_constant (pp, t);
1886 break;
1887
1888 case USERDEF_LITERAL:
1889 pp_cxx_userdef_literal (pp, t);
1890 break;
1891
1892 case THROW_EXPR:
1893 /* While waiting for caret diagnostics, avoid printing
1894 __cxa_allocate_exception, __cxa_throw, and the like. */
1895 pp_cxx_ws_string (pp, M_("<throw-expression>"));
1896 break;
1897
1898 case PTRMEM_CST:
1899 pp_ampersand (pp);
1900 dump_type (pp, PTRMEM_CST_CLASS (t), flags);
1901 pp_cxx_colon_colon (pp);
1902 pp_cxx_tree_identifier (pp, DECL_NAME (PTRMEM_CST_MEMBER (t)));
1903 break;
1904
1905 case COMPOUND_EXPR:
1906 pp_cxx_left_paren (pp);
1907 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1908 pp_separate_with_comma (pp);
1909 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1910 pp_cxx_right_paren (pp);
1911 break;
1912
1913 case COND_EXPR:
1914 pp_cxx_left_paren (pp);
1915 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1916 pp_string (pp, " ? ");
1917 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1918 pp_string (pp, " : ");
1919 dump_expr (pp, TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
1920 pp_cxx_right_paren (pp);
1921 break;
1922
1923 case SAVE_EXPR:
1924 if (TREE_HAS_CONSTRUCTOR (t))
1925 {
1926 pp_cxx_ws_string (pp, "new");
1927 pp_cxx_whitespace (pp);
1928 dump_type (pp, TREE_TYPE (TREE_TYPE (t)), flags);
1929 }
1930 else
1931 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1932 break;
1933
1934 case AGGR_INIT_EXPR:
1935 {
1936 tree fn = NULL_TREE;
1937
1938 if (TREE_CODE (AGGR_INIT_EXPR_FN (t)) == ADDR_EXPR)
1939 fn = TREE_OPERAND (AGGR_INIT_EXPR_FN (t), 0);
1940
1941 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1942 {
1943 if (DECL_CONSTRUCTOR_P (fn))
1944 dump_type (pp, DECL_CONTEXT (fn), flags);
1945 else
1946 dump_decl (pp, fn, 0);
1947 }
1948 else
1949 dump_expr (pp, AGGR_INIT_EXPR_FN (t), 0);
1950 }
1951 dump_aggr_init_expr_args (pp, t, flags, true);
1952 break;
1953
1954 case CALL_EXPR:
1955 {
1956 tree fn = CALL_EXPR_FN (t);
1957 bool skipfirst = false;
1958
1959 if (TREE_CODE (fn) == ADDR_EXPR)
1960 fn = TREE_OPERAND (fn, 0);
1961
1962 /* Nobody is interested in seeing the guts of vcalls. */
1963 if (TREE_CODE (fn) == OBJ_TYPE_REF)
1964 fn = resolve_virtual_fun_from_obj_type_ref (fn);
1965
1966 if (TREE_TYPE (fn) != NULL_TREE
1967 && NEXT_CODE (fn) == METHOD_TYPE
1968 && call_expr_nargs (t))
1969 {
1970 tree ob = CALL_EXPR_ARG (t, 0);
1971 if (TREE_CODE (ob) == ADDR_EXPR)
1972 {
1973 dump_expr (pp, TREE_OPERAND (ob, 0),
1974 flags | TFF_EXPR_IN_PARENS);
1975 pp_cxx_dot (pp);
1976 }
1977 else if (TREE_CODE (ob) != PARM_DECL
1978 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1979 {
1980 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
1981 pp_cxx_arrow (pp);
1982 }
1983 skipfirst = true;
1984 }
1985 dump_expr (pp, fn, flags | TFF_EXPR_IN_PARENS);
1986 dump_call_expr_args (pp, t, flags, skipfirst);
1987 }
1988 break;
1989
1990 case TARGET_EXPR:
1991 /* Note that this only works for G++ target exprs. If somebody
1992 builds a general TARGET_EXPR, there's no way to represent that
1993 it initializes anything other that the parameter slot for the
1994 default argument. Note we may have cleared out the first
1995 operand in expand_expr, so don't go killing ourselves. */
1996 if (TREE_OPERAND (t, 1))
1997 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1998 break;
1999
2000 case POINTER_PLUS_EXPR:
2001 dump_binary_op (pp, "+", t, flags);
2002 break;
2003
2004 case INIT_EXPR:
2005 case MODIFY_EXPR:
2006 dump_binary_op (pp, assignment_operator_name_info[NOP_EXPR].name,
2007 t, flags);
2008 break;
2009
2010 case PLUS_EXPR:
2011 case MINUS_EXPR:
2012 case MULT_EXPR:
2013 case TRUNC_DIV_EXPR:
2014 case TRUNC_MOD_EXPR:
2015 case MIN_EXPR:
2016 case MAX_EXPR:
2017 case LSHIFT_EXPR:
2018 case RSHIFT_EXPR:
2019 case BIT_IOR_EXPR:
2020 case BIT_XOR_EXPR:
2021 case BIT_AND_EXPR:
2022 case TRUTH_ANDIF_EXPR:
2023 case TRUTH_ORIF_EXPR:
2024 case LT_EXPR:
2025 case LE_EXPR:
2026 case GT_EXPR:
2027 case GE_EXPR:
2028 case EQ_EXPR:
2029 case NE_EXPR:
2030 case EXACT_DIV_EXPR:
2031 dump_binary_op (pp, operator_name_info[TREE_CODE (t)].name, t, flags);
2032 break;
2033
2034 case CEIL_DIV_EXPR:
2035 case FLOOR_DIV_EXPR:
2036 case ROUND_DIV_EXPR:
2037 case RDIV_EXPR:
2038 dump_binary_op (pp, "/", t, flags);
2039 break;
2040
2041 case CEIL_MOD_EXPR:
2042 case FLOOR_MOD_EXPR:
2043 case ROUND_MOD_EXPR:
2044 dump_binary_op (pp, "%", t, flags);
2045 break;
2046
2047 case COMPONENT_REF:
2048 {
2049 tree ob = TREE_OPERAND (t, 0);
2050 if (INDIRECT_REF_P (ob))
2051 {
2052 ob = TREE_OPERAND (ob, 0);
2053 if (TREE_CODE (ob) != PARM_DECL
2054 || (DECL_NAME (ob)
2055 && strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this")))
2056 {
2057 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2058 if (TREE_CODE (TREE_TYPE (ob)) == REFERENCE_TYPE)
2059 pp_cxx_dot (pp);
2060 else
2061 pp_cxx_arrow (pp);
2062 }
2063 }
2064 else
2065 {
2066 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2067 pp_cxx_dot (pp);
2068 }
2069 dump_expr (pp, TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
2070 }
2071 break;
2072
2073 case ARRAY_REF:
2074 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2075 pp_cxx_left_bracket (pp);
2076 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2077 pp_cxx_right_bracket (pp);
2078 break;
2079
2080 case ARRAY_NOTATION_REF:
2081 dump_expr (pp, ARRAY_NOTATION_ARRAY (t), flags | TFF_EXPR_IN_PARENS);
2082 pp_cxx_left_bracket (pp);
2083 dump_expr (pp, ARRAY_NOTATION_START (t), flags | TFF_EXPR_IN_PARENS);
2084 pp_colon (pp);
2085 dump_expr (pp, ARRAY_NOTATION_LENGTH (t), flags | TFF_EXPR_IN_PARENS);
2086 pp_colon (pp);
2087 dump_expr (pp, ARRAY_NOTATION_STRIDE (t), flags | TFF_EXPR_IN_PARENS);
2088 pp_cxx_right_bracket (pp);
2089 break;
2090
2091 case UNARY_PLUS_EXPR:
2092 dump_unary_op (pp, "+", t, flags);
2093 break;
2094
2095 case ADDR_EXPR:
2096 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2097 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
2098 /* An ADDR_EXPR can have reference type. In that case, we
2099 shouldn't print the `&' doing so indicates to the user
2100 that the expression has pointer type. */
2101 || (TREE_TYPE (t)
2102 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
2103 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2104 else if (TREE_CODE (TREE_OPERAND (t, 0)) == LABEL_DECL)
2105 dump_unary_op (pp, "&&", t, flags);
2106 else
2107 dump_unary_op (pp, "&", t, flags);
2108 break;
2109
2110 case INDIRECT_REF:
2111 if (TREE_HAS_CONSTRUCTOR (t))
2112 {
2113 t = TREE_OPERAND (t, 0);
2114 gcc_assert (TREE_CODE (t) == CALL_EXPR);
2115 dump_expr (pp, CALL_EXPR_FN (t), flags | TFF_EXPR_IN_PARENS);
2116 dump_call_expr_args (pp, t, flags, true);
2117 }
2118 else
2119 {
2120 if (TREE_OPERAND (t,0) != NULL_TREE
2121 && TREE_TYPE (TREE_OPERAND (t, 0))
2122 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
2123 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2124 else
2125 dump_unary_op (pp, "*", t, flags);
2126 }
2127 break;
2128
2129 case MEM_REF:
2130 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
2131 && integer_zerop (TREE_OPERAND (t, 1)))
2132 dump_expr (pp, TREE_OPERAND (TREE_OPERAND (t, 0), 0), flags);
2133 else
2134 {
2135 pp_cxx_star (pp);
2136 if (!integer_zerop (TREE_OPERAND (t, 1)))
2137 {
2138 pp_cxx_left_paren (pp);
2139 if (!integer_onep (TYPE_SIZE_UNIT
2140 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))))))
2141 {
2142 pp_cxx_left_paren (pp);
2143 dump_type (pp, ptr_type_node, flags);
2144 pp_cxx_right_paren (pp);
2145 }
2146 }
2147 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2148 if (!integer_zerop (TREE_OPERAND (t, 1)))
2149 {
2150 pp_cxx_ws_string (pp, "+");
2151 dump_expr (pp, fold_convert (ssizetype, TREE_OPERAND (t, 1)),
2152 flags);
2153 pp_cxx_right_paren (pp);
2154 }
2155 }
2156 break;
2157
2158 case NEGATE_EXPR:
2159 case BIT_NOT_EXPR:
2160 case TRUTH_NOT_EXPR:
2161 case PREDECREMENT_EXPR:
2162 case PREINCREMENT_EXPR:
2163 dump_unary_op (pp, operator_name_info [TREE_CODE (t)].name, t, flags);
2164 break;
2165
2166 case POSTDECREMENT_EXPR:
2167 case POSTINCREMENT_EXPR:
2168 pp_cxx_left_paren (pp);
2169 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2170 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2171 pp_cxx_right_paren (pp);
2172 break;
2173
2174 case NON_LVALUE_EXPR:
2175 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
2176 should be another level of INDIRECT_REF so that I don't have to do
2177 this. */
2178 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
2179 {
2180 tree next = TREE_TYPE (TREE_TYPE (t));
2181
2182 while (TYPE_PTR_P (next))
2183 next = TREE_TYPE (next);
2184
2185 if (TREE_CODE (next) == FUNCTION_TYPE)
2186 {
2187 if (flags & TFF_EXPR_IN_PARENS)
2188 pp_cxx_left_paren (pp);
2189 pp_cxx_star (pp);
2190 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2191 if (flags & TFF_EXPR_IN_PARENS)
2192 pp_cxx_right_paren (pp);
2193 break;
2194 }
2195 /* Else fall through. */
2196 }
2197 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2198 break;
2199
2200 CASE_CONVERT:
2201 case IMPLICIT_CONV_EXPR:
2202 case VIEW_CONVERT_EXPR:
2203 {
2204 tree op = TREE_OPERAND (t, 0);
2205 tree ttype = TREE_TYPE (t);
2206 tree optype = TREE_TYPE (op);
2207
2208 if (TREE_CODE (ttype) != TREE_CODE (optype)
2209 && POINTER_TYPE_P (ttype)
2210 && POINTER_TYPE_P (optype)
2211 && same_type_p (TREE_TYPE (optype),
2212 TREE_TYPE (ttype)))
2213 {
2214 if (TREE_CODE (ttype) == REFERENCE_TYPE)
2215 dump_unary_op (pp, "*", t, flags);
2216 else
2217 dump_unary_op (pp, "&", t, flags);
2218 }
2219 else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t)))
2220 {
2221 /* It is a cast, but we cannot tell whether it is a
2222 reinterpret or static cast. Use the C style notation. */
2223 if (flags & TFF_EXPR_IN_PARENS)
2224 pp_cxx_left_paren (pp);
2225 pp_cxx_left_paren (pp);
2226 dump_type (pp, TREE_TYPE (t), flags);
2227 pp_cxx_right_paren (pp);
2228 dump_expr (pp, op, flags | TFF_EXPR_IN_PARENS);
2229 if (flags & TFF_EXPR_IN_PARENS)
2230 pp_cxx_right_paren (pp);
2231 }
2232 else
2233 dump_expr (pp, op, flags);
2234 break;
2235 }
2236
2237 case CONSTRUCTOR:
2238 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
2239 {
2240 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
2241
2242 if (integer_zerop (idx))
2243 {
2244 /* A NULL pointer-to-member constant. */
2245 pp_cxx_left_paren (pp);
2246 pp_cxx_left_paren (pp);
2247 dump_type (pp, TREE_TYPE (t), flags);
2248 pp_cxx_right_paren (pp);
2249 pp_character (pp, '0');
2250 pp_cxx_right_paren (pp);
2251 break;
2252 }
2253 else if (host_integerp (idx, 0))
2254 {
2255 tree virtuals;
2256 unsigned HOST_WIDE_INT n;
2257
2258 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
2259 t = TYPE_METHOD_BASETYPE (t);
2260 virtuals = BINFO_VIRTUALS (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
2261
2262 n = tree_low_cst (idx, 0);
2263
2264 /* Map vtable index back one, to allow for the null pointer to
2265 member. */
2266 --n;
2267
2268 while (n > 0 && virtuals)
2269 {
2270 --n;
2271 virtuals = TREE_CHAIN (virtuals);
2272 }
2273 if (virtuals)
2274 {
2275 dump_expr (pp, BV_FN (virtuals),
2276 flags | TFF_EXPR_IN_PARENS);
2277 break;
2278 }
2279 }
2280 }
2281 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
2282 pp_string (pp, "<lambda closure object>");
2283 if (TREE_TYPE (t) && EMPTY_CONSTRUCTOR_P (t))
2284 {
2285 dump_type (pp, TREE_TYPE (t), 0);
2286 pp_cxx_left_paren (pp);
2287 pp_cxx_right_paren (pp);
2288 }
2289 else
2290 {
2291 if (!BRACE_ENCLOSED_INITIALIZER_P (t))
2292 dump_type (pp, TREE_TYPE (t), 0);
2293 pp_cxx_left_brace (pp);
2294 dump_expr_init_vec (pp, CONSTRUCTOR_ELTS (t), flags);
2295 pp_cxx_right_brace (pp);
2296 }
2297
2298 break;
2299
2300 case OFFSET_REF:
2301 {
2302 tree ob = TREE_OPERAND (t, 0);
2303 if (is_dummy_object (ob))
2304 {
2305 t = TREE_OPERAND (t, 1);
2306 if (TREE_CODE (t) == FUNCTION_DECL)
2307 /* A::f */
2308 dump_expr (pp, t, flags | TFF_EXPR_IN_PARENS);
2309 else if (BASELINK_P (t))
2310 dump_expr (pp, OVL_CURRENT (BASELINK_FUNCTIONS (t)),
2311 flags | TFF_EXPR_IN_PARENS);
2312 else
2313 dump_decl (pp, t, flags);
2314 }
2315 else
2316 {
2317 if (INDIRECT_REF_P (ob))
2318 {
2319 dump_expr (pp, TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
2320 pp_cxx_arrow (pp);
2321 pp_cxx_star (pp);
2322 }
2323 else
2324 {
2325 dump_expr (pp, ob, flags | TFF_EXPR_IN_PARENS);
2326 pp_cxx_dot (pp);
2327 pp_cxx_star (pp);
2328 }
2329 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2330 }
2331 break;
2332 }
2333
2334 case TEMPLATE_PARM_INDEX:
2335 dump_decl (pp, TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
2336 break;
2337
2338 case CAST_EXPR:
2339 if (TREE_OPERAND (t, 0) == NULL_TREE
2340 || TREE_CHAIN (TREE_OPERAND (t, 0)))
2341 {
2342 dump_type (pp, TREE_TYPE (t), flags);
2343 pp_cxx_left_paren (pp);
2344 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2345 pp_cxx_right_paren (pp);
2346 }
2347 else
2348 {
2349 pp_cxx_left_paren (pp);
2350 dump_type (pp, TREE_TYPE (t), flags);
2351 pp_cxx_right_paren (pp);
2352 pp_cxx_left_paren (pp);
2353 dump_expr_list (pp, TREE_OPERAND (t, 0), flags);
2354 pp_cxx_right_paren (pp);
2355 }
2356 break;
2357
2358 case STATIC_CAST_EXPR:
2359 pp_cxx_ws_string (pp, "static_cast");
2360 goto cast;
2361 case REINTERPRET_CAST_EXPR:
2362 pp_cxx_ws_string (pp, "reinterpret_cast");
2363 goto cast;
2364 case CONST_CAST_EXPR:
2365 pp_cxx_ws_string (pp, "const_cast");
2366 goto cast;
2367 case DYNAMIC_CAST_EXPR:
2368 pp_cxx_ws_string (pp, "dynamic_cast");
2369 cast:
2370 pp_cxx_begin_template_argument_list (pp);
2371 dump_type (pp, TREE_TYPE (t), flags);
2372 pp_cxx_end_template_argument_list (pp);
2373 pp_cxx_left_paren (pp);
2374 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2375 pp_cxx_right_paren (pp);
2376 break;
2377
2378 case ARROW_EXPR:
2379 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2380 pp_cxx_arrow (pp);
2381 break;
2382
2383 case SIZEOF_EXPR:
2384 case ALIGNOF_EXPR:
2385 if (TREE_CODE (t) == SIZEOF_EXPR)
2386 pp_cxx_ws_string (pp, "sizeof");
2387 else
2388 {
2389 gcc_assert (TREE_CODE (t) == ALIGNOF_EXPR);
2390 pp_cxx_ws_string (pp, "__alignof__");
2391 }
2392 op = TREE_OPERAND (t, 0);
2393 if (PACK_EXPANSION_P (op))
2394 {
2395 pp_string (pp, "...");
2396 op = PACK_EXPANSION_PATTERN (op);
2397 }
2398 pp_cxx_whitespace (pp);
2399 pp_cxx_left_paren (pp);
2400 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
2401 dump_type (pp, TREE_TYPE (op), flags);
2402 else if (TYPE_P (TREE_OPERAND (t, 0)))
2403 dump_type (pp, op, flags);
2404 else
2405 dump_expr (pp, op, flags);
2406 pp_cxx_right_paren (pp);
2407 break;
2408
2409 case AT_ENCODE_EXPR:
2410 pp_cxx_ws_string (pp, "@encode");
2411 pp_cxx_whitespace (pp);
2412 pp_cxx_left_paren (pp);
2413 dump_type (pp, TREE_OPERAND (t, 0), flags);
2414 pp_cxx_right_paren (pp);
2415 break;
2416
2417 case NOEXCEPT_EXPR:
2418 pp_cxx_ws_string (pp, "noexcept");
2419 pp_cxx_whitespace (pp);
2420 pp_cxx_left_paren (pp);
2421 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2422 pp_cxx_right_paren (pp);
2423 break;
2424
2425 case REALPART_EXPR:
2426 case IMAGPART_EXPR:
2427 pp_cxx_ws_string (pp, operator_name_info[TREE_CODE (t)].name);
2428 pp_cxx_whitespace (pp);
2429 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2430 break;
2431
2432 case DEFAULT_ARG:
2433 pp_string (pp, M_("<unparsed>"));
2434 break;
2435
2436 case TRY_CATCH_EXPR:
2437 case WITH_CLEANUP_EXPR:
2438 case CLEANUP_POINT_EXPR:
2439 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2440 break;
2441
2442 case PSEUDO_DTOR_EXPR:
2443 dump_expr (pp, TREE_OPERAND (t, 2), flags);
2444 pp_cxx_dot (pp);
2445 dump_type (pp, TREE_OPERAND (t, 0), flags);
2446 pp_cxx_colon_colon (pp);
2447 pp_cxx_complement (pp);
2448 dump_type (pp, TREE_OPERAND (t, 1), flags);
2449 break;
2450
2451 case TEMPLATE_ID_EXPR:
2452 dump_decl (pp, t, flags);
2453 break;
2454
2455 case BIND_EXPR:
2456 case STMT_EXPR:
2457 case EXPR_STMT:
2458 case STATEMENT_LIST:
2459 /* We don't yet have a way of dumping statements in a
2460 human-readable format. */
2461 pp_string (pp, "({...})");
2462 break;
2463
2464 case LOOP_EXPR:
2465 pp_string (pp, "while (1) { ");
2466 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2467 pp_cxx_right_brace (pp);
2468 break;
2469
2470 case EXIT_EXPR:
2471 pp_string (pp, "if (");
2472 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2473 pp_string (pp, ") break; ");
2474 break;
2475
2476 case BASELINK:
2477 dump_expr (pp, BASELINK_FUNCTIONS (t), flags & ~TFF_EXPR_IN_PARENS);
2478 break;
2479
2480 case EMPTY_CLASS_EXPR:
2481 dump_type (pp, TREE_TYPE (t), flags);
2482 pp_cxx_left_paren (pp);
2483 pp_cxx_right_paren (pp);
2484 break;
2485
2486 case NON_DEPENDENT_EXPR:
2487 dump_expr (pp, TREE_OPERAND (t, 0), flags);
2488 break;
2489
2490 case ARGUMENT_PACK_SELECT:
2491 dump_template_argument (pp, ARGUMENT_PACK_SELECT_FROM_PACK (t), flags);
2492 break;
2493
2494 case RECORD_TYPE:
2495 case UNION_TYPE:
2496 case ENUMERAL_TYPE:
2497 case REAL_TYPE:
2498 case VOID_TYPE:
2499 case BOOLEAN_TYPE:
2500 case INTEGER_TYPE:
2501 case COMPLEX_TYPE:
2502 case VECTOR_TYPE:
2503 pp_type_specifier_seq (pp, t);
2504 break;
2505
2506 case TYPENAME_TYPE:
2507 /* We get here when we want to print a dependent type as an
2508 id-expression, without any disambiguator decoration. */
2509 pp_id_expression (pp, t);
2510 break;
2511
2512 case TEMPLATE_TYPE_PARM:
2513 case TEMPLATE_TEMPLATE_PARM:
2514 case BOUND_TEMPLATE_TEMPLATE_PARM:
2515 dump_type (pp, t, flags);
2516 break;
2517
2518 case TRAIT_EXPR:
2519 pp_cxx_trait_expression (pp, t);
2520 break;
2521
2522 case VA_ARG_EXPR:
2523 pp_cxx_va_arg_expression (pp, t);
2524 break;
2525
2526 case OFFSETOF_EXPR:
2527 pp_cxx_offsetof_expression (pp, t);
2528 break;
2529
2530 case SCOPE_REF:
2531 dump_decl (pp, t, flags);
2532 break;
2533
2534 case EXPR_PACK_EXPANSION:
2535 case TYPEID_EXPR:
2536 case MEMBER_REF:
2537 case DOTSTAR_EXPR:
2538 case NEW_EXPR:
2539 case VEC_NEW_EXPR:
2540 case DELETE_EXPR:
2541 case VEC_DELETE_EXPR:
2542 case MODOP_EXPR:
2543 case ABS_EXPR:
2544 case CONJ_EXPR:
2545 case VECTOR_CST:
2546 case FIXED_CST:
2547 case UNORDERED_EXPR:
2548 case ORDERED_EXPR:
2549 case UNLT_EXPR:
2550 case UNLE_EXPR:
2551 case UNGT_EXPR:
2552 case UNGE_EXPR:
2553 case UNEQ_EXPR:
2554 case LTGT_EXPR:
2555 case COMPLEX_EXPR:
2556 case BIT_FIELD_REF:
2557 case FIX_TRUNC_EXPR:
2558 case FLOAT_EXPR:
2559 pp_expression (pp, t);
2560 break;
2561
2562 case TRUTH_AND_EXPR:
2563 case TRUTH_OR_EXPR:
2564 case TRUTH_XOR_EXPR:
2565 if (flags & TFF_EXPR_IN_PARENS)
2566 pp_cxx_left_paren (pp);
2567 pp_expression (pp, t);
2568 if (flags & TFF_EXPR_IN_PARENS)
2569 pp_cxx_right_paren (pp);
2570 break;
2571
2572 case OBJ_TYPE_REF:
2573 dump_expr (pp, resolve_virtual_fun_from_obj_type_ref (t), flags);
2574 break;
2575
2576 case LAMBDA_EXPR:
2577 pp_string (pp, M_("<lambda>"));
2578 break;
2579
2580 case PAREN_EXPR:
2581 pp_cxx_left_paren (pp);
2582 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2583 pp_cxx_right_paren (pp);
2584 break;
2585
2586 /* This list is incomplete, but should suffice for now.
2587 It is very important that `sorry' does not call
2588 `report_error_function'. That could cause an infinite loop. */
2589 default:
2590 pp_unsupported_tree (pp, t);
2591 /* fall through to ERROR_MARK... */
2592 case ERROR_MARK:
2593 pp_string (pp, M_("<expression error>"));
2594 break;
2595 }
2596 }
2597
2598 static void
2599 dump_binary_op (cxx_pretty_printer *pp, const char *opstring, tree t,
2600 int flags)
2601 {
2602 pp_cxx_left_paren (pp);
2603 dump_expr (pp, TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
2604 pp_cxx_whitespace (pp);
2605 if (opstring)
2606 pp_cxx_ws_string (pp, opstring);
2607 else
2608 pp_string (pp, M_("<unknown operator>"));
2609 pp_cxx_whitespace (pp);
2610 dump_expr (pp, TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
2611 pp_cxx_right_paren (pp);
2612 }
2613
2614 static void
2615 dump_unary_op (cxx_pretty_printer *pp, const char *opstring, tree t, int flags)
2616 {
2617 if (flags & TFF_EXPR_IN_PARENS)
2618 pp_cxx_left_paren (pp);
2619 pp_cxx_ws_string (pp, opstring);
2620 dump_expr (pp, TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2621 if (flags & TFF_EXPR_IN_PARENS)
2622 pp_cxx_right_paren (pp);
2623 }
2624
2625 static void
2626 reinit_cxx_pp (void)
2627 {
2628 pp_clear_output_area (cxx_pp);
2629 cxx_pp->padding = pp_none;
2630 pp_indentation (cxx_pp) = 0;
2631 pp_needs_newline (cxx_pp) = false;
2632 cxx_pp->enclosing_scope = current_function_decl;
2633 }
2634
2635
2636 /* Exported interface to stringifying types, exprs and decls under TFF_*
2637 control. */
2638
2639 const char *
2640 type_as_string (tree typ, int flags)
2641 {
2642 reinit_cxx_pp ();
2643 pp_translate_identifiers (cxx_pp) = false;
2644 dump_type (cxx_pp, typ, flags);
2645 return pp_formatted_text (cxx_pp);
2646 }
2647
2648 const char *
2649 type_as_string_translate (tree typ, int flags)
2650 {
2651 reinit_cxx_pp ();
2652 dump_type (cxx_pp, typ, flags);
2653 return pp_formatted_text (cxx_pp);
2654 }
2655
2656 const char *
2657 expr_as_string (tree decl, int flags)
2658 {
2659 reinit_cxx_pp ();
2660 pp_translate_identifiers (cxx_pp) = false;
2661 dump_expr (cxx_pp, decl, flags);
2662 return pp_formatted_text (cxx_pp);
2663 }
2664
2665 /* Wrap decl_as_string with options appropriate for dwarf. */
2666
2667 const char *
2668 decl_as_dwarf_string (tree decl, int flags)
2669 {
2670 const char *name;
2671 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2672 here will be adequate to get the desired behaviour. */
2673 cxx_pp->flags |= pp_c_flag_gnu_v3;
2674 name = decl_as_string (decl, flags);
2675 /* Subsequent calls to the pretty printer shouldn't use this style. */
2676 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2677 return name;
2678 }
2679
2680 const char *
2681 decl_as_string (tree decl, int flags)
2682 {
2683 reinit_cxx_pp ();
2684 pp_translate_identifiers (cxx_pp) = false;
2685 dump_decl (cxx_pp, decl, flags);
2686 return pp_formatted_text (cxx_pp);
2687 }
2688
2689 const char *
2690 decl_as_string_translate (tree decl, int flags)
2691 {
2692 reinit_cxx_pp ();
2693 dump_decl (cxx_pp, decl, flags);
2694 return pp_formatted_text (cxx_pp);
2695 }
2696
2697 /* Wrap lang_decl_name with options appropriate for dwarf. */
2698
2699 const char *
2700 lang_decl_dwarf_name (tree decl, int v, bool translate)
2701 {
2702 const char *name;
2703 /* Curiously, reinit_cxx_pp doesn't reset the flags field, so setting the flag
2704 here will be adequate to get the desired behaviour. */
2705 cxx_pp->flags |= pp_c_flag_gnu_v3;
2706 name = lang_decl_name (decl, v, translate);
2707 /* Subsequent calls to the pretty printer shouldn't use this style. */
2708 cxx_pp->flags &= ~pp_c_flag_gnu_v3;
2709 return name;
2710 }
2711
2712 /* Generate the three forms of printable names for cxx_printable_name. */
2713
2714 const char *
2715 lang_decl_name (tree decl, int v, bool translate)
2716 {
2717 if (v >= 2)
2718 return (translate
2719 ? decl_as_string_translate (decl, TFF_DECL_SPECIFIERS)
2720 : decl_as_string (decl, TFF_DECL_SPECIFIERS));
2721
2722 reinit_cxx_pp ();
2723 pp_translate_identifiers (cxx_pp) = translate;
2724 if (v == 1
2725 && (DECL_CLASS_SCOPE_P (decl)
2726 || (DECL_NAMESPACE_SCOPE_P (decl)
2727 && CP_DECL_CONTEXT (decl) != global_namespace)))
2728 {
2729 dump_type (cxx_pp, CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
2730 pp_cxx_colon_colon (cxx_pp);
2731 }
2732
2733 if (TREE_CODE (decl) == FUNCTION_DECL)
2734 dump_function_name (cxx_pp, decl, TFF_PLAIN_IDENTIFIER);
2735 else if ((DECL_NAME (decl) == NULL_TREE)
2736 && TREE_CODE (decl) == NAMESPACE_DECL)
2737 dump_decl (cxx_pp, decl, TFF_PLAIN_IDENTIFIER | TFF_UNQUALIFIED_NAME);
2738 else
2739 dump_decl (cxx_pp, DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2740
2741 return pp_formatted_text (cxx_pp);
2742 }
2743
2744 /* Return the location of a tree passed to %+ formats. */
2745
2746 location_t
2747 location_of (tree t)
2748 {
2749 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
2750 t = DECL_CONTEXT (t);
2751 else if (TYPE_P (t))
2752 {
2753 t = TYPE_MAIN_DECL (t);
2754 if (t == NULL_TREE)
2755 return input_location;
2756 }
2757 else if (TREE_CODE (t) == OVERLOAD)
2758 t = OVL_FUNCTION (t);
2759
2760 if (DECL_P (t))
2761 return DECL_SOURCE_LOCATION (t);
2762 return EXPR_LOC_OR_HERE (t);
2763 }
2764
2765 /* Now the interfaces from error et al to dump_type et al. Each takes an
2766 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
2767 function. */
2768
2769 static const char *
2770 decl_to_string (tree decl, int verbose)
2771 {
2772 int flags = 0;
2773
2774 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2775 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
2776 flags = TFF_CLASS_KEY_OR_ENUM;
2777 if (verbose)
2778 flags |= TFF_DECL_SPECIFIERS;
2779 else if (TREE_CODE (decl) == FUNCTION_DECL)
2780 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2781 flags |= TFF_TEMPLATE_HEADER;
2782
2783 reinit_cxx_pp ();
2784 dump_decl (cxx_pp, decl, flags);
2785 return pp_formatted_text (cxx_pp);
2786 }
2787
2788 static const char *
2789 expr_to_string (tree decl)
2790 {
2791 reinit_cxx_pp ();
2792 dump_expr (cxx_pp, decl, 0);
2793 return pp_formatted_text (cxx_pp);
2794 }
2795
2796 static const char *
2797 fndecl_to_string (tree fndecl, int verbose)
2798 {
2799 int flags;
2800
2801 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS
2802 | TFF_TEMPLATE_HEADER;
2803 if (verbose)
2804 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
2805 reinit_cxx_pp ();
2806 dump_decl (cxx_pp, fndecl, flags);
2807 return pp_formatted_text (cxx_pp);
2808 }
2809
2810
2811 static const char *
2812 code_to_string (enum tree_code c)
2813 {
2814 return tree_code_name [c];
2815 }
2816
2817 const char *
2818 language_to_string (enum languages c)
2819 {
2820 switch (c)
2821 {
2822 case lang_c:
2823 return "C";
2824
2825 case lang_cplusplus:
2826 return "C++";
2827
2828 case lang_java:
2829 return "Java";
2830
2831 default:
2832 gcc_unreachable ();
2833 }
2834 return NULL;
2835 }
2836
2837 /* Return the proper printed version of a parameter to a C++ function. */
2838
2839 static const char *
2840 parm_to_string (int p)
2841 {
2842 reinit_cxx_pp ();
2843 if (p < 0)
2844 pp_string (cxx_pp, "'this'");
2845 else
2846 pp_decimal_int (cxx_pp, p + 1);
2847 return pp_formatted_text (cxx_pp);
2848 }
2849
2850 static const char *
2851 op_to_string (enum tree_code p)
2852 {
2853 tree id = operator_name_info[p].identifier;
2854 return id ? IDENTIFIER_POINTER (id) : M_("<unknown>");
2855 }
2856
2857 static const char *
2858 type_to_string (tree typ, int verbose)
2859 {
2860 int flags = 0;
2861 if (verbose)
2862 flags |= TFF_CLASS_KEY_OR_ENUM;
2863 flags |= TFF_TEMPLATE_HEADER;
2864
2865 reinit_cxx_pp ();
2866 dump_type (cxx_pp, typ, flags);
2867 /* If we're printing a type that involves typedefs, also print the
2868 stripped version. But sometimes the stripped version looks
2869 exactly the same, so we don't want it after all. To avoid printing
2870 it in that case, we play ugly obstack games. */
2871 if (typ && TYPE_P (typ) && typ != TYPE_CANONICAL (typ)
2872 && !uses_template_parms (typ))
2873 {
2874 int aka_start; char *p;
2875 struct obstack *ob = cxx_pp->buffer->obstack;
2876 /* Remember the end of the initial dump. */
2877 int len = obstack_object_size (ob);
2878 tree aka = strip_typedefs (typ);
2879 pp_string (cxx_pp, " {aka");
2880 pp_cxx_whitespace (cxx_pp);
2881 /* And remember the start of the aka dump. */
2882 aka_start = obstack_object_size (ob);
2883 dump_type (cxx_pp, aka, flags);
2884 pp_right_brace (cxx_pp);
2885 p = (char*)obstack_base (ob);
2886 /* If they are identical, cut off the aka with a NUL. */
2887 if (memcmp (p, p+aka_start, len) == 0)
2888 p[len] = '\0';
2889 }
2890 return pp_formatted_text (cxx_pp);
2891 }
2892
2893 static const char *
2894 assop_to_string (enum tree_code p)
2895 {
2896 tree id = assignment_operator_name_info[(int) p].identifier;
2897 return id ? IDENTIFIER_POINTER (id) : M_("{unknown}");
2898 }
2899
2900 static const char *
2901 args_to_string (tree p, int verbose)
2902 {
2903 int flags = 0;
2904 if (verbose)
2905 flags |= TFF_CLASS_KEY_OR_ENUM;
2906
2907 if (p == NULL_TREE)
2908 return "";
2909
2910 if (TYPE_P (TREE_VALUE (p)))
2911 return type_as_string_translate (p, flags);
2912
2913 reinit_cxx_pp ();
2914 for (; p; p = TREE_CHAIN (p))
2915 {
2916 if (TREE_VALUE (p) == null_node)
2917 pp_cxx_ws_string (cxx_pp, "NULL");
2918 else
2919 dump_type (cxx_pp, error_type (TREE_VALUE (p)), flags);
2920 if (TREE_CHAIN (p))
2921 pp_separate_with_comma (cxx_pp);
2922 }
2923 return pp_formatted_text (cxx_pp);
2924 }
2925
2926 /* Pretty-print a deduction substitution (from deduction_tsubst_fntype). P
2927 is a TREE_LIST with purpose the TEMPLATE_DECL, value the template
2928 arguments. */
2929
2930 static const char *
2931 subst_to_string (tree p)
2932 {
2933 tree decl = TREE_PURPOSE (p);
2934 tree targs = TREE_VALUE (p);
2935 tree tparms = DECL_TEMPLATE_PARMS (decl);
2936 int flags = (TFF_DECL_SPECIFIERS|TFF_TEMPLATE_HEADER
2937 |TFF_NO_TEMPLATE_BINDINGS);
2938
2939 if (p == NULL_TREE)
2940 return "";
2941
2942 reinit_cxx_pp ();
2943 dump_template_decl (cxx_pp, TREE_PURPOSE (p), flags);
2944 pp_cxx_whitespace (cxx_pp);
2945 pp_cxx_left_bracket (cxx_pp);
2946 pp_cxx_ws_string (cxx_pp, M_("with"));
2947 pp_cxx_whitespace (cxx_pp);
2948 dump_template_bindings (cxx_pp, tparms, targs, NULL);
2949 pp_cxx_right_bracket (cxx_pp);
2950 return pp_formatted_text (cxx_pp);
2951 }
2952
2953 static const char *
2954 cv_to_string (tree p, int v)
2955 {
2956 reinit_cxx_pp ();
2957 cxx_pp->padding = v ? pp_before : pp_none;
2958 pp_cxx_cv_qualifier_seq (cxx_pp, p);
2959 return pp_formatted_text (cxx_pp);
2960 }
2961
2962 /* Langhook for print_error_function. */
2963 void
2964 cxx_print_error_function (diagnostic_context *context, const char *file,
2965 diagnostic_info *diagnostic)
2966 {
2967 lhd_print_error_function (context, file, diagnostic);
2968 pp_set_prefix (context->printer, file);
2969 maybe_print_instantiation_context (context);
2970 }
2971
2972 static void
2973 cp_diagnostic_starter (diagnostic_context *context,
2974 diagnostic_info *diagnostic)
2975 {
2976 diagnostic_report_current_module (context, diagnostic->location);
2977 cp_print_error_function (context, diagnostic);
2978 maybe_print_instantiation_context (context);
2979 maybe_print_constexpr_context (context);
2980 pp_set_prefix (context->printer, diagnostic_build_prefix (context,
2981 diagnostic));
2982 }
2983
2984 static void
2985 cp_diagnostic_finalizer (diagnostic_context *context,
2986 diagnostic_info *diagnostic)
2987 {
2988 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
2989 pp_destroy_prefix (context->printer);
2990 }
2991
2992 /* Print current function onto BUFFER, in the process of reporting
2993 a diagnostic message. Called from cp_diagnostic_starter. */
2994 static void
2995 cp_print_error_function (diagnostic_context *context,
2996 diagnostic_info *diagnostic)
2997 {
2998 /* If we are in an instantiation context, current_function_decl is likely
2999 to be wrong, so just rely on print_instantiation_full_context. */
3000 if (current_instantiation ())
3001 return;
3002 if (diagnostic_last_function_changed (context, diagnostic))
3003 {
3004 const char *old_prefix = context->printer->prefix;
3005 const char *file = LOCATION_FILE (diagnostic->location);
3006 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
3007 char *new_prefix = (file && abstract_origin == NULL)
3008 ? file_name_as_prefix (context, file) : NULL;
3009
3010 pp_set_prefix (context->printer, new_prefix);
3011
3012 if (current_function_decl == NULL)
3013 pp_string (context->printer, _("At global scope:"));
3014 else
3015 {
3016 tree fndecl, ao;
3017
3018 if (abstract_origin)
3019 {
3020 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
3021 while (TREE_CODE (ao) == BLOCK
3022 && BLOCK_ABSTRACT_ORIGIN (ao)
3023 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3024 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3025 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
3026 fndecl = ao;
3027 }
3028 else
3029 fndecl = current_function_decl;
3030
3031 pp_printf (context->printer, function_category (fndecl),
3032 cxx_printable_name_translate (fndecl, 2));
3033
3034 while (abstract_origin)
3035 {
3036 location_t *locus;
3037 tree block = abstract_origin;
3038
3039 locus = &BLOCK_SOURCE_LOCATION (block);
3040 fndecl = NULL;
3041 block = BLOCK_SUPERCONTEXT (block);
3042 while (block && TREE_CODE (block) == BLOCK
3043 && BLOCK_ABSTRACT_ORIGIN (block))
3044 {
3045 ao = BLOCK_ABSTRACT_ORIGIN (block);
3046
3047 while (TREE_CODE (ao) == BLOCK
3048 && BLOCK_ABSTRACT_ORIGIN (ao)
3049 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
3050 ao = BLOCK_ABSTRACT_ORIGIN (ao);
3051
3052 if (TREE_CODE (ao) == FUNCTION_DECL)
3053 {
3054 fndecl = ao;
3055 break;
3056 }
3057 else if (TREE_CODE (ao) != BLOCK)
3058 break;
3059
3060 block = BLOCK_SUPERCONTEXT (block);
3061 }
3062 if (fndecl)
3063 abstract_origin = block;
3064 else
3065 {
3066 while (block && TREE_CODE (block) == BLOCK)
3067 block = BLOCK_SUPERCONTEXT (block);
3068
3069 if (block && TREE_CODE (block) == FUNCTION_DECL)
3070 fndecl = block;
3071 abstract_origin = NULL;
3072 }
3073 if (fndecl)
3074 {
3075 expanded_location s = expand_location (*locus);
3076 pp_character (context->printer, ',');
3077 pp_newline (context->printer);
3078 if (s.file != NULL)
3079 {
3080 if (context->show_column && s.column != 0)
3081 pp_printf (context->printer,
3082 _(" inlined from %qs at %r%s:%d:%d%R"),
3083 cxx_printable_name_translate (fndecl, 2),
3084 "locus", s.file, s.line, s.column);
3085 else
3086 pp_printf (context->printer,
3087 _(" inlined from %qs at %r%s:%d%R"),
3088 cxx_printable_name_translate (fndecl, 2),
3089 "locus", s.file, s.line);
3090
3091 }
3092 else
3093 pp_printf (context->printer, _(" inlined from %qs"),
3094 cxx_printable_name_translate (fndecl, 2));
3095 }
3096 }
3097 pp_character (context->printer, ':');
3098 }
3099 pp_newline (context->printer);
3100
3101 diagnostic_set_last_function (context, diagnostic);
3102 pp_destroy_prefix (context->printer);
3103 context->printer->prefix = old_prefix;
3104 }
3105 }
3106
3107 /* Returns a description of FUNCTION using standard terminology. The
3108 result is a format string of the form "In CATEGORY %qs". */
3109 static const char *
3110 function_category (tree fn)
3111 {
3112 /* We can get called from the middle-end for diagnostics of function
3113 clones. Make sure we have language specific information before
3114 dereferencing it. */
3115 if (DECL_LANG_SPECIFIC (STRIP_TEMPLATE (fn))
3116 && DECL_FUNCTION_MEMBER_P (fn))
3117 {
3118 if (DECL_STATIC_FUNCTION_P (fn))
3119 return _("In static member function %qs");
3120 else if (DECL_COPY_CONSTRUCTOR_P (fn))
3121 return _("In copy constructor %qs");
3122 else if (DECL_CONSTRUCTOR_P (fn))
3123 return _("In constructor %qs");
3124 else if (DECL_DESTRUCTOR_P (fn))
3125 return _("In destructor %qs");
3126 else if (LAMBDA_FUNCTION_P (fn))
3127 return _("In lambda function");
3128 else
3129 return _("In member function %qs");
3130 }
3131 else
3132 return _("In function %qs");
3133 }
3134
3135 /* Report the full context of a current template instantiation,
3136 onto BUFFER. */
3137 static void
3138 print_instantiation_full_context (diagnostic_context *context)
3139 {
3140 struct tinst_level *p = current_instantiation ();
3141 location_t location = input_location;
3142
3143 if (p)
3144 {
3145 pp_verbatim (context->printer,
3146 TREE_CODE (p->decl) == TREE_LIST
3147 ? _("%s: In substitution of %qS:\n")
3148 : _("%s: In instantiation of %q#D:\n"),
3149 LOCATION_FILE (location),
3150 p->decl);
3151
3152 location = p->locus;
3153 p = p->next;
3154 }
3155
3156 print_instantiation_partial_context (context, p, location);
3157 }
3158
3159 /* Helper function of print_instantiation_partial_context() that
3160 prints a single line of instantiation context. */
3161
3162 static void
3163 print_instantiation_partial_context_line (diagnostic_context *context,
3164 const struct tinst_level *t,
3165 location_t loc, bool recursive_p)
3166 {
3167 expanded_location xloc;
3168 xloc = expand_location (loc);
3169
3170 if (context->show_column)
3171 pp_verbatim (context->printer, _("%r%s:%d:%d:%R "),
3172 "locus", xloc.file, xloc.line, xloc.column);
3173 else
3174 pp_verbatim (context->printer, _("%r%s:%d:%R "),
3175 "locus", xloc.file, xloc.line);
3176
3177 if (t != NULL)
3178 {
3179 if (TREE_CODE (t->decl) == TREE_LIST)
3180 pp_verbatim (context->printer,
3181 recursive_p
3182 ? _("recursively required by substitution of %qS\n")
3183 : _("required by substitution of %qS\n"),
3184 t->decl);
3185 else
3186 pp_verbatim (context->printer,
3187 recursive_p
3188 ? _("recursively required from %q#D\n")
3189 : _("required from %q#D\n"),
3190 t->decl);
3191 }
3192 else
3193 {
3194 pp_verbatim (context->printer,
3195 recursive_p
3196 ? _("recursively required from here")
3197 : _("required from here"));
3198 }
3199 }
3200
3201 /* Same as print_instantiation_full_context but less verbose. */
3202
3203 static void
3204 print_instantiation_partial_context (diagnostic_context *context,
3205 struct tinst_level *t0, location_t loc)
3206 {
3207 struct tinst_level *t;
3208 int n_total = 0;
3209 int n;
3210 location_t prev_loc = loc;
3211
3212 for (t = t0; t != NULL; t = t->next)
3213 if (prev_loc != t->locus)
3214 {
3215 prev_loc = t->locus;
3216 n_total++;
3217 }
3218
3219 t = t0;
3220
3221 if (template_backtrace_limit
3222 && n_total > template_backtrace_limit)
3223 {
3224 int skip = n_total - template_backtrace_limit;
3225 int head = template_backtrace_limit / 2;
3226
3227 /* Avoid skipping just 1. If so, skip 2. */
3228 if (skip == 1)
3229 {
3230 skip = 2;
3231 head = (template_backtrace_limit - 1) / 2;
3232 }
3233
3234 for (n = 0; n < head; n++)
3235 {
3236 gcc_assert (t != NULL);
3237 if (loc != t->locus)
3238 print_instantiation_partial_context_line (context, t, loc,
3239 /*recursive_p=*/false);
3240 loc = t->locus;
3241 t = t->next;
3242 }
3243 if (t != NULL && skip > 0)
3244 {
3245 expanded_location xloc;
3246 xloc = expand_location (loc);
3247 if (context->show_column)
3248 pp_verbatim (context->printer,
3249 _("%r%s:%d:%d:%R [ skipping %d instantiation "
3250 "contexts, use -ftemplate-backtrace-limit=0 to "
3251 "disable ]\n"),
3252 "locus", xloc.file, xloc.line, xloc.column, skip);
3253 else
3254 pp_verbatim (context->printer,
3255 _("%r%s:%d:%R [ skipping %d instantiation "
3256 "contexts, use -ftemplate-backtrace-limit=0 to "
3257 "disable ]\n"),
3258 "locus", xloc.file, xloc.line, skip);
3259
3260 do {
3261 loc = t->locus;
3262 t = t->next;
3263 } while (t != NULL && --skip > 0);
3264 }
3265 }
3266
3267 while (t != NULL)
3268 {
3269 while (t->next != NULL && t->locus == t->next->locus)
3270 {
3271 loc = t->locus;
3272 t = t->next;
3273 }
3274 print_instantiation_partial_context_line (context, t, loc,
3275 t->locus == loc);
3276 loc = t->locus;
3277 t = t->next;
3278 }
3279 print_instantiation_partial_context_line (context, NULL, loc,
3280 /*recursive_p=*/false);
3281 pp_newline (context->printer);
3282 }
3283
3284 /* Called from cp_thing to print the template context for an error. */
3285 static void
3286 maybe_print_instantiation_context (diagnostic_context *context)
3287 {
3288 if (!problematic_instantiation_changed () || current_instantiation () == 0)
3289 return;
3290
3291 record_last_problematic_instantiation ();
3292 print_instantiation_full_context (context);
3293 }
3294
3295 /* Report the bare minimum context of a template instantiation. */
3296 void
3297 print_instantiation_context (void)
3298 {
3299 print_instantiation_partial_context
3300 (global_dc, current_instantiation (), input_location);
3301 pp_newline (global_dc->printer);
3302 diagnostic_flush_buffer (global_dc);
3303 }
3304 \f
3305 /* Report what constexpr call(s) we're trying to expand, if any. */
3306
3307 void
3308 maybe_print_constexpr_context (diagnostic_context *context)
3309 {
3310 vec<tree> call_stack = cx_error_context ();
3311 unsigned ix;
3312 tree t;
3313
3314 FOR_EACH_VEC_ELT (call_stack, ix, t)
3315 {
3316 expanded_location xloc = expand_location (EXPR_LOCATION (t));
3317 const char *s = expr_as_string (t, 0);
3318 if (context->show_column)
3319 pp_verbatim (context->printer,
3320 _("%r%s:%d:%d:%R in constexpr expansion of %qs"),
3321 "locus", xloc.file, xloc.line, xloc.column, s);
3322 else
3323 pp_verbatim (context->printer,
3324 _("%r%s:%d:%R in constexpr expansion of %qs"),
3325 "locus", xloc.file, xloc.line, s);
3326 pp_newline (context->printer);
3327 }
3328 }
3329 \f
3330 /* Called from output_format -- during diagnostic message processing --
3331 to handle C++ specific format specifier with the following meanings:
3332 %A function argument-list.
3333 %C tree code.
3334 %D declaration.
3335 %E expression.
3336 %F function declaration.
3337 %L language as used in extern "lang".
3338 %O binary operator.
3339 %P function parameter whose position is indicated by an integer.
3340 %Q assignment operator.
3341 %T type.
3342 %V cv-qualifier. */
3343 static bool
3344 cp_printer (pretty_printer *pp, text_info *text, const char *spec,
3345 int precision, bool wide, bool set_locus, bool verbose)
3346 {
3347 const char *result;
3348 tree t = NULL;
3349 #define next_tree (t = va_arg (*text->args_ptr, tree))
3350 #define next_tcode ((enum tree_code) va_arg (*text->args_ptr, int))
3351 #define next_lang ((enum languages) va_arg (*text->args_ptr, int))
3352 #define next_int va_arg (*text->args_ptr, int)
3353
3354 if (precision != 0 || wide)
3355 return false;
3356
3357 if (text->locus == NULL)
3358 set_locus = false;
3359
3360 switch (*spec)
3361 {
3362 case 'A': result = args_to_string (next_tree, verbose); break;
3363 case 'C': result = code_to_string (next_tcode); break;
3364 case 'D':
3365 {
3366 tree temp = next_tree;
3367 if (VAR_P (temp)
3368 && DECL_HAS_DEBUG_EXPR_P (temp))
3369 {
3370 temp = DECL_DEBUG_EXPR (temp);
3371 if (!DECL_P (temp))
3372 {
3373 result = expr_to_string (temp);
3374 break;
3375 }
3376 }
3377 result = decl_to_string (temp, verbose);
3378 }
3379 break;
3380 case 'E': result = expr_to_string (next_tree); break;
3381 case 'F': result = fndecl_to_string (next_tree, verbose); break;
3382 case 'L': result = language_to_string (next_lang); break;
3383 case 'O': result = op_to_string (next_tcode); break;
3384 case 'P': result = parm_to_string (next_int); break;
3385 case 'Q': result = assop_to_string (next_tcode); break;
3386 case 'S': result = subst_to_string (next_tree); break;
3387 case 'T': result = type_to_string (next_tree, verbose); break;
3388 case 'V': result = cv_to_string (next_tree, verbose); break;
3389
3390 case 'K':
3391 percent_K_format (text);
3392 return true;
3393
3394 default:
3395 return false;
3396 }
3397
3398 pp_string (pp, result);
3399 if (set_locus && t != NULL)
3400 *text->locus = location_of (t);
3401 return true;
3402 #undef next_tree
3403 #undef next_tcode
3404 #undef next_lang
3405 #undef next_int
3406 }
3407 \f
3408 /* Warn about the use of C++0x features when appropriate. */
3409 void
3410 maybe_warn_cpp0x (cpp0x_warn_str str)
3411 {
3412 if ((cxx_dialect == cxx98) && !in_system_header)
3413 /* We really want to suppress this warning in system headers,
3414 because libstdc++ uses variadic templates even when we aren't
3415 in C++0x mode. */
3416 switch (str)
3417 {
3418 case CPP0X_INITIALIZER_LISTS:
3419 pedwarn (input_location, 0,
3420 "extended initializer lists "
3421 "only available with -std=c++11 or -std=gnu++11");
3422 break;
3423 case CPP0X_EXPLICIT_CONVERSION:
3424 pedwarn (input_location, 0,
3425 "explicit conversion operators "
3426 "only available with -std=c++11 or -std=gnu++11");
3427 break;
3428 case CPP0X_VARIADIC_TEMPLATES:
3429 pedwarn (input_location, 0,
3430 "variadic templates "
3431 "only available with -std=c++11 or -std=gnu++11");
3432 break;
3433 case CPP0X_LAMBDA_EXPR:
3434 pedwarn (input_location, 0,
3435 "lambda expressions "
3436 "only available with -std=c++11 or -std=gnu++11");
3437 break;
3438 case CPP0X_AUTO:
3439 pedwarn (input_location, 0,
3440 "C++11 auto only available with -std=c++11 or -std=gnu++11");
3441 break;
3442 case CPP0X_SCOPED_ENUMS:
3443 pedwarn (input_location, 0,
3444 "scoped enums only available with -std=c++11 or -std=gnu++11");
3445 break;
3446 case CPP0X_DEFAULTED_DELETED:
3447 pedwarn (input_location, 0,
3448 "defaulted and deleted functions "
3449 "only available with -std=c++11 or -std=gnu++11");
3450 break;
3451 case CPP0X_INLINE_NAMESPACES:
3452 pedwarn (input_location, OPT_Wpedantic,
3453 "inline namespaces "
3454 "only available with -std=c++11 or -std=gnu++11");
3455 break;
3456 case CPP0X_OVERRIDE_CONTROLS:
3457 pedwarn (input_location, 0,
3458 "override controls (override/final) "
3459 "only available with -std=c++11 or -std=gnu++11");
3460 break;
3461 case CPP0X_NSDMI:
3462 pedwarn (input_location, 0,
3463 "non-static data member initializers "
3464 "only available with -std=c++11 or -std=gnu++11");
3465 break;
3466 case CPP0X_USER_DEFINED_LITERALS:
3467 pedwarn (input_location, 0,
3468 "user-defined literals "
3469 "only available with -std=c++11 or -std=gnu++11");
3470 break;
3471 case CPP0X_DELEGATING_CTORS:
3472 pedwarn (input_location, 0,
3473 "delegating constructors "
3474 "only available with -std=c++11 or -std=gnu++11");
3475 break;
3476 case CPP0X_INHERITING_CTORS:
3477 pedwarn (input_location, 0,
3478 "inheriting constructors "
3479 "only available with -std=c++11 or -std=gnu++11");
3480 break;
3481 case CPP0X_ATTRIBUTES:
3482 pedwarn (input_location, 0,
3483 "c++11 attributes "
3484 "only available with -std=c++11 or -std=gnu++11");
3485 break;
3486 case CPP0X_REF_QUALIFIER:
3487 pedwarn (input_location, 0,
3488 "ref-qualifiers "
3489 "only available with -std=c++11 or -std=gnu++11");
3490 break;
3491 default:
3492 gcc_unreachable ();
3493 }
3494 }
3495
3496 /* Warn about the use of variadic templates when appropriate. */
3497 void
3498 maybe_warn_variadic_templates (void)
3499 {
3500 maybe_warn_cpp0x (CPP0X_VARIADIC_TEMPLATES);
3501 }
3502
3503
3504 /* Issue an ISO C++98 pedantic warning at LOCATION, conditional on
3505 option OPT with text GMSGID. Use this function to report
3506 diagnostics for constructs that are invalid C++98, but valid
3507 C++0x. */
3508 bool
3509 pedwarn_cxx98 (location_t location, int opt, const char *gmsgid, ...)
3510 {
3511 diagnostic_info diagnostic;
3512 va_list ap;
3513 bool ret;
3514
3515 va_start (ap, gmsgid);
3516 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
3517 (cxx_dialect == cxx98) ? DK_PEDWARN : DK_WARNING);
3518 diagnostic.option_index = opt;
3519 ret = report_diagnostic (&diagnostic);
3520 va_end (ap);
3521 return ret;
3522 }
3523
3524 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is what
3525 we found when we tried to do the lookup. LOCATION is the location of
3526 the NAME identifier. */
3527
3528 void
3529 qualified_name_lookup_error (tree scope, tree name,
3530 tree decl, location_t location)
3531 {
3532 if (scope == error_mark_node)
3533 ; /* We already complained. */
3534 else if (TYPE_P (scope))
3535 {
3536 if (!COMPLETE_TYPE_P (scope))
3537 error_at (location, "incomplete type %qT used in nested name specifier",
3538 scope);
3539 else if (TREE_CODE (decl) == TREE_LIST)
3540 {
3541 error_at (location, "reference to %<%T::%D%> is ambiguous",
3542 scope, name);
3543 print_candidates (decl);
3544 }
3545 else
3546 error_at (location, "%qD is not a member of %qT", name, scope);
3547 }
3548 else if (scope != global_namespace)
3549 {
3550 error_at (location, "%qD is not a member of %qD", name, scope);
3551 suggest_alternatives_for (location, name);
3552 }
3553 else
3554 {
3555 error_at (location, "%<::%D%> has not been declared", name);
3556 suggest_alternatives_for (location, name);
3557 }
3558 }