home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / cp-xref.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  18KB  |  831 lines

  1. /* Code for handling XREF output from GNU C++.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "tree.h"
  24. #include <stdio.h>
  25. #include "cp-tree.h"
  26. #include "input.h"
  27.  
  28. #include <ctype.h>
  29.  
  30. char *getpwd ();
  31.  
  32. extern char *index ();
  33. extern char *rindex ();
  34.  
  35. /* The character(s) used to join a directory specification (obtained with
  36.    getwd or equivalent) with a non-absolute file name.  */
  37.  
  38. #ifndef FILE_NAME_JOINER
  39. #define FILE_NAME_JOINER "/"
  40. #endif
  41.  
  42. /* Nonzero if NAME as a file name is absolute.  */
  43. #ifndef FILE_NAME_ABSOLUTE_P
  44. #define FILE_NAME_ABSOLUTE_P(NAME) (NAME[0] == '/')
  45. #endif
  46.  
  47. /* For cross referencing.  */
  48.  
  49. int flag_gnu_xref;
  50.  
  51. /************************************************************************/
  52. /*                                    */
  53. /*    Common definitions                        */
  54. /*                                    */
  55. /************************************************************************/
  56.  
  57. typedef int    Integer;
  58. typedef char *    String;
  59.  
  60.  
  61. #ifndef TRUE
  62. #define TRUE 1
  63. #endif
  64. #ifndef FALSE
  65. #define FALSE 0
  66. #endif
  67. #ifndef NULL
  68. #define NULL 0
  69. #endif
  70.  
  71. #define PALLOC(typ) ((typ *) calloc(1,sizeof(typ)))
  72.  
  73.  
  74. /* Return a malloc'd copy of STR.  */
  75. #define SALLOC(str) \
  76.  ((String) ((str) == NULL ? NULL    \
  77.         : strcpy ((String) malloc (strlen ((str)) + 1), (str))))
  78. #define SFREE(str) (str != NULL && (free(str),0))
  79.  
  80. #define STREQL(s1,s2) (strcmp((s1),(s2)) == 0)
  81. #define STRNEQ(s1,s2) (strcmp((s1),(s2)) != 0)
  82. #define STRLSS(s1,s2) (strcmp((s1),(s2)) < 0)
  83. #define STRLEQ(s1,s2) (strcmp((s1),(s2)) <= 0)
  84. #define STRGTR(s1,s2) (strcmp((s1),(s2)) > 0)
  85. #define STRGEQ(s1,s2) (strcmp((s1),(s2)) >= 0)
  86.  
  87. /************************************************************************/
  88. /*                                    */
  89. /*    Type definitions                        */
  90. /*                                    */
  91. /************************************************************************/
  92.  
  93.  
  94. typedef struct _XREF_FILE *    XREF_FILE;
  95. typedef struct _XREF_SCOPE *    XREF_SCOPE;
  96.  
  97. typedef struct _XREF_FILE
  98. {
  99.   String name;
  100.   String outname;
  101.   XREF_FILE next;
  102. } XREF_FILE_INFO;
  103.  
  104. typedef struct _XREF_SCOPE
  105. {
  106.   Integer gid;
  107.   Integer lid;
  108.   XREF_FILE file;
  109.   Integer start;
  110.   XREF_SCOPE outer;
  111. } XREF_SCOPE_INFO;
  112.  
  113. /************************************************************************/
  114. /*                                    */
  115. /*    Local storage                            */
  116. /*                                    */
  117. /************************************************************************/
  118.  
  119. static    char        doing_xref = 0;
  120. static    FILE *        xref_file = NULL;
  121. static    char        xref_name[1024];
  122. static    XREF_FILE    all_files = NULL;
  123. static    String        wd_name = NULL;
  124. static    XREF_SCOPE    cur_scope = NULL;
  125. static    Integer     scope_ctr = 0;
  126. static    XREF_FILE    last_file = NULL;
  127. static    tree        last_fndecl = NULL;
  128.  
  129. /************************************************************************/
  130. /*                                    */
  131. /*    Forward definitions                        */
  132. /*                                    */
  133. /************************************************************************/
  134.  
  135. extern    void        GNU_xref_begin();
  136. extern    void        GNU_xref_end();
  137. extern    void        GNU_xref_file();
  138. extern    void        GNU_xref_start_scope();
  139. extern    void        GNU_xref_end_scope();
  140. extern    void        GNU_xref_ref();
  141. extern    void        GNU_xref_decl();
  142. extern    void        GNU_xref_call();
  143. extern    void        GNU_xref_function();
  144. extern    void        GNU_xref_assign();
  145. extern    void        GNU_xref_hier();
  146. extern    void        GNU_xref_member();
  147.  
  148. static    void        gen_assign();
  149. static    XREF_FILE    find_file();
  150. static    String        filename();
  151. static    String        fctname();
  152. static    String        declname();
  153. static    void        simplify_type();
  154. static    String        fixname();
  155. static    void        open_xref_file();
  156.  
  157. extern    char *        type_as_string();
  158.  
  159. /* Start cross referencing.  FILE is the name of the file we xref.  */
  160.  
  161. void
  162. GNU_xref_begin (file)
  163.    String file;
  164. {
  165.   doing_xref = 1;
  166.  
  167.   if (file != NULL && STRNEQ (file,"-"))
  168.     {
  169.       open_xref_file(file);
  170.       GNU_xref_file(file);
  171.     }
  172. }
  173.  
  174. /* Finish cross-referencing.  ERRCNT is the number of errors
  175.    we encountered.  */
  176.  
  177. void
  178. GNU_xref_end (ect)
  179.    int ect;
  180. {
  181.   XREF_FILE xf;
  182.  
  183.   if (!doing_xref) return;
  184.  
  185.   xf = find_file (input_filename);
  186.   if (xf == NULL) return;
  187.  
  188.   while (cur_scope != NULL)
  189.     GNU_xref_end_scope(cur_scope->gid,0,0,0,0);
  190.  
  191.   doing_xref = 0;
  192.  
  193.   if (xref_file == NULL) return;
  194.  
  195.   fclose (xref_file);
  196.  
  197.   xref_file = NULL;
  198.   all_files = NULL;
  199.  
  200.   if (ect > 0) unlink (xref_name);
  201. }
  202.  
  203. /* Write out xref for file named NAME.  */
  204.  
  205. void
  206. GNU_xref_file (name)
  207.    String name;
  208. {
  209.   XREF_FILE xf;
  210.  
  211.   if (!doing_xref || name == NULL) return;
  212.  
  213.   if (xref_file == NULL)
  214.     {
  215.       open_xref_file (name);
  216.       if (!doing_xref) return;
  217.     }
  218.  
  219.   if (all_files == NULL)
  220.     fprintf(xref_file,"SCP * 0 0 0 0 RESET\n");
  221.  
  222.   xf = find_file (name);
  223.   if (xf != NULL) return;
  224.  
  225.   xf = PALLOC (XREF_FILE_INFO);
  226.   xf->name = SALLOC (name);
  227.   xf->next = all_files;
  228.   all_files = xf;
  229.  
  230.   if (wd_name == NULL)
  231.     wd_name = getpwd ();
  232.  
  233.   if (FILE_NAME_ABSOLUTE_P (name) || ! wd_name)
  234.     xf->outname = xf->name;
  235.   else
  236.     {
  237.       char *nmbuf
  238.     = (char *) malloc (strlen (wd_name) + strlen (FILE_NAME_JOINER)
  239.                + strlen (name) + 1);
  240.       sprintf (nmbuf, "%s%s%s", wd_name, FILE_NAME_JOINER, name);
  241.       name = nmbuf;
  242.       xf->outname = nmbuf;
  243.     }
  244.  
  245.   fprintf (xref_file, "FIL %s %s 0\n", name, wd_name);
  246.  
  247.   filename (xf);
  248.   fctname (NULL);
  249. }
  250.  
  251. /* Start a scope identified at level ID.  */
  252.  
  253. void
  254. GNU_xref_start_scope (id)
  255.    Integer id;
  256. {
  257.   XREF_SCOPE xs;
  258.   XREF_FILE xf;
  259.  
  260.   if (!doing_xref) return;
  261.   xf = find_file (input_filename);
  262.  
  263.   xs = PALLOC (XREF_SCOPE_INFO);
  264.   xs->file = xf;
  265.   xs->start = lineno;
  266.   if (xs->start <= 0) xs->start = 1;
  267.   xs->gid = id;
  268.   xs->lid = ++scope_ctr;
  269.   xs->outer = cur_scope;
  270.   cur_scope = xs;
  271. }
  272.  
  273. /* Finish a scope at level ID.
  274.    INID is ???
  275.    PRM is ???
  276.    KEEP is nonzero iff this scope is retained (nonzero if it's
  277.    a compiler-generated invisible scope).
  278.    TRNS is ???  */
  279.  
  280. void
  281. GNU_xref_end_scope (id,inid,prm,keep,trns)
  282.    Integer id;
  283.    Integer inid;
  284.    Integer prm,keep,trns;
  285. {
  286.   XREF_FILE xf;
  287.   XREF_SCOPE xs,lxs,oxs;
  288.   String stype;
  289.  
  290.   if (!doing_xref) return;
  291.   xf = find_file (input_filename);
  292.   if (xf == NULL) return;
  293.  
  294.   lxs = NULL;
  295.   for (xs = cur_scope; xs != NULL; xs = xs->outer)
  296.     {
  297.       if (xs->gid == id) break;
  298.       lxs = xs;
  299.     }
  300.   if (xs == NULL) return;
  301.  
  302.   if (inid != 0) {
  303.     for (oxs = cur_scope; oxs != NULL; oxs = oxs->outer) {
  304.       if (oxs->gid == inid) break;
  305.     }
  306.     if (oxs == NULL) return;
  307.     inid = oxs->lid;
  308.   }
  309.  
  310.   if (prm == 2) stype = "SUE";
  311.   else if (prm != 0) stype = "ARGS";
  312.   else if (keep == 2 || inid != 0) stype = "INTERN";
  313.   else stype = "EXTERN";
  314.  
  315.   fprintf (xref_file,"SCP %s %d %d %d %d %s\n",
  316.        filename (xf), xs->start, lineno,xs->lid, inid, stype);
  317.  
  318.   if (lxs == NULL) cur_scope = xs->outer;
  319.   else lxs->outer = xs->outer;
  320.  
  321.   free (xs);
  322. }
  323.  
  324. /* Output a reference to NAME in FNDECL.  */
  325.  
  326. void
  327. GNU_xref_ref (fndecl,name)
  328.    tree fndecl;
  329.    String name;
  330. {
  331.   XREF_FILE xf;
  332.  
  333.   if (!doing_xref) return;
  334.   xf = find_file (input_filename);
  335.   if (xf == NULL) return;
  336.  
  337.   fprintf (xref_file, "REF %s %d %s %s\n",
  338.        filename (xf), lineno, fctname (fndecl), name);
  339. }
  340.  
  341. /* Output a reference to DECL in FNDECL.  */
  342.  
  343. void
  344. GNU_xref_decl (fndecl,decl)
  345.    tree fndecl;
  346.    tree decl;
  347. {
  348.   XREF_FILE xf,xf1;
  349.   String cls;
  350.   String name;
  351.   char buf[10240];
  352.   Integer uselin;
  353.  
  354.   if (!doing_xref) return;
  355.   xf = find_file (input_filename);
  356.   if (xf == NULL) return;
  357.  
  358.   uselin = FALSE;
  359.  
  360.   if (TREE_CODE (decl) == TYPE_DECL) cls = "TYPEDEF";
  361.   else if (TREE_CODE (decl) == FIELD_DECL) cls = "FIELD";
  362.   else if (TREE_CODE (decl) == VAR_DECL)
  363.     {
  364.       if (fndecl == NULL && TREE_STATIC(decl)
  365.       && TREE_READONLY(decl) && DECL_IN