]>
Commit | Line | Data |
---|---|---|
bb09dca5 | 1 | /* Code to maintain a C++ template repository. |
606b494c | 2 | Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc. |
bb09dca5 | 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 | |
c58d4270 | 19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
bb09dca5 | 21 | |
22 | /* My strategy here is as follows: | |
23 | ||
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 | |
11682839 | 26 | explicit code. */ |
bb09dca5 | 27 | |
11682839 | 28 | #include "config.h" |
b3ef7553 | 29 | #include "system.h" |
bb09dca5 | 30 | #include "tree.h" |
31 | #include "cp-tree.h" | |
32 | #include "input.h" | |
3e1ffd94 | 33 | #include "obstack.h" |
2a4e40b0 | 34 | #include "toplev.h" |
1e4853c2 | 35 | #include "ggc.h" |
a587b03b | 36 | #include "diagnostic.h" |
bb09dca5 | 37 | |
24054144 | 38 | static tree repo_get_id PARAMS ((tree)); |
39 | static char *extract_string PARAMS ((char **)); | |
59b774ec | 40 | static const char *get_base_filename PARAMS ((const char *)); |
24054144 | 41 | static void open_repo_file PARAMS ((const char *)); |
42 | static char *afgets PARAMS ((FILE *)); | |
43 | static void reopen_repo_file_for_write PARAMS ((void)); | |
bb09dca5 | 44 | |
1f3233d1 | 45 | static GTY(()) tree pending_repo; |
46 | static GTY(()) tree original_repo; | |
3f7d79e4 | 47 | static char *repo_name; |
bb09dca5 | 48 | static FILE *repo_file; |
49 | ||
6686e84d | 50 | static char *old_args, *old_dir, *old_main; |
51 | ||
d7c47c0e | 52 | static struct obstack temporary_obstack; |
3f7d79e4 | 53 | extern struct obstack permanent_obstack; |
bb09dca5 | 54 | |
bb09dca5 | 55 | #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE)) |
56 | #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE)) | |
57 | ||
e857e9c7 | 58 | #if 0 |
bb09dca5 | 59 | /* Record the flags used to compile this translation unit. */ |
60 | ||
61 | void | |
62 | repo_compile_flags (argc, argv) | |
63 | int argc; | |
64 | char **argv; | |
65 | { | |
66 | } | |
67 | ||
68 | /* If this template has not been seen before, add a note to the repository | |
69 | saying where the declaration was. This may be used to find the | |
70 | definition at link time. */ | |
71 | ||
72 | void | |
73 | repo_template_declared (t) | |
74 | tree t; | |
75 | {} | |
76 | ||
77 | /* Note where the definition of a template lives so that instantiations can | |
78 | be generated later. */ | |
79 | ||
80 | void | |
81 | repo_template_defined (t) | |
82 | tree t; | |
83 | {} | |
84 | ||
85 | /* Note where the definition of a class lives to that template | |
86 | instantiations can use it. */ | |
87 | ||
88 | void | |
89 | repo_class_defined (t) | |
90 | tree t; | |
91 | {} | |
e857e9c7 | 92 | #endif |
bb09dca5 | 93 | |
e857e9c7 | 94 | static tree |
dc76caae | 95 | repo_get_id (t) |
96 | tree t; | |
97 | { | |
9308e976 | 98 | if (TYPE_P (t)) |
dc76caae | 99 | { |
8b1e0315 | 100 | tree vtable; |
101 | ||
d1c45f5d | 102 | /* If we're not done setting up the class, we may not have set up |
103 | the vtable, so going ahead would give the wrong answer. | |
104 | See g++.pt/instantiate4.C. */ | |
4b72716d | 105 | if (!COMPLETE_TYPE_P (t) || TYPE_BEING_DEFINED (t)) |
523ac844 | 106 | abort (); |
d1c45f5d | 107 | |
8b1e0315 | 108 | vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t)); |
109 | ||
8b1e0315 | 110 | t = vtable; |
dc76caae | 111 | if (t == NULL_TREE) |
112 | return t; | |
113 | } | |
114 | return DECL_ASSEMBLER_NAME (t); | |
115 | } | |
116 | ||
bb09dca5 | 117 | /* Note that a template has been used. If we can see the definition, offer |
96624a9e | 118 | to emit it. */ |
bb09dca5 | 119 | |
120 | void | |
121 | repo_template_used (t) | |
122 | tree t; | |
123 | { | |
124 | tree id; | |
125 | ||
126 | if (! flag_use_repository) | |
127 | return; | |
128 | ||
dc76caae | 129 | id = repo_get_id (t); |
130 | if (id == NULL_TREE) | |
131 | return; | |
132 | ||
9308e976 | 133 | if (TYPE_P (t)) |
bb09dca5 | 134 | { |
bb09dca5 | 135 | if (IDENTIFIER_REPO_CHOSEN (id)) |
136 | mark_class_instantiated (t, 0); | |
137 | } | |
9308e976 | 138 | else if (DECL_P (t)) |
bb09dca5 | 139 | { |
bb09dca5 | 140 | if (IDENTIFIER_REPO_CHOSEN (id)) |
3ef20b61 | 141 | /* It doesn't make sense to instantiate a clone, so we |
142 | instantiate the cloned function instead. Note that this | |
143 | approach will not work correctly if collect2 assigns | |
144 | different clones to different files -- but it shouldn't. */ | |
145 | mark_decl_instantiated (DECL_CLONED_FUNCTION_P (t) | |
146 | ? DECL_CLONED_FUNCTION (t) : t, | |
147 | 0); | |
bb09dca5 | 148 | } |
149 | else | |
523ac844 | 150 | abort (); |
bb09dca5 | 151 | |
152 | if (! IDENTIFIER_REPO_USED (id)) | |
153 | { | |
bb09dca5 | 154 | IDENTIFIER_REPO_USED (id) = 1; |
b0652a4f | 155 | pending_repo = tree_cons (NULL_TREE, id, pending_repo); |
bb09dca5 | 156 | } |
bb09dca5 | 157 | } |
158 | ||
e857e9c7 | 159 | #if 0 |
bb09dca5 | 160 | /* Note that the vtable for a class has been used, and offer to emit it. */ |
161 | ||
e857e9c7 | 162 | static void |
bb09dca5 | 163 | repo_vtable_used (t) |
164 | tree t; | |
165 | { | |
166 | if (! flag_use_repository) | |
167 | return; | |
168 | ||
b0652a4f | 169 | pending_repo = tree_cons (NULL_TREE, t, pending_repo); |
bb09dca5 | 170 | } |
171 | ||
172 | /* Note that an inline with external linkage has been used, and offer to | |
173 | emit it. */ | |
174 | ||
175 | void | |
176 | repo_inline_used (fn) | |
177 | tree fn; | |
178 | { | |
179 | if (! flag_use_repository) | |
180 | return; | |
181 | ||
182 | /* Member functions of polymorphic classes go with their vtables. */ | |
1d6228f0 | 183 | if (DECL_FUNCTION_MEMBER_P (fn) |
9ba4048d | 184 | && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn))) |
bb09dca5 | 185 | { |
9ba4048d | 186 | repo_vtable_used (DECL_CONTEXT (fn)); |
bb09dca5 | 187 | return; |
188 | } | |
189 | ||
b0652a4f | 190 | pending_repo = tree_cons (NULL_TREE, fn, pending_repo); |
bb09dca5 | 191 | } |
192 | ||
193 | /* Note that a particular typeinfo node has been used, and offer to | |
194 | emit it. */ | |
195 | ||
196 | void | |
197 | repo_tinfo_used (ti) | |
198 | tree ti; | |
199 | { | |
200 | } | |
e857e9c7 | 201 | #endif |
bb09dca5 | 202 | |
dc76caae | 203 | void |
204 | repo_template_instantiated (t, extern_p) | |
205 | tree t; | |
206 | int extern_p; | |
207 | { | |
208 | if (! extern_p) | |
209 | { | |
210 | tree id = repo_get_id (t); | |
211 | if (id) | |
212 | IDENTIFIER_REPO_CHOSEN (id) = 1; | |
213 | } | |
214 | } | |
215 | ||
6686e84d | 216 | /* Parse a reasonable subset of shell quoting syntax. */ |
217 | ||
218 | static char * | |
219 | extract_string (pp) | |
220 | char **pp; | |
221 | { | |
222 | char *p = *pp; | |
223 | int backquote = 0; | |
224 | int inside = 0; | |
225 | ||
226 | for (;;) | |
227 | { | |
228 | char c = *p; | |
229 | if (c == '\0') | |
230 | break; | |
231 | ++p; | |
232 | if (backquote) | |
233 | obstack_1grow (&temporary_obstack, c); | |
234 | else if (! inside && c == ' ') | |
235 | break; | |
236 | else if (! inside && c == '\\') | |
237 | backquote = 1; | |
238 | else if (c == '\'') | |
239 | inside = !inside; | |
240 | else | |
241 | obstack_1grow (&temporary_obstack, c); | |
242 | } | |
243 | ||
244 | obstack_1grow (&temporary_obstack, '\0'); | |
245 | *pp = p; | |
246 | return obstack_finish (&temporary_obstack); | |
247 | } | |
248 | ||
59b774ec | 249 | const char * |
11682839 | 250 | get_base_filename (filename) |
e1721763 | 251 | const char *filename; |
11682839 | 252 | { |
253 | char *p = getenv ("COLLECT_GCC_OPTIONS"); | |
6686e84d | 254 | char *output = NULL; |
11682839 | 255 | int compiling = 0; |
256 | ||
6686e84d | 257 | while (p && *p) |
258 | { | |
259 | char *q = extract_string (&p); | |
11682839 | 260 | |
6686e84d | 261 | if (strcmp (q, "-o") == 0) |
262 | output = extract_string (&p); | |
263 | else if (strcmp (q, "-c") == 0) | |
264 | compiling = 1; | |
11682839 | 265 | } |
266 | ||
267 | if (compiling && output) | |
268 | return output; | |
269 | ||
3e1ffd94 | 270 | if (p && ! compiling) |
271 | { | |
905d4035 | 272 | warning ("-frepo must be used with -c"); |
3e1ffd94 | 273 | flag_use_repository = 0; |
274 | return NULL; | |
275 | } | |
276 | ||
40275504 | 277 | return lbasename (filename); |
11682839 | 278 | } |
279 | ||
bb09dca5 | 280 | static void |
281 | open_repo_file (filename) | |
e1721763 | 282 | const char *filename; |
bb09dca5 | 283 | { |
e1721763 | 284 | register const char *p; |
285 | const char *s = get_base_filename (filename); | |
3e1ffd94 | 286 | |
3f7d79e4 | 287 | if (s == NULL) |
3e1ffd94 | 288 | return; |
289 | ||
40275504 | 290 | p = lbasename (s); |
78dbff7c | 291 | p = strrchr (p, '.'); |
3f7d79e4 | 292 | if (! p) |
293 | p = s + strlen (s); | |
bb09dca5 | 294 | |
3f7d79e4 | 295 | obstack_grow (&permanent_obstack, s, p - s); |
296 | repo_name = obstack_copy0 (&permanent_obstack, ".rpo", 4); | |
bb09dca5 | 297 | |
298 | repo_file = fopen (repo_name, "r"); | |
299 | } | |
300 | ||
3f7d79e4 | 301 | static char * |
302 | afgets (stream) | |
303 | FILE *stream; | |
304 | { | |
305 | int c; | |
306 | while ((c = getc (stream)) != EOF && c != '\n') | |
307 | obstack_1grow (&temporary_obstack, c); | |
308 | if (obstack_object_size (&temporary_obstack) == 0) | |
309 | return NULL; | |
310 | obstack_1grow (&temporary_obstack, '\0'); | |
311 | return obstack_finish (&temporary_obstack); | |
312 | } | |
313 | ||
bb09dca5 | 314 | void |
315 | init_repo (filename) | |
e1721763 | 316 | const char *filename; |
bb09dca5 | 317 | { |
3f7d79e4 | 318 | char *buf; |
bb09dca5 | 319 | |
320 | if (! flag_use_repository) | |
321 | return; | |
322 | ||
d7c47c0e | 323 | gcc_obstack_init (&temporary_obstack); |
1e4853c2 | 324 | |
3e1ffd94 | 325 | open_repo_file (filename); |
bb09dca5 | 326 | |
327 | if (repo_file == 0) | |
328 | return; | |
329 | ||
034b484a | 330 | while ((buf = afgets (repo_file))) |
bb09dca5 | 331 | { |
bb09dca5 | 332 | switch (buf[0]) |
333 | { | |
334 | case 'A': | |
6686e84d | 335 | old_args = obstack_copy0 (&permanent_obstack, buf + 2, |
336 | strlen (buf + 2)); | |
337 | break; | |
3f7d79e4 | 338 | case 'D': |
6686e84d | 339 | old_dir = obstack_copy0 (&permanent_obstack, buf + 2, |
340 | strlen (buf + 2)); | |
341 | break; | |
bb09dca5 | 342 | case 'M': |
6686e84d | 343 | old_main = obstack_copy0 (&permanent_obstack, buf + 2, |
344 | strlen (buf + 2)); | |
bb09dca5 | 345 | break; |
346 | case 'C': | |
347 | case 'O': | |
348 | { | |
3f7d79e4 | 349 | tree id = get_identifier (buf + 2); |
350 | tree orig; | |
3e1ffd94 | 351 | |
bb09dca5 | 352 | if (buf[0] == 'C') |
dc76caae | 353 | { |
354 | IDENTIFIER_REPO_CHOSEN (id) = 1; | |
355 | orig = integer_one_node; | |
356 | } | |
357 | else | |
358 | orig = NULL_TREE; | |
359 | ||
b0652a4f | 360 | original_repo = tree_cons (orig, id, original_repo); |
bb09dca5 | 361 | } |
362 | break; | |
363 | default: | |
905d4035 | 364 | error ("mysterious repository information in %s", repo_name); |
bb09dca5 | 365 | } |
3f7d79e4 | 366 | obstack_free (&temporary_obstack, buf); |
bb09dca5 | 367 | } |
368 | } | |
369 | ||
370 | static void | |
371 | reopen_repo_file_for_write () | |
372 | { | |
373 | if (repo_file) | |
374 | fclose (repo_file); | |
375 | repo_file = fopen (repo_name, "w"); | |
376 | ||
377 | if (repo_file == 0) | |
378 | { | |
905d4035 | 379 | error ("can't create repository information file `%s'", repo_name); |
bb09dca5 | 380 | flag_use_repository = 0; |
381 | } | |
382 | } | |
383 | ||
384 | /* Emit any pending repos. */ | |
385 | ||
386 | void | |
387 | finish_repo () | |
388 | { | |
389 | tree t; | |
3e1ffd94 | 390 | int repo_changed = 0; |
6686e84d | 391 | char *dir, *args; |
bb09dca5 | 392 | |
393 | if (! flag_use_repository) | |
394 | return; | |
395 | ||
396 | /* Do we have to write out a new info file? */ | |
397 | ||
dc76caae | 398 | /* Are there any old templates that aren't used any longer or that are |
399 | newly chosen? */ | |
3e1ffd94 | 400 | |
401 | for (t = original_repo; t; t = TREE_CHAIN (t)) | |
402 | { | |
dc76caae | 403 | if (! IDENTIFIER_REPO_USED (TREE_VALUE (t)) |
404 | || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t)))) | |
3e1ffd94 | 405 | { |
406 | repo_changed = 1; | |
407 | break; | |
408 | } | |
409 | IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0; | |
410 | } | |
411 | ||
412 | /* Are there any templates that are newly used? */ | |
413 | ||
414 | if (! repo_changed) | |
415 | for (t = pending_repo; t; t = TREE_CHAIN (t)) | |
416 | { | |
417 | if (IDENTIFIER_REPO_USED (TREE_VALUE (t))) | |
418 | { | |
419 | repo_changed = 1; | |
420 | break; | |
421 | } | |
422 | } | |
423 | ||
6686e84d | 424 | dir = getpwd (); |
425 | args = getenv ("COLLECT_GCC_OPTIONS"); | |
426 | ||
427 | if (! repo_changed && pending_repo) | |
428 | if (strcmp (old_main, main_input_filename) != 0 | |
429 | || strcmp (old_dir, dir) != 0 | |
430 | || (args == NULL) != (old_args == NULL) | |
c90e0199 | 431 | || (args && strcmp (old_args, args) != 0)) |
6686e84d | 432 | repo_changed = 1; |
433 | ||
bb09dca5 | 434 | if (! repo_changed || errorcount || sorrycount) |
435 | goto out; | |
436 | ||
437 | reopen_repo_file_for_write (); | |
438 | ||
439 | if (repo_file == 0) | |
440 | goto out; | |
441 | ||
11682839 | 442 | fprintf (repo_file, "M %s\n", main_input_filename); |
6686e84d | 443 | fprintf (repo_file, "D %s\n", dir); |
444 | if (args) | |
445 | fprintf (repo_file, "A %s\n", args); | |
11682839 | 446 | |
bb09dca5 | 447 | for (t = pending_repo; t; t = TREE_CHAIN (t)) |
448 | { | |
449 | tree val = TREE_VALUE (t); | |
3e1ffd94 | 450 | char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O'; |
bb09dca5 | 451 | |
3f7d79e4 | 452 | fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val)); |
bb09dca5 | 453 | } |
454 | ||
455 | out: | |
456 | if (repo_file) | |
457 | fclose (repo_file); | |
458 | } | |
1f3233d1 | 459 | |
460 | #include "gt-cp-repo.h" |