OSDN Git Service

Fix compilation.
[pf3gnuchains/gcc-fork.git] / gcc / cp / pt.c
index 6e62292..7a66d36 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle parameterized types (templates) for GNU C++.
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
+   2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
    Rewritten by Jason Merrill (jason@cygnus.com).
@@ -493,6 +493,9 @@ add_to_template_args (tree args, tree extra_args)
   int i;
   int j;
 
+  if (args == NULL_TREE)
+    return extra_args;
+
   extra_depth = TMPL_ARGS_DEPTH (extra_args);
   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
 
@@ -1560,6 +1563,12 @@ iterative_hash_template_arg (tree arg, hashval_t val)
       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
       return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
 
+    case LAMBDA_EXPR:
+      /* A lambda can't appear in a template arg, but don't crash on
+        erroneous input.  */
+      gcc_assert (errorcount > 0);
+      return val;
+
     default:
       switch (tclass)
        {
@@ -3568,6 +3577,9 @@ end_template_parm_list (tree parms)
       next = TREE_CHAIN (parm);
       TREE_VEC_ELT (saved_parmlist, nparms) = parm;
       TREE_CHAIN (parm) = NULL_TREE;
+      if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
+       TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
+             current_template_parms;
     }
 
   --processing_template_parmlist;
@@ -3714,15 +3726,17 @@ maybe_update_decl_type (tree orig_type, tree scope)
         TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
       tree args = current_template_args ();
       tree auto_node = type_uses_auto (type);
+      tree pushed;
       if (auto_node)
        {
          tree auto_vec = make_tree_vec (1);
          TREE_VEC_ELT (auto_vec, 0) = auto_node;
          args = add_to_template_args (args, auto_vec);
        }
-      push_scope (scope);
+      pushed = push_scope (scope);
       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
-      pop_scope (scope);
+      if (pushed)
+       pop_scope (scope);
     }
 
   if (type == error_mark_node)
@@ -3823,7 +3837,7 @@ process_partial_specialization (tree decl)
   int nargs = TREE_VEC_LENGTH (inner_args);
   int ntparms;
   int  i;
-  int did_error_intro = 0;
+  bool did_error_intro = false;
   struct template_parm_data tpd;
   struct template_parm_data tpd2;
 
@@ -3885,12 +3899,15 @@ process_partial_specialization (tree decl)
        if (!did_error_intro)
          {
            error ("template parameters not used in partial specialization:");
-           did_error_intro = 1;
+           did_error_intro = true;
          }
 
        error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
       }
 
+  if (did_error_intro)
+    return error_mark_node;
+
   /* [temp.class.spec]
 
      The argument list of the specialization shall not be identical to
@@ -4545,6 +4562,9 @@ push_template_decl_real (tree decl, bool is_friend)
 
            if (current == decl)
              current = ctx;
+           else if (current == NULL_TREE)
+             /* Can happen in erroneous input.  */
+             break;
            else
              current = (TYPE_P (current)
                         ? TYPE_CONTEXT (current)
@@ -4616,9 +4636,6 @@ template arguments to %qD do not match original template %qD",
          tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
          if (TREE_CODE (parm) == TEMPLATE_DECL)
            DECL_CONTEXT (parm) = tmpl;
-
-         if (TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM)
-           DECL_CONTEXT (TYPE_NAME (TREE_TYPE (parm))) = tmpl;
        }
     }
 
@@ -4916,6 +4933,7 @@ convert_nontype_argument (tree type, tree expr)
   if (error_operand_p (expr))
     return error_mark_node;
   expr_type = TREE_TYPE (expr);
+  expr = mark_rvalue_use (expr);
 
   /* HACK: Due to double coercion, we can get a
      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
@@ -7322,11 +7340,18 @@ tsubst_friend_function (tree decl, tree args)
              DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
 
              if (TREE_CODE (old_decl) != TEMPLATE_DECL)
-               /* We should have called reregister_specialization in
-                  duplicate_decls.  */
-               gcc_assert (retrieve_specialization (new_template,
-                                                    new_args, 0)
-                           == old_decl);
+               {
+                 /* We should have called reregister_specialization in
+                    duplicate_decls.  */
+                 gcc_assert (retrieve_specialization (new_template,
+                                                      new_args, 0)
+                             == old_decl);
+
+                 /* Instantiate it if the global has already been used.  */
+                 if (DECL_ODR_USED (old_decl))
+                   instantiate_decl (old_decl, /*defer_ok=*/true,
+                                     /*expl_inst_class_mem_p=*/false);
+               }
              else
                {
                  tree t;
@@ -9012,7 +9037,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
               specialize R.  */
            gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
            argvec = tsubst_template_args (DECL_TI_ARGS
-                                          (DECL_TEMPLATE_RESULT (gen_tmpl)),
+                                          (DECL_TEMPLATE_RESULT
+                                                 (DECL_TI_TEMPLATE (t))),
                                           args, complain, in_decl);
 
            /* Check to see if we already have this specialization.  */
@@ -9480,6 +9506,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
              type = DECL_ORIGINAL_TYPE (t);
            else
              type = TREE_TYPE (t);
+           if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
+             type = strip_array_domain (type);
            type = tsubst (type, args, complain, in_decl);
          }
        if (TREE_CODE (r) == VAR_DECL)
