]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/repo.c
Merge from pch-branch.
[thirdparty/gcc.git] / gcc / cp / repo.c
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)
5
6 This file is part of GCC.
7
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.
12
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.
17
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. */
22
23 /* My strategy here is as follows:
24
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. */
28
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"
39
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);
46
47 static GTY(()) tree pending_repo;
48 static GTY(()) tree original_repo;
49 static char *repo_name;
50 static FILE *repo_file;
51
52 static const char *old_args, *old_dir, *old_main;
53
54 static struct obstack temporary_obstack;
55
56 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
57 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
58
59 #if 0
60 /* Record the flags used to compile this translation unit. */
61
62 void
63 repo_compile_flags (int argc, char **argv)
64 {
65 }
66
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. */
70
71 void
72 repo_template_declared (tree t)
73 {}
74
75 /* Note where the definition of a template lives so that instantiations can
76 be generated later. */
77
78 void
79 repo_template_defined (tree t)
80 {}
81
82 /* Note where the definition of a class lives to that template
83 instantiations can use it. */
84
85 void
86 repo_class_defined (tree t)
87 {}
88 #endif
89
90 static tree
91 repo_get_id (tree t)
92 {
93 if (TYPE_P (t))
94 {
95 tree vtable;
96
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 ();
102
103 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t));
104
105 t = vtable;
106 if (t == NULL_TREE)
107 return t;
108 }
109 return DECL_ASSEMBLER_NAME (t);
110 }
111
112 /* Note that a template has been used. If we can see the definition, offer
113 to emit it. */
114
115 void
116 repo_template_used (tree t)
117 {
118 tree id;
119
120 if (! flag_use_repository)
121 return;
122
123 id = repo_get_id (t);
124 if (id == NULL_TREE)
125 return;
126
127 if (TYPE_P (t))
128 {
129 if (IDENTIFIER_REPO_CHOSEN (id))
130 mark_class_instantiated (t, 0);
131 }
132 else if (DECL_P (t))
133 {
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,
141 0);
142 }
143 else
144 abort ();
145
146 if (! IDENTIFIER_REPO_USED (id))
147 {
148 IDENTIFIER_REPO_USED (id) = 1;
149 pending_repo = tree_cons (NULL_TREE, id, pending_repo);
150 }
151 }
152
153 #if 0
154 /* Note that the vtable for a class has been used, and offer to emit it. */
155
156 static void
157 repo_vtable_used (tree t)
158 {
159 if (! flag_use_repository)
160 return;
161
162 pending_repo = tree_cons (NULL_TREE, t, pending_repo);
163 }
164
165 /* Note that an inline with external linkage has been used, and offer to
166 emit it. */
167
168 void
169 repo_inline_used (tree fn)
170 {
171 if (! flag_use_repository)
172 return;
173
174 /* Member functions of polymorphic classes go with their vtables. */
175 if (DECL_FUNCTION_MEMBER_P (fn)
176 && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn)))
177 {
178 repo_vtable_used (DECL_CONTEXT (fn));
179 return;
180 }
181
182 pending_repo = tree_cons (NULL_TREE, fn, pending_repo);
183 }
184
185 /* Note that a particular typeinfo node has been used, and offer to
186 emit it. */
187
188 void
189 repo_tinfo_used (tree ti)
190 {
191 }
192 #endif
193
194 void
195 repo_template_instantiated (tree t, bool extern_p)
196 {
197 if (! extern_p)
198 {
199 tree id = repo_get_id (t);
200 if (id)
201 IDENTIFIER_REPO_CHOSEN (id) = 1;
202 }
203 }
204
205 /* Parse a reasonable subset of shell quoting syntax. */
206
207 static char *
208 extract_string (char **pp)
209 {
210 char *p = *pp;
211 int backquote = 0;
212 int inside = 0;
213
214 for (;;)
215 {
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);
230 }
231
232 obstack_1grow (&temporary_obstack, '\0');
233 *pp = p;
234 return obstack_finish (&temporary_obstack);
235 }
236
237 const char *
238 get_base_filename (const char *filename)
239 {
240 char *p = getenv ("COLLECT_GCC_OPTIONS");
241 char *output = NULL;
242 int compiling = 0;
243
244 while (p && *p)
245 {
246 char *q = extract_string (&p);
247
248 if (strcmp (q, "-o") == 0)
249 output = extract_string (&p);
250 else if (strcmp (q, "-c") == 0)
251 compiling = 1;
252 }
253
254 if (compiling && output)
255 return output;
256
257 if (p && ! compiling)
258 {
259 warning ("-frepo must be used with -c");
260 flag_use_repository = 0;
261 return NULL;
262 }
263
264 return lbasename (filename);
265 }
266
267 static void
268 open_repo_file (const char *filename)
269 {
270 register const char *p;
271 const char *s = get_base_filename (filename);
272
273 if (s == NULL)
274 return;
275
276 p = lbasename (s);
277 p = strrchr (p, '.');
278 if (! p)
279 p = s + strlen (s);
280
281 repo_name = xmalloc (p - s + 5);
282 memcpy (repo_name, s, p - s);
283 memcpy (repo_name + (p - s), ".rpo", 5);
284
285 repo_file = fopen (repo_name, "r");
286 }
287
288 static char *
289 afgets (FILE *stream)
290 {
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);
298 }
299
300 void
301 init_repo (const char *filename)
302 {
303 char *buf;
304
305 if (! flag_use_repository)
306 return;
307
308 gcc_obstack_init (&temporary_obstack);
309
310 open_repo_file (filename);
311
312 if (repo_file == 0)
313 return;
314
315 while ((buf = afgets (repo_file)))
316 {
317 switch (buf[0])
318 {
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':
330 {
331 tree id = get_identifier (buf + 2);
332 tree orig;
333
334 if (buf[0] == 'C')
335 {
336 IDENTIFIER_REPO_CHOSEN (id) = 1;
337 orig = integer_one_node;
338 }
339 else
340 orig = NULL_TREE;
341
342 original_repo = tree_cons (orig, id, original_repo);
343 }
344 break;
345 default:
346 error ("mysterious repository information in %s", repo_name);
347 }
348 obstack_free (&temporary_obstack, buf);
349 }
350 }
351
352 static void
353 reopen_repo_file_for_write (void)
354 {
355 if (repo_file)
356 fclose (repo_file);
357 repo_file = fopen (repo_name, "w");
358
359 if (repo_file == 0)
360 {
361 error ("can't create repository information file `%s'", repo_name);
362 flag_use_repository = 0;
363 }
364 }
365
366 /* Emit any pending repos. */
367
368 void
369 finish_repo (void)
370 {
371 tree t;
372 bool repo_changed = false;
373 char *dir, *args;
374
375 if (!flag_use_repository)
376 return;
377
378 /* Do we have to write out a new info file? */
379
380 /* Are there any old templates that aren't used any longer or that are
381 newly chosen? */
382
383 for (t = original_repo; t; t = TREE_CHAIN (t))
384 {
385 if (!IDENTIFIER_REPO_USED (TREE_VALUE (t))
386 || (!TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
387 {
388 repo_changed = true;
389 break;
390 }
391 IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
392 }
393
394 /* Are there any templates that are newly used? */
395
396 if (!repo_changed)
397 for (t = pending_repo; t; t = TREE_CHAIN (t))
398 {
399 if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
400 {
401 repo_changed = true;
402 break;
403 }
404 }
405
406 dir = getpwd ();
407 args = getenv ("COLLECT_GCC_OPTIONS");
408
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;
415
416 if (!repo_changed || errorcount || sorrycount)
417 goto out;
418
419 reopen_repo_file_for_write ();
420
421 if (repo_file == 0)
422 goto out;
423
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);
428
429 for (t = pending_repo; t; t = TREE_CHAIN (t))
430 {
431 tree val = TREE_VALUE (t);
432 char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
433
434 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val));
435 }
436
437 out:
438 if (repo_file)
439 fclose (repo_file);
440 }
441
442 #include "gt-cp-repo.h"