118334Speter/* Handle parameterized types (templates) for GNU C++.
290075Sobrien   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3146895Skan   2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
418334Speter   Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
550397Sobrien   Rewritten by Jason Merrill (jason@cygnus.com).
618334Speter
7132718SkanThis file is part of GCC.
818334Speter
9132718SkanGCC is free software; you can redistribute it and/or modify
1018334Speterit under the terms of the GNU General Public License as published by
1118334Speterthe Free Software Foundation; either version 2, or (at your option)
1218334Speterany later version.
1318334Speter
14132718SkanGCC is distributed in the hope that it will be useful,
1518334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1618334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1718334SpeterGNU General Public License for more details.
1818334Speter
1918334SpeterYou should have received a copy of the GNU General Public License
20132718Skanalong with GCC; see the file COPYING.  If not, write to
21169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
22169689SkanBoston, MA 02110-1301, USA.  */
2318334Speter
2418334Speter/* Known bugs or deficiencies include:
2518334Speter
2650397Sobrien     all methods must be provided in header files; can't use a source
2750397Sobrien     file that contains only the method templates and "just win".  */
2850397Sobrien
2918334Speter#include "config.h"
3050397Sobrien#include "system.h"
31132718Skan#include "coretypes.h"
32132718Skan#include "tm.h"
3318334Speter#include "obstack.h"
3418334Speter#include "tree.h"
35169689Skan#include "pointer-set.h"
3618334Speter#include "flags.h"
37169689Skan#include "c-common.h"
3818334Speter#include "cp-tree.h"
39169689Skan#include "cp-objcp-common.h"
4090075Sobrien#include "tree-inline.h"
4118334Speter#include "decl.h"
4218334Speter#include "output.h"
4350397Sobrien#include "except.h"
4450397Sobrien#include "toplev.h"
4552284Sobrien#include "rtl.h"
4690075Sobrien#include "timevar.h"
47169689Skan#include "tree-iterator.h"
48169689Skan#include "vecprim.h"
4918334Speter
5050397Sobrien/* The type of functions taking a tree, and some additional data, and
5150397Sobrien   returning an int.  */
52132718Skantypedef int (*tree_fn_t) (tree, void*);
5350397Sobrien
5452284Sobrien/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
5552284Sobrien   instantiations have been deferred, either because their definitions
56132718Skan   were not yet available, or because we were putting off doing the work.
57132718Skan   The TREE_PURPOSE of each entry is either a DECL (for a function or
58132718Skan   static data member), or a TYPE (for a class) indicating what we are
59132718Skan   hoping to instantiate.  The TREE_VALUE is not used.  */
60117395Skanstatic GTY(()) tree pending_templates;
61132718Skanstatic GTY(()) tree last_pending_template;
6218334Speter
6350397Sobrienint processing_template_parmlist;
6450397Sobrienstatic int template_header_count;
6550397Sobrien
66117395Skanstatic GTY(()) tree saved_trees;
67169689Skanstatic VEC(int,heap) *inline_parm_levels;
6850397Sobrien
69117395Skanstatic GTY(()) tree current_tinst_level;
7090075Sobrien
71117395Skanstatic GTY(()) tree saved_access_scope;
72117395Skan
73169689Skan/* Live only within one (recursive) call to tsubst_expr.  We use
74169689Skan   this to pass the statement expression node from the STMT_EXPR
75169689Skan   to the EXPR_STMT that is its result.  */
76169689Skanstatic tree cur_stmt_expr;
77169689Skan
7890075Sobrien/* A map from local variable declarations in the body of the template
7990075Sobrien   presently being instantiated to the corresponding instantiated
8090075Sobrien   local variables.  */
8190075Sobrienstatic htab_t local_specializations;
8290075Sobrien
8350397Sobrien#define UNIFY_ALLOW_NONE 0
8450397Sobrien#define UNIFY_ALLOW_MORE_CV_QUAL 1
8550397Sobrien#define UNIFY_ALLOW_LESS_CV_QUAL 2
8650397Sobrien#define UNIFY_ALLOW_DERIVED 4
8752284Sobrien#define UNIFY_ALLOW_INTEGER 8
8890075Sobrien#define UNIFY_ALLOW_OUTER_LEVEL 16
8990075Sobrien#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
9090075Sobrien#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
9118334Speter
92132718Skanstatic void push_access_scope (tree);
93132718Skanstatic void pop_access_scope (tree);
94171825Skanstatic bool resolve_overloaded_unification (tree, tree, tree, tree,
95171825Skan					    unification_kind_t, int);
96132718Skanstatic int try_one_overload (tree, tree, tree, tree, tree,
97132718Skan			     unification_kind_t, int, bool);
98132718Skanstatic int unify (tree, tree, tree, tree, int);
99132718Skanstatic void add_pending_template (tree);
100169689Skanstatic int push_tinst_level (tree);
101169689Skanstatic void pop_tinst_level (void);
102132718Skanstatic void reopen_tinst_level (tree);
103132718Skanstatic tree classtype_mangled_name (tree);
104132718Skanstatic char* mangle_class_name_for_template (const char *, tree, tree);
105132718Skanstatic tree tsubst_initializer_list (tree, tree);
106132718Skanstatic tree get_class_bindings (tree, tree, tree);
107169689Skanstatic tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
108169689Skan				   bool, bool);
109132718Skanstatic void tsubst_enum	(tree, tree, tree);
110132718Skanstatic tree add_to_template_args (tree, tree);
111132718Skanstatic tree add_outermost_template_args (tree, tree);
112132718Skanstatic bool check_instantiated_args (tree, tree, tsubst_flags_t);
113169689Skanstatic int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
114132718Skanstatic int  type_unification_real (tree, tree, tree, tree,
115169689Skan				   int, unification_kind_t, int);
116132718Skanstatic void note_template_header (int);
117169689Skanstatic tree convert_nontype_argument_function (tree, tree);
118132718Skanstatic tree convert_nontype_argument (tree, tree);
119132718Skanstatic tree convert_template_argument (tree, tree, tree,
120132718Skan				       tsubst_flags_t, int, tree);
121169689Skanstatic int for_each_template_parm (tree, tree_fn_t, void*,
122169689Skan				   struct pointer_set_t*);
123132718Skanstatic tree build_template_parm_index (int, int, int, tree, tree);
124132718Skanstatic int inline_needs_template_parms (tree);
125132718Skanstatic void push_inline_template_parms_recursive (tree, int);
126132718Skanstatic tree retrieve_local_specialization (tree);
127132718Skanstatic void register_local_specialization (tree, tree);
128132718Skanstatic tree reduce_template_parm_level (tree, tree, int);
129132718Skanstatic int mark_template_parm (tree, void *);
130132718Skanstatic int template_parm_this_level_p (tree, void *);
131132718Skanstatic tree tsubst_friend_function (tree, tree);
132132718Skanstatic tree tsubst_friend_class (tree, tree);
133132718Skanstatic int can_complete_type_without_circularity (tree);
134169689Skanstatic tree get_bindings (tree, tree, tree, bool);
135132718Skanstatic int template_decl_level (tree);
136132718Skanstatic int check_cv_quals_for_unify (int, tree, tree);
137132718Skanstatic tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138132718Skanstatic tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
139132718Skanstatic tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140132718Skanstatic void regenerate_decl_from_template (tree, tree);
141132718Skanstatic tree most_specialized_class (tree, tree);
142132718Skanstatic tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143132718Skanstatic tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144132718Skanstatic tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
145169689Skanstatic bool check_specialization_scope (void);
146132718Skanstatic tree process_partial_specialization (tree);
147132718Skanstatic void set_current_access_from_decl (tree);
148132718Skanstatic void check_default_tmpl_args (tree, tree, int, int);
149132718Skanstatic tree get_template_base (tree, tree, tree, tree);
150132718Skanstatic tree try_class_unification (tree, tree, tree, tree);
151132718Skanstatic int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152132718Skan					   tree, tree);
153132718Skanstatic int template_args_equal (tree, tree);
154132718Skanstatic void tsubst_default_arguments (tree);
155132718Skanstatic tree for_each_template_parm_r (tree *, int *, void *);
156132718Skanstatic tree copy_default_args_to_explicit_spec_1 (tree, tree);
157132718Skanstatic void copy_default_args_to_explicit_spec (tree);
158132718Skanstatic int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
159132718Skanstatic int eq_local_specializations (const void *, const void *);
160132718Skanstatic bool dependent_type_p_r (tree);
161132718Skanstatic tree tsubst (tree, tree, tsubst_flags_t, tree);
162169689Skanstatic tree tsubst_expr	(tree, tree, tsubst_flags_t, tree, bool);
163132718Skanstatic tree tsubst_copy	(tree, tree, tsubst_flags_t, tree);
16418334Speter
165117395Skan/* Make the current scope suitable for access checking when we are
166117395Skan   processing T.  T can be FUNCTION_DECL for instantiated function
167132718Skan   template, or VAR_DECL for static member variable (need by
168132718Skan   instantiate_decl).  */
16952284Sobrien
170132718Skanstatic void
171132718Skanpush_access_scope (tree t)
17290075Sobrien{
173169689Skan  gcc_assert (TREE_CODE (t) == FUNCTION_DECL
174169689Skan	      || TREE_CODE (t) == VAR_DECL);
175117395Skan
176146895Skan  if (DECL_FRIEND_CONTEXT (t))
177146895Skan    push_nested_class (DECL_FRIEND_CONTEXT (t));
178146895Skan  else if (DECL_CLASS_SCOPE_P (t))
179132718Skan    push_nested_class (DECL_CONTEXT (t));
180117395Skan  else
181117395Skan    push_to_top_level ();
182169689Skan
183132718Skan  if (TREE_CODE (t) == FUNCTION_DECL)
184117395Skan    {
185117395Skan      saved_access_scope = tree_cons
186117395Skan	(NULL_TREE, current_function_decl, saved_access_scope);
187117395Skan      current_function_decl = t;
188117395Skan    }
18990075Sobrien}
19052284Sobrien
191117395Skan/* Restore the scope set up by push_access_scope.  T is the node we
192117395Skan   are processing.  */
193117395Skan
194132718Skanstatic void
195132718Skanpop_access_scope (tree t)
196117395Skan{
197132718Skan  if (TREE_CODE (t) == FUNCTION_DECL)
198117395Skan    {
199117395Skan      current_function_decl = TREE_VALUE (saved_access_scope);
200117395Skan      saved_access_scope = TREE_CHAIN (saved_access_scope);
201117395Skan    }
202117395Skan
203146895Skan  if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
204117395Skan    pop_nested_class ();
205117395Skan  else
206117395Skan    pop_from_top_level ();
207117395Skan}
208117395Skan
209132718Skan/* Do any processing required when DECL (a member template
210132718Skan   declaration) is finished.  Returns the TEMPLATE_DECL corresponding
211132718Skan   to DECL, unless it is a specialization, in which case the DECL
212132718Skan   itself is returned.  */
21350397Sobrien
21450397Sobrientree
215132718Skanfinish_member_template_decl (tree decl)
21650397Sobrien{
217132718Skan  if (decl == error_mark_node)
218132718Skan    return error_mark_node;
219132718Skan
220169689Skan  gcc_assert (DECL_P (decl));
221132718Skan
222132718Skan  if (TREE_CODE (decl) == TYPE_DECL)
22350397Sobrien    {
224132718Skan      tree type;
225132718Skan
226132718Skan      type = TREE_TYPE (decl);
227169689Skan      if (IS_AGGR_TYPE (type)
228132718Skan	  && CLASSTYPE_TEMPLATE_INFO (type)
229132718Skan	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
23050397Sobrien	{
231132718Skan	  tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
23250397Sobrien	  check_member_template (tmpl);
23350397Sobrien	  return tmpl;
23450397Sobrien	}
23550397Sobrien      return NULL_TREE;
23650397Sobrien    }
23790075Sobrien  else if (TREE_CODE (decl) == FIELD_DECL)
238169689Skan    error ("data member %qD cannot be a member template", decl);
23950397Sobrien  else if (DECL_TEMPLATE_INFO (decl))
24050397Sobrien    {
24150397Sobrien      if (!DECL_TEMPLATE_SPECIALIZATION (decl))
24250397Sobrien	{
24350397Sobrien	  check_member_template (DECL_TI_TEMPLATE (decl));
24450397Sobrien	  return DECL_TI_TEMPLATE (decl);
24550397Sobrien	}
24650397Sobrien      else
24750397Sobrien	return decl;
248169689Skan    }
24950397Sobrien  else
250169689Skan    error ("invalid member template declaration %qD", decl);
25150397Sobrien
25250397Sobrien  return error_mark_node;
25350397Sobrien}
25450397Sobrien
25550397Sobrien/* Returns the template nesting level of the indicated class TYPE.
256169689Skan
25750397Sobrien   For example, in:
25850397Sobrien     template <class T>
25950397Sobrien     struct A
26050397Sobrien     {
26150397Sobrien       template <class U>
26250397Sobrien       struct B {};
26350397Sobrien     };
26450397Sobrien
265169689Skan   A<T>::B<U> has depth two, while A<T> has depth one.
26652284Sobrien   Both A<T>::B<int> and A<int>::B<U> have depth one, if
267169689Skan   they are instantiations, not specializations.
26850397Sobrien
26952284Sobrien   This function is guaranteed to return 0 if passed NULL_TREE so
27052284Sobrien   that, for example, `template_class_depth (current_class_type)' is
27152284Sobrien   always safe.  */
27252284Sobrien
273169689Skanint
274169689Skantemplate_class_depth (tree type)
27550397Sobrien{
27650397Sobrien  int depth;
27750397Sobrien
278169689Skan  for (depth = 0;
27952284Sobrien       type && TREE_CODE (type) != NAMESPACE_DECL;
280169689Skan       type = (TREE_CODE (type) == FUNCTION_DECL)
28190075Sobrien	 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
28252284Sobrien    {
28352284Sobrien      if (TREE_CODE (type) != FUNCTION_DECL)
28452284Sobrien	{
28552284Sobrien	  if (CLASSTYPE_TEMPLATE_INFO (type)
28652284Sobrien	      && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
287169689Skan	      && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
28852284Sobrien	    ++depth;
28952284Sobrien	}
290169689Skan      else
29152284Sobrien	{
29252284Sobrien	  if (DECL_TEMPLATE_INFO (type)
29352284Sobrien	      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
294169689Skan	      && uses_template_parms (DECL_TI_ARGS (type)))
29552284Sobrien	    ++depth;
29652284Sobrien	}
29752284Sobrien    }
29850397Sobrien
29950397Sobrien  return depth;
30050397Sobrien}
30150397Sobrien
30250397Sobrien/* Returns 1 if processing DECL as part of do_pending_inlines
30350397Sobrien   needs us to push template parms.  */
30450397Sobrien
30550397Sobrienstatic int
306132718Skaninline_needs_template_parms (tree decl)
30750397Sobrien{
30850397Sobrien  if (! DECL_TEMPLATE_INFO (decl))
30950397Sobrien    return 0;
31050397Sobrien
31152284Sobrien  return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
31250397Sobrien	  > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
31350397Sobrien}
31450397Sobrien
31550397Sobrien/* Subroutine of maybe_begin_member_template_processing.
31650397Sobrien   Push the template parms in PARMS, starting from LEVELS steps into the
31750397Sobrien   chain, and ending at the beginning, since template parms are listed
31850397Sobrien   innermost first.  */
31950397Sobrien
32050397Sobrienstatic void
321132718Skanpush_inline_template_parms_recursive (tree parmlist, int levels)
32250397Sobrien{
32350397Sobrien  tree parms = TREE_VALUE (parmlist);
32450397Sobrien  int i;
32550397Sobrien
32650397Sobrien  if (levels > 1)
32750397Sobrien    push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
32850397Sobrien
32950397Sobrien  ++processing_template_decl;
33050397Sobrien  current_template_parms
33190075Sobrien    = tree_cons (size_int (processing_template_decl),
33250397Sobrien		 parms, current_template_parms);
33350397Sobrien  TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
33450397Sobrien
335132718Skan  begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
336169689Skan	       NULL);
337169689Skan  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
33850397Sobrien    {
33950397Sobrien      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
34050397Sobrien
341169689Skan      if (parm == error_mark_node)
342169689Skan	continue;
343169689Skan
344169689Skan      gcc_assert (DECL_P (parm));
345169689Skan
34650397Sobrien      switch (TREE_CODE (parm))
34750397Sobrien	{
34850397Sobrien	case TYPE_DECL:
34950397Sobrien	case TEMPLATE_DECL:
35050397Sobrien	  pushdecl (parm);
35150397Sobrien	  break;
35250397Sobrien
35350397Sobrien	case PARM_DECL:
35450397Sobrien	  {
35552284Sobrien	    /* Make a CONST_DECL as is done in process_template_parm.
35652284Sobrien	       It is ugly that we recreate this here; the original
35752284Sobrien	       version built in process_template_parm is no longer
35852284Sobrien	       available.  */
35950397Sobrien	    tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
36050397Sobrien				    TREE_TYPE (parm));
36190075Sobrien	    DECL_ARTIFICIAL (decl) = 1;
362169689Skan	    TREE_CONSTANT (decl) = 1;
363169689Skan	    TREE_INVARIANT (decl) = 1;
364169689Skan	    TREE_READONLY (decl) = 1;
36550397Sobrien	    DECL_INITIAL (decl) = DECL_INITIAL (parm);
36690075Sobrien	    SET_DECL_TEMPLATE_PARM_P (decl);
36750397Sobrien	    pushdecl (decl);
36850397Sobrien	  }
36950397Sobrien	  break;
37050397Sobrien
37150397Sobrien	default:
372169689Skan	  gcc_unreachable ();
37350397Sobrien	}
37450397Sobrien    }
37550397Sobrien}
37650397Sobrien
37750397Sobrien/* Restore the template parameter context for a member template or
37850397Sobrien   a friend template defined in a class definition.  */
37950397Sobrien
38018334Spetervoid
381132718Skanmaybe_begin_member_template_processing (tree decl)
38250397Sobrien{
38350397Sobrien  tree parms;
38452284Sobrien  int levels = 0;
38550397Sobrien
38652284Sobrien  if (inline_needs_template_parms (decl))
38752284Sobrien    {
38852284Sobrien      parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
38952284Sobrien      levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
39050397Sobrien
39152284Sobrien      if (DECL_TEMPLATE_SPECIALIZATION (decl))
39252284Sobrien	{
39352284Sobrien	  --levels;
39452284Sobrien	  parms = TREE_CHAIN (parms);
39552284Sobrien	}
39650397Sobrien
39752284Sobrien      push_inline_template_parms_recursive (parms, levels);
39850397Sobrien    }
39950397Sobrien
40052284Sobrien  /* Remember how many levels of template parameters we pushed so that
40152284Sobrien     we can pop them later.  */
402169689Skan  VEC_safe_push (int, heap, inline_parm_levels, levels);
40350397Sobrien}
40450397Sobrien
405169689Skan/* Undo the effects of maybe_begin_member_template_processing.  */
40650397Sobrien
407169689Skanvoid
408132718Skanmaybe_end_member_template_processing (void)
40950397Sobrien{
41052284Sobrien  int i;
411169689Skan  int last;
41252284Sobrien
413169689Skan  if (VEC_length (int, inline_parm_levels) == 0)
41450397Sobrien    return;
41550397Sobrien
416169689Skan  last = VEC_pop (int, inline_parm_levels);
417169689Skan  for (i = 0; i < last; ++i)
41850397Sobrien    {
41950397Sobrien      --processing_template_decl;
42050397Sobrien      current_template_parms = TREE_CHAIN (current_template_parms);
42150397Sobrien      poplevel (0, 0, 0);
42250397Sobrien    }
42350397Sobrien}
42450397Sobrien
42552284Sobrien/* Return a new template argument vector which contains all of ARGS,
42690075Sobrien   but has as its innermost set of arguments the EXTRA_ARGS.  */
42750397Sobrien
42850397Sobrienstatic tree
429132718Skanadd_to_template_args (tree args, tree extra_args)
43050397Sobrien{
43152284Sobrien  tree new_args;
43252284Sobrien  int extra_depth;
43352284Sobrien  int i;
43452284Sobrien  int j;
43550397Sobrien
43652284Sobrien  extra_depth = TMPL_ARGS_DEPTH (extra_args);
43790075Sobrien  new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
43850397Sobrien
43952284Sobrien  for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
44052284Sobrien    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
44150397Sobrien
44252284Sobrien  for (j = 1; j <= extra_depth; ++j, ++i)
44352284Sobrien    SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
444169689Skan
44550397Sobrien  return new_args;
44650397Sobrien}
44750397Sobrien
44852284Sobrien/* Like add_to_template_args, but only the outermost ARGS are added to
44952284Sobrien   the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
45052284Sobrien   (EXTRA_ARGS) levels are added.  This function is used to combine
45152284Sobrien   the template arguments from a partial instantiation with the
45252284Sobrien   template arguments used to attain the full instantiation from the
45352284Sobrien   partial instantiation.  */
45450397Sobrien
45550397Sobrienstatic tree
456132718Skanadd_outermost_template_args (tree args, tree extra_args)
45750397Sobrien{
45850397Sobrien  tree new_args;
45950397Sobrien
46052284Sobrien  /* If there are more levels of EXTRA_ARGS than there are ARGS,
46152284Sobrien     something very fishy is going on.  */
462169689Skan  gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
46350397Sobrien
46452284Sobrien  /* If *all* the new arguments will be the EXTRA_ARGS, just return
46552284Sobrien     them.  */
46652284Sobrien  if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
46752284Sobrien    return extra_args;
46850397Sobrien
46952284Sobrien  /* For the moment, we make ARGS look like it contains fewer levels.  */
47052284Sobrien  TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
471169689Skan
47252284Sobrien  new_args = add_to_template_args (args, extra_args);
47350397Sobrien
47452284Sobrien  /* Now, we restore ARGS to its full dimensions.  */
47552284Sobrien  TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
47652284Sobrien
47750397Sobrien  return new_args;
47850397Sobrien}
47950397Sobrien
48090075Sobrien/* Return the N levels of innermost template arguments from the ARGS.  */
48190075Sobrien
48290075Sobrientree
483132718Skanget_innermost_template_args (tree args, int n)
48490075Sobrien{
48590075Sobrien  tree new_args;
48690075Sobrien  int extra_levels;
48790075Sobrien  int i;
48890075Sobrien
489169689Skan  gcc_assert (n >= 0);
49090075Sobrien
49190075Sobrien  /* If N is 1, just return the innermost set of template arguments.  */
49290075Sobrien  if (n == 1)
49390075Sobrien    return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
494169689Skan
49590075Sobrien  /* If we're not removing anything, just return the arguments we were
49690075Sobrien     given.  */
49790075Sobrien  extra_levels = TMPL_ARGS_DEPTH (args) - n;
498169689Skan  gcc_assert (extra_levels >= 0);
49990075Sobrien  if (extra_levels == 0)
50090075Sobrien    return args;
50190075Sobrien
50290075Sobrien  /* Make a new set of arguments, not containing the outer arguments.  */
50390075Sobrien  new_args = make_tree_vec (n);
50490075Sobrien  for (i = 1; i <= n; ++i)
505169689Skan    SET_TMPL_ARGS_LEVEL (new_args, i,
50690075Sobrien			 TMPL_ARGS_LEVEL (args, i + extra_levels));
50790075Sobrien
50890075Sobrien  return new_args;
50990075Sobrien}
51090075Sobrien
51150397Sobrien/* We've got a template header coming up; push to a new level for storing
51250397Sobrien   the parms.  */
51350397Sobrien
51450397Sobrienvoid
515132718Skanbegin_template_parm_list (void)
51618334Speter{
51750397Sobrien  /* We use a non-tag-transparent scope here, which causes pushtag to
51850397Sobrien     put tags in this scope, rather than in the enclosing class or
51950397Sobrien     namespace scope.  This is the right thing, since we want
52050397Sobrien     TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
52150397Sobrien     global template class, push_template_decl handles putting the
52250397Sobrien     TEMPLATE_DECL into top-level scope.  For a nested template class,
52350397Sobrien     e.g.:
52450397Sobrien
52550397Sobrien       template <class T> struct S1 {
526169689Skan	 template <class T> struct S2 {};
52750397Sobrien       };
52850397Sobrien
52950397Sobrien     pushtag contains special code to call pushdecl_with_scope on the
53050397Sobrien     TEMPLATE_DECL for S2.  */
531132718Skan  begin_scope (sk_template_parms, NULL);
53250397Sobrien  ++processing_template_decl;
53350397Sobrien  ++processing_template_parmlist;
53450397Sobrien  note_template_header (0);
53518334Speter}
53618334Speter
53752284Sobrien/* This routine is called when a specialization is declared.  If it is
538169689Skan   invalid to declare a specialization here, an error is reported and
539169689Skan   false is returned, otherwise this routine will return true.  */
54052284Sobrien
541169689Skanstatic bool
542132718Skancheck_specialization_scope (void)
54352284Sobrien{
54452284Sobrien  tree scope = current_scope ();
54552284Sobrien
546169689Skan  /* [temp.expl.spec]
547169689Skan
54852284Sobrien     An explicit specialization shall be declared in the namespace of
54952284Sobrien     which the template is a member, or, for member templates, in the
55052284Sobrien     namespace of which the enclosing class or enclosing class
55152284Sobrien     template is a member.  An explicit specialization of a member
55252284Sobrien     function, member class or static data member of a class template
55352284Sobrien     shall be declared in the namespace of which the class template
55452284Sobrien     is a member.  */
55552284Sobrien  if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
556169689Skan    {
557169689Skan      error ("explicit specialization in non-namespace scope %qD", scope);
558169689Skan      return false;
559169689Skan    }
56052284Sobrien
561169689Skan  /* [temp.expl.spec]
56252284Sobrien
56352284Sobrien     In an explicit specialization declaration for a member of a class
56452284Sobrien     template or a member template that appears in namespace scope,
56552284Sobrien     the member template and some of its enclosing class templates may
56652284Sobrien     remain unspecialized, except that the declaration shall not
56752284Sobrien     explicitly specialize a class member template if its enclosing
56852284Sobrien     class templates are not explicitly specialized as well.  */
569169689Skan  if (current_template_parms)
570169689Skan    {
571169689Skan      error ("enclosing class templates are not explicitly specialized");
572169689Skan      return false;
573169689Skan    }
574169689Skan
575169689Skan  return true;
57652284Sobrien}
57752284Sobrien
578117395Skan/* We've just seen template <>.  */
57950397Sobrien
580169689Skanbool
581132718Skanbegin_specialization (void)
58250397Sobrien{
583132718Skan  begin_scope (sk_template_spec, NULL);
58450397Sobrien  note_template_header (1);
585169689Skan  return check_specialization_scope ();
58650397Sobrien}
58750397Sobrien
58890075Sobrien/* Called at then end of processing a declaration preceded by
58950397Sobrien   template<>.  */
59050397Sobrien
591169689Skanvoid
592132718Skanend_specialization (void)
59350397Sobrien{
59490075Sobrien  finish_scope ();
59550397Sobrien  reset_specialization ();
59650397Sobrien}
59750397Sobrien
59850397Sobrien/* Any template <>'s that we have seen thus far are not referring to a
599117395Skan   function specialization.  */
60050397Sobrien
60150397Sobrienvoid
602132718Skanreset_specialization (void)
60350397Sobrien{
60450397Sobrien  processing_specialization = 0;
60550397Sobrien  template_header_count = 0;
60650397Sobrien}
60750397Sobrien
608117395Skan/* We've just seen a template header.  If SPECIALIZATION is nonzero,
60950397Sobrien   it was of the form template <>.  */
61050397Sobrien
611169689Skanstatic void
612132718Skannote_template_header (int specialization)
61350397Sobrien{
61450397Sobrien  processing_specialization = specialization;
61550397Sobrien  template_header_count++;
61650397Sobrien}
61750397Sobrien
61850397Sobrien/* We're beginning an explicit instantiation.  */
61950397Sobrien
62050397Sobrienvoid
621132718Skanbegin_explicit_instantiation (void)
62250397Sobrien{
623169689Skan  gcc_assert (!processing_explicit_instantiation);
624132718Skan  processing_explicit_instantiation = true;
62550397Sobrien}
62650397Sobrien
62750397Sobrien
62850397Sobrienvoid
629132718Skanend_explicit_instantiation (void)
63050397Sobrien{
631169689Skan  gcc_assert (processing_explicit_instantiation);
632132718Skan  processing_explicit_instantiation = false;
63350397Sobrien}
63450397Sobrien
635169689Skan/* An explicit specialization or partial specialization TMPL is being
636146895Skan   declared.  Check that the namespace in which the specialization is
637146895Skan   occurring is permissible.  Returns false iff it is invalid to
638146895Skan   specialize TMPL in the current namespace.  */
639169689Skan
640146895Skanstatic bool
641146895Skancheck_specialization_namespace (tree tmpl)
642146895Skan{
643146895Skan  tree tpl_ns = decl_namespace_context (tmpl);
644146895Skan
645146895Skan  /* [tmpl.expl.spec]
646169689Skan
647146895Skan     An explicit specialization shall be declared in the namespace of
648146895Skan     which the template is a member, or, for member templates, in the
649146895Skan     namespace of which the enclosing class or enclosing class
650146895Skan     template is a member.  An explicit specialization of a member
651146895Skan     function, member class or static data member of a class template
652146895Skan     shall be declared in the namespace of which the class template is
653146895Skan     a member.  */
654146895Skan  if (is_associated_namespace (current_namespace, tpl_ns))
655146895Skan    /* Same or super-using namespace.  */
656146895Skan    return true;
657146895Skan  else
658146895Skan    {
659169689Skan      pedwarn ("specialization of %qD in different namespace", tmpl);
660169689Skan      pedwarn ("  from definition of %q+#D", tmpl);
661146895Skan      return false;
662146895Skan    }
663146895Skan}
664146895Skan
665169689Skan/* SPEC is an explicit instantiation.  Check that it is valid to
666169689Skan   perform this explicit instantiation in the current namespace.  */
667169689Skan
668169689Skanstatic void
669169689Skancheck_explicit_instantiation_namespace (tree spec)
670169689Skan{
671169689Skan  tree ns;
672169689Skan
673169689Skan  /* DR 275: An explicit instantiation shall appear in an enclosing
674169689Skan     namespace of its template.  */
675169689Skan  ns = decl_namespace_context (spec);
676169689Skan  if (!is_ancestor (current_namespace, ns))
677169689Skan    pedwarn ("explicit instantiation of %qD in namespace %qD "
678169689Skan	     "(which does not enclose namespace %qD)",
679169689Skan	     spec, current_namespace, ns);
680169689Skan}
681169689Skan
68252284Sobrien/* The TYPE is being declared.  If it is a template type, that means it
68352284Sobrien   is a partial specialization.  Do appropriate error-checking.  */
68452284Sobrien
685169689Skantree
686132718Skanmaybe_process_partial_specialization (tree type)
68752284Sobrien{
688161651Skan  tree context;
689117395Skan
690161651Skan  if (type == error_mark_node)
691169689Skan    return error_mark_node;
692161651Skan
693169689Skan  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
694169689Skan    {
695169689Skan      error ("name of class shadows template template parameter %qD",
696169689Skan	     TYPE_NAME (type));
697169689Skan      return error_mark_node;
698169689Skan    }
699169689Skan
700161651Skan  context = TYPE_CONTEXT (type);
701161651Skan
702117395Skan  if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
70352284Sobrien    {
704117395Skan      /* This is for ordinary explicit specialization and partial
705117395Skan	 specialization of a template class such as:
706117395Skan
707117395Skan	   template <> class C<int>;
708117395Skan
709117395Skan	 or:
710117395Skan
711117395Skan	   template <class T> class C<T*>;
712117395Skan
713117395Skan	 Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
714117395Skan
71552284Sobrien      if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
71690075Sobrien	  && !COMPLETE_TYPE_P (type))
71752284Sobrien	{
718146895Skan	  check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
71952284Sobrien	  SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
72052284Sobrien	  if (processing_template_decl)
72152284Sobrien	    push_template_decl (TYPE_MAIN_DECL (type));
72252284Sobrien	}
72352284Sobrien      else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
724169689Skan	error ("specialization of %qT after instantiation", type);
72552284Sobrien    }
726117395Skan  else if (CLASS_TYPE_P (type)
727117395Skan	   && !CLASSTYPE_USE_TEMPLATE (type)
728117395Skan	   && CLASSTYPE_TEMPLATE_INFO (type)
729117395Skan	   && context && CLASS_TYPE_P (context)
730117395Skan	   && CLASSTYPE_TEMPLATE_INFO (context))
731117395Skan    {
732117395Skan      /* This is for an explicit specialization of member class
733117395Skan	 template according to [temp.expl.spec/18]:
734117395Skan
735117395Skan	   template <> template <class U> class C<int>::D;
736117395Skan
737117395Skan	 The context `C<int>' must be an implicit instantiation.
738117395Skan	 Otherwise this is just a member class template declared
739117395Skan	 earlier like:
740117395Skan
741117395Skan	   template <> class C<int> { template <class U> class D; };
742117395Skan	   template <> template <class U> class C<int>::D;
743117395Skan
744117395Skan	 In the first case, `C<int>::D' is a specialization of `C<T>::D'
745117395Skan	 while in the second case, `C<int>::D' is a primary template
746117395Skan	 and `C<T>::D' may not exist.  */
747117395Skan
748117395Skan      if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
749117395Skan	  && !COMPLETE_TYPE_P (type))
750117395Skan	{
751117395Skan	  tree t;
752117395Skan
753117395Skan	  if (current_namespace
754117395Skan	      != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
755117395Skan	    {
756169689Skan	      pedwarn ("specializing %q#T in different namespace", type);
757169689Skan	      pedwarn ("  from definition of %q+#D",
758169689Skan		       CLASSTYPE_TI_TEMPLATE (type));
759117395Skan	    }
760117395Skan
761117395Skan	  /* Check for invalid specialization after instantiation:
762117395Skan
763117395Skan	       template <> template <> class C<int>::D<int>;
764117395Skan	       template <> template <class U> class C<int>::D;  */
765117395Skan
766117395Skan	  for (t = DECL_TEMPLATE_INSTANTIATIONS
767117395Skan		 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
768117395Skan	       t; t = TREE_CHAIN (t))
769117395Skan	    if (TREE_VALUE (t) != type
770117395Skan		&& TYPE_CONTEXT (TREE_VALUE (t)) == context)
771169689Skan	      error ("specialization %qT after instantiation %qT",
772117395Skan		     type, TREE_VALUE (t));
773117395Skan
774117395Skan	  /* Mark TYPE as a specialization.  And as a result, we only
775117395Skan	     have one level of template argument for the innermost
776117395Skan	     class template.  */
777117395Skan	  SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
778117395Skan	  CLASSTYPE_TI_ARGS (type)
779117395Skan	    = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
780117395Skan	}
781117395Skan    }
78252284Sobrien  else if (processing_specialization)
783169689Skan    {
784169689Skan      error ("explicit specialization of non-template %qT", type);
785169689Skan      return error_mark_node;
786169689Skan    }
787169689Skan
788169689Skan  return type;
78952284Sobrien}
79052284Sobrien
791169689Skan/* Returns nonzero if we can optimize the retrieval of specializations
792169689Skan   for TMPL, a TEMPLATE_DECL.  In particular, for such a template, we
793169689Skan   do not use DECL_TEMPLATE_SPECIALIZATIONS at all.  */
794169689Skan
795169689Skanstatic inline bool
796169689Skanoptimize_specialization_lookup_p (tree tmpl)
797169689Skan{
798169689Skan  return (DECL_FUNCTION_TEMPLATE_P (tmpl)
799169689Skan	  && DECL_CLASS_SCOPE_P (tmpl)
800169689Skan	  /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
801169689Skan	     parameter.  */
802169689Skan	  && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
803169689Skan	  /* The optimized lookup depends on the fact that the
804169689Skan	     template arguments for the member function template apply
805169689Skan	     purely to the containing class, which is not true if the
806169689Skan	     containing class is an explicit or partial
807169689Skan	     specialization.  */
808169689Skan	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
809169689Skan	  && !DECL_MEMBER_TEMPLATE_P (tmpl)
810169689Skan	  && !DECL_CONV_FN_P (tmpl)
811169689Skan	  /* It is possible to have a template that is not a member
812169689Skan	     template and is not a member of a template class:
813169689Skan
814169689Skan	     template <typename T>
815169689Skan	     struct S { friend A::f(); };
816169689Skan
817169689Skan	     Here, the friend function is a template, but the context does
818169689Skan	     not have template information.  The optimized lookup relies
819169689Skan	     on having ARGS be the template arguments for both the class
820169689Skan	     and the function template.  */
821169689Skan	  && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
822169689Skan}
823169689Skan
82450397Sobrien/* Retrieve the specialization (in the sense of [temp.spec] - a
82550397Sobrien   specialization is either an instantiation or an explicit
82650397Sobrien   specialization) of TMPL for the given template ARGS.  If there is
82750397Sobrien   no such specialization, return NULL_TREE.  The ARGS are a vector of
82850397Sobrien   arguments, or a vector of vectors of arguments, in the case of
829169689Skan   templates with more than one level of parameters.
830169689Skan
831169689Skan   If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
832169689Skan   then we search for a partial specialization matching ARGS.  This
833169689Skan   parameter is ignored if TMPL is not a class template.  */
834169689Skan
83550397Sobrienstatic tree
836169689Skanretrieve_specialization (tree tmpl, tree args,
837169689Skan			 bool class_specializations_p)
83850397Sobrien{
839169689Skan  if (args == error_mark_node)
840169689Skan    return NULL_TREE;
84150397Sobrien
842169689Skan  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
84350397Sobrien
84452284Sobrien  /* There should be as many levels of arguments as there are
84552284Sobrien     levels of parameters.  */
846169689Skan  gcc_assert (TMPL_ARGS_DEPTH (args)
847169689Skan	      == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
84850397Sobrien
849169689Skan  if (optimize_specialization_lookup_p (tmpl))
850169689Skan    {
851169689Skan      tree class_template;
852169689Skan      tree class_specialization;
853169689Skan      VEC(tree,gc) *methods;
854169689Skan      tree fns;
855169689Skan      int idx;
856169689Skan
857169689Skan      /* The template arguments actually apply to the containing
858169689Skan	 class.  Find the class specialization with those
859169689Skan	 arguments.  */
860169689Skan      class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
861169689Skan      class_specialization
862169689Skan	= retrieve_specialization (class_template, args,
863169689Skan				   /*class_specializations_p=*/false);
864169689Skan      if (!class_specialization)
865169689Skan	return NULL_TREE;
866169689Skan      /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
867169689Skan	 for the specialization.  */
868169689Skan      idx = class_method_index_for_fn (class_specialization, tmpl);
869169689Skan      if (idx == -1)
870169689Skan	return NULL_TREE;
871169689Skan      /* Iterate through the methods with the indicated name, looking
872169689Skan	 for the one that has an instance of TMPL.  */
873169689Skan      methods = CLASSTYPE_METHOD_VEC (class_specialization);
874169689Skan      for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
875169689Skan	{
876169689Skan	  tree fn = OVL_CURRENT (fns);
877169689Skan	  if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
878169689Skan	    return fn;
879169689Skan	}
880169689Skan      return NULL_TREE;
881169689Skan    }
882169689Skan  else
883169689Skan    {
884169689Skan      tree *sp;
885169689Skan      tree *head;
886169689Skan
887169689Skan      /* Class templates store their instantiations on the
888169689Skan	 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
889169689Skan	 DECL_TEMPLATE_SPECIALIZATIONS list.  */
890169689Skan      if (!class_specializations_p
891169689Skan	  && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
892169689Skan	sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
893169689Skan      else
894169689Skan	sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
895169689Skan      head = sp;
896169689Skan      /* Iterate through the list until we find a matching template.  */
897169689Skan      while (*sp != NULL_TREE)
898169689Skan	{
899169689Skan	  tree spec = *sp;
900169689Skan
901169689Skan	  if (comp_template_args (TREE_PURPOSE (spec), args))
902169689Skan	    {
903169689Skan	      /* Use the move-to-front heuristic to speed up future
904169689Skan		 searches.  */
905169689Skan	      if (spec != *head)
906169689Skan		{
907169689Skan		  *sp = TREE_CHAIN (*sp);
908169689Skan		  TREE_CHAIN (spec) = *head;
909169689Skan		  *head = spec;
910169689Skan		}
911169689Skan	      return TREE_VALUE (spec);
912169689Skan	    }
913169689Skan	  sp = &TREE_CHAIN (spec);
914169689Skan	}
915169689Skan    }
916169689Skan
91750397Sobrien  return NULL_TREE;
91850397Sobrien}
91950397Sobrien
92090075Sobrien/* Like retrieve_specialization, but for local declarations.  */
92190075Sobrien
92290075Sobrienstatic tree
923132718Skanretrieve_local_specialization (tree tmpl)
92490075Sobrien{
925169689Skan  tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
926169689Skan					  htab_hash_pointer (tmpl));
927132718Skan  return spec ? TREE_PURPOSE (spec) : NULL_TREE;
92890075Sobrien}
92990075Sobrien
930117395Skan/* Returns nonzero iff DECL is a specialization of TMPL.  */
93150397Sobrien
93250397Sobrienint
933132718Skanis_specialization_of (tree decl, tree tmpl)
93450397Sobrien{
93550397Sobrien  tree t;
93650397Sobrien
93750397Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL)
93850397Sobrien    {
939169689Skan      for (t = decl;
94050397Sobrien	   t != NULL_TREE;
94150397Sobrien	   t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
94250397Sobrien	if (t == tmpl)
94350397Sobrien	  return 1;
94450397Sobrien    }
945169689Skan  else
94650397Sobrien    {
947169689Skan      gcc_assert (TREE_CODE (decl) == TYPE_DECL);
94850397Sobrien
94950397Sobrien      for (t = TREE_TYPE (decl);
95050397Sobrien	   t != NULL_TREE;
95150397Sobrien	   t = CLASSTYPE_USE_TEMPLATE (t)
95250397Sobrien	     ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
95390075Sobrien	if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
95450397Sobrien	  return 1;
955169689Skan    }
95650397Sobrien
95750397Sobrien  return 0;
95850397Sobrien}
95950397Sobrien
960132718Skan/* Returns nonzero iff DECL is a specialization of friend declaration
961132718Skan   FRIEND according to [temp.friend].  */
962132718Skan
963132718Skanbool
964132718Skanis_specialization_of_friend (tree decl, tree friend)
965132718Skan{
966132718Skan  bool need_template = true;
967132718Skan  int template_depth;
968132718Skan
969169689Skan  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
970169689Skan	      || TREE_CODE (decl) == TYPE_DECL);
971132718Skan
972132718Skan  /* For [temp.friend/6] when FRIEND is an ordinary member function
973132718Skan     of a template class, we want to check if DECL is a specialization
974132718Skan     if this.  */
975132718Skan  if (TREE_CODE (friend) == FUNCTION_DECL
976132718Skan      && DECL_TEMPLATE_INFO (friend)
977132718Skan      && !DECL_USE_TEMPLATE (friend))
978132718Skan    {
979169689Skan      /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
980132718Skan      friend = DECL_TI_TEMPLATE (friend);
981132718Skan      need_template = false;
982132718Skan    }
983169689Skan  else if (TREE_CODE (friend) == TEMPLATE_DECL
984169689Skan	   && !PRIMARY_TEMPLATE_P (friend))
985169689Skan    need_template = false;
986132718Skan
987132718Skan  /* There is nothing to do if this is not a template friend.  */
988132718Skan  if (TREE_CODE (friend) != TEMPLATE_DECL)
989169689Skan    return false;
990132718Skan
991132718Skan  if (is_specialization_of (decl, friend))
992169689Skan    return true;
993132718Skan
994132718Skan  /* [temp.friend/6]
995132718Skan     A member of a class template may be declared to be a friend of a
996132718Skan     non-template class.  In this case, the corresponding member of
997132718Skan     every specialization of the class template is a friend of the
998132718Skan     class granting friendship.
999169689Skan
1000132718Skan     For example, given a template friend declaration
1001132718Skan
1002132718Skan       template <class T> friend void A<T>::f();
1003132718Skan
1004132718Skan     the member function below is considered a friend
1005132718Skan
1006132718Skan       template <> struct A<int> {
1007132718Skan	 void f();
1008132718Skan       };
1009132718Skan
1010132718Skan     For this type of template friend, TEMPLATE_DEPTH below will be
1011132718Skan     nonzero.  To determine if DECL is a friend of FRIEND, we first
1012132718Skan     check if the enclosing class is a specialization of another.  */
1013132718Skan
1014132718Skan  template_depth = template_class_depth (DECL_CONTEXT (friend));
1015132718Skan  if (template_depth
1016132718Skan      && DECL_CLASS_SCOPE_P (decl)
1017169689Skan      && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1018132718Skan			       CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1019132718Skan    {
1020132718Skan      /* Next, we check the members themselves.  In order to handle
1021169689Skan	 a few tricky cases, such as when FRIEND's are
1022132718Skan
1023132718Skan	   template <class T> friend void A<T>::g(T t);
1024132718Skan	   template <class T> template <T t> friend void A<T>::h();
1025132718Skan
1026169689Skan	 and DECL's are
1027132718Skan
1028169689Skan	   void A<int>::g(int);
1029169689Skan	   template <int> void A<int>::h();
1030169689Skan
1031169689Skan	 we need to figure out ARGS, the template arguments from
1032169689Skan	 the context of DECL.  This is required for template substitution
1033169689Skan	 of `T' in the function parameter of `g' and template parameter
1034169689Skan	 of `h' in the above examples.  Here ARGS corresponds to `int'.  */
1035169689Skan
1036132718Skan      tree context = DECL_CONTEXT (decl);
1037132718Skan      tree args = NULL_TREE;
1038132718Skan      int current_depth = 0;
1039169689Skan
1040132718Skan      while (current_depth < template_depth)
1041132718Skan	{
1042132718Skan	  if (CLASSTYPE_TEMPLATE_INFO (context))
1043132718Skan	    {
1044132718Skan	      if (current_depth == 0)
1045132718Skan		args = TYPE_TI_ARGS (context);
1046132718Skan	      else
1047132718Skan		args = add_to_template_args (TYPE_TI_ARGS (context), args);
1048132718Skan	      current_depth++;
1049132718Skan	    }
1050132718Skan	  context = TYPE_CONTEXT (context);
1051132718Skan	}
1052132718Skan
1053132718Skan      if (TREE_CODE (decl) == FUNCTION_DECL)
1054132718Skan	{
1055132718Skan	  bool is_template;
1056132718Skan	  tree friend_type;
1057132718Skan	  tree decl_type;
1058132718Skan	  tree friend_args_type;
1059132718Skan	  tree decl_args_type;
1060132718Skan
1061132718Skan	  /* Make sure that both DECL and FRIEND are templates or
1062132718Skan	     non-templates.  */
1063132718Skan	  is_template = DECL_TEMPLATE_INFO (decl)
1064132718Skan			&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1065132718Skan	  if (need_template ^ is_template)
1066169689Skan	    return false;
1067132718Skan	  else if (is_template)
1068132718Skan	    {
1069132718Skan	      /* If both are templates, check template parameter list.  */
1070132718Skan	      tree friend_parms
1071132718Skan		= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1072132718Skan					 args, tf_none);
1073132718Skan	      if (!comp_template_parms
1074132718Skan		     (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1075132718Skan		      friend_parms))
1076169689Skan		return false;
1077132718Skan
1078132718Skan	      decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1079132718Skan	    }
1080132718Skan	  else
1081132718Skan	    decl_type = TREE_TYPE (decl);
1082132718Skan
1083132718Skan	  friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1084132718Skan					      tf_none, NULL_TREE);
1085132718Skan	  if (friend_type == error_mark_node)
1086169689Skan	    return false;
1087132718Skan
1088132718Skan	  /* Check if return types match.  */
1089132718Skan	  if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1090169689Skan	    return false;
1091132718Skan
1092132718Skan	  /* Check if function parameter types match, ignoring the
1093132718Skan	     `this' parameter.  */
1094132718Skan	  friend_args_type = TYPE_ARG_TYPES (friend_type);
1095132718Skan	  decl_args_type = TYPE_ARG_TYPES (decl_type);
1096132718Skan	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1097132718Skan	    friend_args_type = TREE_CHAIN (friend_args_type);
1098132718Skan	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1099132718Skan	    decl_args_type = TREE_CHAIN (decl_args_type);
1100169689Skan
1101169689Skan	  return compparms (decl_args_type, friend_args_type);
1102132718Skan	}
1103169689Skan      else
1104169689Skan	{
1105169689Skan	  /* DECL is a TYPE_DECL */
1106169689Skan	  bool is_template;
1107169689Skan	  tree decl_type = TREE_TYPE (decl);
1108169689Skan
1109169689Skan	  /* Make sure that both DECL and FRIEND are templates or
1110169689Skan	     non-templates.  */
1111169689Skan	  is_template
1112169689Skan	    = CLASSTYPE_TEMPLATE_INFO (decl_type)
1113169689Skan	      && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1114169689Skan
1115169689Skan	  if (need_template ^ is_template)
1116169689Skan	    return false;
1117169689Skan	  else if (is_template)
1118169689Skan	    {
1119169689Skan	      tree friend_parms;
1120169689Skan	      /* If both are templates, check the name of the two
1121169689Skan		 TEMPLATE_DECL's first because is_friend didn't.  */
1122169689Skan	      if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1123169689Skan		  != DECL_NAME (friend))
1124169689Skan		return false;
1125169689Skan
1126169689Skan	      /* Now check template parameter list.  */
1127169689Skan	      friend_parms
1128169689Skan		= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1129169689Skan					 args, tf_none);
1130169689Skan	      return comp_template_parms
1131169689Skan		(DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1132169689Skan		 friend_parms);
1133169689Skan	    }
1134169689Skan	  else
1135169689Skan	    return (DECL_NAME (decl)
1136169689Skan		    == DECL_NAME (friend));
1137169689Skan	}
1138132718Skan    }
1139169689Skan  return false;
1140132718Skan}
1141132718Skan
114250397Sobrien/* Register the specialization SPEC as a specialization of TMPL with
1143169689Skan   the indicated ARGS.  IS_FRIEND indicates whether the specialization
1144169689Skan   is actually just a friend declaration.  Returns SPEC, or an
1145169689Skan   equivalent prior declaration, if available.  */
114650397Sobrien
114752284Sobrienstatic tree
1148169689Skanregister_specialization (tree spec, tree tmpl, tree args, bool is_friend)
114950397Sobrien{
1150169689Skan  tree fn;
115150397Sobrien
1152169689Skan  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
115350397Sobrien
1154169689Skan  if (TREE_CODE (spec) == FUNCTION_DECL
115552284Sobrien      && uses_template_parms (DECL_TI_ARGS (spec)))
115652284Sobrien    /* This is the FUNCTION_DECL for a partial instantiation.  Don't
115752284Sobrien       register it; we want the corresponding TEMPLATE_DECL instead.
115852284Sobrien       We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
115952284Sobrien       the more obvious `uses_template_parms (spec)' to avoid problems
116052284Sobrien       with default function arguments.  In particular, given
116152284Sobrien       something like this:
116252284Sobrien
1163169689Skan	  template <class T> void f(T t1, T t = T())
116452284Sobrien
116552284Sobrien       the default argument expression is not substituted for in an
116652284Sobrien       instantiation unless and until it is actually needed.  */
116752284Sobrien    return spec;
1168132718Skan
1169169689Skan  fn = retrieve_specialization (tmpl, args,
1170169689Skan				/*class_specializations_p=*/false);
1171169689Skan  /* We can sometimes try to re-register a specialization that we've
1172169689Skan     already got.  In particular, regenerate_decl_from_template calls
1173169689Skan     duplicate_decls which will update the specialization list.  But,
1174169689Skan     we'll still get called again here anyhow.  It's more convenient
1175169689Skan     to simply allow this than to try to prevent it.  */
1176169689Skan  if (fn == spec)
1177169689Skan    return spec;
1178169689Skan  else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
117990075Sobrien    {
1180169689Skan      if (DECL_TEMPLATE_INSTANTIATION (fn))
118190075Sobrien	{
1182169689Skan	  if (TREE_USED (fn)
1183169689Skan	      || DECL_EXPLICIT_INSTANTIATION (fn))
118490075Sobrien	    {
1185169689Skan	      error ("specialization of %qD after instantiation",
1186169689Skan		     fn);
1187169689Skan	      return error_mark_node;
1188169689Skan	    }
1189169689Skan	  else
1190169689Skan	    {
1191169689Skan	      tree clone;
1192169689Skan	      /* This situation should occur only if the first
1193169689Skan		 specialization is an implicit instantiation, the
1194169689Skan		 second is an explicit specialization, and the
1195169689Skan		 implicit instantiation has not yet been used.  That
1196169689Skan		 situation can occur if we have implicitly
1197169689Skan		 instantiated a member function and then specialized
1198169689Skan		 it later.
119952284Sobrien
1200169689Skan		 We can also wind up here if a friend declaration that
1201169689Skan		 looked like an instantiation turns out to be a
1202169689Skan		 specialization:
120352284Sobrien
1204169689Skan		   template <class T> void foo(T);
1205169689Skan		   class S { friend void foo<>(int) };
1206169689Skan		   template <> void foo(int);
120752284Sobrien
1208169689Skan		 We transform the existing DECL in place so that any
1209169689Skan		 pointers to it become pointers to the updated
1210169689Skan		 declaration.
121152284Sobrien
1212169689Skan		 If there was a definition for the template, but not
1213169689Skan		 for the specialization, we want this to look as if
1214169689Skan		 there were no definition, and vice versa.  */
1215169689Skan	      DECL_INITIAL (fn) = NULL_TREE;
1216169689Skan	      duplicate_decls (spec, fn, is_friend);
1217169689Skan	      /* The call to duplicate_decls will have applied
1218169689Skan		 [temp.expl.spec]:
121952284Sobrien
1220169689Skan		   An explicit specialization of a function template
1221169689Skan		   is inline only if it is explicitly declared to be,
1222169689Skan		   and independently of whether its function template
1223169689Skan		   is.
1224169689Skan
1225169689Skan		to the primary function; now copy the inline bits to
1226169689Skan		the various clones.  */
1227169689Skan	      FOR_EACH_CLONE (clone, fn)
122890075Sobrien		{
1229169689Skan		  DECL_DECLARED_INLINE_P (clone)
1230169689Skan		    = DECL_DECLARED_INLINE_P (fn);
1231169689Skan		  DECL_INLINE (clone)
1232169689Skan		    = DECL_INLINE (fn);
123390075Sobrien		}
1234169689Skan	      check_specialization_namespace (fn);
1235169689Skan
1236169689Skan	      return fn;
123790075Sobrien	    }
123890075Sobrien	}
1239169689Skan      else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1240169689Skan	{
1241169689Skan	  if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1242169689Skan	    /* Dup decl failed, but this is a new definition. Set the
1243169689Skan	       line number so any errors match this new
1244169689Skan	       definition.  */
1245169689Skan	    DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
124650397Sobrien
1247169689Skan	  return fn;
1248169689Skan	}
1249169689Skan    }
1250169689Skan
1251146895Skan  /* A specialization must be declared in the same namespace as the
1252146895Skan     template it is specializing.  */
1253146895Skan  if (DECL_TEMPLATE_SPECIALIZATION (spec)
1254146895Skan      && !check_specialization_namespace (tmpl))
1255169689Skan    DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1256146895Skan
1257169689Skan  if (!optimize_specialization_lookup_p (tmpl))
1258169689Skan    DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1259169689Skan      = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
126052284Sobrien
126152284Sobrien  return spec;
126250397Sobrien}
126350397Sobrien
126452284Sobrien/* Unregister the specialization SPEC as a specialization of TMPL.
1265119256Skan   Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
1266119256Skan   if the SPEC was listed as a specialization of TMPL.  */
126752284Sobrien
1268132718Skanbool
1269119256Skanreregister_specialization (tree spec, tree tmpl, tree new_spec)
127052284Sobrien{
127152284Sobrien  tree* s;
127252284Sobrien
127352284Sobrien  for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
127452284Sobrien       *s != NULL_TREE;
127552284Sobrien       s = &TREE_CHAIN (*s))
127652284Sobrien    if (TREE_VALUE (*s) == spec)
127752284Sobrien      {
1278119256Skan	if (!new_spec)
1279119256Skan	  *s = TREE_CHAIN (*s);
1280119256Skan	else
1281119256Skan	  TREE_VALUE (*s) = new_spec;
128252284Sobrien	return 1;
128352284Sobrien      }
128452284Sobrien
128552284Sobrien  return 0;
128652284Sobrien}
128752284Sobrien
1288132718Skan/* Compare an entry in the local specializations hash table P1 (which
1289132718Skan   is really a pointer to a TREE_LIST) with P2 (which is really a
1290132718Skan   DECL).  */
1291132718Skan
1292132718Skanstatic int
1293132718Skaneq_local_specializations (const void *p1, const void *p2)
1294132718Skan{
1295132718Skan  return TREE_VALUE ((tree) p1) == (tree) p2;
1296132718Skan}
1297132718Skan
1298132718Skan/* Hash P1, an entry in the local specializations table.  */
1299132718Skan
1300132718Skanstatic hashval_t
1301132718Skanhash_local_specialization (const void* p1)
1302132718Skan{
1303132718Skan  return htab_hash_pointer (TREE_VALUE ((tree) p1));
1304132718Skan}
1305132718Skan
130690075Sobrien/* Like register_specialization, but for local declarations.  We are
130790075Sobrien   registering SPEC, an instantiation of TMPL.  */
130890075Sobrien
130990075Sobrienstatic void
1310132718Skanregister_local_specialization (tree spec, tree tmpl)
131190075Sobrien{
131290075Sobrien  void **slot;
131390075Sobrien
1314169689Skan  slot = htab_find_slot_with_hash (local_specializations, tmpl,
1315132718Skan				   htab_hash_pointer (tmpl), INSERT);
1316132718Skan  *slot = build_tree_list (spec, tmpl);
131790075Sobrien}
131890075Sobrien
1319169689Skan/* TYPE is a class type.  Returns true if TYPE is an explicitly
1320169689Skan   specialized class.  */
1321169689Skan
1322169689Skanbool
1323169689Skanexplicit_class_specialization_p (tree type)
1324169689Skan{
1325169689Skan  if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1326169689Skan    return false;
1327169689Skan  return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1328169689Skan}
1329169689Skan
133050397Sobrien/* Print the list of candidate FNS in an error message.  */
133150397Sobrien
133252284Sobrienvoid
1333132718Skanprint_candidates (tree fns)
133450397Sobrien{
133550397Sobrien  tree fn;
133650397Sobrien
133752284Sobrien  const char *str = "candidates are:";
133850397Sobrien
133950397Sobrien  for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
134050397Sobrien    {
134152284Sobrien      tree f;
134252284Sobrien
134352284Sobrien      for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1344169689Skan	error ("%s %+#D", str, OVL_CURRENT (f));
134550397Sobrien      str = "               ";
134650397Sobrien    }
134750397Sobrien}
134850397Sobrien
134950397Sobrien/* Returns the template (one of the functions given by TEMPLATE_ID)
135050397Sobrien   which can be specialized to match the indicated DECL with the
135152284Sobrien   explicit template args given in TEMPLATE_ID.  The DECL may be
135252284Sobrien   NULL_TREE if none is available.  In that case, the functions in
135352284Sobrien   TEMPLATE_ID are non-members.
135450397Sobrien
1355117395Skan   If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
135652284Sobrien   specialization of a member template.
135752284Sobrien
1358169689Skan   The TEMPLATE_COUNT is the number of references to qualifying
1359169689Skan   template classes that appeared in the name of the function. See
1360169689Skan   check_explicit_specialization for a more accurate description.
1361169689Skan
1362169689Skan   TSK indicates what kind of template declaration (if any) is being
1363169689Skan   declared.  TSK_TEMPLATE indicates that the declaration given by
1364169689Skan   DECL, though a FUNCTION_DECL, has template parameters, and is
1365169689Skan   therefore a template function.
1366169689Skan
136752284Sobrien   The template args (those explicitly specified and those deduced)
136852284Sobrien   are output in a newly created vector *TARGS_OUT.
136952284Sobrien
137052284Sobrien   If it is impossible to determine the result, an error message is
137152284Sobrien   issued.  The error_mark_node is returned to indicate failure.  */
137252284Sobrien
137352284Sobrienstatic tree
1374169689Skandetermine_specialization (tree template_id,
1375169689Skan			  tree decl,
1376169689Skan			  tree* targs_out,
1377169689Skan			  int need_member_template,
1378169689Skan			  int template_count,
1379169689Skan			  tmpl_spec_kind tsk)
138050397Sobrien{
138152284Sobrien  tree fns;
138252284Sobrien  tree targs;
138352284Sobrien  tree explicit_targs;
138452284Sobrien  tree candidates = NULL_TREE;
1385169689Skan  /* A TREE_LIST of templates of which DECL may be a specialization.
1386169689Skan     The TREE_VALUE of each node is a TEMPLATE_DECL.  The
1387169689Skan     corresponding TREE_PURPOSE is the set of template arguments that,
1388169689Skan     when used to instantiate the template, would produce a function
1389169689Skan     with the signature of DECL.  */
139050397Sobrien  tree templates = NULL_TREE;
1391169689Skan  int header_count;
1392169689Skan  struct cp_binding_level *b;
139350397Sobrien
139450397Sobrien  *targs_out = NULL_TREE;
139550397Sobrien
1396169689Skan  if (template_id == error_mark_node || decl == error_mark_node)
139750397Sobrien    return error_mark_node;
139850397Sobrien
139950397Sobrien  fns = TREE_OPERAND (template_id, 0);
140052284Sobrien  explicit_targs = TREE_OPERAND (template_id, 1);
140150397Sobrien
140250397Sobrien  if (fns == error_mark_node)
140350397Sobrien    return error_mark_node;
140450397Sobrien
1405117395Skan  /* Check for baselinks.  */
140690075Sobrien  if (BASELINK_P (fns))
1407117395Skan    fns = BASELINK_FUNCTIONS (fns);
140850397Sobrien
140990075Sobrien  if (!is_overloaded_fn (fns))
141090075Sobrien    {
1411169689Skan      error ("%qD is not a function template", fns);
141290075Sobrien      return error_mark_node;
141390075Sobrien    }
141490075Sobrien
1415169689Skan  /* Count the number of template headers specified for this
1416169689Skan     specialization.  */
1417169689Skan  header_count = 0;
1418169689Skan  for (b = current_binding_level;
1419169689Skan       b->kind == sk_template_parms;
1420169689Skan       b = b->level_chain)
1421169689Skan    ++header_count;
1422169689Skan
142350397Sobrien  for (; fns; fns = OVL_NEXT (fns))
142450397Sobrien    {
142590075Sobrien      tree fn = OVL_CURRENT (fns);
142652284Sobrien
142752284Sobrien      if (TREE_CODE (fn) == TEMPLATE_DECL)
1428117395Skan	{
1429117395Skan	  tree decl_arg_types;
1430132718Skan	  tree fn_arg_types;
1431117395Skan
1432169689Skan	  /* In case of explicit specialization, we need to check if
1433169689Skan	     the number of template headers appearing in the specialization
1434169689Skan	     is correct. This is usually done in check_explicit_specialization,
1435169689Skan	     but the check done there cannot be exhaustive when specializing
1436169689Skan	     member functions. Consider the following code:
1437169689Skan
1438169689Skan	     template <> void A<int>::f(int);
1439169689Skan	     template <> template <> void A<int>::f(int);
1440169689Skan
1441169689Skan	     Assuming that A<int> is not itself an explicit specialization
1442169689Skan	     already, the first line specializes "f" which is a non-template
1443169689Skan	     member function, whilst the second line specializes "f" which
1444169689Skan	     is a template member function. So both lines are syntactically
1445169689Skan	     correct, and check_explicit_specialization does not reject
1446169689Skan	     them.
1447169689Skan
1448169689Skan	     Here, we can do better, as we are matching the specialization
1449169689Skan	     against the declarations. We count the number of template
1450169689Skan	     headers, and we check if they match TEMPLATE_COUNT + 1
1451169689Skan	     (TEMPLATE_COUNT is the number of qualifying template classes,
1452169689Skan	     plus there must be another header for the member template
1453169689Skan	     itself).
1454169689Skan
1455169689Skan	     Notice that if header_count is zero, this is not a
1456169689Skan	     specialization but rather a template instantiation, so there
1457169689Skan	     is no check we can perform here.  */
1458169689Skan	  if (header_count && header_count != template_count + 1)
1459169689Skan	    continue;
1460169689Skan
1461169689Skan	  /* Check that the number of template arguments at the
1462169689Skan	     innermost level for DECL is the same as for FN.  */
1463169689Skan	  if (current_binding_level->kind == sk_template_parms
1464169689Skan	      && !current_binding_level->explicit_spec_p
1465169689Skan	      && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1466169689Skan		  != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1467169689Skan				      (current_template_parms))))
1468169689Skan	    continue;
1469169689Skan
1470117395Skan	  /* DECL might be a specialization of FN.  */
1471117395Skan	  decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1472169689Skan	  fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1473117395Skan
1474169689Skan	  /* For a non-static member function, we need to make sure
1475169689Skan	     that the const qualification is the same.  Since
1476169689Skan	     get_bindings does not try to merge the "this" parameter,
1477169689Skan	     we must do the comparison explicitly.  */
1478169689Skan	  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1479169689Skan	      && !same_type_p (TREE_VALUE (fn_arg_types),
1480169689Skan			       TREE_VALUE (decl_arg_types)))
1481169689Skan	    continue;
1482169689Skan
1483169689Skan	  /* Skip the "this" parameter and, for constructors of
1484169689Skan	     classes with virtual bases, the VTT parameter.  A
1485169689Skan	     full specialization of a constructor will have a VTT
1486169689Skan	     parameter, but a template never will.  */
1487169689Skan	  decl_arg_types
1488169689Skan	    = skip_artificial_parms_for (decl, decl_arg_types);
1489169689Skan	  fn_arg_types
1490169689Skan	    = skip_artificial_parms_for (fn, fn_arg_types);
1491169689Skan
1492117395Skan	  /* Check that the number of function parameters matches.
1493117395Skan	     For example,
1494117395Skan	       template <class T> void f(int i = 0);
1495117395Skan	       template <> void f<int>();
1496117395Skan	     The specialization f<int> is invalid but is not caught
1497117395Skan	     by get_bindings below.  */
1498132718Skan	  if (list_length (fn_arg_types) != list_length (decl_arg_types))
1499117395Skan	    continue;
1500117395Skan
1501169689Skan	  /* Function templates cannot be specializations; there are
1502169689Skan	     no partial specializations of functions.  Therefore, if
1503169689Skan	     the type of DECL does not match FN, there is no
1504169689Skan	     match.  */
1505169689Skan	  if (tsk == tsk_template)
1506169689Skan	    {
1507169689Skan	      if (compparms (fn_arg_types, decl_arg_types))
1508169689Skan		candidates = tree_cons (NULL_TREE, fn, candidates);
1509169689Skan	      continue;
1510169689Skan	    }
1511132718Skan
1512117395Skan	  /* See whether this function might be a specialization of this
1513117395Skan	     template.  */
1514169689Skan	  targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1515117395Skan
1516117395Skan	  if (!targs)
1517117395Skan	    /* We cannot deduce template arguments that when used to
1518117395Skan	       specialize TMPL will produce DECL.  */
1519117395Skan	    continue;
1520117395Skan
1521117395Skan	  /* Save this template, and the arguments deduced.  */
1522117395Skan	  templates = tree_cons (targs, fn, templates);
1523117395Skan	}
152452284Sobrien      else if (need_member_template)
152552284Sobrien	/* FN is an ordinary member function, and we need a
152652284Sobrien	   specialization of a member template.  */
1527117395Skan	;
152852284Sobrien      else if (TREE_CODE (fn) != FUNCTION_DECL)
152952284Sobrien	/* We can get IDENTIFIER_NODEs here in certain erroneous
153052284Sobrien	   cases.  */
1531117395Skan	;
153252284Sobrien      else if (!DECL_FUNCTION_MEMBER_P (fn))
153352284Sobrien	/* This is just an ordinary non-member function.  Nothing can
153452284Sobrien	   be a specialization of that.  */
1535117395Skan	;
153690075Sobrien      else if (DECL_ARTIFICIAL (fn))
153790075Sobrien	/* Cannot specialize functions that are created implicitly.  */
1538117395Skan	;
153950397Sobrien      else
154052284Sobrien	{
154152284Sobrien	  tree decl_arg_types;
154250397Sobrien
154352284Sobrien	  /* This is an ordinary member function.  However, since
154452284Sobrien	     we're here, we can assume it's enclosing class is a
154552284Sobrien	     template class.  For example,
1546169689Skan
154752284Sobrien	       template <typename T> struct S { void f(); };
154852284Sobrien	       template <> void S<int>::f() {}
154950397Sobrien
155052284Sobrien	     Here, S<int>::f is a non-template, but S<int> is a
155152284Sobrien	     template class.  If FN has the same type as DECL, we
155252284Sobrien	     might be in business.  */
155390075Sobrien
155490075Sobrien	  if (!DECL_TEMPLATE_INFO (fn))
155590075Sobrien	    /* Its enclosing class is an explicit specialization
155690075Sobrien	       of a template class.  This is not a candidate.  */
155790075Sobrien	    continue;
155890075Sobrien
155952284Sobrien	  if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
156052284Sobrien			    TREE_TYPE (TREE_TYPE (fn))))
156152284Sobrien	    /* The return types differ.  */
156252284Sobrien	    continue;
156350397Sobrien
156452284Sobrien	  /* Adjust the type of DECL in case FN is a static member.  */
156552284Sobrien	  decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1566169689Skan	  if (DECL_STATIC_FUNCTION_P (fn)
156752284Sobrien	      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
156852284Sobrien	    decl_arg_types = TREE_CHAIN (decl_arg_types);
156952284Sobrien
1570169689Skan	  if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
157152284Sobrien			 decl_arg_types))
157252284Sobrien	    /* They match!  */
157352284Sobrien	    candidates = tree_cons (NULL_TREE, fn, candidates);
157450397Sobrien	}
157550397Sobrien    }
157652284Sobrien
157752284Sobrien  if (templates && TREE_CHAIN (templates))
157850397Sobrien    {
157952284Sobrien      /* We have:
1580169689Skan
158152284Sobrien	   [temp.expl.spec]
158250397Sobrien
158352284Sobrien	   It is possible for a specialization with a given function
158452284Sobrien	   signature to be instantiated from more than one function
158552284Sobrien	   template.  In such cases, explicit specification of the
158652284Sobrien	   template arguments must be used to uniquely identify the
158752284Sobrien	   function template specialization being specialized.
158850397Sobrien
158952284Sobrien	 Note that here, there's no suggestion that we're supposed to
159052284Sobrien	 determine which of the candidate templates is most
159152284Sobrien	 specialized.  However, we, also have:
159252284Sobrien
159352284Sobrien	   [temp.func.order]
159452284Sobrien
159552284Sobrien	   Partial ordering of overloaded function template
159652284Sobrien	   declarations is used in the following contexts to select
159752284Sobrien	   the function template to which a function template
1598169689Skan	   specialization refers:
159952284Sobrien
1600169689Skan	   -- when an explicit specialization refers to a function
1601169689Skan	      template.
160252284Sobrien
160352284Sobrien	 So, we do use the partial ordering rules, at least for now.
1604117395Skan	 This extension can only serve to make invalid programs valid,
160552284Sobrien	 so it's safe.  And, there is strong anecdotal evidence that
160652284Sobrien	 the committee intended the partial ordering rules to apply;
160752284Sobrien	 the EDG front-end has that behavior, and John Spicer claims
160852284Sobrien	 that the committee simply forgot to delete the wording in
160952284Sobrien	 [temp.expl.spec].  */
1610169689Skan      tree tmpl = most_specialized_instantiation (templates);
1611169689Skan      if (tmpl != error_mark_node)
1612169689Skan	{
1613169689Skan	  templates = tmpl;
1614169689Skan	  TREE_CHAIN (templates) = NULL_TREE;
1615169689Skan	}
161650397Sobrien    }
161750397Sobrien
161852284Sobrien  if (templates == NULL_TREE && candidates == NULL_TREE)
161950397Sobrien    {
1620169689Skan      error ("template-id %qD for %q+D does not match any template "
1621169689Skan	     "declaration", template_id, decl);
162252284Sobrien      return error_mark_node;
162350397Sobrien    }
162452284Sobrien  else if ((templates && TREE_CHAIN (templates))
162552284Sobrien	   || (candidates && TREE_CHAIN (candidates))
162652284Sobrien	   || (templates && candidates))
162750397Sobrien    {
1628169689Skan      error ("ambiguous template specialization %qD for %q+D",
1629169689Skan	     template_id, decl);
163052284Sobrien      chainon (candidates, templates);
163152284Sobrien      print_candidates (candidates);
163252284Sobrien      return error_mark_node;
163350397Sobrien    }
163450397Sobrien
1635117395Skan  /* We have one, and exactly one, match.  */
163652284Sobrien  if (candidates)
163752284Sobrien    {
1638169689Skan      tree fn = TREE_VALUE (candidates);
1639169689Skan      /* DECL is a re-declaration of a template function.  */
1640169689Skan      if (TREE_CODE (fn) == TEMPLATE_DECL)
1641169689Skan	return fn;
164252284Sobrien      /* It was a specialization of an ordinary member function in a
164352284Sobrien	 template class.  */
1644169689Skan      *targs_out = copy_node (DECL_TI_ARGS (fn));
1645169689Skan      return DECL_TI_TEMPLATE (fn);
164652284Sobrien    }
164752284Sobrien
164852284Sobrien  /* It was a specialization of a template.  */
164990075Sobrien  targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
165052284Sobrien  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
165152284Sobrien    {
165252284Sobrien      *targs_out = copy_node (targs);
1653169689Skan      SET_TMPL_ARGS_LEVEL (*targs_out,
165452284Sobrien			   TMPL_ARGS_DEPTH (*targs_out),
165552284Sobrien			   TREE_PURPOSE (templates));
165652284Sobrien    }
165752284Sobrien  else
165852284Sobrien    *targs_out = TREE_PURPOSE (templates);
165950397Sobrien  return TREE_VALUE (templates);
166050397Sobrien}
166190075Sobrien
166290075Sobrien/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
166390075Sobrien   but with the default argument values filled in from those in the
166490075Sobrien   TMPL_TYPES.  */
1665169689Skan
166690075Sobrienstatic tree
1667132718Skancopy_default_args_to_explicit_spec_1 (tree spec_types,
1668132718Skan				      tree tmpl_types)
166990075Sobrien{
167090075Sobrien  tree new_spec_types;
167190075Sobrien
167290075Sobrien  if (!spec_types)
167390075Sobrien    return NULL_TREE;
167490075Sobrien
167590075Sobrien  if (spec_types == void_list_node)
167690075Sobrien    return void_list_node;
167790075Sobrien
167890075Sobrien  /* Substitute into the rest of the list.  */
167990075Sobrien  new_spec_types =
168090075Sobrien    copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
168190075Sobrien					  TREE_CHAIN (tmpl_types));
1682169689Skan
168390075Sobrien  /* Add the default argument for this parameter.  */
168490075Sobrien  return hash_tree_cons (TREE_PURPOSE (tmpl_types),
168590075Sobrien			 TREE_VALUE (spec_types),
168690075Sobrien			 new_spec_types);
168790075Sobrien}
168890075Sobrien
168990075Sobrien/* DECL is an explicit specialization.  Replicate default arguments
169090075Sobrien   from the template it specializes.  (That way, code like:
169190075Sobrien
169290075Sobrien     template <class T> void f(T = 3);
169390075Sobrien     template <> void f(double);
1694169689Skan     void g () { f (); }
169590075Sobrien
169690075Sobrien   works, as required.)  An alternative approach would be to look up
169790075Sobrien   the correct default arguments at the call-site, but this approach
169890075Sobrien   is consistent with how implicit instantiations are handled.  */
169990075Sobrien
170090075Sobrienstatic void
1701132718Skancopy_default_args_to_explicit_spec (tree decl)
170290075Sobrien{
170390075Sobrien  tree tmpl;
170490075Sobrien  tree spec_types;
170590075Sobrien  tree tmpl_types;
170690075Sobrien  tree new_spec_types;
170790075Sobrien  tree old_type;
170890075Sobrien  tree new_type;
170990075Sobrien  tree t;
171090075Sobrien  tree object_type = NULL_TREE;
171190075Sobrien  tree in_charge = NULL_TREE;
171290075Sobrien  tree vtt = NULL_TREE;
171390075Sobrien
171490075Sobrien  /* See if there's anything we need to do.  */
171590075Sobrien  tmpl = DECL_TI_TEMPLATE (decl);
171690075Sobrien  tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
171790075Sobrien  for (t = tmpl_types; t; t = TREE_CHAIN (t))
171890075Sobrien    if (TREE_PURPOSE (t))
171990075Sobrien      break;
172090075Sobrien  if (!t)
172190075Sobrien    return;
172290075Sobrien
172390075Sobrien  old_type = TREE_TYPE (decl);
172490075Sobrien  spec_types = TYPE_ARG_TYPES (old_type);
1725169689Skan
172690075Sobrien  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
172790075Sobrien    {
172890075Sobrien      /* Remove the this pointer, but remember the object's type for
1729169689Skan	 CV quals.  */
173090075Sobrien      object_type = TREE_TYPE (TREE_VALUE (spec_types));
173190075Sobrien      spec_types = TREE_CHAIN (spec_types);
173290075Sobrien      tmpl_types = TREE_CHAIN (tmpl_types);
1733169689Skan
173490075Sobrien      if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1735169689Skan	{
1736169689Skan	  /* DECL may contain more parameters than TMPL due to the extra
1737169689Skan	     in-charge parameter in constructors and destructors.  */
1738169689Skan	  in_charge = spec_types;
173990075Sobrien	  spec_types = TREE_CHAIN (spec_types);
174090075Sobrien	}
174190075Sobrien      if (DECL_HAS_VTT_PARM_P (decl))
174290075Sobrien	{
174390075Sobrien	  vtt = spec_types;
174490075Sobrien	  spec_types = TREE_CHAIN (spec_types);
174590075Sobrien	}
174690075Sobrien    }
174790075Sobrien
174890075Sobrien  /* Compute the merged default arguments.  */
1749169689Skan  new_spec_types =
175090075Sobrien    copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
175190075Sobrien
175290075Sobrien  /* Compute the new FUNCTION_TYPE.  */
175390075Sobrien  if (object_type)
175490075Sobrien    {
175590075Sobrien      if (vtt)
1756169689Skan	new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1757169689Skan					 TREE_VALUE (vtt),
1758169689Skan					 new_spec_types);
175990075Sobrien
176090075Sobrien      if (in_charge)
1761169689Skan	/* Put the in-charge parameter back.  */
1762169689Skan	new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1763169689Skan					 TREE_VALUE (in_charge),
1764169689Skan					 new_spec_types);
176590075Sobrien
1766132718Skan      new_type = build_method_type_directly (object_type,
1767132718Skan					     TREE_TYPE (old_type),
1768132718Skan					     new_spec_types);
176990075Sobrien    }
177090075Sobrien  else
177190075Sobrien    new_type = build_function_type (TREE_TYPE (old_type),
177290075Sobrien				    new_spec_types);
1773132718Skan  new_type = cp_build_type_attribute_variant (new_type,
1774132718Skan					      TYPE_ATTRIBUTES (old_type));
177590075Sobrien  new_type = build_exception_variant (new_type,
177690075Sobrien				      TYPE_RAISES_EXCEPTIONS (old_type));
177790075Sobrien  TREE_TYPE (decl) = new_type;
177890075Sobrien}
177990075Sobrien
178050397Sobrien/* Check to see if the function just declared, as indicated in
178150397Sobrien   DECLARATOR, and in DECL, is a specialization of a function
178250397Sobrien   template.  We may also discover that the declaration is an explicit
178350397Sobrien   instantiation at this point.
178450397Sobrien
178550397Sobrien   Returns DECL, or an equivalent declaration that should be used
178652284Sobrien   instead if all goes well.  Issues an error message if something is
178752284Sobrien   amiss.  Returns error_mark_node if the error is not easily
178852284Sobrien   recoverable.
178950397Sobrien
1790169689Skan   FLAGS is a bitmask consisting of the following flags:
1791169689Skan
179250397Sobrien   2: The function has a definition.
179350397Sobrien   4: The function is a friend.
179450397Sobrien
179550397Sobrien   The TEMPLATE_COUNT is the number of references to qualifying
179650397Sobrien   template classes that appeared in the name of the function.  For
179750397Sobrien   example, in
179850397Sobrien
179950397Sobrien     template <class T> struct S { void f(); };
180050397Sobrien     void S<int>::f();
1801169689Skan
180250397Sobrien   the TEMPLATE_COUNT would be 1.  However, explicitly specialized
180350397Sobrien   classes are not counted in the TEMPLATE_COUNT, so that in
180450397Sobrien
180550397Sobrien     template <class T> struct S {};
180650397Sobrien     template <> struct S<int> { void f(); }
180752284Sobrien     template <> void S<int>::f();
180850397Sobrien
180950397Sobrien   the TEMPLATE_COUNT would be 0.  (Note that this declaration is
1810117395Skan   invalid; there should be no template <>.)
181150397Sobrien
181250397Sobrien   If the function is a specialization, it is marked as such via
181350397Sobrien   DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
1814169689Skan   is set up correctly, and it is added to the list of specializations
181550397Sobrien   for that template.  */
181650397Sobrien
181750397Sobrientree
1818169689Skancheck_explicit_specialization (tree declarator,
1819169689Skan			       tree decl,
1820169689Skan			       int template_count,
1821169689Skan			       int flags)
182250397Sobrien{
182350397Sobrien  int have_def = flags & 2;
182450397Sobrien  int is_friend = flags & 4;
182550397Sobrien  int specialization = 0;
182650397Sobrien  int explicit_instantiation = 0;
182752284Sobrien  int member_specialization = 0;
182850397Sobrien  tree ctype = DECL_CLASS_CONTEXT (decl);
182950397Sobrien  tree dname = DECL_NAME (decl);
183090075Sobrien  tmpl_spec_kind tsk;
183150397Sobrien
1832169689Skan  if (is_friend)
1833169689Skan    {
1834169689Skan      if (!processing_specialization)
1835169689Skan	tsk = tsk_none;
1836169689Skan      else
1837169689Skan	tsk = tsk_excessive_parms;
1838169689Skan    }
1839169689Skan  else
1840169689Skan    tsk = current_tmpl_spec_kind (template_count);
184190075Sobrien
184290075Sobrien  switch (tsk)
184350397Sobrien    {
184490075Sobrien    case tsk_none:
1845169689Skan      if (processing_specialization)
184650397Sobrien	{
184752284Sobrien	  specialization = 1;
184852284Sobrien	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
184952284Sobrien	}
185090075Sobrien      else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
185152284Sobrien	{
185290075Sobrien	  if (is_friend)
185390075Sobrien	    /* This could be something like:
185450397Sobrien
185590075Sobrien	       template <class T> void f(T);
185690075Sobrien	       class S { friend void f<>(int); }  */
185790075Sobrien	    specialization = 1;
185890075Sobrien	  else
185990075Sobrien	    {
186090075Sobrien	      /* This case handles bogus declarations like template <>
186190075Sobrien		 template <class T> void f<int>(); */
186290075Sobrien
1863169689Skan	      error ("template-id %qD in declaration of primary template",
1864169689Skan		     declarator);
186590075Sobrien	      return decl;
186690075Sobrien	    }
186790075Sobrien	}
186890075Sobrien      break;
186990075Sobrien
187090075Sobrien    case tsk_invalid_member_spec:
187190075Sobrien      /* The error has already been reported in
187290075Sobrien	 check_specialization_scope.  */
187390075Sobrien      return error_mark_node;
187490075Sobrien
187590075Sobrien    case tsk_invalid_expl_inst:
187690075Sobrien      error ("template parameter list used in explicit instantiation");
187790075Sobrien
187890075Sobrien      /* Fall through.  */
187990075Sobrien
188090075Sobrien    case tsk_expl_inst:
188152284Sobrien      if (have_def)
188290075Sobrien	error ("definition provided for explicit instantiation");
1883169689Skan
188452284Sobrien      explicit_instantiation = 1;
188590075Sobrien      break;
188650397Sobrien
188790075Sobrien    case tsk_excessive_parms:
1888169689Skan    case tsk_insufficient_parms:
1889169689Skan      if (tsk == tsk_excessive_parms)
1890169689Skan	error ("too many template parameter lists in declaration of %qD",
1891169689Skan	       decl);
1892169689Skan      else if (template_header_count)
1893169689Skan	error("too few template parameter lists in declaration of %qD", decl);
1894169689Skan      else
1895169689Skan	error("explicit specialization of %qD must be introduced by "
1896169689Skan	      "%<template <>%>", decl);
189750397Sobrien
189890075Sobrien      /* Fall through.  */
189990075Sobrien    case tsk_expl_spec:
190090075Sobrien      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
190190075Sobrien      if (ctype)
190290075Sobrien	member_specialization = 1;
190390075Sobrien      else
190490075Sobrien	specialization = 1;
190590075Sobrien      break;
190650397Sobrien
190790075Sobrien    case tsk_template:
190890075Sobrien      if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
190950397Sobrien	{
191052284Sobrien	  /* This case handles bogus declarations like template <>
191152284Sobrien	     template <class T> void f<int>(); */
191250397Sobrien
191390075Sobrien	  if (uses_template_parms (declarator))
1914169689Skan	    error ("function template partial specialization %qD "
1915169689Skan		   "is not allowed", declarator);
191690075Sobrien	  else
1917169689Skan	    error ("template-id %qD in declaration of primary template",
1918169689Skan		   declarator);
191950397Sobrien	  return decl;
192050397Sobrien	}
192190075Sobrien
192290075Sobrien      if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
192390075Sobrien	/* This is a specialization of a member template, without
192490075Sobrien	   specialization the containing class.  Something like:
192590075Sobrien
192690075Sobrien	     template <class T> struct S {
1927169689Skan	       template <class U> void f (U);
1928169689Skan	     };
192990075Sobrien	     template <> template <class U> void S<int>::f(U) {}
1930169689Skan
193190075Sobrien	   That's a specialization -- but of the entire template.  */
193290075Sobrien	specialization = 1;
193390075Sobrien      break;
193490075Sobrien
193590075Sobrien    default:
1936169689Skan      gcc_unreachable ();
193750397Sobrien    }
193850397Sobrien
193950397Sobrien  if (specialization || member_specialization)
194050397Sobrien    {
194150397Sobrien      tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
194250397Sobrien      for (; t; t = TREE_CHAIN (t))
194350397Sobrien	if (TREE_PURPOSE (t))
194450397Sobrien	  {
194590075Sobrien	    pedwarn
194650397Sobrien	      ("default argument specified in explicit specialization");
194750397Sobrien	    break;
194850397Sobrien	  }
194950397Sobrien    }
195050397Sobrien
195150397Sobrien  if (specialization || member_specialization || explicit_instantiation)
195250397Sobrien    {
195350397Sobrien      tree tmpl = NULL_TREE;
195450397Sobrien      tree targs = NULL_TREE;
195550397Sobrien
195650397Sobrien      /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
195750397Sobrien      if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
195850397Sobrien	{
195950397Sobrien	  tree fns;
196050397Sobrien
1961169689Skan	  gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1962132718Skan	  if (ctype)
1963132718Skan	    fns = dname;
196450397Sobrien	  else
1965132718Skan	    {
1966132718Skan	      /* If there is no class context, the explicit instantiation
1967169689Skan		 must be at namespace scope.  */
1968169689Skan	      gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
196950397Sobrien
1970132718Skan	      /* Find the namespace binding, using the declaration
1971169689Skan		 context.  */
1972169689Skan	      fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1973169689Skan					   false, true);
1974220150Smm	      if (fns == error_mark_node || !is_overloaded_fn (fns))
1975161651Skan		{
1976169689Skan		  error ("%qD is not a template function", dname);
1977161651Skan		  fns = error_mark_node;
1978161651Skan		}
1979169689Skan	      else
1980169689Skan		{
1981169689Skan		  tree fn = OVL_CURRENT (fns);
1982169689Skan		  if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1983169689Skan						CP_DECL_CONTEXT (fn)))
1984169689Skan		    error ("%qD is not declared in %qD",
1985169689Skan			   decl, current_namespace);
1986169689Skan		}
1987132718Skan	    }
1988132718Skan
1989132718Skan	  declarator = lookup_template_function (fns, NULL_TREE);
199050397Sobrien	}
199150397Sobrien
199250397Sobrien      if (declarator == error_mark_node)
199350397Sobrien	return error_mark_node;
199450397Sobrien
199550397Sobrien      if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
199650397Sobrien	{
199750397Sobrien	  if (!explicit_instantiation)
1998117395Skan	    /* A specialization in class scope.  This is invalid,
199952284Sobrien	       but the error will already have been flagged by
200052284Sobrien	       check_specialization_scope.  */
200152284Sobrien	    return error_mark_node;
200252284Sobrien	  else
200350397Sobrien	    {
2004117395Skan	      /* It's not valid to write an explicit instantiation in
200552284Sobrien		 class scope, e.g.:
200650397Sobrien
2007169689Skan		   class C { template void f(); }
200850397Sobrien
200952284Sobrien		   This case is caught by the parser.  However, on
201052284Sobrien		   something like:
2011169689Skan
201252284Sobrien		   template class C { void f(); };
201350397Sobrien
2014117395Skan		   (which is invalid) we can get here.  The error will be
201552284Sobrien		   issued later.  */
201652284Sobrien	      ;
201752284Sobrien	    }
201850397Sobrien
201950397Sobrien	  return decl;
202050397Sobrien	}
2021169689Skan      else if (ctype != NULL_TREE
202250397Sobrien	       && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
202350397Sobrien		   IDENTIFIER_NODE))
202450397Sobrien	{
202550397Sobrien	  /* Find the list of functions in ctype that have the same
202650397Sobrien	     name as the declared function.  */
202750397Sobrien	  tree name = TREE_OPERAND (declarator, 0);
202852284Sobrien	  tree fns = NULL_TREE;
202952284Sobrien	  int idx;
203052284Sobrien
2031117395Skan	  if (constructor_name_p (name, ctype))
203250397Sobrien	    {
203350397Sobrien	      int is_constructor = DECL_CONSTRUCTOR_P (decl);
2034169689Skan
203550397Sobrien	      if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2036169689Skan		  : !CLASSTYPE_DESTRUCTORS (ctype))
203750397Sobrien		{
203850397Sobrien		  /* From [temp.expl.spec]:
2039169689Skan
204050397Sobrien		     If such an explicit specialization for the member
204150397Sobrien		     of a class template names an implicitly-declared
204250397Sobrien		     special member function (clause _special_), the
2043169689Skan		     program is ill-formed.
204450397Sobrien
204550397Sobrien		     Similar language is found in [temp.explicit].  */
204690075Sobrien		  error ("specialization of implicitly-declared special member function");
204752284Sobrien		  return error_mark_node;
204850397Sobrien		}
204950397Sobrien
205050397Sobrien	      name = is_constructor ? ctor_identifier : dtor_identifier;
205150397Sobrien	    }
205250397Sobrien
205390075Sobrien	  if (!DECL_CONV_FN_P (decl))
205452284Sobrien	    {
205552284Sobrien	      idx = lookup_fnfields_1 (ctype, name);
205652284Sobrien	      if (idx >= 0)
2057169689Skan		fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
205852284Sobrien	    }
205952284Sobrien	  else
206052284Sobrien	    {
2061169689Skan	      VEC(tree,gc) *methods;
2062169689Skan	      tree ovl;
206352284Sobrien
206452284Sobrien	      /* For a type-conversion operator, we cannot do a
206552284Sobrien		 name-based lookup.  We might be looking for `operator
206652284Sobrien		 int' which will be a specialization of `operator T'.
206752284Sobrien		 So, we find *all* the conversion operators, and then
206852284Sobrien		 select from them.  */
206952284Sobrien	      fns = NULL_TREE;
207052284Sobrien
207152284Sobrien	      methods = CLASSTYPE_METHOD_VEC (ctype);
207252284Sobrien	      if (methods)
2073117395Skan		for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2074169689Skan		     VEC_iterate (tree, methods, idx, ovl);
2075169689Skan		     ++idx)
207652284Sobrien		  {
2077169689Skan		    if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
207852284Sobrien		      /* There are no more conversion functions.  */
207952284Sobrien		      break;
208052284Sobrien
208152284Sobrien		    /* Glue all these conversion functions together
208252284Sobrien		       with those we already have.  */
208352284Sobrien		    for (; ovl; ovl = OVL_NEXT (ovl))
208452284Sobrien		      fns = ovl_cons (OVL_CURRENT (ovl), fns);
208552284Sobrien		  }
208652284Sobrien	    }
2087169689Skan
2088169689Skan	  if (fns == NULL_TREE)
208950397Sobrien	    {
2090169689Skan	      error ("no member function %qD declared in %qT", name, ctype);
209152284Sobrien	      return error_mark_node;
209250397Sobrien	    }
209350397Sobrien	  else
209450397Sobrien	    TREE_OPERAND (declarator, 0) = fns;
209550397Sobrien	}
2096169689Skan
209750397Sobrien      /* Figure out what exactly is being specialized at this point.
209850397Sobrien	 Note that for an explicit instantiation, even one for a
209950397Sobrien	 member function, we cannot tell apriori whether the
210050397Sobrien	 instantiation is for a member template, or just a member
210152284Sobrien	 function of a template class.  Even if a member template is
210252284Sobrien	 being instantiated, the member template arguments may be
210352284Sobrien	 elided if they can be deduced from the rest of the
210452284Sobrien	 declaration.  */
210550397Sobrien      tmpl = determine_specialization (declarator, decl,
2106169689Skan				       &targs,
2107169689Skan				       member_specialization,
2108169689Skan				       template_count,
2109169689Skan				       tsk);
2110169689Skan
211152284Sobrien      if (!tmpl || tmpl == error_mark_node)
211252284Sobrien	/* We couldn't figure out what this declaration was
211352284Sobrien	   specializing.  */
211452284Sobrien	return error_mark_node;
211552284Sobrien      else
211650397Sobrien	{
211752284Sobrien	  tree gen_tmpl = most_general_template (tmpl);
211852284Sobrien
211950397Sobrien	  if (explicit_instantiation)
212050397Sobrien	    {
212152284Sobrien	      /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2122169689Skan		 is done by do_decl_instantiation later.  */
212352284Sobrien
212452284Sobrien	      int arg_depth = TMPL_ARGS_DEPTH (targs);
212552284Sobrien	      int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
212652284Sobrien
212752284Sobrien	      if (arg_depth > parm_depth)
212852284Sobrien		{
212952284Sobrien		  /* If TMPL is not the most general template (for
213052284Sobrien		     example, if TMPL is a friend template that is
213152284Sobrien		     injected into namespace scope), then there will
213290075Sobrien		     be too many levels of TARGS.  Remove some of them
213352284Sobrien		     here.  */
213452284Sobrien		  int i;
213552284Sobrien		  tree new_targs;
213652284Sobrien
213790075Sobrien		  new_targs = make_tree_vec (parm_depth);
213852284Sobrien		  for (i = arg_depth - parm_depth; i < arg_depth; ++i)
213952284Sobrien		    TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
214052284Sobrien		      = TREE_VEC_ELT (targs, i);
214152284Sobrien		  targs = new_targs;
214252284Sobrien		}
2143169689Skan
2144132718Skan	      return instantiate_template (tmpl, targs, tf_error);
214550397Sobrien	    }
214690075Sobrien
2147117395Skan	  /* If we thought that the DECL was a member function, but it
2148117395Skan	     turns out to be specializing a static member function,
2149132718Skan	     make DECL a static member function as well.  */
2150117395Skan	  if (DECL_STATIC_FUNCTION_P (tmpl)
2151117395Skan	      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2152132718Skan	    revert_static_member_fn (decl);
2153117395Skan
215490075Sobrien	  /* If this is a specialization of a member template of a
2155169689Skan	     template class, we want to return the TEMPLATE_DECL, not
2156169689Skan	     the specialization of it.  */
215790075Sobrien	  if (tsk == tsk_template)
215890075Sobrien	    {
215990075Sobrien	      SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
216090075Sobrien	      DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2161132718Skan	      if (have_def)
2162132718Skan		{
2163132718Skan		  DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2164132718Skan		  DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2165132718Skan		    = DECL_SOURCE_LOCATION (decl);
2166146895Skan		  /* We want to use the argument list specified in the
2167146895Skan		     definition, not in the original declaration.  */
2168146895Skan		  DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2169146895Skan		    = DECL_ARGUMENTS (decl);
2170132718Skan		}
217190075Sobrien	      return tmpl;
217290075Sobrien	    }
217390075Sobrien
217452284Sobrien	  /* Set up the DECL_TEMPLATE_INFO for DECL.  */
217590075Sobrien	  DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
217652284Sobrien
217790075Sobrien	  /* Inherit default function arguments from the template
217890075Sobrien	     DECL is specializing.  */
217990075Sobrien	  copy_default_args_to_explicit_spec (decl);
218052284Sobrien
218190075Sobrien	  /* This specialization has the same protection as the
218290075Sobrien	     template it specializes.  */
218390075Sobrien	  TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
218490075Sobrien	  TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
218552284Sobrien
2186169689Skan	  /* If DECL is a friend declaration, declared using an
2187169689Skan	     unqualified name, the namespace associated with DECL may
2188169689Skan	     have been set incorrectly.  For example, in:
2189169689Skan
2190169689Skan	       template <typename T> void f(T);
2191169689Skan	       namespace N {
2192169689Skan		 struct S { friend void f<int>(int); }
2193169689Skan	       }
2194169689Skan
2195169689Skan	     we will have set the DECL_CONTEXT for the friend
2196169689Skan	     declaration to N, rather than to the global namespace.  */
2197169689Skan	  if (DECL_NAMESPACE_SCOPE_P (decl))
2198169689Skan	    DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2199169689Skan
220052284Sobrien	  if (is_friend && !have_def)
220152284Sobrien	    /* This is not really a declaration of a specialization.
220252284Sobrien	       It's just the name of an instantiation.  But, it's not
220352284Sobrien	       a request for an instantiation, either.  */
220452284Sobrien	    SET_DECL_IMPLICIT_INSTANTIATION (decl);
220590075Sobrien	  else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
220690075Sobrien	    /* This is indeed a specialization.  In case of constructors
220790075Sobrien	       and destructors, we need in-charge and not-in-charge
220890075Sobrien	       versions in V3 ABI.  */
220990075Sobrien	    clone_function_decl (decl, /*update_method_vec_p=*/0);
221050397Sobrien
221152284Sobrien	  /* Register this specialization so that we can find it
221252284Sobrien	     again.  */
2213169689Skan	  decl = register_specialization (decl, gen_tmpl, targs, is_friend);
221452284Sobrien	}
221552284Sobrien    }
2216169689Skan
221752284Sobrien  return decl;
221852284Sobrien}
221950397Sobrien
222050397Sobrien/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
222150397Sobrien   parameters.  These are represented in the same format used for
222250397Sobrien   DECL_TEMPLATE_PARMS.  */
222350397Sobrien
2224169689Skanint
2225169689Skancomp_template_parms (tree parms1, tree parms2)
222650397Sobrien{
222750397Sobrien  tree p1;
222850397Sobrien  tree p2;
222950397Sobrien
223050397Sobrien  if (parms1 == parms2)
223150397Sobrien    return 1;
223250397Sobrien
2233169689Skan  for (p1 = parms1, p2 = parms2;
223450397Sobrien       p1 != NULL_TREE && p2 != NULL_TREE;
223550397Sobrien       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
223650397Sobrien    {
223750397Sobrien      tree t1 = TREE_VALUE (p1);
223850397Sobrien      tree t2 = TREE_VALUE (p2);
223950397Sobrien      int i;
224050397Sobrien
2241169689Skan      gcc_assert (TREE_CODE (t1) == TREE_VEC);
2242169689Skan      gcc_assert (TREE_CODE (t2) == TREE_VEC);
224350397Sobrien
224450397Sobrien      if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
224550397Sobrien	return 0;
224650397Sobrien
2247169689Skan      for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
224850397Sobrien	{
2249169689Skan          tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2250169689Skan          tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
225150397Sobrien
2252169689Skan          /* If either of the template parameters are invalid, assume
2253169689Skan             they match for the sake of error recovery. */
2254169689Skan          if (parm1 == error_mark_node || parm2 == error_mark_node)
2255169689Skan            return 1;
2256169689Skan
225750397Sobrien	  if (TREE_CODE (parm1) != TREE_CODE (parm2))
225850397Sobrien	    return 0;
225950397Sobrien
226050397Sobrien	  if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
226150397Sobrien	    continue;
226252284Sobrien	  else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
226350397Sobrien	    return 0;
226450397Sobrien	}
226550397Sobrien    }
226650397Sobrien
226750397Sobrien  if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
226850397Sobrien    /* One set of parameters has more parameters lists than the
226950397Sobrien       other.  */
227050397Sobrien    return 0;
227150397Sobrien
227250397Sobrien  return 1;
227350397Sobrien}
227450397Sobrien
227552284Sobrien/* Complain if DECL shadows a template parameter.
227652284Sobrien
227752284Sobrien   [temp.local]: A template-parameter shall not be redeclared within its
227852284Sobrien   scope (including nested scopes).  */
227952284Sobrien
228052284Sobrienvoid
2281132718Skancheck_template_shadow (tree decl)
228252284Sobrien{
228352284Sobrien  tree olddecl;
228452284Sobrien
228552284Sobrien  /* If we're not in a template, we can't possibly shadow a template
228652284Sobrien     parameter.  */
228752284Sobrien  if (!current_template_parms)
228852284Sobrien    return;
228952284Sobrien
229052284Sobrien  /* Figure out what we're shadowing.  */
229152284Sobrien  if (TREE_CODE (decl) == OVERLOAD)
229252284Sobrien    decl = OVL_CURRENT (decl);
2293169689Skan  olddecl = innermost_non_namespace_value (DECL_NAME (decl));
229452284Sobrien
229552284Sobrien  /* If there's no previous binding for this name, we're not shadowing
229652284Sobrien     anything, let alone a template parameter.  */
229752284Sobrien  if (!olddecl)
229852284Sobrien    return;
229952284Sobrien
230052284Sobrien  /* If we're not shadowing a template parameter, we're done.  Note
230152284Sobrien     that OLDDECL might be an OVERLOAD (or perhaps even an
230252284Sobrien     ERROR_MARK), so we can't just blithely assume it to be a _DECL
230352284Sobrien     node.  */
230490075Sobrien  if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
230552284Sobrien    return;
230652284Sobrien
230752284Sobrien  /* We check for decl != olddecl to avoid bogus errors for using a
230852284Sobrien     name inside a class.  We check TPFI to avoid duplicate errors for
230952284Sobrien     inline member templates.  */
2310169689Skan  if (decl == olddecl
231152284Sobrien      || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
231252284Sobrien    return;
231352284Sobrien
2314169689Skan  error ("declaration of %q+#D", decl);
2315169689Skan  error (" shadows template parm %q+#D", olddecl);
231652284Sobrien}
231752284Sobrien
231850397Sobrien/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
231950397Sobrien   ORIG_LEVEL, DECL, and TYPE.  */
232050397Sobrien
232150397Sobrienstatic tree
2322169689Skanbuild_template_parm_index (int index,
2323169689Skan			   int level,
2324169689Skan			   int orig_level,
2325169689Skan			   tree decl,
2326169689Skan			   tree type)
232750397Sobrien{
232850397Sobrien  tree t = make_node (TEMPLATE_PARM_INDEX);
232950397Sobrien  TEMPLATE_PARM_IDX (t) = index;
233050397Sobrien  TEMPLATE_PARM_LEVEL (t) = level;
233150397Sobrien  TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
233250397Sobrien  TEMPLATE_PARM_DECL (t) = decl;
233350397Sobrien  TREE_TYPE (t) = type;
2334132718Skan  TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2335169689Skan  TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2336132718Skan  TREE_READONLY (t) = TREE_READONLY (decl);
233750397Sobrien
233850397Sobrien  return t;
233950397Sobrien}
234050397Sobrien
234150397Sobrien/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
234250397Sobrien   TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
234350397Sobrien   TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
234450397Sobrien   new one is created.  */
234550397Sobrien
2346169689Skanstatic tree
2347132718Skanreduce_template_parm_level (tree index, tree type, int levels)
234850397Sobrien{
234950397Sobrien  if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
235050397Sobrien      || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
235150397Sobrien	  != TEMPLATE_PARM_LEVEL (index) - levels))
235250397Sobrien    {
2353132718Skan      tree orig_decl = TEMPLATE_PARM_DECL (index);
2354132718Skan      tree decl, t;
2355169689Skan
2356132718Skan      decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2357132718Skan      TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2358169689Skan      TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2359132718Skan      TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2360132718Skan      DECL_ARTIFICIAL (decl) = 1;
2361132718Skan      SET_DECL_TEMPLATE_PARM_P (decl);
2362169689Skan
2363132718Skan      t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
236450397Sobrien				     TEMPLATE_PARM_LEVEL (index) - levels,
236550397Sobrien				     TEMPLATE_PARM_ORIG_LEVEL (index),
236650397Sobrien				     decl, type);
236750397Sobrien      TEMPLATE_PARM_DESCENDANTS (index) = t;
236850397Sobrien
2369169689Skan	/* Template template parameters need this.  */
2370169689Skan      if (TREE_CODE (decl) != CONST_DECL)
2371169689Skan	DECL_TEMPLATE_PARMS (decl)
2372169689Skan	  = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
237350397Sobrien    }
237450397Sobrien
237550397Sobrien  return TEMPLATE_PARM_DESCENDANTS (index);
237650397Sobrien}
237750397Sobrien
2378169689Skan/* Process information from new template parameter PARM and append it to the
2379169689Skan   LIST being built.  This new parameter is a non-type parameter iff
2380169689Skan   IS_NON_TYPE is true.  */
238150397Sobrien
238218334Spetertree
2383169689Skanprocess_template_parm (tree list, tree parm, bool is_non_type)
238418334Speter{
238518334Speter  tree decl = 0;
238618334Speter  tree defval;
2387169689Skan  tree err_parm_list;
2388169689Skan  int idx = 0;
238950397Sobrien
2390169689Skan  gcc_assert (TREE_CODE (parm) == TREE_LIST);
239118334Speter  defval = TREE_PURPOSE (parm);
239250397Sobrien
239350397Sobrien  if (list)
239450397Sobrien    {
2395169689Skan      tree p = tree_last (list);
239650397Sobrien
2397169689Skan      if (p && TREE_VALUE (p) != error_mark_node)
2398169689Skan        {
2399169689Skan          p = TREE_VALUE (p);
2400169689Skan          if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2401169689Skan            idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2402169689Skan          else
2403169689Skan            idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2404169689Skan        }
2405169689Skan
240650397Sobrien      ++idx;
240750397Sobrien    }
240850397Sobrien  else
240950397Sobrien    idx = 0;
241050397Sobrien
2411169689Skan  if (is_non_type)
241218334Speter    {
2413169689Skan      parm = TREE_VALUE (parm);
2414169689Skan
241590075Sobrien      SET_DECL_TEMPLATE_PARM_P (parm);
241652284Sobrien
2417169689Skan      if (TREE_TYPE (parm) == error_mark_node)
2418169689Skan        {
2419169689Skan          err_parm_list = build_tree_list (defval, parm);
2420169689Skan          TREE_VALUE (err_parm_list) = error_mark_node;
2421169689Skan	   return chainon (list, err_parm_list);
2422169689Skan        }
2423169689Skan      else
2424169689Skan      {
2425169689Skan	/* [temp.param]
242652284Sobrien
2427169689Skan	   The top-level cv-qualifiers on the template-parameter are
2428169689Skan	   ignored when determining its type.  */
2429169689Skan	TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2430169689Skan	if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2431169689Skan          {
2432169689Skan            err_parm_list = build_tree_list (defval, parm);
2433169689Skan            TREE_VALUE (err_parm_list) = error_mark_node;
2434169689Skan	     return chainon (list, err_parm_list);
2435169689Skan          }
2436169689Skan      }
243752284Sobrien
243818334Speter      /* A template parameter is not modifiable.  */
2439169689Skan      TREE_CONSTANT (parm) = 1;
2440169689Skan      TREE_INVARIANT (parm) = 1;
2441169689Skan      TREE_READONLY (parm) = 1;
244218334Speter      decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2443169689Skan      TREE_CONSTANT (decl) = 1;
2444169689Skan      TREE_INVARIANT (decl) = 1;
2445169689Skan      TREE_READONLY (decl) = 1;
2446169689Skan      DECL_INITIAL (parm) = DECL_INITIAL (decl)
244750397Sobrien	= build_template_parm_index (idx, processing_template_decl,
244850397Sobrien				     processing_template_decl,
244950397Sobrien				     decl, TREE_TYPE (parm));
245018334Speter    }
245118334Speter  else
245218334Speter    {
245350397Sobrien      tree t;
2454169689Skan      parm = TREE_VALUE (TREE_VALUE (parm));
2455169689Skan
245650397Sobrien      if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
245718334Speter	{
245890075Sobrien	  t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2459169689Skan	  /* This is for distinguishing between real templates and template
246050397Sobrien	     template parameters */
246150397Sobrien	  TREE_TYPE (parm) = t;
246250397Sobrien	  TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
246350397Sobrien	  decl = parm;
246418334Speter	}
246550397Sobrien      else
246650397Sobrien	{
246790075Sobrien	  t = make_aggr_type (TEMPLATE_TYPE_PARM);
2468132718Skan	  /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
246950397Sobrien	  decl = build_decl (TYPE_DECL, parm, t);
247050397Sobrien	}
2471169689Skan
247250397Sobrien      TYPE_NAME (t) = decl;
247350397Sobrien      TYPE_STUB_DECL (t) = decl;
247450397Sobrien      parm = decl;
247550397Sobrien      TEMPLATE_TYPE_PARM_INDEX (t)
2476169689Skan	= build_template_parm_index (idx, processing_template_decl,
247750397Sobrien				     processing_template_decl,
247850397Sobrien				     decl, TREE_TYPE (parm));
247918334Speter    }
248090075Sobrien  DECL_ARTIFICIAL (decl) = 1;
248190075Sobrien  SET_DECL_TEMPLATE_PARM_P (decl);
248218334Speter  pushdecl (decl);
248318334Speter  parm = build_tree_list (defval, parm);
248418334Speter  return chainon (list, parm);
248518334Speter}
248618334Speter
248718334Speter/* The end of a template parameter list has been reached.  Process the
248818334Speter   tree list into a parameter vector, converting each parameter into a more
248918334Speter   useful form.	 Type parameters are saved as IDENTIFIER_NODEs, and others
249018334Speter   as PARM_DECLs.  */
249118334Speter
249218334Spetertree
2493132718Skanend_template_parm_list (tree parms)
249418334Speter{
249550397Sobrien  int nparms;
249690075Sobrien  tree parm, next;
249750397Sobrien  tree saved_parmlist = make_tree_vec (list_length (parms));
249818334Speter
249950397Sobrien  current_template_parms
250090075Sobrien    = tree_cons (size_int (processing_template_decl),
250150397Sobrien		 saved_parmlist, current_template_parms);
250250397Sobrien
250390075Sobrien  for (parm = parms, nparms = 0; parm; parm = next, nparms++)
250490075Sobrien    {
250590075Sobrien      next = TREE_CHAIN (parm);
250690075Sobrien      TREE_VEC_ELT (saved_parmlist, nparms) = parm;
250790075Sobrien      TREE_CHAIN (parm) = NULL_TREE;
250890075Sobrien    }
250950397Sobrien
251050397Sobrien  --processing_template_parmlist;
251150397Sobrien
251250397Sobrien  return saved_parmlist;
251350397Sobrien}
251450397Sobrien
251550397Sobrien/* end_template_decl is called after a template declaration is seen.  */
251650397Sobrien
251750397Sobrienvoid
2518132718Skanend_template_decl (void)
251950397Sobrien{
252050397Sobrien  reset_specialization ();
252150397Sobrien
252250397Sobrien  if (! processing_template_decl)
252350397Sobrien    return;
252450397Sobrien
252550397Sobrien  /* This matches the pushlevel in begin_template_parm_list.  */
252690075Sobrien  finish_scope ();
252750397Sobrien
252850397Sobrien  --processing_template_decl;
252950397Sobrien  current_template_parms = TREE_CHAIN (current_template_parms);
253050397Sobrien}
253150397Sobrien
253252284Sobrien/* Given a template argument vector containing the template PARMS.
253352284Sobrien   The innermost PARMS are given first.  */
253450397Sobrien
2535169689Skanstatic tree
2536132718Skancurrent_template_args (void)
253750397Sobrien{
253852284Sobrien  tree header;
253952284Sobrien  tree args = NULL_TREE;
254052284Sobrien  int length = TMPL_PARMS_DEPTH (current_template_parms);
254150397Sobrien  int l = length;
254250397Sobrien
254352284Sobrien  /* If there is only one level of template parameters, we do not
254452284Sobrien     create a TREE_VEC of TREE_VECs.  Instead, we return a single
254552284Sobrien     TREE_VEC containing the arguments.  */
254652284Sobrien  if (length > 1)
254752284Sobrien    args = make_tree_vec (length);
254852284Sobrien
254952284Sobrien  for (header = current_template_parms; header; header = TREE_CHAIN (header))
255018334Speter    {
255150397Sobrien      tree a = copy_node (TREE_VALUE (header));
255252284Sobrien      int i;
255352284Sobrien
255450397Sobrien      TREE_TYPE (a) = NULL_TREE;
255552284Sobrien      for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
255618334Speter	{
255750397Sobrien	  tree t = TREE_VEC_ELT (a, i);
255850397Sobrien
255952284Sobrien	  /* T will be a list if we are called from within a
256050397Sobrien	     begin/end_template_parm_list pair, but a vector directly
256150397Sobrien	     if within a begin/end_member_template_processing pair.  */
2562169689Skan	  if (TREE_CODE (t) == TREE_LIST)
256350397Sobrien	    {
256450397Sobrien	      t = TREE_VALUE (t);
2565169689Skan
2566169689Skan	      if (t != error_mark_node)
2567169689Skan		{
2568169689Skan		  if (TREE_CODE (t) == TYPE_DECL
2569169689Skan		      || TREE_CODE (t) == TEMPLATE_DECL)
2570169689Skan		    t = TREE_TYPE (t);
2571169689Skan		  else
2572169689Skan		    t = DECL_INITIAL (t);
2573169689Skan		}
2574169689Skan
257552284Sobrien	      TREE_VEC_ELT (a, i) = t;
257650397Sobrien	    }
257752284Sobrien	}
257850397Sobrien
257952284Sobrien      if (length > 1)
258052284Sobrien	TREE_VEC_ELT (args, --l) = a;
258152284Sobrien      else
258252284Sobrien	args = a;
258350397Sobrien    }
258418334Speter
258550397Sobrien  return args;
258650397Sobrien}
258750397Sobrien
258850397Sobrien/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2589169689Skan   template PARMS.  If MEMBER_TEMPLATE_P is true, the new template is
2590169689Skan   a member template.  Used by push_template_decl below.  */
259150397Sobrien
259250397Sobrienstatic tree
2593169689Skanbuild_template_decl (tree decl, tree parms, bool member_template_p)
259450397Sobrien{
259550397Sobrien  tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
259650397Sobrien  DECL_TEMPLATE_PARMS (tmpl) = parms;
259750397Sobrien  DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2598169689Skan  DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
259950397Sobrien  if (DECL_LANG_SPECIFIC (decl))
260050397Sobrien    {
260152284Sobrien      DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
260252284Sobrien      DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2603117395Skan      DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
260490075Sobrien      DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
260590075Sobrien      DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
260690075Sobrien      if (DECL_OVERLOADED_OPERATOR_P (decl))
2607169689Skan	SET_OVERLOADED_OPERATOR_CODE (tmpl,
260890075Sobrien				      DECL_OVERLOADED_OPERATOR_P (decl));
260950397Sobrien    }
261050397Sobrien
261150397Sobrien  return tmpl;
261250397Sobrien}
261350397Sobrien
261450397Sobrienstruct template_parm_data
261550397Sobrien{
261652284Sobrien  /* The level of the template parameters we are currently
261752284Sobrien     processing.  */
261850397Sobrien  int level;
261952284Sobrien
262052284Sobrien  /* The index of the specialization argument we are currently
262152284Sobrien     processing.  */
262252284Sobrien  int current_arg;
262352284Sobrien
262452284Sobrien  /* An array whose size is the number of template parameters.  The
2625117395Skan     elements are nonzero if the parameter has been used in any one
262652284Sobrien     of the arguments processed so far.  */
262750397Sobrien  int* parms;
262852284Sobrien
262952284Sobrien  /* An array whose size is the number of template arguments.  The
2630117395Skan     elements are nonzero if the argument makes use of template
263152284Sobrien     parameters of this level.  */
263252284Sobrien  int* arg_uses_template_parms;
263350397Sobrien};
263450397Sobrien
263550397Sobrien/* Subroutine of push_template_decl used to see if each template
263650397Sobrien   parameter in a partial specialization is used in the explicit
263750397Sobrien   argument list.  If T is of the LEVEL given in DATA (which is
263850397Sobrien   treated as a template_parm_data*), then DATA->PARMS is marked
263950397Sobrien   appropriately.  */
264050397Sobrien
264150397Sobrienstatic int
2642132718Skanmark_template_parm (tree t, void* data)
264350397Sobrien{
264450397Sobrien  int level;
264550397Sobrien  int idx;
264650397Sobrien  struct template_parm_data* tpd = (struct template_parm_data*) data;
264750397Sobrien
264850397Sobrien  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
264950397Sobrien    {
265050397Sobrien      level = TEMPLATE_PARM_LEVEL (t);
265150397Sobrien      idx = TEMPLATE_PARM_IDX (t);
265250397Sobrien    }
265350397Sobrien  else
265450397Sobrien    {
265550397Sobrien      level = TEMPLATE_TYPE_LEVEL (t);
265650397Sobrien      idx = TEMPLATE_TYPE_IDX (t);
265750397Sobrien    }
265850397Sobrien
265950397Sobrien  if (level == tpd->level)
266052284Sobrien    {
266152284Sobrien      tpd->parms[idx] = 1;
266252284Sobrien      tpd->arg_uses_template_parms[tpd->current_arg] = 1;
266352284Sobrien    }
266450397Sobrien
266550397Sobrien  /* Return zero so that for_each_template_parm will continue the
266650397Sobrien     traversal of the tree; we want to mark *every* template parm.  */
266750397Sobrien  return 0;
266850397Sobrien}
266950397Sobrien
267052284Sobrien/* Process the partial specialization DECL.  */
267152284Sobrien
267252284Sobrienstatic tree
2673132718Skanprocess_partial_specialization (tree decl)
267452284Sobrien{
267552284Sobrien  tree type = TREE_TYPE (decl);
267652284Sobrien  tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
267752284Sobrien  tree specargs = CLASSTYPE_TI_ARGS (type);
267890075Sobrien  tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
267952284Sobrien  tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
268052284Sobrien  tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
268152284Sobrien  int nargs = TREE_VEC_LENGTH (inner_args);
268252284Sobrien  int ntparms = TREE_VEC_LENGTH (inner_parms);
268352284Sobrien  int  i;
268452284Sobrien  int did_error_intro = 0;
268552284Sobrien  struct template_parm_data tpd;
268652284Sobrien  struct template_parm_data tpd2;
268752284Sobrien
268852284Sobrien  /* We check that each of the template parameters given in the
268952284Sobrien     partial specialization is used in the argument list to the
269052284Sobrien     specialization.  For example:
269152284Sobrien
269252284Sobrien       template <class T> struct S;
269352284Sobrien       template <class T> struct S<T*>;
269452284Sobrien
269552284Sobrien     The second declaration is OK because `T*' uses the template
269652284Sobrien     parameter T, whereas
269752284Sobrien
269852284Sobrien       template <class T> struct S<int>;
269952284Sobrien
270052284Sobrien     is no good.  Even trickier is:
270152284Sobrien
270252284Sobrien       template <class T>
270352284Sobrien       struct S1
270452284Sobrien       {
270552284Sobrien	  template <class U>
270652284Sobrien	  struct S2;
270752284Sobrien	  template <class U>
270852284Sobrien	  struct S2<T>;
270952284Sobrien       };
271052284Sobrien
2711117395Skan     The S2<T> declaration is actually invalid; it is a
2712169689Skan     full-specialization.  Of course,
271352284Sobrien
271452284Sobrien	  template <class U>
271552284Sobrien	  struct S2<T (*)(U)>;
271652284Sobrien
271752284Sobrien     or some such would have been OK.  */
271852284Sobrien  tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2719169689Skan  tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2720132718Skan  memset (tpd.parms, 0, sizeof (int) * ntparms);
272152284Sobrien
2722169689Skan  tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2723132718Skan  memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
272452284Sobrien  for (i = 0; i < nargs; ++i)
272552284Sobrien    {
272652284Sobrien      tpd.current_arg = i;
272752284Sobrien      for_each_template_parm (TREE_VEC_ELT (inner_args, i),
272852284Sobrien			      &mark_template_parm,
2729117395Skan			      &tpd,
2730117395Skan			      NULL);
273152284Sobrien    }
273252284Sobrien  for (i = 0; i < ntparms; ++i)
273352284Sobrien    if (tpd.parms[i] == 0)
273452284Sobrien      {
273552284Sobrien	/* One of the template parms was not used in the
273690075Sobrien	   specialization.  */
273752284Sobrien	if (!did_error_intro)
273852284Sobrien	  {
273990075Sobrien	    error ("template parameters not used in partial specialization:");
274052284Sobrien	    did_error_intro = 1;
274152284Sobrien	  }
274252284Sobrien
2743169689Skan	error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
274452284Sobrien      }
274552284Sobrien
274652284Sobrien  /* [temp.class.spec]
274752284Sobrien
274852284Sobrien     The argument list of the specialization shall not be identical to
274952284Sobrien     the implicit argument list of the primary template.  */
2750169689Skan  if (comp_template_args
2751169689Skan      (inner_args,
275290075Sobrien       INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
275390075Sobrien						   (maintmpl)))))
2754169689Skan    error ("partial specialization %qT does not specialize any template arguments", type);
275552284Sobrien
275652284Sobrien  /* [temp.class.spec]
275752284Sobrien
275852284Sobrien     A partially specialized non-type argument expression shall not
275952284Sobrien     involve template parameters of the partial specialization except
276052284Sobrien     when the argument expression is a simple identifier.
276152284Sobrien
276252284Sobrien     The type of a template parameter corresponding to a specialized
276352284Sobrien     non-type argument shall not be dependent on a parameter of the
276452284Sobrien     specialization.  */
2765169689Skan  gcc_assert (nargs == DECL_NTPARMS (maintmpl));
276652284Sobrien  tpd2.parms = 0;
276752284Sobrien  for (i = 0; i < nargs; ++i)
276852284Sobrien    {
276952284Sobrien      tree arg = TREE_VEC_ELT (inner_args, i);
277052284Sobrien      if (/* These first two lines are the `non-type' bit.  */
277190075Sobrien	  !TYPE_P (arg)
277252284Sobrien	  && TREE_CODE (arg) != TEMPLATE_DECL
277352284Sobrien	  /* This next line is the `argument expression is not just a
277452284Sobrien	     simple identifier' condition and also the `specialized
277552284Sobrien	     non-type argument' bit.  */
277652284Sobrien	  && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
277752284Sobrien	{
277852284Sobrien	  if (tpd.arg_uses_template_parms[i])
2779169689Skan	    error ("template argument %qE involves template parameter(s)", arg);
2780169689Skan	  else
278152284Sobrien	    {
278252284Sobrien	      /* Look at the corresponding template parameter,
278352284Sobrien		 marking which template parameters its type depends
278452284Sobrien		 upon.  */
2785169689Skan	      tree type =
2786169689Skan		TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
278752284Sobrien						     i)));
278852284Sobrien
278952284Sobrien	      if (!tpd2.parms)
279052284Sobrien		{
279152284Sobrien		  /* We haven't yet initialized TPD2.  Do so now.  */
2792169689Skan		  tpd2.arg_uses_template_parms
2793169689Skan		    = (int *) alloca (sizeof (int) * nargs);
279452284Sobrien		  /* The number of parameters here is the number in the
279552284Sobrien		     main template, which, as checked in the assertion
279652284Sobrien		     above, is NARGS.  */
2797169689Skan		  tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2798169689Skan		  tpd2.level =
279952284Sobrien		    TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
280052284Sobrien		}
280152284Sobrien
280252284Sobrien	      /* Mark the template parameters.  But this time, we're
280352284Sobrien		 looking for the template parameters of the main
280452284Sobrien		 template, not in the specialization.  */
280552284Sobrien	      tpd2.current_arg = i;
280652284Sobrien	      tpd2.arg_uses_template_parms[i] = 0;
2807132718Skan	      memset (tpd2.parms, 0, sizeof (int) * nargs);
280852284Sobrien	      for_each_template_parm (type,
280952284Sobrien				      &mark_template_parm,
2810117395Skan				      &tpd2,
2811117395Skan				      NULL);
2812169689Skan
281352284Sobrien	      if (tpd2.arg_uses_template_parms [i])
281452284Sobrien		{
281552284Sobrien		  /* The type depended on some template parameters.
281652284Sobrien		     If they are fully specialized in the
281752284Sobrien		     specialization, that's OK.  */
281852284Sobrien		  int j;
281952284Sobrien		  for (j = 0; j < nargs; ++j)
282052284Sobrien		    if (tpd2.parms[j] != 0
282152284Sobrien			&& tpd.arg_uses_template_parms [j])
282252284Sobrien		      {
2823169689Skan			error ("type %qT of template argument %qE depends "
2824169689Skan			       "on template parameter(s)",
2825169689Skan			       type,
2826169689Skan			       arg);
282752284Sobrien			break;
282852284Sobrien		      }
282952284Sobrien		}
283052284Sobrien	    }
283152284Sobrien	}
283252284Sobrien    }
283352284Sobrien
2834169689Skan  if (retrieve_specialization (maintmpl, specargs,
2835169689Skan			       /*class_specializations_p=*/true))
283652284Sobrien    /* We've already got this specialization.  */
283752284Sobrien    return decl;
283852284Sobrien
283990075Sobrien  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2840169689Skan    = tree_cons (specargs, inner_parms,
284190075Sobrien		 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
284252284Sobrien  TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
284352284Sobrien  return decl;
284452284Sobrien}
284552284Sobrien
284652284Sobrien/* Check that a template declaration's use of default arguments is not
284752284Sobrien   invalid.  Here, PARMS are the template parameters.  IS_PRIMARY is
2848117395Skan   nonzero if DECL is the thing declared by a primary template.
2849117395Skan   IS_PARTIAL is nonzero if DECL is a partial specialization.  */
285052284Sobrien
285152284Sobrienstatic void
2852132718Skancheck_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
285352284Sobrien{
285452284Sobrien  const char *msg;
285590075Sobrien  int last_level_to_check;
285690075Sobrien  tree parm_level;
285752284Sobrien
2858169689Skan  /* [temp.param]
285952284Sobrien
286052284Sobrien     A default template-argument shall not be specified in a
286152284Sobrien     function template declaration or a function template definition, nor
286252284Sobrien     in the template-parameter-list of the definition of a member of a
286352284Sobrien     class template.  */
286452284Sobrien
286590075Sobrien  if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
286690075Sobrien    /* You can't have a function template declaration in a local
286790075Sobrien       scope, nor you can you define a member of a class template in a
286890075Sobrien       local scope.  */
286990075Sobrien    return;
287090075Sobrien
287152284Sobrien  if (current_class_type
287252284Sobrien      && !TYPE_BEING_DEFINED (current_class_type)
287352284Sobrien      && DECL_LANG_SPECIFIC (decl)
287452284Sobrien      /* If this is either a friend defined in the scope of the class
287552284Sobrien	 or a member function.  */
2876132718Skan      && (DECL_FUNCTION_MEMBER_P (decl)
2877132718Skan	  ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2878132718Skan	  : DECL_FRIEND_CONTEXT (decl)
2879132718Skan	  ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2880132718Skan	  : false)
288152284Sobrien      /* And, if it was a member function, it really was defined in
288252284Sobrien	 the scope of the class.  */
2883132718Skan      && (!DECL_FUNCTION_MEMBER_P (decl)
2884132718Skan	  || DECL_INITIALIZED_IN_CLASS_P (decl)))
288552284Sobrien    /* We already checked these parameters when the template was
288652284Sobrien       declared, so there's no need to do it again now.  This function
288752284Sobrien       was defined in class scope, but we're processing it's body now
288852284Sobrien       that the class is complete.  */
288952284Sobrien    return;
289052284Sobrien
289190075Sobrien  /* [temp.param]
2892169689Skan
289390075Sobrien     If a template-parameter has a default template-argument, all
289490075Sobrien     subsequent template-parameters shall have a default
289590075Sobrien     template-argument supplied.  */
289690075Sobrien  for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
289790075Sobrien    {
289890075Sobrien      tree inner_parms = TREE_VALUE (parm_level);
289990075Sobrien      int ntparms = TREE_VEC_LENGTH (inner_parms);
2900169689Skan      int seen_def_arg_p = 0;
290190075Sobrien      int i;
290290075Sobrien
2903169689Skan      for (i = 0; i < ntparms; ++i)
290490075Sobrien	{
290590075Sobrien	  tree parm = TREE_VEC_ELT (inner_parms, i);
2906169689Skan
2907169689Skan          if (parm == error_mark_node)
2908169689Skan            continue;
2909169689Skan
291090075Sobrien	  if (TREE_PURPOSE (parm))
291190075Sobrien	    seen_def_arg_p = 1;
291290075Sobrien	  else if (seen_def_arg_p)
291390075Sobrien	    {
2914169689Skan	      error ("no default argument for %qD", TREE_VALUE (parm));
291590075Sobrien	      /* For better subsequent error-recovery, we indicate that
291690075Sobrien		 there should have been a default argument.  */
291790075Sobrien	      TREE_PURPOSE (parm) = error_mark_node;
291890075Sobrien	    }
291990075Sobrien	}
292090075Sobrien    }
292190075Sobrien
292252284Sobrien  if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
292352284Sobrien    /* For an ordinary class template, default template arguments are
292452284Sobrien       allowed at the innermost level, e.g.:
2925169689Skan	 template <class T = int>
292652284Sobrien	 struct S {};
292752284Sobrien       but, in a partial specialization, they're not allowed even
292852284Sobrien       there, as we have in [temp.class.spec]:
2929169689Skan
293052284Sobrien	 The template parameter list of a specialization shall not
2931169689Skan	 contain default template argument values.
293252284Sobrien
293352284Sobrien       So, for a partial specialization, or for a function template,
293452284Sobrien       we look at all of them.  */
293552284Sobrien    ;
293652284Sobrien  else
293752284Sobrien    /* But, for a primary class template that is not a partial
293852284Sobrien       specialization we look at all template parameters except the
293952284Sobrien       innermost ones.  */
294052284Sobrien    parms = TREE_CHAIN (parms);
294152284Sobrien
294252284Sobrien  /* Figure out what error message to issue.  */
294352284Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL)
294490075Sobrien    msg = "default template arguments may not be used in function templates";
294552284Sobrien  else if (is_partial)
294690075Sobrien    msg = "default template arguments may not be used in partial specializations";
294752284Sobrien  else
2948169689Skan    msg = "default argument for template parameter for class enclosing %qD";
294952284Sobrien
295052284Sobrien  if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
295152284Sobrien    /* If we're inside a class definition, there's no need to
295252284Sobrien       examine the parameters to the class itself.  On the one
295352284Sobrien       hand, they will be checked when the class is defined, and,
2954117395Skan       on the other, default arguments are valid in things like:
2955169689Skan	 template <class T = double>
2956169689Skan	 struct S { template <class U> void f(U); };
295752284Sobrien       Here the default argument for `S' has no bearing on the
295852284Sobrien       declaration of `f'.  */
295952284Sobrien    last_level_to_check = template_class_depth (current_class_type) + 1;
296052284Sobrien  else
296152284Sobrien    /* Check everything.  */
296252284Sobrien    last_level_to_check = 0;
296352284Sobrien
2964169689Skan  for (parm_level = parms;
2965169689Skan       parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
296690075Sobrien       parm_level = TREE_CHAIN (parm_level))
296752284Sobrien    {
296890075Sobrien      tree inner_parms = TREE_VALUE (parm_level);
296990075Sobrien      int i;
297090075Sobrien      int ntparms;
297152284Sobrien
297252284Sobrien      ntparms = TREE_VEC_LENGTH (inner_parms);
2973169689Skan      for (i = 0; i < ntparms; ++i)
2974169689Skan        {
2975169689Skan          if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
2976169689Skan            continue;
297752284Sobrien
2978169689Skan	  if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2979169689Skan	    {
2980169689Skan	      if (msg)
2981169689Skan	        {
2982169689Skan		  error (msg, decl);
2983169689Skan		  msg = 0;
2984169689Skan	        }
298552284Sobrien
2986169689Skan	      /* Clear out the default argument so that we are not
2987169689Skan	         confused later.  */
2988169689Skan	      TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2989169689Skan	    }
2990169689Skan        }
2991169689Skan
299252284Sobrien      /* At this point, if we're still interested in issuing messages,
299352284Sobrien	 they must apply to classes surrounding the object declared.  */
299452284Sobrien      if (msg)
2995169689Skan	msg = "default argument for template parameter for class enclosing %qD";
299652284Sobrien    }
299752284Sobrien}
299852284Sobrien
2999117395Skan/* Worker for push_template_decl_real, called via
3000117395Skan   for_each_template_parm.  DATA is really an int, indicating the
3001117395Skan   level of the parameters we are interested in.  If T is a template
3002117395Skan   parameter of that level, return nonzero.  */
3003117395Skan
3004117395Skanstatic int
3005132718Skantemplate_parm_this_level_p (tree t, void* data)
3006117395Skan{
3007117395Skan  int this_level = *(int *)data;
3008117395Skan  int level;
3009117395Skan
3010117395Skan  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3011117395Skan    level = TEMPLATE_PARM_LEVEL (t);
3012117395Skan  else
3013117395Skan    level = TEMPLATE_TYPE_LEVEL (t);
3014117395Skan  return level == this_level;
3015117395Skan}
3016117395Skan
301750397Sobrien/* Creates a TEMPLATE_DECL for the indicated DECL using the template
301850397Sobrien   parameters given by current_template_args, or reuses a
301950397Sobrien   previously existing one, if appropriate.  Returns the DECL, or an
3020169689Skan   equivalent one, if it is replaced via a call to duplicate_decls.
302150397Sobrien
3022169689Skan   If IS_FRIEND is true, DECL is a friend declaration.  */
302350397Sobrien
302450397Sobrientree
3025169689Skanpush_template_decl_real (tree decl, bool is_friend)
302650397Sobrien{
302750397Sobrien  tree tmpl;
302850397Sobrien  tree args;
302950397Sobrien  tree info;
303050397Sobrien  tree ctx;
303150397Sobrien  int primary;
303252284Sobrien  int is_partial;
303390075Sobrien  int new_template_p = 0;
3034169689Skan  /* True if the template is a member template, in the sense of
3035169689Skan     [temp.mem].  */
3036169689Skan  bool member_template_p = false;
303750397Sobrien
3038169689Skan  if (decl == error_mark_node)
3039169689Skan    return decl;
3040169689Skan
304152284Sobrien  /* See if this is a partial specialization.  */
304290075Sobrien  is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
304352284Sobrien		&& TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
304452284Sobrien		&& CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
304552284Sobrien
3046169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3047169689Skan    is_friend = true;
304850397Sobrien
304950397Sobrien  if (is_friend)
305050397Sobrien    /* For a friend, we want the context of the friend function, not
305150397Sobrien       the type of which it is a friend.  */
305250397Sobrien    ctx = DECL_CONTEXT (decl);
305390075Sobrien  else if (CP_DECL_CONTEXT (decl)
305490075Sobrien	   && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
305550397Sobrien    /* In the case of a virtual function, we want the class in which
305650397Sobrien       it is defined.  */
305790075Sobrien    ctx = CP_DECL_CONTEXT (decl);
305850397Sobrien  else
305990075Sobrien    /* Otherwise, if we're currently defining some class, the DECL
306050397Sobrien       is assumed to be a member of the class.  */
306190075Sobrien    ctx = current_scope ();
306250397Sobrien
306350397Sobrien  if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
306450397Sobrien    ctx = NULL_TREE;
306550397Sobrien
306650397Sobrien  if (!DECL_CONTEXT (decl))
306750397Sobrien    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
306850397Sobrien
306952284Sobrien  /* See if this is a primary template.  */
3070169689Skan  if (is_friend && ctx)
3071169689Skan    /* A friend template that specifies a class context, i.e.
3072169689Skan         template <typename T> friend void A<T>::f();
3073169689Skan       is not primary.  */
3074169689Skan    primary = 0;
3075169689Skan  else
3076169689Skan    primary = template_parm_scope_p ();
307750397Sobrien
307850397Sobrien  if (primary)
307950397Sobrien    {
3080169689Skan      if (DECL_CLASS_SCOPE_P (decl))
3081169689Skan	member_template_p = true;
3082169689Skan      if (TREE_CODE (decl) == TYPE_DECL
3083169689Skan	  && ANON_AGGRNAME_P (DECL_NAME (decl)))
308490075Sobrien	error ("template class without a name");
3085146895Skan      else if (TREE_CODE (decl) == FUNCTION_DECL)
3086132718Skan	{
3087146895Skan	  if (DECL_DESTRUCTOR_P (decl))
3088146895Skan	    {
3089146895Skan	      /* [temp.mem]
3090169689Skan
3091169689Skan		 A destructor shall not be a member template.  */
3092169689Skan	      error ("destructor %qD declared as member template", decl);
3093146895Skan	      return error_mark_node;
3094146895Skan	    }
3095146895Skan	  if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3096146895Skan	      && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3097146895Skan		  || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3098146895Skan		  || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3099146895Skan		  || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3100146895Skan		      == void_list_node)))
3101146895Skan	    {
3102169689Skan	      /* [basic.stc.dynamic.allocation]
3103146895Skan
3104169689Skan		 An allocation function can be a function
3105146895Skan		 template. ... Template allocation functions shall
3106146895Skan		 have two or more parameters.  */
3107169689Skan	      error ("invalid template declaration of %qD", decl);
3108169689Skan	      return error_mark_node;
3109146895Skan	    }
3110132718Skan	}
3111161651Skan      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3112161651Skan	       && CLASS_TYPE_P (TREE_TYPE (decl)))
311390075Sobrien	/* OK */;
311490075Sobrien      else
3115132718Skan	{
3116169689Skan	  error ("template declaration of %q#D", decl);
3117132718Skan	  return error_mark_node;
3118132718Skan	}
311950397Sobrien    }
312050397Sobrien
312152284Sobrien  /* Check to see that the rules regarding the use of default
312252284Sobrien     arguments are not being violated.  */
3123169689Skan  check_default_tmpl_args (decl, current_template_parms,
312452284Sobrien			   primary, is_partial);
312550397Sobrien
312652284Sobrien  if (is_partial)
312752284Sobrien    return process_partial_specialization (decl);
312850397Sobrien
312950397Sobrien  args = current_template_args ();
313050397Sobrien
3131169689Skan  if (!ctx
313250397Sobrien      || TREE_CODE (ctx) == FUNCTION_DECL
3133132718Skan      || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
313450397Sobrien      || (is_friend && !DECL_TEMPLATE_INFO (decl)))
313550397Sobrien    {
313650397Sobrien      if (DECL_LANG_SPECIFIC (decl)
313750397Sobrien	  && DECL_TEMPLATE_INFO (decl)
313850397Sobrien	  && DECL_TI_TEMPLATE (decl))
313950397Sobrien	tmpl = DECL_TI_TEMPLATE (decl);
314090075Sobrien      /* If DECL is a TYPE_DECL for a class-template, then there won't
314190075Sobrien	 be DECL_LANG_SPECIFIC.  The information equivalent to
314290075Sobrien	 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
3143169689Skan      else if (DECL_IMPLICIT_TYPEDEF_P (decl)
314490075Sobrien	       && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
314590075Sobrien	       && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
314690075Sobrien	{
314790075Sobrien	  /* Since a template declaration already existed for this
314890075Sobrien	     class-type, we must be redeclaring it here.  Make sure
3149117395Skan	     that the redeclaration is valid.  */
315090075Sobrien	  redeclare_class_template (TREE_TYPE (decl),
315190075Sobrien				    current_template_parms);
315290075Sobrien	  /* We don't need to create a new TEMPLATE_DECL; just use the
315390075Sobrien	     one we already had.  */
315490075Sobrien	  tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
315590075Sobrien	}
315618334Speter      else
315718334Speter	{
3158169689Skan	  tmpl = build_template_decl (decl, current_template_parms,
3159169689Skan				      member_template_p);
316090075Sobrien	  new_template_p = 1;
316190075Sobrien
316250397Sobrien	  if (DECL_LANG_SPECIFIC (decl)
316350397Sobrien	      && DECL_TEMPLATE_SPECIALIZATION (decl))
316450397Sobrien	    {
316550397Sobrien	      /* A specialization of a member template of a template
3166117395Skan		 class.  */
316750397Sobrien	      SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
316850397Sobrien	      DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
316950397Sobrien	      DECL_TEMPLATE_INFO (decl) = NULL_TREE;
317050397Sobrien	    }
317118334Speter	}
317218334Speter    }
317350397Sobrien  else
317450397Sobrien    {
317552284Sobrien      tree a, t, current, parms;
317652284Sobrien      int i;
317750397Sobrien
317850397Sobrien      if (TREE_CODE (decl) == TYPE_DECL)
317950397Sobrien	{
318052284Sobrien	  if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
318152284Sobrien	       || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
318252284Sobrien	      && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
318352284Sobrien	      && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
318452284Sobrien	    tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
318550397Sobrien	  else
318650397Sobrien	    {
3187169689Skan	      error ("%qD does not declare a template type", decl);
318850397Sobrien	      return decl;
318950397Sobrien	    }
319050397Sobrien	}
319190075Sobrien      else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
319250397Sobrien	{
3193169689Skan	  error ("template definition of non-template %q#D", decl);
319450397Sobrien	  return decl;
319550397Sobrien	}
319650397Sobrien      else
319750397Sobrien	tmpl = DECL_TI_TEMPLATE (decl);
3198169689Skan
3199132718Skan      if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3200169689Skan	  && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3201132718Skan	  && DECL_TEMPLATE_SPECIALIZATION (decl)
3202169689Skan	  && DECL_MEMBER_TEMPLATE_P (tmpl))
320350397Sobrien	{
320452284Sobrien	  tree new_tmpl;
320550397Sobrien
320652284Sobrien	  /* The declaration is a specialization of a member
320752284Sobrien	     template, declared outside the class.  Therefore, the
320852284Sobrien	     innermost template arguments will be NULL, so we
320952284Sobrien	     replace them with the arguments determined by the
321052284Sobrien	     earlier call to check_explicit_specialization.  */
321152284Sobrien	  args = DECL_TI_ARGS (decl);
321250397Sobrien
3213169689Skan	  new_tmpl
3214169689Skan	    = build_template_decl (decl, current_template_parms,
3215169689Skan				   member_template_p);
321652284Sobrien	  DECL_TEMPLATE_RESULT (new_tmpl) = decl;
321752284Sobrien	  TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
321852284Sobrien	  DECL_TI_TEMPLATE (decl) = new_tmpl;
321952284Sobrien	  SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3220169689Skan	  DECL_TEMPLATE_INFO (new_tmpl)
322190075Sobrien	    = tree_cons (tmpl, args, NULL_TREE);
322250397Sobrien
3223169689Skan	  register_specialization (new_tmpl,
3224169689Skan				   most_general_template (tmpl),
3225169689Skan				   args,
3226169689Skan				   is_friend);
322752284Sobrien	  return decl;
322850397Sobrien	}
322950397Sobrien
323052284Sobrien      /* Make sure the template headers we got make sense.  */
323150397Sobrien
323252284Sobrien      parms = DECL_TEMPLATE_PARMS (tmpl);
323352284Sobrien      i = TMPL_PARMS_DEPTH (parms);
323452284Sobrien      if (TMPL_ARGS_DEPTH (args) != i)
323550397Sobrien	{
3236169689Skan	  error ("expected %d levels of template parms for %q#D, got %d",
3237169689Skan		 i, decl, TMPL_ARGS_DEPTH (args));
323850397Sobrien	}
323952284Sobrien      else
324052284Sobrien	for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
324152284Sobrien	  {
324252284Sobrien	    a = TMPL_ARGS_LEVEL (args, i);
324352284Sobrien	    t = INNERMOST_TEMPLATE_PARMS (parms);
324450397Sobrien
324552284Sobrien	    if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
324652284Sobrien	      {
324752284Sobrien		if (current == decl)
3248169689Skan		  error ("got %d template parameters for %q#D",
3249169689Skan			 TREE_VEC_LENGTH (a), decl);
325052284Sobrien		else
3251169689Skan		  error ("got %d template parameters for %q#T",
3252169689Skan			 TREE_VEC_LENGTH (a), current);
325390075Sobrien		error ("  but %d required", TREE_VEC_LENGTH (t));
3254161651Skan		return error_mark_node;
325552284Sobrien	      }
325650397Sobrien
325752284Sobrien	    /* Perhaps we should also check that the parms are used in the
3258169689Skan	       appropriate qualifying scopes in the declarator?  */
325952284Sobrien
326052284Sobrien	    if (current == decl)
326152284Sobrien	      current = ctx;
326252284Sobrien	    else
326352284Sobrien	      current = TYPE_CONTEXT (current);
326452284Sobrien	  }
326550397Sobrien    }
326650397Sobrien
326750397Sobrien  DECL_TEMPLATE_RESULT (tmpl) = decl;
326850397Sobrien  TREE_TYPE (tmpl) = TREE_TYPE (decl);
326950397Sobrien
327052284Sobrien  /* Push template declarations for global functions and types.  Note
327152284Sobrien     that we do not try to push a global template friend declared in a
327252284Sobrien     template class; such a thing may well depend on the template
327352284Sobrien     parameters of the class.  */
3274169689Skan  if (new_template_p && !ctx
327552284Sobrien      && !(is_friend && template_class_depth (current_class_type) > 0))
3276169689Skan    {
3277169689Skan      tmpl = pushdecl_namespace_level (tmpl, is_friend);
3278169689Skan      if (tmpl == error_mark_node)
3279169689Skan	return error_mark_node;
328050397Sobrien
3281169689Skan      /* Hide template friend classes that haven't been declared yet.  */
3282169689Skan      if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3283169689Skan	{
3284169689Skan	  DECL_ANTICIPATED (tmpl) = 1;
3285169689Skan	  DECL_FRIEND_P (tmpl) = 1;
3286169689Skan	}
3287169689Skan    }
3288169689Skan
328950397Sobrien  if (primary)
3290117395Skan    {
3291117395Skan      DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3292117395Skan      if (DECL_CONV_FN_P (tmpl))
3293117395Skan	{
3294117395Skan	  int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
329550397Sobrien
3296117395Skan	  /* It is a conversion operator. See if the type converted to
3297117395Skan	     depends on innermost template operands.  */
3298169689Skan
3299132718Skan	  if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3300132718Skan					 depth))
3301117395Skan	    DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3302117395Skan	}
3303117395Skan    }
3304117395Skan
3305169689Skan  /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3306132718Skan     back to its most general template.  If TMPL is a specialization,
3307132718Skan     ARGS may only have the innermost set of arguments.  Add the missing
3308132718Skan     argument levels if necessary.  */
3309132718Skan  if (DECL_TEMPLATE_INFO (tmpl))
3310132718Skan    args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3311132718Skan
331290075Sobrien  info = tree_cons (tmpl, args, NULL_TREE);
331350397Sobrien
331490075Sobrien  if (DECL_IMPLICIT_TYPEDEF_P (decl))
331550397Sobrien    {
331652284Sobrien      SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
331752284Sobrien      if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
331890075Sobrien	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
331990075Sobrien	  /* Don't change the name if we've already set it up.  */
332090075Sobrien	  && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
332150397Sobrien	DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
332250397Sobrien    }
332390075Sobrien  else if (DECL_LANG_SPECIFIC (decl))
332450397Sobrien    DECL_TEMPLATE_INFO (decl) = info;
332550397Sobrien
332650397Sobrien  return DECL_TEMPLATE_RESULT (tmpl);
332718334Speter}
332818334Speter
332950397Sobrientree
3330132718Skanpush_template_decl (tree decl)
333118334Speter{
3332169689Skan  return push_template_decl_real (decl, false);
333350397Sobrien}
333418334Speter
333550397Sobrien/* Called when a class template TYPE is redeclared with the indicated
333650397Sobrien   template PARMS, e.g.:
333718334Speter
333850397Sobrien     template <class T> struct S;
333950397Sobrien     template <class T> struct S {};  */
334018334Speter
3341169689Skanbool
3342132718Skanredeclare_class_template (tree type, tree parms)
334350397Sobrien{
334452284Sobrien  tree tmpl;
334550397Sobrien  tree tmpl_parms;
334650397Sobrien  int i;
334750397Sobrien
334852284Sobrien  if (!TYPE_TEMPLATE_INFO (type))
334952284Sobrien    {
3350169689Skan      error ("%qT is not a template type", type);
3351169689Skan      return false;
335252284Sobrien    }
335352284Sobrien
335452284Sobrien  tmpl = TYPE_TI_TEMPLATE (type);
335550397Sobrien  if (!PRIMARY_TEMPLATE_P (tmpl))
335650397Sobrien    /* The type is nested in some template class.  Nothing to worry
335750397Sobrien       about here; there are no new template parameters for the nested
335850397Sobrien       type.  */
3359169689Skan    return true;
336050397Sobrien
3361169689Skan  if (!parms)
3362169689Skan    {
3363169689Skan      error ("template specifiers not specified in declaration of %qD",
3364169689Skan	     tmpl);
3365169689Skan      return false;
3366169689Skan    }
3367169689Skan
336850397Sobrien  parms = INNERMOST_TEMPLATE_PARMS (parms);
336950397Sobrien  tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
337050397Sobrien
337150397Sobrien  if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
337218334Speter    {
3373169689Skan      error ("previous declaration %q+D", tmpl);
3374161651Skan      error ("used %d template parameter(s) instead of %d",
3375169689Skan	     TREE_VEC_LENGTH (tmpl_parms),
3376161651Skan	     TREE_VEC_LENGTH (parms));
3377169689Skan      return false;
337818334Speter    }
337918334Speter
338050397Sobrien  for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
338118334Speter    {
3382169689Skan      tree tmpl_parm;
3383169689Skan      tree parm;
3384169689Skan      tree tmpl_default;
3385169689Skan      tree parm_default;
338650397Sobrien
3387169689Skan      if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3388169689Skan          || TREE_VEC_ELT (parms, i) == error_mark_node)
3389169689Skan        continue;
3390169689Skan
3391169689Skan      tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3392169689Skan      parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3393169689Skan      tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3394169689Skan      parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3395169689Skan
3396169689Skan      /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3397169689Skan	 TEMPLATE_DECL.  */
3398169689Skan      if (tmpl_parm != error_mark_node
3399169689Skan	   && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3400169689Skan	   || (TREE_CODE (tmpl_parm) != TYPE_DECL
3401169689Skan	       && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
340250397Sobrien	{
3403169689Skan	  error ("template parameter %q+#D", tmpl_parm);
3404169689Skan	  error ("redeclared here as %q#D", parm);
3405169689Skan	  return false;
340650397Sobrien	}
340750397Sobrien
340850397Sobrien      if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
340950397Sobrien	{
341050397Sobrien	  /* We have in [temp.param]:
341150397Sobrien
341250397Sobrien	     A template-parameter may not be given default arguments
341350397Sobrien	     by two different declarations in the same scope.  */
3414169689Skan	  error ("redefinition of default argument for %q#D", parm);
3415132718Skan	  error ("%J  original definition appeared here", tmpl_parm);
3416169689Skan	  return false;
341750397Sobrien	}
341850397Sobrien
341950397Sobrien      if (parm_default != NULL_TREE)
342050397Sobrien	/* Update the previous template parameters (which are the ones
342150397Sobrien	   that will really count) with the new default value.  */
342250397Sobrien	TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
342390075Sobrien      else if (tmpl_default != NULL_TREE)
342490075Sobrien	/* Update the new parameters, too; they'll be used as the
342590075Sobrien	   parameters for any members.  */
342690075Sobrien	TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
342718334Speter    }
3428169689Skan
3429169689Skan    return true;
343050397Sobrien}
343150397Sobrien
3432132718Skan/* Simplify EXPR if it is a non-dependent expression.  Returns the
3433132718Skan   (possibly simplified) expression.  */
3434132718Skan
3435132718Skantree
3436132718Skanfold_non_dependent_expr (tree expr)
3437132718Skan{
3438169689Skan  if (expr == NULL_TREE)
3439169689Skan    return NULL_TREE;
3440169689Skan
3441132718Skan  /* If we're in a template, but EXPR isn't value dependent, simplify
3442132718Skan     it.  We're supposed to treat:
3443169689Skan
3444132718Skan       template <typename T> void f(T[1 + 1]);
3445132718Skan       template <typename T> void f(T[2]);
3446169689Skan
3447132718Skan     as two declarations of the same function, for example.  */
3448132718Skan  if (processing_template_decl
3449132718Skan      && !type_dependent_expression_p (expr)
3450132718Skan      && !value_dependent_expression_p (expr))
3451132718Skan    {
3452132718Skan      HOST_WIDE_INT saved_processing_template_decl;
3453132718Skan
3454132718Skan      saved_processing_template_decl = processing_template_decl;
3455132718Skan      processing_template_decl = 0;
3456132718Skan      expr = tsubst_copy_and_build (expr,
3457132718Skan				    /*args=*/NULL_TREE,
3458132718Skan				    tf_error,
3459132718Skan				    /*in_decl=*/NULL_TREE,
3460169689Skan				    /*function_p=*/false,
3461169689Skan				    /*integral_constant_expression_p=*/true);
3462132718Skan      processing_template_decl = saved_processing_template_decl;
3463132718Skan    }
3464132718Skan  return expr;
3465132718Skan}
3466132718Skan
3467169689Skan/* EXPR is an expression which is used in a constant-expression context.
3468169689Skan   For instance, it could be a VAR_DECL with a constant initializer.
3469169689Skan   Extract the innest constant expression.
3470169689Skan
3471169689Skan   This is basically a more powerful version of
3472169689Skan   integral_constant_value, which can be used also in templates where
3473169689Skan   initializers can maintain a syntactic rather than semantic form
3474169689Skan   (even if they are non-dependent, for access-checking purposes).  */
3475169689Skan
3476169689Skanstatic tree
3477169689Skanfold_decl_constant_value (tree expr)
3478169689Skan{
3479169689Skan  tree const_expr = expr;
3480169689Skan  do
3481169689Skan    {
3482169689Skan      expr = fold_non_dependent_expr (const_expr);
3483169689Skan      const_expr = integral_constant_value (expr);
3484169689Skan    }
3485169689Skan  while (expr != const_expr);
3486169689Skan
3487169689Skan  return expr;
3488169689Skan}
3489169689Skan
3490169689Skan/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3491169689Skan   must be a function or a pointer-to-function type, as specified
3492169689Skan   in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3493169689Skan   and check that the resulting function has external linkage.  */
3494169689Skan
3495169689Skanstatic tree
3496169689Skanconvert_nontype_argument_function (tree type, tree expr)
3497169689Skan{
3498169689Skan  tree fns = expr;
3499169689Skan  tree fn, fn_no_ptr;
3500169689Skan
3501169689Skan  fn = instantiate_type (type, fns, tf_none);
3502169689Skan  if (fn == error_mark_node)
3503169689Skan    return error_mark_node;
3504169689Skan
3505169689Skan  fn_no_ptr = fn;
3506169689Skan  if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3507169689Skan    fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3508169689Skan  if (TREE_CODE (fn_no_ptr) == BASELINK)
3509169689Skan    fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3510169689Skan
3511169689Skan  /* [temp.arg.nontype]/1
3512169689Skan
3513169689Skan     A template-argument for a non-type, non-template template-parameter
3514169689Skan     shall be one of:
3515169689Skan     [...]
3516169689Skan     -- the address of an object or function with external linkage.  */
3517169689Skan  if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3518169689Skan    {
3519169689Skan      error ("%qE is not a valid template argument for type %qT "
3520169689Skan	     "because function %qD has not external linkage",
3521169689Skan	     expr, type, fn_no_ptr);
3522169689Skan      return NULL_TREE;
3523169689Skan    }
3524169689Skan
3525169689Skan  return fn;
3526169689Skan}
3527169689Skan
352850397Sobrien/* Attempt to convert the non-type template parameter EXPR to the
352950397Sobrien   indicated TYPE.  If the conversion is successful, return the
353090075Sobrien   converted value.  If the conversion is unsuccessful, return
353150397Sobrien   NULL_TREE if we issued an error message, or error_mark_node if we
353250397Sobrien   did not.  We issue error messages for out-and-out bad template
353350397Sobrien   parameters, but not simply because the conversion failed, since we
3534132718Skan   might be just trying to do argument deduction.  Both TYPE and EXPR
3535169689Skan   must be non-dependent.
353650397Sobrien
3537169689Skan   The conversion follows the special rules described in
3538169689Skan   [temp.arg.nontype], and it is much more strict than an implicit
3539169689Skan   conversion.
3540169689Skan
3541169689Skan   This function is called twice for each template argument (see
3542169689Skan   lookup_template_class for a more accurate description of this
3543169689Skan   problem). This means that we need to handle expressions which
3544169689Skan   are not valid in a C++ source, but can be created from the
3545169689Skan   first call (for instance, casts to perform conversions). These
3546169689Skan   hacks can go away after we fix the double coercion problem.  */
3547169689Skan
354850397Sobrienstatic tree
3549132718Skanconvert_nontype_argument (tree type, tree expr)
355050397Sobrien{
3551132718Skan  tree expr_type;
355250397Sobrien
3553169689Skan  /* Detect immediately string literals as invalid non-type argument.
3554169689Skan     This special-case is not needed for correctness (we would easily
3555169689Skan     catch this later), but only to provide better diagnostic for this
3556169689Skan     common user mistake. As suggested by DR 100, we do not mention
3557169689Skan     linkage issues in the diagnostic as this is not the point.  */
3558169689Skan  if (TREE_CODE (expr) == STRING_CST)
3559169689Skan    {
3560169689Skan      error ("%qE is not a valid template argument for type %qT "
3561169689Skan	     "because string literals can never be used in this context",
3562169689Skan	     expr, type);
3563169689Skan      return NULL_TREE;
3564169689Skan    }
3565169689Skan
3566132718Skan  /* If we are in a template, EXPR may be non-dependent, but still
3567132718Skan     have a syntactic, rather than semantic, form.  For example, EXPR
3568132718Skan     might be a SCOPE_REF, rather than the VAR_DECL to which the
3569132718Skan     SCOPE_REF refers.  Preserving the qualifying scope is necessary
3570132718Skan     so that access checking can be performed when the template is
3571132718Skan     instantiated -- but here we need the resolved form so that we can
3572132718Skan     convert the argument.  */
3573132718Skan  expr = fold_non_dependent_expr (expr);
3574169689Skan  if (error_operand_p (expr))
3575169689Skan    return error_mark_node;
3576132718Skan  expr_type = TREE_TYPE (expr);
3577132718Skan
3578169689Skan  /* HACK: Due to double coercion, we can get a
3579169689Skan     NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3580169689Skan     which is the tree that we built on the first call (see
3581169689Skan     below when coercing to reference to object or to reference to
3582169689Skan     function). We just strip everything and get to the arg.
3583169689Skan     See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3584169689Skan     for examples.  */
3585169689Skan  if (TREE_CODE (expr) == NOP_EXPR)
358618334Speter    {
3587169689Skan      if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
358850397Sobrien	{
3589169689Skan	  /* ??? Maybe we could use convert_from_reference here, but we
3590169689Skan	     would need to relax its constraints because the NOP_EXPR
3591169689Skan	     could actually change the type to something more cv-qualified,
3592169689Skan	     and this is not folded by convert_from_reference.  */
3593169689Skan	  tree addr = TREE_OPERAND (expr, 0);
3594169689Skan	  gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3595169689Skan	  gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3596169689Skan	  gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3597169689Skan	  gcc_assert (same_type_ignoring_top_level_qualifiers_p
3598169689Skan		      (TREE_TYPE (expr_type),
3599169689Skan		       TREE_TYPE (TREE_TYPE (addr))));
360052284Sobrien
3601169689Skan	  expr = TREE_OPERAND (addr, 0);
3602169689Skan	  expr_type = TREE_TYPE (expr);
360350397Sobrien	}
360418334Speter
3605169689Skan      /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3606169689Skan	 parameter is a pointer to object, through decay and
3607169689Skan	 qualification conversion. Let's strip everything.  */
3608169689Skan      else if (TYPE_PTROBV_P (type))
360918334Speter	{
3610169689Skan	  STRIP_NOPS (expr);
3611169689Skan	  gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3612169689Skan	  gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3613169689Skan	  /* Skip the ADDR_EXPR only if it is part of the decay for
3614169689Skan	     an array. Otherwise, it is part of the original argument
3615169689Skan	     in the source code.  */
3616169689Skan	  if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3617169689Skan	    expr = TREE_OPERAND (expr, 0);
3618169689Skan	  expr_type = TREE_TYPE (expr);
361918334Speter	}
362018334Speter    }
3621132718Skan
3622169689Skan  /* [temp.arg.nontype]/5, bullet 1
362318334Speter
3624169689Skan     For a non-type template-parameter of integral or enumeration type,
3625169689Skan     integral promotions (_conv.prom_) and integral conversions
3626169689Skan     (_conv.integral_) are applied.  */
3627169689Skan  if (INTEGRAL_TYPE_P (type))
362850397Sobrien    {
362950397Sobrien      if (!INTEGRAL_TYPE_P (expr_type))
363050397Sobrien	return error_mark_node;
363150397Sobrien
3632169689Skan      expr = fold_decl_constant_value (expr);
3633169689Skan      /* Notice that there are constant expressions like '4 % 0' which
3634169689Skan	 do not fold into integer constants.  */
363550397Sobrien      if (TREE_CODE (expr) != INTEGER_CST)
3636169689Skan	{
3637169689Skan	  error ("%qE is not a valid template argument for type %qT "
3638169689Skan		 "because it is a non-constant expression", expr, type);
3639169689Skan	  return NULL_TREE;
3640169689Skan	}
3641132718Skan
3642169689Skan      /* At this point, an implicit conversion does what we want,
3643169689Skan	 because we already know that the expression is of integral
3644169689Skan	 type.  */
3645169689Skan      expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3646169689Skan      if (expr == error_mark_node)
3647169689Skan	return error_mark_node;
3648132718Skan
3649169689Skan      /* Conversion was allowed: fold it to a bare integer constant.  */
3650169689Skan      expr = fold (expr);
3651169689Skan    }
3652169689Skan  /* [temp.arg.nontype]/5, bullet 2
3653132718Skan
3654169689Skan     For a non-type template-parameter of type pointer to object,
3655169689Skan     qualification conversions (_conv.qual_) and the array-to-pointer
3656169689Skan     conversion (_conv.array_) are applied.  */
3657169689Skan  else if (TYPE_PTROBV_P (type))
3658169689Skan    {
3659169689Skan      /* [temp.arg.nontype]/1  (TC1 version, DR 49):
366050397Sobrien
3661169689Skan	 A template-argument for a non-type, non-template template-parameter
3662169689Skan	 shall be one of: [...]
366350397Sobrien
3664169689Skan	 -- the name of a non-type template-parameter;
3665169689Skan	 -- the address of an object or function with external linkage, [...]
3666169689Skan	    expressed as "& id-expression" where the & is optional if the name
3667169689Skan	    refers to a function or array, or if the corresponding
3668169689Skan	    template-parameter is a reference.
366950397Sobrien
3670169689Skan	Here, we do not care about functions, as they are invalid anyway
3671169689Skan	for a parameter of type pointer-to-object.  */
367250397Sobrien
3673169689Skan      if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
3674169689Skan	/* Non-type template parameters are OK.  */
3675169689Skan	;
3676169689Skan      else if (TREE_CODE (expr) != ADDR_EXPR
3677169689Skan	       && TREE_CODE (expr_type) != ARRAY_TYPE)
3678169689Skan	{
3679169689Skan	  if (TREE_CODE (expr) == VAR_DECL)
3680169689Skan	    {
3681169689Skan	      error ("%qD is not a valid template argument "
3682169689Skan		     "because %qD is a variable, not the address of "
3683169689Skan		     "a variable",
3684169689Skan		     expr, expr);
3685169689Skan	      return NULL_TREE;
3686169689Skan	    }
3687169689Skan	  /* Other values, like integer constants, might be valid
3688169689Skan	     non-type arguments of some other type.  */
3689169689Skan	  return error_mark_node;
3690169689Skan	}
3691169689Skan      else
3692169689Skan	{
3693169689Skan	  tree decl;
369450397Sobrien
3695169689Skan	  decl = ((TREE_CODE (expr) == ADDR_EXPR)
3696169689Skan		  ? TREE_OPERAND (expr, 0) : expr);
3697169689Skan	  if (TREE_CODE (decl) != VAR_DECL)
3698169689Skan	    {
3699169689Skan	      error ("%qE is not a valid template argument of type %qT "
3700169689Skan		     "because %qE is not a variable",
3701169689Skan		     expr, type, decl);
3702169689Skan	      return NULL_TREE;
3703169689Skan	    }
3704169689Skan	  else if (!DECL_EXTERNAL_LINKAGE_P (decl))
3705169689Skan	    {
3706169689Skan	      error ("%qE is not a valid template argument of type %qT "
3707169689Skan		     "because %qD does not have external linkage",
3708169689Skan		     expr, type, decl);
3709169689Skan	      return NULL_TREE;
3710169689Skan	    }
3711169689Skan	}
371250397Sobrien
3713169689Skan      expr = decay_conversion (expr);
3714169689Skan      if (expr == error_mark_node)
3715169689Skan	return error_mark_node;
371650397Sobrien
3717169689Skan      expr = perform_qualification_conversions (type, expr);
3718169689Skan      if (expr == error_mark_node)
3719169689Skan	return error_mark_node;
3720169689Skan    }
3721169689Skan  /* [temp.arg.nontype]/5, bullet 3
372250397Sobrien
3723169689Skan     For a non-type template-parameter of type reference to object, no
3724169689Skan     conversions apply. The type referred to by the reference may be more
3725169689Skan     cv-qualified than the (otherwise identical) type of the
3726169689Skan     template-argument. The template-parameter is bound directly to the
3727169689Skan     template-argument, which must be an lvalue.  */
3728169689Skan  else if (TYPE_REF_OBJ_P (type))
3729169689Skan    {
3730169689Skan      if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3731169689Skan						      expr_type))
3732169689Skan	return error_mark_node;
373350397Sobrien
3734169689Skan      if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3735169689Skan	{
3736169689Skan	  error ("%qE is not a valid template argument for type %qT "
3737169689Skan		 "because of conflicts in cv-qualification", expr, type);
3738169689Skan	  return NULL_TREE;
3739169689Skan	}
374090075Sobrien
3741169689Skan      if (!real_lvalue_p (expr))
3742169689Skan	{
3743169689Skan	  error ("%qE is not a valid template argument for type %qT "
3744169689Skan		 "because it is not an lvalue", expr, type);
3745169689Skan	  return NULL_TREE;
3746169689Skan	}
374750397Sobrien
3748169689Skan      /* [temp.arg.nontype]/1
374950397Sobrien
3750169689Skan	 A template-argument for a non-type, non-template template-parameter
3751169689Skan	 shall be one of: [...]
375252284Sobrien
3753169689Skan	 -- the address of an object or function with external linkage.  */
3754169689Skan      if (!DECL_EXTERNAL_LINKAGE_P (expr))
3755169689Skan	{
3756169689Skan	  error ("%qE is not a valid template argument for type %qT "
3757169689Skan		 "because object %qD has not external linkage",
3758169689Skan		 expr, type, expr);
3759169689Skan	  return NULL_TREE;
3760169689Skan	}
376150397Sobrien
3762169689Skan      expr = build_nop (type, build_address (expr));
3763169689Skan    }
3764169689Skan  /* [temp.arg.nontype]/5, bullet 4
376550397Sobrien
3766169689Skan     For a non-type template-parameter of type pointer to function, only
3767169689Skan     the function-to-pointer conversion (_conv.func_) is applied. If the
3768169689Skan     template-argument represents a set of overloaded functions (or a
3769169689Skan     pointer to such), the matching function is selected from the set
3770169689Skan     (_over.over_).  */
3771169689Skan  else if (TYPE_PTRFN_P (type))
3772169689Skan    {
3773169689Skan      /* If the argument is a template-id, we might not have enough
3774169689Skan	 context information to decay the pointer.  */
3775169689Skan      if (!type_unknown_p (expr_type))
3776169689Skan	{
3777169689Skan	  expr = decay_conversion (expr);
3778169689Skan	  if (expr == error_mark_node)
3779169689Skan	    return error_mark_node;
3780169689Skan	}
378190075Sobrien
3782169689Skan      expr = convert_nontype_argument_function (type, expr);
3783169689Skan      if (!expr || expr == error_mark_node)
3784169689Skan	return expr;
3785169689Skan    }
3786169689Skan  /* [temp.arg.nontype]/5, bullet 5
378750397Sobrien
3788169689Skan     For a non-type template-parameter of type reference to function, no
3789169689Skan     conversions apply. If the template-argument represents a set of
3790169689Skan     overloaded functions, the matching function is selected from the set
3791169689Skan     (_over.over_).  */
3792169689Skan  else if (TYPE_REFFN_P (type))
3793169689Skan    {
3794169689Skan      if (TREE_CODE (expr) == ADDR_EXPR)
3795169689Skan	{
3796169689Skan	  error ("%qE is not a valid template argument for type %qT "
3797169689Skan		 "because it is a pointer", expr, type);
3798169689Skan	  inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3799169689Skan	  return NULL_TREE;
3800169689Skan	}
380150397Sobrien
3802169689Skan      expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3803169689Skan      if (!expr || expr == error_mark_node)
3804169689Skan	return expr;
380550397Sobrien
3806169689Skan      expr = build_nop (type, build_address (expr));
3807169689Skan    }
3808169689Skan  /* [temp.arg.nontype]/5, bullet 6
380950397Sobrien
3810169689Skan     For a non-type template-parameter of type pointer to member function,
3811169689Skan     no conversions apply. If the template-argument represents a set of
3812169689Skan     overloaded member functions, the matching member function is selected
3813169689Skan     from the set (_over.over_).  */
3814169689Skan  else if (TYPE_PTRMEMFUNC_P (type))
3815169689Skan    {
3816169689Skan      expr = instantiate_type (type, expr, tf_none);
3817169689Skan      if (expr == error_mark_node)
3818169689Skan	return error_mark_node;
381950397Sobrien
3820169689Skan      /* There is no way to disable standard conversions in
3821169689Skan	 resolve_address_of_overloaded_function (called by
3822169689Skan	 instantiate_type). It is possible that the call succeeded by
3823169689Skan	 converting &B::I to &D::I (where B is a base of D), so we need
3824169689Skan	 to reject this conversion here.
382550397Sobrien
3826169689Skan	 Actually, even if there was a way to disable standard conversions,
3827169689Skan	 it would still be better to reject them here so that we can
3828169689Skan	 provide a superior diagnostic.  */
3829169689Skan      if (!same_type_p (TREE_TYPE (expr), type))
3830169689Skan	{
3831169689Skan	  /* Make sure we are just one standard conversion off.  */
3832169689Skan	  gcc_assert (can_convert (type, TREE_TYPE (expr)));
3833169689Skan	  error ("%qE is not a valid template argument for type %qT "
3834169689Skan		 "because it is of type %qT", expr, type,
3835169689Skan		 TREE_TYPE (expr));
3836169689Skan	  inform ("standard conversions are not allowed in this context");
3837169689Skan	  return NULL_TREE;
3838169689Skan	}
3839169689Skan    }
3840169689Skan  /* [temp.arg.nontype]/5, bullet 7
384150397Sobrien
3842169689Skan     For a non-type template-parameter of type pointer to data member,
3843169689Skan     qualification conversions (_conv.qual_) are applied.  */
3844169689Skan  else if (TYPE_PTRMEM_P (type))
3845169689Skan    {
3846169689Skan      expr = perform_qualification_conversions (type, expr);
3847169689Skan      if (expr == error_mark_node)
384850397Sobrien	return expr;
384950397Sobrien    }
3850169689Skan  /* A template non-type parameter must be one of the above.  */
3851169689Skan  else
3852169689Skan    gcc_unreachable ();
385350397Sobrien
3854169689Skan  /* Sanity check: did we actually convert the argument to the
3855169689Skan     right type?  */
3856169689Skan  gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3857169689Skan  return expr;
385818334Speter}
385918334Speter
3860169689Skan
3861169689Skan/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3862169689Skan   template template parameters.  Both PARM_PARMS and ARG_PARMS are
3863169689Skan   vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
386450397Sobrien   or PARM_DECL.
386518334Speter
386650397Sobrien   Consider the example:
3867169689Skan     template <class T> class A;
3868169689Skan     template<template <class U> class TT> class B;
386950397Sobrien
3870169689Skan   For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3871169689Skan   the parameters to A, and OUTER_ARGS contains A.  */
387250397Sobrien
387350397Sobrienstatic int
3874169689Skancoerce_template_template_parms (tree parm_parms,
3875169689Skan				tree arg_parms,
3876169689Skan				tsubst_flags_t complain,
3877132718Skan				tree in_decl,
3878169689Skan				tree outer_args)
387950397Sobrien{
388050397Sobrien  int nparms, nargs, i;
388150397Sobrien  tree parm, arg;
388250397Sobrien
3883169689Skan  gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3884169689Skan  gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
388550397Sobrien
388650397Sobrien  nparms = TREE_VEC_LENGTH (parm_parms);
388750397Sobrien  nargs = TREE_VEC_LENGTH (arg_parms);
388850397Sobrien
3889169689Skan  if (nargs != nparms)
389050397Sobrien    return 0;
389150397Sobrien
389250397Sobrien  for (i = 0; i < nparms; ++i)
389350397Sobrien    {
3894169689Skan      if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3895169689Skan          || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
3896169689Skan        continue;
3897169689Skan
389850397Sobrien      parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
389950397Sobrien      arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
390050397Sobrien
390150397Sobrien      if (arg == NULL_TREE || arg == error_mark_node
3902169689Skan	  || parm == NULL_TREE || parm == error_mark_node)
390350397Sobrien	return 0;
390450397Sobrien
390550397Sobrien      if (TREE_CODE (arg) != TREE_CODE (parm))
3906169689Skan	return 0;
390750397Sobrien
390850397Sobrien      switch (TREE_CODE (parm))
390950397Sobrien	{
391050397Sobrien	case TYPE_DECL:
391150397Sobrien	  break;
391250397Sobrien
391350397Sobrien	case TEMPLATE_DECL:
391450397Sobrien	  /* We encounter instantiations of templates like
391550397Sobrien	       template <template <template <class> class> class TT>
391650397Sobrien	       class C;  */
391752284Sobrien	  {
391852284Sobrien	    tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
391952284Sobrien	    tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
392050397Sobrien
3921132718Skan	    if (!coerce_template_template_parms
3922132718Skan		(parmparm, argparm, complain, in_decl, outer_args))
392352284Sobrien	      return 0;
392452284Sobrien	  }
392552284Sobrien	  break;
392652284Sobrien
392750397Sobrien	case PARM_DECL:
392850397Sobrien	  /* The tsubst call is used to handle cases such as
3929169689Skan
3930169689Skan	       template <int> class C {};
3931169689Skan	       template <class T, template <T> class TT> class D {};
3932169689Skan	       D<int, C> d;
3933169689Skan
393450397Sobrien	     i.e. the parameter list of TT depends on earlier parameters.  */
3935169689Skan	  if (!dependent_type_p (TREE_TYPE (arg))
3936169689Skan	      && !same_type_p
3937169689Skan		    (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3938169689Skan			     TREE_TYPE (arg)))
393950397Sobrien	    return 0;
394050397Sobrien	  break;
3941169689Skan
394250397Sobrien	default:
3943169689Skan	  gcc_unreachable ();
394450397Sobrien	}
394550397Sobrien    }
394650397Sobrien  return 1;
394750397Sobrien}
394850397Sobrien
394952284Sobrien/* Convert the indicated template ARG as necessary to match the
395052284Sobrien   indicated template PARM.  Returns the converted ARG, or
395196263Sobrien   error_mark_node if the conversion was unsuccessful.  Error and
395296263Sobrien   warning messages are issued under control of COMPLAIN.  This
395396263Sobrien   conversion is for the Ith parameter in the parameter list.  ARGS is
395496263Sobrien   the full set of template arguments deduced so far.  */
395550397Sobrien
395652284Sobrienstatic tree
3957169689Skanconvert_template_argument (tree parm,
3958169689Skan			   tree arg,
3959169689Skan			   tree args,
3960169689Skan			   tsubst_flags_t complain,
3961169689Skan			   int i,
3962169689Skan			   tree in_decl)
396352284Sobrien{
396452284Sobrien  tree val;
396552284Sobrien  int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
396652284Sobrien
3967169689Skan  if (TREE_CODE (arg) == TREE_LIST
3968132718Skan      && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3969169689Skan    {
397052284Sobrien      /* The template argument was the name of some
397152284Sobrien	 member function.  That's usually
3972117395Skan	 invalid, but static members are OK.  In any
397352284Sobrien	 case, grab the underlying fields/functions
397452284Sobrien	 and issue an error later if required.  */
397552284Sobrien      arg = TREE_VALUE (arg);
397652284Sobrien      TREE_TYPE (arg) = unknown_type_node;
397752284Sobrien    }
397852284Sobrien
397952284Sobrien  requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
398052284Sobrien  requires_type = (TREE_CODE (parm) == TYPE_DECL
398152284Sobrien		   || requires_tmpl_type);
398252284Sobrien
3983132718Skan  is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3984132718Skan		   && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3985132718Skan		  || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3986132718Skan		  || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3987169689Skan
398890075Sobrien  if (is_tmpl_type
398990075Sobrien      && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
399090075Sobrien	  || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
399152284Sobrien    arg = TYPE_STUB_DECL (arg);
399252284Sobrien
399390075Sobrien  is_type = TYPE_P (arg) || is_tmpl_type;
399452284Sobrien
399552284Sobrien  if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
399652284Sobrien      && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
399752284Sobrien    {
3998169689Skan      pedwarn ("to refer to a type member of a template parameter, "
3999169689Skan	       "use %<typename %E%>", arg);
4000169689Skan
400152284Sobrien      arg = make_typename_type (TREE_OPERAND (arg, 0),
400290075Sobrien				TREE_OPERAND (arg, 1),
4003169689Skan				typename_type,
400496263Sobrien				complain & tf_error);
400552284Sobrien      is_type = 1;
400652284Sobrien    }
400752284Sobrien  if (is_type != requires_type)
400852284Sobrien    {
400952284Sobrien      if (in_decl)
401052284Sobrien	{
401196263Sobrien	  if (complain & tf_error)
401252284Sobrien	    {
4013169689Skan	      error ("type/value mismatch at argument %d in template "
4014169689Skan		     "parameter list for %qD",
4015169689Skan		     i + 1, in_decl);
401652284Sobrien	      if (is_type)
4017169689Skan		error ("  expected a constant of type %qT, got %qT",
4018169689Skan		       TREE_TYPE (parm),
4019169689Skan		       (is_tmpl_type ? DECL_NAME (arg) : arg));
4020132718Skan	      else if (requires_tmpl_type)
4021169689Skan		error ("  expected a class template, got %qE", arg);
402252284Sobrien	      else
4023169689Skan		error ("  expected a type, got %qE", arg);
402452284Sobrien	    }
402552284Sobrien	}
402652284Sobrien      return error_mark_node;
402752284Sobrien    }
402852284Sobrien  if (is_tmpl_type ^ requires_tmpl_type)
402952284Sobrien    {
403096263Sobrien      if (in_decl && (complain & tf_error))
403152284Sobrien	{
4032169689Skan	  error ("type/value mismatch at argument %d in template "
4033169689Skan		 "parameter list for %qD",
4034169689Skan		 i + 1, in_decl);
403552284Sobrien	  if (is_tmpl_type)
4036169689Skan	    error ("  expected a type, got %qT", DECL_NAME (arg));
403752284Sobrien	  else
4038169689Skan	    error ("  expected a class template, got %qT", arg);
403952284Sobrien	}
404052284Sobrien      return error_mark_node;
404152284Sobrien    }
4042169689Skan
404352284Sobrien  if (is_type)
404452284Sobrien    {
404552284Sobrien      if (requires_tmpl_type)
404652284Sobrien	{
404790075Sobrien	  if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
404890075Sobrien	    /* The number of argument required is not known yet.
404990075Sobrien	       Just accept it for now.  */
405090075Sobrien	    val = TREE_TYPE (arg);
405152284Sobrien	  else
405252284Sobrien	    {
405390075Sobrien	      tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
405490075Sobrien	      tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
405590075Sobrien
405690075Sobrien	      if (coerce_template_template_parms (parmparm, argparm,
405790075Sobrien						  complain, in_decl,
4058169689Skan						  args))
405952284Sobrien		{
406090075Sobrien		  val = arg;
4061169689Skan
4062169689Skan		  /* TEMPLATE_TEMPLATE_PARM node is preferred over
406390075Sobrien		     TEMPLATE_DECL.  */
4064169689Skan		  if (val != error_mark_node
406590075Sobrien		      && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
406690075Sobrien		    val = TREE_TYPE (val);
406752284Sobrien		}
406890075Sobrien	      else
406990075Sobrien		{
407096263Sobrien		  if (in_decl && (complain & tf_error))
407190075Sobrien		    {
4072169689Skan		      error ("type/value mismatch at argument %d in "
4073169689Skan			     "template parameter list for %qD",
4074169689Skan			     i + 1, in_decl);
4075169689Skan		      error ("  expected a template of type %qD, got %qD",
4076169689Skan			     parm, arg);
407790075Sobrien		    }
4078169689Skan
407990075Sobrien		  val = error_mark_node;
408090075Sobrien		}
408152284Sobrien	    }
408252284Sobrien	}
408352284Sobrien      else
4084169689Skan	val = arg;
4085169689Skan      /* We only form one instance of each template specialization.
4086169689Skan	 Therefore, if we use a non-canonical variant (i.e., a
4087169689Skan	 typedef), any future messages referring to the type will use
4088169689Skan	 the typedef, which is confusing if those future uses do not
4089169689Skan	 themselves also use the typedef.  */
4090169689Skan      if (TYPE_P (val))
4091169689Skan	val = canonical_type_variant (val);
409252284Sobrien    }
409352284Sobrien  else
409452284Sobrien    {
409552284Sobrien      tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
409652284Sobrien
409790075Sobrien      if (invalid_nontype_parm_type_p (t, complain))
4098169689Skan	return error_mark_node;
4099169689Skan
410052284Sobrien      if (!uses_template_parms (arg) && !uses_template_parms (t))
410152284Sobrien	/* We used to call digest_init here.  However, digest_init
410252284Sobrien	   will report errors, which we don't want when complain
410352284Sobrien	   is zero.  More importantly, digest_init will try too
410452284Sobrien	   hard to convert things: for example, `0' should not be
410552284Sobrien	   converted to pointer type at this point according to
410652284Sobrien	   the standard.  Accepting this is not merely an
410752284Sobrien	   extension, since deciding whether or not these
410852284Sobrien	   conversions can occur is part of determining which
410990075Sobrien	   function template to call, or whether a given explicit
4110117395Skan	   argument specification is valid.  */
411152284Sobrien	val = convert_nontype_argument (t, arg);
411252284Sobrien      else
411352284Sobrien	val = arg;
411452284Sobrien
411552284Sobrien      if (val == NULL_TREE)
411652284Sobrien	val = error_mark_node;
411796263Sobrien      else if (val == error_mark_node && (complain & tf_error))
4118169689Skan	error ("could not convert template argument %qE to %qT",  arg, t);
411952284Sobrien    }
412052284Sobrien
412152284Sobrien  return val;
412252284Sobrien}
412352284Sobrien
412452284Sobrien/* Convert all template arguments to their appropriate types, and
412552284Sobrien   return a vector containing the innermost resulting template
412696263Sobrien   arguments.  If any error occurs, return error_mark_node. Error and
4127132718Skan   warning messages are issued under control of COMPLAIN.
412852284Sobrien
4129169689Skan   If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4130169689Skan   for arguments not specified in ARGS.  Otherwise, if
4131169689Skan   USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4132169689Skan   unspecified arguments.  If REQUIRE_ALL_ARGS is true, but
4133169689Skan   USE_DEFAULT_ARGS is false, then all arguments must be specified in
4134169689Skan   ARGS.  */
4135169689Skan
413618334Speterstatic tree
4137169689Skancoerce_template_parms (tree parms,
4138169689Skan		       tree args,
4139169689Skan		       tree in_decl,
4140132718Skan		       tsubst_flags_t complain,
4141169689Skan		       bool require_all_args,
4142169689Skan		       bool use_default_args)
414318334Speter{
414418334Speter  int nparms, nargs, i, lost = 0;
414552284Sobrien  tree inner_args;
414652284Sobrien  tree new_args;
414752284Sobrien  tree new_inner_args;
4148169689Skan  bool saved_skip_evaluation;
414918334Speter
415090075Sobrien  inner_args = INNERMOST_TEMPLATE_ARGS (args);
4151132718Skan  nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
415218334Speter  nparms = TREE_VEC_LENGTH (parms);
415318334Speter
415418334Speter  if (nargs > nparms
415518334Speter      || (nargs < nparms
4156169689Skan	  && require_all_args
4157169689Skan	  && (!use_default_args
4158169689Skan	      || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4159169689Skan                  && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
416018334Speter    {
4161169689Skan      if (complain & tf_error)
416250397Sobrien	{
416390075Sobrien	  error ("wrong number of template arguments (%d, should be %d)",
4164169689Skan		 nargs, nparms);
4165169689Skan
416650397Sobrien	  if (in_decl)
4167169689Skan	    error ("provided for %q+D", in_decl);
416850397Sobrien	}
416950397Sobrien
417018334Speter      return error_mark_node;
417118334Speter    }
417218334Speter
4173169689Skan  /* We need to evaluate the template arguments, even though this
4174169689Skan     template-id may be nested within a "sizeof".  */
4175169689Skan  saved_skip_evaluation = skip_evaluation;
4176169689Skan  skip_evaluation = false;
417790075Sobrien  new_inner_args = make_tree_vec (nparms);
417852284Sobrien  new_args = add_outermost_template_args (args, new_inner_args);
417952284Sobrien  for (i = 0; i < nparms; i++)
418018334Speter    {
418152284Sobrien      tree arg;
418252284Sobrien      tree parm;
418350397Sobrien
418452284Sobrien      /* Get the Ith template parameter.  */
418552284Sobrien      parm = TREE_VEC_ELT (parms, i);
4186169689Skan
4187169689Skan      if (parm == error_mark_node)
4188169689Skan      {
4189169689Skan        TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
4190169689Skan        continue;
4191169689Skan      }
419252284Sobrien
419352284Sobrien      /* Calculate the Ith argument.  */
4194132718Skan      if (i < nargs)
419552284Sobrien	arg = TREE_VEC_ELT (inner_args, i);
4196169689Skan      else if (require_all_args)
4197132718Skan	/* There must be a default arg in this case.  */
4198132718Skan	arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4199132718Skan				   complain, in_decl);
420052284Sobrien      else
4201132718Skan	break;
4202169689Skan
4203169689Skan      gcc_assert (arg);
4204132718Skan      if (arg == error_mark_node)
4205169689Skan	{
4206169689Skan	  if (complain & tf_error)
4207169689Skan	    error ("template argument %d is invalid", i + 1);
4208169689Skan	}
4209169689Skan      else
4210169689Skan	arg = convert_template_argument (TREE_VALUE (parm),
421152284Sobrien					 arg, new_args, complain, i,
4212169689Skan					 in_decl);
4213169689Skan
421452284Sobrien      if (arg == error_mark_node)
421518334Speter	lost++;
421652284Sobrien      TREE_VEC_ELT (new_inner_args, i) = arg;
421752284Sobrien    }
4218169689Skan  skip_evaluation = saved_skip_evaluation;
421918334Speter
422018334Speter  if (lost)
422118334Speter    return error_mark_node;
422252284Sobrien
422352284Sobrien  return new_inner_args;
422418334Speter}
422518334Speter
422652284Sobrien/* Returns 1 if template args OT and NT are equivalent.  */
422752284Sobrien
422852284Sobrienstatic int
4229132718Skantemplate_args_equal (tree ot, tree nt)
423052284Sobrien{
423152284Sobrien  if (nt == ot)
423252284Sobrien    return 1;
423390075Sobrien
423452284Sobrien  if (TREE_CODE (nt) == TREE_VEC)
423552284Sobrien    /* For member templates */
423690075Sobrien    return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
423790075Sobrien  else if (TYPE_P (nt))
423890075Sobrien    return TYPE_P (ot) && same_type_p (ot, nt);
423990075Sobrien  else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
424090075Sobrien    return 0;
424152284Sobrien  else
4242132718Skan    return cp_tree_equal (ot, nt);
424352284Sobrien}
424452284Sobrien
424552284Sobrien/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
424650397Sobrien   of template arguments.  Returns 0 otherwise.  */
424750397Sobrien
424850397Sobrienint
4249132718Skancomp_template_args (tree oldargs, tree newargs)
425050397Sobrien{
425150397Sobrien  int i;
425250397Sobrien
425350397Sobrien  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
425450397Sobrien    return 0;
425550397Sobrien
425650397Sobrien  for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
425750397Sobrien    {
425850397Sobrien      tree nt = TREE_VEC_ELT (newargs, i);
425950397Sobrien      tree ot = TREE_VEC_ELT (oldargs, i);
426050397Sobrien
426152284Sobrien      if (! template_args_equal (ot, nt))
426250397Sobrien	return 0;
426350397Sobrien    }
426450397Sobrien  return 1;
426550397Sobrien}
426650397Sobrien
426718334Speter/* Given class template name and parameter list, produce a user-friendly name
426818334Speter   for the instantiation.  */
426950397Sobrien
427018334Speterstatic char *
4271132718Skanmangle_class_name_for_template (const char* name, tree parms, tree arglist)
427218334Speter{
427318334Speter  static struct obstack scratch_obstack;
427418334Speter  static char *scratch_firstobj;
427518334Speter  int i, nparms;
427618334Speter
427718334Speter  if (!scratch_firstobj)
427850397Sobrien    gcc_obstack_init (&scratch_obstack);
427918334Speter  else
428018334Speter    obstack_free (&scratch_obstack, scratch_firstobj);
4281169689Skan  scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
428218334Speter
428390075Sobrien#define ccat(C)	obstack_1grow (&scratch_obstack, (C));
428490075Sobrien#define cat(S)	obstack_grow (&scratch_obstack, (S), strlen (S))
428518334Speter
428618334Speter  cat (name);
428718334Speter  ccat ('<');
428818334Speter  nparms = TREE_VEC_LENGTH (parms);
428990075Sobrien  arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4290169689Skan  gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
429118334Speter  for (i = 0; i < nparms; i++)
429218334Speter    {
4293169689Skan      tree parm;
4294169689Skan      tree arg;
429518334Speter
4296169689Skan      parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4297169689Skan      arg = TREE_VEC_ELT (arglist, i);
4298169689Skan
4299169689Skan      if (parm == error_mark_node)
4300169689Skan	continue;
4301169689Skan
430218334Speter      if (i)
430318334Speter	ccat (',');
430418334Speter
430518334Speter      if (TREE_CODE (parm) == TYPE_DECL)
430618334Speter	{
430790075Sobrien	  cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
430818334Speter	  continue;
430918334Speter	}
431050397Sobrien      else if (TREE_CODE (parm) == TEMPLATE_DECL)
431150397Sobrien	{
431250397Sobrien	  if (TREE_CODE (arg) == TEMPLATE_DECL)
431350397Sobrien	    {
4314169689Skan	      /* Already substituted with real template.  Just output
431550397Sobrien		 the template name here */
4316169689Skan	      tree context = DECL_CONTEXT (arg);
4317169689Skan	      if (context)
4318169689Skan		{
4319169689Skan		  /* The template may be defined in a namespace, or
4320169689Skan		     may be a member template.  */
4321169689Skan		  gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4322169689Skan			      || CLASS_TYPE_P (context));
4323169689Skan		  cat (decl_as_string (DECL_CONTEXT (arg),
4324169689Skan				      TFF_PLAIN_IDENTIFIER));
4325169689Skan		  cat ("::");
432650397Sobrien		}
432750397Sobrien	      cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
432850397Sobrien	    }
432950397Sobrien	  else
4330132718Skan	    /* Output the parameter declaration.  */
433190075Sobrien	    cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
433250397Sobrien	  continue;
433350397Sobrien	}
433418334Speter      else
4335169689Skan	gcc_assert (TREE_CODE (parm) == PARM_DECL);
433618334Speter
433718334Speter      /* No need to check arglist against parmlist here; we did that
433818334Speter	 in coerce_template_parms, called from lookup_template_class.  */
433990075Sobrien      cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
434018334Speter    }
434118334Speter  {
434218334Speter    char *bufp = obstack_next_free (&scratch_obstack);
434318334Speter    int offset = 0;
434418334Speter    while (bufp[offset - 1] == ' ')
434518334Speter      offset--;
434618334Speter    obstack_blank_fast (&scratch_obstack, offset);
434718334Speter
434818334Speter    /* B<C<char> >, not B<C<char>> */
434918334Speter    if (bufp[offset - 1] == '>')
435018334Speter      ccat (' ');
435118334Speter  }
435218334Speter  ccat ('>');
435318334Speter  ccat ('\0');
435418334Speter  return (char *) obstack_base (&scratch_obstack);
435518334Speter}
435618334Speter
435750397Sobrienstatic tree
4358132718Skanclasstype_mangled_name (tree t)
435950397Sobrien{
436050397Sobrien  if (CLASSTYPE_TEMPLATE_INFO (t)
436152284Sobrien      /* Specializations have already had their names set up in
436252284Sobrien	 lookup_template_class.  */
436352284Sobrien      && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
436450397Sobrien    {
436552284Sobrien      tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
436652284Sobrien
436752284Sobrien      /* For non-primary templates, the template parameters are
436852284Sobrien	 implicit from their surrounding context.  */
436952284Sobrien      if (PRIMARY_TEMPLATE_P (tmpl))
437052284Sobrien	{
437152284Sobrien	  tree name = DECL_NAME (tmpl);
437252284Sobrien	  char *mangled_name = mangle_class_name_for_template
4373169689Skan	    (IDENTIFIER_POINTER (name),
437452284Sobrien	     DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
437552284Sobrien	     CLASSTYPE_TI_ARGS (t));
437652284Sobrien	  tree id = get_identifier (mangled_name);
437752284Sobrien	  IDENTIFIER_TEMPLATE (id) = name;
437852284Sobrien	  return id;
437952284Sobrien	}
438050397Sobrien    }
438152284Sobrien
438252284Sobrien  return TYPE_IDENTIFIER (t);
438350397Sobrien}
438450397Sobrien
438550397Sobrienstatic void
4386132718Skanadd_pending_template (tree d)
438750397Sobrien{
438890075Sobrien  tree ti = (TYPE_P (d)
438990075Sobrien	     ? CLASSTYPE_TEMPLATE_INFO (d)
439090075Sobrien	     : DECL_TEMPLATE_INFO (d));
439190075Sobrien  tree pt;
439290075Sobrien  int level;
439350397Sobrien
439450397Sobrien  if (TI_PENDING_TEMPLATE_FLAG (ti))
439550397Sobrien    return;
439650397Sobrien
439790075Sobrien  /* We are called both from instantiate_decl, where we've already had a
439890075Sobrien     tinst_level pushed, and instantiate_template, where we haven't.
439990075Sobrien     Compensate.  */
440090075Sobrien  level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
440190075Sobrien
440290075Sobrien  if (level)
440390075Sobrien    push_tinst_level (d);
440490075Sobrien
440590075Sobrien  pt = tree_cons (current_tinst_level, d, NULL_TREE);
440690075Sobrien  if (last_pending_template)
440790075Sobrien    TREE_CHAIN (last_pending_template) = pt;
440890075Sobrien  else
440990075Sobrien    pending_templates = pt;
441090075Sobrien
441190075Sobrien  last_pending_template = pt;
441290075Sobrien
441350397Sobrien  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
441490075Sobrien
441590075Sobrien  if (level)
441690075Sobrien    pop_tinst_level ();
441750397Sobrien}
441850397Sobrien
441950397Sobrien
4420117395Skan/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4421117395Skan   ARGLIST.  Valid choices for FNS are given in the cp-tree.def
4422117395Skan   documentation for TEMPLATE_ID_EXPR.  */
442350397Sobrien
442450397Sobrientree
4425132718Skanlookup_template_function (tree fns, tree arglist)
442650397Sobrien{
442750397Sobrien  tree type;
442850397Sobrien
4429117395Skan  if (fns == error_mark_node || arglist == error_mark_node)
4430117395Skan    return error_mark_node;
4431117395Skan
4432169689Skan  gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4433169689Skan  gcc_assert (fns && (is_overloaded_fn (fns)
4434169689Skan		      || TREE_CODE (fns) == IDENTIFIER_NODE));
443550397Sobrien
4436117395Skan  if (BASELINK_P (fns))
4437117395Skan    {
4438169689Skan      BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4439169689Skan					 unknown_type_node,
4440169689Skan					 BASELINK_FUNCTIONS (fns),
4441169689Skan					 arglist);
4442117395Skan      return fns;
4443117395Skan    }
4444117395Skan
444550397Sobrien  type = TREE_TYPE (fns);
444650397Sobrien  if (TREE_CODE (fns) == OVERLOAD || !type)
444750397Sobrien    type = unknown_type_node;
4448169689Skan
4449169689Skan  return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
445050397Sobrien}
445150397Sobrien
445250397Sobrien/* Within the scope of a template class S<T>, the name S gets bound
445350397Sobrien   (in build_self_reference) to a TYPE_DECL for the class, not a
445450397Sobrien   TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
445550397Sobrien   or one of its enclosing classes, and that type is a template,
445650397Sobrien   return the associated TEMPLATE_DECL.  Otherwise, the original
445750397Sobrien   DECL is returned.  */
445850397Sobrien
4459132718Skantree
4460132718Skanmaybe_get_template_decl_from_type_decl (tree decl)
446150397Sobrien{
446250397Sobrien  return (decl != NULL_TREE
4463169689Skan	  && TREE_CODE (decl) == TYPE_DECL
446450397Sobrien	  && DECL_ARTIFICIAL (decl)
446552284Sobrien	  && CLASS_TYPE_P (TREE_TYPE (decl))
4466169689Skan	  && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
446750397Sobrien    ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
446850397Sobrien}
446950397Sobrien
447018334Speter/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
447118334Speter   parameters, find the desired type.
447218334Speter
447318334Speter   D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
447418334Speter
447518334Speter   IN_DECL, if non-NULL, is the template declaration we are trying to
4476169689Skan   instantiate.
447750397Sobrien
4478117395Skan   If ENTERING_SCOPE is nonzero, we are about to enter the scope of
447952284Sobrien   the class we are looking up.
4480169689Skan
448196263Sobrien   Issue error and warning messages under control of COMPLAIN.
448252284Sobrien
448350397Sobrien   If the template class is really a local class in a template
448450397Sobrien   function, then the FUNCTION_CONTEXT is the function in which it is
4485169689Skan   being instantiated.
448650397Sobrien
4487169689Skan   ??? Note that this function is currently called *twice* for each
4488169689Skan   template-id: the first time from the parser, while creating the
4489169689Skan   incomplete type (finish_template_type), and the second type during the
4490169689Skan   real instantiation (instantiate_template_class). This is surely something
4491169689Skan   that we want to avoid. It also causes some problems with argument
4492169689Skan   coercion (see convert_nontype_argument for more information on this).  */
4493169689Skan
449418334Spetertree
4495169689Skanlookup_template_class (tree d1,
4496169689Skan		       tree arglist,
4497169689Skan		       tree in_decl,
4498169689Skan		       tree context,
4499169689Skan		       int entering_scope,
4500169689Skan		       tsubst_flags_t complain)
450118334Speter{
450250397Sobrien  tree template = NULL_TREE, parmlist;
450352284Sobrien  tree t;
4504169689Skan
4505117395Skan  timevar_push (TV_NAME_LOOKUP);
4506169689Skan
450750397Sobrien  if (TREE_CODE (d1) == IDENTIFIER_NODE)
450850397Sobrien    {
4509169689Skan      tree value = innermost_non_namespace_value (d1);
4510169689Skan      if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4511169689Skan	template = value;
451250397Sobrien      else
451350397Sobrien	{
451450397Sobrien	  if (context)
451550397Sobrien	    push_decl_namespace (context);
4516169689Skan	  template = lookup_name (d1);
451790075Sobrien	  template = maybe_get_template_decl_from_type_decl (template);
451850397Sobrien	  if (context)
451950397Sobrien	    pop_decl_namespace ();
452050397Sobrien	}
452150397Sobrien      if (template)
452250397Sobrien	context = DECL_CONTEXT (template);
452350397Sobrien    }
452450397Sobrien  else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
452550397Sobrien    {
4526132718Skan      tree type = TREE_TYPE (d1);
452752284Sobrien
452852284Sobrien      /* If we are declaring a constructor, say A<T>::A<T>, we will get
452952284Sobrien	 an implicit typename for the second A.  Deal with it.  */
453052284Sobrien      if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
453152284Sobrien	type = TREE_TYPE (type);
4532169689Skan
453352284Sobrien      if (CLASSTYPE_TEMPLATE_INFO (type))
453452284Sobrien	{
453552284Sobrien	  template = CLASSTYPE_TI_TEMPLATE (type);
453652284Sobrien	  d1 = DECL_NAME (template);
453752284Sobrien	}
453850397Sobrien    }
4539169689Skan  else if (TREE_CODE (d1) == ENUMERAL_TYPE
454090075Sobrien	   || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
454150397Sobrien    {
454252284Sobrien      template = TYPE_TI_TEMPLATE (d1);
454350397Sobrien      d1 = DECL_NAME (template);
454450397Sobrien    }
454550397Sobrien  else if (TREE_CODE (d1) == TEMPLATE_DECL
454690075Sobrien	   && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
454750397Sobrien    {
454850397Sobrien      template = d1;
454950397Sobrien      d1 = DECL_NAME (template);
455050397Sobrien      context = DECL_CONTEXT (template);
455150397Sobrien    }
455250397Sobrien
4553169689Skan  /* Issue an error message if we didn't find a template.  */
455418334Speter  if (! template)
455552284Sobrien    {
455696263Sobrien      if (complain & tf_error)
4557169689Skan	error ("%qT is not a template", d1);
4558117395Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
455952284Sobrien    }
456050397Sobrien
456190075Sobrien  if (TREE_CODE (template) != TEMPLATE_DECL
4562169689Skan	 /* Make sure it's a user visible template, if it was named by
4563132718Skan	    the user.  */
4564132718Skan      || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4565132718Skan	  && !PRIMARY_TEMPLATE_P (template)))
456618334Speter    {
456796263Sobrien      if (complain & tf_error)
4568169689Skan	{
4569169689Skan	  error ("non-template type %qT used as a template", d1);
4570169689Skan	  if (in_decl)
4571169689Skan	    error ("for template declaration %q+D", in_decl);
457290075Sobrien	}
4573117395Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
457418334Speter    }
457518334Speter
4576132718Skan  complain &= ~tf_user;
4577169689Skan
457850397Sobrien  if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
457918334Speter    {
458050397Sobrien      /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4581169689Skan	 template arguments */
458218334Speter
458390075Sobrien      tree parm;
458450397Sobrien      tree arglist2;
458518334Speter
458650397Sobrien      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
458718334Speter
458890075Sobrien      /* Consider an example where a template template parameter declared as
458990075Sobrien
459090075Sobrien	   template <class T, class U = std::allocator<T> > class TT
459190075Sobrien
4592169689Skan	 The template parameter level of T and U are one level larger than
4593169689Skan	 of TT.  To proper process the default argument of U, say when an
459490075Sobrien	 instantiation `TT<int>' is seen, we need to build the full
4595102780Skan	 arguments containing {int} as the innermost level.  Outer levels,
4596102780Skan	 available when not appearing as default template argument, can be
4597102780Skan	 obtained from `current_template_args ()'.
459890075Sobrien
4599102780Skan	 Suppose that TT is later substituted with std::vector.  The above
4600102780Skan	 instantiation is `TT<int, std::allocator<T> >' with TT at
4601102780Skan	 level 1, and T at level 2, while the template arguments at level 1
4602102780Skan	 becomes {std::vector} and the inner level 2 is {int}.  */
4603102780Skan
4604102780Skan      if (current_template_parms)
460590075Sobrien	arglist = add_to_template_args (current_template_args (), arglist);
460690075Sobrien
460790075Sobrien      arglist2 = coerce_template_parms (parmlist, arglist, template,
4608169689Skan					complain,
4609169689Skan					/*require_all_args=*/true,
4610169689Skan					/*use_default_args=*/true);
4611132718Skan      if (arglist2 == error_mark_node
4612132718Skan	  || (!uses_template_parms (arglist2)
4613132718Skan	      && check_instantiated_args (template, arglist2, complain)))
4614169689Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
461550397Sobrien
461690075Sobrien      parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4617117395Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
461850397Sobrien    }
4619169689Skan  else
462018334Speter    {
462152284Sobrien      tree template_type = TREE_TYPE (template);
462252284Sobrien      tree gen_tmpl;
462352284Sobrien      tree type_decl;
462452284Sobrien      tree found = NULL_TREE;
462552284Sobrien      int arg_depth;
462652284Sobrien      int parm_depth;
462752284Sobrien      int is_partial_instantiation;
462818334Speter
462952284Sobrien      gen_tmpl = most_general_template (template);
463052284Sobrien      parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
463152284Sobrien      parm_depth = TMPL_PARMS_DEPTH (parmlist);
463252284Sobrien      arg_depth = TMPL_ARGS_DEPTH (arglist);
463350397Sobrien
463452284Sobrien      if (arg_depth == 1 && parm_depth > 1)
463518334Speter	{
463652284Sobrien	  /* We've been given an incomplete set of template arguments.
463752284Sobrien	     For example, given:
463852284Sobrien
463952284Sobrien	       template <class T> struct S1 {
4640169689Skan		 template <class U> struct S2 {};
464152284Sobrien		 template <class U> struct S2<U*> {};
4642169689Skan		};
4643169689Skan
464452284Sobrien	     we will be called with an ARGLIST of `U*', but the
464552284Sobrien	     TEMPLATE will be `template <class T> template
464652284Sobrien	     <class U> struct S1<T>::S2'.  We must fill in the missing
464752284Sobrien	     arguments.  */
4648169689Skan	  arglist
464952284Sobrien	    = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
465052284Sobrien					   arglist);
465152284Sobrien	  arg_depth = TMPL_ARGS_DEPTH (arglist);
465252284Sobrien	}
465352284Sobrien
465490075Sobrien      /* Now we should have enough arguments.  */
4655169689Skan      gcc_assert (parm_depth == arg_depth);
4656169689Skan
465752284Sobrien      /* From here on, we're only interested in the most general
465852284Sobrien	 template.  */
465952284Sobrien      template = gen_tmpl;
466052284Sobrien
466152284Sobrien      /* Calculate the BOUND_ARGS.  These will be the args that are
466252284Sobrien	 actually tsubst'd into the definition to create the
466352284Sobrien	 instantiation.  */
466452284Sobrien      if (parm_depth > 1)
466552284Sobrien	{
466650397Sobrien	  /* We have multiple levels of arguments to coerce, at once.  */
466750397Sobrien	  int i;
466852284Sobrien	  int saved_depth = TMPL_ARGS_DEPTH (arglist);
466952284Sobrien
467090075Sobrien	  tree bound_args = make_tree_vec (parm_depth);
4671169689Skan
467252284Sobrien	  for (i = saved_depth,
4673169689Skan		 t = DECL_TEMPLATE_PARMS (template);
467452284Sobrien	       i > 0 && t != NULL_TREE;
467550397Sobrien	       --i, t = TREE_CHAIN (t))
467652284Sobrien	    {
467752284Sobrien	      tree a = coerce_template_parms (TREE_VALUE (t),
467890075Sobrien					      arglist, template,
4679169689Skan					      complain,
4680169689Skan					      /*require_all_args=*/true,
4681169689Skan					      /*use_default_args=*/true);
4682132718Skan
4683132718Skan	      /* Don't process further if one of the levels fails.  */
4684132718Skan	      if (a == error_mark_node)
4685132718Skan		{
4686132718Skan		  /* Restore the ARGLIST to its full size.  */
4687132718Skan		  TREE_VEC_LENGTH (arglist) = saved_depth;
4688132718Skan		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4689132718Skan		}
4690169689Skan
469152284Sobrien	      SET_TMPL_ARGS_LEVEL (bound_args, i, a);
469252284Sobrien
469352284Sobrien	      /* We temporarily reduce the length of the ARGLIST so
469452284Sobrien		 that coerce_template_parms will see only the arguments
469552284Sobrien		 corresponding to the template parameters it is
469652284Sobrien		 examining.  */
469752284Sobrien	      TREE_VEC_LENGTH (arglist)--;
469852284Sobrien	    }
469952284Sobrien
470052284Sobrien	  /* Restore the ARGLIST to its full size.  */
470152284Sobrien	  TREE_VEC_LENGTH (arglist) = saved_depth;
470252284Sobrien
470352284Sobrien	  arglist = bound_args;
470418334Speter	}
470518334Speter      else
470652284Sobrien	arglist
470752284Sobrien	  = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
470890075Sobrien				   INNERMOST_TEMPLATE_ARGS (arglist),
470990075Sobrien				   template,
4710169689Skan				   complain,
4711169689Skan				   /*require_all_args=*/true,
4712169689Skan				   /*use_default_args=*/true);
471352284Sobrien
471452284Sobrien      if (arglist == error_mark_node)
471552284Sobrien	/* We were unable to bind the arguments.  */
4716117395Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
471752284Sobrien
471852284Sobrien      /* In the scope of a template class, explicit references to the
471952284Sobrien	 template class refer to the type of the template, not any
472052284Sobrien	 instantiation of it.  For example, in:
4721169689Skan
472252284Sobrien	   template <class T> class C { void f(C<T>); }
472352284Sobrien
472452284Sobrien	 the `C<T>' is just the same as `C'.  Outside of the
472552284Sobrien	 class, however, such a reference is an instantiation.  */
472652284Sobrien      if (comp_template_args (TYPE_TI_ARGS (template_type),
472752284Sobrien			      arglist))
472818334Speter	{
472952284Sobrien	  found = template_type;
4730169689Skan
473152284Sobrien	  if (!entering_scope && PRIMARY_TEMPLATE_P (template))
473218334Speter	    {
473352284Sobrien	      tree ctx;
4734169689Skan
4735169689Skan	      for (ctx = current_class_type;
4736132718Skan		   ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4737132718Skan		   ctx = (TYPE_P (ctx)
4738132718Skan			  ? TYPE_CONTEXT (ctx)
4739132718Skan			  : DECL_CONTEXT (ctx)))
4740132718Skan		if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4741132718Skan		  goto found_ctx;
4742169689Skan
4743132718Skan	      /* We're not in the scope of the class, so the
4744132718Skan		 TEMPLATE_TYPE is not the type we want after all.  */
4745132718Skan	      found = NULL_TREE;
4746132718Skan	    found_ctx:;
474718334Speter	    }
474852284Sobrien	}
474952284Sobrien      if (found)
4750169689Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
475150397Sobrien
4752169689Skan      /* If we already have this specialization, return it.  */
4753169689Skan      found = retrieve_specialization (template, arglist,
4754169689Skan				       /*class_specializations_p=*/false);
4755169689Skan      if (found)
4756169689Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
475790075Sobrien
475852284Sobrien      /* This type is a "partial instantiation" if any of the template
475990075Sobrien	 arguments still involve template parameters.  Note that we set
476052284Sobrien	 IS_PARTIAL_INSTANTIATION for partial specializations as
476152284Sobrien	 well.  */
476252284Sobrien      is_partial_instantiation = uses_template_parms (arglist);
476350397Sobrien
4764132718Skan      /* If the deduced arguments are invalid, then the binding
4765132718Skan	 failed.  */
4766132718Skan      if (!is_partial_instantiation
4767132718Skan	  && check_instantiated_args (template,
4768132718Skan				      INNERMOST_TEMPLATE_ARGS (arglist),
4769132718Skan				      complain))
4770132718Skan	POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4771169689Skan
4772169689Skan      if (!is_partial_instantiation
477390075Sobrien	  && !PRIMARY_TEMPLATE_P (template)
477490075Sobrien	  && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
477590075Sobrien	{
477690075Sobrien	  found = xref_tag_from_type (TREE_TYPE (template),
477790075Sobrien				      DECL_NAME (template),
4778169689Skan				      /*tag_scope=*/ts_global);
4779117395Skan	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
478090075Sobrien	}
4781169689Skan
478290075Sobrien      context = tsubst (DECL_CONTEXT (template), arglist,
478396263Sobrien			complain, in_decl);
478490075Sobrien      if (!context)
478590075Sobrien	context = global_namespace;
478690075Sobrien
478752284Sobrien      /* Create the type.  */
478852284Sobrien      if (TREE_CODE (template_type) == ENUMERAL_TYPE)
478918334Speter	{
479052284Sobrien	  if (!is_partial_instantiation)
479190075Sobrien	    {
479290075Sobrien	      set_current_access_from_decl (TYPE_NAME (template_type));
479390075Sobrien	      t = start_enum (TYPE_IDENTIFIER (template_type));
479490075Sobrien	    }
479552284Sobrien	  else
479652284Sobrien	    /* We don't want to call start_enum for this type, since
479752284Sobrien	       the values for the enumeration constants may involve
479852284Sobrien	       template parameters.  And, no one should be interested
479952284Sobrien	       in the enumeration constants for such a type.  */
480052284Sobrien	    t = make_node (ENUMERAL_TYPE);
480118334Speter	}
480252284Sobrien      else
480352284Sobrien	{
480490075Sobrien	  t = make_aggr_type (TREE_CODE (template_type));
4805169689Skan	  CLASSTYPE_DECLARED_CLASS (t)
480652284Sobrien	    = CLASSTYPE_DECLARED_CLASS (template_type);
480752284Sobrien	  SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
480852284Sobrien	  TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
480990075Sobrien
481090075Sobrien	  /* A local class.  Make sure the decl gets registered properly.  */
481190075Sobrien	  if (context == current_function_decl)
4812169689Skan	    pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
481352284Sobrien	}
481450397Sobrien
481590075Sobrien      /* If we called start_enum or pushtag above, this information
481690075Sobrien	 will already be set up.  */
481752284Sobrien      if (!TYPE_NAME (t))
481850397Sobrien	{
481952284Sobrien	  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4820169689Skan
482190075Sobrien	  type_decl = create_implicit_typedef (DECL_NAME (template), t);
482252284Sobrien	  DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
482390075Sobrien	  TYPE_STUB_DECL (t) = type_decl;
4824169689Skan	  DECL_SOURCE_LOCATION (type_decl)
4825117395Skan	    = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
482650397Sobrien	}
482750397Sobrien      else
482852284Sobrien	type_decl = TYPE_NAME (t);
482952284Sobrien
4830117395Skan      TREE_PRIVATE (type_decl)
4831117395Skan	= TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4832117395Skan      TREE_PROTECTED (type_decl)
4833117395Skan	= TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4834169689Skan      DECL_IN_SYSTEM_HEADER (type_decl)
4835169689Skan	= DECL_IN_SYSTEM_HEADER (template);
4836169689Skan      if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4837169689Skan	{
4838169689Skan	  DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4839169689Skan	  DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4840169689Skan	}
4841117395Skan
484252284Sobrien      /* Set up the template information.  We have to figure out which
484352284Sobrien	 template is the immediate parent if this is a full
484452284Sobrien	 instantiation.  */
484552284Sobrien      if (parm_depth == 1 || is_partial_instantiation
484652284Sobrien	  || !PRIMARY_TEMPLATE_P (template))
484752284Sobrien	/* This case is easy; there are no member templates involved.  */
484852284Sobrien	found = template;
484952284Sobrien      else
485050397Sobrien	{
485190075Sobrien	  /* This is a full instantiation of a member template.  Look
485290075Sobrien	     for a partial instantiation of which this is an instance.  */
485352284Sobrien
485452284Sobrien	  for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
485552284Sobrien	       found; found = TREE_CHAIN (found))
485652284Sobrien	    {
485752284Sobrien	      int success;
485852284Sobrien	      tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
485952284Sobrien
486052284Sobrien	      /* We only want partial instantiations, here, not
486152284Sobrien		 specializations or full instantiations.  */
486252284Sobrien	      if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
486352284Sobrien		  || !uses_template_parms (TREE_VALUE (found)))
486452284Sobrien		continue;
486552284Sobrien
486652284Sobrien	      /* Temporarily reduce by one the number of levels in the
486752284Sobrien		 ARGLIST and in FOUND so as to avoid comparing the
486852284Sobrien		 last set of arguments.  */
486952284Sobrien	      TREE_VEC_LENGTH (arglist)--;
487052284Sobrien	      TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
487152284Sobrien
487252284Sobrien	      /* See if the arguments match.  If they do, then TMPL is
487352284Sobrien		 the partial instantiation we want.  */
487452284Sobrien	      success = comp_template_args (TREE_PURPOSE (found), arglist);
487552284Sobrien
487652284Sobrien	      /* Restore the argument vectors to their full size.  */
487752284Sobrien	      TREE_VEC_LENGTH (arglist)++;
487852284Sobrien	      TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
487952284Sobrien
488052284Sobrien	      if (success)
488152284Sobrien		{
488252284Sobrien		  found = tmpl;
488352284Sobrien		  break;
488452284Sobrien		}
488552284Sobrien	    }
488652284Sobrien
488752284Sobrien	  if (!found)
488890075Sobrien	    {
488990075Sobrien	      /* There was no partial instantiation. This happens
4890169689Skan		 where C<T> is a member template of A<T> and it's used
4891169689Skan		 in something like
4892169689Skan
4893169689Skan		  template <typename T> struct B { A<T>::C<int> m; };
4894169689Skan		  B<float>;
4895169689Skan
4896169689Skan		 Create the partial instantiation.
4897169689Skan	       */
4898169689Skan	      TREE_VEC_LENGTH (arglist)--;
4899169689Skan	      found = tsubst (template, arglist, complain, NULL_TREE);
4900169689Skan	      TREE_VEC_LENGTH (arglist)++;
4901169689Skan	    }
490250397Sobrien	}
490350397Sobrien
4904169689Skan      SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4905169689Skan      DECL_TEMPLATE_INSTANTIATIONS (template)
4906169689Skan	= tree_cons (arglist, t,
490752284Sobrien		     DECL_TEMPLATE_INSTANTIATIONS (template));
490850397Sobrien
4909169689Skan      if (TREE_CODE (t) == ENUMERAL_TYPE
491052284Sobrien	  && !is_partial_instantiation)
491152284Sobrien	/* Now that the type has been registered on the instantiations
491252284Sobrien	   list, we set up the enumerators.  Because the enumeration
491352284Sobrien	   constants may involve the enumeration type itself, we make
491452284Sobrien	   sure to register the type first, and then create the
491552284Sobrien	   constants.  That way, doing tsubst_expr for the enumeration
491652284Sobrien	   constants won't result in recursive calls here; we'll find
491752284Sobrien	   the instantiation and exit above.  */
491852284Sobrien	tsubst_enum (template_type, t, arglist);
491950397Sobrien
492052284Sobrien      /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
492152284Sobrien	 is set up.  */
492252284Sobrien      if (TREE_CODE (t) != ENUMERAL_TYPE)
492352284Sobrien	DECL_NAME (type_decl) = classtype_mangled_name (t);
4924132718Skan      if (is_partial_instantiation)
492550397Sobrien	/* If the type makes use of template parameters, the
492650397Sobrien	   code that generates debugging information will crash.  */
492750397Sobrien	DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
492852284Sobrien
4929169689Skan      /* Possibly limit visibility based on template args.  */
4930169689Skan      TREE_PUBLIC (type_decl) = 1;
4931169689Skan      determine_visibility (type_decl);
4932169689Skan
4933117395Skan      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
493450397Sobrien    }
4935117395Skan  timevar_pop (TV_NAME_LOOKUP);
493618334Speter}
493718334Speter
4938169689Skanstruct pair_fn_data
493990075Sobrien{
494090075Sobrien  tree_fn_t fn;
494190075Sobrien  void *data;
4942169689Skan  struct pointer_set_t *visited;
494390075Sobrien};
494450397Sobrien
494590075Sobrien/* Called from for_each_template_parm via walk_tree.  */
494690075Sobrien
494790075Sobrienstatic tree
4948169689Skanfor_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
494918334Speter{
495090075Sobrien  tree t = *tp;
495190075Sobrien  struct pair_fn_data *pfd = (struct pair_fn_data *) d;
495290075Sobrien  tree_fn_t fn = pfd->fn;
495390075Sobrien  void *data = pfd->data;
4954117395Skan
495590075Sobrien  if (TYPE_P (t)
4956117395Skan      && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
495790075Sobrien    return error_mark_node;
495850397Sobrien
495918334Speter  switch (TREE_CODE (t))
496018334Speter    {
496118334Speter    case RECORD_TYPE:
496290075Sobrien      if (TYPE_PTRMEMFUNC_P (t))
496390075Sobrien	break;
496452284Sobrien      /* Fall through.  */
496552284Sobrien
496618334Speter    case UNION_TYPE:
496752284Sobrien    case ENUMERAL_TYPE:
496890075Sobrien      if (!TYPE_TEMPLATE_INFO (t))
496990075Sobrien	*walk_subtrees = 0;
497090075Sobrien      else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4971117395Skan				       fn, data, pfd->visited))
497290075Sobrien	return error_mark_node;
497390075Sobrien      break;
497490075Sobrien
497552284Sobrien    case METHOD_TYPE:
497690075Sobrien      /* Since we're not going to walk subtrees, we have to do this
497790075Sobrien	 explicitly here.  */
4978117395Skan      if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4979117395Skan				  pfd->visited))
498090075Sobrien	return error_mark_node;
498152284Sobrien      /* Fall through.  */
498252284Sobrien
498318334Speter    case FUNCTION_TYPE:
498490075Sobrien      /* Check the return type.  */
4985117395Skan      if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
498690075Sobrien	return error_mark_node;
498790075Sobrien
498852284Sobrien      /* Check the parameter types.  Since default arguments are not
498952284Sobrien	 instantiated until they are needed, the TYPE_ARG_TYPES may
499052284Sobrien	 contain expressions that involve template parameters.  But,
499152284Sobrien	 no-one should be looking at them yet.  And, once they're
499252284Sobrien	 instantiated, they don't contain template parameters, so
499352284Sobrien	 there's no point in looking at them then, either.  */
499452284Sobrien      {
499552284Sobrien	tree parm;
499652284Sobrien
499752284Sobrien	for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4998117395Skan	  if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4999117395Skan				      pfd->visited))
500090075Sobrien	    return error_mark_node;
500190075Sobrien
500290075Sobrien	/* Since we've already handled the TYPE_ARG_TYPES, we don't
500390075Sobrien	   want walk_tree walking into them itself.  */
500490075Sobrien	*walk_subtrees = 0;
500552284Sobrien      }
500690075Sobrien      break;
500752284Sobrien
5008132718Skan    case TYPEOF_TYPE:
5009169689Skan      if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5010132718Skan				  pfd->visited))
5011132718Skan	return error_mark_node;
5012132718Skan      break;
5013132718Skan
501418334Speter    case FUNCTION_DECL:
501550397Sobrien    case VAR_DECL:
501650397Sobrien      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5017117395Skan	  && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5018117395Skan				     pfd->visited))
501990075Sobrien	return error_mark_node;
502090075Sobrien      /* Fall through.  */
502190075Sobrien
5022132718Skan    case PARM_DECL:
502390075Sobrien    case CONST_DECL:
5024132718Skan      if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5025132718Skan	  && for_each_template_parm (DECL_INITIAL (t), fn, data,
5026132718Skan				     pfd->visited))
5027132718Skan	return error_mark_node;
5028169689Skan      if (DECL_CONTEXT (t)
5029117395Skan	  && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5030117395Skan				     pfd->visited))
503190075Sobrien	return error_mark_node;
503290075Sobrien      break;
503318334Speter
503490075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
503590075Sobrien      /* Record template parameters such as `T' inside `TT<T>'.  */
5036117395Skan      if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
503790075Sobrien	return error_mark_node;
503890075Sobrien      /* Fall through.  */
503918334Speter
504050397Sobrien    case TEMPLATE_TEMPLATE_PARM:
504118334Speter    case TEMPLATE_TYPE_PARM:
504250397Sobrien    case TEMPLATE_PARM_INDEX:
504390075Sobrien      if (fn && (*fn)(t, data))
504490075Sobrien	return error_mark_node;
504590075Sobrien      else if (!fn)
504690075Sobrien	return error_mark_node;
504790075Sobrien      break;
504818334Speter
504990075Sobrien    case TEMPLATE_DECL:
5050132718Skan      /* A template template parameter is encountered.  */
505190075Sobrien      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5052117395Skan	  && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
505390075Sobrien	return error_mark_node;
505418334Speter
505590075Sobrien      /* Already substituted template template parameter */
505690075Sobrien      *walk_subtrees = 0;
505790075Sobrien      break;
505818334Speter
505950397Sobrien    case TYPENAME_TYPE:
5060169689Skan      if (!fn
5061117395Skan	  || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5062117395Skan				     data, pfd->visited))
506390075Sobrien	return error_mark_node;
506490075Sobrien      break;
506518334Speter
506618334Speter    case CONSTRUCTOR:
506790075Sobrien      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
506890075Sobrien	  && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5069117395Skan				     (TREE_TYPE (t)), fn, data,
5070117395Skan				     pfd->visited))
507190075Sobrien	return error_mark_node;
507290075Sobrien      break;
5073169689Skan
507490075Sobrien    case INDIRECT_REF:
507590075Sobrien    case COMPONENT_REF:
507690075Sobrien      /* If there's no type, then this thing must be some expression
507790075Sobrien	 involving template parameters.  */
507890075Sobrien      if (!fn && !TREE_TYPE (t))
507990075Sobrien	return error_mark_node;
508090075Sobrien      break;
508118334Speter
508250397Sobrien    case MODOP_EXPR:
508350397Sobrien    case CAST_EXPR:
508450397Sobrien    case REINTERPRET_CAST_EXPR:
508550397Sobrien    case CONST_CAST_EXPR:
508650397Sobrien    case STATIC_CAST_EXPR:
508750397Sobrien    case DYNAMIC_CAST_EXPR:
508850397Sobrien    case ARROW_EXPR:
508950397Sobrien    case DOTSTAR_EXPR:
509050397Sobrien    case TYPEID_EXPR:
509190075Sobrien    case PSEUDO_DTOR_EXPR:
509290075Sobrien      if (!fn)
509390075Sobrien	return error_mark_node;
509490075Sobrien      break;
509550397Sobrien
5096117395Skan    case BASELINK:
5097117395Skan      /* If we do not handle this case specially, we end up walking
5098117395Skan	 the BINFO hierarchy, which is circular, and therefore
5099117395Skan	 confuses walk_tree.  */
5100117395Skan      *walk_subtrees = 0;
5101117395Skan      if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5102117395Skan				  pfd->visited))
5103117395Skan	return error_mark_node;
5104117395Skan      break;
5105117395Skan
510618334Speter    default:
510790075Sobrien      break;
510818334Speter    }
510990075Sobrien
511090075Sobrien  /* We didn't find any template parameters we liked.  */
511190075Sobrien  return NULL_TREE;
511218334Speter}
511318334Speter
5114169689Skan/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5115169689Skan   BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
511690075Sobrien   call FN with the parameter and the DATA.
5117117395Skan   If FN returns nonzero, the iteration is terminated, and
511890075Sobrien   for_each_template_parm returns 1.  Otherwise, the iteration
5119117395Skan   continues.  If FN never returns a nonzero value, the value
512090075Sobrien   returned by for_each_template_parm is 0.  If FN is NULL, it is
512190075Sobrien   considered to be the function which always returns 1.  */
512290075Sobrien
512390075Sobrienstatic int
5124169689Skanfor_each_template_parm (tree t, tree_fn_t fn, void* data,
5125169689Skan			struct pointer_set_t *visited)
512690075Sobrien{
512790075Sobrien  struct pair_fn_data pfd;
5128117395Skan  int result;
512990075Sobrien
513090075Sobrien  /* Set up.  */
513190075Sobrien  pfd.fn = fn;
513290075Sobrien  pfd.data = data;
513390075Sobrien
513490075Sobrien  /* Walk the tree.  (Conceptually, we would like to walk without
513590075Sobrien     duplicates, but for_each_template_parm_r recursively calls
513690075Sobrien     for_each_template_parm, so we would need to reorganize a fair
5137117395Skan     bit to use walk_tree_without_duplicates, so we keep our own
5138117395Skan     visited list.)  */
5139117395Skan  if (visited)
5140117395Skan    pfd.visited = visited;
5141117395Skan  else
5142169689Skan    pfd.visited = pointer_set_create ();
5143169689Skan  result = walk_tree (&t,
5144169689Skan		      for_each_template_parm_r,
5145117395Skan		      &pfd,
5146132718Skan		      pfd.visited) != NULL_TREE;
5147117395Skan
5148117395Skan  /* Clean up.  */
5149117395Skan  if (!visited)
5150169689Skan    {
5151169689Skan      pointer_set_destroy (pfd.visited);
5152169689Skan      pfd.visited = 0;
5153169689Skan    }
5154117395Skan
5155117395Skan  return result;
515690075Sobrien}
515790075Sobrien
5158132718Skan/* Returns true if T depends on any template parameter.  */
5159132718Skan
516050397Sobrienint
5161132718Skanuses_template_parms (tree t)
516218334Speter{
5163132718Skan  bool dependent_p;
5164132718Skan  int saved_processing_template_decl;
5165132718Skan
5166132718Skan  saved_processing_template_decl = processing_template_decl;
5167132718Skan  if (!saved_processing_template_decl)
5168132718Skan    processing_template_decl = 1;
5169132718Skan  if (TYPE_P (t))
5170132718Skan    dependent_p = dependent_type_p (t);
5171132718Skan  else if (TREE_CODE (t) == TREE_VEC)
5172132718Skan    dependent_p = any_dependent_template_arguments_p (t);
5173132718Skan  else if (TREE_CODE (t) == TREE_LIST)
5174132718Skan    dependent_p = (uses_template_parms (TREE_VALUE (t))
5175132718Skan		   || uses_template_parms (TREE_CHAIN (t)));
5176161651Skan  else if (TREE_CODE (t) == TYPE_DECL)
5177161651Skan    dependent_p = dependent_type_p (TREE_TYPE (t));
5178169689Skan  else if (DECL_P (t)
5179169689Skan	   || EXPR_P (t)
5180132718Skan	   || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5181132718Skan	   || TREE_CODE (t) == OVERLOAD
5182132718Skan	   || TREE_CODE (t) == BASELINK
5183161651Skan	   || TREE_CODE (t) == IDENTIFIER_NODE
5184169689Skan	   || CONSTANT_CLASS_P (t))
5185132718Skan    dependent_p = (type_dependent_expression_p (t)
5186132718Skan		   || value_dependent_expression_p (t));
5187169689Skan  else
5188169689Skan    {
5189169689Skan      gcc_assert (t == error_mark_node);
5190169689Skan      dependent_p = false;
5191169689Skan    }
5192169689Skan
5193132718Skan  processing_template_decl = saved_processing_template_decl;
5194132718Skan
5195132718Skan  return dependent_p;
519650397Sobrien}
519718334Speter
5198132718Skan/* Returns true if T depends on any template parameter with level LEVEL.  */
5199132718Skan
5200132718Skanint
5201132718Skanuses_template_parms_level (tree t, int level)
5202132718Skan{
5203132718Skan  return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5204132718Skan}
5205132718Skan
520650397Sobrienstatic int tinst_depth;
520750397Sobrienextern int max_tinst_depth;
520850397Sobrien#ifdef GATHER_STATISTICS
520950397Sobrienint depth_reached;
521050397Sobrien#endif
521190075Sobrienstatic int tinst_level_tick;
521290075Sobrienstatic int last_template_error_tick;
521350397Sobrien
521490075Sobrien/* We're starting to instantiate D; record the template instantiation context
521590075Sobrien   for diagnostics and to restore it later.  */
521650397Sobrien
5217169689Skanstatic int
5218132718Skanpush_tinst_level (tree d)
521950397Sobrien{
522090075Sobrien  tree new;
522118334Speter
522218334Speter  if (tinst_depth >= max_tinst_depth)
522318334Speter    {
522450397Sobrien      /* If the instantiation in question still has unbound template parms,
522550397Sobrien	 we don't really care if we can't instantiate it, so just return.
5226169689Skan	 This happens with base instantiation for implicit `typename'.  */
522750397Sobrien      if (uses_template_parms (d))
522850397Sobrien	return 0;
522950397Sobrien
523050397Sobrien      last_template_error_tick = tinst_level_tick;
5231169689Skan      error ("template instantiation depth exceeds maximum of %d (use "
5232169689Skan	     "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
523390075Sobrien	     max_tinst_depth, d);
523450397Sobrien
523590075Sobrien      print_instantiation_context ();
523650397Sobrien
523718334Speter      return 0;
523818334Speter    }
523918334Speter
5240169689Skan  new = make_node (TINST_LEVEL);
5241169689Skan  TINST_DECL (new) = d;
5242169689Skan  TINST_LOCATION (new) = input_location;
5243169689Skan  TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
524490075Sobrien  TREE_CHAIN (new) = current_tinst_level;
524518334Speter  current_tinst_level = new;
524650397Sobrien
524718334Speter  ++tinst_depth;
524850397Sobrien#ifdef GATHER_STATISTICS
524950397Sobrien  if (tinst_depth > depth_reached)
525050397Sobrien    depth_reached = tinst_depth;
525150397Sobrien#endif
525250397Sobrien
525350397Sobrien  ++tinst_level_tick;
525418334Speter  return 1;
525518334Speter}
525618334Speter
525790075Sobrien/* We're done instantiating this template; return to the instantiation
525890075Sobrien   context.  */
525990075Sobrien
5260169689Skanstatic void
5261132718Skanpop_tinst_level (void)
526218334Speter{
526390075Sobrien  tree old = current_tinst_level;
526418334Speter
526552284Sobrien  /* Restore the filename and line number stashed away when we started
526652284Sobrien     this instantiation.  */
5267169689Skan  input_location = TINST_LOCATION (old);
5268169689Skan  in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
526990075Sobrien  current_tinst_level = TREE_CHAIN (old);
527018334Speter  --tinst_depth;
527150397Sobrien  ++tinst_level_tick;
527218334Speter}
527318334Speter
527490075Sobrien/* We're instantiating a deferred template; restore the template
527590075Sobrien   instantiation context in which the instantiation was requested, which
527690075Sobrien   is one step out from LEVEL.  */
527790075Sobrien
527890075Sobrienstatic void
5279132718Skanreopen_tinst_level (tree level)
528090075Sobrien{
528190075Sobrien  tree t;
528290075Sobrien
528390075Sobrien  tinst_depth = 0;
528490075Sobrien  for (t = level; t; t = TREE_CHAIN (t))
528590075Sobrien    ++tinst_depth;
528690075Sobrien
528790075Sobrien  current_tinst_level = level;
528890075Sobrien  pop_tinst_level ();
528990075Sobrien}
529090075Sobrien
5291220150Smm/* Returns the TINST_LEVEL which gives the original instantiation
5292220150Smm   context.  */
5293220150Smm
5294220150Smmtree
5295220150Smmoutermost_tinst_level (void)
5296220150Smm{
5297220150Smm  return tree_last (current_tinst_level);
5298220150Smm}
5299220150Smm
530050397Sobrien/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
530150397Sobrien   vector of template arguments, as for tsubst.
530250397Sobrien
530390075Sobrien   Returns an appropriate tsubst'd friend declaration.  */
530450397Sobrien
530550397Sobrienstatic tree
5306132718Skantsubst_friend_function (tree decl, tree args)
530750397Sobrien{
530850397Sobrien  tree new_friend;
530950397Sobrien
5310169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL
531150397Sobrien      && DECL_TEMPLATE_INSTANTIATION (decl)
531250397Sobrien      && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
531350397Sobrien    /* This was a friend declared with an explicit template
531450397Sobrien       argument list, e.g.:
5315169689Skan
531650397Sobrien       friend void f<>(T);
5317169689Skan
531850397Sobrien       to indicate that f was a template instantiation, not a new
531950397Sobrien       function declaration.  Now, we have to figure out what
532050397Sobrien       instantiation of what template.  */
532150397Sobrien    {
532290075Sobrien      tree template_id, arglist, fns;
532350397Sobrien      tree new_args;
532450397Sobrien      tree tmpl;
532596263Sobrien      tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5326169689Skan
532790075Sobrien      /* Friend functions are looked up in the containing namespace scope.
5328169689Skan	 We must enter that scope, to avoid finding member functions of the
5329169689Skan	 current cless with same name.  */
533090075Sobrien      push_nested_namespace (ns);
533190075Sobrien      fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5332169689Skan			 tf_warning_or_error, NULL_TREE,
5333169689Skan			 /*integral_constant_expression_p=*/false);
533490075Sobrien      pop_nested_namespace (ns);
533590075Sobrien      arglist = tsubst (DECL_TI_ARGS (decl), args,
5336169689Skan			tf_warning_or_error, NULL_TREE);
533790075Sobrien      template_id = lookup_template_function (fns, arglist);
5338169689Skan
5339169689Skan      new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
534052284Sobrien      tmpl = determine_specialization (template_id, new_friend,
5341169689Skan				       &new_args,
5342169689Skan				       /*need_member_template=*/0,
5343169689Skan				       TREE_VEC_LENGTH (args),
5344169689Skan				       tsk_none);
5345169689Skan      return instantiate_template (tmpl, new_args, tf_error);
534650397Sobrien    }
534752284Sobrien
5348169689Skan  new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5349169689Skan
535052284Sobrien  /* The NEW_FRIEND will look like an instantiation, to the
535150397Sobrien     compiler, but is not an instantiation from the point of view of
535250397Sobrien     the language.  For example, we might have had:
5353169689Skan
535450397Sobrien     template <class T> struct S {
535550397Sobrien       template <class U> friend void f(T, U);
535650397Sobrien     };
5357169689Skan
535850397Sobrien     Then, in S<int>, template <class U> void f(int, U) is not an
535950397Sobrien     instantiation of anything.  */
5360132718Skan  if (new_friend == error_mark_node)
5361132718Skan    return error_mark_node;
5362169689Skan
536350397Sobrien  DECL_USE_TEMPLATE (new_friend) = 0;
536450397Sobrien  if (TREE_CODE (decl) == TEMPLATE_DECL)
536590075Sobrien    {
536690075Sobrien      DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
536790075Sobrien      DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
536890075Sobrien	= DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
536990075Sobrien    }
537052284Sobrien
537190075Sobrien  /* The mangled name for the NEW_FRIEND is incorrect.  The function
537290075Sobrien     is not a template instantiation and should not be mangled like
537390075Sobrien     one.  Therefore, we forget the mangling here; we'll recompute it
537490075Sobrien     later if we need it.  */
537552284Sobrien  if (TREE_CODE (new_friend) != TEMPLATE_DECL)
537652284Sobrien    {
537790075Sobrien      SET_DECL_RTL (new_friend, NULL_RTX);
537890075Sobrien      SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
537952284Sobrien    }
5380169689Skan
538150397Sobrien  if (DECL_NAMESPACE_SCOPE_P (new_friend))
538250397Sobrien    {
538352284Sobrien      tree old_decl;
538452284Sobrien      tree new_friend_template_info;
538552284Sobrien      tree new_friend_result_template_info;
538690075Sobrien      tree ns;
538752284Sobrien      int  new_friend_is_defn;
538852284Sobrien
538952284Sobrien      /* We must save some information from NEW_FRIEND before calling
539052284Sobrien	 duplicate decls since that function will free NEW_FRIEND if
539152284Sobrien	 possible.  */
539252284Sobrien      new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5393132718Skan      new_friend_is_defn =
5394169689Skan	    (DECL_INITIAL (DECL_TEMPLATE_RESULT
5395132718Skan			   (template_for_substitution (new_friend)))
5396132718Skan	     != NULL_TREE);
539750397Sobrien      if (TREE_CODE (new_friend) == TEMPLATE_DECL)
539852284Sobrien	{
539952284Sobrien	  /* This declaration is a `primary' template.  */
540052284Sobrien	  DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5401169689Skan
540252284Sobrien	  new_friend_result_template_info
540390075Sobrien	    = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
540452284Sobrien	}
540552284Sobrien      else
5406132718Skan	new_friend_result_template_info = NULL_TREE;
540750397Sobrien
5408169689Skan      /* Make the init_value nonzero so pushdecl knows this is a defn.  */
5409169689Skan      if (new_friend_is_defn)
5410169689Skan	DECL_INITIAL (new_friend) = error_mark_node;
5411169689Skan
541290075Sobrien      /* Inside pushdecl_namespace_level, we will push into the
541390075Sobrien	 current namespace. However, the friend function should go
5414117395Skan	 into the namespace of the template.  */
541590075Sobrien      ns = decl_namespace_context (new_friend);
541690075Sobrien      push_nested_namespace (ns);
5417169689Skan      old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
541890075Sobrien      pop_nested_namespace (ns);
541952284Sobrien
5420169689Skan      if (old_decl == error_mark_node)
5421169689Skan	return error_mark_node;
5422169689Skan
542352284Sobrien      if (old_decl != new_friend)
542452284Sobrien	{
542552284Sobrien	  /* This new friend declaration matched an existing
542652284Sobrien	     declaration.  For example, given:
542752284Sobrien
542852284Sobrien	       template <class T> void f(T);
5429169689Skan	       template <class U> class C {
5430169689Skan		 template <class T> friend void f(T) {}
543152284Sobrien	       };
543252284Sobrien
543352284Sobrien	     the friend declaration actually provides the definition
543452284Sobrien	     of `f', once C has been instantiated for some type.  So,
543552284Sobrien	     old_decl will be the out-of-class template declaration,
543652284Sobrien	     while new_friend is the in-class definition.
543752284Sobrien
543852284Sobrien	     But, if `f' was called before this point, the
543952284Sobrien	     instantiation of `f' will have DECL_TI_ARGS corresponding
544052284Sobrien	     to `T' but not to `U', references to which might appear
544152284Sobrien	     in the definition of `f'.  Previously, the most general
544252284Sobrien	     template for an instantiation of `f' was the out-of-class
544352284Sobrien	     version; now it is the in-class version.  Therefore, we
544452284Sobrien	     run through all specialization of `f', adding to their
544552284Sobrien	     DECL_TI_ARGS appropriately.  In particular, they need a
544652284Sobrien	     new set of outer arguments, corresponding to the
5447169689Skan	     arguments for this class instantiation.
544852284Sobrien
544952284Sobrien	     The same situation can arise with something like this:
545052284Sobrien
545152284Sobrien	       friend void f(int);
5452169689Skan	       template <class T> class C {
5453169689Skan		 friend void f(T) {}
5454169689Skan	       };
545552284Sobrien
545652284Sobrien	     when `C<int>' is instantiated.  Now, `f(int)' is defined
545752284Sobrien	     in the class.  */
545852284Sobrien
545952284Sobrien	  if (!new_friend_is_defn)
546052284Sobrien	    /* On the other hand, if the in-class declaration does
546152284Sobrien	       *not* provide a definition, then we don't want to alter
546252284Sobrien	       existing definitions.  We can just leave everything
546352284Sobrien	       alone.  */
546452284Sobrien	    ;
546552284Sobrien	  else
546652284Sobrien	    {
546752284Sobrien	      /* Overwrite whatever template info was there before, if
546852284Sobrien		 any, with the new template information pertaining to
546952284Sobrien		 the declaration.  */
547052284Sobrien	      DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
547152284Sobrien
547252284Sobrien	      if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5473119256Skan		reregister_specialization (new_friend,
5474119256Skan					   most_general_template (old_decl),
5475119256Skan					   old_decl);
5476169689Skan	      else
547752284Sobrien		{
547852284Sobrien		  tree t;
547952284Sobrien		  tree new_friend_args;
548052284Sobrien
5481169689Skan		  DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
548252284Sobrien		    = new_friend_result_template_info;
5483169689Skan
548452284Sobrien		  new_friend_args = TI_ARGS (new_friend_template_info);
5485169689Skan		  for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
548652284Sobrien		       t != NULL_TREE;
548752284Sobrien		       t = TREE_CHAIN (t))
548852284Sobrien		    {
548952284Sobrien		      tree spec = TREE_VALUE (t);
5490169689Skan
5491169689Skan		      DECL_TI_ARGS (spec)
549252284Sobrien			= add_outermost_template_args (new_friend_args,
549352284Sobrien						       DECL_TI_ARGS (spec));
549452284Sobrien		    }
549552284Sobrien
549652284Sobrien		  /* Now, since specializations are always supposed to
549752284Sobrien		     hang off of the most general template, we must move
549852284Sobrien		     them.  */
549952284Sobrien		  t = most_general_template (old_decl);
550052284Sobrien		  if (t != old_decl)
550152284Sobrien		    {
550252284Sobrien		      DECL_TEMPLATE_SPECIALIZATIONS (t)
550352284Sobrien			= chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
550452284Sobrien				   DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
550552284Sobrien		      DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
550652284Sobrien		    }
550752284Sobrien		}
550852284Sobrien	    }
550952284Sobrien
551052284Sobrien	  /* The information from NEW_FRIEND has been merged into OLD_DECL
551152284Sobrien	     by duplicate_decls.  */
551252284Sobrien	  new_friend = old_decl;
551352284Sobrien	}
551450397Sobrien    }
5515169689Skan  else
551650397Sobrien    {
5517169689Skan      tree context = DECL_CONTEXT (new_friend);
5518169689Skan      bool dependent_p;
5519169689Skan
5520169689Skan      /* In the code
5521169689Skan	   template <class T> class C {
5522169689Skan	     template <class U> friend void C1<U>::f (); // case 1
5523169689Skan	     friend void C2<T>::f ();			 // case 2
5524169689Skan	   };
5525169689Skan	 we only need to make sure CONTEXT is a complete type for
5526169689Skan	 case 2.  To distinguish between the two cases, we note that
5527169689Skan	 CONTEXT of case 1 remains dependent type after tsubst while
5528169689Skan	 this isn't true for case 2.  */
5529169689Skan      ++processing_template_decl;
5530169689Skan      dependent_p = dependent_type_p (context);
5531169689Skan      --processing_template_decl;
5532169689Skan
5533169689Skan      if (!dependent_p
5534169689Skan	  && !complete_type_or_else (context, NULL_TREE))
5535169689Skan	return error_mark_node;
5536169689Skan
5537169689Skan      if (COMPLETE_TYPE_P (context))
5538169689Skan	{
5539169689Skan	  /* Check to see that the declaration is really present, and,
5540169689Skan	     possibly obtain an improved declaration.  */
5541169689Skan	  tree fn = check_classfn (context,
5542169689Skan				   new_friend, NULL_TREE);
5543169689Skan
5544169689Skan	  if (fn)
5545169689Skan	    new_friend = fn;
5546169689Skan	}
554750397Sobrien    }
554850397Sobrien
554950397Sobrien  return new_friend;
555050397Sobrien}
555150397Sobrien
555250397Sobrien/* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
555350397Sobrien   template arguments, as for tsubst.
555450397Sobrien
555590075Sobrien   Returns an appropriate tsubst'd friend type or error_mark_node on
555690075Sobrien   failure.  */
555750397Sobrien
555850397Sobrienstatic tree
5559132718Skantsubst_friend_class (tree friend_tmpl, tree args)
556050397Sobrien{
556150397Sobrien  tree friend_type;
556252284Sobrien  tree tmpl;
556396263Sobrien  tree context;
556450397Sobrien
556596263Sobrien  context = DECL_CONTEXT (friend_tmpl);
556696263Sobrien
556796263Sobrien  if (context)
556896263Sobrien    {
556996263Sobrien      if (TREE_CODE (context) == NAMESPACE_DECL)
557096263Sobrien	push_nested_namespace (context);
557196263Sobrien      else
5572169689Skan	push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
557396263Sobrien    }
557496263Sobrien
5575169689Skan  /* Look for a class template declaration.  We look for hidden names
5576169689Skan     because two friend declarations of the same template are the
5577169689Skan     same.  For example, in:
557896263Sobrien
5579169689Skan       struct A {
5580169689Skan         template <typename> friend class F;
5581169689Skan       };
5582169689Skan       template <typename> struct B {
5583169689Skan         template <typename> friend class F;
5584169689Skan       };
5585169689Skan
5586169689Skan     both F templates are the same.  */
5587169689Skan  tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5588169689Skan			   /*block_p=*/true, 0,
5589169689Skan			   LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
5590169689Skan
559152284Sobrien  /* But, if we don't find one, it might be because we're in a
559252284Sobrien     situation like this:
559350397Sobrien
559452284Sobrien       template <class T>
559552284Sobrien       struct S {
559696263Sobrien	 template <class U>
559752284Sobrien	 friend struct S;
559852284Sobrien       };
559952284Sobrien
560052284Sobrien     Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
560152284Sobrien     for `S<int>', not the TEMPLATE_DECL.  */
560252284Sobrien  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
560350397Sobrien    {
5604169689Skan      tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
560552284Sobrien      tmpl = maybe_get_template_decl_from_type_decl (tmpl);
560652284Sobrien    }
560752284Sobrien
560852284Sobrien  if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
560952284Sobrien    {
561050397Sobrien      /* The friend template has already been declared.  Just
561152284Sobrien	 check to see that the declarations match, and install any new
561252284Sobrien	 default parameters.  We must tsubst the default parameters,
561352284Sobrien	 of course.  We only need the innermost template parameters
561452284Sobrien	 because that is all that redeclare_class_template will look
561552284Sobrien	 at.  */
561696263Sobrien      if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
561796263Sobrien	  > TMPL_ARGS_DEPTH (args))
561896263Sobrien	{
561996263Sobrien	  tree parms;
562096263Sobrien	  parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5621169689Skan					 args, tf_warning_or_error);
562296263Sobrien	  redeclare_class_template (TREE_TYPE (tmpl), parms);
562396263Sobrien	}
562496263Sobrien
562550397Sobrien      friend_type = TREE_TYPE (tmpl);
562650397Sobrien    }
562750397Sobrien  else
562850397Sobrien    {
562950397Sobrien      /* The friend template has not already been declared.  In this
563050397Sobrien	 case, the instantiation of the template class will cause the
563150397Sobrien	 injection of this template into the global scope.  */
5632169689Skan      tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5633169689Skan      if (tmpl == error_mark_node)
5634169689Skan	return error_mark_node;
563550397Sobrien
563650397Sobrien      /* The new TMPL is not an instantiation of anything, so we
5637169689Skan	 forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE for
563850397Sobrien	 the new type because that is supposed to be the corresponding
563950397Sobrien	 template decl, i.e., TMPL.  */
564050397Sobrien      DECL_USE_TEMPLATE (tmpl) = 0;
564150397Sobrien      DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
564250397Sobrien      CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5643132718Skan      CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5644132718Skan	= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
564550397Sobrien
564650397Sobrien      /* Inject this template into the global scope.  */
5647169689Skan      friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
564850397Sobrien    }
564950397Sobrien
5650169689Skan  if (context)
565196263Sobrien    {
565296263Sobrien      if (TREE_CODE (context) == NAMESPACE_DECL)
565396263Sobrien	pop_nested_namespace (context);
565496263Sobrien      else
565596263Sobrien	pop_nested_class ();
565696263Sobrien    }
565796263Sobrien
565850397Sobrien  return friend_type;
565950397Sobrien}
566050397Sobrien
5661102780Skan/* Returns zero if TYPE cannot be completed later due to circularity.
5662102780Skan   Otherwise returns one.  */
5663102780Skan
5664102780Skanstatic int
5665132718Skancan_complete_type_without_circularity (tree type)
5666102780Skan{
5667102780Skan  if (type == NULL_TREE || type == error_mark_node)
5668102780Skan    return 0;
5669102780Skan  else if (COMPLETE_TYPE_P (type))
5670102780Skan    return 1;
5671102780Skan  else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5672102780Skan    return can_complete_type_without_circularity (TREE_TYPE (type));
5673132718Skan  else if (CLASS_TYPE_P (type)
5674132718Skan	   && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5675102780Skan    return 0;
5676102780Skan  else
5677102780Skan    return 1;
5678102780Skan}
5679102780Skan
568018334Spetertree
5681132718Skaninstantiate_class_template (tree type)
568218334Speter{
5683117395Skan  tree template, args, pattern, t, member;
568452284Sobrien  tree typedecl;
5685132718Skan  tree pbinfo;
5686169689Skan  tree base_list;
5687169689Skan
568850397Sobrien  if (type == error_mark_node)
568918334Speter    return error_mark_node;
569018334Speter
5691169689Skan  if (TYPE_BEING_DEFINED (type)
5692132718Skan      || COMPLETE_TYPE_P (type)
5693132718Skan      || dependent_type_p (type))
569450397Sobrien    return type;
569550397Sobrien
569652284Sobrien  /* Figure out which template is being instantiated.  */
569752284Sobrien  template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5698169689Skan  gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
569950397Sobrien
570052284Sobrien  /* Determine what specialization of the original template to
570152284Sobrien     instantiate.  */
5702169689Skan  t = most_specialized_class (type, template);
5703132718Skan  if (t == error_mark_node)
570418334Speter    {
5705132718Skan      TYPE_BEING_DEFINED (type) = 1;
5706132718Skan      return error_mark_node;
570718334Speter    }
5708169689Skan  else if (t)
5709169689Skan    {
5710169689Skan      /* This TYPE is actually an instantiation of a partial
5711169689Skan	 specialization.  We replace the innermost set of ARGS with
5712169689Skan	 the arguments appropriate for substitution.  For example,
5713169689Skan	 given:
571452284Sobrien
5715169689Skan	   template <class T> struct S {};
5716169689Skan	   template <class T> struct S<T*> {};
5717169689Skan
5718169689Skan	 and supposing that we are instantiating S<int*>, ARGS will
5719169689Skan	 presently be {int*} -- but we need {int}.  */
5720169689Skan      pattern = TREE_TYPE (t);
5721169689Skan      args = TREE_PURPOSE (t);
5722169689Skan    }
572350397Sobrien  else
5724169689Skan    {
5725169689Skan      pattern = TREE_TYPE (template);
5726169689Skan      args = CLASSTYPE_TI_ARGS (type);
5727169689Skan    }
572818334Speter
572952284Sobrien  /* If the template we're instantiating is incomplete, then clearly
573052284Sobrien     there's nothing we can do.  */
573190075Sobrien  if (!COMPLETE_TYPE_P (pattern))
573290075Sobrien    return type;
573318334Speter
573452284Sobrien  /* If we've recursively instantiated too many templates, stop.  */
573552284Sobrien  if (! push_tinst_level (type))
573690075Sobrien    return type;
573750397Sobrien
573852284Sobrien  /* Now we're really doing the instantiation.  Mark the type as in
573952284Sobrien     the process of being defined.  */
574050397Sobrien  TYPE_BEING_DEFINED (type) = 1;
574150397Sobrien
5742132718Skan  /* We may be in the middle of deferred access check.  Disable
5743132718Skan     it now.  */
5744132718Skan  push_deferring_access_checks (dk_no_deferred);
574550397Sobrien
5746132718Skan  push_to_top_level ();
5747132718Skan
5748132718Skan  SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
574918334Speter
5750132718Skan  /* Set the input location to the template definition. This is needed
5751132718Skan     if tsubsting causes an error.  */
5752169689Skan  typedecl = TYPE_MAIN_DECL (type);
5753169689Skan  input_location = DECL_SOURCE_LOCATION (typedecl);
5754169689Skan  in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5755132718Skan
575650397Sobrien  TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
575790075Sobrien  TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
575890075Sobrien  TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
575950397Sobrien  TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
576050397Sobrien  TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
576150397Sobrien  TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
576250397Sobrien  TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
576350397Sobrien  TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
576450397Sobrien  TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
576550397Sobrien  TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
576650397Sobrien  TYPE_PACKED (type) = TYPE_PACKED (pattern);
576750397Sobrien  TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
576890075Sobrien  TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
576950397Sobrien  TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
577090075Sobrien  if (ANON_AGGR_TYPE_P (pattern))
577190075Sobrien    SET_ANON_AGGR_TYPE_P (type);
5772169689Skan  if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5773169689Skan    {
5774169689Skan      CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5775169689Skan      CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5776169689Skan    }
577718334Speter
5778132718Skan  pbinfo = TYPE_BINFO (pattern);
5779117395Skan
5780169689Skan  /* We should never instantiate a nested class before its enclosing
5781169689Skan     class; we need to look up the nested class by name before we can
5782169689Skan     instantiate it, and that lookup should instantiate the enclosing
5783169689Skan     class.  */
5784169689Skan  gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5785169689Skan	      || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5786169689Skan	      || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5787132718Skan
5788169689Skan  base_list = NULL_TREE;
5789169689Skan  if (BINFO_N_BASE_BINFOS (pbinfo))
579050397Sobrien    {
5791169689Skan      tree pbase_binfo;
5792132718Skan      tree context = TYPE_CONTEXT (type);
5793169689Skan      tree pushed_scope;
579452284Sobrien      int i;
579550397Sobrien
5796132718Skan      /* We must enter the scope containing the type, as that is where
5797132718Skan	 the accessibility of types named in dependent bases are
5798132718Skan	 looked up from.  */
5799169689Skan      pushed_scope = push_scope (context ? context : global_namespace);
5800169689Skan
580152284Sobrien      /* Substitute into each of the bases to determine the actual
580252284Sobrien	 basetypes.  */
5803169689Skan      for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
580452284Sobrien	{
580552284Sobrien	  tree base;
5806169689Skan	  tree access = BINFO_BASE_ACCESS (pbinfo, i);
580750397Sobrien
580890075Sobrien	  /* Substitute to figure out the base class.  */
5809169689Skan	  base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
581052284Sobrien	  if (base == error_mark_node)
581152284Sobrien	    continue;
5812169689Skan
581352284Sobrien	  base_list = tree_cons (access, base, base_list);
5814169689Skan	  if (BINFO_VIRTUAL_P (pbase_binfo))
5815169689Skan	    TREE_TYPE (base_list) = integer_type_node;
581652284Sobrien	}
581750397Sobrien
581852284Sobrien      /* The list is now in reverse order; correct that.  */
581952284Sobrien      base_list = nreverse (base_list);
582050397Sobrien
5821169689Skan      if (pushed_scope)
5822169689Skan	pop_scope (pushed_scope);
582352284Sobrien    }
5824169689Skan  /* Now call xref_basetypes to set up all the base-class
5825169689Skan     information.  */
5826169689Skan  xref_basetypes (type, base_list);
582750397Sobrien
5828169689Skan
582952284Sobrien  /* Now that our base classes are set up, enter the scope of the
583052284Sobrien     class, so that name lookups into base classes, etc. will work
583190075Sobrien     correctly.  This is precisely analogous to what we do in
583252284Sobrien     begin_class_definition when defining an ordinary non-template
583352284Sobrien     class.  */
5834132718Skan  pushclass (type);
583552284Sobrien
5836117395Skan  /* Now members are processed in the order of declaration.  */
5837132718Skan  for (member = CLASSTYPE_DECL_LIST (pattern);
5838132718Skan       member; member = TREE_CHAIN (member))
583950397Sobrien    {
5840117395Skan      tree t = TREE_VALUE (member);
584150397Sobrien
5842117395Skan      if (TREE_PURPOSE (member))
584318334Speter	{
5844117395Skan	  if (TYPE_P (t))
5845117395Skan	    {
5846132718Skan	      /* Build new CLASSTYPE_NESTED_UTDS.  */
584752284Sobrien
5848117395Skan	      tree newtag;
5849146895Skan	      bool class_template_p;
585018334Speter
5851169689Skan	      class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5852169689Skan				  && TYPE_LANG_SPECIFIC (t)
5853169689Skan				  && CLASSTYPE_IS_TEMPLATE (t));
5854146895Skan	      /* If the member is a class template, then -- even after
5855169689Skan		 substitution -- there may be dependent types in the
5856146895Skan		 template argument list for the class.  We increment
5857146895Skan		 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5858146895Skan		 that function will assume that no types are dependent
5859146895Skan		 when outside of a template.  */
5860146895Skan	      if (class_template_p)
5861146895Skan		++processing_template_decl;
5862169689Skan	      newtag = tsubst (t, args, tf_error, NULL_TREE);
5863146895Skan	      if (class_template_p)
5864146895Skan		--processing_template_decl;
5865132718Skan	      if (newtag == error_mark_node)
5866132718Skan		continue;
5867132718Skan
5868117395Skan	      if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5869117395Skan		{
5870169689Skan		  tree name = TYPE_IDENTIFIER (t);
5871169689Skan
5872146895Skan		  if (class_template_p)
5873117395Skan		    /* Unfortunately, lookup_template_class sets
5874117395Skan		       CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5875132718Skan		       instantiation (i.e., for the type of a member
5876132718Skan		       template class nested within a template class.)
5877132718Skan		       This behavior is required for
5878132718Skan		       maybe_process_partial_specialization to work
5879132718Skan		       correctly, but is not accurate in this case;
5880132718Skan		       the TAG is not an instantiation of anything.
5881132718Skan		       (The corresponding TEMPLATE_DECL is an
5882132718Skan		       instantiation, but the TYPE is not.) */
5883117395Skan		    CLASSTYPE_USE_TEMPLATE (newtag) = 0;
588452284Sobrien
5885117395Skan		  /* Now, we call pushtag to put this NEWTAG into the scope of
5886117395Skan		     TYPE.  We first set up the IDENTIFIER_TYPE_VALUE to avoid
5887117395Skan		     pushtag calling push_template_decl.  We don't have to do
5888117395Skan		     this for enums because it will already have been done in
5889117395Skan		     tsubst_enum.  */
5890117395Skan		  if (name)
5891117395Skan		    SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5892169689Skan		  pushtag (name, newtag, /*tag_scope=*/ts_current);
5893117395Skan		}
5894117395Skan	    }
5895169689Skan	  else if (TREE_CODE (t) == FUNCTION_DECL
5896117395Skan		   || DECL_FUNCTION_TEMPLATE_P (t))
5897117395Skan	    {
5898117395Skan	      /* Build new TYPE_METHODS.  */
5899132718Skan	      tree r;
5900169689Skan
5901132718Skan	      if (TREE_CODE (t) == TEMPLATE_DECL)
5902132718Skan		++processing_template_decl;
5903132718Skan	      r = tsubst (t, args, tf_error, NULL_TREE);
5904132718Skan	      if (TREE_CODE (t) == TEMPLATE_DECL)
5905132718Skan		--processing_template_decl;
5906117395Skan	      set_current_access_from_decl (r);
5907117395Skan	      finish_member_declaration (r);
5908117395Skan	    }
5909117395Skan	  else
5910117395Skan	    {
5911117395Skan	      /* Build new TYPE_FIELDS.  */
591252284Sobrien
5913117395Skan	      if (TREE_CODE (t) != CONST_DECL)
5914117395Skan		{
5915117395Skan		  tree r;
591652284Sobrien
5917132718Skan		  /* The the file and line for this declaration, to
5918132718Skan		     assist in error message reporting.  Since we
5919132718Skan		     called push_tinst_level above, we don't need to
5920132718Skan		     restore these.  */
5921132718Skan		  input_location = DECL_SOURCE_LOCATION (t);
592252284Sobrien
5923132718Skan		  if (TREE_CODE (t) == TEMPLATE_DECL)
5924132718Skan		    ++processing_template_decl;
5925169689Skan		  r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5926132718Skan		  if (TREE_CODE (t) == TEMPLATE_DECL)
5927132718Skan		    --processing_template_decl;
5928117395Skan		  if (TREE_CODE (r) == VAR_DECL)
5929117395Skan		    {
5930169689Skan		      /* In [temp.inst]:
5931102780Skan
5932169689Skan			   [t]he initialization (and any associated
5933169689Skan			   side-effects) of a static data member does
5934169689Skan			   not occur unless the static data member is
5935169689Skan			   itself used in a way that requires the
5936169689Skan			   definition of the static data member to
5937169689Skan			   exist.
593850397Sobrien
5939169689Skan			 Therefore, we do not substitute into the
5940169689Skan			 initialized for the static data member here.  */
5941132718Skan		      finish_static_data_member_decl
5942169689Skan			(r,
5943169689Skan			 /*init=*/NULL_TREE,
5944169689Skan			 /*init_const_expr_p=*/false,
5945169689Skan			 /*asmspec_tree=*/NULL_TREE,
5946169689Skan			 /*flags=*/0);
5947117395Skan		      if (DECL_INITIALIZED_IN_CLASS_P (r))
5948117395Skan			check_static_variable_definition (r, TREE_TYPE (r));
5949117395Skan		    }
5950117395Skan		  else if (TREE_CODE (r) == FIELD_DECL)
5951117395Skan		    {
5952117395Skan		      /* Determine whether R has a valid type and can be
5953117395Skan			 completed later.  If R is invalid, then it is
5954117395Skan			 replaced by error_mark_node so that it will not be
5955117395Skan			 added to TYPE_FIELDS.  */
5956117395Skan		      tree rtype = TREE_TYPE (r);
5957117395Skan		      if (can_complete_type_without_circularity (rtype))
5958117395Skan			complete_type (rtype);
595950397Sobrien
5960117395Skan		      if (!COMPLETE_TYPE_P (rtype))
5961117395Skan			{
5962117395Skan			  cxx_incomplete_type_error (r, rtype);
5963169689Skan			  r = error_mark_node;
5964117395Skan			}
5965117395Skan		    }
596650397Sobrien
5967117395Skan		  /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5968117395Skan		     such a thing will already have been added to the field
5969117395Skan		     list by tsubst_enum in finish_member_declaration in the
5970132718Skan		     CLASSTYPE_NESTED_UTDS case above.  */
5971117395Skan		  if (!(TREE_CODE (r) == TYPE_DECL
5972117395Skan			&& TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5973117395Skan			&& DECL_ARTIFICIAL (r)))
5974117395Skan		    {
5975117395Skan		      set_current_access_from_decl (r);
5976117395Skan		      finish_member_declaration (r);
5977117395Skan		    }
5978169689Skan		}
5979117395Skan	    }
5980117395Skan	}
5981117395Skan      else
598290075Sobrien	{
5983117395Skan	  if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5984117395Skan	    {
5985117395Skan	      /* Build new CLASSTYPE_FRIEND_CLASSES.  */
598650397Sobrien
5987117395Skan	      tree friend_type = t;
5988169689Skan	      bool adjust_processing_template_decl = false;
598952284Sobrien
5990117395Skan	      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5991117395Skan		{
5992169689Skan		  /* template <class T> friend class C;  */
5993169689Skan		  friend_type = tsubst_friend_class (friend_type, args);
5994169689Skan		  adjust_processing_template_decl = true;
5995169689Skan		}
5996169689Skan	      else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5997169689Skan		{
5998169689Skan		  /* template <class T> friend class C::D;  */
5999169689Skan		  friend_type = tsubst (friend_type, args,
6000169689Skan					tf_warning_or_error, NULL_TREE);
6001169689Skan		  if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6002169689Skan		    friend_type = TREE_TYPE (friend_type);
6003169689Skan		  adjust_processing_template_decl = true;
6004169689Skan		}
6005169689Skan	      else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6006169689Skan		{
6007169689Skan		  /* This could be either
6008169689Skan
6009169689Skan		       friend class T::C;
6010169689Skan
6011169689Skan		     when dependent_type_p is false or
6012169689Skan
6013169689Skan		       template <class U> friend class T::C;
6014169689Skan
6015169689Skan		     otherwise.  */
6016169689Skan		  friend_type = tsubst (friend_type, args,
6017169689Skan					tf_warning_or_error, NULL_TREE);
6018169689Skan		  /* Bump processing_template_decl for correct
6019169689Skan		     dependent_type_p calculation.  */
6020169689Skan		  ++processing_template_decl;
6021169689Skan		  if (dependent_type_p (friend_type))
6022169689Skan		    adjust_processing_template_decl = true;
6023169689Skan		  --processing_template_decl;
6024169689Skan		}
6025169689Skan	      else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6026169689Skan		       && hidden_name_p (TYPE_NAME (friend_type)))
6027169689Skan		{
6028169689Skan		  /* friend class C;
6029169689Skan
6030169689Skan		     where C hasn't been declared yet.  Let's lookup name
6031169689Skan		     from namespace scope directly, bypassing any name that
6032169689Skan		     come from dependent base class.  */
6033117395Skan		  tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
603450397Sobrien
6035117395Skan		  /* The call to xref_tag_from_type does injection for friend
6036117395Skan		     classes.  */
6037117395Skan		  push_nested_namespace (ns);
6038169689Skan		  friend_type =
6039169689Skan		    xref_tag_from_type (friend_type, NULL_TREE,
6040169689Skan					/*tag_scope=*/ts_current);
6041117395Skan		  pop_nested_namespace (ns);
6042117395Skan		}
6043169689Skan	      else if (uses_template_parms (friend_type))
6044169689Skan		/* friend class C<T>;  */
6045169689Skan		friend_type = tsubst (friend_type, args,
6046169689Skan				      tf_warning_or_error, NULL_TREE);
6047169689Skan	      /* Otherwise it's
604850397Sobrien
6049169689Skan		   friend class C;
6050169689Skan
6051169689Skan		 where C is already declared or
6052169689Skan
6053169689Skan		   friend class C<int>;
6054169689Skan
6055169689Skan		 We don't have to do anything in these cases.  */
6056169689Skan
6057169689Skan	      if (adjust_processing_template_decl)
6058117395Skan		/* Trick make_friend_class into realizing that the friend
6059117395Skan		   we're adding is a template, not an ordinary class.  It's
6060117395Skan		   important that we use make_friend_class since it will
6061117395Skan		   perform some error-checking and output cross-reference
6062117395Skan		   information.  */
6063117395Skan		++processing_template_decl;
6064117395Skan
6065169689Skan	      if (friend_type != error_mark_node)
6066169689Skan		make_friend_class (type, friend_type, /*complain=*/false);
6067117395Skan
6068169689Skan	      if (adjust_processing_template_decl)
6069117395Skan		--processing_template_decl;
6070117395Skan	    }
6071117395Skan	  else
6072132718Skan	    {
6073132718Skan	      /* Build new DECL_FRIENDLIST.  */
6074132718Skan	      tree r;
6075132718Skan
6076169689Skan	      /* The the file and line for this declaration, to
6077169689Skan		 assist in error message reporting.  Since we
6078169689Skan		 called push_tinst_level above, we don't need to
6079169689Skan		 restore these.  */
6080169689Skan	      input_location = DECL_SOURCE_LOCATION (t);
6081169689Skan
6082132718Skan	      if (TREE_CODE (t) == TEMPLATE_DECL)
6083169689Skan		{
6084169689Skan		  ++processing_template_decl;
6085169689Skan		  push_deferring_access_checks (dk_no_check);
6086169689Skan		}
6087169689Skan
6088132718Skan	      r = tsubst_friend_function (t, args);
6089169689Skan	      add_friend (type, r, /*complain=*/false);
6090132718Skan	      if (TREE_CODE (t) == TEMPLATE_DECL)
6091169689Skan		{
6092169689Skan		  pop_deferring_access_checks ();
6093169689Skan		  --processing_template_decl;
6094169689Skan		}
6095132718Skan	    }
6096117395Skan	}
609750397Sobrien    }
609850397Sobrien
609952284Sobrien  /* Set the file and line number information to whatever is given for
610052284Sobrien     the class itself.  This puts error messages involving generated
610152284Sobrien     implicit functions at a predictable point, and the same point
610252284Sobrien     that would be used for non-template classes.  */
6103132718Skan  input_location = DECL_SOURCE_LOCATION (typedecl);
6104169689Skan
610552284Sobrien  unreverse_member_declarations (type);
610690075Sobrien  finish_struct_1 (type);
610752284Sobrien  TYPE_BEING_DEFINED (type) = 0;
610850397Sobrien
610990075Sobrien  /* Now that the class is complete, instantiate default arguments for
611090075Sobrien     any member functions.  We don't do this earlier because the
611190075Sobrien     default arguments may reference members of the class.  */
611290075Sobrien  if (!PRIMARY_TEMPLATE_P (template))
611390075Sobrien    for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6114169689Skan      if (TREE_CODE (t) == FUNCTION_DECL
611590075Sobrien	  /* Implicitly generated member functions will not have template
611690075Sobrien	     information; they are not instantiations, but instead are
611790075Sobrien	     created "fresh" for each instantiation.  */
611890075Sobrien	  && DECL_TEMPLATE_INFO (t))
611990075Sobrien	tsubst_default_arguments (t);
612090075Sobrien
612152284Sobrien  popclass ();
612250397Sobrien  pop_from_top_level ();
6123132718Skan  pop_deferring_access_checks ();
612450397Sobrien  pop_tinst_level ();
612550397Sobrien
6126169689Skan  /* The vtable for a template class can be emitted in any translation
6127169689Skan     unit in which the class is instantiated.  When there is no key
6128169689Skan     method, however, finish_struct_1 will already have added TYPE to
6129169689Skan     the keyed_classes list.  */
6130169689Skan  if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6131117395Skan    keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6132117395Skan
613350397Sobrien  return type;
613418334Speter}
613518334Speter
613618334Speterstatic tree
6137132718Skantsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
613818334Speter{
6139132718Skan  tree r;
6140169689Skan
6141132718Skan  if (!t)
6142132718Skan    r = t;
6143132718Skan  else if (TYPE_P (t))
6144132718Skan    r = tsubst (t, args, complain, in_decl);
6145132718Skan  else
614650397Sobrien    {
6147169689Skan      r = tsubst_expr (t, args, complain, in_decl,
6148169689Skan		       /*integral_constant_expression_p=*/true);
6149169689Skan      r = fold_non_dependent_expr (r);
615050397Sobrien    }
6151132718Skan  return r;
615250397Sobrien}
615350397Sobrien
6154132718Skan/* Substitute ARGS into the vector or list of template arguments T.  */
615550397Sobrien
615652284Sobrienstatic tree
6157132718Skantsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
615850397Sobrien{
6159132718Skan  int len = TREE_VEC_LENGTH (t);
6160132718Skan  int need_new = 0, i;
6161169689Skan  tree *elts = (tree *) alloca (len * sizeof (tree));
6162169689Skan
616350397Sobrien  for (i = 0; i < len; i++)
616450397Sobrien    {
6165132718Skan      tree orig_arg = TREE_VEC_ELT (t, i);
6166132718Skan      tree new_arg;
6167132718Skan
6168132718Skan      if (TREE_CODE (orig_arg) == TREE_VEC)
6169132718Skan	new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
617050397Sobrien      else
6171132718Skan	new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
6172169689Skan
6173132718Skan      if (new_arg == error_mark_node)
617490075Sobrien	return error_mark_node;
617590075Sobrien
6176132718Skan      elts[i] = new_arg;
6177132718Skan      if (new_arg != orig_arg)
617850397Sobrien	need_new = 1;
617950397Sobrien    }
6180169689Skan
618150397Sobrien  if (!need_new)
618250397Sobrien    return t;
6183132718Skan
618490075Sobrien  t = make_tree_vec (len);
618550397Sobrien  for (i = 0; i < len; i++)
618650397Sobrien    TREE_VEC_ELT (t, i) = elts[i];
6187169689Skan
618818334Speter  return t;
618918334Speter}
619018334Speter
619152284Sobrien/* Return the result of substituting ARGS into the template parameters
619252284Sobrien   given by PARMS.  If there are m levels of ARGS and m + n levels of
619352284Sobrien   PARMS, then the result will contain n levels of PARMS.  For
619452284Sobrien   example, if PARMS is `template <class T> template <class U>
619552284Sobrien   template <T*, U, class V>' and ARGS is {{int}, {double}} then the
619652284Sobrien   result will be `template <int*, double, class V>'.  */
619750397Sobrien
619852284Sobrienstatic tree
6199132718Skantsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
620018334Speter{
620190075Sobrien  tree r = NULL_TREE;
620290075Sobrien  tree* new_parms;
620318334Speter
6204161651Skan  /* When substituting into a template, we must set
6205161651Skan     PROCESSING_TEMPLATE_DECL as the template parameters may be
6206161651Skan     dependent if they are based on one-another, and the dependency
6207161651Skan     predicates are short-circuit outside of templates.  */
6208161651Skan  ++processing_template_decl;
6209161651Skan
621052284Sobrien  for (new_parms = &r;
621152284Sobrien       TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
621252284Sobrien       new_parms = &(TREE_CHAIN (*new_parms)),
621352284Sobrien	 parms = TREE_CHAIN (parms))
621452284Sobrien    {
6215169689Skan      tree new_vec =
621652284Sobrien	make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
621752284Sobrien      int i;
6218169689Skan
621952284Sobrien      for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
622052284Sobrien	{
6221169689Skan          tree tuple;
6222169689Skan          tree default_value;
6223169689Skan          tree parm_decl;
622490075Sobrien
6225169689Skan          if (parms == error_mark_node)
6226169689Skan            continue;
6227169689Skan
6228169689Skan          tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6229169689Skan
6230169689Skan          if (tuple == error_mark_node)
6231169689Skan            continue;
6232169689Skan
6233169689Skan          default_value = TREE_PURPOSE (tuple);
6234169689Skan          parm_decl = TREE_VALUE (tuple);
6235169689Skan
623690075Sobrien	  parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6237169689Skan	  if (TREE_CODE (parm_decl) == PARM_DECL
6238169689Skan	      && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6239169689Skan	    parm_decl = error_mark_node;
6240132718Skan	  default_value = tsubst_template_arg (default_value, args,
6241132718Skan					       complain, NULL_TREE);
6242169689Skan
6243132718Skan	  tuple = build_tree_list (default_value, parm_decl);
624490075Sobrien	  TREE_VEC_ELT (new_vec, i) = tuple;
624552284Sobrien	}
6246169689Skan
6247169689Skan      *new_parms =
6248169689Skan	tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
624990075Sobrien			     - TMPL_ARGS_DEPTH (args)),
625052284Sobrien		   new_vec, NULL_TREE);
625152284Sobrien    }
625218334Speter
6253161651Skan  --processing_template_decl;
6254161651Skan
625552284Sobrien  return r;
625652284Sobrien}
625718334Speter
625852284Sobrien/* Substitute the ARGS into the indicated aggregate (or enumeration)
625952284Sobrien   type T.  If T is not an aggregate or enumeration type, it is
626052284Sobrien   handled as if by tsubst.  IN_DECL is as for tsubst.  If
6261117395Skan   ENTERING_SCOPE is nonzero, T is the context for a template which
626290075Sobrien   we are presently tsubst'ing.  Return the substituted value.  */
626350397Sobrien
626452284Sobrienstatic tree
6265169689Skantsubst_aggr_type (tree t,
6266169689Skan		  tree args,
6267169689Skan		  tsubst_flags_t complain,
6268169689Skan		  tree in_decl,
6269169689Skan		  int entering_scope)
627052284Sobrien{
627152284Sobrien  if (t == NULL_TREE)
627252284Sobrien    return NULL_TREE;
627352284Sobrien
627418334Speter  switch (TREE_CODE (t))
627518334Speter    {
627618334Speter    case RECORD_TYPE:
627718334Speter      if (TYPE_PTRMEMFUNC_P (t))
627890075Sobrien	return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
627950397Sobrien
6280132718Skan      /* Else fall through.  */
628152284Sobrien    case ENUMERAL_TYPE:
628250397Sobrien    case UNION_TYPE:
628352284Sobrien      if (TYPE_TEMPLATE_INFO (t))
628450397Sobrien	{
628552284Sobrien	  tree argvec;
628650397Sobrien	  tree context;
628750397Sobrien	  tree r;
6288169689Skan	  bool saved_skip_evaluation;
628950397Sobrien
6290169689Skan	  /* In "sizeof(X<I>)" we need to evaluate "I".  */
6291169689Skan	  saved_skip_evaluation = skip_evaluation;
6292169689Skan	  skip_evaluation = false;
6293169689Skan
629452284Sobrien	  /* First, determine the context for the type we are looking
629552284Sobrien	     up.  */
6296132718Skan	  context = TYPE_CONTEXT (t);
6297132718Skan	  if (context)
6298132718Skan	    context = tsubst_aggr_type (context, args, complain,
629952284Sobrien					in_decl, /*entering_scope=*/1);
630050397Sobrien
630152284Sobrien	  /* Then, figure out what arguments are appropriate for the
630252284Sobrien	     type we are trying to find.  For example, given:
630350397Sobrien
630452284Sobrien	       template <class T> struct S;
630552284Sobrien	       template <class T, class U> void f(T, U) { S<U> su; }
630650397Sobrien
630752284Sobrien	     and supposing that we are instantiating f<int, double>,
630852284Sobrien	     then our ARGS will be {int, double}, but, when looking up
630952284Sobrien	     S we only want {double}.  */
6310132718Skan	  argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6311132718Skan					 complain, in_decl);
631290075Sobrien	  if (argvec == error_mark_node)
6313169689Skan	    r = error_mark_node;
6314169689Skan	  else
6315169689Skan	    {
6316169689Skan	      r = lookup_template_class (t, argvec, in_decl, context,
6317169689Skan					 entering_scope, complain);
6318169689Skan	      r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6319169689Skan	    }
632018334Speter
6321169689Skan	  skip_evaluation = saved_skip_evaluation;
632250397Sobrien
6323169689Skan	  return r;
632452284Sobrien	}
6325169689Skan      else
632652284Sobrien	/* This is not a template type, so there's nothing to do.  */
632718334Speter	return t;
632818334Speter
632952284Sobrien    default:
633052284Sobrien      return tsubst (t, args, complain, in_decl);
633152284Sobrien    }
633252284Sobrien}
633318334Speter
633490075Sobrien/* Substitute into the default argument ARG (a default argument for
633590075Sobrien   FN), which has the indicated TYPE.  */
633690075Sobrien
633790075Sobrientree
6338132718Skantsubst_default_argument (tree fn, tree type, tree arg)
633990075Sobrien{
6340146895Skan  tree saved_class_ptr = NULL_TREE;
6341146895Skan  tree saved_class_ref = NULL_TREE;
6342146895Skan
634390075Sobrien  /* This default argument came from a template.  Instantiate the
634490075Sobrien     default argument here, not in tsubst.  In the case of
6345169689Skan     something like:
6346169689Skan
634790075Sobrien       template <class T>
634890075Sobrien       struct S {
634990075Sobrien	 static T t();
635090075Sobrien	 void f(T = t());
635190075Sobrien       };
6352169689Skan
635390075Sobrien     we must be careful to do name lookup in the scope of S<T>,
6354132718Skan     rather than in the current class.  */
6355117395Skan  push_access_scope (fn);
6356146895Skan  /* The "this" pointer is not valid in a default argument.  */
6357146895Skan  if (cfun)
6358146895Skan    {
6359146895Skan      saved_class_ptr = current_class_ptr;
6360146895Skan      cp_function_chain->x_current_class_ptr = NULL_TREE;
6361146895Skan      saved_class_ref = current_class_ref;
6362146895Skan      cp_function_chain->x_current_class_ref = NULL_TREE;
6363146895Skan    }
6364117395Skan
6365132718Skan  push_deferring_access_checks(dk_no_deferred);
6366169689Skan  /* The default argument expression may cause implicitly defined
6367169689Skan     member functions to be synthesized, which will result in garbage
6368169689Skan     collection.  We must treat this situation as if we were within
6369169689Skan     the body of function so as to avoid collecting live data on the
6370169689Skan     stack.  */
6371169689Skan  ++function_depth;
637296263Sobrien  arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6373169689Skan		     tf_warning_or_error, NULL_TREE,
6374169689Skan		     /*integral_constant_expression_p=*/false);
6375169689Skan  --function_depth;
6376132718Skan  pop_deferring_access_checks();
6377132718Skan
6378146895Skan  /* Restore the "this" pointer.  */
6379146895Skan  if (cfun)
6380146895Skan    {
6381146895Skan      cp_function_chain->x_current_class_ptr = saved_class_ptr;
6382146895Skan      cp_function_chain->x_current_class_ref = saved_class_ref;
6383146895Skan    }
6384146895Skan
6385117395Skan  pop_access_scope (fn);
638690075Sobrien
638790075Sobrien  /* Make sure the default argument is reasonable.  */
638890075Sobrien  arg = check_default_argument (type, arg);
638990075Sobrien
639090075Sobrien  return arg;
639190075Sobrien}
639290075Sobrien
639390075Sobrien/* Substitute into all the default arguments for FN.  */
639490075Sobrien
639590075Sobrienstatic void
6396132718Skantsubst_default_arguments (tree fn)
639790075Sobrien{
639890075Sobrien  tree arg;
639990075Sobrien  tree tmpl_args;
640090075Sobrien
640190075Sobrien  tmpl_args = DECL_TI_ARGS (fn);
640290075Sobrien
640390075Sobrien  /* If this function is not yet instantiated, we certainly don't need
640490075Sobrien     its default arguments.  */
640590075Sobrien  if (uses_template_parms (tmpl_args))
640690075Sobrien    return;
640790075Sobrien
6408169689Skan  for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6409169689Skan       arg;
641090075Sobrien       arg = TREE_CHAIN (arg))
641190075Sobrien    if (TREE_PURPOSE (arg))
6412169689Skan      TREE_PURPOSE (arg) = tsubst_default_argument (fn,
641390075Sobrien						    TREE_VALUE (arg),
641490075Sobrien						    TREE_PURPOSE (arg));
641590075Sobrien}
641690075Sobrien
6417169689Skan/* Substitute the ARGS into the T, which is a _DECL.  Return the
6418169689Skan   result of the substitution.  Issue error and warning messages under
6419169689Skan   control of COMPLAIN.  */
642050397Sobrien
642152284Sobrienstatic tree
6422169689Skantsubst_decl (tree t, tree args, tsubst_flags_t complain)
642352284Sobrien{
6424132718Skan  location_t saved_loc;
642552284Sobrien  tree r = NULL_TREE;
642690075Sobrien  tree in_decl = t;
642718334Speter
642852284Sobrien  /* Set the filename and linenumber to improve error-reporting.  */
6429132718Skan  saved_loc = input_location;
6430132718Skan  input_location = DECL_SOURCE_LOCATION (t);
643118334Speter
643252284Sobrien  switch (TREE_CODE (t))
643352284Sobrien    {
643450397Sobrien    case TEMPLATE_DECL:
643550397Sobrien      {
6436169689Skan	/* We can get here when processing a member function template,
6437169689Skan	   member class template, and template template parameter of
6438169689Skan	   a template class.  */
643950397Sobrien	tree decl = DECL_TEMPLATE_RESULT (t);
644050397Sobrien	tree spec;
6441169689Skan	tree tmpl_args;
6442169689Skan	tree full_args;
644318334Speter
6444169689Skan	if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
644550397Sobrien	  {
6446169689Skan	    /* Template template parameter is treated here.  */
6447169689Skan	    tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6448169689Skan	    if (new_type == error_mark_node)
6449169689Skan	      return error_mark_node;
645052284Sobrien
6451169689Skan	    r = copy_decl (t);
6452169689Skan	    TREE_CHAIN (r) = NULL_TREE;
6453169689Skan	    TREE_TYPE (r) = new_type;
6454169689Skan	    DECL_TEMPLATE_RESULT (r)
6455169689Skan	      = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6456169689Skan	    DECL_TEMPLATE_PARMS (r)
6457169689Skan	      = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6458169689Skan				       complain);
6459169689Skan	    TYPE_NAME (new_type) = r;
6460169689Skan	    break;
6461169689Skan	  }
646252284Sobrien
6463169689Skan	/* We might already have an instance of this template.
6464169689Skan	   The ARGS are for the surrounding class type, so the
6465169689Skan	   full args contain the tsubst'd args for the context,
6466169689Skan	   plus the innermost args from the template decl.  */
6467169689Skan	tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6468169689Skan	  ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6469169689Skan	  : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6470169689Skan	/* Because this is a template, the arguments will still be
6471169689Skan	   dependent, even after substitution.  If
6472169689Skan	   PROCESSING_TEMPLATE_DECL is not set, the dependency
6473169689Skan	   predicates will short-circuit.  */
6474169689Skan	++processing_template_decl;
6475169689Skan	full_args = tsubst_template_args (tmpl_args, args,
6476169689Skan					  complain, in_decl);
6477169689Skan	--processing_template_decl;
6478169689Skan	if (full_args == error_mark_node)
6479169689Skan	  return error_mark_node;
6480169689Skan
6481169689Skan	/* tsubst_template_args doesn't copy the vector if
6482169689Skan	   nothing changed.  But, *something* should have
6483169689Skan	   changed.  */
6484169689Skan	gcc_assert (full_args != tmpl_args);
6485169689Skan
6486169689Skan	spec = retrieve_specialization (t, full_args,
6487169689Skan					/*class_specializations_p=*/true);
6488169689Skan	if (spec != NULL_TREE)
6489169689Skan	  {
6490169689Skan	    r = spec;
6491169689Skan	    break;
649250397Sobrien	  }
649318334Speter
649450397Sobrien	/* Make a new template decl.  It will be similar to the
6495169689Skan	   original, but will record the current template arguments.
649650397Sobrien	   We also create a new function declaration, which is just
649750397Sobrien	   like the old one, but points to this new template, rather
649850397Sobrien	   than the old one.  */
649990075Sobrien	r = copy_decl (t);
6500169689Skan	gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
650152284Sobrien	TREE_CHAIN (r) = NULL_TREE;
650218334Speter
650352284Sobrien	DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
650418334Speter
650550397Sobrien	if (TREE_CODE (decl) == TYPE_DECL)
650650397Sobrien	  {
6507161651Skan	    tree new_type;
6508161651Skan	    ++processing_template_decl;
6509161651Skan	    new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6510169689Skan	    --processing_template_decl;
6511132718Skan	    if (new_type == error_mark_node)
6512132718Skan	      return error_mark_node;
6513132718Skan
651452284Sobrien	    TREE_TYPE (r) = new_type;
651552284Sobrien	    CLASSTYPE_TI_TEMPLATE (new_type) = r;
651690075Sobrien	    DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
651752284Sobrien	    DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6518161651Skan	    DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
651950397Sobrien	  }
652050397Sobrien	else
652150397Sobrien	  {
6522161651Skan	    tree new_decl;
6523161651Skan	    ++processing_template_decl;
6524161651Skan	    new_decl = tsubst (decl, args, complain, in_decl);
6525161651Skan	    --processing_template_decl;
6526132718Skan	    if (new_decl == error_mark_node)
6527132718Skan	      return error_mark_node;
652890075Sobrien
652990075Sobrien	    DECL_TEMPLATE_RESULT (r) = new_decl;
653052284Sobrien	    DECL_TI_TEMPLATE (new_decl) = r;
653152284Sobrien	    TREE_TYPE (r) = TREE_TYPE (new_decl);
653252284Sobrien	    DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6533169689Skan	    DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
653450397Sobrien	  }
653518334Speter
653652284Sobrien	SET_DECL_IMPLICIT_INSTANTIATION (r);
653752284Sobrien	DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
653852284Sobrien	DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
653950397Sobrien
654050397Sobrien	/* The template parameters for this new template are all the
654150397Sobrien	   template parameters for the old template, except the
6542117395Skan	   outermost level of parameters.  */
6543169689Skan	DECL_TEMPLATE_PARMS (r)
654452284Sobrien	  = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
654596263Sobrien				   complain);
654650397Sobrien
654750397Sobrien	if (PRIMARY_TEMPLATE_P (t))
654852284Sobrien	  DECL_PRIMARY_TEMPLATE (r) = r;
654950397Sobrien
6550132718Skan	if (TREE_CODE (decl) != TYPE_DECL)
6551132718Skan	  /* Record this non-type partial instantiation.  */
6552169689Skan	  register_specialization (r, t,
6553169689Skan				   DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6554169689Skan				   false);
655550397Sobrien      }
655652284Sobrien      break;
655750397Sobrien
655850397Sobrien    case FUNCTION_DECL:
655950397Sobrien      {
656050397Sobrien	tree ctx;
656152284Sobrien	tree argvec = NULL_TREE;
656252284Sobrien	tree *friends;
656352284Sobrien	tree gen_tmpl;
6564169689Skan	tree type;
656550397Sobrien	int member;
656652284Sobrien	int args_depth;
656752284Sobrien	int parms_depth;
656850397Sobrien
656952284Sobrien	/* Nobody should be tsubst'ing into non-template functions.  */
6570169689Skan	gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
657152284Sobrien
657252284Sobrien	if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
657352284Sobrien	  {
657452284Sobrien	    tree spec;
6575132718Skan	    bool dependent_p;
657652284Sobrien
6577132718Skan	    /* If T is not dependent, just return it.  We have to
6578132718Skan	       increment PROCESSING_TEMPLATE_DECL because
6579132718Skan	       value_dependent_expression_p assumes that nothing is
6580132718Skan	       dependent when PROCESSING_TEMPLATE_DECL is zero.  */
6581132718Skan	    ++processing_template_decl;
6582132718Skan	    dependent_p = value_dependent_expression_p (t);
6583132718Skan	    --processing_template_decl;
6584132718Skan	    if (!dependent_p)
6585117395Skan	      return t;
6586117395Skan
658752284Sobrien	    /* Calculate the most general template of which R is a
658852284Sobrien	       specialization, and the complete set of arguments used to
658952284Sobrien	       specialize R.  */
659052284Sobrien	    gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6591169689Skan	    argvec = tsubst_template_args (DECL_TI_ARGS
6592132718Skan					   (DECL_TEMPLATE_RESULT (gen_tmpl)),
6593169689Skan					   args, complain, in_decl);
659452284Sobrien
659552284Sobrien	    /* Check to see if we already have this specialization.  */
6596169689Skan	    spec = retrieve_specialization (gen_tmpl, argvec,
6597169689Skan					    /*class_specializations_p=*/false);
659852284Sobrien
659952284Sobrien	    if (spec)
660052284Sobrien	      {
660152284Sobrien		r = spec;
660252284Sobrien		break;
660352284Sobrien	      }
660452284Sobrien
660590075Sobrien	    /* We can see more levels of arguments than parameters if
660690075Sobrien	       there was a specialization of a member template, like
660790075Sobrien	       this:
660852284Sobrien
6609169689Skan		 template <class T> struct S { template <class U> void f(); }
6610169689Skan		 template <> template <class U> void S<int>::f(U);
661152284Sobrien
661290075Sobrien	       Here, we'll be substituting into the specialization,
661390075Sobrien	       because that's where we can find the code we actually
661490075Sobrien	       want to generate, but we'll have enough arguments for
6615169689Skan	       the most general template.
661690075Sobrien
661790075Sobrien	       We also deal with the peculiar case:
661890075Sobrien
6619169689Skan		 template <class T> struct S {
662052284Sobrien		   template <class U> friend void f();
662152284Sobrien		 };
662290075Sobrien		 template <class U> void f() {}
662352284Sobrien		 template S<int>;
662452284Sobrien		 template void f<double>();
662552284Sobrien
662652284Sobrien	       Here, the ARGS for the instantiation of will be {int,
662752284Sobrien	       double}.  But, we only need as many ARGS as there are
662852284Sobrien	       levels of template parameters in CODE_PATTERN.  We are
662952284Sobrien	       careful not to get fooled into reducing the ARGS in
663052284Sobrien	       situations like:
663152284Sobrien
663252284Sobrien		 template <class T> struct S { template <class U> void f(U); }
663352284Sobrien		 template <class T> template <> void S<T>::f(int) {}
663452284Sobrien
663552284Sobrien	       which we can spot because the pattern will be a
663652284Sobrien	       specialization in this case.  */
663752284Sobrien	    args_depth = TMPL_ARGS_DEPTH (args);
6638169689Skan	    parms_depth =
6639169689Skan	      TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
664052284Sobrien	    if (args_depth > parms_depth
664152284Sobrien		&& !DECL_TEMPLATE_SPECIALIZATION (t))
664290075Sobrien	      args = get_innermost_template_args (args, parms_depth);
664352284Sobrien	  }
664452284Sobrien	else
664552284Sobrien	  {
664652284Sobrien	    /* This special case arises when we have something like this:
664752284Sobrien
6648169689Skan		 template <class T> struct S {
6649169689Skan		   friend void f<int>(int, double);
665052284Sobrien		 };
665152284Sobrien
665252284Sobrien	       Here, the DECL_TI_TEMPLATE for the friend declaration
6653132718Skan	       will be an IDENTIFIER_NODE.  We are being called from
6654132718Skan	       tsubst_friend_function, and we want only to create a
6655132718Skan	       new decl (R) with appropriate types so that we can call
6656132718Skan	       determine_specialization.  */
665752284Sobrien	    gen_tmpl = NULL_TREE;
665852284Sobrien	  }
665952284Sobrien
666050397Sobrien	if (DECL_CLASS_SCOPE_P (t))
666150397Sobrien	  {
666250397Sobrien	    if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
666350397Sobrien	      member = 2;
666450397Sobrien	    else
666550397Sobrien	      member = 1;
6666169689Skan	    ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6667132718Skan				    complain, t, /*entering_scope=*/1);
666850397Sobrien	  }
666918334Speter	else
667018334Speter	  {
667150397Sobrien	    member = 0;
667290075Sobrien	    ctx = DECL_CONTEXT (t);
667350397Sobrien	  }
6674169689Skan	type = tsubst (TREE_TYPE (t), args, complain, in_decl);
667590075Sobrien	if (type == error_mark_node)
667690075Sobrien	  return error_mark_node;
667718334Speter
667850397Sobrien	/* We do NOT check for matching decls pushed separately at this
6679169689Skan	   point, as they may not represent instantiations of this
6680169689Skan	   template, and in any case are considered separate under the
6681169689Skan	   discrete model.  */
668290075Sobrien	r = copy_decl (t);
668350397Sobrien	DECL_USE_TEMPLATE (r) = 0;
668450397Sobrien	TREE_TYPE (r) = type;
668590075Sobrien	/* Clear out the mangled name and RTL for the instantiation.  */
668690075Sobrien	SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
668790075Sobrien	SET_DECL_RTL (r, NULL_RTX);
6688132718Skan	DECL_INITIAL (r) = NULL_TREE;
668990075Sobrien	DECL_CONTEXT (r) = ctx;
669050397Sobrien
6691169689Skan	if (member && DECL_CONV_FN_P (r))
669252284Sobrien	  /* Type-conversion operator.  Reconstruct the name, in
669352284Sobrien	     case it's the name of one of the template's parameters.  */
669490075Sobrien	  DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
669550397Sobrien
669652284Sobrien	DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
669796263Sobrien				     complain, t);
669850397Sobrien	DECL_RESULT (r) = NULL_TREE;
669950397Sobrien
670050397Sobrien	TREE_STATIC (r) = 0;
670150397Sobrien	TREE_PUBLIC (r) = TREE_PUBLIC (t);
670218334Speter	DECL_EXTERNAL (r) = 1;
6703169689Skan	/* If this is an instantiation of a function with internal
6704169689Skan	   linkage, we already know what object file linkage will be
6705169689Skan	   assigned to the instantiation.  */
6706169689Skan	DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
670750397Sobrien	DECL_DEFER_OUTPUT (r) = 0;
670850397Sobrien	TREE_CHAIN (r) = NULL_TREE;
670950397Sobrien	DECL_PENDING_INLINE_INFO (r) = 0;
671090075Sobrien	DECL_PENDING_INLINE_P (r) = 0;
671190075Sobrien	DECL_SAVED_TREE (r) = NULL_TREE;
671250397Sobrien	TREE_USED (r) = 0;
671390075Sobrien	if (DECL_CLONED_FUNCTION (r))
671490075Sobrien	  {
671590075Sobrien	    DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
671696263Sobrien					       args, complain, t);
671790075Sobrien	    TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
671890075Sobrien	    TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
671990075Sobrien	  }
672018334Speter
672190075Sobrien	/* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
672290075Sobrien	   this in the special friend case mentioned above where
672390075Sobrien	   GEN_TMPL is NULL.  */
672452284Sobrien	if (gen_tmpl)
672550397Sobrien	  {
6726169689Skan	    DECL_TEMPLATE_INFO (r)
672790075Sobrien	      = tree_cons (gen_tmpl, argvec, NULL_TREE);
672852284Sobrien	    SET_DECL_IMPLICIT_INSTANTIATION (r);
6729169689Skan	    register_specialization (r, gen_tmpl, argvec, false);
673050397Sobrien
673190075Sobrien	    /* We're not supposed to instantiate default arguments
673290075Sobrien	       until they are called, for a template.  But, for a
673390075Sobrien	       declaration like:
673460967Sobrien
6735169689Skan		 template <class T> void f ()
6736169689Skan		 { extern void g(int i = T()); }
6737169689Skan
673890075Sobrien	       we should do the substitution when the template is
673990075Sobrien	       instantiated.  We handle the member function case in
674090075Sobrien	       instantiate_class_template since the default arguments
674190075Sobrien	       might refer to other members of the class.  */
674290075Sobrien	    if (!member
674390075Sobrien		&& !PRIMARY_TEMPLATE_P (gen_tmpl)
674490075Sobrien		&& !uses_template_parms (argvec))
674590075Sobrien	      tsubst_default_arguments (r);
674650397Sobrien	  }
6747169689Skan	else
6748169689Skan	  DECL_TEMPLATE_INFO (r) = NULL_TREE;
674950397Sobrien
675052284Sobrien	/* Copy the list of befriending classes.  */
675152284Sobrien	for (friends = &DECL_BEFRIENDING_CLASSES (r);
675252284Sobrien	     *friends;
6753169689Skan	     friends = &TREE_CHAIN (*friends))
675450397Sobrien	  {
675552284Sobrien	    *friends = copy_node (*friends);
675652284Sobrien	    TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
675796263Sobrien					    args, complain,
675852284Sobrien					    in_decl);
675952284Sobrien	  }
676050397Sobrien
676190075Sobrien	if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
676252284Sobrien	  {
676352284Sobrien	    maybe_retrofit_in_chrg (r);
676490075Sobrien	    if (DECL_CONSTRUCTOR_P (r))
676590075Sobrien	      grok_ctor_properties (ctx, r);
676690075Sobrien	    /* If this is an instantiation of a member template, clone it.
676790075Sobrien	       If it isn't, that'll be handled by
676890075Sobrien	       clone_constructors_and_destructors.  */
676990075Sobrien	    if (PRIMARY_TEMPLATE_P (gen_tmpl))
677090075Sobrien	      clone_function_decl (r, /*update_method_vec_p=*/0);
677150397Sobrien	  }
6772169689Skan	else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6773169689Skan		 && !grok_op_properties (r, (complain & tf_error) != 0))
6774169689Skan	  return error_mark_node;
6775132718Skan
6776132718Skan	if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6777132718Skan	  SET_DECL_FRIEND_CONTEXT (r,
6778132718Skan				   tsubst (DECL_FRIEND_CONTEXT (t),
6779132718Skan					    args, complain, in_decl));
6780169689Skan
6781169689Skan	/* Possibly limit visibility based on template args.  */
6782169689Skan	DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
6783169689Skan	if (DECL_VISIBILITY_SPECIFIED (t))
6784169689Skan	  {
6785169689Skan	    DECL_VISIBILITY_SPECIFIED (r) = 0;
6786169689Skan	    DECL_ATTRIBUTES (r)
6787169689Skan	      = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6788169689Skan	  }
6789169689Skan	determine_visibility (r);
679018334Speter      }
679152284Sobrien      break;
679218334Speter
679318334Speter    case PARM_DECL:
679418334Speter      {
6795169689Skan	tree type;
6796169689Skan
679752284Sobrien	r = copy_node (t);
679890075Sobrien	if (DECL_TEMPLATE_PARM_P (t))
679990075Sobrien	  SET_DECL_TEMPLATE_PARM_P (r);
6800117395Skan
6801169689Skan	type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6802169689Skan	type = type_decays_to (type);
680350397Sobrien	TREE_TYPE (r) = type;
6804169689Skan	cp_apply_type_quals_to_decl (cp_type_quals (type), r);
680552284Sobrien
6806132718Skan	if (DECL_INITIAL (r))
6807132718Skan	  {
6808132718Skan	    if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6809132718Skan	      DECL_INITIAL (r) = TREE_TYPE (r);
6810132718Skan	    else
6811132718Skan	      DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6812132718Skan					 complain, in_decl);
6813132718Skan	  }
681450397Sobrien
681550397Sobrien	DECL_CONTEXT (r) = NULL_TREE;
6816117395Skan
6817117395Skan	if (!DECL_TEMPLATE_PARM_P (r))
6818117395Skan	  DECL_ARG_TYPE (r) = type_passed_as (type);
681918334Speter	if (TREE_CHAIN (t))
682052284Sobrien	  TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
682196263Sobrien				   complain, TREE_CHAIN (t));
682218334Speter      }
682352284Sobrien      break;
682418334Speter
682550397Sobrien    case FIELD_DECL:
682650397Sobrien      {
6827169689Skan	tree type;
6828169689Skan
682990075Sobrien	r = copy_decl (t);
6830169689Skan	type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6831169689Skan	if (type == error_mark_node)
6832169689Skan	  return error_mark_node;
683350397Sobrien	TREE_TYPE (r) = type;
6834169689Skan	cp_apply_type_quals_to_decl (cp_type_quals (type), r);
683552284Sobrien
6836169689Skan	/* DECL_INITIAL gives the number of bits in a bit-field.  */
6837169689Skan	DECL_INITIAL (r)
6838169689Skan	  = tsubst_expr (DECL_INITIAL (t), args,
6839169689Skan			 complain, in_decl,
6840169689Skan			 /*integral_constant_expression_p=*/true);
684152284Sobrien	/* We don't have to set DECL_CONTEXT here; it is set by
684252284Sobrien	   finish_member_declaration.  */
684350397Sobrien	TREE_CHAIN (r) = NULL_TREE;
6844169689Skan	if (VOID_TYPE_P (type))
6845169689Skan	  error ("instantiation of %q+D as type %qT", r, type);
684650397Sobrien      }
684752284Sobrien      break;
684850397Sobrien
684950397Sobrien    case USING_DECL:
6850169689Skan      /* We reach here only for member using decls.  */
6851169689Skan      if (DECL_DEPENDENT_P (t))
6852169689Skan	{
6853169689Skan	  r = do_class_using_decl
6854169689Skan	    (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6855169689Skan	     tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6856169689Skan	  if (!r)
6857169689Skan	    r = error_mark_node;
6858169689Skan	}
6859169689Skan      else
6860169689Skan	{
6861169689Skan	  r = copy_node (t);
6862169689Skan	  TREE_CHAIN (r) = NULL_TREE;
6863169689Skan	}
686452284Sobrien      break;
686550397Sobrien
686690075Sobrien    case TYPE_DECL:
686750397Sobrien    case VAR_DECL:
686850397Sobrien      {
686990075Sobrien	tree argvec = NULL_TREE;
687090075Sobrien	tree gen_tmpl = NULL_TREE;
687152284Sobrien	tree spec;
687290075Sobrien	tree tmpl = NULL_TREE;
687390075Sobrien	tree ctx;
6874169689Skan	tree type = NULL_TREE;
6875169689Skan	bool local_p;
687650397Sobrien
6877169689Skan	if (TREE_CODE (t) == TYPE_DECL)
6878169689Skan	  {
6879169689Skan	    type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6880169689Skan	    if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6881169689Skan		|| t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6882169689Skan	      {
6883169689Skan		/* If this is the canonical decl, we don't have to
6884169689Skan		   mess with instantiations, and often we can't (for
6885169689Skan		   typename, template type parms and such).  Note that
6886169689Skan		   TYPE_NAME is not correct for the above test if
6887169689Skan		   we've copied the type for a typedef.  */
6888169689Skan		r = TYPE_NAME (type);
6889169689Skan		break;
6890169689Skan	      }
6891169689Skan	  }
689290075Sobrien
6893169689Skan	/* Check to see if we already have the specialization we
6894169689Skan	   need.  */
6895169689Skan	spec = NULL_TREE;
6896169689Skan	if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6897169689Skan	  {
6898169689Skan	    /* T is a static data member or namespace-scope entity.
6899169689Skan	       We have to substitute into namespace-scope variables
6900169689Skan	       (even though such entities are never templates) because
6901169689Skan	       of cases like:
6902169689Skan
6903169689Skan	         template <class T> void f() { extern T t; }
6904169689Skan
6905169689Skan	       where the entity referenced is not known until
6906169689Skan	       instantiation time.  */
6907169689Skan	    local_p = false;
6908169689Skan	    ctx = DECL_CONTEXT (t);
6909169689Skan	    if (DECL_CLASS_SCOPE_P (t))
6910169689Skan	      {
6911169689Skan		ctx = tsubst_aggr_type (ctx, args,
6912169689Skan					complain,
6913169689Skan					in_decl, /*entering_scope=*/1);
6914169689Skan		/* If CTX is unchanged, then T is in fact the
6915169689Skan		   specialization we want.  That situation occurs when
6916169689Skan		   referencing a static data member within in its own
6917169689Skan		   class.  We can use pointer equality, rather than
6918169689Skan		   same_type_p, because DECL_CONTEXT is always
6919169689Skan		   canonical.  */
6920169689Skan		if (ctx == DECL_CONTEXT (t))
6921169689Skan		  spec = t;
6922169689Skan	      }
6923169689Skan
6924169689Skan	    if (!spec)
6925169689Skan	      {
6926169689Skan		tmpl = DECL_TI_TEMPLATE (t);
6927169689Skan		gen_tmpl = most_general_template (tmpl);
6928169689Skan		argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6929169689Skan		spec = (retrieve_specialization
6930169689Skan			(gen_tmpl, argvec,
6931169689Skan			 /*class_specializations_p=*/false));
6932169689Skan	      }
6933169689Skan	  }
693490075Sobrien	else
693590075Sobrien	  {
6936169689Skan	    /* A local variable.  */
6937169689Skan	    local_p = true;
693890075Sobrien	    /* Subsequent calls to pushdecl will fill this in.  */
693990075Sobrien	    ctx = NULL_TREE;
6940169689Skan	    spec = retrieve_local_specialization (t);
694190075Sobrien	  }
6942169689Skan	/* If we already have the specialization we need, there is
6943169689Skan	   nothing more to do.  */
694452284Sobrien	if (spec)
694550397Sobrien	  {
694652284Sobrien	    r = spec;
694752284Sobrien	    break;
694850397Sobrien	  }
694950397Sobrien
6950169689Skan	/* Create a new node for the specialization we need.  */
695190075Sobrien	r = copy_decl (t);
6952104752Skan	if (TREE_CODE (r) == VAR_DECL)
6953132718Skan	  {
6954169689Skan	    /* Even if the original location is out of scope, the
6955169689Skan	       newly substituted one is not.  */
6956169689Skan	    DECL_DEAD_FOR_LOCAL (r) = 0;
6957169689Skan	    DECL_INITIALIZED_P (r) = 0;
6958169689Skan	    DECL_TEMPLATE_INSTANTIATED (r) = 0;
6959169689Skan	    type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6960169689Skan	    if (type == error_mark_node)
6961169689Skan	      return error_mark_node;
6962169689Skan	    if (TREE_CODE (type) == FUNCTION_TYPE)
6963169689Skan	      {
6964169689Skan		/* It may seem that this case cannot occur, since:
6965169689Skan
6966169689Skan		     typedef void f();
6967169689Skan		     void g() { f x; }
6968169689Skan
6969169689Skan		   declares a function, not a variable.  However:
6970169689Skan
6971169689Skan		     typedef void f();
6972169689Skan		     template <typename T> void g() { T t; }
6973169689Skan		     template void g<f>();
6974169689Skan
6975169689Skan		   is an attempt to declare a variable with function
6976169689Skan		   type.  */
6977169689Skan		error ("variable %qD has function type",
6978169689Skan		       /* R is not yet sufficiently initialized, so we
6979169689Skan			  just use its name.  */
6980169689Skan		       DECL_NAME (r));
6981169689Skan		return error_mark_node;
6982169689Skan	      }
6983132718Skan	    type = complete_type (type);
6984132718Skan	    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6985132718Skan	      = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6986146895Skan	    type = check_var_type (DECL_NAME (r), type);
6987169689Skan
6988169689Skan	    if (DECL_HAS_VALUE_EXPR_P (t))
6989169689Skan	      {
6990169689Skan		tree ve = DECL_VALUE_EXPR (t);
6991169689Skan		ve = tsubst_expr (ve, args, complain, in_decl,
6992169689Skan				  /*constant_expression_p=*/false);
6993169689Skan		SET_DECL_VALUE_EXPR (r, ve);
6994169689Skan	      }
6995132718Skan	  }
6996119256Skan	else if (DECL_SELF_REFERENCE_P (t))
6997119256Skan	  SET_DECL_SELF_REFERENCE_P (r);
699850397Sobrien	TREE_TYPE (r) = type;
6999169689Skan	cp_apply_type_quals_to_decl (cp_type_quals (type), r);
700050397Sobrien	DECL_CONTEXT (r) = ctx;
700190075Sobrien	/* Clear out the mangled name and RTL for the instantiation.  */
700290075Sobrien	SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7003169689Skan	if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7004169689Skan	  SET_DECL_RTL (r, NULL_RTX);
7005169689Skan	/* The initializer must not be expanded until it is required;
7006169689Skan	   see [temp.inst].  */
700750397Sobrien	DECL_INITIAL (r) = NULL_TREE;
7008169689Skan	if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7009169689Skan	  SET_DECL_RTL (r, NULL_RTX);
701090075Sobrien	DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
701190075Sobrien	if (TREE_CODE (r) == VAR_DECL)
7012117395Skan	  {
7013169689Skan	    /* Possibly limit visibility based on template args.  */
7014169689Skan	    DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7015169689Skan	    if (DECL_VISIBILITY_SPECIFIED (t))
7016169689Skan	      {
7017169689Skan		DECL_VISIBILITY_SPECIFIED (r) = 0;
7018169689Skan		DECL_ATTRIBUTES (r)
7019169689Skan		  = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7020169689Skan	      }
7021169689Skan	    determine_visibility (r);
7022117395Skan	  }
702350397Sobrien
702490075Sobrien	if (!local_p)
702590075Sobrien	  {
702690075Sobrien	    /* A static data member declaration is always marked
702790075Sobrien	       external when it is declared in-class, even if an
702890075Sobrien	       initializer is present.  We mimic the non-template
702990075Sobrien	       processing here.  */
703090075Sobrien	    DECL_EXTERNAL (r) = 1;
703190075Sobrien
7032169689Skan	    register_specialization (r, gen_tmpl, argvec, false);
703390075Sobrien	    DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
703490075Sobrien	    SET_DECL_IMPLICIT_INSTANTIATION (r);
703590075Sobrien	  }
703690075Sobrien	else
703790075Sobrien	  register_local_specialization (r, t);
703890075Sobrien
703950397Sobrien	TREE_CHAIN (r) = NULL_TREE;
7040104752Skan	layout_decl (r, 0);
704150397Sobrien      }
704252284Sobrien      break;
704350397Sobrien
704452284Sobrien    default:
7045169689Skan      gcc_unreachable ();
7046169689Skan    }
704752284Sobrien
704852284Sobrien  /* Restore the file and line information.  */
7049132718Skan  input_location = saved_loc;
705052284Sobrien
705152284Sobrien  return r;
705252284Sobrien}
705352284Sobrien
7054132718Skan/* Substitute into the ARG_TYPES of a function type.  */
705552284Sobrien
705652284Sobrienstatic tree
7057169689Skantsubst_arg_types (tree arg_types,
7058169689Skan		  tree args,
7059169689Skan		  tsubst_flags_t complain,
7060169689Skan		  tree in_decl)
706152284Sobrien{
706252284Sobrien  tree remaining_arg_types;
706352284Sobrien  tree type;
7064161651Skan  tree default_arg;
7065161651Skan  tree result = NULL_TREE;
706652284Sobrien
706752284Sobrien  if (!arg_types || arg_types == void_list_node)
706852284Sobrien    return arg_types;
7069169689Skan
707052284Sobrien  remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
707152284Sobrien					  args, complain, in_decl);
707252284Sobrien  if (remaining_arg_types == error_mark_node)
707352284Sobrien    return error_mark_node;
707452284Sobrien
707552284Sobrien  type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
707652284Sobrien  if (type == error_mark_node)
707752284Sobrien    return error_mark_node;
707890075Sobrien  if (VOID_TYPE_P (type))
707990075Sobrien    {
708096263Sobrien      if (complain & tf_error)
7081169689Skan	{
7082169689Skan	  error ("invalid parameter type %qT", type);
7083169689Skan	  if (in_decl)
7084169689Skan	    error ("in declaration %q+D", in_decl);
7085169689Skan	}
708690075Sobrien      return error_mark_node;
708790075Sobrien    }
708852284Sobrien
708952284Sobrien  /* Do array-to-pointer, function-to-pointer conversion, and ignore
709052284Sobrien     top-level qualifiers as required.  */
709152284Sobrien  type = TYPE_MAIN_VARIANT (type_decays_to (type));
709252284Sobrien
7093161651Skan  /* We do not substitute into default arguments here.  The standard
7094161651Skan     mandates that they be instantiated only when needed, which is
7095161651Skan     done in build_over_call.  */
7096161651Skan  default_arg = TREE_PURPOSE (arg_types);
7097169689Skan
7098161651Skan  if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
7099161651Skan    {
7100161651Skan      /* We've instantiated a template before its default arguments
7101169689Skan	 have been parsed.  This can happen for a nested template
7102169689Skan	 class, and is not an error unless we require the default
7103169689Skan	 argument in a call of this function.  */
7104161651Skan      result = tree_cons (default_arg, type, remaining_arg_types);
7105169689Skan      VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
7106161651Skan    }
7107161651Skan  else
7108161651Skan    result = hash_tree_cons (default_arg, type, remaining_arg_types);
7109169689Skan
7110161651Skan  return result;
711152284Sobrien}
711252284Sobrien
711352284Sobrien/* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
711452284Sobrien   *not* handle the exception-specification for FNTYPE, because the
711552284Sobrien   initial substitution of explicitly provided template parameters
711652284Sobrien   during argument deduction forbids substitution into the
711752284Sobrien   exception-specification:
711852284Sobrien
711952284Sobrien     [temp.deduct]
712052284Sobrien
712152284Sobrien     All references in the function type of the function template to  the
712252284Sobrien     corresponding template parameters are replaced by the specified tem-
712352284Sobrien     plate argument values.  If a substitution in a template parameter or
712452284Sobrien     in  the function type of the function template results in an invalid
712552284Sobrien     type, type deduction fails.  [Note: The equivalent  substitution  in
712652284Sobrien     exception specifications is done only when the function is instanti-
712752284Sobrien     ated, at which point a program is  ill-formed  if  the  substitution
712852284Sobrien     results in an invalid type.]  */
712952284Sobrien
713052284Sobrienstatic tree
7131169689Skantsubst_function_type (tree t,
7132169689Skan		      tree args,
7133169689Skan		      tsubst_flags_t complain,
7134169689Skan		      tree in_decl)
713552284Sobrien{
713652284Sobrien  tree return_type;
713752284Sobrien  tree arg_types;
713852284Sobrien  tree fntype;
713952284Sobrien
714052284Sobrien  /* The TYPE_CONTEXT is not used for function/method types.  */
7141169689Skan  gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
714252284Sobrien
714390075Sobrien  /* Substitute the return type.  */
714452284Sobrien  return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
714552284Sobrien  if (return_type == error_mark_node)
714652284Sobrien    return error_mark_node;
7147169689Skan  /* The standard does not presently indicate that creation of a
7148169689Skan     function type with an invalid return type is a deduction failure.
7149169689Skan     However, that is clearly analogous to creating an array of "void"
7150169689Skan     or a reference to a reference.  This is core issue #486.  */
7151169689Skan  if (TREE_CODE (return_type) == ARRAY_TYPE
7152169689Skan      || TREE_CODE (return_type) == FUNCTION_TYPE)
7153169689Skan    {
7154169689Skan      if (complain & tf_error)
7155169689Skan	{
7156169689Skan	  if (TREE_CODE (return_type) == ARRAY_TYPE)
7157169689Skan	    error ("function returning an array");
7158169689Skan	  else
7159169689Skan	    error ("function returning a function");
7160169689Skan	}
7161169689Skan      return error_mark_node;
7162169689Skan    }
716352284Sobrien
7164132718Skan  /* Substitute the argument types.  */
716552284Sobrien  arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
7166169689Skan				complain, in_decl);
716752284Sobrien  if (arg_types == error_mark_node)
716852284Sobrien    return error_mark_node;
7169169689Skan
717052284Sobrien  /* Construct a new type node and return it.  */
717152284Sobrien  if (TREE_CODE (t) == FUNCTION_TYPE)
717252284Sobrien    fntype = build_function_type (return_type, arg_types);
717352284Sobrien  else
717452284Sobrien    {
717552284Sobrien      tree r = TREE_TYPE (TREE_VALUE (arg_types));
717652284Sobrien      if (! IS_AGGR_TYPE (r))
717752284Sobrien	{
717852284Sobrien	  /* [temp.deduct]
7179169689Skan
718052284Sobrien	     Type deduction may fail for any of the following
718152284Sobrien	     reasons:
7182169689Skan
718352284Sobrien	     -- Attempting to create "pointer to member of T" when T
718452284Sobrien	     is not a class type.  */
718596263Sobrien	  if (complain & tf_error)
7186169689Skan	    error ("creating pointer to member function of non-class type %qT",
718752284Sobrien		      r);
718852284Sobrien	  return error_mark_node;
718952284Sobrien	}
7190169689Skan
7191169689Skan      fntype = build_method_type_directly (r, return_type,
7192132718Skan					   TREE_CHAIN (arg_types));
719352284Sobrien    }
719496263Sobrien  fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
7195132718Skan  fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
7196169689Skan
7197169689Skan  return fntype;
719852284Sobrien}
719952284Sobrien
7200169689Skan/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
7201169689Skan   ARGS into that specification, and return the substituted
7202169689Skan   specification.  If there is no specification, return NULL_TREE.  */
720352284Sobrien
720452284Sobrienstatic tree
7205169689Skantsubst_exception_specification (tree fntype,
7206169689Skan				tree args,
7207169689Skan				tsubst_flags_t complain,
7208169689Skan				tree in_decl)
720952284Sobrien{
7210169689Skan  tree specs;
7211169689Skan  tree new_specs;
721252284Sobrien
7213169689Skan  specs = TYPE_RAISES_EXCEPTIONS (fntype);
7214169689Skan  new_specs = NULL_TREE;
7215169689Skan  if (specs)
7216169689Skan    {
7217169689Skan      if (! TREE_VALUE (specs))
7218169689Skan	new_specs = specs;
7219169689Skan      else
7220169689Skan	while (specs)
7221169689Skan	  {
7222169689Skan	    tree spec;
7223169689Skan	    spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7224169689Skan	    if (spec == error_mark_node)
7225169689Skan	      return spec;
7226169689Skan	    new_specs = add_exception_specifier (new_specs, spec, complain);
7227169689Skan	    specs = TREE_CHAIN (specs);
7228169689Skan	  }
7229169689Skan    }
7230169689Skan  return new_specs;
723152284Sobrien}
723252284Sobrien
723352284Sobrien/* Take the tree structure T and replace template parameters used
723452284Sobrien   therein with the argument vector ARGS.  IN_DECL is an associated
723552284Sobrien   decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
723696263Sobrien   Issue error and warning messages under control of COMPLAIN.  Note
723796263Sobrien   that we must be relatively non-tolerant of extensions here, in
723896263Sobrien   order to preserve conformance; if we allow substitutions that
723996263Sobrien   should not be allowed, we may allow argument deductions that should
724096263Sobrien   not succeed, and therefore report ambiguous overload situations
724196263Sobrien   where there are none.  In theory, we could allow the substitution,
724296263Sobrien   but indicate that it should have failed, and allow our caller to
724396263Sobrien   make sure that the right thing happens, but we don't try to do this
724496263Sobrien   yet.
724552284Sobrien
724652284Sobrien   This function is used for dealing with types, decls and the like;
724752284Sobrien   for expressions, use tsubst_expr or tsubst_copy.  */
724852284Sobrien
7249132718Skanstatic tree
7250132718Skantsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
725152284Sobrien{
725252284Sobrien  tree type, r;
725352284Sobrien
725452284Sobrien  if (t == NULL_TREE || t == error_mark_node
725552284Sobrien      || t == integer_type_node
725652284Sobrien      || t == void_type_node
725752284Sobrien      || t == char_type_node
7258146895Skan      || t == unknown_type_node
725952284Sobrien      || TREE_CODE (t) == NAMESPACE_DECL)
726052284Sobrien    return t;
726152284Sobrien
7262169689Skan  if (DECL_P (t))
7263169689Skan    return tsubst_decl (t, args, complain);
7264169689Skan
726552284Sobrien  if (TREE_CODE (t) == IDENTIFIER_NODE)
726652284Sobrien    type = IDENTIFIER_TYPE_VALUE (t);
726752284Sobrien  else
726852284Sobrien    type = TREE_TYPE (t);
726952284Sobrien
7270169689Skan  gcc_assert (type != unknown_type_node);
7271132718Skan
7272169689Skan  if (type
727352284Sobrien      && TREE_CODE (t) != TYPENAME_TYPE
727452284Sobrien      && TREE_CODE (t) != IDENTIFIER_NODE
727552284Sobrien      && TREE_CODE (t) != FUNCTION_TYPE
727652284Sobrien      && TREE_CODE (t) != METHOD_TYPE)
727752284Sobrien    type = tsubst (type, args, complain, in_decl);
727852284Sobrien  if (type == error_mark_node)
727952284Sobrien    return error_mark_node;
728052284Sobrien
728152284Sobrien  switch (TREE_CODE (t))
728252284Sobrien    {
728352284Sobrien    case RECORD_TYPE:
728452284Sobrien    case UNION_TYPE:
728552284Sobrien    case ENUMERAL_TYPE:
728652284Sobrien      return tsubst_aggr_type (t, args, complain, in_decl,
728752284Sobrien			       /*entering_scope=*/0);
728852284Sobrien
728952284Sobrien    case ERROR_MARK:
729052284Sobrien    case IDENTIFIER_NODE:
729152284Sobrien    case VOID_TYPE:
729252284Sobrien    case REAL_TYPE:
729352284Sobrien    case COMPLEX_TYPE:
729490075Sobrien    case VECTOR_TYPE:
729552284Sobrien    case BOOLEAN_TYPE:
729652284Sobrien    case INTEGER_CST:
729752284Sobrien    case REAL_CST:
729852284Sobrien    case STRING_CST:
729952284Sobrien      return t;
730052284Sobrien
730152284Sobrien    case INTEGER_TYPE:
730252284Sobrien      if (t == integer_type_node)
730352284Sobrien	return t;
730452284Sobrien
730552284Sobrien      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
730652284Sobrien	  && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
730752284Sobrien	return t;
730852284Sobrien
730950397Sobrien      {
731052284Sobrien	tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
731152284Sobrien
7312169689Skan	max = tsubst_expr (omax, args, complain, in_decl,
7313169689Skan			   /*integral_constant_expression_p=*/false);
7314169689Skan	max = fold_decl_constant_value (max);
731552750Sobrien
7316169689Skan	if (TREE_CODE (max) != INTEGER_CST
7317169689Skan	    && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7318169689Skan	    && !at_function_scope_p ())
731952284Sobrien	  {
7320169689Skan	    if (complain & tf_error)
7321169689Skan	      error ("array bound is not an integer constant");
7322169689Skan	    return error_mark_node;
732352284Sobrien	  }
732452284Sobrien
7325169689Skan	/* [temp.deduct]
732652284Sobrien
7327169689Skan	   Type deduction may fail for any of the following
7328169689Skan	   reasons:
7329169689Skan
7330169689Skan	     Attempting to create an array with a size that is
7331169689Skan	     zero or negative.  */
7332169689Skan	if (integer_zerop (max) && !(complain & tf_error))
7333169689Skan	  /* We must fail if performing argument deduction (as
7334169689Skan	     indicated by the state of complain), so that
7335169689Skan	     another substitution can be found.  */
7336169689Skan	  return error_mark_node;
7337169689Skan	else if (TREE_CODE (max) == INTEGER_CST
7338169689Skan		 && INT_CST_LT (max, integer_zero_node))
7339169689Skan	  {
734096263Sobrien	    if (complain & tf_error)
7341169689Skan	      error ("creating array with negative size (%qE)", max);
734252284Sobrien
734352284Sobrien	    return error_mark_node;
734452284Sobrien	  }
734552284Sobrien
734690075Sobrien	return compute_array_index_type (NULL_TREE, max);
734752284Sobrien      }
734852284Sobrien
734952284Sobrien    case TEMPLATE_TYPE_PARM:
735052284Sobrien    case TEMPLATE_TEMPLATE_PARM:
735190075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
735252284Sobrien    case TEMPLATE_PARM_INDEX:
735352284Sobrien      {
735452284Sobrien	int idx;
735552284Sobrien	int level;
735652284Sobrien	int levels;
7357169689Skan	tree arg = NULL_TREE;
735852284Sobrien
735952284Sobrien	r = NULL_TREE;
736052284Sobrien
7361169689Skan	gcc_assert (TREE_VEC_LENGTH (args) > 0);
736252284Sobrien	if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
736390075Sobrien	    || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
736490075Sobrien	    || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
736552284Sobrien	  {
736652284Sobrien	    idx = TEMPLATE_TYPE_IDX (t);
736752284Sobrien	    level = TEMPLATE_TYPE_LEVEL (t);
736852284Sobrien	  }
736952284Sobrien	else
737052284Sobrien	  {
737152284Sobrien	    idx = TEMPLATE_PARM_IDX (t);
737252284Sobrien	    level = TEMPLATE_PARM_LEVEL (t);
737352284Sobrien	  }
737452284Sobrien
7375169689Skan	levels = TMPL_ARGS_DEPTH (args);
7376169689Skan	if (level <= levels)
7377169689Skan	  arg = TMPL_ARG (args, level, idx);
7378169689Skan
7379169689Skan	if (arg == error_mark_node)
7380169689Skan	  return error_mark_node;
7381169689Skan	else if (arg != NULL_TREE)
738252284Sobrien	  {
7383169689Skan	    if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
738452284Sobrien	      {
7385169689Skan		int quals;
7386169689Skan		gcc_assert (TYPE_P (arg));
738752284Sobrien
7388169689Skan		/* cv-quals from the template are discarded when
7389169689Skan		   substituting in a function or reference type.  */
7390169689Skan		if (TREE_CODE (arg) == FUNCTION_TYPE
7391169689Skan		    || TREE_CODE (arg) == METHOD_TYPE
7392169689Skan		    || TREE_CODE (arg) == REFERENCE_TYPE)
7393169689Skan		  quals = cp_type_quals (arg);
739452284Sobrien		else
7395169689Skan		  quals = cp_type_quals (arg) | cp_type_quals (t);
7396169689Skan
7397169689Skan		return cp_build_qualified_type_real
7398169689Skan		  (arg, quals, complain | tf_ignore_bad_quals);
739952284Sobrien	      }
7400169689Skan	    else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7401169689Skan	      {
7402169689Skan		/* We are processing a type constructed from a
7403169689Skan		   template template parameter.  */
7404169689Skan		tree argvec = tsubst (TYPE_TI_ARGS (t),
7405169689Skan				      args, complain, in_decl);
7406169689Skan		if (argvec == error_mark_node)
7407169689Skan		  return error_mark_node;
7408169689Skan
7409169689Skan		/* We can get a TEMPLATE_TEMPLATE_PARM here when we
7410169689Skan		   are resolving nested-types in the signature of a
7411169689Skan		   member function templates.  Otherwise ARG is a
7412169689Skan		   TEMPLATE_DECL and is the real template to be
7413169689Skan		   instantiated.  */
7414169689Skan		if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7415169689Skan		  arg = TYPE_NAME (arg);
7416169689Skan
7417169689Skan		r = lookup_template_class (arg,
7418169689Skan					   argvec, in_decl,
7419169689Skan					   DECL_CONTEXT (arg),
7420169689Skan					    /*entering_scope=*/0,
7421169689Skan					   complain);
7422169689Skan		return cp_build_qualified_type_real
7423169689Skan		  (r, TYPE_QUALS (t), complain);
7424169689Skan	      }
7425169689Skan	    else
7426169689Skan	      /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
7427169689Skan	      return arg;
742852284Sobrien	  }
742952284Sobrien
743052284Sobrien	if (level == 1)
743152284Sobrien	  /* This can happen during the attempted tsubst'ing in
743252284Sobrien	     unify.  This means that we don't yet have any information
743352284Sobrien	     about the template parameter in question.  */
743452284Sobrien	  return t;
743552284Sobrien
743652284Sobrien	/* If we get here, we must have been looking at a parm for a
743752284Sobrien	   more deeply nested template.  Make a new version of this
743852284Sobrien	   template parameter, but with a lower level.  */
743952284Sobrien	switch (TREE_CODE (t))
744052284Sobrien	  {
744152284Sobrien	  case TEMPLATE_TYPE_PARM:
744252284Sobrien	  case TEMPLATE_TEMPLATE_PARM:
744390075Sobrien	  case BOUND_TEMPLATE_TEMPLATE_PARM:
744490075Sobrien	    if (cp_type_quals (t))
744552284Sobrien	      {
744690075Sobrien		r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7447169689Skan		r = cp_build_qualified_type_real
7448169689Skan		  (r, cp_type_quals (t),
744996263Sobrien		   complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
745096263Sobrien			       ? tf_ignore_bad_quals : 0));
745190075Sobrien	      }
745290075Sobrien	    else
745390075Sobrien	      {
745490075Sobrien		r = copy_type (t);
745590075Sobrien		TEMPLATE_TYPE_PARM_INDEX (r)
745690075Sobrien		  = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
745790075Sobrien						r, levels);
745890075Sobrien		TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
745990075Sobrien		TYPE_MAIN_VARIANT (r) = r;
746090075Sobrien		TYPE_POINTER_TO (r) = NULL_TREE;
746190075Sobrien		TYPE_REFERENCE_TO (r) = NULL_TREE;
746252284Sobrien
746390075Sobrien		if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
746490075Sobrien		  {
746590075Sobrien		    tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7466169689Skan					  complain, in_decl);
746790075Sobrien		    if (argvec == error_mark_node)
746890075Sobrien		      return error_mark_node;
746990075Sobrien
747090075Sobrien		    TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
747190075Sobrien		      = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
747290075Sobrien		  }
747352284Sobrien	      }
747452284Sobrien	    break;
747552284Sobrien
747652284Sobrien	  case TEMPLATE_PARM_INDEX:
747752284Sobrien	    r = reduce_template_parm_level (t, type, levels);
747852284Sobrien	    break;
7479169689Skan
748052284Sobrien	  default:
7481169689Skan	    gcc_unreachable ();
748252284Sobrien	  }
748352284Sobrien
748450397Sobrien	return r;
748552284Sobrien      }
748650397Sobrien
748718334Speter    case TREE_LIST:
748818334Speter      {
7489169689Skan	tree purpose, value, chain;
749018334Speter
749118334Speter	if (t == void_list_node)
749218334Speter	  return t;
749318334Speter
749418334Speter	purpose = TREE_PURPOSE (t);
749518334Speter	if (purpose)
749652284Sobrien	  {
749752284Sobrien	    purpose = tsubst (purpose, args, complain, in_decl);
749852284Sobrien	    if (purpose == error_mark_node)
749952284Sobrien	      return error_mark_node;
750052284Sobrien	  }
750118334Speter	value = TREE_VALUE (t);
750218334Speter	if (value)
750352284Sobrien	  {
750452284Sobrien	    value = tsubst (value, args, complain, in_decl);
750552284Sobrien	    if (value == error_mark_node)
750652284Sobrien	      return error_mark_node;
750752284Sobrien	  }
750818334Speter	chain = TREE_CHAIN (t);
750918334Speter	if (chain && chain != void_type_node)
751052284Sobrien	  {
751152284Sobrien	    chain = tsubst (chain, args, complain, in_decl);
751252284Sobrien	    if (chain == error_mark_node)
751352284Sobrien	      return error_mark_node;
751452284Sobrien	  }
751518334Speter	if (purpose == TREE_PURPOSE (t)
751618334Speter	    && value == TREE_VALUE (t)
751718334Speter	    && chain == TREE_CHAIN (t))
751818334Speter	  return t;
7519169689Skan	return hash_tree_cons (purpose, value, chain);
752018334Speter      }
752118334Speter
7522169689Skan    case TREE_BINFO:
7523169689Skan      /* We should never be tsubsting a binfo.  */
7524169689Skan      gcc_unreachable ();
752518334Speter
7526169689Skan    case TREE_VEC:
7527169689Skan      /* A vector of template arguments.  */
7528169689Skan      gcc_assert (!type);
7529132718Skan      return tsubst_template_args (t, args, complain, in_decl);
753050397Sobrien
753118334Speter    case POINTER_TYPE:
753218334Speter    case REFERENCE_TYPE:
753318334Speter      {
753418334Speter	enum tree_code code;
753550397Sobrien
753690075Sobrien	if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
753718334Speter	  return t;
753818334Speter
753918334Speter	code = TREE_CODE (t);
754052284Sobrien
754152284Sobrien
754252284Sobrien	/* [temp.deduct]
7543169689Skan
754452284Sobrien	   Type deduction may fail for any of the following
7545169689Skan	   reasons:
754652284Sobrien
754752284Sobrien	   -- Attempting to create a pointer to reference type.
754852284Sobrien	   -- Attempting to create a reference to a reference type or
754952284Sobrien	      a reference to void.  */
755052284Sobrien	if (TREE_CODE (type) == REFERENCE_TYPE
755152284Sobrien	    || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
755250397Sobrien	  {
7553132718Skan	    static location_t last_loc;
755450397Sobrien
755550397Sobrien	    /* We keep track of the last time we issued this error
755650397Sobrien	       message to avoid spewing a ton of messages during a
755750397Sobrien	       single bad template instantiation.  */
755896263Sobrien	    if (complain & tf_error
7559169689Skan#ifdef USE_MAPPED_LOCATION
7560169689Skan		&& last_loc != input_location
7561169689Skan#else
7562132718Skan		&& (last_loc.line != input_line
7563169689Skan		    || last_loc.file != input_filename)
7564169689Skan#endif
7565169689Skan		  )
756650397Sobrien	      {
756752284Sobrien		if (TREE_CODE (type) == VOID_TYPE)
756890075Sobrien		  error ("forming reference to void");
756952284Sobrien		else
7570169689Skan		  error ("forming %s to reference type %qT",
7571169689Skan			 (code == POINTER_TYPE) ? "pointer" : "reference",
7572169689Skan			 type);
7573132718Skan		last_loc = input_location;
757450397Sobrien	      }
757550397Sobrien
757652284Sobrien	    return error_mark_node;
757750397Sobrien	  }
757850397Sobrien	else if (code == POINTER_TYPE)
757990075Sobrien	  {
758090075Sobrien	    r = build_pointer_type (type);
758190075Sobrien	    if (TREE_CODE (type) == METHOD_TYPE)
758290075Sobrien	      r = build_ptrmemfunc_type (r);
758390075Sobrien	  }
758418334Speter	else
758518334Speter	  r = build_reference_type (type);
758690075Sobrien	r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
758750397Sobrien
758890075Sobrien	if (r != error_mark_node)
758990075Sobrien	  /* Will this ever be needed for TYPE_..._TO values?  */
759090075Sobrien	  layout_type (r);
7591169689Skan
759218334Speter	return r;
759318334Speter      }
759418334Speter    case OFFSET_TYPE:
759552284Sobrien      {
759652284Sobrien	r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
759752284Sobrien	if (r == error_mark_node || !IS_AGGR_TYPE (r))
759852284Sobrien	  {
759952284Sobrien	    /* [temp.deduct]
760052284Sobrien
760152284Sobrien	       Type deduction may fail for any of the following
760252284Sobrien	       reasons:
7603169689Skan
760452284Sobrien	       -- Attempting to create "pointer to member of T" when T
7605169689Skan		  is not a class type.  */
760696263Sobrien	    if (complain & tf_error)
7607169689Skan	      error ("creating pointer to member of non-class type %qT", r);
760852284Sobrien	    return error_mark_node;
760952284Sobrien	  }
761090075Sobrien	if (TREE_CODE (type) == REFERENCE_TYPE)
761190075Sobrien	  {
7612117395Skan	    if (complain & tf_error)
7613169689Skan	      error ("creating pointer to member reference type %qT", type);
761490075Sobrien	    return error_mark_node;
761590075Sobrien	  }
7616169689Skan	if (TREE_CODE (type) == VOID_TYPE)
7617169689Skan	  {
7618169689Skan	    if (complain & tf_error)
7619169689Skan	      error ("creating pointer to member of type void");
7620169689Skan	    return error_mark_node;
7621169689Skan	  }
7622169689Skan	gcc_assert (TREE_CODE (type) != METHOD_TYPE);
762390075Sobrien	if (TREE_CODE (type) == FUNCTION_TYPE)
7624132718Skan	  {
7625169689Skan	    /* The type of the implicit object parameter gets its
7626169689Skan	       cv-qualifiers from the FUNCTION_TYPE. */
7627132718Skan	    tree method_type;
7628169689Skan	    tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7629169689Skan						      cp_type_quals (type));
7630169689Skan	    tree memptr;
7631169689Skan	    method_type = build_method_type_directly (this_type,
7632132718Skan						      TREE_TYPE (type),
7633132718Skan						      TYPE_ARG_TYPES (type));
7634169689Skan	    memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7635169689Skan	    return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7636169689Skan						 complain);
7637132718Skan	  }
763890075Sobrien	else
7639132718Skan	  return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7640132718Skan					       TYPE_QUALS (t),
7641132718Skan					       complain);
764252284Sobrien      }
764318334Speter    case FUNCTION_TYPE:
764418334Speter    case METHOD_TYPE:
764518334Speter      {
764650397Sobrien	tree fntype;
7647169689Skan	tree specs;
764852284Sobrien	fntype = tsubst_function_type (t, args, complain, in_decl);
764952284Sobrien	if (fntype == error_mark_node)
765052284Sobrien	  return error_mark_node;
765150397Sobrien
7652132718Skan	/* Substitute the exception specification.  */
7653169689Skan	specs = tsubst_exception_specification (t, args, complain,
7654169689Skan						in_decl);
7655169689Skan	if (specs == error_mark_node)
7656169689Skan	  return error_mark_node;
7657169689Skan	if (specs)
7658169689Skan	  fntype = build_exception_variant (fntype, specs);
765950397Sobrien	return fntype;
766018334Speter      }
766118334Speter    case ARRAY_TYPE:
766218334Speter      {
766352284Sobrien	tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
766452284Sobrien	if (domain == error_mark_node)
766552284Sobrien	  return error_mark_node;
766652284Sobrien
766752284Sobrien	/* As an optimization, we avoid regenerating the array type if
766852284Sobrien	   it will obviously be the same as T.  */
766918334Speter	if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
767018334Speter	  return t;
767152284Sobrien
7672169689Skan	/* These checks should match the ones in grokdeclarator.
767352284Sobrien
7674169689Skan	   [temp.deduct]
767552284Sobrien
7676169689Skan	   The deduction may fail for any of the following reasons:
7677169689Skan
767852284Sobrien	   -- Attempting to create an array with an element type that
7679169689Skan	      is void, a function type, or a reference type, or [DR337]
7680132718Skan	      an abstract class type.  */
7681169689Skan	if (TREE_CODE (type) == VOID_TYPE
768252284Sobrien	    || TREE_CODE (type) == FUNCTION_TYPE
768352284Sobrien	    || TREE_CODE (type) == REFERENCE_TYPE)
768452284Sobrien	  {
768596263Sobrien	    if (complain & tf_error)
7686169689Skan	      error ("creating array of %qT", type);
768752284Sobrien	    return error_mark_node;
768852284Sobrien	  }
7689132718Skan	if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7690132718Skan	  {
7691132718Skan	    if (complain & tf_error)
7692169689Skan	      error ("creating array of %qT, which is an abstract class type",
7693132718Skan		     type);
7694169689Skan	    return error_mark_node;
7695132718Skan	  }
769652284Sobrien
769718334Speter	r = build_cplus_array_type (type, domain);
769818334Speter	return r;
769918334Speter      }
770018334Speter
770150397Sobrien    case PLUS_EXPR:
770218334Speter    case MINUS_EXPR:
770352284Sobrien      {
770496263Sobrien	tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
770596263Sobrien	tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
770618334Speter
770752284Sobrien	if (e1 == error_mark_node || e2 == error_mark_node)
770852284Sobrien	  return error_mark_node;
770952284Sobrien
7710169689Skan	return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
771152284Sobrien      }
771252284Sobrien
771318334Speter    case NEGATE_EXPR:
771418334Speter    case NOP_EXPR:
771552284Sobrien      {
771696263Sobrien	tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
771752284Sobrien	if (e == error_mark_node)
771852284Sobrien	  return error_mark_node;
771918334Speter
7720169689Skan	return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
772152284Sobrien      }
772252284Sobrien
772350397Sobrien    case TYPENAME_TYPE:
772450397Sobrien      {
772552284Sobrien	tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
772652284Sobrien				     in_decl, /*entering_scope=*/1);
772752284Sobrien	tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7728169689Skan			      complain, in_decl);
772952284Sobrien
773052284Sobrien	if (ctx == error_mark_node || f == error_mark_node)
773152284Sobrien	  return error_mark_node;
773252284Sobrien
773352284Sobrien	if (!IS_AGGR_TYPE (ctx))
773452284Sobrien	  {
773596263Sobrien	    if (complain & tf_error)
7736169689Skan	      error ("%qT is not a class, struct, or union type", ctx);
773752284Sobrien	    return error_mark_node;
773852284Sobrien	  }
773952284Sobrien	else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
774052284Sobrien	  {
774152284Sobrien	    /* Normally, make_typename_type does not require that the CTX
774252284Sobrien	       have complete type in order to allow things like:
774352284Sobrien
7744169689Skan		 template <class T> struct S { typename S<T>::X Y; };
7745169689Skan
774652284Sobrien	       But, such constructs have already been resolved by this
774752284Sobrien	       point, so here CTX really should have complete type, unless
774852284Sobrien	       it's a partial instantiation.  */
774952284Sobrien	    ctx = complete_type (ctx);
775090075Sobrien	    if (!COMPLETE_TYPE_P (ctx))
775152284Sobrien	      {
775296263Sobrien		if (complain & tf_error)
7753117395Skan		  cxx_incomplete_type_error (NULL_TREE, ctx);
775452284Sobrien		return error_mark_node;
775552284Sobrien	      }
775652284Sobrien	  }
775752284Sobrien
7758169689Skan	f = make_typename_type (ctx, f, typename_type,
775996263Sobrien				(complain & tf_error) | tf_keep_type_decl);
776052284Sobrien	if (f == error_mark_node)
776152284Sobrien	  return f;
7762169689Skan	if (TREE_CODE (f) == TYPE_DECL)
7763169689Skan	  {
776496263Sobrien	    complain |= tf_ignore_bad_quals;
7765169689Skan	    f = TREE_TYPE (f);
7766169689Skan	  }
7767169689Skan
7768169689Skan	if (TREE_CODE (f) != TYPENAME_TYPE)
7769169689Skan	  {
7770169689Skan	    if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7771169689Skan	      error ("%qT resolves to %qT, which is not an enumeration type",
7772169689Skan		     t, f);
7773169689Skan	    else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7774169689Skan	      error ("%qT resolves to %qT, which is is not a class type",
7775169689Skan		     t, f);
7776169689Skan	  }
7777169689Skan
7778169689Skan	return cp_build_qualified_type_real
7779169689Skan	  (f, cp_type_quals (f) | cp_type_quals (t), complain);
778050397Sobrien      }
7781169689Skan
778290075Sobrien    case UNBOUND_CLASS_TEMPLATE:
778390075Sobrien      {
778490075Sobrien	tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
778590075Sobrien				     in_decl, /*entering_scope=*/1);
778690075Sobrien	tree name = TYPE_IDENTIFIER (t);
7787169689Skan	tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
778890075Sobrien
778990075Sobrien	if (ctx == error_mark_node || name == error_mark_node)
779090075Sobrien	  return error_mark_node;
779190075Sobrien
7792169689Skan	if (parm_list)
7793169689Skan	  parm_list = tsubst_template_parms (parm_list, args, complain);
7794169689Skan	return make_unbound_class_template (ctx, name, parm_list, complain);
779590075Sobrien      }
779690075Sobrien
779750397Sobrien    case INDIRECT_REF:
779850397Sobrien    case ADDR_EXPR:
7799169689Skan    case CALL_EXPR:
7800169689Skan      gcc_unreachable ();
780150397Sobrien
780250397Sobrien    case ARRAY_REF:
780352284Sobrien      {
780496263Sobrien	tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7805169689Skan	tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7806169689Skan			       /*integral_constant_expression_p=*/false);
780752284Sobrien	if (e1 == error_mark_node || e2 == error_mark_node)
780852284Sobrien	  return error_mark_node;
780950397Sobrien
7810169689Skan	return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
781152284Sobrien      }
781252284Sobrien
781350397Sobrien    case SCOPE_REF:
781452284Sobrien      {
781596263Sobrien	tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
781652284Sobrien	tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
781752284Sobrien	if (e1 == error_mark_node || e2 == error_mark_node)
781852284Sobrien	  return error_mark_node;
781950397Sobrien
7820169689Skan	return build_qualified_name (/*type=*/NULL_TREE,
7821169689Skan				     e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
782252284Sobrien      }
782352284Sobrien
782452284Sobrien    case TYPEOF_TYPE:
782552284Sobrien      {
7826132718Skan	tree type;
782752284Sobrien
7828169689Skan	type = finish_typeof (tsubst_expr
7829169689Skan			      (TYPEOF_TYPE_EXPR (t), args,
7830169689Skan			       complain, in_decl,
7831169689Skan			       /*integral_constant_expression_p=*/false));
7832132718Skan	return cp_build_qualified_type_real (type,
7833117395Skan					     cp_type_quals (t)
7834132718Skan					     | cp_type_quals (type),
7835117395Skan					     complain);
783652284Sobrien      }
783752284Sobrien
7838261188Spfg      /* APPLE LOCAL begin blocks 6204446 */
7839261188Spfg    case BLOCK_POINTER_TYPE:
7840261188Spfg      return t;
7841261188Spfg      /* APPLE LOCAL end blocks 6204446 */
7842261188Spfg
784318334Speter    default:
7844169689Skan      sorry ("use of %qs in template",
784518334Speter	     tree_code_name [(int) TREE_CODE (t)]);
784618334Speter      return error_mark_node;
784718334Speter    }
784818334Speter}
784918334Speter
7850132718Skan/* Like tsubst_expr for a BASELINK.  OBJECT_TYPE, if non-NULL, is the
7851132718Skan   type of the expression on the left-hand side of the "." or "->"
7852132718Skan   operator.  */
7853132718Skan
7854132718Skanstatic tree
7855132718Skantsubst_baselink (tree baselink, tree object_type,
7856132718Skan		 tree args, tsubst_flags_t complain, tree in_decl)
7857132718Skan{
7858132718Skan    tree name;
7859132718Skan    tree qualifying_scope;
7860132718Skan    tree fns;
7861169689Skan    tree optype;
7862132718Skan    tree template_args = 0;
7863132718Skan    bool template_id_p = false;
7864132718Skan
7865169689Skan    /* A baselink indicates a function from a base class.  Both the
7866169689Skan       BASELINK_ACCESS_BINFO and the base class referenced may
7867169689Skan       indicate bases of the template class, rather than the
7868169689Skan       instantiated class.  In addition, lookups that were not
7869169689Skan       ambiguous before may be ambiguous now.  Therefore, we perform
7870169689Skan       the lookup again.  */
7871132718Skan    qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7872169689Skan    qualifying_scope = tsubst (qualifying_scope, args,
7873169689Skan			       complain, in_decl);
7874132718Skan    fns = BASELINK_FUNCTIONS (baselink);
7875169689Skan    optype = BASELINK_OPTYPE (baselink);
7876132718Skan    if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7877132718Skan      {
7878132718Skan	template_id_p = true;
7879132718Skan	template_args = TREE_OPERAND (fns, 1);
7880132718Skan	fns = TREE_OPERAND (fns, 0);
7881132718Skan	if (template_args)
7882132718Skan	  template_args = tsubst_template_args (template_args, args,
7883132718Skan						complain, in_decl);
7884132718Skan      }
7885132718Skan    name = DECL_NAME (get_first_fn (fns));
7886132718Skan    baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7887169689Skan
7888169689Skan    /* If lookup found a single function, mark it as used at this
7889169689Skan       point.  (If it lookup found multiple functions the one selected
7890169689Skan       later by overload resolution will be marked as used at that
7891169689Skan       point.)  */
7892169689Skan    if (BASELINK_P (baselink))
7893169689Skan      fns = BASELINK_FUNCTIONS (baselink);
7894169689Skan    if (!template_id_p && !really_overloaded_fn (fns))
7895169689Skan      mark_used (OVL_CURRENT (fns));
7896169689Skan
7897169689Skan    /* Add back the template arguments, if present.  */
7898132718Skan    if (BASELINK_P (baselink) && template_id_p)
7899169689Skan      BASELINK_FUNCTIONS (baselink)
7900132718Skan	= build_nt (TEMPLATE_ID_EXPR,
7901132718Skan		    BASELINK_FUNCTIONS (baselink),
7902132718Skan		    template_args);
7903169689Skan    /* Update the conversion operator type.  */
7904169689Skan    BASELINK_OPTYPE (baselink)
7905169689Skan      = tsubst (optype, args, complain, in_decl);
7906169689Skan
7907132718Skan    if (!object_type)
7908132718Skan      object_type = current_class_type;
7909169689Skan    return adjust_result_of_qualified_name_lookup (baselink,
7910132718Skan						   qualifying_scope,
7911132718Skan						   object_type);
7912132718Skan}
7913132718Skan
7914132718Skan/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
7915132718Skan   true if the qualified-id will be a postfix-expression in-and-of
7916132718Skan   itself; false if more of the postfix-expression follows the
7917132718Skan   QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
7918132718Skan   of "&".  */
7919132718Skan
7920132718Skanstatic tree
7921169689Skantsubst_qualified_id (tree qualified_id, tree args,
7922132718Skan		     tsubst_flags_t complain, tree in_decl,
7923132718Skan		     bool done, bool address_p)
7924132718Skan{
7925132718Skan  tree expr;
7926132718Skan  tree scope;
7927132718Skan  tree name;
7928132718Skan  bool is_template;
7929132718Skan  tree template_args;
7930132718Skan
7931169689Skan  gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7932132718Skan
7933132718Skan  /* Figure out what name to look up.  */
7934132718Skan  name = TREE_OPERAND (qualified_id, 1);
7935132718Skan  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7936132718Skan    {
7937132718Skan      is_template = true;
7938132718Skan      template_args = TREE_OPERAND (name, 1);
7939132718Skan      if (template_args)
7940132718Skan	template_args = tsubst_template_args (template_args, args,
7941132718Skan					      complain, in_decl);
7942132718Skan      name = TREE_OPERAND (name, 0);
7943132718Skan    }
7944132718Skan  else
7945132718Skan    {
7946132718Skan      is_template = false;
7947132718Skan      template_args = NULL_TREE;
7948132718Skan    }
7949132718Skan
7950132718Skan  /* Substitute into the qualifying scope.  When there are no ARGS, we
7951132718Skan     are just trying to simplify a non-dependent expression.  In that
7952132718Skan     case the qualifying scope may be dependent, and, in any case,
7953132718Skan     substituting will not help.  */
7954132718Skan  scope = TREE_OPERAND (qualified_id, 0);
7955132718Skan  if (args)
7956132718Skan    {
7957132718Skan      scope = tsubst (scope, args, complain, in_decl);
7958132718Skan      expr = tsubst_copy (name, args, complain, in_decl);
7959132718Skan    }
7960132718Skan  else
7961132718Skan    expr = name;
7962132718Skan
7963132718Skan  if (dependent_type_p (scope))
7964169689Skan    return build_qualified_name (/*type=*/NULL_TREE,
7965169689Skan				 scope, expr,
7966169689Skan				 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7967169689Skan
7968132718Skan  if (!BASELINK_P (name) && !DECL_P (expr))
7969132718Skan    {
7970169689Skan      if (TREE_CODE (expr) == BIT_NOT_EXPR)
7971169689Skan	/* If this were actually a destructor call, it would have been
7972169689Skan	   parsed as such by the parser.  */
7973169689Skan	expr = error_mark_node;
7974169689Skan      else
7975169689Skan	expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7976132718Skan      if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7977132718Skan		     ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7978132718Skan	{
7979132718Skan	  if (complain & tf_error)
7980132718Skan	    {
7981169689Skan	      error ("dependent-name %qE is parsed as a non-type, but "
7982132718Skan		     "instantiation yields a type", qualified_id);
7983169689Skan	      inform ("say %<typename %E%> if a type is meant", qualified_id);
7984132718Skan	    }
7985132718Skan	  return error_mark_node;
7986132718Skan	}
7987132718Skan    }
7988169689Skan
7989132718Skan  if (DECL_P (expr))
7990169689Skan    {
7991169689Skan      check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7992169689Skan					   scope);
7993169689Skan      /* Remember that there was a reference to this entity.  */
7994169689Skan      mark_used (expr);
7995169689Skan    }
7996132718Skan
7997169689Skan  if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7998169689Skan    {
7999169689Skan      if (complain & tf_error)
8000169689Skan	qualified_name_lookup_error (scope,
8001169689Skan				     TREE_OPERAND (qualified_id, 1),
8002169689Skan				     expr);
8003169689Skan      return error_mark_node;
8004169689Skan    }
8005169689Skan
8006132718Skan  if (is_template)
8007132718Skan    expr = lookup_template_function (expr, template_args);
8008132718Skan
8009132718Skan  if (expr == error_mark_node && complain & tf_error)
8010169689Skan    qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
8011169689Skan				 expr);
8012132718Skan  else if (TYPE_P (scope))
8013132718Skan    {
8014169689Skan      expr = (adjust_result_of_qualified_name_lookup
8015132718Skan	      (expr, scope, current_class_type));
8016169689Skan      expr = (finish_qualified_id_expr
8017169689Skan	      (scope, expr, done, address_p,
8018169689Skan	       QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
8019169689Skan	       /*template_arg_p=*/false));
8020132718Skan    }
8021132718Skan
8022169689Skan  /* Expressions do not generally have reference type.  */
8023169689Skan  if (TREE_CODE (expr) != SCOPE_REF
8024169689Skan      /* However, if we're about to form a pointer-to-member, we just
8025169689Skan	 want the referenced member referenced.  */
8026169689Skan      && TREE_CODE (expr) != OFFSET_REF)
8027169689Skan    expr = convert_from_reference (expr);
8028169689Skan
8029132718Skan  return expr;
8030132718Skan}
8031132718Skan
803250397Sobrien/* Like tsubst, but deals with expressions.  This function just replaces
803350397Sobrien   template parms; to finish processing the resultant expression, use
803450397Sobrien   tsubst_expr.  */
803518334Speter
8036132718Skanstatic tree
8037132718Skantsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
803850397Sobrien{
803950397Sobrien  enum tree_code code;
804052284Sobrien  tree r;
804118334Speter
804250397Sobrien  if (t == NULL_TREE || t == error_mark_node)
804350397Sobrien    return t;
804450397Sobrien
804550397Sobrien  code = TREE_CODE (t);
804650397Sobrien
804750397Sobrien  switch (code)
804818334Speter    {
804950397Sobrien    case PARM_DECL:
8050132718Skan      r = retrieve_local_specialization (t);
8051169689Skan      gcc_assert (r != NULL);
8052132718Skan      mark_used (r);
8053132718Skan      return r;
805418334Speter
805550397Sobrien    case CONST_DECL:
805652284Sobrien      {
805752284Sobrien	tree enum_type;
805852284Sobrien	tree v;
805952284Sobrien
8060132718Skan	if (DECL_TEMPLATE_PARM_P (t))
8061132718Skan	  return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
8062132718Skan	/* There is no need to substitute into namespace-scope
8063132718Skan	   enumerators.  */
8064132718Skan	if (DECL_NAMESPACE_SCOPE_P (t))
806552284Sobrien	  return t;
8066132718Skan	/* If ARGS is NULL, then T is known to be non-dependent.  */
8067132718Skan	if (args == NULL_TREE)
8068169689Skan	  return integral_constant_value (t);
806952284Sobrien
807052284Sobrien	/* Unfortunately, we cannot just call lookup_name here.
807190075Sobrien	   Consider:
8072169689Skan
807390075Sobrien	     template <int I> int f() {
807490075Sobrien	     enum E { a = I };
807590075Sobrien	     struct S { void g() { E e = a; } };
807690075Sobrien	     };
8077169689Skan
807890075Sobrien	   When we instantiate f<7>::S::g(), say, lookup_name is not
807990075Sobrien	   clever enough to find f<7>::a.  */
8080169689Skan	enum_type
8081169689Skan	  = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
808252284Sobrien			      /*entering_scope=*/0);
808352284Sobrien
8084169689Skan	for (v = TYPE_VALUES (enum_type);
8085169689Skan	     v != NULL_TREE;
808652284Sobrien	     v = TREE_CHAIN (v))
808752284Sobrien	  if (TREE_PURPOSE (v) == DECL_NAME (t))
808852284Sobrien	    return TREE_VALUE (v);
808952284Sobrien
809052284Sobrien	  /* We didn't find the name.  That should never happen; if
809152284Sobrien	     name-lookup found it during preliminary parsing, we
809252284Sobrien	     should find it again here during instantiation.  */
8093169689Skan	gcc_unreachable ();
809452284Sobrien      }
809552284Sobrien      return t;
809652284Sobrien
809750397Sobrien    case FIELD_DECL:
809850397Sobrien      if (DECL_CONTEXT (t))
809950397Sobrien	{
810050397Sobrien	  tree ctx;
810118334Speter
810252284Sobrien	  ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
810352284Sobrien				  /*entering_scope=*/1);
810450397Sobrien	  if (ctx != DECL_CONTEXT (t))
8105146895Skan	    {
8106146895Skan	      tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
8107146895Skan	      if (!r)
8108146895Skan		{
8109146895Skan		  if (complain & tf_error)
8110169689Skan		    error ("using invalid field %qD", t);
8111146895Skan		  return error_mark_node;
8112146895Skan		}
8113146895Skan	      return r;
8114146895Skan	    }
811550397Sobrien	}
8116169689Skan
811750397Sobrien      return t;
811818334Speter
811950397Sobrien    case VAR_DECL:
812050397Sobrien    case FUNCTION_DECL:
8121132718Skan      if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8122132718Skan	  || local_variable_p (t))
812352284Sobrien	t = tsubst (t, args, complain, in_decl);
812450397Sobrien      mark_used (t);
812550397Sobrien      return t;
812618334Speter
8127132718Skan    case BASELINK:
8128132718Skan      return tsubst_baselink (t, current_class_type, args, complain, in_decl);
8129132718Skan
813050397Sobrien    case TEMPLATE_DECL:
8131132718Skan      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8132169689Skan	return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
8133132718Skan		       args, complain, in_decl);
8134169689Skan      else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
813552284Sobrien	return tsubst (t, args, complain, in_decl);
8136132718Skan      else if (DECL_CLASS_SCOPE_P (t)
8137132718Skan	       && uses_template_parms (DECL_CONTEXT (t)))
8138132718Skan	{
8139132718Skan	  /* Template template argument like the following example need
8140132718Skan	     special treatment:
814118334Speter
8142132718Skan	       template <template <class> class TT> struct C {};
8143132718Skan	       template <class T> struct D {
8144132718Skan		 template <class U> struct E {};
8145169689Skan		 C<E> c;				// #1
8146132718Skan	       };
8147132718Skan	       D<int> d;				// #2
814852284Sobrien
8149132718Skan	     We are processing the template argument `E' in #1 for
8150132718Skan	     the template instantiation #2.  Originally, `E' is a
8151132718Skan	     TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
8152132718Skan	     have to substitute this with one having context `D<int>'.  */
815352284Sobrien
8154132718Skan	  tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8155132718Skan	  return lookup_field (context, DECL_NAME(t), 0, false);
8156132718Skan	}
8157132718Skan      else
8158132718Skan	/* Ordinary template template argument.  */
815952284Sobrien	return t;
816052284Sobrien
816150397Sobrien    case CAST_EXPR:
816250397Sobrien    case REINTERPRET_CAST_EXPR:
816350397Sobrien    case CONST_CAST_EXPR:
816450397Sobrien    case STATIC_CAST_EXPR:
816550397Sobrien    case DYNAMIC_CAST_EXPR:
816652284Sobrien    case NOP_EXPR:
816750397Sobrien      return build1
816852284Sobrien	(code, tsubst (TREE_TYPE (t), args, complain, in_decl),
816952284Sobrien	 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
817018334Speter
817150397Sobrien    case INDIRECT_REF:
817250397Sobrien    case NEGATE_EXPR:
817350397Sobrien    case TRUTH_NOT_EXPR:
817450397Sobrien    case BIT_NOT_EXPR:
817550397Sobrien    case ADDR_EXPR:
8176169689Skan    case UNARY_PLUS_EXPR:      /* Unary + */
817750397Sobrien    case SIZEOF_EXPR:
817850397Sobrien    case ALIGNOF_EXPR:
817950397Sobrien    case ARROW_EXPR:
818050397Sobrien    case THROW_EXPR:
818150397Sobrien    case TYPEID_EXPR:
818290075Sobrien    case REALPART_EXPR:
818390075Sobrien    case IMAGPART_EXPR:
818450397Sobrien      return build1
818552284Sobrien	(code, tsubst (TREE_TYPE (t), args, complain, in_decl),
818652284Sobrien	 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
818718334Speter
8188132718Skan    case COMPONENT_REF:
8189132718Skan      {
8190132718Skan	tree object;
8191132718Skan	tree name;
8192132718Skan
8193132718Skan	object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8194132718Skan	name = TREE_OPERAND (t, 1);
8195169689Skan	if (TREE_CODE (name) == BIT_NOT_EXPR)
8196132718Skan	  {
8197132718Skan	    name = tsubst_copy (TREE_OPERAND (name, 0), args,
8198132718Skan				complain, in_decl);
8199132718Skan	    name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8200132718Skan	  }
8201132718Skan	else if (TREE_CODE (name) == SCOPE_REF
8202132718Skan		 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8203132718Skan	  {
8204132718Skan	    tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8205132718Skan				     complain, in_decl);
8206132718Skan	    name = TREE_OPERAND (name, 1);
8207132718Skan	    name = tsubst_copy (TREE_OPERAND (name, 0), args,
8208132718Skan				complain, in_decl);
8209132718Skan	    name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8210169689Skan	    name = build_qualified_name (/*type=*/NULL_TREE,
8211169689Skan					 base, name,
8212169689Skan					 /*template_p=*/false);
8213132718Skan	  }
8214132718Skan	else if (TREE_CODE (name) == BASELINK)
8215169689Skan	  name = tsubst_baselink (name,
8216169689Skan				  non_reference (TREE_TYPE (object)),
8217169689Skan				  args, complain,
8218132718Skan				  in_decl);
8219132718Skan	else
8220132718Skan	  name = tsubst_copy (name, args, complain, in_decl);
8221169689Skan	return build_nt (COMPONENT_REF, object, name, NULL_TREE);
8222132718Skan      }
8223132718Skan
822450397Sobrien    case PLUS_EXPR:
822550397Sobrien    case MINUS_EXPR:
822650397Sobrien    case MULT_EXPR:
822750397Sobrien    case TRUNC_DIV_EXPR:
822850397Sobrien    case CEIL_DIV_EXPR:
822950397Sobrien    case FLOOR_DIV_EXPR:
823050397Sobrien    case ROUND_DIV_EXPR:
823150397Sobrien    case EXACT_DIV_EXPR:
823250397Sobrien    case BIT_AND_EXPR:
823350397Sobrien    case BIT_IOR_EXPR:
823450397Sobrien    case BIT_XOR_EXPR:
823550397Sobrien    case TRUNC_MOD_EXPR:
823650397Sobrien    case FLOOR_MOD_EXPR:
823750397Sobrien    case TRUTH_ANDIF_EXPR:
823850397Sobrien    case TRUTH_ORIF_EXPR:
823950397Sobrien    case TRUTH_AND_EXPR:
824050397Sobrien    case TRUTH_OR_EXPR:
824150397Sobrien    case RSHIFT_EXPR:
824250397Sobrien    case LSHIFT_EXPR:
824350397Sobrien    case RROTATE_EXPR:
824450397Sobrien    case LROTATE_EXPR:
824550397Sobrien    case EQ_EXPR:
824650397Sobrien    case NE_EXPR:
824750397Sobrien    case MAX_EXPR:
824850397Sobrien    case MIN_EXPR:
824950397Sobrien    case LE_EXPR:
825050397Sobrien    case GE_EXPR:
825150397Sobrien    case LT_EXPR:
825250397Sobrien    case GT_EXPR:
825350397Sobrien    case COMPOUND_EXPR:
825450397Sobrien    case DOTSTAR_EXPR:
825550397Sobrien    case MEMBER_REF:
825690075Sobrien    case PREDECREMENT_EXPR:
825790075Sobrien    case PREINCREMENT_EXPR:
825890075Sobrien    case POSTDECREMENT_EXPR:
825990075Sobrien    case POSTINCREMENT_EXPR:
826050397Sobrien      return build_nt
826152284Sobrien	(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
826252284Sobrien	 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
826350397Sobrien
8264169689Skan    case SCOPE_REF:
8265169689Skan      return build_qualified_name (/*type=*/NULL_TREE,
8266169689Skan				   tsubst_copy (TREE_OPERAND (t, 0),
8267169689Skan						args, complain, in_decl),
8268169689Skan				   tsubst_copy (TREE_OPERAND (t, 1),
8269169689Skan						args, complain, in_decl),
8270169689Skan				   QUALIFIED_NAME_IS_TEMPLATE (t));
8271169689Skan
8272169689Skan    case ARRAY_REF:
8273169689Skan      return build_nt
8274169689Skan	(ARRAY_REF,
8275169689Skan	 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8276169689Skan	 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8277169689Skan	 NULL_TREE, NULL_TREE);
8278169689Skan
827950397Sobrien    case CALL_EXPR:
8280169689Skan      return build_nt (code,
8281132718Skan		       tsubst_copy (TREE_OPERAND (t, 0), args,
828252284Sobrien				    complain, in_decl),
8283132718Skan		       tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8284132718Skan				    in_decl),
8285132718Skan		       NULL_TREE);
828650397Sobrien
828750397Sobrien    case COND_EXPR:
828850397Sobrien    case MODOP_EXPR:
828990075Sobrien    case PSEUDO_DTOR_EXPR:
829050397Sobrien      {
829152284Sobrien	r = build_nt
829252284Sobrien	  (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
829352284Sobrien	   tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
829452284Sobrien	   tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8295169689Skan	TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
829650397Sobrien	return r;
829750397Sobrien      }
829850397Sobrien
829950397Sobrien    case NEW_EXPR:
830050397Sobrien      {
830152284Sobrien	r = build_nt
830252284Sobrien	(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
830352284Sobrien	 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
830452284Sobrien	 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
830550397Sobrien	NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
830650397Sobrien	return r;
830750397Sobrien      }
830850397Sobrien
830950397Sobrien    case DELETE_EXPR:
831050397Sobrien      {
831152284Sobrien	r = build_nt
831252284Sobrien	(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
831352284Sobrien	 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
831450397Sobrien	DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
831550397Sobrien	DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
831650397Sobrien	return r;
831750397Sobrien      }
831850397Sobrien
831950397Sobrien    case TEMPLATE_ID_EXPR:
832050397Sobrien      {
8321169689Skan	/* Substituted template arguments */
8322132718Skan	tree fn = TREE_OPERAND (t, 0);
8323132718Skan	tree targs = TREE_OPERAND (t, 1);
832450397Sobrien
8325132718Skan	fn = tsubst_copy (fn, args, complain, in_decl);
8326132718Skan	if (targs)
8327132718Skan	  targs = tsubst_template_args (targs, args, complain, in_decl);
8328169689Skan
8329132718Skan	return lookup_template_function (fn, targs);
833050397Sobrien      }
833150397Sobrien
833250397Sobrien    case TREE_LIST:
833350397Sobrien      {
833450397Sobrien	tree purpose, value, chain;
833550397Sobrien
833650397Sobrien	if (t == void_list_node)
833750397Sobrien	  return t;
833850397Sobrien
833950397Sobrien	purpose = TREE_PURPOSE (t);
834050397Sobrien	if (purpose)
834152284Sobrien	  purpose = tsubst_copy (purpose, args, complain, in_decl);
834250397Sobrien	value = TREE_VALUE (t);
834350397Sobrien	if (value)
834452284Sobrien	  value = tsubst_copy (value, args, complain, in_decl);
834550397Sobrien	chain = TREE_CHAIN (t);
834650397Sobrien	if (chain && chain != void_type_node)
834752284Sobrien	  chain = tsubst_copy (chain, args, complain, in_decl);
834850397Sobrien	if (purpose == TREE_PURPOSE (t)
834950397Sobrien	    && value == TREE_VALUE (t)
835050397Sobrien	    && chain == TREE_CHAIN (t))
835150397Sobrien	  return t;
835250397Sobrien	return tree_cons (purpose, value, chain);
835350397Sobrien      }
835450397Sobrien
835550397Sobrien    case RECORD_TYPE:
835650397Sobrien    case UNION_TYPE:
835750397Sobrien    case ENUMERAL_TYPE:
835850397Sobrien    case INTEGER_TYPE:
835950397Sobrien    case TEMPLATE_TYPE_PARM:
836050397Sobrien    case TEMPLATE_TEMPLATE_PARM:
836190075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
836250397Sobrien    case TEMPLATE_PARM_INDEX:
836350397Sobrien    case POINTER_TYPE:
836450397Sobrien    case REFERENCE_TYPE:
836550397Sobrien    case OFFSET_TYPE:
836650397Sobrien    case FUNCTION_TYPE:
836750397Sobrien    case METHOD_TYPE:
836850397Sobrien    case ARRAY_TYPE:
836950397Sobrien    case TYPENAME_TYPE:
837090075Sobrien    case UNBOUND_CLASS_TEMPLATE:
837190075Sobrien    case TYPEOF_TYPE:
837250397Sobrien    case TYPE_DECL:
837352284Sobrien      return tsubst (t, args, complain, in_decl);
837450397Sobrien
837550397Sobrien    case IDENTIFIER_NODE:
837690075Sobrien      if (IDENTIFIER_TYPENAME_P (t))
837790075Sobrien	{
837890075Sobrien	  tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
837990075Sobrien	  return mangle_conv_op_name_for_type (new_type);
838090075Sobrien	}
838150397Sobrien      else
838250397Sobrien	return t;
838350397Sobrien
838450397Sobrien    case CONSTRUCTOR:
8385169689Skan      /* This is handled by tsubst_copy_and_build.  */
8386169689Skan      gcc_unreachable ();
838750397Sobrien
838890075Sobrien    case VA_ARG_EXPR:
838990075Sobrien      return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
839096263Sobrien					  in_decl),
839196263Sobrien			     tsubst (TREE_TYPE (t), args, complain, in_decl));
839290075Sobrien
8393169689Skan    case CLEANUP_POINT_EXPR:
8394169689Skan      /* We shouldn't have built any of these during initial template
8395169689Skan	 generation.  Instead, they should be built during instantiation
8396169689Skan	 in response to the saved STMT_IS_FULL_EXPR_P setting.  */
8397169689Skan      gcc_unreachable ();
8398169689Skan
8399161651Skan    case OFFSET_REF:
8400161651Skan      mark_used (TREE_OPERAND (t, 1));
8401161651Skan      return t;
8402161651Skan
840350397Sobrien    default:
840450397Sobrien      return t;
840518334Speter    }
840650397Sobrien}
840718334Speter
8408169689Skan/* Like tsubst_copy, but specifically for OpenMP clauses.  */
8409169689Skan
8410169689Skanstatic tree
8411169689Skantsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8412169689Skan		    tree in_decl)
8413169689Skan{
8414169689Skan  tree new_clauses = NULL, nc, oc;
8415169689Skan
8416169689Skan  for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8417169689Skan    {
8418169689Skan      nc = copy_node (oc);
8419169689Skan      OMP_CLAUSE_CHAIN (nc) = new_clauses;
8420169689Skan      new_clauses = nc;
8421169689Skan
8422169689Skan      switch (OMP_CLAUSE_CODE (nc))
8423169689Skan	{
8424169689Skan	case OMP_CLAUSE_PRIVATE:
8425169689Skan	case OMP_CLAUSE_SHARED:
8426169689Skan	case OMP_CLAUSE_FIRSTPRIVATE:
8427169689Skan	case OMP_CLAUSE_LASTPRIVATE:
8428169689Skan	case OMP_CLAUSE_REDUCTION:
8429169689Skan	case OMP_CLAUSE_COPYIN:
8430169689Skan	case OMP_CLAUSE_COPYPRIVATE:
8431169689Skan	case OMP_CLAUSE_IF:
8432169689Skan	case OMP_CLAUSE_NUM_THREADS:
8433169689Skan	case OMP_CLAUSE_SCHEDULE:
8434169689Skan	  OMP_CLAUSE_OPERAND (nc, 0)
8435169689Skan	    = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8436169689Skan			   in_decl, /*integral_constant_expression_p=*/false);
8437169689Skan	  break;
8438169689Skan	case OMP_CLAUSE_NOWAIT:
8439169689Skan	case OMP_CLAUSE_ORDERED:
8440169689Skan	case OMP_CLAUSE_DEFAULT:
8441169689Skan	  break;
8442169689Skan	default:
8443169689Skan	  gcc_unreachable ();
8444169689Skan	}
8445169689Skan    }
8446169689Skan
8447169689Skan  return finish_omp_clauses (nreverse (new_clauses));
8448169689Skan}
8449169689Skan
8450169689Skan/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes.  */
8451169689Skan
8452169689Skanstatic tree
8453169689Skantsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8454169689Skan			  tree in_decl)
8455169689Skan{
8456169689Skan#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8457169689Skan
8458169689Skan  tree purpose, value, chain;
8459169689Skan
8460169689Skan  if (t == NULL)
8461169689Skan    return t;
8462169689Skan
8463169689Skan  if (TREE_CODE (t) != TREE_LIST)
8464169689Skan    return tsubst_copy_and_build (t, args, complain, in_decl,
8465169689Skan				  /*function_p=*/false,
8466169689Skan				  /*integral_constant_expression_p=*/false);
8467169689Skan
8468169689Skan  if (t == void_list_node)
8469169689Skan    return t;
8470169689Skan
8471169689Skan  purpose = TREE_PURPOSE (t);
8472169689Skan  if (purpose)
8473169689Skan    purpose = RECUR (purpose);
8474169689Skan  value = TREE_VALUE (t);
8475169689Skan  if (value)
8476169689Skan    value = RECUR (value);
8477169689Skan  chain = TREE_CHAIN (t);
8478169689Skan  if (chain && chain != void_type_node)
8479169689Skan    chain = RECUR (chain);
8480169689Skan  return tree_cons (purpose, value, chain);
8481169689Skan#undef RECUR
8482169689Skan}
8483169689Skan
8484132718Skan/* Like tsubst_copy for expressions, etc. but also does semantic
8485132718Skan   processing.  */
848650397Sobrien
8487132718Skanstatic tree
8488169689Skantsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8489169689Skan	     bool integral_constant_expression_p)
849050397Sobrien{
8491169689Skan#define RECUR(NODE)				\
8492169689Skan  tsubst_expr ((NODE), args, complain, in_decl,	\
8493169689Skan	       integral_constant_expression_p)
8494169689Skan
849590075Sobrien  tree stmt, tmp;
849690075Sobrien
849750397Sobrien  if (t == NULL_TREE || t == error_mark_node)
849850397Sobrien    return t;
849950397Sobrien
8500169689Skan  if (EXPR_HAS_LOCATION (t))
8501169689Skan    input_location = EXPR_LOCATION (t);
8502169689Skan  if (STATEMENT_CODE_P (TREE_CODE (t)))
8503169689Skan    current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8504169689Skan
850550397Sobrien  switch (TREE_CODE (t))
850618334Speter    {
8507169689Skan    case STATEMENT_LIST:
8508169689Skan      {
8509169689Skan	tree_stmt_iterator i;
8510169689Skan	for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8511169689Skan	  RECUR (tsi_stmt (i));
8512169689Skan	break;
8513169689Skan      }
8514169689Skan
851590075Sobrien    case CTOR_INITIALIZER:
8516169689Skan      finish_mem_initializers (tsubst_initializer_list
8517117395Skan			       (TREE_OPERAND (t, 0), args));
8518117395Skan      break;
851990075Sobrien
8520169689Skan    case RETURN_EXPR:
8521169689Skan      finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
852250397Sobrien      break;
852350397Sobrien
852450397Sobrien    case EXPR_STMT:
8525169689Skan      tmp = RECUR (EXPR_STMT_EXPR (t));
8526169689Skan      if (EXPR_STMT_STMT_EXPR_RESULT (t))
8527169689Skan	finish_stmt_expr_expr (tmp, cur_stmt_expr);
8528169689Skan      else
8529169689Skan	finish_expr_stmt (tmp);
8530169689Skan      break;
853150397Sobrien
853290075Sobrien    case USING_STMT:
8533169689Skan      do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
853490075Sobrien      break;
8535169689Skan
8536169689Skan    case DECL_EXPR:
853718334Speter      {
853890075Sobrien	tree decl;
853990075Sobrien	tree init;
854018334Speter
8541169689Skan	decl = DECL_EXPR_DECL (t);
854290075Sobrien	if (TREE_CODE (decl) == LABEL_DECL)
854390075Sobrien	  finish_label_decl (DECL_NAME (decl));
854490075Sobrien	else if (TREE_CODE (decl) == USING_DECL)
854590075Sobrien	  {
8546169689Skan	    tree scope = USING_DECL_SCOPE (decl);
854790075Sobrien	    tree name = DECL_NAME (decl);
8548132718Skan	    tree decl;
8549169689Skan
8550169689Skan	    scope = RECUR (scope);
8551132718Skan	    decl = lookup_qualified_name (scope, name,
8552132718Skan					  /*is_type_p=*/false,
8553132718Skan					  /*complain=*/false);
8554169689Skan	    if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8555169689Skan	      qualified_name_lookup_error (scope, name, decl);
8556132718Skan	    else
8557132718Skan	      do_local_using_decl (decl, scope, name);
855890075Sobrien	  }
855990075Sobrien	else
856090075Sobrien	  {
856190075Sobrien	    init = DECL_INITIAL (decl);
856290075Sobrien	    decl = tsubst (decl, args, complain, in_decl);
856390075Sobrien	    if (decl != error_mark_node)
856490075Sobrien	      {
8565169689Skan		/* By marking the declaration as instantiated, we avoid
8566169689Skan		   trying to instantiate it.  Since instantiate_decl can't
8567169689Skan		   handle local variables, and since we've already done
8568169689Skan		   all that needs to be done, that's the right thing to
8569169689Skan		   do.  */
8570169689Skan		if (TREE_CODE (decl) == VAR_DECL)
8571169689Skan		  DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8572104752Skan		if (TREE_CODE (decl) == VAR_DECL
8573104752Skan		    && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8574104752Skan		  /* Anonymous aggregates are a special case.  */
8575104752Skan		  finish_anon_union (decl);
8576169689Skan		else
857796263Sobrien		  {
8578104752Skan		    maybe_push_decl (decl);
8579132718Skan		    if (TREE_CODE (decl) == VAR_DECL
8580132718Skan			&& DECL_PRETTY_FUNCTION_P (decl))
8581104752Skan		      {
8582104752Skan			/* For __PRETTY_FUNCTION__ we have to adjust the
8583104752Skan			   initializer.  */
8584104752Skan			const char *const name
8585117395Skan			  = cxx_printable_name (current_function_decl, 2);
8586132718Skan			init = cp_fname_init (name, &TREE_TYPE (decl));
8587104752Skan		      }
8588104752Skan		    else
8589169689Skan		      init = RECUR (init);
8590169689Skan		    finish_decl (decl, init, NULL_TREE);
859196263Sobrien		  }
859290075Sobrien	      }
859390075Sobrien	  }
859490075Sobrien
8595169689Skan	/* A DECL_EXPR can also be used as an expression, in the condition
8596169689Skan	   clause of an if/for/while construct.  */
8597169689Skan	return decl;
859850397Sobrien      }
859918334Speter
860050397Sobrien    case FOR_STMT:
8601260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
8602260918Spfg      tmp = RECUR (FOR_ATTRIBUTES (t));
8603260918Spfg      stmt = begin_for_stmt (tmp);
8604260918Spfg      RECUR (FOR_INIT_STMT (t));
8605260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
8606169689Skan      finish_for_init_stmt (stmt);
8607169689Skan      tmp = RECUR (FOR_COND (t));
8608169689Skan      finish_for_cond (tmp, stmt);
8609169689Skan      tmp = RECUR (FOR_EXPR (t));
8610169689Skan      finish_for_expr (tmp, stmt);
8611169689Skan      RECUR (FOR_BODY (t));
8612169689Skan      finish_for_stmt (stmt);
861350397Sobrien      break;
861450397Sobrien
861550397Sobrien    case WHILE_STMT:
8616260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
8617260918Spfg      tmp = RECUR (WHILE_ATTRIBUTES (t));
8618260918Spfg      stmt = begin_while_stmt (tmp);
8619260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
8620169689Skan      tmp = RECUR (WHILE_COND (t));
8621169689Skan      finish_while_stmt_cond (tmp, stmt);
8622169689Skan      RECUR (WHILE_BODY (t));
8623169689Skan      finish_while_stmt (stmt);
862450397Sobrien      break;
862550397Sobrien
862650397Sobrien    case DO_STMT:
8627260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \
8628260918Spfg      tmp = RECUR (DO_ATTRIBUTES (t));
8629260918Spfg      stmt = begin_do_stmt (tmp);
8630260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \
8631169689Skan      RECUR (DO_BODY (t));
8632169689Skan      finish_do_body (stmt);
8633169689Skan      tmp = RECUR (DO_COND (t));
8634169689Skan      finish_do_stmt (tmp, stmt);
863550397Sobrien      break;
863650397Sobrien
863750397Sobrien    case IF_STMT:
8638169689Skan      stmt = begin_if_stmt ();
8639169689Skan      tmp = RECUR (IF_COND (t));
8640169689Skan      finish_if_stmt_cond (tmp, stmt);
8641169689Skan      RECUR (THEN_CLAUSE (t));
8642169689Skan      finish_then_clause (stmt);
864350397Sobrien
8644169689Skan      if (ELSE_CLAUSE (t))
8645169689Skan	{
8646169689Skan	  begin_else_clause (stmt);
8647169689Skan	  RECUR (ELSE_CLAUSE (t));
8648169689Skan	  finish_else_clause (stmt);
8649169689Skan	}
865018334Speter
8651169689Skan      finish_if_stmt (stmt);
865250397Sobrien      break;
865318334Speter
8654169689Skan    case BIND_EXPR:
8655169689Skan      if (BIND_EXPR_BODY_BLOCK (t))
8656169689Skan	stmt = begin_function_body ();
8657169689Skan      else
8658169689Skan	stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8659169689Skan				    ? BCS_TRY_BLOCK : 0);
866050397Sobrien
8661169689Skan      RECUR (BIND_EXPR_BODY (t));
866290075Sobrien
8663169689Skan      if (BIND_EXPR_BODY_BLOCK (t))
8664169689Skan	finish_function_body (stmt);
8665169689Skan      else
8666169689Skan	finish_compound_stmt (stmt);
866750397Sobrien      break;
866850397Sobrien
866950397Sobrien    case BREAK_STMT:
867050397Sobrien      finish_break_stmt ();
867150397Sobrien      break;
867250397Sobrien
867350397Sobrien    case CONTINUE_STMT:
867450397Sobrien      finish_continue_stmt ();
867550397Sobrien      break;
867650397Sobrien
867750397Sobrien    case SWITCH_STMT:
8678169689Skan      stmt = begin_switch_stmt ();
8679169689Skan      tmp = RECUR (SWITCH_STMT_COND (t));
8680169689Skan      finish_switch_cond (tmp, stmt);
8681169689Skan      RECUR (SWITCH_STMT_BODY (t));
8682169689Skan      finish_switch_stmt (stmt);
868350397Sobrien      break;
868450397Sobrien
8685169689Skan    case CASE_LABEL_EXPR:
8686169689Skan      finish_case_label (RECUR (CASE_LOW (t)),
8687169689Skan			 RECUR (CASE_HIGH (t)));
868850397Sobrien      break;
868950397Sobrien
8690169689Skan    case LABEL_EXPR:
8691169689Skan      finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
869250397Sobrien      break;
869350397Sobrien
8694169689Skan    case GOTO_EXPR:
869590075Sobrien      tmp = GOTO_DESTINATION (t);
869690075Sobrien      if (TREE_CODE (tmp) != LABEL_DECL)
869750397Sobrien	/* Computed goto's must be tsubst'd into.  On the other hand,
869850397Sobrien	   non-computed gotos must not be; the identifier in question
869950397Sobrien	   will have no binding.  */
8700169689Skan	tmp = RECUR (tmp);
870190075Sobrien      else
870290075Sobrien	tmp = DECL_NAME (tmp);
870390075Sobrien      finish_goto_stmt (tmp);
870450397Sobrien      break;
870550397Sobrien
8706169689Skan    case ASM_EXPR:
8707102780Skan      tmp = finish_asm_stmt
8708169689Skan	(ASM_VOLATILE_P (t),
8709169689Skan	 RECUR (ASM_STRING (t)),
8710169689Skan	 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8711169689Skan	 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8712169689Skan	 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8713169689Skan      {
8714169689Skan	tree asm_expr = tmp;
8715169689Skan	if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8716169689Skan	  asm_expr = TREE_OPERAND (asm_expr, 0);
8717169689Skan	ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8718169689Skan      }
871950397Sobrien      break;
872050397Sobrien
872150397Sobrien    case TRY_BLOCK:
872290075Sobrien      if (CLEANUP_P (t))
872350397Sobrien	{
872490075Sobrien	  stmt = begin_try_block ();
8725169689Skan	  RECUR (TRY_STMTS (t));
872690075Sobrien	  finish_cleanup_try_block (stmt);
8727169689Skan	  finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
872850397Sobrien	}
872950397Sobrien      else
873090075Sobrien	{
8731169689Skan	  tree compound_stmt = NULL_TREE;
8732169689Skan
873390075Sobrien	  if (FN_TRY_BLOCK_P (t))
8734169689Skan	    stmt = begin_function_try_block (&compound_stmt);
873590075Sobrien	  else
873690075Sobrien	    stmt = begin_try_block ();
873790075Sobrien
8738169689Skan	  RECUR (TRY_STMTS (t));
873990075Sobrien
874090075Sobrien	  if (FN_TRY_BLOCK_P (t))
874190075Sobrien	    finish_function_try_block (stmt);
874290075Sobrien	  else
874390075Sobrien	    finish_try_block (stmt);
874490075Sobrien
8745169689Skan	  RECUR (TRY_HANDLERS (t));
874690075Sobrien	  if (FN_TRY_BLOCK_P (t))
8747169689Skan	    finish_function_handler_sequence (stmt, compound_stmt);
874890075Sobrien	  else
874990075Sobrien	    finish_handler_sequence (stmt);
875090075Sobrien	}
875150397Sobrien      break;
8752169689Skan
875390075Sobrien    case HANDLER:
875490075Sobrien      {
8755169689Skan	tree decl = HANDLER_PARMS (t);
875650397Sobrien
8757169689Skan	if (decl)
875890075Sobrien	  {
875990075Sobrien	    decl = tsubst (decl, args, complain, in_decl);
876090075Sobrien	    /* Prevent instantiate_decl from trying to instantiate
876190075Sobrien	       this variable.  We've already done all that needs to be
876290075Sobrien	       done.  */
8763169689Skan	    if (decl != error_mark_node)
8764169689Skan	      DECL_TEMPLATE_INSTANTIATED (decl) = 1;
876590075Sobrien	  }
8766169689Skan	stmt = begin_handler ();
876790075Sobrien	finish_handler_parms (decl, stmt);
8768169689Skan	RECUR (HANDLER_BODY (t));
876990075Sobrien	finish_handler (stmt);
877090075Sobrien      }
877190075Sobrien      break;
877290075Sobrien
877350397Sobrien    case TAG_DEFN:
877490075Sobrien      tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
877550397Sobrien      break;
877650397Sobrien
8777169689Skan    case OMP_PARALLEL:
8778169689Skan      tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8779169689Skan				args, complain, in_decl);
8780169689Skan      stmt = begin_omp_parallel ();
8781169689Skan      RECUR (OMP_PARALLEL_BODY (t));
8782169689Skan      OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8783169689Skan	= OMP_PARALLEL_COMBINED (t);
8784169689Skan      break;
8785169689Skan
8786169689Skan    case OMP_FOR:
8787169689Skan      {
8788169689Skan	tree clauses, decl, init, cond, incr, body, pre_body;
8789169689Skan
8790169689Skan	clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8791169689Skan				      args, complain, in_decl);
8792169689Skan	init = OMP_FOR_INIT (t);
8793169689Skan	gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8794169689Skan	decl = RECUR (TREE_OPERAND (init, 0));
8795169689Skan	init = RECUR (TREE_OPERAND (init, 1));
8796169689Skan	cond = RECUR (OMP_FOR_COND (t));
8797169689Skan	incr = RECUR (OMP_FOR_INCR (t));
8798169689Skan
8799169689Skan	stmt = begin_omp_structured_block ();
8800169689Skan
8801169689Skan	pre_body = push_stmt_list ();
8802169689Skan	RECUR (OMP_FOR_PRE_BODY (t));
8803169689Skan	pre_body = pop_stmt_list (pre_body);
8804169689Skan
8805169689Skan	body = push_stmt_list ();
8806169689Skan	RECUR (OMP_FOR_BODY (t));
8807169689Skan	body = pop_stmt_list (body);
8808169689Skan
8809169689Skan	t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8810169689Skan			    pre_body);
8811169689Skan	if (t)
8812169689Skan	  OMP_FOR_CLAUSES (t) = clauses;
8813169689Skan
8814169689Skan	add_stmt (finish_omp_structured_block (stmt));
8815169689Skan      }
8816169689Skan      break;
8817169689Skan
8818169689Skan    case OMP_SECTIONS:
8819169689Skan    case OMP_SINGLE:
8820169689Skan      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8821169689Skan      stmt = push_stmt_list ();
8822169689Skan      RECUR (OMP_BODY (t));
8823169689Skan      stmt = pop_stmt_list (stmt);
8824169689Skan
8825169689Skan      t = copy_node (t);
8826169689Skan      OMP_BODY (t) = stmt;
8827169689Skan      OMP_CLAUSES (t) = tmp;
8828169689Skan      add_stmt (t);
8829169689Skan      break;
8830169689Skan
8831169689Skan    case OMP_SECTION:
8832169689Skan    case OMP_CRITICAL:
8833169689Skan    case OMP_MASTER:
8834169689Skan    case OMP_ORDERED:
8835169689Skan      stmt = push_stmt_list ();
8836169689Skan      RECUR (OMP_BODY (t));
8837169689Skan      stmt = pop_stmt_list (stmt);
8838169689Skan
8839169689Skan      t = copy_node (t);
8840169689Skan      OMP_BODY (t) = stmt;
8841169689Skan      add_stmt (t);
8842169689Skan      break;
8843169689Skan
8844169689Skan    case OMP_ATOMIC:
8845169689Skan      {
8846169689Skan	tree op0, op1;
8847169689Skan	op0 = RECUR (TREE_OPERAND (t, 0));
8848169689Skan	op1 = RECUR (TREE_OPERAND (t, 1));
8849169689Skan	finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
8850169689Skan      }
8851169689Skan      break;
8852169689Skan
885350397Sobrien    default:
8854169689Skan      gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8855169689Skan
8856169689Skan      return tsubst_copy_and_build (t, args, complain, in_decl,
8857169689Skan				    /*function_p=*/false,
8858169689Skan				    integral_constant_expression_p);
885918334Speter    }
886090075Sobrien
8861169689Skan  return NULL_TREE;
8862169689Skan#undef RECUR
886350397Sobrien}
886418334Speter
8865132718Skan/* T is a postfix-expression that is not being used in a function
8866132718Skan   call.  Return the substituted version of T.  */
8867132718Skan
8868132718Skanstatic tree
8869169689Skantsubst_non_call_postfix_expression (tree t, tree args,
8870132718Skan				    tsubst_flags_t complain,
8871132718Skan				    tree in_decl)
8872132718Skan{
8873132718Skan  if (TREE_CODE (t) == SCOPE_REF)
8874132718Skan    t = tsubst_qualified_id (t, args, complain, in_decl,
8875132718Skan			     /*done=*/false, /*address_p=*/false);
8876132718Skan  else
8877132718Skan    t = tsubst_copy_and_build (t, args, complain, in_decl,
8878169689Skan			       /*function_p=*/false,
8879169689Skan			       /*integral_constant_expression_p=*/false);
8880132718Skan
8881132718Skan  return t;
8882132718Skan}
8883132718Skan
8884132718Skan/* Like tsubst but deals with expressions and performs semantic
8885132718Skan   analysis.  FUNCTION_P is true if T is the "F" in "F (ARGS)".  */
8886132718Skan
8887132718Skantree
8888169689Skantsubst_copy_and_build (tree t,
8889169689Skan		       tree args,
8890169689Skan		       tsubst_flags_t complain,
8891169689Skan		       tree in_decl,
8892169689Skan		       bool function_p,
8893169689Skan		       bool integral_constant_expression_p)
8894132718Skan{
8895169689Skan#define RECUR(NODE)						\
8896169689Skan  tsubst_copy_and_build (NODE, args, complain, in_decl, 	\
8897169689Skan			 /*function_p=*/false,			\
8898169689Skan			 integral_constant_expression_p)
8899132718Skan
8900132718Skan  tree op1;
8901132718Skan
8902132718Skan  if (t == NULL_TREE || t == error_mark_node)
8903132718Skan    return t;
8904132718Skan
8905132718Skan  switch (TREE_CODE (t))
8906132718Skan    {
8907132718Skan    case USING_DECL:
8908132718Skan      t = DECL_NAME (t);
8909169689Skan      /* Fall through.  */
8910132718Skan    case IDENTIFIER_NODE:
8911132718Skan      {
8912132718Skan	tree decl;
8913132718Skan	cp_id_kind idk;
8914132718Skan	bool non_integral_constant_expression_p;
8915132718Skan	const char *error_msg;
8916132718Skan
8917132718Skan	if (IDENTIFIER_TYPENAME_P (t))
8918132718Skan	  {
8919132718Skan	    tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8920132718Skan	    t = mangle_conv_op_name_for_type (new_type);
8921132718Skan	  }
8922132718Skan
8923132718Skan	/* Look up the name.  */
8924169689Skan	decl = lookup_name (t);
8925132718Skan
8926132718Skan	/* By convention, expressions use ERROR_MARK_NODE to indicate
8927132718Skan	   failure, not NULL_TREE.  */
8928132718Skan	if (decl == NULL_TREE)
8929132718Skan	  decl = error_mark_node;
8930132718Skan
8931132718Skan	decl = finish_id_expression (t, decl, NULL_TREE,
8932132718Skan				     &idk,
8933169689Skan				     integral_constant_expression_p,
8934132718Skan				     /*allow_non_integral_constant_expression_p=*/false,
8935132718Skan				     &non_integral_constant_expression_p,
8936169689Skan				     /*template_p=*/false,
8937169689Skan				     /*done=*/true,
8938169689Skan				     /*address_p=*/false,
8939169689Skan				     /*template_arg_p=*/false,
8940132718Skan				     &error_msg);
8941132718Skan	if (error_msg)
8942260011Spfg	  error ("%s", error_msg);
8943132718Skan	if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8944132718Skan	  decl = unqualified_name_lookup_error (decl);
8945132718Skan	return decl;
8946132718Skan      }
8947132718Skan
8948132718Skan    case TEMPLATE_ID_EXPR:
8949132718Skan      {
8950132718Skan	tree object;
8951132718Skan	tree template = RECUR (TREE_OPERAND (t, 0));
8952132718Skan	tree targs = TREE_OPERAND (t, 1);
8953132718Skan
8954132718Skan	if (targs)
8955132718Skan	  targs = tsubst_template_args (targs, args, complain, in_decl);
8956169689Skan
8957132718Skan	if (TREE_CODE (template) == COMPONENT_REF)
8958132718Skan	  {
8959132718Skan	    object = TREE_OPERAND (template, 0);
8960132718Skan	    template = TREE_OPERAND (template, 1);
8961132718Skan	  }
8962132718Skan	else
8963132718Skan	  object = NULL_TREE;
8964132718Skan	template = lookup_template_function (template, targs);
8965169689Skan
8966132718Skan	if (object)
8967169689Skan	  return build3 (COMPONENT_REF, TREE_TYPE (template),
8968169689Skan			 object, template, NULL_TREE);
8969132718Skan	else
8970169689Skan	  return baselink_for_fns (template);
8971132718Skan      }
8972132718Skan
8973132718Skan    case INDIRECT_REF:
8974169689Skan      {
8975169689Skan	tree r = RECUR (TREE_OPERAND (t, 0));
8976132718Skan
8977169689Skan	if (REFERENCE_REF_P (t))
8978169689Skan	  {
8979169689Skan	    /* A type conversion to reference type will be enclosed in
8980169689Skan	       such an indirect ref, but the substitution of the cast
8981169689Skan	       will have also added such an indirect ref.  */
8982169689Skan	    if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8983169689Skan	      r = convert_from_reference (r);
8984169689Skan	  }
8985169689Skan	else
8986169689Skan	  r = build_x_indirect_ref (r, "unary *");
8987169689Skan	return r;
8988169689Skan      }
8989169689Skan
8990132718Skan    case NOP_EXPR:
8991132718Skan      return build_nop
8992132718Skan	(tsubst (TREE_TYPE (t), args, complain, in_decl),
8993132718Skan	 RECUR (TREE_OPERAND (t, 0)));
8994132718Skan
8995132718Skan    case CAST_EXPR:
8996132718Skan    case REINTERPRET_CAST_EXPR:
8997132718Skan    case CONST_CAST_EXPR:
8998132718Skan    case DYNAMIC_CAST_EXPR:
8999132718Skan    case STATIC_CAST_EXPR:
9000169689Skan      {
9001169689Skan	tree type;
9002169689Skan	tree op;
9003132718Skan
9004169689Skan	type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9005169689Skan	if (integral_constant_expression_p
9006169689Skan	    && !cast_valid_in_integral_constant_expression_p (type))
9007169689Skan	  {
9008169689Skan	    error ("a cast to a type other than an integral or "
9009169689Skan		   "enumeration type cannot appear in a constant-expression");
9010169689Skan	    return error_mark_node;
9011169689Skan	  }
9012169689Skan
9013169689Skan	op = RECUR (TREE_OPERAND (t, 0));
9014169689Skan
9015169689Skan	switch (TREE_CODE (t))
9016169689Skan	  {
9017169689Skan	  case CAST_EXPR:
9018169689Skan	    return build_functional_cast (type, op);
9019169689Skan	  case REINTERPRET_CAST_EXPR:
9020169689Skan	    return build_reinterpret_cast (type, op);
9021169689Skan	  case CONST_CAST_EXPR:
9022169689Skan	    return build_const_cast (type, op);
9023169689Skan	  case DYNAMIC_CAST_EXPR:
9024169689Skan	    return build_dynamic_cast (type, op);
9025169689Skan	  case STATIC_CAST_EXPR:
9026169689Skan	    return build_static_cast (type, op);
9027169689Skan	  default:
9028169689Skan	    gcc_unreachable ();
9029169689Skan	  }
9030169689Skan      }
9031169689Skan
9032132718Skan    case POSTDECREMENT_EXPR:
9033132718Skan    case POSTINCREMENT_EXPR:
9034132718Skan      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9035132718Skan						args, complain, in_decl);
9036132718Skan      return build_x_unary_op (TREE_CODE (t), op1);
9037132718Skan
9038132718Skan    case PREDECREMENT_EXPR:
9039132718Skan    case PREINCREMENT_EXPR:
9040132718Skan    case NEGATE_EXPR:
9041132718Skan    case BIT_NOT_EXPR:
9042132718Skan    case ABS_EXPR:
9043132718Skan    case TRUTH_NOT_EXPR:
9044169689Skan    case UNARY_PLUS_EXPR:  /* Unary + */
9045132718Skan    case REALPART_EXPR:
9046132718Skan    case IMAGPART_EXPR:
9047132718Skan      return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
9048132718Skan
9049132718Skan    case ADDR_EXPR:
9050132718Skan      op1 = TREE_OPERAND (t, 0);
9051132718Skan      if (TREE_CODE (op1) == SCOPE_REF)
9052169689Skan	op1 = tsubst_qualified_id (op1, args, complain, in_decl,
9053132718Skan				   /*done=*/true, /*address_p=*/true);
9054132718Skan      else
9055169689Skan	op1 = tsubst_non_call_postfix_expression (op1, args, complain,
9056132718Skan						  in_decl);
9057132718Skan      if (TREE_CODE (op1) == LABEL_DECL)
9058132718Skan	return finish_label_address_expr (DECL_NAME (op1));
9059132718Skan      return build_x_unary_op (ADDR_EXPR, op1);
9060132718Skan
9061132718Skan    case PLUS_EXPR:
9062132718Skan    case MINUS_EXPR:
9063132718Skan    case MULT_EXPR:
9064132718Skan    case TRUNC_DIV_EXPR:
9065132718Skan    case CEIL_DIV_EXPR:
9066132718Skan    case FLOOR_DIV_EXPR:
9067132718Skan    case ROUND_DIV_EXPR:
9068132718Skan    case EXACT_DIV_EXPR:
9069132718Skan    case BIT_AND_EXPR:
9070132718Skan    case BIT_IOR_EXPR:
9071132718Skan    case BIT_XOR_EXPR:
9072132718Skan    case TRUNC_MOD_EXPR:
9073132718Skan    case FLOOR_MOD_EXPR:
9074132718Skan    case TRUTH_ANDIF_EXPR:
9075132718Skan    case TRUTH_ORIF_EXPR:
9076132718Skan    case TRUTH_AND_EXPR:
9077132718Skan    case TRUTH_OR_EXPR:
9078132718Skan    case RSHIFT_EXPR:
9079132718Skan    case LSHIFT_EXPR:
9080132718Skan    case RROTATE_EXPR:
9081132718Skan    case LROTATE_EXPR:
9082132718Skan    case EQ_EXPR:
9083132718Skan    case NE_EXPR:
9084132718Skan    case MAX_EXPR:
9085132718Skan    case MIN_EXPR:
9086132718Skan    case LE_EXPR:
9087132718Skan    case GE_EXPR:
9088132718Skan    case LT_EXPR:
9089132718Skan    case GT_EXPR:
9090132718Skan    case MEMBER_REF:
9091132718Skan    case DOTSTAR_EXPR:
9092132718Skan      return build_x_binary_op
9093169689Skan	(TREE_CODE (t),
9094132718Skan	 RECUR (TREE_OPERAND (t, 0)),
9095259268Spfg	 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
9096259268Spfg	  ? ERROR_MARK
9097259268Spfg	  : TREE_CODE (TREE_OPERAND (t, 0))),
9098132718Skan	 RECUR (TREE_OPERAND (t, 1)),
9099259268Spfg	 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
9100259268Spfg	  ? ERROR_MARK
9101259268Spfg	  : TREE_CODE (TREE_OPERAND (t, 1))),
9102132718Skan	 /*overloaded_p=*/NULL);
9103132718Skan
9104132718Skan    case SCOPE_REF:
9105132718Skan      return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
9106132718Skan				  /*address_p=*/false);
9107132718Skan    case ARRAY_REF:
9108132718Skan      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9109132718Skan						args, complain, in_decl);
9110259268Spfg      return build_x_binary_op (ARRAY_REF, op1,
9111259268Spfg				(TREE_NO_WARNING (TREE_OPERAND (t, 0))
9112259268Spfg				 ? ERROR_MARK
9113259268Spfg				 : TREE_CODE (TREE_OPERAND (t, 0))),
9114259268Spfg				RECUR (TREE_OPERAND (t, 1)),
9115259268Spfg				(TREE_NO_WARNING (TREE_OPERAND (t, 1))
9116259268Spfg				 ? ERROR_MARK
9117259268Spfg				 : TREE_CODE (TREE_OPERAND (t, 1))),
9118169689Skan				/*overloaded_p=*/NULL);
9119132718Skan
9120132718Skan    case SIZEOF_EXPR:
9121132718Skan    case ALIGNOF_EXPR:
9122132718Skan      op1 = TREE_OPERAND (t, 0);
9123132718Skan      if (!args)
9124132718Skan	{
9125132718Skan	  /* When there are no ARGS, we are trying to evaluate a
9126132718Skan	     non-dependent expression from the parser.  Trying to do
9127132718Skan	     the substitutions may not work.  */
9128132718Skan	  if (!TYPE_P (op1))
9129132718Skan	    op1 = TREE_TYPE (op1);
9130132718Skan	}
9131132718Skan      else
9132132718Skan	{
9133132718Skan	  ++skip_evaluation;
9134169689Skan	  op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9135169689Skan				       /*function_p=*/false,
9136169689Skan				       /*integral_constant_expression_p=*/false);
9137132718Skan	  --skip_evaluation;
9138132718Skan	}
9139132718Skan      if (TYPE_P (op1))
9140132718Skan	return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
9141132718Skan      else
9142132718Skan	return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
9143132718Skan
9144132718Skan    case MODOP_EXPR:
9145169689Skan      {
9146169689Skan	tree r = build_x_modify_expr
9147169689Skan	  (RECUR (TREE_OPERAND (t, 0)),
9148169689Skan	   TREE_CODE (TREE_OPERAND (t, 1)),
9149169689Skan	   RECUR (TREE_OPERAND (t, 2)));
9150169689Skan	/* TREE_NO_WARNING must be set if either the expression was
9151169689Skan	   parenthesized or it uses an operator such as >>= rather
9152169689Skan	   than plain assignment.  In the former case, it was already
9153169689Skan	   set and must be copied.  In the latter case,
9154169689Skan	   build_x_modify_expr sets it and it must not be reset
9155169689Skan	   here.  */
9156169689Skan	if (TREE_NO_WARNING (t))
9157169689Skan	  TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9158169689Skan	return r;
9159169689Skan      }
9160132718Skan
9161132718Skan    case ARROW_EXPR:
9162132718Skan      op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9163132718Skan						args, complain, in_decl);
9164132718Skan      /* Remember that there was a reference to this entity.  */
9165132718Skan      if (DECL_P (op1))
9166132718Skan	mark_used (op1);
9167132718Skan      return build_x_arrow (op1);
9168132718Skan
9169132718Skan    case NEW_EXPR:
9170132718Skan      return build_new
9171132718Skan	(RECUR (TREE_OPERAND (t, 0)),
9172132718Skan	 RECUR (TREE_OPERAND (t, 1)),
9173132718Skan	 RECUR (TREE_OPERAND (t, 2)),
9174169689Skan	 RECUR (TREE_OPERAND (t, 3)),
9175132718Skan	 NEW_EXPR_USE_GLOBAL (t));
9176132718Skan
9177132718Skan    case DELETE_EXPR:
9178132718Skan     return delete_sanity
9179132718Skan       (RECUR (TREE_OPERAND (t, 0)),
9180132718Skan	RECUR (TREE_OPERAND (t, 1)),
9181132718Skan	DELETE_EXPR_USE_VEC (t),
9182132718Skan	DELETE_EXPR_USE_GLOBAL (t));
9183132718Skan
9184132718Skan    case COMPOUND_EXPR:
9185132718Skan      return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9186132718Skan				    RECUR (TREE_OPERAND (t, 1)));
9187132718Skan
9188132718Skan    case CALL_EXPR:
9189132718Skan      {
9190132718Skan	tree function;
9191132718Skan	tree call_args;
9192132718Skan	bool qualified_p;
9193132718Skan	bool koenig_p;
9194132718Skan
9195132718Skan	function = TREE_OPERAND (t, 0);
9196132718Skan	/* When we parsed the expression,  we determined whether or
9197132718Skan	   not Koenig lookup should be performed.  */
9198132718Skan	koenig_p = KOENIG_LOOKUP_P (t);
9199132718Skan	if (TREE_CODE (function) == SCOPE_REF)
9200132718Skan	  {
9201132718Skan	    qualified_p = true;
9202132718Skan	    function = tsubst_qualified_id (function, args, complain, in_decl,
9203169689Skan					    /*done=*/false,
9204132718Skan					    /*address_p=*/false);
9205132718Skan	  }
9206132718Skan	else
9207132718Skan	  {
9208169689Skan	    if (TREE_CODE (function) == COMPONENT_REF)
9209169689Skan	      {
9210169689Skan		tree op = TREE_OPERAND (function, 1);
9211169689Skan
9212169689Skan		qualified_p = (TREE_CODE (op) == SCOPE_REF
9213169689Skan			       || (BASELINK_P (op)
9214169689Skan				   && BASELINK_QUALIFIED_P (op)));
9215169689Skan	      }
9216169689Skan	    else
9217169689Skan	      qualified_p = false;
9218169689Skan
9219169689Skan	    function = tsubst_copy_and_build (function, args, complain,
9220132718Skan					      in_decl,
9221169689Skan					      !qualified_p,
9222169689Skan					      integral_constant_expression_p);
9223169689Skan
9224132718Skan	    if (BASELINK_P (function))
9225132718Skan	      qualified_p = true;
9226132718Skan	  }
9227132718Skan
9228132718Skan	call_args = RECUR (TREE_OPERAND (t, 1));
9229132718Skan
9230132718Skan	/* We do not perform argument-dependent lookup if normal
9231132718Skan	   lookup finds a non-function, in accordance with the
9232132718Skan	   expected resolution of DR 218.  */
9233132718Skan	if (koenig_p
9234146895Skan	    && ((is_overloaded_fn (function)
9235146895Skan		 /* If lookup found a member function, the Koenig lookup is
9236146895Skan		    not appropriate, even if an unqualified-name was used
9237146895Skan		    to denote the function.  */
9238146895Skan		 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
9239132718Skan		|| TREE_CODE (function) == IDENTIFIER_NODE))
9240132718Skan	  function = perform_koenig_lookup (function, call_args);
9241132718Skan
9242132718Skan	if (TREE_CODE (function) == IDENTIFIER_NODE)
9243132718Skan	  {
9244132718Skan	    unqualified_name_lookup_error (function);
9245132718Skan	    return error_mark_node;
9246132718Skan	  }
9247132718Skan
9248132718Skan	/* Remember that there was a reference to this entity.  */
9249132718Skan	if (DECL_P (function))
9250132718Skan	  mark_used (function);
9251132718Skan
9252132718Skan	if (TREE_CODE (function) == OFFSET_REF)
9253132718Skan	  return build_offset_ref_call_from_tree (function, call_args);
9254132718Skan	if (TREE_CODE (function) == COMPONENT_REF)
9255132718Skan	  {
9256132718Skan	    if (!BASELINK_P (TREE_OPERAND (function, 1)))
9257132718Skan	      return finish_call_expr (function, call_args,
9258132718Skan				       /*disallow_virtual=*/false,
9259132718Skan				       /*koenig_p=*/false);
9260132718Skan	    else
9261169689Skan	      return (build_new_method_call
9262132718Skan		      (TREE_OPERAND (function, 0),
9263132718Skan		       TREE_OPERAND (function, 1),
9264169689Skan		       call_args, NULL_TREE,
9265169689Skan		       qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9266169689Skan		       /*fn_p=*/NULL));
9267132718Skan	  }
9268169689Skan	return finish_call_expr (function, call_args,
9269132718Skan				 /*disallow_virtual=*/qualified_p,
9270132718Skan				 koenig_p);
9271132718Skan      }
9272132718Skan
9273132718Skan    case COND_EXPR:
9274132718Skan      return build_x_conditional_expr
9275132718Skan	(RECUR (TREE_OPERAND (t, 0)),
9276132718Skan	 RECUR (TREE_OPERAND (t, 1)),
9277132718Skan	 RECUR (TREE_OPERAND (t, 2)));
9278132718Skan
9279132718Skan    case PSEUDO_DTOR_EXPR:
9280169689Skan      return finish_pseudo_destructor_expr
9281132718Skan	(RECUR (TREE_OPERAND (t, 0)),
9282132718Skan	 RECUR (TREE_OPERAND (t, 1)),
9283132718Skan	 RECUR (TREE_OPERAND (t, 2)));
9284132718Skan
9285132718Skan    case TREE_LIST:
9286132718Skan      {
9287132718Skan	tree purpose, value, chain;
9288132718Skan
9289132718Skan	if (t == void_list_node)
9290132718Skan	  return t;
9291132718Skan
9292132718Skan	purpose = TREE_PURPOSE (t);
9293132718Skan	if (purpose)
9294132718Skan	  purpose = RECUR (purpose);
9295132718Skan	value = TREE_VALUE (t);
9296132718Skan	if (value)
9297132718Skan	  value = RECUR (value);
9298132718Skan	chain = TREE_CHAIN (t);
9299132718Skan	if (chain && chain != void_type_node)
9300132718Skan	  chain = RECUR (chain);
9301132718Skan	if (purpose == TREE_PURPOSE (t)
9302132718Skan	    && value == TREE_VALUE (t)
9303132718Skan	    && chain == TREE_CHAIN (t))
9304132718Skan	  return t;
9305132718Skan	return tree_cons (purpose, value, chain);
9306132718Skan      }
9307132718Skan
9308132718Skan    case COMPONENT_REF:
9309132718Skan      {
9310132718Skan	tree object;
9311169689Skan	tree object_type;
9312132718Skan	tree member;
9313132718Skan
9314132718Skan	object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9315132718Skan						     args, complain, in_decl);
9316132718Skan	/* Remember that there was a reference to this entity.  */
9317132718Skan	if (DECL_P (object))
9318132718Skan	  mark_used (object);
9319169689Skan	object_type = TREE_TYPE (object);
9320132718Skan
9321132718Skan	member = TREE_OPERAND (t, 1);
9322132718Skan	if (BASELINK_P (member))
9323169689Skan	  member = tsubst_baselink (member,
9324132718Skan				    non_reference (TREE_TYPE (object)),
9325132718Skan				    args, complain, in_decl);
9326132718Skan	else
9327132718Skan	  member = tsubst_copy (member, args, complain, in_decl);
9328146895Skan	if (member == error_mark_node)
9329146895Skan	  return error_mark_node;
9330169689Skan
9331169689Skan	if (object_type && !CLASS_TYPE_P (object_type))
9332132718Skan	  {
9333132718Skan	    if (TREE_CODE (member) == BIT_NOT_EXPR)
9334169689Skan	      return finish_pseudo_destructor_expr (object,
9335132718Skan						    NULL_TREE,
9336169689Skan						    object_type);
9337132718Skan	    else if (TREE_CODE (member) == SCOPE_REF
9338132718Skan		     && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
9339169689Skan	      return finish_pseudo_destructor_expr (object,
9340132718Skan						    object,
9341169689Skan						    object_type);
9342132718Skan	  }
9343132718Skan	else if (TREE_CODE (member) == SCOPE_REF
9344132718Skan		 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9345132718Skan	  {
9346132718Skan	    tree tmpl;
9347132718Skan	    tree args;
9348169689Skan
9349132718Skan	    /* Lookup the template functions now that we know what the
9350132718Skan	       scope is.  */
9351132718Skan	    tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9352132718Skan	    args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
9353169689Skan	    member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
9354132718Skan					    /*is_type_p=*/false,
9355132718Skan					    /*complain=*/false);
9356132718Skan	    if (BASELINK_P (member))
9357146895Skan	      {
9358169689Skan		BASELINK_FUNCTIONS (member)
9359146895Skan		  = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9360146895Skan			      args);
9361169689Skan		member = (adjust_result_of_qualified_name_lookup
9362169689Skan			  (member, BINFO_TYPE (BASELINK_BINFO (member)),
9363169689Skan			   object_type));
9364146895Skan	      }
9365132718Skan	    else
9366132718Skan	      {
9367169689Skan		qualified_name_lookup_error (object_type, tmpl, member);
9368132718Skan		return error_mark_node;
9369132718Skan	      }
9370132718Skan	  }
9371146895Skan	else if (TREE_CODE (member) == SCOPE_REF
9372146895Skan		 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9373146895Skan		 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9374146895Skan	  {
9375146895Skan	    if (complain & tf_error)
9376146895Skan	      {
9377146895Skan		if (TYPE_P (TREE_OPERAND (member, 0)))
9378169689Skan		  error ("%qT is not a class or namespace",
9379146895Skan			 TREE_OPERAND (member, 0));
9380146895Skan		else
9381169689Skan		  error ("%qD is not a class or namespace",
9382146895Skan			 TREE_OPERAND (member, 0));
9383146895Skan	      }
9384146895Skan	    return error_mark_node;
9385146895Skan	  }
9386132718Skan	else if (TREE_CODE (member) == FIELD_DECL)
9387132718Skan	  return finish_non_static_data_member (member, object, NULL_TREE);
9388132718Skan
9389169689Skan	return finish_class_member_access_expr (object, member,
9390169689Skan						/*template_p=*/false);
9391132718Skan      }
9392132718Skan
9393132718Skan    case THROW_EXPR:
9394132718Skan      return build_throw
9395132718Skan	(RECUR (TREE_OPERAND (t, 0)));
9396132718Skan
9397132718Skan    case CONSTRUCTOR:
9398132718Skan      {
9399169689Skan	VEC(constructor_elt,gc) *n;
9400169689Skan	constructor_elt *ce;
9401169689Skan	unsigned HOST_WIDE_INT idx;
9402132718Skan	tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9403169689Skan	bool process_index_p;
9404132718Skan
9405169689Skan	if (type == error_mark_node)
9406169689Skan	  return error_mark_node;
9407169689Skan
9408132718Skan	/* digest_init will do the wrong thing if we let it.  */
9409132718Skan	if (type && TYPE_PTRMEMFUNC_P (type))
9410132718Skan	  return t;
9411132718Skan
9412169689Skan	/* We do not want to process the index of aggregate
9413132718Skan	   initializers as they are identifier nodes which will be
9414132718Skan	   looked up by digest_init.  */
9415169689Skan	process_index_p = !(type && IS_AGGR_TYPE (type));
9416169689Skan
9417169689Skan	n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9418169689Skan	for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9419132718Skan	  {
9420169689Skan	    if (ce->index && process_index_p)
9421169689Skan	      ce->index = RECUR (ce->index);
9422169689Skan	    ce->value = RECUR (ce->value);
9423132718Skan	  }
9424132718Skan
9425169689Skan	if (TREE_HAS_CONSTRUCTOR (t))
9426169689Skan	  return finish_compound_literal (type, n);
9427169689Skan
9428169689Skan	return build_constructor (NULL_TREE, n);
9429132718Skan      }
9430132718Skan
9431132718Skan    case TYPEID_EXPR:
9432132718Skan      {
9433132718Skan	tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9434132718Skan	if (TYPE_P (operand_0))
9435132718Skan	  return get_typeid (operand_0);
9436132718Skan	return build_typeid (operand_0);
9437132718Skan      }
9438132718Skan
9439169689Skan    case VAR_DECL:
9440169689Skan      if (!args)
9441169689Skan	return t;
9442169689Skan      /* Fall through */
9443169689Skan
9444132718Skan    case PARM_DECL:
9445169689Skan      {
9446169689Skan	tree r = tsubst_copy (t, args, complain, in_decl);
9447132718Skan
9448169689Skan	if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9449169689Skan	  /* If the original type was a reference, we'll be wrapped in
9450169689Skan	     the appropriate INDIRECT_REF.  */
9451169689Skan	  r = convert_from_reference (r);
9452169689Skan	return r;
9453169689Skan      }
9454132718Skan
9455132718Skan    case VA_ARG_EXPR:
9456132718Skan      return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9457169689Skan			     tsubst_copy (TREE_TYPE (t), args, complain,
9458132718Skan					  in_decl));
9459132718Skan
9460169689Skan    case OFFSETOF_EXPR:
9461169689Skan      return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
9462169689Skan
9463169689Skan    case STMT_EXPR:
9464169689Skan      {
9465169689Skan	tree old_stmt_expr = cur_stmt_expr;
9466169689Skan	tree stmt_expr = begin_stmt_expr ();
9467169689Skan
9468169689Skan	cur_stmt_expr = stmt_expr;
9469169689Skan	tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9470169689Skan		     integral_constant_expression_p);
9471169689Skan	stmt_expr = finish_stmt_expr (stmt_expr, false);
9472169689Skan	cur_stmt_expr = old_stmt_expr;
9473169689Skan
9474169689Skan	return stmt_expr;
9475169689Skan      }
9476169689Skan
9477146895Skan    case CONST_DECL:
9478146895Skan      t = tsubst_copy (t, args, complain, in_decl);
9479146895Skan      /* As in finish_id_expression, we resolve enumeration constants
9480146895Skan	 to their underlying values.  */
9481146895Skan      if (TREE_CODE (t) == CONST_DECL)
9482169689Skan	{
9483169689Skan	  used_types_insert (TREE_TYPE (t));
9484169689Skan	  return DECL_INITIAL (t);
9485169689Skan	}
9486146895Skan      return t;
9487146895Skan
9488132718Skan    default:
9489169689Skan      /* Handle Objective-C++ constructs, if appropriate.  */
9490169689Skan      {
9491169689Skan	tree subst
9492169689Skan	  = objcp_tsubst_copy_and_build (t, args, complain,
9493169689Skan					 in_decl, /*function_p=*/false);
9494169689Skan	if (subst)
9495169689Skan	  return subst;
9496169689Skan      }
9497132718Skan      return tsubst_copy (t, args, complain, in_decl);
9498132718Skan    }
9499132718Skan
9500132718Skan#undef RECUR
9501132718Skan}
9502132718Skan
9503132718Skan/* Verify that the instantiated ARGS are valid. For type arguments,
9504132718Skan   make sure that the type's linkage is ok. For non-type arguments,
9505132718Skan   make sure they are constants if they are integral or enumerations.
9506132718Skan   Emit an error under control of COMPLAIN, and return TRUE on error.  */
9507132718Skan
9508132718Skanstatic bool
9509132718Skancheck_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9510132718Skan{
9511132718Skan  int ix, len = DECL_NTPARMS (tmpl);
9512132718Skan  bool result = false;
9513132718Skan
9514132718Skan  for (ix = 0; ix != len; ix++)
9515132718Skan    {
9516132718Skan      tree t = TREE_VEC_ELT (args, ix);
9517169689Skan
9518132718Skan      if (TYPE_P (t))
9519132718Skan	{
9520132718Skan	  /* [basic.link]: A name with no linkage (notably, the name
9521132718Skan	     of a class or enumeration declared in a local scope)
9522132718Skan	     shall not be used to declare an entity with linkage.
9523132718Skan	     This implies that names with no linkage cannot be used as
9524132718Skan	     template arguments.  */
9525169689Skan	  tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9526132718Skan
9527132718Skan	  if (nt)
9528132718Skan	    {
9529169689Skan	      /* DR 488 makes use of a type with no linkage cause
9530169689Skan		 type deduction to fail.  */
9531169689Skan	      if (complain & tf_error)
9532169689Skan		{
9533169689Skan		  if (TYPE_ANONYMOUS_P (nt))
9534169689Skan		    error ("%qT is/uses anonymous type", t);
9535169689Skan		  else
9536169689Skan		    error ("template argument for %qD uses local type %qT",
9537169689Skan			   tmpl, t);
9538169689Skan		}
9539132718Skan	      result = true;
9540132718Skan	    }
9541132718Skan	  /* In order to avoid all sorts of complications, we do not
9542132718Skan	     allow variably-modified types as template arguments.  */
9543169689Skan	  else if (variably_modified_type_p (t, NULL_TREE))
9544132718Skan	    {
9545132718Skan	      if (complain & tf_error)
9546169689Skan		error ("%qT is a variably modified type", t);
9547132718Skan	      result = true;
9548132718Skan	    }
9549132718Skan	}
9550132718Skan      /* A non-type argument of integral or enumerated type must be a
9551132718Skan	 constant.  */
9552132718Skan      else if (TREE_TYPE (t)
9553132718Skan	       && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9554132718Skan	       && !TREE_CONSTANT (t))
9555132718Skan	{
9556132718Skan	  if (complain & tf_error)
9557169689Skan	    error ("integral expression %qE is not constant", t);
9558132718Skan	  result = true;
9559132718Skan	}
9560132718Skan    }
9561169689Skan  if (result && (complain & tf_error))
9562169689Skan    error ("  trying to instantiate %qD", tmpl);
9563132718Skan  return result;
9564132718Skan}
9565132718Skan
956652284Sobrien/* Instantiate the indicated variable or function template TMPL with
956752284Sobrien   the template arguments in TARG_PTR.  */
956852284Sobrien
956950397Sobrientree
9570132718Skaninstantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
957150397Sobrien{
957250397Sobrien  tree fndecl;
957352284Sobrien  tree gen_tmpl;
957452284Sobrien  tree spec;
9575169689Skan  HOST_WIDE_INT saved_processing_template_decl;
957618334Speter
957750397Sobrien  if (tmpl == error_mark_node)
957850397Sobrien    return error_mark_node;
957950397Sobrien
9580169689Skan  gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
958150397Sobrien
958290075Sobrien  /* If this function is a clone, handle it specially.  */
958390075Sobrien  if (DECL_CLONED_FUNCTION_P (tmpl))
958490075Sobrien    {
9585132718Skan      tree spec;
958690075Sobrien      tree clone;
9587169689Skan
9588132718Skan      spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9589132718Skan				   complain);
9590132718Skan      if (spec == error_mark_node)
9591132718Skan	return error_mark_node;
9592132718Skan
9593117395Skan      /* Look for the clone.  */
9594169689Skan      FOR_EACH_CLONE (clone, spec)
959590075Sobrien	if (DECL_NAME (clone) == DECL_NAME (tmpl))
959690075Sobrien	  return clone;
959790075Sobrien      /* We should always have found the clone by now.  */
9598169689Skan      gcc_unreachable ();
959990075Sobrien      return NULL_TREE;
960090075Sobrien    }
9601169689Skan
960252284Sobrien  /* Check to see if we already have this specialization.  */
9603169689Skan  spec = retrieve_specialization (tmpl, targ_ptr,
9604169689Skan				  /*class_specializations_p=*/false);
960552284Sobrien  if (spec != NULL_TREE)
960652284Sobrien    return spec;
960752284Sobrien
960890075Sobrien  gen_tmpl = most_general_template (tmpl);
960990075Sobrien  if (tmpl != gen_tmpl)
961018334Speter    {
961152284Sobrien      /* The TMPL is a partial instantiation.  To get a full set of
961252284Sobrien	 arguments we must add the arguments used to perform the
961352284Sobrien	 partial instantiation.  */
961452284Sobrien      targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
961552284Sobrien					      targ_ptr);
961652284Sobrien
961752284Sobrien      /* Check to see if we already have this specialization.  */
9618169689Skan      spec = retrieve_specialization (gen_tmpl, targ_ptr,
9619169689Skan				      /*class_specializations_p=*/false);
962050397Sobrien      if (spec != NULL_TREE)
962150397Sobrien	return spec;
962218334Speter    }
962350397Sobrien
9624132718Skan  if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9625132718Skan			       complain))
9626132718Skan    return error_mark_node;
9627169689Skan
9628132718Skan  /* We are building a FUNCTION_DECL, during which the access of its
9629132718Skan     parameters and return types have to be checked.  However this
9630132718Skan     FUNCTION_DECL which is the desired context for access checking
9631132718Skan     is not built yet.  We solve this chicken-and-egg problem by
9632132718Skan     deferring all checks until we have the FUNCTION_DECL.  */
9633132718Skan  push_deferring_access_checks (dk_deferred);
963450397Sobrien
9635169689Skan  /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9636169689Skan     (because, for example, we have encountered a non-dependent
9637169689Skan     function call in the body of a template function and must now
9638169689Skan     determine which of several overloaded functions will be called),
9639169689Skan     within the instantiation itself we are not processing a
9640169689Skan     template.  */
9641169689Skan  saved_processing_template_decl = processing_template_decl;
9642169689Skan  processing_template_decl = 0;
9643169689Skan  /* Substitute template parameters to obtain the specialization.  */
964490075Sobrien  fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9645132718Skan		   targ_ptr, complain, gen_tmpl);
9646169689Skan  processing_template_decl = saved_processing_template_decl;
9647169689Skan  if (fndecl == error_mark_node)
9648169689Skan    return error_mark_node;
9649117395Skan
9650132718Skan  /* Now we know the specialization, compute access previously
9651132718Skan     deferred.  */
9652132718Skan  push_access_scope (fndecl);
9653132718Skan  perform_deferred_access_checks ();
9654132718Skan  pop_access_scope (fndecl);
9655132718Skan  pop_deferring_access_checks ();
9656117395Skan
965752284Sobrien  /* The DECL_TI_TEMPLATE should always be the immediate parent
965852284Sobrien     template, not the most general template.  */
965952284Sobrien  DECL_TI_TEMPLATE (fndecl) = tmpl;
966050397Sobrien
966190075Sobrien  /* If we've just instantiated the main entry point for a function,
966290075Sobrien     instantiate all the alternate entry points as well.  We do this
966390075Sobrien     by cloning the instantiation of the main entry point, not by
966490075Sobrien     instantiating the template clones.  */
966590075Sobrien  if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
966690075Sobrien    clone_function_decl (fndecl, /*update_method_vec_p=*/0);
966718334Speter
966818334Speter  return fndecl;
966918334Speter}
967018334Speter
967152284Sobrien/* The FN is a TEMPLATE_DECL for a function.  The ARGS are the
967252284Sobrien   arguments that are being used when calling it.  TARGS is a vector
9673169689Skan   into which the deduced template arguments are placed.
967450397Sobrien
967550397Sobrien   Return zero for success, 2 for an incomplete match that doesn't resolve
967650397Sobrien   all the types, and 1 for complete failure.  An error message will be
967750397Sobrien   printed only for an incomplete match.
967818334Speter
967990075Sobrien   If FN is a conversion operator, or we are trying to produce a specific
968090075Sobrien   specialization, RETURN_TYPE is the return type desired.
968150397Sobrien
968250397Sobrien   The EXPLICIT_TARGS are explicit template arguments provided via a
968350397Sobrien   template-id.
968450397Sobrien
968550397Sobrien   The parameter STRICT is one of:
968650397Sobrien
9687169689Skan   DEDUCE_CALL:
968850397Sobrien     We are deducing arguments for a function call, as in
968950397Sobrien     [temp.deduct.call].
969050397Sobrien
969150397Sobrien   DEDUCE_CONV:
9692169689Skan     We are deducing arguments for a conversion function, as in
969350397Sobrien     [temp.deduct.conv].
969450397Sobrien
969550397Sobrien   DEDUCE_EXACT:
969690075Sobrien     We are deducing arguments when doing an explicit instantiation
969790075Sobrien     as in [temp.explicit], when determining an explicit specialization
969890075Sobrien     as in [temp.expl.spec], or when taking the address of a function
9699169689Skan     template, as in [temp.deduct.funcaddr].  */
970090075Sobrien
970150397Sobrienint
9702169689Skanfn_type_unification (tree fn,
9703169689Skan		     tree explicit_targs,
9704169689Skan		     tree targs,
9705169689Skan		     tree args,
9706169689Skan		     tree return_type,
9707169689Skan		     unification_kind_t strict,
9708169689Skan		     int flags)
970918334Speter{
971052284Sobrien  tree parms;
971152284Sobrien  tree fntype;
971290075Sobrien  int result;
971318334Speter
9714169689Skan  gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9715117395Skan
971652284Sobrien  fntype = TREE_TYPE (fn);
971752284Sobrien  if (explicit_targs)
971818334Speter    {
971952284Sobrien      /* [temp.deduct]
9720169689Skan
972152284Sobrien	 The specified template arguments must match the template
972252284Sobrien	 parameters in kind (i.e., type, nontype, template), and there
972352284Sobrien	 must not be more arguments than there are parameters;
972452284Sobrien	 otherwise type deduction fails.
972518334Speter
972652284Sobrien	 Nontype arguments must match the types of the corresponding
972752284Sobrien	 nontype template parameters, or must be convertible to the
972852284Sobrien	 types of the corresponding nontype parameters as specified in
972952284Sobrien	 _temp.arg.nontype_, otherwise type deduction fails.
973052284Sobrien
973152284Sobrien	 All references in the function type of the function template
973252284Sobrien	 to the corresponding template parameters are replaced by the
973352284Sobrien	 specified template argument values.  If a substitution in a
973452284Sobrien	 template parameter or in the function type of the function
973552284Sobrien	 template results in an invalid type, type deduction fails.  */
973652284Sobrien      int i;
973752284Sobrien      tree converted_args;
9738132718Skan      bool incomplete;
973952284Sobrien
9740146895Skan      if (explicit_targs == error_mark_node)
9741146895Skan	return 1;
9742146895Skan
974352284Sobrien      converted_args
9744169689Skan	= (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9745169689Skan				  explicit_targs, NULL_TREE, tf_none,
9746169689Skan				  /*require_all_args=*/false,
9747169689Skan				  /*use_default_args=*/false));
974852284Sobrien      if (converted_args == error_mark_node)
974950397Sobrien	return 1;
975018334Speter
9751132718Skan      /* Substitute the explicit args into the function type.  This is
9752169689Skan	 necessary so that, for instance, explicitly declared function
9753169689Skan	 arguments can match null pointed constants.  If we were given
9754169689Skan	 an incomplete set of explicit args, we must not do semantic
9755169689Skan	 processing during substitution as we could create partial
9756169689Skan	 instantiations.  */
9757132718Skan      incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9758132718Skan      processing_template_decl += incomplete;
975996263Sobrien      fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9760132718Skan      processing_template_decl -= incomplete;
9761169689Skan
976252284Sobrien      if (fntype == error_mark_node)
976352284Sobrien	return 1;
976418334Speter
976552284Sobrien      /* Place the explicitly specified arguments in TARGS.  */
9766132718Skan      for (i = NUM_TMPL_ARGS (converted_args); i--;)
976752284Sobrien	TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
976818334Speter    }
9769169689Skan
977090075Sobrien  /* Never do unification on the 'this' parameter.  */
9771169689Skan  parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
9772169689Skan
977390075Sobrien  if (return_type)
977452284Sobrien    {
977590075Sobrien      parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
977690075Sobrien      args = tree_cons (NULL_TREE, return_type, args);
977752284Sobrien    }
977852284Sobrien
977952284Sobrien  /* We allow incomplete unification without an error message here
978052284Sobrien     because the standard doesn't seem to explicitly prohibit it.  Our
978152284Sobrien     callers must be ready to deal with unification failures in any
978252284Sobrien     event.  */
9783169689Skan  result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
978490075Sobrien				  targs, parms, args, /*subr=*/0,
9785169689Skan				  strict, flags);
978690075Sobrien
9787169689Skan  if (result == 0)
978890075Sobrien    /* All is well so far.  Now, check:
9789169689Skan
9790169689Skan       [temp.deduct]
9791169689Skan
979290075Sobrien       When all template arguments have been deduced, all uses of
979390075Sobrien       template parameters in nondeduced contexts are replaced with
979490075Sobrien       the corresponding deduced argument values.  If the
979590075Sobrien       substitution results in an invalid type, as described above,
979690075Sobrien       type deduction fails.  */
979796263Sobrien    if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
979890075Sobrien	== error_mark_node)
979990075Sobrien      return 1;
980090075Sobrien
980190075Sobrien  return result;
980218334Speter}
980318334Speter
980450397Sobrien/* Adjust types before performing type deduction, as described in
980550397Sobrien   [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
980650397Sobrien   sections are symmetric.  PARM is the type of a function parameter
980750397Sobrien   or the return type of the conversion function.  ARG is the type of
980850397Sobrien   the argument passed to the call, or the type of the value
980990075Sobrien   initialized with the result of the conversion function.  */
981050397Sobrien
981190075Sobrienstatic int
9812169689Skanmaybe_adjust_types_for_deduction (unification_kind_t strict,
9813169689Skan				  tree* parm,
9814169689Skan				  tree* arg)
981518334Speter{
981690075Sobrien  int result = 0;
9817169689Skan
981850397Sobrien  switch (strict)
981950397Sobrien    {
982050397Sobrien    case DEDUCE_CALL:
982150397Sobrien      break;
982218334Speter
982350397Sobrien    case DEDUCE_CONV:
982450397Sobrien      {
982550397Sobrien	/* Swap PARM and ARG throughout the remainder of this
982650397Sobrien	   function; the handling is precisely symmetric since PARM
982750397Sobrien	   will initialize ARG rather than vice versa.  */
982850397Sobrien	tree* temp = parm;
982950397Sobrien	parm = arg;
983050397Sobrien	arg = temp;
983150397Sobrien	break;
983250397Sobrien      }
983350397Sobrien
983450397Sobrien    case DEDUCE_EXACT:
983550397Sobrien      /* There is nothing to do in this case.  */
983690075Sobrien      return 0;
983750397Sobrien
983850397Sobrien    default:
9839169689Skan      gcc_unreachable ();
984018334Speter    }
984118334Speter
984250397Sobrien  if (TREE_CODE (*parm) != REFERENCE_TYPE)
984318334Speter    {
984450397Sobrien      /* [temp.deduct.call]
9845169689Skan
984650397Sobrien	 If P is not a reference type:
9847169689Skan
984850397Sobrien	 --If A is an array type, the pointer type produced by the
984950397Sobrien	 array-to-pointer standard conversion (_conv.array_) is
985050397Sobrien	 used in place of A for type deduction; otherwise,
9851169689Skan
985250397Sobrien	 --If A is a function type, the pointer type produced by
985350397Sobrien	 the function-to-pointer standard conversion
985450397Sobrien	 (_conv.func_) is used in place of A for type deduction;
985550397Sobrien	 otherwise,
9856169689Skan
985750397Sobrien	 --If A is a cv-qualified type, the top level
985850397Sobrien	 cv-qualifiers of A's type are ignored for type
985950397Sobrien	 deduction.  */
986050397Sobrien      if (TREE_CODE (*arg) == ARRAY_TYPE)
986150397Sobrien	*arg = build_pointer_type (TREE_TYPE (*arg));
986252284Sobrien      else if (TREE_CODE (*arg) == FUNCTION_TYPE)
986350397Sobrien	*arg = build_pointer_type (*arg);
986450397Sobrien      else
986550397Sobrien	*arg = TYPE_MAIN_VARIANT (*arg);
986618334Speter    }
9867169689Skan
986850397Sobrien  /* [temp.deduct.call]
9869169689Skan
987050397Sobrien     If P is a cv-qualified type, the top level cv-qualifiers
987150397Sobrien     of P's type are ignored for type deduction.  If P is a
987250397Sobrien     reference type, the type referred to by P is used for
987350397Sobrien     type deduction.  */
987450397Sobrien  *parm = TYPE_MAIN_VARIANT (*parm);
987550397Sobrien  if (TREE_CODE (*parm) == REFERENCE_TYPE)
987690075Sobrien    {
987790075Sobrien      *parm = TREE_TYPE (*parm);
987890075Sobrien      result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
987990075Sobrien    }
9880117395Skan
9881117395Skan  /* DR 322. For conversion deduction, remove a reference type on parm
9882117395Skan     too (which has been swapped into ARG).  */
9883117395Skan  if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9884117395Skan    *arg = TREE_TYPE (*arg);
9885169689Skan
988690075Sobrien  return result;
988718334Speter}
988818334Speter
988990075Sobrien/* Most parms like fn_type_unification.
989018334Speter
989150397Sobrien   If SUBR is 1, we're being called recursively (to unify the
989250397Sobrien   arguments of a function or method parameter of a function
9893169689Skan   template). */
989418334Speter
989550397Sobrienstatic int
9896169689Skantype_unification_real (tree tparms,
9897169689Skan		       tree targs,
9898169689Skan		       tree xparms,
9899169689Skan		       tree xargs,
9900169689Skan		       int subr,
9901169689Skan		       unification_kind_t strict,
9902169689Skan		       int flags)
990318334Speter{
990418334Speter  tree parm, arg;
990518334Speter  int i;
990618334Speter  int ntparms = TREE_VEC_LENGTH (tparms);
990750397Sobrien  int sub_strict;
990890075Sobrien  int saw_undeduced = 0;
990990075Sobrien  tree parms, args;
991018334Speter
9911169689Skan  gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9912169689Skan  gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9913169689Skan  gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9914169689Skan  gcc_assert (ntparms > 0);
991518334Speter
991650397Sobrien  switch (strict)
991750397Sobrien    {
991850397Sobrien    case DEDUCE_CALL:
991990075Sobrien      sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9920169689Skan		    | UNIFY_ALLOW_DERIVED);
992150397Sobrien      break;
9922169689Skan
992350397Sobrien    case DEDUCE_CONV:
992450397Sobrien      sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
992550397Sobrien      break;
992618334Speter
992750397Sobrien    case DEDUCE_EXACT:
992850397Sobrien      sub_strict = UNIFY_ALLOW_NONE;
992950397Sobrien      break;
9930169689Skan
993150397Sobrien    default:
9932169689Skan      gcc_unreachable ();
993350397Sobrien    }
993450397Sobrien
993590075Sobrien again:
993690075Sobrien  parms = xparms;
993790075Sobrien  args = xargs;
993890075Sobrien
9939169689Skan  while (parms && parms != void_list_node
9940169689Skan	 && args && args != void_list_node)
994118334Speter    {
994218334Speter      parm = TREE_VALUE (parms);
994318334Speter      parms = TREE_CHAIN (parms);
994418334Speter      arg = TREE_VALUE (args);
994518334Speter      args = TREE_CHAIN (args);
994618334Speter
994718334Speter      if (arg == error_mark_node)
994818334Speter	return 1;
994918334Speter      if (arg == unknown_type_node)
995052284Sobrien	/* We can't deduce anything from this, but we might get all the
995152284Sobrien	   template args from other function args.  */
995252284Sobrien	continue;
995318334Speter
995450397Sobrien      /* Conversions will be performed on a function argument that
995550397Sobrien	 corresponds with a function parameter that contains only
995650397Sobrien	 non-deducible template parameters and explicitly specified
995750397Sobrien	 template parameters.  */
9958132718Skan      if (!uses_template_parms (parm))
995918334Speter	{
996050397Sobrien	  tree type;
996150397Sobrien
996290075Sobrien	  if (!TYPE_P (arg))
996350397Sobrien	    type = TREE_TYPE (arg);
996450397Sobrien	  else
9965132718Skan	    type = arg;
996650397Sobrien
9967161651Skan	  if (same_type_p (parm, type))
996818334Speter	    continue;
9969161651Skan	  if (strict != DEDUCE_EXACT
9970169689Skan	      && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9971169689Skan				  flags))
9972161651Skan	    continue;
9973169689Skan
997418334Speter	  return 1;
997518334Speter	}
9976169689Skan
997790075Sobrien      if (!TYPE_P (arg))
997818334Speter	{
9979169689Skan	  gcc_assert (TREE_TYPE (arg) != NULL_TREE);
998052284Sobrien	  if (type_unknown_p (arg))
998118334Speter	    {
9982171825Skan	      /* [temp.deduct.type]
9983171825Skan
9984171825Skan	         A template-argument can be deduced from a pointer to
9985171825Skan		 function or pointer to member function argument if
9986171825Skan		 the set of overloaded functions does not contain
9987171825Skan		 function templates and at most one of a set of
998852284Sobrien		 overloaded functions provides a unique match.  */
9989171825Skan	      if (resolve_overloaded_unification
9990171825Skan		  (tparms, targs, parm, arg, strict, sub_strict))
9991171825Skan		continue;
999218334Speter
9993171825Skan	      return 1;
999418334Speter	    }
9995169689Skan	  arg = unlowered_expr_type (arg);
9996132718Skan	  if (arg == error_mark_node)
9997132718Skan	    return 1;
999818334Speter	}
9999169689Skan
1000090075Sobrien      {
10001169689Skan	int arg_strict = sub_strict;
10002169689Skan
10003169689Skan	if (!subr)
1000490075Sobrien	  arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
1000518334Speter
10006169689Skan	if (unify (tparms, targs, parm, arg, arg_strict))
10007169689Skan	  return 1;
1000890075Sobrien      }
10009169689Skan    }
1001050397Sobrien
1001118334Speter  /* Fail if we've reached the end of the parm list, and more args
1001218334Speter     are present, and the parm list isn't variadic.  */
1001318334Speter  if (args && args != void_list_node && parms == void_list_node)
1001418334Speter    return 1;
10015132718Skan  /* Fail if parms are left and they don't have default values.  */
10016169689Skan  if (parms && parms != void_list_node
1001718334Speter      && TREE_PURPOSE (parms) == NULL_TREE)
1001818334Speter    return 1;
1001990075Sobrien
1002018334Speter  if (!subr)
1002118334Speter    for (i = 0; i < ntparms; i++)
10022169689Skan      if (!TREE_VEC_ELT (targs, i))
1002318334Speter	{
10024169689Skan	  tree tparm;
1002590075Sobrien
10026169689Skan          if (TREE_VEC_ELT (tparms, i) == error_mark_node)
10027169689Skan            continue;
10028169689Skan
10029169689Skan          tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
10030169689Skan
1003190075Sobrien	  /* If this is an undeduced nontype parameter that depends on
1003290075Sobrien	     a type parameter, try another pass; its type may have been
1003390075Sobrien	     deduced from a later argument than the one from which
1003490075Sobrien	     this parameter can be deduced.  */
1003590075Sobrien	  if (TREE_CODE (tparm) == PARM_DECL
1003690075Sobrien	      && uses_template_parms (TREE_TYPE (tparm))
1003790075Sobrien	      && !saw_undeduced++)
1003890075Sobrien	    goto again;
1003990075Sobrien
1004018334Speter	  return 2;
1004118334Speter	}
10042169689Skan
1004318334Speter  return 0;
1004418334Speter}
1004518334Speter
10046171825Skan/* Subroutine of type_unification_real.  Args are like the variables
10047171825Skan   at the call site.  ARG is an overloaded function (or template-id);
10048171825Skan   we try deducing template args from each of the overloads, and if
10049171825Skan   only one succeeds, we go with that.  Modifies TARGS and returns
10050171825Skan   true on success.  */
1005152284Sobrien
10052171825Skanstatic bool
10053169689Skanresolve_overloaded_unification (tree tparms,
10054169689Skan				tree targs,
10055169689Skan				tree parm,
10056169689Skan				tree arg,
10057169689Skan				unification_kind_t strict,
10058132718Skan				int sub_strict)
1005952284Sobrien{
1006052284Sobrien  tree tempargs = copy_node (targs);
1006152284Sobrien  int good = 0;
10062122180Skan  bool addr_p;
1006352284Sobrien
1006452284Sobrien  if (TREE_CODE (arg) == ADDR_EXPR)
10065122180Skan    {
10066122180Skan      arg = TREE_OPERAND (arg, 0);
10067122180Skan      addr_p = true;
10068122180Skan    }
10069122180Skan  else
10070122180Skan    addr_p = false;
1007152284Sobrien
1007252284Sobrien  if (TREE_CODE (arg) == COMPONENT_REF)
1007352284Sobrien    /* Handle `&x' where `x' is some static or non-static member
1007452284Sobrien       function name.  */
1007552284Sobrien    arg = TREE_OPERAND (arg, 1);
1007652284Sobrien
1007752284Sobrien  if (TREE_CODE (arg) == OFFSET_REF)
1007852284Sobrien    arg = TREE_OPERAND (arg, 1);
1007952284Sobrien
1008052284Sobrien  /* Strip baselink information.  */
10081117395Skan  if (BASELINK_P (arg))
10082117395Skan    arg = BASELINK_FUNCTIONS (arg);
1008352284Sobrien
1008452284Sobrien  if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
1008552284Sobrien    {
1008652284Sobrien      /* If we got some explicit template args, we need to plug them into
1008752284Sobrien	 the affected templates before we try to unify, in case the
1008852284Sobrien	 explicit args will completely resolve the templates in question.  */
1008952284Sobrien
1009052284Sobrien      tree expl_subargs = TREE_OPERAND (arg, 1);
1009152284Sobrien      arg = TREE_OPERAND (arg, 0);
1009252284Sobrien
1009352284Sobrien      for (; arg; arg = OVL_NEXT (arg))
1009452284Sobrien	{
1009552284Sobrien	  tree fn = OVL_CURRENT (arg);
1009652284Sobrien	  tree subargs, elem;
1009752284Sobrien
1009852284Sobrien	  if (TREE_CODE (fn) != TEMPLATE_DECL)
1009952284Sobrien	    continue;
1010052284Sobrien
10101169689Skan	  subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
10102169689Skan				  expl_subargs, /*check_ret=*/false);
1010352284Sobrien	  if (subargs)
1010452284Sobrien	    {
1010596263Sobrien	      elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
10106169689Skan	      good += try_one_overload (tparms, targs, tempargs, parm,
10107122180Skan					elem, strict, sub_strict, addr_p);
1010852284Sobrien	    }
1010952284Sobrien	}
1011052284Sobrien    }
10111171825Skan  else if (TREE_CODE (arg) != OVERLOAD
10112171825Skan	   && TREE_CODE (arg) != FUNCTION_DECL)
10113171825Skan    /* If ARG is, for example, "(0, &f)" then its type will be unknown
10114171825Skan       -- but the deduction does not succeed because the expression is
10115171825Skan       not just the function on its own.  */
10116171825Skan    return false;
10117169689Skan  else
10118171825Skan    for (; arg; arg = OVL_NEXT (arg))
10119171825Skan      good += try_one_overload (tparms, targs, tempargs, parm,
10120171825Skan				TREE_TYPE (OVL_CURRENT (arg)),
10121171825Skan				strict, sub_strict, addr_p);
10122169689Skan
1012352284Sobrien  /* [temp.deduct.type] A template-argument can be deduced from a pointer
1012452284Sobrien     to function or pointer to member function argument if the set of
1012552284Sobrien     overloaded functions does not contain function templates and at most
1012652284Sobrien     one of a set of overloaded functions provides a unique match.
1012752284Sobrien
1012852284Sobrien     So if we found multiple possibilities, we return success but don't
1012952284Sobrien     deduce anything.  */
1013052284Sobrien
1013152284Sobrien  if (good == 1)
1013252284Sobrien    {
1013352284Sobrien      int i = TREE_VEC_LENGTH (targs);
1013452284Sobrien      for (; i--; )
1013552284Sobrien	if (TREE_VEC_ELT (tempargs, i))
1013652284Sobrien	  TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
1013752284Sobrien    }
1013852284Sobrien  if (good)
10139171825Skan    return true;
1014052284Sobrien
10141171825Skan  return false;
1014252284Sobrien}
1014352284Sobrien
1014452284Sobrien/* Subroutine of resolve_overloaded_unification; does deduction for a single
1014552284Sobrien   overload.  Fills TARGS with any deduced arguments, or error_mark_node if
1014652284Sobrien   different overloads deduce different arguments for a given parm.
10147122180Skan   ADDR_P is true if the expression for which deduction is being
10148122180Skan   performed was of the form "& fn" rather than simply "fn".
10149122180Skan
1015052284Sobrien   Returns 1 on success.  */
1015152284Sobrien
1015252284Sobrienstatic int
10153122180Skantry_one_overload (tree tparms,
10154169689Skan		  tree orig_targs,
10155169689Skan		  tree targs,
10156169689Skan		  tree parm,
10157169689Skan		  tree arg,
10158169689Skan		  unification_kind_t strict,
10159122180Skan		  int sub_strict,
10160122180Skan		  bool addr_p)
1016152284Sobrien{
1016252284Sobrien  int nargs;
1016352284Sobrien  tree tempargs;
1016452284Sobrien  int i;
1016552284Sobrien
1016652284Sobrien  /* [temp.deduct.type] A template-argument can be deduced from a pointer
1016752284Sobrien     to function or pointer to member function argument if the set of
1016852284Sobrien     overloaded functions does not contain function templates and at most
1016952284Sobrien     one of a set of overloaded functions provides a unique match.
1017052284Sobrien
1017152284Sobrien     So if this is a template, just return success.  */
1017252284Sobrien
1017352284Sobrien  if (uses_template_parms (arg))
1017452284Sobrien    return 1;
1017552284Sobrien
10176122180Skan  if (TREE_CODE (arg) == METHOD_TYPE)
10177122180Skan    arg = build_ptrmemfunc_type (build_pointer_type (arg));
10178122180Skan  else if (addr_p)
10179122180Skan    arg = build_pointer_type (arg);
10180122180Skan
1018190075Sobrien  sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
1018252284Sobrien
1018352284Sobrien  /* We don't copy orig_targs for this because if we have already deduced
1018452284Sobrien     some template args from previous args, unify would complain when we
1018552284Sobrien     try to deduce a template parameter for the same argument, even though
1018652284Sobrien     there isn't really a conflict.  */
1018752284Sobrien  nargs = TREE_VEC_LENGTH (targs);
1018890075Sobrien  tempargs = make_tree_vec (nargs);
1018952284Sobrien
1019052284Sobrien  if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
1019152284Sobrien    return 0;
1019252284Sobrien
1019352284Sobrien  /* First make sure we didn't deduce anything that conflicts with
1019452284Sobrien     explicitly specified args.  */
1019552284Sobrien  for (i = nargs; i--; )
1019652284Sobrien    {
1019752284Sobrien      tree elt = TREE_VEC_ELT (tempargs, i);
1019852284Sobrien      tree oldelt = TREE_VEC_ELT (orig_targs, i);
1019952284Sobrien
10200169689Skan      if (!elt)
10201169689Skan	/*NOP*/;
1020252284Sobrien      else if (uses_template_parms (elt))
10203169689Skan	/* Since we're unifying against ourselves, we will fill in
10204169689Skan	   template args used in the function parm list with our own
10205169689Skan	   template parms.  Discard them.  */
10206169689Skan	TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10207169689Skan      else if (oldelt && !template_args_equal (oldelt, elt))
1020852284Sobrien	return 0;
1020952284Sobrien    }
1021052284Sobrien
1021152284Sobrien  for (i = nargs; i--; )
1021252284Sobrien    {
1021352284Sobrien      tree elt = TREE_VEC_ELT (tempargs, i);
1021452284Sobrien
1021552284Sobrien      if (elt)
1021652284Sobrien	TREE_VEC_ELT (targs, i) = elt;
1021752284Sobrien    }
1021852284Sobrien
1021952284Sobrien  return 1;
1022052284Sobrien}
1022152284Sobrien
1022252284Sobrien/* PARM is a template class (perhaps with unbound template
1022352284Sobrien   parameters).  ARG is a fully instantiated type.  If ARG can be
1022452284Sobrien   bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
1022552284Sobrien   TARGS are as for unify.  */
1022652284Sobrien
1022752284Sobrienstatic tree
10228132718Skantry_class_unification (tree tparms, tree targs, tree parm, tree arg)
1022952284Sobrien{
1023052284Sobrien  tree copy_of_targs;
1023152284Sobrien
1023252284Sobrien  if (!CLASSTYPE_TEMPLATE_INFO (arg)
10233169689Skan      || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
10234117395Skan	  != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
1023552284Sobrien    return NULL_TREE;
1023652284Sobrien
1023752284Sobrien  /* We need to make a new template argument vector for the call to
1023852284Sobrien     unify.  If we used TARGS, we'd clutter it up with the result of
1023952284Sobrien     the attempted unification, even if this class didn't work out.
1024052284Sobrien     We also don't want to commit ourselves to all the unifications
1024152284Sobrien     we've already done, since unification is supposed to be done on
1024252284Sobrien     an argument-by-argument basis.  In other words, consider the
1024352284Sobrien     following pathological case:
1024452284Sobrien
1024552284Sobrien       template <int I, int J, int K>
1024652284Sobrien       struct S {};
10247169689Skan
1024852284Sobrien       template <int I, int J>
1024952284Sobrien       struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
10250169689Skan
1025152284Sobrien       template <int I, int J, int K>
1025252284Sobrien       void f(S<I, J, K>, S<I, I, I>);
10253169689Skan
1025452284Sobrien       void g() {
10255169689Skan	 S<0, 0, 0> s0;
10256169689Skan	 S<0, 1, 2> s2;
10257169689Skan
10258169689Skan	 f(s0, s2);
1025952284Sobrien       }
1026052284Sobrien
1026152284Sobrien     Now, by the time we consider the unification involving `s2', we
1026252284Sobrien     already know that we must have `f<0, 0, 0>'.  But, even though
10263117395Skan     `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
1026452284Sobrien     because there are two ways to unify base classes of S<0, 1, 2>
1026552284Sobrien     with S<I, I, I>.  If we kept the already deduced knowledge, we
1026652284Sobrien     would reject the possibility I=1.  */
1026790075Sobrien  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
10268169689Skan
1026952284Sobrien  /* If unification failed, we're done.  */
1027090075Sobrien  if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
1027190075Sobrien	     CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
1027252284Sobrien    return NULL_TREE;
1027390075Sobrien
1027490075Sobrien  return arg;
1027552284Sobrien}
1027652284Sobrien
10277169689Skan/* Given a template type PARM and a class type ARG, find the unique
10278169689Skan   base type in ARG that is an instance of PARM.  We do not examine
10279169689Skan   ARG itself; only its base-classes.  If there is not exactly one
10280169689Skan   appropriate base class, return NULL_TREE.  PARM may be the type of
10281169689Skan   a partial specialization, as well as a plain template type.  Used
10282169689Skan   by unify.  */
1028352284Sobrien
1028452284Sobrienstatic tree
10285169689Skanget_template_base (tree tparms, tree targs, tree parm, tree arg)
1028652284Sobrien{
10287169689Skan  tree rval = NULL_TREE;
10288169689Skan  tree binfo;
1028952284Sobrien
10290169689Skan  gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
1029152284Sobrien
10292169689Skan  binfo = TYPE_BINFO (complete_type (arg));
10293169689Skan  if (!binfo)
10294169689Skan    /* The type could not be completed.  */
10295169689Skan    return NULL_TREE;
1029652284Sobrien
10297169689Skan  /* Walk in inheritance graph order.  The search order is not
10298169689Skan     important, and this avoids multiple walks of virtual bases.  */
10299169689Skan  for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
10300169689Skan    {
10301169689Skan      tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
1030252284Sobrien
10303169689Skan      if (r)
10304169689Skan	{
10305169689Skan	  /* If there is more than one satisfactory baseclass, then:
1030652284Sobrien
10307169689Skan	       [temp.deduct.call]
1030852284Sobrien
10309169689Skan	      If they yield more than one possible deduced A, the type
10310169689Skan	      deduction fails.
1031152284Sobrien
10312169689Skan	     applies.  */
10313169689Skan	  if (rval && !same_type_p (r, rval))
10314169689Skan	    return NULL_TREE;
1031552284Sobrien
10316169689Skan	  rval = r;
10317169689Skan	}
1031852284Sobrien    }
1031952284Sobrien
1032052284Sobrien  return rval;
1032152284Sobrien}
1032252284Sobrien
1032350397Sobrien/* Returns the level of DECL, which declares a template parameter.  */
1032450397Sobrien
1032552284Sobrienstatic int
10326132718Skantemplate_decl_level (tree decl)
1032718334Speter{
1032850397Sobrien  switch (TREE_CODE (decl))
1032950397Sobrien    {
1033050397Sobrien    case TYPE_DECL:
1033150397Sobrien    case TEMPLATE_DECL:
1033250397Sobrien      return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
1033350397Sobrien
1033450397Sobrien    case PARM_DECL:
1033550397Sobrien      return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
1033650397Sobrien
1033750397Sobrien    default:
10338169689Skan      gcc_unreachable ();
1033950397Sobrien    }
10340169689Skan  return 0;
1034150397Sobrien}
1034250397Sobrien
1034350397Sobrien/* Decide whether ARG can be unified with PARM, considering only the
1034450397Sobrien   cv-qualifiers of each type, given STRICT as documented for unify.
10345169689Skan   Returns nonzero iff the unification is OK on that basis.  */
1034650397Sobrien
1034752284Sobrienstatic int
10348132718Skancheck_cv_quals_for_unify (int strict, tree arg, tree parm)
1034950397Sobrien{
1035096263Sobrien  int arg_quals = cp_type_quals (arg);
1035196263Sobrien  int parm_quals = cp_type_quals (parm);
1035296263Sobrien
10353132718Skan  if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10354132718Skan      && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
1035596263Sobrien    {
10356132718Skan      /*  Although a CVR qualifier is ignored when being applied to a
10357169689Skan	  substituted template parameter ([8.3.2]/1 for example), that
10358169689Skan	  does not apply during deduction [14.8.2.4]/1, (even though
10359169689Skan	  that is not explicitly mentioned, [14.8.2.4]/9 indicates
10360169689Skan	  this).  Except when we're allowing additional CV qualifiers
10361169689Skan	  at the outer level [14.8.2.1]/3,1st bullet.  */
10362132718Skan      if ((TREE_CODE (arg) == REFERENCE_TYPE
10363132718Skan	   || TREE_CODE (arg) == FUNCTION_TYPE
10364132718Skan	   || TREE_CODE (arg) == METHOD_TYPE)
10365132718Skan	  && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10366132718Skan	return 0;
10367132718Skan
10368132718Skan      if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10369132718Skan	  && (parm_quals & TYPE_QUAL_RESTRICT))
10370132718Skan	return 0;
1037196263Sobrien    }
10372132718Skan
1037390075Sobrien  if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
1037496263Sobrien      && (arg_quals & parm_quals) != parm_quals)
1037590075Sobrien    return 0;
1037690075Sobrien
1037790075Sobrien  if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
1037896263Sobrien      && (parm_quals & arg_quals) != arg_quals)
1037990075Sobrien    return 0;
1038090075Sobrien
1038190075Sobrien  return 1;
1038250397Sobrien}
1038350397Sobrien
10384169689Skan/* Deduce the value of template parameters.  TPARMS is the (innermost)
10385169689Skan   set of template parameters to a template.  TARGS is the bindings
10386169689Skan   for those template parameters, as determined thus far; TARGS may
10387169689Skan   include template arguments for outer levels of template parameters
10388169689Skan   as well.  PARM is a parameter to a template function, or a
10389169689Skan   subcomponent of that parameter; ARG is the corresponding argument.
10390169689Skan   This function attempts to match PARM with ARG in a manner
10391169689Skan   consistent with the existing assignments in TARGS.  If more values
10392169689Skan   are deduced, then TARGS is updated.
1039350397Sobrien
10394169689Skan   Returns 0 if the type deduction succeeds, 1 otherwise.  The
10395169689Skan   parameter STRICT is a bitwise or of the following flags:
10396169689Skan
1039750397Sobrien     UNIFY_ALLOW_NONE:
1039850397Sobrien       Require an exact match between PARM and ARG.
1039950397Sobrien     UNIFY_ALLOW_MORE_CV_QUAL:
1040090075Sobrien       Allow the deduced ARG to be more cv-qualified (by qualification
1040190075Sobrien       conversion) than ARG.
1040250397Sobrien     UNIFY_ALLOW_LESS_CV_QUAL:
1040350397Sobrien       Allow the deduced ARG to be less cv-qualified than ARG.
1040450397Sobrien     UNIFY_ALLOW_DERIVED:
1040550397Sobrien       Allow the deduced ARG to be a template base class of ARG,
1040650397Sobrien       or a pointer to a template base class of the type pointed to by
1040752284Sobrien       ARG.
1040852284Sobrien     UNIFY_ALLOW_INTEGER:
1040952284Sobrien       Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
10410169689Skan       case for more information.
1041190075Sobrien     UNIFY_ALLOW_OUTER_LEVEL:
1041290075Sobrien       This is the outermost level of a deduction. Used to determine validity
1041390075Sobrien       of qualification conversions. A valid qualification conversion must
1041490075Sobrien       have const qualified pointers leading up to the inner type which
1041590075Sobrien       requires additional CV quals, except at the outer level, where const
1041690075Sobrien       is not required [conv.qual]. It would be normal to set this flag in
1041790075Sobrien       addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
1041890075Sobrien     UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
1041990075Sobrien       This is the outermost level of a deduction, and PARM can be more CV
1042090075Sobrien       qualified at this point.
1042190075Sobrien     UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
1042290075Sobrien       This is the outermost level of a deduction, and PARM can be less CV
10423169689Skan       qualified at this point.  */
1042450397Sobrien
1042552284Sobrienstatic int
10426132718Skanunify (tree tparms, tree targs, tree parm, tree arg, int strict)
1042750397Sobrien{
1042818334Speter  int idx;
1042950397Sobrien  tree targ;
1043050397Sobrien  tree tparm;
1043190075Sobrien  int strict_in = strict;
1043218334Speter
1043318334Speter  /* I don't think this will do the right thing with respect to types.
1043418334Speter     But the only case I've seen it in so far has been array bounds, where
1043518334Speter     signedness is the only information lost, and I think that will be
1043618334Speter     okay.  */
1043718334Speter  while (TREE_CODE (parm) == NOP_EXPR)
1043818334Speter    parm = TREE_OPERAND (parm, 0);
1043918334Speter
1044018334Speter  if (arg == error_mark_node)
1044118334Speter    return 1;
1044218334Speter  if (arg == unknown_type_node)
1044352284Sobrien    /* We can't deduce anything from this, but we might get all the
1044452284Sobrien       template args from other function args.  */
1044552284Sobrien    return 0;
1044652284Sobrien
1044750397Sobrien  /* If PARM uses template parameters, then we can't bail out here,
1044852284Sobrien     even if ARG == PARM, since we won't record unifications for the
1044950397Sobrien     template parameters.  We might need them if we're trying to
1045050397Sobrien     figure out which of two things is more specialized.  */
1045150397Sobrien  if (arg == parm && !uses_template_parms (parm))
1045218334Speter    return 0;
1045318334Speter
1045450397Sobrien  /* Immediately reject some pairs that won't unify because of
1045550397Sobrien     cv-qualification mismatches.  */
1045650397Sobrien  if (TREE_CODE (arg) == TREE_CODE (parm)
1045790075Sobrien      && TYPE_P (arg)
1045890075Sobrien      /* It is the elements of the array which hold the cv quals of an array
10459169689Skan	 type, and the elements might be template type parms. We'll check
10460169689Skan	 when we recurse.  */
1046190075Sobrien      && TREE_CODE (arg) != ARRAY_TYPE
1046250397Sobrien      /* We check the cv-qualifiers when unifying with template type
1046350397Sobrien	 parameters below.  We want to allow ARG `const T' to unify with
1046450397Sobrien	 PARM `T' for example, when computing which of two templates
1046550397Sobrien	 is more specialized, for example.  */
1046650397Sobrien      && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
1046790075Sobrien      && !check_cv_quals_for_unify (strict_in, arg, parm))
1046850397Sobrien    return 1;
1046950397Sobrien
1047090075Sobrien  if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
1047190075Sobrien      && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
1047290075Sobrien    strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
1047390075Sobrien  strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
1047490075Sobrien  strict &= ~UNIFY_ALLOW_DERIVED;
1047590075Sobrien  strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
1047690075Sobrien  strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10477169689Skan
1047818334Speter  switch (TREE_CODE (parm))
1047918334Speter    {
1048050397Sobrien    case TYPENAME_TYPE:
1048190075Sobrien    case SCOPE_REF:
1048290075Sobrien    case UNBOUND_CLASS_TEMPLATE:
1048350397Sobrien      /* In a type which contains a nested-name-specifier, template
1048450397Sobrien	 argument values cannot be deduced for template parameters used
1048550397Sobrien	 within the nested-name-specifier.  */
1048650397Sobrien      return 0;
1048750397Sobrien
1048818334Speter    case TEMPLATE_TYPE_PARM:
1048950397Sobrien    case TEMPLATE_TEMPLATE_PARM:
1049090075Sobrien    case BOUND_TEMPLATE_TEMPLATE_PARM:
1049150397Sobrien      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10492220150Smm      if (tparm == error_mark_node)
10493220150Smm	return 1;
1049450397Sobrien
1049550397Sobrien      if (TEMPLATE_TYPE_LEVEL (parm)
1049650397Sobrien	  != template_decl_level (tparm))
1049750397Sobrien	/* The PARM is not one we're trying to unify.  Just check
1049850397Sobrien	   to see if it matches ARG.  */
1049950397Sobrien	return (TREE_CODE (arg) == TREE_CODE (parm)
1050052284Sobrien		&& same_type_p (parm, arg)) ? 0 : 1;
1050118334Speter      idx = TEMPLATE_TYPE_IDX (parm);
10502169689Skan      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
1050350397Sobrien      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
1050450397Sobrien
1050518334Speter      /* Check for mixed types and values.  */
1050650397Sobrien      if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
1050750397Sobrien	   && TREE_CODE (tparm) != TYPE_DECL)
10508169689Skan	  || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
1050950397Sobrien	      && TREE_CODE (tparm) != TEMPLATE_DECL))
1051018334Speter	return 1;
1051150397Sobrien
1051290075Sobrien      if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
1051318334Speter	{
1051490075Sobrien	  /* ARG must be constructed from a template class or a template
1051590075Sobrien	     template parameter.  */
1051690075Sobrien	  if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10517169689Skan	      && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
1051890075Sobrien	    return 1;
1051950397Sobrien
1052090075Sobrien	  {
1052190075Sobrien	    tree parmvec = TYPE_TI_ARGS (parm);
10522169689Skan	    tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
1052390075Sobrien	    tree argtmplvec
1052490075Sobrien	      = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
1052590075Sobrien	    int i;
1052650397Sobrien
10527169689Skan	    /* The resolution to DR150 makes clear that default
10528169689Skan	       arguments for an N-argument may not be used to bind T
10529169689Skan	       to a template template parameter with fewer than N
10530169689Skan	       parameters.  It is not safe to permit the binding of
10531169689Skan	       default arguments as an extension, as that may change
10532169689Skan	       the meaning of a conforming program.  Consider:
1053350397Sobrien
10534169689Skan		  struct Dense { static const unsigned int dim = 1; };
10535169689Skan
10536169689Skan		  template <template <typename> class View,
10537169689Skan			    typename Block>
10538169689Skan		  void operator+(float, View<Block> const&);
10539169689Skan
10540169689Skan		  template <typename Block,
10541169689Skan			    unsigned int Dim = Block::dim>
10542169689Skan		  struct Lvalue_proxy { operator float() const; };
10543169689Skan
10544169689Skan		  void
10545169689Skan		  test_1d (void) {
10546169689Skan		    Lvalue_proxy<Dense> p;
10547169689Skan		    float b;
10548169689Skan		    b + p;
10549169689Skan		  }
10550169689Skan
10551169689Skan	      Here, if Lvalue_proxy is permitted to bind to View, then
10552169689Skan	      the global operator+ will be used; if they are not, the
10553169689Skan	      Lvalue_proxy will be converted to float.  */
10554169689Skan	    if (coerce_template_parms (argtmplvec, parmvec,
10555169689Skan				       TYPE_TI_TEMPLATE (parm),
10556169689Skan				       tf_none,
10557169689Skan				       /*require_all_args=*/true,
10558169689Skan				       /*use_default_args=*/false)
10559169689Skan		== error_mark_node)
1056090075Sobrien	      return 1;
10561169689Skan
10562169689Skan	    /* Deduce arguments T, i from TT<T> or TT<i>.
1056390075Sobrien	       We check each element of PARMVEC and ARGVEC individually
1056490075Sobrien	       rather than the whole TREE_VEC since they can have
1056590075Sobrien	       different number of elements.  */
1056652284Sobrien
1056790075Sobrien	    for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
1056890075Sobrien	      {
10569169689Skan		if (unify (tparms, targs,
10570169689Skan			   TREE_VEC_ELT (parmvec, i),
10571169689Skan			   TREE_VEC_ELT (argvec, i),
1057290075Sobrien			   UNIFY_ALLOW_NONE))
1057390075Sobrien		  return 1;
1057418334Speter	      }
1057590075Sobrien	  }
1057690075Sobrien	  arg = TYPE_TI_TEMPLATE (arg);
1057790075Sobrien
1057890075Sobrien	  /* Fall through to deduce template name.  */
1057918334Speter	}
1058090075Sobrien
1058190075Sobrien      if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
1058290075Sobrien	  || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
1058390075Sobrien	{
1058490075Sobrien	  /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
1058590075Sobrien
1058690075Sobrien	  /* Simple cases: Value already set, does match or doesn't.  */
1058790075Sobrien	  if (targ != NULL_TREE && template_args_equal (targ, arg))
1058890075Sobrien	    return 0;
1058990075Sobrien	  else if (targ)
1059090075Sobrien	    return 1;
1059190075Sobrien	}
1059250397Sobrien      else
1059350397Sobrien	{
1059450397Sobrien	  /* If PARM is `const T' and ARG is only `int', we don't have
1059550397Sobrien	     a match unless we are allowing additional qualification.
1059650397Sobrien	     If ARG is `const int' and PARM is just `T' that's OK;
1059750397Sobrien	     that binds `const int' to `T'.  */
10598169689Skan	  if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
1059950397Sobrien					 arg, parm))
1060050397Sobrien	    return 1;
1060118334Speter
1060250397Sobrien	  /* Consider the case where ARG is `const volatile int' and
1060350397Sobrien	     PARM is `const T'.  Then, T should be `volatile int'.  */
1060496263Sobrien	  arg = cp_build_qualified_type_real
1060596263Sobrien	    (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
1060690075Sobrien	  if (arg == error_mark_node)
1060790075Sobrien	    return 1;
1060890075Sobrien
1060990075Sobrien	  /* Simple cases: Value already set, does match or doesn't.  */
1061090075Sobrien	  if (targ != NULL_TREE && same_type_p (targ, arg))
1061190075Sobrien	    return 0;
1061290075Sobrien	  else if (targ)
1061390075Sobrien	    return 1;
10614110611Skan
10615110611Skan	  /* Make sure that ARG is not a variable-sized array.  (Note
10616110611Skan	     that were talking about variable-sized arrays (like
10617110611Skan	     `int[n]'), rather than arrays of unknown size (like
10618110611Skan	     `int[]').)  We'll get very confused by such a type since
10619110611Skan	     the bound of the array will not be computable in an
10620110611Skan	     instantiation.  Besides, such types are not allowed in
10621110611Skan	     ISO C++, so we can do as we please here.  */
10622169689Skan	  if (variably_modified_type_p (arg, NULL_TREE))
10623110611Skan	    return 1;
1062450397Sobrien	}
1062550397Sobrien
10626169689Skan      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
1062718334Speter      return 0;
1062818334Speter
1062950397Sobrien    case TEMPLATE_PARM_INDEX:
1063050397Sobrien      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10631169689Skan      if (tparm == error_mark_node)
10632169689Skan	return 1;
1063350397Sobrien
10634169689Skan      if (TEMPLATE_PARM_LEVEL (parm)
1063550397Sobrien	  != template_decl_level (tparm))
1063650397Sobrien	/* The PARM is not one we're trying to unify.  Just check
1063750397Sobrien	   to see if it matches ARG.  */
10638132718Skan	return !(TREE_CODE (arg) == TREE_CODE (parm)
10639132718Skan		 && cp_tree_equal (parm, arg));
1064050397Sobrien
1064150397Sobrien      idx = TEMPLATE_PARM_IDX (parm);
10642169689Skan      targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
1064350397Sobrien
1064450397Sobrien      if (targ)
10645132718Skan	return !cp_tree_equal (targ, arg);
1064650397Sobrien
1064752284Sobrien      /* [temp.deduct.type] If, in the declaration of a function template
1064852284Sobrien	 with a non-type template-parameter, the non-type
1064952284Sobrien	 template-parameter is used in an expression in the function
1065052284Sobrien	 parameter-list and, if the corresponding template-argument is
1065152284Sobrien	 deduced, the template-argument type shall match the type of the
1065252284Sobrien	 template-parameter exactly, except that a template-argument
10653169689Skan	 deduced from an array bound may be of any integral type.
1065490075Sobrien	 The non-type parameter might use already deduced type parameters.  */
1065590075Sobrien      tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10656117395Skan      if (!TREE_TYPE (arg))
10657117395Skan	/* Template-parameter dependent expression.  Just accept it for now.
10658117395Skan	   It will later be processed in convert_template_argument.  */
10659117395Skan	;
10660117395Skan      else if (same_type_p (TREE_TYPE (arg), tparm))
10661117395Skan	/* OK */;
1066252284Sobrien      else if ((strict & UNIFY_ALLOW_INTEGER)
1066390075Sobrien	       && (TREE_CODE (tparm) == INTEGER_TYPE
1066490075Sobrien		   || TREE_CODE (tparm) == BOOLEAN_TYPE))
10665146895Skan	/* Convert the ARG to the type of PARM; the deduced non-type
10666146895Skan	   template argument must exactly match the types of the
10667146895Skan	   corresponding parameter.  */
10668146895Skan	arg = fold (build_nop (TREE_TYPE (parm), arg));
1066990075Sobrien      else if (uses_template_parms (tparm))
1067090075Sobrien	/* We haven't deduced the type of this parameter yet.  Try again
1067190075Sobrien	   later.  */
1067290075Sobrien	return 0;
1067352284Sobrien      else
1067452284Sobrien	return 1;
1067552284Sobrien
10676169689Skan      TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
1067750397Sobrien      return 0;
1067850397Sobrien
10679132718Skan    case PTRMEM_CST:
10680132718Skan     {
10681169689Skan	/* A pointer-to-member constant can be unified only with
10682169689Skan	 another constant.  */
10683132718Skan      if (TREE_CODE (arg) != PTRMEM_CST)
10684169689Skan	return 1;
10685132718Skan
10686132718Skan      /* Just unify the class member. It would be useless (and possibly
10687169689Skan	 wrong, depending on the strict flags) to unify also
10688169689Skan	 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10689169689Skan	 arg refer to the same variable, even if through different
10690169689Skan	 classes. For instance:
10691132718Skan
10692169689Skan	 struct A { int x; };
10693169689Skan	 struct B : A { };
10694132718Skan
10695169689Skan	 Unification of &A::x and &B::x must succeed.  */
10696132718Skan      return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10697169689Skan		    PTRMEM_CST_MEMBER (arg), strict);
10698132718Skan     }
10699132718Skan
1070018334Speter    case POINTER_TYPE:
1070150397Sobrien      {
1070250397Sobrien	if (TREE_CODE (arg) != POINTER_TYPE)
1070350397Sobrien	  return 1;
10704169689Skan
1070550397Sobrien	/* [temp.deduct.call]
1070650397Sobrien
1070750397Sobrien	   A can be another pointer or pointer to member type that can
1070850397Sobrien	   be converted to the deduced A via a qualification
1070950397Sobrien	   conversion (_conv.qual_).
1071050397Sobrien
1071150397Sobrien	   We pass down STRICT here rather than UNIFY_ALLOW_NONE.
1071250397Sobrien	   This will allow for additional cv-qualification of the
1071390075Sobrien	   pointed-to types if appropriate.  */
10714169689Skan
1071590075Sobrien	if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
1071650397Sobrien	  /* The derived-to-base conversion only persists through one
1071750397Sobrien	     level of pointers.  */
1071890075Sobrien	  strict |= (strict_in & UNIFY_ALLOW_DERIVED);
1071950397Sobrien
10720169689Skan	return unify (tparms, targs, TREE_TYPE (parm),
1072190075Sobrien		      TREE_TYPE (arg), strict);
1072250397Sobrien      }
1072350397Sobrien
1072418334Speter    case REFERENCE_TYPE:
1072550397Sobrien      if (TREE_CODE (arg) != REFERENCE_TYPE)
1072650397Sobrien	return 1;
1072750397Sobrien      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
1072890075Sobrien		    strict & UNIFY_ALLOW_MORE_CV_QUAL);
1072918334Speter
1073018334Speter    case ARRAY_TYPE:
1073118334Speter      if (TREE_CODE (arg) != ARRAY_TYPE)
1073218334Speter	return 1;
1073350397Sobrien      if ((TYPE_DOMAIN (parm) == NULL_TREE)
1073450397Sobrien	  != (TYPE_DOMAIN (arg) == NULL_TREE))
1073518334Speter	return 1;
10736169689Skan      if (TYPE_DOMAIN (parm) != NULL_TREE)
10737169689Skan	{
10738169689Skan	  tree parm_max;
10739169689Skan	  tree arg_max;
10740169689Skan	  bool parm_cst;
10741169689Skan	  bool arg_cst;
10742169689Skan
10743169689Skan	  /* Our representation of array types uses "N - 1" as the
10744169689Skan	     TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10745169689Skan	     not an integer constant.  We cannot unify arbitrarily
10746169689Skan	     complex expressions, so we eliminate the MINUS_EXPRs
10747169689Skan	     here.  */
10748169689Skan	  parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10749169689Skan	  parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10750169689Skan	  if (!parm_cst)
10751169689Skan	    {
10752169689Skan	      gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
10753169689Skan	      parm_max = TREE_OPERAND (parm_max, 0);
10754169689Skan	    }
10755169689Skan	  arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10756169689Skan	  arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10757169689Skan	  if (!arg_cst)
10758169689Skan	    {
10759169689Skan	      /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10760169689Skan		 trying to unify the type of a variable with the type
10761169689Skan		 of a template parameter.  For example:
10762169689Skan
10763169689Skan                   template <unsigned int N>
10764169689Skan		   void f (char (&) [N]);
10765169689Skan		   int g();
10766169689Skan		   void h(int i) {
10767169689Skan                     char a[g(i)];
10768169689Skan		     f(a);
10769169689Skan                   }
10770169689Skan
10771169689Skan                Here, the type of the ARG will be "int [g(i)]", and
10772169689Skan                may be a SAVE_EXPR, etc.  */
10773169689Skan	      if (TREE_CODE (arg_max) != MINUS_EXPR)
10774169689Skan		return 1;
10775169689Skan	      arg_max = TREE_OPERAND (arg_max, 0);
10776169689Skan	    }
10777169689Skan
10778169689Skan	  /* If only one of the bounds used a MINUS_EXPR, compensate
10779169689Skan	     by adding one to the other bound.  */
10780169689Skan	  if (parm_cst && !arg_cst)
10781169689Skan	    parm_max = fold_build2 (PLUS_EXPR,
10782169689Skan				    integer_type_node,
10783169689Skan				    parm_max,
10784169689Skan				    integer_one_node);
10785169689Skan	  else if (arg_cst && !parm_cst)
10786169689Skan	    arg_max = fold_build2 (PLUS_EXPR,
10787169689Skan				   integer_type_node,
10788169689Skan				   arg_max,
10789169689Skan				   integer_one_node);
10790169689Skan
10791169689Skan	  if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10792169689Skan	    return 1;
10793169689Skan	}
1079450397Sobrien      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10795117395Skan		    strict & UNIFY_ALLOW_MORE_CV_QUAL);
1079618334Speter
1079718334Speter    case REAL_TYPE:
1079850397Sobrien    case COMPLEX_TYPE:
1079990075Sobrien    case VECTOR_TYPE:
1080018334Speter    case INTEGER_TYPE:
1080150397Sobrien    case BOOLEAN_TYPE:
10802146895Skan    case ENUMERAL_TYPE:
1080350397Sobrien    case VOID_TYPE:
1080418334Speter      if (TREE_CODE (arg) != TREE_CODE (parm))
1080518334Speter	return 1;
1080618334Speter
1080790075Sobrien      /* We have already checked cv-qualification at the top of the
1080852284Sobrien	 function.  */
10809169689Skan      if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
1081050397Sobrien	return 1;
1081150397Sobrien
1081218334Speter      /* As far as unification is concerned, this wins.	 Later checks
1081318334Speter	 will invalidate it if necessary.  */
1081418334Speter      return 0;
1081518334Speter
1081618334Speter      /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
1081750397Sobrien      /* Type INTEGER_CST can come from ordinary constant template args.  */
1081818334Speter    case INTEGER_CST:
1081950397Sobrien      while (TREE_CODE (arg) == NOP_EXPR)
1082050397Sobrien	arg = TREE_OPERAND (arg, 0);
1082150397Sobrien
1082218334Speter      if (TREE_CODE (arg) != INTEGER_CST)
1082318334Speter	return 1;
1082418334Speter      return !tree_int_cst_equal (parm, arg);
1082518334Speter
1082618334Speter    case TREE_VEC:
1082718334Speter      {
1082818334Speter	int i;
1082918334Speter	if (TREE_CODE (arg) != TREE_VEC)
1083018334Speter	  return 1;
1083118334Speter	if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
1083218334Speter	  return 1;
1083390075Sobrien	for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
1083450397Sobrien	  if (unify (tparms, targs,
1083518334Speter		     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
1083652284Sobrien		     UNIFY_ALLOW_NONE))
1083718334Speter	    return 1;
1083818334Speter	return 0;
1083918334Speter      }
1084018334Speter
1084118334Speter    case RECORD_TYPE:
1084252284Sobrien    case UNION_TYPE:
1084352284Sobrien      if (TREE_CODE (arg) != TREE_CODE (parm))
1084418334Speter	return 1;
10845169689Skan
1084690075Sobrien      if (TYPE_PTRMEMFUNC_P (parm))
1084790075Sobrien	{
1084890075Sobrien	  if (!TYPE_PTRMEMFUNC_P (arg))
1084990075Sobrien	    return 1;
1085090075Sobrien
10851169689Skan	  return unify (tparms, targs,
1085290075Sobrien			TYPE_PTRMEMFUNC_FN_TYPE (parm),
1085390075Sobrien			TYPE_PTRMEMFUNC_FN_TYPE (arg),
1085490075Sobrien			strict);
1085590075Sobrien	}
1085690075Sobrien
1085752284Sobrien      if (CLASSTYPE_TEMPLATE_INFO (parm))
1085850397Sobrien	{
1085950397Sobrien	  tree t = NULL_TREE;
1086052284Sobrien
1086190075Sobrien	  if (strict_in & UNIFY_ALLOW_DERIVED)
1086252284Sobrien	    {
1086352284Sobrien	      /* First, we try to unify the PARM and ARG directly.  */
1086452284Sobrien	      t = try_class_unification (tparms, targs,
1086552284Sobrien					 parm, arg);
1086650397Sobrien
1086752284Sobrien	      if (!t)
1086852284Sobrien		{
1086952284Sobrien		  /* Fallback to the special case allowed in
1087052284Sobrien		     [temp.deduct.call]:
10871169689Skan
1087252284Sobrien		       If P is a class, and P has the form
1087352284Sobrien		       template-id, then A can be a derived class of
1087452284Sobrien		       the deduced A.  Likewise, if P is a pointer to
1087552284Sobrien		       a class of the form template-id, A can be a
1087652284Sobrien		       pointer to a derived class pointed to by the
1087752284Sobrien		       deduced A.  */
10878169689Skan		  t = get_template_base (tparms, targs, parm, arg);
1087952284Sobrien
10880169689Skan		  if (!t)
1088152284Sobrien		    return 1;
1088252284Sobrien		}
1088352284Sobrien	    }
10884169689Skan	  else if (CLASSTYPE_TEMPLATE_INFO (arg)
10885169689Skan		   && (CLASSTYPE_TI_TEMPLATE (parm)
1088652284Sobrien		       == CLASSTYPE_TI_TEMPLATE (arg)))
1088752284Sobrien	    /* Perhaps PARM is something like S<U> and ARG is S<int>.
1088852284Sobrien	       Then, we should unify `int' and `U'.  */
1088950397Sobrien	    t = arg;
1089052284Sobrien	  else
1089190075Sobrien	    /* There's no chance of unification succeeding.  */
1089250397Sobrien	    return 1;
1089350397Sobrien
1089450397Sobrien	  return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
1089552284Sobrien			CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
1089650397Sobrien	}
1089790075Sobrien      else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
1089850397Sobrien	return 1;
1089918334Speter      return 0;
1090018334Speter
1090118334Speter    case METHOD_TYPE:
1090250397Sobrien    case FUNCTION_TYPE:
1090350397Sobrien      if (TREE_CODE (arg) != TREE_CODE (parm))
1090418334Speter	return 1;
1090518334Speter
10906169689Skan      /* CV qualifications for methods can never be deduced, they must
10907169689Skan	 match exactly.  We need to check them explicitly here,
10908169689Skan	 because type_unification_real treats them as any other
10909169689Skan	 cvqualified parameter.  */
10910169689Skan      if (TREE_CODE (parm) == METHOD_TYPE
10911169689Skan	  && (!check_cv_quals_for_unify
10912169689Skan	      (UNIFY_ALLOW_NONE,
10913169689Skan	       TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10914169689Skan	       TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10915169689Skan	return 1;
10916169689Skan
1091750397Sobrien      if (unify (tparms, targs, TREE_TYPE (parm),
1091852284Sobrien		 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
1091918334Speter	return 1;
1092050397Sobrien      return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10921169689Skan				    TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10922169689Skan				    LOOKUP_NORMAL);
1092318334Speter
1092418334Speter    case OFFSET_TYPE:
10925169689Skan      /* Unify a pointer to member with a pointer to member function, which
10926169689Skan	 deduces the type of the member as a function type. */
10927169689Skan      if (TYPE_PTRMEMFUNC_P (arg))
10928169689Skan	{
10929169689Skan	  tree method_type;
10930169689Skan	  tree fntype;
10931169689Skan	  cp_cv_quals cv_quals;
10932169689Skan
10933169689Skan	  /* Check top-level cv qualifiers */
10934169689Skan	  if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10935169689Skan	    return 1;
10936169689Skan
10937169689Skan	  if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10938169689Skan		     TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10939169689Skan	    return 1;
10940169689Skan
10941169689Skan	  /* Determine the type of the function we are unifying against. */
10942169689Skan	  method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10943169689Skan	  fntype =
10944169689Skan	    build_function_type (TREE_TYPE (method_type),
10945169689Skan				 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10946169689Skan
10947169689Skan	  /* Extract the cv-qualifiers of the member function from the
10948169689Skan	     implicit object parameter and place them on the function
10949169689Skan	     type to be restored later. */
10950169689Skan	  cv_quals =
10951169689Skan	    cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10952169689Skan	  fntype = build_qualified_type (fntype, cv_quals);
10953169689Skan	  return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10954169689Skan	}
10955169689Skan
1095618334Speter      if (TREE_CODE (arg) != OFFSET_TYPE)
1095718334Speter	return 1;
1095850397Sobrien      if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
1095952284Sobrien		 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
1096018334Speter	return 1;
1096150397Sobrien      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
1096252284Sobrien		    strict);
1096318334Speter
1096450397Sobrien    case CONST_DECL:
10965132718Skan      if (DECL_TEMPLATE_PARM_P (parm))
10966132718Skan	return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10967169689Skan      if (arg != integral_constant_value (parm))
1096850397Sobrien	return 1;
1096950397Sobrien      return 0;
1097050397Sobrien
10971132718Skan    case FIELD_DECL:
1097250397Sobrien    case TEMPLATE_DECL:
1097350397Sobrien      /* Matched cases are handled by the ARG == PARM test above.  */
1097450397Sobrien      return 1;
1097550397Sobrien
10976169689Skan    default:
10977169689Skan      gcc_assert (EXPR_P (parm));
1097850397Sobrien
10979169689Skan      /* We must be looking at an expression.  This can happen with
10980169689Skan	 something like:
1098150397Sobrien
10982169689Skan	   template <int I>
10983169689Skan	   void foo(S<I>, S<I + 2>);
1098450397Sobrien
10985169689Skan	 This is a "nondeduced context":
1098650397Sobrien
10987169689Skan	   [deduct.type]
1098850397Sobrien
10989169689Skan	   The nondeduced contexts are:
1099090075Sobrien
10991169689Skan	   --A type that is a template-id in which one or more of
10992169689Skan	     the template-arguments is an expression that references
10993169689Skan	     a template-parameter.
1099450397Sobrien
10995169689Skan	 In these cases, we assume deduction succeeded, but don't
10996169689Skan	 actually infer any unifications.  */
1099750397Sobrien
10998169689Skan      if (!uses_template_parms (parm)
10999169689Skan	  && !template_args_equal (parm, arg))
11000169689Skan	return 1;
11001169689Skan      else
11002169689Skan	return 0;
1100318334Speter    }
1100418334Speter}
1100518334Speter
11006169689Skan/* Note that DECL can be defined in this translation unit, if
11007169689Skan   required.  */
11008169689Skan
11009169689Skanstatic void
11010169689Skanmark_definable (tree decl)
11011169689Skan{
11012169689Skan  tree clone;
11013169689Skan  DECL_NOT_REALLY_EXTERN (decl) = 1;
11014169689Skan  FOR_EACH_CLONE (clone, decl)
11015169689Skan    DECL_NOT_REALLY_EXTERN (clone) = 1;
11016169689Skan}
11017169689Skan
1101852284Sobrien/* Called if RESULT is explicitly instantiated, or is a member of an
11019169689Skan   explicitly instantiated class.  */
1102052284Sobrien
1102150397Sobrienvoid
11022132718Skanmark_decl_instantiated (tree result, int extern_p)
1102350397Sobrien{
11024169689Skan  SET_DECL_EXPLICIT_INSTANTIATION (result);
1102590075Sobrien
11026117395Skan  /* If this entity has already been written out, it's too late to
11027117395Skan     make any modifications.  */
11028117395Skan  if (TREE_ASM_WRITTEN (result))
11029117395Skan    return;
11030117395Skan
11031117395Skan  if (TREE_CODE (result) != FUNCTION_DECL)
11032117395Skan    /* The TREE_PUBLIC flag for function declarations will have been
11033117395Skan       set correctly by tsubst.  */
11034117395Skan    TREE_PUBLIC (result) = 1;
11035117395Skan
11036117395Skan  /* This might have been set by an earlier implicit instantiation.  */
11037117395Skan  DECL_COMDAT (result) = 0;
11038117395Skan
11039169689Skan  if (extern_p)
11040169689Skan    DECL_NOT_REALLY_EXTERN (result) = 0;
11041169689Skan  else
1104250397Sobrien    {
11043169689Skan      mark_definable (result);
1104452284Sobrien      /* Always make artificials weak.  */
1104552284Sobrien      if (DECL_ARTIFICIAL (result) && flag_weak)
1104652284Sobrien	comdat_linkage (result);
1104750397Sobrien      /* For WIN32 we also want to put explicit instantiations in
1104850397Sobrien	 linkonce sections.  */
1104952284Sobrien      else if (TREE_PUBLIC (result))
1105050397Sobrien	maybe_make_one_only (result);
1105150397Sobrien    }
11052117395Skan
11053169689Skan  /* If EXTERN_P, then this function will not be emitted -- unless
11054169689Skan     followed by an explicit instantiation, at which point its linkage
11055169689Skan     will be adjusted.  If !EXTERN_P, then this function will be
11056169689Skan     emitted here.  In neither circumstance do we want
11057169689Skan     import_export_decl to adjust the linkage.  */
11058169689Skan  DECL_INTERFACE_KNOWN (result) = 1;
1105950397Sobrien}
1106050397Sobrien
1106190075Sobrien/* Given two function templates PAT1 and PAT2, return:
1106250397Sobrien
1106350397Sobrien   1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
1106450397Sobrien   -1 if PAT2 is more specialized than PAT1.
1106590075Sobrien   0 if neither is more specialized.
1106690075Sobrien
11067169689Skan   LEN indicates the number of parameters we should consider
11068169689Skan   (defaulted parameters should not be considered).
11069169689Skan
11070169689Skan   The 1998 std underspecified function template partial ordering, and
11071169689Skan   DR214 addresses the issue.  We take pairs of arguments, one from
11072169689Skan   each of the templates, and deduce them against each other.  One of
11073169689Skan   the templates will be more specialized if all the *other*
11074169689Skan   template's arguments deduce against its arguments and at least one
11075169689Skan   of its arguments *does* *not* deduce against the other template's
11076169689Skan   corresponding argument.  Deduction is done as for class templates.
11077169689Skan   The arguments used in deduction have reference and top level cv
11078169689Skan   qualifiers removed.  Iff both arguments were originally reference
11079169689Skan   types *and* deduction succeeds in both directions, the template
11080169689Skan   with the more cv-qualified argument wins for that pairing (if
11081169689Skan   neither is more cv-qualified, they both are equal).  Unlike regular
11082169689Skan   deduction, after all the arguments have been deduced in this way,
11083169689Skan   we do *not* verify the deduced template argument values can be
11084169689Skan   substituted into non-deduced contexts, nor do we have to verify
11085169689Skan   that all template arguments have been deduced.  */
11086169689Skan
1108718334Speterint
11088169689Skanmore_specialized_fn (tree pat1, tree pat2, int len)
1108918334Speter{
11090169689Skan  tree decl1 = DECL_TEMPLATE_RESULT (pat1);
11091169689Skan  tree decl2 = DECL_TEMPLATE_RESULT (pat2);
11092169689Skan  tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
11093169689Skan  tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
11094169689Skan  tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
11095169689Skan  tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
11096169689Skan  tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
11097169689Skan  tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
11098169689Skan  int better1 = 0;
11099169689Skan  int better2 = 0;
1110018334Speter
11101169689Skan  /* Remove the this parameter from non-static member functions.  If
11102169689Skan     one is a non-static member function and the other is not a static
11103169689Skan     member function, remove the first parameter from that function
11104169689Skan     also.  This situation occurs for operator functions where we
11105169689Skan     locate both a member function (with this pointer) and non-member
11106169689Skan     operator (with explicit first operand).  */
11107169689Skan  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
11108169689Skan    {
11109169689Skan      len--; /* LEN is the number of significant arguments for DECL1 */
11110169689Skan      args1 = TREE_CHAIN (args1);
11111169689Skan      if (!DECL_STATIC_FUNCTION_P (decl2))
11112169689Skan	args2 = TREE_CHAIN (args2);
11113169689Skan    }
11114169689Skan  else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
11115169689Skan    {
11116169689Skan      args2 = TREE_CHAIN (args2);
11117169689Skan      if (!DECL_STATIC_FUNCTION_P (decl1))
11118169689Skan	{
11119169689Skan	  len--;
11120169689Skan	  args1 = TREE_CHAIN (args1);
11121169689Skan	}
11122169689Skan    }
1112318334Speter
11124169689Skan  /* If only one is a conversion operator, they are unordered.  */
11125169689Skan  if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
11126169689Skan    return 0;
1112718334Speter
11128169689Skan  /* Consider the return type for a conversion function */
11129169689Skan  if (DECL_CONV_FN_P (decl1))
11130169689Skan    {
11131169689Skan      args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
11132169689Skan      args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
11133169689Skan      len++;
11134169689Skan    }
11135169689Skan
11136169689Skan  processing_template_decl++;
11137169689Skan
11138169689Skan  while (len--)
11139169689Skan    {
11140169689Skan      tree arg1 = TREE_VALUE (args1);
11141169689Skan      tree arg2 = TREE_VALUE (args2);
11142169689Skan      int deduce1, deduce2;
11143169689Skan      int quals1 = -1;
11144169689Skan      int quals2 = -1;
11145169689Skan
11146169689Skan      if (TREE_CODE (arg1) == REFERENCE_TYPE)
11147169689Skan	{
11148169689Skan	  arg1 = TREE_TYPE (arg1);
11149169689Skan	  quals1 = cp_type_quals (arg1);
11150169689Skan	}
11151169689Skan
11152169689Skan      if (TREE_CODE (arg2) == REFERENCE_TYPE)
11153169689Skan	{
11154169689Skan	  arg2 = TREE_TYPE (arg2);
11155169689Skan	  quals2 = cp_type_quals (arg2);
11156169689Skan	}
11157169689Skan
11158169689Skan      if ((quals1 < 0) != (quals2 < 0))
11159169689Skan	{
11160169689Skan	  /* Only of the args is a reference, see if we should apply
11161169689Skan	     array/function pointer decay to it.  This is not part of
11162169689Skan	     DR214, but is, IMHO, consistent with the deduction rules
11163169689Skan	     for the function call itself, and with our earlier
11164169689Skan	     implementation of the underspecified partial ordering
11165169689Skan	     rules.  (nathan).  */
11166169689Skan	  if (quals1 >= 0)
11167169689Skan	    {
11168169689Skan	      switch (TREE_CODE (arg1))
11169169689Skan		{
11170169689Skan		case ARRAY_TYPE:
11171169689Skan		  arg1 = TREE_TYPE (arg1);
11172169689Skan		  /* FALLTHROUGH. */
11173169689Skan		case FUNCTION_TYPE:
11174169689Skan		  arg1 = build_pointer_type (arg1);
11175169689Skan		  break;
11176169689Skan
11177169689Skan		default:
11178169689Skan		  break;
11179169689Skan		}
11180169689Skan	    }
11181169689Skan	  else
11182169689Skan	    {
11183169689Skan	      switch (TREE_CODE (arg2))
11184169689Skan		{
11185169689Skan		case ARRAY_TYPE:
11186169689Skan		  arg2 = TREE_TYPE (arg2);
11187169689Skan		  /* FALLTHROUGH. */
11188169689Skan		case FUNCTION_TYPE:
11189169689Skan		  arg2 = build_pointer_type (arg2);
11190169689Skan		  break;
11191169689Skan
11192169689Skan		default:
11193169689Skan		  break;
11194169689Skan		}
11195169689Skan	    }
11196169689Skan	}
11197169689Skan
11198169689Skan      arg1 = TYPE_MAIN_VARIANT (arg1);
11199169689Skan      arg2 = TYPE_MAIN_VARIANT (arg2);
11200169689Skan
11201169689Skan      deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11202169689Skan      deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11203169689Skan
11204169689Skan      if (!deduce1)
11205169689Skan	better2 = -1;
11206169689Skan      if (!deduce2)
11207169689Skan	better1 = -1;
11208169689Skan      if (better1 < 0 && better2 < 0)
11209169689Skan	/* We've failed to deduce something in either direction.
11210169689Skan	   These must be unordered.  */
11211169689Skan	break;
11212169689Skan
11213169689Skan      if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11214169689Skan	{
11215169689Skan	  /* Deduces in both directions, see if quals can
11216169689Skan	     disambiguate.  Pretend the worse one failed to deduce. */
11217169689Skan	  if ((quals1 & quals2) == quals2)
11218169689Skan	    deduce1 = 0;
11219169689Skan	  if ((quals1 & quals2) == quals1)
11220169689Skan	    deduce2 = 0;
11221169689Skan	}
11222169689Skan      if (deduce1 && !deduce2 && !better2)
11223169689Skan	better2 = 1;
11224169689Skan      if (deduce2 && !deduce1 && !better1)
11225169689Skan	better1 = 1;
11226169689Skan
11227169689Skan      args1 = TREE_CHAIN (args1);
11228169689Skan      args2 = TREE_CHAIN (args2);
11229169689Skan    }
11230169689Skan
11231169689Skan  processing_template_decl--;
11232169689Skan
11233169689Skan  return (better1 > 0) - (better2 > 0);
1123450397Sobrien}
1123550397Sobrien
11236169689Skan/* Determine which of two partial specializations is more specialized.
1123750397Sobrien
11238169689Skan   PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11239169689Skan   to the first partial specialization.  The TREE_VALUE is the
11240169689Skan   innermost set of template parameters for the partial
11241169689Skan   specialization.  PAT2 is similar, but for the second template.
11242119256Skan
11243169689Skan   Return 1 if the first partial specialization is more specialized;
11244169689Skan   -1 if the second is more specialized; 0 if neither is more
11245169689Skan   specialized.
11246169689Skan
11247169689Skan   See [temp.class.order] for information about determining which of
11248169689Skan   two templates is more specialized.  */
11249169689Skan
11250169689Skanstatic int
11251169689Skanmore_specialized_class (tree pat1, tree pat2)
1125250397Sobrien{
1125350397Sobrien  tree targs;
11254169689Skan  tree tmpl1, tmpl2;
1125550397Sobrien  int winner = 0;
1125650397Sobrien
11257169689Skan  tmpl1 = TREE_TYPE (pat1);
11258169689Skan  tmpl2 = TREE_TYPE (pat2);
11259169689Skan
11260169689Skan  /* Just like what happens for functions, if we are ordering between
11261132718Skan     different class template specializations, we may encounter dependent
11262132718Skan     types in the arguments, and we need our dependency check functions
11263132718Skan     to behave correctly.  */
11264132718Skan  ++processing_template_decl;
11265169689Skan  targs = get_class_bindings (TREE_VALUE (pat1),
11266169689Skan			      CLASSTYPE_TI_ARGS (tmpl1),
11267169689Skan			      CLASSTYPE_TI_ARGS (tmpl2));
1126850397Sobrien  if (targs)
1126950397Sobrien    --winner;
1127050397Sobrien
11271169689Skan  targs = get_class_bindings (TREE_VALUE (pat2),
11272169689Skan			      CLASSTYPE_TI_ARGS (tmpl2),
11273169689Skan			      CLASSTYPE_TI_ARGS (tmpl1));
1127450397Sobrien  if (targs)
1127550397Sobrien    ++winner;
11276132718Skan  --processing_template_decl;
1127750397Sobrien
1127850397Sobrien  return winner;
1127950397Sobrien}
1128050397Sobrien
1128150397Sobrien/* Return the template arguments that will produce the function signature
1128250397Sobrien   DECL from the function template FN, with the explicit template
11283169689Skan   arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
1128452284Sobrien   also match.  Return NULL_TREE if no satisfactory arguments could be
11285169689Skan   found.  */
11286169689Skan
1128750397Sobrienstatic tree
11288169689Skanget_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
1128950397Sobrien{
1129050397Sobrien  int ntparms = DECL_NTPARMS (fn);
1129190075Sobrien  tree targs = make_tree_vec (ntparms);
1129252284Sobrien  tree decl_type;
1129352284Sobrien  tree decl_arg_types;
1129450397Sobrien
1129552284Sobrien  /* Substitute the explicit template arguments into the type of DECL.
1129652284Sobrien     The call to fn_type_unification will handle substitution into the
1129752284Sobrien     FN.  */
1129852284Sobrien  decl_type = TREE_TYPE (decl);
1129952284Sobrien  if (explicit_args && uses_template_parms (decl_type))
1130018334Speter    {
1130152284Sobrien      tree tmpl;
1130252284Sobrien      tree converted_args;
1130352284Sobrien
1130452284Sobrien      if (DECL_TEMPLATE_INFO (decl))
1130552284Sobrien	tmpl = DECL_TI_TEMPLATE (decl);
1130650397Sobrien      else
11307117395Skan	/* We can get here for some invalid specializations.  */
1130852284Sobrien	return NULL_TREE;
1130952284Sobrien
1131052284Sobrien      converted_args
11311169689Skan	= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11312169689Skan				 explicit_args, NULL_TREE,
11313169689Skan				 tf_none,
11314169689Skan				 /*require_all_args=*/false,
11315169689Skan				 /*use_default_args=*/false);
1131652284Sobrien      if (converted_args == error_mark_node)
1131752284Sobrien	return NULL_TREE;
11318169689Skan
11319169689Skan      decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
1132052284Sobrien      if (decl_type == error_mark_node)
1132152284Sobrien	return NULL_TREE;
1132250397Sobrien    }
1132318334Speter
1132490075Sobrien  /* Never do unification on the 'this' parameter.  */
11325169689Skan  decl_arg_types = skip_artificial_parms_for (decl,
11326169689Skan					      TYPE_ARG_TYPES (decl_type));
1132752284Sobrien
11328169689Skan  if (fn_type_unification (fn, explicit_args, targs,
1132950397Sobrien			   decl_arg_types,
1133090075Sobrien			   (check_rettype || DECL_CONV_FN_P (fn)
11331169689Skan			    ? TREE_TYPE (decl_type) : NULL_TREE),
11332169689Skan			   DEDUCE_EXACT, LOOKUP_NORMAL))
1133350397Sobrien    return NULL_TREE;
1133418334Speter
1133550397Sobrien  return targs;
1133650397Sobrien}
1133750397Sobrien
1133852284Sobrien/* Return the innermost template arguments that, when applied to a
1133952284Sobrien   template specialization whose innermost template parameters are
1134090075Sobrien   TPARMS, and whose specialization arguments are PARMS, yield the
11341169689Skan   ARGS.
1134252284Sobrien
1134352284Sobrien   For example, suppose we have:
1134452284Sobrien
1134552284Sobrien     template <class T, class U> struct S {};
1134652284Sobrien     template <class T> struct S<T*, int> {};
1134752284Sobrien
1134852284Sobrien   Then, suppose we want to get `S<double*, int>'.  The TPARMS will be
11349169689Skan   {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
1135052284Sobrien   int}.  The resulting vector will be {double}, indicating that `T'
1135152284Sobrien   is bound to `double'.  */
1135252284Sobrien
1135350397Sobrienstatic tree
11354169689Skanget_class_bindings (tree tparms, tree spec_args, tree args)
1135550397Sobrien{
1135650397Sobrien  int i, ntparms = TREE_VEC_LENGTH (tparms);
11357169689Skan  tree deduced_args;
11358169689Skan  tree innermost_deduced_args;
1135950397Sobrien
11360169689Skan  innermost_deduced_args = make_tree_vec (ntparms);
11361169689Skan  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11362169689Skan    {
11363169689Skan      deduced_args = copy_node (args);
11364169689Skan      SET_TMPL_ARGS_LEVEL (deduced_args,
11365169689Skan			   TMPL_ARGS_DEPTH (deduced_args),
11366169689Skan			   innermost_deduced_args);
11367169689Skan    }
11368169689Skan  else
11369169689Skan    deduced_args = innermost_deduced_args;
11370169689Skan
11371169689Skan  if (unify (tparms, deduced_args,
11372169689Skan	     INNERMOST_TEMPLATE_ARGS (spec_args),
11373169689Skan	     INNERMOST_TEMPLATE_ARGS (args),
11374169689Skan	     UNIFY_ALLOW_NONE))
1137552284Sobrien    return NULL_TREE;
1137618334Speter
1137750397Sobrien  for (i =  0; i < ntparms; ++i)
11378169689Skan    if (! TREE_VEC_ELT (innermost_deduced_args, i))
1137950397Sobrien      return NULL_TREE;
1138018334Speter
11381169689Skan  /* Verify that nondeduced template arguments agree with the type
11382169689Skan     obtained from argument deduction.
11383169689Skan
11384169689Skan     For example:
11385169689Skan
11386169689Skan       struct A { typedef int X; };
11387169689Skan       template <class T, class U> struct C {};
11388169689Skan       template <class T> struct C<T, typename T::X> {};
11389169689Skan
11390169689Skan     Then with the instantiation `C<A, int>', we can deduce that
11391169689Skan     `T' is `A' but unify () does not check whether `typename T::X'
11392169689Skan     is `int'.  */
11393169689Skan  spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11394169689Skan  if (spec_args == error_mark_node
11395169689Skan      /* We only need to check the innermost arguments; the other
11396169689Skan	 arguments will always agree.  */
11397169689Skan      || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11398169689Skan			      INNERMOST_TEMPLATE_ARGS (args)))
1139990075Sobrien    return NULL_TREE;
1140090075Sobrien
11401169689Skan  return deduced_args;
1140250397Sobrien}
1140318334Speter
11404169689Skan/* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
11405169689Skan   Return the TREE_LIST node with the most specialized template, if
11406169689Skan   any.  If there is no most specialized template, the error_mark_node
11407169689Skan   is returned.
1140818334Speter
11409169689Skan   Note that this function does not look at, or modify, the
11410169689Skan   TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
11411169689Skan   returned is one of the elements of INSTANTIATIONS, callers may
11412169689Skan   store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11413169689Skan   and retrieve it from the value returned.  */
11414169689Skan
1141550397Sobrientree
11416169689Skanmost_specialized_instantiation (tree templates)
1141750397Sobrien{
1141852284Sobrien  tree fn, champ;
1141950397Sobrien
11420169689Skan  ++processing_template_decl;
1142118334Speter
11422169689Skan  champ = templates;
11423169689Skan  for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
1142418334Speter    {
11425169689Skan      int fate = 0;
11426169689Skan
11427169689Skan      if (get_bindings (TREE_VALUE (champ),
11428169689Skan			DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11429169689Skan			NULL_TREE, /*check_ret=*/false))
11430169689Skan	fate--;
11431169689Skan
11432169689Skan      if (get_bindings (TREE_VALUE (fn),
11433169689Skan			DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11434169689Skan			NULL_TREE, /*check_ret=*/false))
11435169689Skan	fate++;
11436169689Skan
11437169689Skan      if (fate == -1)
11438169689Skan	champ = fn;
11439169689Skan      else if (!fate)
1144050397Sobrien	{
11441169689Skan	  /* Equally specialized, move to next function.  If there
11442169689Skan	     is no next function, nothing's most specialized.  */
11443169689Skan	  fn = TREE_CHAIN (fn);
1144452284Sobrien	  champ = fn;
11445169689Skan	  if (!fn)
11446169689Skan	    break;
1144750397Sobrien	}
1144818334Speter    }
1144918334Speter
11450169689Skan  if (champ)
11451169689Skan    /* Now verify that champ is better than everything earlier in the
11452169689Skan       instantiation list.  */
11453169689Skan    for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11454169689Skan      if (get_bindings (TREE_VALUE (champ),
11455169689Skan			DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11456169689Skan			NULL_TREE, /*check_ret=*/false)
11457169689Skan	  || !get_bindings (TREE_VALUE (fn),
11458169689Skan			    DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11459169689Skan			    NULL_TREE, /*check_ret=*/false))
11460169689Skan	{
11461169689Skan	  champ = NULL_TREE;
11462169689Skan	  break;
11463169689Skan	}
1146418334Speter
11465169689Skan  processing_template_decl--;
1146618334Speter
11467169689Skan  if (!champ)
11468169689Skan    return error_mark_node;
1146950397Sobrien
11470169689Skan  return champ;
1147152284Sobrien}
1147252284Sobrien
1147352284Sobrien/* If DECL is a specialization of some template, return the most
1147490075Sobrien   general such template.  Otherwise, returns NULL_TREE.
1147552284Sobrien
1147690075Sobrien   For example, given:
1147790075Sobrien
1147852284Sobrien     template <class T> struct S { template <class U> void f(U); };
1147952284Sobrien
1148052284Sobrien   if TMPL is `template <class U> void S<int>::f(U)' this will return
1148152284Sobrien   the full template.  This function will not trace past partial
1148252284Sobrien   specializations, however.  For example, given in addition:
1148352284Sobrien
1148452284Sobrien     template <class T> struct S<T*> { template <class U> void f(U); };
1148552284Sobrien
1148652284Sobrien   if TMPL is `template <class U> void S<int*>::f(U)' this will return
1148752284Sobrien   `template <class T> template <class U> S<T*>::f(U)'.  */
1148852284Sobrien
1148990075Sobrientree
11490132718Skanmost_general_template (tree decl)
1149152284Sobrien{
1149290075Sobrien  /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
1149390075Sobrien     an immediate specialization.  */
1149490075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL)
1149590075Sobrien    {
1149690075Sobrien      if (DECL_TEMPLATE_INFO (decl)) {
1149790075Sobrien	decl = DECL_TI_TEMPLATE (decl);
1149890075Sobrien
1149990075Sobrien	/* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
1150090075Sobrien	   template friend.  */
1150190075Sobrien	if (TREE_CODE (decl) != TEMPLATE_DECL)
1150290075Sobrien	  return NULL_TREE;
1150390075Sobrien      } else
1150490075Sobrien	return NULL_TREE;
1150590075Sobrien    }
1150690075Sobrien
1150790075Sobrien  /* Look for more and more general templates.  */
1150852284Sobrien  while (DECL_TEMPLATE_INFO (decl))
1150990075Sobrien    {
11510132718Skan      /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11511132718Skan	 (See cp-tree.h for details.)  */
1151290075Sobrien      if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
1151390075Sobrien	break;
1151452284Sobrien
11515117395Skan      if (CLASS_TYPE_P (TREE_TYPE (decl))
11516117395Skan	  && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11517117395Skan	break;
11518117395Skan
1151990075Sobrien      /* Stop if we run into an explicitly specialized class template.  */
1152090075Sobrien      if (!DECL_NAMESPACE_SCOPE_P (decl)
1152190075Sobrien	  && DECL_CONTEXT (decl)
1152290075Sobrien	  && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
1152390075Sobrien	break;
1152490075Sobrien
1152590075Sobrien      decl = DECL_TI_TEMPLATE (decl);
1152690075Sobrien    }
1152790075Sobrien
1152852284Sobrien  return decl;
1152952284Sobrien}
1153052284Sobrien
11531169689Skan/* Return the most specialized of the class template partial
11532169689Skan   specializations of TMPL which can produce TYPE, a specialization of
11533169689Skan   TMPL.  The value returned is actually a TREE_LIST; the TREE_TYPE is
11534169689Skan   a _TYPE node corresponding to the partial specialization, while the
11535169689Skan   TREE_PURPOSE is the set of template arguments that must be
11536169689Skan   substituted into the TREE_TYPE in order to generate TYPE.
1153752284Sobrien
11538169689Skan   If the choice of partial specialization is ambiguous, a diagnostic
11539169689Skan   is issued, and the error_mark_node is returned.  If there are no
11540169689Skan   partial specializations of TMPL matching TYPE, then NULL_TREE is
11541169689Skan   returned.  */
11542169689Skan
1154352284Sobrienstatic tree
11544169689Skanmost_specialized_class (tree type, tree tmpl)
1154552284Sobrien{
1154652284Sobrien  tree list = NULL_TREE;
1154752284Sobrien  tree t;
1154852284Sobrien  tree champ;
1154950397Sobrien  int fate;
11550169689Skan  bool ambiguous_p;
11551169689Skan  tree args;
1155218334Speter
1155352284Sobrien  tmpl = most_general_template (tmpl);
11554169689Skan  args = CLASSTYPE_TI_ARGS (type);
1155552284Sobrien  for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
1155618334Speter    {
11557169689Skan      tree partial_spec_args;
11558169689Skan      tree spec_args;
11559169689Skan
11560169689Skan      partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11561169689Skan      spec_args = get_class_bindings (TREE_VALUE (t),
11562169689Skan				      partial_spec_args,
11563169689Skan				      args);
1156452284Sobrien      if (spec_args)
1156550397Sobrien	{
11566169689Skan	  list = tree_cons (spec_args, TREE_VALUE (t), list);
1156750397Sobrien	  TREE_TYPE (list) = TREE_TYPE (t);
1156850397Sobrien	}
1156918334Speter    }
1157050397Sobrien
1157150397Sobrien  if (! list)
1157250397Sobrien    return NULL_TREE;
1157350397Sobrien
11574169689Skan  ambiguous_p = false;
1157550397Sobrien  t = list;
1157650397Sobrien  champ = t;
1157750397Sobrien  t = TREE_CHAIN (t);
1157850397Sobrien  for (; t; t = TREE_CHAIN (t))
1157950397Sobrien    {
11580169689Skan      fate = more_specialized_class (champ, t);
1158150397Sobrien      if (fate == 1)
1158250397Sobrien	;
1158350397Sobrien      else
1158450397Sobrien	{
1158550397Sobrien	  if (fate == 0)
1158650397Sobrien	    {
1158750397Sobrien	      t = TREE_CHAIN (t);
1158850397Sobrien	      if (! t)
11589169689Skan		{
11590169689Skan		  ambiguous_p = true;
11591169689Skan		  break;
11592169689Skan		}
1159350397Sobrien	    }
1159450397Sobrien	  champ = t;
1159550397Sobrien	}
1159650397Sobrien    }
1159750397Sobrien
11598169689Skan  if (!ambiguous_p)
11599169689Skan    for (t = list; t && t != champ; t = TREE_CHAIN (t))
11600169689Skan      {
11601169689Skan	fate = more_specialized_class (champ, t);
11602169689Skan	if (fate != 1)
11603169689Skan	  {
11604169689Skan	    ambiguous_p = true;
11605169689Skan	    break;
11606169689Skan	  }
11607169689Skan      }
11608169689Skan
11609169689Skan  if (ambiguous_p)
1161050397Sobrien    {
11611169689Skan      const char *str = "candidates are:";
11612169689Skan      error ("ambiguous class template instantiation for %q#T", type);
11613169689Skan      for (t = list; t; t = TREE_CHAIN (t))
11614169689Skan	{
11615169689Skan	  error ("%s %+#T", str, TREE_TYPE (t));
11616169689Skan	  str = "               ";
11617169689Skan	}
11618169689Skan      return error_mark_node;
1161950397Sobrien    }
1162050397Sobrien
1162150397Sobrien  return champ;
1162218334Speter}
1162318334Speter
11624117395Skan/* Explicitly instantiate DECL.  */
1162550397Sobrien
1162618334Spetervoid
11627117395Skando_decl_instantiation (tree decl, tree storage)
1162818334Speter{
1162918334Speter  tree result = NULL_TREE;
1163018334Speter  int extern_p = 0;
1163118334Speter
11632169689Skan  if (!decl || decl == error_mark_node)
1163390075Sobrien    /* An error occurred, for which grokdeclarator has already issued
1163490075Sobrien       an appropriate message.  */
1163590075Sobrien    return;
1163690075Sobrien  else if (! DECL_LANG_SPECIFIC (decl))
1163750397Sobrien    {
11638169689Skan      error ("explicit instantiation of non-template %q#D", decl);
1163950397Sobrien      return;
1164050397Sobrien    }
1164152284Sobrien  else if (TREE_CODE (decl) == VAR_DECL)
1164218334Speter    {
1164352284Sobrien      /* There is an asymmetry here in the way VAR_DECLs and
1164452284Sobrien	 FUNCTION_DECLs are handled by grokdeclarator.  In the case of
1164552284Sobrien	 the latter, the DECL we get back will be marked as a
1164652284Sobrien	 template instantiation, and the appropriate
1164752284Sobrien	 DECL_TEMPLATE_INFO will be set up.  This does not happen for
1164852284Sobrien	 VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
1164952284Sobrien	 should handle VAR_DECLs as it currently handles
1165052284Sobrien	 FUNCTION_DECLs.  */
11651132718Skan      result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11652132718Skan      if (!result || TREE_CODE (result) != VAR_DECL)
1165352284Sobrien	{
11654169689Skan	  error ("no matching template for %qD found", decl);
1165552284Sobrien	  return;
1165652284Sobrien	}
1165718334Speter    }
1165850397Sobrien  else if (TREE_CODE (decl) != FUNCTION_DECL)
1165950397Sobrien    {
11660169689Skan      error ("explicit instantiation of %q#D", decl);
1166150397Sobrien      return;
1166250397Sobrien    }
1166352284Sobrien  else
1166450397Sobrien    result = decl;
1166550397Sobrien
1166652284Sobrien  /* Check for various error cases.  Note that if the explicit
11667117395Skan     instantiation is valid the RESULT will currently be marked as an
1166852284Sobrien     *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
1166952284Sobrien     until we get here.  */
1167052284Sobrien
1167152284Sobrien  if (DECL_TEMPLATE_SPECIALIZATION (result))
1167218334Speter    {
11673102780Skan      /* DR 259 [temp.spec].
1167452284Sobrien
11675102780Skan	 Both an explicit instantiation and a declaration of an explicit
11676102780Skan	 specialization shall not appear in a program unless the explicit
11677102780Skan	 instantiation follows a declaration of the explicit specialization.
11678102780Skan
11679102780Skan	 For a given set of template parameters, if an explicit
11680102780Skan	 instantiation of a template appears after a declaration of an
11681102780Skan	 explicit specialization for that template, the explicit
11682102780Skan	 instantiation has no effect.  */
1168318334Speter      return;
1168418334Speter    }
1168552284Sobrien  else if (DECL_EXPLICIT_INSTANTIATION (result))
1168652284Sobrien    {
1168752284Sobrien      /* [temp.spec]
1168818334Speter
1168952284Sobrien	 No program shall explicitly instantiate any template more
11690169689Skan	 than once.
1169152284Sobrien
11692169689Skan	 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11693169689Skan	 the first instantiation was `extern' and the second is not,
11694169689Skan	 and EXTERN_P for the opposite case.  */
11695169689Skan      if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11696169689Skan	pedwarn ("duplicate explicit instantiation of %q#D", result);
11697169689Skan      /* If an "extern" explicit instantiation follows an ordinary
11698169689Skan	 explicit instantiation, the template is instantiated.  */
11699169689Skan      if (extern_p)
1170052284Sobrien	return;
1170152284Sobrien    }
1170252284Sobrien  else if (!DECL_IMPLICIT_INSTANTIATION (result))
1170350397Sobrien    {
11704169689Skan      error ("no matching template for %qD found", result);
1170552284Sobrien      return;
1170652284Sobrien    }
1170752284Sobrien  else if (!DECL_TEMPLATE_INFO (result))
1170852284Sobrien    {
11709169689Skan      pedwarn ("explicit instantiation of non-template %q#D", result);
1171050397Sobrien      return;
1171150397Sobrien    }
1171250397Sobrien
1171318334Speter  if (storage == NULL_TREE)
1171418334Speter    ;
1171518334Speter  else if (storage == ridpointers[(int) RID_EXTERN])
1171652284Sobrien    {
1171796263Sobrien      if (pedantic && !in_system_header)
11718169689Skan	pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11719169689Skan		 "instantiations");
1172052284Sobrien      extern_p = 1;
1172152284Sobrien    }
1172218334Speter  else
11723169689Skan    error ("storage class %qD applied to template instantiation", storage);
1172450397Sobrien
11725169689Skan  check_explicit_instantiation_namespace (result);
1172650397Sobrien  mark_decl_instantiated (result, extern_p);
1172750397Sobrien  if (! extern_p)
11728169689Skan    instantiate_decl (result, /*defer_ok=*/1,
11729169689Skan		      /*expl_inst_class_mem_p=*/false);
1173018334Speter}
1173118334Speter
11732169689Skanstatic void
11733132718Skanmark_class_instantiated (tree t, int extern_p)
1173418334Speter{
1173518334Speter  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
1173618334Speter  SET_CLASSTYPE_INTERFACE_KNOWN (t);
1173718334Speter  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
1173818334Speter  TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
1173918334Speter  if (! extern_p)
1174018334Speter    {
1174118334Speter      CLASSTYPE_DEBUG_REQUESTED (t) = 1;
1174218334Speter      rest_of_type_compilation (t, 1);
1174318334Speter    }
11744169689Skan}
1174518334Speter
11746117395Skan/* Called from do_type_instantiation through binding_table_foreach to
11747132718Skan   do recursive instantiation for the type bound in ENTRY.  */
11748117395Skanstatic void
11749117395Skanbt_instantiate_type_proc (binding_entry entry, void *data)
11750117395Skan{
11751117395Skan  tree storage = *(tree *) data;
11752117395Skan
11753117395Skan  if (IS_AGGR_TYPE (entry->type)
11754117395Skan      && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11755117395Skan    do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11756117395Skan}
11757117395Skan
11758169689Skan/* Called from do_type_instantiation to instantiate a member
11759169689Skan   (a member function or a static member variable) of an
11760169689Skan   explicitly instantiated class template.  */
11761169689Skanstatic void
11762169689Skaninstantiate_class_member (tree decl, int extern_p)
11763169689Skan{
11764169689Skan  mark_decl_instantiated (decl, extern_p);
11765169689Skan  if (! extern_p)
11766169689Skan    instantiate_decl (decl, /*defer_ok=*/1,
11767169689Skan		      /*expl_inst_class_mem_p=*/true);
11768169689Skan}
11769169689Skan
1177090075Sobrien/* Perform an explicit instantiation of template class T.  STORAGE, if
1177190075Sobrien   non-null, is the RID for extern, inline or static.  COMPLAIN is
11772117395Skan   nonzero if this is called from the parser, zero if called recursively,
1177390075Sobrien   since the standard is unclear (as detailed below).  */
11774169689Skan
1177518334Spetervoid
11776132718Skando_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
1177718334Speter{
1177818334Speter  int extern_p = 0;
1177918334Speter  int nomem_p = 0;
1178050397Sobrien  int static_p = 0;
11781169689Skan  int previous_instantiation_extern_p = 0;
1178218334Speter
1178350397Sobrien  if (TREE_CODE (t) == TYPE_DECL)
1178450397Sobrien    t = TREE_TYPE (t);
1178550397Sobrien
1178652284Sobrien  if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
1178750397Sobrien    {
11788169689Skan      error ("explicit instantiation of non-template type %qT", t);
1178950397Sobrien      return;
1179050397Sobrien    }
1179150397Sobrien
1179250397Sobrien  complete_type (t);
1179350397Sobrien
1179490075Sobrien  if (!COMPLETE_TYPE_P (t))
1179518334Speter    {
1179696263Sobrien      if (complain & tf_error)
11797169689Skan	error ("explicit instantiation of %q#T before definition of template",
11798169689Skan	       t);
1179918334Speter      return;
1180018334Speter    }
1180118334Speter
1180252284Sobrien  if (storage != NULL_TREE)
1180318334Speter    {
1180496263Sobrien      if (pedantic && !in_system_header)
11805169689Skan	pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11806169689Skan		storage);
1180752284Sobrien
1180852284Sobrien      if (storage == ridpointers[(int) RID_INLINE])
1180952284Sobrien	nomem_p = 1;
1181052284Sobrien      else if (storage == ridpointers[(int) RID_EXTERN])
1181152284Sobrien	extern_p = 1;
1181252284Sobrien      else if (storage == ridpointers[(int) RID_STATIC])
1181352284Sobrien	static_p = 1;
1181452284Sobrien      else
1181552284Sobrien	{
11816169689Skan	  error ("storage class %qD applied to template instantiation",
11817169689Skan		 storage);
1181852284Sobrien	  extern_p = 0;
1181952284Sobrien	}
1182018334Speter    }
1182118334Speter
1182252284Sobrien  if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
1182352284Sobrien    {
11824102780Skan      /* DR 259 [temp.spec].
1182518334Speter
11826102780Skan	 Both an explicit instantiation and a declaration of an explicit
11827102780Skan	 specialization shall not appear in a program unless the explicit
11828102780Skan	 instantiation follows a declaration of the explicit specialization.
11829102780Skan
11830102780Skan	 For a given set of template parameters, if an explicit
11831102780Skan	 instantiation of a template appears after a declaration of an
11832102780Skan	 explicit specialization for that template, the explicit
11833102780Skan	 instantiation has no effect.  */
1183452284Sobrien      return;
1183552284Sobrien    }
1183652284Sobrien  else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
1183718334Speter    {
1183852284Sobrien      /* [temp.spec]
1183952284Sobrien
1184052284Sobrien	 No program shall explicitly instantiate any template more
11841169689Skan	 than once.
1184252284Sobrien
11843169689Skan	 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11844169689Skan	 instantiation was `extern'.  If EXTERN_P then the second is.
11845169689Skan	 These cases are OK.  */
11846169689Skan      previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11847169689Skan
11848169689Skan      if (!previous_instantiation_extern_p && !extern_p
1184996263Sobrien	  && (complain & tf_error))
11850169689Skan	pedwarn ("duplicate explicit instantiation of %q#T", t);
11851169689Skan
1185252284Sobrien      /* If we've already instantiated the template, just return now.  */
1185352284Sobrien      if (!CLASSTYPE_INTERFACE_ONLY (t))
1185452284Sobrien	return;
1185518334Speter    }
1185618334Speter
11857169689Skan  check_explicit_instantiation_namespace (TYPE_NAME (t));
1185852284Sobrien  mark_class_instantiated (t, extern_p);
1185952284Sobrien
1186018334Speter  if (nomem_p)
1186118334Speter    return;
1186218334Speter
1186318334Speter  {
1186418334Speter    tree tmp;
1186518334Speter
1186652284Sobrien    /* In contrast to implicit instantiation, where only the
1186752284Sobrien       declarations, and not the definitions, of members are
1186852284Sobrien       instantiated, we have here:
1186952284Sobrien
11870169689Skan	 [temp.explicit]
1187152284Sobrien
1187252284Sobrien	 The explicit instantiation of a class template specialization
1187352284Sobrien	 implies the instantiation of all of its members not
1187452284Sobrien	 previously explicitly specialized in the translation unit
11875169689Skan	 containing the explicit instantiation.
1187652284Sobrien
1187752284Sobrien       Of course, we can't instantiate member template classes, since
1187852284Sobrien       we don't have any arguments for them.  Note that the standard
1187990075Sobrien       is unclear on whether the instantiation of the members are
11880169689Skan       *explicit* instantiations or not.  However, the most natural
11881169689Skan       interpretation is that it should be an explicit instantiation.  */
1188252284Sobrien
1188350397Sobrien    if (! static_p)
1188450397Sobrien      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
1188550397Sobrien	if (TREE_CODE (tmp) == FUNCTION_DECL
1188650397Sobrien	    && DECL_TEMPLATE_INSTANTIATION (tmp))
11887169689Skan	  instantiate_class_member (tmp, extern_p);
1188850397Sobrien
1188950397Sobrien    for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
1189050397Sobrien      if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11891169689Skan	instantiate_class_member (tmp, extern_p);
1189218334Speter
11893132718Skan    if (CLASSTYPE_NESTED_UTDS (t))
11894132718Skan      binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11895169689Skan			     bt_instantiate_type_proc, &storage);
1189618334Speter  }
1189718334Speter}
1189818334Speter
1189952284Sobrien/* Given a function DECL, which is a specialization of TMPL, modify
1190052284Sobrien   DECL to be a re-instantiation of TMPL with the same template
1190152284Sobrien   arguments.  TMPL should be the template into which tsubst'ing
1190252284Sobrien   should occur for DECL, not the most general template.
1190350397Sobrien
1190450397Sobrien   One reason for doing this is a scenario like this:
1190550397Sobrien
1190650397Sobrien     template <class T>
1190750397Sobrien     void f(const T&, int i);
1190850397Sobrien
1190950397Sobrien     void g() { f(3, 7); }
1191050397Sobrien
1191150397Sobrien     template <class T>
1191250397Sobrien     void f(const T& t, const int i) { }
1191350397Sobrien
1191450397Sobrien   Note that when the template is first instantiated, with
1191550397Sobrien   instantiate_template, the resulting DECL will have no name for the
1191650397Sobrien   first parameter, and the wrong type for the second.  So, when we go
1191750397Sobrien   to instantiate the DECL, we regenerate it.  */
1191850397Sobrien
1191952284Sobrienstatic void
11920132718Skanregenerate_decl_from_template (tree decl, tree tmpl)
1192150397Sobrien{
1192290075Sobrien  /* The arguments used to instantiate DECL, from the most general
1192390075Sobrien     template.  */
1192450397Sobrien  tree args;
1192550397Sobrien  tree code_pattern;
1192650397Sobrien
1192750397Sobrien  args = DECL_TI_ARGS (decl);
1192850397Sobrien  code_pattern = DECL_TEMPLATE_RESULT (tmpl);
1192950397Sobrien
11930132718Skan  /* Make sure that we can see identifiers, and compute access
11931132718Skan     correctly.  */
11932132718Skan  push_access_scope (decl);
1193352284Sobrien
11934169689Skan  if (TREE_CODE (decl) == FUNCTION_DECL)
1193550397Sobrien    {
11936169689Skan      tree decl_parm;
11937169689Skan      tree pattern_parm;
11938169689Skan      tree specs;
11939169689Skan      int args_depth;
11940169689Skan      int parms_depth;
1194150397Sobrien
11942169689Skan      args_depth = TMPL_ARGS_DEPTH (args);
11943169689Skan      parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11944169689Skan      if (args_depth > parms_depth)
11945169689Skan	args = get_innermost_template_args (args, parms_depth);
11946117395Skan
11947169689Skan      specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11948169689Skan					      args, tf_error, NULL_TREE);
11949169689Skan      if (specs)
11950169689Skan	TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11951169689Skan						    specs);
1195252284Sobrien
11953169689Skan      /* Merge parameter declarations.  */
11954169689Skan      decl_parm = skip_artificial_parms_for (decl,
11955169689Skan					     DECL_ARGUMENTS (decl));
11956169689Skan      pattern_parm
11957169689Skan	= skip_artificial_parms_for (code_pattern,
11958169689Skan				     DECL_ARGUMENTS (code_pattern));
11959169689Skan      while (decl_parm)
11960169689Skan	{
11961169689Skan	  tree parm_type;
11962169689Skan	  tree attributes;
1196350397Sobrien
11964169689Skan	  if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11965169689Skan	    DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11966169689Skan	  parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11967169689Skan			      NULL_TREE);
11968169689Skan	  parm_type = type_decays_to (parm_type);
11969169689Skan	  if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11970169689Skan	    TREE_TYPE (decl_parm) = parm_type;
11971169689Skan	  attributes = DECL_ATTRIBUTES (pattern_parm);
11972169689Skan	  if (DECL_ATTRIBUTES (decl_parm) != attributes)
11973169689Skan	    {
11974169689Skan	      DECL_ATTRIBUTES (decl_parm) = attributes;
11975169689Skan	      cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11976169689Skan	    }
11977169689Skan	  decl_parm = TREE_CHAIN (decl_parm);
11978169689Skan	  pattern_parm = TREE_CHAIN (pattern_parm);
11979169689Skan	}
11980169689Skan
11981169689Skan      /* Merge additional specifiers from the CODE_PATTERN.  */
11982169689Skan      if (DECL_DECLARED_INLINE_P (code_pattern)
11983169689Skan	  && !DECL_DECLARED_INLINE_P (decl))
11984169689Skan	DECL_DECLARED_INLINE_P (decl) = 1;
11985169689Skan      if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11986169689Skan	DECL_INLINE (decl) = 1;
11987169689Skan    }
11988169689Skan  else if (TREE_CODE (decl) == VAR_DECL)
11989169689Skan    DECL_INITIAL (decl) =
11990169689Skan      tsubst_expr (DECL_INITIAL (code_pattern), args,
11991169689Skan		   tf_error, DECL_TI_TEMPLATE (decl),
11992169689Skan		   /*integral_constant_expression_p=*/false);
11993169689Skan  else
11994169689Skan    gcc_unreachable ();
11995169689Skan
11996169689Skan  pop_access_scope (decl);
1199750397Sobrien}
1199850397Sobrien
11999132718Skan/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
12000132718Skan   substituted to get DECL.  */
12001132718Skan
12002132718Skantree
12003132718Skantemplate_for_substitution (tree decl)
12004132718Skan{
12005132718Skan  tree tmpl = DECL_TI_TEMPLATE (decl);
12006132718Skan
12007132718Skan  /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
12008132718Skan     for the instantiation.  This is not always the most general
12009132718Skan     template.  Consider, for example:
12010132718Skan
12011169689Skan	template <class T>
12012132718Skan	struct S { template <class U> void f();
12013169689Skan		   template <> void f<int>(); };
12014132718Skan
12015132718Skan     and an instantiation of S<double>::f<int>.  We want TD to be the
12016132718Skan     specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
12017132718Skan  while (/* An instantiation cannot have a definition, so we need a
12018132718Skan	    more general template.  */
12019132718Skan	 DECL_TEMPLATE_INSTANTIATION (tmpl)
12020132718Skan	   /* We must also deal with friend templates.  Given:
12021132718Skan
12022169689Skan		template <class T> struct S {
12023132718Skan		  template <class U> friend void f() {};
12024132718Skan		};
12025132718Skan
12026132718Skan	      S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
12027132718Skan	      so far as the language is concerned, but that's still
12028132718Skan	      where we get the pattern for the instantiation from.  On
12029132718Skan	      other hand, if the definition comes outside the class, say:
12030132718Skan
12031169689Skan		template <class T> struct S {
12032132718Skan		  template <class U> friend void f();
12033132718Skan		};
12034132718Skan		template <class U> friend void f() {}
12035132718Skan
12036132718Skan	      we don't need to look any further.  That's what the check for
12037132718Skan	      DECL_INITIAL is for.  */
12038132718Skan	  || (TREE_CODE (decl) == FUNCTION_DECL
12039132718Skan	      && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
12040132718Skan	      && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
12041132718Skan    {
12042132718Skan      /* The present template, TD, should not be a definition.  If it
12043132718Skan	 were a definition, we should be using it!  Note that we
12044132718Skan	 cannot restructure the loop to just keep going until we find
12045132718Skan	 a template with a definition, since that might go too far if
12046132718Skan	 a specialization was declared, but not defined.  */
12047169689Skan      gcc_assert (TREE_CODE (decl) != VAR_DECL
12048169689Skan		  || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
12049169689Skan
12050132718Skan      /* Fetch the more general template.  */
12051132718Skan      tmpl = DECL_TI_TEMPLATE (tmpl);
12052132718Skan    }
12053132718Skan
12054132718Skan  return tmpl;
12055132718Skan}
12056132718Skan
1205790075Sobrien/* Produce the definition of D, a _DECL generated from a template.  If
12058117395Skan   DEFER_OK is nonzero, then we don't have to actually do the
12059169689Skan   instantiation now; we just have to do it sometime.  Normally it is
12060169689Skan   an error if this is an explicit instantiation but D is undefined.
12061169689Skan   EXPL_INST_CLASS_MEM_P is true iff D is a member of an
12062169689Skan   explicitly instantiated class template.  */
1206350397Sobrien
1206418334Spetertree
12065169689Skaninstantiate_decl (tree d, int defer_ok,
12066169689Skan		  bool expl_inst_class_mem_p)
1206718334Speter{
1206852284Sobrien  tree tmpl = DECL_TI_TEMPLATE (d);
12069117395Skan  tree gen_args;
12070117395Skan  tree args;
1207150397Sobrien  tree td;
1207252284Sobrien  tree code_pattern;
1207352284Sobrien  tree spec;
1207452284Sobrien  tree gen_tmpl;
12075169689Skan  bool pattern_defined;
1207690075Sobrien  int need_push;
12077132718Skan  location_t saved_loc = input_location;
12078169689Skan  int saved_in_system_header = in_system_header;
12079169689Skan  bool external_p;
12080169689Skan
1208152284Sobrien  /* This function should only be used to instantiate templates for
1208252284Sobrien     functions and static member variables.  */
12083169689Skan  gcc_assert (TREE_CODE (d) == FUNCTION_DECL
12084169689Skan	      || TREE_CODE (d) == VAR_DECL);
1208518334Speter
12086132718Skan  /* Variables are never deferred; if instantiation is required, they
12087132718Skan     are instantiated right away.  That allows for better code in the
12088132718Skan     case that an expression refers to the value of the variable --
12089132718Skan     if the variable has a constant value the referring expression can
12090132718Skan     take advantage of that fact.  */
12091117395Skan  if (TREE_CODE (d) == VAR_DECL)
12092117395Skan    defer_ok = 0;
12093117395Skan
1209490075Sobrien  /* Don't instantiate cloned functions.  Instead, instantiate the
1209590075Sobrien     functions they cloned.  */
1209690075Sobrien  if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
1209790075Sobrien    d = DECL_CLONED_FUNCTION (d);
1209890075Sobrien
1209952284Sobrien  if (DECL_TEMPLATE_INSTANTIATED (d))
1210052284Sobrien    /* D has already been instantiated.  It might seem reasonable to
1210190075Sobrien       check whether or not D is an explicit instantiation, and, if so,
1210252284Sobrien       stop here.  But when an explicit instantiation is deferred
1210352284Sobrien       until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
1210452284Sobrien       is set, even though we still need to do the instantiation.  */
1210552284Sobrien    return d;
1210650397Sobrien
1210752284Sobrien  /* If we already have a specialization of this declaration, then
1210852284Sobrien     there's no reason to instantiate it.  Note that
1210952284Sobrien     retrieve_specialization gives us both instantiations and
1211052284Sobrien     specializations, so we must explicitly check
1211152284Sobrien     DECL_TEMPLATE_SPECIALIZATION.  */
1211252284Sobrien  gen_tmpl = most_general_template (tmpl);
12113117395Skan  gen_args = DECL_TI_ARGS (d);
12114169689Skan  spec = retrieve_specialization (gen_tmpl, gen_args,
12115169689Skan				  /*class_specializations_p=*/false);
1211652284Sobrien  if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
1211752284Sobrien    return spec;
1211850397Sobrien
1211952284Sobrien  /* This needs to happen before any tsubsting.  */
1212052284Sobrien  if (! push_tinst_level (d))
1212150397Sobrien    return d;
1212250397Sobrien
1212390075Sobrien  timevar_push (TV_PARSE);
1212490075Sobrien
1212552284Sobrien  /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
12126132718Skan     for the instantiation.  */
12127132718Skan  td = template_for_substitution (d);
1212852284Sobrien  code_pattern = DECL_TEMPLATE_RESULT (td);
1212950397Sobrien
12130169689Skan  /* We should never be trying to instantiate a member of a class
12131169689Skan     template or partial specialization.  */
12132169689Skan  gcc_assert (d != code_pattern);
12133169689Skan
12134117395Skan  if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
12135117395Skan      || DECL_TEMPLATE_SPECIALIZATION (td))
12136117395Skan    /* In the case of a friend template whose definition is provided
12137117395Skan       outside the class, we may have too many arguments.  Drop the
12138117395Skan       ones we don't need.  The same is true for specializations.  */
12139117395Skan    args = get_innermost_template_args
12140117395Skan      (gen_args, TMPL_PARMS_DEPTH  (DECL_TEMPLATE_PARMS (td)));
12141117395Skan  else
12142117395Skan    args = gen_args;
12143117395Skan
1214452284Sobrien  if (TREE_CODE (d) == FUNCTION_DECL)
1214590075Sobrien    pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
1214652284Sobrien  else
1214752284Sobrien    pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
1214852284Sobrien
12149169689Skan  /* We may be in the middle of deferred access check.  Disable it now.  */
12150169689Skan  push_deferring_access_checks (dk_no_deferred);
12151169689Skan
12152169689Skan  /* Unless an explicit instantiation directive has already determined
12153169689Skan     the linkage of D, remember that a definition is available for
12154169689Skan     this entity.  */
12155169689Skan  if (pattern_defined
12156169689Skan      && !DECL_INTERFACE_KNOWN (d)
12157169689Skan      && !DECL_NOT_REALLY_EXTERN (d))
12158169689Skan    mark_definable (d);
12159169689Skan
12160132718Skan  input_location = DECL_SOURCE_LOCATION (d);
12161169689Skan  in_system_header = DECL_IN_SYSTEM_HEADER (d);
1216250397Sobrien
12163169689Skan  /* If D is a member of an explicitly instantiated class template,
12164169689Skan     and no definition is available, treat it like an implicit
12165169689Skan     instantiation.  */
12166169689Skan  if (!pattern_defined && expl_inst_class_mem_p
12167169689Skan      && DECL_EXPLICIT_INSTANTIATION (d))
1216850397Sobrien    {
12169169689Skan      DECL_NOT_REALLY_EXTERN (d) = 0;
12170169689Skan      DECL_INTERFACE_KNOWN (d) = 0;
12171169689Skan      SET_DECL_IMPLICIT_INSTANTIATION (d);
1217250397Sobrien    }
1217350397Sobrien
1217496263Sobrien  if (!defer_ok)
1217596263Sobrien    {
1217696263Sobrien      /* Recheck the substitutions to obtain any warning messages
1217796263Sobrien	 about ignoring cv qualifiers.  */
1217896263Sobrien      tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
1217996263Sobrien      tree type = TREE_TYPE (gen);
1218096263Sobrien
12181117395Skan      /* Make sure that we can see identifiers, and compute access
12182117395Skan	 correctly.  D is already the target FUNCTION_DECL with the
12183117395Skan	 right context.  */
12184117395Skan      push_access_scope (d);
12185117395Skan
1218696263Sobrien      if (TREE_CODE (gen) == FUNCTION_DECL)
1218796263Sobrien	{
12188169689Skan	  tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
12189117395Skan	  tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
12190169689Skan		  tf_warning_or_error, d);
1219196263Sobrien	  /* Don't simply tsubst the function type, as that will give
1219296263Sobrien	     duplicate warnings about poor parameter qualifications.
1219396263Sobrien	     The function arguments are the same as the decl_arguments
12194117395Skan	     without the top level cv qualifiers.  */
1219596263Sobrien	  type = TREE_TYPE (type);
1219696263Sobrien	}
12197169689Skan      tsubst (type, gen_args, tf_warning_or_error, d);
12198117395Skan
12199117395Skan      pop_access_scope (d);
1220096263Sobrien    }
12201169689Skan
12202169689Skan  /* Check to see whether we know that this template will be
12203169689Skan     instantiated in some other file, as with "extern template"
12204169689Skan     extension.  */
12205169689Skan  external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12206169689Skan  /* In general, we do not instantiate such templates...  */
12207169689Skan  if (external_p
12208169689Skan      /* ... but we instantiate inline functions so that we can inline
12209169689Skan	 them and ... */
12210169689Skan      && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12211169689Skan      /* ... we instantiate static data members whose values are
12212169689Skan	 needed in integral constant expressions.  */
12213169689Skan      && ! (TREE_CODE (d) == VAR_DECL
12214169689Skan	    && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
1221550397Sobrien    goto out;
1221690075Sobrien  /* Defer all other templates, unless we have been explicitly
12217169689Skan     forbidden from doing so.  */
12218169689Skan  if (/* If there is no definition, we cannot instantiate the
12219169689Skan	 template.  */
12220169689Skan      ! pattern_defined
12221169689Skan      /* If it's OK to postpone instantiation, do so.  */
12222169689Skan      || defer_ok
12223169689Skan      /* If this is a static data member that will be defined
12224169689Skan	 elsewhere, we don't want to instantiate the entire data
12225169689Skan	 member, but we do want to instantiate the initializer so that
12226169689Skan	 we can substitute that elsewhere.  */
12227169689Skan      || (external_p && TREE_CODE (d) == VAR_DECL))
1222850397Sobrien    {
12229169689Skan      /* The definition of the static data member is now required so
12230169689Skan	 we must substitute the initializer.  */
12231169689Skan      if (TREE_CODE (d) == VAR_DECL
12232169689Skan	  && !DECL_INITIAL (d)
12233169689Skan	  && DECL_INITIAL (code_pattern))
12234169689Skan	{
12235169689Skan	  tree ns;
12236169689Skan	  tree init;
12237169689Skan
12238169689Skan	  ns = decl_namespace_context (d);
12239169689Skan	  push_nested_namespace (ns);
12240169689Skan	  push_nested_class (DECL_CONTEXT (d));
12241169689Skan	  init = tsubst_expr (DECL_INITIAL (code_pattern),
12242169689Skan			      args,
12243169689Skan			      tf_warning_or_error, NULL_TREE,
12244169689Skan			      /*integral_constant_expression_p=*/false);
12245169689Skan	  cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12246169689Skan			  /*asmspec_tree=*/NULL_TREE,
12247169689Skan			  LOOKUP_ONLYCONVERTING);
12248169689Skan	  pop_nested_class ();
12249169689Skan	  pop_nested_namespace (ns);
12250169689Skan	}
12251169689Skan
12252169689Skan      /* We restore the source position here because it's used by
12253169689Skan	 add_pending_template.  */
12254132718Skan      input_location = saved_loc;
1225550397Sobrien
12256169689Skan      if (at_eof && !pattern_defined
1225752284Sobrien	  && DECL_EXPLICIT_INSTANTIATION (d))
1225852284Sobrien	/* [temp.explicit]
1225952284Sobrien
1226052284Sobrien	   The definition of a non-exported function template, a
1226152284Sobrien	   non-exported member function template, or a non-exported
1226252284Sobrien	   member function or static data member of a class template
1226352284Sobrien	   shall be present in every translation unit in which it is
1226452284Sobrien	   explicitly instantiated.  */
1226590075Sobrien	pedwarn
12266169689Skan	  ("explicit instantiation of %qD but no definition available", d);
1226752284Sobrien
12268169689Skan      /* ??? Historically, we have instantiated inline functions, even
12269169689Skan	 when marked as "extern template".  */
12270169689Skan      if (!(external_p && TREE_CODE (d) == VAR_DECL))
12271169689Skan	add_pending_template (d);
1227250397Sobrien      goto out;
1227350397Sobrien    }
12274169689Skan  /* Tell the repository that D is available in this translation unit
12275169689Skan     -- and see if it is supposed to be instantiated here.  */
12276169689Skan  if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12277169689Skan    {
12278169689Skan      /* In a PCH file, despite the fact that the repository hasn't
12279169689Skan	 requested instantiation in the PCH it is still possible that
12280169689Skan	 an instantiation will be required in a file that includes the
12281169689Skan	 PCH.  */
12282169689Skan      if (pch_file)
12283169689Skan	add_pending_template (d);
12284169689Skan      /* Instantiate inline functions so that the inliner can do its
12285169689Skan	 job, even though we'll not be emitting a copy of this
12286169689Skan	 function.  */
12287169689Skan      if (!(TREE_CODE (d) == FUNCTION_DECL
12288169689Skan	    && flag_inline_trees
12289169689Skan	    && DECL_DECLARED_INLINE_P (d)))
12290169689Skan	goto out;
12291169689Skan    }
1229250397Sobrien
12293169689Skan  need_push = !cfun || !global_bindings_p ();
1229490075Sobrien  if (need_push)
1229590075Sobrien    push_to_top_level ();
1229690075Sobrien
12297132718Skan  /* Mark D as instantiated so that recursive calls to
12298132718Skan     instantiate_decl do not try to instantiate it again.  */
12299132718Skan  DECL_TEMPLATE_INSTANTIATED (d) = 1;
12300132718Skan
1230152284Sobrien  /* Regenerate the declaration in case the template has been modified
1230252284Sobrien     by a subsequent redeclaration.  */
1230350397Sobrien  regenerate_decl_from_template (d, td);
12304169689Skan
1230550397Sobrien  /* We already set the file and line above.  Reset them now in case
12306169689Skan     they changed as a result of calling regenerate_decl_from_template.  */
12307132718Skan  input_location = DECL_SOURCE_LOCATION (d);
1230850397Sobrien
1230950397Sobrien  if (TREE_CODE (d) == VAR_DECL)
1231050397Sobrien    {
12311169689Skan      tree init;
12312169689Skan
12313117395Skan      /* Clear out DECL_RTL; whatever was there before may not be right
12314117395Skan	 since we've reset the type of the declaration.  */
12315117395Skan      SET_DECL_RTL (d, NULL_RTX);
1231650397Sobrien      DECL_IN_AGGR_P (d) = 0;
12317117395Skan
12318169689Skan      /* The initializer is placed in DECL_INITIAL by
12319169689Skan	 regenerate_decl_from_template.  Pull it out so that
12320169689Skan	 finish_decl can process it.  */
12321169689Skan      init = DECL_INITIAL (d);
12322169689Skan      DECL_INITIAL (d) = NULL_TREE;
12323169689Skan      DECL_INITIALIZED_P (d) = 0;
12324117395Skan
12325169689Skan      /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12326169689Skan	 initializer.  That function will defer actual emission until
12327169689Skan	 we have a chance to determine linkage.  */
12328169689Skan      DECL_EXTERNAL (d) = 0;
12329117395Skan
12330169689Skan      /* Enter the scope of D so that access-checking works correctly.  */
12331169689Skan      push_nested_class (DECL_CONTEXT (d));
12332169689Skan      finish_decl (d, init, NULL_TREE);
12333169689Skan      pop_nested_class ();
1233450397Sobrien    }
1233550397Sobrien  else if (TREE_CODE (d) == FUNCTION_DECL)
1233650397Sobrien    {
1233790075Sobrien      htab_t saved_local_specializations;
12338132718Skan      tree subst_decl;
12339132718Skan      tree tmpl_parm;
12340132718Skan      tree spec_parm;
1234150397Sobrien
1234290075Sobrien      /* Save away the current list, in case we are instantiating one
1234390075Sobrien	 template from within the body of another.  */
1234490075Sobrien      saved_local_specializations = local_specializations;
1234550397Sobrien
1234690075Sobrien      /* Set up the list of local specializations.  */
12347169689Skan      local_specializations = htab_create (37,
12348132718Skan					   hash_local_specialization,
12349132718Skan					   eq_local_specializations,
1235090075Sobrien					   NULL);
1235150397Sobrien
1235290075Sobrien      /* Set up context.  */
12353169689Skan      start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
1235450397Sobrien
12355132718Skan      /* Create substitution entries for the parameters.  */
12356132718Skan      subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12357132718Skan      tmpl_parm = DECL_ARGUMENTS (subst_decl);
12358132718Skan      spec_parm = DECL_ARGUMENTS (d);
12359132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12360132718Skan	{
12361132718Skan	  register_local_specialization (spec_parm, tmpl_parm);
12362132718Skan	  spec_parm = skip_artificial_parms_for (d, spec_parm);
12363132718Skan	  tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12364132718Skan	}
12365132718Skan      while (tmpl_parm)
12366132718Skan	{
12367132718Skan	  register_local_specialization (spec_parm, tmpl_parm);
12368132718Skan	  tmpl_parm = TREE_CHAIN (tmpl_parm);
12369132718Skan	  spec_parm = TREE_CHAIN (spec_parm);
12370132718Skan	}
12371169689Skan      gcc_assert (!spec_parm);
12372132718Skan
1237390075Sobrien      /* Substitute into the body of the function.  */
1237490075Sobrien      tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
12375169689Skan		   tf_warning_or_error, tmpl,
12376169689Skan		   /*integral_constant_expression_p=*/false);
1237750397Sobrien
1237890075Sobrien      /* We don't need the local specializations any more.  */
1237990075Sobrien      htab_delete (local_specializations);
1238090075Sobrien      local_specializations = saved_local_specializations;
1238150397Sobrien
1238290075Sobrien      /* Finish the function.  */
12383117395Skan      d = finish_function (0);
12384132718Skan      expand_or_defer_fn (d);
1238550397Sobrien    }
1238650397Sobrien
12387169689Skan  /* We're not deferring instantiation any more.  */
12388169689Skan  TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12389169689Skan
1239090075Sobrien  if (need_push)
1239190075Sobrien    pop_from_top_level ();
1239290075Sobrien
1239350397Sobrienout:
12394132718Skan  input_location = saved_loc;
12395169689Skan  in_system_header = saved_in_system_header;
12396132718Skan  pop_deferring_access_checks ();
1239750397Sobrien  pop_tinst_level ();
1239850397Sobrien
1239990075Sobrien  timevar_pop (TV_PARSE);
1240090075Sobrien
1240118334Speter  return d;
1240218334Speter}
1240350397Sobrien
1240452284Sobrien/* Run through the list of templates that we wish we could
12405169689Skan   instantiate, and instantiate any we can.  RETRIES is the
12406169689Skan   number of times we retry pending template instantiation.  */
1240752284Sobrien
12408169689Skanvoid
12409169689Skaninstantiate_pending_templates (int retries)
1241050397Sobrien{
1241152284Sobrien  tree *t;
1241290075Sobrien  tree last = NULL_TREE;
1241352284Sobrien  int reconsider;
12414132718Skan  location_t saved_loc = input_location;
12415169689Skan  int saved_in_system_header = in_system_header;
12416169689Skan
12417169689Skan  /* Instantiating templates may trigger vtable generation.  This in turn
12418169689Skan     may require further template instantiations.  We place a limit here
12419169689Skan     to avoid infinite loop.  */
12420169689Skan  if (pending_templates && retries >= max_tinst_depth)
1242150397Sobrien    {
12422169689Skan      tree decl = TREE_VALUE (pending_templates);
12423169689Skan
12424169689Skan      error ("template instantiation depth exceeds maximum of %d"
12425169689Skan	     " instantiating %q+D, possibly from virtual table generation"
12426169689Skan	     " (use -ftemplate-depth-NN to increase the maximum)",
12427169689Skan	     max_tinst_depth, decl);
12428169689Skan      if (TREE_CODE (decl) == FUNCTION_DECL)
12429169689Skan	/* Pretend that we defined it.  */
12430169689Skan	DECL_INITIAL (decl) = error_mark_node;
12431169689Skan      return;
12432169689Skan    }
12433169689Skan
12434169689Skan  do
12435169689Skan    {
1243652284Sobrien      reconsider = 0;
1243750397Sobrien
1243852284Sobrien      t = &pending_templates;
1243952284Sobrien      while (*t)
1244050397Sobrien	{
1244152284Sobrien	  tree instantiation = TREE_VALUE (*t);
1244252284Sobrien
1244390075Sobrien	  reopen_tinst_level (TREE_PURPOSE (*t));
1244452284Sobrien
1244590075Sobrien	  if (TYPE_P (instantiation))
1244652284Sobrien	    {
1244752284Sobrien	      tree fn;
1244852284Sobrien
1244990075Sobrien	      if (!COMPLETE_TYPE_P (instantiation))
1245052284Sobrien		{
1245152284Sobrien		  instantiate_class_template (instantiation);
1245252284Sobrien		  if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12453169689Skan		    for (fn = TYPE_METHODS (instantiation);
1245452284Sobrien			 fn;
1245552284Sobrien			 fn = TREE_CHAIN (fn))
1245652284Sobrien		      if (! DECL_ARTIFICIAL (fn))
12457169689Skan			instantiate_decl (fn,
12458169689Skan					  /*defer_ok=*/0,
12459169689Skan					  /*expl_inst_class_mem_p=*/false);
1246090075Sobrien		  if (COMPLETE_TYPE_P (instantiation))
12461169689Skan		    reconsider = 1;
1246252284Sobrien		}
1246352284Sobrien
1246490075Sobrien	      if (COMPLETE_TYPE_P (instantiation))
1246552284Sobrien		/* If INSTANTIATION has been instantiated, then we don't
1246652284Sobrien		   need to consider it again in the future.  */
1246752284Sobrien		*t = TREE_CHAIN (*t);
1246890075Sobrien	      else
1246990075Sobrien		{
1247090075Sobrien		  last = *t;
1247190075Sobrien		  t = &TREE_CHAIN (*t);
1247290075Sobrien		}
1247352284Sobrien	    }
1247452284Sobrien	  else
1247552284Sobrien	    {
1247690075Sobrien	      if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
1247752284Sobrien		  && !DECL_TEMPLATE_INSTANTIATED (instantiation))
1247852284Sobrien		{
12479169689Skan		  instantiation
12480169689Skan		    = instantiate_decl (instantiation,
12481169689Skan					/*defer_ok=*/0,
12482169689Skan					/*expl_inst_class_mem_p=*/false);
1248352284Sobrien		  if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12484169689Skan		    reconsider = 1;
1248552284Sobrien		}
1248652284Sobrien
1248790075Sobrien	      if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
1248852284Sobrien		  || DECL_TEMPLATE_INSTANTIATED (instantiation))
1248952284Sobrien		/* If INSTANTIATION has been instantiated, then we don't
1249052284Sobrien		   need to consider it again in the future.  */
1249152284Sobrien		*t = TREE_CHAIN (*t);
1249290075Sobrien	      else
1249390075Sobrien		{
1249490075Sobrien		  last = *t;
1249590075Sobrien		  t = &TREE_CHAIN (*t);
1249690075Sobrien		}
1249752284Sobrien	    }
1249890075Sobrien	  tinst_depth = 0;
1249990075Sobrien	  current_tinst_level = NULL_TREE;
1250050397Sobrien	}
1250190075Sobrien      last_pending_template = last;
12502169689Skan    }
1250352284Sobrien  while (reconsider);
1250452284Sobrien
12505132718Skan  input_location = saved_loc;
12506169689Skan  in_system_header = saved_in_system_header;
1250750397Sobrien}
1250850397Sobrien
1250990075Sobrien/* Substitute ARGVEC into T, which is a list of initializers for
1251090075Sobrien   either base class or a non-static data member.  The TREE_PURPOSEs
1251190075Sobrien   are DECLs, and the TREE_VALUEs are the initializer values.  Used by
1251290075Sobrien   instantiate_decl.  */
1251352284Sobrien
1251450397Sobrienstatic tree
12515132718Skantsubst_initializer_list (tree t, tree argvec)
1251650397Sobrien{
12517117395Skan  tree inits = NULL_TREE;
1251850397Sobrien
1251950397Sobrien  for (; t; t = TREE_CHAIN (t))
1252050397Sobrien    {
1252190075Sobrien      tree decl;
1252290075Sobrien      tree init;
1252350397Sobrien
12524169689Skan      decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
1252590075Sobrien			  NULL_TREE);
12526117395Skan      decl = expand_member_init (decl);
12527117395Skan      if (decl && !DECL_P (decl))
12528117395Skan	in_base_initializer = 1;
1252950397Sobrien
12530169689Skan      init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12531169689Skan			  NULL_TREE,
12532169689Skan			  /*integral_constant_expression_p=*/false);
12533117395Skan      in_base_initializer = 0;
12534117395Skan
12535117395Skan      if (decl)
12536117395Skan	{
12537117395Skan	  init = build_tree_list (decl, init);
12538117395Skan	  TREE_CHAIN (init) = inits;
12539117395Skan	  inits = init;
12540117395Skan	}
1254150397Sobrien    }
12542117395Skan  return inits;
1254350397Sobrien}
1254450397Sobrien
1254552284Sobrien/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL.  */
1254650397Sobrien
1254752284Sobrienstatic void
12548132718Skanset_current_access_from_decl (tree decl)
1254950397Sobrien{
1255052284Sobrien  if (TREE_PRIVATE (decl))
1255152284Sobrien    current_access_specifier = access_private_node;
1255252284Sobrien  else if (TREE_PROTECTED (decl))
1255352284Sobrien    current_access_specifier = access_protected_node;
1255452284Sobrien  else
1255552284Sobrien    current_access_specifier = access_public_node;
1255652284Sobrien}
1255750397Sobrien
1255852284Sobrien/* Instantiate an enumerated type.  TAG is the template type, NEWTAG
1255952284Sobrien   is the instantiation (which should have been created with
1256052284Sobrien   start_enum) and ARGS are the template arguments to use.  */
1256150397Sobrien
1256252284Sobrienstatic void
12563132718Skantsubst_enum (tree tag, tree newtag, tree args)
1256452284Sobrien{
1256552284Sobrien  tree e;
1256652284Sobrien
1256750397Sobrien  for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
1256850397Sobrien    {
1256952284Sobrien      tree value;
12570132718Skan      tree decl;
12571132718Skan
12572132718Skan      decl = TREE_VALUE (e);
1257352284Sobrien      /* Note that in a template enum, the TREE_VALUE is the
1257452284Sobrien	 CONST_DECL, not the corresponding INTEGER_CST.  */
12575169689Skan      value = tsubst_expr (DECL_INITIAL (decl),
12576169689Skan			   args, tf_warning_or_error, NULL_TREE,
12577169689Skan			   /*integral_constant_expression_p=*/true);
1257852284Sobrien
1257952284Sobrien      /* Give this enumeration constant the correct access.  */
12580132718Skan      set_current_access_from_decl (decl);
1258152284Sobrien
1258252284Sobrien      /* Actually build the enumerator itself.  */
12583169689Skan      build_enumerator (DECL_NAME (decl), value, newtag);
1258450397Sobrien    }
1258550397Sobrien
1258652284Sobrien  finish_enum (newtag);
12587117395Skan  DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12588117395Skan    = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
1258952284Sobrien}
1259050397Sobrien
1259190075Sobrien/* DECL is a FUNCTION_DECL that is a template specialization.  Return
1259290075Sobrien   its type -- but without substituting the innermost set of template
1259390075Sobrien   arguments.  So, innermost set of template parameters will appear in
12594117395Skan   the type.  */
1259550397Sobrien
12596169689Skantree
12597132718Skanget_mostly_instantiated_function_type (tree decl)
1259852284Sobrien{
1259952284Sobrien  tree fn_type;
1260090075Sobrien  tree tmpl;
1260190075Sobrien  tree targs;
1260252284Sobrien  tree tparms;
1260352284Sobrien  int parm_depth;
1260450397Sobrien
1260590075Sobrien  tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
1260652284Sobrien  targs = DECL_TI_ARGS (decl);
1260752284Sobrien  tparms = DECL_TEMPLATE_PARMS (tmpl);
1260852284Sobrien  parm_depth = TMPL_PARMS_DEPTH (tparms);
1260952284Sobrien
1261052284Sobrien  /* There should be as many levels of arguments as there are levels
1261152284Sobrien     of parameters.  */
12612169689Skan  gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
1261352284Sobrien
1261452284Sobrien  fn_type = TREE_TYPE (tmpl);
1261552284Sobrien
1261652284Sobrien  if (parm_depth == 1)
1261752284Sobrien    /* No substitution is necessary.  */
1261852284Sobrien    ;
1261952284Sobrien  else
1262052284Sobrien    {
12621169689Skan      int i, save_access_control;
1262252284Sobrien      tree partial_args;
1262352284Sobrien
1262452284Sobrien      /* Replace the innermost level of the TARGS with NULL_TREEs to
1262590075Sobrien	 let tsubst know not to substitute for those parameters.  */
1262690075Sobrien      partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
1262752284Sobrien      for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
1262852284Sobrien	SET_TMPL_ARGS_LEVEL (partial_args, i,
1262952284Sobrien			     TMPL_ARGS_LEVEL (targs, i));
1263052284Sobrien      SET_TMPL_ARGS_LEVEL (partial_args,
1263152284Sobrien			   TMPL_ARGS_DEPTH (targs),
1263290075Sobrien			   make_tree_vec (DECL_NTPARMS (tmpl)));
1263352284Sobrien
12634169689Skan      /* Disable access control as this function is used only during
12635169689Skan	 name-mangling.  */
12636169689Skan      save_access_control = flag_access_control;
12637169689Skan      flag_access_control = 0;
12638117395Skan
12639132718Skan      ++processing_template_decl;
1264052284Sobrien      /* Now, do the (partial) substitution to figure out the
1264152284Sobrien	 appropriate function type.  */
1264296263Sobrien      fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12643132718Skan      --processing_template_decl;
1264452284Sobrien
1264552284Sobrien      /* Substitute into the template parameters to obtain the real
1264652284Sobrien	 innermost set of parameters.  This step is important if the
1264752284Sobrien	 innermost set of template parameters contains value
1264852284Sobrien	 parameters whose types depend on outer template parameters.  */
1264952284Sobrien      TREE_VEC_LENGTH (partial_args)--;
1265096263Sobrien      tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12651117395Skan
12652169689Skan      flag_access_control = save_access_control;
1265352284Sobrien    }
1265452284Sobrien
1265590075Sobrien  return fn_type;
1265690075Sobrien}
1265752284Sobrien
1265890075Sobrien/* Return truthvalue if we're processing a template different from
1265990075Sobrien   the last one involved in diagnostics.  */
1266090075Sobrienint
12661132718Skanproblematic_instantiation_changed (void)
1266290075Sobrien{
1266390075Sobrien  return last_template_error_tick != tinst_level_tick;
1266490075Sobrien}
1266552284Sobrien
1266690075Sobrien/* Remember current template involved in diagnostics.  */
1266790075Sobrienvoid
12668132718Skanrecord_last_problematic_instantiation (void)
1266990075Sobrien{
1267090075Sobrien  last_template_error_tick = tinst_level_tick;
1267190075Sobrien}
1267252284Sobrien
1267390075Sobrientree
12674132718Skancurrent_instantiation (void)
1267590075Sobrien{
1267690075Sobrien  return current_tinst_level;
1267790075Sobrien}
1267852284Sobrien
1267990075Sobrien/* [temp.param] Check that template non-type parm TYPE is of an allowable
12680117395Skan   type. Return zero for ok, nonzero for disallowed. Issue error and
1268196263Sobrien   warning messages under control of COMPLAIN.  */
1268252284Sobrien
1268390075Sobrienstatic int
12684132718Skaninvalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
1268590075Sobrien{
1268690075Sobrien  if (INTEGRAL_TYPE_P (type))
1268790075Sobrien    return 0;
1268890075Sobrien  else if (POINTER_TYPE_P (type))
1268990075Sobrien    return 0;
12690132718Skan  else if (TYPE_PTR_TO_MEMBER_P (type))
1269190075Sobrien    return 0;
1269290075Sobrien  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1269390075Sobrien    return 0;
1269490075Sobrien  else if (TREE_CODE (type) == TYPENAME_TYPE)
1269590075Sobrien    return 0;
12696169689Skan
1269796263Sobrien  if (complain & tf_error)
12698169689Skan    error ("%q#T is not a valid type for a template constant parameter", type);
1269990075Sobrien  return 1;
1270050397Sobrien}
12701117395Skan
12702132718Skan/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12703132718Skan   Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12704132718Skan
12705132718Skanstatic bool
12706132718Skandependent_type_p_r (tree type)
12707132718Skan{
12708132718Skan  tree scope;
12709132718Skan
12710132718Skan  /* [temp.dep.type]
12711132718Skan
12712132718Skan     A type is dependent if it is:
12713132718Skan
12714169689Skan     -- a template parameter. Template template parameters are types
12715169689Skan	for us (since TYPE_P holds true for them) so we handle
12716169689Skan	them here.  */
12717169689Skan  if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12718132718Skan      || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12719132718Skan    return true;
12720132718Skan  /* -- a qualified-id with a nested-name-specifier which contains a
12721169689Skan	class-name that names a dependent type or whose unqualified-id
12722132718Skan	names a dependent type.  */
12723132718Skan  if (TREE_CODE (type) == TYPENAME_TYPE)
12724132718Skan    return true;
12725132718Skan  /* -- a cv-qualified type where the cv-unqualified type is
12726169689Skan	dependent.  */
12727132718Skan  type = TYPE_MAIN_VARIANT (type);
12728132718Skan  /* -- a compound type constructed from any dependent type.  */
12729132718Skan  if (TYPE_PTR_TO_MEMBER_P (type))
12730132718Skan    return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12731169689Skan	    || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12732132718Skan					   (type)));
12733132718Skan  else if (TREE_CODE (type) == POINTER_TYPE
12734132718Skan	   || TREE_CODE (type) == REFERENCE_TYPE)
12735132718Skan    return dependent_type_p (TREE_TYPE (type));
12736132718Skan  else if (TREE_CODE (type) == FUNCTION_TYPE
12737132718Skan	   || TREE_CODE (type) == METHOD_TYPE)
12738132718Skan    {
12739132718Skan      tree arg_type;
12740132718Skan
12741132718Skan      if (dependent_type_p (TREE_TYPE (type)))
12742132718Skan	return true;
12743169689Skan      for (arg_type = TYPE_ARG_TYPES (type);
12744169689Skan	   arg_type;
12745132718Skan	   arg_type = TREE_CHAIN (arg_type))
12746132718Skan	if (dependent_type_p (TREE_VALUE (arg_type)))
12747132718Skan	  return true;
12748132718Skan      return false;
12749132718Skan    }
12750132718Skan  /* -- an array type constructed from any dependent type or whose
12751169689Skan	size is specified by a constant expression that is
12752132718Skan	value-dependent.  */
12753132718Skan  if (TREE_CODE (type) == ARRAY_TYPE)
12754132718Skan    {
12755132718Skan      if (TYPE_DOMAIN (type)
12756169689Skan	  && ((value_dependent_expression_p
12757132718Skan	       (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12758132718Skan	      || (type_dependent_expression_p
12759132718Skan		  (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12760132718Skan	return true;
12761132718Skan      return dependent_type_p (TREE_TYPE (type));
12762132718Skan    }
12763169689Skan
12764132718Skan  /* -- a template-id in which either the template name is a template
12765132718Skan     parameter ...  */
12766132718Skan  if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12767132718Skan    return true;
12768132718Skan  /* ... or any of the template arguments is a dependent type or
12769132718Skan	an expression that is type-dependent or value-dependent.  */
12770132718Skan  else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12771169689Skan	   && (any_dependent_template_arguments_p
12772132718Skan	       (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12773132718Skan    return true;
12774169689Skan
12775132718Skan  /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12776132718Skan     expression is not type-dependent, then it should already been
12777132718Skan     have resolved.  */
12778132718Skan  if (TREE_CODE (type) == TYPEOF_TYPE)
12779132718Skan    return true;
12780169689Skan
12781132718Skan  /* The standard does not specifically mention types that are local
12782132718Skan     to template functions or local classes, but they should be
12783132718Skan     considered dependent too.  For example:
12784132718Skan
12785169689Skan       template <int I> void f() {
12786169689Skan	 enum E { a = I };
12787132718Skan	 S<sizeof (E)> s;
12788132718Skan       }
12789132718Skan
12790132718Skan     The size of `E' cannot be known until the value of `I' has been
12791132718Skan     determined.  Therefore, `E' must be considered dependent.  */
12792132718Skan  scope = TYPE_CONTEXT (type);
12793132718Skan  if (scope && TYPE_P (scope))
12794132718Skan    return dependent_type_p (scope);
12795132718Skan  else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12796132718Skan    return type_dependent_expression_p (scope);
12797132718Skan
12798132718Skan  /* Other types are non-dependent.  */
12799132718Skan  return false;
12800132718Skan}
12801132718Skan
12802132718Skan/* Returns TRUE if TYPE is dependent, in the sense of
12803132718Skan   [temp.dep.type].  */
12804132718Skan
12805132718Skanbool
12806132718Skandependent_type_p (tree type)
12807132718Skan{
12808132718Skan  /* If there are no template parameters in scope, then there can't be
12809132718Skan     any dependent types.  */
12810132718Skan  if (!processing_template_decl)
12811169689Skan    {
12812169689Skan      /* If we are not processing a template, then nobody should be
12813169689Skan	 providing us with a dependent type.  */
12814169689Skan      gcc_assert (type);
12815169689Skan      gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12816169689Skan      return false;
12817169689Skan    }
12818132718Skan
12819132718Skan  /* If the type is NULL, we have not computed a type for the entity
12820132718Skan     in question; in that case, the type is dependent.  */
12821132718Skan  if (!type)
12822132718Skan    return true;
12823132718Skan
12824132718Skan  /* Erroneous types can be considered non-dependent.  */
12825132718Skan  if (type == error_mark_node)
12826132718Skan    return false;
12827132718Skan
12828132718Skan  /* If we have not already computed the appropriate value for TYPE,
12829132718Skan     do so now.  */
12830132718Skan  if (!TYPE_DEPENDENT_P_VALID (type))
12831132718Skan    {
12832132718Skan      TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12833132718Skan      TYPE_DEPENDENT_P_VALID (type) = 1;
12834132718Skan    }
12835132718Skan
12836132718Skan  return TYPE_DEPENDENT_P (type);
12837132718Skan}
12838132718Skan
12839132718Skan/* Returns TRUE if EXPRESSION is dependent, according to CRITERION.  */
12840132718Skan
12841132718Skanstatic bool
12842132718Skandependent_scope_ref_p (tree expression, bool criterion (tree))
12843132718Skan{
12844132718Skan  tree scope;
12845132718Skan  tree name;
12846132718Skan
12847169689Skan  gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12848132718Skan
12849132718Skan  if (!TYPE_P (TREE_OPERAND (expression, 0)))
12850132718Skan    return true;
12851132718Skan
12852132718Skan  scope = TREE_OPERAND (expression, 0);
12853132718Skan  name = TREE_OPERAND (expression, 1);
12854132718Skan
12855132718Skan  /* [temp.dep.expr]
12856132718Skan
12857132718Skan     An id-expression is type-dependent if it contains a
12858132718Skan     nested-name-specifier that contains a class-name that names a
12859132718Skan     dependent type.  */
12860132718Skan  /* The suggested resolution to Core Issue 2 implies that if the
12861132718Skan     qualifying type is the current class, then we must peek
12862132718Skan     inside it.  */
12863169689Skan  if (DECL_P (name)
12864132718Skan      && currently_open_class (scope)
12865132718Skan      && !criterion (name))
12866132718Skan    return false;
12867132718Skan  if (dependent_type_p (scope))
12868132718Skan    return true;
12869132718Skan
12870132718Skan  return false;
12871132718Skan}
12872132718Skan
12873132718Skan/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12874169689Skan   [temp.dep.constexpr].  EXPRESSION is already known to be a constant
12875169689Skan   expression.  */
12876132718Skan
12877132718Skanbool
12878132718Skanvalue_dependent_expression_p (tree expression)
12879132718Skan{
12880132718Skan  if (!processing_template_decl)
12881132718Skan    return false;
12882132718Skan
12883132718Skan  /* A name declared with a dependent type.  */
12884169689Skan  if (DECL_P (expression) && type_dependent_expression_p (expression))
12885132718Skan    return true;
12886169689Skan
12887169689Skan  switch (TREE_CODE (expression))
12888132718Skan    {
12889169689Skan    case IDENTIFIER_NODE:
12890169689Skan      /* A name that has not been looked up -- must be dependent.  */
12891169689Skan      return true;
12892169689Skan
12893169689Skan    case TEMPLATE_PARM_INDEX:
12894169689Skan      /* A non-type template parm.  */
12895169689Skan      return true;
12896169689Skan
12897169689Skan    case CONST_DECL:
12898169689Skan      /* A non-type template parm.  */
12899169689Skan      if (DECL_TEMPLATE_PARM_P (expression))
12900132718Skan	return true;
12901169689Skan      return false;
12902132718Skan
12903169689Skan    case VAR_DECL:
12904169689Skan       /* A constant with integral or enumeration type and is initialized
12905169689Skan	  with an expression that is value-dependent.  */
12906169689Skan      if (DECL_INITIAL (expression)
12907169689Skan	  && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12908169689Skan	  && value_dependent_expression_p (DECL_INITIAL (expression)))
12909169689Skan	return true;
12910169689Skan      return false;
12911169689Skan
12912169689Skan    case DYNAMIC_CAST_EXPR:
12913169689Skan    case STATIC_CAST_EXPR:
12914169689Skan    case CONST_CAST_EXPR:
12915169689Skan    case REINTERPRET_CAST_EXPR:
12916169689Skan    case CAST_EXPR:
12917169689Skan      /* These expressions are value-dependent if the type to which
12918169689Skan	 the cast occurs is dependent or the expression being casted
12919169689Skan	 is value-dependent.  */
12920169689Skan      {
12921169689Skan	tree type = TREE_TYPE (expression);
12922169689Skan
12923169689Skan	if (dependent_type_p (type))
12924169689Skan	  return true;
12925169689Skan
12926169689Skan	/* A functional cast has a list of operands.  */
12927169689Skan	expression = TREE_OPERAND (expression, 0);
12928169689Skan	if (!expression)
12929169689Skan	  {
12930169689Skan	    /* If there are no operands, it must be an expression such
12931169689Skan	       as "int()". This should not happen for aggregate types
12932169689Skan	       because it would form non-constant expressions.  */
12933169689Skan	    gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12934169689Skan
12935169689Skan	    return false;
12936169689Skan	  }
12937169689Skan
12938169689Skan	if (TREE_CODE (expression) == TREE_LIST)
12939171825Skan	  return any_value_dependent_elements_p (expression);
12940169689Skan
12941132718Skan	return value_dependent_expression_p (expression);
12942169689Skan      }
12943169689Skan
12944169689Skan    case SIZEOF_EXPR:
12945169689Skan    case ALIGNOF_EXPR:
12946169689Skan      /* A `sizeof' expression is value-dependent if the operand is
12947169689Skan	 type-dependent.  */
12948132718Skan      expression = TREE_OPERAND (expression, 0);
12949132718Skan      if (TYPE_P (expression))
12950132718Skan	return dependent_type_p (expression);
12951132718Skan      return type_dependent_expression_p (expression);
12952169689Skan
12953169689Skan    case SCOPE_REF:
12954169689Skan      return dependent_scope_ref_p (expression, value_dependent_expression_p);
12955169689Skan
12956169689Skan    case COMPONENT_REF:
12957169689Skan      return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12958169689Skan	      || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12959169689Skan
12960169689Skan    case CALL_EXPR:
12961169689Skan      /* A CALL_EXPR may appear in a constant expression if it is a
12962169689Skan	 call to a builtin function, e.g., __builtin_constant_p.  All
12963169689Skan	 such calls are value-dependent.  */
12964169689Skan      return true;
12965169689Skan
12966169689Skan    case MODOP_EXPR:
12967169689Skan      return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
12968169689Skan	      || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
12969169689Skan
12970169689Skan    default:
12971169689Skan      /* A constant expression is value-dependent if any subexpression is
12972169689Skan	 value-dependent.  */
12973132718Skan      switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12974132718Skan	{
12975169689Skan	case tcc_reference:
12976169689Skan	case tcc_unary:
12977169689Skan	  return (value_dependent_expression_p
12978132718Skan		  (TREE_OPERAND (expression, 0)));
12979169689Skan
12980169689Skan	case tcc_comparison:
12981169689Skan	case tcc_binary:
12982169689Skan	  return ((value_dependent_expression_p
12983132718Skan		   (TREE_OPERAND (expression, 0)))
12984169689Skan		  || (value_dependent_expression_p
12985132718Skan		      (TREE_OPERAND (expression, 1))));
12986169689Skan
12987169689Skan	case tcc_expression:
12988132718Skan	  {
12989132718Skan	    int i;
12990169689Skan	    for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12991132718Skan	      /* In some cases, some of the operands may be missing.
12992132718Skan		 (For example, in the case of PREDECREMENT_EXPR, the
12993132718Skan		 amount to increment by may be missing.)  That doesn't
12994132718Skan		 make the expression dependent.  */
12995132718Skan	      if (TREE_OPERAND (expression, i)
12996132718Skan		  && (value_dependent_expression_p
12997132718Skan		      (TREE_OPERAND (expression, i))))
12998132718Skan		return true;
12999132718Skan	    return false;
13000132718Skan	  }
13001169689Skan
13002169689Skan	default:
13003169689Skan	  break;
13004132718Skan	}
13005132718Skan    }
13006132718Skan
13007132718Skan  /* The expression is not value-dependent.  */
13008132718Skan  return false;
13009132718Skan}
13010132718Skan
13011132718Skan/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
13012132718Skan   [temp.dep.expr].  */
13013132718Skan
13014132718Skanbool
13015132718Skantype_dependent_expression_p (tree expression)
13016132718Skan{
13017132718Skan  if (!processing_template_decl)
13018132718Skan    return false;
13019132718Skan
13020132718Skan  if (expression == error_mark_node)
13021132718Skan    return false;
13022132718Skan
13023132718Skan  /* An unresolved name is always dependent.  */
13024169689Skan  if (TREE_CODE (expression) == IDENTIFIER_NODE
13025169689Skan      || TREE_CODE (expression) == USING_DECL)
13026132718Skan    return true;
13027169689Skan
13028132718Skan  /* Some expression forms are never type-dependent.  */
13029132718Skan  if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
13030132718Skan      || TREE_CODE (expression) == SIZEOF_EXPR
13031132718Skan      || TREE_CODE (expression) == ALIGNOF_EXPR
13032132718Skan      || TREE_CODE (expression) == TYPEID_EXPR
13033132718Skan      || TREE_CODE (expression) == DELETE_EXPR
13034132718Skan      || TREE_CODE (expression) == VEC_DELETE_EXPR
13035132718Skan      || TREE_CODE (expression) == THROW_EXPR)
13036132718Skan    return false;
13037132718Skan
13038132718Skan  /* The types of these expressions depends only on the type to which
13039132718Skan     the cast occurs.  */
13040132718Skan  if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
13041132718Skan      || TREE_CODE (expression) == STATIC_CAST_EXPR
13042132718Skan      || TREE_CODE (expression) == CONST_CAST_EXPR
13043132718Skan      || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
13044132718Skan      || TREE_CODE (expression) == CAST_EXPR)
13045132718Skan    return dependent_type_p (TREE_TYPE (expression));
13046132718Skan
13047132718Skan  /* The types of these expressions depends only on the type created
13048132718Skan     by the expression.  */
13049132718Skan  if (TREE_CODE (expression) == NEW_EXPR
13050132718Skan      || TREE_CODE (expression) == VEC_NEW_EXPR)
13051132718Skan    {
13052132718Skan      /* For NEW_EXPR tree nodes created inside a template, either
13053132718Skan	 the object type itself or a TREE_LIST may appear as the
13054132718Skan	 operand 1.  */
13055132718Skan      tree type = TREE_OPERAND (expression, 1);
13056132718Skan      if (TREE_CODE (type) == TREE_LIST)
13057132718Skan	/* This is an array type.  We need to check array dimensions
13058132718Skan	   as well.  */
13059132718Skan	return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
13060132718Skan	       || value_dependent_expression_p
13061132718Skan		    (TREE_OPERAND (TREE_VALUE (type), 1));
13062132718Skan      else
13063132718Skan	return dependent_type_p (type);
13064132718Skan    }
13065132718Skan
13066132718Skan  if (TREE_CODE (expression) == SCOPE_REF
13067132718Skan      && dependent_scope_ref_p (expression,
13068132718Skan				type_dependent_expression_p))
13069132718Skan    return true;
13070132718Skan
13071132718Skan  if (TREE_CODE (expression) == FUNCTION_DECL
13072132718Skan      && DECL_LANG_SPECIFIC (expression)
13073132718Skan      && DECL_TEMPLATE_INFO (expression)
13074132718Skan      && (any_dependent_template_arguments_p
13075132718Skan	  (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
13076132718Skan    return true;
13077132718Skan
13078132718Skan  if (TREE_CODE (expression) == TEMPLATE_DECL
13079132718Skan      && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
13080132718Skan    return false;
13081132718Skan
13082132718Skan  if (TREE_TYPE (expression) == unknown_type_node)
13083132718Skan    {
13084132718Skan      if (TREE_CODE (expression) == ADDR_EXPR)
13085132718Skan	return type_dependent_expression_p (TREE_OPERAND (expression, 0));
13086132718Skan      if (TREE_CODE (expression) == COMPONENT_REF
13087132718Skan	  || TREE_CODE (expression) == OFFSET_REF)
13088132718Skan	{
13089132718Skan	  if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
13090132718Skan	    return true;
13091132718Skan	  expression = TREE_OPERAND (expression, 1);
13092132718Skan	  if (TREE_CODE (expression) == IDENTIFIER_NODE)
13093132718Skan	    return false;
13094132718Skan	}
13095146895Skan      /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
13096146895Skan      if (TREE_CODE (expression) == SCOPE_REF)
13097146895Skan	return false;
13098169689Skan
13099132718Skan      if (TREE_CODE (expression) == BASELINK)
13100132718Skan	expression = BASELINK_FUNCTIONS (expression);
13101169689Skan
13102132718Skan      if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
13103132718Skan	{
13104132718Skan	  if (any_dependent_template_arguments_p
13105132718Skan	      (TREE_OPERAND (expression, 1)))
13106132718Skan	    return true;
13107132718Skan	  expression = TREE_OPERAND (expression, 0);
13108132718Skan	}
13109169689Skan      gcc_assert (TREE_CODE (expression) == OVERLOAD
13110169689Skan		  || TREE_CODE (expression) == FUNCTION_DECL);
13111169689Skan
13112169689Skan      while (expression)
13113132718Skan	{
13114169689Skan	  if (type_dependent_expression_p (OVL_CURRENT (expression)))
13115169689Skan	    return true;
13116169689Skan	  expression = OVL_NEXT (expression);
13117132718Skan	}
13118169689Skan      return false;
13119132718Skan    }
13120169689Skan
13121169689Skan  gcc_assert (TREE_CODE (expression) != TYPE_DECL);
13122169689Skan
13123132718Skan  return (dependent_type_p (TREE_TYPE (expression)));
13124132718Skan}
13125132718Skan
13126132718Skan/* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13127132718Skan   contains a type-dependent expression.  */
13128132718Skan
13129132718Skanbool
13130132718Skanany_type_dependent_arguments_p (tree args)
13131132718Skan{
13132132718Skan  while (args)
13133132718Skan    {
13134132718Skan      tree arg = TREE_VALUE (args);
13135132718Skan
13136132718Skan      if (type_dependent_expression_p (arg))
13137132718Skan	return true;
13138132718Skan      args = TREE_CHAIN (args);
13139132718Skan    }
13140132718Skan  return false;
13141132718Skan}
13142132718Skan
13143171825Skan/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
13144171825Skan   expressions) contains any value-dependent expressions.  */
13145171825Skan
13146171825Skanbool
13147171825Skanany_value_dependent_elements_p (tree list)
13148171825Skan{
13149171825Skan  for (; list; list = TREE_CHAIN (list))
13150171825Skan    if (value_dependent_expression_p (TREE_VALUE (list)))
13151171825Skan      return true;
13152171825Skan
13153171825Skan  return false;
13154171825Skan}
13155171825Skan
13156132718Skan/* Returns TRUE if the ARG (a template argument) is dependent.  */
13157132718Skan
13158132718Skanstatic bool
13159132718Skandependent_template_arg_p (tree arg)
13160132718Skan{
13161132718Skan  if (!processing_template_decl)
13162132718Skan    return false;
13163132718Skan
13164132718Skan  if (TREE_CODE (arg) == TEMPLATE_DECL
13165132718Skan      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13166132718Skan    return dependent_template_p (arg);
13167132718Skan  else if (TYPE_P (arg))
13168132718Skan    return dependent_type_p (arg);
13169132718Skan  else
13170132718Skan    return (type_dependent_expression_p (arg)
13171132718Skan	    || value_dependent_expression_p (arg));
13172132718Skan}
13173132718Skan
13174132718Skan/* Returns true if ARGS (a collection of template arguments) contains
13175132718Skan   any dependent arguments.  */
13176132718Skan
13177132718Skanbool
13178132718Skanany_dependent_template_arguments_p (tree args)
13179132718Skan{
13180132718Skan  int i;
13181132718Skan  int j;
13182132718Skan
13183132718Skan  if (!args)
13184132718Skan    return false;
13185169689Skan  if (args == error_mark_node)
13186169689Skan    return true;
13187132718Skan
13188132718Skan  for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13189132718Skan    {
13190132718Skan      tree level = TMPL_ARGS_LEVEL (args, i + 1);
13191132718Skan      for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13192132718Skan	if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13193132718Skan	  return true;
13194132718Skan    }
13195132718Skan
13196132718Skan  return false;
13197132718Skan}
13198132718Skan
13199132718Skan/* Returns TRUE if the template TMPL is dependent.  */
13200132718Skan
13201132718Skanbool
13202132718Skandependent_template_p (tree tmpl)
13203132718Skan{
13204132718Skan  if (TREE_CODE (tmpl) == OVERLOAD)
13205132718Skan    {
13206132718Skan      while (tmpl)
13207132718Skan	{
13208132718Skan	  if (dependent_template_p (OVL_FUNCTION (tmpl)))
13209132718Skan	    return true;
13210132718Skan	  tmpl = OVL_CHAIN (tmpl);
13211132718Skan	}
13212132718Skan      return false;
13213132718Skan    }
13214132718Skan
13215132718Skan  /* Template template parameters are dependent.  */
13216132718Skan  if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13217132718Skan      || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13218132718Skan    return true;
13219169689Skan  /* So are names that have not been looked up.  */
13220146895Skan  if (TREE_CODE (tmpl) == SCOPE_REF
13221146895Skan      || TREE_CODE (tmpl) == IDENTIFIER_NODE)
13222132718Skan    return true;
13223132718Skan  /* So are member templates of dependent classes.  */
13224132718Skan  if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13225132718Skan    return dependent_type_p (DECL_CONTEXT (tmpl));
13226132718Skan  return false;
13227132718Skan}
13228132718Skan
13229132718Skan/* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
13230132718Skan
13231132718Skanbool
13232132718Skandependent_template_id_p (tree tmpl, tree args)
13233132718Skan{
13234132718Skan  return (dependent_template_p (tmpl)
13235132718Skan	  || any_dependent_template_arguments_p (args));
13236132718Skan}
13237132718Skan
13238132718Skan/* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13239132718Skan   TYPENAME_TYPE corresponds.  Returns ERROR_MARK_NODE if no such TYPE
13240132718Skan   can be found.  Note that this function peers inside uninstantiated
13241132718Skan   templates and therefore should be used only in extremely limited
13242169689Skan   situations.  ONLY_CURRENT_P restricts this peering to the currently
13243169689Skan   open classes hierarchy (which is required when comparing types).  */
13244132718Skan
13245132718Skantree
13246132718Skanresolve_typename_type (tree type, bool only_current_p)
13247132718Skan{
13248132718Skan  tree scope;
13249132718Skan  tree name;
13250132718Skan  tree decl;
13251132718Skan  int quals;
13252169689Skan  tree pushed_scope;
13253132718Skan
13254169689Skan  gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
13255132718Skan
13256132718Skan  scope = TYPE_CONTEXT (type);
13257132718Skan  name = TYPE_IDENTIFIER (type);
13258132718Skan
13259132718Skan  /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13260132718Skan     it first before we can figure out what NAME refers to.  */
13261132718Skan  if (TREE_CODE (scope) == TYPENAME_TYPE)
13262132718Skan    scope = resolve_typename_type (scope, only_current_p);
13263132718Skan  /* If we don't know what SCOPE refers to, then we cannot resolve the
13264132718Skan     TYPENAME_TYPE.  */
13265132718Skan  if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13266132718Skan    return error_mark_node;
13267132718Skan  /* If the SCOPE is a template type parameter, we have no way of
13268132718Skan     resolving the name.  */
13269132718Skan  if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13270132718Skan    return type;
13271132718Skan  /* If the SCOPE is not the current instantiation, there's no reason
13272132718Skan     to look inside it.  */
13273132718Skan  if (only_current_p && !currently_open_class (scope))
13274132718Skan    return error_mark_node;
13275132718Skan  /* If SCOPE is a partial instantiation, it will not have a valid
13276132718Skan     TYPE_FIELDS list, so use the original template.  */
13277132718Skan  scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
13278132718Skan  /* Enter the SCOPE so that name lookup will be resolved as if we
13279132718Skan     were in the class definition.  In particular, SCOPE will no
13280132718Skan     longer be considered a dependent type.  */
13281169689Skan  pushed_scope = push_scope (scope);
13282132718Skan  /* Look up the declaration.  */
13283132718Skan  decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
13284132718Skan  /* Obtain the set of qualifiers applied to the TYPE.  */
13285132718Skan  quals = cp_type_quals (type);
13286132718Skan  /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13287132718Skan     find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
13288132718Skan  if (!decl)
13289132718Skan    type = error_mark_node;
13290132718Skan  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13291132718Skan	   && TREE_CODE (decl) == TYPE_DECL)
13292132718Skan    type = TREE_TYPE (decl);
13293132718Skan  else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13294132718Skan	   && DECL_CLASS_TEMPLATE_P (decl))
13295132718Skan    {
13296132718Skan      tree tmpl;
13297132718Skan      tree args;
13298132718Skan      /* Obtain the template and the arguments.  */
13299132718Skan      tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13300132718Skan      args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13301132718Skan      /* Instantiate the template.  */
13302132718Skan      type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
13303132718Skan				    /*entering_scope=*/0, tf_error | tf_user);
13304132718Skan    }
13305132718Skan  else
13306132718Skan    type = error_mark_node;
13307132718Skan  /* Qualify the resulting type.  */
13308132718Skan  if (type != error_mark_node && quals)
13309132718Skan    type = cp_build_qualified_type (type, quals);
13310132718Skan  /* Leave the SCOPE.  */
13311169689Skan  if (pushed_scope)
13312169689Skan    pop_scope (pushed_scope);
13313132718Skan
13314132718Skan  return type;
13315132718Skan}
13316132718Skan
13317132718Skan/* EXPR is an expression which is not type-dependent.  Return a proxy
13318132718Skan   for EXPR that can be used to compute the types of larger
13319132718Skan   expressions containing EXPR.  */
13320132718Skan
13321132718Skantree
13322132718Skanbuild_non_dependent_expr (tree expr)
13323132718Skan{
13324132718Skan  tree inner_expr;
13325132718Skan
13326169689Skan  /* Preserve null pointer constants so that the type of things like
13327132718Skan     "p == 0" where "p" is a pointer can be determined.  */
13328132718Skan  if (null_ptr_cst_p (expr))
13329132718Skan    return expr;
13330132718Skan  /* Preserve OVERLOADs; the functions must be available to resolve
13331132718Skan     types.  */
13332169689Skan  inner_expr = expr;
13333169689Skan  if (TREE_CODE (inner_expr) == ADDR_EXPR)
13334169689Skan    inner_expr = TREE_OPERAND (inner_expr, 0);
13335169689Skan  if (TREE_CODE (inner_expr) == COMPONENT_REF)
13336169689Skan    inner_expr = TREE_OPERAND (inner_expr, 1);
13337169689Skan  if (is_overloaded_fn (inner_expr)
13338146895Skan      || TREE_CODE (inner_expr) == OFFSET_REF)
13339132718Skan    return expr;
13340169689Skan  /* There is no need to return a proxy for a variable.  */
13341169689Skan  if (TREE_CODE (expr) == VAR_DECL)
13342169689Skan    return expr;
13343132718Skan  /* Preserve string constants; conversions from string constants to
13344132718Skan     "char *" are allowed, even though normally a "const char *"
13345132718Skan     cannot be used to initialize a "char *".  */
13346132718Skan  if (TREE_CODE (expr) == STRING_CST)
13347132718Skan    return expr;
13348132718Skan  /* Preserve arithmetic constants, as an optimization -- there is no
13349132718Skan     reason to create a new node.  */
13350132718Skan  if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13351132718Skan    return expr;
13352132718Skan  /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13353132718Skan     There is at least one place where we want to know that a
13354132718Skan     particular expression is a throw-expression: when checking a ?:
13355132718Skan     expression, there are special rules if the second or third
13356169689Skan     argument is a throw-expression.  */
13357132718Skan  if (TREE_CODE (expr) == THROW_EXPR)
13358132718Skan    return expr;
13359132718Skan
13360132718Skan  if (TREE_CODE (expr) == COND_EXPR)
13361169689Skan    return build3 (COND_EXPR,
13362169689Skan		   TREE_TYPE (expr),
13363169689Skan		   TREE_OPERAND (expr, 0),
13364169689Skan		   (TREE_OPERAND (expr, 1)
13365169689Skan		    ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13366169689Skan		    : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13367169689Skan		   build_non_dependent_expr (TREE_OPERAND (expr, 2)));
13368132718Skan  if (TREE_CODE (expr) == COMPOUND_EXPR
13369132718Skan      && !COMPOUND_EXPR_OVERLOADED (expr))
13370169689Skan    return build2 (COMPOUND_EXPR,
13371169689Skan		   TREE_TYPE (expr),
13372169689Skan		   TREE_OPERAND (expr, 0),
13373169689Skan		   build_non_dependent_expr (TREE_OPERAND (expr, 1)));
13374132718Skan
13375169689Skan  /* If the type is unknown, it can't really be non-dependent */
13376169689Skan  gcc_assert (TREE_TYPE (expr) != unknown_type_node);
13377169689Skan
13378169689Skan  /* Otherwise, build a NON_DEPENDENT_EXPR.
13379169689Skan
13380132718Skan     REFERENCE_TYPEs are not stripped for expressions in templates
13381132718Skan     because doing so would play havoc with mangling.  Consider, for
13382132718Skan     example:
13383132718Skan
13384169689Skan       template <typename T> void f<T& g>() { g(); }
13385132718Skan
13386132718Skan     In the body of "f", the expression for "g" will have
13387132718Skan     REFERENCE_TYPE, even though the standard says that it should
13388132718Skan     not.  The reason is that we must preserve the syntactic form of
13389132718Skan     the expression so that mangling (say) "f<g>" inside the body of
13390132718Skan     "f" works out correctly.  Therefore, the REFERENCE_TYPE is
13391132718Skan     stripped here.  */
13392132718Skan  return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
13393132718Skan}
13394132718Skan
13395132718Skan/* ARGS is a TREE_LIST of expressions as arguments to a function call.
13396132718Skan   Return a new TREE_LIST with the various arguments replaced with
13397132718Skan   equivalent non-dependent expressions.  */
13398132718Skan
13399132718Skantree
13400132718Skanbuild_non_dependent_args (tree args)
13401132718Skan{
13402132718Skan  tree a;
13403132718Skan  tree new_args;
13404132718Skan
13405132718Skan  new_args = NULL_TREE;
13406132718Skan  for (a = args; a; a = TREE_CHAIN (a))
13407169689Skan    new_args = tree_cons (NULL_TREE,
13408132718Skan			  build_non_dependent_expr (TREE_VALUE (a)),
13409132718Skan			  new_args);
13410132718Skan  return nreverse (new_args);
13411132718Skan}
13412132718Skan
13413117395Skan#include "gt-cp-pt.h"
13414