@@ -10122,7 +10150,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
              }
            else
              /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
-             return arg;
+             return unshare_expr (arg);
          }
 
        if (level == 1)
@@ -14444,6 +14472,10 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict)
       FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
        {
          int elt_strict = strict;
+
+         if (elt == error_mark_node)
+           return 1;
+
          if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
            {
              tree type = TREE_TYPE (elt);
@@ -15055,6 +15087,13 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict)
       /* Matched cases are handled by the ARG == PARM test above.  */
       return 1;
 
+    case VAR_DECL:
+      /* A non-type template parameter that is a variable should be a
+        an integral constant, in which case, it whould have been
+        folded into its (constant) value. So we should not be getting
+        a variable here.  */
+      gcc_unreachable ();
+
     case TYPE_ARGUMENT_PACK:
     case NONTYPE_ARGUMENT_PACK:
       {
@@ -15464,13 +15503,10 @@ more_specialized_fn (tree pat1, tree pat2, int len)
         than the type from the parameter template (as described above)
         that type is considered to be more specialized than the other. If
         neither type is more cv-qualified than the other then neither type
-        is more specialized than the other."
+        is more specialized than the other."  */
 
-         We check same_type_p explicitly because deduction can also succeed
-         in both directions when there is a nondeduced context.  */
       if (deduce1 && deduce2
-         && quals1 != quals2 && quals1 >= 0 && quals2 >= 0
-         && same_type_p (arg1, arg2))
+         && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
        {
          if ((quals1 & quals2) == quals2)
            lose2 = true;
@@ -15913,12 +15949,13 @@ most_specialized_class (tree type, tree tmpl)
       tree parms = TREE_VALUE (t);
 
       partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
+
+      ++processing_template_decl;
+
       if (outer_args)
        {
          int i;
 
-         ++processing_template_decl;
-
          /* Discard the outer levels of args, and then substitute in the
             template args from the enclosing class.  */
          partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
@@ -15935,8 +15972,21 @@ most_specialized_class (tree type, tree tmpl)
            TREE_VEC_ELT (parms, i) =
              tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
 
-         --processing_template_decl;
        }
+
+      partial_spec_args =
+         coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
+                                add_to_template_args (outer_args,
+                                                      partial_spec_args),
+                                tmpl, tf_none,
+                                /*require_all_args=*/true,
+                                /*use_default_args=*/true);
+
+      --processing_template_decl;
+
+      if (partial_spec_args == error_mark_node)
+       return error_mark_node;
+
       spec_args = get_class_bindings (parms,
                                      partial_spec_args,
                                      args);
@@ -16424,10 +16474,15 @@ regenerate_decl_from_template (tree decl, tree tmpl)
        DECL_DECLARED_INLINE_P (decl) = 1;
     }
   else if (TREE_CODE (decl) == VAR_DECL)
-    DECL_INITIAL (decl) =
-      tsubst_expr (DECL_INITIAL (code_pattern), args,
-                  tf_error, DECL_TI_TEMPLATE (decl),
-                  /*integral_constant_expression_p=*/false);
+    {
+      DECL_INITIAL (decl) =
+       tsubst_expr (DECL_INITIAL (code_pattern), args,
+                    tf_error, DECL_TI_TEMPLATE (decl),
+                    /*integral_constant_expression_p=*/false);
+      if (VAR_HAD_UNKNOWN_BOUND (decl))
+       TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
+                                  tf_error, DECL_TI_TEMPLATE (decl));
+    }
   else
     gcc_unreachable ();
 
@@ -17698,6 +17753,15 @@ type_dependent_expression_p (tree expression)
       return false;
     }
 
+  /* A static data member of the current instantiation with incomplete
+     array type is type-dependent, as the definition and specializations
+     can have different bounds.  */
+  if (TREE_CODE (expression) == VAR_DECL
+      && DECL_CLASS_SCOPE_P (expression)
+      && dependent_type_p (DECL_CONTEXT (expression))
+      && VAR_HAD_UNKNOWN_BOUND (expression))
+    return true;
+
   if (TREE_TYPE (expression) == unknown_type_node)
     {
       if (TREE_CODE (expression) == ADDR_EXPR)
@@ -18529,4 +18593,19 @@ init_template_processing (void)
                                          ggc_free);
 }
 
+/* Print stats about the template hash tables for -fstats.  */
+
+void
+print_template_statistics (void)
+{
+  fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
+          "%f collisions\n", (long) htab_size (decl_specializations),
+          (long) htab_elements (decl_specializations),
+          htab_collisions (decl_specializations));
+  fprintf (stderr, "type_specializations: size %ld, %ld elements, "
+          "%f collisions\n", (long) htab_size (type_specializations),
+          (long) htab_elements (type_specializations),
+          htab_collisions (type_specializations));
+}
+
 #include "gt-cp-pt.h"