1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
35 #include "tree-inline.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) PARAMS ((tree
, void*));
50 extern struct obstack permanent_obstack
;
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the
55 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
56 the instantiate request occurred; the TREE_VALUE is a either a DECL
57 (for a function or static data member), or a TYPE (for a class)
58 indicating what we are hoping to instantiate. */
59 static tree pending_templates
;
60 static tree last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static tree saved_trees
;
66 static varray_type inline_parm_levels
;
67 static size_t inline_parm_levels_used
;
69 static tree current_tinst_level
;
71 /* A map from local variable declarations in the body of the template
72 presently being instantiated to the corresponding instantiated
74 static htab_t local_specializations
;
76 #define obstack_chunk_alloc xmalloc
77 #define obstack_chunk_free free
79 #define UNIFY_ALLOW_NONE 0
80 #define UNIFY_ALLOW_MORE_CV_QUAL 1
81 #define UNIFY_ALLOW_LESS_CV_QUAL 2
82 #define UNIFY_ALLOW_DERIVED 4
83 #define UNIFY_ALLOW_INTEGER 8
84 #define UNIFY_ALLOW_OUTER_LEVEL 16
85 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
86 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
87 #define UNIFY_ALLOW_MAX_CORRECTION 128
89 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
90 virtual, or a base class of a virtual
92 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
93 type with the desired type. */
95 static int resolve_overloaded_unification
PARAMS ((tree
, tree
, tree
, tree
,
96 unification_kind_t
, int));
97 static int try_one_overload
PARAMS ((tree
, tree
, tree
, tree
, tree
,
98 unification_kind_t
, int));
99 static int unify
PARAMS ((tree
, tree
, tree
, tree
, int));
100 static void add_pending_template
PARAMS ((tree
));
101 static void reopen_tinst_level
PARAMS ((tree
));
102 static tree classtype_mangled_name
PARAMS ((tree
));
103 static char *mangle_class_name_for_template
PARAMS ((const char *,
105 static tree tsubst_initializer_list
PARAMS ((tree
, tree
));
106 static int list_eq
PARAMS ((tree
, tree
));
107 static tree get_class_bindings
PARAMS ((tree
, tree
, tree
));
108 static tree coerce_template_parms
PARAMS ((tree
, tree
, tree
,
109 tsubst_flags_t
, int));
110 static void tsubst_enum
PARAMS ((tree
, tree
, tree
));
111 static tree add_to_template_args
PARAMS ((tree
, tree
));
112 static tree add_outermost_template_args
PARAMS ((tree
, tree
));
113 static int maybe_adjust_types_for_deduction
PARAMS ((unification_kind_t
, tree
*,
115 static int type_unification_real
PARAMS ((tree
, tree
, tree
, tree
,
116 int, unification_kind_t
, int, int));
117 static void note_template_header
PARAMS ((int));
118 static tree maybe_fold_nontype_arg
PARAMS ((tree
));
119 static tree convert_nontype_argument
PARAMS ((tree
, tree
));
120 static tree convert_template_argument
PARAMS ((tree
, tree
, tree
,
121 tsubst_flags_t
, int, tree
));
122 static tree get_bindings_overload
PARAMS ((tree
, tree
, tree
));
123 static int for_each_template_parm
PARAMS ((tree
, tree_fn_t
, void*));
124 static tree build_template_parm_index
PARAMS ((int, int, int, tree
, tree
));
125 static int inline_needs_template_parms
PARAMS ((tree
));
126 static void push_inline_template_parms_recursive
PARAMS ((tree
, int));
127 static tree retrieve_specialization
PARAMS ((tree
, tree
));
128 static tree retrieve_local_specialization
PARAMS ((tree
));
129 static tree register_specialization
PARAMS ((tree
, tree
, tree
));
130 static void register_local_specialization
PARAMS ((tree
, tree
));
131 static int unregister_specialization
PARAMS ((tree
, tree
));
132 static tree reduce_template_parm_level
PARAMS ((tree
, tree
, int));
133 static tree build_template_decl
PARAMS ((tree
, tree
));
134 static int mark_template_parm
PARAMS ((tree
, void *));
135 static tree tsubst_friend_function
PARAMS ((tree
, tree
));
136 static tree tsubst_friend_class
PARAMS ((tree
, tree
));
137 static int can_complete_type_without_circularity
PARAMS ((tree
));
138 static tree get_bindings_real
PARAMS ((tree
, tree
, tree
, int, int, int));
139 static int template_decl_level
PARAMS ((tree
));
140 static tree maybe_get_template_decl_from_type_decl
PARAMS ((tree
));
141 static int check_cv_quals_for_unify
PARAMS ((int, tree
, tree
));
142 static tree tsubst_template_arg_vector
PARAMS ((tree
, tree
, tsubst_flags_t
));
143 static tree tsubst_template_parms
PARAMS ((tree
, tree
, tsubst_flags_t
));
144 static void regenerate_decl_from_template
PARAMS ((tree
, tree
));
145 static tree most_specialized
PARAMS ((tree
, tree
, tree
));
146 static tree most_specialized_class
PARAMS ((tree
, tree
));
147 static int template_class_depth_real
PARAMS ((tree
, int));
148 static tree tsubst_aggr_type
PARAMS ((tree
, tree
, tsubst_flags_t
, tree
, int));
149 static tree tsubst_decl
PARAMS ((tree
, tree
, tree
, tsubst_flags_t
));
150 static tree tsubst_arg_types
PARAMS ((tree
, tree
, tsubst_flags_t
, tree
));
151 static tree tsubst_function_type
PARAMS ((tree
, tree
, tsubst_flags_t
, tree
));
152 static void check_specialization_scope
PARAMS ((void));
153 static tree process_partial_specialization
PARAMS ((tree
));
154 static void set_current_access_from_decl
PARAMS ((tree
));
155 static void check_default_tmpl_args
PARAMS ((tree
, tree
, int, int));
156 static tree tsubst_call_declarator_parms
PARAMS ((tree
, tree
,
157 tsubst_flags_t
, tree
));
158 static tree get_template_base_recursive
PARAMS ((tree
, tree
,
159 tree
, tree
, tree
, int));
160 static tree get_template_base
PARAMS ((tree
, tree
, tree
, tree
));
161 static int verify_class_unification
PARAMS ((tree
, tree
, tree
));
162 static tree try_class_unification
PARAMS ((tree
, tree
, tree
, tree
));
163 static int coerce_template_template_parms
PARAMS ((tree
, tree
, tsubst_flags_t
,
165 static tree determine_specialization
PARAMS ((tree
, tree
, tree
*, int));
166 static int template_args_equal
PARAMS ((tree
, tree
));
167 static void tsubst_default_arguments
PARAMS ((tree
));
168 static tree for_each_template_parm_r
PARAMS ((tree
*, int *, void *));
169 static tree copy_default_args_to_explicit_spec_1
PARAMS ((tree
, tree
));
170 static void copy_default_args_to_explicit_spec
PARAMS ((tree
));
171 static int invalid_nontype_parm_type_p
PARAMS ((tree
, tsubst_flags_t
));
173 /* Called once to initialize pt.c. */
178 ggc_add_tree_root (&pending_templates
, 1);
179 ggc_add_tree_root (&saved_trees
, 1);
180 ggc_add_tree_root (¤t_tinst_level
, 1);
183 /* Do any processing required when DECL (a member template declaration
184 using TEMPLATE_PARAMETERS as its innermost parameter list) is
185 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
186 it is a specialization, in which case the DECL itself is returned. */
189 finish_member_template_decl (decl
)
192 if (decl
== NULL_TREE
|| decl
== void_type_node
)
194 else if (decl
== error_mark_node
)
195 /* By returning NULL_TREE, the parser will just ignore this
196 declaration. We have already issued the error. */
198 else if (TREE_CODE (decl
) == TREE_LIST
)
200 /* Assume that the class is the only declspec. */
201 decl
= TREE_VALUE (decl
);
202 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
203 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
205 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
206 check_member_template (tmpl
);
211 else if (TREE_CODE (decl
) == FIELD_DECL
)
212 error ("data member `%D' cannot be a member template", decl
);
213 else if (DECL_TEMPLATE_INFO (decl
))
215 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
217 check_member_template (DECL_TI_TEMPLATE (decl
));
218 return DECL_TI_TEMPLATE (decl
);
224 error ("invalid member template declaration `%D'", decl
);
226 return error_mark_node
;
229 /* Returns the template nesting level of the indicated class TYPE.
239 A<T>::B<U> has depth two, while A<T> has depth one.
240 Both A<T>::B<int> and A<int>::B<U> have depth one, if
241 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
244 This function is guaranteed to return 0 if passed NULL_TREE so
245 that, for example, `template_class_depth (current_class_type)' is
249 template_class_depth_real (type
, count_specializations
)
251 int count_specializations
;
256 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
257 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
258 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
260 if (TREE_CODE (type
) != FUNCTION_DECL
)
262 if (CLASSTYPE_TEMPLATE_INFO (type
)
263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
264 && ((count_specializations
265 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
266 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
271 if (DECL_TEMPLATE_INFO (type
)
272 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
273 && ((count_specializations
274 && DECL_TEMPLATE_SPECIALIZATION (type
))
275 || uses_template_parms (DECL_TI_ARGS (type
))))
283 /* Returns the template nesting level of the indicated class TYPE.
284 Like template_class_depth_real, but instantiations do not count in
288 template_class_depth (type
)
291 return template_class_depth_real (type
, /*count_specializations=*/0);
294 /* Returns 1 if processing DECL as part of do_pending_inlines
295 needs us to push template parms. */
298 inline_needs_template_parms (decl
)
301 if (! DECL_TEMPLATE_INFO (decl
))
304 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
305 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
308 /* Subroutine of maybe_begin_member_template_processing.
309 Push the template parms in PARMS, starting from LEVELS steps into the
310 chain, and ending at the beginning, since template parms are listed
314 push_inline_template_parms_recursive (parmlist
, levels
)
318 tree parms
= TREE_VALUE (parmlist
);
322 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
324 ++processing_template_decl
;
325 current_template_parms
326 = tree_cons (size_int (processing_template_decl
),
327 parms
, current_template_parms
);
328 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
331 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
333 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
334 my_friendly_assert (DECL_P (parm
), 0);
336 switch (TREE_CODE (parm
))
345 /* Make a CONST_DECL as is done in process_template_parm.
346 It is ugly that we recreate this here; the original
347 version built in process_template_parm is no longer
349 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
351 DECL_ARTIFICIAL (decl
) = 1;
352 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
353 SET_DECL_TEMPLATE_PARM_P (decl
);
364 /* Restore the template parameter context for a member template or
365 a friend template defined in a class definition. */
368 maybe_begin_member_template_processing (decl
)
374 if (inline_needs_template_parms (decl
))
376 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
377 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
379 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
382 parms
= TREE_CHAIN (parms
);
385 push_inline_template_parms_recursive (parms
, levels
);
388 /* Remember how many levels of template parameters we pushed so that
389 we can pop them later. */
390 if (!inline_parm_levels
)
391 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
392 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
393 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
394 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
395 ++inline_parm_levels_used
;
398 /* Undo the effects of begin_member_template_processing. */
401 maybe_end_member_template_processing ()
405 if (!inline_parm_levels_used
)
408 --inline_parm_levels_used
;
410 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
413 --processing_template_decl
;
414 current_template_parms
= TREE_CHAIN (current_template_parms
);
419 /* Returns non-zero iff T is a member template function. We must be
422 template <class T> class C { void f(); }
424 Here, f is a template function, and a member, but not a member
425 template. This function does not concern itself with the origin of
426 T, only its present state. So if we have
428 template <class T> class C { template <class U> void f(U); }
430 then neither C<int>::f<char> nor C<T>::f<double> is considered
431 to be a member template. But, `template <class U> void
432 C<int>::f(U)' is considered a member template. */
435 is_member_template (t
)
438 if (!DECL_FUNCTION_TEMPLATE_P (t
))
439 /* Anything that isn't a function or a template function is
440 certainly not a member template. */
443 /* A local class can't have member templates. */
444 if (decl_function_context (t
))
447 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
448 /* If there are more levels of template parameters than
449 there are template classes surrounding the declaration,
450 then we have a member template. */
451 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
452 template_class_depth (DECL_CONTEXT (t
))));
456 /* Returns non-zero iff T is a member template class. See
457 is_member_template for a description of what precisely constitutes
458 a member template. */
461 is_member_template_class (t
)
464 if (!DECL_CLASS_TEMPLATE_P (t
))
465 /* Anything that isn't a class template, is certainly not a member
469 if (!DECL_CLASS_SCOPE_P (t
))
470 /* Anything whose context isn't a class type is surely not a
474 /* If there are more levels of template parameters than there are
475 template classes surrounding the declaration, then we have a
477 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
478 template_class_depth (DECL_CONTEXT (t
)));
482 /* Return a new template argument vector which contains all of ARGS,
483 but has as its innermost set of arguments the EXTRA_ARGS. */
486 add_to_template_args (args
, extra_args
)
495 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
496 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
498 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
499 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
501 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
502 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
507 /* Like add_to_template_args, but only the outermost ARGS are added to
508 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
509 (EXTRA_ARGS) levels are added. This function is used to combine
510 the template arguments from a partial instantiation with the
511 template arguments used to attain the full instantiation from the
512 partial instantiation. */
515 add_outermost_template_args (args
, extra_args
)
521 /* If there are more levels of EXTRA_ARGS than there are ARGS,
522 something very fishy is going on. */
523 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
526 /* If *all* the new arguments will be the EXTRA_ARGS, just return
528 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
531 /* For the moment, we make ARGS look like it contains fewer levels. */
532 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
534 new_args
= add_to_template_args (args
, extra_args
);
536 /* Now, we restore ARGS to its full dimensions. */
537 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
542 /* Return the N levels of innermost template arguments from the ARGS. */
545 get_innermost_template_args (args
, n
)
553 my_friendly_assert (n
>= 0, 20000603);
555 /* If N is 1, just return the innermost set of template arguments. */
557 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
559 /* If we're not removing anything, just return the arguments we were
561 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
562 my_friendly_assert (extra_levels
>= 0, 20000603);
563 if (extra_levels
== 0)
566 /* Make a new set of arguments, not containing the outer arguments. */
567 new_args
= make_tree_vec (n
);
568 for (i
= 1; i
<= n
; ++i
)
569 SET_TMPL_ARGS_LEVEL (new_args
, i
,
570 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
575 /* We've got a template header coming up; push to a new level for storing
579 begin_template_parm_list ()
581 /* We use a non-tag-transparent scope here, which causes pushtag to
582 put tags in this scope, rather than in the enclosing class or
583 namespace scope. This is the right thing, since we want
584 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
585 global template class, push_template_decl handles putting the
586 TEMPLATE_DECL into top-level scope. For a nested template class,
589 template <class T> struct S1 {
590 template <class T> struct S2 {};
593 pushtag contains special code to call pushdecl_with_scope on the
594 TEMPLATE_DECL for S2. */
595 begin_scope (sk_template_parms
);
596 ++processing_template_decl
;
597 ++processing_template_parmlist
;
598 note_template_header (0);
601 /* This routine is called when a specialization is declared. If it is
602 illegal to declare a specialization here, an error is reported. */
605 check_specialization_scope ()
607 tree scope
= current_scope ();
611 An explicit specialization shall be declared in the namespace of
612 which the template is a member, or, for member templates, in the
613 namespace of which the enclosing class or enclosing class
614 template is a member. An explicit specialization of a member
615 function, member class or static data member of a class template
616 shall be declared in the namespace of which the class template
618 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
619 error ("explicit specialization in non-namespace scope `%D'",
624 In an explicit specialization declaration for a member of a class
625 template or a member template that appears in namespace scope,
626 the member template and some of its enclosing class templates may
627 remain unspecialized, except that the declaration shall not
628 explicitly specialize a class member template if its enclosing
629 class templates are not explicitly specialized as well. */
630 if (current_template_parms
)
631 error ("enclosing class templates are not explicitly specialized");
634 /* We've just seen template <>. */
637 begin_specialization ()
639 begin_scope (sk_template_spec
);
640 note_template_header (1);
641 check_specialization_scope ();
644 /* Called at then end of processing a declaration preceded by
648 end_specialization ()
651 reset_specialization ();
654 /* Any template <>'s that we have seen thus far are not referring to a
655 function specialization. */
658 reset_specialization ()
660 processing_specialization
= 0;
661 template_header_count
= 0;
664 /* We've just seen a template header. If SPECIALIZATION is non-zero,
665 it was of the form template <>. */
668 note_template_header (specialization
)
671 processing_specialization
= specialization
;
672 template_header_count
++;
675 /* We're beginning an explicit instantiation. */
678 begin_explicit_instantiation ()
680 ++processing_explicit_instantiation
;
685 end_explicit_instantiation ()
687 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
688 --processing_explicit_instantiation
;
691 /* The TYPE is being declared. If it is a template type, that means it
692 is a partial specialization. Do appropriate error-checking. */
695 maybe_process_partial_specialization (type
)
698 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
700 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
701 && !COMPLETE_TYPE_P (type
))
703 if (current_namespace
704 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
706 pedwarn ("specializing `%#T' in different namespace", type
);
707 cp_pedwarn_at (" from definition of `%#D'",
708 CLASSTYPE_TI_TEMPLATE (type
));
710 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
711 if (processing_template_decl
)
712 push_template_decl (TYPE_MAIN_DECL (type
));
714 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
715 error ("specialization of `%T' after instantiation", type
);
717 else if (processing_specialization
)
718 error ("explicit specialization of non-template `%T'", type
);
721 /* Retrieve the specialization (in the sense of [temp.spec] - a
722 specialization is either an instantiation or an explicit
723 specialization) of TMPL for the given template ARGS. If there is
724 no such specialization, return NULL_TREE. The ARGS are a vector of
725 arguments, or a vector of vectors of arguments, in the case of
726 templates with more than one level of parameters. */
729 retrieve_specialization (tmpl
, args
)
735 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
737 /* There should be as many levels of arguments as there are
738 levels of parameters. */
739 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
740 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
743 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
746 if (comp_template_args (TREE_PURPOSE (s
), args
))
747 return TREE_VALUE (s
);
752 /* Like retrieve_specialization, but for local declarations. */
755 retrieve_local_specialization (tmpl
)
758 return (tree
) htab_find (local_specializations
, tmpl
);
761 /* Returns non-zero iff DECL is a specialization of TMPL. */
764 is_specialization_of (decl
, tmpl
)
770 if (TREE_CODE (decl
) == FUNCTION_DECL
)
774 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
780 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
782 for (t
= TREE_TYPE (decl
);
784 t
= CLASSTYPE_USE_TEMPLATE (t
)
785 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
786 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
793 /* Register the specialization SPEC as a specialization of TMPL with
794 the indicated ARGS. Returns SPEC, or an equivalent prior
795 declaration, if available. */
798 register_specialization (spec
, tmpl
, args
)
805 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
807 if (TREE_CODE (spec
) == FUNCTION_DECL
808 && uses_template_parms (DECL_TI_ARGS (spec
)))
809 /* This is the FUNCTION_DECL for a partial instantiation. Don't
810 register it; we want the corresponding TEMPLATE_DECL instead.
811 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
812 the more obvious `uses_template_parms (spec)' to avoid problems
813 with default function arguments. In particular, given
816 template <class T> void f(T t1, T t = T())
818 the default argument expression is not substituted for in an
819 instantiation unless and until it is actually needed. */
822 /* There should be as many levels of arguments as there are
823 levels of parameters. */
824 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
825 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
828 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
832 tree fn
= TREE_VALUE (s
);
834 /* We can sometimes try to re-register a specialization that we've
835 already got. In particular, regenerate_decl_from_template
836 calls duplicate_decls which will update the specialization
837 list. But, we'll still get called again here anyhow. It's
838 more convenient to simply allow this than to try to prevent it. */
841 else if (comp_template_args (TREE_PURPOSE (s
), args
))
843 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
845 if (DECL_TEMPLATE_INSTANTIATION (fn
))
848 || DECL_EXPLICIT_INSTANTIATION (fn
))
850 error ("specialization of %D after instantiation",
856 /* This situation should occur only if the first
857 specialization is an implicit instantiation,
858 the second is an explicit specialization, and
859 the implicit instantiation has not yet been
860 used. That situation can occur if we have
861 implicitly instantiated a member function and
862 then specialized it later.
864 We can also wind up here if a friend
865 declaration that looked like an instantiation
866 turns out to be a specialization:
868 template <class T> void foo(T);
869 class S { friend void foo<>(int) };
870 template <> void foo(int);
872 We transform the existing DECL in place so that
873 any pointers to it become pointers to the
876 If there was a definition for the template, but
877 not for the specialization, we want this to
878 look as if there is no definition, and vice
880 DECL_INITIAL (fn
) = NULL_TREE
;
881 duplicate_decls (spec
, fn
);
886 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
888 duplicate_decls (spec
, fn
);
895 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
896 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
901 /* Unregister the specialization SPEC as a specialization of TMPL.
902 Returns nonzero if the SPEC was listed as a specialization of
906 unregister_specialization (spec
, tmpl
)
912 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
914 s
= &TREE_CHAIN (*s
))
915 if (TREE_VALUE (*s
) == spec
)
917 *s
= TREE_CHAIN (*s
);
924 /* Like register_specialization, but for local declarations. We are
925 registering SPEC, an instantiation of TMPL. */
928 register_local_specialization (spec
, tmpl
)
934 slot
= htab_find_slot (local_specializations
, tmpl
, INSERT
);
938 /* Print the list of candidate FNS in an error message. */
941 print_candidates (fns
)
946 const char *str
= "candidates are:";
948 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
952 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
953 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
958 /* Returns the template (one of the functions given by TEMPLATE_ID)
959 which can be specialized to match the indicated DECL with the
960 explicit template args given in TEMPLATE_ID. The DECL may be
961 NULL_TREE if none is available. In that case, the functions in
962 TEMPLATE_ID are non-members.
964 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
965 specialization of a member template.
967 The template args (those explicitly specified and those deduced)
968 are output in a newly created vector *TARGS_OUT.
970 If it is impossible to determine the result, an error message is
971 issued. The error_mark_node is returned to indicate failure. */
974 determine_specialization (template_id
, decl
, targs_out
,
975 need_member_template
)
979 int need_member_template
;
984 tree candidates
= NULL_TREE
;
985 tree templates
= NULL_TREE
;
987 *targs_out
= NULL_TREE
;
989 if (template_id
== error_mark_node
)
990 return error_mark_node
;
992 fns
= TREE_OPERAND (template_id
, 0);
993 explicit_targs
= TREE_OPERAND (template_id
, 1);
995 if (fns
== error_mark_node
)
996 return error_mark_node
;
998 /* Check for baselinks. */
999 if (BASELINK_P (fns
))
1000 fns
= TREE_VALUE (fns
);
1002 if (!is_overloaded_fn (fns
))
1004 error ("`%D' is not a function template", fns
);
1005 return error_mark_node
;
1008 for (; fns
; fns
= OVL_NEXT (fns
))
1012 tree fn
= OVL_CURRENT (fns
);
1014 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1015 /* DECL might be a specialization of FN. */
1017 else if (need_member_template
)
1018 /* FN is an ordinary member function, and we need a
1019 specialization of a member template. */
1021 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1022 /* We can get IDENTIFIER_NODEs here in certain erroneous
1025 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1026 /* This is just an ordinary non-member function. Nothing can
1027 be a specialization of that. */
1029 else if (DECL_ARTIFICIAL (fn
))
1030 /* Cannot specialize functions that are created implicitly. */
1034 tree decl_arg_types
;
1036 /* This is an ordinary member function. However, since
1037 we're here, we can assume it's enclosing class is a
1038 template class. For example,
1040 template <typename T> struct S { void f(); };
1041 template <> void S<int>::f() {}
1043 Here, S<int>::f is a non-template, but S<int> is a
1044 template class. If FN has the same type as DECL, we
1045 might be in business. */
1047 if (!DECL_TEMPLATE_INFO (fn
))
1048 /* Its enclosing class is an explicit specialization
1049 of a template class. This is not a candidate. */
1052 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1053 TREE_TYPE (TREE_TYPE (fn
))))
1054 /* The return types differ. */
1057 /* Adjust the type of DECL in case FN is a static member. */
1058 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1059 if (DECL_STATIC_FUNCTION_P (fn
)
1060 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1061 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1063 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1066 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1071 /* See whether this function might be a specialization of this
1073 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1076 /* We cannot deduce template arguments that when used to
1077 specialize TMPL will produce DECL. */
1080 /* Save this template, and the arguments deduced. */
1081 templates
= tree_cons (targs
, tmpl
, templates
);
1084 if (templates
&& TREE_CHAIN (templates
))
1090 It is possible for a specialization with a given function
1091 signature to be instantiated from more than one function
1092 template. In such cases, explicit specification of the
1093 template arguments must be used to uniquely identify the
1094 function template specialization being specialized.
1096 Note that here, there's no suggestion that we're supposed to
1097 determine which of the candidate templates is most
1098 specialized. However, we, also have:
1102 Partial ordering of overloaded function template
1103 declarations is used in the following contexts to select
1104 the function template to which a function template
1105 specialization refers:
1107 -- when an explicit specialization refers to a function
1110 So, we do use the partial ordering rules, at least for now.
1111 This extension can only serve to make illegal programs legal,
1112 so it's safe. And, there is strong anecdotal evidence that
1113 the committee intended the partial ordering rules to apply;
1114 the EDG front-end has that behavior, and John Spicer claims
1115 that the committee simply forgot to delete the wording in
1116 [temp.expl.spec]. */
1117 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1118 if (tmpl
&& tmpl
!= error_mark_node
)
1120 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1121 templates
= tree_cons (targs
, tmpl
, NULL_TREE
);
1125 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1127 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1129 return error_mark_node
;
1131 else if ((templates
&& TREE_CHAIN (templates
))
1132 || (candidates
&& TREE_CHAIN (candidates
))
1133 || (templates
&& candidates
))
1135 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1137 chainon (candidates
, templates
);
1138 print_candidates (candidates
);
1139 return error_mark_node
;
1142 /* We have one, and exactly one, match. */
1145 /* It was a specialization of an ordinary member function in a
1147 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1148 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1151 /* It was a specialization of a template. */
1152 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1153 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1155 *targs_out
= copy_node (targs
);
1156 SET_TMPL_ARGS_LEVEL (*targs_out
,
1157 TMPL_ARGS_DEPTH (*targs_out
),
1158 TREE_PURPOSE (templates
));
1161 *targs_out
= TREE_PURPOSE (templates
);
1162 return TREE_VALUE (templates
);
1165 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1166 but with the default argument values filled in from those in the
1170 copy_default_args_to_explicit_spec_1 (spec_types
,
1175 tree new_spec_types
;
1180 if (spec_types
== void_list_node
)
1181 return void_list_node
;
1183 /* Substitute into the rest of the list. */
1185 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1186 TREE_CHAIN (tmpl_types
));
1188 /* Add the default argument for this parameter. */
1189 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1190 TREE_VALUE (spec_types
),
1194 /* DECL is an explicit specialization. Replicate default arguments
1195 from the template it specializes. (That way, code like:
1197 template <class T> void f(T = 3);
1198 template <> void f(double);
1201 works, as required.) An alternative approach would be to look up
1202 the correct default arguments at the call-site, but this approach
1203 is consistent with how implicit instantiations are handled. */
1206 copy_default_args_to_explicit_spec (decl
)
1212 tree new_spec_types
;
1216 tree object_type
= NULL_TREE
;
1217 tree in_charge
= NULL_TREE
;
1218 tree vtt
= NULL_TREE
;
1220 /* See if there's anything we need to do. */
1221 tmpl
= DECL_TI_TEMPLATE (decl
);
1222 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1223 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1224 if (TREE_PURPOSE (t
))
1229 old_type
= TREE_TYPE (decl
);
1230 spec_types
= TYPE_ARG_TYPES (old_type
);
1232 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1234 /* Remove the this pointer, but remember the object's type for
1236 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1237 spec_types
= TREE_CHAIN (spec_types
);
1238 tmpl_types
= TREE_CHAIN (tmpl_types
);
1240 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1242 /* DECL may contain more parameters than TMPL due to the extra
1243 in-charge parameter in constructors and destructors. */
1244 in_charge
= spec_types
;
1245 spec_types
= TREE_CHAIN (spec_types
);
1247 if (DECL_HAS_VTT_PARM_P (decl
))
1250 spec_types
= TREE_CHAIN (spec_types
);
1254 /* Compute the merged default arguments. */
1256 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1258 /* Compute the new FUNCTION_TYPE. */
1262 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1267 /* Put the in-charge parameter back. */
1268 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1269 TREE_VALUE (in_charge
),
1272 new_type
= build_cplus_method_type (object_type
,
1273 TREE_TYPE (old_type
),
1277 new_type
= build_function_type (TREE_TYPE (old_type
),
1279 new_type
= build_type_attribute_variant (new_type
,
1280 TYPE_ATTRIBUTES (old_type
));
1281 new_type
= build_exception_variant (new_type
,
1282 TYPE_RAISES_EXCEPTIONS (old_type
));
1283 TREE_TYPE (decl
) = new_type
;
1286 /* Check to see if the function just declared, as indicated in
1287 DECLARATOR, and in DECL, is a specialization of a function
1288 template. We may also discover that the declaration is an explicit
1289 instantiation at this point.
1291 Returns DECL, or an equivalent declaration that should be used
1292 instead if all goes well. Issues an error message if something is
1293 amiss. Returns error_mark_node if the error is not easily
1296 FLAGS is a bitmask consisting of the following flags:
1298 2: The function has a definition.
1299 4: The function is a friend.
1301 The TEMPLATE_COUNT is the number of references to qualifying
1302 template classes that appeared in the name of the function. For
1305 template <class T> struct S { void f(); };
1308 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1309 classes are not counted in the TEMPLATE_COUNT, so that in
1311 template <class T> struct S {};
1312 template <> struct S<int> { void f(); }
1313 template <> void S<int>::f();
1315 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1316 illegal; there should be no template <>.)
1318 If the function is a specialization, it is marked as such via
1319 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1320 is set up correctly, and it is added to the list of specializations
1321 for that template. */
1324 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1330 int have_def
= flags
& 2;
1331 int is_friend
= flags
& 4;
1332 int specialization
= 0;
1333 int explicit_instantiation
= 0;
1334 int member_specialization
= 0;
1335 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1336 tree dname
= DECL_NAME (decl
);
1339 tsk
= current_tmpl_spec_kind (template_count
);
1344 if (processing_specialization
)
1347 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1349 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1352 /* This could be something like:
1354 template <class T> void f(T);
1355 class S { friend void f<>(int); } */
1359 /* This case handles bogus declarations like template <>
1360 template <class T> void f<int>(); */
1362 error ("template-id `%D' in declaration of primary template",
1369 case tsk_invalid_member_spec
:
1370 /* The error has already been reported in
1371 check_specialization_scope. */
1372 return error_mark_node
;
1374 case tsk_invalid_expl_inst
:
1375 error ("template parameter list used in explicit instantiation");
1381 error ("definition provided for explicit instantiation");
1383 explicit_instantiation
= 1;
1386 case tsk_excessive_parms
:
1387 error ("too many template parameter lists in declaration of `%D'",
1389 return error_mark_node
;
1393 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1395 member_specialization
= 1;
1400 case tsk_insufficient_parms
:
1401 if (template_header_count
)
1403 error("too few template parameter lists in declaration of `%D'",
1407 else if (ctype
!= NULL_TREE
1408 && !TYPE_BEING_DEFINED (ctype
)
1409 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1412 /* For backwards compatibility, we accept:
1414 template <class T> struct S { void f(); };
1415 void S<int>::f() {} // Missing template <>
1417 That used to be legal C++. */
1420 ("explicit specialization not preceded by `template <>'");
1422 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1427 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1429 /* This case handles bogus declarations like template <>
1430 template <class T> void f<int>(); */
1432 if (uses_template_parms (declarator
))
1433 error ("partial specialization `%D' of function template",
1436 error ("template-id `%D' in declaration of primary template",
1441 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1442 /* This is a specialization of a member template, without
1443 specialization the containing class. Something like:
1445 template <class T> struct S {
1446 template <class U> void f (U);
1448 template <> template <class U> void S<int>::f(U) {}
1450 That's a specialization -- but of the entire template. */
1458 if (specialization
|| member_specialization
)
1460 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1461 for (; t
; t
= TREE_CHAIN (t
))
1462 if (TREE_PURPOSE (t
))
1465 ("default argument specified in explicit specialization");
1468 if (current_lang_name
== lang_name_c
)
1469 error ("template specialization with C linkage");
1472 if (specialization
|| member_specialization
|| explicit_instantiation
)
1474 tree tmpl
= NULL_TREE
;
1475 tree targs
= NULL_TREE
;
1477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1478 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1482 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1485 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1490 lookup_template_function (fns
, NULL_TREE
);
1493 if (declarator
== error_mark_node
)
1494 return error_mark_node
;
1496 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1498 if (!explicit_instantiation
)
1499 /* A specialization in class scope. This is illegal,
1500 but the error will already have been flagged by
1501 check_specialization_scope. */
1502 return error_mark_node
;
1505 /* It's not legal to write an explicit instantiation in
1508 class C { template void f(); }
1510 This case is caught by the parser. However, on
1513 template class C { void f(); };
1515 (which is illegal) we can get here. The error will be
1522 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1524 /* A friend declaration. We can't do much, because we don't
1525 know what this resolves to, yet. */
1526 my_friendly_assert (is_friend
!= 0, 0);
1527 my_friendly_assert (!explicit_instantiation
, 0);
1528 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1531 else if (ctype
!= NULL_TREE
1532 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1535 /* Find the list of functions in ctype that have the same
1536 name as the declared function. */
1537 tree name
= TREE_OPERAND (declarator
, 0);
1538 tree fns
= NULL_TREE
;
1541 if (name
== constructor_name (ctype
)
1542 || name
== constructor_name_full (ctype
))
1544 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1546 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1547 : !TYPE_HAS_DESTRUCTOR (ctype
))
1549 /* From [temp.expl.spec]:
1551 If such an explicit specialization for the member
1552 of a class template names an implicitly-declared
1553 special member function (clause _special_), the
1554 program is ill-formed.
1556 Similar language is found in [temp.explicit]. */
1557 error ("specialization of implicitly-declared special member function");
1558 return error_mark_node
;
1561 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1564 if (!DECL_CONV_FN_P (decl
))
1566 idx
= lookup_fnfields_1 (ctype
, name
);
1568 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1574 /* For a type-conversion operator, we cannot do a
1575 name-based lookup. We might be looking for `operator
1576 int' which will be a specialization of `operator T'.
1577 So, we find *all* the conversion operators, and then
1578 select from them. */
1581 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1583 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1585 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1587 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1588 /* There are no more conversion functions. */
1591 /* Glue all these conversion functions together
1592 with those we already have. */
1593 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1594 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1598 if (fns
== NULL_TREE
)
1600 error ("no member function `%D' declared in `%T'",
1602 return error_mark_node
;
1605 TREE_OPERAND (declarator
, 0) = fns
;
1608 /* Figure out what exactly is being specialized at this point.
1609 Note that for an explicit instantiation, even one for a
1610 member function, we cannot tell apriori whether the
1611 instantiation is for a member template, or just a member
1612 function of a template class. Even if a member template is
1613 being instantiated, the member template arguments may be
1614 elided if they can be deduced from the rest of the
1616 tmpl
= determine_specialization (declarator
, decl
,
1618 member_specialization
);
1620 if (!tmpl
|| tmpl
== error_mark_node
)
1621 /* We couldn't figure out what this declaration was
1623 return error_mark_node
;
1626 tree gen_tmpl
= most_general_template (tmpl
);
1628 if (explicit_instantiation
)
1630 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1631 is done by do_decl_instantiation later. */
1633 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1634 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1636 if (arg_depth
> parm_depth
)
1638 /* If TMPL is not the most general template (for
1639 example, if TMPL is a friend template that is
1640 injected into namespace scope), then there will
1641 be too many levels of TARGS. Remove some of them
1646 new_targs
= make_tree_vec (parm_depth
);
1647 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1648 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1649 = TREE_VEC_ELT (targs
, i
);
1653 return instantiate_template (tmpl
, targs
);
1656 /* If this is a specialization of a member template of a
1657 template class. In we want to return the TEMPLATE_DECL,
1658 not the specialization of it. */
1659 if (tsk
== tsk_template
)
1661 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1662 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
1666 /* If we thought that the DECL was a member function, but it
1667 turns out to be specializing a static member function,
1668 make DECL a static member function as well. */
1669 if (DECL_STATIC_FUNCTION_P (tmpl
)
1670 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1672 revert_static_member_fn (decl
);
1673 last_function_parms
= TREE_CHAIN (last_function_parms
);
1676 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1677 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
1679 /* Inherit default function arguments from the template
1680 DECL is specializing. */
1681 copy_default_args_to_explicit_spec (decl
);
1683 /* This specialization has the same protection as the
1684 template it specializes. */
1685 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
1686 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
1688 if (is_friend
&& !have_def
)
1689 /* This is not really a declaration of a specialization.
1690 It's just the name of an instantiation. But, it's not
1691 a request for an instantiation, either. */
1692 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1693 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
1694 /* This is indeed a specialization. In case of constructors
1695 and destructors, we need in-charge and not-in-charge
1696 versions in V3 ABI. */
1697 clone_function_decl (decl
, /*update_method_vec_p=*/0);
1699 /* Register this specialization so that we can find it
1701 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1708 /* TYPE is being declared. Verify that the use of template headers
1709 and such is reasonable. Issue error messages if not. */
1712 maybe_check_template_type (type
)
1715 if (template_header_count
)
1717 /* We are in the scope of some `template <...>' header. */
1720 = template_class_depth_real (TYPE_CONTEXT (type
),
1721 /*count_specializations=*/1);
1723 if (template_header_count
<= context_depth
)
1724 /* This is OK; the template headers are for the context. We
1725 are actually too lenient here; like
1726 check_explicit_specialization we should consider the number
1727 of template types included in the actual declaration. For
1730 template <class T> struct S {
1731 template <class U> template <class V>
1737 template <class T> struct S {
1738 template <class U> struct I;
1741 template <class T> template <class U.
1746 else if (template_header_count
> context_depth
+ 1)
1747 /* There are two many template parameter lists. */
1748 error ("too many template parameter lists in declaration of `%T'", type
);
1752 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1753 parameters. These are represented in the same format used for
1754 DECL_TEMPLATE_PARMS. */
1756 int comp_template_parms (parms1
, parms2
)
1763 if (parms1
== parms2
)
1766 for (p1
= parms1
, p2
= parms2
;
1767 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1768 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1770 tree t1
= TREE_VALUE (p1
);
1771 tree t2
= TREE_VALUE (p2
);
1774 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1775 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1777 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1780 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1782 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1783 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1785 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1788 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1790 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1795 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1796 /* One set of parameters has more parameters lists than the
1803 /* Complain if DECL shadows a template parameter.
1805 [temp.local]: A template-parameter shall not be redeclared within its
1806 scope (including nested scopes). */
1809 check_template_shadow (decl
)
1814 /* If we're not in a template, we can't possibly shadow a template
1816 if (!current_template_parms
)
1819 /* Figure out what we're shadowing. */
1820 if (TREE_CODE (decl
) == OVERLOAD
)
1821 decl
= OVL_CURRENT (decl
);
1822 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1824 /* If there's no previous binding for this name, we're not shadowing
1825 anything, let alone a template parameter. */
1829 /* If we're not shadowing a template parameter, we're done. Note
1830 that OLDDECL might be an OVERLOAD (or perhaps even an
1831 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1833 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
1836 /* We check for decl != olddecl to avoid bogus errors for using a
1837 name inside a class. We check TPFI to avoid duplicate errors for
1838 inline member templates. */
1840 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1843 cp_error_at ("declaration of `%#D'", decl
);
1844 cp_error_at (" shadows template parm `%#D'", olddecl
);
1847 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1848 ORIG_LEVEL, DECL, and TYPE. */
1851 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1858 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1859 TEMPLATE_PARM_IDX (t
) = index
;
1860 TEMPLATE_PARM_LEVEL (t
) = level
;
1861 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1862 TEMPLATE_PARM_DECL (t
) = decl
;
1863 TREE_TYPE (t
) = type
;
1868 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1869 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1870 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1871 new one is created. */
1874 reduce_template_parm_level (index
, type
, levels
)
1879 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1880 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1881 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1884 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1885 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1888 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1889 TEMPLATE_PARM_LEVEL (index
) - levels
,
1890 TEMPLATE_PARM_ORIG_LEVEL (index
),
1892 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1894 DECL_ARTIFICIAL (decl
) = 1;
1895 SET_DECL_TEMPLATE_PARM_P (decl
);
1897 /* Template template parameters need this. */
1898 DECL_TEMPLATE_PARMS (decl
)
1899 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1902 return TEMPLATE_PARM_DESCENDANTS (index
);
1905 /* Process information from new template parameter NEXT and append it to the
1906 LIST being built. */
1909 process_template_parm (list
, next
)
1918 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1919 defval
= TREE_PURPOSE (parm
);
1920 parm
= TREE_VALUE (parm
);
1921 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1925 tree p
= TREE_VALUE (tree_last (list
));
1927 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
1928 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1930 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1938 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1939 /* is a const-param */
1940 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1942 SET_DECL_TEMPLATE_PARM_P (parm
);
1946 The top-level cv-qualifiers on the template-parameter are
1947 ignored when determining its type. */
1948 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1950 /* A template parameter is not modifiable. */
1951 TREE_READONLY (parm
) = 1;
1952 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
1953 TREE_TYPE (parm
) = void_type_node
;
1954 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1955 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1956 = build_template_parm_index (idx
, processing_template_decl
,
1957 processing_template_decl
,
1958 decl
, TREE_TYPE (parm
));
1963 parm
= TREE_VALUE (parm
);
1965 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1967 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
1968 /* This is for distinguishing between real templates and template
1969 template parameters */
1970 TREE_TYPE (parm
) = t
;
1971 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1976 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
1977 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1978 decl
= build_decl (TYPE_DECL
, parm
, t
);
1981 TYPE_NAME (t
) = decl
;
1982 TYPE_STUB_DECL (t
) = decl
;
1984 TEMPLATE_TYPE_PARM_INDEX (t
)
1985 = build_template_parm_index (idx
, processing_template_decl
,
1986 processing_template_decl
,
1987 decl
, TREE_TYPE (parm
));
1989 DECL_ARTIFICIAL (decl
) = 1;
1990 SET_DECL_TEMPLATE_PARM_P (decl
);
1992 parm
= build_tree_list (defval
, parm
);
1993 return chainon (list
, parm
);
1996 /* The end of a template parameter list has been reached. Process the
1997 tree list into a parameter vector, converting each parameter into a more
1998 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2002 end_template_parm_list (parms
)
2007 tree saved_parmlist
= make_tree_vec (list_length (parms
));
2009 current_template_parms
2010 = tree_cons (size_int (processing_template_decl
),
2011 saved_parmlist
, current_template_parms
);
2013 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
2015 next
= TREE_CHAIN (parm
);
2016 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
2017 TREE_CHAIN (parm
) = NULL_TREE
;
2020 --processing_template_parmlist
;
2022 return saved_parmlist
;
2025 /* end_template_decl is called after a template declaration is seen. */
2028 end_template_decl ()
2030 reset_specialization ();
2032 if (! processing_template_decl
)
2035 /* This matches the pushlevel in begin_template_parm_list. */
2038 --processing_template_decl
;
2039 current_template_parms
= TREE_CHAIN (current_template_parms
);
2042 /* Given a template argument vector containing the template PARMS.
2043 The innermost PARMS are given first. */
2046 current_template_args ()
2049 tree args
= NULL_TREE
;
2050 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
2053 /* If there is only one level of template parameters, we do not
2054 create a TREE_VEC of TREE_VECs. Instead, we return a single
2055 TREE_VEC containing the arguments. */
2057 args
= make_tree_vec (length
);
2059 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
2061 tree a
= copy_node (TREE_VALUE (header
));
2064 TREE_TYPE (a
) = NULL_TREE
;
2065 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
2067 tree t
= TREE_VEC_ELT (a
, i
);
2069 /* T will be a list if we are called from within a
2070 begin/end_template_parm_list pair, but a vector directly
2071 if within a begin/end_member_template_processing pair. */
2072 if (TREE_CODE (t
) == TREE_LIST
)
2076 if (TREE_CODE (t
) == TYPE_DECL
2077 || TREE_CODE (t
) == TEMPLATE_DECL
)
2080 t
= DECL_INITIAL (t
);
2081 TREE_VEC_ELT (a
, i
) = t
;
2086 TREE_VEC_ELT (args
, --l
) = a
;
2094 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2095 template PARMS. Used by push_template_decl below. */
2098 build_template_decl (decl
, parms
)
2102 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
2103 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
2104 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
2105 if (DECL_LANG_SPECIFIC (decl
))
2107 if (CAN_HAVE_FULL_LANG_DECL_P (decl
))
2108 DECL_VIRTUAL_CONTEXT (tmpl
) = DECL_VIRTUAL_CONTEXT (decl
);
2109 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
2110 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
2111 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2112 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
2113 if (DECL_OVERLOADED_OPERATOR_P (decl
))
2114 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
2115 DECL_OVERLOADED_OPERATOR_P (decl
));
2121 struct template_parm_data
2123 /* The level of the template parameters we are currently
2127 /* The index of the specialization argument we are currently
2131 /* An array whose size is the number of template parameters. The
2132 elements are non-zero if the parameter has been used in any one
2133 of the arguments processed so far. */
2136 /* An array whose size is the number of template arguments. The
2137 elements are non-zero if the argument makes use of template
2138 parameters of this level. */
2139 int* arg_uses_template_parms
;
2142 /* Subroutine of push_template_decl used to see if each template
2143 parameter in a partial specialization is used in the explicit
2144 argument list. If T is of the LEVEL given in DATA (which is
2145 treated as a template_parm_data*), then DATA->PARMS is marked
2149 mark_template_parm (t
, data
)
2155 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2157 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2159 level
= TEMPLATE_PARM_LEVEL (t
);
2160 idx
= TEMPLATE_PARM_IDX (t
);
2164 level
= TEMPLATE_TYPE_LEVEL (t
);
2165 idx
= TEMPLATE_TYPE_IDX (t
);
2168 if (level
== tpd
->level
)
2170 tpd
->parms
[idx
] = 1;
2171 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2174 /* Return zero so that for_each_template_parm will continue the
2175 traversal of the tree; we want to mark *every* template parm. */
2179 /* Process the partial specialization DECL. */
2182 process_partial_specialization (decl
)
2185 tree type
= TREE_TYPE (decl
);
2186 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2187 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2188 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
2189 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2190 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2191 int nargs
= TREE_VEC_LENGTH (inner_args
);
2192 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2194 int did_error_intro
= 0;
2195 struct template_parm_data tpd
;
2196 struct template_parm_data tpd2
;
2198 /* We check that each of the template parameters given in the
2199 partial specialization is used in the argument list to the
2200 specialization. For example:
2202 template <class T> struct S;
2203 template <class T> struct S<T*>;
2205 The second declaration is OK because `T*' uses the template
2206 parameter T, whereas
2208 template <class T> struct S<int>;
2210 is no good. Even trickier is:
2221 The S2<T> declaration is actually illegal; it is a
2222 full-specialization. Of course,
2225 struct S2<T (*)(U)>;
2227 or some such would have been OK. */
2228 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2229 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2230 memset ((PTR
) tpd
.parms
, 0, sizeof (int) * ntparms
);
2232 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2233 memset ((PTR
) tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
2234 for (i
= 0; i
< nargs
; ++i
)
2236 tpd
.current_arg
= i
;
2237 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2238 &mark_template_parm
,
2241 for (i
= 0; i
< ntparms
; ++i
)
2242 if (tpd
.parms
[i
] == 0)
2244 /* One of the template parms was not used in the
2246 if (!did_error_intro
)
2248 error ("template parameters not used in partial specialization:");
2249 did_error_intro
= 1;
2253 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2256 /* [temp.class.spec]
2258 The argument list of the specialization shall not be identical to
2259 the implicit argument list of the primary template. */
2260 if (comp_template_args
2262 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2264 error ("partial specialization `%T' does not specialize any template arguments", type
);
2266 /* [temp.class.spec]
2268 A partially specialized non-type argument expression shall not
2269 involve template parameters of the partial specialization except
2270 when the argument expression is a simple identifier.
2272 The type of a template parameter corresponding to a specialized
2273 non-type argument shall not be dependent on a parameter of the
2275 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2277 for (i
= 0; i
< nargs
; ++i
)
2279 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2280 if (/* These first two lines are the `non-type' bit. */
2282 && TREE_CODE (arg
) != TEMPLATE_DECL
2283 /* This next line is the `argument expression is not just a
2284 simple identifier' condition and also the `specialized
2285 non-type argument' bit. */
2286 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2288 if (tpd
.arg_uses_template_parms
[i
])
2289 error ("template argument `%E' involves template parameter(s)", arg
);
2292 /* Look at the corresponding template parameter,
2293 marking which template parameters its type depends
2296 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2301 /* We haven't yet initialized TPD2. Do so now. */
2302 tpd2
.arg_uses_template_parms
2303 = (int*) alloca (sizeof (int) * nargs
);
2304 /* The number of parameters here is the number in the
2305 main template, which, as checked in the assertion
2307 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2309 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2312 /* Mark the template parameters. But this time, we're
2313 looking for the template parameters of the main
2314 template, not in the specialization. */
2315 tpd2
.current_arg
= i
;
2316 tpd2
.arg_uses_template_parms
[i
] = 0;
2317 memset ((PTR
) tpd2
.parms
, 0, sizeof (int) * nargs
);
2318 for_each_template_parm (type
,
2319 &mark_template_parm
,
2322 if (tpd2
.arg_uses_template_parms
[i
])
2324 /* The type depended on some template parameters.
2325 If they are fully specialized in the
2326 specialization, that's OK. */
2328 for (j
= 0; j
< nargs
; ++j
)
2329 if (tpd2
.parms
[j
] != 0
2330 && tpd
.arg_uses_template_parms
[j
])
2332 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2342 if (retrieve_specialization (maintmpl
, specargs
))
2343 /* We've already got this specialization. */
2346 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
2347 = tree_cons (inner_args
, inner_parms
,
2348 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2349 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2353 /* Check that a template declaration's use of default arguments is not
2354 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2355 non-zero if DECL is the thing declared by a primary template.
2356 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2359 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2366 int last_level_to_check
;
2371 A default template-argument shall not be specified in a
2372 function template declaration or a function template definition, nor
2373 in the template-parameter-list of the definition of a member of a
2376 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
2377 /* You can't have a function template declaration in a local
2378 scope, nor you can you define a member of a class template in a
2382 if (current_class_type
2383 && !TYPE_BEING_DEFINED (current_class_type
)
2384 && DECL_LANG_SPECIFIC (decl
)
2385 /* If this is either a friend defined in the scope of the class
2386 or a member function. */
2387 && ((DECL_CONTEXT (decl
)
2388 && same_type_p (DECL_CONTEXT (decl
), current_class_type
))
2389 || (DECL_FRIEND_CONTEXT (decl
)
2390 && same_type_p (DECL_FRIEND_CONTEXT (decl
),
2391 current_class_type
)))
2392 /* And, if it was a member function, it really was defined in
2393 the scope of the class. */
2394 && (!DECL_FUNCTION_MEMBER_P (decl
) || DECL_INITIALIZED_IN_CLASS_P (decl
)))
2395 /* We already checked these parameters when the template was
2396 declared, so there's no need to do it again now. This function
2397 was defined in class scope, but we're processing it's body now
2398 that the class is complete. */
2403 If a template-parameter has a default template-argument, all
2404 subsequent template-parameters shall have a default
2405 template-argument supplied. */
2406 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2408 tree inner_parms
= TREE_VALUE (parm_level
);
2409 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2410 int seen_def_arg_p
= 0;
2413 for (i
= 0; i
< ntparms
; ++i
)
2415 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2416 if (TREE_PURPOSE (parm
))
2418 else if (seen_def_arg_p
)
2420 error ("no default argument for `%D'", TREE_VALUE (parm
));
2421 /* For better subsequent error-recovery, we indicate that
2422 there should have been a default argument. */
2423 TREE_PURPOSE (parm
) = error_mark_node
;
2428 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2429 /* For an ordinary class template, default template arguments are
2430 allowed at the innermost level, e.g.:
2431 template <class T = int>
2433 but, in a partial specialization, they're not allowed even
2434 there, as we have in [temp.class.spec]:
2436 The template parameter list of a specialization shall not
2437 contain default template argument values.
2439 So, for a partial specialization, or for a function template,
2440 we look at all of them. */
2443 /* But, for a primary class template that is not a partial
2444 specialization we look at all template parameters except the
2446 parms
= TREE_CHAIN (parms
);
2448 /* Figure out what error message to issue. */
2449 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2450 msg
= "default template arguments may not be used in function templates";
2451 else if (is_partial
)
2452 msg
= "default template arguments may not be used in partial specializations";
2454 msg
= "default argument for template parameter for class enclosing `%D'";
2456 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2457 /* If we're inside a class definition, there's no need to
2458 examine the parameters to the class itself. On the one
2459 hand, they will be checked when the class is defined, and,
2460 on the other, default arguments are legal in things like:
2461 template <class T = double>
2462 struct S { template <class U> void f(U); };
2463 Here the default argument for `S' has no bearing on the
2464 declaration of `f'. */
2465 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2467 /* Check everything. */
2468 last_level_to_check
= 0;
2470 for (parm_level
= parms
;
2471 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2472 parm_level
= TREE_CHAIN (parm_level
))
2474 tree inner_parms
= TREE_VALUE (parm_level
);
2478 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2479 for (i
= 0; i
< ntparms
; ++i
)
2480 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2488 /* Clear out the default argument so that we are not
2490 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2493 /* At this point, if we're still interested in issuing messages,
2494 they must apply to classes surrounding the object declared. */
2496 msg
= "default argument for template parameter for class enclosing `%D'";
2500 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2501 parameters given by current_template_args, or reuses a
2502 previously existing one, if appropriate. Returns the DECL, or an
2503 equivalent one, if it is replaced via a call to duplicate_decls.
2505 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2508 push_template_decl_real (decl
, is_friend
)
2518 int new_template_p
= 0;
2520 /* See if this is a partial specialization. */
2521 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2522 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2523 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2525 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2528 /* For a friend, we want the context of the friend function, not
2529 the type of which it is a friend. */
2530 ctx
= DECL_CONTEXT (decl
);
2531 else if (CP_DECL_CONTEXT (decl
)
2532 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2533 /* In the case of a virtual function, we want the class in which
2535 ctx
= CP_DECL_CONTEXT (decl
);
2537 /* Otherwise, if we're currently defining some class, the DECL
2538 is assumed to be a member of the class. */
2539 ctx
= current_scope ();
2541 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2544 if (!DECL_CONTEXT (decl
))
2545 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2547 /* See if this is a primary template. */
2548 primary
= template_parm_scope_p ();
2552 if (current_lang_name
== lang_name_c
)
2553 error ("template with C linkage");
2554 else if (TREE_CODE (decl
) == TYPE_DECL
2555 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2556 error ("template class without a name");
2557 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2558 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2559 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2560 || TREE_CODE (decl
) == FUNCTION_DECL
)
2563 error ("template declaration of `%#D'", decl
);
2566 /* Check to see that the rules regarding the use of default
2567 arguments are not being violated. */
2568 check_default_tmpl_args (decl
, current_template_parms
,
2569 primary
, is_partial
);
2572 return process_partial_specialization (decl
);
2574 args
= current_template_args ();
2577 || TREE_CODE (ctx
) == FUNCTION_DECL
2578 || (TREE_CODE (ctx
) != TEMPLATE_TYPE_PARM
2579 && TREE_CODE (ctx
) != BOUND_TEMPLATE_TEMPLATE_PARM
2580 && TYPE_BEING_DEFINED (ctx
))
2581 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2583 if (DECL_LANG_SPECIFIC (decl
)
2584 && DECL_TEMPLATE_INFO (decl
)
2585 && DECL_TI_TEMPLATE (decl
))
2586 tmpl
= DECL_TI_TEMPLATE (decl
);
2587 /* If DECL is a TYPE_DECL for a class-template, then there won't
2588 be DECL_LANG_SPECIFIC. The information equivalent to
2589 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2590 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2591 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2592 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2594 /* Since a template declaration already existed for this
2595 class-type, we must be redeclaring it here. Make sure
2596 that the redeclaration is legal. */
2597 redeclare_class_template (TREE_TYPE (decl
),
2598 current_template_parms
);
2599 /* We don't need to create a new TEMPLATE_DECL; just use the
2600 one we already had. */
2601 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2605 tmpl
= build_template_decl (decl
, current_template_parms
);
2608 if (DECL_LANG_SPECIFIC (decl
)
2609 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2611 /* A specialization of a member template of a template
2613 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2614 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2615 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2621 tree a
, t
, current
, parms
;
2624 if (TREE_CODE (decl
) == TYPE_DECL
)
2626 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2627 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2628 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2629 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2630 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2633 error ("`%D' does not declare a template type", decl
);
2637 else if (!DECL_LANG_SPECIFIC (decl
) || !DECL_TEMPLATE_INFO (decl
))
2639 error ("template definition of non-template `%#D'", decl
);
2643 tmpl
= DECL_TI_TEMPLATE (decl
);
2645 if (is_member_template (tmpl
)
2646 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2647 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2648 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2652 /* The declaration is a specialization of a member
2653 template, declared outside the class. Therefore, the
2654 innermost template arguments will be NULL, so we
2655 replace them with the arguments determined by the
2656 earlier call to check_explicit_specialization. */
2657 args
= DECL_TI_ARGS (decl
);
2660 = build_template_decl (decl
, current_template_parms
);
2661 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2662 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2663 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2664 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2665 DECL_TEMPLATE_INFO (new_tmpl
)
2666 = tree_cons (tmpl
, args
, NULL_TREE
);
2668 register_specialization (new_tmpl
,
2669 most_general_template (tmpl
),
2674 /* Make sure the template headers we got make sense. */
2676 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2677 i
= TMPL_PARMS_DEPTH (parms
);
2678 if (TMPL_ARGS_DEPTH (args
) != i
)
2680 error ("expected %d levels of template parms for `%#D', got %d",
2681 i
, decl
, TMPL_ARGS_DEPTH (args
));
2684 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2686 a
= TMPL_ARGS_LEVEL (args
, i
);
2687 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2689 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2691 if (current
== decl
)
2692 error ("got %d template parameters for `%#D'",
2693 TREE_VEC_LENGTH (a
), decl
);
2695 error ("got %d template parameters for `%#T'",
2696 TREE_VEC_LENGTH (a
), current
);
2697 error (" but %d required", TREE_VEC_LENGTH (t
));
2700 /* Perhaps we should also check that the parms are used in the
2701 appropriate qualifying scopes in the declarator? */
2703 if (current
== decl
)
2706 current
= TYPE_CONTEXT (current
);
2710 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2711 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2713 /* Push template declarations for global functions and types. Note
2714 that we do not try to push a global template friend declared in a
2715 template class; such a thing may well depend on the template
2716 parameters of the class. */
2717 if (new_template_p
&& !ctx
2718 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2719 tmpl
= pushdecl_namespace_level (tmpl
);
2722 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2724 info
= tree_cons (tmpl
, args
, NULL_TREE
);
2726 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2728 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2729 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2730 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2731 /* Don't change the name if we've already set it up. */
2732 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl
)))
2733 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2735 else if (DECL_LANG_SPECIFIC (decl
))
2736 DECL_TEMPLATE_INFO (decl
) = info
;
2738 return DECL_TEMPLATE_RESULT (tmpl
);
2742 push_template_decl (decl
)
2745 return push_template_decl_real (decl
, 0);
2748 /* Called when a class template TYPE is redeclared with the indicated
2749 template PARMS, e.g.:
2751 template <class T> struct S;
2752 template <class T> struct S {}; */
2755 redeclare_class_template (type
, parms
)
2763 if (!TYPE_TEMPLATE_INFO (type
))
2765 error ("`%T' is not a template type", type
);
2769 tmpl
= TYPE_TI_TEMPLATE (type
);
2770 if (!PRIMARY_TEMPLATE_P (tmpl
))
2771 /* The type is nested in some template class. Nothing to worry
2772 about here; there are no new template parameters for the nested
2776 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2777 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2779 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2781 cp_error_at ("previous declaration `%D'", tmpl
);
2782 error ("used %d template parameter%s instead of %d",
2783 TREE_VEC_LENGTH (tmpl_parms
),
2784 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2785 TREE_VEC_LENGTH (parms
));
2789 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2791 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2792 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2793 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2794 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2796 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2798 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2799 error ("redeclared here as `%#D'", parm
);
2803 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2805 /* We have in [temp.param]:
2807 A template-parameter may not be given default arguments
2808 by two different declarations in the same scope. */
2809 error ("redefinition of default argument for `%#D'", parm
);
2810 cp_error_at (" original definition appeared here", tmpl_parm
);
2814 if (parm_default
!= NULL_TREE
)
2815 /* Update the previous template parameters (which are the ones
2816 that will really count) with the new default value. */
2817 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2818 else if (tmpl_default
!= NULL_TREE
)
2819 /* Update the new parameters, too; they'll be used as the
2820 parameters for any members. */
2821 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
2825 /* Attempt to convert the non-type template parameter EXPR to the
2826 indicated TYPE. If the conversion is successful, return the
2827 converted value. If the conversion is unsuccessful, return
2828 NULL_TREE if we issued an error message, or error_mark_node if we
2829 did not. We issue error messages for out-and-out bad template
2830 parameters, but not simply because the conversion failed, since we
2831 might be just trying to do argument deduction. By the time this
2832 function is called, neither TYPE nor EXPR may make use of template
2836 convert_nontype_argument (type
, expr
)
2840 tree expr_type
= TREE_TYPE (expr
);
2842 /* A template-argument for a non-type, non-template
2843 template-parameter shall be one of:
2845 --an integral constant-expression of integral or enumeration
2848 --the name of a non-type template-parameter; or
2850 --the name of an object or function with external linkage,
2851 including function templates and function template-ids but
2852 excluding non-static class members, expressed as id-expression;
2855 --the address of an object or function with external linkage,
2856 including function templates and function template-ids but
2857 excluding non-static class members, expressed as & id-expression
2858 where the & is optional if the name refers to a function or
2861 --a pointer to member expressed as described in _expr.unary.op_. */
2863 /* An integral constant-expression can include const variables or
2864 enumerators. Simplify things by folding them to their values,
2865 unless we're about to bind the declaration to a reference
2867 if (INTEGRAL_TYPE_P (expr_type
)
2868 && TREE_CODE (type
) != REFERENCE_TYPE
)
2869 expr
= decl_constant_value (expr
);
2871 if (is_overloaded_fn (expr
))
2872 /* OK for now. We'll check that it has external linkage later.
2873 Check this first since if expr_type is the unknown_type_node
2874 we would otherwise complain below. */
2876 else if (TYPE_PTRMEM_P (expr_type
)
2877 || TYPE_PTRMEMFUNC_P (expr_type
))
2879 if (TREE_CODE (expr
) != PTRMEM_CST
)
2882 else if (TYPE_PTR_P (expr_type
)
2883 || TYPE_PTRMEM_P (expr_type
)
2884 || TREE_CODE (expr_type
) == ARRAY_TYPE
2885 || TREE_CODE (type
) == REFERENCE_TYPE
2886 /* If expr is the address of an overloaded function, we
2887 will get the unknown_type_node at this point. */
2888 || expr_type
== unknown_type_node
)
2894 if (TREE_CODE (expr_type
) == ARRAY_TYPE
2895 || (TREE_CODE (type
) == REFERENCE_TYPE
2896 && TREE_CODE (e
) != ADDR_EXPR
))
2900 if (TREE_CODE (e
) != ADDR_EXPR
)
2903 error ("`%E' is not a valid template argument", expr
);
2904 if (TYPE_PTR_P (expr_type
))
2906 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2907 error ("it must be the address of a function with external linkage");
2909 error ("it must be the address of an object with external linkage");
2911 else if (TYPE_PTRMEM_P (expr_type
)
2912 || TYPE_PTRMEMFUNC_P (expr_type
))
2913 error ("it must be a pointer-to-member of the form `&X::Y'");
2918 referent
= TREE_OPERAND (e
, 0);
2919 STRIP_NOPS (referent
);
2922 if (TREE_CODE (referent
) == STRING_CST
)
2924 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2929 if (is_overloaded_fn (referent
))
2930 /* We'll check that it has external linkage later. */
2932 else if (TREE_CODE (referent
) != VAR_DECL
)
2934 else if (!DECL_EXTERNAL_LINKAGE_P (referent
))
2936 error ("address of non-extern `%E' cannot be used as template argument", referent
);
2937 return error_mark_node
;
2940 else if (INTEGRAL_TYPE_P (expr_type
)
2941 || TYPE_PTRMEM_P (expr_type
)
2942 || TYPE_PTRMEMFUNC_P (expr_type
))
2944 if (! TREE_CONSTANT (expr
))
2947 error ("non-constant `%E' cannot be used as template argument",
2954 error ("object `%E' cannot be used as template argument", expr
);
2958 switch (TREE_CODE (type
))
2963 /* For a non-type template-parameter of integral or enumeration
2964 type, integral promotions (_conv.prom_) and integral
2965 conversions (_conv.integral_) are applied. */
2966 if (!INTEGRAL_TYPE_P (expr_type
))
2967 return error_mark_node
;
2969 /* It's safe to call digest_init in this case; we know we're
2970 just converting one integral constant expression to another. */
2971 expr
= digest_init (type
, expr
, (tree
*) 0);
2973 if (TREE_CODE (expr
) != INTEGER_CST
)
2974 /* Curiously, some TREE_CONSTANT integral expressions do not
2975 simplify to integer constants. For example, `3 % 0',
2976 remains a TRUNC_MOD_EXPR. */
2983 tree type_pointed_to
= TREE_TYPE (type
);
2985 if (TYPE_PTRMEM_P (type
))
2989 /* For a non-type template-parameter of type pointer to data
2990 member, qualification conversions (_conv.qual_) are
2992 e
= perform_qualification_conversions (type
, expr
);
2993 if (TREE_CODE (e
) == NOP_EXPR
)
2994 /* The call to perform_qualification_conversions will
2995 insert a NOP_EXPR over EXPR to do express conversion,
2996 if necessary. But, that will confuse us if we use
2997 this (converted) template parameter to instantiate
2998 another template; then the thing will not look like a
2999 valid template argument. So, just make a new
3000 constant, of the appropriate type. */
3001 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
3004 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
3006 /* For a non-type template-parameter of type pointer to
3007 function, only the function-to-pointer conversion
3008 (_conv.func_) is applied. If the template-argument
3009 represents a set of overloaded functions (or a pointer to
3010 such), the matching function is selected from the set
3015 if (TREE_CODE (expr
) == ADDR_EXPR
)
3016 fns
= TREE_OPERAND (expr
, 0);
3020 fn
= instantiate_type (type_pointed_to
, fns
, tf_none
);
3022 if (fn
== error_mark_node
)
3023 return error_mark_node
;
3025 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3027 if (really_overloaded_fn (fns
))
3028 return error_mark_node
;
3033 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
3035 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3041 /* For a non-type template-parameter of type pointer to
3042 object, qualification conversions (_conv.qual_) and the
3043 array-to-pointer conversion (_conv.array_) are applied.
3044 [Note: In particular, neither the null pointer conversion
3045 (_conv.ptr_) nor the derived-to-base conversion
3046 (_conv.ptr_) are applied. Although 0 is a valid
3047 template-argument for a non-type template-parameter of
3048 integral type, it is not a valid template-argument for a
3049 non-type template-parameter of pointer type.]
3051 The call to decay_conversion performs the
3052 array-to-pointer conversion, if appropriate. */
3053 expr
= decay_conversion (expr
);
3055 if (expr
== error_mark_node
)
3056 return error_mark_node
;
3058 return perform_qualification_conversions (type
, expr
);
3063 case REFERENCE_TYPE
:
3065 tree type_referred_to
= TREE_TYPE (type
);
3067 /* If this expression already has reference type, get the
3068 underling object. */
3069 if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
3071 my_friendly_assert (TREE_CODE (expr
) == ADDR_EXPR
, 20000604);
3072 expr
= TREE_OPERAND (expr
, 0);
3073 expr_type
= TREE_TYPE (expr
);
3076 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
3078 /* For a non-type template-parameter of type reference to
3079 function, no conversions apply. If the
3080 template-argument represents a set of overloaded
3081 functions, the matching function is selected from the
3082 set (_over.over_). */
3085 fn
= instantiate_type (type_referred_to
, expr
, tf_none
);
3087 if (fn
== error_mark_node
)
3088 return error_mark_node
;
3090 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3092 if (really_overloaded_fn (expr
))
3093 /* Don't issue an error here; we might get a different
3094 function if the overloading had worked out
3096 return error_mark_node
;
3101 my_friendly_assert (same_type_p (type_referred_to
,
3109 /* For a non-type template-parameter of type reference to
3110 object, no conversions apply. The type referred to by the
3111 reference may be more cv-qualified than the (otherwise
3112 identical) type of the template-argument. The
3113 template-parameter is bound directly to the
3114 template-argument, which must be an lvalue. */
3115 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type
),
3116 TYPE_MAIN_VARIANT (type_referred_to
))
3117 || !at_least_as_qualified_p (type_referred_to
,
3119 || !real_lvalue_p (expr
))
3120 return error_mark_node
;
3123 mark_addressable (expr
);
3124 return build1 (ADDR_EXPR
, type
, expr
);
3130 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 20010112);
3132 /* For a non-type template-parameter of type pointer to member
3133 function, no conversions apply. If the template-argument
3134 represents a set of overloaded member functions, the
3135 matching member function is selected from the set
3138 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3139 expr_type
!= unknown_type_node
)
3140 return error_mark_node
;
3142 if (TREE_CODE (expr
) == PTRMEM_CST
)
3144 /* A ptr-to-member constant. */
3145 if (!same_type_p (type
, expr_type
))
3146 return error_mark_node
;
3151 if (TREE_CODE (expr
) != ADDR_EXPR
)
3152 return error_mark_node
;
3154 expr
= instantiate_type (type
, expr
, tf_none
);
3156 if (expr
== error_mark_node
)
3157 return error_mark_node
;
3159 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3166 /* All non-type parameters must have one of these types. */
3171 return error_mark_node
;
3174 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3175 template template parameters. Both PARM_PARMS and ARG_PARMS are
3176 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3179 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3180 the case, then extra parameters must have default arguments.
3182 Consider the example:
3183 template <class T, class Allocator = allocator> class vector;
3184 template<template <class U> class TT> class C;
3186 C<vector> is a valid instantiation. PARM_PARMS for the above code
3187 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3188 T and Allocator) and OUTER_ARGS contains the argument that is used to
3189 substitute the TT parameter. */
3192 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
3193 in_decl
, outer_args
)
3194 tree parm_parms
, arg_parms
;
3195 tsubst_flags_t complain
;
3196 tree in_decl
, outer_args
;
3198 int nparms
, nargs
, i
;
3201 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3202 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3204 nparms
= TREE_VEC_LENGTH (parm_parms
);
3205 nargs
= TREE_VEC_LENGTH (arg_parms
);
3207 /* The rule here is opposite of coerce_template_parms. */
3210 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3213 for (i
= 0; i
< nparms
; ++i
)
3215 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3216 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3218 if (arg
== NULL_TREE
|| arg
== error_mark_node
3219 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3222 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3225 switch (TREE_CODE (parm
))
3231 /* We encounter instantiations of templates like
3232 template <template <template <class> class> class TT>
3235 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3236 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3238 if (!coerce_template_template_parms (parmparm
, argparm
,
3246 /* The tsubst call is used to handle cases such as
3247 template <class T, template <T> class TT> class D;
3248 i.e. the parameter list of TT depends on earlier parameters. */
3249 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3262 /* Convert the indicated template ARG as necessary to match the
3263 indicated template PARM. Returns the converted ARG, or
3264 error_mark_node if the conversion was unsuccessful. Error and
3265 warning messages are issued under control of COMPLAIN. This
3266 conversion is for the Ith parameter in the parameter list. ARGS is
3267 the full set of template arguments deduced so far. */
3270 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3274 tsubst_flags_t complain
;
3280 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3282 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3284 if (TREE_CODE (arg
) == TREE_LIST
3285 && TREE_TYPE (arg
) != NULL_TREE
3286 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3288 /* The template argument was the name of some
3289 member function. That's usually
3290 illegal, but static members are OK. In any
3291 case, grab the underlying fields/functions
3292 and issue an error later if required. */
3293 arg
= TREE_VALUE (arg
);
3294 TREE_TYPE (arg
) = unknown_type_node
;
3297 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3298 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3299 || requires_tmpl_type
);
3301 if (TREE_CODE (arg
) != RECORD_TYPE
)
3302 is_tmpl_type
= ((TREE_CODE (arg
) == TEMPLATE_DECL
3303 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3304 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3305 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
3306 else if (CLASSTYPE_TEMPLATE_INFO (arg
) && !CLASSTYPE_USE_TEMPLATE (arg
)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg
)))
3309 if (is_base_of_enclosing_class (arg
, current_class_type
))
3310 /* This is a template name used within the scope of the
3311 template. It could be the template, or it could be the
3312 instantiation. Choose whichever makes sense. */
3313 is_tmpl_type
= requires_tmpl_type
;
3318 /* It is a non-template class, or a specialization of a template
3319 class, or a non-template member of a template class. */
3323 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3324 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
3325 arg
= TYPE_STUB_DECL (arg
);
3326 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3327 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3329 is_type
= TYPE_P (arg
) || is_tmpl_type
;
3331 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3332 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3334 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg
);
3336 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3337 TREE_OPERAND (arg
, 1),
3338 complain
& tf_error
);
3341 if (is_type
!= requires_type
)
3345 if (complain
& tf_error
)
3347 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3350 error (" expected a constant of type `%T', got `%T'",
3352 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3354 error (" expected a type, got `%E'", arg
);
3357 return error_mark_node
;
3359 if (is_tmpl_type
^ requires_tmpl_type
)
3361 if (in_decl
&& (complain
& tf_error
))
3363 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3366 error (" expected a type, got `%T'", DECL_NAME (arg
));
3368 error (" expected a class template, got `%T'", arg
);
3370 return error_mark_node
;
3375 if (requires_tmpl_type
)
3377 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
3378 /* The number of argument required is not known yet.
3379 Just accept it for now. */
3380 val
= TREE_TYPE (arg
);
3383 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3384 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3386 if (coerce_template_template_parms (parmparm
, argparm
,
3392 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3394 if (val
!= error_mark_node
3395 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3396 val
= TREE_TYPE (val
);
3400 if (in_decl
&& (complain
& tf_error
))
3402 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3404 error (" expected a template of type `%D', got `%D'", parm
, arg
);
3407 val
= error_mark_node
;
3413 val
= groktypename (arg
);
3414 if (! processing_template_decl
)
3416 /* [basic.link]: A name with no linkage (notably, the
3417 name of a class or enumeration declared in a local
3418 scope) shall not be used to declare an entity with
3419 linkage. This implies that names with no linkage
3420 cannot be used as template arguments. */
3421 tree t
= no_linkage_check (val
);
3424 if (TYPE_ANONYMOUS_P (t
))
3426 ("template-argument `%T' uses anonymous type", val
);
3429 ("template-argument `%T' uses local type `%T'",
3431 return error_mark_node
;
3438 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3440 if (invalid_nontype_parm_type_p (t
, complain
))
3441 return error_mark_node
;
3443 if (processing_template_decl
)
3444 arg
= maybe_fold_nontype_arg (arg
);
3446 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3447 /* We used to call digest_init here. However, digest_init
3448 will report errors, which we don't want when complain
3449 is zero. More importantly, digest_init will try too
3450 hard to convert things: for example, `0' should not be
3451 converted to pointer type at this point according to
3452 the standard. Accepting this is not merely an
3453 extension, since deciding whether or not these
3454 conversions can occur is part of determining which
3455 function template to call, or whether a given explicit
3456 argument specification is legal. */
3457 val
= convert_nontype_argument (t
, arg
);
3461 if (val
== NULL_TREE
)
3462 val
= error_mark_node
;
3463 else if (val
== error_mark_node
&& (complain
& tf_error
))
3464 error ("could not convert template argument `%E' to `%T'",
3471 /* Convert all template arguments to their appropriate types, and
3472 return a vector containing the innermost resulting template
3473 arguments. If any error occurs, return error_mark_node. Error and
3474 warning messages are issued under control of COMPLAIN. Some error
3475 messages are issued even if COMPLAIN is zero; for instance, if a
3476 template argument is composed from a local class.
3478 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3479 provided in ARGLIST, or else trailing parameters must have default
3480 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3481 deduction for any unspecified trailing arguments. */
3484 coerce_template_parms (parms
, args
, in_decl
,
3486 require_all_arguments
)
3489 tsubst_flags_t complain
;
3490 int require_all_arguments
;
3492 int nparms
, nargs
, i
, lost
= 0;
3495 tree new_inner_args
;
3497 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3498 nargs
= NUM_TMPL_ARGS (inner_args
);
3499 nparms
= TREE_VEC_LENGTH (parms
);
3503 && require_all_arguments
3504 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3506 if (complain
& tf_error
)
3508 error ("wrong number of template arguments (%d, should be %d)",
3512 cp_error_at ("provided for `%D'", in_decl
);
3515 return error_mark_node
;
3518 new_inner_args
= make_tree_vec (nparms
);
3519 new_args
= add_outermost_template_args (args
, new_inner_args
);
3520 for (i
= 0; i
< nparms
; i
++)
3525 /* Get the Ith template parameter. */
3526 parm
= TREE_VEC_ELT (parms
, i
);
3528 /* Calculate the Ith argument. */
3529 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3531 arg
= TREE_VALUE (inner_args
);
3532 inner_args
= TREE_CHAIN (inner_args
);
3535 arg
= TREE_VEC_ELT (inner_args
, i
);
3536 /* If no template argument was supplied, look for a default
3538 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3540 /* There was no default value. */
3541 my_friendly_assert (!require_all_arguments
, 0);
3544 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3545 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3547 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3550 /* Now, convert the Ith argument, as necessary. */
3551 if (arg
== NULL_TREE
)
3552 /* We're out of arguments. */
3554 my_friendly_assert (!require_all_arguments
, 0);
3557 else if (arg
== error_mark_node
)
3559 error ("template argument %d is invalid", i
+ 1);
3560 arg
= error_mark_node
;
3563 arg
= convert_template_argument (TREE_VALUE (parm
),
3564 arg
, new_args
, complain
, i
,
3567 if (arg
== error_mark_node
)
3569 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3573 return error_mark_node
;
3575 return new_inner_args
;
3578 /* Returns 1 if template args OT and NT are equivalent. */
3581 template_args_equal (ot
, nt
)
3587 if (TREE_CODE (nt
) == TREE_VEC
)
3588 /* For member templates */
3589 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
3590 else if (TYPE_P (nt
))
3591 return TYPE_P (ot
) && same_type_p (ot
, nt
);
3592 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
3595 return (cp_tree_equal (ot
, nt
) > 0);
3598 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3599 of template arguments. Returns 0 otherwise. */
3602 comp_template_args (oldargs
, newargs
)
3603 tree oldargs
, newargs
;
3607 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3610 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3612 tree nt
= TREE_VEC_ELT (newargs
, i
);
3613 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3615 if (! template_args_equal (ot
, nt
))
3621 /* Given class template name and parameter list, produce a user-friendly name
3622 for the instantiation. */
3625 mangle_class_name_for_template (name
, parms
, arglist
)
3627 tree parms
, arglist
;
3629 static struct obstack scratch_obstack
;
3630 static char *scratch_firstobj
;
3633 if (!scratch_firstobj
)
3634 gcc_obstack_init (&scratch_obstack
);
3636 obstack_free (&scratch_obstack
, scratch_firstobj
);
3637 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3639 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3640 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3644 nparms
= TREE_VEC_LENGTH (parms
);
3645 arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
3646 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3647 for (i
= 0; i
< nparms
; i
++)
3649 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3650 tree arg
= TREE_VEC_ELT (arglist
, i
);
3655 if (TREE_CODE (parm
) == TYPE_DECL
)
3657 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3660 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3662 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3664 /* Already substituted with real template. Just output
3665 the template name here */
3666 tree context
= DECL_CONTEXT (arg
);
3669 /* The template may be defined in a namespace, or
3670 may be a member template. */
3671 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3672 || CLASS_TYPE_P (context
),
3674 cat(decl_as_string (DECL_CONTEXT (arg
), TFF_PLAIN_IDENTIFIER
));
3677 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3680 /* Output the parameter declaration */
3681 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3685 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3687 if (TREE_CODE (arg
) == TREE_LIST
)
3689 /* New list cell was built because old chain link was in
3691 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3692 arg
= TREE_VALUE (arg
);
3694 /* No need to check arglist against parmlist here; we did that
3695 in coerce_template_parms, called from lookup_template_class. */
3696 cat (expr_as_string (arg
, TFF_PLAIN_IDENTIFIER
));
3699 char *bufp
= obstack_next_free (&scratch_obstack
);
3701 while (bufp
[offset
- 1] == ' ')
3703 obstack_blank_fast (&scratch_obstack
, offset
);
3705 /* B<C<char> >, not B<C<char>> */
3706 if (bufp
[offset
- 1] == '>')
3711 return (char *) obstack_base (&scratch_obstack
);
3715 classtype_mangled_name (t
)
3718 if (CLASSTYPE_TEMPLATE_INFO (t
)
3719 /* Specializations have already had their names set up in
3720 lookup_template_class. */
3721 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3723 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3725 /* For non-primary templates, the template parameters are
3726 implicit from their surrounding context. */
3727 if (PRIMARY_TEMPLATE_P (tmpl
))
3729 tree name
= DECL_NAME (tmpl
);
3730 char *mangled_name
= mangle_class_name_for_template
3731 (IDENTIFIER_POINTER (name
),
3732 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3733 CLASSTYPE_TI_ARGS (t
));
3734 tree id
= get_identifier (mangled_name
);
3735 IDENTIFIER_TEMPLATE (id
) = name
;
3740 return TYPE_IDENTIFIER (t
);
3744 add_pending_template (d
)
3747 tree ti
= (TYPE_P (d
)
3748 ? CLASSTYPE_TEMPLATE_INFO (d
)
3749 : DECL_TEMPLATE_INFO (d
));
3753 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3756 /* We are called both from instantiate_decl, where we've already had a
3757 tinst_level pushed, and instantiate_template, where we haven't.
3759 level
= !(current_tinst_level
&& TINST_DECL (current_tinst_level
) == d
);
3762 push_tinst_level (d
);
3764 pt
= tree_cons (current_tinst_level
, d
, NULL_TREE
);
3765 if (last_pending_template
)
3766 TREE_CHAIN (last_pending_template
) = pt
;
3768 pending_templates
= pt
;
3770 last_pending_template
= pt
;
3772 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3779 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3780 may be either a _DECL or an overloaded function or an
3781 IDENTIFIER_NODE), and ARGLIST. */
3784 lookup_template_function (fns
, arglist
)
3789 if (fns
== NULL_TREE
)
3791 error ("non-template used as template");
3792 return error_mark_node
;
3795 type
= TREE_TYPE (fns
);
3796 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3797 type
= unknown_type_node
;
3799 if (processing_template_decl
)
3800 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3802 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3805 /* Within the scope of a template class S<T>, the name S gets bound
3806 (in build_self_reference) to a TYPE_DECL for the class, not a
3807 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3808 or one of its enclosing classes, and that type is a template,
3809 return the associated TEMPLATE_DECL. Otherwise, the original
3810 DECL is returned. */
3813 maybe_get_template_decl_from_type_decl (decl
)
3816 return (decl
!= NULL_TREE
3817 && TREE_CODE (decl
) == TYPE_DECL
3818 && DECL_ARTIFICIAL (decl
)
3819 && CLASS_TYPE_P (TREE_TYPE (decl
))
3820 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3821 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3824 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3825 parameters, find the desired type.
3827 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3828 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3829 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3832 IN_DECL, if non-NULL, is the template declaration we are trying to
3835 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3836 the class we are looking up.
3838 Issue error and warning messages under control of COMPLAIN.
3840 If the template class is really a local class in a template
3841 function, then the FUNCTION_CONTEXT is the function in which it is
3842 being instantiated. */
3845 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
, complain
)
3850 tsubst_flags_t complain
;
3852 tree
template = NULL_TREE
, parmlist
;
3855 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3857 if (IDENTIFIER_VALUE (d1
)
3858 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3859 template = IDENTIFIER_VALUE (d1
);
3863 push_decl_namespace (context
);
3864 template = lookup_name (d1
, /*prefer_type=*/0);
3865 template = maybe_get_template_decl_from_type_decl (template);
3867 pop_decl_namespace ();
3870 context
= DECL_CONTEXT (template);
3872 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3874 tree type
= TREE_TYPE (d1
);
3876 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3877 an implicit typename for the second A. Deal with it. */
3878 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
3879 type
= TREE_TYPE (type
);
3881 if (CLASSTYPE_TEMPLATE_INFO (type
))
3883 template = CLASSTYPE_TI_TEMPLATE (type
);
3884 d1
= DECL_NAME (template);
3887 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3888 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
3890 template = TYPE_TI_TEMPLATE (d1
);
3891 d1
= DECL_NAME (template);
3893 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3894 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
3897 d1
= DECL_NAME (template);
3898 context
= DECL_CONTEXT (template);
3901 /* With something like `template <class T> class X class X { ... };'
3902 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3903 We don't want to do that, but we have to deal with the situation,
3904 so let's give them some syntax errors to chew on instead of a
3905 crash. Alternatively D1 might not be a template type at all. */
3908 if (complain
& tf_error
)
3909 error ("`%T' is not a template", d1
);
3910 return error_mark_node
;
3913 if (TREE_CODE (template) != TEMPLATE_DECL
3914 /* If we're called from the parser, make sure it's a user visible
3916 || ((!arglist
|| TREE_CODE (arglist
) == TREE_LIST
)
3917 && !DECL_TEMPLATE_PARM_P (template)
3918 && !PRIMARY_TEMPLATE_P (template)))
3920 if (complain
& tf_error
)
3922 error ("non-template type `%T' used as a template", d1
);
3924 cp_error_at ("for template declaration `%D'", in_decl
);
3926 return error_mark_node
;
3929 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3931 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3932 template arguments */
3937 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3939 /* Consider an example where a template template parameter declared as
3941 template <class T, class U = std::allocator<T> > class TT
3943 The template parameter level of T and U are one level larger than
3944 of TT. To proper process the default argument of U, say when an
3945 instantiation `TT<int>' is seen, we need to build the full
3946 arguments containing {int} as the innermost level. Outer levels,
3947 available when not appearing as default template argument, can be
3948 obtained from `current_template_args ()'.
3950 Suppose that TT is later substituted with std::vector. The above
3951 instantiation is `TT<int, std::allocator<T> >' with TT at
3952 level 1, and T at level 2, while the template arguments at level 1
3953 becomes {std::vector} and the inner level 2 is {int}. */
3955 if (current_template_parms
)
3956 arglist
= add_to_template_args (current_template_args (), arglist
);
3958 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
3959 complain
, /*require_all_args=*/1);
3960 if (arglist2
== error_mark_node
)
3961 return error_mark_node
;
3963 parm
= bind_template_template_parm (TREE_TYPE (template), arglist2
);
3968 tree template_type
= TREE_TYPE (template);
3971 tree found
= NULL_TREE
;
3975 int is_partial_instantiation
;
3977 gen_tmpl
= most_general_template (template);
3978 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
3979 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3980 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3982 if (arg_depth
== 1 && parm_depth
> 1)
3984 /* We've been given an incomplete set of template arguments.
3987 template <class T> struct S1 {
3988 template <class U> struct S2 {};
3989 template <class U> struct S2<U*> {};
3992 we will be called with an ARGLIST of `U*', but the
3993 TEMPLATE will be `template <class T> template
3994 <class U> struct S1<T>::S2'. We must fill in the missing
3997 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3999 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
4002 /* Now we should have enough arguments. */
4003 my_friendly_assert (parm_depth
== arg_depth
, 0);
4005 /* From here on, we're only interested in the most general
4007 template = gen_tmpl
;
4009 /* Calculate the BOUND_ARGS. These will be the args that are
4010 actually tsubst'd into the definition to create the
4014 /* We have multiple levels of arguments to coerce, at once. */
4016 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
4018 tree bound_args
= make_tree_vec (parm_depth
);
4020 for (i
= saved_depth
,
4021 t
= DECL_TEMPLATE_PARMS (template);
4022 i
> 0 && t
!= NULL_TREE
;
4023 --i
, t
= TREE_CHAIN (t
))
4025 tree a
= coerce_template_parms (TREE_VALUE (t
),
4027 complain
, /*require_all_args=*/1);
4028 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
4030 /* We temporarily reduce the length of the ARGLIST so
4031 that coerce_template_parms will see only the arguments
4032 corresponding to the template parameters it is
4034 TREE_VEC_LENGTH (arglist
)--;
4037 /* Restore the ARGLIST to its full size. */
4038 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4040 arglist
= bound_args
;
4044 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
4045 INNERMOST_TEMPLATE_ARGS (arglist
),
4047 complain
, /*require_all_args=*/1);
4049 if (arglist
== error_mark_node
)
4050 /* We were unable to bind the arguments. */
4051 return error_mark_node
;
4053 /* In the scope of a template class, explicit references to the
4054 template class refer to the type of the template, not any
4055 instantiation of it. For example, in:
4057 template <class T> class C { void f(C<T>); }
4059 the `C<T>' is just the same as `C'. Outside of the
4060 class, however, such a reference is an instantiation. */
4061 if (comp_template_args (TYPE_TI_ARGS (template_type
),
4064 found
= template_type
;
4066 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
4070 /* Note that we use DECL_CONTEXT, rather than
4071 CP_DECL_CONTEXT, so that the termination test is
4072 always just `ctx'. We're not interested in namespace
4074 for (ctx
= current_class_type
;
4076 ctx
= (TYPE_P (ctx
)) ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
4077 if (same_type_p (ctx
, template_type
))
4081 /* We're not in the scope of the class, so the
4082 TEMPLATE_TYPE is not the type we want after
4090 for (tp
= &DECL_TEMPLATE_INSTANTIATIONS (template);
4092 tp
= &TREE_CHAIN (*tp
))
4093 if (comp_template_args (TREE_PURPOSE (*tp
), arglist
))
4097 /* Use the move-to-front heuristic to speed up future
4099 *tp
= TREE_CHAIN (*tp
);
4101 = DECL_TEMPLATE_INSTANTIATIONS (template);
4102 DECL_TEMPLATE_INSTANTIATIONS (template) = found
;
4104 return TREE_VALUE (found
);
4107 /* This type is a "partial instantiation" if any of the template
4108 arguments still involve template parameters. Note that we set
4109 IS_PARTIAL_INSTANTIATION for partial specializations as
4111 is_partial_instantiation
= uses_template_parms (arglist
);
4113 if (!is_partial_instantiation
4114 && !PRIMARY_TEMPLATE_P (template)
4115 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
4117 found
= xref_tag_from_type (TREE_TYPE (template),
4118 DECL_NAME (template),
4123 context
= tsubst (DECL_CONTEXT (template), arglist
,
4126 context
= global_namespace
;
4128 /* Create the type. */
4129 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
4131 if (!is_partial_instantiation
)
4133 set_current_access_from_decl (TYPE_NAME (template_type
));
4134 t
= start_enum (TYPE_IDENTIFIER (template_type
));
4137 /* We don't want to call start_enum for this type, since
4138 the values for the enumeration constants may involve
4139 template parameters. And, no one should be interested
4140 in the enumeration constants for such a type. */
4141 t
= make_node (ENUMERAL_TYPE
);
4145 t
= make_aggr_type (TREE_CODE (template_type
));
4146 CLASSTYPE_DECLARED_CLASS (t
)
4147 = CLASSTYPE_DECLARED_CLASS (template_type
);
4148 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
4149 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
4150 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
4152 /* A local class. Make sure the decl gets registered properly. */
4153 if (context
== current_function_decl
)
4154 pushtag (DECL_NAME (template), t
, 0);
4157 /* If we called start_enum or pushtag above, this information
4158 will already be set up. */
4161 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4163 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
4164 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
4165 TYPE_STUB_DECL (t
) = type_decl
;
4166 DECL_SOURCE_FILE (type_decl
)
4167 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
4168 DECL_SOURCE_LINE (type_decl
)
4169 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
4172 type_decl
= TYPE_NAME (t
);
4174 /* Set up the template information. We have to figure out which
4175 template is the immediate parent if this is a full
4177 if (parm_depth
== 1 || is_partial_instantiation
4178 || !PRIMARY_TEMPLATE_P (template))
4179 /* This case is easy; there are no member templates involved. */
4183 /* This is a full instantiation of a member template. Look
4184 for a partial instantiation of which this is an instance. */
4186 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4187 found
; found
= TREE_CHAIN (found
))
4190 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4192 /* We only want partial instantiations, here, not
4193 specializations or full instantiations. */
4194 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4195 || !uses_template_parms (TREE_VALUE (found
)))
4198 /* Temporarily reduce by one the number of levels in the
4199 ARGLIST and in FOUND so as to avoid comparing the
4200 last set of arguments. */
4201 TREE_VEC_LENGTH (arglist
)--;
4202 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4204 /* See if the arguments match. If they do, then TMPL is
4205 the partial instantiation we want. */
4206 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4208 /* Restore the argument vectors to their full size. */
4209 TREE_VEC_LENGTH (arglist
)++;
4210 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4221 /* There was no partial instantiation. This happens
4222 where C<T> is a member template of A<T> and it's used
4225 template <typename T> struct B { A<T>::C<int> m; };
4228 Create the partial instantiation.
4230 TREE_VEC_LENGTH (arglist
)--;
4231 found
= tsubst (template, arglist
, complain
, NULL_TREE
);
4232 TREE_VEC_LENGTH (arglist
)++;
4236 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
4237 DECL_TEMPLATE_INSTANTIATIONS (template)
4238 = tree_cons (arglist
, t
,
4239 DECL_TEMPLATE_INSTANTIATIONS (template));
4241 if (TREE_CODE (t
) == ENUMERAL_TYPE
4242 && !is_partial_instantiation
)
4243 /* Now that the type has been registered on the instantiations
4244 list, we set up the enumerators. Because the enumeration
4245 constants may involve the enumeration type itself, we make
4246 sure to register the type first, and then create the
4247 constants. That way, doing tsubst_expr for the enumeration
4248 constants won't result in recursive calls here; we'll find
4249 the instantiation and exit above. */
4250 tsubst_enum (template_type
, t
, arglist
);
4252 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4254 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4255 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4256 if (!is_partial_instantiation
)
4258 /* For backwards compatibility; code that uses
4259 -fexternal-templates expects looking up a template to
4260 instantiate it. I think DDD still relies on this.
4261 (jason 8/20/1998) */
4262 if (TREE_CODE (t
) != ENUMERAL_TYPE
4263 && flag_external_templates
4264 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4265 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4266 add_pending_template (t
);
4269 /* If the type makes use of template parameters, the
4270 code that generates debugging information will crash. */
4271 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4283 /* Called from for_each_template_parm via walk_tree. */
4286 for_each_template_parm_r (tp
, walk_subtrees
, d
)
4292 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
4293 tree_fn_t fn
= pfd
->fn
;
4294 void *data
= pfd
->data
;
4297 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
4298 return error_mark_node
;
4300 switch (TREE_CODE (t
))
4303 if (TYPE_PTRMEMFUNC_P (t
))
4309 if (!TYPE_TEMPLATE_INFO (t
))
4311 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
4313 return error_mark_node
;
4317 /* Since we're not going to walk subtrees, we have to do this
4319 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4320 return error_mark_node
;
4324 /* Check the return type. */
4325 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4326 return error_mark_node
;
4328 /* Check the parameter types. Since default arguments are not
4329 instantiated until they are needed, the TYPE_ARG_TYPES may
4330 contain expressions that involve template parameters. But,
4331 no-one should be looking at them yet. And, once they're
4332 instantiated, they don't contain template parameters, so
4333 there's no point in looking at them then, either. */
4337 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4338 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4339 return error_mark_node
;
4341 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4342 want walk_tree walking into them itself. */
4349 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4350 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4351 return error_mark_node
;
4356 if (DECL_CONTEXT (t
)
4357 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4358 return error_mark_node
;
4361 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4362 /* Record template parameters such as `T' inside `TT<T>'. */
4363 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4364 return error_mark_node
;
4367 case TEMPLATE_TEMPLATE_PARM
:
4368 case TEMPLATE_TYPE_PARM
:
4369 case TEMPLATE_PARM_INDEX
:
4370 if (fn
&& (*fn
)(t
, data
))
4371 return error_mark_node
;
4373 return error_mark_node
;
4377 /* A template template parameter is encountered */
4378 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
4379 && for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4380 return error_mark_node
;
4382 /* Already substituted template template parameter */
4387 if (!fn
|| for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
, data
))
4388 return error_mark_node
;
4392 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
4393 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4394 (TREE_TYPE (t
)), fn
, data
))
4395 return error_mark_node
;
4400 /* If there's no type, then this thing must be some expression
4401 involving template parameters. */
4402 if (!fn
&& !TREE_TYPE (t
))
4403 return error_mark_node
;
4408 case REINTERPRET_CAST_EXPR
:
4409 case CONST_CAST_EXPR
:
4410 case STATIC_CAST_EXPR
:
4411 case DYNAMIC_CAST_EXPR
:
4416 case PSEUDO_DTOR_EXPR
:
4418 return error_mark_node
;
4425 /* We didn't find any template parameters we liked. */
4429 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4430 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4431 call FN with the parameter and the DATA.
4432 If FN returns non-zero, the iteration is terminated, and
4433 for_each_template_parm returns 1. Otherwise, the iteration
4434 continues. If FN never returns a non-zero value, the value
4435 returned by for_each_template_parm is 0. If FN is NULL, it is
4436 considered to be the function which always returns 1. */
4439 for_each_template_parm (t
, fn
, data
)
4444 struct pair_fn_data pfd
;
4450 /* Walk the tree. (Conceptually, we would like to walk without
4451 duplicates, but for_each_template_parm_r recursively calls
4452 for_each_template_parm, so we would need to reorganize a fair
4453 bit to use walk_tree_without_duplicates.) */
4454 return walk_tree (&t
,
4455 for_each_template_parm_r
,
4461 uses_template_parms (t
)
4464 return for_each_template_parm (t
, 0, 0);
4467 static int tinst_depth
;
4468 extern int max_tinst_depth
;
4469 #ifdef GATHER_STATISTICS
4472 static int tinst_level_tick
;
4473 static int last_template_error_tick
;
4475 /* We're starting to instantiate D; record the template instantiation context
4476 for diagnostics and to restore it later. */
4479 push_tinst_level (d
)
4484 if (tinst_depth
>= max_tinst_depth
)
4486 /* If the instantiation in question still has unbound template parms,
4487 we don't really care if we can't instantiate it, so just return.
4488 This happens with base instantiation for implicit `typename'. */
4489 if (uses_template_parms (d
))
4492 last_template_error_tick
= tinst_level_tick
;
4493 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4494 max_tinst_depth
, d
);
4496 print_instantiation_context ();
4501 new = build_expr_wfl (d
, input_filename
, lineno
, 0);
4502 TREE_CHAIN (new) = current_tinst_level
;
4503 current_tinst_level
= new;
4506 #ifdef GATHER_STATISTICS
4507 if (tinst_depth
> depth_reached
)
4508 depth_reached
= tinst_depth
;
4515 /* We're done instantiating this template; return to the instantiation
4521 tree old
= current_tinst_level
;
4523 /* Restore the filename and line number stashed away when we started
4524 this instantiation. */
4525 lineno
= TINST_LINE (old
);
4526 input_filename
= TINST_FILE (old
);
4527 extract_interface_info ();
4529 current_tinst_level
= TREE_CHAIN (old
);
4534 /* We're instantiating a deferred template; restore the template
4535 instantiation context in which the instantiation was requested, which
4536 is one step out from LEVEL. */
4539 reopen_tinst_level (level
)
4545 for (t
= level
; t
; t
= TREE_CHAIN (t
))
4548 current_tinst_level
= level
;
4552 /* Return the outermost template instantiation context, for use with
4553 -falt-external-templates. */
4558 tree p
= current_tinst_level
;
4561 for (; TREE_CHAIN (p
) ; p
= TREE_CHAIN (p
))
4566 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4567 vector of template arguments, as for tsubst.
4569 Returns an appropriate tsubst'd friend declaration. */
4572 tsubst_friend_function (decl
, args
)
4578 const char *file
= input_filename
;
4580 lineno
= DECL_SOURCE_LINE (decl
);
4581 input_filename
= DECL_SOURCE_FILE (decl
);
4583 if (TREE_CODE (decl
) == FUNCTION_DECL
4584 && DECL_TEMPLATE_INSTANTIATION (decl
)
4585 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4586 /* This was a friend declared with an explicit template
4587 argument list, e.g.:
4591 to indicate that f was a template instantiation, not a new
4592 function declaration. Now, we have to figure out what
4593 instantiation of what template. */
4595 tree template_id
, arglist
, fns
;
4598 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
4600 /* Friend functions are looked up in the containing namespace scope.
4601 We must enter that scope, to avoid finding member functions of the
4602 current cless with same name. */
4603 push_nested_namespace (ns
);
4604 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
4605 tf_error
| tf_warning
, NULL_TREE
);
4606 pop_nested_namespace (ns
);
4607 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
4608 tf_error
| tf_warning
, NULL_TREE
);
4609 template_id
= lookup_template_function (fns
, arglist
);
4611 new_friend
= tsubst (decl
, args
, tf_error
| tf_warning
, NULL_TREE
);
4612 tmpl
= determine_specialization (template_id
, new_friend
,
4614 /*need_member_template=*/0);
4615 new_friend
= instantiate_template (tmpl
, new_args
);
4619 new_friend
= tsubst (decl
, args
, tf_error
| tf_warning
, NULL_TREE
);
4621 /* The NEW_FRIEND will look like an instantiation, to the
4622 compiler, but is not an instantiation from the point of view of
4623 the language. For example, we might have had:
4625 template <class T> struct S {
4626 template <class U> friend void f(T, U);
4629 Then, in S<int>, template <class U> void f(int, U) is not an
4630 instantiation of anything. */
4631 DECL_USE_TEMPLATE (new_friend
) = 0;
4632 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4634 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4635 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
4636 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
4639 /* The mangled name for the NEW_FRIEND is incorrect. The function
4640 is not a template instantiation and should not be mangled like
4641 one. Therefore, we forget the mangling here; we'll recompute it
4642 later if we need it. */
4643 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4645 SET_DECL_RTL (new_friend
, NULL_RTX
);
4646 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
4649 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4652 tree new_friend_template_info
;
4653 tree new_friend_result_template_info
;
4655 int new_friend_is_defn
;
4657 /* We must save some information from NEW_FRIEND before calling
4658 duplicate decls since that function will free NEW_FRIEND if
4660 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4661 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4663 /* This declaration is a `primary' template. */
4664 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4667 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend
)) != NULL_TREE
;
4668 new_friend_result_template_info
4669 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
4673 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4674 new_friend_result_template_info
= NULL_TREE
;
4677 /* Inside pushdecl_namespace_level, we will push into the
4678 current namespace. However, the friend function should go
4679 into the namespace of the template. */
4680 ns
= decl_namespace_context (new_friend
);
4681 push_nested_namespace (ns
);
4682 old_decl
= pushdecl_namespace_level (new_friend
);
4683 pop_nested_namespace (ns
);
4685 if (old_decl
!= new_friend
)
4687 /* This new friend declaration matched an existing
4688 declaration. For example, given:
4690 template <class T> void f(T);
4691 template <class U> class C {
4692 template <class T> friend void f(T) {}
4695 the friend declaration actually provides the definition
4696 of `f', once C has been instantiated for some type. So,
4697 old_decl will be the out-of-class template declaration,
4698 while new_friend is the in-class definition.
4700 But, if `f' was called before this point, the
4701 instantiation of `f' will have DECL_TI_ARGS corresponding
4702 to `T' but not to `U', references to which might appear
4703 in the definition of `f'. Previously, the most general
4704 template for an instantiation of `f' was the out-of-class
4705 version; now it is the in-class version. Therefore, we
4706 run through all specialization of `f', adding to their
4707 DECL_TI_ARGS appropriately. In particular, they need a
4708 new set of outer arguments, corresponding to the
4709 arguments for this class instantiation.
4711 The same situation can arise with something like this:
4714 template <class T> class C {
4718 when `C<int>' is instantiated. Now, `f(int)' is defined
4721 if (!new_friend_is_defn
)
4722 /* On the other hand, if the in-class declaration does
4723 *not* provide a definition, then we don't want to alter
4724 existing definitions. We can just leave everything
4729 /* Overwrite whatever template info was there before, if
4730 any, with the new template information pertaining to
4732 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4734 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4735 /* duplicate_decls will take care of this case. */
4740 tree new_friend_args
;
4742 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
4743 = new_friend_result_template_info
;
4745 new_friend_args
= TI_ARGS (new_friend_template_info
);
4746 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4750 tree spec
= TREE_VALUE (t
);
4753 = add_outermost_template_args (new_friend_args
,
4754 DECL_TI_ARGS (spec
));
4757 /* Now, since specializations are always supposed to
4758 hang off of the most general template, we must move
4760 t
= most_general_template (old_decl
);
4763 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4764 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4765 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4766 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4771 /* The information from NEW_FRIEND has been merged into OLD_DECL
4772 by duplicate_decls. */
4773 new_friend
= old_decl
;
4776 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend
)))
4778 /* Check to see that the declaration is really present, and,
4779 possibly obtain an improved declaration. */
4780 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4789 input_filename
= file
;
4793 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4794 template arguments, as for tsubst.
4796 Returns an appropriate tsubst'd friend type or error_mark_node on
4800 tsubst_friend_class (friend_tmpl
, args
)
4808 context
= DECL_CONTEXT (friend_tmpl
);
4812 if (TREE_CODE (context
) == NAMESPACE_DECL
)
4813 push_nested_namespace (context
);
4815 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
), 2);
4818 /* First, we look for a class template. */
4819 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
4821 /* But, if we don't find one, it might be because we're in a
4822 situation like this:
4830 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4831 for `S<int>', not the TEMPLATE_DECL. */
4832 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
4834 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
4835 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4838 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4840 /* The friend template has already been declared. Just
4841 check to see that the declarations match, and install any new
4842 default parameters. We must tsubst the default parameters,
4843 of course. We only need the innermost template parameters
4844 because that is all that redeclare_class_template will look
4846 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
4847 > TMPL_ARGS_DEPTH (args
))
4850 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4851 args
, tf_error
| tf_warning
);
4852 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4855 friend_type
= TREE_TYPE (tmpl
);
4859 /* The friend template has not already been declared. In this
4860 case, the instantiation of the template class will cause the
4861 injection of this template into the global scope. */
4862 tmpl
= tsubst (friend_tmpl
, args
, tf_error
| tf_warning
, NULL_TREE
);
4864 /* The new TMPL is not an instantiation of anything, so we
4865 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4866 the new type because that is supposed to be the corresponding
4867 template decl, i.e., TMPL. */
4868 DECL_USE_TEMPLATE (tmpl
) = 0;
4869 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4870 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4872 /* Inject this template into the global scope. */
4873 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4878 if (TREE_CODE (context
) == NAMESPACE_DECL
)
4879 pop_nested_namespace (context
);
4881 pop_nested_class ();
4887 /* Returns zero if TYPE cannot be completed later due to circularity.
4888 Otherwise returns one. */
4891 can_complete_type_without_circularity (type
)
4894 if (type
== NULL_TREE
|| type
== error_mark_node
)
4896 else if (COMPLETE_TYPE_P (type
))
4898 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
4899 return can_complete_type_without_circularity (TREE_TYPE (type
));
4900 else if (CLASS_TYPE_P (type
) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
4907 instantiate_class_template (type
)
4910 tree
template, args
, pattern
, t
;
4913 if (type
== error_mark_node
)
4914 return error_mark_node
;
4916 if (TYPE_BEING_DEFINED (type
) || COMPLETE_TYPE_P (type
))
4919 /* Figure out which template is being instantiated. */
4920 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4921 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4923 /* Figure out which arguments are being used to do the
4925 args
= CLASSTYPE_TI_ARGS (type
);
4926 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4928 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4929 /* If this is a partial instantiation, then we can't instantiate
4930 the type; there's no telling whether or not one of the
4931 template parameters might eventually be instantiated to some
4932 value that results in a specialization being used. For
4944 Now, the `S<U>' in `f<int>' is the specialization, not an
4945 instantiation of the original template. */
4948 /* Determine what specialization of the original template to
4950 if (PARTIAL_INSTANTIATION_P (type
))
4951 /* There's no telling which specialization is appropriate at this
4952 point. Since all peeking at the innards of this partial
4953 instantiation are extensions (like the "implicit typename"
4954 extension, which allows users to omit the keyword `typename' on
4955 names that are declared as types in template base classes), we
4956 are free to do what we please.
4958 Trying to figure out which partial instantiation to use can
4959 cause a crash. (Some of the template arguments don't even have
4960 types.) So, we just use the most general version. */
4964 t
= most_specialized_class (template, args
);
4966 if (t
== error_mark_node
)
4968 const char *str
= "candidates are:";
4969 error ("ambiguous class template instantiation for `%#T'", type
);
4970 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4973 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4976 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4980 TYPE_BEING_DEFINED (type
) = 1;
4981 return error_mark_node
;
4986 pattern
= TREE_TYPE (t
);
4988 pattern
= TREE_TYPE (template);
4990 /* If the template we're instantiating is incomplete, then clearly
4991 there's nothing we can do. */
4992 if (!COMPLETE_TYPE_P (pattern
))
4995 /* If this is a partial instantiation, don't tsubst anything. We will
4996 only use this type for implicit typename, so the actual contents don't
4997 matter. All that matters is whether a particular name is a type. */
4998 if (PARTIAL_INSTANTIATION_P (type
))
5000 /* The fields set here must be kept in sync with those cleared
5001 in begin_class_definition. */
5002 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
5003 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
5004 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
5005 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
5006 CLASSTYPE_VBASECLASSES (type
) = CLASSTYPE_VBASECLASSES (pattern
);
5008 /* Pretend that the type is complete, so that we will look
5009 inside it during name lookup and such. */
5010 TYPE_SIZE (type
) = bitsize_zero_node
;
5014 /* If we've recursively instantiated too many templates, stop. */
5015 if (! push_tinst_level (type
))
5018 /* Now we're really doing the instantiation. Mark the type as in
5019 the process of being defined. */
5020 TYPE_BEING_DEFINED (type
) = 1;
5022 maybe_push_to_top_level (uses_template_parms (type
));
5026 /* This TYPE is actually a instantiation of of a partial
5027 specialization. We replace the innermost set of ARGS with
5028 the arguments appropriate for substitution. For example,
5031 template <class T> struct S {};
5032 template <class T> struct S<T*> {};
5034 and supposing that we are instantiating S<int*>, ARGS will
5035 present be {int*} but we need {int}. */
5037 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
5040 /* If there were multiple levels in ARGS, replacing the
5041 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5042 want, so we make a copy first. */
5043 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
5045 args
= copy_node (args
);
5046 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
5052 if (flag_external_templates
)
5054 if (flag_alt_external_templates
)
5056 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
5057 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
5061 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
5062 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5063 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
5068 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
5071 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
5072 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
5073 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
5074 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
5075 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
5076 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
5077 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
5078 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
5079 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
5080 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
5081 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
5082 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5083 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5084 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5085 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5086 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
5087 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern
);
5088 TYPE_USES_MULTIPLE_INHERITANCE (type
)
5089 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
5090 TYPE_USES_VIRTUAL_BASECLASSES (type
)
5091 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
5092 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5093 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5094 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
5095 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5096 if (ANON_AGGR_TYPE_P (pattern
))
5097 SET_ANON_AGGR_TYPE_P (type
);
5099 if (TYPE_BINFO_BASETYPES (pattern
))
5101 tree base_list
= NULL_TREE
;
5102 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
5105 /* Substitute into each of the bases to determine the actual
5107 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
5113 pbase
= TREE_VEC_ELT (pbases
, i
);
5115 /* Substitute to figure out the base class. */
5116 base
= tsubst (BINFO_TYPE (pbase
), args
, tf_error
, NULL_TREE
);
5117 if (base
== error_mark_node
)
5120 /* Calculate the correct access node. */
5121 if (TREE_VIA_VIRTUAL (pbase
))
5123 if (TREE_VIA_PUBLIC (pbase
))
5124 access
= access_public_virtual_node
;
5125 else if (TREE_VIA_PROTECTED (pbase
))
5126 access
= access_protected_virtual_node
;
5128 access
= access_private_virtual_node
;
5132 if (TREE_VIA_PUBLIC (pbase
))
5133 access
= access_public_node
;
5134 else if (TREE_VIA_PROTECTED (pbase
))
5135 access
= access_protected_node
;
5137 access
= access_private_node
;
5140 base_list
= tree_cons (access
, base
, base_list
);
5143 /* The list is now in reverse order; correct that. */
5144 base_list
= nreverse (base_list
);
5146 /* Now call xref_basetypes to set up all the base-class
5148 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
5149 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
5150 ? class_type_node
: record_type_node
)
5152 DECL_NAME (TYPE_NAME (pattern
)),
5157 /* Now that our base classes are set up, enter the scope of the
5158 class, so that name lookups into base classes, etc. will work
5159 correctly. This is precisely analogous to what we do in
5160 begin_class_definition when defining an ordinary non-template
5162 pushclass (type
, 1);
5164 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
5166 tree tag
= TREE_VALUE (t
);
5167 tree name
= TYPE_IDENTIFIER (tag
);
5170 newtag
= tsubst (tag
, args
, tf_error
, NULL_TREE
);
5171 my_friendly_assert (newtag
!= error_mark_node
, 20010206);
5172 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5174 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
5175 /* Unfortunately, lookup_template_class sets
5176 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5177 instantiation (i.e., for the type of a member template
5178 class nested within a template class.) This behavior is
5179 required for maybe_process_partial_specialization to work
5180 correctly, but is not accurate in this case; the TAG is not
5181 an instantiation of anything. (The corresponding
5182 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5183 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5185 /* Now, we call pushtag to put this NEWTAG into the scope of
5186 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5187 pushtag calling push_template_decl. We don't have to do
5188 this for enums because it will already have been done in
5191 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5192 pushtag (name
, newtag
, /*globalize=*/0);
5196 /* Don't replace enum constants here. */
5197 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
5198 if (TREE_CODE (t
) != CONST_DECL
)
5202 /* The the file and line for this declaration, to assist in
5203 error message reporting. Since we called push_tinst_level
5204 above, we don't need to restore these. */
5205 lineno
= DECL_SOURCE_LINE (t
);
5206 input_filename
= DECL_SOURCE_FILE (t
);
5208 r
= tsubst (t
, args
, tf_error
| tf_warning
, NULL_TREE
);
5209 if (TREE_CODE (r
) == VAR_DECL
)
5213 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5214 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5215 tf_error
| tf_warning
, NULL_TREE
);
5219 finish_static_data_member_decl (r
, init
,
5220 /*asmspec_tree=*/NULL_TREE
,
5223 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5224 check_static_variable_definition (r
, TREE_TYPE (r
));
5226 else if (TREE_CODE (r
) == FIELD_DECL
)
5228 /* Determine whether R has a valid type and can be
5229 completed later. If R is invalid, then it is replaced
5230 by error_mark_node so that it will not be added to
5232 tree rtype
= TREE_TYPE (r
);
5233 if (!can_complete_type_without_circularity (rtype
))
5235 incomplete_type_error (r
, rtype
);
5236 r
= error_mark_node
;
5240 /* R will have a TREE_CHAIN if and only if it has already been
5241 processed by finish_member_declaration. This can happen
5242 if, for example, it is a TYPE_DECL for a class-scoped
5243 ENUMERAL_TYPE; such a thing will already have been added to
5244 the field list by tsubst_enum above. */
5245 if (!TREE_CHAIN (r
))
5247 set_current_access_from_decl (r
);
5248 finish_member_declaration (r
);
5252 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5253 for this instantiation. */
5254 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
5256 tree r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
5257 set_current_access_from_decl (r
);
5258 grok_special_member_properties (r
);
5259 finish_member_declaration (r
);
5262 /* Construct the DECL_FRIENDLIST for the new class type. */
5263 typedecl
= TYPE_MAIN_DECL (type
);
5264 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
5270 for (friends
= TREE_VALUE (t
);
5271 friends
!= NULL_TREE
;
5272 friends
= TREE_CHAIN (friends
))
5273 if (TREE_PURPOSE (friends
) == error_mark_node
)
5275 tsubst_friend_function (TREE_VALUE (friends
),
5281 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
5285 tree friend_type
= TREE_VALUE (t
);
5286 tree new_friend_type
;
5288 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5289 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5290 else if (uses_template_parms (friend_type
))
5291 new_friend_type
= tsubst (friend_type
, args
,
5292 tf_error
| tf_warning
, NULL_TREE
);
5295 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5297 /* The call to xref_tag_from_type does injection for friend
5299 push_nested_namespace (ns
);
5301 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5302 pop_nested_namespace (ns
);
5305 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5306 /* Trick make_friend_class into realizing that the friend
5307 we're adding is a template, not an ordinary class. It's
5308 important that we use make_friend_class since it will
5309 perform some error-checking and output cross-reference
5311 ++processing_template_decl
;
5313 if (new_friend_type
!= error_mark_node
)
5314 make_friend_class (type
, new_friend_type
);
5316 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5317 --processing_template_decl
;
5320 /* Now that TYPE_FIELDS and TYPE_METHODS are set up. We can
5321 instantiate templates used by this class. */
5322 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5323 if (TREE_CODE (t
) == FIELD_DECL
)
5325 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5326 require_complete_type (t
);
5329 /* Set the file and line number information to whatever is given for
5330 the class itself. This puts error messages involving generated
5331 implicit functions at a predictable point, and the same point
5332 that would be used for non-template classes. */
5333 lineno
= DECL_SOURCE_LINE (typedecl
);
5334 input_filename
= DECL_SOURCE_FILE (typedecl
);
5336 unreverse_member_declarations (type
);
5337 finish_struct_1 (type
);
5338 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5340 /* Clear this now so repo_template_used is happy. */
5341 TYPE_BEING_DEFINED (type
) = 0;
5342 repo_template_used (type
);
5344 /* Now that the class is complete, instantiate default arguments for
5345 any member functions. We don't do this earlier because the
5346 default arguments may reference members of the class. */
5347 if (!PRIMARY_TEMPLATE_P (template))
5348 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5349 if (TREE_CODE (t
) == FUNCTION_DECL
5350 /* Implicitly generated member functions will not have template
5351 information; they are not instantiations, but instead are
5352 created "fresh" for each instantiation. */
5353 && DECL_TEMPLATE_INFO (t
))
5354 tsubst_default_arguments (t
);
5357 pop_from_top_level ();
5367 if (t1
== NULL_TREE
)
5368 return t2
== NULL_TREE
;
5369 if (t2
== NULL_TREE
)
5371 /* Don't care if one declares its arg const and the other doesn't -- the
5372 main variant of the arg type is all that matters. */
5373 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5374 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5376 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5379 /* If arg is a non-type template parameter that does not depend on template
5380 arguments, fold it like we weren't in the body of a template. */
5383 maybe_fold_nontype_arg (arg
)
5386 if (arg
&& !TYPE_P (arg
) && !uses_template_parms (arg
))
5388 /* Sometimes, one of the args was an expression involving a
5389 template constant parameter, like N - 1. Now that we've
5390 tsubst'd, we might have something like 2 - 1. This will
5391 confuse lookup_template_class, so we do constant folding
5392 here. We have to unset processing_template_decl, to
5393 fool build_expr_from_tree() into building an actual
5396 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5397 as simple as it's going to get, and trying to reprocess
5398 the trees will break. */
5399 if (!TREE_TYPE (arg
))
5401 int saved_processing_template_decl
= processing_template_decl
;
5402 processing_template_decl
= 0;
5403 arg
= build_expr_from_tree (arg
);
5404 processing_template_decl
= saved_processing_template_decl
;
5412 /* Substitute ARGS into the vector of template arguments T. */
5415 tsubst_template_arg_vector (t
, args
, complain
)
5418 tsubst_flags_t complain
;
5420 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5421 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5423 memset ((char *) elts
, 0, len
* sizeof (tree
));
5425 for (i
= 0; i
< len
; i
++)
5427 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5428 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5429 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5432 elts
[i
] = maybe_fold_nontype_arg
5433 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5436 if (elts
[i
] == error_mark_node
)
5437 return error_mark_node
;
5439 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5446 t
= make_tree_vec (len
);
5447 for (i
= 0; i
< len
; i
++)
5448 TREE_VEC_ELT (t
, i
) = elts
[i
];
5453 /* Return the result of substituting ARGS into the template parameters
5454 given by PARMS. If there are m levels of ARGS and m + n levels of
5455 PARMS, then the result will contain n levels of PARMS. For
5456 example, if PARMS is `template <class T> template <class U>
5457 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5458 result will be `template <int*, double, class V>'. */
5461 tsubst_template_parms (parms
, args
, complain
)
5464 tsubst_flags_t complain
;
5469 for (new_parms
= &r
;
5470 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5471 new_parms
= &(TREE_CHAIN (*new_parms
)),
5472 parms
= TREE_CHAIN (parms
))
5475 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5478 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5480 tree tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
5481 tree default_value
= TREE_PURPOSE (tuple
);
5482 tree parm_decl
= TREE_VALUE (tuple
);
5484 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
5485 default_value
= tsubst_expr (default_value
, args
,
5486 complain
, NULL_TREE
);
5487 tuple
= build_tree_list (maybe_fold_nontype_arg (default_value
),
5489 TREE_VEC_ELT (new_vec
, i
) = tuple
;
5493 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
5494 - TMPL_ARGS_DEPTH (args
)),
5495 new_vec
, NULL_TREE
);
5501 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5502 type T. If T is not an aggregate or enumeration type, it is
5503 handled as if by tsubst. IN_DECL is as for tsubst. If
5504 ENTERING_SCOPE is non-zero, T is the context for a template which
5505 we are presently tsubst'ing. Return the substituted value. */
5508 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5511 tsubst_flags_t complain
;
5518 switch (TREE_CODE (t
))
5521 if (TYPE_PTRMEMFUNC_P (t
))
5522 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
5524 /* else fall through */
5527 if (TYPE_TEMPLATE_INFO (t
))
5533 /* First, determine the context for the type we are looking
5535 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5536 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5538 in_decl
, /*entering_scope=*/1);
5540 context
= NULL_TREE
;
5542 /* Then, figure out what arguments are appropriate for the
5543 type we are trying to find. For example, given:
5545 template <class T> struct S;
5546 template <class T, class U> void f(T, U) { S<U> su; }
5548 and supposing that we are instantiating f<int, double>,
5549 then our ARGS will be {int, double}, but, when looking up
5550 S we only want {double}. */
5551 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5553 if (argvec
== error_mark_node
)
5554 return error_mark_node
;
5556 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5557 entering_scope
, complain
);
5559 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
5562 /* This is not a template type, so there's nothing to do. */
5566 return tsubst (t
, args
, complain
, in_decl
);
5570 /* Substitute into the default argument ARG (a default argument for
5571 FN), which has the indicated TYPE. */
5574 tsubst_default_argument (fn
, type
, arg
)
5579 /* This default argument came from a template. Instantiate the
5580 default argument here, not in tsubst. In the case of
5589 we must be careful to do name lookup in the scope of S<T>,
5590 rather than in the current class. */
5591 if (DECL_CLASS_SCOPE_P (fn
))
5592 pushclass (DECL_CONTEXT (fn
), 2);
5594 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
5595 tf_error
| tf_warning
, NULL_TREE
);
5597 if (DECL_CLASS_SCOPE_P (fn
))
5600 /* Make sure the default argument is reasonable. */
5601 arg
= check_default_argument (type
, arg
);
5606 /* Substitute into all the default arguments for FN. */
5609 tsubst_default_arguments (fn
)
5615 tmpl_args
= DECL_TI_ARGS (fn
);
5617 /* If this function is not yet instantiated, we certainly don't need
5618 its default arguments. */
5619 if (uses_template_parms (tmpl_args
))
5622 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5624 arg
= TREE_CHAIN (arg
))
5625 if (TREE_PURPOSE (arg
))
5626 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5628 TREE_PURPOSE (arg
));
5631 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5632 (already computed) substitution of ARGS into TREE_TYPE (T), if
5633 appropriate. Return the result of the substitution. Issue error
5634 and warning messages under control of COMPLAIN. */
5637 tsubst_decl (t
, args
, type
, complain
)
5641 tsubst_flags_t complain
;
5644 const char *saved_filename
;
5648 my_friendly_assert (complain
& tf_error
, 20011214);
5650 /* Set the filename and linenumber to improve error-reporting. */
5651 saved_lineno
= lineno
;
5652 saved_filename
= input_filename
;
5653 lineno
= DECL_SOURCE_LINE (t
);
5654 input_filename
= DECL_SOURCE_FILE (t
);
5656 switch (TREE_CODE (t
))
5660 /* We can get here when processing a member template function
5661 of a template class. */
5662 tree decl
= DECL_TEMPLATE_RESULT (t
);
5664 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5666 if (!is_template_template_parm
)
5668 /* We might already have an instance of this template.
5669 The ARGS are for the surrounding class type, so the
5670 full args contain the tsubst'd args for the context,
5671 plus the innermost args from the template decl. */
5672 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5673 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5674 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
5677 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5680 /* tsubst_template_arg_vector doesn't copy the vector if
5681 nothing changed. But, *something* should have
5683 my_friendly_assert (full_args
!= tmpl_args
, 0);
5685 spec
= retrieve_specialization (t
, full_args
);
5686 if (spec
!= NULL_TREE
)
5693 /* Make a new template decl. It will be similar to the
5694 original, but will record the current template arguments.
5695 We also create a new function declaration, which is just
5696 like the old one, but points to this new template, rather
5697 than the old one. */
5699 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5700 TREE_CHAIN (r
) = NULL_TREE
;
5702 if (is_template_template_parm
)
5704 tree new_decl
= tsubst (decl
, args
, complain
, in_decl
);
5705 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5706 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5711 = tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5713 /*entering_scope=*/1);
5714 DECL_VIRTUAL_CONTEXT (r
)
5715 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5717 /*entering_scope=*/1);
5718 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5720 if (TREE_CODE (decl
) == TYPE_DECL
)
5722 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
5723 TREE_TYPE (r
) = new_type
;
5724 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5725 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5726 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5730 tree new_decl
= tsubst (decl
, args
, complain
, in_decl
);
5732 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5733 DECL_TI_TEMPLATE (new_decl
) = r
;
5734 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5735 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5738 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5739 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5740 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5742 /* The template parameters for this new template are all the
5743 template parameters for the old template, except the
5744 outermost level of parameters. */
5745 DECL_TEMPLATE_PARMS (r
)
5746 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5749 if (PRIMARY_TEMPLATE_P (t
))
5750 DECL_PRIMARY_TEMPLATE (r
) = r
;
5752 /* We don't partially instantiate partial specializations. */
5753 if (TREE_CODE (decl
) == TYPE_DECL
)
5756 /* Record this partial instantiation. */
5757 register_specialization (r
, t
,
5758 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)));
5766 tree argvec
= NULL_TREE
;
5773 /* Nobody should be tsubst'ing into non-template functions. */
5774 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5776 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5780 /* Calculate the most general template of which R is a
5781 specialization, and the complete set of arguments used to
5783 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5785 = tsubst_template_arg_vector (DECL_TI_ARGS
5786 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5789 /* Check to see if we already have this specialization. */
5790 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5798 /* We can see more levels of arguments than parameters if
5799 there was a specialization of a member template, like
5802 template <class T> struct S { template <class U> void f(); }
5803 template <> template <class U> void S<int>::f(U);
5805 Here, we'll be substituting into the specialization,
5806 because that's where we can find the code we actually
5807 want to generate, but we'll have enough arguments for
5808 the most general template.
5810 We also deal with the peculiar case:
5812 template <class T> struct S {
5813 template <class U> friend void f();
5815 template <class U> void f() {}
5817 template void f<double>();
5819 Here, the ARGS for the instantiation of will be {int,
5820 double}. But, we only need as many ARGS as there are
5821 levels of template parameters in CODE_PATTERN. We are
5822 careful not to get fooled into reducing the ARGS in
5825 template <class T> struct S { template <class U> void f(U); }
5826 template <class T> template <> void S<T>::f(int) {}
5828 which we can spot because the pattern will be a
5829 specialization in this case. */
5830 args_depth
= TMPL_ARGS_DEPTH (args
);
5832 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5833 if (args_depth
> parms_depth
5834 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5835 args
= get_innermost_template_args (args
, parms_depth
);
5839 /* This special case arises when we have something like this:
5841 template <class T> struct S {
5842 friend void f<int>(int, double);
5845 Here, the DECL_TI_TEMPLATE for the friend declaration
5846 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5847 being called from tsubst_friend_function, and we want
5848 only to create a new decl (R) with appropriate types so
5849 that we can call determine_specialization. */
5850 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5852 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5853 == IDENTIFIER_NODE
), 0);
5854 gen_tmpl
= NULL_TREE
;
5857 if (DECL_CLASS_SCOPE_P (t
))
5859 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5863 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5865 /*entering_scope=*/1);
5870 ctx
= DECL_CONTEXT (t
);
5872 type
= tsubst (type
, args
, complain
, in_decl
);
5873 if (type
== error_mark_node
)
5874 return error_mark_node
;
5876 /* We do NOT check for matching decls pushed separately at this
5877 point, as they may not represent instantiations of this
5878 template, and in any case are considered separate under the
5881 DECL_USE_TEMPLATE (r
) = 0;
5882 TREE_TYPE (r
) = type
;
5883 /* Clear out the mangled name and RTL for the instantiation. */
5884 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
5885 SET_DECL_RTL (r
, NULL_RTX
);
5887 DECL_CONTEXT (r
) = ctx
;
5888 DECL_VIRTUAL_CONTEXT (r
)
5889 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5891 /*entering_scope=*/1);
5893 if (member
&& DECL_CONV_FN_P (r
))
5894 /* Type-conversion operator. Reconstruct the name, in
5895 case it's the name of one of the template's parameters. */
5896 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
5898 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5900 DECL_RESULT (r
) = NULL_TREE
;
5902 TREE_STATIC (r
) = 0;
5903 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5904 DECL_EXTERNAL (r
) = 1;
5905 DECL_INTERFACE_KNOWN (r
) = 0;
5906 DECL_DEFER_OUTPUT (r
) = 0;
5907 TREE_CHAIN (r
) = NULL_TREE
;
5908 DECL_PENDING_INLINE_INFO (r
) = 0;
5909 DECL_PENDING_INLINE_P (r
) = 0;
5910 DECL_SAVED_TREE (r
) = NULL_TREE
;
5912 if (DECL_CLONED_FUNCTION (r
))
5914 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
5916 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
5917 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
5920 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5921 this in the special friend case mentioned above where
5922 GEN_TMPL is NULL. */
5925 DECL_TEMPLATE_INFO (r
)
5926 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5927 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5928 register_specialization (r
, gen_tmpl
, argvec
);
5930 /* We're not supposed to instantiate default arguments
5931 until they are called, for a template. But, for a
5934 template <class T> void f ()
5935 { extern void g(int i = T()); }
5937 we should do the substitution when the template is
5938 instantiated. We handle the member function case in
5939 instantiate_class_template since the default arguments
5940 might refer to other members of the class. */
5942 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
5943 && !uses_template_parms (argvec
))
5944 tsubst_default_arguments (r
);
5947 /* Copy the list of befriending classes. */
5948 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5950 friends
= &TREE_CHAIN (*friends
))
5952 *friends
= copy_node (*friends
);
5953 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5958 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
5960 maybe_retrofit_in_chrg (r
);
5961 if (DECL_CONSTRUCTOR_P (r
))
5962 grok_ctor_properties (ctx
, r
);
5963 /* If this is an instantiation of a member template, clone it.
5964 If it isn't, that'll be handled by
5965 clone_constructors_and_destructors. */
5966 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
5967 clone_function_decl (r
, /*update_method_vec_p=*/0);
5969 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5970 grok_op_properties (r
, DECL_FRIEND_P (r
));
5977 if (DECL_TEMPLATE_PARM_P (t
))
5978 SET_DECL_TEMPLATE_PARM_P (r
);
5980 TREE_TYPE (r
) = type
;
5981 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
5983 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5984 DECL_INITIAL (r
) = TREE_TYPE (r
);
5986 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5989 DECL_CONTEXT (r
) = NULL_TREE
;
5990 if (!DECL_TEMPLATE_PARM_P (r
) && PROMOTE_PROTOTYPES
5991 && INTEGRAL_TYPE_P (type
)
5992 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5993 DECL_ARG_TYPE (r
) = integer_type_node
;
5995 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5996 complain
, TREE_CHAIN (t
));
6003 TREE_TYPE (r
) = type
;
6004 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6006 /* We don't have to set DECL_CONTEXT here; it is set by
6007 finish_member_declaration. */
6008 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
6010 TREE_CHAIN (r
) = NULL_TREE
;
6011 if (VOID_TYPE_P (type
))
6012 cp_error_at ("instantiation of `%D' as type `%T'", r
, type
);
6020 = tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
6021 TREE_CHAIN (r
) = NULL_TREE
;
6026 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
6027 || t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
6029 /* If this is the canonical decl, we don't have to mess with
6030 instantiations, and often we can't (for typename, template
6031 type parms and such). Note that TYPE_NAME is not correct for
6032 the above test if we've copied the type for a typedef. */
6033 r
= TYPE_NAME (type
);
6041 tree argvec
= NULL_TREE
;
6042 tree gen_tmpl
= NULL_TREE
;
6044 tree tmpl
= NULL_TREE
;
6048 /* Assume this is a non-local variable. */
6051 if (TYPE_P (CP_DECL_CONTEXT (t
)))
6052 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6054 in_decl
, /*entering_scope=*/1);
6055 else if (DECL_NAMESPACE_SCOPE_P (t
))
6056 ctx
= DECL_CONTEXT (t
);
6059 /* Subsequent calls to pushdecl will fill this in. */
6064 /* Check to see if we already have this specialization. */
6067 tmpl
= DECL_TI_TEMPLATE (t
);
6068 gen_tmpl
= most_general_template (tmpl
);
6069 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
6070 spec
= retrieve_specialization (gen_tmpl
, argvec
);
6073 spec
= retrieve_local_specialization (t
);
6082 TREE_TYPE (r
) = type
;
6083 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6084 DECL_CONTEXT (r
) = ctx
;
6085 /* Clear out the mangled name and RTL for the instantiation. */
6086 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
6087 SET_DECL_RTL (r
, NULL_RTX
);
6089 /* Don't try to expand the initializer until someone tries to use
6090 this variable; otherwise we run into circular dependencies. */
6091 DECL_INITIAL (r
) = NULL_TREE
;
6092 SET_DECL_RTL (r
, NULL_RTX
);
6093 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
6095 /* Even if the original location is out of scope, the newly
6096 substituted one is not. */
6097 if (TREE_CODE (r
) == VAR_DECL
)
6098 DECL_DEAD_FOR_LOCAL (r
) = 0;
6102 /* A static data member declaration is always marked
6103 external when it is declared in-class, even if an
6104 initializer is present. We mimic the non-template
6106 DECL_EXTERNAL (r
) = 1;
6108 register_specialization (r
, gen_tmpl
, argvec
);
6109 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
6110 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6113 register_local_specialization (r
, t
);
6115 TREE_CHAIN (r
) = NULL_TREE
;
6116 if (TREE_CODE (r
) == VAR_DECL
&& VOID_TYPE_P (type
))
6117 cp_error_at ("instantiation of `%D' as type `%T'", r
, type
);
6125 /* Restore the file and line information. */
6126 lineno
= saved_lineno
;
6127 input_filename
= saved_filename
;
6132 /* Substitue into the ARG_TYPES of a function type. */
6135 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
6138 tsubst_flags_t complain
;
6141 tree remaining_arg_types
;
6144 if (!arg_types
|| arg_types
== void_list_node
)
6147 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6148 args
, complain
, in_decl
);
6149 if (remaining_arg_types
== error_mark_node
)
6150 return error_mark_node
;
6152 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6153 if (type
== error_mark_node
)
6154 return error_mark_node
;
6155 if (VOID_TYPE_P (type
))
6157 if (complain
& tf_error
)
6159 error ("invalid parameter type `%T'", type
);
6161 cp_error_at ("in declaration `%D'", in_decl
);
6163 return error_mark_node
;
6166 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6167 top-level qualifiers as required. */
6168 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6170 /* Note that we do not substitute into default arguments here. The
6171 standard mandates that they be instantiated only when needed,
6172 which is done in build_over_call. */
6173 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6174 remaining_arg_types
);
6178 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6179 *not* handle the exception-specification for FNTYPE, because the
6180 initial substitution of explicitly provided template parameters
6181 during argument deduction forbids substitution into the
6182 exception-specification:
6186 All references in the function type of the function template to the
6187 corresponding template parameters are replaced by the specified tem-
6188 plate argument values. If a substitution in a template parameter or
6189 in the function type of the function template results in an invalid
6190 type, type deduction fails. [Note: The equivalent substitution in
6191 exception specifications is done only when the function is instanti-
6192 ated, at which point a program is ill-formed if the substitution
6193 results in an invalid type.] */
6196 tsubst_function_type (t
, args
, complain
, in_decl
)
6199 tsubst_flags_t complain
;
6206 /* The TYPE_CONTEXT is not used for function/method types. */
6207 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6209 /* Substitute the return type. */
6210 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6211 if (return_type
== error_mark_node
)
6212 return error_mark_node
;
6214 /* Substitue the argument types. */
6215 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6217 if (arg_types
== error_mark_node
)
6218 return error_mark_node
;
6220 /* Construct a new type node and return it. */
6221 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6222 fntype
= build_function_type (return_type
, arg_types
);
6225 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6226 if (! IS_AGGR_TYPE (r
))
6230 Type deduction may fail for any of the following
6233 -- Attempting to create "pointer to member of T" when T
6234 is not a class type. */
6235 if (complain
& tf_error
)
6236 error ("creating pointer to member function of non-class type `%T'",
6238 return error_mark_node
;
6241 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6244 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
6245 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6250 /* Substitute into the PARMS of a call-declarator. */
6253 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
6256 tsubst_flags_t complain
;
6263 if (!parms
|| parms
== void_list_node
)
6266 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6267 args
, complain
, in_decl
);
6269 /* Figure out the type of this parameter. */
6270 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6272 /* Figure out the default argument as well. Note that we use
6273 tsubst_expr since the default argument is really an expression. */
6274 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6276 /* Chain this parameter on to the front of those we have already
6277 processed. We don't use hash_tree_cons because that function
6278 doesn't check TREE_PARMLIST. */
6279 new_parms
= tree_cons (defarg
, type
, new_parms
);
6281 /* And note that these are parameters. */
6282 TREE_PARMLIST (new_parms
) = 1;
6287 /* Take the tree structure T and replace template parameters used
6288 therein with the argument vector ARGS. IN_DECL is an associated
6289 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6290 Issue error and warning messages under control of COMPLAIN. Note
6291 that we must be relatively non-tolerant of extensions here, in
6292 order to preserve conformance; if we allow substitutions that
6293 should not be allowed, we may allow argument deductions that should
6294 not succeed, and therefore report ambiguous overload situations
6295 where there are none. In theory, we could allow the substitution,
6296 but indicate that it should have failed, and allow our caller to
6297 make sure that the right thing happens, but we don't try to do this
6300 This function is used for dealing with types, decls and the like;
6301 for expressions, use tsubst_expr or tsubst_copy. */
6304 tsubst (t
, args
, complain
, in_decl
)
6306 tsubst_flags_t complain
;
6311 if (t
== NULL_TREE
|| t
== error_mark_node
6312 || t
== integer_type_node
6313 || t
== void_type_node
6314 || t
== char_type_node
6315 || TREE_CODE (t
) == NAMESPACE_DECL
)
6318 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6319 type
= IDENTIFIER_TYPE_VALUE (t
);
6321 type
= TREE_TYPE (t
);
6322 if (type
== unknown_type_node
)
6325 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6326 && TREE_CODE (t
) != TYPENAME_TYPE
6327 && TREE_CODE (t
) != TEMPLATE_DECL
6328 && TREE_CODE (t
) != IDENTIFIER_NODE
6329 && TREE_CODE (t
) != FUNCTION_TYPE
6330 && TREE_CODE (t
) != METHOD_TYPE
)
6331 type
= tsubst (type
, args
, complain
, in_decl
);
6332 if (type
== error_mark_node
)
6333 return error_mark_node
;
6336 return tsubst_decl (t
, args
, type
, complain
);
6338 switch (TREE_CODE (t
))
6343 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6344 /*entering_scope=*/0);
6347 case IDENTIFIER_NODE
:
6359 if (t
== integer_type_node
)
6362 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6363 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6367 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6369 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6370 if (max
== error_mark_node
)
6371 return error_mark_node
;
6373 /* See if we can reduce this expression to something simpler. */
6374 max
= maybe_fold_nontype_arg (max
);
6375 if (!processing_template_decl
)
6376 max
= decl_constant_value (max
);
6378 if (processing_template_decl
6379 /* When providing explicit arguments to a template
6380 function, but leaving some arguments for subsequent
6381 deduction, MAX may be template-dependent even if we're
6382 not PROCESSING_TEMPLATE_DECL. We still need to check for
6383 template parms, though; MAX won't be an INTEGER_CST for
6384 dynamic arrays, either. */
6385 || (TREE_CODE (max
) != INTEGER_CST
6386 && uses_template_parms (max
)))
6388 tree itype
= make_node (INTEGER_TYPE
);
6389 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6390 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6395 if (integer_zerop (omax
))
6397 /* Still allow an explicit array of size zero. */
6399 pedwarn ("creating array with size zero");
6401 else if (integer_zerop (max
)
6402 || (TREE_CODE (max
) == INTEGER_CST
6403 && INT_CST_LT (max
, integer_zero_node
)))
6407 Type deduction may fail for any of the following
6410 Attempting to create an array with a size that is
6411 zero or negative. */
6412 if (complain
& tf_error
)
6413 error ("creating array with size zero (`%E')", max
);
6415 return error_mark_node
;
6418 return compute_array_index_type (NULL_TREE
, max
);
6421 case TEMPLATE_TYPE_PARM
:
6422 case TEMPLATE_TEMPLATE_PARM
:
6423 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6424 case TEMPLATE_PARM_INDEX
:
6432 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6433 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6434 || TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6436 idx
= TEMPLATE_TYPE_IDX (t
);
6437 level
= TEMPLATE_TYPE_LEVEL (t
);
6441 idx
= TEMPLATE_PARM_IDX (t
);
6442 level
= TEMPLATE_PARM_LEVEL (t
);
6445 if (TREE_VEC_LENGTH (args
) > 0)
6447 tree arg
= NULL_TREE
;
6449 levels
= TMPL_ARGS_DEPTH (args
);
6450 if (level
<= levels
)
6451 arg
= TMPL_ARG (args
, level
, idx
);
6453 if (arg
== error_mark_node
)
6454 return error_mark_node
;
6455 else if (arg
!= NULL_TREE
)
6457 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6459 my_friendly_assert (TYPE_P (arg
), 0);
6460 return cp_build_qualified_type_real
6461 (arg
, cp_type_quals (arg
) | cp_type_quals (t
),
6462 complain
| tf_ignore_bad_quals
);
6464 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6466 /* We are processing a type constructed from
6467 a template template parameter */
6468 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6469 args
, complain
, in_decl
);
6470 if (argvec
== error_mark_node
)
6471 return error_mark_node
;
6473 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6474 we are resolving nested-types in the signature of
6475 a member function templates.
6476 Otherwise ARG is a TEMPLATE_DECL and is the real
6477 template to be instantiated. */
6478 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6479 arg
= TYPE_NAME (arg
);
6481 r
= lookup_template_class (arg
,
6484 /*entering_scope=*/0,
6486 return cp_build_qualified_type_real
6487 (r
, TYPE_QUALS (t
), complain
);
6490 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6498 /* This can happen during the attempted tsubst'ing in
6499 unify. This means that we don't yet have any information
6500 about the template parameter in question. */
6503 /* If we get here, we must have been looking at a parm for a
6504 more deeply nested template. Make a new version of this
6505 template parameter, but with a lower level. */
6506 switch (TREE_CODE (t
))
6508 case TEMPLATE_TYPE_PARM
:
6509 case TEMPLATE_TEMPLATE_PARM
:
6510 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6511 if (cp_type_quals (t
))
6513 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
6514 r
= cp_build_qualified_type_real
6515 (r
, cp_type_quals (t
),
6516 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6517 ? tf_ignore_bad_quals
: 0));
6522 TEMPLATE_TYPE_PARM_INDEX (r
)
6523 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6525 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6526 TYPE_MAIN_VARIANT (r
) = r
;
6527 TYPE_POINTER_TO (r
) = NULL_TREE
;
6528 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6530 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6532 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6534 if (argvec
== error_mark_node
)
6535 return error_mark_node
;
6537 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6538 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
6543 case TEMPLATE_PARM_INDEX
:
6544 r
= reduce_template_parm_level (t
, type
, levels
);
6556 tree purpose
, value
, chain
, result
;
6558 if (t
== void_list_node
)
6561 purpose
= TREE_PURPOSE (t
);
6564 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6565 if (purpose
== error_mark_node
)
6566 return error_mark_node
;
6568 value
= TREE_VALUE (t
);
6571 value
= tsubst (value
, args
, complain
, in_decl
);
6572 if (value
== error_mark_node
)
6573 return error_mark_node
;
6575 chain
= TREE_CHAIN (t
);
6576 if (chain
&& chain
!= void_type_node
)
6578 chain
= tsubst (chain
, args
, complain
, in_decl
);
6579 if (chain
== error_mark_node
)
6580 return error_mark_node
;
6582 if (purpose
== TREE_PURPOSE (t
)
6583 && value
== TREE_VALUE (t
)
6584 && chain
== TREE_CHAIN (t
))
6586 if (TREE_PARMLIST (t
))
6588 result
= tree_cons (purpose
, value
, chain
);
6589 TREE_PARMLIST (result
) = 1;
6592 result
= hash_tree_cons (purpose
, value
, chain
);
6596 if (type
!= NULL_TREE
)
6598 /* A binfo node. We always need to make a copy, of the node
6599 itself and of its BINFO_BASETYPES. */
6603 /* Make sure type isn't a typedef copy. */
6604 type
= BINFO_TYPE (TYPE_BINFO (type
));
6606 TREE_TYPE (t
) = complete_type (type
);
6607 if (IS_AGGR_TYPE (type
))
6609 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6610 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6611 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6612 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6617 /* Otherwise, a vector of template arguments. */
6618 return tsubst_template_arg_vector (t
, args
, complain
);
6621 case REFERENCE_TYPE
:
6623 enum tree_code code
;
6625 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
6628 code
= TREE_CODE (t
);
6633 Type deduction may fail for any of the following
6636 -- Attempting to create a pointer to reference type.
6637 -- Attempting to create a reference to a reference type or
6638 a reference to void. */
6639 if (TREE_CODE (type
) == REFERENCE_TYPE
6640 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6642 static int last_line
= 0;
6643 static const char* last_file
= 0;
6645 /* We keep track of the last time we issued this error
6646 message to avoid spewing a ton of messages during a
6647 single bad template instantiation. */
6648 if (complain
& tf_error
6649 && (last_line
!= lineno
|| last_file
!= input_filename
))
6651 if (TREE_CODE (type
) == VOID_TYPE
)
6652 error ("forming reference to void");
6654 error ("forming %s to reference type `%T'",
6655 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6658 last_file
= input_filename
;
6661 return error_mark_node
;
6663 else if (code
== POINTER_TYPE
)
6665 r
= build_pointer_type (type
);
6666 if (TREE_CODE (type
) == METHOD_TYPE
)
6667 r
= build_ptrmemfunc_type (r
);
6670 r
= build_reference_type (type
);
6671 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6673 if (r
!= error_mark_node
)
6674 /* Will this ever be needed for TYPE_..._TO values? */
6681 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6682 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6686 Type deduction may fail for any of the following
6689 -- Attempting to create "pointer to member of T" when T
6690 is not a class type. */
6691 if (complain
& tf_error
)
6692 error ("creating pointer to member of non-class type `%T'", r
);
6693 return error_mark_node
;
6695 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6698 error ("creating pointer to member reference type `%T'", type
);
6700 return error_mark_node
;
6702 my_friendly_assert (TREE_CODE (type
) != METHOD_TYPE
, 20011231);
6703 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6704 /* This is really a method type. The cv qualifiers of the
6705 this pointer should _not_ be determined by the cv
6706 qualifiers of the class type. They should be held
6707 somewhere in the FUNCTION_TYPE, but we don't do that at
6708 the moment. Consider
6709 typedef void (Func) () const;
6711 template <typename T1> void Foo (Func T1::*);
6714 return build_cplus_method_type (TYPE_MAIN_VARIANT (r
),
6716 TYPE_ARG_TYPES (type
));
6718 return build_offset_type (r
, type
);
6726 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6727 if (fntype
== error_mark_node
)
6728 return error_mark_node
;
6730 /* Substitue the exception specification. */
6731 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6734 tree list
= NULL_TREE
;
6736 if (! TREE_VALUE (raises
))
6739 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6741 tree spec
= TREE_VALUE (raises
);
6743 spec
= tsubst (spec
, args
, complain
, in_decl
);
6744 if (spec
== error_mark_node
)
6746 list
= add_exception_specifier (list
, spec
, complain
);
6748 fntype
= build_exception_variant (fntype
, list
);
6754 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6755 if (domain
== error_mark_node
)
6756 return error_mark_node
;
6758 /* As an optimization, we avoid regenerating the array type if
6759 it will obviously be the same as T. */
6760 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6763 /* These checks should match the ones in grokdeclarator.
6767 The deduction may fail for any of the following reasons:
6769 -- Attempting to create an array with an element type that
6770 is void, a function type, or a reference type. */
6771 if (TREE_CODE (type
) == VOID_TYPE
6772 || TREE_CODE (type
) == FUNCTION_TYPE
6773 || TREE_CODE (type
) == REFERENCE_TYPE
)
6775 if (complain
& tf_error
)
6776 error ("creating array of `%T'", type
);
6777 return error_mark_node
;
6780 r
= build_cplus_array_type (type
, domain
);
6787 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6788 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6790 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6791 return error_mark_node
;
6793 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6799 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6800 if (e
== error_mark_node
)
6801 return error_mark_node
;
6803 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6808 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6809 in_decl
, /*entering_scope=*/1);
6810 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6813 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6814 return error_mark_node
;
6816 if (!IS_AGGR_TYPE (ctx
))
6818 if (complain
& tf_error
)
6819 error ("`%T' is not a class, struct, or union type",
6821 return error_mark_node
;
6823 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6825 /* Normally, make_typename_type does not require that the CTX
6826 have complete type in order to allow things like:
6828 template <class T> struct S { typename S<T>::X Y; };
6830 But, such constructs have already been resolved by this
6831 point, so here CTX really should have complete type, unless
6832 it's a partial instantiation. */
6833 ctx
= complete_type (ctx
);
6834 if (!COMPLETE_TYPE_P (ctx
))
6836 if (complain
& tf_error
)
6837 incomplete_type_error (NULL_TREE
, ctx
);
6838 return error_mark_node
;
6842 f
= make_typename_type (ctx
, f
,
6843 (complain
& tf_error
) | tf_keep_type_decl
);
6844 if (f
== error_mark_node
)
6846 if (TREE_CODE (f
) == TYPE_DECL
)
6848 complain
|= tf_ignore_bad_quals
;
6852 return cp_build_qualified_type_real
6853 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
6856 case UNBOUND_CLASS_TEMPLATE
:
6858 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6859 in_decl
, /*entering_scope=*/1);
6860 tree name
= TYPE_IDENTIFIER (t
);
6862 if (ctx
== error_mark_node
|| name
== error_mark_node
)
6863 return error_mark_node
;
6865 return make_unbound_class_template (ctx
, name
, complain
);
6870 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6871 if (e
== error_mark_node
)
6872 return error_mark_node
;
6873 return make_pointer_declarator (type
, e
);
6878 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6879 if (e
== error_mark_node
)
6880 return error_mark_node
;
6881 return make_reference_declarator (type
, e
);
6886 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6887 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6888 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6889 return error_mark_node
;
6891 return build_nt (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6896 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6897 tree e2
= (tsubst_call_declarator_parms
6898 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
6899 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
6902 if (e1
== error_mark_node
|| e2
== error_mark_node
6903 || e3
== error_mark_node
)
6904 return error_mark_node
;
6906 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
6911 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6912 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6913 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6914 return error_mark_node
;
6916 return build_nt (TREE_CODE (t
), e1
, e2
);
6921 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
, in_decl
);
6922 if (e1
== error_mark_node
)
6923 return error_mark_node
;
6925 return TREE_TYPE (e1
);
6929 sorry ("use of `%s' in template",
6930 tree_code_name
[(int) TREE_CODE (t
)]);
6931 return error_mark_node
;
6935 /* Like tsubst, but deals with expressions. This function just replaces
6936 template parms; to finish processing the resultant expression, use
6940 tsubst_copy (t
, args
, complain
, in_decl
)
6942 tsubst_flags_t complain
;
6945 enum tree_code code
;
6948 if (t
== NULL_TREE
|| t
== error_mark_node
)
6951 code
= TREE_CODE (t
);
6956 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6963 if (!DECL_CONTEXT (t
))
6964 /* This is a global enumeration constant. */
6967 /* Unfortunately, we cannot just call lookup_name here.
6970 template <int I> int f() {
6972 struct S { void g() { E e = a; } };
6975 When we instantiate f<7>::S::g(), say, lookup_name is not
6976 clever enough to find f<7>::a. */
6978 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6979 /*entering_scope=*/0);
6981 for (v
= TYPE_VALUES (enum_type
);
6984 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6985 return TREE_VALUE (v
);
6987 /* We didn't find the name. That should never happen; if
6988 name-lookup found it during preliminary parsing, we
6989 should find it again here during instantiation. */
6995 if (DECL_CONTEXT (t
))
6999 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
7000 /*entering_scope=*/1);
7001 if (ctx
!= DECL_CONTEXT (t
))
7002 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
7008 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
7009 t
= tsubst (t
, args
, complain
, in_decl
);
7014 if (is_member_template (t
))
7015 return tsubst (t
, args
, complain
, in_decl
);
7021 /* We must tsubst into a LOOKUP_EXPR in case the names to
7022 which it refers is a conversion operator; in that case the
7023 name will change. We avoid making unnecessary copies,
7026 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7028 if (id
!= TREE_OPERAND (t
, 0))
7030 r
= build_nt (LOOKUP_EXPR
, id
);
7031 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
7039 case REINTERPRET_CAST_EXPR
:
7040 case CONST_CAST_EXPR
:
7041 case STATIC_CAST_EXPR
:
7042 case DYNAMIC_CAST_EXPR
:
7045 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7046 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7050 case TRUTH_NOT_EXPR
:
7053 case CONVERT_EXPR
: /* Unary + */
7062 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7063 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7068 case TRUNC_DIV_EXPR
:
7070 case FLOOR_DIV_EXPR
:
7071 case ROUND_DIV_EXPR
:
7072 case EXACT_DIV_EXPR
:
7074 case BIT_ANDTC_EXPR
:
7077 case TRUNC_MOD_EXPR
:
7078 case FLOOR_MOD_EXPR
:
7079 case TRUTH_ANDIF_EXPR
:
7080 case TRUTH_ORIF_EXPR
:
7081 case TRUTH_AND_EXPR
:
7101 case PREDECREMENT_EXPR
:
7102 case PREINCREMENT_EXPR
:
7103 case POSTDECREMENT_EXPR
:
7104 case POSTINCREMENT_EXPR
:
7106 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7107 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7111 tree fn
= TREE_OPERAND (t
, 0);
7112 if (is_overloaded_fn (fn
))
7113 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
7115 /* Sometimes FN is a LOOKUP_EXPR. */
7116 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
7118 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7123 case METHOD_CALL_EXPR
:
7125 tree name
= TREE_OPERAND (t
, 0);
7126 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
7128 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7130 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7132 else if (TREE_CODE (name
) == SCOPE_REF
7133 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
7135 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7137 name
= TREE_OPERAND (name
, 1);
7138 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7140 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7141 name
= build_nt (SCOPE_REF
, base
, name
);
7144 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7146 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
7148 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
7153 /* This processing should really occur in tsubst_expr, However,
7154 tsubst_expr does not recurse into expressions, since it
7155 assumes that there aren't any statements inside them.
7156 Instead, it simply calls build_expr_from_tree. So, we need
7157 to expand the STMT_EXPR here. */
7158 if (!processing_template_decl
)
7160 tree stmt_expr
= begin_stmt_expr ();
7161 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
);
7162 return finish_stmt_expr (stmt_expr
);
7169 case PSEUDO_DTOR_EXPR
:
7172 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7173 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7174 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7181 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7182 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7183 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7184 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7191 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7192 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7193 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7194 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7198 case TEMPLATE_ID_EXPR
:
7200 /* Substituted template arguments */
7201 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7204 if (targs
&& TREE_CODE (targs
) == TREE_LIST
)
7207 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
7208 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
7213 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
7214 TREE_VEC_ELT (targs
, i
)
7215 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
7218 return lookup_template_function
7219 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7224 tree purpose
, value
, chain
;
7226 if (t
== void_list_node
)
7229 purpose
= TREE_PURPOSE (t
);
7231 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7232 value
= TREE_VALUE (t
);
7234 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7235 chain
= TREE_CHAIN (t
);
7236 if (chain
&& chain
!= void_type_node
)
7237 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7238 if (purpose
== TREE_PURPOSE (t
)
7239 && value
== TREE_VALUE (t
)
7240 && chain
== TREE_CHAIN (t
))
7242 return tree_cons (purpose
, value
, chain
);
7249 case TEMPLATE_TYPE_PARM
:
7250 case TEMPLATE_TEMPLATE_PARM
:
7251 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7252 case TEMPLATE_PARM_INDEX
:
7254 case REFERENCE_TYPE
:
7260 case UNBOUND_CLASS_TEMPLATE
:
7263 return tsubst (t
, args
, complain
, in_decl
);
7265 case IDENTIFIER_NODE
:
7266 if (IDENTIFIER_TYPENAME_P (t
))
7268 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7269 return mangle_conv_op_name_for_type (new_type
);
7277 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7278 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
7279 complain
, in_decl
));
7280 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7285 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
7287 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7294 /* Like tsubst_copy, but also does semantic processing. */
7297 tsubst_expr (t
, args
, complain
, in_decl
)
7299 tsubst_flags_t complain
;
7304 if (t
== NULL_TREE
|| t
== error_mark_node
)
7307 if (processing_template_decl
)
7308 return tsubst_copy (t
, args
, complain
, in_decl
);
7310 if (!statement_code_p (TREE_CODE (t
)))
7311 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7313 switch (TREE_CODE (t
))
7317 finish_named_return_value
7318 (TREE_OPERAND (t
, 0),
7319 tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7322 case CTOR_INITIALIZER
:
7324 tree member_init_list
;
7325 tree base_init_list
;
7329 = tsubst_initializer_list (TREE_OPERAND (t
, 0), args
);
7331 = tsubst_initializer_list (TREE_OPERAND (t
, 1), args
);
7332 emit_base_init (member_init_list
, base_init_list
);
7338 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
7339 args
, complain
, in_decl
));
7344 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7345 args
, complain
, in_decl
));
7350 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t
),
7351 args
, complain
, in_decl
));
7360 decl
= DECL_STMT_DECL (t
);
7361 if (TREE_CODE (decl
) == LABEL_DECL
)
7362 finish_label_decl (DECL_NAME (decl
));
7363 else if (TREE_CODE (decl
) == USING_DECL
)
7365 tree scope
= DECL_INITIAL (decl
);
7366 tree name
= DECL_NAME (decl
);
7368 scope
= tsubst_expr (scope
, args
, complain
, in_decl
);
7369 do_local_using_decl (build_nt (SCOPE_REF
, scope
, name
));
7373 init
= DECL_INITIAL (decl
);
7374 decl
= tsubst (decl
, args
, complain
, in_decl
);
7375 if (decl
!= error_mark_node
)
7377 if (TREE_CODE (decl
) != TYPE_DECL
)
7378 /* Make sure the type is instantiated now. */
7379 complete_type (TREE_TYPE (decl
));
7381 DECL_INITIAL (decl
) = error_mark_node
;
7382 /* By marking the declaration as instantiated, we avoid
7383 trying to instantiate it. Since instantiate_decl can't
7384 handle local variables, and since we've already done
7385 all that needs to be done, that's the right thing to
7387 if (TREE_CODE (decl
) == VAR_DECL
)
7388 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7389 maybe_push_decl (decl
);
7390 if (DECL_PRETTY_FUNCTION_P (decl
))
7392 /* For __PRETTY_FUNCTION__ we have to adjust the
7394 const char *const name
7395 = (*decl_printable_name
) (current_function_decl
, 2);
7396 init
= cp_fname_init (name
);
7397 TREE_TYPE (decl
) = TREE_TYPE (init
);
7400 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7401 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
7405 /* A DECL_STMT can also be used as an expression, in the condition
7406 clause of a if/for/while construct. If we aren't followed by
7407 another statement, return our decl. */
7408 if (TREE_CHAIN (t
) == NULL_TREE
)
7417 stmt
= begin_for_stmt ();
7418 tsubst_expr (FOR_INIT_STMT (t
), args
, complain
, in_decl
);
7419 finish_for_init_stmt (stmt
);
7420 finish_for_cond (tsubst_expr (FOR_COND (t
),
7421 args
, complain
, in_decl
),
7423 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7424 finish_for_expr (tmp
, stmt
);
7425 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7426 finish_for_stmt (stmt
);
7433 stmt
= begin_while_stmt ();
7434 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7435 args
, complain
, in_decl
),
7437 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7438 finish_while_stmt (stmt
);
7445 stmt
= begin_do_stmt ();
7446 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7447 finish_do_body (stmt
);
7448 finish_do_stmt (tsubst_expr (DO_COND (t
),
7449 args
, complain
, in_decl
),
7457 stmt
= begin_if_stmt ();
7458 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7459 args
, complain
, in_decl
),
7462 if (tmp
= THEN_CLAUSE (t
), tmp
)
7464 tsubst_expr (tmp
, args
, complain
, in_decl
);
7465 finish_then_clause (stmt
);
7468 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7470 begin_else_clause ();
7471 tsubst_expr (tmp
, args
, complain
, in_decl
);
7472 finish_else_clause (stmt
);
7482 if (COMPOUND_STMT_BODY_BLOCK (t
))
7483 stmt
= begin_function_body ();
7485 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7487 tsubst_expr (COMPOUND_BODY (t
), args
, complain
, in_decl
);
7489 if (COMPOUND_STMT_BODY_BLOCK (t
))
7490 finish_function_body (stmt
);
7492 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7498 finish_break_stmt ();
7503 finish_continue_stmt ();
7511 stmt
= begin_switch_stmt ();
7512 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7513 finish_switch_cond (val
, stmt
);
7514 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7515 finish_switch_stmt (stmt
);
7521 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7522 tsubst_expr (CASE_HIGH (t
), args
, complain
,
7527 lineno
= STMT_LINENO (t
);
7528 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7532 input_filename
= FILE_STMT_FILENAME (t
);
7533 add_stmt (build_nt (FILE_STMT
, FILE_STMT_FILENAME_NODE (t
)));
7538 tmp
= GOTO_DESTINATION (t
);
7539 if (TREE_CODE (tmp
) != LABEL_DECL
)
7540 /* Computed goto's must be tsubst'd into. On the other hand,
7541 non-computed gotos must not be; the identifier in question
7542 will have no binding. */
7543 tmp
= tsubst_expr (tmp
, args
, complain
, in_decl
);
7545 tmp
= DECL_NAME (tmp
);
7546 finish_goto_stmt (tmp
);
7551 tmp
= finish_asm_stmt
7553 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7554 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7555 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7556 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
7557 ASM_INPUT_P (tmp
) = ASM_INPUT_P (t
);
7564 stmt
= begin_try_block ();
7565 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7566 finish_cleanup_try_block (stmt
);
7567 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7573 if (FN_TRY_BLOCK_P (t
))
7574 stmt
= begin_function_try_block ();
7576 stmt
= begin_try_block ();
7578 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7580 if (FN_TRY_BLOCK_P (t
))
7581 finish_function_try_block (stmt
);
7583 finish_try_block (stmt
);
7585 tsubst_expr (TRY_HANDLERS (t
), args
, complain
, in_decl
);
7586 if (FN_TRY_BLOCK_P (t
))
7587 finish_function_handler_sequence (stmt
);
7589 finish_handler_sequence (stmt
);
7598 stmt
= begin_handler ();
7599 if (HANDLER_PARMS (t
))
7601 decl
= DECL_STMT_DECL (HANDLER_PARMS (t
));
7602 decl
= tsubst (decl
, args
, complain
, in_decl
);
7603 /* Prevent instantiate_decl from trying to instantiate
7604 this variable. We've already done all that needs to be
7606 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7610 finish_handler_parms (decl
, stmt
);
7611 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7612 finish_handler (stmt
);
7618 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
7622 add_stmt (copy_node (t
));
7629 return tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7632 /* Instantiate the indicated variable or function template TMPL with
7633 the template arguments in TARG_PTR. */
7636 instantiate_template (tmpl
, targ_ptr
)
7637 tree tmpl
, targ_ptr
;
7645 if (tmpl
== error_mark_node
)
7646 return error_mark_node
;
7648 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7650 /* If this function is a clone, handle it specially. */
7651 if (DECL_CLONED_FUNCTION_P (tmpl
))
7653 tree spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
);
7656 /* Look for the clone. */
7657 for (clone
= TREE_CHAIN (spec
);
7658 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7659 clone
= TREE_CHAIN (clone
))
7660 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
7662 /* We should always have found the clone by now. */
7667 /* Check to see if we already have this specialization. */
7668 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7669 if (spec
!= NULL_TREE
)
7672 gen_tmpl
= most_general_template (tmpl
);
7673 if (tmpl
!= gen_tmpl
)
7675 /* The TMPL is a partial instantiation. To get a full set of
7676 arguments we must add the arguments used to perform the
7677 partial instantiation. */
7678 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7681 /* Check to see if we already have this specialization. */
7682 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7683 if (spec
!= NULL_TREE
)
7687 len
= DECL_NTPARMS (gen_tmpl
);
7688 inner_args
= INNERMOST_TEMPLATE_ARGS (targ_ptr
);
7692 tree t
= TREE_VEC_ELT (inner_args
, i
);
7695 tree nt
= target_type (t
);
7696 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7698 error ("type `%T' composed from a local class is not a valid template-argument", t
);
7699 error (" trying to instantiate `%D'", gen_tmpl
);
7700 return error_mark_node
;
7705 /* substitute template parameters */
7706 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
7707 targ_ptr
, tf_error
, gen_tmpl
);
7708 /* The DECL_TI_TEMPLATE should always be the immediate parent
7709 template, not the most general template. */
7710 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7712 if (flag_external_templates
)
7713 add_pending_template (fndecl
);
7715 /* If we've just instantiated the main entry point for a function,
7716 instantiate all the alternate entry points as well. We do this
7717 by cloning the instantiation of the main entry point, not by
7718 instantiating the template clones. */
7719 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
7720 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
7725 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7726 arguments that are being used when calling it. TARGS is a vector
7727 into which the deduced template arguments are placed.
7729 Return zero for success, 2 for an incomplete match that doesn't resolve
7730 all the types, and 1 for complete failure. An error message will be
7731 printed only for an incomplete match.
7733 If FN is a conversion operator, or we are trying to produce a specific
7734 specialization, RETURN_TYPE is the return type desired.
7736 The EXPLICIT_TARGS are explicit template arguments provided via a
7739 The parameter STRICT is one of:
7742 We are deducing arguments for a function call, as in
7746 We are deducing arguments for a conversion function, as in
7750 We are deducing arguments when doing an explicit instantiation
7751 as in [temp.explicit], when determining an explicit specialization
7752 as in [temp.expl.spec], or when taking the address of a function
7753 template, as in [temp.deduct.funcaddr].
7756 We are deducing arguments when calculating the partial
7757 ordering between specializations of function or class
7758 templates, as in [temp.func.order] and [temp.class.order].
7760 LEN is the number of parms to consider before returning success, or -1
7761 for all. This is used in partial ordering to avoid comparing parms for
7762 which no actual argument was passed, since they are not considered in
7763 overload resolution (and are explicitly excluded from consideration in
7764 partial ordering in [temp.func.order]/6). */
7767 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7769 tree fn
, explicit_targs
, targs
, args
, return_type
;
7770 unification_kind_t strict
;
7777 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7779 fntype
= TREE_TYPE (fn
);
7784 The specified template arguments must match the template
7785 parameters in kind (i.e., type, nontype, template), and there
7786 must not be more arguments than there are parameters;
7787 otherwise type deduction fails.
7789 Nontype arguments must match the types of the corresponding
7790 nontype template parameters, or must be convertible to the
7791 types of the corresponding nontype parameters as specified in
7792 _temp.arg.nontype_, otherwise type deduction fails.
7794 All references in the function type of the function template
7795 to the corresponding template parameters are replaced by the
7796 specified template argument values. If a substitution in a
7797 template parameter or in the function type of the function
7798 template results in an invalid type, type deduction fails. */
7800 tree converted_args
;
7803 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7804 explicit_targs
, NULL_TREE
, tf_none
,
7805 /*require_all_arguments=*/0));
7806 if (converted_args
== error_mark_node
)
7809 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
7810 if (fntype
== error_mark_node
)
7813 /* Place the explicitly specified arguments in TARGS. */
7814 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7815 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7818 parms
= TYPE_ARG_TYPES (fntype
);
7819 /* Never do unification on the 'this' parameter. */
7820 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
7821 parms
= TREE_CHAIN (parms
);
7825 /* We've been given a return type to match, prepend it. */
7826 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
7827 args
= tree_cons (NULL_TREE
, return_type
, args
);
7832 /* We allow incomplete unification without an error message here
7833 because the standard doesn't seem to explicitly prohibit it. Our
7834 callers must be ready to deal with unification failures in any
7836 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7837 targs
, parms
, args
, /*subr=*/0,
7838 strict
, /*allow_incomplete*/1, len
);
7841 /* All is well so far. Now, check:
7845 When all template arguments have been deduced, all uses of
7846 template parameters in nondeduced contexts are replaced with
7847 the corresponding deduced argument values. If the
7848 substitution results in an invalid type, as described above,
7849 type deduction fails. */
7850 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
7857 /* Adjust types before performing type deduction, as described in
7858 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7859 sections are symmetric. PARM is the type of a function parameter
7860 or the return type of the conversion function. ARG is the type of
7861 the argument passed to the call, or the type of the value
7862 initialized with the result of the conversion function. */
7865 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7866 unification_kind_t strict
;
7879 /* Swap PARM and ARG throughout the remainder of this
7880 function; the handling is precisely symmetric since PARM
7881 will initialize ARG rather than vice versa. */
7889 /* There is nothing to do in this case. */
7893 /* DR 214. [temp.func.order] is underspecified, and leads to no
7894 ordering between things like `T *' and `T const &' for `U *'.
7895 The former has T=U and the latter T=U*. The former looks more
7896 specialized and John Spicer considers it well-formed (the EDG
7897 compiler accepts it).
7899 John also confirms that deduction should proceed as in a function
7900 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
7901 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7902 to an actual call can have such a type.
7904 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7905 If only ARG is a REFERENCE_TYPE, we look through that and then
7906 proceed as with DEDUCE_CALL (which could further convert it). */
7907 if (TREE_CODE (*arg
) == REFERENCE_TYPE
)
7909 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7911 *arg
= TREE_TYPE (*arg
);
7918 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7920 /* [temp.deduct.call]
7922 If P is not a reference type:
7924 --If A is an array type, the pointer type produced by the
7925 array-to-pointer standard conversion (_conv.array_) is
7926 used in place of A for type deduction; otherwise,
7928 --If A is a function type, the pointer type produced by
7929 the function-to-pointer standard conversion
7930 (_conv.func_) is used in place of A for type deduction;
7933 --If A is a cv-qualified type, the top level
7934 cv-qualifiers of A's type are ignored for type
7936 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7937 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7938 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7939 *arg
= build_pointer_type (*arg
);
7941 *arg
= TYPE_MAIN_VARIANT (*arg
);
7944 /* [temp.deduct.call]
7946 If P is a cv-qualified type, the top level cv-qualifiers
7947 of P's type are ignored for type deduction. If P is a
7948 reference type, the type referred to by P is used for
7950 *parm
= TYPE_MAIN_VARIANT (*parm
);
7951 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7953 *parm
= TREE_TYPE (*parm
);
7954 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
7959 /* Most parms like fn_type_unification.
7961 If SUBR is 1, we're being called recursively (to unify the
7962 arguments of a function or method parameter of a function
7966 type_unification_real (tparms
, targs
, xparms
, xargs
, subr
,
7967 strict
, allow_incomplete
, xlen
)
7968 tree tparms
, targs
, xparms
, xargs
;
7970 unification_kind_t strict
;
7971 int allow_incomplete
, xlen
;
7975 int ntparms
= TREE_VEC_LENGTH (tparms
);
7977 int saw_undeduced
= 0;
7981 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7982 my_friendly_assert (xparms
== NULL_TREE
7983 || TREE_CODE (xparms
) == TREE_LIST
, 290);
7984 /* ARGS could be NULL (via a call from parse.y to
7985 build_x_function_call). */
7987 my_friendly_assert (TREE_CODE (xargs
) == TREE_LIST
, 291);
7988 my_friendly_assert (ntparms
> 0, 292);
7993 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
7994 | UNIFY_ALLOW_DERIVED
);
7998 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
8002 sub_strict
= UNIFY_ALLOW_NONE
;
8006 sub_strict
= UNIFY_ALLOW_NONE
;
8022 && parms
!= void_list_node
8024 && args
!= void_list_node
)
8026 parm
= TREE_VALUE (parms
);
8027 parms
= TREE_CHAIN (parms
);
8028 arg
= TREE_VALUE (args
);
8029 args
= TREE_CHAIN (args
);
8031 if (arg
== error_mark_node
)
8033 if (arg
== unknown_type_node
)
8034 /* We can't deduce anything from this, but we might get all the
8035 template args from other function args. */
8038 /* Conversions will be performed on a function argument that
8039 corresponds with a function parameter that contains only
8040 non-deducible template parameters and explicitly specified
8041 template parameters. */
8042 if (! uses_template_parms (parm
))
8047 type
= TREE_TYPE (arg
);
8054 if (strict
== DEDUCE_EXACT
|| strict
== DEDUCE_ORDER
)
8056 if (same_type_p (parm
, type
))
8060 /* It might work; we shouldn't check now, because we might
8061 get into infinite recursion. Overload resolution will
8070 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
8071 if (type_unknown_p (arg
))
8073 /* [temp.deduct.type] A template-argument can be deduced from
8074 a pointer to function or pointer to member function
8075 argument if the set of overloaded functions does not
8076 contain function templates and at most one of a set of
8077 overloaded functions provides a unique match. */
8079 if (resolve_overloaded_unification
8080 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
8085 arg
= TREE_TYPE (arg
);
8089 int arg_strict
= sub_strict
;
8092 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8094 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
8098 /* Are we done with the interesting parms? */
8102 /* Fail if we've reached the end of the parm list, and more args
8103 are present, and the parm list isn't variadic. */
8104 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
8106 /* Fail if parms are left and they don't have default values. */
8108 && parms
!= void_list_node
8109 && TREE_PURPOSE (parms
) == NULL_TREE
)
8114 for (i
= 0; i
< ntparms
; i
++)
8115 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
8117 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8119 /* If this is an undeduced nontype parameter that depends on
8120 a type parameter, try another pass; its type may have been
8121 deduced from a later argument than the one from which
8122 this parameter can be deduced. */
8123 if (TREE_CODE (tparm
) == PARM_DECL
8124 && uses_template_parms (TREE_TYPE (tparm
))
8125 && !saw_undeduced
++)
8128 if (!allow_incomplete
)
8129 error ("incomplete type unification");
8135 /* Subroutine of type_unification_real. Args are like the variables at the
8136 call site. ARG is an overloaded function (or template-id); we try
8137 deducing template args from each of the overloads, and if only one
8138 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8141 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
8143 tree tparms
, targs
, parm
, arg
;
8144 unification_kind_t strict
;
8147 tree tempargs
= copy_node (targs
);
8150 if (TREE_CODE (arg
) == ADDR_EXPR
)
8151 arg
= TREE_OPERAND (arg
, 0);
8153 if (TREE_CODE (arg
) == COMPONENT_REF
)
8154 /* Handle `&x' where `x' is some static or non-static member
8156 arg
= TREE_OPERAND (arg
, 1);
8158 if (TREE_CODE (arg
) == OFFSET_REF
)
8159 arg
= TREE_OPERAND (arg
, 1);
8161 /* Strip baselink information. */
8162 while (TREE_CODE (arg
) == TREE_LIST
)
8163 arg
= TREE_VALUE (arg
);
8165 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
8167 /* If we got some explicit template args, we need to plug them into
8168 the affected templates before we try to unify, in case the
8169 explicit args will completely resolve the templates in question. */
8171 tree expl_subargs
= TREE_OPERAND (arg
, 1);
8172 arg
= TREE_OPERAND (arg
, 0);
8174 for (; arg
; arg
= OVL_NEXT (arg
))
8176 tree fn
= OVL_CURRENT (arg
);
8179 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8182 subargs
= get_bindings_overload (fn
, DECL_TEMPLATE_RESULT (fn
),
8186 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
8187 if (TREE_CODE (elem
) == METHOD_TYPE
)
8188 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
8189 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
8190 strict
, sub_strict
);
8194 else if (TREE_CODE (arg
) == OVERLOAD
)
8196 for (; arg
; arg
= OVL_NEXT (arg
))
8198 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
8199 if (TREE_CODE (type
) == METHOD_TYPE
)
8200 type
= build_ptrmemfunc_type (build_pointer_type (type
));
8201 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
8203 strict
, sub_strict
);
8209 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8210 to function or pointer to member function argument if the set of
8211 overloaded functions does not contain function templates and at most
8212 one of a set of overloaded functions provides a unique match.
8214 So if we found multiple possibilities, we return success but don't
8219 int i
= TREE_VEC_LENGTH (targs
);
8221 if (TREE_VEC_ELT (tempargs
, i
))
8222 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
8230 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8231 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8232 different overloads deduce different arguments for a given parm.
8233 Returns 1 on success. */
8236 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
8238 tree tparms
, orig_targs
, targs
, parm
, arg
;
8239 unification_kind_t strict
;
8246 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8247 to function or pointer to member function argument if the set of
8248 overloaded functions does not contain function templates and at most
8249 one of a set of overloaded functions provides a unique match.
8251 So if this is a template, just return success. */
8253 if (uses_template_parms (arg
))
8256 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8258 /* We don't copy orig_targs for this because if we have already deduced
8259 some template args from previous args, unify would complain when we
8260 try to deduce a template parameter for the same argument, even though
8261 there isn't really a conflict. */
8262 nargs
= TREE_VEC_LENGTH (targs
);
8263 tempargs
= make_tree_vec (nargs
);
8265 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
8268 /* First make sure we didn't deduce anything that conflicts with
8269 explicitly specified args. */
8270 for (i
= nargs
; i
--; )
8272 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8273 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
8275 if (elt
== NULL_TREE
)
8277 else if (uses_template_parms (elt
))
8279 /* Since we're unifying against ourselves, we will fill in template
8280 args used in the function parm list with our own template parms.
8282 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
8285 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
8289 for (i
= nargs
; i
--; )
8291 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8294 TREE_VEC_ELT (targs
, i
) = elt
;
8300 /* Verify that nondeduce template argument agrees with the type
8301 obtained from argument deduction. Return nonzero if the
8306 struct A { typedef int X; };
8307 template <class T, class U> struct C {};
8308 template <class T> struct C<T, typename T::X> {};
8310 Then with the instantiation `C<A, int>', we can deduce that
8311 `T' is `A' but unify () does not check whether `typename T::X'
8312 is `int'. This function ensure that they agree.
8314 TARGS, PARMS are the same as the arguments of unify.
8315 ARGS contains template arguments from all levels. */
8318 verify_class_unification (targs
, parms
, args
)
8319 tree targs
, parms
, args
;
8322 int nparms
= TREE_VEC_LENGTH (parms
);
8323 tree new_parms
= tsubst (parms
, add_outermost_template_args (args
, targs
),
8324 tf_none
, NULL_TREE
);
8325 if (new_parms
== error_mark_node
)
8328 args
= INNERMOST_TEMPLATE_ARGS (args
);
8330 for (i
= 0; i
< nparms
; i
++)
8332 tree parm
= TREE_VEC_ELT (new_parms
, i
);
8333 tree arg
= TREE_VEC_ELT (args
, i
);
8335 /* In case we are deducing from a function argument of a function
8336 templates, some parameters may not be deduced yet. So we
8337 make sure that only fully substituted elements of PARM are
8340 if (!uses_template_parms (parm
) && !template_args_equal (parm
, arg
))
8346 /* PARM is a template class (perhaps with unbound template
8347 parameters). ARG is a fully instantiated type. If ARG can be
8348 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8349 TARGS are as for unify. */
8352 try_class_unification (tparms
, targs
, parm
, arg
)
8360 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
8361 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
8364 /* We need to make a new template argument vector for the call to
8365 unify. If we used TARGS, we'd clutter it up with the result of
8366 the attempted unification, even if this class didn't work out.
8367 We also don't want to commit ourselves to all the unifications
8368 we've already done, since unification is supposed to be done on
8369 an argument-by-argument basis. In other words, consider the
8370 following pathological case:
8372 template <int I, int J, int K>
8375 template <int I, int J>
8376 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8378 template <int I, int J, int K>
8379 void f(S<I, J, K>, S<I, I, I>);
8388 Now, by the time we consider the unification involving `s2', we
8389 already know that we must have `f<0, 0, 0>'. But, even though
8390 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8391 because there are two ways to unify base classes of S<0, 1, 2>
8392 with S<I, I, I>. If we kept the already deduced knowledge, we
8393 would reject the possibility I=1. */
8394 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
8396 /* If unification failed, we're done. */
8397 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
8398 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
8404 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8405 have already discovered to be satisfactory. ARG_BINFO is the binfo
8406 for the base class of ARG that we are currently examining. */
8409 get_template_base_recursive (tparms
, targs
, parm
,
8410 arg_binfo
, rval
, flags
)
8420 tree arg
= BINFO_TYPE (arg_binfo
);
8422 if (!(flags
& GTB_IGNORE_TYPE
))
8424 tree r
= try_class_unification (tparms
, targs
,
8427 /* If there is more than one satisfactory baseclass, then:
8431 If they yield more than one possible deduced A, the type
8435 if (r
&& rval
&& !same_type_p (r
, rval
))
8436 return error_mark_node
;
8441 binfos
= BINFO_BASETYPES (arg_binfo
);
8442 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
8444 /* Process base types. */
8445 for (i
= 0; i
< n_baselinks
; i
++)
8447 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
8450 /* Skip this base, if we've already seen it. */
8451 if (BINFO_MARKED (base_binfo
))
8455 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
8457 /* When searching for a non-virtual, we cannot mark virtually
8460 SET_BINFO_MARKED (base_binfo
);
8462 rval
= get_template_base_recursive (tparms
, targs
,
8466 GTB_VIA_VIRTUAL
* this_virtual
);
8468 /* If we discovered more than one matching base class, we can
8470 if (rval
== error_mark_node
)
8471 return error_mark_node
;
8477 /* Given a template type PARM and a class type ARG, find the unique
8478 base type in ARG that is an instance of PARM. We do not examine
8479 ARG itself; only its base-classes. If there is no appropriate base
8480 class, return NULL_TREE. If there is more than one, return
8481 error_mark_node. PARM may be the type of a partial specialization,
8482 as well as a plain template type. Used by unify. */
8485 get_template_base (tparms
, targs
, parm
, arg
)
8494 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
8496 arg_binfo
= TYPE_BINFO (complete_type (arg
));
8497 rval
= get_template_base_recursive (tparms
, targs
,
8502 /* Since get_template_base_recursive marks the bases classes, we
8503 must unmark them here. */
8504 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
8509 /* Returns the level of DECL, which declares a template parameter. */
8512 template_decl_level (decl
)
8515 switch (TREE_CODE (decl
))
8519 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
8522 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
8530 /* Decide whether ARG can be unified with PARM, considering only the
8531 cv-qualifiers of each type, given STRICT as documented for unify.
8532 Returns non-zero iff the unification is OK on that basis.*/
8535 check_cv_quals_for_unify (strict
, arg
, parm
)
8540 int arg_quals
= cp_type_quals (arg
);
8541 int parm_quals
= cp_type_quals (parm
);
8543 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
)
8545 /* If the cvr quals of parm will not unify with ARG, they'll be
8546 ignored in instantiation, so we have to do the same here. */
8547 if (TREE_CODE (arg
) == REFERENCE_TYPE
8548 || TREE_CODE (arg
) == FUNCTION_TYPE
8549 || TREE_CODE (arg
) == METHOD_TYPE
)
8550 parm_quals
&= ~(TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
);
8551 if (!POINTER_TYPE_P (arg
) &&
8552 TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
8553 parm_quals
&= ~TYPE_QUAL_RESTRICT
;
8556 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
8557 && (arg_quals
& parm_quals
) != parm_quals
)
8560 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
8561 && (parm_quals
& arg_quals
) != arg_quals
)
8567 /* Takes parameters as for type_unification. Returns 0 if the
8568 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8569 bitwise or of the following flags:
8572 Require an exact match between PARM and ARG.
8573 UNIFY_ALLOW_MORE_CV_QUAL:
8574 Allow the deduced ARG to be more cv-qualified (by qualification
8575 conversion) than ARG.
8576 UNIFY_ALLOW_LESS_CV_QUAL:
8577 Allow the deduced ARG to be less cv-qualified than ARG.
8578 UNIFY_ALLOW_DERIVED:
8579 Allow the deduced ARG to be a template base class of ARG,
8580 or a pointer to a template base class of the type pointed to by
8582 UNIFY_ALLOW_INTEGER:
8583 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8584 case for more information.
8585 UNIFY_ALLOW_OUTER_LEVEL:
8586 This is the outermost level of a deduction. Used to determine validity
8587 of qualification conversions. A valid qualification conversion must
8588 have const qualified pointers leading up to the inner type which
8589 requires additional CV quals, except at the outer level, where const
8590 is not required [conv.qual]. It would be normal to set this flag in
8591 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8592 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8593 This is the outermost level of a deduction, and PARM can be more CV
8594 qualified at this point.
8595 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8596 This is the outermost level of a deduction, and PARM can be less CV
8597 qualified at this point.
8598 UNIFY_ALLOW_MAX_CORRECTION:
8599 This is an INTEGER_TYPE's maximum value. Used if the range may
8600 have been derived from a size specification, such as an array size.
8601 If the size was given by a nontype template parameter N, the maximum
8602 value will have the form N-1. The flag says that we can (and indeed
8603 must) unify N with (ARG + 1), an exception to the normal rules on
8607 unify (tparms
, targs
, parm
, arg
, strict
)
8608 tree tparms
, targs
, parm
, arg
;
8614 int strict_in
= strict
;
8616 /* I don't think this will do the right thing with respect to types.
8617 But the only case I've seen it in so far has been array bounds, where
8618 signedness is the only information lost, and I think that will be
8620 while (TREE_CODE (parm
) == NOP_EXPR
)
8621 parm
= TREE_OPERAND (parm
, 0);
8623 if (arg
== error_mark_node
)
8625 if (arg
== unknown_type_node
)
8626 /* We can't deduce anything from this, but we might get all the
8627 template args from other function args. */
8630 /* If PARM uses template parameters, then we can't bail out here,
8631 even if ARG == PARM, since we won't record unifications for the
8632 template parameters. We might need them if we're trying to
8633 figure out which of two things is more specialized. */
8634 if (arg
== parm
&& !uses_template_parms (parm
))
8637 /* Immediately reject some pairs that won't unify because of
8638 cv-qualification mismatches. */
8639 if (TREE_CODE (arg
) == TREE_CODE (parm
)
8641 /* It is the elements of the array which hold the cv quals of an array
8642 type, and the elements might be template type parms. We'll check
8644 && TREE_CODE (arg
) != ARRAY_TYPE
8645 /* We check the cv-qualifiers when unifying with template type
8646 parameters below. We want to allow ARG `const T' to unify with
8647 PARM `T' for example, when computing which of two templates
8648 is more specialized, for example. */
8649 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
8650 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
8653 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
8654 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
8655 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
8656 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
8657 strict
&= ~UNIFY_ALLOW_DERIVED
;
8658 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
8659 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
8660 strict
&= ~UNIFY_ALLOW_MAX_CORRECTION
;
8662 switch (TREE_CODE (parm
))
8666 case UNBOUND_CLASS_TEMPLATE
:
8667 /* In a type which contains a nested-name-specifier, template
8668 argument values cannot be deduced for template parameters used
8669 within the nested-name-specifier. */
8672 case TEMPLATE_TYPE_PARM
:
8673 case TEMPLATE_TEMPLATE_PARM
:
8674 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8675 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8677 if (TEMPLATE_TYPE_LEVEL (parm
)
8678 != template_decl_level (tparm
))
8679 /* The PARM is not one we're trying to unify. Just check
8680 to see if it matches ARG. */
8681 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8682 && same_type_p (parm
, arg
)) ? 0 : 1;
8683 idx
= TEMPLATE_TYPE_IDX (parm
);
8684 targ
= TREE_VEC_ELT (targs
, idx
);
8685 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
8687 /* Check for mixed types and values. */
8688 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
8689 && TREE_CODE (tparm
) != TYPE_DECL
)
8690 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8691 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8694 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8696 /* ARG must be constructed from a template class or a template
8697 template parameter. */
8698 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
8699 && (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
)))
8703 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8704 tree parmvec
= TYPE_TI_ARGS (parm
);
8705 tree argvec
= TYPE_TI_ARGS (arg
);
8707 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
8710 /* The parameter and argument roles have to be switched here
8711 in order to handle default arguments properly. For example,
8712 template<template <class> class TT> void f(TT<int>)
8713 should be able to accept vector<int> which comes from
8714 template <class T, class Allocator = allocator>
8717 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8721 /* Deduce arguments T, i from TT<T> or TT<i>.
8722 We check each element of PARMVEC and ARGVEC individually
8723 rather than the whole TREE_VEC since they can have
8724 different number of elements. */
8726 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8728 tree t
= TREE_VEC_ELT (parmvec
, i
);
8730 if (unify (tparms
, targs
, t
,
8731 TREE_VEC_ELT (argvec
, i
),
8736 arg
= TYPE_TI_TEMPLATE (arg
);
8738 /* Fall through to deduce template name. */
8741 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8742 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8744 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8746 /* Simple cases: Value already set, does match or doesn't. */
8747 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
8754 /* If PARM is `const T' and ARG is only `int', we don't have
8755 a match unless we are allowing additional qualification.
8756 If ARG is `const int' and PARM is just `T' that's OK;
8757 that binds `const int' to `T'. */
8758 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
8762 /* Consider the case where ARG is `const volatile int' and
8763 PARM is `const T'. Then, T should be `volatile int'. */
8764 arg
= cp_build_qualified_type_real
8765 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
8766 if (arg
== error_mark_node
)
8769 /* Simple cases: Value already set, does match or doesn't. */
8770 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8776 /* Make sure that ARG is not a variable-sized array. (Note that
8777 were talking about variable-sized arrays (like `int[n]'),
8778 rather than arrays of unknown size (like `int[]').) We'll
8779 get very confused by such a type since the bound of the array
8780 will not be computable in an instantiation. Besides, such
8781 types are not allowed in ISO C++, so we can do as we please
8783 if (TREE_CODE (arg
) == ARRAY_TYPE
8784 && !uses_template_parms (arg
)
8785 && TYPE_DOMAIN (arg
)
8786 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8790 TREE_VEC_ELT (targs
, idx
) = arg
;
8793 case TEMPLATE_PARM_INDEX
:
8794 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8796 if (TEMPLATE_PARM_LEVEL (parm
)
8797 != template_decl_level (tparm
))
8798 /* The PARM is not one we're trying to unify. Just check
8799 to see if it matches ARG. */
8800 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8801 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8803 idx
= TEMPLATE_PARM_IDX (parm
);
8804 targ
= TREE_VEC_ELT (targs
, idx
);
8808 int i
= (cp_tree_equal (targ
, arg
) > 0);
8817 /* [temp.deduct.type] If, in the declaration of a function template
8818 with a non-type template-parameter, the non-type
8819 template-parameter is used in an expression in the function
8820 parameter-list and, if the corresponding template-argument is
8821 deduced, the template-argument type shall match the type of the
8822 template-parameter exactly, except that a template-argument
8823 deduced from an array bound may be of any integral type.
8824 The non-type parameter might use already deduced type parameters. */
8825 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
8826 if (same_type_p (TREE_TYPE (arg
), tparm
))
8828 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8829 && (TREE_CODE (tparm
) == INTEGER_TYPE
8830 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
8832 else if (uses_template_parms (tparm
))
8833 /* We haven't deduced the type of this parameter yet. Try again
8839 TREE_VEC_ELT (targs
, idx
) = arg
;
8844 if (TREE_CODE (arg
) != POINTER_TYPE
)
8847 /* [temp.deduct.call]
8849 A can be another pointer or pointer to member type that can
8850 be converted to the deduced A via a qualification
8851 conversion (_conv.qual_).
8853 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8854 This will allow for additional cv-qualification of the
8855 pointed-to types if appropriate. */
8857 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
8858 /* The derived-to-base conversion only persists through one
8859 level of pointers. */
8860 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
8862 if (TREE_CODE (TREE_TYPE (parm
)) == OFFSET_TYPE
8863 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
8865 /* Avoid getting confused about cv-quals; don't recurse here.
8866 Pointers to members should really be just OFFSET_TYPE, not
8867 this two-level nonsense... */
8869 parm
= TREE_TYPE (parm
);
8870 arg
= TREE_TYPE (arg
);
8874 return unify (tparms
, targs
, TREE_TYPE (parm
),
8875 TREE_TYPE (arg
), strict
);
8878 case REFERENCE_TYPE
:
8879 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8881 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8882 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
8885 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8887 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8888 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8890 if (TYPE_DOMAIN (parm
) != NULL_TREE
8891 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8892 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8894 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8903 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8906 if (TREE_CODE (parm
) == INTEGER_TYPE
8907 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8909 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8910 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8911 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8913 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8914 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8915 TYPE_MAX_VALUE (arg
),
8916 UNIFY_ALLOW_INTEGER
| UNIFY_ALLOW_MAX_CORRECTION
))
8919 /* We have already checked cv-qualification at the top of the
8921 else if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
8924 /* As far as unification is concerned, this wins. Later checks
8925 will invalidate it if necessary. */
8928 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8929 /* Type INTEGER_CST can come from ordinary constant template args. */
8931 while (TREE_CODE (arg
) == NOP_EXPR
)
8932 arg
= TREE_OPERAND (arg
, 0);
8934 if (TREE_CODE (arg
) != INTEGER_CST
)
8936 return !tree_int_cst_equal (parm
, arg
);
8941 if (TREE_CODE (arg
) != TREE_VEC
)
8943 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8945 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
8946 if (unify (tparms
, targs
,
8947 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8955 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8958 if (TYPE_PTRMEMFUNC_P (parm
))
8960 if (!TYPE_PTRMEMFUNC_P (arg
))
8963 return unify (tparms
, targs
,
8964 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8965 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
8969 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8973 if (strict_in
& UNIFY_ALLOW_DERIVED
)
8975 /* First, we try to unify the PARM and ARG directly. */
8976 t
= try_class_unification (tparms
, targs
,
8981 /* Fallback to the special case allowed in
8984 If P is a class, and P has the form
8985 template-id, then A can be a derived class of
8986 the deduced A. Likewise, if P is a pointer to
8987 a class of the form template-id, A can be a
8988 pointer to a derived class pointed to by the
8990 t
= get_template_base (tparms
, targs
,
8993 if (! t
|| t
== error_mark_node
)
8997 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8998 && (CLASSTYPE_TI_TEMPLATE (parm
)
8999 == CLASSTYPE_TI_TEMPLATE (arg
)))
9000 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9001 Then, we should unify `int' and `U'. */
9004 /* There's no chance of unification succeeding. */
9007 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
9008 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
9010 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
9016 if (TREE_CODE (arg
) != TREE_CODE (parm
))
9019 if (unify (tparms
, targs
, TREE_TYPE (parm
),
9020 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
9022 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
9023 TYPE_ARG_TYPES (arg
), 1,
9024 DEDUCE_EXACT
, 0, -1);
9028 if (TREE_CODE (arg
) != OFFSET_TYPE
)
9030 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
9031 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
9033 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
9037 if (arg
!= decl_constant_value (parm
))
9042 /* Matched cases are handled by the ARG == PARM test above. */
9046 if (tree_int_cst_equal (TREE_OPERAND (parm
, 1), integer_one_node
)
9047 && (strict_in
& UNIFY_ALLOW_MAX_CORRECTION
))
9049 /* We handle this case specially, since it comes up with
9050 arrays. In particular, something like:
9052 template <int N> void f(int (&x)[N]);
9054 Here, we are trying to unify the range type, which
9055 looks like [0 ... (N - 1)]. */
9057 t1
= TREE_OPERAND (parm
, 0);
9058 t2
= TREE_OPERAND (parm
, 1);
9060 t
= fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
));
9062 return unify (tparms
, targs
, t1
, t
, strict
);
9064 /* else fall through */
9067 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
9070 /* We're looking at an expression. This can happen with
9074 void foo(S<I>, S<I + 2>);
9076 This is a "nondeduced context":
9080 The nondeduced contexts are:
9082 --A type that is a template-id in which one or more of
9083 the template-arguments is an expression that references
9084 a template-parameter.
9086 In these cases, we assume deduction succeeded, but don't
9087 actually infer any unifications. */
9089 if (!uses_template_parms (parm
)
9090 && !template_args_equal (parm
, arg
))
9096 sorry ("use of `%s' in template type unification",
9097 tree_code_name
[(int) TREE_CODE (parm
)]);
9103 /* Called if RESULT is explicitly instantiated, or is a member of an
9104 explicitly instantiated class, or if using -frepo and the
9105 instantiation of RESULT has been assigned to this file. */
9108 mark_decl_instantiated (result
, extern_p
)
9112 if (TREE_CODE (result
) != FUNCTION_DECL
)
9113 /* The TREE_PUBLIC flag for function declarations will have been
9114 set correctly by tsubst. */
9115 TREE_PUBLIC (result
) = 1;
9117 /* We used to set this unconditionally; we moved that to
9118 do_decl_instantiation so it wouldn't get set on members of
9119 explicit class template instantiations. But we still need to set
9120 it here for the 'extern template' case in order to suppress
9121 implicit instantiations. */
9123 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9127 DECL_INTERFACE_KNOWN (result
) = 1;
9128 DECL_NOT_REALLY_EXTERN (result
) = 1;
9130 /* Always make artificials weak. */
9131 if (DECL_ARTIFICIAL (result
) && flag_weak
)
9132 comdat_linkage (result
);
9133 /* For WIN32 we also want to put explicit instantiations in
9134 linkonce sections. */
9135 else if (TREE_PUBLIC (result
))
9136 maybe_make_one_only (result
);
9138 else if (TREE_CODE (result
) == FUNCTION_DECL
)
9142 /* Given two function templates PAT1 and PAT2, return:
9144 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9146 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9147 -1 if PAT2 is more specialized than PAT1.
9148 0 if neither is more specialized.
9150 LEN is passed through to fn_type_unification. */
9153 more_specialized (pat1
, pat2
, deduce
, len
)
9161 targs
= get_bindings_real (pat1
, DECL_TEMPLATE_RESULT (pat2
),
9162 NULL_TREE
, 0, deduce
, len
);
9166 targs
= get_bindings_real (pat2
, DECL_TEMPLATE_RESULT (pat1
),
9167 NULL_TREE
, 0, deduce
, len
);
9174 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9176 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9177 -1 if PAT2 is more specialized than PAT1.
9178 0 if neither is more specialized. */
9181 more_specialized_class (pat1
, pat2
)
9187 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
9188 TREE_PURPOSE (pat2
));
9192 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
9193 TREE_PURPOSE (pat1
));
9200 /* Return the template arguments that will produce the function signature
9201 DECL from the function template FN, with the explicit template
9202 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9203 also match. Return NULL_TREE if no satisfactory arguments could be
9204 found. DEDUCE and LEN are passed through to fn_type_unification. */
9207 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
, deduce
, len
)
9208 tree fn
, decl
, explicit_args
;
9209 int check_rettype
, deduce
, len
;
9211 int ntparms
= DECL_NTPARMS (fn
);
9212 tree targs
= make_tree_vec (ntparms
);
9214 tree decl_arg_types
;
9217 /* Substitute the explicit template arguments into the type of DECL.
9218 The call to fn_type_unification will handle substitution into the
9220 decl_type
= TREE_TYPE (decl
);
9221 if (explicit_args
&& uses_template_parms (decl_type
))
9224 tree converted_args
;
9226 if (DECL_TEMPLATE_INFO (decl
))
9227 tmpl
= DECL_TI_TEMPLATE (decl
);
9229 /* We can get here for some illegal specializations. */
9233 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
9234 explicit_args
, NULL_TREE
,
9235 tf_none
, /*require_all_arguments=*/0));
9236 if (converted_args
== error_mark_node
)
9239 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
9240 if (decl_type
== error_mark_node
)
9244 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
9245 /* Never do unification on the 'this' parameter. */
9246 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
9247 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
9249 i
= fn_type_unification (fn
, explicit_args
, targs
,
9251 (check_rettype
|| DECL_CONV_FN_P (fn
)
9252 ? TREE_TYPE (decl_type
) : NULL_TREE
),
9261 /* For most uses, we want to check the return type. */
9264 get_bindings (fn
, decl
, explicit_args
)
9265 tree fn
, decl
, explicit_args
;
9267 return get_bindings_real (fn
, decl
, explicit_args
, 1, DEDUCE_EXACT
, -1);
9270 /* But for resolve_overloaded_unification, we only care about the parameter
9274 get_bindings_overload (fn
, decl
, explicit_args
)
9275 tree fn
, decl
, explicit_args
;
9277 return get_bindings_real (fn
, decl
, explicit_args
, 0, DEDUCE_EXACT
, -1);
9280 /* Return the innermost template arguments that, when applied to a
9281 template specialization whose innermost template parameters are
9282 TPARMS, and whose specialization arguments are PARMS, yield the
9285 For example, suppose we have:
9287 template <class T, class U> struct S {};
9288 template <class T> struct S<T*, int> {};
9290 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9291 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9292 int}. The resulting vector will be {double}, indicating that `T'
9293 is bound to `double'. */
9296 get_class_bindings (tparms
, parms
, args
)
9297 tree tparms
, parms
, args
;
9299 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
9300 tree vec
= make_tree_vec (ntparms
);
9302 if (unify (tparms
, vec
, parms
, INNERMOST_TEMPLATE_ARGS (args
),
9306 for (i
= 0; i
< ntparms
; ++i
)
9307 if (! TREE_VEC_ELT (vec
, i
))
9310 if (verify_class_unification (vec
, parms
, args
))
9316 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9317 Pick the most specialized template, and return the corresponding
9318 instantiation, or if there is no corresponding instantiation, the
9319 template itself. If there is no most specialized template,
9320 error_mark_node is returned. If there are no templates at all,
9321 NULL_TREE is returned. */
9324 most_specialized_instantiation (instantiations
)
9325 tree instantiations
;
9330 if (!instantiations
)
9333 champ
= instantiations
;
9334 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
9336 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
9344 fn
= TREE_CHAIN (fn
);
9346 return error_mark_node
;
9352 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
9354 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
9357 return error_mark_node
;
9360 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
9363 /* Return the most specialized of the list of templates in FNS that can
9364 produce an instantiation matching DECL, given the explicit template
9365 arguments EXPLICIT_ARGS. */
9368 most_specialized (fns
, decl
, explicit_args
)
9369 tree fns
, decl
, explicit_args
;
9371 tree candidates
= NULL_TREE
;
9374 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
9376 tree candidate
= TREE_VALUE (fn
);
9378 args
= get_bindings (candidate
, decl
, explicit_args
);
9380 candidates
= tree_cons (NULL_TREE
, candidate
, candidates
);
9383 return most_specialized_instantiation (candidates
);
9386 /* If DECL is a specialization of some template, return the most
9387 general such template. Otherwise, returns NULL_TREE.
9391 template <class T> struct S { template <class U> void f(U); };
9393 if TMPL is `template <class U> void S<int>::f(U)' this will return
9394 the full template. This function will not trace past partial
9395 specializations, however. For example, given in addition:
9397 template <class T> struct S<T*> { template <class U> void f(U); };
9399 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9400 `template <class T> template <class U> S<T*>::f(U)'. */
9403 most_general_template (decl
)
9406 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9407 an immediate specialization. */
9408 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9410 if (DECL_TEMPLATE_INFO (decl
)) {
9411 decl
= DECL_TI_TEMPLATE (decl
);
9413 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9415 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
9421 /* Look for more and more general templates. */
9422 while (DECL_TEMPLATE_INFO (decl
))
9424 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9425 in some cases. (See cp-tree.h for details.) */
9426 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9429 /* Stop if we run into an explicitly specialized class template. */
9430 if (!DECL_NAMESPACE_SCOPE_P (decl
)
9431 && DECL_CONTEXT (decl
)
9432 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
9435 decl
= DECL_TI_TEMPLATE (decl
);
9441 /* Return the most specialized of the class template specializations
9442 of TMPL which can produce an instantiation matching ARGS, or
9443 error_mark_node if the choice is ambiguous. */
9446 most_specialized_class (tmpl
, args
)
9450 tree list
= NULL_TREE
;
9455 tmpl
= most_general_template (tmpl
);
9456 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
9459 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
9462 list
= tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
9463 TREE_TYPE (list
) = TREE_TYPE (t
);
9473 for (; t
; t
= TREE_CHAIN (t
))
9475 fate
= more_specialized_class (champ
, t
);
9484 return error_mark_node
;
9490 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
9492 fate
= more_specialized_class (champ
, t
);
9494 return error_mark_node
;
9500 /* called from the parser. */
9503 do_decl_instantiation (declspecs
, declarator
, storage
)
9504 tree declspecs
, declarator
, storage
;
9506 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL
);
9507 tree result
= NULL_TREE
;
9511 /* An error occurred, for which grokdeclarator has already issued
9512 an appropriate message. */
9514 else if (! DECL_LANG_SPECIFIC (decl
))
9516 error ("explicit instantiation of non-template `%#D'", decl
);
9519 else if (TREE_CODE (decl
) == VAR_DECL
)
9521 /* There is an asymmetry here in the way VAR_DECLs and
9522 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9523 the latter, the DECL we get back will be marked as a
9524 template instantiation, and the appropriate
9525 DECL_TEMPLATE_INFO will be set up. This does not happen for
9526 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9527 should handle VAR_DECLs as it currently handles
9529 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
9530 if (result
&& TREE_CODE (result
) != VAR_DECL
)
9532 error ("no matching template for `%D' found", result
);
9536 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
9538 error ("explicit instantiation of `%#D'", decl
);
9544 /* Check for various error cases. Note that if the explicit
9545 instantiation is legal the RESULT will currently be marked as an
9546 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9547 until we get here. */
9549 if (DECL_TEMPLATE_SPECIALIZATION (result
))
9551 /* DR 259 [temp.spec].
9553 Both an explicit instantiation and a declaration of an explicit
9554 specialization shall not appear in a program unless the explicit
9555 instantiation follows a declaration of the explicit specialization.
9557 For a given set of template parameters, if an explicit
9558 instantiation of a template appears after a declaration of an
9559 explicit specialization for that template, the explicit
9560 instantiation has no effect. */
9563 else if (DECL_EXPLICIT_INSTANTIATION (result
))
9567 No program shall explicitly instantiate any template more
9570 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9571 instantiation was `extern' and the second is not, and EXTERN_P for
9572 the opposite case. If -frepo, chances are we already got marked
9573 as an explicit instantiation because of the repo file. */
9574 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
9575 pedwarn ("duplicate explicit instantiation of `%#D'", result
);
9577 /* If we've already instantiated the template, just return now. */
9578 if (DECL_INTERFACE_KNOWN (result
))
9581 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
9583 error ("no matching template for `%D' found", result
);
9586 else if (!DECL_TEMPLATE_INFO (result
))
9588 pedwarn ("explicit instantiation of non-template `%#D'", result
);
9592 if (flag_external_templates
)
9595 if (storage
== NULL_TREE
)
9597 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9599 if (pedantic
&& !in_system_header
)
9600 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9604 error ("storage class `%D' applied to template instantiation",
9607 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9608 mark_decl_instantiated (result
, extern_p
);
9609 repo_template_instantiated (result
, extern_p
);
9611 instantiate_decl (result
, /*defer_ok=*/1);
9615 mark_class_instantiated (t
, extern_p
)
9619 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
9620 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
9621 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
9622 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
9625 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
9626 rest_of_type_compilation (t
, 1);
9630 /* Perform an explicit instantiation of template class T. STORAGE, if
9631 non-null, is the RID for extern, inline or static. COMPLAIN is
9632 non-zero if this is called from the parser, zero if called recursively,
9633 since the standard is unclear (as detailed below). */
9636 do_type_instantiation (t
, storage
, complain
)
9638 tsubst_flags_t complain
;
9644 if (TREE_CODE (t
) == TYPE_DECL
)
9647 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
9649 error ("explicit instantiation of non-template type `%T'", t
);
9655 /* With -fexternal-templates, explicit instantiations are treated the same
9656 as implicit ones. */
9657 if (flag_external_templates
)
9660 if (!COMPLETE_TYPE_P (t
))
9662 if (complain
& tf_error
)
9663 error ("explicit instantiation of `%#T' before definition of template",
9668 if (storage
!= NULL_TREE
)
9670 if (pedantic
&& !in_system_header
)
9671 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9672 IDENTIFIER_POINTER (storage
));
9674 if (storage
== ridpointers
[(int) RID_INLINE
])
9676 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9678 else if (storage
== ridpointers
[(int) RID_STATIC
])
9682 error ("storage class `%D' applied to template instantiation",
9688 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
9690 /* DR 259 [temp.spec].
9692 Both an explicit instantiation and a declaration of an explicit
9693 specialization shall not appear in a program unless the explicit
9694 instantiation follows a declaration of the explicit specialization.
9696 For a given set of template parameters, if an explicit
9697 instantiation of a template appears after a declaration of an
9698 explicit specialization for that template, the explicit
9699 instantiation has no effect. */
9702 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
9706 No program shall explicitly instantiate any template more
9709 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9710 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9711 are we already got marked as an explicit instantiation because of the
9712 repo file. All these cases are OK. */
9713 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
9714 && (complain
& tf_error
))
9715 pedwarn ("duplicate explicit instantiation of `%#T'", t
);
9717 /* If we've already instantiated the template, just return now. */
9718 if (!CLASSTYPE_INTERFACE_ONLY (t
))
9722 mark_class_instantiated (t
, extern_p
);
9723 repo_template_instantiated (t
, extern_p
);
9731 /* In contrast to implicit instantiation, where only the
9732 declarations, and not the definitions, of members are
9733 instantiated, we have here:
9737 The explicit instantiation of a class template specialization
9738 implies the instantiation of all of its members not
9739 previously explicitly specialized in the translation unit
9740 containing the explicit instantiation.
9742 Of course, we can't instantiate member template classes, since
9743 we don't have any arguments for them. Note that the standard
9744 is unclear on whether the instantiation of the members are
9745 *explicit* instantiations or not. We choose to be generous,
9746 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9747 the explicit instantiation of a class where some of the members
9748 have no definition in the current translation unit. */
9751 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9752 if (TREE_CODE (tmp
) == FUNCTION_DECL
9753 && DECL_TEMPLATE_INSTANTIATION (tmp
))
9755 mark_decl_instantiated (tmp
, extern_p
);
9756 repo_template_instantiated (tmp
, extern_p
);
9758 instantiate_decl (tmp
, /*defer_ok=*/1);
9761 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9762 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
9764 mark_decl_instantiated (tmp
, extern_p
);
9765 repo_template_instantiated (tmp
, extern_p
);
9767 instantiate_decl (tmp
, /*defer_ok=*/1);
9770 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9771 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
9772 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
9773 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
, 0);
9777 /* Given a function DECL, which is a specialization of TMPL, modify
9778 DECL to be a re-instantiation of TMPL with the same template
9779 arguments. TMPL should be the template into which tsubst'ing
9780 should occur for DECL, not the most general template.
9782 One reason for doing this is a scenario like this:
9785 void f(const T&, int i);
9787 void g() { f(3, 7); }
9790 void f(const T& t, const int i) { }
9792 Note that when the template is first instantiated, with
9793 instantiate_template, the resulting DECL will have no name for the
9794 first parameter, and the wrong type for the second. So, when we go
9795 to instantiate the DECL, we regenerate it. */
9798 regenerate_decl_from_template (decl
, tmpl
)
9802 /* The most general version of TMPL. */
9804 /* The arguments used to instantiate DECL, from the most general
9811 args
= DECL_TI_ARGS (decl
);
9812 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9814 /* Unregister the specialization so that when we tsubst we will not
9815 just return DECL. We don't have to unregister DECL from TMPL
9816 because if would only be registered there if it were a partial
9817 instantiation of a specialization, which it isn't: it's a full
9819 gen_tmpl
= most_general_template (tmpl
);
9820 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9822 /* If the DECL was not unregistered then something peculiar is
9823 happening: we created a specialization but did not call
9824 register_specialization for it. */
9825 my_friendly_assert (unregistered
, 0);
9827 if (TREE_CODE (decl
) == VAR_DECL
)
9828 /* Make sure that we can see identifiers, and compute access
9829 correctly, for the class members used in the declaration of
9830 this static variable. */
9831 pushclass (DECL_CONTEXT (decl
), 2);
9833 /* Do the substitution to get the new declaration. */
9834 new_decl
= tsubst (code_pattern
, args
, tf_error
, NULL_TREE
);
9836 if (TREE_CODE (decl
) == VAR_DECL
)
9838 /* Set up DECL_INITIAL, since tsubst doesn't. */
9839 DECL_INITIAL (new_decl
) =
9840 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9841 tf_error
, DECL_TI_TEMPLATE (decl
));
9842 /* Pop the class context we pushed above. */
9845 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9847 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9849 DECL_INITIAL (new_decl
) = error_mark_node
;
9850 /* And don't complain about a duplicate definition. */
9851 DECL_INITIAL (decl
) = NULL_TREE
;
9854 /* The immediate parent of the new template is still whatever it was
9855 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9856 general template. We also reset the DECL_ASSEMBLER_NAME since
9857 tsubst always calculates the name as if the function in question
9858 were really a template instance, and sometimes, with friend
9859 functions, this is not so. See tsubst_friend_function for
9861 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9862 COPY_DECL_ASSEMBLER_NAME (decl
, new_decl
);
9863 COPY_DECL_RTL (decl
, new_decl
);
9864 DECL_USE_TEMPLATE (new_decl
) = DECL_USE_TEMPLATE (decl
);
9866 /* Call duplicate decls to merge the old and new declarations. */
9867 duplicate_decls (new_decl
, decl
);
9869 /* Now, re-register the specialization. */
9870 register_specialization (decl
, gen_tmpl
, args
);
9873 /* Produce the definition of D, a _DECL generated from a template. If
9874 DEFER_OK is non-zero, then we don't have to actually do the
9875 instantiation now; we just have to do it sometime. */
9878 instantiate_decl (d
, defer_ok
)
9882 tree tmpl
= DECL_TI_TEMPLATE (d
);
9883 tree args
= DECL_TI_ARGS (d
);
9888 int pattern_defined
;
9891 const char *file
= input_filename
;
9893 /* This function should only be used to instantiate templates for
9894 functions and static member variables. */
9895 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9896 || TREE_CODE (d
) == VAR_DECL
, 0);
9898 /* Don't instantiate cloned functions. Instead, instantiate the
9899 functions they cloned. */
9900 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
9901 d
= DECL_CLONED_FUNCTION (d
);
9903 if (DECL_TEMPLATE_INSTANTIATED (d
))
9904 /* D has already been instantiated. It might seem reasonable to
9905 check whether or not D is an explicit instantiation, and, if so,
9906 stop here. But when an explicit instantiation is deferred
9907 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9908 is set, even though we still need to do the instantiation. */
9911 /* If we already have a specialization of this declaration, then
9912 there's no reason to instantiate it. Note that
9913 retrieve_specialization gives us both instantiations and
9914 specializations, so we must explicitly check
9915 DECL_TEMPLATE_SPECIALIZATION. */
9916 gen_tmpl
= most_general_template (tmpl
);
9917 spec
= retrieve_specialization (gen_tmpl
, args
);
9918 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9921 /* This needs to happen before any tsubsting. */
9922 if (! push_tinst_level (d
))
9925 timevar_push (TV_PARSE
);
9927 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9928 for the instantiation. This is not always the most general
9929 template. Consider, for example:
9932 struct S { template <class U> void f();
9933 template <> void f<int>(); };
9935 and an instantiation of S<double>::f<int>. We want TD to be the
9936 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9938 while (/* An instantiation cannot have a definition, so we need a
9939 more general template. */
9940 DECL_TEMPLATE_INSTANTIATION (td
)
9941 /* We must also deal with friend templates. Given:
9943 template <class T> struct S {
9944 template <class U> friend void f() {};
9947 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9948 so far as the language is concerned, but that's still
9949 where we get the pattern for the instantiation from. On
9950 other hand, if the definition comes outside the class, say:
9952 template <class T> struct S {
9953 template <class U> friend void f();
9955 template <class U> friend void f() {}
9957 we don't need to look any further. That's what the check for
9958 DECL_INITIAL is for. */
9959 || (TREE_CODE (d
) == FUNCTION_DECL
9960 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9961 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
))))
9963 /* The present template, TD, should not be a definition. If it
9964 were a definition, we should be using it! Note that we
9965 cannot restructure the loop to just keep going until we find
9966 a template with a definition, since that might go too far if
9967 a specialization was declared, but not defined. */
9968 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9969 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9972 /* Fetch the more general template. */
9973 td
= DECL_TI_TEMPLATE (td
);
9976 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9978 if (TREE_CODE (d
) == FUNCTION_DECL
)
9979 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
9981 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9983 lineno
= DECL_SOURCE_LINE (d
);
9984 input_filename
= DECL_SOURCE_FILE (d
);
9986 if (pattern_defined
)
9988 /* Let the repository code that this template definition is
9991 The repository doesn't need to know about cloned functions
9992 because they never actually show up in the object file. It
9993 does need to know about the clones; those are the symbols
9994 that the linker will be emitting error messages about. */
9995 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d
)
9996 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d
))
10000 for (t
= TREE_CHAIN (d
);
10001 t
&& DECL_CLONED_FUNCTION_P (t
);
10002 t
= TREE_CHAIN (t
))
10003 repo_template_used (t
);
10006 repo_template_used (d
);
10008 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
10010 if (flag_alt_external_templates
)
10012 if (interface_unknown
)
10013 warn_if_unknown_interface (d
);
10015 else if (DECL_INTERFACE_KNOWN (code_pattern
))
10017 DECL_INTERFACE_KNOWN (d
) = 1;
10018 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
10021 warn_if_unknown_interface (code_pattern
);
10025 import_export_decl (d
);
10030 /* Recheck the substitutions to obtain any warning messages
10031 about ignoring cv qualifiers. */
10032 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
10033 tree type
= TREE_TYPE (gen
);
10035 if (TREE_CODE (gen
) == FUNCTION_DECL
)
10037 tsubst (DECL_ARGUMENTS (gen
), args
, tf_error
| tf_warning
, d
);
10038 tsubst (TYPE_RAISES_EXCEPTIONS (type
), args
,
10039 tf_error
| tf_warning
, d
);
10040 /* Don't simply tsubst the function type, as that will give
10041 duplicate warnings about poor parameter qualifications.
10042 The function arguments are the same as the decl_arguments
10043 without the top level cv qualifiers. */
10044 type
= TREE_TYPE (type
);
10046 tsubst (type
, args
, tf_error
| tf_warning
, d
);
10049 if (TREE_CODE (d
) == VAR_DECL
&& DECL_INITIALIZED_IN_CLASS_P (d
)
10050 && DECL_INITIAL (d
) == NULL_TREE
)
10051 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10053 /* Reject all external templates except inline functions. */
10054 else if (DECL_INTERFACE_KNOWN (d
)
10055 && ! DECL_NOT_REALLY_EXTERN (d
)
10056 && ! (TREE_CODE (d
) == FUNCTION_DECL
10057 && DECL_INLINE (d
)))
10059 /* Defer all other templates, unless we have been explicitly
10060 forbidden from doing so. We restore the source position here
10061 because it's used by add_pending_template. */
10062 else if (! pattern_defined
|| defer_ok
)
10065 input_filename
= file
;
10067 if (at_eof
&& !pattern_defined
10068 && DECL_EXPLICIT_INSTANTIATION (d
))
10071 The definition of a non-exported function template, a
10072 non-exported member function template, or a non-exported
10073 member function or static data member of a class template
10074 shall be present in every translation unit in which it is
10075 explicitly instantiated. */
10077 ("explicit instantiation of `%D' but no definition available", d
);
10079 add_pending_template (d
);
10083 need_push
= !global_bindings_p ();
10085 push_to_top_level ();
10087 /* We're now committed to instantiating this template. Mark it as
10088 instantiated so that recursive calls to instantiate_decl do not
10089 try to instantiate it again. */
10090 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
10092 /* Regenerate the declaration in case the template has been modified
10093 by a subsequent redeclaration. */
10094 regenerate_decl_from_template (d
, td
);
10096 /* We already set the file and line above. Reset them now in case
10097 they changed as a result of calling regenerate_decl_from_template. */
10098 lineno
= DECL_SOURCE_LINE (d
);
10099 input_filename
= DECL_SOURCE_FILE (d
);
10101 if (TREE_CODE (d
) == VAR_DECL
)
10103 DECL_IN_AGGR_P (d
) = 0;
10104 if (DECL_INTERFACE_KNOWN (d
))
10105 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
10108 DECL_EXTERNAL (d
) = 1;
10109 DECL_NOT_REALLY_EXTERN (d
) = 1;
10111 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0);
10113 else if (TREE_CODE (d
) == FUNCTION_DECL
)
10115 htab_t saved_local_specializations
;
10117 /* Save away the current list, in case we are instantiating one
10118 template from within the body of another. */
10119 saved_local_specializations
= local_specializations
;
10121 /* Set up the list of local specializations. */
10122 local_specializations
= htab_create (37,
10127 /* Set up context. */
10128 start_function (NULL_TREE
, d
, NULL_TREE
, SF_PRE_PARSED
);
10130 /* Substitute into the body of the function. */
10131 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
10132 tf_error
| tf_warning
, tmpl
);
10134 /* We don't need the local specializations any more. */
10135 htab_delete (local_specializations
);
10136 local_specializations
= saved_local_specializations
;
10138 /* Finish the function. */
10139 expand_body (finish_function (0));
10142 /* We're not deferring instantiation any more. */
10143 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
10146 pop_from_top_level ();
10150 input_filename
= file
;
10152 pop_tinst_level ();
10154 timevar_pop (TV_PARSE
);
10159 /* Run through the list of templates that we wish we could
10160 instantiate, and instantiate any we can. */
10163 instantiate_pending_templates ()
10166 tree last
= NULL_TREE
;
10167 int instantiated_something
= 0;
10174 t
= &pending_templates
;
10177 tree instantiation
= TREE_VALUE (*t
);
10179 reopen_tinst_level (TREE_PURPOSE (*t
));
10181 if (TYPE_P (instantiation
))
10185 if (!COMPLETE_TYPE_P (instantiation
))
10187 instantiate_class_template (instantiation
);
10188 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
10189 for (fn
= TYPE_METHODS (instantiation
);
10191 fn
= TREE_CHAIN (fn
))
10192 if (! DECL_ARTIFICIAL (fn
))
10193 instantiate_decl (fn
, /*defer_ok=*/0);
10194 if (COMPLETE_TYPE_P (instantiation
))
10196 instantiated_something
= 1;
10201 if (COMPLETE_TYPE_P (instantiation
))
10202 /* If INSTANTIATION has been instantiated, then we don't
10203 need to consider it again in the future. */
10204 *t
= TREE_CHAIN (*t
);
10208 t
= &TREE_CHAIN (*t
);
10213 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
10214 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
10216 instantiation
= instantiate_decl (instantiation
,
10218 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
10220 instantiated_something
= 1;
10225 if (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
10226 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
10227 /* If INSTANTIATION has been instantiated, then we don't
10228 need to consider it again in the future. */
10229 *t
= TREE_CHAIN (*t
);
10233 t
= &TREE_CHAIN (*t
);
10237 current_tinst_level
= NULL_TREE
;
10239 last_pending_template
= last
;
10241 while (reconsider
);
10243 return instantiated_something
;
10246 /* Substitute ARGVEC into T, which is a list of initializers for
10247 either base class or a non-static data member. The TREE_PURPOSEs
10248 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10249 instantiate_decl. */
10252 tsubst_initializer_list (t
, argvec
)
10255 tree first
= NULL_TREE
;
10258 for (; t
; t
= TREE_CHAIN (t
))
10264 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
, tf_error
| tf_warning
,
10266 init
= tsubst_expr (TREE_VALUE (t
), argvec
, tf_error
| tf_warning
,
10271 else if (TREE_CODE (init
) == TREE_LIST
)
10272 for (val
= init
; val
; val
= TREE_CHAIN (val
))
10273 TREE_VALUE (val
) = convert_from_reference (TREE_VALUE (val
));
10275 init
= convert_from_reference (init
);
10277 *p
= build_tree_list (decl
, init
);
10278 p
= &TREE_CHAIN (*p
);
10283 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10286 set_current_access_from_decl (decl
)
10289 if (TREE_PRIVATE (decl
))
10290 current_access_specifier
= access_private_node
;
10291 else if (TREE_PROTECTED (decl
))
10292 current_access_specifier
= access_protected_node
;
10294 current_access_specifier
= access_public_node
;
10297 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10298 is the instantiation (which should have been created with
10299 start_enum) and ARGS are the template arguments to use. */
10302 tsubst_enum (tag
, newtag
, args
)
10309 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
10313 /* Note that in a template enum, the TREE_VALUE is the
10314 CONST_DECL, not the corresponding INTEGER_CST. */
10315 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
10316 args
, tf_error
| tf_warning
,
10319 /* Give this enumeration constant the correct access. */
10320 set_current_access_from_decl (TREE_VALUE (e
));
10322 /* Actually build the enumerator itself. */
10323 build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
10326 finish_enum (newtag
);
10327 DECL_SOURCE_LINE (TYPE_NAME (newtag
)) = DECL_SOURCE_LINE (TYPE_NAME (tag
));
10328 DECL_SOURCE_FILE (TYPE_NAME (newtag
)) = DECL_SOURCE_FILE (TYPE_NAME (tag
));
10331 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10332 its type -- but without substituting the innermost set of template
10333 arguments. So, innermost set of template parameters will appear in
10334 the type. If CONTEXTP is non-NULL, then the partially substituted
10335 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10336 will be filled in with the substituted template parameters, if it
10340 get_mostly_instantiated_function_type (decl
, contextp
, tparmsp
)
10345 tree context
= NULL_TREE
;
10352 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10353 targs
= DECL_TI_ARGS (decl
);
10354 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
10355 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
10357 /* There should be as many levels of arguments as there are levels
10359 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
10361 fn_type
= TREE_TYPE (tmpl
);
10362 if (DECL_STATIC_FUNCTION_P (decl
))
10363 context
= DECL_CONTEXT (decl
);
10365 if (parm_depth
== 1)
10366 /* No substitution is necessary. */
10373 /* Replace the innermost level of the TARGS with NULL_TREEs to
10374 let tsubst know not to substitute for those parameters. */
10375 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
10376 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
10377 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
10378 TMPL_ARGS_LEVEL (targs
, i
));
10379 SET_TMPL_ARGS_LEVEL (partial_args
,
10380 TMPL_ARGS_DEPTH (targs
),
10381 make_tree_vec (DECL_NTPARMS (tmpl
)));
10383 /* Now, do the (partial) substitution to figure out the
10384 appropriate function type. */
10385 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
10386 if (DECL_STATIC_FUNCTION_P (decl
))
10387 context
= tsubst (context
, partial_args
, tf_error
, NULL_TREE
);
10389 /* Substitute into the template parameters to obtain the real
10390 innermost set of parameters. This step is important if the
10391 innermost set of template parameters contains value
10392 parameters whose types depend on outer template parameters. */
10393 TREE_VEC_LENGTH (partial_args
)--;
10394 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
10398 *contextp
= context
;
10405 /* Return truthvalue if we're processing a template different from
10406 the last one involved in diagnostics. */
10408 problematic_instantiation_changed ()
10410 return last_template_error_tick
!= tinst_level_tick
;
10413 /* Remember current template involved in diagnostics. */
10415 record_last_problematic_instantiation ()
10417 last_template_error_tick
= tinst_level_tick
;
10421 current_instantiation ()
10423 return current_tinst_level
;
10426 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10427 type. Return zero for ok, non-zero for disallowed. Issue error and
10428 warning messages under control of COMPLAIN. */
10431 invalid_nontype_parm_type_p (type
, complain
)
10433 tsubst_flags_t complain
;
10435 if (INTEGRAL_TYPE_P (type
))
10437 else if (POINTER_TYPE_P (type
))
10439 else if (TYPE_PTRMEM_P (type
))
10441 else if (TYPE_PTRMEMFUNC_P (type
))
10443 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
10445 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
10448 if (complain
& tf_error
)
10449 error ("`%#T' is not a valid type for a template constant parameter",