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
33 extern char * rindex ();
34 extern char * getenv ();
36 static tree pending_repo;
37 static char repo_name[1024];
38 static FILE *repo_file;
40 extern int flag_use_repository;
41 extern int errorcount, sorrycount;
43 static int repo_changed;
45 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
46 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
48 /* Record the flags used to compile this translation unit. */
51 repo_compile_flags (argc, argv)
57 /* If this template has not been seen before, add a note to the repository
58 saying where the declaration was. This may be used to find the
59 definition at link time. */
62 repo_template_declared (t)
66 /* Note where the definition of a template lives so that instantiations can
67 be generated later. */
70 repo_template_defined (t)
74 /* Note where the definition of a class lives to that template
75 instantiations can use it. */
78 repo_class_defined (t)
82 /* Note that a template has been used. If we can see the definition, offer
86 repo_template_used (t)
91 if (! flag_use_repository)
94 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
96 id = DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t));
97 if (IDENTIFIER_REPO_CHOSEN (id))
98 mark_class_instantiated (t, 0);
100 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
102 id = DECL_ASSEMBLER_NAME (t);
103 if (IDENTIFIER_REPO_CHOSEN (id))
104 mark_function_instantiated (t, 0);
107 my_friendly_abort (1);
109 if (! IDENTIFIER_REPO_USED (id))
112 IDENTIFIER_REPO_USED (id) = 1;
114 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
117 /* Note that the vtable for a class has been used, and offer to emit it. */
123 if (! flag_use_repository)
126 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
129 /* Note that an inline with external linkage has been used, and offer to
133 repo_inline_used (fn)
136 if (! flag_use_repository)
139 /* Member functions of polymorphic classes go with their vtables. */
140 if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
142 repo_vtable_used (DECL_CLASS_CONTEXT (fn));
146 pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
149 /* Note that a particular typeinfo node has been used, and offer to
163 register char *result = xmalloc (len + 1);
165 bcopy (s, result, len);
171 get_base_filename (filename)
174 char *p = getenv ("COLLECT_GCC_OPTIONS");
182 while (*q && *q != ' ') q++;
183 if (*p == '-' && p[1] == 'o')
190 while (*q && *q != ' ') q++;
193 output = save_string (p, q - p);
195 else if (*p == '-' && p[1] == 'c')
201 if (compiling && output)
204 return save_string (filename, strlen (filename));
208 open_repo_file (filename)
211 register char *p, *q;
212 char *file = get_base_filename (filename);
213 char *s = rindex (file, '/');
219 for (p = repo_name, q = file; q < s; )
222 if ((s = rindex (q, '.')) == NULL)
229 repo_file = fopen (repo_name, "r");
239 if (! flag_use_repository)
247 while (fgets (buf, 1024, repo_file))
249 int len = strlen (buf) - 1;
250 if (buf[len] != '\n')
251 error ("repository info line too long in %s", repo_name);
263 tree id = get_identifier (&buf[2]);
264 IDENTIFIER_REPO_USED (id) = 1;
266 IDENTIFIER_REPO_CHOSEN (id) = 1;
270 error ("mysterious repository information in %s", repo_name);
276 reopen_repo_file_for_write ()
280 repo_file = fopen (repo_name, "w");
284 error ("can't create repository information file `%s'", repo_name);
285 flag_use_repository = 0;
289 /* Emit any pending repos. */
297 if (! flag_use_repository)
300 /* Do we have to write out a new info file? */
302 if (! repo_changed || errorcount || sorrycount)
305 reopen_repo_file_for_write ();
310 fprintf (repo_file, "M %s\n", main_input_filename);
312 p = getenv ("COLLECT_GCC");
314 fprintf (repo_file, "G %s\n", p);
316 p = getenv ("COLLECT_GCC_OPTIONS");
318 fprintf (repo_file, "A %s\n", p);
320 for (t = pending_repo; t; t = TREE_CHAIN (t))
322 tree val = TREE_VALUE (t);
325 if (TREE_CODE_CLASS (TREE_CODE (val)) == 't')
326 val = TYPE_MAIN_DECL (val);
327 val = DECL_ASSEMBLER_NAME (val);
329 if (! IDENTIFIER_REPO_USED (val))
331 IDENTIFIER_REPO_USED (val) = 0;
333 type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
335 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));