OSDN Git Service

PR c++/34913
[pf3gnuchains/gcc-fork.git] / gcc / cp / repo.c
index 47a111b..1fe96a2 100644 (file)
@@ -1,23 +1,23 @@
 /* Code to maintain a C++ template repository.
-   Copyright (C) 1995 Free Software Foundation, Inc.
+   Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
+   2006, 2007  Free Software Foundation, Inc.
    Contributed by Jason Merrill (jason@cygnus.com)
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
+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 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
-GNU CC is distributed in the hope that it will be useful,
+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 GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 /* My strategy here is as follows:
 
@@ -25,253 +25,120 @@ Boston, MA 02111-1307, USA.  */
    The results of the automatic process should be easily reproducible with
    explicit code.  */
 
-#include <stdio.h>
 #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.h"
+#include "flags.h"
 
-extern char * rindex ();
-extern char * getenv ();
-extern char * getpwd ();
+static char *extract_string (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 tree pending_repo;
-static tree original_repo;
+static GTY(()) tree pending_repo;
 static char *repo_name;
-static FILE *repo_file;
 
-extern int flag_use_repository;
-extern int errorcount, sorrycount;
-extern struct obstack temporary_obstack;
-extern struct obstack permanent_obstack;
+static const char *old_args, *old_dir, *old_main;
 
-#define IDENTIFIER_REPO_USED(NODE)   (TREE_LANG_FLAG_3 (NODE))
-#define IDENTIFIER_REPO_CHOSEN(NODE) (TREE_LANG_FLAG_4 (NODE))
+static struct obstack temporary_obstack;
+static bool temporary_obstack_initialized_p;
 
-#if 0
-/* Record the flags used to compile this translation unit.  */
+/* Parse a reasonable subset of shell quoting syntax.  */
 
-void
-repo_compile_flags (argc, argv)
-     int argc;
-     char **argv;
-{
-}
-
-/* If this template has not been seen before, add a note to the repository
-   saying where the declaration was.  This may be used to find the
-   definition at link time.  */
-
-void
-repo_template_declared (t)
-     tree t;
-{}
-
-/* Note where the definition of a template lives so that instantiations can
-   be generated later.  */
-
-void
-repo_template_defined (t)
-     tree t;
-{}
-
-/* Note where the definition of a class lives to that template
-   instantiations can use it.  */
-
-void
-repo_class_defined (t)
-     tree t;
-{}
-#endif
-
-static tree
-repo_get_id (t)
-     tree t;
-{
-  if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
-    {
-      t = TYPE_BINFO_VTABLE (t);
-      if (t == NULL_TREE)
-       return t;
-    }
-  return DECL_ASSEMBLER_NAME (t);
-}
-
-/* Note that a template has been used.  If we can see the definition, offer
-   to emit it.  */
-
-void
-repo_template_used (t)
-     tree t;
+static char *
+extract_string (char **pp)
 {
-  tree id;
+  char *p = *pp;
+  int backquote = 0;
+  int inside = 0;
 
-  if (! flag_use_repository)
-    return;
-
-  id = repo_get_id (t);
-  if (id == NULL_TREE)
-    return;
-  
-  if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
+  for (;;)
     {
-      if (IDENTIFIER_REPO_CHOSEN (id))
-       mark_class_instantiated (t, 0);
-    }
-  else if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
-    {
-      if (IDENTIFIER_REPO_CHOSEN (id))
-       mark_decl_instantiated (t, 0);
-    }
-  else
-    my_friendly_abort (1);
-
-  if (! IDENTIFIER_REPO_USED (id))
-    {
-      IDENTIFIER_REPO_USED (id) = 1;
-      pending_repo = perm_tree_cons (NULL_TREE, id, pending_repo);
+      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);
     }
-}
-
-#if 0
-/* Note that the vtable for a class has been used, and offer to emit it.  */
-
-static void
-repo_vtable_used (t)
-     tree t;
-{
-  if (! flag_use_repository)
-    return;
 
-  pending_repo = perm_tree_cons (NULL_TREE, t, pending_repo);
+  obstack_1grow (&temporary_obstack, '\0');
+  *pp = p;
+  return (char *) obstack_finish (&temporary_obstack);
 }
 
