OSDN Git Service

63rd Cygnus<->FSF merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / repo.c
1 /* Code to maintain a C++ template repository.
2    Copyright (C) 1995 Free Software Foundation, Inc.
3    Contributed by Jason Merrill (jason@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 /* My strategy here is as follows:
22
23    Everything should be emitted in a translation unit where it is used.
24    The results of the automatic process should be easily reproducible with
25    explicit code.  */
26
27 #include <stdio.h>
28 #include "config.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "input.h"
32
33 extern char * rindex ();
34 extern char * getenv ();
35
36 static tree pending_repo;
37 static char repo_name[1024];
38 static FILE *repo_file;
39
40 extern int flag_use_repository;
41 extern int errorcount, sorrycount;
42
43 static int repo_changed;
44
45 #define IDENTIFIER_REPO_USED(NODE)   (TREE_LANG_FLAG_3 (NODE))
46 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
47
48 /* Record the flags used to compile this translation unit.  */
49
50 void
51 repo_compile_flags (argc, argv)
52      int argc;
53      char **argv;
54 {
55 }
56
57 /* If this template has not been seen before, add a note to the repository
58    saying where the declaration was.  This may be used to find the
59    definition at link time.  */
60
61 void
62 repo_template_declared (t)
63      tree t;
64 {}
65
66 /* Note where the definition of a template lives so that instantiations can
67    be generated later.  */
68
69 void
70 repo_template_defined (t)
71      tree t;
72 {}
73
74 /* Note where the definition of a class lives to that template
75    instantiations can use it.  */
76
77 void
78 repo_class_defined (t)
79      tree t;
80 {}
81
82 /* Note that a template has been used.  If we can see the definition, offer
83    to emit it. */
84
85 void
86 repo_template_used (t)
87      tree t;
88 {
89   tree id;
90
91   if (! flag_use_repository)
92     return;
93
94   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
95     {
96       id = DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t));
97       if (IDENTIFIER_REPO_CHOSEN (id))
98         mark_class_instantiated (t, 0);
99     }
100   else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
101     {
102       id = DECL_ASSEMBLER_NAME (t);
103       if (IDENTIFIER_REPO_CHOSEN (id))
104         mark_function_instantiated (t, 0);
105     }
106   else
107     my_friendly_abort (1);
108
109   if (! IDENTIFIER_REPO_USED (id))
110     {
111       repo_changed = 1;
112       IDENTIFIER_REPO_USED (id) = 1;
113     }
114   pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
115 }
116
117 /* Note that the vtable for a class has been used, and offer to emit it.  */
118
119 void
120 repo_vtable_used (t)
121      tree t;
122 {
123   if (! flag_use_repository)
124     return;
125
126   pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
127 }
128
129 /* Note that an inline with external linkage has been used, and offer to
130    emit it.  */
131
132 void
133 repo_inline_used (fn)
134      tree fn;
135 {
136   if (! flag_use_repository)
137     return;
138
139   /* Member functions of polymorphic classes go with their vtables.  */
140   if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
141     {
142       repo_vtable_used (DECL_CLASS_CONTEXT (fn));
143       return;
144     }
145
146   pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
147 }
148
149 /* Note that a particular typeinfo node has been used, and offer to
150    emit it.  */
151
152 void
153 repo_tinfo_used (ti)
154      tree ti;
155 {
156 }
157
158 static char *
159 save_string (s, len)
160      char *s;
161      int len;
162 {
163   register char *result = xmalloc (len + 1);
164
165   bcopy (s, result, len);
166   result[len] = 0;
167   return result;
168 }
169
170 static char *
171 get_base_filename (filename)
172      char *filename;
173 {
174   char *p = getenv ("COLLECT_GCC_OPTIONS");
175   char *output = 0;
176   int compiling = 0;
177
178   if (p)
179     while (*p)
180       {
181         char *q = p;
182         while (*q && *q != ' ') q++;
183         if (*p == '-' && p[1] == 'o')
184           {
185             p += 2;
186             if (p == q)
187               {
188                 p++; q++;
189                 if (*q)
190                   while (*q && *q != ' ') q++;
191               }
192
193             output = save_string (p, q - p);
194           }
195         else if (*p == '-' && p[1] == 'c')
196           compiling = 1;
197         if (*q) q++;
198         p = q;
199       }
200
201   if (compiling && output)
202     return output;
203
204   return save_string (filename, strlen (filename));
205 }        
206
207 static void
208 open_repo_file (filename)
209      char *filename;
210 {
211   register char *p, *q;
212   char *file = get_base_filename (filename);
213   char *s = rindex (file, '/');
214   if (s == NULL)
215     s = file;
216   else
217     ++s;
218
219   for (p = repo_name, q = file; q < s; )
220     *p++ = *q++;
221   *p++ = '.';
222   if ((s = rindex (q, '.')) == NULL)
223     strcpy (p, q);
224   else
225     for (; q < s;)
226       *p++ = *q++;
227   strcat (p, ".repo");
228
229   repo_file = fopen (repo_name, "r");
230   free (file);
231 }
232
233 void
234 init_repo (filename)
235      char *filename;
236 {
237   char buf[1024];
238
239   if (! flag_use_repository)
240     return;
241
242   open_repo_file ();
243
244   if (repo_file == 0)
245     return;
246
247   while (fgets (buf, 1024, repo_file))
248     {
249       int len = strlen (buf) - 1;
250       if (buf[len] != '\n')
251         error ("repository info line too long in %s", repo_name);
252       buf[len] = '\0';
253
254       switch (buf[0])
255         {
256         case 'A':
257         case 'G':
258         case 'M':
259           break;
260         case 'C':
261         case 'O':
262           {
263             tree id = get_identifier (&buf[2]);
264             IDENTIFIER_REPO_USED (id) = 1;
265             if (buf[0] == 'C')
266               IDENTIFIER_REPO_CHOSEN (id) = 1;
267           }
268           break;
269         default:
270           error ("mysterious repository information in %s", repo_name);
271         }
272     }
273 }
274
275 static void
276 reopen_repo_file_for_write ()
277 {
278   if (repo_file)
279     fclose (repo_file);
280   repo_file = fopen (repo_name, "w");
281
282   if (repo_file == 0)
283     {
284       error ("can't create repository information file `%s'", repo_name);
285       flag_use_repository = 0;
286     }
287 }
288
289 /* Emit any pending repos.  */
290
291 void
292 finish_repo ()
293 {
294   tree t;
295   char *p;
296
297   if (! flag_use_repository)
298     return;
299
300   /* Do we have to write out a new info file?  */
301
302   if (! repo_changed || errorcount || sorrycount)
303     goto out;
304
305   reopen_repo_file_for_write ();
306
307   if (repo_file == 0)
308     goto out;
309
310   fprintf (repo_file, "M %s\n", main_input_filename);
311
312   p = getenv ("COLLECT_GCC");
313   if (p != 0)
314     fprintf (repo_file, "G %s\n", p);
315
316   p = getenv ("COLLECT_GCC_OPTIONS");
317   if (p != 0)
318     fprintf (repo_file, "A %s\n", p);
319
320   for (t = pending_repo; t; t = TREE_CHAIN (t))
321     {
322       tree val = TREE_VALUE (t);
323       char type;
324
325       if (TREE_CODE_CLASS (TREE_CODE (val)) == 't')
326         val = TYPE_MAIN_DECL (val);
327       val = DECL_ASSEMBLER_NAME (val);
328
329       if (! IDENTIFIER_REPO_USED (val))
330         continue;
331       IDENTIFIER_REPO_USED (val) = 0;
332
333       type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
334
335       fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
336     }
337
338  out:
339   if (repo_file)
340     fclose (repo_file);
341 }