1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) PARAMS ((tree
, void*));
52 extern struct obstack permanent_obstack
;
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the
57 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
58 the instantiate request occurred; the TREE_VALUE is a either a DECL
59 (for a function or static data member), or a TYPE (for a class)
60 indicating what we are hoping to instantiate. */
61 static tree pending_templates
;
62 static tree
*template_tail
= &pending_templates
;
64 static tree maybe_templates
;
65 static tree
*maybe_template_tail
= &maybe_templates
;
67 int processing_template_parmlist
;
68 static int template_header_count
;
70 static tree saved_trees
;
71 static varray_type inline_parm_levels
;
72 static size_t inline_parm_levels_used
;
74 static tree current_tinst_level
;
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
79 static htab_t local_specializations
;
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 #define UNIFY_ALLOW_NONE 0
85 #define UNIFY_ALLOW_MORE_CV_QUAL 1
86 #define UNIFY_ALLOW_LESS_CV_QUAL 2
87 #define UNIFY_ALLOW_DERIVED 4
88 #define UNIFY_ALLOW_INTEGER 8
89 #define UNIFY_ALLOW_OUTER_LEVEL 16
90 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
91 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
93 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
94 virtual, or a base class of a virtual
96 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
97 type with the desired type. */
99 static int resolve_overloaded_unification
PARAMS ((tree
, tree
, tree
, tree
,
100 unification_kind_t
, int));
101 static int try_one_overload
PARAMS ((tree
, tree
, tree
, tree
, tree
,
102 unification_kind_t
, int));
103 static int unify
PARAMS ((tree
, tree
, tree
, tree
, int));
104 static void add_pending_template
PARAMS ((tree
));
105 static int push_tinst_level
PARAMS ((tree
));
106 static void reopen_tinst_level
PARAMS ((tree
));
107 static tree classtype_mangled_name
PARAMS ((tree
));
108 static char *mangle_class_name_for_template
PARAMS ((const char *, tree
, tree
));
109 static tree tsubst_initializer_list
PARAMS ((tree
, tree
));
110 static int list_eq
PARAMS ((tree
, tree
));
111 static tree get_class_bindings
PARAMS ((tree
, tree
, tree
));
112 static tree coerce_template_parms
PARAMS ((tree
, tree
, tree
, int, int));
113 static void tsubst_enum
PARAMS ((tree
, tree
, tree
));
114 static tree add_to_template_args
PARAMS ((tree
, tree
));
115 static tree add_outermost_template_args
PARAMS ((tree
, tree
));
116 static int maybe_adjust_types_for_deduction
PARAMS ((unification_kind_t
, tree
*,
118 static int type_unification_real
PARAMS ((tree
, tree
, tree
, tree
,
119 int, unification_kind_t
, int, int));
120 static void note_template_header
PARAMS ((int));
121 static tree maybe_fold_nontype_arg
PARAMS ((tree
));
122 static tree convert_nontype_argument
PARAMS ((tree
, tree
));
123 static tree convert_template_argument
PARAMS ((tree
, tree
, tree
, int,
125 static tree get_bindings_overload
PARAMS ((tree
, tree
, tree
));
126 static int for_each_template_parm
PARAMS ((tree
, tree_fn_t
, void*));
127 static tree build_template_parm_index
PARAMS ((int, int, int, tree
, tree
));
128 static int inline_needs_template_parms
PARAMS ((tree
));
129 static void push_inline_template_parms_recursive
PARAMS ((tree
, int));
130 static tree retrieve_specialization
PARAMS ((tree
, tree
));
131 static tree retrieve_local_specialization
PARAMS ((tree
));
132 static tree register_specialization
PARAMS ((tree
, tree
, tree
));
133 static void register_local_specialization
PARAMS ((tree
, tree
));
134 static int unregister_specialization
PARAMS ((tree
, tree
));
135 static tree reduce_template_parm_level
PARAMS ((tree
, tree
, int));
136 static tree build_template_decl
PARAMS ((tree
, tree
));
137 static int mark_template_parm
PARAMS ((tree
, void *));
138 static tree tsubst_friend_function
PARAMS ((tree
, tree
));
139 static tree tsubst_friend_class
PARAMS ((tree
, tree
));
140 static tree get_bindings_real
PARAMS ((tree
, tree
, tree
, int, int, int));
141 static int template_decl_level
PARAMS ((tree
));
142 static tree maybe_get_template_decl_from_type_decl
PARAMS ((tree
));
143 static int check_cv_quals_for_unify
PARAMS ((int, tree
, tree
));
144 static tree tsubst_template_arg_vector
PARAMS ((tree
, tree
, int));
145 static tree tsubst_template_parms
PARAMS ((tree
, tree
, int));
146 static void regenerate_decl_from_template
PARAMS ((tree
, tree
));
147 static tree most_specialized
PARAMS ((tree
, tree
, tree
));
148 static tree most_specialized_class
PARAMS ((tree
, tree
));
149 static void set_mangled_name_for_template_decl
PARAMS ((tree
));
150 static int template_class_depth_real
PARAMS ((tree
, int));
151 static tree tsubst_aggr_type
PARAMS ((tree
, tree
, int, tree
, int));
152 static tree tsubst_decl
PARAMS ((tree
, tree
, tree
, tree
));
153 static tree tsubst_arg_types
PARAMS ((tree
, tree
, int, tree
));
154 static tree tsubst_function_type
PARAMS ((tree
, tree
, int, tree
));
155 static void check_specialization_scope
PARAMS ((void));
156 static tree process_partial_specialization
PARAMS ((tree
));
157 static void set_current_access_from_decl
PARAMS ((tree
));
158 static void check_default_tmpl_args
PARAMS ((tree
, tree
, int, int));
159 static tree tsubst_call_declarator_parms
PARAMS ((tree
, tree
, int, tree
));
160 static tree get_template_base_recursive
PARAMS ((tree
, tree
,
161 tree
, tree
, tree
, int));
162 static tree get_template_base
PARAMS ((tree
, tree
, tree
, tree
));
163 static int verify_class_unification
PARAMS ((tree
, tree
, tree
));
164 static tree try_class_unification
PARAMS ((tree
, tree
, tree
, tree
));
165 static int coerce_template_template_parms
PARAMS ((tree
, tree
, int,
167 static tree determine_specialization
PARAMS ((tree
, tree
, tree
*, int));
168 static int template_args_equal
PARAMS ((tree
, tree
));
169 static void tsubst_default_arguments
PARAMS ((tree
));
170 static tree for_each_template_parm_r
PARAMS ((tree
*, int *, void *));
171 static tree instantiate_clone
PARAMS ((tree
, tree
));
172 static tree copy_default_args_to_explicit_spec_1
PARAMS ((tree
, tree
));
173 static void copy_default_args_to_explicit_spec
PARAMS ((tree
));
174 static int invalid_nontype_parm_type_p
PARAMS ((tree
, int));
176 /* Called once to initialize pt.c. */
181 ggc_add_tree_root (&pending_templates
, 1);
182 ggc_add_tree_root (&maybe_templates
, 1);
183 ggc_add_tree_root (&saved_trees
, 1);
184 ggc_add_tree_root (¤t_tinst_level
, 1);
187 /* Do any processing required when DECL (a member template declaration
188 using TEMPLATE_PARAMETERS as its innermost parameter list) is
189 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
190 it is a specialization, in which case the DECL itself is returned. */
193 finish_member_template_decl (decl
)
196 if (decl
== NULL_TREE
|| decl
== void_type_node
)
198 else if (decl
== error_mark_node
)
199 /* By returning NULL_TREE, the parser will just ignore this
200 declaration. We have already issued the error. */
202 else if (TREE_CODE (decl
) == TREE_LIST
)
204 /* Assume that the class is the only declspec. */
205 decl
= TREE_VALUE (decl
);
206 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
207 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
209 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
210 check_member_template (tmpl
);
215 else if (TREE_CODE (decl
) == FIELD_DECL
)
216 cp_error ("data member `%D' cannot be a member template", decl
);
217 else if (DECL_TEMPLATE_INFO (decl
))
219 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
221 check_member_template (DECL_TI_TEMPLATE (decl
));
222 return DECL_TI_TEMPLATE (decl
);
228 cp_error ("invalid member template declaration `%D'", decl
);
230 return error_mark_node
;
233 /* Returns the template nesting level of the indicated class TYPE.
243 A<T>::B<U> has depth two, while A<T> has depth one.
244 Both A<T>::B<int> and A<int>::B<U> have depth one, if
245 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
248 This function is guaranteed to return 0 if passed NULL_TREE so
249 that, for example, `template_class_depth (current_class_type)' is
253 template_class_depth_real (type
, count_specializations
)
255 int count_specializations
;
260 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
261 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
262 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
264 if (TREE_CODE (type
) != FUNCTION_DECL
)
266 if (CLASSTYPE_TEMPLATE_INFO (type
)
267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
268 && ((count_specializations
269 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
270 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
275 if (DECL_TEMPLATE_INFO (type
)
276 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
277 && ((count_specializations
278 && DECL_TEMPLATE_SPECIALIZATION (type
))
279 || uses_template_parms (DECL_TI_ARGS (type
))))
287 /* Returns the template nesting level of the indicated class TYPE.
288 Like template_class_depth_real, but instantiations do not count in
292 template_class_depth (type
)
295 return template_class_depth_real (type
, /*count_specializations=*/0);
298 /* Returns 1 if processing DECL as part of do_pending_inlines
299 needs us to push template parms. */
302 inline_needs_template_parms (decl
)
305 if (! DECL_TEMPLATE_INFO (decl
))
308 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
309 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
312 /* Subroutine of maybe_begin_member_template_processing.
313 Push the template parms in PARMS, starting from LEVELS steps into the
314 chain, and ending at the beginning, since template parms are listed
318 push_inline_template_parms_recursive (parmlist
, levels
)
322 tree parms
= TREE_VALUE (parmlist
);
326 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
328 ++processing_template_decl
;
329 current_template_parms
330 = tree_cons (size_int (processing_template_decl
),
331 parms
, current_template_parms
);
332 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
335 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
337 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
338 my_friendly_assert (DECL_P (parm
), 0);
340 switch (TREE_CODE (parm
))
349 /* Make a CONST_DECL as is done in process_template_parm.
350 It is ugly that we recreate this here; the original
351 version built in process_template_parm is no longer
353 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
355 DECL_ARTIFICIAL (decl
) = 1;
356 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
357 SET_DECL_TEMPLATE_PARM_P (decl
);
363 my_friendly_abort (0);
368 /* Restore the template parameter context for a member template or
369 a friend template defined in a class definition. */
372 maybe_begin_member_template_processing (decl
)
378 if (inline_needs_template_parms (decl
))
380 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
381 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
383 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
386 parms
= TREE_CHAIN (parms
);
389 push_inline_template_parms_recursive (parms
, levels
);
392 /* Remember how many levels of template parameters we pushed so that
393 we can pop them later. */
394 if (!inline_parm_levels
)
395 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
396 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
397 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
398 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
399 ++inline_parm_levels_used
;
402 /* Undo the effects of begin_member_template_processing. */
405 maybe_end_member_template_processing ()
409 if (!inline_parm_levels_used
)
412 --inline_parm_levels_used
;
414 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
417 --processing_template_decl
;
418 current_template_parms
= TREE_CHAIN (current_template_parms
);
423 /* Returns non-zero iff T is a member template function. We must be
426 template <class T> class C { void f(); }
428 Here, f is a template function, and a member, but not a member
429 template. This function does not concern itself with the origin of
430 T, only its present state. So if we have
432 template <class T> class C { template <class U> void f(U); }
434 then neither C<int>::f<char> nor C<T>::f<double> is considered
435 to be a member template. But, `template <class U> void
436 C<int>::f(U)' is considered a member template. */
439 is_member_template (t
)
442 if (!DECL_FUNCTION_TEMPLATE_P (t
))
443 /* Anything that isn't a function or a template function is
444 certainly not a member template. */
447 /* A local class can't have member templates. */
448 if (decl_function_context (t
))
451 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
452 /* If there are more levels of template parameters than
453 there are template classes surrounding the declaration,
454 then we have a member template. */
455 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
456 template_class_depth (DECL_CONTEXT (t
))));
460 /* Returns non-zero iff T is a member template class. See
461 is_member_template for a description of what precisely constitutes
462 a member template. */
465 is_member_template_class (t
)
468 if (!DECL_CLASS_TEMPLATE_P (t
))
469 /* Anything that isn't a class template, is certainly not a member
473 if (!DECL_CLASS_SCOPE_P (t
))
474 /* Anything whose context isn't a class type is surely not a
478 /* If there are more levels of template parameters than there are
479 template classes surrounding the declaration, then we have a
481 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
482 template_class_depth (DECL_CONTEXT (t
)));
486 /* Return a new template argument vector which contains all of ARGS,
487 but has as its innermost set of arguments the EXTRA_ARGS. */
490 add_to_template_args (args
, extra_args
)
499 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
500 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
502 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
503 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
505 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
506 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
511 /* Like add_to_template_args, but only the outermost ARGS are added to
512 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
513 (EXTRA_ARGS) levels are added. This function is used to combine
514 the template arguments from a partial instantiation with the
515 template arguments used to attain the full instantiation from the
516 partial instantiation. */
519 add_outermost_template_args (args
, extra_args
)
525 /* If there are more levels of EXTRA_ARGS than there are ARGS,
526 something very fishy is going on. */
527 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
530 /* If *all* the new arguments will be the EXTRA_ARGS, just return
532 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
535 /* For the moment, we make ARGS look like it contains fewer levels. */
536 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
538 new_args
= add_to_template_args (args
, extra_args
);
540 /* Now, we restore ARGS to its full dimensions. */
541 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
546 /* Return the N levels of innermost template arguments from the ARGS. */
549 get_innermost_template_args (args
, n
)
557 my_friendly_assert (n
>= 0, 20000603);
559 /* If N is 1, just return the innermost set of template arguments. */
561 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
563 /* If we're not removing anything, just return the arguments we were
565 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
566 my_friendly_assert (extra_levels
>= 0, 20000603);
567 if (extra_levels
== 0)
570 /* Make a new set of arguments, not containing the outer arguments. */
571 new_args
= make_tree_vec (n
);
572 for (i
= 1; i
<= n
; ++i
)
573 SET_TMPL_ARGS_LEVEL (new_args
, i
,
574 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
579 /* We've got a template header coming up; push to a new level for storing
583 begin_template_parm_list ()
585 /* We use a non-tag-transparent scope here, which causes pushtag to
586 put tags in this scope, rather than in the enclosing class or
587 namespace scope. This is the right thing, since we want
588 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
589 global template class, push_template_decl handles putting the
590 TEMPLATE_DECL into top-level scope. For a nested template class,
593 template <class T> struct S1 {
594 template <class T> struct S2 {};
597 pushtag contains special code to call pushdecl_with_scope on the
598 TEMPLATE_DECL for S2. */
599 begin_scope (sk_template_parms
);
600 ++processing_template_decl
;
601 ++processing_template_parmlist
;
602 note_template_header (0);
605 /* This routine is called when a specialization is declared. If it is
606 illegal to declare a specialization here, an error is reported. */
609 check_specialization_scope ()
611 tree scope
= current_scope ();
615 An explicit specialization shall be declared in the namespace of
616 which the template is a member, or, for member templates, in the
617 namespace of which the enclosing class or enclosing class
618 template is a member. An explicit specialization of a member
619 function, member class or static data member of a class template
620 shall be declared in the namespace of which the class template
622 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
623 cp_error ("explicit specialization in non-namespace scope `%D'",
628 In an explicit specialization declaration for a member of a class
629 template or a member template that appears in namespace scope,
630 the member template and some of its enclosing class templates may
631 remain unspecialized, except that the declaration shall not
632 explicitly specialize a class member template if its enclosing
633 class templates are not explicitly specialized as well. */
634 if (current_template_parms
)
635 cp_error ("enclosing class templates are not explicitly specialized");
638 /* We've just seen template <>. */
641 begin_specialization ()
643 begin_scope (sk_template_spec
);
644 note_template_header (1);
645 check_specialization_scope ();
648 /* Called at then end of processing a declaration preceded by
652 end_specialization ()
655 reset_specialization ();
658 /* Any template <>'s that we have seen thus far are not referring to a
659 function specialization. */
662 reset_specialization ()
664 processing_specialization
= 0;
665 template_header_count
= 0;
668 /* We've just seen a template header. If SPECIALIZATION is non-zero,
669 it was of the form template <>. */
672 note_template_header (specialization
)
675 processing_specialization
= specialization
;
676 template_header_count
++;
679 /* We're beginning an explicit instantiation. */
682 begin_explicit_instantiation ()
684 ++processing_explicit_instantiation
;
689 end_explicit_instantiation ()
691 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
692 --processing_explicit_instantiation
;
695 /* The TYPE is being declared. If it is a template type, that means it
696 is a partial specialization. Do appropriate error-checking. */
699 maybe_process_partial_specialization (type
)
702 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
704 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
705 && !COMPLETE_TYPE_P (type
))
707 if (current_namespace
708 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
710 cp_pedwarn ("specializing `%#T' in different namespace", type
);
711 cp_pedwarn_at (" from definition of `%#D'",
712 CLASSTYPE_TI_TEMPLATE (type
));
714 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
715 if (processing_template_decl
)
716 push_template_decl (TYPE_MAIN_DECL (type
));
718 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
719 cp_error ("specialization of `%T' after instantiation", type
);
721 else if (processing_specialization
)
722 cp_error ("explicit specialization of non-template `%T'", type
);
725 /* Retrieve the specialization (in the sense of [temp.spec] - a
726 specialization is either an instantiation or an explicit
727 specialization) of TMPL for the given template ARGS. If there is
728 no such specialization, return NULL_TREE. The ARGS are a vector of
729 arguments, or a vector of vectors of arguments, in the case of
730 templates with more than one level of parameters. */
733 retrieve_specialization (tmpl
, args
)
739 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
741 /* There should be as many levels of arguments as there are
742 levels of parameters. */
743 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
744 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
747 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
750 if (comp_template_args (TREE_PURPOSE (s
), args
))
751 return TREE_VALUE (s
);
756 /* Like retrieve_specialization, but for local declarations. */
759 retrieve_local_specialization (tmpl
)
762 return (tree
) htab_find (local_specializations
, tmpl
);
765 /* Returns non-zero iff DECL is a specialization of TMPL. */
768 is_specialization_of (decl
, tmpl
)
774 if (TREE_CODE (decl
) == FUNCTION_DECL
)
778 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
784 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
786 for (t
= TREE_TYPE (decl
);
788 t
= CLASSTYPE_USE_TEMPLATE (t
)
789 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
790 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
797 /* Register the specialization SPEC as a specialization of TMPL with
798 the indicated ARGS. Returns SPEC, or an equivalent prior
799 declaration, if available. */
802 register_specialization (spec
, tmpl
, args
)
809 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
811 if (TREE_CODE (spec
) == FUNCTION_DECL
812 && uses_template_parms (DECL_TI_ARGS (spec
)))
813 /* This is the FUNCTION_DECL for a partial instantiation. Don't
814 register it; we want the corresponding TEMPLATE_DECL instead.
815 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
816 the more obvious `uses_template_parms (spec)' to avoid problems
817 with default function arguments. In particular, given
820 template <class T> void f(T t1, T t = T())
822 the default argument expression is not substituted for in an
823 instantiation unless and until it is actually needed. */
826 /* There should be as many levels of arguments as there are
827 levels of parameters. */
828 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
829 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
832 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
836 tree fn
= TREE_VALUE (s
);
838 /* We can sometimes try to re-register a specialization that we've
839 already got. In particular, regenerate_decl_from_template
840 calls duplicate_decls which will update the specialization
841 list. But, we'll still get called again here anyhow. It's
842 more convenient to simply allow this than to try to prevent it. */
845 else if (comp_template_args (TREE_PURPOSE (s
), args
))
847 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
849 if (DECL_TEMPLATE_INSTANTIATION (fn
))
852 || DECL_EXPLICIT_INSTANTIATION (fn
))
854 cp_error ("specialization of %D after instantiation",
860 /* This situation should occur only if the first
861 specialization is an implicit instantiation,
862 the second is an explicit specialization, and
863 the implicit instantiation has not yet been
864 used. That situation can occur if we have
865 implicitly instantiated a member function and
866 then specialized it later.
868 We can also wind up here if a friend
869 declaration that looked like an instantiation
870 turns out to be a specialization:
872 template <class T> void foo(T);
873 class S { friend void foo<>(int) };
874 template <> void foo(int);
876 We transform the existing DECL in place so that
877 any pointers to it become pointers to the
880 If there was a definition for the template, but
881 not for the specialization, we want this to
882 look as if there is no definition, and vice
884 DECL_INITIAL (fn
) = NULL_TREE
;
885 duplicate_decls (spec
, fn
);
890 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
892 duplicate_decls (spec
, fn
);
899 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
900 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
905 /* Unregister the specialization SPEC as a specialization of TMPL.
906 Returns nonzero if the SPEC was listed as a specialization of
910 unregister_specialization (spec
, tmpl
)
916 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
918 s
= &TREE_CHAIN (*s
))
919 if (TREE_VALUE (*s
) == spec
)
921 *s
= TREE_CHAIN (*s
);
928 /* Like register_specialization, but for local declarations. We are
929 registering SPEC, an instantiation of TMPL. */
932 register_local_specialization (spec
, tmpl
)
938 slot
= htab_find_slot (local_specializations
, tmpl
, INSERT
);
942 /* Print the list of candidate FNS in an error message. */
945 print_candidates (fns
)
950 const char *str
= "candidates are:";
952 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
956 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
957 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
962 /* Returns the template (one of the functions given by TEMPLATE_ID)
963 which can be specialized to match the indicated DECL with the
964 explicit template args given in TEMPLATE_ID. The DECL may be
965 NULL_TREE if none is available. In that case, the functions in
966 TEMPLATE_ID are non-members.
968 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
969 specialization of a member template.
971 The template args (those explicitly specified and those deduced)
972 are output in a newly created vector *TARGS_OUT.
974 If it is impossible to determine the result, an error message is
975 issued. The error_mark_node is returned to indicate failure. */
978 determine_specialization (template_id
, decl
, targs_out
,
979 need_member_template
)
983 int need_member_template
;
988 tree candidates
= NULL_TREE
;
989 tree templates
= NULL_TREE
;
991 *targs_out
= NULL_TREE
;
993 if (template_id
== error_mark_node
)
994 return error_mark_node
;
996 fns
= TREE_OPERAND (template_id
, 0);
997 explicit_targs
= TREE_OPERAND (template_id
, 1);
999 if (fns
== error_mark_node
)
1000 return error_mark_node
;
1002 /* Check for baselinks. */
1003 if (BASELINK_P (fns
))
1004 fns
= TREE_VALUE (fns
);
1006 if (!is_overloaded_fn (fns
))
1008 cp_error ("`%D' is not a function template", fns
);
1009 return error_mark_node
;
1012 for (; fns
; fns
= OVL_NEXT (fns
))
1016 tree fn
= OVL_CURRENT (fns
);
1018 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1019 /* DECL might be a specialization of FN. */
1021 else if (need_member_template
)
1022 /* FN is an ordinary member function, and we need a
1023 specialization of a member template. */
1025 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1026 /* We can get IDENTIFIER_NODEs here in certain erroneous
1029 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1030 /* This is just an ordinary non-member function. Nothing can
1031 be a specialization of that. */
1035 tree decl_arg_types
;
1037 /* This is an ordinary member function. However, since
1038 we're here, we can assume it's enclosing class is a
1039 template class. For example,
1041 template <typename T> struct S { void f(); };
1042 template <> void S<int>::f() {}
1044 Here, S<int>::f is a non-template, but S<int> is a
1045 template class. If FN has the same type as DECL, we
1046 might be in business. */
1047 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1048 TREE_TYPE (TREE_TYPE (fn
))))
1049 /* The return types differ. */
1052 /* Adjust the type of DECL in case FN is a static member. */
1053 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1054 if (DECL_STATIC_FUNCTION_P (fn
)
1055 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1056 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1058 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1061 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1066 /* See whether this function might be a specialization of this
1068 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1071 /* We cannot deduce template arguments that when used to
1072 specialize TMPL will produce DECL. */
1075 /* Save this template, and the arguments deduced. */
1076 templates
= tree_cons (targs
, tmpl
, templates
);
1079 if (templates
&& TREE_CHAIN (templates
))
1085 It is possible for a specialization with a given function
1086 signature to be instantiated from more than one function
1087 template. In such cases, explicit specification of the
1088 template arguments must be used to uniquely identify the
1089 function template specialization being specialized.
1091 Note that here, there's no suggestion that we're supposed to
1092 determine which of the candidate templates is most
1093 specialized. However, we, also have:
1097 Partial ordering of overloaded function template
1098 declarations is used in the following contexts to select
1099 the function template to which a function template
1100 specialization refers:
1102 -- when an explicit specialization refers to a function
1105 So, we do use the partial ordering rules, at least for now.
1106 This extension can only serve to make illegal programs legal,
1107 so it's safe. And, there is strong anecdotal evidence that
1108 the committee intended the partial ordering rules to apply;
1109 the EDG front-end has that behavior, and John Spicer claims
1110 that the committee simply forgot to delete the wording in
1111 [temp.expl.spec]. */
1112 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1113 if (tmpl
&& tmpl
!= error_mark_node
)
1115 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1116 templates
= tree_cons (targs
, tmpl
, NULL_TREE
);
1120 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1122 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1124 return error_mark_node
;
1126 else if ((templates
&& TREE_CHAIN (templates
))
1127 || (candidates
&& TREE_CHAIN (candidates
))
1128 || (templates
&& candidates
))
1130 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1132 chainon (candidates
, templates
);
1133 print_candidates (candidates
);
1134 return error_mark_node
;
1137 /* We have one, and exactly one, match. */
1140 /* It was a specialization of an ordinary member function in a
1142 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1143 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1146 /* It was a specialization of a template. */
1147 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1148 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1150 *targs_out
= copy_node (targs
);
1151 SET_TMPL_ARGS_LEVEL (*targs_out
,
1152 TMPL_ARGS_DEPTH (*targs_out
),
1153 TREE_PURPOSE (templates
));
1156 *targs_out
= TREE_PURPOSE (templates
);
1157 return TREE_VALUE (templates
);
1160 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1161 but with the default argument values filled in from those in the
1165 copy_default_args_to_explicit_spec_1 (spec_types
,
1170 tree new_spec_types
;
1175 if (spec_types
== void_list_node
)
1176 return void_list_node
;
1178 /* Substitute into the rest of the list. */
1180 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1181 TREE_CHAIN (tmpl_types
));
1183 /* Add the default argument for this parameter. */
1184 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1185 TREE_VALUE (spec_types
),
1189 /* DECL is an explicit specialization. Replicate default arguments
1190 from the template it specializes. (That way, code like:
1192 template <class T> void f(T = 3);
1193 template <> void f(double);
1196 works, as required.) An alternative approach would be to look up
1197 the correct default arguments at the call-site, but this approach
1198 is consistent with how implicit instantiations are handled. */
1201 copy_default_args_to_explicit_spec (decl
)
1207 tree new_spec_types
;
1211 tree object_type
= NULL_TREE
;
1212 tree in_charge
= NULL_TREE
;
1214 /* See if there's anything we need to do. */
1215 tmpl
= DECL_TI_TEMPLATE (decl
);
1216 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1217 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1218 if (TREE_PURPOSE (t
))
1223 old_type
= TREE_TYPE (decl
);
1224 spec_types
= TYPE_ARG_TYPES (old_type
);
1226 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1228 /* Remove the this pointer, but remember the object's type for
1230 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1231 spec_types
= TREE_CHAIN (spec_types
);
1232 tmpl_types
= TREE_CHAIN (tmpl_types
);
1234 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1236 /* DECL may contain more parameters than TMPL due to the extra
1237 in-charge parameter in constructors and destructors. */
1238 in_charge
= spec_types
;
1239 spec_types
= TREE_CHAIN (spec_types
);
1243 /* Compute the merged default arguments. */
1245 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1247 /* Compute the new FUNCTION_TYPE. */
1251 /* Put the in-charge parameter back. */
1252 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1253 TREE_VALUE (in_charge
),
1256 new_type
= build_cplus_method_type (object_type
,
1257 TREE_TYPE (old_type
),
1261 new_type
= build_function_type (TREE_TYPE (old_type
),
1263 new_type
= build_type_attribute_variant (new_type
,
1264 TYPE_ATTRIBUTES (old_type
));
1265 new_type
= build_exception_variant (new_type
,
1266 TYPE_RAISES_EXCEPTIONS (old_type
));
1267 TREE_TYPE (decl
) = new_type
;
1270 /* Check to see if the function just declared, as indicated in
1271 DECLARATOR, and in DECL, is a specialization of a function
1272 template. We may also discover that the declaration is an explicit
1273 instantiation at this point.
1275 Returns DECL, or an equivalent declaration that should be used
1276 instead if all goes well. Issues an error message if something is
1277 amiss. Returns error_mark_node if the error is not easily
1280 FLAGS is a bitmask consisting of the following flags:
1282 2: The function has a definition.
1283 4: The function is a friend.
1285 The TEMPLATE_COUNT is the number of references to qualifying
1286 template classes that appeared in the name of the function. For
1289 template <class T> struct S { void f(); };
1292 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1293 classes are not counted in the TEMPLATE_COUNT, so that in
1295 template <class T> struct S {};
1296 template <> struct S<int> { void f(); }
1297 template <> void S<int>::f();
1299 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1300 illegal; there should be no template <>.)
1302 If the function is a specialization, it is marked as such via
1303 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1304 is set up correctly, and it is added to the list of specializations
1305 for that template. */
1308 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1314 int have_def
= flags
& 2;
1315 int is_friend
= flags
& 4;
1316 int specialization
= 0;
1317 int explicit_instantiation
= 0;
1318 int member_specialization
= 0;
1319 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1320 tree dname
= DECL_NAME (decl
);
1323 tsk
= current_tmpl_spec_kind (template_count
);
1328 if (processing_specialization
)
1331 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1333 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1336 /* This could be something like:
1338 template <class T> void f(T);
1339 class S { friend void f<>(int); } */
1343 /* This case handles bogus declarations like template <>
1344 template <class T> void f<int>(); */
1346 cp_error ("template-id `%D' in declaration of primary template",
1353 case tsk_invalid_member_spec
:
1354 /* The error has already been reported in
1355 check_specialization_scope. */
1356 return error_mark_node
;
1358 case tsk_invalid_expl_inst
:
1359 cp_error ("template parameter list used in explicit instantiation");
1365 cp_error ("definition provided for explicit instantiation");
1367 explicit_instantiation
= 1;
1370 case tsk_excessive_parms
:
1371 cp_error ("too many template parameter lists in declaration of `%D'",
1373 return error_mark_node
;
1377 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1379 member_specialization
= 1;
1384 case tsk_insufficient_parms
:
1385 if (template_header_count
)
1387 cp_error("too few template parameter lists in declaration of `%D'",
1391 else if (ctype
!= NULL_TREE
1392 && !TYPE_BEING_DEFINED (ctype
)
1393 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1396 /* For backwards compatibility, we accept:
1398 template <class T> struct S { void f(); };
1399 void S<int>::f() {} // Missing template <>
1401 That used to be legal C++. */
1404 ("explicit specialization not preceded by `template <>'");
1406 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1411 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1413 /* This case handles bogus declarations like template <>
1414 template <class T> void f<int>(); */
1416 if (uses_template_parms (declarator
))
1417 cp_error ("partial specialization `%D' of function template",
1420 cp_error ("template-id `%D' in declaration of primary template",
1425 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1426 /* This is a specialization of a member template, without
1427 specialization the containing class. Something like:
1429 template <class T> struct S {
1430 template <class U> void f (U);
1432 template <> template <class U> void S<int>::f(U) {}
1434 That's a specialization -- but of the entire template. */
1439 my_friendly_abort (20000309);
1442 if (specialization
|| member_specialization
)
1444 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1445 for (; t
; t
= TREE_CHAIN (t
))
1446 if (TREE_PURPOSE (t
))
1449 ("default argument specified in explicit specialization");
1452 if (current_lang_name
== lang_name_c
)
1453 cp_error ("template specialization with C linkage");
1456 if (specialization
|| member_specialization
|| explicit_instantiation
)
1458 tree tmpl
= NULL_TREE
;
1459 tree targs
= NULL_TREE
;
1461 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1462 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1466 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1469 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1474 lookup_template_function (fns
, NULL_TREE
);
1477 if (declarator
== error_mark_node
)
1478 return error_mark_node
;
1480 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1482 if (!explicit_instantiation
)
1483 /* A specialization in class scope. This is illegal,
1484 but the error will already have been flagged by
1485 check_specialization_scope. */
1486 return error_mark_node
;
1489 /* It's not legal to write an explicit instantiation in
1492 class C { template void f(); }
1494 This case is caught by the parser. However, on
1497 template class C { void f(); };
1499 (which is illegal) we can get here. The error will be
1506 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1508 /* A friend declaration. We can't do much, because we don't
1509 know what this resolves to, yet. */
1510 my_friendly_assert (is_friend
!= 0, 0);
1511 my_friendly_assert (!explicit_instantiation
, 0);
1512 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1515 else if (ctype
!= NULL_TREE
1516 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1519 /* Find the list of functions in ctype that have the same
1520 name as the declared function. */
1521 tree name
= TREE_OPERAND (declarator
, 0);
1522 tree fns
= NULL_TREE
;
1525 if (name
== constructor_name (ctype
)
1526 || name
== constructor_name_full (ctype
))
1528 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1530 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1531 : !TYPE_HAS_DESTRUCTOR (ctype
))
1533 /* From [temp.expl.spec]:
1535 If such an explicit specialization for the member
1536 of a class template names an implicitly-declared
1537 special member function (clause _special_), the
1538 program is ill-formed.
1540 Similar language is found in [temp.explicit]. */
1541 cp_error ("specialization of implicitly-declared special member function");
1542 return error_mark_node
;
1545 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1548 if (!DECL_CONV_FN_P (decl
))
1550 idx
= lookup_fnfields_1 (ctype
, name
);
1552 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1558 /* For a type-conversion operator, we cannot do a
1559 name-based lookup. We might be looking for `operator
1560 int' which will be a specialization of `operator T'.
1561 So, we find *all* the conversion operators, and then
1562 select from them. */
1565 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1567 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1569 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1571 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1572 /* There are no more conversion functions. */
1575 /* Glue all these conversion functions together
1576 with those we already have. */
1577 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1578 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1582 if (fns
== NULL_TREE
)
1584 cp_error ("no member function `%D' declared in `%T'",
1586 return error_mark_node
;
1589 TREE_OPERAND (declarator
, 0) = fns
;
1592 /* Figure out what exactly is being specialized at this point.
1593 Note that for an explicit instantiation, even one for a
1594 member function, we cannot tell apriori whether the
1595 instantiation is for a member template, or just a member
1596 function of a template class. Even if a member template is
1597 being instantiated, the member template arguments may be
1598 elided if they can be deduced from the rest of the
1600 tmpl
= determine_specialization (declarator
, decl
,
1602 member_specialization
);
1604 if (!tmpl
|| tmpl
== error_mark_node
)
1605 /* We couldn't figure out what this declaration was
1607 return error_mark_node
;
1610 tree gen_tmpl
= most_general_template (tmpl
);
1612 if (explicit_instantiation
)
1614 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1615 is done by do_decl_instantiation later. */
1617 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1618 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1620 if (arg_depth
> parm_depth
)
1622 /* If TMPL is not the most general template (for
1623 example, if TMPL is a friend template that is
1624 injected into namespace scope), then there will
1625 be too many levels of TARGS. Remove some of them
1630 new_targs
= make_tree_vec (parm_depth
);
1631 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1632 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1633 = TREE_VEC_ELT (targs
, i
);
1637 return instantiate_template (tmpl
, targs
);
1640 /* If this is a specialization of a member template of a
1641 template class. In we want to return the TEMPLATE_DECL,
1642 not the specialization of it. */
1643 if (tsk
== tsk_template
)
1645 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1646 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
1650 /* If we thought that the DECL was a member function, but it
1651 turns out to be specializing a static member function,
1652 make DECL a static member function as well. */
1653 if (DECL_STATIC_FUNCTION_P (tmpl
)
1654 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1656 revert_static_member_fn (decl
);
1657 last_function_parms
= TREE_CHAIN (last_function_parms
);
1660 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1661 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
1663 /* Inherit default function arguments from the template
1664 DECL is specializing. */
1665 copy_default_args_to_explicit_spec (decl
);
1667 /* Mangle the function name appropriately. Note that we do
1668 not mangle specializations of non-template member
1669 functions of template classes, e.g. with
1671 template <class T> struct S { void f(); }
1673 and given the specialization
1675 template <> void S<int>::f() {}
1677 we do not mangle S<int>::f() here. That's because it's
1678 just an ordinary member function and doesn't need special
1679 treatment. We do this here so that the ordinary,
1680 non-template, name-mangling algorithm will not be used
1682 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1683 && name_mangling_version
>= 1)
1684 set_mangled_name_for_template_decl (decl
);
1686 if (is_friend
&& !have_def
)
1687 /* This is not really a declaration of a specialization.
1688 It's just the name of an instantiation. But, it's not
1689 a request for an instantiation, either. */
1690 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1691 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
1692 /* This is indeed a specialization. In case of constructors
1693 and destructors, we need in-charge and not-in-charge
1694 versions in V3 ABI. */
1695 clone_function_decl (decl
, /*update_method_vec_p=*/0);
1697 /* Register this specialization so that we can find it
1699 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1706 /* TYPE is being declared. Verify that the use of template headers
1707 and such is reasonable. Issue error messages if not. */
1710 maybe_check_template_type (type
)
1713 if (template_header_count
)
1715 /* We are in the scope of some `template <...>' header. */
1718 = template_class_depth_real (TYPE_CONTEXT (type
),
1719 /*count_specializations=*/1);
1721 if (template_header_count
<= context_depth
)
1722 /* This is OK; the template headers are for the context. We
1723 are actually too lenient here; like
1724 check_explicit_specialization we should consider the number
1725 of template types included in the actual declaration. For
1728 template <class T> struct S {
1729 template <class U> template <class V>
1735 template <class T> struct S {
1736 template <class U> struct I;
1739 template <class T> template <class U.
1744 else if (template_header_count
> context_depth
+ 1)
1745 /* There are two many template parameter lists. */
1746 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1750 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1751 parameters. These are represented in the same format used for
1752 DECL_TEMPLATE_PARMS. */
1754 int comp_template_parms (parms1
, parms2
)
1761 if (parms1
== parms2
)
1764 for (p1
= parms1
, p2
= parms2
;
1765 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1766 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1768 tree t1
= TREE_VALUE (p1
);
1769 tree t2
= TREE_VALUE (p2
);
1772 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1773 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1775 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1778 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1780 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1781 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1783 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1786 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1788 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1793 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1794 /* One set of parameters has more parameters lists than the
1801 /* Complain if DECL shadows a template parameter.
1803 [temp.local]: A template-parameter shall not be redeclared within its
1804 scope (including nested scopes). */
1807 check_template_shadow (decl
)
1812 /* If we're not in a template, we can't possibly shadow a template
1814 if (!current_template_parms
)
1817 /* Figure out what we're shadowing. */
1818 if (TREE_CODE (decl
) == OVERLOAD
)
1819 decl
= OVL_CURRENT (decl
);
1820 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1822 /* If there's no previous binding for this name, we're not shadowing
1823 anything, let alone a template parameter. */
1827 /* If we're not shadowing a template parameter, we're done. Note
1828 that OLDDECL might be an OVERLOAD (or perhaps even an
1829 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1831 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
1834 /* We check for decl != olddecl to avoid bogus errors for using a
1835 name inside a class. We check TPFI to avoid duplicate errors for
1836 inline member templates. */
1838 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1841 cp_error_at ("declaration of `%#D'", decl
);
1842 cp_error_at (" shadows template parm `%#D'", olddecl
);
1845 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1846 ORIG_LEVEL, DECL, and TYPE. */
1849 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1856 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1857 TEMPLATE_PARM_IDX (t
) = index
;
1858 TEMPLATE_PARM_LEVEL (t
) = level
;
1859 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1860 TEMPLATE_PARM_DECL (t
) = decl
;
1861 TREE_TYPE (t
) = type
;
1866 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1867 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1868 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1869 new one is created. */
1872 reduce_template_parm_level (index
, type
, levels
)
1877 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1878 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1879 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1882 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1883 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1886 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1887 TEMPLATE_PARM_LEVEL (index
) - levels
,
1888 TEMPLATE_PARM_ORIG_LEVEL (index
),
1890 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1892 DECL_ARTIFICIAL (decl
) = 1;
1893 SET_DECL_TEMPLATE_PARM_P (decl
);
1895 /* Template template parameters need this. */
1896 DECL_TEMPLATE_PARMS (decl
)
1897 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1900 return TEMPLATE_PARM_DESCENDANTS (index
);
1903 /* Process information from new template parameter NEXT and append it to the
1904 LIST being built. */
1907 process_template_parm (list
, next
)
1916 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1917 defval
= TREE_PURPOSE (parm
);
1918 parm
= TREE_VALUE (parm
);
1919 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1923 tree p
= TREE_VALUE (tree_last (list
));
1925 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
1926 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1928 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1936 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1937 /* is a const-param */
1938 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1939 PARM
, 0, NULL_TREE
);
1943 The top-level cv-qualifiers on the template-parameter are
1944 ignored when determining its type. */
1945 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1947 /* A template parameter is not modifiable. */
1948 TREE_READONLY (parm
) = 1;
1949 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
1950 TREE_TYPE (parm
) = void_type_node
;
1951 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1952 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1953 = build_template_parm_index (idx
, processing_template_decl
,
1954 processing_template_decl
,
1955 decl
, TREE_TYPE (parm
));
1960 parm
= TREE_VALUE (parm
);
1962 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1964 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
1965 /* This is for distinguishing between real templates and template
1966 template parameters */
1967 TREE_TYPE (parm
) = t
;
1968 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1973 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
1974 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1975 decl
= build_decl (TYPE_DECL
, parm
, t
);
1978 TYPE_NAME (t
) = decl
;
1979 TYPE_STUB_DECL (t
) = decl
;
1981 TEMPLATE_TYPE_PARM_INDEX (t
)
1982 = build_template_parm_index (idx
, processing_template_decl
,
1983 processing_template_decl
,
1984 decl
, TREE_TYPE (parm
));
1986 DECL_ARTIFICIAL (decl
) = 1;
1987 SET_DECL_TEMPLATE_PARM_P (decl
);
1989 parm
= build_tree_list (defval
, parm
);
1990 return chainon (list
, parm
);
1993 /* The end of a template parameter list has been reached. Process the
1994 tree list into a parameter vector, converting each parameter into a more
1995 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1999 end_template_parm_list (parms
)
2004 tree saved_parmlist
= make_tree_vec (list_length (parms
));
2006 current_template_parms
2007 = tree_cons (size_int (processing_template_decl
),
2008 saved_parmlist
, current_template_parms
);
2010 for (parm
= parms
, nparms
= 0;
2012 parm
= TREE_CHAIN (parm
), nparms
++)
2013 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
2015 --processing_template_parmlist
;
2017 return saved_parmlist
;
2020 /* end_template_decl is called after a template declaration is seen. */
2023 end_template_decl ()
2025 reset_specialization ();
2027 if (! processing_template_decl
)
2030 /* This matches the pushlevel in begin_template_parm_list. */
2033 --processing_template_decl
;
2034 current_template_parms
= TREE_CHAIN (current_template_parms
);
2037 /* Given a template argument vector containing the template PARMS.
2038 The innermost PARMS are given first. */
2041 current_template_args ()
2044 tree args
= NULL_TREE
;
2045 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
2048 /* If there is only one level of template parameters, we do not
2049 create a TREE_VEC of TREE_VECs. Instead, we return a single
2050 TREE_VEC containing the arguments. */
2052 args
= make_tree_vec (length
);
2054 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
2056 tree a
= copy_node (TREE_VALUE (header
));
2059 TREE_TYPE (a
) = NULL_TREE
;
2060 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
2062 tree t
= TREE_VEC_ELT (a
, i
);
2064 /* T will be a list if we are called from within a
2065 begin/end_template_parm_list pair, but a vector directly
2066 if within a begin/end_member_template_processing pair. */
2067 if (TREE_CODE (t
) == TREE_LIST
)
2071 if (TREE_CODE (t
) == TYPE_DECL
2072 || TREE_CODE (t
) == TEMPLATE_DECL
)
2075 t
= DECL_INITIAL (t
);
2076 TREE_VEC_ELT (a
, i
) = t
;
2081 TREE_VEC_ELT (args
, --l
) = a
;
2089 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2090 template PARMS. Used by push_template_decl below. */
2093 build_template_decl (decl
, parms
)
2097 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
2098 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
2099 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
2100 if (DECL_LANG_SPECIFIC (decl
))
2102 if (CAN_HAVE_FULL_LANG_DECL_P (decl
))
2103 DECL_VIRTUAL_CONTEXT (tmpl
) = DECL_VIRTUAL_CONTEXT (decl
);
2104 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
2105 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
2106 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2107 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
2108 if (DECL_OVERLOADED_OPERATOR_P (decl
))
2109 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
2110 DECL_OVERLOADED_OPERATOR_P (decl
));
2116 struct template_parm_data
2118 /* The level of the template parameters we are currently
2122 /* The index of the specialization argument we are currently
2126 /* An array whose size is the number of template parameters. The
2127 elements are non-zero if the parameter has been used in any one
2128 of the arguments processed so far. */
2131 /* An array whose size is the number of template arguments. The
2132 elements are non-zero if the argument makes use of template
2133 parameters of this level. */
2134 int* arg_uses_template_parms
;
2137 /* Subroutine of push_template_decl used to see if each template
2138 parameter in a partial specialization is used in the explicit
2139 argument list. If T is of the LEVEL given in DATA (which is
2140 treated as a template_parm_data*), then DATA->PARMS is marked
2144 mark_template_parm (t
, data
)
2150 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2152 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2154 level
= TEMPLATE_PARM_LEVEL (t
);
2155 idx
= TEMPLATE_PARM_IDX (t
);
2159 level
= TEMPLATE_TYPE_LEVEL (t
);
2160 idx
= TEMPLATE_TYPE_IDX (t
);
2163 if (level
== tpd
->level
)
2165 tpd
->parms
[idx
] = 1;
2166 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2169 /* Return zero so that for_each_template_parm will continue the
2170 traversal of the tree; we want to mark *every* template parm. */
2174 /* Process the partial specialization DECL. */
2177 process_partial_specialization (decl
)
2180 tree type
= TREE_TYPE (decl
);
2181 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2182 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2183 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
2184 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2185 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2186 int nargs
= TREE_VEC_LENGTH (inner_args
);
2187 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2189 int did_error_intro
= 0;
2190 struct template_parm_data tpd
;
2191 struct template_parm_data tpd2
;
2193 /* We check that each of the template parameters given in the
2194 partial specialization is used in the argument list to the
2195 specialization. For example:
2197 template <class T> struct S;
2198 template <class T> struct S<T*>;
2200 The second declaration is OK because `T*' uses the template
2201 parameter T, whereas
2203 template <class T> struct S<int>;
2205 is no good. Even trickier is:
2216 The S2<T> declaration is actually illegal; it is a
2217 full-specialization. Of course,
2220 struct S2<T (*)(U)>;
2222 or some such would have been OK. */
2223 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2224 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2225 memset ((PTR
) tpd
.parms
, 0, sizeof (int) * ntparms
);
2227 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2228 memset ((PTR
) tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
2229 for (i
= 0; i
< nargs
; ++i
)
2231 tpd
.current_arg
= i
;
2232 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2233 &mark_template_parm
,
2236 for (i
= 0; i
< ntparms
; ++i
)
2237 if (tpd
.parms
[i
] == 0)
2239 /* One of the template parms was not used in the
2241 if (!did_error_intro
)
2243 cp_error ("template parameters not used in partial specialization:");
2244 did_error_intro
= 1;
2248 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2251 /* [temp.class.spec]
2253 The argument list of the specialization shall not be identical to
2254 the implicit argument list of the primary template. */
2255 if (comp_template_args
2257 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2259 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2261 /* [temp.class.spec]
2263 A partially specialized non-type argument expression shall not
2264 involve template parameters of the partial specialization except
2265 when the argument expression is a simple identifier.
2267 The type of a template parameter corresponding to a specialized
2268 non-type argument shall not be dependent on a parameter of the
2270 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2272 for (i
= 0; i
< nargs
; ++i
)
2274 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2275 if (/* These first two lines are the `non-type' bit. */
2277 && TREE_CODE (arg
) != TEMPLATE_DECL
2278 /* This next line is the `argument expression is not just a
2279 simple identifier' condition and also the `specialized
2280 non-type argument' bit. */
2281 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2283 if (tpd
.arg_uses_template_parms
[i
])
2284 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2287 /* Look at the corresponding template parameter,
2288 marking which template parameters its type depends
2291 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2296 /* We haven't yet initialized TPD2. Do so now. */
2297 tpd2
.arg_uses_template_parms
2298 = (int*) alloca (sizeof (int) * nargs
);
2299 /* The number of parameters here is the number in the
2300 main template, which, as checked in the assertion
2302 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2304 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2307 /* Mark the template parameters. But this time, we're
2308 looking for the template parameters of the main
2309 template, not in the specialization. */
2310 tpd2
.current_arg
= i
;
2311 tpd2
.arg_uses_template_parms
[i
] = 0;
2312 memset ((PTR
) tpd2
.parms
, 0, sizeof (int) * nargs
);
2313 for_each_template_parm (type
,
2314 &mark_template_parm
,
2317 if (tpd2
.arg_uses_template_parms
[i
])
2319 /* The type depended on some template parameters.
2320 If they are fully specialized in the
2321 specialization, that's OK. */
2323 for (j
= 0; j
< nargs
; ++j
)
2324 if (tpd2
.parms
[j
] != 0
2325 && tpd
.arg_uses_template_parms
[j
])
2327 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2337 if (retrieve_specialization (maintmpl
, specargs
))
2338 /* We've already got this specialization. */
2341 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
2342 = tree_cons (inner_args
, inner_parms
,
2343 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2344 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2348 /* Check that a template declaration's use of default arguments is not
2349 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2350 non-zero if DECL is the thing declared by a primary template.
2351 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2354 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2361 int last_level_to_check
;
2366 A default template-argument shall not be specified in a
2367 function template declaration or a function template definition, nor
2368 in the template-parameter-list of the definition of a member of a
2371 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
2372 /* You can't have a function template declaration in a local
2373 scope, nor you can you define a member of a class template in a
2377 if (current_class_type
2378 && !TYPE_BEING_DEFINED (current_class_type
)
2379 && DECL_LANG_SPECIFIC (decl
)
2380 /* If this is either a friend defined in the scope of the class
2381 or a member function. */
2382 && ((DECL_CONTEXT (decl
)
2383 && same_type_p (DECL_CONTEXT (decl
), current_class_type
))
2384 || (DECL_FRIEND_CONTEXT (decl
)
2385 && same_type_p (DECL_FRIEND_CONTEXT (decl
),
2386 current_class_type
)))
2387 /* And, if it was a member function, it really was defined in
2388 the scope of the class. */
2389 && (!DECL_FUNCTION_MEMBER_P (decl
) || DECL_INITIALIZED_IN_CLASS_P (decl
)))
2390 /* We already checked these parameters when the template was
2391 declared, so there's no need to do it again now. This function
2392 was defined in class scope, but we're processing it's body now
2393 that the class is complete. */
2398 If a template-parameter has a default template-argument, all
2399 subsequent template-parameters shall have a default
2400 template-argument supplied. */
2401 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2403 tree inner_parms
= TREE_VALUE (parm_level
);
2404 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2405 int seen_def_arg_p
= 0;
2408 for (i
= 0; i
< ntparms
; ++i
)
2410 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2411 if (TREE_PURPOSE (parm
))
2413 else if (seen_def_arg_p
)
2415 cp_error ("no default argument for `%D'", TREE_VALUE (parm
));
2416 /* For better subsequent error-recovery, we indicate that
2417 there should have been a default argument. */
2418 TREE_PURPOSE (parm
) = error_mark_node
;
2423 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2424 /* For an ordinary class template, default template arguments are
2425 allowed at the innermost level, e.g.:
2426 template <class T = int>
2428 but, in a partial specialization, they're not allowed even
2429 there, as we have in [temp.class.spec]:
2431 The template parameter list of a specialization shall not
2432 contain default template argument values.
2434 So, for a partial specialization, or for a function template,
2435 we look at all of them. */
2438 /* But, for a primary class template that is not a partial
2439 specialization we look at all template parameters except the
2441 parms
= TREE_CHAIN (parms
);
2443 /* Figure out what error message to issue. */
2444 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2445 msg
= "default argument for template parameter in function template `%D'";
2446 else if (is_partial
)
2447 msg
= "default argument in partial specialization `%D'";
2449 msg
= "default argument for template parameter for class enclosing `%D'";
2451 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2452 /* If we're inside a class definition, there's no need to
2453 examine the parameters to the class itself. On the one
2454 hand, they will be checked when the class is defined, and,
2455 on the other, default arguments are legal in things like:
2456 template <class T = double>
2457 struct S { template <class U> void f(U); };
2458 Here the default argument for `S' has no bearing on the
2459 declaration of `f'. */
2460 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2462 /* Check everything. */
2463 last_level_to_check
= 0;
2465 for (parm_level
= parms
;
2466 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2467 parm_level
= TREE_CHAIN (parm_level
))
2469 tree inner_parms
= TREE_VALUE (parm_level
);
2473 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2474 for (i
= 0; i
< ntparms
; ++i
)
2475 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2479 cp_error (msg
, decl
);
2483 /* Clear out the default argument so that we are not
2485 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2488 /* At this point, if we're still interested in issuing messages,
2489 they must apply to classes surrounding the object declared. */
2491 msg
= "default argument for template parameter for class enclosing `%D'";
2495 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2496 parameters given by current_template_args, or reuses a
2497 previously existing one, if appropriate. Returns the DECL, or an
2498 equivalent one, if it is replaced via a call to duplicate_decls.
2500 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2503 push_template_decl_real (decl
, is_friend
)
2513 int new_template_p
= 0;
2515 /* See if this is a partial specialization. */
2516 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2517 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2518 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2520 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2523 /* For a friend, we want the context of the friend function, not
2524 the type of which it is a friend. */
2525 ctx
= DECL_CONTEXT (decl
);
2526 else if (CP_DECL_CONTEXT (decl
)
2527 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2528 /* In the case of a virtual function, we want the class in which
2530 ctx
= CP_DECL_CONTEXT (decl
);
2532 /* Otherwise, if we're currently defining some class, the DECL
2533 is assumed to be a member of the class. */
2534 ctx
= current_scope ();
2536 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2539 if (!DECL_CONTEXT (decl
))
2540 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2542 /* See if this is a primary template. */
2543 primary
= template_parm_scope_p ();
2547 if (current_lang_name
== lang_name_c
)
2548 cp_error ("template with C linkage");
2549 else if (TREE_CODE (decl
) == TYPE_DECL
2550 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2551 cp_error ("template class without a name");
2552 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2553 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2554 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2555 || TREE_CODE (decl
) == FUNCTION_DECL
)
2558 cp_error ("template declaration of `%#D'", decl
);
2561 /* Check to see that the rules regarding the use of default
2562 arguments are not being violated. */
2563 check_default_tmpl_args (decl
, current_template_parms
,
2564 primary
, is_partial
);
2567 return process_partial_specialization (decl
);
2569 args
= current_template_args ();
2572 || TREE_CODE (ctx
) == FUNCTION_DECL
2573 || TYPE_BEING_DEFINED (ctx
)
2574 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2576 if (DECL_LANG_SPECIFIC (decl
)
2577 && DECL_TEMPLATE_INFO (decl
)
2578 && DECL_TI_TEMPLATE (decl
))
2579 tmpl
= DECL_TI_TEMPLATE (decl
);
2580 /* If DECL is a TYPE_DECL for a class-template, then there won't
2581 be DECL_LANG_SPECIFIC. The information equivalent to
2582 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2583 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2584 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2585 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2587 /* Since a template declaration already existed for this
2588 class-type, we must be redeclaring it here. Make sure
2589 that the redeclaration is legal. */
2590 redeclare_class_template (TREE_TYPE (decl
),
2591 current_template_parms
);
2592 /* We don't need to create a new TEMPLATE_DECL; just use the
2593 one we already had. */
2594 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2598 tmpl
= build_template_decl (decl
, current_template_parms
);
2601 if (DECL_LANG_SPECIFIC (decl
)
2602 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2604 /* A specialization of a member template of a template
2606 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2607 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2608 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2614 tree a
, t
, current
, parms
;
2617 if (TREE_CODE (decl
) == TYPE_DECL
)
2619 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2620 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2621 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2622 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2623 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2626 cp_error ("`%D' does not declare a template type", decl
);
2630 else if (! DECL_TEMPLATE_INFO (decl
))
2632 cp_error ("template definition of non-template `%#D'", decl
);
2636 tmpl
= DECL_TI_TEMPLATE (decl
);
2638 if (is_member_template (tmpl
)
2639 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2640 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2641 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2645 /* The declaration is a specialization of a member
2646 template, declared outside the class. Therefore, the
2647 innermost template arguments will be NULL, so we
2648 replace them with the arguments determined by the
2649 earlier call to check_explicit_specialization. */
2650 args
= DECL_TI_ARGS (decl
);
2653 = build_template_decl (decl
, current_template_parms
);
2654 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2655 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2656 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2657 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2658 DECL_TEMPLATE_INFO (new_tmpl
)
2659 = tree_cons (tmpl
, args
, NULL_TREE
);
2661 register_specialization (new_tmpl
,
2662 most_general_template (tmpl
),
2667 /* Make sure the template headers we got make sense. */
2669 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2670 i
= TMPL_PARMS_DEPTH (parms
);
2671 if (TMPL_ARGS_DEPTH (args
) != i
)
2673 cp_error ("expected %d levels of template parms for `%#D', got %d",
2674 i
, decl
, TMPL_ARGS_DEPTH (args
));
2677 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2679 a
= TMPL_ARGS_LEVEL (args
, i
);
2680 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2682 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2684 if (current
== decl
)
2685 cp_error ("got %d template parameters for `%#D'",
2686 TREE_VEC_LENGTH (a
), decl
);
2688 cp_error ("got %d template parameters for `%#T'",
2689 TREE_VEC_LENGTH (a
), current
);
2690 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2693 /* Perhaps we should also check that the parms are used in the
2694 appropriate qualifying scopes in the declarator? */
2696 if (current
== decl
)
2699 current
= TYPE_CONTEXT (current
);
2703 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2704 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2706 /* Push template declarations for global functions and types. Note
2707 that we do not try to push a global template friend declared in a
2708 template class; such a thing may well depend on the template
2709 parameters of the class. */
2710 if (new_template_p
&& !ctx
2711 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2712 tmpl
= pushdecl_namespace_level (tmpl
);
2715 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2717 info
= tree_cons (tmpl
, args
, NULL_TREE
);
2719 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2721 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2722 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2723 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2724 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2726 else if (DECL_LANG_SPECIFIC (decl
))
2727 DECL_TEMPLATE_INFO (decl
) = info
;
2729 return DECL_TEMPLATE_RESULT (tmpl
);
2733 push_template_decl (decl
)
2736 return push_template_decl_real (decl
, 0);
2739 /* Called when a class template TYPE is redeclared with the indicated
2740 template PARMS, e.g.:
2742 template <class T> struct S;
2743 template <class T> struct S {}; */
2746 redeclare_class_template (type
, parms
)
2754 if (!TYPE_TEMPLATE_INFO (type
))
2756 cp_error ("`%T' is not a template type", type
);
2760 tmpl
= TYPE_TI_TEMPLATE (type
);
2761 if (!PRIMARY_TEMPLATE_P (tmpl
))
2762 /* The type is nested in some template class. Nothing to worry
2763 about here; there are no new template parameters for the nested
2767 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2768 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2770 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2772 cp_error_at ("previous declaration `%D'", tmpl
);
2773 cp_error ("used %d template parameter%s instead of %d",
2774 TREE_VEC_LENGTH (tmpl_parms
),
2775 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2776 TREE_VEC_LENGTH (parms
));
2780 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2782 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2783 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2784 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2785 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2787 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2789 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2790 cp_error ("redeclared here as `%#D'", parm
);
2794 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2796 /* We have in [temp.param]:
2798 A template-parameter may not be given default arguments
2799 by two different declarations in the same scope. */
2800 cp_error ("redefinition of default argument for `%#D'", parm
);
2801 cp_error_at (" original definition appeared here", tmpl_parm
);
2805 if (parm_default
!= NULL_TREE
)
2806 /* Update the previous template parameters (which are the ones
2807 that will really count) with the new default value. */
2808 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2809 else if (tmpl_default
!= NULL_TREE
)
2810 /* Update the new parameters, too; they'll be used as the
2811 parameters for any members. */
2812 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
2816 /* Attempt to convert the non-type template parameter EXPR to the
2817 indicated TYPE. If the conversion is successful, return the
2818 converted value. If the conversion is unsuccessful, return
2819 NULL_TREE if we issued an error message, or error_mark_node if we
2820 did not. We issue error messages for out-and-out bad template
2821 parameters, but not simply because the conversion failed, since we
2822 might be just trying to do argument deduction. By the time this
2823 function is called, neither TYPE nor EXPR may make use of template
2827 convert_nontype_argument (type
, expr
)
2831 tree expr_type
= TREE_TYPE (expr
);
2833 /* A template-argument for a non-type, non-template
2834 template-parameter shall be one of:
2836 --an integral constant-expression of integral or enumeration
2839 --the name of a non-type template-parameter; or
2841 --the name of an object or function with external linkage,
2842 including function templates and function template-ids but
2843 excluding non-static class members, expressed as id-expression;
2846 --the address of an object or function with external linkage,
2847 including function templates and function template-ids but
2848 excluding non-static class members, expressed as & id-expression
2849 where the & is optional if the name refers to a function or
2852 --a pointer to member expressed as described in _expr.unary.op_. */
2854 /* An integral constant-expression can include const variables or
2855 enumerators. Simplify things by folding them to their values,
2856 unless we're about to bind the declaration to a reference
2858 if (INTEGRAL_TYPE_P (expr_type
)
2859 && TREE_CODE (type
) != REFERENCE_TYPE
)
2860 expr
= decl_constant_value (expr
);
2862 if (is_overloaded_fn (expr
))
2863 /* OK for now. We'll check that it has external linkage later.
2864 Check this first since if expr_type is the unknown_type_node
2865 we would otherwise complain below. */
2867 else if (TYPE_PTRMEM_P (expr_type
)
2868 || TYPE_PTRMEMFUNC_P (expr_type
))
2870 if (TREE_CODE (expr
) != PTRMEM_CST
)
2873 else if (TYPE_PTR_P (expr_type
)
2874 || TYPE_PTRMEM_P (expr_type
)
2875 || TREE_CODE (expr_type
) == ARRAY_TYPE
2876 || TREE_CODE (type
) == REFERENCE_TYPE
2877 /* If expr is the address of an overloaded function, we
2878 will get the unknown_type_node at this point. */
2879 || expr_type
== unknown_type_node
)
2885 if (TREE_CODE (expr_type
) == ARRAY_TYPE
2886 || (TREE_CODE (type
) == REFERENCE_TYPE
2887 && TREE_CODE (e
) != ADDR_EXPR
))
2891 if (TREE_CODE (e
) != ADDR_EXPR
)
2894 cp_error ("`%E' is not a valid template argument", expr
);
2895 if (TYPE_PTR_P (expr_type
))
2897 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2898 cp_error ("it must be the address of a function with external linkage");
2900 cp_error ("it must be the address of an object with external linkage");
2902 else if (TYPE_PTRMEM_P (expr_type
)
2903 || TYPE_PTRMEMFUNC_P (expr_type
))
2904 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2909 referent
= TREE_OPERAND (e
, 0);
2910 STRIP_NOPS (referent
);
2913 if (TREE_CODE (referent
) == STRING_CST
)
2915 cp_error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
2920 if (is_overloaded_fn (referent
))
2921 /* We'll check that it has external linkage later. */
2923 else if (TREE_CODE (referent
) != VAR_DECL
)
2925 else if (!DECL_EXTERNAL_LINKAGE_P (referent
))
2927 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2928 return error_mark_node
;
2931 else if (INTEGRAL_TYPE_P (expr_type
)
2932 || TYPE_PTRMEM_P (expr_type
)
2933 || TYPE_PTRMEMFUNC_P (expr_type
)
2934 /* The next two are g++ extensions. */
2935 || TREE_CODE (expr_type
) == REAL_TYPE
2936 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2938 if (! TREE_CONSTANT (expr
))
2941 cp_error ("non-constant `%E' cannot be used as template argument",
2948 cp_error ("object `%E' cannot be used as template argument", expr
);
2952 switch (TREE_CODE (type
))
2957 /* For a non-type template-parameter of integral or enumeration
2958 type, integral promotions (_conv.prom_) and integral
2959 conversions (_conv.integral_) are applied. */
2960 if (!INTEGRAL_TYPE_P (expr_type
))
2961 return error_mark_node
;
2963 /* It's safe to call digest_init in this case; we know we're
2964 just converting one integral constant expression to another. */
2965 expr
= digest_init (type
, expr
, (tree
*) 0);
2967 if (TREE_CODE (expr
) != INTEGER_CST
)
2968 /* Curiously, some TREE_CONSTANT integral expressions do not
2969 simplify to integer constants. For example, `3 % 0',
2970 remains a TRUNC_MOD_EXPR. */
2977 /* These are g++ extensions. */
2978 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2979 return error_mark_node
;
2981 expr
= digest_init (type
, expr
, (tree
*) 0);
2983 if (TREE_CODE (expr
) != REAL_CST
)
2990 tree type_pointed_to
= TREE_TYPE (type
);
2992 if (TYPE_PTRMEM_P (type
))
2996 /* For a non-type template-parameter of type pointer to data
2997 member, qualification conversions (_conv.qual_) are
2999 e
= perform_qualification_conversions (type
, expr
);
3000 if (TREE_CODE (e
) == NOP_EXPR
)
3001 /* The call to perform_qualification_conversions will
3002 insert a NOP_EXPR over EXPR to do express conversion,
3003 if necessary. But, that will confuse us if we use
3004 this (converted) template parameter to instantiate
3005 another template; then the thing will not look like a
3006 valid template argument. So, just make a new
3007 constant, of the appropriate type. */
3008 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
3011 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
3013 /* For a non-type template-parameter of type pointer to
3014 function, only the function-to-pointer conversion
3015 (_conv.func_) is applied. If the template-argument
3016 represents a set of overloaded functions (or a pointer to
3017 such), the matching function is selected from the set
3022 if (TREE_CODE (expr
) == ADDR_EXPR
)
3023 fns
= TREE_OPERAND (expr
, 0);
3027 fn
= instantiate_type (type_pointed_to
, fns
, itf_none
);
3029 if (fn
== error_mark_node
)
3030 return error_mark_node
;
3032 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3034 if (really_overloaded_fn (fns
))
3035 return error_mark_node
;
3040 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
3042 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3048 /* For a non-type template-parameter of type pointer to
3049 object, qualification conversions (_conv.qual_) and the
3050 array-to-pointer conversion (_conv.array_) are applied.
3051 [Note: In particular, neither the null pointer conversion
3052 (_conv.ptr_) nor the derived-to-base conversion
3053 (_conv.ptr_) are applied. Although 0 is a valid
3054 template-argument for a non-type template-parameter of
3055 integral type, it is not a valid template-argument for a
3056 non-type template-parameter of pointer type.]
3058 The call to decay_conversion performs the
3059 array-to-pointer conversion, if appropriate. */
3060 expr
= decay_conversion (expr
);
3062 if (expr
== error_mark_node
)
3063 return error_mark_node
;
3065 return perform_qualification_conversions (type
, expr
);
3070 case REFERENCE_TYPE
:
3072 tree type_referred_to
= TREE_TYPE (type
);
3074 /* If this expression already has reference type, get the
3075 underling object. */
3076 if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
3078 my_friendly_assert (TREE_CODE (expr
) == ADDR_EXPR
, 20000604);
3079 expr
= TREE_OPERAND (expr
, 0);
3080 expr_type
= TREE_TYPE (expr
);
3083 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
3085 /* For a non-type template-parameter of type reference to
3086 function, no conversions apply. If the
3087 template-argument represents a set of overloaded
3088 functions, the matching function is selected from the
3089 set (_over.over_). */
3092 fn
= instantiate_type (type_referred_to
, expr
, itf_none
);
3094 if (fn
== error_mark_node
)
3095 return error_mark_node
;
3097 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3099 if (really_overloaded_fn (expr
))
3100 /* Don't issue an error here; we might get a different
3101 function if the overloading had worked out
3103 return error_mark_node
;
3108 my_friendly_assert (same_type_p (type_referred_to
,
3116 /* For a non-type template-parameter of type reference to
3117 object, no conversions apply. The type referred to by the
3118 reference may be more cv-qualified than the (otherwise
3119 identical) type of the template-argument. The
3120 template-parameter is bound directly to the
3121 template-argument, which must be an lvalue. */
3122 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type
),
3123 TYPE_MAIN_VARIANT (type_referred_to
))
3124 || !at_least_as_qualified_p (type_referred_to
,
3126 || !real_lvalue_p (expr
))
3127 return error_mark_node
;
3130 mark_addressable (expr
);
3131 return build1 (ADDR_EXPR
, type
, expr
);
3137 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 20010112);
3139 /* For a non-type template-parameter of type pointer to member
3140 function, no conversions apply. If the template-argument
3141 represents a set of overloaded member functions, the
3142 matching member function is selected from the set
3145 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3146 expr_type
!= unknown_type_node
)
3147 return error_mark_node
;
3149 if (TREE_CODE (expr
) == PTRMEM_CST
)
3151 /* A ptr-to-member constant. */
3152 if (!same_type_p (type
, expr_type
))
3153 return error_mark_node
;
3158 if (TREE_CODE (expr
) != ADDR_EXPR
)
3159 return error_mark_node
;
3161 expr
= instantiate_type (type
, expr
, itf_none
);
3163 if (expr
== error_mark_node
)
3164 return error_mark_node
;
3166 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3173 /* All non-type parameters must have one of these types. */
3174 my_friendly_abort (0);
3178 return error_mark_node
;
3181 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3182 template template parameters. Both PARM_PARMS and ARG_PARMS are
3183 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3186 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3187 the case, then extra parameters must have default arguments.
3189 Consider the example:
3190 template <class T, class Allocator = allocator> class vector;
3191 template<template <class U> class TT> class C;
3193 C<vector> is a valid instantiation. PARM_PARMS for the above code
3194 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3195 T and Allocator) and OUTER_ARGS contains the argument that is used to
3196 substitute the TT parameter. */
3199 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
3200 in_decl
, outer_args
)
3201 tree parm_parms
, arg_parms
;
3203 tree in_decl
, outer_args
;
3205 int nparms
, nargs
, i
;
3208 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3209 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3211 nparms
= TREE_VEC_LENGTH (parm_parms
);
3212 nargs
= TREE_VEC_LENGTH (arg_parms
);
3214 /* The rule here is opposite of coerce_template_parms. */
3217 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3220 for (i
= 0; i
< nparms
; ++i
)
3222 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3223 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3225 if (arg
== NULL_TREE
|| arg
== error_mark_node
3226 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3229 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3232 switch (TREE_CODE (parm
))
3238 /* We encounter instantiations of templates like
3239 template <template <template <class> class> class TT>
3242 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3243 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3245 if (!coerce_template_template_parms (parmparm
, argparm
,
3253 /* The tsubst call is used to handle cases such as
3254 template <class T, template <T> class TT> class D;
3255 i.e. the parameter list of TT depends on earlier parameters. */
3256 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3263 my_friendly_abort (0);
3269 /* Convert the indicated template ARG as necessary to match the
3270 indicated template PARM. Returns the converted ARG, or
3271 error_mark_node if the conversion was unsuccessful. Error messages
3272 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3273 parameter in the parameter list. ARGS is the full set of template
3274 arguments deduced so far. */
3277 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3287 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3289 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3291 if (TREE_CODE (arg
) == TREE_LIST
3292 && TREE_TYPE (arg
) != NULL_TREE
3293 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3295 /* The template argument was the name of some
3296 member function. That's usually
3297 illegal, but static members are OK. In any
3298 case, grab the underlying fields/functions
3299 and issue an error later if required. */
3300 arg
= TREE_VALUE (arg
);
3301 TREE_TYPE (arg
) = unknown_type_node
;
3304 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3305 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3306 || requires_tmpl_type
);
3308 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3309 we also accept implicitly created TYPE_DECL as a valid argument.
3310 This is necessary to handle the case where we pass a template name
3311 to a template template parameter in a scope where we've derived from
3312 in instantiation of that template, so the template name refers to that
3313 instantiation. We really ought to handle this better. */
3315 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3316 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3317 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3318 || (TREE_CODE (arg
) == RECORD_TYPE
3319 && CLASSTYPE_TEMPLATE_INFO (arg
)
3320 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3321 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3322 && requires_tmpl_type
3323 && is_base_of_enclosing_class (arg
, current_class_type
)));
3324 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
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 cp_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),
3341 if (is_type
!= requires_type
)
3347 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3350 cp_error (" expected a constant of type `%T', got `%T'",
3352 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3354 cp_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
)
3363 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3366 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3368 cp_error (" expected a class template, got `%T'", arg
);
3370 return error_mark_node
;
3375 if (requires_tmpl_type
)
3377 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3378 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3380 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3381 in_decl
, inner_args
))
3385 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3387 if (val
!= error_mark_node
3388 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3389 val
= TREE_TYPE (val
);
3393 if (in_decl
&& complain
)
3395 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3397 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3400 val
= error_mark_node
;
3405 val
= groktypename (arg
);
3406 if (! processing_template_decl
)
3408 /* [basic.link]: A name with no linkage (notably, the
3409 name of a class or enumeration declared in a local
3410 scope) shall not be used to declare an entity with
3411 linkage. This implies that names with no linkage
3412 cannot be used as template arguments. */
3413 tree t
= no_linkage_check (val
);
3416 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3418 ("template-argument `%T' uses anonymous type", val
);
3421 ("template-argument `%T' uses local type `%T'",
3423 return error_mark_node
;
3430 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3432 if (invalid_nontype_parm_type_p (t
, complain
))
3433 return error_mark_node
;
3435 if (processing_template_decl
)
3436 arg
= maybe_fold_nontype_arg (arg
);
3438 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3439 /* We used to call digest_init here. However, digest_init
3440 will report errors, which we don't want when complain
3441 is zero. More importantly, digest_init will try too
3442 hard to convert things: for example, `0' should not be
3443 converted to pointer type at this point according to
3444 the standard. Accepting this is not merely an
3445 extension, since deciding whether or not these
3446 conversions can occur is part of determining which
3447 function template to call, or whether a given explicit
3448 argument specification is legal. */
3449 val
= convert_nontype_argument (t
, arg
);
3453 if (val
== NULL_TREE
)
3454 val
= error_mark_node
;
3455 else if (val
== error_mark_node
&& complain
)
3456 cp_error ("could not convert template argument `%E' to `%T'",
3463 /* Convert all template arguments to their appropriate types, and
3464 return a vector containing the innermost resulting template
3465 arguments. If any error occurs, return error_mark_node, and, if
3466 COMPLAIN is non-zero, issue an error message. Some error messages
3467 are issued even if COMPLAIN is zero; for instance, if a template
3468 argument is composed from a local class.
3470 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3471 provided in ARGLIST, or else trailing parameters must have default
3472 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3473 deduction for any unspecified trailing arguments. */
3476 coerce_template_parms (parms
, args
, in_decl
,
3478 require_all_arguments
)
3482 int require_all_arguments
;
3484 int nparms
, nargs
, i
, lost
= 0;
3487 tree new_inner_args
;
3489 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3490 nargs
= NUM_TMPL_ARGS (inner_args
);
3491 nparms
= TREE_VEC_LENGTH (parms
);
3495 && require_all_arguments
3496 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3500 cp_error ("wrong number of template arguments (%d, should be %d)",
3504 cp_error_at ("provided for `%D'", in_decl
);
3507 return error_mark_node
;
3510 new_inner_args
= make_tree_vec (nparms
);
3511 new_args
= add_outermost_template_args (args
, new_inner_args
);
3512 for (i
= 0; i
< nparms
; i
++)
3517 /* Get the Ith template parameter. */
3518 parm
= TREE_VEC_ELT (parms
, i
);
3520 /* Calculate the Ith argument. */
3521 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3523 arg
= TREE_VALUE (inner_args
);
3524 inner_args
= TREE_CHAIN (inner_args
);
3527 arg
= TREE_VEC_ELT (inner_args
, i
);
3528 /* If no template argument was supplied, look for a default
3530 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3532 /* There was no default value. */
3533 my_friendly_assert (!require_all_arguments
, 0);
3536 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3537 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3539 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3542 /* Now, convert the Ith argument, as necessary. */
3543 if (arg
== NULL_TREE
)
3544 /* We're out of arguments. */
3546 my_friendly_assert (!require_all_arguments
, 0);
3549 else if (arg
== error_mark_node
)
3551 cp_error ("template argument %d is invalid", i
+ 1);
3552 arg
= error_mark_node
;
3555 arg
= convert_template_argument (TREE_VALUE (parm
),
3556 arg
, new_args
, complain
, i
,
3559 if (arg
== error_mark_node
)
3561 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3565 return error_mark_node
;
3567 return new_inner_args
;
3570 /* Returns 1 if template args OT and NT are equivalent. */
3573 template_args_equal (ot
, nt
)
3579 if (TREE_CODE (nt
) == TREE_VEC
)
3580 /* For member templates */
3581 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
3582 else if (TYPE_P (nt
))
3583 return TYPE_P (ot
) && same_type_p (ot
, nt
);
3584 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
3587 return (cp_tree_equal (ot
, nt
) > 0);
3590 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3591 of template arguments. Returns 0 otherwise. */
3594 comp_template_args (oldargs
, newargs
)
3595 tree oldargs
, newargs
;
3599 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3602 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3604 tree nt
= TREE_VEC_ELT (newargs
, i
);
3605 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3607 if (! template_args_equal (ot
, nt
))
3613 /* Given class template name and parameter list, produce a user-friendly name
3614 for the instantiation. */
3617 mangle_class_name_for_template (name
, parms
, arglist
)
3619 tree parms
, arglist
;
3621 static struct obstack scratch_obstack
;
3622 static char *scratch_firstobj
;
3625 if (!scratch_firstobj
)
3626 gcc_obstack_init (&scratch_obstack
);
3628 obstack_free (&scratch_obstack
, scratch_firstobj
);
3629 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3631 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3632 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3636 nparms
= TREE_VEC_LENGTH (parms
);
3637 arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
3638 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3639 for (i
= 0; i
< nparms
; i
++)
3641 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3642 tree arg
= TREE_VEC_ELT (arglist
, i
);
3647 if (TREE_CODE (parm
) == TYPE_DECL
)
3649 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3652 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3654 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3656 /* Already substituted with real template. Just output
3657 the template name here */
3658 tree context
= DECL_CONTEXT (arg
);
3661 /* The template may be defined in a namespace, or
3662 may be a member template. */
3663 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3664 || CLASS_TYPE_P (context
),
3666 cat(decl_as_string (DECL_CONTEXT (arg
), TFF_PLAIN_IDENTIFIER
));
3669 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3672 /* Output the parameter declaration */
3673 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3677 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3679 if (TREE_CODE (arg
) == TREE_LIST
)
3681 /* New list cell was built because old chain link was in
3683 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3684 arg
= TREE_VALUE (arg
);
3686 /* No need to check arglist against parmlist here; we did that
3687 in coerce_template_parms, called from lookup_template_class. */
3688 cat (expr_as_string (arg
, TFF_PLAIN_IDENTIFIER
));
3691 char *bufp
= obstack_next_free (&scratch_obstack
);
3693 while (bufp
[offset
- 1] == ' ')
3695 obstack_blank_fast (&scratch_obstack
, offset
);
3697 /* B<C<char> >, not B<C<char>> */
3698 if (bufp
[offset
- 1] == '>')
3703 return (char *) obstack_base (&scratch_obstack
);
3707 classtype_mangled_name (t
)
3710 if (CLASSTYPE_TEMPLATE_INFO (t
)
3711 /* Specializations have already had their names set up in
3712 lookup_template_class. */
3713 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3715 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3717 /* For non-primary templates, the template parameters are
3718 implicit from their surrounding context. */
3719 if (PRIMARY_TEMPLATE_P (tmpl
))
3721 tree name
= DECL_NAME (tmpl
);
3722 char *mangled_name
= mangle_class_name_for_template
3723 (IDENTIFIER_POINTER (name
),
3724 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3725 CLASSTYPE_TI_ARGS (t
));
3726 tree id
= get_identifier (mangled_name
);
3727 IDENTIFIER_TEMPLATE (id
) = name
;
3732 return TYPE_IDENTIFIER (t
);
3736 add_pending_template (d
)
3739 tree ti
= (TYPE_P (d
)
3740 ? CLASSTYPE_TEMPLATE_INFO (d
)
3741 : DECL_TEMPLATE_INFO (d
));
3744 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3747 /* We are called both from instantiate_decl, where we've already had a
3748 tinst_level pushed, and instantiate_template, where we haven't.
3750 level
= !(current_tinst_level
&& TINST_DECL (current_tinst_level
) == d
);
3753 push_tinst_level (d
);
3755 *template_tail
= tree_cons (current_tinst_level
, d
, NULL_TREE
);
3756 template_tail
= &TREE_CHAIN (*template_tail
);
3757 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3764 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3765 may be either a _DECL or an overloaded function or an
3766 IDENTIFIER_NODE), and ARGLIST. */
3769 lookup_template_function (fns
, arglist
)
3774 if (fns
== NULL_TREE
)
3776 cp_error ("non-template used as template");
3777 return error_mark_node
;
3780 type
= TREE_TYPE (fns
);
3781 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3782 type
= unknown_type_node
;
3784 if (processing_template_decl
)
3785 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3787 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3790 /* Within the scope of a template class S<T>, the name S gets bound
3791 (in build_self_reference) to a TYPE_DECL for the class, not a
3792 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3793 or one of its enclosing classes, and that type is a template,
3794 return the associated TEMPLATE_DECL. Otherwise, the original
3795 DECL is returned. */
3798 maybe_get_template_decl_from_type_decl (decl
)
3801 return (decl
!= NULL_TREE
3802 && TREE_CODE (decl
) == TYPE_DECL
3803 && DECL_ARTIFICIAL (decl
)
3804 && CLASS_TYPE_P (TREE_TYPE (decl
))
3805 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3806 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3809 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3810 parameters, find the desired type.
3812 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3813 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3814 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3817 IN_DECL, if non-NULL, is the template declaration we are trying to
3820 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3821 the class we are looking up.
3823 If COMPLAIN is non-zero, issue error messages.
3825 If the template class is really a local class in a template
3826 function, then the FUNCTION_CONTEXT is the function in which it is
3827 being instantiated. */
3830 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
, complain
)
3837 tree
template = NULL_TREE
, parmlist
;
3840 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3842 if (IDENTIFIER_VALUE (d1
)
3843 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3844 template = IDENTIFIER_VALUE (d1
);
3848 push_decl_namespace (context
);
3849 template = lookup_name (d1
, /*prefer_type=*/0);
3850 template = maybe_get_template_decl_from_type_decl (template);
3852 pop_decl_namespace ();
3855 context
= DECL_CONTEXT (template);
3857 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3859 tree type
= TREE_TYPE (d1
);
3861 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3862 an implicit typename for the second A. Deal with it. */
3863 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
3864 type
= TREE_TYPE (type
);
3866 if (CLASSTYPE_TEMPLATE_INFO (type
))
3868 template = CLASSTYPE_TI_TEMPLATE (type
);
3869 d1
= DECL_NAME (template);
3872 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3873 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
3875 template = TYPE_TI_TEMPLATE (d1
);
3876 d1
= DECL_NAME (template);
3878 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3879 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
3882 d1
= DECL_NAME (template);
3883 context
= DECL_CONTEXT (template);
3886 /* With something like `template <class T> class X class X { ... };'
3887 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3888 We don't want to do that, but we have to deal with the situation,
3889 so let's give them some syntax errors to chew on instead of a
3890 crash. Alternatively D1 might not be a template type at all. */
3894 cp_error ("`%T' is not a template", d1
);
3895 return error_mark_node
;
3898 if (TREE_CODE (template) != TEMPLATE_DECL
)
3902 cp_error ("non-template type `%T' used as a template", d1
);
3904 cp_error_at ("for template declaration `%D'", in_decl
);
3906 return error_mark_node
;
3909 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3911 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3912 template arguments */
3917 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3919 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
3920 complain
, /*require_all_args=*/1);
3921 if (arglist2
== error_mark_node
)
3922 return error_mark_node
;
3924 parm
= copy_template_template_parm (TREE_TYPE (template), arglist2
);
3925 TYPE_SIZE (parm
) = 0;
3930 tree template_type
= TREE_TYPE (template);
3933 tree found
= NULL_TREE
;
3937 int is_partial_instantiation
;
3939 gen_tmpl
= most_general_template (template);
3940 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
3941 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3942 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3944 if (arg_depth
== 1 && parm_depth
> 1)
3946 /* We've been given an incomplete set of template arguments.
3949 template <class T> struct S1 {
3950 template <class U> struct S2 {};
3951 template <class U> struct S2<U*> {};
3954 we will be called with an ARGLIST of `U*', but the
3955 TEMPLATE will be `template <class T> template
3956 <class U> struct S1<T>::S2'. We must fill in the missing
3959 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3961 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3964 /* Now we should enough arguments. */
3965 my_friendly_assert (parm_depth
== arg_depth
, 0);
3967 /* From here on, we're only interested in the most general
3969 template = gen_tmpl
;
3971 /* Calculate the BOUND_ARGS. These will be the args that are
3972 actually tsubst'd into the definition to create the
3976 /* We have multiple levels of arguments to coerce, at once. */
3978 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3980 tree bound_args
= make_tree_vec (parm_depth
);
3982 for (i
= saved_depth
,
3983 t
= DECL_TEMPLATE_PARMS (template);
3984 i
> 0 && t
!= NULL_TREE
;
3985 --i
, t
= TREE_CHAIN (t
))
3987 tree a
= coerce_template_parms (TREE_VALUE (t
),
3989 complain
, /*require_all_args=*/1);
3990 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3992 /* We temporarily reduce the length of the ARGLIST so
3993 that coerce_template_parms will see only the arguments
3994 corresponding to the template parameters it is
3996 TREE_VEC_LENGTH (arglist
)--;
3999 /* Restore the ARGLIST to its full size. */
4000 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4002 arglist
= bound_args
;
4006 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
4007 INNERMOST_TEMPLATE_ARGS (arglist
),
4009 complain
, /*require_all_args=*/1);
4011 if (arglist
== error_mark_node
)
4012 /* We were unable to bind the arguments. */
4013 return error_mark_node
;
4015 /* In the scope of a template class, explicit references to the
4016 template class refer to the type of the template, not any
4017 instantiation of it. For example, in:
4019 template <class T> class C { void f(C<T>); }
4021 the `C<T>' is just the same as `C'. Outside of the
4022 class, however, such a reference is an instantiation. */
4023 if (comp_template_args (TYPE_TI_ARGS (template_type
),
4026 found
= template_type
;
4028 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
4032 /* Note that we use DECL_CONTEXT, rather than
4033 CP_DECL_CONTEXT, so that the termination test is
4034 always just `ctx'. We're not interested in namespace
4036 for (ctx
= current_class_type
;
4038 ctx
= (TYPE_P (ctx
)) ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
4039 if (same_type_p (ctx
, template_type
))
4043 /* We're not in the scope of the class, so the
4044 TEMPLATE_TYPE is not the type we want after
4052 for (tp
= &DECL_TEMPLATE_INSTANTIATIONS (template);
4054 tp
= &TREE_CHAIN (*tp
))
4055 if (comp_template_args (TREE_PURPOSE (*tp
), arglist
))
4059 /* Use the move-to-front heuristic to speed up future
4061 *tp
= TREE_CHAIN (*tp
);
4063 = DECL_TEMPLATE_INSTANTIATIONS (template);
4064 DECL_TEMPLATE_INSTANTIATIONS (template) = found
;
4066 return TREE_VALUE (found
);
4069 /* This type is a "partial instantiation" if any of the template
4070 arguments still involve template parameters. Note that we set
4071 IS_PARTIAL_INSTANTIATION for partial specializations as
4073 is_partial_instantiation
= uses_template_parms (arglist
);
4075 if (!is_partial_instantiation
4076 && !PRIMARY_TEMPLATE_P (template)
4077 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
4079 found
= xref_tag_from_type (TREE_TYPE (template),
4080 DECL_NAME (template),
4085 context
= tsubst (DECL_CONTEXT (template), arglist
,
4086 /*complain=*/0, in_decl
);
4088 context
= global_namespace
;
4090 /* Create the type. */
4091 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
4093 if (!is_partial_instantiation
)
4095 set_current_access_from_decl (TYPE_NAME (template_type
));
4096 t
= start_enum (TYPE_IDENTIFIER (template_type
));
4099 /* We don't want to call start_enum for this type, since
4100 the values for the enumeration constants may involve
4101 template parameters. And, no one should be interested
4102 in the enumeration constants for such a type. */
4103 t
= make_node (ENUMERAL_TYPE
);
4107 t
= make_aggr_type (TREE_CODE (template_type
));
4108 CLASSTYPE_DECLARED_CLASS (t
)
4109 = CLASSTYPE_DECLARED_CLASS (template_type
);
4110 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
4111 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
4112 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
4114 /* A local class. Make sure the decl gets registered properly. */
4115 if (context
== current_function_decl
)
4116 pushtag (DECL_NAME (template), t
, 0);
4119 /* If we called start_enum or pushtag above, this information
4120 will already be set up. */
4123 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4125 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
4126 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
4127 TYPE_STUB_DECL (t
) = type_decl
;
4128 DECL_SOURCE_FILE (type_decl
)
4129 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
4130 DECL_SOURCE_LINE (type_decl
)
4131 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
4134 type_decl
= TYPE_NAME (t
);
4136 /* Set up the template information. We have to figure out which
4137 template is the immediate parent if this is a full
4139 if (parm_depth
== 1 || is_partial_instantiation
4140 || !PRIMARY_TEMPLATE_P (template))
4141 /* This case is easy; there are no member templates involved. */
4145 /* This is a full instantiation of a member template. Look
4146 for a partial instantiation of which this is an instance. */
4148 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4149 found
; found
= TREE_CHAIN (found
))
4152 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4154 /* We only want partial instantiations, here, not
4155 specializations or full instantiations. */
4156 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4157 || !uses_template_parms (TREE_VALUE (found
)))
4160 /* Temporarily reduce by one the number of levels in the
4161 ARGLIST and in FOUND so as to avoid comparing the
4162 last set of arguments. */
4163 TREE_VEC_LENGTH (arglist
)--;
4164 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4166 /* See if the arguments match. If they do, then TMPL is
4167 the partial instantiation we want. */
4168 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4170 /* Restore the argument vectors to their full size. */
4171 TREE_VEC_LENGTH (arglist
)++;
4172 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4183 /* There was no partial instantiation. This happens
4184 where C<T> is a member template of A<T> and it's used
4187 template <typename T> struct B { A<T>::C<int> m; };
4190 Create the partial instantiation.
4192 TREE_VEC_LENGTH (arglist
)--;
4193 template = tsubst (template, arglist
, /*complain=*/0, NULL_TREE
);
4194 TREE_VEC_LENGTH (arglist
)++;
4199 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
4200 DECL_TEMPLATE_INSTANTIATIONS (template)
4201 = tree_cons (arglist
, t
,
4202 DECL_TEMPLATE_INSTANTIATIONS (template));
4204 if (TREE_CODE (t
) == ENUMERAL_TYPE
4205 && !is_partial_instantiation
)
4206 /* Now that the type has been registered on the instantiations
4207 list, we set up the enumerators. Because the enumeration
4208 constants may involve the enumeration type itself, we make
4209 sure to register the type first, and then create the
4210 constants. That way, doing tsubst_expr for the enumeration
4211 constants won't result in recursive calls here; we'll find
4212 the instantiation and exit above. */
4213 tsubst_enum (template_type
, t
, arglist
);
4215 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4217 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4218 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4219 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
4220 if (!is_partial_instantiation
)
4223 DECL_ASSEMBLER_NAME (type_decl
) = mangle_decl (type_decl
);
4225 DECL_ASSEMBLER_NAME (type_decl
)
4226 = get_identifier (build_overload_name (t
, 1, 1));
4228 /* For backwards compatibility; code that uses
4229 -fexternal-templates expects looking up a template to
4230 instantiate it. I think DDD still relies on this.
4231 (jason 8/20/1998) */
4232 if (TREE_CODE (t
) != ENUMERAL_TYPE
4233 && flag_external_templates
4234 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4235 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4236 add_pending_template (t
);
4239 /* If the type makes use of template parameters, the
4240 code that generates debugging information will crash. */
4241 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4253 /* Called from for_each_template_parm via walk_tree. */
4256 for_each_template_parm_r (tp
, walk_subtrees
, d
)
4262 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
4263 tree_fn_t fn
= pfd
->fn
;
4264 void *data
= pfd
->data
;
4267 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
4268 return error_mark_node
;
4270 switch (TREE_CODE (t
))
4273 if (TYPE_PTRMEMFUNC_P (t
))
4279 if (!TYPE_TEMPLATE_INFO (t
))
4281 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
4283 return error_mark_node
;
4287 /* Since we're not going to walk subtrees, we have to do this
4289 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4290 return error_mark_node
;
4294 /* Check the return type. */
4295 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4296 return error_mark_node
;
4298 /* Check the parameter types. Since default arguments are not
4299 instantiated until they are needed, the TYPE_ARG_TYPES may
4300 contain expressions that involve template parameters. But,
4301 no-one should be looking at them yet. And, once they're
4302 instantiated, they don't contain template parameters, so
4303 there's no point in looking at them then, either. */
4307 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4308 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4309 return error_mark_node
;
4311 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4312 want walk_tree walking into them itself. */
4319 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4320 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4321 return error_mark_node
;
4326 if (DECL_CONTEXT (t
)
4327 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4328 return error_mark_node
;
4331 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4332 /* Record template parameters such as `T' inside `TT<T>'. */
4333 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4334 return error_mark_node
;
4337 case TEMPLATE_TEMPLATE_PARM
:
4338 case TEMPLATE_TYPE_PARM
:
4339 case TEMPLATE_PARM_INDEX
:
4340 if (fn
&& (*fn
)(t
, data
))
4341 return error_mark_node
;
4343 return error_mark_node
;
4347 /* A template template parameter is encountered */
4348 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
4349 && for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4350 return error_mark_node
;
4352 /* Already substituted template template parameter */
4357 if (!fn
|| for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
, data
))
4358 return error_mark_node
;
4362 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
4363 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4364 (TREE_TYPE (t
)), fn
, data
))
4365 return error_mark_node
;
4370 /* If there's no type, then this thing must be some expression
4371 involving template parameters. */
4372 if (!fn
&& !TREE_TYPE (t
))
4373 return error_mark_node
;
4378 case REINTERPRET_CAST_EXPR
:
4379 case CONST_CAST_EXPR
:
4380 case STATIC_CAST_EXPR
:
4381 case DYNAMIC_CAST_EXPR
:
4386 case PSEUDO_DTOR_EXPR
:
4388 return error_mark_node
;
4395 /* We didn't find any template parameters we liked. */
4399 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4400 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4401 call FN with the parameter and the DATA.
4402 If FN returns non-zero, the iteration is terminated, and
4403 for_each_template_parm returns 1. Otherwise, the iteration
4404 continues. If FN never returns a non-zero value, the value
4405 returned by for_each_template_parm is 0. If FN is NULL, it is
4406 considered to be the function which always returns 1. */
4409 for_each_template_parm (t
, fn
, data
)
4414 struct pair_fn_data pfd
;
4420 /* Walk the tree. (Conceptually, we would like to walk without
4421 duplicates, but for_each_template_parm_r recursively calls
4422 for_each_template_parm, so we would need to reorganize a fair
4423 bit to use walk_tree_without_duplicates.) */
4424 return walk_tree (&t
,
4425 for_each_template_parm_r
,
4431 uses_template_parms (t
)
4434 return for_each_template_parm (t
, 0, 0);
4437 static int tinst_depth
;
4438 extern int max_tinst_depth
;
4439 #ifdef GATHER_STATISTICS
4442 static int tinst_level_tick
;
4443 static int last_template_error_tick
;
4445 /* We're starting to instantiate D; record the template instantiation context
4446 for diagnostics and to restore it later. */
4449 push_tinst_level (d
)
4454 if (tinst_depth
>= max_tinst_depth
)
4456 /* If the instantiation in question still has unbound template parms,
4457 we don't really care if we can't instantiate it, so just return.
4458 This happens with base instantiation for implicit `typename'. */
4459 if (uses_template_parms (d
))
4462 last_template_error_tick
= tinst_level_tick
;
4463 cp_error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4464 max_tinst_depth
, d
);
4466 print_instantiation_context ();
4471 new = build_expr_wfl (d
, input_filename
, lineno
, 0);
4472 TREE_CHAIN (new) = current_tinst_level
;
4473 current_tinst_level
= new;
4476 #ifdef GATHER_STATISTICS
4477 if (tinst_depth
> depth_reached
)
4478 depth_reached
= tinst_depth
;
4485 /* We're done instantiating this template; return to the instantiation
4491 tree old
= current_tinst_level
;
4493 /* Restore the filename and line number stashed away when we started
4494 this instantiation. */
4495 lineno
= TINST_LINE (old
);
4496 input_filename
= TINST_FILE (old
);
4497 extract_interface_info ();
4499 current_tinst_level
= TREE_CHAIN (old
);
4504 /* We're instantiating a deferred template; restore the template
4505 instantiation context in which the instantiation was requested, which
4506 is one step out from LEVEL. */
4509 reopen_tinst_level (level
)
4515 for (t
= level
; t
; t
= TREE_CHAIN (t
))
4518 current_tinst_level
= level
;
4522 /* Return the outermost template instantiation context, for use with
4523 -falt-external-templates. */
4528 tree p
= current_tinst_level
;
4531 for (; TREE_CHAIN (p
) ; p
= TREE_CHAIN (p
))
4536 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4537 vector of template arguments, as for tsubst.
4539 Returns an appropriate tsubst'd friend declaration. */
4542 tsubst_friend_function (decl
, args
)
4548 const char *file
= input_filename
;
4550 lineno
= DECL_SOURCE_LINE (decl
);
4551 input_filename
= DECL_SOURCE_FILE (decl
);
4553 if (TREE_CODE (decl
) == FUNCTION_DECL
4554 && DECL_TEMPLATE_INSTANTIATION (decl
)
4555 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4556 /* This was a friend declared with an explicit template
4557 argument list, e.g.:
4561 to indicate that f was a template instantiation, not a new
4562 function declaration. Now, we have to figure out what
4563 instantiation of what template. */
4565 tree template_id
, arglist
, fns
;
4568 tree ns
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
4570 /* Friend functions are looked up in the containing namespace scope.
4571 We must enter that scope, to avoid finding member functions of the
4572 current cless with same name. */
4573 push_nested_namespace (ns
);
4574 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
4575 /*complain=*/1, NULL_TREE
);
4576 pop_nested_namespace (ns
);
4577 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
4578 /*complain=*/1, NULL_TREE
);
4579 template_id
= lookup_template_function (fns
, arglist
);
4581 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4582 tmpl
= determine_specialization (template_id
, new_friend
,
4584 /*need_member_template=*/0);
4585 new_friend
= instantiate_template (tmpl
, new_args
);
4589 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4591 /* The NEW_FRIEND will look like an instantiation, to the
4592 compiler, but is not an instantiation from the point of view of
4593 the language. For example, we might have had:
4595 template <class T> struct S {
4596 template <class U> friend void f(T, U);
4599 Then, in S<int>, template <class U> void f(int, U) is not an
4600 instantiation of anything. */
4601 DECL_USE_TEMPLATE (new_friend
) = 0;
4602 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4604 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4605 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
4606 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
4609 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4610 tsubst will have resulted in a call to
4611 set_mangled_name_for_template_decl. But, the function is not a
4612 template instantiation and should not be mangled like one.
4613 Therefore, we remangle the function name. We don't have to do
4614 this if the NEW_FRIEND is a template since
4615 set_mangled_name_for_template_decl doesn't do anything if the
4616 function declaration still uses template arguments. */
4617 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4619 set_mangled_name_for_decl (new_friend
);
4620 DECL_RTL (new_friend
) = 0;
4621 make_decl_rtl (new_friend
, NULL_PTR
);
4624 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4627 tree new_friend_template_info
;
4628 tree new_friend_result_template_info
;
4630 int new_friend_is_defn
;
4632 /* We must save some information from NEW_FRIEND before calling
4633 duplicate decls since that function will free NEW_FRIEND if
4635 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4636 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4638 /* This declaration is a `primary' template. */
4639 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4642 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend
)) != NULL_TREE
;
4643 new_friend_result_template_info
4644 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
4648 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4649 new_friend_result_template_info
= NULL_TREE
;
4652 /* Inside pushdecl_namespace_level, we will push into the
4653 current namespace. However, the friend function should go
4654 into the namespace of the template. */
4655 ns
= decl_namespace_context (new_friend
);
4656 push_nested_namespace (ns
);
4657 old_decl
= pushdecl_namespace_level (new_friend
);
4658 pop_nested_namespace (ns
);
4660 if (old_decl
!= new_friend
)
4662 /* This new friend declaration matched an existing
4663 declaration. For example, given:
4665 template <class T> void f(T);
4666 template <class U> class C {
4667 template <class T> friend void f(T) {}
4670 the friend declaration actually provides the definition
4671 of `f', once C has been instantiated for some type. So,
4672 old_decl will be the out-of-class template declaration,
4673 while new_friend is the in-class definition.
4675 But, if `f' was called before this point, the
4676 instantiation of `f' will have DECL_TI_ARGS corresponding
4677 to `T' but not to `U', references to which might appear
4678 in the definition of `f'. Previously, the most general
4679 template for an instantiation of `f' was the out-of-class
4680 version; now it is the in-class version. Therefore, we
4681 run through all specialization of `f', adding to their
4682 DECL_TI_ARGS appropriately. In particular, they need a
4683 new set of outer arguments, corresponding to the
4684 arguments for this class instantiation.
4686 The same situation can arise with something like this:
4689 template <class T> class C {
4693 when `C<int>' is instantiated. Now, `f(int)' is defined
4696 if (!new_friend_is_defn
)
4697 /* On the other hand, if the in-class declaration does
4698 *not* provide a definition, then we don't want to alter
4699 existing definitions. We can just leave everything
4704 /* Overwrite whatever template info was there before, if
4705 any, with the new template information pertaining to
4707 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4709 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4710 /* duplicate_decls will take care of this case. */
4715 tree new_friend_args
;
4717 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
4718 = new_friend_result_template_info
;
4720 new_friend_args
= TI_ARGS (new_friend_template_info
);
4721 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4725 tree spec
= TREE_VALUE (t
);
4728 = add_outermost_template_args (new_friend_args
,
4729 DECL_TI_ARGS (spec
));
4732 /* Now, since specializations are always supposed to
4733 hang off of the most general template, we must move
4735 t
= most_general_template (old_decl
);
4738 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4739 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4740 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4741 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4746 /* The information from NEW_FRIEND has been merged into OLD_DECL
4747 by duplicate_decls. */
4748 new_friend
= old_decl
;
4751 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend
)))
4753 /* Check to see that the declaration is really present, and,
4754 possibly obtain an improved declaration. */
4755 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4764 input_filename
= file
;
4768 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4769 template arguments, as for tsubst.
4771 Returns an appropriate tsubst'd friend type or error_mark_node on
4775 tsubst_friend_class (friend_tmpl
, args
)
4782 /* First, we look for a class template. */
4783 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
4785 /* But, if we don't find one, it might be because we're in a
4786 situation like this:
4794 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4795 for `S<int>', not the TEMPLATE_DECL. */
4796 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
4798 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
4799 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4802 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4804 /* The friend template has already been declared. Just
4805 check to see that the declarations match, and install any new
4806 default parameters. We must tsubst the default parameters,
4807 of course. We only need the innermost template parameters
4808 because that is all that redeclare_class_template will look
4811 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4812 args
, /*complain=*/1);
4814 return error_mark_node
;
4815 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4816 friend_type
= TREE_TYPE (tmpl
);
4820 /* The friend template has not already been declared. In this
4821 case, the instantiation of the template class will cause the
4822 injection of this template into the global scope. */
4823 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4825 /* The new TMPL is not an instantiation of anything, so we
4826 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4827 the new type because that is supposed to be the corresponding
4828 template decl, i.e., TMPL. */
4829 DECL_USE_TEMPLATE (tmpl
) = 0;
4830 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4831 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4833 /* Inject this template into the global scope. */
4834 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4841 instantiate_class_template (type
)
4844 tree
template, args
, pattern
, t
;
4847 if (type
== error_mark_node
)
4848 return error_mark_node
;
4850 if (TYPE_BEING_DEFINED (type
) || COMPLETE_TYPE_P (type
))
4853 /* Figure out which template is being instantiated. */
4854 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4855 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4857 /* Figure out which arguments are being used to do the
4859 args
= CLASSTYPE_TI_ARGS (type
);
4860 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4862 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4863 /* If this is a partial instantiation, then we can't instantiate
4864 the type; there's no telling whether or not one of the
4865 template parameters might eventually be instantiated to some
4866 value that results in a specialization being used. For
4878 Now, the `S<U>' in `f<int>' is the specialization, not an
4879 instantiation of the original template. */
4882 /* Determine what specialization of the original template to
4884 if (PARTIAL_INSTANTIATION_P (type
))
4885 /* There's no telling which specialization is appropriate at this
4886 point. Since all peeking at the innards of this partial
4887 instantiation are extensions (like the "implicit typename"
4888 extension, which allows users to omit the keyword `typename' on
4889 names that are declared as types in template base classes), we
4890 are free to do what we please.
4892 Trying to figure out which partial instantiation to use can
4893 cause a crash. (Some of the template arguments don't even have
4894 types.) So, we just use the most general version. */
4898 t
= most_specialized_class (template, args
);
4900 if (t
== error_mark_node
)
4902 const char *str
= "candidates are:";
4903 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4904 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4907 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4910 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4914 TYPE_BEING_DEFINED (type
) = 1;
4915 return error_mark_node
;
4920 pattern
= TREE_TYPE (t
);
4922 pattern
= TREE_TYPE (template);
4924 /* If the template we're instantiating is incomplete, then clearly
4925 there's nothing we can do. */
4926 if (!COMPLETE_TYPE_P (pattern
))
4929 /* If this is a partial instantiation, don't tsubst anything. We will
4930 only use this type for implicit typename, so the actual contents don't
4931 matter. All that matters is whether a particular name is a type. */
4932 if (PARTIAL_INSTANTIATION_P (type
))
4934 /* The fields set here must be kept in sync with those cleared
4935 in begin_class_definition. */
4936 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4937 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4938 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4939 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4940 CLASSTYPE_VBASECLASSES (type
) = CLASSTYPE_VBASECLASSES (pattern
);
4942 /* Pretend that the type is complete, so that we will look
4943 inside it during name lookup and such. */
4944 TYPE_SIZE (type
) = bitsize_zero_node
;
4948 /* If we've recursively instantiated too many templates, stop. */
4949 if (! push_tinst_level (type
))
4952 /* Now we're really doing the instantiation. Mark the type as in
4953 the process of being defined. */
4954 TYPE_BEING_DEFINED (type
) = 1;
4956 maybe_push_to_top_level (uses_template_parms (type
));
4960 /* This TYPE is actually a instantiation of of a partial
4961 specialization. We replace the innermost set of ARGS with
4962 the arguments appropriate for substitution. For example,
4965 template <class T> struct S {};
4966 template <class T> struct S<T*> {};
4968 and supposing that we are instantiating S<int*>, ARGS will
4969 present be {int*} but we need {int}. */
4971 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4974 /* If there were multiple levels in ARGS, replacing the
4975 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4976 want, so we make a copy first. */
4977 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4979 args
= copy_node (args
);
4980 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4986 if (flag_external_templates
)
4988 if (flag_alt_external_templates
)
4990 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4991 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4992 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4993 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4994 && CLASSTYPE_INTERFACE_KNOWN (type
));
4998 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4999 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5000 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
5001 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
5002 = (! CLASSTYPE_INTERFACE_ONLY (type
)
5003 && CLASSTYPE_INTERFACE_KNOWN (type
));
5008 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
5009 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
5012 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
5013 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
5014 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
5015 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
5016 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
5017 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
5018 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
5019 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
5020 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
5021 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
5022 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
5023 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
5024 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5025 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5026 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5027 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5028 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
5029 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern
);
5030 TYPE_USES_MULTIPLE_INHERITANCE (type
)
5031 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
5032 TYPE_USES_VIRTUAL_BASECLASSES (type
)
5033 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
5034 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5035 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5036 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
5037 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5038 if (ANON_AGGR_TYPE_P (pattern
))
5039 SET_ANON_AGGR_TYPE_P (type
);
5041 if (TYPE_BINFO_BASETYPES (pattern
))
5043 tree base_list
= NULL_TREE
;
5044 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
5047 /* Substitute into each of the bases to determine the actual
5049 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
5055 pbase
= TREE_VEC_ELT (pbases
, i
);
5057 /* Substitute to figure out the base class. */
5058 base
= tsubst (BINFO_TYPE (pbase
), args
,
5059 /*complain=*/1, NULL_TREE
);
5060 if (base
== error_mark_node
)
5063 /* Calculate the correct access node. */
5064 if (TREE_VIA_VIRTUAL (pbase
))
5066 if (TREE_VIA_PUBLIC (pbase
))
5067 access
= access_public_virtual_node
;
5068 else if (TREE_VIA_PROTECTED (pbase
))
5069 access
= access_protected_virtual_node
;
5071 access
= access_private_virtual_node
;
5075 if (TREE_VIA_PUBLIC (pbase
))
5076 access
= access_public_node
;
5077 else if (TREE_VIA_PROTECTED (pbase
))
5078 access
= access_protected_node
;
5080 access
= access_private_node
;
5083 base_list
= tree_cons (access
, base
, base_list
);
5086 /* The list is now in reverse order; correct that. */
5087 base_list
= nreverse (base_list
);
5089 /* Now call xref_basetypes to set up all the base-class
5091 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
5092 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
5093 ? class_type_node
: record_type_node
)
5095 DECL_NAME (TYPE_NAME (pattern
)),
5100 /* Now that our base classes are set up, enter the scope of the
5101 class, so that name lookups into base classes, etc. will work
5102 correctly. This is precisely analogous to what we do in
5103 begin_class_definition when defining an ordinary non-template
5105 pushclass (type
, 1);
5107 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
5109 tree tag
= TREE_VALUE (t
);
5110 tree name
= TYPE_IDENTIFIER (tag
);
5113 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
5114 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5116 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
5117 /* Unfortunately, lookup_template_class sets
5118 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5119 instantiation (i.e., for the type of a member template
5120 class nested within a template class.) This behavior is
5121 required for maybe_process_partial_specialization to work
5122 correctly, but is not accurate in this case; the TAG is not
5123 an instantiation of anything. (The corresponding
5124 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5125 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5127 /* Now, we call pushtag to put this NEWTAG into the scope of
5128 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5129 pushtag calling push_template_decl. We don't have to do
5130 this for enums because it will already have been done in
5133 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5134 pushtag (name
, newtag
, /*globalize=*/0);
5138 /* Don't replace enum constants here. */
5139 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
5140 if (TREE_CODE (t
) != CONST_DECL
)
5144 /* The the file and line for this declaration, to assist in
5145 error message reporting. Since we called push_tinst_level
5146 above, we don't need to restore these. */
5147 lineno
= DECL_SOURCE_LINE (t
);
5148 input_filename
= DECL_SOURCE_FILE (t
);
5150 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5151 if (TREE_CODE (r
) == VAR_DECL
)
5155 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5156 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5157 /*complain=*/1, NULL_TREE
);
5161 finish_static_data_member_decl (r
, init
,
5162 /*asmspec_tree=*/NULL_TREE
,
5165 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5166 check_static_variable_definition (r
, TREE_TYPE (r
));
5169 /* R will have a TREE_CHAIN if and only if it has already been
5170 processed by finish_member_declaration. This can happen
5171 if, for example, it is a TYPE_DECL for a class-scoped
5172 ENUMERAL_TYPE; such a thing will already have been added to
5173 the field list by tsubst_enum above. */
5174 if (!TREE_CHAIN (r
))
5176 set_current_access_from_decl (r
);
5177 finish_member_declaration (r
);
5181 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5182 for this instantiation. */
5183 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
5185 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5186 set_current_access_from_decl (r
);
5187 finish_member_declaration (r
);
5190 /* Construct the DECL_FRIENDLIST for the new class type. */
5191 typedecl
= TYPE_MAIN_DECL (type
);
5192 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
5198 for (friends
= TREE_VALUE (t
);
5199 friends
!= NULL_TREE
;
5200 friends
= TREE_CHAIN (friends
))
5201 if (TREE_PURPOSE (friends
) == error_mark_node
)
5203 tsubst_friend_function (TREE_VALUE (friends
),
5206 my_friendly_abort (20000216);
5209 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
5213 tree friend_type
= TREE_VALUE (t
);
5214 tree new_friend_type
;
5216 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5217 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5218 else if (uses_template_parms (friend_type
))
5219 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
5223 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5225 /* The call to xref_tag_from_type does injection for friend
5227 push_nested_namespace (ns
);
5229 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5230 pop_nested_namespace (ns
);
5233 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5234 /* Trick make_friend_class into realizing that the friend
5235 we're adding is a template, not an ordinary class. It's
5236 important that we use make_friend_class since it will
5237 perform some error-checking and output cross-reference
5239 ++processing_template_decl
;
5241 if (new_friend_type
!= error_mark_node
)
5242 make_friend_class (type
, new_friend_type
);
5244 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5245 --processing_template_decl
;
5248 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5249 if (TREE_CODE (t
) == FIELD_DECL
)
5251 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5252 require_complete_type (t
);
5255 /* Set the file and line number information to whatever is given for
5256 the class itself. This puts error messages involving generated
5257 implicit functions at a predictable point, and the same point
5258 that would be used for non-template classes. */
5259 lineno
= DECL_SOURCE_LINE (typedecl
);
5260 input_filename
= DECL_SOURCE_FILE (typedecl
);
5262 unreverse_member_declarations (type
);
5263 finish_struct_1 (type
);
5264 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5266 /* Clear this now so repo_template_used is happy. */
5267 TYPE_BEING_DEFINED (type
) = 0;
5268 repo_template_used (type
);
5270 /* Now that the class is complete, instantiate default arguments for
5271 any member functions. We don't do this earlier because the
5272 default arguments may reference members of the class. */
5273 if (!PRIMARY_TEMPLATE_P (template))
5274 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5275 if (TREE_CODE (t
) == FUNCTION_DECL
5276 /* Implicitly generated member functions will not have template
5277 information; they are not instantiations, but instead are
5278 created "fresh" for each instantiation. */
5279 && DECL_TEMPLATE_INFO (t
))
5280 tsubst_default_arguments (t
);
5283 pop_from_top_level ();
5293 if (t1
== NULL_TREE
)
5294 return t2
== NULL_TREE
;
5295 if (t2
== NULL_TREE
)
5297 /* Don't care if one declares its arg const and the other doesn't -- the
5298 main variant of the arg type is all that matters. */
5299 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5300 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5302 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5305 /* If arg is a non-type template parameter that does not depend on template
5306 arguments, fold it like we weren't in the body of a template. */
5309 maybe_fold_nontype_arg (arg
)
5312 /* If we're not in a template, ARG is already as simple as it's going to
5313 get, and trying to reprocess the trees will break. */
5314 if (! processing_template_decl
)
5317 if (!TYPE_P (arg
) && !uses_template_parms (arg
))
5319 /* Sometimes, one of the args was an expression involving a
5320 template constant parameter, like N - 1. Now that we've
5321 tsubst'd, we might have something like 2 - 1. This will
5322 confuse lookup_template_class, so we do constant folding
5323 here. We have to unset processing_template_decl, to
5324 fool build_expr_from_tree() into building an actual
5327 int saved_processing_template_decl
= processing_template_decl
;
5328 processing_template_decl
= 0;
5329 arg
= fold (build_expr_from_tree (arg
));
5330 processing_template_decl
= saved_processing_template_decl
;
5335 /* Substitute ARGS into the vector of template arguments T. */
5338 tsubst_template_arg_vector (t
, args
, complain
)
5343 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5344 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5346 memset ((char *) elts
, 0, len
* sizeof (tree
));
5348 for (i
= 0; i
< len
; i
++)
5350 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5351 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5352 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5355 elts
[i
] = maybe_fold_nontype_arg
5356 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5359 if (elts
[i
] == error_mark_node
)
5360 return error_mark_node
;
5362 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5369 t
= make_tree_vec (len
);
5370 for (i
= 0; i
< len
; i
++)
5371 TREE_VEC_ELT (t
, i
) = elts
[i
];
5376 /* Return the result of substituting ARGS into the template parameters
5377 given by PARMS. If there are m levels of ARGS and m + n levels of
5378 PARMS, then the result will contain n levels of PARMS. For
5379 example, if PARMS is `template <class T> template <class U>
5380 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5381 result will be `template <int*, double, class V>'. */
5384 tsubst_template_parms (parms
, args
, complain
)
5392 for (new_parms
= &r
;
5393 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5394 new_parms
= &(TREE_CHAIN (*new_parms
)),
5395 parms
= TREE_CHAIN (parms
))
5398 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5401 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5403 tree default_value
=
5404 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5406 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5408 TREE_VEC_ELT (new_vec
, i
)
5409 = build_tree_list (tsubst (default_value
, args
, complain
,
5411 tsubst (parm_decl
, args
, complain
,
5416 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
5417 - TMPL_ARGS_DEPTH (args
)),
5418 new_vec
, NULL_TREE
);
5424 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5425 type T. If T is not an aggregate or enumeration type, it is
5426 handled as if by tsubst. IN_DECL is as for tsubst. If
5427 ENTERING_SCOPE is non-zero, T is the context for a template which
5428 we are presently tsubst'ing. Return the substituted value. */
5431 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5441 switch (TREE_CODE (t
))
5444 if (TYPE_PTRMEMFUNC_P (t
))
5446 tree r
= build_ptrmemfunc_type
5447 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5448 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5452 /* else fall through */
5455 if (TYPE_TEMPLATE_INFO (t
))
5461 /* First, determine the context for the type we are looking
5463 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5464 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5466 in_decl
, /*entering_scope=*/1);
5468 context
= NULL_TREE
;
5470 /* Then, figure out what arguments are appropriate for the
5471 type we are trying to find. For example, given:
5473 template <class T> struct S;
5474 template <class T, class U> void f(T, U) { S<U> su; }
5476 and supposing that we are instantiating f<int, double>,
5477 then our ARGS will be {int, double}, but, when looking up
5478 S we only want {double}. */
5479 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5481 if (argvec
== error_mark_node
)
5482 return error_mark_node
;
5484 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5485 entering_scope
, complain
);
5487 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5491 /* This is not a template type, so there's nothing to do. */
5495 return tsubst (t
, args
, complain
, in_decl
);
5499 /* Substitute into the default argument ARG (a default argument for
5500 FN), which has the indicated TYPE. */
5503 tsubst_default_argument (fn
, type
, arg
)
5508 /* This default argument came from a template. Instantiate the
5509 default argument here, not in tsubst. In the case of
5518 we must be careful to do name lookup in the scope of S<T>,
5519 rather than in the current class. */
5520 if (DECL_CLASS_SCOPE_P (fn
))
5521 pushclass (DECL_CONTEXT (fn
), 2);
5523 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), /*complain=*/1, NULL_TREE
);
5525 if (DECL_CLASS_SCOPE_P (fn
))
5528 /* Make sure the default argument is reasonable. */
5529 arg
= check_default_argument (type
, arg
);
5534 /* Substitute into all the default arguments for FN. */
5537 tsubst_default_arguments (fn
)
5543 tmpl_args
= DECL_TI_ARGS (fn
);
5545 /* If this function is not yet instantiated, we certainly don't need
5546 its default arguments. */
5547 if (uses_template_parms (tmpl_args
))
5550 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5552 arg
= TREE_CHAIN (arg
))
5553 if (TREE_PURPOSE (arg
))
5554 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5556 TREE_PURPOSE (arg
));
5559 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5560 (already computed) substitution of ARGS into TREE_TYPE (T), if
5561 appropriate. Return the result of the substitution. IN_DECL is as
5565 tsubst_decl (t
, args
, type
, in_decl
)
5572 const char *saved_filename
;
5575 /* Set the filename and linenumber to improve error-reporting. */
5576 saved_lineno
= lineno
;
5577 saved_filename
= input_filename
;
5578 lineno
= DECL_SOURCE_LINE (t
);
5579 input_filename
= DECL_SOURCE_FILE (t
);
5581 switch (TREE_CODE (t
))
5585 /* We can get here when processing a member template function
5586 of a template class. */
5587 tree decl
= DECL_TEMPLATE_RESULT (t
);
5589 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5591 if (!is_template_template_parm
)
5593 /* We might already have an instance of this template.
5594 The ARGS are for the surrounding class type, so the
5595 full args contain the tsubst'd args for the context,
5596 plus the innermost args from the template decl. */
5597 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5598 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5599 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
5602 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5605 /* tsubst_template_arg_vector doesn't copy the vector if
5606 nothing changed. But, *something* should have
5608 my_friendly_assert (full_args
!= tmpl_args
, 0);
5610 spec
= retrieve_specialization (t
, full_args
);
5611 if (spec
!= NULL_TREE
)
5618 /* Make a new template decl. It will be similar to the
5619 original, but will record the current template arguments.
5620 We also create a new function declaration, which is just
5621 like the old one, but points to this new template, rather
5622 than the old one. */
5624 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5625 TREE_CHAIN (r
) = NULL_TREE
;
5627 if (is_template_template_parm
)
5629 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5630 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5631 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5636 = tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5637 /*complain=*/1, in_decl
,
5638 /*entering_scope=*/1);
5639 DECL_VIRTUAL_CONTEXT (r
)
5640 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5641 /*complain=*/1, in_decl
,
5642 /*entering_scope=*/1);
5643 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5645 if (TREE_CODE (decl
) == TYPE_DECL
)
5647 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5648 /*complain=*/1, in_decl
);
5649 TREE_TYPE (r
) = new_type
;
5650 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5651 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5652 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5656 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5658 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5659 DECL_TI_TEMPLATE (new_decl
) = r
;
5660 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5661 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5664 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5665 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5666 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5668 /* The template parameters for this new template are all the
5669 template parameters for the old template, except the
5670 outermost level of parameters. */
5671 DECL_TEMPLATE_PARMS (r
)
5672 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5675 if (PRIMARY_TEMPLATE_P (t
))
5676 DECL_PRIMARY_TEMPLATE (r
) = r
;
5678 /* We don't partially instantiate partial specializations. */
5679 if (TREE_CODE (decl
) == TYPE_DECL
)
5682 /* Record this partial instantiation. */
5683 register_specialization (r
, t
,
5684 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)));
5692 tree argvec
= NULL_TREE
;
5699 /* Nobody should be tsubst'ing into non-template functions. */
5700 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5702 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5706 /* Calculate the most general template of which R is a
5707 specialization, and the complete set of arguments used to
5709 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5711 = tsubst_template_arg_vector (DECL_TI_ARGS
5712 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5713 args
, /*complain=*/1);
5715 /* Check to see if we already have this specialization. */
5716 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5724 /* We can see more levels of arguments than parameters if
5725 there was a specialization of a member template, like
5728 template <class T> struct S { template <class U> void f(); }
5729 template <> template <class U> void S<int>::f(U);
5731 Here, we'll be substituting into the specialization,
5732 because that's where we can find the code we actually
5733 want to generate, but we'll have enough arguments for
5734 the most general template.
5736 We also deal with the peculiar case:
5738 template <class T> struct S {
5739 template <class U> friend void f();
5741 template <class U> void f() {}
5743 template void f<double>();
5745 Here, the ARGS for the instantiation of will be {int,
5746 double}. But, we only need as many ARGS as there are
5747 levels of template parameters in CODE_PATTERN. We are
5748 careful not to get fooled into reducing the ARGS in
5751 template <class T> struct S { template <class U> void f(U); }
5752 template <class T> template <> void S<T>::f(int) {}
5754 which we can spot because the pattern will be a
5755 specialization in this case. */
5756 args_depth
= TMPL_ARGS_DEPTH (args
);
5758 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5759 if (args_depth
> parms_depth
5760 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5761 args
= get_innermost_template_args (args
, parms_depth
);
5765 /* This special case arises when we have something like this:
5767 template <class T> struct S {
5768 friend void f<int>(int, double);
5771 Here, the DECL_TI_TEMPLATE for the friend declaration
5772 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5773 being called from tsubst_friend_function, and we want
5774 only to create a new decl (R) with appropriate types so
5775 that we can call determine_specialization. */
5776 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5778 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5779 == IDENTIFIER_NODE
), 0);
5780 gen_tmpl
= NULL_TREE
;
5783 if (DECL_CLASS_SCOPE_P (t
))
5785 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5789 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5791 /*entering_scope=*/1);
5796 ctx
= DECL_CONTEXT (t
);
5798 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5799 if (type
== error_mark_node
)
5800 return error_mark_node
;
5802 /* We do NOT check for matching decls pushed separately at this
5803 point, as they may not represent instantiations of this
5804 template, and in any case are considered separate under the
5807 DECL_USE_TEMPLATE (r
) = 0;
5808 TREE_TYPE (r
) = type
;
5810 DECL_CONTEXT (r
) = ctx
;
5811 DECL_VIRTUAL_CONTEXT (r
)
5812 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t
), args
,
5814 /*entering_scope=*/1);
5816 if (member
&& DECL_CONV_FN_P (r
))
5818 /* Type-conversion operator. Reconstruct the name, in
5819 case it's the name of one of the template's parameters. */
5821 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
5823 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5826 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5828 DECL_RESULT (r
) = NULL_TREE
;
5830 TREE_STATIC (r
) = 0;
5831 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5832 DECL_EXTERNAL (r
) = 1;
5833 DECL_INTERFACE_KNOWN (r
) = 0;
5834 DECL_DEFER_OUTPUT (r
) = 0;
5835 TREE_CHAIN (r
) = NULL_TREE
;
5836 DECL_PENDING_INLINE_INFO (r
) = 0;
5837 DECL_PENDING_INLINE_P (r
) = 0;
5838 DECL_SAVED_TREE (r
) = NULL_TREE
;
5840 if (DECL_CLONED_FUNCTION (r
))
5842 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
5843 args
, /*complain=*/1, t
);
5844 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
5845 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
5848 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5849 name. There's no need to do this in the special friend
5850 case mentioned above where GEN_TMPL is NULL. */
5853 DECL_TEMPLATE_INFO (r
)
5854 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5855 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5856 register_specialization (r
, gen_tmpl
, argvec
);
5858 /* Set the mangled name for R. */
5859 if (DECL_DESTRUCTOR_P (t
))
5862 set_mangled_name_for_decl (r
);
5864 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5868 /* Instantiations of template functions must be mangled
5869 specially, in order to conform to 14.5.5.1
5870 [temp.over.link]. */
5871 tree tmpl
= DECL_TI_TEMPLATE (t
);
5873 /* TMPL will be NULL if this is a specialization of a
5874 member function of a template class. */
5875 if (name_mangling_version
< 1
5876 || tmpl
== NULL_TREE
5877 || (member
&& !is_member_template (tmpl
)
5878 && !DECL_TEMPLATE_INFO (tmpl
)))
5879 set_mangled_name_for_decl (r
);
5881 set_mangled_name_for_template_decl (r
);
5885 make_decl_rtl (r
, NULL_PTR
);
5887 /* Like grokfndecl. If we don't do this, pushdecl will
5888 mess up our TREE_CHAIN because it doesn't find a
5889 previous decl. Sigh. */
5891 && ! uses_template_parms (r
)
5892 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5894 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5896 /* We're not supposed to instantiate default arguments
5897 until they are called, for a template. But, for a
5900 template <class T> void f ()
5901 { extern void g(int i = T()); }
5903 we should do the substitution when the template is
5904 instantiated. We handle the member function case in
5905 instantiate_class_template since the default arguments
5906 might refer to other members of the class. */
5908 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
5909 && !uses_template_parms (argvec
))
5910 tsubst_default_arguments (r
);
5913 /* Copy the list of befriending classes. */
5914 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5916 friends
= &TREE_CHAIN (*friends
))
5918 *friends
= copy_node (*friends
);
5919 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5920 args
, /*complain=*/1,
5924 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
5926 maybe_retrofit_in_chrg (r
);
5927 if (DECL_CONSTRUCTOR_P (r
))
5928 grok_ctor_properties (ctx
, r
);
5929 /* If this is an instantiation of a member template, clone it.
5930 If it isn't, that'll be handled by
5931 clone_constructors_and_destructors. */
5932 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
5933 clone_function_decl(r
, /*update_method_vec_p=*/0);
5935 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5936 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5943 TREE_TYPE (r
) = type
;
5944 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5946 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5947 DECL_INITIAL (r
) = TREE_TYPE (r
);
5949 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5950 /*complain=*/1, in_decl
);
5952 DECL_CONTEXT (r
) = NULL_TREE
;
5953 if (PROMOTE_PROTOTYPES
5954 && (TREE_CODE (type
) == INTEGER_TYPE
5955 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5956 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5957 DECL_ARG_TYPE (r
) = integer_type_node
;
5959 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5960 /*complain=*/1, TREE_CHAIN (t
));
5967 TREE_TYPE (r
) = type
;
5968 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5970 /* We don't have to set DECL_CONTEXT here; it is set by
5971 finish_member_declaration. */
5972 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
5973 /*complain=*/1, in_decl
);
5974 TREE_CHAIN (r
) = NULL_TREE
;
5975 if (TREE_CODE (type
) == VOID_TYPE
)
5976 cp_error_at ("instantiation of `%D' as type void", r
);
5984 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
5985 TREE_CHAIN (r
) = NULL_TREE
;
5990 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
5991 || t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
5993 /* If this is the canonical decl, we don't have to mess with
5994 instantiations, and often we can't (for typename, template
5995 type parms and such). Note that TYPE_NAME is not correct for
5996 the above test if we've copied the type for a typedef. */
5997 r
= TYPE_NAME (type
);
6005 tree argvec
= NULL_TREE
;
6006 tree gen_tmpl
= NULL_TREE
;
6008 tree tmpl
= NULL_TREE
;
6012 /* Assume this is a non-local variable. */
6015 if (TYPE_P (CP_DECL_CONTEXT (t
)))
6016 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6018 in_decl
, /*entering_scope=*/1);
6021 /* Subsequent calls to pushdecl will fill this in. */
6023 if (!DECL_NAMESPACE_SCOPE_P (t
))
6027 /* Check to see if we already have this specialization. */
6030 tmpl
= DECL_TI_TEMPLATE (t
);
6031 gen_tmpl
= most_general_template (tmpl
);
6032 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
6033 spec
= retrieve_specialization (gen_tmpl
, argvec
);
6036 spec
= retrieve_local_specialization (t
);
6045 TREE_TYPE (r
) = type
;
6046 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
6047 DECL_CONTEXT (r
) = ctx
;
6049 /* Don't try to expand the initializer until someone tries to use
6050 this variable; otherwise we run into circular dependencies. */
6051 DECL_INITIAL (r
) = NULL_TREE
;
6053 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
6055 /* For __PRETTY_FUNCTION__ we have to adjust the initializer. */
6056 if (DECL_PRETTY_FUNCTION_P (r
))
6058 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (t
),
6062 TREE_TYPE (r
) = TREE_TYPE (DECL_INITIAL (r
));
6065 /* Even if the original location is out of scope, the newly
6066 substituted one is not. */
6067 if (TREE_CODE (r
) == VAR_DECL
)
6068 DECL_DEAD_FOR_LOCAL (r
) = 0;
6072 /* A static data member declaration is always marked
6073 external when it is declared in-class, even if an
6074 initializer is present. We mimic the non-template
6076 DECL_EXTERNAL (r
) = 1;
6078 register_specialization (r
, gen_tmpl
, argvec
);
6079 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
6080 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6083 register_local_specialization (r
, t
);
6085 TREE_CHAIN (r
) = NULL_TREE
;
6086 if (TREE_CODE (r
) == VAR_DECL
&& TREE_CODE (type
) == VOID_TYPE
)
6087 cp_error_at ("instantiation of `%D' as type void", r
);
6092 my_friendly_abort (0);
6095 /* Restore the file and line information. */
6096 lineno
= saved_lineno
;
6097 input_filename
= saved_filename
;
6102 /* Substitue into the ARG_TYPES of a function type. */
6105 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
6111 tree remaining_arg_types
;
6114 if (!arg_types
|| arg_types
== void_list_node
)
6117 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6118 args
, complain
, in_decl
);
6119 if (remaining_arg_types
== error_mark_node
)
6120 return error_mark_node
;
6122 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6123 if (type
== error_mark_node
)
6124 return error_mark_node
;
6126 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6127 top-level qualifiers as required. */
6128 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6130 /* Note that we do not substitute into default arguments here. The
6131 standard mandates that they be instantiated only when needed,
6132 which is done in build_over_call. */
6133 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6134 remaining_arg_types
);
6138 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6139 *not* handle the exception-specification for FNTYPE, because the
6140 initial substitution of explicitly provided template parameters
6141 during argument deduction forbids substitution into the
6142 exception-specification:
6146 All references in the function type of the function template to the
6147 corresponding template parameters are replaced by the specified tem-
6148 plate argument values. If a substitution in a template parameter or
6149 in the function type of the function template results in an invalid
6150 type, type deduction fails. [Note: The equivalent substitution in
6151 exception specifications is done only when the function is instanti-
6152 ated, at which point a program is ill-formed if the substitution
6153 results in an invalid type.] */
6156 tsubst_function_type (t
, args
, complain
, in_decl
)
6166 /* The TYPE_CONTEXT is not used for function/method types. */
6167 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6169 /* Substitue the return type. */
6170 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6171 if (return_type
== error_mark_node
)
6172 return error_mark_node
;
6174 /* Substitue the argument types. */
6175 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6177 if (arg_types
== error_mark_node
)
6178 return error_mark_node
;
6180 /* Construct a new type node and return it. */
6181 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6182 fntype
= build_function_type (return_type
, arg_types
);
6185 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6186 if (! IS_AGGR_TYPE (r
))
6190 Type deduction may fail for any of the following
6193 -- Attempting to create "pointer to member of T" when T
6194 is not a class type. */
6196 cp_error ("creating pointer to member function of non-class type `%T'",
6198 return error_mark_node
;
6201 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6204 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
6205 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6210 /* Substitute into the PARMS of a call-declarator. */
6213 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
6223 if (!parms
|| parms
== void_list_node
)
6226 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6227 args
, complain
, in_decl
);
6229 /* Figure out the type of this parameter. */
6230 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6232 /* Figure out the default argument as well. Note that we use
6233 tsubst_expr since the default argument is really an expression. */
6234 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6236 /* Chain this parameter on to the front of those we have already
6237 processed. We don't use hash_tree_cons because that function
6238 doesn't check TREE_PARMLIST. */
6239 new_parms
= tree_cons (defarg
, type
, new_parms
);
6241 /* And note that these are parameters. */
6242 TREE_PARMLIST (new_parms
) = 1;
6247 /* Take the tree structure T and replace template parameters used
6248 therein with the argument vector ARGS. IN_DECL is an associated
6249 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6250 An appropriate error message is issued only if COMPLAIN is
6251 non-zero. Note that we must be relatively non-tolerant of
6252 extensions here, in order to preserve conformance; if we allow
6253 substitutions that should not be allowed, we may allow argument
6254 deductions that should not succeed, and therefore report ambiguous
6255 overload situations where there are none. In theory, we could
6256 allow the substitution, but indicate that it should have failed,
6257 and allow our caller to make sure that the right thing happens, but
6258 we don't try to do this yet.
6260 This function is used for dealing with types, decls and the like;
6261 for expressions, use tsubst_expr or tsubst_copy. */
6264 tsubst (t
, args
, complain
, in_decl
)
6271 if (t
== NULL_TREE
|| t
== error_mark_node
6272 || t
== integer_type_node
6273 || t
== void_type_node
6274 || t
== char_type_node
6275 || TREE_CODE (t
) == NAMESPACE_DECL
)
6278 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6279 type
= IDENTIFIER_TYPE_VALUE (t
);
6281 type
= TREE_TYPE (t
);
6282 if (type
== unknown_type_node
)
6283 my_friendly_abort (42);
6285 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6286 && TREE_CODE (t
) != TYPENAME_TYPE
6287 && TREE_CODE (t
) != TEMPLATE_DECL
6288 && TREE_CODE (t
) != IDENTIFIER_NODE
6289 && TREE_CODE (t
) != FUNCTION_TYPE
6290 && TREE_CODE (t
) != METHOD_TYPE
)
6291 type
= tsubst (type
, args
, complain
, in_decl
);
6292 if (type
== error_mark_node
)
6293 return error_mark_node
;
6296 return tsubst_decl (t
, args
, type
, in_decl
);
6298 switch (TREE_CODE (t
))
6303 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6304 /*entering_scope=*/0);
6307 case IDENTIFIER_NODE
:
6320 if (t
== integer_type_node
)
6323 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6324 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6328 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6330 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6331 if (max
== error_mark_node
)
6332 return error_mark_node
;
6334 /* See if we can reduce this expression to something simpler. */
6335 max
= maybe_fold_nontype_arg (max
);
6336 if (!processing_template_decl
)
6337 max
= decl_constant_value (max
);
6339 if (processing_template_decl
6340 /* When providing explicit arguments to a template
6341 function, but leaving some arguments for subsequent
6342 deduction, MAX may be template-dependent even if we're
6343 not PROCESSING_TEMPLATE_DECL. We still need to check for
6344 template parms, though; MAX won't be an INTEGER_CST for
6345 dynamic arrays, either. */
6346 || (TREE_CODE (max
) != INTEGER_CST
6347 && uses_template_parms (max
)))
6349 tree itype
= make_node (INTEGER_TYPE
);
6350 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6351 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6356 if (integer_zerop (omax
))
6358 /* Still allow an explicit array of size zero. */
6360 pedwarn ("creating array with size zero");
6362 else if (integer_zerop (max
)
6363 || (TREE_CODE (max
) == INTEGER_CST
6364 && INT_CST_LT (max
, integer_zero_node
)))
6368 Type deduction may fail for any of the following
6371 Attempting to create an array with a size that is
6372 zero or negative. */
6374 cp_error ("creating array with size zero (`%E')", max
);
6376 return error_mark_node
;
6379 return compute_array_index_type (NULL_TREE
, max
);
6382 case TEMPLATE_TYPE_PARM
:
6383 case TEMPLATE_TEMPLATE_PARM
:
6384 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6385 case TEMPLATE_PARM_INDEX
:
6393 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6394 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6395 || TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6397 idx
= TEMPLATE_TYPE_IDX (t
);
6398 level
= TEMPLATE_TYPE_LEVEL (t
);
6402 idx
= TEMPLATE_PARM_IDX (t
);
6403 level
= TEMPLATE_PARM_LEVEL (t
);
6406 if (TREE_VEC_LENGTH (args
) > 0)
6408 tree arg
= NULL_TREE
;
6410 levels
= TMPL_ARGS_DEPTH (args
);
6411 if (level
<= levels
)
6412 arg
= TMPL_ARG (args
, level
, idx
);
6414 if (arg
== error_mark_node
)
6415 return error_mark_node
;
6416 else if (arg
!= NULL_TREE
)
6418 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6420 my_friendly_assert (TYPE_P (arg
), 0);
6421 return cp_build_qualified_type_real
6422 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
),
6425 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6427 /* We are processing a type constructed from
6428 a template template parameter */
6429 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6430 args
, complain
, in_decl
);
6431 if (argvec
== error_mark_node
)
6432 return error_mark_node
;
6434 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6435 we are resolving nested-types in the signature of
6436 a member function templates.
6437 Otherwise ARG is a TEMPLATE_DECL and is the real
6438 template to be instantiated. */
6439 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6440 arg
= TYPE_NAME (arg
);
6442 r
= lookup_template_class (arg
,
6445 /*entering_scope=*/0,
6447 return cp_build_qualified_type_real (r
,
6452 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6457 my_friendly_abort (981018);
6460 /* This can happen during the attempted tsubst'ing in
6461 unify. This means that we don't yet have any information
6462 about the template parameter in question. */
6465 /* If we get here, we must have been looking at a parm for a
6466 more deeply nested template. Make a new version of this
6467 template parameter, but with a lower level. */
6468 switch (TREE_CODE (t
))
6470 case TEMPLATE_TYPE_PARM
:
6471 case TEMPLATE_TEMPLATE_PARM
:
6472 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6473 if (CP_TYPE_QUALS (t
))
6475 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
6476 r
= cp_build_qualified_type_real (r
, CP_TYPE_QUALS (t
),
6482 TEMPLATE_TYPE_PARM_INDEX (r
)
6483 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6485 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6486 TYPE_MAIN_VARIANT (r
) = r
;
6487 TYPE_POINTER_TO (r
) = NULL_TREE
;
6488 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6490 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6492 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6494 if (argvec
== error_mark_node
)
6495 return error_mark_node
;
6497 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6498 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
6503 case TEMPLATE_PARM_INDEX
:
6504 r
= reduce_template_parm_level (t
, type
, levels
);
6508 my_friendly_abort (0);
6516 tree purpose
, value
, chain
, result
;
6518 if (t
== void_list_node
)
6521 purpose
= TREE_PURPOSE (t
);
6524 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6525 if (purpose
== error_mark_node
)
6526 return error_mark_node
;
6528 value
= TREE_VALUE (t
);
6531 value
= tsubst (value
, args
, complain
, in_decl
);
6532 if (value
== error_mark_node
)
6533 return error_mark_node
;
6535 chain
= TREE_CHAIN (t
);
6536 if (chain
&& chain
!= void_type_node
)
6538 chain
= tsubst (chain
, args
, complain
, in_decl
);
6539 if (chain
== error_mark_node
)
6540 return error_mark_node
;
6542 if (purpose
== TREE_PURPOSE (t
)
6543 && value
== TREE_VALUE (t
)
6544 && chain
== TREE_CHAIN (t
))
6546 if (TREE_PARMLIST (t
))
6548 result
= tree_cons (purpose
, value
, chain
);
6549 TREE_PARMLIST (result
) = 1;
6552 result
= hash_tree_cons (purpose
, value
, chain
);
6556 if (type
!= NULL_TREE
)
6558 /* A binfo node. We always need to make a copy, of the node
6559 itself and of its BINFO_BASETYPES. */
6563 /* Make sure type isn't a typedef copy. */
6564 type
= BINFO_TYPE (TYPE_BINFO (type
));
6566 TREE_TYPE (t
) = complete_type (type
);
6567 if (IS_AGGR_TYPE (type
))
6569 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6570 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6571 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6572 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6577 /* Otherwise, a vector of template arguments. */
6578 return tsubst_template_arg_vector (t
, args
, complain
);
6581 case REFERENCE_TYPE
:
6583 enum tree_code code
;
6585 if (type
== TREE_TYPE (t
))
6588 code
= TREE_CODE (t
);
6593 Type deduction may fail for any of the following
6596 -- Attempting to create a pointer to reference type.
6597 -- Attempting to create a reference to a reference type or
6598 a reference to void. */
6599 if (TREE_CODE (type
) == REFERENCE_TYPE
6600 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6602 static int last_line
= 0;
6603 static const char* last_file
= 0;
6605 /* We keep track of the last time we issued this error
6606 message to avoid spewing a ton of messages during a
6607 single bad template instantiation. */
6608 if (complain
&& (last_line
!= lineno
||
6609 last_file
!= input_filename
))
6611 if (TREE_CODE (type
) == VOID_TYPE
)
6612 cp_error ("forming reference to void");
6614 cp_error ("forming %s to reference type `%T'",
6615 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6618 last_file
= input_filename
;
6621 return error_mark_node
;
6623 else if (code
== POINTER_TYPE
)
6624 r
= build_pointer_type (type
);
6626 r
= build_reference_type (type
);
6627 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6629 if (r
!= error_mark_node
)
6630 /* Will this ever be needed for TYPE_..._TO values? */
6637 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6638 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6642 Type deduction may fail for any of the following
6645 -- Attempting to create "pointer to member of T" when T
6646 is not a class type. */
6648 cp_error ("creating pointer to member of non-class type `%T'",
6650 return error_mark_node
;
6652 return build_offset_type (r
, type
);
6660 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6661 if (fntype
== error_mark_node
)
6662 return error_mark_node
;
6664 /* Substitue the exception specification. */
6665 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6668 tree list
= NULL_TREE
;
6670 if (! TREE_VALUE (raises
))
6673 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6675 tree spec
= TREE_VALUE (raises
);
6677 spec
= tsubst (spec
, args
, complain
, in_decl
);
6678 if (spec
== error_mark_node
)
6680 list
= add_exception_specifier (list
, spec
, complain
);
6682 fntype
= build_exception_variant (fntype
, list
);
6688 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6689 if (domain
== error_mark_node
)
6690 return error_mark_node
;
6692 /* As an optimization, we avoid regenerating the array type if
6693 it will obviously be the same as T. */
6694 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6697 /* These checks should match the ones in grokdeclarator.
6701 The deduction may fail for any of the following reasons:
6703 -- Attempting to create an array with an element type that
6704 is void, a function type, or a reference type. */
6705 if (TREE_CODE (type
) == VOID_TYPE
6706 || TREE_CODE (type
) == FUNCTION_TYPE
6707 || TREE_CODE (type
) == REFERENCE_TYPE
)
6710 cp_error ("creating array of `%T'", type
);
6711 return error_mark_node
;
6714 r
= build_cplus_array_type (type
, domain
);
6721 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6723 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6726 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6727 return error_mark_node
;
6729 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6735 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6737 if (e
== error_mark_node
)
6738 return error_mark_node
;
6740 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6745 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6746 in_decl
, /*entering_scope=*/1);
6747 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6750 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6751 return error_mark_node
;
6753 if (!IS_AGGR_TYPE (ctx
))
6756 cp_error ("`%T' is not a class, struct, or union type",
6758 return error_mark_node
;
6760 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6762 /* Normally, make_typename_type does not require that the CTX
6763 have complete type in order to allow things like:
6765 template <class T> struct S { typename S<T>::X Y; };
6767 But, such constructs have already been resolved by this
6768 point, so here CTX really should have complete type, unless
6769 it's a partial instantiation. */
6770 ctx
= complete_type (ctx
);
6771 if (!COMPLETE_TYPE_P (ctx
))
6774 incomplete_type_error (NULL_TREE
, ctx
);
6775 return error_mark_node
;
6779 f
= make_typename_type (ctx
, f
, complain
);
6780 if (f
== error_mark_node
)
6782 return cp_build_qualified_type_real (f
,
6784 | CP_TYPE_QUALS (t
),
6790 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6792 if (e
== error_mark_node
)
6793 return error_mark_node
;
6794 return make_pointer_declarator (type
, e
);
6799 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6801 if (e
== error_mark_node
)
6802 return error_mark_node
;
6803 return make_reference_declarator (type
, e
);
6808 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6810 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
,
6812 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6813 return error_mark_node
;
6815 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6820 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6822 tree e2
= (tsubst_call_declarator_parms
6823 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
6824 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
6827 if (e1
== error_mark_node
|| e2
== error_mark_node
6828 || e3
== error_mark_node
)
6829 return error_mark_node
;
6831 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
6836 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6838 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6839 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6840 return error_mark_node
;
6842 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6847 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6849 if (e1
== error_mark_node
)
6850 return error_mark_node
;
6852 return TREE_TYPE (e1
);
6862 /* This code should match declare_hidden_char_array in
6864 name
= (*decl_printable_name
) (current_function_decl
, 2);
6865 len
= strlen (name
) + 1;
6866 type
= build_array_type (char_type_node
,
6867 build_index_type (size_int (len
)));
6868 str
= build_string (len
, name
);
6869 TREE_TYPE (str
) = type
;
6874 sorry ("use of `%s' in template",
6875 tree_code_name
[(int) TREE_CODE (t
)]);
6876 return error_mark_node
;
6880 /* Like tsubst, but deals with expressions. This function just replaces
6881 template parms; to finish processing the resultant expression, use
6885 tsubst_copy (t
, args
, complain
, in_decl
)
6890 enum tree_code code
;
6893 if (t
== NULL_TREE
|| t
== error_mark_node
)
6896 code
= TREE_CODE (t
);
6901 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6908 if (!DECL_CONTEXT (t
))
6909 /* This is a global enumeration constant. */
6912 /* Unfortunately, we cannot just call lookup_name here.
6915 template <int I> int f() {
6917 struct S { void g() { E e = a; } };
6920 When we instantiate f<7>::S::g(), say, lookup_name is not
6921 clever enough to find f<7>::a. */
6923 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6924 /*entering_scope=*/0);
6926 for (v
= TYPE_VALUES (enum_type
);
6929 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6930 return TREE_VALUE (v
);
6932 /* We didn't find the name. That should never happen; if
6933 name-lookup found it during preliminary parsing, we
6934 should find it again here during instantiation. */
6935 my_friendly_abort (0);
6940 if (DECL_CONTEXT (t
))
6944 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6945 /*entering_scope=*/1);
6946 if (ctx
!= DECL_CONTEXT (t
))
6947 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6953 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6954 t
= tsubst (t
, args
, complain
, in_decl
);
6959 if (is_member_template (t
))
6960 return tsubst (t
, args
, complain
, in_decl
);
6966 /* We must tsbust into a LOOKUP_EXPR in case the names to
6967 which it refers is a conversion operator; in that case the
6968 name will change. We avoid making unnecessary copies,
6971 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6973 if (id
!= TREE_OPERAND (t
, 0))
6975 r
= build_nt (LOOKUP_EXPR
, id
);
6976 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6984 case REINTERPRET_CAST_EXPR
:
6985 case CONST_CAST_EXPR
:
6986 case STATIC_CAST_EXPR
:
6987 case DYNAMIC_CAST_EXPR
:
6990 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6991 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6995 case TRUTH_NOT_EXPR
:
6998 case CONVERT_EXPR
: /* Unary + */
7007 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7008 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7013 case TRUNC_DIV_EXPR
:
7015 case FLOOR_DIV_EXPR
:
7016 case ROUND_DIV_EXPR
:
7017 case EXACT_DIV_EXPR
:
7019 case BIT_ANDTC_EXPR
:
7022 case TRUNC_MOD_EXPR
:
7023 case FLOOR_MOD_EXPR
:
7024 case TRUTH_ANDIF_EXPR
:
7025 case TRUTH_ORIF_EXPR
:
7026 case TRUTH_AND_EXPR
:
7046 case PREDECREMENT_EXPR
:
7047 case PREINCREMENT_EXPR
:
7048 case POSTDECREMENT_EXPR
:
7049 case POSTINCREMENT_EXPR
:
7051 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7052 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7056 tree fn
= TREE_OPERAND (t
, 0);
7057 if (is_overloaded_fn (fn
))
7058 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
7060 /* Sometimes FN is a LOOKUP_EXPR. */
7061 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
7063 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7068 case METHOD_CALL_EXPR
:
7070 tree name
= TREE_OPERAND (t
, 0);
7071 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
7073 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7075 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7077 else if (TREE_CODE (name
) == SCOPE_REF
7078 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
7080 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7082 name
= TREE_OPERAND (name
, 1);
7083 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7085 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7086 name
= build_nt (SCOPE_REF
, base
, name
);
7089 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7091 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
7093 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
7098 /* This processing should really occur in tsubst_expr, However,
7099 tsubst_expr does not recurse into expressions, since it
7100 assumes that there aren't any statements inside them.
7101 Instead, it simply calls build_expr_from_tree. So, we need
7102 to expand the STMT_EXPR here. */
7103 if (!processing_template_decl
)
7105 tree stmt_expr
= begin_stmt_expr ();
7106 tsubst_expr (STMT_EXPR_STMT (t
), args
,
7108 return finish_stmt_expr (stmt_expr
);
7115 case PSEUDO_DTOR_EXPR
:
7118 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7119 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7120 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7127 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7128 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7129 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7130 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7137 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7138 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7139 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7140 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7144 case TEMPLATE_ID_EXPR
:
7146 /* Substituted template arguments */
7147 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7150 if (targs
&& TREE_CODE (targs
) == TREE_LIST
)
7153 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
7154 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
7159 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
7160 TREE_VEC_ELT (targs
, i
)
7161 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
7164 return lookup_template_function
7165 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7170 tree purpose
, value
, chain
;
7172 if (t
== void_list_node
)
7175 purpose
= TREE_PURPOSE (t
);
7177 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7178 value
= TREE_VALUE (t
);
7180 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7181 chain
= TREE_CHAIN (t
);
7182 if (chain
&& chain
!= void_type_node
)
7183 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7184 if (purpose
== TREE_PURPOSE (t
)
7185 && value
== TREE_VALUE (t
)
7186 && chain
== TREE_CHAIN (t
))
7188 return tree_cons (purpose
, value
, chain
);
7195 case TEMPLATE_TYPE_PARM
:
7196 case TEMPLATE_TEMPLATE_PARM
:
7197 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7198 case TEMPLATE_PARM_INDEX
:
7200 case REFERENCE_TYPE
:
7207 return tsubst (t
, args
, complain
, in_decl
);
7209 case IDENTIFIER_NODE
:
7210 if (IDENTIFIER_TYPENAME_P (t
))
7212 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7214 return mangle_conv_op_name_for_type (new_type
);
7216 return (build_typename_overload (new_type
));
7224 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7225 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
7226 complain
, in_decl
));
7227 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7232 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
7234 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7237 return tsubst (t
, args
, complain
, in_decl
);
7244 /* Like tsubst_copy, but also does semantic processing. */
7247 tsubst_expr (t
, args
, complain
, in_decl
)
7254 if (t
== NULL_TREE
|| t
== error_mark_node
)
7257 if (processing_template_decl
)
7258 return tsubst_copy (t
, args
, complain
, in_decl
);
7260 switch (TREE_CODE (t
))
7264 finish_named_return_value
7265 (TREE_OPERAND (t
, 0),
7266 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, in_decl
));
7267 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7270 case CTOR_INITIALIZER
:
7272 tree member_init_list
;
7273 tree base_init_list
;
7277 = tsubst_initializer_list (TREE_OPERAND (t
, 0), args
);
7279 = tsubst_initializer_list (TREE_OPERAND (t
, 1), args
);
7280 setup_vtbl_ptr (member_init_list
, base_init_list
);
7281 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7287 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
7288 args
, complain
, in_decl
));
7293 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7294 args
, complain
, in_decl
));
7303 decl
= DECL_STMT_DECL (t
);
7304 if (TREE_CODE (decl
) == LABEL_DECL
)
7305 finish_label_decl (DECL_NAME (decl
));
7308 init
= DECL_INITIAL (decl
);
7309 decl
= tsubst (decl
, args
, complain
, in_decl
);
7310 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7311 if (decl
!= error_mark_node
)
7313 if (TREE_CODE (decl
) != TYPE_DECL
)
7314 /* Make sure the type is instantiated now. */
7315 complete_type (TREE_TYPE (decl
));
7317 DECL_INITIAL (decl
) = error_mark_node
;
7318 /* By marking the declaration as instantiated, we avoid
7319 trying to instantiate it. Since instantiate_decl can't
7320 handle local variables, and since we've already done
7321 all that needs to be done, that's the right thing to
7323 if (TREE_CODE (decl
) == VAR_DECL
)
7324 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7325 maybe_push_decl (decl
);
7326 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
7337 stmt
= begin_for_stmt ();
7338 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
7339 tsubst_expr (tmp
, args
, complain
, in_decl
);
7340 finish_for_init_stmt (stmt
);
7341 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
7344 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7345 finish_for_expr (tmp
, stmt
);
7346 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7347 finish_for_stmt (stmt
);
7354 stmt
= begin_while_stmt ();
7355 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7356 args
, complain
, in_decl
),
7358 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7359 finish_while_stmt (stmt
);
7366 stmt
= begin_do_stmt ();
7367 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7368 finish_do_body (stmt
);
7369 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
7380 stmt
= begin_if_stmt ();
7381 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7382 args
, complain
, in_decl
),
7385 if (tmp
= THEN_CLAUSE (t
), tmp
)
7387 tsubst_expr (tmp
, args
, complain
, in_decl
);
7388 finish_then_clause (stmt
);
7391 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7393 begin_else_clause ();
7394 tsubst_expr (tmp
, args
, complain
, in_decl
);
7395 finish_else_clause (stmt
);
7407 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7408 for (substmt
= COMPOUND_BODY (t
);
7409 substmt
!= NULL_TREE
;
7410 substmt
= TREE_CHAIN (substmt
))
7411 tsubst_expr (substmt
, args
, complain
, in_decl
);
7412 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7418 finish_break_stmt ();
7423 finish_continue_stmt ();
7431 stmt
= begin_switch_stmt ();
7432 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7433 finish_switch_cond (val
, stmt
);
7434 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7435 finish_switch_stmt (stmt
);
7441 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7442 tsubst_expr (CASE_HIGH (t
), args
, complain
,
7447 lineno
= STMT_LINENO (t
);
7448 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7453 t
= GOTO_DESTINATION (t
);
7454 if (TREE_CODE (t
) != LABEL_DECL
)
7455 /* Computed goto's must be tsubst'd into. On the other hand,
7456 non-computed gotos must not be; the identifier in question
7457 will have no binding. */
7458 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7461 finish_goto_stmt (t
);
7466 finish_asm_stmt (ASM_CV_QUAL (t
),
7467 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7468 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7469 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7470 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7478 stmt
= begin_try_block ();
7479 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7480 finish_cleanup_try_block (stmt
);
7481 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7489 if (FN_TRY_BLOCK_P (t
))
7490 stmt
= begin_function_try_block ();
7492 stmt
= begin_try_block ();
7494 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7496 if (FN_TRY_BLOCK_P (t
))
7497 finish_function_try_block (stmt
);
7499 finish_try_block (stmt
);
7501 handler
= TRY_HANDLERS (t
);
7502 for (; handler
; handler
= TREE_CHAIN (handler
))
7503 tsubst_expr (handler
, args
, complain
, in_decl
);
7504 if (FN_TRY_BLOCK_P (t
))
7505 finish_function_handler_sequence (stmt
);
7507 finish_handler_sequence (stmt
);
7517 stmt
= begin_handler ();
7518 if (HANDLER_PARMS (t
))
7520 decl
= DECL_STMT_DECL (HANDLER_PARMS (t
));
7521 decl
= tsubst (decl
, args
, complain
, in_decl
);
7522 /* Prevent instantiate_decl from trying to instantiate
7523 this variable. We've already done all that needs to be
7525 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7529 blocks
= finish_handler_parms (decl
, stmt
);
7530 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7531 finish_handler (blocks
, stmt
);
7538 tsubst (t
, args
, complain
, NULL_TREE
);
7542 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7547 /* TMPL is a TEMPLATE_DECL for a cloned constructor or destructor.
7548 Instantiate it with the ARGS. */
7551 instantiate_clone (tmpl
, args
)
7558 /* Instantiated the cloned function, rather than the clone. */
7559 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), args
);
7561 /* Then, see if we've already cloned the instantiation. */
7562 for (clone
= TREE_CHAIN (spec
);
7563 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7564 clone
= TREE_CHAIN (clone
))
7565 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
7568 /* If we haven't, do so know. */
7570 clone_function_decl (spec
, /*update_method_vec_p=*/0);
7573 for (clone
= TREE_CHAIN (spec
);
7574 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7575 clone
= TREE_CHAIN (clone
))
7576 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
7579 /* We should always have found the clone by now. */
7580 my_friendly_abort (20000411);
7584 /* Instantiate the indicated variable or function template TMPL with
7585 the template arguments in TARG_PTR. */
7588 instantiate_template (tmpl
, targ_ptr
)
7589 tree tmpl
, targ_ptr
;
7598 if (tmpl
== error_mark_node
)
7599 return error_mark_node
;
7601 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7603 /* If this function is a clone, handle it specially. */
7604 if (DECL_CLONED_FUNCTION_P (tmpl
))
7605 return instantiate_clone (tmpl
, targ_ptr
);
7607 /* Check to see if we already have this specialization. */
7608 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7609 if (spec
!= NULL_TREE
)
7612 gen_tmpl
= most_general_template (tmpl
);
7613 if (tmpl
!= gen_tmpl
)
7615 /* The TMPL is a partial instantiation. To get a full set of
7616 arguments we must add the arguments used to perform the
7617 partial instantiation. */
7618 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7621 /* Check to see if we already have this specialization. */
7622 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7623 if (spec
!= NULL_TREE
)
7627 len
= DECL_NTPARMS (gen_tmpl
);
7628 inner_args
= INNERMOST_TEMPLATE_ARGS (targ_ptr
);
7632 tree t
= TREE_VEC_ELT (inner_args
, i
);
7635 tree nt
= target_type (t
);
7636 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7638 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7639 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7640 return error_mark_node
;
7645 /* substitute template parameters */
7646 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
7647 targ_ptr
, /*complain=*/1, gen_tmpl
);
7648 /* The DECL_TI_TEMPLATE should always be the immediate parent
7649 template, not the most general template. */
7650 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7652 if (flag_external_templates
)
7653 add_pending_template (fndecl
);
7655 /* If we've just instantiated the main entry point for a function,
7656 instantiate all the alternate entry points as well. */
7657 for (clone
= TREE_CHAIN (gen_tmpl
);
7658 clone
&& DECL_CLONED_FUNCTION_P (clone
);
7659 clone
= TREE_CHAIN (clone
))
7660 instantiate_template (clone
, targ_ptr
);
7665 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7666 arguments that are being used when calling it. TARGS is a vector
7667 into which the deduced template arguments are placed.
7669 Return zero for success, 2 for an incomplete match that doesn't resolve
7670 all the types, and 1 for complete failure. An error message will be
7671 printed only for an incomplete match.
7673 If FN is a conversion operator, or we are trying to produce a specific
7674 specialization, RETURN_TYPE is the return type desired.
7676 The EXPLICIT_TARGS are explicit template arguments provided via a
7679 The parameter STRICT is one of:
7682 We are deducing arguments for a function call, as in
7686 We are deducing arguments for a conversion function, as in
7690 We are deducing arguments when doing an explicit instantiation
7691 as in [temp.explicit], when determining an explicit specialization
7692 as in [temp.expl.spec], or when taking the address of a function
7693 template, as in [temp.deduct.funcaddr].
7696 We are deducing arguments when calculating the partial
7697 ordering between specializations of function or class
7698 templates, as in [temp.func.order] and [temp.class.order].
7700 LEN is the number of parms to consider before returning success, or -1
7701 for all. This is used in partial ordering to avoid comparing parms for
7702 which no actual argument was passed, since they are not considered in
7703 overload resolution (and are explicitly excluded from consideration in
7704 partial ordering in [temp.func.order]/6). */
7707 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7709 tree fn
, explicit_targs
, targs
, args
, return_type
;
7710 unification_kind_t strict
;
7717 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7719 fntype
= TREE_TYPE (fn
);
7724 The specified template arguments must match the template
7725 parameters in kind (i.e., type, nontype, template), and there
7726 must not be more arguments than there are parameters;
7727 otherwise type deduction fails.
7729 Nontype arguments must match the types of the corresponding
7730 nontype template parameters, or must be convertible to the
7731 types of the corresponding nontype parameters as specified in
7732 _temp.arg.nontype_, otherwise type deduction fails.
7734 All references in the function type of the function template
7735 to the corresponding template parameters are replaced by the
7736 specified template argument values. If a substitution in a
7737 template parameter or in the function type of the function
7738 template results in an invalid type, type deduction fails. */
7740 tree converted_args
;
7743 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7744 explicit_targs
, NULL_TREE
, /*complain=*/0,
7745 /*require_all_arguments=*/0));
7746 if (converted_args
== error_mark_node
)
7749 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7750 if (fntype
== error_mark_node
)
7753 /* Place the explicitly specified arguments in TARGS. */
7754 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7755 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7758 parms
= TYPE_ARG_TYPES (fntype
);
7759 /* Never do unification on the 'this' parameter. */
7760 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
7761 parms
= TREE_CHAIN (parms
);
7765 /* We've been given a return type to match, prepend it. */
7766 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
7767 args
= tree_cons (NULL_TREE
, return_type
, args
);
7772 /* We allow incomplete unification without an error message here
7773 because the standard doesn't seem to explicitly prohibit it. Our
7774 callers must be ready to deal with unification failures in any
7776 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7777 targs
, parms
, args
, /*subr=*/0,
7778 strict
, /*allow_incomplete*/1, len
);
7781 /* All is well so far. Now, check:
7785 When all template arguments have been deduced, all uses of
7786 template parameters in nondeduced contexts are replaced with
7787 the corresponding deduced argument values. If the
7788 substitution results in an invalid type, as described above,
7789 type deduction fails. */
7790 if (tsubst (TREE_TYPE (fn
), targs
, /*complain=*/0, NULL_TREE
)
7797 /* Adjust types before performing type deduction, as described in
7798 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7799 sections are symmetric. PARM is the type of a function parameter
7800 or the return type of the conversion function. ARG is the type of
7801 the argument passed to the call, or the type of the value
7802 initialized with the result of the conversion function. */
7805 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7806 unification_kind_t strict
;
7819 /* Swap PARM and ARG throughout the remainder of this
7820 function; the handling is precisely symmetric since PARM
7821 will initialize ARG rather than vice versa. */
7829 /* There is nothing to do in this case. */
7833 /* DR 214. [temp.func.order] is underspecified, and leads to no
7834 ordering between things like `T *' and `T const &' for `U *'.
7835 The former has T=U and the latter T=U*. The former looks more
7836 specialized and John Spicer considers it well-formed (the EDG
7837 compiler accepts it).
7839 John also confirms that deduction should proceed as in a function
7840 call. Which implies the usual ARG and PARM bashing as DEDUCE_CALL.
7841 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7842 to an actual call can have such a type.
7844 When deducing against a REFERENCE_TYPE, we can either not change
7845 PARM's type, or we can change ARG's type too. The latter, though
7846 seemingly more safe, turns out to give the following quirk. Consider
7847 deducing a call to a `const int *' with the following template
7849 #1; T const *const & ; T = int
7850 #2; T *const & ; T = const int
7851 #3; T * ; T = const int
7852 It looks like #1 is the more specialized. Taken pairwise, #1 is
7853 more specialized than #2 and #2 is more specialized than #3, yet
7854 there is no ordering between #1 and #3.
7856 So, if ARG is a reference, we look though it when PARM is
7857 not a refence. When both are references we don't change either. */
7858 if (TREE_CODE (*arg
) == REFERENCE_TYPE
)
7860 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7862 *arg
= TREE_TYPE (*arg
);
7863 result
|= UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
7868 my_friendly_abort (0);
7871 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7873 /* [temp.deduct.call]
7875 If P is not a reference type:
7877 --If A is an array type, the pointer type produced by the
7878 array-to-pointer standard conversion (_conv.array_) is
7879 used in place of A for type deduction; otherwise,
7881 --If A is a function type, the pointer type produced by
7882 the function-to-pointer standard conversion
7883 (_conv.func_) is used in place of A for type deduction;
7886 --If A is a cv-qualified type, the top level
7887 cv-qualifiers of A's type are ignored for type
7889 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7890 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7891 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7892 *arg
= build_pointer_type (*arg
);
7894 *arg
= TYPE_MAIN_VARIANT (*arg
);
7898 /* [temp.deduct.call]
7900 If P is a cv-qualified type, the top level cv-qualifiers
7901 of P's type are ignored for type deduction. If P is a
7902 reference type, the type referred to by P is used for
7904 *parm
= TYPE_MAIN_VARIANT (*parm
);
7905 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7907 *parm
= TREE_TYPE (*parm
);
7908 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
7913 /* Most parms like fn_type_unification.
7915 If SUBR is 1, we're being called recursively (to unify the
7916 arguments of a function or method parameter of a function
7920 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7921 strict
, allow_incomplete
, len
)
7922 tree tparms
, targs
, parms
, args
;
7924 unification_kind_t strict
;
7925 int allow_incomplete
, len
;
7929 int ntparms
= TREE_VEC_LENGTH (tparms
);
7932 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7933 my_friendly_assert (parms
== NULL_TREE
7934 || TREE_CODE (parms
) == TREE_LIST
, 290);
7935 /* ARGS could be NULL (via a call from parse.y to
7936 build_x_function_call). */
7938 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7939 my_friendly_assert (ntparms
> 0, 292);
7944 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
7945 | UNIFY_ALLOW_DERIVED
);
7949 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7953 sub_strict
= UNIFY_ALLOW_NONE
;
7957 sub_strict
= UNIFY_ALLOW_NONE
;
7961 my_friendly_abort (0);
7968 && parms
!= void_list_node
7970 && args
!= void_list_node
)
7972 parm
= TREE_VALUE (parms
);
7973 parms
= TREE_CHAIN (parms
);
7974 arg
= TREE_VALUE (args
);
7975 args
= TREE_CHAIN (args
);
7977 if (arg
== error_mark_node
)
7979 if (arg
== unknown_type_node
)
7980 /* We can't deduce anything from this, but we might get all the
7981 template args from other function args. */
7984 /* Conversions will be performed on a function argument that
7985 corresponds with a function parameter that contains only
7986 non-deducible template parameters and explicitly specified
7987 template parameters. */
7988 if (! uses_template_parms (parm
))
7993 type
= TREE_TYPE (arg
);
8000 if (strict
== DEDUCE_EXACT
|| strict
== DEDUCE_ORDER
)
8002 if (same_type_p (parm
, type
))
8006 /* It might work; we shouldn't check now, because we might
8007 get into infinite recursion. Overload resolution will
8016 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
8017 if (type_unknown_p (arg
))
8019 /* [temp.deduct.type] A template-argument can be deduced from
8020 a pointer to function or pointer to member function
8021 argument if the set of overloaded functions does not
8022 contain function templates and at most one of a set of
8023 overloaded functions provides a unique match. */
8025 if (resolve_overloaded_unification
8026 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
8031 arg
= TREE_TYPE (arg
);
8035 int arg_strict
= sub_strict
;
8038 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8040 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
8044 /* Are we done with the interesting parms? */
8048 /* Fail if we've reached the end of the parm list, and more args
8049 are present, and the parm list isn't variadic. */
8050 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
8052 /* Fail if parms are left and they don't have default values. */
8054 && parms
!= void_list_node
8055 && TREE_PURPOSE (parms
) == NULL_TREE
)
8058 for (i
= 0; i
< ntparms
; i
++)
8059 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
8061 if (!allow_incomplete
)
8062 error ("incomplete type unification");
8068 /* Subroutine of type_unification_real. Args are like the variables at the
8069 call site. ARG is an overloaded function (or template-id); we try
8070 deducing template args from each of the overloads, and if only one
8071 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8074 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
8076 tree tparms
, targs
, parm
, arg
;
8077 unification_kind_t strict
;
8080 tree tempargs
= copy_node (targs
);
8083 if (TREE_CODE (arg
) == ADDR_EXPR
)
8084 arg
= TREE_OPERAND (arg
, 0);
8086 if (TREE_CODE (arg
) == COMPONENT_REF
)
8087 /* Handle `&x' where `x' is some static or non-static member
8089 arg
= TREE_OPERAND (arg
, 1);
8091 if (TREE_CODE (arg
) == OFFSET_REF
)
8092 arg
= TREE_OPERAND (arg
, 1);
8094 /* Strip baselink information. */
8095 while (TREE_CODE (arg
) == TREE_LIST
)
8096 arg
= TREE_VALUE (arg
);
8098 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
8100 /* If we got some explicit template args, we need to plug them into
8101 the affected templates before we try to unify, in case the
8102 explicit args will completely resolve the templates in question. */
8104 tree expl_subargs
= TREE_OPERAND (arg
, 1);
8105 arg
= TREE_OPERAND (arg
, 0);
8107 for (; arg
; arg
= OVL_NEXT (arg
))
8109 tree fn
= OVL_CURRENT (arg
);
8112 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8115 subargs
= get_bindings_overload (fn
, DECL_TEMPLATE_RESULT (fn
),
8119 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
8121 if (TREE_CODE (elem
) == METHOD_TYPE
)
8122 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
8123 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
8124 strict
, sub_strict
);
8128 else if (TREE_CODE (arg
) == OVERLOAD
)
8130 for (; arg
; arg
= OVL_NEXT (arg
))
8132 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
8133 if (TREE_CODE (type
) == METHOD_TYPE
)
8134 type
= build_ptrmemfunc_type (build_pointer_type (type
));
8135 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
8137 strict
, sub_strict
);
8141 my_friendly_abort (981006);
8143 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8144 to function or pointer to member function argument if the set of
8145 overloaded functions does not contain function templates and at most
8146 one of a set of overloaded functions provides a unique match.
8148 So if we found multiple possibilities, we return success but don't
8153 int i
= TREE_VEC_LENGTH (targs
);
8155 if (TREE_VEC_ELT (tempargs
, i
))
8156 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
8164 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8165 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8166 different overloads deduce different arguments for a given parm.
8167 Returns 1 on success. */
8170 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
8172 tree tparms
, orig_targs
, targs
, parm
, arg
;
8173 unification_kind_t strict
;
8180 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8181 to function or pointer to member function argument if the set of
8182 overloaded functions does not contain function templates and at most
8183 one of a set of overloaded functions provides a unique match.
8185 So if this is a template, just return success. */
8187 if (uses_template_parms (arg
))
8190 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8192 /* We don't copy orig_targs for this because if we have already deduced
8193 some template args from previous args, unify would complain when we
8194 try to deduce a template parameter for the same argument, even though
8195 there isn't really a conflict. */
8196 nargs
= TREE_VEC_LENGTH (targs
);
8197 tempargs
= make_tree_vec (nargs
);
8199 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
8202 /* First make sure we didn't deduce anything that conflicts with
8203 explicitly specified args. */
8204 for (i
= nargs
; i
--; )
8206 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8207 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
8209 if (elt
== NULL_TREE
)
8211 else if (uses_template_parms (elt
))
8213 /* Since we're unifying against ourselves, we will fill in template
8214 args used in the function parm list with our own template parms.
8216 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
8219 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
8223 for (i
= nargs
; i
--; )
8225 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8228 TREE_VEC_ELT (targs
, i
) = elt
;
8234 /* Verify that nondeduce template argument agrees with the type
8235 obtained from argument deduction. Return nonzero if the
8240 struct A { typedef int X; };
8241 template <class T, class U> struct C {};
8242 template <class T> struct C<T, typename T::X> {};
8244 Then with the instantiation `C<A, int>', we can deduce that
8245 `T' is `A' but unify () does not check whether `typename T::X'
8246 is `int'. This function ensure that they agree.
8248 TARGS, PARMS are the same as the arguments of unify.
8249 ARGS contains template arguments from all levels. */
8252 verify_class_unification (targs
, parms
, args
)
8253 tree targs
, parms
, args
;
8256 int nparms
= TREE_VEC_LENGTH (parms
);
8257 tree new_parms
= tsubst (parms
, add_outermost_template_args (args
, targs
),
8258 /*complain=*/0, NULL_TREE
);
8259 if (new_parms
== error_mark_node
)
8262 args
= INNERMOST_TEMPLATE_ARGS (args
);
8264 for (i
= 0; i
< nparms
; i
++)
8266 tree parm
= TREE_VEC_ELT (new_parms
, i
);
8267 tree arg
= TREE_VEC_ELT (args
, i
);
8269 /* In case we are deducing from a function argument of a function
8270 templates, some parameters may not be deduced yet. So we
8271 make sure that only fully substituted elements of PARM are
8274 if (!uses_template_parms (parm
) && !template_args_equal (parm
, arg
))
8280 /* PARM is a template class (perhaps with unbound template
8281 parameters). ARG is a fully instantiated type. If ARG can be
8282 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8283 TARGS are as for unify. */
8286 try_class_unification (tparms
, targs
, parm
, arg
)
8294 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
8295 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
8298 /* We need to make a new template argument vector for the call to
8299 unify. If we used TARGS, we'd clutter it up with the result of
8300 the attempted unification, even if this class didn't work out.
8301 We also don't want to commit ourselves to all the unifications
8302 we've already done, since unification is supposed to be done on
8303 an argument-by-argument basis. In other words, consider the
8304 following pathological case:
8306 template <int I, int J, int K>
8309 template <int I, int J>
8310 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8312 template <int I, int J, int K>
8313 void f(S<I, J, K>, S<I, I, I>);
8322 Now, by the time we consider the unification involving `s2', we
8323 already know that we must have `f<0, 0, 0>'. But, even though
8324 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8325 because there are two ways to unify base classes of S<0, 1, 2>
8326 with S<I, I, I>. If we kept the already deduced knowledge, we
8327 would reject the possibility I=1. */
8328 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
8330 /* If unification failed, we're done. */
8331 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
8332 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
8338 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8339 have already discovered to be satisfactory. ARG_BINFO is the binfo
8340 for the base class of ARG that we are currently examining. */
8343 get_template_base_recursive (tparms
, targs
, parm
,
8344 arg_binfo
, rval
, flags
)
8354 tree arg
= BINFO_TYPE (arg_binfo
);
8356 if (!(flags
& GTB_IGNORE_TYPE
))
8358 tree r
= try_class_unification (tparms
, targs
,
8361 /* If there is more than one satisfactory baseclass, then:
8365 If they yield more than one possible deduced A, the type
8369 if (r
&& rval
&& !same_type_p (r
, rval
))
8370 return error_mark_node
;
8375 binfos
= BINFO_BASETYPES (arg_binfo
);
8376 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
8378 /* Process base types. */
8379 for (i
= 0; i
< n_baselinks
; i
++)
8381 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
8384 /* Skip this base, if we've already seen it. */
8385 if (BINFO_MARKED (base_binfo
))
8389 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
8391 /* When searching for a non-virtual, we cannot mark virtually
8394 SET_BINFO_MARKED (base_binfo
);
8396 rval
= get_template_base_recursive (tparms
, targs
,
8400 GTB_VIA_VIRTUAL
* this_virtual
);
8402 /* If we discovered more than one matching base class, we can
8404 if (rval
== error_mark_node
)
8405 return error_mark_node
;
8411 /* Given a template type PARM and a class type ARG, find the unique
8412 base type in ARG that is an instance of PARM. We do not examine
8413 ARG itself; only its base-classes. If there is no appropriate base
8414 class, return NULL_TREE. If there is more than one, return
8415 error_mark_node. PARM may be the type of a partial specialization,
8416 as well as a plain template type. Used by unify. */
8419 get_template_base (tparms
, targs
, parm
, arg
)
8428 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
8430 arg_binfo
= TYPE_BINFO (complete_type (arg
));
8431 rval
= get_template_base_recursive (tparms
, targs
,
8436 /* Since get_template_base_recursive marks the bases classes, we
8437 must unmark them here. */
8438 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
8443 /* Returns the level of DECL, which declares a template parameter. */
8446 template_decl_level (decl
)
8449 switch (TREE_CODE (decl
))
8453 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
8456 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
8459 my_friendly_abort (0);
8464 /* Decide whether ARG can be unified with PARM, considering only the
8465 cv-qualifiers of each type, given STRICT as documented for unify.
8466 Returns non-zero iff the unification is OK on that basis.*/
8469 check_cv_quals_for_unify (strict
, arg
, parm
)
8474 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
8475 && !at_least_as_qualified_p (arg
, parm
))
8478 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
8479 && !at_least_as_qualified_p (parm
, arg
))
8485 /* Takes parameters as for type_unification. Returns 0 if the
8486 type deduction succeeds, 1 otherwise. The parameter STRICT is a
8487 bitwise or of the following flags:
8490 Require an exact match between PARM and ARG.
8491 UNIFY_ALLOW_MORE_CV_QUAL:
8492 Allow the deduced ARG to be more cv-qualified (by qualification
8493 conversion) than ARG.
8494 UNIFY_ALLOW_LESS_CV_QUAL:
8495 Allow the deduced ARG to be less cv-qualified than ARG.
8496 UNIFY_ALLOW_DERIVED:
8497 Allow the deduced ARG to be a template base class of ARG,
8498 or a pointer to a template base class of the type pointed to by
8500 UNIFY_ALLOW_INTEGER:
8501 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8502 case for more information.
8503 UNIFY_ALLOW_OUTER_LEVEL:
8504 This is the outermost level of a deduction. Used to determine validity
8505 of qualification conversions. A valid qualification conversion must
8506 have const qualified pointers leading up to the inner type which
8507 requires additional CV quals, except at the outer level, where const
8508 is not required [conv.qual]. It would be normal to set this flag in
8509 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8510 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8511 This is the outermost level of a deduction, and PARM can be more CV
8512 qualified at this point.
8513 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8514 This is the outermost level of a deduction, and PARM can be less CV
8515 qualified at this point. */
8518 unify (tparms
, targs
, parm
, arg
, strict
)
8519 tree tparms
, targs
, parm
, arg
;
8525 int strict_in
= strict
;
8527 /* I don't think this will do the right thing with respect to types.
8528 But the only case I've seen it in so far has been array bounds, where
8529 signedness is the only information lost, and I think that will be
8531 while (TREE_CODE (parm
) == NOP_EXPR
)
8532 parm
= TREE_OPERAND (parm
, 0);
8534 if (arg
== error_mark_node
)
8536 if (arg
== unknown_type_node
)
8537 /* We can't deduce anything from this, but we might get all the
8538 template args from other function args. */
8541 /* If PARM uses template parameters, then we can't bail out here,
8542 even if ARG == PARM, since we won't record unifications for the
8543 template parameters. We might need them if we're trying to
8544 figure out which of two things is more specialized. */
8545 if (arg
== parm
&& !uses_template_parms (parm
))
8548 /* Immediately reject some pairs that won't unify because of
8549 cv-qualification mismatches. */
8550 if (TREE_CODE (arg
) == TREE_CODE (parm
)
8552 /* We check the cv-qualifiers when unifying with template type
8553 parameters below. We want to allow ARG `const T' to unify with
8554 PARM `T' for example, when computing which of two templates
8555 is more specialized, for example. */
8556 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
8557 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
8560 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
8561 && TYPE_P (arg
) && !CP_TYPE_CONST_P (arg
))
8562 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
8563 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
8564 strict
&= ~UNIFY_ALLOW_DERIVED
;
8565 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
8566 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
8568 switch (TREE_CODE (parm
))
8571 /* In a type which contains a nested-name-specifier, template
8572 argument values cannot be deduced for template parameters used
8573 within the nested-name-specifier. */
8576 case TEMPLATE_TYPE_PARM
:
8577 case TEMPLATE_TEMPLATE_PARM
:
8578 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8579 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8581 if (TEMPLATE_TYPE_LEVEL (parm
)
8582 != template_decl_level (tparm
))
8583 /* The PARM is not one we're trying to unify. Just check
8584 to see if it matches ARG. */
8585 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8586 && same_type_p (parm
, arg
)) ? 0 : 1;
8587 idx
= TEMPLATE_TYPE_IDX (parm
);
8588 targ
= TREE_VEC_ELT (targs
, idx
);
8589 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
8591 /* Check for mixed types and values. */
8592 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
8593 && TREE_CODE (tparm
) != TYPE_DECL
)
8594 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8595 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8598 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8600 /* ARG must be constructed from a template class or a template
8601 template parameter. */
8602 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
8603 && (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
)))
8607 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8608 tree parmvec
= TYPE_TI_ARGS (parm
);
8609 tree argvec
= TYPE_TI_ARGS (arg
);
8611 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
8614 /* The parameter and argument roles have to be switched here
8615 in order to handle default arguments properly. For example,
8616 template<template <class> class TT> void f(TT<int>)
8617 should be able to accept vector<int> which comes from
8618 template <class T, class Allocator = allocator>
8621 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8625 /* Deduce arguments T, i from TT<T> or TT<i>.
8626 We check each element of PARMVEC and ARGVEC individually
8627 rather than the whole TREE_VEC since they can have
8628 different number of elements. */
8630 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8632 tree t
= TREE_VEC_ELT (parmvec
, i
);
8634 if (unify (tparms
, targs
, t
,
8635 TREE_VEC_ELT (argvec
, i
),
8640 arg
= TYPE_TI_TEMPLATE (arg
);
8642 /* Fall through to deduce template name. */
8645 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8646 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8648 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8650 /* Simple cases: Value already set, does match or doesn't. */
8651 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
8658 /* If PARM is `const T' and ARG is only `int', we don't have
8659 a match unless we are allowing additional qualification.
8660 If ARG is `const int' and PARM is just `T' that's OK;
8661 that binds `const int' to `T'. */
8662 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
8666 /* Consider the case where ARG is `const volatile int' and
8667 PARM is `const T'. Then, T should be `volatile int'. */
8669 cp_build_qualified_type_real (arg
,
8671 & ~CP_TYPE_QUALS (parm
),
8673 if (arg
== error_mark_node
)
8676 /* Simple cases: Value already set, does match or doesn't. */
8677 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8683 /* Make sure that ARG is not a variable-sized array. (Note that
8684 were talking about variable-sized arrays (like `int[n]'),
8685 rather than arrays of unknown size (like `int[]').) We'll
8686 get very confused by such a type since the bound of the array
8687 will not be computable in an instantiation. Besides, such
8688 types are not allowed in ISO C++, so we can do as we please
8690 if (TREE_CODE (arg
) == ARRAY_TYPE
8691 && !uses_template_parms (arg
)
8692 && TYPE_DOMAIN (arg
)
8693 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8697 TREE_VEC_ELT (targs
, idx
) = arg
;
8700 case TEMPLATE_PARM_INDEX
:
8701 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8703 if (TEMPLATE_PARM_LEVEL (parm
)
8704 != template_decl_level (tparm
))
8705 /* The PARM is not one we're trying to unify. Just check
8706 to see if it matches ARG. */
8707 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8708 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8710 idx
= TEMPLATE_PARM_IDX (parm
);
8711 targ
= TREE_VEC_ELT (targs
, idx
);
8715 int i
= (cp_tree_equal (targ
, arg
) > 0);
8721 my_friendly_abort (42);
8724 /* [temp.deduct.type] If, in the declaration of a function template
8725 with a non-type template-parameter, the non-type
8726 template-parameter is used in an expression in the function
8727 parameter-list and, if the corresponding template-argument is
8728 deduced, the template-argument type shall match the type of the
8729 template-parameter exactly, except that a template-argument
8730 deduced from an array bound may be of any integral type.
8731 The non-type parameter might use already deduced type parameters. */
8732 if (same_type_p (TREE_TYPE (arg
),
8733 tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
)))
8735 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8736 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8737 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8742 TREE_VEC_ELT (targs
, idx
) = arg
;
8747 if (TREE_CODE (arg
) != POINTER_TYPE
)
8750 /* [temp.deduct.call]
8752 A can be another pointer or pointer to member type that can
8753 be converted to the deduced A via a qualification
8754 conversion (_conv.qual_).
8756 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8757 This will allow for additional cv-qualification of the
8758 pointed-to types if appropriate. */
8760 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
8761 /* The derived-to-base conversion only persists through one
8762 level of pointers. */
8763 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
8765 return unify (tparms
, targs
, TREE_TYPE (parm
),
8766 TREE_TYPE (arg
), strict
);
8769 case REFERENCE_TYPE
:
8770 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8772 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8773 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
8776 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8778 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8779 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8781 if (TYPE_DOMAIN (parm
) != NULL_TREE
8782 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8783 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8785 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8794 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8797 if (TREE_CODE (parm
) == INTEGER_TYPE
8798 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8800 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8801 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8802 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8804 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8805 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8806 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8809 /* We have already checked cv-qualification at the top of the
8811 else if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
8814 /* As far as unification is concerned, this wins. Later checks
8815 will invalidate it if necessary. */
8818 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8819 /* Type INTEGER_CST can come from ordinary constant template args. */
8821 while (TREE_CODE (arg
) == NOP_EXPR
)
8822 arg
= TREE_OPERAND (arg
, 0);
8824 if (TREE_CODE (arg
) != INTEGER_CST
)
8826 return !tree_int_cst_equal (parm
, arg
);
8831 if (TREE_CODE (arg
) != TREE_VEC
)
8833 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8835 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
8836 if (unify (tparms
, targs
,
8837 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8845 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8848 if (TYPE_PTRMEMFUNC_P (parm
))
8850 if (!TYPE_PTRMEMFUNC_P (arg
))
8853 return unify (tparms
, targs
,
8854 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8855 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
8859 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8863 if (strict_in
& UNIFY_ALLOW_DERIVED
)
8865 /* First, we try to unify the PARM and ARG directly. */
8866 t
= try_class_unification (tparms
, targs
,
8871 /* Fallback to the special case allowed in
8874 If P is a class, and P has the form
8875 template-id, then A can be a derived class of
8876 the deduced A. Likewise, if P is a pointer to
8877 a class of the form template-id, A can be a
8878 pointer to a derived class pointed to by the
8880 t
= get_template_base (tparms
, targs
,
8883 if (! t
|| t
== error_mark_node
)
8887 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8888 && (CLASSTYPE_TI_TEMPLATE (parm
)
8889 == CLASSTYPE_TI_TEMPLATE (arg
)))
8890 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8891 Then, we should unify `int' and `U'. */
8894 /* There's no chance of unification succeeding. */
8897 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8898 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8900 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
8906 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8909 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8910 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8912 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8913 TYPE_ARG_TYPES (arg
), 1,
8914 DEDUCE_EXACT
, 0, -1);
8917 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8919 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8920 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8922 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8926 if (arg
!= decl_constant_value (parm
))
8931 /* Matched cases are handled by the ARG == PARM test above. */
8935 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8937 /* We handle this case specially, since it comes up with
8938 arrays. In particular, something like:
8940 template <int N> void f(int (&x)[N]);
8942 Here, we are trying to unify the range type, which
8943 looks like [0 ... (N - 1)]. */
8945 t1
= TREE_OPERAND (parm
, 0);
8946 t2
= TREE_OPERAND (parm
, 1);
8948 t
= fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
));
8950 return unify (tparms
, targs
, t1
, t
, strict
);
8952 /* else fall through */
8955 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8958 /* We're looking at an expression. This can happen with
8962 void foo(S<I>, S<I + 2>);
8964 This is a "nondeduced context":
8968 The nondeduced contexts are:
8970 --A type that is a template-id in which one or more of
8971 the template-arguments is an expression that references
8972 a template-parameter.
8974 In these cases, we assume deduction succeeded, but don't
8975 actually infer any unifications. */
8977 if (!uses_template_parms (parm
)
8978 && !template_args_equal (parm
, arg
))
8984 sorry ("use of `%s' in template type unification",
8985 tree_code_name
[(int) TREE_CODE (parm
)]);
8991 /* Called if RESULT is explicitly instantiated, or is a member of an
8992 explicitly instantiated class, or if using -frepo and the
8993 instantiation of RESULT has been assigned to this file. */
8996 mark_decl_instantiated (result
, extern_p
)
9000 if (TREE_CODE (result
) != FUNCTION_DECL
)
9001 /* The TREE_PUBLIC flag for function declarations will have been
9002 set correctly by tsubst. */
9003 TREE_PUBLIC (result
) = 1;
9005 /* We used to set this unconditionally; we moved that to
9006 do_decl_instantiation so it wouldn't get set on members of
9007 explicit class template instantiations. But we still need to set
9008 it here for the 'extern template' case in order to suppress
9009 implicit instantiations. */
9011 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9015 DECL_INTERFACE_KNOWN (result
) = 1;
9016 DECL_NOT_REALLY_EXTERN (result
) = 1;
9018 /* Always make artificials weak. */
9019 if (DECL_ARTIFICIAL (result
) && flag_weak
)
9020 comdat_linkage (result
);
9021 /* For WIN32 we also want to put explicit instantiations in
9022 linkonce sections. */
9023 else if (TREE_PUBLIC (result
))
9024 maybe_make_one_only (result
);
9026 else if (TREE_CODE (result
) == FUNCTION_DECL
)
9030 /* Given two function templates PAT1 and PAT2, return:
9032 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9034 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9035 -1 if PAT2 is more specialized than PAT1.
9036 0 if neither is more specialized.
9038 LEN is passed through to fn_type_unification. */
9041 more_specialized (pat1
, pat2
, deduce
, len
)
9049 targs
= get_bindings_real (pat1
, DECL_TEMPLATE_RESULT (pat2
),
9050 NULL_TREE
, 0, deduce
, len
);
9054 targs
= get_bindings_real (pat2
, DECL_TEMPLATE_RESULT (pat1
),
9055 NULL_TREE
, 0, deduce
, len
);
9062 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9064 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9065 -1 if PAT2 is more specialized than PAT1.
9066 0 if neither is more specialized. */
9069 more_specialized_class (pat1
, pat2
)
9075 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
9076 TREE_PURPOSE (pat2
));
9080 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
9081 TREE_PURPOSE (pat1
));
9088 /* Return the template arguments that will produce the function signature
9089 DECL from the function template FN, with the explicit template
9090 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9091 also match. Return NULL_TREE if no satisfactory arguments could be
9092 found. DEDUCE and LEN are passed through to fn_type_unification. */
9095 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
, deduce
, len
)
9096 tree fn
, decl
, explicit_args
;
9097 int check_rettype
, deduce
, len
;
9099 int ntparms
= DECL_NTPARMS (fn
);
9100 tree targs
= make_tree_vec (ntparms
);
9102 tree decl_arg_types
;
9105 /* Substitute the explicit template arguments into the type of DECL.
9106 The call to fn_type_unification will handle substitution into the
9108 decl_type
= TREE_TYPE (decl
);
9109 if (explicit_args
&& uses_template_parms (decl_type
))
9112 tree converted_args
;
9114 if (DECL_TEMPLATE_INFO (decl
))
9115 tmpl
= DECL_TI_TEMPLATE (decl
);
9117 /* We can get here for some illegal specializations. */
9121 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
9122 explicit_args
, NULL_TREE
,
9124 /*require_all_arguments=*/0));
9125 if (converted_args
== error_mark_node
)
9128 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
9130 if (decl_type
== error_mark_node
)
9134 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
9135 /* Never do unification on the 'this' parameter. */
9136 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
9137 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
9139 i
= fn_type_unification (fn
, explicit_args
, targs
,
9141 (check_rettype
|| DECL_CONV_FN_P (fn
)
9142 ? TREE_TYPE (decl_type
) : NULL_TREE
),
9151 /* For most uses, we want to check the return type. */
9154 get_bindings (fn
, decl
, explicit_args
)
9155 tree fn
, decl
, explicit_args
;
9157 return get_bindings_real (fn
, decl
, explicit_args
, 1, DEDUCE_EXACT
, -1);
9160 /* But for resolve_overloaded_unification, we only care about the parameter
9164 get_bindings_overload (fn
, decl
, explicit_args
)
9165 tree fn
, decl
, explicit_args
;
9167 return get_bindings_real (fn
, decl
, explicit_args
, 0, DEDUCE_EXACT
, -1);
9170 /* Return the innermost template arguments that, when applied to a
9171 template specialization whose innermost template parameters are
9172 TPARMS, and whose specialization arguments are ARGS, yield the
9175 For example, suppose we have:
9177 template <class T, class U> struct S {};
9178 template <class T> struct S<T*, int> {};
9180 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9181 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9182 int}. The resulting vector will be {double}, indicating that `T'
9183 is bound to `double'. */
9186 get_class_bindings (tparms
, parms
, args
)
9187 tree tparms
, parms
, args
;
9189 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
9190 tree vec
= make_tree_vec (ntparms
);
9192 if (unify (tparms
, vec
, parms
, INNERMOST_TEMPLATE_ARGS (args
),
9196 for (i
= 0; i
< ntparms
; ++i
)
9197 if (! TREE_VEC_ELT (vec
, i
))
9200 if (verify_class_unification (vec
, parms
, args
))
9206 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9207 Pick the most specialized template, and return the corresponding
9208 instantiation, or if there is no corresponding instantiation, the
9209 template itself. If there is no most specialized template,
9210 error_mark_node is returned. If there are no templates at all,
9211 NULL_TREE is returned. */
9214 most_specialized_instantiation (instantiations
)
9215 tree instantiations
;
9220 if (!instantiations
)
9223 champ
= instantiations
;
9224 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
9226 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
9234 fn
= TREE_CHAIN (fn
);
9236 return error_mark_node
;
9242 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
9244 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
9247 return error_mark_node
;
9250 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
9253 /* Return the most specialized of the list of templates in FNS that can
9254 produce an instantiation matching DECL, given the explicit template
9255 arguments EXPLICIT_ARGS. */
9258 most_specialized (fns
, decl
, explicit_args
)
9259 tree fns
, decl
, explicit_args
;
9261 tree candidates
= NULL_TREE
;
9264 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
9266 tree candidate
= TREE_VALUE (fn
);
9268 args
= get_bindings (candidate
, decl
, explicit_args
);
9270 candidates
= tree_cons (NULL_TREE
, candidate
, candidates
);
9273 return most_specialized_instantiation (candidates
);
9276 /* If DECL is a specialization of some template, return the most
9277 general such template. Otherwise, returns NULL_TREE.
9281 template <class T> struct S { template <class U> void f(U); };
9283 if TMPL is `template <class U> void S<int>::f(U)' this will return
9284 the full template. This function will not trace past partial
9285 specializations, however. For example, given in addition:
9287 template <class T> struct S<T*> { template <class U> void f(U); };
9289 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9290 `template <class T> template <class U> S<T*>::f(U)'. */
9293 most_general_template (decl
)
9296 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9297 an immediate specialization. */
9298 if (TREE_CODE (decl
) == FUNCTION_DECL
)
9300 if (DECL_TEMPLATE_INFO (decl
)) {
9301 decl
= DECL_TI_TEMPLATE (decl
);
9303 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9305 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
9311 /* Look for more and more general templates. */
9312 while (DECL_TEMPLATE_INFO (decl
))
9314 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9315 in some cases. (See cp-tree.h for details.) */
9316 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9319 /* Stop if we run into an explicitly specialized class template. */
9320 if (!DECL_NAMESPACE_SCOPE_P (decl
)
9321 && DECL_CONTEXT (decl
)
9322 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
9325 decl
= DECL_TI_TEMPLATE (decl
);
9331 /* Return the most specialized of the class template specializations
9332 of TMPL which can produce an instantiation matching ARGS, or
9333 error_mark_node if the choice is ambiguous. */
9336 most_specialized_class (tmpl
, args
)
9340 tree list
= NULL_TREE
;
9345 tmpl
= most_general_template (tmpl
);
9346 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
9349 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
9352 list
= tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
9353 TREE_TYPE (list
) = TREE_TYPE (t
);
9363 for (; t
; t
= TREE_CHAIN (t
))
9365 fate
= more_specialized_class (champ
, t
);
9374 return error_mark_node
;
9380 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
9382 fate
= more_specialized_class (champ
, t
);
9384 return error_mark_node
;
9390 /* called from the parser. */
9393 do_decl_instantiation (declspecs
, declarator
, storage
)
9394 tree declspecs
, declarator
, storage
;
9396 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
9397 tree result
= NULL_TREE
;
9401 /* An error occurred, for which grokdeclarator has already issued
9402 an appropriate message. */
9404 else if (! DECL_LANG_SPECIFIC (decl
))
9406 cp_error ("explicit instantiation of non-template `%#D'", decl
);
9409 else if (TREE_CODE (decl
) == VAR_DECL
)
9411 /* There is an asymmetry here in the way VAR_DECLs and
9412 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9413 the latter, the DECL we get back will be marked as a
9414 template instantiation, and the appropriate
9415 DECL_TEMPLATE_INFO will be set up. This does not happen for
9416 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9417 should handle VAR_DECLs as it currently handles
9419 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
9420 if (result
&& TREE_CODE (result
) != VAR_DECL
)
9422 cp_error ("no matching template for `%D' found", result
);
9426 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
9428 cp_error ("explicit instantiation of `%#D'", decl
);
9434 /* Check for various error cases. Note that if the explicit
9435 instantiation is legal the RESULT will currently be marked as an
9436 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9437 until we get here. */
9439 if (DECL_TEMPLATE_SPECIALIZATION (result
))
9443 No program shall both explicitly instantiate and explicitly
9444 specialize a template. */
9445 cp_pedwarn ("explicit instantiation of `%#D' after", result
);
9446 cp_pedwarn_at ("explicit specialization here", result
);
9449 else if (DECL_EXPLICIT_INSTANTIATION (result
))
9453 No program shall explicitly instantiate any template more
9456 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9457 instantiation was `extern' and the second is not, and EXTERN_P for
9458 the opposite case. If -frepo, chances are we already got marked
9459 as an explicit instantiation because of the repo file. */
9460 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
9461 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result
);
9463 /* If we've already instantiated the template, just return now. */
9464 if (DECL_INTERFACE_KNOWN (result
))
9467 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
9469 cp_error ("no matching template for `%D' found", result
);
9472 else if (!DECL_TEMPLATE_INFO (result
))
9474 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
9478 if (flag_external_templates
)
9481 if (storage
== NULL_TREE
)
9483 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9486 cp_pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
9490 cp_error ("storage class `%D' applied to template instantiation",
9493 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9494 mark_decl_instantiated (result
, extern_p
);
9495 repo_template_instantiated (result
, extern_p
);
9497 instantiate_decl (result
, /*defer_ok=*/1);
9501 mark_class_instantiated (t
, extern_p
)
9505 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
9506 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
9507 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
9508 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
9509 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
9512 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
9513 rest_of_type_compilation (t
, 1);
9517 /* Perform an explicit instantiation of template class T. STORAGE, if
9518 non-null, is the RID for extern, inline or static. COMPLAIN is
9519 non-zero if this is called from the parser, zero if called recursively,
9520 since the standard is unclear (as detailed below). */
9523 do_type_instantiation (t
, storage
, complain
)
9531 if (TREE_CODE (t
) == TYPE_DECL
)
9534 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
9536 cp_error ("explicit instantiation of non-template type `%T'", t
);
9542 /* With -fexternal-templates, explicit instantiations are treated the same
9543 as implicit ones. */
9544 if (flag_external_templates
)
9547 if (!COMPLETE_TYPE_P (t
))
9550 cp_error ("explicit instantiation of `%#T' before definition of template",
9555 if (storage
!= NULL_TREE
)
9558 cp_pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
9559 IDENTIFIER_POINTER (storage
));
9561 if (storage
== ridpointers
[(int) RID_INLINE
])
9563 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9565 else if (storage
== ridpointers
[(int) RID_STATIC
])
9569 cp_error ("storage class `%D' applied to template instantiation",
9575 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
9579 No program shall both explicitly instantiate and explicitly
9580 specialize a template. */
9583 cp_error ("explicit instantiation of `%#T' after", t
);
9584 cp_error_at ("explicit specialization here", t
);
9588 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
9592 No program shall explicitly instantiate any template more
9595 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9596 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9597 are we already got marked as an explicit instantion because of the
9598 repo file. All these cases are OK. */
9599 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
9601 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t
);
9603 /* If we've already instantiated the template, just return now. */
9604 if (!CLASSTYPE_INTERFACE_ONLY (t
))
9608 mark_class_instantiated (t
, extern_p
);
9609 repo_template_instantiated (t
, extern_p
);
9617 /* In contrast to implicit instantiation, where only the
9618 declarations, and not the definitions, of members are
9619 instantiated, we have here:
9623 The explicit instantiation of a class template specialization
9624 implies the instantiation of all of its members not
9625 previously explicitly specialized in the translation unit
9626 containing the explicit instantiation.
9628 Of course, we can't instantiate member template classes, since
9629 we don't have any arguments for them. Note that the standard
9630 is unclear on whether the instantiation of the members are
9631 *explicit* instantiations or not. We choose to be generous,
9632 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9633 the explicit instantiation of a class where some of the members
9634 have no definition in the current translation unit. */
9637 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9638 if (TREE_CODE (tmp
) == FUNCTION_DECL
9639 && DECL_TEMPLATE_INSTANTIATION (tmp
))
9641 mark_decl_instantiated (tmp
, extern_p
);
9642 repo_template_instantiated (tmp
, extern_p
);
9644 instantiate_decl (tmp
, /*defer_ok=*/1);
9647 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9648 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
9650 mark_decl_instantiated (tmp
, extern_p
);
9651 repo_template_instantiated (tmp
, extern_p
);
9653 instantiate_decl (tmp
, /*defer_ok=*/1);
9656 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9657 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
9658 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
9659 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
, 0);
9663 /* Given a function DECL, which is a specialization of TMPL, modify
9664 DECL to be a re-instantiation of TMPL with the same template
9665 arguments. TMPL should be the template into which tsubst'ing
9666 should occur for DECL, not the most general template.
9668 One reason for doing this is a scenario like this:
9671 void f(const T&, int i);
9673 void g() { f(3, 7); }
9676 void f(const T& t, const int i) { }
9678 Note that when the template is first instantiated, with
9679 instantiate_template, the resulting DECL will have no name for the
9680 first parameter, and the wrong type for the second. So, when we go
9681 to instantiate the DECL, we regenerate it. */
9684 regenerate_decl_from_template (decl
, tmpl
)
9688 /* The most general version of TMPL. */
9690 /* The arguments used to instantiate DECL, from the most general
9697 args
= DECL_TI_ARGS (decl
);
9698 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9700 /* Unregister the specialization so that when we tsubst we will not
9701 just return DECL. We don't have to unregister DECL from TMPL
9702 because if would only be registered there if it were a partial
9703 instantiation of a specialization, which it isn't: it's a full
9705 gen_tmpl
= most_general_template (tmpl
);
9706 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9708 /* If the DECL was not unregistered then something peculiar is
9709 happening: we created a specialization but did not call
9710 register_specialization for it. */
9711 my_friendly_assert (unregistered
, 0);
9713 if (TREE_CODE (decl
) == VAR_DECL
)
9714 /* Make sure that we can see identifiers, and compute access
9715 correctly, for the class members used in the declaration of
9716 this static variable. */
9717 pushclass (DECL_CONTEXT (decl
), 2);
9719 /* Do the substitution to get the new declaration. */
9720 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9722 if (TREE_CODE (decl
) == VAR_DECL
)
9724 /* Set up DECL_INITIAL, since tsubst doesn't. */
9725 DECL_INITIAL (new_decl
) =
9726 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9727 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9728 /* Pop the class context we pushed above. */
9731 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9733 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9735 DECL_INITIAL (new_decl
) = error_mark_node
;
9736 /* And don't complain about a duplicate definition. */
9737 DECL_INITIAL (decl
) = NULL_TREE
;
9740 /* The immediate parent of the new template is still whatever it was
9741 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9742 general template. We also reset the DECL_ASSEMBLER_NAME since
9743 tsubst always calculates the name as if the function in question
9744 were really a template instance, and sometimes, with friend
9745 functions, this is not so. See tsubst_friend_function for
9747 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9748 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9749 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9750 DECL_USE_TEMPLATE (new_decl
) = DECL_USE_TEMPLATE (decl
);
9752 /* Call duplicate decls to merge the old and new declarations. */
9753 duplicate_decls (new_decl
, decl
);
9755 /* Now, re-register the specialization. */
9756 register_specialization (decl
, gen_tmpl
, args
);
9759 /* Produce the definition of D, a _DECL generated from a template. If
9760 DEFER_OK is non-zero, then we don't have to actually do the
9761 instantiation now; we just have to do it sometime. */
9764 instantiate_decl (d
, defer_ok
)
9768 tree tmpl
= DECL_TI_TEMPLATE (d
);
9769 tree args
= DECL_TI_ARGS (d
);
9774 int pattern_defined
;
9777 const char *file
= input_filename
;
9779 /* This function should only be used to instantiate templates for
9780 functions and static member variables. */
9781 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9782 || TREE_CODE (d
) == VAR_DECL
, 0);
9784 /* Don't instantiate cloned functions. Instead, instantiate the
9785 functions they cloned. */
9786 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
9787 d
= DECL_CLONED_FUNCTION (d
);
9789 if (DECL_TEMPLATE_INSTANTIATED (d
))
9790 /* D has already been instantiated. It might seem reasonable to
9791 check whether or not D is an explicit instantiation, and, if so,
9792 stop here. But when an explicit instantiation is deferred
9793 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9794 is set, even though we still need to do the instantiation. */
9797 /* If we already have a specialization of this declaration, then
9798 there's no reason to instantiate it. Note that
9799 retrieve_specialization gives us both instantiations and
9800 specializations, so we must explicitly check
9801 DECL_TEMPLATE_SPECIALIZATION. */
9802 gen_tmpl
= most_general_template (tmpl
);
9803 spec
= retrieve_specialization (gen_tmpl
, args
);
9804 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9807 /* This needs to happen before any tsubsting. */
9808 if (! push_tinst_level (d
))
9811 timevar_push (TV_PARSE
);
9813 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9814 for the instantiation. This is not always the most general
9815 template. Consider, for example:
9818 struct S { template <class U> void f();
9819 template <> void f<int>(); };
9821 and an instantiation of S<double>::f<int>. We want TD to be the
9822 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9824 while (/* An instantiation cannot have a definition, so we need a
9825 more general template. */
9826 DECL_TEMPLATE_INSTANTIATION (td
)
9827 /* We must also deal with friend templates. Given:
9829 template <class T> struct S {
9830 template <class U> friend void f() {};
9833 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9834 so far as the language is concerned, but that's still
9835 where we get the pattern for the instantiation from. On
9836 other hand, if the definition comes outside the class, say:
9838 template <class T> struct S {
9839 template <class U> friend void f();
9841 template <class U> friend void f() {}
9843 we don't need to look any further. That's what the check for
9844 DECL_INITIAL is for. */
9845 || (TREE_CODE (d
) == FUNCTION_DECL
9846 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9847 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
))))
9849 /* The present template, TD, should not be a definition. If it
9850 were a definition, we should be using it! Note that we
9851 cannot restructure the loop to just keep going until we find
9852 a template with a definition, since that might go too far if
9853 a specialization was declared, but not defined. */
9854 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9855 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9858 /* Fetch the more general template. */
9859 td
= DECL_TI_TEMPLATE (td
);
9862 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9864 if (TREE_CODE (d
) == FUNCTION_DECL
)
9865 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
9867 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9869 lineno
= DECL_SOURCE_LINE (d
);
9870 input_filename
= DECL_SOURCE_FILE (d
);
9872 if (pattern_defined
)
9874 repo_template_used (d
);
9876 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9878 if (flag_alt_external_templates
)
9880 if (interface_unknown
)
9881 warn_if_unknown_interface (d
);
9883 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9885 DECL_INTERFACE_KNOWN (d
) = 1;
9886 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9889 warn_if_unknown_interface (code_pattern
);
9893 import_export_decl (d
);
9896 /* We need to set up DECL_INITIAL regardless, if
9897 the variable is initialized in the class body. */
9898 if (TREE_CODE (d
) == VAR_DECL
&& DECL_INITIALIZED_IN_CLASS_P (d
))
9900 /* Reject all external templates except inline functions. */
9901 else if (DECL_INTERFACE_KNOWN (d
)
9902 && ! DECL_NOT_REALLY_EXTERN (d
)
9903 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9905 /* Defer all other templates, unless we have been explicitly
9906 forbidden from doing so. We restore the source position here
9907 because it's used by add_pending_template. */
9908 else if (! pattern_defined
|| defer_ok
)
9911 input_filename
= file
;
9913 if (at_eof
&& !pattern_defined
9914 && DECL_EXPLICIT_INSTANTIATION (d
))
9917 The definition of a non-exported function template, a
9918 non-exported member function template, or a non-exported
9919 member function or static data member of a class template
9920 shall be present in every translation unit in which it is
9921 explicitly instantiated. */
9922 cp_error ("explicit instantiation of `%D' but no definition available",
9925 add_pending_template (d
);
9929 need_push
= !global_bindings_p ();
9931 push_to_top_level ();
9933 /* We're now committed to instantiating this template. Mark it as
9934 instantiated so that recursive calls to instantiate_decl do not
9935 try to instantiate it again. */
9936 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9938 /* Regenerate the declaration in case the template has been modified
9939 by a subsequent redeclaration. */
9940 regenerate_decl_from_template (d
, td
);
9942 /* We already set the file and line above. Reset them now in case
9943 they changed as a result of calling regenerate_decl_from_template. */
9944 lineno
= DECL_SOURCE_LINE (d
);
9945 input_filename
= DECL_SOURCE_FILE (d
);
9947 if (TREE_CODE (d
) == VAR_DECL
)
9949 DECL_IN_AGGR_P (d
) = 0;
9950 if (DECL_INTERFACE_KNOWN (d
))
9951 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9954 DECL_EXTERNAL (d
) = 1;
9955 DECL_NOT_REALLY_EXTERN (d
) = 1;
9957 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0);
9959 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9961 htab_t saved_local_specializations
;
9963 /* Save away the current list, in case we are instantiating one
9964 template from within the body of another. */
9965 saved_local_specializations
= local_specializations
;
9967 /* Set up the list of local specializations. */
9968 local_specializations
= htab_create (37,
9973 /* Set up context. */
9974 start_function (NULL_TREE
, d
, NULL_TREE
, SF_PRE_PARSED
);
9976 /* We already set up __FUNCTION__, etc., so we don't want to do
9978 function_name_declared_p
= 1;
9980 /* Substitute into the body of the function. */
9981 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
9982 /*complain=*/1, tmpl
);
9984 /* We don't need the local specializations any more. */
9985 htab_delete (local_specializations
);
9986 local_specializations
= saved_local_specializations
;
9988 /* Finish the function. */
9989 expand_body (finish_function (0));
9992 /* We're not deferring instantiation any more. */
9993 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
9996 pop_from_top_level ();
10000 input_filename
= file
;
10002 pop_tinst_level ();
10004 timevar_pop (TV_PARSE
);
10009 /* Run through the list of templates that we wish we could
10010 instantiate, and instantiate any we can. */
10013 instantiate_pending_templates ()
10016 int instantiated_something
= 0;
10023 t
= &pending_templates
;
10026 tree instantiation
= TREE_VALUE (*t
);
10028 reopen_tinst_level (TREE_PURPOSE (*t
));
10030 if (TYPE_P (instantiation
))
10034 if (!COMPLETE_TYPE_P (instantiation
))
10036 instantiate_class_template (instantiation
);
10037 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
10038 for (fn
= TYPE_METHODS (instantiation
);
10040 fn
= TREE_CHAIN (fn
))
10041 if (! DECL_ARTIFICIAL (fn
))
10042 instantiate_decl (fn
, /*defer_ok=*/0);
10043 if (COMPLETE_TYPE_P (instantiation
))
10045 instantiated_something
= 1;
10050 if (COMPLETE_TYPE_P (instantiation
))
10051 /* If INSTANTIATION has been instantiated, then we don't
10052 need to consider it again in the future. */
10053 *t
= TREE_CHAIN (*t
);
10055 t
= &TREE_CHAIN (*t
);
10059 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
10060 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
10062 instantiation
= instantiate_decl (instantiation
,
10064 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
10066 instantiated_something
= 1;
10071 if (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
10072 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
10073 /* If INSTANTIATION has been instantiated, then we don't
10074 need to consider it again in the future. */
10075 *t
= TREE_CHAIN (*t
);
10077 t
= &TREE_CHAIN (*t
);
10080 current_tinst_level
= NULL_TREE
;
10084 /* Go through the things that are template instantiations if we are
10085 using guiding declarations. */
10086 t
= &maybe_templates
;
10093 fn
= TREE_VALUE (*t
);
10095 if (DECL_INITIAL (fn
))
10096 /* If the FN is already defined, then it was either already
10097 instantiated or, even though guiding declarations were
10098 allowed, a non-template definition was provided. */
10102 template = TREE_PURPOSE (*t
);
10103 args
= get_bindings (template, fn
, NULL_TREE
);
10104 fn
= instantiate_template (template, args
);
10105 instantiate_decl (fn
, /*defer_ok=*/0);
10109 /* Remove this entry from the chain. */
10110 *t
= TREE_CHAIN (*t
);
10112 maybe_template_tail
= t
;
10114 while (reconsider
);
10116 return instantiated_something
;
10119 /* Substitute ARGVEC into T, which is a list of initializers for
10120 either base class or a non-static data member. The TREE_PURPOSEs
10121 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10122 instantiate_decl. */
10125 tsubst_initializer_list (t
, argvec
)
10128 tree first
= NULL_TREE
;
10131 for (; t
; t
= TREE_CHAIN (t
))
10137 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
, /*complain=*/1,
10139 init
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
10144 else if (TREE_CODE (init
) == TREE_LIST
)
10145 for (val
= init
; val
; val
= TREE_CHAIN (val
))
10146 TREE_VALUE (val
) = convert_from_reference (TREE_VALUE (val
));
10148 init
= convert_from_reference (init
);
10150 *p
= build_tree_list (decl
, init
);
10151 p
= &TREE_CHAIN (*p
);
10156 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10159 set_current_access_from_decl (decl
)
10162 if (TREE_PRIVATE (decl
))
10163 current_access_specifier
= access_private_node
;
10164 else if (TREE_PROTECTED (decl
))
10165 current_access_specifier
= access_protected_node
;
10167 current_access_specifier
= access_public_node
;
10170 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
10171 is the instantiation (which should have been created with
10172 start_enum) and ARGS are the template arguments to use. */
10175 tsubst_enum (tag
, newtag
, args
)
10182 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
10186 /* Note that in a template enum, the TREE_VALUE is the
10187 CONST_DECL, not the corresponding INTEGER_CST. */
10188 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
10189 args
, /*complain=*/1,
10192 /* Give this enumeration constant the correct access. */
10193 set_current_access_from_decl (TREE_VALUE (e
));
10195 /* Actually build the enumerator itself. */
10196 build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
10199 finish_enum (newtag
);
10200 DECL_SOURCE_LINE (TYPE_NAME (newtag
)) = DECL_SOURCE_LINE (TYPE_NAME (tag
));
10201 DECL_SOURCE_FILE (TYPE_NAME (newtag
)) = DECL_SOURCE_FILE (TYPE_NAME (tag
));
10204 /* DECL is a FUNCTION_DECL that is a template specialization. Return
10205 its type -- but without substituting the innermost set of template
10206 arguments. So, innermost set of template parameters will appear in
10207 the type. If CONTEXTP is non-NULL, then the partially substituted
10208 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10209 will be filled in with the substituted template parameters, if it
10213 get_mostly_instantiated_function_type (decl
, contextp
, tparmsp
)
10218 tree context
= NULL_TREE
;
10225 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10226 targs
= DECL_TI_ARGS (decl
);
10227 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
10228 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
10230 /* There should be as many levels of arguments as there are levels
10232 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
10234 fn_type
= TREE_TYPE (tmpl
);
10235 if (DECL_STATIC_FUNCTION_P (decl
))
10236 context
= DECL_CONTEXT (decl
);
10238 if (parm_depth
== 1)
10239 /* No substitution is necessary. */
10246 /* Replace the innermost level of the TARGS with NULL_TREEs to
10247 let tsubst know not to substitute for those parameters. */
10248 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
10249 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
10250 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
10251 TMPL_ARGS_LEVEL (targs
, i
));
10252 SET_TMPL_ARGS_LEVEL (partial_args
,
10253 TMPL_ARGS_DEPTH (targs
),
10254 make_tree_vec (DECL_NTPARMS (tmpl
)));
10256 /* Now, do the (partial) substitution to figure out the
10257 appropriate function type. */
10258 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
10259 if (DECL_STATIC_FUNCTION_P (decl
))
10260 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
10262 /* Substitute into the template parameters to obtain the real
10263 innermost set of parameters. This step is important if the
10264 innermost set of template parameters contains value
10265 parameters whose types depend on outer template parameters. */
10266 TREE_VEC_LENGTH (partial_args
)--;
10267 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
10271 *contextp
= context
;
10278 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
10279 is either an instantiation or specialization of a template
10283 set_mangled_name_for_template_decl (decl
)
10286 tree context
= NULL_TREE
;
10293 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
10294 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
10296 /* Under the new ABI, we don't need special machinery. */
10299 set_mangled_name_for_decl (decl
);
10303 /* The names of template functions must be mangled so as to indicate
10304 what template is being specialized with what template arguments.
10305 For example, each of the following three functions must get
10306 different mangled names:
10309 template <> void f<7>(int);
10310 template <> void f<8>(int); */
10312 targs
= DECL_TI_ARGS (decl
);
10313 if (uses_template_parms (targs
))
10314 /* This DECL is for a partial instantiation. There's no need to
10315 mangle the name of such an entity. */
10318 /* We now compute the PARMS and RET_TYPE to give to
10319 build_decl_overload_real. The PARMS and RET_TYPE are the
10320 parameter and return types of the template, after all but the
10321 innermost template arguments have been substituted, not the
10322 parameter and return types of the function DECL. For example,
10325 template <class T> T f(T);
10327 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10328 A more subtle example is:
10330 template <class T> struct S { template <class U> void f(T, U); }
10332 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10333 {int, U}. Thus, the args that we want to subsitute into the
10334 return and parameter type for the function are those in TARGS,
10335 with the innermost level omitted. */
10336 fn_type
= get_mostly_instantiated_function_type (decl
, &context
, &tparms
);
10338 /* Now, get the innermost parameters and arguments, and figure out
10339 the parameter and return types. */
10340 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
10341 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
10342 ret_type
= TREE_TYPE (fn_type
);
10343 parm_types
= TYPE_ARG_TYPES (fn_type
);
10345 /* For a static member function, we generate a fake `this' pointer,
10346 for the purposes of mangling. This indicates of which class the
10347 function is a member. Because of:
10351 There shall not be a static and a nonstatic member function
10352 with the same name and the same parameter types
10354 we don't have to worry that this will result in a clash with a
10355 non-static member function. */
10356 if (DECL_STATIC_FUNCTION_P (decl
))
10357 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
10359 /* There should be the same number of template parameters as
10360 template arguments. */
10361 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
10364 /* Actually set the DECL_ASSEMBLER_NAME. */
10365 DECL_ASSEMBLER_NAME (decl
)
10366 = build_decl_overload_real (decl
, parm_types
, ret_type
,
10368 DECL_FUNCTION_MEMBER_P (decl
)
10369 + DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl
));
10372 /* Return truthvalue if we're processing a template different from
10373 the last one involved in diagnostics. */
10375 problematic_instantiation_changed ()
10377 return last_template_error_tick
!= tinst_level_tick
;
10380 /* Remember current template involved in diagnostics. */
10382 record_last_problematic_instantiation ()
10384 last_template_error_tick
= tinst_level_tick
;
10388 current_instantiation ()
10390 return current_tinst_level
;
10393 /* [temp.param] Check that template non-type parm TYPE is of an allowable
10394 type. Return zero for ok, non-zero for disallowed. If COMPLAIN is
10395 non-zero, then complain. */
10398 invalid_nontype_parm_type_p (type
, complain
)
10402 if (INTEGRAL_TYPE_P (type
))
10404 else if (POINTER_TYPE_P (type
))
10406 else if (TYPE_PTRMEM_P (type
))
10408 else if (TYPE_PTRMEMFUNC_P (type
))
10410 else if (!pedantic
&& TREE_CODE (type
) == REAL_TYPE
)
10411 return 0; /* GNU extension */
10412 else if (!pedantic
&& TREE_CODE (type
) == COMPLEX_TYPE
)
10413 return 0; /* GNU extension */
10414 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
10416 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
10420 cp_error ("`%#T' is not a valid type for a template constant parameter",