gcc/ChangeLog
[official-gcc.git] / gcc / cp / repo.c
blob5155a2fa2959aeece437e286f8c3048d4def35c2
1 /* Code to maintain a C++ template repository.
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
3 Contributed by Jason Merrill (jason@cygnus.com)
5 This file is part of GCC.
7 GCC 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 3, or (at your option)
10 any later version.
12 GCC 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 GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* My strategy here is as follows:
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. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "cp-tree.h"
35 #include "obstack.h"
36 #include "toplev.h"
37 #include "diagnostic-core.h"
38 #include "flags.h"
40 static const char *extract_string (const char **);
41 static const char *get_base_filename (const char *);
42 static FILE *open_repo_file (const char *);
43 static char *afgets (FILE *);
44 static FILE *reopen_repo_file_for_write (void);
46 static GTY(()) vec<tree, va_gc> *pending_repo;
47 static char *repo_name;
49 static const char *old_args, *old_dir, *old_main;
51 static struct obstack temporary_obstack;
52 static bool temporary_obstack_initialized_p;
54 /* Parse a reasonable subset of shell quoting syntax. */
56 static const char *
57 extract_string (const char **pp)
59 const char *p = *pp;
60 int backquote = 0;
61 int inside = 0;
63 for (;;)
65 char c = *p;
66 if (c == '\0')
67 break;
68 ++p;
69 if (backquote)
71 obstack_1grow (&temporary_obstack, c);
72 backquote = 0;
74 else if (! inside && c == ' ')
75 break;
76 else if (! inside && c == '\\')
77 backquote = 1;
78 else if (c == '\'')
79 inside = !inside;
80 else
81 obstack_1grow (&temporary_obstack, c);
84 obstack_1grow (&temporary_obstack, '\0');
85 *pp = p;
86 return (char *) obstack_finish (&temporary_obstack);
89 static const char *
90 get_base_filename (const char *filename)
92 const char *p = getenv ("COLLECT_GCC_OPTIONS");
93 const char *output = NULL;
94 int compiling = 0;
96 while (p && *p)
98 const char *q = extract_string (&p);
100 if (strcmp (q, "-o") == 0)
102 if (flag_compare_debug)
103 /* Just in case aux_base_name was based on a name with two
104 or more '.'s, add an arbitrary extension that will be
105 stripped by the caller. */
106 output = concat (aux_base_name, ".o", NULL);
107 else
108 output = extract_string (&p);
110 else if (strcmp (q, "-c") == 0)
111 compiling = 1;
114 if (compiling && output)
115 return output;
117 if (p && ! compiling)
119 warning (0, "-frepo must be used with -c");
120 flag_use_repository = 0;
121 return NULL;
124 return lbasename (filename);
127 static FILE *
128 open_repo_file (const char *filename)
130 const char *p;
131 const char *s = get_base_filename (filename);
133 if (s == NULL)
134 return NULL;
136 p = lbasename (s);
137 p = strrchr (p, '.');
138 if (! p)
139 p = s + strlen (s);
141 repo_name = XNEWVEC (char, p - s + 5);
142 memcpy (repo_name, s, p - s);
143 memcpy (repo_name + (p - s), ".rpo", 5);
145 return fopen (repo_name, "r");
148 static char *
149 afgets (FILE *stream)
151 int c;
152 while ((c = getc (stream)) != EOF && c != '\n')
153 obstack_1grow (&temporary_obstack, c);
154 if (obstack_object_size (&temporary_obstack) == 0)
155 return NULL;
156 obstack_1grow (&temporary_obstack, '\0');
157 return (char *) obstack_finish (&temporary_obstack);
160 void
161 init_repo (void)
163 char *buf;
164 const char *p;
165 FILE *repo_file;
167 if (! flag_use_repository)
168 return;
170 /* When a PCH file is loaded, the entire identifier table is
171 replaced, with the result that IDENTIFIER_REPO_CHOSEN is cleared.
172 So, we have to reread the repository file. */
173 lang_post_pch_load = init_repo;
175 if (!temporary_obstack_initialized_p)
176 gcc_obstack_init (&temporary_obstack);
178 repo_file = open_repo_file (main_input_filename);
180 if (repo_file == 0)
181 return;
183 while ((buf = afgets (repo_file)))
185 switch (buf[0])
187 case 'A':
188 old_args = ggc_strdup (buf + 2);
189 break;
190 case 'D':
191 old_dir = ggc_strdup (buf + 2);
192 break;
193 case 'M':
194 old_main = ggc_strdup (buf + 2);
195 break;
196 case 'O':
197 /* A symbol that we were able to define the last time this
198 file was compiled. */
199 break;
200 case 'C':
201 /* A symbol that the prelinker has requested that we
202 define. */
204 tree id = get_identifier (buf + 2);
205 IDENTIFIER_REPO_CHOSEN (id) = 1;
207 break;
208 default:
209 error ("mysterious repository information in %s", repo_name);
211 obstack_free (&temporary_obstack, buf);
213 fclose (repo_file);
215 if (old_args && !get_random_seed (true)
216 && (p = strstr (old_args, "'-frandom-seed=")))
217 set_random_seed (extract_string (&p) + strlen ("-frandom-seed="));
220 static FILE *
221 reopen_repo_file_for_write (void)
223 FILE *repo_file = fopen (repo_name, "w");
225 if (repo_file == 0)
227 error ("can%'t create repository information file %qs", repo_name);
228 flag_use_repository = 0;
231 return repo_file;
234 /* Emit any pending repos. */
236 void
237 finish_repo (void)
239 tree val;
240 char *dir, *args;
241 FILE *repo_file;
242 unsigned ix;
244 if (!flag_use_repository || flag_compare_debug)
245 return;
247 if (seen_error ())
248 return;
250 repo_file = reopen_repo_file_for_write ();
251 if (repo_file == 0)
252 goto out;
254 fprintf (repo_file, "M %s\n", main_input_filename);
255 dir = getpwd ();
256 fprintf (repo_file, "D %s\n", dir);
257 args = getenv ("COLLECT_GCC_OPTIONS");
258 if (args)
260 fprintf (repo_file, "A %s", args);
261 /* If -frandom-seed is not among the ARGS, then add the value
262 that we chose. That will ensure that the names of types from
263 anonymous namespaces will get the same mangling when this
264 file is recompiled. */
265 if (!strstr (args, "'-frandom-seed="))
266 fprintf (repo_file, " '-frandom-seed=" HOST_WIDE_INT_PRINT_HEX_PURE "'",
267 get_random_seed (false));
268 fprintf (repo_file, "\n");
271 FOR_EACH_VEC_SAFE_ELT_REVERSE (pending_repo, ix, val)
273 tree name = DECL_ASSEMBLER_NAME (val);
274 char type = IDENTIFIER_REPO_CHOSEN (name) ? 'C' : 'O';
275 fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (name));
278 out:
279 if (repo_file)
280 fclose (repo_file);
283 /* DECL is a FUNCTION_DECL or VAR_DECL with vague linkage whose
284 definition is available in this translation unit. Returns 0 if
285 this definition should not be emitted in this translation unit
286 because it will be emitted elsewhere. Returns 1 if the repository
287 file indicates that that DECL should be emitted in this translation
288 unit, or 2 if the repository file is not in use. */
291 repo_emit_p (tree decl)
293 int ret = 0;
294 gcc_assert (TREE_PUBLIC (decl));
295 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
296 gcc_assert (!DECL_REALLY_EXTERN (decl)
297 /* A clone might not have its linkage flags updated yet
298 because we call import_export_decl before
299 maybe_clone_body. */
300 || DECL_ABSTRACT_ORIGIN (decl));
302 /* When not using the repository, emit everything. */
303 if (!flag_use_repository)
304 return 2;
306 /* Only template instantiations are managed by the repository. This
307 is an artificial restriction; the code in the prelinker and here
308 will work fine if all entities with vague linkage are managed by
309 the repository. */
310 if (VAR_P (decl))
312 tree type = NULL_TREE;
313 if (DECL_VTABLE_OR_VTT_P (decl))
314 type = DECL_CONTEXT (decl);
315 else if (DECL_TINFO_P (decl))
316 type = TREE_TYPE (DECL_NAME (decl));
317 if (!DECL_TEMPLATE_INSTANTIATION (decl)
318 && (!TYPE_LANG_SPECIFIC (type)
319 || !CLASSTYPE_TEMPLATE_INSTANTIATION (type)))
320 return 2;
321 /* Const static data members initialized by constant expressions must
322 be processed where needed so that their definitions are
323 available. Still record them into *.rpo files, so if they
324 weren't actually emitted and collect2 requests them, they can
325 be provided. */
326 if (decl_maybe_constant_var_p (decl)
327 && DECL_CLASS_SCOPE_P (decl))
328 ret = 2;
330 else if (!DECL_TEMPLATE_INSTANTIATION (decl))
331 return 2;
333 if (DECL_EXPLICIT_INSTANTIATION (decl))
334 return 2;
336 /* For constructors and destructors, the repository contains
337 information about the clones -- not the original function --
338 because only the clones are emitted in the object file. */
339 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
340 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
342 int emit_p = 0;
343 tree clone;
344 /* There is no early exit from this loop because we want to
345 ensure that all of the clones are marked as available in this
346 object file. */
347 FOR_EACH_CLONE (clone, decl)
348 /* The only possible results from the recursive call to
349 repo_emit_p are 0 or 1. */
350 if (repo_emit_p (clone))
351 emit_p = 1;
352 return emit_p;
355 /* Keep track of all available entities. */
356 if (!DECL_REPO_AVAILABLE_P (decl))
358 DECL_REPO_AVAILABLE_P (decl) = 1;
359 vec_safe_push (pending_repo, decl);
362 return IDENTIFIER_REPO_CHOSEN (DECL_ASSEMBLER_NAME (decl)) ? 1 : ret;
365 /* Returns true iff the prelinker has explicitly marked CLASS_TYPE for
366 export from this translation unit. */
368 bool
369 repo_export_class_p (const_tree class_type)
371 if (!flag_use_repository)
372 return false;
373 if (!CLASSTYPE_VTABLES (class_type))
374 return false;
375 /* If the virtual table has been assigned to this translation unit,
376 export the class. */
377 return (IDENTIFIER_REPO_CHOSEN
378 (DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (class_type))));
381 #include "gt-cp-repo.h"