* lib/objc.exp: Add -lposix4 on Solaris 2.6 and Solaris 2.7.
[official-gcc.git] / gcc / cp / repo.c
blobe86e1f0805b77cbcc8d04181945f22cced233ab6
1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Jason Merrill (jason@cygnus.com)
5 This file is part of GNU CC.
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.
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.
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. */
22 /* My strategy here is as follows:
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. */
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 #include "ggc.h"
37 static tree repo_get_id PARAMS ((tree));
38 static char *extract_string PARAMS ((char **));
39 static char *get_base_filename PARAMS ((const char *));
40 static void open_repo_file PARAMS ((const char *));
41 static char *afgets PARAMS ((FILE *));
42 static void reopen_repo_file_for_write PARAMS ((void));
44 static tree pending_repo;
45 static tree original_repo;
46 static char *repo_name;
47 static FILE *repo_file;
49 static char *old_args, *old_dir, *old_main;
51 extern int flag_use_repository;
52 static struct obstack temporary_obstack;
53 extern struct obstack permanent_obstack;
55 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
56 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
58 #if 0
59 /* Record the flags used to compile this translation unit. */
61 void
62 repo_compile_flags (argc, argv)
63 int argc;
64 char **argv;
68 /* If this template has not been seen before, add a note to the repository
69 saying where the declaration was. This may be used to find the
70 definition at link time. */
72 void
73 repo_template_declared (t)
74 tree t;
77 /* Note where the definition of a template lives so that instantiations can
78 be generated later. */
80 void
81 repo_template_defined (t)
82 tree t;
85 /* Note where the definition of a class lives to that template
86 instantiations can use it. */
88 void
89 repo_class_defined (t)
90 tree t;
92 #endif
94 static tree
95 repo_get_id (t)
96 tree t;
98 if (TYPE_P (t))
100 tree vtable;
102 /* If we're not done setting up the class, we may not have set up
103 the vtable, so going ahead would give the wrong answer.
104 See g++.pt/instantiate4.C. */
105 if (!COMPLETE_TYPE_P (t) || TYPE_BEING_DEFINED (t))
106 my_friendly_abort (981113);
108 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t));
110 t = vtable;
111 if (t == NULL_TREE)
112 return t;
114 return DECL_ASSEMBLER_NAME (t);
117 /* Note that a template has been used. If we can see the definition, offer
118 to emit it. */
120 void
121 repo_template_used (t)
122 tree t;
124 tree id;
126 if (! flag_use_repository)
127 return;
129 id = repo_get_id (t);
130 if (id == NULL_TREE)
131 return;
133 if (TYPE_P (t))
135 if (IDENTIFIER_REPO_CHOSEN (id))
136 mark_class_instantiated (t, 0);
138 else if (DECL_P (t))
140 if (IDENTIFIER_REPO_CHOSEN (id))
141 mark_decl_instantiated (t, 0);
143 else
144 my_friendly_abort (1);
146 if (! IDENTIFIER_REPO_USED (id))
148 IDENTIFIER_REPO_USED (id) = 1;
149 pending_repo = tree_cons (NULL_TREE, id, pending_repo);
153 #if 0
154 /* Note that the vtable for a class has been used, and offer to emit it. */
156 static void
157 repo_vtable_used (t)
158 tree t;
160 if (! flag_use_repository)
161 return;
163 pending_repo = tree_cons (NULL_TREE, t, pending_repo);
166 /* Note that an inline with external linkage has been used, and offer to
167 emit it. */
169 void
170 repo_inline_used (fn)
171 tree fn;
173 if (! flag_use_repository)
174 return;
176 /* Member functions of polymorphic classes go with their vtables. */
177 if (DECL_FUNCTION_MEMBER_P (fn)
178 && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn)))
180 repo_vtable_used (DECL_CONTEXT (fn));
181 return;
184 pending_repo = tree_cons (NULL_TREE, fn, pending_repo);
187 /* Note that a particular typeinfo node has been used, and offer to
188 emit it. */
190 void
191 repo_tinfo_used (ti)
192 tree ti;
195 #endif
197 void
198 repo_template_instantiated (t, extern_p)
199 tree t;
200 int extern_p;
202 if (! extern_p)
204 tree id = repo_get_id (t);
205 if (id)
206 IDENTIFIER_REPO_CHOSEN (id) = 1;
210 /* Parse a reasonable subset of shell quoting syntax. */
212 static char *
213 extract_string (pp)
214 char **pp;
216 char *p = *pp;
217 int backquote = 0;
218 int inside = 0;
220 for (;;)
222 char c = *p;
223 if (c == '\0')
224 break;
225 ++p;
226 if (backquote)
227 obstack_1grow (&temporary_obstack, c);
228 else if (! inside && c == ' ')
229 break;
230 else if (! inside && c == '\\')
231 backquote = 1;
232 else if (c == '\'')
233 inside = !inside;
234 else
235 obstack_1grow (&temporary_obstack, c);
238 obstack_1grow (&temporary_obstack, '\0');
239 *pp = p;
240 return obstack_finish (&temporary_obstack);
243 static char *
244 get_base_filename (filename)
245 const char *filename;
247 char *p = getenv ("COLLECT_GCC_OPTIONS");
248 char *output = NULL;
249 int compiling = 0;
251 while (p && *p)
253 char *q = extract_string (&p);
255 if (strcmp (q, "-o") == 0)
256 output = extract_string (&p);
257 else if (strcmp (q, "-c") == 0)
258 compiling = 1;
261 if (compiling && output)
262 return output;
264 if (p && ! compiling)
266 warning ("-frepo must be used with -c");
267 flag_use_repository = 0;
268 return NULL;
271 return file_name_nondirectory (filename);
274 static void
275 open_repo_file (filename)
276 const char *filename;
278 register const char *p;
279 const char *s = get_base_filename (filename);
281 if (s == NULL)
282 return;
284 p = file_name_nondirectory (s);
285 p = strrchr (p, '.');
286 if (! p)
287 p = s + strlen (s);
289 obstack_grow (&permanent_obstack, s, p - s);
290 repo_name = obstack_copy0 (&permanent_obstack, ".rpo", 4);
292 repo_file = fopen (repo_name, "r");
295 static char *
296 afgets (stream)
297 FILE *stream;
299 int c;
300 while ((c = getc (stream)) != EOF && c != '\n')
301 obstack_1grow (&temporary_obstack, c);
302 if (obstack_object_size (&temporary_obstack) == 0)
303 return NULL;
304 obstack_1grow (&temporary_obstack, '\0');
305 return obstack_finish (&temporary_obstack);
308 void
309 init_repo (filename)
310 const char *filename;
312 char *buf;
314 if (! flag_use_repository)
315 return;
317 ggc_add_tree_root (&pending_repo, 1);
318 ggc_add_tree_root (&original_repo, 1);
319 gcc_obstack_init (&temporary_obstack);
321 open_repo_file (filename);
323 if (repo_file == 0)
324 return;
326 while ((buf = afgets (repo_file)))
328 switch (buf[0])
330 case 'A':
331 old_args = obstack_copy0 (&permanent_obstack, buf + 2,
332 strlen (buf + 2));
333 break;
334 case 'D':
335 old_dir = obstack_copy0 (&permanent_obstack, buf + 2,
336 strlen (buf + 2));
337 break;
338 case 'M':
339 old_main = obstack_copy0 (&permanent_obstack, buf + 2,
340 strlen (buf + 2));
341 break;
342 case 'C':
343 case 'O':
345 tree id = get_identifier (buf + 2);
346 tree orig;
348 if (buf[0] == 'C')
350 IDENTIFIER_REPO_CHOSEN (id) = 1;
351 orig = integer_one_node;
353 else
354 orig = NULL_TREE;
356 original_repo = tree_cons (orig, id, original_repo);
358 break;
359 default:
360 error ("mysterious repository information in %s", repo_name);
362 obstack_free (&temporary_obstack, buf);
366 static void
367 reopen_repo_file_for_write ()
369 if (repo_file)
370 fclose (repo_file);
371 repo_file = fopen (repo_name, "w");
373 if (repo_file == 0)
375 error ("can't create repository information file `%s'", repo_name);
376 flag_use_repository = 0;
380 /* Emit any pending repos. */
382 void
383 finish_repo ()
385 tree t;
386 int repo_changed = 0;
387 char *dir, *args;
389 if (! flag_use_repository)
390 return;
392 /* Do we have to write out a new info file? */
394 /* Are there any old templates that aren't used any longer or that are
395 newly chosen? */
397 for (t = original_repo; t; t = TREE_CHAIN (t))
399 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t))
400 || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
402 repo_changed = 1;
403 break;
405 IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
408 /* Are there any templates that are newly used? */
410 if (! repo_changed)
411 for (t = pending_repo; t; t = TREE_CHAIN (t))
413 if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
415 repo_changed = 1;
416 break;
420 dir = getpwd ();
421 args = getenv ("COLLECT_GCC_OPTIONS");
423 if (! repo_changed && pending_repo)
424 if (strcmp (old_main, main_input_filename) != 0
425 || strcmp (old_dir, dir) != 0
426 || (args == NULL) != (old_args == NULL)
427 || (args && strcmp (old_args, args) != 0))
428 repo_changed = 1;
430 if (! repo_changed || errorcount || sorrycount)
431 goto out;
433 reopen_repo_file_for_write ();
435 if (repo_file == 0)
436 goto out;
438 fprintf (repo_file, "M %s\n", main_input_filename);
439 fprintf (repo_file, "D %s\n", dir);
440 if (args)
441 fprintf (repo_file, "A %s\n", args);
443 for (t = pending_repo; t; t = TREE_CHAIN (t))
445 tree val = TREE_VALUE (t);
446 char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
448 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
451 out:
452 if (repo_file)
453 fclose (repo_file);