OSDN Git Service

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