-/* Note that an inline with external linkage has been used, and offer to
-   emit it.  */
-
-void
-repo_inline_used (fn)
-     tree fn;
+static const char *
+get_base_filename (const char *filename)
 {
-  if (! flag_use_repository)
-    return;
+  char *p = getenv ("COLLECT_GCC_OPTIONS");
+  char *output = NULL;
+  int compiling = 0;
 
-  /* Member functions of polymorphic classes go with their vtables.  */
-  if (DECL_FUNCTION_MEMBER_P (fn) && TYPE_VIRTUAL_P (DECL_CLASS_CONTEXT (fn)))
+  while (p && *p)
     {
-      repo_vtable_used (DECL_CLASS_CONTEXT (fn));
-      return;
-    }
-
-  pending_repo = perm_tree_cons (NULL_TREE, fn, pending_repo);
-}
+      char *q = extract_string (&p);
 
-/* Note that a particular typeinfo node has been used, and offer to
-   emit it.  */
-
-void
-repo_tinfo_used (ti)
-     tree ti;
-{
-}
-#endif
-
-void
-repo_template_instantiated (t, extern_p)
-     tree t;
-     int extern_p;
-{
-  if (! extern_p)
-    {
-      tree id = repo_get_id (t);
-      if (id)
-       IDENTIFIER_REPO_CHOSEN (id) = 1;
+      if (strcmp (q, "-o") == 0)
+       output = extract_string (&p);
+      else if (strcmp (q, "-c") == 0)
+       compiling = 1;
     }
-}
-
-static char *
-save_string (s, len)
-     char *s;
-     int len;
-{
-  return obstack_copy0 (&temporary_obstack, s, len);
-}
-
-static char *
-get_base_filename (filename)
-     char *filename;
-{
-  char *p = getenv ("COLLECT_GCC_OPTIONS");
-  char *output = 0;
-  int compiling = 0;
-
-  if (p)
-    while (*p)
-      {
-       char *q = p;
-       while (*q && *q != ' ') q++;
-       if (*p == '-' && p[1] == 'o')
-         {
-           p += 2;
-           if (p == q)
-             {
-               p++; q++;
-               if (*q)
-                 while (*q && *q != ' ') q++;
-             }
-
-           output = save_string (p, q - p);
-         }
-       else if (*p == '-' && p[1] == 'c')
-         compiling = 1;
-       if (*q) q++;
-       p = q;
-      }
 
   if (compiling && output)
     return output;
 
   if (p && ! compiling)
     {
-      warning ("-frepo must be used with -c");
+      warning (0, "-frepo must be used with -c");
       flag_use_repository = 0;
       return NULL;
     }
 
-  p = rindex (filename, '/');
-  if (p)
-    return p+1;
-  else
-    return filename;
-}        
+  return lbasename (filename);
+}
 
-static void
-open_repo_file (filename)
-     char *filename;
+static FILE *
+open_repo_file (const char *filename)
 {
-  register char *p;
-  char *s = get_base_filename (filename);
+  const char *p;
+  const char *s = get_base_filename (filename);
 
   if (s == NULL)
-    return;
+    return NULL;
 
-  p = rindex (s, '/');
-  if (! p)
-    p = s;
-  p = rindex (p, '.');
+  p = lbasename (s);
+  p = strrchr (p, '.');
   if (! p)
     p = s + strlen (s);
 
-  obstack_grow (&permanent_obstack, s, p - s);
-  repo_name = obstack_copy0 (&permanent_obstack, ".rpo", 4);
+  repo_name = XNEWVEC (char, p - s + 5);
+  memcpy (repo_name, s, p - s);
+  memcpy (repo_name + (p - s), ".rpo", 5);
 
-  repo_file = fopen (repo_name, "r");
+  return fopen (repo_name, "r");
 }
 
 static char *
-afgets (stream)
-     FILE *stream;
+afgets (FILE *stream)
 {
   int c;
   while ((c = getc (stream)) != EOF && c != '\n')
@@ -279,46 +146,54 @@ afgets (stream)
   if (obstack_object_size (&temporary_obstack) == 0)
     return NULL;
   obstack_1grow (&temporary_obstack, '\0');
-  return obstack_finish (&temporary_obstack);
+  return (char *) obstack_finish (&temporary_obstack);
 }
 
 void
