]>
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" |
805e22b2 | 30 | #include "coretypes.h" |
31 | #include "tm.h" | |
bb09dca5 | 32 | #include "tree.h" |
33 | #include "cp-tree.h" | |
34 | #include "input.h" | |
3e1ffd94 | 35 | #include "obstack.h" |
2a4e40b0 | 36 | #include "toplev.h" |
1e4853c2 | 37 | #include "ggc.h" |
a587b03b | 38 | #include "diagnostic.h" |
bb09dca5 | 39 | |
26ac6687 | 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); | |
bb09dca5 | 46 | |
1f3233d1 | 47 | static GTY(()) tree pending_repo; |
48 | static GTY(()) tree original_repo; | |
3f7d79e4 | 49 | static char *repo_name; |
bb09dca5 | 50 | static FILE *repo_file; |
51 | ||
17dd1766 | 52 | static const char *old_args, *old_dir, *old_main; |
6686e84d | 53 | |
d7c47c0e | 54 | static struct obstack temporary_obstack; |
bb09dca5 | 55 | |
bb09dca5 | 56 | #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE)) |
57 | #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE)) | |
58 | ||
e857e9c7 | 59 | #if 0 |
bb09dca5 | 60 | /* Record the flags used to compile this translation unit. */ |
61 | ||
62 | void | |
26ac6687 | 63 | repo_compile_flags (int argc, char **argv) |
bb09dca5 | 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 | |
26ac6687 | 72 | repo_template_declared (tree t) |
bb09dca5 | 73 | {} |
74 | ||
75 | /* Note where the definition of a template lives so that instantiations can | |
76 | be generated later. */ | |
77 | ||
78 | void | |
26ac6687 | 79 | repo_template_defined (tree t) |
bb09dca5 | 80 | {} |
81 | ||
82 | /* Note where the definition of a class lives to that template | |
83 | instantiations can use it. */ | |
84 | ||
85 | void | |
26ac6687 | 86 | repo_class_defined (tree t) |
bb09dca5 | 87 | {} |
e857e9c7 | 88 | #endif |
bb09dca5 | 89 | |
e857e9c7 | 90 | static tree |
26ac6687 | 91 | repo_get_id (tree t) |
dc76caae | 92 | { |
9308e976 | 93 | if (TYPE_P (t)) |
dc76caae | 94 | { |
8b1e0315 | 95 | tree vtable; |
96 | ||
d1c45f5d | 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. */ | |
4b72716d | 100 | if (!COMPLETE_TYPE_P (t) || TYPE_BEING_DEFINED (t)) |
523ac844 | 101 | abort (); |
d1c45f5d | 102 | |
8b1e0315 | 103 | vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (t)); |
104 | ||
8b1e0315 | 105 | t = vtable; |
dc76caae | 106 | if (t == NULL_TREE) |
107 | return t; | |
108 | } | |
109 | return DECL_ASSEMBLER_NAME (t); | |
110 | } | |
111 | ||
bb09dca5 | 112 | /* Note that a template has been used. If we can see the definition, offer |
96624a9e | 113 | to emit it. */ |
bb09dca5 | 114 | |
115 | void | |
26ac6687 | 116 | repo_template_used (tree t) |
bb09dca5 | 117 | { |
118 | tree id; | |
119 | ||
120 | if (! flag_use_repository) | |
121 | return; | |
122 | ||
dc76caae | 123 | id = repo_get_id (t); |
124 | if (id == NULL_TREE) | |
125 | return; | |
126 | ||
9308e976 | 127 | if (TYPE_P (t)) |
bb09dca5 | 128 | { |
bb09dca5 | 129 | if (IDENTIFIER_REPO_CHOSEN (id)) |
130 | mark_class_instantiated (t, 0); | |
131 | } | |
9308e976 | 132 | else if (DECL_P (t)) |
bb09dca5 | 133 | { |
bb09dca5 | 134 | if (IDENTIFIER_REPO_CHOSEN (id)) |
3ef20b61 | 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); | |
bb09dca5 | 142 | } |
143 | else | |
523ac844 | 144 | abort (); |
bb09dca5 | 145 | |
146 | if (! IDENTIFIER_REPO_USED (id)) | |
147 | { | |
bb09dca5 | 148 | IDENTIFIER_REPO_USED (id) = 1; |
b0652a4f | 149 | pending_repo = tree_cons (NULL_TREE, id, pending_repo); |
bb09dca5 | 150 | } |
bb09dca5 | 151 | } |
152 | ||
e857e9c7 | 153 | #if 0 |
bb09dca5 | 154 | /* Note that the vtable for a class has been used, and offer to emit it. */ |
155 | ||
e857e9c7 | 156 | static void |
26ac6687 | 157 | repo_vtable_used (tree t) |
bb09dca5 | 158 | { |
159 | if (! flag_use_repository) | |
160 | return; | |
161 | ||
b0652a4f | 162 | pending_repo = tree_cons (NULL_TREE, t, pending_repo); |
bb09dca5 | 163 | } |
164 | ||
165 | /* Note that an inline with external linkage has been used, and offer to | |
166 | emit it. */ | |
167 | ||
168 | void | |
26ac6687 | 169 | repo_inline_used (tree fn) |
bb09dca5 | 170 | { |
171 | if (! flag_use_repository) | |
172 | return; | |
173 | ||
174 | /* Member functions of polymorphic classes go with their vtables. */ | |
1d6228f0 | 175 | if (DECL_FUNCTION_MEMBER_P (fn) |
9ba4048d | 176 | && TYPE_POLYMORPHIC_P (DECL_CONTEXT (fn))) |
bb09dca5 | 177 | { |
9ba4048d | 178 | repo_vtable_used (DECL_CONTEXT (fn)); |
bb09dca5 | 179 | return; |
180 | } | |
181 | ||
b0652a4f | 182 | pending_repo = tree_cons (NULL_TREE, fn, pending_repo); |
bb09dca5 | 183 | } |
184 | ||
185 | /* Note that a particular typeinfo node has been used, and offer to | |
186 | emit it. */ | |
187 | ||
188 | void | |
26ac6687 | 189 | repo_tinfo_used (tree ti) |
bb09dca5 | 190 | { |
191 | } | |
e857e9c7 | 192 | #endif |
bb09dca5 | 193 | |
dc76caae | 194 | void |
26ac6687 | 195 | repo_template_instantiated (tree t, bool extern_p) |
dc76caae | 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 | ||
6686e84d | 205 | /* Parse a reasonable subset of shell quoting syntax. */ |
206 | ||
207 | static char * | |
26ac6687 | 208 | extract_string (char **pp) |
6686e84d | 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 | ||
59b774ec | 237 | const char * |
26ac6687 | 238 | get_base_filename (const char *filename) |
11682839 | 239 | { |
240 | char *p = getenv ("COLLECT_GCC_OPTIONS"); | |
6686e84d | 241 | char *output = NULL; |
11682839 | 242 | int compiling = 0; |
243 | ||
6686e84d | 244 | while (p && *p) |
245 | { | |
246 | char *q = extract_string (&p); | |
11682839 | 247 | |
6686e84d | 248 | if (strcmp (q, "-o") == 0) |
249 | output = extract_string (&p); | |
250 | else if (strcmp (q, "-c") == 0) | |
251 | compiling = 1; | |
11682839 | 252 | } |
253 | ||
254 | if (compiling && output) | |
255 | return output; | |
256 | ||
3e1ffd94 | 257 | if (p && ! compiling) |
258 | { | |
905d4035 | 259 | warning ("-frepo must be used with -c"); |
3e1ffd94 | 260 | flag_use_repository = 0; |
261 | return NULL; | |
262 | } | |
263 | ||
40275504 | 264 | return lbasename (filename); |
11682839 | 265 | } |
266 | ||
bb09dca5 | 267 | static void |
26ac6687 | 268 | open_repo_file (const char *filename) |
bb09dca5 | 269 | { |
e1721763 | 270 | register const char *p; |
271 | const char *s = get_base_filename (filename); | |
3e1ffd94 | 272 | |
3f7d79e4 | 273 | if (s == NULL) |
3e1ffd94 | 274 | return; |
275 | ||
40275504 | 276 | p = lbasename (s); |
78dbff7c | 277 | p = strrchr (p, '.'); |
3f7d79e4 | 278 | if (! p) |
279 | p = s + strlen (s); | |
bb09dca5 | 280 | |
92192583 | 281 | repo_name = xmalloc (p - s + 5); |
282 | memcpy (repo_name, s, p - s); | |
283 | memcpy (repo_name + (p - s), ".rpo", 5); | |
bb09dca5 | 284 | |
285 | repo_file = fopen (repo_name, "r"); | |
286 | } | |
287 | ||
3f7d79e4 | 288 | static char * |
26ac6687 | 289 | afgets (FILE *stream) |
3f7d79e4 | 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 | ||
bb09dca5 | 300 | void |
26ac6687 | 301 | init_repo (const char *filename) |
bb09dca5 | 302 | { |
3f7d79e4 | 303 | char *buf; |
bb09dca5 | 304 | |
305 | if (! flag_use_repository) | |
306 | return; | |
307 | ||
d7c47c0e | 308 | gcc_obstack_init (&temporary_obstack); |
1e4853c2 | 309 | |
3e1ffd94 | 310 | open_repo_file (filename); |
bb09dca5 | 311 | |
312 | if (repo_file == 0) | |
313 | return; | |
314 | ||
034b484a | 315 | while ((buf = afgets (repo_file))) |
bb09dca5 | 316 | { |
bb09dca5 | 317 | switch (buf[0]) |
318 | { | |
319 | case 'A': | |
92192583 | 320 | old_args = ggc_strdup (buf + 2); |
6686e84d | 321 | break; |
3f7d79e4 | 322 | case 'D': |
92192583 | 323 | old_dir = ggc_strdup (buf + 2); |
6686e84d | 324 | break; |
bb09dca5 | 325 | case 'M': |
92192583 | 326 | old_main = ggc_strdup (buf + 2); |
bb09dca5 | 327 | break; |
328 | case 'C': | |
329 | case 'O': | |
330 | { | |
3f7d79e4 | 331 | tree id = get_identifier (buf + 2); |
332 | tree orig; | |
3e1ffd94 | 333 | |
bb09dca5 | 334 | if (buf[0] == 'C') |
dc76caae | 335 | { |
336 | IDENTIFIER_REPO_CHOSEN (id) = 1; | |
337 | orig = integer_one_node; | |
338 | } | |
339 | else | |
340 | orig = NULL_TREE; | |
341 | ||
b0652a4f | 342 | original_repo = tree_cons (orig, id, original_repo); |
bb09dca5 | 343 | } |
344 | break; | |
345 | default: | |
905d4035 | 346 | error ("mysterious repository information in %s", repo_name); |
bb09dca5 | 347 | } |
3f7d79e4 | 348 | obstack_free (&temporary_obstack, buf); |
bb09dca5 | 349 | } |
350 | } | |
351 | ||
352 | static void | |
26ac6687 | 353 | reopen_repo_file_for_write (void) |
bb09dca5 | 354 | { |
355 | if (repo_file) | |
356 | fclose (repo_file); | |
357 | repo_file = fopen (repo_name, "w"); | |
358 | ||
359 | if (repo_file == 0) | |
360 | { | |
905d4035 | 361 | error ("can't create repository information file `%s'", repo_name); |
bb09dca5 | 362 | flag_use_repository = 0; |
363 | } | |
364 | } | |
365 | ||
366 | /* Emit any pending repos. */ | |
367 | ||
368 | void | |
26ac6687 | 369 | finish_repo (void) |
bb09dca5 | 370 | { |
371 | tree t; | |
26ac6687 | 372 | bool repo_changed = false; |
6686e84d | 373 | char *dir, *args; |
bb09dca5 | 374 | |
26ac6687 | 375 | if (!flag_use_repository) |
bb09dca5 | 376 | return; |
377 | ||
378 | /* Do we have to write out a new info file? */ | |
379 | ||
dc76caae | 380 | /* Are there any old templates that aren't used any longer or that are |
381 | newly chosen? */ | |
3e1ffd94 | 382 | |
383 | for (t = original_repo; t; t = TREE_CHAIN (t)) | |
384 | { | |
26ac6687 | 385 | if (!IDENTIFIER_REPO_USED (TREE_VALUE (t)) |
386 | || (!TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t)))) | |
3e1ffd94 | 387 | { |
26ac6687 | 388 | repo_changed = true; |
3e1ffd94 | 389 | break; |
390 | } | |
391 | IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0; | |
392 | } | |
393 | ||
394 | /* Are there any templates that are newly used? */ | |
395 | ||
26ac6687 | 396 | if (!repo_changed) |
3e1ffd94 | 397 | for (t = pending_repo; t; t = TREE_CHAIN (t)) |
398 | { | |
399 | if (IDENTIFIER_REPO_USED (TREE_VALUE (t))) | |
400 | { | |
26ac6687 | 401 | repo_changed = true; |
3e1ffd94 | 402 | break; |
403 | } | |
404 | } | |
405 | ||
6686e84d | 406 | dir = getpwd (); |
407 | args = getenv ("COLLECT_GCC_OPTIONS"); | |
408 | ||
26ac6687 | 409 | if (!repo_changed && pending_repo) |
6686e84d | 410 | if (strcmp (old_main, main_input_filename) != 0 |
411 | || strcmp (old_dir, dir) != 0 | |
412 | || (args == NULL) != (old_args == NULL) | |
c90e0199 | 413 | || (args && strcmp (old_args, args) != 0)) |
26ac6687 | 414 | repo_changed = true; |
6686e84d | 415 | |
26ac6687 | 416 | if (!repo_changed || errorcount || sorrycount) |
bb09dca5 | 417 | goto out; |
418 | ||
419 | reopen_repo_file_for_write (); | |
420 | ||
421 | if (repo_file == 0) | |
422 | goto out; | |
423 | ||
11682839 | 424 | fprintf (repo_file, "M %s\n", main_input_filename); |
6686e84d | 425 | fprintf (repo_file, "D %s\n", dir); |
426 | if (args) | |
427 | fprintf (repo_file, "A %s\n", args); | |
11682839 | 428 | |
bb09dca5 | 429 | for (t = pending_repo; t; t = TREE_CHAIN (t)) |
430 | { | |
431 | tree val = TREE_VALUE (t); | |
3e1ffd94 | 432 | char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O'; |
bb09dca5 | 433 | |
3f7d79e4 | 434 | fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (val)); |
bb09dca5 | 435 | } |
436 | ||
437 | out: | |
438 | if (repo_file) | |
439 | fclose (repo_file); | |
440 | } | |
1f3233d1 | 441 | |
442 | #include "gt-cp-repo.h" |