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