1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 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". */
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) PARAMS ((tree
, void*));
52 extern struct obstack permanent_obstack
;
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the
57 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
58 the instantiate request occurred; the TREE_VALUE is a either a DECL
59 (for a function or static data member), or a TYPE (for a class)
60 indicating what we are hoping to instantiate. */
61 static tree pending_templates
;
62 static tree
*template_tail
= &pending_templates
;
64 static tree maybe_templates
;
65 static tree
*maybe_template_tail
= &maybe_templates
;
67 int processing_template_parmlist
;
68 static int template_header_count
;
70 static tree saved_trees
;
71 static varray_type inline_parm_levels
;
72 static size_t inline_parm_levels_used
;
74 /* A map from local variable declarations in the body of the template
75 presently being instantiated to the corresponding instantiated
77 static htab_t local_specializations
;
79 #define obstack_chunk_alloc xmalloc
80 #define obstack_chunk_free free
82 #define UNIFY_ALLOW_NONE 0
83 #define UNIFY_ALLOW_MORE_CV_QUAL 1
84 #define UNIFY_ALLOW_LESS_CV_QUAL 2
85 #define UNIFY_ALLOW_DERIVED 4
86 #define UNIFY_ALLOW_INTEGER 8
88 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
89 virtual, or a base class of a virtual
91 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
92 type with the desired type. */
94 static int resolve_overloaded_unification
PARAMS ((tree
, tree
, tree
, tree
,
95 unification_kind_t
, int));
96 static int try_one_overload
PARAMS ((tree
, tree
, tree
, tree
, tree
,
97 unification_kind_t
, int));
98 static int unify
PARAMS ((tree
, tree
, tree
, tree
, int));
99 static void add_pending_template
PARAMS ((tree
));
100 static int push_tinst_level
PARAMS ((tree
));
101 static tree classtype_mangled_name
PARAMS ((tree
));
102 static char *mangle_class_name_for_template
PARAMS ((char *, tree
, tree
));
103 static tree tsubst_expr_values
PARAMS ((tree
, tree
));
104 static int list_eq
PARAMS ((tree
, tree
));
105 static tree get_class_bindings
PARAMS ((tree
, tree
, tree
));
106 static tree coerce_template_parms
PARAMS ((tree
, tree
, tree
, int, int));
107 static void tsubst_enum
PARAMS ((tree
, tree
, tree
));
108 static tree add_to_template_args
PARAMS ((tree
, tree
));
109 static tree add_outermost_template_args
PARAMS ((tree
, tree
));
110 static void maybe_adjust_types_for_deduction
PARAMS ((unification_kind_t
, tree
*,
112 static int type_unification_real
PARAMS ((tree
, tree
, tree
, tree
,
113 int, unification_kind_t
, int));
114 static void note_template_header
PARAMS ((int));
115 static tree maybe_fold_nontype_arg
PARAMS ((tree
));
116 static tree convert_nontype_argument
PARAMS ((tree
, tree
));
117 static tree convert_template_argument
PARAMS ((tree
, tree
, tree
, int,
119 static tree get_bindings_overload
PARAMS ((tree
, tree
, tree
));
120 static int for_each_template_parm
PARAMS ((tree
, tree_fn_t
, void*));
121 static tree build_template_parm_index
PARAMS ((int, int, int, tree
, tree
));
122 static int inline_needs_template_parms
PARAMS ((tree
));
123 static void push_inline_template_parms_recursive
PARAMS ((tree
, int));
124 static tree retrieve_specialization
PARAMS ((tree
, tree
));
125 static tree retrieve_local_specialization
PARAMS ((tree
));
126 static tree register_specialization
PARAMS ((tree
, tree
, tree
));
127 static tree register_local_specialization
PARAMS ((tree
, tree
));
128 static int unregister_specialization
PARAMS ((tree
, tree
));
129 static tree reduce_template_parm_level
PARAMS ((tree
, tree
, int));
130 static tree build_template_decl
PARAMS ((tree
, tree
));
131 static int mark_template_parm
PARAMS ((tree
, void *));
132 static tree tsubst_friend_function
PARAMS ((tree
, tree
));
133 static tree tsubst_friend_class
PARAMS ((tree
, tree
));
134 static tree get_bindings_real
PARAMS ((tree
, tree
, tree
, int));
135 static int template_decl_level
PARAMS ((tree
));
136 static tree maybe_get_template_decl_from_type_decl
PARAMS ((tree
));
137 static int check_cv_quals_for_unify
PARAMS ((int, tree
, tree
));
138 static tree tsubst_template_arg_vector
PARAMS ((tree
, tree
, int));
139 static tree tsubst_template_parms
PARAMS ((tree
, tree
, int));
140 static void regenerate_decl_from_template
PARAMS ((tree
, tree
));
141 static tree most_specialized
PARAMS ((tree
, tree
, tree
));
142 static tree most_specialized_class
PARAMS ((tree
, tree
));
143 static void set_mangled_name_for_template_decl
PARAMS ((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
, 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 tree try_class_unification
PARAMS ((tree
, tree
, tree
, tree
));
158 static int coerce_template_template_parms
PARAMS ((tree
, tree
, int,
160 static tree determine_specialization
PARAMS ((tree
, tree
, tree
*, int));
161 static int template_args_equal
PARAMS ((tree
, tree
));
162 static void print_template_context
PARAMS ((int));
163 static void tsubst_default_arguments
PARAMS ((tree
));
164 static tree for_each_template_parm_r
PARAMS ((tree
*, int *, void *));
165 static tree instantiate_clone
PARAMS ((tree
, tree
));
167 /* Called once to initialize pt.c. */
172 ggc_add_tree_root (&pending_templates
, 1);
173 ggc_add_tree_root (&maybe_templates
, 1);
174 ggc_add_tree_root (&saved_trees
, 1);
177 /* Do any processing required when DECL (a member template declaration
178 using TEMPLATE_PARAMETERS as its innermost parameter list) is
179 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
180 it is a specialization, in which case the DECL itself is returned. */
183 finish_member_template_decl (decl
)
186 if (decl
== NULL_TREE
|| decl
== void_type_node
)
188 else if (decl
== error_mark_node
)
189 /* By returning NULL_TREE, the parser will just ignore this
190 declaration. We have already issued the error. */
192 else if (TREE_CODE (decl
) == TREE_LIST
)
194 /* Assume that the class is the only declspec. */
195 decl
= TREE_VALUE (decl
);
196 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
197 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
199 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
200 check_member_template (tmpl
);
205 else if (TREE_CODE (decl
) == FIELD_DECL
)
206 cp_error ("data member `%D' cannot be a member template", decl
);
207 else if (DECL_TEMPLATE_INFO (decl
))
209 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
211 check_member_template (DECL_TI_TEMPLATE (decl
));
212 return DECL_TI_TEMPLATE (decl
);
218 cp_error ("invalid member template declaration `%D'", decl
);
220 return error_mark_node
;
223 /* Returns the template nesting level of the indicated class TYPE.
233 A<T>::B<U> has depth two, while A<T> has depth one.
234 Both A<T>::B<int> and A<int>::B<U> have depth one, if
235 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
238 This function is guaranteed to return 0 if passed NULL_TREE so
239 that, for example, `template_class_depth (current_class_type)' is
243 template_class_depth_real (type
, count_specializations
)
245 int count_specializations
;
250 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
251 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
252 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
254 if (TREE_CODE (type
) != FUNCTION_DECL
)
256 if (CLASSTYPE_TEMPLATE_INFO (type
)
257 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
258 && ((count_specializations
259 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
260 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
265 if (DECL_TEMPLATE_INFO (type
)
266 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
267 && ((count_specializations
268 && DECL_TEMPLATE_SPECIALIZATION (type
))
269 || uses_template_parms (DECL_TI_ARGS (type
))))
277 /* Returns the template nesting level of the indicated class TYPE.
278 Like template_class_depth_real, but instantiations do not count in
282 template_class_depth (type
)
285 return template_class_depth_real (type
, /*count_specializations=*/0);
288 /* Returns 1 if processing DECL as part of do_pending_inlines
289 needs us to push template parms. */
292 inline_needs_template_parms (decl
)
295 if (! DECL_TEMPLATE_INFO (decl
))
298 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
299 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
302 /* Subroutine of maybe_begin_member_template_processing.
303 Push the template parms in PARMS, starting from LEVELS steps into the
304 chain, and ending at the beginning, since template parms are listed
308 push_inline_template_parms_recursive (parmlist
, levels
)
312 tree parms
= TREE_VALUE (parmlist
);
316 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
318 ++processing_template_decl
;
319 current_template_parms
320 = tree_cons (build_int_2 (0, processing_template_decl
),
321 parms
, current_template_parms
);
322 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
325 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
327 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
328 my_friendly_assert (DECL_P (parm
), 0);
330 switch (TREE_CODE (parm
))
339 /* Make a CONST_DECL as is done in process_template_parm.
340 It is ugly that we recreate this here; the original
341 version built in process_template_parm is no longer
343 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
345 SET_DECL_ARTIFICIAL (decl
);
346 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
347 SET_DECL_TEMPLATE_PARM_P (decl
);
353 my_friendly_abort (0);
358 /* Restore the template parameter context for a member template or
359 a friend template defined in a class definition. */
362 maybe_begin_member_template_processing (decl
)
368 if (inline_needs_template_parms (decl
))
370 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
371 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
373 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
376 parms
= TREE_CHAIN (parms
);
379 push_inline_template_parms_recursive (parms
, levels
);
382 /* Remember how many levels of template parameters we pushed so that
383 we can pop them later. */
384 if (!inline_parm_levels
)
385 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
386 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
387 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
388 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
389 ++inline_parm_levels_used
;
392 /* Undo the effects of begin_member_template_processing. */
395 maybe_end_member_template_processing ()
399 if (!inline_parm_levels_used
)
402 --inline_parm_levels_used
;
404 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
407 --processing_template_decl
;
408 current_template_parms
= TREE_CHAIN (current_template_parms
);
413 /* Returns non-zero iff T is a member template function. We must be
416 template <class T> class C { void f(); }
418 Here, f is a template function, and a member, but not a member
419 template. This function does not concern itself with the origin of
420 T, only its present state. So if we have
422 template <class T> class C { template <class U> void f(U); }
424 then neither C<int>::f<char> nor C<T>::f<double> is considered
425 to be a member template. But, `template <class U> void
426 C<int>::f(U)' is considered a member template. */
429 is_member_template (t
)
432 if (!DECL_FUNCTION_TEMPLATE_P (t
))
433 /* Anything that isn't a function or a template function is
434 certainly not a member template. */
437 /* A local class can't have member templates. */
438 if (decl_function_context (t
))
441 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
442 /* If there are more levels of template parameters than
443 there are template classes surrounding the declaration,
444 then we have a member template. */
445 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
446 template_class_depth (DECL_CONTEXT (t
))));
450 /* Returns non-zero iff T is a member template class. See
451 is_member_template for a description of what precisely constitutes
452 a member template. */
455 is_member_template_class (t
)
458 if (!DECL_CLASS_TEMPLATE_P (t
))
459 /* Anything that isn't a class template, is certainly not a member
463 if (!DECL_CLASS_SCOPE_P (t
))
464 /* Anything whose context isn't a class type is surely not a
468 /* If there are more levels of template parameters than there are
469 template classes surrounding the declaration, then we have a
471 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
472 template_class_depth (DECL_CONTEXT (t
)));
476 /* Return a new template argument vector which contains all of ARGS,
477 but has as its innermost set of arguments the EXTRA_ARGS. */
480 add_to_template_args (args
, extra_args
)
489 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
490 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
492 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
493 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
495 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
496 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
501 /* Like add_to_template_args, but only the outermost ARGS are added to
502 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
503 (EXTRA_ARGS) levels are added. This function is used to combine
504 the template arguments from a partial instantiation with the
505 template arguments used to attain the full instantiation from the
506 partial instantiation. */
509 add_outermost_template_args (args
, extra_args
)
515 /* If there are more levels of EXTRA_ARGS than there are ARGS,
516 something very fishy is going on. */
517 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
520 /* If *all* the new arguments will be the EXTRA_ARGS, just return
522 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
525 /* For the moment, we make ARGS look like it contains fewer levels. */
526 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
528 new_args
= add_to_template_args (args
, extra_args
);
530 /* Now, we restore ARGS to its full dimensions. */
531 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
536 /* Return the N levels of innermost template arguments from the ARGS. */
539 get_innermost_template_args (args
, n
)
547 my_friendly_assert (n
>= 0, 20000603);
549 /* If N is 1, just return the innermost set of template arguments. */
551 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
553 /* If we're not removing anything, just return the arguments we were
555 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
556 my_friendly_assert (extra_levels
>= 0, 20000603);
557 if (extra_levels
== 0)
560 /* Make a new set of arguments, not containing the outer arguments. */
561 new_args
= make_tree_vec (n
);
562 for (i
= 1; i
<= n
; ++i
)
563 SET_TMPL_ARGS_LEVEL (new_args
, i
,
564 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
569 /* We've got a template header coming up; push to a new level for storing
573 begin_template_parm_list ()
575 /* We use a non-tag-transparent scope here, which causes pushtag to
576 put tags in this scope, rather than in the enclosing class or
577 namespace scope. This is the right thing, since we want
578 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
579 global template class, push_template_decl handles putting the
580 TEMPLATE_DECL into top-level scope. For a nested template class,
583 template <class T> struct S1 {
584 template <class T> struct S2 {};
587 pushtag contains special code to call pushdecl_with_scope on the
588 TEMPLATE_DECL for S2. */
589 begin_scope (sk_template_parms
);
590 ++processing_template_decl
;
591 ++processing_template_parmlist
;
592 note_template_header (0);
595 /* This routine is called when a specialization is declared. If it is
596 illegal to declare a specialization here, an error is reported. */
599 check_specialization_scope ()
601 tree scope
= current_scope ();
605 An explicit specialization shall be declared in the namespace of
606 which the template is a member, or, for member templates, in the
607 namespace of which the enclosing class or enclosing class
608 template is a member. An explicit specialization of a member
609 function, member class or static data member of a class template
610 shall be declared in the namespace of which the class template
612 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
613 cp_error ("explicit specialization in non-namespace scope `%D'",
618 In an explicit specialization declaration for a member of a class
619 template or a member template that appears in namespace scope,
620 the member template and some of its enclosing class templates may
621 remain unspecialized, except that the declaration shall not
622 explicitly specialize a class member template if its enclosing
623 class templates are not explicitly specialized as well. */
624 if (current_template_parms
)
625 cp_error ("enclosing class templates are not explicitly specialized");
628 /* We've just seen template <>. */
631 begin_specialization ()
633 begin_scope (sk_template_spec
);
634 note_template_header (1);
635 check_specialization_scope ();
638 /* Called at then end of processing a declaration preceeded by
642 end_specialization ()
645 reset_specialization ();
648 /* Any template <>'s that we have seen thus far are not referring to a
649 function specialization. */
652 reset_specialization ()
654 processing_specialization
= 0;
655 template_header_count
= 0;
658 /* We've just seen a template header. If SPECIALIZATION is non-zero,
659 it was of the form template <>. */
662 note_template_header (specialization
)
665 processing_specialization
= specialization
;
666 template_header_count
++;
669 /* We're beginning an explicit instantiation. */
672 begin_explicit_instantiation ()
674 ++processing_explicit_instantiation
;
679 end_explicit_instantiation ()
681 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
682 --processing_explicit_instantiation
;
685 /* The TYPE is being declared. If it is a template type, that means it
686 is a partial specialization. Do appropriate error-checking. */
689 maybe_process_partial_specialization (type
)
692 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
694 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
695 && !COMPLETE_TYPE_P (type
))
697 if (current_namespace
698 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
700 cp_pedwarn ("specializing `%#T' in different namespace", type
);
701 cp_pedwarn_at (" from definition of `%#D'",
702 CLASSTYPE_TI_TEMPLATE (type
));
704 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
705 if (processing_template_decl
)
706 push_template_decl (TYPE_MAIN_DECL (type
));
708 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
709 cp_error ("specialization of `%T' after instantiation", type
);
711 else if (processing_specialization
)
712 cp_error ("explicit specialization of non-template `%T'", type
);
715 /* Retrieve the specialization (in the sense of [temp.spec] - a
716 specialization is either an instantiation or an explicit
717 specialization) of TMPL for the given template ARGS. If there is
718 no such specialization, return NULL_TREE. The ARGS are a vector of
719 arguments, or a vector of vectors of arguments, in the case of
720 templates with more than one level of parameters. */
723 retrieve_specialization (tmpl
, args
)
729 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
731 /* There should be as many levels of arguments as there are
732 levels of parameters. */
733 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
734 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
737 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
740 if (comp_template_args (TREE_PURPOSE (s
), args
))
741 return TREE_VALUE (s
);
746 /* Like retrieve_speciailization, but for local declarations. */
749 retrieve_local_specialization (tmpl
)
752 return (tree
) htab_find (local_specializations
, tmpl
);
755 /* Returns non-zero iff DECL is a specialization of TMPL. */
758 is_specialization_of (decl
, tmpl
)
764 if (TREE_CODE (decl
) == FUNCTION_DECL
)
768 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
774 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
776 for (t
= TREE_TYPE (decl
);
778 t
= CLASSTYPE_USE_TEMPLATE (t
)
779 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
780 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
787 /* Register the specialization SPEC as a specialization of TMPL with
788 the indicated ARGS. Returns SPEC, or an equivalent prior
789 declaration, if available. */
792 register_specialization (spec
, tmpl
, args
)
799 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
801 if (TREE_CODE (spec
) == FUNCTION_DECL
802 && uses_template_parms (DECL_TI_ARGS (spec
)))
803 /* This is the FUNCTION_DECL for a partial instantiation. Don't
804 register it; we want the corresponding TEMPLATE_DECL instead.
805 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
806 the more obvious `uses_template_parms (spec)' to avoid problems
807 with default function arguments. In particular, given
810 template <class T> void f(T t1, T t = T())
812 the default argument expression is not substituted for in an
813 instantiation unless and until it is actually needed. */
816 /* There should be as many levels of arguments as there are
817 levels of parameters. */
818 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
819 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
822 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
826 tree fn
= TREE_VALUE (s
);
828 /* We can sometimes try to re-register a specialization that we've
829 already got. In particular, regenerate_decl_from_template
830 calls duplicate_decls which will update the specialization
831 list. But, we'll still get called again here anyhow. It's
832 more convenient to simply allow this than to try to prevent it. */
835 else if (comp_template_args (TREE_PURPOSE (s
), args
))
837 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
839 if (DECL_TEMPLATE_INSTANTIATION (fn
))
842 || DECL_EXPLICIT_INSTANTIATION (fn
))
844 cp_error ("specialization of %D after instantiation",
850 /* This situation should occur only if the first
851 specialization is an implicit instantiation,
852 the second is an explicit specialization, and
853 the implicit instantiation has not yet been
854 used. That situation can occur if we have
855 implicitly instantiated a member function and
856 then specialized it later.
858 We can also wind up here if a friend
859 declaration that looked like an instantiation
860 turns out to be a specialization:
862 template <class T> void foo(T);
863 class S { friend void foo<>(int) };
864 template <> void foo(int);
866 We transform the existing DECL in place so that
867 any pointers to it become pointers to the
870 If there was a definition for the template, but
871 not for the specialization, we want this to
872 look as if there is no definition, and vice
874 DECL_INITIAL (fn
) = NULL_TREE
;
875 duplicate_decls (spec
, fn
);
880 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
882 duplicate_decls (spec
, fn
);
889 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
890 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
895 /* Unregister the specialization SPEC as a specialization of TMPL.
896 Returns nonzero if the SPEC was listed as a specialization of
900 unregister_specialization (spec
, tmpl
)
906 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
908 s
= &TREE_CHAIN (*s
))
909 if (TREE_VALUE (*s
) == spec
)
911 *s
= TREE_CHAIN (*s
);
918 /* Like register_specialization, but for local declarations. We are
919 registering SPEC, an instantiation of TMPL. */
922 register_local_specialization (spec
, tmpl
)
928 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 cp_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. */
1027 tree decl_arg_types
;
1029 /* This is an ordinary member function. However, since
1030 we're here, we can assume it's enclosing class is a
1031 template class. For example,
1033 template <typename T> struct S { void f(); };
1034 template <> void S<int>::f() {}
1036 Here, S<int>::f is a non-template, but S<int> is a
1037 template class. If FN has the same type as DECL, we
1038 might be in business. */
1039 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1040 TREE_TYPE (TREE_TYPE (fn
))))
1041 /* The return types differ. */
1044 /* Adjust the type of DECL in case FN is a static member. */
1045 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1046 if (DECL_STATIC_FUNCTION_P (fn
)
1047 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1048 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1050 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1053 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1058 /* See whether this function might be a specialization of this
1060 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1063 /* We cannot deduce template arguments that when used to
1064 specialize TMPL will produce DECL. */
1067 /* Save this template, and the arguments deduced. */
1068 templates
= tree_cons (targs
, tmpl
, templates
);
1071 if (templates
&& TREE_CHAIN (templates
))
1077 It is possible for a specialization with a given function
1078 signature to be instantiated from more than one function
1079 template. In such cases, explicit specification of the
1080 template arguments must be used to uniquely identify the
1081 function template specialization being specialized.
1083 Note that here, there's no suggestion that we're supposed to
1084 determine which of the candidate templates is most
1085 specialized. However, we, also have:
1089 Partial ordering of overloaded function template
1090 declarations is used in the following contexts to select
1091 the function template to which a function template
1092 specialization refers:
1094 -- when an explicit specialization refers to a function
1097 So, we do use the partial ordering rules, at least for now.
1098 This extension can only serve to make illegal programs legal,
1099 so it's safe. And, there is strong anecdotal evidence that
1100 the committee intended the partial ordering rules to apply;
1101 the EDG front-end has that behavior, and John Spicer claims
1102 that the committee simply forgot to delete the wording in
1103 [temp.expl.spec]. */
1104 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1105 if (tmpl
&& tmpl
!= error_mark_node
)
1107 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1108 templates
= tree_cons (targs
, tmpl
, NULL_TREE
);
1112 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1114 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1116 return error_mark_node
;
1118 else if ((templates
&& TREE_CHAIN (templates
))
1119 || (candidates
&& TREE_CHAIN (candidates
))
1120 || (templates
&& candidates
))
1122 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1124 chainon (candidates
, templates
);
1125 print_candidates (candidates
);
1126 return error_mark_node
;
1129 /* We have one, and exactly one, match. */
1132 /* It was a specialization of an ordinary member function in a
1134 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1135 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1138 /* It was a specialization of a template. */
1139 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1140 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1142 *targs_out
= copy_node (targs
);
1143 SET_TMPL_ARGS_LEVEL (*targs_out
,
1144 TMPL_ARGS_DEPTH (*targs_out
),
1145 TREE_PURPOSE (templates
));
1148 *targs_out
= TREE_PURPOSE (templates
);
1149 return TREE_VALUE (templates
);
1152 /* Check to see if the function just declared, as indicated in
1153 DECLARATOR, and in DECL, is a specialization of a function
1154 template. We may also discover that the declaration is an explicit
1155 instantiation at this point.
1157 Returns DECL, or an equivalent declaration that should be used
1158 instead if all goes well. Issues an error message if something is
1159 amiss. Returns error_mark_node if the error is not easily
1162 FLAGS is a bitmask consisting of the following flags:
1164 2: The function has a definition.
1165 4: The function is a friend.
1167 The TEMPLATE_COUNT is the number of references to qualifying
1168 template classes that appeared in the name of the function. For
1171 template <class T> struct S { void f(); };
1174 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1175 classes are not counted in the TEMPLATE_COUNT, so that in
1177 template <class T> struct S {};
1178 template <> struct S<int> { void f(); }
1179 template <> void S<int>::f();
1181 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1182 illegal; there should be no template <>.)
1184 If the function is a specialization, it is marked as such via
1185 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1186 is set up correctly, and it is added to the list of specializations
1187 for that template. */
1190 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1196 int have_def
= flags
& 2;
1197 int is_friend
= flags
& 4;
1198 int specialization
= 0;
1199 int explicit_instantiation
= 0;
1200 int member_specialization
= 0;
1201 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1202 tree dname
= DECL_NAME (decl
);
1205 tsk
= current_tmpl_spec_kind (template_count
);
1210 if (processing_specialization
)
1213 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1215 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1218 /* This could be something like:
1220 template <class T> void f(T);
1221 class S { friend void f<>(int); } */
1225 /* This case handles bogus declarations like template <>
1226 template <class T> void f<int>(); */
1228 cp_error ("template-id `%D' in declaration of primary template",
1235 case tsk_invalid_member_spec
:
1236 /* The error has already been reported in
1237 check_specialization_scope. */
1238 return error_mark_node
;
1240 case tsk_invalid_expl_inst
:
1241 cp_error ("template parameter list used in explicit instantiation");
1247 cp_error ("definition provided for explicit instantiation");
1249 explicit_instantiation
= 1;
1252 case tsk_excessive_parms
:
1253 cp_error ("too many template parameter lists in declaration of `%D'",
1255 return error_mark_node
;
1259 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1261 member_specialization
= 1;
1266 case tsk_insufficient_parms
:
1267 if (template_header_count
)
1269 cp_error("too few template parameter lists in declaration of `%D'",
1273 else if (ctype
!= NULL_TREE
1274 && !TYPE_BEING_DEFINED (ctype
)
1275 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1278 /* For backwards compatibility, we accept:
1280 template <class T> struct S { void f(); };
1281 void S<int>::f() {} // Missing template <>
1283 That used to be legal C++. */
1286 ("explicit specialization not preceded by `template <>'");
1288 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1293 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1295 /* This case handles bogus declarations like template <>
1296 template <class T> void f<int>(); */
1298 cp_error ("template-id `%D' in declaration of primary template",
1303 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1304 /* This is a specialization of a member template, without
1305 specialization the containing class. Something like:
1307 template <class T> struct S {
1308 template <class U> void f (U);
1310 template <> template <class U> void S<int>::f(U) {}
1312 That's a specialization -- but of the entire template. */
1317 my_friendly_abort (20000309);
1320 if (specialization
|| member_specialization
)
1322 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1323 for (; t
; t
= TREE_CHAIN (t
))
1324 if (TREE_PURPOSE (t
))
1327 ("default argument specified in explicit specialization");
1330 if (current_lang_name
== lang_name_c
)
1331 cp_error ("template specialization with C linkage");
1334 if (specialization
|| member_specialization
|| explicit_instantiation
)
1336 tree tmpl
= NULL_TREE
;
1337 tree targs
= NULL_TREE
;
1339 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1340 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1344 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1347 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1352 lookup_template_function (fns
, NULL_TREE
);
1355 if (declarator
== error_mark_node
)
1356 return error_mark_node
;
1358 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1360 if (!explicit_instantiation
)
1361 /* A specialization in class scope. This is illegal,
1362 but the error will already have been flagged by
1363 check_specialization_scope. */
1364 return error_mark_node
;
1367 /* It's not legal to write an explicit instantiation in
1370 class C { template void f(); }
1372 This case is caught by the parser. However, on
1375 template class C { void f(); };
1377 (which is illegal) we can get here. The error will be
1384 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1386 /* A friend declaration. We can't do much, because we don't
1387 know what this resolves to, yet. */
1388 my_friendly_assert (is_friend
!= 0, 0);
1389 my_friendly_assert (!explicit_instantiation
, 0);
1390 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1393 else if (ctype
!= NULL_TREE
1394 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1397 /* Find the list of functions in ctype that have the same
1398 name as the declared function. */
1399 tree name
= TREE_OPERAND (declarator
, 0);
1400 tree fns
= NULL_TREE
;
1403 if (name
== constructor_name (ctype
)
1404 || name
== constructor_name_full (ctype
))
1406 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1408 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1409 : !TYPE_HAS_DESTRUCTOR (ctype
))
1411 /* From [temp.expl.spec]:
1413 If such an explicit specialization for the member
1414 of a class template names an implicitly-declared
1415 special member function (clause _special_), the
1416 program is ill-formed.
1418 Similar language is found in [temp.explicit]. */
1419 cp_error ("specialization of implicitly-declared special member function");
1420 return error_mark_node
;
1423 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1426 if (!DECL_CONV_FN_P (decl
))
1428 idx
= lookup_fnfields_1 (ctype
, name
);
1430 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1436 /* For a type-conversion operator, we cannot do a
1437 name-based lookup. We might be looking for `operator
1438 int' which will be a specialization of `operator T'.
1439 So, we find *all* the conversion operators, and then
1440 select from them. */
1443 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1445 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1447 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1449 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1450 /* There are no more conversion functions. */
1453 /* Glue all these conversion functions together
1454 with those we already have. */
1455 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1456 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1460 if (fns
== NULL_TREE
)
1462 cp_error ("no member function `%D' declared in `%T'",
1464 return error_mark_node
;
1467 TREE_OPERAND (declarator
, 0) = fns
;
1470 /* Figure out what exactly is being specialized at this point.
1471 Note that for an explicit instantiation, even one for a
1472 member function, we cannot tell apriori whether the
1473 instantiation is for a member template, or just a member
1474 function of a template class. Even if a member template is
1475 being instantiated, the member template arguments may be
1476 elided if they can be deduced from the rest of the
1478 tmpl
= determine_specialization (declarator
, decl
,
1480 member_specialization
);
1482 if (!tmpl
|| tmpl
== error_mark_node
)
1483 /* We couldn't figure out what this declaration was
1485 return error_mark_node
;
1488 tree gen_tmpl
= most_general_template (tmpl
);
1490 if (explicit_instantiation
)
1492 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1493 is done by do_decl_instantiation later. */
1495 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1496 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1498 if (arg_depth
> parm_depth
)
1500 /* If TMPL is not the most general template (for
1501 example, if TMPL is a friend template that is
1502 injected into namespace scope), then there will
1503 be too many levels fo TARGS. Remove some of them
1508 new_targs
= make_tree_vec (parm_depth
);
1509 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1510 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1511 = TREE_VEC_ELT (targs
, i
);
1515 return instantiate_template (tmpl
, targs
);
1518 /* If this is a specialization of a member template of a
1519 template class. In we want to return the TEMPLATE_DECL,
1520 not the specialization of it. */
1521 if (tsk
== tsk_template
)
1523 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1524 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
1528 /* If we though that the DECL was a member function, but it
1529 turns out to be specializing a static member function,
1530 make DECL a static member function as well. */
1531 if (DECL_STATIC_FUNCTION_P (tmpl
)
1532 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1534 revert_static_member_fn (decl
);
1535 last_function_parms
= TREE_CHAIN (last_function_parms
);
1538 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1539 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
1541 /* Mangle the function name appropriately. Note that we do
1542 not mangle specializations of non-template member
1543 functions of template classes, e.g. with
1545 template <class T> struct S { void f(); }
1547 and given the specialization
1549 template <> void S<int>::f() {}
1551 we do not mangle S<int>::f() here. That's because it's
1552 just an ordinary member function and doesn't need special
1553 treatment. We do this here so that the ordinary,
1554 non-template, name-mangling algorithm will not be used
1556 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1557 && name_mangling_version
>= 1)
1558 set_mangled_name_for_template_decl (decl
);
1560 if (is_friend
&& !have_def
)
1561 /* This is not really a declaration of a specialization.
1562 It's just the name of an instantiation. But, it's not
1563 a request for an instantiation, either. */
1564 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1566 /* Register this specialization so that we can find it
1568 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1575 /* TYPE is being declared. Verify that the use of template headers
1576 and such is reasonable. Issue error messages if not. */
1579 maybe_check_template_type (type
)
1582 if (template_header_count
)
1584 /* We are in the scope of some `template <...>' header. */
1587 = template_class_depth_real (TYPE_CONTEXT (type
),
1588 /*count_specializations=*/1);
1590 if (template_header_count
<= context_depth
)
1591 /* This is OK; the template headers are for the context. We
1592 are actually too lenient here; like
1593 check_explicit_specialization we should consider the number
1594 of template types included in the actual declaration. For
1597 template <class T> struct S {
1598 template <class U> template <class V>
1604 template <class T> struct S {
1605 template <class U> struct I;
1608 template <class T> template <class U.
1613 else if (template_header_count
> context_depth
+ 1)
1614 /* There are two many template parameter lists. */
1615 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1619 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1620 parameters. These are represented in the same format used for
1621 DECL_TEMPLATE_PARMS. */
1623 int comp_template_parms (parms1
, parms2
)
1630 if (parms1
== parms2
)
1633 for (p1
= parms1
, p2
= parms2
;
1634 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1635 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1637 tree t1
= TREE_VALUE (p1
);
1638 tree t2
= TREE_VALUE (p2
);
1641 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1642 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1644 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1647 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1649 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1650 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1652 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1655 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1657 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1662 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1663 /* One set of parameters has more parameters lists than the
1670 /* Complain if DECL shadows a template parameter.
1672 [temp.local]: A template-parameter shall not be redeclared within its
1673 scope (including nested scopes). */
1676 check_template_shadow (decl
)
1681 /* If we're not in a template, we can't possibly shadow a template
1683 if (!current_template_parms
)
1686 /* Figure out what we're shadowing. */
1687 if (TREE_CODE (decl
) == OVERLOAD
)
1688 decl
= OVL_CURRENT (decl
);
1689 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1691 /* If there's no previous binding for this name, we're not shadowing
1692 anything, let alone a template parameter. */
1696 /* If we're not shadowing a template parameter, we're done. Note
1697 that OLDDECL might be an OVERLOAD (or perhaps even an
1698 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1700 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
1703 /* We check for decl != olddecl to avoid bogus errors for using a
1704 name inside a class. We check TPFI to avoid duplicate errors for
1705 inline member templates. */
1707 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1710 cp_error_at ("declaration of `%#D'", decl
);
1711 cp_error_at (" shadows template parm `%#D'", olddecl
);
1714 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1715 ORIG_LEVEL, DECL, and TYPE. */
1718 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1725 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1726 TEMPLATE_PARM_IDX (t
) = index
;
1727 TEMPLATE_PARM_LEVEL (t
) = level
;
1728 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1729 TEMPLATE_PARM_DECL (t
) = decl
;
1730 TREE_TYPE (t
) = type
;
1735 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1736 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1737 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1738 new one is created. */
1741 reduce_template_parm_level (index
, type
, levels
)
1746 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1747 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1748 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1751 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1752 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1755 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1756 TEMPLATE_PARM_LEVEL (index
) - levels
,
1757 TEMPLATE_PARM_ORIG_LEVEL (index
),
1759 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1761 /* Template template parameters need this. */
1762 DECL_TEMPLATE_PARMS (decl
)
1763 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1766 return TEMPLATE_PARM_DESCENDANTS (index
);
1769 /* Process information from new template parameter NEXT and append it to the
1770 LIST being built. */
1773 process_template_parm (list
, next
)
1782 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1783 defval
= TREE_PURPOSE (parm
);
1784 parm
= TREE_VALUE (parm
);
1785 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1789 tree p
= TREE_VALUE (tree_last (list
));
1791 if (TREE_CODE (p
) == TYPE_DECL
)
1792 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1793 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1794 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1796 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1804 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1805 /* is a const-param */
1806 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1807 PARM
, 0, NULL_TREE
);
1811 The top-level cv-qualifiers on the template-parameter are
1812 ignored when determining its type. */
1813 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1815 /* A template parameter is not modifiable. */
1816 TREE_READONLY (parm
) = 1;
1817 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1818 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1819 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1821 cp_error ("`%#T' is not a valid type for a template constant parameter",
1823 if (DECL_NAME (parm
) == NULL_TREE
)
1824 error (" a template type parameter must begin with `class' or `typename'");
1825 TREE_TYPE (parm
) = void_type_node
;
1828 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1829 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1830 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1832 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1833 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1834 = build_template_parm_index (idx
, processing_template_decl
,
1835 processing_template_decl
,
1836 decl
, TREE_TYPE (parm
));
1841 parm
= TREE_VALUE (parm
);
1843 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1845 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
1846 /* This is for distinguishing between real templates and template
1847 template parameters */
1848 TREE_TYPE (parm
) = t
;
1849 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1854 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
1855 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1856 decl
= build_decl (TYPE_DECL
, parm
, t
);
1859 TYPE_NAME (t
) = decl
;
1860 TYPE_STUB_DECL (t
) = decl
;
1862 TEMPLATE_TYPE_PARM_INDEX (t
)
1863 = build_template_parm_index (idx
, processing_template_decl
,
1864 processing_template_decl
,
1865 decl
, TREE_TYPE (parm
));
1867 SET_DECL_ARTIFICIAL (decl
);
1868 SET_DECL_TEMPLATE_PARM_P (decl
);
1870 parm
= build_tree_list (defval
, parm
);
1871 return chainon (list
, parm
);
1874 /* The end of a template parameter list has been reached. Process the
1875 tree list into a parameter vector, converting each parameter into a more
1876 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1880 end_template_parm_list (parms
)
1885 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1887 current_template_parms
1888 = tree_cons (build_int_2 (0, processing_template_decl
),
1889 saved_parmlist
, current_template_parms
);
1891 for (parm
= parms
, nparms
= 0;
1893 parm
= TREE_CHAIN (parm
), nparms
++)
1894 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1896 --processing_template_parmlist
;
1898 return saved_parmlist
;
1901 /* end_template_decl is called after a template declaration is seen. */
1904 end_template_decl ()
1906 reset_specialization ();
1908 if (! processing_template_decl
)
1911 /* This matches the pushlevel in begin_template_parm_list. */
1914 --processing_template_decl
;
1915 current_template_parms
= TREE_CHAIN (current_template_parms
);
1918 /* Given a template argument vector containing the template PARMS.
1919 The innermost PARMS are given first. */
1922 current_template_args ()
1925 tree args
= NULL_TREE
;
1926 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1929 /* If there is only one level of template parameters, we do not
1930 create a TREE_VEC of TREE_VECs. Instead, we return a single
1931 TREE_VEC containing the arguments. */
1933 args
= make_tree_vec (length
);
1935 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1937 tree a
= copy_node (TREE_VALUE (header
));
1940 TREE_TYPE (a
) = NULL_TREE
;
1941 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1943 tree t
= TREE_VEC_ELT (a
, i
);
1945 /* T will be a list if we are called from within a
1946 begin/end_template_parm_list pair, but a vector directly
1947 if within a begin/end_member_template_processing pair. */
1948 if (TREE_CODE (t
) == TREE_LIST
)
1952 if (TREE_CODE (t
) == TYPE_DECL
1953 || TREE_CODE (t
) == TEMPLATE_DECL
)
1956 t
= DECL_INITIAL (t
);
1957 TREE_VEC_ELT (a
, i
) = t
;
1962 TREE_VEC_ELT (args
, --l
) = a
;
1970 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1971 template PARMS. Used by push_template_decl below. */
1974 build_template_decl (decl
, parms
)
1978 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1979 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1980 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1981 if (DECL_LANG_SPECIFIC (decl
))
1983 DECL_VIRTUAL_CONTEXT (tmpl
) = DECL_VIRTUAL_CONTEXT (decl
);
1984 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
1985 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
1986 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
1987 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
1988 if (DECL_OVERLOADED_OPERATOR_P (decl
))
1989 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
1990 DECL_OVERLOADED_OPERATOR_P (decl
));
1996 struct template_parm_data
1998 /* The level of the template parameters we are currently
2002 /* The index of the specialization argument we are currently
2006 /* An array whose size is the number of template parameters. The
2007 elements are non-zero if the parameter has been used in any one
2008 of the arguments processed so far. */
2011 /* An array whose size is the number of template arguments. The
2012 elements are non-zero if the argument makes use of template
2013 parameters of this level. */
2014 int* arg_uses_template_parms
;
2017 /* Subroutine of push_template_decl used to see if each template
2018 parameter in a partial specialization is used in the explicit
2019 argument list. If T is of the LEVEL given in DATA (which is
2020 treated as a template_parm_data*), then DATA->PARMS is marked
2024 mark_template_parm (t
, data
)
2030 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2032 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2034 level
= TEMPLATE_PARM_LEVEL (t
);
2035 idx
= TEMPLATE_PARM_IDX (t
);
2039 level
= TEMPLATE_TYPE_LEVEL (t
);
2040 idx
= TEMPLATE_TYPE_IDX (t
);
2043 if (level
== tpd
->level
)
2045 tpd
->parms
[idx
] = 1;
2046 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2049 /* Return zero so that for_each_template_parm will continue the
2050 traversal of the tree; we want to mark *every* template parm. */
2054 /* Process the partial specialization DECL. */
2057 process_partial_specialization (decl
)
2060 tree type
= TREE_TYPE (decl
);
2061 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2062 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2063 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
2064 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2065 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2066 int nargs
= TREE_VEC_LENGTH (inner_args
);
2067 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2069 int did_error_intro
= 0;
2070 struct template_parm_data tpd
;
2071 struct template_parm_data tpd2
;
2073 /* We check that each of the template parameters given in the
2074 partial specialization is used in the argument list to the
2075 specialization. For example:
2077 template <class T> struct S;
2078 template <class T> struct S<T*>;
2080 The second declaration is OK because `T*' uses the template
2081 parameter T, whereas
2083 template <class T> struct S<int>;
2085 is no good. Even trickier is:
2096 The S2<T> declaration is actually illegal; it is a
2097 full-specialization. Of course,
2100 struct S2<T (*)(U)>;
2102 or some such would have been OK. */
2103 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2104 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2105 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
2107 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2108 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
2109 for (i
= 0; i
< nargs
; ++i
)
2111 tpd
.current_arg
= i
;
2112 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2113 &mark_template_parm
,
2116 for (i
= 0; i
< ntparms
; ++i
)
2117 if (tpd
.parms
[i
] == 0)
2119 /* One of the template parms was not used in the
2121 if (!did_error_intro
)
2123 cp_error ("template parameters not used in partial specialization:");
2124 did_error_intro
= 1;
2128 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2131 /* [temp.class.spec]
2133 The argument list of the specialization shall not be identical to
2134 the implicit argument list of the primary template. */
2135 if (comp_template_args
2137 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2139 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2141 /* [temp.class.spec]
2143 A partially specialized non-type argument expression shall not
2144 involve template parameters of the partial specialization except
2145 when the argument expression is a simple identifier.
2147 The type of a template parameter corresponding to a specialized
2148 non-type argument shall not be dependent on a parameter of the
2150 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2152 for (i
= 0; i
< nargs
; ++i
)
2154 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2155 if (/* These first two lines are the `non-type' bit. */
2157 && TREE_CODE (arg
) != TEMPLATE_DECL
2158 /* This next line is the `argument expression is not just a
2159 simple identifier' condition and also the `specialized
2160 non-type argument' bit. */
2161 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2163 if (tpd
.arg_uses_template_parms
[i
])
2164 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2167 /* Look at the corresponding template parameter,
2168 marking which template parameters its type depends
2171 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2176 /* We haven't yet initialized TPD2. Do so now. */
2177 tpd2
.arg_uses_template_parms
2178 = (int*) alloca (sizeof (int) * nargs
);
2179 /* The number of parameters here is the number in the
2180 main template, which, as checked in the assertion
2182 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2184 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2187 /* Mark the template parameters. But this time, we're
2188 looking for the template parameters of the main
2189 template, not in the specialization. */
2190 tpd2
.current_arg
= i
;
2191 tpd2
.arg_uses_template_parms
[i
] = 0;
2192 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
2193 for_each_template_parm (type
,
2194 &mark_template_parm
,
2197 if (tpd2
.arg_uses_template_parms
[i
])
2199 /* The type depended on some template parameters.
2200 If they are fully specialized in the
2201 specialization, that's OK. */
2203 for (j
= 0; j
< nargs
; ++j
)
2204 if (tpd2
.parms
[j
] != 0
2205 && tpd
.arg_uses_template_parms
[j
])
2207 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2217 if (retrieve_specialization (maintmpl
, specargs
))
2218 /* We've already got this specialization. */
2221 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
2222 = tree_cons (inner_args
, inner_parms
,
2223 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2224 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2228 /* Check that a template declaration's use of default arguments is not
2229 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2230 non-zero if DECL is the thing declared by a primary template.
2231 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2234 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2241 int last_level_to_check
;
2246 A default template-argument shall not be specified in a
2247 function template declaration or a function template definition, nor
2248 in the template-parameter-list of the definition of a member of a
2251 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
2252 /* You can't have a function template declaration in a local
2253 scope, nor you can you define a member of a class template in a
2257 if (current_class_type
2258 && !TYPE_BEING_DEFINED (current_class_type
)
2259 && DECL_LANG_SPECIFIC (decl
)
2260 /* If this is either a friend defined in the scope of the class
2261 or a member function. */
2262 && ((DECL_CONTEXT (decl
)
2263 && same_type_p (DECL_CONTEXT (decl
), current_class_type
))
2264 || (DECL_FRIEND_CONTEXT (decl
)
2265 && same_type_p (DECL_FRIEND_CONTEXT (decl
),
2266 current_class_type
)))
2267 /* And, if it was a member function, it really was defined in
2268 the scope of the class. */
2269 && (!DECL_FUNCTION_MEMBER_P (decl
) || DECL_DEFINED_IN_CLASS_P (decl
)))
2270 /* We already checked these parameters when the template was
2271 declared, so there's no need to do it again now. This function
2272 was defined in class scope, but we're processing it's body now
2273 that the class is complete. */
2278 If a template-parameter has a default template-argument, all
2279 subsequent template-parameters shall have a default
2280 template-argument supplied. */
2281 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2283 tree inner_parms
= TREE_VALUE (parm_level
);
2284 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2285 int seen_def_arg_p
= 0;
2288 for (i
= 0; i
< ntparms
; ++i
)
2290 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2291 if (TREE_PURPOSE (parm
))
2293 else if (seen_def_arg_p
)
2295 cp_error ("no default argument for `%D'", TREE_VALUE (parm
));
2296 /* For better subsequent error-recovery, we indicate that
2297 there should have been a default argument. */
2298 TREE_PURPOSE (parm
) = error_mark_node
;
2303 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2304 /* For an ordinary class template, default template arguments are
2305 allowed at the innermost level, e.g.:
2306 template <class T = int>
2308 but, in a partial specialization, they're not allowed even
2309 there, as we have in [temp.class.spec]:
2311 The template parameter list of a specialization shall not
2312 contain default template argument values.
2314 So, for a partial specialization, or for a function template,
2315 we look at all of them. */
2318 /* But, for a primary class template that is not a partial
2319 specialization we look at all template parameters except the
2321 parms
= TREE_CHAIN (parms
);
2323 /* Figure out what error message to issue. */
2324 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2325 msg
= "default argument for template parameter in function template `%D'";
2326 else if (is_partial
)
2327 msg
= "default argument in partial specialization `%D'";
2329 msg
= "default argument for template parameter for class enclosing `%D'";
2331 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2332 /* If we're inside a class definition, there's no need to
2333 examine the parameters to the class itself. On the one
2334 hand, they will be checked when the class is defined, and,
2335 on the other, default arguments are legal in things like:
2336 template <class T = double>
2337 struct S { template <class U> void f(U); };
2338 Here the default argument for `S' has no bearing on the
2339 declaration of `f'. */
2340 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2342 /* Check everything. */
2343 last_level_to_check
= 0;
2345 for (parm_level
= parms
;
2346 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2347 parm_level
= TREE_CHAIN (parm_level
))
2349 tree inner_parms
= TREE_VALUE (parm_level
);
2353 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2354 for (i
= 0; i
< ntparms
; ++i
)
2355 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2359 cp_error (msg
, decl
);
2363 /* Clear out the default argument so that we are not
2365 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2368 /* At this point, if we're still interested in issuing messages,
2369 they must apply to classes surrounding the object declared. */
2371 msg
= "default argument for template parameter for class enclosing `%D'";
2375 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2376 parameters given by current_template_args, or reuses a
2377 previously existing one, if appropriate. Returns the DECL, or an
2378 equivalent one, if it is replaced via a call to duplicate_decls.
2380 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2383 push_template_decl_real (decl
, is_friend
)
2393 int new_template_p
= 0;
2395 /* See if this is a partial specialization. */
2396 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2397 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2398 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2400 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2403 /* For a friend, we want the context of the friend function, not
2404 the type of which it is a friend. */
2405 ctx
= DECL_CONTEXT (decl
);
2406 else if (CP_DECL_CONTEXT (decl
)
2407 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2408 /* In the case of a virtual function, we want the class in which
2410 ctx
= CP_DECL_CONTEXT (decl
);
2412 /* Otherwise, if we're currently definining some class, the DECL
2413 is assumed to be a member of the class. */
2414 ctx
= current_scope ();
2416 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2419 if (!DECL_CONTEXT (decl
))
2420 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2422 /* See if this is a primary template. */
2423 primary
= template_parm_scope_p ();
2427 if (current_lang_name
== lang_name_c
)
2428 cp_error ("template with C linkage");
2429 else if (TREE_CODE (decl
) == TYPE_DECL
2430 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2431 cp_error ("template class without a name");
2432 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2433 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2434 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2435 || TREE_CODE (decl
) == FUNCTION_DECL
)
2438 cp_error ("template declaration of `%#D'", decl
);
2441 /* Check to see that the rules regarding the use of default
2442 arguments are not being violated. */
2443 check_default_tmpl_args (decl
, current_template_parms
,
2444 primary
, is_partial
);
2447 return process_partial_specialization (decl
);
2449 args
= current_template_args ();
2452 || TREE_CODE (ctx
) == FUNCTION_DECL
2453 || TYPE_BEING_DEFINED (ctx
)
2454 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2456 if (DECL_LANG_SPECIFIC (decl
)
2457 && DECL_TEMPLATE_INFO (decl
)
2458 && DECL_TI_TEMPLATE (decl
))
2459 tmpl
= DECL_TI_TEMPLATE (decl
);
2460 /* If DECL is a TYPE_DECL for a class-template, then there won't
2461 be DECL_LANG_SPECIFIC. The information equivalent to
2462 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2463 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2464 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2465 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2467 /* Since a template declaration already existed for this
2468 class-type, we must be redeclaring it here. Make sure
2469 that the redeclaration is legal. */
2470 redeclare_class_template (TREE_TYPE (decl
),
2471 current_template_parms
);
2472 /* We don't need to create a new TEMPLATE_DECL; just use the
2473 one we already had. */
2474 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2478 tmpl
= build_template_decl (decl
, current_template_parms
);
2481 if (DECL_LANG_SPECIFIC (decl
)
2482 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2484 /* A specialization of a member template of a template
2486 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2487 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2488 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2494 tree a
, t
, current
, parms
;
2497 if (TREE_CODE (decl
) == TYPE_DECL
)
2499 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2500 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2501 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2502 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2503 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2506 cp_error ("`%D' does not declare a template type", decl
);
2510 else if (! DECL_TEMPLATE_INFO (decl
))
2512 cp_error ("template definition of non-template `%#D'", decl
);
2516 tmpl
= DECL_TI_TEMPLATE (decl
);
2518 if (is_member_template (tmpl
)
2519 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2520 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2521 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2525 /* The declaration is a specialization of a member
2526 template, declared outside the class. Therefore, the
2527 innermost template arguments will be NULL, so we
2528 replace them with the arguments determined by the
2529 earlier call to check_explicit_specialization. */
2530 args
= DECL_TI_ARGS (decl
);
2533 = build_template_decl (decl
, current_template_parms
);
2534 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2535 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2536 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2537 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2538 DECL_TEMPLATE_INFO (new_tmpl
)
2539 = tree_cons (tmpl
, args
, NULL_TREE
);
2541 register_specialization (new_tmpl
,
2542 most_general_template (tmpl
),
2547 /* Make sure the template headers we got make sense. */
2549 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2550 i
= TMPL_PARMS_DEPTH (parms
);
2551 if (TMPL_ARGS_DEPTH (args
) != i
)
2553 cp_error ("expected %d levels of template parms for `%#D', got %d",
2554 i
, decl
, TMPL_ARGS_DEPTH (args
));
2557 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2559 a
= TMPL_ARGS_LEVEL (args
, i
);
2560 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2562 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2564 if (current
== decl
)
2565 cp_error ("got %d template parameters for `%#D'",
2566 TREE_VEC_LENGTH (a
), decl
);
2568 cp_error ("got %d template parameters for `%#T'",
2569 TREE_VEC_LENGTH (a
), current
);
2570 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2573 /* Perhaps we should also check that the parms are used in the
2574 appropriate qualifying scopes in the declarator? */
2576 if (current
== decl
)
2579 current
= TYPE_CONTEXT (current
);
2583 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2584 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2586 /* Push template declarations for global functions and types. Note
2587 that we do not try to push a global template friend declared in a
2588 template class; such a thing may well depend on the template
2589 parameters of the class. */
2590 if (new_template_p
&& !ctx
2591 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2592 tmpl
= pushdecl_namespace_level (tmpl
);
2595 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2597 info
= tree_cons (tmpl
, args
, NULL_TREE
);
2599 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2601 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2602 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2603 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2604 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2606 else if (DECL_LANG_SPECIFIC (decl
))
2607 DECL_TEMPLATE_INFO (decl
) = info
;
2609 return DECL_TEMPLATE_RESULT (tmpl
);
2613 push_template_decl (decl
)
2616 return push_template_decl_real (decl
, 0);
2619 /* Called when a class template TYPE is redeclared with the indicated
2620 template PARMS, e.g.:
2622 template <class T> struct S;
2623 template <class T> struct S {}; */
2626 redeclare_class_template (type
, parms
)
2634 if (!TYPE_TEMPLATE_INFO (type
))
2636 cp_error ("`%T' is not a template type", type
);
2640 tmpl
= TYPE_TI_TEMPLATE (type
);
2641 if (!PRIMARY_TEMPLATE_P (tmpl
))
2642 /* The type is nested in some template class. Nothing to worry
2643 about here; there are no new template parameters for the nested
2647 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2648 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2650 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2652 cp_error_at ("previous declaration `%D'", tmpl
);
2653 cp_error ("used %d template parameter%s instead of %d",
2654 TREE_VEC_LENGTH (tmpl_parms
),
2655 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2656 TREE_VEC_LENGTH (parms
));
2660 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2662 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2663 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2664 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2665 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2667 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2669 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2670 cp_error ("redeclared here as `%#D'", parm
);
2674 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2676 /* We have in [temp.param]:
2678 A template-parameter may not be given default arguments
2679 by two different declarations in the same scope. */
2680 cp_error ("redefinition of default argument for `%#D'", parm
);
2681 cp_error_at (" original definition appeared here", tmpl_parm
);
2685 if (parm_default
!= NULL_TREE
)
2686 /* Update the previous template parameters (which are the ones
2687 that will really count) with the new default value. */
2688 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2689 else if (tmpl_default
!= NULL_TREE
)
2690 /* Update the new parameters, too; they'll be used as the
2691 parameters for any members. */
2692 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
2696 /* Attempt to convert the non-type template parameter EXPR to the
2697 indicated TYPE. If the conversion is successful, return the
2698 converted value. If the conversion is unsuccesful, return
2699 NULL_TREE if we issued an error message, or error_mark_node if we
2700 did not. We issue error messages for out-and-out bad template
2701 parameters, but not simply because the conversion failed, since we
2702 might be just trying to do argument deduction. By the time this
2703 function is called, neither TYPE nor EXPR may make use of template
2707 convert_nontype_argument (type
, expr
)
2711 tree expr_type
= TREE_TYPE (expr
);
2713 /* A template-argument for a non-type, non-template
2714 template-parameter shall be one of:
2716 --an integral constant-expression of integral or enumeration
2719 --the name of a non-type template-parameter; or
2721 --the name of an object or function with external linkage,
2722 including function templates and function template-ids but
2723 excluding non-static class members, expressed as id-expression;
2726 --the address of an object or function with external linkage,
2727 including function templates and function template-ids but
2728 excluding non-static class members, expressed as & id-expression
2729 where the & is optional if the name refers to a function or
2732 --a pointer to member expressed as described in _expr.unary.op_. */
2734 /* An integral constant-expression can include const variables or
2735 enumerators. Simplify things by folding them to their values,
2736 unless we're about to bind the declaration to a reference
2738 if (INTEGRAL_TYPE_P (expr_type
)
2739 && TREE_CODE (type
) != REFERENCE_TYPE
)
2740 expr
= decl_constant_value (expr
);
2742 if (is_overloaded_fn (expr
))
2743 /* OK for now. We'll check that it has external linkage later.
2744 Check this first since if expr_type is the unknown_type_node
2745 we would otherwise complain below. */
2747 else if (TYPE_PTRMEM_P (expr_type
)
2748 || TYPE_PTRMEMFUNC_P (expr_type
))
2750 if (TREE_CODE (expr
) != PTRMEM_CST
)
2753 else if (TYPE_PTR_P (expr_type
)
2754 || TYPE_PTRMEM_P (expr_type
)
2755 || TREE_CODE (expr_type
) == ARRAY_TYPE
2756 || TREE_CODE (type
) == REFERENCE_TYPE
2757 /* If expr is the address of an overloaded function, we
2758 will get the unknown_type_node at this point. */
2759 || expr_type
== unknown_type_node
)
2765 if (TREE_CODE (type
) == REFERENCE_TYPE
2766 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2770 if (TREE_CODE (e
) != ADDR_EXPR
)
2773 cp_error ("`%E' is not a valid template argument", expr
);
2774 if (TYPE_PTR_P (expr_type
))
2776 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2777 cp_error ("it must be the address of a function with external linkage");
2779 cp_error ("it must be the address of an object with external linkage");
2781 else if (TYPE_PTRMEM_P (expr_type
)
2782 || TYPE_PTRMEMFUNC_P (expr_type
))
2783 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2788 referent
= TREE_OPERAND (e
, 0);
2789 STRIP_NOPS (referent
);
2792 if (TREE_CODE (referent
) == STRING_CST
)
2794 cp_error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2799 if (is_overloaded_fn (referent
))
2800 /* We'll check that it has external linkage later. */
2802 else if (TREE_CODE (referent
) != VAR_DECL
)
2804 else if (!TREE_PUBLIC (referent
))
2806 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2807 return error_mark_node
;
2810 else if (INTEGRAL_TYPE_P (expr_type
)
2811 || TYPE_PTRMEM_P (expr_type
)
2812 || TYPE_PTRMEMFUNC_P (expr_type
)
2813 /* The next two are g++ extensions. */
2814 || TREE_CODE (expr_type
) == REAL_TYPE
2815 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2817 if (! TREE_CONSTANT (expr
))
2820 cp_error ("non-constant `%E' cannot be used as template argument",
2827 cp_error ("object `%E' cannot be used as template argument", expr
);
2831 switch (TREE_CODE (type
))
2836 /* For a non-type template-parameter of integral or enumeration
2837 type, integral promotions (_conv.prom_) and integral
2838 conversions (_conv.integral_) are applied. */
2839 if (!INTEGRAL_TYPE_P (expr_type
))
2840 return error_mark_node
;
2842 /* It's safe to call digest_init in this case; we know we're
2843 just converting one integral constant expression to another. */
2844 expr
= digest_init (type
, expr
, (tree
*) 0);
2846 if (TREE_CODE (expr
) != INTEGER_CST
)
2847 /* Curiously, some TREE_CONSTANT integral expressions do not
2848 simplify to integer constants. For example, `3 % 0',
2849 remains a TRUNC_MOD_EXPR. */
2856 /* These are g++ extensions. */
2857 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2858 return error_mark_node
;
2860 expr
= digest_init (type
, expr
, (tree
*) 0);
2862 if (TREE_CODE (expr
) != REAL_CST
)
2869 tree type_pointed_to
= TREE_TYPE (type
);
2871 if (TYPE_PTRMEM_P (type
))
2875 /* For a non-type template-parameter of type pointer to data
2876 member, qualification conversions (_conv.qual_) are
2878 e
= perform_qualification_conversions (type
, expr
);
2879 if (TREE_CODE (e
) == NOP_EXPR
)
2880 /* The call to perform_qualification_conversions will
2881 insert a NOP_EXPR over EXPR to do express conversion,
2882 if necessary. But, that will confuse us if we use
2883 this (converted) template parameter to instantiate
2884 another template; then the thing will not look like a
2885 valid template argument. So, just make a new
2886 constant, of the appropriate type. */
2887 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
2890 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2892 /* For a non-type template-parameter of type pointer to
2893 function, only the function-to-pointer conversion
2894 (_conv.func_) is applied. If the template-argument
2895 represents a set of overloaded functions (or a pointer to
2896 such), the matching function is selected from the set
2901 if (TREE_CODE (expr
) == ADDR_EXPR
)
2902 fns
= TREE_OPERAND (expr
, 0);
2906 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2908 if (fn
== error_mark_node
)
2909 return error_mark_node
;
2911 if (!TREE_PUBLIC (fn
))
2913 if (really_overloaded_fn (fns
))
2914 return error_mark_node
;
2919 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2921 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2927 /* For a non-type template-parameter of type pointer to
2928 object, qualification conversions (_conv.qual_) and the
2929 array-to-pointer conversion (_conv.array_) are applied.
2930 [Note: In particular, neither the null pointer conversion
2931 (_conv.ptr_) nor the derived-to-base conversion
2932 (_conv.ptr_) are applied. Although 0 is a valid
2933 template-argument for a non-type template-parameter of
2934 integral type, it is not a valid template-argument for a
2935 non-type template-parameter of pointer type.]
2937 The call to decay_conversion performs the
2938 array-to-pointer conversion, if appropriate. */
2939 expr
= decay_conversion (expr
);
2941 if (expr
== error_mark_node
)
2942 return error_mark_node
;
2944 return perform_qualification_conversions (type
, expr
);
2949 case REFERENCE_TYPE
:
2951 tree type_referred_to
= TREE_TYPE (type
);
2953 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2955 /* For a non-type template-parameter of type reference to
2956 function, no conversions apply. If the
2957 template-argument represents a set of overloaded
2958 functions, the matching function is selected from the
2959 set (_over.over_). */
2963 fn
= instantiate_type (type_referred_to
, fns
, 0);
2965 if (fn
== error_mark_node
)
2966 return error_mark_node
;
2968 if (!TREE_PUBLIC (fn
))
2970 if (really_overloaded_fn (fns
))
2971 /* Don't issue an error here; we might get a different
2972 function if the overloading had worked out
2974 return error_mark_node
;
2979 my_friendly_assert (same_type_p (type_referred_to
,
2987 /* For a non-type template-parameter of type reference to
2988 object, no conversions apply. The type referred to by the
2989 reference may be more cv-qualified than the (otherwise
2990 identical) type of the template-argument. The
2991 template-parameter is bound directly to the
2992 template-argument, which must be an lvalue. */
2993 if ((TYPE_MAIN_VARIANT (expr_type
)
2994 != TYPE_MAIN_VARIANT (type_referred_to
))
2995 || !at_least_as_qualified_p (type_referred_to
,
2997 || !real_lvalue_p (expr
))
2998 return error_mark_node
;
3007 if (!TYPE_PTRMEMFUNC_P (type
))
3008 /* This handles templates like
3009 template<class T, T t> void f();
3010 when T is substituted with any class. The second template
3011 parameter becomes invalid and the template candidate is
3013 return error_mark_node
;
3015 /* For a non-type template-parameter of type pointer to member
3016 function, no conversions apply. If the template-argument
3017 represents a set of overloaded member functions, the
3018 matching member function is selected from the set
3021 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3022 expr_type
!= unknown_type_node
)
3023 return error_mark_node
;
3025 if (TREE_CODE (expr
) == PTRMEM_CST
)
3027 /* A ptr-to-member constant. */
3028 if (!same_type_p (type
, expr_type
))
3029 return error_mark_node
;
3034 if (TREE_CODE (expr
) != ADDR_EXPR
)
3035 return error_mark_node
;
3037 expr
= instantiate_type (type
, expr
, 0);
3039 if (expr
== error_mark_node
)
3040 return error_mark_node
;
3042 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3049 /* All non-type parameters must have one of these types. */
3050 my_friendly_abort (0);
3054 return error_mark_node
;
3057 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3058 template template parameters. Both PARM_PARMS and ARG_PARMS are
3059 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3062 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3063 the case, then extra parameters must have default arguments.
3065 Consider the example:
3066 template <class T, class Allocator = allocator> class vector;
3067 template<template <class U> class TT> class C;
3069 C<vector> is a valid instantiation. PARM_PARMS for the above code
3070 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3071 T and Allocator) and OUTER_ARGS contains the argument that is used to
3072 substitute the TT parameter. */
3075 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
3076 in_decl
, outer_args
)
3077 tree parm_parms
, arg_parms
;
3079 tree in_decl
, outer_args
;
3081 int nparms
, nargs
, i
;
3084 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3085 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3087 nparms
= TREE_VEC_LENGTH (parm_parms
);
3088 nargs
= TREE_VEC_LENGTH (arg_parms
);
3090 /* The rule here is opposite of coerce_template_parms. */
3093 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3096 for (i
= 0; i
< nparms
; ++i
)
3098 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3099 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3101 if (arg
== NULL_TREE
|| arg
== error_mark_node
3102 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3105 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3108 switch (TREE_CODE (parm
))
3114 /* We encounter instantiations of templates like
3115 template <template <template <class> class> class TT>
3118 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3119 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3121 if (!coerce_template_template_parms (parmparm
, argparm
,
3129 /* The tsubst call is used to handle cases such as
3130 template <class T, template <T> class TT> class D;
3131 i.e. the parameter list of TT depends on earlier parameters. */
3132 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3139 my_friendly_abort (0);
3145 /* Convert the indicated template ARG as necessary to match the
3146 indicated template PARM. Returns the converted ARG, or
3147 error_mark_node if the conversion was unsuccessful. Error messages
3148 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3149 parameter in the parameter list. ARGS is the full set of template
3150 arguments deduced so far. */
3153 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3163 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3165 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3167 if (TREE_CODE (arg
) == TREE_LIST
3168 && TREE_TYPE (arg
) != NULL_TREE
3169 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3171 /* The template argument was the name of some
3172 member function. That's usually
3173 illegal, but static members are OK. In any
3174 case, grab the underlying fields/functions
3175 and issue an error later if required. */
3176 arg
= TREE_VALUE (arg
);
3177 TREE_TYPE (arg
) = unknown_type_node
;
3180 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3181 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3182 || requires_tmpl_type
);
3184 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3185 we also accept implicitly created TYPE_DECL as a valid argument.
3186 This is necessary to handle the case where we pass a template name
3187 to a template template parameter in a scope where we've derived from
3188 in instantiation of that template, so the template name refers to that
3189 instantiation. We really ought to handle this better. */
3191 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3192 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3193 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3194 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
3195 || (TREE_CODE (arg
) == RECORD_TYPE
3196 && CLASSTYPE_TEMPLATE_INFO (arg
)
3197 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3198 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3199 && requires_tmpl_type
3200 && is_base_of_enclosing_class (arg
, current_class_type
)));
3201 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3202 arg
= TYPE_STUB_DECL (arg
);
3203 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3204 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3206 is_type
= TYPE_P (arg
) || is_tmpl_type
;
3208 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3209 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3211 cp_pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg
);
3213 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3214 TREE_OPERAND (arg
, 1),
3218 if (is_type
!= requires_type
)
3224 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3227 cp_error (" expected a constant of type `%T', got `%T'",
3229 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3231 cp_error (" expected a type, got `%E'", arg
);
3234 return error_mark_node
;
3236 if (is_tmpl_type
^ requires_tmpl_type
)
3238 if (in_decl
&& complain
)
3240 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3243 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3245 cp_error (" expected a class template, got `%T'", arg
);
3247 return error_mark_node
;
3252 if (requires_tmpl_type
)
3254 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3255 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3257 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3258 in_decl
, inner_args
))
3262 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3264 if (val
!= error_mark_node
3265 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3266 val
= TREE_TYPE (val
);
3270 if (in_decl
&& complain
)
3272 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3274 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3277 val
= error_mark_node
;
3282 val
= groktypename (arg
);
3283 if (! processing_template_decl
)
3285 /* [basic.link]: A name with no linkage (notably, the
3286 name of a class or enumeration declared in a local
3287 scope) shall not be used to declare an entity with
3288 linkage. This implies that names with no linkage
3289 cannot be used as template arguments. */
3290 tree t
= no_linkage_check (val
);
3293 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3295 ("template-argument `%T' uses anonymous type", val
);
3298 ("template-argument `%T' uses local type `%T'",
3300 return error_mark_node
;
3307 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3309 if (processing_template_decl
)
3310 arg
= maybe_fold_nontype_arg (arg
);
3312 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3313 /* We used to call digest_init here. However, digest_init
3314 will report errors, which we don't want when complain
3315 is zero. More importantly, digest_init will try too
3316 hard to convert things: for example, `0' should not be
3317 converted to pointer type at this point according to
3318 the standard. Accepting this is not merely an
3319 extension, since deciding whether or not these
3320 conversions can occur is part of determining which
3321 function template to call, or whether a given epxlicit
3322 argument specification is legal. */
3323 val
= convert_nontype_argument (t
, arg
);
3327 if (val
== NULL_TREE
)
3328 val
= error_mark_node
;
3329 else if (val
== error_mark_node
&& complain
)
3330 cp_error ("could not convert template argument `%E' to `%T'",
3337 /* Convert all template arguments to their appropriate types, and
3338 return a vector containing the innermost resulting template
3339 arguments. If any error occurs, return error_mark_node, and, if
3340 COMPLAIN is non-zero, issue an error message. Some error messages
3341 are issued even if COMPLAIN is zero; for instance, if a template
3342 argument is composed from a local class.
3344 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3345 provided in ARGLIST, or else trailing parameters must have default
3346 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3347 deduction for any unspecified trailing arguments. */
3350 coerce_template_parms (parms
, args
, in_decl
,
3352 require_all_arguments
)
3356 int require_all_arguments
;
3358 int nparms
, nargs
, i
, lost
= 0;
3361 tree new_inner_args
;
3363 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3364 nargs
= NUM_TMPL_ARGS (inner_args
);
3365 nparms
= TREE_VEC_LENGTH (parms
);
3369 && require_all_arguments
3370 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3374 cp_error ("wrong number of template arguments (%d, should be %d)",
3378 cp_error_at ("provided for `%D'", in_decl
);
3381 return error_mark_node
;
3384 new_inner_args
= make_tree_vec (nparms
);
3385 new_args
= add_outermost_template_args (args
, new_inner_args
);
3386 for (i
= 0; i
< nparms
; i
++)
3391 /* Get the Ith template parameter. */
3392 parm
= TREE_VEC_ELT (parms
, i
);
3394 /* Calculate the Ith argument. */
3395 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3397 arg
= TREE_VALUE (inner_args
);
3398 inner_args
= TREE_CHAIN (inner_args
);
3401 arg
= TREE_VEC_ELT (inner_args
, i
);
3402 /* If no template argument was supplied, look for a default
3404 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3406 /* There was no default value. */
3407 my_friendly_assert (!require_all_arguments
, 0);
3410 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3411 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3413 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3416 /* Now, convert the Ith argument, as necessary. */
3417 if (arg
== NULL_TREE
)
3418 /* We're out of arguments. */
3420 my_friendly_assert (!require_all_arguments
, 0);
3423 else if (arg
== error_mark_node
)
3425 cp_error ("template argument %d is invalid", i
+ 1);
3426 arg
= error_mark_node
;
3429 arg
= convert_template_argument (TREE_VALUE (parm
),
3430 arg
, new_args
, complain
, i
,
3433 if (arg
== error_mark_node
)
3435 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3439 return error_mark_node
;
3441 return new_inner_args
;
3444 /* Returns 1 if template args OT and NT are equivalent. */
3447 template_args_equal (ot
, nt
)
3452 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3454 if (TREE_CODE (nt
) == TREE_VEC
)
3455 /* For member templates */
3456 return comp_template_args (ot
, nt
);
3457 else if (TYPE_P (ot
))
3458 return same_type_p (ot
, nt
);
3460 return (cp_tree_equal (ot
, nt
) > 0);
3463 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3464 of template arguments. Returns 0 otherwise. */
3467 comp_template_args (oldargs
, newargs
)
3468 tree oldargs
, newargs
;
3472 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3475 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3477 tree nt
= TREE_VEC_ELT (newargs
, i
);
3478 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3480 if (! template_args_equal (ot
, nt
))
3486 /* Given class template name and parameter list, produce a user-friendly name
3487 for the instantiation. */
3490 mangle_class_name_for_template (name
, parms
, arglist
)
3492 tree parms
, arglist
;
3494 static struct obstack scratch_obstack
;
3495 static char *scratch_firstobj
;
3498 if (!scratch_firstobj
)
3499 gcc_obstack_init (&scratch_obstack
);
3501 obstack_free (&scratch_obstack
, scratch_firstobj
);
3502 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3504 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3505 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3509 nparms
= TREE_VEC_LENGTH (parms
);
3510 arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
3511 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3512 for (i
= 0; i
< nparms
; i
++)
3514 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3515 tree arg
= TREE_VEC_ELT (arglist
, i
);
3520 if (TREE_CODE (parm
) == TYPE_DECL
)
3522 cat (type_as_string (arg
, TS_CHASE_TYPEDEFS
));
3525 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3527 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3529 /* Already substituted with real template. Just output
3530 the template name here */
3531 tree context
= DECL_CONTEXT (arg
);
3534 /* The template may be defined in a namespace, or
3535 may be a member template. */
3536 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3537 || CLASS_TYPE_P (context
),
3539 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3542 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3545 /* Output the parameter declaration */
3546 cat (type_as_string (arg
, TS_CHASE_TYPEDEFS
));
3550 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3552 if (TREE_CODE (arg
) == TREE_LIST
)
3554 /* New list cell was built because old chain link was in
3556 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3557 arg
= TREE_VALUE (arg
);
3559 /* No need to check arglist against parmlist here; we did that
3560 in coerce_template_parms, called from lookup_template_class. */
3561 cat (expr_as_string (arg
, 0));
3564 char *bufp
= obstack_next_free (&scratch_obstack
);
3566 while (bufp
[offset
- 1] == ' ')
3568 obstack_blank_fast (&scratch_obstack
, offset
);
3570 /* B<C<char> >, not B<C<char>> */
3571 if (bufp
[offset
- 1] == '>')
3576 return (char *) obstack_base (&scratch_obstack
);
3580 classtype_mangled_name (t
)
3583 if (CLASSTYPE_TEMPLATE_INFO (t
)
3584 /* Specializations have already had their names set up in
3585 lookup_template_class. */
3586 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3588 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3590 /* For non-primary templates, the template parameters are
3591 implicit from their surrounding context. */
3592 if (PRIMARY_TEMPLATE_P (tmpl
))
3594 tree name
= DECL_NAME (tmpl
);
3595 char *mangled_name
= mangle_class_name_for_template
3596 (IDENTIFIER_POINTER (name
),
3597 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3598 CLASSTYPE_TI_ARGS (t
));
3599 tree id
= get_identifier (mangled_name
);
3600 IDENTIFIER_TEMPLATE (id
) = name
;
3605 return TYPE_IDENTIFIER (t
);
3609 add_pending_template (d
)
3612 tree ti
= (TYPE_P (d
)) ? CLASSTYPE_TEMPLATE_INFO (d
) : DECL_TEMPLATE_INFO (d
);
3614 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3617 *template_tail
= tree_cons (build_srcloc_here (), d
, NULL_TREE
);
3618 template_tail
= &TREE_CHAIN (*template_tail
);
3619 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3623 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3624 may be either a _DECL or an overloaded function or an
3625 IDENTIFIER_NODE), and ARGLIST. */
3628 lookup_template_function (fns
, arglist
)
3633 if (fns
== NULL_TREE
)
3635 cp_error ("non-template used as template");
3636 return error_mark_node
;
3639 type
= TREE_TYPE (fns
);
3640 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3641 type
= unknown_type_node
;
3643 if (processing_template_decl
)
3644 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3646 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3649 /* Within the scope of a template class S<T>, the name S gets bound
3650 (in build_self_reference) to a TYPE_DECL for the class, not a
3651 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3652 or one of its enclosing classes, and that type is a template,
3653 return the associated TEMPLATE_DECL. Otherwise, the original
3654 DECL is returned. */
3657 maybe_get_template_decl_from_type_decl (decl
)
3660 return (decl
!= NULL_TREE
3661 && TREE_CODE (decl
) == TYPE_DECL
3662 && DECL_ARTIFICIAL (decl
)
3663 && CLASS_TYPE_P (TREE_TYPE (decl
))
3664 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3665 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3668 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3669 parameters, find the desired type.
3671 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3672 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3673 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3674 otherwise.) Since ARGLIST is build on the temp_decl_obstack, we must
3675 copy it here to keep it from being reclaimed when the decl storage
3678 IN_DECL, if non-NULL, is the template declaration we are trying to
3681 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3682 the class we are looking up.
3684 If the template class is really a local class in a template
3685 function, then the FUNCTION_CONTEXT is the function in which it is
3686 being instantiated. */
3689 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3695 tree
template = NULL_TREE
, parmlist
;
3698 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3700 if (IDENTIFIER_VALUE (d1
)
3701 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3702 template = IDENTIFIER_VALUE (d1
);
3706 push_decl_namespace (context
);
3707 template = lookup_name (d1
, /*prefer_type=*/0);
3708 template = maybe_get_template_decl_from_type_decl (template);
3710 pop_decl_namespace ();
3713 context
= DECL_CONTEXT (template);
3715 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3717 tree type
= TREE_TYPE (d1
);
3719 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3720 an implicit typename for the second A. Deal with it. */
3721 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
3722 type
= TREE_TYPE (type
);
3724 if (CLASSTYPE_TEMPLATE_INFO (type
))
3726 template = CLASSTYPE_TI_TEMPLATE (type
);
3727 d1
= DECL_NAME (template);
3730 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3731 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
3733 template = TYPE_TI_TEMPLATE (d1
);
3734 d1
= DECL_NAME (template);
3736 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3737 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
3740 d1
= DECL_NAME (template);
3741 context
= DECL_CONTEXT (template);
3744 my_friendly_abort (272);
3746 /* With something like `template <class T> class X class X { ... };'
3747 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3748 We don't want to do that, but we have to deal with the situation,
3749 so let's give them some syntax errors to chew on instead of a
3753 cp_error ("`%T' is not a template", d1
);
3754 return error_mark_node
;
3757 if (context
== NULL_TREE
)
3758 context
= global_namespace
;
3760 if (TREE_CODE (template) != TEMPLATE_DECL
)
3762 cp_error ("non-template type `%T' used as a template", d1
);
3764 cp_error_at ("for template declaration `%D'", in_decl
);
3765 return error_mark_node
;
3768 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3770 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3771 template arguments */
3773 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3774 tree template2
= TYPE_STUB_DECL (parm
);
3777 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3779 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3780 if (arglist2
== error_mark_node
)
3781 return error_mark_node
;
3783 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3784 = tree_cons (template2
, arglist2
, NULL_TREE
);
3785 TYPE_SIZE (parm
) = 0;
3790 tree template_type
= TREE_TYPE (template);
3793 tree found
= NULL_TREE
;
3796 int is_partial_instantiation
;
3798 gen_tmpl
= most_general_template (template);
3799 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
3800 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3801 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3803 if (arg_depth
== 1 && parm_depth
> 1)
3805 /* We've been given an incomplete set of template arguments.
3808 template <class T> struct S1 {
3809 template <class U> struct S2 {};
3810 template <class U> struct S2<U*> {};
3813 we will be called with an ARGLIST of `U*', but the
3814 TEMPLATE will be `template <class T> template
3815 <class U> struct S1<T>::S2'. We must fill in the missing
3818 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3820 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3823 /* Now we should enough arguments. */
3824 my_friendly_assert (parm_depth
== arg_depth
, 0);
3826 /* From here on, we're only interested in the most general
3828 template = gen_tmpl
;
3830 /* Calculate the BOUND_ARGS. These will be the args that are
3831 actually tsubst'd into the definition to create the
3835 /* We have multiple levels of arguments to coerce, at once. */
3837 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3839 tree bound_args
= make_tree_vec (parm_depth
);
3841 for (i
= saved_depth
,
3842 t
= DECL_TEMPLATE_PARMS (template);
3843 i
> 0 && t
!= NULL_TREE
;
3844 --i
, t
= TREE_CHAIN (t
))
3846 tree a
= coerce_template_parms (TREE_VALUE (t
),
3847 arglist
, template, 1, 1);
3848 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3850 /* We temporarily reduce the length of the ARGLIST so
3851 that coerce_template_parms will see only the arguments
3852 corresponding to the template parameters it is
3854 TREE_VEC_LENGTH (arglist
)--;
3857 /* Restore the ARGLIST to its full size. */
3858 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3860 arglist
= bound_args
;
3864 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3865 INNERMOST_TEMPLATE_ARGS (arglist
),
3868 if (arglist
== error_mark_node
)
3869 /* We were unable to bind the arguments. */
3870 return error_mark_node
;
3872 /* In the scope of a template class, explicit references to the
3873 template class refer to the type of the template, not any
3874 instantiation of it. For example, in:
3876 template <class T> class C { void f(C<T>); }
3878 the `C<T>' is just the same as `C'. Outside of the
3879 class, however, such a reference is an instantiation. */
3880 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3883 found
= template_type
;
3885 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3889 /* Note that we use DECL_CONTEXT, rather than
3890 CP_DECL_CONTEXT, so that the termination test is
3891 always just `ctx'. We're not interested in namepace
3893 for (ctx
= current_class_type
;
3895 ctx
= (TYPE_P (ctx
)) ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3896 if (same_type_p (ctx
, template_type
))
3900 /* We're not in the scope of the class, so the
3901 TEMPLATE_TYPE is not the type we want after
3909 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3910 found
; found
= TREE_CHAIN (found
))
3911 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3915 found
= TREE_VALUE (found
);
3921 /* This type is a "partial instantiation" if any of the template
3922 arguments still inolve template parameters. Note that we set
3923 IS_PARTIAL_INSTANTIATION for partial specializations as
3925 is_partial_instantiation
= uses_template_parms (arglist
);
3927 if (!is_partial_instantiation
3928 && !PRIMARY_TEMPLATE_P (template)
3929 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
3931 found
= xref_tag_from_type (TREE_TYPE (template),
3932 DECL_NAME (template),
3937 /* Create the type. */
3938 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3940 if (!is_partial_instantiation
)
3941 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3943 /* We don't want to call start_enum for this type, since
3944 the values for the enumeration constants may involve
3945 template parameters. And, no one should be interested
3946 in the enumeration constants for such a type. */
3947 t
= make_node (ENUMERAL_TYPE
);
3951 t
= make_aggr_type (TREE_CODE (template_type
));
3952 CLASSTYPE_DECLARED_CLASS (t
)
3953 = CLASSTYPE_DECLARED_CLASS (template_type
);
3954 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3955 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3956 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3958 /* A local class. Make sure the decl gets registered properly. */
3959 if (context
== current_function_decl
)
3960 pushtag (DECL_NAME (template), t
, 0);
3963 /* If we called start_enum or pushtag above, this information
3964 will already be set up. */
3967 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3969 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
3970 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3971 TYPE_STUB_DECL (t
) = type_decl
;
3972 DECL_SOURCE_FILE (type_decl
)
3973 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
3974 DECL_SOURCE_LINE (type_decl
)
3975 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
3978 type_decl
= TYPE_NAME (t
);
3980 /* Set up the template information. We have to figure out which
3981 template is the immediate parent if this is a full
3983 if (parm_depth
== 1 || is_partial_instantiation
3984 || !PRIMARY_TEMPLATE_P (template))
3985 /* This case is easy; there are no member templates involved. */
3989 /* This is a full instantiation of a member template. There
3990 should be some partial instantiation of which this is an
3993 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3994 found
; found
= TREE_CHAIN (found
))
3997 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
3999 /* We only want partial instantiations, here, not
4000 specializations or full instantiations. */
4001 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4002 || !uses_template_parms (TREE_VALUE (found
)))
4005 /* Temporarily reduce by one the number of levels in the
4006 ARGLIST and in FOUND so as to avoid comparing the
4007 last set of arguments. */
4008 TREE_VEC_LENGTH (arglist
)--;
4009 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4011 /* See if the arguments match. If they do, then TMPL is
4012 the partial instantiation we want. */
4013 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4015 /* Restore the argument vectors to their full size. */
4016 TREE_VEC_LENGTH (arglist
)++;
4017 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4027 my_friendly_abort (0);
4030 SET_TYPE_TEMPLATE_INFO (t
,
4031 tree_cons (found
, arglist
, NULL_TREE
));
4032 DECL_TEMPLATE_INSTANTIATIONS (template)
4033 = tree_cons (arglist
, t
,
4034 DECL_TEMPLATE_INSTANTIATIONS (template));
4036 if (TREE_CODE (t
) == ENUMERAL_TYPE
4037 && !is_partial_instantiation
)
4038 /* Now that the type has been registered on the instantiations
4039 list, we set up the enumerators. Because the enumeration
4040 constants may involve the enumeration type itself, we make
4041 sure to register the type first, and then create the
4042 constants. That way, doing tsubst_expr for the enumeration
4043 constants won't result in recursive calls here; we'll find
4044 the instantiation and exit above. */
4045 tsubst_enum (template_type
, t
, arglist
);
4047 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4049 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4050 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4051 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
4052 if (!is_partial_instantiation
)
4055 DECL_ASSEMBLER_NAME (type_decl
) = mangle_decl (type_decl
);
4057 DECL_ASSEMBLER_NAME (type_decl
)
4058 = get_identifier (build_overload_name (t
, 1, 1));
4060 /* For backwards compatibility; code that uses
4061 -fexternal-templates expects looking up a template to
4062 instantiate it. I think DDD still relies on this.
4063 (jason 8/20/1998) */
4064 if (TREE_CODE (t
) != ENUMERAL_TYPE
4065 && flag_external_templates
4066 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4067 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4068 add_pending_template (t
);
4071 /* If the type makes use of template parameters, the
4072 code that generates debugging information will crash. */
4073 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4085 /* Called from for_each_template_parm via walk_tree. */
4088 for_each_template_parm_r (tp
, walk_subtrees
, d
)
4094 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
4095 tree_fn_t fn
= pfd
->fn
;
4096 void *data
= pfd
->data
;
4099 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
4100 return error_mark_node
;
4102 switch (TREE_CODE (t
))
4105 if (TYPE_PTRMEMFUNC_FLAG (t
))
4111 if (!TYPE_TEMPLATE_INFO (t
))
4113 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
4115 return error_mark_node
;
4119 /* Since we're not going to walk subtrees, we have to do this
4121 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4122 return error_mark_node
;
4125 /* Check the return type. */
4126 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4127 return error_mark_node
;
4129 /* Check the parameter types. Since default arguments are not
4130 instantiated until they are needed, the TYPE_ARG_TYPES may
4131 contain expressions that involve template parameters. But,
4132 no-one should be looking at them yet. And, once they're
4133 instantiated, they don't contain template parameters, so
4134 there's no point in looking at them then, either. */
4138 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4139 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4140 return error_mark_node
;
4142 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4143 want walk_tree walking into them itself. */
4150 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4151 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4152 return error_mark_node
;
4157 if (DECL_CONTEXT (t
)
4158 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4159 return error_mark_node
;
4162 case TEMPLATE_TEMPLATE_PARM
:
4163 /* Record template parameters such as `T' inside `TT<T>'. */
4164 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
4165 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4166 return error_mark_node
;
4169 case TEMPLATE_TYPE_PARM
:
4170 case TEMPLATE_PARM_INDEX
:
4171 if (fn
&& (*fn
)(t
, data
))
4172 return error_mark_node
;
4174 return error_mark_node
;
4178 /* A template template parameter is encountered */
4179 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
4180 && for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4181 return error_mark_node
;
4183 /* Already substituted template template parameter */
4188 if (!fn
|| for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
, data
))
4189 return error_mark_node
;
4193 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
4194 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4195 (TREE_TYPE (t
)), fn
, data
))
4196 return error_mark_node
;
4201 /* If there's no type, then this thing must be some expression
4202 involving template parameters. */
4203 if (!fn
&& !TREE_TYPE (t
))
4204 return error_mark_node
;
4209 case REINTERPRET_CAST_EXPR
:
4210 case CONST_CAST_EXPR
:
4211 case STATIC_CAST_EXPR
:
4212 case DYNAMIC_CAST_EXPR
:
4217 case PSEUDO_DTOR_EXPR
:
4219 return error_mark_node
;
4226 /* We didn't find any template parameters we liked. */
4230 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
4231 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
4232 If FN returns non-zero, the iteration is terminated, and
4233 for_each_template_parm returns 1. Otherwise, the iteration
4234 continues. If FN never returns a non-zero value, the value
4235 returned by for_each_template_parm is 0. If FN is NULL, it is
4236 considered to be the function which always returns 1. */
4239 for_each_template_parm (t
, fn
, data
)
4244 struct pair_fn_data pfd
;
4250 /* Walk the tree. */
4251 return walk_tree (&t
, for_each_template_parm_r
, &pfd
) != NULL_TREE
;
4255 uses_template_parms (t
)
4258 return for_each_template_parm (t
, 0, 0);
4261 static struct tinst_level
*current_tinst_level
;
4262 static struct tinst_level
*free_tinst_level
;
4263 static int tinst_depth
;
4264 extern int max_tinst_depth
;
4265 #ifdef GATHER_STATISTICS
4268 static int tinst_level_tick
;
4269 static int last_template_error_tick
;
4271 /* Print out all the template instantiations that we are currently
4272 working on. If ERR, we are being called from cp_thing, so do
4273 the right thing for an error message. */
4276 print_template_context (err
)
4279 struct tinst_level
*p
= current_tinst_level
;
4281 const char *file
= input_filename
;
4285 if (current_function_decl
!= p
->decl
4286 && current_function_decl
!= NULL_TREE
)
4287 /* We can get here during the processing of some synthesized
4288 method. Then, p->decl will be the function that's causing
4293 if (current_function_decl
== p
->decl
)
4294 /* Avoid redundancy with the the "In function" line. */;
4296 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4297 file
, decl_as_string (p
->decl
, TS_DECL_TYPE
| TS_FUNC_NORETURN
));
4305 for (; p
; p
= p
->next
)
4307 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4308 decl_as_string (p
->decl
, TS_DECL_TYPE
| TS_FUNC_NORETURN
));
4312 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4315 /* Called from cp_thing to print the template context for an error. */
4318 maybe_print_template_context ()
4320 if (last_template_error_tick
== tinst_level_tick
4321 || current_tinst_level
== 0)
4324 last_template_error_tick
= tinst_level_tick
;
4325 print_template_context (1);
4329 push_tinst_level (d
)
4332 struct tinst_level
*new;
4334 if (tinst_depth
>= max_tinst_depth
)
4336 /* If the instantiation in question still has unbound template parms,
4337 we don't really care if we can't instantiate it, so just return.
4338 This happens with base instantiation for implicit `typename'. */
4339 if (uses_template_parms (d
))
4342 last_template_error_tick
= tinst_level_tick
;
4343 cp_error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4344 max_tinst_depth
, d
);
4346 print_template_context (0);
4351 if (free_tinst_level
)
4353 new = free_tinst_level
;
4354 free_tinst_level
= new->next
;
4357 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4361 new->file
= input_filename
;
4362 new->next
= current_tinst_level
;
4363 current_tinst_level
= new;
4366 #ifdef GATHER_STATISTICS
4367 if (tinst_depth
> depth_reached
)
4368 depth_reached
= tinst_depth
;
4378 struct tinst_level
*old
= current_tinst_level
;
4380 /* Restore the filename and line number stashed away when we started
4381 this instantiation. */
4383 input_filename
= old
->file
;
4384 extract_interface_info ();
4386 current_tinst_level
= old
->next
;
4387 old
->next
= free_tinst_level
;
4388 free_tinst_level
= old
;
4393 struct tinst_level
*
4396 struct tinst_level
*p
= current_tinst_level
;
4399 for (; p
->next
; p
= p
->next
)
4404 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4405 vector of template arguments, as for tsubst.
4407 Returns an appropriate tsbust'd friend declaration. */
4410 tsubst_friend_function (decl
, args
)
4416 const char *file
= input_filename
;
4418 lineno
= DECL_SOURCE_LINE (decl
);
4419 input_filename
= DECL_SOURCE_FILE (decl
);
4421 if (TREE_CODE (decl
) == FUNCTION_DECL
4422 && DECL_TEMPLATE_INSTANTIATION (decl
)
4423 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4424 /* This was a friend declared with an explicit template
4425 argument list, e.g.:
4429 to indicate that f was a template instantiation, not a new
4430 function declaration. Now, we have to figure out what
4431 instantiation of what template. */
4438 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4439 args
, /*complain=*/1,
4441 tsubst (DECL_TI_ARGS (decl
),
4442 args
, /*complain=*/1,
4444 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4445 tmpl
= determine_specialization (template_id
, new_friend
,
4447 /*need_member_template=*/0);
4448 new_friend
= instantiate_template (tmpl
, new_args
);
4452 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4454 /* The NEW_FRIEND will look like an instantiation, to the
4455 compiler, but is not an instantiation from the point of view of
4456 the language. For example, we might have had:
4458 template <class T> struct S {
4459 template <class U> friend void f(T, U);
4462 Then, in S<int>, template <class U> void f(int, U) is not an
4463 instantiation of anything. */
4464 DECL_USE_TEMPLATE (new_friend
) = 0;
4465 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4466 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4468 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4469 tsubst will have resulted in a call to
4470 set_mangled_name_for_template_decl. But, the function is not a
4471 template instantiation and should not be mangled like one.
4472 Therefore, we remangle the function name. We don't have to do
4473 this if the NEW_FRIEND is a template since
4474 set_mangled_name_for_template_decl doesn't do anything if the
4475 function declaration still uses template arguments. */
4476 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4478 set_mangled_name_for_decl (new_friend
);
4479 DECL_RTL (new_friend
) = 0;
4480 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4483 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4486 tree new_friend_template_info
;
4487 tree new_friend_result_template_info
;
4489 int new_friend_is_defn
;
4491 /* We must save some information from NEW_FRIEND before calling
4492 duplicate decls since that function will free NEW_FRIEND if
4494 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4495 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4497 /* This declaration is a `primary' template. */
4498 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4501 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend
)) != NULL_TREE
;
4502 new_friend_result_template_info
4503 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
4507 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4508 new_friend_result_template_info
= NULL_TREE
;
4511 /* Inside pushdecl_namespace_level, we will push into the
4512 current namespace. However, the friend function should go
4513 into the namespace of the template. */
4514 ns
= decl_namespace_context (new_friend
);
4515 push_nested_namespace (ns
);
4516 old_decl
= pushdecl_namespace_level (new_friend
);
4517 pop_nested_namespace (ns
);
4519 if (old_decl
!= new_friend
)
4521 /* This new friend declaration matched an existing
4522 declaration. For example, given:
4524 template <class T> void f(T);
4525 template <class U> class C {
4526 template <class T> friend void f(T) {}
4529 the friend declaration actually provides the definition
4530 of `f', once C has been instantiated for some type. So,
4531 old_decl will be the out-of-class template declaration,
4532 while new_friend is the in-class definition.
4534 But, if `f' was called before this point, the
4535 instantiation of `f' will have DECL_TI_ARGS corresponding
4536 to `T' but not to `U', references to which might appear
4537 in the definition of `f'. Previously, the most general
4538 template for an instantiation of `f' was the out-of-class
4539 version; now it is the in-class version. Therefore, we
4540 run through all specialization of `f', adding to their
4541 DECL_TI_ARGS appropriately. In particular, they need a
4542 new set of outer arguments, corresponding to the
4543 arguments for this class instantiation.
4545 The same situation can arise with something like this:
4548 template <class T> class C {
4552 when `C<int>' is instantiated. Now, `f(int)' is defined
4555 if (!new_friend_is_defn
)
4556 /* On the other hand, if the in-class declaration does
4557 *not* provide a definition, then we don't want to alter
4558 existing definitions. We can just leave everything
4563 /* Overwrite whatever template info was there before, if
4564 any, with the new template information pertaining to
4566 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4568 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4569 /* duplicate_decls will take care of this case. */
4574 tree new_friend_args
;
4576 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
4577 = new_friend_result_template_info
;
4579 new_friend_args
= TI_ARGS (new_friend_template_info
);
4580 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4584 tree spec
= TREE_VALUE (t
);
4587 = add_outermost_template_args (new_friend_args
,
4588 DECL_TI_ARGS (spec
));
4591 /* Now, since specializations are always supposed to
4592 hang off of the most general template, we must move
4594 t
= most_general_template (old_decl
);
4597 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4598 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4599 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4600 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4605 /* The information from NEW_FRIEND has been merged into OLD_DECL
4606 by duplicate_decls. */
4607 new_friend
= old_decl
;
4610 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend
)))
4612 /* Check to see that the declaration is really present, and,
4613 possibly obtain an improved declaration. */
4614 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4623 input_filename
= file
;
4627 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4628 template arguments, as for tsubst.
4630 Returns an appropriate tsbust'd friend type. */
4633 tsubst_friend_class (friend_tmpl
, args
)
4640 /* First, we look for a class template. */
4641 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
4643 /* But, if we don't find one, it might be because we're in a
4644 situation like this:
4652 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4653 for `S<int>', not the TEMPLATE_DECL. */
4654 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
4656 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
4657 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4660 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4662 /* The friend template has already been declared. Just
4663 check to see that the declarations match, and install any new
4664 default parameters. We must tsubst the default parameters,
4665 of course. We only need the innermost template parameters
4666 because that is all that redeclare_class_template will look
4669 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4670 args
, /*complain=*/1);
4671 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4672 friend_type
= TREE_TYPE (tmpl
);
4676 /* The friend template has not already been declared. In this
4677 case, the instantiation of the template class will cause the
4678 injection of this template into the global scope. */
4679 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4681 /* The new TMPL is not an instantiation of anything, so we
4682 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4683 the new type because that is supposed to be the corresponding
4684 template decl, i.e., TMPL. */
4685 DECL_USE_TEMPLATE (tmpl
) = 0;
4686 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4687 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4689 /* Inject this template into the global scope. */
4690 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4697 instantiate_class_template (type
)
4700 tree
template, args
, pattern
, t
;
4703 if (type
== error_mark_node
)
4704 return error_mark_node
;
4706 if (TYPE_BEING_DEFINED (type
) || COMPLETE_TYPE_P (type
))
4709 /* Figure out which template is being instantiated. */
4710 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4711 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4713 /* Figure out which arguments are being used to do the
4715 args
= CLASSTYPE_TI_ARGS (type
);
4716 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4718 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4719 /* If this is a partial instantiation, then we can't instantiate
4720 the type; there's no telling whether or not one of the
4721 template parameters might eventually be instantiated to some
4722 value that results in a specialization being used. For
4734 Now, the `S<U>' in `f<int>' is the specialization, not an
4735 instantiation of the original template. */
4738 /* Determine what specialization of the original template to
4740 if (PARTIAL_INSTANTIATION_P (type
))
4741 /* There's no telling which specialization is appropriate at this
4742 point. Since all peeking at the innards of this partial
4743 instantiation are extensions (like the "implicit typename"
4744 extension, which allows users to omit the keyword `typename' on
4745 names that are declared as types in template base classes), we
4746 are free to do what we please.
4748 Trying to figure out which partial instantiation to use can
4749 cause a crash. (Some of the template arguments don't even have
4750 types.) So, we just use the most general version. */
4754 t
= most_specialized_class (template, args
);
4756 if (t
== error_mark_node
)
4758 const char *str
= "candidates are:";
4759 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4760 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4763 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4766 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4770 TYPE_BEING_DEFINED (type
) = 1;
4771 return error_mark_node
;
4776 pattern
= TREE_TYPE (t
);
4778 pattern
= TREE_TYPE (template);
4780 /* If the template we're instantiating is incomplete, then clearly
4781 there's nothing we can do. */
4782 if (!COMPLETE_TYPE_P (pattern
))
4785 /* If this is a partial instantiation, don't tsubst anything. We will
4786 only use this type for implicit typename, so the actual contents don't
4787 matter. All that matters is whether a particular name is a type. */
4788 if (PARTIAL_INSTANTIATION_P (type
))
4790 /* The fields set here must be kept in sync with those cleared
4791 in begin_class_definition. */
4792 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4793 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4794 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4795 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4796 /* Pretend that the type is complete, so that we will look
4797 inside it during name lookup and such. */
4798 TYPE_SIZE (type
) = bitsize_zero_node
;
4802 /* If we've recursively instantiated too many templates, stop. */
4803 if (! push_tinst_level (type
))
4806 /* Now we're really doing the instantiation. Mark the type as in
4807 the process of being defined. */
4808 TYPE_BEING_DEFINED (type
) = 1;
4810 maybe_push_to_top_level (uses_template_parms (type
));
4814 /* This TYPE is actually a instantiation of of a partial
4815 specialization. We replace the innermost set of ARGS with
4816 the arguments appropriate for substitution. For example,
4819 template <class T> struct S {};
4820 template <class T> struct S<T*> {};
4822 and supposing that we are instantiating S<int*>, ARGS will
4823 present be {int*} but we need {int}. */
4825 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4828 /* If there were multiple levels in ARGS, replacing the
4829 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4830 want, so we make a copy first. */
4831 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4833 args
= copy_node (args
);
4834 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4840 if (flag_external_templates
)
4842 if (flag_alt_external_templates
)
4844 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4845 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4846 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4847 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4848 && CLASSTYPE_INTERFACE_KNOWN (type
));
4852 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4853 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4854 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4855 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4856 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4857 && CLASSTYPE_INTERFACE_KNOWN (type
));
4862 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4863 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4866 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4867 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4868 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4869 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4870 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4871 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
4872 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
4873 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4874 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4875 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4876 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4877 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
4878 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
4879 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
4880 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
4881 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
4882 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
4883 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern
);
4884 TYPE_USES_MULTIPLE_INHERITANCE (type
)
4885 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
4886 TYPE_USES_VIRTUAL_BASECLASSES (type
)
4887 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
4888 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
4889 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
4890 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
4891 if (ANON_AGGR_TYPE_P (pattern
))
4892 SET_ANON_AGGR_TYPE_P (type
);
4894 if (TYPE_BINFO_BASETYPES (pattern
))
4896 tree base_list
= NULL_TREE
;
4897 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
4900 /* Substitute into each of the bases to determine the actual
4902 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
4908 pbase
= TREE_VEC_ELT (pbases
, i
);
4910 /* Substitue to figure out the base class. */
4911 base
= tsubst (BINFO_TYPE (pbase
), args
,
4912 /*complain=*/1, NULL_TREE
);
4913 if (base
== error_mark_node
)
4916 /* Calculate the correct access node. */
4917 if (TREE_VIA_VIRTUAL (pbase
))
4919 if (TREE_VIA_PUBLIC (pbase
))
4920 access
= access_public_virtual_node
;
4921 else if (TREE_VIA_PROTECTED (pbase
))
4922 access
= access_protected_virtual_node
;
4924 access
= access_private_virtual_node
;
4928 if (TREE_VIA_PUBLIC (pbase
))
4929 access
= access_public_node
;
4930 else if (TREE_VIA_PROTECTED (pbase
))
4931 access
= access_protected_node
;
4933 access
= access_private_node
;
4936 base_list
= tree_cons (access
, base
, base_list
);
4939 /* The list is now in reverse order; correct that. */
4940 base_list
= nreverse (base_list
);
4942 /* Now call xref_basetypes to set up all the base-class
4944 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
4945 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
4946 ? class_type_node
: record_type_node
)
4948 DECL_NAME (TYPE_NAME (pattern
)),
4953 /* Now that our base classes are set up, enter the scope of the
4954 class, so that name lookups into base classes, etc. will work
4955 corectly. This is precisely analagous to what we do in
4956 begin_class_definition when defining an ordinary non-template
4958 pushclass (type
, 1);
4960 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
4962 tree tag
= TREE_VALUE (t
);
4963 tree name
= TYPE_IDENTIFIER (tag
);
4966 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
4967 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
4969 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
4970 /* Unfortunately, lookup_template_class sets
4971 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4972 instantiation (i.e., for the type of a member template
4973 class nested within a template class.) This behavior is
4974 required for maybe_process_partial_specialization to work
4975 correctly, but is not accurate in this case; the TAG is not
4976 an instantiation of anything. (The corresponding
4977 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4978 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
4980 /* Now, we call pushtag to put this NEWTAG into the scope of
4981 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4982 pushtag calling push_template_decl. We don't have to do
4983 this for enums because it will already have been done in
4986 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
4987 pushtag (name
, newtag
, /*globalize=*/0);
4991 /* Don't replace enum constants here. */
4992 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
4993 if (TREE_CODE (t
) != CONST_DECL
)
4997 /* The the file and line for this declaration, to assist in
4998 error message reporting. Since we called push_tinst_level
4999 above, we don't need to restore these. */
5000 lineno
= DECL_SOURCE_LINE (t
);
5001 input_filename
= DECL_SOURCE_FILE (t
);
5003 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5004 if (TREE_CODE (r
) == VAR_DECL
)
5008 if (DECL_DEFINED_IN_CLASS_P (r
))
5009 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5010 /*complain=*/1, NULL_TREE
);
5014 finish_static_data_member_decl (r
, init
,
5015 /*asmspec_tree=*/NULL_TREE
,
5018 if (DECL_DEFINED_IN_CLASS_P (r
))
5019 check_static_variable_definition (r
, TREE_TYPE (r
));
5022 /* R will have a TREE_CHAIN if and only if it has already been
5023 processed by finish_member_declaration. This can happen
5024 if, for example, it is a TYPE_DECL for a class-scoped
5025 ENUMERAL_TYPE; such a thing will already have been added to
5026 the field list by tsubst_enum above. */
5027 if (!TREE_CHAIN (r
))
5029 set_current_access_from_decl (r
);
5030 finish_member_declaration (r
);
5034 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5035 for this instantiation. */
5036 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
5038 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5039 set_current_access_from_decl (r
);
5040 finish_member_declaration (r
);
5043 /* Construct the DECL_FRIENDLIST for the new class type. */
5044 typedecl
= TYPE_MAIN_DECL (type
);
5045 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
5051 for (friends
= TREE_VALUE (t
);
5052 friends
!= NULL_TREE
;
5053 friends
= TREE_CHAIN (friends
))
5054 if (TREE_PURPOSE (friends
) == error_mark_node
)
5056 tsubst_friend_function (TREE_VALUE (friends
),
5059 my_friendly_abort (20000216);
5062 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
5066 tree friend_type
= TREE_VALUE (t
);
5067 tree new_friend_type
;
5069 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5070 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5071 else if (uses_template_parms (friend_type
))
5072 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
5076 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5078 /* The call to xref_tag_from_type does injection for friend
5080 push_nested_namespace (ns
);
5082 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5083 pop_nested_namespace (ns
);
5086 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5087 /* Trick make_friend_class into realizing that the friend
5088 we're adding is a template, not an ordinary class. It's
5089 important that we use make_friend_class since it will
5090 perform some error-checking and output cross-reference
5092 ++processing_template_decl
;
5094 make_friend_class (type
, new_friend_type
);
5096 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5097 --processing_template_decl
;
5100 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5101 if (TREE_CODE (t
) == FIELD_DECL
)
5103 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5104 require_complete_type (t
);
5107 /* Set the file and line number information to whatever is given for
5108 the class itself. This puts error messages involving generated
5109 implicit functions at a predictable point, and the same point
5110 that would be used for non-template classes. */
5111 lineno
= DECL_SOURCE_LINE (typedecl
);
5112 input_filename
= DECL_SOURCE_FILE (typedecl
);
5114 unreverse_member_declarations (type
);
5115 finish_struct_1 (type
);
5116 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5118 /* Clear this now so repo_template_used is happy. */
5119 TYPE_BEING_DEFINED (type
) = 0;
5120 repo_template_used (type
);
5122 /* Now that the class is complete, instantiate default arguments for
5123 any member functions. We don't do this earlier because the
5124 default arguments may reference members of the class. */
5125 if (!PRIMARY_TEMPLATE_P (template))
5126 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5127 if (TREE_CODE (t
) == FUNCTION_DECL
5128 /* Implicitly generated member functions will not have tmplate
5129 information; they are not instantiations, but instead are
5130 created "fresh" for each instantiation. */
5131 && DECL_TEMPLATE_INFO (t
))
5132 tsubst_default_arguments (t
);
5135 pop_from_top_level ();
5145 if (t1
== NULL_TREE
)
5146 return t2
== NULL_TREE
;
5147 if (t2
== NULL_TREE
)
5149 /* Don't care if one declares its arg const and the other doesn't -- the
5150 main variant of the arg type is all that matters. */
5151 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5152 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5154 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5157 /* If arg is a non-type template parameter that does not depend on template
5158 arguments, fold it like we weren't in the body of a template. */
5161 maybe_fold_nontype_arg (arg
)
5164 /* If we're not in a template, ARG is already as simple as it's going to
5165 get, and trying to reprocess the trees will break. */
5166 if (! processing_template_decl
)
5169 if (!TYPE_P (arg
) && !uses_template_parms (arg
))
5171 /* Sometimes, one of the args was an expression involving a
5172 template constant parameter, like N - 1. Now that we've
5173 tsubst'd, we might have something like 2 - 1. This will
5174 confuse lookup_template_class, so we do constant folding
5175 here. We have to unset processing_template_decl, to
5176 fool build_expr_from_tree() into building an actual
5179 int saved_processing_template_decl
= processing_template_decl
;
5180 processing_template_decl
= 0;
5181 arg
= fold (build_expr_from_tree (arg
));
5182 processing_template_decl
= saved_processing_template_decl
;
5187 /* Substitute ARGS into the vector of template arguments T. */
5190 tsubst_template_arg_vector (t
, args
, complain
)
5195 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5196 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5198 bzero ((char *) elts
, len
* sizeof (tree
));
5200 for (i
= 0; i
< len
; i
++)
5202 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5203 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5204 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5207 elts
[i
] = maybe_fold_nontype_arg
5208 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5211 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5218 t
= make_tree_vec (len
);
5219 for (i
= 0; i
< len
; i
++)
5220 TREE_VEC_ELT (t
, i
) = elts
[i
];
5225 /* Return the result of substituting ARGS into the template parameters
5226 given by PARMS. If there are m levels of ARGS and m + n levels of
5227 PARMS, then the result will contain n levels of PARMS. For
5228 example, if PARMS is `template <class T> template <class U>
5229 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5230 result will be `template <int*, double, class V>'. */
5233 tsubst_template_parms (parms
, args
, complain
)
5241 for (new_parms
= &r
;
5242 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5243 new_parms
= &(TREE_CHAIN (*new_parms
)),
5244 parms
= TREE_CHAIN (parms
))
5247 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5250 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5252 tree default_value
=
5253 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5255 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5257 TREE_VEC_ELT (new_vec
, i
)
5258 = build_tree_list (tsubst (default_value
, args
, complain
,
5260 tsubst (parm_decl
, args
, complain
,
5265 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5266 - TMPL_ARGS_DEPTH (args
))),
5267 new_vec
, NULL_TREE
);
5273 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5274 type T. If T is not an aggregate or enumeration type, it is
5275 handled as if by tsubst. IN_DECL is as for tsubst. If
5276 ENTERING_SCOPE is non-zero, T is the context for a template which
5277 we are presently tsubst'ing. Return the subsituted value. */
5280 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5290 switch (TREE_CODE (t
))
5293 if (TYPE_PTRMEMFUNC_P (t
))
5295 tree r
= build_ptrmemfunc_type
5296 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5297 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5301 /* else fall through */
5304 if (TYPE_TEMPLATE_INFO (t
))
5310 /* First, determine the context for the type we are looking
5312 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5313 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5315 in_decl
, /*entering_scope=*/1);
5317 context
= NULL_TREE
;
5319 /* Then, figure out what arguments are appropriate for the
5320 type we are trying to find. For example, given:
5322 template <class T> struct S;
5323 template <class T, class U> void f(T, U) { S<U> su; }
5325 and supposing that we are instantiating f<int, double>,
5326 then our ARGS will be {int, double}, but, when looking up
5327 S we only want {double}. */
5328 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5331 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5334 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5338 /* This is not a template type, so there's nothing to do. */
5342 return tsubst (t
, args
, complain
, in_decl
);
5346 /* Substitute into the default argument ARG (a default argument for
5347 FN), which has the indicated TYPE. */
5350 tsubst_default_argument (fn
, type
, arg
)
5355 /* This default argument came from a template. Instantiate the
5356 default argument here, not in tsubst. In the case of
5365 we must be careful to do name lookup in the scope of S<T>,
5366 rather than in the current class. */
5367 if (DECL_CLASS_SCOPE_P (fn
))
5368 pushclass (DECL_CONTEXT (fn
), 2);
5370 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), /*complain=*/1, NULL_TREE
);
5372 if (DECL_CLASS_SCOPE_P (fn
))
5375 /* Make sure the default argument is reasonable. */
5376 arg
= check_default_argument (type
, arg
);
5381 /* Substitute into all the default arguments for FN. */
5384 tsubst_default_arguments (fn
)
5390 tmpl_args
= DECL_TI_ARGS (fn
);
5392 /* If this function is not yet instantiated, we certainly don't need
5393 its default arguments. */
5394 if (uses_template_parms (tmpl_args
))
5397 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5399 arg
= TREE_CHAIN (arg
))
5400 if (TREE_PURPOSE (arg
))
5401 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5403 TREE_PURPOSE (arg
));
5406 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5407 (already computed) substitution of ARGS into TREE_TYPE (T), if
5408 appropriate. Return the result of the substitution. IN_DECL is as
5412 tsubst_decl (t
, args
, type
, in_decl
)
5419 const char *saved_filename
;
5422 /* Set the filename and linenumber to improve error-reporting. */
5423 saved_lineno
= lineno
;
5424 saved_filename
= input_filename
;
5425 lineno
= DECL_SOURCE_LINE (t
);
5426 input_filename
= DECL_SOURCE_FILE (t
);
5428 switch (TREE_CODE (t
))
5432 /* We can get here when processing a member template function
5433 of a template class. */
5434 tree decl
= DECL_TEMPLATE_RESULT (t
);
5436 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5438 if (!is_template_template_parm
)
5440 /* We might already have an instance of this template.
5441 The ARGS are for the surrounding class type, so the
5442 full args contain the tsubst'd args for the context,
5443 plus the innermost args from the template decl. */
5444 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5445 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5446 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
5449 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5452 /* tsubst_template_arg_vector doesn't copy the vector if
5453 nothing changed. But, *something* should have
5455 my_friendly_assert (full_args
!= tmpl_args
, 0);
5457 spec
= retrieve_specialization (t
, full_args
);
5458 if (spec
!= NULL_TREE
)
5465 /* Make a new template decl. It will be similar to the
5466 original, but will record the current template arguments.
5467 We also create a new function declaration, which is just
5468 like the old one, but points to this new template, rather
5469 than the old one. */
5471 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5472 TREE_CHAIN (r
) = NULL_TREE
;
5474 if (is_template_template_parm
)
5476 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5477 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5478 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5483 = tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5484 /*complain=*/1, in_decl
,
5485 /*entering_scope=*/1);
5486 DECL_VIRTUAL_CONTEXT (r
)
5487 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5488 /*complain=*/1, in_decl
,
5489 /*entering_scope=*/1);
5490 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5492 if (TREE_CODE (decl
) == TYPE_DECL
)
5494 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5495 /*complain=*/1, in_decl
);
5496 TREE_TYPE (r
) = new_type
;
5497 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5498 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5499 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5503 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5505 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5506 DECL_TI_TEMPLATE (new_decl
) = r
;
5507 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5508 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5511 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5512 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5513 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5515 /* The template parameters for this new template are all the
5516 template parameters for the old template, except the
5517 outermost level of parameters. */
5518 DECL_TEMPLATE_PARMS (r
)
5519 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5522 if (PRIMARY_TEMPLATE_P (t
))
5523 DECL_PRIMARY_TEMPLATE (r
) = r
;
5525 /* We don't partially instantiate partial specializations. */
5526 if (TREE_CODE (decl
) == TYPE_DECL
)
5529 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5531 spec
= TREE_CHAIN (spec
))
5533 /* It helps to consider example here. Consider:
5544 Now, for example, we are instantiating S<int>::f(U u).
5545 We want to make a template:
5550 It will have a specialization, for the case U = int*, of
5554 void S<int>::f<int*>(int*);
5556 This specialization will be an instantiation of
5557 the specialization given in the declaration of S, with
5558 argument list int*. */
5560 tree fn
= TREE_VALUE (spec
);
5564 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5565 /* Instantiations are on the same list, but they're of
5566 no concern to us. */
5569 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5570 /* A full specialization. There's no need to record
5574 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
5575 /*complain=*/1, in_decl
);
5577 = tsubst (DECL_TEMPLATE_RESULT (most_general_template (fn
)),
5578 spec_args
, /*complain=*/1, in_decl
);
5579 DECL_TI_TEMPLATE (new_fn
) = fn
;
5580 register_specialization (new_fn
, r
,
5581 INNERMOST_TEMPLATE_ARGS (spec_args
));
5584 /* Record this partial instantiation. */
5585 register_specialization (r
, t
,
5586 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)));
5594 tree argvec
= NULL_TREE
;
5601 /* Nobody should be tsubst'ing into non-template functions. */
5602 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5604 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5608 /* Calculate the most general template of which R is a
5609 specialization, and the complete set of arguments used to
5611 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5613 = tsubst_template_arg_vector (DECL_TI_ARGS
5614 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5615 args
, /*complain=*/1);
5617 /* Check to see if we already have this specialization. */
5618 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5626 /* We can see more levels of arguments than parameters if
5627 there was a specialization of a member template, like
5630 template <class T> struct S { template <class U> void f(); }
5631 template <> template <class U> void S<int>::f(U);
5633 Here, we'll be subtituting into the specialization,
5634 because that's where we can find the code we actually
5635 want to generate, but we'll have enough arguments for
5636 the most general template.
5638 We also deal with the peculiar case:
5640 template <class T> struct S {
5641 template <class U> friend void f();
5643 template <class U> void f() {}
5645 template void f<double>();
5647 Here, the ARGS for the instantiation of will be {int,
5648 double}. But, we only need as many ARGS as there are
5649 levels of template parameters in CODE_PATTERN. We are
5650 careful not to get fooled into reducing the ARGS in
5653 template <class T> struct S { template <class U> void f(U); }
5654 template <class T> template <> void S<T>::f(int) {}
5656 which we can spot because the pattern will be a
5657 specialization in this case. */
5658 args_depth
= TMPL_ARGS_DEPTH (args
);
5660 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5661 if (args_depth
> parms_depth
5662 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5663 args
= get_innermost_template_args (args
, parms_depth
);
5667 /* This special case arises when we have something like this:
5669 template <class T> struct S {
5670 friend void f<int>(int, double);
5673 Here, the DECL_TI_TEMPLATE for the friend declaration
5674 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5675 being called from tsubst_friend_function, and we want
5676 only to create a new decl (R) with appropriate types so
5677 that we can call determine_specialization. */
5678 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5680 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5681 == IDENTIFIER_NODE
), 0);
5682 gen_tmpl
= NULL_TREE
;
5685 if (DECL_CLASS_SCOPE_P (t
))
5687 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5691 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5693 /*entering_scope=*/1);
5698 ctx
= DECL_CONTEXT (t
);
5700 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5701 if (type
== error_mark_node
)
5702 return error_mark_node
;
5704 /* We do NOT check for matching decls pushed separately at this
5705 point, as they may not represent instantiations of this
5706 template, and in any case are considered separate under the
5707 discrete model. Instead, see add_maybe_template. */
5709 DECL_USE_TEMPLATE (r
) = 0;
5710 TREE_TYPE (r
) = type
;
5712 DECL_CONTEXT (r
) = ctx
;
5713 DECL_VIRTUAL_CONTEXT (r
)
5714 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5716 /*entering_scope=*/1);
5718 if (member
&& DECL_CONV_FN_P (r
))
5720 /* Type-conversion operator. Reconstruct the name, in
5721 case it's the name of one of the template's parameters. */
5723 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
5725 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5728 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5730 DECL_TEMPLATE_RESULT (r
) = NULL_TREE
;
5732 TREE_STATIC (r
) = 0;
5733 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5734 DECL_EXTERNAL (r
) = 1;
5735 DECL_INTERFACE_KNOWN (r
) = 0;
5736 DECL_DEFER_OUTPUT (r
) = 0;
5737 TREE_CHAIN (r
) = NULL_TREE
;
5738 DECL_PENDING_INLINE_INFO (r
) = 0;
5739 DECL_PENDING_INLINE_P (r
) = 0;
5741 if (DECL_CLONED_FUNCTION (r
))
5743 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
5744 args
, /*complain=*/1, t
);
5745 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
5746 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
5749 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5750 name. There's no need to do this in the special friend
5751 case mentioned above where GEN_TMPL is NULL. */
5754 DECL_TEMPLATE_INFO (r
)
5755 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5756 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5757 register_specialization (r
, gen_tmpl
, argvec
);
5759 /* Set the mangled name for R. */
5760 if (DECL_DESTRUCTOR_P (t
))
5763 set_mangled_name_for_decl (r
);
5765 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5769 /* Instantiations of template functions must be mangled
5770 specially, in order to conform to 14.5.5.1
5771 [temp.over.link]. */
5772 tree tmpl
= DECL_TI_TEMPLATE (t
);
5774 /* TMPL will be NULL if this is a specialization of a
5775 member function of a template class. */
5776 if (name_mangling_version
< 1
5777 || tmpl
== NULL_TREE
5778 || (member
&& !is_member_template (tmpl
)
5779 && !DECL_TEMPLATE_INFO (tmpl
)))
5780 set_mangled_name_for_decl (r
);
5782 set_mangled_name_for_template_decl (r
);
5786 make_decl_rtl (r
, NULL_PTR
, 1);
5788 /* Like grokfndecl. If we don't do this, pushdecl will
5789 mess up our TREE_CHAIN because it doesn't find a
5790 previous decl. Sigh. */
5792 && ! uses_template_parms (r
)
5793 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5795 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5797 /* We're not supposed to instantiate default arguments
5798 until they are called, for a template. But, for a
5801 template <class T> void f ()
5802 { extern void g(int i = T()); }
5804 we should do the substitution when the template is
5805 instantiated. We handle the member function case in
5806 instantiate_class_template since the default arguments
5807 might refer to other members of the class. */
5809 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
5810 && !uses_template_parms (argvec
))
5811 tsubst_default_arguments (r
);
5814 /* Copy the list of befriending classes. */
5815 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5817 friends
= &TREE_CHAIN (*friends
))
5819 *friends
= copy_node (*friends
);
5820 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5821 args
, /*complain=*/1,
5825 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
5827 maybe_retrofit_in_chrg (r
);
5828 if (DECL_CONSTRUCTOR_P (r
))
5829 grok_ctor_properties (ctx
, r
);
5831 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5832 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5839 TREE_TYPE (r
) = type
;
5840 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5842 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5843 DECL_INITIAL (r
) = TREE_TYPE (r
);
5845 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5846 /*complain=*/1, in_decl
);
5848 DECL_CONTEXT (r
) = NULL_TREE
;
5849 if (PROMOTE_PROTOTYPES
5850 && (TREE_CODE (type
) == INTEGER_TYPE
5851 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5852 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5853 DECL_ARG_TYPE (r
) = integer_type_node
;
5855 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5856 /*complain=*/1, TREE_CHAIN (t
));
5863 TREE_TYPE (r
) = type
;
5864 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5866 /* We don't have to set DECL_CONTEXT here; it is set by
5867 finish_member_declaration. */
5868 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
5869 /*complain=*/1, in_decl
);
5870 TREE_CHAIN (r
) = NULL_TREE
;
5871 if (TREE_CODE (type
) == VOID_TYPE
)
5872 cp_error_at ("instantiation of `%D' as type void", r
);
5880 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
5881 TREE_CHAIN (r
) = NULL_TREE
;
5886 if (DECL_IMPLICIT_TYPEDEF_P (t
))
5888 /* For an implicit typedef, we just want the implicit
5889 typedef for the tsubst'd type. We've already got the
5890 tsubst'd type, as TYPE, so we just need it's associated
5892 r
= TYPE_NAME (type
);
5895 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
5896 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
5898 /* For a template type parameter, we don't have to do
5899 anything special. */
5900 r
= TYPE_NAME (type
);
5908 tree argvec
= NULL_TREE
;
5909 tree gen_tmpl
= NULL_TREE
;
5911 tree tmpl
= NULL_TREE
;
5915 /* Assume this is a non-local variable. */
5918 if (TYPE_P (CP_DECL_CONTEXT (t
)))
5919 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5921 in_decl
, /*entering_scope=*/1);
5924 /* Subsequent calls to pushdecl will fill this in. */
5926 if (!DECL_NAMESPACE_SCOPE_P (t
))
5930 /* Check to see if we already have this specialization. */
5933 tmpl
= DECL_TI_TEMPLATE (t
);
5934 gen_tmpl
= most_general_template (tmpl
);
5935 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
5936 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5939 spec
= retrieve_local_specialization (t
);
5948 TREE_TYPE (r
) = type
;
5949 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5950 DECL_CONTEXT (r
) = ctx
;
5952 /* Don't try to expand the initializer until someone tries to use
5953 this variable; otherwise we run into circular dependencies. */
5954 DECL_INITIAL (r
) = NULL_TREE
;
5956 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
5958 /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */
5959 if (DECL_PRETTY_FUNCTION_P (r
))
5961 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (t
),
5965 TREE_TYPE (r
) = TREE_TYPE (DECL_INITIAL (r
));
5968 /* Even if the original location is out of scope, the newly
5969 substituted one is not. */
5970 if (TREE_CODE (r
) == VAR_DECL
)
5971 DECL_DEAD_FOR_LOCAL (r
) = 0;
5975 /* A static data member declaration is always marked
5976 external when it is declared in-class, even if an
5977 initializer is present. We mimic the non-template
5979 DECL_EXTERNAL (r
) = 1;
5981 register_specialization (r
, gen_tmpl
, argvec
);
5982 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
5983 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5986 register_local_specialization (r
, t
);
5988 TREE_CHAIN (r
) = NULL_TREE
;
5989 if (TREE_CODE (r
) == VAR_DECL
&& TREE_CODE (type
) == VOID_TYPE
)
5990 cp_error_at ("instantiation of `%D' as type void", r
);
5995 my_friendly_abort (0);
5998 /* Restore the file and line information. */
5999 lineno
= saved_lineno
;
6000 input_filename
= saved_filename
;
6005 /* Substitue into the ARG_TYPES of a function type. */
6008 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
6014 tree remaining_arg_types
;
6017 if (!arg_types
|| arg_types
== void_list_node
)
6020 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6021 args
, complain
, in_decl
);
6022 if (remaining_arg_types
== error_mark_node
)
6023 return error_mark_node
;
6025 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6026 if (type
== error_mark_node
)
6027 return error_mark_node
;
6029 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6030 top-level qualifiers as required. */
6031 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6033 /* Note that we do not substitute into default arguments here. The
6034 standard mandates that they be instantiated only when needed,
6035 which is done in build_over_call. */
6036 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6037 remaining_arg_types
);
6041 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6042 *not* handle the exception-specification for FNTYPE, because the
6043 initial substitution of explicitly provided template parameters
6044 during argument deduction forbids substitution into the
6045 exception-specification:
6049 All references in the function type of the function template to the
6050 corresponding template parameters are replaced by the specified tem-
6051 plate argument values. If a substitution in a template parameter or
6052 in the function type of the function template results in an invalid
6053 type, type deduction fails. [Note: The equivalent substitution in
6054 exception specifications is done only when the function is instanti-
6055 ated, at which point a program is ill-formed if the substitution
6056 results in an invalid type.] */
6059 tsubst_function_type (t
, args
, complain
, in_decl
)
6069 /* The TYPE_CONTEXT is not used for function/method types. */
6070 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6072 /* Substitue the return type. */
6073 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6074 if (return_type
== error_mark_node
)
6075 return error_mark_node
;
6077 /* Substitue the argument types. */
6078 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6080 if (arg_types
== error_mark_node
)
6081 return error_mark_node
;
6083 /* Construct a new type node and return it. */
6084 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6085 fntype
= build_function_type (return_type
, arg_types
);
6088 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6089 if (! IS_AGGR_TYPE (r
))
6093 Type deduction may fail for any of the following
6096 -- Attempting to create "pointer to member of T" when T
6097 is not a class type. */
6099 cp_error ("creating pointer to member function of non-class type `%T'",
6101 return error_mark_node
;
6104 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6107 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
6108 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6113 /* Substitute into the PARMS of a call-declarator. */
6116 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
6126 if (!parms
|| parms
== void_list_node
)
6129 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6130 args
, complain
, in_decl
);
6132 /* Figure out the type of this parameter. */
6133 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6135 /* Figure out the default argument as well. Note that we use
6136 tsubst_expr since the default argument is really an expression. */
6137 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6139 /* Chain this parameter on to the front of those we have already
6140 processed. We don't use hash_tree_cons because that function
6141 doesn't check TREE_PARMLIST. */
6142 new_parms
= tree_cons (defarg
, type
, new_parms
);
6144 /* And note that these are parameters. */
6145 TREE_PARMLIST (new_parms
) = 1;
6150 /* Take the tree structure T and replace template parameters used
6151 therein with the argument vector ARGS. IN_DECL is an associated
6152 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6153 An appropriate error message is issued only if COMPLAIN is
6154 non-zero. Note that we must be relatively non-tolerant of
6155 extensions here, in order to preserve conformance; if we allow
6156 substitutions that should not be allowed, we may allow argument
6157 deductions that should not succeed, and therefore report ambiguous
6158 overload situations where there are none. In theory, we could
6159 allow the substitution, but indicate that it should have failed,
6160 and allow our caller to make sure that the right thing happens, but
6161 we don't try to do this yet.
6163 This function is used for dealing with types, decls and the like;
6164 for expressions, use tsubst_expr or tsubst_copy. */
6167 tsubst (t
, args
, complain
, in_decl
)
6174 if (t
== NULL_TREE
|| t
== error_mark_node
6175 || t
== integer_type_node
6176 || t
== void_type_node
6177 || t
== char_type_node
6178 || TREE_CODE (t
) == NAMESPACE_DECL
)
6181 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6182 type
= IDENTIFIER_TYPE_VALUE (t
);
6184 type
= TREE_TYPE (t
);
6185 if (type
== unknown_type_node
)
6186 my_friendly_abort (42);
6188 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6189 && TREE_CODE (t
) != TYPENAME_TYPE
6190 && TREE_CODE (t
) != TEMPLATE_DECL
6191 && TREE_CODE (t
) != IDENTIFIER_NODE
6192 && TREE_CODE (t
) != FUNCTION_TYPE
6193 && TREE_CODE (t
) != METHOD_TYPE
)
6194 type
= tsubst (type
, args
, complain
, in_decl
);
6195 if (type
== error_mark_node
)
6196 return error_mark_node
;
6199 return tsubst_decl (t
, args
, type
, in_decl
);
6201 switch (TREE_CODE (t
))
6206 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6207 /*entering_scope=*/0);
6210 case IDENTIFIER_NODE
:
6222 if (t
== integer_type_node
)
6225 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6226 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6230 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6232 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6233 if (max
== error_mark_node
)
6234 return error_mark_node
;
6236 /* See if we can reduce this expression to something simpler. */
6237 max
= maybe_fold_nontype_arg (max
);
6238 if (!processing_template_decl
)
6239 max
= decl_constant_value (max
);
6241 if (processing_template_decl
6242 /* When providing explicit arguments to a template
6243 function, but leaving some arguments for subsequent
6244 deduction, MAX may be template-dependent even if we're
6245 not PROCESSING_TEMPLATE_DECL. We still need to check for
6246 template parms, though; MAX won't be an INTEGER_CST for
6247 dynamic arrays, either. */
6248 || (TREE_CODE (max
) != INTEGER_CST
6249 && uses_template_parms (max
)))
6251 tree itype
= make_node (INTEGER_TYPE
);
6252 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6253 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6258 if (integer_zerop (omax
))
6260 /* Still allow an explicit array of size zero. */
6262 pedwarn ("creating array with size zero");
6264 else if (integer_zerop (max
)
6265 || (TREE_CODE (max
) == INTEGER_CST
6266 && INT_CST_LT (max
, integer_zero_node
)))
6270 Type deduction may fail for any of the following
6273 Attempting to create an array with a size that is
6274 zero or negative. */
6276 cp_error ("creating array with size zero (`%E')", max
);
6278 return error_mark_node
;
6281 return compute_array_index_type (NULL_TREE
, max
);
6284 case TEMPLATE_TYPE_PARM
:
6285 case TEMPLATE_TEMPLATE_PARM
:
6286 case TEMPLATE_PARM_INDEX
:
6294 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6295 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6297 idx
= TEMPLATE_TYPE_IDX (t
);
6298 level
= TEMPLATE_TYPE_LEVEL (t
);
6302 idx
= TEMPLATE_PARM_IDX (t
);
6303 level
= TEMPLATE_PARM_LEVEL (t
);
6306 if (TREE_VEC_LENGTH (args
) > 0)
6308 tree arg
= NULL_TREE
;
6310 levels
= TMPL_ARGS_DEPTH (args
);
6311 if (level
<= levels
)
6312 arg
= TMPL_ARG (args
, level
, idx
);
6314 if (arg
== error_mark_node
)
6315 return error_mark_node
;
6316 else if (arg
!= NULL_TREE
)
6318 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6320 my_friendly_assert (TYPE_P (arg
), 0);
6321 return cp_build_qualified_type_real
6322 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
),
6325 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6327 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6329 /* We are processing a type constructed from
6330 a template template parameter */
6331 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6332 args
, complain
, in_decl
);
6333 if (argvec
== error_mark_node
)
6334 return error_mark_node
;
6336 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6337 we are resolving nested-types in the signature of
6338 a member function templates.
6339 Otherwise ARG is a TEMPLATE_DECL and is the real
6340 template to be instantiated. */
6341 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6342 arg
= TYPE_NAME (arg
);
6344 r
= lookup_template_class (DECL_NAME (arg
),
6347 /*entering_scope=*/0);
6348 return cp_build_qualified_type_real (r
,
6353 /* We are processing a template argument list. */
6361 my_friendly_abort (981018);
6364 /* This can happen during the attempted tsubst'ing in
6365 unify. This means that we don't yet have any information
6366 about the template parameter in question. */
6369 /* If we get here, we must have been looking at a parm for a
6370 more deeply nested template. Make a new version of this
6371 template parameter, but with a lower level. */
6372 switch (TREE_CODE (t
))
6374 case TEMPLATE_TYPE_PARM
:
6375 case TEMPLATE_TEMPLATE_PARM
:
6377 TEMPLATE_TYPE_PARM_INDEX (r
)
6378 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6380 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6381 TYPE_MAIN_VARIANT (r
) = r
;
6382 TYPE_POINTER_TO (r
) = NULL_TREE
;
6383 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6385 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6386 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6388 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6390 if (argvec
== error_mark_node
)
6391 return error_mark_node
;
6393 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6394 = tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
6398 case TEMPLATE_PARM_INDEX
:
6399 r
= reduce_template_parm_level (t
, type
, levels
);
6403 my_friendly_abort (0);
6411 tree purpose
, value
, chain
, result
;
6413 if (t
== void_list_node
)
6416 purpose
= TREE_PURPOSE (t
);
6419 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6420 if (purpose
== error_mark_node
)
6421 return error_mark_node
;
6423 value
= TREE_VALUE (t
);
6426 value
= tsubst (value
, args
, complain
, in_decl
);
6427 if (value
== error_mark_node
)
6428 return error_mark_node
;
6430 chain
= TREE_CHAIN (t
);
6431 if (chain
&& chain
!= void_type_node
)
6433 chain
= tsubst (chain
, args
, complain
, in_decl
);
6434 if (chain
== error_mark_node
)
6435 return error_mark_node
;
6437 if (purpose
== TREE_PURPOSE (t
)
6438 && value
== TREE_VALUE (t
)
6439 && chain
== TREE_CHAIN (t
))
6441 result
= hash_tree_cons (purpose
, value
, chain
);
6442 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
6446 if (type
!= NULL_TREE
)
6448 /* A binfo node. We always need to make a copy, of the node
6449 itself and of its BINFO_BASETYPES. */
6453 /* Make sure type isn't a typedef copy. */
6454 type
= BINFO_TYPE (TYPE_BINFO (type
));
6456 TREE_TYPE (t
) = complete_type (type
);
6457 if (IS_AGGR_TYPE (type
))
6459 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6460 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6461 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6462 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6467 /* Otherwise, a vector of template arguments. */
6468 return tsubst_template_arg_vector (t
, args
, complain
);
6471 case REFERENCE_TYPE
:
6473 enum tree_code code
;
6475 if (type
== TREE_TYPE (t
))
6478 code
= TREE_CODE (t
);
6483 Type deduction may fail for any of the following
6486 -- Attempting to create a pointer to reference type.
6487 -- Attempting to create a reference to a reference type or
6488 a reference to void. */
6489 if (TREE_CODE (type
) == REFERENCE_TYPE
6490 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6492 static int last_line
= 0;
6493 static const char* last_file
= 0;
6495 /* We keep track of the last time we issued this error
6496 message to avoid spewing a ton of messages during a
6497 single bad template instantiation. */
6498 if (complain
&& (last_line
!= lineno
||
6499 last_file
!= input_filename
))
6501 if (TREE_CODE (type
) == VOID_TYPE
)
6502 cp_error ("forming reference to void");
6504 cp_error ("forming %s to reference type `%T'",
6505 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6508 last_file
= input_filename
;
6511 return error_mark_node
;
6513 else if (code
== POINTER_TYPE
)
6514 r
= build_pointer_type (type
);
6516 r
= build_reference_type (type
);
6517 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6519 /* Will this ever be needed for TYPE_..._TO values? */
6525 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6526 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6530 Type deduction may fail for any of the following
6533 -- Attempting to create "pointer to member of T" when T
6534 is not a class type. */
6536 cp_error ("creating pointer to member of non-class type `%T'",
6538 return error_mark_node
;
6540 return build_offset_type (r
, type
);
6548 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6549 if (fntype
== error_mark_node
)
6550 return error_mark_node
;
6552 /* Substitue the exception specification. */
6553 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6556 tree list
= NULL_TREE
;
6558 if (! TREE_VALUE (raises
))
6561 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6563 tree spec
= TREE_VALUE (raises
);
6565 spec
= tsubst (spec
, args
, complain
, in_decl
);
6566 if (spec
== error_mark_node
)
6568 list
= add_exception_specifier (list
, spec
, complain
);
6570 fntype
= build_exception_variant (fntype
, list
);
6576 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6577 if (domain
== error_mark_node
)
6578 return error_mark_node
;
6580 /* As an optimization, we avoid regenerating the array type if
6581 it will obviously be the same as T. */
6582 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6585 /* These checks should match the ones in grokdeclarator.
6589 The deduction may fail for any of the following reasons:
6591 -- Attempting to create an array with an element type that
6592 is void, a function type, or a reference type. */
6593 if (TREE_CODE (type
) == VOID_TYPE
6594 || TREE_CODE (type
) == FUNCTION_TYPE
6595 || TREE_CODE (type
) == REFERENCE_TYPE
)
6598 cp_error ("creating array of `%T'", type
);
6599 return error_mark_node
;
6602 r
= build_cplus_array_type (type
, domain
);
6609 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6611 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6614 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6615 return error_mark_node
;
6617 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6623 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6625 if (e
== error_mark_node
)
6626 return error_mark_node
;
6628 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6633 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6634 in_decl
, /*entering_scope=*/1);
6635 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6638 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6639 return error_mark_node
;
6641 if (!IS_AGGR_TYPE (ctx
))
6644 cp_error ("`%T' is not a class, struct, or union type",
6646 return error_mark_node
;
6648 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6650 /* Normally, make_typename_type does not require that the CTX
6651 have complete type in order to allow things like:
6653 template <class T> struct S { typename S<T>::X Y; };
6655 But, such constructs have already been resolved by this
6656 point, so here CTX really should have complete type, unless
6657 it's a partial instantiation. */
6658 ctx
= complete_type (ctx
);
6659 if (!COMPLETE_TYPE_P (ctx
))
6662 incomplete_type_error (NULL_TREE
, ctx
);
6663 return error_mark_node
;
6667 f
= make_typename_type (ctx
, f
, complain
);
6668 if (f
== error_mark_node
)
6670 return cp_build_qualified_type_real (f
,
6672 | CP_TYPE_QUALS (t
),
6678 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6680 if (e
== error_mark_node
)
6681 return error_mark_node
;
6682 return make_pointer_declarator (type
, e
);
6687 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6689 if (e
== error_mark_node
)
6690 return error_mark_node
;
6691 return make_reference_declarator (type
, e
);
6696 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6698 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
,
6700 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6701 return error_mark_node
;
6703 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6708 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6710 tree e2
= (tsubst_call_declarator_parms
6711 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
6712 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
6715 if (e1
== error_mark_node
|| e2
== error_mark_node
6716 || e3
== error_mark_node
)
6717 return error_mark_node
;
6719 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
6724 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6726 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6727 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6728 return error_mark_node
;
6730 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6735 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6737 if (e1
== error_mark_node
)
6738 return error_mark_node
;
6740 return TREE_TYPE (e1
);
6750 /* This code should match declare_hidden_char_array in
6752 name
= (*decl_printable_name
) (current_function_decl
, 2);
6753 len
= strlen (name
) + 1;
6754 type
= build_array_type (char_type_node
,
6755 build_index_type (build_int_2 (len
, 0)));
6756 str
= build_string (len
, name
);
6757 TREE_TYPE (str
) = type
;
6762 sorry ("use of `%s' in template",
6763 tree_code_name
[(int) TREE_CODE (t
)]);
6764 return error_mark_node
;
6768 /* Like tsubst, but deals with expressions. This function just replaces
6769 template parms; to finish processing the resultant expression, use
6773 tsubst_copy (t
, args
, complain
, in_decl
)
6778 enum tree_code code
;
6781 if (t
== NULL_TREE
|| t
== error_mark_node
)
6784 code
= TREE_CODE (t
);
6789 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6796 if (!DECL_CONTEXT (t
))
6797 /* This is a global enumeration constant. */
6800 /* Unfortunately, we cannot just call lookup_name here.
6803 template <int I> int f() {
6805 struct S { void g() { E e = a; } };
6808 When we instantiate f<7>::S::g(), say, lookup_name is not
6809 clever enough to find f<7>::a. */
6811 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6812 /*entering_scope=*/0);
6814 for (v
= TYPE_VALUES (enum_type
);
6817 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6818 return TREE_VALUE (v
);
6820 /* We didn't find the name. That should never happen; if
6821 name-lookup found it during preliminary parsing, we
6822 should find it again here during instantiation. */
6823 my_friendly_abort (0);
6828 if (DECL_CONTEXT (t
))
6832 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6833 /*entering_scope=*/1);
6834 if (ctx
!= DECL_CONTEXT (t
))
6835 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6841 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6842 t
= tsubst (t
, args
, complain
, in_decl
);
6847 if (is_member_template (t
))
6848 return tsubst (t
, args
, complain
, in_decl
);
6854 /* We must tsbust into a LOOKUP_EXPR in case the names to
6855 which it refers is a conversion operator; in that case the
6856 name will change. We avoid making unnecessary copies,
6859 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6861 if (id
!= TREE_OPERAND (t
, 0))
6863 r
= build_nt (LOOKUP_EXPR
, id
);
6864 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6872 case REINTERPRET_CAST_EXPR
:
6873 case CONST_CAST_EXPR
:
6874 case STATIC_CAST_EXPR
:
6875 case DYNAMIC_CAST_EXPR
:
6878 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6879 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6882 case PREDECREMENT_EXPR
:
6883 case PREINCREMENT_EXPR
:
6884 case POSTDECREMENT_EXPR
:
6885 case POSTINCREMENT_EXPR
:
6887 case TRUTH_NOT_EXPR
:
6890 case CONVERT_EXPR
: /* Unary + */
6899 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6900 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6905 case TRUNC_DIV_EXPR
:
6907 case FLOOR_DIV_EXPR
:
6908 case ROUND_DIV_EXPR
:
6909 case EXACT_DIV_EXPR
:
6911 case BIT_ANDTC_EXPR
:
6914 case TRUNC_MOD_EXPR
:
6915 case FLOOR_MOD_EXPR
:
6916 case TRUTH_ANDIF_EXPR
:
6917 case TRUTH_ORIF_EXPR
:
6918 case TRUTH_AND_EXPR
:
6939 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6940 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
6944 tree fn
= TREE_OPERAND (t
, 0);
6945 if (is_overloaded_fn (fn
))
6946 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
6948 /* Sometimes FN is a LOOKUP_EXPR. */
6949 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
6951 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
6956 case METHOD_CALL_EXPR
:
6958 tree name
= TREE_OPERAND (t
, 0);
6959 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
6961 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6963 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6965 else if (TREE_CODE (name
) == SCOPE_REF
6966 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
6968 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6970 name
= TREE_OPERAND (name
, 1);
6971 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6973 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6974 name
= build_nt (SCOPE_REF
, base
, name
);
6977 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6979 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
6981 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
6986 /* This processing should really occur in tsubst_expr, However,
6987 tsubst_expr does not recurse into expressions, since it
6988 assumes that there aren't any statements inside them.
6989 Instead, it simply calls build_expr_from_tree. So, we need
6990 to expand the STMT_EXPR here. */
6991 if (!processing_template_decl
)
6993 tree stmt_expr
= begin_stmt_expr ();
6994 tsubst_expr (STMT_EXPR_STMT (t
), args
,
6996 return finish_stmt_expr (stmt_expr
);
7003 case PSEUDO_DTOR_EXPR
:
7006 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7007 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7008 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7015 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7016 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7017 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7018 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7025 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7026 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7027 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7028 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7032 case TEMPLATE_ID_EXPR
:
7034 /* Substituted template arguments */
7035 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7038 if (targs
&& TREE_CODE (targs
) == TREE_LIST
)
7041 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
7042 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
7047 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
7048 TREE_VEC_ELT (targs
, i
)
7049 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
7052 return lookup_template_function
7053 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7058 tree purpose
, value
, chain
;
7060 if (t
== void_list_node
)
7063 purpose
= TREE_PURPOSE (t
);
7065 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7066 value
= TREE_VALUE (t
);
7068 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7069 chain
= TREE_CHAIN (t
);
7070 if (chain
&& chain
!= void_type_node
)
7071 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7072 if (purpose
== TREE_PURPOSE (t
)
7073 && value
== TREE_VALUE (t
)
7074 && chain
== TREE_CHAIN (t
))
7076 return tree_cons (purpose
, value
, chain
);
7083 case TEMPLATE_TYPE_PARM
:
7084 case TEMPLATE_TEMPLATE_PARM
:
7085 case TEMPLATE_PARM_INDEX
:
7087 case REFERENCE_TYPE
:
7094 return tsubst (t
, args
, complain
, in_decl
);
7096 case IDENTIFIER_NODE
:
7097 if (IDENTIFIER_TYPENAME_P (t
))
7099 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7101 return mangle_conv_op_name_for_type (new_type
);
7103 return (build_typename_overload (new_type
));
7111 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7112 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
7113 complain
, in_decl
));
7114 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7119 return build_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
7121 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7124 return tsubst (t
, args
, complain
, in_decl
);
7131 /* Like tsubst_copy, but also does semantic processing. */
7134 tsubst_expr (t
, args
, complain
, in_decl
)
7141 if (t
== NULL_TREE
|| t
== error_mark_node
)
7144 if (processing_template_decl
)
7145 return tsubst_copy (t
, args
, complain
, in_decl
);
7147 switch (TREE_CODE (t
))
7151 finish_named_return_value
7152 (TREE_OPERAND (t
, 0),
7153 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, in_decl
));
7154 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7157 case CTOR_INITIALIZER
:
7159 current_member_init_list
7160 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
7161 current_base_init_list
7162 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
7164 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7169 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
7170 args
, complain
, in_decl
));
7175 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7176 args
, complain
, in_decl
));
7185 decl
= DECL_STMT_DECL (t
);
7186 if (TREE_CODE (decl
) == LABEL_DECL
)
7187 finish_label_decl (DECL_NAME (decl
));
7190 init
= DECL_INITIAL (decl
);
7191 decl
= tsubst (decl
, args
, complain
, in_decl
);
7192 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7194 DECL_INITIAL (decl
) = error_mark_node
;
7195 /* By marking the declaration as instantiated, we avoid
7196 trying to instantiate it. Since instantiate_decl can't
7197 handle local variables, and since we've already done
7198 all that needs to be done, that's the right thing to
7200 if (TREE_CODE (decl
) == VAR_DECL
)
7201 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7202 maybe_push_decl (decl
);
7203 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
7213 stmt
= begin_for_stmt ();
7214 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
7215 tsubst_expr (tmp
, args
, complain
, in_decl
);
7216 finish_for_init_stmt (stmt
);
7217 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
7220 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7221 finish_for_expr (tmp
, stmt
);
7222 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7223 finish_for_stmt (tmp
, stmt
);
7230 stmt
= begin_while_stmt ();
7231 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7232 args
, complain
, in_decl
),
7234 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7235 finish_while_stmt (stmt
);
7242 stmt
= begin_do_stmt ();
7243 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7244 finish_do_body (stmt
);
7245 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
7256 stmt
= begin_if_stmt ();
7257 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7258 args
, complain
, in_decl
),
7261 if (tmp
= THEN_CLAUSE (t
), tmp
)
7263 tsubst_expr (tmp
, args
, complain
, in_decl
);
7264 finish_then_clause (stmt
);
7267 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7269 begin_else_clause ();
7270 tsubst_expr (tmp
, args
, complain
, in_decl
);
7271 finish_else_clause (stmt
);
7283 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7284 for (substmt
= COMPOUND_BODY (t
);
7285 substmt
!= NULL_TREE
;
7286 substmt
= TREE_CHAIN (substmt
))
7287 tsubst_expr (substmt
, args
, complain
, in_decl
);
7288 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7294 finish_break_stmt ();
7299 finish_continue_stmt ();
7307 stmt
= begin_switch_stmt ();
7308 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7309 finish_switch_cond (val
, stmt
);
7310 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7311 finish_switch_stmt (val
, stmt
);
7317 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7318 tsubst_expr (CASE_HIGH (t
), args
, complain
, in_decl
));
7322 lineno
= STMT_LINENO (t
);
7323 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7328 t
= GOTO_DESTINATION (t
);
7329 if (TREE_CODE (t
) != LABEL_DECL
)
7330 /* Computed goto's must be tsubst'd into. On the other hand,
7331 non-computed gotos must not be; the identifier in question
7332 will have no binding. */
7333 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7336 finish_goto_stmt (t
);
7341 finish_asm_stmt (ASM_CV_QUAL (t
),
7342 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7343 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7344 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7345 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7353 stmt
= begin_try_block ();
7354 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7355 finish_cleanup_try_block (stmt
);
7356 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7364 if (FN_TRY_BLOCK_P (t
))
7365 stmt
= begin_function_try_block ();
7367 stmt
= begin_try_block ();
7369 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7371 if (FN_TRY_BLOCK_P (t
))
7372 finish_function_try_block (stmt
);
7374 finish_try_block (stmt
);
7376 handler
= TRY_HANDLERS (t
);
7377 for (; handler
; handler
= TREE_CHAIN (handler
))
7378 tsubst_expr (handler
, args
, complain
, in_decl
);
7379 if (FN_TRY_BLOCK_P (t
))
7380 finish_function_handler_sequence (stmt
);
7382 finish_handler_sequence (stmt
);
7392 stmt
= begin_handler ();
7393 if (HANDLER_PARMS (t
))
7395 decl
= DECL_STMT_DECL (HANDLER_PARMS (t
));
7396 decl
= tsubst (decl
, args
, complain
, in_decl
);
7397 /* Prevent instantiate_decl from trying to instantiate
7398 this variable. We've already done all that needs to be
7400 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7404 blocks
= finish_handler_parms (decl
, stmt
);
7405 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7406 finish_handler (blocks
, stmt
);
7413 tsubst (t
, args
, complain
, NULL_TREE
);
7417 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7422 /* TMPL is a TEMPLATE_DECL for a cloned constructor or destructor.
7423 Instantiate it with the ARGS. */
7426 instantiate_clone (tmpl
, args
)
7433 /* Instantiated the cloned function, rather than the clone. */
7434 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), args
);
7436 /* Then, see if we've already cloned the instantiation. */
7437 for (clone
= TREE_CHAIN (spec
);
7438 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7439 clone
= TREE_CHAIN (clone
))
7440 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
7443 /* If we haven't, do so know. */
7445 clone_function_decl (spec
, /*update_method_vec_p=*/0);
7448 for (clone
= TREE_CHAIN (spec
);
7449 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7450 clone
= TREE_CHAIN (clone
))
7451 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
7454 /* We should always have found the clone by now. */
7455 my_friendly_abort (20000411);
7459 /* Instantiate the indicated variable or function template TMPL with
7460 the template arguments in TARG_PTR. */
7463 instantiate_template (tmpl
, targ_ptr
)
7464 tree tmpl
, targ_ptr
;
7472 if (tmpl
== error_mark_node
)
7473 return error_mark_node
;
7475 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7477 /* If this function is a clone, handle it specially. */
7478 if (DECL_CLONED_FUNCTION_P (tmpl
))
7479 return instantiate_clone (tmpl
, targ_ptr
);
7481 /* Check to see if we already have this specialization. */
7482 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7483 if (spec
!= NULL_TREE
)
7486 gen_tmpl
= most_general_template (tmpl
);
7487 if (tmpl
!= gen_tmpl
)
7489 /* The TMPL is a partial instantiation. To get a full set of
7490 arguments we must add the arguments used to perform the
7491 partial instantiation. */
7492 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7495 /* Check to see if we already have this specialization. */
7496 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7497 if (spec
!= NULL_TREE
)
7501 len
= DECL_NTPARMS (gen_tmpl
);
7502 inner_args
= INNERMOST_TEMPLATE_ARGS (targ_ptr
);
7506 tree t
= TREE_VEC_ELT (inner_args
, i
);
7509 tree nt
= target_type (t
);
7510 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7512 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7513 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7514 return error_mark_node
;
7519 /* substitute template parameters */
7520 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
7521 targ_ptr
, /*complain=*/1, gen_tmpl
);
7522 /* The DECL_TI_TEMPLATE should always be the immediate parent
7523 template, not the most general template. */
7524 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7526 if (flag_external_templates
)
7527 add_pending_template (fndecl
);
7532 /* Push the name of the class template into the scope of the instantiation. */
7535 overload_template_name (type
)
7538 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
7541 if (IDENTIFIER_CLASS_VALUE (id
)
7542 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
7545 decl
= build_decl (TYPE_DECL
, id
, type
);
7546 SET_DECL_ARTIFICIAL (decl
);
7547 pushdecl_class_level (decl
);
7550 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7551 arguments that are being used when calling it. TARGS is a vector
7552 into which the deduced template arguments are placed.
7554 Return zero for success, 2 for an incomplete match that doesn't resolve
7555 all the types, and 1 for complete failure. An error message will be
7556 printed only for an incomplete match.
7558 If FN is a conversion operator, RETURN_TYPE is the type desired as
7559 the result of the conversion operator.
7561 TPARMS is a vector of template parameters.
7563 The EXPLICIT_TARGS are explicit template arguments provided via a
7566 The parameter STRICT is one of:
7569 We are deducing arguments for a function call, as in
7573 We are deducing arguments for a conversion function, as in
7577 We are deducing arguments when calculating the partial
7578 ordering between specializations of function or class
7579 templates, as in [temp.func.order] and [temp.class.order],
7580 when doing an explicit instantiation as in [temp.explicit],
7581 when determining an explicit specialization as in
7582 [temp.expl.spec], or when taking the address of a function
7583 template, as in [temp.deduct.funcaddr].
7585 The other arguments are as for type_unification. */
7588 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7590 tree fn
, explicit_targs
, targs
, args
, return_type
;
7591 unification_kind_t strict
;
7597 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7599 fntype
= TREE_TYPE (fn
);
7604 The specified template arguments must match the template
7605 parameters in kind (i.e., type, nontype, template), and there
7606 must not be more arguments than there are parameters;
7607 otherwise type deduction fails.
7609 Nontype arguments must match the types of the corresponding
7610 nontype template parameters, or must be convertible to the
7611 types of the corresponding nontype parameters as specified in
7612 _temp.arg.nontype_, otherwise type deduction fails.
7614 All references in the function type of the function template
7615 to the corresponding template parameters are replaced by the
7616 specified template argument values. If a substitution in a
7617 template parameter or in the function type of the function
7618 template results in an invalid type, type deduction fails. */
7620 tree converted_args
;
7623 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7624 explicit_targs
, NULL_TREE
, /*complain=*/0,
7625 /*require_all_arguments=*/0));
7626 if (converted_args
== error_mark_node
)
7629 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7630 if (fntype
== error_mark_node
)
7633 /* Place the explicitly specified arguments in TARGS. */
7634 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7635 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7638 parms
= TYPE_ARG_TYPES (fntype
);
7640 if (DECL_CONV_FN_P (fn
))
7642 /* This is a template conversion operator. Remove `this', since
7643 we could be comparing conversions from different classes. */
7644 parms
= TREE_CHAIN (parms
);
7645 args
= TREE_CHAIN (args
);
7646 my_friendly_assert (return_type
!= NULL_TREE
, 20000227);
7651 /* We've been given a return type to match, prepend it. */
7652 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
7653 args
= tree_cons (NULL_TREE
, return_type
, args
);
7656 /* We allow incomplete unification without an error message here
7657 because the standard doesn't seem to explicitly prohibit it. Our
7658 callers must be ready to deal with unification failures in any
7660 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7661 targs
, parms
, args
, /*subr=*/0,
7662 strict
, /*allow_incomplete*/1);
7665 /* All is well so far. Now, check:
7669 When all template arguments have been deduced, all uses of
7670 template parameters in nondeduced contexts are replaced with
7671 the corresponding deduced argument values. If the
7672 substitution results in an invalid type, as described above,
7673 type deduction fails. */
7674 if (tsubst (TREE_TYPE (fn
), targs
, /*complain=*/0, NULL_TREE
)
7681 /* Adjust types before performing type deduction, as described in
7682 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7683 sections are symmetric. PARM is the type of a function parameter
7684 or the return type of the conversion function. ARG is the type of
7685 the argument passed to the call, or the type of the value
7686 intialized with the result of the conversion function. */
7689 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7690 unification_kind_t strict
;
7701 /* Swap PARM and ARG throughout the remainder of this
7702 function; the handling is precisely symmetric since PARM
7703 will initialize ARG rather than vice versa. */
7711 /* There is nothing to do in this case. */
7715 my_friendly_abort (0);
7718 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7720 /* [temp.deduct.call]
7722 If P is not a reference type:
7724 --If A is an array type, the pointer type produced by the
7725 array-to-pointer standard conversion (_conv.array_) is
7726 used in place of A for type deduction; otherwise,
7728 --If A is a function type, the pointer type produced by
7729 the function-to-pointer standard conversion
7730 (_conv.func_) is used in place of A for type deduction;
7733 --If A is a cv-qualified type, the top level
7734 cv-qualifiers of A's type are ignored for type
7736 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7737 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7738 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7739 *arg
= build_pointer_type (*arg
);
7741 *arg
= TYPE_MAIN_VARIANT (*arg
);
7744 /* [temp.deduct.call]
7746 If P is a cv-qualified type, the top level cv-qualifiers
7747 of P's type are ignored for type deduction. If P is a
7748 reference type, the type referred to by P is used for
7750 *parm
= TYPE_MAIN_VARIANT (*parm
);
7751 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7752 *parm
= TREE_TYPE (*parm
);
7755 /* Like type_unfication.
7757 If SUBR is 1, we're being called recursively (to unify the
7758 arguments of a function or method parameter of a function
7762 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7763 strict
, allow_incomplete
)
7764 tree tparms
, targs
, parms
, args
;
7766 unification_kind_t strict
;
7767 int allow_incomplete
;
7771 int ntparms
= TREE_VEC_LENGTH (tparms
);
7774 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7775 my_friendly_assert (parms
== NULL_TREE
7776 || TREE_CODE (parms
) == TREE_LIST
, 290);
7777 /* ARGS could be NULL (via a call from parse.y to
7778 build_x_function_call). */
7780 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7781 my_friendly_assert (ntparms
> 0, 292);
7786 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7790 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7794 sub_strict
= UNIFY_ALLOW_NONE
;
7798 my_friendly_abort (0);
7802 && parms
!= void_list_node
7804 && args
!= void_list_node
)
7806 parm
= TREE_VALUE (parms
);
7807 parms
= TREE_CHAIN (parms
);
7808 arg
= TREE_VALUE (args
);
7809 args
= TREE_CHAIN (args
);
7811 if (arg
== error_mark_node
)
7813 if (arg
== unknown_type_node
)
7814 /* We can't deduce anything from this, but we might get all the
7815 template args from other function args. */
7818 /* Conversions will be performed on a function argument that
7819 corresponds with a function parameter that contains only
7820 non-deducible template parameters and explicitly specified
7821 template parameters. */
7822 if (! uses_template_parms (parm
))
7827 type
= TREE_TYPE (arg
);
7834 if (strict
== DEDUCE_EXACT
)
7836 if (same_type_p (parm
, type
))
7840 /* It might work; we shouldn't check now, because we might
7841 get into infinite recursion. Overload resolution will
7850 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7851 if (type_unknown_p (arg
))
7853 /* [temp.deduct.type] A template-argument can be deduced from
7854 a pointer to function or pointer to member function
7855 argument if the set of overloaded functions does not
7856 contain function templates and at most one of a set of
7857 overloaded functions provides a unique match. */
7859 if (resolve_overloaded_unification
7860 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
7865 arg
= TREE_TYPE (arg
);
7869 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7871 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
))
7879 /* Fail if we've reached the end of the parm list, and more args
7880 are present, and the parm list isn't variadic. */
7881 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7883 /* Fail if parms are left and they don't have default values. */
7885 && parms
!= void_list_node
7886 && TREE_PURPOSE (parms
) == NULL_TREE
)
7889 for (i
= 0; i
< ntparms
; i
++)
7890 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7892 if (!allow_incomplete
)
7893 error ("incomplete type unification");
7899 /* Subroutine of type_unification_real. Args are like the variables at the
7900 call site. ARG is an overloaded function (or template-id); we try
7901 deducing template args from each of the overloads, and if only one
7902 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7905 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7907 tree tparms
, targs
, parm
, arg
;
7908 unification_kind_t strict
;
7911 tree tempargs
= copy_node (targs
);
7914 if (TREE_CODE (arg
) == ADDR_EXPR
)
7915 arg
= TREE_OPERAND (arg
, 0);
7917 if (TREE_CODE (arg
) == COMPONENT_REF
)
7918 /* Handle `&x' where `x' is some static or non-static member
7920 arg
= TREE_OPERAND (arg
, 1);
7922 if (TREE_CODE (arg
) == OFFSET_REF
)
7923 arg
= TREE_OPERAND (arg
, 1);
7925 /* Strip baselink information. */
7926 while (TREE_CODE (arg
) == TREE_LIST
)
7927 arg
= TREE_VALUE (arg
);
7929 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7931 /* If we got some explicit template args, we need to plug them into
7932 the affected templates before we try to unify, in case the
7933 explicit args will completely resolve the templates in question. */
7935 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7936 arg
= TREE_OPERAND (arg
, 0);
7938 for (; arg
; arg
= OVL_NEXT (arg
))
7940 tree fn
= OVL_CURRENT (arg
);
7943 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7946 subargs
= get_bindings_overload (fn
, DECL_TEMPLATE_RESULT (fn
),
7950 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
7952 if (TREE_CODE (elem
) == METHOD_TYPE
)
7953 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7954 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7955 strict
, sub_strict
);
7959 else if (TREE_CODE (arg
) == OVERLOAD
)
7961 for (; arg
; arg
= OVL_NEXT (arg
))
7963 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
7964 if (TREE_CODE (type
) == METHOD_TYPE
)
7965 type
= build_ptrmemfunc_type (build_pointer_type (type
));
7966 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
7968 strict
, sub_strict
);
7972 my_friendly_abort (981006);
7974 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7975 to function or pointer to member function argument if the set of
7976 overloaded functions does not contain function templates and at most
7977 one of a set of overloaded functions provides a unique match.
7979 So if we found multiple possibilities, we return success but don't
7984 int i
= TREE_VEC_LENGTH (targs
);
7986 if (TREE_VEC_ELT (tempargs
, i
))
7987 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
7995 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7996 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7997 different overloads deduce different arguments for a given parm.
7998 Returns 1 on success. */
8001 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
8003 tree tparms
, orig_targs
, targs
, parm
, arg
;
8004 unification_kind_t strict
;
8011 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8012 to function or pointer to member function argument if the set of
8013 overloaded functions does not contain function templates and at most
8014 one of a set of overloaded functions provides a unique match.
8016 So if this is a template, just return success. */
8018 if (uses_template_parms (arg
))
8021 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8023 /* We don't copy orig_targs for this because if we have already deduced
8024 some template args from previous args, unify would complain when we
8025 try to deduce a template parameter for the same argument, even though
8026 there isn't really a conflict. */
8027 nargs
= TREE_VEC_LENGTH (targs
);
8028 tempargs
= make_tree_vec (nargs
);
8030 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
8033 /* First make sure we didn't deduce anything that conflicts with
8034 explicitly specified args. */
8035 for (i
= nargs
; i
--; )
8037 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8038 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
8040 if (elt
== NULL_TREE
)
8042 else if (uses_template_parms (elt
))
8044 /* Since we're unifying against ourselves, we will fill in template
8045 args used in the function parm list with our own template parms.
8047 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
8050 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
8054 for (i
= nargs
; i
--; )
8056 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8059 TREE_VEC_ELT (targs
, i
) = elt
;
8065 /* PARM is a template class (perhaps with unbound template
8066 parameters). ARG is a fully instantiated type. If ARG can be
8067 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8068 TARGS are as for unify. */
8071 try_class_unification (tparms
, targs
, parm
, arg
)
8080 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
8081 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
8084 /* We need to make a new template argument vector for the call to
8085 unify. If we used TARGS, we'd clutter it up with the result of
8086 the attempted unification, even if this class didn't work out.
8087 We also don't want to commit ourselves to all the unifications
8088 we've already done, since unification is supposed to be done on
8089 an argument-by-argument basis. In other words, consider the
8090 following pathological case:
8092 template <int I, int J, int K>
8095 template <int I, int J>
8096 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8098 template <int I, int J, int K>
8099 void f(S<I, J, K>, S<I, I, I>);
8108 Now, by the time we consider the unification involving `s2', we
8109 already know that we must have `f<0, 0, 0>'. But, even though
8110 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8111 because there are two ways to unify base classes of S<0, 1, 2>
8112 with S<I, I, I>. If we kept the already deduced knowledge, we
8113 would reject the possibility I=1. */
8114 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
8115 i
= unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
8116 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
);
8118 /* If unification failed, we're done. */
8125 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8126 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
8127 for the base class of ARG that we are currently examining. */
8130 get_template_base_recursive (tparms
, targs
, parm
,
8131 arg_binfo
, rval
, flags
)
8141 tree arg
= BINFO_TYPE (arg_binfo
);
8143 if (!(flags
& GTB_IGNORE_TYPE
))
8145 tree r
= try_class_unification (tparms
, targs
,
8148 /* If there is more than one satisfactory baseclass, then:
8152 If they yield more than one possible deduced A, the type
8156 if (r
&& rval
&& !same_type_p (r
, rval
))
8157 return error_mark_node
;
8162 binfos
= BINFO_BASETYPES (arg_binfo
);
8163 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
8165 /* Process base types. */
8166 for (i
= 0; i
< n_baselinks
; i
++)
8168 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
8171 /* Skip this base, if we've already seen it. */
8172 if (BINFO_MARKED (base_binfo
))
8176 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
8178 /* When searching for a non-virtual, we cannot mark virtually
8181 SET_BINFO_MARKED (base_binfo
);
8183 rval
= get_template_base_recursive (tparms
, targs
,
8187 GTB_VIA_VIRTUAL
* this_virtual
);
8189 /* If we discovered more than one matching base class, we can
8191 if (rval
== error_mark_node
)
8192 return error_mark_node
;
8198 /* Given a template type PARM and a class type ARG, find the unique
8199 base type in ARG that is an instance of PARM. We do not examine
8200 ARG itself; only its base-classes. If there is no appropriate base
8201 class, return NULL_TREE. If there is more than one, return
8202 error_mark_node. PARM may be the type of a partial specialization,
8203 as well as a plain template type. Used by unify. */
8206 get_template_base (tparms
, targs
, parm
, arg
)
8215 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
8217 arg_binfo
= TYPE_BINFO (complete_type (arg
));
8218 rval
= get_template_base_recursive (tparms
, targs
,
8223 /* Since get_template_base_recursive marks the bases classes, we
8224 must unmark them here. */
8225 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
8230 /* Returns the level of DECL, which declares a template parameter. */
8233 template_decl_level (decl
)
8236 switch (TREE_CODE (decl
))
8240 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
8243 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
8246 my_friendly_abort (0);
8251 /* Decide whether ARG can be unified with PARM, considering only the
8252 cv-qualifiers of each type, given STRICT as documented for unify.
8253 Returns non-zero iff the unification is OK on that basis.*/
8256 check_cv_quals_for_unify (strict
, arg
, parm
)
8261 if (!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
8262 && !at_least_as_qualified_p (arg
, parm
))
8265 if (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
8266 && !at_least_as_qualified_p (parm
, arg
))
8272 /* Takes parameters as for type_unification. Returns 0 if the
8273 type deduction suceeds, 1 otherwise. The parameter STRICT is a
8274 bitwise or of the following flags:
8277 Require an exact match between PARM and ARG.
8278 UNIFY_ALLOW_MORE_CV_QUAL:
8279 Allow the deduced ARG to be more cv-qualified than ARG.
8280 UNIFY_ALLOW_LESS_CV_QUAL:
8281 Allow the deduced ARG to be less cv-qualified than ARG.
8282 UNIFY_ALLOW_DERIVED:
8283 Allow the deduced ARG to be a template base class of ARG,
8284 or a pointer to a template base class of the type pointed to by
8286 UNIFY_ALLOW_INTEGER:
8287 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8288 case for more information. */
8291 unify (tparms
, targs
, parm
, arg
, strict
)
8292 tree tparms
, targs
, parm
, arg
;
8299 /* I don't think this will do the right thing with respect to types.
8300 But the only case I've seen it in so far has been array bounds, where
8301 signedness is the only information lost, and I think that will be
8303 while (TREE_CODE (parm
) == NOP_EXPR
)
8304 parm
= TREE_OPERAND (parm
, 0);
8306 if (arg
== error_mark_node
)
8308 if (arg
== unknown_type_node
)
8309 /* We can't deduce anything from this, but we might get all the
8310 template args from other function args. */
8313 /* If PARM uses template parameters, then we can't bail out here,
8314 even if ARG == PARM, since we won't record unifications for the
8315 template parameters. We might need them if we're trying to
8316 figure out which of two things is more specialized. */
8317 if (arg
== parm
&& !uses_template_parms (parm
))
8320 /* Immediately reject some pairs that won't unify because of
8321 cv-qualification mismatches. */
8322 if (TREE_CODE (arg
) == TREE_CODE (parm
)
8324 /* We check the cv-qualifiers when unifying with template type
8325 parameters below. We want to allow ARG `const T' to unify with
8326 PARM `T' for example, when computing which of two templates
8327 is more specialized, for example. */
8328 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
8329 && !check_cv_quals_for_unify (strict
, arg
, parm
))
8332 switch (TREE_CODE (parm
))
8335 /* In a type which contains a nested-name-specifier, template
8336 argument values cannot be deduced for template parameters used
8337 within the nested-name-specifier. */
8340 case TEMPLATE_TYPE_PARM
:
8341 case TEMPLATE_TEMPLATE_PARM
:
8342 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8344 if (TEMPLATE_TYPE_LEVEL (parm
)
8345 != template_decl_level (tparm
))
8346 /* The PARM is not one we're trying to unify. Just check
8347 to see if it matches ARG. */
8348 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8349 && same_type_p (parm
, arg
)) ? 0 : 1;
8350 idx
= TEMPLATE_TYPE_IDX (parm
);
8351 targ
= TREE_VEC_ELT (targs
, idx
);
8352 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
8354 /* Check for mixed types and values. */
8355 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
8356 && TREE_CODE (tparm
) != TYPE_DECL
)
8357 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8358 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8361 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
8363 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
8365 /* We arrive here when PARM does not involve template
8368 /* ARG must be constructed from a template class. */
8369 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
8373 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8374 tree parmvec
= TYPE_TI_ARGS (parm
);
8375 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
8377 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
8380 /* The parameter and argument roles have to be switched here
8381 in order to handle default arguments properly. For example,
8382 template<template <class> class TT> void f(TT<int>)
8383 should be able to accept vector<int> which comes from
8384 template <class T, class Allocator = allocator>
8387 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8391 /* Deduce arguments T, i from TT<T> or TT<i>.
8392 We check each element of PARMVEC and ARGVEC individually
8393 rather than the whole TREE_VEC since they can have
8394 different number of elements. */
8396 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8398 tree t
= TREE_VEC_ELT (parmvec
, i
);
8400 if (unify (tparms
, targs
, t
,
8401 TREE_VEC_ELT (argvec
, i
),
8406 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
8411 /* If PARM is `const T' and ARG is only `int', we don't have
8412 a match unless we are allowing additional qualification.
8413 If ARG is `const int' and PARM is just `T' that's OK;
8414 that binds `const int' to `T'. */
8415 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
8419 /* Consider the case where ARG is `const volatile int' and
8420 PARM is `const T'. Then, T should be `volatile int'. */
8422 cp_build_qualified_type_real (arg
,
8424 & ~CP_TYPE_QUALS (parm
),
8426 if (arg
== error_mark_node
)
8430 /* Simple cases: Value already set, does match or doesn't. */
8431 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8436 /* Make sure that ARG is not a variable-sized array. (Note that
8437 were talking about variable-sized arrays (like `int[n]'),
8438 rather than arrays of unknown size (like `int[]').) We'll
8439 get very confused by such a type since the bound of the array
8440 will not be computable in an instantiation. Besides, such
8441 types are not allowed in ISO C++, so we can do as we please
8443 if (TREE_CODE (arg
) == ARRAY_TYPE
8444 && !uses_template_parms (arg
)
8445 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8449 TREE_VEC_ELT (targs
, idx
) = arg
;
8452 case TEMPLATE_PARM_INDEX
:
8453 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8455 if (TEMPLATE_PARM_LEVEL (parm
)
8456 != template_decl_level (tparm
))
8457 /* The PARM is not one we're trying to unify. Just check
8458 to see if it matches ARG. */
8459 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8460 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8462 idx
= TEMPLATE_PARM_IDX (parm
);
8463 targ
= TREE_VEC_ELT (targs
, idx
);
8467 int i
= (cp_tree_equal (targ
, arg
) > 0);
8473 my_friendly_abort (42);
8476 /* [temp.deduct.type] If, in the declaration of a function template
8477 with a non-type template-parameter, the non-type
8478 template-parameter is used in an expression in the function
8479 parameter-list and, if the corresponding template-argument is
8480 deduced, the template-argument type shall match the type of the
8481 template-parameter exactly, except that a template-argument
8482 deduced from an array bound may be of any integral type. */
8483 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
8485 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8486 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8487 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8492 TREE_VEC_ELT (targs
, idx
) = arg
;
8499 if (TREE_CODE (arg
) != POINTER_TYPE
)
8502 /* [temp.deduct.call]
8504 A can be another pointer or pointer to member type that can
8505 be converted to the deduced A via a qualification
8506 conversion (_conv.qual_).
8508 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8509 This will allow for additional cv-qualification of the
8510 pointed-to types if appropriate. In general, this is a bit
8511 too generous; we are only supposed to allow qualification
8512 conversions and this method will allow an ARG of char** and
8513 a deduced ARG of const char**. However, overload
8514 resolution will subsequently invalidate the candidate, so
8515 this is probably OK. */
8516 sub_strict
= strict
;
8518 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
)
8519 /* The derived-to-base conversion only persists through one
8520 level of pointers. */
8521 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
8523 return unify (tparms
, targs
, TREE_TYPE (parm
),
8524 TREE_TYPE (arg
), sub_strict
);
8527 case REFERENCE_TYPE
:
8528 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8530 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8534 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8536 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8537 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8539 if (TYPE_DOMAIN (parm
) != NULL_TREE
8540 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8541 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8543 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8551 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8554 if (TREE_CODE (parm
) == INTEGER_TYPE
8555 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8557 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8558 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8559 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8561 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8562 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8563 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8566 /* We have already checked cv-qualification at the top of the
8568 else if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
8571 /* As far as unification is concerned, this wins. Later checks
8572 will invalidate it if necessary. */
8575 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8576 /* Type INTEGER_CST can come from ordinary constant template args. */
8578 while (TREE_CODE (arg
) == NOP_EXPR
)
8579 arg
= TREE_OPERAND (arg
, 0);
8581 if (TREE_CODE (arg
) != INTEGER_CST
)
8583 return !tree_int_cst_equal (parm
, arg
);
8588 if (TREE_CODE (arg
) != TREE_VEC
)
8590 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8592 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
8593 if (unify (tparms
, targs
,
8594 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8602 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8605 if (TYPE_PTRMEMFUNC_P (parm
))
8607 if (!TYPE_PTRMEMFUNC_P (arg
))
8610 return unify (tparms
, targs
,
8611 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8612 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
8616 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8620 if (strict
& UNIFY_ALLOW_DERIVED
)
8622 /* First, we try to unify the PARM and ARG directly. */
8623 t
= try_class_unification (tparms
, targs
,
8628 /* Fallback to the special case allowed in
8631 If P is a class, and P has the form
8632 template-id, then A can be a derived class of
8633 the deduced A. Likewise, if P is a pointer to
8634 a class of the form template-id, A can be a
8635 pointer to a derived class pointed to by the
8637 t
= get_template_base (tparms
, targs
,
8640 if (! t
|| t
== error_mark_node
)
8644 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8645 && (CLASSTYPE_TI_TEMPLATE (parm
)
8646 == CLASSTYPE_TI_TEMPLATE (arg
)))
8647 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8648 Then, we should unify `int' and `U'. */
8651 /* There's no chance of unication succeeding. */
8654 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8655 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8657 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
8663 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8666 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8667 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8669 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8670 TYPE_ARG_TYPES (arg
), 1,
8674 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8676 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8677 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8679 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8683 if (arg
!= decl_constant_value (parm
))
8688 /* Matched cases are handled by the ARG == PARM test above. */
8692 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8694 /* We handle this case specially, since it comes up with
8695 arrays. In particular, something like:
8697 template <int N> void f(int (&x)[N]);
8699 Here, we are trying to unify the range type, which
8700 looks like [0 ... (N - 1)]. */
8702 t1
= TREE_OPERAND (parm
, 0);
8703 t2
= TREE_OPERAND (parm
, 1);
8705 t
= fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
));
8707 return unify (tparms
, targs
, t1
, t
, strict
);
8709 /* else fall through */
8712 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8713 /* We're looking at an expression. This can happen with
8717 void foo(S<I>, S<I + 2>);
8719 This is a "nondeduced context":
8723 The nondeduced contexts are:
8725 --A type that is a template-id in which one or more of
8726 the template-arguments is an expression that references
8727 a template-parameter.
8729 In these cases, we assume deduction succeeded, but don't
8730 actually infer any unifications. */
8733 sorry ("use of `%s' in template type unification",
8734 tree_code_name
[(int) TREE_CODE (parm
)]);
8740 /* Called if RESULT is explicitly instantiated, or is a member of an
8741 explicitly instantiated class, or if using -frepo and the
8742 instantiation of RESULT has been assigned to this file. */
8745 mark_decl_instantiated (result
, extern_p
)
8749 if (TREE_CODE (result
) != FUNCTION_DECL
)
8750 /* The TREE_PUBLIC flag for function declarations will have been
8751 set correctly by tsubst. */
8752 TREE_PUBLIC (result
) = 1;
8756 DECL_INTERFACE_KNOWN (result
) = 1;
8757 DECL_NOT_REALLY_EXTERN (result
) = 1;
8759 /* Always make artificials weak. */
8760 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8761 comdat_linkage (result
);
8762 /* For WIN32 we also want to put explicit instantiations in
8763 linkonce sections. */
8764 else if (TREE_PUBLIC (result
))
8765 maybe_make_one_only (result
);
8767 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8771 /* Given two function templates PAT1 and PAT2, and explicit template
8772 arguments EXPLICIT_ARGS return:
8774 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8775 -1 if PAT2 is more specialized than PAT1.
8776 0 if neither is more specialized. */
8779 more_specialized (pat1
, pat2
, explicit_args
)
8780 tree pat1
, pat2
, explicit_args
;
8786 = get_bindings_overload (pat1
, DECL_TEMPLATE_RESULT (pat2
), explicit_args
);
8791 = get_bindings_overload (pat2
, DECL_TEMPLATE_RESULT (pat1
), explicit_args
);
8798 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8800 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8801 -1 if PAT2 is more specialized than PAT1.
8802 0 if neither is more specialized. */
8805 more_specialized_class (pat1
, pat2
)
8811 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8812 TREE_PURPOSE (pat2
));
8816 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8817 TREE_PURPOSE (pat1
));
8824 /* Return the template arguments that will produce the function signature
8825 DECL from the function template FN, with the explicit template
8826 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8827 also match. Return NULL_TREE if no satisfactory arguments could be
8831 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8832 tree fn
, decl
, explicit_args
;
8835 int ntparms
= DECL_NTPARMS (fn
);
8836 tree targs
= make_tree_vec (ntparms
);
8838 tree decl_arg_types
;
8841 /* Substitute the explicit template arguments into the type of DECL.
8842 The call to fn_type_unification will handle substitution into the
8844 decl_type
= TREE_TYPE (decl
);
8845 if (explicit_args
&& uses_template_parms (decl_type
))
8848 tree converted_args
;
8850 if (DECL_TEMPLATE_INFO (decl
))
8851 tmpl
= DECL_TI_TEMPLATE (decl
);
8853 /* We can get here for some illegal specializations. */
8857 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
8858 explicit_args
, NULL_TREE
,
8860 /*require_all_arguments=*/0));
8861 if (converted_args
== error_mark_node
)
8864 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
8866 if (decl_type
== error_mark_node
)
8870 /* If FN is a static member function, adjust the type of DECL
8872 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
8873 if (DECL_STATIC_FUNCTION_P (fn
)
8874 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8875 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8877 i
= fn_type_unification (fn
, explicit_args
, targs
,
8879 (check_rettype
|| DECL_CONV_FN_P (fn
)
8880 ? TREE_TYPE (decl_type
) : NULL_TREE
),
8889 /* For most uses, we want to check the return type. */
8892 get_bindings (fn
, decl
, explicit_args
)
8893 tree fn
, decl
, explicit_args
;
8895 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8898 /* But for more_specialized, we only care about the parameter types. */
8901 get_bindings_overload (fn
, decl
, explicit_args
)
8902 tree fn
, decl
, explicit_args
;
8904 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8907 /* Return the innermost template arguments that, when applied to a
8908 template specialization whose innermost template parameters are
8909 TPARMS, and whose specialization arguments are ARGS, yield the
8912 For example, suppose we have:
8914 template <class T, class U> struct S {};
8915 template <class T> struct S<T*, int> {};
8917 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8918 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8919 int}. The resulting vector will be {double}, indicating that `T'
8920 is bound to `double'. */
8923 get_class_bindings (tparms
, parms
, args
)
8924 tree tparms
, parms
, args
;
8926 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8927 tree vec
= make_tree_vec (ntparms
);
8929 args
= INNERMOST_TEMPLATE_ARGS (args
);
8931 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
))
8934 for (i
= 0; i
< ntparms
; ++i
)
8935 if (! TREE_VEC_ELT (vec
, i
))
8941 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8942 Pick the most specialized template, and return the corresponding
8943 instantiation, or if there is no corresponding instantiation, the
8944 template itself. EXPLICIT_ARGS is any template arguments explicity
8945 mentioned in a template-id. If there is no most specialized
8946 template, error_mark_node is returned. If there are no templates
8947 at all, NULL_TREE is returned. */
8950 most_specialized_instantiation (instantiations
, explicit_args
)
8951 tree instantiations
;
8957 if (!instantiations
)
8960 champ
= instantiations
;
8961 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
8963 fate
= more_specialized (TREE_VALUE (champ
),
8964 TREE_VALUE (fn
), explicit_args
);
8971 fn
= TREE_CHAIN (fn
);
8973 return error_mark_node
;
8979 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
8981 fate
= more_specialized (TREE_VALUE (champ
),
8982 TREE_VALUE (fn
), explicit_args
);
8984 return error_mark_node
;
8987 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
8990 /* Return the most specialized of the list of templates in FNS that can
8991 produce an instantiation matching DECL, given the explicit template
8992 arguments EXPLICIT_ARGS. */
8995 most_specialized (fns
, decl
, explicit_args
)
8996 tree fns
, decl
, explicit_args
;
8998 tree candidates
= NULL_TREE
;
9001 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
9003 tree candidate
= TREE_VALUE (fn
);
9005 args
= get_bindings (candidate
, decl
, explicit_args
);
9007 candidates
= tree_cons (NULL_TREE
, candidate
, candidates
);
9010 return most_specialized_instantiation (candidates
, explicit_args
);
9013 /* If DECL is a specialization of some template, return the most
9014 general such template. Otherwise, returns NULL_TREE.
9018 template <class T> struct S { template <class U> void f(U); };
9020 if TMPL is `template <class U> void S<int>::f(U)' this will return
9021 the full template. This function will not trace past partial
9022 specializations, however. For example, given in addition:
9024 template <class T> struct S<T*> { template <class U> void f(U); };
9026 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9027 `template <class T> template <class U> S<T*>::f(U)'. */
9030 most_general_template (decl
)
9033 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9034 an immediate specialization. */
9035 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9037 if (DECL_TEMPLATE_INFO (decl
)) {
9038 decl
= DECL_TI_TEMPLATE (decl
);
9040 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9042 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
9048 /* Look for more and more general templates. */
9049 while (DECL_TEMPLATE_INFO (decl
))
9051 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9052 in some cases. (See cp-tree.h for details.) */
9053 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9056 /* Stop if we run into an explicitly specialized class template. */
9057 if (!DECL_NAMESPACE_SCOPE_P (decl
)
9058 && DECL_CONTEXT (decl
)
9059 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
9062 decl
= DECL_TI_TEMPLATE (decl
);
9068 /* Return the most specialized of the class template specializations
9069 of TMPL which can produce an instantiation matching ARGS, or
9070 error_mark_node if the choice is ambiguous. */
9073 most_specialized_class (tmpl
, args
)
9077 tree list
= NULL_TREE
;
9082 tmpl
= most_general_template (tmpl
);
9083 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
9086 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
9089 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
9090 TREE_TYPE (list
) = TREE_TYPE (t
);
9100 for (; t
; t
= TREE_CHAIN (t
))
9102 fate
= more_specialized_class (champ
, t
);
9111 return error_mark_node
;
9117 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
9119 fate
= more_specialized_class (champ
, t
);
9121 return error_mark_node
;
9127 /* called from the parser. */
9130 do_decl_instantiation (declspecs
, declarator
, storage
)
9131 tree declspecs
, declarator
, storage
;
9133 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
9134 tree result
= NULL_TREE
;
9138 /* An error ocurred, for which grokdeclarator has already issued
9139 an appropriate message. */
9141 else if (! DECL_LANG_SPECIFIC (decl
))
9143 cp_error ("explicit instantiation of non-template `%#D'", decl
);
9146 else if (TREE_CODE (decl
) == VAR_DECL
)
9148 /* There is an asymmetry here in the way VAR_DECLs and
9149 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9150 the latter, the DECL we get back will be marked as a
9151 template instantiation, and the appropriate
9152 DECL_TEMPLATE_INFO will be set up. This does not happen for
9153 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9154 should handle VAR_DECLs as it currently handles
9156 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
9157 if (result
&& TREE_CODE (result
) != VAR_DECL
)
9159 cp_error ("no matching template for `%D' found", result
);
9163 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
9165 cp_error ("explicit instantiation of `%#D'", decl
);
9171 /* Check for various error cases. Note that if the explicit
9172 instantiation is legal the RESULT will currently be marked as an
9173 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9174 until we get here. */
9176 if (DECL_TEMPLATE_SPECIALIZATION (result
))
9180 No program shall both explicitly instantiate and explicitly
9181 specialize a template. */
9182 cp_pedwarn ("explicit instantiation of `%#D' after", result
);
9183 cp_pedwarn_at ("explicit specialization here", result
);
9186 else if (DECL_EXPLICIT_INSTANTIATION (result
))
9190 No program shall explicitly instantiate any template more
9193 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9194 instantiation was `extern' and the second is not, and EXTERN_P for
9195 the opposite case. If -frepo, chances are we already got marked
9196 as an explicit instantion because of the repo file. */
9197 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
9198 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result
);
9200 /* If we've already instantiated the template, just return now. */
9201 if (DECL_INTERFACE_KNOWN (result
))
9204 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
9206 cp_error ("no matching template for `%D' found", result
);
9209 else if (!DECL_TEMPLATE_INFO (result
))
9211 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
9215 if (flag_external_templates
)
9218 if (storage
== NULL_TREE
)
9220 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9223 cp_pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9227 cp_error ("storage class `%D' applied to template instantiation",
9230 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9231 mark_decl_instantiated (result
, extern_p
);
9232 repo_template_instantiated (result
, extern_p
);
9234 instantiate_decl (result
, /*defer_ok=*/1);
9238 mark_class_instantiated (t
, extern_p
)
9242 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
9243 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
9244 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
9245 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
9246 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
9249 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
9250 rest_of_type_compilation (t
, 1);
9255 do_type_instantiation (t
, storage
)
9262 if (TREE_CODE (t
) == TYPE_DECL
)
9265 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
9267 cp_error ("explicit instantiation of non-template type `%T'", t
);
9273 /* With -fexternal-templates, explicit instantiations are treated the same
9274 as implicit ones. */
9275 if (flag_external_templates
)
9278 if (!COMPLETE_TYPE_P (t
))
9280 cp_error ("explicit instantiation of `%#T' before definition of template",
9285 if (storage
!= NULL_TREE
)
9288 cp_pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9289 IDENTIFIER_POINTER (storage
));
9291 if (storage
== ridpointers
[(int) RID_INLINE
])
9293 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9295 else if (storage
== ridpointers
[(int) RID_STATIC
])
9299 cp_error ("storage class `%D' applied to template instantiation",
9305 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
9309 No program shall both explicitly instantiate and explicitly
9310 specialize a template. */
9311 cp_error ("explicit instantiation of `%#T' after", t
);
9312 cp_error_at ("explicit specialization here", t
);
9315 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
9319 No program shall explicitly instantiate any template more
9322 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9323 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9324 are we already got marked as an explicit instantion because of the
9325 repo file. All these cases are OK. */
9326 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
)
9327 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t
);
9329 /* If we've already instantiated the template, just return now. */
9330 if (!CLASSTYPE_INTERFACE_ONLY (t
))
9334 mark_class_instantiated (t
, extern_p
);
9335 repo_template_instantiated (t
, extern_p
);
9343 /* In contrast to implicit instantiation, where only the
9344 declarations, and not the definitions, of members are
9345 instantiated, we have here:
9349 The explicit instantiation of a class template specialization
9350 implies the instantiation of all of its members not
9351 previously explicitly specialized in the translation unit
9352 containing the explicit instantiation.
9354 Of course, we can't instantiate member template classes, since
9355 we don't have any arguments for them. Note that the standard
9356 is unclear on whether the instatiation of the members are
9357 *explicit* instantiations or not. We choose to be generous,
9358 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9359 the explicit instantiation of a class where some of the members
9360 have no definition in the current translation unit. */
9363 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9364 if (TREE_CODE (tmp
) == FUNCTION_DECL
9365 && DECL_TEMPLATE_INSTANTIATION (tmp
))
9367 mark_decl_instantiated (tmp
, extern_p
);
9368 repo_template_instantiated (tmp
, extern_p
);
9370 instantiate_decl (tmp
, /*defer_ok=*/1);
9373 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9374 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
9376 mark_decl_instantiated (tmp
, extern_p
);
9377 repo_template_instantiated (tmp
, extern_p
);
9379 instantiate_decl (tmp
, /*defer_ok=*/1);
9382 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9383 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
9384 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
9385 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
9389 /* Given a function DECL, which is a specialization of TMPL, modify
9390 DECL to be a re-instantiation of TMPL with the same template
9391 arguments. TMPL should be the template into which tsubst'ing
9392 should occur for DECL, not the most general template.
9394 One reason for doing this is a scenario like this:
9397 void f(const T&, int i);
9399 void g() { f(3, 7); }
9402 void f(const T& t, const int i) { }
9404 Note that when the template is first instantiated, with
9405 instantiate_template, the resulting DECL will have no name for the
9406 first parameter, and the wrong type for the second. So, when we go
9407 to instantiate the DECL, we regenerate it. */
9410 regenerate_decl_from_template (decl
, tmpl
)
9414 /* The most general version of TMPL. */
9416 /* The arguments used to instantiate DECL, from the most general
9423 args
= DECL_TI_ARGS (decl
);
9424 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9426 /* Unregister the specialization so that when we tsubst we will not
9427 just return DECL. We don't have to unregister DECL from TMPL
9428 because if would only be registered there if it were a partial
9429 instantiation of a specialization, which it isn't: it's a full
9431 gen_tmpl
= most_general_template (tmpl
);
9432 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9434 /* If the DECL was not unregistered then something peculiar is
9435 happening: we created a specialization but did not call
9436 register_specialization for it. */
9437 my_friendly_assert (unregistered
, 0);
9439 if (TREE_CODE (decl
) == VAR_DECL
)
9440 /* Make sure that we can see identifiers, and compute access
9441 correctly, for the class members used in the declaration of
9442 this static variable. */
9443 pushclass (DECL_CONTEXT (decl
), 2);
9445 /* Do the substitution to get the new declaration. */
9446 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9448 if (TREE_CODE (decl
) == VAR_DECL
)
9450 /* Set up DECL_INITIAL, since tsubst doesn't. */
9451 DECL_INITIAL (new_decl
) =
9452 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9453 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9454 /* Pop the class context we pushed above. */
9457 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9459 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9461 DECL_INITIAL (new_decl
) = error_mark_node
;
9462 /* And don't complain about a duplicate definition. */
9463 DECL_INITIAL (decl
) = NULL_TREE
;
9466 /* The immediate parent of the new template is still whatever it was
9467 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9468 general template. We also reset the DECL_ASSEMBLER_NAME since
9469 tsubst always calculates the name as if the function in question
9470 were really a template instance, and sometimes, with friend
9471 functions, this is not so. See tsubst_friend_function for
9473 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9474 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9475 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9477 /* Call duplicate decls to merge the old and new declarations. */
9478 duplicate_decls (new_decl
, decl
);
9480 /* Now, re-register the specialization. */
9481 register_specialization (decl
, gen_tmpl
, args
);
9484 /* Produce the definition of D, a _DECL generated from a template. If
9485 DEFER_OK is non-zero, then we don't have to actually do the
9486 instantiation now; we just have to do it sometime. */
9489 instantiate_decl (d
, defer_ok
)
9493 tree tmpl
= DECL_TI_TEMPLATE (d
);
9494 tree args
= DECL_TI_ARGS (d
);
9499 int pattern_defined
;
9501 const char *file
= input_filename
;
9503 /* This function should only be used to instantiate templates for
9504 functions and static member variables. */
9505 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9506 || TREE_CODE (d
) == VAR_DECL
, 0);
9508 /* Don't instantiate cloned functions. Instead, instantiate the
9509 functions they cloned. */
9510 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
9511 d
= DECL_CLONED_FUNCTION (d
);
9513 if (DECL_TEMPLATE_INSTANTIATED (d
))
9514 /* D has already been instantiated. It might seem reasonable to
9515 check whether or not D is an explict instantiation, and, if so,
9516 stop here. But when an explicit instantiation is deferred
9517 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9518 is set, even though we still need to do the instantiation. */
9521 /* If we already have a specialization of this declaration, then
9522 there's no reason to instantiate it. Note that
9523 retrieve_specialization gives us both instantiations and
9524 specializations, so we must explicitly check
9525 DECL_TEMPLATE_SPECIALIZATION. */
9526 gen_tmpl
= most_general_template (tmpl
);
9527 spec
= retrieve_specialization (gen_tmpl
, args
);
9528 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9531 /* This needs to happen before any tsubsting. */
9532 if (! push_tinst_level (d
))
9535 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9536 for the instantiation. This is not always the most general
9537 template. Consider, for example:
9540 struct S { template <class U> void f();
9541 template <> void f<int>(); };
9543 and an instantiation of S<double>::f<int>. We want TD to be the
9544 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9546 while (/* An instantiation cannot have a definition, so we need a
9547 more general template. */
9548 DECL_TEMPLATE_INSTANTIATION (td
)
9549 /* We must also deal with friend templates. Given:
9551 template <class T> struct S {
9552 template <class U> friend void f() {};
9555 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9556 so far as the language is concerned, but that's still
9557 where we get the pattern for the instantiation from. On
9558 ther hand, if the definition comes outside the class, say:
9560 template <class T> struct S {
9561 template <class U> friend void f();
9563 template <class U> friend void f() {}
9565 we don't need to look any further. That's what the check for
9566 DECL_INITIAL is for. */
9567 || (TREE_CODE (d
) == FUNCTION_DECL
9568 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9569 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
))))
9571 /* The present template, TD, should not be a definition. If it
9572 were a definition, we should be using it! Note that we
9573 cannot restructure the loop to just keep going until we find
9574 a template with a definition, since that might go too far if
9575 a specialization was declared, but not defined. */
9576 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9577 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9580 /* Fetch the more general template. */
9581 td
= DECL_TI_TEMPLATE (td
);
9584 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9586 if (TREE_CODE (d
) == FUNCTION_DECL
)
9587 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
9589 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9591 push_to_top_level ();
9592 lineno
= DECL_SOURCE_LINE (d
);
9593 input_filename
= DECL_SOURCE_FILE (d
);
9595 if (pattern_defined
)
9597 repo_template_used (d
);
9599 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9601 if (flag_alt_external_templates
)
9603 if (interface_unknown
)
9604 warn_if_unknown_interface (d
);
9606 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9608 DECL_INTERFACE_KNOWN (d
) = 1;
9609 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9612 warn_if_unknown_interface (code_pattern
);
9616 import_export_decl (d
);
9619 /* Reject all external templates except inline functions. */
9620 if (DECL_INTERFACE_KNOWN (d
)
9621 && ! DECL_NOT_REALLY_EXTERN (d
)
9622 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9625 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9626 the variable is a static const initialized in the class body. */
9627 if (TREE_CODE (d
) == VAR_DECL
9628 && TREE_READONLY (d
)
9629 && DECL_INITIAL (d
) == NULL_TREE
9630 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
9632 /* Defer all other templates, unless we have been explicitly
9633 forbidden from doing so. We restore the source position here
9634 because it's used by add_pending_template. */
9635 else if (! pattern_defined
|| defer_ok
)
9638 input_filename
= file
;
9640 if (at_eof
&& !pattern_defined
9641 && DECL_EXPLICIT_INSTANTIATION (d
))
9644 The definition of a non-exported function template, a
9645 non-exported member function template, or a non-exported
9646 member function or static data member of a class template
9647 shall be present in every translation unit in which it is
9648 explicitly instantiated. */
9649 cp_error ("explicit instantiation of `%D' but no definition available",
9652 add_pending_template (d
);
9656 /* We're now committed to instantiating this template. Mark it as
9657 instantiated so that recursive calls to instantiate_decl do not
9658 try to instantiate it again. */
9659 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9661 /* Regenerate the declaration in case the template has been modified
9662 by a subsequent redeclaration. */
9663 regenerate_decl_from_template (d
, td
);
9665 /* We already set the file and line above. Reset them now in case
9666 they changed as a result of calling regenerate_decl_from_template. */
9667 lineno
= DECL_SOURCE_LINE (d
);
9668 input_filename
= DECL_SOURCE_FILE (d
);
9670 if (TREE_CODE (d
) == VAR_DECL
)
9672 DECL_IN_AGGR_P (d
) = 0;
9673 if (DECL_INTERFACE_KNOWN (d
))
9674 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9677 DECL_EXTERNAL (d
) = 1;
9678 DECL_NOT_REALLY_EXTERN (d
) = 1;
9680 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0);
9682 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9684 htab_t saved_local_specializations
;
9686 /* Save away the current list, in case we are instantiating one
9687 template from within the body of another. */
9688 saved_local_specializations
= local_specializations
;
9690 /* Set up the list of local specializations. */
9691 local_specializations
= htab_create (37,
9696 /* Set up context. */
9697 start_function (NULL_TREE
, d
, NULL_TREE
, SF_PRE_PARSED
);
9698 store_parm_decls ();
9700 /* We already set up __FUNCTION__, etc., so we don't want to do
9702 current_function_name_declared
= 1;
9704 /* Substitute into the body of the function. */
9705 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
9706 /*complain=*/1, tmpl
);
9708 /* We don't need the local specializations any more. */
9709 htab_delete (local_specializations
);
9710 local_specializations
= saved_local_specializations
;
9712 /* Finish the function. */
9713 expand_body (finish_function (0));
9716 /* We're not deferring instantiation any more. */
9717 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
9721 input_filename
= file
;
9723 pop_from_top_level ();
9729 /* Run through the list of templates that we wish we could
9730 instantiate, and instantiate any we can. */
9733 instantiate_pending_templates ()
9736 int instantiated_something
= 0;
9743 t
= &pending_templates
;
9746 tree srcloc
= TREE_PURPOSE (*t
);
9747 tree instantiation
= TREE_VALUE (*t
);
9749 input_filename
= SRCLOC_FILE (srcloc
);
9750 lineno
= SRCLOC_LINE (srcloc
);
9752 if (TYPE_P (instantiation
))
9756 if (!COMPLETE_TYPE_P (instantiation
))
9758 instantiate_class_template (instantiation
);
9759 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
9760 for (fn
= TYPE_METHODS (instantiation
);
9762 fn
= TREE_CHAIN (fn
))
9763 if (! DECL_ARTIFICIAL (fn
))
9764 instantiate_decl (fn
, /*defer_ok=*/0);
9765 if (COMPLETE_TYPE_P (instantiation
))
9767 instantiated_something
= 1;
9772 if (COMPLETE_TYPE_P (instantiation
))
9773 /* If INSTANTIATION has been instantiated, then we don't
9774 need to consider it again in the future. */
9775 *t
= TREE_CHAIN (*t
);
9777 t
= &TREE_CHAIN (*t
);
9781 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
9782 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
9784 instantiation
= instantiate_decl (instantiation
,
9786 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
9788 instantiated_something
= 1;
9793 if (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
9794 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
9795 /* If INSTANTIATION has been instantiated, then we don't
9796 need to consider it again in the future. */
9797 *t
= TREE_CHAIN (*t
);
9799 t
= &TREE_CHAIN (*t
);
9804 /* Go through the things that are template instantiations if we are
9805 using guiding declarations. */
9806 t
= &maybe_templates
;
9813 fn
= TREE_VALUE (*t
);
9815 if (DECL_INITIAL (fn
))
9816 /* If the FN is already defined, then it was either already
9817 instantiated or, even though guiding declarations were
9818 allowed, a non-template definition was provided. */
9822 template = TREE_PURPOSE (*t
);
9823 args
= get_bindings (template, fn
, NULL_TREE
);
9824 fn
= instantiate_template (template, args
);
9825 instantiate_decl (fn
, /*defer_ok=*/0);
9829 /* Remove this entry from the chain. */
9830 *t
= TREE_CHAIN (*t
);
9832 maybe_template_tail
= t
;
9836 return instantiated_something
;
9839 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9840 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9841 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9844 tsubst_expr_values (t
, argvec
)
9847 tree first
= NULL_TREE
;
9850 for (; t
; t
= TREE_CHAIN (t
))
9852 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
9853 /*complain=*/1, NULL_TREE
);
9854 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
9856 *p
= build_tree_list (pur
, val
);
9857 p
= &TREE_CHAIN (*p
);
9862 /* D is an undefined function declaration in the presence of templates with
9863 the same name, listed in FNS. If one of them can produce D as an
9864 instantiation, remember this so we can instantiate it at EOF if D has
9865 not been defined by that time. */
9868 add_maybe_template (d
, fns
)
9873 if (DECL_MAYBE_TEMPLATE (d
))
9876 t
= most_specialized (fns
, d
, NULL_TREE
);
9879 if (t
== error_mark_node
)
9881 cp_error ("ambiguous template instantiation for `%D'", d
);
9885 *maybe_template_tail
= tree_cons (t
, d
, NULL_TREE
);
9886 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
9887 DECL_MAYBE_TEMPLATE (d
) = 1;
9890 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9893 set_current_access_from_decl (decl
)
9896 if (TREE_PRIVATE (decl
))
9897 current_access_specifier
= access_private_node
;
9898 else if (TREE_PROTECTED (decl
))
9899 current_access_specifier
= access_protected_node
;
9901 current_access_specifier
= access_public_node
;
9904 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9905 is the instantiation (which should have been created with
9906 start_enum) and ARGS are the template arguments to use. */
9909 tsubst_enum (tag
, newtag
, args
)
9916 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9920 /* Note that in a template enum, the TREE_VALUE is the
9921 CONST_DECL, not the corresponding INTEGER_CST. */
9922 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9923 args
, /*complain=*/1,
9926 /* Give this enumeration constant the correct access. */
9927 set_current_access_from_decl (TREE_VALUE (e
));
9929 /* Actually build the enumerator itself. */
9930 build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9933 finish_enum (newtag
);
9936 /* DECL is a FUNCTION_DECL that is a template specialization. Return
9937 its type -- but without substituting the innermost set of template
9938 arguments. So, innermost set of template parameters will appear in
9939 the type. If CONTEXTP is non-NULL, then the partially substituted
9940 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
9941 will be filled in with the substituted template parameters, if it
9945 get_mostly_instantiated_function_type (decl
, contextp
, tparmsp
)
9950 tree context
= NULL_TREE
;
9957 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9958 targs
= DECL_TI_ARGS (decl
);
9959 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
9960 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
9962 /* There should be as many levels of arguments as there are levels
9964 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
9966 fn_type
= TREE_TYPE (tmpl
);
9967 if (DECL_STATIC_FUNCTION_P (decl
))
9968 context
= DECL_CONTEXT (decl
);
9970 if (parm_depth
== 1)
9971 /* No substitution is necessary. */
9978 /* Replace the innermost level of the TARGS with NULL_TREEs to
9979 let tsubst know not to subsitute for those parameters. */
9980 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
9981 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
9982 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
9983 TMPL_ARGS_LEVEL (targs
, i
));
9984 SET_TMPL_ARGS_LEVEL (partial_args
,
9985 TMPL_ARGS_DEPTH (targs
),
9986 make_tree_vec (DECL_NTPARMS (tmpl
)));
9988 /* Now, do the (partial) substitution to figure out the
9989 appropriate function type. */
9990 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
9991 if (DECL_STATIC_FUNCTION_P (decl
))
9992 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
9994 /* Substitute into the template parameters to obtain the real
9995 innermost set of parameters. This step is important if the
9996 innermost set of template parameters contains value
9997 parameters whose types depend on outer template parameters. */
9998 TREE_VEC_LENGTH (partial_args
)--;
9999 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
10003 *contextp
= context
;
10010 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
10011 is either an instantiation or specialization of a template
10015 set_mangled_name_for_template_decl (decl
)
10018 tree context
= NULL_TREE
;
10025 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
10026 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
10028 /* Under the new ABI, we don't need special machinery. */
10031 set_mangled_name_for_decl (decl
);
10035 /* The names of template functions must be mangled so as to indicate
10036 what template is being specialized with what template arguments.
10037 For example, each of the following three functions must get
10038 different mangled names:
10041 template <> void f<7>(int);
10042 template <> void f<8>(int); */
10044 targs
= DECL_TI_ARGS (decl
);
10045 if (uses_template_parms (targs
))
10046 /* This DECL is for a partial instantiation. There's no need to
10047 mangle the name of such an entity. */
10050 /* We now compute the PARMS and RET_TYPE to give to
10051 build_decl_overload_real. The PARMS and RET_TYPE are the
10052 parameter and return types of the template, after all but the
10053 innermost template arguments have been substituted, not the
10054 parameter and return types of the function DECL. For example,
10057 template <class T> T f(T);
10059 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10060 A more subtle example is:
10062 template <class T> struct S { template <class U> void f(T, U); }
10064 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10065 {int, U}. Thus, the args that we want to subsitute into the
10066 return and parameter type for the function are those in TARGS,
10067 with the innermost level omitted. */
10068 fn_type
= get_mostly_instantiated_function_type (decl
, &context
, &tparms
);
10070 /* Now, get the innermost parameters and arguments, and figure out
10071 the parameter and return types. */
10072 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
10073 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
10074 ret_type
= TREE_TYPE (fn_type
);
10075 parm_types
= TYPE_ARG_TYPES (fn_type
);
10077 /* For a static member function, we generate a fake `this' pointer,
10078 for the purposes of mangling. This indicates of which class the
10079 function is a member. Because of:
10083 There shall not be a static and a nonstatic member function
10084 with the same name and the same parameter types
10086 we don't have to worry that this will result in a clash with a
10087 non-static member function. */
10088 if (DECL_STATIC_FUNCTION_P (decl
))
10089 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
10091 /* There should be the same number of template parameters as
10092 template arguments. */
10093 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
10096 /* Actually set the DECL_ASSEMBLER_NAME. */
10097 DECL_ASSEMBLER_NAME (decl
)
10098 = build_decl_overload_real (decl
, parm_types
, ret_type
,
10100 DECL_FUNCTION_MEMBER_P (decl
)
10101 + DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl
));