62nd Cygnus<->FSF merge
[gcc.git] / gcc / cp / repo.c
1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Contributed by Jason Merrill (jason@cygnus.com)
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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. */
20
21 /* My strategy here is as follows:
22
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
25 explicit code.
26
27 I'm thinking of compiling with -frepo, running a Perl script to update
28 files, and then being able to rebuild everything with -fno-implicit.
29 Full automation can come later. */
30
31 #include <stdio.h>
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "input.h"
35
36 extern char * rindex ();
37
38 static tree pending_repo;
39 static char repo_name[1024];
40 static FILE *repo_file;
41
42 extern int flag_use_repository;
43 extern int errorcount, sorrycount;
44
45 static int repo_changed;
46
47 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
48 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
49
50 /* Record the flags used to compile this translation unit. */
51
52 void
53 repo_compile_flags (argc, argv)
54 int argc;
55 char **argv;
56 {
57 }
58
59 /* If this template has not been seen before, add a note to the repository
60 saying where the declaration was. This may be used to find the
61 definition at link time. */
62
63 void
64 repo_template_declared (t)
65 tree t;
66 {}
67
68 /* Note where the definition of a template lives so that instantiations can
69 be generated later. */
70
71 void
72 repo_template_defined (t)
73 tree t;
74 {}
75
76 /* Note where the definition of a class lives to that template
77 instantiations can use it. */
78
79 void
80 repo_class_defined (t)
81 tree t;
82 {}
83
84 /* Note that a template has been used. If we can see the definition, offer
85 to emit it. */
86
87 void
88 repo_template_used (t)
89 tree t;
90 {
91 tree id;
92
93 if (! flag_use_repository)
94 return;
95
96 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
97 {
98 id = DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t));
99 if (IDENTIFIER_REPO_CHOSEN (id))
100 mark_class_instantiated (t, 0);
101 }
102 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
103 {
104 id = DECL_ASSEMBLER_NAME (t);
105 if (IDENTIFIER_REPO_CHOSEN (id))
106 mark_function_instantiated (t, 0);
107 }
108 else
109 my_friendly_abort (1);
110
111 if (! IDENTIFIER_REPO_USED (id))
112 {
113 repo_changed = 1;
114 IDENTIFIER_REPO_USED (id) = 1;
115 }
116 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
117 }
118
119 /* Note that the vtable for a class has been used, and offer to emit it. */
120
121 void
122 repo_vtable_used (t)
123 tree t;
124 {
125 if (! flag_use_repository)
126 return;
127
128 pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
129 }
130
131 /* Note that an inline with external linkage has been used, and offer to
132 emit it. */
133
134 void
135 repo_inline_used (fn)
136 tree fn;
137 {
138 if (! flag_use_repository)
139 return;
140
141 /* Member functions of polymorphic classes go with their vtables. */
142 if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
143 {
144 repo_vtable_used (DECL_CLASS_CONTEXT (fn));
145 return;
146 }
147
148 pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
149 }
150
151 /* Note that a particular typeinfo node has been used, and offer to
152 emit it. */
153
154 void
155 repo_tinfo_used (ti)
156 tree ti;
157 {
158 }
159
160 static void
161 open_repo_file (filename)
162 char *filename;
163 {
164 register char *p, *q;
165 char *file = filename;
166 char *s = rindex (file, '/');
167 if (s == NULL)
168 s = file;
169 else
170 ++s;
171
172 for (p = repo_name, q = file; q < s; )
173 *p++ = *q++;
174 *p++ = '.';
175 strcpy (p, q);
176 strcat (p, ".repo");
177
178 repo_file = fopen (repo_name, "r");
179 }
180
181 void
182 init_repo (filename)
183 char *filename;
184 {
185 char buf[1024];
186
187 if (! flag_use_repository)
188 return;
189
190 open_repo_file (filename);
191
192 if (repo_file == 0)
193 return;
194
195 while (fgets (buf, 1024, repo_file))
196 {
197 int len = strlen (buf) - 1;
198 if (buf[len] != '\n')
199 error ("repository info line too long in %s", repo_name);
200 buf[len] = '\0';
201
202 switch (buf[0])
203 {
204 case 'A':
205 case 'M':
206 break;
207 case 'C':
208 case 'O':
209 {
210 tree id = get_identifier (&buf[2]);
211 IDENTIFIER_REPO_USED (id) = 1;
212 if (buf[0] == 'C')
213 IDENTIFIER_REPO_CHOSEN (id) = 1;
214 }
215 break;
216 default:
217 error ("mysterious repository information in %s", repo_name);
218 }
219 }
220 }
221
222 static void
223 reopen_repo_file_for_write ()
224 {
225 if (repo_file)
226 fclose (repo_file);
227 repo_file = fopen (repo_name, "w");
228
229 if (repo_file == 0)
230 {
231 error ("man't create repository information file `%s'", repo_name);
232 flag_use_repository = 0;
233 }
234 }
235
236 /* Emit any pending repos. */
237
238 void
239 finish_repo ()
240 {
241 tree t;
242 int changed = 0;
243
244 if (! flag_use_repository)
245 return;
246
247 /* Do we have to write out a new info file? */
248
249 if (! repo_changed || errorcount || sorrycount)
250 goto out;
251
252 reopen_repo_file_for_write ();
253
254 if (repo_file == 0)
255 goto out;
256
257 for (t = pending_repo; t; t = TREE_CHAIN (t))
258 {
259 tree val = TREE_VALUE (t);
260 char type;
261
262 if (TREE_CODE_CLASS (TREE_CODE (val)) == 't')
263 val = TYPE_MAIN_DECL (val);
264 val = DECL_ASSEMBLER_NAME (val);
265
266 if (! IDENTIFIER_REPO_USED (val))
267 continue;
268 IDENTIFIER_REPO_USED (val) = 0;
269
270 type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
271
272 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
273 }
274
275 out:
276 if (repo_file)
277 fclose (repo_file);
278 }