1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* My strategy here is as follows:
23 Everything should be emitted in a translation unit where it is used.
24 The results of the automatic process should be easily reproducible with
34 extern char * rindex ();
35 extern char * getenv ();
36 extern char * getpwd ();
38 static tree pending_repo
;
39 static tree original_repo
;
40 static char *repo_name
;
41 static FILE *repo_file
;
43 extern int flag_use_repository
;
44 extern int errorcount
, sorrycount
;
45 extern struct obstack temporary_obstack
;
46 extern struct obstack permanent_obstack
;
48 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
49 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
51 /* Record the flags used to compile this translation unit. */
54 repo_compile_flags (argc
, argv
)
60 /* If this template has not been seen before, add a note to the repository
61 saying where the declaration was. This may be used to find the
62 definition at link time. */
65 repo_template_declared (t
)
69 /* Note where the definition of a template lives so that instantiations can
70 be generated later. */
73 repo_template_defined (t
)
77 /* Note where the definition of a class lives to that template
78 instantiations can use it. */
81 repo_class_defined (t
)
89 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
91 t
= TYPE_BINFO_VTABLE (t
);
95 return DECL_ASSEMBLER_NAME (t
);
98 /* Note that a template has been used. If we can see the definition, offer
102 repo_template_used (t
)
107 if (! flag_use_repository
)
110 id
= repo_get_id (t
);
114 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
116 if (IDENTIFIER_REPO_CHOSEN (id
))
117 mark_class_instantiated (t
, 0);
119 else if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
121 if (IDENTIFIER_REPO_CHOSEN (id
))
122 mark_function_instantiated (t
, 0);
125 my_friendly_abort (1);
127 if (! IDENTIFIER_REPO_USED (id
))
129 IDENTIFIER_REPO_USED (id
) = 1;
130 pending_repo
= perm_tree_cons (NULL_TREE
, id
, pending_repo
);
134 /* Note that the vtable for a class has been used, and offer to emit it. */
140 if (! flag_use_repository
)
143 pending_repo
= perm_tree_cons (NULL_TREE
, t
, pending_repo
);
146 /* Note that an inline with external linkage has been used, and offer to
150 repo_inline_used (fn
)
153 if (! flag_use_repository
)
156 /* Member functions of polymorphic classes go with their vtables. */
157 if (DECL_FUNCTION_MEMBER_P (fn
) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn
)))
159 repo_vtable_used (DECL_CLASS_CONTEXT (fn
));
163 pending_repo
= perm_tree_cons (NULL_TREE
, fn
, pending_repo
);
166 /* Note that a particular typeinfo node has been used, and offer to
176 repo_template_instantiated (t
, extern_p
)
182 tree id
= repo_get_id (t
);
184 IDENTIFIER_REPO_CHOSEN (id
) = 1;
193 return obstack_copy0 (&temporary_obstack
, s
, len
);
197 get_base_filename (filename
)
200 char *p
= getenv ("COLLECT_GCC_OPTIONS");
208 while (*q
&& *q
!= ' ') q
++;
209 if (*p
== '-' && p
[1] == 'o')
216 while (*q
&& *q
!= ' ') q
++;
219 output
= save_string (p
, q
- p
);
221 else if (*p
== '-' && p
[1] == 'c')
227 if (compiling
&& output
)
230 if (p
&& ! compiling
)
232 warning ("-frepo must be used with -c");
233 flag_use_repository
= 0;
237 p
= rindex (filename
, '/');
245 open_repo_file (filename
)
248 register char *p
, *q
;
249 char *s
= get_base_filename (filename
);
261 obstack_grow (&permanent_obstack
, s
, p
- s
);
262 repo_name
= obstack_copy0 (&permanent_obstack
, ".rpo", 4);
264 repo_file
= fopen (repo_name
, "r");
272 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
273 obstack_1grow (&temporary_obstack
, c
);
274 if (obstack_object_size (&temporary_obstack
) == 0)
276 obstack_1grow (&temporary_obstack
, '\0');
277 return obstack_finish (&temporary_obstack
);
286 if (! flag_use_repository
)
289 open_repo_file (filename
);
294 while (buf
= afgets (repo_file
))
305 tree id
= get_identifier (buf
+ 2);
310 IDENTIFIER_REPO_CHOSEN (id
) = 1;
311 orig
= integer_one_node
;
316 original_repo
= perm_tree_cons (orig
, id
, original_repo
);
320 error ("mysterious repository information in %s", repo_name
);
322 obstack_free (&temporary_obstack
, buf
);
327 reopen_repo_file_for_write ()
331 repo_file
= fopen (repo_name
, "w");
335 error ("can't create repository information file `%s'", repo_name
);
336 flag_use_repository
= 0;
340 /* Emit any pending repos. */
347 int repo_changed
= 0;
349 if (! flag_use_repository
)
352 /* Do we have to write out a new info file? */
354 /* Are there any old templates that aren't used any longer or that are
357 for (t
= original_repo
; t
; t
= TREE_CHAIN (t
))
359 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t
))
360 || (! TREE_PURPOSE (t
) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t
))))
365 IDENTIFIER_REPO_USED (TREE_VALUE (t
)) = 0;
368 /* Are there any templates that are newly used? */
371 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
373 if (IDENTIFIER_REPO_USED (TREE_VALUE (t
)))
380 if (! repo_changed
|| errorcount
|| sorrycount
)
383 reopen_repo_file_for_write ();
388 fprintf (repo_file
, "M %s\n", main_input_filename
);
391 fprintf (repo_file
, "D %s\n", p
);
393 p
= getenv ("COLLECT_GCC_OPTIONS");
395 fprintf (repo_file
, "A %s\n", p
);
397 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
399 tree val
= TREE_VALUE (t
);
400 char type
= IDENTIFIER_REPO_CHOSEN (val
) ? 'C' : 'O';
402 fprintf (repo_file
, "%c %s\n", type
, IDENTIFIER_POINTER (val
));