summaryrefslogtreecommitdiff
path: root/gcc/cp/repo.c
blob: 8b9f73f223adf75d62412d8d96e0a594ed830c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Code to maintain a C++ template repository.
   Copyright (C) 1995-2013 Free Software Foundation, Inc.
   Contributed by Jason Merrill (jason@cygnus.com)

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* My strategy here is as follows:

   Everything should be emitted in a translation unit where it is used.
   The results of the automatic process should be easily reproducible with
   explicit code.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "cp-tree.h"
#include "input.h"
#include "obstack.h"
#include "toplev.h"
#include "diagnostic-core.h"
#include "flags.h"

static const char *extract_string (const char **);
static const char *get_base_filename (const char *);
static FILE *open_repo_file (const char *);
static char *afgets (FILE *);
static FILE *reopen_repo_file_for_write (void);

static GTY(()) vec<tree, va_gc> *pending_repo;
static char *repo_name;

static const char *old_args, *old_dir, *old_main;

static struct obstack temporary_obstack;
static bool temporary_obstack_initialized_p;

/* Parse a reasonable subset of shell quoting syntax.  */

static const char *
extract_string (const char **pp)
{
  const char *p = *pp;
  int backquote = 0;
  int inside = 0;

  for (;;)
    {
      char c = *p;
      if (c == '\0')
	break;
      ++p;
      if (backquote)
	{
	  obstack_1grow (&temporary_obstack, c);
	  backquote = 0;
	}
      else if (! inside && c == ' ')
	break;
      else if (! inside && c == '\\')
	backquote = 1;
      else if (c == '\'')
	inside = !inside;
      else
	obstack_1grow (&temporary_obstack, c);
    }

  obstack_1grow (&temporary_obstack, '\0');
  *pp = p;
  return (char *) obstack_finish (&temporary_obstack);
}

static const char *
get_base_filename (const char *filename)
{
  const char *p = getenv ("COLLECT_GCC_OPTIONS");
  const char *output = NULL;
  int compiling = 0;

  while (p && *p)
    {
      const char *q = extract_string (&p);

      if (strcmp (q, "-o") == 0)
	{
	  if (flag_compare_debug)
	    /* Just in case aux_base_name was based on a name with two
	       or more '.'s, add an arbitrary extension that will be
	       stripped by the caller.  */
	    output = concat (aux_base_name, ".o", NULL);
	  else
	    output = extract_string (&p);
	}
      else if (strcmp (q, "-c") == 0)
	compiling = 1;
    }

  if (compiling && output)
    return output;

  if (p && ! compiling)
    {
      warning (0, "-frepo must be used with -c");
      flag_use_repository = 0;
      return NULL;
    }

  return lbasename (filename);
}

static FILE *
open_repo_file (const char *filename)
{
  const char *p;
  const char *s = get_base_filename (filename);

  if (s == NULL)
    return NULL;

  p = lbasename (s);
  p = strrchr (p, '.');
  if (! p)
    p = s + strlen (s);

  repo_name = XNEWVEC (char, p - s + 5);
  memcpy (repo_name, s, p - s);
  memcpy (repo_name + (p - s), ".rpo", 5);

  return fopen (repo_name, "r");
}

static char *
afgets (FILE *stream)
{
  int c;
  while ((c = getc (stream)) != EOF && c != '\n')
    obstack_1grow (&temporary_obstack, c);
  if (obstack_object_size (&temporary_obstack) == 0)
    return NULL;
  obstack_1grow (&temporary_obstack, '\0');
  return (char *) obstack_finish (&temporary_obstack);
}

void
init_repo (void)
{
  char *buf;
  const char *p;
  FILE *repo_file;

  if (! flag_use_repository)
    return;

  /* When a PCH file is loaded, the entire identifier table is
     replaced, with the result that IDENTIFIER_REPO_CHOSEN is cleared.
     So, we have to reread the repository file.  */
  lang_post_pch_load = init_repo;

  if (!temporary_obstack_initialized_p)
    gcc_obstack_init (&temporary_obstack);

  repo_file = open_repo_file (main_input_filename);

  if (repo_file == 0)
    return;

  while ((buf = afgets (repo_file)))
    {
      switch (buf[0])
	{
	case 'A':
	  old_args = ggc_strdup (buf + 2);
	  break;
	case 'D':
	  old_dir = ggc_strdup (buf + 2);
	  break;
	case 'M':
	  old_main = ggc_strdup (buf + 2);
	  break;
	case 'O':
	  /* A symbol that we were able to define the last time this
	     file was compiled.  */
	  break;
	case 'C':
	  /* A symbol that the prelinker has requested that we
	     define.  */
	  {
	    tree id = get_identifier (buf + 2);
	    IDENTIFIER_REPO_CHOSEN (id) = 1;
	  }
	  break;
	default:
	  error ("mysterious repository information in %s", repo_name);
	}
      obstack_free (&temporary_obstack, buf);
    }
  fclose (repo_file);

  if (old_args && !get_random_seed (true)
      && (p = strstr (old_args, "'-frandom-seed=")))
    set_random_seed (extract_string (&p) + strlen ("-frandom-seed="));
}

