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)
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)
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
36 extern char *getpwd PROTO((void));
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));
45 static tree pending_repo;
46 static tree original_repo;
47 static char *repo_name;
48 static FILE *repo_file;
50 static char *old_args, *old_dir, *old_main;
52 extern int flag_use_repository;
53 extern int errorcount, sorrycount;
54 extern struct obstack temporary_obstack;
55 extern struct obstack permanent_obstack;
57 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
58 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
61 /* Record the flags used to compile this translation unit. */
64 repo_compile_flags (argc, argv)
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. */
75 repo_template_declared (t)
79 /* Note where the definition of a template lives so that instantiations can
80 be generated later. */
83 repo_template_defined (t)
87 /* Note where the definition of a class lives to that template
88 instantiations can use it. */
91 repo_class_defined (t)
100 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
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 (TYPE_SIZE (t) == NULL_TREE || TYPE_BEING_DEFINED (t))
106 my_friendly_abort (981113);
108 t = TYPE_BINFO_VTABLE (t);
112 return DECL_ASSEMBLER_NAME (t);
115 /* Note that a template has been used. If we can see the definition, offer
119 repo_template_used (t)
124 if (! flag_use_repository)
127 id = repo_get_id (t);
131 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
133 if (IDENTIFIER_REPO_CHOSEN (id))
134 mark_class_instantiated (t, 0);
136 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
138 if (IDENTIFIER_REPO_CHOSEN (id))
139 mark_decl_instantiated (t, 0);
142 my_friendly_abort (1);
144 if (! IDENTIFIER_REPO_USED (id))
146 IDENTIFIER_REPO_USED (id) = 1;
147 pending_repo = perm_tree_cons (NULL_TREE, id, pending_repo);
152 /* Note that the vtable for a class has been used, and offer to emit it. */
158 if (! flag_use_repository)
161 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
164 /* Note that an inline with external linkage has been used, and offer to
168 repo_inline_used (fn)
171 if (! flag_use_repository)
174 /* Member functions of polymorphic classes go with their vtables. */
175 if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
177 repo_vtable_used (DECL_CLASS_CONTEXT (fn));
181 pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
184 /* Note that a particular typeinfo node has been used, and offer to
195 repo_template_instantiated (t, extern_p)
201 tree id = repo_get_id (t);
203 IDENTIFIER_REPO_CHOSEN (id) = 1;
207 /* Parse a reasonable subset of shell quoting syntax. */
224 obstack_1grow (&temporary_obstack, c);
225 else if (! inside && c == ' ')
227 else if (! inside && c == '\\')
232 obstack_1grow (&temporary_obstack, c);
235 obstack_1grow (&temporary_obstack, '\0');
237 return obstack_finish (&temporary_obstack);
241 get_base_filename (filename)
242 const char *filename;
244 char *p = getenv ("COLLECT_GCC_OPTIONS");
250 char *q = extract_string (&p);
252 if (strcmp (q, "-o") == 0)
253 output = extract_string (&p);
254 else if (strcmp (q, "-c") == 0)
258 if (compiling && output)
261 if (p && ! compiling)
263 warning ("-frepo must be used with -c");
264 flag_use_repository = 0;
268 return file_name_nondirectory (filename);
272 open_repo_file (filename)
273 const char *filename;
275 register const char *p;
276 const char *s = get_base_filename (filename);
281 p = file_name_nondirectory (s);
286 obstack_grow (&permanent_obstack, s, p - s);
287 repo_name = obstack_copy0 (&permanent_obstack, ".rpo", 4);
289 repo_file = fopen (repo_name, "r");
297 while ((c = getc (stream)) != EOF && c != '\n')
298 obstack_1grow (&temporary_obstack, c);
299 if (obstack_object_size (&temporary_obstack) == 0)
301 obstack_1grow (&temporary_obstack, '\0');
302 return obstack_finish (&temporary_obstack);
307 const char *filename;
311 if (! flag_use_repository)
314 open_repo_file (filename);
319 while ((buf = afgets (repo_file)))
324 old_args = obstack_copy0 (&permanent_obstack, buf + 2,
328 old_dir = obstack_copy0 (&permanent_obstack, buf + 2,
332 old_main = obstack_copy0 (&permanent_obstack, buf + 2,
338 tree id = get_identifier (buf + 2);
343 IDENTIFIER_REPO_CHOSEN (id) = 1;
344 orig = integer_one_node;
349 original_repo = perm_tree_cons (orig, id, original_repo);
353 error ("mysterious repository information in %s", repo_name);
355 obstack_free (&temporary_obstack, buf);
360 reopen_repo_file_for_write ()
364 repo_file = fopen (repo_name, "w");
368 error ("can't create repository information file `%s'", repo_name);
369 flag_use_repository = 0;
373 /* Emit any pending repos. */
379 int repo_changed = 0;
382 if (! flag_use_repository)
385 /* Do we have to write out a new info file? */
387 /* Are there any old templates that aren't used any longer or that are
390 for (t = original_repo; t; t = TREE_CHAIN (t))
392 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t))
393 || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
398 IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
401 /* Are there any templates that are newly used? */
404 for (t = pending_repo; t; t = TREE_CHAIN (t))
406 if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
414 args = getenv ("COLLECT_GCC_OPTIONS");
416 if (! repo_changed && pending_repo)
417 if (strcmp (old_main, main_input_filename) != 0
418 || strcmp (old_dir, dir) != 0
419 || (args == NULL) != (old_args == NULL)
420 || (args && strcmp (old_args, args) != 0))
423 if (! repo_changed || errorcount || sorrycount)
426 reopen_repo_file_for_write ();
431 fprintf (repo_file, "M %s\n", main_input_filename);
432 fprintf (repo_file, "D %s\n", dir);
434 fprintf (repo_file, "A %s\n", args);
436 for (t = pending_repo; t; t = TREE_CHAIN (t))
438 tree val = TREE_VALUE (t);
439 char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
441 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));