OSDN Git Service

67th 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 #include "obstack.h"
33
34 extern char * rindex ();
35 extern char * getenv ();
36 extern char * getpwd ();
37
38 static tree pending_repo;
39 static tree original_repo;
40 static char *repo_name;
41 static FILE *repo_file;
42
43 extern int flag_use_repository;
44 extern int errorcount, sorrycount;
45 extern struct obstack temporary_obstack;
46 extern struct obstack permanent_obstack;
47
48 #define IDENTIFIER_REPO_USED(NODE)   (TREE_LANG_FLAG_3 (NODE))
49 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
50
51 /* Record the flags used to compile this translation unit.  */
52
53 void
54 repo_compile_flags (argc, argv)
55      int argc;
56      char **argv;
57 {
58 }
59
60 /* If this template has not been seen before, add a note to the repository
61    saying where the declaration was.  This may be used to find the
62    definition at link time.  */
63
64 void
65 repo_template_declared (t)
66      tree t;
67 {}
68
69 /* Note where the definition of a template lives so that instantiations can
70    be generated later.  */
71
72 void
73 repo_template_defined (t)
74      tree t;
75 {}
76
77 /* Note where the definition of a class lives to that template
78    instantiations can use it.  */
79
80 void
81 repo_class_defined (t)
82      tree t;
83 {}
84
85 tree
86 repo_get_id (t)
87      tree t;
88 {
89   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
90     {
91       t = TYPE_BINFO_VTABLE (t);
92       if (t == NULL_TREE)
93         return t;
94     }
95   return DECL_ASSEMBLER_NAME (t);
96 }
97
98 /* Note that a template has been used.  If we can see the definition, offer
99    to emit it. */
100
101 void
102 repo_template_used (t)
103      tree t;
104 {
105   tree id;
106
107   if (! flag_use_repository)
108     return;
109
110   id = repo_get_id (t);
111   if (id == NULL_TREE)
112     return;
113   
114   if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
115     {
116       if (IDENTIFIER_REPO_CHOSEN (id))
117         mark_class_instantiated (t, 0);
118     }
119   else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
120     {
121       if (IDENTIFIER_REPO_CHOSEN (id))
122         mark_function_instantiated (t, 0);
123     }
124   else
125     my_friendly_abort (1);
126
127   if (! IDENTIFIER_REPO_USED (id))
128     {
129       IDENTIFIER_REPO_USED (id) = 1;
130       pending_repo = perm_tree_cons (NULL_TREE, id, pending_repo);
131     }
132 }
133
134 /* Note that the vtable for a class has been used, and offer to emit it.  */
135
136 void
137 repo_vtable_used (t)
138      tree t;
139 {
140   if (! flag_use_repository)
141     return;
142
143   pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
144 }
145
146 /* Note that an inline with external linkage has been used, and offer to
147    emit it.  */
148
149 void
150 repo_inline_used (fn)
151      tree fn;
152 {
153   if (! flag_use_repository)
154     return;
155
156   /* Member functions of polymorphic classes go with their vtables.  */
157   if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
158     {
159       repo_vtable_used (DECL_CLASS_CONTEXT (fn));
160       return;
161     }
162
163   pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
164 }
165
166 /* Note that a particular typeinfo node has been used, and offer to
167    emit it.  */
168
169 void
170 repo_tinfo_used (ti)
171      tree ti;
172 {
173 }
174
175 void
176 repo_template_instantiated (t, extern_p)
177      tree t;
178      int extern_p;
179 {
180   if (! extern_p)
181     {
182       tree id = repo_get_id (t);
183       if (id)
184         IDENTIFIER_REPO_CHOSEN (id) = 1;
185     }
186 }
187
188 static char *
189 save_string (s, len)
190      char *s;
191      int len;
192 {
193   return obstack_copy0 (&temporary_obstack, s, len);
194 }
195
196 static char *
197 get_base_filename (filename)
198      char *filename;
199 {
200   char *p = getenv ("COLLECT_GCC_OPTIONS");
201   char *output = 0;
202   int compiling = 0;
203
204   if (p)
205     while (*p)
206       {
207         char *q = p;
208         while (*q && *q != ' ') q++;
209         if (*p == '-' && p[1] == 'o')
210           {
211             p += 2;
212             if (p == q)
213               {
214                 p++; q++;
215                 if (*q)
216                   while (*q && *q != ' ') q++;
217               }
218
219             output = save_string (p, q - p);
220           }
221         else if (*p == '-' && p[1] == 'c')
222           compiling = 1;
223         if (*q) q++;
224         p = q;
225       }
226
227   if (compiling && output)
228     return output;
229
230   if (p && ! compiling)
231     {
232       warning ("-frepo must be used with -c");
233       flag_use_repository = 0;
234       return NULL;
235     }
236
237   p = rindex (filename, '/');
238   if (p)
239     return p+1;
240   else
241     return filename;
242 }        
243
244 static void
245 open_repo_file (filename)
246      char *filename;
247 {
248   register char *p, *q;
249   char *s = get_base_filename (filename);
250
251   if (s == NULL)
252     return;
253
254   p = rindex (s, '/');
255   if (! p)
256     p = s;
257   p = rindex (p, '.');
258   if (! p)
259     p = s + strlen (s);
260
261   obstack_grow (&permanent_obstack, s, p - s);
262   repo_name = obstack_copy0 (&permanent_obstack, ".rpo", 4);
263
264   repo_file = fopen (repo_name, "r");
265 }
266
267 static char *
268 afgets (stream)
269      FILE *stream;
270 {
271   int c;
272   while ((c = getc (stream)) != EOF && c != '\n')
273     obstack_1grow (&temporary_obstack, c);
274   if (obstack_object_size (&temporary_obstack) == 0)
275     return NULL;
276   obstack_1grow (&temporary_obstack, '\0');
277   return obstack_finish (&temporary_obstack);
278 }
279
280 void
281 init_repo (filename)
282      char *filename;
283 {
284   char *buf;
285
286   if (! flag_use_repository)
287     return;
288
289   open_repo_file (filename);
290
291   if (repo_file == 0)
292     return;
293
294   while (buf = afgets (repo_file))
295     {
296       switch (buf[0])
297         {
298         case 'A':
299         case 'D':
300         case 'M':
301           break;
302         case 'C':
303         case 'O':
304           {
305             tree id = get_identifier (buf + 2);
306             tree orig;
307
308             if (buf[0] == 'C')
309               {
310                 IDENTIFIER_REPO_CHOSEN (id) = 1;
311                 orig = integer_one_node;
312               }
313             else
314               orig = NULL_TREE;
315
316             original_repo = perm_tree_cons (orig, id, original_repo);
317           }
318           break;
319         default:
320           error ("mysterious repository information in %s", repo_name);
321         }
322       obstack_free (&temporary_obstack, buf);
323     }
324 }
325
326 static void
327 reopen_repo_file_for_write ()
328 {
329   if (repo_file)
330     fclose (repo_file);
331   repo_file = fopen (repo_name, "w");
332
333   if (repo_file == 0)
334     {
335       error ("can't create repository information file `%s'", repo_name);
336       flag_use_repository = 0;
337     }
338 }
339
340 /* Emit any pending repos.  */
341
342 void
343 finish_repo ()
344 {
345   tree t;
346   char *p;
347   int repo_changed = 0;
348
349   if (! flag_use_repository)
350     return;
351
352   /* Do we have to write out a new info file?  */
353
354   /* Are there any old templates that aren't used any longer or that are
355      newly chosen?  */
356   
357   for (t = original_repo; t; t = TREE_CHAIN (t))
358     {
359       if (! IDENTIFIER_REPO_USED (TREE_VALUE (t))
360           || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
361         {
362           repo_changed = 1;
363           break;
364         }
365       IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
366     }
367
368   /* Are there any templates that are newly used?  */
369   
370   if (! repo_changed)
371     for (t = pending_repo; t; t = TREE_CHAIN (t))
372       {
373         if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
374           {
375             repo_changed = 1;
376             break;
377           }
378       }
379
380   if (! repo_changed || errorcount || sorrycount)
381     goto out;
382
383   reopen_repo_file_for_write ();
384
385   if (repo_file == 0)
386     goto out;
387
388   fprintf (repo_file, "M %s\n", main_input_filename);
389
390   p = getpwd ();
391   fprintf (repo_file, "D %s\n", p);
392
393   p = getenv ("COLLECT_GCC_OPTIONS");
394   if (p != 0)
395     fprintf (repo_file, "A %s\n", p);
396
397   for (t = pending_repo; t; t = TREE_CHAIN (t))
398     {
399       tree val = TREE_VALUE (t);
400       char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
401
402       fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
403     }
404
405  out:
406   if (repo_file)
407     fclose (repo_file);
408 }