]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/repo.c
* call.c: Include system.h. Remove includes, declarations and
[thirdparty/gcc.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
35 extern char *getpwd PROTO((void));
36
37 static tree repo_get_id PROTO((tree));
38 static char *extract_string PROTO((char **));
39 static char *get_base_filename PROTO((char *));
40 static void open_repo_file PROTO((char *));
41 static char *afgets PROTO((FILE *));
42 static void reopen_repo_file_for_write PROTO((void));
43
44 static tree pending_repo;
45 static tree original_repo;
46 static char *repo_name;
47 static FILE *repo_file;
48
49 static char *old_args, *old_dir, *old_main;
50
51 extern int flag_use_repository;
52 extern int errorcount, sorrycount;
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 t = TYPE_BINFO_VTABLE (t);
102 if (t == NULL_TREE)
103 return t;
104 }
105 return DECL_ASSEMBLER_NAME (t);
106 }
107
108 /* Note that a template has been used. If we can see the definition, offer
109 to emit it. */
110
111 void
112 repo_template_used (t)
113 tree t;
114 {
115 tree id;
116
117 if (! flag_use_repository)
118 return;
119
120 id = repo_get_id (t);
121 if (id == NULL_TREE)
122 return;
123
124 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
125 {
126 if (IDENTIFIER_REPO_CHOSEN (id))
127 mark_class_instantiated (t, 0);
128 }
129 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
130 {
131 if (IDENTIFIER_REPO_CHOSEN (id))
132 mark_decl_instantiated (t, 0);
133 }
134 else
135 my_friendly_abort (1);
136
137 if (! IDENTIFIER_REPO_USED (id))
138 {
139 IDENTIFIER_REPO_USED (id) = 1;
140 pending_repo = perm_tree_cons (NULL_TREE, id, pending_repo);
141 }
142 }
143
144 #if 0
145 /* Note that the vtable for a class has been used, and offer to emit it. */
146
147 static void
148 repo_vtable_used (t)
149 tree t;
150 {
151 if (! flag_use_repository)
152 return;
153
154 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
155 }
156
157 /* Note that an inline with external linkage has been used, and offer to
158 emit it. */
159
160 void
161 repo_inline_used (fn)
162 tree fn;
163 {
164 if (! flag_use_repository)
165 return;
166
167 /* Member functions of polymorphic classes go with their vtables. */
168 if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
169 {
170 repo_vtable_used (DECL_CLASS_CONTEXT (fn));
171 return;
172 }
173
174 pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
175 }
176
177 /* Note that a particular typeinfo node has been used, and offer to
178 emit it. */
179
180 void
181 repo_tinfo_used (ti)
182 tree ti;
183 {
184 }
185 #endif
186
187 void
188 repo_template_instantiated (t, extern_p)
189 tree t;
190 int extern_p;
191 {
192 if (! extern_p)
193 {
194 tree id = repo_get_id (t);
195 if (id)
196 IDENTIFIER_REPO_CHOSEN (id) = 1;
197 }
198 }
199
200 /* Parse a reasonable subset of shell quoting syntax. */
201
202 static char *
203 extract_string (pp)
204 char **pp;
205 {
206 char *p = *pp;
207 int backquote = 0;
208 int inside = 0;
209
210 for (;;)
211 {
212 char c = *p;
213 if (c == '\0')
214 break;
215 ++p;
216 if (backquote)
217 obstack_1grow (&temporary_obstack, c);
218 else if (! inside && c == ' ')
219 break;
220 else if (! inside && c == '\\')
221 backquote = 1;
222 else if (c == '\'')
223 inside = !inside;
224 else
225 obstack_1grow (&temporary_obstack, c);
226 }
227
228 obstack_1grow (&temporary_obstack, '\0');
229 *pp = p;
230 return obstack_finish (&temporary_obstack);
231 }
232
233 static char *
234 get_base_filename (filename)
235 char *filename;
236 {
237 char *p = getenv ("COLLECT_GCC_OPTIONS");
238 char *output = NULL;
239 int compiling = 0;
240
241 while (p && *p)
242 {
243 char *q = extract_string (&p);
244
245 if (strcmp (q, "-o") == 0)
246 output = extract_string (&p);
247 else if (strcmp (q, "-c") == 0)
248 compiling = 1;
249 }
250
251 if (compiling && output)
252 return output;
253
254 if (p && ! compiling)
255 {
256 warning ("-frepo must be used with -c");
257 flag_use_repository = 0;
258 return NULL;
259 }
260
261 p = rindex (filename, '/');
262 if (p)
263 return p+1;
264 else
265 return filename;
266 }
267
268 static void
269 open_repo_file (filename)
270 char *filename;
271 {
272 register char *p;
273 char *s = get_base_filename (filename);
274
275 if (s == NULL)
276 return;
277
278 p = rindex (s, '/');
279 if (! p)
280 p = 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 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 }