]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/error.c
(distribute_notes...
[gcc.git] / gcc / cp / error.c
CommitLineData
8d08fdba
MS
1/* Call-backs for C++ error reporting.
2 This code is non-reentrant.
3 Copyright (C) 1993 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
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
12GNU CC is distributed in the hope that it will be useful,
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
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "config.h"
22#include "tree.h"
23#include "cp-tree.h"
24#include "obstack.h"
25#include <ctype.h>
26
27typedef char* cp_printer ();
28
29#define A args_as_string
30#define C code_as_string
31#define D decl_as_string
32#define E expr_as_string
33#define L language_as_string
34#define O op_as_string
35#define P parm_as_string
36#define T type_as_string
37
38#define _ (cp_printer *) 0
39cp_printer * cp_printers[256] =
40{
41/*0 1 2 3 4 5 6 7 8 9 A B C D E F */
42 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x00 */
43 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x10 */
44 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x20 */
45 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x30 */
46 _, A, _, C, D, E, _, _, _, _, _, _, L, _, _, O, /* 0x40 */
47 P, _, _, _, T, _, _, _, _, _, _, _, _, _, _, _, /* 0x50 */
48 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x60 */
49 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x70 */
50};
51#undef C
52#undef D
53#undef E
54#undef L
55#undef O
56#undef P
57#undef T
58#undef _
59
60#define obstack_chunk_alloc xmalloc
61#define obstack_chunk_free free
62
63/* Obstack where we build text strings for overloading, etc. */
64static struct obstack scratch_obstack;
65static char *scratch_firstobj;
66
67# define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
68# define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
69# define OB_PUTC2(C1,C2) \
70 (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
71# define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
72# define OB_PUTID(ID) \
73 (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID), \
74 IDENTIFIER_LENGTH (ID)))
75# define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
76# define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
77# define OB_PUTI(CST) do { sprintf (digit_buffer, "%d", (CST)); \
78 OB_PUTCP (digit_buffer); } while (0)
51c184be 79# define OB_UNPUT(N) obstack_blank (&scratch_obstack, - (N));
8d08fdba
MS
80
81# define NEXT_CODE(t) (TREE_CODE (TREE_TYPE (t)))
82
83static void dump_type (), dump_decl (), dump_function_decl ();
84static void dump_expr (), dump_unary_op (), dump_binary_op ();
85static void dump_aggr_type (), dump_type_prefix (), dump_type_suffix ();
86static void dump_function_name ();
87
88void
89init_error ()
90{
91 gcc_obstack_init (&scratch_obstack);
92 scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
93}
94
8d08fdba
MS
95enum pad { none, before, after };
96
97static void
98dump_readonly_or_volatile (t, p)
99 tree t;
100 enum pad p;
101{
102 if (TYPE_READONLY (t) || TYPE_VOLATILE (t))
103 {
104 if (p == before) OB_PUTC (' ');
105 if (TYPE_READONLY (t))
106 OB_PUTS ("const");
107 if (TYPE_VOLATILE (t))
108 OB_PUTS ("volatile");
109 if (p == after) OB_PUTC (' ');
110 }
111}
112
113/* This must be large enough to hold any printed integer or floating-point
114 value. */
115static char digit_buffer[128];
116
117/* Dump into the obstack a human-readable equivalent of TYPE. */
118static void
119dump_type (t, v)
120 tree t;
121 int v; /* verbose? */
122{
123 if (t == NULL_TREE)
124 return;
125
126 if (TYPE_PTRMEMFUNC_P (t))
127 goto offset_type;
128
129 switch (TREE_CODE (t))
130 {
131 case ERROR_MARK:
51c184be 132 OB_PUTS ("{error}");
8d08fdba
MS
133 break;
134
135 case UNKNOWN_TYPE:
51c184be 136 OB_PUTS ("{unknown type}");
8d08fdba
MS
137 break;
138
139 case TREE_LIST:
140 /* i.e. function taking no arguments */
141 if (t != void_list_node)
142 {
143 dump_type (TREE_VALUE (t), v);
144 /* Can this happen other than for default arguments? */
145 if (TREE_PURPOSE (t) && v)
146 {
147 OB_PUTS (" = ");
148 dump_expr (TREE_PURPOSE (t));
149 }
150 if (TREE_CHAIN (t))
151 {
152 if (TREE_CHAIN (t) != void_list_node)
153 {
154 OB_PUTC2 (',', ' ');
155 dump_type (TREE_CHAIN (t), v);
156 }
157 }
158 else OB_PUTS (" ...");
159 }
160 break;
161
162 case IDENTIFIER_NODE:
163 OB_PUTID (t);
164 break;
165
166 case TREE_VEC:
167 dump_type (BINFO_TYPE (t), v);
168 break;
169
170 case RECORD_TYPE:
171 case UNION_TYPE:
172 case ENUMERAL_TYPE:
173 if (TYPE_LANG_SPECIFIC (t)
174 && (IS_SIGNATURE_POINTER (t) || IS_SIGNATURE_REFERENCE (t)))
175 {
176 if (TYPE_READONLY (t) | TYPE_VOLATILE (t))
177 dump_readonly_or_volatile (t);
178 dump_type (SIGNATURE_TYPE (t), v);
179 if (IS_SIGNATURE_POINTER (t))
180 OB_PUTC ('*');
181 else
182 OB_PUTC ('&');
183 }
184 else
185 dump_aggr_type (t, v);
186 break;
187
188 case TYPE_DECL:
7177d104 189 dump_decl (t, v);
8d08fdba
MS
190 break;
191
192 case INTEGER_TYPE:
193 if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && TREE_UNSIGNED (t))
194 OB_PUTS ("unsigned ");
195 else if (TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && !TREE_UNSIGNED (t))
196 OB_PUTS ("signed ");
197
198 /* fall through. */
199 case REAL_TYPE:
200 case VOID_TYPE:
2986ae00 201 case BOOLEAN_TYPE:
8d08fdba
MS
202 dump_readonly_or_volatile (t, after);
203 OB_PUTID (TYPE_IDENTIFIER (t));
204 break;
205
206 case TEMPLATE_TYPE_PARM:
8d08fdba 207 OB_PUTID (TYPE_IDENTIFIER (t));
8d08fdba
MS
208 break;
209
210 case UNINSTANTIATED_P_TYPE:
211 OB_PUTID (DECL_NAME (UPT_TEMPLATE (t)));
212 OB_PUTS ("<...>");
213 break;
214
215 /* This is not always necessary for pointers and such, but doing this
216 reduces code size. */
217 case ARRAY_TYPE:
218 case POINTER_TYPE:
219 case REFERENCE_TYPE:
220 case OFFSET_TYPE:
221 offset_type:
222 case FUNCTION_TYPE:
223 case METHOD_TYPE:
224 dump_type_prefix (t, v);
225 dump_type_suffix (t, v);
226 break;
227
228 default:
51c184be
MS
229 sorry ("`%s' not supported by dump_type",
230 tree_code_name[(int) TREE_CODE (t)]);
8d08fdba
MS
231 }
232}
233
51c184be
MS
234static char *
235aggr_variety (t)
8d08fdba 236 tree t;
8d08fdba 237{
8d08fdba 238 if (TREE_CODE (t) == ENUMERAL_TYPE)
51c184be 239 return "enum";
8d08fdba 240 else if (TREE_CODE (t) == UNION_TYPE)
51c184be 241 return "union";
8d08fdba 242 else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
51c184be 243 return "class";
8d08fdba 244 else if (TYPE_LANG_SPECIFIC (t) && IS_SIGNATURE (t))
51c184be 245 return "signature";
8d08fdba 246 else
51c184be
MS
247 return "struct";
248}
249
250/* Print out a class declaration, in the form `class foo'. */
251static void
252dump_aggr_type (t, v)
253 tree t;
254 int v; /* verbose? */
255{
256 tree name;
257 char *variety = aggr_variety (t);
8d08fdba
MS
258
259 dump_readonly_or_volatile (t, after);
260
261 if (v > 0)
262 {
263 OB_PUTCP (variety);
264 OB_PUTC (' ');
265 }
266
267 name = TYPE_NAME (t);
268
269 if (DECL_CONTEXT (name))
270 {
271 /* FUNCTION_DECL or RECORD_TYPE */
272 dump_decl (DECL_CONTEXT (name), 0);
273 OB_PUTC2 (':', ':');
274 }
275
276 /* kludge around wierd behavior on g++.brendan/line1.C */
277 if (TREE_CODE (name) != IDENTIFIER_NODE)
278 name = DECL_NAME (name);
279
280 if (ANON_AGGRNAME_P (name))
281 {
51c184be 282 OB_PUTS ("{anonymous");
8d08fdba
MS
283 if (!v)
284 {
285 OB_PUTC (' ');
286 OB_PUTCP (variety);
287 }
51c184be 288 OB_PUTC ('}');
8d08fdba
MS
289 }
290 else
291 OB_PUTID (name);
292}
293
294/* Dump into the obstack the initial part of the output for a given type.
295 This is necessary when dealing with things like functions returning
296 functions. Examples:
297
298 return type of `int (* fee ())()': pointer -> function -> int. Both
299 pointer (and reference and offset) and function (and member) types must
300 deal with prefix and suffix.
301
302 Arrays must also do this for DECL nodes, like int a[], and for things like
303 int *[]&. */
304
305static void
306dump_type_prefix (t, v)
307 tree t;
308 int v; /* verbosity */
309{
310 if (TYPE_PTRMEMFUNC_P (t))
311 {
312 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
313 goto offset_type;
314 }
315
316 switch (TREE_CODE (t))
317 {
318 case POINTER_TYPE:
319 {
320 tree sub = TREE_TYPE (t);
321
322 dump_type_prefix (sub, v);
323 /* A tree for a member pointer looks like pointer to offset,
324 so let the OFFSET_TYPE case handle it. */
325 if (TREE_CODE (sub) != OFFSET_TYPE)
326 {
327 switch (TREE_CODE (sub))
328 {
329 /* We don't want int ( *)() */
330 case FUNCTION_TYPE:
331 case METHOD_TYPE:
332 break;
333
39211cd5
MS
334 case ARRAY_TYPE:
335 OB_PUTC2 (' ', '(');
336 break;
337
8d08fdba
MS
338 case POINTER_TYPE:
339 /* We don't want "char * *" */
340 if (! (TYPE_READONLY (sub) || TYPE_VOLATILE (sub)))
341 break;
342 /* But we do want "char *const *" */
343
344 default:
345 OB_PUTC (' ');
346 }
347 OB_PUTC ('*');
348 dump_readonly_or_volatile (t, none);
349 }
350 }
351 break;
352
353 case REFERENCE_TYPE:
354 {
355 tree sub = TREE_TYPE (t);
356 dump_type_prefix (sub, v);
357
358 switch (TREE_CODE (sub))
359 {
2986ae00
MS
360 case ARRAY_TYPE:
361 OB_PUTC2 (' ', '(');
362 break;
363
8d08fdba
MS
364 case POINTER_TYPE:
365 /* We don't want "char * &" */
366 if (! (TYPE_READONLY (sub) || TYPE_VOLATILE (sub)))
367 break;
368 /* But we do want "char *const &" */
369
370 default:
371 OB_PUTC (' ');
372 }
373 }
374 OB_PUTC ('&');
375 dump_readonly_or_volatile (t, none);
376 break;
377
378 case OFFSET_TYPE:
379 offset_type:
380 dump_type_prefix (TREE_TYPE (t), v);
51c184be
MS
381 if (TREE_CODE (t) == OFFSET_TYPE) /* pmfs deal with this in d_t_p */
382 {
383 OB_PUTC (' ');
384 dump_type (TYPE_OFFSET_BASETYPE (t), 0);
385 OB_PUTC2 (':', ':');
386 }
8d08fdba
MS
387 OB_PUTC ('*');
388 dump_readonly_or_volatile (t, none);
389 break;
390
391 /* Can only be reached through function pointer -- this would not be
392 correct if FUNCTION_DECLs used it. */
393 case FUNCTION_TYPE:
394 dump_type_prefix (TREE_TYPE (t), v);
395 OB_PUTC2 (' ', '(');
396 break;
397
398 case METHOD_TYPE:
399 dump_type_prefix (TREE_TYPE (t), v);
400 OB_PUTC2 (' ', '(');
401 dump_aggr_type (TYPE_METHOD_BASETYPE (t), 0);
402 OB_PUTC2 (':', ':');
403 break;
404
405 case ARRAY_TYPE:
406 dump_type_prefix (TREE_TYPE (t), v);
407 break;
408
409 case ENUMERAL_TYPE:
410 case ERROR_MARK:
411 case IDENTIFIER_NODE:
412 case INTEGER_TYPE:
2986ae00 413 case BOOLEAN_TYPE:
8d08fdba
MS
414 case REAL_TYPE:
415 case RECORD_TYPE:
416 case TEMPLATE_TYPE_PARM:
417 case TREE_LIST:
418 case TYPE_DECL:
419 case TREE_VEC:
420 case UNINSTANTIATED_P_TYPE:
421 case UNION_TYPE:
422 case UNKNOWN_TYPE:
423 case VOID_TYPE:
424 dump_type (t, v);
425 break;
426
427 default:
51c184be
MS
428 sorry ("`%s' not supported by dump_type_prefix",
429 tree_code_name[(int) TREE_CODE (t)]);
8d08fdba
MS
430 }
431}
432
433static void
434dump_type_suffix (t, v)
435 tree t;
436 int v; /* verbose? */
437{
438 if (TYPE_PTRMEMFUNC_P (t))
439 t = TYPE_PTRMEMFUNC_FN_TYPE (t);
440
441 switch (TREE_CODE (t))
442 {
443 case POINTER_TYPE:
444 case REFERENCE_TYPE:
445 case OFFSET_TYPE:
39211cd5
MS
446 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
447 OB_PUTC (')');
8d08fdba
MS
448 dump_type_suffix (TREE_TYPE (t), v);
449 break;
450
451 /* Can only be reached through function pointer */
452 case FUNCTION_TYPE:
453 case METHOD_TYPE:
454 {
455 tree arg;
456 OB_PUTC2 (')', '(');
457 arg = TYPE_ARG_TYPES (t);
458 if (TREE_CODE (t) == METHOD_TYPE)
459 arg = TREE_CHAIN (arg);
460
461 if (arg)
462 dump_type (arg, v);
463 else
464 OB_PUTS ("...");
465 OB_PUTC (')');
466 if (TREE_CODE (t) == METHOD_TYPE)
467 dump_readonly_or_volatile
468 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), before);
469 dump_type_suffix (TREE_TYPE (t), v);
470 break;
471 }
472
473 case ARRAY_TYPE:
474 OB_PUTC ('[');
475 if (TYPE_DOMAIN (t))
476 OB_PUTI (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (t))) + 1);
477 OB_PUTC (']');
478 dump_type_suffix (TREE_TYPE (t), v);
479 break;
480
481 case ENUMERAL_TYPE:
482 case ERROR_MARK:
483 case IDENTIFIER_NODE:
484 case INTEGER_TYPE:
2986ae00 485 case BOOLEAN_TYPE:
8d08fdba
MS
486 case REAL_TYPE:
487 case RECORD_TYPE:
488 case TEMPLATE_TYPE_PARM:
489 case TREE_LIST:
490 case TYPE_DECL:
491 case TREE_VEC:
492 case UNINSTANTIATED_P_TYPE:
493 case UNION_TYPE:
494 case UNKNOWN_TYPE:
495 case VOID_TYPE:
496 break;
497
498 default:
51c184be
MS
499 sorry ("`%s' not supported by dump_type_suffix",
500 tree_code_name[(int) TREE_CODE (t)]);
8d08fdba
MS
501 }
502}
503
504/* Return a function declaration which corresponds to the IDENTIFIER_NODE
505 argument. */
506tree
507ident_fndecl (t)
508 tree t;
509{
700f8a87 510 tree n = lookup_name (t, 0);
8d08fdba
MS
511
512 if (TREE_CODE (n) == FUNCTION_DECL)
513 return n;
514 else if (TREE_CODE (n) == TREE_LIST
515 && TREE_CODE (TREE_VALUE (n)) == FUNCTION_DECL)
516 return TREE_VALUE (n);
8926095f
MS
517
518 my_friendly_abort (66);
519 return NULL_TREE;
8d08fdba
MS
520}
521
522#ifndef NO_DOLLAR_IN_LABEL
523# define GLOBAL_THING "_GLOBAL_$"
524#else
525# ifndef NO_DOT_IN_LABEL
526# define GLOBAL_THING "_GLOBAL_."
527# else
528# define GLOBAL_THING "_GLOBAL__"
529# endif
530#endif
531
532#define GLOBAL_IORD_P(NODE) \
533 !strncmp(IDENTIFIER_POINTER(NODE),GLOBAL_THING,sizeof(GLOBAL_THING)-1)
534
535void
536dump_global_iord (t)
537 tree t;
538{
539 char *name = IDENTIFIER_POINTER (t);
540
541 OB_PUTS ("(static ");
542 if (name [sizeof (GLOBAL_THING) - 1] == 'I')
543 OB_PUTS ("initializers");
544 else if (name [sizeof (GLOBAL_THING) - 1] == 'D')
545 OB_PUTS ("destructors");
546 else
547 my_friendly_abort (352);
548
549 OB_PUTS (" for ");
550 OB_PUTCP (input_filename);
551 OB_PUTC (')');
552}
553
554static void
555dump_decl (t, v)
556 tree t;
557 int v; /* verbosity */
558{
559 if (t == NULL_TREE)
560 return;
561
562 switch (TREE_CODE (t))
563 {
564 case ERROR_MARK:
565 OB_PUTS (" /* decl error */ ");
566 break;
567
7177d104 568 case TYPE_DECL:
8d2733ca
MS
569 {
570 /* Don't say 'typedef class A' */
571 tree type = TREE_TYPE (t);
572 if (IS_AGGR_TYPE (type) && ! TYPE_PTRMEMFUNC_P (type)
573 && type == TYPE_MAIN_VARIANT (type))
574 {
575 dump_type (type, v);
576 break;
577 }
578 }
579 if (v > 0)
580 OB_PUTS ("typedef ");
581 goto general;
7177d104
MS
582 break;
583
8d08fdba
MS
584 case VAR_DECL:
585 if (VTABLE_NAME_P (DECL_NAME (t)))
586 {
587 OB_PUTS ("vtable for ");
588 dump_type (DECL_CONTEXT (t), v);
589 break;
590 }
591 /* else fall through */
592 case FIELD_DECL:
593 case PARM_DECL:
7177d104 594 general:
8d08fdba
MS
595 if (v > 0)
596 {
597 dump_type_prefix (TREE_TYPE (t), v);
7177d104 598 OB_PUTC (' ');
8d08fdba
MS
599 }
600 /* DECL_CLASS_CONTEXT isn't being set in some cases. Hmm... */
8d2733ca
MS
601 if (DECL_CONTEXT (t)
602 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
8d08fdba
MS
603 {
604 dump_type (DECL_CONTEXT (t), 0);
7177d104 605 OB_PUTC2 (':', ':');
8d08fdba
MS
606 }
607 if (DECL_NAME (t))
608 dump_decl (DECL_NAME (t), v);
609 else
51c184be 610 OB_PUTS ("{anon}");
7177d104
MS
611 if (v > 0)
612 dump_type_suffix (TREE_TYPE (t), v);
8d08fdba
MS
613 break;
614
615 case ARRAY_REF:
616 dump_decl (TREE_OPERAND (t, 0), v);
617 OB_PUTC ('[');
618 dump_decl (TREE_OPERAND (t, 1), v);
619 OB_PUTC (']');
620 break;
621
622 /* So that we can do dump_decl in dump_aggr_type and have it work for
623 both class and function scope. */
624 case RECORD_TYPE:
625 case UNION_TYPE:
626 case ENUMERAL_TYPE:
627 dump_type (t, v);
628 break;
629
8d08fdba
MS
630 case TYPE_EXPR:
631 my_friendly_abort (69);
632 break;
633
634 /* These special cases are duplicated here so that other functions
635 can feed identifiers to cp_error and get them demangled properly. */
636 case IDENTIFIER_NODE:
637 if (DESTRUCTOR_NAME_P (t))
638 {
639 OB_PUTC ('~');
640 dump_decl (DECL_NAME (ident_fndecl (t)), 0);
641 }
642 else if (IDENTIFIER_TYPENAME_P (t))
643 {
644 OB_PUTS ("operator ");
645 /* Not exactly IDENTIFIER_TYPE_VALUE. */
646 dump_type (TREE_TYPE (t), 0);
647 break;
648 }
649 else if (IDENTIFIER_OPNAME_P (t))
650 {
651 char *name_string = operator_name_string (t);
652 OB_PUTS ("operator ");
653 OB_PUTCP (name_string);
654 }
655 else
656 OB_PUTID (t);
657 break;
658
659 case FUNCTION_DECL:
660 if (GLOBAL_IORD_P (DECL_ASSEMBLER_NAME (t)))
661 dump_global_iord (DECL_ASSEMBLER_NAME (t));
662 else
663 dump_function_decl (t, v);
664 break;
665
666 case TEMPLATE_DECL:
51c184be
MS
667 {
668 tree args = DECL_TEMPLATE_PARMS (t);
669 int i, len = TREE_VEC_LENGTH (args);
670 OB_PUTS ("template <");
671 for (i = 0; i < len; i++)
672 {
673 tree arg = TREE_VEC_ELT (args, i);
a292b002
MS
674 tree defval = TREE_PURPOSE (arg);
675 arg = TREE_VALUE (arg);
676 if (TREE_CODE (arg) == TYPE_DECL)
51c184be
MS
677 {
678 OB_PUTS ("class ");
a292b002 679 OB_PUTID (DECL_NAME (arg));
51c184be
MS
680 }
681 else
682 dump_decl (arg, 1);
a292b002
MS
683
684 if (defval)
685 {
686 OB_PUTS (" = ");
687 dump_decl (defval, 1);
688 }
689
51c184be
MS
690 OB_PUTC2 (',', ' ');
691 }
692 OB_UNPUT (2);
693 OB_PUTC2 ('>', ' ');
8d08fdba 694
51c184be
MS
695 if (DECL_TEMPLATE_IS_CLASS (t))
696 {
697 OB_PUTS ("class ");
698 OB_PUTID (DECL_NAME (t));
699 }
700 else switch (NEXT_CODE (t))
701 {
702 case METHOD_TYPE:
703 case FUNCTION_TYPE:
704 dump_function_decl (t, v);
705 break;
706
707 default:
708 my_friendly_abort (353);
709 }
710 }
8d08fdba
MS
711 break;
712
713 case LABEL_DECL:
714 OB_PUTID (DECL_NAME (t));
715 break;
716
717 case CONST_DECL:
718 if (NEXT_CODE (t) == ENUMERAL_TYPE)
8d2733ca 719 goto general;
8d08fdba
MS
720 else
721 dump_expr (DECL_INITIAL (t), 0);
722 break;
723
724 default:
51c184be
MS
725 sorry ("`%s' not supported by dump_decl",
726 tree_code_name[(int) TREE_CODE (t)]);
8d08fdba
MS
727 }
728}
729
730/* Pretty printing for announce_function. T is the declaration of the
731 function we are interested in seeing. V is non-zero if we should print
732 the type that this function returns. */
733
734static void
735dump_function_decl (t, v)
736 tree t;
737 int v;
738{
739 tree name = DECL_ASSEMBLER_NAME (t);
740 tree fntype = TREE_TYPE (t);
741 tree parmtypes = TYPE_ARG_TYPES (fntype);
742 tree cname = NULL_TREE;
8d08fdba
MS
743
744 /* Friends have DECL_CLASS_CONTEXT set, but not DECL_CONTEXT. */
745 if (DECL_CONTEXT (t))
746 cname = DECL_CLASS_CONTEXT (t);
747 /* this is for partially instantiated template methods */
748 else if (TREE_CODE (fntype) == METHOD_TYPE)
749 cname = TREE_TYPE (TREE_VALUE (parmtypes));
750
751 v = (v > 0);
752
753 if (v)
754 {
755 if (DECL_STATIC_FUNCTION_P (t))
756 OB_PUTS ("static ");
757
758 if (! IDENTIFIER_TYPENAME_P (name)
759 && ! DECL_CONSTRUCTOR_P (t)
760 && ! DESTRUCTOR_NAME_P (name))
761 {
762 dump_type_prefix (TREE_TYPE (fntype), 1);
763 OB_PUTC (' ');
764 }
765 }
766
767 if (cname)
768 {
769 dump_type (cname, 0);
770 OB_PUTC2 (':', ':');
771 if (TREE_CODE (fntype) == METHOD_TYPE && parmtypes)
772 parmtypes = TREE_CHAIN (parmtypes);
773 if (DECL_CONSTRUCTOR_FOR_VBASE_P (t))
774 /* Skip past "in_charge" identifier. */
775 parmtypes = TREE_CHAIN (parmtypes);
776 }
777
778 if (DESTRUCTOR_NAME_P (name))
779 parmtypes = TREE_CHAIN (parmtypes);
780
781 dump_function_name (t);
782
783 OB_PUTC ('(');
784
785 if (parmtypes)
786 dump_type (parmtypes, v);
787 else
788 OB_PUTS ("...");
789
790 OB_PUTC (')');
791
792 if (v && ! IDENTIFIER_TYPENAME_P (name))
793 dump_type_suffix (TREE_TYPE (fntype), 1);
794
795 if (TREE_CODE (fntype) == METHOD_TYPE)
796 {
797 if (IS_SIGNATURE (cname))
798 /* We look at the type pointed to by the `optr' field of `this.' */
799 dump_readonly_or_volatile
800 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (TYPE_ARG_TYPES (fntype))))), before);
801 else
802 dump_readonly_or_volatile
803 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))), before);
804 }
805}
806
807/* Handle the function name for a FUNCTION_DECL node, grokking operators
808 and destructors properly. */
809static void
810dump_function_name (t)
811 tree t;
812{
813 tree name = DECL_NAME (t);
814
815 /* There ought to be a better way to find out whether or not something is
816 a destructor. */
817 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
818 {
819 OB_PUTC ('~');
820 dump_decl (name, 0);
821 }
822 else if (IDENTIFIER_TYPENAME_P (name))
823 {
824 /* This cannot use the hack that the operator's return
825 type is stashed off of its name because it may be
826 used for error reporting. In the case of conflicting
827 declarations, both will have the same name, yet
828 the types will be different, hence the TREE_TYPE field
829 of the first name will be clobbered by the second. */
830 OB_PUTS ("operator ");
831 dump_type (TREE_TYPE (TREE_TYPE (t)), 0);
832 }
833 else if (IDENTIFIER_OPNAME_P (name))
834 {
835 char *name_string = operator_name_string (name);
836 OB_PUTS ("operator ");
837 OB_PUTCP (name_string);
838 }
839 else
840 dump_decl (name, 0);
841}
842
843static void
844dump_char (c)
845 char c;
846{
847 switch (c)
848 {
a0a33927 849 case TARGET_NEWLINE:
8d08fdba
MS
850 OB_PUTS ("\\n");
851 break;
a0a33927 852 case TARGET_TAB:
8d08fdba
MS
853 OB_PUTS ("\\t");
854 break;
a0a33927 855 case TARGET_VT:
8d08fdba
MS
856 OB_PUTS ("\\v");
857 break;
a0a33927 858 case TARGET_BS:
8d08fdba
MS
859 OB_PUTS ("\\b");
860 break;
a0a33927 861 case TARGET_CR:
8d08fdba
MS
862 OB_PUTS ("\\r");
863 break;
a0a33927 864 case TARGET_FF:
8d08fdba
MS
865 OB_PUTS ("\\f");
866 break;
a0a33927 867 case TARGET_BELL:
8d08fdba
MS
868 OB_PUTS ("\\a");
869 break;
870 case '\\':
871 OB_PUTS ("\\\\");
872 break;
873 case '\'':
874 OB_PUTS ("\\'");
875 break;
876 case '\"':
877 OB_PUTS ("\\\"");
878 break;
879 default:
880 if (isprint (c))
881 OB_PUTC (c);
882 else
883 {
884 sprintf (digit_buffer, "\\%03o", (int) c);
885 OB_PUTCP (digit_buffer);
886 }
887 }
888}
889
890/* Print out a list of initializers (subr of dump_expr) */
891static void
892dump_expr_list (l)
893 tree l;
894{
895 while (l)
896 {
897 dump_expr (TREE_VALUE (l), 0);
898 if (TREE_CHAIN (l))
899 OB_PUTC2 (',', ' ');
900 l = TREE_CHAIN (l);
901 }
902}
903
904/* Print out an expression */
905static void
906dump_expr (t, nop)
907 tree t;
908 int nop; /* suppress parens */
909{
910 switch (TREE_CODE (t))
911 {
912 case VAR_DECL:
913 case PARM_DECL:
914 case FIELD_DECL:
915 case CONST_DECL:
916 case FUNCTION_DECL:
917 dump_decl (t, -1);
918 break;
919
920 case INTEGER_CST:
921 {
922 tree type = TREE_TYPE (t);
923 my_friendly_assert (type != 0, 81);
924
925 /* If it's an enum, output its tag, rather than its value. */
926 if (TREE_CODE (type) == ENUMERAL_TYPE)
927 {
928 char *p = enum_name_string (t, type);
929 OB_PUTCP (p);
930 }
931 else if (type == char_type_node
932 || type == signed_char_type_node
933 || type == unsigned_char_type_node)
934 {
935 OB_PUTC ('\'');
936 dump_char (TREE_INT_CST_LOW (t));
937 OB_PUTC ('\'');
938 }
939 else if (TREE_INT_CST_HIGH (t)
940 != (TREE_INT_CST_LOW (t) >> (HOST_BITS_PER_WIDE_INT - 1)))
941 {
942 tree val = t;
943 if (TREE_INT_CST_HIGH (val) < 0)
944 {
945 OB_PUTC ('-');
946 val = build_int_2 (~TREE_INT_CST_LOW (val),
947 -TREE_INT_CST_HIGH (val));
948 }
949 /* Would "%x%0*x" or "%x%*0x" get zero-padding on all
950 systems? */
951 {
952 static char format[10]; /* "%x%09999x\0" */
953 if (!format[0])
954 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
955 sprintf (digit_buffer, format, TREE_INT_CST_HIGH (val),
956 TREE_INT_CST_LOW (val));
957 OB_PUTCP (digit_buffer);
958 }
959 }
960 else
961 OB_PUTI (TREE_INT_CST_LOW (t));
962 }
963 break;
964
965 case REAL_CST:
966#ifndef REAL_IS_NOT_DOUBLE
967 sprintf (digit_buffer, "%g", TREE_REAL_CST (t));
968#else
969 {
970 unsigned char *p = (unsigned char *) &TREE_REAL_CST (t);
971 int i;
972 strcpy (digit_buffer, "0x");
973 for (i = 0; i < sizeof TREE_REAL_CST (t); i++)
974 sprintf (digit_buffer + 2 + 2*i, "%02x", *p++);
975 }
976#endif
977 OB_PUTCP (digit_buffer);
978 break;
979
980 case STRING_CST:
981 {
982 char *p = TREE_STRING_POINTER (t);
983 int len = TREE_STRING_LENGTH (t) - 1;
984 int i;
985
986 OB_PUTC ('\"');
987 for (i = 0; i < len; i++)
988 dump_char (p[i]);
989 OB_PUTC ('\"');
990 }
991 break;
992
993 case COMPOUND_EXPR:
994 dump_binary_op (",", t);
995 break;
996
997 case COND_EXPR:
998 OB_PUTC ('(');
999 dump_expr (TREE_OPERAND (t, 0), 0);
1000 OB_PUTS (" ? ");
1001 dump_expr (TREE_OPERAND (t, 1), 0);
1002 OB_PUTS (" : ");
1003 dump_expr (TREE_OPERAND (t, 2), 0);
1004 OB_PUTC (')');
1005 break;
1006
1007 case SAVE_EXPR:
1008 if (TREE_HAS_CONSTRUCTOR (t))
1009 {
1010 OB_PUTS ("new ");
1011 dump_type (TREE_TYPE (TREE_TYPE (t)), 0);
1012 PARM_DECL_EXPR (t) = 1;
1013 }
1014 else
1015 {
1016 sorry ("operand of SAVE_EXPR not understood");
1017 goto error;
1018 }
1019 break;
1020
1021 case NEW_EXPR:
1022 OB_PUTID (TYPE_IDENTIFIER (TREE_TYPE (t)));
1023 OB_PUTC ('(');
1024 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)));
1025 OB_PUTC (')');
1026 break;
1027
1028 case CALL_EXPR:
1029 {
1030 tree fn = TREE_OPERAND (t, 0);
1031 tree args = TREE_OPERAND (t, 1);
1032
1033 if (TREE_CODE (fn) == ADDR_EXPR)
1034 fn = TREE_OPERAND (fn, 0);
1035
1036 if (NEXT_CODE (fn) == METHOD_TYPE)
1037 {
1038 tree ob = TREE_VALUE (args);
1039 if (TREE_CODE (ob) == ADDR_EXPR)
1040 {
1041 dump_expr (TREE_OPERAND (ob, 0), 0);
1042 OB_PUTC ('.');
1043 }
1044 else if (TREE_CODE (ob) != PARM_DECL
1045 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1046 {
1047 dump_expr (ob, 0);
1048 OB_PUTC2 ('-', '>');
1049 }
1050 args = TREE_CHAIN (args);
1051 }
1052 dump_expr (fn, 0);
1053 OB_PUTC('(');
1054 dump_expr_list (args);
1055 OB_PUTC (')');
1056 }
1057 break;
1058
1059 case WITH_CLEANUP_EXPR:
1060 /* Note that this only works for G++ cleanups. If somebody
1061 builds a general cleanup, there's no way to represent it. */
1062 dump_expr (TREE_OPERAND (t, 0), 0);
1063 break;
1064
1065 case TARGET_EXPR:
1066 /* Note that this only works for G++ target exprs. If somebody
1067 builds a general TARGET_EXPR, there's no way to represent that
1068 it initializes anything other that the parameter slot for the
1069 default argument. Note we may have cleared out the first
1070 operand in expand_expr, so don't go killing ourselves. */
1071 if (TREE_OPERAND (t, 1))
1072 dump_expr (TREE_OPERAND (t, 1), 0);
1073 break;
1074
1075 case MODIFY_EXPR:
1076 case PLUS_EXPR:
1077 case MINUS_EXPR:
1078 case MULT_EXPR:
1079 case TRUNC_DIV_EXPR:
1080 case TRUNC_MOD_EXPR:
1081 case MIN_EXPR:
1082 case MAX_EXPR:
1083 case LSHIFT_EXPR:
1084 case RSHIFT_EXPR:
1085 case BIT_IOR_EXPR:
1086 case BIT_XOR_EXPR:
1087 case BIT_AND_EXPR:
1088 case BIT_ANDTC_EXPR:
1089 case TRUTH_ANDIF_EXPR:
1090 case TRUTH_ORIF_EXPR:
1091 case LT_EXPR:
1092 case LE_EXPR:
1093 case GT_EXPR:
1094 case GE_EXPR:
1095 case EQ_EXPR:
1096 case NE_EXPR:
1097 dump_binary_op (opname_tab[(int) TREE_CODE (t)], t);
1098 break;
1099
1100 case CEIL_DIV_EXPR:
1101 case FLOOR_DIV_EXPR:
1102 case ROUND_DIV_EXPR:
1103 dump_binary_op ("/", t);
1104 break;
1105
1106 case CEIL_MOD_EXPR:
1107 case FLOOR_MOD_EXPR:
1108 case ROUND_MOD_EXPR:
1109 dump_binary_op ("%", t);
1110 break;
1111
1112 case COMPONENT_REF:
1113 {
1114 tree ob = TREE_OPERAND (t, 0);
1115 if (TREE_CODE (ob) == INDIRECT_REF)
1116 {
1117 ob = TREE_OPERAND (ob, 0);
1118 if (TREE_CODE (ob) != PARM_DECL
1119 || strcmp (IDENTIFIER_POINTER (DECL_NAME (ob)), "this"))
1120 {
1121 dump_expr (ob, 0);
1122 OB_PUTC2 ('-', '>');
1123 }
1124 }
1125 else
1126 {
1127 dump_expr (ob, 0);
1128 OB_PUTC ('.');
1129 }
1130 dump_expr (TREE_OPERAND (t, 1), 1);
1131 }
1132 break;
1133
1134 case CONVERT_EXPR:
1135 dump_unary_op ("+", t, nop);
1136 break;
1137
1138 case ADDR_EXPR:
1139 if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
1140 || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
1141 dump_expr (TREE_OPERAND (t, 0), 0);
1142 else
1143 dump_unary_op ("&", t, nop);
1144 break;
1145
1146 case INDIRECT_REF:
1147 if (TREE_HAS_CONSTRUCTOR (t))
1148 {
1149 t = TREE_OPERAND (t, 0);
1150 my_friendly_assert (TREE_CODE (t) == CALL_EXPR, 237);
1151 dump_expr (TREE_OPERAND (t, 0), 0);
1152 OB_PUTC ('(');
1153 dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)));
1154 OB_PUTC (')');
1155 }
1156 else
1157 {
1158 if (NEXT_CODE (TREE_OPERAND (t, 0)) == REFERENCE_TYPE)
1159 dump_expr (TREE_OPERAND (t, 0), nop);
1160 else
1161 dump_unary_op ("*", t, nop);
1162 }
1163 break;
1164
1165 case NEGATE_EXPR:
1166 case BIT_NOT_EXPR:
1167 case TRUTH_NOT_EXPR:
1168 case PREDECREMENT_EXPR:
1169 case PREINCREMENT_EXPR:
1170 dump_unary_op (opname_tab [(int)TREE_CODE (t)], t, nop);
1171 break;
1172
1173 case POSTDECREMENT_EXPR:
1174 case POSTINCREMENT_EXPR:
1175 OB_PUTC ('(');
1176 dump_expr (TREE_OPERAND (t, 0), 0);
1177 OB_PUTCP (opname_tab[(int)TREE_CODE (t)]);
1178 OB_PUTC (')');
1179 break;
1180
1181 case NON_LVALUE_EXPR:
1182 /* FIXME: This is a KLUDGE workaround for a parsing problem. There
1183 should be another level of INDIRECT_REF so that I don't have to do
1184 this. */
1185 if (NEXT_CODE (t) == POINTER_TYPE)
1186 {
1187 tree next = TREE_TYPE (TREE_TYPE (t));
1188
1189 while (TREE_CODE (next) == POINTER_TYPE)
1190 next = TREE_TYPE (next);
1191
1192 if (TREE_CODE (next) == FUNCTION_TYPE)
1193 {
1194 if (!nop) OB_PUTC ('(');
1195 OB_PUTC ('*');
1196 dump_expr (TREE_OPERAND (t, 0), 1);
1197 if (!nop) OB_PUTC (')');
1198 break;
1199 }
1200 /* else FALLTHRU */
1201 }
1202 dump_expr (TREE_OPERAND (t, 0), 0);
1203 break;
1204
1205 case NOP_EXPR:
1206 dump_expr (TREE_OPERAND (t, 0), nop);
1207 break;
1208
1209 case CONSTRUCTOR:
1210 OB_PUTC ('{');
1211 dump_expr_list (CONSTRUCTOR_ELTS (t), 0);
1212 OB_PUTC ('}');
1213 break;
1214
51c184be
MS
1215 case OFFSET_REF:
1216 {
1217 tree ob = TREE_OPERAND (t, 0);
1218 if (TREE_CODE (ob) == NOP_EXPR
1219 && TREE_OPERAND (ob, 0) == error_mark_node
1220 && TREE_CODE (TREE_OPERAND (t, 1)) == FUNCTION_DECL)
1221 /* A::f */
1222 dump_expr (TREE_OPERAND (t, 1), 0);
1223 else
1224 {
1225 sorry ("operand of OFFSET_REF not understood");
1226 goto error;
1227 }
1228 break;
1229 }
1230
8d08fdba
MS
1231 /* This list is incomplete, but should suffice for now.
1232 It is very important that `sorry' does not call
1233 `report_error_function'. That could cause an infinite loop. */
1234 default:
1235 sorry ("`%s' not supported by dump_expr",
1236 tree_code_name[(int) TREE_CODE (t)]);
1237
1238 /* fall through to ERROR_MARK... */
1239 case ERROR_MARK:
1240 error:
51c184be 1241 OB_PUTCP ("{error}");
8d08fdba
MS
1242 break;
1243 }
1244}
1245
1246static void
1247dump_binary_op (opstring, t)
1248 char *opstring;
1249 tree t;
1250{
1251 OB_PUTC ('(');
1252 dump_expr (TREE_OPERAND (t, 0), 1);
1253 OB_PUTC (' ');
1254 OB_PUTCP (opstring);
1255 OB_PUTC (' ');
1256 dump_expr (TREE_OPERAND (t, 1), 1);
1257 OB_PUTC (')');
1258}
1259
1260static void
1261dump_unary_op (opstring, t, nop)
1262 char *opstring;
1263 tree t;
1264 int nop;
1265{
1266 if (!nop) OB_PUTC ('(');
1267 OB_PUTCP (opstring);
1268 dump_expr (TREE_OPERAND (t, 0), 1);
1269 if (!nop) OB_PUTC (')');
1270}
1271
1272char *
1273fndecl_as_string (cname, fndecl, print_ret_type_p)
1274 tree cname, fndecl;
1275 int print_ret_type_p;
1276{
1277 return decl_as_string (fndecl, print_ret_type_p);
1278}
1279
1280/* Same, but handtype a _TYPE.
1281 Called from convert_to_reference, mangle_class_name_for_template,
1282 build_unary_op, and GNU_xref_decl. */
1283char *
1284type_as_string (typ, v)
1285 tree typ;
1286 int v;
1287{
1288 OB_INIT ();
1289
1290 dump_type (typ, v);
1291
1292 OB_FINISH ();
1293
1294 return (char *)obstack_base (&scratch_obstack);
1295}
1296
1297char *
1298expr_as_string (decl, v)
1299 tree decl;
1300 int v;
1301{
1302 OB_INIT ();
1303
1304 dump_expr (decl, 1);
1305
1306 OB_FINISH ();
1307
1308 return (char *)obstack_base (&scratch_obstack);
1309}
1310
1311/* A cross between type_as_string and fndecl_as_string.
1312 Only called from substitute_nice_name. */
1313char *
1314decl_as_string (decl, v)
1315 tree decl;
1316 int v;
1317{
1318 OB_INIT ();
1319
1320 dump_decl (decl, v);
1321
1322 OB_FINISH ();
1323
1324 return (char *)obstack_base (&scratch_obstack);
1325}
1326
1327char *
1328cp_file_of (t)
1329 tree t;
1330{
1331 if (TREE_CODE (t) == PARM_DECL)
1332 return DECL_SOURCE_FILE (DECL_CONTEXT (t));
1333 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1334 return DECL_SOURCE_FILE (TYPE_NAME (t));
1335 else
1336 return DECL_SOURCE_FILE (t);
1337}
1338
1339int
1340cp_line_of (t)
1341 tree t;
1342{
1343 if (TREE_CODE (t) == PARM_DECL)
1344 return DECL_SOURCE_LINE (DECL_CONTEXT (t));
1345 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1346 return DECL_SOURCE_LINE (TYPE_NAME (t));
1347 else
1348 return DECL_SOURCE_LINE (t);
1349}
1350
1351char *
1352code_as_string (c, v)
1353 enum tree_code c;
1354 int v;
1355{
1356 return tree_code_name [c];
1357}
1358
1359char *
1360language_as_string (c, v)
1361 enum languages c;
8926095f 1362 int v;
8d08fdba
MS
1363{
1364 switch (c)
1365 {
1366 case lang_c:
1367 return "C";
1368
1369 case lang_cplusplus:
1370 return "C++";
1371
1372 default:
1373 my_friendly_abort (355);
8926095f 1374 return 0;
8d08fdba
MS
1375 }
1376}
1377
1378/* Return the proper printed version of a parameter to a C++ function. */
1379char *
1380parm_as_string (p, v)
8926095f 1381 int p, v;
8d08fdba
MS
1382{
1383 if (p < 0)
1384 return "`this'";
1385
1386 sprintf (digit_buffer, "%d", p+1);
1387 return digit_buffer;
1388}
1389
1390char *
1391op_as_string (p, v)
1392 enum tree_code p;
8926095f 1393 int v;
8d08fdba
MS
1394{
1395 static char buf[] = "operator ";
1396
1397 if (p == 0)
51c184be 1398 return "{unknown}";
8d08fdba
MS
1399
1400 strcpy (buf + 9, opname_tab [p]);
1401 return buf;
1402}
1403
1404char *
1405args_as_string (p, v)
1406 tree p;
1407 int v;
1408{
1409 if (p == NULL_TREE)
1410 return "...";
1411
1412 return type_as_string (p, v);
1413}
This page took 0.216453 seconds and 5 git commands to generate.