repo.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* My strategy here is as follows:
  17. Everything should be emitted in a translation unit where it is used.
  18. The results of the automatic process should be easily reproducible with
  19. explicit code. */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "hash-set.h"
  25. #include "machmode.h"
  26. #include "vec.h"
  27. #include "double-int.h"
  28. #include "input.h"
  29. #include "alias.h"
  30. #include "symtab.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "tree.h"
  34. #include "stringpool.h"
  35. #include "cp-tree.h"
  36. #include "input.h"
  37. #include "obstack.h"
  38. #include "toplev.h"
  39. #include "diagnostic-core.h"
  40. #include "flags.h"
  41. static const char *extract_string (const char **);
  42. static const char *get_base_filename (const char *);
  43. static FILE *open_repo_file (const char *);
  44. static char *afgets (FILE *);
  45. static FILE *reopen_repo_file_for_write (void);
  46. static GTY(()) vec<tree, va_gc> *pending_repo;
  47. static char *repo_name;
  48. static const char *old_args, *old_dir, *old_main;
  49. static struct obstack temporary_obstack;
  50. static bool temporary_obstack_initialized_p;
  51. /* Parse a reasonable subset of shell quoting syntax. */
  52. static const char *
  53. extract_string (const char **pp)
  54. {
  55. const char *p = *pp;
  56. int backquote = 0;
  57. int inside = 0;
  58. for (;;)
  59. {
  60. char c = *p;
  61. if (c == '\0')
  62. break;
  63. ++p;
  64. if (backquote)
  65. {
  66. obstack_1grow (&temporary_obstack, c);
  67. backquote = 0;
  68. }
  69. else if (! inside && c == ' ')
  70. break;
  71. else if (! inside && c == '\\')
  72. backquote = 1;
  73. else if (c == '\'')
  74. inside = !inside;
  75. else
  76. obstack_1grow (&temporary_obstack, c);
  77. }
  78. obstack_1grow (&temporary_obstack, '\0');
  79. *pp = p;
  80. return (char *) obstack_finish (&temporary_obstack);
  81. }
  82. static const char *
  83. get_base_filename (const char *filename)
  84. {
  85. const char *p = getenv ("COLLECT_GCC_OPTIONS");
  86. const char *output = NULL;
  87. int compiling = 0;
  88. while (p && *p)
  89. {
  90. const char *q = extract_string (&p);
  91. if (strcmp (q, "-o") == 0)
  92. {
  93. if (flag_compare_debug)
  94. /* Just in case aux_base_name was based on a name with two
  95. or more '.'s, add an arbitrary extension that will be
  96. stripped by the caller. */
  97. output = concat (aux_base_name, ".o", NULL);
  98. else
  99. output = extract_string (&p);
  100. }
  101. else if (strcmp (q, "-c") == 0)
  102. compiling = 1;
  103. }
  104. if (compiling && output)
  105. return output;
  106. if (p && ! compiling)
  107. {
  108. warning (0, "-frepo must be used with -c");
  109. flag_use_repository = 0;
  110. return NULL;
  111. }
  112. return lbasename (filename);
  113. }
  114. static FILE *
  115. open_repo_file (const char *filename)
  116. {
  117. const char *p;
  118. const char *s = get_base_filename (filename);
  119. if (s == NULL)
  120. return NULL;
  121. p = lbasename (s);
  122. p = strrchr (p, '.');
  123. if (! p)
  124. p = s + strlen (s);
  125. repo_name = XNEWVEC (char, p - s + 5);
  126. memcpy (repo_name, s, p - s);
  127. memcpy (repo_name + (p - s), ".rpo", 5);
  128. return fopen (repo_name, "r");
  129. }
  130. static char *
  131. afgets (FILE *stream)
  132. {
  133. int c;
  134. while ((c = getc (stream)) != EOF && c != '\n')
  135. obstack_1grow (&temporary_obstack, c);
  136. if (obstack_object_size (&temporary_obstack) == 0)
  137. return NULL;
  138. obstack_1grow (&temporary_obstack, '\0');
  139. return (char *) obstack_finish (&temporary_obstack);
  140. }
  141. void
  142. init_repo (void)
  143. {
  144. char *buf;
  145. const char *p;
  146. FILE *repo_file;
  147. if (! flag_use_repository)
  148. return;
  149. /* When a PCH file is loaded, the entire identifier table is
  150. replaced, with the result that IDENTIFIER_REPO_CHOSEN is cleared.
  151. So, we have to reread the repository file. */
  152. lang_post_pch_load = init_repo;
  153. if (!temporary_obstack_initialized_p)
  154. gcc_obstack_init (&temporary_obstack);
  155. repo_file = open_repo_file (main_input_filename);
  156. if (repo_file == 0)
  157. return;
  158. while ((buf = afgets (repo_file)))
  159. {
  160. switch (buf[0])
  161. {
  162. case 'A':
  163. old_args = ggc_strdup (buf + 2);
  164. break;
  165. case 'D':
  166. old_dir = ggc_strdup (buf + 2);
  167. break;
  168. case 'M':
  169. old_main = ggc_strdup (buf + 2);
  170. break;
  171. case 'O':
  172. /* A symbol that we were able to define the last time this
  173. file was compiled. */
  174. break;
  175. case 'C':
  176. /* A symbol that the prelinker has requested that we
  177. define. */
  178. {
  179. tree id = get_identifier (buf + 2);
  180. IDENTIFIER_REPO_CHOSEN (id) = 1;
  181. }
  182. break;
  183. default:
  184. error ("mysterious repository information in %s", repo_name);
  185. }
  186. obstack_free (&temporary_obstack, buf);
  187. }
  188. fclose (repo_file);
  189. if (old_args && !get_random_seed (true)
  190. && (p = strstr (old_args, "'-frandom-seed=")))
  191. set_random_seed (extract_string (&p) + strlen ("-frandom-seed="));
  192. }
  193. static FILE *
  194. reopen_repo_file_for_write (void)
  195. {
  196. FILE *repo_file = fopen (repo_name, "w");
  197. if (repo_file == 0)
  198. {
  199. error ("can%'t create repository information file %qs", repo_name);
  200. flag_use_repository = 0;
  201. }
  202. return repo_file;
  203. }
  204. /* Emit any pending repos. */
  205. void
  206. finish_repo (void)
  207. {
  208. tree val;
  209. char *dir, *args;
  210. FILE *repo_file;
  211. unsigned ix;
  212. if (!flag_use_repository || flag_compare_debug)
  213. return;
  214. if (seen_error ())
  215. return;
  216. repo_file = reopen_repo_file_for_write ();
  217. if (repo_file == 0)
  218. goto out;
  219. fprintf (repo_file, "M %s\n", main_input_filename);
  220. dir = getpwd ();
  221. fprintf (repo_file, "D %s\n", dir);
  222. args = getenv ("COLLECT_GCC_OPTIONS");
  223. if (args)
  224. {
  225. fprintf (repo_file, "A %s", args);
  226. /* If -frandom-seed is not among the ARGS, then add the value
  227. that we chose. That will ensure that the names of types from
  228. anonymous namespaces will get the same mangling when this
  229. file is recompiled. */
  230. if (!strstr (args, "'-frandom-seed="))
  231. fprintf (repo_file, " '-frandom-seed=" HOST_WIDE_INT_PRINT_HEX_PURE "'",
  232. get_random_seed (false));
  233. fprintf (repo_file, "\n");
  234. }
  235. FOR_EACH_VEC_SAFE_ELT_REVERSE (pending_repo, ix, val)
  236. {
  237. tree name = DECL_ASSEMBLER_NAME (val);
  238. char type = IDENTIFIER_REPO_CHOSEN (name) ? 'C' : 'O';
  239. fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (name));
  240. }
  241. out:
  242. if (repo_file)
  243. fclose (repo_file);
  244. }
  245. /* DECL is a FUNCTION_DECL or VAR_DECL with vague linkage whose
  246. definition is available in this translation unit. Returns 0 if
  247. this definition should not be emitted in this translation unit
  248. because it will be emitted elsewhere. Returns 1 if the repository
  249. file indicates that that DECL should be emitted in this translation
  250. unit, or 2 if the repository file is not in use. */
  251. int
  252. repo_emit_p (tree decl)
  253. {
  254. int ret = 0;
  255. gcc_assert (TREE_PUBLIC (decl));
  256. gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
  257. gcc_assert (!DECL_REALLY_EXTERN (decl)
  258. /* A clone might not have its linkage flags updated yet
  259. because we call import_export_decl before
  260. maybe_clone_body. */
  261. || DECL_ABSTRACT_ORIGIN (decl));
  262. /* When not using the repository, emit everything. */
  263. if (!flag_use_repository)
  264. return 2;
  265. /* Only template instantiations are managed by the repository. This
  266. is an artificial restriction; the code in the prelinker and here
  267. will work fine if all entities with vague linkage are managed by
  268. the repository. */
  269. if (VAR_P (decl))
  270. {
  271. tree type = NULL_TREE;
  272. if (DECL_VTABLE_OR_VTT_P (decl))
  273. type = DECL_CONTEXT (decl);
  274. else if (DECL_TINFO_P (decl))
  275. type = TREE_TYPE (DECL_NAME (decl));
  276. if (!DECL_TEMPLATE_INSTANTIATION (decl)
  277. && (!TYPE_LANG_SPECIFIC (type)
  278. || !CLASSTYPE_TEMPLATE_INSTANTIATION (type)))
  279. return 2;
  280. /* Const static data members initialized by constant expressions must
  281. be processed where needed so that their definitions are
  282. available. Still record them into *.rpo files, so if they
  283. weren't actually emitted and collect2 requests them, they can
  284. be provided. */
  285. if (decl_maybe_constant_var_p (decl)
  286. && DECL_CLASS_SCOPE_P (decl))
  287. ret = 2;
  288. }
  289. else if (!DECL_TEMPLATE_INSTANTIATION (decl))
  290. return 2;
  291. if (DECL_EXPLICIT_INSTANTIATION (decl))
  292. return 2;
  293. /* For constructors and destructors, the repository contains
  294. information about the clones -- not the original function --
  295. because only the clones are emitted in the object file. */
  296. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
  297. || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
  298. {
  299. int emit_p = 0;
  300. tree clone;
  301. /* There is no early exit from this loop because we want to
  302. ensure that all of the clones are marked as available in this
  303. object file. */
  304. FOR_EACH_CLONE (clone, decl)
  305. /* The only possible results from the recursive call to
  306. repo_emit_p are 0 or 1. */
  307. if (repo_emit_p (clone))
  308. emit_p = 1;
  309. return emit_p;
  310. }
  311. /* Keep track of all available entities. */
  312. if (!DECL_REPO_AVAILABLE_P (decl))
  313. {
  314. DECL_REPO_AVAILABLE_P (decl) = 1;
  315. vec_safe_push (pending_repo, decl);
  316. }
  317. return IDENTIFIER_REPO_CHOSEN (DECL_ASSEMBLER_NAME (decl)) ? 1 : ret;
  318. }
  319. /* Returns true iff the prelinker has explicitly marked CLASS_TYPE for
  320. export from this translation unit. */
  321. bool
  322. repo_export_class_p (const_tree class_type)
  323. {
  324. if (!flag_use_repository)
  325. return false;
  326. if (!CLASSTYPE_VTABLES (class_type))
  327. return false;
  328. /* If the virtual table has been assigned to this translation unit,
  329. export the class. */
  330. return (IDENTIFIER_REPO_CHOSEN
  331. (DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (class_type))));
  332. }
  333. #include "gt-cp-repo.h"