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