• Main Page
  • Modules
  • Data Types
  • Files

osprey/kg++fe/gnu/cp/pt.c

Go to the documentation of this file.
00001 /*
00002    Copyright 2003, 2004, 2005, 2006 PathScale, Inc.  All Rights Reserved.
00003    File modified February 23, 2005 by PathScale, Inc. to add OpenMP support.
00004  */
00005 
00006 /* Handle parameterized types (templates) for GNU C++.
00007    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
00008    2001, 2002  Free Software Foundation, Inc.
00009    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
00010    Rewritten by Jason Merrill (jason@cygnus.com).
00011 
00012 This file is part of GNU CC.
00013 
00014 GNU CC is free software; you can redistribute it and/or modify
00015 it under the terms of the GNU General Public License as published by
00016 the Free Software Foundation; either version 2, or (at your option)
00017 any later version.
00018 
00019 GNU CC is distributed in the hope that it will be useful,
00020 but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with GNU CC; see the file COPYING.  If not, write to
00026 the Free Software Foundation, 59 Temple Place - Suite 330,
00027 Boston, MA 02111-1307, USA.  */
00028 
00029 /* Known bugs or deficiencies include:
00030 
00031      all methods must be provided in header files; can't use a source
00032      file that contains only the method templates and "just win".  */
00033 
00034 #include "config.h"
00035 #include "system.h"
00036 #include "obstack.h"
00037 #include "tree.h"
00038 #include "flags.h"
00039 #include "cp-tree.h"
00040 #include "tree-inline.h"
00041 #include "decl.h"
00042 #include "parse.h"
00043 #include "lex.h"
00044 #include "output.h"
00045 #include "except.h"
00046 #include "toplev.h"
00047 #include "rtl.h"
00048 #include "ggc.h"
00049 #include "timevar.h"
00050 
00051 /* The type of functions taking a tree, and some additional data, and
00052    returning an int.  */
00053 typedef int (*tree_fn_t) PARAMS ((tree, void*));
00054 
00055 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
00056    instantiations have been deferred, either because their definitions
00057    were not yet available, or because we were putting off doing the
00058    work.  The TREE_PURPOSE of each entry is a SRCLOC indicating where
00059    the instantiate request occurred; the TREE_VALUE is either a DECL
00060    (for a function or static data member), or a TYPE (for a class)
00061    indicating what we are hoping to instantiate.  */
00062 static GTY(()) tree pending_templates;
00063 static tree last_pending_template;
00064 
00065 #ifdef KEY
00066 static bool regenerating_decl_from_template = false;
00067 #endif
00068 
00069 int processing_template_parmlist;
00070 static int template_header_count;
00071 
00072 static GTY(()) tree saved_trees;
00073 static GTY(()) varray_type inline_parm_levels;
00074 static size_t inline_parm_levels_used;
00075 
00076 static GTY(()) tree current_tinst_level;
00077 
00078 static GTY(()) tree saved_access_scope;
00079 
00080 /* A map from local variable declarations in the body of the template
00081    presently being instantiated to the corresponding instantiated
00082    local variables.  */
00083 static htab_t local_specializations;
00084 
00085 #define UNIFY_ALLOW_NONE 0
00086 #define UNIFY_ALLOW_MORE_CV_QUAL 1
00087 #define UNIFY_ALLOW_LESS_CV_QUAL 2
00088 #define UNIFY_ALLOW_DERIVED 4
00089 #define UNIFY_ALLOW_INTEGER 8
00090 #define UNIFY_ALLOW_OUTER_LEVEL 16
00091 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
00092 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
00093 #define UNIFY_ALLOW_MAX_CORRECTION 128
00094 
00095 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
00096            virtual, or a base class of a virtual
00097            base.  */
00098 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
00099            type with the desired type.  */
00100 
00101 static void push_access_scope_real PARAMS ((tree, tree, tree));
00102 static void push_access_scope PARAMS ((tree));
00103 static void pop_access_scope PARAMS ((tree));
00104 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
00105                unification_kind_t, int));
00106 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
00107              unification_kind_t, int, bool));
00108 static int unify PARAMS ((tree, tree, tree, tree, int));
00109 static void add_pending_template PARAMS ((tree));
00110 static void reopen_tinst_level PARAMS ((tree));
00111 static tree classtype_mangled_name PARAMS ((tree));
00112 static char *mangle_class_name_for_template PARAMS ((const char *,
00113                  tree, tree));
00114 static tree tsubst_initializer_list PARAMS ((tree, tree));
00115 static int list_eq PARAMS ((tree, tree));
00116 static tree get_class_bindings PARAMS ((tree, tree, tree));
00117 static tree coerce_template_parms PARAMS ((tree, tree, tree,
00118              tsubst_flags_t, int));
00119 static void tsubst_enum PARAMS ((tree, tree, tree));
00120 static tree add_to_template_args PARAMS ((tree, tree));
00121 static tree add_outermost_template_args PARAMS ((tree, tree));
00122 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
00123                  tree*)); 
00124 static int  type_unification_real PARAMS ((tree, tree, tree, tree,
00125              int, unification_kind_t, int, int));
00126 static void note_template_header PARAMS ((int));
00127 static tree maybe_fold_nontype_arg PARAMS ((tree));
00128 static tree convert_nontype_argument PARAMS ((tree, tree));
00129 static tree convert_template_argument PARAMS ((tree, tree, tree,
00130                  tsubst_flags_t, int, tree));
00131 static tree get_bindings_overload PARAMS ((tree, tree, tree));
00132 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t));
00133 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
00134 static int inline_needs_template_parms PARAMS ((tree));
00135 static void push_inline_template_parms_recursive PARAMS ((tree, int));
00136 static tree retrieve_specialization PARAMS ((tree, tree));
00137 static tree retrieve_local_specialization PARAMS ((tree));
00138 static tree register_specialization PARAMS ((tree, tree, tree));
00139 static void register_local_specialization PARAMS ((tree, tree));
00140 static int reregister_specialization PARAMS ((tree, tree, tree));
00141 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
00142 static tree build_template_decl PARAMS ((tree, tree));
00143 static int mark_template_parm PARAMS ((tree, void *));
00144 static int template_parm_this_level_p PARAMS ((tree, void *));
00145 static tree tsubst_friend_function PARAMS ((tree, tree));
00146 static tree tsubst_friend_class PARAMS ((tree, tree));
00147 static int can_complete_type_without_circularity PARAMS ((tree));
00148 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
00149 static int template_decl_level PARAMS ((tree));
00150 static tree maybe_get_template_decl_from_type_decl PARAMS ((tree));
00151 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
00152 static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t));
00153 static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t));
00154 static void regenerate_decl_from_template PARAMS ((tree, tree));
00155 static tree most_specialized PARAMS ((tree, tree, tree));
00156 static tree most_specialized_class PARAMS ((tree, tree));
00157 static int template_class_depth_real PARAMS ((tree, int));
00158 static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int));
00159 static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t));
00160 static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree));
00161 static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree));
00162 static void check_specialization_scope PARAMS ((void));
00163 static tree process_partial_specialization PARAMS ((tree));
00164 static void set_current_access_from_decl PARAMS ((tree));
00165 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
00166 static tree tsubst_call_declarator_parms PARAMS ((tree, tree,
00167               tsubst_flags_t, tree));
00168 static tree get_template_base_recursive PARAMS ((tree, tree,
00169              tree, tree, tree, int)); 
00170 static tree get_template_base PARAMS ((tree, tree, tree, tree));
00171 static int verify_class_unification PARAMS ((tree, tree, tree));
00172 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
00173 static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t,
00174                tree, tree));
00175 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
00176 static int template_args_equal PARAMS ((tree, tree));
00177 static void tsubst_default_arguments PARAMS ((tree));
00178 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
00179 static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
00180 static void copy_default_args_to_explicit_spec PARAMS ((tree));
00181 static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t));
00182 
00183 /* Make the current scope suitable for access checking when we are
00184    processing T.  T can be FUNCTION_DECL for instantiated function
00185    template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for
00186    static member variable (need by instantiate_decl).  ARGS is the 
00187    template argument for TEMPLATE_DECL.  If CONTEXT is not NULL_TREE, 
00188    this is used instead of the context of T.  */
00189 
00190 void
00191 push_access_scope_real (t, args, context)
00192   tree t, args, context;
00193 {
00194   if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
00195     {
00196       /* When we are processing specialization `foo<Outer>' for code like
00197 
00198      template <class U> typename U::Inner foo ();
00199      class Outer {
00200        struct Inner {};
00201        friend Outer::Inner foo<Outer> ();
00202      };
00203 
00204    `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of
00205    its specialization.  We can get the FUNCTION_DECL with the right
00206    information because this specialization has already been
00207    registered by the friend declaration above.  */
00208 
00209       if (DECL_FUNCTION_TEMPLATE_P (t) && args)
00210   {
00211     tree full_args = tsubst_template_arg_vector
00212       (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none);
00213     tree spec = NULL_TREE;
00214     if (full_args != error_mark_node)
00215       spec = retrieve_specialization (t, full_args);
00216     if (spec)
00217       t = spec;
00218   }
00219     }
00220 
00221   if (!context)
00222     context = DECL_CONTEXT (t);
00223   if (context && TYPE_P (context))
00224     push_nested_class (context, 2);
00225   else
00226     push_to_top_level ();
00227     
00228   if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
00229     {
00230       saved_access_scope = tree_cons
00231   (NULL_TREE, current_function_decl, saved_access_scope);
00232       current_function_decl = t;
00233     }
00234 }
00235 
00236 /* Like push_access_scope_real, but always uses DECL_CONTEXT.  */
00237 
00238 void
00239 push_access_scope (t)
00240   tree t;
00241 {
00242   push_access_scope_real (t, NULL_TREE, NULL_TREE);
00243 }
00244 
00245 /* Restore the scope set up by push_access_scope.  T is the node we
00246    are processing.  */
00247 
00248 void
00249 pop_access_scope (t)
00250   tree t;
00251 {
00252   if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
00253     {
00254       current_function_decl = TREE_VALUE (saved_access_scope);
00255       saved_access_scope = TREE_CHAIN (saved_access_scope);
00256     }
00257 
00258   if (DECL_CLASS_SCOPE_P (t))
00259     pop_nested_class ();
00260   else
00261     pop_from_top_level ();
00262 }
00263 
00264 /* Do any processing required when DECL (a member template declaration
00265    using TEMPLATE_PARAMETERS as its innermost parameter list) is
00266    finished.  Returns the TEMPLATE_DECL corresponding to DECL, unless
00267    it is a specialization, in which case the DECL itself is returned.  */
00268 
00269 tree
00270 finish_member_template_decl (decl)
00271   tree decl;
00272 {
00273   if (decl == NULL_TREE || decl == void_type_node)
00274     return NULL_TREE;
00275   else if (decl == error_mark_node)
00276     /* By returning NULL_TREE, the parser will just ignore this
00277        declaration.  We have already issued the error.  */
00278     return NULL_TREE;
00279   else if (TREE_CODE (decl) == TREE_LIST)
00280     {
00281       /* Assume that the class is the only declspec.  */
00282       decl = TREE_VALUE (decl);
00283       if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
00284     && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
00285   {
00286     tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
00287     check_member_template (tmpl);
00288     return tmpl;
00289   }
00290       return NULL_TREE;
00291     }
00292   else if (TREE_CODE (decl) == FIELD_DECL)
00293     error ("data member `%D' cannot be a member template", decl);
00294   else if (DECL_TEMPLATE_INFO (decl))
00295     {
00296       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
00297   {
00298     check_member_template (DECL_TI_TEMPLATE (decl));
00299     return DECL_TI_TEMPLATE (decl);
00300   }
00301       else
00302   return decl;
00303     } 
00304   else
00305     error ("invalid member template declaration `%D'", decl);
00306 
00307   return error_mark_node;
00308 }
00309 
00310 /* Returns the template nesting level of the indicated class TYPE.
00311    
00312    For example, in:
00313      template <class T>
00314      struct A
00315      {
00316        template <class U>
00317        struct B {};
00318      };
00319 
00320    A<T>::B<U> has depth two, while A<T> has depth one.  
00321    Both A<T>::B<int> and A<int>::B<U> have depth one, if
00322    COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
00323    specializations.  
00324 
00325    This function is guaranteed to return 0 if passed NULL_TREE so
00326    that, for example, `template_class_depth (current_class_type)' is
00327    always safe.  */
00328 
00329 static int 
00330 template_class_depth_real (type, count_specializations)
00331      tree type;
00332      int count_specializations;
00333 {
00334   int depth;
00335 
00336   for (depth = 0; 
00337        type && TREE_CODE (type) != NAMESPACE_DECL;
00338        type = (TREE_CODE (type) == FUNCTION_DECL) 
00339    ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
00340     {
00341       if (TREE_CODE (type) != FUNCTION_DECL)
00342   {
00343     if (CLASSTYPE_TEMPLATE_INFO (type)
00344         && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
00345         && ((count_specializations
00346        && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
00347       || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
00348       ++depth;
00349   }
00350       else 
00351   {
00352     if (DECL_TEMPLATE_INFO (type)
00353         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
00354         && ((count_specializations
00355        && DECL_TEMPLATE_SPECIALIZATION (type))
00356       || uses_template_parms (DECL_TI_ARGS (type))))
00357       ++depth;
00358   }
00359     }
00360 
00361   return depth;
00362 }
00363 
00364 /* Returns the template nesting level of the indicated class TYPE.
00365    Like template_class_depth_real, but instantiations do not count in
00366    the depth.  */
00367 
00368 int 
00369 template_class_depth (type)
00370      tree type;
00371 {
00372   return template_class_depth_real (type, /*count_specializations=*/0);
00373 }
00374 
00375 /* Returns 1 if processing DECL as part of do_pending_inlines
00376    needs us to push template parms.  */
00377 
00378 static int
00379 inline_needs_template_parms (decl)
00380      tree decl;
00381 {
00382   if (! DECL_TEMPLATE_INFO (decl))
00383     return 0;
00384 
00385   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
00386     > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
00387 }
00388 
00389 /* Subroutine of maybe_begin_member_template_processing.
00390    Push the template parms in PARMS, starting from LEVELS steps into the
00391    chain, and ending at the beginning, since template parms are listed
00392    innermost first.  */
00393 
00394 static void
00395 push_inline_template_parms_recursive (parmlist, levels)
00396      tree parmlist;
00397      int levels;
00398 {
00399   tree parms = TREE_VALUE (parmlist);
00400   int i;
00401 
00402   if (levels > 1)
00403     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
00404 
00405   ++processing_template_decl;
00406   current_template_parms
00407     = tree_cons (size_int (processing_template_decl),
00408      parms, current_template_parms);
00409   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
00410 
00411   pushlevel (0);
00412   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 
00413     {
00414       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
00415       my_friendly_assert (DECL_P (parm), 0);
00416 
00417       switch (TREE_CODE (parm))
00418   {
00419   case TYPE_DECL:
00420   case TEMPLATE_DECL:
00421     pushdecl (parm);
00422     break;
00423 
00424   case PARM_DECL:
00425     {
00426       /* Make a CONST_DECL as is done in process_template_parm.
00427          It is ugly that we recreate this here; the original
00428          version built in process_template_parm is no longer
00429          available.  */
00430       tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
00431             TREE_TYPE (parm));
00432       DECL_ARTIFICIAL (decl) = 1;
00433       DECL_INITIAL (decl) = DECL_INITIAL (parm);
00434       SET_DECL_TEMPLATE_PARM_P (decl);
00435       pushdecl (decl);
00436     }
00437     break;
00438 
00439   default:
00440     abort ();
00441   }
00442     }
00443 }
00444 
00445 /* Restore the template parameter context for a member template or
00446    a friend template defined in a class definition.  */
00447 
00448 void
00449 maybe_begin_member_template_processing (decl)
00450      tree decl;
00451 {
00452   tree parms;
00453   int levels = 0;
00454 
00455   if (inline_needs_template_parms (decl))
00456     {
00457       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
00458       levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
00459 
00460       if (DECL_TEMPLATE_SPECIALIZATION (decl))
00461   {
00462     --levels;
00463     parms = TREE_CHAIN (parms);
00464   }
00465 
00466       push_inline_template_parms_recursive (parms, levels);
00467     }
00468 
00469   /* Remember how many levels of template parameters we pushed so that
00470      we can pop them later.  */
00471   if (!inline_parm_levels)
00472     VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
00473   if (inline_parm_levels_used == inline_parm_levels->num_elements)
00474     VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
00475   VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
00476   ++inline_parm_levels_used;
00477 }
00478 
00479 /* Undo the effects of begin_member_template_processing.  */
00480 
00481 void 
00482 maybe_end_member_template_processing ()
00483 {
00484   int i;
00485 
00486   if (!inline_parm_levels_used)
00487     return;
00488 
00489   --inline_parm_levels_used;
00490   for (i = 0; 
00491        i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
00492        ++i) 
00493     {
00494       --processing_template_decl;
00495       current_template_parms = TREE_CHAIN (current_template_parms);
00496       poplevel (0, 0, 0);
00497     }
00498 }
00499 
00500 /* Returns nonzero iff T is a member template function.  We must be
00501    careful as in
00502 
00503      template <class T> class C { void f(); }
00504 
00505    Here, f is a template function, and a member, but not a member
00506    template.  This function does not concern itself with the origin of
00507    T, only its present state.  So if we have 
00508 
00509      template <class T> class C { template <class U> void f(U); }
00510 
00511    then neither C<int>::f<char> nor C<T>::f<double> is considered
00512    to be a member template.  But, `template <class U> void
00513    C<int>::f(U)' is considered a member template.  */
00514 
00515 int
00516 is_member_template (t)
00517      tree t;
00518 {
00519   if (!DECL_FUNCTION_TEMPLATE_P (t))
00520     /* Anything that isn't a function or a template function is
00521        certainly not a member template.  */
00522     return 0;
00523 
00524   /* A local class can't have member templates.  */
00525   if (decl_function_context (t))
00526     return 0;
00527 
00528   return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
00529     /* If there are more levels of template parameters than
00530        there are template classes surrounding the declaration,
00531        then we have a member template.  */
00532     && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 
00533         template_class_depth (DECL_CONTEXT (t))));
00534 }
00535 
00536 #if 0 /* UNUSED */
00537 /* Returns nonzero iff T is a member template class.  See
00538    is_member_template for a description of what precisely constitutes
00539    a member template.  */
00540 
00541 int
00542 is_member_template_class (t)
00543      tree t;
00544 {
00545   if (!DECL_CLASS_TEMPLATE_P (t))
00546     /* Anything that isn't a class template, is certainly not a member
00547        template.  */
00548     return 0;
00549 
00550   if (!DECL_CLASS_SCOPE_P (t))
00551     /* Anything whose context isn't a class type is surely not a
00552        member template.  */
00553     return 0;
00554 
00555   /* If there are more levels of template parameters than there are
00556      template classes surrounding the declaration, then we have a
00557      member template.  */
00558   return  (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 
00559      template_class_depth (DECL_CONTEXT (t)));
00560 }
00561 #endif
00562 
00563 /* Return a new template argument vector which contains all of ARGS,
00564    but has as its innermost set of arguments the EXTRA_ARGS.  */
00565 
00566 static tree
00567 add_to_template_args (args, extra_args)
00568      tree args;
00569      tree extra_args;
00570 {
00571   tree new_args;
00572   int extra_depth;
00573   int i;
00574   int j;
00575 
00576   extra_depth = TMPL_ARGS_DEPTH (extra_args);
00577   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
00578 
00579   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
00580     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
00581 
00582   for (j = 1; j <= extra_depth; ++j, ++i)
00583     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
00584     
00585   return new_args;
00586 }
00587 
00588 /* Like add_to_template_args, but only the outermost ARGS are added to
00589    the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
00590    (EXTRA_ARGS) levels are added.  This function is used to combine
00591    the template arguments from a partial instantiation with the
00592    template arguments used to attain the full instantiation from the
00593    partial instantiation.  */
00594 
00595 static tree
00596 add_outermost_template_args (args, extra_args)
00597      tree args;
00598      tree extra_args;
00599 {
00600   tree new_args;
00601 
00602   /* If there are more levels of EXTRA_ARGS than there are ARGS,
00603      something very fishy is going on.  */
00604   my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
00605           0);
00606 
00607   /* If *all* the new arguments will be the EXTRA_ARGS, just return
00608      them.  */
00609   if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
00610     return extra_args;
00611 
00612   /* For the moment, we make ARGS look like it contains fewer levels.  */
00613   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
00614   
00615   new_args = add_to_template_args (args, extra_args);
00616 
00617   /* Now, we restore ARGS to its full dimensions.  */
00618   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
00619 
00620   return new_args;
00621 }
00622 
00623 /* Return the N levels of innermost template arguments from the ARGS.  */
00624 
00625 tree
00626 get_innermost_template_args (args, n)
00627      tree args;
00628      int n;
00629 {
00630   tree new_args;
00631   int extra_levels;
00632   int i;
00633 
00634   my_friendly_assert (n >= 0, 20000603);
00635 
00636   /* If N is 1, just return the innermost set of template arguments.  */
00637   if (n == 1)
00638     return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
00639   
00640   /* If we're not removing anything, just return the arguments we were
00641      given.  */
00642   extra_levels = TMPL_ARGS_DEPTH (args) - n;
00643   my_friendly_assert (extra_levels >= 0, 20000603);
00644   if (extra_levels == 0)
00645     return args;
00646 
00647   /* Make a new set of arguments, not containing the outer arguments.  */
00648   new_args = make_tree_vec (n);
00649   for (i = 1; i <= n; ++i)
00650     SET_TMPL_ARGS_LEVEL (new_args, i, 
00651        TMPL_ARGS_LEVEL (args, i + extra_levels));
00652 
00653   return new_args;
00654 }
00655 
00656 /* We've got a template header coming up; push to a new level for storing
00657    the parms.  */
00658 
00659 void
00660 begin_template_parm_list ()
00661 {
00662   /* We use a non-tag-transparent scope here, which causes pushtag to
00663      put tags in this scope, rather than in the enclosing class or
00664      namespace scope.  This is the right thing, since we want
00665      TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
00666      global template class, push_template_decl handles putting the
00667      TEMPLATE_DECL into top-level scope.  For a nested template class,
00668      e.g.:
00669 
00670        template <class T> struct S1 {
00671          template <class T> struct S2 {}; 
00672        };
00673 
00674      pushtag contains special code to call pushdecl_with_scope on the
00675      TEMPLATE_DECL for S2.  */
00676   begin_scope (sk_template_parms);
00677   ++processing_template_decl;
00678   ++processing_template_parmlist;
00679   note_template_header (0);
00680 }
00681 
00682 /* This routine is called when a specialization is declared.  If it is
00683    invalid to declare a specialization here, an error is reported.  */
00684 
00685 static void
00686 check_specialization_scope ()
00687 {
00688   tree scope = current_scope ();
00689 
00690   /* [temp.expl.spec] 
00691      
00692      An explicit specialization shall be declared in the namespace of
00693      which the template is a member, or, for member templates, in the
00694      namespace of which the enclosing class or enclosing class
00695      template is a member.  An explicit specialization of a member
00696      function, member class or static data member of a class template
00697      shall be declared in the namespace of which the class template
00698      is a member.  */
00699   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
00700     error ("explicit specialization in non-namespace scope `%D'",
00701         scope);
00702 
00703   /* [temp.expl.spec] 
00704 
00705      In an explicit specialization declaration for a member of a class
00706      template or a member template that appears in namespace scope,
00707      the member template and some of its enclosing class templates may
00708      remain unspecialized, except that the declaration shall not
00709      explicitly specialize a class member template if its enclosing
00710      class templates are not explicitly specialized as well.  */
00711   if (current_template_parms) 
00712     error ("enclosing class templates are not explicitly specialized");
00713 }
00714 
00715 /* We've just seen template <>.  */
00716 
00717 void
00718 begin_specialization ()
00719 {
00720   begin_scope (sk_template_spec);
00721   note_template_header (1);
00722   check_specialization_scope ();
00723 }
00724 
00725 /* Called at then end of processing a declaration preceded by
00726    template<>.  */
00727 
00728 void 
00729 end_specialization ()
00730 {
00731   finish_scope ();
00732   reset_specialization ();
00733 }
00734 
00735 /* Any template <>'s that we have seen thus far are not referring to a
00736    function specialization.  */
00737 
00738 void
00739 reset_specialization ()
00740 {
00741   processing_specialization = 0;
00742   template_header_count = 0;
00743 }
00744 
00745 /* We've just seen a template header.  If SPECIALIZATION is nonzero,
00746    it was of the form template <>.  */
00747 
00748 static void 
00749 note_template_header (specialization)
00750      int specialization;
00751 {
00752   processing_specialization = specialization;
00753   template_header_count++;
00754 }
00755 
00756 /* We're beginning an explicit instantiation.  */
00757 
00758 void
00759 begin_explicit_instantiation ()
00760 {
00761   ++processing_explicit_instantiation;
00762 }
00763 
00764 
00765 void
00766 end_explicit_instantiation ()
00767 {
00768   my_friendly_assert(processing_explicit_instantiation > 0, 0);
00769   --processing_explicit_instantiation;
00770 }
00771 
00772 /* The TYPE is being declared.  If it is a template type, that means it
00773    is a partial specialization.  Do appropriate error-checking.  */
00774 
00775 void 
00776 maybe_process_partial_specialization (type)
00777      tree type;
00778 {
00779   /* TYPE maybe an ERROR_MARK_NODE.  */
00780   tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
00781 
00782   if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
00783     {
00784       /* This is for ordinary explicit specialization and partial
00785    specialization of a template class such as:
00786 
00787      template <> class C<int>;
00788 
00789    or:
00790 
00791      template <class T> class C<T*>;
00792 
00793    Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
00794 
00795       if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
00796     && !COMPLETE_TYPE_P (type))
00797   {
00798     if (current_namespace
00799         != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
00800       {
00801         pedwarn ("specializing `%#T' in different namespace", type);
00802         cp_pedwarn_at ("  from definition of `%#D'",
00803            CLASSTYPE_TI_TEMPLATE (type));
00804       }
00805     SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
00806     if (processing_template_decl)
00807       push_template_decl (TYPE_MAIN_DECL (type));
00808   }
00809       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
00810   error ("specialization of `%T' after instantiation", type);
00811     }
00812   else if (CLASS_TYPE_P (type)
00813      && !CLASSTYPE_USE_TEMPLATE (type)
00814      && CLASSTYPE_TEMPLATE_INFO (type)
00815      && context && CLASS_TYPE_P (context)
00816      && CLASSTYPE_TEMPLATE_INFO (context))
00817     {
00818       /* This is for an explicit specialization of member class
00819    template according to [temp.expl.spec/18]:
00820 
00821      template <> template <class U> class C<int>::D;
00822 
00823    The context `C<int>' must be an implicit instantiation.
00824    Otherwise this is just a member class template declared
00825    earlier like:
00826 
00827      template <> class C<int> { template <class U> class D; };
00828      template <> template <class U> class C<int>::D;
00829 
00830    In the first case, `C<int>::D' is a specialization of `C<T>::D'
00831    while in the second case, `C<int>::D' is a primary template
00832    and `C<T>::D' may not exist.  */
00833 
00834       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
00835     && !COMPLETE_TYPE_P (type))
00836   {
00837     tree t;
00838 
00839     if (current_namespace
00840         != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
00841       {
00842         pedwarn ("specializing `%#T' in different namespace", type);
00843         cp_pedwarn_at ("  from definition of `%#D'",
00844            CLASSTYPE_TI_TEMPLATE (type));
00845       }
00846 
00847     /* Check for invalid specialization after instantiation:
00848 
00849          template <> template <> class C<int>::D<int>;
00850          template <> template <class U> class C<int>::D;  */
00851 
00852     for (t = DECL_TEMPLATE_INSTANTIATIONS
00853      (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
00854          t; t = TREE_CHAIN (t))
00855       if (TREE_VALUE (t) != type
00856     && TYPE_CONTEXT (TREE_VALUE (t)) == context)
00857         error ("specialization `%T' after instantiation `%T'",
00858          type, TREE_VALUE (t));
00859 
00860     /* Mark TYPE as a specialization.  And as a result, we only
00861        have one level of template argument for the innermost
00862        class template.  */
00863     SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
00864     CLASSTYPE_TI_ARGS (type)
00865       = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
00866   }
00867     }
00868   else if (processing_specialization)
00869     error ("explicit specialization of non-template `%T'", type);
00870 }
00871 
00872 /* Retrieve the specialization (in the sense of [temp.spec] - a
00873    specialization is either an instantiation or an explicit
00874    specialization) of TMPL for the given template ARGS.  If there is
00875    no such specialization, return NULL_TREE.  The ARGS are a vector of
00876    arguments, or a vector of vectors of arguments, in the case of
00877    templates with more than one level of parameters.  */
00878    
00879 static tree
00880 retrieve_specialization (tmpl, args)
00881      tree tmpl;
00882      tree args;
00883 {
00884   tree s;
00885 
00886   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
00887 
00888   /* There should be as many levels of arguments as there are
00889      levels of parameters.  */
00890   my_friendly_assert (TMPL_ARGS_DEPTH (args) 
00891           == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
00892           0);
00893           
00894   for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
00895        s != NULL_TREE;
00896        s = TREE_CHAIN (s))
00897     if (comp_template_args (TREE_PURPOSE (s), args))
00898       return TREE_VALUE (s);
00899 
00900   return NULL_TREE;
00901 }
00902 
00903 /* Like retrieve_specialization, but for local declarations.  */
00904 
00905 static tree
00906 retrieve_local_specialization (tmpl)
00907      tree tmpl;
00908 {
00909   return (tree) htab_find (local_specializations, tmpl);
00910 }
00911 
00912 /* Returns nonzero iff DECL is a specialization of TMPL.  */
00913 
00914 int
00915 is_specialization_of (decl, tmpl)
00916      tree decl;
00917      tree tmpl;
00918 {
00919   tree t;
00920 
00921   if (TREE_CODE (decl) == FUNCTION_DECL)
00922     {
00923       for (t = decl; 
00924      t != NULL_TREE;
00925      t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
00926   if (t == tmpl)
00927     return 1;
00928     }
00929   else 
00930     {
00931       my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
00932 
00933       for (t = TREE_TYPE (decl);
00934      t != NULL_TREE;
00935      t = CLASSTYPE_USE_TEMPLATE (t)
00936        ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
00937   if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
00938     return 1;
00939     }  
00940 
00941   return 0;
00942 }
00943 
00944 /* Register the specialization SPEC as a specialization of TMPL with
00945    the indicated ARGS.  Returns SPEC, or an equivalent prior
00946    declaration, if available.  */
00947 
00948 static tree
00949 register_specialization (spec, tmpl, args)
00950      tree spec;
00951      tree tmpl;
00952      tree args;
00953 {
00954   tree s;
00955 
00956   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
00957 
00958   if (TREE_CODE (spec) == FUNCTION_DECL 
00959       && uses_template_parms (DECL_TI_ARGS (spec)))
00960     /* This is the FUNCTION_DECL for a partial instantiation.  Don't
00961        register it; we want the corresponding TEMPLATE_DECL instead.
00962        We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
00963        the more obvious `uses_template_parms (spec)' to avoid problems
00964        with default function arguments.  In particular, given
00965        something like this:
00966 
00967           template <class T> void f(T t1, T t = T())
00968 
00969        the default argument expression is not substituted for in an
00970        instantiation unless and until it is actually needed.  */
00971     return spec;
00972     
00973   /* There should be as many levels of arguments as there are
00974      levels of parameters.  */
00975   my_friendly_assert (TMPL_ARGS_DEPTH (args) 
00976           == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
00977           0);
00978 
00979   for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
00980        s != NULL_TREE;
00981        s = TREE_CHAIN (s))
00982     {
00983       tree fn = TREE_VALUE (s);
00984 
00985       /* We can sometimes try to re-register a specialization that we've
00986    already got.  In particular, regenerate_decl_from_template
00987    calls duplicate_decls which will update the specialization
00988    list.  But, we'll still get called again here anyhow.  It's
00989    more convenient to simply allow this than to try to prevent it.  */
00990       if (fn == spec)
00991   return spec;
00992       else if (comp_template_args (TREE_PURPOSE (s), args))
00993   {
00994     if (DECL_TEMPLATE_SPECIALIZATION (spec))
00995       {
00996         if (DECL_TEMPLATE_INSTANTIATION (fn))
00997     {
00998       if (TREE_USED (fn) 
00999           || DECL_EXPLICIT_INSTANTIATION (fn))
01000         {
01001           error ("specialization of %D after instantiation",
01002         fn);
01003           return spec;
01004         }
01005       else
01006         {
01007           /* This situation should occur only if the first
01008        specialization is an implicit instantiation,
01009        the second is an explicit specialization, and
01010        the implicit instantiation has not yet been
01011        used.  That situation can occur if we have
01012        implicitly instantiated a member function and
01013        then specialized it later.
01014 
01015        We can also wind up here if a friend
01016        declaration that looked like an instantiation
01017        turns out to be a specialization:
01018 
01019          template <class T> void foo(T);
01020          class S { friend void foo<>(int) };
01021          template <> void foo(int);  
01022 
01023        We transform the existing DECL in place so that
01024        any pointers to it become pointers to the
01025        updated declaration.  
01026 
01027        If there was a definition for the template, but
01028        not for the specialization, we want this to
01029        look as if there is no definition, and vice
01030        versa.  */
01031           DECL_INITIAL (fn) = NULL_TREE;
01032           duplicate_decls (spec, fn);
01033 
01034           return fn;
01035         }
01036     }
01037         else if (DECL_TEMPLATE_SPECIALIZATION (fn))
01038     {
01039       duplicate_decls (spec, fn);
01040       return fn;
01041     }
01042       }
01043   }
01044       }
01045 
01046   DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
01047      = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
01048 
01049   return spec;
01050 }
01051 
01052 /* Unregister the specialization SPEC as a specialization of TMPL.
01053    Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
01054    if the SPEC was listed as a specialization of TMPL.  */
01055 
01056 static int
01057 reregister_specialization (tree spec, tree tmpl, tree new_spec)
01058 {
01059   tree* s;
01060 
01061   for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
01062        *s != NULL_TREE;
01063        s = &TREE_CHAIN (*s))
01064     if (TREE_VALUE (*s) == spec)
01065       {
01066   if (!new_spec)
01067     *s = TREE_CHAIN (*s);
01068   else
01069     TREE_VALUE (*s) = new_spec;
01070   return 1;
01071       }
01072 
01073   return 0;
01074 }
01075 
01076 /* Like register_specialization, but for local declarations.  We are
01077    registering SPEC, an instantiation of TMPL.  */
01078 
01079 static void
01080 register_local_specialization (spec, tmpl)
01081      tree spec;
01082      tree tmpl;
01083 {
01084   void **slot;
01085 
01086   slot = htab_find_slot (local_specializations, tmpl, INSERT);
01087   *slot = spec;
01088 }
01089 
01090 /* Print the list of candidate FNS in an error message.  */
01091 
01092 void
01093 print_candidates (fns)
01094      tree fns;
01095 {
01096   tree fn;
01097 
01098   const char *str = "candidates are:";
01099 
01100   for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
01101     {
01102       tree f;
01103 
01104       for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
01105   cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
01106       str = "               ";
01107     }
01108 }
01109 
01110 /* Returns the template (one of the functions given by TEMPLATE_ID)
01111    which can be specialized to match the indicated DECL with the
01112    explicit template args given in TEMPLATE_ID.  The DECL may be
01113    NULL_TREE if none is available.  In that case, the functions in
01114    TEMPLATE_ID are non-members.
01115 
01116    If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
01117    specialization of a member template.
01118 
01119    The template args (those explicitly specified and those deduced)
01120    are output in a newly created vector *TARGS_OUT.
01121 
01122    If it is impossible to determine the result, an error message is
01123    issued.  The error_mark_node is returned to indicate failure.  */
01124 
01125 static tree
01126 determine_specialization (template_id, decl, targs_out, 
01127         need_member_template)
01128      tree template_id;
01129      tree decl;
01130      tree* targs_out;
01131      int need_member_template;
01132 {
01133   tree fns;
01134   tree targs;
01135   tree explicit_targs;
01136   tree candidates = NULL_TREE;
01137   tree templates = NULL_TREE;
01138 
01139   *targs_out = NULL_TREE;
01140 
01141   if (template_id == error_mark_node)
01142     return error_mark_node;
01143 
01144   fns = TREE_OPERAND (template_id, 0);
01145   explicit_targs = TREE_OPERAND (template_id, 1);
01146 
01147   if (fns == error_mark_node)
01148     return error_mark_node;
01149 
01150   /* Check for baselinks.  */
01151   if (BASELINK_P (fns))
01152     fns = BASELINK_FUNCTIONS (fns);
01153 
01154   if (!is_overloaded_fn (fns))
01155     {
01156       error ("`%D' is not a function template", fns);
01157       return error_mark_node;
01158     }
01159 
01160   for (; fns; fns = OVL_NEXT (fns))
01161     {
01162       tree fn = OVL_CURRENT (fns);
01163 
01164       if (TREE_CODE (fn) == TEMPLATE_DECL)
01165   {
01166     tree decl_arg_types;
01167 
01168     /* DECL might be a specialization of FN.  */
01169 
01170     /* Adjust the type of DECL in case FN is a static member.  */
01171     decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
01172     if (DECL_STATIC_FUNCTION_P (fn) 
01173         && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01174       decl_arg_types = TREE_CHAIN (decl_arg_types);
01175 
01176     /* Check that the number of function parameters matches.
01177        For example,
01178          template <class T> void f(int i = 0);
01179          template <> void f<int>();
01180        The specialization f<int> is invalid but is not caught
01181        by get_bindings below.  */
01182 
01183     if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn)))
01184         != list_length (decl_arg_types))
01185       continue;
01186 
01187     /* See whether this function might be a specialization of this
01188        template.  */
01189     targs = get_bindings (fn, decl, explicit_targs);
01190 
01191     if (!targs)
01192       /* We cannot deduce template arguments that when used to
01193          specialize TMPL will produce DECL.  */
01194       continue;
01195 
01196     /* Save this template, and the arguments deduced.  */
01197     templates = tree_cons (targs, fn, templates);
01198   }
01199       else if (need_member_template)
01200   /* FN is an ordinary member function, and we need a
01201      specialization of a member template.  */
01202   ;
01203       else if (TREE_CODE (fn) != FUNCTION_DECL)
01204   /* We can get IDENTIFIER_NODEs here in certain erroneous
01205      cases.  */
01206   ;
01207       else if (!DECL_FUNCTION_MEMBER_P (fn))
01208   /* This is just an ordinary non-member function.  Nothing can
01209      be a specialization of that.  */
01210   ;
01211       else if (DECL_ARTIFICIAL (fn))
01212   /* Cannot specialize functions that are created implicitly.  */
01213   ;
01214       else
01215   {
01216     tree decl_arg_types;
01217 
01218     /* This is an ordinary member function.  However, since
01219        we're here, we can assume it's enclosing class is a
01220        template class.  For example,
01221        
01222          template <typename T> struct S { void f(); };
01223          template <> void S<int>::f() {}
01224 
01225        Here, S<int>::f is a non-template, but S<int> is a
01226        template class.  If FN has the same type as DECL, we
01227        might be in business.  */
01228 
01229     if (!DECL_TEMPLATE_INFO (fn))
01230       /* Its enclosing class is an explicit specialization
01231          of a template class.  This is not a candidate.  */
01232       continue;
01233 
01234     if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
01235           TREE_TYPE (TREE_TYPE (fn))))
01236       /* The return types differ.  */
01237       continue;
01238 
01239     /* Adjust the type of DECL in case FN is a static member.  */
01240     decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
01241     if (DECL_STATIC_FUNCTION_P (fn) 
01242         && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01243       decl_arg_types = TREE_CHAIN (decl_arg_types);
01244 
01245     if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 
01246        decl_arg_types))
01247       /* They match!  */
01248       candidates = tree_cons (NULL_TREE, fn, candidates);
01249   }
01250     }
01251 
01252   if (templates && TREE_CHAIN (templates))
01253     {
01254       /* We have:
01255    
01256      [temp.expl.spec]
01257 
01258      It is possible for a specialization with a given function
01259      signature to be instantiated from more than one function
01260      template.  In such cases, explicit specification of the
01261      template arguments must be used to uniquely identify the
01262      function template specialization being specialized.
01263 
01264    Note that here, there's no suggestion that we're supposed to
01265    determine which of the candidate templates is most
01266    specialized.  However, we, also have:
01267 
01268      [temp.func.order]
01269 
01270      Partial ordering of overloaded function template
01271      declarations is used in the following contexts to select
01272      the function template to which a function template
01273      specialization refers: 
01274 
01275            -- when an explicit specialization refers to a function
01276         template. 
01277 
01278    So, we do use the partial ordering rules, at least for now.
01279    This extension can only serve to make invalid programs valid,
01280    so it's safe.  And, there is strong anecdotal evidence that
01281    the committee intended the partial ordering rules to apply;
01282    the EDG front-end has that behavior, and John Spicer claims
01283    that the committee simply forgot to delete the wording in
01284    [temp.expl.spec].  */
01285      tree tmpl = most_specialized (templates, decl, explicit_targs);
01286      if (tmpl && tmpl != error_mark_node)
01287        {
01288    targs = get_bindings (tmpl, decl, explicit_targs);
01289    templates = tree_cons (targs, tmpl, NULL_TREE);
01290        }
01291     }
01292 
01293   if (templates == NULL_TREE && candidates == NULL_TREE)
01294     {
01295       cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
01296        template_id, decl);
01297       return error_mark_node;
01298     }
01299   else if ((templates && TREE_CHAIN (templates))
01300      || (candidates && TREE_CHAIN (candidates))
01301      || (templates && candidates))
01302     {
01303       cp_error_at ("ambiguous template specialization `%D' for `%+D'",
01304        template_id, decl);
01305       chainon (candidates, templates);
01306       print_candidates (candidates);
01307       return error_mark_node;
01308     }
01309 
01310   /* We have one, and exactly one, match.  */
01311   if (candidates)
01312     {
01313       /* It was a specialization of an ordinary member function in a
01314    template class.  */
01315       *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
01316       return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
01317     }
01318 
01319   /* It was a specialization of a template.  */
01320   targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
01321   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
01322     {
01323       *targs_out = copy_node (targs);
01324       SET_TMPL_ARGS_LEVEL (*targs_out, 
01325          TMPL_ARGS_DEPTH (*targs_out),
01326          TREE_PURPOSE (templates));
01327     }
01328   else
01329     *targs_out = TREE_PURPOSE (templates);
01330   return TREE_VALUE (templates);
01331 }
01332 
01333 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
01334    but with the default argument values filled in from those in the
01335    TMPL_TYPES.  */
01336       
01337 static tree
01338 copy_default_args_to_explicit_spec_1 (spec_types,
01339               tmpl_types)
01340      tree spec_types;
01341      tree tmpl_types;
01342 {
01343   tree new_spec_types;
01344 
01345   if (!spec_types)
01346     return NULL_TREE;
01347 
01348   if (spec_types == void_list_node)
01349     return void_list_node;
01350 
01351   /* Substitute into the rest of the list.  */
01352   new_spec_types =
01353     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
01354             TREE_CHAIN (tmpl_types));
01355   
01356   /* Add the default argument for this parameter.  */
01357   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
01358        TREE_VALUE (spec_types),
01359        new_spec_types);
01360 }
01361 
01362 /* DECL is an explicit specialization.  Replicate default arguments
01363    from the template it specializes.  (That way, code like:
01364 
01365      template <class T> void f(T = 3);
01366      template <> void f(double);
01367      void g () { f (); } 
01368 
01369    works, as required.)  An alternative approach would be to look up
01370    the correct default arguments at the call-site, but this approach
01371    is consistent with how implicit instantiations are handled.  */
01372 
01373 static void
01374 copy_default_args_to_explicit_spec (decl)
01375      tree decl;
01376 {
01377   tree tmpl;
01378   tree spec_types;
01379   tree tmpl_types;
01380   tree new_spec_types;
01381   tree old_type;
01382   tree new_type;
01383   tree t;
01384   tree object_type = NULL_TREE;
01385   tree in_charge = NULL_TREE;
01386   tree vtt = NULL_TREE;
01387 
01388   /* See if there's anything we need to do.  */
01389   tmpl = DECL_TI_TEMPLATE (decl);
01390   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
01391   for (t = tmpl_types; t; t = TREE_CHAIN (t))
01392     if (TREE_PURPOSE (t))
01393       break;
01394   if (!t)
01395     return;
01396 
01397   old_type = TREE_TYPE (decl);
01398   spec_types = TYPE_ARG_TYPES (old_type);
01399   
01400   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01401     {
01402       /* Remove the this pointer, but remember the object's type for
01403          CV quals.  */
01404       object_type = TREE_TYPE (TREE_VALUE (spec_types));
01405       spec_types = TREE_CHAIN (spec_types);
01406       tmpl_types = TREE_CHAIN (tmpl_types);
01407       
01408       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
01409         {
01410           /* DECL may contain more parameters than TMPL due to the extra
01411              in-charge parameter in constructors and destructors.  */
01412           in_charge = spec_types;
01413     spec_types = TREE_CHAIN (spec_types);
01414   }
01415       if (DECL_HAS_VTT_PARM_P (decl))
01416   {
01417     vtt = spec_types;
01418     spec_types = TREE_CHAIN (spec_types);
01419   }
01420     }
01421 
01422   /* Compute the merged default arguments.  */
01423   new_spec_types = 
01424     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
01425 
01426   /* Compute the new FUNCTION_TYPE.  */
01427   if (object_type)
01428     {
01429       if (vtt)
01430         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
01431                    TREE_VALUE (vtt),
01432                  new_spec_types);
01433 
01434       if (in_charge)
01435         /* Put the in-charge parameter back.  */
01436         new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
01437                    TREE_VALUE (in_charge),
01438                  new_spec_types);
01439 
01440       new_type = build_cplus_method_type (object_type,
01441             TREE_TYPE (old_type),
01442             new_spec_types);
01443     }
01444   else
01445     new_type = build_function_type (TREE_TYPE (old_type),
01446             new_spec_types);
01447   new_type = build_type_attribute_variant (new_type,
01448              TYPE_ATTRIBUTES (old_type));
01449   new_type = build_exception_variant (new_type,
01450               TYPE_RAISES_EXCEPTIONS (old_type));
01451   TREE_TYPE (decl) = new_type;
01452 }
01453 
01454 /* Check to see if the function just declared, as indicated in
01455    DECLARATOR, and in DECL, is a specialization of a function
01456    template.  We may also discover that the declaration is an explicit
01457    instantiation at this point.
01458 
01459    Returns DECL, or an equivalent declaration that should be used
01460    instead if all goes well.  Issues an error message if something is
01461    amiss.  Returns error_mark_node if the error is not easily
01462    recoverable.
01463    
01464    FLAGS is a bitmask consisting of the following flags: 
01465 
01466    2: The function has a definition.
01467    4: The function is a friend.
01468 
01469    The TEMPLATE_COUNT is the number of references to qualifying
01470    template classes that appeared in the name of the function.  For
01471    example, in
01472 
01473      template <class T> struct S { void f(); };
01474      void S<int>::f();
01475      
01476    the TEMPLATE_COUNT would be 1.  However, explicitly specialized
01477    classes are not counted in the TEMPLATE_COUNT, so that in
01478 
01479      template <class T> struct S {};
01480      template <> struct S<int> { void f(); }
01481      template <> void S<int>::f();
01482 
01483    the TEMPLATE_COUNT would be 0.  (Note that this declaration is
01484    invalid; there should be no template <>.)
01485 
01486    If the function is a specialization, it is marked as such via
01487    DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
01488    is set up correctly, and it is added to the list of specializations 
01489    for that template.  */
01490 
01491 tree
01492 check_explicit_specialization (declarator, decl, template_count, flags)
01493      tree declarator;
01494      tree decl;
01495      int template_count;
01496      int flags;
01497 {
01498   int have_def = flags & 2;
01499   int is_friend = flags & 4;
01500   int specialization = 0;
01501   int explicit_instantiation = 0;
01502   int member_specialization = 0;
01503   tree ctype = DECL_CLASS_CONTEXT (decl);
01504   tree dname = DECL_NAME (decl);
01505   tmpl_spec_kind tsk;
01506 
01507   tsk = current_tmpl_spec_kind (template_count);
01508 
01509   switch (tsk)
01510     {
01511     case tsk_none:
01512       if (processing_specialization) 
01513   {
01514     specialization = 1;
01515     SET_DECL_TEMPLATE_SPECIALIZATION (decl);
01516   }
01517       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
01518   {
01519     if (is_friend)
01520       /* This could be something like:
01521 
01522          template <class T> void f(T);
01523          class S { friend void f<>(int); }  */
01524       specialization = 1;
01525     else
01526       {
01527         /* This case handles bogus declarations like template <>
01528      template <class T> void f<int>(); */
01529 
01530         error ("template-id `%D' in declaration of primary template",
01531       declarator);
01532         return decl;
01533       }
01534   }
01535       break;
01536 
01537     case tsk_invalid_member_spec:
01538       /* The error has already been reported in
01539    check_specialization_scope.  */
01540       return error_mark_node;
01541 
01542     case tsk_invalid_expl_inst:
01543       error ("template parameter list used in explicit instantiation");
01544 
01545       /* Fall through.  */
01546 
01547     case tsk_expl_inst:
01548       if (have_def)
01549   error ("definition provided for explicit instantiation");
01550       
01551       explicit_instantiation = 1;
01552       break;
01553 
01554     case tsk_excessive_parms:
01555       error ("too many template parameter lists in declaration of `%D'", 
01556     decl);
01557       return error_mark_node;
01558 
01559       /* Fall through.  */
01560     case tsk_expl_spec:
01561       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
01562       if (ctype)
01563   member_specialization = 1;
01564       else
01565   specialization = 1;
01566       break;
01567      
01568     case tsk_insufficient_parms:
01569       if (template_header_count)
01570   {
01571     error("too few template parameter lists in declaration of `%D'", 
01572        decl);
01573     return decl;
01574   }
01575       else if (ctype != NULL_TREE
01576          && !TYPE_BEING_DEFINED (ctype)
01577          && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
01578          && !is_friend)
01579   {
01580     /* For backwards compatibility, we accept:
01581 
01582          template <class T> struct S { void f(); };
01583          void S<int>::f() {} // Missing template <>
01584 
01585        That used to be valid C++.  */
01586     if (pedantic)
01587       pedwarn
01588         ("explicit specialization not preceded by `template <>'");
01589     specialization = 1;
01590     SET_DECL_TEMPLATE_SPECIALIZATION (decl);
01591   }
01592       break;
01593 
01594     case tsk_template:
01595       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
01596   {
01597     /* This case handles bogus declarations like template <>
01598        template <class T> void f<int>(); */
01599 
01600     if (uses_template_parms (declarator))
01601       error ("partial specialization `%D' of function template",
01602           declarator);
01603     else
01604       error ("template-id `%D' in declaration of primary template",
01605           declarator);
01606     return decl;
01607   }
01608 
01609       if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
01610   /* This is a specialization of a member template, without
01611      specialization the containing class.  Something like:
01612 
01613        template <class T> struct S {
01614          template <class U> void f (U); 
01615              };
01616        template <> template <class U> void S<int>::f(U) {}
01617        
01618      That's a specialization -- but of the entire template.  */
01619   specialization = 1;
01620       break;
01621 
01622     default:
01623       abort ();
01624     }
01625 
01626   if (specialization || member_specialization)
01627     {
01628       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
01629       for (; t; t = TREE_CHAIN (t))
01630   if (TREE_PURPOSE (t))
01631     {
01632       pedwarn
01633         ("default argument specified in explicit specialization");
01634       break;
01635     }
01636       if (current_lang_name == lang_name_c)
01637   error ("template specialization with C linkage");
01638     }
01639 
01640   if (specialization || member_specialization || explicit_instantiation)
01641     {
01642       tree tmpl = NULL_TREE;
01643       tree targs = NULL_TREE;
01644 
01645       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
01646       if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
01647   {
01648     tree fns;
01649 
01650     my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 
01651             0);
01652     if (!ctype)
01653       fns = IDENTIFIER_NAMESPACE_VALUE (dname);
01654     else
01655       fns = dname;
01656 
01657     declarator = 
01658       lookup_template_function (fns, NULL_TREE);
01659   }
01660 
01661       if (declarator == error_mark_node)
01662   return error_mark_node;
01663 
01664       if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
01665   {
01666     if (!explicit_instantiation)
01667       /* A specialization in class scope.  This is invalid,
01668          but the error will already have been flagged by
01669          check_specialization_scope.  */
01670       return error_mark_node;
01671     else
01672       {
01673         /* It's not valid to write an explicit instantiation in
01674      class scope, e.g.:
01675 
01676              class C { template void f(); }
01677 
01678        This case is caught by the parser.  However, on
01679        something like:
01680          
01681        template class C { void f(); };
01682 
01683        (which is invalid) we can get here.  The error will be
01684        issued later.  */
01685         ;
01686       }
01687 
01688     return decl;
01689   }
01690       else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
01691   {
01692     /* A friend declaration.  We can't do much, because we don't
01693        know what this resolves to, yet.  */
01694     my_friendly_assert (is_friend != 0, 0);
01695     my_friendly_assert (!explicit_instantiation, 0);
01696     SET_DECL_IMPLICIT_INSTANTIATION (decl);
01697     return decl;
01698   } 
01699       else if (ctype != NULL_TREE 
01700          && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
01701        IDENTIFIER_NODE))
01702   {
01703     /* Find the list of functions in ctype that have the same
01704        name as the declared function.  */
01705     tree name = TREE_OPERAND (declarator, 0);
01706     tree fns = NULL_TREE;
01707     int idx;
01708 
01709     if (constructor_name_p (name, ctype))
01710       {
01711         int is_constructor = DECL_CONSTRUCTOR_P (decl);
01712         
01713         if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
01714       : !TYPE_HAS_DESTRUCTOR (ctype))
01715     {
01716       /* From [temp.expl.spec]:
01717            
01718          If such an explicit specialization for the member
01719          of a class template names an implicitly-declared
01720          special member function (clause _special_), the
01721          program is ill-formed.  
01722 
01723          Similar language is found in [temp.explicit].  */
01724       error ("specialization of implicitly-declared special member function");
01725       return error_mark_node;
01726     }
01727 
01728         name = is_constructor ? ctor_identifier : dtor_identifier;
01729       }
01730 
01731     if (!DECL_CONV_FN_P (decl))
01732       {
01733         idx = lookup_fnfields_1 (ctype, name);
01734         if (idx >= 0)
01735     fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
01736       }
01737     else
01738       {
01739         tree methods;
01740 
01741         /* For a type-conversion operator, we cannot do a
01742      name-based lookup.  We might be looking for `operator
01743      int' which will be a specialization of `operator T'.
01744      So, we find *all* the conversion operators, and then
01745      select from them.  */
01746         fns = NULL_TREE;
01747 
01748         methods = CLASSTYPE_METHOD_VEC (ctype);
01749         if (methods)
01750     for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
01751          idx < TREE_VEC_LENGTH (methods); ++idx) 
01752       {
01753         tree ovl = TREE_VEC_ELT (methods, idx);
01754 
01755         if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
01756           /* There are no more conversion functions.  */
01757           break;
01758 
01759         /* Glue all these conversion functions together
01760            with those we already have.  */
01761         for (; ovl; ovl = OVL_NEXT (ovl))
01762           fns = ovl_cons (OVL_CURRENT (ovl), fns);
01763       }
01764       }
01765         
01766     if (fns == NULL_TREE) 
01767       {
01768         error ("no member function `%D' declared in `%T'",
01769       name, ctype);
01770         return error_mark_node;
01771       }
01772     else
01773       TREE_OPERAND (declarator, 0) = fns;
01774   }
01775       
01776       /* Figure out what exactly is being specialized at this point.
01777    Note that for an explicit instantiation, even one for a
01778    member function, we cannot tell apriori whether the
01779    instantiation is for a member template, or just a member
01780    function of a template class.  Even if a member template is
01781    being instantiated, the member template arguments may be
01782    elided if they can be deduced from the rest of the
01783    declaration.  */
01784       tmpl = determine_specialization (declarator, decl,
01785                &targs, 
01786                member_specialization);
01787       
01788       if (!tmpl || tmpl == error_mark_node)
01789   /* We couldn't figure out what this declaration was
01790      specializing.  */
01791   return error_mark_node;
01792       else
01793   {
01794     tree gen_tmpl = most_general_template (tmpl);
01795 
01796     if (explicit_instantiation)
01797       {
01798         /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
01799      is done by do_decl_instantiation later.  */ 
01800 
01801         int arg_depth = TMPL_ARGS_DEPTH (targs);
01802         int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
01803 
01804         if (arg_depth > parm_depth)
01805     {
01806       /* If TMPL is not the most general template (for
01807          example, if TMPL is a friend template that is
01808          injected into namespace scope), then there will
01809          be too many levels of TARGS.  Remove some of them
01810          here.  */
01811       int i;
01812       tree new_targs;
01813 
01814       new_targs = make_tree_vec (parm_depth);
01815       for (i = arg_depth - parm_depth; i < arg_depth; ++i)
01816         TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
01817           = TREE_VEC_ELT (targs, i);
01818       targs = new_targs;
01819     }
01820       
01821         return instantiate_template (tmpl, targs);
01822       }
01823 
01824     /* If we thought that the DECL was a member function, but it
01825        turns out to be specializing a static member function,
01826        make DECL a static member function as well.  We also have
01827        to adjust last_function_parms to avoid confusing
01828        start_function later.  */
01829     if (DECL_STATIC_FUNCTION_P (tmpl)
01830         && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
01831       {
01832         revert_static_member_fn (decl);
01833         last_function_parms = TREE_CHAIN (last_function_parms);
01834       }
01835 
01836     /* If this is a specialization of a member template of a
01837        template class.  In we want to return the TEMPLATE_DECL,
01838        not the specialization of it.  */
01839     if (tsk == tsk_template)
01840       {
01841         SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
01842         DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
01843         return tmpl;
01844       }
01845 
01846     /* Set up the DECL_TEMPLATE_INFO for DECL.  */
01847     DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
01848 
01849     /* Inherit default function arguments from the template
01850        DECL is specializing.  */
01851     copy_default_args_to_explicit_spec (decl);
01852 
01853     /* This specialization has the same protection as the
01854        template it specializes.  */
01855     TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
01856     TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
01857 
01858     if (is_friend && !have_def)
01859       /* This is not really a declaration of a specialization.
01860          It's just the name of an instantiation.  But, it's not
01861          a request for an instantiation, either.  */
01862       SET_DECL_IMPLICIT_INSTANTIATION (decl);
01863     else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
01864       /* This is indeed a specialization.  In case of constructors
01865          and destructors, we need in-charge and not-in-charge
01866          versions in V3 ABI.  */
01867       clone_function_decl (decl, /*update_method_vec_p=*/0);
01868 
01869     /* Register this specialization so that we can find it
01870        again.  */
01871     decl = register_specialization (decl, gen_tmpl, targs);
01872   }
01873     }
01874   
01875   return decl;
01876 }
01877 
01878 /* TYPE is being declared.  Verify that the use of template headers
01879    and such is reasonable.  Issue error messages if not.  */
01880 
01881 void
01882 maybe_check_template_type (type)
01883      tree type;
01884 {
01885   if (template_header_count)
01886     {
01887       /* We are in the scope of some `template <...>' header.  */
01888 
01889       int context_depth 
01890   = template_class_depth_real (TYPE_CONTEXT (type),
01891              /*count_specializations=*/1);
01892 
01893       if (template_header_count <= context_depth)
01894   /* This is OK; the template headers are for the context.  We
01895      are actually too lenient here; like
01896      check_explicit_specialization we should consider the number
01897      of template types included in the actual declaration.  For
01898      example, 
01899 
01900        template <class T> struct S {
01901          template <class U> template <class V>
01902          struct I {};
01903        }; 
01904 
01905      is invalid, but:
01906 
01907        template <class T> struct S {
01908          template <class U> struct I;
01909        }; 
01910 
01911        template <class T> template <class U.
01912        struct S<T>::I {};
01913 
01914      is not.  */
01915   ; 
01916       else if (template_header_count > context_depth + 1)
01917   /* There are two many template parameter lists.  */
01918   error ("too many template parameter lists in declaration of `%T'", type); 
01919     }
01920 }
01921 
01922 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
01923    parameters.  These are represented in the same format used for
01924    DECL_TEMPLATE_PARMS.  */
01925 
01926 int comp_template_parms (parms1, parms2)
01927      tree parms1;
01928      tree parms2;
01929 {
01930   tree p1;
01931   tree p2;
01932 
01933   if (parms1 == parms2)
01934     return 1;
01935 
01936   for (p1 = parms1, p2 = parms2; 
01937        p1 != NULL_TREE && p2 != NULL_TREE;
01938        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
01939     {
01940       tree t1 = TREE_VALUE (p1);
01941       tree t2 = TREE_VALUE (p2);
01942       int i;
01943 
01944       my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
01945       my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
01946 
01947       if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
01948   return 0;
01949 
01950       for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) 
01951   {
01952     tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
01953     tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
01954 
01955     if (TREE_CODE (parm1) != TREE_CODE (parm2))
01956       return 0;
01957 
01958     if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
01959       continue;
01960     else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
01961       return 0;
01962   }
01963     }
01964 
01965   if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
01966     /* One set of parameters has more parameters lists than the
01967        other.  */
01968     return 0;
01969 
01970   return 1;
01971 }
01972 
01973 /* Complain if DECL shadows a template parameter.
01974 
01975    [temp.local]: A template-parameter shall not be redeclared within its
01976    scope (including nested scopes).  */
01977 
01978 void
01979 check_template_shadow (decl)
01980      tree decl;
01981 {
01982   tree olddecl;
01983 
01984   /* If we're not in a template, we can't possibly shadow a template
01985      parameter.  */
01986   if (!current_template_parms)
01987     return;
01988 
01989   /* Figure out what we're shadowing.  */
01990   if (TREE_CODE (decl) == OVERLOAD)
01991     decl = OVL_CURRENT (decl);
01992   olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
01993 
01994   /* If there's no previous binding for this name, we're not shadowing
01995      anything, let alone a template parameter.  */
01996   if (!olddecl)
01997     return;
01998 
01999   /* If we're not shadowing a template parameter, we're done.  Note
02000      that OLDDECL might be an OVERLOAD (or perhaps even an
02001      ERROR_MARK), so we can't just blithely assume it to be a _DECL
02002      node.  */
02003   if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
02004     return;
02005 
02006   /* We check for decl != olddecl to avoid bogus errors for using a
02007      name inside a class.  We check TPFI to avoid duplicate errors for
02008      inline member templates.  */
02009   if (decl == olddecl 
02010       || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
02011     return;
02012 
02013   cp_error_at ("declaration of `%#D'", decl);
02014   cp_error_at (" shadows template parm `%#D'", olddecl);
02015 }
02016 
02017 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
02018    ORIG_LEVEL, DECL, and TYPE.  */
02019 
02020 static tree
02021 build_template_parm_index (index, level, orig_level, decl, type)
02022      int index;
02023      int level;
02024      int orig_level;
02025      tree decl;
02026      tree type;
02027 {
02028   tree t = make_node (TEMPLATE_PARM_INDEX);
02029   TEMPLATE_PARM_IDX (t) = index;
02030   TEMPLATE_PARM_LEVEL (t) = level;
02031   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
02032   TEMPLATE_PARM_DECL (t) = decl;
02033   TREE_TYPE (t) = type;
02034 
02035   return t;
02036 }
02037 
02038 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
02039    TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
02040    TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
02041    new one is created.  */
02042 
02043 static tree 
02044 reduce_template_parm_level (index, type, levels)
02045      tree index;
02046      tree type;
02047      int levels;
02048 {
02049   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
02050       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
02051     != TEMPLATE_PARM_LEVEL (index) - levels))
02052     {
02053       tree decl 
02054   = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
02055           DECL_NAME (TEMPLATE_PARM_DECL (index)),
02056           type);
02057       tree t
02058   = build_template_parm_index (TEMPLATE_PARM_IDX (index),
02059              TEMPLATE_PARM_LEVEL (index) - levels,
02060              TEMPLATE_PARM_ORIG_LEVEL (index),
02061              decl, type);
02062       TEMPLATE_PARM_DESCENDANTS (index) = t;
02063 
02064       DECL_ARTIFICIAL (decl) = 1;
02065       SET_DECL_TEMPLATE_PARM_P (decl);
02066 
02067       /* Template template parameters need this.  */
02068       DECL_TEMPLATE_PARMS (decl)
02069   = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
02070     }
02071 
02072   return TEMPLATE_PARM_DESCENDANTS (index);
02073 }
02074 
02075 /* Process information from new template parameter NEXT and append it to the
02076    LIST being built.  */
02077 
02078 tree
02079 process_template_parm (list, next)
02080      tree list, next;
02081 {
02082   tree parm;
02083   tree decl = 0;
02084   tree defval;
02085   int is_type, idx;
02086 
02087   parm = next;
02088   my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
02089   defval = TREE_PURPOSE (parm);
02090   parm = TREE_VALUE (parm);
02091   is_type = TREE_PURPOSE (parm) == class_type_node;
02092 
02093   if (list)
02094     {
02095       tree p = TREE_VALUE (tree_last (list));
02096 
02097       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
02098   idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
02099       else
02100   idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
02101       ++idx;
02102     }
02103   else
02104     idx = 0;
02105 
02106   if (!is_type)
02107     {
02108       my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
02109       /* is a const-param */
02110       parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
02111            PARM, 0, NULL);
02112       SET_DECL_TEMPLATE_PARM_P (parm);
02113 
02114       /* [temp.param]
02115 
02116    The top-level cv-qualifiers on the template-parameter are
02117    ignored when determining its type.  */
02118       TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
02119 
02120       /* A template parameter is not modifiable.  */
02121       TREE_READONLY (parm) = 1;
02122       if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
02123         TREE_TYPE (parm) = void_type_node;
02124       decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
02125       DECL_INITIAL (parm) = DECL_INITIAL (decl) 
02126   = build_template_parm_index (idx, processing_template_decl,
02127              processing_template_decl,
02128              decl, TREE_TYPE (parm));
02129     }
02130   else
02131     {
02132       tree t;
02133       parm = TREE_VALUE (parm);
02134       
02135       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
02136   {
02137     t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
02138     /* This is for distinguishing between real templates and template 
02139        template parameters */
02140     TREE_TYPE (parm) = t;
02141     TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
02142     decl = parm;
02143   }
02144       else
02145   {
02146     t = make_aggr_type (TEMPLATE_TYPE_PARM);
02147     /* parm is either IDENTIFIER_NODE or NULL_TREE */
02148     decl = build_decl (TYPE_DECL, parm, t);
02149   }
02150         
02151       TYPE_NAME (t) = decl;
02152       TYPE_STUB_DECL (t) = decl;
02153       parm = decl;
02154       TEMPLATE_TYPE_PARM_INDEX (t)
02155   = build_template_parm_index (idx, processing_template_decl, 
02156              processing_template_decl,
02157              decl, TREE_TYPE (parm));
02158     }
02159   DECL_ARTIFICIAL (decl) = 1;
02160   SET_DECL_TEMPLATE_PARM_P (decl);
02161   pushdecl (decl);
02162   parm = build_tree_list (defval, parm);
02163   return chainon (list, parm);
02164 }
02165 
02166 /* The end of a template parameter list has been reached.  Process the
02167    tree list into a parameter vector, converting each parameter into a more
02168    useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
02169    as PARM_DECLs.  */
02170 
02171 tree
02172 end_template_parm_list (parms)
02173      tree parms;
02174 {
02175   int nparms;
02176   tree parm, next;
02177   tree saved_parmlist = make_tree_vec (list_length (parms));
02178 
02179   current_template_parms
02180     = tree_cons (size_int (processing_template_decl),
02181      saved_parmlist, current_template_parms);
02182 
02183   for (parm = parms, nparms = 0; parm; parm = next, nparms++)
02184     {
02185       next = TREE_CHAIN (parm);
02186       TREE_VEC_ELT (saved_parmlist, nparms) = parm;
02187       TREE_CHAIN (parm) = NULL_TREE;
02188     }
02189 
02190   --processing_template_parmlist;
02191 
02192   return saved_parmlist;
02193 }
02194 
02195 /* end_template_decl is called after a template declaration is seen.  */
02196 
02197 void
02198 end_template_decl ()
02199 {
02200   reset_specialization ();
02201 
02202   if (! processing_template_decl)
02203     return;
02204 
02205   /* This matches the pushlevel in begin_template_parm_list.  */
02206   finish_scope ();
02207 
02208   --processing_template_decl;
02209   current_template_parms = TREE_CHAIN (current_template_parms);
02210 }
02211 
02212 /* Given a template argument vector containing the template PARMS.
02213    The innermost PARMS are given first.  */
02214 
02215 tree
02216 current_template_args ()
02217 {
02218   tree header;
02219   tree args = NULL_TREE;
02220   int length = TMPL_PARMS_DEPTH (current_template_parms);
02221   int l = length;
02222 
02223   /* If there is only one level of template parameters, we do not
02224      create a TREE_VEC of TREE_VECs.  Instead, we return a single
02225      TREE_VEC containing the arguments.  */
02226   if (length > 1)
02227     args = make_tree_vec (length);
02228 
02229   for (header = current_template_parms; header; header = TREE_CHAIN (header))
02230     {
02231       tree a = copy_node (TREE_VALUE (header));
02232       int i;
02233 
02234       TREE_TYPE (a) = NULL_TREE;
02235       for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
02236   {
02237     tree t = TREE_VEC_ELT (a, i);
02238 
02239     /* T will be a list if we are called from within a
02240        begin/end_template_parm_list pair, but a vector directly
02241        if within a begin/end_member_template_processing pair.  */
02242     if (TREE_CODE (t) == TREE_LIST) 
02243       {
02244         t = TREE_VALUE (t);
02245         
02246         if (TREE_CODE (t) == TYPE_DECL 
02247       || TREE_CODE (t) == TEMPLATE_DECL)
02248     t = TREE_TYPE (t);
02249         else
02250     t = DECL_INITIAL (t);
02251         TREE_VEC_ELT (a, i) = t;
02252       }
02253   }
02254 
02255       if (length > 1)
02256   TREE_VEC_ELT (args, --l) = a;
02257       else
02258   args = a;
02259     }
02260 
02261   return args;
02262 }
02263 
02264 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
02265    template PARMS.  Used by push_template_decl below.  */
02266 
02267 static tree
02268 build_template_decl (decl, parms)
02269      tree decl;
02270      tree parms;
02271 {
02272   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
02273   DECL_TEMPLATE_PARMS (tmpl) = parms;
02274   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
02275   if (DECL_LANG_SPECIFIC (decl))
02276     {
02277       DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
02278       DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
02279       DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
02280       DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
02281       DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
02282       if (DECL_OVERLOADED_OPERATOR_P (decl))
02283   SET_OVERLOADED_OPERATOR_CODE (tmpl, 
02284               DECL_OVERLOADED_OPERATOR_P (decl));
02285     }
02286 
02287   return tmpl;
02288 }
02289 
02290 struct template_parm_data
02291 {
02292   /* The level of the template parameters we are currently
02293      processing.  */
02294   int level;
02295 
02296   /* The index of the specialization argument we are currently
02297      processing.  */
02298   int current_arg;
02299 
02300   /* An array whose size is the number of template parameters.  The
02301      elements are nonzero if the parameter has been used in any one
02302      of the arguments processed so far.  */
02303   int* parms;
02304 
02305   /* An array whose size is the number of template arguments.  The
02306      elements are nonzero if the argument makes use of template
02307      parameters of this level.  */
02308   int* arg_uses_template_parms;
02309 };
02310 
02311 /* Subroutine of push_template_decl used to see if each template
02312    parameter in a partial specialization is used in the explicit
02313    argument list.  If T is of the LEVEL given in DATA (which is
02314    treated as a template_parm_data*), then DATA->PARMS is marked
02315    appropriately.  */
02316 
02317 static int
02318 mark_template_parm (t, data)
02319      tree t;
02320      void* data;
02321 {
02322   int level;
02323   int idx;
02324   struct template_parm_data* tpd = (struct template_parm_data*) data;
02325 
02326   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
02327     {
02328       level = TEMPLATE_PARM_LEVEL (t);
02329       idx = TEMPLATE_PARM_IDX (t);
02330     }
02331   else
02332     {
02333       level = TEMPLATE_TYPE_LEVEL (t);
02334       idx = TEMPLATE_TYPE_IDX (t);
02335     }
02336 
02337   if (level == tpd->level)
02338     {
02339       tpd->parms[idx] = 1;
02340       tpd->arg_uses_template_parms[tpd->current_arg] = 1;
02341     }
02342 
02343   /* Return zero so that for_each_template_parm will continue the
02344      traversal of the tree; we want to mark *every* template parm.  */
02345   return 0;
02346 }
02347 
02348 /* Process the partial specialization DECL.  */
02349 
02350 static tree
02351 process_partial_specialization (decl)
02352      tree decl;
02353 {
02354   tree type = TREE_TYPE (decl);
02355   tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
02356   tree specargs = CLASSTYPE_TI_ARGS (type);
02357   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
02358   tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
02359   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
02360   int nargs = TREE_VEC_LENGTH (inner_args);
02361   int ntparms = TREE_VEC_LENGTH (inner_parms);
02362   int  i;
02363   int did_error_intro = 0;
02364   struct template_parm_data tpd;
02365   struct template_parm_data tpd2;
02366 
02367   /* We check that each of the template parameters given in the
02368      partial specialization is used in the argument list to the
02369      specialization.  For example:
02370 
02371        template <class T> struct S;
02372        template <class T> struct S<T*>;
02373 
02374      The second declaration is OK because `T*' uses the template
02375      parameter T, whereas
02376 
02377        template <class T> struct S<int>;
02378 
02379      is no good.  Even trickier is:
02380 
02381        template <class T>
02382        struct S1
02383        {
02384     template <class U>
02385     struct S2;
02386     template <class U>
02387     struct S2<T>;
02388        };
02389 
02390      The S2<T> declaration is actually invalid; it is a
02391      full-specialization.  Of course, 
02392 
02393     template <class U>
02394     struct S2<T (*)(U)>;
02395 
02396      or some such would have been OK.  */
02397   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
02398   tpd.parms = alloca (sizeof (int) * ntparms);
02399   memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
02400 
02401   tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
02402   memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
02403   for (i = 0; i < nargs; ++i)
02404     {
02405       tpd.current_arg = i;
02406       for_each_template_parm (TREE_VEC_ELT (inner_args, i),
02407             &mark_template_parm,
02408             &tpd,
02409             NULL);
02410     }
02411   for (i = 0; i < ntparms; ++i)
02412     if (tpd.parms[i] == 0)
02413       {
02414   /* One of the template parms was not used in the
02415      specialization.  */
02416   if (!did_error_intro)
02417     {
02418       error ("template parameters not used in partial specialization:");
02419       did_error_intro = 1;
02420     }
02421 
02422   error ("        `%D'", 
02423       TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
02424       }
02425 
02426   /* [temp.class.spec]
02427 
02428      The argument list of the specialization shall not be identical to
02429      the implicit argument list of the primary template.  */
02430   if (comp_template_args 
02431       (inner_args, 
02432        INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
02433                (maintmpl)))))
02434     error ("partial specialization `%T' does not specialize any template arguments", type);
02435 
02436   /* [temp.class.spec]
02437 
02438      A partially specialized non-type argument expression shall not
02439      involve template parameters of the partial specialization except
02440      when the argument expression is a simple identifier.
02441 
02442      The type of a template parameter corresponding to a specialized
02443      non-type argument shall not be dependent on a parameter of the
02444      specialization.  */
02445   my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
02446   tpd2.parms = 0;
02447   for (i = 0; i < nargs; ++i)
02448     {
02449       tree arg = TREE_VEC_ELT (inner_args, i);
02450       if (/* These first two lines are the `non-type' bit.  */
02451     !TYPE_P (arg)
02452     && TREE_CODE (arg) != TEMPLATE_DECL
02453     /* This next line is the `argument expression is not just a
02454        simple identifier' condition and also the `specialized
02455        non-type argument' bit.  */
02456     && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
02457   {
02458     if (tpd.arg_uses_template_parms[i])
02459       error ("template argument `%E' involves template parameter(s)", arg);
02460     else 
02461       {
02462         /* Look at the corresponding template parameter,
02463      marking which template parameters its type depends
02464      upon.  */
02465         tree type = 
02466     TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, 
02467                  i)));
02468 
02469         if (!tpd2.parms)
02470     {
02471       /* We haven't yet initialized TPD2.  Do so now.  */
02472       tpd2.arg_uses_template_parms 
02473         =  (int*) alloca (sizeof (int) * nargs);
02474       /* The number of parameters here is the number in the
02475          main template, which, as checked in the assertion
02476          above, is NARGS.  */
02477       tpd2.parms = (int*) alloca (sizeof (int) * nargs);
02478       tpd2.level = 
02479         TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
02480     }
02481 
02482         /* Mark the template parameters.  But this time, we're
02483      looking for the template parameters of the main
02484      template, not in the specialization.  */
02485         tpd2.current_arg = i;
02486         tpd2.arg_uses_template_parms[i] = 0;
02487         memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
02488         for_each_template_parm (type,
02489               &mark_template_parm,
02490               &tpd2,
02491               NULL);
02492       
02493         if (tpd2.arg_uses_template_parms [i])
02494     {
02495       /* The type depended on some template parameters.
02496          If they are fully specialized in the
02497          specialization, that's OK.  */
02498       int j;
02499       for (j = 0; j < nargs; ++j)
02500         if (tpd2.parms[j] != 0
02501       && tpd.arg_uses_template_parms [j])
02502           {
02503       error ("type `%T' of template argument `%E' depends on template parameter(s)", 
02504           type,
02505           arg);
02506       break;
02507           }
02508     }
02509       }
02510   }
02511     }
02512 
02513   if (retrieve_specialization (maintmpl, specargs))
02514     /* We've already got this specialization.  */
02515     return decl;
02516 
02517   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
02518     = tree_cons (inner_args, inner_parms,
02519      DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
02520   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
02521   return decl;
02522 }
02523 
02524 /* Check that a template declaration's use of default arguments is not
02525    invalid.  Here, PARMS are the template parameters.  IS_PRIMARY is
02526    nonzero if DECL is the thing declared by a primary template.
02527    IS_PARTIAL is nonzero if DECL is a partial specialization.  */
02528 
02529 static void
02530 check_default_tmpl_args (decl, parms, is_primary, is_partial)
02531      tree decl;
02532      tree parms;
02533      int is_primary;
02534      int is_partial;
02535 {
02536   const char *msg;
02537   int last_level_to_check;
02538   tree parm_level;
02539 
02540   /* [temp.param] 
02541 
02542      A default template-argument shall not be specified in a
02543      function template declaration or a function template definition, nor
02544      in the template-parameter-list of the definition of a member of a
02545      class template.  */
02546 
02547   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
02548     /* You can't have a function template declaration in a local
02549        scope, nor you can you define a member of a class template in a
02550        local scope.  */
02551     return;
02552 
02553   if (current_class_type
02554       && !TYPE_BEING_DEFINED (current_class_type)
02555       && DECL_LANG_SPECIFIC (decl)
02556       /* If this is either a friend defined in the scope of the class
02557    or a member function.  */
02558       && ((DECL_CONTEXT (decl) 
02559      && same_type_p (DECL_CONTEXT (decl), current_class_type))
02560     || (DECL_FRIEND_CONTEXT (decl)
02561         && same_type_p (DECL_FRIEND_CONTEXT (decl), 
02562             current_class_type)))
02563       /* And, if it was a member function, it really was defined in
02564    the scope of the class.  */
02565       && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
02566     /* We already checked these parameters when the template was
02567        declared, so there's no need to do it again now.  This function
02568        was defined in class scope, but we're processing it's body now
02569        that the class is complete.  */
02570     return;
02571 
02572   /* [temp.param]
02573    
02574      If a template-parameter has a default template-argument, all
02575      subsequent template-parameters shall have a default
02576      template-argument supplied.  */
02577   for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
02578     {
02579       tree inner_parms = TREE_VALUE (parm_level);
02580       int ntparms = TREE_VEC_LENGTH (inner_parms);
02581       int seen_def_arg_p = 0; 
02582       int i;
02583 
02584       for (i = 0; i < ntparms; ++i) 
02585   {
02586     tree parm = TREE_VEC_ELT (inner_parms, i);
02587     if (TREE_PURPOSE (parm))
02588       seen_def_arg_p = 1;
02589     else if (seen_def_arg_p)
02590       {
02591         error ("no default argument for `%D'", TREE_VALUE (parm));
02592         /* For better subsequent error-recovery, we indicate that
02593      there should have been a default argument.  */
02594         TREE_PURPOSE (parm) = error_mark_node;
02595       }
02596   }
02597     }
02598 
02599   if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
02600     /* For an ordinary class template, default template arguments are
02601        allowed at the innermost level, e.g.:
02602          template <class T = int>
02603    struct S {};
02604        but, in a partial specialization, they're not allowed even
02605        there, as we have in [temp.class.spec]:
02606      
02607    The template parameter list of a specialization shall not
02608    contain default template argument values.  
02609 
02610        So, for a partial specialization, or for a function template,
02611        we look at all of them.  */
02612     ;
02613   else
02614     /* But, for a primary class template that is not a partial
02615        specialization we look at all template parameters except the
02616        innermost ones.  */
02617     parms = TREE_CHAIN (parms);
02618 
02619   /* Figure out what error message to issue.  */
02620   if (TREE_CODE (decl) == FUNCTION_DECL)
02621     msg = "default template arguments may not be used in function templates";
02622   else if (is_partial)
02623     msg = "default template arguments may not be used in partial specializations";
02624   else
02625     msg = "default argument for template parameter for class enclosing `%D'";
02626 
02627   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
02628     /* If we're inside a class definition, there's no need to
02629        examine the parameters to the class itself.  On the one
02630        hand, they will be checked when the class is defined, and,
02631        on the other, default arguments are valid in things like:
02632          template <class T = double>
02633          struct S { template <class U> void f(U); };
02634        Here the default argument for `S' has no bearing on the
02635        declaration of `f'.  */
02636     last_level_to_check = template_class_depth (current_class_type) + 1;
02637   else
02638     /* Check everything.  */
02639     last_level_to_check = 0;
02640 
02641   for (parm_level = parms; 
02642        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; 
02643        parm_level = TREE_CHAIN (parm_level))
02644     {
02645       tree inner_parms = TREE_VALUE (parm_level);
02646       int i;
02647       int ntparms;
02648 
02649       ntparms = TREE_VEC_LENGTH (inner_parms);
02650       for (i = 0; i < ntparms; ++i) 
02651   if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
02652     {
02653       if (msg)
02654         {
02655     error (msg, decl);
02656     msg = 0;
02657         }
02658 
02659       /* Clear out the default argument so that we are not
02660          confused later.  */
02661       TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
02662     }
02663 
02664       /* At this point, if we're still interested in issuing messages,
02665    they must apply to classes surrounding the object declared.  */
02666       if (msg)
02667   msg = "default argument for template parameter for class enclosing `%D'"; 
02668     }
02669 }
02670 
02671 /* Worker for push_template_decl_real, called via
02672    for_each_template_parm.  DATA is really an int, indicating the
02673    level of the parameters we are interested in.  If T is a template
02674    parameter of that level, return nonzero.  */
02675 
02676 static int
02677 template_parm_this_level_p (t, data)
02678      tree t;
02679      void *data;
02680 {
02681   int this_level = *(int *)data;
02682   int level;
02683 
02684   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
02685     level = TEMPLATE_PARM_LEVEL (t);
02686   else
02687     level = TEMPLATE_TYPE_LEVEL (t);
02688   return level == this_level;
02689 }
02690 
02691 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
02692    parameters given by current_template_args, or reuses a
02693    previously existing one, if appropriate.  Returns the DECL, or an
02694    equivalent one, if it is replaced via a call to duplicate_decls.  
02695 
02696    If IS_FRIEND is nonzero, DECL is a friend declaration.  */
02697 
02698 tree
02699 push_template_decl_real (decl, is_friend)
02700      tree decl;
02701      int is_friend;
02702 {
02703   tree tmpl;
02704   tree args;
02705   tree info;
02706   tree ctx;
02707   int primary;
02708   int is_partial;
02709   int new_template_p = 0;
02710 
02711   /* See if this is a partial specialization.  */
02712   is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
02713     && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
02714     && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
02715 
02716   is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
02717 
02718   if (is_friend)
02719     /* For a friend, we want the context of the friend function, not
02720        the type of which it is a friend.  */
02721     ctx = DECL_CONTEXT (decl);
02722   else if (CP_DECL_CONTEXT (decl)
02723      && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
02724     /* In the case of a virtual function, we want the class in which
02725        it is defined.  */
02726     ctx = CP_DECL_CONTEXT (decl);
02727   else
02728     /* Otherwise, if we're currently defining some class, the DECL
02729        is assumed to be a member of the class.  */
02730     ctx = current_scope ();
02731 
02732   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
02733     ctx = NULL_TREE;
02734 
02735   if (!DECL_CONTEXT (decl))
02736     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
02737 
02738   /* See if this is a primary template.  */
02739   primary = template_parm_scope_p ();
02740 
02741   if (primary)
02742     {
02743       if (current_lang_name == lang_name_c)
02744   error ("template with C linkage");
02745       else if (TREE_CODE (decl) == TYPE_DECL 
02746          && ANON_AGGRNAME_P (DECL_NAME (decl))) 
02747   error ("template class without a name");
02748       else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
02749     && CLASS_TYPE_P (TREE_TYPE (decl)))
02750          || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
02751          || TREE_CODE (decl) == FUNCTION_DECL)
02752   /* OK */;
02753       else
02754   error ("template declaration of `%#D'", decl);
02755     }
02756 
02757   /* Check to see that the rules regarding the use of default
02758      arguments are not being violated.  */
02759   check_default_tmpl_args (decl, current_template_parms, 
02760          primary, is_partial);
02761 
02762   if (is_partial)
02763     return process_partial_specialization (decl);
02764 
02765   args = current_template_args ();
02766 
02767   if (!ctx 
02768       || TREE_CODE (ctx) == FUNCTION_DECL
02769       || (TREE_CODE (ctx) != TEMPLATE_TYPE_PARM
02770     && TREE_CODE (ctx) != BOUND_TEMPLATE_TEMPLATE_PARM
02771     && TYPE_BEING_DEFINED (ctx))
02772       || (is_friend && !DECL_TEMPLATE_INFO (decl)))
02773     {
02774       if (DECL_LANG_SPECIFIC (decl)
02775     && DECL_TEMPLATE_INFO (decl)
02776     && DECL_TI_TEMPLATE (decl))
02777   tmpl = DECL_TI_TEMPLATE (decl);
02778       /* If DECL is a TYPE_DECL for a class-template, then there won't
02779    be DECL_LANG_SPECIFIC.  The information equivalent to
02780    DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
02781       else if (DECL_IMPLICIT_TYPEDEF_P (decl) 
02782          && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
02783          && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
02784   {
02785     /* Since a template declaration already existed for this
02786        class-type, we must be redeclaring it here.  Make sure
02787        that the redeclaration is valid.  */
02788     redeclare_class_template (TREE_TYPE (decl),
02789             current_template_parms);
02790     /* We don't need to create a new TEMPLATE_DECL; just use the
02791        one we already had.  */
02792     tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
02793   }
02794       else
02795   {
02796     tmpl = build_template_decl (decl, current_template_parms);
02797     new_template_p = 1;
02798 
02799     if (DECL_LANG_SPECIFIC (decl)
02800         && DECL_TEMPLATE_SPECIALIZATION (decl))
02801       {
02802         /* A specialization of a member template of a template
02803      class.  */
02804         SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
02805         DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
02806         DECL_TEMPLATE_INFO (decl) = NULL_TREE;
02807       }
02808   }
02809     }
02810   else
02811     {
02812       tree a, t, current, parms;
02813       int i;
02814 
02815       if (TREE_CODE (decl) == TYPE_DECL)
02816   {
02817     if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
02818          || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
02819         && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
02820         && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
02821       tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
02822     else
02823       {
02824         error ("`%D' does not declare a template type", decl);
02825         return decl;
02826       }
02827   }
02828       else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
02829   {
02830     error ("template definition of non-template `%#D'", decl);
02831     return decl;
02832   }
02833       else
02834   tmpl = DECL_TI_TEMPLATE (decl);
02835       
02836       if (is_member_template (tmpl)
02837     && DECL_FUNCTION_TEMPLATE_P (tmpl)
02838     && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 
02839     && DECL_TEMPLATE_SPECIALIZATION (decl))
02840   {
02841     tree new_tmpl;
02842 
02843     /* The declaration is a specialization of a member
02844        template, declared outside the class.  Therefore, the
02845        innermost template arguments will be NULL, so we
02846        replace them with the arguments determined by the
02847        earlier call to check_explicit_specialization.  */
02848     args = DECL_TI_ARGS (decl);
02849 
02850     new_tmpl 
02851       = build_template_decl (decl, current_template_parms);
02852     DECL_TEMPLATE_RESULT (new_tmpl) = decl;
02853     TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
02854     DECL_TI_TEMPLATE (decl) = new_tmpl;
02855     SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
02856     DECL_TEMPLATE_INFO (new_tmpl) 
02857       = tree_cons (tmpl, args, NULL_TREE);
02858 
02859     register_specialization (new_tmpl, 
02860            most_general_template (tmpl), 
02861            args);
02862     return decl;
02863   }
02864 
02865       /* Make sure the template headers we got make sense.  */
02866 
02867       parms = DECL_TEMPLATE_PARMS (tmpl);
02868       i = TMPL_PARMS_DEPTH (parms);
02869       if (TMPL_ARGS_DEPTH (args) != i)
02870   {
02871     error ("expected %d levels of template parms for `%#D', got %d",
02872         i, decl, TMPL_ARGS_DEPTH (args));
02873   }
02874       else
02875   for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
02876     {
02877       a = TMPL_ARGS_LEVEL (args, i);
02878       t = INNERMOST_TEMPLATE_PARMS (parms);
02879 
02880       if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
02881         {
02882     if (current == decl)
02883       error ("got %d template parameters for `%#D'",
02884           TREE_VEC_LENGTH (a), decl);
02885     else
02886       error ("got %d template parameters for `%#T'",
02887           TREE_VEC_LENGTH (a), current);
02888     error ("  but %d required", TREE_VEC_LENGTH (t));
02889         }
02890 
02891       /* Perhaps we should also check that the parms are used in the
02892                appropriate qualifying scopes in the declarator?  */
02893 
02894       if (current == decl)
02895         current = ctx;
02896       else
02897         current = TYPE_CONTEXT (current);
02898     }
02899     }
02900 
02901   DECL_TEMPLATE_RESULT (tmpl) = decl;
02902   TREE_TYPE (tmpl) = TREE_TYPE (decl);
02903 
02904   /* Push template declarations for global functions and types.  Note
02905      that we do not try to push a global template friend declared in a
02906      template class; such a thing may well depend on the template
02907      parameters of the class.  */
02908   if (new_template_p && !ctx 
02909       && !(is_friend && template_class_depth (current_class_type) > 0))
02910     tmpl = pushdecl_namespace_level (tmpl);
02911 
02912   if (primary)
02913     {
02914       DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
02915       if (DECL_CONV_FN_P (tmpl))
02916   {
02917     int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
02918 
02919     /* It is a conversion operator. See if the type converted to
02920        depends on innermost template operands.  */
02921     
02922     if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)),
02923               template_parm_this_level_p,
02924               &depth,
02925               NULL))
02926       DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
02927   }
02928     }
02929 
02930   /* OSP_230, GCC_13635 */
02931   /* The DECL_TI_ARGS of DECL contains full set of arguments refering
02932      back to its most general template.  If TMPL is a specialization,
02933      ARGS may only have the innermost set of arguments.  Add the missing
02934      argument levels if necessary.  */
02935   if (DECL_TEMPLATE_INFO (tmpl))
02936     args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
02937   
02938   info = tree_cons (tmpl, args, NULL_TREE);
02939 
02940   if (DECL_IMPLICIT_TYPEDEF_P (decl))
02941     {
02942       SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
02943       if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
02944     && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
02945     /* Don't change the name if we've already set it up.  */
02946     && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
02947   DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
02948     }
02949   else if (DECL_LANG_SPECIFIC (decl))
02950     DECL_TEMPLATE_INFO (decl) = info;
02951 
02952   return DECL_TEMPLATE_RESULT (tmpl);
02953 }
02954 
02955 tree
02956 push_template_decl (decl)
02957      tree decl;
02958 {
02959   return push_template_decl_real (decl, 0);
02960 }
02961 
02962 /* Called when a class template TYPE is redeclared with the indicated
02963    template PARMS, e.g.:
02964 
02965      template <class T> struct S;
02966      template <class T> struct S {};  */
02967 
02968 void 
02969 redeclare_class_template (type, parms)
02970      tree type;
02971      tree parms;
02972 {
02973   tree tmpl;
02974   tree tmpl_parms;
02975   int i;
02976 
02977   if (!TYPE_TEMPLATE_INFO (type))
02978     {
02979       error ("`%T' is not a template type", type);
02980       return;
02981     }
02982 
02983   tmpl = TYPE_TI_TEMPLATE (type);
02984   if (!PRIMARY_TEMPLATE_P (tmpl))
02985     /* The type is nested in some template class.  Nothing to worry
02986        about here; there are no new template parameters for the nested
02987        type.  */
02988     return;
02989 
02990   parms = INNERMOST_TEMPLATE_PARMS (parms);
02991   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
02992 
02993   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
02994     {
02995       cp_error_at ("previous declaration `%D'", tmpl);
02996       error ("used %d template parameter%s instead of %d",
02997     TREE_VEC_LENGTH (tmpl_parms), 
02998     TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
02999     TREE_VEC_LENGTH (parms));
03000       return;
03001     }
03002 
03003   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
03004     {
03005       tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
03006       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
03007       tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
03008       tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
03009 
03010       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
03011   {
03012     cp_error_at ("template parameter `%#D'", tmpl_parm);
03013     error ("redeclared here as `%#D'", parm);
03014     return;
03015   }
03016 
03017       if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
03018   {
03019     /* We have in [temp.param]:
03020 
03021        A template-parameter may not be given default arguments
03022        by two different declarations in the same scope.  */
03023     error ("redefinition of default argument for `%#D'", parm);
03024     cp_error_at ("  original definition appeared here", tmpl_parm);
03025     return;
03026   }
03027 
03028       if (parm_default != NULL_TREE)
03029   /* Update the previous template parameters (which are the ones
03030      that will really count) with the new default value.  */
03031   TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
03032       else if (tmpl_default != NULL_TREE)
03033   /* Update the new parameters, too; they'll be used as the
03034      parameters for any members.  */
03035   TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
03036     }
03037 }
03038 
03039 /* Attempt to convert the non-type template parameter EXPR to the
03040    indicated TYPE.  If the conversion is successful, return the
03041    converted value.  If the conversion is unsuccessful, return
03042    NULL_TREE if we issued an error message, or error_mark_node if we
03043    did not.  We issue error messages for out-and-out bad template
03044    parameters, but not simply because the conversion failed, since we
03045    might be just trying to do argument deduction.  By the time this
03046    function is called, neither TYPE nor EXPR may make use of template
03047    parameters.  */
03048 
03049 static tree
03050 convert_nontype_argument (type, expr)
03051      tree type;
03052      tree expr;
03053 {
03054   tree expr_type = TREE_TYPE (expr);
03055 
03056   /* A template-argument for a non-type, non-template
03057      template-parameter shall be one of:
03058 
03059      --an integral constant-expression of integral or enumeration
03060      type; or
03061      
03062      --the name of a non-type template-parameter; or
03063      
03064      --the name of an object or function with external linkage,
03065      including function templates and function template-ids but
03066      excluding non-static class members, expressed as id-expression;
03067      or
03068      
03069      --the address of an object or function with external linkage,
03070      including function templates and function template-ids but
03071      excluding non-static class members, expressed as & id-expression
03072      where the & is optional if the name refers to a function or
03073      array; or
03074      
03075      --a pointer to member expressed as described in _expr.unary.op_.  */
03076 
03077   /* An integral constant-expression can include const variables or
03078      enumerators.  Simplify things by folding them to their values,
03079      unless we're about to bind the declaration to a reference
03080      parameter.  */
03081   if (INTEGRAL_TYPE_P (expr_type)
03082       && TREE_CODE (type) != REFERENCE_TYPE)
03083     expr = decl_constant_value (expr);
03084 
03085   if (is_overloaded_fn (expr))
03086     /* OK for now.  We'll check that it has external linkage later.
03087        Check this first since if expr_type is the unknown_type_node
03088        we would otherwise complain below.  */
03089     ;
03090   else if (TYPE_PTRMEM_P (expr_type)
03091      || TYPE_PTRMEMFUNC_P (expr_type))
03092     {
03093       if (TREE_CODE (expr) != PTRMEM_CST)
03094   goto bad_argument;
03095     }
03096   else if (TYPE_PTR_P (expr_type)
03097      || TYPE_PTRMEM_P (expr_type)
03098      || TREE_CODE (expr_type) == ARRAY_TYPE
03099      || TREE_CODE (type) == REFERENCE_TYPE
03100      /* If expr is the address of an overloaded function, we
03101         will get the unknown_type_node at this point.  */
03102      || expr_type == unknown_type_node)
03103     {
03104       tree referent;
03105       tree e = expr;
03106       STRIP_NOPS (e);
03107 
03108       if (TREE_CODE (expr_type) == ARRAY_TYPE
03109     || (TREE_CODE (type) == REFERENCE_TYPE
03110         && TREE_CODE (e) != ADDR_EXPR))
03111   referent = e;
03112       else
03113   {
03114     if (TREE_CODE (e) != ADDR_EXPR)
03115       {
03116       bad_argument:
03117         error ("`%E' is not a valid template argument", expr);
03118         if (TYPE_PTR_P (expr_type))
03119     {
03120       if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
03121         error ("it must be the address of a function with external linkage");
03122       else
03123         error ("it must be the address of an object with external linkage");
03124     }
03125         else if (TYPE_PTRMEM_P (expr_type)
03126            || TYPE_PTRMEMFUNC_P (expr_type))
03127     error ("it must be a pointer-to-member of the form `&X::Y'");
03128 
03129         return NULL_TREE;
03130       }
03131 
03132     referent = TREE_OPERAND (e, 0);
03133     STRIP_NOPS (referent);
03134   }
03135 
03136       if (TREE_CODE (referent) == STRING_CST)
03137   {
03138     error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", 
03139         referent);
03140     return NULL_TREE;
03141   }
03142 
03143       if (is_overloaded_fn (referent))
03144   /* We'll check that it has external linkage later.  */
03145   ;
03146       else if (TREE_CODE (referent) != VAR_DECL)
03147   goto bad_argument;
03148       else if (!DECL_EXTERNAL_LINKAGE_P (referent))
03149   {
03150     error ("address of non-extern `%E' cannot be used as template argument", referent); 
03151     return error_mark_node;
03152   }
03153     }
03154   else if (INTEGRAL_TYPE_P (expr_type) 
03155      || TYPE_PTRMEM_P (expr_type) 
03156      || TYPE_PTRMEMFUNC_P (expr_type))
03157     {
03158       if (! TREE_CONSTANT (expr))
03159   {
03160   non_constant:
03161     error ("non-constant `%E' cannot be used as template argument",
03162         expr);
03163     return NULL_TREE;
03164   }
03165     }
03166   else 
03167     {
03168       error ("object `%E' cannot be used as template argument", expr);
03169       return NULL_TREE;
03170     }
03171 
03172   switch (TREE_CODE (type))
03173     {
03174     case INTEGER_TYPE:
03175     case BOOLEAN_TYPE:
03176     case ENUMERAL_TYPE:
03177       /* For a non-type template-parameter of integral or enumeration
03178          type, integral promotions (_conv.prom_) and integral
03179          conversions (_conv.integral_) are applied.  */
03180       if (!INTEGRAL_TYPE_P (expr_type))
03181   return error_mark_node;
03182       
03183       /* It's safe to call digest_init in this case; we know we're
03184    just converting one integral constant expression to another.  */
03185       expr = digest_init (type, expr, (tree*) 0);
03186 
03187       if (TREE_CODE (expr) != INTEGER_CST)
03188   /* Curiously, some TREE_CONSTANT integral expressions do not
03189      simplify to integer constants.  For example, `3 % 0',
03190      remains a TRUNC_MOD_EXPR.  */
03191   goto non_constant;
03192       
03193       return expr;
03194   
03195     case POINTER_TYPE:
03196       {
03197   tree type_pointed_to = TREE_TYPE (type);
03198  
03199   if (TYPE_PTRMEM_P (type))
03200     {
03201       tree e;
03202 
03203       /* For a non-type template-parameter of type pointer to data
03204          member, qualification conversions (_conv.qual_) are
03205          applied.  */
03206       e = perform_qualification_conversions (type, expr);
03207       if (TREE_CODE (e) == NOP_EXPR)
03208         /* The call to perform_qualification_conversions will
03209      insert a NOP_EXPR over EXPR to do express conversion,
03210      if necessary.  But, that will confuse us if we use
03211      this (converted) template parameter to instantiate
03212      another template; then the thing will not look like a
03213      valid template argument.  So, just make a new
03214      constant, of the appropriate type.  */
03215         e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
03216       return e;
03217     }
03218   else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
03219     { 
03220       /* For a non-type template-parameter of type pointer to
03221          function, only the function-to-pointer conversion
03222          (_conv.func_) is applied.  If the template-argument
03223          represents a set of overloaded functions (or a pointer to
03224          such), the matching function is selected from the set
03225          (_over.over_).  */
03226       tree fns;
03227       tree fn;
03228 
03229       if (TREE_CODE (expr) == ADDR_EXPR)
03230         fns = TREE_OPERAND (expr, 0);
03231       else
03232         fns = expr;
03233 
03234       fn = instantiate_type (type_pointed_to, fns, tf_none);
03235 
03236       if (fn == error_mark_node)
03237         return error_mark_node;
03238 
03239       if (!DECL_EXTERNAL_LINKAGE_P (fn))
03240         {
03241     if (really_overloaded_fn (fns))
03242       return error_mark_node;
03243     else
03244       goto bad_argument;
03245         }
03246 
03247       expr = build_unary_op (ADDR_EXPR, fn, 0);
03248 
03249       my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 
03250         0);
03251       return expr;
03252     }
03253   else 
03254     {
03255       /* For a non-type template-parameter of type pointer to
03256          object, qualification conversions (_conv.qual_) and the
03257          array-to-pointer conversion (_conv.array_) are applied.
03258          [Note: In particular, neither the null pointer conversion
03259          (_conv.ptr_) nor the derived-to-base conversion
03260          (_conv.ptr_) are applied.  Although 0 is a valid
03261          template-argument for a non-type template-parameter of
03262          integral type, it is not a valid template-argument for a
03263          non-type template-parameter of pointer type.]  
03264       
03265          The call to decay_conversion performs the
03266          array-to-pointer conversion, if appropriate.  */
03267       expr = decay_conversion (expr);
03268 
03269       if (expr == error_mark_node)
03270         return error_mark_node;
03271       else
03272         return perform_qualification_conversions (type, expr);
03273     }
03274       }
03275       break;
03276 
03277     case REFERENCE_TYPE:
03278       {
03279   tree type_referred_to = TREE_TYPE (type);
03280 
03281   /* If this expression already has reference type, get the
03282      underling object.  */
03283   if (TREE_CODE (expr_type) == REFERENCE_TYPE) 
03284     {
03285       my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
03286       expr = TREE_OPERAND (expr, 0);
03287       expr_type = TREE_TYPE (expr);
03288     }
03289 
03290   if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
03291     {
03292       /* For a non-type template-parameter of type reference to
03293          function, no conversions apply.  If the
03294          template-argument represents a set of overloaded
03295          functions, the matching function is selected from the
03296          set (_over.over_).  */
03297       tree fn;
03298 
03299       fn = instantiate_type (type_referred_to, expr, tf_none);
03300 
03301       if (fn == error_mark_node)
03302         return error_mark_node;
03303 
03304       if (!DECL_EXTERNAL_LINKAGE_P (fn))
03305         {
03306     if (really_overloaded_fn (expr))
03307       /* Don't issue an error here; we might get a different
03308          function if the overloading had worked out
03309          differently.  */
03310       return error_mark_node;
03311     else
03312       goto bad_argument;
03313         }
03314 
03315       my_friendly_assert (same_type_p (type_referred_to, 
03316                TREE_TYPE (fn)),
03317         0);
03318 
03319       expr = fn;
03320     }
03321   else
03322     {
03323       /* For a non-type template-parameter of type reference to
03324          object, no conversions apply.  The type referred to by the
03325          reference may be more cv-qualified than the (otherwise
03326          identical) type of the template-argument.  The
03327          template-parameter is bound directly to the
03328          template-argument, which must be an lvalue.  */
03329       if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
03330             TYPE_MAIN_VARIANT (type_referred_to))
03331     || !at_least_as_qualified_p (type_referred_to,
03332                expr_type)
03333     || !real_lvalue_p (expr))
03334         return error_mark_node;
03335     }
03336 
03337   cxx_mark_addressable (expr);
03338   return build1 (ADDR_EXPR, type, expr);
03339       }
03340       break;
03341 
03342     case RECORD_TYPE:
03343       {
03344   my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
03345 
03346   /* For a non-type template-parameter of type pointer to member
03347      function, no conversions apply.  If the template-argument
03348      represents a set of overloaded member functions, the
03349      matching member function is selected from the set
03350      (_over.over_).  */
03351 
03352   if (!TYPE_PTRMEMFUNC_P (expr_type) && 
03353       expr_type != unknown_type_node)
03354     return error_mark_node;
03355 
03356   if (TREE_CODE (expr) == PTRMEM_CST)
03357     {
03358       /* A ptr-to-member constant.  */
03359       if (!same_type_p (type, expr_type))
03360         return error_mark_node;
03361       else 
03362         return expr;
03363     }
03364 
03365   if (TREE_CODE (expr) != ADDR_EXPR)
03366     return error_mark_node;
03367 
03368   expr = instantiate_type (type, expr, tf_none);
03369   
03370   if (expr == error_mark_node)
03371     return error_mark_node;
03372 
03373   my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
03374           0);
03375   return expr;
03376       }
03377       break;
03378 
03379     default:
03380       /* All non-type parameters must have one of these types.  */
03381       abort ();
03382       break;
03383     }
03384 
03385   return error_mark_node;
03386 }
03387 
03388 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for 
03389    template template parameters.  Both PARM_PARMS and ARG_PARMS are 
03390    vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL 
03391    or PARM_DECL.
03392    
03393    ARG_PARMS may contain more parameters than PARM_PARMS.  If this is 
03394    the case, then extra parameters must have default arguments.
03395 
03396    Consider the example:
03397      template <class T, class Allocator = allocator> class vector;
03398      template<template <class U> class TT> class C;
03399 
03400    C<vector> is a valid instantiation.  PARM_PARMS for the above code 
03401    contains a TYPE_DECL (for U),  ARG_PARMS contains two TYPE_DECLs (for 
03402    T and Allocator) and OUTER_ARGS contains the argument that is used to 
03403    substitute the TT parameter.  */
03404 
03405 static int
03406 coerce_template_template_parms (parm_parms, arg_parms, complain, 
03407         in_decl, outer_args)
03408      tree parm_parms, arg_parms;
03409      tsubst_flags_t complain;
03410      tree in_decl, outer_args;
03411 {
03412   int nparms, nargs, i;
03413   tree parm, arg;
03414 
03415   my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
03416   my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
03417 
03418   nparms = TREE_VEC_LENGTH (parm_parms);
03419   nargs = TREE_VEC_LENGTH (arg_parms);
03420 
03421   /* The rule here is opposite of coerce_template_parms.  */
03422   if (nargs < nparms
03423       || (nargs > nparms
03424     && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
03425     return 0;
03426 
03427   for (i = 0; i < nparms; ++i)
03428     {
03429       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
03430       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
03431 
03432       if (arg == NULL_TREE || arg == error_mark_node
03433           || parm == NULL_TREE || parm == error_mark_node)
03434   return 0;
03435 
03436       if (TREE_CODE (arg) != TREE_CODE (parm))
03437         return 0;
03438 
03439       switch (TREE_CODE (parm))
03440   {
03441   case TYPE_DECL:
03442     break;
03443 
03444   case TEMPLATE_DECL:
03445     /* We encounter instantiations of templates like
03446          template <template <template <class> class> class TT>
03447          class C;  */
03448     {
03449       tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
03450       tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
03451 
03452       if (!coerce_template_template_parms (parmparm, argparm, 
03453                    complain, in_decl,
03454              outer_args))
03455         return 0;
03456     }
03457     break;
03458 
03459   case PARM_DECL:
03460     /* The tsubst call is used to handle cases such as
03461          template <class T, template <T> class TT> class D;  
03462        i.e. the parameter list of TT depends on earlier parameters.  */
03463     if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, 
03464             complain, in_decl),
03465           TREE_TYPE (arg)))
03466       return 0;
03467     break;
03468     
03469   default:
03470     abort ();
03471   }
03472     }
03473   return 1;
03474 }
03475 
03476 /* Convert the indicated template ARG as necessary to match the
03477    indicated template PARM.  Returns the converted ARG, or
03478    error_mark_node if the conversion was unsuccessful.  Error and
03479    warning messages are issued under control of COMPLAIN.  This
03480    conversion is for the Ith parameter in the parameter list.  ARGS is
03481    the full set of template arguments deduced so far.  */
03482 
03483 static tree
03484 convert_template_argument (parm, arg, args, complain, i, in_decl)
03485      tree parm;
03486      tree arg;
03487      tree args;
03488      tsubst_flags_t complain;
03489      int i;
03490      tree in_decl;
03491 {
03492   tree val;
03493   tree inner_args;
03494   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
03495   
03496   inner_args = INNERMOST_TEMPLATE_ARGS (args);
03497 
03498   if (TREE_CODE (arg) == TREE_LIST 
03499       && TREE_TYPE (arg) != NULL_TREE
03500       && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
03501     {  
03502       /* The template argument was the name of some
03503    member function.  That's usually
03504    invalid, but static members are OK.  In any
03505    case, grab the underlying fields/functions
03506    and issue an error later if required.  */
03507       arg = TREE_VALUE (arg);
03508       TREE_TYPE (arg) = unknown_type_node;
03509     }
03510 
03511   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
03512   requires_type = (TREE_CODE (parm) == TYPE_DECL
03513        || requires_tmpl_type);
03514 
03515   if (TREE_CODE (arg) != RECORD_TYPE)
03516     is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
03517          && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
03518         || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
03519         || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
03520   else if (CLASSTYPE_IS_TEMPLATE (arg)
03521      && is_base_of_enclosing_class (arg, current_class_type))
03522     /* This is a template name used within the scope of the
03523        template. It could be the template, or it could be the
03524        instantiation. Choose whichever makes sense.  */
03525     is_tmpl_type = requires_tmpl_type;
03526   else
03527     /* It is a non-template class, or a specialization of a template
03528        class, or a non-template member of a template class.  */
03529     is_tmpl_type = 0;
03530   
03531   if (is_tmpl_type
03532       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
03533     || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
03534     arg = TYPE_STUB_DECL (arg);
03535   else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
03536     arg = CLASSTYPE_TI_TEMPLATE (arg);
03537 
03538   is_type = TYPE_P (arg) || is_tmpl_type;
03539 
03540   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
03541       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
03542     {
03543       pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
03544       
03545       arg = make_typename_type (TREE_OPERAND (arg, 0),
03546         TREE_OPERAND (arg, 1),
03547         complain & tf_error);
03548       is_type = 1;
03549     }
03550   if (is_type != requires_type)
03551     {
03552       if (in_decl)
03553   {
03554     if (complain & tf_error)
03555       {
03556         error ("type/value mismatch at argument %d in template parameter list for `%D'",
03557       i + 1, in_decl);
03558         if (is_type)
03559     error ("  expected a constant of type `%T', got `%T'",
03560         TREE_TYPE (parm),
03561         (is_tmpl_type ? DECL_NAME (arg) : arg));
03562         else
03563     error ("  expected a type, got `%E'", arg);
03564       }
03565   }
03566       return error_mark_node;
03567     }
03568   if (is_tmpl_type ^ requires_tmpl_type)
03569     {
03570       if (in_decl && (complain & tf_error))
03571   {
03572     error ("type/value mismatch at argument %d in template parameter list for `%D'",
03573         i + 1, in_decl);
03574     if (is_tmpl_type)
03575       error ("  expected a type, got `%T'", DECL_NAME (arg));
03576     else
03577       error ("  expected a class template, got `%T'", arg);
03578   }
03579       return error_mark_node;
03580     }
03581       
03582   if (is_type)
03583     {
03584       if (requires_tmpl_type)
03585   {
03586     if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
03587       /* The number of argument required is not known yet.
03588          Just accept it for now.  */
03589       val = TREE_TYPE (arg);
03590     else
03591       {
03592         tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
03593         tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
03594 
03595         if (coerce_template_template_parms (parmparm, argparm,
03596               complain, in_decl,
03597               inner_args))
03598     {
03599       val = arg;
03600       
03601       /* TEMPLATE_TEMPLATE_PARM node is preferred over 
03602          TEMPLATE_DECL.  */
03603       if (val != error_mark_node 
03604           && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
03605         val = TREE_TYPE (val);
03606     }
03607         else
03608     {
03609       if (in_decl && (complain & tf_error))
03610         {
03611           error ("type/value mismatch at argument %d in template parameter list for `%D'",
03612         i + 1, in_decl);
03613           error ("  expected a template of type `%D', got `%D'", parm, arg);
03614         }
03615       
03616       val = error_mark_node;
03617     }
03618       }
03619   }
03620       else
03621   {
03622     val = groktypename (arg);
03623     if (! processing_template_decl)
03624       {
03625         /* [basic.link]: A name with no linkage (notably, the
03626      name of a class or enumeration declared in a local
03627      scope) shall not be used to declare an entity with
03628      linkage.  This implies that names with no linkage
03629      cannot be used as template arguments.  */
03630         tree t = no_linkage_check (val);
03631         if (t)
03632     {
03633       if (TYPE_ANONYMOUS_P (t))
03634         pedwarn
03635           ("template-argument `%T' uses anonymous type", val);
03636       else
03637         error
03638           ("template-argument `%T' uses local type `%T'",
03639            val, t);
03640       return error_mark_node;
03641     }
03642 
03643         /* In order to avoid all sorts of complications, we do
03644      not allow variably-modified types as template
03645      arguments.  */
03646         if (variably_modified_type_p (val))
03647     {
03648       error ("template-argument `%T' is a variably modified type",
03649        val);
03650       return error_mark_node;
03651     }
03652       }
03653   }
03654     }
03655   else
03656     {
03657       tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
03658 
03659       if (invalid_nontype_parm_type_p (t, complain))
03660         return error_mark_node;
03661       
03662       if (processing_template_decl)
03663   arg = maybe_fold_nontype_arg (arg);
03664 
03665       if (!uses_template_parms (arg) && !uses_template_parms (t))
03666   /* We used to call digest_init here.  However, digest_init
03667      will report errors, which we don't want when complain
03668      is zero.  More importantly, digest_init will try too
03669      hard to convert things: for example, `0' should not be
03670      converted to pointer type at this point according to
03671      the standard.  Accepting this is not merely an
03672      extension, since deciding whether or not these
03673      conversions can occur is part of determining which
03674      function template to call, or whether a given explicit
03675      argument specification is valid.  */
03676   val = convert_nontype_argument (t, arg);
03677       else
03678   val = arg;
03679 
03680       if (val == NULL_TREE)
03681   val = error_mark_node;
03682       else if (val == error_mark_node && (complain & tf_error))
03683   error ("could not convert template argument `%E' to `%T'", 
03684       arg, t);
03685     }
03686 
03687   return val;
03688 }
03689 
03690 /* Convert all template arguments to their appropriate types, and
03691    return a vector containing the innermost resulting template
03692    arguments.  If any error occurs, return error_mark_node. Error and
03693    warning messages are issued under control of COMPLAIN.  Some error
03694    messages are issued even if COMPLAIN is zero; for instance, if a
03695    template argument is composed from a local class.
03696 
03697    If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
03698    provided in ARGLIST, or else trailing parameters must have default
03699    values.  If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
03700    deduction for any unspecified trailing arguments.  */
03701    
03702 static tree
03703 coerce_template_parms (parms, args, in_decl,
03704            complain,
03705            require_all_arguments)
03706      tree parms, args;
03707      tree in_decl;
03708      tsubst_flags_t complain;
03709      int require_all_arguments;
03710 {
03711   int nparms, nargs, i, lost = 0;
03712   tree inner_args;
03713   tree new_args;
03714   tree new_inner_args;
03715 
03716   inner_args = INNERMOST_TEMPLATE_ARGS (args);
03717   nargs = NUM_TMPL_ARGS (inner_args);
03718   nparms = TREE_VEC_LENGTH (parms);
03719 
03720   if (nargs > nparms
03721       || (nargs < nparms
03722     && require_all_arguments
03723     && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
03724     {
03725       if (complain & tf_error) 
03726   {
03727     error ("wrong number of template arguments (%d, should be %d)",
03728         nargs, nparms);
03729     
03730     if (in_decl)
03731       cp_error_at ("provided for `%D'", in_decl);
03732   }
03733 
03734       return error_mark_node;
03735     }
03736 
03737   new_inner_args = make_tree_vec (nparms);
03738   new_args = add_outermost_template_args (args, new_inner_args);
03739   for (i = 0; i < nparms; i++)
03740     {
03741       tree arg;
03742       tree parm;
03743 
03744       /* Get the Ith template parameter.  */
03745       parm = TREE_VEC_ELT (parms, i);
03746 
03747       /* Calculate the Ith argument.  */
03748       if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
03749   {
03750     arg = TREE_VALUE (inner_args);
03751     inner_args = TREE_CHAIN (inner_args);
03752   }
03753       else if (i < nargs)
03754   arg = TREE_VEC_ELT (inner_args, i);
03755       /* If no template argument was supplied, look for a default
03756    value.  */
03757       else if (TREE_PURPOSE (parm) == NULL_TREE)
03758   {
03759     /* There was no default value.  */
03760     my_friendly_assert (!require_all_arguments, 0);
03761     break;
03762   }
03763       else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
03764   arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
03765       else
03766   arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
03767          in_decl);
03768 
03769       /* Now, convert the Ith argument, as necessary.  */
03770       if (arg == NULL_TREE)
03771   /* We're out of arguments.  */
03772   {
03773     my_friendly_assert (!require_all_arguments, 0);
03774     break;
03775   }
03776       else if (arg == error_mark_node)
03777   {
03778     error ("template argument %d is invalid", i + 1);
03779     arg = error_mark_node;
03780   }
03781       else 
03782   arg = convert_template_argument (TREE_VALUE (parm), 
03783            arg, new_args, complain, i,
03784            in_decl); 
03785       
03786       if (arg == error_mark_node)
03787   lost++;
03788       TREE_VEC_ELT (new_inner_args, i) = arg;
03789     }
03790 
03791   if (lost)
03792     return error_mark_node;
03793 
03794   return new_inner_args;
03795 }
03796 
03797 /* Returns 1 if template args OT and NT are equivalent.  */
03798 
03799 static int
03800 template_args_equal (ot, nt)
03801      tree ot, nt;
03802 {
03803   if (nt == ot)
03804     return 1;
03805 
03806   if (TREE_CODE (nt) == TREE_VEC)
03807     /* For member templates */
03808     return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
03809   else if (TYPE_P (nt))
03810     return TYPE_P (ot) && same_type_p (ot, nt);
03811   else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
03812     return 0;
03813   else
03814     return (cp_tree_equal (ot, nt) > 0);
03815 }
03816 
03817 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
03818    of template arguments.  Returns 0 otherwise.  */
03819 
03820 int
03821 comp_template_args (oldargs, newargs)
03822      tree oldargs, newargs;
03823 {
03824   int i;
03825 
03826   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
03827     return 0;
03828 
03829   for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
03830     {
03831       tree nt = TREE_VEC_ELT (newargs, i);
03832       tree ot = TREE_VEC_ELT (oldargs, i);
03833 
03834       if (! template_args_equal (ot, nt))
03835   return 0;
03836     }
03837   return 1;
03838 }
03839 
03840 /* Given class template name and parameter list, produce a user-friendly name
03841    for the instantiation.  */
03842 
03843 static char *
03844 mangle_class_name_for_template (name, parms, arglist)
03845      const char *name;
03846      tree parms, arglist;
03847 {
03848   static struct obstack scratch_obstack;
03849   static char *scratch_firstobj;
03850   int i, nparms;
03851 
03852   if (!scratch_firstobj)
03853     gcc_obstack_init (&scratch_obstack);
03854   else
03855     obstack_free (&scratch_obstack, scratch_firstobj);
03856   scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
03857 
03858 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
03859 #define cat(S)  obstack_grow (&scratch_obstack, (S), strlen (S))
03860 
03861   cat (name);
03862   ccat ('<');
03863   nparms = TREE_VEC_LENGTH (parms);
03864   arglist = INNERMOST_TEMPLATE_ARGS (arglist);
03865   my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
03866   for (i = 0; i < nparms; i++)
03867     {
03868       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
03869       tree arg = TREE_VEC_ELT (arglist, i);
03870 
03871       if (i)
03872   ccat (',');
03873 
03874       if (TREE_CODE (parm) == TYPE_DECL)
03875   {
03876     cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
03877     continue;
03878   }
03879       else if (TREE_CODE (parm) == TEMPLATE_DECL)
03880   {
03881     if (TREE_CODE (arg) == TEMPLATE_DECL)
03882       {
03883         /* Already substituted with real template.  Just output 
03884      the template name here */
03885               tree context = DECL_CONTEXT (arg);
03886               if (context)
03887                 {
03888                   /* The template may be defined in a namespace, or
03889                      may be a member template.  */
03890                   my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
03891                                       || CLASS_TYPE_P (context), 
03892                                       980422);
03893       cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
03894       cat("::");
03895     }
03896         cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
03897       }
03898     else
03899       /* Output the parameter declaration */
03900       cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
03901     continue;
03902   }
03903       else
03904   my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
03905 
03906       if (TREE_CODE (arg) == TREE_LIST)
03907   {
03908     /* New list cell was built because old chain link was in
03909        use.  */
03910     my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
03911     arg = TREE_VALUE (arg);
03912   }
03913       /* No need to check arglist against parmlist here; we did that
03914    in coerce_template_parms, called from lookup_template_class.  */
03915       cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
03916     }
03917   {
03918     char *bufp = obstack_next_free (&scratch_obstack);
03919     int offset = 0;
03920     while (bufp[offset - 1] == ' ')
03921       offset--;
03922     obstack_blank_fast (&scratch_obstack, offset);
03923 
03924     /* B<C<char> >, not B<C<char>> */
03925     if (bufp[offset - 1] == '>')
03926       ccat (' ');
03927   }
03928   ccat ('>');
03929   ccat ('\0');
03930   return (char *) obstack_base (&scratch_obstack);
03931 }
03932 
03933 static tree
03934 classtype_mangled_name (t)
03935      tree t;
03936 {
03937   if (CLASSTYPE_TEMPLATE_INFO (t)
03938       /* Specializations have already had their names set up in
03939    lookup_template_class.  */
03940       && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03941     {
03942       tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
03943 
03944       /* For non-primary templates, the template parameters are
03945    implicit from their surrounding context.  */
03946       if (PRIMARY_TEMPLATE_P (tmpl))
03947   {
03948     tree name = DECL_NAME (tmpl);
03949     char *mangled_name = mangle_class_name_for_template
03950       (IDENTIFIER_POINTER (name), 
03951        DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
03952        CLASSTYPE_TI_ARGS (t));
03953     tree id = get_identifier (mangled_name);
03954     IDENTIFIER_TEMPLATE (id) = name;
03955     return id;
03956   }
03957     }
03958 
03959   return TYPE_IDENTIFIER (t);
03960 }
03961 
03962 static void
03963 add_pending_template (d)
03964      tree d;
03965 {
03966   tree ti = (TYPE_P (d)
03967        ? CLASSTYPE_TEMPLATE_INFO (d)
03968        : DECL_TEMPLATE_INFO (d));
03969   tree pt;
03970   int level;
03971 
03972   if (TI_PENDING_TEMPLATE_FLAG (ti))
03973     return;
03974 
03975   /* We are called both from instantiate_decl, where we've already had a
03976      tinst_level pushed, and instantiate_template, where we haven't.
03977      Compensate.  */
03978   level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
03979 
03980   if (level)
03981     push_tinst_level (d);
03982 
03983   pt = tree_cons (current_tinst_level, d, NULL_TREE);
03984   if (last_pending_template)
03985     TREE_CHAIN (last_pending_template) = pt;
03986   else
03987     pending_templates = pt;
03988 
03989   last_pending_template = pt;
03990 
03991   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
03992 
03993   if (level)
03994     pop_tinst_level ();
03995 }
03996 
03997 
03998 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
03999    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
04000    documentation for TEMPLATE_ID_EXPR.  */
04001 
04002 tree
04003 lookup_template_function (fns, arglist)
04004      tree fns, arglist;
04005 {
04006   tree type;
04007 
04008   if (fns == error_mark_node || arglist == error_mark_node)
04009     return error_mark_node;
04010 
04011   if (fns == NULL_TREE
04012       || TREE_CODE (fns) == FUNCTION_DECL)
04013     {
04014       error ("non-template used as template");
04015       return error_mark_node;
04016     }
04017 
04018   my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL
04019           || TREE_CODE (fns) == OVERLOAD
04020           || BASELINK_P (fns)
04021           || TREE_CODE (fns) == IDENTIFIER_NODE
04022           || TREE_CODE (fns) == LOOKUP_EXPR,
04023           20020730);
04024 
04025   if (BASELINK_P (fns))
04026     {
04027       BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR,
04028           unknown_type_node,
04029           BASELINK_FUNCTIONS (fns),
04030           arglist);
04031       return fns;
04032     }
04033 
04034   type = TREE_TYPE (fns);
04035   if (TREE_CODE (fns) == OVERLOAD || !type)
04036     type = unknown_type_node;
04037   
04038   return build (TEMPLATE_ID_EXPR, type, fns, arglist);
04039 }
04040 
04041 /* Within the scope of a template class S<T>, the name S gets bound
04042    (in build_self_reference) to a TYPE_DECL for the class, not a
04043    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
04044    or one of its enclosing classes, and that type is a template,
04045    return the associated TEMPLATE_DECL.  Otherwise, the original
04046    DECL is returned.  */
04047 
04048 static tree
04049 maybe_get_template_decl_from_type_decl (decl)
04050      tree decl;
04051 {
04052   return (decl != NULL_TREE
04053     && TREE_CODE (decl) == TYPE_DECL 
04054     && DECL_ARTIFICIAL (decl)
04055     && CLASS_TYPE_P (TREE_TYPE (decl))
04056     && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 
04057     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
04058 }
04059 
04060 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
04061    parameters, find the desired type.
04062 
04063    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
04064    (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC.  It will
04065    be a TREE_LIST if called directly from the parser, and a TREE_VEC
04066    otherwise.)
04067 
04068    IN_DECL, if non-NULL, is the template declaration we are trying to
04069    instantiate.  
04070 
04071    If ENTERING_SCOPE is nonzero, we are about to enter the scope of
04072    the class we are looking up.
04073    
04074    Issue error and warning messages under control of COMPLAIN.
04075 
04076    If the template class is really a local class in a template
04077    function, then the FUNCTION_CONTEXT is the function in which it is
04078    being instantiated.  */
04079 
04080 tree
04081 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
04082      tree d1, arglist;
04083      tree in_decl;
04084      tree context;
04085      int entering_scope;
04086      tsubst_flags_t complain;
04087 {
04088   tree template = NULL_TREE, parmlist;
04089   tree t;
04090 
04091   timevar_push (TV_NAME_LOOKUP);
04092   if (TREE_CODE (d1) == IDENTIFIER_NODE)
04093     {
04094       if (IDENTIFIER_VALUE (d1) 
04095     && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
04096   template = IDENTIFIER_VALUE (d1);
04097       else
04098   {
04099     if (context)
04100       push_decl_namespace (context);
04101     template = lookup_name (d1, /*prefer_type=*/0);
04102     template = maybe_get_template_decl_from_type_decl (template);
04103     if (context)
04104       pop_decl_namespace ();
04105   }
04106       if (template)
04107   context = DECL_CONTEXT (template);
04108       if (template
04109     && TREE_CODE (template) == TYPE_DECL
04110     && IS_AGGR_TYPE (TREE_TYPE (template))
04111     && TREE_CODE (TREE_TYPE (template)) != TEMPLATE_TYPE_PARM)
04112   {
04113     d1 = template;
04114     goto type_decl;
04115   }
04116     }
04117   else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
04118     {
04119       tree type;
04120     type_decl:
04121       type = TREE_TYPE (d1);
04122 
04123       /* If we are declaring a constructor, say A<T>::A<T>, we will get
04124    an implicit typename for the second A.  Deal with it.  */
04125       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
04126   type = TREE_TYPE (type);
04127   
04128       if (CLASSTYPE_TEMPLATE_INFO (type))
04129   {
04130     template = CLASSTYPE_TI_TEMPLATE (type);
04131     d1 = DECL_NAME (template);
04132   }
04133     }
04134   else if (TREE_CODE (d1) == ENUMERAL_TYPE 
04135      || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
04136     {
04137       template = TYPE_TI_TEMPLATE (d1);
04138       d1 = DECL_NAME (template);
04139     }
04140   else if (TREE_CODE (d1) == TEMPLATE_DECL
04141      && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
04142     {
04143       template = d1;
04144       d1 = DECL_NAME (template);
04145       context = DECL_CONTEXT (template);
04146     }
04147 
04148   /* With something like `template <class T> class X class X { ... };'
04149      we could end up with D1 having nothing but an IDENTIFIER_VALUE.
04150      We don't want to do that, but we have to deal with the situation,
04151      so let's give them some syntax errors to chew on instead of a
04152      crash. Alternatively D1 might not be a template type at all.  */
04153   if (! template)
04154     {
04155       if (complain & tf_error)
04156         error ("`%T' is not a template", d1);
04157       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04158     }
04159 
04160   if (TREE_CODE (template) != TEMPLATE_DECL
04161          /* If we're called from the parser, make sure it's a user visible
04162             template.  */
04163       || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
04164           && !DECL_TEMPLATE_PARM_P (template)
04165           && !PRIMARY_TEMPLATE_P (template)))
04166     {
04167       if (complain & tf_error)
04168         {
04169           error ("non-template type `%T' used as a template", d1);
04170           if (in_decl)
04171       cp_error_at ("for template declaration `%D'", in_decl);
04172   }
04173       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04174     }
04175 
04176   if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
04177     {
04178       /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
04179          template arguments */
04180 
04181       tree parm;
04182       tree arglist2;
04183 
04184       parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
04185 
04186       /* Consider an example where a template template parameter declared as
04187 
04188      template <class T, class U = std::allocator<T> > class TT
04189 
04190    The template parameter level of T and U are one level larger than 
04191    of TT.  To proper process the default argument of U, say when an 
04192    instantiation `TT<int>' is seen, we need to build the full
04193    arguments containing {int} as the innermost level.  Outer levels,
04194    available when not appearing as default template argument, can be
04195    obtained from `current_template_args ()'.
04196 
04197    Suppose that TT is later substituted with std::vector.  The above
04198    instantiation is `TT<int, std::allocator<T> >' with TT at
04199    level 1, and T at level 2, while the template arguments at level 1
04200    becomes {std::vector} and the inner level 2 is {int}.  */
04201 
04202       if (current_template_parms)
04203   arglist = add_to_template_args (current_template_args (), arglist);
04204 
04205       arglist2 = coerce_template_parms (parmlist, arglist, template,
04206                                         complain, /*require_all_args=*/1);
04207       if (arglist2 == error_mark_node)
04208   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04209 
04210       parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
04211       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
04212     }
04213   else 
04214     {
04215       tree template_type = TREE_TYPE (template);
04216       tree gen_tmpl;
04217       tree type_decl;
04218       tree found = NULL_TREE;
04219       tree *tp;
04220       int arg_depth;
04221       int parm_depth;
04222       int is_partial_instantiation;
04223 
04224       gen_tmpl = most_general_template (template);
04225       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
04226       parm_depth = TMPL_PARMS_DEPTH (parmlist);
04227       arg_depth = TMPL_ARGS_DEPTH (arglist);
04228 
04229       if (arg_depth == 1 && parm_depth > 1)
04230   {
04231     /* We've been given an incomplete set of template arguments.
04232        For example, given:
04233 
04234          template <class T> struct S1 {
04235            template <class U> struct S2 {};
04236      template <class U> struct S2<U*> {};
04237           };
04238        
04239        we will be called with an ARGLIST of `U*', but the
04240        TEMPLATE will be `template <class T> template
04241        <class U> struct S1<T>::S2'.  We must fill in the missing
04242        arguments.  */
04243     arglist 
04244       = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
04245              arglist);
04246     arg_depth = TMPL_ARGS_DEPTH (arglist);
04247   }
04248 
04249       /* Now we should have enough arguments.  */
04250       my_friendly_assert (parm_depth == arg_depth, 0);
04251       
04252       /* From here on, we're only interested in the most general
04253    template.  */
04254       template = gen_tmpl;
04255 
04256       /* Calculate the BOUND_ARGS.  These will be the args that are
04257    actually tsubst'd into the definition to create the
04258    instantiation.  */
04259       if (parm_depth > 1)
04260   {
04261     /* We have multiple levels of arguments to coerce, at once.  */
04262     int i;
04263     int saved_depth = TMPL_ARGS_DEPTH (arglist);
04264 
04265     tree bound_args = make_tree_vec (parm_depth);
04266     
04267     for (i = saved_depth,
04268      t = DECL_TEMPLATE_PARMS (template); 
04269          i > 0 && t != NULL_TREE;
04270          --i, t = TREE_CHAIN (t))
04271       {
04272         tree a = coerce_template_parms (TREE_VALUE (t),
04273                 arglist, template,
04274                                         complain, /*require_all_args=*/1);
04275         SET_TMPL_ARGS_LEVEL (bound_args, i, a);
04276 
04277         /* We temporarily reduce the length of the ARGLIST so
04278      that coerce_template_parms will see only the arguments
04279      corresponding to the template parameters it is
04280      examining.  */
04281         TREE_VEC_LENGTH (arglist)--;
04282       }
04283 
04284     /* Restore the ARGLIST to its full size.  */
04285     TREE_VEC_LENGTH (arglist) = saved_depth;
04286 
04287     arglist = bound_args;
04288   }
04289       else
04290   arglist
04291     = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
04292            INNERMOST_TEMPLATE_ARGS (arglist),
04293            template,
04294                              complain, /*require_all_args=*/1);
04295 
04296       if (arglist == error_mark_node)
04297   /* We were unable to bind the arguments.  */
04298   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
04299 
04300       /* In the scope of a template class, explicit references to the
04301    template class refer to the type of the template, not any
04302    instantiation of it.  For example, in:
04303    
04304      template <class T> class C { void f(C<T>); }
04305 
04306    the `C<T>' is just the same as `C'.  Outside of the
04307    class, however, such a reference is an instantiation.  */
04308       if (comp_template_args (TYPE_TI_ARGS (template_type),
04309             arglist))
04310   {
04311     found = template_type;
04312     
04313     if (!entering_scope && PRIMARY_TEMPLATE_P (template))
04314       {
04315         tree ctx;
04316         
04317         /* Note that we use DECL_CONTEXT, rather than
04318      CP_DECL_CONTEXT, so that the termination test is
04319      always just `ctx'.  We're not interested in namespace
04320      scopes.  */
04321         for (ctx = current_class_type; 
04322        ctx; 
04323        ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
04324     if (same_type_p (ctx, template_type))
04325       break;
04326         
04327         if (!ctx)
04328     /* We're not in the scope of the class, so the
04329        TEMPLATE_TYPE is not the type we want after
04330        all.  */
04331     found = NULL_TREE;
04332       }
04333   }
04334       if (found)
04335         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
04336 
04337       for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
04338      *tp;
04339      tp = &TREE_CHAIN (*tp))
04340   if (comp_template_args (TREE_PURPOSE (*tp), arglist))
04341     {
04342       found = *tp;
04343 
04344       /* Use the move-to-front heuristic to speed up future
04345          searches.  */
04346       *tp = TREE_CHAIN (*tp);
04347       TREE_CHAIN (found) 
04348         = DECL_TEMPLATE_INSTANTIATIONS (template);
04349       DECL_TEMPLATE_INSTANTIATIONS (template) = found;
04350 
04351       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
04352     }
04353 
04354       /* This type is a "partial instantiation" if any of the template
04355    arguments still involve template parameters.  Note that we set
04356    IS_PARTIAL_INSTANTIATION for partial specializations as
04357    well.  */
04358       is_partial_instantiation = uses_template_parms (arglist);
04359 
04360       if (!is_partial_instantiation 
04361     && !PRIMARY_TEMPLATE_P (template)
04362     && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
04363   {
04364     found = xref_tag_from_type (TREE_TYPE (template),
04365               DECL_NAME (template),
04366               /*globalize=*/1);
04367     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
04368   }
04369       
04370       context = tsubst (DECL_CONTEXT (template), arglist,
04371       complain, in_decl);
04372       if (!context)
04373   context = global_namespace;
04374 
04375       /* Create the type.  */
04376       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
04377   {
04378     if (!is_partial_instantiation)
04379       {
04380         set_current_access_from_decl (TYPE_NAME (template_type));
04381         t = start_enum (TYPE_IDENTIFIER (template_type));
04382       }
04383     else
04384       /* We don't want to call start_enum for this type, since
04385          the values for the enumeration constants may involve
04386          template parameters.  And, no one should be interested
04387          in the enumeration constants for such a type.  */
04388       t = make_node (ENUMERAL_TYPE);
04389   }
04390       else
04391   {
04392     t = make_aggr_type (TREE_CODE (template_type));
04393     CLASSTYPE_DECLARED_CLASS (t) 
04394       = CLASSTYPE_DECLARED_CLASS (template_type);
04395     CLASSTYPE_GOT_SEMICOLON (t) = 1;
04396     SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
04397     TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
04398 
04399     /* A local class.  Make sure the decl gets registered properly.  */
04400     if (context == current_function_decl)
04401       pushtag (DECL_NAME (template), t, 0);
04402   }
04403 
04404       /* If we called start_enum or pushtag above, this information
04405    will already be set up.  */
04406       if (!TYPE_NAME (t))
04407   {
04408     TYPE_CONTEXT (t) = FROB_CONTEXT (context);
04409     
04410     type_decl = create_implicit_typedef (DECL_NAME (template), t);
04411     DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
04412     TYPE_STUB_DECL (t) = type_decl;
04413     DECL_SOURCE_LOCATION (type_decl) 
04414       = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
04415   }
04416       else
04417   type_decl = TYPE_NAME (t);
04418 
04419       TREE_PRIVATE (type_decl)
04420   = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
04421       TREE_PROTECTED (type_decl)
04422   = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
04423 
04424       /* Set up the template information.  We have to figure out which
04425    template is the immediate parent if this is a full
04426    instantiation.  */
04427       if (parm_depth == 1 || is_partial_instantiation
04428     || !PRIMARY_TEMPLATE_P (template))
04429   /* This case is easy; there are no member templates involved.  */
04430   found = template;
04431       else
04432   {
04433     /* This is a full instantiation of a member template.  Look
04434        for a partial instantiation of which this is an instance.  */
04435 
04436     for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
04437          found; found = TREE_CHAIN (found))
04438       {
04439         int success;
04440         tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
04441 
04442         /* We only want partial instantiations, here, not
04443      specializations or full instantiations.  */
04444         if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
04445       || !uses_template_parms (TREE_VALUE (found)))
04446     continue;
04447 
04448         /* Temporarily reduce by one the number of levels in the
04449      ARGLIST and in FOUND so as to avoid comparing the
04450      last set of arguments.  */
04451         TREE_VEC_LENGTH (arglist)--;
04452         TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
04453 
04454         /* See if the arguments match.  If they do, then TMPL is
04455      the partial instantiation we want.  */
04456         success = comp_template_args (TREE_PURPOSE (found), arglist);
04457 
04458         /* Restore the argument vectors to their full size.  */
04459         TREE_VEC_LENGTH (arglist)++;
04460         TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
04461 
04462         if (success)
04463     {
04464       found = tmpl;
04465       break;
04466     }
04467       }
04468 
04469     if (!found)
04470       {
04471         /* There was no partial instantiation. This happens
04472                  where C<T> is a member template of A<T> and it's used
04473                  in something like
04474                 
04475                   template <typename T> struct B { A<T>::C<int> m; };
04476                   B<float>;
04477                 
04478                  Create the partial instantiation.
04479                */
04480               TREE_VEC_LENGTH (arglist)--;
04481               found = tsubst (template, arglist, complain, NULL_TREE);
04482               TREE_VEC_LENGTH (arglist)++;
04483             }
04484   }
04485 
04486       SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));  
04487       DECL_TEMPLATE_INSTANTIATIONS (template) 
04488   = tree_cons (arglist, t, 
04489          DECL_TEMPLATE_INSTANTIATIONS (template));
04490 
04491       if (TREE_CODE (t) == ENUMERAL_TYPE 
04492     && !is_partial_instantiation)
04493   /* Now that the type has been registered on the instantiations
04494      list, we set up the enumerators.  Because the enumeration
04495      constants may involve the enumeration type itself, we make
04496      sure to register the type first, and then create the
04497      constants.  That way, doing tsubst_expr for the enumeration
04498      constants won't result in recursive calls here; we'll find
04499      the instantiation and exit above.  */
04500   tsubst_enum (template_type, t, arglist);
04501 
04502       /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
04503    is set up.  */
04504       if (TREE_CODE (t) != ENUMERAL_TYPE)
04505   DECL_NAME (type_decl) = classtype_mangled_name (t);
04506       if (!is_partial_instantiation)
04507   {
04508     /* For backwards compatibility; code that uses
04509        -fexternal-templates expects looking up a template to
04510        instantiate it.  I think DDD still relies on this.
04511        (jason 8/20/1998) */
04512     if (TREE_CODE (t) != ENUMERAL_TYPE
04513         && flag_external_templates
04514         && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
04515         && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
04516       add_pending_template (t);
04517   }
04518       else
04519   /* If the type makes use of template parameters, the
04520      code that generates debugging information will crash.  */
04521   DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
04522 
04523       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
04524     }
04525   timevar_pop (TV_NAME_LOOKUP);
04526 }
04527 
04528 struct pair_fn_data 
04529 {
04530   tree_fn_t fn;
04531   void *data;
04532   htab_t visited;
04533 };
04534 
04535 /* Called from for_each_template_parm via walk_tree.  */
04536 
04537 static tree
04538 for_each_template_parm_r (tp, walk_subtrees, d)
04539      tree *tp;
04540      int *walk_subtrees;
04541      void *d;
04542 {
04543   tree t = *tp;
04544   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
04545   tree_fn_t fn = pfd->fn;
04546   void *data = pfd->data;
04547   void **slot;
04548 
04549   /* If we have already visited this tree, there's no need to walk
04550      subtrees.  Otherwise, add it to the visited table.  */
04551   slot = htab_find_slot (pfd->visited, *tp, INSERT);
04552   if (*slot)
04553     {
04554       *walk_subtrees = 0;
04555       return NULL_TREE;
04556     }
04557   *slot = *tp;
04558 
04559   if (TYPE_P (t)
04560       && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
04561     return error_mark_node;
04562 
04563   switch (TREE_CODE (t))
04564     {
04565     case RECORD_TYPE:
04566       if (TYPE_PTRMEMFUNC_P (t))
04567   break;
04568       /* Fall through.  */
04569 
04570     case UNION_TYPE:
04571     case ENUMERAL_TYPE:
04572       if (!TYPE_TEMPLATE_INFO (t))
04573   *walk_subtrees = 0;
04574       else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
04575                fn, data, pfd->visited))
04576   return error_mark_node;
04577       break;
04578 
04579     case METHOD_TYPE:
04580       /* Since we're not going to walk subtrees, we have to do this
04581    explicitly here.  */
04582       if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
04583           pfd->visited))
04584   return error_mark_node;
04585       /* Fall through.  */
04586 
04587     case FUNCTION_TYPE:
04588       /* Check the return type.  */
04589       if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
04590   return error_mark_node;
04591 
04592       /* Check the parameter types.  Since default arguments are not
04593    instantiated until they are needed, the TYPE_ARG_TYPES may
04594    contain expressions that involve template parameters.  But,
04595    no-one should be looking at them yet.  And, once they're
04596    instantiated, they don't contain template parameters, so
04597    there's no point in looking at them then, either.  */
04598       {
04599   tree parm;
04600 
04601   for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
04602     if (for_each_template_parm (TREE_VALUE (parm), fn, data,
04603               pfd->visited))
04604       return error_mark_node;
04605 
04606   /* Since we've already handled the TYPE_ARG_TYPES, we don't
04607      want walk_tree walking into them itself.  */
04608   *walk_subtrees = 0;
04609       }
04610       break;
04611 
04612     case FUNCTION_DECL:
04613     case VAR_DECL:
04614       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
04615     && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
04616              pfd->visited))
04617   return error_mark_node;
04618       /* Fall through.  */
04619 
04620     case CONST_DECL:
04621     case PARM_DECL:
04622       if (DECL_CONTEXT (t) 
04623     && for_each_template_parm (DECL_CONTEXT (t), fn, data,
04624              pfd->visited))
04625   return error_mark_node;
04626       break;
04627 
04628     case BOUND_TEMPLATE_TEMPLATE_PARM:
04629       /* Record template parameters such as `T' inside `TT<T>'.  */
04630       if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
04631   return error_mark_node;
04632       /* Fall through.  */
04633 
04634     case TEMPLATE_TEMPLATE_PARM:
04635     case TEMPLATE_TYPE_PARM:
04636     case TEMPLATE_PARM_INDEX:
04637       if (fn && (*fn)(t, data))
04638   return error_mark_node;
04639       else if (!fn)
04640   return error_mark_node;
04641       break;
04642 
04643     case TEMPLATE_DECL:
04644       /* A template template parameter is encountered */
04645       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
04646     && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
04647   return error_mark_node;
04648 
04649       /* Already substituted template template parameter */
04650       *walk_subtrees = 0;
04651       break;
04652 
04653     case TYPENAME_TYPE:
04654       if (!fn 
04655     || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
04656              data, pfd->visited))
04657   return error_mark_node;
04658       break;
04659 
04660     case CONSTRUCTOR:
04661       if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
04662     && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
04663              (TREE_TYPE (t)), fn, data,
04664              pfd->visited))
04665   return error_mark_node;
04666       break;
04667       
04668     case INDIRECT_REF:
04669     case COMPONENT_REF:
04670       /* If there's no type, then this thing must be some expression
04671    involving template parameters.  */
04672       if (!fn && !TREE_TYPE (t))
04673   return error_mark_node;
04674       break;
04675 
04676     case MODOP_EXPR:
04677     case CAST_EXPR:
04678     case REINTERPRET_CAST_EXPR:
04679     case CONST_CAST_EXPR:
04680     case STATIC_CAST_EXPR:
04681     case DYNAMIC_CAST_EXPR:
04682     case ARROW_EXPR:
04683     case DOTSTAR_EXPR:
04684     case TYPEID_EXPR:
04685     case LOOKUP_EXPR:
04686     case PSEUDO_DTOR_EXPR:
04687       if (!fn)
04688   return error_mark_node;
04689       break;
04690 
04691     case BASELINK:
04692       /* If we do not handle this case specially, we end up walking
04693    the BINFO hierarchy, which is circular, and therefore
04694    confuses walk_tree.  */
04695       *walk_subtrees = 0;
04696       if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
04697           pfd->visited))
04698   return error_mark_node;
04699       break;
04700 
04701     default:
04702       break;
04703     }
04704 
04705   /* We didn't find any template parameters we liked.  */
04706   return NULL_TREE;
04707 }
04708 
04709 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, 
04710    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, 
04711    call FN with the parameter and the DATA.
04712    If FN returns nonzero, the iteration is terminated, and
04713    for_each_template_parm returns 1.  Otherwise, the iteration
04714    continues.  If FN never returns a nonzero value, the value
04715    returned by for_each_template_parm is 0.  If FN is NULL, it is
04716    considered to be the function which always returns 1.  */
04717 
04718 static int
04719 for_each_template_parm (t, fn, data, visited)
04720      tree t;
04721      tree_fn_t fn;
04722      void* data;
04723      htab_t visited;
04724 {
04725   struct pair_fn_data pfd;
04726   int result;
04727 
04728   /* Set up.  */
04729   pfd.fn = fn;
04730   pfd.data = data;
04731 
04732   /* Walk the tree.  (Conceptually, we would like to walk without
04733      duplicates, but for_each_template_parm_r recursively calls
04734      for_each_template_parm, so we would need to reorganize a fair
04735      bit to use walk_tree_without_duplicates, so we keep our own
04736      visited list.)  */
04737   if (visited)
04738     pfd.visited = visited;
04739   else
04740     pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, 
04741              NULL);
04742   result = walk_tree (&t, 
04743           for_each_template_parm_r, 
04744           &pfd,
04745           NULL) != NULL_TREE;
04746 
04747   /* Clean up.  */
04748   if (!visited)
04749     htab_delete (pfd.visited);
04750 
04751   return result;
04752 }
04753 
04754 int
04755 uses_template_parms (t)
04756      tree t;
04757 {
04758   return for_each_template_parm (t, 0, 0, NULL);
04759 }
04760 
04761 static int tinst_depth;
04762 extern int max_tinst_depth;
04763 #ifdef GATHER_STATISTICS
04764 int depth_reached;
04765 #endif
04766 static int tinst_level_tick;
04767 static int last_template_error_tick;
04768 
04769 /* We're starting to instantiate D; record the template instantiation context
04770    for diagnostics and to restore it later.  */
04771 
04772 int
04773 push_tinst_level (d)
04774      tree d;
04775 {
04776   tree new;
04777 
04778   if (tinst_depth >= max_tinst_depth)
04779     {
04780       /* If the instantiation in question still has unbound template parms,
04781    we don't really care if we can't instantiate it, so just return.
04782          This happens with base instantiation for implicit `typename'.  */
04783       if (uses_template_parms (d))
04784   return 0;
04785 
04786       last_template_error_tick = tinst_level_tick;
04787       error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
04788        max_tinst_depth, d);
04789 
04790       print_instantiation_context ();
04791 
04792       return 0;
04793     }
04794 
04795   new = build_expr_wfl (d, input_filename, lineno, 0);
04796   TREE_CHAIN (new) = current_tinst_level;
04797   current_tinst_level = new;
04798 
04799   ++tinst_depth;
04800 #ifdef GATHER_STATISTICS
04801   if (tinst_depth > depth_reached)
04802     depth_reached = tinst_depth;
04803 #endif
04804 
04805   ++tinst_level_tick;
04806   return 1;
04807 }
04808 
04809 /* We're done instantiating this template; return to the instantiation
04810    context.  */
04811 
04812 void
04813 pop_tinst_level ()
04814 {
04815   tree old = current_tinst_level;
04816 
04817   /* Restore the filename and line number stashed away when we started
04818      this instantiation.  */
04819   lineno = TINST_LINE (old);
04820   input_filename = TINST_FILE (old);
04821   extract_interface_info ();
04822   
04823   current_tinst_level = TREE_CHAIN (old);
04824   --tinst_depth;
04825   ++tinst_level_tick;
04826 }
04827 
04828 /* We're instantiating a deferred template; restore the template
04829    instantiation context in which the instantiation was requested, which
04830    is one step out from LEVEL.  */
04831 
04832 static void
04833 reopen_tinst_level (level)
04834      tree level;
04835 {
04836   tree t;
04837 
04838   tinst_depth = 0;
04839   for (t = level; t; t = TREE_CHAIN (t))
04840     ++tinst_depth;
04841 
04842   current_tinst_level = level;
04843   pop_tinst_level ();
04844 }
04845 
04846 /* Return the outermost template instantiation context, for use with
04847    -falt-external-templates.  */
04848 
04849 tree
04850 tinst_for_decl ()
04851 {
04852   tree p = current_tinst_level;
04853 
04854   if (p)
04855     for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
04856       ;
04857   return p;
04858 }
04859 
04860 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
04861    vector of template arguments, as for tsubst.
04862 
04863    Returns an appropriate tsubst'd friend declaration.  */
04864 
04865 static tree
04866 tsubst_friend_function (decl, args)
04867      tree decl;
04868      tree args;
04869 {
04870   tree new_friend;
04871   int line = lineno;
04872   const char *file = input_filename;
04873 
04874   lineno = DECL_SOURCE_LINE (decl);
04875   input_filename = DECL_SOURCE_FILE (decl);
04876 
04877   if (TREE_CODE (decl) == FUNCTION_DECL 
04878       && DECL_TEMPLATE_INSTANTIATION (decl)
04879       && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
04880     /* This was a friend declared with an explicit template
04881        argument list, e.g.:
04882        
04883        friend void f<>(T);
04884        
04885        to indicate that f was a template instantiation, not a new
04886        function declaration.  Now, we have to figure out what
04887        instantiation of what template.  */
04888     {
04889       tree template_id, arglist, fns;
04890       tree new_args;
04891       tree tmpl;
04892       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
04893       
04894       /* Friend functions are looked up in the containing namespace scope.
04895          We must enter that scope, to avoid finding member functions of the
04896          current cless with same name.  */
04897       push_nested_namespace (ns);
04898       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
04899                          tf_error | tf_warning, NULL_TREE);
04900       pop_nested_namespace (ns);
04901       arglist = tsubst (DECL_TI_ARGS (decl), args,
04902                         tf_error | tf_warning, NULL_TREE);
04903       template_id = lookup_template_function (fns, arglist);
04904       
04905       new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
04906       tmpl = determine_specialization (template_id, new_friend,
04907                &new_args, 
04908                /*need_member_template=*/0);
04909       new_friend = instantiate_template (tmpl, new_args);
04910       goto done;
04911     }
04912 
04913   new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
04914   
04915   /* The NEW_FRIEND will look like an instantiation, to the
04916      compiler, but is not an instantiation from the point of view of
04917      the language.  For example, we might have had:
04918      
04919      template <class T> struct S {
04920        template <class U> friend void f(T, U);
04921      };
04922      
04923      Then, in S<int>, template <class U> void f(int, U) is not an
04924      instantiation of anything.  */
04925   DECL_USE_TEMPLATE (new_friend) = 0;
04926   if (TREE_CODE (decl) == TEMPLATE_DECL)
04927     {
04928       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
04929       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
04930   = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
04931     }
04932 
04933   /* The mangled name for the NEW_FRIEND is incorrect.  The function
04934      is not a template instantiation and should not be mangled like
04935      one.  Therefore, we forget the mangling here; we'll recompute it
04936      later if we need it.  */
04937   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
04938     {
04939       SET_DECL_RTL (new_friend, NULL_RTX);
04940       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
04941     }
04942       
04943   if (DECL_NAMESPACE_SCOPE_P (new_friend))
04944     {
04945       tree old_decl;
04946       tree new_friend_template_info;
04947       tree new_friend_result_template_info;
04948       tree ns;
04949       int  new_friend_is_defn;
04950 
04951       /* We must save some information from NEW_FRIEND before calling
04952    duplicate decls since that function will free NEW_FRIEND if
04953    possible.  */
04954       new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
04955       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
04956   {
04957     /* This declaration is a `primary' template.  */
04958     DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
04959     
04960     new_friend_is_defn 
04961       = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
04962     new_friend_result_template_info
04963       = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
04964   }
04965       else
04966   {
04967     new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
04968     new_friend_result_template_info = NULL_TREE;
04969   }
04970 
04971       /* Inside pushdecl_namespace_level, we will push into the
04972    current namespace. However, the friend function should go
04973    into the namespace of the template.  */
04974       ns = decl_namespace_context (new_friend);
04975       push_nested_namespace (ns);
04976       old_decl = pushdecl_namespace_level (new_friend);
04977       pop_nested_namespace (ns);
04978 
04979       if (old_decl != new_friend)
04980   {
04981     /* This new friend declaration matched an existing
04982        declaration.  For example, given:
04983 
04984          template <class T> void f(T);
04985          template <class U> class C { 
04986      template <class T> friend void f(T) {} 
04987          };
04988 
04989        the friend declaration actually provides the definition
04990        of `f', once C has been instantiated for some type.  So,
04991        old_decl will be the out-of-class template declaration,
04992        while new_friend is the in-class definition.
04993 
04994        But, if `f' was called before this point, the
04995        instantiation of `f' will have DECL_TI_ARGS corresponding
04996        to `T' but not to `U', references to which might appear
04997        in the definition of `f'.  Previously, the most general
04998        template for an instantiation of `f' was the out-of-class
04999        version; now it is the in-class version.  Therefore, we
05000        run through all specialization of `f', adding to their
05001        DECL_TI_ARGS appropriately.  In particular, they need a
05002        new set of outer arguments, corresponding to the
05003        arguments for this class instantiation.  
05004 
05005        The same situation can arise with something like this:
05006 
05007          friend void f(int);
05008          template <class T> class C { 
05009            friend void f(T) {}
05010                };
05011 
05012        when `C<int>' is instantiated.  Now, `f(int)' is defined
05013        in the class.  */
05014 
05015     if (!new_friend_is_defn)
05016       /* On the other hand, if the in-class declaration does
05017          *not* provide a definition, then we don't want to alter
05018          existing definitions.  We can just leave everything
05019          alone.  */
05020       ;
05021     else
05022       {
05023         /* Overwrite whatever template info was there before, if
05024      any, with the new template information pertaining to
05025      the declaration.  */
05026         DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
05027 
05028         if (TREE_CODE (old_decl) != TEMPLATE_DECL)
05029     reregister_specialization (new_friend,
05030              most_general_template (old_decl),
05031              old_decl);
05032         else 
05033     {
05034       tree t;
05035       tree new_friend_args;
05036 
05037       DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) 
05038         = new_friend_result_template_info;
05039         
05040       new_friend_args = TI_ARGS (new_friend_template_info);
05041       for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); 
05042            t != NULL_TREE;
05043            t = TREE_CHAIN (t))
05044         {
05045           tree spec = TREE_VALUE (t);
05046       
05047           DECL_TI_ARGS (spec) 
05048       = add_outermost_template_args (new_friend_args,
05049                    DECL_TI_ARGS (spec));
05050         }
05051 
05052       /* Now, since specializations are always supposed to
05053          hang off of the most general template, we must move
05054          them.  */
05055       t = most_general_template (old_decl);
05056       if (t != old_decl)
05057         {
05058           DECL_TEMPLATE_SPECIALIZATIONS (t)
05059       = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
05060            DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
05061           DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
05062         }
05063     }
05064       }
05065 
05066     /* The information from NEW_FRIEND has been merged into OLD_DECL
05067        by duplicate_decls.  */
05068     new_friend = old_decl;
05069   }
05070     }
05071   else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
05072     {
05073       /* Check to see that the declaration is really present, and,
05074    possibly obtain an improved declaration.  */
05075       tree fn = check_classfn (DECL_CONTEXT (new_friend),
05076              new_friend);
05077       
05078       if (fn)
05079   new_friend = fn;
05080     }
05081 
05082  done:
05083   lineno = line;
05084   input_filename = file;
05085   return new_friend;
05086 }
05087 
05088 /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
05089    template arguments, as for tsubst.
05090 
05091    Returns an appropriate tsubst'd friend type or error_mark_node on
05092    failure.  */
05093 
05094 static tree
05095 tsubst_friend_class (friend_tmpl, args)
05096      tree friend_tmpl;
05097      tree args;
05098 {
05099   tree friend_type;
05100   tree tmpl;
05101   tree context;
05102 
05103   context = DECL_CONTEXT (friend_tmpl);
05104 
05105   if (context)
05106     {
05107       if (TREE_CODE (context) == NAMESPACE_DECL)
05108   push_nested_namespace (context);
05109       else
05110   push_nested_class (tsubst (context, args, tf_none, NULL_TREE), 2);
05111     }
05112 
05113   /* First, we look for a class template.  */
05114   tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); 
05115 
05116   /* But, if we don't find one, it might be because we're in a
05117      situation like this:
05118 
05119        template <class T>
05120        struct S {
05121    template <class U>
05122    friend struct S;
05123        };
05124 
05125      Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
05126      for `S<int>', not the TEMPLATE_DECL.  */
05127   if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
05128     {
05129       tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
05130       tmpl = maybe_get_template_decl_from_type_decl (tmpl);
05131     }
05132 
05133   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
05134     {
05135       /* The friend template has already been declared.  Just
05136    check to see that the declarations match, and install any new
05137    default parameters.  We must tsubst the default parameters,
05138    of course.  We only need the innermost template parameters
05139    because that is all that redeclare_class_template will look
05140    at.  */
05141       if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
05142     > TMPL_ARGS_DEPTH (args))
05143   {
05144     tree parms;
05145     parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
05146            args, tf_error | tf_warning);
05147     redeclare_class_template (TREE_TYPE (tmpl), parms);
05148   }
05149 
05150       friend_type = TREE_TYPE (tmpl);
05151     }
05152   else
05153     {
05154       /* The friend template has not already been declared.  In this
05155    case, the instantiation of the template class will cause the
05156    injection of this template into the global scope.  */
05157       tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
05158 
05159       /* The new TMPL is not an instantiation of anything, so we
05160    forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE for
05161    the new type because that is supposed to be the corresponding
05162    template decl, i.e., TMPL.  */
05163       DECL_USE_TEMPLATE (tmpl) = 0;
05164       DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
05165       CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
05166 
05167       /* Inject this template into the global scope.  */
05168       friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
05169     }
05170 
05171   if (context) 
05172     {
05173       if (TREE_CODE (context) == NAMESPACE_DECL)
05174   pop_nested_namespace (context);
05175       else
05176   pop_nested_class ();
05177     }
05178 
05179   return friend_type;
05180 }
05181 
05182 /* Returns zero if TYPE cannot be completed later due to circularity.
05183    Otherwise returns one.  */
05184 
05185 static int
05186 can_complete_type_without_circularity (type)
05187      tree type;
05188 {
05189   if (type == NULL_TREE || type == error_mark_node)
05190     return 0;
05191   else if (COMPLETE_TYPE_P (type))
05192     return 1;
05193   else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
05194     return can_complete_type_without_circularity (TREE_TYPE (type));
05195   else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
05196     return 0;
05197   else
05198     return 1;
05199 }
05200 
05201 tree
05202 instantiate_class_template (type)
05203      tree type;
05204 {
05205   tree template, args, pattern, t, member;
05206   tree typedecl;
05207 
05208   if (type == error_mark_node)
05209     return error_mark_node;
05210 
05211   if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
05212     return type;
05213 
05214   /* Figure out which template is being instantiated.  */
05215   template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
05216   my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
05217 
05218   /* Figure out which arguments are being used to do the
05219      instantiation.  */
05220   args = CLASSTYPE_TI_ARGS (type);
05221   PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
05222 
05223   if (pedantic && PARTIAL_INSTANTIATION_P (type))
05224     /* If this is a partial instantiation, then we can't instantiate
05225        the type; there's no telling whether or not one of the
05226        template parameters might eventually be instantiated to some
05227        value that results in a specialization being used.  For
05228        example, consider:
05229 
05230          template <class T>
05231          struct S {};
05232 
05233          template <class U> 
05234          void f(S<U>);
05235        
05236          template <> 
05237          struct S<int> {};
05238 
05239        Now, the `S<U>' in `f<int>' is the specialization, not an
05240        instantiation of the original template.  */
05241     return type;
05242 
05243   /* Determine what specialization of the original template to
05244      instantiate.  */
05245   if (PARTIAL_INSTANTIATION_P (type))
05246     /* There's no telling which specialization is appropriate at this
05247        point.  Since all peeking at the innards of this partial
05248        instantiation are extensions (like the "implicit typename"
05249        extension, which allows users to omit the keyword `typename' on
05250        names that are declared as types in template base classes), we
05251        are free to do what we please.
05252 
05253        Trying to figure out which partial instantiation to use can
05254        cause a crash.  (Some of the template arguments don't even have
05255        types.)  So, we just use the most general version.  */
05256     t = NULL_TREE;
05257   else
05258     {
05259       t = most_specialized_class (template, args);
05260 
05261       if (t == error_mark_node)
05262   {
05263     const char *str = "candidates are:";
05264     error ("ambiguous class template instantiation for `%#T'", type);
05265     for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; 
05266          t = TREE_CHAIN (t))
05267       {
05268         if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
05269               args))
05270     {
05271       cp_error_at ("%s %+#T", str, TREE_TYPE (t));
05272       str = "               ";
05273     }
05274       }
05275     TYPE_BEING_DEFINED (type) = 1;
05276     return error_mark_node;
05277   }
05278     }
05279 
05280   if (t)
05281     pattern = TREE_TYPE (t);
05282   else
05283     pattern = TREE_TYPE (template);
05284 
05285   /* If the template we're instantiating is incomplete, then clearly
05286      there's nothing we can do.  */
05287   if (!COMPLETE_TYPE_P (pattern))
05288     return type;
05289 
05290   /* If this is a partial instantiation, don't tsubst anything.  We will
05291      only use this type for implicit typename, so the actual contents don't
05292      matter.  All that matters is whether a particular name is a type.  */
05293   if (PARTIAL_INSTANTIATION_P (type))
05294     {
05295       /* The fields set here must be kept in sync with those cleared
05296    in begin_class_definition.  */
05297       TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
05298       TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
05299       TYPE_METHODS (type) = TYPE_METHODS (pattern);
05300       CLASSTYPE_DECL_LIST (type) = CLASSTYPE_DECL_LIST (pattern);
05301       CLASSTYPE_NESTED_UDTS (type) = CLASSTYPE_NESTED_UDTS (pattern);
05302       CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
05303       
05304       /* Pretend that the type is complete, so that we will look
05305    inside it during name lookup and such.  */
05306       TYPE_SIZE (type) = bitsize_zero_node;
05307       return type;
05308     }
05309 
05310   /* If we've recursively instantiated too many templates, stop.  */
05311   if (! push_tinst_level (type))
05312     return type;
05313 
05314   /* Now we're really doing the instantiation.  Mark the type as in
05315      the process of being defined.  */
05316   TYPE_BEING_DEFINED (type) = 1;
05317 
05318   maybe_push_to_top_level (uses_template_parms (type));
05319 
05320   if (t)
05321     {
05322       /* This TYPE is actually an instantiation of a partial
05323    specialization.  We replace the innermost set of ARGS with
05324    the arguments appropriate for substitution.  For example,
05325    given:
05326 
05327      template <class T> struct S {};
05328      template <class T> struct S<T*> {};
05329    
05330    and supposing that we are instantiating S<int*>, ARGS will
05331    present be {int*} but we need {int}.  */
05332       tree inner_args 
05333   = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
05334             args);
05335 
05336       /* If there were multiple levels in ARGS, replacing the
05337    innermost level would alter CLASSTYPE_TI_ARGS, which we don't
05338    want, so we make a copy first.  */
05339       if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
05340   {
05341     args = copy_node (args);
05342     SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
05343   }
05344       else
05345   args = inner_args;
05346     }
05347 
05348   if (flag_external_templates)
05349     {
05350       if (flag_alt_external_templates)
05351   {
05352     CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
05353     SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
05354   }
05355       else
05356   {
05357     CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
05358     SET_CLASSTYPE_INTERFACE_UNKNOWN_X
05359       (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
05360   }
05361     }
05362   else
05363     {
05364       SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
05365     }
05366 
05367   TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
05368   TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
05369   TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
05370   TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
05371   TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
05372   TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
05373   TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
05374   TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
05375   TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
05376   TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
05377   TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
05378   TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
05379   TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
05380   TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
05381   TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
05382   TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
05383     = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
05384   TYPE_USES_MULTIPLE_INHERITANCE (type)
05385     = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
05386   TYPE_USES_VIRTUAL_BASECLASSES (type)
05387     = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
05388   TYPE_PACKED (type) = TYPE_PACKED (pattern);
05389   TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
05390   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
05391   TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
05392   if (ANON_AGGR_TYPE_P (pattern))
05393     SET_ANON_AGGR_TYPE_P (type);
05394 
05395   if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)))
05396     /* First instantiate our enclosing class.  */
05397     complete_type (TYPE_CONTEXT (type));
05398 
05399   if (TYPE_BINFO_BASETYPES (pattern))
05400     {
05401       tree base_list = NULL_TREE;
05402       tree pbases = TYPE_BINFO_BASETYPES (pattern);
05403       int i;
05404 
05405       /* Substitute into each of the bases to determine the actual
05406    basetypes.  */
05407       for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
05408   {
05409     tree base;
05410     tree access;
05411     tree pbase;
05412 
05413     pbase = TREE_VEC_ELT (pbases, i);
05414 
05415     /* Substitute to figure out the base class.  */
05416     base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
05417     if (base == error_mark_node)
05418       continue;
05419 
05420     /* Calculate the correct access node.  */
05421     if (TREE_VIA_VIRTUAL (pbase)) 
05422       {
05423         if (TREE_VIA_PUBLIC (pbase))
05424     access = access_public_virtual_node;
05425         else if (TREE_VIA_PROTECTED (pbase))
05426     access = access_protected_virtual_node;
05427         else 
05428     access = access_private_virtual_node;
05429       }
05430     else
05431       {
05432         if (TREE_VIA_PUBLIC (pbase))
05433     access = access_public_node;
05434         else if (TREE_VIA_PROTECTED (pbase))
05435     access = access_protected_node;
05436         else 
05437     access = access_private_node;
05438       }
05439 
05440     base_list = tree_cons (access, base, base_list);
05441   }
05442 
05443       /* The list is now in reverse order; correct that.  */
05444       base_list = nreverse (base_list);
05445 
05446       /* Now call xref_basetypes to set up all the base-class
05447    information.  */
05448       xref_basetypes (type, base_list);
05449     }
05450 
05451   /* Now that our base classes are set up, enter the scope of the
05452      class, so that name lookups into base classes, etc. will work
05453      correctly.  This is precisely analogous to what we do in
05454      begin_class_definition when defining an ordinary non-template
05455      class.  */
05456   pushclass (type, 1);
05457 
05458   /* Now members are processed in the order of declaration.  */
05459   for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
05460     {
05461       tree t = TREE_VALUE (member);
05462 
05463       if (TREE_PURPOSE (member))
05464   {
05465     if (TYPE_P (t))
05466       {
05467         /* Build new CLASSTYPE_NESTED_UDTS.  */
05468 
05469         tree tag = t;
05470         tree name = TYPE_IDENTIFIER (tag);
05471         tree newtag;
05472 
05473         newtag = tsubst (tag, args, tf_error, NULL_TREE);
05474         my_friendly_assert (newtag != error_mark_node, 20010206);
05475         if (TREE_CODE (newtag) != ENUMERAL_TYPE)
05476     {
05477       if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
05478         /* Unfortunately, lookup_template_class sets
05479            CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
05480            instantiation (i.e., for the type of a member template
05481            class nested within a template class.)  This behavior is
05482            required for maybe_process_partial_specialization to work
05483            correctly, but is not accurate in this case; the TAG is not
05484            an instantiation of anything.  (The corresponding
05485            TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
05486         CLASSTYPE_USE_TEMPLATE (newtag) = 0;
05487 
05488       /* Now, we call pushtag to put this NEWTAG into the scope of
05489          TYPE.  We first set up the IDENTIFIER_TYPE_VALUE to avoid
05490          pushtag calling push_template_decl.  We don't have to do
05491          this for enums because it will already have been done in
05492          tsubst_enum.  */
05493       if (name)
05494         SET_IDENTIFIER_TYPE_VALUE (name, newtag);
05495       pushtag (name, newtag, /*globalize=*/0);
05496     }
05497       }
05498     else if (TREE_CODE (t) == FUNCTION_DECL 
05499        || DECL_FUNCTION_TEMPLATE_P (t))
05500       {
05501         /* Build new TYPE_METHODS.  */
05502 
05503         tree r = tsubst (t, args, tf_error, NULL_TREE);
05504         set_current_access_from_decl (r);
05505         grok_special_member_properties (r);
05506         finish_member_declaration (r);
05507       }
05508     else
05509       {
05510         /* Build new TYPE_FIELDS.  */
05511 
05512         if (TREE_CODE (t) != CONST_DECL)
05513     {
05514       tree r;
05515 
05516       /* The the file and line for this declaration, to assist
05517          in error message reporting.  Since we called 
05518          push_tinst_level above, we don't need to restore these.  */
05519       lineno = DECL_SOURCE_LINE (t);
05520       input_filename = DECL_SOURCE_FILE (t);
05521 
05522       r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
05523       if (TREE_CODE (r) == VAR_DECL)
05524         {
05525           tree init;
05526 
05527           if (DECL_INITIALIZED_IN_CLASS_P (r))
05528       init = tsubst_expr (DECL_INITIAL (t), args,
05529               tf_error | tf_warning, NULL_TREE);
05530           else
05531       init = NULL_TREE;
05532 
05533           finish_static_data_member_decl (r, init,
05534                   /*asmspec_tree=*/NULL_TREE, 
05535                   /*flags=*/0);
05536 
05537           if (DECL_INITIALIZED_IN_CLASS_P (r))
05538       check_static_variable_definition (r, TREE_TYPE (r));
05539         }
05540       else if (TREE_CODE (r) == FIELD_DECL)
05541         {
05542           /* Determine whether R has a valid type and can be
05543        completed later.  If R is invalid, then it is
05544        replaced by error_mark_node so that it will not be
05545        added to TYPE_FIELDS.  */
05546           tree rtype = TREE_TYPE (r);
05547           if (can_complete_type_without_circularity (rtype))
05548       complete_type (rtype);
05549 
05550           if (!COMPLETE_TYPE_P (rtype))
05551       {
05552         cxx_incomplete_type_error (r, rtype);
05553           r = error_mark_node;
05554       }
05555         }
05556 
05557       /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
05558          such a thing will already have been added to the field
05559          list by tsubst_enum in finish_member_declaration in the
05560          CLASSTYPE_NESTED_UDTS case above.  */
05561       if (!(TREE_CODE (r) == TYPE_DECL
05562       && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
05563       && DECL_ARTIFICIAL (r)))
05564         {
05565           set_current_access_from_decl (r);
05566           finish_member_declaration (r);
05567         }
05568           }
05569       }
05570   }
05571       else
05572   {
05573     if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
05574       {
05575         /* Build new CLASSTYPE_FRIEND_CLASSES.  */
05576 
05577         tree friend_type = t;
05578         tree new_friend_type;
05579 
05580         if (TREE_CODE (friend_type) == TEMPLATE_DECL)
05581     new_friend_type = tsubst_friend_class (friend_type, args);
05582         else if (uses_template_parms (friend_type))
05583     new_friend_type = tsubst (friend_type, args,
05584             tf_error | tf_warning, NULL_TREE);
05585         else 
05586     {
05587       tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
05588 
05589       /* The call to xref_tag_from_type does injection for friend
05590          classes.  */
05591       push_nested_namespace (ns);
05592       new_friend_type = 
05593         xref_tag_from_type (friend_type, NULL_TREE, 1);
05594       pop_nested_namespace (ns);
05595     }
05596 
05597         if (TREE_CODE (friend_type) == TEMPLATE_DECL)
05598     /* Trick make_friend_class into realizing that the friend
05599        we're adding is a template, not an ordinary class.  It's
05600        important that we use make_friend_class since it will
05601        perform some error-checking and output cross-reference
05602        information.  */
05603     ++processing_template_decl;
05604 
05605         if (new_friend_type != error_mark_node)
05606           make_friend_class (type, new_friend_type);
05607 
05608         if (TREE_CODE (friend_type) == TEMPLATE_DECL)
05609     --processing_template_decl;
05610       }
05611     else
05612       /* Build new DECL_FRIENDLIST.  */
05613       add_friend (type, tsubst_friend_function (t, args));
05614   }
05615     }
05616 
05617   /* Set the file and line number information to whatever is given for
05618      the class itself.  This puts error messages involving generated
05619      implicit functions at a predictable point, and the same point
05620      that would be used for non-template classes.  */
05621   typedecl = TYPE_MAIN_DECL (type);
05622   lineno = DECL_SOURCE_LINE (typedecl);
05623   input_filename = DECL_SOURCE_FILE (typedecl);
05624 
05625   unreverse_member_declarations (type);
05626   finish_struct_1 (type);
05627   CLASSTYPE_GOT_SEMICOLON (type) = 1;
05628 
05629   /* Clear this now so repo_template_used is happy.  */
05630   TYPE_BEING_DEFINED (type) = 0;
05631   repo_template_used (type);
05632 
05633   /* Now that the class is complete, instantiate default arguments for
05634      any member functions.  We don't do this earlier because the
05635      default arguments may reference members of the class.  */
05636   if (!PRIMARY_TEMPLATE_P (template))
05637     for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
05638       if (TREE_CODE (t) == FUNCTION_DECL 
05639     /* Implicitly generated member functions will not have template
05640        information; they are not instantiations, but instead are
05641        created "fresh" for each instantiation.  */
05642     && DECL_TEMPLATE_INFO (t))
05643   tsubst_default_arguments (t);
05644 
05645   popclass ();
05646   pop_from_top_level ();
05647   pop_tinst_level ();
05648 
05649   if (TYPE_CONTAINS_VPTR_P (type))
05650     keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
05651 
05652   return type;
05653 }
05654 
05655 static int
05656 list_eq (t1, t2)
05657      tree t1, t2;
05658 {
05659   if (t1 == NULL_TREE)
05660     return t2 == NULL_TREE;
05661   if (t2 == NULL_TREE)
05662     return 0;
05663   /* Don't care if one declares its arg const and the other doesn't -- the
05664      main variant of the arg type is all that matters.  */
05665   if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
05666       != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
05667     return 0;
05668   return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
05669 }
05670 
05671 /* If arg is a non-type template parameter that does not depend on template
05672    arguments, fold it like we weren't in the body of a template.  */
05673 
05674 static tree
05675 maybe_fold_nontype_arg (arg)
05676      tree arg;
05677 {
05678   if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
05679     {
05680       /* Sometimes, one of the args was an expression involving a
05681    template constant parameter, like N - 1.  Now that we've
05682    tsubst'd, we might have something like 2 - 1.  This will
05683    confuse lookup_template_class, so we do constant folding
05684    here.  We have to unset processing_template_decl, to
05685    fool build_expr_from_tree() into building an actual
05686    tree.  */
05687 
05688       /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
05689    as simple as it's going to get, and trying to reprocess
05690    the trees will break.  */
05691       if (!TREE_TYPE (arg))
05692   {
05693     int saved_processing_template_decl = processing_template_decl; 
05694     processing_template_decl = 0;
05695     arg = build_expr_from_tree (arg);
05696     processing_template_decl = saved_processing_template_decl; 
05697   }
05698 
05699       arg = fold (arg);
05700     }
05701   return arg;
05702 }
05703 
05704 /* Substitute ARGS into the vector of template arguments T.  */
05705 
05706 static tree
05707 tsubst_template_arg_vector (t, args, complain)
05708      tree t;
05709      tree args;
05710      tsubst_flags_t complain;
05711 {
05712   int len = TREE_VEC_LENGTH (t), need_new = 0, i;
05713   tree *elts = (tree *) alloca (len * sizeof (tree));
05714   
05715   memset ((char *) elts, 0, len * sizeof (tree));
05716   
05717   for (i = 0; i < len; i++)
05718     {
05719       if (TREE_VEC_ELT (t, i) != NULL_TREE
05720     && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
05721   elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
05722                 args, complain);
05723       else
05724   elts[i] = maybe_fold_nontype_arg
05725     (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
05726       NULL_TREE));
05727       
05728       if (elts[i] == error_mark_node)
05729   return error_mark_node;
05730 
05731       if (elts[i] != TREE_VEC_ELT (t, i))
05732   need_new = 1;
05733     }
05734   
05735   if (!need_new)
05736     return t;
05737   
05738   t = make_tree_vec (len);
05739   for (i = 0; i < len; i++)
05740     TREE_VEC_ELT (t, i) = elts[i];
05741   
05742   return t;
05743 }
05744 
05745 /* Return the result of substituting ARGS into the template parameters
05746    given by PARMS.  If there are m levels of ARGS and m + n levels of
05747    PARMS, then the result will contain n levels of PARMS.  For
05748    example, if PARMS is `template <class T> template <class U>
05749    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
05750    result will be `template <int*, double, class V>'.  */
05751 
05752 static tree
05753 tsubst_template_parms (parms, args, complain)
05754      tree parms;
05755      tree args;
05756      tsubst_flags_t complain;
05757 {
05758   tree r = NULL_TREE;
05759   tree* new_parms;
05760 
05761   for (new_parms = &r;
05762        TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
05763        new_parms = &(TREE_CHAIN (*new_parms)),
05764    parms = TREE_CHAIN (parms))
05765     {
05766       tree new_vec = 
05767   make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
05768       int i;
05769       
05770       for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
05771   {
05772     tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
05773     tree default_value = TREE_PURPOSE (tuple);
05774     tree parm_decl = TREE_VALUE (tuple);
05775 
05776     parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
05777     default_value = tsubst_expr (default_value, args,
05778                complain, NULL_TREE);
05779     tuple = build_tree_list (maybe_fold_nontype_arg (default_value), 
05780            parm_decl);
05781     TREE_VEC_ELT (new_vec, i) = tuple;
05782   }
05783       
05784       *new_parms = 
05785   tree_cons (size_int (TMPL_PARMS_DEPTH (parms) 
05786            - TMPL_ARGS_DEPTH (args)),
05787        new_vec, NULL_TREE);
05788     }
05789 
05790   return r;
05791 }
05792 
05793 /* Substitute the ARGS into the indicated aggregate (or enumeration)
05794    type T.  If T is not an aggregate or enumeration type, it is
05795    handled as if by tsubst.  IN_DECL is as for tsubst.  If
05796    ENTERING_SCOPE is nonzero, T is the context for a template which
05797    we are presently tsubst'ing.  Return the substituted value.  */
05798 
05799 static tree
05800 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
05801      tree t;
05802      tree args;
05803      tsubst_flags_t complain;
05804      tree in_decl;
05805      int entering_scope;
05806 {
05807   if (t == NULL_TREE)
05808     return NULL_TREE;
05809 
05810   switch (TREE_CODE (t))
05811     {
05812     case RECORD_TYPE:
05813       if (TYPE_PTRMEMFUNC_P (t))
05814   return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
05815 
05816       /* else fall through */
05817     case ENUMERAL_TYPE:
05818     case UNION_TYPE:
05819       if (TYPE_TEMPLATE_INFO (t))
05820   {
05821     tree argvec;
05822     tree context;
05823     tree r;
05824 
05825     /* First, determine the context for the type we are looking
05826        up.  */
05827     if (TYPE_CONTEXT (t) != NULL_TREE)
05828       context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
05829           complain,
05830           in_decl, /*entering_scope=*/1);
05831     else
05832       context = NULL_TREE;
05833 
05834     /* Then, figure out what arguments are appropriate for the
05835        type we are trying to find.  For example, given:
05836 
05837          template <class T> struct S;
05838          template <class T, class U> void f(T, U) { S<U> su; }
05839 
05840        and supposing that we are instantiating f<int, double>,
05841        then our ARGS will be {int, double}, but, when looking up
05842        S we only want {double}.  */
05843     argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
05844                  complain);
05845     if (argvec == error_mark_node)
05846       return error_mark_node;
05847 
05848       r = lookup_template_class (t, argvec, in_decl, context,
05849              entering_scope, complain);
05850 
05851     return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
05852   }
05853       else 
05854   /* This is not a template type, so there's nothing to do.  */
05855   return t;
05856 
05857     default:
05858       return tsubst (t, args, complain, in_decl);
05859     }
05860 }
05861 
05862 /* Substitute into the default argument ARG (a default argument for
05863    FN), which has the indicated TYPE.  */
05864 
05865 tree
05866 tsubst_default_argument (fn, type, arg)
05867      tree fn;
05868      tree type;
05869      tree arg;
05870 {
05871   /* This default argument came from a template.  Instantiate the
05872      default argument here, not in tsubst.  In the case of
05873      something like: 
05874      
05875        template <class T>
05876        struct S {
05877    static T t();
05878    void f(T = t());
05879        };
05880      
05881      we must be careful to do name lookup in the scope of S<T>,
05882      rather than in the current class.
05883 
05884      ??? current_class_type affects a lot more than name lookup.  This is
05885      very fragile.  Fortunately, it will go away when we do 2-phase name
05886      binding properly.  */
05887 
05888   /* FN is already the desired FUNCTION_DECL.  */
05889   push_access_scope (fn);
05890 
05891   arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
05892          tf_error | tf_warning, NULL_TREE);
05893   
05894   pop_access_scope (fn);
05895 
05896   /* Make sure the default argument is reasonable.  */
05897   arg = check_default_argument (type, arg);
05898 
05899   return arg;
05900 }
05901 
05902 /* Substitute into all the default arguments for FN.  */
05903 
05904 static void
05905 tsubst_default_arguments (fn)
05906      tree fn;
05907 {
05908   tree arg;
05909   tree tmpl_args;
05910 
05911   tmpl_args = DECL_TI_ARGS (fn);
05912 
05913   /* If this function is not yet instantiated, we certainly don't need
05914      its default arguments.  */
05915   if (uses_template_parms (tmpl_args))
05916     return;
05917 
05918   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); 
05919        arg; 
05920        arg = TREE_CHAIN (arg))
05921     if (TREE_PURPOSE (arg))
05922       TREE_PURPOSE (arg) = tsubst_default_argument (fn, 
05923                 TREE_VALUE (arg),
05924                 TREE_PURPOSE (arg));
05925 }
05926 
05927 /* Substitute the ARGS into the T, which is a _DECL.  TYPE is the
05928    (already computed) substitution of ARGS into TREE_TYPE (T), if
05929    appropriate.  Return the result of the substitution.  Issue error
05930    and warning messages under control of COMPLAIN.  */
05931 
05932 static tree
05933 tsubst_decl (t, args, type, complain)
05934      tree t;
05935      tree args;
05936      tree type;
05937      tsubst_flags_t complain;
05938 {
05939   int saved_lineno;
05940   const char *saved_filename;
05941   tree r = NULL_TREE;
05942   tree in_decl = t;
05943 
05944   /* Set the filename and linenumber to improve error-reporting.  */
05945   saved_lineno = lineno;
05946   saved_filename = input_filename;
05947   lineno = DECL_SOURCE_LINE (t);
05948   input_filename = DECL_SOURCE_FILE (t);
05949 
05950   switch (TREE_CODE (t))
05951     {
05952     case TEMPLATE_DECL:
05953       {
05954   /* We can get here when processing a member template function
05955      of a template class.  */
05956   tree decl = DECL_TEMPLATE_RESULT (t);
05957   tree spec;
05958   int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
05959 
05960   if (!is_template_template_parm)
05961     {
05962       /* We might already have an instance of this template.
05963          The ARGS are for the surrounding class type, so the
05964          full args contain the tsubst'd args for the context,
05965          plus the innermost args from the template decl.  */
05966       tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) 
05967         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
05968         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
05969       tree full_args;
05970       
05971       full_args = tsubst_template_arg_vector (tmpl_args, args,
05972                 complain);
05973 
05974       /* tsubst_template_arg_vector doesn't copy the vector if
05975          nothing changed.  But, *something* should have
05976          changed.  */
05977       my_friendly_assert (full_args != tmpl_args, 0);
05978 
05979       spec = retrieve_specialization (t, full_args);
05980       if (spec != NULL_TREE)
05981         {
05982     r = spec;
05983     break;
05984         }
05985     }
05986 
05987   /* Make a new template decl.  It will be similar to the
05988      original, but will record the current template arguments. 
05989      We also create a new function declaration, which is just
05990      like the old one, but points to this new template, rather
05991      than the old one.  */
05992   r = copy_decl (t);
05993   my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
05994   TREE_CHAIN (r) = NULL_TREE;
05995 
05996   if (is_template_template_parm)
05997     {
05998       tree new_decl = tsubst (decl, args, complain, in_decl);
05999       DECL_TEMPLATE_RESULT (r) = new_decl;
06000       TREE_TYPE (r) = TREE_TYPE (new_decl);
06001       break;
06002     }
06003 
06004   DECL_CONTEXT (r) 
06005     = tsubst_aggr_type (DECL_CONTEXT (t), args, 
06006             complain, in_decl, 
06007             /*entering_scope=*/1); 
06008   DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
06009 
06010   if (TREE_CODE (decl) == TYPE_DECL)
06011     {
06012       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06013       TREE_TYPE (r) = new_type;
06014       CLASSTYPE_TI_TEMPLATE (new_type) = r;
06015       DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
06016       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
06017     }
06018   else
06019     {
06020       tree new_decl = tsubst (decl, args, complain, in_decl);
06021 
06022       DECL_TEMPLATE_RESULT (r) = new_decl;
06023       DECL_TI_TEMPLATE (new_decl) = r;
06024       TREE_TYPE (r) = TREE_TYPE (new_decl);
06025       DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
06026     }
06027 
06028   SET_DECL_IMPLICIT_INSTANTIATION (r);
06029   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
06030   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
06031 
06032   /* The template parameters for this new template are all the
06033      template parameters for the old template, except the
06034      outermost level of parameters.  */
06035   DECL_TEMPLATE_PARMS (r) 
06036     = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
06037            complain);
06038 
06039   if (PRIMARY_TEMPLATE_P (t))
06040     DECL_PRIMARY_TEMPLATE (r) = r;
06041 
06042   /* We don't partially instantiate partial specializations.  */
06043   if (TREE_CODE (decl) == TYPE_DECL)
06044     break;
06045 
06046   /* Record this partial instantiation.  */
06047   register_specialization (r, t, 
06048          DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
06049 
06050       }
06051       break;
06052 
06053     case FUNCTION_DECL:
06054       {
06055   tree ctx;
06056   tree argvec = NULL_TREE;
06057   tree *friends;
06058   tree gen_tmpl;
06059   int member;
06060   int args_depth;
06061   int parms_depth;
06062 
06063   /* Nobody should be tsubst'ing into non-template functions.  */
06064   my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
06065 
06066   if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
06067     {
06068       tree spec;
06069 
06070       /* If T is not dependent, just return it.  */
06071       if (!uses_template_parms (DECL_TI_ARGS (t)))
06072         return t;
06073 
06074       /* Calculate the most general template of which R is a
06075          specialization, and the complete set of arguments used to
06076          specialize R.  */
06077       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
06078       argvec 
06079         = tsubst_template_arg_vector (DECL_TI_ARGS 
06080               (DECL_TEMPLATE_RESULT (gen_tmpl)),
06081               args, complain); 
06082 
06083       /* Check to see if we already have this specialization.  */
06084       spec = retrieve_specialization (gen_tmpl, argvec);
06085 
06086       if (spec)
06087         {
06088     r = spec;
06089     break;
06090         }
06091 
06092       /* We can see more levels of arguments than parameters if
06093          there was a specialization of a member template, like
06094          this:
06095 
06096            template <class T> struct S { template <class U> void f(); }
06097      template <> template <class U> void S<int>::f(U); 
06098 
06099          Here, we'll be substituting into the specialization,
06100          because that's where we can find the code we actually
06101          want to generate, but we'll have enough arguments for
06102          the most general template.        
06103 
06104          We also deal with the peculiar case:
06105 
06106      template <class T> struct S { 
06107        template <class U> friend void f();
06108      };
06109      template <class U> void f() {}
06110      template S<int>;
06111      template void f<double>();
06112 
06113          Here, the ARGS for the instantiation of will be {int,
06114          double}.  But, we only need as many ARGS as there are
06115          levels of template parameters in CODE_PATTERN.  We are
06116          careful not to get fooled into reducing the ARGS in
06117          situations like:
06118 
06119      template <class T> struct S { template <class U> void f(U); }
06120      template <class T> template <> void S<T>::f(int) {}
06121 
06122          which we can spot because the pattern will be a
06123          specialization in this case.  */
06124       args_depth = TMPL_ARGS_DEPTH (args);
06125       parms_depth = 
06126         TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); 
06127       if (args_depth > parms_depth
06128     && !DECL_TEMPLATE_SPECIALIZATION (t))
06129         args = get_innermost_template_args (args, parms_depth);
06130     }
06131   else
06132     {
06133       /* This special case arises when we have something like this:
06134 
06135            template <class T> struct S { 
06136        friend void f<int>(int, double); 
06137      };
06138 
06139          Here, the DECL_TI_TEMPLATE for the friend declaration
06140          will be a LOOKUP_EXPR or an IDENTIFIER_NODE.  We are
06141          being called from tsubst_friend_function, and we want
06142          only to create a new decl (R) with appropriate types so
06143          that we can call determine_specialization.  */
06144       my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) 
06145          == LOOKUP_EXPR)
06146         || (TREE_CODE (DECL_TI_TEMPLATE (t))
06147             == IDENTIFIER_NODE), 0);
06148       gen_tmpl = NULL_TREE;
06149     }
06150 
06151   if (DECL_CLASS_SCOPE_P (t))
06152     {
06153       if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
06154         member = 2;
06155       else
06156         member = 1;
06157       ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 
06158             complain, t, 
06159             /*entering_scope=*/1);
06160     }
06161   else
06162     {
06163       member = 0;
06164       ctx = DECL_CONTEXT (t);
06165     }
06166   type = tsubst (type, args, complain, in_decl);
06167   if (type == error_mark_node)
06168     return error_mark_node;
06169 
06170   /* We do NOT check for matching decls pushed separately at this
06171            point, as they may not represent instantiations of this
06172            template, and in any case are considered separate under the
06173            discrete model.  */
06174   r = copy_decl (t);
06175   DECL_USE_TEMPLATE (r) = 0;
06176   TREE_TYPE (r) = type;
06177   /* Clear out the mangled name and RTL for the instantiation.  */
06178   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
06179   SET_DECL_RTL (r, NULL_RTX);
06180 
06181   DECL_CONTEXT (r) = ctx;
06182 
06183   if (member && DECL_CONV_FN_P (r)) 
06184     /* Type-conversion operator.  Reconstruct the name, in
06185        case it's the name of one of the template's parameters.  */
06186     DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
06187 
06188   DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
06189              complain, t);
06190   DECL_RESULT (r) = NULL_TREE;
06191 
06192   TREE_STATIC (r) = 0;
06193   TREE_PUBLIC (r) = TREE_PUBLIC (t);
06194   DECL_EXTERNAL (r) = 1;
06195   DECL_INTERFACE_KNOWN (r) = 0;
06196   DECL_DEFER_OUTPUT (r) = 0;
06197   TREE_CHAIN (r) = NULL_TREE;
06198   DECL_PENDING_INLINE_INFO (r) = 0;
06199   DECL_PENDING_INLINE_P (r) = 0;
06200   DECL_SAVED_TREE (r) = NULL_TREE;
06201   TREE_USED (r) = 0;
06202   if (DECL_CLONED_FUNCTION (r))
06203     {
06204       DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
06205                  args, complain, t);
06206       TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
06207       TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
06208     }
06209 
06210   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
06211      this in the special friend case mentioned above where
06212      GEN_TMPL is NULL.  */
06213   if (gen_tmpl)
06214     {
06215       DECL_TEMPLATE_INFO (r) 
06216         = tree_cons (gen_tmpl, argvec, NULL_TREE);
06217       SET_DECL_IMPLICIT_INSTANTIATION (r);
06218       register_specialization (r, gen_tmpl, argvec);
06219 
06220       /* We're not supposed to instantiate default arguments
06221          until they are called, for a template.  But, for a
06222          declaration like:
06223 
06224            template <class T> void f () 
06225                  { extern void g(int i = T()); }
06226      
06227          we should do the substitution when the template is
06228          instantiated.  We handle the member function case in
06229          instantiate_class_template since the default arguments
06230          might refer to other members of the class.  */
06231       if (!member
06232     && !PRIMARY_TEMPLATE_P (gen_tmpl)
06233     && !uses_template_parms (argvec))
06234         tsubst_default_arguments (r);
06235     }
06236 
06237   /* Copy the list of befriending classes.  */
06238   for (friends = &DECL_BEFRIENDING_CLASSES (r);
06239        *friends;
06240        friends = &TREE_CHAIN (*friends)) 
06241     {
06242       *friends = copy_node (*friends);
06243       TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
06244               args, complain,
06245               in_decl);
06246     }
06247 
06248   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
06249     {
06250       maybe_retrofit_in_chrg (r);
06251       if (DECL_CONSTRUCTOR_P (r))
06252         grok_ctor_properties (ctx, r);
06253       /* If this is an instantiation of a member template, clone it.
06254          If it isn't, that'll be handled by
06255          clone_constructors_and_destructors.  */
06256       if (PRIMARY_TEMPLATE_P (gen_tmpl))
06257         clone_function_decl (r, /*update_method_vec_p=*/0);
06258     }
06259   else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
06260     grok_op_properties (r, DECL_FRIEND_P (r));
06261       }
06262       break;
06263 
06264     case PARM_DECL:
06265       {
06266   r = copy_node (t);
06267   if (DECL_TEMPLATE_PARM_P (t))
06268     SET_DECL_TEMPLATE_PARM_P (r);
06269 
06270   TREE_TYPE (r) = type;
06271   c_apply_type_quals_to_decl (cp_type_quals (type), r);
06272 
06273   if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
06274     DECL_INITIAL (r) = TREE_TYPE (r);
06275   else
06276     DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
06277              complain, in_decl);
06278 
06279   DECL_CONTEXT (r) = NULL_TREE;
06280 
06281   if (!DECL_TEMPLATE_PARM_P (r))
06282     DECL_ARG_TYPE (r) = type_passed_as (type);
06283   if (TREE_CHAIN (t))
06284     TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
06285            complain, TREE_CHAIN (t));
06286       }
06287       break;
06288 
06289     case FIELD_DECL:
06290       {
06291   r = copy_decl (t);
06292   TREE_TYPE (r) = type;
06293   c_apply_type_quals_to_decl (cp_type_quals (type), r);
06294 
06295   /* We don't have to set DECL_CONTEXT here; it is set by
06296      finish_member_declaration.  */
06297   DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
06298           complain, in_decl);
06299   TREE_CHAIN (r) = NULL_TREE;
06300   if (VOID_TYPE_P (type)) 
06301     cp_error_at ("instantiation of `%D' as type `%T'", r, type);
06302       }
06303       break;
06304 
06305     case USING_DECL:
06306       {
06307   r = copy_node (t);
06308   DECL_INITIAL (r)
06309     = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
06310   TREE_CHAIN (r) = NULL_TREE;
06311       }
06312       break;
06313 
06314     case TYPE_DECL:
06315       if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
06316     || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
06317   {
06318     /* If this is the canonical decl, we don't have to mess with
06319              instantiations, and often we can't (for typename, template
06320        type parms and such).  Note that TYPE_NAME is not correct for
06321        the above test if we've copied the type for a typedef.  */
06322     r = TYPE_NAME (type);
06323     break;
06324   }
06325 
06326       /* Fall through.  */
06327 
06328     case VAR_DECL:
06329       {
06330   tree argvec = NULL_TREE;
06331   tree gen_tmpl = NULL_TREE;
06332   tree spec;
06333   tree tmpl = NULL_TREE;
06334   tree ctx;
06335   int local_p;
06336 
06337   /* Assume this is a non-local variable.  */
06338   local_p = 0;
06339 
06340   if (TYPE_P (CP_DECL_CONTEXT (t)))
06341     ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 
06342           complain,
06343           in_decl, /*entering_scope=*/1);
06344   else if (DECL_NAMESPACE_SCOPE_P (t))
06345     ctx = DECL_CONTEXT (t);
06346   else
06347     {
06348       /* Subsequent calls to pushdecl will fill this in.  */
06349       ctx = NULL_TREE;
06350       local_p = 1;
06351     }
06352 
06353   /* Check to see if we already have this specialization.  */
06354   if (!local_p)
06355     {
06356       tmpl = DECL_TI_TEMPLATE (t);
06357       gen_tmpl = most_general_template (tmpl);
06358       argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
06359       spec = retrieve_specialization (gen_tmpl, argvec);
06360     }
06361   else
06362     spec = retrieve_local_specialization (t);
06363 
06364   if (spec)
06365     {
06366       r = spec;
06367       break;
06368     }
06369 
06370   r = copy_decl (t);
06371   if (TREE_CODE (r) == VAR_DECL)
06372     type = complete_type (type);
06373   else if (DECL_SELF_REFERENCE_P (t))
06374     SET_DECL_SELF_REFERENCE_P (r);
06375   TREE_TYPE (r) = type;
06376   c_apply_type_quals_to_decl (cp_type_quals (type), r);
06377   DECL_CONTEXT (r) = ctx;
06378   /* Clear out the mangled name and RTL for the instantiation.  */
06379   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
06380   SET_DECL_RTL (r, NULL_RTX);
06381 
06382   /* Don't try to expand the initializer until someone tries to use
06383      this variable; otherwise we run into circular dependencies.  */
06384   DECL_INITIAL (r) = NULL_TREE;
06385   SET_DECL_RTL (r, NULL_RTX);
06386   DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
06387 
06388   /* Even if the original location is out of scope, the newly
06389      substituted one is not.  */
06390   if (TREE_CODE (r) == VAR_DECL)
06391     {
06392       DECL_DEAD_FOR_LOCAL (r) = 0;
06393       DECL_INITIALIZED_P (r) = 0;
06394     }
06395 
06396   if (!local_p)
06397     {
06398       /* A static data member declaration is always marked
06399          external when it is declared in-class, even if an
06400          initializer is present.  We mimic the non-template
06401          processing here.  */
06402       DECL_EXTERNAL (r) = 1;
06403 
06404       register_specialization (r, gen_tmpl, argvec);
06405       DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
06406       SET_DECL_IMPLICIT_INSTANTIATION (r);
06407     }
06408   else
06409     register_local_specialization (r, t);
06410 
06411   TREE_CHAIN (r) = NULL_TREE;
06412   if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
06413     cp_error_at ("instantiation of `%D' as type `%T'", r, type);
06414   /* Compute the size, alignment, etc. of R.  */
06415   layout_decl (r, 0);
06416       }
06417       break;
06418 
06419     default:
06420       abort ();
06421     } 
06422 
06423   /* Restore the file and line information.  */
06424   lineno = saved_lineno;
06425   input_filename = saved_filename;
06426 
06427   return r;
06428 }
06429 
06430 /* Substitue into the ARG_TYPES of a function type.  */
06431 
06432 static tree
06433 tsubst_arg_types (arg_types, args, complain, in_decl)
06434      tree arg_types;
06435      tree args;
06436      tsubst_flags_t complain;
06437      tree in_decl;
06438 {
06439   tree remaining_arg_types;
06440   tree type;
06441 
06442   if (!arg_types || arg_types == void_list_node)
06443     return arg_types;
06444   
06445   remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
06446             args, complain, in_decl);
06447   if (remaining_arg_types == error_mark_node)
06448     return error_mark_node;
06449 
06450   type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
06451   if (type == error_mark_node)
06452     return error_mark_node;
06453   if (VOID_TYPE_P (type))
06454     {
06455       if (complain & tf_error)
06456         {
06457           error ("invalid parameter type `%T'", type);
06458           if (in_decl)
06459             cp_error_at ("in declaration `%D'", in_decl);
06460         }
06461       return error_mark_node;
06462     }
06463 
06464   /* Do array-to-pointer, function-to-pointer conversion, and ignore
06465      top-level qualifiers as required.  */
06466   type = TYPE_MAIN_VARIANT (type_decays_to (type));
06467 
06468   /* Note that we do not substitute into default arguments here.  The
06469      standard mandates that they be instantiated only when needed,
06470      which is done in build_over_call.  */
06471   return hash_tree_cons (TREE_PURPOSE (arg_types), type,
06472        remaining_arg_types);
06473        
06474 }
06475 
06476 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
06477    *not* handle the exception-specification for FNTYPE, because the
06478    initial substitution of explicitly provided template parameters
06479    during argument deduction forbids substitution into the
06480    exception-specification:
06481 
06482      [temp.deduct]
06483 
06484      All references in the function type of the function template to  the
06485      corresponding template parameters are replaced by the specified tem-
06486      plate argument values.  If a substitution in a template parameter or
06487      in  the function type of the function template results in an invalid
06488      type, type deduction fails.  [Note: The equivalent  substitution  in
06489      exception specifications is done only when the function is instanti-
06490      ated, at which point a program is  ill-formed  if  the  substitution
06491      results in an invalid type.]  */
06492 
06493 static tree
06494 tsubst_function_type (t, args, complain, in_decl)
06495      tree t;
06496      tree args;
06497      tsubst_flags_t complain;
06498      tree in_decl;
06499 {
06500   tree return_type;
06501   tree arg_types;
06502   tree fntype;
06503 
06504   /* The TYPE_CONTEXT is not used for function/method types.  */
06505   my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
06506 
06507   /* Substitute the return type.  */
06508   return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
06509   if (return_type == error_mark_node)
06510     return error_mark_node;
06511 
06512   /* Substitue the argument types.  */
06513   arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
06514         complain, in_decl); 
06515   if (arg_types == error_mark_node)
06516     return error_mark_node;
06517   
06518   /* Construct a new type node and return it.  */
06519   if (TREE_CODE (t) == FUNCTION_TYPE)
06520     fntype = build_function_type (return_type, arg_types);
06521   else
06522     {
06523       tree r = TREE_TYPE (TREE_VALUE (arg_types));
06524       if (! IS_AGGR_TYPE (r))
06525   {
06526     /* [temp.deduct]
06527        
06528        Type deduction may fail for any of the following
06529        reasons:
06530        
06531        -- Attempting to create "pointer to member of T" when T
06532        is not a class type.  */
06533     if (complain & tf_error)
06534       error ("creating pointer to member function of non-class type `%T'",
06535           r);
06536     return error_mark_node;
06537   }
06538       
06539       fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
06540           (arg_types));
06541     }
06542   fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
06543   fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
06544   
06545   return fntype;  
06546 }
06547 
06548 /* Substitute into the PARMS of a call-declarator.  */
06549 
06550 static tree
06551 tsubst_call_declarator_parms (parms, args, complain, in_decl)
06552      tree parms;
06553      tree args;
06554      tsubst_flags_t complain;
06555      tree in_decl;
06556 {
06557   tree new_parms;
06558   tree type;
06559   tree defarg;
06560 
06561   if (!parms || parms == void_list_node)
06562     return parms;
06563   
06564   new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
06565               args, complain, in_decl);
06566 
06567   /* Figure out the type of this parameter.  */
06568   type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
06569   
06570   /* Figure out the default argument as well.  Note that we use
06571      tsubst_expr since the default argument is really an expression.  */
06572   defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
06573 
06574   /* Chain this parameter on to the front of those we have already
06575      processed.  We don't use hash_tree_cons because that function
06576      doesn't check TREE_PARMLIST.  */
06577   new_parms = tree_cons (defarg, type, new_parms);
06578 
06579   /* And note that these are parameters.  */
06580   TREE_PARMLIST (new_parms) = 1;
06581   
06582   return new_parms;
06583 }
06584 
06585 #ifdef KEY
06586 extern void template_substituted (tree, tree, tree);
06587 extern int flag_openmp;
06588 #endif /* KEY */
06589 
06590 /* Take the tree structure T and replace template parameters used
06591    therein with the argument vector ARGS.  IN_DECL is an associated
06592    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
06593    Issue error and warning messages under control of COMPLAIN.  Note
06594    that we must be relatively non-tolerant of extensions here, in
06595    order to preserve conformance; if we allow substitutions that
06596    should not be allowed, we may allow argument deductions that should
06597    not succeed, and therefore report ambiguous overload situations
06598    where there are none.  In theory, we could allow the substitution,
06599    but indicate that it should have failed, and allow our caller to
06600    make sure that the right thing happens, but we don't try to do this
06601    yet.
06602 
06603    This function is used for dealing with types, decls and the like;
06604    for expressions, use tsubst_expr or tsubst_copy.  */
06605 
06606 tree
06607 tsubst (t, args, complain, in_decl)
06608      tree t, args;
06609      tsubst_flags_t complain;
06610      tree in_decl;
06611 {
06612   tree type, r;
06613 #ifdef KEY
06614   tree new_t;
06615 #endif
06616 
06617   if (t == NULL_TREE || t == error_mark_node
06618       || t == integer_type_node
06619       || t == void_type_node
06620       || t == char_type_node
06621       || TREE_CODE (t) == NAMESPACE_DECL)
06622     return t;
06623 
06624   if (TREE_CODE (t) == IDENTIFIER_NODE)
06625     type = IDENTIFIER_TYPE_VALUE (t);
06626   else
06627     type = TREE_TYPE (t);
06628   if (type == unknown_type_node)
06629     abort ();
06630 
06631   if (type && TREE_CODE (t) != FUNCTION_DECL
06632       && TREE_CODE (t) != TYPENAME_TYPE
06633       && TREE_CODE (t) != TEMPLATE_DECL
06634       && TREE_CODE (t) != IDENTIFIER_NODE
06635       && TREE_CODE (t) != FUNCTION_TYPE
06636       && TREE_CODE (t) != METHOD_TYPE)
06637     type = tsubst (type, args, complain, in_decl);
06638   if (type == error_mark_node)
06639     return error_mark_node;
06640 
06641   if (DECL_P (t))
06642   {
06643 #ifdef KEY
06644     new_t = tsubst_decl (t, args, type, complain);
06645     /* For now do it for var- and parm-decls when -mp is passed */
06646     if (flag_openmp)
06647     {
06648       // If it is a parm-decl, wait for it to be regenerated. See comments
06649       // for regenerate_decl_from_template
06650       if ((TREE_CODE (t) == VAR_DECL) ||
06651           (TREE_CODE (t) == PARM_DECL && regenerating_decl_from_template))
06652         template_substituted (t, new_t, current_function_decl);
06653     }
06654     return new_t;
06655 #else
06656     return tsubst_decl (t, args, type, complain);
06657 #endif /* KEY */
06658   }
06659 
06660   switch (TREE_CODE (t))
06661     {
06662     case RECORD_TYPE:
06663     case UNION_TYPE:
06664     case ENUMERAL_TYPE:
06665       return tsubst_aggr_type (t, args, complain, in_decl,
06666              /*entering_scope=*/0);
06667 
06668     case ERROR_MARK:
06669     case IDENTIFIER_NODE:
06670     case VOID_TYPE:
06671     case REAL_TYPE:
06672     case COMPLEX_TYPE:
06673     case VECTOR_TYPE:
06674     case BOOLEAN_TYPE:
06675     case INTEGER_CST:
06676     case REAL_CST:
06677     case STRING_CST:
06678       return t;
06679 
06680     case INTEGER_TYPE:
06681       if (t == integer_type_node)
06682   return t;
06683 
06684       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
06685     && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
06686   return t;
06687 
06688       {
06689   tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
06690 
06691   max = tsubst_expr (omax, args, complain, in_decl);
06692   if (max == error_mark_node)
06693     return error_mark_node;
06694 
06695   /* See if we can reduce this expression to something simpler.  */
06696   max = maybe_fold_nontype_arg (max);
06697   if (!processing_template_decl)
06698     max = decl_constant_value (max);
06699 
06700   if (processing_template_decl 
06701       /* When providing explicit arguments to a template
06702          function, but leaving some arguments for subsequent
06703          deduction, MAX may be template-dependent even if we're
06704          not PROCESSING_TEMPLATE_DECL.  We still need to check for
06705          template parms, though; MAX won't be an INTEGER_CST for
06706          dynamic arrays, either.  */
06707       || (TREE_CODE (max) != INTEGER_CST
06708     && uses_template_parms (max)))
06709     {
06710       tree itype = make_node (INTEGER_TYPE);
06711       TYPE_MIN_VALUE (itype) = size_zero_node;
06712       TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
06713             integer_one_node);
06714       return itype;
06715     }
06716 
06717   if (integer_zerop (omax))
06718     {
06719       /* Still allow an explicit array of size zero.  */
06720       if (pedantic)
06721         pedwarn ("creating array with size zero");
06722     }
06723   else if (integer_zerop (max) 
06724      || (TREE_CODE (max) == INTEGER_CST 
06725          && INT_CST_LT (max, integer_zero_node)))
06726     {
06727       /* [temp.deduct]
06728 
06729          Type deduction may fail for any of the following
06730          reasons:  
06731 
06732      Attempting to create an array with a size that is
06733      zero or negative.  */
06734       if (complain & tf_error)
06735         error ("creating array with size zero (`%E')", max);
06736 
06737       return error_mark_node;
06738     }
06739 
06740   return compute_array_index_type (NULL_TREE, max);
06741       }
06742 
06743     case TEMPLATE_TYPE_PARM:
06744     case TEMPLATE_TEMPLATE_PARM:
06745     case BOUND_TEMPLATE_TEMPLATE_PARM:
06746     case TEMPLATE_PARM_INDEX:
06747       {
06748   int idx;
06749   int level;
06750   int levels;
06751 
06752   r = NULL_TREE;
06753 
06754   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
06755       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
06756       || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
06757     {
06758       idx = TEMPLATE_TYPE_IDX (t);
06759       level = TEMPLATE_TYPE_LEVEL (t);
06760     }
06761   else
06762     {
06763       idx = TEMPLATE_PARM_IDX (t);
06764       level = TEMPLATE_PARM_LEVEL (t);
06765     }
06766 
06767   if (TREE_VEC_LENGTH (args) > 0)
06768     {
06769       tree arg = NULL_TREE;
06770 
06771       levels = TMPL_ARGS_DEPTH (args);
06772       if (level <= levels)
06773         arg = TMPL_ARG (args, level, idx);
06774 
06775       if (arg == error_mark_node)
06776         return error_mark_node;
06777       else if (arg != NULL_TREE)
06778         {
06779     if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
06780       {
06781         my_friendly_assert (TYPE_P (arg), 0);
06782         return cp_build_qualified_type_real
06783           (arg, cp_type_quals (arg) | cp_type_quals (t),
06784            complain | tf_ignore_bad_quals);
06785       }
06786     else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
06787       {
06788         /* We are processing a type constructed from
06789            a template template parameter */
06790         tree argvec = tsubst (TYPE_TI_ARGS (t),
06791             args, complain, in_decl);
06792         if (argvec == error_mark_node)
06793           return error_mark_node;
06794       
06795         /* We can get a TEMPLATE_TEMPLATE_PARM here when 
06796            we are resolving nested-types in the signature of 
06797            a member function templates.
06798            Otherwise ARG is a TEMPLATE_DECL and is the real 
06799            template to be instantiated.  */
06800         if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
06801           arg = TYPE_NAME (arg);
06802 
06803         r = lookup_template_class (arg, 
06804                  argvec, in_decl, 
06805                  DECL_CONTEXT (arg),
06806                  /*entering_scope=*/0,
06807                                          complain);
06808         return cp_build_qualified_type_real
06809           (r, TYPE_QUALS (t), complain);
06810       }
06811     else
06812       /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
06813       return arg;
06814         }
06815     }
06816   else
06817     abort ();
06818 
06819   if (level == 1)
06820     /* This can happen during the attempted tsubst'ing in
06821        unify.  This means that we don't yet have any information
06822        about the template parameter in question.  */
06823     return t;
06824 
06825   /* If we get here, we must have been looking at a parm for a
06826      more deeply nested template.  Make a new version of this
06827      template parameter, but with a lower level.  */
06828   switch (TREE_CODE (t))
06829     {
06830     case TEMPLATE_TYPE_PARM:
06831     case TEMPLATE_TEMPLATE_PARM:
06832     case BOUND_TEMPLATE_TEMPLATE_PARM:
06833       if (cp_type_quals (t))
06834         {
06835     r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
06836     r = cp_build_qualified_type_real
06837       (r, cp_type_quals (t),
06838        complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
06839              ? tf_ignore_bad_quals : 0));
06840         }
06841       else
06842         {
06843     r = copy_type (t);
06844     TEMPLATE_TYPE_PARM_INDEX (r)
06845       = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
06846             r, levels);
06847     TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
06848     TYPE_MAIN_VARIANT (r) = r;
06849     TYPE_POINTER_TO (r) = NULL_TREE;
06850     TYPE_REFERENCE_TO (r) = NULL_TREE;
06851 
06852     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
06853       {
06854         tree argvec = tsubst (TYPE_TI_ARGS (t), args,
06855             complain, in_decl); 
06856         if (argvec == error_mark_node)
06857           return error_mark_node;
06858 
06859         TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
06860           = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
06861       }
06862         }
06863       break;
06864 
06865     case TEMPLATE_PARM_INDEX:
06866       r = reduce_template_parm_level (t, type, levels);
06867       break;
06868      
06869     default:
06870       abort ();
06871     }
06872 
06873   return r;
06874       }
06875 
06876     case TREE_LIST:
06877       {
06878   tree purpose, value, chain, result;
06879 
06880   if (t == void_list_node)
06881     return t;
06882 
06883   purpose = TREE_PURPOSE (t);
06884   if (purpose)
06885     {
06886       purpose = tsubst (purpose, args, complain, in_decl);
06887       if (purpose == error_mark_node)
06888         return error_mark_node;
06889     }
06890   value = TREE_VALUE (t);
06891   if (value)
06892     {
06893       value = tsubst (value, args, complain, in_decl);
06894       if (value == error_mark_node)
06895         return error_mark_node;
06896     }
06897   chain = TREE_CHAIN (t);
06898   if (chain && chain != void_type_node)
06899     {
06900       chain = tsubst (chain, args, complain, in_decl);
06901       if (chain == error_mark_node)
06902         return error_mark_node;
06903     }
06904   if (purpose == TREE_PURPOSE (t)
06905       && value == TREE_VALUE (t)
06906       && chain == TREE_CHAIN (t))
06907     return t;
06908   if (TREE_PARMLIST (t))
06909     {
06910       result = tree_cons (purpose, value, chain);
06911       TREE_PARMLIST (result) = 1;
06912     }
06913   else
06914     result = hash_tree_cons (purpose, value, chain);
06915   return result;
06916       }
06917     case TREE_VEC:
06918       if (type != NULL_TREE)
06919   {
06920     /* A binfo node.  We always need to make a copy, of the node
06921        itself and of its BINFO_BASETYPES.  */
06922 
06923     t = copy_node (t);
06924 
06925     /* Make sure type isn't a typedef copy.  */
06926     type = BINFO_TYPE (TYPE_BINFO (type));
06927 
06928     TREE_TYPE (t) = complete_type (type);
06929     if (IS_AGGR_TYPE (type))
06930       {
06931         BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
06932         BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
06933         if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
06934     BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
06935       }
06936     return t;
06937   }
06938 
06939       /* Otherwise, a vector of template arguments.  */
06940       return tsubst_template_arg_vector (t, args, complain);
06941 
06942     case POINTER_TYPE:
06943     case REFERENCE_TYPE:
06944       {
06945   enum tree_code code;
06946 
06947   if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
06948     return t;
06949 
06950   code = TREE_CODE (t);
06951 
06952 
06953   /* [temp.deduct]
06954      
06955      Type deduction may fail for any of the following
06956      reasons:  
06957 
06958      -- Attempting to create a pointer to reference type.
06959      -- Attempting to create a reference to a reference type or
06960         a reference to void.  */
06961   if (TREE_CODE (type) == REFERENCE_TYPE
06962       || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
06963     {
06964       static int   last_line = 0;
06965       static const char* last_file = 0;
06966 
06967       /* We keep track of the last time we issued this error
06968          message to avoid spewing a ton of messages during a
06969          single bad template instantiation.  */
06970       if (complain & tf_error
06971     && (last_line != lineno || last_file != input_filename))
06972         {
06973     if (TREE_CODE (type) == VOID_TYPE)
06974       error ("forming reference to void");
06975     else
06976       error ("forming %s to reference type `%T'",
06977           (code == POINTER_TYPE) ? "pointer" : "reference",
06978           type);
06979     last_line = lineno;
06980     last_file = input_filename;
06981         }
06982 
06983       return error_mark_node;
06984     }
06985   else if (code == POINTER_TYPE)
06986     {
06987       r = build_pointer_type (type);
06988       if (TREE_CODE (type) == METHOD_TYPE)
06989         r = build_ptrmemfunc_type (r);
06990     }
06991   else
06992     r = build_reference_type (type);
06993   r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
06994 
06995   if (r != error_mark_node)
06996     /* Will this ever be needed for TYPE_..._TO values?  */
06997     layout_type (r);
06998   
06999   return r;
07000       }
07001     case OFFSET_TYPE:
07002       {
07003   r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
07004   if (r == error_mark_node || !IS_AGGR_TYPE (r))
07005     {
07006       /* [temp.deduct]
07007 
07008          Type deduction may fail for any of the following
07009          reasons:
07010          
07011          -- Attempting to create "pointer to member of T" when T
07012             is not a class type.  */
07013       if (complain & tf_error)
07014         error ("creating pointer to member of non-class type `%T'", r);
07015       return error_mark_node;
07016     }
07017   if (TREE_CODE (type) == REFERENCE_TYPE)
07018     {
07019       if (complain & tf_error)
07020         error ("creating pointer to member reference type `%T'", type);
07021       
07022       return error_mark_node;
07023     }
07024   my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
07025   if (TREE_CODE (type) == FUNCTION_TYPE)
07026     /* This is really a method type. The cv qualifiers of the
07027        this pointer should _not_ be determined by the cv
07028        qualifiers of the class type.  They should be held
07029        somewhere in the FUNCTION_TYPE, but we don't do that at
07030        the moment.  Consider
07031           typedef void (Func) () const;
07032 
07033     template <typename T1> void Foo (Func T1::*);
07034 
07035         */
07036     return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
07037             TREE_TYPE (type),
07038             TYPE_ARG_TYPES (type));
07039   else
07040     return build_offset_type (r, type);
07041       }
07042     case FUNCTION_TYPE:
07043     case METHOD_TYPE:
07044       {
07045   tree fntype;
07046   tree raises;
07047 
07048   fntype = tsubst_function_type (t, args, complain, in_decl);
07049   if (fntype == error_mark_node)
07050     return error_mark_node;
07051 
07052   /* Substitue the exception specification.  */
07053   raises = TYPE_RAISES_EXCEPTIONS (t);
07054   if (raises)
07055     {
07056       tree   list = NULL_TREE;
07057       
07058       if (! TREE_VALUE (raises))
07059         list = raises;
07060       else
07061         for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
07062           {
07063             tree spec = TREE_VALUE (raises);
07064             
07065             spec = tsubst (spec, args, complain, in_decl);
07066             if (spec == error_mark_node)
07067               return spec;
07068             list = add_exception_specifier (list, spec, complain);
07069           }
07070       fntype = build_exception_variant (fntype, list);
07071     }
07072   return fntype;
07073       }
07074     case ARRAY_TYPE:
07075       {
07076   tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
07077   if (domain == error_mark_node)
07078     return error_mark_node;
07079 
07080   /* As an optimization, we avoid regenerating the array type if
07081      it will obviously be the same as T.  */
07082   if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
07083     return t;
07084 
07085   /* These checks should match the ones in grokdeclarator.  
07086 
07087      [temp.deduct] 
07088   
07089      The deduction may fail for any of the following reasons: 
07090 
07091      -- Attempting to create an array with an element type that
07092         is void, a function type, or a reference type.  */
07093   if (TREE_CODE (type) == VOID_TYPE 
07094       || TREE_CODE (type) == FUNCTION_TYPE
07095       || TREE_CODE (type) == REFERENCE_TYPE)
07096     {
07097       if (complain & tf_error)
07098         error ("creating array of `%T'", type);
07099       return error_mark_node;
07100     }
07101 
07102   r = build_cplus_array_type (type, domain);
07103   return r;
07104       }
07105 
07106     case PLUS_EXPR:
07107     case MINUS_EXPR:
07108       {
07109   tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07110   tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
07111 
07112   if (e1 == error_mark_node || e2 == error_mark_node)
07113     return error_mark_node;
07114 
07115   return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
07116       }
07117 
07118     case NEGATE_EXPR:
07119     case NOP_EXPR:
07120       {
07121   tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07122   if (e == error_mark_node)
07123     return error_mark_node;
07124 
07125   return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
07126       }
07127 
07128     case TYPENAME_TYPE:
07129       {
07130   tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
07131              in_decl, /*entering_scope=*/1);
07132   tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
07133             complain, in_decl); 
07134 
07135   if (ctx == error_mark_node || f == error_mark_node)
07136     return error_mark_node;
07137 
07138   if (!IS_AGGR_TYPE (ctx))
07139     {
07140       if (complain & tf_error)
07141         error ("`%T' is not a class, struct, or union type",
07142       ctx);
07143       return error_mark_node;
07144     }
07145   else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
07146     {
07147       /* Normally, make_typename_type does not require that the CTX
07148          have complete type in order to allow things like:
07149        
07150            template <class T> struct S { typename S<T>::X Y; };
07151 
07152          But, such constructs have already been resolved by this
07153          point, so here CTX really should have complete type, unless
07154          it's a partial instantiation.  */
07155       ctx = complete_type (ctx);
07156       if (!COMPLETE_TYPE_P (ctx))
07157         {
07158     if (complain & tf_error)
07159       cxx_incomplete_type_error (NULL_TREE, ctx);
07160     return error_mark_node;
07161         }
07162     }
07163 
07164   f = make_typename_type (ctx, f,
07165         (complain & tf_error) | tf_keep_type_decl);
07166   if (f == error_mark_node)
07167     return f;
07168   if (TREE_CODE (f) == TYPE_DECL)
07169     {
07170       complain |= tf_ignore_bad_quals;
07171       f = TREE_TYPE (f);
07172     }
07173   
07174   return cp_build_qualified_type_real
07175     (f, cp_type_quals (f) | cp_type_quals (t), complain);
07176       }
07177          
07178     case UNBOUND_CLASS_TEMPLATE:
07179       {
07180   tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
07181              in_decl, /*entering_scope=*/1);
07182   tree name = TYPE_IDENTIFIER (t);
07183 
07184   if (ctx == error_mark_node || name == error_mark_node)
07185     return error_mark_node;
07186 
07187   return make_unbound_class_template (ctx, name, complain);
07188       }
07189 
07190     case INDIRECT_REF:
07191       {
07192   tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07193   if (e == error_mark_node)
07194     return error_mark_node;
07195   return make_pointer_declarator (type, e);
07196       }
07197 
07198     case ADDR_EXPR:
07199       {
07200   tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07201   if (e == error_mark_node)
07202     return error_mark_node;
07203   return make_reference_declarator (type, e);
07204       }
07205 
07206     case ARRAY_REF:
07207       {
07208   tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07209   tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
07210   if (e1 == error_mark_node || e2 == error_mark_node)
07211     return error_mark_node;
07212 
07213   return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
07214       }
07215 
07216     case CALL_EXPR:
07217       {
07218   tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07219   tree e2 = (tsubst_call_declarator_parms
07220        (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
07221   tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
07222         complain, in_decl);
07223 
07224   if (e1 == error_mark_node || e2 == error_mark_node 
07225       || e3 == error_mark_node)
07226     return error_mark_node;
07227 
07228   return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
07229       }
07230 
07231     case SCOPE_REF:
07232       {
07233   tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
07234   tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
07235   if (e1 == error_mark_node || e2 == error_mark_node)
07236     return error_mark_node;
07237 
07238   return build_nt (TREE_CODE (t), e1, e2);
07239       }
07240 
07241     case TYPEOF_TYPE:
07242       {
07243   tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
07244   if (e1 == error_mark_node)
07245     return error_mark_node;
07246 
07247   return cp_build_qualified_type_real (TREE_TYPE (e1),
07248                cp_type_quals (t)
07249                | cp_type_quals (TREE_TYPE (e1)),
07250                complain);
07251       }
07252 
07253     default:
07254       sorry ("use of `%s' in template",
07255        tree_code_name [(int) TREE_CODE (t)]);
07256       return error_mark_node;
07257     }
07258 }
07259 
07260 /* Like tsubst, but deals with expressions.  This function just replaces
07261    template parms; to finish processing the resultant expression, use
07262    tsubst_expr.  */
07263 
07264 tree
07265 tsubst_copy (t, args, complain, in_decl)
07266      tree t, args;
07267      tsubst_flags_t complain;
07268      tree in_decl;
07269 {
07270   enum tree_code code;
07271   tree r;
07272 
07273   if (t == NULL_TREE || t == error_mark_node)
07274     return t;
07275 
07276   code = TREE_CODE (t);
07277 
07278   switch (code)
07279     {
07280     case PARM_DECL:
07281       return do_identifier (DECL_NAME (t), 0, NULL_TREE);
07282 
07283     case CONST_DECL:
07284       {
07285   tree enum_type;
07286   tree v;
07287 
07288   if (!DECL_CONTEXT (t))
07289     /* This is a global enumeration constant.  */
07290     return t;
07291 
07292   /* Unfortunately, we cannot just call lookup_name here.
07293      Consider:
07294      
07295        template <int I> int f() {
07296        enum E { a = I };
07297        struct S { void g() { E e = a; } };
07298        };
07299      
07300      When we instantiate f<7>::S::g(), say, lookup_name is not
07301      clever enough to find f<7>::a.  */
07302   enum_type 
07303     = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, 
07304             /*entering_scope=*/0);
07305 
07306   for (v = TYPE_VALUES (enum_type); 
07307        v != NULL_TREE; 
07308        v = TREE_CHAIN (v))
07309     if (TREE_PURPOSE (v) == DECL_NAME (t))
07310       return TREE_VALUE (v);
07311 
07312     /* We didn't find the name.  That should never happen; if
07313        name-lookup found it during preliminary parsing, we
07314        should find it again here during instantiation.  */
07315   abort ();
07316       }
07317       return t;
07318 
07319     case FIELD_DECL:
07320       if (DECL_CONTEXT (t))
07321   {
07322     tree ctx;
07323 
07324     ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
07325           /*entering_scope=*/1);
07326     if (ctx != DECL_CONTEXT (t))
07327       return lookup_field (ctx, DECL_NAME (t), 0, 0);
07328   }
07329       return t;
07330 
07331     case VAR_DECL:
07332     case FUNCTION_DECL:
07333       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
07334   t = tsubst (t, args, complain, in_decl);
07335       mark_used (t);
07336       return t;
07337 
07338     case TEMPLATE_DECL:
07339       if (is_member_template (t))
07340   return tsubst (t, args, complain, in_decl);
07341       else
07342   return t;
07343 
07344     case LOOKUP_EXPR:
07345       {
07346   /* We must tsubst into a LOOKUP_EXPR in case the names to
07347      which it refers is a conversion operator; in that case the
07348      name will change.  We avoid making unnecessary copies,
07349      however.  */
07350   
07351   tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
07352 
07353   if (id != TREE_OPERAND (t, 0))
07354     {
07355       r = build_nt (LOOKUP_EXPR, id);
07356       LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
07357       t = r;
07358     }
07359 
07360   return t;
07361       }
07362 
07363     case CAST_EXPR:
07364     case REINTERPRET_CAST_EXPR:
07365     case CONST_CAST_EXPR:
07366     case STATIC_CAST_EXPR:
07367     case DYNAMIC_CAST_EXPR:
07368     case NOP_EXPR:
07369       return build1
07370   (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
07371    tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
07372 
07373     case INDIRECT_REF:
07374     case NEGATE_EXPR:
07375     case TRUTH_NOT_EXPR:
07376     case BIT_NOT_EXPR:
07377     case ADDR_EXPR:
07378     case CONVERT_EXPR:      /* Unary + */
07379     case SIZEOF_EXPR:
07380     case ALIGNOF_EXPR:
07381     case ARROW_EXPR:
07382     case THROW_EXPR:
07383     case TYPEID_EXPR:
07384     case REALPART_EXPR:
07385     case IMAGPART_EXPR:
07386       return build1
07387   (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
07388    tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
07389 
07390     case PLUS_EXPR:
07391     case MINUS_EXPR:
07392     case MULT_EXPR:
07393     case TRUNC_DIV_EXPR:
07394     case CEIL_DIV_EXPR:
07395     case FLOOR_DIV_EXPR:
07396     case ROUND_DIV_EXPR:
07397     case EXACT_DIV_EXPR:
07398     case BIT_AND_EXPR:
07399     case BIT_ANDTC_EXPR:
07400     case BIT_IOR_EXPR:
07401     case BIT_XOR_EXPR:
07402     case TRUNC_MOD_EXPR:
07403     case FLOOR_MOD_EXPR:
07404     case TRUTH_ANDIF_EXPR:
07405     case TRUTH_ORIF_EXPR:
07406     case TRUTH_AND_EXPR:
07407     case TRUTH_OR_EXPR:
07408     case RSHIFT_EXPR:
07409     case LSHIFT_EXPR:
07410     case RROTATE_EXPR:
07411     case LROTATE_EXPR:
07412     case EQ_EXPR:
07413     case NE_EXPR:
07414     case MAX_EXPR:
07415     case MIN_EXPR:
07416     case LE_EXPR:
07417     case GE_EXPR:
07418     case LT_EXPR:
07419     case GT_EXPR:
07420     case COMPONENT_REF:
07421     case ARRAY_REF:
07422     case COMPOUND_EXPR:
07423     case SCOPE_REF:
07424     case DOTSTAR_EXPR:
07425     case MEMBER_REF:
07426     case PREDECREMENT_EXPR:
07427     case PREINCREMENT_EXPR:
07428     case POSTDECREMENT_EXPR:
07429     case POSTINCREMENT_EXPR:
07430       return build_nt
07431   (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07432    tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
07433 
07434     case CALL_EXPR:
07435       {
07436   tree fn = TREE_OPERAND (t, 0);
07437   if (is_overloaded_fn (fn))
07438     fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
07439   else
07440     /* Sometimes FN is a LOOKUP_EXPR.  */
07441     fn = tsubst_copy (fn, args, complain, in_decl);
07442   return build_nt
07443     (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
07444           in_decl),
07445      NULL_TREE);
07446       }
07447 
07448     case METHOD_CALL_EXPR:
07449       {
07450   tree name = TREE_OPERAND (t, 0);
07451   if (TREE_CODE (name) == BIT_NOT_EXPR)
07452     {
07453       name = tsubst_copy (TREE_OPERAND (name, 0), args,
07454         complain, in_decl);
07455       name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
07456     }
07457   else if (TREE_CODE (name) == SCOPE_REF
07458      && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
07459     {
07460       tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
07461              complain, in_decl);
07462       name = TREE_OPERAND (TREE_OPERAND (name, 1), 0);
07463       if (TREE_CODE (name) == TYPE_DECL)
07464         name = TREE_TYPE (name);
07465       name = tsubst_copy (name, args, complain, in_decl);
07466       name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
07467       name = build_nt (SCOPE_REF, base, name);
07468     }
07469   else
07470     name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
07471   return build_nt
07472     (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
07473             complain, in_decl),
07474      tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
07475      NULL_TREE);
07476       }
07477 
07478     case STMT_EXPR:
07479       /* This processing should really occur in tsubst_expr, However,
07480    tsubst_expr does not recurse into expressions, since it
07481    assumes that there aren't any statements inside them.
07482    Instead, it simply calls build_expr_from_tree.  So, we need
07483    to expand the STMT_EXPR here.  */
07484       if (!processing_template_decl)
07485   {
07486     tree stmt_expr = begin_stmt_expr ();
07487     tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
07488     return finish_stmt_expr (stmt_expr);
07489   }
07490       
07491       return t;
07492 
07493     case COND_EXPR:
07494     case MODOP_EXPR:
07495     case PSEUDO_DTOR_EXPR:
07496       {
07497   r = build_nt
07498     (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07499      tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
07500      tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
07501   return r;
07502       }
07503 
07504     case NEW_EXPR:
07505       {
07506   r = build_nt
07507   (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07508    tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
07509    tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
07510   NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
07511   return r;
07512       }
07513 
07514     case DELETE_EXPR:
07515       {
07516   r = build_nt
07517   (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
07518    tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
07519   DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
07520   DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
07521   return r;
07522       }
07523 
07524     case TEMPLATE_ID_EXPR:
07525       {
07526         /* Substituted template arguments */
07527   tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
07528           in_decl);
07529 
07530   if (targs && TREE_CODE (targs) == TREE_LIST)
07531     {
07532       tree chain;
07533       for (chain = targs; chain; chain = TREE_CHAIN (chain))
07534         TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
07535     }
07536   else if (targs)
07537     {
07538       int i;
07539       for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
07540         TREE_VEC_ELT (targs, i) 
07541     = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
07542     }
07543 
07544   return lookup_template_function
07545     (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
07546       }
07547 
07548     case TREE_LIST:
07549       {
07550   tree purpose, value, chain;
07551 
07552   if (t == void_list_node)
07553     return t;
07554 
07555   purpose = TREE_PURPOSE (t);
07556   if (purpose)
07557     purpose = tsubst_copy (purpose, args, complain, in_decl);
07558   value = TREE_VALUE (t);
07559   if (value)
07560     value = tsubst_copy (value, args, complain, in_decl);
07561   chain = TREE_CHAIN (t);
07562   if (chain && chain != void_type_node)
07563     chain = tsubst_copy (chain, args, complain, in_decl);
07564   if (purpose == TREE_PURPOSE (t)
07565       && value == TREE_VALUE (t)
07566       && chain == TREE_CHAIN (t))
07567     return t;
07568   return tree_cons (purpose, value, chain);
07569       }
07570 
07571     case RECORD_TYPE:
07572     case UNION_TYPE:
07573     case ENUMERAL_TYPE:
07574     case INTEGER_TYPE:
07575     case TEMPLATE_TYPE_PARM:
07576     case TEMPLATE_TEMPLATE_PARM:
07577     case BOUND_TEMPLATE_TEMPLATE_PARM:
07578     case TEMPLATE_PARM_INDEX:
07579     case POINTER_TYPE:
07580     case REFERENCE_TYPE:
07581     case OFFSET_TYPE:
07582     case FUNCTION_TYPE:
07583     case METHOD_TYPE:
07584     case ARRAY_TYPE:
07585     case TYPENAME_TYPE:
07586     case UNBOUND_CLASS_TEMPLATE:
07587     case TYPEOF_TYPE:
07588     case TYPE_DECL:
07589       return tsubst (t, args, complain, in_decl);
07590 
07591     case IDENTIFIER_NODE:
07592       if (IDENTIFIER_TYPENAME_P (t))
07593   {
07594     tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
07595     return mangle_conv_op_name_for_type (new_type);
07596   }
07597       else
07598   return t;
07599 
07600     case CONSTRUCTOR:
07601       {
07602   r = build
07603     (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), 
07604      NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
07605            complain, in_decl));
07606   TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
07607   return r;
07608       }
07609 
07610     case VA_ARG_EXPR:
07611       return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
07612             in_decl),
07613            tsubst (TREE_TYPE (t), args, complain, in_decl));
07614 
07615     default:
07616       return t;
07617     }
07618 }
07619 
07620 /* Like tsubst_copy, but also does semantic processing.  */
07621 
07622 tree
07623 tsubst_expr (t, args, complain, in_decl)
07624      tree t, args;
07625      tsubst_flags_t complain;
07626      tree in_decl;
07627 {
07628   tree stmt, tmp;
07629 
07630   if (t == NULL_TREE || t == error_mark_node)
07631     return t;
07632 
07633   if (processing_template_decl)
07634     return tsubst_copy (t, args, complain, in_decl);
07635 
07636   if (!statement_code_p (TREE_CODE (t)))
07637     return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
07638     
07639   switch (TREE_CODE (t))
07640     {
07641     case RETURN_INIT:
07642       prep_stmt (t);
07643       finish_named_return_value
07644   (TREE_OPERAND (t, 0),
07645    tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl));
07646       break;
07647 
07648     case CTOR_INITIALIZER:
07649       prep_stmt (t);
07650       finish_mem_initializers (tsubst_initializer_list 
07651              (TREE_OPERAND (t, 0), args));
07652       break;
07653 
07654     case RETURN_STMT:
07655       prep_stmt (t);
07656       finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
07657                args, complain, in_decl));
07658       break;
07659 
07660     case EXPR_STMT:
07661       prep_stmt (t);
07662       finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
07663              args, complain, in_decl));
07664       break;
07665 
07666     case USING_STMT:
07667       prep_stmt (t);
07668       do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
07669                args, complain, in_decl));
07670       break;
07671       
07672     case DECL_STMT:
07673       {
07674   tree decl;
07675   tree init;
07676 
07677   prep_stmt (t);
07678   decl = DECL_STMT_DECL (t);
07679   if (TREE_CODE (decl) == LABEL_DECL)
07680     finish_label_decl (DECL_NAME (decl));
07681   else if (TREE_CODE (decl) == USING_DECL)
07682     {
07683       tree scope = DECL_INITIAL (decl);
07684       tree name = DECL_NAME (decl);
07685       
07686       scope = tsubst_expr (scope, args, complain, in_decl);
07687       do_local_using_decl (build_nt (SCOPE_REF, scope, name));
07688     }
07689   else
07690     {
07691       init = DECL_INITIAL (decl);
07692       decl = tsubst (decl, args, complain, in_decl);
07693       if (decl != error_mark_node)
07694         {
07695           if (init)
07696             DECL_INITIAL (decl) = error_mark_node;
07697           /* By marking the declaration as instantiated, we avoid
07698              trying to instantiate it.  Since instantiate_decl can't
07699              handle local variables, and since we've already done
07700              all that needs to be done, that's the right thing to
07701              do.  */
07702           if (TREE_CODE (decl) == VAR_DECL)
07703             DECL_TEMPLATE_INSTANTIATED (decl) = 1;
07704     if (TREE_CODE (decl) == VAR_DECL
07705         && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
07706       /* Anonymous aggregates are a special case.  */
07707       finish_anon_union (decl);
07708     else 
07709       {
07710         maybe_push_decl (decl);
07711         if (DECL_PRETTY_FUNCTION_P (decl))
07712           {
07713       /* For __PRETTY_FUNCTION__ we have to adjust the
07714          initializer.  */
07715       const char *const name
07716         = cxx_printable_name (current_function_decl, 2);
07717       init = cp_fname_init (name);
07718       TREE_TYPE (decl) = TREE_TYPE (init);
07719           }
07720         else
07721           init = tsubst_expr (init, args, complain, in_decl);
07722         cp_finish_decl (decl, init, NULL_TREE, 0);
07723       }
07724         }
07725     }
07726 
07727   /* A DECL_STMT can also be used as an expression, in the condition
07728      clause of an if/for/while construct.  If we aren't followed by
07729      another statement, return our decl.  */
07730   if (TREE_CHAIN (t) == NULL_TREE)
07731     return decl;
07732       }
07733       break;
07734 
07735     case FOR_STMT:
07736       {
07737   prep_stmt (t);
07738 
07739   stmt = begin_for_stmt ();
07740 #ifdef KEY
07741   // Propagate flag to indicate if it is an OpenMP FOR loop and should
07742   // be expanded using WHIRL DO_LOOP.
07743   TREE_ADDRESSABLE (stmt) = TREE_ADDRESSABLE (t);
07744 #endif
07745   tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
07746   finish_for_init_stmt (stmt);
07747   finish_for_cond (tsubst_expr (FOR_COND (t),
07748               args, complain, in_decl),
07749        stmt);
07750   tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
07751   finish_for_expr (tmp, stmt);
07752   tsubst_expr (FOR_BODY (t), args, complain, in_decl);
07753   finish_for_stmt (stmt);
07754       }
07755       break;
07756 
07757     case WHILE_STMT:
07758       {
07759   prep_stmt (t);
07760   stmt = begin_while_stmt ();
07761   finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
07762                args, complain, in_decl),
07763         stmt);
07764   tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
07765   finish_while_stmt (stmt);
07766       }
07767       break;
07768 
07769     case DO_STMT:
07770       {
07771   prep_stmt (t);
07772   stmt = begin_do_stmt ();
07773   tsubst_expr (DO_BODY (t), args, complain, in_decl);
07774   finish_do_body (stmt);
07775   finish_do_stmt (tsubst_expr (DO_COND (t),
07776              args, complain, in_decl),
07777       stmt);
07778       }
07779       break;
07780 
07781     case IF_STMT:
07782       {
07783   prep_stmt (t);
07784   stmt = begin_if_stmt ();
07785   finish_if_stmt_cond (tsubst_expr (IF_COND (t),
07786             args, complain, in_decl),
07787            stmt);
07788 
07789   if (tmp = THEN_CLAUSE (t), tmp)
07790     {
07791       tsubst_expr (tmp, args, complain, in_decl);
07792       finish_then_clause (stmt);
07793     }
07794 
07795   if (tmp = ELSE_CLAUSE (t), tmp)
07796     {
07797       begin_else_clause ();
07798       tsubst_expr (tmp, args, complain, in_decl);
07799       finish_else_clause (stmt);
07800     }
07801 
07802   finish_if_stmt ();
07803       }
07804       break;
07805 
07806     case COMPOUND_STMT:
07807       {
07808   prep_stmt (t);
07809   if (COMPOUND_STMT_BODY_BLOCK (t))
07810     stmt = begin_function_body ();
07811   else
07812     stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
07813 
07814   tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
07815 
07816   if (COMPOUND_STMT_BODY_BLOCK (t))
07817     finish_function_body (stmt);
07818   else
07819     finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
07820       }
07821       break;
07822 
07823     case BREAK_STMT:
07824       prep_stmt (t);
07825       finish_break_stmt ();
07826       break;
07827 
07828     case CONTINUE_STMT:
07829       prep_stmt (t);
07830       finish_continue_stmt ();
07831       break;
07832 
07833     case SWITCH_STMT:
07834       {
07835   tree val;
07836 
07837   prep_stmt (t);
07838   stmt = begin_switch_stmt ();
07839   val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
07840   finish_switch_cond (val, stmt);
07841   tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
07842   finish_switch_stmt (stmt);
07843       }
07844       break;
07845 
07846     case CASE_LABEL:
07847       prep_stmt (t);
07848       finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
07849        tsubst_expr (CASE_HIGH (t), args, complain,
07850               in_decl));
07851       break;
07852 
07853     case LABEL_STMT:
07854       lineno = STMT_LINENO (t);
07855       finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
07856       break;
07857 
07858     case FILE_STMT:
07859       input_filename = FILE_STMT_FILENAME (t);
07860       add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
07861       break;
07862 
07863     case GOTO_STMT:
07864       prep_stmt (t);
07865       tmp = GOTO_DESTINATION (t);
07866       if (TREE_CODE (tmp) != LABEL_DECL)
07867   /* Computed goto's must be tsubst'd into.  On the other hand,
07868      non-computed gotos must not be; the identifier in question
07869      will have no binding.  */
07870   tmp = tsubst_expr (tmp, args, complain, in_decl);
07871       else
07872   tmp = DECL_NAME (tmp);
07873       finish_goto_stmt (tmp);
07874       break;
07875 
07876     case ASM_STMT:
07877       prep_stmt (t);
07878       tmp = finish_asm_stmt
07879   (ASM_CV_QUAL (t),
07880    tsubst_expr (ASM_STRING (t), args, complain, in_decl),
07881    tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
07882    tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), 
07883    tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
07884       ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
07885       break;
07886 
07887     case TRY_BLOCK:
07888       prep_stmt (t);
07889       if (CLEANUP_P (t))
07890   {
07891     stmt = begin_try_block ();
07892     tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
07893     finish_cleanup_try_block (stmt);
07894     finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
07895                complain, in_decl),
07896         stmt);
07897   }
07898       else
07899   {
07900     if (FN_TRY_BLOCK_P (t))
07901       stmt = begin_function_try_block ();
07902     else
07903       stmt = begin_try_block ();
07904 
07905     tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
07906 
07907     if (FN_TRY_BLOCK_P (t))
07908       finish_function_try_block (stmt);
07909     else
07910       finish_try_block (stmt);
07911 
07912     tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
07913     if (FN_TRY_BLOCK_P (t))
07914       finish_function_handler_sequence (stmt);
07915     else
07916       finish_handler_sequence (stmt);
07917   }
07918       break;
07919       
07920     case HANDLER:
07921       {
07922   tree decl;
07923 
07924   prep_stmt (t);
07925   stmt = begin_handler ();
07926   if (HANDLER_PARMS (t))
07927     {
07928       decl = DECL_STMT_DECL (HANDLER_PARMS (t));
07929       decl = tsubst (decl, args, complain, in_decl);
07930       /* Prevent instantiate_decl from trying to instantiate
07931          this variable.  We've already done all that needs to be
07932          done.  */
07933       DECL_TEMPLATE_INSTANTIATED (decl) = 1;
07934     }
07935   else
07936     decl = NULL_TREE;
07937   finish_handler_parms (decl, stmt);
07938   tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
07939   finish_handler (stmt);
07940       }
07941       break;
07942 
07943     case TAG_DEFN:
07944       prep_stmt (t);
07945       tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
07946       break;
07947 
07948 #ifdef KEY
07949     // Build an exact copy of the OMP statement, during template
07950     // instantiation
07951     case OMP_MARKER_STMT:
07952       add_stmt (build_omp_stmt (t->omp.choice, (void *)t->omp.omp_clause_list));
07953       break;
07954 #endif
07955 
07956     default:
07957       abort ();
07958     }
07959 
07960   return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
07961 }
07962 
07963 /* Instantiate the indicated variable or function template TMPL with
07964    the template arguments in TARG_PTR.  */
07965 
07966 tree
07967 instantiate_template (tmpl, targ_ptr)
07968      tree tmpl, targ_ptr;
07969 {
07970   tree fndecl;
07971   tree gen_tmpl;
07972   tree spec;
07973   int i, len;
07974   tree inner_args;
07975 
07976   if (tmpl == error_mark_node)
07977     return error_mark_node;
07978 
07979   my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
07980 
07981   /* If this function is a clone, handle it specially.  */
07982   if (DECL_CLONED_FUNCTION_P (tmpl))
07983     {
07984       tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
07985       tree clone;
07986       
07987       /* Look for the clone.  */
07988       for (clone = TREE_CHAIN (spec);
07989      clone && DECL_CLONED_FUNCTION_P (clone);
07990      clone = TREE_CHAIN (clone))
07991   if (DECL_NAME (clone) == DECL_NAME (tmpl))
07992     return clone;
07993       /* We should always have found the clone by now.  */
07994       abort ();
07995       return NULL_TREE;
07996     }
07997   
07998   /* Check to see if we already have this specialization.  */
07999   spec = retrieve_specialization (tmpl, targ_ptr);
08000   if (spec != NULL_TREE)
08001     return spec;
08002 
08003   gen_tmpl = most_general_template (tmpl);
08004   if (tmpl != gen_tmpl)
08005     {
08006       /* The TMPL is a partial instantiation.  To get a full set of
08007    arguments we must add the arguments used to perform the
08008    partial instantiation.  */
08009       targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
08010                 targ_ptr);
08011 
08012       /* Check to see if we already have this specialization.  */
08013       spec = retrieve_specialization (gen_tmpl, targ_ptr);
08014       if (spec != NULL_TREE)
08015   return spec;
08016     }
08017 
08018   len = DECL_NTPARMS (gen_tmpl);
08019   inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
08020   i = len;
08021   while (i--)
08022     {
08023       tree t = TREE_VEC_ELT (inner_args, i);
08024       if (TYPE_P (t))
08025   {
08026     tree nt = target_type (t);
08027     if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
08028       {
08029         error ("type `%T' composed from a local class is not a valid template-argument", t);
08030         error ("  trying to instantiate `%D'", gen_tmpl);
08031         return error_mark_node;
08032       }
08033   }
08034     }
08035 
08036   /* Make sure that we can see identifiers, and compute access
08037      correctly.  The desired FUNCTION_DECL for FNDECL may or may not be
08038      created earlier.  Let push_access_scope_real figure that out.  */
08039   push_access_scope_real
08040     (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr, 
08041          tf_error, gen_tmpl));
08042 
08043   /* substitute template parameters */
08044   fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
08045        targ_ptr, tf_error, gen_tmpl);
08046 
08047   pop_access_scope (gen_tmpl);
08048 
08049   /* The DECL_TI_TEMPLATE should always be the immediate parent
08050      template, not the most general template.  */
08051   DECL_TI_TEMPLATE (fndecl) = tmpl;
08052 
08053   if (flag_external_templates)
08054     add_pending_template (fndecl);
08055 
08056   /* If we've just instantiated the main entry point for a function,
08057      instantiate all the alternate entry points as well.  We do this
08058      by cloning the instantiation of the main entry point, not by
08059      instantiating the template clones.  */
08060   if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
08061     clone_function_decl (fndecl, /*update_method_vec_p=*/0);
08062 
08063   return fndecl;
08064 }
08065 
08066 /* The FN is a TEMPLATE_DECL for a function.  The ARGS are the
08067    arguments that are being used when calling it.  TARGS is a vector
08068    into which the deduced template arguments are placed.  
08069 
08070    Return zero for success, 2 for an incomplete match that doesn't resolve
08071    all the types, and 1 for complete failure.  An error message will be
08072    printed only for an incomplete match.
08073 
08074    If FN is a conversion operator, or we are trying to produce a specific
08075    specialization, RETURN_TYPE is the return type desired.
08076 
08077    The EXPLICIT_TARGS are explicit template arguments provided via a
08078    template-id.
08079 
08080    The parameter STRICT is one of:
08081 
08082    DEDUCE_CALL: 
08083      We are deducing arguments for a function call, as in
08084      [temp.deduct.call].
08085 
08086    DEDUCE_CONV:
08087      We are deducing arguments for a conversion function, as in 
08088      [temp.deduct.conv].
08089 
08090    DEDUCE_EXACT:
08091      We are deducing arguments when doing an explicit instantiation
08092      as in [temp.explicit], when determining an explicit specialization
08093      as in [temp.expl.spec], or when taking the address of a function
08094      template, as in [temp.deduct.funcaddr]. 
08095 
08096    DEDUCE_ORDER:
08097      We are deducing arguments when calculating the partial
08098      ordering between specializations of function or class
08099      templates, as in [temp.func.order] and [temp.class.order].
08100 
08101    LEN is the number of parms to consider before returning success, or -1
08102    for all.  This is used in partial ordering to avoid comparing parms for
08103    which no actual argument was passed, since they are not considered in
08104    overload resolution (and are explicitly excluded from consideration in
08105    partial ordering in [temp.func.order]/6).  */
08106 
08107 int
08108 fn_type_unification (fn, explicit_targs, targs, args, return_type,
08109          strict, len)
08110      tree fn, explicit_targs, targs, args, return_type;
08111      unification_kind_t strict;
08112      int len;
08113 {
08114   tree parms;
08115   tree fntype;
08116   int result;
08117 
08118   my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
08119 
08120   fntype = TREE_TYPE (fn);
08121   if (explicit_targs)
08122     {
08123       /* [temp.deduct]
08124     
08125    The specified template arguments must match the template
08126    parameters in kind (i.e., type, nontype, template), and there
08127    must not be more arguments than there are parameters;
08128    otherwise type deduction fails.
08129 
08130    Nontype arguments must match the types of the corresponding
08131    nontype template parameters, or must be convertible to the
08132    types of the corresponding nontype parameters as specified in
08133    _temp.arg.nontype_, otherwise type deduction fails.
08134 
08135    All references in the function type of the function template
08136    to the corresponding template parameters are replaced by the
08137    specified template argument values.  If a substitution in a
08138    template parameter or in the function type of the function
08139    template results in an invalid type, type deduction fails.  */
08140       int i;
08141       tree converted_args;
08142 
08143       converted_args
08144   = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), 
08145           explicit_targs, NULL_TREE, tf_none, 
08146           /*require_all_arguments=*/0));
08147       if (converted_args == error_mark_node)
08148   return 1;
08149 
08150       fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
08151       if (fntype == error_mark_node)
08152   return 1;
08153 
08154       /* Place the explicitly specified arguments in TARGS.  */
08155       for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
08156   TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
08157     }
08158      
08159   parms = TYPE_ARG_TYPES (fntype);
08160   /* Never do unification on the 'this' parameter.  */
08161   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
08162     parms = TREE_CHAIN (parms);
08163   
08164   if (return_type)
08165     {
08166       /* We've been given a return type to match, prepend it.  */
08167       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
08168       args = tree_cons (NULL_TREE, return_type, args);
08169       if (len >= 0)
08170   ++len;
08171     }
08172 
08173   /* We allow incomplete unification without an error message here
08174      because the standard doesn't seem to explicitly prohibit it.  Our
08175      callers must be ready to deal with unification failures in any
08176      event.  */
08177   result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), 
08178           targs, parms, args, /*subr=*/0,
08179           strict, /*allow_incomplete*/1, len);
08180 
08181   if (result == 0) 
08182     /* All is well so far.  Now, check:
08183        
08184        [temp.deduct] 
08185        
08186        When all template arguments have been deduced, all uses of
08187        template parameters in nondeduced contexts are replaced with
08188        the corresponding deduced argument values.  If the
08189        substitution results in an invalid type, as described above,
08190        type deduction fails.  */
08191     if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
08192   == error_mark_node)
08193       return 1;
08194 
08195   return result;
08196 }
08197 
08198 /* Adjust types before performing type deduction, as described in
08199    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
08200    sections are symmetric.  PARM is the type of a function parameter
08201    or the return type of the conversion function.  ARG is the type of
08202    the argument passed to the call, or the type of the value
08203    initialized with the result of the conversion function.  */
08204 
08205 static int
08206 maybe_adjust_types_for_deduction (strict, parm, arg)
08207      unification_kind_t strict;
08208      tree* parm;
08209      tree* arg;
08210 {
08211   int result = 0;
08212   
08213   switch (strict)
08214     {
08215     case DEDUCE_CALL:
08216       break;
08217 
08218     case DEDUCE_CONV:
08219       {
08220   /* Swap PARM and ARG throughout the remainder of this
08221      function; the handling is precisely symmetric since PARM
08222      will initialize ARG rather than vice versa.  */
08223   tree* temp = parm;
08224   parm = arg;
08225   arg = temp;
08226   break;
08227       }
08228 
08229     case DEDUCE_EXACT:
08230       /* There is nothing to do in this case.  */
08231       return 0;
08232 
08233     case DEDUCE_ORDER:
08234       /* DR 214. [temp.func.order] is underspecified, and leads to no
08235          ordering between things like `T *' and `T const &' for `U *'.
08236          The former has T=U and the latter T=U*. The former looks more
08237          specialized and John Spicer considers it well-formed (the EDG
08238          compiler accepts it).
08239 
08240          John also confirms that deduction should proceed as in a function
08241          call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
08242          However, in ordering, ARG can have REFERENCE_TYPE, but no argument
08243          to an actual call can have such a type.
08244          
08245          If both ARG and PARM are REFERENCE_TYPE, we change neither.
08246          If only ARG is a REFERENCE_TYPE, we look through that and then
08247          proceed as with DEDUCE_CALL (which could further convert it).  */
08248       if (TREE_CODE (*arg) == REFERENCE_TYPE)
08249         {
08250           if (TREE_CODE (*parm) == REFERENCE_TYPE)
08251             return 0;
08252           *arg = TREE_TYPE (*arg);
08253         }
08254       break;
08255     default:
08256       abort ();
08257     }
08258 
08259   if (TREE_CODE (*parm) != REFERENCE_TYPE)
08260     {
08261       /* [temp.deduct.call]
08262    
08263    If P is not a reference type:
08264    
08265    --If A is an array type, the pointer type produced by the
08266    array-to-pointer standard conversion (_conv.array_) is
08267    used in place of A for type deduction; otherwise,
08268    
08269    --If A is a function type, the pointer type produced by
08270    the function-to-pointer standard conversion
08271    (_conv.func_) is used in place of A for type deduction;
08272    otherwise,
08273    
08274    --If A is a cv-qualified type, the top level
08275    cv-qualifiers of A's type are ignored for type
08276    deduction.  */
08277       if (TREE_CODE (*arg) == ARRAY_TYPE)
08278   *arg = build_pointer_type (TREE_TYPE (*arg));
08279       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
08280   *arg = build_pointer_type (*arg);
08281       else
08282   *arg = TYPE_MAIN_VARIANT (*arg);
08283     }
08284   
08285   /* [temp.deduct.call]
08286      
08287      If P is a cv-qualified type, the top level cv-qualifiers
08288      of P's type are ignored for type deduction.  If P is a
08289      reference type, the type referred to by P is used for
08290      type deduction.  */
08291   *parm = TYPE_MAIN_VARIANT (*parm);
08292   if (TREE_CODE (*parm) == REFERENCE_TYPE)
08293     {
08294       *parm = TREE_TYPE (*parm);
08295       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
08296     }
08297 
08298   /* DR 322. For conversion deduction, remove a reference type on parm
08299      too (which has been swapped into ARG).  */
08300   if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
08301     *arg = TREE_TYPE (*arg);
08302   
08303   return result;
08304 }
08305 
08306 /* Most parms like fn_type_unification.
08307 
08308    If SUBR is 1, we're being called recursively (to unify the
08309    arguments of a function or method parameter of a function
08310    template).  */
08311 
08312 static int
08313 type_unification_real (tparms, targs, xparms, xargs, subr,
08314            strict, allow_incomplete, xlen)
08315      tree tparms, targs, xparms, xargs;
08316      int subr;
08317      unification_kind_t strict;
08318      int allow_incomplete, xlen;
08319 {
08320   tree parm, arg;
08321   int i;
08322   int ntparms = TREE_VEC_LENGTH (tparms);
08323   int sub_strict;
08324   int saw_undeduced = 0;
08325   tree parms, args;
08326   int len;
08327 
08328   my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
08329   my_friendly_assert (xparms == NULL_TREE 
08330           || TREE_CODE (xparms) == TREE_LIST, 290);
08331   /* ARGS could be NULL.  */
08332   if (xargs)
08333     my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
08334   my_friendly_assert (ntparms > 0, 292);
08335 
08336   switch (strict)
08337     {
08338     case DEDUCE_CALL:
08339       sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
08340                     | UNIFY_ALLOW_DERIVED);
08341       break;
08342       
08343     case DEDUCE_CONV:
08344       sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
08345       break;
08346 
08347     case DEDUCE_EXACT:
08348       sub_strict = UNIFY_ALLOW_NONE;
08349       break;
08350     
08351     case DEDUCE_ORDER:
08352       sub_strict = UNIFY_ALLOW_NONE;
08353       break;
08354       
08355     default:
08356       abort ();
08357     }
08358 
08359   if (xlen == 0)
08360     return 0;
08361 
08362  again:
08363   parms = xparms;
08364   args = xargs;
08365   len = xlen;
08366 
08367   while (parms
08368    && parms != void_list_node
08369    && args
08370    && args != void_list_node)
08371     {
08372       parm = TREE_VALUE (parms);
08373       parms = TREE_CHAIN (parms);
08374       arg = TREE_VALUE (args);
08375       args = TREE_CHAIN (args);
08376 
08377       if (arg == error_mark_node)
08378   return 1;
08379       if (arg == unknown_type_node)
08380   /* We can't deduce anything from this, but we might get all the
08381      template args from other function args.  */
08382   continue;
08383 
08384       /* Conversions will be performed on a function argument that
08385    corresponds with a function parameter that contains only
08386    non-deducible template parameters and explicitly specified
08387    template parameters.  */
08388       if (! uses_template_parms (parm))
08389   {
08390     tree type;
08391 
08392     if (!TYPE_P (arg))
08393       type = TREE_TYPE (arg);
08394     else
08395       {
08396         type = arg;
08397         arg = NULL_TREE;
08398       }
08399 
08400     if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
08401       {
08402         if (same_type_p (parm, type))
08403     continue;
08404       }
08405     else
08406       /* It might work; we shouldn't check now, because we might
08407          get into infinite recursion.  Overload resolution will
08408          handle it.  */
08409       continue;
08410 
08411     return 1;
08412   }
08413   
08414       if (!TYPE_P (arg))
08415   {
08416     my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
08417     if (type_unknown_p (arg))
08418       {
08419         /* [temp.deduct.type] A template-argument can be deduced from
08420      a pointer to function or pointer to member function
08421      argument if the set of overloaded functions does not
08422      contain function templates and at most one of a set of
08423      overloaded functions provides a unique match.  */
08424 
08425         if (resolve_overloaded_unification
08426       (tparms, targs, parm, arg, strict, sub_strict)
08427       != 0)
08428     return 1;
08429         continue;
08430       }
08431     arg = TREE_TYPE (arg);
08432   }
08433       
08434       {
08435         int arg_strict = sub_strict;
08436         
08437         if (!subr)
08438     arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
08439 
08440