]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/error.c
2003-07-01 Michael Koch <konqueror@gmx.de>
[gcc.git] / gcc / cp / error.c
CommitLineData
8d08fdba
MS
1/* Call-backs for C++ error reporting.
2 This code is non-reentrant.
d479d37f
NS
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
4 2003 Free Software Foundation, Inc.
f5adbb8d 5 This file is part of GCC.
8d08fdba 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
8d08fdba
MS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
f5adbb8d 18along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22#include "config.h"
8d052bc7 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
8d08fdba
MS
26#include "tree.h"
27#include "cp-tree.h"
a1bda5f1 28#include "real.h"
54f92bfb 29#include "toplev.h"
749ced52 30#include "flags.h"
cb753e49 31#include "diagnostic.h"
7cb32822 32#include "langhooks-def.h"
8d08fdba 33
a1066c99
GDR
34enum pad { none, before, after };
35
c3e76028
GDR
36#define sorry_for_unsupported_tree(T) \
37 sorry ("`%s' not supported by %s", tree_code_name[(int) TREE_CODE (T)], \
38 __FUNCTION__)
39
b34c06e3
GS
40#define print_scope_operator(BUFFER) output_add_string ((BUFFER), "::")
41#define print_left_paren(BUFFER) output_add_character ((BUFFER), '(')
42#define print_right_paren(BUFFER) output_add_character ((BUFFER), ')')
43#define print_left_bracket(BUFFER) output_add_character ((BUFFER), '[')
44#define print_right_bracket(BUFFER) output_add_character ((BUFFER), ']')
99885b3f 45#define print_template_argument_list_start(BUFFER) \
b34c06e3 46 print_non_consecutive_character ((BUFFER), '<')
99885b3f 47#define print_template_argument_list_end(BUFFER) \
b34c06e3 48 print_non_consecutive_character ((BUFFER), '>')
a1066c99 49#define print_tree_identifier(BUFFER, TID) \
b34c06e3
GS
50 output_add_string ((BUFFER), IDENTIFIER_POINTER (TID))
51#define print_identifier(BUFFER, ID) output_add_string ((BUFFER), (ID))
52#define separate_with_comma(BUFFER) output_add_string ((BUFFER), ", ")
a1066c99 53
99885b3f
GDR
54/* The global buffer where we dump everything. It is there only for
55 transitional purpose. It is expected, in the near future, to be
56 completely removed. */
57static output_buffer scratch_buffer_rec;
58static output_buffer *scratch_buffer = &scratch_buffer_rec;
0aafb128 59
b34c06e3 60# define NEXT_CODE(T) (TREE_CODE (TREE_TYPE (T)))
8d08fdba 61
99885b3f
GDR
62#define reinit_global_formatting_buffer() \
63 output_clear_message_text (scratch_buffer)
64
3f8548e7 65static const char *args_to_string (tree, int);
4e3f84b7
GDR
66static const char *assop_to_string (enum tree_code);
67static const char *code_to_string (enum tree_code);
3f8548e7
GDR
68static const char *cv_to_string (tree, int);
69static const char *decl_to_string (tree, int);
4e3f84b7 70static const char *expr_to_string (tree);
3f8548e7 71static const char *fndecl_to_string (tree, int);
4e3f84b7
GDR
72static const char *op_to_string (enum tree_code);
73static const char *parm_to_string (int);
3f8548e7
GDR
74static const char *type_to_string (tree, int);
75
76static void dump_type (tree, int);
77static void dump_typename (tree, int);
78static void dump_simple_decl (tree, tree, int);
79static void dump_decl (tree, int);
80static void dump_template_decl (tree, int);
81static void dump_function_decl (tree, int);
82static void dump_expr (tree, int);
83static void dump_unary_op (const char *, tree, int);
84static void dump_binary_op (const char *, tree, int);
85static void dump_aggr_type (tree, int);
86static enum pad dump_type_prefix (tree, int);
87static void dump_type_suffix (tree, int);
88static void dump_function_name (tree, int);
89static void dump_expr_list (tree, int);
90static void dump_global_iord (tree);
91static enum pad dump_qualifiers (tree, enum pad);
92static void dump_char (int);
93static void dump_parameters (tree, int);
94static void dump_exception_spec (tree, int);
95static const char *class_key_or_enum (tree);
96static void dump_template_argument (tree, int);
97static void dump_template_argument_list (tree, int);
98static void dump_template_parameter (tree, int);
99static void dump_template_bindings (tree, tree);
100static void dump_scope (tree, int);
101static void dump_template_parms (tree, int, int);
102
103static const char *function_category (tree);
104static void maybe_print_instantiation_context (diagnostic_context *);
105static void print_instantiation_full_context (diagnostic_context *);
106static void print_instantiation_partial_context (diagnostic_context *,
35773471 107 tree, location_t);
3f8548e7
GDR
108static void cp_diagnostic_starter (diagnostic_context *, diagnostic_info *);
109static void cp_diagnostic_finalizer (diagnostic_context *, diagnostic_info *);
110static void cp_print_error_function (diagnostic_context *, diagnostic_info *);
111
112static bool cp_printer (output_buffer *, text_info *);
113static void print_non_consecutive_character (output_buffer *, int);
3f8548e7 114static tree locate_error (const char *, va_list);
9a472a42 115static location_t location_of (tree);
8d08fdba
MS
116
117void
3f8548e7 118init_error (void)
8d08fdba 119{
f68fc4db
GDR
120 diagnostic_starter (global_dc) = cp_diagnostic_starter;
121 diagnostic_finalizer (global_dc) = cp_diagnostic_finalizer;
749ced52
ZW
122 diagnostic_format_decoder (global_dc) = cp_printer;
123
f68fc4db 124 init_output_buffer (scratch_buffer, /* prefix */NULL, /* line-width */0);
8d08fdba
MS
125}
126
9e93bc9d 127/* Dump a scope, if deemed necessary. */
bbcec105 128
9e93bc9d 129static void
3f8548e7 130dump_scope (tree scope, int flags)
bbcec105 131{
55ace93c 132 int f = ~TFF_RETURN_TYPE & (flags & (TFF_SCOPE | TFF_CHASE_TYPEDEF));
761f0855 133
9e93bc9d
NS
134 if (scope == NULL_TREE)
135 return;
bb20cc46 136
9e93bc9d
NS
137 if (TREE_CODE (scope) == NAMESPACE_DECL)
138 {
139 if (scope != global_namespace)
140 {
761f0855 141 dump_decl (scope, f);
99885b3f 142 print_scope_operator (scratch_buffer);
9e93bc9d 143 }
9e93bc9d
NS
144 }
145 else if (AGGREGATE_TYPE_P (scope))
146 {
761f0855 147 dump_type (scope, f);
99885b3f 148 print_scope_operator (scratch_buffer);
9e93bc9d 149 }
761f0855 150 else if ((flags & TFF_SCOPE) && TREE_CODE (scope) == FUNCTION_DECL)
9e93bc9d 151 {
761f0855 152 dump_function_decl (scope, f);
99885b3f 153 print_scope_operator (scratch_buffer);
9e93bc9d 154 }
bbcec105
JM
155}
156
9e93bc9d
NS
157/* Dump type qualifiers, providing padding as requested. Return an
158 indication of whether we dumped something. */
159
160static enum pad
3f8548e7 161dump_qualifiers (tree t, enum pad p)
8d08fdba 162{
9e93bc9d
NS
163 static const int masks[] =
164 {TYPE_QUAL_CONST, TYPE_QUAL_VOLATILE, TYPE_QUAL_RESTRICT};
165 static const char *const names[] =
166 {"const", "volatile", "__restrict"};
167 int ix;
168 int quals = TYPE_QUALS (t);
169 int do_after = p == after;
bb20cc46 170
9e93bc9d 171 if (quals)
8d08fdba 172 {
9e93bc9d
NS
173 for (ix = 0; ix != 3; ix++)
174 if (masks[ix] & quals)
175 {
176 if (p == before)
99885b3f 177 output_add_space (scratch_buffer);
9e93bc9d 178 p = before;
99885b3f 179 print_identifier (scratch_buffer, names[ix]);
9e93bc9d
NS
180 }
181 if (do_after)
99885b3f 182 output_add_space (scratch_buffer);
8d08fdba 183 }
9e93bc9d
NS
184 else
185 p = none;
186 return p;
8d08fdba
MS
187}
188
612c671a 189/* Dump the template ARGument under control of FLAGS. */
5f77d6cc
GDR
190
191static void
3f8548e7 192dump_template_argument (tree arg, int flags)
5f77d6cc 193{
2f939d94 194 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
761f0855 195 dump_type (arg, flags & ~TFF_CLASS_KEY_OR_ENUM);
5f77d6cc 196 else
761f0855 197 dump_expr (arg, (flags | TFF_EXPR_IN_PARENS) & ~TFF_CLASS_KEY_OR_ENUM);
5f77d6cc
GDR
198}
199
612c671a
GDR
200/* Dump a template-argument-list ARGS (always a TREE_VEC) under control
201 of FLAGS. */
202
203static void
3f8548e7 204dump_template_argument_list (tree args, int flags)
612c671a
GDR
205{
206 int n = TREE_VEC_LENGTH (args);
207 int need_comma = 0;
208 int i;
209
210 for (i = 0; i< n; ++i)
211 {
212 if (need_comma)
99885b3f 213 separate_with_comma (scratch_buffer);
612c671a
GDR
214 dump_template_argument (TREE_VEC_ELT (args, i), flags);
215 need_comma = 1;
216 }
217}
218
219/* Dump a template parameter PARM (a TREE_LIST) under control of FLAGS. */
220
221static void
3f8548e7 222dump_template_parameter (tree parm, int flags)
612c671a
GDR
223{
224 tree p = TREE_VALUE (parm);
225 tree a = TREE_PURPOSE (parm);
226
227 if (TREE_CODE (p) == TYPE_DECL)
228 {
761f0855 229 if (flags & TFF_DECL_SPECIFIERS)
612c671a 230 {
99885b3f 231 print_identifier (scratch_buffer, "class");
612c671a
GDR
232 if (DECL_NAME (p))
233 {
99885b3f
GDR
234 output_add_space (scratch_buffer);
235 print_tree_identifier (scratch_buffer, DECL_NAME (p));
612c671a
GDR
236 }
237 }
238 else if (DECL_NAME (p))
99885b3f 239 print_tree_identifier (scratch_buffer, DECL_NAME (p));
612c671a 240 else
99885b3f 241 print_identifier (scratch_buffer, "{template default argument error}");
612c671a
GDR
242 }
243 else
761f0855 244 dump_decl (p, flags | TFF_DECL_SPECIFIERS);
612c671a 245
761f0855 246 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && a != NULL_TREE)
612c671a 247 {
99885b3f 248 output_add_string (scratch_buffer, " = ");
31bb3027 249 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
761f0855 250 dump_type (a, flags & ~TFF_CHASE_TYPEDEF);
612c671a 251 else
761f0855 252 dump_expr (a, flags | TFF_EXPR_IN_PARENS);
612c671a
GDR
253 }
254}
255
256/* Dump, under control of FLAGS, a template-parameter-list binding.
257 PARMS is a TREE_LIST of TREE_VEC of TREE_LIST and ARGS is a
258 TREE_VEC. */
259
260static void
3f8548e7 261dump_template_bindings (tree parms, tree args)
612c671a 262{
612c671a
GDR
263 int need_comma = 0;
264
265 while (parms)
266 {
267 tree p = TREE_VALUE (parms);
b5ac18ea
MM
268 int lvl = TMPL_PARMS_DEPTH (parms);
269 int arg_idx = 0;
612c671a
GDR
270 int i;
271
272 for (i = 0; i < TREE_VEC_LENGTH (p); ++i)
b5ac18ea 273 {
2bb5d995
JM
274 tree arg = NULL_TREE;
275
276 /* Don't crash if we had an invalid argument list. */
277 if (TMPL_ARGS_DEPTH (args) >= lvl)
278 {
279 tree lvl_args = TMPL_ARGS_LEVEL (args, lvl);
280 if (NUM_TMPL_ARGS (lvl_args) > arg_idx)
281 arg = TREE_VEC_ELT (lvl_args, arg_idx);
282 }
b5ac18ea
MM
283
284 if (need_comma)
99885b3f 285 separate_with_comma (scratch_buffer);
761f0855 286 dump_template_parameter (TREE_VEC_ELT (p, i), TFF_PLAIN_IDENTIFIER);
99885b3f 287 output_add_string (scratch_buffer, " = ");
b5ac18ea 288 if (arg)
761f0855 289 dump_template_argument (arg, TFF_PLAIN_IDENTIFIER);
b5ac18ea 290 else
99885b3f 291 print_identifier (scratch_buffer, "<missing>");
bb20cc46 292
b5ac18ea
MM
293 ++arg_idx;
294 need_comma = 1;
295 }
612c671a
GDR
296
297 parms = TREE_CHAIN (parms);
298 }
299}
300
b3426eb9
GK
301/* Dump a human-readable equivalent of TYPE. FLAGS controls the
302 format. */
e92cc029 303
8d08fdba 304static void
3f8548e7 305dump_type (tree t, int flags)
8d08fdba
MS
306{
307 if (t == NULL_TREE)
308 return;
bb20cc46 309
8d08fdba
MS
310 if (TYPE_PTRMEMFUNC_P (t))
311 goto offset_type;
312
313 switch (TREE_CODE (t))
314 {
8d08fdba 315 case UNKNOWN_TYPE:
99885b3f 316 print_identifier (scratch_buffer, "<unknown type>");
8d08fdba
MS
317 break;
318
dcd08efc
JM
319 case TREE_LIST:
320 /* A list of function parms. */
9e93bc9d 321 dump_parameters (t, flags);
dcd08efc
JM
322 break;
323
8d08fdba 324 case IDENTIFIER_NODE:
99885b3f 325 print_tree_identifier (scratch_buffer, t);
8d08fdba
MS
326 break;
327
328 case TREE_VEC:
9e93bc9d 329 dump_type (BINFO_TYPE (t), flags);
8d08fdba
MS
330 break;
331
332 case RECORD_TYPE:
333 case UNION_TYPE:
334 case ENUMERAL_TYPE:
9e93bc9d 335 dump_aggr_type (t, flags);
8d08fdba
MS
336 break;
337
338 case TYPE_DECL:
761f0855 339 if (flags & TFF_CHASE_TYPEDEF)
9e93bc9d
NS
340 {
341 dump_type (DECL_ORIGINAL_TYPE (t)
342 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t), flags);
343 break;
344 }
345 /* else fallthrough */
bb20cc46 346
67c2a928 347 case TEMPLATE_DECL:
009425e1 348 case NAMESPACE_DECL:
761f0855 349 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
8d08fdba 350 break;
bb20cc46 351
37c46b43 352 case COMPLEX_TYPE:
99885b3f 353 output_add_string (scratch_buffer, "__complex__ ");
9e93bc9d 354 dump_type (TREE_TYPE (t), flags);
37c46b43
MS
355 break;
356
c00996a3 357 case VECTOR_TYPE:
4e3f84b7 358 output_add_string (scratch_buffer, "__vector__ ");
d8a6f584
JM
359 {
360 /* The subtype of a VECTOR_TYPE is something like intQI_type_node,
361 which has no name and is not very useful for diagnostics. So
362 look up the equivalent C type and print its name. */
363 tree elt = TREE_TYPE (t);
b0c48229 364 elt = c_common_type_for_mode (TYPE_MODE (elt), TREE_UNSIGNED (elt));
d8a6f584
JM
365 dump_type (elt, flags);
366 }
c00996a3
JM
367 break;
368
8d08fdba
MS
369 case INTEGER_TYPE:
370 if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && TREE_UNSIGNED (t))
99885b3f 371 output_add_string (scratch_buffer, "unsigned ");
8d08fdba 372 else if (TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && !TREE_UNSIGNED (t))
99885b3f 373 output_add_string (scratch_buffer, "signed ");
8d08fdba
MS
374
375 /* fall through. */
376 case REAL_TYPE:
377 case VOID_TYPE:
2986ae00 378 case BOOLEAN_TYPE:
3df095e2
MM
379 {
380 tree type;
91063b51 381 dump_qualifiers (t, after);
761f0855 382 type = flags & TFF_CHASE_TYPEDEF ? TYPE_MAIN_VARIANT (t) : t;
3df095e2 383 if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
99885b3f 384 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (type));
3df095e2
MM
385 else
386 /* Types like intQI_type_node and friends have no names.
387 These don't come up in user error messages, but it's nice
388 to be able to print them from the debugger. */
99885b3f 389 print_identifier (scratch_buffer, "<anonymous>");
3df095e2 390 }
8d08fdba
MS
391 break;
392
73b0fce8 393 case TEMPLATE_TEMPLATE_PARM:
c6002625 394 /* For parameters inside template signature. */
a1281f45 395 if (TYPE_IDENTIFIER (t))
99885b3f 396 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
73b0fce8 397 else
99885b3f
GDR
398 print_identifier
399 (scratch_buffer, "<anonymous template template parameter>");
a1281f45
KL
400 break;
401
402 case BOUND_TEMPLATE_TEMPLATE_PARM:
403 {
404 tree args = TYPE_TI_ARGS (t);
99885b3f
GDR
405 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
406 print_template_argument_list_start (scratch_buffer);
a1281f45 407 dump_template_argument_list (args, flags);
99885b3f 408 print_template_argument_list_end (scratch_buffer);
a1281f45 409 }
73b0fce8
KL
410 break;
411
8d08fdba 412 case TEMPLATE_TYPE_PARM:
91063b51 413 dump_qualifiers (t, after);
ec255269 414 if (TYPE_IDENTIFIER (t))
99885b3f 415 print_tree_identifier (scratch_buffer, TYPE_IDENTIFIER (t));
ec255269 416 else
99885b3f
GDR
417 print_identifier
418 (scratch_buffer, "<anonymous template type parameter>");
8d08fdba
MS
419 break;
420
8d08fdba
MS
421 /* This is not always necessary for pointers and such, but doing this
422 reduces code size. */
423 case ARRAY_TYPE:
424 case POINTER_TYPE:
425 case REFERENCE_TYPE:
426 case OFFSET_TYPE:
427 offset_type:
428 case FUNCTION_TYPE:
429 case METHOD_TYPE:
9e93bc9d
NS
430 {
431 dump_type_prefix (t, flags);
432 dump_type_suffix (t, flags);
8d08fdba 433 break;
9e93bc9d 434 }
5566b478 435 case TYPENAME_TYPE:
14d22dd6 436 dump_qualifiers (t, after);
8fbc5ae7 437 output_add_string (scratch_buffer, "typename ");
46e2747c 438 dump_typename (t, flags);
5566b478
MS
439 break;
440
b8c6534b
KL
441 case UNBOUND_CLASS_TEMPLATE:
442 dump_type (TYPE_CONTEXT (t), flags);
443 print_scope_operator (scratch_buffer);
444 print_identifier (scratch_buffer, "template ");
445 dump_type (DECL_NAME (TYPE_NAME (t)), flags);
446 break;
447
b894fc05 448 case TYPEOF_TYPE:
99885b3f 449 output_add_string (scratch_buffer, "__typeof (");
761f0855 450 dump_expr (TYPE_FIELDS (t), flags & ~TFF_EXPR_IN_PARENS);
4e664ef1 451 print_right_paren (scratch_buffer);
b894fc05
JM
452 break;
453
8d08fdba 454 default:
99885b3f 455 sorry_for_unsupported_tree (t);
c6002625 456 /* Fall through to error. */
9e93bc9d
NS
457
458 case ERROR_MARK:
99885b3f 459 print_identifier (scratch_buffer, "<type error>");
9e93bc9d 460 break;
8d08fdba
MS
461 }
462}
463
46e2747c
NS
464/* Dump a TYPENAME_TYPE. We need to notice when the context is itself
465 a TYPENAME_TYPE. */
466
467static void
3f8548e7 468dump_typename (tree t, int flags)
46e2747c
NS
469{
470 tree ctx = TYPE_CONTEXT (t);
bb20cc46 471
46e2747c
NS
472 if (TREE_CODE (ctx) == TYPENAME_TYPE)
473 dump_typename (ctx, flags);
474 else
761f0855 475 dump_type (ctx, flags & ~TFF_CLASS_KEY_OR_ENUM);
99885b3f 476 print_scope_operator (scratch_buffer);
46e2747c
NS
477 dump_decl (TYPENAME_TYPE_FULLNAME (t), flags);
478}
479
9e93bc9d
NS
480/* Return the name of the supplied aggregate, or enumeral type. */
481
9c0758dd 482static const char *
3f8548e7 483class_key_or_enum (tree t)
8d08fdba 484{
8d08fdba 485 if (TREE_CODE (t) == ENUMERAL_TYPE)
51c184be 486 return "enum";
8d08fdba 487 else if (TREE_CODE (t) == UNION_TYPE)
51c184be 488 return "union";
8d08fdba 489 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
51c184be 490 return "class";
8d08fdba 491 else
51c184be
MS
492 return "struct";
493}
494
9e93bc9d
NS
495/* Print out a class declaration T under the control of FLAGS,
496 in the form `class foo'. */
e92cc029 497
51c184be 498static void
3f8548e7 499dump_aggr_type (tree t, int flags)
51c184be
MS
500{
501 tree name;
c3e76028 502 const char *variety = class_key_or_enum (t);
9e93bc9d
NS
503 int typdef = 0;
504 int tmplate = 0;
8d08fdba 505
91063b51 506 dump_qualifiers (t, after);
8d08fdba 507
761f0855 508 if (flags & TFF_CLASS_KEY_OR_ENUM)
8d08fdba 509 {
99885b3f
GDR
510 print_identifier (scratch_buffer, variety);
511 output_add_space (scratch_buffer);
8d08fdba 512 }
bb20cc46 513
761f0855 514 if (flags & TFF_CHASE_TYPEDEF)
9e93bc9d 515 t = TYPE_MAIN_VARIANT (t);
8d08fdba 516
9e93bc9d
NS
517 name = TYPE_NAME (t);
518
519 if (name)
8d08fdba 520 {
9e93bc9d
NS
521 typdef = !DECL_ARTIFICIAL (name);
522 tmplate = !typdef && TREE_CODE (t) != ENUMERAL_TYPE
523 && TYPE_LANG_SPECIFIC (t) && CLASSTYPE_TEMPLATE_INFO (t)
370af2d5 524 && (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
9e93bc9d
NS
525 || TREE_CODE (CLASSTYPE_TI_TEMPLATE (t)) != TEMPLATE_DECL
526 || DECL_TEMPLATE_SPECIALIZATION (CLASSTYPE_TI_TEMPLATE (t))
527 || PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
761f0855 528 dump_scope (CP_DECL_CONTEXT (name), flags | TFF_SCOPE);
9e93bc9d
NS
529 if (tmplate)
530 {
531 /* Because the template names are mangled, we have to locate
532 the most general template, and use that name. */
533 tree tpl = CLASSTYPE_TI_TEMPLATE (t);
bb20cc46 534
9e93bc9d
NS
535 while (DECL_TEMPLATE_INFO (tpl))
536 tpl = DECL_TI_TEMPLATE (tpl);
537 name = tpl;
538 }
539 name = DECL_NAME (name);
8d08fdba
MS
540 }
541
f30432d7 542 if (name == 0 || ANON_AGGRNAME_P (name))
8d08fdba 543 {
761f0855 544 if (flags & TFF_CLASS_KEY_OR_ENUM)
99885b3f
GDR
545 print_identifier (scratch_buffer, "<anonymous>");
546 else
547 output_printf (scratch_buffer, "<anonymous %s>", variety);
8d08fdba
MS
548 }
549 else
99885b3f 550 print_tree_identifier (scratch_buffer, name);
9e93bc9d
NS
551 if (tmplate)
552 dump_template_parms (TYPE_TEMPLATE_INFO (t),
553 !CLASSTYPE_USE_TEMPLATE (t),
761f0855 554 flags & ~TFF_TEMPLATE_HEADER);
8d08fdba
MS
555}
556
557/* Dump into the obstack the initial part of the output for a given type.
558 This is necessary when dealing with things like functions returning
559 functions. Examples:
560
561 return type of `int (* fee ())()': pointer -> function -> int. Both
562 pointer (and reference and offset) and function (and member) types must
563 deal with prefix and suffix.
564
565 Arrays must also do this for DECL nodes, like int a[], and for things like
bb20cc46
AJ
566 int *[]&.
567
9e93bc9d
NS
568 Return indicates how you should pad an object name after this. I.e. you
569 want to pad non-*, non-& cores, but not pad * or & types. */
8d08fdba 570
9e93bc9d 571static enum pad
3f8548e7 572dump_type_prefix (tree t, int flags)
8d08fdba 573{
9e93bc9d 574 enum pad padding = before;
bb20cc46 575
8d08fdba
MS
576 if (TYPE_PTRMEMFUNC_P (t))
577 {
578 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
579 goto offset_type;
580 }
bb20cc46 581
8d08fdba
MS
582 switch (TREE_CODE (t))
583 {
584 case POINTER_TYPE:
91063b51 585 case REFERENCE_TYPE:
8d08fdba
MS
586 {
587 tree sub = TREE_TYPE (t);
bb20cc46 588
9e93bc9d 589 padding = dump_type_prefix (sub, flags);
8d08fdba
MS
590 /* A tree for a member pointer looks like pointer to offset,
591 so let the OFFSET_TYPE case handle it. */
91063b51 592 if (!TYPE_PTRMEM_P (t))
8d08fdba 593 {
9e93bc9d 594 if (TREE_CODE (sub) == ARRAY_TYPE)
5f9cd837
GDR
595 {
596 output_add_space (scratch_buffer);
597 print_left_paren (scratch_buffer);
598 }
99885b3f
GDR
599 output_add_character
600 (scratch_buffer, "&*"[TREE_CODE (t) == POINTER_TYPE]);
5f9cd837 601 padding = dump_qualifiers (t, before);
8d08fdba
MS
602 }
603 }
8d08fdba
MS
604 break;
605
606 case OFFSET_TYPE:
607 offset_type:
9e93bc9d 608 padding = dump_type_prefix (TREE_TYPE (t), flags);
51c184be
MS
609 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
610 {
9e93bc9d 611 if (padding != none)
99885b3f 612 output_add_space (scratch_buffer);
9e93bc9d 613 dump_type (TYPE_OFFSET_BASETYPE (t), flags);
99885b3f 614 print_scope_operator (scratch_buffer);
51c184be 615 }
99885b3f 616 output_add_character (scratch_buffer, '*');
9e93bc9d 617 padding = dump_qualifiers (t, none);
8d08fdba
MS
618 break;
619
620 /* Can only be reached through function pointer -- this would not be
621 correct if FUNCTION_DECLs used it. */
622 case FUNCTION_TYPE:
9e93bc9d
NS
623 padding = dump_type_prefix (TREE_TYPE (t), flags);
624 if (padding != none)
99885b3f
GDR
625 output_add_space (scratch_buffer);
626 print_left_paren (scratch_buffer);
9e93bc9d 627 padding = none;
8d08fdba
MS
628 break;
629
630 case METHOD_TYPE:
9e93bc9d
NS
631 padding = dump_type_prefix (TREE_TYPE (t), flags);
632 if (padding != none)
99885b3f
GDR
633 output_add_space (scratch_buffer);
634 print_left_paren (scratch_buffer);
9e93bc9d
NS
635 padding = none;
636 dump_aggr_type (TYPE_METHOD_BASETYPE (t), flags);
99885b3f 637 print_scope_operator (scratch_buffer);
8d08fdba
MS
638 break;
639
640 case ARRAY_TYPE:
9e93bc9d 641 padding = dump_type_prefix (TREE_TYPE (t), flags);
8d08fdba
MS
642 break;
643
644 case ENUMERAL_TYPE:
8d08fdba
MS
645 case IDENTIFIER_NODE:
646 case INTEGER_TYPE:
2986ae00 647 case BOOLEAN_TYPE:
8d08fdba
MS
648 case REAL_TYPE:
649 case RECORD_TYPE:
650 case TEMPLATE_TYPE_PARM:
73b0fce8 651 case TEMPLATE_TEMPLATE_PARM:
a1281f45 652 case BOUND_TEMPLATE_TEMPLATE_PARM:
8d08fdba
MS
653 case TREE_LIST:
654 case TYPE_DECL:
655 case TREE_VEC:
8d08fdba
MS
656 case UNION_TYPE:
657 case UNKNOWN_TYPE:
658 case VOID_TYPE:
5566b478 659 case TYPENAME_TYPE:
37c46b43 660 case COMPLEX_TYPE:
c00996a3 661 case VECTOR_TYPE:
0df4ae96 662 case TYPEOF_TYPE:
9e93bc9d
NS
663 dump_type (t, flags);
664 padding = before;
8d08fdba 665 break;
bb20cc46 666
8d08fdba 667 default:
99885b3f
GDR
668 sorry_for_unsupported_tree (t);
669 /* fall through. */
9e93bc9d 670 case ERROR_MARK:
99885b3f 671 print_identifier (scratch_buffer, "<typeprefixerror>");
9e93bc9d 672 break;
8d08fdba 673 }
9e93bc9d 674 return padding;
8d08fdba
MS
675}
676
9e93bc9d
NS
677/* Dump the suffix of type T, under control of FLAGS. This is the part
678 which appears after the identifier (or function parms). */
679
8d08fdba 680static void
3f8548e7 681dump_type_suffix (tree t, int flags)
8d08fdba
MS
682{
683 if (TYPE_PTRMEMFUNC_P (t))
684 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
685
686 switch (TREE_CODE (t))
687 {
688 case POINTER_TYPE:
689 case REFERENCE_TYPE:
690 case OFFSET_TYPE:
39211cd5 691 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
99885b3f 692 print_right_paren (scratch_buffer);
9e93bc9d 693 dump_type_suffix (TREE_TYPE (t), flags);
8d08fdba
MS
694 break;
695
696 /* Can only be reached through function pointer */
697 case FUNCTION_TYPE:
698 case METHOD_TYPE:
699 {
700 tree arg;
99885b3f 701 print_right_paren (scratch_buffer);
8d08fdba
MS
702 arg = TYPE_ARG_TYPES (t);
703 if (TREE_CODE (t) == METHOD_TYPE)
704 arg = TREE_CHAIN (arg);
705
4995028c
NS
706 /* Function pointers don't have default args. Not in standard C++,
707 anyway; they may in g++, but we'll just pretend otherwise. */
761f0855 708 dump_parameters (arg, flags & ~TFF_FUNCTION_DEFAULT_ARGUMENTS);
4995028c 709
8d08fdba 710 if (TREE_CODE (t) == METHOD_TYPE)
91063b51 711 dump_qualifiers
8d08fdba 712 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), before);
9e93bc9d 713 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (t), flags);
ad6b1795 714 dump_type_suffix (TREE_TYPE (t), flags);
8d08fdba
MS
715 break;
716 }
717
718 case ARRAY_TYPE:
99885b3f 719 print_left_bracket (scratch_buffer);
8d08fdba 720 if (TYPE_DOMAIN (t))
5156628f 721 {
665f2503 722 if (host_integerp (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0))
748f6cb5 723 output_host_wide_integer
99885b3f
GDR
724 (scratch_buffer,
725 tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0) + 1);
5156628f 726 else if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (t))) == MINUS_EXPR)
9e93bc9d 727 dump_expr (TREE_OPERAND (TYPE_MAX_VALUE (TYPE_DOMAIN (t)), 0),
761f0855 728 flags & ~TFF_EXPR_IN_PARENS);
5156628f 729 else
ab76ca54 730 dump_expr (fold (cp_build_binary_op
5156628f 731 (PLUS_EXPR, TYPE_MAX_VALUE (TYPE_DOMAIN (t)),
9e93bc9d 732 integer_one_node)),
761f0855 733 flags & ~TFF_EXPR_IN_PARENS);
5156628f 734 }
99885b3f 735 print_right_bracket (scratch_buffer);
9e93bc9d 736 dump_type_suffix (TREE_TYPE (t), flags);
8d08fdba 737 break;
bb20cc46 738
8d08fdba 739 case ENUMERAL_TYPE:
8d08fdba
MS
740 case IDENTIFIER_NODE:
741 case INTEGER_TYPE:
2986ae00 742 case BOOLEAN_TYPE:
8d08fdba
MS
743 case REAL_TYPE:
744 case RECORD_TYPE:
745 case TEMPLATE_TYPE_PARM:
73b0fce8 746 case TEMPLATE_TEMPLATE_PARM:
a97d0689 747 case BOUND_TEMPLATE_TEMPLATE_PARM:
8d08fdba
MS
748 case TREE_LIST:
749 case TYPE_DECL:
750 case TREE_VEC:
8d08fdba
MS
751 case UNION_TYPE:
752 case UNKNOWN_TYPE:
753 case VOID_TYPE:
5566b478 754 case TYPENAME_TYPE:
37c46b43 755 case COMPLEX_TYPE:
c00996a3 756 case VECTOR_TYPE:
0df4ae96 757 case TYPEOF_TYPE:
8d08fdba
MS
758 break;
759
760 default:
99885b3f 761 sorry_for_unsupported_tree (t);
9e93bc9d
NS
762 case ERROR_MARK:
763 /* Don't mark it here, we should have already done in
764 dump_type_prefix. */
765 break;
8d08fdba
MS
766 }
767}
768
49c249e1 769static void
3f8548e7 770dump_global_iord (tree t)
8d08fdba 771{
99885b3f 772 const char *p = NULL;
8d08fdba 773
92643fea 774 if (DECL_GLOBAL_CTOR_P (t))
99885b3f 775 p = "initializers";
92643fea 776 else if (DECL_GLOBAL_DTOR_P (t))
99885b3f 777 p = "destructors";
8d08fdba 778 else
a98facb0 779 abort ();
bb20cc46 780
99885b3f 781 output_printf (scratch_buffer, "(static %s for %s)", p, input_filename);
8d08fdba
MS
782}
783
07389efe 784static void
3f8548e7 785dump_simple_decl (tree t, tree type, int flags)
07389efe 786{
761f0855 787 if (flags & TFF_DECL_SPECIFIERS)
07389efe 788 {
9e93bc9d 789 if (dump_type_prefix (type, flags) != none)
99885b3f 790 output_add_space (scratch_buffer);
07389efe 791 }
9e93bc9d
NS
792 if (!DECL_INITIAL (t) || TREE_CODE (DECL_INITIAL (t)) != TEMPLATE_PARM_INDEX)
793 dump_scope (CP_DECL_CONTEXT (t), flags);
07389efe 794 if (DECL_NAME (t))
9e93bc9d 795 dump_decl (DECL_NAME (t), flags);
07389efe 796 else
99885b3f 797 print_identifier (scratch_buffer, "<anonymous>");
761f0855 798 if (flags & TFF_DECL_SPECIFIERS)
9e93bc9d 799 dump_type_suffix (type, flags);
07389efe
MM
800}
801
9e93bc9d
NS
802/* Dump a human readable string for the decl T under control of FLAGS. */
803
8d08fdba 804static void
3f8548e7 805dump_decl (tree t, int flags)
8d08fdba
MS
806{
807 if (t == NULL_TREE)
808 return;
809
810 switch (TREE_CODE (t))
811 {
7177d104 812 case TYPE_DECL:
8d2733ca
MS
813 {
814 /* Don't say 'typedef class A' */
fc378698 815 if (DECL_ARTIFICIAL (t))
8d2733ca 816 {
761f0855 817 if ((flags & TFF_DECL_SPECIFIERS)
9e93bc9d 818 && TREE_CODE (TREE_TYPE (t)) == TEMPLATE_TYPE_PARM)
c6002625 819 /* Say `class T' not just `T'. */
99885b3f 820 output_add_string (scratch_buffer, "class ");
a9936d39 821
9e93bc9d 822 dump_type (TREE_TYPE (t), flags);
8d2733ca
MS
823 break;
824 }
825 }
761f0855 826 if (flags & TFF_DECL_SPECIFIERS)
99885b3f 827 output_add_string (scratch_buffer, "typedef ");
bb20cc46 828 dump_simple_decl (t, DECL_ORIGINAL_TYPE (t)
9e93bc9d
NS
829 ? DECL_ORIGINAL_TYPE (t) : TREE_TYPE (t),
830 flags);
7177d104 831 break;
bb20cc46 832
8d08fdba 833 case VAR_DECL:
b7484fbe 834 if (DECL_NAME (t) && VTABLE_NAME_P (DECL_NAME (t)))
8d08fdba 835 {
99885b3f 836 output_add_string (scratch_buffer, "vtable for ");
c4372ef4
NS
837 my_friendly_assert (TYPE_P (DECL_CONTEXT (t)), 20010720);
838 dump_type (DECL_CONTEXT (t), flags);
8d08fdba
MS
839 break;
840 }
841 /* else fall through */
842 case FIELD_DECL:
843 case PARM_DECL:
9e93bc9d 844 dump_simple_decl (t, TREE_TYPE (t), flags);
8d08fdba
MS
845 break;
846
f6a898ba 847 case RESULT_DECL:
99885b3f 848 output_add_string (scratch_buffer, "<return value> ");
f6a898ba
AO
849 dump_simple_decl (t, TREE_TYPE (t), flags);
850 break;
851
a9aedbc2 852 case NAMESPACE_DECL:
9e93bc9d 853 dump_scope (CP_DECL_CONTEXT (t), flags);
0c8feefe 854 if (DECL_NAME (t) == anonymous_namespace_name)
99885b3f 855 print_identifier (scratch_buffer, "<unnamed>");
0c8feefe 856 else
99885b3f 857 print_tree_identifier (scratch_buffer, DECL_NAME (t));
a9aedbc2
MS
858 break;
859
3e3f722c 860 case SCOPE_REF:
761f0855 861 dump_decl (TREE_OPERAND (t, 0), flags & ~TFF_DECL_SPECIFIERS);
99885b3f 862 print_scope_operator (scratch_buffer);
9e93bc9d 863 dump_decl (TREE_OPERAND (t, 1), flags);
bb20cc46 864 break;
3e3f722c 865
8d08fdba 866 case ARRAY_REF:
9e93bc9d 867 dump_decl (TREE_OPERAND (t, 0), flags);
99885b3f 868 print_left_bracket (scratch_buffer);
9e93bc9d 869 dump_decl (TREE_OPERAND (t, 1), flags);
99885b3f 870 print_right_bracket (scratch_buffer);
8d08fdba
MS
871 break;
872
9e93bc9d 873 /* So that we can do dump_decl on an aggr type. */
8d08fdba
MS
874 case RECORD_TYPE:
875 case UNION_TYPE:
876 case ENUMERAL_TYPE:
9e93bc9d 877 dump_type (t, flags);
8d08fdba
MS
878 break;
879
8d08fdba 880 case TYPE_EXPR:
a98facb0 881 abort ();
8d08fdba
MS
882 break;
883
884 /* These special cases are duplicated here so that other functions
33bd39a2 885 can feed identifiers to error and get them demangled properly. */
8d08fdba 886 case IDENTIFIER_NODE:
dc5c569a
JJ
887 if (IDENTIFIER_TYPENAME_P (t))
888 {
889 output_add_string (scratch_buffer, "operator ");
890 /* Not exactly IDENTIFIER_TYPE_VALUE. */
891 dump_type (TREE_TYPE (t), flags);
892 break;
893 }
894 else
895 print_tree_identifier (scratch_buffer, t);
8d08fdba
MS
896 break;
897
8f032717 898 case OVERLOAD:
65a5559b
MM
899 if (OVL_CHAIN (t))
900 {
901 t = OVL_CURRENT (t);
902 if (DECL_CLASS_SCOPE_P (t))
903 {
904 dump_type (DECL_CONTEXT (t), flags);
905 output_add_string (scratch_buffer, "::");
906 }
907 else if (DECL_CONTEXT (t))
908 {
909 dump_decl (DECL_CONTEXT (t), flags);
910 output_add_string (scratch_buffer, "::");
911 }
912 dump_decl (DECL_NAME (t), flags);
913 break;
914 }
915
916 /* If there's only one function, just treat it like an ordinary
917 FUNCTION_DECL. */
8f032717
MM
918 t = OVL_CURRENT (t);
919 /* Fall through. */
920
8d08fdba 921 case FUNCTION_DECL:
92643fea
MM
922 if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
923 dump_global_iord (t);
da20811c 924 else if (! DECL_LANG_SPECIFIC (t))
99885b3f 925 print_identifier (scratch_buffer, "<internal>");
8d08fdba 926 else
9e93bc9d 927 dump_function_decl (t, flags);
8d08fdba
MS
928 break;
929
930 case TEMPLATE_DECL:
38066e83 931 dump_template_decl (t, flags);
8d08fdba
MS
932 break;
933
74cd8397
JM
934 case TEMPLATE_ID_EXPR:
935 {
936 tree args;
aa36c081
JM
937 tree name = TREE_OPERAND (t, 0);
938 if (is_overloaded_fn (name))
939 name = DECL_NAME (get_first_fn (name));
9e93bc9d 940 dump_decl (name, flags);
99885b3f 941 print_template_argument_list_start (scratch_buffer);
74cd8397
JM
942 for (args = TREE_OPERAND (t, 1); args; args = TREE_CHAIN (args))
943 {
612c671a 944 dump_template_argument (TREE_VALUE (args), flags);
74cd8397 945 if (TREE_CHAIN (args))
99885b3f 946 separate_with_comma (scratch_buffer);
74cd8397 947 }
99885b3f 948 print_template_argument_list_end (scratch_buffer);
74cd8397
JM
949 }
950 break;
951
0fb9f1c3 952 case LOOKUP_EXPR:
9e93bc9d 953 dump_decl (TREE_OPERAND (t, 0), flags);
0fb9f1c3
JM
954 break;
955
8d08fdba 956 case LABEL_DECL:
99885b3f 957 print_tree_identifier (scratch_buffer, DECL_NAME (t));
8d08fdba
MS
958 break;
959
960 case CONST_DECL:
6467930b 961 if ((TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == ENUMERAL_TYPE)
03555413 962 || (DECL_INITIAL (t) &&
f84b4be9 963 TREE_CODE (DECL_INITIAL (t)) == TEMPLATE_PARM_INDEX))
9e93bc9d 964 dump_simple_decl (t, TREE_TYPE (t), flags);
224c649b 965 else if (DECL_NAME (t))
9e93bc9d 966 dump_decl (DECL_NAME (t), flags);
03555413 967 else if (DECL_INITIAL (t))
761f0855 968 dump_expr (DECL_INITIAL (t), flags | TFF_EXPR_IN_PARENS);
03555413 969 else
f3146d75 970 print_identifier (scratch_buffer, "<enumerator>");
8d08fdba
MS
971 break;
972
cffa8729 973 case USING_DECL:
99885b3f 974 output_add_string (scratch_buffer, "using ");
9e93bc9d 975 dump_type (DECL_INITIAL (t), flags);
99885b3f 976 print_scope_operator (scratch_buffer);
37198bc8 977 dump_decl (DECL_NAME (t), flags);
cffa8729
MS
978 break;
979
50ad9642
MM
980 case BASELINK:
981 dump_decl (BASELINK_FUNCTIONS (t), flags);
982 break;
983
8d08fdba 984 default:
99885b3f 985 sorry_for_unsupported_tree (t);
9e93bc9d
NS
986 /* Fallthrough to error. */
987
988 case ERROR_MARK:
99885b3f 989 print_identifier (scratch_buffer, "<declaration error>");
9e93bc9d
NS
990 break;
991 }
992}
993
994/* Dump a template declaration T under control of FLAGS. This means the
995 'template <...> leaders plus the 'class X' or 'void fn(...)' part. */
996
997static void
3f8548e7 998dump_template_decl (tree t, int flags)
9e93bc9d 999{
b5ac18ea
MM
1000 tree orig_parms = DECL_TEMPLATE_PARMS (t);
1001 tree parms;
bb20cc46
AJ
1002 int i;
1003
761f0855 1004 if (flags & TFF_TEMPLATE_HEADER)
9e93bc9d 1005 {
bb20cc46 1006 for (parms = orig_parms = nreverse (orig_parms);
b5ac18ea
MM
1007 parms;
1008 parms = TREE_CHAIN (parms))
9e93bc9d 1009 {
b5ac18ea
MM
1010 tree inner_parms = INNERMOST_TEMPLATE_PARMS (parms);
1011 int len = TREE_VEC_LENGTH (inner_parms);
bb20cc46 1012
99885b3f 1013 output_add_string (scratch_buffer, "template<");
38066e83
KL
1014
1015 /* If we've shown the template prefix, we'd better show the
1016 parameters' and decl's type too. */
1017 flags |= TFF_DECL_SPECIFIERS;
1018
9e93bc9d
NS
1019 for (i = 0; i < len; i++)
1020 {
9e93bc9d 1021 if (i)
99885b3f 1022 separate_with_comma (scratch_buffer);
b5ac18ea 1023 dump_template_parameter (TREE_VEC_ELT (inner_parms, i), flags);
9e93bc9d 1024 }
99885b3f 1025 print_template_argument_list_end (scratch_buffer);
faafaee6 1026 output_add_space (scratch_buffer);
9e93bc9d 1027 }
b5ac18ea 1028 nreverse(orig_parms);
38066e83
KL
1029
1030 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
c6002625 1031 /* Say `template<arg> class TT' not just `template<arg> TT'. */
38066e83 1032 output_add_string (scratch_buffer, "class ");
9e93bc9d 1033 }
38066e83 1034
9e93bc9d
NS
1035 if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
1036 dump_type (TREE_TYPE (t),
761f0855
GDR
1037 ((flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1038 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0)));
9e93bc9d 1039 else if (TREE_CODE (DECL_TEMPLATE_RESULT (t)) == VAR_DECL)
761f0855 1040 dump_decl (DECL_TEMPLATE_RESULT (t), flags | TFF_TEMPLATE_NAME);
9e93bc9d 1041 else if (TREE_TYPE (t) == NULL_TREE)
a98facb0 1042 abort ();
9e93bc9d
NS
1043 else
1044 switch (NEXT_CODE (t))
1045 {
1046 case METHOD_TYPE:
1047 case FUNCTION_TYPE:
761f0855 1048 dump_function_decl (t, flags | TFF_TEMPLATE_NAME);
9e93bc9d
NS
1049 break;
1050 default:
0e339752 1051 /* This case can occur with some invalid code. */
9e93bc9d 1052 dump_type (TREE_TYPE (t),
761f0855
GDR
1053 (flags & ~TFF_CLASS_KEY_OR_ENUM) | TFF_TEMPLATE_NAME
1054 | (flags & TFF_DECL_SPECIFIERS ? TFF_CLASS_KEY_OR_ENUM : 0));
8d08fdba
MS
1055 }
1056}
1057
4995028c 1058/* Pretty print a function decl. There are several ways we want to print a
761f0855 1059 function declaration. The TFF_ bits in FLAGS tells us how to behave.
33bd39a2 1060 As error can only apply the '#' flag once to give 0 and 1 for V, there
c6002625 1061 is %D which doesn't print the throw specs, and %F which does. */
8d08fdba
MS
1062
1063static void
3f8548e7 1064dump_function_decl (tree t, int flags)
8d08fdba 1065{
98c1c668
JM
1066 tree fntype;
1067 tree parmtypes;
8d08fdba 1068 tree cname = NULL_TREE;
612c671a
GDR
1069 tree template_args = NULL_TREE;
1070 tree template_parms = NULL_TREE;
761f0855 1071 int show_return = flags & TFF_RETURN_TYPE || flags & TFF_DECL_SPECIFIERS;
8d08fdba 1072
98c1c668
JM
1073 if (TREE_CODE (t) == TEMPLATE_DECL)
1074 t = DECL_TEMPLATE_RESULT (t);
1075
612c671a 1076 /* Pretty print template instantiations only. */
f9817201 1077 if (DECL_USE_TEMPLATE (t) && DECL_TEMPLATE_INFO (t))
612c671a 1078 {
f9a7ae04
MM
1079 tree tmpl;
1080
612c671a 1081 template_args = DECL_TI_ARGS (t);
f9a7ae04
MM
1082 tmpl = most_general_template (t);
1083 if (tmpl && TREE_CODE (tmpl) == TEMPLATE_DECL)
1084 {
1085 template_parms = DECL_TEMPLATE_PARMS (tmpl);
1086 t = tmpl;
1087 }
612c671a
GDR
1088 }
1089
98c1c668 1090 fntype = TREE_TYPE (t);
e0fff4b3 1091 parmtypes = FUNCTION_FIRST_USER_PARMTYPE (t);
98c1c668 1092
2642b9bf 1093 if (DECL_CLASS_SCOPE_P (t))
4f1c5b7d 1094 cname = DECL_CONTEXT (t);
8d08fdba
MS
1095 /* this is for partially instantiated template methods */
1096 else if (TREE_CODE (fntype) == METHOD_TYPE)
1097 cname = TREE_TYPE (TREE_VALUE (parmtypes));
1098
761f0855 1099 if (!(flags & TFF_DECL_SPECIFIERS))
9e93bc9d
NS
1100 /* OK */;
1101 else if (DECL_STATIC_FUNCTION_P (t))
99885b3f 1102 print_identifier (scratch_buffer, "static ");
00bb3dad 1103 else if (DECL_VIRTUAL_P (t))
99885b3f 1104 print_identifier (scratch_buffer, "virtual ");
bb20cc46 1105
9e93bc9d
NS
1106 /* Print the return type? */
1107 if (show_return)
1108 show_return = !DECL_CONV_FN_P (t) && !DECL_CONSTRUCTOR_P (t)
1109 && !DECL_DESTRUCTOR_P (t);
1110 if (show_return)
8d08fdba 1111 {
5f9cd837
GDR
1112 dump_type_prefix (TREE_TYPE (fntype), flags);
1113 output_add_space (scratch_buffer);
8d08fdba
MS
1114 }
1115
61cd552e 1116 /* Print the function name. */
8d08fdba
MS
1117 if (cname)
1118 {
9e93bc9d 1119 dump_type (cname, flags);
99885b3f 1120 print_scope_operator (scratch_buffer);
2642b9bf 1121 }
9e93bc9d
NS
1122 else
1123 dump_scope (CP_DECL_CONTEXT (t), flags);
8d08fdba 1124
9e93bc9d 1125 dump_function_name (t, flags);
bb20cc46 1126
55ace93c 1127 if (1)
ad50e811 1128 {
ad50e811 1129 dump_parameters (parmtypes, flags);
bb20cc46 1130
ad50e811
MM
1131 if (TREE_CODE (fntype) == METHOD_TYPE)
1132 dump_qualifiers (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))),
1133 before);
bb20cc46 1134
761f0855 1135 if (flags & TFF_EXCEPTION_SPECIFICATION)
ad50e811 1136 dump_exception_spec (TYPE_RAISES_EXCEPTIONS (fntype), flags);
ad6b1795
JM
1137
1138 if (show_return)
1139 dump_type_suffix (TREE_TYPE (fntype), flags);
ad50e811 1140 }
612c671a
GDR
1141
1142 /* If T is a template instantiation, dump the parameter binding. */
1143 if (template_parms != NULL_TREE && template_args != NULL_TREE)
1144 {
99885b3f 1145 output_add_string (scratch_buffer, " [with ");
b5ac18ea 1146 dump_template_bindings (template_parms, template_args);
99885b3f 1147 print_right_bracket (scratch_buffer);
612c671a 1148 }
4995028c
NS
1149}
1150
9e93bc9d
NS
1151/* Print a parameter list. If this is for a member function, the
1152 member object ptr (and any other hidden args) should have
c6002625 1153 already been removed. */
4995028c
NS
1154
1155static void
3f8548e7 1156dump_parameters (tree parmtypes, int flags)
4995028c
NS
1157{
1158 int first;
99885b3f
GDR
1159
1160 print_left_paren (scratch_buffer);
4995028c
NS
1161
1162 for (first = 1; parmtypes != void_list_node;
1163 parmtypes = TREE_CHAIN (parmtypes))
1164 {
1165 if (!first)
99885b3f 1166 separate_with_comma (scratch_buffer);
4995028c
NS
1167 first = 0;
1168 if (!parmtypes)
1169 {
99885b3f 1170 print_identifier (scratch_buffer, "...");
4995028c
NS
1171 break;
1172 }
9e93bc9d 1173 dump_type (TREE_VALUE (parmtypes), flags);
bb20cc46 1174
761f0855 1175 if ((flags & TFF_FUNCTION_DEFAULT_ARGUMENTS) && TREE_PURPOSE (parmtypes))
4995028c 1176 {
99885b3f 1177 output_add_string (scratch_buffer, " = ");
761f0855 1178 dump_expr (TREE_PURPOSE (parmtypes), flags | TFF_EXPR_IN_PARENS);
4995028c
NS
1179 }
1180 }
1181
99885b3f 1182 print_right_paren (scratch_buffer);
4995028c
NS
1183}
1184
c6002625 1185/* Print an exception specification. T is the exception specification. */
4995028c
NS
1186
1187static void
3f8548e7 1188dump_exception_spec (tree t, int flags)
4995028c
NS
1189{
1190 if (t)
1191 {
99885b3f 1192 output_add_string (scratch_buffer, " throw (");
4995028c
NS
1193 if (TREE_VALUE (t) != NULL_TREE)
1194 while (1)
1195 {
9e93bc9d 1196 dump_type (TREE_VALUE (t), flags);
4995028c
NS
1197 t = TREE_CHAIN (t);
1198 if (!t)
1199 break;
99885b3f 1200 separate_with_comma (scratch_buffer);
4995028c 1201 }
99885b3f 1202 print_right_paren (scratch_buffer);
4995028c 1203 }
8d08fdba
MS
1204}
1205
1206/* Handle the function name for a FUNCTION_DECL node, grokking operators
1207 and destructors properly. */
e92cc029 1208
8d08fdba 1209static void
3f8548e7 1210dump_function_name (tree t, int flags)
8d08fdba
MS
1211{
1212 tree name = DECL_NAME (t);
1213
78abea27
RS
1214 if (TREE_CODE (t) == TEMPLATE_DECL)
1215 t = DECL_TEMPLATE_RESULT (t);
1216
5e818b93
JM
1217 /* Don't let the user see __comp_ctor et al. */
1218 if (DECL_CONSTRUCTOR_P (t)
1219 || DECL_DESTRUCTOR_P (t))
1220 name = constructor_name (DECL_CONTEXT (t));
1221
aa45967f 1222 if (DECL_DESTRUCTOR_P (t))
8d08fdba 1223 {
99885b3f 1224 output_add_character (scratch_buffer, '~');
761f0855 1225 dump_decl (name, TFF_PLAIN_IDENTIFIER);
8d08fdba 1226 }
aa45967f 1227 else if (DECL_CONV_FN_P (t))
8d08fdba
MS
1228 {
1229 /* This cannot use the hack that the operator's return
1230 type is stashed off of its name because it may be
1231 used for error reporting. In the case of conflicting
1232 declarations, both will have the same name, yet
1233 the types will be different, hence the TREE_TYPE field
1234 of the first name will be clobbered by the second. */
99885b3f 1235 output_add_string (scratch_buffer, "operator ");
9e93bc9d 1236 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
8d08fdba
MS
1237 }
1238 else if (IDENTIFIER_OPNAME_P (name))
99885b3f 1239 print_tree_identifier (scratch_buffer, name);
8d08fdba 1240 else
9e93bc9d 1241 dump_decl (name, flags);
386b8a85 1242
9e93bc9d 1243 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
05fd666b 1244 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t)
bb20cc46 1245 && (DECL_TEMPLATE_SPECIALIZATION (t)
36a117a5
MM
1246 || TREE_CODE (DECL_TI_TEMPLATE (t)) != TEMPLATE_DECL
1247 || DECL_TEMPLATE_SPECIALIZATION (DECL_TI_TEMPLATE (t))
1248 || PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
9e93bc9d
NS
1249 dump_template_parms (DECL_TEMPLATE_INFO (t), !DECL_USE_TEMPLATE (t), flags);
1250}
75650646 1251
9e93bc9d
NS
1252/* Dump the template parameters from the template info INFO under control of
1253 FLAGS. PRIMARY indicates whether this is a primary template decl, or
1254 specialization (partial or complete). For partial specializations we show
1255 the specialized parameter values. For a primary template we show no
1256 decoration. */
1257
1258static void
3f8548e7 1259dump_template_parms (tree info, int primary, int flags)
9e93bc9d
NS
1260{
1261 tree args = info ? TI_ARGS (info) : NULL_TREE;
bb20cc46 1262
761f0855 1263 if (primary && flags & TFF_TEMPLATE_NAME)
9e93bc9d 1264 return;
761f0855 1265 flags &= ~(TFF_CLASS_KEY_OR_ENUM | TFF_TEMPLATE_NAME);
99885b3f 1266 print_template_argument_list_start (scratch_buffer);
9e93bc9d
NS
1267
1268 /* Be careful only to print things when we have them, so as not
36a117a5 1269 to crash producing error messages. */
9e93bc9d
NS
1270 if (args && !primary)
1271 {
1272 int len = 0;
1273 int ix = 0;
1274 int need_comma = 0;
bb20cc46 1275
9e93bc9d
NS
1276 if (TREE_CODE (args) == TREE_VEC)
1277 {
1278 if (TREE_VEC_LENGTH (args) > 0
bb20cc46
AJ
1279 && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
1280 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1281
9e93bc9d
NS
1282 len = TREE_VEC_LENGTH (args);
1283 }
1284 else if (TREE_CODE (args) == TREE_LIST)
1285 len = -1;
1286 while (ix != len && args)
1287 {
1288 tree arg;
1289 if (len >= 0)
1290 {
1291 arg = TREE_VEC_ELT (args, ix);
1292 ix++;
1293 }
1294 else
1295 {
1296 arg = TREE_VALUE (args);
1297 args = TREE_CHAIN (args);
1298 }
1299 if (need_comma)
99885b3f
GDR
1300 separate_with_comma (scratch_buffer);
1301
9e93bc9d 1302 if (!arg)
99885b3f 1303 print_identifier (scratch_buffer, "<template parameter error>");
9e93bc9d 1304 else
612c671a 1305 dump_template_argument (arg, flags);
9e93bc9d
NS
1306 need_comma = 1;
1307 }
1308 }
9e93bc9d
NS
1309 else if (primary)
1310 {
1311 tree tpl = TI_TEMPLATE (info);
1312 tree parms = DECL_TEMPLATE_PARMS (tpl);
1313 int len, ix;
bb20cc46 1314
9e93bc9d
NS
1315 parms = TREE_CODE (parms) == TREE_LIST ? TREE_VALUE (parms) : NULL_TREE;
1316 len = parms ? TREE_VEC_LENGTH (parms) : 0;
bb20cc46 1317
9e93bc9d
NS
1318 for (ix = 0; ix != len; ix++)
1319 {
1320 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, ix));
1321
1322 if (ix)
99885b3f 1323 separate_with_comma (scratch_buffer);
bb20cc46 1324
761f0855 1325 dump_decl (parm, flags & ~TFF_DECL_SPECIFIERS);
9e93bc9d 1326 }
386b8a85 1327 }
99885b3f 1328 print_template_argument_list_end (scratch_buffer);
8d08fdba
MS
1329}
1330
1331static void
3f8548e7 1332dump_char (int c)
8d08fdba
MS
1333{
1334 switch (c)
1335 {
a0a33927 1336 case TARGET_NEWLINE:
99885b3f 1337 output_add_string (scratch_buffer, "\\n");
8d08fdba 1338 break;
a0a33927 1339 case TARGET_TAB:
99885b3f 1340 output_add_string (scratch_buffer, "\\t");
8d08fdba 1341 break;
a0a33927 1342 case TARGET_VT:
99885b3f 1343 output_add_string (scratch_buffer, "\\v");
8d08fdba 1344 break;
a0a33927 1345 case TARGET_BS:
99885b3f 1346 output_add_string (scratch_buffer, "\\b");
8d08fdba 1347 break;
a0a33927 1348 case TARGET_CR:
99885b3f 1349 output_add_string (scratch_buffer, "\\r");
8d08fdba 1350 break;
a0a33927 1351 case TARGET_FF:
99885b3f 1352 output_add_string (scratch_buffer, "\\f");
8d08fdba 1353 break;
a0a33927 1354 case TARGET_BELL:
99885b3f 1355 output_add_string (scratch_buffer, "\\a");
8d08fdba
MS
1356 break;
1357 case '\\':
99885b3f 1358 output_add_string (scratch_buffer, "\\\\");
8d08fdba
MS
1359 break;
1360 case '\'':
99885b3f 1361 output_add_string (scratch_buffer, "\\'");
8d08fdba
MS
1362 break;
1363 case '\"':
99885b3f 1364 output_add_string (scratch_buffer, "\\\"");
8d08fdba
MS
1365 break;
1366 default:
faa25e97 1367 if (ISPRINT (c))
99885b3f 1368 output_add_character (scratch_buffer, c);
8d08fdba 1369 else
4e3f84b7
GDR
1370 output_formatted_scalar (scratch_buffer, "\\%03o", (unsigned) c);
1371 break;
8d08fdba
MS
1372 }
1373}
1374
1375/* Print out a list of initializers (subr of dump_expr) */
e92cc029 1376
8d08fdba 1377static void
3f8548e7 1378dump_expr_list (tree l, int flags)
8d08fdba
MS
1379{
1380 while (l)
1381 {
761f0855 1382 dump_expr (TREE_VALUE (l), flags | TFF_EXPR_IN_PARENS);
8d08fdba 1383 l = TREE_CHAIN (l);
9e93bc9d 1384 if (l)
99885b3f 1385 separate_with_comma (scratch_buffer);
8d08fdba
MS
1386 }
1387}
1388
c6002625 1389/* Print out an expression E under control of FLAGS. */
e92cc029 1390
8d08fdba 1391static void
3f8548e7 1392dump_expr (tree t, int flags)
8d08fdba 1393{
8c048a52
GDR
1394 if (t == 0)
1395 return;
1396
8d08fdba
MS
1397 switch (TREE_CODE (t))
1398 {
1399 case VAR_DECL:
1400 case PARM_DECL:
1401 case FIELD_DECL:
1402 case CONST_DECL:
1403 case FUNCTION_DECL:
ec255269 1404 case TEMPLATE_DECL:
6b57ac29 1405 case NAMESPACE_DECL:
5d73aa63 1406 case OVERLOAD:
a723baf1 1407 case IDENTIFIER_NODE:
761f0855 1408 dump_decl (t, flags & ~TFF_DECL_SPECIFIERS);
8d08fdba
MS
1409 break;
1410
1411 case INTEGER_CST:
1412 {
1413 tree type = TREE_TYPE (t);
1414 my_friendly_assert (type != 0, 81);
1415
1416 /* If it's an enum, output its tag, rather than its value. */
1417 if (TREE_CODE (type) == ENUMERAL_TYPE)
1418 {
fa40aa12 1419 tree values = TYPE_VALUES (type);
bb20cc46 1420
fa40aa12
NS
1421 for (; values;
1422 values = TREE_CHAIN (values))
1423 if (tree_int_cst_equal (TREE_VALUE (values), t))
1424 break;
bb20cc46 1425
fa40aa12 1426 if (values)
99885b3f 1427 print_tree_identifier (scratch_buffer, TREE_PURPOSE (values));
fa40aa12
NS
1428 else
1429 {
1430 /* Value must have been cast. */
99885b3f 1431 print_left_paren (scratch_buffer);
fa40aa12 1432 dump_type (type, flags);
99885b3f 1433 print_right_paren (scratch_buffer);
fa40aa12
NS
1434 goto do_int;
1435 }
8d08fdba 1436 }
b7484fbe
MS
1437 else if (type == boolean_type_node)
1438 {
665f2503 1439 if (t == boolean_false_node || integer_zerop (t))
99885b3f 1440 print_identifier (scratch_buffer, "false");
b7484fbe 1441 else if (t == boolean_true_node)
99885b3f 1442 print_identifier (scratch_buffer, "true");
b7484fbe
MS
1443 }
1444 else if (type == char_type_node)
8d08fdba 1445 {
99885b3f 1446 output_add_character (scratch_buffer, '\'');
5089de93
MM
1447 if (host_integerp (t, TREE_UNSIGNED (type)))
1448 dump_char (tree_low_cst (t, TREE_UNSIGNED (type)));
1449 else
42b99e03
KG
1450 output_printf (scratch_buffer, "\\x%x",
1451 (unsigned int) TREE_INT_CST_LOW (t));
99885b3f 1452 output_add_character (scratch_buffer, '\'');
8d08fdba 1453 }
fa40aa12 1454 else
8d08fdba 1455 {
fa40aa12 1456 do_int:
ba523395 1457 if (! host_integerp (t, 0))
8d08fdba 1458 {
fa40aa12
NS
1459 tree val = t;
1460
1461 if (tree_int_cst_sgn (val) < 0)
1462 {
99885b3f 1463 output_add_character (scratch_buffer, '-');
fa40aa12
NS
1464 val = build_int_2 (-TREE_INT_CST_LOW (val),
1465 ~TREE_INT_CST_HIGH (val)
1466 + !TREE_INT_CST_LOW (val));
1467 }
4e3f84b7
GDR
1468 sprintf (scratch_buffer->digit_buffer,
1469 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
58228df6 1470 TREE_INT_CST_HIGH (val), TREE_INT_CST_LOW (val));
4e3f84b7
GDR
1471 output_add_string
1472 (scratch_buffer, scratch_buffer->digit_buffer);
8d08fdba 1473 }
fa40aa12 1474 else
748f6cb5 1475 output_host_wide_integer (scratch_buffer, TREE_INT_CST_LOW (t));
8d08fdba 1476 }
8d08fdba
MS
1477 }
1478 break;
1479
1480 case REAL_CST:
4e3f84b7
GDR
1481 real_to_decimal (scratch_buffer->digit_buffer, &TREE_REAL_CST (t),
1482 sizeof (scratch_buffer->digit_buffer), 0, 1);
1483 output_add_string (scratch_buffer, scratch_buffer->digit_buffer);
8d08fdba
MS
1484 break;
1485
61a127b3 1486 case PTRMEM_CST:
99885b3f 1487 output_add_character (scratch_buffer, '&');
9e93bc9d 1488 dump_type (PTRMEM_CST_CLASS (t), flags);
99885b3f
GDR
1489 print_scope_operator (scratch_buffer);
1490 print_tree_identifier
1491 (scratch_buffer, DECL_NAME (PTRMEM_CST_MEMBER (t)));
61a127b3
MM
1492 break;
1493
8d08fdba
MS
1494 case STRING_CST:
1495 {
9c0758dd 1496 const char *p = TREE_STRING_POINTER (t);
8d08fdba
MS
1497 int len = TREE_STRING_LENGTH (t) - 1;
1498 int i;
1499
99885b3f 1500 output_add_character (scratch_buffer, '\"');
8d08fdba
MS
1501 for (i = 0; i < len; i++)
1502 dump_char (p[i]);
99885b3f 1503 output_add_character (scratch_buffer, '\"');
8d08fdba
MS
1504 }
1505 break;
1506
1507 case COMPOUND_EXPR:
99885b3f 1508 print_left_paren (scratch_buffer);
44978276
GB
1509 /* Within templates, a COMPOUND_EXPR has only one operand,
1510 containing a TREE_LIST of the two operands. */
1511 if (TREE_CODE (TREE_OPERAND (t, 0)) == TREE_LIST)
1512 {
1513 if (TREE_OPERAND (t, 1))
1514 abort();
1515 dump_expr_list (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1516 }
1517 else
1518 {
1519 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
1520 separate_with_comma (scratch_buffer);
1521 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
1522 }
99885b3f 1523 print_right_paren (scratch_buffer);
8d08fdba
MS
1524 break;
1525
1526 case COND_EXPR:
99885b3f 1527 print_left_paren (scratch_buffer);
761f0855 1528 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 1529 output_add_string (scratch_buffer, " ? ");
761f0855 1530 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
99885b3f 1531 output_add_string (scratch_buffer, " : ");
761f0855 1532 dump_expr (TREE_OPERAND (t, 2), flags | TFF_EXPR_IN_PARENS);
99885b3f 1533 print_right_paren (scratch_buffer);
8d08fdba
MS
1534 break;
1535
1536 case SAVE_EXPR:
1537 if (TREE_HAS_CONSTRUCTOR (t))
1538 {
99885b3f 1539 output_add_string (scratch_buffer, "new ");
9e93bc9d 1540 dump_type (TREE_TYPE (TREE_TYPE (t)), flags);
8d08fdba
MS
1541 }
1542 else
1543 {
761f0855 1544 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
8d08fdba
MS
1545 }
1546 break;
1547
02531345 1548 case AGGR_INIT_EXPR:
27b8d0cd
MM
1549 {
1550 tree fn = NULL_TREE;
bb20cc46 1551
27b8d0cd
MM
1552 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
1553 fn = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
1554
1555 if (fn && TREE_CODE (fn) == FUNCTION_DECL)
1556 {
1557 if (DECL_CONSTRUCTOR_P (fn))
99885b3f
GDR
1558 print_tree_identifier
1559 (scratch_buffer, TYPE_IDENTIFIER (TREE_TYPE (t)));
27b8d0cd
MM
1560 else
1561 dump_decl (fn, 0);
1562 }
1563 else
1564 dump_expr (TREE_OPERAND (t, 0), 0);
1565 }
99885b3f 1566 print_left_paren (scratch_buffer);
42976354 1567 if (TREE_OPERAND (t, 1))
9e93bc9d 1568 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
99885b3f 1569 print_right_paren (scratch_buffer);
8d08fdba
MS
1570 break;
1571
1572 case CALL_EXPR:
1573 {
1574 tree fn = TREE_OPERAND (t, 0);
1575 tree args = TREE_OPERAND (t, 1);
bb20cc46 1576
8d08fdba
MS
1577 if (TREE_CODE (fn) == ADDR_EXPR)
1578 fn = TREE_OPERAND (fn, 0);
1579
6467930b 1580 if (TREE_TYPE (fn) != NULL_TREE && NEXT_CODE (fn) == METHOD_TYPE)
8d08fdba
MS
1581 {
1582 tree ob = TREE_VALUE (args);
1583 if (TREE_CODE (ob) == ADDR_EXPR)
1584 {
761f0855 1585 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 1586 output_add_character (scratch_buffer, '.');
8d08fdba
MS
1587 }
1588 else if (TREE_CODE (ob) != PARM_DECL
1589 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1590 {
761f0855 1591 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
99885b3f 1592 output_add_string (scratch_buffer, "->");
8d08fdba
MS
1593 }
1594 args = TREE_CHAIN (args);
1595 }
761f0855 1596 dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
99885b3f 1597 print_left_paren (scratch_buffer);
9e93bc9d 1598 dump_expr_list (args, flags);
99885b3f 1599 print_right_paren (scratch_buffer);
8d08fdba
MS
1600 }
1601 break;
1602
285baa06
JM
1603 case NEW_EXPR:
1604 {
1605 tree type = TREE_OPERAND (t, 1);
60cde936 1606 tree init = TREE_OPERAND (t, 2);
285baa06 1607 if (NEW_EXPR_USE_GLOBAL (t))
99885b3f
GDR
1608 print_scope_operator (scratch_buffer);
1609 output_add_string (scratch_buffer, "new ");
285baa06
JM
1610 if (TREE_OPERAND (t, 0))
1611 {
99885b3f 1612 print_left_paren (scratch_buffer);
9e93bc9d 1613 dump_expr_list (TREE_OPERAND (t, 0), flags);
99885b3f 1614 output_add_string (scratch_buffer, ") ");
285baa06
JM
1615 }
1616 if (TREE_CODE (type) == ARRAY_REF)
1617 type = build_cplus_array_type
1618 (TREE_OPERAND (type, 0),
fed3cef0
RK
1619 build_index_type (fold (build (MINUS_EXPR, integer_type_node,
1620 TREE_OPERAND (type, 1),
1621 integer_one_node))));
9e93bc9d 1622 dump_type (type, flags);
60cde936 1623 if (init)
285baa06 1624 {
99885b3f 1625 print_left_paren (scratch_buffer);
60cde936
MM
1626 if (TREE_CODE (init) == TREE_LIST)
1627 dump_expr_list (init, flags);
1628 else if (init == void_zero_node)
1629 /* This representation indicates an empty initializer,
1630 e.g.: "new int()". */
1631 ;
1632 else
1633 dump_expr (init, flags);
99885b3f 1634 print_right_paren (scratch_buffer);
285baa06
JM
1635 }
1636 }
1637 break;
1638
8d08fdba
MS
1639 case TARGET_EXPR:
1640 /* Note that this only works for G++ target exprs. If somebody
1641 builds a general TARGET_EXPR, there's no way to represent that
1642 it initializes anything other that the parameter slot for the
1643 default argument. Note we may have cleared out the first
1644 operand in expand_expr, so don't go killing ourselves. */
1645 if (TREE_OPERAND (t, 1))
761f0855 1646 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
8d08fdba
MS
1647 break;
1648
b3ab27f3 1649 case INIT_EXPR:
8d08fdba
MS
1650 case MODIFY_EXPR:
1651 case PLUS_EXPR:
1652 case MINUS_EXPR:
1653 case MULT_EXPR:
1654 case TRUNC_DIV_EXPR:
1655 case TRUNC_MOD_EXPR:
1656 case MIN_EXPR:
1657 case MAX_EXPR:
1658 case LSHIFT_EXPR:
1659 case RSHIFT_EXPR:
1660 case BIT_IOR_EXPR:
1661 case BIT_XOR_EXPR:
1662 case BIT_AND_EXPR:
1663 case BIT_ANDTC_EXPR:
1664 case TRUTH_ANDIF_EXPR:
1665 case TRUTH_ORIF_EXPR:
1666 case LT_EXPR:
1667 case LE_EXPR:
1668 case GT_EXPR:
1669 case GE_EXPR:
1670 case EQ_EXPR:
1671 case NE_EXPR:
2adeacc9 1672 case EXACT_DIV_EXPR:
596ea4e5 1673 dump_binary_op (operator_name_info[(int) TREE_CODE (t)].name, t, flags);
8d08fdba
MS
1674 break;
1675
1676 case CEIL_DIV_EXPR:
1677 case FLOOR_DIV_EXPR:
1678 case ROUND_DIV_EXPR:
9e93bc9d 1679 dump_binary_op ("/", t, flags);
8d08fdba
MS
1680 break;
1681
1682 case CEIL_MOD_EXPR:
1683 case FLOOR_MOD_EXPR:
1684 case ROUND_MOD_EXPR:
9e93bc9d 1685 dump_binary_op ("%", t, flags);
8d08fdba
MS
1686 break;
1687
1688 case COMPONENT_REF:
1689 {
1690 tree ob = TREE_OPERAND (t, 0);
1691 if (TREE_CODE (ob) == INDIRECT_REF)
1692 {
1693 ob = TREE_OPERAND (ob, 0);
1694 if (TREE_CODE (ob) != PARM_DECL
1695 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1696 {
761f0855 1697 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
99885b3f 1698 output_add_string (scratch_buffer, "->");
8d08fdba
MS
1699 }
1700 }
1701 else
1702 {
761f0855 1703 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
99885b3f 1704 output_add_character (scratch_buffer, '.');
8d08fdba 1705 }
761f0855 1706 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
8d08fdba
MS
1707 }
1708 break;
1709
28cbf42c 1710 case ARRAY_REF:
761f0855 1711 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 1712 print_left_bracket (scratch_buffer);
761f0855 1713 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
99885b3f 1714 print_right_bracket (scratch_buffer);
28cbf42c
MS
1715 break;
1716
8d08fdba 1717 case CONVERT_EXPR:
03da5286 1718 if (TREE_TYPE (t) && VOID_TYPE_P (TREE_TYPE (t)))
df39af7d 1719 {
99885b3f 1720 print_left_paren (scratch_buffer);
b72801e2 1721 dump_type (TREE_TYPE (t), flags);
99885b3f 1722 print_right_paren (scratch_buffer);
9e93bc9d 1723 dump_expr (TREE_OPERAND (t, 0), flags);
df39af7d
JM
1724 }
1725 else
9e93bc9d 1726 dump_unary_op ("+", t, flags);
8d08fdba
MS
1727 break;
1728
1729 case ADDR_EXPR:
1730 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
fd74ca0b
MM
1731 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST
1732 /* An ADDR_EXPR can have reference type. In that case, we
1733 shouldn't print the `&' doing so indicates to the user
1734 that the expression has pointer type. */
bb20cc46 1735 || (TREE_TYPE (t)
fd74ca0b 1736 && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE))
761f0855 1737 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
8d08fdba 1738 else
9e93bc9d 1739 dump_unary_op ("&", t, flags);
8d08fdba
MS
1740 break;
1741
1742 case INDIRECT_REF:
1743 if (TREE_HAS_CONSTRUCTOR (t))
1744 {
1745 t = TREE_OPERAND (t, 0);
1746 my_friendly_assert (TREE_CODE (t) == CALL_EXPR, 237);
761f0855 1747 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 1748 print_left_paren (scratch_buffer);
9e93bc9d 1749 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)), flags);
99885b3f 1750 print_right_paren (scratch_buffer);
8d08fdba
MS
1751 }
1752 else
1753 {
6467930b 1754 if (TREE_OPERAND (t,0) != NULL_TREE
5082a355 1755 && TREE_TYPE (TREE_OPERAND (t, 0))
6467930b 1756 && NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
9e93bc9d 1757 dump_expr (TREE_OPERAND (t, 0), flags);
8d08fdba 1758 else
9e93bc9d 1759 dump_unary_op ("*", t, flags);
8d08fdba
MS
1760 }
1761 break;
1762
1763 case NEGATE_EXPR:
1764 case BIT_NOT_EXPR:
1765 case TRUTH_NOT_EXPR:
1766 case PREDECREMENT_EXPR:
1767 case PREINCREMENT_EXPR:
596ea4e5 1768 dump_unary_op (operator_name_info [(int)TREE_CODE (t)].name, t, flags);
8d08fdba
MS
1769 break;
1770
1771 case POSTDECREMENT_EXPR:
1772 case POSTINCREMENT_EXPR:
99885b3f 1773 print_left_paren (scratch_buffer);
761f0855 1774 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f
GDR
1775 print_identifier
1776 (scratch_buffer, operator_name_info[(int)TREE_CODE (t)].name);
1777 print_right_paren (scratch_buffer);
8d08fdba
MS
1778 break;
1779
1780 case NON_LVALUE_EXPR:
1781 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1782 should be another level of INDIRECT_REF so that I don't have to do
1783 this. */
6467930b 1784 if (TREE_TYPE (t) != NULL_TREE && NEXT_CODE (t) == POINTER_TYPE)
8d08fdba
MS
1785 {
1786 tree next = TREE_TYPE (TREE_TYPE (t));
1787
1788 while (TREE_CODE (next) == POINTER_TYPE)
1789 next = TREE_TYPE (next);
bb20cc46 1790
8d08fdba
MS
1791 if (TREE_CODE (next) == FUNCTION_TYPE)
1792 {
761f0855 1793 if (flags & TFF_EXPR_IN_PARENS)
99885b3f
GDR
1794 print_left_paren (scratch_buffer);
1795 output_add_character (scratch_buffer, '*');
761f0855
GDR
1796 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
1797 if (flags & TFF_EXPR_IN_PARENS)
99885b3f 1798 print_right_paren (scratch_buffer);
8d08fdba
MS
1799 break;
1800 }
1801 /* else FALLTHRU */
1802 }
761f0855 1803 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
8d08fdba
MS
1804 break;
1805
1806 case NOP_EXPR:
9e93bc9d 1807 dump_expr (TREE_OPERAND (t, 0), flags);
8d08fdba
MS
1808 break;
1809
89c6e7ac
MM
1810 case EXPR_WITH_FILE_LOCATION:
1811 dump_expr (EXPR_WFL_NODE (t), flags);
1812 break;
1813
8d08fdba 1814 case CONSTRUCTOR:
9a3b49ac
MS
1815 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1816 {
50ad9642 1817 tree idx = build_ptrmemfunc_access_expr (t, pfn_identifier);
9a3b49ac 1818
c4372ef4 1819 if (integer_zerop (idx))
e08a8f45
MM
1820 {
1821 /* A NULL pointer-to-member constant. */
99885b3f 1822 output_add_string (scratch_buffer, "((");
9e93bc9d 1823 dump_type (TREE_TYPE (t), flags);
99885b3f 1824 output_add_string (scratch_buffer, ") 0)");
e08a8f45
MM
1825 break;
1826 }
665f2503 1827 else if (host_integerp (idx, 0))
9a3b49ac
MS
1828 {
1829 tree virtuals;
1830 unsigned HOST_WIDE_INT n;
1831
1832 t = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1833 t = TYPE_METHOD_BASETYPE (t);
83f2ccf4 1834 virtuals = TYPE_BINFO_VIRTUALS (TYPE_MAIN_VARIANT (t));
bb20cc46 1835
1f84ec23 1836 n = tree_low_cst (idx, 0);
9a3b49ac
MS
1837
1838 /* Map vtable index back one, to allow for the null pointer to
1839 member. */
1840 --n;
1841
1842 while (n > 0 && virtuals)
1843 {
1844 --n;
1845 virtuals = TREE_CHAIN (virtuals);
1846 }
1847 if (virtuals)
1848 {
31f8e4f3 1849 dump_expr (BV_FN (virtuals),
761f0855 1850 flags | TFF_EXPR_IN_PARENS);
9a3b49ac
MS
1851 break;
1852 }
1853 }
1854 }
f3146d75
NS
1855 if (TREE_TYPE (t) && !CONSTRUCTOR_ELTS (t))
1856 {
1857 dump_type (TREE_TYPE (t), 0);
1858 output_add_character (scratch_buffer, '(');
1859 output_add_character (scratch_buffer, ')');
1860 }
1861 else
1862 {
1863 output_add_character (scratch_buffer, '{');
1864 dump_expr_list (CONSTRUCTOR_ELTS (t), flags);
1865 output_add_character (scratch_buffer, '}');
1866 }
1867
8d08fdba
MS
1868 break;
1869
51c184be
MS
1870 case OFFSET_REF:
1871 {
1872 tree ob = TREE_OPERAND (t, 0);
51924768 1873 if (is_dummy_object (ob))
61a127b3 1874 {
05e0b2f4
JM
1875 t = TREE_OPERAND (t, 1);
1876 if (TREE_CODE (t) == FUNCTION_DECL)
61a127b3 1877 /* A::f */
761f0855 1878 dump_expr (t, flags | TFF_EXPR_IN_PARENS);
05e0b2f4 1879 else if (BASELINK_P (t))
da15dae6
MM
1880 dump_expr (OVL_CURRENT (BASELINK_FUNCTIONS (t)),
1881 flags | TFF_EXPR_IN_PARENS);
61a127b3 1882 else
9e93bc9d 1883 dump_decl (t, flags);
61a127b3 1884 }
51c184be
MS
1885 else
1886 {
bbcec105
JM
1887 if (TREE_CODE (ob) == INDIRECT_REF)
1888 {
761f0855 1889 dump_expr (TREE_OPERAND (ob, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 1890 output_add_string (scratch_buffer, "->*");
bbcec105
JM
1891 }
1892 else
1893 {
761f0855 1894 dump_expr (ob, flags | TFF_EXPR_IN_PARENS);
99885b3f 1895 output_add_string (scratch_buffer, ".*");
bbcec105 1896 }
761f0855 1897 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
51c184be
MS
1898 }
1899 break;
1900 }
1901
f84b4be9 1902 case TEMPLATE_PARM_INDEX:
761f0855 1903 dump_decl (TEMPLATE_PARM_DECL (t), flags & ~TFF_DECL_SPECIFIERS);
de22184b 1904 break;
5566b478 1905
5566b478 1906 case SCOPE_REF:
9e93bc9d 1907 dump_type (TREE_OPERAND (t, 0), flags);
99885b3f 1908 print_scope_operator (scratch_buffer);
761f0855 1909 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
5566b478
MS
1910 break;
1911
1912 case CAST_EXPR:
e349ee73
MS
1913 if (TREE_OPERAND (t, 0) == NULL_TREE
1914 || TREE_CHAIN (TREE_OPERAND (t, 0)))
e76a2646 1915 {
9e93bc9d 1916 dump_type (TREE_TYPE (t), flags);
99885b3f 1917 print_left_paren (scratch_buffer);
9e93bc9d 1918 dump_expr_list (TREE_OPERAND (t, 0), flags);
99885b3f 1919 print_right_paren (scratch_buffer);
e76a2646
MS
1920 }
1921 else
1922 {
99885b3f 1923 print_left_paren (scratch_buffer);
9e93bc9d 1924 dump_type (TREE_TYPE (t), flags);
99885b3f 1925 output_add_string (scratch_buffer, ")(");
9e93bc9d 1926 dump_expr_list (TREE_OPERAND (t, 0), flags);
99885b3f 1927 print_right_paren (scratch_buffer);
e76a2646
MS
1928 }
1929 break;
1930
477f6664
JM
1931 case STATIC_CAST_EXPR:
1932 output_add_string (scratch_buffer, "static_cast<");
1933 goto cast;
1934 case REINTERPRET_CAST_EXPR:
1935 output_add_string (scratch_buffer, "reinterpret_cast<");
1936 goto cast;
1937 case CONST_CAST_EXPR:
1938 output_add_string (scratch_buffer, "const_cast<");
1939 goto cast;
1940 case DYNAMIC_CAST_EXPR:
1941 output_add_string (scratch_buffer, "dynamic_cast<");
1942 cast:
1943 dump_type (TREE_TYPE (t), flags);
1944 output_add_string (scratch_buffer, ">(");
1945 dump_expr (TREE_OPERAND (t, 0), flags);
1946 print_right_paren (scratch_buffer);
1947 break;
1948
e76a2646 1949 case LOOKUP_EXPR:
99885b3f 1950 print_tree_identifier (scratch_buffer, TREE_OPERAND (t, 0));
e76a2646
MS
1951 break;
1952
5a11e05b 1953 case ARROW_EXPR:
9e93bc9d 1954 dump_expr (TREE_OPERAND (t, 0), flags);
99885b3f 1955 output_add_string (scratch_buffer, "->");
5a11e05b
BK
1956 break;
1957
e76a2646 1958 case SIZEOF_EXPR:
abff8e06
JM
1959 case ALIGNOF_EXPR:
1960 if (TREE_CODE (t) == SIZEOF_EXPR)
99885b3f 1961 output_add_string (scratch_buffer, "sizeof (");
bb20cc46 1962 else
abff8e06
JM
1963 {
1964 my_friendly_assert (TREE_CODE (t) == ALIGNOF_EXPR, 0);
99885b3f 1965 output_add_string (scratch_buffer, "__alignof__ (");
abff8e06 1966 }
2f939d94 1967 if (TYPE_P (TREE_OPERAND (t, 0)))
9e93bc9d 1968 dump_type (TREE_OPERAND (t, 0), flags);
e76a2646 1969 else
44978276 1970 dump_expr (TREE_OPERAND (t, 0), flags);
99885b3f 1971 print_right_paren (scratch_buffer);
e76a2646 1972 break;
5566b478 1973
da20811c 1974 case DEFAULT_ARG:
99885b3f 1975 print_identifier (scratch_buffer, "<unparsed>");
da20811c
JM
1976 break;
1977
6748b643
JM
1978 case TRY_CATCH_EXPR:
1979 case WITH_CLEANUP_EXPR:
1980 case CLEANUP_POINT_EXPR:
9e93bc9d 1981 dump_expr (TREE_OPERAND (t, 0), flags);
6748b643
JM
1982 break;
1983
40242ccf 1984 case PSEUDO_DTOR_EXPR:
9e93bc9d 1985 dump_expr (TREE_OPERAND (t, 2), flags);
99885b3f 1986 output_add_character (scratch_buffer, '.');
9e93bc9d 1987 dump_type (TREE_OPERAND (t, 0), flags);
99885b3f 1988 output_add_string (scratch_buffer, "::~");
9e93bc9d 1989 dump_type (TREE_OPERAND (t, 1), flags);
40242ccf
MM
1990 break;
1991
7ac7b28f 1992 case TEMPLATE_ID_EXPR:
9e93bc9d 1993 dump_decl (t, flags);
7ac7b28f
MM
1994 break;
1995
558475f0
MM
1996 case STMT_EXPR:
1997 /* We don't yet have a way of dumping statements in a
1998 human-readable format. */
99885b3f 1999 output_add_string (scratch_buffer, "({...})");
558475f0
MM
2000 break;
2001
0045e0bc 2002 case BIND_EXPR:
52a11cbf 2003 output_add_character (scratch_buffer, '{');
761f0855 2004 dump_expr (TREE_OPERAND (t, 1), flags & ~TFF_EXPR_IN_PARENS);
99885b3f 2005 output_add_character (scratch_buffer, '}');
0045e0bc 2006 break;
bb20cc46 2007
0045e0bc 2008 case LOOP_EXPR:
99885b3f 2009 output_add_string (scratch_buffer, "while (1) { ");
761f0855 2010 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
99885b3f 2011 output_add_character (scratch_buffer, '}');
0045e0bc
MM
2012 break;
2013
2014 case EXIT_EXPR:
99885b3f 2015 output_add_string (scratch_buffer, "if (");
761f0855 2016 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
99885b3f 2017 output_add_string (scratch_buffer, ") break; ");
0045e0bc
MM
2018 break;
2019
a723baf1 2020 case BASELINK:
bd83b409 2021 dump_expr (get_first_fn (t), flags & ~TFF_EXPR_IN_PARENS);
a723baf1
MM
2022 break;
2023
bb20cc46 2024 /* else fall through */
00595019 2025
8d08fdba
MS
2026 /* This list is incomplete, but should suffice for now.
2027 It is very important that `sorry' does not call
2028 `report_error_function'. That could cause an infinite loop. */
2029 default:
99885b3f 2030 sorry_for_unsupported_tree (t);
8d08fdba
MS
2031 /* fall through to ERROR_MARK... */
2032 case ERROR_MARK:
99885b3f 2033 print_identifier (scratch_buffer, "<expression error>");
8d08fdba
MS
2034 break;
2035 }
2036}
2037
2038static void
3f8548e7 2039dump_binary_op (const char *opstring, tree t, int flags)
8d08fdba 2040{
99885b3f 2041 print_left_paren (scratch_buffer);
761f0855 2042 dump_expr (TREE_OPERAND (t, 0), flags | TFF_EXPR_IN_PARENS);
99885b3f 2043 output_add_space (scratch_buffer);
2adeacc9 2044 if (opstring)
99885b3f 2045 print_identifier (scratch_buffer, opstring);
2adeacc9 2046 else
99885b3f
GDR
2047 print_identifier (scratch_buffer, "<unknown operator>");
2048 output_add_space (scratch_buffer);
761f0855 2049 dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
99885b3f 2050 print_right_paren (scratch_buffer);
8d08fdba
MS
2051}
2052
2053static void
3f8548e7 2054dump_unary_op (const char *opstring, tree t, int flags)
8d08fdba 2055{
761f0855 2056 if (flags & TFF_EXPR_IN_PARENS)
99885b3f
GDR
2057 print_left_paren (scratch_buffer);
2058 print_identifier (scratch_buffer, opstring);
761f0855
GDR
2059 dump_expr (TREE_OPERAND (t, 0), flags & ~TFF_EXPR_IN_PARENS);
2060 if (flags & TFF_EXPR_IN_PARENS)
99885b3f 2061 print_right_paren (scratch_buffer);
8d08fdba
MS
2062}
2063
761f0855 2064/* Exported interface to stringifying types, exprs and decls under TFF_*
9e93bc9d 2065 control. */
4995028c 2066
e1def31b 2067const char *
3f8548e7 2068type_as_string (tree typ, int flags)
8d08fdba 2069{
99885b3f 2070 reinit_global_formatting_buffer ();
4995028c 2071
9e93bc9d
NS
2072 dump_type (typ, flags);
2073
99885b3f 2074 return output_finalize_message (scratch_buffer);
8d08fdba
MS
2075}
2076
e1def31b 2077const char *
3f8548e7 2078expr_as_string (tree decl, int flags)
8d08fdba 2079{
99885b3f 2080 reinit_global_formatting_buffer ();
8d08fdba 2081
9e93bc9d 2082 dump_expr (decl, flags);
8d08fdba 2083
99885b3f 2084 return output_finalize_message (scratch_buffer);
8d08fdba
MS
2085}
2086
e1def31b 2087const char *
3f8548e7 2088decl_as_string (tree decl, int flags)
8d08fdba 2089{
99885b3f 2090 reinit_global_formatting_buffer ();
8d08fdba 2091
9e93bc9d 2092 dump_decl (decl, flags);
8d08fdba 2093
99885b3f 2094 return output_finalize_message (scratch_buffer);
8d08fdba
MS
2095}
2096
e1def31b 2097const char *
3f8548e7 2098context_as_string (tree context, int flags)
8d08fdba 2099{
99885b3f 2100 reinit_global_formatting_buffer ();
bb20cc46 2101
9e93bc9d 2102 dump_scope (context, flags);
bb20cc46 2103
99885b3f 2104 return output_finalize_message (scratch_buffer);
8d08fdba
MS
2105}
2106
7afff7cf 2107/* Generate the three forms of printable names for cxx_printable_name. */
2ba25f50 2108
e1def31b 2109const char *
3f8548e7 2110lang_decl_name (tree decl, int v)
2ba25f50
MS
2111{
2112 if (v >= 2)
761f0855 2113 return decl_as_string (decl, TFF_DECL_SPECIFIERS);
2ba25f50 2114
99885b3f 2115 reinit_global_formatting_buffer ();
2ba25f50 2116
6eb3bb27 2117 if (v == 1 && DECL_CLASS_SCOPE_P (decl))
2ba25f50 2118 {
761f0855 2119 dump_type (CP_DECL_CONTEXT (decl), TFF_PLAIN_IDENTIFIER);
99885b3f 2120 print_scope_operator (scratch_buffer);
2ba25f50
MS
2121 }
2122
2123 if (TREE_CODE (decl) == FUNCTION_DECL)
761f0855 2124 dump_function_name (decl, TFF_PLAIN_IDENTIFIER);
2ba25f50 2125 else
761f0855 2126 dump_decl (DECL_NAME (decl), TFF_PLAIN_IDENTIFIER);
2ba25f50 2127
99885b3f 2128 return output_finalize_message (scratch_buffer);
2ba25f50 2129}
2ba25f50 2130
9a472a42
NS
2131static location_t
2132location_of (tree t)
8d08fdba 2133{
741f2839 2134 if (TREE_CODE (t) == PARM_DECL && DECL_CONTEXT (t))
9a472a42 2135 t = DECL_CONTEXT (t);
2f939d94 2136 else if (TYPE_P (t))
9a472a42 2137 t = TYPE_MAIN_DECL (t);
8f032717 2138 else if (TREE_CODE (t) == OVERLOAD)
9a472a42
NS
2139 t = OVL_FUNCTION (t);
2140
2141 return DECL_SOURCE_LOCATION (t);
8d08fdba
MS
2142}
2143
33bd39a2 2144/* Now the interfaces from error et al to dump_type et al. Each takes an
761f0855 2145 on/off VERBOSE flag and supply the appropriate TFF_ flags to a dump_
9e93bc9d
NS
2146 function. */
2147
2148static const char *
3f8548e7 2149decl_to_string (tree decl, int verbose)
9e93bc9d 2150{
761f0855 2151 int flags = 0;
d67cdbc3 2152
9e93bc9d
NS
2153 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == RECORD_TYPE
2154 || TREE_CODE (decl) == UNION_TYPE || TREE_CODE (decl) == ENUMERAL_TYPE)
761f0855 2155 flags = TFF_CLASS_KEY_OR_ENUM;
9e93bc9d 2156 if (verbose)
761f0855 2157 flags |= TFF_DECL_SPECIFIERS | TFF_FUNCTION_DEFAULT_ARGUMENTS;
9e93bc9d 2158 else if (TREE_CODE (decl) == FUNCTION_DECL)
761f0855
GDR
2159 flags |= TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE;
2160 flags |= TFF_TEMPLATE_HEADER;
bb20cc46 2161
99885b3f 2162 reinit_global_formatting_buffer ();
9e93bc9d
NS
2163
2164 dump_decl (decl, flags);
2165
99885b3f 2166 return output_finalize_message (scratch_buffer);
9e93bc9d
NS
2167}
2168
2169static const char *
4e3f84b7 2170expr_to_string (tree decl)
9e93bc9d 2171{
99885b3f 2172 reinit_global_formatting_buffer ();
9e93bc9d
NS
2173
2174 dump_expr (decl, 0);
2175
99885b3f 2176 return output_finalize_message (scratch_buffer);
9e93bc9d
NS
2177}
2178
2179static const char *
3f8548e7 2180fndecl_to_string (tree fndecl, int verbose)
9e93bc9d 2181{
761f0855 2182 int flags;
bb20cc46 2183
761f0855 2184 flags = TFF_EXCEPTION_SPECIFICATION | TFF_DECL_SPECIFIERS;
9e93bc9d 2185 if (verbose)
761f0855 2186 flags |= TFF_FUNCTION_DEFAULT_ARGUMENTS;
99885b3f 2187 reinit_global_formatting_buffer ();
9e93bc9d
NS
2188
2189 dump_decl (fndecl, flags);
bb20cc46 2190
99885b3f 2191 return output_finalize_message (scratch_buffer);
9e93bc9d
NS
2192}
2193
2194
2195static const char *
4e3f84b7 2196code_to_string (enum tree_code c)
8d08fdba
MS
2197{
2198 return tree_code_name [c];
2199}
2200
421844e7 2201const char *
4e3f84b7 2202language_to_string (enum languages c)
8d08fdba
MS
2203{
2204 switch (c)
2205 {
2206 case lang_c:
2207 return "C";
2208
2209 case lang_cplusplus:
2210 return "C++";
2211
31b72b87
PB
2212 case lang_java:
2213 return "Java";
2214
8d08fdba 2215 default:
a98facb0 2216 abort ();
8926095f 2217 return 0;
8d08fdba
MS
2218 }
2219}
2220
2221/* Return the proper printed version of a parameter to a C++ function. */
e92cc029 2222
9e93bc9d 2223static const char *
4e3f84b7 2224parm_to_string (int p)
8d08fdba
MS
2225{
2226 if (p < 0)
4e3f84b7
GDR
2227 output_add_string (scratch_buffer, "'this'");
2228 else
2229 output_decimal (scratch_buffer, p + 1);
8d08fdba 2230
4e3f84b7 2231 return output_finalize_message (scratch_buffer);
8d08fdba
MS
2232}
2233
9e93bc9d 2234static const char *
4e3f84b7 2235op_to_string (enum tree_code p)
8d08fdba 2236{
596ea4e5 2237 tree id;
8d08fdba 2238
596ea4e5 2239 id = operator_name_info[(int) p].identifier;
4e3f84b7 2240 return id ? IDENTIFIER_POINTER (id) : "<unknown>";
8d08fdba
MS
2241}
2242
9e93bc9d 2243static const char *
3f8548e7 2244type_to_string (tree typ, int verbose)
9e93bc9d 2245{
761f0855 2246 int flags;
bb20cc46 2247
9e93bc9d
NS
2248 flags = 0;
2249 if (verbose)
761f0855
GDR
2250 flags |= TFF_CLASS_KEY_OR_ENUM;
2251 flags |= TFF_TEMPLATE_HEADER;
bb20cc46 2252
99885b3f 2253 reinit_global_formatting_buffer ();
9e93bc9d
NS
2254
2255 dump_type (typ, flags);
2256
99885b3f 2257 return output_finalize_message (scratch_buffer);
9e93bc9d
NS
2258}
2259
2260static const char *
4e3f84b7 2261assop_to_string (enum tree_code p)
c91a56d2 2262{
596ea4e5 2263 tree id;
c91a56d2 2264
596ea4e5
AS
2265 id = assignment_operator_name_info[(int) p].identifier;
2266 return id ? IDENTIFIER_POINTER (id) : "{unknown}";
c91a56d2
MS
2267}
2268
9e93bc9d 2269static const char *
3f8548e7 2270args_to_string (tree p, int verbose)
8d08fdba 2271{
761f0855 2272 int flags = 0;
9e93bc9d 2273 if (verbose)
761f0855 2274 flags |= TFF_CLASS_KEY_OR_ENUM;
bb20cc46 2275
8d08fdba 2276 if (p == NULL_TREE)
c73964b2 2277 return "";
8d08fdba 2278
2f939d94 2279 if (TYPE_P (TREE_VALUE (p)))
9e93bc9d 2280 return type_as_string (p, flags);
c73964b2 2281
99885b3f 2282 reinit_global_formatting_buffer ();
c73964b2
MS
2283 for (; p; p = TREE_CHAIN (p))
2284 {
a6967cc0 2285 if (TREE_VALUE (p) == null_node)
99885b3f 2286 print_identifier (scratch_buffer, "NULL");
a6967cc0 2287 else
9e93bc9d 2288 dump_type (error_type (TREE_VALUE (p)), flags);
c73964b2 2289 if (TREE_CHAIN (p))
99885b3f 2290 separate_with_comma (scratch_buffer);
c73964b2 2291 }
99885b3f 2292 return output_finalize_message (scratch_buffer);
8d08fdba 2293}
f30432d7 2294
9e93bc9d 2295static const char *
3f8548e7 2296cv_to_string (tree p, int v)
f30432d7 2297{
99885b3f 2298 reinit_global_formatting_buffer ();
f30432d7 2299
ea50ad82 2300 dump_qualifiers (p, v ? before : none);
f30432d7 2301
99885b3f 2302 return output_finalize_message (scratch_buffer);
f30432d7 2303}
cb753e49 2304
7cb32822
NB
2305/* Langhook for print_error_function. */
2306void
3f8548e7 2307cxx_print_error_function (diagnostic_context *context, const char *file)
a72462a4 2308{
7cb32822 2309 lhd_print_error_function (context, file);
47b69537
GDR
2310 output_set_prefix (&context->buffer, file);
2311 maybe_print_instantiation_context (context);
a72462a4
GDR
2312}
2313
cb753e49 2314static void
3f8548e7
GDR
2315cp_diagnostic_starter (diagnostic_context *context,
2316 diagnostic_info *diagnostic)
cb753e49 2317{
47b69537
GDR
2318 diagnostic_report_current_module (context);
2319 cp_print_error_function (context, diagnostic);
2320 maybe_print_instantiation_context (context);
2321 output_set_prefix (&context->buffer, diagnostic_build_prefix (diagnostic));
cb753e49
GDR
2322}
2323
2324static void
3f8548e7
GDR
2325cp_diagnostic_finalizer (diagnostic_context *context,
2326 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
cb753e49 2327{
47b69537 2328 output_destroy_prefix (&context->buffer);
cb753e49
GDR
2329}
2330
2331/* Print current function onto BUFFER, in the process of reporting
2332 a diagnostic message. Called from cp_diagnostic_starter. */
2333static void
3f8548e7
GDR
2334cp_print_error_function (diagnostic_context *context,
2335 diagnostic_info *diagnostic)
cb753e49 2336{
47b69537 2337 if (diagnostic_last_function_changed (context))
cb753e49 2338 {
47b69537
GDR
2339 const char *old_prefix = output_prefix (&context->buffer);
2340 char *new_prefix = diagnostic->location.file
2341 ? file_name_as_prefix (diagnostic->location.file)
cb753e49 2342 : NULL;
cb753e49 2343
47b69537 2344 output_set_prefix (&context->buffer, new_prefix);
bb20cc46 2345
cb753e49 2346 if (current_function_decl == NULL)
47b69537 2347 output_add_string (&context->buffer, "At global scope:");
cb753e49 2348 else
47b69537
GDR
2349 output_printf (&context->buffer, "In %s `%s':",
2350 function_category (current_function_decl),
2351 cxx_printable_name (current_function_decl, 2));
2352 output_add_newline (&context->buffer);
2353
2354 diagnostic_set_last_function (context);
2355 output_destroy_prefix (&context->buffer);
2356 context->buffer.state.prefix = old_prefix;
cb753e49
GDR
2357 }
2358}
2359
2360/* Returns a description of FUNCTION using standard terminology. */
2361static const char *
3f8548e7 2362function_category (tree fn)
cb753e49
GDR
2363{
2364 if (DECL_FUNCTION_MEMBER_P (fn))
2365 {
2366 if (DECL_STATIC_FUNCTION_P (fn))
2367 return "static member function";
2368 else if (DECL_COPY_CONSTRUCTOR_P (fn))
2369 return "copy constructor";
2370 else if (DECL_CONSTRUCTOR_P (fn))
2371 return "constructor";
2372 else if (DECL_DESTRUCTOR_P (fn))
2373 return "destructor";
2374 else
2375 return "member function";
2376 }
2377 else
2378 return "function";
2379}
2380
2381/* Report the full context of a current template instantiation,
2382 onto BUFFER. */
2383static void
3f8548e7 2384print_instantiation_full_context (diagnostic_context *context)
cb753e49
GDR
2385{
2386 tree p = current_instantiation ();
82a98427
NS
2387 location_t location = input_location;
2388
cb753e49
GDR
2389 if (p)
2390 {
2391 if (current_function_decl != TINST_DECL (p)
2392 && current_function_decl != NULL_TREE)
2393 /* We can get here during the processing of some synthesized
2394 method. Then, TINST_DECL (p) will be the function that's causing
2395 the synthesis. */
2396 ;
2397 else
2398 {
2399 if (current_function_decl == TINST_DECL (p))
2400 /* Avoid redundancy with the the "In function" line. */;
bb20cc46 2401 else
47b69537 2402 output_verbatim (&context->buffer,
82a98427 2403 "%s: In instantiation of `%s':\n", location.file,
cb753e49 2404 decl_as_string (TINST_DECL (p),
761f0855 2405 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
bb20cc46 2406
82a98427
NS
2407 location.line = TINST_LINE (p);
2408 location.file = TINST_FILE (p);
cb753e49
GDR
2409 p = TREE_CHAIN (p);
2410 }
2411 }
bb20cc46 2412
35773471 2413 print_instantiation_partial_context (context, p, location);
cb753e49
GDR
2414}
2415
2416/* Same as above but less verbose. */
2417static void
3f8548e7 2418print_instantiation_partial_context (diagnostic_context *context,
35773471 2419 tree t, location_t loc)
cb753e49
GDR
2420{
2421 for (; t; t = TREE_CHAIN (t))
2422 {
2423 output_verbatim
35773471
NS
2424 (&context->buffer, "%s:%d: instantiated from `%s'\n",
2425 loc.file, loc.line,
2426 decl_as_string (TINST_DECL (t),
2427 TFF_DECL_SPECIFIERS | TFF_RETURN_TYPE));
2428 loc.line = TINST_LINE (t);
2429 loc.file = TINST_FILE (t);
cb753e49 2430 }
35773471
NS
2431 output_verbatim (&context->buffer, "%s:%d: instantiated from here\n",
2432 loc.file, loc.line);
cb753e49
GDR
2433}
2434
2435/* Called from cp_thing to print the template context for an error. */
2436static void
3f8548e7 2437maybe_print_instantiation_context (diagnostic_context *context)
cb753e49
GDR
2438{
2439 if (!problematic_instantiation_changed () || current_instantiation () == 0)
2440 return;
2441
2442 record_last_problematic_instantiation ();
47b69537 2443 print_instantiation_full_context (context);
cb753e49
GDR
2444}
2445
2446/* Report the bare minimum context of a template instantiation. */
2447void
3f8548e7 2448print_instantiation_context (void)
cb753e49
GDR
2449{
2450 print_instantiation_partial_context
35773471 2451 (global_dc, current_instantiation (), input_location);
47b69537 2452 diagnostic_flush_buffer (global_dc);
cb753e49 2453}
a1066c99
GDR
2454\f
2455/* Called from output_format -- during diagnostic message processing --
2456 to handle C++ specific format specifier with the following meanings:
2457 %A function argument-list.
749ced52 2458 %C tree code.
a1066c99
GDR
2459 %D declaration.
2460 %E expression.
2461 %F function declaration.
749ced52
ZW
2462 %L language as used in extern "lang".
2463 %O binary operator.
a1066c99 2464 %P function parameter whose position is indicated by an integer.
749ced52 2465 %Q assignment operator.
a1066c99
GDR
2466 %T type.
2467 %V cv-qualifier. */
47b69537 2468static bool
3f8548e7 2469cp_printer (output_buffer *buffer, text_info *text)
a1066c99 2470{
749ced52
ZW
2471 int verbose = 0;
2472 const char *result;
47b69537
GDR
2473#define next_tree va_arg (*text->args_ptr, tree)
2474#define next_tcode va_arg (*text->args_ptr, enum tree_code)
2475#define next_lang va_arg (*text->args_ptr, enum languages)
2476#define next_int va_arg (*text->args_ptr, int)
2477
2478 if (*text->format_spec == '+')
2479 ++text->format_spec;
2480 if (*text->format_spec == '#')
a1066c99 2481 {
749ced52 2482 verbose = 1;
47b69537 2483 ++text->format_spec;
a1066c99
GDR
2484 }
2485
47b69537 2486 switch (*text->format_spec)
22a4158c 2487 {
749ced52 2488 case 'A': result = args_to_string (next_tree, verbose); break;
4e3f84b7 2489 case 'C': result = code_to_string (next_tcode); break;
749ced52 2490 case 'D': result = decl_to_string (next_tree, verbose); break;
4e3f84b7 2491 case 'E': result = expr_to_string (next_tree); break;
749ced52 2492 case 'F': result = fndecl_to_string (next_tree, verbose); break;
4e3f84b7
GDR
2493 case 'L': result = language_to_string (next_lang); break;
2494 case 'O': result = op_to_string (next_tcode); break;
2495 case 'P': result = parm_to_string (next_int); break;
2496 case 'Q': result = assop_to_string (next_tcode); break;
749ced52
ZW
2497 case 'T': result = type_to_string (next_tree, verbose); break;
2498 case 'V': result = cv_to_string (next_tree, verbose); break;
2499
22a4158c 2500 default:
47b69537 2501 return false;
a1066c99 2502 }
bb20cc46 2503
749ced52 2504 output_add_string (buffer, result);
47b69537 2505 return true;
749ced52
ZW
2506#undef next_tree
2507#undef next_tcode
2508#undef next_lang
2509#undef next_int
a1066c99
GDR
2510}
2511
a1066c99 2512static void
3f8548e7 2513print_non_consecutive_character (output_buffer *buffer, int c)
c3e76028 2514{
749ced52 2515 const char *p = output_last_position (buffer);
c3e76028 2516
749ced52
ZW
2517 if (p != NULL && *p == c)
2518 output_add_space (buffer);
2519 output_add_character (buffer, c);
c3e76028
GDR
2520}
2521
749ced52
ZW
2522/* These are temporary wrapper functions which handle the historic
2523 behavior of cp_*_at. */
c3e76028 2524
749ced52 2525static tree
3f8548e7 2526locate_error (const char *msgid, va_list ap)
c3e76028 2527{
749ced52
ZW
2528 tree here = 0, t;
2529 int plus = 0;
2530 const char *f;
c3e76028 2531
749ced52 2532 for (f = msgid; *f; f++)
c3e76028 2533 {
749ced52
ZW
2534 plus = 0;
2535 if (*f == '%')
2536 {
2537 f++;
2538 if (*f == '+')
2539 f++, plus = 1;
2540 if (*f == '#')
2541 f++;
c3e76028 2542
749ced52
ZW
2543 switch (*f)
2544 {
2545 /* Just ignore these possibilities. */
2546 case '%': break;
2547 case 'd': (void) va_arg (ap, int); break;
2548 case 's': (void) va_arg (ap, char *); break;
2549 case 'L': (void) va_arg (ap, enum languages); break;
2550 case 'C':
2551 case 'O':
2552 case 'Q': (void) va_arg (ap, enum tree_code); break;
2553
2554 /* These take a tree, which may be where the error is
2555 located. */
2556 case 'A':
2557 case 'D':
2558 case 'E':
2559 case 'F':
2560 case 'P':
2561 case 'T':
2562 case 'V':
2563 t = va_arg (ap, tree);
2564 if (!here || plus)
2565 here = t;
2566 break;
c3e76028 2567
749ced52
ZW
2568 default:
2569 errorcount = 0; /* damn ICE suppression */
2570 internal_error ("unexpected letter `%c' in locate_error\n", *f);
2571 }
2572 }
c3e76028
GDR
2573 }
2574
749ced52
ZW
2575 if (here == 0)
2576 here = va_arg (ap, tree);
c3e76028 2577
749ced52 2578 return here;
c3e76028
GDR
2579}
2580
c3e76028 2581
749ced52 2582void
e34d07f2 2583cp_error_at (const char *msgid, ...)
a1066c99 2584{
749ced52 2585 tree here;
47b69537 2586 diagnostic_info diagnostic;
e34d07f2 2587 va_list ap;
bb20cc46 2588
e34d07f2 2589 va_start (ap, msgid);
749ced52 2590 here = locate_error (msgid, ap);
e34d07f2 2591 va_end (ap);
c3e76028 2592
e34d07f2 2593 va_start (ap, msgid);
47b69537 2594 diagnostic_set_info (&diagnostic, msgid, &ap,
9a472a42 2595 location_of (here), DK_ERROR);
47b69537 2596 report_diagnostic (&diagnostic);
e34d07f2 2597 va_end (ap);
c3e76028
GDR
2598}
2599
749ced52 2600void
e34d07f2 2601cp_warning_at (const char *msgid, ...)
c3e76028 2602{
749ced52 2603 tree here;
47b69537 2604 diagnostic_info diagnostic;
e34d07f2 2605 va_list ap;
c3e76028 2606
e34d07f2 2607 va_start (ap, msgid);
749ced52 2608 here = locate_error (msgid, ap);
e34d07f2 2609 va_end (ap);
c3e76028 2610
e34d07f2 2611 va_start (ap, msgid);
47b69537 2612 diagnostic_set_info (&diagnostic, msgid, &ap,
9a472a42 2613 location_of (here), DK_WARNING);
47b69537 2614 report_diagnostic (&diagnostic);
e34d07f2 2615 va_end (ap);
c3e76028
GDR
2616}
2617
749ced52 2618void
e34d07f2 2619cp_pedwarn_at (const char *msgid, ...)
c3e76028 2620{
749ced52 2621 tree here;
47b69537 2622 diagnostic_info diagnostic;
e34d07f2 2623 va_list ap;
c3e76028 2624
e34d07f2 2625 va_start (ap, msgid);
749ced52 2626 here = locate_error (msgid, ap);
e34d07f2 2627 va_end (ap);
c3e76028 2628
e34d07f2 2629 va_start (ap, msgid);
47b69537 2630 diagnostic_set_info (&diagnostic, msgid, &ap,
9a472a42 2631 location_of (here), pedantic_error_kind());
47b69537 2632 report_diagnostic (&diagnostic);
e34d07f2 2633 va_end (ap);
c3e76028 2634}
This page took 1.562286 seconds and 5 git commands to generate.