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, 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
35 extern char * rindex ();
36 extern char * getenv ();
37 extern char * getpwd ();
39 static tree pending_repo
;
40 static tree original_repo
;
41 static char *repo_name
;
42 static FILE *repo_file
;
44 extern int flag_use_repository
;
45 extern int errorcount
, sorrycount
;
46 extern struct obstack temporary_obstack
;
47 extern struct obstack permanent_obstack
;
49 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
50 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
53 /* Record the flags used to compile this translation unit. */
56 repo_compile_flags (argc
, argv
)
62 /* If this template has not been seen before, add a note to the repository
63 saying where the declaration was. This may be used to find the
64 definition at link time. */
67 repo_template_declared (t
)
71 /* Note where the definition of a template lives so that instantiations can
72 be generated later. */
75 repo_template_defined (t
)
79 /* Note where the definition of a class lives to that template
80 instantiations can use it. */
83 repo_class_defined (t
)
92 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
94 t
= TYPE_BINFO_VTABLE (t
);
98 return DECL_ASSEMBLER_NAME (t
);
101 /* Note that a template has been used. If we can see the definition, offer
105 repo_template_used (t
)
110 if (! flag_use_repository
)
113 id
= repo_get_id (t
);
117 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
119 if (IDENTIFIER_REPO_CHOSEN (id
))
120 mark_class_instantiated (t
, 0);
122 else if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
124 if (IDENTIFIER_REPO_CHOSEN (id
))
125 mark_decl_instantiated (t
, 0);
128 my_friendly_abort (1);
130 if (! IDENTIFIER_REPO_USED (id
))
132 IDENTIFIER_REPO_USED (id
) = 1;
133 pending_repo
= perm_tree_cons (NULL_TREE
, id
, pending_repo
);
138 /* Note that the vtable for a class has been used, and offer to emit it. */
144 if (! flag_use_repository
)
147 pending_repo
= perm_tree_cons (NULL_TREE
, t
, pending_repo
);
150 /* Note that an inline with external linkage has been used, and offer to
154 repo_inline_used (fn
)
157 if (! flag_use_repository
)
160 /* Member functions of polymorphic classes go with their vtables. */
161 if (DECL_FUNCTION_MEMBER_P (fn
) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn
)))
163 repo_vtable_used (DECL_CLASS_CONTEXT (fn
));
167 pending_repo
= perm_tree_cons (NULL_TREE
, fn
, pending_repo
);
170 /* Note that a particular typeinfo node has been used, and offer to
181 repo_template_instantiated (t
, extern_p
)
187 tree id
= repo_get_id (t
);
189 IDENTIFIER_REPO_CHOSEN (id
) = 1;
198 return obstack_copy0 (&temporary_obstack
, s
, len
);
202 get_base_filename (filename
)
205 char *p
= getenv ("COLLECT_GCC_OPTIONS");
213 while (*q
&& *q
!= ' ') q
++;
214 if (*p
== '-' && p
[1] == 'o')
221 while (*q
&& *q
!= ' ') q
++;
224 output
= save_string (p
, q
- p
);
226 else if (*p
== '-' && p
[1] == 'c')
232 if (compiling
&& output
)
235 if (p
&& ! compiling
)
237 warning ("-frepo must be used with -c");
238 flag_use_repository
= 0;
242 p
= rindex (filename
, '/');
250 open_repo_file (filename
)
254 char *s
= get_base_filename (filename
);
266 obstack_grow (&permanent_obstack
, s
, p
- s
);
267 repo_name
= obstack_copy0 (&permanent_obstack
, ".rpo", 4);
269 repo_file
= fopen (repo_name
, "r");
277 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
278 obstack_1grow (&temporary_obstack
, c
);
279 if (obstack_object_size (&temporary_obstack
) == 0)
281 obstack_1grow (&temporary_obstack
, '\0');
282 return obstack_finish (&temporary_obstack
);
291 if (! flag_use_repository
)
294 open_repo_file (filename
);
299 while (buf
= afgets (repo_file
))
310 tree id
= get_identifier (buf
+ 2);
315 IDENTIFIER_REPO_CHOSEN (id
) = 1;
316 orig
= integer_one_node
;
321 original_repo
= perm_tree_cons (orig
, id
, original_repo
);
325 error ("mysterious repository information in %s", repo_name
);
327 obstack_free (&temporary_obstack
, buf
);
332 reopen_repo_file_for_write ()
336 repo_file
= fopen (repo_name
, "w");
340 error ("can't create repository information file `%s'", repo_name
);
341 flag_use_repository
= 0;
345 /* Emit any pending repos. */
352 int repo_changed
= 0;
354 if (! flag_use_repository
)
357 /* Do we have to write out a new info file? */
359 /* Are there any old templates that aren't used any longer or that are
362 for (t
= original_repo
; t
; t
= TREE_CHAIN (t
))
364 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t
))
365 || (! TREE_PURPOSE (t
) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t
))))
370 IDENTIFIER_REPO_USED (TREE_VALUE (t
)) = 0;
373 /* Are there any templates that are newly used? */
376 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
378 if (IDENTIFIER_REPO_USED (TREE_VALUE (t
)))
385 if (! repo_changed
|| errorcount
|| sorrycount
)
388 reopen_repo_file_for_write ();
393 fprintf (repo_file
, "M %s\n", main_input_filename
);
396 fprintf (repo_file
, "D %s\n", p
);
398 p
= getenv ("COLLECT_GCC_OPTIONS");
400 fprintf (repo_file
, "A %s\n", p
);
402 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
404 tree val
= TREE_VALUE (t
);
405 char type
= IDENTIFIER_REPO_CHOSEN (val
) ? 'C' : 'O';
407 fprintf (repo_file
, "%c %s\n", type
, IDENTIFIER_POINTER (val
));