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)
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 #include "diagnostic.h"
38 static tree repo_get_id PARAMS ((tree));
39 static char *extract_string PARAMS ((char **));
40 static const char *get_base_filename PARAMS ((const char *));
41 static void open_repo_file PARAMS ((const char *));
42 static char *afgets PARAMS ((FILE *));
43 static void reopen_repo_file_for_write PARAMS ((void));
45 static GTY(()) tree pending_repo;
46 static GTY(()) tree original_repo;
47 static char *repo_name;
48 static FILE *repo_file;
50 static char *old_args, *old_dir, *old_main;
52 static struct obstack temporary_obstack;
54 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
55 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
58 /* Record the flags used to compile this translation unit. */
61 repo_compile_flags (argc, argv)
67 /* If this template has not been seen before, add a note to the repository
68 saying where the declaration was. This may be used to find the
69 definition at link time. */
72 repo_template_declared (t)
76 /* Note where the definition of a template lives so that instantiations can
77 be generated later. */
80 repo_template_defined (t)
84 /* Note where the definition of a class lives to that template
85 instantiations can use it. */
88 repo_class_defined (t)
101 /* If we're not done setting up the class, we may not have set up
102 the vtable, so going ahead would give the wrong answer.
103 See g++.pt/instantiate4.C. */
104 if (!COMPLETE_TYPE_P (t) || TYPE_BEING_DEFINED (t))
107 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t));
113 return DECL_ASSEMBLER_NAME (t);
116 /* Note that a template has been used. If we can see the definition, offer
120 repo_template_used (t)
125 if (! flag_use_repository)
128 id = repo_get_id (t);
134 if (IDENTIFIER_REPO_CHOSEN (id))
135 mark_class_instantiated (t, 0);
139 if (IDENTIFIER_REPO_CHOSEN (id))
140 /* It doesn't make sense to instantiate a clone, so we
141 instantiate the cloned function instead. Note that this
142 approach will not work correctly if collect2 assigns
143 different clones to different files -- but it shouldn't. */
144 mark_decl_instantiated (DECL_CLONED_FUNCTION_P (t)
145 ? DECL_CLONED_FUNCTION (t) : t,
151 if (! IDENTIFIER_REPO_USED (id))
153 IDENTIFIER_REPO_USED (id) = 1;
154 pending_repo = tree_cons (NULL_TREE, id, pending_repo);
159 /* Note that the vtable for a class has been used, and offer to emit it. */
165 if (! flag_use_repository)
168 pending_repo = tree_cons (NULL_TREE, t, pending_repo);
171 /* Note that an inline with external linkage has been used, and offer to
175 repo_inline_used (fn)
178 if (! flag_use_repository)
181 /* Member functions of polymorphic classes go with their vtables. */
182 if (DECL_FUNCTION_MEMBER_P (fn)
183 && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn)))
185 repo_vtable_used (DECL_CONTEXT (fn));
189 pending_repo = tree_cons (NULL_TREE, fn, pending_repo);
192 /* Note that a particular typeinfo node has been used, and offer to
203 repo_template_instantiated (t, extern_p)
209 tree id = repo_get_id (t);
211 IDENTIFIER_REPO_CHOSEN (id) = 1;
215 /* Parse a reasonable subset of shell quoting syntax. */
232 obstack_1grow (&temporary_obstack, c);
233 else if (! inside && c == ' ')
235 else if (! inside && c == '\\')
240 obstack_1grow (&temporary_obstack, c);
243 obstack_1grow (&temporary_obstack, '\0');
245 return obstack_finish (&temporary_obstack);
249 get_base_filename (filename)
250 const char *filename;
252 char *p = getenv ("COLLECT_GCC_OPTIONS");
258 char *q = extract_string (&p);
260 if (strcmp (q, "-o") == 0)
261 output = extract_string (&p);
262 else if (strcmp (q, "-c") == 0)
266 if (compiling && output)
269 if (p && ! compiling)
271 warning ("-frepo must be used with -c");
272 flag_use_repository = 0;
276 return lbasename (filename);
280 open_repo_file (filename)
281 const char *filename;
283 register const char *p;
284 const char *s = get_base_filename (filename);
290 p = strrchr (p, '.');
294 repo_name = xmalloc (p - s + 5);
295 memcpy (repo_name, s, p - s);
296 memcpy (repo_name + (p - s), ".rpo", 5);
298 repo_file = fopen (repo_name, "r");
306 while ((c = getc (stream)) != EOF && c != '\n')
307 obstack_1grow (&temporary_obstack, c);
308 if (obstack_object_size (&temporary_obstack) == 0)
310 obstack_1grow (&temporary_obstack, '\0');
311 return obstack_finish (&temporary_obstack);
316 const char *filename;
320 if (! flag_use_repository)
323 gcc_obstack_init (&temporary_obstack);
325 open_repo_file (filename);
330 while ((buf = afgets (repo_file)))
335 old_args = ggc_strdup (buf + 2);
338 old_dir = ggc_strdup (buf + 2);
341 old_main = ggc_strdup (buf + 2);
346 tree id = get_identifier (buf + 2);
351 IDENTIFIER_REPO_CHOSEN (id) = 1;
352 orig = integer_one_node;
357 original_repo = tree_cons (orig, id, original_repo);
361 error ("mysterious repository information in %s", repo_name);
363 obstack_free (&temporary_obstack, buf);
368 reopen_repo_file_for_write ()
372 repo_file = fopen (repo_name, "w");
376 error ("can't create repository information file `%s'", repo_name);
377 flag_use_repository = 0;
381 /* Emit any pending repos. */
387 int repo_changed = 0;
390 if (! flag_use_repository)
393 /* Do we have to write out a new info file? */
395 /* Are there any old templates that aren't used any longer or that are
398 for (t = original_repo; t; t = TREE_CHAIN (t))
400 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t))
401 || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
406 IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
409 /* Are there any templates that are newly used? */
412 for (t = pending_repo; t; t = TREE_CHAIN (t))
414 if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
422 args = getenv ("COLLECT_GCC_OPTIONS");
424 if (! repo_changed && pending_repo)
425 if (strcmp (old_main, main_input_filename) != 0
426 || strcmp (old_dir, dir) != 0
427 || (args == NULL) != (old_args == NULL)
428 || (args && strcmp (old_args, args) != 0))
431 if (! repo_changed || errorcount || sorrycount)
434 reopen_repo_file_for_write ();
439 fprintf (repo_file, "M %s\n", main_input_filename);
440 fprintf (repo_file, "D %s\n", dir);
442 fprintf (repo_file, "A %s\n", args);
444 for (t = pending_repo; t; t = TREE_CHAIN (t))
446 tree val = TREE_VALUE (t);
447 char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
449 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
457 #include "gt-cp-repo.h"