1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995, 1997 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* My strategy here is as follows:
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
41 extern char * getenv ();
44 #ifdef NEED_DECLARATION_RINDEX
45 extern char *rindex ();
47 extern char *getpwd
PROTO((void));
49 static tree repo_get_id
PROTO((tree
));
50 static char *save_string
PROTO((char *, int));
51 static char *extract_string
PROTO((char **));
52 static char *get_base_filename
PROTO((char *));
53 static void open_repo_file
PROTO((char *));
54 static char *afgets
PROTO((FILE *));
55 static void reopen_repo_file_for_write
PROTO((void));
57 static tree pending_repo
;
58 static tree original_repo
;
59 static char *repo_name
;
60 static FILE *repo_file
;
62 static char *old_args
, *old_dir
, *old_main
;
64 extern int flag_use_repository
;
65 extern int errorcount
, sorrycount
;
66 extern struct obstack temporary_obstack
;
67 extern struct obstack permanent_obstack
;
69 #define IDENTIFIER_REPO_USED(NODE) (TREE_LANG_FLAG_3 (NODE))
70 #define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
73 /* Record the flags used to compile this translation unit. */
76 repo_compile_flags (argc
, argv
)
82 /* If this template has not been seen before, add a note to the repository
83 saying where the declaration was. This may be used to find the
84 definition at link time. */
87 repo_template_declared (t
)
91 /* Note where the definition of a template lives so that instantiations can
92 be generated later. */
95 repo_template_defined (t
)
99 /* Note where the definition of a class lives to that template
100 instantiations can use it. */
103 repo_class_defined (t
)
112 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
114 t
= TYPE_BINFO_VTABLE (t
);
118 return DECL_ASSEMBLER_NAME (t
);
121 /* Note that a template has been used. If we can see the definition, offer
125 repo_template_used (t
)
130 if (! flag_use_repository
)
133 id
= repo_get_id (t
);
137 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
139 if (IDENTIFIER_REPO_CHOSEN (id
))
140 mark_class_instantiated (t
, 0);
142 else if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
144 if (IDENTIFIER_REPO_CHOSEN (id
))
145 mark_decl_instantiated (t
, 0);
148 my_friendly_abort (1);
150 if (! IDENTIFIER_REPO_USED (id
))
152 IDENTIFIER_REPO_USED (id
) = 1;
153 pending_repo
= perm_tree_cons (NULL_TREE
, id
, pending_repo
);
158 /* Note that the vtable for a class has been used, and offer to emit it. */
164 if (! flag_use_repository
)
167 pending_repo
= perm_tree_cons (NULL_TREE
, t
, pending_repo
);
170 /* Note that an inline with external linkage has been used, and offer to
174 repo_inline_used (fn
)
177 if (! flag_use_repository
)
180 /* Member functions of polymorphic classes go with their vtables. */
181 if (DECL_FUNCTION_MEMBER_P (fn
) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn
)))
183 repo_vtable_used (DECL_CLASS_CONTEXT (fn
));
187 pending_repo
= perm_tree_cons (NULL_TREE
, fn
, pending_repo
);
190 /* Note that a particular typeinfo node has been used, and offer to
201 repo_template_instantiated (t
, extern_p
)
207 tree id
= repo_get_id (t
);
209 IDENTIFIER_REPO_CHOSEN (id
) = 1;
218 return obstack_copy0 (&temporary_obstack
, s
, len
);
221 /* Parse a reasonable subset of shell quoting syntax. */
238 obstack_1grow (&temporary_obstack
, c
);
239 else if (! inside
&& c
== ' ')
241 else if (! inside
&& c
== '\\')
246 obstack_1grow (&temporary_obstack
, c
);
249 obstack_1grow (&temporary_obstack
, '\0');
251 return obstack_finish (&temporary_obstack
);
255 get_base_filename (filename
)
258 char *p
= getenv ("COLLECT_GCC_OPTIONS");
264 char *q
= extract_string (&p
);
266 if (strcmp (q
, "-o") == 0)
267 output
= extract_string (&p
);
268 else if (strcmp (q
, "-c") == 0)
272 if (compiling
&& output
)
275 if (p
&& ! compiling
)
277 warning ("-frepo must be used with -c");
278 flag_use_repository
= 0;
282 p
= rindex (filename
, '/');
290 open_repo_file (filename
)
294 char *s
= get_base_filename (filename
);
306 obstack_grow (&permanent_obstack
, s
, p
- s
);
307 repo_name
= obstack_copy0 (&permanent_obstack
, ".rpo", 4);
309 repo_file
= fopen (repo_name
, "r");
317 while ((c
= getc (stream
)) != EOF
&& c
!= '\n')
318 obstack_1grow (&temporary_obstack
, c
);
319 if (obstack_object_size (&temporary_obstack
) == 0)
321 obstack_1grow (&temporary_obstack
, '\0');
322 return obstack_finish (&temporary_obstack
);
331 if (! flag_use_repository
)
334 open_repo_file (filename
);
339 while ((buf
= afgets (repo_file
)))
344 old_args
= obstack_copy0 (&permanent_obstack
, buf
+ 2,
348 old_dir
= obstack_copy0 (&permanent_obstack
, buf
+ 2,
352 old_main
= obstack_copy0 (&permanent_obstack
, buf
+ 2,
358 tree id
= get_identifier (buf
+ 2);
363 IDENTIFIER_REPO_CHOSEN (id
) = 1;
364 orig
= integer_one_node
;
369 original_repo
= perm_tree_cons (orig
, id
, original_repo
);
373 error ("mysterious repository information in %s", repo_name
);
375 obstack_free (&temporary_obstack
, buf
);
380 reopen_repo_file_for_write ()
384 repo_file
= fopen (repo_name
, "w");
388 error ("can't create repository information file `%s'", repo_name
);
389 flag_use_repository
= 0;
393 /* Emit any pending repos. */
399 int repo_changed
= 0;
402 if (! flag_use_repository
)
405 /* Do we have to write out a new info file? */
407 /* Are there any old templates that aren't used any longer or that are
410 for (t
= original_repo
; t
; t
= TREE_CHAIN (t
))
412 if (! IDENTIFIER_REPO_USED (TREE_VALUE (t
))
413 || (! TREE_PURPOSE (t
) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t
))))
418 IDENTIFIER_REPO_USED (TREE_VALUE (t
)) = 0;
421 /* Are there any templates that are newly used? */
424 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
426 if (IDENTIFIER_REPO_USED (TREE_VALUE (t
)))
434 args
= getenv ("COLLECT_GCC_OPTIONS");
436 if (! repo_changed
&& pending_repo
)
437 if (strcmp (old_main
, main_input_filename
) != 0
438 || strcmp (old_dir
, dir
) != 0
439 || (args
== NULL
) != (old_args
== NULL
)
440 || (args
&& strcmp (old_args
, args
) != 0))
443 if (! repo_changed
|| errorcount
|| sorrycount
)
446 reopen_repo_file_for_write ();
451 fprintf (repo_file
, "M %s\n", main_input_filename
);
452 fprintf (repo_file
, "D %s\n", dir
);
454 fprintf (repo_file
, "A %s\n", args
);
456 for (t
= pending_repo
; t
; t
= TREE_CHAIN (t
))
458 tree val
= TREE_VALUE (t
);
459 char type
= IDENTIFIER_REPO_CHOSEN (val
) ? 'C' : 'O';
461 fprintf (repo_file
, "%c %s\n", type
, IDENTIFIER_POINTER (val
));