* configure.in (sparc64-*-linux*): Use posix threads if enabled.
[official-gcc.git] / gcc / cp / xref.c
blob5c6e7b787a35b3b96e10b510d7a836863000ecad
1 /* Code for handling XREF output from GNU C++.
2 Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
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.
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.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "input.h"
28 #include "toplev.h"
30 /* The character(s) used to join a directory specification (obtained with
31 getwd or equivalent) with a non-absolute file name. */
33 #ifndef FILE_NAME_JOINER
34 #define FILE_NAME_JOINER "/"
35 #endif
37 /* Nonzero if NAME as a file name is absolute. */
38 #ifndef FILE_NAME_ABSOLUTE_P
39 #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
40 #endif
42 /* For cross referencing. */
44 int flag_gnu_xref;
46 /************************************************************************/
47 /* */
48 /* Common definitions */
49 /* */
50 /************************************************************************/
52 #ifndef TRUE
53 #define TRUE 1
54 #endif
55 #ifndef FALSE
56 #define FALSE 0
57 #endif
59 #define PALLOC(typ) ((typ *) xcalloc(1,sizeof(typ)))
62 /* Return a malloc'd copy of STR. */
63 #define SALLOC(str) ((char *) ((str) == NULL ? NULL : xstrdup (str)))
64 #define SFREE(str) (str != NULL && (free(str),0))
66 #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
67 #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
68 #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
69 #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
70 #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
71 #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
73 /************************************************************************/
74 /* */
75 /* Type definitions */
76 /* */
77 /************************************************************************/
80 typedef struct _XREF_FILE * XREF_FILE;
81 typedef struct _XREF_SCOPE * XREF_SCOPE;
83 typedef struct _XREF_FILE
85 const char *name;
86 const char *outname;
87 XREF_FILE next;
88 } XREF_FILE_INFO;
90 typedef struct _XREF_SCOPE
92 int gid;
93 int lid;
94 XREF_FILE file;
95 int start;
96 XREF_SCOPE outer;
97 } XREF_SCOPE_INFO;
99 /************************************************************************/
100 /* */
101 /* Local storage */
102 /* */
103 /************************************************************************/
105 static char doing_xref = 0;
106 static FILE * xref_file = NULL;
107 static char xref_name[1024];
108 static XREF_FILE all_files = NULL;
109 static char * wd_name = NULL;
110 static XREF_SCOPE cur_scope = NULL;
111 static int scope_ctr = 0;
112 static XREF_FILE last_file = NULL;
113 static tree last_fndecl = NULL;
115 /************************************************************************/
116 /* */
117 /* Forward definitions */
118 /* */
119 /************************************************************************/
120 static void gen_assign PROTO((XREF_FILE, tree));
121 static XREF_FILE find_file PROTO((const char *));
122 static const char * filename PROTO((XREF_FILE));
123 static const char * fctname PROTO((tree));
124 static const char * declname PROTO((tree));
125 static void simplify_type PROTO((char *));
126 static const char * fixname PROTO((const char *, char *));
127 static void open_xref_file PROTO((const char *));
128 static const char * classname PROTO((tree));
130 /* Start cross referencing. FILE is the name of the file we xref. */
132 void
133 GNU_xref_begin (file)
134 const char *file;
136 doing_xref = 1;
138 if (file != NULL && STRNEQ (file,"-"))
140 open_xref_file(file);
141 GNU_xref_file(file);
145 /* Finish cross-referencing. ERRCNT is the number of errors
146 we encountered. */
148 void
149 GNU_xref_end (ect)
150 int ect;
152 XREF_FILE xf;
154 if (!doing_xref) return;
156 xf = find_file (input_filename);
157 if (xf == NULL) return;
159 while (cur_scope != NULL)
160 GNU_xref_end_scope(cur_scope->gid,0,0,0);
162 doing_xref = 0;
164 if (xref_file == NULL) return;
166 fclose (xref_file);
168 xref_file = NULL;
169 all_files = NULL;
171 if (ect > 0) unlink (xref_name);
174 /* Write out xref for file named NAME. */
176 void
177 GNU_xref_file (name)
178 const char *name;
180 XREF_FILE xf;
182 if (!doing_xref || name == NULL) return;
184 if (xref_file == NULL)
186 open_xref_file (name);
187 if (!doing_xref) return;
190 if (all_files == NULL)
191 fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
193 xf = find_file (name);
194 if (xf != NULL) return;
196 xf = PALLOC (XREF_FILE_INFO);
197 xf->name = SALLOC (name);
198 xf->next = all_files;
199 all_files = xf;
201 if (wd_name == NULL)
202 wd_name = getpwd ();
204 if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
205 xf->outname = xf->name;
206 else
208 char *nmbuf
209 = (char *) xmalloc (strlen (wd_name) + strlen (FILE_NAME_JOINER)
210 + strlen (name) + 1);
211 sprintf (nmbuf, "%s%s%s", wd_name, FILE_NAME_JOINER, name);
212 name = nmbuf;
213 xf->outname = nmbuf;
216 fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
218 filename (xf);
219 fctname (NULL);
222 /* Start a scope identified at level ID. */
224 void
225 GNU_xref_start_scope (id)
226 HOST_WIDE_INT id;
228 XREF_SCOPE xs;
229 XREF_FILE xf;
231 if (!doing_xref) return;
232 xf = find_file (input_filename);
234 xs = PALLOC (XREF_SCOPE_INFO);
235 xs->file = xf;
236 xs->start = lineno;
237 if (xs->start <= 0) xs->start = 1;
238 xs->gid = id;
239 xs->lid = ++scope_ctr;
240 xs->outer = cur_scope;
241 cur_scope = xs;
244 /* Finish a scope at level ID.
245 INID is ???
246 PRM is ???
247 KEEP is nonzero iff this scope is retained (nonzero if it's
248 a compiler-generated invisible scope).
249 TRNS is ??? */
251 void
252 GNU_xref_end_scope (id,inid,prm,keep)
253 HOST_WIDE_INT id;
254 HOST_WIDE_INT inid;
255 int prm,keep;
257 XREF_FILE xf;
258 XREF_SCOPE xs,lxs,oxs;
259 const char *stype;
261 if (!doing_xref) return;
262 xf = find_file (input_filename);
263 if (xf == NULL) return;
265 lxs = NULL;
266 for (xs = cur_scope; xs != NULL; xs = xs->outer)
268 if (xs->gid == id) break;
269 lxs = xs;
271 if (xs == NULL) return;
273 if (inid != 0) {
274 for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
275 if (oxs->gid == inid) break;
277 if (oxs == NULL) return;
278 inid = oxs->lid;
281 if (prm == 2) stype = "SUE";
282 else if (prm != 0) stype = "ARGS";
283 else if (keep == 2 || inid != 0) stype = "INTERN";
284 else stype = "EXTERN";
286 fprintf (xref_file, "SCP %s %d %d %d ",
287 filename (xf), xs->start, lineno,xs->lid);
288 fprintf (xref_file, HOST_WIDE_INT_PRINT_DEC, inid);
289 fprintf (xref_file, " %s\n", stype);
291 if (lxs == NULL) cur_scope = xs->outer;
292 else lxs->outer = xs->outer;
294 free (xs);
297 /* Output a reference to NAME in FNDECL. */
299 void
300 GNU_xref_ref (fndecl,name)
301 tree fndecl;
302 const char *name;
304 XREF_FILE xf;
306 if (!doing_xref) return;
307 xf = find_file (input_filename);
308 if (xf == NULL) return;
310 fprintf (xref_file, "REF %s %d %s %s\n",
311 filename (xf), lineno, fctname (fndecl), name);
314 /* Output a reference to DECL in FNDECL. */
316 void
317 GNU_xref_decl (fndecl,decl)
318 tree fndecl;
319 tree decl;
321 XREF_FILE xf,xf1;
322 const char *cls = 0;
323 const char *name;
324 char buf[10240];
325 int uselin;
327 if (!doing_xref) return;
328 xf = find_file (input_filename);
329 if (xf == NULL) return;
331 uselin = FALSE;
333 if (TREE_CODE (decl) == TYPE_DECL) cls = "TYPEDEF";
334 else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
335 else if (TREE_CODE (decl) == VAR_DECL)
337 if (fndecl == NULL && TREE_STATIC(decl)
338 && TREE_READONLY(decl) && DECL_INITIAL(decl) != 0
339 && !TREE_PUBLIC(decl) && !DECL_EXTERNAL(decl)
340 && DECL_MODE(decl) != BLKmode) cls = "CONST";
341 else if (DECL_EXTERNAL(decl)) cls = "EXTERN";
342 else if (TREE_PUBLIC(decl)) cls = "EXTDEF";
343 else if (TREE_STATIC(decl)) cls = "STATIC";
344 else if (DECL_REGISTER(decl)) cls = "REGISTER";
345 else cls = "AUTO";
347 else if (TREE_CODE (decl) == PARM_DECL) cls = "PARAM";
348 else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
349 else if (TREE_CODE (decl) == CONST_DECL) cls = "CONST";
350 else if (TREE_CODE (decl) == FUNCTION_DECL)
352 if (DECL_EXTERNAL (decl)) cls = "EXTERN";
353 else if (TREE_PUBLIC (decl)) cls = "EFUNCTION";
354 else cls = "SFUNCTION";
356 else if (TREE_CODE (decl) == LABEL_DECL) cls = "LABEL";
357 else if (TREE_CODE (decl) == UNION_TYPE)
359 cls = "UNIONID";
360 decl = TYPE_NAME (decl);
361 uselin = TRUE;
363 else if (TREE_CODE (decl) == RECORD_TYPE)
365 if (CLASSTYPE_DECLARED_CLASS (decl)) cls = "CLASSID";
366 else cls = "STRUCTID";
367 decl = TYPE_NAME (decl);
368 uselin = TRUE;
370 else if (TREE_CODE (decl) == ENUMERAL_TYPE)
372 cls = "ENUMID";
373 decl = TYPE_NAME (decl);
374 uselin = TRUE;
376 else if (TREE_CODE (decl) == TEMPLATE_DECL)
378 if (TREE_CODE (DECL_RESULT (decl)) == TYPE_DECL)
379 cls = "CLASSTEMP";
380 else if (TREE_CODE (DECL_RESULT (decl)) == FUNCTION_DECL)
381 cls = "FUNCTEMP";
382 else if (TREE_CODE (DECL_RESULT (decl)) == VAR_DECL)
383 cls = "VARTEMP";
384 else
385 my_friendly_abort (358);
386 uselin = TRUE;
388 else cls = "UNKNOWN";
390 if (decl == NULL || DECL_NAME (decl) == NULL) return;
392 if (uselin && decl->decl.linenum > 0 && decl->decl.filename != NULL)
394 xf1 = find_file (decl->decl.filename);
395 if (xf1 != NULL)
397 lineno = decl->decl.linenum;
398 xf = xf1;
402 if (DECL_ASSEMBLER_NAME (decl))
403 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
404 else
405 name = IDENTIFIER_POINTER (DECL_NAME (decl));
407 strcpy (buf, type_as_string (TREE_TYPE (decl), 0));
408 simplify_type (buf);
410 fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
411 filename(xf), lineno, name,
412 (cur_scope != NULL ? cur_scope->lid : 0),
413 cls, fctname(fndecl), buf);
415 if (STREQL (cls, "STRUCTID") || STREQL (cls, "UNIONID"))
417 cls = "CLASSID";
418 fprintf (xref_file, "DCL %s %d %s %d %s %s %s\n",
419 filename(xf), lineno,name,
420 (cur_scope != NULL ? cur_scope->lid : 0),
421 cls, fctname(fndecl), buf);
425 /* Output a reference to a call to NAME in FNDECL. */
427 void
428 GNU_xref_call (fndecl, name)
429 tree fndecl;
430 const char *name;
432 XREF_FILE xf;
433 char buf[1024];
434 const char *s;
436 if (!doing_xref) return;
437 xf = find_file (input_filename);
438 if (xf == NULL) return;
439 name = fixname (name, buf);
441 for (s = name; *s != 0; ++s)
442 if (*s == '_' && s[1] == '_') break;
443 if (*s != 0) GNU_xref_ref (fndecl, name);
445 fprintf (xref_file, "CAL %s %d %s %s\n",
446 filename (xf), lineno, name, fctname (fndecl));
449 /* Output cross-reference info about FNDECL. If non-NULL,
450 ARGS are the arguments for the function (i.e., before the FUNCTION_DECL
451 has been fully built). */
453 void
454 GNU_xref_function (fndecl, args)
455 tree fndecl;
456 tree args;
458 XREF_FILE xf;
459 int ct;
460 char buf[1024];
462 if (!doing_xref) return;
463 xf = find_file (input_filename);
464 if (xf == NULL) return;
466 ct = 0;
467 buf[0] = 0;
468 if (args == NULL) args = DECL_ARGUMENTS (fndecl);
470 GNU_xref_decl (NULL, fndecl);
472 for ( ; args != NULL; args = TREE_CHAIN (args))
474 GNU_xref_decl (fndecl,args);
475 if (ct != 0) strcat (buf,",");
476 strcat (buf, declname (args));
477 ++ct;
480 fprintf (xref_file, "PRC %s %d %s %d %d %s\n",
481 filename(xf), lineno, declname(fndecl),
482 (cur_scope != NULL ? cur_scope->lid : 0),
483 ct, buf);
486 /* Output cross-reference info about an assignment to NAME. */
488 void
489 GNU_xref_assign(name)
490 tree name;
492 XREF_FILE xf;
494 if (!doing_xref) return;
495 xf = find_file(input_filename);
496 if (xf == NULL) return;
498 gen_assign(xf, name);
501 static void
502 gen_assign(xf, name)
503 XREF_FILE xf;
504 tree name;
506 const char *s;
508 s = NULL;
510 switch (TREE_CODE (name))
512 case IDENTIFIER_NODE :
513 s = IDENTIFIER_POINTER(name);
514 break;
515 case VAR_DECL :
516 s = declname(name);
517 break;
518 case COMPONENT_REF :
519 gen_assign(xf, TREE_OPERAND(name, 0));
520 gen_assign(xf, TREE_OPERAND(name, 1));
521 break;
522 case INDIRECT_REF :
523 case OFFSET_REF :
524 case ARRAY_REF :
525 case BUFFER_REF :
526 gen_assign(xf, TREE_OPERAND(name, 0));
527 break;
528 case COMPOUND_EXPR :
529 gen_assign(xf, TREE_OPERAND(name, 1));
530 break;
531 default :
532 break;
535 if (s != NULL)
536 fprintf(xref_file, "ASG %s %d %s\n", filename(xf), lineno, s);
539 static const char *
540 classname (cls)
541 tree cls;
543 if (cls && TREE_CODE_CLASS (TREE_CODE (cls)) == 't')
544 cls = TYPE_NAME (cls);
545 if (cls && TREE_CODE_CLASS (TREE_CODE (cls)) == 'd')
546 cls = DECL_NAME (cls);
547 if (cls && TREE_CODE (cls) == IDENTIFIER_NODE)
548 return IDENTIFIER_POINTER (cls);
549 return "?";
552 /* Output cross-reference info about a class hierarchy.
553 CLS is the class type of interest. BASE is a baseclass
554 for CLS. PUB and VIRT give the access info about
555 the class derivation. FRND is nonzero iff BASE is a friend
556 of CLS.
558 ??? Needs to handle nested classes. */
560 void
561 GNU_xref_hier(cls, base, pub, virt, frnd)
562 tree cls;
563 tree base;
564 int pub;
565 int virt;
566 int frnd;
568 XREF_FILE xf;
570 if (!doing_xref) return;
571 xf = find_file(input_filename);
572 if (xf == NULL) return;
574 fprintf(xref_file, "HIE %s %d %s %s %d %d %d\n",
575 filename(xf), lineno, classname (cls), classname (base),
576 pub, virt, frnd);
579 /* Output cross-reference info about class members. CLS
580 is the containing type; FLD is the class member. */
582 void
583 GNU_xref_member(cls, fld)
584 tree cls;
585 tree fld;
587 XREF_FILE xf;
588 const char *prot;
589 int confg, pure;
590 const char *d;
591 #ifdef XREF_SHORT_MEMBER_NAMES
592 int i;
593 #endif
594 char buf[1024], bufa[1024];
596 if (!doing_xref) return;
597 xf = find_file(fld->decl.filename);
598 if (xf == NULL) return;
600 if (TREE_PRIVATE (fld)) prot = "PRIVATE";
601 else if (TREE_PROTECTED(fld)) prot = "PROTECTED";
602 else prot = "PUBLIC";
604 confg = 0;
605 if (TREE_CODE (fld) == FUNCTION_DECL && DECL_CONST_MEMFUNC_P(fld))
606 confg = 1;
607 else if (TREE_CODE (fld) == CONST_DECL)
608 confg = 1;
610 pure = 0;
611 if (TREE_CODE (fld) == FUNCTION_DECL && DECL_ABSTRACT_VIRTUAL_P(fld))
612 pure = 1;
614 d = IDENTIFIER_POINTER(cls);
615 sprintf(buf, "%d%s", (int) strlen(d), d);
616 #ifdef XREF_SHORT_MEMBER_NAMES
617 i = strlen(buf);
618 #endif
619 strcpy(bufa, declname(fld));
621 #ifdef XREF_SHORT_MEMBER_NAMES
622 for (p = &bufa[1]; *p != 0; ++p)
624 if (p[0] == '_' && p[1] == '_' && p[2] >= '0' && p[2] <= '9') {
625 if (strncmp(&p[2], buf, i) == 0) *p = 0;
626 break;
628 else if (p[0] == '_' && p[1] == '_' && p[2] == 'C' && p[3] >= '0' && p[3] <= '9') {
629 if (strncmp(&p[3], buf, i) == 0) *p = 0;
630 break;
633 #endif
635 fprintf(xref_file, "MEM %s %d %s %s %s %d %d %d %d %d %d %d\n",
636 filename(xf), fld->decl.linenum, d, bufa, prot,
637 (TREE_CODE (fld) == FUNCTION_DECL ? 0 : 1),
638 (DECL_INLINE (fld) ? 1 : 0),
639 (DECL_LANG_SPECIFIC(fld) && DECL_FRIEND_P(fld) ? 1 : 0),
640 (DECL_VINDEX(fld) ? 1 : 0),
641 (TREE_STATIC(fld) ? 1 : 0),
642 pure, confg);
645 /* Find file entry given name. */
647 static XREF_FILE
648 find_file(name)
649 const char *name;
651 XREF_FILE xf;
653 for (xf = all_files; xf != NULL; xf = xf->next) {
654 if (STREQL(name, xf->name)) break;
657 return xf;
660 /* Return filename for output purposes. */
662 static const char *
663 filename(xf)
664 XREF_FILE xf;
666 if (xf == NULL) {
667 last_file = NULL;
668 return "*";
671 if (last_file == xf) return "*";
673 last_file = xf;
675 return xf->outname;
678 /* Return function name for output purposes. */
680 static const char *
681 fctname(fndecl)
682 tree fndecl;
684 static char fctbuf[1024];
685 const char *s;
687 if (fndecl == NULL && last_fndecl == NULL) return "*";
689 if (fndecl == NULL)
691 last_fndecl = NULL;
692 return "*TOP*";
695 if (fndecl == last_fndecl) return "*";
697 last_fndecl = fndecl;
699 s = declname(fndecl);
700 s = fixname(s, fctbuf);
702 return s;
705 /* Return decl name for output purposes. */
707 static const char *
708 declname(dcl)
709 tree dcl;
711 if (DECL_NAME (dcl) == NULL) return "?";
713 if (DECL_ASSEMBLER_NAME (dcl))
714 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (dcl));
715 else
716 return IDENTIFIER_POINTER (DECL_NAME (dcl));
719 /* Simplify a type string by removing unneeded parenthesis. */
721 static void
722 simplify_type(typ)
723 char *typ;
725 char *s;
726 int lvl, i;
728 i = strlen(typ);
729 while (i > 0 && ISSPACE((unsigned char) typ[i-1])) typ[--i] = 0;
731 if (i > 7 && STREQL(&typ[i-5], "const"))
733 typ[i-5] = 0;
734 i -= 5;
737 if (typ[i-1] != ')') return;
739 s = &typ[i-2];
740 lvl = 1;
741 while (*s != 0) {
742 if (*s == ')') ++lvl;
743 else if (*s == '(')
745 --lvl;
746 if (lvl == 0)
748 s[1] = ')';
749 s[2] = 0;
750 break;
753 --s;
756 if (*s != 0 && s[-1] == ')')
758 --s;
759 --s;
760 if (*s == '(') s[2] = 0;
761 else if (*s == ':') {
762 while (*s != '(') --s;
763 s[1] = ')';
764 s[2] = 0;
769 /* Fixup a function name (take care of embedded spaces). */
771 static const char *
772 fixname(nam, buf)
773 const char *nam;
774 char *buf;
776 const char *s;
777 char *t;
778 int fg;
780 s = nam;
781 t = buf;
782 fg = 0;
784 while (*s != 0)
786 if (*s == ' ')
788 *t++ = '\36';
789 ++fg;
791 else *t++ = *s;
792 ++s;
794 *t = 0;
796 if (fg == 0) return nam;
798 return buf;
801 /* Open file for xreffing. */
803 static void
804 open_xref_file(file)
805 const char *file;
807 const char *s;
808 char *t;
810 #ifdef XREF_FILE_NAME
811 XREF_FILE_NAME (xref_name, file);
812 #else
813 s = rindex (file, '/');
814 if (s == NULL)
815 sprintf (xref_name, ".%s.gxref", file);
816 else
818 ++s;
819 strcpy (xref_name, file);
820 t = rindex (xref_name, '/');
821 ++t;
822 *t++ = '.';
823 strcpy (t, s);
824 strcat (t, ".gxref");
826 #endif /* no XREF_FILE_NAME */
828 xref_file = fopen(xref_name, "w");
830 if (xref_file == NULL)
832 error("Can't create cross-reference file `%s'", xref_name);
833 doing_xref = 0;