PR c++/9252
[official-gcc.git] / gcc / cp / pt.c
blob972bdab265a40de8644d8311c5d152d68e5e63c0
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "cp-tree.h"
37 #include "tree-inline.h"
38 #include "decl.h"
39 #include "lex.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "rtl.h"
44 #include "timevar.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) PARAMS ((tree, void*));
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work.
53 The TREE_PURPOSE of each entry is either a DECL (for a function or
54 static data member), or a TYPE (for a class) indicating what we are
55 hoping to instantiate. The TREE_VALUE is not used. */
56 static GTY(()) tree pending_templates;
57 static GTY(()) tree last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static GTY(()) varray_type inline_parm_levels;
64 static size_t inline_parm_levels_used;
66 static GTY(()) tree current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* A map from local variable declarations in the body of the template
71 presently being instantiated to the corresponding instantiated
72 local variables. */
73 static htab_t local_specializations;
75 #define UNIFY_ALLOW_NONE 0
76 #define UNIFY_ALLOW_MORE_CV_QUAL 1
77 #define UNIFY_ALLOW_LESS_CV_QUAL 2
78 #define UNIFY_ALLOW_DERIVED 4
79 #define UNIFY_ALLOW_INTEGER 8
80 #define UNIFY_ALLOW_OUTER_LEVEL 16
81 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
82 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
83 #define UNIFY_ALLOW_MAX_CORRECTION 128
85 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
86 virtual, or a base class of a virtual
87 base. */
88 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
89 type with the desired type. */
91 static void push_access_scope_real PARAMS ((tree, tree, tree));
92 static void push_access_scope PARAMS ((tree));
93 static void pop_access_scope PARAMS ((tree));
94 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
95 unification_kind_t, int));
96 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
97 unification_kind_t, int));
98 static int unify PARAMS ((tree, tree, tree, tree, int));
99 static void add_pending_template PARAMS ((tree));
100 static void reopen_tinst_level PARAMS ((tree));
101 static tree classtype_mangled_name PARAMS ((tree));
102 static char *mangle_class_name_for_template PARAMS ((const char *,
103 tree, tree));
104 static tree tsubst_initializer_list PARAMS ((tree, tree));
105 static int list_eq PARAMS ((tree, tree));
106 static tree get_class_bindings PARAMS ((tree, tree, tree));
107 static tree coerce_template_parms PARAMS ((tree, tree, tree,
108 tsubst_flags_t, int));
109 static void tsubst_enum PARAMS ((tree, tree, tree));
110 static tree add_to_template_args PARAMS ((tree, tree));
111 static tree add_outermost_template_args PARAMS ((tree, tree));
112 static bool check_instantiated_args PARAMS ((tree, tree, tsubst_flags_t));
113 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
114 tree*));
115 static int type_unification_real PARAMS ((tree, tree, tree, tree,
116 int, unification_kind_t, int, int));
117 static void note_template_header PARAMS ((int));
118 static tree maybe_fold_nontype_arg PARAMS ((tree));
119 static void maybe_fold_nontype_args PARAMS ((tree));
120 static tree convert_nontype_argument PARAMS ((tree, tree));
121 static tree convert_template_argument PARAMS ((tree, tree, tree,
122 tsubst_flags_t, int, tree));
123 static tree get_bindings_overload PARAMS ((tree, tree, tree));
124 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t));
125 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
126 static int inline_needs_template_parms PARAMS ((tree));
127 static void push_inline_template_parms_recursive PARAMS ((tree, int));
128 static tree retrieve_specialization PARAMS ((tree, tree));
129 static tree retrieve_local_specialization PARAMS ((tree));
130 static tree register_specialization PARAMS ((tree, tree, tree));
131 static void register_local_specialization PARAMS ((tree, tree));
132 static int unregister_specialization PARAMS ((tree, tree));
133 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
134 static tree build_template_decl PARAMS ((tree, tree));
135 static int mark_template_parm PARAMS ((tree, void *));
136 static int template_parm_this_level_p PARAMS ((tree, void *));
137 static tree tsubst_friend_function PARAMS ((tree, tree));
138 static tree tsubst_friend_class PARAMS ((tree, tree));
139 static int can_complete_type_without_circularity PARAMS ((tree));
140 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
141 static int template_decl_level PARAMS ((tree));
142 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
143 static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t));
144 static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t));
145 static void regenerate_decl_from_template PARAMS ((tree, tree));
146 static tree most_specialized PARAMS ((tree, tree, tree));
147 static tree most_specialized_class PARAMS ((tree, tree));
148 static int template_class_depth_real PARAMS ((tree, int));
149 static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int));
150 static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t));
151 static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree));
152 static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree));
153 static void check_specialization_scope PARAMS ((void));
154 static tree process_partial_specialization PARAMS ((tree));
155 static void set_current_access_from_decl PARAMS ((tree));
156 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
157 static tree tsubst_call_declarator_parms PARAMS ((tree, tree,
158 tsubst_flags_t, tree));
159 static tree get_template_base_recursive PARAMS ((tree, tree,
160 tree, tree, tree, int));
161 static tree get_template_base PARAMS ((tree, tree, tree, tree));
162 static int verify_class_unification PARAMS ((tree, tree, tree));
163 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
164 static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t,
165 tree, tree));
166 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
167 static int template_args_equal PARAMS ((tree, tree));
168 static void tsubst_default_arguments PARAMS ((tree));
169 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
170 static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
171 static void copy_default_args_to_explicit_spec PARAMS ((tree));
172 static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t));
173 static int eq_local_specializations (const void *, const void *);
174 static tree template_for_substitution (tree);
175 static bool dependent_type_p_r (tree);
176 static bool dependent_template_id_p (tree, tree);
177 static tree tsubst (tree, tree, tsubst_flags_t, tree);
178 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
180 static tree tsubst_copy_and_build (tree, tree, tsubst_flags_t, tree);
182 /* Make the current scope suitable for access checking when we are
183 processing T. T can be FUNCTION_DECL for instantiated function
184 template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for
185 static member variable (need by instantiate_decl). ARGS is the
186 template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE,
187 this is used instead of the context of T. */
189 void
190 push_access_scope_real (t, args, context)
191 tree t, args, context;
193 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
195 /* When we are processing specialization `foo<Outer>' for code like
197 template <class U> typename U::Inner foo ();
198 class Outer {
199 struct Inner {};
200 friend Outer::Inner foo<Outer> ();
203 `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of
204 its specialization. We can get the FUNCTION_DECL with the right
205 information because this specialization has already been
206 registered by the friend declaration above. */
208 if (DECL_FUNCTION_TEMPLATE_P (t) && args)
210 tree full_args = tsubst_template_arg_vector
211 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none);
212 tree spec = NULL_TREE;
213 if (full_args != error_mark_node)
214 spec = retrieve_specialization (t, full_args);
215 if (spec)
216 t = spec;
220 if (!context)
221 context = DECL_CONTEXT (t);
222 if (context && TYPE_P (context))
223 push_nested_class (context);
224 else
225 push_to_top_level ();
227 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
229 saved_access_scope = tree_cons
230 (NULL_TREE, current_function_decl, saved_access_scope);
231 current_function_decl = t;
235 /* Like push_access_scope_real, but always uses DECL_CONTEXT. */
237 void
238 push_access_scope (t)
239 tree t;
241 push_access_scope_real (t, NULL_TREE, NULL_TREE);
244 /* Restore the scope set up by push_access_scope. T is the node we
245 are processing. */
247 void
248 pop_access_scope (t)
249 tree t;
251 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
257 if (DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
268 tree
269 finish_member_template_decl (decl)
270 tree decl;
272 if (decl == error_mark_node)
273 return error_mark_node;
275 my_friendly_assert (DECL_P (decl), 20020812);
277 if (TREE_CODE (decl) == TYPE_DECL)
279 tree type;
281 type = TREE_TYPE (decl);
282 if (IS_AGGR_TYPE (type)
283 && CLASSTYPE_TEMPLATE_INFO (type)
284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
287 check_member_template (tmpl);
288 return tmpl;
290 return NULL_TREE;
292 else if (TREE_CODE (decl) == FIELD_DECL)
293 error ("data member `%D' cannot be a member template", decl);
294 else if (DECL_TEMPLATE_INFO (decl))
296 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 check_member_template (DECL_TI_TEMPLATE (decl));
299 return DECL_TI_TEMPLATE (decl);
301 else
302 return decl;
304 else
305 error ("invalid member template declaration `%D'", decl);
307 return error_mark_node;
310 /* Returns the template nesting level of the indicated class TYPE.
312 For example, in:
313 template <class T>
314 struct A
316 template <class U>
317 struct B {};
320 A<T>::B<U> has depth two, while A<T> has depth one.
321 Both A<T>::B<int> and A<int>::B<U> have depth one, if
322 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
323 specializations.
325 This function is guaranteed to return 0 if passed NULL_TREE so
326 that, for example, `template_class_depth (current_class_type)' is
327 always safe. */
329 static int
330 template_class_depth_real (type, count_specializations)
331 tree type;
332 int count_specializations;
334 int depth;
336 for (depth = 0;
337 type && TREE_CODE (type) != NAMESPACE_DECL;
338 type = (TREE_CODE (type) == FUNCTION_DECL)
339 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
341 if (TREE_CODE (type) != FUNCTION_DECL)
343 if (CLASSTYPE_TEMPLATE_INFO (type)
344 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
345 && ((count_specializations
346 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
347 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
348 ++depth;
350 else
352 if (DECL_TEMPLATE_INFO (type)
353 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
354 && ((count_specializations
355 && DECL_TEMPLATE_SPECIALIZATION (type))
356 || uses_template_parms (DECL_TI_ARGS (type))))
357 ++depth;
361 return depth;
364 /* Returns the template nesting level of the indicated class TYPE.
365 Like template_class_depth_real, but instantiations do not count in
366 the depth. */
368 int
369 template_class_depth (type)
370 tree type;
372 return template_class_depth_real (type, /*count_specializations=*/0);
375 /* Returns 1 if processing DECL as part of do_pending_inlines
376 needs us to push template parms. */
378 static int
379 inline_needs_template_parms (decl)
380 tree decl;
382 if (! DECL_TEMPLATE_INFO (decl))
383 return 0;
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
386 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
392 innermost first. */
394 static void
395 push_inline_template_parms_recursive (parmlist, levels)
396 tree parmlist;
397 int levels;
399 tree parms = TREE_VALUE (parmlist);
400 int i;
402 if (levels > 1)
403 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
405 ++processing_template_decl;
406 current_template_parms
407 = tree_cons (size_int (processing_template_decl),
408 parms, current_template_parms);
409 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
411 pushlevel (0);
412 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
414 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
415 my_friendly_assert (DECL_P (parm), 0);
417 switch (TREE_CODE (parm))
419 case TYPE_DECL:
420 case TEMPLATE_DECL:
421 pushdecl (parm);
422 break;
424 case PARM_DECL:
426 /* Make a CONST_DECL as is done in process_template_parm.
427 It is ugly that we recreate this here; the original
428 version built in process_template_parm is no longer
429 available. */
430 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
431 TREE_TYPE (parm));
432 DECL_ARTIFICIAL (decl) = 1;
433 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
434 DECL_INITIAL (decl) = DECL_INITIAL (parm);
435 SET_DECL_TEMPLATE_PARM_P (decl);
436 pushdecl (decl);
438 break;
440 default:
441 abort ();
446 /* Restore the template parameter context for a member template or
447 a friend template defined in a class definition. */
449 void
450 maybe_begin_member_template_processing (decl)
451 tree decl;
453 tree parms;
454 int levels = 0;
456 if (inline_needs_template_parms (decl))
458 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
459 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
461 if (DECL_TEMPLATE_SPECIALIZATION (decl))
463 --levels;
464 parms = TREE_CHAIN (parms);
467 push_inline_template_parms_recursive (parms, levels);
470 /* Remember how many levels of template parameters we pushed so that
471 we can pop them later. */
472 if (!inline_parm_levels)
473 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
474 if (inline_parm_levels_used == inline_parm_levels->num_elements)
475 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
476 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
477 ++inline_parm_levels_used;
480 /* Undo the effects of begin_member_template_processing. */
482 void
483 maybe_end_member_template_processing ()
485 int i;
487 if (!inline_parm_levels_used)
488 return;
490 --inline_parm_levels_used;
491 for (i = 0;
492 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
493 ++i)
495 --processing_template_decl;
496 current_template_parms = TREE_CHAIN (current_template_parms);
497 poplevel (0, 0, 0);
501 /* Returns nonzero iff T is a member template function. We must be
502 careful as in
504 template <class T> class C { void f(); }
506 Here, f is a template function, and a member, but not a member
507 template. This function does not concern itself with the origin of
508 T, only its present state. So if we have
510 template <class T> class C { template <class U> void f(U); }
512 then neither C<int>::f<char> nor C<T>::f<double> is considered
513 to be a member template. But, `template <class U> void
514 C<int>::f(U)' is considered a member template. */
517 is_member_template (t)
518 tree t;
520 if (!DECL_FUNCTION_TEMPLATE_P (t))
521 /* Anything that isn't a function or a template function is
522 certainly not a member template. */
523 return 0;
525 /* A local class can't have member templates. */
526 if (decl_function_context (t))
527 return 0;
529 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
530 /* If there are more levels of template parameters than
531 there are template classes surrounding the declaration,
532 then we have a member template. */
533 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
534 template_class_depth (DECL_CONTEXT (t))));
537 #if 0 /* UNUSED */
538 /* Returns nonzero iff T is a member template class. See
539 is_member_template for a description of what precisely constitutes
540 a member template. */
543 is_member_template_class (t)
544 tree t;
546 if (!DECL_CLASS_TEMPLATE_P (t))
547 /* Anything that isn't a class template, is certainly not a member
548 template. */
549 return 0;
551 if (!DECL_CLASS_SCOPE_P (t))
552 /* Anything whose context isn't a class type is surely not a
553 member template. */
554 return 0;
556 /* If there are more levels of template parameters than there are
557 template classes surrounding the declaration, then we have a
558 member template. */
559 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
560 template_class_depth (DECL_CONTEXT (t)));
562 #endif
564 /* Return a new template argument vector which contains all of ARGS,
565 but has as its innermost set of arguments the EXTRA_ARGS. */
567 static tree
568 add_to_template_args (args, extra_args)
569 tree args;
570 tree extra_args;
572 tree new_args;
573 int extra_depth;
574 int i;
575 int j;
577 extra_depth = TMPL_ARGS_DEPTH (extra_args);
578 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
580 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
583 for (j = 1; j <= extra_depth; ++j, ++i)
584 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
586 return new_args;
589 /* Like add_to_template_args, but only the outermost ARGS are added to
590 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
591 (EXTRA_ARGS) levels are added. This function is used to combine
592 the template arguments from a partial instantiation with the
593 template arguments used to attain the full instantiation from the
594 partial instantiation. */
596 static tree
597 add_outermost_template_args (args, extra_args)
598 tree args;
599 tree extra_args;
601 tree new_args;
603 /* If there are more levels of EXTRA_ARGS than there are ARGS,
604 something very fishy is going on. */
605 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
608 /* If *all* the new arguments will be the EXTRA_ARGS, just return
609 them. */
610 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
611 return extra_args;
613 /* For the moment, we make ARGS look like it contains fewer levels. */
614 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
616 new_args = add_to_template_args (args, extra_args);
618 /* Now, we restore ARGS to its full dimensions. */
619 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
621 return new_args;
624 /* Return the N levels of innermost template arguments from the ARGS. */
626 tree
627 get_innermost_template_args (args, n)
628 tree args;
629 int n;
631 tree new_args;
632 int extra_levels;
633 int i;
635 my_friendly_assert (n >= 0, 20000603);
637 /* If N is 1, just return the innermost set of template arguments. */
638 if (n == 1)
639 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
641 /* If we're not removing anything, just return the arguments we were
642 given. */
643 extra_levels = TMPL_ARGS_DEPTH (args) - n;
644 my_friendly_assert (extra_levels >= 0, 20000603);
645 if (extra_levels == 0)
646 return args;
648 /* Make a new set of arguments, not containing the outer arguments. */
649 new_args = make_tree_vec (n);
650 for (i = 1; i <= n; ++i)
651 SET_TMPL_ARGS_LEVEL (new_args, i,
652 TMPL_ARGS_LEVEL (args, i + extra_levels));
654 return new_args;
657 /* We've got a template header coming up; push to a new level for storing
658 the parms. */
660 void
661 begin_template_parm_list ()
663 /* We use a non-tag-transparent scope here, which causes pushtag to
664 put tags in this scope, rather than in the enclosing class or
665 namespace scope. This is the right thing, since we want
666 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
667 global template class, push_template_decl handles putting the
668 TEMPLATE_DECL into top-level scope. For a nested template class,
669 e.g.:
671 template <class T> struct S1 {
672 template <class T> struct S2 {};
675 pushtag contains special code to call pushdecl_with_scope on the
676 TEMPLATE_DECL for S2. */
677 begin_scope (sk_template_parms);
678 ++processing_template_decl;
679 ++processing_template_parmlist;
680 note_template_header (0);
683 /* This routine is called when a specialization is declared. If it is
684 invalid to declare a specialization here, an error is reported. */
686 static void
687 check_specialization_scope ()
689 tree scope = current_scope ();
691 /* [temp.expl.spec]
693 An explicit specialization shall be declared in the namespace of
694 which the template is a member, or, for member templates, in the
695 namespace of which the enclosing class or enclosing class
696 template is a member. An explicit specialization of a member
697 function, member class or static data member of a class template
698 shall be declared in the namespace of which the class template
699 is a member. */
700 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
701 error ("explicit specialization in non-namespace scope `%D'",
702 scope);
704 /* [temp.expl.spec]
706 In an explicit specialization declaration for a member of a class
707 template or a member template that appears in namespace scope,
708 the member template and some of its enclosing class templates may
709 remain unspecialized, except that the declaration shall not
710 explicitly specialize a class member template if its enclosing
711 class templates are not explicitly specialized as well. */
712 if (current_template_parms)
713 error ("enclosing class templates are not explicitly specialized");
716 /* We've just seen template <>. */
718 void
719 begin_specialization ()
721 begin_scope (sk_template_spec);
722 note_template_header (1);
723 check_specialization_scope ();
726 /* Called at then end of processing a declaration preceded by
727 template<>. */
729 void
730 end_specialization ()
732 finish_scope ();
733 reset_specialization ();
736 /* Any template <>'s that we have seen thus far are not referring to a
737 function specialization. */
739 void
740 reset_specialization ()
742 processing_specialization = 0;
743 template_header_count = 0;
746 /* We've just seen a template header. If SPECIALIZATION is nonzero,
747 it was of the form template <>. */
749 static void
750 note_template_header (specialization)
751 int specialization;
753 processing_specialization = specialization;
754 template_header_count++;
757 /* We're beginning an explicit instantiation. */
759 void
760 begin_explicit_instantiation ()
762 my_friendly_assert (!processing_explicit_instantiation, 20020913);
763 processing_explicit_instantiation = true;
767 void
768 end_explicit_instantiation ()
770 my_friendly_assert(processing_explicit_instantiation, 20020913);
771 processing_explicit_instantiation = false;
774 /* The TYPE is being declared. If it is a template type, that means it
775 is a partial specialization. Do appropriate error-checking. */
777 void
778 maybe_process_partial_specialization (type)
779 tree type;
781 /* TYPE maybe an ERROR_MARK_NODE. */
782 tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
784 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
786 /* This is for ordinary explicit specialization and partial
787 specialization of a template class such as:
789 template <> class C<int>;
793 template <class T> class C<T*>;
795 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
797 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
798 && !COMPLETE_TYPE_P (type))
800 if (current_namespace
801 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
803 pedwarn ("specializing `%#T' in different namespace", type);
804 cp_pedwarn_at (" from definition of `%#D'",
805 CLASSTYPE_TI_TEMPLATE (type));
807 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
808 if (processing_template_decl)
809 push_template_decl (TYPE_MAIN_DECL (type));
811 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
812 error ("specialization of `%T' after instantiation", type);
814 else if (CLASS_TYPE_P (type)
815 && !CLASSTYPE_USE_TEMPLATE (type)
816 && CLASSTYPE_TEMPLATE_INFO (type)
817 && context && CLASS_TYPE_P (context)
818 && CLASSTYPE_TEMPLATE_INFO (context))
820 /* This is for an explicit specialization of member class
821 template according to [temp.expl.spec/18]:
823 template <> template <class U> class C<int>::D;
825 The context `C<int>' must be an implicit instantiation.
826 Otherwise this is just a member class template declared
827 earlier like:
829 template <> class C<int> { template <class U> class D; };
830 template <> template <class U> class C<int>::D;
832 In the first case, `C<int>::D' is a specialization of `C<T>::D'
833 while in the second case, `C<int>::D' is a primary template
834 and `C<T>::D' may not exist. */
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
837 && !COMPLETE_TYPE_P (type))
839 tree t;
841 if (current_namespace
842 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
844 pedwarn ("specializing `%#T' in different namespace", type);
845 cp_pedwarn_at (" from definition of `%#D'",
846 CLASSTYPE_TI_TEMPLATE (type));
849 /* Check for invalid specialization after instantiation:
851 template <> template <> class C<int>::D<int>;
852 template <> template <class U> class C<int>::D; */
854 for (t = DECL_TEMPLATE_INSTANTIATIONS
855 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
856 t; t = TREE_CHAIN (t))
857 if (TREE_VALUE (t) != type
858 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
859 error ("specialization `%T' after instantiation `%T'",
860 type, TREE_VALUE (t));
862 /* Mark TYPE as a specialization. And as a result, we only
863 have one level of template argument for the innermost
864 class template. */
865 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
866 CLASSTYPE_TI_ARGS (type)
867 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
870 else if (processing_specialization)
871 error ("explicit specialization of non-template `%T'", type);
874 /* Retrieve the specialization (in the sense of [temp.spec] - a
875 specialization is either an instantiation or an explicit
876 specialization) of TMPL for the given template ARGS. If there is
877 no such specialization, return NULL_TREE. The ARGS are a vector of
878 arguments, or a vector of vectors of arguments, in the case of
879 templates with more than one level of parameters. */
881 static tree
882 retrieve_specialization (tmpl, args)
883 tree tmpl;
884 tree args;
886 tree s;
888 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
890 /* There should be as many levels of arguments as there are
891 levels of parameters. */
892 my_friendly_assert (TMPL_ARGS_DEPTH (args)
893 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
896 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
897 s != NULL_TREE;
898 s = TREE_CHAIN (s))
899 if (comp_template_args (TREE_PURPOSE (s), args))
900 return TREE_VALUE (s);
902 return NULL_TREE;
905 /* Like retrieve_specialization, but for local declarations. */
907 static tree
908 retrieve_local_specialization (tmpl)
909 tree tmpl;
911 tree spec =
912 (tree) htab_find_with_hash (local_specializations, tmpl,
913 htab_hash_pointer (tmpl));
914 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
917 /* Returns nonzero iff DECL is a specialization of TMPL. */
920 is_specialization_of (decl, tmpl)
921 tree decl;
922 tree tmpl;
924 tree t;
926 if (TREE_CODE (decl) == FUNCTION_DECL)
928 for (t = decl;
929 t != NULL_TREE;
930 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
931 if (t == tmpl)
932 return 1;
934 else
936 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
938 for (t = TREE_TYPE (decl);
939 t != NULL_TREE;
940 t = CLASSTYPE_USE_TEMPLATE (t)
941 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
942 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
943 return 1;
946 return 0;
949 /* Register the specialization SPEC as a specialization of TMPL with
950 the indicated ARGS. Returns SPEC, or an equivalent prior
951 declaration, if available. */
953 static tree
954 register_specialization (spec, tmpl, args)
955 tree spec;
956 tree tmpl;
957 tree args;
959 tree s;
961 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
963 if (TREE_CODE (spec) == FUNCTION_DECL
964 && uses_template_parms (DECL_TI_ARGS (spec)))
965 /* This is the FUNCTION_DECL for a partial instantiation. Don't
966 register it; we want the corresponding TEMPLATE_DECL instead.
967 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
968 the more obvious `uses_template_parms (spec)' to avoid problems
969 with default function arguments. In particular, given
970 something like this:
972 template <class T> void f(T t1, T t = T())
974 the default argument expression is not substituted for in an
975 instantiation unless and until it is actually needed. */
976 return spec;
978 /* There should be as many levels of arguments as there are
979 levels of parameters. */
980 my_friendly_assert (TMPL_ARGS_DEPTH (args)
981 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
984 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
985 s != NULL_TREE;
986 s = TREE_CHAIN (s))
988 tree fn = TREE_VALUE (s);
990 /* We can sometimes try to re-register a specialization that we've
991 already got. In particular, regenerate_decl_from_template
992 calls duplicate_decls which will update the specialization
993 list. But, we'll still get called again here anyhow. It's
994 more convenient to simply allow this than to try to prevent it. */
995 if (fn == spec)
996 return spec;
997 else if (comp_template_args (TREE_PURPOSE (s), args))
999 if (DECL_TEMPLATE_SPECIALIZATION (spec))
1001 if (DECL_TEMPLATE_INSTANTIATION (fn))
1003 if (TREE_USED (fn)
1004 || DECL_EXPLICIT_INSTANTIATION (fn))
1006 error ("specialization of %D after instantiation",
1007 fn);
1008 return spec;
1010 else
1012 /* This situation should occur only if the first
1013 specialization is an implicit instantiation,
1014 the second is an explicit specialization, and
1015 the implicit instantiation has not yet been
1016 used. That situation can occur if we have
1017 implicitly instantiated a member function and
1018 then specialized it later.
1020 We can also wind up here if a friend
1021 declaration that looked like an instantiation
1022 turns out to be a specialization:
1024 template <class T> void foo(T);
1025 class S { friend void foo<>(int) };
1026 template <> void foo(int);
1028 We transform the existing DECL in place so that
1029 any pointers to it become pointers to the
1030 updated declaration.
1032 If there was a definition for the template, but
1033 not for the specialization, we want this to
1034 look as if there is no definition, and vice
1035 versa. */
1036 DECL_INITIAL (fn) = NULL_TREE;
1037 duplicate_decls (spec, fn);
1039 return fn;
1042 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1044 duplicate_decls (spec, fn);
1045 return fn;
1051 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1052 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1054 return spec;
1057 /* Unregister the specialization SPEC as a specialization of TMPL.
1058 Returns nonzero if the SPEC was listed as a specialization of
1059 TMPL. */
1061 static int
1062 unregister_specialization (spec, tmpl)
1063 tree spec;
1064 tree tmpl;
1066 tree* s;
1068 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1069 *s != NULL_TREE;
1070 s = &TREE_CHAIN (*s))
1071 if (TREE_VALUE (*s) == spec)
1073 *s = TREE_CHAIN (*s);
1074 return 1;
1077 return 0;
1080 /* Compare an entry in the local specializations hash table P1 (which
1081 is really a pointer to a TREE_LIST) with P2 (which is really a
1082 DECL). */
1084 static int
1085 eq_local_specializations (const void *p1, const void *p2)
1087 return TREE_VALUE ((tree) p1) == (tree) p2;
1090 /* Hash P1, an entry in the local specializations table. */
1092 static hashval_t
1093 hash_local_specialization (const void* p1)
1095 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1098 /* Like register_specialization, but for local declarations. We are
1099 registering SPEC, an instantiation of TMPL. */
1101 static void
1102 register_local_specialization (spec, tmpl)
1103 tree spec;
1104 tree tmpl;
1106 void **slot;
1108 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1109 htab_hash_pointer (tmpl), INSERT);
1110 *slot = build_tree_list (spec, tmpl);
1113 /* Print the list of candidate FNS in an error message. */
1115 void
1116 print_candidates (fns)
1117 tree fns;
1119 tree fn;
1121 const char *str = "candidates are:";
1123 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1125 tree f;
1127 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1128 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
1129 str = " ";
1133 /* Returns the template (one of the functions given by TEMPLATE_ID)
1134 which can be specialized to match the indicated DECL with the
1135 explicit template args given in TEMPLATE_ID. The DECL may be
1136 NULL_TREE if none is available. In that case, the functions in
1137 TEMPLATE_ID are non-members.
1139 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1140 specialization of a member template.
1142 The template args (those explicitly specified and those deduced)
1143 are output in a newly created vector *TARGS_OUT.
1145 If it is impossible to determine the result, an error message is
1146 issued. The error_mark_node is returned to indicate failure. */
1148 static tree
1149 determine_specialization (template_id, decl, targs_out,
1150 need_member_template)
1151 tree template_id;
1152 tree decl;
1153 tree* targs_out;
1154 int need_member_template;
1156 tree fns;
1157 tree targs;
1158 tree explicit_targs;
1159 tree candidates = NULL_TREE;
1160 tree templates = NULL_TREE;
1162 *targs_out = NULL_TREE;
1164 if (template_id == error_mark_node)
1165 return error_mark_node;
1167 fns = TREE_OPERAND (template_id, 0);
1168 explicit_targs = TREE_OPERAND (template_id, 1);
1170 if (fns == error_mark_node)
1171 return error_mark_node;
1173 /* Check for baselinks. */
1174 if (BASELINK_P (fns))
1175 fns = BASELINK_FUNCTIONS (fns);
1177 if (!is_overloaded_fn (fns))
1179 error ("`%D' is not a function template", fns);
1180 return error_mark_node;
1183 for (; fns; fns = OVL_NEXT (fns))
1185 tree fn = OVL_CURRENT (fns);
1187 if (TREE_CODE (fn) == TEMPLATE_DECL)
1189 tree decl_arg_types;
1191 /* DECL might be a specialization of FN. */
1193 /* Adjust the type of DECL in case FN is a static member. */
1194 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1195 if (DECL_STATIC_FUNCTION_P (fn)
1196 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1197 decl_arg_types = TREE_CHAIN (decl_arg_types);
1199 /* Check that the number of function parameters matches.
1200 For example,
1201 template <class T> void f(int i = 0);
1202 template <> void f<int>();
1203 The specialization f<int> is invalid but is not caught
1204 by get_bindings below. */
1206 if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn)))
1207 != list_length (decl_arg_types))
1208 continue;
1210 /* See whether this function might be a specialization of this
1211 template. */
1212 targs = get_bindings (fn, decl, explicit_targs);
1214 if (!targs)
1215 /* We cannot deduce template arguments that when used to
1216 specialize TMPL will produce DECL. */
1217 continue;
1219 /* Save this template, and the arguments deduced. */
1220 templates = tree_cons (targs, fn, templates);
1222 else if (need_member_template)
1223 /* FN is an ordinary member function, and we need a
1224 specialization of a member template. */
1226 else if (TREE_CODE (fn) != FUNCTION_DECL)
1227 /* We can get IDENTIFIER_NODEs here in certain erroneous
1228 cases. */
1230 else if (!DECL_FUNCTION_MEMBER_P (fn))
1231 /* This is just an ordinary non-member function. Nothing can
1232 be a specialization of that. */
1234 else if (DECL_ARTIFICIAL (fn))
1235 /* Cannot specialize functions that are created implicitly. */
1237 else
1239 tree decl_arg_types;
1241 /* This is an ordinary member function. However, since
1242 we're here, we can assume it's enclosing class is a
1243 template class. For example,
1245 template <typename T> struct S { void f(); };
1246 template <> void S<int>::f() {}
1248 Here, S<int>::f is a non-template, but S<int> is a
1249 template class. If FN has the same type as DECL, we
1250 might be in business. */
1252 if (!DECL_TEMPLATE_INFO (fn))
1253 /* Its enclosing class is an explicit specialization
1254 of a template class. This is not a candidate. */
1255 continue;
1257 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1258 TREE_TYPE (TREE_TYPE (fn))))
1259 /* The return types differ. */
1260 continue;
1262 /* Adjust the type of DECL in case FN is a static member. */
1263 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1264 if (DECL_STATIC_FUNCTION_P (fn)
1265 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1266 decl_arg_types = TREE_CHAIN (decl_arg_types);
1268 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1269 decl_arg_types))
1270 /* They match! */
1271 candidates = tree_cons (NULL_TREE, fn, candidates);
1275 if (templates && TREE_CHAIN (templates))
1277 /* We have:
1279 [temp.expl.spec]
1281 It is possible for a specialization with a given function
1282 signature to be instantiated from more than one function
1283 template. In such cases, explicit specification of the
1284 template arguments must be used to uniquely identify the
1285 function template specialization being specialized.
1287 Note that here, there's no suggestion that we're supposed to
1288 determine which of the candidate templates is most
1289 specialized. However, we, also have:
1291 [temp.func.order]
1293 Partial ordering of overloaded function template
1294 declarations is used in the following contexts to select
1295 the function template to which a function template
1296 specialization refers:
1298 -- when an explicit specialization refers to a function
1299 template.
1301 So, we do use the partial ordering rules, at least for now.
1302 This extension can only serve to make invalid programs valid,
1303 so it's safe. And, there is strong anecdotal evidence that
1304 the committee intended the partial ordering rules to apply;
1305 the EDG front-end has that behavior, and John Spicer claims
1306 that the committee simply forgot to delete the wording in
1307 [temp.expl.spec]. */
1308 tree tmpl = most_specialized (templates, decl, explicit_targs);
1309 if (tmpl && tmpl != error_mark_node)
1311 targs = get_bindings (tmpl, decl, explicit_targs);
1312 templates = tree_cons (targs, tmpl, NULL_TREE);
1316 if (templates == NULL_TREE && candidates == NULL_TREE)
1318 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1319 template_id, decl);
1320 return error_mark_node;
1322 else if ((templates && TREE_CHAIN (templates))
1323 || (candidates && TREE_CHAIN (candidates))
1324 || (templates && candidates))
1326 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1327 template_id, decl);
1328 chainon (candidates, templates);
1329 print_candidates (candidates);
1330 return error_mark_node;
1333 /* We have one, and exactly one, match. */
1334 if (candidates)
1336 /* It was a specialization of an ordinary member function in a
1337 template class. */
1338 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1339 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1342 /* It was a specialization of a template. */
1343 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1344 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1346 *targs_out = copy_node (targs);
1347 SET_TMPL_ARGS_LEVEL (*targs_out,
1348 TMPL_ARGS_DEPTH (*targs_out),
1349 TREE_PURPOSE (templates));
1351 else
1352 *targs_out = TREE_PURPOSE (templates);
1353 return TREE_VALUE (templates);
1356 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1357 but with the default argument values filled in from those in the
1358 TMPL_TYPES. */
1360 static tree
1361 copy_default_args_to_explicit_spec_1 (spec_types,
1362 tmpl_types)
1363 tree spec_types;
1364 tree tmpl_types;
1366 tree new_spec_types;
1368 if (!spec_types)
1369 return NULL_TREE;
1371 if (spec_types == void_list_node)
1372 return void_list_node;
1374 /* Substitute into the rest of the list. */
1375 new_spec_types =
1376 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1377 TREE_CHAIN (tmpl_types));
1379 /* Add the default argument for this parameter. */
1380 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1381 TREE_VALUE (spec_types),
1382 new_spec_types);
1385 /* DECL is an explicit specialization. Replicate default arguments
1386 from the template it specializes. (That way, code like:
1388 template <class T> void f(T = 3);
1389 template <> void f(double);
1390 void g () { f (); }
1392 works, as required.) An alternative approach would be to look up
1393 the correct default arguments at the call-site, but this approach
1394 is consistent with how implicit instantiations are handled. */
1396 static void
1397 copy_default_args_to_explicit_spec (decl)
1398 tree decl;
1400 tree tmpl;
1401 tree spec_types;
1402 tree tmpl_types;
1403 tree new_spec_types;
1404 tree old_type;
1405 tree new_type;
1406 tree t;
1407 tree object_type = NULL_TREE;
1408 tree in_charge = NULL_TREE;
1409 tree vtt = NULL_TREE;
1411 /* See if there's anything we need to do. */
1412 tmpl = DECL_TI_TEMPLATE (decl);
1413 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1414 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1415 if (TREE_PURPOSE (t))
1416 break;
1417 if (!t)
1418 return;
1420 old_type = TREE_TYPE (decl);
1421 spec_types = TYPE_ARG_TYPES (old_type);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1425 /* Remove the this pointer, but remember the object's type for
1426 CV quals. */
1427 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1428 spec_types = TREE_CHAIN (spec_types);
1429 tmpl_types = TREE_CHAIN (tmpl_types);
1431 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1433 /* DECL may contain more parameters than TMPL due to the extra
1434 in-charge parameter in constructors and destructors. */
1435 in_charge = spec_types;
1436 spec_types = TREE_CHAIN (spec_types);
1438 if (DECL_HAS_VTT_PARM_P (decl))
1440 vtt = spec_types;
1441 spec_types = TREE_CHAIN (spec_types);
1445 /* Compute the merged default arguments. */
1446 new_spec_types =
1447 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1449 /* Compute the new FUNCTION_TYPE. */
1450 if (object_type)
1452 if (vtt)
1453 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1454 TREE_VALUE (vtt),
1455 new_spec_types);
1457 if (in_charge)
1458 /* Put the in-charge parameter back. */
1459 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1460 TREE_VALUE (in_charge),
1461 new_spec_types);
1463 new_type = build_cplus_method_type (object_type,
1464 TREE_TYPE (old_type),
1465 new_spec_types);
1467 else
1468 new_type = build_function_type (TREE_TYPE (old_type),
1469 new_spec_types);
1470 new_type = build_type_attribute_variant (new_type,
1471 TYPE_ATTRIBUTES (old_type));
1472 new_type = build_exception_variant (new_type,
1473 TYPE_RAISES_EXCEPTIONS (old_type));
1474 TREE_TYPE (decl) = new_type;
1477 /* Check to see if the function just declared, as indicated in
1478 DECLARATOR, and in DECL, is a specialization of a function
1479 template. We may also discover that the declaration is an explicit
1480 instantiation at this point.
1482 Returns DECL, or an equivalent declaration that should be used
1483 instead if all goes well. Issues an error message if something is
1484 amiss. Returns error_mark_node if the error is not easily
1485 recoverable.
1487 FLAGS is a bitmask consisting of the following flags:
1489 2: The function has a definition.
1490 4: The function is a friend.
1492 The TEMPLATE_COUNT is the number of references to qualifying
1493 template classes that appeared in the name of the function. For
1494 example, in
1496 template <class T> struct S { void f(); };
1497 void S<int>::f();
1499 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1500 classes are not counted in the TEMPLATE_COUNT, so that in
1502 template <class T> struct S {};
1503 template <> struct S<int> { void f(); }
1504 template <> void S<int>::f();
1506 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1507 invalid; there should be no template <>.)
1509 If the function is a specialization, it is marked as such via
1510 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1511 is set up correctly, and it is added to the list of specializations
1512 for that template. */
1514 tree
1515 check_explicit_specialization (declarator, decl, template_count, flags)
1516 tree declarator;
1517 tree decl;
1518 int template_count;
1519 int flags;
1521 int have_def = flags & 2;
1522 int is_friend = flags & 4;
1523 int specialization = 0;
1524 int explicit_instantiation = 0;
1525 int member_specialization = 0;
1526 tree ctype = DECL_CLASS_CONTEXT (decl);
1527 tree dname = DECL_NAME (decl);
1528 tmpl_spec_kind tsk;
1530 tsk = current_tmpl_spec_kind (template_count);
1532 switch (tsk)
1534 case tsk_none:
1535 if (processing_specialization)
1537 specialization = 1;
1538 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1540 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1542 if (is_friend)
1543 /* This could be something like:
1545 template <class T> void f(T);
1546 class S { friend void f<>(int); } */
1547 specialization = 1;
1548 else
1550 /* This case handles bogus declarations like template <>
1551 template <class T> void f<int>(); */
1553 error ("template-id `%D' in declaration of primary template",
1554 declarator);
1555 return decl;
1558 break;
1560 case tsk_invalid_member_spec:
1561 /* The error has already been reported in
1562 check_specialization_scope. */
1563 return error_mark_node;
1565 case tsk_invalid_expl_inst:
1566 error ("template parameter list used in explicit instantiation");
1568 /* Fall through. */
1570 case tsk_expl_inst:
1571 if (have_def)
1572 error ("definition provided for explicit instantiation");
1574 explicit_instantiation = 1;
1575 break;
1577 case tsk_excessive_parms:
1578 error ("too many template parameter lists in declaration of `%D'",
1579 decl);
1580 return error_mark_node;
1582 /* Fall through. */
1583 case tsk_expl_spec:
1584 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1585 if (ctype)
1586 member_specialization = 1;
1587 else
1588 specialization = 1;
1589 break;
1591 case tsk_insufficient_parms:
1592 if (template_header_count)
1594 error("too few template parameter lists in declaration of `%D'",
1595 decl);
1596 return decl;
1598 else if (ctype != NULL_TREE
1599 && !TYPE_BEING_DEFINED (ctype)
1600 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1601 && !is_friend)
1603 /* For backwards compatibility, we accept:
1605 template <class T> struct S { void f(); };
1606 void S<int>::f() {} // Missing template <>
1608 That used to be valid C++. */
1609 if (pedantic)
1610 pedwarn
1611 ("explicit specialization not preceded by `template <>'");
1612 specialization = 1;
1613 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1615 break;
1617 case tsk_template:
1618 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1620 /* This case handles bogus declarations like template <>
1621 template <class T> void f<int>(); */
1623 if (uses_template_parms (declarator))
1624 error ("partial specialization `%D' of function template",
1625 declarator);
1626 else
1627 error ("template-id `%D' in declaration of primary template",
1628 declarator);
1629 return decl;
1632 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1633 /* This is a specialization of a member template, without
1634 specialization the containing class. Something like:
1636 template <class T> struct S {
1637 template <class U> void f (U);
1639 template <> template <class U> void S<int>::f(U) {}
1641 That's a specialization -- but of the entire template. */
1642 specialization = 1;
1643 break;
1645 default:
1646 abort ();
1649 if (specialization || member_specialization)
1651 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1652 for (; t; t = TREE_CHAIN (t))
1653 if (TREE_PURPOSE (t))
1655 pedwarn
1656 ("default argument specified in explicit specialization");
1657 break;
1659 if (current_lang_name == lang_name_c)
1660 error ("template specialization with C linkage");
1663 if (specialization || member_specialization || explicit_instantiation)
1665 tree tmpl = NULL_TREE;
1666 tree targs = NULL_TREE;
1668 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1669 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1671 tree fns;
1673 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1675 if (!ctype)
1676 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1677 else
1678 fns = dname;
1680 declarator =
1681 lookup_template_function (fns, NULL_TREE);
1684 if (declarator == error_mark_node)
1685 return error_mark_node;
1687 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1689 if (!explicit_instantiation)
1690 /* A specialization in class scope. This is invalid,
1691 but the error will already have been flagged by
1692 check_specialization_scope. */
1693 return error_mark_node;
1694 else
1696 /* It's not valid to write an explicit instantiation in
1697 class scope, e.g.:
1699 class C { template void f(); }
1701 This case is caught by the parser. However, on
1702 something like:
1704 template class C { void f(); };
1706 (which is invalid) we can get here. The error will be
1707 issued later. */
1711 return decl;
1713 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1715 /* A friend declaration. We can't do much, because we don't
1716 know what this resolves to, yet. */
1717 my_friendly_assert (is_friend != 0, 0);
1718 my_friendly_assert (!explicit_instantiation, 0);
1719 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1720 return decl;
1722 else if (ctype != NULL_TREE
1723 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1724 IDENTIFIER_NODE))
1726 /* Find the list of functions in ctype that have the same
1727 name as the declared function. */
1728 tree name = TREE_OPERAND (declarator, 0);
1729 tree fns = NULL_TREE;
1730 int idx;
1732 if (constructor_name_p (name, ctype))
1734 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1736 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1737 : !TYPE_HAS_DESTRUCTOR (ctype))
1739 /* From [temp.expl.spec]:
1741 If such an explicit specialization for the member
1742 of a class template names an implicitly-declared
1743 special member function (clause _special_), the
1744 program is ill-formed.
1746 Similar language is found in [temp.explicit]. */
1747 error ("specialization of implicitly-declared special member function");
1748 return error_mark_node;
1751 name = is_constructor ? ctor_identifier : dtor_identifier;
1754 if (!DECL_CONV_FN_P (decl))
1756 idx = lookup_fnfields_1 (ctype, name);
1757 if (idx >= 0)
1758 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1760 else
1762 tree methods;
1764 /* For a type-conversion operator, we cannot do a
1765 name-based lookup. We might be looking for `operator
1766 int' which will be a specialization of `operator T'.
1767 So, we find *all* the conversion operators, and then
1768 select from them. */
1769 fns = NULL_TREE;
1771 methods = CLASSTYPE_METHOD_VEC (ctype);
1772 if (methods)
1773 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
1774 idx < TREE_VEC_LENGTH (methods); ++idx)
1776 tree ovl = TREE_VEC_ELT (methods, idx);
1778 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1779 /* There are no more conversion functions. */
1780 break;
1782 /* Glue all these conversion functions together
1783 with those we already have. */
1784 for (; ovl; ovl = OVL_NEXT (ovl))
1785 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1789 if (fns == NULL_TREE)
1791 error ("no member function `%D' declared in `%T'",
1792 name, ctype);
1793 return error_mark_node;
1795 else
1796 TREE_OPERAND (declarator, 0) = fns;
1799 /* Figure out what exactly is being specialized at this point.
1800 Note that for an explicit instantiation, even one for a
1801 member function, we cannot tell apriori whether the
1802 instantiation is for a member template, or just a member
1803 function of a template class. Even if a member template is
1804 being instantiated, the member template arguments may be
1805 elided if they can be deduced from the rest of the
1806 declaration. */
1807 tmpl = determine_specialization (declarator, decl,
1808 &targs,
1809 member_specialization);
1811 if (!tmpl || tmpl == error_mark_node)
1812 /* We couldn't figure out what this declaration was
1813 specializing. */
1814 return error_mark_node;
1815 else
1817 tree gen_tmpl = most_general_template (tmpl);
1819 if (explicit_instantiation)
1821 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1822 is done by do_decl_instantiation later. */
1824 int arg_depth = TMPL_ARGS_DEPTH (targs);
1825 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1827 if (arg_depth > parm_depth)
1829 /* If TMPL is not the most general template (for
1830 example, if TMPL is a friend template that is
1831 injected into namespace scope), then there will
1832 be too many levels of TARGS. Remove some of them
1833 here. */
1834 int i;
1835 tree new_targs;
1837 new_targs = make_tree_vec (parm_depth);
1838 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1839 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1840 = TREE_VEC_ELT (targs, i);
1841 targs = new_targs;
1844 return instantiate_template (tmpl, targs, tf_error);
1847 /* If this is a specialization of a member template of a
1848 template class. In we want to return the TEMPLATE_DECL,
1849 not the specialization of it. */
1850 if (tsk == tsk_template)
1852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1853 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1854 return tmpl;
1857 /* If we thought that the DECL was a member function, but it
1858 turns out to be specializing a static member function,
1859 make DECL a static member function as well. */
1860 if (DECL_STATIC_FUNCTION_P (tmpl)
1861 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1863 revert_static_member_fn (decl);
1864 last_function_parms = TREE_CHAIN (last_function_parms);
1867 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1868 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1870 /* Inherit default function arguments from the template
1871 DECL is specializing. */
1872 copy_default_args_to_explicit_spec (decl);
1874 /* This specialization has the same protection as the
1875 template it specializes. */
1876 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1877 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1879 if (is_friend && !have_def)
1880 /* This is not really a declaration of a specialization.
1881 It's just the name of an instantiation. But, it's not
1882 a request for an instantiation, either. */
1883 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1884 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1885 /* This is indeed a specialization. In case of constructors
1886 and destructors, we need in-charge and not-in-charge
1887 versions in V3 ABI. */
1888 clone_function_decl (decl, /*update_method_vec_p=*/0);
1890 /* Register this specialization so that we can find it
1891 again. */
1892 decl = register_specialization (decl, gen_tmpl, targs);
1896 return decl;
1899 /* TYPE is being declared. Verify that the use of template headers
1900 and such is reasonable. Issue error messages if not. */
1902 void
1903 maybe_check_template_type (type)
1904 tree type;
1906 if (template_header_count)
1908 /* We are in the scope of some `template <...>' header. */
1910 int context_depth
1911 = template_class_depth_real (TYPE_CONTEXT (type),
1912 /*count_specializations=*/1);
1914 if (template_header_count <= context_depth)
1915 /* This is OK; the template headers are for the context. We
1916 are actually too lenient here; like
1917 check_explicit_specialization we should consider the number
1918 of template types included in the actual declaration. For
1919 example,
1921 template <class T> struct S {
1922 template <class U> template <class V>
1923 struct I {};
1926 is invalid, but:
1928 template <class T> struct S {
1929 template <class U> struct I;
1932 template <class T> template <class U.
1933 struct S<T>::I {};
1935 is not. */
1937 else if (template_header_count > context_depth + 1)
1938 /* There are two many template parameter lists. */
1939 error ("too many template parameter lists in declaration of `%T'", type);
1943 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1944 parameters. These are represented in the same format used for
1945 DECL_TEMPLATE_PARMS. */
1947 int comp_template_parms (parms1, parms2)
1948 tree parms1;
1949 tree parms2;
1951 tree p1;
1952 tree p2;
1954 if (parms1 == parms2)
1955 return 1;
1957 for (p1 = parms1, p2 = parms2;
1958 p1 != NULL_TREE && p2 != NULL_TREE;
1959 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1961 tree t1 = TREE_VALUE (p1);
1962 tree t2 = TREE_VALUE (p2);
1963 int i;
1965 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1966 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1968 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1969 return 0;
1971 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1973 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1974 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1976 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1977 return 0;
1979 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1980 continue;
1981 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1982 return 0;
1986 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1987 /* One set of parameters has more parameters lists than the
1988 other. */
1989 return 0;
1991 return 1;
1994 /* Complain if DECL shadows a template parameter.
1996 [temp.local]: A template-parameter shall not be redeclared within its
1997 scope (including nested scopes). */
1999 void
2000 check_template_shadow (decl)
2001 tree decl;
2003 tree olddecl;
2005 /* If we're not in a template, we can't possibly shadow a template
2006 parameter. */
2007 if (!current_template_parms)
2008 return;
2010 /* Figure out what we're shadowing. */
2011 if (TREE_CODE (decl) == OVERLOAD)
2012 decl = OVL_CURRENT (decl);
2013 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
2015 /* If there's no previous binding for this name, we're not shadowing
2016 anything, let alone a template parameter. */
2017 if (!olddecl)
2018 return;
2020 /* If we're not shadowing a template parameter, we're done. Note
2021 that OLDDECL might be an OVERLOAD (or perhaps even an
2022 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2023 node. */
2024 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2025 return;
2027 /* We check for decl != olddecl to avoid bogus errors for using a
2028 name inside a class. We check TPFI to avoid duplicate errors for
2029 inline member templates. */
2030 if (decl == olddecl
2031 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2032 return;
2034 cp_error_at ("declaration of `%#D'", decl);
2035 cp_error_at (" shadows template parm `%#D'", olddecl);
2038 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2039 ORIG_LEVEL, DECL, and TYPE. */
2041 static tree
2042 build_template_parm_index (index, level, orig_level, decl, type)
2043 int index;
2044 int level;
2045 int orig_level;
2046 tree decl;
2047 tree type;
2049 tree t = make_node (TEMPLATE_PARM_INDEX);
2050 TEMPLATE_PARM_IDX (t) = index;
2051 TEMPLATE_PARM_LEVEL (t) = level;
2052 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2053 TEMPLATE_PARM_DECL (t) = decl;
2054 TREE_TYPE (t) = type;
2055 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2056 TREE_READONLY (t) = TREE_READONLY (decl);
2058 return t;
2061 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2062 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2063 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2064 new one is created. */
2066 static tree
2067 reduce_template_parm_level (index, type, levels)
2068 tree index;
2069 tree type;
2070 int levels;
2072 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2073 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2074 != TEMPLATE_PARM_LEVEL (index) - levels))
2076 tree orig_decl = TEMPLATE_PARM_DECL (index);
2077 tree decl, t;
2079 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2080 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2081 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2082 DECL_ARTIFICIAL (decl) = 1;
2083 SET_DECL_TEMPLATE_PARM_P (decl);
2085 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2086 TEMPLATE_PARM_LEVEL (index) - levels,
2087 TEMPLATE_PARM_ORIG_LEVEL (index),
2088 decl, type);
2089 TEMPLATE_PARM_DESCENDANTS (index) = t;
2091 /* Template template parameters need this. */
2092 DECL_TEMPLATE_PARMS (decl)
2093 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2096 return TEMPLATE_PARM_DESCENDANTS (index);
2099 /* Process information from new template parameter NEXT and append it to the
2100 LIST being built. */
2102 tree
2103 process_template_parm (list, next)
2104 tree list, next;
2106 tree parm;
2107 tree decl = 0;
2108 tree defval;
2109 int is_type, idx;
2111 parm = next;
2112 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
2113 defval = TREE_PURPOSE (parm);
2114 parm = TREE_VALUE (parm);
2115 is_type = TREE_PURPOSE (parm) == class_type_node;
2117 if (list)
2119 tree p = TREE_VALUE (tree_last (list));
2121 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2122 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2123 else
2124 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2125 ++idx;
2127 else
2128 idx = 0;
2130 if (!is_type)
2132 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
2133 /* is a const-param */
2134 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
2135 PARM, 0, NULL);
2136 SET_DECL_TEMPLATE_PARM_P (parm);
2138 /* [temp.param]
2140 The top-level cv-qualifiers on the template-parameter are
2141 ignored when determining its type. */
2142 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2144 /* A template parameter is not modifiable. */
2145 TREE_READONLY (parm) = TREE_CONSTANT (parm) = 1;
2146 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2147 TREE_TYPE (parm) = void_type_node;
2148 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2149 TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
2150 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2151 = build_template_parm_index (idx, processing_template_decl,
2152 processing_template_decl,
2153 decl, TREE_TYPE (parm));
2155 else
2157 tree t;
2158 parm = TREE_VALUE (parm);
2160 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2162 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2163 /* This is for distinguishing between real templates and template
2164 template parameters */
2165 TREE_TYPE (parm) = t;
2166 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2167 decl = parm;
2169 else
2171 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2172 /* parm is either IDENTIFIER_NODE or NULL_TREE */
2173 decl = build_decl (TYPE_DECL, parm, t);
2176 TYPE_NAME (t) = decl;
2177 TYPE_STUB_DECL (t) = decl;
2178 parm = decl;
2179 TEMPLATE_TYPE_PARM_INDEX (t)
2180 = build_template_parm_index (idx, processing_template_decl,
2181 processing_template_decl,
2182 decl, TREE_TYPE (parm));
2184 DECL_ARTIFICIAL (decl) = 1;
2185 SET_DECL_TEMPLATE_PARM_P (decl);
2186 pushdecl (decl);
2187 parm = build_tree_list (defval, parm);
2188 return chainon (list, parm);
2191 /* The end of a template parameter list has been reached. Process the
2192 tree list into a parameter vector, converting each parameter into a more
2193 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2194 as PARM_DECLs. */
2196 tree
2197 end_template_parm_list (parms)
2198 tree parms;
2200 int nparms;
2201 tree parm, next;
2202 tree saved_parmlist = make_tree_vec (list_length (parms));
2204 current_template_parms
2205 = tree_cons (size_int (processing_template_decl),
2206 saved_parmlist, current_template_parms);
2208 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2210 next = TREE_CHAIN (parm);
2211 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2212 TREE_CHAIN (parm) = NULL_TREE;
2215 --processing_template_parmlist;
2217 return saved_parmlist;
2220 /* end_template_decl is called after a template declaration is seen. */
2222 void
2223 end_template_decl ()
2225 reset_specialization ();
2227 if (! processing_template_decl)
2228 return;
2230 /* This matches the pushlevel in begin_template_parm_list. */
2231 finish_scope ();
2233 --processing_template_decl;
2234 current_template_parms = TREE_CHAIN (current_template_parms);
2237 /* Given a template argument vector containing the template PARMS.
2238 The innermost PARMS are given first. */
2240 tree
2241 current_template_args ()
2243 tree header;
2244 tree args = NULL_TREE;
2245 int length = TMPL_PARMS_DEPTH (current_template_parms);
2246 int l = length;
2248 /* If there is only one level of template parameters, we do not
2249 create a TREE_VEC of TREE_VECs. Instead, we return a single
2250 TREE_VEC containing the arguments. */
2251 if (length > 1)
2252 args = make_tree_vec (length);
2254 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2256 tree a = copy_node (TREE_VALUE (header));
2257 int i;
2259 TREE_TYPE (a) = NULL_TREE;
2260 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2262 tree t = TREE_VEC_ELT (a, i);
2264 /* T will be a list if we are called from within a
2265 begin/end_template_parm_list pair, but a vector directly
2266 if within a begin/end_member_template_processing pair. */
2267 if (TREE_CODE (t) == TREE_LIST)
2269 t = TREE_VALUE (t);
2271 if (TREE_CODE (t) == TYPE_DECL
2272 || TREE_CODE (t) == TEMPLATE_DECL)
2273 t = TREE_TYPE (t);
2274 else
2275 t = DECL_INITIAL (t);
2276 TREE_VEC_ELT (a, i) = t;
2280 if (length > 1)
2281 TREE_VEC_ELT (args, --l) = a;
2282 else
2283 args = a;
2286 return args;
2289 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2290 template PARMS. Used by push_template_decl below. */
2292 static tree
2293 build_template_decl (decl, parms)
2294 tree decl;
2295 tree parms;
2297 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2298 DECL_TEMPLATE_PARMS (tmpl) = parms;
2299 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2300 if (DECL_LANG_SPECIFIC (decl))
2302 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2303 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2304 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2305 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2306 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2307 if (DECL_OVERLOADED_OPERATOR_P (decl))
2308 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2309 DECL_OVERLOADED_OPERATOR_P (decl));
2312 return tmpl;
2315 struct template_parm_data
2317 /* The level of the template parameters we are currently
2318 processing. */
2319 int level;
2321 /* The index of the specialization argument we are currently
2322 processing. */
2323 int current_arg;
2325 /* An array whose size is the number of template parameters. The
2326 elements are nonzero if the parameter has been used in any one
2327 of the arguments processed so far. */
2328 int* parms;
2330 /* An array whose size is the number of template arguments. The
2331 elements are nonzero if the argument makes use of template
2332 parameters of this level. */
2333 int* arg_uses_template_parms;
2336 /* Subroutine of push_template_decl used to see if each template
2337 parameter in a partial specialization is used in the explicit
2338 argument list. If T is of the LEVEL given in DATA (which is
2339 treated as a template_parm_data*), then DATA->PARMS is marked
2340 appropriately. */
2342 static int
2343 mark_template_parm (t, data)
2344 tree t;
2345 void* data;
2347 int level;
2348 int idx;
2349 struct template_parm_data* tpd = (struct template_parm_data*) data;
2351 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2353 level = TEMPLATE_PARM_LEVEL (t);
2354 idx = TEMPLATE_PARM_IDX (t);
2356 else
2358 level = TEMPLATE_TYPE_LEVEL (t);
2359 idx = TEMPLATE_TYPE_IDX (t);
2362 if (level == tpd->level)
2364 tpd->parms[idx] = 1;
2365 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2368 /* Return zero so that for_each_template_parm will continue the
2369 traversal of the tree; we want to mark *every* template parm. */
2370 return 0;
2373 /* Process the partial specialization DECL. */
2375 static tree
2376 process_partial_specialization (decl)
2377 tree decl;
2379 tree type = TREE_TYPE (decl);
2380 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2381 tree specargs = CLASSTYPE_TI_ARGS (type);
2382 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2383 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2384 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2385 int nargs = TREE_VEC_LENGTH (inner_args);
2386 int ntparms = TREE_VEC_LENGTH (inner_parms);
2387 int i;
2388 int did_error_intro = 0;
2389 struct template_parm_data tpd;
2390 struct template_parm_data tpd2;
2392 /* We check that each of the template parameters given in the
2393 partial specialization is used in the argument list to the
2394 specialization. For example:
2396 template <class T> struct S;
2397 template <class T> struct S<T*>;
2399 The second declaration is OK because `T*' uses the template
2400 parameter T, whereas
2402 template <class T> struct S<int>;
2404 is no good. Even trickier is:
2406 template <class T>
2407 struct S1
2409 template <class U>
2410 struct S2;
2411 template <class U>
2412 struct S2<T>;
2415 The S2<T> declaration is actually invalid; it is a
2416 full-specialization. Of course,
2418 template <class U>
2419 struct S2<T (*)(U)>;
2421 or some such would have been OK. */
2422 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2423 tpd.parms = alloca (sizeof (int) * ntparms);
2424 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2426 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2427 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2428 for (i = 0; i < nargs; ++i)
2430 tpd.current_arg = i;
2431 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2432 &mark_template_parm,
2433 &tpd,
2434 NULL);
2436 for (i = 0; i < ntparms; ++i)
2437 if (tpd.parms[i] == 0)
2439 /* One of the template parms was not used in the
2440 specialization. */
2441 if (!did_error_intro)
2443 error ("template parameters not used in partial specialization:");
2444 did_error_intro = 1;
2447 error (" `%D'",
2448 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2451 /* [temp.class.spec]
2453 The argument list of the specialization shall not be identical to
2454 the implicit argument list of the primary template. */
2455 if (comp_template_args
2456 (inner_args,
2457 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2458 (maintmpl)))))
2459 error ("partial specialization `%T' does not specialize any template arguments", type);
2461 /* [temp.class.spec]
2463 A partially specialized non-type argument expression shall not
2464 involve template parameters of the partial specialization except
2465 when the argument expression is a simple identifier.
2467 The type of a template parameter corresponding to a specialized
2468 non-type argument shall not be dependent on a parameter of the
2469 specialization. */
2470 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2471 tpd2.parms = 0;
2472 for (i = 0; i < nargs; ++i)
2474 tree arg = TREE_VEC_ELT (inner_args, i);
2475 if (/* These first two lines are the `non-type' bit. */
2476 !TYPE_P (arg)
2477 && TREE_CODE (arg) != TEMPLATE_DECL
2478 /* This next line is the `argument expression is not just a
2479 simple identifier' condition and also the `specialized
2480 non-type argument' bit. */
2481 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2483 if (tpd.arg_uses_template_parms[i])
2484 error ("template argument `%E' involves template parameter(s)", arg);
2485 else
2487 /* Look at the corresponding template parameter,
2488 marking which template parameters its type depends
2489 upon. */
2490 tree type =
2491 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2492 i)));
2494 if (!tpd2.parms)
2496 /* We haven't yet initialized TPD2. Do so now. */
2497 tpd2.arg_uses_template_parms
2498 = (int*) alloca (sizeof (int) * nargs);
2499 /* The number of parameters here is the number in the
2500 main template, which, as checked in the assertion
2501 above, is NARGS. */
2502 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2503 tpd2.level =
2504 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2507 /* Mark the template parameters. But this time, we're
2508 looking for the template parameters of the main
2509 template, not in the specialization. */
2510 tpd2.current_arg = i;
2511 tpd2.arg_uses_template_parms[i] = 0;
2512 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2513 for_each_template_parm (type,
2514 &mark_template_parm,
2515 &tpd2,
2516 NULL);
2518 if (tpd2.arg_uses_template_parms [i])
2520 /* The type depended on some template parameters.
2521 If they are fully specialized in the
2522 specialization, that's OK. */
2523 int j;
2524 for (j = 0; j < nargs; ++j)
2525 if (tpd2.parms[j] != 0
2526 && tpd.arg_uses_template_parms [j])
2528 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2529 type,
2530 arg);
2531 break;
2538 if (retrieve_specialization (maintmpl, specargs))
2539 /* We've already got this specialization. */
2540 return decl;
2542 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2543 = tree_cons (inner_args, inner_parms,
2544 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2545 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2546 return decl;
2549 /* Check that a template declaration's use of default arguments is not
2550 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2551 nonzero if DECL is the thing declared by a primary template.
2552 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2554 static void
2555 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2556 tree decl;
2557 tree parms;
2558 int is_primary;
2559 int is_partial;
2561 const char *msg;
2562 int last_level_to_check;
2563 tree parm_level;
2565 /* [temp.param]
2567 A default template-argument shall not be specified in a
2568 function template declaration or a function template definition, nor
2569 in the template-parameter-list of the definition of a member of a
2570 class template. */
2572 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2573 /* You can't have a function template declaration in a local
2574 scope, nor you can you define a member of a class template in a
2575 local scope. */
2576 return;
2578 if (current_class_type
2579 && !TYPE_BEING_DEFINED (current_class_type)
2580 && DECL_LANG_SPECIFIC (decl)
2581 /* If this is either a friend defined in the scope of the class
2582 or a member function. */
2583 && ((DECL_CONTEXT (decl)
2584 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2585 || (DECL_FRIEND_CONTEXT (decl)
2586 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2587 current_class_type)))
2588 /* And, if it was a member function, it really was defined in
2589 the scope of the class. */
2590 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2591 /* We already checked these parameters when the template was
2592 declared, so there's no need to do it again now. This function
2593 was defined in class scope, but we're processing it's body now
2594 that the class is complete. */
2595 return;
2597 /* [temp.param]
2599 If a template-parameter has a default template-argument, all
2600 subsequent template-parameters shall have a default
2601 template-argument supplied. */
2602 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2604 tree inner_parms = TREE_VALUE (parm_level);
2605 int ntparms = TREE_VEC_LENGTH (inner_parms);
2606 int seen_def_arg_p = 0;
2607 int i;
2609 for (i = 0; i < ntparms; ++i)
2611 tree parm = TREE_VEC_ELT (inner_parms, i);
2612 if (TREE_PURPOSE (parm))
2613 seen_def_arg_p = 1;
2614 else if (seen_def_arg_p)
2616 error ("no default argument for `%D'", TREE_VALUE (parm));
2617 /* For better subsequent error-recovery, we indicate that
2618 there should have been a default argument. */
2619 TREE_PURPOSE (parm) = error_mark_node;
2624 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2625 /* For an ordinary class template, default template arguments are
2626 allowed at the innermost level, e.g.:
2627 template <class T = int>
2628 struct S {};
2629 but, in a partial specialization, they're not allowed even
2630 there, as we have in [temp.class.spec]:
2632 The template parameter list of a specialization shall not
2633 contain default template argument values.
2635 So, for a partial specialization, or for a function template,
2636 we look at all of them. */
2638 else
2639 /* But, for a primary class template that is not a partial
2640 specialization we look at all template parameters except the
2641 innermost ones. */
2642 parms = TREE_CHAIN (parms);
2644 /* Figure out what error message to issue. */
2645 if (TREE_CODE (decl) == FUNCTION_DECL)
2646 msg = "default template arguments may not be used in function templates";
2647 else if (is_partial)
2648 msg = "default template arguments may not be used in partial specializations";
2649 else
2650 msg = "default argument for template parameter for class enclosing `%D'";
2652 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2653 /* If we're inside a class definition, there's no need to
2654 examine the parameters to the class itself. On the one
2655 hand, they will be checked when the class is defined, and,
2656 on the other, default arguments are valid in things like:
2657 template <class T = double>
2658 struct S { template <class U> void f(U); };
2659 Here the default argument for `S' has no bearing on the
2660 declaration of `f'. */
2661 last_level_to_check = template_class_depth (current_class_type) + 1;
2662 else
2663 /* Check everything. */
2664 last_level_to_check = 0;
2666 for (parm_level = parms;
2667 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2668 parm_level = TREE_CHAIN (parm_level))
2670 tree inner_parms = TREE_VALUE (parm_level);
2671 int i;
2672 int ntparms;
2674 ntparms = TREE_VEC_LENGTH (inner_parms);
2675 for (i = 0; i < ntparms; ++i)
2676 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2678 if (msg)
2680 error (msg, decl);
2681 msg = 0;
2684 /* Clear out the default argument so that we are not
2685 confused later. */
2686 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2689 /* At this point, if we're still interested in issuing messages,
2690 they must apply to classes surrounding the object declared. */
2691 if (msg)
2692 msg = "default argument for template parameter for class enclosing `%D'";
2696 /* Worker for push_template_decl_real, called via
2697 for_each_template_parm. DATA is really an int, indicating the
2698 level of the parameters we are interested in. If T is a template
2699 parameter of that level, return nonzero. */
2701 static int
2702 template_parm_this_level_p (t, data)
2703 tree t;
2704 void *data;
2706 int this_level = *(int *)data;
2707 int level;
2709 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2710 level = TEMPLATE_PARM_LEVEL (t);
2711 else
2712 level = TEMPLATE_TYPE_LEVEL (t);
2713 return level == this_level;
2716 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2717 parameters given by current_template_args, or reuses a
2718 previously existing one, if appropriate. Returns the DECL, or an
2719 equivalent one, if it is replaced via a call to duplicate_decls.
2721 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2723 tree
2724 push_template_decl_real (decl, is_friend)
2725 tree decl;
2726 int is_friend;
2728 tree tmpl;
2729 tree args;
2730 tree info;
2731 tree ctx;
2732 int primary;
2733 int is_partial;
2734 int new_template_p = 0;
2736 /* See if this is a partial specialization. */
2737 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2738 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2739 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2741 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2743 if (is_friend)
2744 /* For a friend, we want the context of the friend function, not
2745 the type of which it is a friend. */
2746 ctx = DECL_CONTEXT (decl);
2747 else if (CP_DECL_CONTEXT (decl)
2748 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2749 /* In the case of a virtual function, we want the class in which
2750 it is defined. */
2751 ctx = CP_DECL_CONTEXT (decl);
2752 else
2753 /* Otherwise, if we're currently defining some class, the DECL
2754 is assumed to be a member of the class. */
2755 ctx = current_scope ();
2757 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2758 ctx = NULL_TREE;
2760 if (!DECL_CONTEXT (decl))
2761 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2763 /* See if this is a primary template. */
2764 primary = template_parm_scope_p ();
2766 if (primary)
2768 if (current_lang_name == lang_name_c)
2769 error ("template with C linkage");
2770 else if (TREE_CODE (decl) == TYPE_DECL
2771 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2772 error ("template class without a name");
2773 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2774 && CLASS_TYPE_P (TREE_TYPE (decl)))
2775 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2776 || TREE_CODE (decl) == FUNCTION_DECL)
2777 /* OK */;
2778 else
2779 error ("template declaration of `%#D'", decl);
2782 /* Check to see that the rules regarding the use of default
2783 arguments are not being violated. */
2784 check_default_tmpl_args (decl, current_template_parms,
2785 primary, is_partial);
2787 if (is_partial)
2788 return process_partial_specialization (decl);
2790 args = current_template_args ();
2792 if (!ctx
2793 || TREE_CODE (ctx) == FUNCTION_DECL
2794 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
2795 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2797 if (DECL_LANG_SPECIFIC (decl)
2798 && DECL_TEMPLATE_INFO (decl)
2799 && DECL_TI_TEMPLATE (decl))
2800 tmpl = DECL_TI_TEMPLATE (decl);
2801 /* If DECL is a TYPE_DECL for a class-template, then there won't
2802 be DECL_LANG_SPECIFIC. The information equivalent to
2803 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2804 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2805 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2806 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2808 /* Since a template declaration already existed for this
2809 class-type, we must be redeclaring it here. Make sure
2810 that the redeclaration is valid. */
2811 redeclare_class_template (TREE_TYPE (decl),
2812 current_template_parms);
2813 /* We don't need to create a new TEMPLATE_DECL; just use the
2814 one we already had. */
2815 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2817 else
2819 tmpl = build_template_decl (decl, current_template_parms);
2820 new_template_p = 1;
2822 if (DECL_LANG_SPECIFIC (decl)
2823 && DECL_TEMPLATE_SPECIALIZATION (decl))
2825 /* A specialization of a member template of a template
2826 class. */
2827 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2828 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2829 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2833 else
2835 tree a, t, current, parms;
2836 int i;
2838 if (TREE_CODE (decl) == TYPE_DECL)
2840 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2841 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2842 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2843 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2844 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2845 else
2847 error ("`%D' does not declare a template type", decl);
2848 return decl;
2851 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2853 error ("template definition of non-template `%#D'", decl);
2854 return decl;
2856 else
2857 tmpl = DECL_TI_TEMPLATE (decl);
2859 if (is_member_template (tmpl)
2860 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2861 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2862 && DECL_TEMPLATE_SPECIALIZATION (decl))
2864 tree new_tmpl;
2866 /* The declaration is a specialization of a member
2867 template, declared outside the class. Therefore, the
2868 innermost template arguments will be NULL, so we
2869 replace them with the arguments determined by the
2870 earlier call to check_explicit_specialization. */
2871 args = DECL_TI_ARGS (decl);
2873 new_tmpl
2874 = build_template_decl (decl, current_template_parms);
2875 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2876 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2877 DECL_TI_TEMPLATE (decl) = new_tmpl;
2878 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2879 DECL_TEMPLATE_INFO (new_tmpl)
2880 = tree_cons (tmpl, args, NULL_TREE);
2882 register_specialization (new_tmpl,
2883 most_general_template (tmpl),
2884 args);
2885 return decl;
2888 /* Make sure the template headers we got make sense. */
2890 parms = DECL_TEMPLATE_PARMS (tmpl);
2891 i = TMPL_PARMS_DEPTH (parms);
2892 if (TMPL_ARGS_DEPTH (args) != i)
2894 error ("expected %d levels of template parms for `%#D', got %d",
2895 i, decl, TMPL_ARGS_DEPTH (args));
2897 else
2898 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2900 a = TMPL_ARGS_LEVEL (args, i);
2901 t = INNERMOST_TEMPLATE_PARMS (parms);
2903 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2905 if (current == decl)
2906 error ("got %d template parameters for `%#D'",
2907 TREE_VEC_LENGTH (a), decl);
2908 else
2909 error ("got %d template parameters for `%#T'",
2910 TREE_VEC_LENGTH (a), current);
2911 error (" but %d required", TREE_VEC_LENGTH (t));
2914 /* Perhaps we should also check that the parms are used in the
2915 appropriate qualifying scopes in the declarator? */
2917 if (current == decl)
2918 current = ctx;
2919 else
2920 current = TYPE_CONTEXT (current);
2924 DECL_TEMPLATE_RESULT (tmpl) = decl;
2925 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2927 /* Push template declarations for global functions and types. Note
2928 that we do not try to push a global template friend declared in a
2929 template class; such a thing may well depend on the template
2930 parameters of the class. */
2931 if (new_template_p && !ctx
2932 && !(is_friend && template_class_depth (current_class_type) > 0))
2933 tmpl = pushdecl_namespace_level (tmpl);
2935 if (primary)
2937 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2938 if (DECL_CONV_FN_P (tmpl))
2940 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2942 /* It is a conversion operator. See if the type converted to
2943 depends on innermost template operands. */
2945 if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)),
2946 template_parm_this_level_p,
2947 &depth,
2948 NULL))
2949 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
2953 info = tree_cons (tmpl, args, NULL_TREE);
2955 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2957 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2958 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2959 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2960 /* Don't change the name if we've already set it up. */
2961 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2962 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2964 else if (DECL_LANG_SPECIFIC (decl))
2965 DECL_TEMPLATE_INFO (decl) = info;
2967 return DECL_TEMPLATE_RESULT (tmpl);
2970 tree
2971 push_template_decl (decl)
2972 tree decl;
2974 return push_template_decl_real (decl, 0);
2977 /* Called when a class template TYPE is redeclared with the indicated
2978 template PARMS, e.g.:
2980 template <class T> struct S;
2981 template <class T> struct S {}; */
2983 void
2984 redeclare_class_template (type, parms)
2985 tree type;
2986 tree parms;
2988 tree tmpl;
2989 tree tmpl_parms;
2990 int i;
2992 if (!TYPE_TEMPLATE_INFO (type))
2994 error ("`%T' is not a template type", type);
2995 return;
2998 tmpl = TYPE_TI_TEMPLATE (type);
2999 if (!PRIMARY_TEMPLATE_P (tmpl))
3000 /* The type is nested in some template class. Nothing to worry
3001 about here; there are no new template parameters for the nested
3002 type. */
3003 return;
3005 parms = INNERMOST_TEMPLATE_PARMS (parms);
3006 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3008 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3010 cp_error_at ("previous declaration `%D'", tmpl);
3011 error ("used %d template parameter%s instead of %d",
3012 TREE_VEC_LENGTH (tmpl_parms),
3013 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
3014 TREE_VEC_LENGTH (parms));
3015 return;
3018 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3020 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3021 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3022 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3023 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3025 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
3027 cp_error_at ("template parameter `%#D'", tmpl_parm);
3028 error ("redeclared here as `%#D'", parm);
3029 return;
3032 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3034 /* We have in [temp.param]:
3036 A template-parameter may not be given default arguments
3037 by two different declarations in the same scope. */
3038 error ("redefinition of default argument for `%#D'", parm);
3039 cp_error_at (" original definition appeared here", tmpl_parm);
3040 return;
3043 if (parm_default != NULL_TREE)
3044 /* Update the previous template parameters (which are the ones
3045 that will really count) with the new default value. */
3046 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3047 else if (tmpl_default != NULL_TREE)
3048 /* Update the new parameters, too; they'll be used as the
3049 parameters for any members. */
3050 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3054 /* Attempt to convert the non-type template parameter EXPR to the
3055 indicated TYPE. If the conversion is successful, return the
3056 converted value. If the conversion is unsuccessful, return
3057 NULL_TREE if we issued an error message, or error_mark_node if we
3058 did not. We issue error messages for out-and-out bad template
3059 parameters, but not simply because the conversion failed, since we
3060 might be just trying to do argument deduction. By the time this
3061 function is called, neither TYPE nor EXPR may make use of template
3062 parameters. */
3064 static tree
3065 convert_nontype_argument (type, expr)
3066 tree type;
3067 tree expr;
3069 tree expr_type = TREE_TYPE (expr);
3071 /* A template-argument for a non-type, non-template
3072 template-parameter shall be one of:
3074 --an integral constant-expression of integral or enumeration
3075 type; or
3077 --the name of a non-type template-parameter; or
3079 --the name of an object or function with external linkage,
3080 including function templates and function template-ids but
3081 excluding non-static class members, expressed as id-expression;
3084 --the address of an object or function with external linkage,
3085 including function templates and function template-ids but
3086 excluding non-static class members, expressed as & id-expression
3087 where the & is optional if the name refers to a function or
3088 array; or
3090 --a pointer to member expressed as described in _expr.unary.op_. */
3092 /* An integral constant-expression can include const variables or
3093 enumerators. Simplify things by folding them to their values,
3094 unless we're about to bind the declaration to a reference
3095 parameter. */
3096 if (INTEGRAL_TYPE_P (expr_type)
3097 && TREE_CODE (type) != REFERENCE_TYPE)
3098 expr = decl_constant_value (expr);
3100 if (is_overloaded_fn (expr))
3101 /* OK for now. We'll check that it has external linkage later.
3102 Check this first since if expr_type is the unknown_type_node
3103 we would otherwise complain below. */
3105 else if (TYPE_PTRMEM_P (expr_type)
3106 || TYPE_PTRMEMFUNC_P (expr_type))
3108 if (TREE_CODE (expr) != PTRMEM_CST)
3109 goto bad_argument;
3111 else if (TYPE_PTR_P (expr_type)
3112 || TYPE_PTRMEM_P (expr_type)
3113 || TREE_CODE (expr_type) == ARRAY_TYPE
3114 || TREE_CODE (type) == REFERENCE_TYPE
3115 /* If expr is the address of an overloaded function, we
3116 will get the unknown_type_node at this point. */
3117 || expr_type == unknown_type_node)
3119 tree referent;
3120 tree e = expr;
3121 STRIP_NOPS (e);
3123 if (TREE_CODE (expr_type) == ARRAY_TYPE
3124 || (TREE_CODE (type) == REFERENCE_TYPE
3125 && TREE_CODE (e) != ADDR_EXPR))
3126 referent = e;
3127 else
3129 if (TREE_CODE (e) != ADDR_EXPR)
3131 bad_argument:
3132 error ("`%E' is not a valid template argument", expr);
3133 if (TYPE_PTR_P (expr_type))
3135 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
3136 error ("it must be the address of a function with external linkage");
3137 else
3138 error ("it must be the address of an object with external linkage");
3140 else if (TYPE_PTRMEM_P (expr_type)
3141 || TYPE_PTRMEMFUNC_P (expr_type))
3142 error ("it must be a pointer-to-member of the form `&X::Y'");
3144 return NULL_TREE;
3147 referent = TREE_OPERAND (e, 0);
3148 STRIP_NOPS (referent);
3151 if (TREE_CODE (referent) == STRING_CST)
3153 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
3154 referent);
3155 return NULL_TREE;
3158 if (is_overloaded_fn (referent))
3159 /* We'll check that it has external linkage later. */
3161 else if (TREE_CODE (referent) != VAR_DECL)
3162 goto bad_argument;
3163 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
3165 error ("address of non-extern `%E' cannot be used as template argument", referent);
3166 return error_mark_node;
3169 else if (INTEGRAL_TYPE_P (expr_type)
3170 || TYPE_PTRMEM_P (expr_type)
3171 || TYPE_PTRMEMFUNC_P (expr_type))
3173 if (! TREE_CONSTANT (expr))
3175 non_constant:
3176 error ("non-constant `%E' cannot be used as template argument",
3177 expr);
3178 return NULL_TREE;
3181 else
3183 error ("object `%E' cannot be used as template argument", expr);
3184 return NULL_TREE;
3187 switch (TREE_CODE (type))
3189 case INTEGER_TYPE:
3190 case BOOLEAN_TYPE:
3191 case ENUMERAL_TYPE:
3192 /* For a non-type template-parameter of integral or enumeration
3193 type, integral promotions (_conv.prom_) and integral
3194 conversions (_conv.integral_) are applied. */
3195 if (!INTEGRAL_TYPE_P (expr_type))
3196 return error_mark_node;
3198 /* It's safe to call digest_init in this case; we know we're
3199 just converting one integral constant expression to another. */
3200 expr = digest_init (type, expr, (tree*) 0);
3202 if (TREE_CODE (expr) != INTEGER_CST)
3203 /* Curiously, some TREE_CONSTANT integral expressions do not
3204 simplify to integer constants. For example, `3 % 0',
3205 remains a TRUNC_MOD_EXPR. */
3206 goto non_constant;
3208 return expr;
3210 case POINTER_TYPE:
3212 tree type_pointed_to = TREE_TYPE (type);
3214 if (TYPE_PTRMEM_P (type))
3216 tree e;
3218 /* For a non-type template-parameter of type pointer to data
3219 member, qualification conversions (_conv.qual_) are
3220 applied. */
3221 e = perform_qualification_conversions (type, expr);
3222 if (TREE_CODE (e) == NOP_EXPR)
3223 /* The call to perform_qualification_conversions will
3224 insert a NOP_EXPR over EXPR to do express conversion,
3225 if necessary. But, that will confuse us if we use
3226 this (converted) template parameter to instantiate
3227 another template; then the thing will not look like a
3228 valid template argument. So, just make a new
3229 constant, of the appropriate type. */
3230 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
3231 return e;
3233 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3235 /* For a non-type template-parameter of type pointer to
3236 function, only the function-to-pointer conversion
3237 (_conv.func_) is applied. If the template-argument
3238 represents a set of overloaded functions (or a pointer to
3239 such), the matching function is selected from the set
3240 (_over.over_). */
3241 tree fns;
3242 tree fn;
3244 if (TREE_CODE (expr) == ADDR_EXPR)
3245 fns = TREE_OPERAND (expr, 0);
3246 else
3247 fns = expr;
3249 fn = instantiate_type (type_pointed_to, fns, tf_none);
3251 if (fn == error_mark_node)
3252 return error_mark_node;
3254 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3256 if (really_overloaded_fn (fns))
3257 return error_mark_node;
3258 else
3259 goto bad_argument;
3262 expr = build_unary_op (ADDR_EXPR, fn, 0);
3264 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3266 return expr;
3268 else
3270 /* For a non-type template-parameter of type pointer to
3271 object, qualification conversions (_conv.qual_) and the
3272 array-to-pointer conversion (_conv.array_) are applied.
3273 [Note: In particular, neither the null pointer conversion
3274 (_conv.ptr_) nor the derived-to-base conversion
3275 (_conv.ptr_) are applied. Although 0 is a valid
3276 template-argument for a non-type template-parameter of
3277 integral type, it is not a valid template-argument for a
3278 non-type template-parameter of pointer type.]
3280 The call to decay_conversion performs the
3281 array-to-pointer conversion, if appropriate. */
3282 expr = decay_conversion (expr);
3284 if (expr == error_mark_node)
3285 return error_mark_node;
3286 else
3287 return perform_qualification_conversions (type, expr);
3290 break;
3292 case REFERENCE_TYPE:
3294 tree type_referred_to = TREE_TYPE (type);
3296 /* If this expression already has reference type, get the
3297 underling object. */
3298 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3300 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3301 expr = TREE_OPERAND (expr, 0);
3302 expr_type = TREE_TYPE (expr);
3305 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3307 /* For a non-type template-parameter of type reference to
3308 function, no conversions apply. If the
3309 template-argument represents a set of overloaded
3310 functions, the matching function is selected from the
3311 set (_over.over_). */
3312 tree fn;
3314 fn = instantiate_type (type_referred_to, expr, tf_none);
3316 if (fn == error_mark_node)
3317 return error_mark_node;
3319 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3321 if (really_overloaded_fn (expr))
3322 /* Don't issue an error here; we might get a different
3323 function if the overloading had worked out
3324 differently. */
3325 return error_mark_node;
3326 else
3327 goto bad_argument;
3330 my_friendly_assert (same_type_p (type_referred_to,
3331 TREE_TYPE (fn)),
3334 expr = fn;
3336 else
3338 /* For a non-type template-parameter of type reference to
3339 object, no conversions apply. The type referred to by the
3340 reference may be more cv-qualified than the (otherwise
3341 identical) type of the template-argument. The
3342 template-parameter is bound directly to the
3343 template-argument, which must be an lvalue. */
3344 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3345 TYPE_MAIN_VARIANT (type_referred_to))
3346 || !at_least_as_qualified_p (type_referred_to,
3347 expr_type)
3348 || !real_lvalue_p (expr))
3349 return error_mark_node;
3352 cxx_mark_addressable (expr);
3353 return build1 (ADDR_EXPR, type, expr);
3355 break;
3357 case RECORD_TYPE:
3359 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3361 /* For a non-type template-parameter of type pointer to member
3362 function, no conversions apply. If the template-argument
3363 represents a set of overloaded member functions, the
3364 matching member function is selected from the set
3365 (_over.over_). */
3367 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3368 expr_type != unknown_type_node)
3369 return error_mark_node;
3371 if (TREE_CODE (expr) == PTRMEM_CST)
3373 /* A ptr-to-member constant. */
3374 if (!same_type_p (type, expr_type))
3375 return error_mark_node;
3376 else
3377 return expr;
3380 if (TREE_CODE (expr) != ADDR_EXPR)
3381 return error_mark_node;
3383 expr = instantiate_type (type, expr, tf_none);
3385 if (expr == error_mark_node)
3386 return error_mark_node;
3388 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3390 return expr;
3392 break;
3394 default:
3395 /* All non-type parameters must have one of these types. */
3396 abort ();
3397 break;
3400 return error_mark_node;
3403 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3404 template template parameters. Both PARM_PARMS and ARG_PARMS are
3405 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3406 or PARM_DECL.
3408 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3409 the case, then extra parameters must have default arguments.
3411 Consider the example:
3412 template <class T, class Allocator = allocator> class vector;
3413 template<template <class U> class TT> class C;
3415 C<vector> is a valid instantiation. PARM_PARMS for the above code
3416 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3417 T and Allocator) and OUTER_ARGS contains the argument that is used to
3418 substitute the TT parameter. */
3420 static int
3421 coerce_template_template_parms (parm_parms, arg_parms, complain,
3422 in_decl, outer_args)
3423 tree parm_parms, arg_parms;
3424 tsubst_flags_t complain;
3425 tree in_decl, outer_args;
3427 int nparms, nargs, i;
3428 tree parm, arg;
3430 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3431 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3433 nparms = TREE_VEC_LENGTH (parm_parms);
3434 nargs = TREE_VEC_LENGTH (arg_parms);
3436 /* The rule here is opposite of coerce_template_parms. */
3437 if (nargs < nparms
3438 || (nargs > nparms
3439 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3440 return 0;
3442 for (i = 0; i < nparms; ++i)
3444 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3445 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3447 if (arg == NULL_TREE || arg == error_mark_node
3448 || parm == NULL_TREE || parm == error_mark_node)
3449 return 0;
3451 if (TREE_CODE (arg) != TREE_CODE (parm))
3452 return 0;
3454 switch (TREE_CODE (parm))
3456 case TYPE_DECL:
3457 break;
3459 case TEMPLATE_DECL:
3460 /* We encounter instantiations of templates like
3461 template <template <template <class> class> class TT>
3462 class C; */
3464 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3465 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3467 if (!coerce_template_template_parms (parmparm, argparm,
3468 complain, in_decl,
3469 outer_args))
3470 return 0;
3472 break;
3474 case PARM_DECL:
3475 /* The tsubst call is used to handle cases such as
3476 template <class T, template <T> class TT> class D;
3477 i.e. the parameter list of TT depends on earlier parameters. */
3478 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3479 complain, in_decl),
3480 TREE_TYPE (arg)))
3481 return 0;
3482 break;
3484 default:
3485 abort ();
3488 return 1;
3491 /* Convert the indicated template ARG as necessary to match the
3492 indicated template PARM. Returns the converted ARG, or
3493 error_mark_node if the conversion was unsuccessful. Error and
3494 warning messages are issued under control of COMPLAIN. This
3495 conversion is for the Ith parameter in the parameter list. ARGS is
3496 the full set of template arguments deduced so far. */
3498 static tree
3499 convert_template_argument (parm, arg, args, complain, i, in_decl)
3500 tree parm;
3501 tree arg;
3502 tree args;
3503 tsubst_flags_t complain;
3504 int i;
3505 tree in_decl;
3507 tree val;
3508 tree inner_args;
3509 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3511 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3513 if (TREE_CODE (arg) == TREE_LIST
3514 && TREE_TYPE (arg) != NULL_TREE
3515 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3517 /* The template argument was the name of some
3518 member function. That's usually
3519 invalid, but static members are OK. In any
3520 case, grab the underlying fields/functions
3521 and issue an error later if required. */
3522 arg = TREE_VALUE (arg);
3523 TREE_TYPE (arg) = unknown_type_node;
3526 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3527 requires_type = (TREE_CODE (parm) == TYPE_DECL
3528 || requires_tmpl_type);
3530 if (TREE_CODE (arg) != RECORD_TYPE)
3531 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3532 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3533 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3534 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3535 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3536 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3538 if (is_base_of_enclosing_class (arg, current_class_type))
3539 /* This is a template name used within the scope of the
3540 template. It could be the template, or it could be the
3541 instantiation. Choose whichever makes sense. */
3542 is_tmpl_type = requires_tmpl_type;
3543 else
3544 is_tmpl_type = 1;
3546 else
3547 /* It is a non-template class, or a specialization of a template
3548 class, or a non-template member of a template class. */
3549 is_tmpl_type = 0;
3551 if (is_tmpl_type
3552 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3553 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3554 arg = TYPE_STUB_DECL (arg);
3555 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3556 arg = CLASSTYPE_TI_TEMPLATE (arg);
3558 is_type = TYPE_P (arg) || is_tmpl_type;
3560 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3561 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3563 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3565 arg = make_typename_type (TREE_OPERAND (arg, 0),
3566 TREE_OPERAND (arg, 1),
3567 complain & tf_error);
3568 is_type = 1;
3570 if (is_type != requires_type)
3572 if (in_decl)
3574 if (complain & tf_error)
3576 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3577 i + 1, in_decl);
3578 if (is_type)
3579 error (" expected a constant of type `%T', got `%T'",
3580 TREE_TYPE (parm),
3581 (is_tmpl_type ? DECL_NAME (arg) : arg));
3582 else if (requires_tmpl_type)
3583 error (" expected a class template, got `%E'", arg);
3584 else
3585 error (" expected a type, got `%E'", arg);
3588 return error_mark_node;
3590 if (is_tmpl_type ^ requires_tmpl_type)
3592 if (in_decl && (complain & tf_error))
3594 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3595 i + 1, in_decl);
3596 if (is_tmpl_type)
3597 error (" expected a type, got `%T'", DECL_NAME (arg));
3598 else
3599 error (" expected a class template, got `%T'", arg);
3601 return error_mark_node;
3604 if (is_type)
3606 if (requires_tmpl_type)
3608 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3609 /* The number of argument required is not known yet.
3610 Just accept it for now. */
3611 val = TREE_TYPE (arg);
3612 else
3614 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3615 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3617 if (coerce_template_template_parms (parmparm, argparm,
3618 complain, in_decl,
3619 inner_args))
3621 val = arg;
3623 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3624 TEMPLATE_DECL. */
3625 if (val != error_mark_node
3626 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3627 val = TREE_TYPE (val);
3629 else
3631 if (in_decl && (complain & tf_error))
3633 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3634 i + 1, in_decl);
3635 error (" expected a template of type `%D', got `%D'", parm, arg);
3638 val = error_mark_node;
3642 else
3643 val = groktypename (arg);
3645 else
3647 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3649 if (invalid_nontype_parm_type_p (t, complain))
3650 return error_mark_node;
3652 if (processing_template_decl)
3653 arg = maybe_fold_nontype_arg (arg);
3655 if (!uses_template_parms (arg) && !uses_template_parms (t))
3656 /* We used to call digest_init here. However, digest_init
3657 will report errors, which we don't want when complain
3658 is zero. More importantly, digest_init will try too
3659 hard to convert things: for example, `0' should not be
3660 converted to pointer type at this point according to
3661 the standard. Accepting this is not merely an
3662 extension, since deciding whether or not these
3663 conversions can occur is part of determining which
3664 function template to call, or whether a given explicit
3665 argument specification is valid. */
3666 val = convert_nontype_argument (t, arg);
3667 else
3668 val = arg;
3670 if (val == NULL_TREE)
3671 val = error_mark_node;
3672 else if (val == error_mark_node && (complain & tf_error))
3673 error ("could not convert template argument `%E' to `%T'",
3674 arg, t);
3677 return val;
3680 /* Convert all template arguments to their appropriate types, and
3681 return a vector containing the innermost resulting template
3682 arguments. If any error occurs, return error_mark_node. Error and
3683 warning messages are issued under control of COMPLAIN.
3685 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3686 provided in ARGLIST, or else trailing parameters must have default
3687 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3688 deduction for any unspecified trailing arguments. */
3690 static tree
3691 coerce_template_parms (parms, args, in_decl,
3692 complain,
3693 require_all_arguments)
3694 tree parms, args;
3695 tree in_decl;
3696 tsubst_flags_t complain;
3697 int require_all_arguments;
3699 int nparms, nargs, i, lost = 0;
3700 tree inner_args;
3701 tree new_args;
3702 tree new_inner_args;
3704 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3705 nargs = NUM_TMPL_ARGS (inner_args);
3706 nparms = TREE_VEC_LENGTH (parms);
3708 if (nargs > nparms
3709 || (nargs < nparms
3710 && require_all_arguments
3711 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3713 if (complain & tf_error)
3715 error ("wrong number of template arguments (%d, should be %d)",
3716 nargs, nparms);
3718 if (in_decl)
3719 cp_error_at ("provided for `%D'", in_decl);
3722 return error_mark_node;
3725 new_inner_args = make_tree_vec (nparms);
3726 new_args = add_outermost_template_args (args, new_inner_args);
3727 for (i = 0; i < nparms; i++)
3729 tree arg;
3730 tree parm;
3732 /* Get the Ith template parameter. */
3733 parm = TREE_VEC_ELT (parms, i);
3735 /* Calculate the Ith argument. */
3736 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3738 arg = TREE_VALUE (inner_args);
3739 inner_args = TREE_CHAIN (inner_args);
3741 else if (i < nargs)
3742 arg = TREE_VEC_ELT (inner_args, i);
3743 /* If no template argument was supplied, look for a default
3744 value. */
3745 else if (TREE_PURPOSE (parm) == NULL_TREE)
3747 /* There was no default value. */
3748 my_friendly_assert (!require_all_arguments, 0);
3749 break;
3751 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3752 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3753 else
3754 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3755 in_decl);
3757 /* Now, convert the Ith argument, as necessary. */
3758 if (arg == NULL_TREE)
3759 /* We're out of arguments. */
3761 my_friendly_assert (!require_all_arguments, 0);
3762 break;
3764 else if (arg == error_mark_node)
3766 error ("template argument %d is invalid", i + 1);
3767 arg = error_mark_node;
3769 else
3770 arg = convert_template_argument (TREE_VALUE (parm),
3771 arg, new_args, complain, i,
3772 in_decl);
3774 if (arg == error_mark_node)
3775 lost++;
3776 TREE_VEC_ELT (new_inner_args, i) = arg;
3779 if (lost)
3780 return error_mark_node;
3782 return new_inner_args;
3785 /* Returns 1 if template args OT and NT are equivalent. */
3787 static int
3788 template_args_equal (ot, nt)
3789 tree ot, nt;
3791 if (nt == ot)
3792 return 1;
3794 if (TREE_CODE (nt) == TREE_VEC)
3795 /* For member templates */
3796 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3797 else if (TYPE_P (nt))
3798 return TYPE_P (ot) && same_type_p (ot, nt);
3799 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3800 return 0;
3801 else
3802 return (cp_tree_equal (ot, nt) > 0);
3805 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3806 of template arguments. Returns 0 otherwise. */
3809 comp_template_args (oldargs, newargs)
3810 tree oldargs, newargs;
3812 int i;
3814 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3815 return 0;
3817 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3819 tree nt = TREE_VEC_ELT (newargs, i);
3820 tree ot = TREE_VEC_ELT (oldargs, i);
3822 if (! template_args_equal (ot, nt))
3823 return 0;
3825 return 1;
3828 /* Given class template name and parameter list, produce a user-friendly name
3829 for the instantiation. */
3831 static char *
3832 mangle_class_name_for_template (name, parms, arglist)
3833 const char *name;
3834 tree parms, arglist;
3836 static struct obstack scratch_obstack;
3837 static char *scratch_firstobj;
3838 int i, nparms;
3840 if (!scratch_firstobj)
3841 gcc_obstack_init (&scratch_obstack);
3842 else
3843 obstack_free (&scratch_obstack, scratch_firstobj);
3844 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3846 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3847 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3849 cat (name);
3850 ccat ('<');
3851 nparms = TREE_VEC_LENGTH (parms);
3852 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3853 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3854 for (i = 0; i < nparms; i++)
3856 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3857 tree arg = TREE_VEC_ELT (arglist, i);
3859 if (i)
3860 ccat (',');
3862 if (TREE_CODE (parm) == TYPE_DECL)
3864 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3865 continue;
3867 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3869 if (TREE_CODE (arg) == TEMPLATE_DECL)
3871 /* Already substituted with real template. Just output
3872 the template name here */
3873 tree context = DECL_CONTEXT (arg);
3874 if (context)
3876 /* The template may be defined in a namespace, or
3877 may be a member template. */
3878 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3879 || CLASS_TYPE_P (context),
3880 980422);
3881 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3882 cat("::");
3884 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3886 else
3887 /* Output the parameter declaration */
3888 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3889 continue;
3891 else
3892 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3894 if (TREE_CODE (arg) == TREE_LIST)
3896 /* New list cell was built because old chain link was in
3897 use. */
3898 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3899 arg = TREE_VALUE (arg);
3901 /* No need to check arglist against parmlist here; we did that
3902 in coerce_template_parms, called from lookup_template_class. */
3903 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3906 char *bufp = obstack_next_free (&scratch_obstack);
3907 int offset = 0;
3908 while (bufp[offset - 1] == ' ')
3909 offset--;
3910 obstack_blank_fast (&scratch_obstack, offset);
3912 /* B<C<char> >, not B<C<char>> */
3913 if (bufp[offset - 1] == '>')
3914 ccat (' ');
3916 ccat ('>');
3917 ccat ('\0');
3918 return (char *) obstack_base (&scratch_obstack);
3921 static tree
3922 classtype_mangled_name (t)
3923 tree t;
3925 if (CLASSTYPE_TEMPLATE_INFO (t)
3926 /* Specializations have already had their names set up in
3927 lookup_template_class. */
3928 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3930 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3932 /* For non-primary templates, the template parameters are
3933 implicit from their surrounding context. */
3934 if (PRIMARY_TEMPLATE_P (tmpl))
3936 tree name = DECL_NAME (tmpl);
3937 char *mangled_name = mangle_class_name_for_template
3938 (IDENTIFIER_POINTER (name),
3939 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3940 CLASSTYPE_TI_ARGS (t));
3941 tree id = get_identifier (mangled_name);
3942 IDENTIFIER_TEMPLATE (id) = name;
3943 return id;
3947 return TYPE_IDENTIFIER (t);
3950 static void
3951 add_pending_template (d)
3952 tree d;
3954 tree ti = (TYPE_P (d)
3955 ? CLASSTYPE_TEMPLATE_INFO (d)
3956 : DECL_TEMPLATE_INFO (d));
3957 tree pt;
3958 int level;
3960 if (TI_PENDING_TEMPLATE_FLAG (ti))
3961 return;
3963 /* We are called both from instantiate_decl, where we've already had a
3964 tinst_level pushed, and instantiate_template, where we haven't.
3965 Compensate. */
3966 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3968 if (level)
3969 push_tinst_level (d);
3971 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3972 if (last_pending_template)
3973 TREE_CHAIN (last_pending_template) = pt;
3974 else
3975 pending_templates = pt;
3977 last_pending_template = pt;
3979 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3981 if (level)
3982 pop_tinst_level ();
3986 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
3987 ARGLIST. Valid choices for FNS are given in the cp-tree.def
3988 documentation for TEMPLATE_ID_EXPR. */
3990 tree
3991 lookup_template_function (fns, arglist)
3992 tree fns, arglist;
3994 tree type;
3996 if (fns == error_mark_node || arglist == error_mark_node)
3997 return error_mark_node;
3999 if (fns == NULL_TREE)
4001 error ("non-template used as template");
4002 return error_mark_node;
4005 my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL
4006 || TREE_CODE (fns) == OVERLOAD
4007 || BASELINK_P (fns)
4008 || TREE_CODE (fns) == IDENTIFIER_NODE
4009 || TREE_CODE (fns) == LOOKUP_EXPR,
4010 20020730);
4012 if (BASELINK_P (fns))
4014 BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR,
4015 unknown_type_node,
4016 BASELINK_FUNCTIONS (fns),
4017 arglist);
4018 return fns;
4021 type = TREE_TYPE (fns);
4022 if (TREE_CODE (fns) == OVERLOAD || !type)
4023 type = unknown_type_node;
4025 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
4028 /* Within the scope of a template class S<T>, the name S gets bound
4029 (in build_self_reference) to a TYPE_DECL for the class, not a
4030 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4031 or one of its enclosing classes, and that type is a template,
4032 return the associated TEMPLATE_DECL. Otherwise, the original
4033 DECL is returned. */
4035 tree
4036 maybe_get_template_decl_from_type_decl (decl)
4037 tree decl;
4039 return (decl != NULL_TREE
4040 && TREE_CODE (decl) == TYPE_DECL
4041 && DECL_ARTIFICIAL (decl)
4042 && CLASS_TYPE_P (TREE_TYPE (decl))
4043 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4044 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4047 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4048 parameters, find the desired type.
4050 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4051 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
4052 be a TREE_LIST if called directly from the parser, and a TREE_VEC
4053 otherwise.)
4055 IN_DECL, if non-NULL, is the template declaration we are trying to
4056 instantiate.
4058 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4059 the class we are looking up.
4061 Issue error and warning messages under control of COMPLAIN.
4063 If the template class is really a local class in a template
4064 function, then the FUNCTION_CONTEXT is the function in which it is
4065 being instantiated. */
4067 tree
4068 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
4069 tree d1, arglist;
4070 tree in_decl;
4071 tree context;
4072 int entering_scope;
4073 tsubst_flags_t complain;
4075 tree template = NULL_TREE, parmlist;
4076 tree t;
4078 timevar_push (TV_NAME_LOOKUP);
4079 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4081 if (IDENTIFIER_VALUE (d1)
4082 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
4083 template = IDENTIFIER_VALUE (d1);
4084 else
4086 if (context)
4087 push_decl_namespace (context);
4088 template = lookup_name (d1, /*prefer_type=*/0);
4089 template = maybe_get_template_decl_from_type_decl (template);
4090 if (context)
4091 pop_decl_namespace ();
4093 if (template)
4094 context = DECL_CONTEXT (template);
4096 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4098 tree type = TREE_TYPE (d1);
4100 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4101 an implicit typename for the second A. Deal with it. */
4102 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4103 type = TREE_TYPE (type);
4105 if (CLASSTYPE_TEMPLATE_INFO (type))
4107 template = CLASSTYPE_TI_TEMPLATE (type);
4108 d1 = DECL_NAME (template);
4111 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4112 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4114 template = TYPE_TI_TEMPLATE (d1);
4115 d1 = DECL_NAME (template);
4117 else if (TREE_CODE (d1) == TEMPLATE_DECL
4118 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4120 template = d1;
4121 d1 = DECL_NAME (template);
4122 context = DECL_CONTEXT (template);
4125 /* With something like `template <class T> class X class X { ... };'
4126 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
4127 We don't want to do that, but we have to deal with the situation,
4128 so let's give them some syntax errors to chew on instead of a
4129 crash. Alternatively D1 might not be a template type at all. */
4130 if (! template)
4132 if (complain & tf_error)
4133 error ("`%T' is not a template", d1);
4134 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4137 if (TREE_CODE (template) != TEMPLATE_DECL
4138 /* If we're called from the parser, make sure it's a user visible
4139 template. */
4140 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
4141 && !DECL_TEMPLATE_PARM_P (template)
4142 && !PRIMARY_TEMPLATE_P (template)))
4144 if (complain & tf_error)
4146 error ("non-template type `%T' used as a template", d1);
4147 if (in_decl)
4148 cp_error_at ("for template declaration `%D'", in_decl);
4150 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4153 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4155 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4156 template arguments */
4158 tree parm;
4159 tree arglist2;
4161 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4163 /* Consider an example where a template template parameter declared as
4165 template <class T, class U = std::allocator<T> > class TT
4167 The template parameter level of T and U are one level larger than
4168 of TT. To proper process the default argument of U, say when an
4169 instantiation `TT<int>' is seen, we need to build the full
4170 arguments containing {int} as the innermost level. Outer levels,
4171 available when not appearing as default template argument, can be
4172 obtained from `current_template_args ()'.
4174 Suppose that TT is later substituted with std::vector. The above
4175 instantiation is `TT<int, std::allocator<T> >' with TT at
4176 level 1, and T at level 2, while the template arguments at level 1
4177 becomes {std::vector} and the inner level 2 is {int}. */
4179 if (current_template_parms)
4180 arglist = add_to_template_args (current_template_args (), arglist);
4182 arglist2 = coerce_template_parms (parmlist, arglist, template,
4183 complain, /*require_all_args=*/1);
4184 if (arglist2 == error_mark_node
4185 || (!uses_template_parms (arglist2)
4186 && check_instantiated_args (template, arglist2, complain)))
4187 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4189 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4190 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4192 else
4194 tree template_type = TREE_TYPE (template);
4195 tree gen_tmpl;
4196 tree type_decl;
4197 tree found = NULL_TREE;
4198 tree *tp;
4199 int arg_depth;
4200 int parm_depth;
4201 int is_partial_instantiation;
4203 gen_tmpl = most_general_template (template);
4204 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4205 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4206 arg_depth = TMPL_ARGS_DEPTH (arglist);
4208 if (arg_depth == 1 && parm_depth > 1)
4210 /* We've been given an incomplete set of template arguments.
4211 For example, given:
4213 template <class T> struct S1 {
4214 template <class U> struct S2 {};
4215 template <class U> struct S2<U*> {};
4218 we will be called with an ARGLIST of `U*', but the
4219 TEMPLATE will be `template <class T> template
4220 <class U> struct S1<T>::S2'. We must fill in the missing
4221 arguments. */
4222 arglist
4223 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4224 arglist);
4225 arg_depth = TMPL_ARGS_DEPTH (arglist);
4228 /* Now we should have enough arguments. */
4229 my_friendly_assert (parm_depth == arg_depth, 0);
4231 /* From here on, we're only interested in the most general
4232 template. */
4233 template = gen_tmpl;
4235 /* Calculate the BOUND_ARGS. These will be the args that are
4236 actually tsubst'd into the definition to create the
4237 instantiation. */
4238 if (parm_depth > 1)
4240 /* We have multiple levels of arguments to coerce, at once. */
4241 int i;
4242 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4244 tree bound_args = make_tree_vec (parm_depth);
4246 for (i = saved_depth,
4247 t = DECL_TEMPLATE_PARMS (template);
4248 i > 0 && t != NULL_TREE;
4249 --i, t = TREE_CHAIN (t))
4251 tree a = coerce_template_parms (TREE_VALUE (t),
4252 arglist, template,
4253 complain, /*require_all_args=*/1);
4255 /* Don't process further if one of the levels fails. */
4256 if (a == error_mark_node)
4258 /* Restore the ARGLIST to its full size. */
4259 TREE_VEC_LENGTH (arglist) = saved_depth;
4260 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4263 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4265 /* We temporarily reduce the length of the ARGLIST so
4266 that coerce_template_parms will see only the arguments
4267 corresponding to the template parameters it is
4268 examining. */
4269 TREE_VEC_LENGTH (arglist)--;
4272 /* Restore the ARGLIST to its full size. */
4273 TREE_VEC_LENGTH (arglist) = saved_depth;
4275 arglist = bound_args;
4277 else
4278 arglist
4279 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4280 INNERMOST_TEMPLATE_ARGS (arglist),
4281 template,
4282 complain, /*require_all_args=*/1);
4284 if (arglist == error_mark_node
4285 || (!uses_template_parms (INNERMOST_TEMPLATE_ARGS (arglist))
4286 && check_instantiated_args (template,
4287 INNERMOST_TEMPLATE_ARGS (arglist),
4288 complain)))
4289 /* We were unable to bind the arguments. */
4290 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4292 /* In the scope of a template class, explicit references to the
4293 template class refer to the type of the template, not any
4294 instantiation of it. For example, in:
4296 template <class T> class C { void f(C<T>); }
4298 the `C<T>' is just the same as `C'. Outside of the
4299 class, however, such a reference is an instantiation. */
4300 if (comp_template_args (TYPE_TI_ARGS (template_type),
4301 arglist))
4303 found = template_type;
4305 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4307 tree ctx;
4309 /* Note that we use DECL_CONTEXT, rather than
4310 CP_DECL_CONTEXT, so that the termination test is
4311 always just `ctx'. We're not interested in namespace
4312 scopes. */
4313 for (ctx = current_class_type;
4314 ctx;
4315 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4316 if (same_type_p (ctx, template_type))
4317 break;
4319 if (!ctx)
4320 /* We're not in the scope of the class, so the
4321 TEMPLATE_TYPE is not the type we want after
4322 all. */
4323 found = NULL_TREE;
4326 if (found)
4327 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4329 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4330 *tp;
4331 tp = &TREE_CHAIN (*tp))
4332 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4334 found = *tp;
4336 /* Use the move-to-front heuristic to speed up future
4337 searches. */
4338 *tp = TREE_CHAIN (*tp);
4339 TREE_CHAIN (found)
4340 = DECL_TEMPLATE_INSTANTIATIONS (template);
4341 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4343 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
4346 /* This type is a "partial instantiation" if any of the template
4347 arguments still involve template parameters. Note that we set
4348 IS_PARTIAL_INSTANTIATION for partial specializations as
4349 well. */
4350 is_partial_instantiation = uses_template_parms (arglist);
4352 if (!is_partial_instantiation
4353 && !PRIMARY_TEMPLATE_P (template)
4354 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4356 found = xref_tag_from_type (TREE_TYPE (template),
4357 DECL_NAME (template),
4358 /*globalize=*/1);
4359 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4362 context = tsubst (DECL_CONTEXT (template), arglist,
4363 complain, in_decl);
4364 if (!context)
4365 context = global_namespace;
4367 /* Create the type. */
4368 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4370 if (!is_partial_instantiation)
4372 set_current_access_from_decl (TYPE_NAME (template_type));
4373 t = start_enum (TYPE_IDENTIFIER (template_type));
4375 else
4376 /* We don't want to call start_enum for this type, since
4377 the values for the enumeration constants may involve
4378 template parameters. And, no one should be interested
4379 in the enumeration constants for such a type. */
4380 t = make_node (ENUMERAL_TYPE);
4382 else
4384 t = make_aggr_type (TREE_CODE (template_type));
4385 CLASSTYPE_DECLARED_CLASS (t)
4386 = CLASSTYPE_DECLARED_CLASS (template_type);
4387 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4388 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4389 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4391 /* A local class. Make sure the decl gets registered properly. */
4392 if (context == current_function_decl)
4393 pushtag (DECL_NAME (template), t, 0);
4396 /* If we called start_enum or pushtag above, this information
4397 will already be set up. */
4398 if (!TYPE_NAME (t))
4400 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4402 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4403 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4404 TYPE_STUB_DECL (t) = type_decl;
4405 DECL_SOURCE_LOCATION (type_decl)
4406 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4408 else
4409 type_decl = TYPE_NAME (t);
4411 TREE_PRIVATE (type_decl)
4412 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4413 TREE_PROTECTED (type_decl)
4414 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4416 /* Set up the template information. We have to figure out which
4417 template is the immediate parent if this is a full
4418 instantiation. */
4419 if (parm_depth == 1 || is_partial_instantiation
4420 || !PRIMARY_TEMPLATE_P (template))
4421 /* This case is easy; there are no member templates involved. */
4422 found = template;
4423 else
4425 /* This is a full instantiation of a member template. Look
4426 for a partial instantiation of which this is an instance. */
4428 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4429 found; found = TREE_CHAIN (found))
4431 int success;
4432 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4434 /* We only want partial instantiations, here, not
4435 specializations or full instantiations. */
4436 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4437 || !uses_template_parms (TREE_VALUE (found)))
4438 continue;
4440 /* Temporarily reduce by one the number of levels in the
4441 ARGLIST and in FOUND so as to avoid comparing the
4442 last set of arguments. */
4443 TREE_VEC_LENGTH (arglist)--;
4444 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4446 /* See if the arguments match. If they do, then TMPL is
4447 the partial instantiation we want. */
4448 success = comp_template_args (TREE_PURPOSE (found), arglist);
4450 /* Restore the argument vectors to their full size. */
4451 TREE_VEC_LENGTH (arglist)++;
4452 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4454 if (success)
4456 found = tmpl;
4457 break;
4461 if (!found)
4463 /* There was no partial instantiation. This happens
4464 where C<T> is a member template of A<T> and it's used
4465 in something like
4467 template <typename T> struct B { A<T>::C<int> m; };
4468 B<float>;
4470 Create the partial instantiation.
4472 TREE_VEC_LENGTH (arglist)--;
4473 found = tsubst (template, arglist, complain, NULL_TREE);
4474 TREE_VEC_LENGTH (arglist)++;
4478 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4479 DECL_TEMPLATE_INSTANTIATIONS (template)
4480 = tree_cons (arglist, t,
4481 DECL_TEMPLATE_INSTANTIATIONS (template));
4483 if (TREE_CODE (t) == ENUMERAL_TYPE
4484 && !is_partial_instantiation)
4485 /* Now that the type has been registered on the instantiations
4486 list, we set up the enumerators. Because the enumeration
4487 constants may involve the enumeration type itself, we make
4488 sure to register the type first, and then create the
4489 constants. That way, doing tsubst_expr for the enumeration
4490 constants won't result in recursive calls here; we'll find
4491 the instantiation and exit above. */
4492 tsubst_enum (template_type, t, arglist);
4494 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4495 is set up. */
4496 if (TREE_CODE (t) != ENUMERAL_TYPE)
4497 DECL_NAME (type_decl) = classtype_mangled_name (t);
4498 if (!is_partial_instantiation)
4500 /* For backwards compatibility; code that uses
4501 -fexternal-templates expects looking up a template to
4502 instantiate it. I think DDD still relies on this.
4503 (jason 8/20/1998) */
4504 if (TREE_CODE (t) != ENUMERAL_TYPE
4505 && flag_external_templates
4506 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4507 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4508 add_pending_template (t);
4510 else
4511 /* If the type makes use of template parameters, the
4512 code that generates debugging information will crash. */
4513 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4515 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4517 timevar_pop (TV_NAME_LOOKUP);
4520 struct pair_fn_data
4522 tree_fn_t fn;
4523 void *data;
4524 htab_t visited;
4527 /* Called from for_each_template_parm via walk_tree. */
4529 static tree
4530 for_each_template_parm_r (tp, walk_subtrees, d)
4531 tree *tp;
4532 int *walk_subtrees;
4533 void *d;
4535 tree t = *tp;
4536 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4537 tree_fn_t fn = pfd->fn;
4538 void *data = pfd->data;
4539 void **slot;
4541 /* If we have already visited this tree, there's no need to walk
4542 subtrees. Otherwise, add it to the visited table. */
4543 slot = htab_find_slot (pfd->visited, *tp, INSERT);
4544 if (*slot)
4546 *walk_subtrees = 0;
4547 return NULL_TREE;
4549 *slot = *tp;
4551 if (TYPE_P (t)
4552 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4553 return error_mark_node;
4555 switch (TREE_CODE (t))
4557 case RECORD_TYPE:
4558 if (TYPE_PTRMEMFUNC_P (t))
4559 break;
4560 /* Fall through. */
4562 case UNION_TYPE:
4563 case ENUMERAL_TYPE:
4564 if (!TYPE_TEMPLATE_INFO (t))
4565 *walk_subtrees = 0;
4566 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4567 fn, data, pfd->visited))
4568 return error_mark_node;
4569 break;
4571 case METHOD_TYPE:
4572 /* Since we're not going to walk subtrees, we have to do this
4573 explicitly here. */
4574 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4575 pfd->visited))
4576 return error_mark_node;
4577 /* Fall through. */
4579 case FUNCTION_TYPE:
4580 /* Check the return type. */
4581 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4582 return error_mark_node;
4584 /* Check the parameter types. Since default arguments are not
4585 instantiated until they are needed, the TYPE_ARG_TYPES may
4586 contain expressions that involve template parameters. But,
4587 no-one should be looking at them yet. And, once they're
4588 instantiated, they don't contain template parameters, so
4589 there's no point in looking at them then, either. */
4591 tree parm;
4593 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4594 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4595 pfd->visited))
4596 return error_mark_node;
4598 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4599 want walk_tree walking into them itself. */
4600 *walk_subtrees = 0;
4602 break;
4604 case TYPEOF_TYPE:
4605 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
4606 pfd->visited))
4607 return error_mark_node;
4608 break;
4610 case FUNCTION_DECL:
4611 case VAR_DECL:
4612 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4613 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4614 pfd->visited))
4615 return error_mark_node;
4616 /* Fall through. */
4618 case PARM_DECL:
4619 case CONST_DECL:
4620 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4621 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4622 pfd->visited))
4623 return error_mark_node;
4624 if (DECL_CONTEXT (t)
4625 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4626 pfd->visited))
4627 return error_mark_node;
4628 break;
4630 case BOUND_TEMPLATE_TEMPLATE_PARM:
4631 /* Record template parameters such as `T' inside `TT<T>'. */
4632 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
4633 return error_mark_node;
4634 /* Fall through. */
4636 case TEMPLATE_TEMPLATE_PARM:
4637 case TEMPLATE_TYPE_PARM:
4638 case TEMPLATE_PARM_INDEX:
4639 if (fn && (*fn)(t, data))
4640 return error_mark_node;
4641 else if (!fn)
4642 return error_mark_node;
4643 break;
4645 case TEMPLATE_DECL:
4646 /* A template template parameter is encountered */
4647 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4648 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4649 return error_mark_node;
4651 /* Already substituted template template parameter */
4652 *walk_subtrees = 0;
4653 break;
4655 case TYPENAME_TYPE:
4656 if (!fn
4657 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4658 data, pfd->visited))
4659 return error_mark_node;
4660 break;
4662 case CONSTRUCTOR:
4663 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4664 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4665 (TREE_TYPE (t)), fn, data,
4666 pfd->visited))
4667 return error_mark_node;
4668 break;
4670 case INDIRECT_REF:
4671 case COMPONENT_REF:
4672 /* If there's no type, then this thing must be some expression
4673 involving template parameters. */
4674 if (!fn && !TREE_TYPE (t))
4675 return error_mark_node;
4676 break;
4678 case MODOP_EXPR:
4679 case CAST_EXPR:
4680 case REINTERPRET_CAST_EXPR:
4681 case CONST_CAST_EXPR:
4682 case STATIC_CAST_EXPR:
4683 case DYNAMIC_CAST_EXPR:
4684 case ARROW_EXPR:
4685 case DOTSTAR_EXPR:
4686 case TYPEID_EXPR:
4687 case LOOKUP_EXPR:
4688 case PSEUDO_DTOR_EXPR:
4689 if (!fn)
4690 return error_mark_node;
4691 break;
4693 case BASELINK:
4694 /* If we do not handle this case specially, we end up walking
4695 the BINFO hierarchy, which is circular, and therefore
4696 confuses walk_tree. */
4697 *walk_subtrees = 0;
4698 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
4699 pfd->visited))
4700 return error_mark_node;
4701 break;
4703 default:
4704 break;
4707 /* We didn't find any template parameters we liked. */
4708 return NULL_TREE;
4711 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4712 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4713 call FN with the parameter and the DATA.
4714 If FN returns nonzero, the iteration is terminated, and
4715 for_each_template_parm returns 1. Otherwise, the iteration
4716 continues. If FN never returns a nonzero value, the value
4717 returned by for_each_template_parm is 0. If FN is NULL, it is
4718 considered to be the function which always returns 1. */
4720 static int
4721 for_each_template_parm (t, fn, data, visited)
4722 tree t;
4723 tree_fn_t fn;
4724 void* data;
4725 htab_t visited;
4727 struct pair_fn_data pfd;
4728 int result;
4730 /* Set up. */
4731 pfd.fn = fn;
4732 pfd.data = data;
4734 /* Walk the tree. (Conceptually, we would like to walk without
4735 duplicates, but for_each_template_parm_r recursively calls
4736 for_each_template_parm, so we would need to reorganize a fair
4737 bit to use walk_tree_without_duplicates, so we keep our own
4738 visited list.) */
4739 if (visited)
4740 pfd.visited = visited;
4741 else
4742 pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer,
4743 NULL);
4744 result = walk_tree (&t,
4745 for_each_template_parm_r,
4746 &pfd,
4747 NULL) != NULL_TREE;
4749 /* Clean up. */
4750 if (!visited)
4751 htab_delete (pfd.visited);
4753 return result;
4757 uses_template_parms (t)
4758 tree t;
4760 return for_each_template_parm (t, 0, 0, NULL);
4763 static int tinst_depth;
4764 extern int max_tinst_depth;
4765 #ifdef GATHER_STATISTICS
4766 int depth_reached;
4767 #endif
4768 static int tinst_level_tick;
4769 static int last_template_error_tick;
4771 /* We're starting to instantiate D; record the template instantiation context
4772 for diagnostics and to restore it later. */
4775 push_tinst_level (d)
4776 tree d;
4778 tree new;
4780 if (tinst_depth >= max_tinst_depth)
4782 /* If the instantiation in question still has unbound template parms,
4783 we don't really care if we can't instantiate it, so just return.
4784 This happens with base instantiation for implicit `typename'. */
4785 if (uses_template_parms (d))
4786 return 0;
4788 last_template_error_tick = tinst_level_tick;
4789 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4790 max_tinst_depth, d);
4792 print_instantiation_context ();
4794 return 0;
4797 new = build_expr_wfl (d, input_filename, input_line, 0);
4798 TREE_CHAIN (new) = current_tinst_level;
4799 current_tinst_level = new;
4801 ++tinst_depth;
4802 #ifdef GATHER_STATISTICS
4803 if (tinst_depth > depth_reached)
4804 depth_reached = tinst_depth;
4805 #endif
4807 ++tinst_level_tick;
4808 return 1;
4811 /* We're done instantiating this template; return to the instantiation
4812 context. */
4814 void
4815 pop_tinst_level ()
4817 tree old = current_tinst_level;
4819 /* Restore the filename and line number stashed away when we started
4820 this instantiation. */
4821 input_line = TINST_LINE (old);
4822 input_filename = TINST_FILE (old);
4823 extract_interface_info ();
4825 current_tinst_level = TREE_CHAIN (old);
4826 --tinst_depth;
4827 ++tinst_level_tick;
4830 /* We're instantiating a deferred template; restore the template
4831 instantiation context in which the instantiation was requested, which
4832 is one step out from LEVEL. */
4834 static void
4835 reopen_tinst_level (level)
4836 tree level;
4838 tree t;
4840 tinst_depth = 0;
4841 for (t = level; t; t = TREE_CHAIN (t))
4842 ++tinst_depth;
4844 current_tinst_level = level;
4845 pop_tinst_level ();
4848 /* Return the outermost template instantiation context, for use with
4849 -falt-external-templates. */
4851 tree
4852 tinst_for_decl ()
4854 tree p = current_tinst_level;
4856 if (p)
4857 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4859 return p;
4862 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4863 vector of template arguments, as for tsubst.
4865 Returns an appropriate tsubst'd friend declaration. */
4867 static tree
4868 tsubst_friend_function (decl, args)
4869 tree decl;
4870 tree args;
4872 tree new_friend;
4873 location_t saved_loc = input_location;
4875 input_location = DECL_SOURCE_LOCATION (decl);
4877 if (TREE_CODE (decl) == FUNCTION_DECL
4878 && DECL_TEMPLATE_INSTANTIATION (decl)
4879 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4880 /* This was a friend declared with an explicit template
4881 argument list, e.g.:
4883 friend void f<>(T);
4885 to indicate that f was a template instantiation, not a new
4886 function declaration. Now, we have to figure out what
4887 instantiation of what template. */
4889 tree template_id, arglist, fns;
4890 tree new_args;
4891 tree tmpl;
4892 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
4894 /* Friend functions are looked up in the containing namespace scope.
4895 We must enter that scope, to avoid finding member functions of the
4896 current cless with same name. */
4897 push_nested_namespace (ns);
4898 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4899 tf_error | tf_warning, NULL_TREE);
4900 pop_nested_namespace (ns);
4901 arglist = tsubst (DECL_TI_ARGS (decl), args,
4902 tf_error | tf_warning, NULL_TREE);
4903 template_id = lookup_template_function (fns, arglist);
4905 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4906 tmpl = determine_specialization (template_id, new_friend,
4907 &new_args,
4908 /*need_member_template=*/0);
4909 new_friend = instantiate_template (tmpl, new_args, tf_error);
4910 goto done;
4913 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
4915 /* The NEW_FRIEND will look like an instantiation, to the
4916 compiler, but is not an instantiation from the point of view of
4917 the language. For example, we might have had:
4919 template <class T> struct S {
4920 template <class U> friend void f(T, U);
4923 Then, in S<int>, template <class U> void f(int, U) is not an
4924 instantiation of anything. */
4925 DECL_USE_TEMPLATE (new_friend) = 0;
4926 if (TREE_CODE (decl) == TEMPLATE_DECL)
4928 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4929 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4930 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4933 /* The mangled name for the NEW_FRIEND is incorrect. The function
4934 is not a template instantiation and should not be mangled like
4935 one. Therefore, we forget the mangling here; we'll recompute it
4936 later if we need it. */
4937 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4939 SET_DECL_RTL (new_friend, NULL_RTX);
4940 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4943 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4945 tree old_decl;
4946 tree new_friend_template_info;
4947 tree new_friend_result_template_info;
4948 tree ns;
4949 int new_friend_is_defn;
4951 /* We must save some information from NEW_FRIEND before calling
4952 duplicate decls since that function will free NEW_FRIEND if
4953 possible. */
4954 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4955 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4957 /* This declaration is a `primary' template. */
4958 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4960 new_friend_is_defn
4961 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4962 new_friend_result_template_info
4963 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4965 else
4967 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4968 new_friend_result_template_info = NULL_TREE;
4971 /* Inside pushdecl_namespace_level, we will push into the
4972 current namespace. However, the friend function should go
4973 into the namespace of the template. */
4974 ns = decl_namespace_context (new_friend);
4975 push_nested_namespace (ns);
4976 old_decl = pushdecl_namespace_level (new_friend);
4977 pop_nested_namespace (ns);
4979 if (old_decl != new_friend)
4981 /* This new friend declaration matched an existing
4982 declaration. For example, given:
4984 template <class T> void f(T);
4985 template <class U> class C {
4986 template <class T> friend void f(T) {}
4989 the friend declaration actually provides the definition
4990 of `f', once C has been instantiated for some type. So,
4991 old_decl will be the out-of-class template declaration,
4992 while new_friend is the in-class definition.
4994 But, if `f' was called before this point, the
4995 instantiation of `f' will have DECL_TI_ARGS corresponding
4996 to `T' but not to `U', references to which might appear
4997 in the definition of `f'. Previously, the most general
4998 template for an instantiation of `f' was the out-of-class
4999 version; now it is the in-class version. Therefore, we
5000 run through all specialization of `f', adding to their
5001 DECL_TI_ARGS appropriately. In particular, they need a
5002 new set of outer arguments, corresponding to the
5003 arguments for this class instantiation.
5005 The same situation can arise with something like this:
5007 friend void f(int);
5008 template <class T> class C {
5009 friend void f(T) {}
5012 when `C<int>' is instantiated. Now, `f(int)' is defined
5013 in the class. */
5015 if (!new_friend_is_defn)
5016 /* On the other hand, if the in-class declaration does
5017 *not* provide a definition, then we don't want to alter
5018 existing definitions. We can just leave everything
5019 alone. */
5021 else
5023 /* Overwrite whatever template info was there before, if
5024 any, with the new template information pertaining to
5025 the declaration. */
5026 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5028 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5029 /* duplicate_decls will take care of this case. */
5031 else
5033 tree t;
5034 tree new_friend_args;
5036 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5037 = new_friend_result_template_info;
5039 new_friend_args = TI_ARGS (new_friend_template_info);
5040 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5041 t != NULL_TREE;
5042 t = TREE_CHAIN (t))
5044 tree spec = TREE_VALUE (t);
5046 DECL_TI_ARGS (spec)
5047 = add_outermost_template_args (new_friend_args,
5048 DECL_TI_ARGS (spec));
5051 /* Now, since specializations are always supposed to
5052 hang off of the most general template, we must move
5053 them. */
5054 t = most_general_template (old_decl);
5055 if (t != old_decl)
5057 DECL_TEMPLATE_SPECIALIZATIONS (t)
5058 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5059 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5060 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5065 /* The information from NEW_FRIEND has been merged into OLD_DECL
5066 by duplicate_decls. */
5067 new_friend = old_decl;
5070 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
5072 /* Check to see that the declaration is really present, and,
5073 possibly obtain an improved declaration. */
5074 tree fn = check_classfn (DECL_CONTEXT (new_friend),
5075 new_friend);
5077 if (fn)
5078 new_friend = fn;
5081 done:
5082 input_location = saved_loc;
5083 return new_friend;
5086 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5087 template arguments, as for tsubst.
5089 Returns an appropriate tsubst'd friend type or error_mark_node on
5090 failure. */
5092 static tree
5093 tsubst_friend_class (friend_tmpl, args)
5094 tree friend_tmpl;
5095 tree args;
5097 tree friend_type;
5098 tree tmpl;
5099 tree context;
5101 context = DECL_CONTEXT (friend_tmpl);
5103 if (context)
5105 if (TREE_CODE (context) == NAMESPACE_DECL)
5106 push_nested_namespace (context);
5107 else
5108 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5111 /* First, we look for a class template. */
5112 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
5114 /* But, if we don't find one, it might be because we're in a
5115 situation like this:
5117 template <class T>
5118 struct S {
5119 template <class U>
5120 friend struct S;
5123 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5124 for `S<int>', not the TEMPLATE_DECL. */
5125 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5127 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
5128 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5131 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5133 /* The friend template has already been declared. Just
5134 check to see that the declarations match, and install any new
5135 default parameters. We must tsubst the default parameters,
5136 of course. We only need the innermost template parameters
5137 because that is all that redeclare_class_template will look
5138 at. */
5139 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5140 > TMPL_ARGS_DEPTH (args))
5142 tree parms;
5143 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5144 args, tf_error | tf_warning);
5145 redeclare_class_template (TREE_TYPE (tmpl), parms);
5148 friend_type = TREE_TYPE (tmpl);
5150 else
5152 /* The friend template has not already been declared. In this
5153 case, the instantiation of the template class will cause the
5154 injection of this template into the global scope. */
5155 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
5157 /* The new TMPL is not an instantiation of anything, so we
5158 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5159 the new type because that is supposed to be the corresponding
5160 template decl, i.e., TMPL. */
5161 DECL_USE_TEMPLATE (tmpl) = 0;
5162 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5163 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5165 /* Inject this template into the global scope. */
5166 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
5169 if (context)
5171 if (TREE_CODE (context) == NAMESPACE_DECL)
5172 pop_nested_namespace (context);
5173 else
5174 pop_nested_class ();
5177 return friend_type;
5180 /* Returns zero if TYPE cannot be completed later due to circularity.
5181 Otherwise returns one. */
5183 static int
5184 can_complete_type_without_circularity (type)
5185 tree type;
5187 if (type == NULL_TREE || type == error_mark_node)
5188 return 0;
5189 else if (COMPLETE_TYPE_P (type))
5190 return 1;
5191 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5192 return can_complete_type_without_circularity (TREE_TYPE (type));
5193 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5194 return 0;
5195 else
5196 return 1;
5199 tree
5200 instantiate_class_template (type)
5201 tree type;
5203 tree template, args, pattern, t, member;
5204 tree typedecl;
5205 tree pbinfo;
5207 if (type == error_mark_node)
5208 return error_mark_node;
5210 if (TYPE_BEING_DEFINED (type)
5211 || COMPLETE_TYPE_P (type)
5212 || dependent_type_p (type))
5213 return type;
5215 /* Figure out which template is being instantiated. */
5216 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5217 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
5219 /* Figure out which arguments are being used to do the
5220 instantiation. */
5221 args = CLASSTYPE_TI_ARGS (type);
5223 /* Determine what specialization of the original template to
5224 instantiate. */
5225 t = most_specialized_class (template, args);
5226 if (t == error_mark_node)
5228 const char *str = "candidates are:";
5229 error ("ambiguous class template instantiation for `%#T'", type);
5230 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5231 t = TREE_CHAIN (t))
5233 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5234 args))
5236 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5237 str = " ";
5240 TYPE_BEING_DEFINED (type) = 1;
5241 return error_mark_node;
5244 if (t)
5245 pattern = TREE_TYPE (t);
5246 else
5247 pattern = TREE_TYPE (template);
5249 /* If the template we're instantiating is incomplete, then clearly
5250 there's nothing we can do. */
5251 if (!COMPLETE_TYPE_P (pattern))
5252 return type;
5254 /* If we've recursively instantiated too many templates, stop. */
5255 if (! push_tinst_level (type))
5256 return type;
5258 /* Now we're really doing the instantiation. Mark the type as in
5259 the process of being defined. */
5260 TYPE_BEING_DEFINED (type) = 1;
5262 /* We may be in the middle of deferred access check. Disable
5263 it now. */
5264 push_deferring_access_checks (dk_no_deferred);
5266 maybe_push_to_top_level (uses_template_parms (type));
5268 if (t)
5270 /* This TYPE is actually an instantiation of a partial
5271 specialization. We replace the innermost set of ARGS with
5272 the arguments appropriate for substitution. For example,
5273 given:
5275 template <class T> struct S {};
5276 template <class T> struct S<T*> {};
5278 and supposing that we are instantiating S<int*>, ARGS will
5279 present be {int*} but we need {int}. */
5280 tree inner_args
5281 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5282 args);
5284 /* If there were multiple levels in ARGS, replacing the
5285 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5286 want, so we make a copy first. */
5287 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5289 args = copy_node (args);
5290 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5292 else
5293 args = inner_args;
5296 if (flag_external_templates)
5298 if (flag_alt_external_templates)
5300 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5301 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5303 else
5305 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5306 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5307 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5310 else
5312 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5315 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5316 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5317 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5318 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5319 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5320 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5321 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5322 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5323 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5324 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5325 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5326 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5327 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5328 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5329 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5330 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5331 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5332 TYPE_USES_MULTIPLE_INHERITANCE (type)
5333 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5334 TYPE_USES_VIRTUAL_BASECLASSES (type)
5335 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5336 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5337 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5338 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5339 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5340 if (ANON_AGGR_TYPE_P (pattern))
5341 SET_ANON_AGGR_TYPE_P (type);
5343 pbinfo = TYPE_BINFO (pattern);
5345 if (BINFO_BASETYPES (pbinfo))
5347 tree base_list = NULL_TREE;
5348 tree pbases = BINFO_BASETYPES (pbinfo);
5349 tree paccesses = BINFO_BASEACCESSES (pbinfo);
5350 int i;
5352 /* Substitute into each of the bases to determine the actual
5353 basetypes. */
5354 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5356 tree base;
5357 tree access;
5358 tree pbase;
5360 pbase = TREE_VEC_ELT (pbases, i);
5361 access = TREE_VEC_ELT (paccesses, i);
5363 /* Substitute to figure out the base class. */
5364 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
5365 if (base == error_mark_node)
5366 continue;
5368 base_list = tree_cons (access, base, base_list);
5369 TREE_VIA_VIRTUAL (base_list) = TREE_VIA_VIRTUAL (pbase);
5372 /* The list is now in reverse order; correct that. */
5373 base_list = nreverse (base_list);
5375 /* Now call xref_basetypes to set up all the base-class
5376 information. */
5377 xref_basetypes (type, base_list);
5380 /* Now that our base classes are set up, enter the scope of the
5381 class, so that name lookups into base classes, etc. will work
5382 correctly. This is precisely analogous to what we do in
5383 begin_class_definition when defining an ordinary non-template
5384 class. */
5385 pushclass (type, true);
5387 /* Now members are processed in the order of declaration. */
5388 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
5390 tree t = TREE_VALUE (member);
5392 if (TREE_PURPOSE (member))
5394 if (TYPE_P (t))
5396 /* Build new CLASSTYPE_TAGS. */
5398 tree tag = t;
5399 tree name = TYPE_IDENTIFIER (tag);
5400 tree newtag;
5402 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5403 my_friendly_assert (newtag != error_mark_node, 20010206);
5404 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5406 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5407 /* Unfortunately, lookup_template_class sets
5408 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5409 instantiation (i.e., for the type of a member template
5410 class nested within a template class.) This behavior is
5411 required for maybe_process_partial_specialization to work
5412 correctly, but is not accurate in this case; the TAG is not
5413 an instantiation of anything. (The corresponding
5414 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5415 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5417 /* Now, we call pushtag to put this NEWTAG into the scope of
5418 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5419 pushtag calling push_template_decl. We don't have to do
5420 this for enums because it will already have been done in
5421 tsubst_enum. */
5422 if (name)
5423 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5424 pushtag (name, newtag, /*globalize=*/0);
5427 else if (TREE_CODE (t) == FUNCTION_DECL
5428 || DECL_FUNCTION_TEMPLATE_P (t))
5430 /* Build new TYPE_METHODS. */
5432 tree r = tsubst (t, args, tf_error, NULL_TREE);
5433 set_current_access_from_decl (r);
5434 grok_special_member_properties (r);
5435 finish_member_declaration (r);
5437 else
5439 /* Build new TYPE_FIELDS. */
5441 if (TREE_CODE (t) != CONST_DECL)
5443 tree r;
5445 /* The the file and line for this declaration, to
5446 assist in error message reporting. Since we
5447 called push_tinst_level above, we don't need to
5448 restore these. */
5449 input_location = DECL_SOURCE_LOCATION (t);
5451 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5452 if (TREE_CODE (r) == VAR_DECL)
5454 tree init;
5456 if (DECL_INITIALIZED_IN_CLASS_P (r))
5457 init = tsubst_expr (DECL_INITIAL (t), args,
5458 tf_error | tf_warning, NULL_TREE);
5459 else
5460 init = NULL_TREE;
5462 finish_static_data_member_decl (r, init,
5463 /*asmspec_tree=*/NULL_TREE,
5464 /*flags=*/0);
5466 if (DECL_INITIALIZED_IN_CLASS_P (r))
5467 check_static_variable_definition (r, TREE_TYPE (r));
5469 else if (TREE_CODE (r) == FIELD_DECL)
5471 /* Determine whether R has a valid type and can be
5472 completed later. If R is invalid, then it is
5473 replaced by error_mark_node so that it will not be
5474 added to TYPE_FIELDS. */
5475 tree rtype = TREE_TYPE (r);
5476 if (can_complete_type_without_circularity (rtype))
5477 complete_type (rtype);
5479 if (!COMPLETE_TYPE_P (rtype))
5481 cxx_incomplete_type_error (r, rtype);
5482 r = error_mark_node;
5486 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5487 such a thing will already have been added to the field
5488 list by tsubst_enum in finish_member_declaration in the
5489 CLASSTYPE_TAGS case above. */
5490 if (!(TREE_CODE (r) == TYPE_DECL
5491 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5492 && TYPE_CONTEXT (TREE_TYPE (r)) == type))
5494 set_current_access_from_decl (r);
5495 finish_member_declaration (r);
5500 else
5502 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5504 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5506 tree friend_type = t;
5507 tree new_friend_type;
5509 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5510 new_friend_type = tsubst_friend_class (friend_type, args);
5511 else if (uses_template_parms (friend_type))
5512 new_friend_type = tsubst (friend_type, args,
5513 tf_error | tf_warning, NULL_TREE);
5514 else
5516 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5518 /* The call to xref_tag_from_type does injection for friend
5519 classes. */
5520 push_nested_namespace (ns);
5521 new_friend_type =
5522 xref_tag_from_type (friend_type, NULL_TREE, 1);
5523 pop_nested_namespace (ns);
5526 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5527 /* Trick make_friend_class into realizing that the friend
5528 we're adding is a template, not an ordinary class. It's
5529 important that we use make_friend_class since it will
5530 perform some error-checking and output cross-reference
5531 information. */
5532 ++processing_template_decl;
5534 if (new_friend_type != error_mark_node)
5535 make_friend_class (type, new_friend_type);
5537 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5538 --processing_template_decl;
5540 else
5541 /* Build new DECL_FRIENDLIST. */
5542 add_friend (type, tsubst_friend_function (t, args));
5546 /* Set the file and line number information to whatever is given for
5547 the class itself. This puts error messages involving generated
5548 implicit functions at a predictable point, and the same point
5549 that would be used for non-template classes. */
5550 typedecl = TYPE_MAIN_DECL (type);
5551 input_location = DECL_SOURCE_LOCATION (typedecl);
5553 unreverse_member_declarations (type);
5554 finish_struct_1 (type);
5555 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5557 /* Clear this now so repo_template_used is happy. */
5558 TYPE_BEING_DEFINED (type) = 0;
5559 repo_template_used (type);
5561 /* Now that the class is complete, instantiate default arguments for
5562 any member functions. We don't do this earlier because the
5563 default arguments may reference members of the class. */
5564 if (!PRIMARY_TEMPLATE_P (template))
5565 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5566 if (TREE_CODE (t) == FUNCTION_DECL
5567 /* Implicitly generated member functions will not have template
5568 information; they are not instantiations, but instead are
5569 created "fresh" for each instantiation. */
5570 && DECL_TEMPLATE_INFO (t))
5571 tsubst_default_arguments (t);
5573 popclass ();
5574 pop_from_top_level ();
5575 pop_deferring_access_checks ();
5576 pop_tinst_level ();
5578 if (TYPE_CONTAINS_VPTR_P (type))
5579 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5581 return type;
5584 static int
5585 list_eq (t1, t2)
5586 tree t1, t2;
5588 if (t1 == NULL_TREE)
5589 return t2 == NULL_TREE;
5590 if (t2 == NULL_TREE)
5591 return 0;
5592 /* Don't care if one declares its arg const and the other doesn't -- the
5593 main variant of the arg type is all that matters. */
5594 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5595 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5596 return 0;
5597 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5600 /* If arg is a non-type template parameter that does not depend on template
5601 arguments, fold it like we weren't in the body of a template. */
5603 static tree
5604 maybe_fold_nontype_arg (arg)
5605 tree arg;
5607 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5609 /* Sometimes, one of the args was an expression involving a
5610 template constant parameter, like N - 1. Now that we've
5611 tsubst'd, we might have something like 2 - 1. This will
5612 confuse lookup_template_class, so we do constant folding
5613 here. We have to unset processing_template_decl, to
5614 fool build_expr_from_tree() into building an actual
5615 tree. */
5617 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5618 as simple as it's going to get, and trying to reprocess
5619 the trees will break. */
5620 if (!TREE_TYPE (arg))
5622 int saved_processing_template_decl = processing_template_decl;
5623 processing_template_decl = 0;
5624 arg = build_expr_from_tree (arg);
5625 processing_template_decl = saved_processing_template_decl;
5628 arg = fold (arg);
5630 return arg;
5633 /* Apply maybe_fold_nontype_arg on a list or vector of args. */
5635 static void
5636 maybe_fold_nontype_args (tree targs)
5638 if (!targs)
5639 /*OK*/;
5640 else if (TREE_CODE (targs) == TREE_LIST)
5642 tree chain;
5643 for (chain = targs; chain; chain = TREE_CHAIN (chain))
5644 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
5646 else
5648 int i;
5649 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
5650 TREE_VEC_ELT (targs, i)
5651 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
5655 /* Substitute ARGS into the vector of template arguments T. */
5657 static tree
5658 tsubst_template_arg_vector (t, args, complain)
5659 tree t;
5660 tree args;
5661 tsubst_flags_t complain;
5663 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5664 tree *elts = (tree *) alloca (len * sizeof (tree));
5666 memset ((char *) elts, 0, len * sizeof (tree));
5668 for (i = 0; i < len; i++)
5670 if (TREE_VEC_ELT (t, i) != NULL_TREE
5671 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5672 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5673 args, complain);
5674 else
5675 elts[i] = maybe_fold_nontype_arg
5676 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5677 NULL_TREE));
5679 if (elts[i] == error_mark_node)
5680 return error_mark_node;
5682 if (elts[i] != TREE_VEC_ELT (t, i))
5683 need_new = 1;
5686 if (!need_new)
5687 return t;
5689 t = make_tree_vec (len);
5690 for (i = 0; i < len; i++)
5691 TREE_VEC_ELT (t, i) = elts[i];
5693 return t;
5696 /* Return the result of substituting ARGS into the template parameters
5697 given by PARMS. If there are m levels of ARGS and m + n levels of
5698 PARMS, then the result will contain n levels of PARMS. For
5699 example, if PARMS is `template <class T> template <class U>
5700 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5701 result will be `template <int*, double, class V>'. */
5703 static tree
5704 tsubst_template_parms (parms, args, complain)
5705 tree parms;
5706 tree args;
5707 tsubst_flags_t complain;
5709 tree r = NULL_TREE;
5710 tree* new_parms;
5712 for (new_parms = &r;
5713 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5714 new_parms = &(TREE_CHAIN (*new_parms)),
5715 parms = TREE_CHAIN (parms))
5717 tree new_vec =
5718 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5719 int i;
5721 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5723 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5724 tree default_value = TREE_PURPOSE (tuple);
5725 tree parm_decl = TREE_VALUE (tuple);
5727 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5728 default_value = tsubst_expr (default_value, args,
5729 complain, NULL_TREE);
5730 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5731 parm_decl);
5732 TREE_VEC_ELT (new_vec, i) = tuple;
5735 *new_parms =
5736 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5737 - TMPL_ARGS_DEPTH (args)),
5738 new_vec, NULL_TREE);
5741 return r;
5744 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5745 type T. If T is not an aggregate or enumeration type, it is
5746 handled as if by tsubst. IN_DECL is as for tsubst. If
5747 ENTERING_SCOPE is nonzero, T is the context for a template which
5748 we are presently tsubst'ing. Return the substituted value. */
5750 static tree
5751 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5752 tree t;
5753 tree args;
5754 tsubst_flags_t complain;
5755 tree in_decl;
5756 int entering_scope;
5758 if (t == NULL_TREE)
5759 return NULL_TREE;
5761 switch (TREE_CODE (t))
5763 case RECORD_TYPE:
5764 if (TYPE_PTRMEMFUNC_P (t))
5765 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5767 /* else fall through */
5768 case ENUMERAL_TYPE:
5769 case UNION_TYPE:
5770 if (TYPE_TEMPLATE_INFO (t))
5772 tree argvec;
5773 tree context;
5774 tree r;
5776 /* First, determine the context for the type we are looking
5777 up. */
5778 if (TYPE_CONTEXT (t) != NULL_TREE)
5779 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5780 complain,
5781 in_decl, /*entering_scope=*/1);
5782 else
5783 context = NULL_TREE;
5785 /* Then, figure out what arguments are appropriate for the
5786 type we are trying to find. For example, given:
5788 template <class T> struct S;
5789 template <class T, class U> void f(T, U) { S<U> su; }
5791 and supposing that we are instantiating f<int, double>,
5792 then our ARGS will be {int, double}, but, when looking up
5793 S we only want {double}. */
5794 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5795 complain);
5796 if (argvec == error_mark_node)
5797 return error_mark_node;
5799 r = lookup_template_class (t, argvec, in_decl, context,
5800 entering_scope, complain);
5802 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
5804 else
5805 /* This is not a template type, so there's nothing to do. */
5806 return t;
5808 default:
5809 return tsubst (t, args, complain, in_decl);
5813 /* Substitute into the default argument ARG (a default argument for
5814 FN), which has the indicated TYPE. */
5816 tree
5817 tsubst_default_argument (fn, type, arg)
5818 tree fn;
5819 tree type;
5820 tree arg;
5822 /* This default argument came from a template. Instantiate the
5823 default argument here, not in tsubst. In the case of
5824 something like:
5826 template <class T>
5827 struct S {
5828 static T t();
5829 void f(T = t());
5832 we must be careful to do name lookup in the scope of S<T>,
5833 rather than in the current class.
5835 ??? current_class_type affects a lot more than name lookup. This is
5836 very fragile. Fortunately, it will go away when we do 2-phase name
5837 binding properly. */
5839 /* FN is already the desired FUNCTION_DECL. */
5840 push_access_scope (fn);
5842 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5843 tf_error | tf_warning, NULL_TREE);
5845 pop_access_scope (fn);
5847 /* Make sure the default argument is reasonable. */
5848 arg = check_default_argument (type, arg);
5850 return arg;
5853 /* Substitute into all the default arguments for FN. */
5855 static void
5856 tsubst_default_arguments (fn)
5857 tree fn;
5859 tree arg;
5860 tree tmpl_args;
5862 tmpl_args = DECL_TI_ARGS (fn);
5864 /* If this function is not yet instantiated, we certainly don't need
5865 its default arguments. */
5866 if (uses_template_parms (tmpl_args))
5867 return;
5869 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5870 arg;
5871 arg = TREE_CHAIN (arg))
5872 if (TREE_PURPOSE (arg))
5873 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5874 TREE_VALUE (arg),
5875 TREE_PURPOSE (arg));
5878 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5879 (already computed) substitution of ARGS into TREE_TYPE (T), if
5880 appropriate. Return the result of the substitution. Issue error
5881 and warning messages under control of COMPLAIN. */
5883 static tree
5884 tsubst_decl (t, args, type, complain)
5885 tree t;
5886 tree args;
5887 tree type;
5888 tsubst_flags_t complain;
5890 location_t saved_loc;
5891 tree r = NULL_TREE;
5892 tree in_decl = t;
5894 /* Set the filename and linenumber to improve error-reporting. */
5895 saved_loc = input_location;
5896 input_location = DECL_SOURCE_LOCATION (t);
5898 switch (TREE_CODE (t))
5900 case TEMPLATE_DECL:
5902 /* We can get here when processing a member template function
5903 of a template class. */
5904 tree decl = DECL_TEMPLATE_RESULT (t);
5905 tree spec;
5906 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5908 if (!is_template_template_parm)
5910 /* We might already have an instance of this template.
5911 The ARGS are for the surrounding class type, so the
5912 full args contain the tsubst'd args for the context,
5913 plus the innermost args from the template decl. */
5914 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5915 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5916 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5917 tree full_args;
5919 full_args = tsubst_template_arg_vector (tmpl_args, args,
5920 complain);
5922 /* tsubst_template_arg_vector doesn't copy the vector if
5923 nothing changed. But, *something* should have
5924 changed. */
5925 my_friendly_assert (full_args != tmpl_args, 0);
5927 spec = retrieve_specialization (t, full_args);
5928 if (spec != NULL_TREE)
5930 r = spec;
5931 break;
5935 /* Make a new template decl. It will be similar to the
5936 original, but will record the current template arguments.
5937 We also create a new function declaration, which is just
5938 like the old one, but points to this new template, rather
5939 than the old one. */
5940 r = copy_decl (t);
5941 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5942 TREE_CHAIN (r) = NULL_TREE;
5944 if (is_template_template_parm)
5946 tree new_decl = tsubst (decl, args, complain, in_decl);
5947 DECL_TEMPLATE_RESULT (r) = new_decl;
5948 TREE_TYPE (r) = TREE_TYPE (new_decl);
5949 break;
5952 DECL_CONTEXT (r)
5953 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5954 complain, in_decl,
5955 /*entering_scope=*/1);
5956 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5958 if (TREE_CODE (decl) == TYPE_DECL)
5960 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
5961 TREE_TYPE (r) = new_type;
5962 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5963 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5964 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5966 else
5968 tree new_decl = tsubst (decl, args, complain, in_decl);
5970 DECL_TEMPLATE_RESULT (r) = new_decl;
5971 DECL_TI_TEMPLATE (new_decl) = r;
5972 TREE_TYPE (r) = TREE_TYPE (new_decl);
5973 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5976 SET_DECL_IMPLICIT_INSTANTIATION (r);
5977 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5978 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5980 /* The template parameters for this new template are all the
5981 template parameters for the old template, except the
5982 outermost level of parameters. */
5983 DECL_TEMPLATE_PARMS (r)
5984 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5985 complain);
5987 if (PRIMARY_TEMPLATE_P (t))
5988 DECL_PRIMARY_TEMPLATE (r) = r;
5990 /* We don't partially instantiate partial specializations. */
5991 if (TREE_CODE (decl) == TYPE_DECL)
5992 break;
5994 /* Record this partial instantiation. */
5995 register_specialization (r, t,
5996 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5999 break;
6001 case FUNCTION_DECL:
6003 tree ctx;
6004 tree argvec = NULL_TREE;
6005 tree *friends;
6006 tree gen_tmpl;
6007 int member;
6008 int args_depth;
6009 int parms_depth;
6011 /* Nobody should be tsubst'ing into non-template functions. */
6012 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
6014 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6016 tree spec;
6018 /* Calculate the most general template of which R is a
6019 specialization, and the complete set of arguments used to
6020 specialize R. */
6021 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6022 argvec
6023 = tsubst_template_arg_vector (DECL_TI_ARGS
6024 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6025 args, complain);
6027 /* Check to see if we already have this specialization. */
6028 spec = retrieve_specialization (gen_tmpl, argvec);
6030 if (spec)
6032 r = spec;
6033 break;
6036 /* We can see more levels of arguments than parameters if
6037 there was a specialization of a member template, like
6038 this:
6040 template <class T> struct S { template <class U> void f(); }
6041 template <> template <class U> void S<int>::f(U);
6043 Here, we'll be substituting into the specialization,
6044 because that's where we can find the code we actually
6045 want to generate, but we'll have enough arguments for
6046 the most general template.
6048 We also deal with the peculiar case:
6050 template <class T> struct S {
6051 template <class U> friend void f();
6053 template <class U> void f() {}
6054 template S<int>;
6055 template void f<double>();
6057 Here, the ARGS for the instantiation of will be {int,
6058 double}. But, we only need as many ARGS as there are
6059 levels of template parameters in CODE_PATTERN. We are
6060 careful not to get fooled into reducing the ARGS in
6061 situations like:
6063 template <class T> struct S { template <class U> void f(U); }
6064 template <class T> template <> void S<T>::f(int) {}
6066 which we can spot because the pattern will be a
6067 specialization in this case. */
6068 args_depth = TMPL_ARGS_DEPTH (args);
6069 parms_depth =
6070 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6071 if (args_depth > parms_depth
6072 && !DECL_TEMPLATE_SPECIALIZATION (t))
6073 args = get_innermost_template_args (args, parms_depth);
6075 else
6077 /* This special case arises when we have something like this:
6079 template <class T> struct S {
6080 friend void f<int>(int, double);
6083 Here, the DECL_TI_TEMPLATE for the friend declaration
6084 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
6085 being called from tsubst_friend_function, and we want
6086 only to create a new decl (R) with appropriate types so
6087 that we can call determine_specialization. */
6088 gen_tmpl = NULL_TREE;
6091 if (DECL_CLASS_SCOPE_P (t))
6093 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6094 member = 2;
6095 else
6096 member = 1;
6097 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6098 complain, t,
6099 /*entering_scope=*/1);
6101 else
6103 member = 0;
6104 ctx = DECL_CONTEXT (t);
6106 type = tsubst (type, args, complain, in_decl);
6107 if (type == error_mark_node)
6108 return error_mark_node;
6110 /* We do NOT check for matching decls pushed separately at this
6111 point, as they may not represent instantiations of this
6112 template, and in any case are considered separate under the
6113 discrete model. */
6114 r = copy_decl (t);
6115 DECL_USE_TEMPLATE (r) = 0;
6116 TREE_TYPE (r) = type;
6117 /* Clear out the mangled name and RTL for the instantiation. */
6118 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6119 SET_DECL_RTL (r, NULL_RTX);
6121 DECL_CONTEXT (r) = ctx;
6123 if (member && DECL_CONV_FN_P (r))
6124 /* Type-conversion operator. Reconstruct the name, in
6125 case it's the name of one of the template's parameters. */
6126 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6128 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6129 complain, t);
6130 DECL_RESULT (r) = NULL_TREE;
6132 TREE_STATIC (r) = 0;
6133 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6134 DECL_EXTERNAL (r) = 1;
6135 DECL_INTERFACE_KNOWN (r) = 0;
6136 DECL_DEFER_OUTPUT (r) = 0;
6137 TREE_CHAIN (r) = NULL_TREE;
6138 DECL_PENDING_INLINE_INFO (r) = 0;
6139 DECL_PENDING_INLINE_P (r) = 0;
6140 DECL_SAVED_TREE (r) = NULL_TREE;
6141 TREE_USED (r) = 0;
6142 if (DECL_CLONED_FUNCTION (r))
6144 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6145 args, complain, t);
6146 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6147 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6150 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6151 this in the special friend case mentioned above where
6152 GEN_TMPL is NULL. */
6153 if (gen_tmpl)
6155 DECL_TEMPLATE_INFO (r)
6156 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6157 SET_DECL_IMPLICIT_INSTANTIATION (r);
6158 register_specialization (r, gen_tmpl, argvec);
6160 /* We're not supposed to instantiate default arguments
6161 until they are called, for a template. But, for a
6162 declaration like:
6164 template <class T> void f ()
6165 { extern void g(int i = T()); }
6167 we should do the substitution when the template is
6168 instantiated. We handle the member function case in
6169 instantiate_class_template since the default arguments
6170 might refer to other members of the class. */
6171 if (!member
6172 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6173 && !uses_template_parms (argvec))
6174 tsubst_default_arguments (r);
6177 /* Copy the list of befriending classes. */
6178 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6179 *friends;
6180 friends = &TREE_CHAIN (*friends))
6182 *friends = copy_node (*friends);
6183 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6184 args, complain,
6185 in_decl);
6188 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6190 maybe_retrofit_in_chrg (r);
6191 if (DECL_CONSTRUCTOR_P (r))
6192 grok_ctor_properties (ctx, r);
6193 /* If this is an instantiation of a member template, clone it.
6194 If it isn't, that'll be handled by
6195 clone_constructors_and_destructors. */
6196 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6197 clone_function_decl (r, /*update_method_vec_p=*/0);
6199 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
6200 grok_op_properties (r, DECL_FRIEND_P (r));
6202 break;
6204 case PARM_DECL:
6206 r = copy_node (t);
6207 if (DECL_TEMPLATE_PARM_P (t))
6208 SET_DECL_TEMPLATE_PARM_P (r);
6210 TREE_TYPE (r) = type;
6211 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6213 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6214 DECL_INITIAL (r) = TREE_TYPE (r);
6215 else
6216 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6217 complain, in_decl);
6219 DECL_CONTEXT (r) = NULL_TREE;
6221 if (!DECL_TEMPLATE_PARM_P (r))
6222 DECL_ARG_TYPE (r) = type_passed_as (type);
6223 if (TREE_CHAIN (t))
6224 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6225 complain, TREE_CHAIN (t));
6227 break;
6229 case FIELD_DECL:
6231 r = copy_decl (t);
6232 TREE_TYPE (r) = type;
6233 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6235 /* We don't have to set DECL_CONTEXT here; it is set by
6236 finish_member_declaration. */
6237 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6238 complain, in_decl);
6239 TREE_CHAIN (r) = NULL_TREE;
6240 if (VOID_TYPE_P (type))
6241 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6243 break;
6245 case USING_DECL:
6247 r = copy_node (t);
6248 DECL_INITIAL (r)
6249 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
6250 TREE_CHAIN (r) = NULL_TREE;
6252 break;
6254 case TYPE_DECL:
6255 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6256 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6258 /* If this is the canonical decl, we don't have to mess with
6259 instantiations, and often we can't (for typename, template
6260 type parms and such). Note that TYPE_NAME is not correct for
6261 the above test if we've copied the type for a typedef. */
6262 r = TYPE_NAME (type);
6263 break;
6266 /* Fall through. */
6268 case VAR_DECL:
6270 tree argvec = NULL_TREE;
6271 tree gen_tmpl = NULL_TREE;
6272 tree spec;
6273 tree tmpl = NULL_TREE;
6274 tree ctx;
6275 int local_p;
6277 /* Assume this is a non-local variable. */
6278 local_p = 0;
6280 if (TYPE_P (CP_DECL_CONTEXT (t)))
6281 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6282 complain,
6283 in_decl, /*entering_scope=*/1);
6284 else if (DECL_NAMESPACE_SCOPE_P (t))
6285 ctx = DECL_CONTEXT (t);
6286 else
6288 /* Subsequent calls to pushdecl will fill this in. */
6289 ctx = NULL_TREE;
6290 local_p = 1;
6293 /* Check to see if we already have this specialization. */
6294 if (!local_p)
6296 tmpl = DECL_TI_TEMPLATE (t);
6297 gen_tmpl = most_general_template (tmpl);
6298 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6299 spec = retrieve_specialization (gen_tmpl, argvec);
6301 else
6302 spec = retrieve_local_specialization (t);
6304 if (spec)
6306 r = spec;
6307 break;
6310 r = copy_decl (t);
6311 if (TREE_CODE (r) == VAR_DECL)
6312 type = complete_type (type);
6313 TREE_TYPE (r) = type;
6314 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6315 DECL_CONTEXT (r) = ctx;
6316 /* Clear out the mangled name and RTL for the instantiation. */
6317 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6318 SET_DECL_RTL (r, NULL_RTX);
6320 /* Don't try to expand the initializer until someone tries to use
6321 this variable; otherwise we run into circular dependencies. */
6322 DECL_INITIAL (r) = NULL_TREE;
6323 SET_DECL_RTL (r, NULL_RTX);
6324 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6326 /* Even if the original location is out of scope, the newly
6327 substituted one is not. */
6328 if (TREE_CODE (r) == VAR_DECL)
6330 DECL_DEAD_FOR_LOCAL (r) = 0;
6331 DECL_INITIALIZED_P (r) = 0;
6334 if (!local_p)
6336 /* A static data member declaration is always marked
6337 external when it is declared in-class, even if an
6338 initializer is present. We mimic the non-template
6339 processing here. */
6340 DECL_EXTERNAL (r) = 1;
6342 register_specialization (r, gen_tmpl, argvec);
6343 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6344 SET_DECL_IMPLICIT_INSTANTIATION (r);
6346 else
6347 register_local_specialization (r, t);
6349 TREE_CHAIN (r) = NULL_TREE;
6350 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6351 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6352 /* Compute the size, alignment, etc. of R. */
6353 layout_decl (r, 0);
6355 break;
6357 default:
6358 abort ();
6361 /* Restore the file and line information. */
6362 input_location = saved_loc;
6364 return r;
6367 /* Substitue into the ARG_TYPES of a function type. */
6369 static tree
6370 tsubst_arg_types (arg_types, args, complain, in_decl)
6371 tree arg_types;
6372 tree args;
6373 tsubst_flags_t complain;
6374 tree in_decl;
6376 tree remaining_arg_types;
6377 tree type;
6379 if (!arg_types || arg_types == void_list_node)
6380 return arg_types;
6382 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6383 args, complain, in_decl);
6384 if (remaining_arg_types == error_mark_node)
6385 return error_mark_node;
6387 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6388 if (type == error_mark_node)
6389 return error_mark_node;
6390 if (VOID_TYPE_P (type))
6392 if (complain & tf_error)
6394 error ("invalid parameter type `%T'", type);
6395 if (in_decl)
6396 cp_error_at ("in declaration `%D'", in_decl);
6398 return error_mark_node;
6401 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6402 top-level qualifiers as required. */
6403 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6405 /* Note that we do not substitute into default arguments here. The
6406 standard mandates that they be instantiated only when needed,
6407 which is done in build_over_call. */
6408 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6409 remaining_arg_types);
6413 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6414 *not* handle the exception-specification for FNTYPE, because the
6415 initial substitution of explicitly provided template parameters
6416 during argument deduction forbids substitution into the
6417 exception-specification:
6419 [temp.deduct]
6421 All references in the function type of the function template to the
6422 corresponding template parameters are replaced by the specified tem-
6423 plate argument values. If a substitution in a template parameter or
6424 in the function type of the function template results in an invalid
6425 type, type deduction fails. [Note: The equivalent substitution in
6426 exception specifications is done only when the function is instanti-
6427 ated, at which point a program is ill-formed if the substitution
6428 results in an invalid type.] */
6430 static tree
6431 tsubst_function_type (t, args, complain, in_decl)
6432 tree t;
6433 tree args;
6434 tsubst_flags_t complain;
6435 tree in_decl;
6437 tree return_type;
6438 tree arg_types;
6439 tree fntype;
6441 /* The TYPE_CONTEXT is not used for function/method types. */
6442 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6444 /* Substitute the return type. */
6445 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6446 if (return_type == error_mark_node)
6447 return error_mark_node;
6449 /* Substitue the argument types. */
6450 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6451 complain, in_decl);
6452 if (arg_types == error_mark_node)
6453 return error_mark_node;
6455 /* Construct a new type node and return it. */
6456 if (TREE_CODE (t) == FUNCTION_TYPE)
6457 fntype = build_function_type (return_type, arg_types);
6458 else
6460 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6461 if (! IS_AGGR_TYPE (r))
6463 /* [temp.deduct]
6465 Type deduction may fail for any of the following
6466 reasons:
6468 -- Attempting to create "pointer to member of T" when T
6469 is not a class type. */
6470 if (complain & tf_error)
6471 error ("creating pointer to member function of non-class type `%T'",
6473 return error_mark_node;
6476 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6477 (arg_types));
6479 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
6480 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6482 return fntype;
6485 /* Substitute into the PARMS of a call-declarator. */
6487 static tree
6488 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6489 tree parms;
6490 tree args;
6491 tsubst_flags_t complain;
6492 tree in_decl;
6494 tree new_parms;
6495 tree type;
6496 tree defarg;
6498 if (!parms || parms == void_list_node)
6499 return parms;
6501 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6502 args, complain, in_decl);
6504 /* Figure out the type of this parameter. */
6505 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6507 /* Figure out the default argument as well. Note that we use
6508 tsubst_expr since the default argument is really an expression. */
6509 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6511 /* Chain this parameter on to the front of those we have already
6512 processed. We don't use hash_tree_cons because that function
6513 doesn't check TREE_PARMLIST. */
6514 new_parms = tree_cons (defarg, type, new_parms);
6516 /* And note that these are parameters. */
6517 TREE_PARMLIST (new_parms) = 1;
6519 return new_parms;
6522 /* Take the tree structure T and replace template parameters used
6523 therein with the argument vector ARGS. IN_DECL is an associated
6524 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6525 Issue error and warning messages under control of COMPLAIN. Note
6526 that we must be relatively non-tolerant of extensions here, in
6527 order to preserve conformance; if we allow substitutions that
6528 should not be allowed, we may allow argument deductions that should
6529 not succeed, and therefore report ambiguous overload situations
6530 where there are none. In theory, we could allow the substitution,
6531 but indicate that it should have failed, and allow our caller to
6532 make sure that the right thing happens, but we don't try to do this
6533 yet.
6535 This function is used for dealing with types, decls and the like;
6536 for expressions, use tsubst_expr or tsubst_copy. */
6538 static tree
6539 tsubst (t, args, complain, in_decl)
6540 tree t, args;
6541 tsubst_flags_t complain;
6542 tree in_decl;
6544 tree type, r;
6546 if (t == NULL_TREE || t == error_mark_node
6547 || t == integer_type_node
6548 || t == void_type_node
6549 || t == char_type_node
6550 || TREE_CODE (t) == NAMESPACE_DECL)
6551 return t;
6553 if (TREE_CODE (t) == IDENTIFIER_NODE)
6554 type = IDENTIFIER_TYPE_VALUE (t);
6555 else
6556 type = TREE_TYPE (t);
6557 if (type == unknown_type_node)
6558 abort ();
6560 if (type && TREE_CODE (t) != FUNCTION_DECL
6561 && TREE_CODE (t) != TYPENAME_TYPE
6562 && TREE_CODE (t) != TEMPLATE_DECL
6563 && TREE_CODE (t) != IDENTIFIER_NODE
6564 && TREE_CODE (t) != FUNCTION_TYPE
6565 && TREE_CODE (t) != METHOD_TYPE)
6566 type = tsubst (type, args, complain, in_decl);
6567 if (type == error_mark_node)
6568 return error_mark_node;
6570 if (DECL_P (t))
6571 return tsubst_decl (t, args, type, complain);
6573 switch (TREE_CODE (t))
6575 case RECORD_TYPE:
6576 case UNION_TYPE:
6577 case ENUMERAL_TYPE:
6578 return tsubst_aggr_type (t, args, complain, in_decl,
6579 /*entering_scope=*/0);
6581 case ERROR_MARK:
6582 case IDENTIFIER_NODE:
6583 case VOID_TYPE:
6584 case REAL_TYPE:
6585 case COMPLEX_TYPE:
6586 case VECTOR_TYPE:
6587 case BOOLEAN_TYPE:
6588 case INTEGER_CST:
6589 case REAL_CST:
6590 case STRING_CST:
6591 return t;
6593 case INTEGER_TYPE:
6594 if (t == integer_type_node)
6595 return t;
6597 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6598 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6599 return t;
6602 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6604 max = tsubst_expr (omax, args, complain, in_decl);
6605 if (max == error_mark_node)
6606 return error_mark_node;
6608 /* See if we can reduce this expression to something simpler. */
6609 max = maybe_fold_nontype_arg (max);
6610 if (!processing_template_decl)
6611 max = decl_constant_value (max);
6613 if (processing_template_decl
6614 /* When providing explicit arguments to a template
6615 function, but leaving some arguments for subsequent
6616 deduction, MAX may be template-dependent even if we're
6617 not PROCESSING_TEMPLATE_DECL. We still need to check for
6618 template parms, though; MAX won't be an INTEGER_CST for
6619 dynamic arrays, either. */
6620 || (TREE_CODE (max) != INTEGER_CST
6621 && uses_template_parms (max)))
6623 tree itype = make_node (INTEGER_TYPE);
6624 TYPE_MIN_VALUE (itype) = size_zero_node;
6625 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6626 integer_one_node);
6627 return itype;
6630 if (integer_zerop (omax))
6632 /* Still allow an explicit array of size zero. */
6633 if (pedantic)
6634 pedwarn ("creating array with size zero");
6636 else if (integer_zerop (max)
6637 || (TREE_CODE (max) == INTEGER_CST
6638 && INT_CST_LT (max, integer_zero_node)))
6640 /* [temp.deduct]
6642 Type deduction may fail for any of the following
6643 reasons:
6645 Attempting to create an array with a size that is
6646 zero or negative. */
6647 if (complain & tf_error)
6648 error ("creating array with size zero (`%E')", max);
6650 return error_mark_node;
6653 return compute_array_index_type (NULL_TREE, max);
6656 case TEMPLATE_TYPE_PARM:
6657 case TEMPLATE_TEMPLATE_PARM:
6658 case BOUND_TEMPLATE_TEMPLATE_PARM:
6659 case TEMPLATE_PARM_INDEX:
6661 int idx;
6662 int level;
6663 int levels;
6665 r = NULL_TREE;
6667 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6668 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6669 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6671 idx = TEMPLATE_TYPE_IDX (t);
6672 level = TEMPLATE_TYPE_LEVEL (t);
6674 else
6676 idx = TEMPLATE_PARM_IDX (t);
6677 level = TEMPLATE_PARM_LEVEL (t);
6680 if (TREE_VEC_LENGTH (args) > 0)
6682 tree arg = NULL_TREE;
6684 levels = TMPL_ARGS_DEPTH (args);
6685 if (level <= levels)
6686 arg = TMPL_ARG (args, level, idx);
6688 if (arg == error_mark_node)
6689 return error_mark_node;
6690 else if (arg != NULL_TREE)
6692 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6694 my_friendly_assert (TYPE_P (arg), 0);
6695 return cp_build_qualified_type_real
6696 (arg, cp_type_quals (arg) | cp_type_quals (t),
6697 complain | tf_ignore_bad_quals);
6699 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6701 /* We are processing a type constructed from
6702 a template template parameter */
6703 tree argvec = tsubst (TYPE_TI_ARGS (t),
6704 args, complain, in_decl);
6705 if (argvec == error_mark_node)
6706 return error_mark_node;
6708 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6709 we are resolving nested-types in the signature of
6710 a member function templates.
6711 Otherwise ARG is a TEMPLATE_DECL and is the real
6712 template to be instantiated. */
6713 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6714 arg = TYPE_NAME (arg);
6716 r = lookup_template_class (arg,
6717 argvec, in_decl,
6718 DECL_CONTEXT (arg),
6719 /*entering_scope=*/0,
6720 complain);
6721 return cp_build_qualified_type_real
6722 (r, TYPE_QUALS (t), complain);
6724 else
6725 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6726 return arg;
6729 else
6730 abort ();
6732 if (level == 1)
6733 /* This can happen during the attempted tsubst'ing in
6734 unify. This means that we don't yet have any information
6735 about the template parameter in question. */
6736 return t;
6738 /* If we get here, we must have been looking at a parm for a
6739 more deeply nested template. Make a new version of this
6740 template parameter, but with a lower level. */
6741 switch (TREE_CODE (t))
6743 case TEMPLATE_TYPE_PARM:
6744 case TEMPLATE_TEMPLATE_PARM:
6745 case BOUND_TEMPLATE_TEMPLATE_PARM:
6746 if (cp_type_quals (t))
6748 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6749 r = cp_build_qualified_type_real
6750 (r, cp_type_quals (t),
6751 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6752 ? tf_ignore_bad_quals : 0));
6754 else
6756 r = copy_type (t);
6757 TEMPLATE_TYPE_PARM_INDEX (r)
6758 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6759 r, levels);
6760 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6761 TYPE_MAIN_VARIANT (r) = r;
6762 TYPE_POINTER_TO (r) = NULL_TREE;
6763 TYPE_REFERENCE_TO (r) = NULL_TREE;
6765 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6767 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6768 complain, in_decl);
6769 if (argvec == error_mark_node)
6770 return error_mark_node;
6772 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6773 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6776 break;
6778 case TEMPLATE_PARM_INDEX:
6779 r = reduce_template_parm_level (t, type, levels);
6780 break;
6782 default:
6783 abort ();
6786 return r;
6789 case TREE_LIST:
6791 tree purpose, value, chain, result;
6793 if (t == void_list_node)
6794 return t;
6796 purpose = TREE_PURPOSE (t);
6797 if (purpose)
6799 purpose = tsubst (purpose, args, complain, in_decl);
6800 if (purpose == error_mark_node)
6801 return error_mark_node;
6803 value = TREE_VALUE (t);
6804 if (value)
6806 value = tsubst (value, args, complain, in_decl);
6807 if (value == error_mark_node)
6808 return error_mark_node;
6810 chain = TREE_CHAIN (t);
6811 if (chain && chain != void_type_node)
6813 chain = tsubst (chain, args, complain, in_decl);
6814 if (chain == error_mark_node)
6815 return error_mark_node;
6817 if (purpose == TREE_PURPOSE (t)
6818 && value == TREE_VALUE (t)
6819 && chain == TREE_CHAIN (t))
6820 return t;
6821 if (TREE_PARMLIST (t))
6823 result = tree_cons (purpose, value, chain);
6824 TREE_PARMLIST (result) = 1;
6826 else
6827 result = hash_tree_cons (purpose, value, chain);
6828 return result;
6830 case TREE_VEC:
6831 if (type != NULL_TREE)
6833 /* A binfo node. We always need to make a copy, of the node
6834 itself and of its BINFO_BASETYPES. */
6836 t = copy_node (t);
6838 /* Make sure type isn't a typedef copy. */
6839 type = BINFO_TYPE (TYPE_BINFO (type));
6841 TREE_TYPE (t) = complete_type (type);
6842 if (IS_AGGR_TYPE (type))
6844 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6845 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6846 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6847 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6849 return t;
6852 /* Otherwise, a vector of template arguments. */
6853 return tsubst_template_arg_vector (t, args, complain);
6855 case POINTER_TYPE:
6856 case REFERENCE_TYPE:
6858 enum tree_code code;
6860 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6861 return t;
6863 code = TREE_CODE (t);
6866 /* [temp.deduct]
6868 Type deduction may fail for any of the following
6869 reasons:
6871 -- Attempting to create a pointer to reference type.
6872 -- Attempting to create a reference to a reference type or
6873 a reference to void. */
6874 if (TREE_CODE (type) == REFERENCE_TYPE
6875 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6877 static location_t last_loc;
6879 /* We keep track of the last time we issued this error
6880 message to avoid spewing a ton of messages during a
6881 single bad template instantiation. */
6882 if (complain & tf_error
6883 && (last_loc.line != input_line
6884 || last_loc.file != input_filename))
6886 if (TREE_CODE (type) == VOID_TYPE)
6887 error ("forming reference to void");
6888 else
6889 error ("forming %s to reference type `%T'",
6890 (code == POINTER_TYPE) ? "pointer" : "reference",
6891 type);
6892 last_loc = input_location;
6895 return error_mark_node;
6897 else if (code == POINTER_TYPE)
6899 r = build_pointer_type (type);
6900 if (TREE_CODE (type) == METHOD_TYPE)
6901 r = build_ptrmemfunc_type (r);
6903 else
6904 r = build_reference_type (type);
6905 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6907 if (r != error_mark_node)
6908 /* Will this ever be needed for TYPE_..._TO values? */
6909 layout_type (r);
6911 return r;
6913 case OFFSET_TYPE:
6915 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6916 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6918 /* [temp.deduct]
6920 Type deduction may fail for any of the following
6921 reasons:
6923 -- Attempting to create "pointer to member of T" when T
6924 is not a class type. */
6925 if (complain & tf_error)
6926 error ("creating pointer to member of non-class type `%T'", r);
6927 return error_mark_node;
6929 if (TREE_CODE (type) == REFERENCE_TYPE)
6931 if (complain & tf_error)
6932 error ("creating pointer to member reference type `%T'", type);
6934 return error_mark_node;
6936 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6937 if (TREE_CODE (type) == FUNCTION_TYPE)
6938 /* This is really a method type. The cv qualifiers of the
6939 this pointer should _not_ be determined by the cv
6940 qualifiers of the class type. They should be held
6941 somewhere in the FUNCTION_TYPE, but we don't do that at
6942 the moment. Consider
6943 typedef void (Func) () const;
6945 template <typename T1> void Foo (Func T1::*);
6948 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6949 TREE_TYPE (type),
6950 TYPE_ARG_TYPES (type));
6951 else
6952 return build_offset_type (r, type);
6954 case FUNCTION_TYPE:
6955 case METHOD_TYPE:
6957 tree fntype;
6958 tree raises;
6960 fntype = tsubst_function_type (t, args, complain, in_decl);
6961 if (fntype == error_mark_node)
6962 return error_mark_node;
6964 /* Substitue the exception specification. */
6965 raises = TYPE_RAISES_EXCEPTIONS (t);
6966 if (raises)
6968 tree list = NULL_TREE;
6970 if (! TREE_VALUE (raises))
6971 list = raises;
6972 else
6973 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6975 tree spec = TREE_VALUE (raises);
6977 spec = tsubst (spec, args, complain, in_decl);
6978 if (spec == error_mark_node)
6979 return spec;
6980 list = add_exception_specifier (list, spec, complain);
6982 fntype = build_exception_variant (fntype, list);
6984 return fntype;
6986 case ARRAY_TYPE:
6988 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6989 if (domain == error_mark_node)
6990 return error_mark_node;
6992 /* As an optimization, we avoid regenerating the array type if
6993 it will obviously be the same as T. */
6994 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6995 return t;
6997 /* These checks should match the ones in grokdeclarator.
6999 [temp.deduct]
7001 The deduction may fail for any of the following reasons:
7003 -- Attempting to create an array with an element type that
7004 is void, a function type, or a reference type. */
7005 if (TREE_CODE (type) == VOID_TYPE
7006 || TREE_CODE (type) == FUNCTION_TYPE
7007 || TREE_CODE (type) == REFERENCE_TYPE)
7009 if (complain & tf_error)
7010 error ("creating array of `%T'", type);
7011 return error_mark_node;
7014 r = build_cplus_array_type (type, domain);
7015 return r;
7018 case PLUS_EXPR:
7019 case MINUS_EXPR:
7021 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7022 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7024 if (e1 == error_mark_node || e2 == error_mark_node)
7025 return error_mark_node;
7027 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
7030 case NEGATE_EXPR:
7031 case NOP_EXPR:
7033 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7034 if (e == error_mark_node)
7035 return error_mark_node;
7037 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
7040 case TYPENAME_TYPE:
7042 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7043 in_decl, /*entering_scope=*/1);
7044 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7045 complain, in_decl);
7047 if (ctx == error_mark_node || f == error_mark_node)
7048 return error_mark_node;
7050 if (!IS_AGGR_TYPE (ctx))
7052 if (complain & tf_error)
7053 error ("`%T' is not a class, struct, or union type",
7054 ctx);
7055 return error_mark_node;
7057 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7059 /* Normally, make_typename_type does not require that the CTX
7060 have complete type in order to allow things like:
7062 template <class T> struct S { typename S<T>::X Y; };
7064 But, such constructs have already been resolved by this
7065 point, so here CTX really should have complete type, unless
7066 it's a partial instantiation. */
7067 ctx = complete_type (ctx);
7068 if (!COMPLETE_TYPE_P (ctx))
7070 if (complain & tf_error)
7071 cxx_incomplete_type_error (NULL_TREE, ctx);
7072 return error_mark_node;
7076 f = make_typename_type (ctx, f,
7077 (complain & tf_error) | tf_keep_type_decl);
7078 if (f == error_mark_node)
7079 return f;
7080 if (TREE_CODE (f) == TYPE_DECL)
7082 complain |= tf_ignore_bad_quals;
7083 f = TREE_TYPE (f);
7086 return cp_build_qualified_type_real
7087 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7090 case UNBOUND_CLASS_TEMPLATE:
7092 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7093 in_decl, /*entering_scope=*/1);
7094 tree name = TYPE_IDENTIFIER (t);
7096 if (ctx == error_mark_node || name == error_mark_node)
7097 return error_mark_node;
7099 return make_unbound_class_template (ctx, name, complain);
7102 case INDIRECT_REF:
7104 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7105 if (e == error_mark_node)
7106 return error_mark_node;
7107 return make_pointer_declarator (type, e);
7110 case ADDR_EXPR:
7112 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7113 if (e == error_mark_node)
7114 return error_mark_node;
7115 return make_reference_declarator (type, e);
7118 case ARRAY_REF:
7120 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7121 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
7122 if (e1 == error_mark_node || e2 == error_mark_node)
7123 return error_mark_node;
7125 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
7128 case CALL_EXPR:
7130 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7131 tree e2 = (tsubst_call_declarator_parms
7132 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
7133 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
7134 complain, in_decl);
7136 if (e1 == error_mark_node || e2 == error_mark_node
7137 || e3 == error_mark_node)
7138 return error_mark_node;
7140 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
7143 case SCOPE_REF:
7145 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7146 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7147 if (e1 == error_mark_node || e2 == error_mark_node)
7148 return error_mark_node;
7150 return build_nt (TREE_CODE (t), e1, e2);
7153 case TYPEOF_TYPE:
7155 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
7156 if (e1 == error_mark_node)
7157 return error_mark_node;
7159 return TREE_TYPE (e1);
7162 default:
7163 sorry ("use of `%s' in template",
7164 tree_code_name [(int) TREE_CODE (t)]);
7165 return error_mark_node;
7169 /* Like tsubst, but deals with expressions. This function just replaces
7170 template parms; to finish processing the resultant expression, use
7171 tsubst_expr. */
7173 static tree
7174 tsubst_copy (t, args, complain, in_decl)
7175 tree t, args;
7176 tsubst_flags_t complain;
7177 tree in_decl;
7179 enum tree_code code;
7180 tree r;
7182 if (t == NULL_TREE || t == error_mark_node)
7183 return t;
7185 code = TREE_CODE (t);
7187 switch (code)
7189 case PARM_DECL:
7190 r = retrieve_local_specialization (t);
7191 my_friendly_assert (r != NULL, 20020903);
7192 return r;
7194 case CONST_DECL:
7196 tree enum_type;
7197 tree v;
7199 if (DECL_TEMPLATE_PARM_P (t))
7200 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7201 if (!DECL_CONTEXT (t))
7202 /* This is a global enumeration constant. */
7203 return t;
7205 /* Unfortunately, we cannot just call lookup_name here.
7206 Consider:
7208 template <int I> int f() {
7209 enum E { a = I };
7210 struct S { void g() { E e = a; } };
7213 When we instantiate f<7>::S::g(), say, lookup_name is not
7214 clever enough to find f<7>::a. */
7215 enum_type
7216 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
7217 /*entering_scope=*/0);
7219 for (v = TYPE_VALUES (enum_type);
7220 v != NULL_TREE;
7221 v = TREE_CHAIN (v))
7222 if (TREE_PURPOSE (v) == DECL_NAME (t))
7223 return TREE_VALUE (v);
7225 /* We didn't find the name. That should never happen; if
7226 name-lookup found it during preliminary parsing, we
7227 should find it again here during instantiation. */
7228 abort ();
7230 return t;
7232 case FIELD_DECL:
7233 if (DECL_CONTEXT (t))
7235 tree ctx;
7237 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
7238 /*entering_scope=*/1);
7239 if (ctx != DECL_CONTEXT (t))
7240 return lookup_field (ctx, DECL_NAME (t), 0, false);
7242 return t;
7244 case VAR_DECL:
7245 case FUNCTION_DECL:
7246 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7247 || local_variable_p (t))
7248 t = tsubst (t, args, complain, in_decl);
7249 mark_used (t);
7250 return t;
7252 case BASELINK:
7254 tree name;
7255 tree qualifying_scope;
7256 tree fns;
7257 tree template_args = 0;
7258 bool template_id_p = false;
7260 /* A baselink indicates a function from a base class. The
7261 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7262 non-dependent types; otherwise, the lookup could not have
7263 succeeded. However, they may indicate bases of the template
7264 class, rather than the instantiated class.
7266 In addition, lookups that were not ambiguous before may be
7267 ambiguous now. Therefore, we perform the lookup again. */
7268 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t));
7269 fns = BASELINK_FUNCTIONS (t);
7270 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7272 template_id_p = true;
7273 template_args = TREE_OPERAND (fns, 1);
7274 fns = TREE_OPERAND (fns, 0);
7275 template_args = tsubst_copy (template_args, args,
7276 complain, in_decl);
7277 maybe_fold_nontype_args (template_args);
7279 name = DECL_NAME (get_first_fn (fns));
7280 t = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7281 if (BASELINK_P (t) && template_id_p)
7282 BASELINK_FUNCTIONS (t)
7283 = build_nt (TEMPLATE_ID_EXPR,
7284 BASELINK_FUNCTIONS (t),
7285 template_args);
7286 return adjust_result_of_qualified_name_lookup (t,
7287 qualifying_scope,
7288 current_class_type);
7291 case TEMPLATE_DECL:
7292 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7293 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7294 args, complain, in_decl);
7295 else if (is_member_template (t))
7296 return tsubst (t, args, complain, in_decl);
7297 else
7298 return t;
7300 case LOOKUP_EXPR:
7302 /* We must tsubst into a LOOKUP_EXPR in case the names to
7303 which it refers is a conversion operator; in that case the
7304 name will change. We avoid making unnecessary copies,
7305 however. */
7307 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7309 if (id != TREE_OPERAND (t, 0))
7311 r = build_nt (LOOKUP_EXPR, id);
7312 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7313 t = r;
7316 return t;
7319 case CAST_EXPR:
7320 case REINTERPRET_CAST_EXPR:
7321 case CONST_CAST_EXPR:
7322 case STATIC_CAST_EXPR:
7323 case DYNAMIC_CAST_EXPR:
7324 case NOP_EXPR:
7325 return build1
7326 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7327 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7329 case INDIRECT_REF:
7330 case NEGATE_EXPR:
7331 case TRUTH_NOT_EXPR:
7332 case BIT_NOT_EXPR:
7333 case ADDR_EXPR:
7334 case CONVERT_EXPR: /* Unary + */
7335 case SIZEOF_EXPR:
7336 case ALIGNOF_EXPR:
7337 case ARROW_EXPR:
7338 case THROW_EXPR:
7339 case TYPEID_EXPR:
7340 case REALPART_EXPR:
7341 case IMAGPART_EXPR:
7342 return build1
7343 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7344 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7346 case COMPONENT_REF:
7348 tree object;
7349 tree name;
7351 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7352 name = TREE_OPERAND (t, 1);
7353 if (TREE_CODE (name) == BIT_NOT_EXPR)
7355 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7356 complain, in_decl);
7357 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7359 else if (TREE_CODE (name) == SCOPE_REF
7360 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7362 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7363 complain, in_decl);
7364 name = TREE_OPERAND (name, 1);
7365 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7366 complain, in_decl);
7367 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7368 name = build_nt (SCOPE_REF, base, name);
7370 else
7371 name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7372 return build_nt (COMPONENT_REF, object, name);
7375 case PLUS_EXPR:
7376 case MINUS_EXPR:
7377 case MULT_EXPR:
7378 case TRUNC_DIV_EXPR:
7379 case CEIL_DIV_EXPR:
7380 case FLOOR_DIV_EXPR:
7381 case ROUND_DIV_EXPR:
7382 case EXACT_DIV_EXPR:
7383 case BIT_AND_EXPR:
7384 case BIT_ANDTC_EXPR:
7385 case BIT_IOR_EXPR:
7386 case BIT_XOR_EXPR:
7387 case TRUNC_MOD_EXPR:
7388 case FLOOR_MOD_EXPR:
7389 case TRUTH_ANDIF_EXPR:
7390 case TRUTH_ORIF_EXPR:
7391 case TRUTH_AND_EXPR:
7392 case TRUTH_OR_EXPR:
7393 case RSHIFT_EXPR:
7394 case LSHIFT_EXPR:
7395 case RROTATE_EXPR:
7396 case LROTATE_EXPR:
7397 case EQ_EXPR:
7398 case NE_EXPR:
7399 case MAX_EXPR:
7400 case MIN_EXPR:
7401 case LE_EXPR:
7402 case GE_EXPR:
7403 case LT_EXPR:
7404 case GT_EXPR:
7405 case ARRAY_REF:
7406 case COMPOUND_EXPR:
7407 case SCOPE_REF:
7408 case DOTSTAR_EXPR:
7409 case MEMBER_REF:
7410 case PREDECREMENT_EXPR:
7411 case PREINCREMENT_EXPR:
7412 case POSTDECREMENT_EXPR:
7413 case POSTINCREMENT_EXPR:
7414 return build_nt
7415 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7416 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7418 case CALL_EXPR:
7419 return build_nt (code,
7420 tsubst_copy (TREE_OPERAND (t, 0), args,
7421 complain, in_decl),
7422 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7423 in_decl),
7424 NULL_TREE);
7426 case METHOD_CALL_EXPR:
7427 return build_nt
7428 (code,
7429 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7430 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7431 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7432 NULL_TREE);
7434 case STMT_EXPR:
7435 /* This processing should really occur in tsubst_expr. However,
7436 tsubst_expr does not recurse into expressions, since it
7437 assumes that there aren't any statements inside them. So, we
7438 need to expand the STMT_EXPR here. */
7439 if (!processing_template_decl)
7441 tree stmt_expr = begin_stmt_expr ();
7442 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
7443 return finish_stmt_expr (stmt_expr);
7446 return t;
7448 case COND_EXPR:
7449 case MODOP_EXPR:
7450 case PSEUDO_DTOR_EXPR:
7452 r = build_nt
7453 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7454 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7455 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7456 return r;
7459 case NEW_EXPR:
7461 r = build_nt
7462 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7463 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7464 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7465 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7466 return r;
7469 case DELETE_EXPR:
7471 r = build_nt
7472 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7473 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7474 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7475 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7476 return r;
7479 case TEMPLATE_ID_EXPR:
7481 /* Substituted template arguments */
7482 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7483 in_decl);
7485 maybe_fold_nontype_args (targs);
7486 return lookup_template_function
7487 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7490 case TREE_LIST:
7492 tree purpose, value, chain;
7494 if (t == void_list_node)
7495 return t;
7497 purpose = TREE_PURPOSE (t);
7498 if (purpose)
7499 purpose = tsubst_copy (purpose, args, complain, in_decl);
7500 value = TREE_VALUE (t);
7501 if (value)
7502 value = tsubst_copy (value, args, complain, in_decl);
7503 chain = TREE_CHAIN (t);
7504 if (chain && chain != void_type_node)
7505 chain = tsubst_copy (chain, args, complain, in_decl);
7506 if (purpose == TREE_PURPOSE (t)
7507 && value == TREE_VALUE (t)
7508 && chain == TREE_CHAIN (t))
7509 return t;
7510 return tree_cons (purpose, value, chain);
7513 case RECORD_TYPE:
7514 case UNION_TYPE:
7515 case ENUMERAL_TYPE:
7516 case INTEGER_TYPE:
7517 case TEMPLATE_TYPE_PARM:
7518 case TEMPLATE_TEMPLATE_PARM:
7519 case BOUND_TEMPLATE_TEMPLATE_PARM:
7520 case TEMPLATE_PARM_INDEX:
7521 case POINTER_TYPE:
7522 case REFERENCE_TYPE:
7523 case OFFSET_TYPE:
7524 case FUNCTION_TYPE:
7525 case METHOD_TYPE:
7526 case ARRAY_TYPE:
7527 case TYPENAME_TYPE:
7528 case UNBOUND_CLASS_TEMPLATE:
7529 case TYPEOF_TYPE:
7530 case TYPE_DECL:
7531 return tsubst (t, args, complain, in_decl);
7533 case IDENTIFIER_NODE:
7534 if (IDENTIFIER_TYPENAME_P (t))
7536 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7537 return mangle_conv_op_name_for_type (new_type);
7539 else
7540 return t;
7542 case CONSTRUCTOR:
7544 r = build_constructor
7545 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7546 tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
7547 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7548 return r;
7551 case VA_ARG_EXPR:
7552 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7553 in_decl),
7554 tsubst (TREE_TYPE (t), args, complain, in_decl));
7556 default:
7557 return t;
7561 /* Like tsubst_copy for expressions, etc. but also does semantic
7562 processing. */
7564 static tree
7565 tsubst_expr (t, args, complain, in_decl)
7566 tree t, args;
7567 tsubst_flags_t complain;
7568 tree in_decl;
7570 tree stmt, tmp;
7572 if (t == NULL_TREE || t == error_mark_node)
7573 return t;
7575 if (processing_template_decl)
7576 return tsubst_copy (t, args, complain, in_decl);
7578 if (!STATEMENT_CODE_P (TREE_CODE (t)))
7579 return tsubst_copy_and_build (t, args, complain, in_decl);
7581 switch (TREE_CODE (t))
7583 case CTOR_INITIALIZER:
7584 prep_stmt (t);
7585 finish_mem_initializers (tsubst_initializer_list
7586 (TREE_OPERAND (t, 0), args));
7587 break;
7589 case RETURN_STMT:
7590 prep_stmt (t);
7591 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
7592 args, complain, in_decl));
7593 break;
7595 case EXPR_STMT:
7596 prep_stmt (t);
7597 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7598 args, complain, in_decl));
7599 break;
7601 case USING_STMT:
7602 prep_stmt (t);
7603 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7604 args, complain, in_decl));
7605 break;
7607 case DECL_STMT:
7609 tree decl;
7610 tree init;
7612 prep_stmt (t);
7613 decl = DECL_STMT_DECL (t);
7614 if (TREE_CODE (decl) == LABEL_DECL)
7615 finish_label_decl (DECL_NAME (decl));
7616 else if (TREE_CODE (decl) == USING_DECL)
7618 tree scope = DECL_INITIAL (decl);
7619 tree name = DECL_NAME (decl);
7621 scope = tsubst_expr (scope, args, complain, in_decl);
7622 do_local_using_decl (lookup_qualified_name (scope,
7623 name,
7624 /*is_type_p=*/0,
7625 /*flags=*/0));
7627 else
7629 init = DECL_INITIAL (decl);
7630 decl = tsubst (decl, args, complain, in_decl);
7631 if (decl != error_mark_node)
7633 if (init)
7634 DECL_INITIAL (decl) = error_mark_node;
7635 /* By marking the declaration as instantiated, we avoid
7636 trying to instantiate it. Since instantiate_decl can't
7637 handle local variables, and since we've already done
7638 all that needs to be done, that's the right thing to
7639 do. */
7640 if (TREE_CODE (decl) == VAR_DECL)
7641 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7642 if (TREE_CODE (decl) == VAR_DECL
7643 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7644 /* Anonymous aggregates are a special case. */
7645 finish_anon_union (decl);
7646 else
7648 maybe_push_decl (decl);
7649 if (DECL_PRETTY_FUNCTION_P (decl))
7651 /* For __PRETTY_FUNCTION__ we have to adjust the
7652 initializer. */
7653 const char *const name
7654 = cxx_printable_name (current_function_decl, 2);
7655 init = cp_fname_init (name);
7656 TREE_TYPE (decl) = TREE_TYPE (init);
7658 else
7659 init = tsubst_expr (init, args, complain, in_decl);
7660 cp_finish_decl (decl, init, NULL_TREE, 0);
7665 /* A DECL_STMT can also be used as an expression, in the condition
7666 clause of an if/for/while construct. If we aren't followed by
7667 another statement, return our decl. */
7668 if (TREE_CHAIN (t) == NULL_TREE)
7669 return decl;
7671 break;
7673 case FOR_STMT:
7675 prep_stmt (t);
7677 stmt = begin_for_stmt ();
7678 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7679 finish_for_init_stmt (stmt);
7680 finish_for_cond (tsubst_expr (FOR_COND (t),
7681 args, complain, in_decl),
7682 stmt);
7683 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7684 finish_for_expr (tmp, stmt);
7685 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7686 finish_for_stmt (stmt);
7688 break;
7690 case WHILE_STMT:
7692 prep_stmt (t);
7693 stmt = begin_while_stmt ();
7694 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7695 args, complain, in_decl),
7696 stmt);
7697 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7698 finish_while_stmt (stmt);
7700 break;
7702 case DO_STMT:
7704 prep_stmt (t);
7705 stmt = begin_do_stmt ();
7706 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7707 finish_do_body (stmt);
7708 finish_do_stmt (tsubst_expr (DO_COND (t),
7709 args, complain, in_decl),
7710 stmt);
7712 break;
7714 case IF_STMT:
7716 prep_stmt (t);
7717 stmt = begin_if_stmt ();
7718 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7719 args, complain, in_decl),
7720 stmt);
7722 if (tmp = THEN_CLAUSE (t), tmp)
7724 tsubst_expr (tmp, args, complain, in_decl);
7725 finish_then_clause (stmt);
7728 if (tmp = ELSE_CLAUSE (t), tmp)
7730 begin_else_clause ();
7731 tsubst_expr (tmp, args, complain, in_decl);
7732 finish_else_clause (stmt);
7735 finish_if_stmt ();
7737 break;
7739 case COMPOUND_STMT:
7741 prep_stmt (t);
7742 if (COMPOUND_STMT_BODY_BLOCK (t))
7743 stmt = begin_function_body ();
7744 else
7745 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7747 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7749 if (COMPOUND_STMT_BODY_BLOCK (t))
7750 finish_function_body (stmt);
7751 else
7752 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7754 break;
7756 case BREAK_STMT:
7757 prep_stmt (t);
7758 finish_break_stmt ();
7759 break;
7761 case CONTINUE_STMT:
7762 prep_stmt (t);
7763 finish_continue_stmt ();
7764 break;
7766 case SWITCH_STMT:
7768 tree val;
7770 prep_stmt (t);
7771 stmt = begin_switch_stmt ();
7772 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7773 finish_switch_cond (val, stmt);
7774 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7775 finish_switch_stmt (stmt);
7777 break;
7779 case CASE_LABEL:
7780 prep_stmt (t);
7781 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7782 tsubst_expr (CASE_HIGH (t), args, complain,
7783 in_decl));
7784 break;
7786 case LABEL_STMT:
7787 input_line = STMT_LINENO (t);
7788 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7789 break;
7791 case FILE_STMT:
7792 input_filename = FILE_STMT_FILENAME (t);
7793 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7794 break;
7796 case GOTO_STMT:
7797 prep_stmt (t);
7798 tmp = GOTO_DESTINATION (t);
7799 if (TREE_CODE (tmp) != LABEL_DECL)
7800 /* Computed goto's must be tsubst'd into. On the other hand,
7801 non-computed gotos must not be; the identifier in question
7802 will have no binding. */
7803 tmp = tsubst_expr (tmp, args, complain, in_decl);
7804 else
7805 tmp = DECL_NAME (tmp);
7806 finish_goto_stmt (tmp);
7807 break;
7809 case ASM_STMT:
7810 prep_stmt (t);
7811 tmp = finish_asm_stmt
7812 (ASM_CV_QUAL (t),
7813 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7814 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7815 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7816 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7817 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
7818 break;
7820 case TRY_BLOCK:
7821 prep_stmt (t);
7822 if (CLEANUP_P (t))
7824 stmt = begin_try_block ();
7825 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7826 finish_cleanup_try_block (stmt);
7827 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7828 complain, in_decl),
7829 stmt);
7831 else
7833 if (FN_TRY_BLOCK_P (t))
7834 stmt = begin_function_try_block ();
7835 else
7836 stmt = begin_try_block ();
7838 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7840 if (FN_TRY_BLOCK_P (t))
7841 finish_function_try_block (stmt);
7842 else
7843 finish_try_block (stmt);
7845 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7846 if (FN_TRY_BLOCK_P (t))
7847 finish_function_handler_sequence (stmt);
7848 else
7849 finish_handler_sequence (stmt);
7851 break;
7853 case HANDLER:
7855 tree decl;
7857 prep_stmt (t);
7858 stmt = begin_handler ();
7859 if (HANDLER_PARMS (t))
7861 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7862 decl = tsubst (decl, args, complain, in_decl);
7863 /* Prevent instantiate_decl from trying to instantiate
7864 this variable. We've already done all that needs to be
7865 done. */
7866 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7868 else
7869 decl = NULL_TREE;
7870 finish_handler_parms (decl, stmt);
7871 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7872 finish_handler (stmt);
7874 break;
7876 case TAG_DEFN:
7877 prep_stmt (t);
7878 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7879 break;
7881 default:
7882 abort ();
7885 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7888 /* Like tsubst but deals with expressions and performs semantic
7889 analysis. */
7891 static tree
7892 tsubst_copy_and_build (t, args, complain, in_decl)
7893 tree t, args;
7894 tsubst_flags_t complain;
7895 tree in_decl;
7897 if (t == NULL_TREE || t == error_mark_node)
7898 return t;
7900 switch (TREE_CODE (t))
7902 case IDENTIFIER_NODE:
7903 if (IDENTIFIER_TYPENAME_P (t))
7905 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7906 return do_identifier (mangle_conv_op_name_for_type (new_type),
7907 NULL_TREE);
7909 else
7910 return do_identifier (t, NULL_TREE);
7912 case LOOKUP_EXPR:
7914 if (LOOKUP_EXPR_GLOBAL (t))
7916 tree token
7917 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7918 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
7920 else
7922 t = do_identifier
7923 (tsubst_copy
7924 (TREE_OPERAND (t, 0), args, complain, in_decl),
7925 NULL_TREE);
7926 if (TREE_CODE (t) == ALIAS_DECL)
7927 t = DECL_INITIAL (t);
7928 return t;
7932 case TEMPLATE_ID_EXPR:
7934 tree object;
7935 tree template
7936 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7937 tree targs
7938 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7940 if (TREE_CODE (template) == COMPONENT_REF)
7942 object = TREE_OPERAND (template, 0);
7943 template = TREE_OPERAND (template, 1);
7945 else
7946 object = NULL_TREE;
7947 maybe_fold_nontype_args (targs);
7948 template = lookup_template_function (template, targs);
7950 if (object)
7951 return build (COMPONENT_REF, TREE_TYPE (template),
7952 object, template);
7953 else
7954 return template;
7957 case INDIRECT_REF:
7958 return build_x_indirect_ref
7959 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7960 "unary *");
7962 case CAST_EXPR:
7963 return build_functional_cast
7964 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7965 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7967 case REINTERPRET_CAST_EXPR:
7968 return build_reinterpret_cast
7969 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7970 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7972 case CONST_CAST_EXPR:
7973 return build_const_cast
7974 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7975 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7977 case DYNAMIC_CAST_EXPR:
7978 return build_dynamic_cast
7979 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7980 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7982 case STATIC_CAST_EXPR:
7983 return build_static_cast
7984 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7985 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7987 case PREDECREMENT_EXPR:
7988 case PREINCREMENT_EXPR:
7989 case POSTDECREMENT_EXPR:
7990 case POSTINCREMENT_EXPR:
7991 if (TREE_TYPE (t))
7992 return tsubst_copy (t, args, complain, in_decl);
7993 else
7994 return build_x_unary_op
7995 (TREE_CODE (t),
7996 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7997 in_decl));
7999 case NEGATE_EXPR:
8000 case BIT_NOT_EXPR:
8001 if (TREE_TYPE (t))
8002 return tsubst_copy (t, args, complain, in_decl);
8003 else
8004 return build_x_unary_op
8005 (TREE_CODE (t),
8006 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8007 in_decl));
8009 case ABS_EXPR:
8010 if (TREE_TYPE (t))
8011 return t;
8012 return build_x_unary_op
8013 (TREE_CODE (t),
8014 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8016 case TRUTH_NOT_EXPR:
8017 case ADDR_EXPR:
8018 case CONVERT_EXPR: /* Unary + */
8019 case REALPART_EXPR:
8020 case IMAGPART_EXPR:
8021 if (TREE_TYPE (t))
8022 return tsubst_copy (t, args, complain, in_decl);
8023 else
8024 return build_x_unary_op
8025 (TREE_CODE (t),
8026 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8027 in_decl));
8029 case PLUS_EXPR:
8030 case MINUS_EXPR:
8031 case MULT_EXPR:
8032 case TRUNC_DIV_EXPR:
8033 case CEIL_DIV_EXPR:
8034 case FLOOR_DIV_EXPR:
8035 case ROUND_DIV_EXPR:
8036 case EXACT_DIV_EXPR:
8037 case BIT_AND_EXPR:
8038 case BIT_ANDTC_EXPR:
8039 case BIT_IOR_EXPR:
8040 case BIT_XOR_EXPR:
8041 case TRUNC_MOD_EXPR:
8042 case FLOOR_MOD_EXPR:
8043 case TRUTH_ANDIF_EXPR:
8044 case TRUTH_ORIF_EXPR:
8045 case TRUTH_AND_EXPR:
8046 case TRUTH_OR_EXPR:
8047 case RSHIFT_EXPR:
8048 case LSHIFT_EXPR:
8049 case RROTATE_EXPR:
8050 case LROTATE_EXPR:
8051 case EQ_EXPR:
8052 case NE_EXPR:
8053 case MAX_EXPR:
8054 case MIN_EXPR:
8055 case LE_EXPR:
8056 case GE_EXPR:
8057 case LT_EXPR:
8058 case GT_EXPR:
8059 case MEMBER_REF:
8060 return build_x_binary_op
8061 (TREE_CODE (t),
8062 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8063 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8065 case DOTSTAR_EXPR:
8066 return build_m_component_ref
8067 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8068 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8070 case SCOPE_REF:
8071 return build_offset_ref
8072 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8073 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8075 case ARRAY_REF:
8077 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8078 == NULL_TREE)
8079 /* new-type-id */
8080 return build_nt
8081 (ARRAY_REF, NULL_TREE,
8082 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8083 in_decl));
8085 return grok_array_decl
8086 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8087 in_decl),
8088 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8089 in_decl));
8092 case SIZEOF_EXPR:
8093 case ALIGNOF_EXPR:
8095 tree r =
8096 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8097 if (!TYPE_P (r))
8098 return TREE_CODE (t) == SIZEOF_EXPR ?
8099 expr_sizeof (r) : c_alignof_expr (r);
8100 else
8101 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
8104 case MODOP_EXPR:
8105 return build_x_modify_expr
8106 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8107 TREE_CODE (TREE_OPERAND (t, 1)),
8108 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8110 case ARROW_EXPR:
8111 return build_x_arrow
8112 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8114 case NEW_EXPR:
8115 return build_new
8116 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8117 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8118 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl),
8119 NEW_EXPR_USE_GLOBAL (t));
8121 case DELETE_EXPR:
8122 return delete_sanity
8123 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8124 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8125 DELETE_EXPR_USE_VEC (t),
8126 DELETE_EXPR_USE_GLOBAL (t));
8128 case COMPOUND_EXPR:
8130 if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)
8131 == NULL_TREE)
8132 return build_x_compound_expr
8133 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8134 in_decl));
8135 else
8136 abort ();
8139 case METHOD_CALL_EXPR:
8141 tree method
8142 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8144 if (TREE_CODE (method) == SCOPE_REF)
8146 tree name = TREE_OPERAND (method, 1);
8148 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8149 name = build_nt (TEMPLATE_ID_EXPR,
8150 TREE_OPERAND (name, 0),
8151 TREE_OPERAND (name, 1));
8153 return build_scoped_method_call
8154 (tsubst_copy_and_build
8155 (TREE_OPERAND (t, 1), args, complain, in_decl),
8156 TREE_OPERAND (method, 0),
8157 name,
8158 tsubst_copy_and_build
8159 (TREE_OPERAND (t, 2), args, complain, in_decl));
8161 else
8163 /* We can get a TEMPLATE_ID_EXPR here on code like:
8165 x->f<2>();
8167 so we must resolve that. However, we can also get things
8168 like a BIT_NOT_EXPR here, when referring to a destructor,
8169 and things like that are not correctly resolved by this
8170 function so just use it when we really need it. */
8171 if (TREE_CODE (method) == TEMPLATE_ID_EXPR)
8172 method = lookup_template_function
8173 (TREE_OPERAND (method, 0),
8174 TREE_OPERAND (method, 1));
8176 return build_method_call
8177 (tsubst_copy_and_build
8178 (TREE_OPERAND (t, 1), args, complain, in_decl),
8179 method,
8180 tsubst_copy_and_build
8181 (TREE_OPERAND (t, 2), args, complain, in_decl),
8182 NULL_TREE, LOOKUP_NORMAL);
8186 case CALL_EXPR:
8188 tree function, copy_args;
8190 function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8191 copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
8192 complain, in_decl);
8194 if (BASELINK_P (function))
8195 return build_call_from_tree (function, copy_args, 1);
8196 else if (TREE_CODE (function) == SCOPE_REF)
8198 tree name = TREE_OPERAND (function, 1);
8199 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8200 name = build_nt (TEMPLATE_ID_EXPR,
8201 TREE_OPERAND (name, 0),
8202 build_expr_from_tree (TREE_OPERAND (name, 1)));
8204 function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
8205 name);
8207 return build_call_from_tree (function, copy_args, 1);
8209 else
8211 tree name = function;
8212 tree id;
8214 if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
8215 && !LOOKUP_EXPR_GLOBAL (name)
8216 && (TREE_CODE ((id = TREE_OPERAND (name, 0)))
8217 == IDENTIFIER_NODE)
8218 && (!current_class_type
8219 || !lookup_member (current_class_type, id, 0, false)))
8221 /* Do Koenig lookup if there are no class members. */
8222 name = do_identifier (id, copy_args);
8224 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
8225 || ! really_overloaded_fn (name))
8226 name = build_expr_from_tree (name);
8228 if (TREE_CODE (name) == OFFSET_REF)
8229 return build_offset_ref_call_from_tree (name, copy_args);
8230 if (TREE_CODE (name) == COMPONENT_REF)
8231 return finish_object_call_expr (TREE_OPERAND (name, 1),
8232 TREE_OPERAND (name, 0),
8233 copy_args);
8234 name = convert_from_reference (name);
8235 return build_call_from_tree (name, copy_args,
8236 /*disallow_virtual=*/false);
8240 case COND_EXPR:
8241 return build_x_conditional_expr
8242 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8243 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8244 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8246 case PSEUDO_DTOR_EXPR:
8247 return finish_pseudo_destructor_expr
8248 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8249 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8250 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8252 case TREE_LIST:
8254 tree purpose, value, chain;
8256 if (t == void_list_node)
8257 return t;
8259 purpose = TREE_PURPOSE (t);
8260 if (purpose)
8261 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
8262 value = TREE_VALUE (t);
8263 if (value)
8264 value = tsubst_copy_and_build (value, args, complain, in_decl);
8265 chain = TREE_CHAIN (t);
8266 if (chain && chain != void_type_node)
8267 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
8268 if (purpose == TREE_PURPOSE (t)
8269 && value == TREE_VALUE (t)
8270 && chain == TREE_CHAIN (t))
8271 return t;
8272 return tree_cons (purpose, value, chain);
8275 case COMPONENT_REF:
8277 tree object =
8278 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8279 tree member =
8280 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
8282 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8284 if (TREE_CODE (member) == BIT_NOT_EXPR)
8285 return finish_pseudo_destructor_expr (object,
8286 NULL_TREE,
8287 TREE_TYPE (object));
8288 else if (TREE_CODE (member) == SCOPE_REF
8289 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8290 return finish_pseudo_destructor_expr (object,
8291 object,
8292 TREE_TYPE (object));
8294 else if (TREE_CODE (member) == SCOPE_REF
8295 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8297 tree tmpl;
8298 tree args;
8300 /* Lookup the template functions now that we know what the
8301 scope is. */
8302 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8303 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8304 member = lookup_qualified_name (TREE_OPERAND (member, 0),
8305 tmpl,
8306 /*is_type=*/0,
8307 /*flags=*/0);
8308 if (BASELINK_P (member))
8309 BASELINK_FUNCTIONS (member)
8310 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8311 args);
8312 else
8314 error ("`%D' is not a member of `%T'",
8315 tmpl, TREE_TYPE (object));
8316 return error_mark_node;
8320 return finish_class_member_access_expr (object, member);
8323 case THROW_EXPR:
8324 return build_throw
8325 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8327 case CONSTRUCTOR:
8329 tree r;
8330 tree elts;
8331 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8332 bool purpose_p;
8334 /* digest_init will do the wrong thing if we let it. */
8335 if (type && TYPE_PTRMEMFUNC_P (type))
8336 return t;
8338 r = NULL_TREE;
8339 /* We do not want to process the purpose of aggregate
8340 initializers as they are identifier nodes which will be
8341 looked up by digest_init. */
8342 purpose_p = !(type && IS_AGGR_TYPE (type));
8343 for (elts = CONSTRUCTOR_ELTS (t);
8344 elts;
8345 elts = TREE_CHAIN (elts))
8347 tree purpose = TREE_PURPOSE (elts);
8348 tree value = TREE_VALUE (elts);
8350 if (purpose && purpose_p)
8351 purpose
8352 = tsubst_copy_and_build (purpose, args, complain, in_decl);
8353 value = tsubst_copy_and_build (value, args, complain, in_decl);
8354 r = tree_cons (purpose, value, r);
8357 r = build_constructor (NULL_TREE, nreverse (r));
8358 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8360 if (type)
8361 return digest_init (type, r, 0);
8362 return r;
8365 case TYPEID_EXPR:
8367 tree operand_0
8368 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8369 in_decl);
8371 if (TYPE_P (operand_0))
8372 return get_typeid (operand_0);
8373 return build_typeid (operand_0);
8376 case PARM_DECL:
8377 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8379 case VAR_DECL:
8380 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8382 case VA_ARG_EXPR:
8383 return build_x_va_arg
8384 (tsubst_copy_and_build
8385 (TREE_OPERAND (t, 0), args, complain, in_decl),
8386 tsubst_copy (TREE_TYPE (t), args, complain, in_decl));
8388 default:
8389 return tsubst_copy (t, args, complain, in_decl);
8393 /* Verify that the instantiated ARGS are valid. For type arguments,
8394 make sure that the type's linkage is ok. For non-type arguments,
8395 make sure they are constants if they are integral or enumarations.
8396 Emit an error under control of COMPLAIN, and return TRUE on error. */
8398 static bool
8399 check_instantiated_args (tmpl, args, complain)
8400 tree tmpl, args;
8401 tsubst_flags_t complain;
8403 int ix, len = DECL_NTPARMS (tmpl);
8404 bool result = false;
8406 for (ix = 0; ix != len; ix++)
8408 tree t = TREE_VEC_ELT (args, ix);
8410 if (TYPE_P (t))
8412 /* [basic.link]: A name with no linkage (notably, the name
8413 of a class or enumeration declared in a local scope)
8414 shall not be used to declare an entity with linkage.
8415 This implies that names with no linkage cannot be used as
8416 template arguments. */
8417 tree nt = no_linkage_check (t);
8419 if (nt)
8421 if (!(complain & tf_error))
8422 /*OK*/;
8423 else if (TYPE_ANONYMOUS_P (nt))
8424 error ("`%T' uses anonymous type", t);
8425 else
8426 error ("`%T' uses local type `%T'", t, nt);
8427 result = true;
8429 /* In order to avoid all sorts of complications, we do not
8430 allow variably-modified types as template arguments. */
8431 else if (variably_modified_type_p (t))
8433 if (complain & tf_error)
8434 error ("`%T' is a variably modified type", t);
8435 result = true;
8438 /* A non-type argument of integral or enumerated type must be a
8439 constant. */
8440 else if (TREE_TYPE (t)
8441 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
8442 && !TREE_CONSTANT (t))
8444 if (complain & tf_error)
8445 error ("integral expression `%E' is not constant", t);
8446 result = true;
8449 if (result && complain & tf_error)
8450 error (" trying to instantiate `%D'", tmpl);
8451 return result;
8454 /* Instantiate the indicated variable or function template TMPL with
8455 the template arguments in TARG_PTR. */
8457 tree
8458 instantiate_template (tmpl, targ_ptr, complain)
8459 tree tmpl, targ_ptr;
8460 tsubst_flags_t complain;
8462 tree fndecl;
8463 tree gen_tmpl;
8464 tree spec;
8466 if (tmpl == error_mark_node)
8467 return error_mark_node;
8469 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
8471 /* If this function is a clone, handle it specially. */
8472 if (DECL_CLONED_FUNCTION_P (tmpl))
8474 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
8475 complain);
8476 tree clone;
8478 /* Look for the clone. */
8479 for (clone = TREE_CHAIN (spec);
8480 clone && DECL_CLONED_FUNCTION_P (clone);
8481 clone = TREE_CHAIN (clone))
8482 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8483 return clone;
8484 /* We should always have found the clone by now. */
8485 abort ();
8486 return NULL_TREE;
8489 /* Check to see if we already have this specialization. */
8490 spec = retrieve_specialization (tmpl, targ_ptr);
8491 if (spec != NULL_TREE)
8492 return spec;
8494 gen_tmpl = most_general_template (tmpl);
8495 if (tmpl != gen_tmpl)
8497 /* The TMPL is a partial instantiation. To get a full set of
8498 arguments we must add the arguments used to perform the
8499 partial instantiation. */
8500 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8501 targ_ptr);
8503 /* Check to see if we already have this specialization. */
8504 spec = retrieve_specialization (gen_tmpl, targ_ptr);
8505 if (spec != NULL_TREE)
8506 return spec;
8509 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
8510 complain))
8511 return error_mark_node;
8513 /* Make sure that we can see identifiers, and compute access
8514 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8515 created earlier. Let push_access_scope_real figure that out. */
8516 push_access_scope_real
8517 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
8518 complain, gen_tmpl));
8520 /* substitute template parameters */
8521 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
8522 targ_ptr, complain, gen_tmpl);
8524 pop_access_scope (gen_tmpl);
8526 /* The DECL_TI_TEMPLATE should always be the immediate parent
8527 template, not the most general template. */
8528 DECL_TI_TEMPLATE (fndecl) = tmpl;
8530 if (flag_external_templates)
8531 add_pending_template (fndecl);
8533 /* If we've just instantiated the main entry point for a function,
8534 instantiate all the alternate entry points as well. We do this
8535 by cloning the instantiation of the main entry point, not by
8536 instantiating the template clones. */
8537 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8538 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
8540 return fndecl;
8543 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8544 arguments that are being used when calling it. TARGS is a vector
8545 into which the deduced template arguments are placed.
8547 Return zero for success, 2 for an incomplete match that doesn't resolve
8548 all the types, and 1 for complete failure. An error message will be
8549 printed only for an incomplete match.
8551 If FN is a conversion operator, or we are trying to produce a specific
8552 specialization, RETURN_TYPE is the return type desired.
8554 The EXPLICIT_TARGS are explicit template arguments provided via a
8555 template-id.
8557 The parameter STRICT is one of:
8559 DEDUCE_CALL:
8560 We are deducing arguments for a function call, as in
8561 [temp.deduct.call].
8563 DEDUCE_CONV:
8564 We are deducing arguments for a conversion function, as in
8565 [temp.deduct.conv].
8567 DEDUCE_EXACT:
8568 We are deducing arguments when doing an explicit instantiation
8569 as in [temp.explicit], when determining an explicit specialization
8570 as in [temp.expl.spec], or when taking the address of a function
8571 template, as in [temp.deduct.funcaddr].
8573 DEDUCE_ORDER:
8574 We are deducing arguments when calculating the partial
8575 ordering between specializations of function or class
8576 templates, as in [temp.func.order] and [temp.class.order].
8578 LEN is the number of parms to consider before returning success, or -1
8579 for all. This is used in partial ordering to avoid comparing parms for
8580 which no actual argument was passed, since they are not considered in
8581 overload resolution (and are explicitly excluded from consideration in
8582 partial ordering in [temp.func.order]/6). */
8585 fn_type_unification (fn, explicit_targs, targs, args, return_type,
8586 strict, len)
8587 tree fn, explicit_targs, targs, args, return_type;
8588 unification_kind_t strict;
8589 int len;
8591 tree parms;
8592 tree fntype;
8593 int result;
8595 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
8597 fntype = TREE_TYPE (fn);
8598 if (explicit_targs)
8600 /* [temp.deduct]
8602 The specified template arguments must match the template
8603 parameters in kind (i.e., type, nontype, template), and there
8604 must not be more arguments than there are parameters;
8605 otherwise type deduction fails.
8607 Nontype arguments must match the types of the corresponding
8608 nontype template parameters, or must be convertible to the
8609 types of the corresponding nontype parameters as specified in
8610 _temp.arg.nontype_, otherwise type deduction fails.
8612 All references in the function type of the function template
8613 to the corresponding template parameters are replaced by the
8614 specified template argument values. If a substitution in a
8615 template parameter or in the function type of the function
8616 template results in an invalid type, type deduction fails. */
8617 int i;
8618 tree converted_args;
8620 converted_args
8621 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8622 explicit_targs, NULL_TREE, tf_none,
8623 /*require_all_arguments=*/0));
8624 if (converted_args == error_mark_node)
8625 return 1;
8627 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
8628 if (fntype == error_mark_node)
8629 return 1;
8631 /* Place the explicitly specified arguments in TARGS. */
8632 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
8633 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
8636 parms = TYPE_ARG_TYPES (fntype);
8637 /* Never do unification on the 'this' parameter. */
8638 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8639 parms = TREE_CHAIN (parms);
8641 if (return_type)
8643 /* We've been given a return type to match, prepend it. */
8644 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8645 args = tree_cons (NULL_TREE, return_type, args);
8646 if (len >= 0)
8647 ++len;
8650 /* We allow incomplete unification without an error message here
8651 because the standard doesn't seem to explicitly prohibit it. Our
8652 callers must be ready to deal with unification failures in any
8653 event. */
8654 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8655 targs, parms, args, /*subr=*/0,
8656 strict, /*allow_incomplete*/1, len);
8658 if (result == 0)
8659 /* All is well so far. Now, check:
8661 [temp.deduct]
8663 When all template arguments have been deduced, all uses of
8664 template parameters in nondeduced contexts are replaced with
8665 the corresponding deduced argument values. If the
8666 substitution results in an invalid type, as described above,
8667 type deduction fails. */
8668 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
8669 == error_mark_node)
8670 return 1;
8672 return result;
8675 /* Adjust types before performing type deduction, as described in
8676 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8677 sections are symmetric. PARM is the type of a function parameter
8678 or the return type of the conversion function. ARG is the type of
8679 the argument passed to the call, or the type of the value
8680 initialized with the result of the conversion function. */
8682 static int
8683 maybe_adjust_types_for_deduction (strict, parm, arg)
8684 unification_kind_t strict;
8685 tree* parm;
8686 tree* arg;
8688 int result = 0;
8690 switch (strict)
8692 case DEDUCE_CALL:
8693 break;
8695 case DEDUCE_CONV:
8697 /* Swap PARM and ARG throughout the remainder of this
8698 function; the handling is precisely symmetric since PARM
8699 will initialize ARG rather than vice versa. */
8700 tree* temp = parm;
8701 parm = arg;
8702 arg = temp;
8703 break;
8706 case DEDUCE_EXACT:
8707 /* There is nothing to do in this case. */
8708 return 0;
8710 case DEDUCE_ORDER:
8711 /* DR 214. [temp.func.order] is underspecified, and leads to no
8712 ordering between things like `T *' and `T const &' for `U *'.
8713 The former has T=U and the latter T=U*. The former looks more
8714 specialized and John Spicer considers it well-formed (the EDG
8715 compiler accepts it).
8717 John also confirms that deduction should proceed as in a function
8718 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8719 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8720 to an actual call can have such a type.
8722 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8723 If only ARG is a REFERENCE_TYPE, we look through that and then
8724 proceed as with DEDUCE_CALL (which could further convert it). */
8725 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8727 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8728 return 0;
8729 *arg = TREE_TYPE (*arg);
8731 break;
8732 default:
8733 abort ();
8736 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8738 /* [temp.deduct.call]
8740 If P is not a reference type:
8742 --If A is an array type, the pointer type produced by the
8743 array-to-pointer standard conversion (_conv.array_) is
8744 used in place of A for type deduction; otherwise,
8746 --If A is a function type, the pointer type produced by
8747 the function-to-pointer standard conversion
8748 (_conv.func_) is used in place of A for type deduction;
8749 otherwise,
8751 --If A is a cv-qualified type, the top level
8752 cv-qualifiers of A's type are ignored for type
8753 deduction. */
8754 if (TREE_CODE (*arg) == ARRAY_TYPE)
8755 *arg = build_pointer_type (TREE_TYPE (*arg));
8756 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
8757 *arg = build_pointer_type (*arg);
8758 else
8759 *arg = TYPE_MAIN_VARIANT (*arg);
8762 /* [temp.deduct.call]
8764 If P is a cv-qualified type, the top level cv-qualifiers
8765 of P's type are ignored for type deduction. If P is a
8766 reference type, the type referred to by P is used for
8767 type deduction. */
8768 *parm = TYPE_MAIN_VARIANT (*parm);
8769 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8771 *parm = TREE_TYPE (*parm);
8772 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8775 /* DR 322. For conversion deduction, remove a reference type on parm
8776 too (which has been swapped into ARG). */
8777 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
8778 *arg = TREE_TYPE (*arg);
8780 return result;
8783 /* Most parms like fn_type_unification.
8785 If SUBR is 1, we're being called recursively (to unify the
8786 arguments of a function or method parameter of a function
8787 template). */
8789 static int
8790 type_unification_real (tparms, targs, xparms, xargs, subr,
8791 strict, allow_incomplete, xlen)
8792 tree tparms, targs, xparms, xargs;
8793 int subr;
8794 unification_kind_t strict;
8795 int allow_incomplete, xlen;
8797 tree parm, arg;
8798 int i;
8799 int ntparms = TREE_VEC_LENGTH (tparms);
8800 int sub_strict;
8801 int saw_undeduced = 0;
8802 tree parms, args;
8803 int len;
8805 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
8806 my_friendly_assert (xparms == NULL_TREE
8807 || TREE_CODE (xparms) == TREE_LIST, 290);
8808 /* ARGS could be NULL. */
8809 if (xargs)
8810 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8811 my_friendly_assert (ntparms > 0, 292);
8813 switch (strict)
8815 case DEDUCE_CALL:
8816 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8817 | UNIFY_ALLOW_DERIVED);
8818 break;
8820 case DEDUCE_CONV:
8821 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8822 break;
8824 case DEDUCE_EXACT:
8825 sub_strict = UNIFY_ALLOW_NONE;
8826 break;
8828 case DEDUCE_ORDER:
8829 sub_strict = UNIFY_ALLOW_NONE;
8830 break;
8832 default:
8833 abort ();
8836 if (xlen == 0)
8837 return 0;
8839 again:
8840 parms = xparms;
8841 args = xargs;
8842 len = xlen;
8844 while (parms
8845 && parms != void_list_node
8846 && args
8847 && args != void_list_node)
8849 parm = TREE_VALUE (parms);
8850 parms = TREE_CHAIN (parms);
8851 arg = TREE_VALUE (args);
8852 args = TREE_CHAIN (args);
8854 if (arg == error_mark_node)
8855 return 1;
8856 if (arg == unknown_type_node)
8857 /* We can't deduce anything from this, but we might get all the
8858 template args from other function args. */
8859 continue;
8861 /* Conversions will be performed on a function argument that
8862 corresponds with a function parameter that contains only
8863 non-deducible template parameters and explicitly specified
8864 template parameters. */
8865 if (! uses_template_parms (parm))
8867 tree type;
8869 if (!TYPE_P (arg))
8870 type = TREE_TYPE (arg);
8871 else
8873 type = arg;
8874 arg = NULL_TREE;
8877 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
8879 if (same_type_p (parm, type))
8880 continue;
8882 else
8883 /* It might work; we shouldn't check now, because we might
8884 get into infinite recursion. Overload resolution will
8885 handle it. */
8886 continue;
8888 return 1;
8891 if (!TYPE_P (arg))
8893 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8894 if (type_unknown_p (arg))
8896 /* [temp.deduct.type] A template-argument can be deduced from
8897 a pointer to function or pointer to member function
8898 argument if the set of overloaded functions does not
8899 contain function templates and at most one of a set of
8900 overloaded functions provides a unique match. */
8902 if (resolve_overloaded_unification
8903 (tparms, targs, parm, arg, strict, sub_strict)
8904 != 0)
8905 return 1;
8906 continue;
8908 arg = TREE_TYPE (arg);
8912 int arg_strict = sub_strict;
8914 if (!subr)
8915 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8917 if (unify (tparms, targs, parm, arg, arg_strict))
8918 return 1;
8921 /* Are we done with the interesting parms? */
8922 if (--len == 0)
8923 goto done;
8925 /* Fail if we've reached the end of the parm list, and more args
8926 are present, and the parm list isn't variadic. */
8927 if (args && args != void_list_node && parms == void_list_node)
8928 return 1;
8929 /* Fail if parms are left and they don't have default values. */
8930 if (parms
8931 && parms != void_list_node
8932 && TREE_PURPOSE (parms) == NULL_TREE)
8933 return 1;
8935 done:
8936 if (!subr)
8937 for (i = 0; i < ntparms; i++)
8938 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8940 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8942 /* If this is an undeduced nontype parameter that depends on
8943 a type parameter, try another pass; its type may have been
8944 deduced from a later argument than the one from which
8945 this parameter can be deduced. */
8946 if (TREE_CODE (tparm) == PARM_DECL
8947 && uses_template_parms (TREE_TYPE (tparm))
8948 && !saw_undeduced++)
8949 goto again;
8951 if (!allow_incomplete)
8952 error ("incomplete type unification");
8953 return 2;
8955 return 0;
8958 /* Subroutine of type_unification_real. Args are like the variables at the
8959 call site. ARG is an overloaded function (or template-id); we try
8960 deducing template args from each of the overloads, and if only one
8961 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8963 static int
8964 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8965 sub_strict)
8966 tree tparms, targs, parm, arg;
8967 unification_kind_t strict;
8968 int sub_strict;
8970 tree tempargs = copy_node (targs);
8971 int good = 0;
8973 if (TREE_CODE (arg) == ADDR_EXPR)
8974 arg = TREE_OPERAND (arg, 0);
8976 if (TREE_CODE (arg) == COMPONENT_REF)
8977 /* Handle `&x' where `x' is some static or non-static member
8978 function name. */
8979 arg = TREE_OPERAND (arg, 1);
8981 if (TREE_CODE (arg) == OFFSET_REF)
8982 arg = TREE_OPERAND (arg, 1);
8984 /* Strip baselink information. */
8985 if (BASELINK_P (arg))
8986 arg = BASELINK_FUNCTIONS (arg);
8988 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8990 /* If we got some explicit template args, we need to plug them into
8991 the affected templates before we try to unify, in case the
8992 explicit args will completely resolve the templates in question. */
8994 tree expl_subargs = TREE_OPERAND (arg, 1);
8995 arg = TREE_OPERAND (arg, 0);
8997 for (; arg; arg = OVL_NEXT (arg))
8999 tree fn = OVL_CURRENT (arg);
9000 tree subargs, elem;
9002 if (TREE_CODE (fn) != TEMPLATE_DECL)
9003 continue;
9005 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
9006 expl_subargs);
9007 if (subargs)
9009 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
9010 if (TREE_CODE (elem) == METHOD_TYPE)
9011 elem = build_ptrmemfunc_type (build_pointer_type (elem));
9012 good += try_one_overload (tparms, targs, tempargs, parm, elem,
9013 strict, sub_strict);
9017 else if (TREE_CODE (arg) == OVERLOAD
9018 || TREE_CODE (arg) == FUNCTION_DECL)
9020 for (; arg; arg = OVL_NEXT (arg))
9022 tree type = TREE_TYPE (OVL_CURRENT (arg));
9023 if (TREE_CODE (type) == METHOD_TYPE)
9024 type = build_ptrmemfunc_type (build_pointer_type (type));
9025 good += try_one_overload (tparms, targs, tempargs, parm,
9026 type,
9027 strict, sub_strict);
9030 else
9031 abort ();
9033 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9034 to function or pointer to member function argument if the set of
9035 overloaded functions does not contain function templates and at most
9036 one of a set of overloaded functions provides a unique match.
9038 So if we found multiple possibilities, we return success but don't
9039 deduce anything. */
9041 if (good == 1)
9043 int i = TREE_VEC_LENGTH (targs);
9044 for (; i--; )
9045 if (TREE_VEC_ELT (tempargs, i))
9046 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
9048 if (good)
9049 return 0;
9051 return 1;
9054 /* Subroutine of resolve_overloaded_unification; does deduction for a single
9055 overload. Fills TARGS with any deduced arguments, or error_mark_node if
9056 different overloads deduce different arguments for a given parm.
9057 Returns 1 on success. */
9059 static int
9060 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
9061 sub_strict)
9062 tree tparms, orig_targs, targs, parm, arg;
9063 unification_kind_t strict;
9064 int sub_strict;
9066 int nargs;
9067 tree tempargs;
9068 int i;
9070 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9071 to function or pointer to member function argument if the set of
9072 overloaded functions does not contain function templates and at most
9073 one of a set of overloaded functions provides a unique match.
9075 So if this is a template, just return success. */
9077 if (uses_template_parms (arg))
9078 return 1;
9080 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9082 /* We don't copy orig_targs for this because if we have already deduced
9083 some template args from previous args, unify would complain when we
9084 try to deduce a template parameter for the same argument, even though
9085 there isn't really a conflict. */
9086 nargs = TREE_VEC_LENGTH (targs);
9087 tempargs = make_tree_vec (nargs);
9089 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
9090 return 0;
9092 /* First make sure we didn't deduce anything that conflicts with
9093 explicitly specified args. */
9094 for (i = nargs; i--; )
9096 tree elt = TREE_VEC_ELT (tempargs, i);
9097 tree oldelt = TREE_VEC_ELT (orig_targs, i);
9099 if (elt == NULL_TREE)
9100 continue;
9101 else if (uses_template_parms (elt))
9103 /* Since we're unifying against ourselves, we will fill in template
9104 args used in the function parm list with our own template parms.
9105 Discard them. */
9106 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
9107 continue;
9109 else if (oldelt && ! template_args_equal (oldelt, elt))
9110 return 0;
9113 for (i = nargs; i--; )
9115 tree elt = TREE_VEC_ELT (tempargs, i);
9117 if (elt)
9118 TREE_VEC_ELT (targs, i) = elt;
9121 return 1;
9124 /* Verify that nondeduce template argument agrees with the type
9125 obtained from argument deduction. Return nonzero if the
9126 verification fails.
9128 For example:
9130 struct A { typedef int X; };
9131 template <class T, class U> struct C {};
9132 template <class T> struct C<T, typename T::X> {};
9134 Then with the instantiation `C<A, int>', we can deduce that
9135 `T' is `A' but unify () does not check whether `typename T::X'
9136 is `int'. This function ensure that they agree.
9138 TARGS, PARMS are the same as the arguments of unify.
9139 ARGS contains template arguments from all levels. */
9141 static int
9142 verify_class_unification (targs, parms, args)
9143 tree targs, parms, args;
9145 parms = tsubst (parms, add_outermost_template_args (args, targs),
9146 tf_none, NULL_TREE);
9147 if (parms == error_mark_node)
9148 return 1;
9150 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
9153 /* PARM is a template class (perhaps with unbound template
9154 parameters). ARG is a fully instantiated type. If ARG can be
9155 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9156 TARGS are as for unify. */
9158 static tree
9159 try_class_unification (tparms, targs, parm, arg)
9160 tree tparms;
9161 tree targs;
9162 tree parm;
9163 tree arg;
9165 tree copy_of_targs;
9167 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9168 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
9169 return NULL_TREE;
9171 /* We need to make a new template argument vector for the call to
9172 unify. If we used TARGS, we'd clutter it up with the result of
9173 the attempted unification, even if this class didn't work out.
9174 We also don't want to commit ourselves to all the unifications
9175 we've already done, since unification is supposed to be done on
9176 an argument-by-argument basis. In other words, consider the
9177 following pathological case:
9179 template <int I, int J, int K>
9180 struct S {};
9182 template <int I, int J>
9183 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9185 template <int I, int J, int K>
9186 void f(S<I, J, K>, S<I, I, I>);
9188 void g() {
9189 S<0, 0, 0> s0;
9190 S<0, 1, 2> s2;
9192 f(s0, s2);
9195 Now, by the time we consider the unification involving `s2', we
9196 already know that we must have `f<0, 0, 0>'. But, even though
9197 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9198 because there are two ways to unify base classes of S<0, 1, 2>
9199 with S<I, I, I>. If we kept the already deduced knowledge, we
9200 would reject the possibility I=1. */
9201 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
9203 /* If unification failed, we're done. */
9204 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9205 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
9206 return NULL_TREE;
9208 return arg;
9211 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9212 have already discovered to be satisfactory. ARG_BINFO is the binfo
9213 for the base class of ARG that we are currently examining. */
9215 static tree
9216 get_template_base_recursive (tparms, targs, parm,
9217 arg_binfo, rval, flags)
9218 tree tparms;
9219 tree targs;
9220 tree arg_binfo;
9221 tree rval;
9222 tree parm;
9223 int flags;
9225 tree binfos;
9226 int i, n_baselinks;
9227 tree arg = BINFO_TYPE (arg_binfo);
9229 if (!(flags & GTB_IGNORE_TYPE))
9231 tree r = try_class_unification (tparms, targs,
9232 parm, arg);
9234 /* If there is more than one satisfactory baseclass, then:
9236 [temp.deduct.call]
9238 If they yield more than one possible deduced A, the type
9239 deduction fails.
9241 applies. */
9242 if (r && rval && !same_type_p (r, rval))
9243 return error_mark_node;
9244 else if (r)
9245 rval = r;
9248 binfos = BINFO_BASETYPES (arg_binfo);
9249 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9251 /* Process base types. */
9252 for (i = 0; i < n_baselinks; i++)
9254 tree base_binfo = TREE_VEC_ELT (binfos, i);
9255 int this_virtual;
9257 /* Skip this base, if we've already seen it. */
9258 if (BINFO_MARKED (base_binfo))
9259 continue;
9261 this_virtual =
9262 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9264 /* When searching for a non-virtual, we cannot mark virtually
9265 found binfos. */
9266 if (! this_virtual)
9267 BINFO_MARKED (base_binfo) = 1;
9269 rval = get_template_base_recursive (tparms, targs,
9270 parm,
9271 base_binfo,
9272 rval,
9273 GTB_VIA_VIRTUAL * this_virtual);
9275 /* If we discovered more than one matching base class, we can
9276 stop now. */
9277 if (rval == error_mark_node)
9278 return error_mark_node;
9281 return rval;
9284 /* Given a template type PARM and a class type ARG, find the unique
9285 base type in ARG that is an instance of PARM. We do not examine
9286 ARG itself; only its base-classes. If there is no appropriate base
9287 class, return NULL_TREE. If there is more than one, return
9288 error_mark_node. PARM may be the type of a partial specialization,
9289 as well as a plain template type. Used by unify. */
9291 static tree
9292 get_template_base (tparms, targs, parm, arg)
9293 tree tparms;
9294 tree targs;
9295 tree parm;
9296 tree arg;
9298 tree rval;
9299 tree arg_binfo;
9301 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9303 arg_binfo = TYPE_BINFO (complete_type (arg));
9304 rval = get_template_base_recursive (tparms, targs,
9305 parm, arg_binfo,
9306 NULL_TREE,
9307 GTB_IGNORE_TYPE);
9309 /* Since get_template_base_recursive marks the bases classes, we
9310 must unmark them here. */
9311 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
9313 return rval;
9316 /* Returns the level of DECL, which declares a template parameter. */
9318 static int
9319 template_decl_level (decl)
9320 tree decl;
9322 switch (TREE_CODE (decl))
9324 case TYPE_DECL:
9325 case TEMPLATE_DECL:
9326 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9328 case PARM_DECL:
9329 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9331 default:
9332 abort ();
9333 return 0;
9337 /* Decide whether ARG can be unified with PARM, considering only the
9338 cv-qualifiers of each type, given STRICT as documented for unify.
9339 Returns nonzero iff the unification is OK on that basis.*/
9341 static int
9342 check_cv_quals_for_unify (strict, arg, parm)
9343 int strict;
9344 tree arg;
9345 tree parm;
9347 int arg_quals = cp_type_quals (arg);
9348 int parm_quals = cp_type_quals (parm);
9350 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
9352 /* If the cvr quals of parm will not unify with ARG, they'll be
9353 ignored in instantiation, so we have to do the same here. */
9354 if (TREE_CODE (arg) == REFERENCE_TYPE)
9355 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
9356 if (!POINTER_TYPE_P (arg) &&
9357 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9358 parm_quals &= ~TYPE_QUAL_RESTRICT;
9361 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
9362 && (arg_quals & parm_quals) != parm_quals)
9363 return 0;
9365 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
9366 && (parm_quals & arg_quals) != arg_quals)
9367 return 0;
9369 return 1;
9372 /* Takes parameters as for type_unification. Returns 0 if the
9373 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9374 bitwise or of the following flags:
9376 UNIFY_ALLOW_NONE:
9377 Require an exact match between PARM and ARG.
9378 UNIFY_ALLOW_MORE_CV_QUAL:
9379 Allow the deduced ARG to be more cv-qualified (by qualification
9380 conversion) than ARG.
9381 UNIFY_ALLOW_LESS_CV_QUAL:
9382 Allow the deduced ARG to be less cv-qualified than ARG.
9383 UNIFY_ALLOW_DERIVED:
9384 Allow the deduced ARG to be a template base class of ARG,
9385 or a pointer to a template base class of the type pointed to by
9386 ARG.
9387 UNIFY_ALLOW_INTEGER:
9388 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9389 case for more information.
9390 UNIFY_ALLOW_OUTER_LEVEL:
9391 This is the outermost level of a deduction. Used to determine validity
9392 of qualification conversions. A valid qualification conversion must
9393 have const qualified pointers leading up to the inner type which
9394 requires additional CV quals, except at the outer level, where const
9395 is not required [conv.qual]. It would be normal to set this flag in
9396 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9397 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9398 This is the outermost level of a deduction, and PARM can be more CV
9399 qualified at this point.
9400 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9401 This is the outermost level of a deduction, and PARM can be less CV
9402 qualified at this point.
9403 UNIFY_ALLOW_MAX_CORRECTION:
9404 This is an INTEGER_TYPE's maximum value. Used if the range may
9405 have been derived from a size specification, such as an array size.
9406 If the size was given by a nontype template parameter N, the maximum
9407 value will have the form N-1. The flag says that we can (and indeed
9408 must) unify N with (ARG + 1), an exception to the normal rules on
9409 folding PARM. */
9411 static int
9412 unify (tparms, targs, parm, arg, strict)
9413 tree tparms, targs, parm, arg;
9414 int strict;
9416 int idx;
9417 tree targ;
9418 tree tparm;
9419 int strict_in = strict;
9421 /* I don't think this will do the right thing with respect to types.
9422 But the only case I've seen it in so far has been array bounds, where
9423 signedness is the only information lost, and I think that will be
9424 okay. */
9425 while (TREE_CODE (parm) == NOP_EXPR)
9426 parm = TREE_OPERAND (parm, 0);
9428 if (arg == error_mark_node)
9429 return 1;
9430 if (arg == unknown_type_node)
9431 /* We can't deduce anything from this, but we might get all the
9432 template args from other function args. */
9433 return 0;
9435 /* If PARM uses template parameters, then we can't bail out here,
9436 even if ARG == PARM, since we won't record unifications for the
9437 template parameters. We might need them if we're trying to
9438 figure out which of two things is more specialized. */
9439 if (arg == parm && !uses_template_parms (parm))
9440 return 0;
9442 /* Immediately reject some pairs that won't unify because of
9443 cv-qualification mismatches. */
9444 if (TREE_CODE (arg) == TREE_CODE (parm)
9445 && TYPE_P (arg)
9446 /* It is the elements of the array which hold the cv quals of an array
9447 type, and the elements might be template type parms. We'll check
9448 when we recurse. */
9449 && TREE_CODE (arg) != ARRAY_TYPE
9450 /* We check the cv-qualifiers when unifying with template type
9451 parameters below. We want to allow ARG `const T' to unify with
9452 PARM `T' for example, when computing which of two templates
9453 is more specialized, for example. */
9454 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
9455 && !check_cv_quals_for_unify (strict_in, arg, parm))
9456 return 1;
9458 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
9459 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
9460 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9461 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9462 strict &= ~UNIFY_ALLOW_DERIVED;
9463 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9464 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
9465 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
9467 switch (TREE_CODE (parm))
9469 case TYPENAME_TYPE:
9470 case SCOPE_REF:
9471 case UNBOUND_CLASS_TEMPLATE:
9472 /* In a type which contains a nested-name-specifier, template
9473 argument values cannot be deduced for template parameters used
9474 within the nested-name-specifier. */
9475 return 0;
9477 case TEMPLATE_TYPE_PARM:
9478 case TEMPLATE_TEMPLATE_PARM:
9479 case BOUND_TEMPLATE_TEMPLATE_PARM:
9480 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9482 if (TEMPLATE_TYPE_LEVEL (parm)
9483 != template_decl_level (tparm))
9484 /* The PARM is not one we're trying to unify. Just check
9485 to see if it matches ARG. */
9486 return (TREE_CODE (arg) == TREE_CODE (parm)
9487 && same_type_p (parm, arg)) ? 0 : 1;
9488 idx = TEMPLATE_TYPE_IDX (parm);
9489 targ = TREE_VEC_ELT (targs, idx);
9490 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
9492 /* Check for mixed types and values. */
9493 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9494 && TREE_CODE (tparm) != TYPE_DECL)
9495 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9496 && TREE_CODE (tparm) != TEMPLATE_DECL))
9497 return 1;
9499 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9501 /* ARG must be constructed from a template class or a template
9502 template parameter. */
9503 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9504 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
9505 return 1;
9508 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9509 tree parmvec = TYPE_TI_ARGS (parm);
9510 tree argvec = TYPE_TI_ARGS (arg);
9511 tree argtmplvec
9512 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
9513 int i;
9515 /* The parameter and argument roles have to be switched here
9516 in order to handle default arguments properly. For example,
9517 template<template <class> class TT> void f(TT<int>)
9518 should be able to accept vector<int> which comes from
9519 template <class T, class Allocator = allocator>
9520 class vector. */
9522 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9523 == error_mark_node)
9524 return 1;
9526 /* Deduce arguments T, i from TT<T> or TT<i>.
9527 We check each element of PARMVEC and ARGVEC individually
9528 rather than the whole TREE_VEC since they can have
9529 different number of elements. */
9531 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9533 tree t = TREE_VEC_ELT (parmvec, i);
9535 if (unify (tparms, targs, t,
9536 TREE_VEC_ELT (argvec, i),
9537 UNIFY_ALLOW_NONE))
9538 return 1;
9541 arg = TYPE_TI_TEMPLATE (arg);
9543 /* Fall through to deduce template name. */
9546 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9547 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9549 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9551 /* Simple cases: Value already set, does match or doesn't. */
9552 if (targ != NULL_TREE && template_args_equal (targ, arg))
9553 return 0;
9554 else if (targ)
9555 return 1;
9557 else
9559 /* If ARG is an offset type, we're trying to unify '*T' with
9560 'U C::*', which is ill-formed. See the comment in the
9561 POINTER_TYPE case about this ugliness. */
9562 if (TREE_CODE (arg) == OFFSET_TYPE)
9563 return 1;
9565 /* If PARM is `const T' and ARG is only `int', we don't have
9566 a match unless we are allowing additional qualification.
9567 If ARG is `const int' and PARM is just `T' that's OK;
9568 that binds `const int' to `T'. */
9569 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
9570 arg, parm))
9571 return 1;
9573 /* Consider the case where ARG is `const volatile int' and
9574 PARM is `const T'. Then, T should be `volatile int'. */
9575 arg = cp_build_qualified_type_real
9576 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
9577 if (arg == error_mark_node)
9578 return 1;
9580 /* Simple cases: Value already set, does match or doesn't. */
9581 if (targ != NULL_TREE && same_type_p (targ, arg))
9582 return 0;
9583 else if (targ)
9584 return 1;
9586 /* Make sure that ARG is not a variable-sized array. (Note
9587 that were talking about variable-sized arrays (like
9588 `int[n]'), rather than arrays of unknown size (like
9589 `int[]').) We'll get very confused by such a type since
9590 the bound of the array will not be computable in an
9591 instantiation. Besides, such types are not allowed in
9592 ISO C++, so we can do as we please here. */
9593 if (variably_modified_type_p (arg))
9594 return 1;
9597 TREE_VEC_ELT (targs, idx) = arg;
9598 return 0;
9600 case TEMPLATE_PARM_INDEX:
9601 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9603 if (TEMPLATE_PARM_LEVEL (parm)
9604 != template_decl_level (tparm))
9605 /* The PARM is not one we're trying to unify. Just check
9606 to see if it matches ARG. */
9607 return (TREE_CODE (arg) == TREE_CODE (parm)
9608 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
9610 idx = TEMPLATE_PARM_IDX (parm);
9611 targ = TREE_VEC_ELT (targs, idx);
9613 if (targ)
9615 int i = (cp_tree_equal (targ, arg) > 0);
9616 if (i == 1)
9617 return 0;
9618 else if (i == 0)
9619 return 1;
9620 else
9621 abort ();
9624 /* [temp.deduct.type] If, in the declaration of a function template
9625 with a non-type template-parameter, the non-type
9626 template-parameter is used in an expression in the function
9627 parameter-list and, if the corresponding template-argument is
9628 deduced, the template-argument type shall match the type of the
9629 template-parameter exactly, except that a template-argument
9630 deduced from an array bound may be of any integral type.
9631 The non-type parameter might use already deduced type parameters. */
9632 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
9633 if (!TREE_TYPE (arg))
9634 /* Template-parameter dependent expression. Just accept it for now.
9635 It will later be processed in convert_template_argument. */
9637 else if (same_type_p (TREE_TYPE (arg), tparm))
9638 /* OK */;
9639 else if ((strict & UNIFY_ALLOW_INTEGER)
9640 && (TREE_CODE (tparm) == INTEGER_TYPE
9641 || TREE_CODE (tparm) == BOOLEAN_TYPE))
9642 /* OK */;
9643 else if (uses_template_parms (tparm))
9644 /* We haven't deduced the type of this parameter yet. Try again
9645 later. */
9646 return 0;
9647 else
9648 return 1;
9650 TREE_VEC_ELT (targs, idx) = arg;
9651 return 0;
9653 case POINTER_TYPE:
9655 if (TREE_CODE (arg) != POINTER_TYPE)
9656 return 1;
9658 /* [temp.deduct.call]
9660 A can be another pointer or pointer to member type that can
9661 be converted to the deduced A via a qualification
9662 conversion (_conv.qual_).
9664 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9665 This will allow for additional cv-qualification of the
9666 pointed-to types if appropriate. */
9668 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
9669 /* The derived-to-base conversion only persists through one
9670 level of pointers. */
9671 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
9673 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
9674 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
9676 /* Avoid getting confused about cv-quals; don't recurse here.
9677 Pointers to members should really be just OFFSET_TYPE, not
9678 this two-level nonsense... */
9680 parm = TREE_TYPE (parm);
9681 arg = TREE_TYPE (arg);
9682 goto offset;
9685 return unify (tparms, targs, TREE_TYPE (parm),
9686 TREE_TYPE (arg), strict);
9689 case REFERENCE_TYPE:
9690 if (TREE_CODE (arg) != REFERENCE_TYPE)
9691 return 1;
9692 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9693 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9695 case ARRAY_TYPE:
9696 if (TREE_CODE (arg) != ARRAY_TYPE)
9697 return 1;
9698 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9699 != (TYPE_DOMAIN (arg) == NULL_TREE))
9700 return 1;
9701 if (TYPE_DOMAIN (parm) != NULL_TREE
9702 && unify (tparms, targs, TYPE_DOMAIN (parm),
9703 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
9704 return 1;
9705 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9706 strict & UNIFY_ALLOW_MORE_CV_QUAL);
9708 case REAL_TYPE:
9709 case COMPLEX_TYPE:
9710 case VECTOR_TYPE:
9711 case INTEGER_TYPE:
9712 case BOOLEAN_TYPE:
9713 case VOID_TYPE:
9714 if (TREE_CODE (arg) != TREE_CODE (parm))
9715 return 1;
9717 if (TREE_CODE (parm) == INTEGER_TYPE
9718 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
9720 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
9721 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
9722 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
9723 return 1;
9724 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
9725 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
9726 TYPE_MAX_VALUE (arg),
9727 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
9728 return 1;
9730 /* We have already checked cv-qualification at the top of the
9731 function. */
9732 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
9733 return 1;
9735 /* As far as unification is concerned, this wins. Later checks
9736 will invalidate it if necessary. */
9737 return 0;
9739 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9740 /* Type INTEGER_CST can come from ordinary constant template args. */
9741 case INTEGER_CST:
9742 while (TREE_CODE (arg) == NOP_EXPR)
9743 arg = TREE_OPERAND (arg, 0);
9745 if (TREE_CODE (arg) != INTEGER_CST)
9746 return 1;
9747 return !tree_int_cst_equal (parm, arg);
9749 case TREE_VEC:
9751 int i;
9752 if (TREE_CODE (arg) != TREE_VEC)
9753 return 1;
9754 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9755 return 1;
9756 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
9757 if (unify (tparms, targs,
9758 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
9759 UNIFY_ALLOW_NONE))
9760 return 1;
9761 return 0;
9764 case RECORD_TYPE:
9765 case UNION_TYPE:
9766 if (TREE_CODE (arg) != TREE_CODE (parm))
9767 return 1;
9769 if (TYPE_PTRMEMFUNC_P (parm))
9771 if (!TYPE_PTRMEMFUNC_P (arg))
9772 return 1;
9774 return unify (tparms, targs,
9775 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9776 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9777 strict);
9780 if (CLASSTYPE_TEMPLATE_INFO (parm))
9782 tree t = NULL_TREE;
9784 if (strict_in & UNIFY_ALLOW_DERIVED)
9786 /* First, we try to unify the PARM and ARG directly. */
9787 t = try_class_unification (tparms, targs,
9788 parm, arg);
9790 if (!t)
9792 /* Fallback to the special case allowed in
9793 [temp.deduct.call]:
9795 If P is a class, and P has the form
9796 template-id, then A can be a derived class of
9797 the deduced A. Likewise, if P is a pointer to
9798 a class of the form template-id, A can be a
9799 pointer to a derived class pointed to by the
9800 deduced A. */
9801 t = get_template_base (tparms, targs,
9802 parm, arg);
9804 if (! t || t == error_mark_node)
9805 return 1;
9808 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9809 && (CLASSTYPE_TI_TEMPLATE (parm)
9810 == CLASSTYPE_TI_TEMPLATE (arg)))
9811 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9812 Then, we should unify `int' and `U'. */
9813 t = arg;
9814 else
9815 /* There's no chance of unification succeeding. */
9816 return 1;
9818 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
9819 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
9821 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
9822 return 1;
9823 return 0;
9825 case METHOD_TYPE:
9826 case FUNCTION_TYPE:
9827 if (TREE_CODE (arg) != TREE_CODE (parm))
9828 return 1;
9830 if (unify (tparms, targs, TREE_TYPE (parm),
9831 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
9832 return 1;
9833 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
9834 TYPE_ARG_TYPES (arg), 1,
9835 DEDUCE_EXACT, 0, -1);
9837 case OFFSET_TYPE:
9838 offset:
9839 if (TREE_CODE (arg) != OFFSET_TYPE)
9840 return 1;
9841 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
9842 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
9843 return 1;
9844 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9845 strict);
9847 case CONST_DECL:
9848 if (DECL_TEMPLATE_PARM_P (parm))
9849 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
9850 if (arg != decl_constant_value (parm))
9851 return 1;
9852 return 0;
9854 case TEMPLATE_DECL:
9855 /* Matched cases are handled by the ARG == PARM test above. */
9856 return 1;
9858 case MINUS_EXPR:
9859 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9860 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
9862 /* We handle this case specially, since it comes up with
9863 arrays. In particular, something like:
9865 template <int N> void f(int (&x)[N]);
9867 Here, we are trying to unify the range type, which
9868 looks like [0 ... (N - 1)]. */
9869 tree t, t1, t2;
9870 t1 = TREE_OPERAND (parm, 0);
9871 t2 = TREE_OPERAND (parm, 1);
9873 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
9875 return unify (tparms, targs, t1, t, strict);
9877 /* else fall through */
9879 default:
9880 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
9883 /* We're looking at an expression. This can happen with
9884 something like:
9886 template <int I>
9887 void foo(S<I>, S<I + 2>);
9889 This is a "nondeduced context":
9891 [deduct.type]
9893 The nondeduced contexts are:
9895 --A type that is a template-id in which one or more of
9896 the template-arguments is an expression that references
9897 a template-parameter.
9899 In these cases, we assume deduction succeeded, but don't
9900 actually infer any unifications. */
9902 if (!uses_template_parms (parm)
9903 && !template_args_equal (parm, arg))
9904 return 1;
9905 else
9906 return 0;
9908 else
9909 sorry ("use of `%s' in template type unification",
9910 tree_code_name [(int) TREE_CODE (parm)]);
9912 return 1;
9916 /* Called if RESULT is explicitly instantiated, or is a member of an
9917 explicitly instantiated class, or if using -frepo and the
9918 instantiation of RESULT has been assigned to this file. */
9920 void
9921 mark_decl_instantiated (result, extern_p)
9922 tree result;
9923 int extern_p;
9925 /* We used to set this unconditionally; we moved that to
9926 do_decl_instantiation so it wouldn't get set on members of
9927 explicit class template instantiations. But we still need to set
9928 it here for the 'extern template' case in order to suppress
9929 implicit instantiations. */
9930 if (extern_p)
9931 SET_DECL_EXPLICIT_INSTANTIATION (result);
9933 /* If this entity has already been written out, it's too late to
9934 make any modifications. */
9935 if (TREE_ASM_WRITTEN (result))
9936 return;
9938 if (TREE_CODE (result) != FUNCTION_DECL)
9939 /* The TREE_PUBLIC flag for function declarations will have been
9940 set correctly by tsubst. */
9941 TREE_PUBLIC (result) = 1;
9943 if (! extern_p)
9945 DECL_INTERFACE_KNOWN (result) = 1;
9946 DECL_NOT_REALLY_EXTERN (result) = 1;
9948 /* Always make artificials weak. */
9949 if (DECL_ARTIFICIAL (result) && flag_weak)
9950 comdat_linkage (result);
9951 /* For WIN32 we also want to put explicit instantiations in
9952 linkonce sections. */
9953 else if (TREE_PUBLIC (result))
9954 maybe_make_one_only (result);
9957 if (TREE_CODE (result) == FUNCTION_DECL)
9958 defer_fn (result);
9961 /* Given two function templates PAT1 and PAT2, return:
9963 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9965 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9966 -1 if PAT2 is more specialized than PAT1.
9967 0 if neither is more specialized.
9969 LEN is passed through to fn_type_unification. */
9972 more_specialized (pat1, pat2, deduce, len)
9973 tree pat1, pat2;
9974 int deduce;
9975 int len;
9977 tree targs;
9978 int winner = 0;
9980 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9981 NULL_TREE, 0, deduce, len);
9982 if (targs)
9983 --winner;
9985 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9986 NULL_TREE, 0, deduce, len);
9987 if (targs)
9988 ++winner;
9990 return winner;
9993 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9995 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9996 -1 if PAT2 is more specialized than PAT1.
9997 0 if neither is more specialized. */
10000 more_specialized_class (pat1, pat2)
10001 tree pat1, pat2;
10003 tree targs;
10004 int winner = 0;
10006 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
10007 TREE_PURPOSE (pat2));
10008 if (targs)
10009 --winner;
10011 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
10012 TREE_PURPOSE (pat1));
10013 if (targs)
10014 ++winner;
10016 return winner;
10019 /* Return the template arguments that will produce the function signature
10020 DECL from the function template FN, with the explicit template
10021 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
10022 also match. Return NULL_TREE if no satisfactory arguments could be
10023 found. DEDUCE and LEN are passed through to fn_type_unification. */
10025 static tree
10026 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
10027 tree fn, decl, explicit_args;
10028 int check_rettype, deduce, len;
10030 int ntparms = DECL_NTPARMS (fn);
10031 tree targs = make_tree_vec (ntparms);
10032 tree decl_type;
10033 tree decl_arg_types;
10034 int i;
10036 /* Substitute the explicit template arguments into the type of DECL.
10037 The call to fn_type_unification will handle substitution into the
10038 FN. */
10039 decl_type = TREE_TYPE (decl);
10040 if (explicit_args && uses_template_parms (decl_type))
10042 tree tmpl;
10043 tree converted_args;
10045 if (DECL_TEMPLATE_INFO (decl))
10046 tmpl = DECL_TI_TEMPLATE (decl);
10047 else
10048 /* We can get here for some invalid specializations. */
10049 return NULL_TREE;
10051 converted_args
10052 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
10053 explicit_args, NULL_TREE,
10054 tf_none, /*require_all_arguments=*/0));
10055 if (converted_args == error_mark_node)
10056 return NULL_TREE;
10058 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
10059 if (decl_type == error_mark_node)
10060 return NULL_TREE;
10063 decl_arg_types = TYPE_ARG_TYPES (decl_type);
10064 /* Never do unification on the 'this' parameter. */
10065 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
10066 decl_arg_types = TREE_CHAIN (decl_arg_types);
10068 i = fn_type_unification (fn, explicit_args, targs,
10069 decl_arg_types,
10070 (check_rettype || DECL_CONV_FN_P (fn)
10071 ? TREE_TYPE (decl_type) : NULL_TREE),
10072 deduce, len);
10074 if (i != 0)
10075 return NULL_TREE;
10077 return targs;
10080 /* For most uses, we want to check the return type. */
10082 tree
10083 get_bindings (fn, decl, explicit_args)
10084 tree fn, decl, explicit_args;
10086 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
10089 /* But for resolve_overloaded_unification, we only care about the parameter
10090 types. */
10092 static tree
10093 get_bindings_overload (fn, decl, explicit_args)
10094 tree fn, decl, explicit_args;
10096 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
10099 /* Return the innermost template arguments that, when applied to a
10100 template specialization whose innermost template parameters are
10101 TPARMS, and whose specialization arguments are PARMS, yield the
10102 ARGS.
10104 For example, suppose we have:
10106 template <class T, class U> struct S {};
10107 template <class T> struct S<T*, int> {};
10109 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10110 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10111 int}. The resulting vector will be {double}, indicating that `T'
10112 is bound to `double'. */
10114 static tree
10115 get_class_bindings (tparms, parms, args)
10116 tree tparms, parms, args;
10118 int i, ntparms = TREE_VEC_LENGTH (tparms);
10119 tree vec = make_tree_vec (ntparms);
10121 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10122 UNIFY_ALLOW_NONE))
10123 return NULL_TREE;
10125 for (i = 0; i < ntparms; ++i)
10126 if (! TREE_VEC_ELT (vec, i))
10127 return NULL_TREE;
10129 if (verify_class_unification (vec, parms, args))
10130 return NULL_TREE;
10132 return vec;
10135 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10136 Pick the most specialized template, and return the corresponding
10137 instantiation, or if there is no corresponding instantiation, the
10138 template itself. If there is no most specialized template,
10139 error_mark_node is returned. If there are no templates at all,
10140 NULL_TREE is returned. */
10142 tree
10143 most_specialized_instantiation (instantiations)
10144 tree instantiations;
10146 tree fn, champ;
10147 int fate;
10149 if (!instantiations)
10150 return NULL_TREE;
10152 champ = instantiations;
10153 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
10155 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10156 DEDUCE_EXACT, -1);
10157 if (fate == 1)
10159 else
10161 if (fate == 0)
10163 fn = TREE_CHAIN (fn);
10164 if (! fn)
10165 return error_mark_node;
10167 champ = fn;
10171 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
10173 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10174 DEDUCE_EXACT, -1);
10175 if (fate != 1)
10176 return error_mark_node;
10179 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10182 /* Return the most specialized of the list of templates in FNS that can
10183 produce an instantiation matching DECL, given the explicit template
10184 arguments EXPLICIT_ARGS. */
10186 static tree
10187 most_specialized (fns, decl, explicit_args)
10188 tree fns, decl, explicit_args;
10190 tree candidates = NULL_TREE;
10191 tree fn, args;
10193 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10195 tree candidate = TREE_VALUE (fn);
10197 args = get_bindings (candidate, decl, explicit_args);
10198 if (args)
10199 candidates = tree_cons (NULL_TREE, candidate, candidates);
10202 return most_specialized_instantiation (candidates);
10205 /* If DECL is a specialization of some template, return the most
10206 general such template. Otherwise, returns NULL_TREE.
10208 For example, given:
10210 template <class T> struct S { template <class U> void f(U); };
10212 if TMPL is `template <class U> void S<int>::f(U)' this will return
10213 the full template. This function will not trace past partial
10214 specializations, however. For example, given in addition:
10216 template <class T> struct S<T*> { template <class U> void f(U); };
10218 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10219 `template <class T> template <class U> S<T*>::f(U)'. */
10221 tree
10222 most_general_template (decl)
10223 tree decl;
10225 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10226 an immediate specialization. */
10227 if (TREE_CODE (decl) == FUNCTION_DECL)
10229 if (DECL_TEMPLATE_INFO (decl)) {
10230 decl = DECL_TI_TEMPLATE (decl);
10232 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10233 template friend. */
10234 if (TREE_CODE (decl) != TEMPLATE_DECL)
10235 return NULL_TREE;
10236 } else
10237 return NULL_TREE;
10240 /* Look for more and more general templates. */
10241 while (DECL_TEMPLATE_INFO (decl))
10243 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10244 in some cases. (See cp-tree.h for details.) */
10245 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10246 break;
10248 if (CLASS_TYPE_P (TREE_TYPE (decl))
10249 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
10250 break;
10252 /* Stop if we run into an explicitly specialized class template. */
10253 if (!DECL_NAMESPACE_SCOPE_P (decl)
10254 && DECL_CONTEXT (decl)
10255 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10256 break;
10258 decl = DECL_TI_TEMPLATE (decl);
10261 return decl;
10264 /* Return the most specialized of the class template specializations
10265 of TMPL which can produce an instantiation matching ARGS, or
10266 error_mark_node if the choice is ambiguous. */
10268 static tree
10269 most_specialized_class (tmpl, args)
10270 tree tmpl;
10271 tree args;
10273 tree list = NULL_TREE;
10274 tree t;
10275 tree champ;
10276 int fate;
10278 tmpl = most_general_template (tmpl);
10279 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
10281 tree spec_args
10282 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10283 if (spec_args)
10285 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
10286 TREE_TYPE (list) = TREE_TYPE (t);
10290 if (! list)
10291 return NULL_TREE;
10293 t = list;
10294 champ = t;
10295 t = TREE_CHAIN (t);
10296 for (; t; t = TREE_CHAIN (t))
10298 fate = more_specialized_class (champ, t);
10299 if (fate == 1)
10301 else
10303 if (fate == 0)
10305 t = TREE_CHAIN (t);
10306 if (! t)
10307 return error_mark_node;
10309 champ = t;
10313 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10315 fate = more_specialized_class (champ, t);
10316 if (fate != 1)
10317 return error_mark_node;
10320 return champ;
10323 /* Explicitly instantiate DECL. */
10325 void
10326 do_decl_instantiation (tree decl, tree storage)
10328 tree result = NULL_TREE;
10329 int extern_p = 0;
10331 if (!decl)
10332 /* An error occurred, for which grokdeclarator has already issued
10333 an appropriate message. */
10334 return;
10335 else if (! DECL_LANG_SPECIFIC (decl))
10337 error ("explicit instantiation of non-template `%#D'", decl);
10338 return;
10340 else if (TREE_CODE (decl) == VAR_DECL)
10342 /* There is an asymmetry here in the way VAR_DECLs and
10343 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10344 the latter, the DECL we get back will be marked as a
10345 template instantiation, and the appropriate
10346 DECL_TEMPLATE_INFO will be set up. This does not happen for
10347 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10348 should handle VAR_DECLs as it currently handles
10349 FUNCTION_DECLs. */
10350 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
10351 if (!result || TREE_CODE (result) != VAR_DECL)
10353 error ("no matching template for `%D' found", decl);
10354 return;
10357 else if (TREE_CODE (decl) != FUNCTION_DECL)
10359 error ("explicit instantiation of `%#D'", decl);
10360 return;
10362 else
10363 result = decl;
10365 /* Check for various error cases. Note that if the explicit
10366 instantiation is valid the RESULT will currently be marked as an
10367 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10368 until we get here. */
10370 if (DECL_TEMPLATE_SPECIALIZATION (result))
10372 /* DR 259 [temp.spec].
10374 Both an explicit instantiation and a declaration of an explicit
10375 specialization shall not appear in a program unless the explicit
10376 instantiation follows a declaration of the explicit specialization.
10378 For a given set of template parameters, if an explicit
10379 instantiation of a template appears after a declaration of an
10380 explicit specialization for that template, the explicit
10381 instantiation has no effect. */
10382 return;
10384 else if (DECL_EXPLICIT_INSTANTIATION (result))
10386 /* [temp.spec]
10388 No program shall explicitly instantiate any template more
10389 than once.
10391 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10392 instantiation was `extern' and the second is not, and EXTERN_P for
10393 the opposite case. If -frepo, chances are we already got marked
10394 as an explicit instantiation because of the repo file. */
10395 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
10396 pedwarn ("duplicate explicit instantiation of `%#D'", result);
10398 /* If we've already instantiated the template, just return now. */
10399 if (DECL_INTERFACE_KNOWN (result))
10400 return;
10402 else if (!DECL_IMPLICIT_INSTANTIATION (result))
10404 error ("no matching template for `%D' found", result);
10405 return;
10407 else if (!DECL_TEMPLATE_INFO (result))
10409 pedwarn ("explicit instantiation of non-template `%#D'", result);
10410 return;
10413 if (flag_external_templates)
10414 return;
10416 if (storage == NULL_TREE)
10418 else if (storage == ridpointers[(int) RID_EXTERN])
10420 if (pedantic && !in_system_header)
10421 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10422 extern_p = 1;
10424 else
10425 error ("storage class `%D' applied to template instantiation",
10426 storage);
10428 SET_DECL_EXPLICIT_INSTANTIATION (result);
10429 mark_decl_instantiated (result, extern_p);
10430 repo_template_instantiated (result, extern_p);
10431 if (! extern_p)
10432 instantiate_decl (result, /*defer_ok=*/1);
10435 void
10436 mark_class_instantiated (t, extern_p)
10437 tree t;
10438 int extern_p;
10440 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
10441 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10442 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
10443 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10444 if (! extern_p)
10446 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10447 rest_of_type_compilation (t, 1);
10451 /* Perform an explicit instantiation of template class T. STORAGE, if
10452 non-null, is the RID for extern, inline or static. COMPLAIN is
10453 nonzero if this is called from the parser, zero if called recursively,
10454 since the standard is unclear (as detailed below). */
10456 void
10457 do_type_instantiation (t, storage, complain)
10458 tree t, storage;
10459 tsubst_flags_t complain;
10461 int extern_p = 0;
10462 int nomem_p = 0;
10463 int static_p = 0;
10465 if (TREE_CODE (t) == TYPE_DECL)
10466 t = TREE_TYPE (t);
10468 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
10470 error ("explicit instantiation of non-template type `%T'", t);
10471 return;
10474 complete_type (t);
10476 /* With -fexternal-templates, explicit instantiations are treated the same
10477 as implicit ones. */
10478 if (flag_external_templates)
10479 return;
10481 if (!COMPLETE_TYPE_P (t))
10483 if (complain & tf_error)
10484 error ("explicit instantiation of `%#T' before definition of template",
10486 return;
10489 if (storage != NULL_TREE)
10491 if (pedantic && !in_system_header)
10492 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
10493 IDENTIFIER_POINTER (storage));
10495 if (storage == ridpointers[(int) RID_INLINE])
10496 nomem_p = 1;
10497 else if (storage == ridpointers[(int) RID_EXTERN])
10498 extern_p = 1;
10499 else if (storage == ridpointers[(int) RID_STATIC])
10500 static_p = 1;
10501 else
10503 error ("storage class `%D' applied to template instantiation",
10504 storage);
10505 extern_p = 0;
10509 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10511 /* DR 259 [temp.spec].
10513 Both an explicit instantiation and a declaration of an explicit
10514 specialization shall not appear in a program unless the explicit
10515 instantiation follows a declaration of the explicit specialization.
10517 For a given set of template parameters, if an explicit
10518 instantiation of a template appears after a declaration of an
10519 explicit specialization for that template, the explicit
10520 instantiation has no effect. */
10521 return;
10523 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
10525 /* [temp.spec]
10527 No program shall explicitly instantiate any template more
10528 than once.
10530 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10531 was `extern'. If EXTERN_P then the second is. If -frepo, chances
10532 are we already got marked as an explicit instantiation because of the
10533 repo file. All these cases are OK. */
10534 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
10535 && (complain & tf_error))
10536 pedwarn ("duplicate explicit instantiation of `%#T'", t);
10538 /* If we've already instantiated the template, just return now. */
10539 if (!CLASSTYPE_INTERFACE_ONLY (t))
10540 return;
10543 mark_class_instantiated (t, extern_p);
10544 repo_template_instantiated (t, extern_p);
10546 if (nomem_p)
10547 return;
10550 tree tmp;
10552 /* In contrast to implicit instantiation, where only the
10553 declarations, and not the definitions, of members are
10554 instantiated, we have here:
10556 [temp.explicit]
10558 The explicit instantiation of a class template specialization
10559 implies the instantiation of all of its members not
10560 previously explicitly specialized in the translation unit
10561 containing the explicit instantiation.
10563 Of course, we can't instantiate member template classes, since
10564 we don't have any arguments for them. Note that the standard
10565 is unclear on whether the instantiation of the members are
10566 *explicit* instantiations or not. We choose to be generous,
10567 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10568 the explicit instantiation of a class where some of the members
10569 have no definition in the current translation unit. */
10571 if (! static_p)
10572 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
10573 if (TREE_CODE (tmp) == FUNCTION_DECL
10574 && DECL_TEMPLATE_INSTANTIATION (tmp))
10576 mark_decl_instantiated (tmp, extern_p);
10577 repo_template_instantiated (tmp, extern_p);
10578 if (! extern_p)
10579 instantiate_decl (tmp, /*defer_ok=*/1);
10582 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10583 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
10585 mark_decl_instantiated (tmp, extern_p);
10586 repo_template_instantiated (tmp, extern_p);
10587 if (! extern_p)
10588 instantiate_decl (tmp, /*defer_ok=*/1);
10591 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
10592 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
10593 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
10594 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
10598 /* Given a function DECL, which is a specialization of TMPL, modify
10599 DECL to be a re-instantiation of TMPL with the same template
10600 arguments. TMPL should be the template into which tsubst'ing
10601 should occur for DECL, not the most general template.
10603 One reason for doing this is a scenario like this:
10605 template <class T>
10606 void f(const T&, int i);
10608 void g() { f(3, 7); }
10610 template <class T>
10611 void f(const T& t, const int i) { }
10613 Note that when the template is first instantiated, with
10614 instantiate_template, the resulting DECL will have no name for the
10615 first parameter, and the wrong type for the second. So, when we go
10616 to instantiate the DECL, we regenerate it. */
10618 static void
10619 regenerate_decl_from_template (decl, tmpl)
10620 tree decl;
10621 tree tmpl;
10623 /* The most general version of TMPL. */
10624 tree gen_tmpl;
10625 /* The arguments used to instantiate DECL, from the most general
10626 template. */
10627 tree args;
10628 tree code_pattern;
10629 tree new_decl;
10630 int unregistered;
10632 args = DECL_TI_ARGS (decl);
10633 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10635 /* Unregister the specialization so that when we tsubst we will not
10636 just return DECL. We don't have to unregister DECL from TMPL
10637 because if would only be registered there if it were a partial
10638 instantiation of a specialization, which it isn't: it's a full
10639 instantiation. */
10640 gen_tmpl = most_general_template (tmpl);
10641 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl));
10642 unregistered = unregister_specialization (decl, gen_tmpl);
10644 /* If the DECL was not unregistered then something peculiar is
10645 happening: we created a specialization but did not call
10646 register_specialization for it. */
10647 my_friendly_assert (unregistered, 0);
10649 /* Do the substitution to get the new declaration. */
10650 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
10652 if (TREE_CODE (decl) == VAR_DECL)
10654 /* Set up DECL_INITIAL, since tsubst doesn't. */
10655 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10656 DECL_INITIAL (new_decl) =
10657 tsubst_expr (DECL_INITIAL (code_pattern), args,
10658 tf_error, DECL_TI_TEMPLATE (decl));
10660 else if (TREE_CODE (decl) == FUNCTION_DECL)
10662 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10663 new decl. */
10664 DECL_INITIAL (new_decl) = error_mark_node;
10665 /* And don't complain about a duplicate definition. */
10666 DECL_INITIAL (decl) = NULL_TREE;
10669 pop_access_scope (decl);
10671 /* The immediate parent of the new template is still whatever it was
10672 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10673 general template. We also reset the DECL_ASSEMBLER_NAME since
10674 tsubst always calculates the name as if the function in question
10675 were really a template instance, and sometimes, with friend
10676 functions, this is not so. See tsubst_friend_function for
10677 details. */
10678 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
10679 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
10680 COPY_DECL_RTL (decl, new_decl);
10681 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
10683 /* Call duplicate decls to merge the old and new declarations. */
10684 duplicate_decls (new_decl, decl);
10686 /* Now, re-register the specialization. */
10687 register_specialization (decl, gen_tmpl, args);
10690 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10691 substituted to get DECL. */
10693 static tree
10694 template_for_substitution (tree decl)
10696 tree tmpl = DECL_TI_TEMPLATE (decl);
10698 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10699 for the instantiation. This is not always the most general
10700 template. Consider, for example:
10702 template <class T>
10703 struct S { template <class U> void f();
10704 template <> void f<int>(); };
10706 and an instantiation of S<double>::f<int>. We want TD to be the
10707 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10708 while (/* An instantiation cannot have a definition, so we need a
10709 more general template. */
10710 DECL_TEMPLATE_INSTANTIATION (tmpl)
10711 /* We must also deal with friend templates. Given:
10713 template <class T> struct S {
10714 template <class U> friend void f() {};
10717 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10718 so far as the language is concerned, but that's still
10719 where we get the pattern for the instantiation from. On
10720 other hand, if the definition comes outside the class, say:
10722 template <class T> struct S {
10723 template <class U> friend void f();
10725 template <class U> friend void f() {}
10727 we don't need to look any further. That's what the check for
10728 DECL_INITIAL is for. */
10729 || (TREE_CODE (decl) == FUNCTION_DECL
10730 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10731 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10733 /* The present template, TD, should not be a definition. If it
10734 were a definition, we should be using it! Note that we
10735 cannot restructure the loop to just keep going until we find
10736 a template with a definition, since that might go too far if
10737 a specialization was declared, but not defined. */
10738 my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL
10739 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))),
10740 0);
10742 /* Fetch the more general template. */
10743 tmpl = DECL_TI_TEMPLATE (tmpl);
10746 return tmpl;
10749 /* Produce the definition of D, a _DECL generated from a template. If
10750 DEFER_OK is nonzero, then we don't have to actually do the
10751 instantiation now; we just have to do it sometime. */
10753 tree
10754 instantiate_decl (d, defer_ok)
10755 tree d;
10756 int defer_ok;
10758 tree tmpl = DECL_TI_TEMPLATE (d);
10759 tree gen_args;
10760 tree args;
10761 tree td;
10762 tree code_pattern;
10763 tree spec;
10764 tree gen_tmpl;
10765 int pattern_defined;
10766 int need_push;
10767 location_t saved_loc = input_location;
10769 /* This function should only be used to instantiate templates for
10770 functions and static member variables. */
10771 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
10772 || TREE_CODE (d) == VAR_DECL, 0);
10774 /* Variables are never deferred; if instantiation is required, they
10775 are instantiated right away. That allows for better code in the
10776 case that an expression refers to the value of the variable --
10777 if the variable has a constant value the referring expression can
10778 take advantage of that fact. */
10779 if (TREE_CODE (d) == VAR_DECL)
10780 defer_ok = 0;
10782 /* Don't instantiate cloned functions. Instead, instantiate the
10783 functions they cloned. */
10784 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10785 d = DECL_CLONED_FUNCTION (d);
10787 if (DECL_TEMPLATE_INSTANTIATED (d))
10788 /* D has already been instantiated. It might seem reasonable to
10789 check whether or not D is an explicit instantiation, and, if so,
10790 stop here. But when an explicit instantiation is deferred
10791 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10792 is set, even though we still need to do the instantiation. */
10793 return d;
10795 /* If we already have a specialization of this declaration, then
10796 there's no reason to instantiate it. Note that
10797 retrieve_specialization gives us both instantiations and
10798 specializations, so we must explicitly check
10799 DECL_TEMPLATE_SPECIALIZATION. */
10800 gen_tmpl = most_general_template (tmpl);
10801 gen_args = DECL_TI_ARGS (d);
10802 spec = retrieve_specialization (gen_tmpl, gen_args);
10803 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10804 return spec;
10806 /* This needs to happen before any tsubsting. */
10807 if (! push_tinst_level (d))
10808 return d;
10810 timevar_push (TV_PARSE);
10812 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
10813 for the instantiation. */
10814 td = template_for_substitution (d);
10815 code_pattern = DECL_TEMPLATE_RESULT (td);
10817 if (DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
10818 /* In the case of a friend template whose definition is provided
10819 outside the class, we may have too many arguments. Drop the
10820 ones we don't need. */
10821 args = get_innermost_template_args
10822 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
10823 else
10824 args = gen_args;
10826 if (TREE_CODE (d) == FUNCTION_DECL)
10827 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
10828 else
10829 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
10831 input_location = DECL_SOURCE_LOCATION (d);
10833 if (pattern_defined)
10835 /* Let the repository code that this template definition is
10836 available.
10838 The repository doesn't need to know about cloned functions
10839 because they never actually show up in the object file. It
10840 does need to know about the clones; those are the symbols
10841 that the linker will be emitting error messages about. */
10842 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10843 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10845 tree t;
10847 for (t = TREE_CHAIN (d);
10848 t && DECL_CLONED_FUNCTION_P (t);
10849 t = TREE_CHAIN (t))
10850 repo_template_used (t);
10852 else
10853 repo_template_used (d);
10855 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10857 if (flag_alt_external_templates)
10859 if (interface_unknown)
10860 warn_if_unknown_interface (d);
10862 else if (DECL_INTERFACE_KNOWN (code_pattern))
10864 DECL_INTERFACE_KNOWN (d) = 1;
10865 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
10867 else
10868 warn_if_unknown_interface (code_pattern);
10871 if (at_eof)
10872 import_export_decl (d);
10875 if (!defer_ok)
10877 /* Recheck the substitutions to obtain any warning messages
10878 about ignoring cv qualifiers. */
10879 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10880 tree type = TREE_TYPE (gen);
10882 /* Make sure that we can see identifiers, and compute access
10883 correctly. D is already the target FUNCTION_DECL with the
10884 right context. */
10885 push_access_scope (d);
10887 if (TREE_CODE (gen) == FUNCTION_DECL)
10889 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
10890 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
10891 tf_error | tf_warning, d);
10892 /* Don't simply tsubst the function type, as that will give
10893 duplicate warnings about poor parameter qualifications.
10894 The function arguments are the same as the decl_arguments
10895 without the top level cv qualifiers. */
10896 type = TREE_TYPE (type);
10898 tsubst (type, gen_args, tf_error | tf_warning, d);
10900 pop_access_scope (d);
10903 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10904 && DECL_INITIAL (d) == NULL_TREE)
10905 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10906 abort ();
10907 /* Reject all external templates except inline functions. */
10908 else if (DECL_INTERFACE_KNOWN (d)
10909 && ! DECL_NOT_REALLY_EXTERN (d)
10910 && ! (TREE_CODE (d) == FUNCTION_DECL
10911 && DECL_INLINE (d)))
10912 goto out;
10913 /* Defer all other templates, unless we have been explicitly
10914 forbidden from doing so. We restore the source position here
10915 because it's used by add_pending_template. */
10916 else if (! pattern_defined || defer_ok)
10918 input_location = saved_loc;
10920 if (at_eof && !pattern_defined
10921 && DECL_EXPLICIT_INSTANTIATION (d))
10922 /* [temp.explicit]
10924 The definition of a non-exported function template, a
10925 non-exported member function template, or a non-exported
10926 member function or static data member of a class template
10927 shall be present in every translation unit in which it is
10928 explicitly instantiated. */
10929 pedwarn
10930 ("explicit instantiation of `%D' but no definition available", d);
10932 add_pending_template (d);
10933 goto out;
10936 need_push = !global_bindings_p ();
10937 if (need_push)
10938 push_to_top_level ();
10940 /* Regenerate the declaration in case the template has been modified
10941 by a subsequent redeclaration. */
10942 regenerate_decl_from_template (d, td);
10944 /* We already set the file and line above. Reset them now in case
10945 they changed as a result of calling
10946 regenerate_decl_from_template. */
10947 input_location = DECL_SOURCE_LOCATION (d);
10949 if (TREE_CODE (d) == VAR_DECL)
10951 /* Clear out DECL_RTL; whatever was there before may not be right
10952 since we've reset the type of the declaration. */
10953 SET_DECL_RTL (d, NULL_RTX);
10955 DECL_IN_AGGR_P (d) = 0;
10956 import_export_decl (d);
10957 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10959 if (DECL_EXTERNAL (d))
10961 /* The fact that this code is executing indicates that:
10963 (1) D is a template static data member, for which a
10964 definition is available.
10966 (2) An implicit or explicit instantiation has occured.
10968 (3) We are not going to emit a definition of the static
10969 data member at this time.
10971 This situation is peculiar, but it occurs on platforms
10972 without weak symbols when performing an implicit
10973 instantiation. There, we cannot implicitly instantiate a
10974 defined static data member in more than one translation
10975 unit, so import_export_decl marks the declaration as
10976 external; we must rely on explicit instantiation. */
10978 else
10980 /* Mark D as instantiated so that recursive calls to
10981 instantiate_decl do not try to instantiate it again. */
10982 DECL_TEMPLATE_INSTANTIATED (d) = 1;
10983 cp_finish_decl (d,
10984 (!DECL_INITIALIZED_IN_CLASS_P (d)
10985 ? DECL_INITIAL (d) : NULL_TREE),
10986 NULL_TREE, 0);
10989 else if (TREE_CODE (d) == FUNCTION_DECL)
10991 htab_t saved_local_specializations;
10992 tree subst_decl;
10993 tree tmpl_parm;
10994 tree spec_parm;
10996 /* Mark D as instantiated so that recursive calls to
10997 instantiate_decl do not try to instantiate it again. */
10998 DECL_TEMPLATE_INSTANTIATED (d) = 1;
11000 /* Save away the current list, in case we are instantiating one
11001 template from within the body of another. */
11002 saved_local_specializations = local_specializations;
11004 /* Set up the list of local specializations. */
11005 local_specializations = htab_create (37,
11006 hash_local_specialization,
11007 eq_local_specializations,
11008 NULL);
11010 /* Set up context. */
11011 import_export_decl (d);
11012 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
11014 /* Create substitution entries for the parameters. */
11015 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
11016 tmpl_parm = DECL_ARGUMENTS (subst_decl);
11017 spec_parm = DECL_ARGUMENTS (d);
11018 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
11020 register_local_specialization (spec_parm, tmpl_parm);
11021 spec_parm = skip_artificial_parms_for (d, spec_parm);
11022 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
11024 while (tmpl_parm)
11026 register_local_specialization (spec_parm, tmpl_parm);
11027 tmpl_parm = TREE_CHAIN (tmpl_parm);
11028 spec_parm = TREE_CHAIN (spec_parm);
11030 my_friendly_assert (!spec_parm, 20020813);
11032 /* Substitute into the body of the function. */
11033 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
11034 tf_error | tf_warning, tmpl);
11036 /* We don't need the local specializations any more. */
11037 htab_delete (local_specializations);
11038 local_specializations = saved_local_specializations;
11040 /* Finish the function. */
11041 d = finish_function (0);
11042 expand_body (d);
11045 /* We're not deferring instantiation any more. */
11046 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
11048 if (need_push)
11049 pop_from_top_level ();
11051 out:
11052 input_location = saved_loc;
11053 pop_tinst_level ();
11055 timevar_pop (TV_PARSE);
11057 return d;
11060 /* Run through the list of templates that we wish we could
11061 instantiate, and instantiate any we can. */
11064 instantiate_pending_templates ()
11066 tree *t;
11067 tree last = NULL_TREE;
11068 int instantiated_something = 0;
11069 int reconsider;
11073 reconsider = 0;
11075 t = &pending_templates;
11076 while (*t)
11078 tree instantiation = TREE_VALUE (*t);
11080 reopen_tinst_level (TREE_PURPOSE (*t));
11082 if (TYPE_P (instantiation))
11084 tree fn;
11086 if (!COMPLETE_TYPE_P (instantiation))
11088 instantiate_class_template (instantiation);
11089 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
11090 for (fn = TYPE_METHODS (instantiation);
11092 fn = TREE_CHAIN (fn))
11093 if (! DECL_ARTIFICIAL (fn))
11094 instantiate_decl (fn, /*defer_ok=*/0);
11095 if (COMPLETE_TYPE_P (instantiation))
11097 instantiated_something = 1;
11098 reconsider = 1;
11102 if (COMPLETE_TYPE_P (instantiation))
11103 /* If INSTANTIATION has been instantiated, then we don't
11104 need to consider it again in the future. */
11105 *t = TREE_CHAIN (*t);
11106 else
11108 last = *t;
11109 t = &TREE_CHAIN (*t);
11112 else
11114 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
11115 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
11117 instantiation = instantiate_decl (instantiation,
11118 /*defer_ok=*/0);
11119 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
11121 instantiated_something = 1;
11122 reconsider = 1;
11126 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
11127 || DECL_TEMPLATE_INSTANTIATED (instantiation))
11128 /* If INSTANTIATION has been instantiated, then we don't
11129 need to consider it again in the future. */
11130 *t = TREE_CHAIN (*t);
11131 else
11133 last = *t;
11134 t = &TREE_CHAIN (*t);
11137 tinst_depth = 0;
11138 current_tinst_level = NULL_TREE;
11140 last_pending_template = last;
11142 while (reconsider);
11144 return instantiated_something;
11147 /* Substitute ARGVEC into T, which is a list of initializers for
11148 either base class or a non-static data member. The TREE_PURPOSEs
11149 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11150 instantiate_decl. */
11152 static tree
11153 tsubst_initializer_list (t, argvec)
11154 tree t, argvec;
11156 tree inits = NULL_TREE;
11158 for (; t; t = TREE_CHAIN (t))
11160 tree decl;
11161 tree init;
11162 tree val;
11164 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
11165 NULL_TREE);
11166 decl = expand_member_init (decl);
11167 if (decl && !DECL_P (decl))
11168 in_base_initializer = 1;
11170 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
11171 NULL_TREE);
11172 if (!init)
11174 else if (TREE_CODE (init) == TREE_LIST)
11175 for (val = init; val; val = TREE_CHAIN (val))
11176 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
11177 else if (init != void_type_node)
11178 init = convert_from_reference (init);
11180 in_base_initializer = 0;
11182 if (decl)
11184 init = build_tree_list (decl, init);
11185 TREE_CHAIN (init) = inits;
11186 inits = init;
11189 return inits;
11192 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11194 static void
11195 set_current_access_from_decl (decl)
11196 tree decl;
11198 if (TREE_PRIVATE (decl))
11199 current_access_specifier = access_private_node;
11200 else if (TREE_PROTECTED (decl))
11201 current_access_specifier = access_protected_node;
11202 else
11203 current_access_specifier = access_public_node;
11206 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11207 is the instantiation (which should have been created with
11208 start_enum) and ARGS are the template arguments to use. */
11210 static void
11211 tsubst_enum (tag, newtag, args)
11212 tree tag;
11213 tree newtag;
11214 tree args;
11216 tree e;
11218 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11220 tree value;
11222 /* Note that in a template enum, the TREE_VALUE is the
11223 CONST_DECL, not the corresponding INTEGER_CST. */
11224 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
11225 args, tf_error | tf_warning,
11226 NULL_TREE);
11228 /* Give this enumeration constant the correct access. */
11229 set_current_access_from_decl (TREE_VALUE (e));
11231 /* Actually build the enumerator itself. */
11232 build_enumerator (TREE_PURPOSE (e), value, newtag);
11235 finish_enum (newtag);
11236 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11237 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
11240 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11241 its type -- but without substituting the innermost set of template
11242 arguments. So, innermost set of template parameters will appear in
11243 the type. */
11245 tree
11246 get_mostly_instantiated_function_type (decl)
11247 tree decl;
11249 tree fn_type;
11250 tree tmpl;
11251 tree targs;
11252 tree tparms;
11253 int parm_depth;
11255 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11256 targs = DECL_TI_ARGS (decl);
11257 tparms = DECL_TEMPLATE_PARMS (tmpl);
11258 parm_depth = TMPL_PARMS_DEPTH (tparms);
11260 /* There should be as many levels of arguments as there are levels
11261 of parameters. */
11262 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
11264 fn_type = TREE_TYPE (tmpl);
11266 if (parm_depth == 1)
11267 /* No substitution is necessary. */
11269 else
11271 int i;
11272 tree partial_args;
11274 /* Replace the innermost level of the TARGS with NULL_TREEs to
11275 let tsubst know not to substitute for those parameters. */
11276 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11277 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11278 SET_TMPL_ARGS_LEVEL (partial_args, i,
11279 TMPL_ARGS_LEVEL (targs, i));
11280 SET_TMPL_ARGS_LEVEL (partial_args,
11281 TMPL_ARGS_DEPTH (targs),
11282 make_tree_vec (DECL_NTPARMS (tmpl)));
11284 /* Make sure that we can see identifiers, and compute access
11285 correctly. We can just use the context of DECL for the
11286 partial substitution here. It depends only on outer template
11287 parameters, regardless of whether the innermost level is
11288 specialized or not. */
11289 push_access_scope (decl);
11291 /* Now, do the (partial) substitution to figure out the
11292 appropriate function type. */
11293 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
11295 /* Substitute into the template parameters to obtain the real
11296 innermost set of parameters. This step is important if the
11297 innermost set of template parameters contains value
11298 parameters whose types depend on outer template parameters. */
11299 TREE_VEC_LENGTH (partial_args)--;
11300 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
11302 pop_access_scope (decl);
11305 return fn_type;
11308 /* Return truthvalue if we're processing a template different from
11309 the last one involved in diagnostics. */
11311 problematic_instantiation_changed ()
11313 return last_template_error_tick != tinst_level_tick;
11316 /* Remember current template involved in diagnostics. */
11317 void
11318 record_last_problematic_instantiation ()
11320 last_template_error_tick = tinst_level_tick;
11323 tree
11324 current_instantiation ()
11326 return current_tinst_level;
11329 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11330 type. Return zero for ok, nonzero for disallowed. Issue error and
11331 warning messages under control of COMPLAIN. */
11333 static int
11334 invalid_nontype_parm_type_p (type, complain)
11335 tree type;
11336 tsubst_flags_t complain;
11338 if (INTEGRAL_TYPE_P (type))
11339 return 0;
11340 else if (POINTER_TYPE_P (type))
11341 return 0;
11342 else if (TYPE_PTRMEM_P (type))
11343 return 0;
11344 else if (TYPE_PTRMEMFUNC_P (type))
11345 return 0;
11346 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11347 return 0;
11348 else if (TREE_CODE (type) == TYPENAME_TYPE)
11349 return 0;
11351 if (complain & tf_error)
11352 error ("`%#T' is not a valid type for a template constant parameter",
11353 type);
11354 return 1;
11357 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11358 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11360 static bool
11361 dependent_type_p_r (tree type)
11363 tree scope;
11365 /* [temp.dep.type]
11367 A type is dependent if it is:
11369 -- a template parameter. */
11370 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11371 return true;
11372 /* -- a qualified-id with a nested-name-specifier which contains a
11373 class-name that names a dependent type or whose unqualified-id
11374 names a dependent type. */
11375 if (TREE_CODE (type) == TYPENAME_TYPE)
11376 return true;
11377 /* -- a cv-qualified type where the cv-unqualified type is
11378 dependent. */
11379 type = TYPE_MAIN_VARIANT (type);
11380 /* -- a compound type constructed from any dependent type. */
11381 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
11382 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11383 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11384 (type)));
11385 else if (TREE_CODE (type) == POINTER_TYPE
11386 || TREE_CODE (type) == REFERENCE_TYPE)
11387 return dependent_type_p (TREE_TYPE (type));
11388 else if (TREE_CODE (type) == FUNCTION_TYPE
11389 || TREE_CODE (type) == METHOD_TYPE)
11391 tree arg_type;
11393 if (dependent_type_p (TREE_TYPE (type)))
11394 return true;
11395 for (arg_type = TYPE_ARG_TYPES (type);
11396 arg_type;
11397 arg_type = TREE_CHAIN (arg_type))
11398 if (dependent_type_p (TREE_VALUE (arg_type)))
11399 return true;
11400 return false;
11402 /* -- an array type constructed from any dependent type or whose
11403 size is specified by a constant expression that is
11404 value-dependent. */
11405 if (TREE_CODE (type) == ARRAY_TYPE)
11407 if (TYPE_DOMAIN (type)
11408 && ((value_dependent_expression_p
11409 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11410 || (type_dependent_expression_p
11411 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11412 return true;
11413 return dependent_type_p (TREE_TYPE (type));
11415 /* -- a template-id in which either the template name is a template
11416 parameter or any of the template arguments is a dependent type or
11417 an expression that is type-dependent or value-dependent.
11419 This language seems somewhat confused; for example, it does not
11420 discuss template template arguments. Therefore, we use the
11421 definition for dependent template arguments in [temp.dep.temp]. */
11422 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11423 && (dependent_template_id_p
11424 (CLASSTYPE_TI_TEMPLATE (type),
11425 CLASSTYPE_TI_ARGS (type))))
11426 return true;
11427 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11428 return true;
11429 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11430 expression is not type-dependent, then it should already been
11431 have resolved. */
11432 if (TREE_CODE (type) == TYPEOF_TYPE)
11433 return true;
11434 /* The standard does not specifically mention types that are local
11435 to template functions or local classes, but they should be
11436 considered dependent too. For example:
11438 template <int I> void f() {
11439 enum E { a = I };
11440 S<sizeof (E)> s;
11443 The size of `E' cannot be known until the value of `I' has been
11444 determined. Therefore, `E' must be considered dependent. */
11445 scope = TYPE_CONTEXT (type);
11446 if (scope && TYPE_P (scope))
11447 return dependent_type_p (scope);
11448 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11449 return type_dependent_expression_p (scope);
11451 /* Other types are non-dependent. */
11452 return false;
11455 /* Returns TRUE if TYPE is dependent, in the sense of
11456 [temp.dep.type]. */
11458 bool
11459 dependent_type_p (tree type)
11461 /* If there are no template parameters in scope, then there can't be
11462 any dependent types. */
11463 if (!processing_template_decl)
11464 return false;
11466 /* If the type is NULL, we have not computed a type for the entity
11467 in question; in that case, the type is dependent. */
11468 if (!type)
11469 return true;
11471 /* Erroneous types can be considered non-dependent. */
11472 if (type == error_mark_node)
11473 return false;
11475 /* If we have not already computed the appropriate value for TYPE,
11476 do so now. */
11477 if (!TYPE_DEPENDENT_P_VALID (type))
11479 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11480 TYPE_DEPENDENT_P_VALID (type) = 1;
11483 return TYPE_DEPENDENT_P (type);
11486 /* Returns TRUE if the EXPRESSION is value-dependent. */
11488 bool
11489 value_dependent_expression_p (tree expression)
11491 if (!processing_template_decl)
11492 return false;
11494 /* A name declared with a dependent type. */
11495 if (TREE_CODE (expression) == LOOKUP_EXPR
11496 || (DECL_P (expression)
11497 && dependent_type_p (TREE_TYPE (expression))))
11498 return true;
11499 /* A non-type template parameter. */
11500 if ((TREE_CODE (expression) == CONST_DECL
11501 && DECL_TEMPLATE_PARM_P (expression))
11502 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11503 return true;
11504 /* A constant with integral or enumeration type and is initialized
11505 with an expression that is value-dependent. */
11506 if (TREE_CODE (expression) == VAR_DECL
11507 && DECL_INITIAL (expression)
11508 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
11509 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
11510 && value_dependent_expression_p (DECL_INITIAL (expression)))
11511 return true;
11512 /* These expressions are value-dependent if the type to which the
11513 cast occurs is dependent. */
11514 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11515 || TREE_CODE (expression) == STATIC_CAST_EXPR
11516 || TREE_CODE (expression) == CONST_CAST_EXPR
11517 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11518 || TREE_CODE (expression) == CAST_EXPR)
11519 && dependent_type_p (TREE_TYPE (expression)))
11520 return true;
11521 /* A `sizeof' expression where the sizeof operand is a type is
11522 value-dependent if the type is dependent. If the type was not
11523 dependent, we would no longer have a SIZEOF_EXPR, so any
11524 SIZEOF_EXPR is dependent. */
11525 if (TREE_CODE (expression) == SIZEOF_EXPR)
11526 return true;
11527 /* A constant expression is value-dependent if any subexpression is
11528 value-dependent. */
11529 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
11531 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11533 case '1':
11534 return (value_dependent_expression_p
11535 (TREE_OPERAND (expression, 0)));
11536 case '<':
11537 case '2':
11538 return ((value_dependent_expression_p
11539 (TREE_OPERAND (expression, 0)))
11540 || (value_dependent_expression_p
11541 (TREE_OPERAND (expression, 1))));
11542 case 'e':
11544 int i;
11545 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11546 /* In some cases, some of the operands may be missing.
11547 (For example, in the case of PREDECREMENT_EXPR, the
11548 amount to increment by may be missing.) That doesn't
11549 make the expression dependent. */
11550 if (TREE_OPERAND (expression, i)
11551 && (value_dependent_expression_p
11552 (TREE_OPERAND (expression, i))))
11553 return true;
11554 return false;
11559 /* The expression is not value-dependent. */
11560 return false;
11563 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11564 [temp.dep.expr]. */
11566 bool
11567 type_dependent_expression_p (expression)
11568 tree expression;
11570 if (!processing_template_decl)
11571 return false;
11573 /* Some expression forms are never type-dependent. */
11574 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11575 || TREE_CODE (expression) == SIZEOF_EXPR
11576 || TREE_CODE (expression) == ALIGNOF_EXPR
11577 || TREE_CODE (expression) == TYPEID_EXPR
11578 || TREE_CODE (expression) == DELETE_EXPR
11579 || TREE_CODE (expression) == VEC_DELETE_EXPR
11580 || TREE_CODE (expression) == THROW_EXPR)
11581 return false;
11583 /* The types of these expressions depends only on the type to which
11584 the cast occurs. */
11585 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11586 || TREE_CODE (expression) == STATIC_CAST_EXPR
11587 || TREE_CODE (expression) == CONST_CAST_EXPR
11588 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11589 || TREE_CODE (expression) == CAST_EXPR)
11590 return dependent_type_p (TREE_TYPE (expression));
11591 /* The types of these expressions depends only on the type created
11592 by the expression. */
11593 else if (TREE_CODE (expression) == NEW_EXPR
11594 || TREE_CODE (expression) == VEC_NEW_EXPR)
11596 /* For NEW_EXPR tree nodes created inside a template, either
11597 the object type itself or a TREE_LIST may appear as the
11598 operand 1. */
11599 tree type = TREE_OPERAND (expression, 1);
11600 if (TREE_CODE (type) == TREE_LIST)
11601 /* This is an array type. We need to check array dimensions
11602 as well. */
11603 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
11604 || value_dependent_expression_p
11605 (TREE_OPERAND (TREE_VALUE (type), 1));
11606 else
11607 return dependent_type_p (type);
11610 if (TREE_CODE (expression) == FUNCTION_DECL
11611 && DECL_LANG_SPECIFIC (expression)
11612 && DECL_TEMPLATE_INFO (expression)
11613 && (dependent_template_id_p
11614 (DECL_TI_TEMPLATE (expression),
11615 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11616 return true;
11618 return (dependent_type_p (TREE_TYPE (expression)));
11621 /* Returns TRUE if the ARG (a template argument) is dependent. */
11623 bool
11624 dependent_template_arg_p (tree arg)
11626 if (!processing_template_decl)
11627 return false;
11629 if (TREE_CODE (arg) == TEMPLATE_DECL
11630 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11631 return dependent_template_p (arg);
11632 else if (TYPE_P (arg))
11633 return dependent_type_p (arg);
11634 else
11635 return (type_dependent_expression_p (arg)
11636 || value_dependent_expression_p (arg));
11639 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11641 static bool
11642 dependent_template_id_p (tree tmpl, tree args)
11644 int i;
11646 if (dependent_template_p (tmpl))
11647 return true;
11648 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
11649 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
11650 return true;
11651 return false;
11654 /* Returns TRUE if the template TMPL is dependent. */
11656 bool
11657 dependent_template_p (tree tmpl)
11659 /* Template template parameters are dependent. */
11660 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
11661 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
11662 return true;
11663 /* So are member templates of dependent classes. */
11664 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
11665 return dependent_type_p (DECL_CONTEXT (tmpl));
11666 return false;
11669 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11670 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11671 can be found. Note that this function peers inside uninstantiated
11672 templates and therefore should be used only in extremely limited
11673 situations. */
11675 tree
11676 resolve_typename_type (tree type, bool only_current_p)
11678 tree scope;
11679 tree name;
11680 tree decl;
11681 int quals;
11683 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
11684 20010702);
11686 scope = TYPE_CONTEXT (type);
11687 name = TYPE_IDENTIFIER (type);
11689 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11690 it first before we can figure out what NAME refers to. */
11691 if (TREE_CODE (scope) == TYPENAME_TYPE)
11692 scope = resolve_typename_type (scope, only_current_p);
11693 /* If we don't know what SCOPE refers to, then we cannot resolve the
11694 TYPENAME_TYPE. */
11695 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
11696 return error_mark_node;
11697 /* If the SCOPE is a template type parameter, we have no way of
11698 resolving the name. */
11699 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
11700 return type;
11701 /* If the SCOPE is not the current instantiation, there's no reason
11702 to look inside it. */
11703 if (only_current_p && !currently_open_class (scope))
11704 return error_mark_node;
11705 /* If SCOPE is a partial instantiation, it will not have a valid
11706 TYPE_FIELDS list, so use the original template. */
11707 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
11708 /* Enter the SCOPE so that name lookup will be resolved as if we
11709 were in the class definition. In particular, SCOPE will no
11710 longer be considered a dependent type. */
11711 push_scope (scope);
11712 /* Look up the declaration. */
11713 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
11714 /* Obtain the set of qualifiers applied to the TYPE. */
11715 quals = cp_type_quals (type);
11716 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11717 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11718 if (!decl)
11719 type = error_mark_node;
11720 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
11721 && TREE_CODE (decl) == TYPE_DECL)
11722 type = TREE_TYPE (decl);
11723 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
11724 && DECL_CLASS_TEMPLATE_P (decl))
11726 tree tmpl;
11727 tree args;
11728 /* Obtain the template and the arguments. */
11729 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
11730 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
11731 /* Instantiate the template. */
11732 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
11733 /*entering_scope=*/0,
11734 tf_error);
11736 else
11737 type = error_mark_node;
11738 /* Qualify the resulting type. */
11739 if (type != error_mark_node && quals)
11740 type = cp_build_qualified_type (type, quals);
11741 /* Leave the SCOPE. */
11742 pop_scope (scope);
11744 return type;
11747 tree
11748 resolve_typename_type_in_current_instantiation (tree type)
11750 tree t;
11752 t = resolve_typename_type (type, /*only_current_p=*/true);
11753 return (t != error_mark_node) ? t : type;
11756 #include "gt-cp-pt.h"