1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 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 GNU CC.
9 GNU CC 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)
14 GNU CC 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 GNU CC; 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". */
35 #include "tree-inline.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t) PARAMS ((tree, void*));
50 extern struct obstack permanent_obstack;
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the
55 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
56 the instantiate request occurred; the TREE_VALUE is a either a DECL
57 (for a function or static data member), or a TYPE (for a class)
58 indicating what we are hoping to instantiate. */
59 static tree pending_templates;
60 static tree last_pending_template;
62 int processing_template_parmlist;
63 static int template_header_count;
65 static tree saved_trees;
66 static varray_type inline_parm_levels;
67 static size_t inline_parm_levels_used;
69 static tree current_tinst_level;
71 /* A map from local variable declarations in the body of the template
72 presently being instantiated to the corresponding instantiated
74 static htab_t local_specializations;
76 #define obstack_chunk_alloc xmalloc
77 #define obstack_chunk_free free
79 #define UNIFY_ALLOW_NONE 0
80 #define UNIFY_ALLOW_MORE_CV_QUAL 1
81 #define UNIFY_ALLOW_LESS_CV_QUAL 2
82 #define UNIFY_ALLOW_DERIVED 4
83 #define UNIFY_ALLOW_INTEGER 8
84 #define UNIFY_ALLOW_OUTER_LEVEL 16
85 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
86 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
87 #define UNIFY_ALLOW_MAX_CORRECTION 128
89 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
90 virtual, or a base class of a virtual
92 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
93 type with the desired type. */
95 static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree,
96 unification_kind_t, int));
97 static int try_one_overload PARAMS ((tree, tree, tree, tree, tree,
98 unification_kind_t, int));
99 static int unify PARAMS ((tree, tree, tree, tree, int));
100 static void add_pending_template PARAMS ((tree));
101 static void reopen_tinst_level PARAMS ((tree));
102 static tree classtype_mangled_name PARAMS ((tree));
103 static char *mangle_class_name_for_template PARAMS ((const char *, 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, int, int));
108 static void tsubst_enum PARAMS ((tree, tree, tree));
109 static tree add_to_template_args PARAMS ((tree, tree));
110 static tree add_outermost_template_args PARAMS ((tree, tree));
111 static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*,
113 static int type_unification_real PARAMS ((tree, tree, tree, tree,
114 int, unification_kind_t, int, int));
115 static void note_template_header PARAMS ((int));
116 static tree maybe_fold_nontype_arg PARAMS ((tree));
117 static tree convert_nontype_argument PARAMS ((tree, tree));
118 static tree convert_template_argument PARAMS ((tree, tree, tree, int,
120 static tree get_bindings_overload PARAMS ((tree, tree, tree));
121 static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*));
122 static tree build_template_parm_index PARAMS ((int, int, int, tree, tree));
123 static int inline_needs_template_parms PARAMS ((tree));
124 static void push_inline_template_parms_recursive PARAMS ((tree, int));
125 static tree retrieve_specialization PARAMS ((tree, tree));
126 static tree retrieve_local_specialization PARAMS ((tree));
127 static tree register_specialization PARAMS ((tree, tree, tree));
128 static void register_local_specialization PARAMS ((tree, tree));
129 static int unregister_specialization PARAMS ((tree, tree));
130 static tree reduce_template_parm_level PARAMS ((tree, tree, int));
131 static tree build_template_decl PARAMS ((tree, tree));
132 static int mark_template_parm PARAMS ((tree, void *));
133 static tree tsubst_friend_function PARAMS ((tree, tree));
134 static tree tsubst_friend_class PARAMS ((tree, tree));
135 static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int));
136 static int template_decl_level PARAMS ((tree));
137 static tree maybe_get_template_decl_from_type_decl PARAMS ((tree));
138 static int check_cv_quals_for_unify PARAMS ((int, tree, tree));
139 static tree tsubst_template_arg_vector PARAMS ((tree, tree, int));
140 static tree tsubst_template_parms PARAMS ((tree, tree, int));
141 static void regenerate_decl_from_template PARAMS ((tree, tree));
142 static tree most_specialized PARAMS ((tree, tree, tree));
143 static tree most_specialized_class PARAMS ((tree, tree));
144 static int template_class_depth_real PARAMS ((tree, int));
145 static tree tsubst_aggr_type PARAMS ((tree, tree, int, tree, int));
146 static tree tsubst_decl PARAMS ((tree, tree, tree));
147 static tree tsubst_arg_types PARAMS ((tree, tree, int, tree));
148 static tree tsubst_function_type PARAMS ((tree, tree, int, tree));
149 static void check_specialization_scope PARAMS ((void));
150 static tree process_partial_specialization PARAMS ((tree));
151 static void set_current_access_from_decl PARAMS ((tree));
152 static void check_default_tmpl_args PARAMS ((tree, tree, int, int));
153 static tree tsubst_call_declarator_parms PARAMS ((tree, tree, int, tree));
154 static tree get_template_base_recursive PARAMS ((tree, tree,
155 tree, tree, tree, int));
156 static tree get_template_base PARAMS ((tree, tree, tree, tree));
157 static int verify_class_unification PARAMS ((tree, tree, tree));
158 static tree try_class_unification PARAMS ((tree, tree, tree, tree));
159 static int coerce_template_template_parms PARAMS ((tree, tree, int,
161 static tree determine_specialization PARAMS ((tree, tree, tree *, int));
162 static int template_args_equal PARAMS ((tree, tree));
163 static void tsubst_default_arguments PARAMS ((tree));
164 static tree for_each_template_parm_r PARAMS ((tree *, int *, void *));
165 static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree));
166 static void copy_default_args_to_explicit_spec PARAMS ((tree));
167 static int invalid_nontype_parm_type_p PARAMS ((tree, int));
169 /* Called once to initialize pt.c. */
174 ggc_add_tree_root (&pending_templates, 1);
175 ggc_add_tree_root (&saved_trees, 1);
176 ggc_add_tree_root (¤t_tinst_level, 1);
179 /* Do any processing required when DECL (a member template declaration
180 using TEMPLATE_PARAMETERS as its innermost parameter list) is
181 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
182 it is a specialization, in which case the DECL itself is returned. */
185 finish_member_template_decl (decl)
188 if (decl == NULL_TREE || decl == void_type_node)
190 else if (decl == error_mark_node)
191 /* By returning NULL_TREE, the parser will just ignore this
192 declaration. We have already issued the error. */
194 else if (TREE_CODE (decl) == TREE_LIST)
196 /* Assume that the class is the only declspec. */
197 decl = TREE_VALUE (decl);
198 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
199 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
201 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
202 check_member_template (tmpl);
207 else if (TREE_CODE (decl) == FIELD_DECL)
208 error ("data member `%D' cannot be a member template", decl);
209 else if (DECL_TEMPLATE_INFO (decl))
211 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
213 check_member_template (DECL_TI_TEMPLATE (decl));
214 return DECL_TI_TEMPLATE (decl);
220 error ("invalid member template declaration `%D'", decl);
222 return error_mark_node;
225 /* Returns the template nesting level of the indicated class TYPE.
235 A<T>::B<U> has depth two, while A<T> has depth one.
236 Both A<T>::B<int> and A<int>::B<U> have depth one, if
237 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
240 This function is guaranteed to return 0 if passed NULL_TREE so
241 that, for example, `template_class_depth (current_class_type)' is
245 template_class_depth_real (type, count_specializations)
247 int count_specializations;
252 type && TREE_CODE (type) != NAMESPACE_DECL;
253 type = (TREE_CODE (type) == FUNCTION_DECL)
254 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
256 if (TREE_CODE (type) != FUNCTION_DECL)
258 if (CLASSTYPE_TEMPLATE_INFO (type)
259 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
260 && ((count_specializations
261 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
262 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
267 if (DECL_TEMPLATE_INFO (type)
268 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
269 && ((count_specializations
270 && DECL_TEMPLATE_SPECIALIZATION (type))
271 || uses_template_parms (DECL_TI_ARGS (type))))
279 /* Returns the template nesting level of the indicated class TYPE.
280 Like template_class_depth_real, but instantiations do not count in
284 template_class_depth (type)
287 return template_class_depth_real (type, /*count_specializations=*/0);
290 /* Returns 1 if processing DECL as part of do_pending_inlines
291 needs us to push template parms. */
294 inline_needs_template_parms (decl)
297 if (! DECL_TEMPLATE_INFO (decl))
300 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
301 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
304 /* Subroutine of maybe_begin_member_template_processing.
305 Push the template parms in PARMS, starting from LEVELS steps into the
306 chain, and ending at the beginning, since template parms are listed
310 push_inline_template_parms_recursive (parmlist, levels)
314 tree parms = TREE_VALUE (parmlist);
318 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
320 ++processing_template_decl;
321 current_template_parms
322 = tree_cons (size_int (processing_template_decl),
323 parms, current_template_parms);
324 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
327 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
329 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
330 my_friendly_assert (DECL_P (parm), 0);
332 switch (TREE_CODE (parm))
341 /* Make a CONST_DECL as is done in process_template_parm.
342 It is ugly that we recreate this here; the original
343 version built in process_template_parm is no longer
345 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
347 DECL_ARTIFICIAL (decl) = 1;
348 DECL_INITIAL (decl) = DECL_INITIAL (parm);
349 SET_DECL_TEMPLATE_PARM_P (decl);
360 /* Restore the template parameter context for a member template or
361 a friend template defined in a class definition. */
364 maybe_begin_member_template_processing (decl)
370 if (inline_needs_template_parms (decl))
372 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
373 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
375 if (DECL_TEMPLATE_SPECIALIZATION (decl))
378 parms = TREE_CHAIN (parms);
381 push_inline_template_parms_recursive (parms, levels);
384 /* Remember how many levels of template parameters we pushed so that
385 we can pop them later. */
386 if (!inline_parm_levels)
387 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
388 if (inline_parm_levels_used == inline_parm_levels->num_elements)
389 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
390 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
391 ++inline_parm_levels_used;
394 /* Undo the effects of begin_member_template_processing. */
397 maybe_end_member_template_processing ()
401 if (!inline_parm_levels_used)
404 --inline_parm_levels_used;
406 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
409 --processing_template_decl;
410 current_template_parms = TREE_CHAIN (current_template_parms);
415 /* Returns non-zero iff T is a member template function. We must be
418 template <class T> class C { void f(); }
420 Here, f is a template function, and a member, but not a member
421 template. This function does not concern itself with the origin of
422 T, only its present state. So if we have
424 template <class T> class C { template <class U> void f(U); }
426 then neither C<int>::f<char> nor C<T>::f<double> is considered
427 to be a member template. But, `template <class U> void
428 C<int>::f(U)' is considered a member template. */
431 is_member_template (t)
434 if (!DECL_FUNCTION_TEMPLATE_P (t))
435 /* Anything that isn't a function or a template function is
436 certainly not a member template. */
439 /* A local class can't have member templates. */
440 if (decl_function_context (t))
443 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
444 /* If there are more levels of template parameters than
445 there are template classes surrounding the declaration,
446 then we have a member template. */
447 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
448 template_class_depth (DECL_CONTEXT (t))));
452 /* Returns non-zero iff T is a member template class. See
453 is_member_template for a description of what precisely constitutes
454 a member template. */
457 is_member_template_class (t)
460 if (!DECL_CLASS_TEMPLATE_P (t))
461 /* Anything that isn't a class template, is certainly not a member
465 if (!DECL_CLASS_SCOPE_P (t))
466 /* Anything whose context isn't a class type is surely not a
470 /* If there are more levels of template parameters than there are
471 template classes surrounding the declaration, then we have a
473 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
474 template_class_depth (DECL_CONTEXT (t)));
478 /* Return a new template argument vector which contains all of ARGS,
479 but has as its innermost set of arguments the EXTRA_ARGS. */
482 add_to_template_args (args, extra_args)
491 extra_depth = TMPL_ARGS_DEPTH (extra_args);
492 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
494 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
495 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
497 for (j = 1; j <= extra_depth; ++j, ++i)
498 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
503 /* Like add_to_template_args, but only the outermost ARGS are added to
504 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
505 (EXTRA_ARGS) levels are added. This function is used to combine
506 the template arguments from a partial instantiation with the
507 template arguments used to attain the full instantiation from the
508 partial instantiation. */
511 add_outermost_template_args (args, extra_args)
517 /* If there are more levels of EXTRA_ARGS than there are ARGS,
518 something very fishy is going on. */
519 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
522 /* If *all* the new arguments will be the EXTRA_ARGS, just return
524 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
527 /* For the moment, we make ARGS look like it contains fewer levels. */
528 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
530 new_args = add_to_template_args (args, extra_args);
532 /* Now, we restore ARGS to its full dimensions. */
533 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
538 /* Return the N levels of innermost template arguments from the ARGS. */
541 get_innermost_template_args (args, n)
549 my_friendly_assert (n >= 0, 20000603);
551 /* If N is 1, just return the innermost set of template arguments. */
553 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
555 /* If we're not removing anything, just return the arguments we were
557 extra_levels = TMPL_ARGS_DEPTH (args) - n;
558 my_friendly_assert (extra_levels >= 0, 20000603);
559 if (extra_levels == 0)
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args = make_tree_vec (n);
564 for (i = 1; i <= n; ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i,
566 TMPL_ARGS_LEVEL (args, i + extra_levels));
571 /* We've got a template header coming up; push to a new level for storing
575 begin_template_parm_list ()
577 /* We use a non-tag-transparent scope here, which causes pushtag to
578 put tags in this scope, rather than in the enclosing class or
579 namespace scope. This is the right thing, since we want
580 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
581 global template class, push_template_decl handles putting the
582 TEMPLATE_DECL into top-level scope. For a nested template class,
585 template <class T> struct S1 {
586 template <class T> struct S2 {};
589 pushtag contains special code to call pushdecl_with_scope on the
590 TEMPLATE_DECL for S2. */
591 begin_scope (sk_template_parms);
592 ++processing_template_decl;
593 ++processing_template_parmlist;
594 note_template_header (0);
597 /* This routine is called when a specialization is declared. If it is
598 illegal to declare a specialization here, an error is reported. */
601 check_specialization_scope ()
603 tree scope = current_scope ();
607 An explicit specialization shall be declared in the namespace of
608 which the template is a member, or, for member templates, in the
609 namespace of which the enclosing class or enclosing class
610 template is a member. An explicit specialization of a member
611 function, member class or static data member of a class template
612 shall be declared in the namespace of which the class template
614 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
615 error ("explicit specialization in non-namespace scope `%D'",
620 In an explicit specialization declaration for a member of a class
621 template or a member template that appears in namespace scope,
622 the member template and some of its enclosing class templates may
623 remain unspecialized, except that the declaration shall not
624 explicitly specialize a class member template if its enclosing
625 class templates are not explicitly specialized as well. */
626 if (current_template_parms)
627 error ("enclosing class templates are not explicitly specialized");
630 /* We've just seen template <>. */
633 begin_specialization ()
635 begin_scope (sk_template_spec);
636 note_template_header (1);
637 check_specialization_scope ();
640 /* Called at then end of processing a declaration preceded by
644 end_specialization ()
647 reset_specialization ();
650 /* Any template <>'s that we have seen thus far are not referring to a
651 function specialization. */
654 reset_specialization ()
656 processing_specialization = 0;
657 template_header_count = 0;
660 /* We've just seen a template header. If SPECIALIZATION is non-zero,
661 it was of the form template <>. */
664 note_template_header (specialization)
667 processing_specialization = specialization;
668 template_header_count++;
671 /* We're beginning an explicit instantiation. */
674 begin_explicit_instantiation ()
676 ++processing_explicit_instantiation;
681 end_explicit_instantiation ()
683 my_friendly_assert(processing_explicit_instantiation > 0, 0);
684 --processing_explicit_instantiation;
687 /* The TYPE is being declared. If it is a template type, that means it
688 is a partial specialization. Do appropriate error-checking. */
691 maybe_process_partial_specialization (type)
694 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
696 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
697 && !COMPLETE_TYPE_P (type))
699 if (current_namespace
700 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
702 pedwarn ("specializing `%#T' in different namespace", type);
703 cp_pedwarn_at (" from definition of `%#D'",
704 CLASSTYPE_TI_TEMPLATE (type));
706 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
707 if (processing_template_decl)
708 push_template_decl (TYPE_MAIN_DECL (type));
710 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
711 error ("specialization of `%T' after instantiation", type);
713 else if (processing_specialization)
714 error ("explicit specialization of non-template `%T'", type);
717 /* Retrieve the specialization (in the sense of [temp.spec] - a
718 specialization is either an instantiation or an explicit
719 specialization) of TMPL for the given template ARGS. If there is
720 no such specialization, return NULL_TREE. The ARGS are a vector of
721 arguments, or a vector of vectors of arguments, in the case of
722 templates with more than one level of parameters. */
725 retrieve_specialization (tmpl, args)
731 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
733 /* There should be as many levels of arguments as there are
734 levels of parameters. */
735 my_friendly_assert (TMPL_ARGS_DEPTH (args)
736 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
739 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
742 if (comp_template_args (TREE_PURPOSE (s), args))
743 return TREE_VALUE (s);
748 /* Like retrieve_specialization, but for local declarations. */
751 retrieve_local_specialization (tmpl)
754 return (tree) htab_find (local_specializations, tmpl);
757 /* Returns non-zero iff DECL is a specialization of TMPL. */
760 is_specialization_of (decl, tmpl)
766 if (TREE_CODE (decl) == FUNCTION_DECL)
770 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
776 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
778 for (t = TREE_TYPE (decl);
780 t = CLASSTYPE_USE_TEMPLATE (t)
781 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
782 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
789 /* Register the specialization SPEC as a specialization of TMPL with
790 the indicated ARGS. Returns SPEC, or an equivalent prior
791 declaration, if available. */
794 register_specialization (spec, tmpl, args)
801 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
803 if (TREE_CODE (spec) == FUNCTION_DECL
804 && uses_template_parms (DECL_TI_ARGS (spec)))
805 /* This is the FUNCTION_DECL for a partial instantiation. Don't
806 register it; we want the corresponding TEMPLATE_DECL instead.
807 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
808 the more obvious `uses_template_parms (spec)' to avoid problems
809 with default function arguments. In particular, given
812 template <class T> void f(T t1, T t = T())
814 the default argument expression is not substituted for in an
815 instantiation unless and until it is actually needed. */
818 /* There should be as many levels of arguments as there are
819 levels of parameters. */
820 my_friendly_assert (TMPL_ARGS_DEPTH (args)
821 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
824 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
828 tree fn = TREE_VALUE (s);
830 /* We can sometimes try to re-register a specialization that we've
831 already got. In particular, regenerate_decl_from_template
832 calls duplicate_decls which will update the specialization
833 list. But, we'll still get called again here anyhow. It's
834 more convenient to simply allow this than to try to prevent it. */
837 else if (comp_template_args (TREE_PURPOSE (s), args))
839 if (DECL_TEMPLATE_SPECIALIZATION (spec))
841 if (DECL_TEMPLATE_INSTANTIATION (fn))
844 || DECL_EXPLICIT_INSTANTIATION (fn))
846 error ("specialization of %D after instantiation",
852 /* This situation should occur only if the first
853 specialization is an implicit instantiation,
854 the second is an explicit specialization, and
855 the implicit instantiation has not yet been
856 used. That situation can occur if we have
857 implicitly instantiated a member function and
858 then specialized it later.
860 We can also wind up here if a friend
861 declaration that looked like an instantiation
862 turns out to be a specialization:
864 template <class T> void foo(T);
865 class S { friend void foo<>(int) };
866 template <> void foo(int);
868 We transform the existing DECL in place so that
869 any pointers to it become pointers to the
872 If there was a definition for the template, but
873 not for the specialization, we want this to
874 look as if there is no definition, and vice
876 DECL_INITIAL (fn) = NULL_TREE;
877 duplicate_decls (spec, fn);
882 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
884 duplicate_decls (spec, fn);
891 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
892 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
897 /* Unregister the specialization SPEC as a specialization of TMPL.
898 Returns nonzero if the SPEC was listed as a specialization of
902 unregister_specialization (spec, tmpl)
908 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
910 s = &TREE_CHAIN (*s))
911 if (TREE_VALUE (*s) == spec)
913 *s = TREE_CHAIN (*s);
920 /* Like register_specialization, but for local declarations. We are
921 registering SPEC, an instantiation of TMPL. */
924 register_local_specialization (spec, tmpl)
930 slot = htab_find_slot (local_specializations, tmpl, INSERT);
934 /* Print the list of candidate FNS in an error message. */
937 print_candidates (fns)
942 const char *str = "candidates are:";
944 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
948 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
949 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
954 /* Returns the template (one of the functions given by TEMPLATE_ID)
955 which can be specialized to match the indicated DECL with the
956 explicit template args given in TEMPLATE_ID. The DECL may be
957 NULL_TREE if none is available. In that case, the functions in
958 TEMPLATE_ID are non-members.
960 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
961 specialization of a member template.
963 The template args (those explicitly specified and those deduced)
964 are output in a newly created vector *TARGS_OUT.
966 If it is impossible to determine the result, an error message is
967 issued. The error_mark_node is returned to indicate failure. */
970 determine_specialization (template_id, decl, targs_out,
971 need_member_template)
975 int need_member_template;
980 tree candidates = NULL_TREE;
981 tree templates = NULL_TREE;
983 *targs_out = NULL_TREE;
985 if (template_id == error_mark_node)
986 return error_mark_node;
988 fns = TREE_OPERAND (template_id, 0);
989 explicit_targs = TREE_OPERAND (template_id, 1);
991 if (fns == error_mark_node)
992 return error_mark_node;
994 /* Check for baselinks. */
995 if (BASELINK_P (fns))
996 fns = TREE_VALUE (fns);
998 if (!is_overloaded_fn (fns))
1000 error ("`%D' is not a function template", fns);
1001 return error_mark_node;
1004 for (; fns; fns = OVL_NEXT (fns))
1008 tree fn = OVL_CURRENT (fns);
1010 if (TREE_CODE (fn) == TEMPLATE_DECL)
1011 /* DECL might be a specialization of FN. */
1013 else if (need_member_template)
1014 /* FN is an ordinary member function, and we need a
1015 specialization of a member template. */
1017 else if (TREE_CODE (fn) != FUNCTION_DECL)
1018 /* We can get IDENTIFIER_NODEs here in certain erroneous
1021 else if (!DECL_FUNCTION_MEMBER_P (fn))
1022 /* This is just an ordinary non-member function. Nothing can
1023 be a specialization of that. */
1025 else if (DECL_ARTIFICIAL (fn))
1026 /* Cannot specialize functions that are created implicitly. */
1030 tree decl_arg_types;
1032 /* This is an ordinary member function. However, since
1033 we're here, we can assume it's enclosing class is a
1034 template class. For example,
1036 template <typename T> struct S { void f(); };
1037 template <> void S<int>::f() {}
1039 Here, S<int>::f is a non-template, but S<int> is a
1040 template class. If FN has the same type as DECL, we
1041 might be in business. */
1043 if (!DECL_TEMPLATE_INFO (fn))
1044 /* Its enclosing class is an explicit specialization
1045 of a template class. This is not a candidate. */
1048 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1049 TREE_TYPE (TREE_TYPE (fn))))
1050 /* The return types differ. */
1053 /* Adjust the type of DECL in case FN is a static member. */
1054 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1055 if (DECL_STATIC_FUNCTION_P (fn)
1056 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1057 decl_arg_types = TREE_CHAIN (decl_arg_types);
1059 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1062 candidates = tree_cons (NULL_TREE, fn, candidates);
1067 /* See whether this function might be a specialization of this
1069 targs = get_bindings (tmpl, decl, explicit_targs);
1072 /* We cannot deduce template arguments that when used to
1073 specialize TMPL will produce DECL. */
1076 /* Save this template, and the arguments deduced. */
1077 templates = tree_cons (targs, tmpl, templates);
1080 if (templates && TREE_CHAIN (templates))
1086 It is possible for a specialization with a given function
1087 signature to be instantiated from more than one function
1088 template. In such cases, explicit specification of the
1089 template arguments must be used to uniquely identify the
1090 function template specialization being specialized.
1092 Note that here, there's no suggestion that we're supposed to
1093 determine which of the candidate templates is most
1094 specialized. However, we, also have:
1098 Partial ordering of overloaded function template
1099 declarations is used in the following contexts to select
1100 the function template to which a function template
1101 specialization refers:
1103 -- when an explicit specialization refers to a function
1106 So, we do use the partial ordering rules, at least for now.
1107 This extension can only serve to make illegal programs legal,
1108 so it's safe. And, there is strong anecdotal evidence that
1109 the committee intended the partial ordering rules to apply;
1110 the EDG front-end has that behavior, and John Spicer claims
1111 that the committee simply forgot to delete the wording in
1112 [temp.expl.spec]. */
1113 tree tmpl = most_specialized (templates, decl, explicit_targs);
1114 if (tmpl && tmpl != error_mark_node)
1116 targs = get_bindings (tmpl, decl, explicit_targs);
1117 templates = tree_cons (targs, tmpl, NULL_TREE);
1121 if (templates == NULL_TREE && candidates == NULL_TREE)
1123 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1125 return error_mark_node;
1127 else if ((templates && TREE_CHAIN (templates))
1128 || (candidates && TREE_CHAIN (candidates))
1129 || (templates && candidates))
1131 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1133 chainon (candidates, templates);
1134 print_candidates (candidates);
1135 return error_mark_node;
1138 /* We have one, and exactly one, match. */
1141 /* It was a specialization of an ordinary member function in a
1143 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1144 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1147 /* It was a specialization of a template. */
1148 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1149 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1151 *targs_out = copy_node (targs);
1152 SET_TMPL_ARGS_LEVEL (*targs_out,
1153 TMPL_ARGS_DEPTH (*targs_out),
1154 TREE_PURPOSE (templates));
1157 *targs_out = TREE_PURPOSE (templates);
1158 return TREE_VALUE (templates);
1161 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1162 but with the default argument values filled in from those in the
1166 copy_default_args_to_explicit_spec_1 (spec_types,
1171 tree new_spec_types;
1176 if (spec_types == void_list_node)
1177 return void_list_node;
1179 /* Substitute into the rest of the list. */
1181 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1182 TREE_CHAIN (tmpl_types));
1184 /* Add the default argument for this parameter. */
1185 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1186 TREE_VALUE (spec_types),
1190 /* DECL is an explicit specialization. Replicate default arguments
1191 from the template it specializes. (That way, code like:
1193 template <class T> void f(T = 3);
1194 template <> void f(double);
1197 works, as required.) An alternative approach would be to look up
1198 the correct default arguments at the call-site, but this approach
1199 is consistent with how implicit instantiations are handled. */
1202 copy_default_args_to_explicit_spec (decl)
1208 tree new_spec_types;
1212 tree object_type = NULL_TREE;
1213 tree in_charge = NULL_TREE;
1214 tree vtt = NULL_TREE;
1216 /* See if there's anything we need to do. */
1217 tmpl = DECL_TI_TEMPLATE (decl);
1218 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1219 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1220 if (TREE_PURPOSE (t))
1225 old_type = TREE_TYPE (decl);
1226 spec_types = TYPE_ARG_TYPES (old_type);
1228 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1230 /* Remove the this pointer, but remember the object's type for
1232 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1233 spec_types = TREE_CHAIN (spec_types);
1234 tmpl_types = TREE_CHAIN (tmpl_types);
1236 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1238 /* DECL may contain more parameters than TMPL due to the extra
1239 in-charge parameter in constructors and destructors. */
1240 in_charge = spec_types;
1241 spec_types = TREE_CHAIN (spec_types);
1243 if (DECL_HAS_VTT_PARM_P (decl))
1246 spec_types = TREE_CHAIN (spec_types);
1250 /* Compute the merged default arguments. */
1252 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1254 /* Compute the new FUNCTION_TYPE. */
1258 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1263 /* Put the in-charge parameter back. */
1264 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1265 TREE_VALUE (in_charge),
1268 new_type = build_cplus_method_type (object_type,
1269 TREE_TYPE (old_type),
1273 new_type = build_function_type (TREE_TYPE (old_type),
1275 new_type = build_type_attribute_variant (new_type,
1276 TYPE_ATTRIBUTES (old_type));
1277 new_type = build_exception_variant (new_type,
1278 TYPE_RAISES_EXCEPTIONS (old_type));
1279 TREE_TYPE (decl) = new_type;
1282 /* Check to see if the function just declared, as indicated in
1283 DECLARATOR, and in DECL, is a specialization of a function
1284 template. We may also discover that the declaration is an explicit
1285 instantiation at this point.
1287 Returns DECL, or an equivalent declaration that should be used
1288 instead if all goes well. Issues an error message if something is
1289 amiss. Returns error_mark_node if the error is not easily
1292 FLAGS is a bitmask consisting of the following flags:
1294 2: The function has a definition.
1295 4: The function is a friend.
1297 The TEMPLATE_COUNT is the number of references to qualifying
1298 template classes that appeared in the name of the function. For
1301 template <class T> struct S { void f(); };
1304 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1305 classes are not counted in the TEMPLATE_COUNT, so that in
1307 template <class T> struct S {};
1308 template <> struct S<int> { void f(); }
1309 template <> void S<int>::f();
1311 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1312 illegal; there should be no template <>.)
1314 If the function is a specialization, it is marked as such via
1315 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1316 is set up correctly, and it is added to the list of specializations
1317 for that template. */
1320 check_explicit_specialization (declarator, decl, template_count, flags)
1326 int have_def = flags & 2;
1327 int is_friend = flags & 4;
1328 int specialization = 0;
1329 int explicit_instantiation = 0;
1330 int member_specialization = 0;
1331 tree ctype = DECL_CLASS_CONTEXT (decl);
1332 tree dname = DECL_NAME (decl);
1335 tsk = current_tmpl_spec_kind (template_count);
1340 if (processing_specialization)
1343 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1345 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1348 /* This could be something like:
1350 template <class T> void f(T);
1351 class S { friend void f<>(int); } */
1355 /* This case handles bogus declarations like template <>
1356 template <class T> void f<int>(); */
1358 error ("template-id `%D' in declaration of primary template",
1365 case tsk_invalid_member_spec:
1366 /* The error has already been reported in
1367 check_specialization_scope. */
1368 return error_mark_node;
1370 case tsk_invalid_expl_inst:
1371 error ("template parameter list used in explicit instantiation");
1377 error ("definition provided for explicit instantiation");
1379 explicit_instantiation = 1;
1382 case tsk_excessive_parms:
1383 error ("too many template parameter lists in declaration of `%D'",
1385 return error_mark_node;
1389 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1391 member_specialization = 1;
1396 case tsk_insufficient_parms:
1397 if (template_header_count)
1399 error("too few template parameter lists in declaration of `%D'",
1403 else if (ctype != NULL_TREE
1404 && !TYPE_BEING_DEFINED (ctype)
1405 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1408 /* For backwards compatibility, we accept:
1410 template <class T> struct S { void f(); };
1411 void S<int>::f() {} // Missing template <>
1413 That used to be legal C++. */
1416 ("explicit specialization not preceded by `template <>'");
1418 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1423 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1425 /* This case handles bogus declarations like template <>
1426 template <class T> void f<int>(); */
1428 if (uses_template_parms (declarator))
1429 error ("partial specialization `%D' of function template",
1432 error ("template-id `%D' in declaration of primary template",
1437 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1438 /* This is a specialization of a member template, without
1439 specialization the containing class. Something like:
1441 template <class T> struct S {
1442 template <class U> void f (U);
1444 template <> template <class U> void S<int>::f(U) {}
1446 That's a specialization -- but of the entire template. */
1454 if (specialization || member_specialization)
1456 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1457 for (; t; t = TREE_CHAIN (t))
1458 if (TREE_PURPOSE (t))
1461 ("default argument specified in explicit specialization");
1464 if (current_lang_name == lang_name_c)
1465 error ("template specialization with C linkage");
1468 if (specialization || member_specialization || explicit_instantiation)
1470 tree tmpl = NULL_TREE;
1471 tree targs = NULL_TREE;
1473 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1474 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1478 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1481 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1486 lookup_template_function (fns, NULL_TREE);
1489 if (declarator == error_mark_node)
1490 return error_mark_node;
1492 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1494 if (!explicit_instantiation)
1495 /* A specialization in class scope. This is illegal,
1496 but the error will already have been flagged by
1497 check_specialization_scope. */
1498 return error_mark_node;
1501 /* It's not legal to write an explicit instantiation in
1504 class C { template void f(); }
1506 This case is caught by the parser. However, on
1509 template class C { void f(); };
1511 (which is illegal) we can get here. The error will be
1518 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1520 /* A friend declaration. We can't do much, because we don't
1521 know what this resolves to, yet. */
1522 my_friendly_assert (is_friend != 0, 0);
1523 my_friendly_assert (!explicit_instantiation, 0);
1524 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1527 else if (ctype != NULL_TREE
1528 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1531 /* Find the list of functions in ctype that have the same
1532 name as the declared function. */
1533 tree name = TREE_OPERAND (declarator, 0);
1534 tree fns = NULL_TREE;
1537 if (name == constructor_name (ctype)
1538 || name == constructor_name_full (ctype))
1540 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1542 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1543 : !TYPE_HAS_DESTRUCTOR (ctype))
1545 /* From [temp.expl.spec]:
1547 If such an explicit specialization for the member
1548 of a class template names an implicitly-declared
1549 special member function (clause _special_), the
1550 program is ill-formed.
1552 Similar language is found in [temp.explicit]. */
1553 error ("specialization of implicitly-declared special member function");
1554 return error_mark_node;
1557 name = is_constructor ? ctor_identifier : dtor_identifier;
1560 if (!DECL_CONV_FN_P (decl))
1562 idx = lookup_fnfields_1 (ctype, name);
1564 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1570 /* For a type-conversion operator, we cannot do a
1571 name-based lookup. We might be looking for `operator
1572 int' which will be a specialization of `operator T'.
1573 So, we find *all* the conversion operators, and then
1574 select from them. */
1577 methods = CLASSTYPE_METHOD_VEC (ctype);
1579 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1581 tree ovl = TREE_VEC_ELT (methods, idx);
1583 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1584 /* There are no more conversion functions. */
1587 /* Glue all these conversion functions together
1588 with those we already have. */
1589 for (; ovl; ovl = OVL_NEXT (ovl))
1590 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1594 if (fns == NULL_TREE)
1596 error ("no member function `%D' declared in `%T'",
1598 return error_mark_node;
1601 TREE_OPERAND (declarator, 0) = fns;
1604 /* Figure out what exactly is being specialized at this point.
1605 Note that for an explicit instantiation, even one for a
1606 member function, we cannot tell apriori whether the
1607 instantiation is for a member template, or just a member
1608 function of a template class. Even if a member template is
1609 being instantiated, the member template arguments may be
1610 elided if they can be deduced from the rest of the
1612 tmpl = determine_specialization (declarator, decl,
1614 member_specialization);
1616 if (!tmpl || tmpl == error_mark_node)
1617 /* We couldn't figure out what this declaration was
1619 return error_mark_node;
1622 tree gen_tmpl = most_general_template (tmpl);
1624 if (explicit_instantiation)
1626 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1627 is done by do_decl_instantiation later. */
1629 int arg_depth = TMPL_ARGS_DEPTH (targs);
1630 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1632 if (arg_depth > parm_depth)
1634 /* If TMPL is not the most general template (for
1635 example, if TMPL is a friend template that is
1636 injected into namespace scope), then there will
1637 be too many levels of TARGS. Remove some of them
1642 new_targs = make_tree_vec (parm_depth);
1643 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1644 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1645 = TREE_VEC_ELT (targs, i);
1649 return instantiate_template (tmpl, targs);
1652 /* If this is a specialization of a member template of a
1653 template class. In we want to return the TEMPLATE_DECL,
1654 not the specialization of it. */
1655 if (tsk == tsk_template)
1657 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1658 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
1662 /* If we thought that the DECL was a member function, but it
1663 turns out to be specializing a static member function,
1664 make DECL a static member function as well. */
1665 if (DECL_STATIC_FUNCTION_P (tmpl)
1666 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1668 revert_static_member_fn (decl);
1669 last_function_parms = TREE_CHAIN (last_function_parms);
1672 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1673 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
1675 /* Inherit default function arguments from the template
1676 DECL is specializing. */
1677 copy_default_args_to_explicit_spec (decl);
1679 /* This specialization has the same protection as the
1680 template it specializes. */
1681 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
1682 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
1684 if (is_friend && !have_def)
1685 /* This is not really a declaration of a specialization.
1686 It's just the name of an instantiation. But, it's not
1687 a request for an instantiation, either. */
1688 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1689 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
1690 /* This is indeed a specialization. In case of constructors
1691 and destructors, we need in-charge and not-in-charge
1692 versions in V3 ABI. */
1693 clone_function_decl (decl, /*update_method_vec_p=*/0);
1695 /* Register this specialization so that we can find it
1697 decl = register_specialization (decl, gen_tmpl, targs);
1704 /* TYPE is being declared. Verify that the use of template headers
1705 and such is reasonable. Issue error messages if not. */
1708 maybe_check_template_type (type)
1711 if (template_header_count)
1713 /* We are in the scope of some `template <...>' header. */
1716 = template_class_depth_real (TYPE_CONTEXT (type),
1717 /*count_specializations=*/1);
1719 if (template_header_count <= context_depth)
1720 /* This is OK; the template headers are for the context. We
1721 are actually too lenient here; like
1722 check_explicit_specialization we should consider the number
1723 of template types included in the actual declaration. For
1726 template <class T> struct S {
1727 template <class U> template <class V>
1733 template <class T> struct S {
1734 template <class U> struct I;
1737 template <class T> template <class U.
1742 else if (template_header_count > context_depth + 1)
1743 /* There are two many template parameter lists. */
1744 error ("too many template parameter lists in declaration of `%T'", type);
1748 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1749 parameters. These are represented in the same format used for
1750 DECL_TEMPLATE_PARMS. */
1752 int comp_template_parms (parms1, parms2)
1759 if (parms1 == parms2)
1762 for (p1 = parms1, p2 = parms2;
1763 p1 != NULL_TREE && p2 != NULL_TREE;
1764 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1766 tree t1 = TREE_VALUE (p1);
1767 tree t2 = TREE_VALUE (p2);
1770 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1771 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1773 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1776 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1778 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1779 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1781 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1784 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1786 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
1791 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1792 /* One set of parameters has more parameters lists than the
1799 /* Complain if DECL shadows a template parameter.
1801 [temp.local]: A template-parameter shall not be redeclared within its
1802 scope (including nested scopes). */
1805 check_template_shadow (decl)
1810 /* If we're not in a template, we can't possibly shadow a template
1812 if (!current_template_parms)
1815 /* Figure out what we're shadowing. */
1816 if (TREE_CODE (decl) == OVERLOAD)
1817 decl = OVL_CURRENT (decl);
1818 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
1820 /* If there's no previous binding for this name, we're not shadowing
1821 anything, let alone a template parameter. */
1825 /* If we're not shadowing a template parameter, we're done. Note
1826 that OLDDECL might be an OVERLOAD (or perhaps even an
1827 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1829 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
1832 /* We check for decl != olddecl to avoid bogus errors for using a
1833 name inside a class. We check TPFI to avoid duplicate errors for
1834 inline member templates. */
1836 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1839 cp_error_at ("declaration of `%#D'", decl);
1840 cp_error_at (" shadows template parm `%#D'", olddecl);
1843 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1844 ORIG_LEVEL, DECL, and TYPE. */
1847 build_template_parm_index (index, level, orig_level, decl, type)
1854 tree t = make_node (TEMPLATE_PARM_INDEX);
1855 TEMPLATE_PARM_IDX (t) = index;
1856 TEMPLATE_PARM_LEVEL (t) = level;
1857 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1858 TEMPLATE_PARM_DECL (t) = decl;
1859 TREE_TYPE (t) = type;
1864 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1865 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1866 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1867 new one is created. */
1870 reduce_template_parm_level (index, type, levels)
1875 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1876 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1877 != TEMPLATE_PARM_LEVEL (index) - levels))
1880 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1881 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1884 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1885 TEMPLATE_PARM_LEVEL (index) - levels,
1886 TEMPLATE_PARM_ORIG_LEVEL (index),
1888 TEMPLATE_PARM_DESCENDANTS (index) = t;
1890 DECL_ARTIFICIAL (decl) = 1;
1891 SET_DECL_TEMPLATE_PARM_P (decl);
1893 /* Template template parameters need this. */
1894 DECL_TEMPLATE_PARMS (decl)
1895 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1898 return TEMPLATE_PARM_DESCENDANTS (index);
1901 /* Process information from new template parameter NEXT and append it to the
1902 LIST being built. */
1905 process_template_parm (list, next)
1914 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1915 defval = TREE_PURPOSE (parm);
1916 parm = TREE_VALUE (parm);
1917 is_type = TREE_PURPOSE (parm) == class_type_node;
1921 tree p = TREE_VALUE (tree_last (list));
1923 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
1924 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1926 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1934 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1935 /* is a const-param */
1936 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1938 SET_DECL_TEMPLATE_PARM_P (parm);
1942 The top-level cv-qualifiers on the template-parameter are
1943 ignored when determining its type. */
1944 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1946 /* A template parameter is not modifiable. */
1947 TREE_READONLY (parm) = 1;
1948 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
1949 TREE_TYPE (parm) = void_type_node;
1950 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1951 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1952 = build_template_parm_index (idx, processing_template_decl,
1953 processing_template_decl,
1954 decl, TREE_TYPE (parm));
1959 parm = TREE_VALUE (parm);
1961 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1963 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1964 /* This is for distinguishing between real templates and template
1965 template parameters */
1966 TREE_TYPE (parm) = t;
1967 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1972 t = make_aggr_type (TEMPLATE_TYPE_PARM);
1973 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1974 decl = build_decl (TYPE_DECL, parm, t);
1977 TYPE_NAME (t) = decl;
1978 TYPE_STUB_DECL (t) = decl;
1980 TEMPLATE_TYPE_PARM_INDEX (t)
1981 = build_template_parm_index (idx, processing_template_decl,
1982 processing_template_decl,
1983 decl, TREE_TYPE (parm));
1985 DECL_ARTIFICIAL (decl) = 1;
1986 SET_DECL_TEMPLATE_PARM_P (decl);
1988 parm = build_tree_list (defval, parm);
1989 return chainon (list, parm);
1992 /* The end of a template parameter list has been reached. Process the
1993 tree list into a parameter vector, converting each parameter into a more
1994 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1998 end_template_parm_list (parms)
2003 tree saved_parmlist = make_tree_vec (list_length (parms));
2005 current_template_parms
2006 = tree_cons (size_int (processing_template_decl),
2007 saved_parmlist, current_template_parms);
2009 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2011 next = TREE_CHAIN (parm);
2012 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2013 TREE_CHAIN (parm) = NULL_TREE;
2016 --processing_template_parmlist;
2018 return saved_parmlist;
2021 /* end_template_decl is called after a template declaration is seen. */
2024 end_template_decl ()
2026 reset_specialization ();
2028 if (! processing_template_decl)
2031 /* This matches the pushlevel in begin_template_parm_list. */
2034 --processing_template_decl;
2035 current_template_parms = TREE_CHAIN (current_template_parms);
2038 /* Given a template argument vector containing the template PARMS.
2039 The innermost PARMS are given first. */
2042 current_template_args ()
2045 tree args = NULL_TREE;
2046 int length = TMPL_PARMS_DEPTH (current_template_parms);
2049 /* If there is only one level of template parameters, we do not
2050 create a TREE_VEC of TREE_VECs. Instead, we return a single
2051 TREE_VEC containing the arguments. */
2053 args = make_tree_vec (length);
2055 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2057 tree a = copy_node (TREE_VALUE (header));
2060 TREE_TYPE (a) = NULL_TREE;
2061 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2063 tree t = TREE_VEC_ELT (a, i);
2065 /* T will be a list if we are called from within a
2066 begin/end_template_parm_list pair, but a vector directly
2067 if within a begin/end_member_template_processing pair. */
2068 if (TREE_CODE (t) == TREE_LIST)
2072 if (TREE_CODE (t) == TYPE_DECL
2073 || TREE_CODE (t) == TEMPLATE_DECL)
2076 t = DECL_INITIAL (t);
2077 TREE_VEC_ELT (a, i) = t;
2082 TREE_VEC_ELT (args, --l) = a;
2090 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2091 template PARMS. Used by push_template_decl below. */
2094 build_template_decl (decl, parms)
2098 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2099 DECL_TEMPLATE_PARMS (tmpl) = parms;
2100 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2101 if (DECL_LANG_SPECIFIC (decl))
2103 if (CAN_HAVE_FULL_LANG_DECL_P (decl))
2104 DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl);
2105 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2106 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2107 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2108 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2109 if (DECL_OVERLOADED_OPERATOR_P (decl))
2110 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2111 DECL_OVERLOADED_OPERATOR_P (decl));
2117 struct template_parm_data
2119 /* The level of the template parameters we are currently
2123 /* The index of the specialization argument we are currently
2127 /* An array whose size is the number of template parameters. The
2128 elements are non-zero if the parameter has been used in any one
2129 of the arguments processed so far. */
2132 /* An array whose size is the number of template arguments. The
2133 elements are non-zero if the argument makes use of template
2134 parameters of this level. */
2135 int* arg_uses_template_parms;
2138 /* Subroutine of push_template_decl used to see if each template
2139 parameter in a partial specialization is used in the explicit
2140 argument list. If T is of the LEVEL given in DATA (which is
2141 treated as a template_parm_data*), then DATA->PARMS is marked
2145 mark_template_parm (t, data)
2151 struct template_parm_data* tpd = (struct template_parm_data*) data;
2153 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2155 level = TEMPLATE_PARM_LEVEL (t);
2156 idx = TEMPLATE_PARM_IDX (t);
2160 level = TEMPLATE_TYPE_LEVEL (t);
2161 idx = TEMPLATE_TYPE_IDX (t);
2164 if (level == tpd->level)
2166 tpd->parms[idx] = 1;
2167 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2170 /* Return zero so that for_each_template_parm will continue the
2171 traversal of the tree; we want to mark *every* template parm. */
2175 /* Process the partial specialization DECL. */
2178 process_partial_specialization (decl)
2181 tree type = TREE_TYPE (decl);
2182 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2183 tree specargs = CLASSTYPE_TI_ARGS (type);
2184 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2185 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2186 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2187 int nargs = TREE_VEC_LENGTH (inner_args);
2188 int ntparms = TREE_VEC_LENGTH (inner_parms);
2190 int did_error_intro = 0;
2191 struct template_parm_data tpd;
2192 struct template_parm_data tpd2;
2194 /* We check that each of the template parameters given in the
2195 partial specialization is used in the argument list to the
2196 specialization. For example:
2198 template <class T> struct S;
2199 template <class T> struct S<T*>;
2201 The second declaration is OK because `T*' uses the template
2202 parameter T, whereas
2204 template <class T> struct S<int>;
2206 is no good. Even trickier is:
2217 The S2<T> declaration is actually illegal; it is a
2218 full-specialization. Of course,
2221 struct S2<T (*)(U)>;
2223 or some such would have been OK. */
2224 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2225 tpd.parms = alloca (sizeof (int) * ntparms);
2226 memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms);
2228 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
2229 memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2230 for (i = 0; i < nargs; ++i)
2232 tpd.current_arg = i;
2233 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2234 &mark_template_parm,
2237 for (i = 0; i < ntparms; ++i)
2238 if (tpd.parms[i] == 0)
2240 /* One of the template parms was not used in the
2242 if (!did_error_intro)
2244 error ("template parameters not used in partial specialization:");
2245 did_error_intro = 1;
2249 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2252 /* [temp.class.spec]
2254 The argument list of the specialization shall not be identical to
2255 the implicit argument list of the primary template. */
2256 if (comp_template_args
2258 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2260 error ("partial specialization `%T' does not specialize any template arguments", type);
2262 /* [temp.class.spec]
2264 A partially specialized non-type argument expression shall not
2265 involve template parameters of the partial specialization except
2266 when the argument expression is a simple identifier.
2268 The type of a template parameter corresponding to a specialized
2269 non-type argument shall not be dependent on a parameter of the
2271 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2273 for (i = 0; i < nargs; ++i)
2275 tree arg = TREE_VEC_ELT (inner_args, i);
2276 if (/* These first two lines are the `non-type' bit. */
2278 && TREE_CODE (arg) != TEMPLATE_DECL
2279 /* This next line is the `argument expression is not just a
2280 simple identifier' condition and also the `specialized
2281 non-type argument' bit. */
2282 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2284 if (tpd.arg_uses_template_parms[i])
2285 error ("template argument `%E' involves template parameter(s)", arg);
2288 /* Look at the corresponding template parameter,
2289 marking which template parameters its type depends
2292 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2297 /* We haven't yet initialized TPD2. Do so now. */
2298 tpd2.arg_uses_template_parms
2299 = (int*) alloca (sizeof (int) * nargs);
2300 /* The number of parameters here is the number in the
2301 main template, which, as checked in the assertion
2303 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2305 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2308 /* Mark the template parameters. But this time, we're
2309 looking for the template parameters of the main
2310 template, not in the specialization. */
2311 tpd2.current_arg = i;
2312 tpd2.arg_uses_template_parms[i] = 0;
2313 memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs);
2314 for_each_template_parm (type,
2315 &mark_template_parm,
2318 if (tpd2.arg_uses_template_parms [i])
2320 /* The type depended on some template parameters.
2321 If they are fully specialized in the
2322 specialization, that's OK. */
2324 for (j = 0; j < nargs; ++j)
2325 if (tpd2.parms[j] != 0
2326 && tpd.arg_uses_template_parms [j])
2328 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2338 if (retrieve_specialization (maintmpl, specargs))
2339 /* We've already got this specialization. */
2342 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2343 = tree_cons (inner_args, inner_parms,
2344 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2345 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2349 /* Check that a template declaration's use of default arguments is not
2350 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2351 non-zero if DECL is the thing declared by a primary template.
2352 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2355 check_default_tmpl_args (decl, parms, is_primary, is_partial)
2362 int last_level_to_check;
2367 A default template-argument shall not be specified in a
2368 function template declaration or a function template definition, nor
2369 in the template-parameter-list of the definition of a member of a
2372 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2373 /* You can't have a function template declaration in a local
2374 scope, nor you can you define a member of a class template in a
2378 if (current_class_type
2379 && !TYPE_BEING_DEFINED (current_class_type)
2380 && DECL_LANG_SPECIFIC (decl)
2381 /* If this is either a friend defined in the scope of the class
2382 or a member function. */
2383 && ((DECL_CONTEXT (decl)
2384 && same_type_p (DECL_CONTEXT (decl), current_class_type))
2385 || (DECL_FRIEND_CONTEXT (decl)
2386 && same_type_p (DECL_FRIEND_CONTEXT (decl),
2387 current_class_type)))
2388 /* And, if it was a member function, it really was defined in
2389 the scope of the class. */
2390 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl)))
2391 /* We already checked these parameters when the template was
2392 declared, so there's no need to do it again now. This function
2393 was defined in class scope, but we're processing it's body now
2394 that the class is complete. */
2399 If a template-parameter has a default template-argument, all
2400 subsequent template-parameters shall have a default
2401 template-argument supplied. */
2402 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2404 tree inner_parms = TREE_VALUE (parm_level);
2405 int ntparms = TREE_VEC_LENGTH (inner_parms);
2406 int seen_def_arg_p = 0;
2409 for (i = 0; i < ntparms; ++i)
2411 tree parm = TREE_VEC_ELT (inner_parms, i);
2412 if (TREE_PURPOSE (parm))
2414 else if (seen_def_arg_p)
2416 error ("no default argument for `%D'", TREE_VALUE (parm));
2417 /* For better subsequent error-recovery, we indicate that
2418 there should have been a default argument. */
2419 TREE_PURPOSE (parm) = error_mark_node;
2424 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2425 /* For an ordinary class template, default template arguments are
2426 allowed at the innermost level, e.g.:
2427 template <class T = int>
2429 but, in a partial specialization, they're not allowed even
2430 there, as we have in [temp.class.spec]:
2432 The template parameter list of a specialization shall not
2433 contain default template argument values.
2435 So, for a partial specialization, or for a function template,
2436 we look at all of them. */
2439 /* But, for a primary class template that is not a partial
2440 specialization we look at all template parameters except the
2442 parms = TREE_CHAIN (parms);
2444 /* Figure out what error message to issue. */
2445 if (TREE_CODE (decl) == FUNCTION_DECL)
2446 msg = "default template arguments may not be used in function templates";
2447 else if (is_partial)
2448 msg = "default template arguments may not be used in partial specializations";
2450 msg = "default argument for template parameter for class enclosing `%D'";
2452 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2453 /* If we're inside a class definition, there's no need to
2454 examine the parameters to the class itself. On the one
2455 hand, they will be checked when the class is defined, and,
2456 on the other, default arguments are legal in things like:
2457 template <class T = double>
2458 struct S { template <class U> void f(U); };
2459 Here the default argument for `S' has no bearing on the
2460 declaration of `f'. */
2461 last_level_to_check = template_class_depth (current_class_type) + 1;
2463 /* Check everything. */
2464 last_level_to_check = 0;
2466 for (parm_level = parms;
2467 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2468 parm_level = TREE_CHAIN (parm_level))
2470 tree inner_parms = TREE_VALUE (parm_level);
2474 ntparms = TREE_VEC_LENGTH (inner_parms);
2475 for (i = 0; i < ntparms; ++i)
2476 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2484 /* Clear out the default argument so that we are not
2486 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2489 /* At this point, if we're still interested in issuing messages,
2490 they must apply to classes surrounding the object declared. */
2492 msg = "default argument for template parameter for class enclosing `%D'";
2496 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2497 parameters given by current_template_args, or reuses a
2498 previously existing one, if appropriate. Returns the DECL, or an
2499 equivalent one, if it is replaced via a call to duplicate_decls.
2501 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2504 push_template_decl_real (decl, is_friend)
2514 int new_template_p = 0;
2516 /* See if this is a partial specialization. */
2517 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
2518 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2519 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
2521 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
2524 /* For a friend, we want the context of the friend function, not
2525 the type of which it is a friend. */
2526 ctx = DECL_CONTEXT (decl);
2527 else if (CP_DECL_CONTEXT (decl)
2528 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
2529 /* In the case of a virtual function, we want the class in which
2531 ctx = CP_DECL_CONTEXT (decl);
2533 /* Otherwise, if we're currently defining some class, the DECL
2534 is assumed to be a member of the class. */
2535 ctx = current_scope ();
2537 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2540 if (!DECL_CONTEXT (decl))
2541 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2543 /* See if this is a primary template. */
2544 primary = template_parm_scope_p ();
2548 if (current_lang_name == lang_name_c)
2549 error ("template with C linkage");
2550 else if (TREE_CODE (decl) == TYPE_DECL
2551 && ANON_AGGRNAME_P (DECL_NAME (decl)))
2552 error ("template class without a name");
2553 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2554 && CLASS_TYPE_P (TREE_TYPE (decl)))
2555 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2556 || TREE_CODE (decl) == FUNCTION_DECL)
2559 error ("template declaration of `%#D'", decl);
2562 /* Check to see that the rules regarding the use of default
2563 arguments are not being violated. */
2564 check_default_tmpl_args (decl, current_template_parms,
2565 primary, is_partial);
2568 return process_partial_specialization (decl);
2570 args = current_template_args ();
2573 || TREE_CODE (ctx) == FUNCTION_DECL
2574 || TYPE_BEING_DEFINED (ctx)
2575 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
2577 if (DECL_LANG_SPECIFIC (decl)
2578 && DECL_TEMPLATE_INFO (decl)
2579 && DECL_TI_TEMPLATE (decl))
2580 tmpl = DECL_TI_TEMPLATE (decl);
2581 /* If DECL is a TYPE_DECL for a class-template, then there won't
2582 be DECL_LANG_SPECIFIC. The information equivalent to
2583 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2584 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2585 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2586 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2588 /* Since a template declaration already existed for this
2589 class-type, we must be redeclaring it here. Make sure
2590 that the redeclaration is legal. */
2591 redeclare_class_template (TREE_TYPE (decl),
2592 current_template_parms);
2593 /* We don't need to create a new TEMPLATE_DECL; just use the
2594 one we already had. */
2595 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2599 tmpl = build_template_decl (decl, current_template_parms);
2602 if (DECL_LANG_SPECIFIC (decl)
2603 && DECL_TEMPLATE_SPECIALIZATION (decl))
2605 /* A specialization of a member template of a template
2607 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2608 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2609 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2615 tree a, t, current, parms;
2618 if (TREE_CODE (decl) == TYPE_DECL)
2620 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2621 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2622 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2623 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2624 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2627 error ("`%D' does not declare a template type", decl);
2631 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
2633 error ("template definition of non-template `%#D'", decl);
2637 tmpl = DECL_TI_TEMPLATE (decl);
2639 if (is_member_template (tmpl)
2640 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2641 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2642 && DECL_TEMPLATE_SPECIALIZATION (decl))
2646 /* The declaration is a specialization of a member
2647 template, declared outside the class. Therefore, the
2648 innermost template arguments will be NULL, so we
2649 replace them with the arguments determined by the
2650 earlier call to check_explicit_specialization. */
2651 args = DECL_TI_ARGS (decl);
2654 = build_template_decl (decl, current_template_parms);
2655 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2656 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2657 DECL_TI_TEMPLATE (decl) = new_tmpl;
2658 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2659 DECL_TEMPLATE_INFO (new_tmpl)
2660 = tree_cons (tmpl, args, NULL_TREE);
2662 register_specialization (new_tmpl,
2663 most_general_template (tmpl),
2668 /* Make sure the template headers we got make sense. */
2670 parms = DECL_TEMPLATE_PARMS (tmpl);
2671 i = TMPL_PARMS_DEPTH (parms);
2672 if (TMPL_ARGS_DEPTH (args) != i)
2674 error ("expected %d levels of template parms for `%#D', got %d",
2675 i, decl, TMPL_ARGS_DEPTH (args));
2678 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2680 a = TMPL_ARGS_LEVEL (args, i);
2681 t = INNERMOST_TEMPLATE_PARMS (parms);
2683 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2685 if (current == decl)
2686 error ("got %d template parameters for `%#D'",
2687 TREE_VEC_LENGTH (a), decl);
2689 error ("got %d template parameters for `%#T'",
2690 TREE_VEC_LENGTH (a), current);
2691 error (" but %d required", TREE_VEC_LENGTH (t));
2694 /* Perhaps we should also check that the parms are used in the
2695 appropriate qualifying scopes in the declarator? */
2697 if (current == decl)
2700 current = TYPE_CONTEXT (current);
2704 DECL_TEMPLATE_RESULT (tmpl) = decl;
2705 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2707 /* Push template declarations for global functions and types. Note
2708 that we do not try to push a global template friend declared in a
2709 template class; such a thing may well depend on the template
2710 parameters of the class. */
2711 if (new_template_p && !ctx
2712 && !(is_friend && template_class_depth (current_class_type) > 0))
2713 tmpl = pushdecl_namespace_level (tmpl);
2716 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2718 info = tree_cons (tmpl, args, NULL_TREE);
2720 if (DECL_IMPLICIT_TYPEDEF_P (decl))
2722 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2723 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2724 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2725 /* Don't change the name if we've already set it up. */
2726 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
2727 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2729 else if (DECL_LANG_SPECIFIC (decl))
2730 DECL_TEMPLATE_INFO (decl) = info;
2732 return DECL_TEMPLATE_RESULT (tmpl);
2736 push_template_decl (decl)
2739 return push_template_decl_real (decl, 0);
2742 /* Called when a class template TYPE is redeclared with the indicated
2743 template PARMS, e.g.:
2745 template <class T> struct S;
2746 template <class T> struct S {}; */
2749 redeclare_class_template (type, parms)
2757 if (!TYPE_TEMPLATE_INFO (type))
2759 error ("`%T' is not a template type", type);
2763 tmpl = TYPE_TI_TEMPLATE (type);
2764 if (!PRIMARY_TEMPLATE_P (tmpl))
2765 /* The type is nested in some template class. Nothing to worry
2766 about here; there are no new template parameters for the nested
2770 parms = INNERMOST_TEMPLATE_PARMS (parms);
2771 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2773 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2775 cp_error_at ("previous declaration `%D'", tmpl);
2776 error ("used %d template parameter%s instead of %d",
2777 TREE_VEC_LENGTH (tmpl_parms),
2778 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2779 TREE_VEC_LENGTH (parms));
2783 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2785 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2786 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2787 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2788 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2790 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2792 cp_error_at ("template parameter `%#D'", tmpl_parm);
2793 error ("redeclared here as `%#D'", parm);
2797 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2799 /* We have in [temp.param]:
2801 A template-parameter may not be given default arguments
2802 by two different declarations in the same scope. */
2803 error ("redefinition of default argument for `%#D'", parm);
2804 cp_error_at (" original definition appeared here", tmpl_parm);
2808 if (parm_default != NULL_TREE)
2809 /* Update the previous template parameters (which are the ones
2810 that will really count) with the new default value. */
2811 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2812 else if (tmpl_default != NULL_TREE)
2813 /* Update the new parameters, too; they'll be used as the
2814 parameters for any members. */
2815 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
2819 /* Attempt to convert the non-type template parameter EXPR to the
2820 indicated TYPE. If the conversion is successful, return the
2821 converted value. If the conversion is unsuccessful, return
2822 NULL_TREE if we issued an error message, or error_mark_node if we
2823 did not. We issue error messages for out-and-out bad template
2824 parameters, but not simply because the conversion failed, since we
2825 might be just trying to do argument deduction. By the time this
2826 function is called, neither TYPE nor EXPR may make use of template
2830 convert_nontype_argument (type, expr)
2834 tree expr_type = TREE_TYPE (expr);
2836 /* A template-argument for a non-type, non-template
2837 template-parameter shall be one of:
2839 --an integral constant-expression of integral or enumeration
2842 --the name of a non-type template-parameter; or
2844 --the name of an object or function with external linkage,
2845 including function templates and function template-ids but
2846 excluding non-static class members, expressed as id-expression;
2849 --the address of an object or function with external linkage,
2850 including function templates and function template-ids but
2851 excluding non-static class members, expressed as & id-expression
2852 where the & is optional if the name refers to a function or
2855 --a pointer to member expressed as described in _expr.unary.op_. */
2857 /* An integral constant-expression can include const variables or
2858 enumerators. Simplify things by folding them to their values,
2859 unless we're about to bind the declaration to a reference
2861 if (INTEGRAL_TYPE_P (expr_type)
2862 && TREE_CODE (type) != REFERENCE_TYPE)
2863 expr = decl_constant_value (expr);
2865 if (is_overloaded_fn (expr))
2866 /* OK for now. We'll check that it has external linkage later.
2867 Check this first since if expr_type is the unknown_type_node
2868 we would otherwise complain below. */
2870 else if (TYPE_PTRMEM_P (expr_type)
2871 || TYPE_PTRMEMFUNC_P (expr_type))
2873 if (TREE_CODE (expr) != PTRMEM_CST)
2876 else if (TYPE_PTR_P (expr_type)
2877 || TYPE_PTRMEM_P (expr_type)
2878 || TREE_CODE (expr_type) == ARRAY_TYPE
2879 || TREE_CODE (type) == REFERENCE_TYPE
2880 /* If expr is the address of an overloaded function, we
2881 will get the unknown_type_node at this point. */
2882 || expr_type == unknown_type_node)
2888 if (TREE_CODE (expr_type) == ARRAY_TYPE
2889 || (TREE_CODE (type) == REFERENCE_TYPE
2890 && TREE_CODE (e) != ADDR_EXPR))
2894 if (TREE_CODE (e) != ADDR_EXPR)
2897 error ("`%E' is not a valid template argument", expr);
2898 if (TYPE_PTR_P (expr_type))
2900 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2901 error ("it must be the address of a function with external linkage");
2903 error ("it must be the address of an object with external linkage");
2905 else if (TYPE_PTRMEM_P (expr_type)
2906 || TYPE_PTRMEMFUNC_P (expr_type))
2907 error ("it must be a pointer-to-member of the form `&X::Y'");
2912 referent = TREE_OPERAND (e, 0);
2913 STRIP_NOPS (referent);
2916 if (TREE_CODE (referent) == STRING_CST)
2918 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2923 if (is_overloaded_fn (referent))
2924 /* We'll check that it has external linkage later. */
2926 else if (TREE_CODE (referent) != VAR_DECL)
2928 else if (!DECL_EXTERNAL_LINKAGE_P (referent))
2930 error ("address of non-extern `%E' cannot be used as template argument", referent);
2931 return error_mark_node;
2934 else if (INTEGRAL_TYPE_P (expr_type)
2935 || TYPE_PTRMEM_P (expr_type)
2936 || TYPE_PTRMEMFUNC_P (expr_type))
2938 if (! TREE_CONSTANT (expr))
2941 error ("non-constant `%E' cannot be used as template argument",
2948 error ("object `%E' cannot be used as template argument", expr);
2952 switch (TREE_CODE (type))
2957 /* For a non-type template-parameter of integral or enumeration
2958 type, integral promotions (_conv.prom_) and integral
2959 conversions (_conv.integral_) are applied. */
2960 if (!INTEGRAL_TYPE_P (expr_type))
2961 return error_mark_node;
2963 /* It's safe to call digest_init in this case; we know we're
2964 just converting one integral constant expression to another. */
2965 expr = digest_init (type, expr, (tree*) 0);
2967 if (TREE_CODE (expr) != INTEGER_CST)
2968 /* Curiously, some TREE_CONSTANT integral expressions do not
2969 simplify to integer constants. For example, `3 % 0',
2970 remains a TRUNC_MOD_EXPR. */
2977 tree type_pointed_to = TREE_TYPE (type);
2979 if (TYPE_PTRMEM_P (type))
2983 /* For a non-type template-parameter of type pointer to data
2984 member, qualification conversions (_conv.qual_) are
2986 e = perform_qualification_conversions (type, expr);
2987 if (TREE_CODE (e) == NOP_EXPR)
2988 /* The call to perform_qualification_conversions will
2989 insert a NOP_EXPR over EXPR to do express conversion,
2990 if necessary. But, that will confuse us if we use
2991 this (converted) template parameter to instantiate
2992 another template; then the thing will not look like a
2993 valid template argument. So, just make a new
2994 constant, of the appropriate type. */
2995 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
2998 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
3000 /* For a non-type template-parameter of type pointer to
3001 function, only the function-to-pointer conversion
3002 (_conv.func_) is applied. If the template-argument
3003 represents a set of overloaded functions (or a pointer to
3004 such), the matching function is selected from the set
3009 if (TREE_CODE (expr) == ADDR_EXPR)
3010 fns = TREE_OPERAND (expr, 0);
3014 fn = instantiate_type (type_pointed_to, fns, itf_none);
3016 if (fn == error_mark_node)
3017 return error_mark_node;
3019 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3021 if (really_overloaded_fn (fns))
3022 return error_mark_node;
3027 expr = build_unary_op (ADDR_EXPR, fn, 0);
3029 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3035 /* For a non-type template-parameter of type pointer to
3036 object, qualification conversions (_conv.qual_) and the
3037 array-to-pointer conversion (_conv.array_) are applied.
3038 [Note: In particular, neither the null pointer conversion
3039 (_conv.ptr_) nor the derived-to-base conversion
3040 (_conv.ptr_) are applied. Although 0 is a valid
3041 template-argument for a non-type template-parameter of
3042 integral type, it is not a valid template-argument for a
3043 non-type template-parameter of pointer type.]
3045 The call to decay_conversion performs the
3046 array-to-pointer conversion, if appropriate. */
3047 expr = decay_conversion (expr);
3049 if (expr == error_mark_node)
3050 return error_mark_node;
3052 return perform_qualification_conversions (type, expr);
3057 case REFERENCE_TYPE:
3059 tree type_referred_to = TREE_TYPE (type);
3061 /* If this expression already has reference type, get the
3062 underling object. */
3063 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
3065 my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604);
3066 expr = TREE_OPERAND (expr, 0);
3067 expr_type = TREE_TYPE (expr);
3070 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
3072 /* For a non-type template-parameter of type reference to
3073 function, no conversions apply. If the
3074 template-argument represents a set of overloaded
3075 functions, the matching function is selected from the
3076 set (_over.over_). */
3079 fn = instantiate_type (type_referred_to, expr, itf_none);
3081 if (fn == error_mark_node)
3082 return error_mark_node;
3084 if (!DECL_EXTERNAL_LINKAGE_P (fn))
3086 if (really_overloaded_fn (expr))
3087 /* Don't issue an error here; we might get a different
3088 function if the overloading had worked out
3090 return error_mark_node;
3095 my_friendly_assert (same_type_p (type_referred_to,
3103 /* For a non-type template-parameter of type reference to
3104 object, no conversions apply. The type referred to by the
3105 reference may be more cv-qualified than the (otherwise
3106 identical) type of the template-argument. The
3107 template-parameter is bound directly to the
3108 template-argument, which must be an lvalue. */
3109 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type),
3110 TYPE_MAIN_VARIANT (type_referred_to))
3111 || !at_least_as_qualified_p (type_referred_to,
3113 || !real_lvalue_p (expr))
3114 return error_mark_node;
3117 mark_addressable (expr);
3118 return build1 (ADDR_EXPR, type, expr);
3124 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112);
3126 /* For a non-type template-parameter of type pointer to member
3127 function, no conversions apply. If the template-argument
3128 represents a set of overloaded member functions, the
3129 matching member function is selected from the set
3132 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3133 expr_type != unknown_type_node)
3134 return error_mark_node;
3136 if (TREE_CODE (expr) == PTRMEM_CST)
3138 /* A ptr-to-member constant. */
3139 if (!same_type_p (type, expr_type))
3140 return error_mark_node;
3145 if (TREE_CODE (expr) != ADDR_EXPR)
3146 return error_mark_node;
3148 expr = instantiate_type (type, expr, itf_none);
3150 if (expr == error_mark_node)
3151 return error_mark_node;
3153 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
3160 /* All non-type parameters must have one of these types. */
3165 return error_mark_node;
3168 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3169 template template parameters. Both PARM_PARMS and ARG_PARMS are
3170 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3173 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3174 the case, then extra parameters must have default arguments.
3176 Consider the example:
3177 template <class T, class Allocator = allocator> class vector;
3178 template<template <class U> class TT> class C;
3180 C<vector> is a valid instantiation. PARM_PARMS for the above code
3181 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3182 T and Allocator) and OUTER_ARGS contains the argument that is used to
3183 substitute the TT parameter. */
3186 coerce_template_template_parms (parm_parms, arg_parms, complain,
3187 in_decl, outer_args)
3188 tree parm_parms, arg_parms;
3190 tree in_decl, outer_args;
3192 int nparms, nargs, i;
3195 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3196 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3198 nparms = TREE_VEC_LENGTH (parm_parms);
3199 nargs = TREE_VEC_LENGTH (arg_parms);
3201 /* The rule here is opposite of coerce_template_parms. */
3204 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3207 for (i = 0; i < nparms; ++i)
3209 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3210 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3212 if (arg == NULL_TREE || arg == error_mark_node
3213 || parm == NULL_TREE || parm == error_mark_node)
3216 if (TREE_CODE (arg) != TREE_CODE (parm))
3219 switch (TREE_CODE (parm))
3225 /* We encounter instantiations of templates like
3226 template <template <template <class> class> class TT>
3229 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3230 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3232 if (!coerce_template_template_parms (parmparm, argparm,
3240 /* The tsubst call is used to handle cases such as
3241 template <class T, template <T> class TT> class D;
3242 i.e. the parameter list of TT depends on earlier parameters. */
3243 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3256 /* Convert the indicated template ARG as necessary to match the
3257 indicated template PARM. Returns the converted ARG, or
3258 error_mark_node if the conversion was unsuccessful. Error messages
3259 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3260 parameter in the parameter list. ARGS is the full set of template
3261 arguments deduced so far. */
3264 convert_template_argument (parm, arg, args, complain, i, in_decl)
3274 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3276 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3278 if (TREE_CODE (arg) == TREE_LIST
3279 && TREE_TYPE (arg) != NULL_TREE
3280 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3282 /* The template argument was the name of some
3283 member function. That's usually
3284 illegal, but static members are OK. In any
3285 case, grab the underlying fields/functions
3286 and issue an error later if required. */
3287 arg = TREE_VALUE (arg);
3288 TREE_TYPE (arg) = unknown_type_node;
3291 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3292 requires_type = (TREE_CODE (parm) == TYPE_DECL
3293 || requires_tmpl_type);
3295 if (TREE_CODE (arg) != RECORD_TYPE)
3296 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3297 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3298 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3299 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3300 else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg)
3301 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg)))
3303 if (is_base_of_enclosing_class (arg, current_class_type))
3304 /* This is a template name used within the scope of the
3305 template. It could be the template, or it could be the
3306 instantiation. Choose whichever makes sense. */
3307 is_tmpl_type = requires_tmpl_type;
3312 /* It is a non-template class, or a specialization of a template
3313 class, or a non-template member of a template class. */
3317 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3318 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3319 arg = TYPE_STUB_DECL (arg);
3320 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3321 arg = CLASSTYPE_TI_TEMPLATE (arg);
3323 is_type = TYPE_P (arg) || is_tmpl_type;
3325 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3326 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3328 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
3330 arg = make_typename_type (TREE_OPERAND (arg, 0),
3331 TREE_OPERAND (arg, 1),
3335 if (is_type != requires_type)
3341 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3344 error (" expected a constant of type `%T', got `%T'",
3346 (is_tmpl_type ? DECL_NAME (arg) : arg));
3348 error (" expected a type, got `%E'", arg);
3351 return error_mark_node;
3353 if (is_tmpl_type ^ requires_tmpl_type)
3355 if (in_decl && complain)
3357 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3360 error (" expected a type, got `%T'", DECL_NAME (arg));
3362 error (" expected a class template, got `%T'", arg);
3364 return error_mark_node;
3369 if (requires_tmpl_type)
3371 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3372 /* The number of argument required is not known yet.
3373 Just accept it for now. */
3374 val = TREE_TYPE (arg);
3377 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3378 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3380 if (coerce_template_template_parms (parmparm, argparm,
3386 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3388 if (val != error_mark_node
3389 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3390 val = TREE_TYPE (val);
3394 if (in_decl && complain)
3396 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3398 error (" expected a template of type `%D', got `%D'", parm, arg);
3401 val = error_mark_node;
3407 val = groktypename (arg);
3408 if (! processing_template_decl)
3410 /* [basic.link]: A name with no linkage (notably, the
3411 name of a class or enumeration declared in a local
3412 scope) shall not be used to declare an entity with
3413 linkage. This implies that names with no linkage
3414 cannot be used as template arguments. */
3415 tree t = no_linkage_check (val);
3418 if (TYPE_ANONYMOUS_P (t))
3420 ("template-argument `%T' uses anonymous type", val);
3423 ("template-argument `%T' uses local type `%T'",
3425 return error_mark_node;
3432 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
3434 if (invalid_nontype_parm_type_p (t, complain))
3435 return error_mark_node;
3437 if (processing_template_decl)
3438 arg = maybe_fold_nontype_arg (arg);
3440 if (!uses_template_parms (arg) && !uses_template_parms (t))
3441 /* We used to call digest_init here. However, digest_init
3442 will report errors, which we don't want when complain
3443 is zero. More importantly, digest_init will try too
3444 hard to convert things: for example, `0' should not be
3445 converted to pointer type at this point according to
3446 the standard. Accepting this is not merely an
3447 extension, since deciding whether or not these
3448 conversions can occur is part of determining which
3449 function template to call, or whether a given explicit
3450 argument specification is legal. */
3451 val = convert_nontype_argument (t, arg);
3455 if (val == NULL_TREE)
3456 val = error_mark_node;
3457 else if (val == error_mark_node && complain)
3458 error ("could not convert template argument `%E' to `%T'",
3465 /* Convert all template arguments to their appropriate types, and
3466 return a vector containing the innermost resulting template
3467 arguments. If any error occurs, return error_mark_node, and, if
3468 COMPLAIN is non-zero, issue an error message. Some error messages
3469 are issued even if COMPLAIN is zero; for instance, if a template
3470 argument is composed from a local class.
3472 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3473 provided in ARGLIST, or else trailing parameters must have default
3474 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3475 deduction for any unspecified trailing arguments. */
3478 coerce_template_parms (parms, args, in_decl,
3480 require_all_arguments)
3484 int require_all_arguments;
3486 int nparms, nargs, i, lost = 0;
3489 tree new_inner_args;
3491 inner_args = INNERMOST_TEMPLATE_ARGS (args);
3492 nargs = NUM_TMPL_ARGS (inner_args);
3493 nparms = TREE_VEC_LENGTH (parms);
3497 && require_all_arguments
3498 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
3502 error ("wrong number of template arguments (%d, should be %d)",
3506 cp_error_at ("provided for `%D'", in_decl);
3509 return error_mark_node;
3512 new_inner_args = make_tree_vec (nparms);
3513 new_args = add_outermost_template_args (args, new_inner_args);
3514 for (i = 0; i < nparms; i++)
3519 /* Get the Ith template parameter. */
3520 parm = TREE_VEC_ELT (parms, i);
3522 /* Calculate the Ith argument. */
3523 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
3525 arg = TREE_VALUE (inner_args);
3526 inner_args = TREE_CHAIN (inner_args);
3529 arg = TREE_VEC_ELT (inner_args, i);
3530 /* If no template argument was supplied, look for a default
3532 else if (TREE_PURPOSE (parm) == NULL_TREE)
3534 /* There was no default value. */
3535 my_friendly_assert (!require_all_arguments, 0);
3538 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3539 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
3541 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3544 /* Now, convert the Ith argument, as necessary. */
3545 if (arg == NULL_TREE)
3546 /* We're out of arguments. */
3548 my_friendly_assert (!require_all_arguments, 0);
3551 else if (arg == error_mark_node)
3553 error ("template argument %d is invalid", i + 1);
3554 arg = error_mark_node;
3557 arg = convert_template_argument (TREE_VALUE (parm),
3558 arg, new_args, complain, i,
3561 if (arg == error_mark_node)
3563 TREE_VEC_ELT (new_inner_args, i) = arg;
3567 return error_mark_node;
3569 return new_inner_args;
3572 /* Returns 1 if template args OT and NT are equivalent. */
3575 template_args_equal (ot, nt)
3581 if (TREE_CODE (nt) == TREE_VEC)
3582 /* For member templates */
3583 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
3584 else if (TYPE_P (nt))
3585 return TYPE_P (ot) && same_type_p (ot, nt);
3586 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
3589 return (cp_tree_equal (ot, nt) > 0);
3592 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3593 of template arguments. Returns 0 otherwise. */
3596 comp_template_args (oldargs, newargs)
3597 tree oldargs, newargs;
3601 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3604 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3606 tree nt = TREE_VEC_ELT (newargs, i);
3607 tree ot = TREE_VEC_ELT (oldargs, i);
3609 if (! template_args_equal (ot, nt))
3615 /* Given class template name and parameter list, produce a user-friendly name
3616 for the instantiation. */
3619 mangle_class_name_for_template (name, parms, arglist)
3621 tree parms, arglist;
3623 static struct obstack scratch_obstack;
3624 static char *scratch_firstobj;
3627 if (!scratch_firstobj)
3628 gcc_obstack_init (&scratch_obstack);
3630 obstack_free (&scratch_obstack, scratch_firstobj);
3631 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
3633 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3634 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3638 nparms = TREE_VEC_LENGTH (parms);
3639 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
3640 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3641 for (i = 0; i < nparms; i++)
3643 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3644 tree arg = TREE_VEC_ELT (arglist, i);
3649 if (TREE_CODE (parm) == TYPE_DECL)
3651 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3654 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3656 if (TREE_CODE (arg) == TEMPLATE_DECL)
3658 /* Already substituted with real template. Just output
3659 the template name here */
3660 tree context = DECL_CONTEXT (arg);
3663 /* The template may be defined in a namespace, or
3664 may be a member template. */
3665 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3666 || CLASS_TYPE_P (context),
3668 cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER));
3671 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3674 /* Output the parameter declaration */
3675 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
3679 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3681 if (TREE_CODE (arg) == TREE_LIST)
3683 /* New list cell was built because old chain link was in
3685 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3686 arg = TREE_VALUE (arg);
3688 /* No need to check arglist against parmlist here; we did that
3689 in coerce_template_parms, called from lookup_template_class. */
3690 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
3693 char *bufp = obstack_next_free (&scratch_obstack);
3695 while (bufp[offset - 1] == ' ')
3697 obstack_blank_fast (&scratch_obstack, offset);
3699 /* B<C<char> >, not B<C<char>> */
3700 if (bufp[offset - 1] == '>')
3705 return (char *) obstack_base (&scratch_obstack);
3709 classtype_mangled_name (t)
3712 if (CLASSTYPE_TEMPLATE_INFO (t)
3713 /* Specializations have already had their names set up in
3714 lookup_template_class. */
3715 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3717 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3719 /* For non-primary templates, the template parameters are
3720 implicit from their surrounding context. */
3721 if (PRIMARY_TEMPLATE_P (tmpl))
3723 tree name = DECL_NAME (tmpl);
3724 char *mangled_name = mangle_class_name_for_template
3725 (IDENTIFIER_POINTER (name),
3726 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3727 CLASSTYPE_TI_ARGS (t));
3728 tree id = get_identifier (mangled_name);
3729 IDENTIFIER_TEMPLATE (id) = name;
3734 return TYPE_IDENTIFIER (t);
3738 add_pending_template (d)
3741 tree ti = (TYPE_P (d)
3742 ? CLASSTYPE_TEMPLATE_INFO (d)
3743 : DECL_TEMPLATE_INFO (d));
3747 if (TI_PENDING_TEMPLATE_FLAG (ti))
3750 /* We are called both from instantiate_decl, where we've already had a
3751 tinst_level pushed, and instantiate_template, where we haven't.
3753 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
3756 push_tinst_level (d);
3758 pt = tree_cons (current_tinst_level, d, NULL_TREE);
3759 if (last_pending_template)
3760 TREE_CHAIN (last_pending_template) = pt;
3762 pending_templates = pt;
3764 last_pending_template = pt;
3766 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3773 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3774 may be either a _DECL or an overloaded function or an
3775 IDENTIFIER_NODE), and ARGLIST. */
3778 lookup_template_function (fns, arglist)
3783 if (fns == NULL_TREE)
3785 error ("non-template used as template");
3786 return error_mark_node;
3789 type = TREE_TYPE (fns);
3790 if (TREE_CODE (fns) == OVERLOAD || !type)
3791 type = unknown_type_node;
3793 if (processing_template_decl)
3794 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3796 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3799 /* Within the scope of a template class S<T>, the name S gets bound
3800 (in build_self_reference) to a TYPE_DECL for the class, not a
3801 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3802 or one of its enclosing classes, and that type is a template,
3803 return the associated TEMPLATE_DECL. Otherwise, the original
3804 DECL is returned. */
3807 maybe_get_template_decl_from_type_decl (decl)
3810 return (decl != NULL_TREE
3811 && TREE_CODE (decl) == TYPE_DECL
3812 && DECL_ARTIFICIAL (decl)
3813 && CLASS_TYPE_P (TREE_TYPE (decl))
3814 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3815 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3818 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3819 parameters, find the desired type.
3821 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3822 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3823 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3826 IN_DECL, if non-NULL, is the template declaration we are trying to
3829 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3830 the class we are looking up.
3832 If COMPLAIN is non-zero, issue error messages.
3834 If the template class is really a local class in a template
3835 function, then the FUNCTION_CONTEXT is the function in which it is
3836 being instantiated. */
3839 lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain)
3846 tree template = NULL_TREE, parmlist;
3849 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3851 if (IDENTIFIER_VALUE (d1)
3852 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3853 template = IDENTIFIER_VALUE (d1);
3857 push_decl_namespace (context);
3858 template = lookup_name (d1, /*prefer_type=*/0);
3859 template = maybe_get_template_decl_from_type_decl (template);
3861 pop_decl_namespace ();
3864 context = DECL_CONTEXT (template);
3866 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3868 tree type = TREE_TYPE (d1);
3870 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3871 an implicit typename for the second A. Deal with it. */
3872 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3873 type = TREE_TYPE (type);
3875 if (CLASSTYPE_TEMPLATE_INFO (type))
3877 template = CLASSTYPE_TI_TEMPLATE (type);
3878 d1 = DECL_NAME (template);
3881 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3882 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
3884 template = TYPE_TI_TEMPLATE (d1);
3885 d1 = DECL_NAME (template);
3887 else if (TREE_CODE (d1) == TEMPLATE_DECL
3888 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
3891 d1 = DECL_NAME (template);
3892 context = DECL_CONTEXT (template);
3895 /* With something like `template <class T> class X class X { ... };'
3896 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3897 We don't want to do that, but we have to deal with the situation,
3898 so let's give them some syntax errors to chew on instead of a
3899 crash. Alternatively D1 might not be a template type at all. */
3903 error ("`%T' is not a template", d1);
3904 return error_mark_node;
3907 if (TREE_CODE (template) != TEMPLATE_DECL
3908 /* If we're called from the parser, make sure it's a user visible
3910 || ((!arglist || TREE_CODE (arglist) == TREE_LIST)
3911 && !DECL_TEMPLATE_PARM_P (template)
3912 && !PRIMARY_TEMPLATE_P (template)))
3916 error ("non-template type `%T' used as a template", d1);
3918 cp_error_at ("for template declaration `%D'", in_decl);
3920 return error_mark_node;
3923 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3925 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3926 template arguments */
3931 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3933 /* Consider an example where a template template parameter declared as
3935 template <class T, class U = std::allocator<T> > class TT
3937 The template parameter level of T and U are one level larger than
3938 of TT. To proper process the default argument of U, say when an
3939 instantiation `TT<int>' is seen, we need to build the full
3940 arguments containing {int} as the innermost level. Outer levels
3941 can be obtained from `current_template_args ()'. */
3943 if (processing_template_decl)
3944 arglist = add_to_template_args (current_template_args (), arglist);
3946 arglist2 = coerce_template_parms (parmlist, arglist, template,
3947 complain, /*require_all_args=*/1);
3948 if (arglist2 == error_mark_node)
3949 return error_mark_node;
3951 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
3956 tree template_type = TREE_TYPE (template);
3959 tree found = NULL_TREE;
3963 int is_partial_instantiation;
3965 gen_tmpl = most_general_template (template);
3966 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
3967 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3968 arg_depth = TMPL_ARGS_DEPTH (arglist);
3970 if (arg_depth == 1 && parm_depth > 1)
3972 /* We've been given an incomplete set of template arguments.
3975 template <class T> struct S1 {
3976 template <class U> struct S2 {};
3977 template <class U> struct S2<U*> {};
3980 we will be called with an ARGLIST of `U*', but the
3981 TEMPLATE will be `template <class T> template
3982 <class U> struct S1<T>::S2'. We must fill in the missing
3985 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3987 arg_depth = TMPL_ARGS_DEPTH (arglist);
3990 /* Now we should have enough arguments. */
3991 my_friendly_assert (parm_depth == arg_depth, 0);
3993 /* From here on, we're only interested in the most general
3995 template = gen_tmpl;
3997 /* Calculate the BOUND_ARGS. These will be the args that are
3998 actually tsubst'd into the definition to create the
4002 /* We have multiple levels of arguments to coerce, at once. */
4004 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4006 tree bound_args = make_tree_vec (parm_depth);
4008 for (i = saved_depth,
4009 t = DECL_TEMPLATE_PARMS (template);
4010 i > 0 && t != NULL_TREE;
4011 --i, t = TREE_CHAIN (t))
4013 tree a = coerce_template_parms (TREE_VALUE (t),
4015 complain, /*require_all_args=*/1);
4016 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4018 /* We temporarily reduce the length of the ARGLIST so
4019 that coerce_template_parms will see only the arguments
4020 corresponding to the template parameters it is
4022 TREE_VEC_LENGTH (arglist)--;
4025 /* Restore the ARGLIST to its full size. */
4026 TREE_VEC_LENGTH (arglist) = saved_depth;
4028 arglist = bound_args;
4032 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4033 INNERMOST_TEMPLATE_ARGS (arglist),
4035 complain, /*require_all_args=*/1);
4037 if (arglist == error_mark_node)
4038 /* We were unable to bind the arguments. */
4039 return error_mark_node;
4041 /* In the scope of a template class, explicit references to the
4042 template class refer to the type of the template, not any
4043 instantiation of it. For example, in:
4045 template <class T> class C { void f(C<T>); }
4047 the `C<T>' is just the same as `C'. Outside of the
4048 class, however, such a reference is an instantiation. */
4049 if (comp_template_args (TYPE_TI_ARGS (template_type),
4052 found = template_type;
4054 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4058 /* Note that we use DECL_CONTEXT, rather than
4059 CP_DECL_CONTEXT, so that the termination test is
4060 always just `ctx'. We're not interested in namespace
4062 for (ctx = current_class_type;
4064 ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
4065 if (same_type_p (ctx, template_type))
4069 /* We're not in the scope of the class, so the
4070 TEMPLATE_TYPE is not the type we want after
4078 for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
4080 tp = &TREE_CHAIN (*tp))
4081 if (comp_template_args (TREE_PURPOSE (*tp), arglist))
4085 /* Use the move-to-front heuristic to speed up future
4087 *tp = TREE_CHAIN (*tp);
4089 = DECL_TEMPLATE_INSTANTIATIONS (template);
4090 DECL_TEMPLATE_INSTANTIATIONS (template) = found;
4092 return TREE_VALUE (found);
4095 /* This type is a "partial instantiation" if any of the template
4096 arguments still involve template parameters. Note that we set
4097 IS_PARTIAL_INSTANTIATION for partial specializations as
4099 is_partial_instantiation = uses_template_parms (arglist);
4101 if (!is_partial_instantiation
4102 && !PRIMARY_TEMPLATE_P (template)
4103 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4105 found = xref_tag_from_type (TREE_TYPE (template),
4106 DECL_NAME (template),
4111 context = tsubst (DECL_CONTEXT (template), arglist,
4112 /*complain=*/0, in_decl);
4114 context = global_namespace;
4116 /* Create the type. */
4117 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4119 if (!is_partial_instantiation)
4121 set_current_access_from_decl (TYPE_NAME (template_type));
4122 t = start_enum (TYPE_IDENTIFIER (template_type));
4125 /* We don't want to call start_enum for this type, since
4126 the values for the enumeration constants may involve
4127 template parameters. And, no one should be interested
4128 in the enumeration constants for such a type. */
4129 t = make_node (ENUMERAL_TYPE);
4133 t = make_aggr_type (TREE_CODE (template_type));
4134 CLASSTYPE_DECLARED_CLASS (t)
4135 = CLASSTYPE_DECLARED_CLASS (template_type);
4136 CLASSTYPE_GOT_SEMICOLON (t) = 1;
4137 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4138 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4140 /* A local class. Make sure the decl gets registered properly. */
4141 if (context == current_function_decl)
4142 pushtag (DECL_NAME (template), t, 0);
4145 /* If we called start_enum or pushtag above, this information
4146 will already be set up. */
4149 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4151 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4152 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4153 TYPE_STUB_DECL (t) = type_decl;
4154 DECL_SOURCE_FILE (type_decl)
4155 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4156 DECL_SOURCE_LINE (type_decl)
4157 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
4160 type_decl = TYPE_NAME (t);
4162 /* Set up the template information. We have to figure out which
4163 template is the immediate parent if this is a full
4165 if (parm_depth == 1 || is_partial_instantiation
4166 || !PRIMARY_TEMPLATE_P (template))
4167 /* This case is easy; there are no member templates involved. */
4171 /* This is a full instantiation of a member template. Look
4172 for a partial instantiation of which this is an instance. */
4174 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4175 found; found = TREE_CHAIN (found))
4178 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4180 /* We only want partial instantiations, here, not
4181 specializations or full instantiations. */
4182 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4183 || !uses_template_parms (TREE_VALUE (found)))
4186 /* Temporarily reduce by one the number of levels in the
4187 ARGLIST and in FOUND so as to avoid comparing the
4188 last set of arguments. */
4189 TREE_VEC_LENGTH (arglist)--;
4190 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4192 /* See if the arguments match. If they do, then TMPL is
4193 the partial instantiation we want. */
4194 success = comp_template_args (TREE_PURPOSE (found), arglist);
4196 /* Restore the argument vectors to their full size. */
4197 TREE_VEC_LENGTH (arglist)++;
4198 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4209 /* There was no partial instantiation. This happens
4210 where C<T> is a member template of A<T> and it's used
4213 template <typename T> struct B { A<T>::C<int> m; };
4216 Create the partial instantiation.
4218 TREE_VEC_LENGTH (arglist)--;
4219 found = tsubst (template, arglist, /*complain=*/0, NULL_TREE);
4220 TREE_VEC_LENGTH (arglist)++;
4224 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4225 DECL_TEMPLATE_INSTANTIATIONS (template)
4226 = tree_cons (arglist, t,
4227 DECL_TEMPLATE_INSTANTIATIONS (template));
4229 if (TREE_CODE (t) == ENUMERAL_TYPE
4230 && !is_partial_instantiation)
4231 /* Now that the type has been registered on the instantiations
4232 list, we set up the enumerators. Because the enumeration
4233 constants may involve the enumeration type itself, we make
4234 sure to register the type first, and then create the
4235 constants. That way, doing tsubst_expr for the enumeration
4236 constants won't result in recursive calls here; we'll find
4237 the instantiation and exit above. */
4238 tsubst_enum (template_type, t, arglist);
4240 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4242 if (TREE_CODE (t) != ENUMERAL_TYPE)
4243 DECL_NAME (type_decl) = classtype_mangled_name (t);
4244 if (!is_partial_instantiation)
4246 /* For backwards compatibility; code that uses
4247 -fexternal-templates expects looking up a template to
4248 instantiate it. I think DDD still relies on this.
4249 (jason 8/20/1998) */
4250 if (TREE_CODE (t) != ENUMERAL_TYPE
4251 && flag_external_templates
4252 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4253 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4254 add_pending_template (t);
4257 /* If the type makes use of template parameters, the
4258 code that generates debugging information will crash. */
4259 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4271 /* Called from for_each_template_parm via walk_tree. */
4274 for_each_template_parm_r (tp, walk_subtrees, d)
4280 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4281 tree_fn_t fn = pfd->fn;
4282 void *data = pfd->data;
4285 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4286 return error_mark_node;
4288 switch (TREE_CODE (t))
4291 if (TYPE_PTRMEMFUNC_P (t))
4297 if (!TYPE_TEMPLATE_INFO (t))
4299 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4301 return error_mark_node;
4305 /* Since we're not going to walk subtrees, we have to do this
4307 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
4308 return error_mark_node;
4312 /* Check the return type. */
4313 if (for_each_template_parm (TREE_TYPE (t), fn, data))
4314 return error_mark_node;
4316 /* Check the parameter types. Since default arguments are not
4317 instantiated until they are needed, the TYPE_ARG_TYPES may
4318 contain expressions that involve template parameters. But,
4319 no-one should be looking at them yet. And, once they're
4320 instantiated, they don't contain template parameters, so
4321 there's no point in looking at them then, either. */
4325 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4326 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4327 return error_mark_node;
4329 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4330 want walk_tree walking into them itself. */
4337 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4338 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
4339 return error_mark_node;
4344 if (DECL_CONTEXT (t)
4345 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
4346 return error_mark_node;
4349 case BOUND_TEMPLATE_TEMPLATE_PARM:
4350 /* Record template parameters such as `T' inside `TT<T>'. */
4351 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
4352 return error_mark_node;
4355 case TEMPLATE_TEMPLATE_PARM:
4356 case TEMPLATE_TYPE_PARM:
4357 case TEMPLATE_PARM_INDEX:
4358 if (fn && (*fn)(t, data))
4359 return error_mark_node;
4361 return error_mark_node;
4365 /* A template template parameter is encountered */
4366 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4367 && for_each_template_parm (TREE_TYPE (t), fn, data))
4368 return error_mark_node;
4370 /* Already substituted template template parameter */
4375 if (!fn || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, data))
4376 return error_mark_node;
4380 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4381 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4382 (TREE_TYPE (t)), fn, data))
4383 return error_mark_node;
4388 /* If there's no type, then this thing must be some expression
4389 involving template parameters. */
4390 if (!fn && !TREE_TYPE (t))
4391 return error_mark_node;
4396 case REINTERPRET_CAST_EXPR:
4397 case CONST_CAST_EXPR:
4398 case STATIC_CAST_EXPR:
4399 case DYNAMIC_CAST_EXPR:
4404 case PSEUDO_DTOR_EXPR:
4406 return error_mark_node;
4413 /* We didn't find any template parameters we liked. */
4417 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4418 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4419 call FN with the parameter and the DATA.
4420 If FN returns non-zero, the iteration is terminated, and
4421 for_each_template_parm returns 1. Otherwise, the iteration
4422 continues. If FN never returns a non-zero value, the value
4423 returned by for_each_template_parm is 0. If FN is NULL, it is
4424 considered to be the function which always returns 1. */
4427 for_each_template_parm (t, fn, data)
4432 struct pair_fn_data pfd;
4438 /* Walk the tree. (Conceptually, we would like to walk without
4439 duplicates, but for_each_template_parm_r recursively calls
4440 for_each_template_parm, so we would need to reorganize a fair
4441 bit to use walk_tree_without_duplicates.) */
4442 return walk_tree (&t,
4443 for_each_template_parm_r,
4449 uses_template_parms (t)
4452 return for_each_template_parm (t, 0, 0);
4455 static int tinst_depth;
4456 extern int max_tinst_depth;
4457 #ifdef GATHER_STATISTICS
4460 static int tinst_level_tick;
4461 static int last_template_error_tick;
4463 /* We're starting to instantiate D; record the template instantiation context
4464 for diagnostics and to restore it later. */
4467 push_tinst_level (d)
4472 if (tinst_depth >= max_tinst_depth)
4474 /* If the instantiation in question still has unbound template parms,
4475 we don't really care if we can't instantiate it, so just return.
4476 This happens with base instantiation for implicit `typename'. */
4477 if (uses_template_parms (d))
4480 last_template_error_tick = tinst_level_tick;
4481 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4482 max_tinst_depth, d);
4484 print_instantiation_context ();
4489 new = build_expr_wfl (d, input_filename, lineno, 0);
4490 TREE_CHAIN (new) = current_tinst_level;
4491 current_tinst_level = new;
4494 #ifdef GATHER_STATISTICS
4495 if (tinst_depth > depth_reached)
4496 depth_reached = tinst_depth;
4503 /* We're done instantiating this template; return to the instantiation
4509 tree old = current_tinst_level;
4511 /* Restore the filename and line number stashed away when we started
4512 this instantiation. */
4513 lineno = TINST_LINE (old);
4514 input_filename = TINST_FILE (old);
4515 extract_interface_info ();
4517 current_tinst_level = TREE_CHAIN (old);
4522 /* We're instantiating a deferred template; restore the template
4523 instantiation context in which the instantiation was requested, which
4524 is one step out from LEVEL. */
4527 reopen_tinst_level (level)
4533 for (t = level; t; t = TREE_CHAIN (t))
4536 current_tinst_level = level;
4540 /* Return the outermost template instantiation context, for use with
4541 -falt-external-templates. */
4546 tree p = current_tinst_level;
4549 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
4554 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4555 vector of template arguments, as for tsubst.
4557 Returns an appropriate tsubst'd friend declaration. */
4560 tsubst_friend_function (decl, args)
4566 const char *file = input_filename;
4568 lineno = DECL_SOURCE_LINE (decl);
4569 input_filename = DECL_SOURCE_FILE (decl);
4571 if (TREE_CODE (decl) == FUNCTION_DECL
4572 && DECL_TEMPLATE_INSTANTIATION (decl)
4573 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4574 /* This was a friend declared with an explicit template
4575 argument list, e.g.:
4579 to indicate that f was a template instantiation, not a new
4580 function declaration. Now, we have to figure out what
4581 instantiation of what template. */
4583 tree template_id, arglist, fns;
4586 tree ns = CP_DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4588 /* Friend functions are looked up in the containing namespace scope.
4589 We must enter that scope, to avoid finding member functions of the
4590 current cless with same name. */
4591 push_nested_namespace (ns);
4592 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
4593 /*complain=*/1, NULL_TREE);
4594 pop_nested_namespace (ns);
4595 arglist = tsubst (DECL_TI_ARGS (decl), args,
4596 /*complain=*/1, NULL_TREE);
4597 template_id = lookup_template_function (fns, arglist);
4599 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4600 tmpl = determine_specialization (template_id, new_friend,
4602 /*need_member_template=*/0);
4603 new_friend = instantiate_template (tmpl, new_args);
4607 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
4609 /* The NEW_FRIEND will look like an instantiation, to the
4610 compiler, but is not an instantiation from the point of view of
4611 the language. For example, we might have had:
4613 template <class T> struct S {
4614 template <class U> friend void f(T, U);
4617 Then, in S<int>, template <class U> void f(int, U) is not an
4618 instantiation of anything. */
4619 DECL_USE_TEMPLATE (new_friend) = 0;
4620 if (TREE_CODE (decl) == TEMPLATE_DECL)
4622 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4623 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4624 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4627 /* The mangled name for the NEW_FRIEND is incorrect. The function
4628 is not a template instantiation and should not be mangled like
4629 one. Therefore, we forget the mangling here; we'll recompute it
4630 later if we need it. */
4631 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4633 SET_DECL_RTL (new_friend, NULL_RTX);
4634 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
4637 if (DECL_NAMESPACE_SCOPE_P (new_friend))
4640 tree new_friend_template_info;
4641 tree new_friend_result_template_info;
4643 int new_friend_is_defn;
4645 /* We must save some information from NEW_FRIEND before calling
4646 duplicate decls since that function will free NEW_FRIEND if
4648 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
4649 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
4651 /* This declaration is a `primary' template. */
4652 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4655 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
4656 new_friend_result_template_info
4657 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
4661 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4662 new_friend_result_template_info = NULL_TREE;
4665 /* Inside pushdecl_namespace_level, we will push into the
4666 current namespace. However, the friend function should go
4667 into the namespace of the template. */
4668 ns = decl_namespace_context (new_friend);
4669 push_nested_namespace (ns);
4670 old_decl = pushdecl_namespace_level (new_friend);
4671 pop_nested_namespace (ns);
4673 if (old_decl != new_friend)
4675 /* This new friend declaration matched an existing
4676 declaration. For example, given:
4678 template <class T> void f(T);
4679 template <class U> class C {
4680 template <class T> friend void f(T) {}
4683 the friend declaration actually provides the definition
4684 of `f', once C has been instantiated for some type. So,
4685 old_decl will be the out-of-class template declaration,
4686 while new_friend is the in-class definition.
4688 But, if `f' was called before this point, the
4689 instantiation of `f' will have DECL_TI_ARGS corresponding
4690 to `T' but not to `U', references to which might appear
4691 in the definition of `f'. Previously, the most general
4692 template for an instantiation of `f' was the out-of-class
4693 version; now it is the in-class version. Therefore, we
4694 run through all specialization of `f', adding to their
4695 DECL_TI_ARGS appropriately. In particular, they need a
4696 new set of outer arguments, corresponding to the
4697 arguments for this class instantiation.
4699 The same situation can arise with something like this:
4702 template <class T> class C {
4706 when `C<int>' is instantiated. Now, `f(int)' is defined
4709 if (!new_friend_is_defn)
4710 /* On the other hand, if the in-class declaration does
4711 *not* provide a definition, then we don't want to alter
4712 existing definitions. We can just leave everything
4717 /* Overwrite whatever template info was there before, if
4718 any, with the new template information pertaining to
4720 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4722 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4723 /* duplicate_decls will take care of this case. */
4728 tree new_friend_args;
4730 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
4731 = new_friend_result_template_info;
4733 new_friend_args = TI_ARGS (new_friend_template_info);
4734 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4738 tree spec = TREE_VALUE (t);
4741 = add_outermost_template_args (new_friend_args,
4742 DECL_TI_ARGS (spec));
4745 /* Now, since specializations are always supposed to
4746 hang off of the most general template, we must move
4748 t = most_general_template (old_decl);
4751 DECL_TEMPLATE_SPECIALIZATIONS (t)
4752 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4753 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4754 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4759 /* The information from NEW_FRIEND has been merged into OLD_DECL
4760 by duplicate_decls. */
4761 new_friend = old_decl;
4764 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
4766 /* Check to see that the declaration is really present, and,
4767 possibly obtain an improved declaration. */
4768 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4777 input_filename = file;
4781 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4782 template arguments, as for tsubst.
4784 Returns an appropriate tsubst'd friend type or error_mark_node on
4788 tsubst_friend_class (friend_tmpl, args)
4795 /* First, we look for a class template. */
4796 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4798 /* But, if we don't find one, it might be because we're in a
4799 situation like this:
4807 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4808 for `S<int>', not the TEMPLATE_DECL. */
4809 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4811 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4812 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4815 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
4817 /* The friend template has already been declared. Just
4818 check to see that the declarations match, and install any new
4819 default parameters. We must tsubst the default parameters,
4820 of course. We only need the innermost template parameters
4821 because that is all that redeclare_class_template will look
4824 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4825 args, /*complain=*/1);
4827 return error_mark_node;
4828 redeclare_class_template (TREE_TYPE (tmpl), parms);
4829 friend_type = TREE_TYPE (tmpl);
4833 /* The friend template has not already been declared. In this
4834 case, the instantiation of the template class will cause the
4835 injection of this template into the global scope. */
4836 tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE);
4838 /* The new TMPL is not an instantiation of anything, so we
4839 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4840 the new type because that is supposed to be the corresponding
4841 template decl, i.e., TMPL. */
4842 DECL_USE_TEMPLATE (tmpl) = 0;
4843 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4844 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4846 /* Inject this template into the global scope. */
4847 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4854 instantiate_class_template (type)
4857 tree template, args, pattern, t;
4860 if (type == error_mark_node)
4861 return error_mark_node;
4863 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
4866 /* Figure out which template is being instantiated. */
4867 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4868 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4870 /* Figure out which arguments are being used to do the
4872 args = CLASSTYPE_TI_ARGS (type);
4873 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
4875 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4876 /* If this is a partial instantiation, then we can't instantiate
4877 the type; there's no telling whether or not one of the
4878 template parameters might eventually be instantiated to some
4879 value that results in a specialization being used. For
4891 Now, the `S<U>' in `f<int>' is the specialization, not an
4892 instantiation of the original template. */
4895 /* Determine what specialization of the original template to
4897 if (PARTIAL_INSTANTIATION_P (type))
4898 /* There's no telling which specialization is appropriate at this
4899 point. Since all peeking at the innards of this partial
4900 instantiation are extensions (like the "implicit typename"
4901 extension, which allows users to omit the keyword `typename' on
4902 names that are declared as types in template base classes), we
4903 are free to do what we please.
4905 Trying to figure out which partial instantiation to use can
4906 cause a crash. (Some of the template arguments don't even have
4907 types.) So, we just use the most general version. */
4911 t = most_specialized_class (template, args);
4913 if (t == error_mark_node)
4915 const char *str = "candidates are:";
4916 error ("ambiguous class template instantiation for `%#T'", type);
4917 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4920 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4923 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4927 TYPE_BEING_DEFINED (type) = 1;
4928 return error_mark_node;
4933 pattern = TREE_TYPE (t);
4935 pattern = TREE_TYPE (template);
4937 /* If the template we're instantiating is incomplete, then clearly
4938 there's nothing we can do. */
4939 if (!COMPLETE_TYPE_P (pattern))
4942 /* If this is a partial instantiation, don't tsubst anything. We will
4943 only use this type for implicit typename, so the actual contents don't
4944 matter. All that matters is whether a particular name is a type. */
4945 if (PARTIAL_INSTANTIATION_P (type))
4947 /* The fields set here must be kept in sync with those cleared
4948 in begin_class_definition. */
4949 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4950 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4951 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4952 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4953 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
4955 /* Pretend that the type is complete, so that we will look
4956 inside it during name lookup and such. */
4957 TYPE_SIZE (type) = bitsize_zero_node;
4961 /* If we've recursively instantiated too many templates, stop. */
4962 if (! push_tinst_level (type))
4965 /* Now we're really doing the instantiation. Mark the type as in
4966 the process of being defined. */
4967 TYPE_BEING_DEFINED (type) = 1;
4969 maybe_push_to_top_level (uses_template_parms (type));
4973 /* This TYPE is actually a instantiation of of a partial
4974 specialization. We replace the innermost set of ARGS with
4975 the arguments appropriate for substitution. For example,
4978 template <class T> struct S {};
4979 template <class T> struct S<T*> {};
4981 and supposing that we are instantiating S<int*>, ARGS will
4982 present be {int*} but we need {int}. */
4984 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4987 /* If there were multiple levels in ARGS, replacing the
4988 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4989 want, so we make a copy first. */
4990 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4992 args = copy_node (args);
4993 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4999 if (flag_external_templates)
5001 if (flag_alt_external_templates)
5003 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5004 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5008 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5009 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5010 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5015 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5018 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5019 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
5020 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5021 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5022 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
5023 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5024 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5025 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5026 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5027 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5028 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5029 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5030 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5031 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5032 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5033 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5034 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
5035 TYPE_USES_MULTIPLE_INHERITANCE (type)
5036 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5037 TYPE_USES_VIRTUAL_BASECLASSES (type)
5038 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5039 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5040 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5041 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5042 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5043 if (ANON_AGGR_TYPE_P (pattern))
5044 SET_ANON_AGGR_TYPE_P (type);
5046 if (TYPE_BINFO_BASETYPES (pattern))
5048 tree base_list = NULL_TREE;
5049 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5052 /* Substitute into each of the bases to determine the actual
5054 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5060 pbase = TREE_VEC_ELT (pbases, i);
5062 /* Substitute to figure out the base class. */
5063 base = tsubst (BINFO_TYPE (pbase), args,
5064 /*complain=*/1, NULL_TREE);
5065 if (base == error_mark_node)
5068 /* Calculate the correct access node. */
5069 if (TREE_VIA_VIRTUAL (pbase))
5071 if (TREE_VIA_PUBLIC (pbase))
5072 access = access_public_virtual_node;
5073 else if (TREE_VIA_PROTECTED (pbase))
5074 access = access_protected_virtual_node;
5076 access = access_private_virtual_node;
5080 if (TREE_VIA_PUBLIC (pbase))
5081 access = access_public_node;
5082 else if (TREE_VIA_PROTECTED (pbase))
5083 access = access_protected_node;
5085 access = access_private_node;
5088 base_list = tree_cons (access, base, base_list);
5091 /* The list is now in reverse order; correct that. */
5092 base_list = nreverse (base_list);
5094 /* Now call xref_basetypes to set up all the base-class
5096 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5097 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5098 ? class_type_node : record_type_node)
5100 DECL_NAME (TYPE_NAME (pattern)),
5105 /* Now that our base classes are set up, enter the scope of the
5106 class, so that name lookups into base classes, etc. will work
5107 correctly. This is precisely analogous to what we do in
5108 begin_class_definition when defining an ordinary non-template
5110 pushclass (type, 1);
5112 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
5114 tree tag = TREE_VALUE (t);
5115 tree name = TYPE_IDENTIFIER (tag);
5118 newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE);
5119 my_friendly_assert (newtag != error_mark_node, 20010206);
5120 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5122 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5123 /* Unfortunately, lookup_template_class sets
5124 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5125 instantiation (i.e., for the type of a member template
5126 class nested within a template class.) This behavior is
5127 required for maybe_process_partial_specialization to work
5128 correctly, but is not accurate in this case; the TAG is not
5129 an instantiation of anything. (The corresponding
5130 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5131 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5133 /* Now, we call pushtag to put this NEWTAG into the scope of
5134 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5135 pushtag calling push_template_decl. We don't have to do
5136 this for enums because it will already have been done in
5139 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5140 pushtag (name, newtag, /*globalize=*/0);
5144 /* Don't replace enum constants here. */
5145 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
5146 if (TREE_CODE (t) != CONST_DECL)
5150 /* The the file and line for this declaration, to assist in
5151 error message reporting. Since we called push_tinst_level
5152 above, we don't need to restore these. */
5153 lineno = DECL_SOURCE_LINE (t);
5154 input_filename = DECL_SOURCE_FILE (t);
5156 r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5157 if (TREE_CODE (r) == VAR_DECL)
5161 if (DECL_INITIALIZED_IN_CLASS_P (r))
5162 init = tsubst_expr (DECL_INITIAL (t), args,
5163 /*complain=*/1, NULL_TREE);
5167 finish_static_data_member_decl (r, init,
5168 /*asmspec_tree=*/NULL_TREE,
5171 if (DECL_INITIALIZED_IN_CLASS_P (r))
5172 check_static_variable_definition (r, TREE_TYPE (r));
5175 /* R will have a TREE_CHAIN if and only if it has already been
5176 processed by finish_member_declaration. This can happen
5177 if, for example, it is a TYPE_DECL for a class-scoped
5178 ENUMERAL_TYPE; such a thing will already have been added to
5179 the field list by tsubst_enum above. */
5180 if (!TREE_CHAIN (r))
5182 set_current_access_from_decl (r);
5183 finish_member_declaration (r);
5187 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5188 for this instantiation. */
5189 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5191 tree r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5192 set_current_access_from_decl (r);
5193 grok_special_member_properties (r);
5194 finish_member_declaration (r);
5197 /* Construct the DECL_FRIENDLIST for the new class type. */
5198 typedecl = TYPE_MAIN_DECL (type);
5199 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5205 for (friends = TREE_VALUE (t);
5206 friends != NULL_TREE;
5207 friends = TREE_CHAIN (friends))
5208 if (TREE_PURPOSE (friends) == error_mark_node)
5210 tsubst_friend_function (TREE_VALUE (friends),
5216 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5220 tree friend_type = TREE_VALUE (t);
5221 tree new_friend_type;
5223 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5224 new_friend_type = tsubst_friend_class (friend_type, args);
5225 else if (uses_template_parms (friend_type))
5226 new_friend_type = tsubst (friend_type, args, /*complain=*/1,
5230 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5232 /* The call to xref_tag_from_type does injection for friend
5234 push_nested_namespace (ns);
5236 xref_tag_from_type (friend_type, NULL_TREE, 1);
5237 pop_nested_namespace (ns);
5240 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5241 /* Trick make_friend_class into realizing that the friend
5242 we're adding is a template, not an ordinary class. It's
5243 important that we use make_friend_class since it will
5244 perform some error-checking and output cross-reference
5246 ++processing_template_decl;
5248 if (new_friend_type != error_mark_node)
5249 make_friend_class (type, new_friend_type);
5251 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5252 --processing_template_decl;
5255 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5256 if (TREE_CODE (t) == FIELD_DECL)
5258 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5259 require_complete_type (t);
5262 /* Set the file and line number information to whatever is given for
5263 the class itself. This puts error messages involving generated
5264 implicit functions at a predictable point, and the same point
5265 that would be used for non-template classes. */
5266 lineno = DECL_SOURCE_LINE (typedecl);
5267 input_filename = DECL_SOURCE_FILE (typedecl);
5269 unreverse_member_declarations (type);
5270 finish_struct_1 (type);
5271 CLASSTYPE_GOT_SEMICOLON (type) = 1;
5273 /* Clear this now so repo_template_used is happy. */
5274 TYPE_BEING_DEFINED (type) = 0;
5275 repo_template_used (type);
5277 /* Now that the class is complete, instantiate default arguments for
5278 any member functions. We don't do this earlier because the
5279 default arguments may reference members of the class. */
5280 if (!PRIMARY_TEMPLATE_P (template))
5281 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5282 if (TREE_CODE (t) == FUNCTION_DECL
5283 /* Implicitly generated member functions will not have template
5284 information; they are not instantiations, but instead are
5285 created "fresh" for each instantiation. */
5286 && DECL_TEMPLATE_INFO (t))
5287 tsubst_default_arguments (t);
5290 pop_from_top_level ();
5300 if (t1 == NULL_TREE)
5301 return t2 == NULL_TREE;
5302 if (t2 == NULL_TREE)
5304 /* Don't care if one declares its arg const and the other doesn't -- the
5305 main variant of the arg type is all that matters. */
5306 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5307 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5309 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5312 /* If arg is a non-type template parameter that does not depend on template
5313 arguments, fold it like we weren't in the body of a template. */
5316 maybe_fold_nontype_arg (arg)
5319 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
5321 /* Sometimes, one of the args was an expression involving a
5322 template constant parameter, like N - 1. Now that we've
5323 tsubst'd, we might have something like 2 - 1. This will
5324 confuse lookup_template_class, so we do constant folding
5325 here. We have to unset processing_template_decl, to
5326 fool build_expr_from_tree() into building an actual
5329 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5330 as simple as it's going to get, and trying to reprocess
5331 the trees will break. */
5332 if (!TREE_TYPE (arg))
5334 int saved_processing_template_decl = processing_template_decl;
5335 processing_template_decl = 0;
5336 arg = build_expr_from_tree (arg);
5337 processing_template_decl = saved_processing_template_decl;
5345 /* Substitute ARGS into the vector of template arguments T. */
5348 tsubst_template_arg_vector (t, args, complain)
5353 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5354 tree *elts = (tree *) alloca (len * sizeof (tree));
5356 memset ((char *) elts, 0, len * sizeof (tree));
5358 for (i = 0; i < len; i++)
5360 if (TREE_VEC_ELT (t, i) != NULL_TREE
5361 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
5362 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5365 elts[i] = maybe_fold_nontype_arg
5366 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5369 if (elts[i] == error_mark_node)
5370 return error_mark_node;
5372 if (elts[i] != TREE_VEC_ELT (t, i))
5379 t = make_tree_vec (len);
5380 for (i = 0; i < len; i++)
5381 TREE_VEC_ELT (t, i) = elts[i];
5386 /* Return the result of substituting ARGS into the template parameters
5387 given by PARMS. If there are m levels of ARGS and m + n levels of
5388 PARMS, then the result will contain n levels of PARMS. For
5389 example, if PARMS is `template <class T> template <class U>
5390 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5391 result will be `template <int*, double, class V>'. */
5394 tsubst_template_parms (parms, args, complain)
5402 for (new_parms = &r;
5403 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5404 new_parms = &(TREE_CHAIN (*new_parms)),
5405 parms = TREE_CHAIN (parms))
5408 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5411 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5413 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5414 tree default_value = TREE_PURPOSE (tuple);
5415 tree parm_decl = TREE_VALUE (tuple);
5417 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5418 default_value = tsubst_expr (default_value, args,
5419 complain, NULL_TREE);
5420 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5422 TREE_VEC_ELT (new_vec, i) = tuple;
5426 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5427 - TMPL_ARGS_DEPTH (args)),
5428 new_vec, NULL_TREE);
5434 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5435 type T. If T is not an aggregate or enumeration type, it is
5436 handled as if by tsubst. IN_DECL is as for tsubst. If
5437 ENTERING_SCOPE is non-zero, T is the context for a template which
5438 we are presently tsubst'ing. Return the substituted value. */
5441 tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
5451 switch (TREE_CODE (t))
5454 if (TYPE_PTRMEMFUNC_P (t))
5455 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
5457 /* else fall through */
5460 if (TYPE_TEMPLATE_INFO (t))
5466 /* First, determine the context for the type we are looking
5468 if (TYPE_CONTEXT (t) != NULL_TREE)
5469 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5471 in_decl, /*entering_scope=*/1);
5473 context = NULL_TREE;
5475 /* Then, figure out what arguments are appropriate for the
5476 type we are trying to find. For example, given:
5478 template <class T> struct S;
5479 template <class T, class U> void f(T, U) { S<U> su; }
5481 and supposing that we are instantiating f<int, double>,
5482 then our ARGS will be {int, double}, but, when looking up
5483 S we only want {double}. */
5484 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5486 if (argvec == error_mark_node)
5487 return error_mark_node;
5489 r = lookup_template_class (t, argvec, in_decl, context,
5490 entering_scope, complain);
5492 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5496 /* This is not a template type, so there's nothing to do. */
5500 return tsubst (t, args, complain, in_decl);
5504 /* Substitute into the default argument ARG (a default argument for
5505 FN), which has the indicated TYPE. */
5508 tsubst_default_argument (fn, type, arg)
5513 /* This default argument came from a template. Instantiate the
5514 default argument here, not in tsubst. In the case of
5523 we must be careful to do name lookup in the scope of S<T>,
5524 rather than in the current class. */
5525 if (DECL_CLASS_SCOPE_P (fn))
5526 pushclass (DECL_CONTEXT (fn), 2);
5528 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
5530 if (DECL_CLASS_SCOPE_P (fn))
5533 /* Make sure the default argument is reasonable. */
5534 arg = check_default_argument (type, arg);
5539 /* Substitute into all the default arguments for FN. */
5542 tsubst_default_arguments (fn)
5548 tmpl_args = DECL_TI_ARGS (fn);
5550 /* If this function is not yet instantiated, we certainly don't need
5551 its default arguments. */
5552 if (uses_template_parms (tmpl_args))
5555 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5557 arg = TREE_CHAIN (arg))
5558 if (TREE_PURPOSE (arg))
5559 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5561 TREE_PURPOSE (arg));
5564 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5565 (already computed) substitution of ARGS into TREE_TYPE (T), if
5566 appropriate. Return the result of the substitution. */
5569 tsubst_decl (t, args, type)
5575 const char *saved_filename;
5579 /* Set the filename and linenumber to improve error-reporting. */
5580 saved_lineno = lineno;
5581 saved_filename = input_filename;
5582 lineno = DECL_SOURCE_LINE (t);
5583 input_filename = DECL_SOURCE_FILE (t);
5585 switch (TREE_CODE (t))
5589 /* We can get here when processing a member template function
5590 of a template class. */
5591 tree decl = DECL_TEMPLATE_RESULT (t);
5593 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
5595 if (!is_template_template_parm)
5597 /* We might already have an instance of this template.
5598 The ARGS are for the surrounding class type, so the
5599 full args contain the tsubst'd args for the context,
5600 plus the innermost args from the template decl. */
5601 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5602 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5603 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
5606 full_args = tsubst_template_arg_vector (tmpl_args, args,
5609 /* tsubst_template_arg_vector doesn't copy the vector if
5610 nothing changed. But, *something* should have
5612 my_friendly_assert (full_args != tmpl_args, 0);
5614 spec = retrieve_specialization (t, full_args);
5615 if (spec != NULL_TREE)
5622 /* Make a new template decl. It will be similar to the
5623 original, but will record the current template arguments.
5624 We also create a new function declaration, which is just
5625 like the old one, but points to this new template, rather
5626 than the old one. */
5628 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5629 TREE_CHAIN (r) = NULL_TREE;
5631 if (is_template_template_parm)
5633 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5634 DECL_TEMPLATE_RESULT (r) = new_decl;
5635 TREE_TYPE (r) = TREE_TYPE (new_decl);
5640 = tsubst_aggr_type (DECL_CONTEXT (t), args,
5641 /*complain=*/1, in_decl,
5642 /*entering_scope=*/1);
5643 DECL_VIRTUAL_CONTEXT (r)
5644 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5645 /*complain=*/1, in_decl,
5646 /*entering_scope=*/1);
5647 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
5649 if (TREE_CODE (decl) == TYPE_DECL)
5651 tree new_type = tsubst (TREE_TYPE (t), args,
5652 /*complain=*/1, in_decl);
5653 TREE_TYPE (r) = new_type;
5654 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5655 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
5656 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
5660 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
5662 DECL_TEMPLATE_RESULT (r) = new_decl;
5663 DECL_TI_TEMPLATE (new_decl) = r;
5664 TREE_TYPE (r) = TREE_TYPE (new_decl);
5665 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
5668 SET_DECL_IMPLICIT_INSTANTIATION (r);
5669 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5670 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
5672 /* The template parameters for this new template are all the
5673 template parameters for the old template, except the
5674 outermost level of parameters. */
5675 DECL_TEMPLATE_PARMS (r)
5676 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5679 if (PRIMARY_TEMPLATE_P (t))
5680 DECL_PRIMARY_TEMPLATE (r) = r;
5682 /* We don't partially instantiate partial specializations. */
5683 if (TREE_CODE (decl) == TYPE_DECL)
5686 /* Record this partial instantiation. */
5687 register_specialization (r, t,
5688 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
5696 tree argvec = NULL_TREE;
5703 /* Nobody should be tsubst'ing into non-template functions. */
5704 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5706 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5710 /* Calculate the most general template of which R is a
5711 specialization, and the complete set of arguments used to
5713 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
5715 = tsubst_template_arg_vector (DECL_TI_ARGS
5716 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5717 args, /*complain=*/1);
5719 /* Check to see if we already have this specialization. */
5720 spec = retrieve_specialization (gen_tmpl, argvec);
5728 /* We can see more levels of arguments than parameters if
5729 there was a specialization of a member template, like
5732 template <class T> struct S { template <class U> void f(); }
5733 template <> template <class U> void S<int>::f(U);
5735 Here, we'll be substituting into the specialization,
5736 because that's where we can find the code we actually
5737 want to generate, but we'll have enough arguments for
5738 the most general template.
5740 We also deal with the peculiar case:
5742 template <class T> struct S {
5743 template <class U> friend void f();
5745 template <class U> void f() {}
5747 template void f<double>();
5749 Here, the ARGS for the instantiation of will be {int,
5750 double}. But, we only need as many ARGS as there are
5751 levels of template parameters in CODE_PATTERN. We are
5752 careful not to get fooled into reducing the ARGS in
5755 template <class T> struct S { template <class U> void f(U); }
5756 template <class T> template <> void S<T>::f(int) {}
5758 which we can spot because the pattern will be a
5759 specialization in this case. */
5760 args_depth = TMPL_ARGS_DEPTH (args);
5762 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5763 if (args_depth > parms_depth
5764 && !DECL_TEMPLATE_SPECIALIZATION (t))
5765 args = get_innermost_template_args (args, parms_depth);
5769 /* This special case arises when we have something like this:
5771 template <class T> struct S {
5772 friend void f<int>(int, double);
5775 Here, the DECL_TI_TEMPLATE for the friend declaration
5776 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5777 being called from tsubst_friend_function, and we want
5778 only to create a new decl (R) with appropriate types so
5779 that we can call determine_specialization. */
5780 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5782 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5783 == IDENTIFIER_NODE), 0);
5784 gen_tmpl = NULL_TREE;
5787 if (DECL_CLASS_SCOPE_P (t))
5789 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5793 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5795 /*entering_scope=*/1);
5800 ctx = DECL_CONTEXT (t);
5802 type = tsubst (type, args, /*complain=*/1, in_decl);
5803 if (type == error_mark_node)
5804 return error_mark_node;
5806 /* We do NOT check for matching decls pushed separately at this
5807 point, as they may not represent instantiations of this
5808 template, and in any case are considered separate under the
5811 DECL_USE_TEMPLATE (r) = 0;
5812 TREE_TYPE (r) = type;
5813 /* Clear out the mangled name and RTL for the instantiation. */
5814 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5815 SET_DECL_RTL (r, NULL_RTX);
5817 DECL_CONTEXT (r) = ctx;
5818 DECL_VIRTUAL_CONTEXT (r)
5819 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
5821 /*entering_scope=*/1);
5823 if (member && DECL_CONV_FN_P (r))
5824 /* Type-conversion operator. Reconstruct the name, in
5825 case it's the name of one of the template's parameters. */
5826 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5828 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5830 DECL_RESULT (r) = NULL_TREE;
5832 TREE_STATIC (r) = 0;
5833 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5834 DECL_EXTERNAL (r) = 1;
5835 DECL_INTERFACE_KNOWN (r) = 0;
5836 DECL_DEFER_OUTPUT (r) = 0;
5837 TREE_CHAIN (r) = NULL_TREE;
5838 DECL_PENDING_INLINE_INFO (r) = 0;
5839 DECL_PENDING_INLINE_P (r) = 0;
5840 DECL_SAVED_TREE (r) = NULL_TREE;
5842 if (DECL_CLONED_FUNCTION (r))
5844 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
5845 args, /*complain=*/1, t);
5846 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
5847 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
5850 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5851 this in the special friend case mentioned above where
5852 GEN_TMPL is NULL. */
5855 DECL_TEMPLATE_INFO (r)
5856 = tree_cons (gen_tmpl, argvec, NULL_TREE);
5857 SET_DECL_IMPLICIT_INSTANTIATION (r);
5858 register_specialization (r, gen_tmpl, argvec);
5860 /* We're not supposed to instantiate default arguments
5861 until they are called, for a template. But, for a
5864 template <class T> void f ()
5865 { extern void g(int i = T()); }
5867 we should do the substitution when the template is
5868 instantiated. We handle the member function case in
5869 instantiate_class_template since the default arguments
5870 might refer to other members of the class. */
5872 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5873 && !uses_template_parms (argvec))
5874 tsubst_default_arguments (r);
5877 /* Copy the list of befriending classes. */
5878 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5880 friends = &TREE_CHAIN (*friends))
5882 *friends = copy_node (*friends);
5883 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5884 args, /*complain=*/1,
5888 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
5890 maybe_retrofit_in_chrg (r);
5891 if (DECL_CONSTRUCTOR_P (r))
5892 grok_ctor_properties (ctx, r);
5893 /* If this is an instantiation of a member template, clone it.
5894 If it isn't, that'll be handled by
5895 clone_constructors_and_destructors. */
5896 if (PRIMARY_TEMPLATE_P (gen_tmpl))
5897 clone_function_decl (r, /*update_method_vec_p=*/0);
5899 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5900 grok_op_properties (r, DECL_FRIEND_P (r));
5907 if (DECL_TEMPLATE_PARM_P (t))
5908 SET_DECL_TEMPLATE_PARM_P (r);
5910 TREE_TYPE (r) = type;
5911 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5913 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5914 DECL_INITIAL (r) = TREE_TYPE (r);
5916 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5917 /*complain=*/1, in_decl);
5919 DECL_CONTEXT (r) = NULL_TREE;
5920 if (!DECL_TEMPLATE_PARM_P (r) && PROMOTE_PROTOTYPES
5921 && INTEGRAL_TYPE_P (type)
5922 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5923 DECL_ARG_TYPE (r) = integer_type_node;
5925 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5926 /*complain=*/1, TREE_CHAIN (t));
5933 TREE_TYPE (r) = type;
5934 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5936 /* We don't have to set DECL_CONTEXT here; it is set by
5937 finish_member_declaration. */
5938 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
5939 /*complain=*/1, in_decl);
5940 TREE_CHAIN (r) = NULL_TREE;
5941 if (VOID_TYPE_P (type))
5942 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
5950 = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl);
5951 TREE_CHAIN (r) = NULL_TREE;
5956 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5957 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
5959 /* If this is the canonical decl, we don't have to mess with
5960 instantiations, and often we can't (for typename, template
5961 type parms and such). Note that TYPE_NAME is not correct for
5962 the above test if we've copied the type for a typedef. */
5963 r = TYPE_NAME (type);
5971 tree argvec = NULL_TREE;
5972 tree gen_tmpl = NULL_TREE;
5974 tree tmpl = NULL_TREE;
5978 /* Assume this is a non-local variable. */
5981 if (TYPE_P (CP_DECL_CONTEXT (t)))
5982 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
5984 in_decl, /*entering_scope=*/1);
5985 else if (DECL_NAMESPACE_SCOPE_P (t))
5986 ctx = DECL_CONTEXT (t);
5989 /* Subsequent calls to pushdecl will fill this in. */
5994 /* Check to see if we already have this specialization. */
5997 tmpl = DECL_TI_TEMPLATE (t);
5998 gen_tmpl = most_general_template (tmpl);
5999 argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl);
6000 spec = retrieve_specialization (gen_tmpl, argvec);
6003 spec = retrieve_local_specialization (t);
6012 TREE_TYPE (r) = type;
6013 c_apply_type_quals_to_decl (cp_type_quals (type), r);
6014 DECL_CONTEXT (r) = ctx;
6015 /* Clear out the mangled name and RTL for the instantiation. */
6016 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6017 SET_DECL_RTL (r, NULL_RTX);
6019 /* Don't try to expand the initializer until someone tries to use
6020 this variable; otherwise we run into circular dependencies. */
6021 DECL_INITIAL (r) = NULL_TREE;
6022 SET_DECL_RTL (r, NULL_RTX);
6023 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
6025 /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */
6026 if (DECL_PRETTY_FUNCTION_P (r))
6028 const char *const name = (*decl_printable_name)
6029 (current_function_decl, 2);
6030 DECL_INITIAL (r) = cp_fname_init (name);
6031 TREE_TYPE (r) = TREE_TYPE (DECL_INITIAL (r));
6034 /* Even if the original location is out of scope, the newly
6035 substituted one is not. */
6036 if (TREE_CODE (r) == VAR_DECL)
6037 DECL_DEAD_FOR_LOCAL (r) = 0;
6041 /* A static data member declaration is always marked
6042 external when it is declared in-class, even if an
6043 initializer is present. We mimic the non-template
6045 DECL_EXTERNAL (r) = 1;
6047 register_specialization (r, gen_tmpl, argvec);
6048 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6049 SET_DECL_IMPLICIT_INSTANTIATION (r);
6052 register_local_specialization (r, t);
6054 TREE_CHAIN (r) = NULL_TREE;
6055 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6056 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
6064 /* Restore the file and line information. */
6065 lineno = saved_lineno;
6066 input_filename = saved_filename;
6071 /* Substitue into the ARG_TYPES of a function type. */
6074 tsubst_arg_types (arg_types, args, complain, in_decl)
6080 tree remaining_arg_types;
6083 if (!arg_types || arg_types == void_list_node)
6086 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
6087 args, complain, in_decl);
6088 if (remaining_arg_types == error_mark_node)
6089 return error_mark_node;
6091 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6092 if (type == error_mark_node)
6093 return error_mark_node;
6094 if (VOID_TYPE_P (type))
6098 error ("invalid parameter type `%T'", type);
6100 cp_error_at ("in declaration `%D'", in_decl);
6102 return error_mark_node;
6105 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6106 top-level qualifiers as required. */
6107 type = TYPE_MAIN_VARIANT (type_decays_to (type));
6109 /* Note that we do not substitute into default arguments here. The
6110 standard mandates that they be instantiated only when needed,
6111 which is done in build_over_call. */
6112 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6113 remaining_arg_types);
6117 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6118 *not* handle the exception-specification for FNTYPE, because the
6119 initial substitution of explicitly provided template parameters
6120 during argument deduction forbids substitution into the
6121 exception-specification:
6125 All references in the function type of the function template to the
6126 corresponding template parameters are replaced by the specified tem-
6127 plate argument values. If a substitution in a template parameter or
6128 in the function type of the function template results in an invalid
6129 type, type deduction fails. [Note: The equivalent substitution in
6130 exception specifications is done only when the function is instanti-
6131 ated, at which point a program is ill-formed if the substitution
6132 results in an invalid type.] */
6135 tsubst_function_type (t, args, complain, in_decl)
6145 /* The TYPE_CONTEXT is not used for function/method types. */
6146 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6148 /* Substitute the return type. */
6149 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6150 if (return_type == error_mark_node)
6151 return error_mark_node;
6153 /* Substitue the argument types. */
6154 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6156 if (arg_types == error_mark_node)
6157 return error_mark_node;
6159 /* Construct a new type node and return it. */
6160 if (TREE_CODE (t) == FUNCTION_TYPE)
6161 fntype = build_function_type (return_type, arg_types);
6164 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6165 if (! IS_AGGR_TYPE (r))
6169 Type deduction may fail for any of the following
6172 -- Attempting to create "pointer to member of T" when T
6173 is not a class type. */
6175 error ("creating pointer to member function of non-class type `%T'",
6177 return error_mark_node;
6180 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6183 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
6184 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
6189 /* Substitute into the PARMS of a call-declarator. */
6192 tsubst_call_declarator_parms (parms, args, complain, in_decl)
6202 if (!parms || parms == void_list_node)
6205 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
6206 args, complain, in_decl);
6208 /* Figure out the type of this parameter. */
6209 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
6211 /* Figure out the default argument as well. Note that we use
6212 tsubst_expr since the default argument is really an expression. */
6213 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
6215 /* Chain this parameter on to the front of those we have already
6216 processed. We don't use hash_tree_cons because that function
6217 doesn't check TREE_PARMLIST. */
6218 new_parms = tree_cons (defarg, type, new_parms);
6220 /* And note that these are parameters. */
6221 TREE_PARMLIST (new_parms) = 1;
6226 /* Take the tree structure T and replace template parameters used
6227 therein with the argument vector ARGS. IN_DECL is an associated
6228 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6229 An appropriate error message is issued only if COMPLAIN is
6230 non-zero. Note that we must be relatively non-tolerant of
6231 extensions here, in order to preserve conformance; if we allow
6232 substitutions that should not be allowed, we may allow argument
6233 deductions that should not succeed, and therefore report ambiguous
6234 overload situations where there are none. In theory, we could
6235 allow the substitution, but indicate that it should have failed,
6236 and allow our caller to make sure that the right thing happens, but
6237 we don't try to do this yet.
6239 This function is used for dealing with types, decls and the like;
6240 for expressions, use tsubst_expr or tsubst_copy. */
6243 tsubst (t, args, complain, in_decl)
6250 if (t == NULL_TREE || t == error_mark_node
6251 || t == integer_type_node
6252 || t == void_type_node
6253 || t == char_type_node
6254 || TREE_CODE (t) == NAMESPACE_DECL)
6257 if (TREE_CODE (t) == IDENTIFIER_NODE)
6258 type = IDENTIFIER_TYPE_VALUE (t);
6260 type = TREE_TYPE (t);
6261 if (type == unknown_type_node)
6264 if (type && TREE_CODE (t) != FUNCTION_DECL
6265 && TREE_CODE (t) != TYPENAME_TYPE
6266 && TREE_CODE (t) != TEMPLATE_DECL
6267 && TREE_CODE (t) != IDENTIFIER_NODE
6268 && TREE_CODE (t) != FUNCTION_TYPE
6269 && TREE_CODE (t) != METHOD_TYPE)
6270 type = tsubst (type, args, complain, in_decl);
6271 if (type == error_mark_node)
6272 return error_mark_node;
6275 return tsubst_decl (t, args, type);
6277 switch (TREE_CODE (t))
6282 return tsubst_aggr_type (t, args, complain, in_decl,
6283 /*entering_scope=*/0);
6286 case IDENTIFIER_NODE:
6298 if (t == integer_type_node)
6301 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6302 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6306 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
6308 max = tsubst_expr (omax, args, complain, in_decl);
6309 if (max == error_mark_node)
6310 return error_mark_node;
6312 /* See if we can reduce this expression to something simpler. */
6313 max = maybe_fold_nontype_arg (max);
6314 if (!processing_template_decl)
6315 max = decl_constant_value (max);
6317 if (processing_template_decl
6318 /* When providing explicit arguments to a template
6319 function, but leaving some arguments for subsequent
6320 deduction, MAX may be template-dependent even if we're
6321 not PROCESSING_TEMPLATE_DECL. We still need to check for
6322 template parms, though; MAX won't be an INTEGER_CST for
6323 dynamic arrays, either. */
6324 || (TREE_CODE (max) != INTEGER_CST
6325 && uses_template_parms (max)))
6327 tree itype = make_node (INTEGER_TYPE);
6328 TYPE_MIN_VALUE (itype) = size_zero_node;
6329 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6334 if (integer_zerop (omax))
6336 /* Still allow an explicit array of size zero. */
6338 pedwarn ("creating array with size zero");
6340 else if (integer_zerop (max)
6341 || (TREE_CODE (max) == INTEGER_CST
6342 && INT_CST_LT (max, integer_zero_node)))
6346 Type deduction may fail for any of the following
6349 Attempting to create an array with a size that is
6350 zero or negative. */
6352 error ("creating array with size zero (`%E')", max);
6354 return error_mark_node;
6357 return compute_array_index_type (NULL_TREE, max);
6360 case TEMPLATE_TYPE_PARM:
6361 case TEMPLATE_TEMPLATE_PARM:
6362 case BOUND_TEMPLATE_TEMPLATE_PARM:
6363 case TEMPLATE_PARM_INDEX:
6371 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6372 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6373 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6375 idx = TEMPLATE_TYPE_IDX (t);
6376 level = TEMPLATE_TYPE_LEVEL (t);
6380 idx = TEMPLATE_PARM_IDX (t);
6381 level = TEMPLATE_PARM_LEVEL (t);
6384 if (TREE_VEC_LENGTH (args) > 0)
6386 tree arg = NULL_TREE;
6388 levels = TMPL_ARGS_DEPTH (args);
6389 if (level <= levels)
6390 arg = TMPL_ARG (args, level, idx);
6392 if (arg == error_mark_node)
6393 return error_mark_node;
6394 else if (arg != NULL_TREE)
6396 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6398 my_friendly_assert (TYPE_P (arg), 0);
6399 return cp_build_qualified_type_real
6400 (arg, cp_type_quals (arg) | cp_type_quals (t),
6403 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6405 /* We are processing a type constructed from
6406 a template template parameter */
6407 tree argvec = tsubst (TYPE_TI_ARGS (t),
6408 args, complain, in_decl);
6409 if (argvec == error_mark_node)
6410 return error_mark_node;
6412 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6413 we are resolving nested-types in the signature of
6414 a member function templates.
6415 Otherwise ARG is a TEMPLATE_DECL and is the real
6416 template to be instantiated. */
6417 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6418 arg = TYPE_NAME (arg);
6420 r = lookup_template_class (arg,
6423 /*entering_scope=*/0,
6425 return cp_build_qualified_type_real (r,
6430 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6438 /* This can happen during the attempted tsubst'ing in
6439 unify. This means that we don't yet have any information
6440 about the template parameter in question. */
6443 /* If we get here, we must have been looking at a parm for a
6444 more deeply nested template. Make a new version of this
6445 template parameter, but with a lower level. */
6446 switch (TREE_CODE (t))
6448 case TEMPLATE_TYPE_PARM:
6449 case TEMPLATE_TEMPLATE_PARM:
6450 case BOUND_TEMPLATE_TEMPLATE_PARM:
6451 if (cp_type_quals (t))
6453 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
6454 r = cp_build_qualified_type_real (r, cp_type_quals (t),
6460 TEMPLATE_TYPE_PARM_INDEX (r)
6461 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6463 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6464 TYPE_MAIN_VARIANT (r) = r;
6465 TYPE_POINTER_TO (r) = NULL_TREE;
6466 TYPE_REFERENCE_TO (r) = NULL_TREE;
6468 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
6470 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6472 if (argvec == error_mark_node)
6473 return error_mark_node;
6475 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6476 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6481 case TEMPLATE_PARM_INDEX:
6482 r = reduce_template_parm_level (t, type, levels);
6494 tree purpose, value, chain, result;
6496 if (t == void_list_node)
6499 purpose = TREE_PURPOSE (t);
6502 purpose = tsubst (purpose, args, complain, in_decl);
6503 if (purpose == error_mark_node)
6504 return error_mark_node;
6506 value = TREE_VALUE (t);
6509 value = tsubst (value, args, complain, in_decl);
6510 if (value == error_mark_node)
6511 return error_mark_node;
6513 chain = TREE_CHAIN (t);
6514 if (chain && chain != void_type_node)
6516 chain = tsubst (chain, args, complain, in_decl);
6517 if (chain == error_mark_node)
6518 return error_mark_node;
6520 if (purpose == TREE_PURPOSE (t)
6521 && value == TREE_VALUE (t)
6522 && chain == TREE_CHAIN (t))
6524 if (TREE_PARMLIST (t))
6526 result = tree_cons (purpose, value, chain);
6527 TREE_PARMLIST (result) = 1;
6530 result = hash_tree_cons (purpose, value, chain);
6534 if (type != NULL_TREE)
6536 /* A binfo node. We always need to make a copy, of the node
6537 itself and of its BINFO_BASETYPES. */
6541 /* Make sure type isn't a typedef copy. */
6542 type = BINFO_TYPE (TYPE_BINFO (type));
6544 TREE_TYPE (t) = complete_type (type);
6545 if (IS_AGGR_TYPE (type))
6547 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6548 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6549 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6550 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6555 /* Otherwise, a vector of template arguments. */
6556 return tsubst_template_arg_vector (t, args, complain);
6559 case REFERENCE_TYPE:
6561 enum tree_code code;
6563 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
6566 code = TREE_CODE (t);
6571 Type deduction may fail for any of the following
6574 -- Attempting to create a pointer to reference type.
6575 -- Attempting to create a reference to a reference type or
6576 a reference to void. */
6577 if (TREE_CODE (type) == REFERENCE_TYPE
6578 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
6580 static int last_line = 0;
6581 static const char* last_file = 0;
6583 /* We keep track of the last time we issued this error
6584 message to avoid spewing a ton of messages during a
6585 single bad template instantiation. */
6586 if (complain && (last_line != lineno ||
6587 last_file != input_filename))
6589 if (TREE_CODE (type) == VOID_TYPE)
6590 error ("forming reference to void");
6592 error ("forming %s to reference type `%T'",
6593 (code == POINTER_TYPE) ? "pointer" : "reference",
6596 last_file = input_filename;
6599 return error_mark_node;
6601 else if (code == POINTER_TYPE)
6603 r = build_pointer_type (type);
6604 if (TREE_CODE (type) == METHOD_TYPE)
6605 r = build_ptrmemfunc_type (r);
6608 r = build_reference_type (type);
6609 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6611 if (r != error_mark_node)
6612 /* Will this ever be needed for TYPE_..._TO values? */
6619 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6620 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6624 Type deduction may fail for any of the following
6627 -- Attempting to create "pointer to member of T" when T
6628 is not a class type. */
6630 error ("creating pointer to member of non-class type `%T'",
6632 return error_mark_node;
6634 if (TREE_CODE (type) == REFERENCE_TYPE)
6637 error ("creating pointer to member reference type `%T'", type);
6639 return error_mark_node;
6641 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6642 if (TREE_CODE (type) == FUNCTION_TYPE)
6643 /* This is really a method type. The cv qualifiers of the
6644 this pointer should _not_ be determined by the cv
6645 qualifiers of the class type. They should be held
6646 somewhere in the FUNCTION_TYPE, but we don't do that at
6647 the moment. Consider
6648 typedef void (Func) () const;
6650 template <typename T1> void Foo (Func T1::*);
6653 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6655 TYPE_ARG_TYPES (type));
6657 return build_offset_type (r, type);
6665 fntype = tsubst_function_type (t, args, complain, in_decl);
6666 if (fntype == error_mark_node)
6667 return error_mark_node;
6669 /* Substitue the exception specification. */
6670 raises = TYPE_RAISES_EXCEPTIONS (t);
6673 tree list = NULL_TREE;
6675 if (! TREE_VALUE (raises))
6678 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6680 tree spec = TREE_VALUE (raises);
6682 spec = tsubst (spec, args, complain, in_decl);
6683 if (spec == error_mark_node)
6685 list = add_exception_specifier (list, spec, complain);
6687 fntype = build_exception_variant (fntype, list);
6693 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6694 if (domain == error_mark_node)
6695 return error_mark_node;
6697 /* As an optimization, we avoid regenerating the array type if
6698 it will obviously be the same as T. */
6699 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6702 /* These checks should match the ones in grokdeclarator.
6706 The deduction may fail for any of the following reasons:
6708 -- Attempting to create an array with an element type that
6709 is void, a function type, or a reference type. */
6710 if (TREE_CODE (type) == VOID_TYPE
6711 || TREE_CODE (type) == FUNCTION_TYPE
6712 || TREE_CODE (type) == REFERENCE_TYPE)
6715 error ("creating array of `%T'", type);
6716 return error_mark_node;
6719 r = build_cplus_array_type (type, domain);
6726 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6728 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain,
6731 if (e1 == error_mark_node || e2 == error_mark_node)
6732 return error_mark_node;
6734 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6740 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6742 if (e == error_mark_node)
6743 return error_mark_node;
6745 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6750 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6751 in_decl, /*entering_scope=*/1);
6752 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6755 if (ctx == error_mark_node || f == error_mark_node)
6756 return error_mark_node;
6758 if (!IS_AGGR_TYPE (ctx))
6761 error ("`%T' is not a class, struct, or union type",
6763 return error_mark_node;
6765 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6767 /* Normally, make_typename_type does not require that the CTX
6768 have complete type in order to allow things like:
6770 template <class T> struct S { typename S<T>::X Y; };
6772 But, such constructs have already been resolved by this
6773 point, so here CTX really should have complete type, unless
6774 it's a partial instantiation. */
6775 ctx = complete_type (ctx);
6776 if (!COMPLETE_TYPE_P (ctx))
6779 incomplete_type_error (NULL_TREE, ctx);
6780 return error_mark_node;
6784 f = make_typename_type (ctx, f, complain);
6785 if (f == error_mark_node)
6787 return cp_build_qualified_type_real (f,
6789 | cp_type_quals (t),
6793 case UNBOUND_CLASS_TEMPLATE:
6795 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6796 in_decl, /*entering_scope=*/1);
6797 tree name = TYPE_IDENTIFIER (t);
6799 if (ctx == error_mark_node || name == error_mark_node)
6800 return error_mark_node;
6802 return make_unbound_class_template (ctx, name, complain);
6807 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6809 if (e == error_mark_node)
6810 return error_mark_node;
6811 return make_pointer_declarator (type, e);
6816 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6818 if (e == error_mark_node)
6819 return error_mark_node;
6820 return make_reference_declarator (type, e);
6825 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6827 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain,
6829 if (e1 == error_mark_node || e2 == error_mark_node)
6830 return error_mark_node;
6832 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
6837 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6839 tree e2 = (tsubst_call_declarator_parms
6840 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6841 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6844 if (e1 == error_mark_node || e2 == error_mark_node
6845 || e3 == error_mark_node)
6846 return error_mark_node;
6848 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
6853 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6855 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6856 if (e1 == error_mark_node || e2 == error_mark_node)
6857 return error_mark_node;
6859 return build_nt (TREE_CODE (t), e1, e2);
6864 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain,
6866 if (e1 == error_mark_node)
6867 return error_mark_node;
6869 return TREE_TYPE (e1);
6873 sorry ("use of `%s' in template",
6874 tree_code_name [(int) TREE_CODE (t)]);
6875 return error_mark_node;
6879 /* Like tsubst, but deals with expressions. This function just replaces
6880 template parms; to finish processing the resultant expression, use
6884 tsubst_copy (t, args, complain, in_decl)
6889 enum tree_code code;
6892 if (t == NULL_TREE || t == error_mark_node)
6895 code = TREE_CODE (t);
6900 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
6907 if (!DECL_CONTEXT (t))
6908 /* This is a global enumeration constant. */
6911 /* Unfortunately, we cannot just call lookup_name here.
6914 template <int I> int f() {
6916 struct S { void g() { E e = a; } };
6919 When we instantiate f<7>::S::g(), say, lookup_name is not
6920 clever enough to find f<7>::a. */
6922 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
6923 /*entering_scope=*/0);
6925 for (v = TYPE_VALUES (enum_type);
6928 if (TREE_PURPOSE (v) == DECL_NAME (t))
6929 return TREE_VALUE (v);
6931 /* We didn't find the name. That should never happen; if
6932 name-lookup found it during preliminary parsing, we
6933 should find it again here during instantiation. */
6939 if (DECL_CONTEXT (t))
6943 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
6944 /*entering_scope=*/1);
6945 if (ctx != DECL_CONTEXT (t))
6946 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6952 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
6953 t = tsubst (t, args, complain, in_decl);
6958 if (is_member_template (t))
6959 return tsubst (t, args, complain, in_decl);
6965 /* We must tsubst into a LOOKUP_EXPR in case the names to
6966 which it refers is a conversion operator; in that case the
6967 name will change. We avoid making unnecessary copies,
6970 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
6972 if (id != TREE_OPERAND (t, 0))
6974 r = build_nt (LOOKUP_EXPR, id);
6975 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6983 case REINTERPRET_CAST_EXPR:
6984 case CONST_CAST_EXPR:
6985 case STATIC_CAST_EXPR:
6986 case DYNAMIC_CAST_EXPR:
6989 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6990 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
6994 case TRUTH_NOT_EXPR:
6997 case CONVERT_EXPR: /* Unary + */
7006 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7007 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
7012 case TRUNC_DIV_EXPR:
7014 case FLOOR_DIV_EXPR:
7015 case ROUND_DIV_EXPR:
7016 case EXACT_DIV_EXPR:
7018 case BIT_ANDTC_EXPR:
7021 case TRUNC_MOD_EXPR:
7022 case FLOOR_MOD_EXPR:
7023 case TRUTH_ANDIF_EXPR:
7024 case TRUTH_ORIF_EXPR:
7025 case TRUTH_AND_EXPR:
7045 case PREDECREMENT_EXPR:
7046 case PREINCREMENT_EXPR:
7047 case POSTDECREMENT_EXPR:
7048 case POSTINCREMENT_EXPR:
7050 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7051 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7055 tree fn = TREE_OPERAND (t, 0);
7056 if (is_overloaded_fn (fn))
7057 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
7059 /* Sometimes FN is a LOOKUP_EXPR. */
7060 fn = tsubst_copy (fn, args, complain, in_decl);
7062 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7067 case METHOD_CALL_EXPR:
7069 tree name = TREE_OPERAND (t, 0);
7070 if (TREE_CODE (name) == BIT_NOT_EXPR)
7072 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7074 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7076 else if (TREE_CODE (name) == SCOPE_REF
7077 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7079 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7081 name = TREE_OPERAND (name, 1);
7082 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7084 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7085 name = build_nt (SCOPE_REF, base, name);
7088 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7090 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7092 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7097 /* This processing should really occur in tsubst_expr, However,
7098 tsubst_expr does not recurse into expressions, since it
7099 assumes that there aren't any statements inside them.
7100 Instead, it simply calls build_expr_from_tree. So, we need
7101 to expand the STMT_EXPR here. */
7102 if (!processing_template_decl)
7104 tree stmt_expr = begin_stmt_expr ();
7105 tsubst_expr (STMT_EXPR_STMT (t), args,
7107 return finish_stmt_expr (stmt_expr);
7114 case PSEUDO_DTOR_EXPR:
7117 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7118 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7119 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7126 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7127 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7128 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
7129 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7136 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7137 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7138 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7139 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7143 case TEMPLATE_ID_EXPR:
7145 /* Substituted template arguments */
7146 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7149 if (targs && TREE_CODE (targs) == TREE_LIST)
7152 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7153 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7158 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7159 TREE_VEC_ELT (targs, i)
7160 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7163 return lookup_template_function
7164 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
7169 tree purpose, value, chain;
7171 if (t == void_list_node)
7174 purpose = TREE_PURPOSE (t);
7176 purpose = tsubst_copy (purpose, args, complain, in_decl);
7177 value = TREE_VALUE (t);
7179 value = tsubst_copy (value, args, complain, in_decl);
7180 chain = TREE_CHAIN (t);
7181 if (chain && chain != void_type_node)
7182 chain = tsubst_copy (chain, args, complain, in_decl);
7183 if (purpose == TREE_PURPOSE (t)
7184 && value == TREE_VALUE (t)
7185 && chain == TREE_CHAIN (t))
7187 return tree_cons (purpose, value, chain);
7194 case TEMPLATE_TYPE_PARM:
7195 case TEMPLATE_TEMPLATE_PARM:
7196 case BOUND_TEMPLATE_TEMPLATE_PARM:
7197 case TEMPLATE_PARM_INDEX:
7199 case REFERENCE_TYPE:
7205 case UNBOUND_CLASS_TEMPLATE:
7208 return tsubst (t, args, complain, in_decl);
7210 case IDENTIFIER_NODE:
7211 if (IDENTIFIER_TYPENAME_P (t))
7213 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7214 return mangle_conv_op_name_for_type (new_type);
7222 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7223 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7224 complain, in_decl));
7225 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7230 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
7232 tsubst (TREE_TYPE (t), args, complain, in_decl));
7239 /* Like tsubst_copy, but also does semantic processing. */
7242 tsubst_expr (t, args, complain, in_decl)
7249 if (t == NULL_TREE || t == error_mark_node)
7252 if (processing_template_decl)
7253 return tsubst_copy (t, args, complain, in_decl);
7255 if (!statement_code_p (TREE_CODE (t)))
7256 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7258 switch (TREE_CODE (t))
7262 finish_named_return_value
7263 (TREE_OPERAND (t, 0),
7264 tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl));
7267 case CTOR_INITIALIZER:
7269 tree member_init_list;
7270 tree base_init_list;
7274 = tsubst_initializer_list (TREE_OPERAND (t, 0), args);
7276 = tsubst_initializer_list (TREE_OPERAND (t, 1), args);
7277 emit_base_init (member_init_list, base_init_list);
7283 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
7284 args, complain, in_decl));
7289 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
7290 args, complain, in_decl));
7295 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7296 args, complain, in_decl));
7305 decl = DECL_STMT_DECL (t);
7306 if (TREE_CODE (decl) == LABEL_DECL)
7307 finish_label_decl (DECL_NAME (decl));
7308 else if (TREE_CODE (decl) == USING_DECL)
7310 tree scope = DECL_INITIAL (decl);
7311 tree name = DECL_NAME (decl);
7313 scope = tsubst_expr (scope, args, complain, in_decl);
7314 do_local_using_decl (build_nt (SCOPE_REF, scope, name));
7318 init = DECL_INITIAL (decl);
7319 decl = tsubst (decl, args, complain, in_decl);
7320 if (DECL_PRETTY_FUNCTION_P (decl))
7321 init = DECL_INITIAL (decl);
7323 init = tsubst_expr (init, args, complain, in_decl);
7324 if (decl != error_mark_node)
7326 if (TREE_CODE (decl) != TYPE_DECL)
7327 /* Make sure the type is instantiated now. */
7328 complete_type (TREE_TYPE (decl));
7330 DECL_INITIAL (decl) = error_mark_node;
7331 /* By marking the declaration as instantiated, we avoid
7332 trying to instantiate it. Since instantiate_decl can't
7333 handle local variables, and since we've already done
7334 all that needs to be done, that's the right thing to
7336 if (TREE_CODE (decl) == VAR_DECL)
7337 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7338 maybe_push_decl (decl);
7339 cp_finish_decl (decl, init, NULL_TREE, 0);
7343 /* A DECL_STMT can also be used as an expression, in the condition
7344 clause of a if/for/while construct. If we aren't followed by
7345 another statement, return our decl. */
7346 if (TREE_CHAIN (t) == NULL_TREE)
7355 stmt = begin_for_stmt ();
7356 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
7357 finish_for_init_stmt (stmt);
7358 finish_for_cond (tsubst_expr (FOR_COND (t), args,
7361 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
7362 finish_for_expr (tmp, stmt);
7363 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
7364 finish_for_stmt (stmt);
7371 stmt = begin_while_stmt ();
7372 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
7373 args, complain, in_decl),
7375 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
7376 finish_while_stmt (stmt);
7383 stmt = begin_do_stmt ();
7384 tsubst_expr (DO_BODY (t), args, complain, in_decl);
7385 finish_do_body (stmt);
7386 finish_do_stmt (tsubst_expr (DO_COND (t), args,
7395 stmt = begin_if_stmt ();
7396 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
7397 args, complain, in_decl),
7400 if (tmp = THEN_CLAUSE (t), tmp)
7402 tsubst_expr (tmp, args, complain, in_decl);
7403 finish_then_clause (stmt);
7406 if (tmp = ELSE_CLAUSE (t), tmp)
7408 begin_else_clause ();
7409 tsubst_expr (tmp, args, complain, in_decl);
7410 finish_else_clause (stmt);
7420 if (COMPOUND_STMT_BODY_BLOCK (t))
7421 stmt = begin_function_body ();
7423 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7425 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
7427 if (COMPOUND_STMT_BODY_BLOCK (t))
7428 finish_function_body (stmt);
7430 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
7436 finish_break_stmt ();
7441 finish_continue_stmt ();
7449 stmt = begin_switch_stmt ();
7450 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
7451 finish_switch_cond (val, stmt);
7452 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7453 finish_switch_stmt (stmt);
7459 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7460 tsubst_expr (CASE_HIGH (t), args, complain,
7465 lineno = STMT_LINENO (t);
7466 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
7471 tmp = GOTO_DESTINATION (t);
7472 if (TREE_CODE (tmp) != LABEL_DECL)
7473 /* Computed goto's must be tsubst'd into. On the other hand,
7474 non-computed gotos must not be; the identifier in question
7475 will have no binding. */
7476 tmp = tsubst_expr (tmp, args, complain, in_decl);
7478 tmp = DECL_NAME (tmp);
7479 finish_goto_stmt (tmp);
7484 finish_asm_stmt (ASM_CV_QUAL (t),
7485 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7486 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7487 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7488 tsubst_expr (ASM_CLOBBERS (t), args, complain,
7496 stmt = begin_try_block ();
7497 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7498 finish_cleanup_try_block (stmt);
7499 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7505 if (FN_TRY_BLOCK_P (t))
7506 stmt = begin_function_try_block ();
7508 stmt = begin_try_block ();
7510 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7512 if (FN_TRY_BLOCK_P (t))
7513 finish_function_try_block (stmt);
7515 finish_try_block (stmt);
7517 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
7518 if (FN_TRY_BLOCK_P (t))
7519 finish_function_handler_sequence (stmt);
7521 finish_handler_sequence (stmt);
7530 stmt = begin_handler ();
7531 if (HANDLER_PARMS (t))
7533 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7534 decl = tsubst (decl, args, complain, in_decl);
7535 /* Prevent instantiate_decl from trying to instantiate
7536 this variable. We've already done all that needs to be
7538 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7542 finish_handler_parms (decl, stmt);
7543 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
7544 finish_handler (stmt);
7550 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
7554 add_stmt (copy_node (t));
7561 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7564 /* Instantiate the indicated variable or function template TMPL with
7565 the template arguments in TARG_PTR. */
7568 instantiate_template (tmpl, targ_ptr)
7569 tree tmpl, targ_ptr;
7577 if (tmpl == error_mark_node)
7578 return error_mark_node;
7580 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7582 /* If this function is a clone, handle it specially. */
7583 if (DECL_CLONED_FUNCTION_P (tmpl))
7585 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
7588 /* Look for the clone. */
7589 for (clone = TREE_CHAIN (spec);
7590 clone && DECL_CLONED_FUNCTION_P (clone);
7591 clone = TREE_CHAIN (clone))
7592 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7594 /* We should always have found the clone by now. */
7599 /* Check to see if we already have this specialization. */
7600 spec = retrieve_specialization (tmpl, targ_ptr);
7601 if (spec != NULL_TREE)
7604 gen_tmpl = most_general_template (tmpl);
7605 if (tmpl != gen_tmpl)
7607 /* The TMPL is a partial instantiation. To get a full set of
7608 arguments we must add the arguments used to perform the
7609 partial instantiation. */
7610 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7613 /* Check to see if we already have this specialization. */
7614 spec = retrieve_specialization (gen_tmpl, targ_ptr);
7615 if (spec != NULL_TREE)
7619 len = DECL_NTPARMS (gen_tmpl);
7620 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
7624 tree t = TREE_VEC_ELT (inner_args, i);
7627 tree nt = target_type (t);
7628 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
7630 error ("type `%T' composed from a local class is not a valid template-argument", t);
7631 error (" trying to instantiate `%D'", gen_tmpl);
7632 return error_mark_node;
7637 /* substitute template parameters */
7638 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
7639 targ_ptr, /*complain=*/1, gen_tmpl);
7640 /* The DECL_TI_TEMPLATE should always be the immediate parent
7641 template, not the most general template. */
7642 DECL_TI_TEMPLATE (fndecl) = tmpl;
7644 if (flag_external_templates)
7645 add_pending_template (fndecl);
7647 /* If we've just instantiated the main entry point for a function,
7648 instantiate all the alternate entry points as well. We do this
7649 by cloning the instantiation of the main entry point, not by
7650 instantiating the template clones. */
7651 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
7652 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
7657 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7658 arguments that are being used when calling it. TARGS is a vector
7659 into which the deduced template arguments are placed.
7661 Return zero for success, 2 for an incomplete match that doesn't resolve
7662 all the types, and 1 for complete failure. An error message will be
7663 printed only for an incomplete match.
7665 If FN is a conversion operator, or we are trying to produce a specific
7666 specialization, RETURN_TYPE is the return type desired.
7668 The EXPLICIT_TARGS are explicit template arguments provided via a
7671 The parameter STRICT is one of:
7674 We are deducing arguments for a function call, as in
7678 We are deducing arguments for a conversion function, as in
7682 We are deducing arguments when doing an explicit instantiation
7683 as in [temp.explicit], when determining an explicit specialization
7684 as in [temp.expl.spec], or when taking the address of a function
7685 template, as in [temp.deduct.funcaddr].
7688 We are deducing arguments when calculating the partial
7689 ordering between specializations of function or class
7690 templates, as in [temp.func.order] and [temp.class.order].
7692 LEN is the number of parms to consider before returning success, or -1
7693 for all. This is used in partial ordering to avoid comparing parms for
7694 which no actual argument was passed, since they are not considered in
7695 overload resolution (and are explicitly excluded from consideration in
7696 partial ordering in [temp.func.order]/6). */
7699 fn_type_unification (fn, explicit_targs, targs, args, return_type,
7701 tree fn, explicit_targs, targs, args, return_type;
7702 unification_kind_t strict;
7709 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7711 fntype = TREE_TYPE (fn);
7716 The specified template arguments must match the template
7717 parameters in kind (i.e., type, nontype, template), and there
7718 must not be more arguments than there are parameters;
7719 otherwise type deduction fails.
7721 Nontype arguments must match the types of the corresponding
7722 nontype template parameters, or must be convertible to the
7723 types of the corresponding nontype parameters as specified in
7724 _temp.arg.nontype_, otherwise type deduction fails.
7726 All references in the function type of the function template
7727 to the corresponding template parameters are replaced by the
7728 specified template argument values. If a substitution in a
7729 template parameter or in the function type of the function
7730 template results in an invalid type, type deduction fails. */
7732 tree converted_args;
7735 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7736 explicit_targs, NULL_TREE, /*complain=*/0,
7737 /*require_all_arguments=*/0));
7738 if (converted_args == error_mark_node)
7741 fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE);
7742 if (fntype == error_mark_node)
7745 /* Place the explicitly specified arguments in TARGS. */
7746 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7747 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
7750 parms = TYPE_ARG_TYPES (fntype);
7751 /* Never do unification on the 'this' parameter. */
7752 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
7753 parms = TREE_CHAIN (parms);
7757 /* We've been given a return type to match, prepend it. */
7758 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
7759 args = tree_cons (NULL_TREE, return_type, args);
7764 /* We allow incomplete unification without an error message here
7765 because the standard doesn't seem to explicitly prohibit it. Our
7766 callers must be ready to deal with unification failures in any
7768 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7769 targs, parms, args, /*subr=*/0,
7770 strict, /*allow_incomplete*/1, len);
7773 /* All is well so far. Now, check:
7777 When all template arguments have been deduced, all uses of
7778 template parameters in nondeduced contexts are replaced with
7779 the corresponding deduced argument values. If the
7780 substitution results in an invalid type, as described above,
7781 type deduction fails. */
7782 if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE)
7789 /* Adjust types before performing type deduction, as described in
7790 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7791 sections are symmetric. PARM is the type of a function parameter
7792 or the return type of the conversion function. ARG is the type of
7793 the argument passed to the call, or the type of the value
7794 initialized with the result of the conversion function. */
7797 maybe_adjust_types_for_deduction (strict, parm, arg)
7798 unification_kind_t strict;
7811 /* Swap PARM and ARG throughout the remainder of this
7812 function; the handling is precisely symmetric since PARM
7813 will initialize ARG rather than vice versa. */
7821 /* There is nothing to do in this case. */
7825 /* DR 214. [temp.func.order] is underspecified, and leads to no
7826 ordering between things like `T *' and `T const &' for `U *'.
7827 The former has T=U and the latter T=U*. The former looks more
7828 specialized and John Spicer considers it well-formed (the EDG
7829 compiler accepts it).
7831 John also confirms that deduction should proceed as in a function
7832 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
7833 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7834 to an actual call can have such a type.
7836 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7837 If only ARG is a REFERENCE_TYPE, we look through that and then
7838 proceed as with DEDUCE_CALL (which could further convert it). */
7839 if (TREE_CODE (*arg) == REFERENCE_TYPE)
7841 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7843 *arg = TREE_TYPE (*arg);
7850 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7852 /* [temp.deduct.call]
7854 If P is not a reference type:
7856 --If A is an array type, the pointer type produced by the
7857 array-to-pointer standard conversion (_conv.array_) is
7858 used in place of A for type deduction; otherwise,
7860 --If A is a function type, the pointer type produced by
7861 the function-to-pointer standard conversion
7862 (_conv.func_) is used in place of A for type deduction;
7865 --If A is a cv-qualified type, the top level
7866 cv-qualifiers of A's type are ignored for type
7868 if (TREE_CODE (*arg) == ARRAY_TYPE)
7869 *arg = build_pointer_type (TREE_TYPE (*arg));
7870 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
7871 *arg = build_pointer_type (*arg);
7873 *arg = TYPE_MAIN_VARIANT (*arg);
7876 /* [temp.deduct.call]
7878 If P is a cv-qualified type, the top level cv-qualifiers
7879 of P's type are ignored for type deduction. If P is a
7880 reference type, the type referred to by P is used for
7882 *parm = TYPE_MAIN_VARIANT (*parm);
7883 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7885 *parm = TREE_TYPE (*parm);
7886 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
7891 /* Most parms like fn_type_unification.
7893 If SUBR is 1, we're being called recursively (to unify the
7894 arguments of a function or method parameter of a function
7898 type_unification_real (tparms, targs, xparms, xargs, subr,
7899 strict, allow_incomplete, xlen)
7900 tree tparms, targs, xparms, xargs;
7902 unification_kind_t strict;
7903 int allow_incomplete, xlen;
7907 int ntparms = TREE_VEC_LENGTH (tparms);
7909 int saw_undeduced = 0;
7913 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
7914 my_friendly_assert (xparms == NULL_TREE
7915 || TREE_CODE (xparms) == TREE_LIST, 290);
7916 /* ARGS could be NULL (via a call from parse.y to
7917 build_x_function_call). */
7919 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
7920 my_friendly_assert (ntparms > 0, 292);
7925 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
7926 | UNIFY_ALLOW_DERIVED);
7930 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7934 sub_strict = UNIFY_ALLOW_NONE;
7938 sub_strict = UNIFY_ALLOW_NONE;
7954 && parms != void_list_node
7956 && args != void_list_node)
7958 parm = TREE_VALUE (parms);
7959 parms = TREE_CHAIN (parms);
7960 arg = TREE_VALUE (args);
7961 args = TREE_CHAIN (args);
7963 if (arg == error_mark_node)
7965 if (arg == unknown_type_node)
7966 /* We can't deduce anything from this, but we might get all the
7967 template args from other function args. */
7970 /* Conversions will be performed on a function argument that
7971 corresponds with a function parameter that contains only
7972 non-deducible template parameters and explicitly specified
7973 template parameters. */
7974 if (! uses_template_parms (parm))
7979 type = TREE_TYPE (arg);
7986 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
7988 if (same_type_p (parm, type))
7992 /* It might work; we shouldn't check now, because we might
7993 get into infinite recursion. Overload resolution will
8002 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
8003 if (type_unknown_p (arg))
8005 /* [temp.deduct.type] A template-argument can be deduced from
8006 a pointer to function or pointer to member function
8007 argument if the set of overloaded functions does not
8008 contain function templates and at most one of a set of
8009 overloaded functions provides a unique match. */
8011 if (resolve_overloaded_unification
8012 (tparms, targs, parm, arg, strict, sub_strict)
8017 arg = TREE_TYPE (arg);
8021 int arg_strict = sub_strict;
8024 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8026 if (unify (tparms, targs, parm, arg, arg_strict))
8030 /* Are we done with the interesting parms? */
8034 /* Fail if we've reached the end of the parm list, and more args
8035 are present, and the parm list isn't variadic. */
8036 if (args && args != void_list_node && parms == void_list_node)
8038 /* Fail if parms are left and they don't have default values. */
8040 && parms != void_list_node
8041 && TREE_PURPOSE (parms) == NULL_TREE)
8046 for (i = 0; i < ntparms; i++)
8047 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8049 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8051 /* If this is an undeduced nontype parameter that depends on
8052 a type parameter, try another pass; its type may have been
8053 deduced from a later argument than the one from which
8054 this parameter can be deduced. */
8055 if (TREE_CODE (tparm) == PARM_DECL
8056 && uses_template_parms (TREE_TYPE (tparm))
8057 && !saw_undeduced++)
8060 if (!allow_incomplete)
8061 error ("incomplete type unification");
8067 /* Subroutine of type_unification_real. Args are like the variables at the
8068 call site. ARG is an overloaded function (or template-id); we try
8069 deducing template args from each of the overloads, and if only one
8070 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8073 resolve_overloaded_unification (tparms, targs, parm, arg, strict,
8075 tree tparms, targs, parm, arg;
8076 unification_kind_t strict;
8079 tree tempargs = copy_node (targs);
8082 if (TREE_CODE (arg) == ADDR_EXPR)
8083 arg = TREE_OPERAND (arg, 0);
8085 if (TREE_CODE (arg) == COMPONENT_REF)
8086 /* Handle `&x' where `x' is some static or non-static member
8088 arg = TREE_OPERAND (arg, 1);
8090 if (TREE_CODE (arg) == OFFSET_REF)
8091 arg = TREE_OPERAND (arg, 1);
8093 /* Strip baselink information. */
8094 while (TREE_CODE (arg) == TREE_LIST)
8095 arg = TREE_VALUE (arg);
8097 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8099 /* If we got some explicit template args, we need to plug them into
8100 the affected templates before we try to unify, in case the
8101 explicit args will completely resolve the templates in question. */
8103 tree expl_subargs = TREE_OPERAND (arg, 1);
8104 arg = TREE_OPERAND (arg, 0);
8106 for (; arg; arg = OVL_NEXT (arg))
8108 tree fn = OVL_CURRENT (arg);
8111 if (TREE_CODE (fn) != TEMPLATE_DECL)
8114 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8118 elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0,
8120 if (TREE_CODE (elem) == METHOD_TYPE)
8121 elem = build_ptrmemfunc_type (build_pointer_type (elem));
8122 good += try_one_overload (tparms, targs, tempargs, parm, elem,
8123 strict, sub_strict);
8127 else if (TREE_CODE (arg) == OVERLOAD)
8129 for (; arg; arg = OVL_NEXT (arg))
8131 tree type = TREE_TYPE (OVL_CURRENT (arg));
8132 if (TREE_CODE (type) == METHOD_TYPE)
8133 type = build_ptrmemfunc_type (build_pointer_type (type));
8134 good += try_one_overload (tparms, targs, tempargs, parm,
8136 strict, sub_strict);
8142 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8143 to function or pointer to member function argument if the set of
8144 overloaded functions does not contain function templates and at most
8145 one of a set of overloaded functions provides a unique match.
8147 So if we found multiple possibilities, we return success but don't
8152 int i = TREE_VEC_LENGTH (targs);
8154 if (TREE_VEC_ELT (tempargs, i))
8155 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8163 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8164 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8165 different overloads deduce different arguments for a given parm.
8166 Returns 1 on success. */
8169 try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
8171 tree tparms, orig_targs, targs, parm, arg;
8172 unification_kind_t strict;
8179 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8180 to function or pointer to member function argument if the set of
8181 overloaded functions does not contain function templates and at most
8182 one of a set of overloaded functions provides a unique match.
8184 So if this is a template, just return success. */
8186 if (uses_template_parms (arg))
8189 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
8191 /* We don't copy orig_targs for this because if we have already deduced
8192 some template args from previous args, unify would complain when we
8193 try to deduce a template parameter for the same argument, even though
8194 there isn't really a conflict. */
8195 nargs = TREE_VEC_LENGTH (targs);
8196 tempargs = make_tree_vec (nargs);
8198 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
8201 /* First make sure we didn't deduce anything that conflicts with
8202 explicitly specified args. */
8203 for (i = nargs; i--; )
8205 tree elt = TREE_VEC_ELT (tempargs, i);
8206 tree oldelt = TREE_VEC_ELT (orig_targs, i);
8208 if (elt == NULL_TREE)
8210 else if (uses_template_parms (elt))
8212 /* Since we're unifying against ourselves, we will fill in template
8213 args used in the function parm list with our own template parms.
8215 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8218 else if (oldelt && ! template_args_equal (oldelt, elt))
8222 for (i = nargs; i--; )
8224 tree elt = TREE_VEC_ELT (tempargs, i);
8227 TREE_VEC_ELT (targs, i) = elt;
8233 /* Verify that nondeduce template argument agrees with the type
8234 obtained from argument deduction. Return nonzero if the
8239 struct A { typedef int X; };
8240 template <class T, class U> struct C {};
8241 template <class T> struct C<T, typename T::X> {};
8243 Then with the instantiation `C<A, int>', we can deduce that
8244 `T' is `A' but unify () does not check whether `typename T::X'
8245 is `int'. This function ensure that they agree.
8247 TARGS, PARMS are the same as the arguments of unify.
8248 ARGS contains template arguments from all levels. */
8251 verify_class_unification (targs, parms, args)
8252 tree targs, parms, args;
8255 int nparms = TREE_VEC_LENGTH (parms);
8256 tree new_parms = tsubst (parms, add_outermost_template_args (args, targs),
8257 /*complain=*/0, NULL_TREE);
8258 if (new_parms == error_mark_node)
8261 args = INNERMOST_TEMPLATE_ARGS (args);
8263 for (i = 0; i < nparms; i++)
8265 tree parm = TREE_VEC_ELT (new_parms, i);
8266 tree arg = TREE_VEC_ELT (args, i);
8268 /* In case we are deducing from a function argument of a function
8269 templates, some parameters may not be deduced yet. So we
8270 make sure that only fully substituted elements of PARM are
8273 if (!uses_template_parms (parm) && !template_args_equal (parm, arg))
8279 /* PARM is a template class (perhaps with unbound template
8280 parameters). ARG is a fully instantiated type. If ARG can be
8281 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8282 TARGS are as for unify. */
8285 try_class_unification (tparms, targs, parm, arg)
8293 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8294 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8297 /* We need to make a new template argument vector for the call to
8298 unify. If we used TARGS, we'd clutter it up with the result of
8299 the attempted unification, even if this class didn't work out.
8300 We also don't want to commit ourselves to all the unifications
8301 we've already done, since unification is supposed to be done on
8302 an argument-by-argument basis. In other words, consider the
8303 following pathological case:
8305 template <int I, int J, int K>
8308 template <int I, int J>
8309 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8311 template <int I, int J, int K>
8312 void f(S<I, J, K>, S<I, I, I>);
8321 Now, by the time we consider the unification involving `s2', we
8322 already know that we must have `f<0, 0, 0>'. But, even though
8323 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8324 because there are two ways to unify base classes of S<0, 1, 2>
8325 with S<I, I, I>. If we kept the already deduced knowledge, we
8326 would reject the possibility I=1. */
8327 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
8329 /* If unification failed, we're done. */
8330 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8331 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
8337 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8338 have already discovered to be satisfactory. ARG_BINFO is the binfo
8339 for the base class of ARG that we are currently examining. */
8342 get_template_base_recursive (tparms, targs, parm,
8343 arg_binfo, rval, flags)
8353 tree arg = BINFO_TYPE (arg_binfo);
8355 if (!(flags & GTB_IGNORE_TYPE))
8357 tree r = try_class_unification (tparms, targs,
8360 /* If there is more than one satisfactory baseclass, then:
8364 If they yield more than one possible deduced A, the type
8368 if (r && rval && !same_type_p (r, rval))
8369 return error_mark_node;
8374 binfos = BINFO_BASETYPES (arg_binfo);
8375 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8377 /* Process base types. */
8378 for (i = 0; i < n_baselinks; i++)
8380 tree base_binfo = TREE_VEC_ELT (binfos, i);
8383 /* Skip this base, if we've already seen it. */
8384 if (BINFO_MARKED (base_binfo))
8388 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8390 /* When searching for a non-virtual, we cannot mark virtually
8393 SET_BINFO_MARKED (base_binfo);
8395 rval = get_template_base_recursive (tparms, targs,
8399 GTB_VIA_VIRTUAL * this_virtual);
8401 /* If we discovered more than one matching base class, we can
8403 if (rval == error_mark_node)
8404 return error_mark_node;
8410 /* Given a template type PARM and a class type ARG, find the unique
8411 base type in ARG that is an instance of PARM. We do not examine
8412 ARG itself; only its base-classes. If there is no appropriate base
8413 class, return NULL_TREE. If there is more than one, return
8414 error_mark_node. PARM may be the type of a partial specialization,
8415 as well as a plain template type. Used by unify. */
8418 get_template_base (tparms, targs, parm, arg)
8427 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8429 arg_binfo = TYPE_BINFO (complete_type (arg));
8430 rval = get_template_base_recursive (tparms, targs,
8435 /* Since get_template_base_recursive marks the bases classes, we
8436 must unmark them here. */
8437 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
8442 /* Returns the level of DECL, which declares a template parameter. */
8445 template_decl_level (decl)
8448 switch (TREE_CODE (decl))
8452 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8455 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8463 /* Decide whether ARG can be unified with PARM, considering only the
8464 cv-qualifiers of each type, given STRICT as documented for unify.
8465 Returns non-zero iff the unification is OK on that basis.*/
8468 check_cv_quals_for_unify (strict, arg, parm)
8473 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
8474 && !at_least_as_qualified_p (arg, parm))
8477 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
8478 && !at_least_as_qualified_p (parm, arg))
8484 /* Takes parameters as for type_unification. Returns 0 if the
8485 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8486 bitwise or of the following flags:
8489 Require an exact match between PARM and ARG.
8490 UNIFY_ALLOW_MORE_CV_QUAL:
8491 Allow the deduced ARG to be more cv-qualified (by qualification
8492 conversion) than ARG.
8493 UNIFY_ALLOW_LESS_CV_QUAL:
8494 Allow the deduced ARG to be less cv-qualified than ARG.
8495 UNIFY_ALLOW_DERIVED:
8496 Allow the deduced ARG to be a template base class of ARG,
8497 or a pointer to a template base class of the type pointed to by
8499 UNIFY_ALLOW_INTEGER:
8500 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8501 case for more information.
8502 UNIFY_ALLOW_OUTER_LEVEL:
8503 This is the outermost level of a deduction. Used to determine validity
8504 of qualification conversions. A valid qualification conversion must
8505 have const qualified pointers leading up to the inner type which
8506 requires additional CV quals, except at the outer level, where const
8507 is not required [conv.qual]. It would be normal to set this flag in
8508 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8509 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8510 This is the outermost level of a deduction, and PARM can be more CV
8511 qualified at this point.
8512 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8513 This is the outermost level of a deduction, and PARM can be less CV
8514 qualified at this point.
8515 UNIFY_ALLOW_MAX_CORRECTION:
8516 This is an INTEGER_TYPE's maximum value. Used if the range may
8517 have been derived from a size specification, such as an array size.
8518 If the size was given by a nontype template parameter N, the maximum
8519 value will have the form N-1. The flag says that we can (and indeed
8520 must) unify N with (ARG + 1), an exception to the normal rules on
8524 unify (tparms, targs, parm, arg, strict)
8525 tree tparms, targs, parm, arg;
8531 int strict_in = strict;
8533 /* I don't think this will do the right thing with respect to types.
8534 But the only case I've seen it in so far has been array bounds, where
8535 signedness is the only information lost, and I think that will be
8537 while (TREE_CODE (parm) == NOP_EXPR)
8538 parm = TREE_OPERAND (parm, 0);
8540 if (arg == error_mark_node)
8542 if (arg == unknown_type_node)
8543 /* We can't deduce anything from this, but we might get all the
8544 template args from other function args. */
8547 /* If PARM uses template parameters, then we can't bail out here,
8548 even if ARG == PARM, since we won't record unifications for the
8549 template parameters. We might need them if we're trying to
8550 figure out which of two things is more specialized. */
8551 if (arg == parm && !uses_template_parms (parm))
8554 /* Immediately reject some pairs that won't unify because of
8555 cv-qualification mismatches. */
8556 if (TREE_CODE (arg) == TREE_CODE (parm)
8558 /* It is the elements of the array which hold the cv quals of an array
8559 type, and the elements might be template type parms. We'll check
8561 && TREE_CODE (arg) != ARRAY_TYPE
8562 /* We check the cv-qualifiers when unifying with template type
8563 parameters below. We want to allow ARG `const T' to unify with
8564 PARM `T' for example, when computing which of two templates
8565 is more specialized, for example. */
8566 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8567 && !check_cv_quals_for_unify (strict_in, arg, parm))
8570 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
8571 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
8572 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
8573 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
8574 strict &= ~UNIFY_ALLOW_DERIVED;
8575 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8576 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
8577 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
8579 switch (TREE_CODE (parm))
8583 case UNBOUND_CLASS_TEMPLATE:
8584 /* In a type which contains a nested-name-specifier, template
8585 argument values cannot be deduced for template parameters used
8586 within the nested-name-specifier. */
8589 case TEMPLATE_TYPE_PARM:
8590 case TEMPLATE_TEMPLATE_PARM:
8591 case BOUND_TEMPLATE_TEMPLATE_PARM:
8592 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8594 if (TEMPLATE_TYPE_LEVEL (parm)
8595 != template_decl_level (tparm))
8596 /* The PARM is not one we're trying to unify. Just check
8597 to see if it matches ARG. */
8598 return (TREE_CODE (arg) == TREE_CODE (parm)
8599 && same_type_p (parm, arg)) ? 0 : 1;
8600 idx = TEMPLATE_TYPE_IDX (parm);
8601 targ = TREE_VEC_ELT (targs, idx);
8602 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
8604 /* Check for mixed types and values. */
8605 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8606 && TREE_CODE (tparm) != TYPE_DECL)
8607 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8608 && TREE_CODE (tparm) != TEMPLATE_DECL))
8611 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8613 /* ARG must be constructed from a template class or a template
8614 template parameter. */
8615 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
8616 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
8620 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8621 tree parmvec = TYPE_TI_ARGS (parm);
8622 tree argvec = TYPE_TI_ARGS (arg);
8624 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
8627 /* The parameter and argument roles have to be switched here
8628 in order to handle default arguments properly. For example,
8629 template<template <class> class TT> void f(TT<int>)
8630 should be able to accept vector<int> which comes from
8631 template <class T, class Allocator = allocator>
8634 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8638 /* Deduce arguments T, i from TT<T> or TT<i>.
8639 We check each element of PARMVEC and ARGVEC individually
8640 rather than the whole TREE_VEC since they can have
8641 different number of elements. */
8643 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8645 tree t = TREE_VEC_ELT (parmvec, i);
8647 if (unify (tparms, targs, t,
8648 TREE_VEC_ELT (argvec, i),
8653 arg = TYPE_TI_TEMPLATE (arg);
8655 /* Fall through to deduce template name. */
8658 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8659 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8661 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8663 /* Simple cases: Value already set, does match or doesn't. */
8664 if (targ != NULL_TREE && template_args_equal (targ, arg))
8671 /* If PARM is `const T' and ARG is only `int', we don't have
8672 a match unless we are allowing additional qualification.
8673 If ARG is `const int' and PARM is just `T' that's OK;
8674 that binds `const int' to `T'. */
8675 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
8679 /* Consider the case where ARG is `const volatile int' and
8680 PARM is `const T'. Then, T should be `volatile int'. */
8682 cp_build_qualified_type_real (arg,
8684 & ~cp_type_quals (parm),
8686 if (arg == error_mark_node)
8689 /* Simple cases: Value already set, does match or doesn't. */
8690 if (targ != NULL_TREE && same_type_p (targ, arg))
8696 /* Make sure that ARG is not a variable-sized array. (Note that
8697 were talking about variable-sized arrays (like `int[n]'),
8698 rather than arrays of unknown size (like `int[]').) We'll
8699 get very confused by such a type since the bound of the array
8700 will not be computable in an instantiation. Besides, such
8701 types are not allowed in ISO C++, so we can do as we please
8703 if (TREE_CODE (arg) == ARRAY_TYPE
8704 && !uses_template_parms (arg)
8705 && TYPE_DOMAIN (arg)
8706 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8710 TREE_VEC_ELT (targs, idx) = arg;
8713 case TEMPLATE_PARM_INDEX:
8714 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8716 if (TEMPLATE_PARM_LEVEL (parm)
8717 != template_decl_level (tparm))
8718 /* The PARM is not one we're trying to unify. Just check
8719 to see if it matches ARG. */
8720 return (TREE_CODE (arg) == TREE_CODE (parm)
8721 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
8723 idx = TEMPLATE_PARM_IDX (parm);
8724 targ = TREE_VEC_ELT (targs, idx);
8728 int i = (cp_tree_equal (targ, arg) > 0);
8737 /* [temp.deduct.type] If, in the declaration of a function template
8738 with a non-type template-parameter, the non-type
8739 template-parameter is used in an expression in the function
8740 parameter-list and, if the corresponding template-argument is
8741 deduced, the template-argument type shall match the type of the
8742 template-parameter exactly, except that a template-argument
8743 deduced from an array bound may be of any integral type.
8744 The non-type parameter might use already deduced type parameters. */
8745 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
8746 if (same_type_p (TREE_TYPE (arg), tparm))
8748 else if ((strict & UNIFY_ALLOW_INTEGER)
8749 && (TREE_CODE (tparm) == INTEGER_TYPE
8750 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8752 else if (uses_template_parms (tparm))
8753 /* We haven't deduced the type of this parameter yet. Try again
8759 TREE_VEC_ELT (targs, idx) = arg;
8764 if (TREE_CODE (arg) != POINTER_TYPE)
8767 /* [temp.deduct.call]
8769 A can be another pointer or pointer to member type that can
8770 be converted to the deduced A via a qualification
8771 conversion (_conv.qual_).
8773 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8774 This will allow for additional cv-qualification of the
8775 pointed-to types if appropriate. */
8777 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
8778 /* The derived-to-base conversion only persists through one
8779 level of pointers. */
8780 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
8782 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
8783 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
8785 /* Avoid getting confused about cv-quals; don't recurse here.
8786 Pointers to members should really be just OFFSET_TYPE, not
8787 this two-level nonsense... */
8789 parm = TREE_TYPE (parm);
8790 arg = TREE_TYPE (arg);
8794 return unify (tparms, targs, TREE_TYPE (parm),
8795 TREE_TYPE (arg), strict);
8798 case REFERENCE_TYPE:
8799 if (TREE_CODE (arg) != REFERENCE_TYPE)
8801 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8802 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8805 if (TREE_CODE (arg) != ARRAY_TYPE)
8807 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8808 != (TYPE_DOMAIN (arg) == NULL_TREE))
8810 if (TYPE_DOMAIN (parm) != NULL_TREE
8811 && unify (tparms, targs, TYPE_DOMAIN (parm),
8812 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8814 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8823 if (TREE_CODE (arg) != TREE_CODE (parm))
8826 if (TREE_CODE (parm) == INTEGER_TYPE
8827 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8829 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
8830 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
8831 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8833 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
8834 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
8835 TYPE_MAX_VALUE (arg),
8836 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
8839 /* We have already checked cv-qualification at the top of the
8841 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
8844 /* As far as unification is concerned, this wins. Later checks
8845 will invalidate it if necessary. */
8848 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8849 /* Type INTEGER_CST can come from ordinary constant template args. */
8851 while (TREE_CODE (arg) == NOP_EXPR)
8852 arg = TREE_OPERAND (arg, 0);
8854 if (TREE_CODE (arg) != INTEGER_CST)
8856 return !tree_int_cst_equal (parm, arg);
8861 if (TREE_CODE (arg) != TREE_VEC)
8863 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8865 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
8866 if (unify (tparms, targs,
8867 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
8875 if (TREE_CODE (arg) != TREE_CODE (parm))
8878 if (TYPE_PTRMEMFUNC_P (parm))
8880 if (!TYPE_PTRMEMFUNC_P (arg))
8883 return unify (tparms, targs,
8884 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8885 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8889 if (CLASSTYPE_TEMPLATE_INFO (parm))
8893 if (strict_in & UNIFY_ALLOW_DERIVED)
8895 /* First, we try to unify the PARM and ARG directly. */
8896 t = try_class_unification (tparms, targs,
8901 /* Fallback to the special case allowed in
8904 If P is a class, and P has the form
8905 template-id, then A can be a derived class of
8906 the deduced A. Likewise, if P is a pointer to
8907 a class of the form template-id, A can be a
8908 pointer to a derived class pointed to by the
8910 t = get_template_base (tparms, targs,
8913 if (! t || t == error_mark_node)
8917 else if (CLASSTYPE_TEMPLATE_INFO (arg)
8918 && (CLASSTYPE_TI_TEMPLATE (parm)
8919 == CLASSTYPE_TI_TEMPLATE (arg)))
8920 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8921 Then, we should unify `int' and `U'. */
8924 /* There's no chance of unification succeeding. */
8927 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
8928 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
8930 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
8936 if (TREE_CODE (arg) != TREE_CODE (parm))
8939 if (unify (tparms, targs, TREE_TYPE (parm),
8940 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
8942 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
8943 TYPE_ARG_TYPES (arg), 1,
8944 DEDUCE_EXACT, 0, -1);
8948 if (TREE_CODE (arg) != OFFSET_TYPE)
8950 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
8951 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
8953 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
8957 if (arg != decl_constant_value (parm))
8962 /* Matched cases are handled by the ARG == PARM test above. */
8966 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
8967 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
8969 /* We handle this case specially, since it comes up with
8970 arrays. In particular, something like:
8972 template <int N> void f(int (&x)[N]);
8974 Here, we are trying to unify the range type, which
8975 looks like [0 ... (N - 1)]. */
8977 t1 = TREE_OPERAND (parm, 0);
8978 t2 = TREE_OPERAND (parm, 1);
8980 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
8982 return unify (tparms, targs, t1, t, strict);
8984 /* else fall through */
8987 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
8990 /* We're looking at an expression. This can happen with
8994 void foo(S<I>, S<I + 2>);
8996 This is a "nondeduced context":
9000 The nondeduced contexts are:
9002 --A type that is a template-id in which one or more of
9003 the template-arguments is an expression that references
9004 a template-parameter.
9006 In these cases, we assume deduction succeeded, but don't
9007 actually infer any unifications. */
9009 if (!uses_template_parms (parm)
9010 && !template_args_equal (parm, arg))
9016 sorry ("use of `%s' in template type unification",
9017 tree_code_name [(int) TREE_CODE (parm)]);
9023 /* Called if RESULT is explicitly instantiated, or is a member of an
9024 explicitly instantiated class, or if using -frepo and the
9025 instantiation of RESULT has been assigned to this file. */
9028 mark_decl_instantiated (result, extern_p)
9032 if (TREE_CODE (result) != FUNCTION_DECL)
9033 /* The TREE_PUBLIC flag for function declarations will have been
9034 set correctly by tsubst. */
9035 TREE_PUBLIC (result) = 1;
9037 /* We used to set this unconditionally; we moved that to
9038 do_decl_instantiation so it wouldn't get set on members of
9039 explicit class template instantiations. But we still need to set
9040 it here for the 'extern template' case in order to suppress
9041 implicit instantiations. */
9043 SET_DECL_EXPLICIT_INSTANTIATION (result);
9047 DECL_INTERFACE_KNOWN (result) = 1;
9048 DECL_NOT_REALLY_EXTERN (result) = 1;
9050 /* Always make artificials weak. */
9051 if (DECL_ARTIFICIAL (result) && flag_weak)
9052 comdat_linkage (result);
9053 /* For WIN32 we also want to put explicit instantiations in
9054 linkonce sections. */
9055 else if (TREE_PUBLIC (result))
9056 maybe_make_one_only (result);
9058 else if (TREE_CODE (result) == FUNCTION_DECL)
9062 /* Given two function templates PAT1 and PAT2, return:
9064 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9066 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9067 -1 if PAT2 is more specialized than PAT1.
9068 0 if neither is more specialized.
9070 LEN is passed through to fn_type_unification. */
9073 more_specialized (pat1, pat2, deduce, len)
9081 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9082 NULL_TREE, 0, deduce, len);
9086 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9087 NULL_TREE, 0, deduce, len);
9094 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9096 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9097 -1 if PAT2 is more specialized than PAT1.
9098 0 if neither is more specialized. */
9101 more_specialized_class (pat1, pat2)
9107 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9108 TREE_PURPOSE (pat2));
9112 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9113 TREE_PURPOSE (pat1));
9120 /* Return the template arguments that will produce the function signature
9121 DECL from the function template FN, with the explicit template
9122 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9123 also match. Return NULL_TREE if no satisfactory arguments could be
9124 found. DEDUCE and LEN are passed through to fn_type_unification. */
9127 get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
9128 tree fn, decl, explicit_args;
9129 int check_rettype, deduce, len;
9131 int ntparms = DECL_NTPARMS (fn);
9132 tree targs = make_tree_vec (ntparms);
9134 tree decl_arg_types;
9137 /* Substitute the explicit template arguments into the type of DECL.
9138 The call to fn_type_unification will handle substitution into the
9140 decl_type = TREE_TYPE (decl);
9141 if (explicit_args && uses_template_parms (decl_type))
9144 tree converted_args;
9146 if (DECL_TEMPLATE_INFO (decl))
9147 tmpl = DECL_TI_TEMPLATE (decl);
9149 /* We can get here for some illegal specializations. */
9153 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9154 explicit_args, NULL_TREE,
9156 /*require_all_arguments=*/0));
9157 if (converted_args == error_mark_node)
9160 decl_type = tsubst (decl_type, converted_args, /*complain=*/0,
9162 if (decl_type == error_mark_node)
9166 decl_arg_types = TYPE_ARG_TYPES (decl_type);
9167 /* Never do unification on the 'this' parameter. */
9168 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9169 decl_arg_types = TREE_CHAIN (decl_arg_types);
9171 i = fn_type_unification (fn, explicit_args, targs,
9173 (check_rettype || DECL_CONV_FN_P (fn)
9174 ? TREE_TYPE (decl_type) : NULL_TREE),
9183 /* For most uses, we want to check the return type. */
9186 get_bindings (fn, decl, explicit_args)
9187 tree fn, decl, explicit_args;
9189 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
9192 /* But for resolve_overloaded_unification, we only care about the parameter
9196 get_bindings_overload (fn, decl, explicit_args)
9197 tree fn, decl, explicit_args;
9199 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
9202 /* Return the innermost template arguments that, when applied to a
9203 template specialization whose innermost template parameters are
9204 TPARMS, and whose specialization arguments are PARMS, yield the
9207 For example, suppose we have:
9209 template <class T, class U> struct S {};
9210 template <class T> struct S<T*, int> {};
9212 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9213 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9214 int}. The resulting vector will be {double}, indicating that `T'
9215 is bound to `double'. */
9218 get_class_bindings (tparms, parms, args)
9219 tree tparms, parms, args;
9221 int i, ntparms = TREE_VEC_LENGTH (tparms);
9222 tree vec = make_tree_vec (ntparms);
9224 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9228 for (i = 0; i < ntparms; ++i)
9229 if (! TREE_VEC_ELT (vec, i))
9232 if (verify_class_unification (vec, parms, args))
9238 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9239 Pick the most specialized template, and return the corresponding
9240 instantiation, or if there is no corresponding instantiation, the
9241 template itself. If there is no most specialized template,
9242 error_mark_node is returned. If there are no templates at all,
9243 NULL_TREE is returned. */
9246 most_specialized_instantiation (instantiations)
9247 tree instantiations;
9252 if (!instantiations)
9255 champ = instantiations;
9256 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
9258 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9266 fn = TREE_CHAIN (fn);
9268 return error_mark_node;
9274 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
9276 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9279 return error_mark_node;
9282 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9285 /* Return the most specialized of the list of templates in FNS that can
9286 produce an instantiation matching DECL, given the explicit template
9287 arguments EXPLICIT_ARGS. */
9290 most_specialized (fns, decl, explicit_args)
9291 tree fns, decl, explicit_args;
9293 tree candidates = NULL_TREE;
9296 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9298 tree candidate = TREE_VALUE (fn);
9300 args = get_bindings (candidate, decl, explicit_args);
9302 candidates = tree_cons (NULL_TREE, candidate, candidates);
9305 return most_specialized_instantiation (candidates);
9308 /* If DECL is a specialization of some template, return the most
9309 general such template. Otherwise, returns NULL_TREE.
9313 template <class T> struct S { template <class U> void f(U); };
9315 if TMPL is `template <class U> void S<int>::f(U)' this will return
9316 the full template. This function will not trace past partial
9317 specializations, however. For example, given in addition:
9319 template <class T> struct S<T*> { template <class U> void f(U); };
9321 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9322 `template <class T> template <class U> S<T*>::f(U)'. */
9325 most_general_template (decl)
9328 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9329 an immediate specialization. */
9330 if (TREE_CODE (decl) == FUNCTION_DECL)
9332 if (DECL_TEMPLATE_INFO (decl)) {
9333 decl = DECL_TI_TEMPLATE (decl);
9335 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9337 if (TREE_CODE (decl) != TEMPLATE_DECL)
9343 /* Look for more and more general templates. */
9344 while (DECL_TEMPLATE_INFO (decl))
9346 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9347 in some cases. (See cp-tree.h for details.) */
9348 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9351 /* Stop if we run into an explicitly specialized class template. */
9352 if (!DECL_NAMESPACE_SCOPE_P (decl)
9353 && DECL_CONTEXT (decl)
9354 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
9357 decl = DECL_TI_TEMPLATE (decl);
9363 /* Return the most specialized of the class template specializations
9364 of TMPL which can produce an instantiation matching ARGS, or
9365 error_mark_node if the choice is ambiguous. */
9368 most_specialized_class (tmpl, args)
9372 tree list = NULL_TREE;
9377 tmpl = most_general_template (tmpl);
9378 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
9381 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9384 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9385 TREE_TYPE (list) = TREE_TYPE (t);
9395 for (; t; t = TREE_CHAIN (t))
9397 fate = more_specialized_class (champ, t);
9406 return error_mark_node;
9412 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9414 fate = more_specialized_class (champ, t);
9416 return error_mark_node;
9422 /* called from the parser. */
9425 do_decl_instantiation (declspecs, declarator, storage)
9426 tree declspecs, declarator, storage;
9428 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL);
9429 tree result = NULL_TREE;
9433 /* An error occurred, for which grokdeclarator has already issued
9434 an appropriate message. */
9436 else if (! DECL_LANG_SPECIFIC (decl))
9438 error ("explicit instantiation of non-template `%#D'", decl);
9441 else if (TREE_CODE (decl) == VAR_DECL)
9443 /* There is an asymmetry here in the way VAR_DECLs and
9444 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9445 the latter, the DECL we get back will be marked as a
9446 template instantiation, and the appropriate
9447 DECL_TEMPLATE_INFO will be set up. This does not happen for
9448 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9449 should handle VAR_DECLs as it currently handles
9451 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9452 if (result && TREE_CODE (result) != VAR_DECL)
9454 error ("no matching template for `%D' found", result);
9458 else if (TREE_CODE (decl) != FUNCTION_DECL)
9460 error ("explicit instantiation of `%#D'", decl);
9466 /* Check for various error cases. Note that if the explicit
9467 instantiation is legal the RESULT will currently be marked as an
9468 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9469 until we get here. */
9471 if (DECL_TEMPLATE_SPECIALIZATION (result))
9475 No program shall both explicitly instantiate and explicitly
9476 specialize a template. */
9477 pedwarn ("explicit instantiation of `%#D' after", result);
9478 cp_pedwarn_at ("explicit specialization here", result);
9481 else if (DECL_EXPLICIT_INSTANTIATION (result))
9485 No program shall explicitly instantiate any template more
9488 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9489 instantiation was `extern' and the second is not, and EXTERN_P for
9490 the opposite case. If -frepo, chances are we already got marked
9491 as an explicit instantiation because of the repo file. */
9492 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
9493 pedwarn ("duplicate explicit instantiation of `%#D'", result);
9495 /* If we've already instantiated the template, just return now. */
9496 if (DECL_INTERFACE_KNOWN (result))
9499 else if (!DECL_IMPLICIT_INSTANTIATION (result))
9501 error ("no matching template for `%D' found", result);
9504 else if (!DECL_TEMPLATE_INFO (result))
9506 pedwarn ("explicit instantiation of non-template `%#D'", result);
9510 if (flag_external_templates)
9513 if (storage == NULL_TREE)
9515 else if (storage == ridpointers[(int) RID_EXTERN])
9518 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9522 error ("storage class `%D' applied to template instantiation",
9525 SET_DECL_EXPLICIT_INSTANTIATION (result);
9526 mark_decl_instantiated (result, extern_p);
9527 repo_template_instantiated (result, extern_p);
9529 instantiate_decl (result, /*defer_ok=*/1);
9533 mark_class_instantiated (t, extern_p)
9537 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
9538 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9539 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
9540 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9543 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9544 rest_of_type_compilation (t, 1);
9548 /* Perform an explicit instantiation of template class T. STORAGE, if
9549 non-null, is the RID for extern, inline or static. COMPLAIN is
9550 non-zero if this is called from the parser, zero if called recursively,
9551 since the standard is unclear (as detailed below). */
9554 do_type_instantiation (t, storage, complain)
9562 if (TREE_CODE (t) == TYPE_DECL)
9565 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
9567 error ("explicit instantiation of non-template type `%T'", t);
9573 /* With -fexternal-templates, explicit instantiations are treated the same
9574 as implicit ones. */
9575 if (flag_external_templates)
9578 if (!COMPLETE_TYPE_P (t))
9581 error ("explicit instantiation of `%#T' before definition of template",
9586 if (storage != NULL_TREE)
9589 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9590 IDENTIFIER_POINTER (storage));
9592 if (storage == ridpointers[(int) RID_INLINE])
9594 else if (storage == ridpointers[(int) RID_EXTERN])
9596 else if (storage == ridpointers[(int) RID_STATIC])
9600 error ("storage class `%D' applied to template instantiation",
9606 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9610 No program shall both explicitly instantiate and explicitly
9611 specialize a template. */
9614 error ("explicit instantiation of `%#T' after", t);
9615 cp_error_at ("explicit specialization here", t);
9619 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
9623 No program shall explicitly instantiate any template more
9626 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9627 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9628 are we already got marked as an explicit instantiation because of the
9629 repo file. All these cases are OK. */
9630 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
9632 pedwarn ("duplicate explicit instantiation of `%#T'", t);
9634 /* If we've already instantiated the template, just return now. */
9635 if (!CLASSTYPE_INTERFACE_ONLY (t))
9639 mark_class_instantiated (t, extern_p);
9640 repo_template_instantiated (t, extern_p);
9648 /* In contrast to implicit instantiation, where only the
9649 declarations, and not the definitions, of members are
9650 instantiated, we have here:
9654 The explicit instantiation of a class template specialization
9655 implies the instantiation of all of its members not
9656 previously explicitly specialized in the translation unit
9657 containing the explicit instantiation.
9659 Of course, we can't instantiate member template classes, since
9660 we don't have any arguments for them. Note that the standard
9661 is unclear on whether the instantiation of the members are
9662 *explicit* instantiations or not. We choose to be generous,
9663 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9664 the explicit instantiation of a class where some of the members
9665 have no definition in the current translation unit. */
9668 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
9669 if (TREE_CODE (tmp) == FUNCTION_DECL
9670 && DECL_TEMPLATE_INSTANTIATION (tmp))
9672 mark_decl_instantiated (tmp, extern_p);
9673 repo_template_instantiated (tmp, extern_p);
9675 instantiate_decl (tmp, /*defer_ok=*/1);
9678 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9679 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
9681 mark_decl_instantiated (tmp, extern_p);
9682 repo_template_instantiated (tmp, extern_p);
9684 instantiate_decl (tmp, /*defer_ok=*/1);
9687 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
9688 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9689 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
9690 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
9694 /* Given a function DECL, which is a specialization of TMPL, modify
9695 DECL to be a re-instantiation of TMPL with the same template
9696 arguments. TMPL should be the template into which tsubst'ing
9697 should occur for DECL, not the most general template.
9699 One reason for doing this is a scenario like this:
9702 void f(const T&, int i);
9704 void g() { f(3, 7); }
9707 void f(const T& t, const int i) { }
9709 Note that when the template is first instantiated, with
9710 instantiate_template, the resulting DECL will have no name for the
9711 first parameter, and the wrong type for the second. So, when we go
9712 to instantiate the DECL, we regenerate it. */
9715 regenerate_decl_from_template (decl, tmpl)
9719 /* The most general version of TMPL. */
9721 /* The arguments used to instantiate DECL, from the most general
9728 args = DECL_TI_ARGS (decl);
9729 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9731 /* Unregister the specialization so that when we tsubst we will not
9732 just return DECL. We don't have to unregister DECL from TMPL
9733 because if would only be registered there if it were a partial
9734 instantiation of a specialization, which it isn't: it's a full
9736 gen_tmpl = most_general_template (tmpl);
9737 unregistered = unregister_specialization (decl, gen_tmpl);
9739 /* If the DECL was not unregistered then something peculiar is
9740 happening: we created a specialization but did not call
9741 register_specialization for it. */
9742 my_friendly_assert (unregistered, 0);
9744 if (TREE_CODE (decl) == VAR_DECL)
9745 /* Make sure that we can see identifiers, and compute access
9746 correctly, for the class members used in the declaration of
9747 this static variable. */
9748 pushclass (DECL_CONTEXT (decl), 2);
9750 /* Do the substitution to get the new declaration. */
9751 new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE);
9753 if (TREE_CODE (decl) == VAR_DECL)
9755 /* Set up DECL_INITIAL, since tsubst doesn't. */
9756 DECL_INITIAL (new_decl) =
9757 tsubst_expr (DECL_INITIAL (code_pattern), args,
9758 /*complain=*/1, DECL_TI_TEMPLATE (decl));
9759 /* Pop the class context we pushed above. */
9762 else if (TREE_CODE (decl) == FUNCTION_DECL)
9764 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9766 DECL_INITIAL (new_decl) = error_mark_node;
9767 /* And don't complain about a duplicate definition. */
9768 DECL_INITIAL (decl) = NULL_TREE;
9771 /* The immediate parent of the new template is still whatever it was
9772 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9773 general template. We also reset the DECL_ASSEMBLER_NAME since
9774 tsubst always calculates the name as if the function in question
9775 were really a template instance, and sometimes, with friend
9776 functions, this is not so. See tsubst_friend_function for
9778 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9779 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
9780 COPY_DECL_RTL (decl, new_decl);
9781 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
9783 /* Call duplicate decls to merge the old and new declarations. */
9784 duplicate_decls (new_decl, decl);
9786 /* Now, re-register the specialization. */
9787 register_specialization (decl, gen_tmpl, args);
9790 /* Produce the definition of D, a _DECL generated from a template. If
9791 DEFER_OK is non-zero, then we don't have to actually do the
9792 instantiation now; we just have to do it sometime. */
9795 instantiate_decl (d, defer_ok)
9799 tree tmpl = DECL_TI_TEMPLATE (d);
9800 tree args = DECL_TI_ARGS (d);
9805 int pattern_defined;
9808 const char *file = input_filename;
9810 /* This function should only be used to instantiate templates for
9811 functions and static member variables. */
9812 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9813 || TREE_CODE (d) == VAR_DECL, 0);
9815 /* Don't instantiate cloned functions. Instead, instantiate the
9816 functions they cloned. */
9817 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
9818 d = DECL_CLONED_FUNCTION (d);
9820 if (DECL_TEMPLATE_INSTANTIATED (d))
9821 /* D has already been instantiated. It might seem reasonable to
9822 check whether or not D is an explicit instantiation, and, if so,
9823 stop here. But when an explicit instantiation is deferred
9824 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9825 is set, even though we still need to do the instantiation. */
9828 /* If we already have a specialization of this declaration, then
9829 there's no reason to instantiate it. Note that
9830 retrieve_specialization gives us both instantiations and
9831 specializations, so we must explicitly check
9832 DECL_TEMPLATE_SPECIALIZATION. */
9833 gen_tmpl = most_general_template (tmpl);
9834 spec = retrieve_specialization (gen_tmpl, args);
9835 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9838 /* This needs to happen before any tsubsting. */
9839 if (! push_tinst_level (d))
9842 timevar_push (TV_PARSE);
9844 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9845 for the instantiation. This is not always the most general
9846 template. Consider, for example:
9849 struct S { template <class U> void f();
9850 template <> void f<int>(); };
9852 and an instantiation of S<double>::f<int>. We want TD to be the
9853 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9855 while (/* An instantiation cannot have a definition, so we need a
9856 more general template. */
9857 DECL_TEMPLATE_INSTANTIATION (td)
9858 /* We must also deal with friend templates. Given:
9860 template <class T> struct S {
9861 template <class U> friend void f() {};
9864 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9865 so far as the language is concerned, but that's still
9866 where we get the pattern for the instantiation from. On
9867 other hand, if the definition comes outside the class, say:
9869 template <class T> struct S {
9870 template <class U> friend void f();
9872 template <class U> friend void f() {}
9874 we don't need to look any further. That's what the check for
9875 DECL_INITIAL is for. */
9876 || (TREE_CODE (d) == FUNCTION_DECL
9877 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9878 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))))
9880 /* The present template, TD, should not be a definition. If it
9881 were a definition, we should be using it! Note that we
9882 cannot restructure the loop to just keep going until we find
9883 a template with a definition, since that might go too far if
9884 a specialization was declared, but not defined. */
9885 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9886 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9889 /* Fetch the more general template. */
9890 td = DECL_TI_TEMPLATE (td);
9893 code_pattern = DECL_TEMPLATE_RESULT (td);
9895 if (TREE_CODE (d) == FUNCTION_DECL)
9896 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
9898 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
9900 lineno = DECL_SOURCE_LINE (d);
9901 input_filename = DECL_SOURCE_FILE (d);
9903 if (pattern_defined)
9905 /* Let the repository code that this template definition is
9908 The repository doesn't need to know about cloned functions
9909 because they never actually show up in the object file. It
9910 does need to know about the clones; those are the symbols
9911 that the linker will be emitting error messages about. */
9912 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
9913 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
9917 for (t = TREE_CHAIN (d);
9918 t && DECL_CLONED_FUNCTION_P (t);
9920 repo_template_used (t);
9923 repo_template_used (d);
9925 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
9927 if (flag_alt_external_templates)
9929 if (interface_unknown)
9930 warn_if_unknown_interface (d);
9932 else if (DECL_INTERFACE_KNOWN (code_pattern))
9934 DECL_INTERFACE_KNOWN (d) = 1;
9935 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
9938 warn_if_unknown_interface (code_pattern);
9942 import_export_decl (d);
9945 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
9946 && DECL_INITIAL (d) == NULL_TREE)
9947 /* We should have set up DECL_INITIAL in instantiate_class_template. */
9949 /* Reject all external templates except inline functions. */
9950 else if (DECL_INTERFACE_KNOWN (d)
9951 && ! DECL_NOT_REALLY_EXTERN (d)
9952 && ! (TREE_CODE (d) == FUNCTION_DECL
9953 && DECL_INLINE (d)))
9955 /* Defer all other templates, unless we have been explicitly
9956 forbidden from doing so. We restore the source position here
9957 because it's used by add_pending_template. */
9958 else if (! pattern_defined || defer_ok)
9961 input_filename = file;
9963 if (at_eof && !pattern_defined
9964 && DECL_EXPLICIT_INSTANTIATION (d))
9967 The definition of a non-exported function template, a
9968 non-exported member function template, or a non-exported
9969 member function or static data member of a class template
9970 shall be present in every translation unit in which it is
9971 explicitly instantiated. */
9973 ("explicit instantiation of `%D' but no definition available", d);
9975 add_pending_template (d);
9979 need_push = !global_bindings_p ();
9981 push_to_top_level ();
9983 /* We're now committed to instantiating this template. Mark it as
9984 instantiated so that recursive calls to instantiate_decl do not
9985 try to instantiate it again. */
9986 DECL_TEMPLATE_INSTANTIATED (d) = 1;
9988 /* Regenerate the declaration in case the template has been modified
9989 by a subsequent redeclaration. */
9990 regenerate_decl_from_template (d, td);
9992 /* We already set the file and line above. Reset them now in case
9993 they changed as a result of calling regenerate_decl_from_template. */
9994 lineno = DECL_SOURCE_LINE (d);
9995 input_filename = DECL_SOURCE_FILE (d);
9997 if (TREE_CODE (d) == VAR_DECL)
9999 DECL_IN_AGGR_P (d) = 0;
10000 if (DECL_INTERFACE_KNOWN (d))
10001 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10004 DECL_EXTERNAL (d) = 1;
10005 DECL_NOT_REALLY_EXTERN (d) = 1;
10007 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0);
10009 else if (TREE_CODE (d) == FUNCTION_DECL)
10011 htab_t saved_local_specializations;
10013 /* Save away the current list, in case we are instantiating one
10014 template from within the body of another. */
10015 saved_local_specializations = local_specializations;
10017 /* Set up the list of local specializations. */
10018 local_specializations = htab_create (37,
10023 /* Set up context. */
10024 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
10026 /* Substitute into the body of the function. */
10027 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
10028 /*complain=*/1, tmpl);
10030 /* We don't need the local specializations any more. */
10031 htab_delete (local_specializations);
10032 local_specializations = saved_local_specializations;
10034 /* Finish the function. */
10035 expand_body (finish_function (0));
10038 /* We're not deferring instantiation any more. */
10039 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10042 pop_from_top_level ();
10046 input_filename = file;
10048 pop_tinst_level ();
10050 timevar_pop (TV_PARSE);
10055 /* Run through the list of templates that we wish we could
10056 instantiate, and instantiate any we can. */
10059 instantiate_pending_templates ()
10062 tree last = NULL_TREE;
10063 int instantiated_something = 0;
10070 t = &pending_templates;
10073 tree instantiation = TREE_VALUE (*t);
10075 reopen_tinst_level (TREE_PURPOSE (*t));
10077 if (TYPE_P (instantiation))
10081 if (!COMPLETE_TYPE_P (instantiation))
10083 instantiate_class_template (instantiation);
10084 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10085 for (fn = TYPE_METHODS (instantiation);
10087 fn = TREE_CHAIN (fn))
10088 if (! DECL_ARTIFICIAL (fn))
10089 instantiate_decl (fn, /*defer_ok=*/0);
10090 if (COMPLETE_TYPE_P (instantiation))
10092 instantiated_something = 1;
10097 if (COMPLETE_TYPE_P (instantiation))
10098 /* If INSTANTIATION has been instantiated, then we don't
10099 need to consider it again in the future. */
10100 *t = TREE_CHAIN (*t);
10104 t = &TREE_CHAIN (*t);
10109 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
10110 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10112 instantiation = instantiate_decl (instantiation,
10114 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10116 instantiated_something = 1;
10121 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
10122 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10123 /* If INSTANTIATION has been instantiated, then we don't
10124 need to consider it again in the future. */
10125 *t = TREE_CHAIN (*t);
10129 t = &TREE_CHAIN (*t);
10133 current_tinst_level = NULL_TREE;
10135 last_pending_template = last;
10137 while (reconsider);
10139 return instantiated_something;
10142 /* Substitute ARGVEC into T, which is a list of initializers for
10143 either base class or a non-static data member. The TREE_PURPOSEs
10144 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10145 instantiate_decl. */
10148 tsubst_initializer_list (t, argvec)
10151 tree first = NULL_TREE;
10154 for (; t; t = TREE_CHAIN (t))
10160 decl = tsubst_copy (TREE_PURPOSE (t), argvec, /*complain=*/1,
10162 init = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1,
10167 else if (TREE_CODE (init) == TREE_LIST)
10168 for (val = init; val; val = TREE_CHAIN (val))
10169 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
10171 init = convert_from_reference (init);
10173 *p = build_tree_list (decl, init);
10174 p = &TREE_CHAIN (*p);
10179 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10182 set_current_access_from_decl (decl)
10185 if (TREE_PRIVATE (decl))
10186 current_access_specifier = access_private_node;
10187 else if (TREE_PROTECTED (decl))
10188 current_access_specifier = access_protected_node;
10190 current_access_specifier = access_public_node;
10193 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10194 is the instantiation (which should have been created with
10195 start_enum) and ARGS are the template arguments to use. */
10198 tsubst_enum (tag, newtag, args)
10205 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
10209 /* Note that in a template enum, the TREE_VALUE is the
10210 CONST_DECL, not the corresponding INTEGER_CST. */
10211 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
10212 args, /*complain=*/1,
10215 /* Give this enumeration constant the correct access. */
10216 set_current_access_from_decl (TREE_VALUE (e));
10218 /* Actually build the enumerator itself. */
10219 build_enumerator (TREE_PURPOSE (e), value, newtag);
10222 finish_enum (newtag);
10223 DECL_SOURCE_LINE (TYPE_NAME (newtag)) = DECL_SOURCE_LINE (TYPE_NAME (tag));
10224 DECL_SOURCE_FILE (TYPE_NAME (newtag)) = DECL_SOURCE_FILE (TYPE_NAME (tag));
10227 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10228 its type -- but without substituting the innermost set of template
10229 arguments. So, innermost set of template parameters will appear in
10230 the type. If CONTEXTP is non-NULL, then the partially substituted
10231 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10232 will be filled in with the substituted template parameters, if it
10236 get_mostly_instantiated_function_type (decl, contextp, tparmsp)
10241 tree context = NULL_TREE;
10248 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10249 targs = DECL_TI_ARGS (decl);
10250 tparms = DECL_TEMPLATE_PARMS (tmpl);
10251 parm_depth = TMPL_PARMS_DEPTH (tparms);
10253 /* There should be as many levels of arguments as there are levels
10255 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10257 fn_type = TREE_TYPE (tmpl);
10258 if (DECL_STATIC_FUNCTION_P (decl))
10259 context = DECL_CONTEXT (decl);
10261 if (parm_depth == 1)
10262 /* No substitution is necessary. */
10269 /* Replace the innermost level of the TARGS with NULL_TREEs to
10270 let tsubst know not to substitute for those parameters. */
10271 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
10272 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10273 SET_TMPL_ARGS_LEVEL (partial_args, i,
10274 TMPL_ARGS_LEVEL (targs, i));
10275 SET_TMPL_ARGS_LEVEL (partial_args,
10276 TMPL_ARGS_DEPTH (targs),
10277 make_tree_vec (DECL_NTPARMS (tmpl)));
10279 /* Now, do the (partial) substitution to figure out the
10280 appropriate function type. */
10281 fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE);
10282 if (DECL_STATIC_FUNCTION_P (decl))
10283 context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE);
10285 /* Substitute into the template parameters to obtain the real
10286 innermost set of parameters. This step is important if the
10287 innermost set of template parameters contains value
10288 parameters whose types depend on outer template parameters. */
10289 TREE_VEC_LENGTH (partial_args)--;
10290 tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1);
10294 *contextp = context;
10301 /* Return truthvalue if we're processing a template different from
10302 the last one involved in diagnostics. */
10304 problematic_instantiation_changed ()
10306 return last_template_error_tick != tinst_level_tick;
10309 /* Remember current template involved in diagnostics. */
10311 record_last_problematic_instantiation ()
10313 last_template_error_tick = tinst_level_tick;
10317 current_instantiation ()
10319 return current_tinst_level;
10322 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10323 type. Return zero for ok, non-zero for disallowed. If COMPLAIN is
10324 non-zero, then complain. */
10327 invalid_nontype_parm_type_p (type, complain)
10331 if (INTEGRAL_TYPE_P (type))
10333 else if (POINTER_TYPE_P (type))
10335 else if (TYPE_PTRMEM_P (type))
10337 else if (TYPE_PTRMEMFUNC_P (type))
10339 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10341 else if (TREE_CODE (type) == TYPENAME_TYPE)
10345 error ("`%#T' is not a valid type for a template constant parameter",