OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [cp/] [pt.c] - Diff between revs 816 and 826

Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* Handle parameterized types (templates) for GNU C++.
/* Handle parameterized types (templates) for GNU C++.
   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
   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
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
   Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
   Rewritten by Jason Merrill (jason@cygnus.com).
   Rewritten by Jason Merrill (jason@cygnus.com).
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC 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
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC 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
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
/* Known bugs or deficiencies include:
/* Known bugs or deficiencies include:
 
 
     all methods must be provided in header files; can't use a source
     all methods must be provided in header files; can't use a source
     file that contains only the method templates and "just win".  */
     file that contains only the method templates and "just win".  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "tm.h"
#include "tm.h"
#include "obstack.h"
#include "obstack.h"
#include "tree.h"
#include "tree.h"
#include "intl.h"
#include "intl.h"
#include "pointer-set.h"
#include "pointer-set.h"
#include "flags.h"
#include "flags.h"
#include "c-common.h"
#include "c-common.h"
#include "cp-tree.h"
#include "cp-tree.h"
#include "cp-objcp-common.h"
#include "cp-objcp-common.h"
#include "tree-inline.h"
#include "tree-inline.h"
#include "decl.h"
#include "decl.h"
#include "output.h"
#include "output.h"
#include "except.h"
#include "except.h"
#include "toplev.h"
#include "toplev.h"
#include "rtl.h"
#include "rtl.h"
#include "timevar.h"
#include "timevar.h"
#include "tree-iterator.h"
#include "tree-iterator.h"
#include "vecprim.h"
#include "vecprim.h"
 
 
/* The type of functions taking a tree, and some additional data, and
/* The type of functions taking a tree, and some additional data, and
   returning an int.  */
   returning an int.  */
typedef int (*tree_fn_t) (tree, void*);
typedef int (*tree_fn_t) (tree, void*);
 
 
/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
   instantiations have been deferred, either because their definitions
   instantiations have been deferred, either because their definitions
   were not yet available, or because we were putting off doing the work.  */
   were not yet available, or because we were putting off doing the work.  */
struct GTY (()) pending_template {
struct GTY (()) pending_template {
  struct pending_template *next;
  struct pending_template *next;
  struct tinst_level *tinst;
  struct tinst_level *tinst;
};
};
 
 
static GTY(()) struct pending_template *pending_templates;
static GTY(()) struct pending_template *pending_templates;
static GTY(()) struct pending_template *last_pending_template;
static GTY(()) struct pending_template *last_pending_template;
 
 
int processing_template_parmlist;
int processing_template_parmlist;
static int template_header_count;
static int template_header_count;
 
 
static GTY(()) tree saved_trees;
static GTY(()) tree saved_trees;
static VEC(int,heap) *inline_parm_levels;
static VEC(int,heap) *inline_parm_levels;
 
 
static GTY(()) struct tinst_level *current_tinst_level;
static GTY(()) struct tinst_level *current_tinst_level;
 
 
static GTY(()) tree saved_access_scope;
static GTY(()) tree saved_access_scope;
 
 
/* Live only within one (recursive) call to tsubst_expr.  We use
/* Live only within one (recursive) call to tsubst_expr.  We use
   this to pass the statement expression node from the STMT_EXPR
   this to pass the statement expression node from the STMT_EXPR
   to the EXPR_STMT that is its result.  */
   to the EXPR_STMT that is its result.  */
static tree cur_stmt_expr;
static tree cur_stmt_expr;
 
 
/* A map from local variable declarations in the body of the template
/* A map from local variable declarations in the body of the template
   presently being instantiated to the corresponding instantiated
   presently being instantiated to the corresponding instantiated
   local variables.  */
   local variables.  */
static htab_t local_specializations;
static htab_t local_specializations;
 
 
typedef struct GTY(()) spec_entry
typedef struct GTY(()) spec_entry
{
{
  tree tmpl;
  tree tmpl;
  tree args;
  tree args;
  tree spec;
  tree spec;
} spec_entry;
} spec_entry;
 
 
static GTY ((param_is (spec_entry)))
static GTY ((param_is (spec_entry)))
  htab_t decl_specializations;
  htab_t decl_specializations;
 
 
static GTY ((param_is (spec_entry)))
static GTY ((param_is (spec_entry)))
  htab_t type_specializations;
  htab_t type_specializations;
 
 
/* Contains canonical template parameter types. The vector is indexed by
/* Contains canonical template parameter types. The vector is indexed by
   the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
   the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
   TREE_LIST, whose TREE_VALUEs contain the canonical template
   TREE_LIST, whose TREE_VALUEs contain the canonical template
   parameters of various types and levels.  */
   parameters of various types and levels.  */
static GTY(()) VEC(tree,gc) *canonical_template_parms;
static GTY(()) VEC(tree,gc) *canonical_template_parms;
 
 
#define UNIFY_ALLOW_NONE 0
#define UNIFY_ALLOW_NONE 0
#define UNIFY_ALLOW_MORE_CV_QUAL 1
#define UNIFY_ALLOW_MORE_CV_QUAL 1
#define UNIFY_ALLOW_LESS_CV_QUAL 2
#define UNIFY_ALLOW_LESS_CV_QUAL 2
#define UNIFY_ALLOW_DERIVED 4
#define UNIFY_ALLOW_DERIVED 4
#define UNIFY_ALLOW_INTEGER 8
#define UNIFY_ALLOW_INTEGER 8
#define UNIFY_ALLOW_OUTER_LEVEL 16
#define UNIFY_ALLOW_OUTER_LEVEL 16
#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
 
 
static void push_access_scope (tree);
static void push_access_scope (tree);
static void pop_access_scope (tree);
static void pop_access_scope (tree);
static bool resolve_overloaded_unification (tree, tree, tree, tree,
static bool resolve_overloaded_unification (tree, tree, tree, tree,
                                            unification_kind_t, int);
                                            unification_kind_t, int);
static int try_one_overload (tree, tree, tree, tree, tree,
static int try_one_overload (tree, tree, tree, tree, tree,
                             unification_kind_t, int, bool);
                             unification_kind_t, int, bool);
static int unify (tree, tree, tree, tree, int);
static int unify (tree, tree, tree, tree, int);
static void add_pending_template (tree);
static void add_pending_template (tree);
static tree reopen_tinst_level (struct tinst_level *);
static tree reopen_tinst_level (struct tinst_level *);
static tree tsubst_initializer_list (tree, tree);
static tree tsubst_initializer_list (tree, tree);
static tree get_class_bindings (tree, tree, tree);
static tree get_class_bindings (tree, tree, tree);
static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
                                   bool, bool);
                                   bool, bool);
static void tsubst_enum (tree, tree, tree);
static void tsubst_enum (tree, tree, tree);
static tree add_to_template_args (tree, tree);
static tree add_to_template_args (tree, tree);
static tree add_outermost_template_args (tree, tree);
static tree add_outermost_template_args (tree, tree);
static bool check_instantiated_args (tree, tree, tsubst_flags_t);
static bool check_instantiated_args (tree, tree, tsubst_flags_t);
static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
                                             tree);
                                             tree);
static int type_unification_real (tree, tree, tree, const tree *,
static int type_unification_real (tree, tree, tree, const tree *,
                                  unsigned int, int, unification_kind_t, int);
                                  unsigned int, int, unification_kind_t, int);
static void note_template_header (int);
static void note_template_header (int);
static tree convert_nontype_argument_function (tree, tree);
static tree convert_nontype_argument_function (tree, tree);
static tree convert_nontype_argument (tree, tree);
static tree convert_nontype_argument (tree, tree);
static tree convert_template_argument (tree, tree, tree,
static tree convert_template_argument (tree, tree, tree,
                                       tsubst_flags_t, int, tree);
                                       tsubst_flags_t, int, tree);
static int for_each_template_parm (tree, tree_fn_t, void*,
static int for_each_template_parm (tree, tree_fn_t, void*,
                                   struct pointer_set_t*, bool);
                                   struct pointer_set_t*, bool);
static tree expand_template_argument_pack (tree);
static tree expand_template_argument_pack (tree);
static tree build_template_parm_index (int, int, int, tree, tree);
static tree build_template_parm_index (int, int, int, tree, tree);
static bool inline_needs_template_parms (tree);
static bool inline_needs_template_parms (tree);
static void push_inline_template_parms_recursive (tree, int);
static void push_inline_template_parms_recursive (tree, int);
static tree retrieve_local_specialization (tree);
static tree retrieve_local_specialization (tree);
static void register_local_specialization (tree, tree);
static void register_local_specialization (tree, tree);
static hashval_t hash_specialization (const void *p);
static hashval_t hash_specialization (const void *p);
static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
static int mark_template_parm (tree, void *);
static int mark_template_parm (tree, void *);
static int template_parm_this_level_p (tree, void *);
static int template_parm_this_level_p (tree, void *);
static tree tsubst_friend_function (tree, tree);
static tree tsubst_friend_function (tree, tree);
static tree tsubst_friend_class (tree, tree);
static tree tsubst_friend_class (tree, tree);
static int can_complete_type_without_circularity (tree);
static int can_complete_type_without_circularity (tree);
static tree get_bindings (tree, tree, tree, bool);
static tree get_bindings (tree, tree, tree, bool);
static int template_decl_level (tree);
static int template_decl_level (tree);
static int check_cv_quals_for_unify (int, tree, tree);
static int check_cv_quals_for_unify (int, tree, tree);
static void template_parm_level_and_index (tree, int*, int*);
static void template_parm_level_and_index (tree, int*, int*);
static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
static void regenerate_decl_from_template (tree, tree);
static void regenerate_decl_from_template (tree, tree);
static tree most_specialized_class (tree, tree);
static tree most_specialized_class (tree, tree);
static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
static bool check_specialization_scope (void);
static bool check_specialization_scope (void);
static tree process_partial_specialization (tree);
static tree process_partial_specialization (tree);
static void set_current_access_from_decl (tree);
static void set_current_access_from_decl (tree);
static tree get_template_base (tree, tree, tree, tree);
static tree get_template_base (tree, tree, tree, tree);
static tree try_class_unification (tree, tree, tree, tree);
static tree try_class_unification (tree, tree, tree, tree);
static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
                                           tree, tree);
                                           tree, tree);
static bool template_template_parm_bindings_ok_p (tree, tree);
static bool template_template_parm_bindings_ok_p (tree, tree);
static int template_args_equal (tree, tree);
static int template_args_equal (tree, tree);
static void tsubst_default_arguments (tree);
static void tsubst_default_arguments (tree);
static tree for_each_template_parm_r (tree *, int *, void *);
static tree for_each_template_parm_r (tree *, int *, void *);
static tree copy_default_args_to_explicit_spec_1 (tree, tree);
static tree copy_default_args_to_explicit_spec_1 (tree, tree);
static void copy_default_args_to_explicit_spec (tree);
static void copy_default_args_to_explicit_spec (tree);
static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
static int eq_local_specializations (const void *, const void *);
static int eq_local_specializations (const void *, const void *);
static bool dependent_template_arg_p (tree);
static bool dependent_template_arg_p (tree);
static bool any_template_arguments_need_structural_equality_p (tree);
static bool any_template_arguments_need_structural_equality_p (tree);
static bool dependent_type_p_r (tree);
static bool dependent_type_p_r (tree);
static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
static tree tsubst_decl (tree, tree, tsubst_flags_t);
static tree tsubst_decl (tree, tree, tsubst_flags_t);
static void perform_typedefs_access_check (tree tmpl, tree targs);
static void perform_typedefs_access_check (tree tmpl, tree targs);
static void append_type_to_template_for_access_check_1 (tree, tree, tree,
static void append_type_to_template_for_access_check_1 (tree, tree, tree,
                                                        location_t);
                                                        location_t);
static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
static tree listify (tree);
static tree listify (tree);
static tree listify_autos (tree, tree);
static tree listify_autos (tree, tree);
 
 
/* Make the current scope suitable for access checking when we are
/* Make the current scope suitable for access checking when we are
   processing T.  T can be FUNCTION_DECL for instantiated function
   processing T.  T can be FUNCTION_DECL for instantiated function
   template, or VAR_DECL for static member variable (need by
   template, or VAR_DECL for static member variable (need by
   instantiate_decl).  */
   instantiate_decl).  */
 
 
static void
static void
push_access_scope (tree t)
push_access_scope (tree t)
{
{
  gcc_assert (TREE_CODE (t) == FUNCTION_DECL
  gcc_assert (TREE_CODE (t) == FUNCTION_DECL
              || TREE_CODE (t) == VAR_DECL);
              || TREE_CODE (t) == VAR_DECL);
 
 
  if (DECL_FRIEND_CONTEXT (t))
  if (DECL_FRIEND_CONTEXT (t))
    push_nested_class (DECL_FRIEND_CONTEXT (t));
    push_nested_class (DECL_FRIEND_CONTEXT (t));
  else if (DECL_CLASS_SCOPE_P (t))
  else if (DECL_CLASS_SCOPE_P (t))
    push_nested_class (DECL_CONTEXT (t));
    push_nested_class (DECL_CONTEXT (t));
  else
  else
    push_to_top_level ();
    push_to_top_level ();
 
 
  if (TREE_CODE (t) == FUNCTION_DECL)
  if (TREE_CODE (t) == FUNCTION_DECL)
    {
    {
      saved_access_scope = tree_cons
      saved_access_scope = tree_cons
        (NULL_TREE, current_function_decl, saved_access_scope);
        (NULL_TREE, current_function_decl, saved_access_scope);
      current_function_decl = t;
      current_function_decl = t;
    }
    }
}
}
 
 
/* Restore the scope set up by push_access_scope.  T is the node we
/* Restore the scope set up by push_access_scope.  T is the node we
   are processing.  */
   are processing.  */
 
 
static void
static void
pop_access_scope (tree t)
pop_access_scope (tree t)
{
{
  if (TREE_CODE (t) == FUNCTION_DECL)
  if (TREE_CODE (t) == FUNCTION_DECL)
    {
    {
      current_function_decl = TREE_VALUE (saved_access_scope);
      current_function_decl = TREE_VALUE (saved_access_scope);
      saved_access_scope = TREE_CHAIN (saved_access_scope);
      saved_access_scope = TREE_CHAIN (saved_access_scope);
    }
    }
 
 
  if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
  if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
    pop_nested_class ();
    pop_nested_class ();
  else
  else
    pop_from_top_level ();
    pop_from_top_level ();
}
}
 
 
/* Do any processing required when DECL (a member template
/* Do any processing required when DECL (a member template
   declaration) is finished.  Returns the TEMPLATE_DECL corresponding
   declaration) is finished.  Returns the TEMPLATE_DECL corresponding
   to DECL, unless it is a specialization, in which case the DECL
   to DECL, unless it is a specialization, in which case the DECL
   itself is returned.  */
   itself is returned.  */
 
 
tree
tree
finish_member_template_decl (tree decl)
finish_member_template_decl (tree decl)
{
{
  if (decl == error_mark_node)
  if (decl == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  gcc_assert (DECL_P (decl));
  gcc_assert (DECL_P (decl));
 
 
  if (TREE_CODE (decl) == TYPE_DECL)
  if (TREE_CODE (decl) == TYPE_DECL)
    {
    {
      tree type;
      tree type;
 
 
      type = TREE_TYPE (decl);
      type = TREE_TYPE (decl);
      if (type == error_mark_node)
      if (type == error_mark_node)
        return error_mark_node;
        return error_mark_node;
      if (MAYBE_CLASS_TYPE_P (type)
      if (MAYBE_CLASS_TYPE_P (type)
          && CLASSTYPE_TEMPLATE_INFO (type)
          && CLASSTYPE_TEMPLATE_INFO (type)
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
        {
        {
          tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
          tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
          check_member_template (tmpl);
          check_member_template (tmpl);
          return tmpl;
          return tmpl;
        }
        }
      return NULL_TREE;
      return NULL_TREE;
    }
    }
  else if (TREE_CODE (decl) == FIELD_DECL)
  else if (TREE_CODE (decl) == FIELD_DECL)
    error ("data member %qD cannot be a member template", decl);
    error ("data member %qD cannot be a member template", decl);
  else if (DECL_TEMPLATE_INFO (decl))
  else if (DECL_TEMPLATE_INFO (decl))
    {
    {
      if (!DECL_TEMPLATE_SPECIALIZATION (decl))
      if (!DECL_TEMPLATE_SPECIALIZATION (decl))
        {
        {
          check_member_template (DECL_TI_TEMPLATE (decl));
          check_member_template (DECL_TI_TEMPLATE (decl));
          return DECL_TI_TEMPLATE (decl);
          return DECL_TI_TEMPLATE (decl);
        }
        }
      else
      else
        return decl;
        return decl;
    }
    }
  else
  else
    error ("invalid member template declaration %qD", decl);
    error ("invalid member template declaration %qD", decl);
 
 
  return error_mark_node;
  return error_mark_node;
}
}
 
 
/* Create a template info node.  */
/* Create a template info node.  */
 
 
tree
tree
build_template_info (tree template_decl, tree template_args)
build_template_info (tree template_decl, tree template_args)
{
{
  tree result = make_node (TEMPLATE_INFO);
  tree result = make_node (TEMPLATE_INFO);
  TI_TEMPLATE (result) = template_decl;
  TI_TEMPLATE (result) = template_decl;
  TI_ARGS (result) = template_args;
  TI_ARGS (result) = template_args;
  return result;
  return result;
}
}
 
 
/* Return the template info node corresponding to T, whatever T is.  */
/* Return the template info node corresponding to T, whatever T is.  */
 
 
tree
tree
get_template_info (const_tree t)
get_template_info (const_tree t)
{
{
  tree tinfo = NULL_TREE;
  tree tinfo = NULL_TREE;
 
 
  if (!t || t == error_mark_node)
  if (!t || t == error_mark_node)
    return NULL;
    return NULL;
 
 
  if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
  if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
    tinfo = DECL_TEMPLATE_INFO (t);
    tinfo = DECL_TEMPLATE_INFO (t);
 
 
  if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
  if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
    t = TREE_TYPE (t);
    t = TREE_TYPE (t);
 
 
  if (TAGGED_TYPE_P (t))
  if (TAGGED_TYPE_P (t))
    tinfo = TYPE_TEMPLATE_INFO (t);
    tinfo = TYPE_TEMPLATE_INFO (t);
  else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
  else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
    tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
 
 
  return tinfo;
  return tinfo;
}
}
 
 
/* Returns the template nesting level of the indicated class TYPE.
/* Returns the template nesting level of the indicated class TYPE.
 
 
   For example, in:
   For example, in:
     template <class T>
     template <class T>
     struct A
     struct A
     {
     {
       template <class U>
       template <class U>
       struct B {};
       struct B {};
     };
     };
 
 
   A<T>::B<U> has depth two, while A<T> has depth one.
   A<T>::B<U> has depth two, while A<T> has depth one.
   Both A<T>::B<int> and A<int>::B<U> have depth one, if
   Both A<T>::B<int> and A<int>::B<U> have depth one, if
   they are instantiations, not specializations.
   they are instantiations, not specializations.
 
 
   This function is guaranteed to return 0 if passed NULL_TREE so
   This function is guaranteed to return 0 if passed NULL_TREE so
   that, for example, `template_class_depth (current_class_type)' is
   that, for example, `template_class_depth (current_class_type)' is
   always safe.  */
   always safe.  */
 
 
int
int
template_class_depth (tree type)
template_class_depth (tree type)
{
{
  int depth;
  int depth;
 
 
  for (depth = 0;
  for (depth = 0;
       type && TREE_CODE (type) != NAMESPACE_DECL;
       type && TREE_CODE (type) != NAMESPACE_DECL;
       type = (TREE_CODE (type) == FUNCTION_DECL)
       type = (TREE_CODE (type) == FUNCTION_DECL)
         ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
         ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
    {
    {
      tree tinfo = get_template_info (type);
      tree tinfo = get_template_info (type);
 
 
      if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
      if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
          && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
          && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
        ++depth;
        ++depth;
    }
    }
 
 
  return depth;
  return depth;
}
}
 
 
/* Subroutine of maybe_begin_member_template_processing.
/* Subroutine of maybe_begin_member_template_processing.
   Returns true if processing DECL needs us to push template parms.  */
   Returns true if processing DECL needs us to push template parms.  */
 
 
static bool
static bool
inline_needs_template_parms (tree decl)
inline_needs_template_parms (tree decl)
{
{
  if (! DECL_TEMPLATE_INFO (decl))
  if (! DECL_TEMPLATE_INFO (decl))
    return false;
    return false;
 
 
  return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
  return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
          > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
          > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
}
}
 
 
/* Subroutine of maybe_begin_member_template_processing.
/* Subroutine of maybe_begin_member_template_processing.
   Push the template parms in PARMS, starting from LEVELS steps into the
   Push the template parms in PARMS, starting from LEVELS steps into the
   chain, and ending at the beginning, since template parms are listed
   chain, and ending at the beginning, since template parms are listed
   innermost first.  */
   innermost first.  */
 
 
static void
static void
push_inline_template_parms_recursive (tree parmlist, int levels)
push_inline_template_parms_recursive (tree parmlist, int levels)
{
{
  tree parms = TREE_VALUE (parmlist);
  tree parms = TREE_VALUE (parmlist);
  int i;
  int i;
 
 
  if (levels > 1)
  if (levels > 1)
    push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
    push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
 
 
  ++processing_template_decl;
  ++processing_template_decl;
  current_template_parms
  current_template_parms
    = tree_cons (size_int (processing_template_decl),
    = tree_cons (size_int (processing_template_decl),
                 parms, current_template_parms);
                 parms, current_template_parms);
  TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
  TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
 
 
  begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
  begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
               NULL);
               NULL);
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
    {
    {
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
 
 
      if (parm == error_mark_node)
      if (parm == error_mark_node)
        continue;
        continue;
 
 
      gcc_assert (DECL_P (parm));
      gcc_assert (DECL_P (parm));
 
 
      switch (TREE_CODE (parm))
      switch (TREE_CODE (parm))
        {
        {
        case TYPE_DECL:
        case TYPE_DECL:
        case TEMPLATE_DECL:
        case TEMPLATE_DECL:
          pushdecl (parm);
          pushdecl (parm);
          break;
          break;
 
 
        case PARM_DECL:
        case PARM_DECL:
          {
          {
            /* Make a CONST_DECL as is done in process_template_parm.
            /* Make a CONST_DECL as is done in process_template_parm.
               It is ugly that we recreate this here; the original
               It is ugly that we recreate this here; the original
               version built in process_template_parm is no longer
               version built in process_template_parm is no longer
               available.  */
               available.  */
            tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
            tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
                                    CONST_DECL, DECL_NAME (parm),
                                    CONST_DECL, DECL_NAME (parm),
                                    TREE_TYPE (parm));
                                    TREE_TYPE (parm));
            DECL_ARTIFICIAL (decl) = 1;
            DECL_ARTIFICIAL (decl) = 1;
            TREE_CONSTANT (decl) = 1;
            TREE_CONSTANT (decl) = 1;
            TREE_READONLY (decl) = 1;
            TREE_READONLY (decl) = 1;
            DECL_INITIAL (decl) = DECL_INITIAL (parm);
            DECL_INITIAL (decl) = DECL_INITIAL (parm);
            SET_DECL_TEMPLATE_PARM_P (decl);
            SET_DECL_TEMPLATE_PARM_P (decl);
            pushdecl (decl);
            pushdecl (decl);
          }
          }
          break;
          break;
 
 
        default:
        default:
          gcc_unreachable ();
          gcc_unreachable ();
        }
        }
    }
    }
}
}
 
 
/* Restore the template parameter context for a member template or
/* Restore the template parameter context for a member template or
   a friend template defined in a class definition.  */
   a friend template defined in a class definition.  */
 
 
void
void
maybe_begin_member_template_processing (tree decl)
maybe_begin_member_template_processing (tree decl)
{
{
  tree parms;
  tree parms;
  int levels = 0;
  int levels = 0;
 
 
  if (inline_needs_template_parms (decl))
  if (inline_needs_template_parms (decl))
    {
    {
      parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
      parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
      levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
      levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
 
 
      if (DECL_TEMPLATE_SPECIALIZATION (decl))
      if (DECL_TEMPLATE_SPECIALIZATION (decl))
        {
        {
          --levels;
          --levels;
          parms = TREE_CHAIN (parms);
          parms = TREE_CHAIN (parms);
        }
        }
 
 
      push_inline_template_parms_recursive (parms, levels);
      push_inline_template_parms_recursive (parms, levels);
    }
    }
 
 
  /* Remember how many levels of template parameters we pushed so that
  /* Remember how many levels of template parameters we pushed so that
     we can pop them later.  */
     we can pop them later.  */
  VEC_safe_push (int, heap, inline_parm_levels, levels);
  VEC_safe_push (int, heap, inline_parm_levels, levels);
}
}
 
 
/* Undo the effects of maybe_begin_member_template_processing.  */
/* Undo the effects of maybe_begin_member_template_processing.  */
 
 
void
void
maybe_end_member_template_processing (void)
maybe_end_member_template_processing (void)
{
{
  int i;
  int i;
  int last;
  int last;
 
 
  if (VEC_length (int, inline_parm_levels) == 0)
  if (VEC_length (int, inline_parm_levels) == 0)
    return;
    return;
 
 
  last = VEC_pop (int, inline_parm_levels);
  last = VEC_pop (int, inline_parm_levels);
  for (i = 0; i < last; ++i)
  for (i = 0; i < last; ++i)
    {
    {
      --processing_template_decl;
      --processing_template_decl;
      current_template_parms = TREE_CHAIN (current_template_parms);
      current_template_parms = TREE_CHAIN (current_template_parms);
      poplevel (0, 0, 0);
      poplevel (0, 0, 0);
    }
    }
}
}
 
 
/* Return a new template argument vector which contains all of ARGS,
/* Return a new template argument vector which contains all of ARGS,
   but has as its innermost set of arguments the EXTRA_ARGS.  */
   but has as its innermost set of arguments the EXTRA_ARGS.  */
 
 
static tree
static tree
add_to_template_args (tree args, tree extra_args)
add_to_template_args (tree args, tree extra_args)
{
{
  tree new_args;
  tree new_args;
  int extra_depth;
  int extra_depth;
  int i;
  int i;
  int j;
  int j;
 
 
  if (args == NULL_TREE)
  if (args == NULL_TREE)
    return extra_args;
    return extra_args;
 
 
  extra_depth = TMPL_ARGS_DEPTH (extra_args);
  extra_depth = TMPL_ARGS_DEPTH (extra_args);
  new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
  new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
 
 
  for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
  for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
 
 
  for (j = 1; j <= extra_depth; ++j, ++i)
  for (j = 1; j <= extra_depth; ++j, ++i)
    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
 
 
  return new_args;
  return new_args;
}
}
 
 
/* Like add_to_template_args, but only the outermost ARGS are added to
/* Like add_to_template_args, but only the outermost ARGS are added to
   the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
   the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
   (EXTRA_ARGS) levels are added.  This function is used to combine
   (EXTRA_ARGS) levels are added.  This function is used to combine
   the template arguments from a partial instantiation with the
   the template arguments from a partial instantiation with the
   template arguments used to attain the full instantiation from the
   template arguments used to attain the full instantiation from the
   partial instantiation.  */
   partial instantiation.  */
 
 
static tree
static tree
add_outermost_template_args (tree args, tree extra_args)
add_outermost_template_args (tree args, tree extra_args)
{
{
  tree new_args;
  tree new_args;
 
 
  /* If there are more levels of EXTRA_ARGS than there are ARGS,
  /* If there are more levels of EXTRA_ARGS than there are ARGS,
     something very fishy is going on.  */
     something very fishy is going on.  */
  gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
  gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
 
 
  /* If *all* the new arguments will be the EXTRA_ARGS, just return
  /* If *all* the new arguments will be the EXTRA_ARGS, just return
     them.  */
     them.  */
  if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
  if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
    return extra_args;
    return extra_args;
 
 
  /* For the moment, we make ARGS look like it contains fewer levels.  */
  /* For the moment, we make ARGS look like it contains fewer levels.  */
  TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
  TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
 
 
  new_args = add_to_template_args (args, extra_args);
  new_args = add_to_template_args (args, extra_args);
 
 
  /* Now, we restore ARGS to its full dimensions.  */
  /* Now, we restore ARGS to its full dimensions.  */
  TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
  TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
 
 
  return new_args;
  return new_args;
}
}
 
 
/* Return the N levels of innermost template arguments from the ARGS.  */
/* Return the N levels of innermost template arguments from the ARGS.  */
 
 
tree
tree
get_innermost_template_args (tree args, int n)
get_innermost_template_args (tree args, int n)
{
{
  tree new_args;
  tree new_args;
  int extra_levels;
  int extra_levels;
  int i;
  int i;
 
 
  gcc_assert (n >= 0);
  gcc_assert (n >= 0);
 
 
  /* If N is 1, just return the innermost set of template arguments.  */
  /* If N is 1, just return the innermost set of template arguments.  */
  if (n == 1)
  if (n == 1)
    return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
    return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
 
 
  /* If we're not removing anything, just return the arguments we were
  /* If we're not removing anything, just return the arguments we were
     given.  */
     given.  */
  extra_levels = TMPL_ARGS_DEPTH (args) - n;
  extra_levels = TMPL_ARGS_DEPTH (args) - n;
  gcc_assert (extra_levels >= 0);
  gcc_assert (extra_levels >= 0);
  if (extra_levels == 0)
  if (extra_levels == 0)
    return args;
    return args;
 
 
  /* Make a new set of arguments, not containing the outer arguments.  */
  /* Make a new set of arguments, not containing the outer arguments.  */
  new_args = make_tree_vec (n);
  new_args = make_tree_vec (n);
  for (i = 1; i <= n; ++i)
  for (i = 1; i <= n; ++i)
    SET_TMPL_ARGS_LEVEL (new_args, i,
    SET_TMPL_ARGS_LEVEL (new_args, i,
                         TMPL_ARGS_LEVEL (args, i + extra_levels));
                         TMPL_ARGS_LEVEL (args, i + extra_levels));
 
 
  return new_args;
  return new_args;
}
}
 
 
/* The inverse of get_innermost_template_args: Return all but the innermost
/* The inverse of get_innermost_template_args: Return all but the innermost
   EXTRA_LEVELS levels of template arguments from the ARGS.  */
   EXTRA_LEVELS levels of template arguments from the ARGS.  */
 
 
static tree
static tree
strip_innermost_template_args (tree args, int extra_levels)
strip_innermost_template_args (tree args, int extra_levels)
{
{
  tree new_args;
  tree new_args;
  int n = TMPL_ARGS_DEPTH (args) - extra_levels;
  int n = TMPL_ARGS_DEPTH (args) - extra_levels;
  int i;
  int i;
 
 
  gcc_assert (n >= 0);
  gcc_assert (n >= 0);
 
 
  /* If N is 1, just return the outermost set of template arguments.  */
  /* If N is 1, just return the outermost set of template arguments.  */
  if (n == 1)
  if (n == 1)
    return TMPL_ARGS_LEVEL (args, 1);
    return TMPL_ARGS_LEVEL (args, 1);
 
 
  /* If we're not removing anything, just return the arguments we were
  /* If we're not removing anything, just return the arguments we were
     given.  */
     given.  */
  gcc_assert (extra_levels >= 0);
  gcc_assert (extra_levels >= 0);
  if (extra_levels == 0)
  if (extra_levels == 0)
    return args;
    return args;
 
 
  /* Make a new set of arguments, not containing the inner arguments.  */
  /* Make a new set of arguments, not containing the inner arguments.  */
  new_args = make_tree_vec (n);
  new_args = make_tree_vec (n);
  for (i = 1; i <= n; ++i)
  for (i = 1; i <= n; ++i)
    SET_TMPL_ARGS_LEVEL (new_args, i,
    SET_TMPL_ARGS_LEVEL (new_args, i,
                         TMPL_ARGS_LEVEL (args, i));
                         TMPL_ARGS_LEVEL (args, i));
 
 
  return new_args;
  return new_args;
}
}
 
 
/* We've got a template header coming up; push to a new level for storing
/* We've got a template header coming up; push to a new level for storing
   the parms.  */
   the parms.  */
 
 
void
void
begin_template_parm_list (void)
begin_template_parm_list (void)
{
{
  /* We use a non-tag-transparent scope here, which causes pushtag to
  /* We use a non-tag-transparent scope here, which causes pushtag to
     put tags in this scope, rather than in the enclosing class or
     put tags in this scope, rather than in the enclosing class or
     namespace scope.  This is the right thing, since we want
     namespace scope.  This is the right thing, since we want
     TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     global template class, push_template_decl handles putting the
     global template class, push_template_decl handles putting the
     TEMPLATE_DECL into top-level scope.  For a nested template class,
     TEMPLATE_DECL into top-level scope.  For a nested template class,
     e.g.:
     e.g.:
 
 
       template <class T> struct S1 {
       template <class T> struct S1 {
         template <class T> struct S2 {};
         template <class T> struct S2 {};
       };
       };
 
 
     pushtag contains special code to call pushdecl_with_scope on the
     pushtag contains special code to call pushdecl_with_scope on the
     TEMPLATE_DECL for S2.  */
     TEMPLATE_DECL for S2.  */
  begin_scope (sk_template_parms, NULL);
  begin_scope (sk_template_parms, NULL);
  ++processing_template_decl;
  ++processing_template_decl;
  ++processing_template_parmlist;
  ++processing_template_parmlist;
  note_template_header (0);
  note_template_header (0);
}
}
 
 
/* This routine is called when a specialization is declared.  If it is
/* This routine is called when a specialization is declared.  If it is
   invalid to declare a specialization here, an error is reported and
   invalid to declare a specialization here, an error is reported and
   false is returned, otherwise this routine will return true.  */
   false is returned, otherwise this routine will return true.  */
 
 
static bool
static bool
check_specialization_scope (void)
check_specialization_scope (void)
{
{
  tree scope = current_scope ();
  tree scope = current_scope ();
 
 
  /* [temp.expl.spec]
  /* [temp.expl.spec]
 
 
     An explicit specialization shall be declared in the namespace of
     An explicit specialization shall be declared in the namespace of
     which the template is a member, or, for member templates, in the
     which the template is a member, or, for member templates, in the
     namespace of which the enclosing class or enclosing class
     namespace of which the enclosing class or enclosing class
     template is a member.  An explicit specialization of a member
     template is a member.  An explicit specialization of a member
     function, member class or static data member of a class template
     function, member class or static data member of a class template
     shall be declared in the namespace of which the class template
     shall be declared in the namespace of which the class template
     is a member.  */
     is a member.  */
  if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
  if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
    {
    {
      error ("explicit specialization in non-namespace scope %qD", scope);
      error ("explicit specialization in non-namespace scope %qD", scope);
      return false;
      return false;
    }
    }
 
 
  /* [temp.expl.spec]
  /* [temp.expl.spec]
 
 
     In an explicit specialization declaration for a member of a class
     In an explicit specialization declaration for a member of a class
     template or a member template that appears in namespace scope,
     template or a member template that appears in namespace scope,
     the member template and some of its enclosing class templates may
     the member template and some of its enclosing class templates may
     remain unspecialized, except that the declaration shall not
     remain unspecialized, except that the declaration shall not
     explicitly specialize a class member template if its enclosing
     explicitly specialize a class member template if its enclosing
     class templates are not explicitly specialized as well.  */
     class templates are not explicitly specialized as well.  */
  if (current_template_parms)
  if (current_template_parms)
    {
    {
      error ("enclosing class templates are not explicitly specialized");
      error ("enclosing class templates are not explicitly specialized");
      return false;
      return false;
    }
    }
 
 
  return true;
  return true;
}
}
 
 
/* We've just seen template <>.  */
/* We've just seen template <>.  */
 
 
bool
bool
begin_specialization (void)
begin_specialization (void)
{
{
  begin_scope (sk_template_spec, NULL);
  begin_scope (sk_template_spec, NULL);
  note_template_header (1);
  note_template_header (1);
  return check_specialization_scope ();
  return check_specialization_scope ();
}
}
 
 
/* Called at then end of processing a declaration preceded by
/* Called at then end of processing a declaration preceded by
   template<>.  */
   template<>.  */
 
 
void
void
end_specialization (void)
end_specialization (void)
{
{
  finish_scope ();
  finish_scope ();
  reset_specialization ();
  reset_specialization ();
}
}
 
 
/* Any template <>'s that we have seen thus far are not referring to a
/* Any template <>'s that we have seen thus far are not referring to a
   function specialization.  */
   function specialization.  */
 
 
void
void
reset_specialization (void)
reset_specialization (void)
{
{
  processing_specialization = 0;
  processing_specialization = 0;
  template_header_count = 0;
  template_header_count = 0;
}
}
 
 
/* We've just seen a template header.  If SPECIALIZATION is nonzero,
/* We've just seen a template header.  If SPECIALIZATION is nonzero,
   it was of the form template <>.  */
   it was of the form template <>.  */
 
 
static void
static void
note_template_header (int specialization)
note_template_header (int specialization)
{
{
  processing_specialization = specialization;
  processing_specialization = specialization;
  template_header_count++;
  template_header_count++;
}
}
 
 
/* We're beginning an explicit instantiation.  */
/* We're beginning an explicit instantiation.  */
 
 
void
void
begin_explicit_instantiation (void)
begin_explicit_instantiation (void)
{
{
  gcc_assert (!processing_explicit_instantiation);
  gcc_assert (!processing_explicit_instantiation);
  processing_explicit_instantiation = true;
  processing_explicit_instantiation = true;
}
}
 
 
 
 
void
void
end_explicit_instantiation (void)
end_explicit_instantiation (void)
{
{
  gcc_assert (processing_explicit_instantiation);
  gcc_assert (processing_explicit_instantiation);
  processing_explicit_instantiation = false;
  processing_explicit_instantiation = false;
}
}
 
 
/* An explicit specialization or partial specialization TMPL is being
/* An explicit specialization or partial specialization TMPL is being
   declared.  Check that the namespace in which the specialization is
   declared.  Check that the namespace in which the specialization is
   occurring is permissible.  Returns false iff it is invalid to
   occurring is permissible.  Returns false iff it is invalid to
   specialize TMPL in the current namespace.  */
   specialize TMPL in the current namespace.  */
 
 
static bool
static bool
check_specialization_namespace (tree tmpl)
check_specialization_namespace (tree tmpl)
{
{
  tree tpl_ns = decl_namespace_context (tmpl);
  tree tpl_ns = decl_namespace_context (tmpl);
 
 
  /* [tmpl.expl.spec]
  /* [tmpl.expl.spec]
 
 
     An explicit specialization shall be declared in the namespace of
     An explicit specialization shall be declared in the namespace of
     which the template is a member, or, for member templates, in the
     which the template is a member, or, for member templates, in the
     namespace of which the enclosing class or enclosing class
     namespace of which the enclosing class or enclosing class
     template is a member.  An explicit specialization of a member
     template is a member.  An explicit specialization of a member
     function, member class or static data member of a class template
     function, member class or static data member of a class template
     shall be declared in the namespace of which the class template is
     shall be declared in the namespace of which the class template is
     a member.  */
     a member.  */
  if (current_scope() != DECL_CONTEXT (tmpl)
  if (current_scope() != DECL_CONTEXT (tmpl)
      && !at_namespace_scope_p ())
      && !at_namespace_scope_p ())
    {
    {
      error ("specialization of %qD must appear at namespace scope", tmpl);
      error ("specialization of %qD must appear at namespace scope", tmpl);
      return false;
      return false;
    }
    }
  if (is_associated_namespace (current_namespace, tpl_ns))
  if (is_associated_namespace (current_namespace, tpl_ns))
    /* Same or super-using namespace.  */
    /* Same or super-using namespace.  */
    return true;
    return true;
  else
  else
    {
    {
      permerror (input_location, "specialization of %qD in different namespace", tmpl);
      permerror (input_location, "specialization of %qD in different namespace", tmpl);
      permerror (input_location, "  from definition of %q+#D", tmpl);
      permerror (input_location, "  from definition of %q+#D", tmpl);
      return false;
      return false;
    }
    }
}
}
 
 
/* SPEC is an explicit instantiation.  Check that it is valid to
/* SPEC is an explicit instantiation.  Check that it is valid to
   perform this explicit instantiation in the current namespace.  */
   perform this explicit instantiation in the current namespace.  */
 
 
static void
static void
check_explicit_instantiation_namespace (tree spec)
check_explicit_instantiation_namespace (tree spec)
{
{
  tree ns;
  tree ns;
 
 
  /* DR 275: An explicit instantiation shall appear in an enclosing
  /* DR 275: An explicit instantiation shall appear in an enclosing
     namespace of its template.  */
     namespace of its template.  */
  ns = decl_namespace_context (spec);
  ns = decl_namespace_context (spec);
  if (!is_ancestor (current_namespace, ns))
  if (!is_ancestor (current_namespace, ns))
    permerror (input_location, "explicit instantiation of %qD in namespace %qD "
    permerror (input_location, "explicit instantiation of %qD in namespace %qD "
               "(which does not enclose namespace %qD)",
               "(which does not enclose namespace %qD)",
               spec, current_namespace, ns);
               spec, current_namespace, ns);
}
}
 
 
/* The TYPE is being declared.  If it is a template type, that means it
/* The TYPE is being declared.  If it is a template type, that means it
   is a partial specialization.  Do appropriate error-checking.  */
   is a partial specialization.  Do appropriate error-checking.  */
 
 
tree
tree
maybe_process_partial_specialization (tree type)
maybe_process_partial_specialization (tree type)
{
{
  tree context;
  tree context;
 
 
  if (type == error_mark_node)
  if (type == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    {
    {
      error ("name of class shadows template template parameter %qD",
      error ("name of class shadows template template parameter %qD",
             TYPE_NAME (type));
             TYPE_NAME (type));
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  context = TYPE_CONTEXT (type);
  context = TYPE_CONTEXT (type);
 
 
  if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
  if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
    {
    {
      /* This is for ordinary explicit specialization and partial
      /* This is for ordinary explicit specialization and partial
         specialization of a template class such as:
         specialization of a template class such as:
 
 
           template <> class C<int>;
           template <> class C<int>;
 
 
         or:
         or:
 
 
           template <class T> class C<T*>;
           template <class T> class C<T*>;
 
 
         Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
         Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
 
 
      if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
      if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
          && !COMPLETE_TYPE_P (type))
          && !COMPLETE_TYPE_P (type))
        {
        {
          check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
          check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
          SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
          SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
          if (processing_template_decl)
          if (processing_template_decl)
            {
            {
              if (push_template_decl (TYPE_MAIN_DECL (type))
              if (push_template_decl (TYPE_MAIN_DECL (type))
                  == error_mark_node)
                  == error_mark_node)
                return error_mark_node;
                return error_mark_node;
            }
            }
        }
        }
      else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
      else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
        error ("specialization of %qT after instantiation", type);
        error ("specialization of %qT after instantiation", type);
    }
    }
  else if (CLASS_TYPE_P (type)
  else if (CLASS_TYPE_P (type)
           && !CLASSTYPE_USE_TEMPLATE (type)
           && !CLASSTYPE_USE_TEMPLATE (type)
           && CLASSTYPE_TEMPLATE_INFO (type)
           && CLASSTYPE_TEMPLATE_INFO (type)
           && context && CLASS_TYPE_P (context)
           && context && CLASS_TYPE_P (context)
           && CLASSTYPE_TEMPLATE_INFO (context))
           && CLASSTYPE_TEMPLATE_INFO (context))
    {
    {
      /* This is for an explicit specialization of member class
      /* This is for an explicit specialization of member class
         template according to [temp.expl.spec/18]:
         template according to [temp.expl.spec/18]:
 
 
           template <> template <class U> class C<int>::D;
           template <> template <class U> class C<int>::D;
 
 
         The context `C<int>' must be an implicit instantiation.
         The context `C<int>' must be an implicit instantiation.
         Otherwise this is just a member class template declared
         Otherwise this is just a member class template declared
         earlier like:
         earlier like:
 
 
           template <> class C<int> { template <class U> class D; };
           template <> class C<int> { template <class U> class D; };
           template <> template <class U> class C<int>::D;
           template <> template <class U> class C<int>::D;
 
 
         In the first case, `C<int>::D' is a specialization of `C<T>::D'
         In the first case, `C<int>::D' is a specialization of `C<T>::D'
         while in the second case, `C<int>::D' is a primary template
         while in the second case, `C<int>::D' is a primary template
         and `C<T>::D' may not exist.  */
         and `C<T>::D' may not exist.  */
 
 
      if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
      if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
          && !COMPLETE_TYPE_P (type))
          && !COMPLETE_TYPE_P (type))
        {
        {
          tree t;
          tree t;
          tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
          tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
 
 
          if (current_namespace
          if (current_namespace
              != decl_namespace_context (tmpl))
              != decl_namespace_context (tmpl))
            {
            {
              permerror (input_location, "specializing %q#T in different namespace", type);
              permerror (input_location, "specializing %q#T in different namespace", type);
              permerror (input_location, "  from definition of %q+#D", tmpl);
              permerror (input_location, "  from definition of %q+#D", tmpl);
            }
            }
 
 
          /* Check for invalid specialization after instantiation:
          /* Check for invalid specialization after instantiation:
 
 
               template <> template <> class C<int>::D<int>;
               template <> template <> class C<int>::D<int>;
               template <> template <class U> class C<int>::D;  */
               template <> template <class U> class C<int>::D;  */
 
 
          for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
          for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
               t; t = TREE_CHAIN (t))
               t; t = TREE_CHAIN (t))
            {
            {
              tree inst = TREE_VALUE (t);
              tree inst = TREE_VALUE (t);
              if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
              if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
                {
                {
                  /* We already have a full specialization of this partial
                  /* We already have a full specialization of this partial
                     instantiation.  Reassign it to the new member
                     instantiation.  Reassign it to the new member
                     specialization template.  */
                     specialization template.  */
                  spec_entry elt;
                  spec_entry elt;
                  spec_entry **slot;
                  spec_entry **slot;
 
 
                  elt.tmpl = most_general_template (tmpl);
                  elt.tmpl = most_general_template (tmpl);
                  elt.args = CLASSTYPE_TI_ARGS (inst);
                  elt.args = CLASSTYPE_TI_ARGS (inst);
                  elt.spec = inst;
                  elt.spec = inst;
 
 
                  htab_remove_elt (type_specializations, &elt);
                  htab_remove_elt (type_specializations, &elt);
 
 
                  elt.tmpl = tmpl;
                  elt.tmpl = tmpl;
                  elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
                  elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
 
 
                  slot = (spec_entry **)
                  slot = (spec_entry **)
                    htab_find_slot (type_specializations, &elt, INSERT);
                    htab_find_slot (type_specializations, &elt, INSERT);
                  *slot = GGC_NEW (spec_entry);
                  *slot = GGC_NEW (spec_entry);
                  **slot = elt;
                  **slot = elt;
                }
                }
              else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
              else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
                /* But if we've had an implicit instantiation, that's a
                /* But if we've had an implicit instantiation, that's a
                   problem ([temp.expl.spec]/6).  */
                   problem ([temp.expl.spec]/6).  */
                error ("specialization %qT after instantiation %qT",
                error ("specialization %qT after instantiation %qT",
                       type, inst);
                       type, inst);
            }
            }
 
 
          /* Mark TYPE as a specialization.  And as a result, we only
          /* Mark TYPE as a specialization.  And as a result, we only
             have one level of template argument for the innermost
             have one level of template argument for the innermost
             class template.  */
             class template.  */
          SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
          SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
          CLASSTYPE_TI_ARGS (type)
          CLASSTYPE_TI_ARGS (type)
            = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
            = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
        }
        }
    }
    }
  else if (processing_specialization)
  else if (processing_specialization)
    {
    {
      error ("explicit specialization of non-template %qT", type);
      error ("explicit specialization of non-template %qT", type);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  return type;
  return type;
}
}
 
 
/* Returns nonzero if we can optimize the retrieval of specializations
/* Returns nonzero if we can optimize the retrieval of specializations
   for TMPL, a TEMPLATE_DECL.  In particular, for such a template, we
   for TMPL, a TEMPLATE_DECL.  In particular, for such a template, we
   do not use DECL_TEMPLATE_SPECIALIZATIONS at all.  */
   do not use DECL_TEMPLATE_SPECIALIZATIONS at all.  */
 
 
static inline bool
static inline bool
optimize_specialization_lookup_p (tree tmpl)
optimize_specialization_lookup_p (tree tmpl)
{
{
  return (DECL_FUNCTION_TEMPLATE_P (tmpl)
  return (DECL_FUNCTION_TEMPLATE_P (tmpl)
          && DECL_CLASS_SCOPE_P (tmpl)
          && DECL_CLASS_SCOPE_P (tmpl)
          /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
          /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
             parameter.  */
             parameter.  */
          && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
          && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
          /* The optimized lookup depends on the fact that the
          /* The optimized lookup depends on the fact that the
             template arguments for the member function template apply
             template arguments for the member function template apply
             purely to the containing class, which is not true if the
             purely to the containing class, which is not true if the
             containing class is an explicit or partial
             containing class is an explicit or partial
             specialization.  */
             specialization.  */
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
          && !DECL_MEMBER_TEMPLATE_P (tmpl)
          && !DECL_MEMBER_TEMPLATE_P (tmpl)
          && !DECL_CONV_FN_P (tmpl)
          && !DECL_CONV_FN_P (tmpl)
          /* It is possible to have a template that is not a member
          /* It is possible to have a template that is not a member
             template and is not a member of a template class:
             template and is not a member of a template class:
 
 
             template <typename T>
             template <typename T>
             struct S { friend A::f(); };
             struct S { friend A::f(); };
 
 
             Here, the friend function is a template, but the context does
             Here, the friend function is a template, but the context does
             not have template information.  The optimized lookup relies
             not have template information.  The optimized lookup relies
             on having ARGS be the template arguments for both the class
             on having ARGS be the template arguments for both the class
             and the function template.  */
             and the function template.  */
          && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
          && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
}
}
 
 
/* Retrieve the specialization (in the sense of [temp.spec] - a
/* Retrieve the specialization (in the sense of [temp.spec] - a
   specialization is either an instantiation or an explicit
   specialization is either an instantiation or an explicit
   specialization) of TMPL for the given template ARGS.  If there is
   specialization) of TMPL for the given template ARGS.  If there is
   no such specialization, return NULL_TREE.  The ARGS are a vector of
   no such specialization, return NULL_TREE.  The ARGS are a vector of
   arguments, or a vector of vectors of arguments, in the case of
   arguments, or a vector of vectors of arguments, in the case of
   templates with more than one level of parameters.
   templates with more than one level of parameters.
 
 
   If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
   If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
   then we search for a partial specialization matching ARGS.  This
   then we search for a partial specialization matching ARGS.  This
   parameter is ignored if TMPL is not a class template.  */
   parameter is ignored if TMPL is not a class template.  */
 
 
static tree
static tree
retrieve_specialization (tree tmpl, tree args, hashval_t hash)
retrieve_specialization (tree tmpl, tree args, hashval_t hash)
{
{
  if (args == error_mark_node)
  if (args == error_mark_node)
    return NULL_TREE;
    return NULL_TREE;
 
 
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
 
 
  /* There should be as many levels of arguments as there are
  /* There should be as many levels of arguments as there are
     levels of parameters.  */
     levels of parameters.  */
  gcc_assert (TMPL_ARGS_DEPTH (args)
  gcc_assert (TMPL_ARGS_DEPTH (args)
              == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
              == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
 
 
  if (optimize_specialization_lookup_p (tmpl))
  if (optimize_specialization_lookup_p (tmpl))
    {
    {
      tree class_template;
      tree class_template;
      tree class_specialization;
      tree class_specialization;
      VEC(tree,gc) *methods;
      VEC(tree,gc) *methods;
      tree fns;
      tree fns;
      int idx;
      int idx;
 
 
      /* The template arguments actually apply to the containing
      /* The template arguments actually apply to the containing
         class.  Find the class specialization with those
         class.  Find the class specialization with those
         arguments.  */
         arguments.  */
      class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
      class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
      class_specialization
      class_specialization
        = retrieve_specialization (class_template, args, 0);
        = retrieve_specialization (class_template, args, 0);
      if (!class_specialization)
      if (!class_specialization)
        return NULL_TREE;
        return NULL_TREE;
      /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
      /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
         for the specialization.  */
         for the specialization.  */
      idx = class_method_index_for_fn (class_specialization, tmpl);
      idx = class_method_index_for_fn (class_specialization, tmpl);
      if (idx == -1)
      if (idx == -1)
        return NULL_TREE;
        return NULL_TREE;
      /* Iterate through the methods with the indicated name, looking
      /* Iterate through the methods with the indicated name, looking
         for the one that has an instance of TMPL.  */
         for the one that has an instance of TMPL.  */
      methods = CLASSTYPE_METHOD_VEC (class_specialization);
      methods = CLASSTYPE_METHOD_VEC (class_specialization);
      for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
      for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
        {
        {
          tree fn = OVL_CURRENT (fns);
          tree fn = OVL_CURRENT (fns);
          if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
          if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
              /* using-declarations can add base methods to the method vec,
              /* using-declarations can add base methods to the method vec,
                 and we don't want those here.  */
                 and we don't want those here.  */
              && DECL_CONTEXT (fn) == class_specialization)
              && DECL_CONTEXT (fn) == class_specialization)
            return fn;
            return fn;
        }
        }
      return NULL_TREE;
      return NULL_TREE;
    }
    }
  else
  else
    {
    {
      spec_entry *found;
      spec_entry *found;
      spec_entry elt;
      spec_entry elt;
      htab_t specializations;
      htab_t specializations;
 
 
      elt.tmpl = tmpl;
      elt.tmpl = tmpl;
      elt.args = args;
      elt.args = args;
      elt.spec = NULL_TREE;
      elt.spec = NULL_TREE;
 
 
      if (DECL_CLASS_TEMPLATE_P (tmpl))
      if (DECL_CLASS_TEMPLATE_P (tmpl))
        specializations = type_specializations;
        specializations = type_specializations;
      else
      else
        specializations = decl_specializations;
        specializations = decl_specializations;
 
 
      if (hash == 0)
      if (hash == 0)
        hash = hash_specialization (&elt);
        hash = hash_specialization (&elt);
      found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
      found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
      if (found)
      if (found)
        return found->spec;
        return found->spec;
    }
    }
 
 
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* Like retrieve_specialization, but for local declarations.  */
/* Like retrieve_specialization, but for local declarations.  */
 
 
static tree
static tree
retrieve_local_specialization (tree tmpl)
retrieve_local_specialization (tree tmpl)
{
{
  tree spec;
  tree spec;
 
 
  if (local_specializations == NULL)
  if (local_specializations == NULL)
    return NULL_TREE;
    return NULL_TREE;
 
 
  spec = (tree) htab_find_with_hash (local_specializations, tmpl,
  spec = (tree) htab_find_with_hash (local_specializations, tmpl,
                                     htab_hash_pointer (tmpl));
                                     htab_hash_pointer (tmpl));
  return spec ? TREE_PURPOSE (spec) : NULL_TREE;
  return spec ? TREE_PURPOSE (spec) : NULL_TREE;
}
}
 
 
/* Returns nonzero iff DECL is a specialization of TMPL.  */
/* Returns nonzero iff DECL is a specialization of TMPL.  */
 
 
int
int
is_specialization_of (tree decl, tree tmpl)
is_specialization_of (tree decl, tree tmpl)
{
{
  tree t;
  tree t;
 
 
  if (TREE_CODE (decl) == FUNCTION_DECL)
  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
    {
      for (t = decl;
      for (t = decl;
           t != NULL_TREE;
           t != NULL_TREE;
           t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
           t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
        if (t == tmpl)
        if (t == tmpl)
          return 1;
          return 1;
    }
    }
  else
  else
    {
    {
      gcc_assert (TREE_CODE (decl) == TYPE_DECL);
      gcc_assert (TREE_CODE (decl) == TYPE_DECL);
 
 
      for (t = TREE_TYPE (decl);
      for (t = TREE_TYPE (decl);
           t != NULL_TREE;
           t != NULL_TREE;
           t = CLASSTYPE_USE_TEMPLATE (t)
           t = CLASSTYPE_USE_TEMPLATE (t)
             ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
             ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
        if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
        if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
          return 1;
          return 1;
    }
    }
 
 
  return 0;
  return 0;
}
}
 
 
/* Returns nonzero iff DECL is a specialization of friend declaration
/* Returns nonzero iff DECL is a specialization of friend declaration
   FRIEND_DECL according to [temp.friend].  */
   FRIEND_DECL according to [temp.friend].  */
 
 
bool
bool
is_specialization_of_friend (tree decl, tree friend_decl)
is_specialization_of_friend (tree decl, tree friend_decl)
{
{
  bool need_template = true;
  bool need_template = true;
  int template_depth;
  int template_depth;
 
 
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
              || TREE_CODE (decl) == TYPE_DECL);
              || TREE_CODE (decl) == TYPE_DECL);
 
 
  /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
  /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
     of a template class, we want to check if DECL is a specialization
     of a template class, we want to check if DECL is a specialization
     if this.  */
     if this.  */
  if (TREE_CODE (friend_decl) == FUNCTION_DECL
  if (TREE_CODE (friend_decl) == FUNCTION_DECL
      && DECL_TEMPLATE_INFO (friend_decl)
      && DECL_TEMPLATE_INFO (friend_decl)
      && !DECL_USE_TEMPLATE (friend_decl))
      && !DECL_USE_TEMPLATE (friend_decl))
    {
    {
      /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
      /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
      friend_decl = DECL_TI_TEMPLATE (friend_decl);
      friend_decl = DECL_TI_TEMPLATE (friend_decl);
      need_template = false;
      need_template = false;
    }
    }
  else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
  else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
           && !PRIMARY_TEMPLATE_P (friend_decl))
           && !PRIMARY_TEMPLATE_P (friend_decl))
    need_template = false;
    need_template = false;
 
 
  /* There is nothing to do if this is not a template friend.  */
  /* There is nothing to do if this is not a template friend.  */
  if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
  if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    return false;
    return false;
 
 
  if (is_specialization_of (decl, friend_decl))
  if (is_specialization_of (decl, friend_decl))
    return true;
    return true;
 
 
  /* [temp.friend/6]
  /* [temp.friend/6]
     A member of a class template may be declared to be a friend of a
     A member of a class template may be declared to be a friend of a
     non-template class.  In this case, the corresponding member of
     non-template class.  In this case, the corresponding member of
     every specialization of the class template is a friend of the
     every specialization of the class template is a friend of the
     class granting friendship.
     class granting friendship.
 
 
     For example, given a template friend declaration
     For example, given a template friend declaration
 
 
       template <class T> friend void A<T>::f();
       template <class T> friend void A<T>::f();
 
 
     the member function below is considered a friend
     the member function below is considered a friend
 
 
       template <> struct A<int> {
       template <> struct A<int> {
         void f();
         void f();
       };
       };
 
 
     For this type of template friend, TEMPLATE_DEPTH below will be
     For this type of template friend, TEMPLATE_DEPTH below will be
     nonzero.  To determine if DECL is a friend of FRIEND, we first
     nonzero.  To determine if DECL is a friend of FRIEND, we first
     check if the enclosing class is a specialization of another.  */
     check if the enclosing class is a specialization of another.  */
 
 
  template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
  template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
  if (template_depth
  if (template_depth
      && DECL_CLASS_SCOPE_P (decl)
      && DECL_CLASS_SCOPE_P (decl)
      && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
      && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
                               CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
                               CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
    {
    {
      /* Next, we check the members themselves.  In order to handle
      /* Next, we check the members themselves.  In order to handle
         a few tricky cases, such as when FRIEND_DECL's are
         a few tricky cases, such as when FRIEND_DECL's are
 
 
           template <class T> friend void A<T>::g(T t);
           template <class T> friend void A<T>::g(T t);
           template <class T> template <T t> friend void A<T>::h();
           template <class T> template <T t> friend void A<T>::h();
 
 
         and DECL's are
         and DECL's are
 
 
           void A<int>::g(int);
           void A<int>::g(int);
           template <int> void A<int>::h();
           template <int> void A<int>::h();
 
 
         we need to figure out ARGS, the template arguments from
         we need to figure out ARGS, the template arguments from
         the context of DECL.  This is required for template substitution
         the context of DECL.  This is required for template substitution
         of `T' in the function parameter of `g' and template parameter
         of `T' in the function parameter of `g' and template parameter
         of `h' in the above examples.  Here ARGS corresponds to `int'.  */
         of `h' in the above examples.  Here ARGS corresponds to `int'.  */
 
 
      tree context = DECL_CONTEXT (decl);
      tree context = DECL_CONTEXT (decl);
      tree args = NULL_TREE;
      tree args = NULL_TREE;
      int current_depth = 0;
      int current_depth = 0;
 
 
      while (current_depth < template_depth)
      while (current_depth < template_depth)
        {
        {
          if (CLASSTYPE_TEMPLATE_INFO (context))
          if (CLASSTYPE_TEMPLATE_INFO (context))
            {
            {
              if (current_depth == 0)
              if (current_depth == 0)
                args = TYPE_TI_ARGS (context);
                args = TYPE_TI_ARGS (context);
              else
              else
                args = add_to_template_args (TYPE_TI_ARGS (context), args);
                args = add_to_template_args (TYPE_TI_ARGS (context), args);
              current_depth++;
              current_depth++;
            }
            }
          context = TYPE_CONTEXT (context);
          context = TYPE_CONTEXT (context);
        }
        }
 
 
      if (TREE_CODE (decl) == FUNCTION_DECL)
      if (TREE_CODE (decl) == FUNCTION_DECL)
        {
        {
          bool is_template;
          bool is_template;
          tree friend_type;
          tree friend_type;
          tree decl_type;
          tree decl_type;
          tree friend_args_type;
          tree friend_args_type;
          tree decl_args_type;
          tree decl_args_type;
 
 
          /* Make sure that both DECL and FRIEND_DECL are templates or
          /* Make sure that both DECL and FRIEND_DECL are templates or
             non-templates.  */
             non-templates.  */
          is_template = DECL_TEMPLATE_INFO (decl)
          is_template = DECL_TEMPLATE_INFO (decl)
                        && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
                        && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
          if (need_template ^ is_template)
          if (need_template ^ is_template)
            return false;
            return false;
          else if (is_template)
          else if (is_template)
            {
            {
              /* If both are templates, check template parameter list.  */
              /* If both are templates, check template parameter list.  */
              tree friend_parms
              tree friend_parms
                = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
                = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
                                         args, tf_none);
                                         args, tf_none);
              if (!comp_template_parms
              if (!comp_template_parms
                     (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
                     (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
                      friend_parms))
                      friend_parms))
                return false;
                return false;
 
 
              decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
              decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
            }
            }
          else
          else
            decl_type = TREE_TYPE (decl);
            decl_type = TREE_TYPE (decl);
 
 
          friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
          friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
                                              tf_none, NULL_TREE);
                                              tf_none, NULL_TREE);
          if (friend_type == error_mark_node)
          if (friend_type == error_mark_node)
            return false;
            return false;
 
 
          /* Check if return types match.  */
          /* Check if return types match.  */
          if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
          if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
            return false;
            return false;
 
 
          /* Check if function parameter types match, ignoring the
          /* Check if function parameter types match, ignoring the
             `this' parameter.  */
             `this' parameter.  */
          friend_args_type = TYPE_ARG_TYPES (friend_type);
          friend_args_type = TYPE_ARG_TYPES (friend_type);
          decl_args_type = TYPE_ARG_TYPES (decl_type);
          decl_args_type = TYPE_ARG_TYPES (decl_type);
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
            friend_args_type = TREE_CHAIN (friend_args_type);
            friend_args_type = TREE_CHAIN (friend_args_type);
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
            decl_args_type = TREE_CHAIN (decl_args_type);
            decl_args_type = TREE_CHAIN (decl_args_type);
 
 
          return compparms (decl_args_type, friend_args_type);
          return compparms (decl_args_type, friend_args_type);
        }
        }
      else
      else
        {
        {
          /* DECL is a TYPE_DECL */
          /* DECL is a TYPE_DECL */
          bool is_template;
          bool is_template;
          tree decl_type = TREE_TYPE (decl);
          tree decl_type = TREE_TYPE (decl);
 
 
          /* Make sure that both DECL and FRIEND_DECL are templates or
          /* Make sure that both DECL and FRIEND_DECL are templates or
             non-templates.  */
             non-templates.  */
          is_template
          is_template
            = CLASSTYPE_TEMPLATE_INFO (decl_type)
            = CLASSTYPE_TEMPLATE_INFO (decl_type)
              && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
              && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
 
 
          if (need_template ^ is_template)
          if (need_template ^ is_template)
            return false;
            return false;
          else if (is_template)
          else if (is_template)
            {
            {
              tree friend_parms;
              tree friend_parms;
              /* If both are templates, check the name of the two
              /* If both are templates, check the name of the two
                 TEMPLATE_DECL's first because is_friend didn't.  */
                 TEMPLATE_DECL's first because is_friend didn't.  */
              if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
              if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
                  != DECL_NAME (friend_decl))
                  != DECL_NAME (friend_decl))
                return false;
                return false;
 
 
              /* Now check template parameter list.  */
              /* Now check template parameter list.  */
              friend_parms
              friend_parms
                = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
                = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
                                         args, tf_none);
                                         args, tf_none);
              return comp_template_parms
              return comp_template_parms
                (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
                (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
                 friend_parms);
                 friend_parms);
            }
            }
          else
          else
            return (DECL_NAME (decl)
            return (DECL_NAME (decl)
                    == DECL_NAME (friend_decl));
                    == DECL_NAME (friend_decl));
        }
        }
    }
    }
  return false;
  return false;
}
}
 
 
/* Register the specialization SPEC as a specialization of TMPL with
/* Register the specialization SPEC as a specialization of TMPL with
   the indicated ARGS.  IS_FRIEND indicates whether the specialization
   the indicated ARGS.  IS_FRIEND indicates whether the specialization
   is actually just a friend declaration.  Returns SPEC, or an
   is actually just a friend declaration.  Returns SPEC, or an
   equivalent prior declaration, if available.  */
   equivalent prior declaration, if available.  */
 
 
static tree
static tree
register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
                         hashval_t hash)
                         hashval_t hash)
{
{
  tree fn;
  tree fn;
  spec_entry **slot = NULL;
  spec_entry **slot = NULL;
  spec_entry elt;
  spec_entry elt;
 
 
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
 
 
  if (TREE_CODE (spec) == FUNCTION_DECL
  if (TREE_CODE (spec) == FUNCTION_DECL
      && uses_template_parms (DECL_TI_ARGS (spec)))
      && uses_template_parms (DECL_TI_ARGS (spec)))
    /* This is the FUNCTION_DECL for a partial instantiation.  Don't
    /* This is the FUNCTION_DECL for a partial instantiation.  Don't
       register it; we want the corresponding TEMPLATE_DECL instead.
       register it; we want the corresponding TEMPLATE_DECL instead.
       We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
       We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
       the more obvious `uses_template_parms (spec)' to avoid problems
       the more obvious `uses_template_parms (spec)' to avoid problems
       with default function arguments.  In particular, given
       with default function arguments.  In particular, given
       something like this:
       something like this:
 
 
          template <class T> void f(T t1, T t = T())
          template <class T> void f(T t1, T t = T())
 
 
       the default argument expression is not substituted for in an
       the default argument expression is not substituted for in an
       instantiation unless and until it is actually needed.  */
       instantiation unless and until it is actually needed.  */
    return spec;
    return spec;
 
 
  if (optimize_specialization_lookup_p (tmpl))
  if (optimize_specialization_lookup_p (tmpl))
    /* We don't put these specializations in the hash table, but we might
    /* We don't put these specializations in the hash table, but we might
       want to give an error about a mismatch.  */
       want to give an error about a mismatch.  */
    fn = retrieve_specialization (tmpl, args, 0);
    fn = retrieve_specialization (tmpl, args, 0);
  else
  else
    {
    {
      elt.tmpl = tmpl;
      elt.tmpl = tmpl;
      elt.args = args;
      elt.args = args;
      elt.spec = spec;
      elt.spec = spec;
 
 
      if (hash == 0)
      if (hash == 0)
        hash = hash_specialization (&elt);
        hash = hash_specialization (&elt);
 
 
      slot = (spec_entry **)
      slot = (spec_entry **)
        htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
        htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
      if (*slot)
      if (*slot)
        fn = (*slot)->spec;
        fn = (*slot)->spec;
      else
      else
        fn = NULL_TREE;
        fn = NULL_TREE;
    }
    }
 
 
  /* We can sometimes try to re-register a specialization that we've
  /* We can sometimes try to re-register a specialization that we've
     already got.  In particular, regenerate_decl_from_template calls
     already got.  In particular, regenerate_decl_from_template calls
     duplicate_decls which will update the specialization list.  But,
     duplicate_decls which will update the specialization list.  But,
     we'll still get called again here anyhow.  It's more convenient
     we'll still get called again here anyhow.  It's more convenient
     to simply allow this than to try to prevent it.  */
     to simply allow this than to try to prevent it.  */
  if (fn == spec)
  if (fn == spec)
    return spec;
    return spec;
  else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
  else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    {
    {
      if (DECL_TEMPLATE_INSTANTIATION (fn))
      if (DECL_TEMPLATE_INSTANTIATION (fn))
        {
        {
          if (DECL_ODR_USED (fn)
          if (DECL_ODR_USED (fn)
              || DECL_EXPLICIT_INSTANTIATION (fn))
              || DECL_EXPLICIT_INSTANTIATION (fn))
            {
            {
              error ("specialization of %qD after instantiation",
              error ("specialization of %qD after instantiation",
                     fn);
                     fn);
              return error_mark_node;
              return error_mark_node;
            }
            }
          else
          else
            {
            {
              tree clone;
              tree clone;
              /* This situation should occur only if the first
              /* This situation should occur only if the first
                 specialization is an implicit instantiation, the
                 specialization is an implicit instantiation, the
                 second is an explicit specialization, and the
                 second is an explicit specialization, and the
                 implicit instantiation has not yet been used.  That
                 implicit instantiation has not yet been used.  That
                 situation can occur if we have implicitly
                 situation can occur if we have implicitly
                 instantiated a member function and then specialized
                 instantiated a member function and then specialized
                 it later.
                 it later.
 
 
                 We can also wind up here if a friend declaration that
                 We can also wind up here if a friend declaration that
                 looked like an instantiation turns out to be a
                 looked like an instantiation turns out to be a
                 specialization:
                 specialization:
 
 
                   template <class T> void foo(T);
                   template <class T> void foo(T);
                   class S { friend void foo<>(int) };
                   class S { friend void foo<>(int) };
                   template <> void foo(int);
                   template <> void foo(int);
 
 
                 We transform the existing DECL in place so that any
                 We transform the existing DECL in place so that any
                 pointers to it become pointers to the updated
                 pointers to it become pointers to the updated
                 declaration.
                 declaration.
 
 
                 If there was a definition for the template, but not
                 If there was a definition for the template, but not
                 for the specialization, we want this to look as if
                 for the specialization, we want this to look as if
                 there were no definition, and vice versa.  */
                 there were no definition, and vice versa.  */
              DECL_INITIAL (fn) = NULL_TREE;
              DECL_INITIAL (fn) = NULL_TREE;
              duplicate_decls (spec, fn, is_friend);
              duplicate_decls (spec, fn, is_friend);
              /* The call to duplicate_decls will have applied
              /* The call to duplicate_decls will have applied
                 [temp.expl.spec]:
                 [temp.expl.spec]:
 
 
                   An explicit specialization of a function template
                   An explicit specialization of a function template
                   is inline only if it is explicitly declared to be,
                   is inline only if it is explicitly declared to be,
                   and independently of whether its function template
                   and independently of whether its function template
                   is.
                   is.
 
 
                to the primary function; now copy the inline bits to
                to the primary function; now copy the inline bits to
                the various clones.  */
                the various clones.  */
              FOR_EACH_CLONE (clone, fn)
              FOR_EACH_CLONE (clone, fn)
                {
                {
                  DECL_DECLARED_INLINE_P (clone)
                  DECL_DECLARED_INLINE_P (clone)
                    = DECL_DECLARED_INLINE_P (fn);
                    = DECL_DECLARED_INLINE_P (fn);
                  DECL_SOURCE_LOCATION (clone)
                  DECL_SOURCE_LOCATION (clone)
                    = DECL_SOURCE_LOCATION (fn);
                    = DECL_SOURCE_LOCATION (fn);
                }
                }
              check_specialization_namespace (fn);
              check_specialization_namespace (fn);
 
 
              return fn;
              return fn;
            }
            }
        }
        }
      else if (DECL_TEMPLATE_SPECIALIZATION (fn))
      else if (DECL_TEMPLATE_SPECIALIZATION (fn))
        {
        {
          if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
          if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
            /* Dup decl failed, but this is a new definition. Set the
            /* Dup decl failed, but this is a new definition. Set the
               line number so any errors match this new
               line number so any errors match this new
               definition.  */
               definition.  */
            DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
            DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
 
 
          return fn;
          return fn;
        }
        }
    }
    }
  else if (fn)
  else if (fn)
    return duplicate_decls (spec, fn, is_friend);
    return duplicate_decls (spec, fn, is_friend);
 
 
  /* A specialization must be declared in the same namespace as the
  /* A specialization must be declared in the same namespace as the
     template it is specializing.  */
     template it is specializing.  */
  if (DECL_TEMPLATE_SPECIALIZATION (spec)
  if (DECL_TEMPLATE_SPECIALIZATION (spec)
      && !check_specialization_namespace (tmpl))
      && !check_specialization_namespace (tmpl))
    DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
 
 
  if (!optimize_specialization_lookup_p (tmpl))
  if (!optimize_specialization_lookup_p (tmpl))
    {
    {
      gcc_assert (tmpl && args && spec);
      gcc_assert (tmpl && args && spec);
      *slot = GGC_NEW (spec_entry);
      *slot = GGC_NEW (spec_entry);
      **slot = elt;
      **slot = elt;
      if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
      if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
          && PRIMARY_TEMPLATE_P (tmpl)
          && PRIMARY_TEMPLATE_P (tmpl)
          && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
          && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
        /* TMPL is a forward declaration of a template function; keep a list
        /* TMPL is a forward declaration of a template function; keep a list
           of all specializations in case we need to reassign them to a friend
           of all specializations in case we need to reassign them to a friend
           template later in tsubst_friend_function.  */
           template later in tsubst_friend_function.  */
        DECL_TEMPLATE_INSTANTIATIONS (tmpl)
        DECL_TEMPLATE_INSTANTIATIONS (tmpl)
          = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
          = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
    }
    }
 
 
  return spec;
  return spec;
}
}
 
 
/* Returns true iff two spec_entry nodes are equivalent.  Only compares the
/* Returns true iff two spec_entry nodes are equivalent.  Only compares the
   TMPL and ARGS members, ignores SPEC.  */
   TMPL and ARGS members, ignores SPEC.  */
 
 
static int
static int
eq_specializations (const void *p1, const void *p2)
eq_specializations (const void *p1, const void *p2)
{
{
  const spec_entry *e1 = (const spec_entry *)p1;
  const spec_entry *e1 = (const spec_entry *)p1;
  const spec_entry *e2 = (const spec_entry *)p2;
  const spec_entry *e2 = (const spec_entry *)p2;
 
 
  return (e1->tmpl == e2->tmpl
  return (e1->tmpl == e2->tmpl
          && comp_template_args (e1->args, e2->args));
          && comp_template_args (e1->args, e2->args));
}
}
 
 
/* Returns a hash for a template TMPL and template arguments ARGS.  */
/* Returns a hash for a template TMPL and template arguments ARGS.  */
 
 
static hashval_t
static hashval_t
hash_tmpl_and_args (tree tmpl, tree args)
hash_tmpl_and_args (tree tmpl, tree args)
{
{
  hashval_t val = DECL_UID (tmpl);
  hashval_t val = DECL_UID (tmpl);
  return iterative_hash_template_arg (args, val);
  return iterative_hash_template_arg (args, val);
}
}
 
 
/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
   ignoring SPEC.  */
   ignoring SPEC.  */
 
 
static hashval_t
static hashval_t
hash_specialization (const void *p)
hash_specialization (const void *p)
{
{
  const spec_entry *e = (const spec_entry *)p;
  const spec_entry *e = (const spec_entry *)p;
  return hash_tmpl_and_args (e->tmpl, e->args);
  return hash_tmpl_and_args (e->tmpl, e->args);
}
}
 
 
/* Recursively calculate a hash value for a template argument ARG, for use
/* Recursively calculate a hash value for a template argument ARG, for use
   in the hash tables of template specializations.  */
   in the hash tables of template specializations.  */
 
 
static hashval_t
static hashval_t
iterative_hash_template_arg (tree arg, hashval_t val)
iterative_hash_template_arg (tree arg, hashval_t val)
{
{
  unsigned HOST_WIDE_INT i;
  unsigned HOST_WIDE_INT i;
  enum tree_code code;
  enum tree_code code;
  char tclass;
  char tclass;
 
 
  if (arg == NULL_TREE)
  if (arg == NULL_TREE)
    return iterative_hash_object (arg, val);
    return iterative_hash_object (arg, val);
 
 
  if (!TYPE_P (arg))
  if (!TYPE_P (arg))
    STRIP_NOPS (arg);
    STRIP_NOPS (arg);
 
 
  if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
  if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
    /* We can get one of these when re-hashing a previous entry in the middle
    /* We can get one of these when re-hashing a previous entry in the middle
       of substituting into a pack expansion.  Just look through it.  */
       of substituting into a pack expansion.  Just look through it.  */
    arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
    arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
 
 
  code = TREE_CODE (arg);
  code = TREE_CODE (arg);
  tclass = TREE_CODE_CLASS (code);
  tclass = TREE_CODE_CLASS (code);
 
 
  val = iterative_hash_object (code, val);
  val = iterative_hash_object (code, val);
 
 
  switch (code)
  switch (code)
    {
    {
    case ERROR_MARK:
    case ERROR_MARK:
      return val;
      return val;
 
 
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
      return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
      return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
 
 
    case TREE_VEC:
    case TREE_VEC:
      {
      {
        int i, len = TREE_VEC_LENGTH (arg);
        int i, len = TREE_VEC_LENGTH (arg);
        for (i = 0; i < len; ++i)
        for (i = 0; i < len; ++i)
          val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
          val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
        return val;
        return val;
      }
      }
 
 
    case TYPE_PACK_EXPANSION:
    case TYPE_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
      return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
      return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
 
 
    case TYPE_ARGUMENT_PACK:
    case TYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
      return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
 
 
    case TREE_LIST:
    case TREE_LIST:
      for (; arg; arg = TREE_CHAIN (arg))
      for (; arg; arg = TREE_CHAIN (arg))
        val = iterative_hash_template_arg (TREE_VALUE (arg), val);
        val = iterative_hash_template_arg (TREE_VALUE (arg), val);
      return val;
      return val;
 
 
    case OVERLOAD:
    case OVERLOAD:
      for (; arg; arg = OVL_CHAIN (arg))
      for (; arg; arg = OVL_CHAIN (arg))
        val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
        val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
      return val;
      return val;
 
 
    case CONSTRUCTOR:
    case CONSTRUCTOR:
      {
      {
        tree field, value;
        tree field, value;
        FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
        FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
          {
          {
            val = iterative_hash_template_arg (field, val);
            val = iterative_hash_template_arg (field, val);
            val = iterative_hash_template_arg (value, val);
            val = iterative_hash_template_arg (value, val);
          }
          }
        return val;
        return val;
      }
      }
 
 
    case PARM_DECL:
    case PARM_DECL:
      if (!DECL_ARTIFICIAL (arg))
      if (!DECL_ARTIFICIAL (arg))
        val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
        val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
      return iterative_hash_template_arg (TREE_TYPE (arg), val);
      return iterative_hash_template_arg (TREE_TYPE (arg), val);
 
 
    case TARGET_EXPR:
    case TARGET_EXPR:
      return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
      return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
 
 
    case PTRMEM_CST:
    case PTRMEM_CST:
      val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
      val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
      return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
      return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
 
 
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      val = iterative_hash_template_arg
      val = iterative_hash_template_arg
        (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
        (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
      val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
      val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
      return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
      return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
 
 
    case TRAIT_EXPR:
    case TRAIT_EXPR:
      val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
      val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
      val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
      val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
      return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
      return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
 
 
    case BASELINK:
    case BASELINK:
      val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
      val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
                                         val);
                                         val);
      return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
      return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
                                          val);
                                          val);
 
 
    case MODOP_EXPR:
    case MODOP_EXPR:
      val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
      val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
      code = TREE_CODE (TREE_OPERAND (arg, 1));
      code = TREE_CODE (TREE_OPERAND (arg, 1));
      val = iterative_hash_object (code, val);
      val = iterative_hash_object (code, val);
      return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
      return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
 
 
    case ARRAY_TYPE:
    case ARRAY_TYPE:
      /* layout_type sets structural equality for arrays of
      /* layout_type sets structural equality for arrays of
         incomplete type, so we can't rely on the canonical type
         incomplete type, so we can't rely on the canonical type
         for hashing.  */
         for hashing.  */
      val = iterative_hash_template_arg (TREE_TYPE (arg), val);
      val = iterative_hash_template_arg (TREE_TYPE (arg), val);
      return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
      return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
 
 
    case LAMBDA_EXPR:
    case LAMBDA_EXPR:
      /* A lambda can't appear in a template arg, but don't crash on
      /* A lambda can't appear in a template arg, but don't crash on
         erroneous input.  */
         erroneous input.  */
      gcc_assert (errorcount > 0);
      gcc_assert (errorcount > 0);
      return val;
      return val;
 
 
    default:
    default:
      switch (tclass)
      switch (tclass)
        {
        {
        case tcc_type:
        case tcc_type:
          if (TYPE_CANONICAL (arg))
          if (TYPE_CANONICAL (arg))
            return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
            return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
                                          val);
                                          val);
          else if (TREE_CODE (arg) == DECLTYPE_TYPE)
          else if (TREE_CODE (arg) == DECLTYPE_TYPE)
            return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
            return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
          /* Otherwise just compare the types during lookup.  */
          /* Otherwise just compare the types during lookup.  */
          return val;
          return val;
 
 
        case tcc_declaration:
        case tcc_declaration:
        case tcc_constant:
        case tcc_constant:
          return iterative_hash_expr (arg, val);
          return iterative_hash_expr (arg, val);
 
 
        default:
        default:
          gcc_assert (IS_EXPR_CODE_CLASS (tclass));
          gcc_assert (IS_EXPR_CODE_CLASS (tclass));
          {
          {
            unsigned n = TREE_OPERAND_LENGTH (arg);
            unsigned n = TREE_OPERAND_LENGTH (arg);
            for (i = 0; i < n; ++i)
            for (i = 0; i < n; ++i)
              val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
              val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
            return val;
            return val;
          }
          }
        }
        }
    }
    }
  gcc_unreachable ();
  gcc_unreachable ();
  return 0;
  return 0;
}
}
 
 
/* Unregister the specialization SPEC as a specialization of TMPL.
/* Unregister the specialization SPEC as a specialization of TMPL.
   Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
   Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
   if the SPEC was listed as a specialization of TMPL.
   if the SPEC was listed as a specialization of TMPL.
 
 
   Note that SPEC has been ggc_freed, so we can't look inside it.  */
   Note that SPEC has been ggc_freed, so we can't look inside it.  */
 
 
bool
bool
reregister_specialization (tree spec, tree tinfo, tree new_spec)
reregister_specialization (tree spec, tree tinfo, tree new_spec)
{
{
  spec_entry **slot;
  spec_entry **slot;
  spec_entry elt;
  spec_entry elt;
 
 
  elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
  elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
  elt.args = TI_ARGS (tinfo);
  elt.args = TI_ARGS (tinfo);
  elt.spec = NULL_TREE;
  elt.spec = NULL_TREE;
 
 
  slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
  slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
  if (*slot)
  if (*slot)
    {
    {
      gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
      gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
      gcc_assert (new_spec != NULL_TREE);
      gcc_assert (new_spec != NULL_TREE);
      (*slot)->spec = new_spec;
      (*slot)->spec = new_spec;
      return 1;
      return 1;
    }
    }
 
 
  return 0;
  return 0;
}
}
 
 
/* Compare an entry in the local specializations hash table P1 (which
/* Compare an entry in the local specializations hash table P1 (which
   is really a pointer to a TREE_LIST) with P2 (which is really a
   is really a pointer to a TREE_LIST) with P2 (which is really a
   DECL).  */
   DECL).  */
 
 
static int
static int
eq_local_specializations (const void *p1, const void *p2)
eq_local_specializations (const void *p1, const void *p2)
{
{
  return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
  return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
}
}
 
 
/* Hash P1, an entry in the local specializations table.  */
/* Hash P1, an entry in the local specializations table.  */
 
 
static hashval_t
static hashval_t
hash_local_specialization (const void* p1)
hash_local_specialization (const void* p1)
{
{
  return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
  return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
}
}
 
 
/* Like register_specialization, but for local declarations.  We are
/* Like register_specialization, but for local declarations.  We are
   registering SPEC, an instantiation of TMPL.  */
   registering SPEC, an instantiation of TMPL.  */
 
 
static void
static void
register_local_specialization (tree spec, tree tmpl)
register_local_specialization (tree spec, tree tmpl)
{
{
  void **slot;
  void **slot;
 
 
  slot = htab_find_slot_with_hash (local_specializations, tmpl,
  slot = htab_find_slot_with_hash (local_specializations, tmpl,
                                   htab_hash_pointer (tmpl), INSERT);
                                   htab_hash_pointer (tmpl), INSERT);
  *slot = build_tree_list (spec, tmpl);
  *slot = build_tree_list (spec, tmpl);
}
}
 
 
/* TYPE is a class type.  Returns true if TYPE is an explicitly
/* TYPE is a class type.  Returns true if TYPE is an explicitly
   specialized class.  */
   specialized class.  */
 
 
bool
bool
explicit_class_specialization_p (tree type)
explicit_class_specialization_p (tree type)
{
{
  if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
  if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    return false;
    return false;
  return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
  return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
}
}
 
 
/* Print the list of functions at FNS, going through all the overloads
/* Print the list of functions at FNS, going through all the overloads
   for each element of the list.  Alternatively, FNS can not be a
   for each element of the list.  Alternatively, FNS can not be a
   TREE_LIST, in which case it will be printed together with all the
   TREE_LIST, in which case it will be printed together with all the
   overloads.
   overloads.
 
 
   MORE and *STR should respectively be FALSE and NULL when the function
   MORE and *STR should respectively be FALSE and NULL when the function
   is called from the outside.  They are used internally on recursive
   is called from the outside.  They are used internally on recursive
   calls.  print_candidates manages the two parameters and leaves NULL
   calls.  print_candidates manages the two parameters and leaves NULL
   in *STR when it ends.  */
   in *STR when it ends.  */
 
 
static void
static void
print_candidates_1 (tree fns, bool more, const char **str)
print_candidates_1 (tree fns, bool more, const char **str)
{
{
  tree fn, fn2;
  tree fn, fn2;
  char *spaces = NULL;
  char *spaces = NULL;
 
 
  for (fn = fns; fn; fn = OVL_NEXT (fn))
  for (fn = fns; fn; fn = OVL_NEXT (fn))
    if (TREE_CODE (fn) == TREE_LIST)
    if (TREE_CODE (fn) == TREE_LIST)
      {
      {
        gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
        gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
        for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
        for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
          print_candidates_1 (TREE_VALUE (fn2),
          print_candidates_1 (TREE_VALUE (fn2),
                              TREE_CHAIN (fn2) || more, str);
                              TREE_CHAIN (fn2) || more, str);
      }
      }
    else
    else
      {
      {
        if (!*str)
        if (!*str)
          {
          {
            /* Pick the prefix string.  */
            /* Pick the prefix string.  */
            if (!more && !OVL_NEXT (fns))
            if (!more && !OVL_NEXT (fns))
              {
              {
                error ("candidate is: %+#D", OVL_CURRENT (fn));
                error ("candidate is: %+#D", OVL_CURRENT (fn));
                continue;
                continue;
              }
              }
 
 
            *str = _("candidates are:");
            *str = _("candidates are:");
            spaces = get_spaces (*str);
            spaces = get_spaces (*str);
          }
          }
        error ("%s %+#D", *str, OVL_CURRENT (fn));
        error ("%s %+#D", *str, OVL_CURRENT (fn));
        *str = spaces ? spaces : *str;
        *str = spaces ? spaces : *str;
      }
      }
 
 
  if (!more)
  if (!more)
    {
    {
      free (spaces);
      free (spaces);
      *str = NULL;
      *str = NULL;
    }
    }
}
}
 
 
/* Print the list of candidate FNS in an error message.  */
/* Print the list of candidate FNS in an error message.  */
 
 
void
void
print_candidates (tree fns)
print_candidates (tree fns)
{
{
  const char *str = NULL;
  const char *str = NULL;
  print_candidates_1 (fns, false, &str);
  print_candidates_1 (fns, false, &str);
  gcc_assert (str == NULL);
  gcc_assert (str == NULL);
}
}
 
 
/* Returns the template (one of the functions given by TEMPLATE_ID)
/* Returns the template (one of the functions given by TEMPLATE_ID)
   which can be specialized to match the indicated DECL with the
   which can be specialized to match the indicated DECL with the
   explicit template args given in TEMPLATE_ID.  The DECL may be
   explicit template args given in TEMPLATE_ID.  The DECL may be
   NULL_TREE if none is available.  In that case, the functions in
   NULL_TREE if none is available.  In that case, the functions in
   TEMPLATE_ID are non-members.
   TEMPLATE_ID are non-members.
 
 
   If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
   If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
   specialization of a member template.
   specialization of a member template.
 
 
   The TEMPLATE_COUNT is the number of references to qualifying
   The TEMPLATE_COUNT is the number of references to qualifying
   template classes that appeared in the name of the function. See
   template classes that appeared in the name of the function. See
   check_explicit_specialization for a more accurate description.
   check_explicit_specialization for a more accurate description.
 
 
   TSK indicates what kind of template declaration (if any) is being
   TSK indicates what kind of template declaration (if any) is being
   declared.  TSK_TEMPLATE indicates that the declaration given by
   declared.  TSK_TEMPLATE indicates that the declaration given by
   DECL, though a FUNCTION_DECL, has template parameters, and is
   DECL, though a FUNCTION_DECL, has template parameters, and is
   therefore a template function.
   therefore a template function.
 
 
   The template args (those explicitly specified and those deduced)
   The template args (those explicitly specified and those deduced)
   are output in a newly created vector *TARGS_OUT.
   are output in a newly created vector *TARGS_OUT.
 
 
   If it is impossible to determine the result, an error message is
   If it is impossible to determine the result, an error message is
   issued.  The error_mark_node is returned to indicate failure.  */
   issued.  The error_mark_node is returned to indicate failure.  */
 
 
static tree
static tree
determine_specialization (tree template_id,
determine_specialization (tree template_id,
                          tree decl,
                          tree decl,
                          tree* targs_out,
                          tree* targs_out,
                          int need_member_template,
                          int need_member_template,
                          int template_count,
                          int template_count,
                          tmpl_spec_kind tsk)
                          tmpl_spec_kind tsk)
{
{
  tree fns;
  tree fns;
  tree targs;
  tree targs;
  tree explicit_targs;
  tree explicit_targs;
  tree candidates = NULL_TREE;
  tree candidates = NULL_TREE;
  /* A TREE_LIST of templates of which DECL may be a specialization.
  /* A TREE_LIST of templates of which DECL may be a specialization.
     The TREE_VALUE of each node is a TEMPLATE_DECL.  The
     The TREE_VALUE of each node is a TEMPLATE_DECL.  The
     corresponding TREE_PURPOSE is the set of template arguments that,
     corresponding TREE_PURPOSE is the set of template arguments that,
     when used to instantiate the template, would produce a function
     when used to instantiate the template, would produce a function
     with the signature of DECL.  */
     with the signature of DECL.  */
  tree templates = NULL_TREE;
  tree templates = NULL_TREE;
  int header_count;
  int header_count;
  struct cp_binding_level *b;
  struct cp_binding_level *b;
 
 
  *targs_out = NULL_TREE;
  *targs_out = NULL_TREE;
 
 
  if (template_id == error_mark_node || decl == error_mark_node)
  if (template_id == error_mark_node || decl == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  fns = TREE_OPERAND (template_id, 0);
  fns = TREE_OPERAND (template_id, 0);
  explicit_targs = TREE_OPERAND (template_id, 1);
  explicit_targs = TREE_OPERAND (template_id, 1);
 
 
  if (fns == error_mark_node)
  if (fns == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  /* Check for baselinks.  */
  /* Check for baselinks.  */
  if (BASELINK_P (fns))
  if (BASELINK_P (fns))
    fns = BASELINK_FUNCTIONS (fns);
    fns = BASELINK_FUNCTIONS (fns);
 
 
  if (!is_overloaded_fn (fns))
  if (!is_overloaded_fn (fns))
    {
    {
      error ("%qD is not a function template", fns);
      error ("%qD is not a function template", fns);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* Count the number of template headers specified for this
  /* Count the number of template headers specified for this
     specialization.  */
     specialization.  */
  header_count = 0;
  header_count = 0;
  for (b = current_binding_level;
  for (b = current_binding_level;
       b->kind == sk_template_parms;
       b->kind == sk_template_parms;
       b = b->level_chain)
       b = b->level_chain)
    ++header_count;
    ++header_count;
 
 
  for (; fns; fns = OVL_NEXT (fns))
  for (; fns; fns = OVL_NEXT (fns))
    {
    {
      tree fn = OVL_CURRENT (fns);
      tree fn = OVL_CURRENT (fns);
 
 
      if (TREE_CODE (fn) == TEMPLATE_DECL)
      if (TREE_CODE (fn) == TEMPLATE_DECL)
        {
        {
          tree decl_arg_types;
          tree decl_arg_types;
          tree fn_arg_types;
          tree fn_arg_types;
 
 
          /* In case of explicit specialization, we need to check if
          /* In case of explicit specialization, we need to check if
             the number of template headers appearing in the specialization
             the number of template headers appearing in the specialization
             is correct. This is usually done in check_explicit_specialization,
             is correct. This is usually done in check_explicit_specialization,
             but the check done there cannot be exhaustive when specializing
             but the check done there cannot be exhaustive when specializing
             member functions. Consider the following code:
             member functions. Consider the following code:
 
 
             template <> void A<int>::f(int);
             template <> void A<int>::f(int);
             template <> template <> void A<int>::f(int);
             template <> template <> void A<int>::f(int);
 
 
             Assuming that A<int> is not itself an explicit specialization
             Assuming that A<int> is not itself an explicit specialization
             already, the first line specializes "f" which is a non-template
             already, the first line specializes "f" which is a non-template
             member function, whilst the second line specializes "f" which
             member function, whilst the second line specializes "f" which
             is a template member function. So both lines are syntactically
             is a template member function. So both lines are syntactically
             correct, and check_explicit_specialization does not reject
             correct, and check_explicit_specialization does not reject
             them.
             them.
 
 
             Here, we can do better, as we are matching the specialization
             Here, we can do better, as we are matching the specialization
             against the declarations. We count the number of template
             against the declarations. We count the number of template
             headers, and we check if they match TEMPLATE_COUNT + 1
             headers, and we check if they match TEMPLATE_COUNT + 1
             (TEMPLATE_COUNT is the number of qualifying template classes,
             (TEMPLATE_COUNT is the number of qualifying template classes,
             plus there must be another header for the member template
             plus there must be another header for the member template
             itself).
             itself).
 
 
             Notice that if header_count is zero, this is not a
             Notice that if header_count is zero, this is not a
             specialization but rather a template instantiation, so there
             specialization but rather a template instantiation, so there
             is no check we can perform here.  */
             is no check we can perform here.  */
          if (header_count && header_count != template_count + 1)
          if (header_count && header_count != template_count + 1)
            continue;
            continue;
 
 
          /* Check that the number of template arguments at the
          /* Check that the number of template arguments at the
             innermost level for DECL is the same as for FN.  */
             innermost level for DECL is the same as for FN.  */
          if (current_binding_level->kind == sk_template_parms
          if (current_binding_level->kind == sk_template_parms
              && !current_binding_level->explicit_spec_p
              && !current_binding_level->explicit_spec_p
              && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
              && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
                  != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
                  != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
                                      (current_template_parms))))
                                      (current_template_parms))))
            continue;
            continue;
 
 
          /* DECL might be a specialization of FN.  */
          /* DECL might be a specialization of FN.  */
          decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
          decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
          fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
          fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
 
 
          /* For a non-static member function, we need to make sure
          /* For a non-static member function, we need to make sure
             that the const qualification is the same.  Since
             that the const qualification is the same.  Since
             get_bindings does not try to merge the "this" parameter,
             get_bindings does not try to merge the "this" parameter,
             we must do the comparison explicitly.  */
             we must do the comparison explicitly.  */
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
          if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
              && !same_type_p (TREE_VALUE (fn_arg_types),
              && !same_type_p (TREE_VALUE (fn_arg_types),
                               TREE_VALUE (decl_arg_types)))
                               TREE_VALUE (decl_arg_types)))
            continue;
            continue;
 
 
          /* Skip the "this" parameter and, for constructors of
          /* Skip the "this" parameter and, for constructors of
             classes with virtual bases, the VTT parameter.  A
             classes with virtual bases, the VTT parameter.  A
             full specialization of a constructor will have a VTT
             full specialization of a constructor will have a VTT
             parameter, but a template never will.  */
             parameter, but a template never will.  */
          decl_arg_types
          decl_arg_types
            = skip_artificial_parms_for (decl, decl_arg_types);
            = skip_artificial_parms_for (decl, decl_arg_types);
          fn_arg_types
          fn_arg_types
            = skip_artificial_parms_for (fn, fn_arg_types);
            = skip_artificial_parms_for (fn, fn_arg_types);
 
 
          /* Check that the number of function parameters matches.
          /* Check that the number of function parameters matches.
             For example,
             For example,
               template <class T> void f(int i = 0);
               template <class T> void f(int i = 0);
               template <> void f<int>();
               template <> void f<int>();
             The specialization f<int> is invalid but is not caught
             The specialization f<int> is invalid but is not caught
             by get_bindings below.  */
             by get_bindings below.  */
          if (list_length (fn_arg_types) != list_length (decl_arg_types))
          if (list_length (fn_arg_types) != list_length (decl_arg_types))
            continue;
            continue;
 
 
          /* Function templates cannot be specializations; there are
          /* Function templates cannot be specializations; there are
             no partial specializations of functions.  Therefore, if
             no partial specializations of functions.  Therefore, if
             the type of DECL does not match FN, there is no
             the type of DECL does not match FN, there is no
             match.  */
             match.  */
          if (tsk == tsk_template)
          if (tsk == tsk_template)
            {
            {
              if (compparms (fn_arg_types, decl_arg_types))
              if (compparms (fn_arg_types, decl_arg_types))
                candidates = tree_cons (NULL_TREE, fn, candidates);
                candidates = tree_cons (NULL_TREE, fn, candidates);
              continue;
              continue;
            }
            }
 
 
          /* See whether this function might be a specialization of this
          /* See whether this function might be a specialization of this
             template.  */
             template.  */
          targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
          targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
 
 
          if (!targs)
          if (!targs)
            /* We cannot deduce template arguments that when used to
            /* We cannot deduce template arguments that when used to
               specialize TMPL will produce DECL.  */
               specialize TMPL will produce DECL.  */
            continue;
            continue;
 
 
          /* Save this template, and the arguments deduced.  */
          /* Save this template, and the arguments deduced.  */
          templates = tree_cons (targs, fn, templates);
          templates = tree_cons (targs, fn, templates);
        }
        }
      else if (need_member_template)
      else if (need_member_template)
        /* FN is an ordinary member function, and we need a
        /* FN is an ordinary member function, and we need a
           specialization of a member template.  */
           specialization of a member template.  */
        ;
        ;
      else if (TREE_CODE (fn) != FUNCTION_DECL)
      else if (TREE_CODE (fn) != FUNCTION_DECL)
        /* We can get IDENTIFIER_NODEs here in certain erroneous
        /* We can get IDENTIFIER_NODEs here in certain erroneous
           cases.  */
           cases.  */
        ;
        ;
      else if (!DECL_FUNCTION_MEMBER_P (fn))
      else if (!DECL_FUNCTION_MEMBER_P (fn))
        /* This is just an ordinary non-member function.  Nothing can
        /* This is just an ordinary non-member function.  Nothing can
           be a specialization of that.  */
           be a specialization of that.  */
        ;
        ;
      else if (DECL_ARTIFICIAL (fn))
      else if (DECL_ARTIFICIAL (fn))
        /* Cannot specialize functions that are created implicitly.  */
        /* Cannot specialize functions that are created implicitly.  */
        ;
        ;
      else
      else
        {
        {
          tree decl_arg_types;
          tree decl_arg_types;
 
 
          /* This is an ordinary member function.  However, since
          /* This is an ordinary member function.  However, since
             we're here, we can assume it's enclosing class is a
             we're here, we can assume it's enclosing class is a
             template class.  For example,
             template class.  For example,
 
 
               template <typename T> struct S { void f(); };
               template <typename T> struct S { void f(); };
               template <> void S<int>::f() {}
               template <> void S<int>::f() {}
 
 
             Here, S<int>::f is a non-template, but S<int> is a
             Here, S<int>::f is a non-template, but S<int> is a
             template class.  If FN has the same type as DECL, we
             template class.  If FN has the same type as DECL, we
             might be in business.  */
             might be in business.  */
 
 
          if (!DECL_TEMPLATE_INFO (fn))
          if (!DECL_TEMPLATE_INFO (fn))
            /* Its enclosing class is an explicit specialization
            /* Its enclosing class is an explicit specialization
               of a template class.  This is not a candidate.  */
               of a template class.  This is not a candidate.  */
            continue;
            continue;
 
 
          if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
          if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
                            TREE_TYPE (TREE_TYPE (fn))))
                            TREE_TYPE (TREE_TYPE (fn))))
            /* The return types differ.  */
            /* The return types differ.  */
            continue;
            continue;
 
 
          /* Adjust the type of DECL in case FN is a static member.  */
          /* Adjust the type of DECL in case FN is a static member.  */
          decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
          decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
          if (DECL_STATIC_FUNCTION_P (fn)
          if (DECL_STATIC_FUNCTION_P (fn)
              && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
              && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
            decl_arg_types = TREE_CHAIN (decl_arg_types);
            decl_arg_types = TREE_CHAIN (decl_arg_types);
 
 
          if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
          if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
                         decl_arg_types))
                         decl_arg_types))
            /* They match!  */
            /* They match!  */
            candidates = tree_cons (NULL_TREE, fn, candidates);
            candidates = tree_cons (NULL_TREE, fn, candidates);
        }
        }
    }
    }
 
 
  if (templates && TREE_CHAIN (templates))
  if (templates && TREE_CHAIN (templates))
    {
    {
      /* We have:
      /* We have:
 
 
           [temp.expl.spec]
           [temp.expl.spec]
 
 
           It is possible for a specialization with a given function
           It is possible for a specialization with a given function
           signature to be instantiated from more than one function
           signature to be instantiated from more than one function
           template.  In such cases, explicit specification of the
           template.  In such cases, explicit specification of the
           template arguments must be used to uniquely identify the
           template arguments must be used to uniquely identify the
           function template specialization being specialized.
           function template specialization being specialized.
 
 
         Note that here, there's no suggestion that we're supposed to
         Note that here, there's no suggestion that we're supposed to
         determine which of the candidate templates is most
         determine which of the candidate templates is most
         specialized.  However, we, also have:
         specialized.  However, we, also have:
 
 
           [temp.func.order]
           [temp.func.order]
 
 
           Partial ordering of overloaded function template
           Partial ordering of overloaded function template
           declarations is used in the following contexts to select
           declarations is used in the following contexts to select
           the function template to which a function template
           the function template to which a function template
           specialization refers:
           specialization refers:
 
 
           -- when an explicit specialization refers to a function
           -- when an explicit specialization refers to a function
              template.
              template.
 
 
         So, we do use the partial ordering rules, at least for now.
         So, we do use the partial ordering rules, at least for now.
         This extension can only serve to make invalid programs valid,
         This extension can only serve to make invalid programs valid,
         so it's safe.  And, there is strong anecdotal evidence that
         so it's safe.  And, there is strong anecdotal evidence that
         the committee intended the partial ordering rules to apply;
         the committee intended the partial ordering rules to apply;
         the EDG front end has that behavior, and John Spicer claims
         the EDG front end has that behavior, and John Spicer claims
         that the committee simply forgot to delete the wording in
         that the committee simply forgot to delete the wording in
         [temp.expl.spec].  */
         [temp.expl.spec].  */
      tree tmpl = most_specialized_instantiation (templates);
      tree tmpl = most_specialized_instantiation (templates);
      if (tmpl != error_mark_node)
      if (tmpl != error_mark_node)
        {
        {
          templates = tmpl;
          templates = tmpl;
          TREE_CHAIN (templates) = NULL_TREE;
          TREE_CHAIN (templates) = NULL_TREE;
        }
        }
    }
    }
 
 
  if (templates == NULL_TREE && candidates == NULL_TREE)
  if (templates == NULL_TREE && candidates == NULL_TREE)
    {
    {
      error ("template-id %qD for %q+D does not match any template "
      error ("template-id %qD for %q+D does not match any template "
             "declaration", template_id, decl);
             "declaration", template_id, decl);
      if (header_count && header_count != template_count + 1)
      if (header_count && header_count != template_count + 1)
        inform (input_location, "saw %d %<template<>%>, need %d for "
        inform (input_location, "saw %d %<template<>%>, need %d for "
                "specializing a member function template",
                "specializing a member function template",
                header_count, template_count + 1);
                header_count, template_count + 1);
      return error_mark_node;
      return error_mark_node;
    }
    }
  else if ((templates && TREE_CHAIN (templates))
  else if ((templates && TREE_CHAIN (templates))
           || (candidates && TREE_CHAIN (candidates))
           || (candidates && TREE_CHAIN (candidates))
           || (templates && candidates))
           || (templates && candidates))
    {
    {
      error ("ambiguous template specialization %qD for %q+D",
      error ("ambiguous template specialization %qD for %q+D",
             template_id, decl);
             template_id, decl);
      candidates = chainon (candidates, templates);
      candidates = chainon (candidates, templates);
      print_candidates (candidates);
      print_candidates (candidates);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* We have one, and exactly one, match.  */
  /* We have one, and exactly one, match.  */
  if (candidates)
  if (candidates)
    {
    {
      tree fn = TREE_VALUE (candidates);
      tree fn = TREE_VALUE (candidates);
      *targs_out = copy_node (DECL_TI_ARGS (fn));
      *targs_out = copy_node (DECL_TI_ARGS (fn));
      /* DECL is a re-declaration or partial instantiation of a template
      /* DECL is a re-declaration or partial instantiation of a template
         function.  */
         function.  */
      if (TREE_CODE (fn) == TEMPLATE_DECL)
      if (TREE_CODE (fn) == TEMPLATE_DECL)
        return fn;
        return fn;
      /* It was a specialization of an ordinary member function in a
      /* It was a specialization of an ordinary member function in a
         template class.  */
         template class.  */
      return DECL_TI_TEMPLATE (fn);
      return DECL_TI_TEMPLATE (fn);
    }
    }
 
 
  /* It was a specialization of a template.  */
  /* It was a specialization of a template.  */
  targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
  targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
    {
    {
      *targs_out = copy_node (targs);
      *targs_out = copy_node (targs);
      SET_TMPL_ARGS_LEVEL (*targs_out,
      SET_TMPL_ARGS_LEVEL (*targs_out,
                           TMPL_ARGS_DEPTH (*targs_out),
                           TMPL_ARGS_DEPTH (*targs_out),
                           TREE_PURPOSE (templates));
                           TREE_PURPOSE (templates));
    }
    }
  else
  else
    *targs_out = TREE_PURPOSE (templates);
    *targs_out = TREE_PURPOSE (templates);
  return TREE_VALUE (templates);
  return TREE_VALUE (templates);
}
}
 
 
/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
   but with the default argument values filled in from those in the
   but with the default argument values filled in from those in the
   TMPL_TYPES.  */
   TMPL_TYPES.  */
 
 
static tree
static tree
copy_default_args_to_explicit_spec_1 (tree spec_types,
copy_default_args_to_explicit_spec_1 (tree spec_types,
                                      tree tmpl_types)
                                      tree tmpl_types)
{
{
  tree new_spec_types;
  tree new_spec_types;
 
 
  if (!spec_types)
  if (!spec_types)
    return NULL_TREE;
    return NULL_TREE;
 
 
  if (spec_types == void_list_node)
  if (spec_types == void_list_node)
    return void_list_node;
    return void_list_node;
 
 
  /* Substitute into the rest of the list.  */
  /* Substitute into the rest of the list.  */
  new_spec_types =
  new_spec_types =
    copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
                                          TREE_CHAIN (tmpl_types));
                                          TREE_CHAIN (tmpl_types));
 
 
  /* Add the default argument for this parameter.  */
  /* Add the default argument for this parameter.  */
  return hash_tree_cons (TREE_PURPOSE (tmpl_types),
  return hash_tree_cons (TREE_PURPOSE (tmpl_types),
                         TREE_VALUE (spec_types),
                         TREE_VALUE (spec_types),
                         new_spec_types);
                         new_spec_types);
}
}
 
 
/* DECL is an explicit specialization.  Replicate default arguments
/* DECL is an explicit specialization.  Replicate default arguments
   from the template it specializes.  (That way, code like:
   from the template it specializes.  (That way, code like:
 
 
     template <class T> void f(T = 3);
     template <class T> void f(T = 3);
     template <> void f(double);
     template <> void f(double);
     void g () { f (); }
     void g () { f (); }
 
 
   works, as required.)  An alternative approach would be to look up
   works, as required.)  An alternative approach would be to look up
   the correct default arguments at the call-site, but this approach
   the correct default arguments at the call-site, but this approach
   is consistent with how implicit instantiations are handled.  */
   is consistent with how implicit instantiations are handled.  */
 
 
static void
static void
copy_default_args_to_explicit_spec (tree decl)
copy_default_args_to_explicit_spec (tree decl)
{
{
  tree tmpl;
  tree tmpl;
  tree spec_types;
  tree spec_types;
  tree tmpl_types;
  tree tmpl_types;
  tree new_spec_types;
  tree new_spec_types;
  tree old_type;
  tree old_type;
  tree new_type;
  tree new_type;
  tree t;
  tree t;
  tree object_type = NULL_TREE;
  tree object_type = NULL_TREE;
  tree in_charge = NULL_TREE;
  tree in_charge = NULL_TREE;
  tree vtt = NULL_TREE;
  tree vtt = NULL_TREE;
 
 
  /* See if there's anything we need to do.  */
  /* See if there's anything we need to do.  */
  tmpl = DECL_TI_TEMPLATE (decl);
  tmpl = DECL_TI_TEMPLATE (decl);
  tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
  tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
  for (t = tmpl_types; t; t = TREE_CHAIN (t))
  for (t = tmpl_types; t; t = TREE_CHAIN (t))
    if (TREE_PURPOSE (t))
    if (TREE_PURPOSE (t))
      break;
      break;
  if (!t)
  if (!t)
    return;
    return;
 
 
  old_type = TREE_TYPE (decl);
  old_type = TREE_TYPE (decl);
  spec_types = TYPE_ARG_TYPES (old_type);
  spec_types = TYPE_ARG_TYPES (old_type);
 
 
  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    {
    {
      /* Remove the this pointer, but remember the object's type for
      /* Remove the this pointer, but remember the object's type for
         CV quals.  */
         CV quals.  */
      object_type = TREE_TYPE (TREE_VALUE (spec_types));
      object_type = TREE_TYPE (TREE_VALUE (spec_types));
      spec_types = TREE_CHAIN (spec_types);
      spec_types = TREE_CHAIN (spec_types);
      tmpl_types = TREE_CHAIN (tmpl_types);
      tmpl_types = TREE_CHAIN (tmpl_types);
 
 
      if (DECL_HAS_IN_CHARGE_PARM_P (decl))
      if (DECL_HAS_IN_CHARGE_PARM_P (decl))
        {
        {
          /* DECL may contain more parameters than TMPL due to the extra
          /* DECL may contain more parameters than TMPL due to the extra
             in-charge parameter in constructors and destructors.  */
             in-charge parameter in constructors and destructors.  */
          in_charge = spec_types;
          in_charge = spec_types;
          spec_types = TREE_CHAIN (spec_types);
          spec_types = TREE_CHAIN (spec_types);
        }
        }
      if (DECL_HAS_VTT_PARM_P (decl))
      if (DECL_HAS_VTT_PARM_P (decl))
        {
        {
          vtt = spec_types;
          vtt = spec_types;
          spec_types = TREE_CHAIN (spec_types);
          spec_types = TREE_CHAIN (spec_types);
        }
        }
    }
    }
 
 
  /* Compute the merged default arguments.  */
  /* Compute the merged default arguments.  */
  new_spec_types =
  new_spec_types =
    copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
 
 
  /* Compute the new FUNCTION_TYPE.  */
  /* Compute the new FUNCTION_TYPE.  */
  if (object_type)
  if (object_type)
    {
    {
      if (vtt)
      if (vtt)
        new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
        new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
                                         TREE_VALUE (vtt),
                                         TREE_VALUE (vtt),
                                         new_spec_types);
                                         new_spec_types);
 
 
      if (in_charge)
      if (in_charge)
        /* Put the in-charge parameter back.  */
        /* Put the in-charge parameter back.  */
        new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
        new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
                                         TREE_VALUE (in_charge),
                                         TREE_VALUE (in_charge),
                                         new_spec_types);
                                         new_spec_types);
 
 
      new_type = build_method_type_directly (object_type,
      new_type = build_method_type_directly (object_type,
                                             TREE_TYPE (old_type),
                                             TREE_TYPE (old_type),
                                             new_spec_types);
                                             new_spec_types);
    }
    }
  else
  else
    new_type = build_function_type (TREE_TYPE (old_type),
    new_type = build_function_type (TREE_TYPE (old_type),
                                    new_spec_types);
                                    new_spec_types);
  new_type = cp_build_type_attribute_variant (new_type,
  new_type = cp_build_type_attribute_variant (new_type,
                                              TYPE_ATTRIBUTES (old_type));
                                              TYPE_ATTRIBUTES (old_type));
  new_type = build_exception_variant (new_type,
  new_type = build_exception_variant (new_type,
                                      TYPE_RAISES_EXCEPTIONS (old_type));
                                      TYPE_RAISES_EXCEPTIONS (old_type));
  TREE_TYPE (decl) = new_type;
  TREE_TYPE (decl) = new_type;
}
}
 
 
/* Check to see if the function just declared, as indicated in
/* Check to see if the function just declared, as indicated in
   DECLARATOR, and in DECL, is a specialization of a function
   DECLARATOR, and in DECL, is a specialization of a function
   template.  We may also discover that the declaration is an explicit
   template.  We may also discover that the declaration is an explicit
   instantiation at this point.
   instantiation at this point.
 
 
   Returns DECL, or an equivalent declaration that should be used
   Returns DECL, or an equivalent declaration that should be used
   instead if all goes well.  Issues an error message if something is
   instead if all goes well.  Issues an error message if something is
   amiss.  Returns error_mark_node if the error is not easily
   amiss.  Returns error_mark_node if the error is not easily
   recoverable.
   recoverable.
 
 
   FLAGS is a bitmask consisting of the following flags:
   FLAGS is a bitmask consisting of the following flags:
 
 
   2: The function has a definition.
   2: The function has a definition.
   4: The function is a friend.
   4: The function is a friend.
 
 
   The TEMPLATE_COUNT is the number of references to qualifying
   The TEMPLATE_COUNT is the number of references to qualifying
   template classes that appeared in the name of the function.  For
   template classes that appeared in the name of the function.  For
   example, in
   example, in
 
 
     template <class T> struct S { void f(); };
     template <class T> struct S { void f(); };
     void S<int>::f();
     void S<int>::f();
 
 
   the TEMPLATE_COUNT would be 1.  However, explicitly specialized
   the TEMPLATE_COUNT would be 1.  However, explicitly specialized
   classes are not counted in the TEMPLATE_COUNT, so that in
   classes are not counted in the TEMPLATE_COUNT, so that in
 
 
     template <class T> struct S {};
     template <class T> struct S {};
     template <> struct S<int> { void f(); }
     template <> struct S<int> { void f(); }
     template <> void S<int>::f();
     template <> void S<int>::f();
 
 
   the TEMPLATE_COUNT would be 0.  (Note that this declaration is
   the TEMPLATE_COUNT would be 0.  (Note that this declaration is
   invalid; there should be no template <>.)
   invalid; there should be no template <>.)
 
 
   If the function is a specialization, it is marked as such via
   If the function is a specialization, it is marked as such via
   DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
   DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
   is set up correctly, and it is added to the list of specializations
   is set up correctly, and it is added to the list of specializations
   for that template.  */
   for that template.  */
 
 
tree
tree
check_explicit_specialization (tree declarator,
check_explicit_specialization (tree declarator,
                               tree decl,
                               tree decl,
                               int template_count,
                               int template_count,
                               int flags)
                               int flags)
{
{
  int have_def = flags & 2;
  int have_def = flags & 2;
  int is_friend = flags & 4;
  int is_friend = flags & 4;
  int specialization = 0;
  int specialization = 0;
  int explicit_instantiation = 0;
  int explicit_instantiation = 0;
  int member_specialization = 0;
  int member_specialization = 0;
  tree ctype = DECL_CLASS_CONTEXT (decl);
  tree ctype = DECL_CLASS_CONTEXT (decl);
  tree dname = DECL_NAME (decl);
  tree dname = DECL_NAME (decl);
  tmpl_spec_kind tsk;
  tmpl_spec_kind tsk;
 
 
  if (is_friend)
  if (is_friend)
    {
    {
      if (!processing_specialization)
      if (!processing_specialization)
        tsk = tsk_none;
        tsk = tsk_none;
      else
      else
        tsk = tsk_excessive_parms;
        tsk = tsk_excessive_parms;
    }
    }
  else
  else
    tsk = current_tmpl_spec_kind (template_count);
    tsk = current_tmpl_spec_kind (template_count);
 
 
  switch (tsk)
  switch (tsk)
    {
    {
    case tsk_none:
    case tsk_none:
      if (processing_specialization)
      if (processing_specialization)
        {
        {
          specialization = 1;
          specialization = 1;
          SET_DECL_TEMPLATE_SPECIALIZATION (decl);
          SET_DECL_TEMPLATE_SPECIALIZATION (decl);
        }
        }
      else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
      else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
        {
        {
          if (is_friend)
          if (is_friend)
            /* This could be something like:
            /* This could be something like:
 
 
               template <class T> void f(T);
               template <class T> void f(T);
               class S { friend void f<>(int); }  */
               class S { friend void f<>(int); }  */
            specialization = 1;
            specialization = 1;
          else
          else
            {
            {
              /* This case handles bogus declarations like template <>
              /* This case handles bogus declarations like template <>
                 template <class T> void f<int>(); */
                 template <class T> void f<int>(); */
 
 
              error ("template-id %qD in declaration of primary template",
              error ("template-id %qD in declaration of primary template",
                     declarator);
                     declarator);
              return decl;
              return decl;
            }
            }
        }
        }
      break;
      break;
 
 
    case tsk_invalid_member_spec:
    case tsk_invalid_member_spec:
      /* The error has already been reported in
      /* The error has already been reported in
         check_specialization_scope.  */
         check_specialization_scope.  */
      return error_mark_node;
      return error_mark_node;
 
 
    case tsk_invalid_expl_inst:
    case tsk_invalid_expl_inst:
      error ("template parameter list used in explicit instantiation");
      error ("template parameter list used in explicit instantiation");
 
 
      /* Fall through.  */
      /* Fall through.  */
 
 
    case tsk_expl_inst:
    case tsk_expl_inst:
      if (have_def)
      if (have_def)
        error ("definition provided for explicit instantiation");
        error ("definition provided for explicit instantiation");
 
 
      explicit_instantiation = 1;
      explicit_instantiation = 1;
      break;
      break;
 
 
    case tsk_excessive_parms:
    case tsk_excessive_parms:
    case tsk_insufficient_parms:
    case tsk_insufficient_parms:
      if (tsk == tsk_excessive_parms)
      if (tsk == tsk_excessive_parms)
        error ("too many template parameter lists in declaration of %qD",
        error ("too many template parameter lists in declaration of %qD",
               decl);
               decl);
      else if (template_header_count)
      else if (template_header_count)
        error("too few template parameter lists in declaration of %qD", decl);
        error("too few template parameter lists in declaration of %qD", decl);
      else
      else
        error("explicit specialization of %qD must be introduced by "
        error("explicit specialization of %qD must be introduced by "
              "%<template <>%>", decl);
              "%<template <>%>", decl);
 
 
      /* Fall through.  */
      /* Fall through.  */
    case tsk_expl_spec:
    case tsk_expl_spec:
      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
      if (ctype)
      if (ctype)
        member_specialization = 1;
        member_specialization = 1;
      else
      else
        specialization = 1;
        specialization = 1;
      break;
      break;
 
 
    case tsk_template:
    case tsk_template:
      if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
      if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
        {
        {
          /* This case handles bogus declarations like template <>
          /* This case handles bogus declarations like template <>
             template <class T> void f<int>(); */
             template <class T> void f<int>(); */
 
 
          if (uses_template_parms (declarator))
          if (uses_template_parms (declarator))
            error ("function template partial specialization %qD "
            error ("function template partial specialization %qD "
                   "is not allowed", declarator);
                   "is not allowed", declarator);
          else
          else
            error ("template-id %qD in declaration of primary template",
            error ("template-id %qD in declaration of primary template",
                   declarator);
                   declarator);
          return decl;
          return decl;
        }
        }
 
 
      if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
      if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
        /* This is a specialization of a member template, without
        /* This is a specialization of a member template, without
           specialization the containing class.  Something like:
           specialization the containing class.  Something like:
 
 
             template <class T> struct S {
             template <class T> struct S {
               template <class U> void f (U);
               template <class U> void f (U);
             };
             };
             template <> template <class U> void S<int>::f(U) {}
             template <> template <class U> void S<int>::f(U) {}
 
 
           That's a specialization -- but of the entire template.  */
           That's a specialization -- but of the entire template.  */
        specialization = 1;
        specialization = 1;
      break;
      break;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
 
 
  if (specialization || member_specialization)
  if (specialization || member_specialization)
    {
    {
      tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
      tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
      for (; t; t = TREE_CHAIN (t))
      for (; t; t = TREE_CHAIN (t))
        if (TREE_PURPOSE (t))
        if (TREE_PURPOSE (t))
          {
          {
            permerror (input_location,
            permerror (input_location,
                       "default argument specified in explicit specialization");
                       "default argument specified in explicit specialization");
            break;
            break;
          }
          }
    }
    }
 
 
  if (specialization || member_specialization || explicit_instantiation)
  if (specialization || member_specialization || explicit_instantiation)
    {
    {
      tree tmpl = NULL_TREE;
      tree tmpl = NULL_TREE;
      tree targs = NULL_TREE;
      tree targs = NULL_TREE;
 
 
      /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
      /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
      if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
      if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
        {
        {
          tree fns;
          tree fns;
 
 
          gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
          gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
          if (ctype)
          if (ctype)
            fns = dname;
            fns = dname;
          else
          else
            {
            {
              /* If there is no class context, the explicit instantiation
              /* If there is no class context, the explicit instantiation
                 must be at namespace scope.  */
                 must be at namespace scope.  */
              gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
              gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
 
 
              /* Find the namespace binding, using the declaration
              /* Find the namespace binding, using the declaration
                 context.  */
                 context.  */
              fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
              fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
                                           false, true);
                                           false, true);
              if (fns == error_mark_node || !is_overloaded_fn (fns))
              if (fns == error_mark_node || !is_overloaded_fn (fns))
                {
                {
                  error ("%qD is not a template function", dname);
                  error ("%qD is not a template function", dname);
                  fns = error_mark_node;
                  fns = error_mark_node;
                }
                }
              else
              else
                {
                {
                  tree fn = OVL_CURRENT (fns);
                  tree fn = OVL_CURRENT (fns);
                  if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
                  if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
                                                CP_DECL_CONTEXT (fn)))
                                                CP_DECL_CONTEXT (fn)))
                    error ("%qD is not declared in %qD",
                    error ("%qD is not declared in %qD",
                           decl, current_namespace);
                           decl, current_namespace);
                }
                }
            }
            }
 
 
          declarator = lookup_template_function (fns, NULL_TREE);
          declarator = lookup_template_function (fns, NULL_TREE);
        }
        }
 
 
      if (declarator == error_mark_node)
      if (declarator == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
      if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
        {
        {
          if (!explicit_instantiation)
          if (!explicit_instantiation)
            /* A specialization in class scope.  This is invalid,
            /* A specialization in class scope.  This is invalid,
               but the error will already have been flagged by
               but the error will already have been flagged by
               check_specialization_scope.  */
               check_specialization_scope.  */
            return error_mark_node;
            return error_mark_node;
          else
          else
            {
            {
              /* It's not valid to write an explicit instantiation in
              /* It's not valid to write an explicit instantiation in
                 class scope, e.g.:
                 class scope, e.g.:
 
 
                   class C { template void f(); }
                   class C { template void f(); }
 
 
                   This case is caught by the parser.  However, on
                   This case is caught by the parser.  However, on
                   something like:
                   something like:
 
 
                   template class C { void f(); };
                   template class C { void f(); };
 
 
                   (which is invalid) we can get here.  The error will be
                   (which is invalid) we can get here.  The error will be
                   issued later.  */
                   issued later.  */
              ;
              ;
            }
            }
 
 
          return decl;
          return decl;
        }
        }
      else if (ctype != NULL_TREE
      else if (ctype != NULL_TREE
               && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
               && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
                   IDENTIFIER_NODE))
                   IDENTIFIER_NODE))
        {
        {
          /* Find the list of functions in ctype that have the same
          /* Find the list of functions in ctype that have the same
             name as the declared function.  */
             name as the declared function.  */
          tree name = TREE_OPERAND (declarator, 0);
          tree name = TREE_OPERAND (declarator, 0);
          tree fns = NULL_TREE;
          tree fns = NULL_TREE;
          int idx;
          int idx;
 
 
          if (constructor_name_p (name, ctype))
          if (constructor_name_p (name, ctype))
            {
            {
              int is_constructor = DECL_CONSTRUCTOR_P (decl);
              int is_constructor = DECL_CONSTRUCTOR_P (decl);
 
 
              if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
              if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
                  : !CLASSTYPE_DESTRUCTORS (ctype))
                  : !CLASSTYPE_DESTRUCTORS (ctype))
                {
                {
                  /* From [temp.expl.spec]:
                  /* From [temp.expl.spec]:
 
 
                     If such an explicit specialization for the member
                     If such an explicit specialization for the member
                     of a class template names an implicitly-declared
                     of a class template names an implicitly-declared
                     special member function (clause _special_), the
                     special member function (clause _special_), the
                     program is ill-formed.
                     program is ill-formed.
 
 
                     Similar language is found in [temp.explicit].  */
                     Similar language is found in [temp.explicit].  */
                  error ("specialization of implicitly-declared special member function");
                  error ("specialization of implicitly-declared special member function");
                  return error_mark_node;
                  return error_mark_node;
                }
                }
 
 
              name = is_constructor ? ctor_identifier : dtor_identifier;
              name = is_constructor ? ctor_identifier : dtor_identifier;
            }
            }
 
 
          if (!DECL_CONV_FN_P (decl))
          if (!DECL_CONV_FN_P (decl))
            {
            {
              idx = lookup_fnfields_1 (ctype, name);
              idx = lookup_fnfields_1 (ctype, name);
              if (idx >= 0)
              if (idx >= 0)
                fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
                fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
            }
            }
          else
          else
            {
            {
              VEC(tree,gc) *methods;
              VEC(tree,gc) *methods;
              tree ovl;
              tree ovl;
 
 
              /* For a type-conversion operator, we cannot do a
              /* For a type-conversion operator, we cannot do a
                 name-based lookup.  We might be looking for `operator
                 name-based lookup.  We might be looking for `operator
                 int' which will be a specialization of `operator T'.
                 int' which will be a specialization of `operator T'.
                 So, we find *all* the conversion operators, and then
                 So, we find *all* the conversion operators, and then
                 select from them.  */
                 select from them.  */
              fns = NULL_TREE;
              fns = NULL_TREE;
 
 
              methods = CLASSTYPE_METHOD_VEC (ctype);
              methods = CLASSTYPE_METHOD_VEC (ctype);
              if (methods)
              if (methods)
                for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
                for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
                     VEC_iterate (tree, methods, idx, ovl);
                     VEC_iterate (tree, methods, idx, ovl);
                     ++idx)
                     ++idx)
                  {
                  {
                    if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
                    if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
                      /* There are no more conversion functions.  */
                      /* There are no more conversion functions.  */
                      break;
                      break;
 
 
                    /* Glue all these conversion functions together
                    /* Glue all these conversion functions together
                       with those we already have.  */
                       with those we already have.  */
                    for (; ovl; ovl = OVL_NEXT (ovl))
                    for (; ovl; ovl = OVL_NEXT (ovl))
                      fns = ovl_cons (OVL_CURRENT (ovl), fns);
                      fns = ovl_cons (OVL_CURRENT (ovl), fns);
                  }
                  }
            }
            }
 
 
          if (fns == NULL_TREE)
          if (fns == NULL_TREE)
            {
            {
              error ("no member function %qD declared in %qT", name, ctype);
              error ("no member function %qD declared in %qT", name, ctype);
              return error_mark_node;
              return error_mark_node;
            }
            }
          else
          else
            TREE_OPERAND (declarator, 0) = fns;
            TREE_OPERAND (declarator, 0) = fns;
        }
        }
 
 
      /* Figure out what exactly is being specialized at this point.
      /* Figure out what exactly is being specialized at this point.
         Note that for an explicit instantiation, even one for a
         Note that for an explicit instantiation, even one for a
         member function, we cannot tell apriori whether the
         member function, we cannot tell apriori whether the
         instantiation is for a member template, or just a member
         instantiation is for a member template, or just a member
         function of a template class.  Even if a member template is
         function of a template class.  Even if a member template is
         being instantiated, the member template arguments may be
         being instantiated, the member template arguments may be
         elided if they can be deduced from the rest of the
         elided if they can be deduced from the rest of the
         declaration.  */
         declaration.  */
      tmpl = determine_specialization (declarator, decl,
      tmpl = determine_specialization (declarator, decl,
                                       &targs,
                                       &targs,
                                       member_specialization,
                                       member_specialization,
                                       template_count,
                                       template_count,
                                       tsk);
                                       tsk);
 
 
      if (!tmpl || tmpl == error_mark_node)
      if (!tmpl || tmpl == error_mark_node)
        /* We couldn't figure out what this declaration was
        /* We couldn't figure out what this declaration was
           specializing.  */
           specializing.  */
        return error_mark_node;
        return error_mark_node;
      else
      else
        {
        {
          tree gen_tmpl = most_general_template (tmpl);
          tree gen_tmpl = most_general_template (tmpl);
 
 
          if (explicit_instantiation)
          if (explicit_instantiation)
            {
            {
              /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
              /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
                 is done by do_decl_instantiation later.  */
                 is done by do_decl_instantiation later.  */
 
 
              int arg_depth = TMPL_ARGS_DEPTH (targs);
              int arg_depth = TMPL_ARGS_DEPTH (targs);
              int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
              int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
 
 
              if (arg_depth > parm_depth)
              if (arg_depth > parm_depth)
                {
                {
                  /* If TMPL is not the most general template (for
                  /* If TMPL is not the most general template (for
                     example, if TMPL is a friend template that is
                     example, if TMPL is a friend template that is
                     injected into namespace scope), then there will
                     injected into namespace scope), then there will
                     be too many levels of TARGS.  Remove some of them
                     be too many levels of TARGS.  Remove some of them
                     here.  */
                     here.  */
                  int i;
                  int i;
                  tree new_targs;
                  tree new_targs;
 
 
                  new_targs = make_tree_vec (parm_depth);
                  new_targs = make_tree_vec (parm_depth);
                  for (i = arg_depth - parm_depth; i < arg_depth; ++i)
                  for (i = arg_depth - parm_depth; i < arg_depth; ++i)
                    TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
                    TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
                      = TREE_VEC_ELT (targs, i);
                      = TREE_VEC_ELT (targs, i);
                  targs = new_targs;
                  targs = new_targs;
                }
                }
 
 
              return instantiate_template (tmpl, targs, tf_error);
              return instantiate_template (tmpl, targs, tf_error);
            }
            }
 
 
          /* If we thought that the DECL was a member function, but it
          /* If we thought that the DECL was a member function, but it
             turns out to be specializing a static member function,
             turns out to be specializing a static member function,
             make DECL a static member function as well.  */
             make DECL a static member function as well.  */
          if (DECL_STATIC_FUNCTION_P (tmpl)
          if (DECL_STATIC_FUNCTION_P (tmpl)
              && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
              && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
            revert_static_member_fn (decl);
            revert_static_member_fn (decl);
 
 
          /* If this is a specialization of a member template of a
          /* If this is a specialization of a member template of a
             template class, we want to return the TEMPLATE_DECL, not
             template class, we want to return the TEMPLATE_DECL, not
             the specialization of it.  */
             the specialization of it.  */
          if (tsk == tsk_template)
          if (tsk == tsk_template)
            {
            {
              tree result = DECL_TEMPLATE_RESULT (tmpl);
              tree result = DECL_TEMPLATE_RESULT (tmpl);
              SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
              SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
              DECL_INITIAL (result) = NULL_TREE;
              DECL_INITIAL (result) = NULL_TREE;
              if (have_def)
              if (have_def)
                {
                {
                  tree parm;
                  tree parm;
                  DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
                  DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
                  DECL_SOURCE_LOCATION (result)
                  DECL_SOURCE_LOCATION (result)
                    = DECL_SOURCE_LOCATION (decl);
                    = DECL_SOURCE_LOCATION (decl);
                  /* We want to use the argument list specified in the
                  /* We want to use the argument list specified in the
                     definition, not in the original declaration.  */
                     definition, not in the original declaration.  */
                  DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
                  DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
                  for (parm = DECL_ARGUMENTS (result); parm;
                  for (parm = DECL_ARGUMENTS (result); parm;
                       parm = TREE_CHAIN (parm))
                       parm = TREE_CHAIN (parm))
                    DECL_CONTEXT (parm) = result;
                    DECL_CONTEXT (parm) = result;
                }
                }
              return register_specialization (tmpl, gen_tmpl, targs,
              return register_specialization (tmpl, gen_tmpl, targs,
                                              is_friend, 0);
                                              is_friend, 0);
            }
            }
 
 
          /* Set up the DECL_TEMPLATE_INFO for DECL.  */
          /* Set up the DECL_TEMPLATE_INFO for DECL.  */
          DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
          DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
 
 
          /* Inherit default function arguments from the template
          /* Inherit default function arguments from the template
             DECL is specializing.  */
             DECL is specializing.  */
          copy_default_args_to_explicit_spec (decl);
          copy_default_args_to_explicit_spec (decl);
 
 
          /* This specialization has the same protection as the
          /* This specialization has the same protection as the
             template it specializes.  */
             template it specializes.  */
          TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
          TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
          TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
          TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
 
 
          /* 7.1.1-1 [dcl.stc]
          /* 7.1.1-1 [dcl.stc]
 
 
             A storage-class-specifier shall not be specified in an
             A storage-class-specifier shall not be specified in an
             explicit specialization...
             explicit specialization...
 
 
             The parser rejects these, so unless action is taken here,
             The parser rejects these, so unless action is taken here,
             explicit function specializations will always appear with
             explicit function specializations will always appear with
             global linkage.
             global linkage.
 
 
             The action recommended by the C++ CWG in response to C++
             The action recommended by the C++ CWG in response to C++
             defect report 605 is to make the storage class and linkage
             defect report 605 is to make the storage class and linkage
             of the explicit specialization match the templated function:
             of the explicit specialization match the templated function:
 
 
             http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
             http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
           */
           */
          if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
          if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
            {
            {
              tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
              tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
              gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
              gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
 
 
              /* This specialization has the same linkage and visibility as
              /* This specialization has the same linkage and visibility as
                 the function template it specializes.  */
                 the function template it specializes.  */
              TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
              TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
              if (! TREE_PUBLIC (decl))
              if (! TREE_PUBLIC (decl))
                {
                {
                  DECL_INTERFACE_KNOWN (decl) = 1;
                  DECL_INTERFACE_KNOWN (decl) = 1;
                  DECL_NOT_REALLY_EXTERN (decl) = 1;
                  DECL_NOT_REALLY_EXTERN (decl) = 1;
                }
                }
              DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
              DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
              if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
              if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
                {
                {
                  DECL_VISIBILITY_SPECIFIED (decl) = 1;
                  DECL_VISIBILITY_SPECIFIED (decl) = 1;
                  DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
                  DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
                }
                }
            }
            }
 
 
          /* If DECL is a friend declaration, declared using an
          /* If DECL is a friend declaration, declared using an
             unqualified name, the namespace associated with DECL may
             unqualified name, the namespace associated with DECL may
             have been set incorrectly.  For example, in:
             have been set incorrectly.  For example, in:
 
 
               template <typename T> void f(T);
               template <typename T> void f(T);
               namespace N {
               namespace N {
                 struct S { friend void f<int>(int); }
                 struct S { friend void f<int>(int); }
               }
               }
 
 
             we will have set the DECL_CONTEXT for the friend
             we will have set the DECL_CONTEXT for the friend
             declaration to N, rather than to the global namespace.  */
             declaration to N, rather than to the global namespace.  */
          if (DECL_NAMESPACE_SCOPE_P (decl))
          if (DECL_NAMESPACE_SCOPE_P (decl))
            DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
            DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
 
 
          if (is_friend && !have_def)
          if (is_friend && !have_def)
            /* This is not really a declaration of a specialization.
            /* This is not really a declaration of a specialization.
               It's just the name of an instantiation.  But, it's not
               It's just the name of an instantiation.  But, it's not
               a request for an instantiation, either.  */
               a request for an instantiation, either.  */
            SET_DECL_IMPLICIT_INSTANTIATION (decl);
            SET_DECL_IMPLICIT_INSTANTIATION (decl);
          else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
          else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
            /* This is indeed a specialization.  In case of constructors
            /* This is indeed a specialization.  In case of constructors
               and destructors, we need in-charge and not-in-charge
               and destructors, we need in-charge and not-in-charge
               versions in V3 ABI.  */
               versions in V3 ABI.  */
            clone_function_decl (decl, /*update_method_vec_p=*/0);
            clone_function_decl (decl, /*update_method_vec_p=*/0);
 
 
          /* Register this specialization so that we can find it
          /* Register this specialization so that we can find it
             again.  */
             again.  */
          decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
          decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
        }
        }
    }
    }
 
 
  return decl;
  return decl;
}
}
 
 
/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
   parameters.  These are represented in the same format used for
   parameters.  These are represented in the same format used for
   DECL_TEMPLATE_PARMS.  */
   DECL_TEMPLATE_PARMS.  */
 
 
int
int
comp_template_parms (const_tree parms1, const_tree parms2)
comp_template_parms (const_tree parms1, const_tree parms2)
{
{
  const_tree p1;
  const_tree p1;
  const_tree p2;
  const_tree p2;
 
 
  if (parms1 == parms2)
  if (parms1 == parms2)
    return 1;
    return 1;
 
 
  for (p1 = parms1, p2 = parms2;
  for (p1 = parms1, p2 = parms2;
       p1 != NULL_TREE && p2 != NULL_TREE;
       p1 != NULL_TREE && p2 != NULL_TREE;
       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
    {
    {
      tree t1 = TREE_VALUE (p1);
      tree t1 = TREE_VALUE (p1);
      tree t2 = TREE_VALUE (p2);
      tree t2 = TREE_VALUE (p2);
      int i;
      int i;
 
 
      gcc_assert (TREE_CODE (t1) == TREE_VEC);
      gcc_assert (TREE_CODE (t1) == TREE_VEC);
      gcc_assert (TREE_CODE (t2) == TREE_VEC);
      gcc_assert (TREE_CODE (t2) == TREE_VEC);
 
 
      if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
      if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
        return 0;
        return 0;
 
 
      for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
      for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
        {
        {
          tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
          tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
          tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
          tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
 
 
          /* If either of the template parameters are invalid, assume
          /* If either of the template parameters are invalid, assume
             they match for the sake of error recovery. */
             they match for the sake of error recovery. */
          if (parm1 == error_mark_node || parm2 == error_mark_node)
          if (parm1 == error_mark_node || parm2 == error_mark_node)
            return 1;
            return 1;
 
 
          if (TREE_CODE (parm1) != TREE_CODE (parm2))
          if (TREE_CODE (parm1) != TREE_CODE (parm2))
            return 0;
            return 0;
 
 
          if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
          if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
              && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
              && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
                  == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
                  == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
            continue;
            continue;
          else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
          else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
            return 0;
            return 0;
        }
        }
    }
    }
 
 
  if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
  if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
    /* One set of parameters has more parameters lists than the
    /* One set of parameters has more parameters lists than the
       other.  */
       other.  */
    return 0;
    return 0;
 
 
  return 1;
  return 1;
}
}
 
 
/* Determine whether PARM is a parameter pack.  */
/* Determine whether PARM is a parameter pack.  */
 
 
bool
bool
template_parameter_pack_p (const_tree parm)
template_parameter_pack_p (const_tree parm)
{
{
  /* Determine if we have a non-type template parameter pack.  */
  /* Determine if we have a non-type template parameter pack.  */
  if (TREE_CODE (parm) == PARM_DECL)
  if (TREE_CODE (parm) == PARM_DECL)
    return (DECL_TEMPLATE_PARM_P (parm)
    return (DECL_TEMPLATE_PARM_P (parm)
            && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
            && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
 
 
  /* If this is a list of template parameters, we could get a
  /* If this is a list of template parameters, we could get a
     TYPE_DECL or a TEMPLATE_DECL.  */
     TYPE_DECL or a TEMPLATE_DECL.  */
  if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
  if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    parm = TREE_TYPE (parm);
    parm = TREE_TYPE (parm);
 
 
  return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
  return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
           || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
           || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
          && TEMPLATE_TYPE_PARAMETER_PACK (parm));
          && TEMPLATE_TYPE_PARAMETER_PACK (parm));
}
}
 
 
/* Determine if T is a function parameter pack.  */
/* Determine if T is a function parameter pack.  */
 
 
bool
bool
function_parameter_pack_p (const_tree t)
function_parameter_pack_p (const_tree t)
{
{
  if (t && TREE_CODE (t) == PARM_DECL)
  if (t && TREE_CODE (t) == PARM_DECL)
    return FUNCTION_PARAMETER_PACK_P (t);
    return FUNCTION_PARAMETER_PACK_P (t);
  return false;
  return false;
}
}
 
 
/* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
/* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
   PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
   PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
 
 
tree
tree
get_function_template_decl (const_tree primary_func_tmpl_inst)
get_function_template_decl (const_tree primary_func_tmpl_inst)
{
{
  if (! primary_func_tmpl_inst
  if (! primary_func_tmpl_inst
      || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
      || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
      || ! primary_template_instantiation_p (primary_func_tmpl_inst))
      || ! primary_template_instantiation_p (primary_func_tmpl_inst))
    return NULL;
    return NULL;
 
 
  return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
  return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
}
}
 
 
/* Return true iff the function parameter PARAM_DECL was expanded
/* Return true iff the function parameter PARAM_DECL was expanded
   from the function parameter pack PACK.  */
   from the function parameter pack PACK.  */
 
 
bool
bool
function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
{
{
  if (DECL_ARTIFICIAL (param_decl)
  if (DECL_ARTIFICIAL (param_decl)
      || !function_parameter_pack_p (pack))
      || !function_parameter_pack_p (pack))
    return false;
    return false;
 
 
  /* The parameter pack and its pack arguments have the same
  /* The parameter pack and its pack arguments have the same
     DECL_PARM_INDEX.  */
     DECL_PARM_INDEX.  */
  return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
  return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
}
}
 
 
/* Determine whether ARGS describes a variadic template args list,
/* Determine whether ARGS describes a variadic template args list,
   i.e., one that is terminated by a template argument pack.  */
   i.e., one that is terminated by a template argument pack.  */
 
 
static bool
static bool
template_args_variadic_p (tree args)
template_args_variadic_p (tree args)
{
{
  int nargs;
  int nargs;
  tree last_parm;
  tree last_parm;
 
 
  if (args == NULL_TREE)
  if (args == NULL_TREE)
    return false;
    return false;
 
 
  args = INNERMOST_TEMPLATE_ARGS (args);
  args = INNERMOST_TEMPLATE_ARGS (args);
  nargs = TREE_VEC_LENGTH (args);
  nargs = TREE_VEC_LENGTH (args);
 
 
  if (nargs == 0)
  if (nargs == 0)
    return false;
    return false;
 
 
  last_parm = TREE_VEC_ELT (args, nargs - 1);
  last_parm = TREE_VEC_ELT (args, nargs - 1);
 
 
  return ARGUMENT_PACK_P (last_parm);
  return ARGUMENT_PACK_P (last_parm);
}
}
 
 
/* Generate a new name for the parameter pack name NAME (an
/* Generate a new name for the parameter pack name NAME (an
   IDENTIFIER_NODE) that incorporates its */
   IDENTIFIER_NODE) that incorporates its */
 
 
static tree
static tree
make_ith_pack_parameter_name (tree name, int i)
make_ith_pack_parameter_name (tree name, int i)
{
{
  /* Munge the name to include the parameter index.  */
  /* Munge the name to include the parameter index.  */
#define NUMBUF_LEN 128
#define NUMBUF_LEN 128
  char numbuf[NUMBUF_LEN];
  char numbuf[NUMBUF_LEN];
  char* newname;
  char* newname;
  int newname_len;
  int newname_len;
 
 
  snprintf (numbuf, NUMBUF_LEN, "%i", i);
  snprintf (numbuf, NUMBUF_LEN, "%i", i);
  newname_len = IDENTIFIER_LENGTH (name)
  newname_len = IDENTIFIER_LENGTH (name)
                + strlen (numbuf) + 2;
                + strlen (numbuf) + 2;
  newname = (char*)alloca (newname_len);
  newname = (char*)alloca (newname_len);
  snprintf (newname, newname_len,
  snprintf (newname, newname_len,
            "%s#%i", IDENTIFIER_POINTER (name), i);
            "%s#%i", IDENTIFIER_POINTER (name), i);
  return get_identifier (newname);
  return get_identifier (newname);
}
}
 
 
/* Return true if T is a primary function
/* Return true if T is a primary function
   or class template instantiation.  */
   or class template instantiation.  */
 
 
bool
bool
primary_template_instantiation_p (const_tree t)
primary_template_instantiation_p (const_tree t)
{
{
  if (!t)
  if (!t)
    return false;
    return false;
 
 
  if (TREE_CODE (t) == FUNCTION_DECL)
  if (TREE_CODE (t) == FUNCTION_DECL)
    return DECL_LANG_SPECIFIC (t)
    return DECL_LANG_SPECIFIC (t)
           && DECL_TEMPLATE_INSTANTIATION (t)
           && DECL_TEMPLATE_INSTANTIATION (t)
           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
  else if (CLASS_TYPE_P (t))
  else if (CLASS_TYPE_P (t))
    return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
    return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
           && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
           && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
  return false;
  return false;
}
}
 
 
/* Return true if PARM is a template template parameter.  */
/* Return true if PARM is a template template parameter.  */
 
 
bool
bool
template_template_parameter_p (const_tree parm)
template_template_parameter_p (const_tree parm)
{
{
  return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
  return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
}
}
 
 
/* Return the template parameters of T if T is a
/* Return the template parameters of T if T is a
   primary template instantiation, NULL otherwise.  */
   primary template instantiation, NULL otherwise.  */
 
 
tree
tree
get_primary_template_innermost_parameters (const_tree t)
get_primary_template_innermost_parameters (const_tree t)
{
{
  tree parms = NULL, template_info = NULL;
  tree parms = NULL, template_info = NULL;
 
 
  if ((template_info = get_template_info (t))
  if ((template_info = get_template_info (t))
      && primary_template_instantiation_p (t))
      && primary_template_instantiation_p (t))
    parms = INNERMOST_TEMPLATE_PARMS
    parms = INNERMOST_TEMPLATE_PARMS
        (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
        (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
 
 
  return parms;
  return parms;
}
}
 
 
/* Return the template parameters of the LEVELth level from the full list
/* Return the template parameters of the LEVELth level from the full list
   of template parameters PARMS.  */
   of template parameters PARMS.  */
 
 
tree
tree
get_template_parms_at_level (tree parms, int level)
get_template_parms_at_level (tree parms, int level)
{
{
  tree p;
  tree p;
  if (!parms
  if (!parms
      || TREE_CODE (parms) != TREE_LIST
      || TREE_CODE (parms) != TREE_LIST
      || level > TMPL_PARMS_DEPTH (parms))
      || level > TMPL_PARMS_DEPTH (parms))
    return NULL_TREE;
    return NULL_TREE;
 
 
  for (p = parms; p; p = TREE_CHAIN (p))
  for (p = parms; p; p = TREE_CHAIN (p))
    if (TMPL_PARMS_DEPTH (p) == level)
    if (TMPL_PARMS_DEPTH (p) == level)
      return p;
      return p;
 
 
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* Returns the template arguments of T if T is a template instantiation,
/* Returns the template arguments of T if T is a template instantiation,
   NULL otherwise.  */
   NULL otherwise.  */
 
 
tree
tree
get_template_innermost_arguments (const_tree t)
get_template_innermost_arguments (const_tree t)
{
{
  tree args = NULL, template_info = NULL;
  tree args = NULL, template_info = NULL;
 
 
  if ((template_info = get_template_info (t))
  if ((template_info = get_template_info (t))
      && TI_ARGS (template_info))
      && TI_ARGS (template_info))
    args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
 
 
  return args;
  return args;
}
}
 
 
/* Return the argument pack elements of T if T is a template argument pack,
/* Return the argument pack elements of T if T is a template argument pack,
   NULL otherwise.  */
   NULL otherwise.  */
 
 
tree
tree
get_template_argument_pack_elems (const_tree t)
get_template_argument_pack_elems (const_tree t)
{
{
  if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
  if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
      && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
      && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    return NULL;
    return NULL;
 
 
  return ARGUMENT_PACK_ARGS (t);
  return ARGUMENT_PACK_ARGS (t);
}
}
 
 
/* Structure used to track the progress of find_parameter_packs_r.  */
/* Structure used to track the progress of find_parameter_packs_r.  */
struct find_parameter_pack_data
struct find_parameter_pack_data
{
{
  /* TREE_LIST that will contain all of the parameter packs found by
  /* TREE_LIST that will contain all of the parameter packs found by
     the traversal.  */
     the traversal.  */
  tree* parameter_packs;
  tree* parameter_packs;
 
 
  /* Set of AST nodes that have been visited by the traversal.  */
  /* Set of AST nodes that have been visited by the traversal.  */
  struct pointer_set_t *visited;
  struct pointer_set_t *visited;
};
};
 
 
/* Identifies all of the argument packs that occur in a template
/* Identifies all of the argument packs that occur in a template
   argument and appends them to the TREE_LIST inside DATA, which is a
   argument and appends them to the TREE_LIST inside DATA, which is a
   find_parameter_pack_data structure. This is a subroutine of
   find_parameter_pack_data structure. This is a subroutine of
   make_pack_expansion and uses_parameter_packs.  */
   make_pack_expansion and uses_parameter_packs.  */
static tree
static tree
find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
{
{
  tree t = *tp;
  tree t = *tp;
  struct find_parameter_pack_data* ppd =
  struct find_parameter_pack_data* ppd =
    (struct find_parameter_pack_data*)data;
    (struct find_parameter_pack_data*)data;
  bool parameter_pack_p = false;
  bool parameter_pack_p = false;
 
 
  /* Identify whether this is a parameter pack or not.  */
  /* Identify whether this is a parameter pack or not.  */
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      if (TEMPLATE_PARM_PARAMETER_PACK (t))
      if (TEMPLATE_PARM_PARAMETER_PACK (t))
        parameter_pack_p = true;
        parameter_pack_p = true;
      break;
      break;
 
 
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
      if (TEMPLATE_TYPE_PARAMETER_PACK (t))
      if (TEMPLATE_TYPE_PARAMETER_PACK (t))
        parameter_pack_p = true;
        parameter_pack_p = true;
      break;
      break;
 
 
    case PARM_DECL:
    case PARM_DECL:
      if (FUNCTION_PARAMETER_PACK_P (t))
      if (FUNCTION_PARAMETER_PACK_P (t))
        {
        {
          /* We don't want to walk into the type of a PARM_DECL,
          /* We don't want to walk into the type of a PARM_DECL,
             because we don't want to see the type parameter pack.  */
             because we don't want to see the type parameter pack.  */
          *walk_subtrees = 0;
          *walk_subtrees = 0;
          parameter_pack_p = true;
          parameter_pack_p = true;
        }
        }
      break;
      break;
 
 
    default:
    default:
      /* Not a parameter pack.  */
      /* Not a parameter pack.  */
      break;
      break;
    }
    }
 
 
  if (parameter_pack_p)
  if (parameter_pack_p)
    {
    {
      /* Add this parameter pack to the list.  */
      /* Add this parameter pack to the list.  */
      *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
      *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    }
    }
 
 
  if (TYPE_P (t))
  if (TYPE_P (t))
    cp_walk_tree (&TYPE_CONTEXT (t),
    cp_walk_tree (&TYPE_CONTEXT (t),
                  &find_parameter_packs_r, ppd, ppd->visited);
                  &find_parameter_packs_r, ppd, ppd->visited);
 
 
  /* This switch statement will return immediately if we don't find a
  /* This switch statement will return immediately if we don't find a
     parameter pack.  */
     parameter pack.  */
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      return NULL_TREE;
      return NULL_TREE;
 
 
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
      /* Check the template itself.  */
      /* Check the template itself.  */
      cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
      cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
                    &find_parameter_packs_r, ppd, ppd->visited);
                    &find_parameter_packs_r, ppd, ppd->visited);
      /* Check the template arguments.  */
      /* Check the template arguments.  */
      cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
      cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
                    ppd->visited);
                    ppd->visited);
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
      return NULL_TREE;
      return NULL_TREE;
 
 
    case PARM_DECL:
    case PARM_DECL:
      return NULL_TREE;
      return NULL_TREE;
 
 
    case RECORD_TYPE:
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
      if (TYPE_PTRMEMFUNC_P (t))
        return NULL_TREE;
        return NULL_TREE;
      /* Fall through.  */
      /* Fall through.  */
 
 
    case UNION_TYPE:
    case UNION_TYPE:
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
      if (TYPE_TEMPLATE_INFO (t))
      if (TYPE_TEMPLATE_INFO (t))
        cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
        cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
                      &find_parameter_packs_r, ppd, ppd->visited);
                      &find_parameter_packs_r, ppd, ppd->visited);
 
 
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      cp_walk_tree (&TREE_TYPE (t),
      cp_walk_tree (&TREE_TYPE (t),
                    &find_parameter_packs_r, ppd, ppd->visited);
                    &find_parameter_packs_r, ppd, ppd->visited);
      return NULL_TREE;
      return NULL_TREE;
 
 
    case TYPENAME_TYPE:
    case TYPENAME_TYPE:
      cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
      cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
                   ppd, ppd->visited);
                   ppd, ppd->visited);
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    case TYPE_PACK_EXPANSION:
    case TYPE_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    case INTEGER_TYPE:
    case INTEGER_TYPE:
      cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
      cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
                    ppd, ppd->visited);
                    ppd, ppd->visited);
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
      cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
      cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
                    ppd->visited);
                    ppd->visited);
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      return NULL_TREE;
      return NULL_TREE;
 
 
    default:
    default:
      return NULL_TREE;
      return NULL_TREE;
    }
    }
 
 
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* Determines if the expression or type T uses any parameter packs.  */
/* Determines if the expression or type T uses any parameter packs.  */
bool
bool
uses_parameter_packs (tree t)
uses_parameter_packs (tree t)
{
{
  tree parameter_packs = NULL_TREE;
  tree parameter_packs = NULL_TREE;
  struct find_parameter_pack_data ppd;
  struct find_parameter_pack_data ppd;
  ppd.parameter_packs = &parameter_packs;
  ppd.parameter_packs = &parameter_packs;
  ppd.visited = pointer_set_create ();
  ppd.visited = pointer_set_create ();
  cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
  cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
  pointer_set_destroy (ppd.visited);
  pointer_set_destroy (ppd.visited);
  return parameter_packs != NULL_TREE;
  return parameter_packs != NULL_TREE;
}
}
 
 
/* Turn ARG, which may be an expression, type, or a TREE_LIST
/* Turn ARG, which may be an expression, type, or a TREE_LIST
   representation a base-class initializer into a parameter pack
   representation a base-class initializer into a parameter pack
   expansion. If all goes well, the resulting node will be an
   expansion. If all goes well, the resulting node will be an
   EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
   EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
   respectively.  */
   respectively.  */
tree
tree
make_pack_expansion (tree arg)
make_pack_expansion (tree arg)
{
{
  tree result;
  tree result;
  tree parameter_packs = NULL_TREE;
  tree parameter_packs = NULL_TREE;
  bool for_types = false;
  bool for_types = false;
  struct find_parameter_pack_data ppd;
  struct find_parameter_pack_data ppd;
 
 
  if (!arg || arg == error_mark_node)
  if (!arg || arg == error_mark_node)
    return arg;
    return arg;
 
 
  if (TREE_CODE (arg) == TREE_LIST)
  if (TREE_CODE (arg) == TREE_LIST)
    {
    {
      /* The only time we will see a TREE_LIST here is for a base
      /* The only time we will see a TREE_LIST here is for a base
         class initializer.  In this case, the TREE_PURPOSE will be a
         class initializer.  In this case, the TREE_PURPOSE will be a
         _TYPE node (representing the base class expansion we're
         _TYPE node (representing the base class expansion we're
         initializing) and the TREE_VALUE will be a TREE_LIST
         initializing) and the TREE_VALUE will be a TREE_LIST
         containing the initialization arguments.
         containing the initialization arguments.
 
 
         The resulting expansion looks somewhat different from most
         The resulting expansion looks somewhat different from most
         expansions. Rather than returning just one _EXPANSION, we
         expansions. Rather than returning just one _EXPANSION, we
         return a TREE_LIST whose TREE_PURPOSE is a
         return a TREE_LIST whose TREE_PURPOSE is a
         TYPE_PACK_EXPANSION containing the bases that will be
         TYPE_PACK_EXPANSION containing the bases that will be
         initialized.  The TREE_VALUE will be identical to the
         initialized.  The TREE_VALUE will be identical to the
         original TREE_VALUE, which is a list of arguments that will
         original TREE_VALUE, which is a list of arguments that will
         be passed to each base.  We do not introduce any new pack
         be passed to each base.  We do not introduce any new pack
         expansion nodes into the TREE_VALUE (although it is possible
         expansion nodes into the TREE_VALUE (although it is possible
         that some already exist), because the TREE_PURPOSE and
         that some already exist), because the TREE_PURPOSE and
         TREE_VALUE all need to be expanded together with the same
         TREE_VALUE all need to be expanded together with the same
         _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
         _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
         resulting TREE_PURPOSE will mention the parameter packs in
         resulting TREE_PURPOSE will mention the parameter packs in
         both the bases and the arguments to the bases.  */
         both the bases and the arguments to the bases.  */
      tree purpose;
      tree purpose;
      tree value;
      tree value;
      tree parameter_packs = NULL_TREE;
      tree parameter_packs = NULL_TREE;
 
 
      /* Determine which parameter packs will be used by the base
      /* Determine which parameter packs will be used by the base
         class expansion.  */
         class expansion.  */
      ppd.visited = pointer_set_create ();
      ppd.visited = pointer_set_create ();
      ppd.parameter_packs = &parameter_packs;
      ppd.parameter_packs = &parameter_packs;
      cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
      cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
                    &ppd, ppd.visited);
                    &ppd, ppd.visited);
 
 
      if (parameter_packs == NULL_TREE)
      if (parameter_packs == NULL_TREE)
        {
        {
          error ("base initializer expansion %<%T%> contains no parameter packs", arg);
          error ("base initializer expansion %<%T%> contains no parameter packs", arg);
          pointer_set_destroy (ppd.visited);
          pointer_set_destroy (ppd.visited);
          return error_mark_node;
          return error_mark_node;
        }
        }
 
 
      if (TREE_VALUE (arg) != void_type_node)
      if (TREE_VALUE (arg) != void_type_node)
        {
        {
          /* Collect the sets of parameter packs used in each of the
          /* Collect the sets of parameter packs used in each of the
             initialization arguments.  */
             initialization arguments.  */
          for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
          for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
            {
            {
              /* Determine which parameter packs will be expanded in this
              /* Determine which parameter packs will be expanded in this
                 argument.  */
                 argument.  */
              cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
              cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
                            &ppd, ppd.visited);
                            &ppd, ppd.visited);
            }
            }
        }
        }
 
 
      pointer_set_destroy (ppd.visited);
      pointer_set_destroy (ppd.visited);
 
 
      /* Create the pack expansion type for the base type.  */
      /* Create the pack expansion type for the base type.  */
      purpose = cxx_make_type (TYPE_PACK_EXPANSION);
      purpose = cxx_make_type (TYPE_PACK_EXPANSION);
      SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
      SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
      PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
      PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
 
 
      /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
      /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
         they will rarely be compared to anything.  */
         they will rarely be compared to anything.  */
      SET_TYPE_STRUCTURAL_EQUALITY (purpose);
      SET_TYPE_STRUCTURAL_EQUALITY (purpose);
 
 
      return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
      return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
    }
    }
 
 
  if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
  if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    for_types = true;
    for_types = true;
 
 
  /* Build the PACK_EXPANSION_* node.  */
  /* Build the PACK_EXPANSION_* node.  */
  result = for_types
  result = for_types
     ? cxx_make_type (TYPE_PACK_EXPANSION)
     ? cxx_make_type (TYPE_PACK_EXPANSION)
     : make_node (EXPR_PACK_EXPANSION);
     : make_node (EXPR_PACK_EXPANSION);
  SET_PACK_EXPANSION_PATTERN (result, arg);
  SET_PACK_EXPANSION_PATTERN (result, arg);
  if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
  if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    {
    {
      /* Propagate type and const-expression information.  */
      /* Propagate type and const-expression information.  */
      TREE_TYPE (result) = TREE_TYPE (arg);
      TREE_TYPE (result) = TREE_TYPE (arg);
      TREE_CONSTANT (result) = TREE_CONSTANT (arg);
      TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    }
    }
  else
  else
    /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
       they will rarely be compared to anything.  */
       they will rarely be compared to anything.  */
    SET_TYPE_STRUCTURAL_EQUALITY (result);
    SET_TYPE_STRUCTURAL_EQUALITY (result);
 
 
  /* Determine which parameter packs will be expanded.  */
  /* Determine which parameter packs will be expanded.  */
  ppd.parameter_packs = &parameter_packs;
  ppd.parameter_packs = &parameter_packs;
  ppd.visited = pointer_set_create ();
  ppd.visited = pointer_set_create ();
  cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
  cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
  pointer_set_destroy (ppd.visited);
  pointer_set_destroy (ppd.visited);
 
 
  /* Make sure we found some parameter packs.  */
  /* Make sure we found some parameter packs.  */
  if (parameter_packs == NULL_TREE)
  if (parameter_packs == NULL_TREE)
    {
    {
      if (TYPE_P (arg))
      if (TYPE_P (arg))
        error ("expansion pattern %<%T%> contains no argument packs", arg);
        error ("expansion pattern %<%T%> contains no argument packs", arg);
      else
      else
        error ("expansion pattern %<%E%> contains no argument packs", arg);
        error ("expansion pattern %<%E%> contains no argument packs", arg);
      return error_mark_node;
      return error_mark_node;
    }
    }
  PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
  PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
 
 
  return result;
  return result;
}
}
 
 
/* Checks T for any "bare" parameter packs, which have not yet been
/* Checks T for any "bare" parameter packs, which have not yet been
   expanded, and issues an error if any are found. This operation can
   expanded, and issues an error if any are found. This operation can
   only be done on full expressions or types (e.g., an expression
   only be done on full expressions or types (e.g., an expression
   statement, "if" condition, etc.), because we could have expressions like:
   statement, "if" condition, etc.), because we could have expressions like:
 
 
     foo(f(g(h(args)))...)
     foo(f(g(h(args)))...)
 
 
   where "args" is a parameter pack. check_for_bare_parameter_packs
   where "args" is a parameter pack. check_for_bare_parameter_packs
   should not be called for the subexpressions args, h(args),
   should not be called for the subexpressions args, h(args),
   g(h(args)), or f(g(h(args))), because we would produce erroneous
   g(h(args)), or f(g(h(args))), because we would produce erroneous
   error messages.
   error messages.
 
 
   Returns TRUE and emits an error if there were bare parameter packs,
   Returns TRUE and emits an error if there were bare parameter packs,
   returns FALSE otherwise.  */
   returns FALSE otherwise.  */
bool
bool
check_for_bare_parameter_packs (tree t)
check_for_bare_parameter_packs (tree t)
{
{
  tree parameter_packs = NULL_TREE;
  tree parameter_packs = NULL_TREE;
  struct find_parameter_pack_data ppd;
  struct find_parameter_pack_data ppd;
 
 
  if (!processing_template_decl || !t || t == error_mark_node)
  if (!processing_template_decl || !t || t == error_mark_node)
    return false;
    return false;
 
 
  if (TREE_CODE (t) == TYPE_DECL)
  if (TREE_CODE (t) == TYPE_DECL)
    t = TREE_TYPE (t);
    t = TREE_TYPE (t);
 
 
  ppd.parameter_packs = &parameter_packs;
  ppd.parameter_packs = &parameter_packs;
  ppd.visited = pointer_set_create ();
  ppd.visited = pointer_set_create ();
  cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
  cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
  pointer_set_destroy (ppd.visited);
  pointer_set_destroy (ppd.visited);
 
 
  if (parameter_packs)
  if (parameter_packs)
    {
    {
      error ("parameter packs not expanded with %<...%>:");
      error ("parameter packs not expanded with %<...%>:");
      while (parameter_packs)
      while (parameter_packs)
        {
        {
          tree pack = TREE_VALUE (parameter_packs);
          tree pack = TREE_VALUE (parameter_packs);
          tree name = NULL_TREE;
          tree name = NULL_TREE;
 
 
          if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
          if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
              || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
              || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
            name = TYPE_NAME (pack);
            name = TYPE_NAME (pack);
          else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
          else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
            name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
            name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
          else
          else
            name = DECL_NAME (pack);
            name = DECL_NAME (pack);
 
 
          if (name)
          if (name)
            inform (input_location, "        %qD", name);
            inform (input_location, "        %qD", name);
          else
          else
            inform (input_location, "        <anonymous>");
            inform (input_location, "        <anonymous>");
 
 
          parameter_packs = TREE_CHAIN (parameter_packs);
          parameter_packs = TREE_CHAIN (parameter_packs);
        }
        }
 
 
      return true;
      return true;
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* Expand any parameter packs that occur in the template arguments in
/* Expand any parameter packs that occur in the template arguments in
   ARGS.  */
   ARGS.  */
tree
tree
expand_template_argument_pack (tree args)
expand_template_argument_pack (tree args)
{
{
  tree result_args = NULL_TREE;
  tree result_args = NULL_TREE;
  int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
  int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
  int num_result_args = -1;
  int num_result_args = -1;
  int non_default_args_count = -1;
  int non_default_args_count = -1;
 
 
  /* First, determine if we need to expand anything, and the number of
  /* First, determine if we need to expand anything, and the number of
     slots we'll need.  */
     slots we'll need.  */
  for (in_arg = 0; in_arg < nargs; ++in_arg)
  for (in_arg = 0; in_arg < nargs; ++in_arg)
    {
    {
      tree arg = TREE_VEC_ELT (args, in_arg);
      tree arg = TREE_VEC_ELT (args, in_arg);
      if (arg == NULL_TREE)
      if (arg == NULL_TREE)
        return args;
        return args;
      if (ARGUMENT_PACK_P (arg))
      if (ARGUMENT_PACK_P (arg))
        {
        {
          int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
          int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
          if (num_result_args < 0)
          if (num_result_args < 0)
            num_result_args = in_arg + num_packed;
            num_result_args = in_arg + num_packed;
          else
          else
            num_result_args += num_packed;
            num_result_args += num_packed;
        }
        }
      else
      else
        {
        {
          if (num_result_args >= 0)
          if (num_result_args >= 0)
            num_result_args++;
            num_result_args++;
        }
        }
    }
    }
 
 
  /* If no expansion is necessary, we're done.  */
  /* If no expansion is necessary, we're done.  */
  if (num_result_args < 0)
  if (num_result_args < 0)
    return args;
    return args;
 
 
  /* Expand arguments.  */
  /* Expand arguments.  */
  result_args = make_tree_vec (num_result_args);
  result_args = make_tree_vec (num_result_args);
  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    non_default_args_count =
    non_default_args_count =
      GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
      GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
  for (in_arg = 0; in_arg < nargs; ++in_arg)
  for (in_arg = 0; in_arg < nargs; ++in_arg)
    {
    {
      tree arg = TREE_VEC_ELT (args, in_arg);
      tree arg = TREE_VEC_ELT (args, in_arg);
      if (ARGUMENT_PACK_P (arg))
      if (ARGUMENT_PACK_P (arg))
        {
        {
          tree packed = ARGUMENT_PACK_ARGS (arg);
          tree packed = ARGUMENT_PACK_ARGS (arg);
          int i, num_packed = TREE_VEC_LENGTH (packed);
          int i, num_packed = TREE_VEC_LENGTH (packed);
          for (i = 0; i < num_packed; ++i, ++out_arg)
          for (i = 0; i < num_packed; ++i, ++out_arg)
            TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
            TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
          if (non_default_args_count > 0)
          if (non_default_args_count > 0)
            non_default_args_count += num_packed;
            non_default_args_count += num_packed;
        }
        }
      else
      else
        {
        {
          TREE_VEC_ELT (result_args, out_arg) = arg;
          TREE_VEC_ELT (result_args, out_arg) = arg;
          ++out_arg;
          ++out_arg;
        }
        }
    }
    }
  if (non_default_args_count >= 0)
  if (non_default_args_count >= 0)
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
  return result_args;
  return result_args;
}
}
 
 
/* Checks if DECL shadows a template parameter.
/* Checks if DECL shadows a template parameter.
 
 
   [temp.local]: A template-parameter shall not be redeclared within its
   [temp.local]: A template-parameter shall not be redeclared within its
   scope (including nested scopes).
   scope (including nested scopes).
 
 
   Emits an error and returns TRUE if the DECL shadows a parameter,
   Emits an error and returns TRUE if the DECL shadows a parameter,
   returns FALSE otherwise.  */
   returns FALSE otherwise.  */
 
 
bool
bool
check_template_shadow (tree decl)
check_template_shadow (tree decl)
{
{
  tree olddecl;
  tree olddecl;
 
 
  /* If we're not in a template, we can't possibly shadow a template
  /* If we're not in a template, we can't possibly shadow a template
     parameter.  */
     parameter.  */
  if (!current_template_parms)
  if (!current_template_parms)
    return true;
    return true;
 
 
  /* Figure out what we're shadowing.  */
  /* Figure out what we're shadowing.  */
  if (TREE_CODE (decl) == OVERLOAD)
  if (TREE_CODE (decl) == OVERLOAD)
    decl = OVL_CURRENT (decl);
    decl = OVL_CURRENT (decl);
  olddecl = innermost_non_namespace_value (DECL_NAME (decl));
  olddecl = innermost_non_namespace_value (DECL_NAME (decl));
 
 
  /* If there's no previous binding for this name, we're not shadowing
  /* If there's no previous binding for this name, we're not shadowing
     anything, let alone a template parameter.  */
     anything, let alone a template parameter.  */
  if (!olddecl)
  if (!olddecl)
    return true;
    return true;
 
 
  /* If we're not shadowing a template parameter, we're done.  Note
  /* If we're not shadowing a template parameter, we're done.  Note
     that OLDDECL might be an OVERLOAD (or perhaps even an
     that OLDDECL might be an OVERLOAD (or perhaps even an
     ERROR_MARK), so we can't just blithely assume it to be a _DECL
     ERROR_MARK), so we can't just blithely assume it to be a _DECL
     node.  */
     node.  */
  if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
  if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
    return true;
    return true;
 
 
  /* We check for decl != olddecl to avoid bogus errors for using a
  /* We check for decl != olddecl to avoid bogus errors for using a
     name inside a class.  We check TPFI to avoid duplicate errors for
     name inside a class.  We check TPFI to avoid duplicate errors for
     inline member templates.  */
     inline member templates.  */
  if (decl == olddecl
  if (decl == olddecl
      || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
      || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
    return true;
    return true;
 
 
  error ("declaration of %q+#D", decl);
  error ("declaration of %q+#D", decl);
  error (" shadows template parm %q+#D", olddecl);
  error (" shadows template parm %q+#D", olddecl);
  return false;
  return false;
}
}
 
 
/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
   ORIG_LEVEL, DECL, and TYPE.  */
   ORIG_LEVEL, DECL, and TYPE.  */
 
 
static tree
static tree
build_template_parm_index (int index,
build_template_parm_index (int index,
                           int level,
                           int level,
                           int orig_level,
                           int orig_level,
                           tree decl,
                           tree decl,
                           tree type)
                           tree type)
{
{
  tree t = make_node (TEMPLATE_PARM_INDEX);
  tree t = make_node (TEMPLATE_PARM_INDEX);
  TEMPLATE_PARM_IDX (t) = index;
  TEMPLATE_PARM_IDX (t) = index;
  TEMPLATE_PARM_LEVEL (t) = level;
  TEMPLATE_PARM_LEVEL (t) = level;
  TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
  TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
  TEMPLATE_PARM_DECL (t) = decl;
  TEMPLATE_PARM_DECL (t) = decl;
  TREE_TYPE (t) = type;
  TREE_TYPE (t) = type;
  TREE_CONSTANT (t) = TREE_CONSTANT (decl);
  TREE_CONSTANT (t) = TREE_CONSTANT (decl);
  TREE_READONLY (t) = TREE_READONLY (decl);
  TREE_READONLY (t) = TREE_READONLY (decl);
 
 
  return t;
  return t;
}
}
 
 
/* Find the canonical type parameter for the given template type
/* Find the canonical type parameter for the given template type
   parameter.  Returns the canonical type parameter, which may be TYPE
   parameter.  Returns the canonical type parameter, which may be TYPE
   if no such parameter existed.  */
   if no such parameter existed.  */
static tree
static tree
canonical_type_parameter (tree type)
canonical_type_parameter (tree type)
{
{
  tree list;
  tree list;
  int idx = TEMPLATE_TYPE_IDX (type);
  int idx = TEMPLATE_TYPE_IDX (type);
  if (!canonical_template_parms)
  if (!canonical_template_parms)
    canonical_template_parms = VEC_alloc (tree, gc, idx+1);
    canonical_template_parms = VEC_alloc (tree, gc, idx+1);
 
 
  while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
  while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
    VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
    VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
 
 
  list = VEC_index (tree, canonical_template_parms, idx);
  list = VEC_index (tree, canonical_template_parms, idx);
  while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
  while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
    list = TREE_CHAIN (list);
    list = TREE_CHAIN (list);
 
 
  if (list)
  if (list)
    return TREE_VALUE (list);
    return TREE_VALUE (list);
  else
  else
    {
    {
      VEC_replace(tree, canonical_template_parms, idx,
      VEC_replace(tree, canonical_template_parms, idx,
                  tree_cons (NULL_TREE, type,
                  tree_cons (NULL_TREE, type,
                             VEC_index (tree, canonical_template_parms, idx)));
                             VEC_index (tree, canonical_template_parms, idx)));
      return type;
      return type;
    }
    }
}
}
 
 
/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
   TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
   TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
   TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
   TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
   new one is created.  */
   new one is created.  */
 
 
static tree
static tree
reduce_template_parm_level (tree index, tree type, int levels, tree args,
reduce_template_parm_level (tree index, tree type, int levels, tree args,
                            tsubst_flags_t complain)
                            tsubst_flags_t complain)
{
{
  if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
  if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
      || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
      || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
          != TEMPLATE_PARM_LEVEL (index) - levels)
          != TEMPLATE_PARM_LEVEL (index) - levels)
      || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
      || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    {
    {
      tree orig_decl = TEMPLATE_PARM_DECL (index);
      tree orig_decl = TEMPLATE_PARM_DECL (index);
      tree decl, t;
      tree decl, t;
 
 
      decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
      decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
                         TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
                         TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
      TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
      TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
      TREE_READONLY (decl) = TREE_READONLY (orig_decl);
      TREE_READONLY (decl) = TREE_READONLY (orig_decl);
      DECL_ARTIFICIAL (decl) = 1;
      DECL_ARTIFICIAL (decl) = 1;
      SET_DECL_TEMPLATE_PARM_P (decl);
      SET_DECL_TEMPLATE_PARM_P (decl);
 
 
      t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
      t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
                                     TEMPLATE_PARM_LEVEL (index) - levels,
                                     TEMPLATE_PARM_LEVEL (index) - levels,
                                     TEMPLATE_PARM_ORIG_LEVEL (index),
                                     TEMPLATE_PARM_ORIG_LEVEL (index),
                                     decl, type);
                                     decl, type);
      TEMPLATE_PARM_DESCENDANTS (index) = t;
      TEMPLATE_PARM_DESCENDANTS (index) = t;
      TEMPLATE_PARM_PARAMETER_PACK (t)
      TEMPLATE_PARM_PARAMETER_PACK (t)
        = TEMPLATE_PARM_PARAMETER_PACK (index);
        = TEMPLATE_PARM_PARAMETER_PACK (index);
 
 
        /* Template template parameters need this.  */
        /* Template template parameters need this.  */
      if (TREE_CODE (decl) == TEMPLATE_DECL)
      if (TREE_CODE (decl) == TEMPLATE_DECL)
        DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
        DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
          (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
          (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
           args, complain);
           args, complain);
    }
    }
 
 
  return TEMPLATE_PARM_DESCENDANTS (index);
  return TEMPLATE_PARM_DESCENDANTS (index);
}
}
 
 
/* Process information from new template parameter PARM and append it to the
/* Process information from new template parameter PARM and append it to the
   LIST being built.  This new parameter is a non-type parameter iff
   LIST being built.  This new parameter is a non-type parameter iff
   IS_NON_TYPE is true. This new parameter is a parameter
   IS_NON_TYPE is true. This new parameter is a parameter
   pack iff IS_PARAMETER_PACK is true.  The location of PARM is in
   pack iff IS_PARAMETER_PACK is true.  The location of PARM is in
   PARM_LOC.  */
   PARM_LOC.  */
 
 
tree
tree
process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
                       bool is_parameter_pack)
                       bool is_parameter_pack)
{
{
  tree decl = 0;
  tree decl = 0;
  tree defval;
  tree defval;
  tree err_parm_list;
  tree err_parm_list;
  int idx = 0;
  int idx = 0;
 
 
  gcc_assert (TREE_CODE (parm) == TREE_LIST);
  gcc_assert (TREE_CODE (parm) == TREE_LIST);
  defval = TREE_PURPOSE (parm);
  defval = TREE_PURPOSE (parm);
 
 
  if (list)
  if (list)
    {
    {
      tree p = tree_last (list);
      tree p = tree_last (list);
 
 
      if (p && TREE_VALUE (p) != error_mark_node)
      if (p && TREE_VALUE (p) != error_mark_node)
        {
        {
          p = TREE_VALUE (p);
          p = TREE_VALUE (p);
          if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
          if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
            idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
            idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
          else
          else
            idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
            idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
        }
        }
 
 
      ++idx;
      ++idx;
    }
    }
  else
  else
    idx = 0;
    idx = 0;
 
 
  if (is_non_type)
  if (is_non_type)
    {
    {
      parm = TREE_VALUE (parm);
      parm = TREE_VALUE (parm);
 
 
      SET_DECL_TEMPLATE_PARM_P (parm);
      SET_DECL_TEMPLATE_PARM_P (parm);
 
 
      if (TREE_TYPE (parm) == error_mark_node)
      if (TREE_TYPE (parm) == error_mark_node)
        {
        {
          err_parm_list = build_tree_list (defval, parm);
          err_parm_list = build_tree_list (defval, parm);
          TREE_VALUE (err_parm_list) = error_mark_node;
          TREE_VALUE (err_parm_list) = error_mark_node;
           return chainon (list, err_parm_list);
           return chainon (list, err_parm_list);
        }
        }
      else
      else
      {
      {
        /* [temp.param]
        /* [temp.param]
 
 
           The top-level cv-qualifiers on the template-parameter are
           The top-level cv-qualifiers on the template-parameter are
           ignored when determining its type.  */
           ignored when determining its type.  */
        TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
        TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
        if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
        if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
          {
          {
            err_parm_list = build_tree_list (defval, parm);
            err_parm_list = build_tree_list (defval, parm);
            TREE_VALUE (err_parm_list) = error_mark_node;
            TREE_VALUE (err_parm_list) = error_mark_node;
             return chainon (list, err_parm_list);
             return chainon (list, err_parm_list);
          }
          }
 
 
        if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
        if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
          {
          {
            /* This template parameter is not a parameter pack, but it
            /* This template parameter is not a parameter pack, but it
               should be. Complain about "bare" parameter packs.  */
               should be. Complain about "bare" parameter packs.  */
            check_for_bare_parameter_packs (TREE_TYPE (parm));
            check_for_bare_parameter_packs (TREE_TYPE (parm));
 
 
            /* Recover by calling this a parameter pack.  */
            /* Recover by calling this a parameter pack.  */
            is_parameter_pack = true;
            is_parameter_pack = true;
          }
          }
      }
      }
 
 
      /* A template parameter is not modifiable.  */
      /* A template parameter is not modifiable.  */
      TREE_CONSTANT (parm) = 1;
      TREE_CONSTANT (parm) = 1;
      TREE_READONLY (parm) = 1;
      TREE_READONLY (parm) = 1;
      decl = build_decl (parm_loc,
      decl = build_decl (parm_loc,
                         CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
                         CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
      TREE_CONSTANT (decl) = 1;
      TREE_CONSTANT (decl) = 1;
      TREE_READONLY (decl) = 1;
      TREE_READONLY (decl) = 1;
      DECL_INITIAL (parm) = DECL_INITIAL (decl)
      DECL_INITIAL (parm) = DECL_INITIAL (decl)
        = build_template_parm_index (idx, processing_template_decl,
        = build_template_parm_index (idx, processing_template_decl,
                                     processing_template_decl,
                                     processing_template_decl,
                                     decl, TREE_TYPE (parm));
                                     decl, TREE_TYPE (parm));
 
 
      TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
      TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
        = is_parameter_pack;
        = is_parameter_pack;
    }
    }
  else
  else
    {
    {
      tree t;
      tree t;
      parm = TREE_VALUE (TREE_VALUE (parm));
      parm = TREE_VALUE (TREE_VALUE (parm));
 
 
      if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
      if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
        {
        {
          t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
          t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
          /* This is for distinguishing between real templates and template
          /* This is for distinguishing between real templates and template
             template parameters */
             template parameters */
          TREE_TYPE (parm) = t;
          TREE_TYPE (parm) = t;
          TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
          TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
          decl = parm;
          decl = parm;
        }
        }
      else
      else
        {
        {
          t = cxx_make_type (TEMPLATE_TYPE_PARM);
          t = cxx_make_type (TEMPLATE_TYPE_PARM);
          /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
          /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
          decl = build_decl (parm_loc,
          decl = build_decl (parm_loc,
                             TYPE_DECL, parm, t);
                             TYPE_DECL, parm, t);
        }
        }
 
 
      TYPE_NAME (t) = decl;
      TYPE_NAME (t) = decl;
      TYPE_STUB_DECL (t) = decl;
      TYPE_STUB_DECL (t) = decl;
      parm = decl;
      parm = decl;
      TEMPLATE_TYPE_PARM_INDEX (t)
      TEMPLATE_TYPE_PARM_INDEX (t)
        = build_template_parm_index (idx, processing_template_decl,
        = build_template_parm_index (idx, processing_template_decl,
                                     processing_template_decl,
                                     processing_template_decl,
                                     decl, TREE_TYPE (parm));
                                     decl, TREE_TYPE (parm));
      TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
      TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
      TYPE_CANONICAL (t) = canonical_type_parameter (t);
      TYPE_CANONICAL (t) = canonical_type_parameter (t);
    }
    }
  DECL_ARTIFICIAL (decl) = 1;
  DECL_ARTIFICIAL (decl) = 1;
  SET_DECL_TEMPLATE_PARM_P (decl);
  SET_DECL_TEMPLATE_PARM_P (decl);
  pushdecl (decl);
  pushdecl (decl);
  parm = build_tree_list (defval, parm);
  parm = build_tree_list (defval, parm);
  return chainon (list, parm);
  return chainon (list, parm);
}
}
 
 
/* The end of a template parameter list has been reached.  Process the
/* The end of a template parameter list has been reached.  Process the
   tree list into a parameter vector, converting each parameter into a more
   tree list into a parameter vector, converting each parameter into a more
   useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
   useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
   as PARM_DECLs.  */
   as PARM_DECLs.  */
 
 
tree
tree
end_template_parm_list (tree parms)
end_template_parm_list (tree parms)
{
{
  int nparms;
  int nparms;
  tree parm, next;
  tree parm, next;
  tree saved_parmlist = make_tree_vec (list_length (parms));
  tree saved_parmlist = make_tree_vec (list_length (parms));
 
 
  current_template_parms
  current_template_parms
    = tree_cons (size_int (processing_template_decl),
    = tree_cons (size_int (processing_template_decl),
                 saved_parmlist, current_template_parms);
                 saved_parmlist, current_template_parms);
 
 
  for (parm = parms, nparms = 0; parm; parm = next, nparms++)
  for (parm = parms, nparms = 0; parm; parm = next, nparms++)
    {
    {
      next = TREE_CHAIN (parm);
      next = TREE_CHAIN (parm);
      TREE_VEC_ELT (saved_parmlist, nparms) = parm;
      TREE_VEC_ELT (saved_parmlist, nparms) = parm;
      TREE_CHAIN (parm) = NULL_TREE;
      TREE_CHAIN (parm) = NULL_TREE;
      if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
      if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
        TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
        TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
              current_template_parms;
              current_template_parms;
    }
    }
 
 
  --processing_template_parmlist;
  --processing_template_parmlist;
 
 
  return saved_parmlist;
  return saved_parmlist;
}
}
 
 
/* end_template_decl is called after a template declaration is seen.  */
/* end_template_decl is called after a template declaration is seen.  */
 
 
void
void
end_template_decl (void)
end_template_decl (void)
{
{
  reset_specialization ();
  reset_specialization ();
 
 
  if (! processing_template_decl)
  if (! processing_template_decl)
    return;
    return;
 
 
  /* This matches the pushlevel in begin_template_parm_list.  */
  /* This matches the pushlevel in begin_template_parm_list.  */
  finish_scope ();
  finish_scope ();
 
 
  --processing_template_decl;
  --processing_template_decl;
  current_template_parms = TREE_CHAIN (current_template_parms);
  current_template_parms = TREE_CHAIN (current_template_parms);
}
}
 
 
/* Within the declaration of a template, return all levels of template
/* Within the declaration of a template, return all levels of template
   parameters that apply.  The template parameters are represented as
   parameters that apply.  The template parameters are represented as
   a TREE_VEC, in the form documented in cp-tree.h for template
   a TREE_VEC, in the form documented in cp-tree.h for template
   arguments.  */
   arguments.  */
 
 
static tree
static tree
current_template_args (void)
current_template_args (void)
{
{
  tree header;
  tree header;
  tree args = NULL_TREE;
  tree args = NULL_TREE;
  int length = TMPL_PARMS_DEPTH (current_template_parms);
  int length = TMPL_PARMS_DEPTH (current_template_parms);
  int l = length;
  int l = length;
 
 
  /* If there is only one level of template parameters, we do not
  /* If there is only one level of template parameters, we do not
     create a TREE_VEC of TREE_VECs.  Instead, we return a single
     create a TREE_VEC of TREE_VECs.  Instead, we return a single
     TREE_VEC containing the arguments.  */
     TREE_VEC containing the arguments.  */
  if (length > 1)
  if (length > 1)
    args = make_tree_vec (length);
    args = make_tree_vec (length);
 
 
  for (header = current_template_parms; header; header = TREE_CHAIN (header))
  for (header = current_template_parms; header; header = TREE_CHAIN (header))
    {
    {
      tree a = copy_node (TREE_VALUE (header));
      tree a = copy_node (TREE_VALUE (header));
      int i;
      int i;
 
 
      TREE_TYPE (a) = NULL_TREE;
      TREE_TYPE (a) = NULL_TREE;
      for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
      for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
        {
        {
          tree t = TREE_VEC_ELT (a, i);
          tree t = TREE_VEC_ELT (a, i);
 
 
          /* T will be a list if we are called from within a
          /* T will be a list if we are called from within a
             begin/end_template_parm_list pair, but a vector directly
             begin/end_template_parm_list pair, but a vector directly
             if within a begin/end_member_template_processing pair.  */
             if within a begin/end_member_template_processing pair.  */
          if (TREE_CODE (t) == TREE_LIST)
          if (TREE_CODE (t) == TREE_LIST)
            {
            {
              t = TREE_VALUE (t);
              t = TREE_VALUE (t);
 
 
              if (!error_operand_p (t))
              if (!error_operand_p (t))
                {
                {
                  if (TREE_CODE (t) == TYPE_DECL
                  if (TREE_CODE (t) == TYPE_DECL
                      || TREE_CODE (t) == TEMPLATE_DECL)
                      || TREE_CODE (t) == TEMPLATE_DECL)
                    {
                    {
                      t = TREE_TYPE (t);
                      t = TREE_TYPE (t);
 
 
                      if (TEMPLATE_TYPE_PARAMETER_PACK (t))
                      if (TEMPLATE_TYPE_PARAMETER_PACK (t))
                        {
                        {
                          /* Turn this argument into a TYPE_ARGUMENT_PACK
                          /* Turn this argument into a TYPE_ARGUMENT_PACK
                             with a single element, which expands T.  */
                             with a single element, which expands T.  */
                          tree vec = make_tree_vec (1);
                          tree vec = make_tree_vec (1);
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
                          SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
                          SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
                                (vec, TREE_VEC_LENGTH (vec));
                                (vec, TREE_VEC_LENGTH (vec));
#endif
#endif
                          TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
                          TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
 
 
                          t = cxx_make_type (TYPE_ARGUMENT_PACK);
                          t = cxx_make_type (TYPE_ARGUMENT_PACK);
                          SET_ARGUMENT_PACK_ARGS (t, vec);
                          SET_ARGUMENT_PACK_ARGS (t, vec);
                        }
                        }
                    }
                    }
                  else
                  else
                    {
                    {
                      t = DECL_INITIAL (t);
                      t = DECL_INITIAL (t);
 
 
                      if (TEMPLATE_PARM_PARAMETER_PACK (t))
                      if (TEMPLATE_PARM_PARAMETER_PACK (t))
                        {
                        {
                          /* Turn this argument into a NONTYPE_ARGUMENT_PACK
                          /* Turn this argument into a NONTYPE_ARGUMENT_PACK
                             with a single element, which expands T.  */
                             with a single element, which expands T.  */
                          tree vec = make_tree_vec (1);
                          tree vec = make_tree_vec (1);
                          tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
                          tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
                          SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
                          SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
                                (vec, TREE_VEC_LENGTH (vec));
                                (vec, TREE_VEC_LENGTH (vec));
#endif
#endif
                          TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
                          TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
 
 
                          t  = make_node (NONTYPE_ARGUMENT_PACK);
                          t  = make_node (NONTYPE_ARGUMENT_PACK);
                          SET_ARGUMENT_PACK_ARGS (t, vec);
                          SET_ARGUMENT_PACK_ARGS (t, vec);
                          TREE_TYPE (t) = type;
                          TREE_TYPE (t) = type;
                        }
                        }
                    }
                    }
                  TREE_VEC_ELT (a, i) = t;
                  TREE_VEC_ELT (a, i) = t;
                }
                }
            }
            }
        }
        }
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
#endif
#endif
 
 
      if (length > 1)
      if (length > 1)
        TREE_VEC_ELT (args, --l) = a;
        TREE_VEC_ELT (args, --l) = a;
      else
      else
        args = a;
        args = a;
    }
    }
 
 
  return args;
  return args;
}
}
 
 
/* Update the declared TYPE by doing any lookups which were thought to be
/* Update the declared TYPE by doing any lookups which were thought to be
   dependent, but are not now that we know the SCOPE of the declarator.  */
   dependent, but are not now that we know the SCOPE of the declarator.  */
 
 
tree
tree
maybe_update_decl_type (tree orig_type, tree scope)
maybe_update_decl_type (tree orig_type, tree scope)
{
{
  tree type = orig_type;
  tree type = orig_type;
 
 
  if (type == NULL_TREE)
  if (type == NULL_TREE)
    return type;
    return type;
 
 
  if (TREE_CODE (orig_type) == TYPE_DECL)
  if (TREE_CODE (orig_type) == TYPE_DECL)
    type = TREE_TYPE (type);
    type = TREE_TYPE (type);
 
 
  if (scope && TYPE_P (scope) && dependent_type_p (scope)
  if (scope && TYPE_P (scope) && dependent_type_p (scope)
      && dependent_type_p (type)
      && dependent_type_p (type)
      /* Don't bother building up the args in this case.  */
      /* Don't bother building up the args in this case.  */
      && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
      && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
    {
    {
      /* tsubst in the args corresponding to the template parameters,
      /* tsubst in the args corresponding to the template parameters,
         including auto if present.  Most things will be unchanged, but
         including auto if present.  Most things will be unchanged, but
         make_typename_type and tsubst_qualified_id will resolve
         make_typename_type and tsubst_qualified_id will resolve
         TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
         TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
      tree args = current_template_args ();
      tree args = current_template_args ();
      tree auto_node = type_uses_auto (type);
      tree auto_node = type_uses_auto (type);
      tree pushed;
      tree pushed;
      if (auto_node)
      if (auto_node)
        {
        {
          tree auto_vec = make_tree_vec (1);
          tree auto_vec = make_tree_vec (1);
          TREE_VEC_ELT (auto_vec, 0) = auto_node;
          TREE_VEC_ELT (auto_vec, 0) = auto_node;
          args = add_to_template_args (args, auto_vec);
          args = add_to_template_args (args, auto_vec);
        }
        }
      pushed = push_scope (scope);
      pushed = push_scope (scope);
      type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
      type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
      if (pushed)
      if (pushed)
        pop_scope (scope);
        pop_scope (scope);
    }
    }
 
 
  if (type == error_mark_node)
  if (type == error_mark_node)
    return orig_type;
    return orig_type;
 
 
  if (TREE_CODE (orig_type) == TYPE_DECL)
  if (TREE_CODE (orig_type) == TYPE_DECL)
    {
    {
      if (same_type_p (type, TREE_TYPE (orig_type)))
      if (same_type_p (type, TREE_TYPE (orig_type)))
        type = orig_type;
        type = orig_type;
      else
      else
        type = TYPE_NAME (type);
        type = TYPE_NAME (type);
    }
    }
  return type;
  return type;
}
}
 
 
/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
   template PARMS.  If MEMBER_TEMPLATE_P is true, the new template is
   template PARMS.  If MEMBER_TEMPLATE_P is true, the new template is
   a member template.  Used by push_template_decl below.  */
   a member template.  Used by push_template_decl below.  */
 
 
static tree
static tree
build_template_decl (tree decl, tree parms, bool member_template_p)
build_template_decl (tree decl, tree parms, bool member_template_p)
{
{
  tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
  tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
  DECL_TEMPLATE_PARMS (tmpl) = parms;
  DECL_TEMPLATE_PARMS (tmpl) = parms;
  DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
  DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
  DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
  DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
 
 
  return tmpl;
  return tmpl;
}
}
 
 
struct template_parm_data
struct template_parm_data
{
{
  /* The level of the template parameters we are currently
  /* The level of the template parameters we are currently
     processing.  */
     processing.  */
  int level;
  int level;
 
 
  /* The index of the specialization argument we are currently
  /* The index of the specialization argument we are currently
     processing.  */
     processing.  */
  int current_arg;
  int current_arg;
 
 
  /* An array whose size is the number of template parameters.  The
  /* An array whose size is the number of template parameters.  The
     elements are nonzero if the parameter has been used in any one
     elements are nonzero if the parameter has been used in any one
     of the arguments processed so far.  */
     of the arguments processed so far.  */
  int* parms;
  int* parms;
 
 
  /* An array whose size is the number of template arguments.  The
  /* An array whose size is the number of template arguments.  The
     elements are nonzero if the argument makes use of template
     elements are nonzero if the argument makes use of template
     parameters of this level.  */
     parameters of this level.  */
  int* arg_uses_template_parms;
  int* arg_uses_template_parms;
};
};
 
 
/* Subroutine of push_template_decl used to see if each template
/* Subroutine of push_template_decl used to see if each template
   parameter in a partial specialization is used in the explicit
   parameter in a partial specialization is used in the explicit
   argument list.  If T is of the LEVEL given in DATA (which is
   argument list.  If T is of the LEVEL given in DATA (which is
   treated as a template_parm_data*), then DATA->PARMS is marked
   treated as a template_parm_data*), then DATA->PARMS is marked
   appropriately.  */
   appropriately.  */
 
 
static int
static int
mark_template_parm (tree t, void* data)
mark_template_parm (tree t, void* data)
{
{
  int level;
  int level;
  int idx;
  int idx;
  struct template_parm_data* tpd = (struct template_parm_data*) data;
  struct template_parm_data* tpd = (struct template_parm_data*) data;
 
 
  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    {
    {
      level = TEMPLATE_PARM_LEVEL (t);
      level = TEMPLATE_PARM_LEVEL (t);
      idx = TEMPLATE_PARM_IDX (t);
      idx = TEMPLATE_PARM_IDX (t);
    }
    }
  else
  else
    {
    {
      level = TEMPLATE_TYPE_LEVEL (t);
      level = TEMPLATE_TYPE_LEVEL (t);
      idx = TEMPLATE_TYPE_IDX (t);
      idx = TEMPLATE_TYPE_IDX (t);
    }
    }
 
 
  if (level == tpd->level)
  if (level == tpd->level)
    {
    {
      tpd->parms[idx] = 1;
      tpd->parms[idx] = 1;
      tpd->arg_uses_template_parms[tpd->current_arg] = 1;
      tpd->arg_uses_template_parms[tpd->current_arg] = 1;
    }
    }
 
 
  /* Return zero so that for_each_template_parm will continue the
  /* Return zero so that for_each_template_parm will continue the
     traversal of the tree; we want to mark *every* template parm.  */
     traversal of the tree; we want to mark *every* template parm.  */
  return 0;
  return 0;
}
}
 
 
/* Process the partial specialization DECL.  */
/* Process the partial specialization DECL.  */
 
 
static tree
static tree
process_partial_specialization (tree decl)
process_partial_specialization (tree decl)
{
{
  tree type = TREE_TYPE (decl);
  tree type = TREE_TYPE (decl);
  tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
  tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
  tree specargs = CLASSTYPE_TI_ARGS (type);
  tree specargs = CLASSTYPE_TI_ARGS (type);
  tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
  tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
  tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
  tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
  tree inner_parms;
  tree inner_parms;
  int nargs = TREE_VEC_LENGTH (inner_args);
  int nargs = TREE_VEC_LENGTH (inner_args);
  int ntparms;
  int ntparms;
  int  i;
  int  i;
  int did_error_intro = 0;
  int did_error_intro = 0;
  struct template_parm_data tpd;
  struct template_parm_data tpd;
  struct template_parm_data tpd2;
  struct template_parm_data tpd2;
 
 
  gcc_assert (current_template_parms);
  gcc_assert (current_template_parms);
 
 
  inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
  inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
  ntparms = TREE_VEC_LENGTH (inner_parms);
  ntparms = TREE_VEC_LENGTH (inner_parms);
 
 
  /* We check that each of the template parameters given in the
  /* We check that each of the template parameters given in the
     partial specialization is used in the argument list to the
     partial specialization is used in the argument list to the
     specialization.  For example:
     specialization.  For example:
 
 
       template <class T> struct S;
       template <class T> struct S;
       template <class T> struct S<T*>;
       template <class T> struct S<T*>;
 
 
     The second declaration is OK because `T*' uses the template
     The second declaration is OK because `T*' uses the template
     parameter T, whereas
     parameter T, whereas
 
 
       template <class T> struct S<int>;
       template <class T> struct S<int>;
 
 
     is no good.  Even trickier is:
     is no good.  Even trickier is:
 
 
       template <class T>
       template <class T>
       struct S1
       struct S1
       {
       {
          template <class U>
          template <class U>
          struct S2;
          struct S2;
          template <class U>
          template <class U>
          struct S2<T>;
          struct S2<T>;
       };
       };
 
 
     The S2<T> declaration is actually invalid; it is a
     The S2<T> declaration is actually invalid; it is a
     full-specialization.  Of course,
     full-specialization.  Of course,
 
 
          template <class U>
          template <class U>
          struct S2<T (*)(U)>;
          struct S2<T (*)(U)>;
 
 
     or some such would have been OK.  */
     or some such would have been OK.  */
  tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
  tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
  tpd.parms = (int *) alloca (sizeof (int) * ntparms);
  tpd.parms = (int *) alloca (sizeof (int) * ntparms);
  memset (tpd.parms, 0, sizeof (int) * ntparms);
  memset (tpd.parms, 0, sizeof (int) * ntparms);
 
 
  tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
  tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
  memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
  memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
  for (i = 0; i < nargs; ++i)
  for (i = 0; i < nargs; ++i)
    {
    {
      tpd.current_arg = i;
      tpd.current_arg = i;
      for_each_template_parm (TREE_VEC_ELT (inner_args, i),
      for_each_template_parm (TREE_VEC_ELT (inner_args, i),
                              &mark_template_parm,
                              &mark_template_parm,
                              &tpd,
                              &tpd,
                              NULL,
                              NULL,
                              /*include_nondeduced_p=*/false);
                              /*include_nondeduced_p=*/false);
    }
    }
  for (i = 0; i < ntparms; ++i)
  for (i = 0; i < ntparms; ++i)
    if (tpd.parms[i] == 0)
    if (tpd.parms[i] == 0)
      {
      {
        /* One of the template parms was not used in the
        /* One of the template parms was not used in the
           specialization.  */
           specialization.  */
        if (!did_error_intro)
        if (!did_error_intro)
          {
          {
            error ("template parameters not used in partial specialization:");
            error ("template parameters not used in partial specialization:");
            did_error_intro = 1;
            did_error_intro = 1;
          }
          }
 
 
        error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
        error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
      }
      }
 
 
  /* [temp.class.spec]
  /* [temp.class.spec]
 
 
     The argument list of the specialization shall not be identical to
     The argument list of the specialization shall not be identical to
     the implicit argument list of the primary template.  */
     the implicit argument list of the primary template.  */
  if (comp_template_args
  if (comp_template_args
      (inner_args,
      (inner_args,
       INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
       INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
                                                   (maintmpl)))))
                                                   (maintmpl)))))
    error ("partial specialization %qT does not specialize any template arguments", type);
    error ("partial specialization %qT does not specialize any template arguments", type);
 
 
  /* [temp.class.spec]
  /* [temp.class.spec]
 
 
     A partially specialized non-type argument expression shall not
     A partially specialized non-type argument expression shall not
     involve template parameters of the partial specialization except
     involve template parameters of the partial specialization except
     when the argument expression is a simple identifier.
     when the argument expression is a simple identifier.
 
 
     The type of a template parameter corresponding to a specialized
     The type of a template parameter corresponding to a specialized
     non-type argument shall not be dependent on a parameter of the
     non-type argument shall not be dependent on a parameter of the
     specialization.
     specialization.
 
 
     Also, we verify that pack expansions only occur at the
     Also, we verify that pack expansions only occur at the
     end of the argument list.  */
     end of the argument list.  */
  gcc_assert (nargs == DECL_NTPARMS (maintmpl));
  gcc_assert (nargs == DECL_NTPARMS (maintmpl));
  tpd2.parms = 0;
  tpd2.parms = 0;
  for (i = 0; i < nargs; ++i)
  for (i = 0; i < nargs; ++i)
    {
    {
      tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
      tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
      tree arg = TREE_VEC_ELT (inner_args, i);
      tree arg = TREE_VEC_ELT (inner_args, i);
      tree packed_args = NULL_TREE;
      tree packed_args = NULL_TREE;
      int j, len = 1;
      int j, len = 1;
 
 
      if (ARGUMENT_PACK_P (arg))
      if (ARGUMENT_PACK_P (arg))
        {
        {
          /* Extract the arguments from the argument pack. We'll be
          /* Extract the arguments from the argument pack. We'll be
             iterating over these in the following loop.  */
             iterating over these in the following loop.  */
          packed_args = ARGUMENT_PACK_ARGS (arg);
          packed_args = ARGUMENT_PACK_ARGS (arg);
          len = TREE_VEC_LENGTH (packed_args);
          len = TREE_VEC_LENGTH (packed_args);
        }
        }
 
 
      for (j = 0; j < len; j++)
      for (j = 0; j < len; j++)
        {
        {
          if (packed_args)
          if (packed_args)
            /* Get the Jth argument in the parameter pack.  */
            /* Get the Jth argument in the parameter pack.  */
            arg = TREE_VEC_ELT (packed_args, j);
            arg = TREE_VEC_ELT (packed_args, j);
 
 
          if (PACK_EXPANSION_P (arg))
          if (PACK_EXPANSION_P (arg))
            {
            {
              /* Pack expansions must come at the end of the
              /* Pack expansions must come at the end of the
                 argument list.  */
                 argument list.  */
              if ((packed_args && j < len - 1)
              if ((packed_args && j < len - 1)
                  || (!packed_args && i < nargs - 1))
                  || (!packed_args && i < nargs - 1))
                {
                {
                  if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
                  if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
                    error ("parameter pack argument %qE must be at the "
                    error ("parameter pack argument %qE must be at the "
                           "end of the template argument list", arg);
                           "end of the template argument list", arg);
                  else
                  else
                    error ("parameter pack argument %qT must be at the "
                    error ("parameter pack argument %qT must be at the "
                           "end of the template argument list", arg);
                           "end of the template argument list", arg);
                }
                }
            }
            }
 
 
          if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
          if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
            /* We only care about the pattern.  */
            /* We only care about the pattern.  */
            arg = PACK_EXPANSION_PATTERN (arg);
            arg = PACK_EXPANSION_PATTERN (arg);
 
 
          if (/* These first two lines are the `non-type' bit.  */
          if (/* These first two lines are the `non-type' bit.  */
              !TYPE_P (arg)
              !TYPE_P (arg)
              && TREE_CODE (arg) != TEMPLATE_DECL
              && TREE_CODE (arg) != TEMPLATE_DECL
              /* This next line is the `argument expression is not just a
              /* This next line is the `argument expression is not just a
                 simple identifier' condition and also the `specialized
                 simple identifier' condition and also the `specialized
                 non-type argument' bit.  */
                 non-type argument' bit.  */
              && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
              && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
            {
            {
              if ((!packed_args && tpd.arg_uses_template_parms[i])
              if ((!packed_args && tpd.arg_uses_template_parms[i])
                  || (packed_args && uses_template_parms (arg)))
                  || (packed_args && uses_template_parms (arg)))
                error ("template argument %qE involves template parameter(s)",
                error ("template argument %qE involves template parameter(s)",
                       arg);
                       arg);
              else
              else
                {
                {
                  /* Look at the corresponding template parameter,
                  /* Look at the corresponding template parameter,
                     marking which template parameters its type depends
                     marking which template parameters its type depends
                     upon.  */
                     upon.  */
                  tree type = TREE_TYPE (parm);
                  tree type = TREE_TYPE (parm);
 
 
                  if (!tpd2.parms)
                  if (!tpd2.parms)
                    {
                    {
                      /* We haven't yet initialized TPD2.  Do so now.  */
                      /* We haven't yet initialized TPD2.  Do so now.  */
                      tpd2.arg_uses_template_parms
                      tpd2.arg_uses_template_parms
                        = (int *) alloca (sizeof (int) * nargs);
                        = (int *) alloca (sizeof (int) * nargs);
                      /* The number of parameters here is the number in the
                      /* The number of parameters here is the number in the
                         main template, which, as checked in the assertion
                         main template, which, as checked in the assertion
                         above, is NARGS.  */
                         above, is NARGS.  */
                      tpd2.parms = (int *) alloca (sizeof (int) * nargs);
                      tpd2.parms = (int *) alloca (sizeof (int) * nargs);
                      tpd2.level =
                      tpd2.level =
                        TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
                        TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
                    }
                    }
 
 
                  /* Mark the template parameters.  But this time, we're
                  /* Mark the template parameters.  But this time, we're
                     looking for the template parameters of the main
                     looking for the template parameters of the main
                     template, not in the specialization.  */
                     template, not in the specialization.  */
                  tpd2.current_arg = i;
                  tpd2.current_arg = i;
                  tpd2.arg_uses_template_parms[i] = 0;
                  tpd2.arg_uses_template_parms[i] = 0;
                  memset (tpd2.parms, 0, sizeof (int) * nargs);
                  memset (tpd2.parms, 0, sizeof (int) * nargs);
                  for_each_template_parm (type,
                  for_each_template_parm (type,
                                          &mark_template_parm,
                                          &mark_template_parm,
                                          &tpd2,
                                          &tpd2,
                                          NULL,
                                          NULL,
                                          /*include_nondeduced_p=*/false);
                                          /*include_nondeduced_p=*/false);
 
 
                  if (tpd2.arg_uses_template_parms [i])
                  if (tpd2.arg_uses_template_parms [i])
                    {
                    {
                      /* The type depended on some template parameters.
                      /* The type depended on some template parameters.
                         If they are fully specialized in the
                         If they are fully specialized in the
                         specialization, that's OK.  */
                         specialization, that's OK.  */
                      int j;
                      int j;
                      int count = 0;
                      int count = 0;
                      for (j = 0; j < nargs; ++j)
                      for (j = 0; j < nargs; ++j)
                        if (tpd2.parms[j] != 0
                        if (tpd2.parms[j] != 0
                            && tpd.arg_uses_template_parms [j])
                            && tpd.arg_uses_template_parms [j])
                          ++count;
                          ++count;
                      if (count != 0)
                      if (count != 0)
                        error_n (input_location, count,
                        error_n (input_location, count,
                                 "type %qT of template argument %qE depends "
                                 "type %qT of template argument %qE depends "
                                 "on a template parameter",
                                 "on a template parameter",
                                 "type %qT of template argument %qE depends "
                                 "type %qT of template argument %qE depends "
                                 "on template parameters",
                                 "on template parameters",
                                 type,
                                 type,
                                 arg);
                                 arg);
                    }
                    }
                }
                }
            }
            }
        }
        }
    }
    }
 
 
  /* We should only get here once.  */
  /* We should only get here once.  */
  gcc_assert (!COMPLETE_TYPE_P (type));
  gcc_assert (!COMPLETE_TYPE_P (type));
 
 
  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    = tree_cons (specargs, inner_parms,
    = tree_cons (specargs, inner_parms,
                 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
                 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
  TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
  TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
  return decl;
  return decl;
}
}
 
 
/* Check that a template declaration's use of default arguments and
/* Check that a template declaration's use of default arguments and
   parameter packs is not invalid.  Here, PARMS are the template
   parameter packs is not invalid.  Here, PARMS are the template
   parameters.  IS_PRIMARY is nonzero if DECL is the thing declared by
   parameters.  IS_PRIMARY is nonzero if DECL is the thing declared by
   a primary template.  IS_PARTIAL is nonzero if DECL is a partial
   a primary template.  IS_PARTIAL is nonzero if DECL is a partial
   specialization.
   specialization.
 
 
 
 
   IS_FRIEND_DECL is nonzero if DECL is a friend function template
   IS_FRIEND_DECL is nonzero if DECL is a friend function template
   declaration (but not a definition); 1 indicates a declaration, 2
   declaration (but not a definition); 1 indicates a declaration, 2
   indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
   indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
   emitted for extraneous default arguments.
   emitted for extraneous default arguments.
 
 
   Returns TRUE if there were no errors found, FALSE otherwise. */
   Returns TRUE if there were no errors found, FALSE otherwise. */
 
 
bool
bool
check_default_tmpl_args (tree decl, tree parms, int is_primary,
check_default_tmpl_args (tree decl, tree parms, int is_primary,
                         int is_partial, int is_friend_decl)
                         int is_partial, int is_friend_decl)
{
{
  const char *msg;
  const char *msg;
  int last_level_to_check;
  int last_level_to_check;
  tree parm_level;
  tree parm_level;
  bool no_errors = true;
  bool no_errors = true;
 
 
  /* [temp.param]
  /* [temp.param]
 
 
     A default template-argument shall not be specified in a
     A default template-argument shall not be specified in a
     function template declaration or a function template definition, nor
     function template declaration or a function template definition, nor
     in the template-parameter-list of the definition of a member of a
     in the template-parameter-list of the definition of a member of a
     class template.  */
     class template.  */
 
 
  if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
  if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
    /* You can't have a function template declaration in a local
    /* You can't have a function template declaration in a local
       scope, nor you can you define a member of a class template in a
       scope, nor you can you define a member of a class template in a
       local scope.  */
       local scope.  */
    return true;
    return true;
 
 
  if (current_class_type
  if (current_class_type
      && !TYPE_BEING_DEFINED (current_class_type)
      && !TYPE_BEING_DEFINED (current_class_type)
      && DECL_LANG_SPECIFIC (decl)
      && DECL_LANG_SPECIFIC (decl)
      && DECL_DECLARES_FUNCTION_P (decl)
      && DECL_DECLARES_FUNCTION_P (decl)
      /* If this is either a friend defined in the scope of the class
      /* If this is either a friend defined in the scope of the class
         or a member function.  */
         or a member function.  */
      && (DECL_FUNCTION_MEMBER_P (decl)
      && (DECL_FUNCTION_MEMBER_P (decl)
          ? same_type_p (DECL_CONTEXT (decl), current_class_type)
          ? same_type_p (DECL_CONTEXT (decl), current_class_type)
          : DECL_FRIEND_CONTEXT (decl)
          : DECL_FRIEND_CONTEXT (decl)
          ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
          ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
          : false)
          : false)
      /* And, if it was a member function, it really was defined in
      /* And, if it was a member function, it really was defined in
         the scope of the class.  */
         the scope of the class.  */
      && (!DECL_FUNCTION_MEMBER_P (decl)
      && (!DECL_FUNCTION_MEMBER_P (decl)
          || DECL_INITIALIZED_IN_CLASS_P (decl)))
          || DECL_INITIALIZED_IN_CLASS_P (decl)))
    /* We already checked these parameters when the template was
    /* We already checked these parameters when the template was
       declared, so there's no need to do it again now.  This function
       declared, so there's no need to do it again now.  This function
       was defined in class scope, but we're processing it's body now
       was defined in class scope, but we're processing it's body now
       that the class is complete.  */
       that the class is complete.  */
    return true;
    return true;
 
 
  /* Core issue 226 (C++0x only): the following only applies to class
  /* Core issue 226 (C++0x only): the following only applies to class
     templates.  */
     templates.  */
  if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
  if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
    {
    {
      /* [temp.param]
      /* [temp.param]
 
 
         If a template-parameter has a default template-argument, all
         If a template-parameter has a default template-argument, all
         subsequent template-parameters shall have a default
         subsequent template-parameters shall have a default
         template-argument supplied.  */
         template-argument supplied.  */
      for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
      for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
        {
        {
          tree inner_parms = TREE_VALUE (parm_level);
          tree inner_parms = TREE_VALUE (parm_level);
          int ntparms = TREE_VEC_LENGTH (inner_parms);
          int ntparms = TREE_VEC_LENGTH (inner_parms);
          int seen_def_arg_p = 0;
          int seen_def_arg_p = 0;
          int i;
          int i;
 
 
          for (i = 0; i < ntparms; ++i)
          for (i = 0; i < ntparms; ++i)
            {
            {
              tree parm = TREE_VEC_ELT (inner_parms, i);
              tree parm = TREE_VEC_ELT (inner_parms, i);
 
 
              if (parm == error_mark_node)
              if (parm == error_mark_node)
                continue;
                continue;
 
 
              if (TREE_PURPOSE (parm))
              if (TREE_PURPOSE (parm))
                seen_def_arg_p = 1;
                seen_def_arg_p = 1;
              else if (seen_def_arg_p
              else if (seen_def_arg_p
                       && !template_parameter_pack_p (TREE_VALUE (parm)))
                       && !template_parameter_pack_p (TREE_VALUE (parm)))
                {
                {
                  error ("no default argument for %qD", TREE_VALUE (parm));
                  error ("no default argument for %qD", TREE_VALUE (parm));
                  /* For better subsequent error-recovery, we indicate that
                  /* For better subsequent error-recovery, we indicate that
                     there should have been a default argument.  */
                     there should have been a default argument.  */
                  TREE_PURPOSE (parm) = error_mark_node;
                  TREE_PURPOSE (parm) = error_mark_node;
                  no_errors = false;
                  no_errors = false;
                }
                }
              else if (is_primary
              else if (is_primary
                       && !is_partial
                       && !is_partial
                       && !is_friend_decl
                       && !is_friend_decl
                       /* Don't complain about an enclosing partial
                       /* Don't complain about an enclosing partial
                          specialization.  */
                          specialization.  */
                       && parm_level == parms
                       && parm_level == parms
                       && TREE_CODE (decl) == TYPE_DECL
                       && TREE_CODE (decl) == TYPE_DECL
                       && i < ntparms - 1
                       && i < ntparms - 1
                       && template_parameter_pack_p (TREE_VALUE (parm)))
                       && template_parameter_pack_p (TREE_VALUE (parm)))
                {
                {
                  /* A primary class template can only have one
                  /* A primary class template can only have one
                     parameter pack, at the end of the template
                     parameter pack, at the end of the template
                     parameter list.  */
                     parameter list.  */
 
 
                  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
                  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
                    error ("parameter pack %qE must be at the end of the"
                    error ("parameter pack %qE must be at the end of the"
                           " template parameter list", TREE_VALUE (parm));
                           " template parameter list", TREE_VALUE (parm));
                  else
                  else
                    error ("parameter pack %qT must be at the end of the"
                    error ("parameter pack %qT must be at the end of the"
                           " template parameter list",
                           " template parameter list",
                           TREE_TYPE (TREE_VALUE (parm)));
                           TREE_TYPE (TREE_VALUE (parm)));
 
 
                  TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
                  TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
                    = error_mark_node;
                    = error_mark_node;
                  no_errors = false;
                  no_errors = false;
                }
                }
            }
            }
        }
        }
    }
    }
 
 
  if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
  if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
      || is_partial
      || is_partial
      || !is_primary
      || !is_primary
      || is_friend_decl)
      || is_friend_decl)
    /* For an ordinary class template, default template arguments are
    /* For an ordinary class template, default template arguments are
       allowed at the innermost level, e.g.:
       allowed at the innermost level, e.g.:
         template <class T = int>
         template <class T = int>
         struct S {};
         struct S {};
       but, in a partial specialization, they're not allowed even
       but, in a partial specialization, they're not allowed even
       there, as we have in [temp.class.spec]:
       there, as we have in [temp.class.spec]:
 
 
         The template parameter list of a specialization shall not
         The template parameter list of a specialization shall not
         contain default template argument values.
         contain default template argument values.
 
 
       So, for a partial specialization, or for a function template
       So, for a partial specialization, or for a function template
       (in C++98/C++03), we look at all of them.  */
       (in C++98/C++03), we look at all of them.  */
    ;
    ;
  else
  else
    /* But, for a primary class template that is not a partial
    /* But, for a primary class template that is not a partial
       specialization we look at all template parameters except the
       specialization we look at all template parameters except the
       innermost ones.  */
       innermost ones.  */
    parms = TREE_CHAIN (parms);
    parms = TREE_CHAIN (parms);
 
 
  /* Figure out what error message to issue.  */
  /* Figure out what error message to issue.  */
  if (is_friend_decl == 2)
  if (is_friend_decl == 2)
    msg = G_("default template arguments may not be used in function template "
    msg = G_("default template arguments may not be used in function template "
             "friend re-declaration");
             "friend re-declaration");
  else if (is_friend_decl)
  else if (is_friend_decl)
    msg = G_("default template arguments may not be used in function template "
    msg = G_("default template arguments may not be used in function template "
             "friend declarations");
             "friend declarations");
  else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
  else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
    msg = G_("default template arguments may not be used in function templates "
    msg = G_("default template arguments may not be used in function templates "
             "without -std=c++0x or -std=gnu++0x");
             "without -std=c++0x or -std=gnu++0x");
  else if (is_partial)
  else if (is_partial)
    msg = G_("default template arguments may not be used in "
    msg = G_("default template arguments may not be used in "
             "partial specializations");
             "partial specializations");
  else
  else
    msg = G_("default argument for template parameter for class enclosing %qD");
    msg = G_("default argument for template parameter for class enclosing %qD");
 
 
  if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
  if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
    /* If we're inside a class definition, there's no need to
    /* If we're inside a class definition, there's no need to
       examine the parameters to the class itself.  On the one
       examine the parameters to the class itself.  On the one
       hand, they will be checked when the class is defined, and,
       hand, they will be checked when the class is defined, and,
       on the other, default arguments are valid in things like:
       on the other, default arguments are valid in things like:
         template <class T = double>
         template <class T = double>
         struct S { template <class U> void f(U); };
         struct S { template <class U> void f(U); };
       Here the default argument for `S' has no bearing on the
       Here the default argument for `S' has no bearing on the
       declaration of `f'.  */
       declaration of `f'.  */
    last_level_to_check = template_class_depth (current_class_type) + 1;
    last_level_to_check = template_class_depth (current_class_type) + 1;
  else
  else
    /* Check everything.  */
    /* Check everything.  */
    last_level_to_check = 0;
    last_level_to_check = 0;
 
 
  for (parm_level = parms;
  for (parm_level = parms;
       parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
       parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
       parm_level = TREE_CHAIN (parm_level))
       parm_level = TREE_CHAIN (parm_level))
    {
    {
      tree inner_parms = TREE_VALUE (parm_level);
      tree inner_parms = TREE_VALUE (parm_level);
      int i;
      int i;
      int ntparms;
      int ntparms;
 
 
      ntparms = TREE_VEC_LENGTH (inner_parms);
      ntparms = TREE_VEC_LENGTH (inner_parms);
      for (i = 0; i < ntparms; ++i)
      for (i = 0; i < ntparms; ++i)
        {
        {
          if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
          if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
            continue;
            continue;
 
 
          if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
          if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
            {
            {
              if (msg)
              if (msg)
                {
                {
                  no_errors = false;
                  no_errors = false;
                  if (is_friend_decl == 2)
                  if (is_friend_decl == 2)
                    return no_errors;
                    return no_errors;
 
 
                  error (msg, decl);
                  error (msg, decl);
                  msg = 0;
                  msg = 0;
                }
                }
 
 
              /* Clear out the default argument so that we are not
              /* Clear out the default argument so that we are not
                 confused later.  */
                 confused later.  */
              TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
              TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
            }
            }
        }
        }
 
 
      /* At this point, if we're still interested in issuing messages,
      /* At this point, if we're still interested in issuing messages,
         they must apply to classes surrounding the object declared.  */
         they must apply to classes surrounding the object declared.  */
      if (msg)
      if (msg)
        msg = G_("default argument for template parameter for class "
        msg = G_("default argument for template parameter for class "
                 "enclosing %qD");
                 "enclosing %qD");
    }
    }
 
 
  return no_errors;
  return no_errors;
}
}
 
 
/* Worker for push_template_decl_real, called via
/* Worker for push_template_decl_real, called via
   for_each_template_parm.  DATA is really an int, indicating the
   for_each_template_parm.  DATA is really an int, indicating the
   level of the parameters we are interested in.  If T is a template
   level of the parameters we are interested in.  If T is a template
   parameter of that level, return nonzero.  */
   parameter of that level, return nonzero.  */
 
 
static int
static int
template_parm_this_level_p (tree t, void* data)
template_parm_this_level_p (tree t, void* data)
{
{
  int this_level = *(int *)data;
  int this_level = *(int *)data;
  int level;
  int level;
 
 
  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    level = TEMPLATE_PARM_LEVEL (t);
    level = TEMPLATE_PARM_LEVEL (t);
  else
  else
    level = TEMPLATE_TYPE_LEVEL (t);
    level = TEMPLATE_TYPE_LEVEL (t);
  return level == this_level;
  return level == this_level;
}
}
 
 
/* Creates a TEMPLATE_DECL for the indicated DECL using the template
/* Creates a TEMPLATE_DECL for the indicated DECL using the template
   parameters given by current_template_args, or reuses a
   parameters given by current_template_args, or reuses a
   previously existing one, if appropriate.  Returns the DECL, or an
   previously existing one, if appropriate.  Returns the DECL, or an
   equivalent one, if it is replaced via a call to duplicate_decls.
   equivalent one, if it is replaced via a call to duplicate_decls.
 
 
   If IS_FRIEND is true, DECL is a friend declaration.  */
   If IS_FRIEND is true, DECL is a friend declaration.  */
 
 
tree
tree
push_template_decl_real (tree decl, bool is_friend)
push_template_decl_real (tree decl, bool is_friend)
{
{
  tree tmpl;
  tree tmpl;
  tree args;
  tree args;
  tree info;
  tree info;
  tree ctx;
  tree ctx;
  int primary;
  int primary;
  int is_partial;
  int is_partial;
  int new_template_p = 0;
  int new_template_p = 0;
  /* True if the template is a member template, in the sense of
  /* True if the template is a member template, in the sense of
     [temp.mem].  */
     [temp.mem].  */
  bool member_template_p = false;
  bool member_template_p = false;
 
 
  if (decl == error_mark_node || !current_template_parms)
  if (decl == error_mark_node || !current_template_parms)
    return error_mark_node;
    return error_mark_node;
 
 
  /* See if this is a partial specialization.  */
  /* See if this is a partial specialization.  */
  is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
  is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
                && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
                && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
                && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
                && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
 
 
  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
    is_friend = true;
    is_friend = true;
 
 
  if (is_friend)
  if (is_friend)
    /* For a friend, we want the context of the friend function, not
    /* For a friend, we want the context of the friend function, not
       the type of which it is a friend.  */
       the type of which it is a friend.  */
    ctx = DECL_CONTEXT (decl);
    ctx = DECL_CONTEXT (decl);
  else if (CP_DECL_CONTEXT (decl)
  else if (CP_DECL_CONTEXT (decl)
           && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
           && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
    /* In the case of a virtual function, we want the class in which
    /* In the case of a virtual function, we want the class in which
       it is defined.  */
       it is defined.  */
    ctx = CP_DECL_CONTEXT (decl);
    ctx = CP_DECL_CONTEXT (decl);
  else
  else
    /* Otherwise, if we're currently defining some class, the DECL
    /* Otherwise, if we're currently defining some class, the DECL
       is assumed to be a member of the class.  */
       is assumed to be a member of the class.  */
    ctx = current_scope ();
    ctx = current_scope ();
 
 
  if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
  if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    ctx = NULL_TREE;
    ctx = NULL_TREE;
 
 
  if (!DECL_CONTEXT (decl))
  if (!DECL_CONTEXT (decl))
    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
 
 
  /* See if this is a primary template.  */
  /* See if this is a primary template.  */
  if (is_friend && ctx)
  if (is_friend && ctx)
    /* A friend template that specifies a class context, i.e.
    /* A friend template that specifies a class context, i.e.
         template <typename T> friend void A<T>::f();
         template <typename T> friend void A<T>::f();
       is not primary.  */
       is not primary.  */
    primary = 0;
    primary = 0;
  else
  else
    primary = template_parm_scope_p ();
    primary = template_parm_scope_p ();
 
 
  if (primary)
  if (primary)
    {
    {
      if (DECL_CLASS_SCOPE_P (decl))
      if (DECL_CLASS_SCOPE_P (decl))
        member_template_p = true;
        member_template_p = true;
      if (TREE_CODE (decl) == TYPE_DECL
      if (TREE_CODE (decl) == TYPE_DECL
          && ANON_AGGRNAME_P (DECL_NAME (decl)))
          && ANON_AGGRNAME_P (DECL_NAME (decl)))
        {
        {
          error ("template class without a name");
          error ("template class without a name");
          return error_mark_node;
          return error_mark_node;
        }
        }
      else if (TREE_CODE (decl) == FUNCTION_DECL)
      else if (TREE_CODE (decl) == FUNCTION_DECL)
        {
        {
          if (DECL_DESTRUCTOR_P (decl))
          if (DECL_DESTRUCTOR_P (decl))
            {
            {
              /* [temp.mem]
              /* [temp.mem]
 
 
                 A destructor shall not be a member template.  */
                 A destructor shall not be a member template.  */
              error ("destructor %qD declared as member template", decl);
              error ("destructor %qD declared as member template", decl);
              return error_mark_node;
              return error_mark_node;
            }
            }
          if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
          if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
              && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
              && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
                  || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
                  || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
                  || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
                  || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
                  || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
                  || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
                      == void_list_node)))
                      == void_list_node)))
            {
            {
              /* [basic.stc.dynamic.allocation]
              /* [basic.stc.dynamic.allocation]
 
 
                 An allocation function can be a function
                 An allocation function can be a function
                 template. ... Template allocation functions shall
                 template. ... Template allocation functions shall
                 have two or more parameters.  */
                 have two or more parameters.  */
              error ("invalid template declaration of %qD", decl);
              error ("invalid template declaration of %qD", decl);
              return error_mark_node;
              return error_mark_node;
            }
            }
        }
        }
      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
               && CLASS_TYPE_P (TREE_TYPE (decl)))
               && CLASS_TYPE_P (TREE_TYPE (decl)))
        /* OK */;
        /* OK */;
      else
      else
        {
        {
          error ("template declaration of %q#D", decl);
          error ("template declaration of %q#D", decl);
          return error_mark_node;
          return error_mark_node;
        }
        }
    }
    }
 
 
  /* Check to see that the rules regarding the use of default
  /* Check to see that the rules regarding the use of default
     arguments are not being violated.  */
     arguments are not being violated.  */
  check_default_tmpl_args (decl, current_template_parms,
  check_default_tmpl_args (decl, current_template_parms,
                           primary, is_partial, /*is_friend_decl=*/0);
                           primary, is_partial, /*is_friend_decl=*/0);
 
 
  /* Ensure that there are no parameter packs in the type of this
  /* Ensure that there are no parameter packs in the type of this
     declaration that have not been expanded.  */
     declaration that have not been expanded.  */
  if (TREE_CODE (decl) == FUNCTION_DECL)
  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
    {
      /* Check each of the arguments individually to see if there are
      /* Check each of the arguments individually to see if there are
         any bare parameter packs.  */
         any bare parameter packs.  */
      tree type = TREE_TYPE (decl);
      tree type = TREE_TYPE (decl);
      tree arg = DECL_ARGUMENTS (decl);
      tree arg = DECL_ARGUMENTS (decl);
      tree argtype = TYPE_ARG_TYPES (type);
      tree argtype = TYPE_ARG_TYPES (type);
 
 
      while (arg && argtype)
      while (arg && argtype)
        {
        {
          if (!FUNCTION_PARAMETER_PACK_P (arg)
          if (!FUNCTION_PARAMETER_PACK_P (arg)
              && check_for_bare_parameter_packs (TREE_TYPE (arg)))
              && check_for_bare_parameter_packs (TREE_TYPE (arg)))
            {
            {
            /* This is a PARM_DECL that contains unexpanded parameter
            /* This is a PARM_DECL that contains unexpanded parameter
               packs. We have already complained about this in the
               packs. We have already complained about this in the
               check_for_bare_parameter_packs call, so just replace
               check_for_bare_parameter_packs call, so just replace
               these types with ERROR_MARK_NODE.  */
               these types with ERROR_MARK_NODE.  */
              TREE_TYPE (arg) = error_mark_node;
              TREE_TYPE (arg) = error_mark_node;
              TREE_VALUE (argtype) = error_mark_node;
              TREE_VALUE (argtype) = error_mark_node;
            }
            }
 
 
          arg = TREE_CHAIN (arg);
          arg = TREE_CHAIN (arg);
          argtype = TREE_CHAIN (argtype);
          argtype = TREE_CHAIN (argtype);
        }
        }
 
 
      /* Check for bare parameter packs in the return type and the
      /* Check for bare parameter packs in the return type and the
         exception specifiers.  */
         exception specifiers.  */
      if (check_for_bare_parameter_packs (TREE_TYPE (type)))
      if (check_for_bare_parameter_packs (TREE_TYPE (type)))
        /* Errors were already issued, set return type to int
        /* Errors were already issued, set return type to int
           as the frontend doesn't expect error_mark_node as
           as the frontend doesn't expect error_mark_node as
           the return type.  */
           the return type.  */
        TREE_TYPE (type) = integer_type_node;
        TREE_TYPE (type) = integer_type_node;
      if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
      if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
        TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
        TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    }
    }
  else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
  else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
    {
    {
      TREE_TYPE (decl) = error_mark_node;
      TREE_TYPE (decl) = error_mark_node;
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  if (is_partial)
  if (is_partial)
    return process_partial_specialization (decl);
    return process_partial_specialization (decl);
 
 
  args = current_template_args ();
  args = current_template_args ();
 
 
  if (!ctx
  if (!ctx
      || TREE_CODE (ctx) == FUNCTION_DECL
      || TREE_CODE (ctx) == FUNCTION_DECL
      || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
      || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
      || (is_friend && !DECL_TEMPLATE_INFO (decl)))
      || (is_friend && !DECL_TEMPLATE_INFO (decl)))
    {
    {
      if (DECL_LANG_SPECIFIC (decl)
      if (DECL_LANG_SPECIFIC (decl)
          && DECL_TEMPLATE_INFO (decl)
          && DECL_TEMPLATE_INFO (decl)
          && DECL_TI_TEMPLATE (decl))
          && DECL_TI_TEMPLATE (decl))
        tmpl = DECL_TI_TEMPLATE (decl);
        tmpl = DECL_TI_TEMPLATE (decl);
      /* If DECL is a TYPE_DECL for a class-template, then there won't
      /* If DECL is a TYPE_DECL for a class-template, then there won't
         be DECL_LANG_SPECIFIC.  The information equivalent to
         be DECL_LANG_SPECIFIC.  The information equivalent to
         DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
         DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
               && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
               && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
               && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
               && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
        {
        {
          /* Since a template declaration already existed for this
          /* Since a template declaration already existed for this
             class-type, we must be redeclaring it here.  Make sure
             class-type, we must be redeclaring it here.  Make sure
             that the redeclaration is valid.  */
             that the redeclaration is valid.  */
          redeclare_class_template (TREE_TYPE (decl),
          redeclare_class_template (TREE_TYPE (decl),
                                    current_template_parms);
                                    current_template_parms);
          /* We don't need to create a new TEMPLATE_DECL; just use the
          /* We don't need to create a new TEMPLATE_DECL; just use the
             one we already had.  */
             one we already had.  */
          tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
          tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
        }
        }
      else
      else
        {
        {
          tmpl = build_template_decl (decl, current_template_parms,
          tmpl = build_template_decl (decl, current_template_parms,
                                      member_template_p);
                                      member_template_p);
          new_template_p = 1;
          new_template_p = 1;
 
 
          if (DECL_LANG_SPECIFIC (decl)
          if (DECL_LANG_SPECIFIC (decl)
              && DECL_TEMPLATE_SPECIALIZATION (decl))
              && DECL_TEMPLATE_SPECIALIZATION (decl))
            {
            {
              /* A specialization of a member template of a template
              /* A specialization of a member template of a template
                 class.  */
                 class.  */
              SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
              SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
              DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
              DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
              DECL_TEMPLATE_INFO (decl) = NULL_TREE;
              DECL_TEMPLATE_INFO (decl) = NULL_TREE;
            }
            }
        }
        }
    }
    }
  else
  else
    {
    {
      tree a, t, current, parms;
      tree a, t, current, parms;
      int i;
      int i;
      tree tinfo = get_template_info (decl);
      tree tinfo = get_template_info (decl);
 
 
      if (!tinfo)
      if (!tinfo)
        {
        {
          error ("template definition of non-template %q#D", decl);
          error ("template definition of non-template %q#D", decl);
          return error_mark_node;
          return error_mark_node;
        }
        }
 
 
      tmpl = TI_TEMPLATE (tinfo);
      tmpl = TI_TEMPLATE (tinfo);
 
 
      if (DECL_FUNCTION_TEMPLATE_P (tmpl)
      if (DECL_FUNCTION_TEMPLATE_P (tmpl)
          && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
          && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
          && DECL_TEMPLATE_SPECIALIZATION (decl)
          && DECL_TEMPLATE_SPECIALIZATION (decl)
          && DECL_MEMBER_TEMPLATE_P (tmpl))
          && DECL_MEMBER_TEMPLATE_P (tmpl))
        {
        {
          tree new_tmpl;
          tree new_tmpl;
 
 
          /* The declaration is a specialization of a member
          /* The declaration is a specialization of a member
             template, declared outside the class.  Therefore, the
             template, declared outside the class.  Therefore, the
             innermost template arguments will be NULL, so we
             innermost template arguments will be NULL, so we
             replace them with the arguments determined by the
             replace them with the arguments determined by the
             earlier call to check_explicit_specialization.  */
             earlier call to check_explicit_specialization.  */
          args = DECL_TI_ARGS (decl);
          args = DECL_TI_ARGS (decl);
 
 
          new_tmpl
          new_tmpl
            = build_template_decl (decl, current_template_parms,
            = build_template_decl (decl, current_template_parms,
                                   member_template_p);
                                   member_template_p);
          DECL_TEMPLATE_RESULT (new_tmpl) = decl;
          DECL_TEMPLATE_RESULT (new_tmpl) = decl;
          TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
          TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
          DECL_TI_TEMPLATE (decl) = new_tmpl;
          DECL_TI_TEMPLATE (decl) = new_tmpl;
          SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
          SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
          DECL_TEMPLATE_INFO (new_tmpl)
          DECL_TEMPLATE_INFO (new_tmpl)
            = build_template_info (tmpl, args);
            = build_template_info (tmpl, args);
 
 
          register_specialization (new_tmpl,
          register_specialization (new_tmpl,
                                   most_general_template (tmpl),
                                   most_general_template (tmpl),
                                   args,
                                   args,
                                   is_friend, 0);
                                   is_friend, 0);
          return decl;
          return decl;
        }
        }
 
 
      /* Make sure the template headers we got make sense.  */
      /* Make sure the template headers we got make sense.  */
 
 
      parms = DECL_TEMPLATE_PARMS (tmpl);
      parms = DECL_TEMPLATE_PARMS (tmpl);
      i = TMPL_PARMS_DEPTH (parms);
      i = TMPL_PARMS_DEPTH (parms);
      if (TMPL_ARGS_DEPTH (args) != i)
      if (TMPL_ARGS_DEPTH (args) != i)
        {
        {
          error ("expected %d levels of template parms for %q#D, got %d",
          error ("expected %d levels of template parms for %q#D, got %d",
                 i, decl, TMPL_ARGS_DEPTH (args));
                 i, decl, TMPL_ARGS_DEPTH (args));
        }
        }
      else
      else
        for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
        for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
          {
          {
            a = TMPL_ARGS_LEVEL (args, i);
            a = TMPL_ARGS_LEVEL (args, i);
            t = INNERMOST_TEMPLATE_PARMS (parms);
            t = INNERMOST_TEMPLATE_PARMS (parms);
 
 
            if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
            if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
              {
              {
                if (current == decl)
                if (current == decl)
                  error ("got %d template parameters for %q#D",
                  error ("got %d template parameters for %q#D",
                         TREE_VEC_LENGTH (a), decl);
                         TREE_VEC_LENGTH (a), decl);
                else
                else
                  error ("got %d template parameters for %q#T",
                  error ("got %d template parameters for %q#T",
                         TREE_VEC_LENGTH (a), current);
                         TREE_VEC_LENGTH (a), current);
                error ("  but %d required", TREE_VEC_LENGTH (t));
                error ("  but %d required", TREE_VEC_LENGTH (t));
                return error_mark_node;
                return error_mark_node;
              }
              }
 
 
            if (current == decl)
            if (current == decl)
              current = ctx;
              current = ctx;
            else if (current == NULL_TREE)
            else if (current == NULL_TREE)
              /* Can happen in erroneous input.  */
              /* Can happen in erroneous input.  */
              break;
              break;
            else
            else
              current = (TYPE_P (current)
              current = (TYPE_P (current)
                         ? TYPE_CONTEXT (current)
                         ? TYPE_CONTEXT (current)
                         : DECL_CONTEXT (current));
                         : DECL_CONTEXT (current));
          }
          }
 
 
      /* Check that the parms are used in the appropriate qualifying scopes
      /* Check that the parms are used in the appropriate qualifying scopes
         in the declarator.  */
         in the declarator.  */
      if (!comp_template_args
      if (!comp_template_args
          (TI_ARGS (tinfo),
          (TI_ARGS (tinfo),
           TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
           TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
        {
        {
          error ("\
          error ("\
template arguments to %qD do not match original template %qD",
template arguments to %qD do not match original template %qD",
                 decl, DECL_TEMPLATE_RESULT (tmpl));
                 decl, DECL_TEMPLATE_RESULT (tmpl));
          if (!uses_template_parms (TI_ARGS (tinfo)))
          if (!uses_template_parms (TI_ARGS (tinfo)))
            inform (input_location, "use template<> for an explicit specialization");
            inform (input_location, "use template<> for an explicit specialization");
          /* Avoid crash in import_export_decl.  */
          /* Avoid crash in import_export_decl.  */
          DECL_INTERFACE_KNOWN (decl) = 1;
          DECL_INTERFACE_KNOWN (decl) = 1;
          return error_mark_node;
          return error_mark_node;
        }
        }
    }
    }
 
 
  DECL_TEMPLATE_RESULT (tmpl) = decl;
  DECL_TEMPLATE_RESULT (tmpl) = decl;
  TREE_TYPE (tmpl) = TREE_TYPE (decl);
  TREE_TYPE (tmpl) = TREE_TYPE (decl);
 
 
  /* Push template declarations for global functions and types.  Note
  /* Push template declarations for global functions and types.  Note
     that we do not try to push a global template friend declared in a
     that we do not try to push a global template friend declared in a
     template class; such a thing may well depend on the template
     template class; such a thing may well depend on the template
     parameters of the class.  */
     parameters of the class.  */
  if (new_template_p && !ctx
  if (new_template_p && !ctx
      && !(is_friend && template_class_depth (current_class_type) > 0))
      && !(is_friend && template_class_depth (current_class_type) > 0))
    {
    {
      tmpl = pushdecl_namespace_level (tmpl, is_friend);
      tmpl = pushdecl_namespace_level (tmpl, is_friend);
      if (tmpl == error_mark_node)
      if (tmpl == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* Hide template friend classes that haven't been declared yet.  */
      /* Hide template friend classes that haven't been declared yet.  */
      if (is_friend && TREE_CODE (decl) == TYPE_DECL)
      if (is_friend && TREE_CODE (decl) == TYPE_DECL)
        {
        {
          DECL_ANTICIPATED (tmpl) = 1;
          DECL_ANTICIPATED (tmpl) = 1;
          DECL_FRIEND_P (tmpl) = 1;
          DECL_FRIEND_P (tmpl) = 1;
        }
        }
    }
    }
 
 
  if (primary)
  if (primary)
    {
    {
      tree parms = DECL_TEMPLATE_PARMS (tmpl);
      tree parms = DECL_TEMPLATE_PARMS (tmpl);
      int i;
      int i;
 
 
      DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
      DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
      if (DECL_CONV_FN_P (tmpl))
      if (DECL_CONV_FN_P (tmpl))
        {
        {
          int depth = TMPL_PARMS_DEPTH (parms);
          int depth = TMPL_PARMS_DEPTH (parms);
 
 
          /* It is a conversion operator. See if the type converted to
          /* It is a conversion operator. See if the type converted to
             depends on innermost template operands.  */
             depends on innermost template operands.  */
 
 
          if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
          if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
                                         depth))
                                         depth))
            DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
            DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
        }
        }
 
 
      /* Give template template parms a DECL_CONTEXT of the template
      /* Give template template parms a DECL_CONTEXT of the template
         for which they are a parameter.  */
         for which they are a parameter.  */
      parms = INNERMOST_TEMPLATE_PARMS (parms);
      parms = INNERMOST_TEMPLATE_PARMS (parms);
      for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
      for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
        {
        {
          tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
          tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
          if (TREE_CODE (parm) == TEMPLATE_DECL)
          if (TREE_CODE (parm) == TEMPLATE_DECL)
            DECL_CONTEXT (parm) = tmpl;
            DECL_CONTEXT (parm) = tmpl;
        }
        }
    }
    }
 
 
  /* The DECL_TI_ARGS of DECL contains full set of arguments referring
  /* The DECL_TI_ARGS of DECL contains full set of arguments referring
     back to its most general template.  If TMPL is a specialization,
     back to its most general template.  If TMPL is a specialization,
     ARGS may only have the innermost set of arguments.  Add the missing
     ARGS may only have the innermost set of arguments.  Add the missing
     argument levels if necessary.  */
     argument levels if necessary.  */
  if (DECL_TEMPLATE_INFO (tmpl))
  if (DECL_TEMPLATE_INFO (tmpl))
    args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
 
 
  info = build_template_info (tmpl, args);
  info = build_template_info (tmpl, args);
 
 
  if (DECL_IMPLICIT_TYPEDEF_P (decl))
  if (DECL_IMPLICIT_TYPEDEF_P (decl))
    SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
  else if (DECL_LANG_SPECIFIC (decl))
  else if (DECL_LANG_SPECIFIC (decl))
    DECL_TEMPLATE_INFO (decl) = info;
    DECL_TEMPLATE_INFO (decl) = info;
 
 
  return DECL_TEMPLATE_RESULT (tmpl);
  return DECL_TEMPLATE_RESULT (tmpl);
}
}
 
 
tree
tree
push_template_decl (tree decl)
push_template_decl (tree decl)
{
{
  return push_template_decl_real (decl, false);
  return push_template_decl_real (decl, false);
}
}
 
 
/* Called when a class template TYPE is redeclared with the indicated
/* Called when a class template TYPE is redeclared with the indicated
   template PARMS, e.g.:
   template PARMS, e.g.:
 
 
     template <class T> struct S;
     template <class T> struct S;
     template <class T> struct S {};  */
     template <class T> struct S {};  */
 
 
bool
bool
redeclare_class_template (tree type, tree parms)
redeclare_class_template (tree type, tree parms)
{
{
  tree tmpl;
  tree tmpl;
  tree tmpl_parms;
  tree tmpl_parms;
  int i;
  int i;
 
 
  if (!TYPE_TEMPLATE_INFO (type))
  if (!TYPE_TEMPLATE_INFO (type))
    {
    {
      error ("%qT is not a template type", type);
      error ("%qT is not a template type", type);
      return false;
      return false;
    }
    }
 
 
  tmpl = TYPE_TI_TEMPLATE (type);
  tmpl = TYPE_TI_TEMPLATE (type);
  if (!PRIMARY_TEMPLATE_P (tmpl))
  if (!PRIMARY_TEMPLATE_P (tmpl))
    /* The type is nested in some template class.  Nothing to worry
    /* The type is nested in some template class.  Nothing to worry
       about here; there are no new template parameters for the nested
       about here; there are no new template parameters for the nested
       type.  */
       type.  */
    return true;
    return true;
 
 
  if (!parms)
  if (!parms)
    {
    {
      error ("template specifiers not specified in declaration of %qD",
      error ("template specifiers not specified in declaration of %qD",
             tmpl);
             tmpl);
      return false;
      return false;
    }
    }
 
 
  parms = INNERMOST_TEMPLATE_PARMS (parms);
  parms = INNERMOST_TEMPLATE_PARMS (parms);
  tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
  tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
 
 
  if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
  if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    {
    {
      error_n (input_location, TREE_VEC_LENGTH (parms),
      error_n (input_location, TREE_VEC_LENGTH (parms),
               "redeclared with %d template parameter",
               "redeclared with %d template parameter",
               "redeclared with %d template parameters",
               "redeclared with %d template parameters",
               TREE_VEC_LENGTH (parms));
               TREE_VEC_LENGTH (parms));
      inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
      inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
                "previous declaration %q+D used %d template parameter",
                "previous declaration %q+D used %d template parameter",
                "previous declaration %q+D used %d template parameters",
                "previous declaration %q+D used %d template parameters",
                tmpl, TREE_VEC_LENGTH (tmpl_parms));
                tmpl, TREE_VEC_LENGTH (tmpl_parms));
      return false;
      return false;
    }
    }
 
 
  for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
  for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    {
    {
      tree tmpl_parm;
      tree tmpl_parm;
      tree parm;
      tree parm;
      tree tmpl_default;
      tree tmpl_default;
      tree parm_default;
      tree parm_default;
 
 
      if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
      if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
          || TREE_VEC_ELT (parms, i) == error_mark_node)
          || TREE_VEC_ELT (parms, i) == error_mark_node)
        continue;
        continue;
 
 
      tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
      tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
      if (tmpl_parm == error_mark_node)
      if (tmpl_parm == error_mark_node)
        return false;
        return false;
 
 
      parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
      tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
      parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
      parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
 
 
      /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
      /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
         TEMPLATE_DECL.  */
         TEMPLATE_DECL.  */
      if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
      if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
          || (TREE_CODE (tmpl_parm) != TYPE_DECL
          || (TREE_CODE (tmpl_parm) != TYPE_DECL
              && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
              && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
          || (TREE_CODE (tmpl_parm) != PARM_DECL
          || (TREE_CODE (tmpl_parm) != PARM_DECL
              && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
              && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
                  != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
                  != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
          || (TREE_CODE (tmpl_parm) == PARM_DECL
          || (TREE_CODE (tmpl_parm) == PARM_DECL
              && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
              && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
                  != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
                  != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
        {
        {
          error ("template parameter %q+#D", tmpl_parm);
          error ("template parameter %q+#D", tmpl_parm);
          error ("redeclared here as %q#D", parm);
          error ("redeclared here as %q#D", parm);
          return false;
          return false;
        }
        }
 
 
      if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
      if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
        {
        {
          /* We have in [temp.param]:
          /* We have in [temp.param]:
 
 
             A template-parameter may not be given default arguments
             A template-parameter may not be given default arguments
             by two different declarations in the same scope.  */
             by two different declarations in the same scope.  */
          error_at (input_location, "redefinition of default argument for %q#D", parm);
          error_at (input_location, "redefinition of default argument for %q#D", parm);
          inform (DECL_SOURCE_LOCATION (tmpl_parm),
          inform (DECL_SOURCE_LOCATION (tmpl_parm),
                  "original definition appeared here");
                  "original definition appeared here");
          return false;
          return false;
        }
        }
 
 
      if (parm_default != NULL_TREE)
      if (parm_default != NULL_TREE)
        /* Update the previous template parameters (which are the ones
        /* Update the previous template parameters (which are the ones
           that will really count) with the new default value.  */
           that will really count) with the new default value.  */
        TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
        TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
      else if (tmpl_default != NULL_TREE)
      else if (tmpl_default != NULL_TREE)
        /* Update the new parameters, too; they'll be used as the
        /* Update the new parameters, too; they'll be used as the
           parameters for any members.  */
           parameters for any members.  */
        TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
        TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
    }
    }
 
 
    return true;
    return true;
}
}
 
 
/* Simplify EXPR if it is a non-dependent expression.  Returns the
/* Simplify EXPR if it is a non-dependent expression.  Returns the
   (possibly simplified) expression.  */
   (possibly simplified) expression.  */
 
 
tree
tree
fold_non_dependent_expr (tree expr)
fold_non_dependent_expr (tree expr)
{
{
  if (expr == NULL_TREE)
  if (expr == NULL_TREE)
    return NULL_TREE;
    return NULL_TREE;
 
 
  /* If we're in a template, but EXPR isn't value dependent, simplify
  /* If we're in a template, but EXPR isn't value dependent, simplify
     it.  We're supposed to treat:
     it.  We're supposed to treat:
 
 
       template <typename T> void f(T[1 + 1]);
       template <typename T> void f(T[1 + 1]);
       template <typename T> void f(T[2]);
       template <typename T> void f(T[2]);
 
 
     as two declarations of the same function, for example.  */
     as two declarations of the same function, for example.  */
  if (processing_template_decl
  if (processing_template_decl
      && !type_dependent_expression_p (expr)
      && !type_dependent_expression_p (expr)
      && !value_dependent_expression_p (expr))
      && !value_dependent_expression_p (expr))
    {
    {
      HOST_WIDE_INT saved_processing_template_decl;
      HOST_WIDE_INT saved_processing_template_decl;
 
 
      saved_processing_template_decl = processing_template_decl;
      saved_processing_template_decl = processing_template_decl;
      processing_template_decl = 0;
      processing_template_decl = 0;
      expr = tsubst_copy_and_build (expr,
      expr = tsubst_copy_and_build (expr,
                                    /*args=*/NULL_TREE,
                                    /*args=*/NULL_TREE,
                                    tf_error,
                                    tf_error,
                                    /*in_decl=*/NULL_TREE,
                                    /*in_decl=*/NULL_TREE,
                                    /*function_p=*/false,
                                    /*function_p=*/false,
                                    /*integral_constant_expression_p=*/true);
                                    /*integral_constant_expression_p=*/true);
      processing_template_decl = saved_processing_template_decl;
      processing_template_decl = saved_processing_template_decl;
    }
    }
  return expr;
  return expr;
}
}
 
 
/* EXPR is an expression which is used in a constant-expression context.
/* EXPR is an expression which is used in a constant-expression context.
   For instance, it could be a VAR_DECL with a constant initializer.
   For instance, it could be a VAR_DECL with a constant initializer.
   Extract the innermost constant expression.
   Extract the innermost constant expression.
 
 
   This is basically a more powerful version of
   This is basically a more powerful version of
   integral_constant_value, which can be used also in templates where
   integral_constant_value, which can be used also in templates where
   initializers can maintain a syntactic rather than semantic form
   initializers can maintain a syntactic rather than semantic form
   (even if they are non-dependent, for access-checking purposes).  */
   (even if they are non-dependent, for access-checking purposes).  */
 
 
static tree
static tree
fold_decl_constant_value (tree expr)
fold_decl_constant_value (tree expr)
{
{
  tree const_expr = expr;
  tree const_expr = expr;
  do
  do
    {
    {
      expr = fold_non_dependent_expr (const_expr);
      expr = fold_non_dependent_expr (const_expr);
      const_expr = integral_constant_value (expr);
      const_expr = integral_constant_value (expr);
    }
    }
  while (expr != const_expr);
  while (expr != const_expr);
 
 
  return expr;
  return expr;
}
}
 
 
/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
   must be a function or a pointer-to-function type, as specified
   must be a function or a pointer-to-function type, as specified
   in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
   in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
   and check that the resulting function has external linkage.  */
   and check that the resulting function has external linkage.  */
 
 
static tree
static tree
convert_nontype_argument_function (tree type, tree expr)
convert_nontype_argument_function (tree type, tree expr)
{
{
  tree fns = expr;
  tree fns = expr;
  tree fn, fn_no_ptr;
  tree fn, fn_no_ptr;
 
 
  fn = instantiate_type (type, fns, tf_none);
  fn = instantiate_type (type, fns, tf_none);
  if (fn == error_mark_node)
  if (fn == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  fn_no_ptr = fn;
  fn_no_ptr = fn;
  if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
  if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
  if (TREE_CODE (fn_no_ptr) == BASELINK)
  if (TREE_CODE (fn_no_ptr) == BASELINK)
    fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
 
 
  /* [temp.arg.nontype]/1
  /* [temp.arg.nontype]/1
 
 
     A template-argument for a non-type, non-template template-parameter
     A template-argument for a non-type, non-template template-parameter
     shall be one of:
     shall be one of:
     [...]
     [...]
     -- the address of an object or function with external linkage.  */
     -- the address of an object or function with external linkage.  */
  if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
  if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
    {
    {
      error ("%qE is not a valid template argument for type %qT "
      error ("%qE is not a valid template argument for type %qT "
             "because function %qD has not external linkage",
             "because function %qD has not external linkage",
             expr, type, fn_no_ptr);
             expr, type, fn_no_ptr);
      return NULL_TREE;
      return NULL_TREE;
    }
    }
 
 
  return fn;
  return fn;
}
}
 
 
/* Subroutine of convert_nontype_argument.
/* Subroutine of convert_nontype_argument.
   Check if EXPR of type TYPE is a valid pointer-to-member constant.
   Check if EXPR of type TYPE is a valid pointer-to-member constant.
   Emit an error otherwise.  */
   Emit an error otherwise.  */
 
 
static bool
static bool
check_valid_ptrmem_cst_expr (tree type, tree expr)
check_valid_ptrmem_cst_expr (tree type, tree expr)
{
{
  STRIP_NOPS (expr);
  STRIP_NOPS (expr);
  if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
  if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
    return true;
    return true;
  error ("%qE is not a valid template argument for type %qT",
  error ("%qE is not a valid template argument for type %qT",
         expr, type);
         expr, type);
  error ("it must be a pointer-to-member of the form `&X::Y'");
  error ("it must be a pointer-to-member of the form `&X::Y'");
  return false;
  return false;
}
}
 
 
/* Returns TRUE iff the address of OP is value-dependent.
/* Returns TRUE iff the address of OP is value-dependent.
 
 
   14.6.2.4 [temp.dep.temp]:
   14.6.2.4 [temp.dep.temp]:
   A non-integral non-type template-argument is dependent if its type is
   A non-integral non-type template-argument is dependent if its type is
   dependent or it has either of the following forms
   dependent or it has either of the following forms
     qualified-id
     qualified-id
     & qualified-id
     & qualified-id
   and contains a nested-name-specifier which specifies a class-name that
   and contains a nested-name-specifier which specifies a class-name that
   names a dependent type.
   names a dependent type.
 
 
   We generalize this to just say that the address of a member of a
   We generalize this to just say that the address of a member of a
   dependent class is value-dependent; the above doesn't cover the
   dependent class is value-dependent; the above doesn't cover the
   address of a static data member named with an unqualified-id.  */
   address of a static data member named with an unqualified-id.  */
 
 
static bool
static bool
has_value_dependent_address (tree op)
has_value_dependent_address (tree op)
{
{
  /* We could use get_inner_reference here, but there's no need;
  /* We could use get_inner_reference here, but there's no need;
     this is only relevant for template non-type arguments, which
     this is only relevant for template non-type arguments, which
     can only be expressed as &id-expression.  */
     can only be expressed as &id-expression.  */
  if (DECL_P (op))
  if (DECL_P (op))
    {
    {
      tree ctx = CP_DECL_CONTEXT (op);
      tree ctx = CP_DECL_CONTEXT (op);
      if (TYPE_P (ctx) && dependent_type_p (ctx))
      if (TYPE_P (ctx) && dependent_type_p (ctx))
        return true;
        return true;
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* Attempt to convert the non-type template parameter EXPR to the
/* Attempt to convert the non-type template parameter EXPR to the
   indicated TYPE.  If the conversion is successful, return the
   indicated TYPE.  If the conversion is successful, return the
   converted value.  If the conversion is unsuccessful, return
   converted value.  If the conversion is unsuccessful, return
   NULL_TREE if we issued an error message, or error_mark_node if we
   NULL_TREE if we issued an error message, or error_mark_node if we
   did not.  We issue error messages for out-and-out bad template
   did not.  We issue error messages for out-and-out bad template
   parameters, but not simply because the conversion failed, since we
   parameters, but not simply because the conversion failed, since we
   might be just trying to do argument deduction.  Both TYPE and EXPR
   might be just trying to do argument deduction.  Both TYPE and EXPR
   must be non-dependent.
   must be non-dependent.
 
 
   The conversion follows the special rules described in
   The conversion follows the special rules described in
   [temp.arg.nontype], and it is much more strict than an implicit
   [temp.arg.nontype], and it is much more strict than an implicit
   conversion.
   conversion.
 
 
   This function is called twice for each template argument (see
   This function is called twice for each template argument (see
   lookup_template_class for a more accurate description of this
   lookup_template_class for a more accurate description of this
   problem). This means that we need to handle expressions which
   problem). This means that we need to handle expressions which
   are not valid in a C++ source, but can be created from the
   are not valid in a C++ source, but can be created from the
   first call (for instance, casts to perform conversions). These
   first call (for instance, casts to perform conversions). These
   hacks can go away after we fix the double coercion problem.  */
   hacks can go away after we fix the double coercion problem.  */
 
 
static tree
static tree
convert_nontype_argument (tree type, tree expr)
convert_nontype_argument (tree type, tree expr)
{
{
  tree expr_type;
  tree expr_type;
 
 
  /* Detect immediately string literals as invalid non-type argument.
  /* Detect immediately string literals as invalid non-type argument.
     This special-case is not needed for correctness (we would easily
     This special-case is not needed for correctness (we would easily
     catch this later), but only to provide better diagnostic for this
     catch this later), but only to provide better diagnostic for this
     common user mistake. As suggested by DR 100, we do not mention
     common user mistake. As suggested by DR 100, we do not mention
     linkage issues in the diagnostic as this is not the point.  */
     linkage issues in the diagnostic as this is not the point.  */
  if (TREE_CODE (expr) == STRING_CST)
  if (TREE_CODE (expr) == STRING_CST)
    {
    {
      error ("%qE is not a valid template argument for type %qT "
      error ("%qE is not a valid template argument for type %qT "
             "because string literals can never be used in this context",
             "because string literals can never be used in this context",
             expr, type);
             expr, type);
      return NULL_TREE;
      return NULL_TREE;
    }
    }
 
 
  /* Add the ADDR_EXPR now for the benefit of
  /* Add the ADDR_EXPR now for the benefit of
     value_dependent_expression_p.  */
     value_dependent_expression_p.  */
  if (TYPE_PTROBV_P (type))
  if (TYPE_PTROBV_P (type))
    expr = decay_conversion (expr);
    expr = decay_conversion (expr);
 
 
  /* If we are in a template, EXPR may be non-dependent, but still
  /* If we are in a template, EXPR may be non-dependent, but still
     have a syntactic, rather than semantic, form.  For example, EXPR
     have a syntactic, rather than semantic, form.  For example, EXPR
     might be a SCOPE_REF, rather than the VAR_DECL to which the
     might be a SCOPE_REF, rather than the VAR_DECL to which the
     SCOPE_REF refers.  Preserving the qualifying scope is necessary
     SCOPE_REF refers.  Preserving the qualifying scope is necessary
     so that access checking can be performed when the template is
     so that access checking can be performed when the template is
     instantiated -- but here we need the resolved form so that we can
     instantiated -- but here we need the resolved form so that we can
     convert the argument.  */
     convert the argument.  */
  if (TYPE_REF_OBJ_P (type)
  if (TYPE_REF_OBJ_P (type)
      && has_value_dependent_address (expr))
      && has_value_dependent_address (expr))
    /* If we want the address and it's value-dependent, don't fold.  */;
    /* If we want the address and it's value-dependent, don't fold.  */;
  else
  else
    expr = fold_non_dependent_expr (expr);
    expr = fold_non_dependent_expr (expr);
  if (error_operand_p (expr))
  if (error_operand_p (expr))
    return error_mark_node;
    return error_mark_node;
  expr_type = TREE_TYPE (expr);
  expr_type = TREE_TYPE (expr);
 
 
  /* HACK: Due to double coercion, we can get a
  /* HACK: Due to double coercion, we can get a
     NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
     NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
     which is the tree that we built on the first call (see
     which is the tree that we built on the first call (see
     below when coercing to reference to object or to reference to
     below when coercing to reference to object or to reference to
     function). We just strip everything and get to the arg.
     function). We just strip everything and get to the arg.
     See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
     See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
     for examples.  */
     for examples.  */
  if (TREE_CODE (expr) == NOP_EXPR)
  if (TREE_CODE (expr) == NOP_EXPR)
    {
    {
      if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
      if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
        {
        {
          /* ??? Maybe we could use convert_from_reference here, but we
          /* ??? Maybe we could use convert_from_reference here, but we
             would need to relax its constraints because the NOP_EXPR
             would need to relax its constraints because the NOP_EXPR
             could actually change the type to something more cv-qualified,
             could actually change the type to something more cv-qualified,
             and this is not folded by convert_from_reference.  */
             and this is not folded by convert_from_reference.  */
          tree addr = TREE_OPERAND (expr, 0);
          tree addr = TREE_OPERAND (expr, 0);
          gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
          gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
          gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
          gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
          gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
          gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
          gcc_assert (same_type_ignoring_top_level_qualifiers_p
          gcc_assert (same_type_ignoring_top_level_qualifiers_p
                      (TREE_TYPE (expr_type),
                      (TREE_TYPE (expr_type),
                       TREE_TYPE (TREE_TYPE (addr))));
                       TREE_TYPE (TREE_TYPE (addr))));
 
 
          expr = TREE_OPERAND (addr, 0);
          expr = TREE_OPERAND (addr, 0);
          expr_type = TREE_TYPE (expr);
          expr_type = TREE_TYPE (expr);
        }
        }
 
 
      /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
      /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
         parameter is a pointer to object, through decay and
         parameter is a pointer to object, through decay and
         qualification conversion. Let's strip everything.  */
         qualification conversion. Let's strip everything.  */
      else if (TYPE_PTROBV_P (type))
      else if (TYPE_PTROBV_P (type))
        {
        {
          STRIP_NOPS (expr);
          STRIP_NOPS (expr);
          gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
          gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
          gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
          gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
          /* Skip the ADDR_EXPR only if it is part of the decay for
          /* Skip the ADDR_EXPR only if it is part of the decay for
             an array. Otherwise, it is part of the original argument
             an array. Otherwise, it is part of the original argument
             in the source code.  */
             in the source code.  */
          if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
          if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
            expr = TREE_OPERAND (expr, 0);
            expr = TREE_OPERAND (expr, 0);
          expr_type = TREE_TYPE (expr);
          expr_type = TREE_TYPE (expr);
        }
        }
    }
    }
 
 
  /* [temp.arg.nontype]/5, bullet 1
  /* [temp.arg.nontype]/5, bullet 1
 
 
     For a non-type template-parameter of integral or enumeration type,
     For a non-type template-parameter of integral or enumeration type,
     integral promotions (_conv.prom_) and integral conversions
     integral promotions (_conv.prom_) and integral conversions
     (_conv.integral_) are applied.  */
     (_conv.integral_) are applied.  */
  if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
    {
    {
      if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
      if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
        return error_mark_node;
        return error_mark_node;
 
 
      expr = fold_decl_constant_value (expr);
      expr = fold_decl_constant_value (expr);
      /* Notice that there are constant expressions like '4 % 0' which
      /* Notice that there are constant expressions like '4 % 0' which
         do not fold into integer constants.  */
         do not fold into integer constants.  */
      if (TREE_CODE (expr) != INTEGER_CST)
      if (TREE_CODE (expr) != INTEGER_CST)
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because it is a non-constant expression", expr, type);
                 "because it is a non-constant expression", expr, type);
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      /* At this point, an implicit conversion does what we want,
      /* At this point, an implicit conversion does what we want,
         because we already know that the expression is of integral
         because we already know that the expression is of integral
         type.  */
         type.  */
      expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
      expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
      if (expr == error_mark_node)
      if (expr == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* Conversion was allowed: fold it to a bare integer constant.  */
      /* Conversion was allowed: fold it to a bare integer constant.  */
      expr = fold (expr);
      expr = fold (expr);
    }
    }
  /* [temp.arg.nontype]/5, bullet 2
  /* [temp.arg.nontype]/5, bullet 2
 
 
     For a non-type template-parameter of type pointer to object,
     For a non-type template-parameter of type pointer to object,
     qualification conversions (_conv.qual_) and the array-to-pointer
     qualification conversions (_conv.qual_) and the array-to-pointer
     conversion (_conv.array_) are applied.  */
     conversion (_conv.array_) are applied.  */
  else if (TYPE_PTROBV_P (type))
  else if (TYPE_PTROBV_P (type))
    {
    {
      /* [temp.arg.nontype]/1  (TC1 version, DR 49):
      /* [temp.arg.nontype]/1  (TC1 version, DR 49):
 
 
         A template-argument for a non-type, non-template template-parameter
         A template-argument for a non-type, non-template template-parameter
         shall be one of: [...]
         shall be one of: [...]
 
 
         -- the name of a non-type template-parameter;
         -- the name of a non-type template-parameter;
         -- the address of an object or function with external linkage, [...]
         -- the address of an object or function with external linkage, [...]
            expressed as "& id-expression" where the & is optional if the name
            expressed as "& id-expression" where the & is optional if the name
            refers to a function or array, or if the corresponding
            refers to a function or array, or if the corresponding
            template-parameter is a reference.
            template-parameter is a reference.
 
 
        Here, we do not care about functions, as they are invalid anyway
        Here, we do not care about functions, as they are invalid anyway
        for a parameter of type pointer-to-object.  */
        for a parameter of type pointer-to-object.  */
 
 
      if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
      if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
        /* Non-type template parameters are OK.  */
        /* Non-type template parameters are OK.  */
        ;
        ;
      else if (TREE_CODE (expr) != ADDR_EXPR
      else if (TREE_CODE (expr) != ADDR_EXPR
               && TREE_CODE (expr_type) != ARRAY_TYPE)
               && TREE_CODE (expr_type) != ARRAY_TYPE)
        {
        {
          if (TREE_CODE (expr) == VAR_DECL)
          if (TREE_CODE (expr) == VAR_DECL)
            {
            {
              error ("%qD is not a valid template argument "
              error ("%qD is not a valid template argument "
                     "because %qD is a variable, not the address of "
                     "because %qD is a variable, not the address of "
                     "a variable",
                     "a variable",
                     expr, expr);
                     expr, expr);
              return NULL_TREE;
              return NULL_TREE;
            }
            }
          /* Other values, like integer constants, might be valid
          /* Other values, like integer constants, might be valid
             non-type arguments of some other type.  */
             non-type arguments of some other type.  */
          return error_mark_node;
          return error_mark_node;
        }
        }
      else
      else
        {
        {
          tree decl;
          tree decl;
 
 
          decl = ((TREE_CODE (expr) == ADDR_EXPR)
          decl = ((TREE_CODE (expr) == ADDR_EXPR)
                  ? TREE_OPERAND (expr, 0) : expr);
                  ? TREE_OPERAND (expr, 0) : expr);
          if (TREE_CODE (decl) != VAR_DECL)
          if (TREE_CODE (decl) != VAR_DECL)
            {
            {
              error ("%qE is not a valid template argument of type %qT "
              error ("%qE is not a valid template argument of type %qT "
                     "because %qE is not a variable",
                     "because %qE is not a variable",
                     expr, type, decl);
                     expr, type, decl);
              return NULL_TREE;
              return NULL_TREE;
            }
            }
          else if (!DECL_EXTERNAL_LINKAGE_P (decl))
          else if (!DECL_EXTERNAL_LINKAGE_P (decl))
            {
            {
              error ("%qE is not a valid template argument of type %qT "
              error ("%qE is not a valid template argument of type %qT "
                     "because %qD does not have external linkage",
                     "because %qD does not have external linkage",
                     expr, type, decl);
                     expr, type, decl);
              return NULL_TREE;
              return NULL_TREE;
            }
            }
        }
        }
 
 
      expr = decay_conversion (expr);
      expr = decay_conversion (expr);
      if (expr == error_mark_node)
      if (expr == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      expr = perform_qualification_conversions (type, expr);
      expr = perform_qualification_conversions (type, expr);
      if (expr == error_mark_node)
      if (expr == error_mark_node)
        return error_mark_node;
        return error_mark_node;
    }
    }
  /* [temp.arg.nontype]/5, bullet 3
  /* [temp.arg.nontype]/5, bullet 3
 
 
     For a non-type template-parameter of type reference to object, no
     For a non-type template-parameter of type reference to object, no
     conversions apply. The type referred to by the reference may be more
     conversions apply. The type referred to by the reference may be more
     cv-qualified than the (otherwise identical) type of the
     cv-qualified than the (otherwise identical) type of the
     template-argument. The template-parameter is bound directly to the
     template-argument. The template-parameter is bound directly to the
     template-argument, which must be an lvalue.  */
     template-argument, which must be an lvalue.  */
  else if (TYPE_REF_OBJ_P (type))
  else if (TYPE_REF_OBJ_P (type))
    {
    {
      if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
      if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
                                                      expr_type))
                                                      expr_type))
        return error_mark_node;
        return error_mark_node;
 
 
      if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
      if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because of conflicts in cv-qualification", expr, type);
                 "because of conflicts in cv-qualification", expr, type);
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      if (!real_lvalue_p (expr))
      if (!real_lvalue_p (expr))
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because it is not an lvalue", expr, type);
                 "because it is not an lvalue", expr, type);
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      /* [temp.arg.nontype]/1
      /* [temp.arg.nontype]/1
 
 
         A template-argument for a non-type, non-template template-parameter
         A template-argument for a non-type, non-template template-parameter
         shall be one of: [...]
         shall be one of: [...]
 
 
         -- the address of an object or function with external linkage.  */
         -- the address of an object or function with external linkage.  */
      if (TREE_CODE (expr) == INDIRECT_REF
      if (TREE_CODE (expr) == INDIRECT_REF
          && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
          && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
        {
        {
          expr = TREE_OPERAND (expr, 0);
          expr = TREE_OPERAND (expr, 0);
          if (DECL_P (expr))
          if (DECL_P (expr))
            {
            {
              error ("%q#D is not a valid template argument for type %qT "
              error ("%q#D is not a valid template argument for type %qT "
                     "because a reference variable does not have a constant "
                     "because a reference variable does not have a constant "
                     "address", expr, type);
                     "address", expr, type);
              return NULL_TREE;
              return NULL_TREE;
            }
            }
        }
        }
 
 
      if (!DECL_P (expr))
      if (!DECL_P (expr))
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because it is not an object with external linkage",
                 "because it is not an object with external linkage",
                 expr, type);
                 expr, type);
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      if (!DECL_EXTERNAL_LINKAGE_P (expr))
      if (!DECL_EXTERNAL_LINKAGE_P (expr))
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because object %qD has not external linkage",
                 "because object %qD has not external linkage",
                 expr, type, expr);
                 expr, type, expr);
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      expr = build_nop (type, build_address (expr));
      expr = build_nop (type, build_address (expr));
    }
    }
  /* [temp.arg.nontype]/5, bullet 4
  /* [temp.arg.nontype]/5, bullet 4
 
 
     For a non-type template-parameter of type pointer to function, only
     For a non-type template-parameter of type pointer to function, only
     the function-to-pointer conversion (_conv.func_) is applied. If the
     the function-to-pointer conversion (_conv.func_) is applied. If the
     template-argument represents a set of overloaded functions (or a
     template-argument represents a set of overloaded functions (or a
     pointer to such), the matching function is selected from the set
     pointer to such), the matching function is selected from the set
     (_over.over_).  */
     (_over.over_).  */
  else if (TYPE_PTRFN_P (type))
  else if (TYPE_PTRFN_P (type))
    {
    {
      /* If the argument is a template-id, we might not have enough
      /* If the argument is a template-id, we might not have enough
         context information to decay the pointer.  */
         context information to decay the pointer.  */
      if (!type_unknown_p (expr_type))
      if (!type_unknown_p (expr_type))
        {
        {
          expr = decay_conversion (expr);
          expr = decay_conversion (expr);
          if (expr == error_mark_node)
          if (expr == error_mark_node)
            return error_mark_node;
            return error_mark_node;
        }
        }
 
 
      expr = convert_nontype_argument_function (type, expr);
      expr = convert_nontype_argument_function (type, expr);
      if (!expr || expr == error_mark_node)
      if (!expr || expr == error_mark_node)
        return expr;
        return expr;
 
 
      if (TREE_CODE (expr) != ADDR_EXPR)
      if (TREE_CODE (expr) != ADDR_EXPR)
        {
        {
          error ("%qE is not a valid template argument for type %qT", expr, type);
          error ("%qE is not a valid template argument for type %qT", expr, type);
          error ("it must be the address of a function with external linkage");
          error ("it must be the address of a function with external linkage");
          return NULL_TREE;
          return NULL_TREE;
        }
        }
    }
    }
  /* [temp.arg.nontype]/5, bullet 5
  /* [temp.arg.nontype]/5, bullet 5
 
 
     For a non-type template-parameter of type reference to function, no
     For a non-type template-parameter of type reference to function, no
     conversions apply. If the template-argument represents a set of
     conversions apply. If the template-argument represents a set of
     overloaded functions, the matching function is selected from the set
     overloaded functions, the matching function is selected from the set
     (_over.over_).  */
     (_over.over_).  */
  else if (TYPE_REFFN_P (type))
  else if (TYPE_REFFN_P (type))
    {
    {
      if (TREE_CODE (expr) == ADDR_EXPR)
      if (TREE_CODE (expr) == ADDR_EXPR)
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because it is a pointer", expr, type);
                 "because it is a pointer", expr, type);
          inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
          inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
          return NULL_TREE;
          return NULL_TREE;
        }
        }
 
 
      expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
      expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
      if (!expr || expr == error_mark_node)
      if (!expr || expr == error_mark_node)
        return expr;
        return expr;
 
 
      expr = build_nop (type, build_address (expr));
      expr = build_nop (type, build_address (expr));
    }
    }
  /* [temp.arg.nontype]/5, bullet 6
  /* [temp.arg.nontype]/5, bullet 6
 
 
     For a non-type template-parameter of type pointer to member function,
     For a non-type template-parameter of type pointer to member function,
     no conversions apply. If the template-argument represents a set of
     no conversions apply. If the template-argument represents a set of
     overloaded member functions, the matching member function is selected
     overloaded member functions, the matching member function is selected
     from the set (_over.over_).  */
     from the set (_over.over_).  */
  else if (TYPE_PTRMEMFUNC_P (type))
  else if (TYPE_PTRMEMFUNC_P (type))
    {
    {
      expr = instantiate_type (type, expr, tf_none);
      expr = instantiate_type (type, expr, tf_none);
      if (expr == error_mark_node)
      if (expr == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* [temp.arg.nontype] bullet 1 says the pointer to member
      /* [temp.arg.nontype] bullet 1 says the pointer to member
         expression must be a pointer-to-member constant.  */
         expression must be a pointer-to-member constant.  */
      if (!check_valid_ptrmem_cst_expr (type, expr))
      if (!check_valid_ptrmem_cst_expr (type, expr))
        return error_mark_node;
        return error_mark_node;
 
 
      /* There is no way to disable standard conversions in
      /* There is no way to disable standard conversions in
         resolve_address_of_overloaded_function (called by
         resolve_address_of_overloaded_function (called by
         instantiate_type). It is possible that the call succeeded by
         instantiate_type). It is possible that the call succeeded by
         converting &B::I to &D::I (where B is a base of D), so we need
         converting &B::I to &D::I (where B is a base of D), so we need
         to reject this conversion here.
         to reject this conversion here.
 
 
         Actually, even if there was a way to disable standard conversions,
         Actually, even if there was a way to disable standard conversions,
         it would still be better to reject them here so that we can
         it would still be better to reject them here so that we can
         provide a superior diagnostic.  */
         provide a superior diagnostic.  */
      if (!same_type_p (TREE_TYPE (expr), type))
      if (!same_type_p (TREE_TYPE (expr), type))
        {
        {
          error ("%qE is not a valid template argument for type %qT "
          error ("%qE is not a valid template argument for type %qT "
                 "because it is of type %qT", expr, type,
                 "because it is of type %qT", expr, type,
                 TREE_TYPE (expr));
                 TREE_TYPE (expr));
          /* If we are just one standard conversion off, explain.  */
          /* If we are just one standard conversion off, explain.  */
          if (can_convert (type, TREE_TYPE (expr)))
          if (can_convert (type, TREE_TYPE (expr)))
            inform (input_location,
            inform (input_location,
                    "standard conversions are not allowed in this context");
                    "standard conversions are not allowed in this context");
          return NULL_TREE;
          return NULL_TREE;
        }
        }
    }
    }
  /* [temp.arg.nontype]/5, bullet 7
  /* [temp.arg.nontype]/5, bullet 7
 
 
     For a non-type template-parameter of type pointer to data member,
     For a non-type template-parameter of type pointer to data member,
     qualification conversions (_conv.qual_) are applied.  */
     qualification conversions (_conv.qual_) are applied.  */
  else if (TYPE_PTRMEM_P (type))
  else if (TYPE_PTRMEM_P (type))
    {
    {
      /* [temp.arg.nontype] bullet 1 says the pointer to member
      /* [temp.arg.nontype] bullet 1 says the pointer to member
         expression must be a pointer-to-member constant.  */
         expression must be a pointer-to-member constant.  */
      if (!check_valid_ptrmem_cst_expr (type, expr))
      if (!check_valid_ptrmem_cst_expr (type, expr))
        return error_mark_node;
        return error_mark_node;
 
 
      expr = perform_qualification_conversions (type, expr);
      expr = perform_qualification_conversions (type, expr);
      if (expr == error_mark_node)
      if (expr == error_mark_node)
        return expr;
        return expr;
    }
    }
  /* A template non-type parameter must be one of the above.  */
  /* A template non-type parameter must be one of the above.  */
  else
  else
    gcc_unreachable ();
    gcc_unreachable ();
 
 
  /* Sanity check: did we actually convert the argument to the
  /* Sanity check: did we actually convert the argument to the
     right type?  */
     right type?  */
  gcc_assert (same_type_p (type, TREE_TYPE (expr)));
  gcc_assert (same_type_p (type, TREE_TYPE (expr)));
  return expr;
  return expr;
}
}
 
 
/* Subroutine of coerce_template_template_parms, which returns 1 if
/* Subroutine of coerce_template_template_parms, which returns 1 if
   PARM_PARM and ARG_PARM match using the rule for the template
   PARM_PARM and ARG_PARM match using the rule for the template
   parameters of template template parameters. Both PARM and ARG are
   parameters of template template parameters. Both PARM and ARG are
   template parameters; the rest of the arguments are the same as for
   template parameters; the rest of the arguments are the same as for
   coerce_template_template_parms.
   coerce_template_template_parms.
 */
 */
static int
static int
coerce_template_template_parm (tree parm,
coerce_template_template_parm (tree parm,
                              tree arg,
                              tree arg,
                              tsubst_flags_t complain,
                              tsubst_flags_t complain,
                              tree in_decl,
                              tree in_decl,
                              tree outer_args)
                              tree outer_args)
{
{
  if (arg == NULL_TREE || arg == error_mark_node
  if (arg == NULL_TREE || arg == error_mark_node
      || parm == NULL_TREE || parm == error_mark_node)
      || parm == NULL_TREE || parm == error_mark_node)
    return 0;
    return 0;
 
 
  if (TREE_CODE (arg) != TREE_CODE (parm))
  if (TREE_CODE (arg) != TREE_CODE (parm))
    return 0;
    return 0;
 
 
  switch (TREE_CODE (parm))
  switch (TREE_CODE (parm))
    {
    {
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      /* We encounter instantiations of templates like
      /* We encounter instantiations of templates like
         template <template <template <class> class> class TT>
         template <template <template <class> class> class TT>
         class C;  */
         class C;  */
      {
      {
        tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
        tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
        tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
        tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
 
 
        if (!coerce_template_template_parms
        if (!coerce_template_template_parms
            (parmparm, argparm, complain, in_decl, outer_args))
            (parmparm, argparm, complain, in_decl, outer_args))
          return 0;
          return 0;
      }
      }
      /* Fall through.  */
      /* Fall through.  */
 
 
    case TYPE_DECL:
    case TYPE_DECL:
      if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
      if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
          && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
          && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
        /* Argument is a parameter pack but parameter is not.  */
        /* Argument is a parameter pack but parameter is not.  */
        return 0;
        return 0;
      break;
      break;
 
 
    case PARM_DECL:
    case PARM_DECL:
      /* The tsubst call is used to handle cases such as
      /* The tsubst call is used to handle cases such as
 
 
           template <int> class C {};
           template <int> class C {};
           template <class T, template <T> class TT> class D {};
           template <class T, template <T> class TT> class D {};
           D<int, C> d;
           D<int, C> d;
 
 
         i.e. the parameter list of TT depends on earlier parameters.  */
         i.e. the parameter list of TT depends on earlier parameters.  */
      if (!uses_template_parms (TREE_TYPE (arg))
      if (!uses_template_parms (TREE_TYPE (arg))
          && !same_type_p
          && !same_type_p
                (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
                (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
                 TREE_TYPE (arg)))
                 TREE_TYPE (arg)))
        return 0;
        return 0;
 
 
      if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
      if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
          && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
          && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
        /* Argument is a parameter pack but parameter is not.  */
        /* Argument is a parameter pack but parameter is not.  */
        return 0;
        return 0;
 
 
      break;
      break;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
 
 
  return 1;
  return 1;
}
}
 
 
 
 
/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
   template template parameters.  Both PARM_PARMS and ARG_PARMS are
   template template parameters.  Both PARM_PARMS and ARG_PARMS are
   vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
   vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
   or PARM_DECL.
   or PARM_DECL.
 
 
   Consider the example:
   Consider the example:
     template <class T> class A;
     template <class T> class A;
     template<template <class U> class TT> class B;
     template<template <class U> class TT> class B;
 
 
   For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
   For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
   the parameters to A, and OUTER_ARGS contains A.  */
   the parameters to A, and OUTER_ARGS contains A.  */
 
 
static int
static int
coerce_template_template_parms (tree parm_parms,
coerce_template_template_parms (tree parm_parms,
                                tree arg_parms,
                                tree arg_parms,
                                tsubst_flags_t complain,
                                tsubst_flags_t complain,
                                tree in_decl,
                                tree in_decl,
                                tree outer_args)
                                tree outer_args)
{
{
  int nparms, nargs, i;
  int nparms, nargs, i;
  tree parm, arg;
  tree parm, arg;
  int variadic_p = 0;
  int variadic_p = 0;
 
 
  gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
  gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
  gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
  gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
 
 
  nparms = TREE_VEC_LENGTH (parm_parms);
  nparms = TREE_VEC_LENGTH (parm_parms);
  nargs = TREE_VEC_LENGTH (arg_parms);
  nargs = TREE_VEC_LENGTH (arg_parms);
 
 
  /* Determine whether we have a parameter pack at the end of the
  /* Determine whether we have a parameter pack at the end of the
     template template parameter's template parameter list.  */
     template template parameter's template parameter list.  */
  if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
  if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    {
    {
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
 
 
      if (parm == error_mark_node)
      if (parm == error_mark_node)
        return 0;
        return 0;
 
 
      switch (TREE_CODE (parm))
      switch (TREE_CODE (parm))
        {
        {
        case TEMPLATE_DECL:
        case TEMPLATE_DECL:
        case TYPE_DECL:
        case TYPE_DECL:
          if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
          if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
            variadic_p = 1;
            variadic_p = 1;
          break;
          break;
 
 
        case PARM_DECL:
        case PARM_DECL:
          if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
          if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
            variadic_p = 1;
            variadic_p = 1;
          break;
          break;
 
 
        default:
        default:
          gcc_unreachable ();
          gcc_unreachable ();
        }
        }
    }
    }
 
 
  if (nargs != nparms
  if (nargs != nparms
      && !(variadic_p && nargs >= nparms - 1))
      && !(variadic_p && nargs >= nparms - 1))
    return 0;
    return 0;
 
 
  /* Check all of the template parameters except the parameter pack at
  /* Check all of the template parameters except the parameter pack at
     the end (if any).  */
     the end (if any).  */
  for (i = 0; i < nparms - variadic_p; ++i)
  for (i = 0; i < nparms - variadic_p; ++i)
    {
    {
      if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
      if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
          || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
          || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
        continue;
        continue;
 
 
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
      arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
      arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
 
 
      if (!coerce_template_template_parm (parm, arg, complain, in_decl,
      if (!coerce_template_template_parm (parm, arg, complain, in_decl,
                                          outer_args))
                                          outer_args))
        return 0;
        return 0;
 
 
    }
    }
 
 
  if (variadic_p)
  if (variadic_p)
    {
    {
      /* Check each of the template parameters in the template
      /* Check each of the template parameters in the template
         argument against the template parameter pack at the end of
         argument against the template parameter pack at the end of
         the template template parameter.  */
         the template template parameter.  */
      if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
      if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
        return 0;
        return 0;
 
 
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
 
 
      for (; i < nargs; ++i)
      for (; i < nargs; ++i)
        {
        {
          if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
          if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
            continue;
            continue;
 
 
          arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
          arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
 
 
          if (!coerce_template_template_parm (parm, arg, complain, in_decl,
          if (!coerce_template_template_parm (parm, arg, complain, in_decl,
                                              outer_args))
                                              outer_args))
            return 0;
            return 0;
        }
        }
    }
    }
 
 
  return 1;
  return 1;
}
}
 
 
/* Verifies that the deduced template arguments (in TARGS) for the
/* Verifies that the deduced template arguments (in TARGS) for the
   template template parameters (in TPARMS) represent valid bindings,
   template template parameters (in TPARMS) represent valid bindings,
   by comparing the template parameter list of each template argument
   by comparing the template parameter list of each template argument
   to the template parameter list of its corresponding template
   to the template parameter list of its corresponding template
   template parameter, in accordance with DR150. This
   template parameter, in accordance with DR150. This
   routine can only be called after all template arguments have been
   routine can only be called after all template arguments have been
   deduced. It will return TRUE if all of the template template
   deduced. It will return TRUE if all of the template template
   parameter bindings are okay, FALSE otherwise.  */
   parameter bindings are okay, FALSE otherwise.  */
bool
bool
template_template_parm_bindings_ok_p (tree tparms, tree targs)
template_template_parm_bindings_ok_p (tree tparms, tree targs)
{
{
  int i, ntparms = TREE_VEC_LENGTH (tparms);
  int i, ntparms = TREE_VEC_LENGTH (tparms);
  bool ret = true;
  bool ret = true;
 
 
  /* We're dealing with template parms in this process.  */
  /* We're dealing with template parms in this process.  */
  ++processing_template_decl;
  ++processing_template_decl;
 
 
  targs = INNERMOST_TEMPLATE_ARGS (targs);
  targs = INNERMOST_TEMPLATE_ARGS (targs);
 
 
  for (i = 0; i < ntparms; ++i)
  for (i = 0; i < ntparms; ++i)
    {
    {
      tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
      tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
      tree targ = TREE_VEC_ELT (targs, i);
      tree targ = TREE_VEC_ELT (targs, i);
 
 
      if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
      if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
        {
        {
          tree packed_args = NULL_TREE;
          tree packed_args = NULL_TREE;
          int idx, len = 1;
          int idx, len = 1;
 
 
          if (ARGUMENT_PACK_P (targ))
          if (ARGUMENT_PACK_P (targ))
            {
            {
              /* Look inside the argument pack.  */
              /* Look inside the argument pack.  */
              packed_args = ARGUMENT_PACK_ARGS (targ);
              packed_args = ARGUMENT_PACK_ARGS (targ);
              len = TREE_VEC_LENGTH (packed_args);
              len = TREE_VEC_LENGTH (packed_args);
            }
            }
 
 
          for (idx = 0; idx < len; ++idx)
          for (idx = 0; idx < len; ++idx)
            {
            {
              tree targ_parms = NULL_TREE;
              tree targ_parms = NULL_TREE;
 
 
              if (packed_args)
              if (packed_args)
                /* Extract the next argument from the argument
                /* Extract the next argument from the argument
                   pack.  */
                   pack.  */
                targ = TREE_VEC_ELT (packed_args, idx);
                targ = TREE_VEC_ELT (packed_args, idx);
 
 
              if (PACK_EXPANSION_P (targ))
              if (PACK_EXPANSION_P (targ))
                /* Look at the pattern of the pack expansion.  */
                /* Look at the pattern of the pack expansion.  */
                targ = PACK_EXPANSION_PATTERN (targ);
                targ = PACK_EXPANSION_PATTERN (targ);
 
 
              /* Extract the template parameters from the template
              /* Extract the template parameters from the template
                 argument.  */
                 argument.  */
              if (TREE_CODE (targ) == TEMPLATE_DECL)
              if (TREE_CODE (targ) == TEMPLATE_DECL)
                targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
                targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
              else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
              else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
                targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
                targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
 
 
              /* Verify that we can coerce the template template
              /* Verify that we can coerce the template template
                 parameters from the template argument to the template
                 parameters from the template argument to the template
                 parameter.  This requires an exact match.  */
                 parameter.  This requires an exact match.  */
              if (targ_parms
              if (targ_parms
                  && !coerce_template_template_parms
                  && !coerce_template_template_parms
                       (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
                       (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
                        targ_parms,
                        targ_parms,
                        tf_none,
                        tf_none,
                        tparm,
                        tparm,
                        targs))
                        targs))
                {
                {
                  ret = false;
                  ret = false;
                  goto out;
                  goto out;
                }
                }
            }
            }
        }
        }
    }
    }
 
 
 out:
 out:
 
 
  --processing_template_decl;
  --processing_template_decl;
  return ret;
  return ret;
}
}
 
 
/* Convert the indicated template ARG as necessary to match the
/* Convert the indicated template ARG as necessary to match the
   indicated template PARM.  Returns the converted ARG, or
   indicated template PARM.  Returns the converted ARG, or
   error_mark_node if the conversion was unsuccessful.  Error and
   error_mark_node if the conversion was unsuccessful.  Error and
   warning messages are issued under control of COMPLAIN.  This
   warning messages are issued under control of COMPLAIN.  This
   conversion is for the Ith parameter in the parameter list.  ARGS is
   conversion is for the Ith parameter in the parameter list.  ARGS is
   the full set of template arguments deduced so far.  */
   the full set of template arguments deduced so far.  */
 
 
static tree
static tree
convert_template_argument (tree parm,
convert_template_argument (tree parm,
                           tree arg,
                           tree arg,
                           tree args,
                           tree args,
                           tsubst_flags_t complain,
                           tsubst_flags_t complain,
                           int i,
                           int i,
                           tree in_decl)
                           tree in_decl)
{
{
  tree orig_arg;
  tree orig_arg;
  tree val;
  tree val;
  int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
  int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
 
 
  if (TREE_CODE (arg) == TREE_LIST
  if (TREE_CODE (arg) == TREE_LIST
      && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
      && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    {
    {
      /* The template argument was the name of some
      /* The template argument was the name of some
         member function.  That's usually
         member function.  That's usually
         invalid, but static members are OK.  In any
         invalid, but static members are OK.  In any
         case, grab the underlying fields/functions
         case, grab the underlying fields/functions
         and issue an error later if required.  */
         and issue an error later if required.  */
      orig_arg = TREE_VALUE (arg);
      orig_arg = TREE_VALUE (arg);
      TREE_TYPE (arg) = unknown_type_node;
      TREE_TYPE (arg) = unknown_type_node;
    }
    }
 
 
  orig_arg = arg;
  orig_arg = arg;
 
 
  requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
  requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
  requires_type = (TREE_CODE (parm) == TYPE_DECL
  requires_type = (TREE_CODE (parm) == TYPE_DECL
                   || requires_tmpl_type);
                   || requires_tmpl_type);
 
 
  /* When determining whether an argument pack expansion is a template,
  /* When determining whether an argument pack expansion is a template,
     look at the pattern.  */
     look at the pattern.  */
  if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
  if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
    arg = PACK_EXPANSION_PATTERN (arg);
    arg = PACK_EXPANSION_PATTERN (arg);
 
 
  /* Deal with an injected-class-name used as a template template arg.  */
  /* Deal with an injected-class-name used as a template template arg.  */
  if (requires_tmpl_type && CLASS_TYPE_P (arg))
  if (requires_tmpl_type && CLASS_TYPE_P (arg))
    {
    {
      tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
      tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
      if (TREE_CODE (t) == TEMPLATE_DECL)
      if (TREE_CODE (t) == TEMPLATE_DECL)
        {
        {
          if (complain & tf_warning_or_error)
          if (complain & tf_warning_or_error)
            pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
            pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
                     " used as template template argument", TYPE_NAME (arg));
                     " used as template template argument", TYPE_NAME (arg));
          else if (flag_pedantic_errors)
          else if (flag_pedantic_errors)
            t = arg;
            t = arg;
 
 
          arg = t;
          arg = t;
        }
        }
    }
    }
 
 
  is_tmpl_type =
  is_tmpl_type =
    ((TREE_CODE (arg) == TEMPLATE_DECL
    ((TREE_CODE (arg) == TEMPLATE_DECL
      && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
      && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
     || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
     || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
     || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
     || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
 
 
  if (is_tmpl_type
  if (is_tmpl_type
      && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
      && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
          || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
          || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    arg = TYPE_STUB_DECL (arg);
    arg = TYPE_STUB_DECL (arg);
 
 
  is_type = TYPE_P (arg) || is_tmpl_type;
  is_type = TYPE_P (arg) || is_tmpl_type;
 
 
  if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
  if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
      && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
      && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    {
    {
      permerror (input_location, "to refer to a type member of a template parameter, "
      permerror (input_location, "to refer to a type member of a template parameter, "
                 "use %<typename %E%>", orig_arg);
                 "use %<typename %E%>", orig_arg);
 
 
      orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
      orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
                                     TREE_OPERAND (arg, 1),
                                     TREE_OPERAND (arg, 1),
                                     typename_type,
                                     typename_type,
                                     complain & tf_error);
                                     complain & tf_error);
      arg = orig_arg;
      arg = orig_arg;
      is_type = 1;
      is_type = 1;
    }
    }
  if (is_type != requires_type)
  if (is_type != requires_type)
    {
    {
      if (in_decl)
      if (in_decl)
        {
        {
          if (complain & tf_error)
          if (complain & tf_error)
            {
            {
              error ("type/value mismatch at argument %d in template "
              error ("type/value mismatch at argument %d in template "
                     "parameter list for %qD",
                     "parameter list for %qD",
                     i + 1, in_decl);
                     i + 1, in_decl);
              if (is_type)
              if (is_type)
                error ("  expected a constant of type %qT, got %qT",
                error ("  expected a constant of type %qT, got %qT",
                       TREE_TYPE (parm),
                       TREE_TYPE (parm),
                       (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
                       (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
              else if (requires_tmpl_type)
              else if (requires_tmpl_type)
                error ("  expected a class template, got %qE", orig_arg);
                error ("  expected a class template, got %qE", orig_arg);
              else
              else
                error ("  expected a type, got %qE", orig_arg);
                error ("  expected a type, got %qE", orig_arg);
            }
            }
        }
        }
      return error_mark_node;
      return error_mark_node;
    }
    }
  if (is_tmpl_type ^ requires_tmpl_type)
  if (is_tmpl_type ^ requires_tmpl_type)
    {
    {
      if (in_decl && (complain & tf_error))
      if (in_decl && (complain & tf_error))
        {
        {
          error ("type/value mismatch at argument %d in template "
          error ("type/value mismatch at argument %d in template "
                 "parameter list for %qD",
                 "parameter list for %qD",
                 i + 1, in_decl);
                 i + 1, in_decl);
          if (is_tmpl_type)
          if (is_tmpl_type)
            error ("  expected a type, got %qT", DECL_NAME (arg));
            error ("  expected a type, got %qT", DECL_NAME (arg));
          else
          else
            error ("  expected a class template, got %qT", orig_arg);
            error ("  expected a class template, got %qT", orig_arg);
        }
        }
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  if (is_type)
  if (is_type)
    {
    {
      if (requires_tmpl_type)
      if (requires_tmpl_type)
        {
        {
          if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
          if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
            /* The number of argument required is not known yet.
            /* The number of argument required is not known yet.
               Just accept it for now.  */
               Just accept it for now.  */
            val = TREE_TYPE (arg);
            val = TREE_TYPE (arg);
          else
          else
            {
            {
              tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
              tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
              tree argparm;
              tree argparm;
 
 
              argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
              argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
 
 
              if (coerce_template_template_parms (parmparm, argparm,
              if (coerce_template_template_parms (parmparm, argparm,
                                                  complain, in_decl,
                                                  complain, in_decl,
                                                  args))
                                                  args))
                {
                {
                  val = arg;
                  val = arg;
 
 
                  /* TEMPLATE_TEMPLATE_PARM node is preferred over
                  /* TEMPLATE_TEMPLATE_PARM node is preferred over
                     TEMPLATE_DECL.  */
                     TEMPLATE_DECL.  */
                  if (val != error_mark_node)
                  if (val != error_mark_node)
                    {
                    {
                      if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
                      if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
                        val = TREE_TYPE (val);
                        val = TREE_TYPE (val);
                      if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
                      if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
                        val = make_pack_expansion (val);
                        val = make_pack_expansion (val);
                    }
                    }
                }
                }
              else
              else
                {
                {
                  if (in_decl && (complain & tf_error))
                  if (in_decl && (complain & tf_error))
                    {
                    {
                      error ("type/value mismatch at argument %d in "
                      error ("type/value mismatch at argument %d in "
                             "template parameter list for %qD",
                             "template parameter list for %qD",
                             i + 1, in_decl);
                             i + 1, in_decl);
                      error ("  expected a template of type %qD, got %qT",
                      error ("  expected a template of type %qD, got %qT",
                             parm, orig_arg);
                             parm, orig_arg);
                    }
                    }
 
 
                  val = error_mark_node;
                  val = error_mark_node;
                }
                }
            }
            }
        }
        }
      else
      else
        val = orig_arg;
        val = orig_arg;
      /* We only form one instance of each template specialization.
      /* We only form one instance of each template specialization.
         Therefore, if we use a non-canonical variant (i.e., a
         Therefore, if we use a non-canonical variant (i.e., a
         typedef), any future messages referring to the type will use
         typedef), any future messages referring to the type will use
         the typedef, which is confusing if those future uses do not
         the typedef, which is confusing if those future uses do not
         themselves also use the typedef.  */
         themselves also use the typedef.  */
      if (TYPE_P (val))
      if (TYPE_P (val))
        val = strip_typedefs (val);
        val = strip_typedefs (val);
    }
    }
  else
  else
    {
    {
      tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
      tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
 
 
      if (invalid_nontype_parm_type_p (t, complain))
      if (invalid_nontype_parm_type_p (t, complain))
        return error_mark_node;
        return error_mark_node;
 
 
      if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
      if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
        {
        {
          if (same_type_p (t, TREE_TYPE (orig_arg)))
          if (same_type_p (t, TREE_TYPE (orig_arg)))
            val = orig_arg;
            val = orig_arg;
          else
          else
            {
            {
              /* Not sure if this is reachable, but it doesn't hurt
              /* Not sure if this is reachable, but it doesn't hurt
                 to be robust.  */
                 to be robust.  */
              error ("type mismatch in nontype parameter pack");
              error ("type mismatch in nontype parameter pack");
              val = error_mark_node;
              val = error_mark_node;
            }
            }
        }
        }
      else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
      else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
        /* We used to call digest_init here.  However, digest_init
        /* We used to call digest_init here.  However, digest_init
           will report errors, which we don't want when complain
           will report errors, which we don't want when complain
           is zero.  More importantly, digest_init will try too
           is zero.  More importantly, digest_init will try too
           hard to convert things: for example, `0' should not be
           hard to convert things: for example, `0' should not be
           converted to pointer type at this point according to
           converted to pointer type at this point according to
           the standard.  Accepting this is not merely an
           the standard.  Accepting this is not merely an
           extension, since deciding whether or not these
           extension, since deciding whether or not these
           conversions can occur is part of determining which
           conversions can occur is part of determining which
           function template to call, or whether a given explicit
           function template to call, or whether a given explicit
           argument specification is valid.  */
           argument specification is valid.  */
        val = convert_nontype_argument (t, orig_arg);
        val = convert_nontype_argument (t, orig_arg);
      else
      else
        val = orig_arg;
        val = orig_arg;
 
 
      if (val == NULL_TREE)
      if (val == NULL_TREE)
        val = error_mark_node;
        val = error_mark_node;
      else if (val == error_mark_node && (complain & tf_error))
      else if (val == error_mark_node && (complain & tf_error))
        error ("could not convert template argument %qE to %qT",  orig_arg, t);
        error ("could not convert template argument %qE to %qT",  orig_arg, t);
 
 
      if (TREE_CODE (val) == SCOPE_REF)
      if (TREE_CODE (val) == SCOPE_REF)
        {
        {
          /* Strip typedefs from the SCOPE_REF.  */
          /* Strip typedefs from the SCOPE_REF.  */
          tree type = strip_typedefs (TREE_TYPE (val));
          tree type = strip_typedefs (TREE_TYPE (val));
          tree scope = strip_typedefs (TREE_OPERAND (val, 0));
          tree scope = strip_typedefs (TREE_OPERAND (val, 0));
          val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
          val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
                                      QUALIFIED_NAME_IS_TEMPLATE (val));
                                      QUALIFIED_NAME_IS_TEMPLATE (val));
        }
        }
    }
    }
 
 
  return val;
  return val;
}
}
 
 
/* Coerces the remaining template arguments in INNER_ARGS (from
/* Coerces the remaining template arguments in INNER_ARGS (from
   ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
   ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
   Returns the coerced argument pack. PARM_IDX is the position of this
   Returns the coerced argument pack. PARM_IDX is the position of this
   parameter in the template parameter list. ARGS is the original
   parameter in the template parameter list. ARGS is the original
   template argument list.  */
   template argument list.  */
static tree
static tree
coerce_template_parameter_pack (tree parms,
coerce_template_parameter_pack (tree parms,
                                int parm_idx,
                                int parm_idx,
                                tree args,
                                tree args,
                                tree inner_args,
                                tree inner_args,
                                int arg_idx,
                                int arg_idx,
                                tree new_args,
                                tree new_args,
                                int* lost,
                                int* lost,
                                tree in_decl,
                                tree in_decl,
                                tsubst_flags_t complain)
                                tsubst_flags_t complain)
{
{
  tree parm = TREE_VEC_ELT (parms, parm_idx);
  tree parm = TREE_VEC_ELT (parms, parm_idx);
  int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
  int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
  tree packed_args;
  tree packed_args;
  tree argument_pack;
  tree argument_pack;
  tree packed_types = NULL_TREE;
  tree packed_types = NULL_TREE;
 
 
  if (arg_idx > nargs)
  if (arg_idx > nargs)
    arg_idx = nargs;
    arg_idx = nargs;
 
 
  packed_args = make_tree_vec (nargs - arg_idx);
  packed_args = make_tree_vec (nargs - arg_idx);
 
 
  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
  if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
      && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
      && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
    {
    {
      /* When the template parameter is a non-type template
      /* When the template parameter is a non-type template
         parameter pack whose type uses parameter packs, we need
         parameter pack whose type uses parameter packs, we need
         to look at each of the template arguments
         to look at each of the template arguments
         separately. Build a vector of the types for these
         separately. Build a vector of the types for these
         non-type template parameters in PACKED_TYPES.  */
         non-type template parameters in PACKED_TYPES.  */
      tree expansion
      tree expansion
        = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
        = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
      packed_types = tsubst_pack_expansion (expansion, args,
      packed_types = tsubst_pack_expansion (expansion, args,
                                            complain, in_decl);
                                            complain, in_decl);
 
 
      if (packed_types == error_mark_node)
      if (packed_types == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* Check that we have the right number of arguments.  */
      /* Check that we have the right number of arguments.  */
      if (arg_idx < nargs
      if (arg_idx < nargs
          && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
          && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
          && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
          && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
        {
        {
          int needed_parms
          int needed_parms
            = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
            = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
          error ("wrong number of template arguments (%d, should be %d)",
          error ("wrong number of template arguments (%d, should be %d)",
                 nargs, needed_parms);
                 nargs, needed_parms);
          return error_mark_node;
          return error_mark_node;
        }
        }
 
 
      /* If we aren't able to check the actual arguments now
      /* If we aren't able to check the actual arguments now
         (because they haven't been expanded yet), we can at least
         (because they haven't been expanded yet), we can at least
         verify that all of the types used for the non-type
         verify that all of the types used for the non-type
         template parameter pack are, in fact, valid for non-type
         template parameter pack are, in fact, valid for non-type
         template parameters.  */
         template parameters.  */
      if (arg_idx < nargs
      if (arg_idx < nargs
          && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
          && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
        {
        {
          int j, len = TREE_VEC_LENGTH (packed_types);
          int j, len = TREE_VEC_LENGTH (packed_types);
          for (j = 0; j < len; ++j)
          for (j = 0; j < len; ++j)
            {
            {
              tree t = TREE_VEC_ELT (packed_types, j);
              tree t = TREE_VEC_ELT (packed_types, j);
              if (invalid_nontype_parm_type_p (t, complain))
              if (invalid_nontype_parm_type_p (t, complain))
                return error_mark_node;
                return error_mark_node;
            }
            }
        }
        }
    }
    }
 
 
  /* Convert the remaining arguments, which will be a part of the
  /* Convert the remaining arguments, which will be a part of the
     parameter pack "parm".  */
     parameter pack "parm".  */
  for (; arg_idx < nargs; ++arg_idx)
  for (; arg_idx < nargs; ++arg_idx)
    {
    {
      tree arg = TREE_VEC_ELT (inner_args, arg_idx);
      tree arg = TREE_VEC_ELT (inner_args, arg_idx);
      tree actual_parm = TREE_VALUE (parm);
      tree actual_parm = TREE_VALUE (parm);
 
 
      if (packed_types && !PACK_EXPANSION_P (arg))
      if (packed_types && !PACK_EXPANSION_P (arg))
        {
        {
          /* When we have a vector of types (corresponding to the
          /* When we have a vector of types (corresponding to the
             non-type template parameter pack that uses parameter
             non-type template parameter pack that uses parameter
             packs in its type, as mention above), and the
             packs in its type, as mention above), and the
             argument is not an expansion (which expands to a
             argument is not an expansion (which expands to a
             currently unknown number of arguments), clone the
             currently unknown number of arguments), clone the
             parm and give it the next type in PACKED_TYPES.  */
             parm and give it the next type in PACKED_TYPES.  */
          actual_parm = copy_node (actual_parm);
          actual_parm = copy_node (actual_parm);
          TREE_TYPE (actual_parm) =
          TREE_TYPE (actual_parm) =
            TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
            TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
        }
        }
 
 
      if (arg != error_mark_node)
      if (arg != error_mark_node)
        arg = convert_template_argument (actual_parm,
        arg = convert_template_argument (actual_parm,
                                         arg, new_args, complain, parm_idx,
                                         arg, new_args, complain, parm_idx,
                                         in_decl);
                                         in_decl);
      if (arg == error_mark_node)
      if (arg == error_mark_node)
        (*lost)++;
        (*lost)++;
      TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
      TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
    }
    }
 
 
  if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
  if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
      || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
      || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
  else
  else
    {
    {
      argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
      argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
      TREE_TYPE (argument_pack)
      TREE_TYPE (argument_pack)
        = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
        = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
      TREE_CONSTANT (argument_pack) = 1;
      TREE_CONSTANT (argument_pack) = 1;
    }
    }
 
 
  SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
  SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
  SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
                                       TREE_VEC_LENGTH (packed_args));
                                       TREE_VEC_LENGTH (packed_args));
#endif
#endif
  return argument_pack;
  return argument_pack;
}
}
 
 
/* Convert all template arguments to their appropriate types, and
/* Convert all template arguments to their appropriate types, and
   return a vector containing the innermost resulting template
   return a vector containing the innermost resulting template
   arguments.  If any error occurs, return error_mark_node. Error and
   arguments.  If any error occurs, return error_mark_node. Error and
   warning messages are issued under control of COMPLAIN.
   warning messages are issued under control of COMPLAIN.
 
 
   If REQUIRE_ALL_ARGS is false, argument deduction will be performed
   If REQUIRE_ALL_ARGS is false, argument deduction will be performed
   for arguments not specified in ARGS.  Otherwise, if
   for arguments not specified in ARGS.  Otherwise, if
   USE_DEFAULT_ARGS is true, default arguments will be used to fill in
   USE_DEFAULT_ARGS is true, default arguments will be used to fill in
   unspecified arguments.  If REQUIRE_ALL_ARGS is true, but
   unspecified arguments.  If REQUIRE_ALL_ARGS is true, but
   USE_DEFAULT_ARGS is false, then all arguments must be specified in
   USE_DEFAULT_ARGS is false, then all arguments must be specified in
   ARGS.  */
   ARGS.  */
 
 
static tree
static tree
coerce_template_parms (tree parms,
coerce_template_parms (tree parms,
                       tree args,
                       tree args,
                       tree in_decl,
                       tree in_decl,
                       tsubst_flags_t complain,
                       tsubst_flags_t complain,
                       bool require_all_args,
                       bool require_all_args,
                       bool use_default_args)
                       bool use_default_args)
{
{
  int nparms, nargs, parm_idx, arg_idx, lost = 0;
  int nparms, nargs, parm_idx, arg_idx, lost = 0;
  tree inner_args;
  tree inner_args;
  tree new_args;
  tree new_args;
  tree new_inner_args;
  tree new_inner_args;
  int saved_unevaluated_operand;
  int saved_unevaluated_operand;
  int saved_inhibit_evaluation_warnings;
  int saved_inhibit_evaluation_warnings;
 
 
  /* When used as a boolean value, indicates whether this is a
  /* When used as a boolean value, indicates whether this is a
     variadic template parameter list. Since it's an int, we can also
     variadic template parameter list. Since it's an int, we can also
     subtract it from nparms to get the number of non-variadic
     subtract it from nparms to get the number of non-variadic
     parameters.  */
     parameters.  */
  int variadic_p = 0;
  int variadic_p = 0;
 
 
  nparms = TREE_VEC_LENGTH (parms);
  nparms = TREE_VEC_LENGTH (parms);
 
 
  /* Determine if there are any parameter packs.  */
  /* Determine if there are any parameter packs.  */
  for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
  for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    {
    {
      tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
      tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
      if (template_parameter_pack_p (tparm))
      if (template_parameter_pack_p (tparm))
        ++variadic_p;
        ++variadic_p;
    }
    }
 
 
  inner_args = INNERMOST_TEMPLATE_ARGS (args);
  inner_args = INNERMOST_TEMPLATE_ARGS (args);
  /* If there are 0 or 1 parameter packs, we need to expand any argument
  /* If there are 0 or 1 parameter packs, we need to expand any argument
     packs so that we can deduce a parameter pack from some non-packed args
     packs so that we can deduce a parameter pack from some non-packed args
     followed by an argument pack, as in variadic85.C.  If there are more
     followed by an argument pack, as in variadic85.C.  If there are more
     than that, we need to leave argument packs intact so the arguments are
     than that, we need to leave argument packs intact so the arguments are
     assigned to the right parameter packs.  This should only happen when
     assigned to the right parameter packs.  This should only happen when
     dealing with a nested class inside a partial specialization of a class
     dealing with a nested class inside a partial specialization of a class
     template, as in variadic92.C.  */
     template, as in variadic92.C.  */
  if (variadic_p <= 1)
  if (variadic_p <= 1)
    inner_args = expand_template_argument_pack (inner_args);
    inner_args = expand_template_argument_pack (inner_args);
 
 
  nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
  nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
  if ((nargs > nparms && !variadic_p)
  if ((nargs > nparms && !variadic_p)
      || (nargs < nparms - variadic_p
      || (nargs < nparms - variadic_p
          && require_all_args
          && require_all_args
          && (!use_default_args
          && (!use_default_args
              || (TREE_VEC_ELT (parms, nargs) != error_mark_node
              || (TREE_VEC_ELT (parms, nargs) != error_mark_node
                  && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
                  && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        {
        {
          const char *or_more = "";
          const char *or_more = "";
          if (variadic_p)
          if (variadic_p)
            {
            {
              or_more = " or more";
              or_more = " or more";
              --nparms;
              --nparms;
            }
            }
 
 
          error ("wrong number of template arguments (%d, should be %d%s)",
          error ("wrong number of template arguments (%d, should be %d%s)",
                 nargs, nparms, or_more);
                 nargs, nparms, or_more);
 
 
          if (in_decl)
          if (in_decl)
            error ("provided for %q+D", in_decl);
            error ("provided for %q+D", in_decl);
        }
        }
 
 
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* We need to evaluate the template arguments, even though this
  /* We need to evaluate the template arguments, even though this
     template-id may be nested within a "sizeof".  */
     template-id may be nested within a "sizeof".  */
  saved_unevaluated_operand = cp_unevaluated_operand;
  saved_unevaluated_operand = cp_unevaluated_operand;
  cp_unevaluated_operand = 0;
  cp_unevaluated_operand = 0;
  saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
  saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
  c_inhibit_evaluation_warnings = 0;
  c_inhibit_evaluation_warnings = 0;
  new_inner_args = make_tree_vec (nparms);
  new_inner_args = make_tree_vec (nparms);
  new_args = add_outermost_template_args (args, new_inner_args);
  new_args = add_outermost_template_args (args, new_inner_args);
  for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
  for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    {
    {
      tree arg;
      tree arg;
      tree parm;
      tree parm;
 
 
      /* Get the Ith template parameter.  */
      /* Get the Ith template parameter.  */
      parm = TREE_VEC_ELT (parms, parm_idx);
      parm = TREE_VEC_ELT (parms, parm_idx);
 
 
      if (parm == error_mark_node)
      if (parm == error_mark_node)
      {
      {
        TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
        TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
        continue;
        continue;
      }
      }
 
 
      /* Calculate the next argument.  */
      /* Calculate the next argument.  */
      if (arg_idx < nargs)
      if (arg_idx < nargs)
        arg = TREE_VEC_ELT (inner_args, arg_idx);
        arg = TREE_VEC_ELT (inner_args, arg_idx);
      else
      else
        arg = NULL_TREE;
        arg = NULL_TREE;
 
 
      if (template_parameter_pack_p (TREE_VALUE (parm))
      if (template_parameter_pack_p (TREE_VALUE (parm))
          && !(arg && ARGUMENT_PACK_P (arg)))
          && !(arg && ARGUMENT_PACK_P (arg)))
        {
        {
          /* All remaining arguments will be placed in the
          /* All remaining arguments will be placed in the
             template parameter pack PARM.  */
             template parameter pack PARM.  */
          arg = coerce_template_parameter_pack (parms, parm_idx, args,
          arg = coerce_template_parameter_pack (parms, parm_idx, args,
                                                inner_args, arg_idx,
                                                inner_args, arg_idx,
                                                new_args, &lost,
                                                new_args, &lost,
                                                in_decl, complain);
                                                in_decl, complain);
 
 
          /* Store this argument.  */
          /* Store this argument.  */
          if (arg == error_mark_node)
          if (arg == error_mark_node)
            lost++;
            lost++;
          TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
          TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
 
 
          /* We are done with all of the arguments.  */
          /* We are done with all of the arguments.  */
          arg_idx = nargs;
          arg_idx = nargs;
 
 
          continue;
          continue;
        }
        }
      else if (arg)
      else if (arg)
        {
        {
          if (PACK_EXPANSION_P (arg))
          if (PACK_EXPANSION_P (arg))
            {
            {
              if (complain & tf_error)
              if (complain & tf_error)
                {
                {
                  /* FIXME this restriction was removed by N2555; see
                  /* FIXME this restriction was removed by N2555; see
                     bug 35722.  */
                     bug 35722.  */
                  /* If ARG is a pack expansion, but PARM is not a
                  /* If ARG is a pack expansion, but PARM is not a
                     template parameter pack (if it were, we would have
                     template parameter pack (if it were, we would have
                     handled it above), we're trying to expand into a
                     handled it above), we're trying to expand into a
                     fixed-length argument list.  */
                     fixed-length argument list.  */
                  if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
                  if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
                    sorry ("cannot expand %<%E%> into a fixed-length "
                    sorry ("cannot expand %<%E%> into a fixed-length "
                           "argument list", arg);
                           "argument list", arg);
                  else
                  else
                    sorry ("cannot expand %<%T%> into a fixed-length "
                    sorry ("cannot expand %<%T%> into a fixed-length "
                           "argument list", arg);
                           "argument list", arg);
                }
                }
              return error_mark_node;
              return error_mark_node;
            }
            }
        }
        }
      else if (require_all_args)
      else if (require_all_args)
        {
        {
          /* There must be a default arg in this case.  */
          /* There must be a default arg in this case.  */
          arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
          arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
                                     complain, in_decl);
                                     complain, in_decl);
          /* The position of the first default template argument,
          /* The position of the first default template argument,
             is also the number of non-defaulted arguments in NEW_INNER_ARGS.
             is also the number of non-defaulted arguments in NEW_INNER_ARGS.
             Record that.  */
             Record that.  */
          if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
          if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
            SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
            SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
        }
        }
      else
      else
        break;
        break;
 
 
      if (arg == error_mark_node)
      if (arg == error_mark_node)
        {
        {
          if (complain & tf_error)
          if (complain & tf_error)
            error ("template argument %d is invalid", arg_idx + 1);
            error ("template argument %d is invalid", arg_idx + 1);
        }
        }
      else if (!arg)
      else if (!arg)
        /* This only occurs if there was an error in the template
        /* This only occurs if there was an error in the template
           parameter list itself (which we would already have
           parameter list itself (which we would already have
           reported) that we are trying to recover from, e.g., a class
           reported) that we are trying to recover from, e.g., a class
           template with a parameter list such as
           template with a parameter list such as
           template<typename..., typename>.  */
           template<typename..., typename>.  */
        return error_mark_node;
        return error_mark_node;
      else
      else
        arg = convert_template_argument (TREE_VALUE (parm),
        arg = convert_template_argument (TREE_VALUE (parm),
                                         arg, new_args, complain,
                                         arg, new_args, complain,
                                         parm_idx, in_decl);
                                         parm_idx, in_decl);
 
 
      if (arg == error_mark_node)
      if (arg == error_mark_node)
        lost++;
        lost++;
      TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
      TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
    }
    }
  cp_unevaluated_operand = saved_unevaluated_operand;
  cp_unevaluated_operand = saved_unevaluated_operand;
  c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
  c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
 
 
  if (lost)
  if (lost)
    return error_mark_node;
    return error_mark_node;
 
 
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
                                         TREE_VEC_LENGTH (new_inner_args));
                                         TREE_VEC_LENGTH (new_inner_args));
#endif
#endif
 
 
  return new_inner_args;
  return new_inner_args;
}
}
 
 
/* Returns 1 if template args OT and NT are equivalent.  */
/* Returns 1 if template args OT and NT are equivalent.  */
 
 
static int
static int
template_args_equal (tree ot, tree nt)
template_args_equal (tree ot, tree nt)
{
{
  if (nt == ot)
  if (nt == ot)
    return 1;
    return 1;
 
 
  if (TREE_CODE (nt) == TREE_VEC)
  if (TREE_CODE (nt) == TREE_VEC)
    /* For member templates */
    /* For member templates */
    return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
    return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
  else if (PACK_EXPANSION_P (ot))
  else if (PACK_EXPANSION_P (ot))
    return PACK_EXPANSION_P (nt)
    return PACK_EXPANSION_P (nt)
      && template_args_equal (PACK_EXPANSION_PATTERN (ot),
      && template_args_equal (PACK_EXPANSION_PATTERN (ot),
                              PACK_EXPANSION_PATTERN (nt));
                              PACK_EXPANSION_PATTERN (nt));
  else if (ARGUMENT_PACK_P (ot))
  else if (ARGUMENT_PACK_P (ot))
    {
    {
      int i, len;
      int i, len;
      tree opack, npack;
      tree opack, npack;
 
 
      if (!ARGUMENT_PACK_P (nt))
      if (!ARGUMENT_PACK_P (nt))
        return 0;
        return 0;
 
 
      opack = ARGUMENT_PACK_ARGS (ot);
      opack = ARGUMENT_PACK_ARGS (ot);
      npack = ARGUMENT_PACK_ARGS (nt);
      npack = ARGUMENT_PACK_ARGS (nt);
      len = TREE_VEC_LENGTH (opack);
      len = TREE_VEC_LENGTH (opack);
      if (TREE_VEC_LENGTH (npack) != len)
      if (TREE_VEC_LENGTH (npack) != len)
        return 0;
        return 0;
      for (i = 0; i < len; ++i)
      for (i = 0; i < len; ++i)
        if (!template_args_equal (TREE_VEC_ELT (opack, i),
        if (!template_args_equal (TREE_VEC_ELT (opack, i),
                                  TREE_VEC_ELT (npack, i)))
                                  TREE_VEC_ELT (npack, i)))
          return 0;
          return 0;
      return 1;
      return 1;
    }
    }
  else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
  else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    {
    {
      /* We get here probably because we are in the middle of substituting
      /* We get here probably because we are in the middle of substituting
         into the pattern of a pack expansion. In that case the
         into the pattern of a pack expansion. In that case the
         ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
         ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
         interested in. So we want to use the initial pack argument for
         interested in. So we want to use the initial pack argument for
         the comparison.  */
         the comparison.  */
      ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
      ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
      if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
      if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
        nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
        nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
      return template_args_equal (ot, nt);
      return template_args_equal (ot, nt);
    }
    }
  else if (TYPE_P (nt))
  else if (TYPE_P (nt))
    return TYPE_P (ot) && same_type_p (ot, nt);
    return TYPE_P (ot) && same_type_p (ot, nt);
  else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
  else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
    return 0;
    return 0;
  else
  else
    return cp_tree_equal (ot, nt);
    return cp_tree_equal (ot, nt);
}
}
 
 
/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
   of template arguments.  Returns 0 otherwise.  */
   of template arguments.  Returns 0 otherwise.  */
 
 
int
int
comp_template_args (tree oldargs, tree newargs)
comp_template_args (tree oldargs, tree newargs)
{
{
  int i;
  int i;
 
 
  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    return 0;
    return 0;
 
 
  for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
  for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    {
    {
      tree nt = TREE_VEC_ELT (newargs, i);
      tree nt = TREE_VEC_ELT (newargs, i);
      tree ot = TREE_VEC_ELT (oldargs, i);
      tree ot = TREE_VEC_ELT (oldargs, i);
 
 
      if (! template_args_equal (ot, nt))
      if (! template_args_equal (ot, nt))
        return 0;
        return 0;
    }
    }
  return 1;
  return 1;
}
}
 
 
static void
static void
add_pending_template (tree d)
add_pending_template (tree d)
{
{
  tree ti = (TYPE_P (d)
  tree ti = (TYPE_P (d)
             ? CLASSTYPE_TEMPLATE_INFO (d)
             ? CLASSTYPE_TEMPLATE_INFO (d)
             : DECL_TEMPLATE_INFO (d));
             : DECL_TEMPLATE_INFO (d));
  struct pending_template *pt;
  struct pending_template *pt;
  int level;
  int level;
 
 
  if (TI_PENDING_TEMPLATE_FLAG (ti))
  if (TI_PENDING_TEMPLATE_FLAG (ti))
    return;
    return;
 
 
  /* We are called both from instantiate_decl, where we've already had a
  /* We are called both from instantiate_decl, where we've already had a
     tinst_level pushed, and instantiate_template, where we haven't.
     tinst_level pushed, and instantiate_template, where we haven't.
     Compensate.  */
     Compensate.  */
  level = !current_tinst_level || current_tinst_level->decl != d;
  level = !current_tinst_level || current_tinst_level->decl != d;
 
 
  if (level)
  if (level)
    push_tinst_level (d);
    push_tinst_level (d);
 
 
  pt = GGC_NEW (struct pending_template);
  pt = GGC_NEW (struct pending_template);
  pt->next = NULL;
  pt->next = NULL;
  pt->tinst = current_tinst_level;
  pt->tinst = current_tinst_level;
  if (last_pending_template)
  if (last_pending_template)
    last_pending_template->next = pt;
    last_pending_template->next = pt;
  else
  else
    pending_templates = pt;
    pending_templates = pt;
 
 
  last_pending_template = pt;
  last_pending_template = pt;
 
 
  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
 
 
  if (level)
  if (level)
    pop_tinst_level ();
    pop_tinst_level ();
}
}
 
 
 
 
/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
   ARGLIST.  Valid choices for FNS are given in the cp-tree.def
   ARGLIST.  Valid choices for FNS are given in the cp-tree.def
   documentation for TEMPLATE_ID_EXPR.  */
   documentation for TEMPLATE_ID_EXPR.  */
 
 
tree
tree
lookup_template_function (tree fns, tree arglist)
lookup_template_function (tree fns, tree arglist)
{
{
  tree type;
  tree type;
 
 
  if (fns == error_mark_node || arglist == error_mark_node)
  if (fns == error_mark_node || arglist == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
  gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
  gcc_assert (fns && (is_overloaded_fn (fns)
  gcc_assert (fns && (is_overloaded_fn (fns)
                      || TREE_CODE (fns) == IDENTIFIER_NODE));
                      || TREE_CODE (fns) == IDENTIFIER_NODE));
 
 
  if (BASELINK_P (fns))
  if (BASELINK_P (fns))
    {
    {
      BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
      BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
                                         unknown_type_node,
                                         unknown_type_node,
                                         BASELINK_FUNCTIONS (fns),
                                         BASELINK_FUNCTIONS (fns),
                                         arglist);
                                         arglist);
      return fns;
      return fns;
    }
    }
 
 
  type = TREE_TYPE (fns);
  type = TREE_TYPE (fns);
  if (TREE_CODE (fns) == OVERLOAD || !type)
  if (TREE_CODE (fns) == OVERLOAD || !type)
    type = unknown_type_node;
    type = unknown_type_node;
 
 
  return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
  return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
}
}
 
 
/* Within the scope of a template class S<T>, the name S gets bound
/* Within the scope of a template class S<T>, the name S gets bound
   (in build_self_reference) to a TYPE_DECL for the class, not a
   (in build_self_reference) to a TYPE_DECL for the class, not a
   TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   or one of its enclosing classes, and that type is a template,
   or one of its enclosing classes, and that type is a template,
   return the associated TEMPLATE_DECL.  Otherwise, the original
   return the associated TEMPLATE_DECL.  Otherwise, the original
   DECL is returned.
   DECL is returned.
 
 
   Also handle the case when DECL is a TREE_LIST of ambiguous
   Also handle the case when DECL is a TREE_LIST of ambiguous
   injected-class-names from different bases.  */
   injected-class-names from different bases.  */
 
 
tree
tree
maybe_get_template_decl_from_type_decl (tree decl)
maybe_get_template_decl_from_type_decl (tree decl)
{
{
  if (decl == NULL_TREE)
  if (decl == NULL_TREE)
    return decl;
    return decl;
 
 
  /* DR 176: A lookup that finds an injected-class-name (10.2
  /* DR 176: A lookup that finds an injected-class-name (10.2
     [class.member.lookup]) can result in an ambiguity in certain cases
     [class.member.lookup]) can result in an ambiguity in certain cases
     (for example, if it is found in more than one base class). If all of
     (for example, if it is found in more than one base class). If all of
     the injected-class-names that are found refer to specializations of
     the injected-class-names that are found refer to specializations of
     the same class template, and if the name is followed by a
     the same class template, and if the name is followed by a
     template-argument-list, the reference refers to the class template
     template-argument-list, the reference refers to the class template
     itself and not a specialization thereof, and is not ambiguous.  */
     itself and not a specialization thereof, and is not ambiguous.  */
  if (TREE_CODE (decl) == TREE_LIST)
  if (TREE_CODE (decl) == TREE_LIST)
    {
    {
      tree t, tmpl = NULL_TREE;
      tree t, tmpl = NULL_TREE;
      for (t = decl; t; t = TREE_CHAIN (t))
      for (t = decl; t; t = TREE_CHAIN (t))
        {
        {
          tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
          tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
          if (!tmpl)
          if (!tmpl)
            tmpl = elt;
            tmpl = elt;
          else if (tmpl != elt)
          else if (tmpl != elt)
            break;
            break;
        }
        }
      if (tmpl && t == NULL_TREE)
      if (tmpl && t == NULL_TREE)
        return tmpl;
        return tmpl;
      else
      else
        return decl;
        return decl;
    }
    }
 
 
  return (decl != NULL_TREE
  return (decl != NULL_TREE
          && DECL_SELF_REFERENCE_P (decl)
          && DECL_SELF_REFERENCE_P (decl)
          && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
          && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
    ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
    ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
}
}
 
 
/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
   parameters, find the desired type.
   parameters, find the desired type.
 
 
   D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
 
 
   IN_DECL, if non-NULL, is the template declaration we are trying to
   IN_DECL, if non-NULL, is the template declaration we are trying to
   instantiate.
   instantiate.
 
 
   If ENTERING_SCOPE is nonzero, we are about to enter the scope of
   If ENTERING_SCOPE is nonzero, we are about to enter the scope of
   the class we are looking up.
   the class we are looking up.
 
 
   Issue error and warning messages under control of COMPLAIN.
   Issue error and warning messages under control of COMPLAIN.
 
 
   If the template class is really a local class in a template
   If the template class is really a local class in a template
   function, then the FUNCTION_CONTEXT is the function in which it is
   function, then the FUNCTION_CONTEXT is the function in which it is
   being instantiated.
   being instantiated.
 
 
   ??? Note that this function is currently called *twice* for each
   ??? Note that this function is currently called *twice* for each
   template-id: the first time from the parser, while creating the
   template-id: the first time from the parser, while creating the
   incomplete type (finish_template_type), and the second type during the
   incomplete type (finish_template_type), and the second type during the
   real instantiation (instantiate_template_class). This is surely something
   real instantiation (instantiate_template_class). This is surely something
   that we want to avoid. It also causes some problems with argument
   that we want to avoid. It also causes some problems with argument
   coercion (see convert_nontype_argument for more information on this).  */
   coercion (see convert_nontype_argument for more information on this).  */
 
 
tree
tree
lookup_template_class (tree d1,
lookup_template_class (tree d1,
                       tree arglist,
                       tree arglist,
                       tree in_decl,
                       tree in_decl,
                       tree context,
                       tree context,
                       int entering_scope,
                       int entering_scope,
                       tsubst_flags_t complain)
                       tsubst_flags_t complain)
{
{
  tree templ = NULL_TREE, parmlist;
  tree templ = NULL_TREE, parmlist;
  tree t;
  tree t;
  spec_entry **slot;
  spec_entry **slot;
  spec_entry *entry;
  spec_entry *entry;
  spec_entry elt;
  spec_entry elt;
  hashval_t hash;
  hashval_t hash;
 
 
  timevar_push (TV_NAME_LOOKUP);
  timevar_push (TV_NAME_LOOKUP);
 
 
  if (TREE_CODE (d1) == IDENTIFIER_NODE)
  if (TREE_CODE (d1) == IDENTIFIER_NODE)
    {
    {
      tree value = innermost_non_namespace_value (d1);
      tree value = innermost_non_namespace_value (d1);
      if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
      if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
        templ = value;
        templ = value;
      else
      else
        {
        {
          if (context)
          if (context)
            push_decl_namespace (context);
            push_decl_namespace (context);
          templ = lookup_name (d1);
          templ = lookup_name (d1);
          templ = maybe_get_template_decl_from_type_decl (templ);
          templ = maybe_get_template_decl_from_type_decl (templ);
          if (context)
          if (context)
            pop_decl_namespace ();
            pop_decl_namespace ();
        }
        }
      if (templ)
      if (templ)
        context = DECL_CONTEXT (templ);
        context = DECL_CONTEXT (templ);
    }
    }
  else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
  else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
    {
    {
      tree type = TREE_TYPE (d1);
      tree type = TREE_TYPE (d1);
 
 
      /* If we are declaring a constructor, say A<T>::A<T>, we will get
      /* If we are declaring a constructor, say A<T>::A<T>, we will get
         an implicit typename for the second A.  Deal with it.  */
         an implicit typename for the second A.  Deal with it.  */
      if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
      if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
        type = TREE_TYPE (type);
        type = TREE_TYPE (type);
 
 
      if (CLASSTYPE_TEMPLATE_INFO (type))
      if (CLASSTYPE_TEMPLATE_INFO (type))
        {
        {
          templ = CLASSTYPE_TI_TEMPLATE (type);
          templ = CLASSTYPE_TI_TEMPLATE (type);
          d1 = DECL_NAME (templ);
          d1 = DECL_NAME (templ);
        }
        }
    }
    }
  else if (TREE_CODE (d1) == ENUMERAL_TYPE
  else if (TREE_CODE (d1) == ENUMERAL_TYPE
           || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
           || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
    {
    {
      templ = TYPE_TI_TEMPLATE (d1);
      templ = TYPE_TI_TEMPLATE (d1);
      d1 = DECL_NAME (templ);
      d1 = DECL_NAME (templ);
    }
    }
  else if (TREE_CODE (d1) == TEMPLATE_DECL
  else if (TREE_CODE (d1) == TEMPLATE_DECL
           && DECL_TEMPLATE_RESULT (d1)
           && DECL_TEMPLATE_RESULT (d1)
           && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
           && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
    {
    {
      templ = d1;
      templ = d1;
      d1 = DECL_NAME (templ);
      d1 = DECL_NAME (templ);
      context = DECL_CONTEXT (templ);
      context = DECL_CONTEXT (templ);
    }
    }
 
 
  /* Issue an error message if we didn't find a template.  */
  /* Issue an error message if we didn't find a template.  */
  if (! templ)
  if (! templ)
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        error ("%qT is not a template", d1);
        error ("%qT is not a template", d1);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
    }
    }
 
 
  if (TREE_CODE (templ) != TEMPLATE_DECL
  if (TREE_CODE (templ) != TEMPLATE_DECL
         /* Make sure it's a user visible template, if it was named by
         /* Make sure it's a user visible template, if it was named by
            the user.  */
            the user.  */
      || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
      || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
          && !PRIMARY_TEMPLATE_P (templ)))
          && !PRIMARY_TEMPLATE_P (templ)))
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        {
        {
          error ("non-template type %qT used as a template", d1);
          error ("non-template type %qT used as a template", d1);
          if (in_decl)
          if (in_decl)
            error ("for template declaration %q+D", in_decl);
            error ("for template declaration %q+D", in_decl);
        }
        }
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
    }
    }
 
 
  complain &= ~tf_user;
  complain &= ~tf_user;
 
 
  if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
  if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
    {
    {
      /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
      /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
         template arguments */
         template arguments */
 
 
      tree parm;
      tree parm;
      tree arglist2;
      tree arglist2;
      tree outer;
      tree outer;
 
 
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
 
 
      /* Consider an example where a template template parameter declared as
      /* Consider an example where a template template parameter declared as
 
 
           template <class T, class U = std::allocator<T> > class TT
           template <class T, class U = std::allocator<T> > class TT
 
 
         The template parameter level of T and U are one level larger than
         The template parameter level of T and U are one level larger than
         of TT.  To proper process the default argument of U, say when an
         of TT.  To proper process the default argument of U, say when an
         instantiation `TT<int>' is seen, we need to build the full
         instantiation `TT<int>' is seen, we need to build the full
         arguments containing {int} as the innermost level.  Outer levels,
         arguments containing {int} as the innermost level.  Outer levels,
         available when not appearing as default template argument, can be
         available when not appearing as default template argument, can be
         obtained from the arguments of the enclosing template.
         obtained from the arguments of the enclosing template.
 
 
         Suppose that TT is later substituted with std::vector.  The above
         Suppose that TT is later substituted with std::vector.  The above
         instantiation is `TT<int, std::allocator<T> >' with TT at
         instantiation is `TT<int, std::allocator<T> >' with TT at
         level 1, and T at level 2, while the template arguments at level 1
         level 1, and T at level 2, while the template arguments at level 1
         becomes {std::vector} and the inner level 2 is {int}.  */
         becomes {std::vector} and the inner level 2 is {int}.  */
 
 
      outer = DECL_CONTEXT (templ);
      outer = DECL_CONTEXT (templ);
      if (outer)
      if (outer)
        outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
        outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
      else if (current_template_parms)
      else if (current_template_parms)
        /* This is an argument of the current template, so we haven't set
        /* This is an argument of the current template, so we haven't set
           DECL_CONTEXT yet.  */
           DECL_CONTEXT yet.  */
        outer = current_template_args ();
        outer = current_template_args ();
 
 
      if (outer)
      if (outer)
        arglist = add_to_template_args (outer, arglist);
        arglist = add_to_template_args (outer, arglist);
 
 
      arglist2 = coerce_template_parms (parmlist, arglist, templ,
      arglist2 = coerce_template_parms (parmlist, arglist, templ,
                                        complain,
                                        complain,
                                        /*require_all_args=*/true,
                                        /*require_all_args=*/true,
                                        /*use_default_args=*/true);
                                        /*use_default_args=*/true);
      if (arglist2 == error_mark_node
      if (arglist2 == error_mark_node
          || (!uses_template_parms (arglist2)
          || (!uses_template_parms (arglist2)
              && check_instantiated_args (templ, arglist2, complain)))
              && check_instantiated_args (templ, arglist2, complain)))
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
 
      parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
      parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
    }
    }
  else
  else
    {
    {
      tree template_type = TREE_TYPE (templ);
      tree template_type = TREE_TYPE (templ);
      tree gen_tmpl;
      tree gen_tmpl;
      tree type_decl;
      tree type_decl;
      tree found = NULL_TREE;
      tree found = NULL_TREE;
      int arg_depth;
      int arg_depth;
      int parm_depth;
      int parm_depth;
      int is_dependent_type;
      int is_dependent_type;
      int use_partial_inst_tmpl = false;
      int use_partial_inst_tmpl = false;
 
 
      gen_tmpl = most_general_template (templ);
      gen_tmpl = most_general_template (templ);
      parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
      parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
      parm_depth = TMPL_PARMS_DEPTH (parmlist);
      parm_depth = TMPL_PARMS_DEPTH (parmlist);
      arg_depth = TMPL_ARGS_DEPTH (arglist);
      arg_depth = TMPL_ARGS_DEPTH (arglist);
 
 
      if (arg_depth == 1 && parm_depth > 1)
      if (arg_depth == 1 && parm_depth > 1)
        {
        {
          /* We've been given an incomplete set of template arguments.
          /* We've been given an incomplete set of template arguments.
             For example, given:
             For example, given:
 
 
               template <class T> struct S1 {
               template <class T> struct S1 {
                 template <class U> struct S2 {};
                 template <class U> struct S2 {};
                 template <class U> struct S2<U*> {};
                 template <class U> struct S2<U*> {};
                };
                };
 
 
             we will be called with an ARGLIST of `U*', but the
             we will be called with an ARGLIST of `U*', but the
             TEMPLATE will be `template <class T> template
             TEMPLATE will be `template <class T> template
             <class U> struct S1<T>::S2'.  We must fill in the missing
             <class U> struct S1<T>::S2'.  We must fill in the missing
             arguments.  */
             arguments.  */
          arglist
          arglist
            = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
            = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
                                           arglist);
                                           arglist);
          arg_depth = TMPL_ARGS_DEPTH (arglist);
          arg_depth = TMPL_ARGS_DEPTH (arglist);
        }
        }
 
 
      /* Now we should have enough arguments.  */
      /* Now we should have enough arguments.  */
      gcc_assert (parm_depth == arg_depth);
      gcc_assert (parm_depth == arg_depth);
 
 
      /* From here on, we're only interested in the most general
      /* From here on, we're only interested in the most general
         template.  */
         template.  */
 
 
      /* Calculate the BOUND_ARGS.  These will be the args that are
      /* Calculate the BOUND_ARGS.  These will be the args that are
         actually tsubst'd into the definition to create the
         actually tsubst'd into the definition to create the
         instantiation.  */
         instantiation.  */
      if (parm_depth > 1)
      if (parm_depth > 1)
        {
        {
          /* We have multiple levels of arguments to coerce, at once.  */
          /* We have multiple levels of arguments to coerce, at once.  */
          int i;
          int i;
          int saved_depth = TMPL_ARGS_DEPTH (arglist);
          int saved_depth = TMPL_ARGS_DEPTH (arglist);
 
 
          tree bound_args = make_tree_vec (parm_depth);
          tree bound_args = make_tree_vec (parm_depth);
 
 
          for (i = saved_depth,
          for (i = saved_depth,
                 t = DECL_TEMPLATE_PARMS (gen_tmpl);
                 t = DECL_TEMPLATE_PARMS (gen_tmpl);
               i > 0 && t != NULL_TREE;
               i > 0 && t != NULL_TREE;
               --i, t = TREE_CHAIN (t))
               --i, t = TREE_CHAIN (t))
            {
            {
              tree a = coerce_template_parms (TREE_VALUE (t),
              tree a = coerce_template_parms (TREE_VALUE (t),
                                              arglist, gen_tmpl,
                                              arglist, gen_tmpl,
                                              complain,
                                              complain,
                                              /*require_all_args=*/true,
                                              /*require_all_args=*/true,
                                              /*use_default_args=*/true);
                                              /*use_default_args=*/true);
 
 
              /* Don't process further if one of the levels fails.  */
              /* Don't process further if one of the levels fails.  */
              if (a == error_mark_node)
              if (a == error_mark_node)
                {
                {
                  /* Restore the ARGLIST to its full size.  */
                  /* Restore the ARGLIST to its full size.  */
                  TREE_VEC_LENGTH (arglist) = saved_depth;
                  TREE_VEC_LENGTH (arglist) = saved_depth;
                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
                }
                }
 
 
              SET_TMPL_ARGS_LEVEL (bound_args, i, a);
              SET_TMPL_ARGS_LEVEL (bound_args, i, a);
 
 
              /* We temporarily reduce the length of the ARGLIST so
              /* We temporarily reduce the length of the ARGLIST so
                 that coerce_template_parms will see only the arguments
                 that coerce_template_parms will see only the arguments
                 corresponding to the template parameters it is
                 corresponding to the template parameters it is
                 examining.  */
                 examining.  */
              TREE_VEC_LENGTH (arglist)--;
              TREE_VEC_LENGTH (arglist)--;
            }
            }
 
 
          /* Restore the ARGLIST to its full size.  */
          /* Restore the ARGLIST to its full size.  */
          TREE_VEC_LENGTH (arglist) = saved_depth;
          TREE_VEC_LENGTH (arglist) = saved_depth;
 
 
          arglist = bound_args;
          arglist = bound_args;
        }
        }
      else
      else
        arglist
        arglist
          = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
          = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
                                   INNERMOST_TEMPLATE_ARGS (arglist),
                                   INNERMOST_TEMPLATE_ARGS (arglist),
                                   gen_tmpl,
                                   gen_tmpl,
                                   complain,
                                   complain,
                                   /*require_all_args=*/true,
                                   /*require_all_args=*/true,
                                   /*use_default_args=*/true);
                                   /*use_default_args=*/true);
 
 
      if (arglist == error_mark_node)
      if (arglist == error_mark_node)
        /* We were unable to bind the arguments.  */
        /* We were unable to bind the arguments.  */
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
 
      /* In the scope of a template class, explicit references to the
      /* In the scope of a template class, explicit references to the
         template class refer to the type of the template, not any
         template class refer to the type of the template, not any
         instantiation of it.  For example, in:
         instantiation of it.  For example, in:
 
 
           template <class T> class C { void f(C<T>); }
           template <class T> class C { void f(C<T>); }
 
 
         the `C<T>' is just the same as `C'.  Outside of the
         the `C<T>' is just the same as `C'.  Outside of the
         class, however, such a reference is an instantiation.  */
         class, however, such a reference is an instantiation.  */
      if ((entering_scope
      if ((entering_scope
           || !PRIMARY_TEMPLATE_P (gen_tmpl)
           || !PRIMARY_TEMPLATE_P (gen_tmpl)
           || currently_open_class (template_type))
           || currently_open_class (template_type))
          /* comp_template_args is expensive, check it last.  */
          /* comp_template_args is expensive, check it last.  */
          && comp_template_args (TYPE_TI_ARGS (template_type),
          && comp_template_args (TYPE_TI_ARGS (template_type),
                                 arglist))
                                 arglist))
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
 
 
      /* If we already have this specialization, return it.  */
      /* If we already have this specialization, return it.  */
      elt.tmpl = gen_tmpl;
      elt.tmpl = gen_tmpl;
      elt.args = arglist;
      elt.args = arglist;
      hash = hash_specialization (&elt);
      hash = hash_specialization (&elt);
      entry = (spec_entry *) htab_find_with_hash (type_specializations,
      entry = (spec_entry *) htab_find_with_hash (type_specializations,
                                                  &elt, hash);
                                                  &elt, hash);
 
 
      if (entry)
      if (entry)
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
 
 
      is_dependent_type = uses_template_parms (arglist);
      is_dependent_type = uses_template_parms (arglist);
 
 
      /* If the deduced arguments are invalid, then the binding
      /* If the deduced arguments are invalid, then the binding
         failed.  */
         failed.  */
      if (!is_dependent_type
      if (!is_dependent_type
          && check_instantiated_args (gen_tmpl,
          && check_instantiated_args (gen_tmpl,
                                      INNERMOST_TEMPLATE_ARGS (arglist),
                                      INNERMOST_TEMPLATE_ARGS (arglist),
                                      complain))
                                      complain))
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
 
      if (!is_dependent_type
      if (!is_dependent_type
          && !PRIMARY_TEMPLATE_P (gen_tmpl)
          && !PRIMARY_TEMPLATE_P (gen_tmpl)
          && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
          && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
          && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
          && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
        {
        {
          found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
          found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
                                      DECL_NAME (gen_tmpl),
                                      DECL_NAME (gen_tmpl),
                                      /*tag_scope=*/ts_global);
                                      /*tag_scope=*/ts_global);
          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
        }
        }
 
 
      context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
      context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
                        complain, in_decl);
                        complain, in_decl);
      if (!context)
      if (!context)
        context = global_namespace;
        context = global_namespace;
 
 
      /* Create the type.  */
      /* Create the type.  */
      if (TREE_CODE (template_type) == ENUMERAL_TYPE)
      if (TREE_CODE (template_type) == ENUMERAL_TYPE)
        {
        {
          if (!is_dependent_type)
          if (!is_dependent_type)
            {
            {
              set_current_access_from_decl (TYPE_NAME (template_type));
              set_current_access_from_decl (TYPE_NAME (template_type));
              t = start_enum (TYPE_IDENTIFIER (template_type),
              t = start_enum (TYPE_IDENTIFIER (template_type),
                              tsubst (ENUM_UNDERLYING_TYPE (template_type),
                              tsubst (ENUM_UNDERLYING_TYPE (template_type),
                                      arglist, complain, in_decl),
                                      arglist, complain, in_decl),
                              SCOPED_ENUM_P (template_type));
                              SCOPED_ENUM_P (template_type));
            }
            }
          else
          else
            {
            {
              /* We don't want to call start_enum for this type, since
              /* We don't want to call start_enum for this type, since
                 the values for the enumeration constants may involve
                 the values for the enumeration constants may involve
                 template parameters.  And, no one should be interested
                 template parameters.  And, no one should be interested
                 in the enumeration constants for such a type.  */
                 in the enumeration constants for such a type.  */
              t = cxx_make_type (ENUMERAL_TYPE);
              t = cxx_make_type (ENUMERAL_TYPE);
              SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
              SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
            }
            }
        }
        }
      else
      else
        {
        {
          t = make_class_type (TREE_CODE (template_type));
          t = make_class_type (TREE_CODE (template_type));
          CLASSTYPE_DECLARED_CLASS (t)
          CLASSTYPE_DECLARED_CLASS (t)
            = CLASSTYPE_DECLARED_CLASS (template_type);
            = CLASSTYPE_DECLARED_CLASS (template_type);
          SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
          SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
          TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
          TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
 
 
          /* A local class.  Make sure the decl gets registered properly.  */
          /* A local class.  Make sure the decl gets registered properly.  */
          if (context == current_function_decl)
          if (context == current_function_decl)
            pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
            pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
 
 
          if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
          if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
            /* This instantiation is another name for the primary
            /* This instantiation is another name for the primary
               template type. Set the TYPE_CANONICAL field
               template type. Set the TYPE_CANONICAL field
               appropriately. */
               appropriately. */
            TYPE_CANONICAL (t) = template_type;
            TYPE_CANONICAL (t) = template_type;
          else if (any_template_arguments_need_structural_equality_p (arglist))
          else if (any_template_arguments_need_structural_equality_p (arglist))
            /* Some of the template arguments require structural
            /* Some of the template arguments require structural
               equality testing, so this template class requires
               equality testing, so this template class requires
               structural equality testing. */
               structural equality testing. */
            SET_TYPE_STRUCTURAL_EQUALITY (t);
            SET_TYPE_STRUCTURAL_EQUALITY (t);
        }
        }
 
 
      /* If we called start_enum or pushtag above, this information
      /* If we called start_enum or pushtag above, this information
         will already be set up.  */
         will already be set up.  */
      if (!TYPE_NAME (t))
      if (!TYPE_NAME (t))
        {
        {
          TYPE_CONTEXT (t) = FROB_CONTEXT (context);
          TYPE_CONTEXT (t) = FROB_CONTEXT (context);
 
 
          type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
          type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
          DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
          DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
          DECL_SOURCE_LOCATION (type_decl)
          DECL_SOURCE_LOCATION (type_decl)
            = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
            = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
        }
        }
      else
      else
        type_decl = TYPE_NAME (t);
        type_decl = TYPE_NAME (t);
 
 
      TREE_PRIVATE (type_decl)
      TREE_PRIVATE (type_decl)
        = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
        = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
      TREE_PROTECTED (type_decl)
      TREE_PROTECTED (type_decl)
        = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
        = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
      if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
      if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
        {
        {
          DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
          DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
          DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
          DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
        }
        }
 
 
      /* Let's consider the explicit specialization of a member
      /* Let's consider the explicit specialization of a member
         of a class template specialization that is implicitely instantiated,
         of a class template specialization that is implicitely instantiated,
         e.g.:
         e.g.:
             template<class T>
             template<class T>
             struct S
             struct S
             {
             {
               template<class U> struct M {}; //#0
               template<class U> struct M {}; //#0
             };
             };
 
 
             template<>
             template<>
             template<>
             template<>
             struct S<int>::M<char> //#1
             struct S<int>::M<char> //#1
             {
             {
               int i;
               int i;
             };
             };
        [temp.expl.spec]/4 says this is valid.
        [temp.expl.spec]/4 says this is valid.
 
 
        In this case, when we write:
        In this case, when we write:
        S<int>::M<char> m;
        S<int>::M<char> m;
 
 
        M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
        M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
        the one of #0.
        the one of #0.
 
 
        When we encounter #1, we want to store the partial instantiation
        When we encounter #1, we want to store the partial instantiation
        of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
        of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
 
 
        For all cases other than this "explicit specialization of member of a
        For all cases other than this "explicit specialization of member of a
        class template", we just want to store the most general template into
        class template", we just want to store the most general template into
        the CLASSTYPE_TI_TEMPLATE of M.
        the CLASSTYPE_TI_TEMPLATE of M.
 
 
        This case of "explicit specialization of member of a class template"
        This case of "explicit specialization of member of a class template"
        only happens when:
        only happens when:
        1/ the enclosing class is an instantiation of, and therefore not
        1/ the enclosing class is an instantiation of, and therefore not
        the same as, the context of the most general template, and
        the same as, the context of the most general template, and
        2/ we aren't looking at the partial instantiation itself, i.e.
        2/ we aren't looking at the partial instantiation itself, i.e.
        the innermost arguments are not the same as the innermost parms of
        the innermost arguments are not the same as the innermost parms of
        the most general template.
        the most general template.
 
 
        So it's only when 1/ and 2/ happens that we want to use the partial
        So it's only when 1/ and 2/ happens that we want to use the partial
        instantiation of the member template in lieu of its most general
        instantiation of the member template in lieu of its most general
        template.  */
        template.  */
 
 
      if (PRIMARY_TEMPLATE_P (gen_tmpl)
      if (PRIMARY_TEMPLATE_P (gen_tmpl)
          && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
          && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
          /* the enclosing class must be an instantiation...  */
          /* the enclosing class must be an instantiation...  */
          && CLASS_TYPE_P (context)
          && CLASS_TYPE_P (context)
          && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
          && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
        {
        {
          tree partial_inst_args;
          tree partial_inst_args;
          TREE_VEC_LENGTH (arglist)--;
          TREE_VEC_LENGTH (arglist)--;
          ++processing_template_decl;
          ++processing_template_decl;
          partial_inst_args =
          partial_inst_args =
            tsubst (INNERMOST_TEMPLATE_ARGS
            tsubst (INNERMOST_TEMPLATE_ARGS
                        (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
                        (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
                    arglist, complain, NULL_TREE);
                    arglist, complain, NULL_TREE);
          --processing_template_decl;
          --processing_template_decl;
          TREE_VEC_LENGTH (arglist)++;
          TREE_VEC_LENGTH (arglist)++;
          use_partial_inst_tmpl =
          use_partial_inst_tmpl =
            /*...and we must not be looking at the partial instantiation
            /*...and we must not be looking at the partial instantiation
             itself. */
             itself. */
            !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
            !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
                                 partial_inst_args);
                                 partial_inst_args);
        }
        }
 
 
      if (!use_partial_inst_tmpl)
      if (!use_partial_inst_tmpl)
        /* This case is easy; there are no member templates involved.  */
        /* This case is easy; there are no member templates involved.  */
        found = gen_tmpl;
        found = gen_tmpl;
      else
      else
        {
        {
          /* This is a full instantiation of a member template.  Find
          /* This is a full instantiation of a member template.  Find
             the partial instantiation of which this is an instance.  */
             the partial instantiation of which this is an instance.  */
 
 
          /* Temporarily reduce by one the number of levels in the ARGLIST
          /* Temporarily reduce by one the number of levels in the ARGLIST
             so as to avoid comparing the last set of arguments.  */
             so as to avoid comparing the last set of arguments.  */
          TREE_VEC_LENGTH (arglist)--;
          TREE_VEC_LENGTH (arglist)--;
          found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
          found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
          TREE_VEC_LENGTH (arglist)++;
          TREE_VEC_LENGTH (arglist)++;
          found = CLASSTYPE_TI_TEMPLATE (found);
          found = CLASSTYPE_TI_TEMPLATE (found);
        }
        }
 
 
      SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
      SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
 
 
      elt.spec = t;
      elt.spec = t;
      slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
      slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
                                                       &elt, hash, INSERT);
                                                       &elt, hash, INSERT);
      *slot = GGC_NEW (spec_entry);
      *slot = GGC_NEW (spec_entry);
      **slot = elt;
      **slot = elt;
 
 
      /* Note this use of the partial instantiation so we can check it
      /* Note this use of the partial instantiation so we can check it
         later in maybe_process_partial_specialization.  */
         later in maybe_process_partial_specialization.  */
      DECL_TEMPLATE_INSTANTIATIONS (templ)
      DECL_TEMPLATE_INSTANTIATIONS (templ)
        = tree_cons (arglist, t,
        = tree_cons (arglist, t,
                     DECL_TEMPLATE_INSTANTIATIONS (templ));
                     DECL_TEMPLATE_INSTANTIATIONS (templ));
 
 
      if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
      if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
        /* Now that the type has been registered on the instantiations
        /* Now that the type has been registered on the instantiations
           list, we set up the enumerators.  Because the enumeration
           list, we set up the enumerators.  Because the enumeration
           constants may involve the enumeration type itself, we make
           constants may involve the enumeration type itself, we make
           sure to register the type first, and then create the
           sure to register the type first, and then create the
           constants.  That way, doing tsubst_expr for the enumeration
           constants.  That way, doing tsubst_expr for the enumeration
           constants won't result in recursive calls here; we'll find
           constants won't result in recursive calls here; we'll find
           the instantiation and exit above.  */
           the instantiation and exit above.  */
        tsubst_enum (template_type, t, arglist);
        tsubst_enum (template_type, t, arglist);
 
 
      if (is_dependent_type)
      if (is_dependent_type)
        /* If the type makes use of template parameters, the
        /* If the type makes use of template parameters, the
           code that generates debugging information will crash.  */
           code that generates debugging information will crash.  */
        DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
        DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
 
 
      /* Possibly limit visibility based on template args.  */
      /* Possibly limit visibility based on template args.  */
      TREE_PUBLIC (type_decl) = 1;
      TREE_PUBLIC (type_decl) = 1;
      determine_visibility (type_decl);
      determine_visibility (type_decl);
 
 
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
    }
    }
  timevar_pop (TV_NAME_LOOKUP);
  timevar_pop (TV_NAME_LOOKUP);
}
}


struct pair_fn_data
struct pair_fn_data
{
{
  tree_fn_t fn;
  tree_fn_t fn;
  void *data;
  void *data;
  /* True when we should also visit template parameters that occur in
  /* True when we should also visit template parameters that occur in
     non-deduced contexts.  */
     non-deduced contexts.  */
  bool include_nondeduced_p;
  bool include_nondeduced_p;
  struct pointer_set_t *visited;
  struct pointer_set_t *visited;
};
};
 
 
/* Called from for_each_template_parm via walk_tree.  */
/* Called from for_each_template_parm via walk_tree.  */
 
 
static tree
static tree
for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
{
{
  tree t = *tp;
  tree t = *tp;
  struct pair_fn_data *pfd = (struct pair_fn_data *) d;
  struct pair_fn_data *pfd = (struct pair_fn_data *) d;
  tree_fn_t fn = pfd->fn;
  tree_fn_t fn = pfd->fn;
  void *data = pfd->data;
  void *data = pfd->data;
 
 
  if (TYPE_P (t)
  if (TYPE_P (t)
      && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
      && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
      && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
      && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
                                 pfd->include_nondeduced_p))
                                 pfd->include_nondeduced_p))
    return error_mark_node;
    return error_mark_node;
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case RECORD_TYPE:
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
      if (TYPE_PTRMEMFUNC_P (t))
        break;
        break;
      /* Fall through.  */
      /* Fall through.  */
 
 
    case UNION_TYPE:
    case UNION_TYPE:
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
      if (!TYPE_TEMPLATE_INFO (t))
      if (!TYPE_TEMPLATE_INFO (t))
        *walk_subtrees = 0;
        *walk_subtrees = 0;
      else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
      else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
                                       fn, data, pfd->visited,
                                       fn, data, pfd->visited,
                                       pfd->include_nondeduced_p))
                                       pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case INTEGER_TYPE:
    case INTEGER_TYPE:
      if (for_each_template_parm (TYPE_MIN_VALUE (t),
      if (for_each_template_parm (TYPE_MIN_VALUE (t),
                                  fn, data, pfd->visited,
                                  fn, data, pfd->visited,
                                  pfd->include_nondeduced_p)
                                  pfd->include_nondeduced_p)
          || for_each_template_parm (TYPE_MAX_VALUE (t),
          || for_each_template_parm (TYPE_MAX_VALUE (t),
                                     fn, data, pfd->visited,
                                     fn, data, pfd->visited,
                                     pfd->include_nondeduced_p))
                                     pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case METHOD_TYPE:
    case METHOD_TYPE:
      /* Since we're not going to walk subtrees, we have to do this
      /* Since we're not going to walk subtrees, we have to do this
         explicitly here.  */
         explicitly here.  */
      if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
      if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
                                  pfd->visited, pfd->include_nondeduced_p))
                                  pfd->visited, pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      /* Fall through.  */
      /* Fall through.  */
 
 
    case FUNCTION_TYPE:
    case FUNCTION_TYPE:
      /* Check the return type.  */
      /* Check the return type.  */
      if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
      if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
                                  pfd->include_nondeduced_p))
                                  pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
 
 
      /* Check the parameter types.  Since default arguments are not
      /* Check the parameter types.  Since default arguments are not
         instantiated until they are needed, the TYPE_ARG_TYPES may
         instantiated until they are needed, the TYPE_ARG_TYPES may
         contain expressions that involve template parameters.  But,
         contain expressions that involve template parameters.  But,
         no-one should be looking at them yet.  And, once they're
         no-one should be looking at them yet.  And, once they're
         instantiated, they don't contain template parameters, so
         instantiated, they don't contain template parameters, so
         there's no point in looking at them then, either.  */
         there's no point in looking at them then, either.  */
      {
      {
        tree parm;
        tree parm;
 
 
        for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
        for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
          if (for_each_template_parm (TREE_VALUE (parm), fn, data,
          if (for_each_template_parm (TREE_VALUE (parm), fn, data,
                                      pfd->visited, pfd->include_nondeduced_p))
                                      pfd->visited, pfd->include_nondeduced_p))
            return error_mark_node;
            return error_mark_node;
 
 
        /* Since we've already handled the TYPE_ARG_TYPES, we don't
        /* Since we've already handled the TYPE_ARG_TYPES, we don't
           want walk_tree walking into them itself.  */
           want walk_tree walking into them itself.  */
        *walk_subtrees = 0;
        *walk_subtrees = 0;
      }
      }
      break;
      break;
 
 
    case TYPEOF_TYPE:
    case TYPEOF_TYPE:
      if (pfd->include_nondeduced_p
      if (pfd->include_nondeduced_p
          && for_each_template_parm (TYPE_FIELDS (t), fn, data,
          && for_each_template_parm (TYPE_FIELDS (t), fn, data,
                                     pfd->visited,
                                     pfd->visited,
                                     pfd->include_nondeduced_p))
                                     pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case FUNCTION_DECL:
    case FUNCTION_DECL:
    case VAR_DECL:
    case VAR_DECL:
      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
          && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
          && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
                                     pfd->visited, pfd->include_nondeduced_p))
                                     pfd->visited, pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      /* Fall through.  */
      /* Fall through.  */
 
 
    case PARM_DECL:
    case PARM_DECL:
    case CONST_DECL:
    case CONST_DECL:
      if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
      if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
          && for_each_template_parm (DECL_INITIAL (t), fn, data,
          && for_each_template_parm (DECL_INITIAL (t), fn, data,
                                     pfd->visited, pfd->include_nondeduced_p))
                                     pfd->visited, pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      if (DECL_CONTEXT (t)
      if (DECL_CONTEXT (t)
          && pfd->include_nondeduced_p
          && pfd->include_nondeduced_p
          && for_each_template_parm (DECL_CONTEXT (t), fn, data,
          && for_each_template_parm (DECL_CONTEXT (t), fn, data,
                                     pfd->visited, pfd->include_nondeduced_p))
                                     pfd->visited, pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
      /* Record template parameters such as `T' inside `TT<T>'.  */
      /* Record template parameters such as `T' inside `TT<T>'.  */
      if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
      if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
                                  pfd->include_nondeduced_p))
                                  pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      /* Fall through.  */
      /* Fall through.  */
 
 
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      if (fn && (*fn)(t, data))
      if (fn && (*fn)(t, data))
        return error_mark_node;
        return error_mark_node;
      else if (!fn)
      else if (!fn)
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      /* A template template parameter is encountered.  */
      /* A template template parameter is encountered.  */
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
          && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
          && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
                                     pfd->include_nondeduced_p))
                                     pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
 
 
      /* Already substituted template template parameter */
      /* Already substituted template template parameter */
      *walk_subtrees = 0;
      *walk_subtrees = 0;
      break;
      break;
 
 
    case TYPENAME_TYPE:
    case TYPENAME_TYPE:
      if (!fn
      if (!fn
          || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
          || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
                                     data, pfd->visited,
                                     data, pfd->visited,
                                     pfd->include_nondeduced_p))
                                     pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case CONSTRUCTOR:
    case CONSTRUCTOR:
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
          && pfd->include_nondeduced_p
          && pfd->include_nondeduced_p
          && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
          && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
                                     (TREE_TYPE (t)), fn, data,
                                     (TREE_TYPE (t)), fn, data,
                                     pfd->visited, pfd->include_nondeduced_p))
                                     pfd->visited, pfd->include_nondeduced_p))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case INDIRECT_REF:
    case INDIRECT_REF:
    case COMPONENT_REF:
    case COMPONENT_REF:
      /* If there's no type, then this thing must be some expression
      /* If there's no type, then this thing must be some expression
         involving template parameters.  */
         involving template parameters.  */
      if (!fn && !TREE_TYPE (t))
      if (!fn && !TREE_TYPE (t))
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    case MODOP_EXPR:
    case MODOP_EXPR:
    case CAST_EXPR:
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CONST_CAST_EXPR:
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case ARROW_EXPR:
    case ARROW_EXPR:
    case DOTSTAR_EXPR:
    case DOTSTAR_EXPR:
    case TYPEID_EXPR:
    case TYPEID_EXPR:
    case PSEUDO_DTOR_EXPR:
    case PSEUDO_DTOR_EXPR:
      if (!fn)
      if (!fn)
        return error_mark_node;
        return error_mark_node;
      break;
      break;
 
 
    default:
    default:
      break;
      break;
    }
    }
 
 
  /* We didn't find any template parameters we liked.  */
  /* We didn't find any template parameters we liked.  */
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   call FN with the parameter and the DATA.
   call FN with the parameter and the DATA.
   If FN returns nonzero, the iteration is terminated, and
   If FN returns nonzero, the iteration is terminated, and
   for_each_template_parm returns 1.  Otherwise, the iteration
   for_each_template_parm returns 1.  Otherwise, the iteration
   continues.  If FN never returns a nonzero value, the value
   continues.  If FN never returns a nonzero value, the value
   returned by for_each_template_parm is 0.  If FN is NULL, it is
   returned by for_each_template_parm is 0.  If FN is NULL, it is
   considered to be the function which always returns 1.
   considered to be the function which always returns 1.
 
 
   If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   parameters that occur in non-deduced contexts.  When false, only
   parameters that occur in non-deduced contexts.  When false, only
   visits those template parameters that can be deduced.  */
   visits those template parameters that can be deduced.  */
 
 
static int
static int
for_each_template_parm (tree t, tree_fn_t fn, void* data,
for_each_template_parm (tree t, tree_fn_t fn, void* data,
                        struct pointer_set_t *visited,
                        struct pointer_set_t *visited,
                        bool include_nondeduced_p)
                        bool include_nondeduced_p)
{
{
  struct pair_fn_data pfd;
  struct pair_fn_data pfd;
  int result;
  int result;
 
 
  /* Set up.  */
  /* Set up.  */
  pfd.fn = fn;
  pfd.fn = fn;
  pfd.data = data;
  pfd.data = data;
  pfd.include_nondeduced_p = include_nondeduced_p;
  pfd.include_nondeduced_p = include_nondeduced_p;
 
 
  /* Walk the tree.  (Conceptually, we would like to walk without
  /* Walk the tree.  (Conceptually, we would like to walk without
     duplicates, but for_each_template_parm_r recursively calls
     duplicates, but for_each_template_parm_r recursively calls
     for_each_template_parm, so we would need to reorganize a fair
     for_each_template_parm, so we would need to reorganize a fair
     bit to use walk_tree_without_duplicates, so we keep our own
     bit to use walk_tree_without_duplicates, so we keep our own
     visited list.)  */
     visited list.)  */
  if (visited)
  if (visited)
    pfd.visited = visited;
    pfd.visited = visited;
  else
  else
    pfd.visited = pointer_set_create ();
    pfd.visited = pointer_set_create ();
  result = cp_walk_tree (&t,
  result = cp_walk_tree (&t,
                         for_each_template_parm_r,
                         for_each_template_parm_r,
                         &pfd,
                         &pfd,
                         pfd.visited) != NULL_TREE;
                         pfd.visited) != NULL_TREE;
 
 
  /* Clean up.  */
  /* Clean up.  */
  if (!visited)
  if (!visited)
    {
    {
      pointer_set_destroy (pfd.visited);
      pointer_set_destroy (pfd.visited);
      pfd.visited = 0;
      pfd.visited = 0;
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Returns true if T depends on any template parameter.  */
/* Returns true if T depends on any template parameter.  */
 
 
int
int
uses_template_parms (tree t)
uses_template_parms (tree t)
{
{
  bool dependent_p;
  bool dependent_p;
  int saved_processing_template_decl;
  int saved_processing_template_decl;
 
 
  saved_processing_template_decl = processing_template_decl;
  saved_processing_template_decl = processing_template_decl;
  if (!saved_processing_template_decl)
  if (!saved_processing_template_decl)
    processing_template_decl = 1;
    processing_template_decl = 1;
  if (TYPE_P (t))
  if (TYPE_P (t))
    dependent_p = dependent_type_p (t);
    dependent_p = dependent_type_p (t);
  else if (TREE_CODE (t) == TREE_VEC)
  else if (TREE_CODE (t) == TREE_VEC)
    dependent_p = any_dependent_template_arguments_p (t);
    dependent_p = any_dependent_template_arguments_p (t);
  else if (TREE_CODE (t) == TREE_LIST)
  else if (TREE_CODE (t) == TREE_LIST)
    dependent_p = (uses_template_parms (TREE_VALUE (t))
    dependent_p = (uses_template_parms (TREE_VALUE (t))
                   || uses_template_parms (TREE_CHAIN (t)));
                   || uses_template_parms (TREE_CHAIN (t)));
  else if (TREE_CODE (t) == TYPE_DECL)
  else if (TREE_CODE (t) == TYPE_DECL)
    dependent_p = dependent_type_p (TREE_TYPE (t));
    dependent_p = dependent_type_p (TREE_TYPE (t));
  else if (DECL_P (t)
  else if (DECL_P (t)
           || EXPR_P (t)
           || EXPR_P (t)
           || TREE_CODE (t) == TEMPLATE_PARM_INDEX
           || TREE_CODE (t) == TEMPLATE_PARM_INDEX
           || TREE_CODE (t) == OVERLOAD
           || TREE_CODE (t) == OVERLOAD
           || TREE_CODE (t) == BASELINK
           || TREE_CODE (t) == BASELINK
           || TREE_CODE (t) == IDENTIFIER_NODE
           || TREE_CODE (t) == IDENTIFIER_NODE
           || TREE_CODE (t) == TRAIT_EXPR
           || TREE_CODE (t) == TRAIT_EXPR
           || TREE_CODE (t) == CONSTRUCTOR
           || TREE_CODE (t) == CONSTRUCTOR
           || CONSTANT_CLASS_P (t))
           || CONSTANT_CLASS_P (t))
    dependent_p = (type_dependent_expression_p (t)
    dependent_p = (type_dependent_expression_p (t)
                   || value_dependent_expression_p (t));
                   || value_dependent_expression_p (t));
  else
  else
    {
    {
      gcc_assert (t == error_mark_node);
      gcc_assert (t == error_mark_node);
      dependent_p = false;
      dependent_p = false;
    }
    }
 
 
  processing_template_decl = saved_processing_template_decl;
  processing_template_decl = saved_processing_template_decl;
 
 
  return dependent_p;
  return dependent_p;
}
}
 
 
/* Returns true if T depends on any template parameter with level LEVEL.  */
/* Returns true if T depends on any template parameter with level LEVEL.  */
 
 
int
int
uses_template_parms_level (tree t, int level)
uses_template_parms_level (tree t, int level)
{
{
  return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
  return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
                                 /*include_nondeduced_p=*/true);
                                 /*include_nondeduced_p=*/true);
}
}
 
 
static int tinst_depth;
static int tinst_depth;
extern int max_tinst_depth;
extern int max_tinst_depth;
#ifdef GATHER_STATISTICS
#ifdef GATHER_STATISTICS
int depth_reached;
int depth_reached;
#endif
#endif
static int tinst_level_tick;
static int tinst_level_tick;
static int last_template_error_tick;
static int last_template_error_tick;
 
 
/* We're starting to instantiate D; record the template instantiation context
/* We're starting to instantiate D; record the template instantiation context
   for diagnostics and to restore it later.  */
   for diagnostics and to restore it later.  */
 
 
int
int
push_tinst_level (tree d)
push_tinst_level (tree d)
{
{
  struct tinst_level *new_level;
  struct tinst_level *new_level;
 
 
  if (tinst_depth >= max_tinst_depth)
  if (tinst_depth >= max_tinst_depth)
    {
    {
      /* If the instantiation in question still has unbound template parms,
      /* If the instantiation in question still has unbound template parms,
         we don't really care if we can't instantiate it, so just return.
         we don't really care if we can't instantiate it, so just return.
         This happens with base instantiation for implicit `typename'.  */
         This happens with base instantiation for implicit `typename'.  */
      if (uses_template_parms (d))
      if (uses_template_parms (d))
        return 0;
        return 0;
 
 
      last_template_error_tick = tinst_level_tick;
      last_template_error_tick = tinst_level_tick;
      error ("template instantiation depth exceeds maximum of %d (use "
      error ("template instantiation depth exceeds maximum of %d (use "
             "-ftemplate-depth= to increase the maximum) instantiating %qD",
             "-ftemplate-depth= to increase the maximum) instantiating %qD",
             max_tinst_depth, d);
             max_tinst_depth, d);
 
 
      print_instantiation_context ();
      print_instantiation_context ();
 
 
      return 0;
      return 0;
    }
    }
 
 
  new_level = GGC_NEW (struct tinst_level);
  new_level = GGC_NEW (struct tinst_level);
  new_level->decl = d;
  new_level->decl = d;
  new_level->locus = input_location;
  new_level->locus = input_location;
  new_level->in_system_header_p = in_system_header;
  new_level->in_system_header_p = in_system_header;
  new_level->next = current_tinst_level;
  new_level->next = current_tinst_level;
  current_tinst_level = new_level;
  current_tinst_level = new_level;
 
 
  ++tinst_depth;
  ++tinst_depth;
#ifdef GATHER_STATISTICS
#ifdef GATHER_STATISTICS
  if (tinst_depth > depth_reached)
  if (tinst_depth > depth_reached)
    depth_reached = tinst_depth;
    depth_reached = tinst_depth;
#endif
#endif
 
 
  ++tinst_level_tick;
  ++tinst_level_tick;
  return 1;
  return 1;
}
}
 
 
/* We're done instantiating this template; return to the instantiation
/* We're done instantiating this template; return to the instantiation
   context.  */
   context.  */
 
 
void
void
pop_tinst_level (void)
pop_tinst_level (void)
{
{
  /* Restore the filename and line number stashed away when we started
  /* Restore the filename and line number stashed away when we started
     this instantiation.  */
     this instantiation.  */
  input_location = current_tinst_level->locus;
  input_location = current_tinst_level->locus;
  current_tinst_level = current_tinst_level->next;
  current_tinst_level = current_tinst_level->next;
  --tinst_depth;
  --tinst_depth;
  ++tinst_level_tick;
  ++tinst_level_tick;
}
}
 
 
/* We're instantiating a deferred template; restore the template
/* We're instantiating a deferred template; restore the template
   instantiation context in which the instantiation was requested, which
   instantiation context in which the instantiation was requested, which
   is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
 
 
static tree
static tree
reopen_tinst_level (struct tinst_level *level)
reopen_tinst_level (struct tinst_level *level)
{
{
  struct tinst_level *t;
  struct tinst_level *t;
 
 
  tinst_depth = 0;
  tinst_depth = 0;
  for (t = level; t; t = t->next)
  for (t = level; t; t = t->next)
    ++tinst_depth;
    ++tinst_depth;
 
 
  current_tinst_level = level;
  current_tinst_level = level;
  pop_tinst_level ();
  pop_tinst_level ();
  return level->decl;
  return level->decl;
}
}
 
 
/* Returns the TINST_LEVEL which gives the original instantiation
/* Returns the TINST_LEVEL which gives the original instantiation
   context.  */
   context.  */
 
 
struct tinst_level *
struct tinst_level *
outermost_tinst_level (void)
outermost_tinst_level (void)
{
{
  struct tinst_level *level = current_tinst_level;
  struct tinst_level *level = current_tinst_level;
  if (level)
  if (level)
    while (level->next)
    while (level->next)
      level = level->next;
      level = level->next;
  return level;
  return level;
}
}
 
 
/* Returns TRUE if PARM is a parameter of the template TEMPL.  */
/* Returns TRUE if PARM is a parameter of the template TEMPL.  */
 
 
bool
bool
parameter_of_template_p (tree parm, tree templ)
parameter_of_template_p (tree parm, tree templ)
{
{
  tree parms;
  tree parms;
  int i;
  int i;
 
 
  if (!parm || !templ)
  if (!parm || !templ)
    return false;
    return false;
 
 
  gcc_assert (DECL_TEMPLATE_PARM_P (parm));
  gcc_assert (DECL_TEMPLATE_PARM_P (parm));
  gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
  gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
 
 
  parms = DECL_TEMPLATE_PARMS (templ);
  parms = DECL_TEMPLATE_PARMS (templ);
  parms = INNERMOST_TEMPLATE_PARMS (parms);
  parms = INNERMOST_TEMPLATE_PARMS (parms);
 
 
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
    if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
    if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
      return true;
      return true;
 
 
  return false;
  return false;
}
}
 
 
/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   vector of template arguments, as for tsubst.
   vector of template arguments, as for tsubst.
 
 
   Returns an appropriate tsubst'd friend declaration.  */
   Returns an appropriate tsubst'd friend declaration.  */
 
 
static tree
static tree
tsubst_friend_function (tree decl, tree args)
tsubst_friend_function (tree decl, tree args)
{
{
  tree new_friend;
  tree new_friend;
 
 
  if (TREE_CODE (decl) == FUNCTION_DECL
  if (TREE_CODE (decl) == FUNCTION_DECL
      && DECL_TEMPLATE_INSTANTIATION (decl)
      && DECL_TEMPLATE_INSTANTIATION (decl)
      && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
      && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
    /* This was a friend declared with an explicit template
    /* This was a friend declared with an explicit template
       argument list, e.g.:
       argument list, e.g.:
 
 
       friend void f<>(T);
       friend void f<>(T);
 
 
       to indicate that f was a template instantiation, not a new
       to indicate that f was a template instantiation, not a new
       function declaration.  Now, we have to figure out what
       function declaration.  Now, we have to figure out what
       instantiation of what template.  */
       instantiation of what template.  */
    {
    {
      tree template_id, arglist, fns;
      tree template_id, arglist, fns;
      tree new_args;
      tree new_args;
      tree tmpl;
      tree tmpl;
      tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
      tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
 
 
      /* Friend functions are looked up in the containing namespace scope.
      /* Friend functions are looked up in the containing namespace scope.
         We must enter that scope, to avoid finding member functions of the
         We must enter that scope, to avoid finding member functions of the
         current class with same name.  */
         current class with same name.  */
      push_nested_namespace (ns);
      push_nested_namespace (ns);
      fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
      fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
                         tf_warning_or_error, NULL_TREE,
                         tf_warning_or_error, NULL_TREE,
                         /*integral_constant_expression_p=*/false);
                         /*integral_constant_expression_p=*/false);
      pop_nested_namespace (ns);
      pop_nested_namespace (ns);
      arglist = tsubst (DECL_TI_ARGS (decl), args,
      arglist = tsubst (DECL_TI_ARGS (decl), args,
                        tf_warning_or_error, NULL_TREE);
                        tf_warning_or_error, NULL_TREE);
      template_id = lookup_template_function (fns, arglist);
      template_id = lookup_template_function (fns, arglist);
 
 
      new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
      new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
      tmpl = determine_specialization (template_id, new_friend,
      tmpl = determine_specialization (template_id, new_friend,
                                       &new_args,
                                       &new_args,
                                       /*need_member_template=*/0,
                                       /*need_member_template=*/0,
                                       TREE_VEC_LENGTH (args),
                                       TREE_VEC_LENGTH (args),
                                       tsk_none);
                                       tsk_none);
      return instantiate_template (tmpl, new_args, tf_error);
      return instantiate_template (tmpl, new_args, tf_error);
    }
    }
 
 
  new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
  new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
 
 
  /* The NEW_FRIEND will look like an instantiation, to the
  /* The NEW_FRIEND will look like an instantiation, to the
     compiler, but is not an instantiation from the point of view of
     compiler, but is not an instantiation from the point of view of
     the language.  For example, we might have had:
     the language.  For example, we might have had:
 
 
     template <class T> struct S {
     template <class T> struct S {
       template <class U> friend void f(T, U);
       template <class U> friend void f(T, U);
     };
     };
 
 
     Then, in S<int>, template <class U> void f(int, U) is not an
     Then, in S<int>, template <class U> void f(int, U) is not an
     instantiation of anything.  */
     instantiation of anything.  */
  if (new_friend == error_mark_node)
  if (new_friend == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  DECL_USE_TEMPLATE (new_friend) = 0;
  DECL_USE_TEMPLATE (new_friend) = 0;
  if (TREE_CODE (decl) == TEMPLATE_DECL)
  if (TREE_CODE (decl) == TEMPLATE_DECL)
    {
    {
      DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
      DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
      DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
      DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
        = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
        = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
    }
    }
 
 
  /* The mangled name for the NEW_FRIEND is incorrect.  The function
  /* The mangled name for the NEW_FRIEND is incorrect.  The function
     is not a template instantiation and should not be mangled like
     is not a template instantiation and should not be mangled like
     one.  Therefore, we forget the mangling here; we'll recompute it
     one.  Therefore, we forget the mangling here; we'll recompute it
     later if we need it.  */
     later if we need it.  */
  if (TREE_CODE (new_friend) != TEMPLATE_DECL)
  if (TREE_CODE (new_friend) != TEMPLATE_DECL)
    {
    {
      SET_DECL_RTL (new_friend, NULL_RTX);
      SET_DECL_RTL (new_friend, NULL_RTX);
      SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
      SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
    }
    }
 
 
  if (DECL_NAMESPACE_SCOPE_P (new_friend))
  if (DECL_NAMESPACE_SCOPE_P (new_friend))
    {
    {
      tree old_decl;
      tree old_decl;
      tree new_friend_template_info;
      tree new_friend_template_info;
      tree new_friend_result_template_info;
      tree new_friend_result_template_info;
      tree ns;
      tree ns;
      int  new_friend_is_defn;
      int  new_friend_is_defn;
 
 
      /* We must save some information from NEW_FRIEND before calling
      /* We must save some information from NEW_FRIEND before calling
         duplicate decls since that function will free NEW_FRIEND if
         duplicate decls since that function will free NEW_FRIEND if
         possible.  */
         possible.  */
      new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
      new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
      new_friend_is_defn =
      new_friend_is_defn =
            (DECL_INITIAL (DECL_TEMPLATE_RESULT
            (DECL_INITIAL (DECL_TEMPLATE_RESULT
                           (template_for_substitution (new_friend)))
                           (template_for_substitution (new_friend)))
             != NULL_TREE);
             != NULL_TREE);
      if (TREE_CODE (new_friend) == TEMPLATE_DECL)
      if (TREE_CODE (new_friend) == TEMPLATE_DECL)
        {
        {
          /* This declaration is a `primary' template.  */
          /* This declaration is a `primary' template.  */
          DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
          DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
 
 
          new_friend_result_template_info
          new_friend_result_template_info
            = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
            = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
        }
        }
      else
      else
        new_friend_result_template_info = NULL_TREE;
        new_friend_result_template_info = NULL_TREE;
 
 
      /* Make the init_value nonzero so pushdecl knows this is a defn.  */
      /* Make the init_value nonzero so pushdecl knows this is a defn.  */
      if (new_friend_is_defn)
      if (new_friend_is_defn)
        DECL_INITIAL (new_friend) = error_mark_node;
        DECL_INITIAL (new_friend) = error_mark_node;
 
 
      /* Inside pushdecl_namespace_level, we will push into the
      /* Inside pushdecl_namespace_level, we will push into the
         current namespace. However, the friend function should go
         current namespace. However, the friend function should go
         into the namespace of the template.  */
         into the namespace of the template.  */
      ns = decl_namespace_context (new_friend);
      ns = decl_namespace_context (new_friend);
      push_nested_namespace (ns);
      push_nested_namespace (ns);
      old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
      old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
      pop_nested_namespace (ns);
      pop_nested_namespace (ns);
 
 
      if (old_decl == error_mark_node)
      if (old_decl == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      if (old_decl != new_friend)
      if (old_decl != new_friend)
        {
        {
          /* This new friend declaration matched an existing
          /* This new friend declaration matched an existing
             declaration.  For example, given:
             declaration.  For example, given:
 
 
               template <class T> void f(T);
               template <class T> void f(T);
               template <class U> class C {
               template <class U> class C {
                 template <class T> friend void f(T) {}
                 template <class T> friend void f(T) {}
               };
               };
 
 
             the friend declaration actually provides the definition
             the friend declaration actually provides the definition
             of `f', once C has been instantiated for some type.  So,
             of `f', once C has been instantiated for some type.  So,
             old_decl will be the out-of-class template declaration,
             old_decl will be the out-of-class template declaration,
             while new_friend is the in-class definition.
             while new_friend is the in-class definition.
 
 
             But, if `f' was called before this point, the
             But, if `f' was called before this point, the
             instantiation of `f' will have DECL_TI_ARGS corresponding
             instantiation of `f' will have DECL_TI_ARGS corresponding
             to `T' but not to `U', references to which might appear
             to `T' but not to `U', references to which might appear
             in the definition of `f'.  Previously, the most general
             in the definition of `f'.  Previously, the most general
             template for an instantiation of `f' was the out-of-class
             template for an instantiation of `f' was the out-of-class
             version; now it is the in-class version.  Therefore, we
             version; now it is the in-class version.  Therefore, we
             run through all specialization of `f', adding to their
             run through all specialization of `f', adding to their
             DECL_TI_ARGS appropriately.  In particular, they need a
             DECL_TI_ARGS appropriately.  In particular, they need a
             new set of outer arguments, corresponding to the
             new set of outer arguments, corresponding to the
             arguments for this class instantiation.
             arguments for this class instantiation.
 
 
             The same situation can arise with something like this:
             The same situation can arise with something like this:
 
 
               friend void f(int);
               friend void f(int);
               template <class T> class C {
               template <class T> class C {
                 friend void f(T) {}
                 friend void f(T) {}
               };
               };
 
 
             when `C<int>' is instantiated.  Now, `f(int)' is defined
             when `C<int>' is instantiated.  Now, `f(int)' is defined
             in the class.  */
             in the class.  */
 
 
          if (!new_friend_is_defn)
          if (!new_friend_is_defn)
            /* On the other hand, if the in-class declaration does
            /* On the other hand, if the in-class declaration does
               *not* provide a definition, then we don't want to alter
               *not* provide a definition, then we don't want to alter
               existing definitions.  We can just leave everything
               existing definitions.  We can just leave everything
               alone.  */
               alone.  */
            ;
            ;
          else
          else
            {
            {
              tree new_template = TI_TEMPLATE (new_friend_template_info);
              tree new_template = TI_TEMPLATE (new_friend_template_info);
              tree new_args = TI_ARGS (new_friend_template_info);
              tree new_args = TI_ARGS (new_friend_template_info);
 
 
              /* Overwrite whatever template info was there before, if
              /* Overwrite whatever template info was there before, if
                 any, with the new template information pertaining to
                 any, with the new template information pertaining to
                 the declaration.  */
                 the declaration.  */
              DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
              DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
 
 
              if (TREE_CODE (old_decl) != TEMPLATE_DECL)
              if (TREE_CODE (old_decl) != TEMPLATE_DECL)
                /* We should have called reregister_specialization in
                /* We should have called reregister_specialization in
                   duplicate_decls.  */
                   duplicate_decls.  */
                gcc_assert (retrieve_specialization (new_template,
                gcc_assert (retrieve_specialization (new_template,
                                                     new_args, 0)
                                                     new_args, 0)
                            == old_decl);
                            == old_decl);
              else
              else
                {
                {
                  tree t;
                  tree t;
 
 
                  /* Indicate that the old function template is a partial
                  /* Indicate that the old function template is a partial
                     instantiation.  */
                     instantiation.  */
                  DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
                  DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
                    = new_friend_result_template_info;
                    = new_friend_result_template_info;
 
 
                  gcc_assert (new_template
                  gcc_assert (new_template
                              == most_general_template (new_template));
                              == most_general_template (new_template));
                  gcc_assert (new_template != old_decl);
                  gcc_assert (new_template != old_decl);
 
 
                  /* Reassign any specializations already in the hash table
                  /* Reassign any specializations already in the hash table
                     to the new more general template, and add the
                     to the new more general template, and add the
                     additional template args.  */
                     additional template args.  */
                  for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
                  for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
                       t != NULL_TREE;
                       t != NULL_TREE;
                       t = TREE_CHAIN (t))
                       t = TREE_CHAIN (t))
                    {
                    {
                      tree spec = TREE_VALUE (t);
                      tree spec = TREE_VALUE (t);
                      spec_entry elt;
                      spec_entry elt;
 
 
                      elt.tmpl = old_decl;
                      elt.tmpl = old_decl;
                      elt.args = DECL_TI_ARGS (spec);
                      elt.args = DECL_TI_ARGS (spec);
                      elt.spec = NULL_TREE;
                      elt.spec = NULL_TREE;
 
 
                      htab_remove_elt (decl_specializations, &elt);
                      htab_remove_elt (decl_specializations, &elt);
 
 
                      DECL_TI_ARGS (spec)
                      DECL_TI_ARGS (spec)
                        = add_outermost_template_args (new_args,
                        = add_outermost_template_args (new_args,
                                                       DECL_TI_ARGS (spec));
                                                       DECL_TI_ARGS (spec));
 
 
                      register_specialization
                      register_specialization
                        (spec, new_template, DECL_TI_ARGS (spec), true, 0);
                        (spec, new_template, DECL_TI_ARGS (spec), true, 0);
 
 
                    }
                    }
                  DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
                  DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
                }
                }
            }
            }
 
 
          /* The information from NEW_FRIEND has been merged into OLD_DECL
          /* The information from NEW_FRIEND has been merged into OLD_DECL
             by duplicate_decls.  */
             by duplicate_decls.  */
          new_friend = old_decl;
          new_friend = old_decl;
        }
        }
    }
    }
  else
  else
    {
    {
      tree context = DECL_CONTEXT (new_friend);
      tree context = DECL_CONTEXT (new_friend);
      bool dependent_p;
      bool dependent_p;
 
 
      /* In the code
      /* In the code
           template <class T> class C {
           template <class T> class C {
             template <class U> friend void C1<U>::f (); // case 1
             template <class U> friend void C1<U>::f (); // case 1
             friend void C2<T>::f ();                    // case 2
             friend void C2<T>::f ();                    // case 2
           };
           };
         we only need to make sure CONTEXT is a complete type for
         we only need to make sure CONTEXT is a complete type for
         case 2.  To distinguish between the two cases, we note that
         case 2.  To distinguish between the two cases, we note that
         CONTEXT of case 1 remains dependent type after tsubst while
         CONTEXT of case 1 remains dependent type after tsubst while
         this isn't true for case 2.  */
         this isn't true for case 2.  */
      ++processing_template_decl;
      ++processing_template_decl;
      dependent_p = dependent_type_p (context);
      dependent_p = dependent_type_p (context);
      --processing_template_decl;
      --processing_template_decl;
 
 
      if (!dependent_p
      if (!dependent_p
          && !complete_type_or_else (context, NULL_TREE))
          && !complete_type_or_else (context, NULL_TREE))
        return error_mark_node;
        return error_mark_node;
 
 
      if (COMPLETE_TYPE_P (context))
      if (COMPLETE_TYPE_P (context))
        {
        {
          /* Check to see that the declaration is really present, and,
          /* Check to see that the declaration is really present, and,
             possibly obtain an improved declaration.  */
             possibly obtain an improved declaration.  */
          tree fn = check_classfn (context,
          tree fn = check_classfn (context,
                                   new_friend, NULL_TREE);
                                   new_friend, NULL_TREE);
 
 
          if (fn)
          if (fn)
            new_friend = fn;
            new_friend = fn;
        }
        }
    }
    }
 
 
  return new_friend;
  return new_friend;
}
}
 
 
/* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
/* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   template arguments, as for tsubst.
   template arguments, as for tsubst.
 
 
   Returns an appropriate tsubst'd friend type or error_mark_node on
   Returns an appropriate tsubst'd friend type or error_mark_node on
   failure.  */
   failure.  */
 
 
static tree
static tree
tsubst_friend_class (tree friend_tmpl, tree args)
tsubst_friend_class (tree friend_tmpl, tree args)
{
{
  tree friend_type;
  tree friend_type;
  tree tmpl;
  tree tmpl;
  tree context;
  tree context;
 
 
  context = DECL_CONTEXT (friend_tmpl);
  context = DECL_CONTEXT (friend_tmpl);
 
 
  if (context)
  if (context)
    {
    {
      if (TREE_CODE (context) == NAMESPACE_DECL)
      if (TREE_CODE (context) == NAMESPACE_DECL)
        push_nested_namespace (context);
        push_nested_namespace (context);
      else
      else
        push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
        push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
    }
    }
 
 
  /* Look for a class template declaration.  We look for hidden names
  /* Look for a class template declaration.  We look for hidden names
     because two friend declarations of the same template are the
     because two friend declarations of the same template are the
     same.  For example, in:
     same.  For example, in:
 
 
       struct A {
       struct A {
         template <typename> friend class F;
         template <typename> friend class F;
       };
       };
       template <typename> struct B {
       template <typename> struct B {
         template <typename> friend class F;
         template <typename> friend class F;
       };
       };
 
 
     both F templates are the same.  */
     both F templates are the same.  */
  tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
  tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
                           /*block_p=*/true, 0,
                           /*block_p=*/true, 0,
                           LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
                           LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
 
 
  /* But, if we don't find one, it might be because we're in a
  /* But, if we don't find one, it might be because we're in a
     situation like this:
     situation like this:
 
 
       template <class T>
       template <class T>
       struct S {
       struct S {
         template <class U>
         template <class U>
         friend struct S;
         friend struct S;
       };
       };
 
 
     Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
     Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
     for `S<int>', not the TEMPLATE_DECL.  */
     for `S<int>', not the TEMPLATE_DECL.  */
  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
    {
    {
      tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
      tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
      tmpl = maybe_get_template_decl_from_type_decl (tmpl);
      tmpl = maybe_get_template_decl_from_type_decl (tmpl);
    }
    }
 
 
  if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
  if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
    {
    {
      /* The friend template has already been declared.  Just
      /* The friend template has already been declared.  Just
         check to see that the declarations match, and install any new
         check to see that the declarations match, and install any new
         default parameters.  We must tsubst the default parameters,
         default parameters.  We must tsubst the default parameters,
         of course.  We only need the innermost template parameters
         of course.  We only need the innermost template parameters
         because that is all that redeclare_class_template will look
         because that is all that redeclare_class_template will look
         at.  */
         at.  */
      if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
      if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
          > TMPL_ARGS_DEPTH (args))
          > TMPL_ARGS_DEPTH (args))
        {
        {
          tree parms;
          tree parms;
          location_t saved_input_location;
          location_t saved_input_location;
          parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
          parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
                                         args, tf_warning_or_error);
                                         args, tf_warning_or_error);
 
 
          saved_input_location = input_location;
          saved_input_location = input_location;
          input_location = DECL_SOURCE_LOCATION (friend_tmpl);
          input_location = DECL_SOURCE_LOCATION (friend_tmpl);
          redeclare_class_template (TREE_TYPE (tmpl), parms);
          redeclare_class_template (TREE_TYPE (tmpl), parms);
          input_location = saved_input_location;
          input_location = saved_input_location;
 
 
        }
        }
 
 
      friend_type = TREE_TYPE (tmpl);
      friend_type = TREE_TYPE (tmpl);
    }
    }
  else
  else
    {
    {
      /* The friend template has not already been declared.  In this
      /* The friend template has not already been declared.  In this
         case, the instantiation of the template class will cause the
         case, the instantiation of the template class will cause the
         injection of this template into the global scope.  */
         injection of this template into the global scope.  */
      tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
      tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
      if (tmpl == error_mark_node)
      if (tmpl == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* The new TMPL is not an instantiation of anything, so we
      /* The new TMPL is not an instantiation of anything, so we
         forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE for
         forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE for
         the new type because that is supposed to be the corresponding
         the new type because that is supposed to be the corresponding
         template decl, i.e., TMPL.  */
         template decl, i.e., TMPL.  */
      DECL_USE_TEMPLATE (tmpl) = 0;
      DECL_USE_TEMPLATE (tmpl) = 0;
      DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
      DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
      CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
      CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
      CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
      CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
        = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
        = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
 
 
      /* Inject this template into the global scope.  */
      /* Inject this template into the global scope.  */
      friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
      friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
    }
    }
 
 
  if (context)
  if (context)
    {
    {
      if (TREE_CODE (context) == NAMESPACE_DECL)
      if (TREE_CODE (context) == NAMESPACE_DECL)
        pop_nested_namespace (context);
        pop_nested_namespace (context);
      else
      else
        pop_nested_class ();
        pop_nested_class ();
    }
    }
 
 
  return friend_type;
  return friend_type;
}
}
 
 
/* Returns zero if TYPE cannot be completed later due to circularity.
/* Returns zero if TYPE cannot be completed later due to circularity.
   Otherwise returns one.  */
   Otherwise returns one.  */
 
 
static int
static int
can_complete_type_without_circularity (tree type)
can_complete_type_without_circularity (tree type)
{
{
  if (type == NULL_TREE || type == error_mark_node)
  if (type == NULL_TREE || type == error_mark_node)
    return 0;
    return 0;
  else if (COMPLETE_TYPE_P (type))
  else if (COMPLETE_TYPE_P (type))
    return 1;
    return 1;
  else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
  else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
    return can_complete_type_without_circularity (TREE_TYPE (type));
    return can_complete_type_without_circularity (TREE_TYPE (type));
  else if (CLASS_TYPE_P (type)
  else if (CLASS_TYPE_P (type)
           && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
           && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
    return 0;
    return 0;
  else
  else
    return 1;
    return 1;
}
}
 
 
/* Apply any attributes which had to be deferred until instantiation
/* Apply any attributes which had to be deferred until instantiation
   time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   ARGS, COMPLAIN, IN_DECL are as tsubst.  */
   ARGS, COMPLAIN, IN_DECL are as tsubst.  */
 
 
static void
static void
apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
                                tree args, tsubst_flags_t complain, tree in_decl)
                                tree args, tsubst_flags_t complain, tree in_decl)
{
{
  tree last_dep = NULL_TREE;
  tree last_dep = NULL_TREE;
  tree t;
  tree t;
  tree *p;
  tree *p;
 
 
  for (t = attributes; t; t = TREE_CHAIN (t))
  for (t = attributes; t; t = TREE_CHAIN (t))
    if (ATTR_IS_DEPENDENT (t))
    if (ATTR_IS_DEPENDENT (t))
      {
      {
        last_dep = t;
        last_dep = t;
        attributes = copy_list (attributes);
        attributes = copy_list (attributes);
        break;
        break;
      }
      }
 
 
  if (DECL_P (*decl_p))
  if (DECL_P (*decl_p))
    {
    {
      if (TREE_TYPE (*decl_p) == error_mark_node)
      if (TREE_TYPE (*decl_p) == error_mark_node)
        return;
        return;
      p = &DECL_ATTRIBUTES (*decl_p);
      p = &DECL_ATTRIBUTES (*decl_p);
    }
    }
  else
  else
    p = &TYPE_ATTRIBUTES (*decl_p);
    p = &TYPE_ATTRIBUTES (*decl_p);
 
 
  if (last_dep)
  if (last_dep)
    {
    {
      tree late_attrs = NULL_TREE;
      tree late_attrs = NULL_TREE;
      tree *q = &late_attrs;
      tree *q = &late_attrs;
 
 
      for (*p = attributes; *p; )
      for (*p = attributes; *p; )
        {
        {
          t = *p;
          t = *p;
          if (ATTR_IS_DEPENDENT (t))
          if (ATTR_IS_DEPENDENT (t))
            {
            {
              *p = TREE_CHAIN (t);
              *p = TREE_CHAIN (t);
              TREE_CHAIN (t) = NULL_TREE;
              TREE_CHAIN (t) = NULL_TREE;
              /* If the first attribute argument is an identifier, don't
              /* If the first attribute argument is an identifier, don't
                 pass it through tsubst.  Attributes like mode, format,
                 pass it through tsubst.  Attributes like mode, format,
                 cleanup and several target specific attributes expect it
                 cleanup and several target specific attributes expect it
                 unmodified.  */
                 unmodified.  */
              if (TREE_VALUE (t)
              if (TREE_VALUE (t)
                  && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
                  && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
                  && TREE_VALUE (TREE_VALUE (t))
                  && TREE_VALUE (TREE_VALUE (t))
                  && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
                  && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
                      == IDENTIFIER_NODE))
                      == IDENTIFIER_NODE))
                {
                {
                  tree chain
                  tree chain
                    = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
                    = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
                                   in_decl,
                                   in_decl,
                                   /*integral_constant_expression_p=*/false);
                                   /*integral_constant_expression_p=*/false);
                  if (chain != TREE_CHAIN (TREE_VALUE (t)))
                  if (chain != TREE_CHAIN (TREE_VALUE (t)))
                    TREE_VALUE (t)
                    TREE_VALUE (t)
                      = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
                      = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
                                   chain);
                                   chain);
                }
                }
              else
              else
                TREE_VALUE (t)
                TREE_VALUE (t)
                  = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
                  = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
                                 /*integral_constant_expression_p=*/false);
                                 /*integral_constant_expression_p=*/false);
              *q = t;
              *q = t;
              q = &TREE_CHAIN (t);
              q = &TREE_CHAIN (t);
            }
            }
          else
          else
            p = &TREE_CHAIN (t);
            p = &TREE_CHAIN (t);
        }
        }
 
 
      cplus_decl_attributes (decl_p, late_attrs, attr_flags);
      cplus_decl_attributes (decl_p, late_attrs, attr_flags);
    }
    }
}
}
 
 
/* Perform (or defer) access check for typedefs that were referenced
/* Perform (or defer) access check for typedefs that were referenced
   from within the template TMPL code.
   from within the template TMPL code.
   This is a subroutine of instantiate_template and instantiate_class_template.
   This is a subroutine of instantiate_template and instantiate_class_template.
   TMPL is the template to consider and TARGS is the list of arguments of
   TMPL is the template to consider and TARGS is the list of arguments of
   that template.  */
   that template.  */
 
 
static void
static void
perform_typedefs_access_check (tree tmpl, tree targs)
perform_typedefs_access_check (tree tmpl, tree targs)
{
{
  location_t saved_location;
  location_t saved_location;
  int i;
  int i;
  qualified_typedef_usage_t *iter;
  qualified_typedef_usage_t *iter;
 
 
  if (!tmpl
  if (!tmpl
      || (!CLASS_TYPE_P (tmpl)
      || (!CLASS_TYPE_P (tmpl)
          && TREE_CODE (tmpl) != FUNCTION_DECL))
          && TREE_CODE (tmpl) != FUNCTION_DECL))
    return;
    return;
 
 
  saved_location = input_location;
  saved_location = input_location;
  for (i = 0;
  for (i = 0;
       VEC_iterate (qualified_typedef_usage_t,
       VEC_iterate (qualified_typedef_usage_t,
                    get_types_needing_access_check (tmpl),
                    get_types_needing_access_check (tmpl),
                    i, iter);
                    i, iter);
        ++i)
        ++i)
    {
    {
      tree type_decl = iter->typedef_decl;
      tree type_decl = iter->typedef_decl;
      tree type_scope = iter->context;
      tree type_scope = iter->context;
 
 
      if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
      if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
        continue;
        continue;
 
 
      if (uses_template_parms (type_decl))
      if (uses_template_parms (type_decl))
        type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
        type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
      if (uses_template_parms (type_scope))
      if (uses_template_parms (type_scope))
        type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
        type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
 
 
      /* Make access check error messages point to the location
      /* Make access check error messages point to the location
         of the use of the typedef.  */
         of the use of the typedef.  */
      input_location = iter->locus;
      input_location = iter->locus;
      perform_or_defer_access_check (TYPE_BINFO (type_scope),
      perform_or_defer_access_check (TYPE_BINFO (type_scope),
                                     type_decl, type_decl);
                                     type_decl, type_decl);
    }
    }
    input_location = saved_location;
    input_location = saved_location;
}
}
 
 
tree
tree
instantiate_class_template (tree type)
instantiate_class_template (tree type)
{
{
  tree templ, args, pattern, t, member;
  tree templ, args, pattern, t, member;
  tree typedecl;
  tree typedecl;
  tree pbinfo;
  tree pbinfo;
  tree base_list;
  tree base_list;
  unsigned int saved_maximum_field_alignment;
  unsigned int saved_maximum_field_alignment;
 
 
  if (type == error_mark_node)
  if (type == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  if (TYPE_BEING_DEFINED (type)
  if (TYPE_BEING_DEFINED (type)
      || COMPLETE_TYPE_P (type)
      || COMPLETE_TYPE_P (type)
      || uses_template_parms (type))
      || uses_template_parms (type))
    return type;
    return type;
 
 
  /* Figure out which template is being instantiated.  */
  /* Figure out which template is being instantiated.  */
  templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
  templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
  gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
  gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
 
 
  /* Determine what specialization of the original template to
  /* Determine what specialization of the original template to
     instantiate.  */
     instantiate.  */
  t = most_specialized_class (type, templ);
  t = most_specialized_class (type, templ);
  if (t == error_mark_node)
  if (t == error_mark_node)
    {
    {
      TYPE_BEING_DEFINED (type) = 1;
      TYPE_BEING_DEFINED (type) = 1;
      return error_mark_node;
      return error_mark_node;
    }
    }
  else if (t)
  else if (t)
    {
    {
      /* This TYPE is actually an instantiation of a partial
      /* This TYPE is actually an instantiation of a partial
         specialization.  We replace the innermost set of ARGS with
         specialization.  We replace the innermost set of ARGS with
         the arguments appropriate for substitution.  For example,
         the arguments appropriate for substitution.  For example,
         given:
         given:
 
 
           template <class T> struct S {};
           template <class T> struct S {};
           template <class T> struct S<T*> {};
           template <class T> struct S<T*> {};
 
 
         and supposing that we are instantiating S<int*>, ARGS will
         and supposing that we are instantiating S<int*>, ARGS will
         presently be {int*} -- but we need {int}.  */
         presently be {int*} -- but we need {int}.  */
      pattern = TREE_TYPE (t);
      pattern = TREE_TYPE (t);
      args = TREE_PURPOSE (t);
      args = TREE_PURPOSE (t);
    }
    }
  else
  else
    {
    {
      pattern = TREE_TYPE (templ);
      pattern = TREE_TYPE (templ);
      args = CLASSTYPE_TI_ARGS (type);
      args = CLASSTYPE_TI_ARGS (type);
    }
    }
 
 
  /* If the template we're instantiating is incomplete, then clearly
  /* If the template we're instantiating is incomplete, then clearly
     there's nothing we can do.  */
     there's nothing we can do.  */
  if (!COMPLETE_TYPE_P (pattern))
  if (!COMPLETE_TYPE_P (pattern))
    return type;
    return type;
 
 
  /* If we've recursively instantiated too many templates, stop.  */
  /* If we've recursively instantiated too many templates, stop.  */
  if (! push_tinst_level (type))
  if (! push_tinst_level (type))
    return type;
    return type;
 
 
  /* Now we're really doing the instantiation.  Mark the type as in
  /* Now we're really doing the instantiation.  Mark the type as in
     the process of being defined.  */
     the process of being defined.  */
  TYPE_BEING_DEFINED (type) = 1;
  TYPE_BEING_DEFINED (type) = 1;
 
 
  /* We may be in the middle of deferred access check.  Disable
  /* We may be in the middle of deferred access check.  Disable
     it now.  */
     it now.  */
  push_deferring_access_checks (dk_no_deferred);
  push_deferring_access_checks (dk_no_deferred);
 
 
  push_to_top_level ();
  push_to_top_level ();
  /* Use #pragma pack from the template context.  */
  /* Use #pragma pack from the template context.  */
  saved_maximum_field_alignment = maximum_field_alignment;
  saved_maximum_field_alignment = maximum_field_alignment;
  maximum_field_alignment = TYPE_PRECISION (pattern);
  maximum_field_alignment = TYPE_PRECISION (pattern);
 
 
  SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
  SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
 
 
  /* Set the input location to the most specialized template definition.
  /* Set the input location to the most specialized template definition.
     This is needed if tsubsting causes an error.  */
     This is needed if tsubsting causes an error.  */
  typedecl = TYPE_MAIN_DECL (pattern);
  typedecl = TYPE_MAIN_DECL (pattern);
  input_location = DECL_SOURCE_LOCATION (typedecl);
  input_location = DECL_SOURCE_LOCATION (typedecl);
 
 
  TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
  TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
  TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
  TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
  TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
  TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
  TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
  TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
  TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
  TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
  TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
  TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
  TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
  TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
  TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
  TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
  TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
  TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
  TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
  TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
  TYPE_PACKED (type) = TYPE_PACKED (pattern);
  TYPE_PACKED (type) = TYPE_PACKED (pattern);
  TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
  TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
  TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
  TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
  TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
  TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
  if (ANON_AGGR_TYPE_P (pattern))
  if (ANON_AGGR_TYPE_P (pattern))
    SET_ANON_AGGR_TYPE_P (type);
    SET_ANON_AGGR_TYPE_P (type);
  if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
  if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
    {
    {
      CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
      CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
      CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
      CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
    }
    }
 
 
  pbinfo = TYPE_BINFO (pattern);
  pbinfo = TYPE_BINFO (pattern);
 
 
  /* We should never instantiate a nested class before its enclosing
  /* We should never instantiate a nested class before its enclosing
     class; we need to look up the nested class by name before we can
     class; we need to look up the nested class by name before we can
     instantiate it, and that lookup should instantiate the enclosing
     instantiate it, and that lookup should instantiate the enclosing
     class.  */
     class.  */
  gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
  gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
              || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
              || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
              || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
              || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
 
 
  base_list = NULL_TREE;
  base_list = NULL_TREE;
  if (BINFO_N_BASE_BINFOS (pbinfo))
  if (BINFO_N_BASE_BINFOS (pbinfo))
    {
    {
      tree pbase_binfo;
      tree pbase_binfo;
      tree context = TYPE_CONTEXT (type);
      tree context = TYPE_CONTEXT (type);
      tree pushed_scope;
      tree pushed_scope;
      int i;
      int i;
 
 
      /* We must enter the scope containing the type, as that is where
      /* We must enter the scope containing the type, as that is where
         the accessibility of types named in dependent bases are
         the accessibility of types named in dependent bases are
         looked up from.  */
         looked up from.  */
      pushed_scope = push_scope (context ? context : global_namespace);
      pushed_scope = push_scope (context ? context : global_namespace);
 
 
      /* Substitute into each of the bases to determine the actual
      /* Substitute into each of the bases to determine the actual
         basetypes.  */
         basetypes.  */
      for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
      for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
        {
        {
          tree base;
          tree base;
          tree access = BINFO_BASE_ACCESS (pbinfo, i);
          tree access = BINFO_BASE_ACCESS (pbinfo, i);
          tree expanded_bases = NULL_TREE;
          tree expanded_bases = NULL_TREE;
          int idx, len = 1;
          int idx, len = 1;
 
 
          if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
          if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
            {
            {
              expanded_bases =
              expanded_bases =
                tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
                tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
                                       args, tf_error, NULL_TREE);
                                       args, tf_error, NULL_TREE);
              if (expanded_bases == error_mark_node)
              if (expanded_bases == error_mark_node)
                continue;
                continue;
 
 
              len = TREE_VEC_LENGTH (expanded_bases);
              len = TREE_VEC_LENGTH (expanded_bases);
            }
            }
 
 
          for (idx = 0; idx < len; idx++)
          for (idx = 0; idx < len; idx++)
            {
            {
              if (expanded_bases)
              if (expanded_bases)
                /* Extract the already-expanded base class.  */
                /* Extract the already-expanded base class.  */
                base = TREE_VEC_ELT (expanded_bases, idx);
                base = TREE_VEC_ELT (expanded_bases, idx);
              else
              else
                /* Substitute to figure out the base class.  */
                /* Substitute to figure out the base class.  */
                base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
                base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
                               NULL_TREE);
                               NULL_TREE);
 
 
              if (base == error_mark_node)
              if (base == error_mark_node)
                continue;
                continue;
 
 
              base_list = tree_cons (access, base, base_list);
              base_list = tree_cons (access, base, base_list);
              if (BINFO_VIRTUAL_P (pbase_binfo))
              if (BINFO_VIRTUAL_P (pbase_binfo))
                TREE_TYPE (base_list) = integer_type_node;
                TREE_TYPE (base_list) = integer_type_node;
            }
            }
        }
        }
 
 
      /* The list is now in reverse order; correct that.  */
      /* The list is now in reverse order; correct that.  */
      base_list = nreverse (base_list);
      base_list = nreverse (base_list);
 
 
      if (pushed_scope)
      if (pushed_scope)
        pop_scope (pushed_scope);
        pop_scope (pushed_scope);
    }
    }
  /* Now call xref_basetypes to set up all the base-class
  /* Now call xref_basetypes to set up all the base-class
     information.  */
     information.  */
  xref_basetypes (type, base_list);
  xref_basetypes (type, base_list);
 
 
  apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
  apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
                                  (int) ATTR_FLAG_TYPE_IN_PLACE,
                                  (int) ATTR_FLAG_TYPE_IN_PLACE,
                                  args, tf_error, NULL_TREE);
                                  args, tf_error, NULL_TREE);
 
 
  /* Now that our base classes are set up, enter the scope of the
  /* Now that our base classes are set up, enter the scope of the
     class, so that name lookups into base classes, etc. will work
     class, so that name lookups into base classes, etc. will work
     correctly.  This is precisely analogous to what we do in
     correctly.  This is precisely analogous to what we do in
     begin_class_definition when defining an ordinary non-template
     begin_class_definition when defining an ordinary non-template
     class, except we also need to push the enclosing classes.  */
     class, except we also need to push the enclosing classes.  */
  push_nested_class (type);
  push_nested_class (type);
 
 
  /* Now members are processed in the order of declaration.  */
  /* Now members are processed in the order of declaration.  */
  for (member = CLASSTYPE_DECL_LIST (pattern);
  for (member = CLASSTYPE_DECL_LIST (pattern);
       member; member = TREE_CHAIN (member))
       member; member = TREE_CHAIN (member))
    {
    {
      tree t = TREE_VALUE (member);
      tree t = TREE_VALUE (member);
 
 
      if (TREE_PURPOSE (member))
      if (TREE_PURPOSE (member))
        {
        {
          if (TYPE_P (t))
          if (TYPE_P (t))
            {
            {
              /* Build new CLASSTYPE_NESTED_UTDS.  */
              /* Build new CLASSTYPE_NESTED_UTDS.  */
 
 
              tree newtag;
              tree newtag;
              bool class_template_p;
              bool class_template_p;
 
 
              class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
              class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
                                  && TYPE_LANG_SPECIFIC (t)
                                  && TYPE_LANG_SPECIFIC (t)
                                  && CLASSTYPE_IS_TEMPLATE (t));
                                  && CLASSTYPE_IS_TEMPLATE (t));
              /* If the member is a class template, then -- even after
              /* If the member is a class template, then -- even after
                 substitution -- there may be dependent types in the
                 substitution -- there may be dependent types in the
                 template argument list for the class.  We increment
                 template argument list for the class.  We increment
                 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
                 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
                 that function will assume that no types are dependent
                 that function will assume that no types are dependent
                 when outside of a template.  */
                 when outside of a template.  */
              if (class_template_p)
              if (class_template_p)
                ++processing_template_decl;
                ++processing_template_decl;
              newtag = tsubst (t, args, tf_error, NULL_TREE);
              newtag = tsubst (t, args, tf_error, NULL_TREE);
              if (class_template_p)
              if (class_template_p)
                --processing_template_decl;
                --processing_template_decl;
              if (newtag == error_mark_node)
              if (newtag == error_mark_node)
                continue;
                continue;
 
 
              if (TREE_CODE (newtag) != ENUMERAL_TYPE)
              if (TREE_CODE (newtag) != ENUMERAL_TYPE)
                {
                {
                  tree name = TYPE_IDENTIFIER (t);
                  tree name = TYPE_IDENTIFIER (t);
 
 
                  if (class_template_p)
                  if (class_template_p)
                    /* Unfortunately, lookup_template_class sets
                    /* Unfortunately, lookup_template_class sets
                       CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
                       CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
                       instantiation (i.e., for the type of a member
                       instantiation (i.e., for the type of a member
                       template class nested within a template class.)
                       template class nested within a template class.)
                       This behavior is required for
                       This behavior is required for
                       maybe_process_partial_specialization to work
                       maybe_process_partial_specialization to work
                       correctly, but is not accurate in this case;
                       correctly, but is not accurate in this case;
                       the TAG is not an instantiation of anything.
                       the TAG is not an instantiation of anything.
                       (The corresponding TEMPLATE_DECL is an
                       (The corresponding TEMPLATE_DECL is an
                       instantiation, but the TYPE is not.) */
                       instantiation, but the TYPE is not.) */
                    CLASSTYPE_USE_TEMPLATE (newtag) = 0;
                    CLASSTYPE_USE_TEMPLATE (newtag) = 0;
 
 
                  /* Now, we call pushtag to put this NEWTAG into the scope of
                  /* Now, we call pushtag to put this NEWTAG into the scope of
                     TYPE.  We first set up the IDENTIFIER_TYPE_VALUE to avoid
                     TYPE.  We first set up the IDENTIFIER_TYPE_VALUE to avoid
                     pushtag calling push_template_decl.  We don't have to do
                     pushtag calling push_template_decl.  We don't have to do
                     this for enums because it will already have been done in
                     this for enums because it will already have been done in
                     tsubst_enum.  */
                     tsubst_enum.  */
                  if (name)
                  if (name)
                    SET_IDENTIFIER_TYPE_VALUE (name, newtag);
                    SET_IDENTIFIER_TYPE_VALUE (name, newtag);
                  pushtag (name, newtag, /*tag_scope=*/ts_current);
                  pushtag (name, newtag, /*tag_scope=*/ts_current);
                }
                }
            }
            }
          else if (TREE_CODE (t) == FUNCTION_DECL
          else if (TREE_CODE (t) == FUNCTION_DECL
                   || DECL_FUNCTION_TEMPLATE_P (t))
                   || DECL_FUNCTION_TEMPLATE_P (t))
            {
            {
              /* Build new TYPE_METHODS.  */
              /* Build new TYPE_METHODS.  */
              tree r;
              tree r;
 
 
              if (TREE_CODE (t) == TEMPLATE_DECL)
              if (TREE_CODE (t) == TEMPLATE_DECL)
                ++processing_template_decl;
                ++processing_template_decl;
              r = tsubst (t, args, tf_error, NULL_TREE);
              r = tsubst (t, args, tf_error, NULL_TREE);
              if (TREE_CODE (t) == TEMPLATE_DECL)
              if (TREE_CODE (t) == TEMPLATE_DECL)
                --processing_template_decl;
                --processing_template_decl;
              set_current_access_from_decl (r);
              set_current_access_from_decl (r);
              finish_member_declaration (r);
              finish_member_declaration (r);
            }
            }
          else
          else
            {
            {
              /* Build new TYPE_FIELDS.  */
              /* Build new TYPE_FIELDS.  */
              if (TREE_CODE (t) == STATIC_ASSERT)
              if (TREE_CODE (t) == STATIC_ASSERT)
                {
                {
                  tree condition =
                  tree condition =
                    tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
                    tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
                                 tf_warning_or_error, NULL_TREE,
                                 tf_warning_or_error, NULL_TREE,
                                 /*integral_constant_expression_p=*/true);
                                 /*integral_constant_expression_p=*/true);
                  finish_static_assert (condition,
                  finish_static_assert (condition,
                                        STATIC_ASSERT_MESSAGE (t),
                                        STATIC_ASSERT_MESSAGE (t),
                                        STATIC_ASSERT_SOURCE_LOCATION (t),
                                        STATIC_ASSERT_SOURCE_LOCATION (t),
                                        /*member_p=*/true);
                                        /*member_p=*/true);
                }
                }
              else if (TREE_CODE (t) != CONST_DECL)
              else if (TREE_CODE (t) != CONST_DECL)
                {
                {
                  tree r;
                  tree r;
 
 
                  /* The file and line for this declaration, to
                  /* The file and line for this declaration, to
                     assist in error message reporting.  Since we
                     assist in error message reporting.  Since we
                     called push_tinst_level above, we don't need to
                     called push_tinst_level above, we don't need to
                     restore these.  */
                     restore these.  */
                  input_location = DECL_SOURCE_LOCATION (t);
                  input_location = DECL_SOURCE_LOCATION (t);
 
 
                  if (TREE_CODE (t) == TEMPLATE_DECL)
                  if (TREE_CODE (t) == TEMPLATE_DECL)
                    ++processing_template_decl;
                    ++processing_template_decl;
                  r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
                  r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
                  if (TREE_CODE (t) == TEMPLATE_DECL)
                  if (TREE_CODE (t) == TEMPLATE_DECL)
                    --processing_template_decl;
                    --processing_template_decl;
                  if (TREE_CODE (r) == VAR_DECL)
                  if (TREE_CODE (r) == VAR_DECL)
                    {
                    {
                      /* In [temp.inst]:
                      /* In [temp.inst]:
 
 
                           [t]he initialization (and any associated
                           [t]he initialization (and any associated
                           side-effects) of a static data member does
                           side-effects) of a static data member does
                           not occur unless the static data member is
                           not occur unless the static data member is
                           itself used in a way that requires the
                           itself used in a way that requires the
                           definition of the static data member to
                           definition of the static data member to
                           exist.
                           exist.
 
 
                         Therefore, we do not substitute into the
                         Therefore, we do not substitute into the
                         initialized for the static data member here.  */
                         initialized for the static data member here.  */
                      finish_static_data_member_decl
                      finish_static_data_member_decl
                        (r,
                        (r,
                         /*init=*/NULL_TREE,
                         /*init=*/NULL_TREE,
                         /*init_const_expr_p=*/false,
                         /*init_const_expr_p=*/false,
                         /*asmspec_tree=*/NULL_TREE,
                         /*asmspec_tree=*/NULL_TREE,
                         /*flags=*/0);
                         /*flags=*/0);
                      if (DECL_INITIALIZED_IN_CLASS_P (r))
                      if (DECL_INITIALIZED_IN_CLASS_P (r))
                        check_static_variable_definition (r, TREE_TYPE (r));
                        check_static_variable_definition (r, TREE_TYPE (r));
                    }
                    }
                  else if (TREE_CODE (r) == FIELD_DECL)
                  else if (TREE_CODE (r) == FIELD_DECL)
                    {
                    {
                      /* Determine whether R has a valid type and can be
                      /* Determine whether R has a valid type and can be
                         completed later.  If R is invalid, then it is
                         completed later.  If R is invalid, then it is
                         replaced by error_mark_node so that it will not be
                         replaced by error_mark_node so that it will not be
                         added to TYPE_FIELDS.  */
                         added to TYPE_FIELDS.  */
                      tree rtype = TREE_TYPE (r);
                      tree rtype = TREE_TYPE (r);
                      if (can_complete_type_without_circularity (rtype))
                      if (can_complete_type_without_circularity (rtype))
                        complete_type (rtype);
                        complete_type (rtype);
 
 
                      if (!COMPLETE_TYPE_P (rtype))
                      if (!COMPLETE_TYPE_P (rtype))
                        {
                        {
                          cxx_incomplete_type_error (r, rtype);
                          cxx_incomplete_type_error (r, rtype);
                          r = error_mark_node;
                          r = error_mark_node;
                        }
                        }
                    }
                    }
 
 
                  /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
                  /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
                     such a thing will already have been added to the field
                     such a thing will already have been added to the field
                     list by tsubst_enum in finish_member_declaration in the
                     list by tsubst_enum in finish_member_declaration in the
                     CLASSTYPE_NESTED_UTDS case above.  */
                     CLASSTYPE_NESTED_UTDS case above.  */
                  if (!(TREE_CODE (r) == TYPE_DECL
                  if (!(TREE_CODE (r) == TYPE_DECL
                        && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
                        && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
                        && DECL_ARTIFICIAL (r)))
                        && DECL_ARTIFICIAL (r)))
                    {
                    {
                      set_current_access_from_decl (r);
                      set_current_access_from_decl (r);
                      finish_member_declaration (r);
                      finish_member_declaration (r);
                    }
                    }
                }
                }
            }
            }
        }
        }
      else
      else
        {
        {
          if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
          if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
            {
            {
              /* Build new CLASSTYPE_FRIEND_CLASSES.  */
              /* Build new CLASSTYPE_FRIEND_CLASSES.  */
 
 
              tree friend_type = t;
              tree friend_type = t;
              bool adjust_processing_template_decl = false;
              bool adjust_processing_template_decl = false;
 
 
              if (TREE_CODE (friend_type) == TEMPLATE_DECL)
              if (TREE_CODE (friend_type) == TEMPLATE_DECL)
                {
                {
                  /* template <class T> friend class C;  */
                  /* template <class T> friend class C;  */
                  friend_type = tsubst_friend_class (friend_type, args);
                  friend_type = tsubst_friend_class (friend_type, args);
                  adjust_processing_template_decl = true;
                  adjust_processing_template_decl = true;
                }
                }
              else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
              else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
                {
                {
                  /* template <class T> friend class C::D;  */
                  /* template <class T> friend class C::D;  */
                  friend_type = tsubst (friend_type, args,
                  friend_type = tsubst (friend_type, args,
                                        tf_warning_or_error, NULL_TREE);
                                        tf_warning_or_error, NULL_TREE);
                  if (TREE_CODE (friend_type) == TEMPLATE_DECL)
                  if (TREE_CODE (friend_type) == TEMPLATE_DECL)
                    friend_type = TREE_TYPE (friend_type);
                    friend_type = TREE_TYPE (friend_type);
                  adjust_processing_template_decl = true;
                  adjust_processing_template_decl = true;
                }
                }
              else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
              else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
                {
                {
                  /* This could be either
                  /* This could be either
 
 
                       friend class T::C;
                       friend class T::C;
 
 
                     when dependent_type_p is false or
                     when dependent_type_p is false or
 
 
                       template <class U> friend class T::C;
                       template <class U> friend class T::C;
 
 
                     otherwise.  */
                     otherwise.  */
                  friend_type = tsubst (friend_type, args,
                  friend_type = tsubst (friend_type, args,
                                        tf_warning_or_error, NULL_TREE);
                                        tf_warning_or_error, NULL_TREE);
                  /* Bump processing_template_decl for correct
                  /* Bump processing_template_decl for correct
                     dependent_type_p calculation.  */
                     dependent_type_p calculation.  */
                  ++processing_template_decl;
                  ++processing_template_decl;
                  if (dependent_type_p (friend_type))
                  if (dependent_type_p (friend_type))
                    adjust_processing_template_decl = true;
                    adjust_processing_template_decl = true;
                  --processing_template_decl;
                  --processing_template_decl;
                }
                }
              else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
              else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
                       && hidden_name_p (TYPE_NAME (friend_type)))
                       && hidden_name_p (TYPE_NAME (friend_type)))
                {
                {
                  /* friend class C;
                  /* friend class C;
 
 
                     where C hasn't been declared yet.  Let's lookup name
                     where C hasn't been declared yet.  Let's lookup name
                     from namespace scope directly, bypassing any name that
                     from namespace scope directly, bypassing any name that
                     come from dependent base class.  */
                     come from dependent base class.  */
                  tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
                  tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
 
 
                  /* The call to xref_tag_from_type does injection for friend
                  /* The call to xref_tag_from_type does injection for friend
                     classes.  */
                     classes.  */
                  push_nested_namespace (ns);
                  push_nested_namespace (ns);
                  friend_type =
                  friend_type =
                    xref_tag_from_type (friend_type, NULL_TREE,
                    xref_tag_from_type (friend_type, NULL_TREE,
                                        /*tag_scope=*/ts_current);
                                        /*tag_scope=*/ts_current);
                  pop_nested_namespace (ns);
                  pop_nested_namespace (ns);
                }
                }
              else if (uses_template_parms (friend_type))
              else if (uses_template_parms (friend_type))
                /* friend class C<T>;  */
                /* friend class C<T>;  */
                friend_type = tsubst (friend_type, args,
                friend_type = tsubst (friend_type, args,
                                      tf_warning_or_error, NULL_TREE);
                                      tf_warning_or_error, NULL_TREE);
              /* Otherwise it's
              /* Otherwise it's
 
 
                   friend class C;
                   friend class C;
 
 
                 where C is already declared or
                 where C is already declared or
 
 
                   friend class C<int>;
                   friend class C<int>;
 
 
                 We don't have to do anything in these cases.  */
                 We don't have to do anything in these cases.  */
 
 
              if (adjust_processing_template_decl)
              if (adjust_processing_template_decl)
                /* Trick make_friend_class into realizing that the friend
                /* Trick make_friend_class into realizing that the friend
                   we're adding is a template, not an ordinary class.  It's
                   we're adding is a template, not an ordinary class.  It's
                   important that we use make_friend_class since it will
                   important that we use make_friend_class since it will
                   perform some error-checking and output cross-reference
                   perform some error-checking and output cross-reference
                   information.  */
                   information.  */
                ++processing_template_decl;
                ++processing_template_decl;
 
 
              if (friend_type != error_mark_node)
              if (friend_type != error_mark_node)
                make_friend_class (type, friend_type, /*complain=*/false);
                make_friend_class (type, friend_type, /*complain=*/false);
 
 
              if (adjust_processing_template_decl)
              if (adjust_processing_template_decl)
                --processing_template_decl;
                --processing_template_decl;
            }
            }
          else
          else
            {
            {
              /* Build new DECL_FRIENDLIST.  */
              /* Build new DECL_FRIENDLIST.  */
              tree r;
              tree r;
 
 
              /* The file and line for this declaration, to
              /* The file and line for this declaration, to
                 assist in error message reporting.  Since we
                 assist in error message reporting.  Since we
                 called push_tinst_level above, we don't need to
                 called push_tinst_level above, we don't need to
                 restore these.  */
                 restore these.  */
              input_location = DECL_SOURCE_LOCATION (t);
              input_location = DECL_SOURCE_LOCATION (t);
 
 
              if (TREE_CODE (t) == TEMPLATE_DECL)
              if (TREE_CODE (t) == TEMPLATE_DECL)
                {
                {
                  ++processing_template_decl;
                  ++processing_template_decl;
                  push_deferring_access_checks (dk_no_check);
                  push_deferring_access_checks (dk_no_check);
                }
                }
 
 
              r = tsubst_friend_function (t, args);
              r = tsubst_friend_function (t, args);
              add_friend (type, r, /*complain=*/false);
              add_friend (type, r, /*complain=*/false);
              if (TREE_CODE (t) == TEMPLATE_DECL)
              if (TREE_CODE (t) == TEMPLATE_DECL)
                {
                {
                  pop_deferring_access_checks ();
                  pop_deferring_access_checks ();
                  --processing_template_decl;
                  --processing_template_decl;
                }
                }
            }
            }
        }
        }
    }
    }
 
 
  /* Set the file and line number information to whatever is given for
  /* Set the file and line number information to whatever is given for
     the class itself.  This puts error messages involving generated
     the class itself.  This puts error messages involving generated
     implicit functions at a predictable point, and the same point
     implicit functions at a predictable point, and the same point
     that would be used for non-template classes.  */
     that would be used for non-template classes.  */
  input_location = DECL_SOURCE_LOCATION (typedecl);
  input_location = DECL_SOURCE_LOCATION (typedecl);
 
 
  unreverse_member_declarations (type);
  unreverse_member_declarations (type);
  finish_struct_1 (type);
  finish_struct_1 (type);
  TYPE_BEING_DEFINED (type) = 0;
  TYPE_BEING_DEFINED (type) = 0;
 
 
  /* Now that the class is complete, instantiate default arguments for
  /* Now that the class is complete, instantiate default arguments for
     any member functions.  We don't do this earlier because the
     any member functions.  We don't do this earlier because the
     default arguments may reference members of the class.  */
     default arguments may reference members of the class.  */
  if (!PRIMARY_TEMPLATE_P (templ))
  if (!PRIMARY_TEMPLATE_P (templ))
    for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
    for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
      if (TREE_CODE (t) == FUNCTION_DECL
      if (TREE_CODE (t) == FUNCTION_DECL
          /* Implicitly generated member functions will not have template
          /* Implicitly generated member functions will not have template
             information; they are not instantiations, but instead are
             information; they are not instantiations, but instead are
             created "fresh" for each instantiation.  */
             created "fresh" for each instantiation.  */
          && DECL_TEMPLATE_INFO (t))
          && DECL_TEMPLATE_INFO (t))
        tsubst_default_arguments (t);
        tsubst_default_arguments (t);
 
 
  /* Some typedefs referenced from within the template code need to be access
  /* Some typedefs referenced from within the template code need to be access
     checked at template instantiation time, i.e now. These types were
     checked at template instantiation time, i.e now. These types were
     added to the template at parsing time. Let's get those and perform
     added to the template at parsing time. Let's get those and perform
     the access checks then.  */
     the access checks then.  */
  perform_typedefs_access_check (pattern, args);
  perform_typedefs_access_check (pattern, args);
  perform_deferred_access_checks ();
  perform_deferred_access_checks ();
  pop_nested_class ();
  pop_nested_class ();
  maximum_field_alignment = saved_maximum_field_alignment;
  maximum_field_alignment = saved_maximum_field_alignment;
  pop_from_top_level ();
  pop_from_top_level ();
  pop_deferring_access_checks ();
  pop_deferring_access_checks ();
  pop_tinst_level ();
  pop_tinst_level ();
 
 
  /* The vtable for a template class can be emitted in any translation
  /* The vtable for a template class can be emitted in any translation
     unit in which the class is instantiated.  When there is no key
     unit in which the class is instantiated.  When there is no key
     method, however, finish_struct_1 will already have added TYPE to
     method, however, finish_struct_1 will already have added TYPE to
     the keyed_classes list.  */
     the keyed_classes list.  */
  if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
  if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
    keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
    keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
 
 
  return type;
  return type;
}
}
 
 
static tree
static tree
tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
{
  tree r;
  tree r;
 
 
  if (!t)
  if (!t)
    r = t;
    r = t;
  else if (TYPE_P (t))
  else if (TYPE_P (t))
    r = tsubst (t, args, complain, in_decl);
    r = tsubst (t, args, complain, in_decl);
  else
  else
    {
    {
      r = tsubst_expr (t, args, complain, in_decl,
      r = tsubst_expr (t, args, complain, in_decl,
                       /*integral_constant_expression_p=*/true);
                       /*integral_constant_expression_p=*/true);
      r = fold_non_dependent_expr (r);
      r = fold_non_dependent_expr (r);
    }
    }
  return r;
  return r;
}
}
 
 
/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   NONTYPE_ARGUMENT_PACK.  */
   NONTYPE_ARGUMENT_PACK.  */
 
 
static tree
static tree
make_fnparm_pack (tree spec_parm)
make_fnparm_pack (tree spec_parm)
{
{
  /* Collect all of the extra "packed" parameters into an
  /* Collect all of the extra "packed" parameters into an
     argument pack.  */
     argument pack.  */
  tree parmvec;
  tree parmvec;
  tree parmtypevec;
  tree parmtypevec;
  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
  tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
  tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
  tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
  int i, len = list_length (spec_parm);
  int i, len = list_length (spec_parm);
 
 
  /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters.  */
  /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters.  */
  parmvec = make_tree_vec (len);
  parmvec = make_tree_vec (len);
  parmtypevec = make_tree_vec (len);
  parmtypevec = make_tree_vec (len);
  for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
  for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
    {
    {
      TREE_VEC_ELT (parmvec, i) = spec_parm;
      TREE_VEC_ELT (parmvec, i) = spec_parm;
      TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
      TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
    }
    }
 
 
  /* Build the argument packs.  */
  /* Build the argument packs.  */
  SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
  SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
  SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
  SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
  TREE_TYPE (argpack) = argtypepack;
  TREE_TYPE (argpack) = argtypepack;
 
 
  return argpack;
  return argpack;
}
}
 
 
/* Substitute ARGS into T, which is an pack expansion
/* Substitute ARGS into T, which is an pack expansion
   (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
   (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
   TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   (if only a partial substitution could be performed) or
   (if only a partial substitution could be performed) or
   ERROR_MARK_NODE if there was an error.  */
   ERROR_MARK_NODE if there was an error.  */
tree
tree
tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
                       tree in_decl)
                       tree in_decl)
{
{
  tree pattern;
  tree pattern;
  tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
  tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
  int i, len = -1;
  int i, len = -1;
  tree result;
  tree result;
  int incomplete = 0;
  int incomplete = 0;
  htab_t saved_local_specializations = NULL;
  htab_t saved_local_specializations = NULL;
 
 
  gcc_assert (PACK_EXPANSION_P (t));
  gcc_assert (PACK_EXPANSION_P (t));
  pattern = PACK_EXPANSION_PATTERN (t);
  pattern = PACK_EXPANSION_PATTERN (t);
 
 
  /* Determine the argument packs that will instantiate the parameter
  /* Determine the argument packs that will instantiate the parameter
     packs used in the expansion expression. While we're at it,
     packs used in the expansion expression. While we're at it,
     compute the number of arguments to be expanded and make sure it
     compute the number of arguments to be expanded and make sure it
     is consistent.  */
     is consistent.  */
  for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
  for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
       pack = TREE_CHAIN (pack))
       pack = TREE_CHAIN (pack))
    {
    {
      tree parm_pack = TREE_VALUE (pack);
      tree parm_pack = TREE_VALUE (pack);
      tree arg_pack = NULL_TREE;
      tree arg_pack = NULL_TREE;
      tree orig_arg = NULL_TREE;
      tree orig_arg = NULL_TREE;
 
 
      if (TREE_CODE (parm_pack) == PARM_DECL)
      if (TREE_CODE (parm_pack) == PARM_DECL)
        {
        {
          if (!cp_unevaluated_operand)
          if (!cp_unevaluated_operand)
            arg_pack = retrieve_local_specialization (parm_pack);
            arg_pack = retrieve_local_specialization (parm_pack);
          else
          else
            {
            {
              /* We can't rely on local_specializations for a parameter
              /* We can't rely on local_specializations for a parameter
                 name used later in a function declaration (such as in a
                 name used later in a function declaration (such as in a
                 late-specified return type).  Even if it exists, it might
                 late-specified return type).  Even if it exists, it might
                 have the wrong value for a recursive call.  Just make a
                 have the wrong value for a recursive call.  Just make a
                 dummy decl, since it's only used for its type.  */
                 dummy decl, since it's only used for its type.  */
              arg_pack = tsubst_decl (parm_pack, args, complain);
              arg_pack = tsubst_decl (parm_pack, args, complain);
              arg_pack = make_fnparm_pack (arg_pack);
              arg_pack = make_fnparm_pack (arg_pack);
            }
            }
        }
        }
      else
      else
        {
        {
          int level, idx, levels;
          int level, idx, levels;
          template_parm_level_and_index (parm_pack, &level, &idx);
          template_parm_level_and_index (parm_pack, &level, &idx);
 
 
          levels = TMPL_ARGS_DEPTH (args);
          levels = TMPL_ARGS_DEPTH (args);
          if (level <= levels)
          if (level <= levels)
            arg_pack = TMPL_ARG (args, level, idx);
            arg_pack = TMPL_ARG (args, level, idx);
        }
        }
 
 
      orig_arg = arg_pack;
      orig_arg = arg_pack;
      if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
      if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
        arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
        arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
 
 
      if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
      if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
        /* This can only happen if we forget to expand an argument
        /* This can only happen if we forget to expand an argument
           pack somewhere else. Just return an error, silently.  */
           pack somewhere else. Just return an error, silently.  */
        {
        {
          result = make_tree_vec (1);
          result = make_tree_vec (1);
          TREE_VEC_ELT (result, 0) = error_mark_node;
          TREE_VEC_ELT (result, 0) = error_mark_node;
          return result;
          return result;
        }
        }
 
 
      if (arg_pack
      if (arg_pack
          && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
          && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
          && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
          && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
        {
        {
          tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
          tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
          tree pattern = PACK_EXPANSION_PATTERN (expansion);
          tree pattern = PACK_EXPANSION_PATTERN (expansion);
          if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
          if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
              || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
              || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
            /* The argument pack that the parameter maps to is just an
            /* The argument pack that the parameter maps to is just an
               expansion of the parameter itself, such as one would
               expansion of the parameter itself, such as one would
               find in the implicit typedef of a class inside the
               find in the implicit typedef of a class inside the
               class itself.  Consider this parameter "unsubstituted",
               class itself.  Consider this parameter "unsubstituted",
               so that we will maintain the outer pack expansion.  */
               so that we will maintain the outer pack expansion.  */
            arg_pack = NULL_TREE;
            arg_pack = NULL_TREE;
        }
        }
 
 
      if (arg_pack)
      if (arg_pack)
        {
        {
          int my_len =
          int my_len =
            TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
            TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
 
 
          /* It's all-or-nothing with incomplete argument packs.  */
          /* It's all-or-nothing with incomplete argument packs.  */
          if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
          if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
            return error_mark_node;
            return error_mark_node;
 
 
          if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
          if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
            incomplete = 1;
            incomplete = 1;
 
 
          if (len < 0)
          if (len < 0)
            len = my_len;
            len = my_len;
          else if (len != my_len)
          else if (len != my_len)
            {
            {
              if (incomplete)
              if (incomplete)
                /* We got explicit args for some packs but not others;
                /* We got explicit args for some packs but not others;
                   do nothing now and try again after deduction.  */
                   do nothing now and try again after deduction.  */
                return t;
                return t;
              if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
              if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
                error ("mismatched argument pack lengths while expanding "
                error ("mismatched argument pack lengths while expanding "
                       "%<%T%>",
                       "%<%T%>",
                       pattern);
                       pattern);
              else
              else
                error ("mismatched argument pack lengths while expanding "
                error ("mismatched argument pack lengths while expanding "
                       "%<%E%>",
                       "%<%E%>",
                       pattern);
                       pattern);
              return error_mark_node;
              return error_mark_node;
            }
            }
 
 
          /* Keep track of the parameter packs and their corresponding
          /* Keep track of the parameter packs and their corresponding
             argument packs.  */
             argument packs.  */
          packs = tree_cons (parm_pack, arg_pack, packs);
          packs = tree_cons (parm_pack, arg_pack, packs);
          TREE_TYPE (packs) = orig_arg;
          TREE_TYPE (packs) = orig_arg;
        }
        }
      else
      else
        /* We can't substitute for this parameter pack.  */
        /* We can't substitute for this parameter pack.  */
        unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
        unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
                                         TREE_VALUE (pack),
                                         TREE_VALUE (pack),
                                         unsubstituted_packs);
                                         unsubstituted_packs);
    }
    }
 
 
  /* We cannot expand this expansion expression, because we don't have
  /* We cannot expand this expansion expression, because we don't have
     all of the argument packs we need. Substitute into the pattern
     all of the argument packs we need. Substitute into the pattern
     and return a PACK_EXPANSION_*. The caller will need to deal with
     and return a PACK_EXPANSION_*. The caller will need to deal with
     that.  */
     that.  */
  if (unsubstituted_packs)
  if (unsubstituted_packs)
    {
    {
      tree new_pat;
      tree new_pat;
      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
        new_pat = tsubst_expr (pattern, args, complain, in_decl,
        new_pat = tsubst_expr (pattern, args, complain, in_decl,
                               /*integral_constant_expression_p=*/false);
                               /*integral_constant_expression_p=*/false);
      else
      else
        new_pat = tsubst (pattern, args, complain, in_decl);
        new_pat = tsubst (pattern, args, complain, in_decl);
      return make_pack_expansion (new_pat);
      return make_pack_expansion (new_pat);
    }
    }
 
 
  /* We could not find any argument packs that work.  */
  /* We could not find any argument packs that work.  */
  if (len < 0)
  if (len < 0)
    return error_mark_node;
    return error_mark_node;
 
 
  if (cp_unevaluated_operand)
  if (cp_unevaluated_operand)
    {
    {
      /* We're in a late-specified return type, so create our own local
      /* We're in a late-specified return type, so create our own local
         specializations table; the current table is either NULL or (in the
         specializations table; the current table is either NULL or (in the
         case of recursive unification) might have bindings that we don't
         case of recursive unification) might have bindings that we don't
         want to use or alter.  */
         want to use or alter.  */
      saved_local_specializations = local_specializations;
      saved_local_specializations = local_specializations;
      local_specializations = htab_create (37,
      local_specializations = htab_create (37,
                                           hash_local_specialization,
                                           hash_local_specialization,
                                           eq_local_specializations,
                                           eq_local_specializations,
                                           NULL);
                                           NULL);
    }
    }
 
 
  /* For each argument in each argument pack, substitute into the
  /* For each argument in each argument pack, substitute into the
     pattern.  */
     pattern.  */
  result = make_tree_vec (len + incomplete);
  result = make_tree_vec (len + incomplete);
  for (i = 0; i < len + incomplete; ++i)
  for (i = 0; i < len + incomplete; ++i)
    {
    {
      /* For parameter pack, change the substitution of the parameter
      /* For parameter pack, change the substitution of the parameter
         pack to the ith argument in its argument pack, then expand
         pack to the ith argument in its argument pack, then expand
         the pattern.  */
         the pattern.  */
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
        {
        {
          tree parm = TREE_PURPOSE (pack);
          tree parm = TREE_PURPOSE (pack);
 
 
          if (TREE_CODE (parm) == PARM_DECL)
          if (TREE_CODE (parm) == PARM_DECL)
            {
            {
              /* Select the Ith argument from the pack.  */
              /* Select the Ith argument from the pack.  */
              tree arg = make_node (ARGUMENT_PACK_SELECT);
              tree arg = make_node (ARGUMENT_PACK_SELECT);
              ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
              ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
              ARGUMENT_PACK_SELECT_INDEX (arg) = i;
              ARGUMENT_PACK_SELECT_INDEX (arg) = i;
              mark_used (parm);
              mark_used (parm);
              register_local_specialization (arg, parm);
              register_local_specialization (arg, parm);
            }
            }
          else
          else
            {
            {
              tree value = parm;
              tree value = parm;
              int idx, level;
              int idx, level;
              template_parm_level_and_index (parm, &level, &idx);
              template_parm_level_and_index (parm, &level, &idx);
 
 
              if (i < len)
              if (i < len)
                {
                {
                  /* Select the Ith argument from the pack. */
                  /* Select the Ith argument from the pack. */
                  value = make_node (ARGUMENT_PACK_SELECT);
                  value = make_node (ARGUMENT_PACK_SELECT);
                  ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
                  ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
                  ARGUMENT_PACK_SELECT_INDEX (value) = i;
                  ARGUMENT_PACK_SELECT_INDEX (value) = i;
                }
                }
 
 
              /* Update the corresponding argument.  */
              /* Update the corresponding argument.  */
              TMPL_ARG (args, level, idx) = value;
              TMPL_ARG (args, level, idx) = value;
            }
            }
        }
        }
 
 
      /* Substitute into the PATTERN with the altered arguments.  */
      /* Substitute into the PATTERN with the altered arguments.  */
      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
        TREE_VEC_ELT (result, i) =
        TREE_VEC_ELT (result, i) =
          tsubst_expr (pattern, args, complain, in_decl,
          tsubst_expr (pattern, args, complain, in_decl,
                       /*integral_constant_expression_p=*/false);
                       /*integral_constant_expression_p=*/false);
      else
      else
        TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
        TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
 
 
      if (i == len)
      if (i == len)
        /* When we have incomplete argument packs, the last "expanded"
        /* When we have incomplete argument packs, the last "expanded"
           result is itself a pack expansion, which allows us
           result is itself a pack expansion, which allows us
           to deduce more arguments.  */
           to deduce more arguments.  */
        TREE_VEC_ELT (result, i) =
        TREE_VEC_ELT (result, i) =
          make_pack_expansion (TREE_VEC_ELT (result, i));
          make_pack_expansion (TREE_VEC_ELT (result, i));
 
 
      if (TREE_VEC_ELT (result, i) == error_mark_node)
      if (TREE_VEC_ELT (result, i) == error_mark_node)
        {
        {
          result = error_mark_node;
          result = error_mark_node;
          break;
          break;
        }
        }
    }
    }
 
 
  /* Update ARGS to restore the substitution from parameter packs to
  /* Update ARGS to restore the substitution from parameter packs to
     their argument packs.  */
     their argument packs.  */
  for (pack = packs; pack; pack = TREE_CHAIN (pack))
  for (pack = packs; pack; pack = TREE_CHAIN (pack))
    {
    {
      tree parm = TREE_PURPOSE (pack);
      tree parm = TREE_PURPOSE (pack);
 
 
      if (TREE_CODE (parm) == PARM_DECL)
      if (TREE_CODE (parm) == PARM_DECL)
        register_local_specialization (TREE_TYPE (pack), parm);
        register_local_specialization (TREE_TYPE (pack), parm);
      else
      else
        {
        {
          int idx, level;
          int idx, level;
          template_parm_level_and_index (parm, &level, &idx);
          template_parm_level_and_index (parm, &level, &idx);
 
 
          /* Update the corresponding argument.  */
          /* Update the corresponding argument.  */
          if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
          if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
            TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
            TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
              TREE_TYPE (pack);
              TREE_TYPE (pack);
          else
          else
            TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
            TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
        }
        }
    }
    }
 
 
  if (saved_local_specializations)
  if (saved_local_specializations)
    {
    {
      htab_delete (local_specializations);
      htab_delete (local_specializations);
      local_specializations = saved_local_specializations;
      local_specializations = saved_local_specializations;
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
/* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
   TMPL.  We do this using DECL_PARM_INDEX, which should work even with
   TMPL.  We do this using DECL_PARM_INDEX, which should work even with
   parameter packs; all parms generated from a function parameter pack will
   parameter packs; all parms generated from a function parameter pack will
   have the same DECL_PARM_INDEX.  */
   have the same DECL_PARM_INDEX.  */
 
 
tree
tree
get_pattern_parm (tree parm, tree tmpl)
get_pattern_parm (tree parm, tree tmpl)
{
{
  tree pattern = DECL_TEMPLATE_RESULT (tmpl);
  tree pattern = DECL_TEMPLATE_RESULT (tmpl);
  tree patparm;
  tree patparm;
 
 
  if (DECL_ARTIFICIAL (parm))
  if (DECL_ARTIFICIAL (parm))
    {
    {
      for (patparm = DECL_ARGUMENTS (pattern);
      for (patparm = DECL_ARGUMENTS (pattern);
           patparm; patparm = TREE_CHAIN (patparm))
           patparm; patparm = TREE_CHAIN (patparm))
        if (DECL_ARTIFICIAL (patparm)
        if (DECL_ARTIFICIAL (patparm)
            && DECL_NAME (parm) == DECL_NAME (patparm))
            && DECL_NAME (parm) == DECL_NAME (patparm))
          break;
          break;
    }
    }
  else
  else
    {
    {
      patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
      patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
      patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
      patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
      gcc_assert (DECL_PARM_INDEX (patparm)
      gcc_assert (DECL_PARM_INDEX (patparm)
                  == DECL_PARM_INDEX (parm));
                  == DECL_PARM_INDEX (parm));
    }
    }
 
 
  return patparm;
  return patparm;
}
}
 
 
/* Substitute ARGS into the vector or list of template arguments T.  */
/* Substitute ARGS into the vector or list of template arguments T.  */
 
 
static tree
static tree
tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
{
  tree orig_t = t;
  tree orig_t = t;
  int len = TREE_VEC_LENGTH (t);
  int len = TREE_VEC_LENGTH (t);
  int need_new = 0, i, expanded_len_adjust = 0, out;
  int need_new = 0, i, expanded_len_adjust = 0, out;
  tree *elts = (tree *) alloca (len * sizeof (tree));
  tree *elts = (tree *) alloca (len * sizeof (tree));
 
 
  for (i = 0; i < len; i++)
  for (i = 0; i < len; i++)
    {
    {
      tree orig_arg = TREE_VEC_ELT (t, i);
      tree orig_arg = TREE_VEC_ELT (t, i);
      tree new_arg;
      tree new_arg;
 
 
      if (TREE_CODE (orig_arg) == TREE_VEC)
      if (TREE_CODE (orig_arg) == TREE_VEC)
        new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
        new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
      else if (PACK_EXPANSION_P (orig_arg))
      else if (PACK_EXPANSION_P (orig_arg))
        {
        {
          /* Substitute into an expansion expression.  */
          /* Substitute into an expansion expression.  */
          new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
          new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
 
 
          if (TREE_CODE (new_arg) == TREE_VEC)
          if (TREE_CODE (new_arg) == TREE_VEC)
            /* Add to the expanded length adjustment the number of
            /* Add to the expanded length adjustment the number of
               expanded arguments. We subtract one from this
               expanded arguments. We subtract one from this
               measurement, because the argument pack expression
               measurement, because the argument pack expression
               itself is already counted as 1 in
               itself is already counted as 1 in
               LEN. EXPANDED_LEN_ADJUST can actually be negative, if
               LEN. EXPANDED_LEN_ADJUST can actually be negative, if
               the argument pack is empty.  */
               the argument pack is empty.  */
            expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
            expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
        }
        }
      else if (ARGUMENT_PACK_P (orig_arg))
      else if (ARGUMENT_PACK_P (orig_arg))
        {
        {
          /* Substitute into each of the arguments.  */
          /* Substitute into each of the arguments.  */
          new_arg = TYPE_P (orig_arg)
          new_arg = TYPE_P (orig_arg)
            ? cxx_make_type (TREE_CODE (orig_arg))
            ? cxx_make_type (TREE_CODE (orig_arg))
            : make_node (TREE_CODE (orig_arg));
            : make_node (TREE_CODE (orig_arg));
 
 
          SET_ARGUMENT_PACK_ARGS (
          SET_ARGUMENT_PACK_ARGS (
            new_arg,
            new_arg,
            tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
            tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
                                  args, complain, in_decl));
                                  args, complain, in_decl));
 
 
          if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
          if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
            new_arg = error_mark_node;
            new_arg = error_mark_node;
 
 
          if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
          if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
            TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
            TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
                                          complain, in_decl);
                                          complain, in_decl);
            TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
            TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
 
 
            if (TREE_TYPE (new_arg) == error_mark_node)
            if (TREE_TYPE (new_arg) == error_mark_node)
              new_arg = error_mark_node;
              new_arg = error_mark_node;
          }
          }
        }
        }
      else
      else
        new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
        new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
 
 
      if (new_arg == error_mark_node)
      if (new_arg == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      elts[i] = new_arg;
      elts[i] = new_arg;
      if (new_arg != orig_arg)
      if (new_arg != orig_arg)
        need_new = 1;
        need_new = 1;
    }
    }
 
 
  if (!need_new)
  if (!need_new)
    return t;
    return t;
 
 
  /* Make space for the expanded arguments coming from template
  /* Make space for the expanded arguments coming from template
     argument packs.  */
     argument packs.  */
  t = make_tree_vec (len + expanded_len_adjust);
  t = make_tree_vec (len + expanded_len_adjust);
  /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
  /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
     arguments for a member template.
     arguments for a member template.
     In that case each TREE_VEC in ORIG_T represents a level of template
     In that case each TREE_VEC in ORIG_T represents a level of template
     arguments, and ORIG_T won't carry any non defaulted argument count.
     arguments, and ORIG_T won't carry any non defaulted argument count.
     It will rather be the nested TREE_VECs that will carry one.
     It will rather be the nested TREE_VECs that will carry one.
     In other words, ORIG_T carries a non defaulted argument count only
     In other words, ORIG_T carries a non defaulted argument count only
     if it doesn't contain any nested TREE_VEC.  */
     if it doesn't contain any nested TREE_VEC.  */
  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
    {
    {
      int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
      int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
      count += expanded_len_adjust;
      count += expanded_len_adjust;
      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
    }
    }
  for (i = 0, out = 0; i < len; i++)
  for (i = 0, out = 0; i < len; i++)
    {
    {
      if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
      if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
           || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
           || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
          && TREE_CODE (elts[i]) == TREE_VEC)
          && TREE_CODE (elts[i]) == TREE_VEC)
        {
        {
          int idx;
          int idx;
 
 
          /* Now expand the template argument pack "in place".  */
          /* Now expand the template argument pack "in place".  */
          for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
          for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
            TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
            TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
        }
        }
      else
      else
        {
        {
          TREE_VEC_ELT (t, out) = elts[i];
          TREE_VEC_ELT (t, out) = elts[i];
          out++;
          out++;
        }
        }
    }
    }
 
 
  return t;
  return t;
}
}
 
 
/* Return the result of substituting ARGS into the template parameters
/* Return the result of substituting ARGS into the template parameters
   given by PARMS.  If there are m levels of ARGS and m + n levels of
   given by PARMS.  If there are m levels of ARGS and m + n levels of
   PARMS, then the result will contain n levels of PARMS.  For
   PARMS, then the result will contain n levels of PARMS.  For
   example, if PARMS is `template <class T> template <class U>
   example, if PARMS is `template <class T> template <class U>
   template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   result will be `template <int*, double, class V>'.  */
   result will be `template <int*, double, class V>'.  */
 
 
static tree
static tree
tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
{
{
  tree r = NULL_TREE;
  tree r = NULL_TREE;
  tree* new_parms;
  tree* new_parms;
 
 
  /* When substituting into a template, we must set
  /* When substituting into a template, we must set
     PROCESSING_TEMPLATE_DECL as the template parameters may be
     PROCESSING_TEMPLATE_DECL as the template parameters may be
     dependent if they are based on one-another, and the dependency
     dependent if they are based on one-another, and the dependency
     predicates are short-circuit outside of templates.  */
     predicates are short-circuit outside of templates.  */
  ++processing_template_decl;
  ++processing_template_decl;
 
 
  for (new_parms = &r;
  for (new_parms = &r;
       TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
       TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
       new_parms = &(TREE_CHAIN (*new_parms)),
       new_parms = &(TREE_CHAIN (*new_parms)),
         parms = TREE_CHAIN (parms))
         parms = TREE_CHAIN (parms))
    {
    {
      tree new_vec =
      tree new_vec =
        make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
        make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
      int i;
      int i;
 
 
      for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
      for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
        {
        {
          tree tuple;
          tree tuple;
          tree default_value;
          tree default_value;
          tree parm_decl;
          tree parm_decl;
 
 
          if (parms == error_mark_node)
          if (parms == error_mark_node)
            continue;
            continue;
 
 
          tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
          tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
 
 
          if (tuple == error_mark_node)
          if (tuple == error_mark_node)
            continue;
            continue;
 
 
          default_value = TREE_PURPOSE (tuple);
          default_value = TREE_PURPOSE (tuple);
          parm_decl = TREE_VALUE (tuple);
          parm_decl = TREE_VALUE (tuple);
 
 
          parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
          parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
          if (TREE_CODE (parm_decl) == PARM_DECL
          if (TREE_CODE (parm_decl) == PARM_DECL
              && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
              && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
            parm_decl = error_mark_node;
            parm_decl = error_mark_node;
          default_value = tsubst_template_arg (default_value, args,
          default_value = tsubst_template_arg (default_value, args,
                                               complain, NULL_TREE);
                                               complain, NULL_TREE);
 
 
          tuple = build_tree_list (default_value, parm_decl);
          tuple = build_tree_list (default_value, parm_decl);
          TREE_VEC_ELT (new_vec, i) = tuple;
          TREE_VEC_ELT (new_vec, i) = tuple;
        }
        }
 
 
      *new_parms =
      *new_parms =
        tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
        tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
                             - TMPL_ARGS_DEPTH (args)),
                             - TMPL_ARGS_DEPTH (args)),
                   new_vec, NULL_TREE);
                   new_vec, NULL_TREE);
    }
    }
 
 
  --processing_template_decl;
  --processing_template_decl;
 
 
  return r;
  return r;
}
}
 
 
/* Substitute the ARGS into the indicated aggregate (or enumeration)
/* Substitute the ARGS into the indicated aggregate (or enumeration)
   type T.  If T is not an aggregate or enumeration type, it is
   type T.  If T is not an aggregate or enumeration type, it is
   handled as if by tsubst.  IN_DECL is as for tsubst.  If
   handled as if by tsubst.  IN_DECL is as for tsubst.  If
   ENTERING_SCOPE is nonzero, T is the context for a template which
   ENTERING_SCOPE is nonzero, T is the context for a template which
   we are presently tsubst'ing.  Return the substituted value.  */
   we are presently tsubst'ing.  Return the substituted value.  */
 
 
static tree
static tree
tsubst_aggr_type (tree t,
tsubst_aggr_type (tree t,
                  tree args,
                  tree args,
                  tsubst_flags_t complain,
                  tsubst_flags_t complain,
                  tree in_decl,
                  tree in_decl,
                  int entering_scope)
                  int entering_scope)
{
{
  if (t == NULL_TREE)
  if (t == NULL_TREE)
    return NULL_TREE;
    return NULL_TREE;
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case RECORD_TYPE:
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
      if (TYPE_PTRMEMFUNC_P (t))
        return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
        return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
 
 
      /* Else fall through.  */
      /* Else fall through.  */
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
    case UNION_TYPE:
    case UNION_TYPE:
      if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
      if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
        {
        {
          tree argvec;
          tree argvec;
          tree context;
          tree context;
          tree r;
          tree r;
          int saved_unevaluated_operand;
          int saved_unevaluated_operand;
          int saved_inhibit_evaluation_warnings;
          int saved_inhibit_evaluation_warnings;
 
 
          /* In "sizeof(X<I>)" we need to evaluate "I".  */
          /* In "sizeof(X<I>)" we need to evaluate "I".  */
          saved_unevaluated_operand = cp_unevaluated_operand;
          saved_unevaluated_operand = cp_unevaluated_operand;
          cp_unevaluated_operand = 0;
          cp_unevaluated_operand = 0;
          saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
          saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
          c_inhibit_evaluation_warnings = 0;
          c_inhibit_evaluation_warnings = 0;
 
 
          /* First, determine the context for the type we are looking
          /* First, determine the context for the type we are looking
             up.  */
             up.  */
          context = TYPE_CONTEXT (t);
          context = TYPE_CONTEXT (t);
          if (context)
          if (context)
            {
            {
              context = tsubst_aggr_type (context, args, complain,
              context = tsubst_aggr_type (context, args, complain,
                                          in_decl, /*entering_scope=*/1);
                                          in_decl, /*entering_scope=*/1);
              /* If context is a nested class inside a class template,
              /* If context is a nested class inside a class template,
                 it may still need to be instantiated (c++/33959).  */
                 it may still need to be instantiated (c++/33959).  */
              if (TYPE_P (context))
              if (TYPE_P (context))
                context = complete_type (context);
                context = complete_type (context);
            }
            }
 
 
          /* Then, figure out what arguments are appropriate for the
          /* Then, figure out what arguments are appropriate for the
             type we are trying to find.  For example, given:
             type we are trying to find.  For example, given:
 
 
               template <class T> struct S;
               template <class T> struct S;
               template <class T, class U> void f(T, U) { S<U> su; }
               template <class T, class U> void f(T, U) { S<U> su; }
 
 
             and supposing that we are instantiating f<int, double>,
             and supposing that we are instantiating f<int, double>,
             then our ARGS will be {int, double}, but, when looking up
             then our ARGS will be {int, double}, but, when looking up
             S we only want {double}.  */
             S we only want {double}.  */
          argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
          argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
                                         complain, in_decl);
                                         complain, in_decl);
          if (argvec == error_mark_node)
          if (argvec == error_mark_node)
            r = error_mark_node;
            r = error_mark_node;
          else
          else
            {
            {
              r = lookup_template_class (t, argvec, in_decl, context,
              r = lookup_template_class (t, argvec, in_decl, context,
                                         entering_scope, complain);
                                         entering_scope, complain);
              r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
              r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
            }
            }
 
 
          cp_unevaluated_operand = saved_unevaluated_operand;
          cp_unevaluated_operand = saved_unevaluated_operand;
          c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
          c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
 
 
          return r;
          return r;
        }
        }
      else
      else
        /* This is not a template type, so there's nothing to do.  */
        /* This is not a template type, so there's nothing to do.  */
        return t;
        return t;
 
 
    default:
    default:
      return tsubst (t, args, complain, in_decl);
      return tsubst (t, args, complain, in_decl);
    }
    }
}
}
 
 
/* Substitute into the default argument ARG (a default argument for
/* Substitute into the default argument ARG (a default argument for
   FN), which has the indicated TYPE.  */
   FN), which has the indicated TYPE.  */
 
 
tree
tree
tsubst_default_argument (tree fn, tree type, tree arg)
tsubst_default_argument (tree fn, tree type, tree arg)
{
{
  tree saved_class_ptr = NULL_TREE;
  tree saved_class_ptr = NULL_TREE;
  tree saved_class_ref = NULL_TREE;
  tree saved_class_ref = NULL_TREE;
 
 
  /* This default argument came from a template.  Instantiate the
  /* This default argument came from a template.  Instantiate the
     default argument here, not in tsubst.  In the case of
     default argument here, not in tsubst.  In the case of
     something like:
     something like:
 
 
       template <class T>
       template <class T>
       struct S {
       struct S {
         static T t();
         static T t();
         void f(T = t());
         void f(T = t());
       };
       };
 
 
     we must be careful to do name lookup in the scope of S<T>,
     we must be careful to do name lookup in the scope of S<T>,
     rather than in the current class.  */
     rather than in the current class.  */
  push_access_scope (fn);
  push_access_scope (fn);
  /* The "this" pointer is not valid in a default argument.  */
  /* The "this" pointer is not valid in a default argument.  */
  if (cfun)
  if (cfun)
    {
    {
      saved_class_ptr = current_class_ptr;
      saved_class_ptr = current_class_ptr;
      cp_function_chain->x_current_class_ptr = NULL_TREE;
      cp_function_chain->x_current_class_ptr = NULL_TREE;
      saved_class_ref = current_class_ref;
      saved_class_ref = current_class_ref;
      cp_function_chain->x_current_class_ref = NULL_TREE;
      cp_function_chain->x_current_class_ref = NULL_TREE;
    }
    }
 
 
  push_deferring_access_checks(dk_no_deferred);
  push_deferring_access_checks(dk_no_deferred);
  /* The default argument expression may cause implicitly defined
  /* The default argument expression may cause implicitly defined
     member functions to be synthesized, which will result in garbage
     member functions to be synthesized, which will result in garbage
     collection.  We must treat this situation as if we were within
     collection.  We must treat this situation as if we were within
     the body of function so as to avoid collecting live data on the
     the body of function so as to avoid collecting live data on the
     stack.  */
     stack.  */
  ++function_depth;
  ++function_depth;
  arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
  arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
                     tf_warning_or_error, NULL_TREE,
                     tf_warning_or_error, NULL_TREE,
                     /*integral_constant_expression_p=*/false);
                     /*integral_constant_expression_p=*/false);
  --function_depth;
  --function_depth;
  pop_deferring_access_checks();
  pop_deferring_access_checks();
 
 
  /* Restore the "this" pointer.  */
  /* Restore the "this" pointer.  */
  if (cfun)
  if (cfun)
    {
    {
      cp_function_chain->x_current_class_ptr = saved_class_ptr;
      cp_function_chain->x_current_class_ptr = saved_class_ptr;
      cp_function_chain->x_current_class_ref = saved_class_ref;
      cp_function_chain->x_current_class_ref = saved_class_ref;
    }
    }
 
 
  /* Make sure the default argument is reasonable.  */
  /* Make sure the default argument is reasonable.  */
  arg = check_default_argument (type, arg);
  arg = check_default_argument (type, arg);
 
 
  pop_access_scope (fn);
  pop_access_scope (fn);
 
 
  return arg;
  return arg;
}
}
 
 
/* Substitute into all the default arguments for FN.  */
/* Substitute into all the default arguments for FN.  */
 
 
static void
static void
tsubst_default_arguments (tree fn)
tsubst_default_arguments (tree fn)
{
{
  tree arg;
  tree arg;
  tree tmpl_args;
  tree tmpl_args;
 
 
  tmpl_args = DECL_TI_ARGS (fn);
  tmpl_args = DECL_TI_ARGS (fn);
 
 
  /* If this function is not yet instantiated, we certainly don't need
  /* If this function is not yet instantiated, we certainly don't need
     its default arguments.  */
     its default arguments.  */
  if (uses_template_parms (tmpl_args))
  if (uses_template_parms (tmpl_args))
    return;
    return;
 
 
  for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
  for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
       arg;
       arg;
       arg = TREE_CHAIN (arg))
       arg = TREE_CHAIN (arg))
    if (TREE_PURPOSE (arg))
    if (TREE_PURPOSE (arg))
      TREE_PURPOSE (arg) = tsubst_default_argument (fn,
      TREE_PURPOSE (arg) = tsubst_default_argument (fn,
                                                    TREE_VALUE (arg),
                                                    TREE_VALUE (arg),
                                                    TREE_PURPOSE (arg));
                                                    TREE_PURPOSE (arg));
}
}
 
 
/* Substitute the ARGS into the T, which is a _DECL.  Return the
/* Substitute the ARGS into the T, which is a _DECL.  Return the
   result of the substitution.  Issue error and warning messages under
   result of the substitution.  Issue error and warning messages under
   control of COMPLAIN.  */
   control of COMPLAIN.  */
 
 
static tree
static tree
tsubst_decl (tree t, tree args, tsubst_flags_t complain)
tsubst_decl (tree t, tree args, tsubst_flags_t complain)
{
{
#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
#define RETURN(EXP) do { r = (EXP); goto out; } while(0)
  location_t saved_loc;
  location_t saved_loc;
  tree r = NULL_TREE;
  tree r = NULL_TREE;
  tree in_decl = t;
  tree in_decl = t;
  hashval_t hash = 0;
  hashval_t hash = 0;
 
 
  /* Set the filename and linenumber to improve error-reporting.  */
  /* Set the filename and linenumber to improve error-reporting.  */
  saved_loc = input_location;
  saved_loc = input_location;
  input_location = DECL_SOURCE_LOCATION (t);
  input_location = DECL_SOURCE_LOCATION (t);
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      {
      {
        /* We can get here when processing a member function template,
        /* We can get here when processing a member function template,
           member class template, or template template parameter.  */
           member class template, or template template parameter.  */
        tree decl = DECL_TEMPLATE_RESULT (t);
        tree decl = DECL_TEMPLATE_RESULT (t);
        tree spec;
        tree spec;
        tree tmpl_args;
        tree tmpl_args;
        tree full_args;
        tree full_args;
 
 
        if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
        if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
          {
          {
            /* Template template parameter is treated here.  */
            /* Template template parameter is treated here.  */
            tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            if (new_type == error_mark_node)
            if (new_type == error_mark_node)
              RETURN (error_mark_node);
              RETURN (error_mark_node);
 
 
            r = copy_decl (t);
            r = copy_decl (t);
            TREE_CHAIN (r) = NULL_TREE;
            TREE_CHAIN (r) = NULL_TREE;
            TREE_TYPE (r) = new_type;
            TREE_TYPE (r) = new_type;
            DECL_TEMPLATE_RESULT (r)
            DECL_TEMPLATE_RESULT (r)
              = build_decl (DECL_SOURCE_LOCATION (decl),
              = build_decl (DECL_SOURCE_LOCATION (decl),
                            TYPE_DECL, DECL_NAME (decl), new_type);
                            TYPE_DECL, DECL_NAME (decl), new_type);
            DECL_TEMPLATE_PARMS (r)
            DECL_TEMPLATE_PARMS (r)
              = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
              = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
                                       complain);
                                       complain);
            TYPE_NAME (new_type) = r;
            TYPE_NAME (new_type) = r;
            break;
            break;
          }
          }
 
 
        /* We might already have an instance of this template.
        /* We might already have an instance of this template.
           The ARGS are for the surrounding class type, so the
           The ARGS are for the surrounding class type, so the
           full args contain the tsubst'd args for the context,
           full args contain the tsubst'd args for the context,
           plus the innermost args from the template decl.  */
           plus the innermost args from the template decl.  */
        tmpl_args = DECL_CLASS_TEMPLATE_P (t)
        tmpl_args = DECL_CLASS_TEMPLATE_P (t)
          ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
          ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
          : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
          : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
        /* Because this is a template, the arguments will still be
        /* Because this is a template, the arguments will still be
           dependent, even after substitution.  If
           dependent, even after substitution.  If
           PROCESSING_TEMPLATE_DECL is not set, the dependency
           PROCESSING_TEMPLATE_DECL is not set, the dependency
           predicates will short-circuit.  */
           predicates will short-circuit.  */
        ++processing_template_decl;
        ++processing_template_decl;
        full_args = tsubst_template_args (tmpl_args, args,
        full_args = tsubst_template_args (tmpl_args, args,
                                          complain, in_decl);
                                          complain, in_decl);
        --processing_template_decl;
        --processing_template_decl;
        if (full_args == error_mark_node)
        if (full_args == error_mark_node)
          RETURN (error_mark_node);
          RETURN (error_mark_node);
 
 
        /* If this is a default template template argument,
        /* If this is a default template template argument,
           tsubst might not have changed anything.  */
           tsubst might not have changed anything.  */
        if (full_args == tmpl_args)
        if (full_args == tmpl_args)
          RETURN (t);
          RETURN (t);
 
 
        hash = hash_tmpl_and_args (t, full_args);
        hash = hash_tmpl_and_args (t, full_args);
        spec = retrieve_specialization (t, full_args, hash);
        spec = retrieve_specialization (t, full_args, hash);
        if (spec != NULL_TREE)
        if (spec != NULL_TREE)
          {
          {
            r = spec;
            r = spec;
            break;
            break;
          }
          }
 
 
        /* Make a new template decl.  It will be similar to the
        /* Make a new template decl.  It will be similar to the
           original, but will record the current template arguments.
           original, but will record the current template arguments.
           We also create a new function declaration, which is just
           We also create a new function declaration, which is just
           like the old one, but points to this new template, rather
           like the old one, but points to this new template, rather
           than the old one.  */
           than the old one.  */
        r = copy_decl (t);
        r = copy_decl (t);
        gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
        gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
        TREE_CHAIN (r) = NULL_TREE;
        TREE_CHAIN (r) = NULL_TREE;
 
 
        DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
        DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
 
 
        if (TREE_CODE (decl) == TYPE_DECL)
        if (TREE_CODE (decl) == TYPE_DECL)
          {
          {
            tree new_type;
            tree new_type;
            ++processing_template_decl;
            ++processing_template_decl;
            new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            --processing_template_decl;
            --processing_template_decl;
            if (new_type == error_mark_node)
            if (new_type == error_mark_node)
              RETURN (error_mark_node);
              RETURN (error_mark_node);
 
 
            TREE_TYPE (r) = new_type;
            TREE_TYPE (r) = new_type;
            CLASSTYPE_TI_TEMPLATE (new_type) = r;
            CLASSTYPE_TI_TEMPLATE (new_type) = r;
            DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
            DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
            DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
            DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
            DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
            DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
          }
          }
        else
        else
          {
          {
            tree new_decl;
            tree new_decl;
            ++processing_template_decl;
            ++processing_template_decl;
            new_decl = tsubst (decl, args, complain, in_decl);
            new_decl = tsubst (decl, args, complain, in_decl);
            --processing_template_decl;
            --processing_template_decl;
            if (new_decl == error_mark_node)
            if (new_decl == error_mark_node)
              RETURN (error_mark_node);
              RETURN (error_mark_node);
 
 
            DECL_TEMPLATE_RESULT (r) = new_decl;
            DECL_TEMPLATE_RESULT (r) = new_decl;
            DECL_TI_TEMPLATE (new_decl) = r;
            DECL_TI_TEMPLATE (new_decl) = r;
            TREE_TYPE (r) = TREE_TYPE (new_decl);
            TREE_TYPE (r) = TREE_TYPE (new_decl);
            DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
            DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
            DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
            DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
          }
          }
 
 
        SET_DECL_IMPLICIT_INSTANTIATION (r);
        SET_DECL_IMPLICIT_INSTANTIATION (r);
        DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
        DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
        DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
        DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
 
 
        /* The template parameters for this new template are all the
        /* The template parameters for this new template are all the
           template parameters for the old template, except the
           template parameters for the old template, except the
           outermost level of parameters.  */
           outermost level of parameters.  */
        DECL_TEMPLATE_PARMS (r)
        DECL_TEMPLATE_PARMS (r)
          = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
          = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
                                   complain);
                                   complain);
 
 
        if (PRIMARY_TEMPLATE_P (t))
        if (PRIMARY_TEMPLATE_P (t))
          DECL_PRIMARY_TEMPLATE (r) = r;
          DECL_PRIMARY_TEMPLATE (r) = r;
 
 
        if (TREE_CODE (decl) != TYPE_DECL)
        if (TREE_CODE (decl) != TYPE_DECL)
          /* Record this non-type partial instantiation.  */
          /* Record this non-type partial instantiation.  */
          register_specialization (r, t,
          register_specialization (r, t,
                                   DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
                                   DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
                                   false, hash);
                                   false, hash);
      }
      }
      break;
      break;
 
 
    case FUNCTION_DECL:
    case FUNCTION_DECL:
      {
      {
        tree ctx;
        tree ctx;
        tree argvec = NULL_TREE;
        tree argvec = NULL_TREE;
        tree *friends;
        tree *friends;
        tree gen_tmpl;
        tree gen_tmpl;
        tree type;
        tree type;
        int member;
        int member;
        int args_depth;
        int args_depth;
        int parms_depth;
        int parms_depth;
 
 
        /* Nobody should be tsubst'ing into non-template functions.  */
        /* Nobody should be tsubst'ing into non-template functions.  */
        gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
        gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
 
 
        if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
        if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
          {
          {
            tree spec;
            tree spec;
            bool dependent_p;
            bool dependent_p;
 
 
            /* If T is not dependent, just return it.  We have to
            /* If T is not dependent, just return it.  We have to
               increment PROCESSING_TEMPLATE_DECL because
               increment PROCESSING_TEMPLATE_DECL because
               value_dependent_expression_p assumes that nothing is
               value_dependent_expression_p assumes that nothing is
               dependent when PROCESSING_TEMPLATE_DECL is zero.  */
               dependent when PROCESSING_TEMPLATE_DECL is zero.  */
            ++processing_template_decl;
            ++processing_template_decl;
            dependent_p = value_dependent_expression_p (t);
            dependent_p = value_dependent_expression_p (t);
            --processing_template_decl;
            --processing_template_decl;
            if (!dependent_p)
            if (!dependent_p)
              RETURN (t);
              RETURN (t);
 
 
            /* Calculate the most general template of which R is a
            /* Calculate the most general template of which R is a
               specialization, and the complete set of arguments used to
               specialization, and the complete set of arguments used to
               specialize R.  */
               specialize R.  */
            gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
            gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
            argvec = tsubst_template_args (DECL_TI_ARGS
            argvec = tsubst_template_args (DECL_TI_ARGS
                                           (DECL_TEMPLATE_RESULT (gen_tmpl)),
                                           (DECL_TEMPLATE_RESULT (gen_tmpl)),
                                           args, complain, in_decl);
                                           args, complain, in_decl);
 
 
            /* Check to see if we already have this specialization.  */
            /* Check to see if we already have this specialization.  */
            hash = hash_tmpl_and_args (gen_tmpl, argvec);
            hash = hash_tmpl_and_args (gen_tmpl, argvec);
            spec = retrieve_specialization (gen_tmpl, argvec, hash);
            spec = retrieve_specialization (gen_tmpl, argvec, hash);
 
 
            if (spec)
            if (spec)
              {
              {
                r = spec;
                r = spec;
                break;
                break;
              }
              }
 
 
            /* We can see more levels of arguments than parameters if
            /* We can see more levels of arguments than parameters if
               there was a specialization of a member template, like
               there was a specialization of a member template, like
               this:
               this:
 
 
                 template <class T> struct S { template <class U> void f(); }
                 template <class T> struct S { template <class U> void f(); }
                 template <> template <class U> void S<int>::f(U);
                 template <> template <class U> void S<int>::f(U);
 
 
               Here, we'll be substituting into the specialization,
               Here, we'll be substituting into the specialization,
               because that's where we can find the code we actually
               because that's where we can find the code we actually
               want to generate, but we'll have enough arguments for
               want to generate, but we'll have enough arguments for
               the most general template.
               the most general template.
 
 
               We also deal with the peculiar case:
               We also deal with the peculiar case:
 
 
                 template <class T> struct S {
                 template <class T> struct S {
                   template <class U> friend void f();
                   template <class U> friend void f();
                 };
                 };
                 template <class U> void f() {}
                 template <class U> void f() {}
                 template S<int>;
                 template S<int>;
                 template void f<double>();
                 template void f<double>();
 
 
               Here, the ARGS for the instantiation of will be {int,
               Here, the ARGS for the instantiation of will be {int,
               double}.  But, we only need as many ARGS as there are
               double}.  But, we only need as many ARGS as there are
               levels of template parameters in CODE_PATTERN.  We are
               levels of template parameters in CODE_PATTERN.  We are
               careful not to get fooled into reducing the ARGS in
               careful not to get fooled into reducing the ARGS in
               situations like:
               situations like:
 
 
                 template <class T> struct S { template <class U> void f(U); }
                 template <class T> struct S { template <class U> void f(U); }
                 template <class T> template <> void S<T>::f(int) {}
                 template <class T> template <> void S<T>::f(int) {}
 
 
               which we can spot because the pattern will be a
               which we can spot because the pattern will be a
               specialization in this case.  */
               specialization in this case.  */
            args_depth = TMPL_ARGS_DEPTH (args);
            args_depth = TMPL_ARGS_DEPTH (args);
            parms_depth =
            parms_depth =
              TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
              TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
            if (args_depth > parms_depth
            if (args_depth > parms_depth
                && !DECL_TEMPLATE_SPECIALIZATION (t))
                && !DECL_TEMPLATE_SPECIALIZATION (t))
              args = get_innermost_template_args (args, parms_depth);
              args = get_innermost_template_args (args, parms_depth);
          }
          }
        else
        else
          {
          {
            /* This special case arises when we have something like this:
            /* This special case arises when we have something like this:
 
 
                 template <class T> struct S {
                 template <class T> struct S {
                   friend void f<int>(int, double);
                   friend void f<int>(int, double);
                 };
                 };
 
 
               Here, the DECL_TI_TEMPLATE for the friend declaration
               Here, the DECL_TI_TEMPLATE for the friend declaration
               will be an IDENTIFIER_NODE.  We are being called from
               will be an IDENTIFIER_NODE.  We are being called from
               tsubst_friend_function, and we want only to create a
               tsubst_friend_function, and we want only to create a
               new decl (R) with appropriate types so that we can call
               new decl (R) with appropriate types so that we can call
               determine_specialization.  */
               determine_specialization.  */
            gen_tmpl = NULL_TREE;
            gen_tmpl = NULL_TREE;
          }
          }
 
 
        if (DECL_CLASS_SCOPE_P (t))
        if (DECL_CLASS_SCOPE_P (t))
          {
          {
            if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
            if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
              member = 2;
              member = 2;
            else
            else
              member = 1;
              member = 1;
            ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
            ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
                                    complain, t, /*entering_scope=*/1);
                                    complain, t, /*entering_scope=*/1);
          }
          }
        else
        else
          {
          {
            member = 0;
            member = 0;
            ctx = DECL_CONTEXT (t);
            ctx = DECL_CONTEXT (t);
          }
          }
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        if (type == error_mark_node)
        if (type == error_mark_node)
          RETURN (error_mark_node);
          RETURN (error_mark_node);
 
 
        /* We do NOT check for matching decls pushed separately at this
        /* We do NOT check for matching decls pushed separately at this
           point, as they may not represent instantiations of this
           point, as they may not represent instantiations of this
           template, and in any case are considered separate under the
           template, and in any case are considered separate under the
           discrete model.  */
           discrete model.  */
        r = copy_decl (t);
        r = copy_decl (t);
        DECL_USE_TEMPLATE (r) = 0;
        DECL_USE_TEMPLATE (r) = 0;
        TREE_TYPE (r) = type;
        TREE_TYPE (r) = type;
        /* Clear out the mangled name and RTL for the instantiation.  */
        /* Clear out the mangled name and RTL for the instantiation.  */
        SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
        SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
        SET_DECL_RTL (r, NULL_RTX);
        SET_DECL_RTL (r, NULL_RTX);
        /* Leave DECL_INITIAL set on deleted instantiations.  */
        /* Leave DECL_INITIAL set on deleted instantiations.  */
        if (!DECL_DELETED_FN (r))
        if (!DECL_DELETED_FN (r))
          DECL_INITIAL (r) = NULL_TREE;
          DECL_INITIAL (r) = NULL_TREE;
        DECL_CONTEXT (r) = ctx;
        DECL_CONTEXT (r) = ctx;
 
 
        if (member && DECL_CONV_FN_P (r))
        if (member && DECL_CONV_FN_P (r))
          /* Type-conversion operator.  Reconstruct the name, in
          /* Type-conversion operator.  Reconstruct the name, in
             case it's the name of one of the template's parameters.  */
             case it's the name of one of the template's parameters.  */
          DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
          DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
 
 
        DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
        DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
                                     complain, t);
                                     complain, t);
        DECL_RESULT (r) = NULL_TREE;
        DECL_RESULT (r) = NULL_TREE;
 
 
        TREE_STATIC (r) = 0;
        TREE_STATIC (r) = 0;
        TREE_PUBLIC (r) = TREE_PUBLIC (t);
        TREE_PUBLIC (r) = TREE_PUBLIC (t);
        DECL_EXTERNAL (r) = 1;
        DECL_EXTERNAL (r) = 1;
        /* If this is an instantiation of a function with internal
        /* If this is an instantiation of a function with internal
           linkage, we already know what object file linkage will be
           linkage, we already know what object file linkage will be
           assigned to the instantiation.  */
           assigned to the instantiation.  */
        DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
        DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
        DECL_DEFER_OUTPUT (r) = 0;
        DECL_DEFER_OUTPUT (r) = 0;
        TREE_CHAIN (r) = NULL_TREE;
        TREE_CHAIN (r) = NULL_TREE;
        DECL_PENDING_INLINE_INFO (r) = 0;
        DECL_PENDING_INLINE_INFO (r) = 0;
        DECL_PENDING_INLINE_P (r) = 0;
        DECL_PENDING_INLINE_P (r) = 0;
        DECL_SAVED_TREE (r) = NULL_TREE;
        DECL_SAVED_TREE (r) = NULL_TREE;
        DECL_STRUCT_FUNCTION (r) = NULL;
        DECL_STRUCT_FUNCTION (r) = NULL;
        TREE_USED (r) = 0;
        TREE_USED (r) = 0;
        /* We'll re-clone as appropriate in instantiate_template.  */
        /* We'll re-clone as appropriate in instantiate_template.  */
        DECL_CLONED_FUNCTION (r) = NULL_TREE;
        DECL_CLONED_FUNCTION (r) = NULL_TREE;
 
 
        /* If we aren't complaining now, return on error before we register
        /* If we aren't complaining now, return on error before we register
           the specialization so that we'll complain eventually.  */
           the specialization so that we'll complain eventually.  */
        if ((complain & tf_error) == 0
        if ((complain & tf_error) == 0
            && IDENTIFIER_OPNAME_P (DECL_NAME (r))
            && IDENTIFIER_OPNAME_P (DECL_NAME (r))
            && !grok_op_properties (r, /*complain=*/false))
            && !grok_op_properties (r, /*complain=*/false))
          RETURN (error_mark_node);
          RETURN (error_mark_node);
 
 
        /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
        /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
           this in the special friend case mentioned above where
           this in the special friend case mentioned above where
           GEN_TMPL is NULL.  */
           GEN_TMPL is NULL.  */
        if (gen_tmpl)
        if (gen_tmpl)
          {
          {
            DECL_TEMPLATE_INFO (r)
            DECL_TEMPLATE_INFO (r)
              = build_template_info (gen_tmpl, argvec);
              = build_template_info (gen_tmpl, argvec);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
            register_specialization (r, gen_tmpl, argvec, false, hash);
            register_specialization (r, gen_tmpl, argvec, false, hash);
 
 
            /* We're not supposed to instantiate default arguments
            /* We're not supposed to instantiate default arguments
               until they are called, for a template.  But, for a
               until they are called, for a template.  But, for a
               declaration like:
               declaration like:
 
 
                 template <class T> void f ()
                 template <class T> void f ()
                 { extern void g(int i = T()); }
                 { extern void g(int i = T()); }
 
 
               we should do the substitution when the template is
               we should do the substitution when the template is
               instantiated.  We handle the member function case in
               instantiated.  We handle the member function case in
               instantiate_class_template since the default arguments
               instantiate_class_template since the default arguments
               might refer to other members of the class.  */
               might refer to other members of the class.  */
            if (!member
            if (!member
                && !PRIMARY_TEMPLATE_P (gen_tmpl)
                && !PRIMARY_TEMPLATE_P (gen_tmpl)
                && !uses_template_parms (argvec))
                && !uses_template_parms (argvec))
              tsubst_default_arguments (r);
              tsubst_default_arguments (r);
          }
          }
        else
        else
          DECL_TEMPLATE_INFO (r) = NULL_TREE;
          DECL_TEMPLATE_INFO (r) = NULL_TREE;
 
 
        /* Copy the list of befriending classes.  */
        /* Copy the list of befriending classes.  */
        for (friends = &DECL_BEFRIENDING_CLASSES (r);
        for (friends = &DECL_BEFRIENDING_CLASSES (r);
             *friends;
             *friends;
             friends = &TREE_CHAIN (*friends))
             friends = &TREE_CHAIN (*friends))
          {
          {
            *friends = copy_node (*friends);
            *friends = copy_node (*friends);
            TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
            TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
                                            args, complain,
                                            args, complain,
                                            in_decl);
                                            in_decl);
          }
          }
 
 
        if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
        if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
          {
          {
            maybe_retrofit_in_chrg (r);
            maybe_retrofit_in_chrg (r);
            if (DECL_CONSTRUCTOR_P (r))
            if (DECL_CONSTRUCTOR_P (r))
              grok_ctor_properties (ctx, r);
              grok_ctor_properties (ctx, r);
            /* If this is an instantiation of a member template, clone it.
            /* If this is an instantiation of a member template, clone it.
               If it isn't, that'll be handled by
               If it isn't, that'll be handled by
               clone_constructors_and_destructors.  */
               clone_constructors_and_destructors.  */
            if (PRIMARY_TEMPLATE_P (gen_tmpl))
            if (PRIMARY_TEMPLATE_P (gen_tmpl))
              clone_function_decl (r, /*update_method_vec_p=*/0);
              clone_function_decl (r, /*update_method_vec_p=*/0);
          }
          }
        else if ((complain & tf_error) != 0
        else if ((complain & tf_error) != 0
                 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
                 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
                 && !grok_op_properties (r, /*complain=*/true))
                 && !grok_op_properties (r, /*complain=*/true))
          RETURN (error_mark_node);
          RETURN (error_mark_node);
 
 
        if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
        if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
          SET_DECL_FRIEND_CONTEXT (r,
          SET_DECL_FRIEND_CONTEXT (r,
                                   tsubst (DECL_FRIEND_CONTEXT (t),
                                   tsubst (DECL_FRIEND_CONTEXT (t),
                                            args, complain, in_decl));
                                            args, complain, in_decl));
 
 
        /* Possibly limit visibility based on template args.  */
        /* Possibly limit visibility based on template args.  */
        DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
        DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
        if (DECL_VISIBILITY_SPECIFIED (t))
        if (DECL_VISIBILITY_SPECIFIED (t))
          {
          {
            DECL_VISIBILITY_SPECIFIED (r) = 0;
            DECL_VISIBILITY_SPECIFIED (r) = 0;
            DECL_ATTRIBUTES (r)
            DECL_ATTRIBUTES (r)
              = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
              = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
          }
          }
        determine_visibility (r);
        determine_visibility (r);
        if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
        if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
            && !processing_template_decl)
            && !processing_template_decl)
          defaulted_late_check (r);
          defaulted_late_check (r);
 
 
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
                                        args, complain, in_decl);
                                        args, complain, in_decl);
      }
      }
      break;
      break;
 
 
    case PARM_DECL:
    case PARM_DECL:
      {
      {
        tree type = NULL_TREE;
        tree type = NULL_TREE;
        int i, len = 1;
        int i, len = 1;
        tree expanded_types = NULL_TREE;
        tree expanded_types = NULL_TREE;
        tree prev_r = NULL_TREE;
        tree prev_r = NULL_TREE;
        tree first_r = NULL_TREE;
        tree first_r = NULL_TREE;
 
 
        if (FUNCTION_PARAMETER_PACK_P (t))
        if (FUNCTION_PARAMETER_PACK_P (t))
          {
          {
            /* If there is a local specialization that isn't a
            /* If there is a local specialization that isn't a
               parameter pack, it means that we're doing a "simple"
               parameter pack, it means that we're doing a "simple"
               substitution from inside tsubst_pack_expansion. Just
               substitution from inside tsubst_pack_expansion. Just
               return the local specialization (which will be a single
               return the local specialization (which will be a single
               parm).  */
               parm).  */
            tree spec = retrieve_local_specialization (t);
            tree spec = retrieve_local_specialization (t);
            if (spec
            if (spec
                && TREE_CODE (spec) == PARM_DECL
                && TREE_CODE (spec) == PARM_DECL
                && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
                && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
              RETURN (spec);
              RETURN (spec);
 
 
            /* Expand the TYPE_PACK_EXPANSION that provides the types for
            /* Expand the TYPE_PACK_EXPANSION that provides the types for
               the parameters in this function parameter pack.  */
               the parameters in this function parameter pack.  */
            expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
            expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
                                                    complain, in_decl);
                                                    complain, in_decl);
            if (TREE_CODE (expanded_types) == TREE_VEC)
            if (TREE_CODE (expanded_types) == TREE_VEC)
              {
              {
                len = TREE_VEC_LENGTH (expanded_types);
                len = TREE_VEC_LENGTH (expanded_types);
 
 
                /* Zero-length parameter packs are boring. Just substitute
                /* Zero-length parameter packs are boring. Just substitute
                   into the chain.  */
                   into the chain.  */
                if (len == 0)
                if (len == 0)
                  RETURN (tsubst (TREE_CHAIN (t), args, complain,
                  RETURN (tsubst (TREE_CHAIN (t), args, complain,
                                  TREE_CHAIN (t)));
                                  TREE_CHAIN (t)));
              }
              }
            else
            else
              {
              {
                /* All we did was update the type. Make a note of that.  */
                /* All we did was update the type. Make a note of that.  */
                type = expanded_types;
                type = expanded_types;
                expanded_types = NULL_TREE;
                expanded_types = NULL_TREE;
              }
              }
          }
          }
 
 
        /* Loop through all of the parameter's we'll build. When T is
        /* Loop through all of the parameter's we'll build. When T is
           a function parameter pack, LEN is the number of expanded
           a function parameter pack, LEN is the number of expanded
           types in EXPANDED_TYPES; otherwise, LEN is 1.  */
           types in EXPANDED_TYPES; otherwise, LEN is 1.  */
        r = NULL_TREE;
        r = NULL_TREE;
        for (i = 0; i < len; ++i)
        for (i = 0; i < len; ++i)
          {
          {
            prev_r = r;
            prev_r = r;
            r = copy_node (t);
            r = copy_node (t);
            if (DECL_TEMPLATE_PARM_P (t))
            if (DECL_TEMPLATE_PARM_P (t))
              SET_DECL_TEMPLATE_PARM_P (r);
              SET_DECL_TEMPLATE_PARM_P (r);
 
 
            /* An argument of a function parameter pack is not a parameter
            /* An argument of a function parameter pack is not a parameter
               pack.  */
               pack.  */
            FUNCTION_PARAMETER_PACK_P (r) = false;
            FUNCTION_PARAMETER_PACK_P (r) = false;
 
 
            if (expanded_types)
            if (expanded_types)
              /* We're on the Ith parameter of the function parameter
              /* We're on the Ith parameter of the function parameter
                 pack.  */
                 pack.  */
              {
              {
                /* Get the Ith type.  */
                /* Get the Ith type.  */
                type = TREE_VEC_ELT (expanded_types, i);
                type = TREE_VEC_ELT (expanded_types, i);
 
 
                if (DECL_NAME (r))
                if (DECL_NAME (r))
                  /* Rename the parameter to include the index.  */
                  /* Rename the parameter to include the index.  */
                  DECL_NAME (r) =
                  DECL_NAME (r) =
                    make_ith_pack_parameter_name (DECL_NAME (r), i);
                    make_ith_pack_parameter_name (DECL_NAME (r), i);
              }
              }
            else if (!type)
            else if (!type)
              /* We're dealing with a normal parameter.  */
              /* We're dealing with a normal parameter.  */
              type = tsubst (TREE_TYPE (t), args, complain, in_decl);
              type = tsubst (TREE_TYPE (t), args, complain, in_decl);
 
 
            type = type_decays_to (type);
            type = type_decays_to (type);
            TREE_TYPE (r) = type;
            TREE_TYPE (r) = type;
            cp_apply_type_quals_to_decl (cp_type_quals (type), r);
            cp_apply_type_quals_to_decl (cp_type_quals (type), r);
 
 
            if (DECL_INITIAL (r))
            if (DECL_INITIAL (r))
              {
              {
                if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
                if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
                  DECL_INITIAL (r) = TREE_TYPE (r);
                  DECL_INITIAL (r) = TREE_TYPE (r);
                else
                else
                  DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
                  DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
                                             complain, in_decl);
                                             complain, in_decl);
              }
              }
 
 
            DECL_CONTEXT (r) = NULL_TREE;
            DECL_CONTEXT (r) = NULL_TREE;
 
 
            if (!DECL_TEMPLATE_PARM_P (r))
            if (!DECL_TEMPLATE_PARM_P (r))
              DECL_ARG_TYPE (r) = type_passed_as (type);
              DECL_ARG_TYPE (r) = type_passed_as (type);
 
 
            apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
            apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
                                            args, complain, in_decl);
                                            args, complain, in_decl);
 
 
            /* Keep track of the first new parameter we
            /* Keep track of the first new parameter we
               generate. That's what will be returned to the
               generate. That's what will be returned to the
               caller.  */
               caller.  */
            if (!first_r)
            if (!first_r)
              first_r = r;
              first_r = r;
 
 
            /* Build a proper chain of parameters when substituting
            /* Build a proper chain of parameters when substituting
               into a function parameter pack.  */
               into a function parameter pack.  */
            if (prev_r)
            if (prev_r)
              TREE_CHAIN (prev_r) = r;
              TREE_CHAIN (prev_r) = r;
          }
          }
 
 
        if (TREE_CHAIN (t))
        if (TREE_CHAIN (t))
          TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
          TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
                                   complain, TREE_CHAIN (t));
                                   complain, TREE_CHAIN (t));
 
 
        /* FIRST_R contains the start of the chain we've built.  */
        /* FIRST_R contains the start of the chain we've built.  */
        r = first_r;
        r = first_r;
      }
      }
      break;
      break;
 
 
    case FIELD_DECL:
    case FIELD_DECL:
      {
      {
        tree type;
        tree type;
 
 
        r = copy_decl (t);
        r = copy_decl (t);
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        if (type == error_mark_node)
        if (type == error_mark_node)
          RETURN (error_mark_node);
          RETURN (error_mark_node);
        TREE_TYPE (r) = type;
        TREE_TYPE (r) = type;
        cp_apply_type_quals_to_decl (cp_type_quals (type), r);
        cp_apply_type_quals_to_decl (cp_type_quals (type), r);
 
 
        /* DECL_INITIAL gives the number of bits in a bit-field.  */
        /* DECL_INITIAL gives the number of bits in a bit-field.  */
        DECL_INITIAL (r)
        DECL_INITIAL (r)
          = tsubst_expr (DECL_INITIAL (t), args,
          = tsubst_expr (DECL_INITIAL (t), args,
                         complain, in_decl,
                         complain, in_decl,
                         /*integral_constant_expression_p=*/true);
                         /*integral_constant_expression_p=*/true);
        /* We don't have to set DECL_CONTEXT here; it is set by
        /* We don't have to set DECL_CONTEXT here; it is set by
           finish_member_declaration.  */
           finish_member_declaration.  */
        TREE_CHAIN (r) = NULL_TREE;
        TREE_CHAIN (r) = NULL_TREE;
        if (VOID_TYPE_P (type))
        if (VOID_TYPE_P (type))
          error ("instantiation of %q+D as type %qT", r, type);
          error ("instantiation of %q+D as type %qT", r, type);
 
 
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
                                        args, complain, in_decl);
                                        args, complain, in_decl);
      }
      }
      break;
      break;
 
 
    case USING_DECL:
    case USING_DECL:
      /* We reach here only for member using decls.  */
      /* We reach here only for member using decls.  */
      if (DECL_DEPENDENT_P (t))
      if (DECL_DEPENDENT_P (t))
        {
        {
          r = do_class_using_decl
          r = do_class_using_decl
            (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
            (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
             tsubst_copy (DECL_NAME (t), args, complain, in_decl));
             tsubst_copy (DECL_NAME (t), args, complain, in_decl));
          if (!r)
          if (!r)
            r = error_mark_node;
            r = error_mark_node;
          else
          else
            {
            {
              TREE_PROTECTED (r) = TREE_PROTECTED (t);
              TREE_PROTECTED (r) = TREE_PROTECTED (t);
              TREE_PRIVATE (r) = TREE_PRIVATE (t);
              TREE_PRIVATE (r) = TREE_PRIVATE (t);
            }
            }
        }
        }
      else
      else
        {
        {
          r = copy_node (t);
          r = copy_node (t);
          TREE_CHAIN (r) = NULL_TREE;
          TREE_CHAIN (r) = NULL_TREE;
        }
        }
      break;
      break;
 
 
    case TYPE_DECL:
    case TYPE_DECL:
    case VAR_DECL:
    case VAR_DECL:
      {
      {
        tree argvec = NULL_TREE;
        tree argvec = NULL_TREE;
        tree gen_tmpl = NULL_TREE;
        tree gen_tmpl = NULL_TREE;
        tree spec;
        tree spec;
        tree tmpl = NULL_TREE;
        tree tmpl = NULL_TREE;
        tree ctx;
        tree ctx;
        tree type = NULL_TREE;
        tree type = NULL_TREE;
        bool local_p;
        bool local_p;
 
 
        if (TREE_CODE (t) == TYPE_DECL
        if (TREE_CODE (t) == TYPE_DECL
            && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
            && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
          {
          {
            /* If this is the canonical decl, we don't have to
            /* If this is the canonical decl, we don't have to
               mess with instantiations, and often we can't (for
               mess with instantiations, and often we can't (for
               typename, template type parms and such).  Note that
               typename, template type parms and such).  Note that
               TYPE_NAME is not correct for the above test if
               TYPE_NAME is not correct for the above test if
               we've copied the type for a typedef.  */
               we've copied the type for a typedef.  */
            type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            if (type == error_mark_node)
            if (type == error_mark_node)
              RETURN (error_mark_node);
              RETURN (error_mark_node);
            r = TYPE_NAME (type);
            r = TYPE_NAME (type);
            break;
            break;
          }
          }
 
 
        /* Check to see if we already have the specialization we
        /* Check to see if we already have the specialization we
           need.  */
           need.  */
        spec = NULL_TREE;
        spec = NULL_TREE;
        if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
        if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
          {
          {
            /* T is a static data member or namespace-scope entity.
            /* T is a static data member or namespace-scope entity.
               We have to substitute into namespace-scope variables
               We have to substitute into namespace-scope variables
               (even though such entities are never templates) because
               (even though such entities are never templates) because
               of cases like:
               of cases like:
 
 
                 template <class T> void f() { extern T t; }
                 template <class T> void f() { extern T t; }
 
 
               where the entity referenced is not known until
               where the entity referenced is not known until
               instantiation time.  */
               instantiation time.  */
            local_p = false;
            local_p = false;
            ctx = DECL_CONTEXT (t);
            ctx = DECL_CONTEXT (t);
            if (DECL_CLASS_SCOPE_P (t))
            if (DECL_CLASS_SCOPE_P (t))
              {
              {
                ctx = tsubst_aggr_type (ctx, args,
                ctx = tsubst_aggr_type (ctx, args,
                                        complain,
                                        complain,
                                        in_decl, /*entering_scope=*/1);
                                        in_decl, /*entering_scope=*/1);
                /* If CTX is unchanged, then T is in fact the
                /* If CTX is unchanged, then T is in fact the
                   specialization we want.  That situation occurs when
                   specialization we want.  That situation occurs when
                   referencing a static data member within in its own
                   referencing a static data member within in its own
                   class.  We can use pointer equality, rather than
                   class.  We can use pointer equality, rather than
                   same_type_p, because DECL_CONTEXT is always
                   same_type_p, because DECL_CONTEXT is always
                   canonical.  */
                   canonical.  */
                if (ctx == DECL_CONTEXT (t))
                if (ctx == DECL_CONTEXT (t))
                  spec = t;
                  spec = t;
              }
              }
 
 
            if (!spec)
            if (!spec)
              {
              {
                tmpl = DECL_TI_TEMPLATE (t);
                tmpl = DECL_TI_TEMPLATE (t);
                gen_tmpl = most_general_template (tmpl);
                gen_tmpl = most_general_template (tmpl);
                argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
                argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
                hash = hash_tmpl_and_args (gen_tmpl, argvec);
                hash = hash_tmpl_and_args (gen_tmpl, argvec);
                spec = retrieve_specialization (gen_tmpl, argvec, hash);
                spec = retrieve_specialization (gen_tmpl, argvec, hash);
              }
              }
          }
          }
        else
        else
          {
          {
            /* A local variable.  */
            /* A local variable.  */
            local_p = true;
            local_p = true;
            /* Subsequent calls to pushdecl will fill this in.  */
            /* Subsequent calls to pushdecl will fill this in.  */
            ctx = NULL_TREE;
            ctx = NULL_TREE;
            spec = retrieve_local_specialization (t);
            spec = retrieve_local_specialization (t);
          }
          }
        /* If we already have the specialization we need, there is
        /* If we already have the specialization we need, there is
           nothing more to do.  */
           nothing more to do.  */
        if (spec)
        if (spec)
          {
          {
            r = spec;
            r = spec;
            break;
            break;
          }
          }
 
 
        /* Create a new node for the specialization we need.  */
        /* Create a new node for the specialization we need.  */
        r = copy_decl (t);
        r = copy_decl (t);
        if (type == NULL_TREE)
        if (type == NULL_TREE)
          {
          {
            if (is_typedef_decl (t))
            if (is_typedef_decl (t))
              type = DECL_ORIGINAL_TYPE (t);
              type = DECL_ORIGINAL_TYPE (t);
            else
            else
              type = TREE_TYPE (t);
              type = TREE_TYPE (t);
            type = tsubst (type, args, complain, in_decl);
            type = tsubst (type, args, complain, in_decl);
          }
          }
        if (TREE_CODE (r) == VAR_DECL)
        if (TREE_CODE (r) == VAR_DECL)
          {
          {
            /* Even if the original location is out of scope, the
            /* Even if the original location is out of scope, the
               newly substituted one is not.  */
               newly substituted one is not.  */
            DECL_DEAD_FOR_LOCAL (r) = 0;
            DECL_DEAD_FOR_LOCAL (r) = 0;
            DECL_INITIALIZED_P (r) = 0;
            DECL_INITIALIZED_P (r) = 0;
            DECL_TEMPLATE_INSTANTIATED (r) = 0;
            DECL_TEMPLATE_INSTANTIATED (r) = 0;
            if (type == error_mark_node)
            if (type == error_mark_node)
              RETURN (error_mark_node);
              RETURN (error_mark_node);
            if (TREE_CODE (type) == FUNCTION_TYPE)
            if (TREE_CODE (type) == FUNCTION_TYPE)
              {
              {
                /* It may seem that this case cannot occur, since:
                /* It may seem that this case cannot occur, since:
 
 
                     typedef void f();
                     typedef void f();
                     void g() { f x; }
                     void g() { f x; }
 
 
                   declares a function, not a variable.  However:
                   declares a function, not a variable.  However:
 
 
                     typedef void f();
                     typedef void f();
                     template <typename T> void g() { T t; }
                     template <typename T> void g() { T t; }
                     template void g<f>();
                     template void g<f>();
 
 
                   is an attempt to declare a variable with function
                   is an attempt to declare a variable with function
                   type.  */
                   type.  */
                error ("variable %qD has function type",
                error ("variable %qD has function type",
                       /* R is not yet sufficiently initialized, so we
                       /* R is not yet sufficiently initialized, so we
                          just use its name.  */
                          just use its name.  */
                       DECL_NAME (r));
                       DECL_NAME (r));
                RETURN (error_mark_node);
                RETURN (error_mark_node);
              }
              }
            type = complete_type (type);
            type = complete_type (type);
            DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
            DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
              = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
              = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
            type = check_var_type (DECL_NAME (r), type);
            type = check_var_type (DECL_NAME (r), type);
 
 
            if (DECL_HAS_VALUE_EXPR_P (t))
            if (DECL_HAS_VALUE_EXPR_P (t))
              {
              {
                tree ve = DECL_VALUE_EXPR (t);
                tree ve = DECL_VALUE_EXPR (t);
                ve = tsubst_expr (ve, args, complain, in_decl,
                ve = tsubst_expr (ve, args, complain, in_decl,
                                  /*constant_expression_p=*/false);
                                  /*constant_expression_p=*/false);
                SET_DECL_VALUE_EXPR (r, ve);
                SET_DECL_VALUE_EXPR (r, ve);
              }
              }
          }
          }
        else if (DECL_SELF_REFERENCE_P (t))
        else if (DECL_SELF_REFERENCE_P (t))
          SET_DECL_SELF_REFERENCE_P (r);
          SET_DECL_SELF_REFERENCE_P (r);
        TREE_TYPE (r) = type;
        TREE_TYPE (r) = type;
        cp_apply_type_quals_to_decl (cp_type_quals (type), r);
        cp_apply_type_quals_to_decl (cp_type_quals (type), r);
        DECL_CONTEXT (r) = ctx;
        DECL_CONTEXT (r) = ctx;
        /* Clear out the mangled name and RTL for the instantiation.  */
        /* Clear out the mangled name and RTL for the instantiation.  */
        SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
        SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
          SET_DECL_RTL (r, NULL_RTX);
          SET_DECL_RTL (r, NULL_RTX);
        /* The initializer must not be expanded until it is required;
        /* The initializer must not be expanded until it is required;
           see [temp.inst].  */
           see [temp.inst].  */
        DECL_INITIAL (r) = NULL_TREE;
        DECL_INITIAL (r) = NULL_TREE;
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
          SET_DECL_RTL (r, NULL_RTX);
          SET_DECL_RTL (r, NULL_RTX);
        DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
        DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
        if (TREE_CODE (r) == VAR_DECL)
        if (TREE_CODE (r) == VAR_DECL)
          {
          {
            /* Possibly limit visibility based on template args.  */
            /* Possibly limit visibility based on template args.  */
            DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
            DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
            if (DECL_VISIBILITY_SPECIFIED (t))
            if (DECL_VISIBILITY_SPECIFIED (t))
              {
              {
                DECL_VISIBILITY_SPECIFIED (r) = 0;
                DECL_VISIBILITY_SPECIFIED (r) = 0;
                DECL_ATTRIBUTES (r)
                DECL_ATTRIBUTES (r)
                  = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
                  = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
              }
              }
            determine_visibility (r);
            determine_visibility (r);
          }
          }
 
 
        if (!local_p)
        if (!local_p)
          {
          {
            /* A static data member declaration is always marked
            /* A static data member declaration is always marked
               external when it is declared in-class, even if an
               external when it is declared in-class, even if an
               initializer is present.  We mimic the non-template
               initializer is present.  We mimic the non-template
               processing here.  */
               processing here.  */
            DECL_EXTERNAL (r) = 1;
            DECL_EXTERNAL (r) = 1;
 
 
            register_specialization (r, gen_tmpl, argvec, false, hash);
            register_specialization (r, gen_tmpl, argvec, false, hash);
            DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
            DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
          }
          }
        else if (cp_unevaluated_operand)
        else if (cp_unevaluated_operand)
          {
          {
            /* We're substituting this var in a decltype outside of its
            /* We're substituting this var in a decltype outside of its
               scope, such as for a lambda return type.  Don't add it to
               scope, such as for a lambda return type.  Don't add it to
               local_specializations, do perform auto deduction.  */
               local_specializations, do perform auto deduction.  */
            tree auto_node = type_uses_auto (type);
            tree auto_node = type_uses_auto (type);
            tree init
            tree init
              = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
              = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
                             /*constant_expression_p=*/false);
                             /*constant_expression_p=*/false);
 
 
            if (auto_node && init && describable_type (init))
            if (auto_node && init && describable_type (init))
              {
              {
                type = do_auto_deduction (type, init, auto_node);
                type = do_auto_deduction (type, init, auto_node);
                TREE_TYPE (r) = type;
                TREE_TYPE (r) = type;
              }
              }
          }
          }
        else
        else
          register_local_specialization (r, t);
          register_local_specialization (r, t);
 
 
        TREE_CHAIN (r) = NULL_TREE;
        TREE_CHAIN (r) = NULL_TREE;
 
 
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
                                        /*flags=*/0,
                                        /*flags=*/0,
                                        args, complain, in_decl);
                                        args, complain, in_decl);
 
 
        /* Preserve a typedef that names a type.  */
        /* Preserve a typedef that names a type.  */
        if (is_typedef_decl (r))
        if (is_typedef_decl (r))
          {
          {
            DECL_ORIGINAL_TYPE (r) = NULL_TREE;
            DECL_ORIGINAL_TYPE (r) = NULL_TREE;
            set_underlying_type (r);
            set_underlying_type (r);
          }
          }
 
 
        layout_decl (r, 0);
        layout_decl (r, 0);
      }
      }
      break;
      break;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
#undef RETURN
#undef RETURN
 
 
 out:
 out:
  /* Restore the file and line information.  */
  /* Restore the file and line information.  */
  input_location = saved_loc;
  input_location = saved_loc;
 
 
  return r;
  return r;
}
}
 
 
/* Substitute into the ARG_TYPES of a function type.  */
/* Substitute into the ARG_TYPES of a function type.  */
 
 
static tree
static tree
tsubst_arg_types (tree arg_types,
tsubst_arg_types (tree arg_types,
                  tree args,
                  tree args,
                  tsubst_flags_t complain,
                  tsubst_flags_t complain,
                  tree in_decl)
                  tree in_decl)
{
{
  tree remaining_arg_types;
  tree remaining_arg_types;
  tree type = NULL_TREE;
  tree type = NULL_TREE;
  int i = 1;
  int i = 1;
  tree expanded_args = NULL_TREE;
  tree expanded_args = NULL_TREE;
  tree default_arg;
  tree default_arg;
 
 
  if (!arg_types || arg_types == void_list_node)
  if (!arg_types || arg_types == void_list_node)
    return arg_types;
    return arg_types;
 
 
  remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
  remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
                                          args, complain, in_decl);
                                          args, complain, in_decl);
  if (remaining_arg_types == error_mark_node)
  if (remaining_arg_types == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
  if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
    {
    {
      /* For a pack expansion, perform substitution on the
      /* For a pack expansion, perform substitution on the
         entire expression. Later on, we'll handle the arguments
         entire expression. Later on, we'll handle the arguments
         one-by-one.  */
         one-by-one.  */
      expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
      expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
                                            args, complain, in_decl);
                                            args, complain, in_decl);
 
 
      if (TREE_CODE (expanded_args) == TREE_VEC)
      if (TREE_CODE (expanded_args) == TREE_VEC)
        /* So that we'll spin through the parameters, one by one.  */
        /* So that we'll spin through the parameters, one by one.  */
        i = TREE_VEC_LENGTH (expanded_args);
        i = TREE_VEC_LENGTH (expanded_args);
      else
      else
        {
        {
          /* We only partially substituted into the parameter
          /* We only partially substituted into the parameter
             pack. Our type is TYPE_PACK_EXPANSION.  */
             pack. Our type is TYPE_PACK_EXPANSION.  */
          type = expanded_args;
          type = expanded_args;
          expanded_args = NULL_TREE;
          expanded_args = NULL_TREE;
        }
        }
    }
    }
 
 
  while (i > 0) {
  while (i > 0) {
    --i;
    --i;
 
 
    if (expanded_args)
    if (expanded_args)
      type = TREE_VEC_ELT (expanded_args, i);
      type = TREE_VEC_ELT (expanded_args, i);
    else if (!type)
    else if (!type)
      type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
      type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
 
 
    if (type == error_mark_node)
    if (type == error_mark_node)
      return error_mark_node;
      return error_mark_node;
    if (VOID_TYPE_P (type))
    if (VOID_TYPE_P (type))
      {
      {
        if (complain & tf_error)
        if (complain & tf_error)
          {
          {
            error ("invalid parameter type %qT", type);
            error ("invalid parameter type %qT", type);
            if (in_decl)
            if (in_decl)
              error ("in declaration %q+D", in_decl);
              error ("in declaration %q+D", in_decl);
          }
          }
        return error_mark_node;
        return error_mark_node;
    }
    }
 
 
    /* Do array-to-pointer, function-to-pointer conversion, and ignore
    /* Do array-to-pointer, function-to-pointer conversion, and ignore
       top-level qualifiers as required.  */
       top-level qualifiers as required.  */
    type = TYPE_MAIN_VARIANT (type_decays_to (type));
    type = TYPE_MAIN_VARIANT (type_decays_to (type));
 
 
    /* We do not substitute into default arguments here.  The standard
    /* We do not substitute into default arguments here.  The standard
       mandates that they be instantiated only when needed, which is
       mandates that they be instantiated only when needed, which is
       done in build_over_call.  */
       done in build_over_call.  */
    default_arg = TREE_PURPOSE (arg_types);
    default_arg = TREE_PURPOSE (arg_types);
 
 
    if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
    if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
      {
      {
        /* We've instantiated a template before its default arguments
        /* We've instantiated a template before its default arguments
           have been parsed.  This can happen for a nested template
           have been parsed.  This can happen for a nested template
           class, and is not an error unless we require the default
           class, and is not an error unless we require the default
           argument in a call of this function.  */
           argument in a call of this function.  */
        remaining_arg_types =
        remaining_arg_types =
          tree_cons (default_arg, type, remaining_arg_types);
          tree_cons (default_arg, type, remaining_arg_types);
        VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
        VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
                       remaining_arg_types);
                       remaining_arg_types);
      }
      }
    else
    else
      remaining_arg_types =
      remaining_arg_types =
        hash_tree_cons (default_arg, type, remaining_arg_types);
        hash_tree_cons (default_arg, type, remaining_arg_types);
  }
  }
 
 
  return remaining_arg_types;
  return remaining_arg_types;
}
}
 
 
/* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
/* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   *not* handle the exception-specification for FNTYPE, because the
   *not* handle the exception-specification for FNTYPE, because the
   initial substitution of explicitly provided template parameters
   initial substitution of explicitly provided template parameters
   during argument deduction forbids substitution into the
   during argument deduction forbids substitution into the
   exception-specification:
   exception-specification:
 
 
     [temp.deduct]
     [temp.deduct]
 
 
     All references in the function type of the function template to  the
     All references in the function type of the function template to  the
     corresponding template parameters are replaced by the specified tem-
     corresponding template parameters are replaced by the specified tem-
     plate argument values.  If a substitution in a template parameter or
     plate argument values.  If a substitution in a template parameter or
     in  the function type of the function template results in an invalid
     in  the function type of the function template results in an invalid
     type, type deduction fails.  [Note: The equivalent  substitution  in
     type, type deduction fails.  [Note: The equivalent  substitution  in
     exception specifications is done only when the function is instanti-
     exception specifications is done only when the function is instanti-
     ated, at which point a program is  ill-formed  if  the  substitution
     ated, at which point a program is  ill-formed  if  the  substitution
     results in an invalid type.]  */
     results in an invalid type.]  */
 
 
static tree
static tree
tsubst_function_type (tree t,
tsubst_function_type (tree t,
                      tree args,
                      tree args,
                      tsubst_flags_t complain,
                      tsubst_flags_t complain,
                      tree in_decl)
                      tree in_decl)
{
{
  tree return_type;
  tree return_type;
  tree arg_types;
  tree arg_types;
  tree fntype;
  tree fntype;
 
 
  /* The TYPE_CONTEXT is not used for function/method types.  */
  /* The TYPE_CONTEXT is not used for function/method types.  */
  gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
  gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
 
 
  /* Substitute the return type.  */
  /* Substitute the return type.  */
  return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
  return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
  if (return_type == error_mark_node)
  if (return_type == error_mark_node)
    return error_mark_node;
    return error_mark_node;
  /* The standard does not presently indicate that creation of a
  /* The standard does not presently indicate that creation of a
     function type with an invalid return type is a deduction failure.
     function type with an invalid return type is a deduction failure.
     However, that is clearly analogous to creating an array of "void"
     However, that is clearly analogous to creating an array of "void"
     or a reference to a reference.  This is core issue #486.  */
     or a reference to a reference.  This is core issue #486.  */
  if (TREE_CODE (return_type) == ARRAY_TYPE
  if (TREE_CODE (return_type) == ARRAY_TYPE
      || TREE_CODE (return_type) == FUNCTION_TYPE)
      || TREE_CODE (return_type) == FUNCTION_TYPE)
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        {
        {
          if (TREE_CODE (return_type) == ARRAY_TYPE)
          if (TREE_CODE (return_type) == ARRAY_TYPE)
            error ("function returning an array");
            error ("function returning an array");
          else
          else
            error ("function returning a function");
            error ("function returning a function");
        }
        }
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* Substitute the argument types.  */
  /* Substitute the argument types.  */
  arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
  arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
                                complain, in_decl);
                                complain, in_decl);
  if (arg_types == error_mark_node)
  if (arg_types == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  /* Construct a new type node and return it.  */
  /* Construct a new type node and return it.  */
  if (TREE_CODE (t) == FUNCTION_TYPE)
  if (TREE_CODE (t) == FUNCTION_TYPE)
    fntype = build_function_type (return_type, arg_types);
    fntype = build_function_type (return_type, arg_types);
  else
  else
    {
    {
      tree r = TREE_TYPE (TREE_VALUE (arg_types));
      tree r = TREE_TYPE (TREE_VALUE (arg_types));
      if (! MAYBE_CLASS_TYPE_P (r))
      if (! MAYBE_CLASS_TYPE_P (r))
        {
        {
          /* [temp.deduct]
          /* [temp.deduct]
 
 
             Type deduction may fail for any of the following
             Type deduction may fail for any of the following
             reasons:
             reasons:
 
 
             -- Attempting to create "pointer to member of T" when T
             -- Attempting to create "pointer to member of T" when T
             is not a class type.  */
             is not a class type.  */
          if (complain & tf_error)
          if (complain & tf_error)
            error ("creating pointer to member function of non-class type %qT",
            error ("creating pointer to member function of non-class type %qT",
                      r);
                      r);
          return error_mark_node;
          return error_mark_node;
        }
        }
 
 
      fntype = build_method_type_directly (r, return_type,
      fntype = build_method_type_directly (r, return_type,
                                           TREE_CHAIN (arg_types));
                                           TREE_CHAIN (arg_types));
    }
    }
  fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
  fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
  fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
  fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
 
 
  return fntype;
  return fntype;
}
}
 
 
/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   ARGS into that specification, and return the substituted
   ARGS into that specification, and return the substituted
   specification.  If there is no specification, return NULL_TREE.  */
   specification.  If there is no specification, return NULL_TREE.  */
 
 
static tree
static tree
tsubst_exception_specification (tree fntype,
tsubst_exception_specification (tree fntype,
                                tree args,
                                tree args,
                                tsubst_flags_t complain,
                                tsubst_flags_t complain,
                                tree in_decl)
                                tree in_decl)
{
{
  tree specs;
  tree specs;
  tree new_specs;
  tree new_specs;
 
 
  specs = TYPE_RAISES_EXCEPTIONS (fntype);
  specs = TYPE_RAISES_EXCEPTIONS (fntype);
  new_specs = NULL_TREE;
  new_specs = NULL_TREE;
  if (specs)
  if (specs)
    {
    {
      if (! TREE_VALUE (specs))
      if (! TREE_VALUE (specs))
        new_specs = specs;
        new_specs = specs;
      else
      else
        while (specs)
        while (specs)
          {
          {
            tree spec;
            tree spec;
            int i, len = 1;
            int i, len = 1;
            tree expanded_specs = NULL_TREE;
            tree expanded_specs = NULL_TREE;
 
 
            if (PACK_EXPANSION_P (TREE_VALUE (specs)))
            if (PACK_EXPANSION_P (TREE_VALUE (specs)))
              {
              {
                /* Expand the pack expansion type.  */
                /* Expand the pack expansion type.  */
                expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
                expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
                                                       args, complain,
                                                       args, complain,
                                                       in_decl);
                                                       in_decl);
 
 
                if (expanded_specs == error_mark_node)
                if (expanded_specs == error_mark_node)
                  return error_mark_node;
                  return error_mark_node;
                else if (TREE_CODE (expanded_specs) == TREE_VEC)
                else if (TREE_CODE (expanded_specs) == TREE_VEC)
                  len = TREE_VEC_LENGTH (expanded_specs);
                  len = TREE_VEC_LENGTH (expanded_specs);
                else
                else
                  {
                  {
                    /* We're substituting into a member template, so
                    /* We're substituting into a member template, so
                       we got a TYPE_PACK_EXPANSION back.  Add that
                       we got a TYPE_PACK_EXPANSION back.  Add that
                       expansion and move on.  */
                       expansion and move on.  */
                    gcc_assert (TREE_CODE (expanded_specs)
                    gcc_assert (TREE_CODE (expanded_specs)
                                == TYPE_PACK_EXPANSION);
                                == TYPE_PACK_EXPANSION);
                    new_specs = add_exception_specifier (new_specs,
                    new_specs = add_exception_specifier (new_specs,
                                                         expanded_specs,
                                                         expanded_specs,
                                                         complain);
                                                         complain);
                    specs = TREE_CHAIN (specs);
                    specs = TREE_CHAIN (specs);
                    continue;
                    continue;
                  }
                  }
              }
              }
 
 
            for (i = 0; i < len; ++i)
            for (i = 0; i < len; ++i)
              {
              {
                if (expanded_specs)
                if (expanded_specs)
                  spec = TREE_VEC_ELT (expanded_specs, i);
                  spec = TREE_VEC_ELT (expanded_specs, i);
                else
                else
                  spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
                  spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
                if (spec == error_mark_node)
                if (spec == error_mark_node)
                  return spec;
                  return spec;
                new_specs = add_exception_specifier (new_specs, spec,
                new_specs = add_exception_specifier (new_specs, spec,
                                                     complain);
                                                     complain);
              }
              }
 
 
            specs = TREE_CHAIN (specs);
            specs = TREE_CHAIN (specs);
          }
          }
    }
    }
  return new_specs;
  return new_specs;
}
}
 
 
/* Take the tree structure T and replace template parameters used
/* Take the tree structure T and replace template parameters used
   therein with the argument vector ARGS.  IN_DECL is an associated
   therein with the argument vector ARGS.  IN_DECL is an associated
   decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   Issue error and warning messages under control of COMPLAIN.  Note
   Issue error and warning messages under control of COMPLAIN.  Note
   that we must be relatively non-tolerant of extensions here, in
   that we must be relatively non-tolerant of extensions here, in
   order to preserve conformance; if we allow substitutions that
   order to preserve conformance; if we allow substitutions that
   should not be allowed, we may allow argument deductions that should
   should not be allowed, we may allow argument deductions that should
   not succeed, and therefore report ambiguous overload situations
   not succeed, and therefore report ambiguous overload situations
   where there are none.  In theory, we could allow the substitution,
   where there are none.  In theory, we could allow the substitution,
   but indicate that it should have failed, and allow our caller to
   but indicate that it should have failed, and allow our caller to
   make sure that the right thing happens, but we don't try to do this
   make sure that the right thing happens, but we don't try to do this
   yet.
   yet.
 
 
   This function is used for dealing with types, decls and the like;
   This function is used for dealing with types, decls and the like;
   for expressions, use tsubst_expr or tsubst_copy.  */
   for expressions, use tsubst_expr or tsubst_copy.  */
 
 
tree
tree
tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
{
  tree type, r;
  tree type, r;
 
 
  if (t == NULL_TREE || t == error_mark_node
  if (t == NULL_TREE || t == error_mark_node
      || t == integer_type_node
      || t == integer_type_node
      || t == void_type_node
      || t == void_type_node
      || t == char_type_node
      || t == char_type_node
      || t == unknown_type_node
      || t == unknown_type_node
      || TREE_CODE (t) == NAMESPACE_DECL)
      || TREE_CODE (t) == NAMESPACE_DECL)
    return t;
    return t;
 
 
  if (DECL_P (t))
  if (DECL_P (t))
    return tsubst_decl (t, args, complain);
    return tsubst_decl (t, args, complain);
 
 
  if (args == NULL_TREE)
  if (args == NULL_TREE)
    return t;
    return t;
 
 
  if (TREE_CODE (t) == IDENTIFIER_NODE)
  if (TREE_CODE (t) == IDENTIFIER_NODE)
    type = IDENTIFIER_TYPE_VALUE (t);
    type = IDENTIFIER_TYPE_VALUE (t);
  else
  else
    type = TREE_TYPE (t);
    type = TREE_TYPE (t);
 
 
  gcc_assert (type != unknown_type_node);
  gcc_assert (type != unknown_type_node);
 
 
  /* Reuse typedefs.  We need to do this to handle dependent attributes,
  /* Reuse typedefs.  We need to do this to handle dependent attributes,
     such as attribute aligned.  */
     such as attribute aligned.  */
  if (TYPE_P (t)
  if (TYPE_P (t)
      && TYPE_NAME (t)
      && TYPE_NAME (t)
      && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
      && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
    {
    {
      tree decl = TYPE_NAME (t);
      tree decl = TYPE_NAME (t);
 
 
      if (DECL_CLASS_SCOPE_P (decl)
      if (DECL_CLASS_SCOPE_P (decl)
          && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
          && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
          && uses_template_parms (DECL_CONTEXT (decl)))
          && uses_template_parms (DECL_CONTEXT (decl)))
        {
        {
          tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
          tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
          tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
          tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
          r = retrieve_specialization (tmpl, gen_args, 0);
          r = retrieve_specialization (tmpl, gen_args, 0);
        }
        }
      else if (DECL_FUNCTION_SCOPE_P (decl)
      else if (DECL_FUNCTION_SCOPE_P (decl)
               && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
               && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
               && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
               && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
        r = retrieve_local_specialization (decl);
        r = retrieve_local_specialization (decl);
      else
      else
        /* The typedef is from a non-template context.  */
        /* The typedef is from a non-template context.  */
        return t;
        return t;
 
 
      if (r)
      if (r)
        {
        {
          r = TREE_TYPE (r);
          r = TREE_TYPE (r);
          r = cp_build_qualified_type_real
          r = cp_build_qualified_type_real
            (r, cp_type_quals (t) | cp_type_quals (r),
            (r, cp_type_quals (t) | cp_type_quals (r),
             complain | tf_ignore_bad_quals);
             complain | tf_ignore_bad_quals);
          return r;
          return r;
        }
        }
      /* Else we must be instantiating the typedef, so fall through.  */
      /* Else we must be instantiating the typedef, so fall through.  */
    }
    }
 
 
  if (type
  if (type
      && TREE_CODE (t) != TYPENAME_TYPE
      && TREE_CODE (t) != TYPENAME_TYPE
      && TREE_CODE (t) != TEMPLATE_TYPE_PARM
      && TREE_CODE (t) != TEMPLATE_TYPE_PARM
      && TREE_CODE (t) != IDENTIFIER_NODE
      && TREE_CODE (t) != IDENTIFIER_NODE
      && TREE_CODE (t) != FUNCTION_TYPE
      && TREE_CODE (t) != FUNCTION_TYPE
      && TREE_CODE (t) != METHOD_TYPE)
      && TREE_CODE (t) != METHOD_TYPE)
    type = tsubst (type, args, complain, in_decl);
    type = tsubst (type, args, complain, in_decl);
  if (type == error_mark_node)
  if (type == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case RECORD_TYPE:
    case RECORD_TYPE:
    case UNION_TYPE:
    case UNION_TYPE:
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
      return tsubst_aggr_type (t, args, complain, in_decl,
      return tsubst_aggr_type (t, args, complain, in_decl,
                               /*entering_scope=*/0);
                               /*entering_scope=*/0);
 
 
    case ERROR_MARK:
    case ERROR_MARK:
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
    case VOID_TYPE:
    case VOID_TYPE:
    case REAL_TYPE:
    case REAL_TYPE:
    case COMPLEX_TYPE:
    case COMPLEX_TYPE:
    case VECTOR_TYPE:
    case VECTOR_TYPE:
    case BOOLEAN_TYPE:
    case BOOLEAN_TYPE:
    case INTEGER_CST:
    case INTEGER_CST:
    case REAL_CST:
    case REAL_CST:
    case STRING_CST:
    case STRING_CST:
      return t;
      return t;
 
 
    case INTEGER_TYPE:
    case INTEGER_TYPE:
      if (t == integer_type_node)
      if (t == integer_type_node)
        return t;
        return t;
 
 
      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
          && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
          && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
        return t;
        return t;
 
 
      {
      {
        tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
        tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
 
 
        max = tsubst_expr (omax, args, complain, in_decl,
        max = tsubst_expr (omax, args, complain, in_decl,
                           /*integral_constant_expression_p=*/false);
                           /*integral_constant_expression_p=*/false);
 
 
        /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
        /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
           needed.  */
           needed.  */
        if (TREE_CODE (max) == NOP_EXPR
        if (TREE_CODE (max) == NOP_EXPR
            && TREE_SIDE_EFFECTS (omax)
            && TREE_SIDE_EFFECTS (omax)
            && !TREE_TYPE (max))
            && !TREE_TYPE (max))
          TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
          TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
 
 
        max = fold_decl_constant_value (max);
        max = fold_decl_constant_value (max);
 
 
        /* If we're in a partial instantiation, preserve the magic NOP_EXPR
        /* If we're in a partial instantiation, preserve the magic NOP_EXPR
           with TREE_SIDE_EFFECTS that indicates this is not an integral
           with TREE_SIDE_EFFECTS that indicates this is not an integral
           constant expression.  */
           constant expression.  */
        if (processing_template_decl
        if (processing_template_decl
            && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
            && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
          {
          {
            gcc_assert (TREE_CODE (max) == NOP_EXPR);
            gcc_assert (TREE_CODE (max) == NOP_EXPR);
            TREE_SIDE_EFFECTS (max) = 1;
            TREE_SIDE_EFFECTS (max) = 1;
          }
          }
 
 
        if (TREE_CODE (max) != INTEGER_CST
        if (TREE_CODE (max) != INTEGER_CST
            && !at_function_scope_p ()
            && !at_function_scope_p ()
            && !TREE_SIDE_EFFECTS (max)
            && !TREE_SIDE_EFFECTS (max)
            && !value_dependent_expression_p (max))
            && !value_dependent_expression_p (max))
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("array bound is not an integer constant");
              error ("array bound is not an integer constant");
            return error_mark_node;
            return error_mark_node;
          }
          }
 
 
        /* [temp.deduct]
        /* [temp.deduct]
 
 
           Type deduction may fail for any of the following
           Type deduction may fail for any of the following
           reasons:
           reasons:
 
 
             Attempting to create an array with a size that is
             Attempting to create an array with a size that is
             zero or negative.  */
             zero or negative.  */
        if (integer_zerop (max) && !(complain & tf_error))
        if (integer_zerop (max) && !(complain & tf_error))
          /* We must fail if performing argument deduction (as
          /* We must fail if performing argument deduction (as
             indicated by the state of complain), so that
             indicated by the state of complain), so that
             another substitution can be found.  */
             another substitution can be found.  */
          return error_mark_node;
          return error_mark_node;
        else if (TREE_CODE (max) == INTEGER_CST
        else if (TREE_CODE (max) == INTEGER_CST
                 && INT_CST_LT (max, integer_zero_node))
                 && INT_CST_LT (max, integer_zero_node))
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating array with negative size (%qE)", max);
              error ("creating array with negative size (%qE)", max);
 
 
            return error_mark_node;
            return error_mark_node;
          }
          }
 
 
        return compute_array_index_type (NULL_TREE, max);
        return compute_array_index_type (NULL_TREE, max);
      }
      }
 
 
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      {
      {
        int idx;
        int idx;
        int level;
        int level;
        int levels;
        int levels;
        tree arg = NULL_TREE;
        tree arg = NULL_TREE;
 
 
        r = NULL_TREE;
        r = NULL_TREE;
 
 
        gcc_assert (TREE_VEC_LENGTH (args) > 0);
        gcc_assert (TREE_VEC_LENGTH (args) > 0);
        template_parm_level_and_index (t, &level, &idx);
        template_parm_level_and_index (t, &level, &idx);
 
 
        levels = TMPL_ARGS_DEPTH (args);
        levels = TMPL_ARGS_DEPTH (args);
        if (level <= levels)
        if (level <= levels)
          {
          {
            arg = TMPL_ARG (args, level, idx);
            arg = TMPL_ARG (args, level, idx);
 
 
            if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
            if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
              /* See through ARGUMENT_PACK_SELECT arguments. */
              /* See through ARGUMENT_PACK_SELECT arguments. */
              arg = ARGUMENT_PACK_SELECT_ARG (arg);
              arg = ARGUMENT_PACK_SELECT_ARG (arg);
          }
          }
 
 
        if (arg == error_mark_node)
        if (arg == error_mark_node)
          return error_mark_node;
          return error_mark_node;
        else if (arg != NULL_TREE)
        else if (arg != NULL_TREE)
          {
          {
            if (ARGUMENT_PACK_P (arg))
            if (ARGUMENT_PACK_P (arg))
              /* If ARG is an argument pack, we don't actually want to
              /* If ARG is an argument pack, we don't actually want to
                 perform a substitution here, because substitutions
                 perform a substitution here, because substitutions
                 for argument packs are only done
                 for argument packs are only done
                 element-by-element. We can get to this point when
                 element-by-element. We can get to this point when
                 substituting the type of a non-type template
                 substituting the type of a non-type template
                 parameter pack, when that type actually contains
                 parameter pack, when that type actually contains
                 template parameter packs from an outer template, e.g.,
                 template parameter packs from an outer template, e.g.,
 
 
                 template<typename... Types> struct A {
                 template<typename... Types> struct A {
                   template<Types... Values> struct B { };
                   template<Types... Values> struct B { };
                 };  */
                 };  */
              return t;
              return t;
 
 
            if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
            if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
              {
              {
                int quals;
                int quals;
                gcc_assert (TYPE_P (arg));
                gcc_assert (TYPE_P (arg));
 
 
                /* cv-quals from the template are discarded when
                /* cv-quals from the template are discarded when
                   substituting in a function or reference type.  */
                   substituting in a function or reference type.  */
                if (TREE_CODE (arg) == FUNCTION_TYPE
                if (TREE_CODE (arg) == FUNCTION_TYPE
                    || TREE_CODE (arg) == METHOD_TYPE
                    || TREE_CODE (arg) == METHOD_TYPE
                    || TREE_CODE (arg) == REFERENCE_TYPE)
                    || TREE_CODE (arg) == REFERENCE_TYPE)
                  quals = cp_type_quals (arg);
                  quals = cp_type_quals (arg);
                else
                else
                  quals = cp_type_quals (arg) | cp_type_quals (t);
                  quals = cp_type_quals (arg) | cp_type_quals (t);
 
 
                return cp_build_qualified_type_real
                return cp_build_qualified_type_real
                  (arg, quals, complain | tf_ignore_bad_quals);
                  (arg, quals, complain | tf_ignore_bad_quals);
              }
              }
            else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
            else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
              {
              {
                /* We are processing a type constructed from a
                /* We are processing a type constructed from a
                   template template parameter.  */
                   template template parameter.  */
                tree argvec = tsubst (TYPE_TI_ARGS (t),
                tree argvec = tsubst (TYPE_TI_ARGS (t),
                                      args, complain, in_decl);
                                      args, complain, in_decl);
                if (argvec == error_mark_node)
                if (argvec == error_mark_node)
                  return error_mark_node;
                  return error_mark_node;
 
 
                /* We can get a TEMPLATE_TEMPLATE_PARM here when we
                /* We can get a TEMPLATE_TEMPLATE_PARM here when we
                   are resolving nested-types in the signature of a
                   are resolving nested-types in the signature of a
                   member function templates.  Otherwise ARG is a
                   member function templates.  Otherwise ARG is a
                   TEMPLATE_DECL and is the real template to be
                   TEMPLATE_DECL and is the real template to be
                   instantiated.  */
                   instantiated.  */
                if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
                if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
                  arg = TYPE_NAME (arg);
                  arg = TYPE_NAME (arg);
 
 
                r = lookup_template_class (arg,
                r = lookup_template_class (arg,
                                           argvec, in_decl,
                                           argvec, in_decl,
                                           DECL_CONTEXT (arg),
                                           DECL_CONTEXT (arg),
                                            /*entering_scope=*/0,
                                            /*entering_scope=*/0,
                                           complain);
                                           complain);
                return cp_build_qualified_type_real
                return cp_build_qualified_type_real
                  (r, TYPE_QUALS (t), complain);
                  (r, TYPE_QUALS (t), complain);
              }
              }
            else
            else
              /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
              /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
              return arg;
              return arg;
          }
          }
 
 
        if (level == 1)
        if (level == 1)
          /* This can happen during the attempted tsubst'ing in
          /* This can happen during the attempted tsubst'ing in
             unify.  This means that we don't yet have any information
             unify.  This means that we don't yet have any information
             about the template parameter in question.  */
             about the template parameter in question.  */
          return t;
          return t;
 
 
        /* If we get here, we must have been looking at a parm for a
        /* If we get here, we must have been looking at a parm for a
           more deeply nested template.  Make a new version of this
           more deeply nested template.  Make a new version of this
           template parameter, but with a lower level.  */
           template parameter, but with a lower level.  */
        switch (TREE_CODE (t))
        switch (TREE_CODE (t))
          {
          {
          case TEMPLATE_TYPE_PARM:
          case TEMPLATE_TYPE_PARM:
          case TEMPLATE_TEMPLATE_PARM:
          case TEMPLATE_TEMPLATE_PARM:
          case BOUND_TEMPLATE_TEMPLATE_PARM:
          case BOUND_TEMPLATE_TEMPLATE_PARM:
            if (cp_type_quals (t))
            if (cp_type_quals (t))
              {
              {
                r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
                r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
                r = cp_build_qualified_type_real
                r = cp_build_qualified_type_real
                  (r, cp_type_quals (t),
                  (r, cp_type_quals (t),
                   complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
                   complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
                               ? tf_ignore_bad_quals : 0));
                               ? tf_ignore_bad_quals : 0));
              }
              }
            else
            else
              {
              {
                r = copy_type (t);
                r = copy_type (t);
                TEMPLATE_TYPE_PARM_INDEX (r)
                TEMPLATE_TYPE_PARM_INDEX (r)
                  = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
                  = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
                                                r, levels, args, complain);
                                                r, levels, args, complain);
                TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
                TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
                TYPE_MAIN_VARIANT (r) = r;
                TYPE_MAIN_VARIANT (r) = r;
                TYPE_POINTER_TO (r) = NULL_TREE;
                TYPE_POINTER_TO (r) = NULL_TREE;
                TYPE_REFERENCE_TO (r) = NULL_TREE;
                TYPE_REFERENCE_TO (r) = NULL_TREE;
 
 
                if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
                if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
                  /* We have reduced the level of the template
                  /* We have reduced the level of the template
                     template parameter, but not the levels of its
                     template parameter, but not the levels of its
                     template parameters, so canonical_type_parameter
                     template parameters, so canonical_type_parameter
                     will not be able to find the canonical template
                     will not be able to find the canonical template
                     template parameter for this level. Thus, we
                     template parameter for this level. Thus, we
                     require structural equality checking to compare
                     require structural equality checking to compare
                     TEMPLATE_TEMPLATE_PARMs. */
                     TEMPLATE_TEMPLATE_PARMs. */
                  SET_TYPE_STRUCTURAL_EQUALITY (r);
                  SET_TYPE_STRUCTURAL_EQUALITY (r);
                else if (TYPE_STRUCTURAL_EQUALITY_P (t))
                else if (TYPE_STRUCTURAL_EQUALITY_P (t))
                  SET_TYPE_STRUCTURAL_EQUALITY (r);
                  SET_TYPE_STRUCTURAL_EQUALITY (r);
                else
                else
                  TYPE_CANONICAL (r) = canonical_type_parameter (r);
                  TYPE_CANONICAL (r) = canonical_type_parameter (r);
 
 
                if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
                if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
                  {
                  {
                    tree argvec = tsubst (TYPE_TI_ARGS (t), args,
                    tree argvec = tsubst (TYPE_TI_ARGS (t), args,
                                          complain, in_decl);
                                          complain, in_decl);
                    if (argvec == error_mark_node)
                    if (argvec == error_mark_node)
                      return error_mark_node;
                      return error_mark_node;
 
 
                    TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
                    TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
                      = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
                      = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
                  }
                  }
              }
              }
            break;
            break;
 
 
          case TEMPLATE_PARM_INDEX:
          case TEMPLATE_PARM_INDEX:
            r = reduce_template_parm_level (t, type, levels, args, complain);
            r = reduce_template_parm_level (t, type, levels, args, complain);
            break;
            break;
 
 
          default:
          default:
            gcc_unreachable ();
            gcc_unreachable ();
          }
          }
 
 
        return r;
        return r;
      }
      }
 
 
    case TREE_LIST:
    case TREE_LIST:
      {
      {
        tree purpose, value, chain;
        tree purpose, value, chain;
 
 
        if (t == void_list_node)
        if (t == void_list_node)
          return t;
          return t;
 
 
        purpose = TREE_PURPOSE (t);
        purpose = TREE_PURPOSE (t);
        if (purpose)
        if (purpose)
          {
          {
            purpose = tsubst (purpose, args, complain, in_decl);
            purpose = tsubst (purpose, args, complain, in_decl);
            if (purpose == error_mark_node)
            if (purpose == error_mark_node)
              return error_mark_node;
              return error_mark_node;
          }
          }
        value = TREE_VALUE (t);
        value = TREE_VALUE (t);
        if (value)
        if (value)
          {
          {
            value = tsubst (value, args, complain, in_decl);
            value = tsubst (value, args, complain, in_decl);
            if (value == error_mark_node)
            if (value == error_mark_node)
              return error_mark_node;
              return error_mark_node;
          }
          }
        chain = TREE_CHAIN (t);
        chain = TREE_CHAIN (t);
        if (chain && chain != void_type_node)
        if (chain && chain != void_type_node)
          {
          {
            chain = tsubst (chain, args, complain, in_decl);
            chain = tsubst (chain, args, complain, in_decl);
            if (chain == error_mark_node)
            if (chain == error_mark_node)
              return error_mark_node;
              return error_mark_node;
          }
          }
        if (purpose == TREE_PURPOSE (t)
        if (purpose == TREE_PURPOSE (t)
            && value == TREE_VALUE (t)
            && value == TREE_VALUE (t)
            && chain == TREE_CHAIN (t))
            && chain == TREE_CHAIN (t))
          return t;
          return t;
        return hash_tree_cons (purpose, value, chain);
        return hash_tree_cons (purpose, value, chain);
      }
      }
 
 
    case TREE_BINFO:
    case TREE_BINFO:
      /* We should never be tsubsting a binfo.  */
      /* We should never be tsubsting a binfo.  */
      gcc_unreachable ();
      gcc_unreachable ();
 
 
    case TREE_VEC:
    case TREE_VEC:
      /* A vector of template arguments.  */
      /* A vector of template arguments.  */
      gcc_assert (!type);
      gcc_assert (!type);
      return tsubst_template_args (t, args, complain, in_decl);
      return tsubst_template_args (t, args, complain, in_decl);
 
 
    case POINTER_TYPE:
    case POINTER_TYPE:
    case REFERENCE_TYPE:
    case REFERENCE_TYPE:
      {
      {
        enum tree_code code;
        enum tree_code code;
 
 
        if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
        if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
          return t;
          return t;
 
 
        code = TREE_CODE (t);
        code = TREE_CODE (t);
 
 
 
 
        /* [temp.deduct]
        /* [temp.deduct]
 
 
           Type deduction may fail for any of the following
           Type deduction may fail for any of the following
           reasons:
           reasons:
 
 
           -- Attempting to create a pointer to reference type.
           -- Attempting to create a pointer to reference type.
           -- Attempting to create a reference to a reference type or
           -- Attempting to create a reference to a reference type or
              a reference to void.
              a reference to void.
 
 
          Core issue 106 says that creating a reference to a reference
          Core issue 106 says that creating a reference to a reference
          during instantiation is no longer a cause for failure. We
          during instantiation is no longer a cause for failure. We
          only enforce this check in strict C++98 mode.  */
          only enforce this check in strict C++98 mode.  */
        if ((TREE_CODE (type) == REFERENCE_TYPE
        if ((TREE_CODE (type) == REFERENCE_TYPE
             && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
             && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
            || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
            || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
          {
          {
            static location_t last_loc;
            static location_t last_loc;
 
 
            /* We keep track of the last time we issued this error
            /* We keep track of the last time we issued this error
               message to avoid spewing a ton of messages during a
               message to avoid spewing a ton of messages during a
               single bad template instantiation.  */
               single bad template instantiation.  */
            if (complain & tf_error
            if (complain & tf_error
                && last_loc != input_location)
                && last_loc != input_location)
              {
              {
                if (TREE_CODE (type) == VOID_TYPE)
                if (TREE_CODE (type) == VOID_TYPE)
                  error ("forming reference to void");
                  error ("forming reference to void");
               else if (code == POINTER_TYPE)
               else if (code == POINTER_TYPE)
                 error ("forming pointer to reference type %qT", type);
                 error ("forming pointer to reference type %qT", type);
               else
               else
                  error ("forming reference to reference type %qT", type);
                  error ("forming reference to reference type %qT", type);
                last_loc = input_location;
                last_loc = input_location;
              }
              }
 
 
            return error_mark_node;
            return error_mark_node;
          }
          }
        else if (code == POINTER_TYPE)
        else if (code == POINTER_TYPE)
          {
          {
            r = build_pointer_type (type);
            r = build_pointer_type (type);
            if (TREE_CODE (type) == METHOD_TYPE)
            if (TREE_CODE (type) == METHOD_TYPE)
              r = build_ptrmemfunc_type (r);
              r = build_ptrmemfunc_type (r);
          }
          }
        else if (TREE_CODE (type) == REFERENCE_TYPE)
        else if (TREE_CODE (type) == REFERENCE_TYPE)
          /* In C++0x, during template argument substitution, when there is an
          /* In C++0x, during template argument substitution, when there is an
             attempt to create a reference to a reference type, reference
             attempt to create a reference to a reference type, reference
             collapsing is applied as described in [14.3.1/4 temp.arg.type]:
             collapsing is applied as described in [14.3.1/4 temp.arg.type]:
 
 
             "If a template-argument for a template-parameter T names a type
             "If a template-argument for a template-parameter T names a type
             that is a reference to a type A, an attempt to create the type
             that is a reference to a type A, an attempt to create the type
             'lvalue reference to cv T' creates the type 'lvalue reference to
             'lvalue reference to cv T' creates the type 'lvalue reference to
             A,' while an attempt to create the type type rvalue reference to
             A,' while an attempt to create the type type rvalue reference to
             cv T' creates the type T"
             cv T' creates the type T"
          */
          */
          r = cp_build_reference_type
          r = cp_build_reference_type
              (TREE_TYPE (type),
              (TREE_TYPE (type),
               TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
               TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
        else
        else
          r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
          r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
        r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
        r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
 
 
        if (r != error_mark_node)
        if (r != error_mark_node)
          /* Will this ever be needed for TYPE_..._TO values?  */
          /* Will this ever be needed for TYPE_..._TO values?  */
          layout_type (r);
          layout_type (r);
 
 
        return r;
        return r;
      }
      }
    case OFFSET_TYPE:
    case OFFSET_TYPE:
      {
      {
        r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
        r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
        if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
        if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
          {
          {
            /* [temp.deduct]
            /* [temp.deduct]
 
 
               Type deduction may fail for any of the following
               Type deduction may fail for any of the following
               reasons:
               reasons:
 
 
               -- Attempting to create "pointer to member of T" when T
               -- Attempting to create "pointer to member of T" when T
                  is not a class type.  */
                  is not a class type.  */
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating pointer to member of non-class type %qT", r);
              error ("creating pointer to member of non-class type %qT", r);
            return error_mark_node;
            return error_mark_node;
          }
          }
        if (TREE_CODE (type) == REFERENCE_TYPE)
        if (TREE_CODE (type) == REFERENCE_TYPE)
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating pointer to member reference type %qT", type);
              error ("creating pointer to member reference type %qT", type);
            return error_mark_node;
            return error_mark_node;
          }
          }
        if (TREE_CODE (type) == VOID_TYPE)
        if (TREE_CODE (type) == VOID_TYPE)
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating pointer to member of type void");
              error ("creating pointer to member of type void");
            return error_mark_node;
            return error_mark_node;
          }
          }
        gcc_assert (TREE_CODE (type) != METHOD_TYPE);
        gcc_assert (TREE_CODE (type) != METHOD_TYPE);
        if (TREE_CODE (type) == FUNCTION_TYPE)
        if (TREE_CODE (type) == FUNCTION_TYPE)
          {
          {
            /* The type of the implicit object parameter gets its
            /* The type of the implicit object parameter gets its
               cv-qualifiers from the FUNCTION_TYPE. */
               cv-qualifiers from the FUNCTION_TYPE. */
            tree memptr;
            tree memptr;
            tree method_type = build_memfn_type (type, r, cp_type_quals (type));
            tree method_type = build_memfn_type (type, r, cp_type_quals (type));
            memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
            memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
            return cp_build_qualified_type_real (memptr, cp_type_quals (t),
            return cp_build_qualified_type_real (memptr, cp_type_quals (t),
                                                 complain);
                                                 complain);
          }
          }
        else
        else
          return cp_build_qualified_type_real (build_ptrmem_type (r, type),
          return cp_build_qualified_type_real (build_ptrmem_type (r, type),
                                               TYPE_QUALS (t),
                                               TYPE_QUALS (t),
                                               complain);
                                               complain);
      }
      }
    case FUNCTION_TYPE:
    case FUNCTION_TYPE:
    case METHOD_TYPE:
    case METHOD_TYPE:
      {
      {
        tree fntype;
        tree fntype;
        tree specs;
        tree specs;
        fntype = tsubst_function_type (t, args, complain, in_decl);
        fntype = tsubst_function_type (t, args, complain, in_decl);
        if (fntype == error_mark_node)
        if (fntype == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        /* Substitute the exception specification.  */
        /* Substitute the exception specification.  */
        specs = tsubst_exception_specification (t, args, complain,
        specs = tsubst_exception_specification (t, args, complain,
                                                in_decl);
                                                in_decl);
        if (specs == error_mark_node)
        if (specs == error_mark_node)
          return error_mark_node;
          return error_mark_node;
        if (specs)
        if (specs)
          fntype = build_exception_variant (fntype, specs);
          fntype = build_exception_variant (fntype, specs);
        return fntype;
        return fntype;
      }
      }
    case ARRAY_TYPE:
    case ARRAY_TYPE:
      {
      {
        tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
        tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
        if (domain == error_mark_node)
        if (domain == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        /* As an optimization, we avoid regenerating the array type if
        /* As an optimization, we avoid regenerating the array type if
           it will obviously be the same as T.  */
           it will obviously be the same as T.  */
        if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
        if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
          return t;
          return t;
 
 
        /* These checks should match the ones in grokdeclarator.
        /* These checks should match the ones in grokdeclarator.
 
 
           [temp.deduct]
           [temp.deduct]
 
 
           The deduction may fail for any of the following reasons:
           The deduction may fail for any of the following reasons:
 
 
           -- Attempting to create an array with an element type that
           -- Attempting to create an array with an element type that
              is void, a function type, or a reference type, or [DR337]
              is void, a function type, or a reference type, or [DR337]
              an abstract class type.  */
              an abstract class type.  */
        if (TREE_CODE (type) == VOID_TYPE
        if (TREE_CODE (type) == VOID_TYPE
            || TREE_CODE (type) == FUNCTION_TYPE
            || TREE_CODE (type) == FUNCTION_TYPE
            || TREE_CODE (type) == REFERENCE_TYPE)
            || TREE_CODE (type) == REFERENCE_TYPE)
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating array of %qT", type);
              error ("creating array of %qT", type);
            return error_mark_node;
            return error_mark_node;
          }
          }
        if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
        if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("creating array of %qT, which is an abstract class type",
              error ("creating array of %qT, which is an abstract class type",
                     type);
                     type);
            return error_mark_node;
            return error_mark_node;
          }
          }
 
 
        r = build_cplus_array_type (type, domain);
        r = build_cplus_array_type (type, domain);
 
 
        if (TYPE_USER_ALIGN (t))
        if (TYPE_USER_ALIGN (t))
          {
          {
            TYPE_ALIGN (r) = TYPE_ALIGN (t);
            TYPE_ALIGN (r) = TYPE_ALIGN (t);
            TYPE_USER_ALIGN (r) = 1;
            TYPE_USER_ALIGN (r) = 1;
          }
          }
 
 
        return r;
        return r;
      }
      }
 
 
    case PLUS_EXPR:
    case PLUS_EXPR:
    case MINUS_EXPR:
    case MINUS_EXPR:
      {
      {
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
        tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
 
 
        if (e1 == error_mark_node || e2 == error_mark_node)
        if (e1 == error_mark_node || e2 == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        return fold_build2_loc (input_location,
        return fold_build2_loc (input_location,
                            TREE_CODE (t), TREE_TYPE (t), e1, e2);
                            TREE_CODE (t), TREE_TYPE (t), e1, e2);
      }
      }
 
 
    case NEGATE_EXPR:
    case NEGATE_EXPR:
    case NOP_EXPR:
    case NOP_EXPR:
      {
      {
        tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        if (e == error_mark_node)
        if (e == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
        return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
      }
      }
 
 
    case TYPENAME_TYPE:
    case TYPENAME_TYPE:
      {
      {
        tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
        tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
                                     in_decl, /*entering_scope=*/1);
                                     in_decl, /*entering_scope=*/1);
        tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
        tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
                              complain, in_decl);
                              complain, in_decl);
        int quals;
        int quals;
 
 
        if (ctx == error_mark_node || f == error_mark_node)
        if (ctx == error_mark_node || f == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        if (!MAYBE_CLASS_TYPE_P (ctx))
        if (!MAYBE_CLASS_TYPE_P (ctx))
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("%qT is not a class, struct, or union type", ctx);
              error ("%qT is not a class, struct, or union type", ctx);
            return error_mark_node;
            return error_mark_node;
          }
          }
        else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
        else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
          {
          {
            /* Normally, make_typename_type does not require that the CTX
            /* Normally, make_typename_type does not require that the CTX
               have complete type in order to allow things like:
               have complete type in order to allow things like:
 
 
                 template <class T> struct S { typename S<T>::X Y; };
                 template <class T> struct S { typename S<T>::X Y; };
 
 
               But, such constructs have already been resolved by this
               But, such constructs have already been resolved by this
               point, so here CTX really should have complete type, unless
               point, so here CTX really should have complete type, unless
               it's a partial instantiation.  */
               it's a partial instantiation.  */
            if (!(complain & tf_no_class_instantiations))
            if (!(complain & tf_no_class_instantiations))
              ctx = complete_type (ctx);
              ctx = complete_type (ctx);
            if (!COMPLETE_TYPE_P (ctx))
            if (!COMPLETE_TYPE_P (ctx))
              {
              {
                if (complain & tf_error)
                if (complain & tf_error)
                  cxx_incomplete_type_error (NULL_TREE, ctx);
                  cxx_incomplete_type_error (NULL_TREE, ctx);
                return error_mark_node;
                return error_mark_node;
              }
              }
          }
          }
 
 
        f = make_typename_type (ctx, f, typename_type,
        f = make_typename_type (ctx, f, typename_type,
                                (complain & tf_error) | tf_keep_type_decl);
                                (complain & tf_error) | tf_keep_type_decl);
        if (f == error_mark_node)
        if (f == error_mark_node)
          return f;
          return f;
        if (TREE_CODE (f) == TYPE_DECL)
        if (TREE_CODE (f) == TYPE_DECL)
          {
          {
            complain |= tf_ignore_bad_quals;
            complain |= tf_ignore_bad_quals;
            f = TREE_TYPE (f);
            f = TREE_TYPE (f);
          }
          }
 
 
        if (TREE_CODE (f) != TYPENAME_TYPE)
        if (TREE_CODE (f) != TYPENAME_TYPE)
          {
          {
            if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
            if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
              error ("%qT resolves to %qT, which is not an enumeration type",
              error ("%qT resolves to %qT, which is not an enumeration type",
                     t, f);
                     t, f);
            else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
            else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
              error ("%qT resolves to %qT, which is is not a class type",
              error ("%qT resolves to %qT, which is is not a class type",
                     t, f);
                     t, f);
          }
          }
 
 
        /* cv-quals from the template are discarded when
        /* cv-quals from the template are discarded when
           substituting in a function or reference type.  */
           substituting in a function or reference type.  */
        if (TREE_CODE (f) == FUNCTION_TYPE
        if (TREE_CODE (f) == FUNCTION_TYPE
            || TREE_CODE (f) == METHOD_TYPE
            || TREE_CODE (f) == METHOD_TYPE
            || TREE_CODE (f) == REFERENCE_TYPE)
            || TREE_CODE (f) == REFERENCE_TYPE)
          quals = cp_type_quals (f);
          quals = cp_type_quals (f);
        else
        else
          quals = cp_type_quals (f) | cp_type_quals (t);
          quals = cp_type_quals (f) | cp_type_quals (t);
        return cp_build_qualified_type_real (f, quals, complain);
        return cp_build_qualified_type_real (f, quals, complain);
      }
      }
 
 
    case UNBOUND_CLASS_TEMPLATE:
    case UNBOUND_CLASS_TEMPLATE:
      {
      {
        tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
        tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
                                     in_decl, /*entering_scope=*/1);
                                     in_decl, /*entering_scope=*/1);
        tree name = TYPE_IDENTIFIER (t);
        tree name = TYPE_IDENTIFIER (t);
        tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
        tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
 
 
        if (ctx == error_mark_node || name == error_mark_node)
        if (ctx == error_mark_node || name == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        if (parm_list)
        if (parm_list)
          parm_list = tsubst_template_parms (parm_list, args, complain);
          parm_list = tsubst_template_parms (parm_list, args, complain);
        return make_unbound_class_template (ctx, name, parm_list, complain);
        return make_unbound_class_template (ctx, name, parm_list, complain);
      }
      }
 
 
    case INDIRECT_REF:
    case INDIRECT_REF:
    case ADDR_EXPR:
    case ADDR_EXPR:
    case CALL_EXPR:
    case CALL_EXPR:
      gcc_unreachable ();
      gcc_unreachable ();
 
 
    case ARRAY_REF:
    case ARRAY_REF:
      {
      {
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
        tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
                               /*integral_constant_expression_p=*/false);
                               /*integral_constant_expression_p=*/false);
        if (e1 == error_mark_node || e2 == error_mark_node)
        if (e1 == error_mark_node || e2 == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
        return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
      }
      }
 
 
    case SCOPE_REF:
    case SCOPE_REF:
      {
      {
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
        tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
        tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
        if (e1 == error_mark_node || e2 == error_mark_node)
        if (e1 == error_mark_node || e2 == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        return build_qualified_name (/*type=*/NULL_TREE,
        return build_qualified_name (/*type=*/NULL_TREE,
                                     e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
                                     e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
      }
      }
 
 
    case TYPEOF_TYPE:
    case TYPEOF_TYPE:
      {
      {
        tree type;
        tree type;
 
 
        ++cp_unevaluated_operand;
        ++cp_unevaluated_operand;
        ++c_inhibit_evaluation_warnings;
        ++c_inhibit_evaluation_warnings;
 
 
        type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
        type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
                            complain, in_decl,
                            complain, in_decl,
                            /*integral_constant_expression_p=*/false);
                            /*integral_constant_expression_p=*/false);
 
 
        --cp_unevaluated_operand;
        --cp_unevaluated_operand;
        --c_inhibit_evaluation_warnings;
        --c_inhibit_evaluation_warnings;
 
 
        type = finish_typeof (type);
        type = finish_typeof (type);
        return cp_build_qualified_type_real (type,
        return cp_build_qualified_type_real (type,
                                             cp_type_quals (t)
                                             cp_type_quals (t)
                                             | cp_type_quals (type),
                                             | cp_type_quals (type),
                                             complain);
                                             complain);
      }
      }
 
 
    case DECLTYPE_TYPE:
    case DECLTYPE_TYPE:
      {
      {
        tree type;
        tree type;
 
 
        ++cp_unevaluated_operand;
        ++cp_unevaluated_operand;
        ++c_inhibit_evaluation_warnings;
        ++c_inhibit_evaluation_warnings;
 
 
        type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
        type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
                            complain, in_decl,
                            complain, in_decl,
                            /*integral_constant_expression_p=*/false);
                            /*integral_constant_expression_p=*/false);
 
 
        --cp_unevaluated_operand;
        --cp_unevaluated_operand;
        --c_inhibit_evaluation_warnings;
        --c_inhibit_evaluation_warnings;
 
 
        if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
        if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
          type = lambda_capture_field_type (type);
          type = lambda_capture_field_type (type);
        else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
        else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
          type = lambda_return_type (type);
          type = lambda_return_type (type);
        else
        else
          type = finish_decltype_type
          type = finish_decltype_type
            (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
            (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
        return cp_build_qualified_type_real (type,
        return cp_build_qualified_type_real (type,
                                             cp_type_quals (t)
                                             cp_type_quals (t)
                                             | cp_type_quals (type),
                                             | cp_type_quals (type),
                                             complain);
                                             complain);
      }
      }
 
 
    case TYPE_ARGUMENT_PACK:
    case TYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      {
      {
        tree r = TYPE_P (t)
        tree r = TYPE_P (t)
          ? cxx_make_type (TREE_CODE (t))
          ? cxx_make_type (TREE_CODE (t))
          : make_node (TREE_CODE (t));
          : make_node (TREE_CODE (t));
        tree packed_out =
        tree packed_out =
          tsubst_template_args (ARGUMENT_PACK_ARGS (t),
          tsubst_template_args (ARGUMENT_PACK_ARGS (t),
                                args,
                                args,
                                complain,
                                complain,
                                in_decl);
                                in_decl);
        SET_ARGUMENT_PACK_ARGS (r, packed_out);
        SET_ARGUMENT_PACK_ARGS (r, packed_out);
 
 
        /* For template nontype argument packs, also substitute into
        /* For template nontype argument packs, also substitute into
           the type.  */
           the type.  */
        if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
        if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
          TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
          TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
 
 
        return r;
        return r;
      }
      }
      break;
      break;
 
 
    default:
    default:
      sorry ("use of %qs in template",
      sorry ("use of %qs in template",
             tree_code_name [(int) TREE_CODE (t)]);
             tree_code_name [(int) TREE_CODE (t)]);
      return error_mark_node;
      return error_mark_node;
    }
    }
}
}
 
 
/* Like tsubst_expr for a BASELINK.  OBJECT_TYPE, if non-NULL, is the
/* Like tsubst_expr for a BASELINK.  OBJECT_TYPE, if non-NULL, is the
   type of the expression on the left-hand side of the "." or "->"
   type of the expression on the left-hand side of the "." or "->"
   operator.  */
   operator.  */
 
 
static tree
static tree
tsubst_baselink (tree baselink, tree object_type,
tsubst_baselink (tree baselink, tree object_type,
                 tree args, tsubst_flags_t complain, tree in_decl)
                 tree args, tsubst_flags_t complain, tree in_decl)
{
{
    tree name;
    tree name;
    tree qualifying_scope;
    tree qualifying_scope;
    tree fns;
    tree fns;
    tree optype;
    tree optype;
    tree template_args = 0;
    tree template_args = 0;
    bool template_id_p = false;
    bool template_id_p = false;
 
 
    /* A baselink indicates a function from a base class.  Both the
    /* A baselink indicates a function from a base class.  Both the
       BASELINK_ACCESS_BINFO and the base class referenced may
       BASELINK_ACCESS_BINFO and the base class referenced may
       indicate bases of the template class, rather than the
       indicate bases of the template class, rather than the
       instantiated class.  In addition, lookups that were not
       instantiated class.  In addition, lookups that were not
       ambiguous before may be ambiguous now.  Therefore, we perform
       ambiguous before may be ambiguous now.  Therefore, we perform
       the lookup again.  */
       the lookup again.  */
    qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
    qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
    qualifying_scope = tsubst (qualifying_scope, args,
    qualifying_scope = tsubst (qualifying_scope, args,
                               complain, in_decl);
                               complain, in_decl);
    fns = BASELINK_FUNCTIONS (baselink);
    fns = BASELINK_FUNCTIONS (baselink);
    optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
    optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
    if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
    if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
      {
      {
        template_id_p = true;
        template_id_p = true;
        template_args = TREE_OPERAND (fns, 1);
        template_args = TREE_OPERAND (fns, 1);
        fns = TREE_OPERAND (fns, 0);
        fns = TREE_OPERAND (fns, 0);
        if (template_args)
        if (template_args)
          template_args = tsubst_template_args (template_args, args,
          template_args = tsubst_template_args (template_args, args,
                                                complain, in_decl);
                                                complain, in_decl);
      }
      }
    name = DECL_NAME (get_first_fn (fns));
    name = DECL_NAME (get_first_fn (fns));
    if (IDENTIFIER_TYPENAME_P (name))
    if (IDENTIFIER_TYPENAME_P (name))
      name = mangle_conv_op_name_for_type (optype);
      name = mangle_conv_op_name_for_type (optype);
    baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
    baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
    if (!baselink)
    if (!baselink)
      return error_mark_node;
      return error_mark_node;
 
 
    /* If lookup found a single function, mark it as used at this
    /* If lookup found a single function, mark it as used at this
       point.  (If it lookup found multiple functions the one selected
       point.  (If it lookup found multiple functions the one selected
       later by overload resolution will be marked as used at that
       later by overload resolution will be marked as used at that
       point.)  */
       point.)  */
    if (BASELINK_P (baselink))
    if (BASELINK_P (baselink))
      fns = BASELINK_FUNCTIONS (baselink);
      fns = BASELINK_FUNCTIONS (baselink);
    if (!template_id_p && !really_overloaded_fn (fns))
    if (!template_id_p && !really_overloaded_fn (fns))
      mark_used (OVL_CURRENT (fns));
      mark_used (OVL_CURRENT (fns));
 
 
    /* Add back the template arguments, if present.  */
    /* Add back the template arguments, if present.  */
    if (BASELINK_P (baselink) && template_id_p)
    if (BASELINK_P (baselink) && template_id_p)
      BASELINK_FUNCTIONS (baselink)
      BASELINK_FUNCTIONS (baselink)
        = build_nt (TEMPLATE_ID_EXPR,
        = build_nt (TEMPLATE_ID_EXPR,
                    BASELINK_FUNCTIONS (baselink),
                    BASELINK_FUNCTIONS (baselink),
                    template_args);
                    template_args);
    /* Update the conversion operator type.  */
    /* Update the conversion operator type.  */
    BASELINK_OPTYPE (baselink) = optype;
    BASELINK_OPTYPE (baselink) = optype;
 
 
    if (!object_type)
    if (!object_type)
      object_type = current_class_type;
      object_type = current_class_type;
    return adjust_result_of_qualified_name_lookup (baselink,
    return adjust_result_of_qualified_name_lookup (baselink,
                                                   qualifying_scope,
                                                   qualifying_scope,
                                                   object_type);
                                                   object_type);
}
}
 
 
/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   true if the qualified-id will be a postfix-expression in-and-of
   true if the qualified-id will be a postfix-expression in-and-of
   itself; false if more of the postfix-expression follows the
   itself; false if more of the postfix-expression follows the
   QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   of "&".  */
   of "&".  */
 
 
static tree
static tree
tsubst_qualified_id (tree qualified_id, tree args,
tsubst_qualified_id (tree qualified_id, tree args,
                     tsubst_flags_t complain, tree in_decl,
                     tsubst_flags_t complain, tree in_decl,
                     bool done, bool address_p)
                     bool done, bool address_p)
{
{
  tree expr;
  tree expr;
  tree scope;
  tree scope;
  tree name;
  tree name;
  bool is_template;
  bool is_template;
  tree template_args;
  tree template_args;
 
 
  gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
  gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
 
 
  /* Figure out what name to look up.  */
  /* Figure out what name to look up.  */
  name = TREE_OPERAND (qualified_id, 1);
  name = TREE_OPERAND (qualified_id, 1);
  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
    {
    {
      is_template = true;
      is_template = true;
      template_args = TREE_OPERAND (name, 1);
      template_args = TREE_OPERAND (name, 1);
      if (template_args)
      if (template_args)
        template_args = tsubst_template_args (template_args, args,
        template_args = tsubst_template_args (template_args, args,
                                              complain, in_decl);
                                              complain, in_decl);
      name = TREE_OPERAND (name, 0);
      name = TREE_OPERAND (name, 0);
    }
    }
  else
  else
    {
    {
      is_template = false;
      is_template = false;
      template_args = NULL_TREE;
      template_args = NULL_TREE;
    }
    }
 
 
  /* Substitute into the qualifying scope.  When there are no ARGS, we
  /* Substitute into the qualifying scope.  When there are no ARGS, we
     are just trying to simplify a non-dependent expression.  In that
     are just trying to simplify a non-dependent expression.  In that
     case the qualifying scope may be dependent, and, in any case,
     case the qualifying scope may be dependent, and, in any case,
     substituting will not help.  */
     substituting will not help.  */
  scope = TREE_OPERAND (qualified_id, 0);
  scope = TREE_OPERAND (qualified_id, 0);
  if (args)
  if (args)
    {
    {
      scope = tsubst (scope, args, complain, in_decl);
      scope = tsubst (scope, args, complain, in_decl);
      expr = tsubst_copy (name, args, complain, in_decl);
      expr = tsubst_copy (name, args, complain, in_decl);
    }
    }
  else
  else
    expr = name;
    expr = name;
 
 
  if (dependent_scope_p (scope))
  if (dependent_scope_p (scope))
    return build_qualified_name (NULL_TREE, scope, expr,
    return build_qualified_name (NULL_TREE, scope, expr,
                                 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
                                 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
 
 
  if (!BASELINK_P (name) && !DECL_P (expr))
  if (!BASELINK_P (name) && !DECL_P (expr))
    {
    {
      if (TREE_CODE (expr) == BIT_NOT_EXPR)
      if (TREE_CODE (expr) == BIT_NOT_EXPR)
        {
        {
          /* A BIT_NOT_EXPR is used to represent a destructor.  */
          /* A BIT_NOT_EXPR is used to represent a destructor.  */
          if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
          if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
            {
            {
              error ("qualifying type %qT does not match destructor name ~%qT",
              error ("qualifying type %qT does not match destructor name ~%qT",
                     scope, TREE_OPERAND (expr, 0));
                     scope, TREE_OPERAND (expr, 0));
              expr = error_mark_node;
              expr = error_mark_node;
            }
            }
          else
          else
            expr = lookup_qualified_name (scope, complete_dtor_identifier,
            expr = lookup_qualified_name (scope, complete_dtor_identifier,
                                          /*is_type_p=*/0, false);
                                          /*is_type_p=*/0, false);
        }
        }
      else
      else
        expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
        expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
      if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
      if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
                     ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
                     ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
        {
        {
          if (complain & tf_error)
          if (complain & tf_error)
            {
            {
              error ("dependent-name %qE is parsed as a non-type, but "
              error ("dependent-name %qE is parsed as a non-type, but "
                     "instantiation yields a type", qualified_id);
                     "instantiation yields a type", qualified_id);
              inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
              inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
            }
            }
          return error_mark_node;
          return error_mark_node;
        }
        }
    }
    }
 
 
  if (DECL_P (expr))
  if (DECL_P (expr))
    {
    {
      check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
      check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
                                           scope);
                                           scope);
      /* Remember that there was a reference to this entity.  */
      /* Remember that there was a reference to this entity.  */
      mark_used (expr);
      mark_used (expr);
    }
    }
 
 
  if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
  if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        qualified_name_lookup_error (scope,
        qualified_name_lookup_error (scope,
                                     TREE_OPERAND (qualified_id, 1),
                                     TREE_OPERAND (qualified_id, 1),
                                     expr, input_location);
                                     expr, input_location);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  if (is_template)
  if (is_template)
    expr = lookup_template_function (expr, template_args);
    expr = lookup_template_function (expr, template_args);
 
 
  if (expr == error_mark_node && complain & tf_error)
  if (expr == error_mark_node && complain & tf_error)
    qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
    qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
                                 expr, input_location);
                                 expr, input_location);
  else if (TYPE_P (scope))
  else if (TYPE_P (scope))
    {
    {
      expr = (adjust_result_of_qualified_name_lookup
      expr = (adjust_result_of_qualified_name_lookup
              (expr, scope, current_class_type));
              (expr, scope, current_class_type));
      expr = (finish_qualified_id_expr
      expr = (finish_qualified_id_expr
              (scope, expr, done, address_p,
              (scope, expr, done, address_p,
               QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
               QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
               /*template_arg_p=*/false));
               /*template_arg_p=*/false));
    }
    }
 
 
  /* Expressions do not generally have reference type.  */
  /* Expressions do not generally have reference type.  */
  if (TREE_CODE (expr) != SCOPE_REF
  if (TREE_CODE (expr) != SCOPE_REF
      /* However, if we're about to form a pointer-to-member, we just
      /* However, if we're about to form a pointer-to-member, we just
         want the referenced member referenced.  */
         want the referenced member referenced.  */
      && TREE_CODE (expr) != OFFSET_REF)
      && TREE_CODE (expr) != OFFSET_REF)
    expr = convert_from_reference (expr);
    expr = convert_from_reference (expr);
 
 
  return expr;
  return expr;
}
}
 
 
/* Like tsubst, but deals with expressions.  This function just replaces
/* Like tsubst, but deals with expressions.  This function just replaces
   template parms; to finish processing the resultant expression, use
   template parms; to finish processing the resultant expression, use
   tsubst_expr.  */
   tsubst_expr.  */
 
 
static tree
static tree
tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
{
  enum tree_code code;
  enum tree_code code;
  tree r;
  tree r;
 
 
  if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
  if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
    return t;
    return t;
 
 
  code = TREE_CODE (t);
  code = TREE_CODE (t);
 
 
  switch (code)
  switch (code)
    {
    {
    case PARM_DECL:
    case PARM_DECL:
      r = retrieve_local_specialization (t);
      r = retrieve_local_specialization (t);
 
 
      if (r == NULL)
      if (r == NULL)
        {
        {
          tree c;
          tree c;
          /* This can happen for a parameter name used later in a function
          /* This can happen for a parameter name used later in a function
             declaration (such as in a late-specified return type).  Just
             declaration (such as in a late-specified return type).  Just
             make a dummy decl, since it's only used for its type.  */
             make a dummy decl, since it's only used for its type.  */
          gcc_assert (cp_unevaluated_operand != 0);
          gcc_assert (cp_unevaluated_operand != 0);
          /* We copy T because want to tsubst the PARM_DECL only,
          /* We copy T because want to tsubst the PARM_DECL only,
             not the following PARM_DECLs that are chained to T.  */
             not the following PARM_DECLs that are chained to T.  */
          c = copy_node (t);
          c = copy_node (t);
          r = tsubst_decl (c, args, complain);
          r = tsubst_decl (c, args, complain);
          /* Give it the template pattern as its context; its true context
          /* Give it the template pattern as its context; its true context
             hasn't been instantiated yet and this is good enough for
             hasn't been instantiated yet and this is good enough for
             mangling.  */
             mangling.  */
          DECL_CONTEXT (r) = DECL_CONTEXT (t);
          DECL_CONTEXT (r) = DECL_CONTEXT (t);
        }
        }
 
 
      if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
      if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
        r = ARGUMENT_PACK_SELECT_ARG (r);
        r = ARGUMENT_PACK_SELECT_ARG (r);
      mark_used (r);
      mark_used (r);
      return r;
      return r;
 
 
    case CONST_DECL:
    case CONST_DECL:
      {
      {
        tree enum_type;
        tree enum_type;
        tree v;
        tree v;
 
 
        if (DECL_TEMPLATE_PARM_P (t))
        if (DECL_TEMPLATE_PARM_P (t))
          return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
          return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
        /* There is no need to substitute into namespace-scope
        /* There is no need to substitute into namespace-scope
           enumerators.  */
           enumerators.  */
        if (DECL_NAMESPACE_SCOPE_P (t))
        if (DECL_NAMESPACE_SCOPE_P (t))
          return t;
          return t;
        /* If ARGS is NULL, then T is known to be non-dependent.  */
        /* If ARGS is NULL, then T is known to be non-dependent.  */
        if (args == NULL_TREE)
        if (args == NULL_TREE)
          return integral_constant_value (t);
          return integral_constant_value (t);
 
 
        /* Unfortunately, we cannot just call lookup_name here.
        /* Unfortunately, we cannot just call lookup_name here.
           Consider:
           Consider:
 
 
             template <int I> int f() {
             template <int I> int f() {
             enum E { a = I };
             enum E { a = I };
             struct S { void g() { E e = a; } };
             struct S { void g() { E e = a; } };
             };
             };
 
 
           When we instantiate f<7>::S::g(), say, lookup_name is not
           When we instantiate f<7>::S::g(), say, lookup_name is not
           clever enough to find f<7>::a.  */
           clever enough to find f<7>::a.  */
        enum_type
        enum_type
          = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
          = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
                              /*entering_scope=*/0);
                              /*entering_scope=*/0);
 
 
        for (v = TYPE_VALUES (enum_type);
        for (v = TYPE_VALUES (enum_type);
             v != NULL_TREE;
             v != NULL_TREE;
             v = TREE_CHAIN (v))
             v = TREE_CHAIN (v))
          if (TREE_PURPOSE (v) == DECL_NAME (t))
          if (TREE_PURPOSE (v) == DECL_NAME (t))
            return TREE_VALUE (v);
            return TREE_VALUE (v);
 
 
          /* We didn't find the name.  That should never happen; if
          /* We didn't find the name.  That should never happen; if
             name-lookup found it during preliminary parsing, we
             name-lookup found it during preliminary parsing, we
             should find it again here during instantiation.  */
             should find it again here during instantiation.  */
        gcc_unreachable ();
        gcc_unreachable ();
      }
      }
      return t;
      return t;
 
 
    case FIELD_DECL:
    case FIELD_DECL:
      if (DECL_CONTEXT (t))
      if (DECL_CONTEXT (t))
        {
        {
          tree ctx;
          tree ctx;
 
 
          ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
          ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
                                  /*entering_scope=*/1);
                                  /*entering_scope=*/1);
          if (ctx != DECL_CONTEXT (t))
          if (ctx != DECL_CONTEXT (t))
            {
            {
              tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
              tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
              if (!r)
              if (!r)
                {
                {
                  if (complain & tf_error)
                  if (complain & tf_error)
                    error ("using invalid field %qD", t);
                    error ("using invalid field %qD", t);
                  return error_mark_node;
                  return error_mark_node;
                }
                }
              return r;
              return r;
            }
            }
        }
        }
 
 
      return t;
      return t;
 
 
    case VAR_DECL:
    case VAR_DECL:
    case FUNCTION_DECL:
    case FUNCTION_DECL:
      if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
      if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
          || local_variable_p (t))
          || local_variable_p (t))
        t = tsubst (t, args, complain, in_decl);
        t = tsubst (t, args, complain, in_decl);
      mark_used (t);
      mark_used (t);
      return t;
      return t;
 
 
    case BASELINK:
    case BASELINK:
      return tsubst_baselink (t, current_class_type, args, complain, in_decl);
      return tsubst_baselink (t, current_class_type, args, complain, in_decl);
 
 
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
        return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
        return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
                       args, complain, in_decl);
                       args, complain, in_decl);
      else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
      else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
        return tsubst (t, args, complain, in_decl);
        return tsubst (t, args, complain, in_decl);
      else if (DECL_CLASS_SCOPE_P (t)
      else if (DECL_CLASS_SCOPE_P (t)
               && uses_template_parms (DECL_CONTEXT (t)))
               && uses_template_parms (DECL_CONTEXT (t)))
        {
        {
          /* Template template argument like the following example need
          /* Template template argument like the following example need
             special treatment:
             special treatment:
 
 
               template <template <class> class TT> struct C {};
               template <template <class> class TT> struct C {};
               template <class T> struct D {
               template <class T> struct D {
                 template <class U> struct E {};
                 template <class U> struct E {};
                 C<E> c;                                // #1
                 C<E> c;                                // #1
               };
               };
               D<int> d;                                // #2
               D<int> d;                                // #2
 
 
             We are processing the template argument `E' in #1 for
             We are processing the template argument `E' in #1 for
             the template instantiation #2.  Originally, `E' is a
             the template instantiation #2.  Originally, `E' is a
             TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
             TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
             have to substitute this with one having context `D<int>'.  */
             have to substitute this with one having context `D<int>'.  */
 
 
          tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
          tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
          return lookup_field (context, DECL_NAME(t), 0, false);
          return lookup_field (context, DECL_NAME(t), 0, false);
        }
        }
      else
      else
        /* Ordinary template template argument.  */
        /* Ordinary template template argument.  */
        return t;
        return t;
 
 
    case CAST_EXPR:
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CONST_CAST_EXPR:
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case NOP_EXPR:
    case NOP_EXPR:
      return build1
      return build1
        (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
        (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
 
 
    case SIZEOF_EXPR:
    case SIZEOF_EXPR:
      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
        {
        {
          /* We only want to compute the number of arguments.  */
          /* We only want to compute the number of arguments.  */
          tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
          tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
                                                complain, in_decl);
                                                complain, in_decl);
          int len = 0;
          int len = 0;
 
 
          if (TREE_CODE (expanded) == TREE_VEC)
          if (TREE_CODE (expanded) == TREE_VEC)
            len = TREE_VEC_LENGTH (expanded);
            len = TREE_VEC_LENGTH (expanded);
 
 
          if (expanded == error_mark_node)
          if (expanded == error_mark_node)
            return error_mark_node;
            return error_mark_node;
          else if (PACK_EXPANSION_P (expanded)
          else if (PACK_EXPANSION_P (expanded)
                   || (TREE_CODE (expanded) == TREE_VEC
                   || (TREE_CODE (expanded) == TREE_VEC
                       && len > 0
                       && len > 0
                       && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
                       && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
            {
            {
              if (TREE_CODE (expanded) == TREE_VEC)
              if (TREE_CODE (expanded) == TREE_VEC)
                expanded = TREE_VEC_ELT (expanded, len - 1);
                expanded = TREE_VEC_ELT (expanded, len - 1);
 
 
              if (TYPE_P (expanded))
              if (TYPE_P (expanded))
                return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
                return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
                                                   complain & tf_error);
                                                   complain & tf_error);
              else
              else
                return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
                return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
                                                   complain & tf_error);
                                                   complain & tf_error);
            }
            }
          else
          else
            return build_int_cst (size_type_node, len);
            return build_int_cst (size_type_node, len);
        }
        }
      /* Fall through */
      /* Fall through */
 
 
    case INDIRECT_REF:
    case INDIRECT_REF:
    case NEGATE_EXPR:
    case NEGATE_EXPR:
    case TRUTH_NOT_EXPR:
    case TRUTH_NOT_EXPR:
    case BIT_NOT_EXPR:
    case BIT_NOT_EXPR:
    case ADDR_EXPR:
    case ADDR_EXPR:
    case UNARY_PLUS_EXPR:      /* Unary + */
    case UNARY_PLUS_EXPR:      /* Unary + */
    case ALIGNOF_EXPR:
    case ALIGNOF_EXPR:
    case ARROW_EXPR:
    case ARROW_EXPR:
    case THROW_EXPR:
    case THROW_EXPR:
    case TYPEID_EXPR:
    case TYPEID_EXPR:
    case REALPART_EXPR:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case IMAGPART_EXPR:
      return build1
      return build1
        (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
        (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
 
 
    case COMPONENT_REF:
    case COMPONENT_REF:
      {
      {
        tree object;
        tree object;
        tree name;
        tree name;
 
 
        object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
        object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
        name = TREE_OPERAND (t, 1);
        name = TREE_OPERAND (t, 1);
        if (TREE_CODE (name) == BIT_NOT_EXPR)
        if (TREE_CODE (name) == BIT_NOT_EXPR)
          {
          {
            name = tsubst_copy (TREE_OPERAND (name, 0), args,
            name = tsubst_copy (TREE_OPERAND (name, 0), args,
                                complain, in_decl);
                                complain, in_decl);
            name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
            name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
          }
          }
        else if (TREE_CODE (name) == SCOPE_REF
        else if (TREE_CODE (name) == SCOPE_REF
                 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
                 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
          {
          {
            tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
            tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
                                     complain, in_decl);
                                     complain, in_decl);
            name = TREE_OPERAND (name, 1);
            name = TREE_OPERAND (name, 1);
            name = tsubst_copy (TREE_OPERAND (name, 0), args,
            name = tsubst_copy (TREE_OPERAND (name, 0), args,
                                complain, in_decl);
                                complain, in_decl);
            name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
            name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
            name = build_qualified_name (/*type=*/NULL_TREE,
            name = build_qualified_name (/*type=*/NULL_TREE,
                                         base, name,
                                         base, name,
                                         /*template_p=*/false);
                                         /*template_p=*/false);
          }
          }
        else if (TREE_CODE (name) == BASELINK)
        else if (TREE_CODE (name) == BASELINK)
          name = tsubst_baselink (name,
          name = tsubst_baselink (name,
                                  non_reference (TREE_TYPE (object)),
                                  non_reference (TREE_TYPE (object)),
                                  args, complain,
                                  args, complain,
                                  in_decl);
                                  in_decl);
        else
        else
          name = tsubst_copy (name, args, complain, in_decl);
          name = tsubst_copy (name, args, complain, in_decl);
        return build_nt (COMPONENT_REF, object, name, NULL_TREE);
        return build_nt (COMPONENT_REF, object, name, NULL_TREE);
      }
      }
 
 
    case PLUS_EXPR:
    case PLUS_EXPR:
    case MINUS_EXPR:
    case MINUS_EXPR:
    case MULT_EXPR:
    case MULT_EXPR:
    case TRUNC_DIV_EXPR:
    case TRUNC_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case EXACT_DIV_EXPR:
    case EXACT_DIV_EXPR:
    case BIT_AND_EXPR:
    case BIT_AND_EXPR:
    case BIT_IOR_EXPR:
    case BIT_IOR_EXPR:
    case BIT_XOR_EXPR:
    case BIT_XOR_EXPR:
    case TRUNC_MOD_EXPR:
    case TRUNC_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case TRUTH_AND_EXPR:
    case TRUTH_AND_EXPR:
    case TRUTH_OR_EXPR:
    case TRUTH_OR_EXPR:
    case RSHIFT_EXPR:
    case RSHIFT_EXPR:
    case LSHIFT_EXPR:
    case LSHIFT_EXPR:
    case RROTATE_EXPR:
    case RROTATE_EXPR:
    case LROTATE_EXPR:
    case LROTATE_EXPR:
    case EQ_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
    case NE_EXPR:
    case MAX_EXPR:
    case MAX_EXPR:
    case MIN_EXPR:
    case MIN_EXPR:
    case LE_EXPR:
    case LE_EXPR:
    case GE_EXPR:
    case GE_EXPR:
    case LT_EXPR:
    case LT_EXPR:
    case GT_EXPR:
    case GT_EXPR:
    case COMPOUND_EXPR:
    case COMPOUND_EXPR:
    case DOTSTAR_EXPR:
    case DOTSTAR_EXPR:
    case MEMBER_REF:
    case MEMBER_REF:
    case PREDECREMENT_EXPR:
    case PREDECREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case POSTDECREMENT_EXPR:
    case POSTDECREMENT_EXPR:
    case POSTINCREMENT_EXPR:
    case POSTINCREMENT_EXPR:
      return build_nt
      return build_nt
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
 
 
    case SCOPE_REF:
    case SCOPE_REF:
      return build_qualified_name (/*type=*/NULL_TREE,
      return build_qualified_name (/*type=*/NULL_TREE,
                                   tsubst_copy (TREE_OPERAND (t, 0),
                                   tsubst_copy (TREE_OPERAND (t, 0),
                                                args, complain, in_decl),
                                                args, complain, in_decl),
                                   tsubst_copy (TREE_OPERAND (t, 1),
                                   tsubst_copy (TREE_OPERAND (t, 1),
                                                args, complain, in_decl),
                                                args, complain, in_decl),
                                   QUALIFIED_NAME_IS_TEMPLATE (t));
                                   QUALIFIED_NAME_IS_TEMPLATE (t));
 
 
    case ARRAY_REF:
    case ARRAY_REF:
      return build_nt
      return build_nt
        (ARRAY_REF,
        (ARRAY_REF,
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
         NULL_TREE, NULL_TREE);
         NULL_TREE, NULL_TREE);
 
 
    case CALL_EXPR:
    case CALL_EXPR:
      {
      {
        int n = VL_EXP_OPERAND_LENGTH (t);
        int n = VL_EXP_OPERAND_LENGTH (t);
        tree result = build_vl_exp (CALL_EXPR, n);
        tree result = build_vl_exp (CALL_EXPR, n);
        int i;
        int i;
        for (i = 0; i < n; i++)
        for (i = 0; i < n; i++)
          TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
          TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
                                             complain, in_decl);
                                             complain, in_decl);
        return result;
        return result;
      }
      }
 
 
    case COND_EXPR:
    case COND_EXPR:
    case MODOP_EXPR:
    case MODOP_EXPR:
    case PSEUDO_DTOR_EXPR:
    case PSEUDO_DTOR_EXPR:
      {
      {
        r = build_nt
        r = build_nt
          (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
          (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
           tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
           tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
           tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
           tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
        TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
        TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
        return r;
        return r;
      }
      }
 
 
    case NEW_EXPR:
    case NEW_EXPR:
      {
      {
        r = build_nt
        r = build_nt
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
         tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
        NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
        NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
        return r;
        return r;
      }
      }
 
 
    case DELETE_EXPR:
    case DELETE_EXPR:
      {
      {
        r = build_nt
        r = build_nt
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
        (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
         tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
        DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
        DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
        DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
        DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
        return r;
        return r;
      }
      }
 
 
    case TEMPLATE_ID_EXPR:
    case TEMPLATE_ID_EXPR:
      {
      {
        /* Substituted template arguments */
        /* Substituted template arguments */
        tree fn = TREE_OPERAND (t, 0);
        tree fn = TREE_OPERAND (t, 0);
        tree targs = TREE_OPERAND (t, 1);
        tree targs = TREE_OPERAND (t, 1);
 
 
        fn = tsubst_copy (fn, args, complain, in_decl);
        fn = tsubst_copy (fn, args, complain, in_decl);
        if (targs)
        if (targs)
          targs = tsubst_template_args (targs, args, complain, in_decl);
          targs = tsubst_template_args (targs, args, complain, in_decl);
 
 
        return lookup_template_function (fn, targs);
        return lookup_template_function (fn, targs);
      }
      }
 
 
    case TREE_LIST:
    case TREE_LIST:
      {
      {
        tree purpose, value, chain;
        tree purpose, value, chain;
 
 
        if (t == void_list_node)
        if (t == void_list_node)
          return t;
          return t;
 
 
        purpose = TREE_PURPOSE (t);
        purpose = TREE_PURPOSE (t);
        if (purpose)
        if (purpose)
          purpose = tsubst_copy (purpose, args, complain, in_decl);
          purpose = tsubst_copy (purpose, args, complain, in_decl);
        value = TREE_VALUE (t);
        value = TREE_VALUE (t);
        if (value)
        if (value)
          value = tsubst_copy (value, args, complain, in_decl);
          value = tsubst_copy (value, args, complain, in_decl);
        chain = TREE_CHAIN (t);
        chain = TREE_CHAIN (t);
        if (chain && chain != void_type_node)
        if (chain && chain != void_type_node)
          chain = tsubst_copy (chain, args, complain, in_decl);
          chain = tsubst_copy (chain, args, complain, in_decl);
        if (purpose == TREE_PURPOSE (t)
        if (purpose == TREE_PURPOSE (t)
            && value == TREE_VALUE (t)
            && value == TREE_VALUE (t)
            && chain == TREE_CHAIN (t))
            && chain == TREE_CHAIN (t))
          return t;
          return t;
        return tree_cons (purpose, value, chain);
        return tree_cons (purpose, value, chain);
      }
      }
 
 
    case RECORD_TYPE:
    case RECORD_TYPE:
    case UNION_TYPE:
    case UNION_TYPE:
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
    case INTEGER_TYPE:
    case INTEGER_TYPE:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
    case POINTER_TYPE:
    case POINTER_TYPE:
    case REFERENCE_TYPE:
    case REFERENCE_TYPE:
    case OFFSET_TYPE:
    case OFFSET_TYPE:
    case FUNCTION_TYPE:
    case FUNCTION_TYPE:
    case METHOD_TYPE:
    case METHOD_TYPE:
    case ARRAY_TYPE:
    case ARRAY_TYPE:
    case TYPENAME_TYPE:
    case TYPENAME_TYPE:
    case UNBOUND_CLASS_TEMPLATE:
    case UNBOUND_CLASS_TEMPLATE:
    case TYPEOF_TYPE:
    case TYPEOF_TYPE:
    case DECLTYPE_TYPE:
    case DECLTYPE_TYPE:
    case TYPE_DECL:
    case TYPE_DECL:
      return tsubst (t, args, complain, in_decl);
      return tsubst (t, args, complain, in_decl);
 
 
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
      if (IDENTIFIER_TYPENAME_P (t))
      if (IDENTIFIER_TYPENAME_P (t))
        {
        {
          tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
          tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
          return mangle_conv_op_name_for_type (new_type);
          return mangle_conv_op_name_for_type (new_type);
        }
        }
      else
      else
        return t;
        return t;
 
 
    case CONSTRUCTOR:
    case CONSTRUCTOR:
      /* This is handled by tsubst_copy_and_build.  */
      /* This is handled by tsubst_copy_and_build.  */
      gcc_unreachable ();
      gcc_unreachable ();
 
 
    case VA_ARG_EXPR:
    case VA_ARG_EXPR:
      return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
      return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
                                          in_decl),
                                          in_decl),
                             tsubst (TREE_TYPE (t), args, complain, in_decl));
                             tsubst (TREE_TYPE (t), args, complain, in_decl));
 
 
    case CLEANUP_POINT_EXPR:
    case CLEANUP_POINT_EXPR:
      /* We shouldn't have built any of these during initial template
      /* We shouldn't have built any of these during initial template
         generation.  Instead, they should be built during instantiation
         generation.  Instead, they should be built during instantiation
         in response to the saved STMT_IS_FULL_EXPR_P setting.  */
         in response to the saved STMT_IS_FULL_EXPR_P setting.  */
      gcc_unreachable ();
      gcc_unreachable ();
 
 
    case OFFSET_REF:
    case OFFSET_REF:
      mark_used (TREE_OPERAND (t, 1));
      mark_used (TREE_OPERAND (t, 1));
      return t;
      return t;
 
 
    case EXPR_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
      error ("invalid use of pack expansion expression");
      error ("invalid use of pack expansion expression");
      return error_mark_node;
      return error_mark_node;
 
 
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      error ("use %<...%> to expand argument pack");
      error ("use %<...%> to expand argument pack");
      return error_mark_node;
      return error_mark_node;
 
 
    default:
    default:
      return t;
      return t;
    }
    }
}
}
 
 
/* Like tsubst_copy, but specifically for OpenMP clauses.  */
/* Like tsubst_copy, but specifically for OpenMP clauses.  */
 
 
static tree
static tree
tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
                    tree in_decl)
                    tree in_decl)
{
{
  tree new_clauses = NULL, nc, oc;
  tree new_clauses = NULL, nc, oc;
 
 
  for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
  for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
    {
    {
      nc = copy_node (oc);
      nc = copy_node (oc);
      OMP_CLAUSE_CHAIN (nc) = new_clauses;
      OMP_CLAUSE_CHAIN (nc) = new_clauses;
      new_clauses = nc;
      new_clauses = nc;
 
 
      switch (OMP_CLAUSE_CODE (nc))
      switch (OMP_CLAUSE_CODE (nc))
        {
        {
        case OMP_CLAUSE_LASTPRIVATE:
        case OMP_CLAUSE_LASTPRIVATE:
          if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
          if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
            {
            {
              OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
              OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
              tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
              tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
                           in_decl, /*integral_constant_expression_p=*/false);
                           in_decl, /*integral_constant_expression_p=*/false);
              OMP_CLAUSE_LASTPRIVATE_STMT (nc)
              OMP_CLAUSE_LASTPRIVATE_STMT (nc)
                = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
                = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
            }
            }
          /* FALLTHRU */
          /* FALLTHRU */
        case OMP_CLAUSE_PRIVATE:
        case OMP_CLAUSE_PRIVATE:
        case OMP_CLAUSE_SHARED:
        case OMP_CLAUSE_SHARED:
        case OMP_CLAUSE_FIRSTPRIVATE:
        case OMP_CLAUSE_FIRSTPRIVATE:
        case OMP_CLAUSE_REDUCTION:
        case OMP_CLAUSE_REDUCTION:
        case OMP_CLAUSE_COPYIN:
        case OMP_CLAUSE_COPYIN:
        case OMP_CLAUSE_COPYPRIVATE:
        case OMP_CLAUSE_COPYPRIVATE:
        case OMP_CLAUSE_IF:
        case OMP_CLAUSE_IF:
        case OMP_CLAUSE_NUM_THREADS:
        case OMP_CLAUSE_NUM_THREADS:
        case OMP_CLAUSE_SCHEDULE:
        case OMP_CLAUSE_SCHEDULE:
        case OMP_CLAUSE_COLLAPSE:
        case OMP_CLAUSE_COLLAPSE:
          OMP_CLAUSE_OPERAND (nc, 0)
          OMP_CLAUSE_OPERAND (nc, 0)
            = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
            = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
                           in_decl, /*integral_constant_expression_p=*/false);
                           in_decl, /*integral_constant_expression_p=*/false);
          break;
          break;
        case OMP_CLAUSE_NOWAIT:
        case OMP_CLAUSE_NOWAIT:
        case OMP_CLAUSE_ORDERED:
        case OMP_CLAUSE_ORDERED:
        case OMP_CLAUSE_DEFAULT:
        case OMP_CLAUSE_DEFAULT:
        case OMP_CLAUSE_UNTIED:
        case OMP_CLAUSE_UNTIED:
          break;
          break;
        default:
        default:
          gcc_unreachable ();
          gcc_unreachable ();
        }
        }
    }
    }
 
 
  return finish_omp_clauses (nreverse (new_clauses));
  return finish_omp_clauses (nreverse (new_clauses));
}
}
 
 
/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes.  */
/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes.  */
 
 
static tree
static tree
tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
                          tree in_decl)
                          tree in_decl)
{
{
#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
 
 
  tree purpose, value, chain;
  tree purpose, value, chain;
 
 
  if (t == NULL)
  if (t == NULL)
    return t;
    return t;
 
 
  if (TREE_CODE (t) != TREE_LIST)
  if (TREE_CODE (t) != TREE_LIST)
    return tsubst_copy_and_build (t, args, complain, in_decl,
    return tsubst_copy_and_build (t, args, complain, in_decl,
                                  /*function_p=*/false,
                                  /*function_p=*/false,
                                  /*integral_constant_expression_p=*/false);
                                  /*integral_constant_expression_p=*/false);
 
 
  if (t == void_list_node)
  if (t == void_list_node)
    return t;
    return t;
 
 
  purpose = TREE_PURPOSE (t);
  purpose = TREE_PURPOSE (t);
  if (purpose)
  if (purpose)
    purpose = RECUR (purpose);
    purpose = RECUR (purpose);
  value = TREE_VALUE (t);
  value = TREE_VALUE (t);
  if (value && TREE_CODE (value) != LABEL_DECL)
  if (value && TREE_CODE (value) != LABEL_DECL)
    value = RECUR (value);
    value = RECUR (value);
  chain = TREE_CHAIN (t);
  chain = TREE_CHAIN (t);
  if (chain && chain != void_type_node)
  if (chain && chain != void_type_node)
    chain = RECUR (chain);
    chain = RECUR (chain);
  return tree_cons (purpose, value, chain);
  return tree_cons (purpose, value, chain);
#undef RECUR
#undef RECUR
}
}
 
 
/* Substitute one OMP_FOR iterator.  */
/* Substitute one OMP_FOR iterator.  */
 
 
static void
static void
tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
                         tree condv, tree incrv, tree *clauses,
                         tree condv, tree incrv, tree *clauses,
                         tree args, tsubst_flags_t complain, tree in_decl,
                         tree args, tsubst_flags_t complain, tree in_decl,
                         bool integral_constant_expression_p)
                         bool integral_constant_expression_p)
{
{
#define RECUR(NODE)                             \
#define RECUR(NODE)                             \
  tsubst_expr ((NODE), args, complain, in_decl, \
  tsubst_expr ((NODE), args, complain, in_decl, \
               integral_constant_expression_p)
               integral_constant_expression_p)
  tree decl, init, cond, incr, auto_node;
  tree decl, init, cond, incr, auto_node;
 
 
  init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
  init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
  gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
  gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
  decl = RECUR (TREE_OPERAND (init, 0));
  decl = RECUR (TREE_OPERAND (init, 0));
  init = TREE_OPERAND (init, 1);
  init = TREE_OPERAND (init, 1);
  auto_node = type_uses_auto (TREE_TYPE (decl));
  auto_node = type_uses_auto (TREE_TYPE (decl));
  if (auto_node && init)
  if (auto_node && init)
    {
    {
      tree init_expr = init;
      tree init_expr = init;
      if (TREE_CODE (init_expr) == DECL_EXPR)
      if (TREE_CODE (init_expr) == DECL_EXPR)
        init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
        init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
      init_expr = RECUR (init_expr);
      init_expr = RECUR (init_expr);
      TREE_TYPE (decl)
      TREE_TYPE (decl)
        = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
        = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
    }
    }
  gcc_assert (!type_dependent_expression_p (decl));
  gcc_assert (!type_dependent_expression_p (decl));
 
 
  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
    {
    {
      cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
      cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
      incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
      incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
      if (TREE_CODE (incr) == MODIFY_EXPR)
      if (TREE_CODE (incr) == MODIFY_EXPR)
        incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
        incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
                                    RECUR (TREE_OPERAND (incr, 1)),
                                    RECUR (TREE_OPERAND (incr, 1)),
                                    complain);
                                    complain);
      else
      else
        incr = RECUR (incr);
        incr = RECUR (incr);
      TREE_VEC_ELT (declv, i) = decl;
      TREE_VEC_ELT (declv, i) = decl;
      TREE_VEC_ELT (initv, i) = init;
      TREE_VEC_ELT (initv, i) = init;
      TREE_VEC_ELT (condv, i) = cond;
      TREE_VEC_ELT (condv, i) = cond;
      TREE_VEC_ELT (incrv, i) = incr;
      TREE_VEC_ELT (incrv, i) = incr;
      return;
      return;
    }
    }
 
 
  if (init && TREE_CODE (init) != DECL_EXPR)
  if (init && TREE_CODE (init) != DECL_EXPR)
    {
    {
      tree c;
      tree c;
      for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
      for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
        {
        {
          if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
          if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
              && OMP_CLAUSE_DECL (c) == decl)
              && OMP_CLAUSE_DECL (c) == decl)
            break;
            break;
          else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
          else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
                   && OMP_CLAUSE_DECL (c) == decl)
                   && OMP_CLAUSE_DECL (c) == decl)
            error ("iteration variable %qD should not be firstprivate", decl);
            error ("iteration variable %qD should not be firstprivate", decl);
          else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
          else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
                   && OMP_CLAUSE_DECL (c) == decl)
                   && OMP_CLAUSE_DECL (c) == decl)
            error ("iteration variable %qD should not be reduction", decl);
            error ("iteration variable %qD should not be reduction", decl);
        }
        }
      if (c == NULL)
      if (c == NULL)
        {
        {
          c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
          c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
          OMP_CLAUSE_DECL (c) = decl;
          OMP_CLAUSE_DECL (c) = decl;
          c = finish_omp_clauses (c);
          c = finish_omp_clauses (c);
          if (c)
          if (c)
            {
            {
              OMP_CLAUSE_CHAIN (c) = *clauses;
              OMP_CLAUSE_CHAIN (c) = *clauses;
              *clauses = c;
              *clauses = c;
            }
            }
        }
        }
    }
    }
  cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
  cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
  if (COMPARISON_CLASS_P (cond))
  if (COMPARISON_CLASS_P (cond))
    cond = build2 (TREE_CODE (cond), boolean_type_node,
    cond = build2 (TREE_CODE (cond), boolean_type_node,
                   RECUR (TREE_OPERAND (cond, 0)),
                   RECUR (TREE_OPERAND (cond, 0)),
                   RECUR (TREE_OPERAND (cond, 1)));
                   RECUR (TREE_OPERAND (cond, 1)));
  else
  else
    cond = RECUR (cond);
    cond = RECUR (cond);
  incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
  incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
  switch (TREE_CODE (incr))
  switch (TREE_CODE (incr))
    {
    {
    case PREINCREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case PREDECREMENT_EXPR:
    case PREDECREMENT_EXPR:
    case POSTINCREMENT_EXPR:
    case POSTINCREMENT_EXPR:
    case POSTDECREMENT_EXPR:
    case POSTDECREMENT_EXPR:
      incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
      incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
                     RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
                     RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
      break;
      break;
    case MODIFY_EXPR:
    case MODIFY_EXPR:
      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
          || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
          || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
        {
        {
          tree rhs = TREE_OPERAND (incr, 1);
          tree rhs = TREE_OPERAND (incr, 1);
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
                         RECUR (TREE_OPERAND (incr, 0)),
                         RECUR (TREE_OPERAND (incr, 0)),
                         build2 (TREE_CODE (rhs), TREE_TYPE (decl),
                         build2 (TREE_CODE (rhs), TREE_TYPE (decl),
                                 RECUR (TREE_OPERAND (rhs, 0)),
                                 RECUR (TREE_OPERAND (rhs, 0)),
                                 RECUR (TREE_OPERAND (rhs, 1))));
                                 RECUR (TREE_OPERAND (rhs, 1))));
        }
        }
      else
      else
        incr = RECUR (incr);
        incr = RECUR (incr);
      break;
      break;
    case MODOP_EXPR:
    case MODOP_EXPR:
      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
          || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
          || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
        {
        {
          tree lhs = RECUR (TREE_OPERAND (incr, 0));
          tree lhs = RECUR (TREE_OPERAND (incr, 0));
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
                         build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
                         build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
                                 TREE_TYPE (decl), lhs,
                                 TREE_TYPE (decl), lhs,
                                 RECUR (TREE_OPERAND (incr, 2))));
                                 RECUR (TREE_OPERAND (incr, 2))));
        }
        }
      else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
      else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
               && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
               && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
                   || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
                   || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
        {
        {
          tree rhs = TREE_OPERAND (incr, 2);
          tree rhs = TREE_OPERAND (incr, 2);
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
          incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
                         RECUR (TREE_OPERAND (incr, 0)),
                         RECUR (TREE_OPERAND (incr, 0)),
                         build2 (TREE_CODE (rhs), TREE_TYPE (decl),
                         build2 (TREE_CODE (rhs), TREE_TYPE (decl),
                                 RECUR (TREE_OPERAND (rhs, 0)),
                                 RECUR (TREE_OPERAND (rhs, 0)),
                                 RECUR (TREE_OPERAND (rhs, 1))));
                                 RECUR (TREE_OPERAND (rhs, 1))));
        }
        }
      else
      else
        incr = RECUR (incr);
        incr = RECUR (incr);
      break;
      break;
    default:
    default:
      incr = RECUR (incr);
      incr = RECUR (incr);
      break;
      break;
    }
    }
 
 
  TREE_VEC_ELT (declv, i) = decl;
  TREE_VEC_ELT (declv, i) = decl;
  TREE_VEC_ELT (initv, i) = init;
  TREE_VEC_ELT (initv, i) = init;
  TREE_VEC_ELT (condv, i) = cond;
  TREE_VEC_ELT (condv, i) = cond;
  TREE_VEC_ELT (incrv, i) = incr;
  TREE_VEC_ELT (incrv, i) = incr;
#undef RECUR
#undef RECUR
}
}
 
 
/* Like tsubst_copy for expressions, etc. but also does semantic
/* Like tsubst_copy for expressions, etc. but also does semantic
   processing.  */
   processing.  */
 
 
static tree
static tree
tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
             bool integral_constant_expression_p)
             bool integral_constant_expression_p)
{
{
#define RECUR(NODE)                             \
#define RECUR(NODE)                             \
  tsubst_expr ((NODE), args, complain, in_decl, \
  tsubst_expr ((NODE), args, complain, in_decl, \
               integral_constant_expression_p)
               integral_constant_expression_p)
 
 
  tree stmt, tmp;
  tree stmt, tmp;
 
 
  if (t == NULL_TREE || t == error_mark_node)
  if (t == NULL_TREE || t == error_mark_node)
    return t;
    return t;
 
 
  if (EXPR_HAS_LOCATION (t))
  if (EXPR_HAS_LOCATION (t))
    input_location = EXPR_LOCATION (t);
    input_location = EXPR_LOCATION (t);
  if (STATEMENT_CODE_P (TREE_CODE (t)))
  if (STATEMENT_CODE_P (TREE_CODE (t)))
    current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
    current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case STATEMENT_LIST:
    case STATEMENT_LIST:
      {
      {
        tree_stmt_iterator i;
        tree_stmt_iterator i;
        for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
        for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
          RECUR (tsi_stmt (i));
          RECUR (tsi_stmt (i));
        break;
        break;
      }
      }
 
 
    case CTOR_INITIALIZER:
    case CTOR_INITIALIZER:
      finish_mem_initializers (tsubst_initializer_list
      finish_mem_initializers (tsubst_initializer_list
                               (TREE_OPERAND (t, 0), args));
                               (TREE_OPERAND (t, 0), args));
      break;
      break;
 
 
    case RETURN_EXPR:
    case RETURN_EXPR:
      finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
      finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
      break;
      break;
 
 
    case EXPR_STMT:
    case EXPR_STMT:
      tmp = RECUR (EXPR_STMT_EXPR (t));
      tmp = RECUR (EXPR_STMT_EXPR (t));
      if (EXPR_STMT_STMT_EXPR_RESULT (t))
      if (EXPR_STMT_STMT_EXPR_RESULT (t))
        finish_stmt_expr_expr (tmp, cur_stmt_expr);
        finish_stmt_expr_expr (tmp, cur_stmt_expr);
      else
      else
        finish_expr_stmt (tmp);
        finish_expr_stmt (tmp);
      break;
      break;
 
 
    case USING_STMT:
    case USING_STMT:
      do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
      do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
      break;
      break;
 
 
    case DECL_EXPR:
    case DECL_EXPR:
      {
      {
        tree decl;
        tree decl;
        tree init;
        tree init;
 
 
        decl = DECL_EXPR_DECL (t);
        decl = DECL_EXPR_DECL (t);
        if (TREE_CODE (decl) == LABEL_DECL)
        if (TREE_CODE (decl) == LABEL_DECL)
          finish_label_decl (DECL_NAME (decl));
          finish_label_decl (DECL_NAME (decl));
        else if (TREE_CODE (decl) == USING_DECL)
        else if (TREE_CODE (decl) == USING_DECL)
          {
          {
            tree scope = USING_DECL_SCOPE (decl);
            tree scope = USING_DECL_SCOPE (decl);
            tree name = DECL_NAME (decl);
            tree name = DECL_NAME (decl);
            tree decl;
            tree decl;
 
 
            scope = RECUR (scope);
            scope = RECUR (scope);
            decl = lookup_qualified_name (scope, name,
            decl = lookup_qualified_name (scope, name,
                                          /*is_type_p=*/false,
                                          /*is_type_p=*/false,
                                          /*complain=*/false);
                                          /*complain=*/false);
            if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
            if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
              qualified_name_lookup_error (scope, name, decl, input_location);
              qualified_name_lookup_error (scope, name, decl, input_location);
            else
            else
              do_local_using_decl (decl, scope, name);
              do_local_using_decl (decl, scope, name);
          }
          }
        else
        else
          {
          {
            init = DECL_INITIAL (decl);
            init = DECL_INITIAL (decl);
            decl = tsubst (decl, args, complain, in_decl);
            decl = tsubst (decl, args, complain, in_decl);
            if (decl != error_mark_node)
            if (decl != error_mark_node)
              {
              {
                /* By marking the declaration as instantiated, we avoid
                /* By marking the declaration as instantiated, we avoid
                   trying to instantiate it.  Since instantiate_decl can't
                   trying to instantiate it.  Since instantiate_decl can't
                   handle local variables, and since we've already done
                   handle local variables, and since we've already done
                   all that needs to be done, that's the right thing to
                   all that needs to be done, that's the right thing to
                   do.  */
                   do.  */
                if (TREE_CODE (decl) == VAR_DECL)
                if (TREE_CODE (decl) == VAR_DECL)
                  DECL_TEMPLATE_INSTANTIATED (decl) = 1;
                  DECL_TEMPLATE_INSTANTIATED (decl) = 1;
                if (TREE_CODE (decl) == VAR_DECL
                if (TREE_CODE (decl) == VAR_DECL
                    && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
                    && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
                  /* Anonymous aggregates are a special case.  */
                  /* Anonymous aggregates are a special case.  */
                  finish_anon_union (decl);
                  finish_anon_union (decl);
                else
                else
                  {
                  {
                    maybe_push_decl (decl);
                    maybe_push_decl (decl);
                    if (TREE_CODE (decl) == VAR_DECL
                    if (TREE_CODE (decl) == VAR_DECL
                        && DECL_PRETTY_FUNCTION_P (decl))
                        && DECL_PRETTY_FUNCTION_P (decl))
                      {
                      {
                        /* For __PRETTY_FUNCTION__ we have to adjust the
                        /* For __PRETTY_FUNCTION__ we have to adjust the
                           initializer.  */
                           initializer.  */
                        const char *const name
                        const char *const name
                          = cxx_printable_name (current_function_decl, 2);
                          = cxx_printable_name (current_function_decl, 2);
                        init = cp_fname_init (name, &TREE_TYPE (decl));
                        init = cp_fname_init (name, &TREE_TYPE (decl));
                      }
                      }
                    else
                    else
                      {
                      {
                        tree t = RECUR (init);
                        tree t = RECUR (init);
 
 
                        if (init && !t)
                        if (init && !t)
                          /* If we had an initializer but it
                          /* If we had an initializer but it
                             instantiated to nothing,
                             instantiated to nothing,
                             value-initialize the object.  This will
                             value-initialize the object.  This will
                             only occur when the initializer was a
                             only occur when the initializer was a
                             pack expansion where the parameter packs
                             pack expansion where the parameter packs
                             used in that expansion were of length
                             used in that expansion were of length
                             zero.  */
                             zero.  */
                          init = build_value_init (TREE_TYPE (decl));
                          init = build_value_init (TREE_TYPE (decl));
                        else
                        else
                          init = t;
                          init = t;
                      }
                      }
 
 
                    cp_finish_decl (decl, init, false, NULL_TREE, 0);
                    cp_finish_decl (decl, init, false, NULL_TREE, 0);
                  }
                  }
              }
              }
          }
          }
 
 
        /* A DECL_EXPR can also be used as an expression, in the condition
        /* A DECL_EXPR can also be used as an expression, in the condition
           clause of an if/for/while construct.  */
           clause of an if/for/while construct.  */
        return decl;
        return decl;
      }
      }
 
 
    case FOR_STMT:
    case FOR_STMT:
      stmt = begin_for_stmt ();
      stmt = begin_for_stmt ();
                          RECUR (FOR_INIT_STMT (t));
                          RECUR (FOR_INIT_STMT (t));
      finish_for_init_stmt (stmt);
      finish_for_init_stmt (stmt);
      tmp = RECUR (FOR_COND (t));
      tmp = RECUR (FOR_COND (t));
      finish_for_cond (tmp, stmt);
      finish_for_cond (tmp, stmt);
      tmp = RECUR (FOR_EXPR (t));
      tmp = RECUR (FOR_EXPR (t));
      finish_for_expr (tmp, stmt);
      finish_for_expr (tmp, stmt);
      RECUR (FOR_BODY (t));
      RECUR (FOR_BODY (t));
      finish_for_stmt (stmt);
      finish_for_stmt (stmt);
      break;
      break;
 
 
    case WHILE_STMT:
    case WHILE_STMT:
      stmt = begin_while_stmt ();
      stmt = begin_while_stmt ();
      tmp = RECUR (WHILE_COND (t));
      tmp = RECUR (WHILE_COND (t));
      finish_while_stmt_cond (tmp, stmt);
      finish_while_stmt_cond (tmp, stmt);
      RECUR (WHILE_BODY (t));
      RECUR (WHILE_BODY (t));
      finish_while_stmt (stmt);
      finish_while_stmt (stmt);
      break;
      break;
 
 
    case DO_STMT:
    case DO_STMT:
      stmt = begin_do_stmt ();
      stmt = begin_do_stmt ();
      RECUR (DO_BODY (t));
      RECUR (DO_BODY (t));
      finish_do_body (stmt);
      finish_do_body (stmt);
      tmp = RECUR (DO_COND (t));
      tmp = RECUR (DO_COND (t));
      finish_do_stmt (tmp, stmt);
      finish_do_stmt (tmp, stmt);
      break;
      break;
 
 
    case IF_STMT:
    case IF_STMT:
      stmt = begin_if_stmt ();
      stmt = begin_if_stmt ();
      tmp = RECUR (IF_COND (t));
      tmp = RECUR (IF_COND (t));
      finish_if_stmt_cond (tmp, stmt);
      finish_if_stmt_cond (tmp, stmt);
      RECUR (THEN_CLAUSE (t));
      RECUR (THEN_CLAUSE (t));
      finish_then_clause (stmt);
      finish_then_clause (stmt);
 
 
      if (ELSE_CLAUSE (t))
      if (ELSE_CLAUSE (t))
        {
        {
          begin_else_clause (stmt);
          begin_else_clause (stmt);
          RECUR (ELSE_CLAUSE (t));
          RECUR (ELSE_CLAUSE (t));
          finish_else_clause (stmt);
          finish_else_clause (stmt);
        }
        }
 
 
      finish_if_stmt (stmt);
      finish_if_stmt (stmt);
      break;
      break;
 
 
    case BIND_EXPR:
    case BIND_EXPR:
      if (BIND_EXPR_BODY_BLOCK (t))
      if (BIND_EXPR_BODY_BLOCK (t))
        stmt = begin_function_body ();
        stmt = begin_function_body ();
      else
      else
        stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
        stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
                                    ? BCS_TRY_BLOCK : 0);
                                    ? BCS_TRY_BLOCK : 0);
 
 
      RECUR (BIND_EXPR_BODY (t));
      RECUR (BIND_EXPR_BODY (t));
 
 
      if (BIND_EXPR_BODY_BLOCK (t))
      if (BIND_EXPR_BODY_BLOCK (t))
        finish_function_body (stmt);
        finish_function_body (stmt);
      else
      else
        finish_compound_stmt (stmt);
        finish_compound_stmt (stmt);
      break;
      break;
 
 
    case BREAK_STMT:
    case BREAK_STMT:
      finish_break_stmt ();
      finish_break_stmt ();
      break;
      break;
 
 
    case CONTINUE_STMT:
    case CONTINUE_STMT:
      finish_continue_stmt ();
      finish_continue_stmt ();
      break;
      break;
 
 
    case SWITCH_STMT:
    case SWITCH_STMT:
      stmt = begin_switch_stmt ();
      stmt = begin_switch_stmt ();
      tmp = RECUR (SWITCH_STMT_COND (t));
      tmp = RECUR (SWITCH_STMT_COND (t));
      finish_switch_cond (tmp, stmt);
      finish_switch_cond (tmp, stmt);
      RECUR (SWITCH_STMT_BODY (t));
      RECUR (SWITCH_STMT_BODY (t));
      finish_switch_stmt (stmt);
      finish_switch_stmt (stmt);
      break;
      break;
 
 
    case CASE_LABEL_EXPR:
    case CASE_LABEL_EXPR:
      finish_case_label (EXPR_LOCATION (t),
      finish_case_label (EXPR_LOCATION (t),
                         RECUR (CASE_LOW (t)),
                         RECUR (CASE_LOW (t)),
                         RECUR (CASE_HIGH (t)));
                         RECUR (CASE_HIGH (t)));
      break;
      break;
 
 
    case LABEL_EXPR:
    case LABEL_EXPR:
      {
      {
        tree decl = LABEL_EXPR_LABEL (t);
        tree decl = LABEL_EXPR_LABEL (t);
        tree label;
        tree label;
 
 
        label = finish_label_stmt (DECL_NAME (decl));
        label = finish_label_stmt (DECL_NAME (decl));
        if (DECL_ATTRIBUTES (decl) != NULL_TREE)
        if (DECL_ATTRIBUTES (decl) != NULL_TREE)
          cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
          cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
      }
      }
      break;
      break;
 
 
    case GOTO_EXPR:
    case GOTO_EXPR:
      tmp = GOTO_DESTINATION (t);
      tmp = GOTO_DESTINATION (t);
      if (TREE_CODE (tmp) != LABEL_DECL)
      if (TREE_CODE (tmp) != LABEL_DECL)
        /* Computed goto's must be tsubst'd into.  On the other hand,
        /* Computed goto's must be tsubst'd into.  On the other hand,
           non-computed gotos must not be; the identifier in question
           non-computed gotos must not be; the identifier in question
           will have no binding.  */
           will have no binding.  */
        tmp = RECUR (tmp);
        tmp = RECUR (tmp);
      else
      else
        tmp = DECL_NAME (tmp);
        tmp = DECL_NAME (tmp);
      finish_goto_stmt (tmp);
      finish_goto_stmt (tmp);
      break;
      break;
 
 
    case ASM_EXPR:
    case ASM_EXPR:
      tmp = finish_asm_stmt
      tmp = finish_asm_stmt
        (ASM_VOLATILE_P (t),
        (ASM_VOLATILE_P (t),
         RECUR (ASM_STRING (t)),
         RECUR (ASM_STRING (t)),
         tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
         tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
         tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
      {
      {
        tree asm_expr = tmp;
        tree asm_expr = tmp;
        if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
        if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
          asm_expr = TREE_OPERAND (asm_expr, 0);
          asm_expr = TREE_OPERAND (asm_expr, 0);
        ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
        ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
      }
      }
      break;
      break;
 
 
    case TRY_BLOCK:
    case TRY_BLOCK:
      if (CLEANUP_P (t))
      if (CLEANUP_P (t))
        {
        {
          stmt = begin_try_block ();
          stmt = begin_try_block ();
          RECUR (TRY_STMTS (t));
          RECUR (TRY_STMTS (t));
          finish_cleanup_try_block (stmt);
          finish_cleanup_try_block (stmt);
          finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
          finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
        }
        }
      else
      else
        {
        {
          tree compound_stmt = NULL_TREE;
          tree compound_stmt = NULL_TREE;
 
 
          if (FN_TRY_BLOCK_P (t))
          if (FN_TRY_BLOCK_P (t))
            stmt = begin_function_try_block (&compound_stmt);
            stmt = begin_function_try_block (&compound_stmt);
          else
          else
            stmt = begin_try_block ();
            stmt = begin_try_block ();
 
 
          RECUR (TRY_STMTS (t));
          RECUR (TRY_STMTS (t));
 
 
          if (FN_TRY_BLOCK_P (t))
          if (FN_TRY_BLOCK_P (t))
            finish_function_try_block (stmt);
            finish_function_try_block (stmt);
          else
          else
            finish_try_block (stmt);
            finish_try_block (stmt);
 
 
          RECUR (TRY_HANDLERS (t));
          RECUR (TRY_HANDLERS (t));
          if (FN_TRY_BLOCK_P (t))
          if (FN_TRY_BLOCK_P (t))
            finish_function_handler_sequence (stmt, compound_stmt);
            finish_function_handler_sequence (stmt, compound_stmt);
          else
          else
            finish_handler_sequence (stmt);
            finish_handler_sequence (stmt);
        }
        }
      break;
      break;
 
 
    case HANDLER:
    case HANDLER:
      {
      {
        tree decl = HANDLER_PARMS (t);
        tree decl = HANDLER_PARMS (t);
 
 
        if (decl)
        if (decl)
          {
          {
            decl = tsubst (decl, args, complain, in_decl);
            decl = tsubst (decl, args, complain, in_decl);
            /* Prevent instantiate_decl from trying to instantiate
            /* Prevent instantiate_decl from trying to instantiate
               this variable.  We've already done all that needs to be
               this variable.  We've already done all that needs to be
               done.  */
               done.  */
            if (decl != error_mark_node)
            if (decl != error_mark_node)
              DECL_TEMPLATE_INSTANTIATED (decl) = 1;
              DECL_TEMPLATE_INSTANTIATED (decl) = 1;
          }
          }
        stmt = begin_handler ();
        stmt = begin_handler ();
        finish_handler_parms (decl, stmt);
        finish_handler_parms (decl, stmt);
        RECUR (HANDLER_BODY (t));
        RECUR (HANDLER_BODY (t));
        finish_handler (stmt);
        finish_handler (stmt);
      }
      }
      break;
      break;
 
 
    case TAG_DEFN:
    case TAG_DEFN:
      tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
      tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
      break;
      break;
 
 
    case STATIC_ASSERT:
    case STATIC_ASSERT:
      {
      {
        tree condition =
        tree condition =
          tsubst_expr (STATIC_ASSERT_CONDITION (t),
          tsubst_expr (STATIC_ASSERT_CONDITION (t),
                       args,
                       args,
                       complain, in_decl,
                       complain, in_decl,
                       /*integral_constant_expression_p=*/true);
                       /*integral_constant_expression_p=*/true);
        finish_static_assert (condition,
        finish_static_assert (condition,
                              STATIC_ASSERT_MESSAGE (t),
                              STATIC_ASSERT_MESSAGE (t),
                              STATIC_ASSERT_SOURCE_LOCATION (t),
                              STATIC_ASSERT_SOURCE_LOCATION (t),
                              /*member_p=*/false);
                              /*member_p=*/false);
      }
      }
      break;
      break;
 
 
    case OMP_PARALLEL:
    case OMP_PARALLEL:
      tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
      tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
                                args, complain, in_decl);
                                args, complain, in_decl);
      stmt = begin_omp_parallel ();
      stmt = begin_omp_parallel ();
      RECUR (OMP_PARALLEL_BODY (t));
      RECUR (OMP_PARALLEL_BODY (t));
      OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
      OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
        = OMP_PARALLEL_COMBINED (t);
        = OMP_PARALLEL_COMBINED (t);
      break;
      break;
 
 
    case OMP_TASK:
    case OMP_TASK:
      tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
      tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
                                args, complain, in_decl);
                                args, complain, in_decl);
      stmt = begin_omp_task ();
      stmt = begin_omp_task ();
      RECUR (OMP_TASK_BODY (t));
      RECUR (OMP_TASK_BODY (t));
      finish_omp_task (tmp, stmt);
      finish_omp_task (tmp, stmt);
      break;
      break;
 
 
    case OMP_FOR:
    case OMP_FOR:
      {
      {
        tree clauses, body, pre_body;
        tree clauses, body, pre_body;
        tree declv, initv, condv, incrv;
        tree declv, initv, condv, incrv;
        int i;
        int i;
 
 
        clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
        clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
                                      args, complain, in_decl);
                                      args, complain, in_decl);
        declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
        incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
 
 
        for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
        for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
          tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
          tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
                                   &clauses, args, complain, in_decl,
                                   &clauses, args, complain, in_decl,
                                   integral_constant_expression_p);
                                   integral_constant_expression_p);
 
 
        stmt = begin_omp_structured_block ();
        stmt = begin_omp_structured_block ();
 
 
        for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
        for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
          if (TREE_VEC_ELT (initv, i) == NULL
          if (TREE_VEC_ELT (initv, i) == NULL
              || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
              || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
            TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
            TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
          else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
          else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
            {
            {
              tree init = RECUR (TREE_VEC_ELT (initv, i));
              tree init = RECUR (TREE_VEC_ELT (initv, i));
              gcc_assert (init == TREE_VEC_ELT (declv, i));
              gcc_assert (init == TREE_VEC_ELT (declv, i));
              TREE_VEC_ELT (initv, i) = NULL_TREE;
              TREE_VEC_ELT (initv, i) = NULL_TREE;
            }
            }
          else
          else
            {
            {
              tree decl_expr = TREE_VEC_ELT (initv, i);
              tree decl_expr = TREE_VEC_ELT (initv, i);
              tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
              tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
              gcc_assert (init != NULL);
              gcc_assert (init != NULL);
              TREE_VEC_ELT (initv, i) = RECUR (init);
              TREE_VEC_ELT (initv, i) = RECUR (init);
              DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
              DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
              RECUR (decl_expr);
              RECUR (decl_expr);
              DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
              DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
            }
            }
 
 
        pre_body = push_stmt_list ();
        pre_body = push_stmt_list ();
        RECUR (OMP_FOR_PRE_BODY (t));
        RECUR (OMP_FOR_PRE_BODY (t));
        pre_body = pop_stmt_list (pre_body);
        pre_body = pop_stmt_list (pre_body);
 
 
        body = push_stmt_list ();
        body = push_stmt_list ();
        RECUR (OMP_FOR_BODY (t));
        RECUR (OMP_FOR_BODY (t));
        body = pop_stmt_list (body);
        body = pop_stmt_list (body);
 
 
        t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
        t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
                            body, pre_body, clauses);
                            body, pre_body, clauses);
 
 
        add_stmt (finish_omp_structured_block (stmt));
        add_stmt (finish_omp_structured_block (stmt));
      }
      }
      break;
      break;
 
 
    case OMP_SECTIONS:
    case OMP_SECTIONS:
    case OMP_SINGLE:
    case OMP_SINGLE:
      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
      stmt = push_stmt_list ();
      stmt = push_stmt_list ();
      RECUR (OMP_BODY (t));
      RECUR (OMP_BODY (t));
      stmt = pop_stmt_list (stmt);
      stmt = pop_stmt_list (stmt);
 
 
      t = copy_node (t);
      t = copy_node (t);
      OMP_BODY (t) = stmt;
      OMP_BODY (t) = stmt;
      OMP_CLAUSES (t) = tmp;
      OMP_CLAUSES (t) = tmp;
      add_stmt (t);
      add_stmt (t);
      break;
      break;
 
 
    case OMP_SECTION:
    case OMP_SECTION:
    case OMP_CRITICAL:
    case OMP_CRITICAL:
    case OMP_MASTER:
    case OMP_MASTER:
    case OMP_ORDERED:
    case OMP_ORDERED:
      stmt = push_stmt_list ();
      stmt = push_stmt_list ();
      RECUR (OMP_BODY (t));
      RECUR (OMP_BODY (t));
      stmt = pop_stmt_list (stmt);
      stmt = pop_stmt_list (stmt);
 
 
      t = copy_node (t);
      t = copy_node (t);
      OMP_BODY (t) = stmt;
      OMP_BODY (t) = stmt;
      add_stmt (t);
      add_stmt (t);
      break;
      break;
 
 
    case OMP_ATOMIC:
    case OMP_ATOMIC:
      gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
      gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
      {
      {
        tree op1 = TREE_OPERAND (t, 1);
        tree op1 = TREE_OPERAND (t, 1);
        tree lhs = RECUR (TREE_OPERAND (op1, 0));
        tree lhs = RECUR (TREE_OPERAND (op1, 0));
        tree rhs = RECUR (TREE_OPERAND (op1, 1));
        tree rhs = RECUR (TREE_OPERAND (op1, 1));
        finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
        finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
      }
      }
      break;
      break;
 
 
    case EXPR_PACK_EXPANSION:
    case EXPR_PACK_EXPANSION:
      error ("invalid use of pack expansion expression");
      error ("invalid use of pack expansion expression");
      return error_mark_node;
      return error_mark_node;
 
 
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      error ("use %<...%> to expand argument pack");
      error ("use %<...%> to expand argument pack");
      return error_mark_node;
      return error_mark_node;
 
 
    default:
    default:
      gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
      gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
 
 
      return tsubst_copy_and_build (t, args, complain, in_decl,
      return tsubst_copy_and_build (t, args, complain, in_decl,
                                    /*function_p=*/false,
                                    /*function_p=*/false,
                                    integral_constant_expression_p);
                                    integral_constant_expression_p);
    }
    }
 
 
  return NULL_TREE;
  return NULL_TREE;
#undef RECUR
#undef RECUR
}
}
 
 
/* T is a postfix-expression that is not being used in a function
/* T is a postfix-expression that is not being used in a function
   call.  Return the substituted version of T.  */
   call.  Return the substituted version of T.  */
 
 
static tree
static tree
tsubst_non_call_postfix_expression (tree t, tree args,
tsubst_non_call_postfix_expression (tree t, tree args,
                                    tsubst_flags_t complain,
                                    tsubst_flags_t complain,
                                    tree in_decl)
                                    tree in_decl)
{
{
  if (TREE_CODE (t) == SCOPE_REF)
  if (TREE_CODE (t) == SCOPE_REF)
    t = tsubst_qualified_id (t, args, complain, in_decl,
    t = tsubst_qualified_id (t, args, complain, in_decl,
                             /*done=*/false, /*address_p=*/false);
                             /*done=*/false, /*address_p=*/false);
  else
  else
    t = tsubst_copy_and_build (t, args, complain, in_decl,
    t = tsubst_copy_and_build (t, args, complain, in_decl,
                               /*function_p=*/false,
                               /*function_p=*/false,
                               /*integral_constant_expression_p=*/false);
                               /*integral_constant_expression_p=*/false);
 
 
  return t;
  return t;
}
}
 
 
/* Like tsubst but deals with expressions and performs semantic
/* Like tsubst but deals with expressions and performs semantic
   analysis.  FUNCTION_P is true if T is the "F" in "F (ARGS)".  */
   analysis.  FUNCTION_P is true if T is the "F" in "F (ARGS)".  */
 
 
tree
tree
tsubst_copy_and_build (tree t,
tsubst_copy_and_build (tree t,
                       tree args,
                       tree args,
                       tsubst_flags_t complain,
                       tsubst_flags_t complain,
                       tree in_decl,
                       tree in_decl,
                       bool function_p,
                       bool function_p,
                       bool integral_constant_expression_p)
                       bool integral_constant_expression_p)
{
{
#define RECUR(NODE)                                             \
#define RECUR(NODE)                                             \
  tsubst_copy_and_build (NODE, args, complain, in_decl,         \
  tsubst_copy_and_build (NODE, args, complain, in_decl,         \
                         /*function_p=*/false,                  \
                         /*function_p=*/false,                  \
                         integral_constant_expression_p)
                         integral_constant_expression_p)
 
 
  tree op1;
  tree op1;
 
 
  if (t == NULL_TREE || t == error_mark_node)
  if (t == NULL_TREE || t == error_mark_node)
    return t;
    return t;
 
 
  switch (TREE_CODE (t))
  switch (TREE_CODE (t))
    {
    {
    case USING_DECL:
    case USING_DECL:
      t = DECL_NAME (t);
      t = DECL_NAME (t);
      /* Fall through.  */
      /* Fall through.  */
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
      {
      {
        tree decl;
        tree decl;
        cp_id_kind idk;
        cp_id_kind idk;
        bool non_integral_constant_expression_p;
        bool non_integral_constant_expression_p;
        const char *error_msg;
        const char *error_msg;
 
 
        if (IDENTIFIER_TYPENAME_P (t))
        if (IDENTIFIER_TYPENAME_P (t))
          {
          {
            tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
            t = mangle_conv_op_name_for_type (new_type);
            t = mangle_conv_op_name_for_type (new_type);
          }
          }
 
 
        /* Look up the name.  */
        /* Look up the name.  */
        decl = lookup_name (t);
        decl = lookup_name (t);
 
 
        /* By convention, expressions use ERROR_MARK_NODE to indicate
        /* By convention, expressions use ERROR_MARK_NODE to indicate
           failure, not NULL_TREE.  */
           failure, not NULL_TREE.  */
        if (decl == NULL_TREE)
        if (decl == NULL_TREE)
          decl = error_mark_node;
          decl = error_mark_node;
 
 
        decl = finish_id_expression (t, decl, NULL_TREE,
        decl = finish_id_expression (t, decl, NULL_TREE,
                                     &idk,
                                     &idk,
                                     integral_constant_expression_p,
                                     integral_constant_expression_p,
                                     /*allow_non_integral_constant_expression_p=*/false,
                                     /*allow_non_integral_constant_expression_p=*/false,
                                     &non_integral_constant_expression_p,
                                     &non_integral_constant_expression_p,
                                     /*template_p=*/false,
                                     /*template_p=*/false,
                                     /*done=*/true,
                                     /*done=*/true,
                                     /*address_p=*/false,
                                     /*address_p=*/false,
                                     /*template_arg_p=*/false,
                                     /*template_arg_p=*/false,
                                     &error_msg,
                                     &error_msg,
                                     input_location);
                                     input_location);
        if (error_msg)
        if (error_msg)
          error (error_msg);
          error (error_msg);
        if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
        if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
          decl = unqualified_name_lookup_error (decl);
          decl = unqualified_name_lookup_error (decl);
        return decl;
        return decl;
      }
      }
 
 
    case TEMPLATE_ID_EXPR:
    case TEMPLATE_ID_EXPR:
      {
      {
        tree object;
        tree object;
        tree templ = RECUR (TREE_OPERAND (t, 0));
        tree templ = RECUR (TREE_OPERAND (t, 0));
        tree targs = TREE_OPERAND (t, 1);
        tree targs = TREE_OPERAND (t, 1);
 
 
        if (targs)
        if (targs)
          targs = tsubst_template_args (targs, args, complain, in_decl);
          targs = tsubst_template_args (targs, args, complain, in_decl);
 
 
        if (TREE_CODE (templ) == COMPONENT_REF)
        if (TREE_CODE (templ) == COMPONENT_REF)
          {
          {
            object = TREE_OPERAND (templ, 0);
            object = TREE_OPERAND (templ, 0);
            templ = TREE_OPERAND (templ, 1);
            templ = TREE_OPERAND (templ, 1);
          }
          }
        else
        else
          object = NULL_TREE;
          object = NULL_TREE;
        templ = lookup_template_function (templ, targs);
        templ = lookup_template_function (templ, targs);
 
 
        if (object)
        if (object)
          return build3 (COMPONENT_REF, TREE_TYPE (templ),
          return build3 (COMPONENT_REF, TREE_TYPE (templ),
                         object, templ, NULL_TREE);
                         object, templ, NULL_TREE);
        else
        else
          return baselink_for_fns (templ);
          return baselink_for_fns (templ);
      }
      }
 
 
    case INDIRECT_REF:
    case INDIRECT_REF:
      {
      {
        tree r = RECUR (TREE_OPERAND (t, 0));
        tree r = RECUR (TREE_OPERAND (t, 0));
 
 
        if (REFERENCE_REF_P (t))
        if (REFERENCE_REF_P (t))
          {
          {
            /* A type conversion to reference type will be enclosed in
            /* A type conversion to reference type will be enclosed in
               such an indirect ref, but the substitution of the cast
               such an indirect ref, but the substitution of the cast
               will have also added such an indirect ref.  */
               will have also added such an indirect ref.  */
            if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
            if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
              r = convert_from_reference (r);
              r = convert_from_reference (r);
          }
          }
        else
        else
          r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
          r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
        return r;
        return r;
      }
      }
 
 
    case NOP_EXPR:
    case NOP_EXPR:
      return build_nop
      return build_nop
        (tsubst (TREE_TYPE (t), args, complain, in_decl),
        (tsubst (TREE_TYPE (t), args, complain, in_decl),
         RECUR (TREE_OPERAND (t, 0)));
         RECUR (TREE_OPERAND (t, 0)));
 
 
    case CAST_EXPR:
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CONST_CAST_EXPR:
    case CONST_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
      {
      {
        tree type;
        tree type;
        tree op;
        tree op;
 
 
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        if (integral_constant_expression_p
        if (integral_constant_expression_p
            && !cast_valid_in_integral_constant_expression_p (type))
            && !cast_valid_in_integral_constant_expression_p (type))
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              error ("a cast to a type other than an integral or "
              error ("a cast to a type other than an integral or "
                     "enumeration type cannot appear in a constant-expression");
                     "enumeration type cannot appear in a constant-expression");
            return error_mark_node;
            return error_mark_node;
          }
          }
 
 
        op = RECUR (TREE_OPERAND (t, 0));
        op = RECUR (TREE_OPERAND (t, 0));
 
 
        switch (TREE_CODE (t))
        switch (TREE_CODE (t))
          {
          {
          case CAST_EXPR:
          case CAST_EXPR:
            return build_functional_cast (type, op, complain);
            return build_functional_cast (type, op, complain);
          case REINTERPRET_CAST_EXPR:
          case REINTERPRET_CAST_EXPR:
            return build_reinterpret_cast (type, op, complain);
            return build_reinterpret_cast (type, op, complain);
          case CONST_CAST_EXPR:
          case CONST_CAST_EXPR:
            return build_const_cast (type, op, complain);
            return build_const_cast (type, op, complain);
          case DYNAMIC_CAST_EXPR:
          case DYNAMIC_CAST_EXPR:
            return build_dynamic_cast (type, op, complain);
            return build_dynamic_cast (type, op, complain);
          case STATIC_CAST_EXPR:
          case STATIC_CAST_EXPR:
            return build_static_cast (type, op, complain);
            return build_static_cast (type, op, complain);
          default:
          default:
            gcc_unreachable ();
            gcc_unreachable ();
          }
          }
      }
      }
 
 
    case POSTDECREMENT_EXPR:
    case POSTDECREMENT_EXPR:
    case POSTINCREMENT_EXPR:
    case POSTINCREMENT_EXPR:
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
                                                args, complain, in_decl);
                                                args, complain, in_decl);
      return build_x_unary_op (TREE_CODE (t), op1, complain);
      return build_x_unary_op (TREE_CODE (t), op1, complain);
 
 
    case PREDECREMENT_EXPR:
    case PREDECREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case NEGATE_EXPR:
    case NEGATE_EXPR:
    case BIT_NOT_EXPR:
    case BIT_NOT_EXPR:
    case ABS_EXPR:
    case ABS_EXPR:
    case TRUTH_NOT_EXPR:
    case TRUTH_NOT_EXPR:
    case UNARY_PLUS_EXPR:  /* Unary + */
    case UNARY_PLUS_EXPR:  /* Unary + */
    case REALPART_EXPR:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case IMAGPART_EXPR:
      return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
      return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
                               complain);
                               complain);
 
 
    case ADDR_EXPR:
    case ADDR_EXPR:
      op1 = TREE_OPERAND (t, 0);
      op1 = TREE_OPERAND (t, 0);
      if (TREE_CODE (op1) == SCOPE_REF)
      if (TREE_CODE (op1) == SCOPE_REF)
        op1 = tsubst_qualified_id (op1, args, complain, in_decl,
        op1 = tsubst_qualified_id (op1, args, complain, in_decl,
                                   /*done=*/true, /*address_p=*/true);
                                   /*done=*/true, /*address_p=*/true);
      else
      else
        op1 = tsubst_non_call_postfix_expression (op1, args, complain,
        op1 = tsubst_non_call_postfix_expression (op1, args, complain,
                                                  in_decl);
                                                  in_decl);
      if (TREE_CODE (op1) == LABEL_DECL)
      if (TREE_CODE (op1) == LABEL_DECL)
        return finish_label_address_expr (DECL_NAME (op1),
        return finish_label_address_expr (DECL_NAME (op1),
                                          EXPR_LOCATION (op1));
                                          EXPR_LOCATION (op1));
      return build_x_unary_op (ADDR_EXPR, op1, complain);
      return build_x_unary_op (ADDR_EXPR, op1, complain);
 
 
    case PLUS_EXPR:
    case PLUS_EXPR:
    case MINUS_EXPR:
    case MINUS_EXPR:
    case MULT_EXPR:
    case MULT_EXPR:
    case TRUNC_DIV_EXPR:
    case TRUNC_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case EXACT_DIV_EXPR:
    case EXACT_DIV_EXPR:
    case BIT_AND_EXPR:
    case BIT_AND_EXPR:
    case BIT_IOR_EXPR:
    case BIT_IOR_EXPR:
    case BIT_XOR_EXPR:
    case BIT_XOR_EXPR:
    case TRUNC_MOD_EXPR:
    case TRUNC_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case TRUTH_AND_EXPR:
    case TRUTH_AND_EXPR:
    case TRUTH_OR_EXPR:
    case TRUTH_OR_EXPR:
    case RSHIFT_EXPR:
    case RSHIFT_EXPR:
    case LSHIFT_EXPR:
    case LSHIFT_EXPR:
    case RROTATE_EXPR:
    case RROTATE_EXPR:
    case LROTATE_EXPR:
    case LROTATE_EXPR:
    case EQ_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
    case NE_EXPR:
    case MAX_EXPR:
    case MAX_EXPR:
    case MIN_EXPR:
    case MIN_EXPR:
    case LE_EXPR:
    case LE_EXPR:
    case GE_EXPR:
    case GE_EXPR:
    case LT_EXPR:
    case LT_EXPR:
    case GT_EXPR:
    case GT_EXPR:
    case MEMBER_REF:
    case MEMBER_REF:
    case DOTSTAR_EXPR:
    case DOTSTAR_EXPR:
      return build_x_binary_op
      return build_x_binary_op
        (TREE_CODE (t),
        (TREE_CODE (t),
         RECUR (TREE_OPERAND (t, 0)),
         RECUR (TREE_OPERAND (t, 0)),
         (TREE_NO_WARNING (TREE_OPERAND (t, 0))
         (TREE_NO_WARNING (TREE_OPERAND (t, 0))
          ? ERROR_MARK
          ? ERROR_MARK
          : TREE_CODE (TREE_OPERAND (t, 0))),
          : TREE_CODE (TREE_OPERAND (t, 0))),
         RECUR (TREE_OPERAND (t, 1)),
         RECUR (TREE_OPERAND (t, 1)),
         (TREE_NO_WARNING (TREE_OPERAND (t, 1))
         (TREE_NO_WARNING (TREE_OPERAND (t, 1))
          ? ERROR_MARK
          ? ERROR_MARK
          : TREE_CODE (TREE_OPERAND (t, 1))),
          : TREE_CODE (TREE_OPERAND (t, 1))),
         /*overloaded_p=*/NULL,
         /*overloaded_p=*/NULL,
         complain);
         complain);
 
 
    case SCOPE_REF:
    case SCOPE_REF:
      return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
      return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
                                  /*address_p=*/false);
                                  /*address_p=*/false);
    case ARRAY_REF:
    case ARRAY_REF:
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
                                                args, complain, in_decl);
                                                args, complain, in_decl);
      return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
      return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
 
 
    case SIZEOF_EXPR:
    case SIZEOF_EXPR:
      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
        return tsubst_copy (t, args, complain, in_decl);
        return tsubst_copy (t, args, complain, in_decl);
      /* Fall through */
      /* Fall through */
 
 
    case ALIGNOF_EXPR:
    case ALIGNOF_EXPR:
      op1 = TREE_OPERAND (t, 0);
      op1 = TREE_OPERAND (t, 0);
      if (!args)
      if (!args)
        {
        {
          /* When there are no ARGS, we are trying to evaluate a
          /* When there are no ARGS, we are trying to evaluate a
             non-dependent expression from the parser.  Trying to do
             non-dependent expression from the parser.  Trying to do
             the substitutions may not work.  */
             the substitutions may not work.  */
          if (!TYPE_P (op1))
          if (!TYPE_P (op1))
            op1 = TREE_TYPE (op1);
            op1 = TREE_TYPE (op1);
        }
        }
      else
      else
        {
        {
          ++cp_unevaluated_operand;
          ++cp_unevaluated_operand;
          ++c_inhibit_evaluation_warnings;
          ++c_inhibit_evaluation_warnings;
          op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
          op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
                                       /*function_p=*/false,
                                       /*function_p=*/false,
                                       /*integral_constant_expression_p=*/false);
                                       /*integral_constant_expression_p=*/false);
          --cp_unevaluated_operand;
          --cp_unevaluated_operand;
          --c_inhibit_evaluation_warnings;
          --c_inhibit_evaluation_warnings;
        }
        }
      if (TYPE_P (op1))
      if (TYPE_P (op1))
        return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
        return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
                                           complain & tf_error);
                                           complain & tf_error);
      else
      else
        return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
        return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
                                           complain & tf_error);
                                           complain & tf_error);
 
 
    case MODOP_EXPR:
    case MODOP_EXPR:
      {
      {
        tree r = build_x_modify_expr
        tree r = build_x_modify_expr
          (RECUR (TREE_OPERAND (t, 0)),
          (RECUR (TREE_OPERAND (t, 0)),
           TREE_CODE (TREE_OPERAND (t, 1)),
           TREE_CODE (TREE_OPERAND (t, 1)),
           RECUR (TREE_OPERAND (t, 2)),
           RECUR (TREE_OPERAND (t, 2)),
           complain);
           complain);
        /* TREE_NO_WARNING must be set if either the expression was
        /* TREE_NO_WARNING must be set if either the expression was
           parenthesized or it uses an operator such as >>= rather
           parenthesized or it uses an operator such as >>= rather
           than plain assignment.  In the former case, it was already
           than plain assignment.  In the former case, it was already
           set and must be copied.  In the latter case,
           set and must be copied.  In the latter case,
           build_x_modify_expr sets it and it must not be reset
           build_x_modify_expr sets it and it must not be reset
           here.  */
           here.  */
        if (TREE_NO_WARNING (t))
        if (TREE_NO_WARNING (t))
          TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
          TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
        return r;
        return r;
      }
      }
 
 
    case ARROW_EXPR:
    case ARROW_EXPR:
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
                                                args, complain, in_decl);
                                                args, complain, in_decl);
      /* Remember that there was a reference to this entity.  */
      /* Remember that there was a reference to this entity.  */
      if (DECL_P (op1))
      if (DECL_P (op1))
        mark_used (op1);
        mark_used (op1);
      return build_x_arrow (op1);
      return build_x_arrow (op1);
 
 
    case NEW_EXPR:
    case NEW_EXPR:
      {
      {
        tree placement = RECUR (TREE_OPERAND (t, 0));
        tree placement = RECUR (TREE_OPERAND (t, 0));
        tree init = RECUR (TREE_OPERAND (t, 3));
        tree init = RECUR (TREE_OPERAND (t, 3));
        VEC(tree,gc) *placement_vec;
        VEC(tree,gc) *placement_vec;
        VEC(tree,gc) *init_vec;
        VEC(tree,gc) *init_vec;
        tree ret;
        tree ret;
 
 
        if (placement == NULL_TREE)
        if (placement == NULL_TREE)
          placement_vec = NULL;
          placement_vec = NULL;
        else
        else
          {
          {
            placement_vec = make_tree_vector ();
            placement_vec = make_tree_vector ();
            for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
            for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
              VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
              VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
          }
          }
 
 
        /* If there was an initializer in the original tree, but it
        /* If there was an initializer in the original tree, but it
           instantiated to an empty list, then we should pass a
           instantiated to an empty list, then we should pass a
           non-NULL empty vector to tell build_new that it was an
           non-NULL empty vector to tell build_new that it was an
           empty initializer() rather than no initializer.  This can
           empty initializer() rather than no initializer.  This can
           only happen when the initializer is a pack expansion whose
           only happen when the initializer is a pack expansion whose
           parameter packs are of length zero.  */
           parameter packs are of length zero.  */
        if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
        if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
          init_vec = NULL;
          init_vec = NULL;
        else
        else
          {
          {
            init_vec = make_tree_vector ();
            init_vec = make_tree_vector ();
            if (init == void_zero_node)
            if (init == void_zero_node)
              gcc_assert (init_vec != NULL);
              gcc_assert (init_vec != NULL);
            else
            else
              {
              {
                for (; init != NULL_TREE; init = TREE_CHAIN (init))
                for (; init != NULL_TREE; init = TREE_CHAIN (init))
                  VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
                  VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
              }
              }
          }
          }
 
 
        ret = build_new (&placement_vec,
        ret = build_new (&placement_vec,
                         RECUR (TREE_OPERAND (t, 1)),
                         RECUR (TREE_OPERAND (t, 1)),
                         RECUR (TREE_OPERAND (t, 2)),
                         RECUR (TREE_OPERAND (t, 2)),
                         &init_vec,
                         &init_vec,
                         NEW_EXPR_USE_GLOBAL (t),
                         NEW_EXPR_USE_GLOBAL (t),
                         complain);
                         complain);
 
 
        if (placement_vec != NULL)
        if (placement_vec != NULL)
          release_tree_vector (placement_vec);
          release_tree_vector (placement_vec);
        if (init_vec != NULL)
        if (init_vec != NULL)
          release_tree_vector (init_vec);
          release_tree_vector (init_vec);
 
 
        return ret;
        return ret;
      }
      }
 
 
    case DELETE_EXPR:
    case DELETE_EXPR:
     return delete_sanity
     return delete_sanity
       (RECUR (TREE_OPERAND (t, 0)),
       (RECUR (TREE_OPERAND (t, 0)),
        RECUR (TREE_OPERAND (t, 1)),
        RECUR (TREE_OPERAND (t, 1)),
        DELETE_EXPR_USE_VEC (t),
        DELETE_EXPR_USE_VEC (t),
        DELETE_EXPR_USE_GLOBAL (t));
        DELETE_EXPR_USE_GLOBAL (t));
 
 
    case COMPOUND_EXPR:
    case COMPOUND_EXPR:
      return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
      return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
                                    RECUR (TREE_OPERAND (t, 1)),
                                    RECUR (TREE_OPERAND (t, 1)),
                                    complain);
                                    complain);
 
 
    case CALL_EXPR:
    case CALL_EXPR:
      {
      {
        tree function;
        tree function;
        VEC(tree,gc) *call_args;
        VEC(tree,gc) *call_args;
        unsigned int nargs, i;
        unsigned int nargs, i;
        bool qualified_p;
        bool qualified_p;
        bool koenig_p;
        bool koenig_p;
        tree ret;
        tree ret;
 
 
        function = CALL_EXPR_FN (t);
        function = CALL_EXPR_FN (t);
        /* When we parsed the expression,  we determined whether or
        /* When we parsed the expression,  we determined whether or
           not Koenig lookup should be performed.  */
           not Koenig lookup should be performed.  */
        koenig_p = KOENIG_LOOKUP_P (t);
        koenig_p = KOENIG_LOOKUP_P (t);
        if (TREE_CODE (function) == SCOPE_REF)
        if (TREE_CODE (function) == SCOPE_REF)
          {
          {
            qualified_p = true;
            qualified_p = true;
            function = tsubst_qualified_id (function, args, complain, in_decl,
            function = tsubst_qualified_id (function, args, complain, in_decl,
                                            /*done=*/false,
                                            /*done=*/false,
                                            /*address_p=*/false);
                                            /*address_p=*/false);
          }
          }
        else
        else
          {
          {
            if (TREE_CODE (function) == COMPONENT_REF)
            if (TREE_CODE (function) == COMPONENT_REF)
              {
              {
                tree op = TREE_OPERAND (function, 1);
                tree op = TREE_OPERAND (function, 1);
 
 
                qualified_p = (TREE_CODE (op) == SCOPE_REF
                qualified_p = (TREE_CODE (op) == SCOPE_REF
                               || (BASELINK_P (op)
                               || (BASELINK_P (op)
                                   && BASELINK_QUALIFIED_P (op)));
                                   && BASELINK_QUALIFIED_P (op)));
              }
              }
            else
            else
              qualified_p = false;
              qualified_p = false;
 
 
            function = tsubst_copy_and_build (function, args, complain,
            function = tsubst_copy_and_build (function, args, complain,
                                              in_decl,
                                              in_decl,
                                              !qualified_p,
                                              !qualified_p,
                                              integral_constant_expression_p);
                                              integral_constant_expression_p);
 
 
            if (BASELINK_P (function))
            if (BASELINK_P (function))
              qualified_p = true;
              qualified_p = true;
          }
          }
 
 
        nargs = call_expr_nargs (t);
        nargs = call_expr_nargs (t);
        call_args = make_tree_vector ();
        call_args = make_tree_vector ();
        for (i = 0; i < nargs; ++i)
        for (i = 0; i < nargs; ++i)
          {
          {
            tree arg = CALL_EXPR_ARG (t, i);
            tree arg = CALL_EXPR_ARG (t, i);
 
 
            if (!PACK_EXPANSION_P (arg))
            if (!PACK_EXPANSION_P (arg))
              VEC_safe_push (tree, gc, call_args,
              VEC_safe_push (tree, gc, call_args,
                             RECUR (CALL_EXPR_ARG (t, i)));
                             RECUR (CALL_EXPR_ARG (t, i)));
            else
            else
              {
              {
                /* Expand the pack expansion and push each entry onto
                /* Expand the pack expansion and push each entry onto
                   CALL_ARGS.  */
                   CALL_ARGS.  */
                arg = tsubst_pack_expansion (arg, args, complain, in_decl);
                arg = tsubst_pack_expansion (arg, args, complain, in_decl);
                if (TREE_CODE (arg) == TREE_VEC)
                if (TREE_CODE (arg) == TREE_VEC)
                  {
                  {
                    unsigned int len, j;
                    unsigned int len, j;
 
 
                    len = TREE_VEC_LENGTH (arg);
                    len = TREE_VEC_LENGTH (arg);
                    for (j = 0; j < len; ++j)
                    for (j = 0; j < len; ++j)
                      {
                      {
                        tree value = TREE_VEC_ELT (arg, j);
                        tree value = TREE_VEC_ELT (arg, j);
                        if (value != NULL_TREE)
                        if (value != NULL_TREE)
                          value = convert_from_reference (value);
                          value = convert_from_reference (value);
                        VEC_safe_push (tree, gc, call_args, value);
                        VEC_safe_push (tree, gc, call_args, value);
                      }
                      }
                  }
                  }
                else
                else
                  {
                  {
                    /* A partial substitution.  Add one entry.  */
                    /* A partial substitution.  Add one entry.  */
                    VEC_safe_push (tree, gc, call_args, arg);
                    VEC_safe_push (tree, gc, call_args, arg);
                  }
                  }
              }
              }
          }
          }
 
 
        /* We do not perform argument-dependent lookup if normal
        /* We do not perform argument-dependent lookup if normal
           lookup finds a non-function, in accordance with the
           lookup finds a non-function, in accordance with the
           expected resolution of DR 218.  */
           expected resolution of DR 218.  */
        if (koenig_p
        if (koenig_p
            && ((is_overloaded_fn (function)
            && ((is_overloaded_fn (function)
                 /* If lookup found a member function, the Koenig lookup is
                 /* If lookup found a member function, the Koenig lookup is
                    not appropriate, even if an unqualified-name was used
                    not appropriate, even if an unqualified-name was used
                    to denote the function.  */
                    to denote the function.  */
                 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
                 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
                || TREE_CODE (function) == IDENTIFIER_NODE)
                || TREE_CODE (function) == IDENTIFIER_NODE)
            /* Only do this when substitution turns a dependent call
            /* Only do this when substitution turns a dependent call
               into a non-dependent call.  */
               into a non-dependent call.  */
            && type_dependent_expression_p_push (t)
            && type_dependent_expression_p_push (t)
            && !any_type_dependent_arguments_p (call_args))
            && !any_type_dependent_arguments_p (call_args))
          function = perform_koenig_lookup (function, call_args);
          function = perform_koenig_lookup (function, call_args);
 
 
        if (TREE_CODE (function) == IDENTIFIER_NODE)
        if (TREE_CODE (function) == IDENTIFIER_NODE)
          {
          {
            unqualified_name_lookup_error (function);
            unqualified_name_lookup_error (function);
            release_tree_vector (call_args);
            release_tree_vector (call_args);
            return error_mark_node;
            return error_mark_node;
          }
          }
 
 
        /* Remember that there was a reference to this entity.  */
        /* Remember that there was a reference to this entity.  */
        if (DECL_P (function))
        if (DECL_P (function))
          mark_used (function);
          mark_used (function);
 
 
        if (TREE_CODE (function) == OFFSET_REF)
        if (TREE_CODE (function) == OFFSET_REF)
          ret = build_offset_ref_call_from_tree (function, &call_args);
          ret = build_offset_ref_call_from_tree (function, &call_args);
        else if (TREE_CODE (function) == COMPONENT_REF)
        else if (TREE_CODE (function) == COMPONENT_REF)
          {
          {
            if (!BASELINK_P (TREE_OPERAND (function, 1)))
            if (!BASELINK_P (TREE_OPERAND (function, 1)))
              ret = finish_call_expr (function, &call_args,
              ret = finish_call_expr (function, &call_args,
                                       /*disallow_virtual=*/false,
                                       /*disallow_virtual=*/false,
                                       /*koenig_p=*/false,
                                       /*koenig_p=*/false,
                                       complain);
                                       complain);
            else
            else
              ret = (build_new_method_call
              ret = (build_new_method_call
                      (TREE_OPERAND (function, 0),
                      (TREE_OPERAND (function, 0),
                       TREE_OPERAND (function, 1),
                       TREE_OPERAND (function, 1),
                       &call_args, NULL_TREE,
                       &call_args, NULL_TREE,
                       qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
                       qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
                       /*fn_p=*/NULL,
                       /*fn_p=*/NULL,
                       complain));
                       complain));
          }
          }
        else
        else
          ret = finish_call_expr (function, &call_args,
          ret = finish_call_expr (function, &call_args,
                                  /*disallow_virtual=*/qualified_p,
                                  /*disallow_virtual=*/qualified_p,
                                  koenig_p,
                                  koenig_p,
                                  complain);
                                  complain);
 
 
        release_tree_vector (call_args);
        release_tree_vector (call_args);
 
 
        return ret;
        return ret;
      }
      }
 
 
    case COND_EXPR:
    case COND_EXPR:
      return build_x_conditional_expr
      return build_x_conditional_expr
        (RECUR (TREE_OPERAND (t, 0)),
        (RECUR (TREE_OPERAND (t, 0)),
         RECUR (TREE_OPERAND (t, 1)),
         RECUR (TREE_OPERAND (t, 1)),
         RECUR (TREE_OPERAND (t, 2)),
         RECUR (TREE_OPERAND (t, 2)),
         complain);
         complain);
 
 
    case PSEUDO_DTOR_EXPR:
    case PSEUDO_DTOR_EXPR:
      return finish_pseudo_destructor_expr
      return finish_pseudo_destructor_expr
        (RECUR (TREE_OPERAND (t, 0)),
        (RECUR (TREE_OPERAND (t, 0)),
         RECUR (TREE_OPERAND (t, 1)),
         RECUR (TREE_OPERAND (t, 1)),
         RECUR (TREE_OPERAND (t, 2)));
         RECUR (TREE_OPERAND (t, 2)));
 
 
    case TREE_LIST:
    case TREE_LIST:
      {
      {
        tree purpose, value, chain;
        tree purpose, value, chain;
 
 
        if (t == void_list_node)
        if (t == void_list_node)
          return t;
          return t;
 
 
        if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
        if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
            || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
            || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
          {
          {
            /* We have pack expansions, so expand those and
            /* We have pack expansions, so expand those and
               create a new list out of it.  */
               create a new list out of it.  */
            tree purposevec = NULL_TREE;
            tree purposevec = NULL_TREE;
            tree valuevec = NULL_TREE;
            tree valuevec = NULL_TREE;
            tree chain;
            tree chain;
            int i, len = -1;
            int i, len = -1;
 
 
            /* Expand the argument expressions.  */
            /* Expand the argument expressions.  */
            if (TREE_PURPOSE (t))
            if (TREE_PURPOSE (t))
              purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
              purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
                                                 complain, in_decl);
                                                 complain, in_decl);
            if (TREE_VALUE (t))
            if (TREE_VALUE (t))
              valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
              valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
                                               complain, in_decl);
                                               complain, in_decl);
 
 
            /* Build the rest of the list.  */
            /* Build the rest of the list.  */
            chain = TREE_CHAIN (t);
            chain = TREE_CHAIN (t);
            if (chain && chain != void_type_node)
            if (chain && chain != void_type_node)
              chain = RECUR (chain);
              chain = RECUR (chain);
 
 
            /* Determine the number of arguments.  */
            /* Determine the number of arguments.  */
            if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
            if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
              {
              {
                len = TREE_VEC_LENGTH (purposevec);
                len = TREE_VEC_LENGTH (purposevec);
                gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
                gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
              }
              }
            else if (TREE_CODE (valuevec) == TREE_VEC)
            else if (TREE_CODE (valuevec) == TREE_VEC)
              len = TREE_VEC_LENGTH (valuevec);
              len = TREE_VEC_LENGTH (valuevec);
            else
            else
              {
              {
                /* Since we only performed a partial substitution into
                /* Since we only performed a partial substitution into
                   the argument pack, we only return a single list
                   the argument pack, we only return a single list
                   node.  */
                   node.  */
                if (purposevec == TREE_PURPOSE (t)
                if (purposevec == TREE_PURPOSE (t)
                    && valuevec == TREE_VALUE (t)
                    && valuevec == TREE_VALUE (t)
                    && chain == TREE_CHAIN (t))
                    && chain == TREE_CHAIN (t))
                  return t;
                  return t;
 
 
                return tree_cons (purposevec, valuevec, chain);
                return tree_cons (purposevec, valuevec, chain);
              }
              }
 
 
            /* Convert the argument vectors into a TREE_LIST */
            /* Convert the argument vectors into a TREE_LIST */
            i = len;
            i = len;
            while (i > 0)
            while (i > 0)
              {
              {
                /* Grab the Ith values.  */
                /* Grab the Ith values.  */
                i--;
                i--;
                purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
                purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
                                     : NULL_TREE;
                                     : NULL_TREE;
                value
                value
                  = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
                  = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
                             : NULL_TREE;
                             : NULL_TREE;
 
 
                /* Build the list (backwards).  */
                /* Build the list (backwards).  */
                chain = tree_cons (purpose, value, chain);
                chain = tree_cons (purpose, value, chain);
              }
              }
 
 
            return chain;
            return chain;
          }
          }
 
 
        purpose = TREE_PURPOSE (t);
        purpose = TREE_PURPOSE (t);
        if (purpose)
        if (purpose)
          purpose = RECUR (purpose);
          purpose = RECUR (purpose);
        value = TREE_VALUE (t);
        value = TREE_VALUE (t);
        if (value)
        if (value)
          value = RECUR (value);
          value = RECUR (value);
        chain = TREE_CHAIN (t);
        chain = TREE_CHAIN (t);
        if (chain && chain != void_type_node)
        if (chain && chain != void_type_node)
          chain = RECUR (chain);
          chain = RECUR (chain);
        if (purpose == TREE_PURPOSE (t)
        if (purpose == TREE_PURPOSE (t)
            && value == TREE_VALUE (t)
            && value == TREE_VALUE (t)
            && chain == TREE_CHAIN (t))
            && chain == TREE_CHAIN (t))
          return t;
          return t;
        return tree_cons (purpose, value, chain);
        return tree_cons (purpose, value, chain);
      }
      }
 
 
    case COMPONENT_REF:
    case COMPONENT_REF:
      {
      {
        tree object;
        tree object;
        tree object_type;
        tree object_type;
        tree member;
        tree member;
 
 
        object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
        object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
                                                     args, complain, in_decl);
                                                     args, complain, in_decl);
        /* Remember that there was a reference to this entity.  */
        /* Remember that there was a reference to this entity.  */
        if (DECL_P (object))
        if (DECL_P (object))
          mark_used (object);
          mark_used (object);
        object_type = TREE_TYPE (object);
        object_type = TREE_TYPE (object);
 
 
        member = TREE_OPERAND (t, 1);
        member = TREE_OPERAND (t, 1);
        if (BASELINK_P (member))
        if (BASELINK_P (member))
          member = tsubst_baselink (member,
          member = tsubst_baselink (member,
                                    non_reference (TREE_TYPE (object)),
                                    non_reference (TREE_TYPE (object)),
                                    args, complain, in_decl);
                                    args, complain, in_decl);
        else
        else
          member = tsubst_copy (member, args, complain, in_decl);
          member = tsubst_copy (member, args, complain, in_decl);
        if (member == error_mark_node)
        if (member == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        if (object_type && !CLASS_TYPE_P (object_type))
        if (object_type && !CLASS_TYPE_P (object_type))
          {
          {
            if (SCALAR_TYPE_P (object_type))
            if (SCALAR_TYPE_P (object_type))
              {
              {
                tree s = NULL_TREE;
                tree s = NULL_TREE;
                tree dtor = member;
                tree dtor = member;
 
 
                if (TREE_CODE (dtor) == SCOPE_REF)
                if (TREE_CODE (dtor) == SCOPE_REF)
                  {
                  {
                    s = TREE_OPERAND (dtor, 0);
                    s = TREE_OPERAND (dtor, 0);
                    dtor = TREE_OPERAND (dtor, 1);
                    dtor = TREE_OPERAND (dtor, 1);
                  }
                  }
                if (TREE_CODE (dtor) == BIT_NOT_EXPR)
                if (TREE_CODE (dtor) == BIT_NOT_EXPR)
                  {
                  {
                    dtor = TREE_OPERAND (dtor, 0);
                    dtor = TREE_OPERAND (dtor, 0);
                    if (TYPE_P (dtor))
                    if (TYPE_P (dtor))
                      return finish_pseudo_destructor_expr (object, s, dtor);
                      return finish_pseudo_destructor_expr (object, s, dtor);
                  }
                  }
              }
              }
          }
          }
        else if (TREE_CODE (member) == SCOPE_REF
        else if (TREE_CODE (member) == SCOPE_REF
                 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
                 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
          {
          {
            tree tmpl;
            tree tmpl;
            tree args;
            tree args;
 
 
            /* Lookup the template functions now that we know what the
            /* Lookup the template functions now that we know what the
               scope is.  */
               scope is.  */
            tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
            tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
            args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
            args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
            member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
            member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
                                            /*is_type_p=*/false,
                                            /*is_type_p=*/false,
                                            /*complain=*/false);
                                            /*complain=*/false);
            if (BASELINK_P (member))
            if (BASELINK_P (member))
              {
              {
                BASELINK_FUNCTIONS (member)
                BASELINK_FUNCTIONS (member)
                  = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
                  = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
                              args);
                              args);
                member = (adjust_result_of_qualified_name_lookup
                member = (adjust_result_of_qualified_name_lookup
                          (member, BINFO_TYPE (BASELINK_BINFO (member)),
                          (member, BINFO_TYPE (BASELINK_BINFO (member)),
                           object_type));
                           object_type));
              }
              }
            else
            else
              {
              {
                qualified_name_lookup_error (object_type, tmpl, member,
                qualified_name_lookup_error (object_type, tmpl, member,
                                             input_location);
                                             input_location);
                return error_mark_node;
                return error_mark_node;
              }
              }
          }
          }
        else if (TREE_CODE (member) == SCOPE_REF
        else if (TREE_CODE (member) == SCOPE_REF
                 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
                 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
                 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
                 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
          {
          {
            if (complain & tf_error)
            if (complain & tf_error)
              {
              {
                if (TYPE_P (TREE_OPERAND (member, 0)))
                if (TYPE_P (TREE_OPERAND (member, 0)))
                  error ("%qT is not a class or namespace",
                  error ("%qT is not a class or namespace",
                         TREE_OPERAND (member, 0));
                         TREE_OPERAND (member, 0));
                else
                else
                  error ("%qD is not a class or namespace",
                  error ("%qD is not a class or namespace",
                         TREE_OPERAND (member, 0));
                         TREE_OPERAND (member, 0));
              }
              }
            return error_mark_node;
            return error_mark_node;
          }
          }
        else if (TREE_CODE (member) == FIELD_DECL)
        else if (TREE_CODE (member) == FIELD_DECL)
          return finish_non_static_data_member (member, object, NULL_TREE);
          return finish_non_static_data_member (member, object, NULL_TREE);
 
 
        return finish_class_member_access_expr (object, member,
        return finish_class_member_access_expr (object, member,
                                                /*template_p=*/false,
                                                /*template_p=*/false,
                                                complain);
                                                complain);
      }
      }
 
 
    case THROW_EXPR:
    case THROW_EXPR:
      return build_throw
      return build_throw
        (RECUR (TREE_OPERAND (t, 0)));
        (RECUR (TREE_OPERAND (t, 0)));
 
 
    case CONSTRUCTOR:
    case CONSTRUCTOR:
      {
      {
        VEC(constructor_elt,gc) *n;
        VEC(constructor_elt,gc) *n;
        constructor_elt *ce;
        constructor_elt *ce;
        unsigned HOST_WIDE_INT idx;
        unsigned HOST_WIDE_INT idx;
        tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
        bool process_index_p;
        bool process_index_p;
        int newlen;
        int newlen;
        bool need_copy_p = false;
        bool need_copy_p = false;
        tree r;
        tree r;
 
 
        if (type == error_mark_node)
        if (type == error_mark_node)
          return error_mark_node;
          return error_mark_node;
 
 
        /* digest_init will do the wrong thing if we let it.  */
        /* digest_init will do the wrong thing if we let it.  */
        if (type && TYPE_PTRMEMFUNC_P (type))
        if (type && TYPE_PTRMEMFUNC_P (type))
          return t;
          return t;
 
 
        /* We do not want to process the index of aggregate
        /* We do not want to process the index of aggregate
           initializers as they are identifier nodes which will be
           initializers as they are identifier nodes which will be
           looked up by digest_init.  */
           looked up by digest_init.  */
        process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
        process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
 
 
        n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
        n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
        newlen = VEC_length (constructor_elt, n);
        newlen = VEC_length (constructor_elt, n);
        for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
        for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
          {
          {
            if (ce->index && process_index_p)
            if (ce->index && process_index_p)
              ce->index = RECUR (ce->index);
              ce->index = RECUR (ce->index);
 
 
            if (PACK_EXPANSION_P (ce->value))
            if (PACK_EXPANSION_P (ce->value))
              {
              {
                /* Substitute into the pack expansion.  */
                /* Substitute into the pack expansion.  */
                ce->value = tsubst_pack_expansion (ce->value, args, complain,
                ce->value = tsubst_pack_expansion (ce->value, args, complain,
                                                  in_decl);
                                                  in_decl);
 
 
                if (ce->value == error_mark_node)
                if (ce->value == error_mark_node)
                  ;
                  ;
                else if (TREE_VEC_LENGTH (ce->value) == 1)
                else if (TREE_VEC_LENGTH (ce->value) == 1)
                  /* Just move the argument into place.  */
                  /* Just move the argument into place.  */
                  ce->value = TREE_VEC_ELT (ce->value, 0);
                  ce->value = TREE_VEC_ELT (ce->value, 0);
                else
                else
                  {
                  {
                    /* Update the length of the final CONSTRUCTOR
                    /* Update the length of the final CONSTRUCTOR
                       arguments vector, and note that we will need to
                       arguments vector, and note that we will need to
                       copy.*/
                       copy.*/
                    newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
                    newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
                    need_copy_p = true;
                    need_copy_p = true;
                  }
                  }
              }
              }
            else
            else
              ce->value = RECUR (ce->value);
              ce->value = RECUR (ce->value);
          }
          }
 
 
        if (need_copy_p)
        if (need_copy_p)
          {
          {
            VEC(constructor_elt,gc) *old_n = n;
            VEC(constructor_elt,gc) *old_n = n;
 
 
            n = VEC_alloc (constructor_elt, gc, newlen);
            n = VEC_alloc (constructor_elt, gc, newlen);
            for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
            for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
                 idx++)
                 idx++)
              {
              {
                if (TREE_CODE (ce->value) == TREE_VEC)
                if (TREE_CODE (ce->value) == TREE_VEC)
                  {
                  {
                    int i, len = TREE_VEC_LENGTH (ce->value);
                    int i, len = TREE_VEC_LENGTH (ce->value);
                    for (i = 0; i < len; ++i)
                    for (i = 0; i < len; ++i)
                      CONSTRUCTOR_APPEND_ELT (n, 0,
                      CONSTRUCTOR_APPEND_ELT (n, 0,
                                              TREE_VEC_ELT (ce->value, i));
                                              TREE_VEC_ELT (ce->value, i));
                  }
                  }
                else
                else
                  CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
                  CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
              }
              }
          }
          }
 
 
        r = build_constructor (init_list_type_node, n);
        r = build_constructor (init_list_type_node, n);
        CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
        CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
 
 
        if (TREE_HAS_CONSTRUCTOR (t))
        if (TREE_HAS_CONSTRUCTOR (t))
          return finish_compound_literal (type, r);
          return finish_compound_literal (type, r);
 
 
        return r;
        return r;
      }
      }
 
 
    case TYPEID_EXPR:
    case TYPEID_EXPR:
      {
      {
        tree operand_0 = RECUR (TREE_OPERAND (t, 0));
        tree operand_0 = RECUR (TREE_OPERAND (t, 0));
        if (TYPE_P (operand_0))
        if (TYPE_P (operand_0))
          return get_typeid (operand_0);
          return get_typeid (operand_0);
        return build_typeid (operand_0);
        return build_typeid (operand_0);
      }
      }
 
 
    case VAR_DECL:
    case VAR_DECL:
      if (!args)
      if (!args)
        return t;
        return t;
      /* Fall through */
      /* Fall through */
 
 
    case PARM_DECL:
    case PARM_DECL:
      {
      {
        tree r = tsubst_copy (t, args, complain, in_decl);
        tree r = tsubst_copy (t, args, complain, in_decl);
 
 
        if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
        if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
          /* If the original type was a reference, we'll be wrapped in
          /* If the original type was a reference, we'll be wrapped in
             the appropriate INDIRECT_REF.  */
             the appropriate INDIRECT_REF.  */
          r = convert_from_reference (r);
          r = convert_from_reference (r);
        return r;
        return r;
      }
      }
 
 
    case VA_ARG_EXPR:
    case VA_ARG_EXPR:
      return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
      return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
                             tsubst_copy (TREE_TYPE (t), args, complain,
                             tsubst_copy (TREE_TYPE (t), args, complain,
                                          in_decl));
                                          in_decl));
 
 
    case OFFSETOF_EXPR:
    case OFFSETOF_EXPR:
      return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
      return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
 
 
    case TRAIT_EXPR:
    case TRAIT_EXPR:
      {
      {
        tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
        tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
                                  complain, in_decl);
                                  complain, in_decl);
 
 
        tree type2 = TRAIT_EXPR_TYPE2 (t);
        tree type2 = TRAIT_EXPR_TYPE2 (t);
        if (type2)
        if (type2)
          type2 = tsubst_copy (type2, args, complain, in_decl);
          type2 = tsubst_copy (type2, args, complain, in_decl);
 
 
        return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
        return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
      }
      }
 
 
    case STMT_EXPR:
    case STMT_EXPR:
      {
      {
        tree old_stmt_expr = cur_stmt_expr;
        tree old_stmt_expr = cur_stmt_expr;
        tree stmt_expr = begin_stmt_expr ();
        tree stmt_expr = begin_stmt_expr ();
 
 
        cur_stmt_expr = stmt_expr;
        cur_stmt_expr = stmt_expr;
        tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
        tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
                     integral_constant_expression_p);
                     integral_constant_expression_p);
        stmt_expr = finish_stmt_expr (stmt_expr, false);
        stmt_expr = finish_stmt_expr (stmt_expr, false);
        cur_stmt_expr = old_stmt_expr;
        cur_stmt_expr = old_stmt_expr;
 
 
        /* If the resulting list of expression statement is empty,
        /* If the resulting list of expression statement is empty,
           fold it further into void_zero_node.  */
           fold it further into void_zero_node.  */
        if (empty_expr_stmt_p (stmt_expr))
        if (empty_expr_stmt_p (stmt_expr))
          stmt_expr = void_zero_node;
          stmt_expr = void_zero_node;
 
 
        return stmt_expr;
        return stmt_expr;
      }
      }
 
 
    case CONST_DECL:
    case CONST_DECL:
      t = tsubst_copy (t, args, complain, in_decl);
      t = tsubst_copy (t, args, complain, in_decl);
      /* As in finish_id_expression, we resolve enumeration constants
      /* As in finish_id_expression, we resolve enumeration constants
         to their underlying values.  */
         to their underlying values.  */
      if (TREE_CODE (t) == CONST_DECL)
      if (TREE_CODE (t) == CONST_DECL)
        {
        {
          used_types_insert (TREE_TYPE (t));
          used_types_insert (TREE_TYPE (t));
          return DECL_INITIAL (t);
          return DECL_INITIAL (t);
        }
        }
      return t;
      return t;
 
 
    case LAMBDA_EXPR:
    case LAMBDA_EXPR:
      {
      {
        tree r = build_lambda_expr ();
        tree r = build_lambda_expr ();
 
 
        tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
        tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
        TREE_TYPE (r) = type;
        TREE_TYPE (r) = type;
        CLASSTYPE_LAMBDA_EXPR (type) = r;
        CLASSTYPE_LAMBDA_EXPR (type) = r;
 
 
        LAMBDA_EXPR_LOCATION (r)
        LAMBDA_EXPR_LOCATION (r)
          = LAMBDA_EXPR_LOCATION (t);
          = LAMBDA_EXPR_LOCATION (t);
        LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
        LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
          = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
          = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
        LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
        LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
        LAMBDA_EXPR_DISCRIMINATOR (r)
        LAMBDA_EXPR_DISCRIMINATOR (r)
          = (LAMBDA_EXPR_DISCRIMINATOR (t));
          = (LAMBDA_EXPR_DISCRIMINATOR (t));
        LAMBDA_EXPR_CAPTURE_LIST (r)
        LAMBDA_EXPR_CAPTURE_LIST (r)
          = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
          = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
        LAMBDA_EXPR_THIS_CAPTURE (r)
        LAMBDA_EXPR_THIS_CAPTURE (r)
          = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
          = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
        LAMBDA_EXPR_EXTRA_SCOPE (r)
        LAMBDA_EXPR_EXTRA_SCOPE (r)
          = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
          = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
 
 
        /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
        /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
        determine_visibility (TYPE_NAME (type));
        determine_visibility (TYPE_NAME (type));
        /* Now that we know visibility, instantiate the type so we have a
        /* Now that we know visibility, instantiate the type so we have a
           declaration of the op() for later calls to lambda_function.  */
           declaration of the op() for later calls to lambda_function.  */
        complete_type (type);
        complete_type (type);
 
 
        type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
        type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
        if (type)
        if (type)
          apply_lambda_return_type (r, type);
          apply_lambda_return_type (r, type);
 
 
        return build_lambda_object (r);
        return build_lambda_object (r);
      }
      }
 
 
    default:
    default:
      /* Handle Objective-C++ constructs, if appropriate.  */
      /* Handle Objective-C++ constructs, if appropriate.  */
      {
      {
        tree subst
        tree subst
          = objcp_tsubst_copy_and_build (t, args, complain,
          = objcp_tsubst_copy_and_build (t, args, complain,
                                         in_decl, /*function_p=*/false);
                                         in_decl, /*function_p=*/false);
        if (subst)
        if (subst)
          return subst;
          return subst;
      }
      }
      return tsubst_copy (t, args, complain, in_decl);
      return tsubst_copy (t, args, complain, in_decl);
    }
    }
 
 
#undef RECUR
#undef RECUR
}
}
 
 
/* Verify that the instantiated ARGS are valid. For type arguments,
/* Verify that the instantiated ARGS are valid. For type arguments,
   make sure that the type's linkage is ok. For non-type arguments,
   make sure that the type's linkage is ok. For non-type arguments,
   make sure they are constants if they are integral or enumerations.
   make sure they are constants if they are integral or enumerations.
   Emit an error under control of COMPLAIN, and return TRUE on error.  */
   Emit an error under control of COMPLAIN, and return TRUE on error.  */
 
 
static bool
static bool
check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
{
{
  if (ARGUMENT_PACK_P (t))
  if (ARGUMENT_PACK_P (t))
    {
    {
      tree vec = ARGUMENT_PACK_ARGS (t);
      tree vec = ARGUMENT_PACK_ARGS (t);
      int len = TREE_VEC_LENGTH (vec);
      int len = TREE_VEC_LENGTH (vec);
      bool result = false;
      bool result = false;
      int i;
      int i;
 
 
      for (i = 0; i < len; ++i)
      for (i = 0; i < len; ++i)
        if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
        if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
          result = true;
          result = true;
      return result;
      return result;
    }
    }
  else if (TYPE_P (t))
  else if (TYPE_P (t))
    {
    {
      /* [basic.link]: A name with no linkage (notably, the name
      /* [basic.link]: A name with no linkage (notably, the name
         of a class or enumeration declared in a local scope)
         of a class or enumeration declared in a local scope)
         shall not be used to declare an entity with linkage.
         shall not be used to declare an entity with linkage.
         This implies that names with no linkage cannot be used as
         This implies that names with no linkage cannot be used as
         template arguments
         template arguments
 
 
         DR 757 relaxes this restriction for C++0x.  */
         DR 757 relaxes this restriction for C++0x.  */
      tree nt = (cxx_dialect > cxx98 ? NULL_TREE
      tree nt = (cxx_dialect > cxx98 ? NULL_TREE
                 : no_linkage_check (t, /*relaxed_p=*/false));
                 : no_linkage_check (t, /*relaxed_p=*/false));
 
 
      if (nt)
      if (nt)
        {
        {
          /* DR 488 makes use of a type with no linkage cause
          /* DR 488 makes use of a type with no linkage cause
             type deduction to fail.  */
             type deduction to fail.  */
          if (complain & tf_error)
          if (complain & tf_error)
            {
            {
              if (TYPE_ANONYMOUS_P (nt))
              if (TYPE_ANONYMOUS_P (nt))
                error ("%qT is/uses anonymous type", t);
                error ("%qT is/uses anonymous type", t);
              else
              else
                error ("template argument for %qD uses local type %qT",
                error ("template argument for %qD uses local type %qT",
                       tmpl, t);
                       tmpl, t);
            }
            }
          return true;
          return true;
        }
        }
      /* In order to avoid all sorts of complications, we do not
      /* In order to avoid all sorts of complications, we do not
         allow variably-modified types as template arguments.  */
         allow variably-modified types as template arguments.  */
      else if (variably_modified_type_p (t, NULL_TREE))
      else if (variably_modified_type_p (t, NULL_TREE))
        {
        {
          if (complain & tf_error)
          if (complain & tf_error)
            error ("%qT is a variably modified type", t);
            error ("%qT is a variably modified type", t);
          return true;
          return true;
        }
        }
    }
    }
  /* A non-type argument of integral or enumerated type must be a
  /* A non-type argument of integral or enumerated type must be a
     constant.  */
     constant.  */
  else if (TREE_TYPE (t)
  else if (TREE_TYPE (t)
           && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
           && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
           && !TREE_CONSTANT (t))
           && !TREE_CONSTANT (t))
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        error ("integral expression %qE is not constant", t);
        error ("integral expression %qE is not constant", t);
      return true;
      return true;
    }
    }
  return false;
  return false;
}
}
 
 
static bool
static bool
check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
{
{
  int ix, len = DECL_NTPARMS (tmpl);
  int ix, len = DECL_NTPARMS (tmpl);
  bool result = false;
  bool result = false;
 
 
  for (ix = 0; ix != len; ix++)
  for (ix = 0; ix != len; ix++)
    {
    {
      if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
      if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
        result = true;
        result = true;
    }
    }
  if (result && (complain & tf_error))
  if (result && (complain & tf_error))
    error ("  trying to instantiate %qD", tmpl);
    error ("  trying to instantiate %qD", tmpl);
  return result;
  return result;
}
}
 
 
/* Instantiate the indicated variable or function template TMPL with
/* Instantiate the indicated variable or function template TMPL with
   the template arguments in TARG_PTR.  */
   the template arguments in TARG_PTR.  */
 
 
tree
tree
instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
{
{
  tree targ_ptr = orig_args;
  tree targ_ptr = orig_args;
  tree fndecl;
  tree fndecl;
  tree gen_tmpl;
  tree gen_tmpl;
  tree spec;
  tree spec;
  HOST_WIDE_INT saved_processing_template_decl;
  HOST_WIDE_INT saved_processing_template_decl;
 
 
  if (tmpl == error_mark_node)
  if (tmpl == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
 
 
  /* If this function is a clone, handle it specially.  */
  /* If this function is a clone, handle it specially.  */
  if (DECL_CLONED_FUNCTION_P (tmpl))
  if (DECL_CLONED_FUNCTION_P (tmpl))
    {
    {
      tree spec;
      tree spec;
      tree clone;
      tree clone;
 
 
      /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
      /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
         DECL_CLONED_FUNCTION.  */
         DECL_CLONED_FUNCTION.  */
      spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
      spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
                                   targ_ptr, complain);
                                   targ_ptr, complain);
      if (spec == error_mark_node)
      if (spec == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      /* Look for the clone.  */
      /* Look for the clone.  */
      FOR_EACH_CLONE (clone, spec)
      FOR_EACH_CLONE (clone, spec)
        if (DECL_NAME (clone) == DECL_NAME (tmpl))
        if (DECL_NAME (clone) == DECL_NAME (tmpl))
          return clone;
          return clone;
      /* We should always have found the clone by now.  */
      /* We should always have found the clone by now.  */
      gcc_unreachable ();
      gcc_unreachable ();
      return NULL_TREE;
      return NULL_TREE;
    }
    }
 
 
  /* Check to see if we already have this specialization.  */
  /* Check to see if we already have this specialization.  */
  gen_tmpl = most_general_template (tmpl);
  gen_tmpl = most_general_template (tmpl);
  if (tmpl != gen_tmpl)
  if (tmpl != gen_tmpl)
    /* The TMPL is a partial instantiation.  To get a full set of
    /* The TMPL is a partial instantiation.  To get a full set of
       arguments we must add the arguments used to perform the
       arguments we must add the arguments used to perform the
       partial instantiation.  */
       partial instantiation.  */
    targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
    targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
                                            targ_ptr);
                                            targ_ptr);
 
 
  /* It would be nice to avoid hashing here and then again in tsubst_decl,
  /* It would be nice to avoid hashing here and then again in tsubst_decl,
     but it doesn't seem to be on the hot path.  */
     but it doesn't seem to be on the hot path.  */
  spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
  spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
 
 
  gcc_assert (tmpl == gen_tmpl
  gcc_assert (tmpl == gen_tmpl
              || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
              || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
                  == spec)
                  == spec)
              || fndecl == NULL_TREE);
              || fndecl == NULL_TREE);
 
 
  if (spec != NULL_TREE)
  if (spec != NULL_TREE)
    return spec;
    return spec;
 
 
  if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
  if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
                               complain))
                               complain))
    return error_mark_node;
    return error_mark_node;
 
 
  /* We are building a FUNCTION_DECL, during which the access of its
  /* We are building a FUNCTION_DECL, during which the access of its
     parameters and return types have to be checked.  However this
     parameters and return types have to be checked.  However this
     FUNCTION_DECL which is the desired context for access checking
     FUNCTION_DECL which is the desired context for access checking
     is not built yet.  We solve this chicken-and-egg problem by
     is not built yet.  We solve this chicken-and-egg problem by
     deferring all checks until we have the FUNCTION_DECL.  */
     deferring all checks until we have the FUNCTION_DECL.  */
  push_deferring_access_checks (dk_deferred);
  push_deferring_access_checks (dk_deferred);
 
 
  /* Although PROCESSING_TEMPLATE_DECL may be true at this point
  /* Although PROCESSING_TEMPLATE_DECL may be true at this point
     (because, for example, we have encountered a non-dependent
     (because, for example, we have encountered a non-dependent
     function call in the body of a template function and must now
     function call in the body of a template function and must now
     determine which of several overloaded functions will be called),
     determine which of several overloaded functions will be called),
     within the instantiation itself we are not processing a
     within the instantiation itself we are not processing a
     template.  */
     template.  */
  saved_processing_template_decl = processing_template_decl;
  saved_processing_template_decl = processing_template_decl;
  processing_template_decl = 0;
  processing_template_decl = 0;
  /* Substitute template parameters to obtain the specialization.  */
  /* Substitute template parameters to obtain the specialization.  */
  fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
  fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
                   targ_ptr, complain, gen_tmpl);
                   targ_ptr, complain, gen_tmpl);
  processing_template_decl = saved_processing_template_decl;
  processing_template_decl = saved_processing_template_decl;
  if (fndecl == error_mark_node)
  if (fndecl == error_mark_node)
    return error_mark_node;
    return error_mark_node;
 
 
  /* Now we know the specialization, compute access previously
  /* Now we know the specialization, compute access previously
     deferred.  */
     deferred.  */
  push_access_scope (fndecl);
  push_access_scope (fndecl);
 
 
  /* Some typedefs referenced from within the template code need to be access
  /* Some typedefs referenced from within the template code need to be access
     checked at template instantiation time, i.e now. These types were
     checked at template instantiation time, i.e now. These types were
     added to the template at parsing time. Let's get those and perfom
     added to the template at parsing time. Let's get those and perfom
     the acces checks then.  */
     the acces checks then.  */
  perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
  perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
  perform_deferred_access_checks ();
  perform_deferred_access_checks ();
  pop_access_scope (fndecl);
  pop_access_scope (fndecl);
  pop_deferring_access_checks ();
  pop_deferring_access_checks ();
 
 
  /* The DECL_TI_TEMPLATE should always be the immediate parent
  /* The DECL_TI_TEMPLATE should always be the immediate parent
     template, not the most general template.  */
     template, not the most general template.  */
  DECL_TI_TEMPLATE (fndecl) = tmpl;
  DECL_TI_TEMPLATE (fndecl) = tmpl;
 
 
  /* If we've just instantiated the main entry point for a function,
  /* If we've just instantiated the main entry point for a function,
     instantiate all the alternate entry points as well.  We do this
     instantiate all the alternate entry points as well.  We do this
     by cloning the instantiation of the main entry point, not by
     by cloning the instantiation of the main entry point, not by
     instantiating the template clones.  */
     instantiating the template clones.  */
  if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
  if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
    clone_function_decl (fndecl, /*update_method_vec_p=*/0);
    clone_function_decl (fndecl, /*update_method_vec_p=*/0);
 
 
  return fndecl;
  return fndecl;
}
}
 
 
/* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
/* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   NARGS elements of the arguments that are being used when calling
   NARGS elements of the arguments that are being used when calling
   it.  TARGS is a vector into which the deduced template arguments
   it.  TARGS is a vector into which the deduced template arguments
   are placed.
   are placed.
 
 
   Return zero for success, 2 for an incomplete match that doesn't resolve
   Return zero for success, 2 for an incomplete match that doesn't resolve
   all the types, and 1 for complete failure.  An error message will be
   all the types, and 1 for complete failure.  An error message will be
   printed only for an incomplete match.
   printed only for an incomplete match.
 
 
   If FN is a conversion operator, or we are trying to produce a specific
   If FN is a conversion operator, or we are trying to produce a specific
   specialization, RETURN_TYPE is the return type desired.
   specialization, RETURN_TYPE is the return type desired.
 
 
   The EXPLICIT_TARGS are explicit template arguments provided via a
   The EXPLICIT_TARGS are explicit template arguments provided via a
   template-id.
   template-id.
 
 
   The parameter STRICT is one of:
   The parameter STRICT is one of:
 
 
   DEDUCE_CALL:
   DEDUCE_CALL:
     We are deducing arguments for a function call, as in
     We are deducing arguments for a function call, as in
     [temp.deduct.call].
     [temp.deduct.call].
 
 
   DEDUCE_CONV:
   DEDUCE_CONV:
     We are deducing arguments for a conversion function, as in
     We are deducing arguments for a conversion function, as in
     [temp.deduct.conv].
     [temp.deduct.conv].
 
 
   DEDUCE_EXACT:
   DEDUCE_EXACT:
     We are deducing arguments when doing an explicit instantiation
     We are deducing arguments when doing an explicit instantiation
     as in [temp.explicit], when determining an explicit specialization
     as in [temp.explicit], when determining an explicit specialization
     as in [temp.expl.spec], or when taking the address of a function
     as in [temp.expl.spec], or when taking the address of a function
     template, as in [temp.deduct.funcaddr].  */
     template, as in [temp.deduct.funcaddr].  */
 
 
int
int
fn_type_unification (tree fn,
fn_type_unification (tree fn,
                     tree explicit_targs,
                     tree explicit_targs,
                     tree targs,
                     tree targs,
                     const tree *args,
                     const tree *args,
                     unsigned int nargs,
                     unsigned int nargs,
                     tree return_type,
                     tree return_type,
                     unification_kind_t strict,
                     unification_kind_t strict,
                     int flags)
                     int flags)
{
{
  tree parms;
  tree parms;
  tree fntype;
  tree fntype;
  int result;
  int result;
  bool incomplete_argument_packs_p = false;
  bool incomplete_argument_packs_p = false;
 
 
  gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
  gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
 
 
  fntype = TREE_TYPE (fn);
  fntype = TREE_TYPE (fn);
  if (explicit_targs)
  if (explicit_targs)
    {
    {
      /* [temp.deduct]
      /* [temp.deduct]
 
 
         The specified template arguments must match the template
         The specified template arguments must match the template
         parameters in kind (i.e., type, nontype, template), and there
         parameters in kind (i.e., type, nontype, template), and there
         must not be more arguments than there are parameters;
         must not be more arguments than there are parameters;
         otherwise type deduction fails.
         otherwise type deduction fails.
 
 
         Nontype arguments must match the types of the corresponding
         Nontype arguments must match the types of the corresponding
         nontype template parameters, or must be convertible to the
         nontype template parameters, or must be convertible to the
         types of the corresponding nontype parameters as specified in
         types of the corresponding nontype parameters as specified in
         _temp.arg.nontype_, otherwise type deduction fails.
         _temp.arg.nontype_, otherwise type deduction fails.
 
 
         All references in the function type of the function template
         All references in the function type of the function template
         to the corresponding template parameters are replaced by the
         to the corresponding template parameters are replaced by the
         specified template argument values.  If a substitution in a
         specified template argument values.  If a substitution in a
         template parameter or in the function type of the function
         template parameter or in the function type of the function
         template results in an invalid type, type deduction fails.  */
         template results in an invalid type, type deduction fails.  */
      tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
      tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
      int i, len = TREE_VEC_LENGTH (tparms);
      int i, len = TREE_VEC_LENGTH (tparms);
      tree converted_args;
      tree converted_args;
      bool incomplete = false;
      bool incomplete = false;
 
 
      if (explicit_targs == error_mark_node)
      if (explicit_targs == error_mark_node)
        return 1;
        return 1;
 
 
      converted_args
      converted_args
        = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
        = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
                                  /*require_all_args=*/false,
                                  /*require_all_args=*/false,
                                  /*use_default_args=*/false));
                                  /*use_default_args=*/false));
      if (converted_args == error_mark_node)
      if (converted_args == error_mark_node)
        return 1;
        return 1;
 
 
      /* Substitute the explicit args into the function type.  This is
      /* Substitute the explicit args into the function type.  This is
         necessary so that, for instance, explicitly declared function
         necessary so that, for instance, explicitly declared function
         arguments can match null pointed constants.  If we were given
         arguments can match null pointed constants.  If we were given
         an incomplete set of explicit args, we must not do semantic
         an incomplete set of explicit args, we must not do semantic
         processing during substitution as we could create partial
         processing during substitution as we could create partial
         instantiations.  */
         instantiations.  */
      for (i = 0; i < len; i++)
      for (i = 0; i < len; i++)
        {
        {
          tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
          tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
          bool parameter_pack = false;
          bool parameter_pack = false;
 
 
          /* Dig out the actual parm.  */
          /* Dig out the actual parm.  */
          if (TREE_CODE (parm) == TYPE_DECL
          if (TREE_CODE (parm) == TYPE_DECL
              || TREE_CODE (parm) == TEMPLATE_DECL)
              || TREE_CODE (parm) == TEMPLATE_DECL)
            {
            {
              parm = TREE_TYPE (parm);
              parm = TREE_TYPE (parm);
              parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
              parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
            }
            }
          else if (TREE_CODE (parm) == PARM_DECL)
          else if (TREE_CODE (parm) == PARM_DECL)
            {
            {
              parm = DECL_INITIAL (parm);
              parm = DECL_INITIAL (parm);
              parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
              parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
            }
            }
 
 
          if (parameter_pack)
          if (parameter_pack)
            {
            {
              int level, idx;
              int level, idx;
              tree targ;
              tree targ;
              template_parm_level_and_index (parm, &level, &idx);
              template_parm_level_and_index (parm, &level, &idx);
 
 
              /* Mark the argument pack as "incomplete". We could
              /* Mark the argument pack as "incomplete". We could
                 still deduce more arguments during unification.  */
                 still deduce more arguments during unification.  */
              targ = TMPL_ARG (converted_args, level, idx);
              targ = TMPL_ARG (converted_args, level, idx);
              if (targ)
              if (targ)
                {
                {
                  ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
                  ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
                  ARGUMENT_PACK_EXPLICIT_ARGS (targ)
                  ARGUMENT_PACK_EXPLICIT_ARGS (targ)
                    = ARGUMENT_PACK_ARGS (targ);
                    = ARGUMENT_PACK_ARGS (targ);
                }
                }
 
 
              /* We have some incomplete argument packs.  */
              /* We have some incomplete argument packs.  */
              incomplete_argument_packs_p = true;
              incomplete_argument_packs_p = true;
            }
            }
        }
        }
 
 
      if (incomplete_argument_packs_p)
      if (incomplete_argument_packs_p)
        /* Any substitution is guaranteed to be incomplete if there
        /* Any substitution is guaranteed to be incomplete if there
           are incomplete argument packs, because we can still deduce
           are incomplete argument packs, because we can still deduce
           more arguments.  */
           more arguments.  */
        incomplete = 1;
        incomplete = 1;
      else
      else
        incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
        incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
 
 
      processing_template_decl += incomplete;
      processing_template_decl += incomplete;
      fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
      fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
      processing_template_decl -= incomplete;
      processing_template_decl -= incomplete;
 
 
      if (fntype == error_mark_node)
      if (fntype == error_mark_node)
        return 1;
        return 1;
 
 
      /* Place the explicitly specified arguments in TARGS.  */
      /* Place the explicitly specified arguments in TARGS.  */
      for (i = NUM_TMPL_ARGS (converted_args); i--;)
      for (i = NUM_TMPL_ARGS (converted_args); i--;)
        TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
        TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
    }
    }
 
 
  /* Never do unification on the 'this' parameter.  */
  /* Never do unification on the 'this' parameter.  */
  parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
  parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
 
 
  if (return_type)
  if (return_type)
    {
    {
      tree *new_args;
      tree *new_args;
 
 
      parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
      parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
      new_args = XALLOCAVEC (tree, nargs + 1);
      new_args = XALLOCAVEC (tree, nargs + 1);
      new_args[0] = return_type;
      new_args[0] = return_type;
      memcpy (new_args + 1, args, nargs * sizeof (tree));
      memcpy (new_args + 1, args, nargs * sizeof (tree));
      args = new_args;
      args = new_args;
      ++nargs;
      ++nargs;
    }
    }
 
 
  /* We allow incomplete unification without an error message here
  /* We allow incomplete unification without an error message here
     because the standard doesn't seem to explicitly prohibit it.  Our
     because the standard doesn't seem to explicitly prohibit it.  Our
     callers must be ready to deal with unification failures in any
     callers must be ready to deal with unification failures in any
     event.  */
     event.  */
  result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
  result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
                                  targs, parms, args, nargs, /*subr=*/0,
                                  targs, parms, args, nargs, /*subr=*/0,
                                  strict, flags);
                                  strict, flags);
 
 
  if (result == 0 && incomplete_argument_packs_p)
  if (result == 0 && incomplete_argument_packs_p)
    {
    {
      int i, len = NUM_TMPL_ARGS (targs);
      int i, len = NUM_TMPL_ARGS (targs);
 
 
      /* Clear the "incomplete" flags on all argument packs.  */
      /* Clear the "incomplete" flags on all argument packs.  */
      for (i = 0; i < len; i++)
      for (i = 0; i < len; i++)
        {
        {
          tree arg = TREE_VEC_ELT (targs, i);
          tree arg = TREE_VEC_ELT (targs, i);
          if (ARGUMENT_PACK_P (arg))
          if (ARGUMENT_PACK_P (arg))
            {
            {
              ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
              ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
              ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
              ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
            }
            }
        }
        }
    }
    }
 
 
  /* Now that we have bindings for all of the template arguments,
  /* Now that we have bindings for all of the template arguments,
     ensure that the arguments deduced for the template template
     ensure that the arguments deduced for the template template
     parameters have compatible template parameter lists.  We cannot
     parameters have compatible template parameter lists.  We cannot
     check this property before we have deduced all template
     check this property before we have deduced all template
     arguments, because the template parameter types of a template
     arguments, because the template parameter types of a template
     template parameter might depend on prior template parameters
     template parameter might depend on prior template parameters
     deduced after the template template parameter.  The following
     deduced after the template template parameter.  The following
     ill-formed example illustrates this issue:
     ill-formed example illustrates this issue:
 
 
       template<typename T, template<T> class C> void f(C<5>, T);
       template<typename T, template<T> class C> void f(C<5>, T);
 
 
       template<int N> struct X {};
       template<int N> struct X {};
 
 
       void g() {
       void g() {
         f(X<5>(), 5l); // error: template argument deduction fails
         f(X<5>(), 5l); // error: template argument deduction fails
       }
       }
 
 
     The template parameter list of 'C' depends on the template type
     The template parameter list of 'C' depends on the template type
     parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
     parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
     'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
     'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
     time that we deduce 'C'.  */
     time that we deduce 'C'.  */
  if (result == 0
  if (result == 0
      && !template_template_parm_bindings_ok_p
      && !template_template_parm_bindings_ok_p
           (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
           (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
    return 1;
    return 1;
 
 
  if (result == 0)
  if (result == 0)
    /* All is well so far.  Now, check:
    /* All is well so far.  Now, check:
 
 
       [temp.deduct]
       [temp.deduct]
 
 
       When all template arguments have been deduced, all uses of
       When all template arguments have been deduced, all uses of
       template parameters in nondeduced contexts are replaced with
       template parameters in nondeduced contexts are replaced with
       the corresponding deduced argument values.  If the
       the corresponding deduced argument values.  If the
       substitution results in an invalid type, as described above,
       substitution results in an invalid type, as described above,
       type deduction fails.  */
       type deduction fails.  */
    {
    {
      tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
      tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
      if (substed == error_mark_node)
      if (substed == error_mark_node)
        return 1;
        return 1;
 
 
      /* If we're looking for an exact match, check that what we got
      /* If we're looking for an exact match, check that what we got
         is indeed an exact match.  It might not be if some template
         is indeed an exact match.  It might not be if some template
         parameters are used in non-deduced contexts.  */
         parameters are used in non-deduced contexts.  */
      if (strict == DEDUCE_EXACT)
      if (strict == DEDUCE_EXACT)
        {
        {
          unsigned int i;
          unsigned int i;
 
 
          tree sarg
          tree sarg
            = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
            = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
          if (return_type)
          if (return_type)
            sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
            sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
          for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
          for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
            if (!same_type_p (args[i], TREE_VALUE (sarg)))
            if (!same_type_p (args[i], TREE_VALUE (sarg)))
              return 1;
              return 1;
        }
        }
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Adjust types before performing type deduction, as described in
/* Adjust types before performing type deduction, as described in
   [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   sections are symmetric.  PARM is the type of a function parameter
   sections are symmetric.  PARM is the type of a function parameter
   or the return type of the conversion function.  ARG is the type of
   or the return type of the conversion function.  ARG is the type of
   the argument passed to the call, or the type of the value
   the argument passed to the call, or the type of the value
   initialized with the result of the conversion function.
   initialized with the result of the conversion function.
   ARG_EXPR is the original argument expression, which may be null.  */
   ARG_EXPR is the original argument expression, which may be null.  */
 
 
static int
static int
maybe_adjust_types_for_deduction (unification_kind_t strict,
maybe_adjust_types_for_deduction (unification_kind_t strict,
                                  tree* parm,
                                  tree* parm,
                                  tree* arg,
                                  tree* arg,
                                  tree arg_expr)
                                  tree arg_expr)
{
{
  int result = 0;
  int result = 0;
 
 
  switch (strict)
  switch (strict)
    {
    {
    case DEDUCE_CALL:
    case DEDUCE_CALL:
      break;
      break;
 
 
    case DEDUCE_CONV:
    case DEDUCE_CONV:
      {
      {
        /* Swap PARM and ARG throughout the remainder of this
        /* Swap PARM and ARG throughout the remainder of this
           function; the handling is precisely symmetric since PARM
           function; the handling is precisely symmetric since PARM
           will initialize ARG rather than vice versa.  */
           will initialize ARG rather than vice versa.  */
        tree* temp = parm;
        tree* temp = parm;
        parm = arg;
        parm = arg;
        arg = temp;
        arg = temp;
        break;
        break;
      }
      }
 
 
    case DEDUCE_EXACT:
    case DEDUCE_EXACT:
      /* Core issue #873: Do the DR606 thing (see below) for these cases,
      /* Core issue #873: Do the DR606 thing (see below) for these cases,
         too, but here handle it by stripping the reference from PARM
         too, but here handle it by stripping the reference from PARM
         rather than by adding it to ARG.  */
         rather than by adding it to ARG.  */
      if (TREE_CODE (*parm) == REFERENCE_TYPE
      if (TREE_CODE (*parm) == REFERENCE_TYPE
          && TYPE_REF_IS_RVALUE (*parm)
          && TYPE_REF_IS_RVALUE (*parm)
          && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
          && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
          && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
          && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
          && TREE_CODE (*arg) == REFERENCE_TYPE
          && TREE_CODE (*arg) == REFERENCE_TYPE
          && !TYPE_REF_IS_RVALUE (*arg))
          && !TYPE_REF_IS_RVALUE (*arg))
        *parm = TREE_TYPE (*parm);
        *parm = TREE_TYPE (*parm);
      /* Nothing else to do in this case.  */
      /* Nothing else to do in this case.  */
      return 0;
      return 0;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
 
 
  if (TREE_CODE (*parm) != REFERENCE_TYPE)
  if (TREE_CODE (*parm) != REFERENCE_TYPE)
    {
    {
      /* [temp.deduct.call]
      /* [temp.deduct.call]
 
 
         If P is not a reference type:
         If P is not a reference type:
 
 
         --If A is an array type, the pointer type produced by the
         --If A is an array type, the pointer type produced by the
         array-to-pointer standard conversion (_conv.array_) is
         array-to-pointer standard conversion (_conv.array_) is
         used in place of A for type deduction; otherwise,
         used in place of A for type deduction; otherwise,
 
 
         --If A is a function type, the pointer type produced by
         --If A is a function type, the pointer type produced by
         the function-to-pointer standard conversion
         the function-to-pointer standard conversion
         (_conv.func_) is used in place of A for type deduction;
         (_conv.func_) is used in place of A for type deduction;
         otherwise,
         otherwise,
 
 
         --If A is a cv-qualified type, the top level
         --If A is a cv-qualified type, the top level
         cv-qualifiers of A's type are ignored for type
         cv-qualifiers of A's type are ignored for type
         deduction.  */
         deduction.  */
      if (TREE_CODE (*arg) == ARRAY_TYPE)
      if (TREE_CODE (*arg) == ARRAY_TYPE)
        *arg = build_pointer_type (TREE_TYPE (*arg));
        *arg = build_pointer_type (TREE_TYPE (*arg));
      else if (TREE_CODE (*arg) == FUNCTION_TYPE)
      else if (TREE_CODE (*arg) == FUNCTION_TYPE)
        *arg = build_pointer_type (*arg);
        *arg = build_pointer_type (*arg);
      else
      else
        *arg = TYPE_MAIN_VARIANT (*arg);
        *arg = TYPE_MAIN_VARIANT (*arg);
    }
    }
 
 
  /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
  /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
     of the form T&&, where T is a template parameter, and the argument
     of the form T&&, where T is a template parameter, and the argument
     is an lvalue, T is deduced as A& */
     is an lvalue, T is deduced as A& */
  if (TREE_CODE (*parm) == REFERENCE_TYPE
  if (TREE_CODE (*parm) == REFERENCE_TYPE
      && TYPE_REF_IS_RVALUE (*parm)
      && TYPE_REF_IS_RVALUE (*parm)
      && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
      && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
      && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
      && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
      && arg_expr && real_lvalue_p (arg_expr))
      && arg_expr && real_lvalue_p (arg_expr))
    *arg = build_reference_type (*arg);
    *arg = build_reference_type (*arg);
 
 
  /* [temp.deduct.call]
  /* [temp.deduct.call]
 
 
     If P is a cv-qualified type, the top level cv-qualifiers
     If P is a cv-qualified type, the top level cv-qualifiers
     of P's type are ignored for type deduction.  If P is a
     of P's type are ignored for type deduction.  If P is a
     reference type, the type referred to by P is used for
     reference type, the type referred to by P is used for
     type deduction.  */
     type deduction.  */
  *parm = TYPE_MAIN_VARIANT (*parm);
  *parm = TYPE_MAIN_VARIANT (*parm);
  if (TREE_CODE (*parm) == REFERENCE_TYPE)
  if (TREE_CODE (*parm) == REFERENCE_TYPE)
    {
    {
      *parm = TREE_TYPE (*parm);
      *parm = TREE_TYPE (*parm);
      result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
      result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
    }
    }
 
 
  /* DR 322. For conversion deduction, remove a reference type on parm
  /* DR 322. For conversion deduction, remove a reference type on parm
     too (which has been swapped into ARG).  */
     too (which has been swapped into ARG).  */
  if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
  if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
    *arg = TREE_TYPE (*arg);
    *arg = TREE_TYPE (*arg);
 
 
  return result;
  return result;
}
}
 
 
/* Most parms like fn_type_unification.
/* Most parms like fn_type_unification.
 
 
   If SUBR is 1, we're being called recursively (to unify the
   If SUBR is 1, we're being called recursively (to unify the
   arguments of a function or method parameter of a function
   arguments of a function or method parameter of a function
   template). */
   template). */
 
 
static int
static int
type_unification_real (tree tparms,
type_unification_real (tree tparms,
                       tree targs,
                       tree targs,
                       tree xparms,
                       tree xparms,
                       const tree *xargs,
                       const tree *xargs,
                       unsigned int xnargs,
                       unsigned int xnargs,
                       int subr,
                       int subr,
                       unification_kind_t strict,
                       unification_kind_t strict,
                       int flags)
                       int flags)
{
{
  tree parm, arg, arg_expr;
  tree parm, arg, arg_expr;
  int i;
  int i;
  int ntparms = TREE_VEC_LENGTH (tparms);
  int ntparms = TREE_VEC_LENGTH (tparms);
  int sub_strict;
  int sub_strict;
  int saw_undeduced = 0;
  int saw_undeduced = 0;
  tree parms;
  tree parms;
  const tree *args;
  const tree *args;
  unsigned int nargs;
  unsigned int nargs;
  unsigned int ia;
  unsigned int ia;
 
 
  gcc_assert (TREE_CODE (tparms) == TREE_VEC);
  gcc_assert (TREE_CODE (tparms) == TREE_VEC);
  gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
  gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
  gcc_assert (ntparms > 0);
  gcc_assert (ntparms > 0);
 
 
  /* Reset the number of non-defaulted template arguments contained
  /* Reset the number of non-defaulted template arguments contained
     in in TARGS.  */
     in in TARGS.  */
  NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
  NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
 
 
  switch (strict)
  switch (strict)
    {
    {
    case DEDUCE_CALL:
    case DEDUCE_CALL:
      sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
      sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
                    | UNIFY_ALLOW_DERIVED);
                    | UNIFY_ALLOW_DERIVED);
      break;
      break;
 
 
    case DEDUCE_CONV:
    case DEDUCE_CONV:
      sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
      sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
      break;
      break;
 
 
    case DEDUCE_EXACT:
    case DEDUCE_EXACT:
      sub_strict = UNIFY_ALLOW_NONE;
      sub_strict = UNIFY_ALLOW_NONE;
      break;
      break;
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
 
 
 again:
 again:
  parms = xparms;
  parms = xparms;
  args = xargs;
  args = xargs;
  nargs = xnargs;
  nargs = xnargs;
 
 
  ia = 0;
  ia = 0;
  while (parms && parms != void_list_node
  while (parms && parms != void_list_node
         && ia < nargs)
         && ia < nargs)
    {
    {
      if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
      if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
        break;
        break;
 
 
      parm = TREE_VALUE (parms);
      parm = TREE_VALUE (parms);
      parms = TREE_CHAIN (parms);
      parms = TREE_CHAIN (parms);
      arg = args[ia];
      arg = args[ia];
      ++ia;
      ++ia;
      arg_expr = NULL;
      arg_expr = NULL;
 
 
      if (arg == error_mark_node)
      if (arg == error_mark_node)
        return 1;
        return 1;
      if (arg == unknown_type_node)
      if (arg == unknown_type_node)
        /* We can't deduce anything from this, but we might get all the
        /* We can't deduce anything from this, but we might get all the
           template args from other function args.  */
           template args from other function args.  */
        continue;
        continue;
 
 
      /* Conversions will be performed on a function argument that
      /* Conversions will be performed on a function argument that
         corresponds with a function parameter that contains only
         corresponds with a function parameter that contains only
         non-deducible template parameters and explicitly specified
         non-deducible template parameters and explicitly specified
         template parameters.  */
         template parameters.  */
      if (!uses_template_parms (parm))
      if (!uses_template_parms (parm))
        {
        {
          tree type;
          tree type;
 
 
          if (!TYPE_P (arg))
          if (!TYPE_P (arg))
            type = TREE_TYPE (arg);
            type = TREE_TYPE (arg);
          else
          else
            type = arg;
            type = arg;
 
 
          if (same_type_p (parm, type))
          if (same_type_p (parm, type))
            continue;
            continue;
          if (strict != DEDUCE_EXACT
          if (strict != DEDUCE_EXACT
              && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
              && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
                                  flags))
                                  flags))
            continue;
            continue;
 
 
          return 1;
          return 1;
        }
        }
 
 
      if (!TYPE_P (arg))
      if (!TYPE_P (arg))
        {
        {
          gcc_assert (TREE_TYPE (arg) != NULL_TREE);
          gcc_assert (TREE_TYPE (arg) != NULL_TREE);
          if (type_unknown_p (arg))
          if (type_unknown_p (arg))
            {
            {
              /* [temp.deduct.type]
              /* [temp.deduct.type]
 
 
                 A template-argument can be deduced from a pointer to
                 A template-argument can be deduced from a pointer to
                 function or pointer to member function argument if
                 function or pointer to member function argument if
                 the set of overloaded functions does not contain
                 the set of overloaded functions does not contain
                 function templates and at most one of a set of
                 function templates and at most one of a set of
                 overloaded functions provides a unique match.  */
                 overloaded functions provides a unique match.  */
              if (resolve_overloaded_unification
              if (resolve_overloaded_unification
                  (tparms, targs, parm, arg, strict, sub_strict))
                  (tparms, targs, parm, arg, strict, sub_strict))
                continue;
                continue;
 
 
              return 1;
              return 1;
            }
            }
          arg_expr = arg;
          arg_expr = arg;
          arg = unlowered_expr_type (arg);
          arg = unlowered_expr_type (arg);
          if (arg == error_mark_node)
          if (arg == error_mark_node)
            return 1;
            return 1;
        }
        }
 
 
      {
      {
        int arg_strict = sub_strict;
        int arg_strict = sub_strict;
 
 
        if (!subr)
        if (!subr)
          arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
          arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
                                                          arg_expr);
                                                          arg_expr);
 
 
        if (arg == init_list_type_node && arg_expr)
        if (arg == init_list_type_node && arg_expr)
          arg = arg_expr;
          arg = arg_expr;
        if (unify (tparms, targs, parm, arg, arg_strict))
        if (unify (tparms, targs, parm, arg, arg_strict))
          return 1;
          return 1;
      }
      }
    }
    }
 
 
 
 
  if (parms
  if (parms
      && parms != void_list_node
      && parms != void_list_node
      && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
      && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
    {
    {
      /* Unify the remaining arguments with the pack expansion type.  */
      /* Unify the remaining arguments with the pack expansion type.  */
      tree argvec;
      tree argvec;
      tree parmvec = make_tree_vec (1);
      tree parmvec = make_tree_vec (1);
 
 
      /* Allocate a TREE_VEC and copy in all of the arguments */
      /* Allocate a TREE_VEC and copy in all of the arguments */
      argvec = make_tree_vec (nargs - ia);
      argvec = make_tree_vec (nargs - ia);
      for (i = 0; ia < nargs; ++ia, ++i)
      for (i = 0; ia < nargs; ++ia, ++i)
        TREE_VEC_ELT (argvec, i) = args[ia];
        TREE_VEC_ELT (argvec, i) = args[ia];
 
 
      /* Copy the parameter into parmvec.  */
      /* Copy the parameter into parmvec.  */
      TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
      TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
      if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
      if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
                                /*call_args_p=*/true, /*subr=*/subr))
                                /*call_args_p=*/true, /*subr=*/subr))
        return 1;
        return 1;
 
 
      /* Advance to the end of the list of parameters.  */
      /* Advance to the end of the list of parameters.  */
      parms = TREE_CHAIN (parms);
      parms = TREE_CHAIN (parms);
    }
    }
 
 
  /* Fail if we've reached the end of the parm list, and more args
  /* Fail if we've reached the end of the parm list, and more args
     are present, and the parm list isn't variadic.  */
     are present, and the parm list isn't variadic.  */
  if (ia < nargs && parms == void_list_node)
  if (ia < nargs && parms == void_list_node)
    return 1;
    return 1;
  /* Fail if parms are left and they don't have default values.  */
  /* Fail if parms are left and they don't have default values.  */
  if (parms && parms != void_list_node
  if (parms && parms != void_list_node
      && TREE_PURPOSE (parms) == NULL_TREE)
      && TREE_PURPOSE (parms) == NULL_TREE)
    return 1;
    return 1;
 
 
  if (!subr)
  if (!subr)
    for (i = 0; i < ntparms; i++)
    for (i = 0; i < ntparms; i++)
      if (!TREE_VEC_ELT (targs, i))
      if (!TREE_VEC_ELT (targs, i))
        {
        {
          tree tparm;
          tree tparm;
 
 
          if (TREE_VEC_ELT (tparms, i) == error_mark_node)
          if (TREE_VEC_ELT (tparms, i) == error_mark_node)
            continue;
            continue;
 
 
          tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
          tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
 
 
          /* If this is an undeduced nontype parameter that depends on
          /* If this is an undeduced nontype parameter that depends on
             a type parameter, try another pass; its type may have been
             a type parameter, try another pass; its type may have been
             deduced from a later argument than the one from which
             deduced from a later argument than the one from which
             this parameter can be deduced.  */
             this parameter can be deduced.  */
          if (TREE_CODE (tparm) == PARM_DECL
          if (TREE_CODE (tparm) == PARM_DECL
              && uses_template_parms (TREE_TYPE (tparm))
              && uses_template_parms (TREE_TYPE (tparm))
              && !saw_undeduced++)
              && !saw_undeduced++)
            goto again;
            goto again;
 
 
          /* Core issue #226 (C++0x) [temp.deduct]:
          /* Core issue #226 (C++0x) [temp.deduct]:
 
 
               If a template argument has not been deduced, its
               If a template argument has not been deduced, its
               default template argument, if any, is used.
               default template argument, if any, is used.
 
 
             When we are in C++98 mode, TREE_PURPOSE will either
             When we are in C++98 mode, TREE_PURPOSE will either
             be NULL_TREE or ERROR_MARK_NODE, so we do not need
             be NULL_TREE or ERROR_MARK_NODE, so we do not need
             to explicitly check cxx_dialect here.  */
             to explicitly check cxx_dialect here.  */
          if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
          if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
            {
            {
              tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
              tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
              tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
              tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
              arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
              arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
              arg = convert_template_argument (parm, arg, targs, tf_none,
              arg = convert_template_argument (parm, arg, targs, tf_none,
                                               i, NULL_TREE);
                                               i, NULL_TREE);
              if (arg == error_mark_node)
              if (arg == error_mark_node)
                return 1;
                return 1;
              else
              else
                {
                {
                  TREE_VEC_ELT (targs, i) = arg;
                  TREE_VEC_ELT (targs, i) = arg;
                  /* The position of the first default template argument,
                  /* The position of the first default template argument,
                     is also the number of non-defaulted arguments in TARGS.
                     is also the number of non-defaulted arguments in TARGS.
                     Record that.  */
                     Record that.  */
                  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
                  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
                    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
                    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
                  continue;
                  continue;
                }
                }
            }
            }
 
 
          /* If the type parameter is a parameter pack, then it will
          /* If the type parameter is a parameter pack, then it will
             be deduced to an empty parameter pack.  */
             be deduced to an empty parameter pack.  */
          if (template_parameter_pack_p (tparm))
          if (template_parameter_pack_p (tparm))
            {
            {
              tree arg;
              tree arg;
 
 
              if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
              if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
                {
                {
                  arg = make_node (NONTYPE_ARGUMENT_PACK);
                  arg = make_node (NONTYPE_ARGUMENT_PACK);
                  TREE_TYPE (arg)  = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
                  TREE_TYPE (arg)  = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
                  TREE_CONSTANT (arg) = 1;
                  TREE_CONSTANT (arg) = 1;
                }
                }
              else
              else
                arg = cxx_make_type (TYPE_ARGUMENT_PACK);
                arg = cxx_make_type (TYPE_ARGUMENT_PACK);
 
 
              SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
              SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
 
 
              TREE_VEC_ELT (targs, i) = arg;
              TREE_VEC_ELT (targs, i) = arg;
              continue;
              continue;
            }
            }
 
 
          return 2;
          return 2;
        }
        }
#ifdef ENABLE_CHECKING
#ifdef ENABLE_CHECKING
  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
#endif
#endif
 
 
  return 0;
  return 0;
}
}
 
 
/* Subroutine of type_unification_real.  Args are like the variables
/* Subroutine of type_unification_real.  Args are like the variables
   at the call site.  ARG is an overloaded function (or template-id);
   at the call site.  ARG is an overloaded function (or template-id);
   we try deducing template args from each of the overloads, and if
   we try deducing template args from each of the overloads, and if
   only one succeeds, we go with that.  Modifies TARGS and returns
   only one succeeds, we go with that.  Modifies TARGS and returns
   true on success.  */
   true on success.  */
 
 
static bool
static bool
resolve_overloaded_unification (tree tparms,
resolve_overloaded_unification (tree tparms,
                                tree targs,
                                tree targs,
                                tree parm,
                                tree parm,
                                tree arg,
                                tree arg,
                                unification_kind_t strict,
                                unification_kind_t strict,
                                int sub_strict)
                                int sub_strict)
{
{
  tree tempargs = copy_node (targs);
  tree tempargs = copy_node (targs);
  int good = 0;
  int good = 0;
  tree goodfn = NULL_TREE;
  tree goodfn = NULL_TREE;
  bool addr_p;
  bool addr_p;
 
 
  if (TREE_CODE (arg) == ADDR_EXPR)
  if (TREE_CODE (arg) == ADDR_EXPR)
    {
    {
      arg = TREE_OPERAND (arg, 0);
      arg = TREE_OPERAND (arg, 0);
      addr_p = true;
      addr_p = true;
    }
    }
  else
  else
    addr_p = false;
    addr_p = false;
 
 
  if (TREE_CODE (arg) == COMPONENT_REF)
  if (TREE_CODE (arg) == COMPONENT_REF)
    /* Handle `&x' where `x' is some static or non-static member
    /* Handle `&x' where `x' is some static or non-static member
       function name.  */
       function name.  */
    arg = TREE_OPERAND (arg, 1);
    arg = TREE_OPERAND (arg, 1);
 
 
  if (TREE_CODE (arg) == OFFSET_REF)
  if (TREE_CODE (arg) == OFFSET_REF)
    arg = TREE_OPERAND (arg, 1);
    arg = TREE_OPERAND (arg, 1);
 
 
  /* Strip baselink information.  */
  /* Strip baselink information.  */
  if (BASELINK_P (arg))
  if (BASELINK_P (arg))
    arg = BASELINK_FUNCTIONS (arg);
    arg = BASELINK_FUNCTIONS (arg);
 
 
  if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
  if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
    {
    {
      /* If we got some explicit template args, we need to plug them into
      /* If we got some explicit template args, we need to plug them into
         the affected templates before we try to unify, in case the
         the affected templates before we try to unify, in case the
         explicit args will completely resolve the templates in question.  */
         explicit args will completely resolve the templates in question.  */
 
 
      tree expl_subargs = TREE_OPERAND (arg, 1);
      tree expl_subargs = TREE_OPERAND (arg, 1);
      arg = TREE_OPERAND (arg, 0);
      arg = TREE_OPERAND (arg, 0);
 
 
      for (; arg; arg = OVL_NEXT (arg))
      for (; arg; arg = OVL_NEXT (arg))
        {
        {
          tree fn = OVL_CURRENT (arg);
          tree fn = OVL_CURRENT (arg);
          tree subargs, elem;
          tree subargs, elem;
 
 
          if (TREE_CODE (fn) != TEMPLATE_DECL)
          if (TREE_CODE (fn) != TEMPLATE_DECL)
            continue;
            continue;
 
 
          ++processing_template_decl;
          ++processing_template_decl;
          subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
          subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
                                  expl_subargs, /*check_ret=*/false);
                                  expl_subargs, /*check_ret=*/false);
          if (subargs)
          if (subargs)
            {
            {
              elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
              elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
              if (try_one_overload (tparms, targs, tempargs, parm,
              if (try_one_overload (tparms, targs, tempargs, parm,
                                    elem, strict, sub_strict, addr_p)
                                    elem, strict, sub_strict, addr_p)
                  && (!goodfn || !decls_match (goodfn, elem)))
                  && (!goodfn || !decls_match (goodfn, elem)))
                {
                {
                  goodfn = elem;
                  goodfn = elem;
                  ++good;
                  ++good;
                }
                }
            }
            }
          --processing_template_decl;
          --processing_template_decl;
        }
        }
    }
    }
  else if (TREE_CODE (arg) != OVERLOAD
  else if (TREE_CODE (arg) != OVERLOAD
           && TREE_CODE (arg) != FUNCTION_DECL)
           && TREE_CODE (arg) != FUNCTION_DECL)
    /* If ARG is, for example, "(0, &f)" then its type will be unknown
    /* If ARG is, for example, "(0, &f)" then its type will be unknown
       -- but the deduction does not succeed because the expression is
       -- but the deduction does not succeed because the expression is
       not just the function on its own.  */
       not just the function on its own.  */
    return false;
    return false;
  else
  else
    for (; arg; arg = OVL_NEXT (arg))
    for (; arg; arg = OVL_NEXT (arg))
      if (try_one_overload (tparms, targs, tempargs, parm,
      if (try_one_overload (tparms, targs, tempargs, parm,
                            TREE_TYPE (OVL_CURRENT (arg)),
                            TREE_TYPE (OVL_CURRENT (arg)),
                            strict, sub_strict, addr_p)
                            strict, sub_strict, addr_p)
          && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
          && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
        {
        {
          goodfn = OVL_CURRENT (arg);
          goodfn = OVL_CURRENT (arg);
          ++good;
          ++good;
        }
        }
 
 
  /* [temp.deduct.type] A template-argument can be deduced from a pointer
  /* [temp.deduct.type] A template-argument can be deduced from a pointer
     to function or pointer to member function argument if the set of
     to function or pointer to member function argument if the set of
     overloaded functions does not contain function templates and at most
     overloaded functions does not contain function templates and at most
     one of a set of overloaded functions provides a unique match.
     one of a set of overloaded functions provides a unique match.
 
 
     So if we found multiple possibilities, we return success but don't
     So if we found multiple possibilities, we return success but don't
     deduce anything.  */
     deduce anything.  */
 
 
  if (good == 1)
  if (good == 1)
    {
    {
      int i = TREE_VEC_LENGTH (targs);
      int i = TREE_VEC_LENGTH (targs);
      for (; i--; )
      for (; i--; )
        if (TREE_VEC_ELT (tempargs, i))
        if (TREE_VEC_ELT (tempargs, i))
          TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
          TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
    }
    }
  if (good)
  if (good)
    return true;
    return true;
 
 
  return false;
  return false;
}
}
 
 
/* Core DR 115: In contexts where deduction is done and fails, or in
/* Core DR 115: In contexts where deduction is done and fails, or in
   contexts where deduction is not done, if a template argument list is
   contexts where deduction is not done, if a template argument list is
   specified and it, along with any default template arguments, identifies
   specified and it, along with any default template arguments, identifies
   a single function template specialization, then the template-id is an
   a single function template specialization, then the template-id is an
   lvalue for the function template specialization.  */
   lvalue for the function template specialization.  */
 
 
tree
tree
resolve_nondeduced_context (tree orig_expr)
resolve_nondeduced_context (tree orig_expr)
{
{
  tree expr, offset, baselink;
  tree expr, offset, baselink;
  bool addr;
  bool addr;
 
 
  if (!type_unknown_p (orig_expr))
  if (!type_unknown_p (orig_expr))
    return orig_expr;
    return orig_expr;
 
 
  expr = orig_expr;
  expr = orig_expr;
  addr = false;
  addr = false;
  offset = NULL_TREE;
  offset = NULL_TREE;
  baselink = NULL_TREE;
  baselink = NULL_TREE;
 
 
  if (TREE_CODE (expr) == ADDR_EXPR)
  if (TREE_CODE (expr) == ADDR_EXPR)
    {
    {
      expr = TREE_OPERAND (expr, 0);
      expr = TREE_OPERAND (expr, 0);
      addr = true;
      addr = true;
    }
    }
  if (TREE_CODE (expr) == OFFSET_REF)
  if (TREE_CODE (expr) == OFFSET_REF)
    {
    {
      offset = expr;
      offset = expr;
      expr = TREE_OPERAND (expr, 1);
      expr = TREE_OPERAND (expr, 1);
    }
    }
  if (TREE_CODE (expr) == BASELINK)
  if (TREE_CODE (expr) == BASELINK)
    {
    {
      baselink = expr;
      baselink = expr;
      expr = BASELINK_FUNCTIONS (expr);
      expr = BASELINK_FUNCTIONS (expr);
    }
    }
 
 
  if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
  if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
    {
    {
      int good = 0;
      int good = 0;
      tree goodfn = NULL_TREE;
      tree goodfn = NULL_TREE;
 
 
      /* If we got some explicit template args, we need to plug them into
      /* If we got some explicit template args, we need to plug them into
         the affected templates before we try to unify, in case the
         the affected templates before we try to unify, in case the
         explicit args will completely resolve the templates in question.  */
         explicit args will completely resolve the templates in question.  */
 
 
      tree expl_subargs = TREE_OPERAND (expr, 1);
      tree expl_subargs = TREE_OPERAND (expr, 1);
      tree arg = TREE_OPERAND (expr, 0);
      tree arg = TREE_OPERAND (expr, 0);
      tree badfn = NULL_TREE;
      tree badfn = NULL_TREE;
      tree badargs = NULL_TREE;
      tree badargs = NULL_TREE;
 
 
      for (; arg; arg = OVL_NEXT (arg))
      for (; arg; arg = OVL_NEXT (arg))
        {
        {
          tree fn = OVL_CURRENT (arg);
          tree fn = OVL_CURRENT (arg);
          tree subargs, elem;
          tree subargs, elem;
 
 
          if (TREE_CODE (fn) != TEMPLATE_DECL)
          if (TREE_CODE (fn) != TEMPLATE_DECL)
            continue;
            continue;
 
 
          ++processing_template_decl;
          ++processing_template_decl;
          subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
          subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
                                  expl_subargs, /*check_ret=*/false);
                                  expl_subargs, /*check_ret=*/false);
          if (subargs && !any_dependent_template_arguments_p (subargs))
          if (subargs && !any_dependent_template_arguments_p (subargs))
            {
            {
              elem = instantiate_template (fn, subargs, tf_none);
              elem = instantiate_template (fn, subargs, tf_none);
              if (elem == error_mark_node)
              if (elem == error_mark_node)
                {
                {
                  badfn = fn;
                  badfn = fn;
                  badargs = subargs;
                  badargs = subargs;
                }
                }
              else if (elem && (!goodfn || !decls_match (goodfn, elem)))
              else if (elem && (!goodfn || !decls_match (goodfn, elem)))
                {
                {
                  goodfn = elem;
                  goodfn = elem;
                  ++good;
                  ++good;
                }
                }
            }
            }
          --processing_template_decl;
          --processing_template_decl;
        }
        }
      if (good == 1)
      if (good == 1)
        {
        {
          expr = goodfn;
          expr = goodfn;
          if (baselink)
          if (baselink)
            expr = build_baselink (BASELINK_BINFO (baselink),
            expr = build_baselink (BASELINK_BINFO (baselink),
                                   BASELINK_ACCESS_BINFO (baselink),
                                   BASELINK_ACCESS_BINFO (baselink),
                                   expr, BASELINK_OPTYPE (baselink));
                                   expr, BASELINK_OPTYPE (baselink));
          if (offset)
          if (offset)
            expr = build2 (OFFSET_REF, TREE_TYPE (expr),
            expr = build2 (OFFSET_REF, TREE_TYPE (expr),
                           TREE_OPERAND (offset, 0), expr);
                           TREE_OPERAND (offset, 0), expr);
          if (addr)
          if (addr)
            expr = build_address (expr);
            expr = build_address (expr);
          return expr;
          return expr;
        }
        }
      else if (good == 0 && badargs)
      else if (good == 0 && badargs)
        /* There were no good options and at least one bad one, so let the
        /* There were no good options and at least one bad one, so let the
           user know what the problem is.  */
           user know what the problem is.  */
        instantiate_template (badfn, badargs, tf_warning_or_error);
        instantiate_template (badfn, badargs, tf_warning_or_error);
    }
    }
  return orig_expr;
  return orig_expr;
}
}
 
 
/* Subroutine of resolve_overloaded_unification; does deduction for a single
/* Subroutine of resolve_overloaded_unification; does deduction for a single
   overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   different overloads deduce different arguments for a given parm.
   different overloads deduce different arguments for a given parm.
   ADDR_P is true if the expression for which deduction is being
   ADDR_P is true if the expression for which deduction is being
   performed was of the form "& fn" rather than simply "fn".
   performed was of the form "& fn" rather than simply "fn".
 
 
   Returns 1 on success.  */
   Returns 1 on success.  */
 
 
static int
static int
try_one_overload (tree tparms,
try_one_overload (tree tparms,
                  tree orig_targs,
                  tree orig_targs,
                  tree targs,
                  tree targs,
                  tree parm,
                  tree parm,
                  tree arg,
                  tree arg,
                  unification_kind_t strict,
                  unification_kind_t strict,
                  int sub_strict,
                  int sub_strict,
                  bool addr_p)
                  bool addr_p)
{
{
  int nargs;
  int nargs;
  tree tempargs;
  tree tempargs;
  int i;
  int i;
 
 
  /* [temp.deduct.type] A template-argument can be deduced from a pointer
  /* [temp.deduct.type] A template-argument can be deduced from a pointer
     to function or pointer to member function argument if the set of
     to function or pointer to member function argument if the set of
     overloaded functions does not contain function templates and at most
     overloaded functions does not contain function templates and at most
     one of a set of overloaded functions provides a unique match.
     one of a set of overloaded functions provides a unique match.
 
 
     So if this is a template, just return success.  */
     So if this is a template, just return success.  */
 
 
  if (uses_template_parms (arg))
  if (uses_template_parms (arg))
    return 1;
    return 1;
 
 
  if (TREE_CODE (arg) == METHOD_TYPE)
  if (TREE_CODE (arg) == METHOD_TYPE)
    arg = build_ptrmemfunc_type (build_pointer_type (arg));
    arg = build_ptrmemfunc_type (build_pointer_type (arg));
  else if (addr_p)
  else if (addr_p)
    arg = build_pointer_type (arg);
    arg = build_pointer_type (arg);
 
 
  sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
  sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
 
 
  /* We don't copy orig_targs for this because if we have already deduced
  /* We don't copy orig_targs for this because if we have already deduced
     some template args from previous args, unify would complain when we
     some template args from previous args, unify would complain when we
     try to deduce a template parameter for the same argument, even though
     try to deduce a template parameter for the same argument, even though
     there isn't really a conflict.  */
     there isn't really a conflict.  */
  nargs = TREE_VEC_LENGTH (targs);
  nargs = TREE_VEC_LENGTH (targs);
  tempargs = make_tree_vec (nargs);
  tempargs = make_tree_vec (nargs);
 
 
  if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
  if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
    return 0;
    return 0;
 
 
  /* First make sure we didn't deduce anything that conflicts with
  /* First make sure we didn't deduce anything that conflicts with
     explicitly specified args.  */
     explicitly specified args.  */
  for (i = nargs; i--; )
  for (i = nargs; i--; )
    {
    {
      tree elt = TREE_VEC_ELT (tempargs, i);
      tree elt = TREE_VEC_ELT (tempargs, i);
      tree oldelt = TREE_VEC_ELT (orig_targs, i);
      tree oldelt = TREE_VEC_ELT (orig_targs, i);
 
 
      if (!elt)
      if (!elt)
        /*NOP*/;
        /*NOP*/;
      else if (uses_template_parms (elt))
      else if (uses_template_parms (elt))
        /* Since we're unifying against ourselves, we will fill in
        /* Since we're unifying against ourselves, we will fill in
           template args used in the function parm list with our own
           template args used in the function parm list with our own
           template parms.  Discard them.  */
           template parms.  Discard them.  */
        TREE_VEC_ELT (tempargs, i) = NULL_TREE;
        TREE_VEC_ELT (tempargs, i) = NULL_TREE;
      else if (oldelt && !template_args_equal (oldelt, elt))
      else if (oldelt && !template_args_equal (oldelt, elt))
        return 0;
        return 0;
    }
    }
 
 
  for (i = nargs; i--; )
  for (i = nargs; i--; )
    {
    {
      tree elt = TREE_VEC_ELT (tempargs, i);
      tree elt = TREE_VEC_ELT (tempargs, i);
 
 
      if (elt)
      if (elt)
        TREE_VEC_ELT (targs, i) = elt;
        TREE_VEC_ELT (targs, i) = elt;
    }
    }
 
 
  return 1;
  return 1;
}
}
 
 
/* PARM is a template class (perhaps with unbound template
/* PARM is a template class (perhaps with unbound template
   parameters).  ARG is a fully instantiated type.  If ARG can be
   parameters).  ARG is a fully instantiated type.  If ARG can be
   bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   TARGS are as for unify.  */
   TARGS are as for unify.  */
 
 
static tree
static tree
try_class_unification (tree tparms, tree targs, tree parm, tree arg)
try_class_unification (tree tparms, tree targs, tree parm, tree arg)
{
{
  tree copy_of_targs;
  tree copy_of_targs;
 
 
  if (!CLASSTYPE_TEMPLATE_INFO (arg)
  if (!CLASSTYPE_TEMPLATE_INFO (arg)
      || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
      || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
          != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
          != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
    return NULL_TREE;
    return NULL_TREE;
 
 
  /* We need to make a new template argument vector for the call to
  /* We need to make a new template argument vector for the call to
     unify.  If we used TARGS, we'd clutter it up with the result of
     unify.  If we used TARGS, we'd clutter it up with the result of
     the attempted unification, even if this class didn't work out.
     the attempted unification, even if this class didn't work out.
     We also don't want to commit ourselves to all the unifications
     We also don't want to commit ourselves to all the unifications
     we've already done, since unification is supposed to be done on
     we've already done, since unification is supposed to be done on
     an argument-by-argument basis.  In other words, consider the
     an argument-by-argument basis.  In other words, consider the
     following pathological case:
     following pathological case:
 
 
       template <int I, int J, int K>
       template <int I, int J, int K>
       struct S {};
       struct S {};
 
 
       template <int I, int J>
       template <int I, int J>
       struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
       struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
 
 
       template <int I, int J, int K>
       template <int I, int J, int K>
       void f(S<I, J, K>, S<I, I, I>);
       void f(S<I, J, K>, S<I, I, I>);
 
 
       void g() {
       void g() {
         S<0, 0, 0> s0;
         S<0, 0, 0> s0;
         S<0, 1, 2> s2;
         S<0, 1, 2> s2;
 
 
         f(s0, s2);
         f(s0, s2);
       }
       }
 
 
     Now, by the time we consider the unification involving `s2', we
     Now, by the time we consider the unification involving `s2', we
     already know that we must have `f<0, 0, 0>'.  But, even though
     already know that we must have `f<0, 0, 0>'.  But, even though
     `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
     `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
     because there are two ways to unify base classes of S<0, 1, 2>
     because there are two ways to unify base classes of S<0, 1, 2>
     with S<I, I, I>.  If we kept the already deduced knowledge, we
     with S<I, I, I>.  If we kept the already deduced knowledge, we
     would reject the possibility I=1.  */
     would reject the possibility I=1.  */
  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
 
 
  /* If unification failed, we're done.  */
  /* If unification failed, we're done.  */
  if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
  if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
             CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
             CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
    return NULL_TREE;
    return NULL_TREE;
 
 
  return arg;
  return arg;
}
}
 
 
/* Given a template type PARM and a class type ARG, find the unique
/* Given a template type PARM and a class type ARG, find the unique
   base type in ARG that is an instance of PARM.  We do not examine
   base type in ARG that is an instance of PARM.  We do not examine
   ARG itself; only its base-classes.  If there is not exactly one
   ARG itself; only its base-classes.  If there is not exactly one
   appropriate base class, return NULL_TREE.  PARM may be the type of
   appropriate base class, return NULL_TREE.  PARM may be the type of
   a partial specialization, as well as a plain template type.  Used
   a partial specialization, as well as a plain template type.  Used
   by unify.  */
   by unify.  */
 
 
static tree
static tree
get_template_base (tree tparms, tree targs, tree parm, tree arg)
get_template_base (tree tparms, tree targs, tree parm, tree arg)
{
{
  tree rval = NULL_TREE;
  tree rval = NULL_TREE;
  tree binfo;
  tree binfo;
 
 
  gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
  gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
 
 
  binfo = TYPE_BINFO (complete_type (arg));
  binfo = TYPE_BINFO (complete_type (arg));
  if (!binfo)
  if (!binfo)
    /* The type could not be completed.  */
    /* The type could not be completed.  */
    return NULL_TREE;
    return NULL_TREE;
 
 
  /* Walk in inheritance graph order.  The search order is not
  /* Walk in inheritance graph order.  The search order is not
     important, and this avoids multiple walks of virtual bases.  */
     important, and this avoids multiple walks of virtual bases.  */
  for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
  for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
    {
    {
      tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
      tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
 
 
      if (r)
      if (r)
        {
        {
          /* If there is more than one satisfactory baseclass, then:
          /* If there is more than one satisfactory baseclass, then:
 
 
               [temp.deduct.call]
               [temp.deduct.call]
 
 
              If they yield more than one possible deduced A, the type
              If they yield more than one possible deduced A, the type
              deduction fails.
              deduction fails.
 
 
             applies.  */
             applies.  */
          if (rval && !same_type_p (r, rval))
          if (rval && !same_type_p (r, rval))
            return NULL_TREE;
            return NULL_TREE;
 
 
          rval = r;
          rval = r;
        }
        }
    }
    }
 
 
  return rval;
  return rval;
}
}
 
 
/* Returns the level of DECL, which declares a template parameter.  */
/* Returns the level of DECL, which declares a template parameter.  */
 
 
static int
static int
template_decl_level (tree decl)
template_decl_level (tree decl)
{
{
  switch (TREE_CODE (decl))
  switch (TREE_CODE (decl))
    {
    {
    case TYPE_DECL:
    case TYPE_DECL:
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
      return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
 
 
    case PARM_DECL:
    case PARM_DECL:
      return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
      return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
 
 
    default:
    default:
      gcc_unreachable ();
      gcc_unreachable ();
    }
    }
  return 0;
  return 0;
}
}
 
 
/* Decide whether ARG can be unified with PARM, considering only the
/* Decide whether ARG can be unified with PARM, considering only the
   cv-qualifiers of each type, given STRICT as documented for unify.
   cv-qualifiers of each type, given STRICT as documented for unify.
   Returns nonzero iff the unification is OK on that basis.  */
   Returns nonzero iff the unification is OK on that basis.  */
 
 
static int
static int
check_cv_quals_for_unify (int strict, tree arg, tree parm)
check_cv_quals_for_unify (int strict, tree arg, tree parm)
{
{
  int arg_quals = cp_type_quals (arg);
  int arg_quals = cp_type_quals (arg);
  int parm_quals = cp_type_quals (parm);
  int parm_quals = cp_type_quals (parm);
 
 
  if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
  if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
      && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
      && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
    {
    {
      /*  Although a CVR qualifier is ignored when being applied to a
      /*  Although a CVR qualifier is ignored when being applied to a
          substituted template parameter ([8.3.2]/1 for example), that
          substituted template parameter ([8.3.2]/1 for example), that
          does not apply during deduction [14.8.2.4]/1, (even though
          does not apply during deduction [14.8.2.4]/1, (even though
          that is not explicitly mentioned, [14.8.2.4]/9 indicates
          that is not explicitly mentioned, [14.8.2.4]/9 indicates
          this).  Except when we're allowing additional CV qualifiers
          this).  Except when we're allowing additional CV qualifiers
          at the outer level [14.8.2.1]/3,1st bullet.  */
          at the outer level [14.8.2.1]/3,1st bullet.  */
      if ((TREE_CODE (arg) == REFERENCE_TYPE
      if ((TREE_CODE (arg) == REFERENCE_TYPE
           || TREE_CODE (arg) == FUNCTION_TYPE
           || TREE_CODE (arg) == FUNCTION_TYPE
           || TREE_CODE (arg) == METHOD_TYPE)
           || TREE_CODE (arg) == METHOD_TYPE)
          && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
          && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
        return 0;
        return 0;
 
 
      if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
      if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
          && (parm_quals & TYPE_QUAL_RESTRICT))
          && (parm_quals & TYPE_QUAL_RESTRICT))
        return 0;
        return 0;
    }
    }
 
 
  if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
  if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
      && (arg_quals & parm_quals) != parm_quals)
      && (arg_quals & parm_quals) != parm_quals)
    return 0;
    return 0;
 
 
  if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
  if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
      && (parm_quals & arg_quals) != arg_quals)
      && (parm_quals & arg_quals) != arg_quals)
    return 0;
    return 0;
 
 
  return 1;
  return 1;
}
}
 
 
/* Determines the LEVEL and INDEX for the template parameter PARM.  */
/* Determines the LEVEL and INDEX for the template parameter PARM.  */
void
void
template_parm_level_and_index (tree parm, int* level, int* index)
template_parm_level_and_index (tree parm, int* level, int* index)
{
{
  if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
  if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
      || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
      || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
      || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
      || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
    {
    {
      *index = TEMPLATE_TYPE_IDX (parm);
      *index = TEMPLATE_TYPE_IDX (parm);
      *level = TEMPLATE_TYPE_LEVEL (parm);
      *level = TEMPLATE_TYPE_LEVEL (parm);
    }
    }
  else
  else
    {
    {
      *index = TEMPLATE_PARM_IDX (parm);
      *index = TEMPLATE_PARM_IDX (parm);
      *level = TEMPLATE_PARM_LEVEL (parm);
      *level = TEMPLATE_PARM_LEVEL (parm);
    }
    }
}
}
 
 
/* Unifies the remaining arguments in PACKED_ARGS with the pack
/* Unifies the remaining arguments in PACKED_ARGS with the pack
   expansion at the end of PACKED_PARMS. Returns 0 if the type
   expansion at the end of PACKED_PARMS. Returns 0 if the type
   deduction succeeds, 1 otherwise. STRICT is the same as in
   deduction succeeds, 1 otherwise. STRICT is the same as in
   unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
   unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
   call argument list. We'll need to adjust the arguments to make them
   call argument list. We'll need to adjust the arguments to make them
   types. SUBR tells us if this is from a recursive call to
   types. SUBR tells us if this is from a recursive call to
   type_unification_real.  */
   type_unification_real.  */
int
int
unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
                      tree packed_args, int strict, bool call_args_p,
                      tree packed_args, int strict, bool call_args_p,
                      bool subr)
                      bool subr)
{
{
  tree parm
  tree parm
    = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
    = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
  tree pattern = PACK_EXPANSION_PATTERN (parm);
  tree pattern = PACK_EXPANSION_PATTERN (parm);
  tree pack, packs = NULL_TREE;
  tree pack, packs = NULL_TREE;
  int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
  int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
  int len = TREE_VEC_LENGTH (packed_args);
  int len = TREE_VEC_LENGTH (packed_args);
 
 
  /* Determine the parameter packs we will be deducing from the
  /* Determine the parameter packs we will be deducing from the
     pattern, and record their current deductions.  */
     pattern, and record their current deductions.  */
  for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
  for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
       pack; pack = TREE_CHAIN (pack))
       pack; pack = TREE_CHAIN (pack))
    {
    {
      tree parm_pack = TREE_VALUE (pack);
      tree parm_pack = TREE_VALUE (pack);
      int idx, level;
      int idx, level;
 
 
      /* Determine the index and level of this parameter pack.  */
      /* Determine the index and level of this parameter pack.  */
      template_parm_level_and_index (parm_pack, &level, &idx);
      template_parm_level_and_index (parm_pack, &level, &idx);
 
 
      /* Keep track of the parameter packs and their corresponding
      /* Keep track of the parameter packs and their corresponding
         argument packs.  */
         argument packs.  */
      packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
      packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
      TREE_TYPE (packs) = make_tree_vec (len - start);
      TREE_TYPE (packs) = make_tree_vec (len - start);
    }
    }
 
 
  /* Loop through all of the arguments that have not yet been
  /* Loop through all of the arguments that have not yet been
     unified and unify each with the pattern.  */
     unified and unify each with the pattern.  */
  for (i = start; i < len; i++)
  for (i = start; i < len; i++)
    {
    {
      tree parm = pattern;
      tree parm = pattern;
 
 
      /* For each parameter pack, clear out the deduced value so that
      /* For each parameter pack, clear out the deduced value so that
         we can deduce it again.  */
         we can deduce it again.  */
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
        {
        {
          int idx, level;
          int idx, level;
          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
 
 
          TMPL_ARG (targs, level, idx) = NULL_TREE;
          TMPL_ARG (targs, level, idx) = NULL_TREE;
        }
        }
 
 
      /* Unify the pattern with the current argument.  */
      /* Unify the pattern with the current argument.  */
      {
      {
        tree arg = TREE_VEC_ELT (packed_args, i);
        tree arg = TREE_VEC_ELT (packed_args, i);
        tree arg_expr = NULL_TREE;
        tree arg_expr = NULL_TREE;
        int arg_strict = strict;
        int arg_strict = strict;
        bool skip_arg_p = false;
        bool skip_arg_p = false;
 
 
        if (call_args_p)
        if (call_args_p)
          {
          {
            int sub_strict;
            int sub_strict;
 
 
            /* This mirrors what we do in type_unification_real.  */
            /* This mirrors what we do in type_unification_real.  */
            switch (strict)
            switch (strict)
              {
              {
              case DEDUCE_CALL:
              case DEDUCE_CALL:
                sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
                sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
                              | UNIFY_ALLOW_MORE_CV_QUAL
                              | UNIFY_ALLOW_MORE_CV_QUAL
                              | UNIFY_ALLOW_DERIVED);
                              | UNIFY_ALLOW_DERIVED);
                break;
                break;
 
 
              case DEDUCE_CONV:
              case DEDUCE_CONV:
                sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
                sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
                break;
                break;
 
 
              case DEDUCE_EXACT:
              case DEDUCE_EXACT:
                sub_strict = UNIFY_ALLOW_NONE;
                sub_strict = UNIFY_ALLOW_NONE;
                break;
                break;
 
 
              default:
              default:
                gcc_unreachable ();
                gcc_unreachable ();
              }
              }
 
 
            if (!TYPE_P (arg))
            if (!TYPE_P (arg))
              {
              {
                gcc_assert (TREE_TYPE (arg) != NULL_TREE);
                gcc_assert (TREE_TYPE (arg) != NULL_TREE);
                if (type_unknown_p (arg))
                if (type_unknown_p (arg))
                  {
                  {
                    /* [temp.deduct.type] A template-argument can be
                    /* [temp.deduct.type] A template-argument can be
                       deduced from a pointer to function or pointer
                       deduced from a pointer to function or pointer
                       to member function argument if the set of
                       to member function argument if the set of
                       overloaded functions does not contain function
                       overloaded functions does not contain function
                       templates and at most one of a set of
                       templates and at most one of a set of
                       overloaded functions provides a unique
                       overloaded functions provides a unique
                       match.  */
                       match.  */
 
 
                    if (resolve_overloaded_unification
                    if (resolve_overloaded_unification
                        (tparms, targs, parm, arg,
                        (tparms, targs, parm, arg,
                         (unification_kind_t) strict,
                         (unification_kind_t) strict,
                         sub_strict)
                         sub_strict)
                        != 0)
                        != 0)
                      return 1;
                      return 1;
                    skip_arg_p = true;
                    skip_arg_p = true;
                  }
                  }
 
 
                if (!skip_arg_p)
                if (!skip_arg_p)
                  {
                  {
                    arg_expr = arg;
                    arg_expr = arg;
                    arg = unlowered_expr_type (arg);
                    arg = unlowered_expr_type (arg);
                    if (arg == error_mark_node)
                    if (arg == error_mark_node)
                      return 1;
                      return 1;
                  }
                  }
              }
              }
 
 
            arg_strict = sub_strict;
            arg_strict = sub_strict;
 
 
            if (!subr)
            if (!subr)
              arg_strict |=
              arg_strict |=
                maybe_adjust_types_for_deduction ((unification_kind_t) strict,
                maybe_adjust_types_for_deduction ((unification_kind_t) strict,
                                                  &parm, &arg, arg_expr);
                                                  &parm, &arg, arg_expr);
          }
          }
 
 
        if (!skip_arg_p)
        if (!skip_arg_p)
          {
          {
            /* For deduction from an init-list we need the actual list.  */
            /* For deduction from an init-list we need the actual list.  */
            if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
            if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
              arg = arg_expr;
              arg = arg_expr;
            if (unify (tparms, targs, parm, arg, arg_strict))
            if (unify (tparms, targs, parm, arg, arg_strict))
              return 1;
              return 1;
          }
          }
      }
      }
 
 
      /* For each parameter pack, collect the deduced value.  */
      /* For each parameter pack, collect the deduced value.  */
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
      for (pack = packs; pack; pack = TREE_CHAIN (pack))
        {
        {
          int idx, level;
          int idx, level;
          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
 
 
          TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
          TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
            TMPL_ARG (targs, level, idx);
            TMPL_ARG (targs, level, idx);
        }
        }
    }
    }
 
 
  /* Verify that the results of unification with the parameter packs
  /* Verify that the results of unification with the parameter packs
     produce results consistent with what we've seen before, and make
     produce results consistent with what we've seen before, and make
     the deduced argument packs available.  */
     the deduced argument packs available.  */
  for (pack = packs; pack; pack = TREE_CHAIN (pack))
  for (pack = packs; pack; pack = TREE_CHAIN (pack))
    {
    {
      tree old_pack = TREE_VALUE (pack);
      tree old_pack = TREE_VALUE (pack);
      tree new_args = TREE_TYPE (pack);
      tree new_args = TREE_TYPE (pack);
      int i, len = TREE_VEC_LENGTH (new_args);
      int i, len = TREE_VEC_LENGTH (new_args);
      int idx, level;
      int idx, level;
      bool nondeduced_p = false;
      bool nondeduced_p = false;
 
 
      /* By default keep the original deduced argument pack.
      /* By default keep the original deduced argument pack.
         If necessary, more specific code is going to update the
         If necessary, more specific code is going to update the
         resulting deduced argument later down in this function.  */
         resulting deduced argument later down in this function.  */
      template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
      template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
      TMPL_ARG (targs, level, idx) = old_pack;
      TMPL_ARG (targs, level, idx) = old_pack;
 
 
      /* If NEW_ARGS contains any NULL_TREE entries, we didn't
      /* If NEW_ARGS contains any NULL_TREE entries, we didn't
         actually deduce anything.  */
         actually deduce anything.  */
      for (i = 0; i < len && !nondeduced_p; ++i)
      for (i = 0; i < len && !nondeduced_p; ++i)
        if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
        if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
          nondeduced_p = true;
          nondeduced_p = true;
      if (nondeduced_p)
      if (nondeduced_p)
        continue;
        continue;
 
 
      if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
      if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
        {
        {
          /* Prepend the explicit arguments onto NEW_ARGS.  */
          /* Prepend the explicit arguments onto NEW_ARGS.  */
          tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
          tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
          tree old_args = new_args;
          tree old_args = new_args;
          int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
          int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
          int len = explicit_len + TREE_VEC_LENGTH (old_args);
          int len = explicit_len + TREE_VEC_LENGTH (old_args);
 
 
          /* Copy the explicit arguments.  */
          /* Copy the explicit arguments.  */
          new_args = make_tree_vec (len);
          new_args = make_tree_vec (len);
          for (i = 0; i < explicit_len; i++)
          for (i = 0; i < explicit_len; i++)
            TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
            TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
 
 
          /* Copy the deduced arguments.  */
          /* Copy the deduced arguments.  */
          for (; i < len; i++)
          for (; i < len; i++)
            TREE_VEC_ELT (new_args, i) =
            TREE_VEC_ELT (new_args, i) =
              TREE_VEC_ELT (old_args, i - explicit_len);
              TREE_VEC_ELT (old_args, i - explicit_len);
        }
        }
 
 
      if (!old_pack)
      if (!old_pack)
        {
        {
          tree result;
          tree result;
          /* Build the deduced *_ARGUMENT_PACK.  */
          /* Build the deduced *_ARGUMENT_PACK.  */
          if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
          if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
            {
            {
              result = make_node (NONTYPE_ARGUMENT_PACK);
              result = make_node (NONTYPE_ARGUMENT_PACK);
              TREE_TYPE (result) =
              TREE_TYPE (result) =
                TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
                TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
              TREE_CONSTANT (result) = 1;
              TREE_CONSTANT (result) = 1;
            }
            }
          else
          else
            result = cxx_make_type (TYPE_ARGUMENT_PACK);
            result = cxx_make_type (TYPE_ARGUMENT_PACK);
 
 
          SET_ARGUMENT_PACK_ARGS (result, new_args);
          SET_ARGUMENT_PACK_ARGS (result, new_args);
 
 
          /* Note the deduced argument packs for this parameter
          /* Note the deduced argument packs for this parameter
             pack.  */
             pack.  */
          TMPL_ARG (targs, level, idx) = result;
          TMPL_ARG (targs, level, idx) = result;
        }
        }
      else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
      else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
               && (ARGUMENT_PACK_ARGS (old_pack)
               && (ARGUMENT_PACK_ARGS (old_pack)
                   == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
                   == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
        {
        {
          /* We only had the explicitly-provided arguments before, but
          /* We only had the explicitly-provided arguments before, but
             now we have a complete set of arguments.  */
             now we have a complete set of arguments.  */
          tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
          tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
 
 
          SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
          SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
          ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
          ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
          ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
          ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
        }
        }
      else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
      else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
                                    new_args))
                                    new_args))
        /* Inconsistent unification of this parameter pack.  */
        /* Inconsistent unification of this parameter pack.  */
        return 1;
        return 1;
    }
    }
 
 
  return 0;
  return 0;
}
}
 
 
/* Deduce the value of template parameters.  TPARMS is the (innermost)
/* Deduce the value of template parameters.  TPARMS is the (innermost)
   set of template parameters to a template.  TARGS is the bindings
   set of template parameters to a template.  TARGS is the bindings
   for those template parameters, as determined thus far; TARGS may
   for those template parameters, as determined thus far; TARGS may
   include template arguments for outer levels of template parameters
   include template arguments for outer levels of template parameters
   as well.  PARM is a parameter to a template function, or a
   as well.  PARM is a parameter to a template function, or a
   subcomponent of that parameter; ARG is the corresponding argument.
   subcomponent of that parameter; ARG is the corresponding argument.
   This function attempts to match PARM with ARG in a manner
   This function attempts to match PARM with ARG in a manner
   consistent with the existing assignments in TARGS.  If more values
   consistent with the existing assignments in TARGS.  If more values
   are deduced, then TARGS is updated.
   are deduced, then TARGS is updated.
 
 
   Returns 0 if the type deduction succeeds, 1 otherwise.  The
   Returns 0 if the type deduction succeeds, 1 otherwise.  The
   parameter STRICT is a bitwise or of the following flags:
   parameter STRICT is a bitwise or of the following flags:
 
 
     UNIFY_ALLOW_NONE:
     UNIFY_ALLOW_NONE:
       Require an exact match between PARM and ARG.
       Require an exact match between PARM and ARG.
     UNIFY_ALLOW_MORE_CV_QUAL:
     UNIFY_ALLOW_MORE_CV_QUAL:
       Allow the deduced ARG to be more cv-qualified (by qualification
       Allow the deduced ARG to be more cv-qualified (by qualification
       conversion) than ARG.
       conversion) than ARG.
     UNIFY_ALLOW_LESS_CV_QUAL:
     UNIFY_ALLOW_LESS_CV_QUAL:
       Allow the deduced ARG to be less cv-qualified than ARG.
       Allow the deduced ARG to be less cv-qualified than ARG.
     UNIFY_ALLOW_DERIVED:
     UNIFY_ALLOW_DERIVED:
       Allow the deduced ARG to be a template base class of ARG,
       Allow the deduced ARG to be a template base class of ARG,
       or a pointer to a template base class of the type pointed to by
       or a pointer to a template base class of the type pointed to by
       ARG.
       ARG.
     UNIFY_ALLOW_INTEGER:
     UNIFY_ALLOW_INTEGER:
       Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
       Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
       case for more information.
       case for more information.
     UNIFY_ALLOW_OUTER_LEVEL:
     UNIFY_ALLOW_OUTER_LEVEL:
       This is the outermost level of a deduction. Used to determine validity
       This is the outermost level of a deduction. Used to determine validity
       of qualification conversions. A valid qualification conversion must
       of qualification conversions. A valid qualification conversion must
       have const qualified pointers leading up to the inner type which
       have const qualified pointers leading up to the inner type which
       requires additional CV quals, except at the outer level, where const
       requires additional CV quals, except at the outer level, where const
       is not required [conv.qual]. It would be normal to set this flag in
       is not required [conv.qual]. It would be normal to set this flag in
       addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
       addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
     UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
     UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
       This is the outermost level of a deduction, and PARM can be more CV
       This is the outermost level of a deduction, and PARM can be more CV
       qualified at this point.
       qualified at this point.
     UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
     UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
       This is the outermost level of a deduction, and PARM can be less CV
       This is the outermost level of a deduction, and PARM can be less CV
       qualified at this point.  */
       qualified at this point.  */
 
 
static int
static int
unify (tree tparms, tree targs, tree parm, tree arg, int strict)
unify (tree tparms, tree targs, tree parm, tree arg, int strict)
{
{
  int idx;
  int idx;
  tree targ;
  tree targ;
  tree tparm;
  tree tparm;
  int strict_in = strict;
  int strict_in = strict;
 
 
  /* I don't think this will do the right thing with respect to types.
  /* I don't think this will do the right thing with respect to types.
     But the only case I've seen it in so far has been array bounds, where
     But the only case I've seen it in so far has been array bounds, where
     signedness is the only information lost, and I think that will be
     signedness is the only information lost, and I think that will be
     okay.  */
     okay.  */
  while (TREE_CODE (parm) == NOP_EXPR)
  while (TREE_CODE (parm) == NOP_EXPR)
    parm = TREE_OPERAND (parm, 0);
    parm = TREE_OPERAND (parm, 0);
 
 
  if (arg == error_mark_node)
  if (arg == error_mark_node)
    return 1;
    return 1;
  if (arg == unknown_type_node
  if (arg == unknown_type_node
      || arg == init_list_type_node)
      || arg == init_list_type_node)
    /* We can't deduce anything from this, but we might get all the
    /* We can't deduce anything from this, but we might get all the
       template args from other function args.  */
       template args from other function args.  */
    return 0;
    return 0;
 
 
  /* If PARM uses template parameters, then we can't bail out here,
  /* If PARM uses template parameters, then we can't bail out here,
     even if ARG == PARM, since we won't record unifications for the
     even if ARG == PARM, since we won't record unifications for the
     template parameters.  We might need them if we're trying to
     template parameters.  We might need them if we're trying to
     figure out which of two things is more specialized.  */
     figure out which of two things is more specialized.  */
  if (arg == parm && !uses_template_parms (parm))
  if (arg == parm && !uses_template_parms (parm))
    return 0;
    return 0;
 
 
  /* Handle init lists early, so the rest of the function can assume
  /* Handle init lists early, so the rest of the function can assume
     we're dealing with a type. */
     we're dealing with a type. */
  if (BRACE_ENCLOSED_INITIALIZER_P (arg))
  if (BRACE_ENCLOSED_INITIALIZER_P (arg))
    {
    {
      tree elt, elttype;
      tree elt, elttype;
      unsigned i;
      unsigned i;
      tree orig_parm = parm;
      tree orig_parm = parm;
 
 
      /* Replace T with std::initializer_list<T> for deduction.  */
      /* Replace T with std::initializer_list<T> for deduction.  */
      if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
      if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
          && flag_deduce_init_list)
          && flag_deduce_init_list)
        parm = listify (parm);
        parm = listify (parm);
 
 
      if (!is_std_init_list (parm))
      if (!is_std_init_list (parm))
        /* We can only deduce from an initializer list argument if the
        /* We can only deduce from an initializer list argument if the
           parameter is std::initializer_list; otherwise this is a
           parameter is std::initializer_list; otherwise this is a
           non-deduced context. */
           non-deduced context. */
        return 0;
        return 0;
 
 
      elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
      elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
 
 
      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
        {
        {
          int elt_strict = strict;
          int elt_strict = strict;
          if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
          if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
            {
            {
              tree type = TREE_TYPE (elt);
              tree type = TREE_TYPE (elt);
              /* It should only be possible to get here for a call.  */
              /* It should only be possible to get here for a call.  */
              gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
              gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
              elt_strict |= maybe_adjust_types_for_deduction
              elt_strict |= maybe_adjust_types_for_deduction
                (DEDUCE_CALL, &elttype, &type, elt);
                (DEDUCE_CALL, &elttype, &type, elt);
              elt = type;
              elt = type;
            }
            }
 
 
          if (unify (tparms, targs, elttype, elt, elt_strict))
          if (unify (tparms, targs, elttype, elt, elt_strict))
            return 1;
            return 1;
        }
        }
 
 
      /* If the std::initializer_list<T> deduction worked, replace the
      /* If the std::initializer_list<T> deduction worked, replace the
         deduced A with std::initializer_list<A>.  */
         deduced A with std::initializer_list<A>.  */
      if (orig_parm != parm)
      if (orig_parm != parm)
        {
        {
          idx = TEMPLATE_TYPE_IDX (orig_parm);
          idx = TEMPLATE_TYPE_IDX (orig_parm);
          targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
          targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
          targ = listify (targ);
          targ = listify (targ);
          TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
          TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
        }
        }
      return 0;
      return 0;
    }
    }
 
 
  /* Immediately reject some pairs that won't unify because of
  /* Immediately reject some pairs that won't unify because of
     cv-qualification mismatches.  */
     cv-qualification mismatches.  */
  if (TREE_CODE (arg) == TREE_CODE (parm)
  if (TREE_CODE (arg) == TREE_CODE (parm)
      && TYPE_P (arg)
      && TYPE_P (arg)
      /* It is the elements of the array which hold the cv quals of an array
      /* It is the elements of the array which hold the cv quals of an array
         type, and the elements might be template type parms. We'll check
         type, and the elements might be template type parms. We'll check
         when we recurse.  */
         when we recurse.  */
      && TREE_CODE (arg) != ARRAY_TYPE
      && TREE_CODE (arg) != ARRAY_TYPE
      /* We check the cv-qualifiers when unifying with template type
      /* We check the cv-qualifiers when unifying with template type
         parameters below.  We want to allow ARG `const T' to unify with
         parameters below.  We want to allow ARG `const T' to unify with
         PARM `T' for example, when computing which of two templates
         PARM `T' for example, when computing which of two templates
         is more specialized, for example.  */
         is more specialized, for example.  */
      && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
      && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
      && !check_cv_quals_for_unify (strict_in, arg, parm))
      && !check_cv_quals_for_unify (strict_in, arg, parm))
    return 1;
    return 1;
 
 
  if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
  if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
      && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
      && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
    strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
    strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
  strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
  strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
  strict &= ~UNIFY_ALLOW_DERIVED;
  strict &= ~UNIFY_ALLOW_DERIVED;
  strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
  strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
  strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
  strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
 
 
  switch (TREE_CODE (parm))
  switch (TREE_CODE (parm))
    {
    {
    case TYPENAME_TYPE:
    case TYPENAME_TYPE:
    case SCOPE_REF:
    case SCOPE_REF:
    case UNBOUND_CLASS_TEMPLATE:
    case UNBOUND_CLASS_TEMPLATE:
      /* In a type which contains a nested-name-specifier, template
      /* In a type which contains a nested-name-specifier, template
         argument values cannot be deduced for template parameters used
         argument values cannot be deduced for template parameters used
         within the nested-name-specifier.  */
         within the nested-name-specifier.  */
      return 0;
      return 0;
 
 
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
    case BOUND_TEMPLATE_TEMPLATE_PARM:
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
      if (tparm == error_mark_node)
      if (tparm == error_mark_node)
        return 1;
        return 1;
 
 
      if (TEMPLATE_TYPE_LEVEL (parm)
      if (TEMPLATE_TYPE_LEVEL (parm)
          != template_decl_level (tparm))
          != template_decl_level (tparm))
        /* The PARM is not one we're trying to unify.  Just check
        /* The PARM is not one we're trying to unify.  Just check
           to see if it matches ARG.  */
           to see if it matches ARG.  */
        return (TREE_CODE (arg) == TREE_CODE (parm)
        return (TREE_CODE (arg) == TREE_CODE (parm)
                && same_type_p (parm, arg)) ? 0 : 1;
                && same_type_p (parm, arg)) ? 0 : 1;
      idx = TEMPLATE_TYPE_IDX (parm);
      idx = TEMPLATE_TYPE_IDX (parm);
      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
 
 
      /* Check for mixed types and values.  */
      /* Check for mixed types and values.  */
      if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
      if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
           && TREE_CODE (tparm) != TYPE_DECL)
           && TREE_CODE (tparm) != TYPE_DECL)
          || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
          || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
              && TREE_CODE (tparm) != TEMPLATE_DECL))
              && TREE_CODE (tparm) != TEMPLATE_DECL))
        return 1;
        return 1;
 
 
      if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
      if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
        {
        {
          /* ARG must be constructed from a template class or a template
          /* ARG must be constructed from a template class or a template
             template parameter.  */
             template parameter.  */
          if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
          if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
              && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
              && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
            return 1;
            return 1;
 
 
          {
          {
            tree parmvec = TYPE_TI_ARGS (parm);
            tree parmvec = TYPE_TI_ARGS (parm);
            tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
            tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
            tree parm_parms
            tree parm_parms
              = DECL_INNERMOST_TEMPLATE_PARMS
              = DECL_INNERMOST_TEMPLATE_PARMS
                  (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
                  (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
            int i, len;
            int i, len;
            int parm_variadic_p = 0;
            int parm_variadic_p = 0;
 
 
            /* The resolution to DR150 makes clear that default
            /* The resolution to DR150 makes clear that default
               arguments for an N-argument may not be used to bind T
               arguments for an N-argument may not be used to bind T
               to a template template parameter with fewer than N
               to a template template parameter with fewer than N
               parameters.  It is not safe to permit the binding of
               parameters.  It is not safe to permit the binding of
               default arguments as an extension, as that may change
               default arguments as an extension, as that may change
               the meaning of a conforming program.  Consider:
               the meaning of a conforming program.  Consider:
 
 
                  struct Dense { static const unsigned int dim = 1; };
                  struct Dense { static const unsigned int dim = 1; };
 
 
                  template <template <typename> class View,
                  template <template <typename> class View,
                            typename Block>
                            typename Block>
                  void operator+(float, View<Block> const&);
                  void operator+(float, View<Block> const&);
 
 
                  template <typename Block,
                  template <typename Block,
                            unsigned int Dim = Block::dim>
                            unsigned int Dim = Block::dim>
                  struct Lvalue_proxy { operator float() const; };
                  struct Lvalue_proxy { operator float() const; };
 
 
                  void
                  void
                  test_1d (void) {
                  test_1d (void) {
                    Lvalue_proxy<Dense> p;
                    Lvalue_proxy<Dense> p;
                    float b;
                    float b;
                    b + p;
                    b + p;
                  }
                  }
 
 
              Here, if Lvalue_proxy is permitted to bind to View, then
              Here, if Lvalue_proxy is permitted to bind to View, then
              the global operator+ will be used; if they are not, the
              the global operator+ will be used; if they are not, the
              Lvalue_proxy will be converted to float.  */
              Lvalue_proxy will be converted to float.  */
            if (coerce_template_parms (parm_parms,
            if (coerce_template_parms (parm_parms,
                                       argvec,
                                       argvec,
                                       TYPE_TI_TEMPLATE (parm),
                                       TYPE_TI_TEMPLATE (parm),
                                       tf_none,
                                       tf_none,
                                       /*require_all_args=*/true,
                                       /*require_all_args=*/true,
                                       /*use_default_args=*/false)
                                       /*use_default_args=*/false)
                == error_mark_node)
                == error_mark_node)
              return 1;
              return 1;
 
 
            /* Deduce arguments T, i from TT<T> or TT<i>.
            /* Deduce arguments T, i from TT<T> or TT<i>.
               We check each element of PARMVEC and ARGVEC individually
               We check each element of PARMVEC and ARGVEC individually
               rather than the whole TREE_VEC since they can have
               rather than the whole TREE_VEC since they can have
               different number of elements.  */
               different number of elements.  */
 
 
            parmvec = expand_template_argument_pack (parmvec);
            parmvec = expand_template_argument_pack (parmvec);
            argvec = expand_template_argument_pack (argvec);
            argvec = expand_template_argument_pack (argvec);
 
 
            len = TREE_VEC_LENGTH (parmvec);
            len = TREE_VEC_LENGTH (parmvec);
 
 
            /* Check if the parameters end in a pack, making them
            /* Check if the parameters end in a pack, making them
               variadic.  */
               variadic.  */
            if (len > 0
            if (len > 0
                && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
                && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
              parm_variadic_p = 1;
              parm_variadic_p = 1;
 
 
            if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
            if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
              return 1;
              return 1;
 
 
             for (i = 0; i < len - parm_variadic_p; ++i)
             for (i = 0; i < len - parm_variadic_p; ++i)
              {
              {
                if (unify (tparms, targs,
                if (unify (tparms, targs,
                           TREE_VEC_ELT (parmvec, i),
                           TREE_VEC_ELT (parmvec, i),
                           TREE_VEC_ELT (argvec, i),
                           TREE_VEC_ELT (argvec, i),
                           UNIFY_ALLOW_NONE))
                           UNIFY_ALLOW_NONE))
                  return 1;
                  return 1;
              }
              }
 
 
            if (parm_variadic_p
            if (parm_variadic_p
                && unify_pack_expansion (tparms, targs,
                && unify_pack_expansion (tparms, targs,
                                         parmvec, argvec,
                                         parmvec, argvec,
                                         UNIFY_ALLOW_NONE,
                                         UNIFY_ALLOW_NONE,
                                         /*call_args_p=*/false,
                                         /*call_args_p=*/false,
                                         /*subr=*/false))
                                         /*subr=*/false))
              return 1;
              return 1;
          }
          }
          arg = TYPE_TI_TEMPLATE (arg);
          arg = TYPE_TI_TEMPLATE (arg);
 
 
          /* Fall through to deduce template name.  */
          /* Fall through to deduce template name.  */
        }
        }
 
 
      if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
      if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
          || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
          || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
        {
        {
          /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
          /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
 
 
          /* Simple cases: Value already set, does match or doesn't.  */
          /* Simple cases: Value already set, does match or doesn't.  */
          if (targ != NULL_TREE && template_args_equal (targ, arg))
          if (targ != NULL_TREE && template_args_equal (targ, arg))
            return 0;
            return 0;
          else if (targ)
          else if (targ)
            return 1;
            return 1;
        }
        }
      else
      else
        {
        {
          /* If PARM is `const T' and ARG is only `int', we don't have
          /* If PARM is `const T' and ARG is only `int', we don't have
             a match unless we are allowing additional qualification.
             a match unless we are allowing additional qualification.
             If ARG is `const int' and PARM is just `T' that's OK;
             If ARG is `const int' and PARM is just `T' that's OK;
             that binds `const int' to `T'.  */
             that binds `const int' to `T'.  */
          if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
          if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
                                         arg, parm))
                                         arg, parm))
            return 1;
            return 1;
 
 
          /* Consider the case where ARG is `const volatile int' and
          /* Consider the case where ARG is `const volatile int' and
             PARM is `const T'.  Then, T should be `volatile int'.  */
             PARM is `const T'.  Then, T should be `volatile int'.  */
          arg = cp_build_qualified_type_real
          arg = cp_build_qualified_type_real
            (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
            (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
          if (arg == error_mark_node)
          if (arg == error_mark_node)
            return 1;
            return 1;
 
 
          /* Simple cases: Value already set, does match or doesn't.  */
          /* Simple cases: Value already set, does match or doesn't.  */
          if (targ != NULL_TREE && same_type_p (targ, arg))
          if (targ != NULL_TREE && same_type_p (targ, arg))
            return 0;
            return 0;
          else if (targ)
          else if (targ)
            return 1;
            return 1;
 
 
          /* Make sure that ARG is not a variable-sized array.  (Note
          /* Make sure that ARG is not a variable-sized array.  (Note
             that were talking about variable-sized arrays (like
             that were talking about variable-sized arrays (like
             `int[n]'), rather than arrays of unknown size (like
             `int[n]'), rather than arrays of unknown size (like
             `int[]').)  We'll get very confused by such a type since
             `int[]').)  We'll get very confused by such a type since
             the bound of the array will not be computable in an
             the bound of the array will not be computable in an
             instantiation.  Besides, such types are not allowed in
             instantiation.  Besides, such types are not allowed in
             ISO C++, so we can do as we please here.  */
             ISO C++, so we can do as we please here.  */
          if (variably_modified_type_p (arg, NULL_TREE))
          if (variably_modified_type_p (arg, NULL_TREE))
            return 1;
            return 1;
 
 
          /* Strip typedefs as in convert_template_argument.  */
          /* Strip typedefs as in convert_template_argument.  */
          arg = strip_typedefs (arg);
          arg = strip_typedefs (arg);
        }
        }
 
 
      /* If ARG is a parameter pack or an expansion, we cannot unify
      /* If ARG is a parameter pack or an expansion, we cannot unify
         against it unless PARM is also a parameter pack.  */
         against it unless PARM is also a parameter pack.  */
      if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
      if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
          && !template_parameter_pack_p (parm))
          && !template_parameter_pack_p (parm))
        return 1;
        return 1;
 
 
      /* If the argument deduction results is a METHOD_TYPE,
      /* If the argument deduction results is a METHOD_TYPE,
         then there is a problem.
         then there is a problem.
         METHOD_TYPE doesn't map to any real C++ type the result of
         METHOD_TYPE doesn't map to any real C++ type the result of
         the deduction can not be of that type.  */
         the deduction can not be of that type.  */
      if (TREE_CODE (arg) == METHOD_TYPE)
      if (TREE_CODE (arg) == METHOD_TYPE)
        return 1;
        return 1;
 
 
      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
      return 0;
      return 0;
 
 
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
      if (tparm == error_mark_node)
      if (tparm == error_mark_node)
        return 1;
        return 1;
 
 
      if (TEMPLATE_PARM_LEVEL (parm)
      if (TEMPLATE_PARM_LEVEL (parm)
          != template_decl_level (tparm))
          != template_decl_level (tparm))
        /* The PARM is not one we're trying to unify.  Just check
        /* The PARM is not one we're trying to unify.  Just check
           to see if it matches ARG.  */
           to see if it matches ARG.  */
        return !(TREE_CODE (arg) == TREE_CODE (parm)
        return !(TREE_CODE (arg) == TREE_CODE (parm)
                 && cp_tree_equal (parm, arg));
                 && cp_tree_equal (parm, arg));
 
 
      idx = TEMPLATE_PARM_IDX (parm);
      idx = TEMPLATE_PARM_IDX (parm);
      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
 
 
      if (targ)
      if (targ)
        return !cp_tree_equal (targ, arg);
        return !cp_tree_equal (targ, arg);
 
 
      /* [temp.deduct.type] If, in the declaration of a function template
      /* [temp.deduct.type] If, in the declaration of a function template
         with a non-type template-parameter, the non-type
         with a non-type template-parameter, the non-type
         template-parameter is used in an expression in the function
         template-parameter is used in an expression in the function
         parameter-list and, if the corresponding template-argument is
         parameter-list and, if the corresponding template-argument is
         deduced, the template-argument type shall match the type of the
         deduced, the template-argument type shall match the type of the
         template-parameter exactly, except that a template-argument
         template-parameter exactly, except that a template-argument
         deduced from an array bound may be of any integral type.
         deduced from an array bound may be of any integral type.
         The non-type parameter might use already deduced type parameters.  */
         The non-type parameter might use already deduced type parameters.  */
      tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
      tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
      if (!TREE_TYPE (arg))
      if (!TREE_TYPE (arg))
        /* Template-parameter dependent expression.  Just accept it for now.
        /* Template-parameter dependent expression.  Just accept it for now.
           It will later be processed in convert_template_argument.  */
           It will later be processed in convert_template_argument.  */
        ;
        ;
      else if (same_type_p (TREE_TYPE (arg), tparm))
      else if (same_type_p (TREE_TYPE (arg), tparm))
        /* OK */;
        /* OK */;
      else if ((strict & UNIFY_ALLOW_INTEGER)
      else if ((strict & UNIFY_ALLOW_INTEGER)
               && (TREE_CODE (tparm) == INTEGER_TYPE
               && (TREE_CODE (tparm) == INTEGER_TYPE
                   || TREE_CODE (tparm) == BOOLEAN_TYPE))
                   || TREE_CODE (tparm) == BOOLEAN_TYPE))
        /* Convert the ARG to the type of PARM; the deduced non-type
        /* Convert the ARG to the type of PARM; the deduced non-type
           template argument must exactly match the types of the
           template argument must exactly match the types of the
           corresponding parameter.  */
           corresponding parameter.  */
        arg = fold (build_nop (tparm, arg));
        arg = fold (build_nop (tparm, arg));
      else if (uses_template_parms (tparm))
      else if (uses_template_parms (tparm))
        /* We haven't deduced the type of this parameter yet.  Try again
        /* We haven't deduced the type of this parameter yet.  Try again
           later.  */
           later.  */
        return 0;
        return 0;
      else
      else
        return 1;
        return 1;
 
 
      /* If ARG is a parameter pack or an expansion, we cannot unify
      /* If ARG is a parameter pack or an expansion, we cannot unify
         against it unless PARM is also a parameter pack.  */
         against it unless PARM is also a parameter pack.  */
      if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
      if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
          && !TEMPLATE_PARM_PARAMETER_PACK (parm))
          && !TEMPLATE_PARM_PARAMETER_PACK (parm))
        return 1;
        return 1;
 
 
      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
      return 0;
      return 0;
 
 
    case PTRMEM_CST:
    case PTRMEM_CST:
     {
     {
        /* A pointer-to-member constant can be unified only with
        /* A pointer-to-member constant can be unified only with
         another constant.  */
         another constant.  */
      if (TREE_CODE (arg) != PTRMEM_CST)
      if (TREE_CODE (arg) != PTRMEM_CST)
        return 1;
        return 1;
 
 
      /* Just unify the class member. It would be useless (and possibly
      /* Just unify the class member. It would be useless (and possibly
         wrong, depending on the strict flags) to unify also
         wrong, depending on the strict flags) to unify also
         PTRMEM_CST_CLASS, because we want to be sure that both parm and
         PTRMEM_CST_CLASS, because we want to be sure that both parm and
         arg refer to the same variable, even if through different
         arg refer to the same variable, even if through different
         classes. For instance:
         classes. For instance:
 
 
         struct A { int x; };
         struct A { int x; };
         struct B : A { };
         struct B : A { };
 
 
         Unification of &A::x and &B::x must succeed.  */
         Unification of &A::x and &B::x must succeed.  */
      return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
      return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
                    PTRMEM_CST_MEMBER (arg), strict);
                    PTRMEM_CST_MEMBER (arg), strict);
     }
     }
 
 
    case POINTER_TYPE:
    case POINTER_TYPE:
      {
      {
        if (TREE_CODE (arg) != POINTER_TYPE)
        if (TREE_CODE (arg) != POINTER_TYPE)
          return 1;
          return 1;
 
 
        /* [temp.deduct.call]
        /* [temp.deduct.call]
 
 
           A can be another pointer or pointer to member type that can
           A can be another pointer or pointer to member type that can
           be converted to the deduced A via a qualification
           be converted to the deduced A via a qualification
           conversion (_conv.qual_).
           conversion (_conv.qual_).
 
 
           We pass down STRICT here rather than UNIFY_ALLOW_NONE.
           We pass down STRICT here rather than UNIFY_ALLOW_NONE.
           This will allow for additional cv-qualification of the
           This will allow for additional cv-qualification of the
           pointed-to types if appropriate.  */
           pointed-to types if appropriate.  */
 
 
        if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
        if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
          /* The derived-to-base conversion only persists through one
          /* The derived-to-base conversion only persists through one
             level of pointers.  */
             level of pointers.  */
          strict |= (strict_in & UNIFY_ALLOW_DERIVED);
          strict |= (strict_in & UNIFY_ALLOW_DERIVED);
 
 
        return unify (tparms, targs, TREE_TYPE (parm),
        return unify (tparms, targs, TREE_TYPE (parm),
                      TREE_TYPE (arg), strict);
                      TREE_TYPE (arg), strict);
      }
      }
 
 
    case REFERENCE_TYPE:
    case REFERENCE_TYPE:
      if (TREE_CODE (arg) != REFERENCE_TYPE)
      if (TREE_CODE (arg) != REFERENCE_TYPE)
        return 1;
        return 1;
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
                    strict & UNIFY_ALLOW_MORE_CV_QUAL);
                    strict & UNIFY_ALLOW_MORE_CV_QUAL);
 
 
    case ARRAY_TYPE:
    case ARRAY_TYPE:
      if (TREE_CODE (arg) != ARRAY_TYPE)
      if (TREE_CODE (arg) != ARRAY_TYPE)
        return 1;
        return 1;
      if ((TYPE_DOMAIN (parm) == NULL_TREE)
      if ((TYPE_DOMAIN (parm) == NULL_TREE)
          != (TYPE_DOMAIN (arg) == NULL_TREE))
          != (TYPE_DOMAIN (arg) == NULL_TREE))
        return 1;
        return 1;
      if (TYPE_DOMAIN (parm) != NULL_TREE)
      if (TYPE_DOMAIN (parm) != NULL_TREE)
        {
        {
          tree parm_max;
          tree parm_max;
          tree arg_max;
          tree arg_max;
          bool parm_cst;
          bool parm_cst;
          bool arg_cst;
          bool arg_cst;
 
 
          /* Our representation of array types uses "N - 1" as the
          /* Our representation of array types uses "N - 1" as the
             TYPE_MAX_VALUE for an array with "N" elements, if "N" is
             TYPE_MAX_VALUE for an array with "N" elements, if "N" is
             not an integer constant.  We cannot unify arbitrarily
             not an integer constant.  We cannot unify arbitrarily
             complex expressions, so we eliminate the MINUS_EXPRs
             complex expressions, so we eliminate the MINUS_EXPRs
             here.  */
             here.  */
          parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
          parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
          parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
          parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
          if (!parm_cst)
          if (!parm_cst)
            {
            {
              gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
              gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
              parm_max = TREE_OPERAND (parm_max, 0);
              parm_max = TREE_OPERAND (parm_max, 0);
            }
            }
          arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
          arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
          arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
          arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
          if (!arg_cst)
          if (!arg_cst)
            {
            {
              /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
              /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
                 trying to unify the type of a variable with the type
                 trying to unify the type of a variable with the type
                 of a template parameter.  For example:
                 of a template parameter.  For example:
 
 
                   template <unsigned int N>
                   template <unsigned int N>
                   void f (char (&) [N]);
                   void f (char (&) [N]);
                   int g();
                   int g();
                   void h(int i) {
                   void h(int i) {
                     char a[g(i)];
                     char a[g(i)];
                     f(a);
                     f(a);
                   }
                   }
 
 
                Here, the type of the ARG will be "int [g(i)]", and
                Here, the type of the ARG will be "int [g(i)]", and
                may be a SAVE_EXPR, etc.  */
                may be a SAVE_EXPR, etc.  */
              if (TREE_CODE (arg_max) != MINUS_EXPR)
              if (TREE_CODE (arg_max) != MINUS_EXPR)
                return 1;
                return 1;
              arg_max = TREE_OPERAND (arg_max, 0);
              arg_max = TREE_OPERAND (arg_max, 0);
            }
            }
 
 
          /* If only one of the bounds used a MINUS_EXPR, compensate
          /* If only one of the bounds used a MINUS_EXPR, compensate
             by adding one to the other bound.  */
             by adding one to the other bound.  */
          if (parm_cst && !arg_cst)
          if (parm_cst && !arg_cst)
            parm_max = fold_build2_loc (input_location, PLUS_EXPR,
            parm_max = fold_build2_loc (input_location, PLUS_EXPR,
                                    integer_type_node,
                                    integer_type_node,
                                    parm_max,
                                    parm_max,
                                    integer_one_node);
                                    integer_one_node);
          else if (arg_cst && !parm_cst)
          else if (arg_cst && !parm_cst)
            arg_max = fold_build2_loc (input_location, PLUS_EXPR,
            arg_max = fold_build2_loc (input_location, PLUS_EXPR,
                                   integer_type_node,
                                   integer_type_node,
                                   arg_max,
                                   arg_max,
                                   integer_one_node);
                                   integer_one_node);
 
 
          if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
          if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
            return 1;
            return 1;
        }
        }
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
                    strict & UNIFY_ALLOW_MORE_CV_QUAL);
                    strict & UNIFY_ALLOW_MORE_CV_QUAL);
 
 
    case REAL_TYPE:
    case REAL_TYPE:
    case COMPLEX_TYPE:
    case COMPLEX_TYPE:
    case VECTOR_TYPE:
    case VECTOR_TYPE:
    case INTEGER_TYPE:
    case INTEGER_TYPE:
    case BOOLEAN_TYPE:
    case BOOLEAN_TYPE:
    case ENUMERAL_TYPE:
    case ENUMERAL_TYPE:
    case VOID_TYPE:
    case VOID_TYPE:
      if (TREE_CODE (arg) != TREE_CODE (parm))
      if (TREE_CODE (arg) != TREE_CODE (parm))
        return 1;
        return 1;
 
 
      /* We have already checked cv-qualification at the top of the
      /* We have already checked cv-qualification at the top of the
         function.  */
         function.  */
      if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
      if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
        return 1;
        return 1;
 
 
      /* As far as unification is concerned, this wins.  Later checks
      /* As far as unification is concerned, this wins.  Later checks
         will invalidate it if necessary.  */
         will invalidate it if necessary.  */
      return 0;
      return 0;
 
 
      /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
      /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
      /* Type INTEGER_CST can come from ordinary constant template args.  */
      /* Type INTEGER_CST can come from ordinary constant template args.  */
    case INTEGER_CST:
    case INTEGER_CST:
      while (TREE_CODE (arg) == NOP_EXPR)
      while (TREE_CODE (arg) == NOP_EXPR)
        arg = TREE_OPERAND (arg, 0);
        arg = TREE_OPERAND (arg, 0);
 
 
      if (TREE_CODE (arg) != INTEGER_CST)
      if (TREE_CODE (arg) != INTEGER_CST)
        return 1;
        return 1;
      return !tree_int_cst_equal (parm, arg);
      return !tree_int_cst_equal (parm, arg);
 
 
    case TREE_VEC:
    case TREE_VEC:
      {
      {
        int i;
        int i;
        if (TREE_CODE (arg) != TREE_VEC)
        if (TREE_CODE (arg) != TREE_VEC)
          return 1;
          return 1;
        if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
        if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
          return 1;
          return 1;
        for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
        for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
          if (unify (tparms, targs,
          if (unify (tparms, targs,
                     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
                     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
                     UNIFY_ALLOW_NONE))
                     UNIFY_ALLOW_NONE))
            return 1;
            return 1;
        return 0;
        return 0;
      }
      }
 
 
    case RECORD_TYPE:
    case RECORD_TYPE:
    case UNION_TYPE:
    case UNION_TYPE:
      if (TREE_CODE (arg) != TREE_CODE (parm))
      if (TREE_CODE (arg) != TREE_CODE (parm))
        return 1;
        return 1;
 
 
      if (TYPE_PTRMEMFUNC_P (parm))
      if (TYPE_PTRMEMFUNC_P (parm))
        {
        {
          if (!TYPE_PTRMEMFUNC_P (arg))
          if (!TYPE_PTRMEMFUNC_P (arg))
            return 1;
            return 1;
 
 
          return unify (tparms, targs,
          return unify (tparms, targs,
                        TYPE_PTRMEMFUNC_FN_TYPE (parm),
                        TYPE_PTRMEMFUNC_FN_TYPE (parm),
                        TYPE_PTRMEMFUNC_FN_TYPE (arg),
                        TYPE_PTRMEMFUNC_FN_TYPE (arg),
                        strict);
                        strict);
        }
        }
 
 
      if (CLASSTYPE_TEMPLATE_INFO (parm))
      if (CLASSTYPE_TEMPLATE_INFO (parm))
        {
        {
          tree t = NULL_TREE;
          tree t = NULL_TREE;
 
 
          if (strict_in & UNIFY_ALLOW_DERIVED)
          if (strict_in & UNIFY_ALLOW_DERIVED)
            {
            {
              /* First, we try to unify the PARM and ARG directly.  */
              /* First, we try to unify the PARM and ARG directly.  */
              t = try_class_unification (tparms, targs,
              t = try_class_unification (tparms, targs,
                                         parm, arg);
                                         parm, arg);
 
 
              if (!t)
              if (!t)
                {
                {
                  /* Fallback to the special case allowed in
                  /* Fallback to the special case allowed in
                     [temp.deduct.call]:
                     [temp.deduct.call]:
 
 
                       If P is a class, and P has the form
                       If P is a class, and P has the form
                       template-id, then A can be a derived class of
                       template-id, then A can be a derived class of
                       the deduced A.  Likewise, if P is a pointer to
                       the deduced A.  Likewise, if P is a pointer to
                       a class of the form template-id, A can be a
                       a class of the form template-id, A can be a
                       pointer to a derived class pointed to by the
                       pointer to a derived class pointed to by the
                       deduced A.  */
                       deduced A.  */
                  t = get_template_base (tparms, targs, parm, arg);
                  t = get_template_base (tparms, targs, parm, arg);
 
 
                  if (!t)
                  if (!t)
                    return 1;
                    return 1;
                }
                }
            }
            }
          else if (CLASSTYPE_TEMPLATE_INFO (arg)
          else if (CLASSTYPE_TEMPLATE_INFO (arg)
                   && (CLASSTYPE_TI_TEMPLATE (parm)
                   && (CLASSTYPE_TI_TEMPLATE (parm)
                       == CLASSTYPE_TI_TEMPLATE (arg)))
                       == CLASSTYPE_TI_TEMPLATE (arg)))
            /* Perhaps PARM is something like S<U> and ARG is S<int>.
            /* Perhaps PARM is something like S<U> and ARG is S<int>.
               Then, we should unify `int' and `U'.  */
               Then, we should unify `int' and `U'.  */
            t = arg;
            t = arg;
          else
          else
            /* There's no chance of unification succeeding.  */
            /* There's no chance of unification succeeding.  */
            return 1;
            return 1;
 
 
          return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
          return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
                        CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
                        CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
        }
        }
      else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
      else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
        return 1;
        return 1;
      return 0;
      return 0;
 
 
    case METHOD_TYPE:
    case METHOD_TYPE:
    case FUNCTION_TYPE:
    case FUNCTION_TYPE:
      {
      {
        unsigned int nargs;
        unsigned int nargs;
        tree *args;
        tree *args;
        tree a;
        tree a;
        unsigned int i;
        unsigned int i;
 
 
        if (TREE_CODE (arg) != TREE_CODE (parm))
        if (TREE_CODE (arg) != TREE_CODE (parm))
          return 1;
          return 1;
 
 
        /* CV qualifications for methods can never be deduced, they must
        /* CV qualifications for methods can never be deduced, they must
           match exactly.  We need to check them explicitly here,
           match exactly.  We need to check them explicitly here,
           because type_unification_real treats them as any other
           because type_unification_real treats them as any other
           cv-qualified parameter.  */
           cv-qualified parameter.  */
        if (TREE_CODE (parm) == METHOD_TYPE
        if (TREE_CODE (parm) == METHOD_TYPE
            && (!check_cv_quals_for_unify
            && (!check_cv_quals_for_unify
                (UNIFY_ALLOW_NONE,
                (UNIFY_ALLOW_NONE,
                 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
                 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
                 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
                 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
          return 1;
          return 1;
 
 
        if (unify (tparms, targs, TREE_TYPE (parm),
        if (unify (tparms, targs, TREE_TYPE (parm),
                   TREE_TYPE (arg), UNIFY_ALLOW_NONE))
                   TREE_TYPE (arg), UNIFY_ALLOW_NONE))
          return 1;
          return 1;
 
 
        nargs = list_length (TYPE_ARG_TYPES (arg));
        nargs = list_length (TYPE_ARG_TYPES (arg));
        args = XALLOCAVEC (tree, nargs);
        args = XALLOCAVEC (tree, nargs);
        for (a = TYPE_ARG_TYPES (arg), i = 0;
        for (a = TYPE_ARG_TYPES (arg), i = 0;
             a != NULL_TREE && a != void_list_node;
             a != NULL_TREE && a != void_list_node;
             a = TREE_CHAIN (a), ++i)
             a = TREE_CHAIN (a), ++i)
          args[i] = TREE_VALUE (a);
          args[i] = TREE_VALUE (a);
        nargs = i;
        nargs = i;
 
 
        return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
        return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
                                      args, nargs, 1, DEDUCE_EXACT,
                                      args, nargs, 1, DEDUCE_EXACT,
                                      LOOKUP_NORMAL);
                                      LOOKUP_NORMAL);
      }
      }
 
 
    case OFFSET_TYPE:
    case OFFSET_TYPE:
      /* Unify a pointer to member with a pointer to member function, which
      /* Unify a pointer to member with a pointer to member function, which
         deduces the type of the member as a function type. */
         deduces the type of the member as a function type. */
      if (TYPE_PTRMEMFUNC_P (arg))
      if (TYPE_PTRMEMFUNC_P (arg))
        {
        {
          tree method_type;
          tree method_type;
          tree fntype;
          tree fntype;
          cp_cv_quals cv_quals;
          cp_cv_quals cv_quals;
 
 
          /* Check top-level cv qualifiers */
          /* Check top-level cv qualifiers */
          if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
          if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
            return 1;
            return 1;
 
 
          if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
          if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
                     TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
                     TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
            return 1;
            return 1;
 
 
          /* Determine the type of the function we are unifying against. */
          /* Determine the type of the function we are unifying against. */
          method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
          method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
          fntype =
          fntype =
            build_function_type (TREE_TYPE (method_type),
            build_function_type (TREE_TYPE (method_type),
                                 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
                                 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
 
 
          /* Extract the cv-qualifiers of the member function from the
          /* Extract the cv-qualifiers of the member function from the
             implicit object parameter and place them on the function
             implicit object parameter and place them on the function
             type to be restored later. */
             type to be restored later. */
          cv_quals =
          cv_quals =
            cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
            cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
          fntype = build_qualified_type (fntype, cv_quals);
          fntype = build_qualified_type (fntype, cv_quals);
          return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
          return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
        }
        }
 
 
      if (TREE_CODE (arg) != OFFSET_TYPE)
      if (TREE_CODE (arg) != OFFSET_TYPE)
        return 1;
        return 1;
      if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
      if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
                 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
                 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
        return 1;
        return 1;
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
                    strict);
                    strict);
 
 
    case CONST_DECL:
    case CONST_DECL:
      if (DECL_TEMPLATE_PARM_P (parm))
      if (DECL_TEMPLATE_PARM_P (parm))
        return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
        return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
      if (arg != integral_constant_value (parm))
      if (arg != integral_constant_value (parm))
        return 1;
        return 1;
      return 0;
      return 0;
 
 
    case FIELD_DECL:
    case FIELD_DECL:
    case TEMPLATE_DECL:
    case TEMPLATE_DECL:
      /* Matched cases are handled by the ARG == PARM test above.  */
      /* Matched cases are handled by the ARG == PARM test above.  */
      return 1;
      return 1;
 
 
    case VAR_DECL:
    case VAR_DECL:
      /* A non-type template parameter that is a variable should be a
      /* A non-type template parameter that is a variable should be a
         an integral constant, in which case, it whould have been
         an integral constant, in which case, it whould have been
         folded into its (constant) value. So we should not be getting
         folded into its (constant) value. So we should not be getting
         a variable here.  */
         a variable here.  */
      gcc_unreachable ();
      gcc_unreachable ();
 
 
    case TYPE_ARGUMENT_PACK:
    case TYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      {
      {
        tree packed_parms = ARGUMENT_PACK_ARGS (parm);
        tree packed_parms = ARGUMENT_PACK_ARGS (parm);
        tree packed_args = ARGUMENT_PACK_ARGS (arg);
        tree packed_args = ARGUMENT_PACK_ARGS (arg);
        int i, len = TREE_VEC_LENGTH (packed_parms);
        int i, len = TREE_VEC_LENGTH (packed_parms);
        int argslen = TREE_VEC_LENGTH (packed_args);
        int argslen = TREE_VEC_LENGTH (packed_args);
        int parm_variadic_p = 0;
        int parm_variadic_p = 0;
 
 
        for (i = 0; i < len; ++i)
        for (i = 0; i < len; ++i)
          {
          {
            if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
            if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
              {
              {
                if (i == len - 1)
                if (i == len - 1)
                  /* We can unify against something with a trailing
                  /* We can unify against something with a trailing
                     parameter pack.  */
                     parameter pack.  */
                  parm_variadic_p = 1;
                  parm_variadic_p = 1;
                else
                else
                  /* Since there is something following the pack
                  /* Since there is something following the pack
                     expansion, we cannot unify this template argument
                     expansion, we cannot unify this template argument
                     list.  */
                     list.  */
                  return 0;
                  return 0;
              }
              }
          }
          }
 
 
 
 
        /* If we don't have enough arguments to satisfy the parameters
        /* If we don't have enough arguments to satisfy the parameters
           (not counting the pack expression at the end), or we have
           (not counting the pack expression at the end), or we have
           too many arguments for a parameter list that doesn't end in
           too many arguments for a parameter list that doesn't end in
           a pack expression, we can't unify.  */
           a pack expression, we can't unify.  */
        if (argslen < (len - parm_variadic_p)
        if (argslen < (len - parm_variadic_p)
            || (argslen > len && !parm_variadic_p))
            || (argslen > len && !parm_variadic_p))
          return 1;
          return 1;
 
 
        /* Unify all of the parameters that precede the (optional)
        /* Unify all of the parameters that precede the (optional)
           pack expression.  */
           pack expression.  */
        for (i = 0; i < len - parm_variadic_p; ++i)
        for (i = 0; i < len - parm_variadic_p; ++i)
          {
          {
            if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
            if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
                       TREE_VEC_ELT (packed_args, i), strict))
                       TREE_VEC_ELT (packed_args, i), strict))
              return 1;
              return 1;
          }
          }
 
 
        if (parm_variadic_p)
        if (parm_variadic_p)
          return unify_pack_expansion (tparms, targs,
          return unify_pack_expansion (tparms, targs,
                                       packed_parms, packed_args,
                                       packed_parms, packed_args,
                                       strict, /*call_args_p=*/false,
                                       strict, /*call_args_p=*/false,
                                       /*subr=*/false);
                                       /*subr=*/false);
        return 0;
        return 0;
      }
      }
 
 
      break;
      break;
 
 
    case TYPEOF_TYPE:
    case TYPEOF_TYPE:
    case DECLTYPE_TYPE:
    case DECLTYPE_TYPE:
      /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
      /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
         nodes.  */
         nodes.  */
      return 0;
      return 0;
 
 
    case ERROR_MARK:
    case ERROR_MARK:
      /* Unification fails if we hit an error node.  */
      /* Unification fails if we hit an error node.  */
      return 1;
      return 1;
 
 
    default:
    default:
      gcc_assert (EXPR_P (parm));
      gcc_assert (EXPR_P (parm));
 
 
      /* We must be looking at an expression.  This can happen with
      /* We must be looking at an expression.  This can happen with
         something like:
         something like:
 
 
           template <int I>
           template <int I>
           void foo(S<I>, S<I + 2>);
           void foo(S<I>, S<I + 2>);
 
 
         This is a "nondeduced context":
         This is a "nondeduced context":
 
 
           [deduct.type]
           [deduct.type]
 
 
           The nondeduced contexts are:
           The nondeduced contexts are:
 
 
           --A type that is a template-id in which one or more of
           --A type that is a template-id in which one or more of
             the template-arguments is an expression that references
             the template-arguments is an expression that references
             a template-parameter.
             a template-parameter.
 
 
         In these cases, we assume deduction succeeded, but don't
         In these cases, we assume deduction succeeded, but don't
         actually infer any unifications.  */
         actually infer any unifications.  */
 
 
      if (!uses_template_parms (parm)
      if (!uses_template_parms (parm)
          && !template_args_equal (parm, arg))
          && !template_args_equal (parm, arg))
        return 1;
        return 1;
      else
      else
        return 0;
        return 0;
    }
    }
}
}


/* Note that DECL can be defined in this translation unit, if
/* Note that DECL can be defined in this translation unit, if
   required.  */
   required.  */
 
 
static void
static void
mark_definable (tree decl)
mark_definable (tree decl)
{
{
  tree clone;
  tree clone;
  DECL_NOT_REALLY_EXTERN (decl) = 1;
  DECL_NOT_REALLY_EXTERN (decl) = 1;
  FOR_EACH_CLONE (clone, decl)
  FOR_EACH_CLONE (clone, decl)
    DECL_NOT_REALLY_EXTERN (clone) = 1;
    DECL_NOT_REALLY_EXTERN (clone) = 1;
}
}
 
 
/* Called if RESULT is explicitly instantiated, or is a member of an
/* Called if RESULT is explicitly instantiated, or is a member of an
   explicitly instantiated class.  */
   explicitly instantiated class.  */
 
 
void
void
mark_decl_instantiated (tree result, int extern_p)
mark_decl_instantiated (tree result, int extern_p)
{
{
  SET_DECL_EXPLICIT_INSTANTIATION (result);
  SET_DECL_EXPLICIT_INSTANTIATION (result);
 
 
  /* If this entity has already been written out, it's too late to
  /* If this entity has already been written out, it's too late to
     make any modifications.  */
     make any modifications.  */
  if (TREE_ASM_WRITTEN (result))
  if (TREE_ASM_WRITTEN (result))
    return;
    return;
 
 
  if (TREE_CODE (result) != FUNCTION_DECL)
  if (TREE_CODE (result) != FUNCTION_DECL)
    /* The TREE_PUBLIC flag for function declarations will have been
    /* The TREE_PUBLIC flag for function declarations will have been
       set correctly by tsubst.  */
       set correctly by tsubst.  */
    TREE_PUBLIC (result) = 1;
    TREE_PUBLIC (result) = 1;
 
 
  /* This might have been set by an earlier implicit instantiation.  */
  /* This might have been set by an earlier implicit instantiation.  */
  DECL_COMDAT (result) = 0;
  DECL_COMDAT (result) = 0;
 
 
  if (extern_p)
  if (extern_p)
    DECL_NOT_REALLY_EXTERN (result) = 0;
    DECL_NOT_REALLY_EXTERN (result) = 0;
  else
  else
    {
    {
      mark_definable (result);
      mark_definable (result);
      /* Always make artificials weak.  */
      /* Always make artificials weak.  */
      if (DECL_ARTIFICIAL (result) && flag_weak)
      if (DECL_ARTIFICIAL (result) && flag_weak)
        comdat_linkage (result);
        comdat_linkage (result);
      /* For WIN32 we also want to put explicit instantiations in
      /* For WIN32 we also want to put explicit instantiations in
         linkonce sections.  */
         linkonce sections.  */
      else if (TREE_PUBLIC (result))
      else if (TREE_PUBLIC (result))
        maybe_make_one_only (result);
        maybe_make_one_only (result);
    }
    }
 
 
  /* If EXTERN_P, then this function will not be emitted -- unless
  /* If EXTERN_P, then this function will not be emitted -- unless
     followed by an explicit instantiation, at which point its linkage
     followed by an explicit instantiation, at which point its linkage
     will be adjusted.  If !EXTERN_P, then this function will be
     will be adjusted.  If !EXTERN_P, then this function will be
     emitted here.  In neither circumstance do we want
     emitted here.  In neither circumstance do we want
     import_export_decl to adjust the linkage.  */
     import_export_decl to adjust the linkage.  */
  DECL_INTERFACE_KNOWN (result) = 1;
  DECL_INTERFACE_KNOWN (result) = 1;
}
}
 
 
/* Subroutine of more_specialized_fn: check whether TARGS is missing any
/* Subroutine of more_specialized_fn: check whether TARGS is missing any
   important template arguments.  If any are missing, we check whether
   important template arguments.  If any are missing, we check whether
   they're important by using error_mark_node for substituting into any
   they're important by using error_mark_node for substituting into any
   args that were used for partial ordering (the ones between ARGS and END)
   args that were used for partial ordering (the ones between ARGS and END)
   and seeing if it bubbles up.  */
   and seeing if it bubbles up.  */
 
 
static bool
static bool
check_undeduced_parms (tree targs, tree args, tree end)
check_undeduced_parms (tree targs, tree args, tree end)
{
{
  bool found = false;
  bool found = false;
  int i;
  int i;
  for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
  for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
    if (TREE_VEC_ELT (targs, i) == NULL_TREE)
    if (TREE_VEC_ELT (targs, i) == NULL_TREE)
      {
      {
        found = true;
        found = true;
        TREE_VEC_ELT (targs, i) = error_mark_node;
        TREE_VEC_ELT (targs, i) = error_mark_node;
      }
      }
  if (found)
  if (found)
    {
    {
      for (; args != end; args = TREE_CHAIN (args))
      for (; args != end; args = TREE_CHAIN (args))
        {
        {
          tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
          tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
          if (substed == error_mark_node)
          if (substed == error_mark_node)
            return true;
            return true;
        }
        }
    }
    }
  return false;
  return false;
}
}
 
 
/* Given two function templates PAT1 and PAT2, return:
/* Given two function templates PAT1 and PAT2, return:
 
 
   1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   -1 if PAT2 is more specialized than PAT1.
   -1 if PAT2 is more specialized than PAT1.
   0 if neither is more specialized.
   0 if neither is more specialized.
 
 
   LEN indicates the number of parameters we should consider
   LEN indicates the number of parameters we should consider
   (defaulted parameters should not be considered).
   (defaulted parameters should not be considered).
 
 
   The 1998 std underspecified function template partial ordering, and
   The 1998 std underspecified function template partial ordering, and
   DR214 addresses the issue.  We take pairs of arguments, one from
   DR214 addresses the issue.  We take pairs of arguments, one from
   each of the templates, and deduce them against each other.  One of
   each of the templates, and deduce them against each other.  One of
   the templates will be more specialized if all the *other*
   the templates will be more specialized if all the *other*
   template's arguments deduce against its arguments and at least one
   template's arguments deduce against its arguments and at least one
   of its arguments *does* *not* deduce against the other template's
   of its arguments *does* *not* deduce against the other template's
   corresponding argument.  Deduction is done as for class templates.
   corresponding argument.  Deduction is done as for class templates.
   The arguments used in deduction have reference and top level cv
   The arguments used in deduction have reference and top level cv
   qualifiers removed.  Iff both arguments were originally reference
   qualifiers removed.  Iff both arguments were originally reference
   types *and* deduction succeeds in both directions, the template
   types *and* deduction succeeds in both directions, the template
   with the more cv-qualified argument wins for that pairing (if
   with the more cv-qualified argument wins for that pairing (if
   neither is more cv-qualified, they both are equal).  Unlike regular
   neither is more cv-qualified, they both are equal).  Unlike regular
   deduction, after all the arguments have been deduced in this way,
   deduction, after all the arguments have been deduced in this way,
   we do *not* verify the deduced template argument values can be
   we do *not* verify the deduced template argument values can be
   substituted into non-deduced contexts.
   substituted into non-deduced contexts.
 
 
   The logic can be a bit confusing here, because we look at deduce1 and
   The logic can be a bit confusing here, because we look at deduce1 and
   targs1 to see if pat2 is at least as specialized, and vice versa; if we
   targs1 to see if pat2 is at least as specialized, and vice versa; if we
   can find template arguments for pat1 to make arg1 look like arg2, that
   can find template arguments for pat1 to make arg1 look like arg2, that
   means that arg2 is at least as specialized as arg1.  */
   means that arg2 is at least as specialized as arg1.  */
 
 
int
int
more_specialized_fn (tree pat1, tree pat2, int len)
more_specialized_fn (tree pat1, tree pat2, int len)
{
{
  tree decl1 = DECL_TEMPLATE_RESULT (pat1);
  tree decl1 = DECL_TEMPLATE_RESULT (pat1);
  tree decl2 = DECL_TEMPLATE_RESULT (pat2);
  tree decl2 = DECL_TEMPLATE_RESULT (pat2);
  tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
  tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
  tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
  tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
  tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
  tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
  tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
  tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
  tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
  tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
  tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
  tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
  tree origs1, origs2;
  tree origs1, origs2;
  bool lose1 = false;
  bool lose1 = false;
  bool lose2 = false;
  bool lose2 = false;
 
 
  /* Remove the this parameter from non-static member functions.  If
  /* Remove the this parameter from non-static member functions.  If
     one is a non-static member function and the other is not a static
     one is a non-static member function and the other is not a static
     member function, remove the first parameter from that function
     member function, remove the first parameter from that function
     also.  This situation occurs for operator functions where we
     also.  This situation occurs for operator functions where we
     locate both a member function (with this pointer) and non-member
     locate both a member function (with this pointer) and non-member
     operator (with explicit first operand).  */
     operator (with explicit first operand).  */
  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
    {
    {
      len--; /* LEN is the number of significant arguments for DECL1 */
      len--; /* LEN is the number of significant arguments for DECL1 */
      args1 = TREE_CHAIN (args1);
      args1 = TREE_CHAIN (args1);
      if (!DECL_STATIC_FUNCTION_P (decl2))
      if (!DECL_STATIC_FUNCTION_P (decl2))
        args2 = TREE_CHAIN (args2);
        args2 = TREE_CHAIN (args2);
    }
    }
  else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
  else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
    {
    {
      args2 = TREE_CHAIN (args2);
      args2 = TREE_CHAIN (args2);
      if (!DECL_STATIC_FUNCTION_P (decl1))
      if (!DECL_STATIC_FUNCTION_P (decl1))
        {
        {
          len--;
          len--;
          args1 = TREE_CHAIN (args1);
          args1 = TREE_CHAIN (args1);
        }
        }
    }
    }
 
 
  /* If only one is a conversion operator, they are unordered.  */
  /* If only one is a conversion operator, they are unordered.  */
  if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
  if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
    return 0;
    return 0;
 
 
  /* Consider the return type for a conversion function */
  /* Consider the return type for a conversion function */
  if (DECL_CONV_FN_P (decl1))
  if (DECL_CONV_FN_P (decl1))
    {
    {
      args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
      args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
      args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
      args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
      len++;
      len++;
    }
    }
 
 
  processing_template_decl++;
  processing_template_decl++;
 
 
  origs1 = args1;
  origs1 = args1;
  origs2 = args2;
  origs2 = args2;
 
 
  while (len--
  while (len--
         /* Stop when an ellipsis is seen.  */
         /* Stop when an ellipsis is seen.  */
         && args1 != NULL_TREE && args2 != NULL_TREE)
         && args1 != NULL_TREE && args2 != NULL_TREE)
    {
    {
      tree arg1 = TREE_VALUE (args1);
      tree arg1 = TREE_VALUE (args1);
      tree arg2 = TREE_VALUE (args2);
      tree arg2 = TREE_VALUE (args2);
      int deduce1, deduce2;
      int deduce1, deduce2;
      int quals1 = -1;
      int quals1 = -1;
      int quals2 = -1;
      int quals2 = -1;
 
 
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
          && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
          && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
        {
        {
          /* When both arguments are pack expansions, we need only
          /* When both arguments are pack expansions, we need only
             unify the patterns themselves.  */
             unify the patterns themselves.  */
          arg1 = PACK_EXPANSION_PATTERN (arg1);
          arg1 = PACK_EXPANSION_PATTERN (arg1);
          arg2 = PACK_EXPANSION_PATTERN (arg2);
          arg2 = PACK_EXPANSION_PATTERN (arg2);
 
 
          /* This is the last comparison we need to do.  */
          /* This is the last comparison we need to do.  */
          len = 0;
          len = 0;
        }
        }
 
 
      if (TREE_CODE (arg1) == REFERENCE_TYPE)
      if (TREE_CODE (arg1) == REFERENCE_TYPE)
        {
        {
          arg1 = TREE_TYPE (arg1);
          arg1 = TREE_TYPE (arg1);
          quals1 = cp_type_quals (arg1);
          quals1 = cp_type_quals (arg1);
        }
        }
 
 
      if (TREE_CODE (arg2) == REFERENCE_TYPE)
      if (TREE_CODE (arg2) == REFERENCE_TYPE)
        {
        {
          arg2 = TREE_TYPE (arg2);
          arg2 = TREE_TYPE (arg2);
          quals2 = cp_type_quals (arg2);
          quals2 = cp_type_quals (arg2);
        }
        }
 
 
      if ((quals1 < 0) != (quals2 < 0))
      if ((quals1 < 0) != (quals2 < 0))
        {
        {
          /* Only of the args is a reference, see if we should apply
          /* Only of the args is a reference, see if we should apply
             array/function pointer decay to it.  This is not part of
             array/function pointer decay to it.  This is not part of
             DR214, but is, IMHO, consistent with the deduction rules
             DR214, but is, IMHO, consistent with the deduction rules
             for the function call itself, and with our earlier
             for the function call itself, and with our earlier
             implementation of the underspecified partial ordering
             implementation of the underspecified partial ordering
             rules.  (nathan).  */
             rules.  (nathan).  */
          if (quals1 >= 0)
          if (quals1 >= 0)
            {
            {
              switch (TREE_CODE (arg1))
              switch (TREE_CODE (arg1))
                {
                {
                case ARRAY_TYPE:
                case ARRAY_TYPE:
                  arg1 = TREE_TYPE (arg1);
                  arg1 = TREE_TYPE (arg1);
                  /* FALLTHROUGH. */
                  /* FALLTHROUGH. */
                case FUNCTION_TYPE:
                case FUNCTION_TYPE:
                  arg1 = build_pointer_type (arg1);
                  arg1 = build_pointer_type (arg1);
                  break;
                  break;
 
 
                default:
                default:
                  break;
                  break;
                }
                }
            }
            }
          else
          else
            {
            {
              switch (TREE_CODE (arg2))
              switch (TREE_CODE (arg2))
                {
                {
                case ARRAY_TYPE:
                case ARRAY_TYPE:
                  arg2 = TREE_TYPE (arg2);
                  arg2 = TREE_TYPE (arg2);
                  /* FALLTHROUGH. */
                  /* FALLTHROUGH. */
                case FUNCTION_TYPE:
                case FUNCTION_TYPE:
                  arg2 = build_pointer_type (arg2);
                  arg2 = build_pointer_type (arg2);
                  break;
                  break;
 
 
                default:
                default:
                  break;
                  break;
                }
                }
            }
            }
        }
        }
 
 
      arg1 = TYPE_MAIN_VARIANT (arg1);
      arg1 = TYPE_MAIN_VARIANT (arg1);
      arg2 = TYPE_MAIN_VARIANT (arg2);
      arg2 = TYPE_MAIN_VARIANT (arg2);
 
 
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
        {
        {
          int i, len2 = list_length (args2);
          int i, len2 = list_length (args2);
          tree parmvec = make_tree_vec (1);
          tree parmvec = make_tree_vec (1);
          tree argvec = make_tree_vec (len2);
          tree argvec = make_tree_vec (len2);
          tree ta = args2;
          tree ta = args2;
 
 
          /* Setup the parameter vector, which contains only ARG1.  */
          /* Setup the parameter vector, which contains only ARG1.  */
          TREE_VEC_ELT (parmvec, 0) = arg1;
          TREE_VEC_ELT (parmvec, 0) = arg1;
 
 
          /* Setup the argument vector, which contains the remaining
          /* Setup the argument vector, which contains the remaining
             arguments.  */
             arguments.  */
          for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
          for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
            TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
            TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
 
 
          deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
          deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
                                           argvec, UNIFY_ALLOW_NONE,
                                           argvec, UNIFY_ALLOW_NONE,
                                           /*call_args_p=*/false,
                                           /*call_args_p=*/false,
                                           /*subr=*/0);
                                           /*subr=*/0);
 
 
          /* We cannot deduce in the other direction, because ARG1 is
          /* We cannot deduce in the other direction, because ARG1 is
             a pack expansion but ARG2 is not.  */
             a pack expansion but ARG2 is not.  */
          deduce2 = 0;
          deduce2 = 0;
        }
        }
      else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
      else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
        {
        {
          int i, len1 = list_length (args1);
          int i, len1 = list_length (args1);
          tree parmvec = make_tree_vec (1);
          tree parmvec = make_tree_vec (1);
          tree argvec = make_tree_vec (len1);
          tree argvec = make_tree_vec (len1);
          tree ta = args1;
          tree ta = args1;
 
 
          /* Setup the parameter vector, which contains only ARG1.  */
          /* Setup the parameter vector, which contains only ARG1.  */
          TREE_VEC_ELT (parmvec, 0) = arg2;
          TREE_VEC_ELT (parmvec, 0) = arg2;
 
 
          /* Setup the argument vector, which contains the remaining
          /* Setup the argument vector, which contains the remaining
             arguments.  */
             arguments.  */
          for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
          for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
            TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
            TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
 
 
          deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
          deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
                                           argvec, UNIFY_ALLOW_NONE,
                                           argvec, UNIFY_ALLOW_NONE,
                                           /*call_args_p=*/false,
                                           /*call_args_p=*/false,
                                           /*subr=*/0);
                                           /*subr=*/0);
 
 
          /* We cannot deduce in the other direction, because ARG2 is
          /* We cannot deduce in the other direction, because ARG2 is
             a pack expansion but ARG1 is not.*/
             a pack expansion but ARG1 is not.*/
          deduce1 = 0;
          deduce1 = 0;
        }
        }
 
 
      else
      else
        {
        {
          /* The normal case, where neither argument is a pack
          /* The normal case, where neither argument is a pack
             expansion.  */
             expansion.  */
          deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
          deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
          deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
          deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
        }
        }
 
 
      /* If we couldn't deduce arguments for tparms1 to make arg1 match
      /* If we couldn't deduce arguments for tparms1 to make arg1 match
         arg2, then arg2 is not as specialized as arg1.  */
         arg2, then arg2 is not as specialized as arg1.  */
      if (!deduce1)
      if (!deduce1)
        lose2 = true;
        lose2 = true;
      if (!deduce2)
      if (!deduce2)
        lose1 = true;
        lose1 = true;
 
 
      /* "If, for a given type, deduction succeeds in both directions
      /* "If, for a given type, deduction succeeds in both directions
         (i.e., the types are identical after the transformations above)
         (i.e., the types are identical after the transformations above)
         and if the type from the argument template is more cv-qualified
         and if the type from the argument template is more cv-qualified
         than the type from the parameter template (as described above)
         than the type from the parameter template (as described above)
         that type is considered to be more specialized than the other. If
         that type is considered to be more specialized than the other. If
         neither type is more cv-qualified than the other then neither type
         neither type is more cv-qualified than the other then neither type
         is more specialized than the other."  */
         is more specialized than the other."  */
 
 
      if (deduce1 && deduce2
      if (deduce1 && deduce2
          && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
          && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
        {
        {
          if ((quals1 & quals2) == quals2)
          if ((quals1 & quals2) == quals2)
            lose2 = true;
            lose2 = true;
          if ((quals1 & quals2) == quals1)
          if ((quals1 & quals2) == quals1)
            lose1 = true;
            lose1 = true;
        }
        }
 
 
      if (lose1 && lose2)
      if (lose1 && lose2)
        /* We've failed to deduce something in either direction.
        /* We've failed to deduce something in either direction.
           These must be unordered.  */
           These must be unordered.  */
        break;
        break;
 
 
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
      if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
          || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
          || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
        /* We have already processed all of the arguments in our
        /* We have already processed all of the arguments in our
           handing of the pack expansion type.  */
           handing of the pack expansion type.  */
        len = 0;
        len = 0;
 
 
      args1 = TREE_CHAIN (args1);
      args1 = TREE_CHAIN (args1);
      args2 = TREE_CHAIN (args2);
      args2 = TREE_CHAIN (args2);
    }
    }
 
 
  /* "In most cases, all template parameters must have values in order for
  /* "In most cases, all template parameters must have values in order for
     deduction to succeed, but for partial ordering purposes a template
     deduction to succeed, but for partial ordering purposes a template
     parameter may remain without a value provided it is not used in the
     parameter may remain without a value provided it is not used in the
     types being used for partial ordering."
     types being used for partial ordering."
 
 
     Thus, if we are missing any of the targs1 we need to substitute into
     Thus, if we are missing any of the targs1 we need to substitute into
     origs1, then pat2 is not as specialized as pat1.  This can happen when
     origs1, then pat2 is not as specialized as pat1.  This can happen when
     there is a nondeduced context.  */
     there is a nondeduced context.  */
  if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
  if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
    lose2 = true;
    lose2 = true;
  if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
  if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
    lose1 = true;
    lose1 = true;
 
 
  processing_template_decl--;
  processing_template_decl--;
 
 
  /* All things being equal, if the next argument is a pack expansion
  /* All things being equal, if the next argument is a pack expansion
     for one function but not for the other, prefer the
     for one function but not for the other, prefer the
     non-variadic function.  FIXME this is bogus; see c++/41958.  */
     non-variadic function.  FIXME this is bogus; see c++/41958.  */
  if (lose1 == lose2
  if (lose1 == lose2
      && args1 && TREE_VALUE (args1)
      && args1 && TREE_VALUE (args1)
      && args2 && TREE_VALUE (args2))
      && args2 && TREE_VALUE (args2))
    {
    {
      lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
      lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
      lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
      lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
    }
    }
 
 
  if (lose1 == lose2)
  if (lose1 == lose2)
    return 0;
    return 0;
  else if (!lose1)
  else if (!lose1)
    return 1;
    return 1;
  else
  else
    return -1;
    return -1;
}
}
 
 
/* Determine which of two partial specializations is more specialized.
/* Determine which of two partial specializations is more specialized.
 
 
   PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
   PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
   to the first partial specialization.  The TREE_VALUE is the
   to the first partial specialization.  The TREE_VALUE is the
   innermost set of template parameters for the partial
   innermost set of template parameters for the partial
   specialization.  PAT2 is similar, but for the second template.
   specialization.  PAT2 is similar, but for the second template.
 
 
   Return 1 if the first partial specialization is more specialized;
   Return 1 if the first partial specialization is more specialized;
   -1 if the second is more specialized; 0 if neither is more
   -1 if the second is more specialized; 0 if neither is more
   specialized.
   specialized.
 
 
   See [temp.class.order] for information about determining which of
   See [temp.class.order] for information about determining which of
   two templates is more specialized.  */
   two templates is more specialized.  */
 
 
static int
static int
more_specialized_class (tree pat1, tree pat2)
more_specialized_class (tree pat1, tree pat2)
{
{
  tree targs;
  tree targs;
  tree tmpl1, tmpl2;
  tree tmpl1, tmpl2;
  int winner = 0;
  int winner = 0;
  bool any_deductions = false;
  bool any_deductions = false;
 
 
  tmpl1 = TREE_TYPE (pat1);
  tmpl1 = TREE_TYPE (pat1);
  tmpl2 = TREE_TYPE (pat2);
  tmpl2 = TREE_TYPE (pat2);
 
 
  /* Just like what happens for functions, if we are ordering between
  /* Just like what happens for functions, if we are ordering between
     different class template specializations, we may encounter dependent
     different class template specializations, we may encounter dependent
     types in the arguments, and we need our dependency check functions
     types in the arguments, and we need our dependency check functions
     to behave correctly.  */
     to behave correctly.  */
  ++processing_template_decl;
  ++processing_template_decl;
  targs = get_class_bindings (TREE_VALUE (pat1),
  targs = get_class_bindings (TREE_VALUE (pat1),
                              CLASSTYPE_TI_ARGS (tmpl1),
                              CLASSTYPE_TI_ARGS (tmpl1),
                              CLASSTYPE_TI_ARGS (tmpl2));
                              CLASSTYPE_TI_ARGS (tmpl2));
  if (targs)
  if (targs)
    {
    {
      --winner;
      --winner;
      any_deductions = true;
      any_deductions = true;
    }
    }
 
 
  targs = get_class_bindings (TREE_VALUE (pat2),
  targs = get_class_bindings (TREE_VALUE (pat2),
                              CLASSTYPE_TI_ARGS (tmpl2),
                              CLASSTYPE_TI_ARGS (tmpl2),
                              CLASSTYPE_TI_ARGS (tmpl1));
                              CLASSTYPE_TI_ARGS (tmpl1));
  if (targs)
  if (targs)
    {
    {
      ++winner;
      ++winner;
      any_deductions = true;
      any_deductions = true;
    }
    }
  --processing_template_decl;
  --processing_template_decl;
 
 
  /* In the case of a tie where at least one of the class templates
  /* In the case of a tie where at least one of the class templates
     has a parameter pack at the end, the template with the most
     has a parameter pack at the end, the template with the most
     non-packed parameters wins.  */
     non-packed parameters wins.  */
  if (winner == 0
  if (winner == 0
      && any_deductions
      && any_deductions
      && (template_args_variadic_p (TREE_PURPOSE (pat1))
      && (template_args_variadic_p (TREE_PURPOSE (pat1))
          || template_args_variadic_p (TREE_PURPOSE (pat2))))
          || template_args_variadic_p (TREE_PURPOSE (pat2))))
    {
    {
      tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
      tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
      tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
      tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
      int len1 = TREE_VEC_LENGTH (args1);
      int len1 = TREE_VEC_LENGTH (args1);
      int len2 = TREE_VEC_LENGTH (args2);
      int len2 = TREE_VEC_LENGTH (args2);
 
 
      /* We don't count the pack expansion at the end.  */
      /* We don't count the pack expansion at the end.  */
      if (template_args_variadic_p (TREE_PURPOSE (pat1)))
      if (template_args_variadic_p (TREE_PURPOSE (pat1)))
        --len1;
        --len1;
      if (template_args_variadic_p (TREE_PURPOSE (pat2)))
      if (template_args_variadic_p (TREE_PURPOSE (pat2)))
        --len2;
        --len2;
 
 
      if (len1 > len2)
      if (len1 > len2)
        return 1;
        return 1;
      else if (len1 < len2)
      else if (len1 < len2)
        return -1;
        return -1;
    }
    }
 
 
  return winner;
  return winner;
}
}
 
 
/* Return the template arguments that will produce the function signature
/* Return the template arguments that will produce the function signature
   DECL from the function template FN, with the explicit template
   DECL from the function template FN, with the explicit template
   arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   also match.  Return NULL_TREE if no satisfactory arguments could be
   also match.  Return NULL_TREE if no satisfactory arguments could be
   found.  */
   found.  */
 
 
static tree
static tree
get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
{
{
  int ntparms = DECL_NTPARMS (fn);
  int ntparms = DECL_NTPARMS (fn);
  tree targs = make_tree_vec (ntparms);
  tree targs = make_tree_vec (ntparms);
  tree decl_type;
  tree decl_type;
  tree decl_arg_types;
  tree decl_arg_types;
  tree *args;
  tree *args;
  unsigned int nargs, ix;
  unsigned int nargs, ix;
  tree arg;
  tree arg;
 
 
  /* Substitute the explicit template arguments into the type of DECL.
  /* Substitute the explicit template arguments into the type of DECL.
     The call to fn_type_unification will handle substitution into the
     The call to fn_type_unification will handle substitution into the
     FN.  */
     FN.  */
  decl_type = TREE_TYPE (decl);
  decl_type = TREE_TYPE (decl);
  if (explicit_args && uses_template_parms (decl_type))
  if (explicit_args && uses_template_parms (decl_type))
    {
    {
      tree tmpl;
      tree tmpl;
      tree converted_args;
      tree converted_args;
 
 
      if (DECL_TEMPLATE_INFO (decl))
      if (DECL_TEMPLATE_INFO (decl))
        tmpl = DECL_TI_TEMPLATE (decl);
        tmpl = DECL_TI_TEMPLATE (decl);
      else
      else
        /* We can get here for some invalid specializations.  */
        /* We can get here for some invalid specializations.  */
        return NULL_TREE;
        return NULL_TREE;
 
 
      converted_args
      converted_args
        = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
        = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
                                 explicit_args, NULL_TREE,
                                 explicit_args, NULL_TREE,
                                 tf_none,
                                 tf_none,
                                 /*require_all_args=*/false,
                                 /*require_all_args=*/false,
                                 /*use_default_args=*/false);
                                 /*use_default_args=*/false);
      if (converted_args == error_mark_node)
      if (converted_args == error_mark_node)
        return NULL_TREE;
        return NULL_TREE;
 
 
      decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
      decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
      if (decl_type == error_mark_node)
      if (decl_type == error_mark_node)
        return NULL_TREE;
        return NULL_TREE;
    }
    }
 
 
  /* Never do unification on the 'this' parameter.  */
  /* Never do unification on the 'this' parameter.  */
  decl_arg_types = skip_artificial_parms_for (decl,
  decl_arg_types = skip_artificial_parms_for (decl,
                                              TYPE_ARG_TYPES (decl_type));
                                              TYPE_ARG_TYPES (decl_type));
 
 
  nargs = list_length (decl_arg_types);
  nargs = list_length (decl_arg_types);
  args = XALLOCAVEC (tree, nargs);
  args = XALLOCAVEC (tree, nargs);
  for (arg = decl_arg_types, ix = 0;
  for (arg = decl_arg_types, ix = 0;
       arg != NULL_TREE && arg != void_list_node;
       arg != NULL_TREE && arg != void_list_node;
       arg = TREE_CHAIN (arg), ++ix)
       arg = TREE_CHAIN (arg), ++ix)
    args[ix] = TREE_VALUE (arg);
    args[ix] = TREE_VALUE (arg);
 
 
  if (fn_type_unification (fn, explicit_args, targs,
  if (fn_type_unification (fn, explicit_args, targs,
                           args, ix,
                           args, ix,
                           (check_rettype || DECL_CONV_FN_P (fn)
                           (check_rettype || DECL_CONV_FN_P (fn)
                            ? TREE_TYPE (decl_type) : NULL_TREE),
                            ? TREE_TYPE (decl_type) : NULL_TREE),
                           DEDUCE_EXACT, LOOKUP_NORMAL))
                           DEDUCE_EXACT, LOOKUP_NORMAL))
    return NULL_TREE;
    return NULL_TREE;
 
 
  return targs;
  return targs;
}
}
 
 
/* Return the innermost template arguments that, when applied to a
/* Return the innermost template arguments that, when applied to a
   template specialization whose innermost template parameters are
   template specialization whose innermost template parameters are
   TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
   TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
   ARGS.
   ARGS.
 
 
   For example, suppose we have:
   For example, suppose we have:
 
 
     template <class T, class U> struct S {};
     template <class T, class U> struct S {};
     template <class T> struct S<T*, int> {};
     template <class T> struct S<T*, int> {};
 
 
   Then, suppose we want to get `S<double*, int>'.  The TPARMS will be
   Then, suppose we want to get `S<double*, int>'.  The TPARMS will be
   {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
   {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
   int}.  The resulting vector will be {double}, indicating that `T'
   int}.  The resulting vector will be {double}, indicating that `T'
   is bound to `double'.  */
   is bound to `double'.  */
 
 
static tree
static tree
get_class_bindings (tree tparms, tree spec_args, tree args)
get_class_bindings (tree tparms, tree spec_args, tree args)
{
{
  int i, ntparms = TREE_VEC_LENGTH (tparms);
  int i, ntparms = TREE_VEC_LENGTH (tparms);
  tree deduced_args;
  tree deduced_args;
  tree innermost_deduced_args;
  tree innermost_deduced_args;
 
 
  innermost_deduced_args = make_tree_vec (ntparms);
  innermost_deduced_args = make_tree_vec (ntparms);
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
    {
    {
      deduced_args = copy_node (args);
      deduced_args = copy_node (args);
      SET_TMPL_ARGS_LEVEL (deduced_args,
      SET_TMPL_ARGS_LEVEL (deduced_args,
                           TMPL_ARGS_DEPTH (deduced_args),
                           TMPL_ARGS_DEPTH (deduced_args),
                           innermost_deduced_args);
                           innermost_deduced_args);
    }
    }
  else
  else
    deduced_args = innermost_deduced_args;
    deduced_args = innermost_deduced_args;
 
 
  if (unify (tparms, deduced_args,
  if (unify (tparms, deduced_args,
             INNERMOST_TEMPLATE_ARGS (spec_args),
             INNERMOST_TEMPLATE_ARGS (spec_args),
             INNERMOST_TEMPLATE_ARGS (args),
             INNERMOST_TEMPLATE_ARGS (args),
             UNIFY_ALLOW_NONE))
             UNIFY_ALLOW_NONE))
    return NULL_TREE;
    return NULL_TREE;
 
 
  for (i =  0; i < ntparms; ++i)
  for (i =  0; i < ntparms; ++i)
    if (! TREE_VEC_ELT (innermost_deduced_args, i))
    if (! TREE_VEC_ELT (innermost_deduced_args, i))
      return NULL_TREE;
      return NULL_TREE;
 
 
  /* Verify that nondeduced template arguments agree with the type
  /* Verify that nondeduced template arguments agree with the type
     obtained from argument deduction.
     obtained from argument deduction.
 
 
     For example:
     For example:
 
 
       struct A { typedef int X; };
       struct A { typedef int X; };
       template <class T, class U> struct C {};
       template <class T, class U> struct C {};
       template <class T> struct C<T, typename T::X> {};
       template <class T> struct C<T, typename T::X> {};
 
 
     Then with the instantiation `C<A, int>', we can deduce that
     Then with the instantiation `C<A, int>', we can deduce that
     `T' is `A' but unify () does not check whether `typename T::X'
     `T' is `A' but unify () does not check whether `typename T::X'
     is `int'.  */
     is `int'.  */
  spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
  spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
  if (spec_args == error_mark_node
  if (spec_args == error_mark_node
      /* We only need to check the innermost arguments; the other
      /* We only need to check the innermost arguments; the other
         arguments will always agree.  */
         arguments will always agree.  */
      || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
      || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
                              INNERMOST_TEMPLATE_ARGS (args)))
                              INNERMOST_TEMPLATE_ARGS (args)))
    return NULL_TREE;
    return NULL_TREE;
 
 
  /* Now that we have bindings for all of the template arguments,
  /* Now that we have bindings for all of the template arguments,
     ensure that the arguments deduced for the template template
     ensure that the arguments deduced for the template template
     parameters have compatible template parameter lists.  See the use
     parameters have compatible template parameter lists.  See the use
     of template_template_parm_bindings_ok_p in fn_type_unification
     of template_template_parm_bindings_ok_p in fn_type_unification
     for more information.  */
     for more information.  */
  if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
  if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
    return NULL_TREE;
    return NULL_TREE;
 
 
  return deduced_args;
  return deduced_args;
}
}
 
 
/* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
/* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   Return the TREE_LIST node with the most specialized template, if
   Return the TREE_LIST node with the most specialized template, if
   any.  If there is no most specialized template, the error_mark_node
   any.  If there is no most specialized template, the error_mark_node
   is returned.
   is returned.
 
 
   Note that this function does not look at, or modify, the
   Note that this function does not look at, or modify, the
   TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   returned is one of the elements of INSTANTIATIONS, callers may
   returned is one of the elements of INSTANTIATIONS, callers may
   store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   and retrieve it from the value returned.  */
   and retrieve it from the value returned.  */
 
 
tree
tree
most_specialized_instantiation (tree templates)
most_specialized_instantiation (tree templates)
{
{
  tree fn, champ;
  tree fn, champ;
 
 
  ++processing_template_decl;
  ++processing_template_decl;
 
 
  champ = templates;
  champ = templates;
  for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
  for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
    {
    {
      int fate = 0;
      int fate = 0;
 
 
      if (get_bindings (TREE_VALUE (champ),
      if (get_bindings (TREE_VALUE (champ),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
                        NULL_TREE, /*check_ret=*/false))
                        NULL_TREE, /*check_ret=*/false))
        fate--;
        fate--;
 
 
      if (get_bindings (TREE_VALUE (fn),
      if (get_bindings (TREE_VALUE (fn),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
                        NULL_TREE, /*check_ret=*/false))
                        NULL_TREE, /*check_ret=*/false))
        fate++;
        fate++;
 
 
      if (fate == -1)
      if (fate == -1)
        champ = fn;
        champ = fn;
      else if (!fate)
      else if (!fate)
        {
        {
          /* Equally specialized, move to next function.  If there
          /* Equally specialized, move to next function.  If there
             is no next function, nothing's most specialized.  */
             is no next function, nothing's most specialized.  */
          fn = TREE_CHAIN (fn);
          fn = TREE_CHAIN (fn);
          champ = fn;
          champ = fn;
          if (!fn)
          if (!fn)
            break;
            break;
        }
        }
    }
    }
 
 
  if (champ)
  if (champ)
    /* Now verify that champ is better than everything earlier in the
    /* Now verify that champ is better than everything earlier in the
       instantiation list.  */
       instantiation list.  */
    for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
    for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
      if (get_bindings (TREE_VALUE (champ),
      if (get_bindings (TREE_VALUE (champ),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
                        DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
                        NULL_TREE, /*check_ret=*/false)
                        NULL_TREE, /*check_ret=*/false)
          || !get_bindings (TREE_VALUE (fn),
          || !get_bindings (TREE_VALUE (fn),
                            DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
                            DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
                            NULL_TREE, /*check_ret=*/false))
                            NULL_TREE, /*check_ret=*/false))
        {
        {
          champ = NULL_TREE;
          champ = NULL_TREE;
          break;
          break;
        }
        }
 
 
  processing_template_decl--;
  processing_template_decl--;
 
 
  if (!champ)
  if (!champ)
    return error_mark_node;
    return error_mark_node;
 
 
  return champ;
  return champ;
}
}
 
 
/* If DECL is a specialization of some template, return the most
/* If DECL is a specialization of some template, return the most
   general such template.  Otherwise, returns NULL_TREE.
   general such template.  Otherwise, returns NULL_TREE.
 
 
   For example, given:
   For example, given:
 
 
     template <class T> struct S { template <class U> void f(U); };
     template <class T> struct S { template <class U> void f(U); };
 
 
   if TMPL is `template <class U> void S<int>::f(U)' this will return
   if TMPL is `template <class U> void S<int>::f(U)' this will return
   the full template.  This function will not trace past partial
   the full template.  This function will not trace past partial
   specializations, however.  For example, given in addition:
   specializations, however.  For example, given in addition:
 
 
     template <class T> struct S<T*> { template <class U> void f(U); };
     template <class T> struct S<T*> { template <class U> void f(U); };
 
 
   if TMPL is `template <class U> void S<int*>::f(U)' this will return
   if TMPL is `template <class U> void S<int*>::f(U)' this will return
   `template <class T> template <class U> S<T*>::f(U)'.  */
   `template <class T> template <class U> S<T*>::f(U)'.  */
 
 
tree
tree
most_general_template (tree decl)
most_general_template (tree decl)
{
{
  /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
  /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
     an immediate specialization.  */
     an immediate specialization.  */
  if (TREE_CODE (decl) == FUNCTION_DECL)
  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
    {
      if (DECL_TEMPLATE_INFO (decl)) {
      if (DECL_TEMPLATE_INFO (decl)) {
        decl = DECL_TI_TEMPLATE (decl);
        decl = DECL_TI_TEMPLATE (decl);
 
 
        /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
        /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
           template friend.  */
           template friend.  */
        if (TREE_CODE (decl) != TEMPLATE_DECL)
        if (TREE_CODE (decl) != TEMPLATE_DECL)
          return NULL_TREE;
          return NULL_TREE;
      } else
      } else
        return NULL_TREE;
        return NULL_TREE;
    }
    }
 
 
  /* Look for more and more general templates.  */
  /* Look for more and more general templates.  */
  while (DECL_TEMPLATE_INFO (decl))
  while (DECL_TEMPLATE_INFO (decl))
    {
    {
      /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
      /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
         (See cp-tree.h for details.)  */
         (See cp-tree.h for details.)  */
      if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
      if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
        break;
        break;
 
 
      if (CLASS_TYPE_P (TREE_TYPE (decl))
      if (CLASS_TYPE_P (TREE_TYPE (decl))
          && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
          && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
        break;
        break;
 
 
      /* Stop if we run into an explicitly specialized class template.  */
      /* Stop if we run into an explicitly specialized class template.  */
      if (!DECL_NAMESPACE_SCOPE_P (decl)
      if (!DECL_NAMESPACE_SCOPE_P (decl)
          && DECL_CONTEXT (decl)
          && DECL_CONTEXT (decl)
          && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
          && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
        break;
        break;
 
 
      decl = DECL_TI_TEMPLATE (decl);
      decl = DECL_TI_TEMPLATE (decl);
    }
    }
 
 
  return decl;
  return decl;
}
}
 
 
/* Return the most specialized of the class template partial
/* Return the most specialized of the class template partial
   specializations of TMPL which can produce TYPE, a specialization of
   specializations of TMPL which can produce TYPE, a specialization of
   TMPL.  The value returned is actually a TREE_LIST; the TREE_TYPE is
   TMPL.  The value returned is actually a TREE_LIST; the TREE_TYPE is
   a _TYPE node corresponding to the partial specialization, while the
   a _TYPE node corresponding to the partial specialization, while the
   TREE_PURPOSE is the set of template arguments that must be
   TREE_PURPOSE is the set of template arguments that must be
   substituted into the TREE_TYPE in order to generate TYPE.
   substituted into the TREE_TYPE in order to generate TYPE.
 
 
   If the choice of partial specialization is ambiguous, a diagnostic
   If the choice of partial specialization is ambiguous, a diagnostic
   is issued, and the error_mark_node is returned.  If there are no
   is issued, and the error_mark_node is returned.  If there are no
   partial specializations of TMPL matching TYPE, then NULL_TREE is
   partial specializations of TMPL matching TYPE, then NULL_TREE is
   returned.  */
   returned.  */
 
 
static tree
static tree
most_specialized_class (tree type, tree tmpl)
most_specialized_class (tree type, tree tmpl)
{
{
  tree list = NULL_TREE;
  tree list = NULL_TREE;
  tree t;
  tree t;
  tree champ;
  tree champ;
  int fate;
  int fate;
  bool ambiguous_p;
  bool ambiguous_p;
  tree args;
  tree args;
  tree outer_args = NULL_TREE;
  tree outer_args = NULL_TREE;
 
 
  tmpl = most_general_template (tmpl);
  tmpl = most_general_template (tmpl);
  args = CLASSTYPE_TI_ARGS (type);
  args = CLASSTYPE_TI_ARGS (type);
 
 
  /* For determining which partial specialization to use, only the
  /* For determining which partial specialization to use, only the
     innermost args are interesting.  */
     innermost args are interesting.  */
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
    {
    {
      outer_args = strip_innermost_template_args (args, 1);
      outer_args = strip_innermost_template_args (args, 1);
      args = INNERMOST_TEMPLATE_ARGS (args);
      args = INNERMOST_TEMPLATE_ARGS (args);
    }
    }
 
 
  for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
  for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
    {
    {
      tree partial_spec_args;
      tree partial_spec_args;
      tree spec_args;
      tree spec_args;
      tree parms = TREE_VALUE (t);
      tree parms = TREE_VALUE (t);
 
 
      partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
      partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
 
 
      ++processing_template_decl;
      ++processing_template_decl;
 
 
      if (outer_args)
      if (outer_args)
        {
        {
          int i;
          int i;
 
 
          /* Discard the outer levels of args, and then substitute in the
          /* Discard the outer levels of args, and then substitute in the
             template args from the enclosing class.  */
             template args from the enclosing class.  */
          partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
          partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
          partial_spec_args = tsubst_template_args
          partial_spec_args = tsubst_template_args
            (partial_spec_args, outer_args, tf_none, NULL_TREE);
            (partial_spec_args, outer_args, tf_none, NULL_TREE);
 
 
          /* PARMS already refers to just the innermost parms, but the
          /* PARMS already refers to just the innermost parms, but the
             template parms in partial_spec_args had their levels lowered
             template parms in partial_spec_args had their levels lowered
             by tsubst, so we need to do the same for the parm list.  We
             by tsubst, so we need to do the same for the parm list.  We
             can't just tsubst the TREE_VEC itself, as tsubst wants to
             can't just tsubst the TREE_VEC itself, as tsubst wants to
             treat a TREE_VEC as an argument vector.  */
             treat a TREE_VEC as an argument vector.  */
          parms = copy_node (parms);
          parms = copy_node (parms);
          for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
          for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
            TREE_VEC_ELT (parms, i) =
            TREE_VEC_ELT (parms, i) =
              tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
              tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
 
 
        }
        }
 
 
      partial_spec_args =
      partial_spec_args =
          coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
          coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
                                 add_to_template_args (outer_args,
                                 add_to_template_args (outer_args,
                                                       partial_spec_args),
                                                       partial_spec_args),
                                 tmpl, tf_none,
                                 tmpl, tf_none,
                                 /*require_all_args=*/true,
                                 /*require_all_args=*/true,
                                 /*use_default_args=*/true);
                                 /*use_default_args=*/true);
 
 
      --processing_template_decl;
      --processing_template_decl;
 
 
      if (partial_spec_args == error_mark_node)
      if (partial_spec_args == error_mark_node)
        return error_mark_node;
        return error_mark_node;
 
 
      spec_args = get_class_bindings (parms,
      spec_args = get_class_bindings (parms,
                                      partial_spec_args,
                                      partial_spec_args,
                                      args);
                                      args);
      if (spec_args)
      if (spec_args)
        {
        {
          if (outer_args)
          if (outer_args)
            spec_args = add_to_template_args (outer_args, spec_args);
            spec_args = add_to_template_args (outer_args, spec_args);
          list = tree_cons (spec_args, TREE_VALUE (t), list);
          list = tree_cons (spec_args, TREE_VALUE (t), list);
          TREE_TYPE (list) = TREE_TYPE (t);
          TREE_TYPE (list) = TREE_TYPE (t);
        }
        }
    }
    }
 
 
  if (! list)
  if (! list)
    return NULL_TREE;
    return NULL_TREE;
 
 
  ambiguous_p = false;
  ambiguous_p = false;
  t = list;
  t = list;
  champ = t;
  champ = t;
  t = TREE_CHAIN (t);
  t = TREE_CHAIN (t);
  for (; t; t = TREE_CHAIN (t))
  for (; t; t = TREE_CHAIN (t))
    {
    {
      fate = more_specialized_class (champ, t);
      fate = more_specialized_class (champ, t);
      if (fate == 1)
      if (fate == 1)
        ;
        ;
      else
      else
        {
        {
          if (fate == 0)
          if (fate == 0)
            {
            {
              t = TREE_CHAIN (t);
              t = TREE_CHAIN (t);
              if (! t)
              if (! t)
                {
                {
                  ambiguous_p = true;
                  ambiguous_p = true;
                  break;
                  break;
                }
                }
            }
            }
          champ = t;
          champ = t;
        }
        }
    }
    }
 
 
  if (!ambiguous_p)
  if (!ambiguous_p)
    for (t = list; t && t != champ; t = TREE_CHAIN (t))
    for (t = list; t && t != champ; t = TREE_CHAIN (t))
      {
      {
        fate = more_specialized_class (champ, t);
        fate = more_specialized_class (champ, t);
        if (fate != 1)
        if (fate != 1)
          {
          {
            ambiguous_p = true;
            ambiguous_p = true;
            break;
            break;
          }
          }
      }
      }
 
 
  if (ambiguous_p)
  if (ambiguous_p)
    {
    {
      const char *str;
      const char *str;
      char *spaces = NULL;
      char *spaces = NULL;
      error ("ambiguous class template instantiation for %q#T", type);
      error ("ambiguous class template instantiation for %q#T", type);
      str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
      str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
      for (t = list; t; t = TREE_CHAIN (t))
      for (t = list; t; t = TREE_CHAIN (t))
        {
        {
          error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
          error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
          spaces = spaces ? spaces : get_spaces (str);
          spaces = spaces ? spaces : get_spaces (str);
        }
        }
      free (spaces);
      free (spaces);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  return champ;
  return champ;
}
}
 
 
/* Explicitly instantiate DECL.  */
/* Explicitly instantiate DECL.  */
 
 
void
void
do_decl_instantiation (tree decl, tree storage)
do_decl_instantiation (tree decl, tree storage)
{
{
  tree result = NULL_TREE;
  tree result = NULL_TREE;
  int extern_p = 0;
  int extern_p = 0;
 
 
  if (!decl || decl == error_mark_node)
  if (!decl || decl == error_mark_node)
    /* An error occurred, for which grokdeclarator has already issued
    /* An error occurred, for which grokdeclarator has already issued
       an appropriate message.  */
       an appropriate message.  */
    return;
    return;
  else if (! DECL_LANG_SPECIFIC (decl))
  else if (! DECL_LANG_SPECIFIC (decl))
    {
    {
      error ("explicit instantiation of non-template %q#D", decl);
      error ("explicit instantiation of non-template %q#D", decl);
      return;
      return;
    }
    }
  else if (TREE_CODE (decl) == VAR_DECL)
  else if (TREE_CODE (decl) == VAR_DECL)
    {
    {
      /* There is an asymmetry here in the way VAR_DECLs and
      /* There is an asymmetry here in the way VAR_DECLs and
         FUNCTION_DECLs are handled by grokdeclarator.  In the case of
         FUNCTION_DECLs are handled by grokdeclarator.  In the case of
         the latter, the DECL we get back will be marked as a
         the latter, the DECL we get back will be marked as a
         template instantiation, and the appropriate
         template instantiation, and the appropriate
         DECL_TEMPLATE_INFO will be set up.  This does not happen for
         DECL_TEMPLATE_INFO will be set up.  This does not happen for
         VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
         VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
         should handle VAR_DECLs as it currently handles
         should handle VAR_DECLs as it currently handles
         FUNCTION_DECLs.  */
         FUNCTION_DECLs.  */
      if (!DECL_CLASS_SCOPE_P (decl))
      if (!DECL_CLASS_SCOPE_P (decl))
        {
        {
          error ("%qD is not a static data member of a class template", decl);
          error ("%qD is not a static data member of a class template", decl);
          return;
          return;
        }
        }
      result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
      result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
      if (!result || TREE_CODE (result) != VAR_DECL)
      if (!result || TREE_CODE (result) != VAR_DECL)
        {
        {
          error ("no matching template for %qD found", decl);
          error ("no matching template for %qD found", decl);
          return;
          return;
        }
        }
      if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
      if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
        {
        {
          error ("type %qT for explicit instantiation %qD does not match "
          error ("type %qT for explicit instantiation %qD does not match "
                 "declared type %qT", TREE_TYPE (result), decl,
                 "declared type %qT", TREE_TYPE (result), decl,
                 TREE_TYPE (decl));
                 TREE_TYPE (decl));
          return;
          return;
        }
        }
    }
    }
  else if (TREE_CODE (decl) != FUNCTION_DECL)
  else if (TREE_CODE (decl) != FUNCTION_DECL)
    {
    {
      error ("explicit instantiation of %q#D", decl);
      error ("explicit instantiation of %q#D", decl);
      return;
      return;
    }
    }
  else
  else
    result = decl;
    result = decl;
 
 
  /* Check for various error cases.  Note that if the explicit
  /* Check for various error cases.  Note that if the explicit
     instantiation is valid the RESULT will currently be marked as an
     instantiation is valid the RESULT will currently be marked as an
     *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
     *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
     until we get here.  */
     until we get here.  */
 
 
  if (DECL_TEMPLATE_SPECIALIZATION (result))
  if (DECL_TEMPLATE_SPECIALIZATION (result))
    {
    {
      /* DR 259 [temp.spec].
      /* DR 259 [temp.spec].
 
 
         Both an explicit instantiation and a declaration of an explicit
         Both an explicit instantiation and a declaration of an explicit
         specialization shall not appear in a program unless the explicit
         specialization shall not appear in a program unless the explicit
         instantiation follows a declaration of the explicit specialization.
         instantiation follows a declaration of the explicit specialization.
 
 
         For a given set of template parameters, if an explicit
         For a given set of template parameters, if an explicit
         instantiation of a template appears after a declaration of an
         instantiation of a template appears after a declaration of an
         explicit specialization for that template, the explicit
         explicit specialization for that template, the explicit
         instantiation has no effect.  */
         instantiation has no effect.  */
      return;
      return;
    }
    }
  else if (DECL_EXPLICIT_INSTANTIATION (result))
  else if (DECL_EXPLICIT_INSTANTIATION (result))
    {
    {
      /* [temp.spec]
      /* [temp.spec]
 
 
         No program shall explicitly instantiate any template more
         No program shall explicitly instantiate any template more
         than once.
         than once.
 
 
         We check DECL_NOT_REALLY_EXTERN so as not to complain when
         We check DECL_NOT_REALLY_EXTERN so as not to complain when
         the first instantiation was `extern' and the second is not,
         the first instantiation was `extern' and the second is not,
         and EXTERN_P for the opposite case.  */
         and EXTERN_P for the opposite case.  */
      if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
      if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
        permerror (input_location, "duplicate explicit instantiation of %q#D", result);
        permerror (input_location, "duplicate explicit instantiation of %q#D", result);
      /* If an "extern" explicit instantiation follows an ordinary
      /* If an "extern" explicit instantiation follows an ordinary
         explicit instantiation, the template is instantiated.  */
         explicit instantiation, the template is instantiated.  */
      if (extern_p)
      if (extern_p)
        return;
        return;
    }
    }
  else if (!DECL_IMPLICIT_INSTANTIATION (result))
  else if (!DECL_IMPLICIT_INSTANTIATION (result))
    {
    {
      error ("no matching template for %qD found", result);
      error ("no matching template for %qD found", result);
      return;
      return;
    }
    }
  else if (!DECL_TEMPLATE_INFO (result))
  else if (!DECL_TEMPLATE_INFO (result))
    {
    {
      permerror (input_location, "explicit instantiation of non-template %q#D", result);
      permerror (input_location, "explicit instantiation of non-template %q#D", result);
      return;
      return;
    }
    }
 
 
  if (storage == NULL_TREE)
  if (storage == NULL_TREE)
    ;
    ;
  else if (storage == ridpointers[(int) RID_EXTERN])
  else if (storage == ridpointers[(int) RID_EXTERN])
    {
    {
      if (!in_system_header && (cxx_dialect == cxx98))
      if (!in_system_header && (cxx_dialect == cxx98))
        pedwarn (input_location, OPT_pedantic,
        pedwarn (input_location, OPT_pedantic,
                 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
                 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
                 "instantiations");
                 "instantiations");
      extern_p = 1;
      extern_p = 1;
    }
    }
  else
  else
    error ("storage class %qD applied to template instantiation", storage);
    error ("storage class %qD applied to template instantiation", storage);
 
 
  check_explicit_instantiation_namespace (result);
  check_explicit_instantiation_namespace (result);
  mark_decl_instantiated (result, extern_p);
  mark_decl_instantiated (result, extern_p);
  if (! extern_p)
  if (! extern_p)
    instantiate_decl (result, /*defer_ok=*/1,
    instantiate_decl (result, /*defer_ok=*/1,
                      /*expl_inst_class_mem_p=*/false);
                      /*expl_inst_class_mem_p=*/false);
}
}
 
 
static void
static void
mark_class_instantiated (tree t, int extern_p)
mark_class_instantiated (tree t, int extern_p)
{
{
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
  TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
  TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
  if (! extern_p)
  if (! extern_p)
    {
    {
      CLASSTYPE_DEBUG_REQUESTED (t) = 1;
      CLASSTYPE_DEBUG_REQUESTED (t) = 1;
      rest_of_type_compilation (t, 1);
      rest_of_type_compilation (t, 1);
    }
    }
}
}
 
 
/* Called from do_type_instantiation through binding_table_foreach to
/* Called from do_type_instantiation through binding_table_foreach to
   do recursive instantiation for the type bound in ENTRY.  */
   do recursive instantiation for the type bound in ENTRY.  */
static void
static void
bt_instantiate_type_proc (binding_entry entry, void *data)
bt_instantiate_type_proc (binding_entry entry, void *data)
{
{
  tree storage = *(tree *) data;
  tree storage = *(tree *) data;
 
 
  if (MAYBE_CLASS_TYPE_P (entry->type)
  if (MAYBE_CLASS_TYPE_P (entry->type)
      && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
      && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
    do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
    do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
}
}
 
 
/* Called from do_type_instantiation to instantiate a member
/* Called from do_type_instantiation to instantiate a member
   (a member function or a static member variable) of an
   (a member function or a static member variable) of an
   explicitly instantiated class template.  */
   explicitly instantiated class template.  */
static void
static void
instantiate_class_member (tree decl, int extern_p)
instantiate_class_member (tree decl, int extern_p)
{
{
  mark_decl_instantiated (decl, extern_p);
  mark_decl_instantiated (decl, extern_p);
  if (! extern_p)
  if (! extern_p)
    instantiate_decl (decl, /*defer_ok=*/1,
    instantiate_decl (decl, /*defer_ok=*/1,
                      /*expl_inst_class_mem_p=*/true);
                      /*expl_inst_class_mem_p=*/true);
}
}
 
 
/* Perform an explicit instantiation of template class T.  STORAGE, if
/* Perform an explicit instantiation of template class T.  STORAGE, if
   non-null, is the RID for extern, inline or static.  COMPLAIN is
   non-null, is the RID for extern, inline or static.  COMPLAIN is
   nonzero if this is called from the parser, zero if called recursively,
   nonzero if this is called from the parser, zero if called recursively,
   since the standard is unclear (as detailed below).  */
   since the standard is unclear (as detailed below).  */
 
 
void
void
do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
{
{
  int extern_p = 0;
  int extern_p = 0;
  int nomem_p = 0;
  int nomem_p = 0;
  int static_p = 0;
  int static_p = 0;
  int previous_instantiation_extern_p = 0;
  int previous_instantiation_extern_p = 0;
 
 
  if (TREE_CODE (t) == TYPE_DECL)
  if (TREE_CODE (t) == TYPE_DECL)
    t = TREE_TYPE (t);
    t = TREE_TYPE (t);
 
 
  if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
  if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
    {
    {
      error ("explicit instantiation of non-template type %qT", t);
      error ("explicit instantiation of non-template type %qT", t);
      return;
      return;
    }
    }
 
 
  complete_type (t);
  complete_type (t);
 
 
  if (!COMPLETE_TYPE_P (t))
  if (!COMPLETE_TYPE_P (t))
    {
    {
      if (complain & tf_error)
      if (complain & tf_error)
        error ("explicit instantiation of %q#T before definition of template",
        error ("explicit instantiation of %q#T before definition of template",
               t);
               t);
      return;
      return;
    }
    }
 
 
  if (storage != NULL_TREE)
  if (storage != NULL_TREE)
    {
    {
      if (!in_system_header)
      if (!in_system_header)
        {
        {
          if (storage == ridpointers[(int) RID_EXTERN])
          if (storage == ridpointers[(int) RID_EXTERN])
            {
            {
              if (cxx_dialect == cxx98)
              if (cxx_dialect == cxx98)
                pedwarn (input_location, OPT_pedantic,
                pedwarn (input_location, OPT_pedantic,
                         "ISO C++ 1998 forbids the use of %<extern%> on "
                         "ISO C++ 1998 forbids the use of %<extern%> on "
                         "explicit instantiations");
                         "explicit instantiations");
            }
            }
          else
          else
            pedwarn (input_location, OPT_pedantic,
            pedwarn (input_location, OPT_pedantic,
                     "ISO C++ forbids the use of %qE"
                     "ISO C++ forbids the use of %qE"
                     " on explicit instantiations", storage);
                     " on explicit instantiations", storage);
        }
        }
 
 
      if (storage == ridpointers[(int) RID_INLINE])
      if (storage == ridpointers[(int) RID_INLINE])
        nomem_p = 1;
        nomem_p = 1;
      else if (storage == ridpointers[(int) RID_EXTERN])
      else if (storage == ridpointers[(int) RID_EXTERN])
        extern_p = 1;
        extern_p = 1;
      else if (storage == ridpointers[(int) RID_STATIC])
      else if (storage == ridpointers[(int) RID_STATIC])
        static_p = 1;
        static_p = 1;
      else
      else
        {
        {
          error ("storage class %qD applied to template instantiation",
          error ("storage class %qD applied to template instantiation",
                 storage);
                 storage);
          extern_p = 0;
          extern_p = 0;
        }
        }
    }
    }
 
 
  if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
  if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
    {
    {
      /* DR 259 [temp.spec].
      /* DR 259 [temp.spec].
 
 
         Both an explicit instantiation and a declaration of an explicit
         Both an explicit instantiation and a declaration of an explicit
         specialization shall not appear in a program unless the explicit
         specialization shall not appear in a program unless the explicit
         instantiation follows a declaration of the explicit specialization.
         instantiation follows a declaration of the explicit specialization.
 
 
         For a given set of template parameters, if an explicit
         For a given set of template parameters, if an explicit
         instantiation of a template appears after a declaration of an
         instantiation of a template appears after a declaration of an
         explicit specialization for that template, the explicit
         explicit specialization for that template, the explicit
         instantiation has no effect.  */
         instantiation has no effect.  */
      return;
      return;
    }
    }
  else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
  else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
    {
    {
      /* [temp.spec]
      /* [temp.spec]
 
 
         No program shall explicitly instantiate any template more
         No program shall explicitly instantiate any template more
         than once.
         than once.
 
 
         If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
         If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
         instantiation was `extern'.  If EXTERN_P then the second is.
         instantiation was `extern'.  If EXTERN_P then the second is.
         These cases are OK.  */
         These cases are OK.  */
      previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
      previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
 
 
      if (!previous_instantiation_extern_p && !extern_p
      if (!previous_instantiation_extern_p && !extern_p
          && (complain & tf_error))
          && (complain & tf_error))
        permerror (input_location, "duplicate explicit instantiation of %q#T", t);
        permerror (input_location, "duplicate explicit instantiation of %q#T", t);
 
 
      /* If we've already instantiated the template, just return now.  */
      /* If we've already instantiated the template, just return now.  */
      if (!CLASSTYPE_INTERFACE_ONLY (t))
      if (!CLASSTYPE_INTERFACE_ONLY (t))
        return;
        return;
    }
    }
 
 
  check_explicit_instantiation_namespace (TYPE_NAME (t));
  check_explicit_instantiation_namespace (TYPE_NAME (t));
  mark_class_instantiated (t, extern_p);
  mark_class_instantiated (t, extern_p);
 
 
  if (nomem_p)
  if (nomem_p)
    return;
    return;
 
 
  {
  {
    tree tmp;
    tree tmp;
 
 
    /* In contrast to implicit instantiation, where only the
    /* In contrast to implicit instantiation, where only the
       declarations, and not the definitions, of members are
       declarations, and not the definitions, of members are
       instantiated, we have here:
       instantiated, we have here:
 
 
         [temp.explicit]
         [temp.explicit]
 
 
         The explicit instantiation of a class template specialization
         The explicit instantiation of a class template specialization
         implies the instantiation of all of its members not
         implies the instantiation of all of its members not
         previously explicitly specialized in the translation unit
         previously explicitly specialized in the translation unit
         containing the explicit instantiation.
         containing the explicit instantiation.
 
 
       Of course, we can't instantiate member template classes, since
       Of course, we can't instantiate member template classes, since
       we don't have any arguments for them.  Note that the standard
       we don't have any arguments for them.  Note that the standard
       is unclear on whether the instantiation of the members are
       is unclear on whether the instantiation of the members are
       *explicit* instantiations or not.  However, the most natural
       *explicit* instantiations or not.  However, the most natural
       interpretation is that it should be an explicit instantiation.  */
       interpretation is that it should be an explicit instantiation.  */
 
 
    if (! static_p)
    if (! static_p)
      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
        if (TREE_CODE (tmp) == FUNCTION_DECL
        if (TREE_CODE (tmp) == FUNCTION_DECL
            && DECL_TEMPLATE_INSTANTIATION (tmp))
            && DECL_TEMPLATE_INSTANTIATION (tmp))
          instantiate_class_member (tmp, extern_p);
          instantiate_class_member (tmp, extern_p);
 
 
    for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
    for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
      if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
      if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
        instantiate_class_member (tmp, extern_p);
        instantiate_class_member (tmp, extern_p);
 
 
    if (CLASSTYPE_NESTED_UTDS (t))
    if (CLASSTYPE_NESTED_UTDS (t))
      binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
      binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
                             bt_instantiate_type_proc, &storage);
                             bt_instantiate_type_proc, &storage);
  }
  }
}
}
 
 
/* Given a function DECL, which is a specialization of TMPL, modify
/* Given a function DECL, which is a specialization of TMPL, modify
   DECL to be a re-instantiation of TMPL with the same template
   DECL to be a re-instantiation of TMPL with the same template
   arguments.  TMPL should be the template into which tsubst'ing
   arguments.  TMPL should be the template into which tsubst'ing
   should occur for DECL, not the most general template.
   should occur for DECL, not the most general template.
 
 
   One reason for doing this is a scenario like this:
   One reason for doing this is a scenario like this:
 
 
     template <class T>
     template <class T>
     void f(const T&, int i);
     void f(const T&, int i);
 
 
     void g() { f(3, 7); }
     void g() { f(3, 7); }
 
 
     template <class T>
     template <class T>
     void f(const T& t, const int i) { }
     void f(const T& t, const int i) { }
 
 
   Note that when the template is first instantiated, with
   Note that when the template is first instantiated, with
   instantiate_template, the resulting DECL will have no name for the
   instantiate_template, the resulting DECL will have no name for the
   first parameter, and the wrong type for the second.  So, when we go
   first parameter, and the wrong type for the second.  So, when we go
   to instantiate the DECL, we regenerate it.  */
   to instantiate the DECL, we regenerate it.  */
 
 
static void
static void
regenerate_decl_from_template (tree decl, tree tmpl)
regenerate_decl_from_template (tree decl, tree tmpl)
{
{
  /* The arguments used to instantiate DECL, from the most general
  /* The arguments used to instantiate DECL, from the most general
     template.  */
     template.  */
  tree args;
  tree args;
  tree code_pattern;
  tree code_pattern;
 
 
  args = DECL_TI_ARGS (decl);
  args = DECL_TI_ARGS (decl);
  code_pattern = DECL_TEMPLATE_RESULT (tmpl);
  code_pattern = DECL_TEMPLATE_RESULT (tmpl);
 
 
  /* Make sure that we can see identifiers, and compute access
  /* Make sure that we can see identifiers, and compute access
     correctly.  */
     correctly.  */
  push_access_scope (decl);
  push_access_scope (decl);
 
 
  if (TREE_CODE (decl) == FUNCTION_DECL)
  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
    {
      tree decl_parm;
      tree decl_parm;
      tree pattern_parm;
      tree pattern_parm;
      tree specs;
      tree specs;
      int args_depth;
      int args_depth;
      int parms_depth;
      int parms_depth;
 
 
      args_depth = TMPL_ARGS_DEPTH (args);
      args_depth = TMPL_ARGS_DEPTH (args);
      parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
      parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
      if (args_depth > parms_depth)
      if (args_depth > parms_depth)
        args = get_innermost_template_args (args, parms_depth);
        args = get_innermost_template_args (args, parms_depth);
 
 
      specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
      specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
                                              args, tf_error, NULL_TREE);
                                              args, tf_error, NULL_TREE);
      if (specs)
      if (specs)
        TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
        TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
                                                    specs);
                                                    specs);
 
 
      /* Merge parameter declarations.  */
      /* Merge parameter declarations.  */
      decl_parm = skip_artificial_parms_for (decl,
      decl_parm = skip_artificial_parms_for (decl,
                                             DECL_ARGUMENTS (decl));
                                             DECL_ARGUMENTS (decl));
      pattern_parm
      pattern_parm
        = skip_artificial_parms_for (code_pattern,
        = skip_artificial_parms_for (code_pattern,
                                     DECL_ARGUMENTS (code_pattern));
                                     DECL_ARGUMENTS (code_pattern));
      while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
      while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
        {
        {
          tree parm_type;
          tree parm_type;
          tree attributes;
          tree attributes;
 
 
          if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
          if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
            DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
            DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
          parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
          parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
                              NULL_TREE);
                              NULL_TREE);
          parm_type = type_decays_to (parm_type);
          parm_type = type_decays_to (parm_type);
          if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
          if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
            TREE_TYPE (decl_parm) = parm_type;
            TREE_TYPE (decl_parm) = parm_type;
          attributes = DECL_ATTRIBUTES (pattern_parm);
          attributes = DECL_ATTRIBUTES (pattern_parm);
          if (DECL_ATTRIBUTES (decl_parm) != attributes)
          if (DECL_ATTRIBUTES (decl_parm) != attributes)
            {
            {
              DECL_ATTRIBUTES (decl_parm) = attributes;
              DECL_ATTRIBUTES (decl_parm) = attributes;
              cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
              cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
            }
            }
          decl_parm = TREE_CHAIN (decl_parm);
          decl_parm = TREE_CHAIN (decl_parm);
          pattern_parm = TREE_CHAIN (pattern_parm);
          pattern_parm = TREE_CHAIN (pattern_parm);
        }
        }
      /* Merge any parameters that match with the function parameter
      /* Merge any parameters that match with the function parameter
         pack.  */
         pack.  */
      if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
      if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
        {
        {
          int i, len;
          int i, len;
          tree expanded_types;
          tree expanded_types;
          /* Expand the TYPE_PACK_EXPANSION that provides the types for
          /* Expand the TYPE_PACK_EXPANSION that provides the types for
             the parameters in this function parameter pack.  */
             the parameters in this function parameter pack.  */
          expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
          expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
                                                 args, tf_error, NULL_TREE);
                                                 args, tf_error, NULL_TREE);
          len = TREE_VEC_LENGTH (expanded_types);
          len = TREE_VEC_LENGTH (expanded_types);
          for (i = 0; i < len; i++)
          for (i = 0; i < len; i++)
            {
            {
              tree parm_type;
              tree parm_type;
              tree attributes;
              tree attributes;
 
 
              if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
              if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
                /* Rename the parameter to include the index.  */
                /* Rename the parameter to include the index.  */
                DECL_NAME (decl_parm) =
                DECL_NAME (decl_parm) =
                  make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
                  make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
              parm_type = TREE_VEC_ELT (expanded_types, i);
              parm_type = TREE_VEC_ELT (expanded_types, i);
              parm_type = type_decays_to (parm_type);
              parm_type = type_decays_to (parm_type);
              if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
              if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
                TREE_TYPE (decl_parm) = parm_type;
                TREE_TYPE (decl_parm) = parm_type;
              attributes = DECL_ATTRIBUTES (pattern_parm);
              attributes = DECL_ATTRIBUTES (pattern_parm);
              if (DECL_ATTRIBUTES (decl_parm) != attributes)
              if (DECL_ATTRIBUTES (decl_parm) != attributes)
                {
                {
                  DECL_ATTRIBUTES (decl_parm) = attributes;
                  DECL_ATTRIBUTES (decl_parm) = attributes;
                  cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
                  cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
                }
                }
              decl_parm = TREE_CHAIN (decl_parm);
              decl_parm = TREE_CHAIN (decl_parm);
            }
            }
        }
        }
      /* Merge additional specifiers from the CODE_PATTERN.  */
      /* Merge additional specifiers from the CODE_PATTERN.  */
      if (DECL_DECLARED_INLINE_P (code_pattern)
      if (DECL_DECLARED_INLINE_P (code_pattern)
          && !DECL_DECLARED_INLINE_P (decl))
          && !DECL_DECLARED_INLINE_P (decl))
        DECL_DECLARED_INLINE_P (decl) = 1;
        DECL_DECLARED_INLINE_P (decl) = 1;
    }
    }
  else if (TREE_CODE (decl) == VAR_DECL)
  else if (TREE_CODE (decl) == VAR_DECL)
    DECL_INITIAL (decl) =
    DECL_INITIAL (decl) =
      tsubst_expr (DECL_INITIAL (code_pattern), args,
      tsubst_expr (DECL_INITIAL (code_pattern), args,
                   tf_error, DECL_TI_TEMPLATE (decl),
                   tf_error, DECL_TI_TEMPLATE (decl),
                   /*integral_constant_expression_p=*/false);
                   /*integral_constant_expression_p=*/false);
  else
  else
    gcc_unreachable ();
    gcc_unreachable ();
 
 
  pop_access_scope (decl);
  pop_access_scope (decl);
}
}
 
 
/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   substituted to get DECL.  */
   substituted to get DECL.  */
 
 
tree
tree
template_for_substitution (tree decl)
template_for_substitution (tree decl)
{
{
  tree tmpl = DECL_TI_TEMPLATE (decl);
  tree tmpl = DECL_TI_TEMPLATE (decl);
 
 
  /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
  /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
     for the instantiation.  This is not always the most general
     for the instantiation.  This is not always the most general
     template.  Consider, for example:
     template.  Consider, for example:
 
 
        template <class T>
        template <class T>
        struct S { template <class U> void f();
        struct S { template <class U> void f();
                   template <> void f<int>(); };
                   template <> void f<int>(); };
 
 
     and an instantiation of S<double>::f<int>.  We want TD to be the
     and an instantiation of S<double>::f<int>.  We want TD to be the
     specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
     specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
  while (/* An instantiation cannot have a definition, so we need a
  while (/* An instantiation cannot have a definition, so we need a
            more general template.  */
            more general template.  */
         DECL_TEMPLATE_INSTANTIATION (tmpl)
         DECL_TEMPLATE_INSTANTIATION (tmpl)
           /* We must also deal with friend templates.  Given:
           /* We must also deal with friend templates.  Given:
 
 
                template <class T> struct S {
                template <class T> struct S {
                  template <class U> friend void f() {};
                  template <class U> friend void f() {};
                };
                };
 
 
              S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
              S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
              so far as the language is concerned, but that's still
              so far as the language is concerned, but that's still
              where we get the pattern for the instantiation from.  On
              where we get the pattern for the instantiation from.  On
              other hand, if the definition comes outside the class, say:
              other hand, if the definition comes outside the class, say:
 
 
                template <class T> struct S {
                template <class T> struct S {
                  template <class U> friend void f();
                  template <class U> friend void f();
                };
                };
                template <class U> friend void f() {}
                template <class U> friend void f() {}
 
 
              we don't need to look any further.  That's what the check for
              we don't need to look any further.  That's what the check for
              DECL_INITIAL is for.  */
              DECL_INITIAL is for.  */
          || (TREE_CODE (decl) == FUNCTION_DECL
          || (TREE_CODE (decl) == FUNCTION_DECL
              && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
              && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
              && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
              && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
    {
    {
      /* The present template, TD, should not be a definition.  If it
      /* The present template, TD, should not be a definition.  If it
         were a definition, we should be using it!  Note that we
         were a definition, we should be using it!  Note that we
         cannot restructure the loop to just keep going until we find
         cannot restructure the loop to just keep going until we find
         a template with a definition, since that might go too far if
         a template with a definition, since that might go too far if
         a specialization was declared, but not defined.  */
         a specialization was declared, but not defined.  */
      gcc_assert (TREE_CODE (decl) != VAR_DECL
      gcc_assert (TREE_CODE (decl) != VAR_DECL
                  || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
                  || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
 
 
      /* Fetch the more general template.  */
      /* Fetch the more general template.  */
      tmpl = DECL_TI_TEMPLATE (tmpl);
      tmpl = DECL_TI_TEMPLATE (tmpl);
    }
    }
 
 
  return tmpl;
  return tmpl;
}
}
 
 
/* Returns true if we need to instantiate this template instance even if we
/* Returns true if we need to instantiate this template instance even if we
   know we aren't going to emit it..  */
   know we aren't going to emit it..  */
 
 
bool
bool
always_instantiate_p (tree decl)
always_instantiate_p (tree decl)
{
{
  /* We always instantiate inline functions so that we can inline them.  An
  /* We always instantiate inline functions so that we can inline them.  An
     explicit instantiation declaration prohibits implicit instantiation of
     explicit instantiation declaration prohibits implicit instantiation of
     non-inline functions.  With high levels of optimization, we would
     non-inline functions.  With high levels of optimization, we would
     normally inline non-inline functions -- but we're not allowed to do
     normally inline non-inline functions -- but we're not allowed to do
     that for "extern template" functions.  Therefore, we check
     that for "extern template" functions.  Therefore, we check
     DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
     DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
  return ((TREE_CODE (decl) == FUNCTION_DECL
  return ((TREE_CODE (decl) == FUNCTION_DECL
           && DECL_DECLARED_INLINE_P (decl))
           && DECL_DECLARED_INLINE_P (decl))
          /* And we need to instantiate static data members so that
          /* And we need to instantiate static data members so that
             their initializers are available in integral constant
             their initializers are available in integral constant
             expressions.  */
             expressions.  */
          || (TREE_CODE (decl) == VAR_DECL
          || (TREE_CODE (decl) == VAR_DECL
              && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
              && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
}
}
 
 
/* Produce the definition of D, a _DECL generated from a template.  If
/* Produce the definition of D, a _DECL generated from a template.  If
   DEFER_OK is nonzero, then we don't have to actually do the
   DEFER_OK is nonzero, then we don't have to actually do the
   instantiation now; we just have to do it sometime.  Normally it is
   instantiation now; we just have to do it sometime.  Normally it is
   an error if this is an explicit instantiation but D is undefined.
   an error if this is an explicit instantiation but D is undefined.
   EXPL_INST_CLASS_MEM_P is true iff D is a member of an
   EXPL_INST_CLASS_MEM_P is true iff D is a member of an
   explicitly instantiated class template.  */
   explicitly instantiated class template.  */
 
 
tree
tree
instantiate_decl (tree d, int defer_ok,
instantiate_decl (tree d, int defer_ok,
                  bool expl_inst_class_mem_p)
                  bool expl_inst_class_mem_p)
{
{
  tree tmpl = DECL_TI_TEMPLATE (d);
  tree tmpl = DECL_TI_TEMPLATE (d);
  tree gen_args;
  tree gen_args;
  tree args;
  tree args;
  tree td;
  tree td;
  tree code_pattern;
  tree code_pattern;
  tree spec;
  tree spec;
  tree gen_tmpl;
  tree gen_tmpl;
  bool pattern_defined;
  bool pattern_defined;
  int need_push;
  int need_push;
  location_t saved_loc = input_location;
  location_t saved_loc = input_location;
  bool external_p;
  bool external_p;
 
 
  /* This function should only be used to instantiate templates for
  /* This function should only be used to instantiate templates for
     functions and static member variables.  */
     functions and static member variables.  */
  gcc_assert (TREE_CODE (d) == FUNCTION_DECL
  gcc_assert (TREE_CODE (d) == FUNCTION_DECL
              || TREE_CODE (d) == VAR_DECL);
              || TREE_CODE (d) == VAR_DECL);
 
 
  /* Variables are never deferred; if instantiation is required, they
  /* Variables are never deferred; if instantiation is required, they
     are instantiated right away.  That allows for better code in the
     are instantiated right away.  That allows for better code in the
     case that an expression refers to the value of the variable --
     case that an expression refers to the value of the variable --
     if the variable has a constant value the referring expression can
     if the variable has a constant value the referring expression can
     take advantage of that fact.  */
     take advantage of that fact.  */
  if (TREE_CODE (d) == VAR_DECL)
  if (TREE_CODE (d) == VAR_DECL)
    defer_ok = 0;
    defer_ok = 0;
 
 
  /* Don't instantiate cloned functions.  Instead, instantiate the
  /* Don't instantiate cloned functions.  Instead, instantiate the
     functions they cloned.  */
     functions they cloned.  */
  if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
  if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
    d = DECL_CLONED_FUNCTION (d);
    d = DECL_CLONED_FUNCTION (d);
 
 
  if (DECL_TEMPLATE_INSTANTIATED (d)
  if (DECL_TEMPLATE_INSTANTIATED (d)
      || DECL_TEMPLATE_SPECIALIZATION (d))
      || DECL_TEMPLATE_SPECIALIZATION (d))
    /* D has already been instantiated or explicitly specialized, so
    /* D has already been instantiated or explicitly specialized, so
       there's nothing for us to do here.
       there's nothing for us to do here.
 
 
       It might seem reasonable to check whether or not D is an explicit
       It might seem reasonable to check whether or not D is an explicit
       instantiation, and, if so, stop here.  But when an explicit
       instantiation, and, if so, stop here.  But when an explicit
       instantiation is deferred until the end of the compilation,
       instantiation is deferred until the end of the compilation,
       DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
       DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
       the instantiation.  */
       the instantiation.  */
    return d;
    return d;
 
 
  /* Check to see whether we know that this template will be
  /* Check to see whether we know that this template will be
     instantiated in some other file, as with "extern template"
     instantiated in some other file, as with "extern template"
     extension.  */
     extension.  */
  external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
  external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
 
 
  /* In general, we do not instantiate such templates.  */
  /* In general, we do not instantiate such templates.  */
  if (external_p && !always_instantiate_p (d))
  if (external_p && !always_instantiate_p (d))
    return d;
    return d;
 
 
  gen_tmpl = most_general_template (tmpl);
  gen_tmpl = most_general_template (tmpl);
  gen_args = DECL_TI_ARGS (d);
  gen_args = DECL_TI_ARGS (d);
 
 
  if (tmpl != gen_tmpl)
  if (tmpl != gen_tmpl)
    /* We should already have the extra args.  */
    /* We should already have the extra args.  */
    gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
    gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
                == TMPL_ARGS_DEPTH (gen_args));
                == TMPL_ARGS_DEPTH (gen_args));
  /* And what's in the hash table should match D.  */
  /* And what's in the hash table should match D.  */
  gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
  gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
              || spec == NULL_TREE);
              || spec == NULL_TREE);
 
 
  /* This needs to happen before any tsubsting.  */
  /* This needs to happen before any tsubsting.  */
  if (! push_tinst_level (d))
  if (! push_tinst_level (d))
    return d;
    return d;
 
 
  timevar_push (TV_PARSE);
  timevar_push (TV_PARSE);
 
 
  /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
  /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
     for the instantiation.  */
     for the instantiation.  */
  td = template_for_substitution (d);
  td = template_for_substitution (d);
  code_pattern = DECL_TEMPLATE_RESULT (td);
  code_pattern = DECL_TEMPLATE_RESULT (td);
 
 
  /* We should never be trying to instantiate a member of a class
  /* We should never be trying to instantiate a member of a class
     template or partial specialization.  */
     template or partial specialization.  */
  gcc_assert (d != code_pattern);
  gcc_assert (d != code_pattern);
 
 
  if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
  if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
      || DECL_TEMPLATE_SPECIALIZATION (td))
      || DECL_TEMPLATE_SPECIALIZATION (td))
    /* In the case of a friend template whose definition is provided
    /* In the case of a friend template whose definition is provided
       outside the class, we may have too many arguments.  Drop the
       outside the class, we may have too many arguments.  Drop the
       ones we don't need.  The same is true for specializations.  */
       ones we don't need.  The same is true for specializations.  */
    args = get_innermost_template_args
    args = get_innermost_template_args
      (gen_args, TMPL_PARMS_DEPTH  (DECL_TEMPLATE_PARMS (td)));
      (gen_args, TMPL_PARMS_DEPTH  (DECL_TEMPLATE_PARMS (td)));
  else
  else
    args = gen_args;
    args = gen_args;
 
 
  if (TREE_CODE (d) == FUNCTION_DECL)
  if (TREE_CODE (d) == FUNCTION_DECL)
    pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
    pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
  else
  else
    pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
    pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
 
 
  /* We may be in the middle of deferred access check.  Disable it now.  */
  /* We may be in the middle of deferred access check.  Disable it now.  */
  push_deferring_access_checks (dk_no_deferred);
  push_deferring_access_checks (dk_no_deferred);
 
 
  /* Unless an explicit instantiation directive has already determined
  /* Unless an explicit instantiation directive has already determined
     the linkage of D, remember that a definition is available for
     the linkage of D, remember that a definition is available for
     this entity.  */
     this entity.  */
  if (pattern_defined
  if (pattern_defined
      && !DECL_INTERFACE_KNOWN (d)
      && !DECL_INTERFACE_KNOWN (d)
      && !DECL_NOT_REALLY_EXTERN (d))
      && !DECL_NOT_REALLY_EXTERN (d))
    mark_definable (d);
    mark_definable (d);
 
 
  input_location = DECL_SOURCE_LOCATION (d);
  input_location = DECL_SOURCE_LOCATION (d);
 
 
  /* If D is a member of an explicitly instantiated class template,
  /* If D is a member of an explicitly instantiated class template,
     and no definition is available, treat it like an implicit
     and no definition is available, treat it like an implicit
     instantiation.  */
     instantiation.  */
  if (!pattern_defined && expl_inst_class_mem_p
  if (!pattern_defined && expl_inst_class_mem_p
      && DECL_EXPLICIT_INSTANTIATION (d))
      && DECL_EXPLICIT_INSTANTIATION (d))
    {
    {
      DECL_NOT_REALLY_EXTERN (d) = 0;
      DECL_NOT_REALLY_EXTERN (d) = 0;
      DECL_INTERFACE_KNOWN (d) = 0;
      DECL_INTERFACE_KNOWN (d) = 0;
      SET_DECL_IMPLICIT_INSTANTIATION (d);
      SET_DECL_IMPLICIT_INSTANTIATION (d);
    }
    }
 
 
  /* Recheck the substitutions to obtain any warning messages
  /* Recheck the substitutions to obtain any warning messages
     about ignoring cv qualifiers.  Don't do this for artificial decls,
     about ignoring cv qualifiers.  Don't do this for artificial decls,
     as it breaks the context-sensitive substitution for lambda op(). */
     as it breaks the context-sensitive substitution for lambda op(). */
  if (!defer_ok && !DECL_ARTIFICIAL (d))
  if (!defer_ok && !DECL_ARTIFICIAL (d))
    {
    {
      tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
      tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
      tree type = TREE_TYPE (gen);
      tree type = TREE_TYPE (gen);
 
 
      /* Make sure that we can see identifiers, and compute access
      /* Make sure that we can see identifiers, and compute access
         correctly.  D is already the target FUNCTION_DECL with the
         correctly.  D is already the target FUNCTION_DECL with the
         right context.  */
         right context.  */
      push_access_scope (d);
      push_access_scope (d);
 
 
      if (TREE_CODE (gen) == FUNCTION_DECL)
      if (TREE_CODE (gen) == FUNCTION_DECL)
        {
        {
          tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
          tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
          tsubst_exception_specification (type, gen_args, tf_warning_or_error,
          tsubst_exception_specification (type, gen_args, tf_warning_or_error,
                                          d);
                                          d);
          /* Don't simply tsubst the function type, as that will give
          /* Don't simply tsubst the function type, as that will give
             duplicate warnings about poor parameter qualifications.
             duplicate warnings about poor parameter qualifications.
             The function arguments are the same as the decl_arguments
             The function arguments are the same as the decl_arguments
             without the top level cv qualifiers.  */
             without the top level cv qualifiers.  */
          type = TREE_TYPE (type);
          type = TREE_TYPE (type);
        }
        }
      tsubst (type, gen_args, tf_warning_or_error, d);
      tsubst (type, gen_args, tf_warning_or_error, d);
 
 
      pop_access_scope (d);
      pop_access_scope (d);
    }
    }
 
 
  /* Defer all other templates, unless we have been explicitly
  /* Defer all other templates, unless we have been explicitly
     forbidden from doing so.  */
     forbidden from doing so.  */
  if (/* If there is no definition, we cannot instantiate the
  if (/* If there is no definition, we cannot instantiate the
         template.  */
         template.  */
      ! pattern_defined
      ! pattern_defined
      /* If it's OK to postpone instantiation, do so.  */
      /* If it's OK to postpone instantiation, do so.  */
      || defer_ok
      || defer_ok
      /* If this is a static data member that will be defined
      /* If this is a static data member that will be defined
         elsewhere, we don't want to instantiate the entire data
         elsewhere, we don't want to instantiate the entire data
         member, but we do want to instantiate the initializer so that
         member, but we do want to instantiate the initializer so that
         we can substitute that elsewhere.  */
         we can substitute that elsewhere.  */
      || (external_p && TREE_CODE (d) == VAR_DECL))
      || (external_p && TREE_CODE (d) == VAR_DECL))
    {
    {
      /* The definition of the static data member is now required so
      /* The definition of the static data member is now required so
         we must substitute the initializer.  */
         we must substitute the initializer.  */
      if (TREE_CODE (d) == VAR_DECL
      if (TREE_CODE (d) == VAR_DECL
          && !DECL_INITIAL (d)
          && !DECL_INITIAL (d)
          && DECL_INITIAL (code_pattern))
          && DECL_INITIAL (code_pattern))
        {
        {
          tree ns;
          tree ns;
          tree init;
          tree init;
 
 
          ns = decl_namespace_context (d);
          ns = decl_namespace_context (d);
          push_nested_namespace (ns);
          push_nested_namespace (ns);
          push_nested_class (DECL_CONTEXT (d));
          push_nested_class (DECL_CONTEXT (d));
          init = tsubst_expr (DECL_INITIAL (code_pattern),
          init = tsubst_expr (DECL_INITIAL (code_pattern),
                              args,
                              args,
                              tf_warning_or_error, NULL_TREE,
                              tf_warning_or_error, NULL_TREE,
                              /*integral_constant_expression_p=*/false);
                              /*integral_constant_expression_p=*/false);
          cp_finish_decl (d, init, /*init_const_expr_p=*/false,
          cp_finish_decl (d, init, /*init_const_expr_p=*/false,
                          /*asmspec_tree=*/NULL_TREE,
                          /*asmspec_tree=*/NULL_TREE,
                          LOOKUP_ONLYCONVERTING);
                          LOOKUP_ONLYCONVERTING);
          pop_nested_class ();
          pop_nested_class ();
          pop_nested_namespace (ns);
          pop_nested_namespace (ns);
        }
        }
 
 
      /* We restore the source position here because it's used by
      /* We restore the source position here because it's used by
         add_pending_template.  */
         add_pending_template.  */
      input_location = saved_loc;
      input_location = saved_loc;
 
 
      if (at_eof && !pattern_defined
      if (at_eof && !pattern_defined
          && DECL_EXPLICIT_INSTANTIATION (d)
          && DECL_EXPLICIT_INSTANTIATION (d)
          && DECL_NOT_REALLY_EXTERN (d))
          && DECL_NOT_REALLY_EXTERN (d))
        /* [temp.explicit]
        /* [temp.explicit]
 
 
           The definition of a non-exported function template, a
           The definition of a non-exported function template, a
           non-exported member function template, or a non-exported
           non-exported member function template, or a non-exported
           member function or static data member of a class template
           member function or static data member of a class template
           shall be present in every translation unit in which it is
           shall be present in every translation unit in which it is
           explicitly instantiated.  */
           explicitly instantiated.  */
        permerror (input_location,  "explicit instantiation of %qD "
        permerror (input_location,  "explicit instantiation of %qD "
                   "but no definition available", d);
                   "but no definition available", d);
 
 
      /* ??? Historically, we have instantiated inline functions, even
      /* ??? Historically, we have instantiated inline functions, even
         when marked as "extern template".  */
         when marked as "extern template".  */
      if (!(external_p && TREE_CODE (d) == VAR_DECL))
      if (!(external_p && TREE_CODE (d) == VAR_DECL))
        add_pending_template (d);
        add_pending_template (d);
      goto out;
      goto out;
    }
    }
  /* Tell the repository that D is available in this translation unit
  /* Tell the repository that D is available in this translation unit
     -- and see if it is supposed to be instantiated here.  */
     -- and see if it is supposed to be instantiated here.  */
  if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
  if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
    {
    {
      /* In a PCH file, despite the fact that the repository hasn't
      /* In a PCH file, despite the fact that the repository hasn't
         requested instantiation in the PCH it is still possible that
         requested instantiation in the PCH it is still possible that
         an instantiation will be required in a file that includes the
         an instantiation will be required in a file that includes the
         PCH.  */
         PCH.  */
      if (pch_file)
      if (pch_file)
        add_pending_template (d);
        add_pending_template (d);
      /* Instantiate inline functions so that the inliner can do its
      /* Instantiate inline functions so that the inliner can do its
         job, even though we'll not be emitting a copy of this
         job, even though we'll not be emitting a copy of this
         function.  */
         function.  */
      if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
      if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
        goto out;
        goto out;
    }
    }
 
 
  need_push = !cfun || !global_bindings_p ();
  need_push = !cfun || !global_bindings_p ();
  if (need_push)
  if (need_push)
    push_to_top_level ();
    push_to_top_level ();
 
 
  /* Mark D as instantiated so that recursive calls to
  /* Mark D as instantiated so that recursive calls to
     instantiate_decl do not try to instantiate it again.  */
     instantiate_decl do not try to instantiate it again.  */
  DECL_TEMPLATE_INSTANTIATED (d) = 1;
  DECL_TEMPLATE_INSTANTIATED (d) = 1;
 
 
  /* Regenerate the declaration in case the template has been modified
  /* Regenerate the declaration in case the template has been modified
     by a subsequent redeclaration.  */
     by a subsequent redeclaration.  */
  regenerate_decl_from_template (d, td);
  regenerate_decl_from_template (d, td);
 
 
  /* We already set the file and line above.  Reset them now in case
  /* We already set the file and line above.  Reset them now in case
     they changed as a result of calling regenerate_decl_from_template.  */
     they changed as a result of calling regenerate_decl_from_template.  */
  input_location = DECL_SOURCE_LOCATION (d);
  input_location = DECL_SOURCE_LOCATION (d);
 
 
  if (TREE_CODE (d) == VAR_DECL)
  if (TREE_CODE (d) == VAR_DECL)
    {
    {
      tree init;
      tree init;
 
 
      /* Clear out DECL_RTL; whatever was there before may not be right
      /* Clear out DECL_RTL; whatever was there before may not be right
         since we've reset the type of the declaration.  */
         since we've reset the type of the declaration.  */
      SET_DECL_RTL (d, NULL_RTX);
      SET_DECL_RTL (d, NULL_RTX);
      DECL_IN_AGGR_P (d) = 0;
      DECL_IN_AGGR_P (d) = 0;
 
 
      /* The initializer is placed in DECL_INITIAL by
      /* The initializer is placed in DECL_INITIAL by
         regenerate_decl_from_template.  Pull it out so that
         regenerate_decl_from_template.  Pull it out so that
         cp_finish_decl can process it.  */
         cp_finish_decl can process it.  */
      init = DECL_INITIAL (d);
      init = DECL_INITIAL (d);
      DECL_INITIAL (d) = NULL_TREE;
      DECL_INITIAL (d) = NULL_TREE;
      DECL_INITIALIZED_P (d) = 0;
      DECL_INITIALIZED_P (d) = 0;
 
 
      /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
      /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
         initializer.  That function will defer actual emission until
         initializer.  That function will defer actual emission until
         we have a chance to determine linkage.  */
         we have a chance to determine linkage.  */
      DECL_EXTERNAL (d) = 0;
      DECL_EXTERNAL (d) = 0;
 
 
      /* Enter the scope of D so that access-checking works correctly.  */
      /* Enter the scope of D so that access-checking works correctly.  */
      push_nested_class (DECL_CONTEXT (d));
      push_nested_class (DECL_CONTEXT (d));
      cp_finish_decl (d, init, false, NULL_TREE, 0);
      cp_finish_decl (d, init, false, NULL_TREE, 0);
      pop_nested_class ();
      pop_nested_class ();
    }
    }
  else if (TREE_CODE (d) == FUNCTION_DECL)
  else if (TREE_CODE (d) == FUNCTION_DECL)
    {
    {
      htab_t saved_local_specializations;
      htab_t saved_local_specializations;
      tree subst_decl;
      tree subst_decl;
      tree tmpl_parm;
      tree tmpl_parm;
      tree spec_parm;
      tree spec_parm;
 
 
      /* Save away the current list, in case we are instantiating one
      /* Save away the current list, in case we are instantiating one
         template from within the body of another.  */
         template from within the body of another.  */
      saved_local_specializations = local_specializations;
      saved_local_specializations = local_specializations;
 
 
      /* Set up the list of local specializations.  */
      /* Set up the list of local specializations.  */
      local_specializations = htab_create (37,
      local_specializations = htab_create (37,
                                           hash_local_specialization,
                                           hash_local_specialization,
                                           eq_local_specializations,
                                           eq_local_specializations,
                                           NULL);
                                           NULL);
 
 
      /* Set up context.  */
      /* Set up context.  */
      start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
      start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
 
 
      /* Create substitution entries for the parameters.  */
      /* Create substitution entries for the parameters.  */
      subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
      subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
      tmpl_parm = DECL_ARGUMENTS (subst_decl);
      tmpl_parm = DECL_ARGUMENTS (subst_decl);
      spec_parm = DECL_ARGUMENTS (d);
      spec_parm = DECL_ARGUMENTS (d);
      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
        {
        {
          register_local_specialization (spec_parm, tmpl_parm);
          register_local_specialization (spec_parm, tmpl_parm);
          spec_parm = skip_artificial_parms_for (d, spec_parm);
          spec_parm = skip_artificial_parms_for (d, spec_parm);
          tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
          tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
        }
        }
      while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
      while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
        {
        {
          register_local_specialization (spec_parm, tmpl_parm);
          register_local_specialization (spec_parm, tmpl_parm);
          tmpl_parm = TREE_CHAIN (tmpl_parm);
          tmpl_parm = TREE_CHAIN (tmpl_parm);
          spec_parm = TREE_CHAIN (spec_parm);
          spec_parm = TREE_CHAIN (spec_parm);
        }
        }
      if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
      if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
        {
        {
          /* Register the (value) argument pack as a specialization of
          /* Register the (value) argument pack as a specialization of
             TMPL_PARM, then move on.  */
             TMPL_PARM, then move on.  */
          tree argpack = make_fnparm_pack (spec_parm);
          tree argpack = make_fnparm_pack (spec_parm);
          register_local_specialization (argpack, tmpl_parm);
          register_local_specialization (argpack, tmpl_parm);
          tmpl_parm = TREE_CHAIN (tmpl_parm);
          tmpl_parm = TREE_CHAIN (tmpl_parm);
          spec_parm = NULL_TREE;
          spec_parm = NULL_TREE;
        }
        }
      gcc_assert (!spec_parm);
      gcc_assert (!spec_parm);
 
 
      /* Substitute into the body of the function.  */
      /* Substitute into the body of the function.  */
      tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
      tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
                   tf_warning_or_error, tmpl,
                   tf_warning_or_error, tmpl,
                   /*integral_constant_expression_p=*/false);
                   /*integral_constant_expression_p=*/false);
 
 
      /* Set the current input_location to the end of the function
      /* Set the current input_location to the end of the function
         so that finish_function knows where we are.  */
         so that finish_function knows where we are.  */
      input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
      input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
 
 
      /* We don't need the local specializations any more.  */
      /* We don't need the local specializations any more.  */
      htab_delete (local_specializations);
      htab_delete (local_specializations);
      local_specializations = saved_local_specializations;
      local_specializations = saved_local_specializations;
 
 
      /* Finish the function.  */
      /* Finish the function.  */
      d = finish_function (0);
      d = finish_function (0);
      expand_or_defer_fn (d);
      expand_or_defer_fn (d);
    }
    }
 
 
  /* We're not deferring instantiation any more.  */
  /* We're not deferring instantiation any more.  */
  TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
  TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
 
 
  if (need_push)
  if (need_push)
    pop_from_top_level ();
    pop_from_top_level ();
 
 
out:
out:
  input_location = saved_loc;
  input_location = saved_loc;
  pop_deferring_access_checks ();
  pop_deferring_access_checks ();
  pop_tinst_level ();
  pop_tinst_level ();
 
 
  timevar_pop (TV_PARSE);
  timevar_pop (TV_PARSE);
 
 
  return d;
  return d;
}
}
 
 
/* Run through the list of templates that we wish we could
/* Run through the list of templates that we wish we could
   instantiate, and instantiate any we can.  RETRIES is the
   instantiate, and instantiate any we can.  RETRIES is the
   number of times we retry pending template instantiation.  */
   number of times we retry pending template instantiation.  */
 
 
void
void
instantiate_pending_templates (int retries)
instantiate_pending_templates (int retries)
{
{
  int reconsider;
  int reconsider;
  location_t saved_loc = input_location;
  location_t saved_loc = input_location;
 
 
  /* Instantiating templates may trigger vtable generation.  This in turn
  /* Instantiating templates may trigger vtable generation.  This in turn
     may require further template instantiations.  We place a limit here
     may require further template instantiations.  We place a limit here
     to avoid infinite loop.  */
     to avoid infinite loop.  */
  if (pending_templates && retries >= max_tinst_depth)
  if (pending_templates && retries >= max_tinst_depth)
    {
    {
      tree decl = pending_templates->tinst->decl;
      tree decl = pending_templates->tinst->decl;
 
 
      error ("template instantiation depth exceeds maximum of %d"
      error ("template instantiation depth exceeds maximum of %d"
             " instantiating %q+D, possibly from virtual table generation"
             " instantiating %q+D, possibly from virtual table generation"
             " (use -ftemplate-depth= to increase the maximum)",
             " (use -ftemplate-depth= to increase the maximum)",
             max_tinst_depth, decl);
             max_tinst_depth, decl);
      if (TREE_CODE (decl) == FUNCTION_DECL)
      if (TREE_CODE (decl) == FUNCTION_DECL)
        /* Pretend that we defined it.  */
        /* Pretend that we defined it.  */
        DECL_INITIAL (decl) = error_mark_node;
        DECL_INITIAL (decl) = error_mark_node;
      return;
      return;
    }
    }
 
 
  do
  do
    {
    {
      struct pending_template **t = &pending_templates;
      struct pending_template **t = &pending_templates;
      struct pending_template *last = NULL;
      struct pending_template *last = NULL;
      reconsider = 0;
      reconsider = 0;
      while (*t)
      while (*t)
        {
        {
          tree instantiation = reopen_tinst_level ((*t)->tinst);
          tree instantiation = reopen_tinst_level ((*t)->tinst);
          bool complete = false;
          bool complete = false;
 
 
          if (TYPE_P (instantiation))
          if (TYPE_P (instantiation))
            {
            {
              tree fn;
              tree fn;
 
 
              if (!COMPLETE_TYPE_P (instantiation))
              if (!COMPLETE_TYPE_P (instantiation))
                {
                {
                  instantiate_class_template (instantiation);
                  instantiate_class_template (instantiation);
                  if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
                  if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
                    for (fn = TYPE_METHODS (instantiation);
                    for (fn = TYPE_METHODS (instantiation);
                         fn;
                         fn;
                         fn = TREE_CHAIN (fn))
                         fn = TREE_CHAIN (fn))
                      if (! DECL_ARTIFICIAL (fn))
                      if (! DECL_ARTIFICIAL (fn))
                        instantiate_decl (fn,
                        instantiate_decl (fn,
                                          /*defer_ok=*/0,
                                          /*defer_ok=*/0,
                                          /*expl_inst_class_mem_p=*/false);
                                          /*expl_inst_class_mem_p=*/false);
                  if (COMPLETE_TYPE_P (instantiation))
                  if (COMPLETE_TYPE_P (instantiation))
                    reconsider = 1;
                    reconsider = 1;
                }
                }
 
 
              complete = COMPLETE_TYPE_P (instantiation);
              complete = COMPLETE_TYPE_P (instantiation);
            }
            }
          else
          else
            {
            {
              if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
              if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
                  && !DECL_TEMPLATE_INSTANTIATED (instantiation))
                  && !DECL_TEMPLATE_INSTANTIATED (instantiation))
                {
                {
                  instantiation
                  instantiation
                    = instantiate_decl (instantiation,
                    = instantiate_decl (instantiation,
                                        /*defer_ok=*/0,
                                        /*defer_ok=*/0,
                                        /*expl_inst_class_mem_p=*/false);
                                        /*expl_inst_class_mem_p=*/false);
                  if (DECL_TEMPLATE_INSTANTIATED (instantiation))
                  if (DECL_TEMPLATE_INSTANTIATED (instantiation))
                    reconsider = 1;
                    reconsider = 1;
                }
                }
 
 
              complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
              complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
                          || DECL_TEMPLATE_INSTANTIATED (instantiation));
                          || DECL_TEMPLATE_INSTANTIATED (instantiation));
            }
            }
 
 
          if (complete)
          if (complete)
            /* If INSTANTIATION has been instantiated, then we don't
            /* If INSTANTIATION has been instantiated, then we don't
               need to consider it again in the future.  */
               need to consider it again in the future.  */
            *t = (*t)->next;
            *t = (*t)->next;
          else
          else
            {
            {
              last = *t;
              last = *t;
              t = &(*t)->next;
              t = &(*t)->next;
            }
            }
          tinst_depth = 0;
          tinst_depth = 0;
          current_tinst_level = NULL;
          current_tinst_level = NULL;
        }
        }
      last_pending_template = last;
      last_pending_template = last;
    }
    }
  while (reconsider);
  while (reconsider);
 
 
  input_location = saved_loc;
  input_location = saved_loc;
}
}
 
 
/* Substitute ARGVEC into T, which is a list of initializers for
/* Substitute ARGVEC into T, which is a list of initializers for
   either base class or a non-static data member.  The TREE_PURPOSEs
   either base class or a non-static data member.  The TREE_PURPOSEs
   are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   instantiate_decl.  */
   instantiate_decl.  */
 
 
static tree
static tree
tsubst_initializer_list (tree t, tree argvec)
tsubst_initializer_list (tree t, tree argvec)
{
{
  tree inits = NULL_TREE;
  tree inits = NULL_TREE;
 
 
  for (; t; t = TREE_CHAIN (t))
  for (; t; t = TREE_CHAIN (t))
    {
    {
      tree decl;
      tree decl;
      tree init;
      tree init;
      tree expanded_bases = NULL_TREE;
      tree expanded_bases = NULL_TREE;
      tree expanded_arguments = NULL_TREE;
      tree expanded_arguments = NULL_TREE;
      int i, len = 1;
      int i, len = 1;
 
 
      if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
      if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
        {
        {
          tree expr;
          tree expr;
          tree arg;
          tree arg;
 
 
          /* Expand the base class expansion type into separate base
          /* Expand the base class expansion type into separate base
             classes.  */
             classes.  */
          expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
          expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
                                                 tf_warning_or_error,
                                                 tf_warning_or_error,
                                                 NULL_TREE);
                                                 NULL_TREE);
          if (expanded_bases == error_mark_node)
          if (expanded_bases == error_mark_node)
            continue;
            continue;
 
 
          /* We'll be building separate TREE_LISTs of arguments for
          /* We'll be building separate TREE_LISTs of arguments for
             each base.  */
             each base.  */
          len = TREE_VEC_LENGTH (expanded_bases);
          len = TREE_VEC_LENGTH (expanded_bases);
          expanded_arguments = make_tree_vec (len);
          expanded_arguments = make_tree_vec (len);
          for (i = 0; i < len; i++)
          for (i = 0; i < len; i++)
            TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
            TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
 
 
          /* Build a dummy EXPR_PACK_EXPANSION that will be used to
          /* Build a dummy EXPR_PACK_EXPANSION that will be used to
             expand each argument in the TREE_VALUE of t.  */
             expand each argument in the TREE_VALUE of t.  */
          expr = make_node (EXPR_PACK_EXPANSION);
          expr = make_node (EXPR_PACK_EXPANSION);
          PACK_EXPANSION_PARAMETER_PACKS (expr) =
          PACK_EXPANSION_PARAMETER_PACKS (expr) =
            PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
            PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
 
 
          if (TREE_VALUE (t) == void_type_node)
          if (TREE_VALUE (t) == void_type_node)
            /* VOID_TYPE_NODE is used to indicate
            /* VOID_TYPE_NODE is used to indicate
               value-initialization.  */
               value-initialization.  */
            {
            {
              for (i = 0; i < len; i++)
              for (i = 0; i < len; i++)
                TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
                TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
            }
            }
          else
          else
            {
            {
              /* Substitute parameter packs into each argument in the
              /* Substitute parameter packs into each argument in the
                 TREE_LIST.  */
                 TREE_LIST.  */
              in_base_initializer = 1;
              in_base_initializer = 1;
              for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
              for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
                {
                {
                  tree expanded_exprs;
                  tree expanded_exprs;
 
 
                  /* Expand the argument.  */
                  /* Expand the argument.  */
                  SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
                  SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
                  expanded_exprs
                  expanded_exprs
                    = tsubst_pack_expansion (expr, argvec,
                    = tsubst_pack_expansion (expr, argvec,
                                             tf_warning_or_error,
                                             tf_warning_or_error,
                                             NULL_TREE);
                                             NULL_TREE);
                  if (expanded_exprs == error_mark_node)
                  if (expanded_exprs == error_mark_node)
                    continue;
                    continue;
 
 
                  /* Prepend each of the expanded expressions to the
                  /* Prepend each of the expanded expressions to the
                     corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
                     corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
                  for (i = 0; i < len; i++)
                  for (i = 0; i < len; i++)
                    {
                    {
                      TREE_VEC_ELT (expanded_arguments, i) =
                      TREE_VEC_ELT (expanded_arguments, i) =
                        tree_cons (NULL_TREE,
                        tree_cons (NULL_TREE,
                                   TREE_VEC_ELT (expanded_exprs, i),
                                   TREE_VEC_ELT (expanded_exprs, i),
                                   TREE_VEC_ELT (expanded_arguments, i));
                                   TREE_VEC_ELT (expanded_arguments, i));
                    }
                    }
                }
                }
              in_base_initializer = 0;
              in_base_initializer = 0;
 
 
              /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
              /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
                 since we built them backwards.  */
                 since we built them backwards.  */
              for (i = 0; i < len; i++)
              for (i = 0; i < len; i++)
                {
                {
                  TREE_VEC_ELT (expanded_arguments, i) =
                  TREE_VEC_ELT (expanded_arguments, i) =
                    nreverse (TREE_VEC_ELT (expanded_arguments, i));
                    nreverse (TREE_VEC_ELT (expanded_arguments, i));
                }
                }
            }
            }
        }
        }
 
 
      for (i = 0; i < len; ++i)
      for (i = 0; i < len; ++i)
        {
        {
          if (expanded_bases)
          if (expanded_bases)
            {
            {
              decl = TREE_VEC_ELT (expanded_bases, i);
              decl = TREE_VEC_ELT (expanded_bases, i);
              decl = expand_member_init (decl);
              decl = expand_member_init (decl);
              init = TREE_VEC_ELT (expanded_arguments, i);
              init = TREE_VEC_ELT (expanded_arguments, i);
            }
            }
          else
          else
            {
            {
              decl = tsubst_copy (TREE_PURPOSE (t), argvec,
              decl = tsubst_copy (TREE_PURPOSE (t), argvec,
                                  tf_warning_or_error, NULL_TREE);
                                  tf_warning_or_error, NULL_TREE);
 
 
              decl = expand_member_init (decl);
              decl = expand_member_init (decl);
              if (decl && !DECL_P (decl))
              if (decl && !DECL_P (decl))
                in_base_initializer = 1;
                in_base_initializer = 1;
 
 
              init = tsubst_expr (TREE_VALUE (t), argvec,
              init = tsubst_expr (TREE_VALUE (t), argvec,
                                  tf_warning_or_error, NULL_TREE,
                                  tf_warning_or_error, NULL_TREE,
                                  /*integral_constant_expression_p=*/false);
                                  /*integral_constant_expression_p=*/false);
              in_base_initializer = 0;
              in_base_initializer = 0;
            }
            }
 
 
          if (decl)
          if (decl)
            {
            {
              init = build_tree_list (decl, init);
              init = build_tree_list (decl, init);
              TREE_CHAIN (init) = inits;
              TREE_CHAIN (init) = inits;
              inits = init;
              inits = init;
            }
            }
        }
        }
    }
    }
  return inits;
  return inits;
}
}
 
 
/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL.  */
/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL.  */
 
 
static void
static void
set_current_access_from_decl (tree decl)
set_current_access_from_decl (tree decl)
{
{
  if (TREE_PRIVATE (decl))
  if (TREE_PRIVATE (decl))
    current_access_specifier = access_private_node;
    current_access_specifier = access_private_node;
  else if (TREE_PROTECTED (decl))
  else if (TREE_PROTECTED (decl))
    current_access_specifier = access_protected_node;
    current_access_specifier = access_protected_node;
  else
  else
    current_access_specifier = access_public_node;
    current_access_specifier = access_public_node;
}
}
 
 
/* Instantiate an enumerated type.  TAG is the template type, NEWTAG
/* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   is the instantiation (which should have been created with
   is the instantiation (which should have been created with
   start_enum) and ARGS are the template arguments to use.  */
   start_enum) and ARGS are the template arguments to use.  */
 
 
static void
static void
tsubst_enum (tree tag, tree newtag, tree args)
tsubst_enum (tree tag, tree newtag, tree args)
{
{
  tree e;
  tree e;
 
 
  for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
  for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
    {
    {
      tree value;
      tree value;
      tree decl;
      tree decl;
 
 
      decl = TREE_VALUE (e);
      decl = TREE_VALUE (e);
      /* Note that in a template enum, the TREE_VALUE is the
      /* Note that in a template enum, the TREE_VALUE is the
         CONST_DECL, not the corresponding INTEGER_CST.  */
         CONST_DECL, not the corresponding INTEGER_CST.  */
      value = tsubst_expr (DECL_INITIAL (decl),
      value = tsubst_expr (DECL_INITIAL (decl),
                           args, tf_warning_or_error, NULL_TREE,
                           args, tf_warning_or_error, NULL_TREE,
                           /*integral_constant_expression_p=*/true);
                           /*integral_constant_expression_p=*/true);
 
 
      /* Give this enumeration constant the correct access.  */
      /* Give this enumeration constant the correct access.  */
      set_current_access_from_decl (decl);
      set_current_access_from_decl (decl);
 
 
      /* Actually build the enumerator itself.  */
      /* Actually build the enumerator itself.  */
      build_enumerator (DECL_NAME (decl), value, newtag);
      build_enumerator (DECL_NAME (decl), value, newtag);
    }
    }
 
 
  finish_enum (newtag);
  finish_enum (newtag);
  DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
  DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
    = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
    = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
}
}
 
 
/* DECL is a FUNCTION_DECL that is a template specialization.  Return
/* DECL is a FUNCTION_DECL that is a template specialization.  Return
   its type -- but without substituting the innermost set of template
   its type -- but without substituting the innermost set of template
   arguments.  So, innermost set of template parameters will appear in
   arguments.  So, innermost set of template parameters will appear in
   the type.  */
   the type.  */
 
 
tree
tree
get_mostly_instantiated_function_type (tree decl)
get_mostly_instantiated_function_type (tree decl)
{
{
  tree fn_type;
  tree fn_type;
  tree tmpl;
  tree tmpl;
  tree targs;
  tree targs;
  tree tparms;
  tree tparms;
  int parm_depth;
  int parm_depth;
 
 
  tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
  tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
  targs = DECL_TI_ARGS (decl);
  targs = DECL_TI_ARGS (decl);
  tparms = DECL_TEMPLATE_PARMS (tmpl);
  tparms = DECL_TEMPLATE_PARMS (tmpl);
  parm_depth = TMPL_PARMS_DEPTH (tparms);
  parm_depth = TMPL_PARMS_DEPTH (tparms);
 
 
  /* There should be as many levels of arguments as there are levels
  /* There should be as many levels of arguments as there are levels
     of parameters.  */
     of parameters.  */
  gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
  gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
 
 
  fn_type = TREE_TYPE (tmpl);
  fn_type = TREE_TYPE (tmpl);
 
 
  if (parm_depth == 1)
  if (parm_depth == 1)
    /* No substitution is necessary.  */
    /* No substitution is necessary.  */
    ;
    ;
  else
  else
    {
    {
      int i, save_access_control;
      int i, save_access_control;
      tree partial_args;
      tree partial_args;
 
 
      /* Replace the innermost level of the TARGS with NULL_TREEs to
      /* Replace the innermost level of the TARGS with NULL_TREEs to
         let tsubst know not to substitute for those parameters.  */
         let tsubst know not to substitute for those parameters.  */
      partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
      partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
      for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
      for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
        SET_TMPL_ARGS_LEVEL (partial_args, i,
        SET_TMPL_ARGS_LEVEL (partial_args, i,
                             TMPL_ARGS_LEVEL (targs, i));
                             TMPL_ARGS_LEVEL (targs, i));
      SET_TMPL_ARGS_LEVEL (partial_args,
      SET_TMPL_ARGS_LEVEL (partial_args,
                           TMPL_ARGS_DEPTH (targs),
                           TMPL_ARGS_DEPTH (targs),
                           make_tree_vec (DECL_NTPARMS (tmpl)));
                           make_tree_vec (DECL_NTPARMS (tmpl)));
 
 
      /* Disable access control as this function is used only during
      /* Disable access control as this function is used only during
         name-mangling.  */
         name-mangling.  */
      save_access_control = flag_access_control;
      save_access_control = flag_access_control;
      flag_access_control = 0;
      flag_access_control = 0;
 
 
      ++processing_template_decl;
      ++processing_template_decl;
      /* Now, do the (partial) substitution to figure out the
      /* Now, do the (partial) substitution to figure out the
         appropriate function type.  */
         appropriate function type.  */
      fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
      fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
      --processing_template_decl;
      --processing_template_decl;
 
 
      /* Substitute into the template parameters to obtain the real
      /* Substitute into the template parameters to obtain the real
         innermost set of parameters.  This step is important if the
         innermost set of parameters.  This step is important if the
         innermost set of template parameters contains value
         innermost set of template parameters contains value
         parameters whose types depend on outer template parameters.  */
         parameters whose types depend on outer template parameters.  */
      TREE_VEC_LENGTH (partial_args)--;
      TREE_VEC_LENGTH (partial_args)--;
      tparms = tsubst_template_parms (tparms, partial_args, tf_error);
      tparms = tsubst_template_parms (tparms, partial_args, tf_error);
 
 
      flag_access_control = save_access_control;
      flag_access_control = save_access_control;
    }
    }
 
 
  return fn_type;
  return fn_type;
}
}
 
 
/* Return truthvalue if we're processing a template different from
/* Return truthvalue if we're processing a template different from
   the last one involved in diagnostics.  */
   the last one involved in diagnostics.  */
int
int
problematic_instantiation_changed (void)
problematic_instantiation_changed (void)
{
{
  return last_template_error_tick != tinst_level_tick;
  return last_template_error_tick != tinst_level_tick;
}
}
 
 
/* Remember current template involved in diagnostics.  */
/* Remember current template involved in diagnostics.  */
void
void
record_last_problematic_instantiation (void)
record_last_problematic_instantiation (void)
{
{
  last_template_error_tick = tinst_level_tick;
  last_template_error_tick = tinst_level_tick;
}
}
 
 
struct tinst_level *
struct tinst_level *
current_instantiation (void)
current_instantiation (void)
{
{
  return current_tinst_level;
  return current_tinst_level;
}
}
 
 
/* [temp.param] Check that template non-type parm TYPE is of an allowable
/* [temp.param] Check that template non-type parm TYPE is of an allowable
   type. Return zero for ok, nonzero for disallowed. Issue error and
   type. Return zero for ok, nonzero for disallowed. Issue error and
   warning messages under control of COMPLAIN.  */
   warning messages under control of COMPLAIN.  */
 
 
static int
static int
invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
{
{
  if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
    return 0;
    return 0;
  else if (POINTER_TYPE_P (type))
  else if (POINTER_TYPE_P (type))
    return 0;
    return 0;
  else if (TYPE_PTR_TO_MEMBER_P (type))
  else if (TYPE_PTR_TO_MEMBER_P (type))
    return 0;
    return 0;
  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
    return 0;
    return 0;
  else if (TREE_CODE (type) == TYPENAME_TYPE)
  else if (TREE_CODE (type) == TYPENAME_TYPE)
    return 0;
    return 0;
 
 
  if (complain & tf_error)
  if (complain & tf_error)
    error ("%q#T is not a valid type for a template constant parameter", type);
    error ("%q#T is not a valid type for a template constant parameter", type);
  return 1;
  return 1;
}
}
 
 
/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
 
 
static bool
static bool
dependent_type_p_r (tree type)
dependent_type_p_r (tree type)
{
{
  tree scope;
  tree scope;
 
 
  /* [temp.dep.type]
  /* [temp.dep.type]
 
 
     A type is dependent if it is:
     A type is dependent if it is:
 
 
     -- a template parameter. Template template parameters are types
     -- a template parameter. Template template parameters are types
        for us (since TYPE_P holds true for them) so we handle
        for us (since TYPE_P holds true for them) so we handle
        them here.  */
        them here.  */
  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
      || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
      || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
    return true;
    return true;
  /* -- a qualified-id with a nested-name-specifier which contains a
  /* -- a qualified-id with a nested-name-specifier which contains a
        class-name that names a dependent type or whose unqualified-id
        class-name that names a dependent type or whose unqualified-id
        names a dependent type.  */
        names a dependent type.  */
  if (TREE_CODE (type) == TYPENAME_TYPE)
  if (TREE_CODE (type) == TYPENAME_TYPE)
    return true;
    return true;
  /* -- a cv-qualified type where the cv-unqualified type is
  /* -- a cv-qualified type where the cv-unqualified type is
        dependent.  */
        dependent.  */
  type = TYPE_MAIN_VARIANT (type);
  type = TYPE_MAIN_VARIANT (type);
  /* -- a compound type constructed from any dependent type.  */
  /* -- a compound type constructed from any dependent type.  */
  if (TYPE_PTR_TO_MEMBER_P (type))
  if (TYPE_PTR_TO_MEMBER_P (type))
    return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
    return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
            || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
            || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
                                           (type)));
                                           (type)));
  else if (TREE_CODE (type) == POINTER_TYPE
  else if (TREE_CODE (type) == POINTER_TYPE
           || TREE_CODE (type) == REFERENCE_TYPE)
           || TREE_CODE (type) == REFERENCE_TYPE)
    return dependent_type_p (TREE_TYPE (type));
    return dependent_type_p (TREE_TYPE (type));
  else if (TREE_CODE (type) == FUNCTION_TYPE
  else if (TREE_CODE (type) == FUNCTION_TYPE
           || TREE_CODE (type) == METHOD_TYPE)
           || TREE_CODE (type) == METHOD_TYPE)
    {
    {
      tree arg_type;
      tree arg_type;
 
 
      if (dependent_type_p (TREE_TYPE (type)))
      if (dependent_type_p (TREE_TYPE (type)))
        return true;
        return true;
      for (arg_type = TYPE_ARG_TYPES (type);
      for (arg_type = TYPE_ARG_TYPES (type);
           arg_type;
           arg_type;
           arg_type = TREE_CHAIN (arg_type))
           arg_type = TREE_CHAIN (arg_type))
        if (dependent_type_p (TREE_VALUE (arg_type)))
        if (dependent_type_p (TREE_VALUE (arg_type)))
          return true;
          return true;
      return false;
      return false;
    }
    }
  /* -- an array type constructed from any dependent type or whose
  /* -- an array type constructed from any dependent type or whose
        size is specified by a constant expression that is
        size is specified by a constant expression that is
        value-dependent.  */
        value-dependent.  */
  if (TREE_CODE (type) == ARRAY_TYPE)
  if (TREE_CODE (type) == ARRAY_TYPE)
    {
    {
      if (TYPE_DOMAIN (type)
      if (TYPE_DOMAIN (type)
          && dependent_type_p (TYPE_DOMAIN (type)))
          && dependent_type_p (TYPE_DOMAIN (type)))
        return true;
        return true;
      return dependent_type_p (TREE_TYPE (type));
      return dependent_type_p (TREE_TYPE (type));
    }
    }
  else if (TREE_CODE (type) == INTEGER_TYPE
  else if (TREE_CODE (type) == INTEGER_TYPE
           && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
           && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
    {
    {
      /* If this is the TYPE_DOMAIN of an array type, consider it
      /* If this is the TYPE_DOMAIN of an array type, consider it
         dependent.  We already checked for value-dependence in
         dependent.  We already checked for value-dependence in
         compute_array_index_type.  */
         compute_array_index_type.  */
      return type_dependent_expression_p (TYPE_MAX_VALUE (type));
      return type_dependent_expression_p (TYPE_MAX_VALUE (type));
    }
    }
 
 
  /* -- a template-id in which either the template name is a template
  /* -- a template-id in which either the template name is a template
     parameter ...  */
     parameter ...  */
  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    return true;
    return true;
  /* ... or any of the template arguments is a dependent type or
  /* ... or any of the template arguments is a dependent type or
        an expression that is type-dependent or value-dependent.  */
        an expression that is type-dependent or value-dependent.  */
  else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
  else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
           && (any_dependent_template_arguments_p
           && (any_dependent_template_arguments_p
               (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
               (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
    return true;
    return true;
 
 
  /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
  /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
     argument of the `typeof' expression is not type-dependent, then
     argument of the `typeof' expression is not type-dependent, then
     it should already been have resolved.  */
     it should already been have resolved.  */
  if (TREE_CODE (type) == TYPEOF_TYPE
  if (TREE_CODE (type) == TYPEOF_TYPE
      || TREE_CODE (type) == DECLTYPE_TYPE)
      || TREE_CODE (type) == DECLTYPE_TYPE)
    return true;
    return true;
 
 
  /* A template argument pack is dependent if any of its packed
  /* A template argument pack is dependent if any of its packed
     arguments are.  */
     arguments are.  */
  if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
  if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
    {
    {
      tree args = ARGUMENT_PACK_ARGS (type);
      tree args = ARGUMENT_PACK_ARGS (type);
      int i, len = TREE_VEC_LENGTH (args);
      int i, len = TREE_VEC_LENGTH (args);
      for (i = 0; i < len; ++i)
      for (i = 0; i < len; ++i)
        if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
        if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
          return true;
          return true;
    }
    }
 
 
  /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
  /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
     be template parameters.  */
     be template parameters.  */
  if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
  if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
    return true;
    return true;
 
 
  /* The standard does not specifically mention types that are local
  /* The standard does not specifically mention types that are local
     to template functions or local classes, but they should be
     to template functions or local classes, but they should be
     considered dependent too.  For example:
     considered dependent too.  For example:
 
 
       template <int I> void f() {
       template <int I> void f() {
         enum E { a = I };
         enum E { a = I };
         S<sizeof (E)> s;
         S<sizeof (E)> s;
       }
       }
 
 
     The size of `E' cannot be known until the value of `I' has been
     The size of `E' cannot be known until the value of `I' has been
     determined.  Therefore, `E' must be considered dependent.  */
     determined.  Therefore, `E' must be considered dependent.  */
  scope = TYPE_CONTEXT (type);
  scope = TYPE_CONTEXT (type);
  if (scope && TYPE_P (scope))
  if (scope && TYPE_P (scope))
    return dependent_type_p (scope);
    return dependent_type_p (scope);
  else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
  else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
    return type_dependent_expression_p (scope);
    return type_dependent_expression_p (scope);
 
 
  /* Other types are non-dependent.  */
  /* Other types are non-dependent.  */
  return false;
  return false;
}
}
 
 
/* Returns TRUE if TYPE is dependent, in the sense of
/* Returns TRUE if TYPE is dependent, in the sense of
   [temp.dep.type].  */
   [temp.dep.type].  */
 
 
bool
bool
dependent_type_p (tree type)
dependent_type_p (tree type)
{
{
  /* If there are no template parameters in scope, then there can't be
  /* If there are no template parameters in scope, then there can't be
     any dependent types.  */
     any dependent types.  */
  if (!processing_template_decl)
  if (!processing_template_decl)
    {
    {
      /* If we are not processing a template, then nobody should be
      /* If we are not processing a template, then nobody should be
         providing us with a dependent type.  */
         providing us with a dependent type.  */
      gcc_assert (type);
      gcc_assert (type);
      gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
      gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
      return false;
      return false;
    }
    }
 
 
  /* If the type is NULL, we have not computed a type for the entity
  /* If the type is NULL, we have not computed a type for the entity
     in question; in that case, the type is dependent.  */
     in question; in that case, the type is dependent.  */
  if (!type)
  if (!type)
    return true;
    return true;
 
 
  /* Erroneous types can be considered non-dependent.  */
  /* Erroneous types can be considered non-dependent.  */
  if (type == error_mark_node)
  if (type == error_mark_node)
    return false;
    return false;
 
 
  /* If we have not already computed the appropriate value for TYPE,
  /* If we have not already computed the appropriate value for TYPE,
     do so now.  */
     do so now.  */
  if (!TYPE_DEPENDENT_P_VALID (type))
  if (!TYPE_DEPENDENT_P_VALID (type))
    {
    {
      TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
      TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
      TYPE_DEPENDENT_P_VALID (type) = 1;
      TYPE_DEPENDENT_P_VALID (type) = 1;
    }
    }
 
 
  return TYPE_DEPENDENT_P (type);
  return TYPE_DEPENDENT_P (type);
}
}
 
 
/* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
/* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   lookup.  In other words, a dependent type that is not the current
   lookup.  In other words, a dependent type that is not the current
   instantiation.  */
   instantiation.  */
 
 
bool
bool
dependent_scope_p (tree scope)
dependent_scope_p (tree scope)
{
{
  return (scope && TYPE_P (scope) && dependent_type_p (scope)
  return (scope && TYPE_P (scope) && dependent_type_p (scope)
          && !currently_open_class (scope));
          && !currently_open_class (scope));
}
}
 
 
/* Returns TRUE if EXPRESSION is dependent, according to CRITERION.  */
/* Returns TRUE if EXPRESSION is dependent, according to CRITERION.  */
 
 
static bool
static bool
dependent_scope_ref_p (tree expression, bool criterion (tree))
dependent_scope_ref_p (tree expression, bool criterion (tree))
{
{
  tree scope;
  tree scope;
  tree name;
  tree name;
 
 
  gcc_assert (TREE_CODE (expression) == SCOPE_REF);
  gcc_assert (TREE_CODE (expression) == SCOPE_REF);
 
 
  if (!TYPE_P (TREE_OPERAND (expression, 0)))
  if (!TYPE_P (TREE_OPERAND (expression, 0)))
    return true;
    return true;
 
 
  scope = TREE_OPERAND (expression, 0);
  scope = TREE_OPERAND (expression, 0);
  name = TREE_OPERAND (expression, 1);
  name = TREE_OPERAND (expression, 1);
 
 
  /* [temp.dep.expr]
  /* [temp.dep.expr]
 
 
     An id-expression is type-dependent if it contains a
     An id-expression is type-dependent if it contains a
     nested-name-specifier that contains a class-name that names a
     nested-name-specifier that contains a class-name that names a
     dependent type.  */
     dependent type.  */
  /* The suggested resolution to Core Issue 224 implies that if the
  /* The suggested resolution to Core Issue 224 implies that if the
     qualifying type is the current class, then we must peek
     qualifying type is the current class, then we must peek
     inside it.  */
     inside it.  */
  if (DECL_P (name)
  if (DECL_P (name)
      && currently_open_class (scope)
      && currently_open_class (scope)
      && !criterion (name))
      && !criterion (name))
    return false;
    return false;
  if (dependent_type_p (scope))
  if (dependent_type_p (scope))
    return true;
    return true;
 
 
  return false;
  return false;
}
}
 
 
/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   expression.  */
   expression.  */
 
 
bool
bool
value_dependent_expression_p (tree expression)
value_dependent_expression_p (tree expression)
{
{
  if (!processing_template_decl)
  if (!processing_template_decl)
    return false;
    return false;
 
 
  /* A name declared with a dependent type.  */
  /* A name declared with a dependent type.  */
  if (DECL_P (expression) && type_dependent_expression_p (expression))
  if (DECL_P (expression) && type_dependent_expression_p (expression))
    return true;
    return true;
 
 
  switch (TREE_CODE (expression))
  switch (TREE_CODE (expression))
    {
    {
    case IDENTIFIER_NODE:
    case IDENTIFIER_NODE:
      /* A name that has not been looked up -- must be dependent.  */
      /* A name that has not been looked up -- must be dependent.  */
      return true;
      return true;
 
 
    case TEMPLATE_PARM_INDEX:
    case TEMPLATE_PARM_INDEX:
      /* A non-type template parm.  */
      /* A non-type template parm.  */
      return true;
      return true;
 
 
    case CONST_DECL:
    case CONST_DECL:
      /* A non-type template parm.  */
      /* A non-type template parm.  */
      if (DECL_TEMPLATE_PARM_P (expression))
      if (DECL_TEMPLATE_PARM_P (expression))
        return true;
        return true;
      return value_dependent_expression_p (DECL_INITIAL (expression));
      return value_dependent_expression_p (DECL_INITIAL (expression));
 
 
    case VAR_DECL:
    case VAR_DECL:
       /* A constant with integral or enumeration type and is initialized
       /* A constant with integral or enumeration type and is initialized
          with an expression that is value-dependent.  */
          with an expression that is value-dependent.  */
      if (DECL_INITIAL (expression)
      if (DECL_INITIAL (expression)
          && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
          && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
          && value_dependent_expression_p (DECL_INITIAL (expression)))
          && value_dependent_expression_p (DECL_INITIAL (expression)))
        return true;
        return true;
      return false;
      return false;
 
 
    case DYNAMIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case CONST_CAST_EXPR:
    case CONST_CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CAST_EXPR:
    case CAST_EXPR:
      /* These expressions are value-dependent if the type to which
      /* These expressions are value-dependent if the type to which
         the cast occurs is dependent or the expression being casted
         the cast occurs is dependent or the expression being casted
         is value-dependent.  */
         is value-dependent.  */
      {
      {
        tree type = TREE_TYPE (expression);
        tree type = TREE_TYPE (expression);
 
 
        if (dependent_type_p (type))
        if (dependent_type_p (type))
          return true;
          return true;
 
 
        /* A functional cast has a list of operands.  */
        /* A functional cast has a list of operands.  */
        expression = TREE_OPERAND (expression, 0);
        expression = TREE_OPERAND (expression, 0);
        if (!expression)
        if (!expression)
          {
          {
            /* If there are no operands, it must be an expression such
            /* If there are no operands, it must be an expression such
               as "int()". This should not happen for aggregate types
               as "int()". This should not happen for aggregate types
               because it would form non-constant expressions.  */
               because it would form non-constant expressions.  */
            gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
            gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
 
 
            return false;
            return false;
          }
          }
 
 
        if (TREE_CODE (expression) == TREE_LIST)
        if (TREE_CODE (expression) == TREE_LIST)
          return any_value_dependent_elements_p (expression);
          return any_value_dependent_elements_p (expression);
 
 
        return value_dependent_expression_p (expression);
        return value_dependent_expression_p (expression);
      }
      }
 
 
    case SIZEOF_EXPR:
    case SIZEOF_EXPR:
    case ALIGNOF_EXPR:
    case ALIGNOF_EXPR:
      /* A `sizeof' expression is value-dependent if the operand is
      /* A `sizeof' expression is value-dependent if the operand is
         type-dependent or is a pack expansion.  */
         type-dependent or is a pack expansion.  */
      expression = TREE_OPERAND (expression, 0);
      expression = TREE_OPERAND (expression, 0);
      if (PACK_EXPANSION_P (expression))
      if (PACK_EXPANSION_P (expression))
        return true;
        return true;
      else if (TYPE_P (expression))
      else if (TYPE_P (expression))
        return dependent_type_p (expression);
        return dependent_type_p (expression);
      return type_dependent_expression_p (expression);
      return type_dependent_expression_p (expression);
 
 
    case SCOPE_REF:
    case SCOPE_REF:
      return dependent_scope_ref_p (expression, value_dependent_expression_p);
      return dependent_scope_ref_p (expression, value_dependent_expression_p);
 
 
    case COMPONENT_REF:
    case COMPONENT_REF:
      return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
      return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
              || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
              || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
 
 
    case CALL_EXPR:
    case CALL_EXPR:
      /* A CALL_EXPR may appear in a constant expression if it is a
      /* A CALL_EXPR may appear in a constant expression if it is a
         call to a builtin function, e.g., __builtin_constant_p.  All
         call to a builtin function, e.g., __builtin_constant_p.  All
         such calls are value-dependent.  */
         such calls are value-dependent.  */
      return true;
      return true;
 
 
    case NONTYPE_ARGUMENT_PACK:
    case NONTYPE_ARGUMENT_PACK:
      /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
      /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
         is value-dependent.  */
         is value-dependent.  */
      {
      {
        tree values = ARGUMENT_PACK_ARGS (expression);
        tree values = ARGUMENT_PACK_ARGS (expression);
        int i, len = TREE_VEC_LENGTH (values);
        int i, len = TREE_VEC_LENGTH (values);
 
 
        for (i = 0; i < len; ++i)
        for (i = 0; i < len; ++i)
          if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
          if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
            return true;
            return true;
 
 
        return false;
        return false;
      }
      }
 
 
    case TRAIT_EXPR:
    case TRAIT_EXPR:
      {
      {
        tree type2 = TRAIT_EXPR_TYPE2 (expression);
        tree type2 = TRAIT_EXPR_TYPE2 (expression);
        return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
        return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
                || (type2 ? dependent_type_p (type2) : false));
                || (type2 ? dependent_type_p (type2) : false));
      }
      }
 
 
    case MODOP_EXPR:
    case MODOP_EXPR:
      return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
      return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
              || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
              || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
 
 
    case ADDR_EXPR:
    case ADDR_EXPR:
      {
      {
        tree op = TREE_OPERAND (expression, 0);
        tree op = TREE_OPERAND (expression, 0);
        return (value_dependent_expression_p (op)
        return (value_dependent_expression_p (op)
                || has_value_dependent_address (op));
                || has_value_dependent_address (op));
      }
      }
 
 
    default:
    default:
      /* A constant expression is value-dependent if any subexpression is
      /* A constant expression is value-dependent if any subexpression is
         value-dependent.  */
         value-dependent.  */
      switch (TREE_CODE_CLASS (TREE_CODE (expression)))
      switch (TREE_CODE_CLASS (TREE_CODE (expression)))
        {
        {
        case tcc_reference:
        case tcc_reference:
        case tcc_unary:
        case tcc_unary:
          return (value_dependent_expression_p
          return (value_dependent_expression_p
                  (TREE_OPERAND (expression, 0)));
                  (TREE_OPERAND (expression, 0)));
 
 
        case tcc_comparison:
        case tcc_comparison:
        case tcc_binary:
        case tcc_binary:
          return ((value_dependent_expression_p
          return ((value_dependent_expression_p
                   (TREE_OPERAND (expression, 0)))
                   (TREE_OPERAND (expression, 0)))
                  || (value_dependent_expression_p
                  || (value_dependent_expression_p
                      (TREE_OPERAND (expression, 1))));
                      (TREE_OPERAND (expression, 1))));
 
 
        case tcc_expression:
        case tcc_expression:
        case tcc_vl_exp:
        case tcc_vl_exp:
          {
          {
            int i;
            int i;
            for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
            for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
              /* In some cases, some of the operands may be missing.
              /* In some cases, some of the operands may be missing.
                 (For example, in the case of PREDECREMENT_EXPR, the
                 (For example, in the case of PREDECREMENT_EXPR, the
                 amount to increment by may be missing.)  That doesn't
                 amount to increment by may be missing.)  That doesn't
                 make the expression dependent.  */
                 make the expression dependent.  */
              if (TREE_OPERAND (expression, i)
              if (TREE_OPERAND (expression, i)
                  && (value_dependent_expression_p
                  && (value_dependent_expression_p
                      (TREE_OPERAND (expression, i))))
                      (TREE_OPERAND (expression, i))))
                return true;
                return true;
            return false;
            return false;
          }
          }
 
 
        default:
        default:
          break;
          break;
        }
        }
    }
    }
 
 
  /* The expression is not value-dependent.  */
  /* The expression is not value-dependent.  */
  return false;
  return false;
}
}
 
 
/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   [temp.dep.expr].  */
   [temp.dep.expr].  */
 
 
bool
bool
type_dependent_expression_p (tree expression)
type_dependent_expression_p (tree expression)
{
{
  if (!processing_template_decl)
  if (!processing_template_decl)
    return false;
    return false;
 
 
  if (expression == error_mark_node)
  if (expression == error_mark_node)
    return false;
    return false;
 
 
  /* An unresolved name is always dependent.  */
  /* An unresolved name is always dependent.  */
  if (TREE_CODE (expression) == IDENTIFIER_NODE
  if (TREE_CODE (expression) == IDENTIFIER_NODE
      || TREE_CODE (expression) == USING_DECL)
      || TREE_CODE (expression) == USING_DECL)
    return true;
    return true;
 
 
  /* Some expression forms are never type-dependent.  */
  /* Some expression forms are never type-dependent.  */
  if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
  if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
      || TREE_CODE (expression) == SIZEOF_EXPR
      || TREE_CODE (expression) == SIZEOF_EXPR
      || TREE_CODE (expression) == ALIGNOF_EXPR
      || TREE_CODE (expression) == ALIGNOF_EXPR
      || TREE_CODE (expression) == TRAIT_EXPR
      || TREE_CODE (expression) == TRAIT_EXPR
      || TREE_CODE (expression) == TYPEID_EXPR
      || TREE_CODE (expression) == TYPEID_EXPR
      || TREE_CODE (expression) == DELETE_EXPR
      || TREE_CODE (expression) == DELETE_EXPR
      || TREE_CODE (expression) == VEC_DELETE_EXPR
      || TREE_CODE (expression) == VEC_DELETE_EXPR
      || TREE_CODE (expression) == THROW_EXPR)
      || TREE_CODE (expression) == THROW_EXPR)
    return false;
    return false;
 
 
  /* The types of these expressions depends only on the type to which
  /* The types of these expressions depends only on the type to which
     the cast occurs.  */
     the cast occurs.  */
  if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
  if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
      || TREE_CODE (expression) == STATIC_CAST_EXPR
      || TREE_CODE (expression) == STATIC_CAST_EXPR
      || TREE_CODE (expression) == CONST_CAST_EXPR
      || TREE_CODE (expression) == CONST_CAST_EXPR
      || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
      || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
      || TREE_CODE (expression) == CAST_EXPR)
      || TREE_CODE (expression) == CAST_EXPR)
    return dependent_type_p (TREE_TYPE (expression));
    return dependent_type_p (TREE_TYPE (expression));
 
 
  /* The types of these expressions depends only on the type created
  /* The types of these expressions depends only on the type created
     by the expression.  */
     by the expression.  */
  if (TREE_CODE (expression) == NEW_EXPR
  if (TREE_CODE (expression) == NEW_EXPR
      || TREE_CODE (expression) == VEC_NEW_EXPR)
      || TREE_CODE (expression) == VEC_NEW_EXPR)
    {
    {
      /* For NEW_EXPR tree nodes created inside a template, either
      /* For NEW_EXPR tree nodes created inside a template, either
         the object type itself or a TREE_LIST may appear as the
         the object type itself or a TREE_LIST may appear as the
         operand 1.  */
         operand 1.  */
      tree type = TREE_OPERAND (expression, 1);
      tree type = TREE_OPERAND (expression, 1);
      if (TREE_CODE (type) == TREE_LIST)
      if (TREE_CODE (type) == TREE_LIST)
        /* This is an array type.  We need to check array dimensions
        /* This is an array type.  We need to check array dimensions
           as well.  */
           as well.  */
        return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
        return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
               || value_dependent_expression_p
               || value_dependent_expression_p
                    (TREE_OPERAND (TREE_VALUE (type), 1));
                    (TREE_OPERAND (TREE_VALUE (type), 1));
      else
      else
        return dependent_type_p (type);
        return dependent_type_p (type);
    }
    }
 
 
  if (TREE_CODE (expression) == SCOPE_REF
  if (TREE_CODE (expression) == SCOPE_REF
      && dependent_scope_ref_p (expression,
      && dependent_scope_ref_p (expression,
                                type_dependent_expression_p))
                                type_dependent_expression_p))
    return true;
    return true;
 
 
  if (TREE_CODE (expression) == FUNCTION_DECL
  if (TREE_CODE (expression) == FUNCTION_DECL
      && DECL_LANG_SPECIFIC (expression)
      && DECL_LANG_SPECIFIC (expression)
      && DECL_TEMPLATE_INFO (expression)
      && DECL_TEMPLATE_INFO (expression)
      && (any_dependent_template_arguments_p
      && (any_dependent_template_arguments_p
          (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
          (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
    return true;
    return true;
 
 
  if (TREE_CODE (expression) == TEMPLATE_DECL
  if (TREE_CODE (expression) == TEMPLATE_DECL
      && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
      && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
    return false;
    return false;
 
 
  if (TREE_CODE (expression) == STMT_EXPR)
  if (TREE_CODE (expression) == STMT_EXPR)
    expression = stmt_expr_value_expr (expression);
    expression = stmt_expr_value_expr (expression);
 
 
  if (BRACE_ENCLOSED_INITIALIZER_P (expression))
  if (BRACE_ENCLOSED_INITIALIZER_P (expression))
    {
    {
      tree elt;
      tree elt;
      unsigned i;
      unsigned i;
 
 
      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
        {
        {
          if (type_dependent_expression_p (elt))
          if (type_dependent_expression_p (elt))
            return true;
            return true;
        }
        }
      return false;
      return false;
    }
    }
 
 
  if (TREE_TYPE (expression) == unknown_type_node)
  if (TREE_TYPE (expression) == unknown_type_node)
    {
    {
      if (TREE_CODE (expression) == ADDR_EXPR)
      if (TREE_CODE (expression) == ADDR_EXPR)
        return type_dependent_expression_p (TREE_OPERAND (expression, 0));
        return type_dependent_expression_p (TREE_OPERAND (expression, 0));
      if (TREE_CODE (expression) == COMPONENT_REF
      if (TREE_CODE (expression) == COMPONENT_REF
          || TREE_CODE (expression) == OFFSET_REF)
          || TREE_CODE (expression) == OFFSET_REF)
        {
        {
          if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
          if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
            return true;
            return true;
          expression = TREE_OPERAND (expression, 1);
          expression = TREE_OPERAND (expression, 1);
          if (TREE_CODE (expression) == IDENTIFIER_NODE)
          if (TREE_CODE (expression) == IDENTIFIER_NODE)
            return false;
            return false;
        }
        }
      /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
      /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
      if (TREE_CODE (expression) == SCOPE_REF)
      if (TREE_CODE (expression) == SCOPE_REF)
        return false;
        return false;
 
 
      if (TREE_CODE (expression) == BASELINK)
      if (TREE_CODE (expression) == BASELINK)
        expression = BASELINK_FUNCTIONS (expression);
        expression = BASELINK_FUNCTIONS (expression);
 
 
      if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
      if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
        {
        {
          if (any_dependent_template_arguments_p
          if (any_dependent_template_arguments_p
              (TREE_OPERAND (expression, 1)))
              (TREE_OPERAND (expression, 1)))
            return true;
            return true;
          expression = TREE_OPERAND (expression, 0);
          expression = TREE_OPERAND (expression, 0);
        }
        }
      gcc_assert (TREE_CODE (expression) == OVERLOAD
      gcc_assert (TREE_CODE (expression) == OVERLOAD
                  || TREE_CODE (expression) == FUNCTION_DECL);
                  || TREE_CODE (expression) == FUNCTION_DECL);
 
 
      while (expression)
      while (expression)
        {
        {
          if (type_dependent_expression_p (OVL_CURRENT (expression)))
          if (type_dependent_expression_p (OVL_CURRENT (expression)))
            return true;
            return true;
          expression = OVL_NEXT (expression);
          expression = OVL_NEXT (expression);
        }
        }
      return false;
      return false;
    }
    }
 
 
  gcc_assert (TREE_CODE (expression) != TYPE_DECL);
  gcc_assert (TREE_CODE (expression) != TYPE_DECL);
 
 
  return (dependent_type_p (TREE_TYPE (expression)));
  return (dependent_type_p (TREE_TYPE (expression)));
}
}
 
 
/* Like type_dependent_expression_p, but it also works while not processing
/* Like type_dependent_expression_p, but it also works while not processing
   a template definition, i.e. during substitution or mangling.  */
   a template definition, i.e. during substitution or mangling.  */
 
 
bool
bool
type_dependent_expression_p_push (tree expr)
type_dependent_expression_p_push (tree expr)
{
{
  bool b;
  bool b;
  ++processing_template_decl;
  ++processing_template_decl;
  b = type_dependent_expression_p (expr);
  b = type_dependent_expression_p (expr);
  --processing_template_decl;
  --processing_template_decl;
  return b;
  return b;
}
}
 
 
/* Returns TRUE if ARGS contains a type-dependent expression.  */
/* Returns TRUE if ARGS contains a type-dependent expression.  */
 
 
bool
bool
any_type_dependent_arguments_p (const VEC(tree,gc) *args)
any_type_dependent_arguments_p (const VEC(tree,gc) *args)
{
{
  unsigned int i;
  unsigned int i;
  tree arg;
  tree arg;
 
 
  for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
  for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
    {
    {
      if (type_dependent_expression_p (arg))
      if (type_dependent_expression_p (arg))
        return true;
        return true;
    }
    }
  return false;
  return false;
}
}
 
 
/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   expressions) contains any value-dependent expressions.  */
   expressions) contains any value-dependent expressions.  */
 
 
bool
bool
any_value_dependent_elements_p (const_tree list)
any_value_dependent_elements_p (const_tree list)
{
{
  for (; list; list = TREE_CHAIN (list))
  for (; list; list = TREE_CHAIN (list))
    if (value_dependent_expression_p (TREE_VALUE (list)))
    if (value_dependent_expression_p (TREE_VALUE (list)))
      return true;
      return true;
 
 
  return false;
  return false;
}
}
 
 
/* Returns TRUE if the ARG (a template argument) is dependent.  */
/* Returns TRUE if the ARG (a template argument) is dependent.  */
 
 
bool
bool
dependent_template_arg_p (tree arg)
dependent_template_arg_p (tree arg)
{
{
  if (!processing_template_decl)
  if (!processing_template_decl)
    return false;
    return false;
 
 
  if (TREE_CODE (arg) == TEMPLATE_DECL
  if (TREE_CODE (arg) == TEMPLATE_DECL
      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
    return dependent_template_p (arg);
    return dependent_template_p (arg);
  else if (ARGUMENT_PACK_P (arg))
  else if (ARGUMENT_PACK_P (arg))
    {
    {
      tree args = ARGUMENT_PACK_ARGS (arg);
      tree args = ARGUMENT_PACK_ARGS (arg);
      int i, len = TREE_VEC_LENGTH (args);
      int i, len = TREE_VEC_LENGTH (args);
      for (i = 0; i < len; ++i)
      for (i = 0; i < len; ++i)
        {
        {
          if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
          if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
            return true;
            return true;
        }
        }
 
 
      return false;
      return false;
    }
    }
  else if (TYPE_P (arg))
  else if (TYPE_P (arg))
    return dependent_type_p (arg);
    return dependent_type_p (arg);
  else
  else
    return (type_dependent_expression_p (arg)
    return (type_dependent_expression_p (arg)
            || value_dependent_expression_p (arg));
            || value_dependent_expression_p (arg));
}
}
 
 
/* Returns true if ARGS (a collection of template arguments) contains
/* Returns true if ARGS (a collection of template arguments) contains
   any types that require structural equality testing.  */
   any types that require structural equality testing.  */
 
 
bool
bool
any_template_arguments_need_structural_equality_p (tree args)
any_template_arguments_need_structural_equality_p (tree args)
{
{
  int i;
  int i;
  int j;
  int j;
 
 
  if (!args)
  if (!args)
    return false;
    return false;
  if (args == error_mark_node)
  if (args == error_mark_node)
    return true;
    return true;
 
 
  for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
  for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
    {
    {
      tree level = TMPL_ARGS_LEVEL (args, i + 1);
      tree level = TMPL_ARGS_LEVEL (args, i + 1);
      for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
      for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
        {
        {
          tree arg = TREE_VEC_ELT (level, j);
          tree arg = TREE_VEC_ELT (level, j);
          tree packed_args = NULL_TREE;
          tree packed_args = NULL_TREE;
          int k, len = 1;
          int k, len = 1;
 
 
          if (ARGUMENT_PACK_P (arg))
          if (ARGUMENT_PACK_P (arg))
            {
            {
              /* Look inside the argument pack.  */
              /* Look inside the argument pack.  */
              packed_args = ARGUMENT_PACK_ARGS (arg);
              packed_args = ARGUMENT_PACK_ARGS (arg);
              len = TREE_VEC_LENGTH (packed_args);
              len = TREE_VEC_LENGTH (packed_args);
            }
            }
 
 
          for (k = 0; k < len; ++k)
          for (k = 0; k < len; ++k)
            {
            {
              if (packed_args)
              if (packed_args)
                arg = TREE_VEC_ELT (packed_args, k);
                arg = TREE_VEC_ELT (packed_args, k);
 
 
              if (error_operand_p (arg))
              if (error_operand_p (arg))
                return true;
                return true;
              else if (TREE_CODE (arg) == TEMPLATE_DECL
              else if (TREE_CODE (arg) == TEMPLATE_DECL
                       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
                       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
                continue;
                continue;
              else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
              else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
                return true;
                return true;
              else if (!TYPE_P (arg) && TREE_TYPE (arg)
              else if (!TYPE_P (arg) && TREE_TYPE (arg)
                       && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
                       && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
                return true;
                return true;
            }
            }
        }
        }
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* Returns true if ARGS (a collection of template arguments) contains
/* Returns true if ARGS (a collection of template arguments) contains
   any dependent arguments.  */
   any dependent arguments.  */
 
 
bool
bool
any_dependent_template_arguments_p (const_tree args)
any_dependent_template_arguments_p (const_tree args)
{
{
  int i;
  int i;
  int j;
  int j;
 
 
  if (!args)
  if (!args)
    return false;
    return false;
  if (args == error_mark_node)
  if (args == error_mark_node)
    return true;
    return true;
 
 
  for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
  for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
    {
    {
      const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
      const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
      for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
      for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
        if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
        if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
          return true;
          return true;
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* Returns TRUE if the template TMPL is dependent.  */
/* Returns TRUE if the template TMPL is dependent.  */
 
 
bool
bool
dependent_template_p (tree tmpl)
dependent_template_p (tree tmpl)
{
{
  if (TREE_CODE (tmpl) == OVERLOAD)
  if (TREE_CODE (tmpl) == OVERLOAD)
    {
    {
      while (tmpl)
      while (tmpl)
        {
        {
          if (dependent_template_p (OVL_FUNCTION (tmpl)))
          if (dependent_template_p (OVL_FUNCTION (tmpl)))
            return true;
            return true;
          tmpl = OVL_CHAIN (tmpl);
          tmpl = OVL_CHAIN (tmpl);
        }
        }
      return false;
      return false;
    }
    }
 
 
  /* Template template parameters are dependent.  */
  /* Template template parameters are dependent.  */
  if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
  if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
      || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
      || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
    return true;
    return true;
  /* So are names that have not been looked up.  */
  /* So are names that have not been looked up.  */
  if (TREE_CODE (tmpl) == SCOPE_REF
  if (TREE_CODE (tmpl) == SCOPE_REF
      || TREE_CODE (tmpl) == IDENTIFIER_NODE)
      || TREE_CODE (tmpl) == IDENTIFIER_NODE)
    return true;
    return true;
  /* So are member templates of dependent classes.  */
  /* So are member templates of dependent classes.  */
  if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
  if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
    return dependent_type_p (DECL_CONTEXT (tmpl));
    return dependent_type_p (DECL_CONTEXT (tmpl));
  return false;
  return false;
}
}
 
 
/* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
/* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
 
 
bool
bool
dependent_template_id_p (tree tmpl, tree args)
dependent_template_id_p (tree tmpl, tree args)
{
{
  return (dependent_template_p (tmpl)
  return (dependent_template_p (tmpl)
          || any_dependent_template_arguments_p (args));
          || any_dependent_template_arguments_p (args));
}
}
 
 
/* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
/* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   is dependent.  */
   is dependent.  */
 
 
bool
bool
dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
{
{
  int i;
  int i;
 
 
  if (!processing_template_decl)
  if (!processing_template_decl)
    return false;
    return false;
 
 
  for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
  for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
    {
    {
      tree decl = TREE_VEC_ELT (declv, i);
      tree decl = TREE_VEC_ELT (declv, i);
      tree init = TREE_VEC_ELT (initv, i);
      tree init = TREE_VEC_ELT (initv, i);
      tree cond = TREE_VEC_ELT (condv, i);
      tree cond = TREE_VEC_ELT (condv, i);
      tree incr = TREE_VEC_ELT (incrv, i);
      tree incr = TREE_VEC_ELT (incrv, i);
 
 
      if (type_dependent_expression_p (decl))
      if (type_dependent_expression_p (decl))
        return true;
        return true;
 
 
      if (init && type_dependent_expression_p (init))
      if (init && type_dependent_expression_p (init))
        return true;
        return true;
 
 
      if (type_dependent_expression_p (cond))
      if (type_dependent_expression_p (cond))
        return true;
        return true;
 
 
      if (COMPARISON_CLASS_P (cond)
      if (COMPARISON_CLASS_P (cond)
          && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
          && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
              || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
              || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
        return true;
        return true;
 
 
      if (TREE_CODE (incr) == MODOP_EXPR)
      if (TREE_CODE (incr) == MODOP_EXPR)
        {
        {
          if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
          if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
              || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
              || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
            return true;
            return true;
        }
        }
      else if (type_dependent_expression_p (incr))
      else if (type_dependent_expression_p (incr))
        return true;
        return true;
      else if (TREE_CODE (incr) == MODIFY_EXPR)
      else if (TREE_CODE (incr) == MODIFY_EXPR)
        {
        {
          if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
          if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
            return true;
            return true;
          else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
          else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
            {
            {
              tree t = TREE_OPERAND (incr, 1);
              tree t = TREE_OPERAND (incr, 1);
              if (type_dependent_expression_p (TREE_OPERAND (t, 0))
              if (type_dependent_expression_p (TREE_OPERAND (t, 0))
                  || type_dependent_expression_p (TREE_OPERAND (t, 1)))
                  || type_dependent_expression_p (TREE_OPERAND (t, 1)))
                return true;
                return true;
            }
            }
        }
        }
    }
    }
 
 
  return false;
  return false;
}
}
 
 
/* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
/* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   no such TYPE can be found.  Note that this function peers inside
   no such TYPE can be found.  Note that this function peers inside
   uninstantiated templates and therefore should be used only in
   uninstantiated templates and therefore should be used only in
   extremely limited situations.  ONLY_CURRENT_P restricts this
   extremely limited situations.  ONLY_CURRENT_P restricts this
   peering to the currently open classes hierarchy (which is required
   peering to the currently open classes hierarchy (which is required
   when comparing types).  */
   when comparing types).  */
 
 
tree
tree
resolve_typename_type (tree type, bool only_current_p)
resolve_typename_type (tree type, bool only_current_p)
{
{
  tree scope;
  tree scope;
  tree name;
  tree name;
  tree decl;
  tree decl;
  int quals;
  int quals;
  tree pushed_scope;
  tree pushed_scope;
  tree result;
  tree result;
 
 
  gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
  gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
 
 
  scope = TYPE_CONTEXT (type);
  scope = TYPE_CONTEXT (type);
  /* Usually the non-qualified identifier of a TYPENAME_TYPE is
  /* Usually the non-qualified identifier of a TYPENAME_TYPE is
     TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
     TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
     a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
     a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
     the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
     the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
     identifier  of the TYPENAME_TYPE anymore.
     identifier  of the TYPENAME_TYPE anymore.
     So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
     So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
     TYPENAME_TYPE instead, we avoid messing up with a possible
     TYPENAME_TYPE instead, we avoid messing up with a possible
     typedef variant case.  */
     typedef variant case.  */
  name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
  name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
 
 
  /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
  /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
     it first before we can figure out what NAME refers to.  */
     it first before we can figure out what NAME refers to.  */
  if (TREE_CODE (scope) == TYPENAME_TYPE)
  if (TREE_CODE (scope) == TYPENAME_TYPE)
    scope = resolve_typename_type (scope, only_current_p);
    scope = resolve_typename_type (scope, only_current_p);
  /* If we don't know what SCOPE refers to, then we cannot resolve the
  /* If we don't know what SCOPE refers to, then we cannot resolve the
     TYPENAME_TYPE.  */
     TYPENAME_TYPE.  */
  if (TREE_CODE (scope) == TYPENAME_TYPE)
  if (TREE_CODE (scope) == TYPENAME_TYPE)
    return type;
    return type;
  /* If the SCOPE is a template type parameter, we have no way of
  /* If the SCOPE is a template type parameter, we have no way of
     resolving the name.  */
     resolving the name.  */
  if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
  if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
    return type;
    return type;
  /* If the SCOPE is not the current instantiation, there's no reason
  /* If the SCOPE is not the current instantiation, there's no reason
     to look inside it.  */
     to look inside it.  */
  if (only_current_p && !currently_open_class (scope))
  if (only_current_p && !currently_open_class (scope))
    return type;
    return type;
  /* If this is a typedef, we don't want to look inside (c++/11987).  */
  /* If this is a typedef, we don't want to look inside (c++/11987).  */
  if (typedef_variant_p (type))
  if (typedef_variant_p (type))
    return type;
    return type;
  /* If SCOPE isn't the template itself, it will not have a valid
  /* If SCOPE isn't the template itself, it will not have a valid
     TYPE_FIELDS list.  */
     TYPE_FIELDS list.  */
  if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
  if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
    /* scope is either the template itself or a compatible instantiation
    /* scope is either the template itself or a compatible instantiation
       like X<T>, so look up the name in the original template.  */
       like X<T>, so look up the name in the original template.  */
    scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
    scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
  else
  else
    /* scope is a partial instantiation, so we can't do the lookup or we
    /* scope is a partial instantiation, so we can't do the lookup or we
       will lose the template arguments.  */
       will lose the template arguments.  */
    return type;
    return type;
  /* Enter the SCOPE so that name lookup will be resolved as if we
  /* Enter the SCOPE so that name lookup will be resolved as if we
     were in the class definition.  In particular, SCOPE will no
     were in the class definition.  In particular, SCOPE will no
     longer be considered a dependent type.  */
     longer be considered a dependent type.  */
  pushed_scope = push_scope (scope);
  pushed_scope = push_scope (scope);
  /* Look up the declaration.  */
  /* Look up the declaration.  */
  decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
  decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
 
 
  result = NULL_TREE;
  result = NULL_TREE;
 
 
  /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
  /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
     find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
     find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
  if (!decl)
  if (!decl)
    /*nop*/;
    /*nop*/;
  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
           && TREE_CODE (decl) == TYPE_DECL)
           && TREE_CODE (decl) == TYPE_DECL)
    {
    {
      result = TREE_TYPE (decl);
      result = TREE_TYPE (decl);
      if (result == error_mark_node)
      if (result == error_mark_node)
        result = NULL_TREE;
        result = NULL_TREE;
    }
    }
  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
           && DECL_CLASS_TEMPLATE_P (decl))
           && DECL_CLASS_TEMPLATE_P (decl))
    {
    {
      tree tmpl;
      tree tmpl;
      tree args;
      tree args;
      /* Obtain the template and the arguments.  */
      /* Obtain the template and the arguments.  */
      tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
      tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
      args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
      args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
      /* Instantiate the template.  */
      /* Instantiate the template.  */
      result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
      result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
                                      /*entering_scope=*/0,
                                      /*entering_scope=*/0,
                                      tf_error | tf_user);
                                      tf_error | tf_user);
      if (result == error_mark_node)
      if (result == error_mark_node)
        result = NULL_TREE;
        result = NULL_TREE;
    }
    }
 
 
  /* Leave the SCOPE.  */
  /* Leave the SCOPE.  */
  if (pushed_scope)
  if (pushed_scope)
    pop_scope (pushed_scope);
    pop_scope (pushed_scope);
 
 
  /* If we failed to resolve it, return the original typename.  */
  /* If we failed to resolve it, return the original typename.  */
  if (!result)
  if (!result)
    return type;
    return type;
 
 
  /* If lookup found a typename type, resolve that too.  */
  /* If lookup found a typename type, resolve that too.  */
  if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
  if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
    {
    {
      /* Ill-formed programs can cause infinite recursion here, so we
      /* Ill-formed programs can cause infinite recursion here, so we
         must catch that.  */
         must catch that.  */
      TYPENAME_IS_RESOLVING_P (type) = 1;
      TYPENAME_IS_RESOLVING_P (type) = 1;
      result = resolve_typename_type (result, only_current_p);
      result = resolve_typename_type (result, only_current_p);
      TYPENAME_IS_RESOLVING_P (type) = 0;
      TYPENAME_IS_RESOLVING_P (type) = 0;
    }
    }
 
 
  /* Qualify the resulting type.  */
  /* Qualify the resulting type.  */
  quals = cp_type_quals (type);
  quals = cp_type_quals (type);
  if (quals)
  if (quals)
    result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
    result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
 
 
  return result;
  return result;
}
}
 
 
/* EXPR is an expression which is not type-dependent.  Return a proxy
/* EXPR is an expression which is not type-dependent.  Return a proxy
   for EXPR that can be used to compute the types of larger
   for EXPR that can be used to compute the types of larger
   expressions containing EXPR.  */
   expressions containing EXPR.  */
 
 
tree
tree
build_non_dependent_expr (tree expr)
build_non_dependent_expr (tree expr)
{
{
  tree inner_expr;
  tree inner_expr;
 
 
  /* Preserve null pointer constants so that the type of things like
  /* Preserve null pointer constants so that the type of things like
     "p == 0" where "p" is a pointer can be determined.  */
     "p == 0" where "p" is a pointer can be determined.  */
  if (null_ptr_cst_p (expr))
  if (null_ptr_cst_p (expr))
    return expr;
    return expr;
  /* Preserve OVERLOADs; the functions must be available to resolve
  /* Preserve OVERLOADs; the functions must be available to resolve
     types.  */
     types.  */
  inner_expr = expr;
  inner_expr = expr;
  if (TREE_CODE (inner_expr) == STMT_EXPR)
  if (TREE_CODE (inner_expr) == STMT_EXPR)
    inner_expr = stmt_expr_value_expr (inner_expr);
    inner_expr = stmt_expr_value_expr (inner_expr);
  if (TREE_CODE (inner_expr) == ADDR_EXPR)
  if (TREE_CODE (inner_expr) == ADDR_EXPR)
    inner_expr = TREE_OPERAND (inner_expr, 0);
    inner_expr = TREE_OPERAND (inner_expr, 0);
  if (TREE_CODE (inner_expr) == COMPONENT_REF)
  if (TREE_CODE (inner_expr) == COMPONENT_REF)
    inner_expr = TREE_OPERAND (inner_expr, 1);
    inner_expr = TREE_OPERAND (inner_expr, 1);
  if (is_overloaded_fn (inner_expr)
  if (is_overloaded_fn (inner_expr)
      || TREE_CODE (inner_expr) == OFFSET_REF)
      || TREE_CODE (inner_expr) == OFFSET_REF)
    return expr;
    return expr;
  /* There is no need to return a proxy for a variable.  */
  /* There is no need to return a proxy for a variable.  */
  if (TREE_CODE (expr) == VAR_DECL)
  if (TREE_CODE (expr) == VAR_DECL)
    return expr;
    return expr;
  /* Preserve string constants; conversions from string constants to
  /* Preserve string constants; conversions from string constants to
     "char *" are allowed, even though normally a "const char *"
     "char *" are allowed, even though normally a "const char *"
     cannot be used to initialize a "char *".  */
     cannot be used to initialize a "char *".  */
  if (TREE_CODE (expr) == STRING_CST)
  if (TREE_CODE (expr) == STRING_CST)
    return expr;
    return expr;
  /* Preserve arithmetic constants, as an optimization -- there is no
  /* Preserve arithmetic constants, as an optimization -- there is no
     reason to create a new node.  */
     reason to create a new node.  */
  if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
  if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
    return expr;
    return expr;
  /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
  /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
     There is at least one place where we want to know that a
     There is at least one place where we want to know that a
     particular expression is a throw-expression: when checking a ?:
     particular expression is a throw-expression: when checking a ?:
     expression, there are special rules if the second or third
     expression, there are special rules if the second or third
     argument is a throw-expression.  */
     argument is a throw-expression.  */
  if (TREE_CODE (expr) == THROW_EXPR)
  if (TREE_CODE (expr) == THROW_EXPR)
    return expr;
    return expr;
 
 
  if (TREE_CODE (expr) == COND_EXPR)
  if (TREE_CODE (expr) == COND_EXPR)
    return build3 (COND_EXPR,
    return build3 (COND_EXPR,
                   TREE_TYPE (expr),
                   TREE_TYPE (expr),
                   TREE_OPERAND (expr, 0),
                   TREE_OPERAND (expr, 0),
                   (TREE_OPERAND (expr, 1)
                   (TREE_OPERAND (expr, 1)
                    ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
                    ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
                    : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
                    : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
                   build_non_dependent_expr (TREE_OPERAND (expr, 2)));
                   build_non_dependent_expr (TREE_OPERAND (expr, 2)));
  if (TREE_CODE (expr) == COMPOUND_EXPR
  if (TREE_CODE (expr) == COMPOUND_EXPR
      && !COMPOUND_EXPR_OVERLOADED (expr))
      && !COMPOUND_EXPR_OVERLOADED (expr))
    return build2 (COMPOUND_EXPR,
    return build2 (COMPOUND_EXPR,
                   TREE_TYPE (expr),
                   TREE_TYPE (expr),
                   TREE_OPERAND (expr, 0),
                   TREE_OPERAND (expr, 0),
                   build_non_dependent_expr (TREE_OPERAND (expr, 1)));
                   build_non_dependent_expr (TREE_OPERAND (expr, 1)));
 
 
  /* If the type is unknown, it can't really be non-dependent */
  /* If the type is unknown, it can't really be non-dependent */
  gcc_assert (TREE_TYPE (expr) != unknown_type_node);
  gcc_assert (TREE_TYPE (expr) != unknown_type_node);
 
 
  /* Otherwise, build a NON_DEPENDENT_EXPR.
  /* Otherwise, build a NON_DEPENDENT_EXPR.
 
 
     REFERENCE_TYPEs are not stripped for expressions in templates
     REFERENCE_TYPEs are not stripped for expressions in templates
     because doing so would play havoc with mangling.  Consider, for
     because doing so would play havoc with mangling.  Consider, for
     example:
     example:
 
 
       template <typename T> void f<T& g>() { g(); }
       template <typename T> void f<T& g>() { g(); }
 
 
     In the body of "f", the expression for "g" will have
     In the body of "f", the expression for "g" will have
     REFERENCE_TYPE, even though the standard says that it should
     REFERENCE_TYPE, even though the standard says that it should
     not.  The reason is that we must preserve the syntactic form of
     not.  The reason is that we must preserve the syntactic form of
     the expression so that mangling (say) "f<g>" inside the body of
     the expression so that mangling (say) "f<g>" inside the body of
     "f" works out correctly.  Therefore, the REFERENCE_TYPE is
     "f" works out correctly.  Therefore, the REFERENCE_TYPE is
     stripped here.  */
     stripped here.  */
  return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
  return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
}
}
 
 
/* ARGS is a vector of expressions as arguments to a function call.
/* ARGS is a vector of expressions as arguments to a function call.
   Replace the arguments with equivalent non-dependent expressions.
   Replace the arguments with equivalent non-dependent expressions.
   This modifies ARGS in place.  */
   This modifies ARGS in place.  */
 
 
void
void
make_args_non_dependent (VEC(tree,gc) *args)
make_args_non_dependent (VEC(tree,gc) *args)
{
{
  unsigned int ix;
  unsigned int ix;
  tree arg;
  tree arg;
 
 
  for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
  for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
    {
    {
      tree newarg = build_non_dependent_expr (arg);
      tree newarg = build_non_dependent_expr (arg);
      if (newarg != arg)
      if (newarg != arg)
        VEC_replace (tree, args, ix, newarg);
        VEC_replace (tree, args, ix, newarg);
    }
    }
}
}
 
 
/* Returns a type which represents 'auto'.  We use a TEMPLATE_TYPE_PARM
/* Returns a type which represents 'auto'.  We use a TEMPLATE_TYPE_PARM
   with a level one deeper than the actual template parms.  */
   with a level one deeper than the actual template parms.  */
 
 
tree
tree
make_auto (void)
make_auto (void)
{
{
  tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
  tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
  TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
  TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
                               TYPE_DECL, get_identifier ("auto"), au);
                               TYPE_DECL, get_identifier ("auto"), au);
  TYPE_STUB_DECL (au) = TYPE_NAME (au);
  TYPE_STUB_DECL (au) = TYPE_NAME (au);
  TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
  TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
    (0, processing_template_decl + 1, processing_template_decl + 1,
    (0, processing_template_decl + 1, processing_template_decl + 1,
     TYPE_NAME (au), NULL_TREE);
     TYPE_NAME (au), NULL_TREE);
  TYPE_CANONICAL (au) = canonical_type_parameter (au);
  TYPE_CANONICAL (au) = canonical_type_parameter (au);
  DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
  DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
  SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
  SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
 
 
  return au;
  return au;
}
}
 
 
/* Given type ARG, return std::initializer_list<ARG>.  */
/* Given type ARG, return std::initializer_list<ARG>.  */
 
 
static tree
static tree
listify (tree arg)
listify (tree arg)
{
{
  tree std_init_list = namespace_binding
  tree std_init_list = namespace_binding
    (get_identifier ("initializer_list"), std_node);
    (get_identifier ("initializer_list"), std_node);
  tree argvec;
  tree argvec;
  if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
  if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
    {
    {
      error ("deducing from brace-enclosed initializer list requires "
      error ("deducing from brace-enclosed initializer list requires "
             "#include <initializer_list>");
             "#include <initializer_list>");
      return error_mark_node;
      return error_mark_node;
    }
    }
  argvec = make_tree_vec (1);
  argvec = make_tree_vec (1);
  TREE_VEC_ELT (argvec, 0) = arg;
  TREE_VEC_ELT (argvec, 0) = arg;
  return lookup_template_class (std_init_list, argvec, NULL_TREE,
  return lookup_template_class (std_init_list, argvec, NULL_TREE,
                                NULL_TREE, 0, tf_warning_or_error);
                                NULL_TREE, 0, tf_warning_or_error);
}
}
 
 
/* Replace auto in TYPE with std::initializer_list<auto>.  */
/* Replace auto in TYPE with std::initializer_list<auto>.  */
 
 
static tree
static tree
listify_autos (tree type, tree auto_node)
listify_autos (tree type, tree auto_node)
{
{
  tree init_auto = listify (auto_node);
  tree init_auto = listify (auto_node);
  tree argvec = make_tree_vec (1);
  tree argvec = make_tree_vec (1);
  TREE_VEC_ELT (argvec, 0) = init_auto;
  TREE_VEC_ELT (argvec, 0) = init_auto;
  if (processing_template_decl)
  if (processing_template_decl)
    argvec = add_to_template_args (current_template_args (), argvec);
    argvec = add_to_template_args (current_template_args (), argvec);
  return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
  return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
}
}
 
 
/* walk_tree helper for do_auto_deduction.  */
/* walk_tree helper for do_auto_deduction.  */
 
 
static tree
static tree
contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
                 void *type)
                 void *type)
{
{
  /* Is this a variable with the type we're looking for?  */
  /* Is this a variable with the type we're looking for?  */
  if (DECL_P (*tp)
  if (DECL_P (*tp)
      && TREE_TYPE (*tp) == type)
      && TREE_TYPE (*tp) == type)
    return *tp;
    return *tp;
  else
  else
    return NULL_TREE;
    return NULL_TREE;
}
}
 
 
/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.  */
   from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.  */
 
 
tree
tree
do_auto_deduction (tree type, tree init, tree auto_node)
do_auto_deduction (tree type, tree init, tree auto_node)
{
{
  tree parms, tparms, targs;
  tree parms, tparms, targs;
  tree args[1];
  tree args[1];
  tree decl;
  tree decl;
  int val;
  int val;
 
 
  /* The name of the object being declared shall not appear in the
  /* The name of the object being declared shall not appear in the
     initializer expression.  */
     initializer expression.  */
  decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
  decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
  if (decl)
  if (decl)
    {
    {
      error ("variable %q#D with %<auto%> type used in its own "
      error ("variable %q#D with %<auto%> type used in its own "
             "initializer", decl);
             "initializer", decl);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
  /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
     with either a new invented type template parameter U or, if the
     with either a new invented type template parameter U or, if the
     initializer is a braced-init-list (8.5.4), with
     initializer is a braced-init-list (8.5.4), with
     std::initializer_list<U>.  */
     std::initializer_list<U>.  */
  if (BRACE_ENCLOSED_INITIALIZER_P (init))
  if (BRACE_ENCLOSED_INITIALIZER_P (init))
    type = listify_autos (type, auto_node);
    type = listify_autos (type, auto_node);
 
 
  parms = build_tree_list (NULL_TREE, type);
  parms = build_tree_list (NULL_TREE, type);
  args[0] = init;
  args[0] = init;
  tparms = make_tree_vec (1);
  tparms = make_tree_vec (1);
  targs = make_tree_vec (1);
  targs = make_tree_vec (1);
  TREE_VEC_ELT (tparms, 0)
  TREE_VEC_ELT (tparms, 0)
    = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
    = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
  val = type_unification_real (tparms, targs, parms, args, 1, 0,
  val = type_unification_real (tparms, targs, parms, args, 1, 0,
                               DEDUCE_CALL, LOOKUP_NORMAL);
                               DEDUCE_CALL, LOOKUP_NORMAL);
  if (val > 0)
  if (val > 0)
    {
    {
      error ("unable to deduce %qT from %qE", type, init);
      error ("unable to deduce %qT from %qE", type, init);
      return error_mark_node;
      return error_mark_node;
    }
    }
 
 
  /* If the list of declarators contains more than one declarator, the type
  /* If the list of declarators contains more than one declarator, the type
     of each declared variable is determined as described above. If the
     of each declared variable is determined as described above. If the
     type deduced for the template parameter U is not the same in each
     type deduced for the template parameter U is not the same in each
     deduction, the program is ill-formed.  */
     deduction, the program is ill-formed.  */
  if (TREE_TYPE (auto_node)
  if (TREE_TYPE (auto_node)
      && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
      && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
    {
    {
      error ("inconsistent deduction for %qT: %qT and then %qT",
      error ("inconsistent deduction for %qT: %qT and then %qT",
             auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
             auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
      return error_mark_node;
      return error_mark_node;
    }
    }
  TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
  TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
 
 
  if (processing_template_decl)
  if (processing_template_decl)
    targs = add_to_template_args (current_template_args (), targs);
    targs = add_to_template_args (current_template_args (), targs);
  return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
  return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
}
}
 
 
/* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
/* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   result.  */
   result.  */
 
 
tree
tree
splice_late_return_type (tree type, tree late_return_type)
splice_late_return_type (tree type, tree late_return_type)
{
{
  tree argvec;
  tree argvec;
 
 
  if (late_return_type == NULL_TREE)
  if (late_return_type == NULL_TREE)
    return type;
    return type;
  argvec = make_tree_vec (1);
  argvec = make_tree_vec (1);
  TREE_VEC_ELT (argvec, 0) = late_return_type;
  TREE_VEC_ELT (argvec, 0) = late_return_type;
  if (processing_template_decl)
  if (processing_template_decl)
    argvec = add_to_template_args (current_template_args (), argvec);
    argvec = add_to_template_args (current_template_args (), argvec);
  return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
  return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
}
}
 
 
/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'.  */
/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'.  */
 
 
bool
bool
is_auto (const_tree type)
is_auto (const_tree type)
{
{
  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
      && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
      && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
    return true;
    return true;
  else
  else
    return false;
    return false;
}
}
 
 
/* Returns true iff TYPE contains a use of 'auto'.  Since auto can only
/* Returns true iff TYPE contains a use of 'auto'.  Since auto can only
   appear as a type-specifier for the declaration in question, we don't
   appear as a type-specifier for the declaration in question, we don't
   have to look through the whole type.  */
   have to look through the whole type.  */
 
 
tree
tree
type_uses_auto (tree type)
type_uses_auto (tree type)
{
{
  enum tree_code code;
  enum tree_code code;
  if (is_auto (type))
  if (is_auto (type))
    return type;
    return type;
 
 
  code = TREE_CODE (type);
  code = TREE_CODE (type);
 
 
  if (code == POINTER_TYPE || code == REFERENCE_TYPE
  if (code == POINTER_TYPE || code == REFERENCE_TYPE
      || code == OFFSET_TYPE || code == FUNCTION_TYPE
      || code == OFFSET_TYPE || code == FUNCTION_TYPE
      || code == METHOD_TYPE || code == ARRAY_TYPE)
      || code == METHOD_TYPE || code == ARRAY_TYPE)
    return type_uses_auto (TREE_TYPE (type));
    return type_uses_auto (TREE_TYPE (type));
 
 
  if (TYPE_PTRMEMFUNC_P (type))
  if (TYPE_PTRMEMFUNC_P (type))
    return type_uses_auto (TREE_TYPE (TREE_TYPE
    return type_uses_auto (TREE_TYPE (TREE_TYPE
                                   (TYPE_PTRMEMFUNC_FN_TYPE (type))));
                                   (TYPE_PTRMEMFUNC_FN_TYPE (type))));
 
 
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* For a given template T, return the vector of typedefs referenced
/* For a given template T, return the vector of typedefs referenced
   in T for which access check is needed at T instantiation time.
   in T for which access check is needed at T instantiation time.
   T is either  a FUNCTION_DECL or a RECORD_TYPE.
   T is either  a FUNCTION_DECL or a RECORD_TYPE.
   Those typedefs were added to T by the function
   Those typedefs were added to T by the function
   append_type_to_template_for_access_check.  */
   append_type_to_template_for_access_check.  */
 
 
VEC(qualified_typedef_usage_t,gc)*
VEC(qualified_typedef_usage_t,gc)*
get_types_needing_access_check (tree t)
get_types_needing_access_check (tree t)
{
{
  tree ti;
  tree ti;
  VEC(qualified_typedef_usage_t,gc) *result = NULL;
  VEC(qualified_typedef_usage_t,gc) *result = NULL;
 
 
  if (!t || t == error_mark_node)
  if (!t || t == error_mark_node)
    return NULL;
    return NULL;
 
 
  if (!(ti = get_template_info (t)))
  if (!(ti = get_template_info (t)))
    return NULL;
    return NULL;
 
 
  if (CLASS_TYPE_P (t)
  if (CLASS_TYPE_P (t)
      || TREE_CODE (t) == FUNCTION_DECL)
      || TREE_CODE (t) == FUNCTION_DECL)
    {
    {
      if (!TI_TEMPLATE (ti))
      if (!TI_TEMPLATE (ti))
        return NULL;
        return NULL;
 
 
      result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
      result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
    }
    }
 
 
  return result;
  return result;
}
}
 
 
/* Append the typedef TYPE_DECL used in template T to a list of typedefs
/* Append the typedef TYPE_DECL used in template T to a list of typedefs
   tied to T. That list of typedefs will be access checked at
   tied to T. That list of typedefs will be access checked at
   T instantiation time.
   T instantiation time.
   T is either a FUNCTION_DECL or a RECORD_TYPE.
   T is either a FUNCTION_DECL or a RECORD_TYPE.
   TYPE_DECL is a TYPE_DECL node representing a typedef.
   TYPE_DECL is a TYPE_DECL node representing a typedef.
   SCOPE is the scope through which TYPE_DECL is accessed.
   SCOPE is the scope through which TYPE_DECL is accessed.
   LOCATION is the location of the usage point of TYPE_DECL.
   LOCATION is the location of the usage point of TYPE_DECL.
 
 
   This function is a subroutine of
   This function is a subroutine of
   append_type_to_template_for_access_check.  */
   append_type_to_template_for_access_check.  */
 
 
static void
static void
append_type_to_template_for_access_check_1 (tree t,
append_type_to_template_for_access_check_1 (tree t,
                                            tree type_decl,
                                            tree type_decl,
                                            tree scope,
                                            tree scope,
                                            location_t location)
                                            location_t location)
{
{
  qualified_typedef_usage_t typedef_usage;
  qualified_typedef_usage_t typedef_usage;
  tree ti;
  tree ti;
 
 
  if (!t || t == error_mark_node)
  if (!t || t == error_mark_node)
    return;
    return;
 
 
  gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
  gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
               || CLASS_TYPE_P (t))
               || CLASS_TYPE_P (t))
              && type_decl
              && type_decl
              && TREE_CODE (type_decl) == TYPE_DECL
              && TREE_CODE (type_decl) == TYPE_DECL
              && scope);
              && scope);
 
 
  if (!(ti = get_template_info (t)))
  if (!(ti = get_template_info (t)))
    return;
    return;
 
 
  gcc_assert (TI_TEMPLATE (ti));
  gcc_assert (TI_TEMPLATE (ti));
 
 
  typedef_usage.typedef_decl = type_decl;
  typedef_usage.typedef_decl = type_decl;
  typedef_usage.context = scope;
  typedef_usage.context = scope;
  typedef_usage.locus = location;
  typedef_usage.locus = location;
 
 
  VEC_safe_push (qualified_typedef_usage_t, gc,
  VEC_safe_push (qualified_typedef_usage_t, gc,
                 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
                 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
                 &typedef_usage);
                 &typedef_usage);
}
}
 
 
/* Append TYPE_DECL to the template TEMPL.
/* Append TYPE_DECL to the template TEMPL.
   TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
   TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
   At TEMPL instanciation time, TYPE_DECL will be checked to see
   At TEMPL instanciation time, TYPE_DECL will be checked to see
   if it can be accessed through SCOPE.
   if it can be accessed through SCOPE.
   LOCATION is the location of the usage point of TYPE_DECL.
   LOCATION is the location of the usage point of TYPE_DECL.
 
 
   e.g. consider the following code snippet:
   e.g. consider the following code snippet:
 
 
     class C
     class C
     {
     {
       typedef int myint;
       typedef int myint;
     };
     };
 
 
     template<class U> struct S
     template<class U> struct S
     {
     {
       C::myint mi; // <-- usage point of the typedef C::myint
       C::myint mi; // <-- usage point of the typedef C::myint
     };
     };
 
 
     S<char> s;
     S<char> s;
 
 
   At S<char> instantiation time, we need to check the access of C::myint
   At S<char> instantiation time, we need to check the access of C::myint
   In other words, we need to check the access of the myint typedef through
   In other words, we need to check the access of the myint typedef through
   the C scope. For that purpose, this function will add the myint typedef
   the C scope. For that purpose, this function will add the myint typedef
   and the scope C through which its being accessed to a list of typedefs
   and the scope C through which its being accessed to a list of typedefs
   tied to the template S. That list will be walked at template instantiation
   tied to the template S. That list will be walked at template instantiation
   time and access check performed on each typedefs it contains.
   time and access check performed on each typedefs it contains.
   Note that this particular code snippet should yield an error because
   Note that this particular code snippet should yield an error because
   myint is private to C.  */
   myint is private to C.  */
 
 
void
void
append_type_to_template_for_access_check (tree templ,
append_type_to_template_for_access_check (tree templ,
                                          tree type_decl,
                                          tree type_decl,
                                          tree scope,
                                          tree scope,
                                          location_t location)
                                          location_t location)
{
{
  qualified_typedef_usage_t *iter;
  qualified_typedef_usage_t *iter;
  int i;
  int i;
 
 
  gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
  gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
 
 
  /* Make sure we don't append the type to the template twice.  */
  /* Make sure we don't append the type to the template twice.  */
  for (i = 0;
  for (i = 0;
       VEC_iterate (qualified_typedef_usage_t,
       VEC_iterate (qualified_typedef_usage_t,
                    get_types_needing_access_check (templ),
                    get_types_needing_access_check (templ),
                    i, iter);
                    i, iter);
       ++i)
       ++i)
    if (iter->typedef_decl == type_decl && scope == iter->context)
    if (iter->typedef_decl == type_decl && scope == iter->context)
      return;
      return;
 
 
  append_type_to_template_for_access_check_1 (templ, type_decl,
  append_type_to_template_for_access_check_1 (templ, type_decl,
                                              scope, location);
                                              scope, location);
}
}
 
 
/* Set up the hash tables for template instantiations.  */
/* Set up the hash tables for template instantiations.  */
 
 
void
void
init_template_processing (void)
init_template_processing (void)
{
{
  decl_specializations = htab_create_ggc (37,
  decl_specializations = htab_create_ggc (37,
                                          hash_specialization,
                                          hash_specialization,
                                          eq_specializations,
                                          eq_specializations,
                                          ggc_free);
                                          ggc_free);
  type_specializations = htab_create_ggc (37,
  type_specializations = htab_create_ggc (37,
                                          hash_specialization,
                                          hash_specialization,
                                          eq_specializations,
                                          eq_specializations,
                                          ggc_free);
                                          ggc_free);
}
}
 
 
#include "gt-cp-pt.h"
#include "gt-cp-pt.h"
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.