static FILE *
reopen_repo_file_for_write (void)
{
  FILE *repo_file = fopen (repo_name, "w");

  if (repo_file == 0)
    {
      error ("can%'t create repository information file %qs", repo_name);
      flag_use_repository = 0;
    }

  return repo_file;
}

/* Emit any pending repos.  */

void
finish_repo (void)
{
  tree val;
  char *dir, *args;
  FILE *repo_file;
  unsigned ix;

  if (!flag_use_repository || flag_compare_debug)
    return;

  if (seen_error ())
    return;

  repo_file = reopen_repo_file_for_write ();
  if (repo_file == 0)
    goto out;

  fprintf (repo_file, "M %s\n", main_input_filename);
  dir = getpwd ();
  fprintf (repo_file, "D %s\n", dir);
  args = getenv ("COLLECT_GCC_OPTIONS");
  if (args)
    {
      fprintf (repo_file, "A %s", args);
      /* If -frandom-seed is not among the ARGS, then add the value
	 that we chose.  That will ensure that the names of types from
	 anonymous namespaces will get the same mangling when this
	 file is recompiled.  */
      if (!strstr (args, "'-frandom-seed="))
	fprintf (repo_file, " '-frandom-seed=" HOST_WIDE_INT_PRINT_HEX_PURE "'", 
		 get_random_seed (false));
      fprintf (repo_file, "\n");
    }

  FOR_EACH_VEC_SAFE_ELT_REVERSE (pending_repo, ix, val)
    {
      tree name = DECL_ASSEMBLER_NAME (val);
      char type = IDENTIFIER_REPO_CHOSEN (name) ? 'C' : 'O';
      fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (name));
    }

 out:
  if (repo_file)
    fclose (repo_file);
}

/* DECL is a FUNCTION_DECL or VAR_DECL with vague linkage whose
   definition is available in this translation unit.  Returns 0 if
   this definition should not be emitted in this translation unit
   because it will be emitted elsewhere.  Returns 1 if the repository
   file indicates that that DECL should be emitted in this translation
   unit, or 2 if the repository file is not in use.  */

int
repo_emit_p (tree decl)
{
  int ret = 0;
  gcc_assert (TREE_PUBLIC (decl));
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
	      || TREE_CODE (decl) == VAR_DECL);
  gcc_assert (!DECL_REALLY_EXTERN (decl));

  /* When not using the repository, emit everything.  */
  if (!flag_use_repository)
    return 2;

  /* Only template instantiations are managed by the repository.  This
     is an artificial restriction; the code in the prelinker and here
     will work fine if all entities with vague linkage are managed by
     the repository.  */
  if (TREE_CODE (decl) == VAR_DECL)
    {
      tree type = NULL_TREE;
      if (DECL_VTABLE_OR_VTT_P (decl))
	type = DECL_CONTEXT (decl);
      else if (DECL_TINFO_P (decl))
	type = TREE_TYPE (DECL_NAME (decl));
      if (!DECL_TEMPLATE_INSTANTIATION (decl)
	  && (!TYPE_LANG_SPECIFIC (type)
	      || !CLASSTYPE_TEMPLATE_INSTANTIATION (type)))
	return 2;
      /* Const static data members initialized by constant expressions must
	 be processed where needed so that their definitions are
	 available.  Still record them into *.rpo files, so if they
	 weren't actually emitted and collect2 requests them, they can
	 be provided.  */
      if (decl_maybe_constant_var_p (decl)
	  && DECL_CLASS_SCOPE_P (decl))
	ret = 2;
    }
  else if (!DECL_TEMPLATE_INSTANTIATION (decl))
    return 2;

  if (DECL_EXPLICIT_INSTANTIATION (decl))
    return 2;

  /* For constructors and destructors, the repository contains
     information about the clones -- not the original function --
     because only the clones are emitted in the object file.  */
  if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
      || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
    {
      int emit_p = 0;
      tree clone;
      /* There is no early exit from this loop because we want to
	 ensure that all of the clones are marked as available in this
	 object file.  */
      FOR_EACH_CLONE (clone, decl)
	/* The only possible results from the recursive call to
	   repo_emit_p are 0 or 1.  */
	if (repo_emit_p (clone))
	  emit_p = 1;
      return emit_p;
    }

  /* Keep track of all available entities.  */
  if (!DECL_REPO_AVAILABLE_P (decl))
    {
      DECL_REPO_AVAILABLE_P (decl) = 1;
      vec_safe_push (pending_repo, decl);
    }

  return IDENTIFIER_REPO_CHOSEN (DECL_ASSEMBLER_NAME (decl)) ? 1 : ret;
}

/* Returns true iff the prelinker has explicitly marked CLASS_TYPE for
   export from this translation unit.  */

bool
repo_export_class_p (const_tree class_type)
{
  if (!flag_use_repository)
    return false;
  if (!CLASSTYPE_VTABLES (class_type))
    return false;
  /* If the virtual table has been assigned to this translation unit,
     export the class.  */
  return (IDENTIFIER_REPO_CHOSEN
	  (DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (class_type))));
}

#include "gt-cp-repo.h"