* class.c (build_base_path): Tidy a bit.
[official-gcc.git] / gcc / cp / repo.c
bloba9a5b35c293b51de6c71e720c180823545485dd6
1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Contributed by Jason Merrill (jason@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* My strategy here is as follows:
25 Everything should be emitted in a translation unit where it is used.
26 The results of the automatic process should be easily reproducible with
27 explicit code. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "cp-tree.h"
35 #include "input.h"
36 #include "obstack.h"
37 #include "toplev.h"
38 #include "diagnostic.h"
40 static tree repo_get_id (tree);
41 static char *extract_string (char **);
42 static const char *get_base_filename (const char *);
43 static void open_repo_file (const char *);
44 static char *afgets (FILE *);
45 static void reopen_repo_file_for_write (void);
47 static GTY(()) tree pending_repo;
48 static GTY(()) tree original_repo;
49 static char *repo_name;
50 static FILE *repo_file;
52 static const char *old_args, *old_dir, *old_main;
54 static struct obstack temporary_obstack;
56 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
57 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
59 #if 0
60 /* Record the flags used to compile this translation unit. */
62 void
63 repo_compile_flags (int argc, char **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. */
71 void
72 repo_template_declared (tree t)
75 /* Note where the definition of a template lives so that instantiations can
76 be generated later. */
78 void
79 repo_template_defined (tree t)
82 /* Note where the definition of a class lives to that template
83 instantiations can use it. */
85 void
86 repo_class_defined (tree t)
88 #endif
90 static tree
91 repo_get_id (tree t)
93 if (TYPE_P (t))
95 tree vtable;
97 /* If we're not done setting up the class, we may not have set up
98 the vtable, so going ahead would give the wrong answer.
99 See g++.pt/instantiate4.C. */
100 if (!COMPLETE_TYPE_P (t) || TYPE_BEING_DEFINED (t))
101 abort ();
103 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t));
105 t = vtable;
106 if (t == NULL_TREE)
107 return t;
109 return DECL_ASSEMBLER_NAME (t);
112 /* Note that a template has been used. If we can see the definition, offer
113 to emit it. */
115 void
116 repo_template_used (tree t)
118 tree id;
120 if (! flag_use_repository)
121 return;
123 id = repo_get_id (t);
124 if (id == NULL_TREE)
125 return;
127 if (TYPE_P (t))
129 if (IDENTIFIER_REPO_CHOSEN (id))
130 mark_class_instantiated (t, 0);
132 else if (DECL_P (t))
134 if (IDENTIFIER_REPO_CHOSEN (id))
135 /* It doesn't make sense to instantiate a clone, so we
136 instantiate the cloned function instead. Note that this
137 approach will not work correctly if collect2 assigns
138 different clones to different files -- but it shouldn't. */
139 mark_decl_instantiated (DECL_CLONED_FUNCTION_P (t)
140 ? DECL_CLONED_FUNCTION (t) : t,
143 else
144 abort ();
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 (tree t)
159 if (! flag_use_repository)
160 return;
162 pending_repo = tree_cons (NULL_TREE, t, pending_repo);
165 /* Note that an inline with external linkage has been used, and offer to
166 emit it. */
168 void
169 repo_inline_used (tree fn)
171 if (! flag_use_repository)
172 return;
174 /* Member functions of polymorphic classes go with their vtables. */
175 if (DECL_FUNCTION_MEMBER_P (fn)
176 && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn)))
178 repo_vtable_used (DECL_CONTEXT (fn));
179 return;
182 pending_repo = tree_cons (NULL_TREE, fn, pending_repo);
185 /* Note that a particular typeinfo node has been used, and offer to
186 emit it. */
188 void
189 repo_tinfo_used (tree ti)
192 #endif
194 void
195 repo_template_instantiated (tree t, bool extern_p)
197 if (! extern_p)
199 tree id = repo_get_id (t);
200 if (id)
201 IDENTIFIER_REPO_CHOSEN (id) = 1;
205 /* Parse a reasonable subset of shell quoting syntax. */
207 static char *
208 extract_string (char **pp)
210 char *p = *pp;
211 int backquote = 0;
212 int inside = 0;
214 for (;;)
216 char c = *p;
217 if (c == '\0')
218 break;
219 ++p;
220 if (backquote)
221 obstack_1grow (&temporary_obstack, c);
222 else if (! inside && c == ' ')
223 break;
224 else if (! inside && c == '\\')
225 backquote = 1;
226 else if (c == '\'')
227 inside = !inside;
228 else
229 obstack_1grow (&temporary_obstack, c);
232 obstack_1grow (&temporary_obstack, '\0');
233 *pp = p;
234 return obstack_finish (&temporary_obstack);
237 static const char *
238 get_base_filename (const char *filename)
240 char *p = getenv ("COLLECT_GCC_OPTIONS");
241 char *output = NULL;
242 int compiling = 0;
244 while (p && *p)
246 char *q = extract_string (&p);
248 if (strcmp (q, "-o") == 0)
249 output = extract_string (&p);
250 else if (strcmp (q, "-c") == 0)
251 compiling = 1;
254 if (compiling && output)
255 return output;
257 if (p && ! compiling)
259 warning ("-frepo must be used with -c");
260 flag_use_repository = 0;
261 return NULL;
264 return lbasename (filename);
267 static void
268 open_repo_file (const char *filename)
270 const char *p;
271 const char *s = get_base_filename (filename);
273 if (s == NULL)
274 return;
276 p = lbasename (s);
277 p = strrchr (p, '.');
278 if (! p)
279 p = s + strlen (s);
281 repo_name = xmalloc (p - s + 5);
282 memcpy (repo_name, s, p - s);
283 memcpy (repo_name + (p - s), ".rpo", 5);
285 repo_file = fopen (repo_name, "r");
288 static char *
289 afgets (FILE *stream)
291 int c;
292 while ((c = getc (stream)) != EOF && c != '\n')
293 obstack_1grow (&temporary_obstack, c);
294 if (obstack_object_size (&temporary_obstack) == 0)
295 return NULL;
296 obstack_1grow (&temporary_obstack, '\0');
297 return obstack_finish (&temporary_obstack);
300 void
301 init_repo (const char *filename)
303 char *buf;
305 if (! flag_use_repository)
306 return;
308 gcc_obstack_init (&temporary_obstack);
310 open_repo_file (filename);
312 if (repo_file == 0)
313 return;
315 while ((buf = afgets (repo_file)))
317 switch (buf[0])
319 case 'A':
320 old_args = ggc_strdup (buf + 2);
321 break;
322 case 'D':
323 old_dir = ggc_strdup (buf + 2);
324 break;
325 case 'M':
326 old_main = ggc_strdup (buf + 2);
327 break;
328 case 'C':
329 case 'O':
331 tree id = get_identifier (buf + 2);
332 tree orig;
334 if (buf[0] == 'C')
336 IDENTIFIER_REPO_CHOSEN (id) = 1;
337 orig = integer_one_node;
339 else
340 orig = NULL_TREE;
342 original_repo = tree_cons (orig, id, original_repo);
344 break;
345 default:
346 error ("mysterious repository information in %s", repo_name);
348 obstack_free (&temporary_obstack, buf);
352 static void
353 reopen_repo_file_for_write (void)
355 if (repo_file)
356 fclose (repo_file);
357 repo_file = fopen (repo_name, "w");
359 if (repo_file == 0)
361 error ("can't create repository information file `%s'", repo_name);
362 flag_use_repository = 0;
366 /* Emit any pending repos. */
368 void
369 finish_repo (void)
371 tree t;
372 bool repo_changed = false;
373 char *dir, *args;
375 if (!flag_use_repository)
376 return;
378 /* Do we have to write out a new info file? */
380 /* Are there any old templates that aren't used any longer or that are
381 newly chosen? */
383 for (t = original_repo; t; t = TREE_CHAIN (t))
385 if (!IDENTIFIER_REPO_USED (TREE_VALUE (t))
386 || (!TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
388 repo_changed = true;
389 break;
391 IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
394 /* Are there any templates that are newly used? */
396 if (!repo_changed)
397 for (t = pending_repo; t; t = TREE_CHAIN (t))
399 if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
401 repo_changed = true;
402 break;
406 dir = getpwd ();
407 args = getenv ("COLLECT_GCC_OPTIONS");
409 if (!repo_changed && pending_repo)
410 if (strcmp (old_main, main_input_filename) != 0
411 || strcmp (old_dir, dir) != 0
412 || (args == NULL) != (old_args == NULL)
413 || (args && strcmp (old_args, args) != 0))
414 repo_changed = true;
416 if (!repo_changed || errorcount || sorrycount)
417 goto out;
419 reopen_repo_file_for_write ();
421 if (repo_file == 0)
422 goto out;
424 fprintf (repo_file, "M %s\n", main_input_filename);
425 fprintf (repo_file, "D %s\n", dir);
426 if (args)
427 fprintf (repo_file, "A %s\n", args);
429 for (t = pending_repo; t; t = TREE_CHAIN (t))
431 tree val = TREE_VALUE (t);
432 char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
434 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
437 out:
438 if (repo_file)
439 fclose (repo_file);
442 #include "gt-cp-repo.h"