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