-init_repo (filename)
-     char *filename;
+init_repo (void)
 {
   char *buf;
+  FILE *repo_file;
 
   if (! flag_use_repository)
     return;
 
-  open_repo_file (filename);
+  /* 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))
+  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 'C':
        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);
-           tree orig;
-
-           if (buf[0] == 'C')
-             {
-               IDENTIFIER_REPO_CHOSEN (id) = 1;
-               orig = integer_one_node;
-             }
-           else
-             orig = NULL_TREE;
-
-           original_repo = perm_tree_cons (orig, id, original_repo);
+           IDENTIFIER_REPO_CHOSEN (id) = 1;
          }
          break;
        default:
@@ -326,88 +201,165 @@ init_repo (filename)
        }
       obstack_free (&temporary_obstack, buf);
     }
+  fclose (repo_file);
+
+  if (old_args && !get_random_seed (true)
+      && (buf = strstr (old_args, "'-frandom-seed=")))
+    set_random_seed (extract_string (&buf) + strlen ("-frandom-seed="));
 }
 
-static void
-reopen_repo_file_for_write ()
+static FILE *
+reopen_repo_file_for_write (void)
 {
-  if (repo_file)
-    fclose (repo_file);
-  repo_file = fopen (repo_name, "w");
+  FILE *repo_file = fopen (repo_name, "w");
 
   if (repo_file == 0)
     {
-      error ("can't create repository information file `%s'", repo_name);
+      error ("can't create repository information file %qs", repo_name);
       flag_use_repository = 0;
     }
+
+  return repo_file;
 }
 
 /* Emit any pending repos.  */
 
 void
-finish_repo ()
+finish_repo (void)
 {
   tree t;
-  char *p;
-  int repo_changed = 0;
+  char *dir, *args;
+  FILE *repo_file;
 
-  if (! flag_use_repository)
+  if (!flag_use_repository)
     return;
 
-  /* Do we have to write out a new info file?  */
-
-  /* Are there any old templates that aren't used any longer or that are
-     newly chosen?  */
-  
-  for (t = original_repo; t; t = TREE_CHAIN (t))
-    {
-      if (! IDENTIFIER_REPO_USED (TREE_VALUE (t))
-         || (! TREE_PURPOSE (t) && IDENTIFIER_REPO_CHOSEN (TREE_VALUE (t))))
-       {
-         repo_changed = 1;
-         break;
-       }
-      IDENTIFIER_REPO_USED (TREE_VALUE (t)) = 0;
-    }
-
-  /* Are there any templates that are newly used?  */
-  
-  if (! repo_changed)
-    for (t = pending_repo; t; t = TREE_CHAIN (t))
-      {
-       if (IDENTIFIER_REPO_USED (TREE_VALUE (t)))
-         {
-           repo_changed = 1;
-           break;
-         }
-      }
-
-  if (! repo_changed || errorcount || sorrycount)
-    goto out;
-
-  reopen_repo_file_for_write ();
+  if (errorcount || sorrycount)
+    return;
 
+  repo_file = reopen_repo_file_for_write ();
   if (repo_file == 0)
     goto out;
 
   fprintf (repo_file, "M %s\n", main_input_filename);
-
-  p = getpwd ();
-  fprintf (repo_file, "D %s\n", p);
-
-  p = getenv ("COLLECT_GCC_OPTIONS");
-  if (p != 0)
-    fprintf (repo_file, "A %s\n", p);
+  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=%s'", get_random_seed (false));
+      fprintf (repo_file, "\n");
+    }
 
   for (t = pending_repo; t; t = TREE_CHAIN (t))
     {
       tree val = TREE_VALUE (t);
-      char type = IDENTIFIER_REPO_CHOSEN (val) ? 'C' : 'O';
-
-      fprintf (repo_file, "%c %s\n", type, IDENTIFIER_POINTER (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)
+{
+  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.  */
+      if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)
+         && DECL_CLASS_SCOPE_P (decl))
+       return 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;
+      pending_repo = tree_cons (NULL_TREE, decl, pending_repo);
+    }
+
+  return IDENTIFIER_REPO_CHOSEN (DECL_ASSEMBLER_NAME (decl));
+}
+
+/* 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"