OSDN Git Service

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