1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
37 #include "tree-inline.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work.
53 The TREE_PURPOSE of each entry is either a DECL (for a function or
54 static data member), or a TYPE (for a class) indicating what we are
55 hoping to instantiate. The TREE_VALUE is not used. */
56 static GTY(()) tree pending_templates
;
57 static GTY(()) tree last_pending_template
;
59 int processing_template_parmlist
;
60 static int template_header_count
;
62 static GTY(()) tree saved_trees
;
63 static GTY(()) varray_type inline_parm_levels
;
64 static size_t inline_parm_levels_used
;
66 static GTY(()) tree current_tinst_level
;
68 static GTY(()) tree saved_access_scope
;
70 /* A map from local variable declarations in the body of the template
71 presently being instantiated to the corresponding instantiated
73 static htab_t local_specializations
;
75 #define UNIFY_ALLOW_NONE 0
76 #define UNIFY_ALLOW_MORE_CV_QUAL 1
77 #define UNIFY_ALLOW_LESS_CV_QUAL 2
78 #define UNIFY_ALLOW_DERIVED 4
79 #define UNIFY_ALLOW_INTEGER 8
80 #define UNIFY_ALLOW_OUTER_LEVEL 16
81 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
82 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
83 #define UNIFY_ALLOW_MAX_CORRECTION 128
85 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
86 virtual, or a base class of a virtual
88 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
89 type with the desired type. */
91 static void push_access_scope_real (tree
, tree
, tree
);
92 static void push_access_scope (tree
);
93 static void pop_access_scope (tree
);
94 static int resolve_overloaded_unification (tree
, tree
, tree
, tree
,
95 unification_kind_t
, int);
96 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
97 unification_kind_t
, int);
98 static int unify (tree
, tree
, tree
, tree
, int);
99 static void add_pending_template (tree
);
100 static void reopen_tinst_level (tree
);
101 static tree
classtype_mangled_name (tree
);
102 static char* mangle_class_name_for_template (const char *, tree
, tree
);
103 static tree
tsubst_initializer_list (tree
, tree
);
104 static int list_eq (tree
, tree
);
105 static tree
get_class_bindings (tree
, tree
, tree
);
106 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
, int);
107 static void tsubst_enum (tree
, tree
, tree
);
108 static tree
add_to_template_args (tree
, tree
);
109 static tree
add_outermost_template_args (tree
, tree
);
110 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
111 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*);
112 static int type_unification_real (tree
, tree
, tree
, tree
,
113 int, unification_kind_t
, int, int);
114 static void note_template_header (int);
115 static tree
maybe_fold_nontype_arg (tree
);
116 static void maybe_fold_nontype_args (tree
);
117 static tree
convert_nontype_argument (tree
, tree
);
118 static tree
convert_template_argument (tree
, tree
, tree
,
119 tsubst_flags_t
, int, tree
);
120 static tree
get_bindings_overload (tree
, tree
, tree
);
121 static int for_each_template_parm (tree
, tree_fn_t
, void*, htab_t
);
122 static tree
build_template_parm_index (int, int, int, tree
, tree
);
123 static int inline_needs_template_parms (tree
);
124 static void push_inline_template_parms_recursive (tree
, int);
125 static tree
retrieve_specialization (tree
, tree
);
126 static tree
retrieve_local_specialization (tree
);
127 static tree
register_specialization (tree
, tree
, tree
);
128 static void register_local_specialization (tree
, tree
);
129 static int unregister_specialization (tree
, tree
);
130 static tree
reduce_template_parm_level (tree
, tree
, int);
131 static tree
build_template_decl (tree
, tree
);
132 static int mark_template_parm (tree
, void *);
133 static int template_parm_this_level_p (tree
, void *);
134 static tree
tsubst_friend_function (tree
, tree
);
135 static tree
tsubst_friend_class (tree
, tree
);
136 static int can_complete_type_without_circularity (tree
);
137 static tree
get_bindings_real (tree
, tree
, tree
, int, int, int);
138 static int template_decl_level (tree
);
139 static int check_cv_quals_for_unify (int, tree
, tree
);
140 static tree
tsubst_template_arg_vector (tree
, tree
, tsubst_flags_t
);
141 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
142 static void regenerate_decl_from_template (tree
, tree
);
143 static tree
most_specialized (tree
, tree
, tree
);
144 static tree
most_specialized_class (tree
, tree
);
145 static int template_class_depth_real (tree
, int);
146 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
147 static tree
tsubst_decl (tree
, tree
, tree
, tsubst_flags_t
);
148 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
149 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
150 static void check_specialization_scope (void);
151 static tree
process_partial_specialization (tree
);
152 static void set_current_access_from_decl (tree
);
153 static void check_default_tmpl_args (tree
, tree
, int, int);
154 static tree
tsubst_call_declarator_parms (tree
, tree
, tsubst_flags_t
, tree
);
155 static tree
get_template_base_recursive (tree
, tree
, tree
, tree
, tree
, int);
156 static tree
get_template_base (tree
, tree
, tree
, tree
);
157 static int verify_class_unification (tree
, tree
, tree
);
158 static tree
try_class_unification (tree
, tree
, tree
, tree
);
159 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
161 static tree
determine_specialization (tree
, tree
, tree
*, int);
162 static int template_args_equal (tree
, tree
);
163 static void tsubst_default_arguments (tree
);
164 static tree
for_each_template_parm_r (tree
*, int *, void *);
165 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
166 static void copy_default_args_to_explicit_spec (tree
);
167 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
168 static int eq_local_specializations (const void *, const void *);
169 static bool dependent_type_p_r (tree
);
170 static bool dependent_template_id_p (tree
, tree
);
171 static tree
tsubst (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
);
173 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
174 static tree
tsubst_copy_and_build (tree
, tree
, tsubst_flags_t
, tree
);
176 /* Make the current scope suitable for access checking when we are
177 processing T. T can be FUNCTION_DECL for instantiated function
178 template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for
179 static member variable (need by instantiate_decl). ARGS is the
180 template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE,
181 this is used instead of the context of T. */
184 push_access_scope_real (tree t
, tree args
, tree context
)
186 if (TREE_CODE (t
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (t
))
188 /* When we are processing specialization `foo<Outer>' for code like
190 template <class U> typename U::Inner foo ();
193 friend Outer::Inner foo<Outer> ();
196 `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of
197 its specialization. We can get the FUNCTION_DECL with the right
198 information because this specialization has already been
199 registered by the friend declaration above. */
201 if (DECL_FUNCTION_TEMPLATE_P (t
) && args
)
203 tree full_args
= tsubst_template_arg_vector
204 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
)), args
, tf_none
);
205 tree spec
= NULL_TREE
;
206 if (full_args
!= error_mark_node
)
207 spec
= retrieve_specialization (t
, full_args
);
214 context
= DECL_CONTEXT (t
);
215 if (context
&& TYPE_P (context
))
216 push_nested_class (context
);
218 push_to_top_level ();
220 if (TREE_CODE (t
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (t
))
222 saved_access_scope
= tree_cons
223 (NULL_TREE
, current_function_decl
, saved_access_scope
);
224 current_function_decl
= t
;
228 /* Like push_access_scope_real, but always uses DECL_CONTEXT. */
231 push_access_scope (tree t
)
233 push_access_scope_real (t
, NULL_TREE
, NULL_TREE
);
236 /* Restore the scope set up by push_access_scope. T is the node we
240 pop_access_scope (tree t
)
242 if (TREE_CODE (t
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (t
))
244 current_function_decl
= TREE_VALUE (saved_access_scope
);
245 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
248 if (DECL_CLASS_SCOPE_P (t
))
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
260 finish_member_template_decl (tree decl
)
262 if (decl
== error_mark_node
)
263 return error_mark_node
;
265 my_friendly_assert (DECL_P (decl
), 20020812);
267 if (TREE_CODE (decl
) == TYPE_DECL
)
271 type
= TREE_TYPE (decl
);
272 if (IS_AGGR_TYPE (type
)
273 && CLASSTYPE_TEMPLATE_INFO (type
)
274 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
276 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
277 check_member_template (tmpl
);
282 else if (TREE_CODE (decl
) == FIELD_DECL
)
283 error ("data member `%D' cannot be a member template", decl
);
284 else if (DECL_TEMPLATE_INFO (decl
))
286 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
288 check_member_template (DECL_TI_TEMPLATE (decl
));
289 return DECL_TI_TEMPLATE (decl
);
295 error ("invalid member template declaration `%D'", decl
);
297 return error_mark_node
;
300 /* Returns the template nesting level of the indicated class TYPE.
310 A<T>::B<U> has depth two, while A<T> has depth one.
311 Both A<T>::B<int> and A<int>::B<U> have depth one, if
312 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
315 This function is guaranteed to return 0 if passed NULL_TREE so
316 that, for example, `template_class_depth (current_class_type)' is
320 template_class_depth_real (tree type
, int count_specializations
)
325 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
326 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
327 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
329 if (TREE_CODE (type
) != FUNCTION_DECL
)
331 if (CLASSTYPE_TEMPLATE_INFO (type
)
332 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
333 && ((count_specializations
334 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
335 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
340 if (DECL_TEMPLATE_INFO (type
)
341 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
342 && ((count_specializations
343 && DECL_TEMPLATE_SPECIALIZATION (type
))
344 || uses_template_parms (DECL_TI_ARGS (type
))))
352 /* Returns the template nesting level of the indicated class TYPE.
353 Like template_class_depth_real, but instantiations do not count in
357 template_class_depth (tree type
)
359 return template_class_depth_real (type
, /*count_specializations=*/0);
362 /* Returns 1 if processing DECL as part of do_pending_inlines
363 needs us to push template parms. */
366 inline_needs_template_parms (tree decl
)
368 if (! DECL_TEMPLATE_INFO (decl
))
371 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
372 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
375 /* Subroutine of maybe_begin_member_template_processing.
376 Push the template parms in PARMS, starting from LEVELS steps into the
377 chain, and ending at the beginning, since template parms are listed
381 push_inline_template_parms_recursive (tree parmlist
, int levels
)
383 tree parms
= TREE_VALUE (parmlist
);
387 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
389 ++processing_template_decl
;
390 current_template_parms
391 = tree_cons (size_int (processing_template_decl
),
392 parms
, current_template_parms
);
393 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
396 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
398 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
399 my_friendly_assert (DECL_P (parm
), 0);
401 switch (TREE_CODE (parm
))
410 /* Make a CONST_DECL as is done in process_template_parm.
411 It is ugly that we recreate this here; the original
412 version built in process_template_parm is no longer
414 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
416 DECL_ARTIFICIAL (decl
) = 1;
417 TREE_CONSTANT (decl
) = TREE_READONLY (decl
) = 1;
418 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
419 SET_DECL_TEMPLATE_PARM_P (decl
);
430 /* Restore the template parameter context for a member template or
431 a friend template defined in a class definition. */
434 maybe_begin_member_template_processing (tree decl
)
439 if (inline_needs_template_parms (decl
))
441 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
442 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
444 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
447 parms
= TREE_CHAIN (parms
);
450 push_inline_template_parms_recursive (parms
, levels
);
453 /* Remember how many levels of template parameters we pushed so that
454 we can pop them later. */
455 if (!inline_parm_levels
)
456 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
457 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
458 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
459 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
460 ++inline_parm_levels_used
;
463 /* Undo the effects of begin_member_template_processing. */
466 maybe_end_member_template_processing (void)
470 if (!inline_parm_levels_used
)
473 --inline_parm_levels_used
;
475 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
478 --processing_template_decl
;
479 current_template_parms
= TREE_CHAIN (current_template_parms
);
484 /* Returns nonzero iff T is a member template function. We must be
487 template <class T> class C { void f(); }
489 Here, f is a template function, and a member, but not a member
490 template. This function does not concern itself with the origin of
491 T, only its present state. So if we have
493 template <class T> class C { template <class U> void f(U); }
495 then neither C<int>::f<char> nor C<T>::f<double> is considered
496 to be a member template. But, `template <class U> void
497 C<int>::f(U)' is considered a member template. */
500 is_member_template (tree t
)
502 if (!DECL_FUNCTION_TEMPLATE_P (t
))
503 /* Anything that isn't a function or a template function is
504 certainly not a member template. */
507 /* A local class can't have member templates. */
508 if (decl_function_context (t
))
511 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
512 /* If there are more levels of template parameters than
513 there are template classes surrounding the declaration,
514 then we have a member template. */
515 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
516 template_class_depth (DECL_CONTEXT (t
))));
520 /* Returns nonzero iff T is a member template class. See
521 is_member_template for a description of what precisely constitutes
522 a member template. */
525 is_member_template_class (tree t
)
527 if (!DECL_CLASS_TEMPLATE_P (t
))
528 /* Anything that isn't a class template, is certainly not a member
532 if (!DECL_CLASS_SCOPE_P (t
))
533 /* Anything whose context isn't a class type is surely not a
537 /* If there are more levels of template parameters than there are
538 template classes surrounding the declaration, then we have a
540 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
541 template_class_depth (DECL_CONTEXT (t
)));
545 /* Return a new template argument vector which contains all of ARGS,
546 but has as its innermost set of arguments the EXTRA_ARGS. */
549 add_to_template_args (tree args
, tree extra_args
)
556 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
557 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
559 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
560 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
562 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
563 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
568 /* Like add_to_template_args, but only the outermost ARGS are added to
569 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
570 (EXTRA_ARGS) levels are added. This function is used to combine
571 the template arguments from a partial instantiation with the
572 template arguments used to attain the full instantiation from the
573 partial instantiation. */
576 add_outermost_template_args (tree args
, tree extra_args
)
580 /* If there are more levels of EXTRA_ARGS than there are ARGS,
581 something very fishy is going on. */
582 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
585 /* If *all* the new arguments will be the EXTRA_ARGS, just return
587 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
590 /* For the moment, we make ARGS look like it contains fewer levels. */
591 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
593 new_args
= add_to_template_args (args
, extra_args
);
595 /* Now, we restore ARGS to its full dimensions. */
596 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
601 /* Return the N levels of innermost template arguments from the ARGS. */
604 get_innermost_template_args (tree args
, int n
)
610 my_friendly_assert (n
>= 0, 20000603);
612 /* If N is 1, just return the innermost set of template arguments. */
614 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
616 /* If we're not removing anything, just return the arguments we were
618 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
619 my_friendly_assert (extra_levels
>= 0, 20000603);
620 if (extra_levels
== 0)
623 /* Make a new set of arguments, not containing the outer arguments. */
624 new_args
= make_tree_vec (n
);
625 for (i
= 1; i
<= n
; ++i
)
626 SET_TMPL_ARGS_LEVEL (new_args
, i
,
627 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
632 /* We've got a template header coming up; push to a new level for storing
636 begin_template_parm_list (void)
638 /* We use a non-tag-transparent scope here, which causes pushtag to
639 put tags in this scope, rather than in the enclosing class or
640 namespace scope. This is the right thing, since we want
641 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
642 global template class, push_template_decl handles putting the
643 TEMPLATE_DECL into top-level scope. For a nested template class,
646 template <class T> struct S1 {
647 template <class T> struct S2 {};
650 pushtag contains special code to call pushdecl_with_scope on the
651 TEMPLATE_DECL for S2. */
652 begin_scope (sk_template_parms
);
653 ++processing_template_decl
;
654 ++processing_template_parmlist
;
655 note_template_header (0);
658 /* This routine is called when a specialization is declared. If it is
659 invalid to declare a specialization here, an error is reported. */
662 check_specialization_scope (void)
664 tree scope
= current_scope ();
668 An explicit specialization shall be declared in the namespace of
669 which the template is a member, or, for member templates, in the
670 namespace of which the enclosing class or enclosing class
671 template is a member. An explicit specialization of a member
672 function, member class or static data member of a class template
673 shall be declared in the namespace of which the class template
675 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
676 error ("explicit specialization in non-namespace scope `%D'",
681 In an explicit specialization declaration for a member of a class
682 template or a member template that appears in namespace scope,
683 the member template and some of its enclosing class templates may
684 remain unspecialized, except that the declaration shall not
685 explicitly specialize a class member template if its enclosing
686 class templates are not explicitly specialized as well. */
687 if (current_template_parms
)
688 error ("enclosing class templates are not explicitly specialized");
691 /* We've just seen template <>. */
694 begin_specialization (void)
696 begin_scope (sk_template_spec
);
697 note_template_header (1);
698 check_specialization_scope ();
701 /* Called at then end of processing a declaration preceded by
705 end_specialization (void)
708 reset_specialization ();
711 /* Any template <>'s that we have seen thus far are not referring to a
712 function specialization. */
715 reset_specialization (void)
717 processing_specialization
= 0;
718 template_header_count
= 0;
721 /* We've just seen a template header. If SPECIALIZATION is nonzero,
722 it was of the form template <>. */
725 note_template_header (int specialization
)
727 processing_specialization
= specialization
;
728 template_header_count
++;
731 /* We're beginning an explicit instantiation. */
734 begin_explicit_instantiation (void)
736 my_friendly_assert (!processing_explicit_instantiation
, 20020913);
737 processing_explicit_instantiation
= true;
742 end_explicit_instantiation (void)
744 my_friendly_assert(processing_explicit_instantiation
, 20020913);
745 processing_explicit_instantiation
= false;
748 /* The TYPE is being declared. If it is a template type, that means it
749 is a partial specialization. Do appropriate error-checking. */
752 maybe_process_partial_specialization (tree type
)
754 /* TYPE maybe an ERROR_MARK_NODE. */
755 tree context
= TYPE_P (type
) ? TYPE_CONTEXT (type
) : NULL_TREE
;
757 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
759 /* This is for ordinary explicit specialization and partial
760 specialization of a template class such as:
762 template <> class C<int>;
766 template <class T> class C<T*>;
768 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
770 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
771 && !COMPLETE_TYPE_P (type
))
773 if (current_namespace
774 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
776 pedwarn ("specializing `%#T' in different namespace", type
);
777 cp_pedwarn_at (" from definition of `%#D'",
778 CLASSTYPE_TI_TEMPLATE (type
));
780 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
781 if (processing_template_decl
)
782 push_template_decl (TYPE_MAIN_DECL (type
));
784 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
785 error ("specialization of `%T' after instantiation", type
);
787 else if (CLASS_TYPE_P (type
)
788 && !CLASSTYPE_USE_TEMPLATE (type
)
789 && CLASSTYPE_TEMPLATE_INFO (type
)
790 && context
&& CLASS_TYPE_P (context
)
791 && CLASSTYPE_TEMPLATE_INFO (context
))
793 /* This is for an explicit specialization of member class
794 template according to [temp.expl.spec/18]:
796 template <> template <class U> class C<int>::D;
798 The context `C<int>' must be an implicit instantiation.
799 Otherwise this is just a member class template declared
802 template <> class C<int> { template <class U> class D; };
803 template <> template <class U> class C<int>::D;
805 In the first case, `C<int>::D' is a specialization of `C<T>::D'
806 while in the second case, `C<int>::D' is a primary template
807 and `C<T>::D' may not exist. */
809 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
810 && !COMPLETE_TYPE_P (type
))
814 if (current_namespace
815 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
817 pedwarn ("specializing `%#T' in different namespace", type
);
818 cp_pedwarn_at (" from definition of `%#D'",
819 CLASSTYPE_TI_TEMPLATE (type
));
822 /* Check for invalid specialization after instantiation:
824 template <> template <> class C<int>::D<int>;
825 template <> template <class U> class C<int>::D; */
827 for (t
= DECL_TEMPLATE_INSTANTIATIONS
828 (most_general_template (CLASSTYPE_TI_TEMPLATE (type
)));
829 t
; t
= TREE_CHAIN (t
))
830 if (TREE_VALUE (t
) != type
831 && TYPE_CONTEXT (TREE_VALUE (t
)) == context
)
832 error ("specialization `%T' after instantiation `%T'",
833 type
, TREE_VALUE (t
));
835 /* Mark TYPE as a specialization. And as a result, we only
836 have one level of template argument for the innermost
838 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
839 CLASSTYPE_TI_ARGS (type
)
840 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
843 else if (processing_specialization
)
844 error ("explicit specialization of non-template `%T'", type
);
847 /* Retrieve the specialization (in the sense of [temp.spec] - a
848 specialization is either an instantiation or an explicit
849 specialization) of TMPL for the given template ARGS. If there is
850 no such specialization, return NULL_TREE. The ARGS are a vector of
851 arguments, or a vector of vectors of arguments, in the case of
852 templates with more than one level of parameters. */
855 retrieve_specialization (tree tmpl
, tree args
)
859 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
861 /* There should be as many levels of arguments as there are
862 levels of parameters. */
863 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
864 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
867 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
870 if (comp_template_args (TREE_PURPOSE (s
), args
))
871 return TREE_VALUE (s
);
876 /* Like retrieve_specialization, but for local declarations. */
879 retrieve_local_specialization (tree tmpl
)
882 (tree
) htab_find_with_hash (local_specializations
, tmpl
,
883 htab_hash_pointer (tmpl
));
884 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
887 /* Returns nonzero iff DECL is a specialization of TMPL. */
890 is_specialization_of (tree decl
, tree tmpl
)
894 if (TREE_CODE (decl
) == FUNCTION_DECL
)
898 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
904 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
906 for (t
= TREE_TYPE (decl
);
908 t
= CLASSTYPE_USE_TEMPLATE (t
)
909 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
910 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
917 /* Register the specialization SPEC as a specialization of TMPL with
918 the indicated ARGS. Returns SPEC, or an equivalent prior
919 declaration, if available. */
922 register_specialization (tree spec
, tree tmpl
, tree args
)
926 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
928 if (TREE_CODE (spec
) == FUNCTION_DECL
929 && uses_template_parms (DECL_TI_ARGS (spec
)))
930 /* This is the FUNCTION_DECL for a partial instantiation. Don't
931 register it; we want the corresponding TEMPLATE_DECL instead.
932 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
933 the more obvious `uses_template_parms (spec)' to avoid problems
934 with default function arguments. In particular, given
937 template <class T> void f(T t1, T t = T())
939 the default argument expression is not substituted for in an
940 instantiation unless and until it is actually needed. */
943 /* There should be as many levels of arguments as there are
944 levels of parameters. */
945 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
946 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
949 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
953 tree fn
= TREE_VALUE (s
);
955 /* We can sometimes try to re-register a specialization that we've
956 already got. In particular, regenerate_decl_from_template
957 calls duplicate_decls which will update the specialization
958 list. But, we'll still get called again here anyhow. It's
959 more convenient to simply allow this than to try to prevent it. */
962 else if (comp_template_args (TREE_PURPOSE (s
), args
))
964 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
966 if (DECL_TEMPLATE_INSTANTIATION (fn
))
969 || DECL_EXPLICIT_INSTANTIATION (fn
))
971 error ("specialization of %D after instantiation",
977 /* This situation should occur only if the first
978 specialization is an implicit instantiation,
979 the second is an explicit specialization, and
980 the implicit instantiation has not yet been
981 used. That situation can occur if we have
982 implicitly instantiated a member function and
983 then specialized it later.
985 We can also wind up here if a friend
986 declaration that looked like an instantiation
987 turns out to be a specialization:
989 template <class T> void foo(T);
990 class S { friend void foo<>(int) };
991 template <> void foo(int);
993 We transform the existing DECL in place so that
994 any pointers to it become pointers to the
997 If there was a definition for the template, but
998 not for the specialization, we want this to
999 look as if there is no definition, and vice
1001 DECL_INITIAL (fn
) = NULL_TREE
;
1002 duplicate_decls (spec
, fn
);
1007 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1009 if (!duplicate_decls (spec
, fn
) && DECL_INITIAL (spec
))
1010 /* Dup decl failed, but this is a new
1011 definition. Set the line number so any errors
1012 match this new definition. */
1013 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1021 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
1022 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
1027 /* Unregister the specialization SPEC as a specialization of TMPL.
1028 Returns nonzero if the SPEC was listed as a specialization of
1032 unregister_specialization (tree spec
, tree tmpl
)
1036 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
1038 s
= &TREE_CHAIN (*s
))
1039 if (TREE_VALUE (*s
) == spec
)
1041 *s
= TREE_CHAIN (*s
);
1048 /* Compare an entry in the local specializations hash table P1 (which
1049 is really a pointer to a TREE_LIST) with P2 (which is really a
1053 eq_local_specializations (const void *p1
, const void *p2
)
1055 return TREE_VALUE ((tree
) p1
) == (tree
) p2
;
1058 /* Hash P1, an entry in the local specializations table. */
1061 hash_local_specialization (const void* p1
)
1063 return htab_hash_pointer (TREE_VALUE ((tree
) p1
));
1066 /* Like register_specialization, but for local declarations. We are
1067 registering SPEC, an instantiation of TMPL. */
1070 register_local_specialization (tree spec
, tree tmpl
)
1074 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1075 htab_hash_pointer (tmpl
), INSERT
);
1076 *slot
= build_tree_list (spec
, tmpl
);
1079 /* Print the list of candidate FNS in an error message. */
1082 print_candidates (tree fns
)
1086 const char *str
= "candidates are:";
1088 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
1092 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
1093 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
1098 /* Returns the template (one of the functions given by TEMPLATE_ID)
1099 which can be specialized to match the indicated DECL with the
1100 explicit template args given in TEMPLATE_ID. The DECL may be
1101 NULL_TREE if none is available. In that case, the functions in
1102 TEMPLATE_ID are non-members.
1104 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1105 specialization of a member template.
1107 The template args (those explicitly specified and those deduced)
1108 are output in a newly created vector *TARGS_OUT.
1110 If it is impossible to determine the result, an error message is
1111 issued. The error_mark_node is returned to indicate failure. */
1114 determine_specialization (tree template_id
,
1117 int need_member_template
)
1121 tree explicit_targs
;
1122 tree candidates
= NULL_TREE
;
1123 tree templates
= NULL_TREE
;
1125 *targs_out
= NULL_TREE
;
1127 if (template_id
== error_mark_node
)
1128 return error_mark_node
;
1130 fns
= TREE_OPERAND (template_id
, 0);
1131 explicit_targs
= TREE_OPERAND (template_id
, 1);
1133 if (fns
== error_mark_node
)
1134 return error_mark_node
;
1136 /* Check for baselinks. */
1137 if (BASELINK_P (fns
))
1138 fns
= BASELINK_FUNCTIONS (fns
);
1140 if (!is_overloaded_fn (fns
))
1142 error ("`%D' is not a function template", fns
);
1143 return error_mark_node
;
1146 for (; fns
; fns
= OVL_NEXT (fns
))
1148 tree fn
= OVL_CURRENT (fns
);
1150 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1152 tree decl_arg_types
;
1154 /* DECL might be a specialization of FN. */
1156 /* Adjust the type of DECL in case FN is a static member. */
1157 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1158 if (DECL_STATIC_FUNCTION_P (fn
)
1159 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1160 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1162 /* Check that the number of function parameters matches.
1164 template <class T> void f(int i = 0);
1165 template <> void f<int>();
1166 The specialization f<int> is invalid but is not caught
1167 by get_bindings below. */
1169 if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
1170 != list_length (decl_arg_types
))
1173 /* See whether this function might be a specialization of this
1175 targs
= get_bindings (fn
, decl
, explicit_targs
);
1178 /* We cannot deduce template arguments that when used to
1179 specialize TMPL will produce DECL. */
1182 /* Save this template, and the arguments deduced. */
1183 templates
= tree_cons (targs
, fn
, templates
);
1185 else if (need_member_template
)
1186 /* FN is an ordinary member function, and we need a
1187 specialization of a member template. */
1189 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1190 /* We can get IDENTIFIER_NODEs here in certain erroneous
1193 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1194 /* This is just an ordinary non-member function. Nothing can
1195 be a specialization of that. */
1197 else if (DECL_ARTIFICIAL (fn
))
1198 /* Cannot specialize functions that are created implicitly. */
1202 tree decl_arg_types
;
1204 /* This is an ordinary member function. However, since
1205 we're here, we can assume it's enclosing class is a
1206 template class. For example,
1208 template <typename T> struct S { void f(); };
1209 template <> void S<int>::f() {}
1211 Here, S<int>::f is a non-template, but S<int> is a
1212 template class. If FN has the same type as DECL, we
1213 might be in business. */
1215 if (!DECL_TEMPLATE_INFO (fn
))
1216 /* Its enclosing class is an explicit specialization
1217 of a template class. This is not a candidate. */
1220 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1221 TREE_TYPE (TREE_TYPE (fn
))))
1222 /* The return types differ. */
1225 /* Adjust the type of DECL in case FN is a static member. */
1226 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1227 if (DECL_STATIC_FUNCTION_P (fn
)
1228 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1229 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1231 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1234 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1238 if (templates
&& TREE_CHAIN (templates
))
1244 It is possible for a specialization with a given function
1245 signature to be instantiated from more than one function
1246 template. In such cases, explicit specification of the
1247 template arguments must be used to uniquely identify the
1248 function template specialization being specialized.
1250 Note that here, there's no suggestion that we're supposed to
1251 determine which of the candidate templates is most
1252 specialized. However, we, also have:
1256 Partial ordering of overloaded function template
1257 declarations is used in the following contexts to select
1258 the function template to which a function template
1259 specialization refers:
1261 -- when an explicit specialization refers to a function
1264 So, we do use the partial ordering rules, at least for now.
1265 This extension can only serve to make invalid programs valid,
1266 so it's safe. And, there is strong anecdotal evidence that
1267 the committee intended the partial ordering rules to apply;
1268 the EDG front-end has that behavior, and John Spicer claims
1269 that the committee simply forgot to delete the wording in
1270 [temp.expl.spec]. */
1271 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1272 if (tmpl
&& tmpl
!= error_mark_node
)
1274 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1275 templates
= tree_cons (targs
, tmpl
, NULL_TREE
);
1279 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1281 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1283 return error_mark_node
;
1285 else if ((templates
&& TREE_CHAIN (templates
))
1286 || (candidates
&& TREE_CHAIN (candidates
))
1287 || (templates
&& candidates
))
1289 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1291 chainon (candidates
, templates
);
1292 print_candidates (candidates
);
1293 return error_mark_node
;
1296 /* We have one, and exactly one, match. */
1299 /* It was a specialization of an ordinary member function in a
1301 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1302 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1305 /* It was a specialization of a template. */
1306 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1307 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1309 *targs_out
= copy_node (targs
);
1310 SET_TMPL_ARGS_LEVEL (*targs_out
,
1311 TMPL_ARGS_DEPTH (*targs_out
),
1312 TREE_PURPOSE (templates
));
1315 *targs_out
= TREE_PURPOSE (templates
);
1316 return TREE_VALUE (templates
);
1319 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1320 but with the default argument values filled in from those in the
1324 copy_default_args_to_explicit_spec_1 (tree spec_types
,
1327 tree new_spec_types
;
1332 if (spec_types
== void_list_node
)
1333 return void_list_node
;
1335 /* Substitute into the rest of the list. */
1337 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1338 TREE_CHAIN (tmpl_types
));
1340 /* Add the default argument for this parameter. */
1341 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1342 TREE_VALUE (spec_types
),
1346 /* DECL is an explicit specialization. Replicate default arguments
1347 from the template it specializes. (That way, code like:
1349 template <class T> void f(T = 3);
1350 template <> void f(double);
1353 works, as required.) An alternative approach would be to look up
1354 the correct default arguments at the call-site, but this approach
1355 is consistent with how implicit instantiations are handled. */
1358 copy_default_args_to_explicit_spec (tree decl
)
1363 tree new_spec_types
;
1367 tree object_type
= NULL_TREE
;
1368 tree in_charge
= NULL_TREE
;
1369 tree vtt
= NULL_TREE
;
1371 /* See if there's anything we need to do. */
1372 tmpl
= DECL_TI_TEMPLATE (decl
);
1373 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1374 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1375 if (TREE_PURPOSE (t
))
1380 old_type
= TREE_TYPE (decl
);
1381 spec_types
= TYPE_ARG_TYPES (old_type
);
1383 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1385 /* Remove the this pointer, but remember the object's type for
1387 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1388 spec_types
= TREE_CHAIN (spec_types
);
1389 tmpl_types
= TREE_CHAIN (tmpl_types
);
1391 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1393 /* DECL may contain more parameters than TMPL due to the extra
1394 in-charge parameter in constructors and destructors. */
1395 in_charge
= spec_types
;
1396 spec_types
= TREE_CHAIN (spec_types
);
1398 if (DECL_HAS_VTT_PARM_P (decl
))
1401 spec_types
= TREE_CHAIN (spec_types
);
1405 /* Compute the merged default arguments. */
1407 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1409 /* Compute the new FUNCTION_TYPE. */
1413 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1418 /* Put the in-charge parameter back. */
1419 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1420 TREE_VALUE (in_charge
),
1423 new_type
= build_cplus_method_type (object_type
,
1424 TREE_TYPE (old_type
),
1428 new_type
= build_function_type (TREE_TYPE (old_type
),
1430 new_type
= build_type_attribute_variant (new_type
,
1431 TYPE_ATTRIBUTES (old_type
));
1432 new_type
= build_exception_variant (new_type
,
1433 TYPE_RAISES_EXCEPTIONS (old_type
));
1434 TREE_TYPE (decl
) = new_type
;
1437 /* Check to see if the function just declared, as indicated in
1438 DECLARATOR, and in DECL, is a specialization of a function
1439 template. We may also discover that the declaration is an explicit
1440 instantiation at this point.
1442 Returns DECL, or an equivalent declaration that should be used
1443 instead if all goes well. Issues an error message if something is
1444 amiss. Returns error_mark_node if the error is not easily
1447 FLAGS is a bitmask consisting of the following flags:
1449 2: The function has a definition.
1450 4: The function is a friend.
1452 The TEMPLATE_COUNT is the number of references to qualifying
1453 template classes that appeared in the name of the function. For
1456 template <class T> struct S { void f(); };
1459 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1460 classes are not counted in the TEMPLATE_COUNT, so that in
1462 template <class T> struct S {};
1463 template <> struct S<int> { void f(); }
1464 template <> void S<int>::f();
1466 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1467 invalid; there should be no template <>.)
1469 If the function is a specialization, it is marked as such via
1470 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1471 is set up correctly, and it is added to the list of specializations
1472 for that template. */
1475 check_explicit_specialization (tree declarator
,
1480 int have_def
= flags
& 2;
1481 int is_friend
= flags
& 4;
1482 int specialization
= 0;
1483 int explicit_instantiation
= 0;
1484 int member_specialization
= 0;
1485 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1486 tree dname
= DECL_NAME (decl
);
1489 tsk
= current_tmpl_spec_kind (template_count
);
1494 if (processing_specialization
)
1497 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1499 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1502 /* This could be something like:
1504 template <class T> void f(T);
1505 class S { friend void f<>(int); } */
1509 /* This case handles bogus declarations like template <>
1510 template <class T> void f<int>(); */
1512 error ("template-id `%D' in declaration of primary template",
1519 case tsk_invalid_member_spec
:
1520 /* The error has already been reported in
1521 check_specialization_scope. */
1522 return error_mark_node
;
1524 case tsk_invalid_expl_inst
:
1525 error ("template parameter list used in explicit instantiation");
1531 error ("definition provided for explicit instantiation");
1533 explicit_instantiation
= 1;
1536 case tsk_excessive_parms
:
1537 error ("too many template parameter lists in declaration of `%D'",
1539 return error_mark_node
;
1543 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1545 member_specialization
= 1;
1550 case tsk_insufficient_parms
:
1551 if (template_header_count
)
1553 error("too few template parameter lists in declaration of `%D'",
1557 else if (ctype
!= NULL_TREE
1558 && !TYPE_BEING_DEFINED (ctype
)
1559 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1562 /* For backwards compatibility, we accept:
1564 template <class T> struct S { void f(); };
1565 void S<int>::f() {} // Missing template <>
1567 That used to be valid C++. */
1570 ("explicit specialization not preceded by `template <>'");
1572 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1577 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1579 /* This case handles bogus declarations like template <>
1580 template <class T> void f<int>(); */
1582 if (uses_template_parms (declarator
))
1583 error ("partial specialization `%D' of function template",
1586 error ("template-id `%D' in declaration of primary template",
1591 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1592 /* This is a specialization of a member template, without
1593 specialization the containing class. Something like:
1595 template <class T> struct S {
1596 template <class U> void f (U);
1598 template <> template <class U> void S<int>::f(U) {}
1600 That's a specialization -- but of the entire template. */
1608 if (specialization
|| member_specialization
)
1610 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1611 for (; t
; t
= TREE_CHAIN (t
))
1612 if (TREE_PURPOSE (t
))
1615 ("default argument specified in explicit specialization");
1618 if (current_lang_name
== lang_name_c
)
1619 error ("template specialization with C linkage");
1622 if (specialization
|| member_specialization
|| explicit_instantiation
)
1624 tree tmpl
= NULL_TREE
;
1625 tree targs
= NULL_TREE
;
1627 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1628 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1632 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
, 0);
1637 /* If there is no class context, the explicit instantiation
1638 must be at namespace scope. */
1639 my_friendly_assert (DECL_NAMESPACE_SCOPE_P (decl
), 20030625);
1641 /* Find the namespace binding, using the declaration
1643 fns
= namespace_binding (dname
, CP_DECL_CONTEXT (decl
));
1646 declarator
= lookup_template_function (fns
, NULL_TREE
);
1649 if (declarator
== error_mark_node
)
1650 return error_mark_node
;
1652 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1654 if (!explicit_instantiation
)
1655 /* A specialization in class scope. This is invalid,
1656 but the error will already have been flagged by
1657 check_specialization_scope. */
1658 return error_mark_node
;
1661 /* It's not valid to write an explicit instantiation in
1664 class C { template void f(); }
1666 This case is caught by the parser. However, on
1669 template class C { void f(); };
1671 (which is invalid) we can get here. The error will be
1678 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1680 /* A friend declaration. We can't do much, because we don't
1681 know what this resolves to, yet. */
1682 my_friendly_assert (is_friend
!= 0, 0);
1683 my_friendly_assert (!explicit_instantiation
, 0);
1684 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1687 else if (ctype
!= NULL_TREE
1688 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1691 /* Find the list of functions in ctype that have the same
1692 name as the declared function. */
1693 tree name
= TREE_OPERAND (declarator
, 0);
1694 tree fns
= NULL_TREE
;
1697 if (constructor_name_p (name
, ctype
))
1699 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1701 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1702 : !TYPE_HAS_DESTRUCTOR (ctype
))
1704 /* From [temp.expl.spec]:
1706 If such an explicit specialization for the member
1707 of a class template names an implicitly-declared
1708 special member function (clause _special_), the
1709 program is ill-formed.
1711 Similar language is found in [temp.explicit]. */
1712 error ("specialization of implicitly-declared special member function");
1713 return error_mark_node
;
1716 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1719 if (!DECL_CONV_FN_P (decl
))
1721 idx
= lookup_fnfields_1 (ctype
, name
);
1723 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1729 /* For a type-conversion operator, we cannot do a
1730 name-based lookup. We might be looking for `operator
1731 int' which will be a specialization of `operator T'.
1732 So, we find *all* the conversion operators, and then
1733 select from them. */
1736 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1738 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
1739 idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1741 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1743 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1744 /* There are no more conversion functions. */
1747 /* Glue all these conversion functions together
1748 with those we already have. */
1749 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1750 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1754 if (fns
== NULL_TREE
)
1756 error ("no member function `%D' declared in `%T'",
1758 return error_mark_node
;
1761 TREE_OPERAND (declarator
, 0) = fns
;
1764 /* Figure out what exactly is being specialized at this point.
1765 Note that for an explicit instantiation, even one for a
1766 member function, we cannot tell apriori whether the
1767 instantiation is for a member template, or just a member
1768 function of a template class. Even if a member template is
1769 being instantiated, the member template arguments may be
1770 elided if they can be deduced from the rest of the
1772 tmpl
= determine_specialization (declarator
, decl
,
1774 member_specialization
);
1776 if (!tmpl
|| tmpl
== error_mark_node
)
1777 /* We couldn't figure out what this declaration was
1779 return error_mark_node
;
1782 tree gen_tmpl
= most_general_template (tmpl
);
1784 if (explicit_instantiation
)
1786 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1787 is done by do_decl_instantiation later. */
1789 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1790 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1792 if (arg_depth
> parm_depth
)
1794 /* If TMPL is not the most general template (for
1795 example, if TMPL is a friend template that is
1796 injected into namespace scope), then there will
1797 be too many levels of TARGS. Remove some of them
1802 new_targs
= make_tree_vec (parm_depth
);
1803 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1804 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1805 = TREE_VEC_ELT (targs
, i
);
1809 return instantiate_template (tmpl
, targs
, tf_error
);
1812 /* If we thought that the DECL was a member function, but it
1813 turns out to be specializing a static member function,
1814 make DECL a static member function as well. We also have
1815 to adjust last_function_parms to avoid confusing
1816 start_function later. */
1817 if (DECL_STATIC_FUNCTION_P (tmpl
)
1818 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1820 revert_static_member_fn (decl
);
1821 last_function_parms
= TREE_CHAIN (last_function_parms
);
1824 /* If this is a specialization of a member template of a
1825 template class. In we want to return the TEMPLATE_DECL,
1826 not the specialization of it. */
1827 if (tsk
== tsk_template
)
1829 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1830 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
1833 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
1834 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl
))
1835 = DECL_SOURCE_LOCATION (decl
);
1840 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1841 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
1843 /* Inherit default function arguments from the template
1844 DECL is specializing. */
1845 copy_default_args_to_explicit_spec (decl
);
1847 /* This specialization has the same protection as the
1848 template it specializes. */
1849 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
1850 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
1852 if (is_friend
&& !have_def
)
1853 /* This is not really a declaration of a specialization.
1854 It's just the name of an instantiation. But, it's not
1855 a request for an instantiation, either. */
1856 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1857 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
1858 /* This is indeed a specialization. In case of constructors
1859 and destructors, we need in-charge and not-in-charge
1860 versions in V3 ABI. */
1861 clone_function_decl (decl
, /*update_method_vec_p=*/0);
1863 /* Register this specialization so that we can find it
1865 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1872 /* TYPE is being declared. Verify that the use of template headers
1873 and such is reasonable. Issue error messages if not. */
1876 maybe_check_template_type (tree type
)
1878 if (template_header_count
)
1880 /* We are in the scope of some `template <...>' header. */
1883 = template_class_depth_real (TYPE_CONTEXT (type
),
1884 /*count_specializations=*/1);
1886 if (template_header_count
<= context_depth
)
1887 /* This is OK; the template headers are for the context. We
1888 are actually too lenient here; like
1889 check_explicit_specialization we should consider the number
1890 of template types included in the actual declaration. For
1893 template <class T> struct S {
1894 template <class U> template <class V>
1900 template <class T> struct S {
1901 template <class U> struct I;
1904 template <class T> template <class U.
1909 else if (template_header_count
> context_depth
+ 1)
1910 /* There are two many template parameter lists. */
1911 error ("too many template parameter lists in declaration of `%T'", type
);
1915 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1916 parameters. These are represented in the same format used for
1917 DECL_TEMPLATE_PARMS. */
1919 int comp_template_parms (tree parms1
, tree parms2
)
1924 if (parms1
== parms2
)
1927 for (p1
= parms1
, p2
= parms2
;
1928 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1929 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1931 tree t1
= TREE_VALUE (p1
);
1932 tree t2
= TREE_VALUE (p2
);
1935 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1936 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1938 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1941 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1943 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1944 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1946 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1949 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1951 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1956 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1957 /* One set of parameters has more parameters lists than the
1964 /* Complain if DECL shadows a template parameter.
1966 [temp.local]: A template-parameter shall not be redeclared within its
1967 scope (including nested scopes). */
1970 check_template_shadow (tree decl
)
1974 /* If we're not in a template, we can't possibly shadow a template
1976 if (!current_template_parms
)
1979 /* Figure out what we're shadowing. */
1980 if (TREE_CODE (decl
) == OVERLOAD
)
1981 decl
= OVL_CURRENT (decl
);
1982 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1984 /* If there's no previous binding for this name, we're not shadowing
1985 anything, let alone a template parameter. */
1989 /* If we're not shadowing a template parameter, we're done. Note
1990 that OLDDECL might be an OVERLOAD (or perhaps even an
1991 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1993 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
1996 /* We check for decl != olddecl to avoid bogus errors for using a
1997 name inside a class. We check TPFI to avoid duplicate errors for
1998 inline member templates. */
2000 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
2003 cp_error_at ("declaration of `%#D'", decl
);
2004 cp_error_at (" shadows template parm `%#D'", olddecl
);
2007 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2008 ORIG_LEVEL, DECL, and TYPE. */
2011 build_template_parm_index (int index
,
2017 tree t
= make_node (TEMPLATE_PARM_INDEX
);
2018 TEMPLATE_PARM_IDX (t
) = index
;
2019 TEMPLATE_PARM_LEVEL (t
) = level
;
2020 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
2021 TEMPLATE_PARM_DECL (t
) = decl
;
2022 TREE_TYPE (t
) = type
;
2023 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
2024 TREE_READONLY (t
) = TREE_READONLY (decl
);
2029 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2030 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2031 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2032 new one is created. */
2035 reduce_template_parm_level (tree index
, tree type
, int levels
)
2037 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
2038 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
2039 != TEMPLATE_PARM_LEVEL (index
) - levels
))
2041 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
2044 decl
= build_decl (TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
2045 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
2046 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
2047 DECL_ARTIFICIAL (decl
) = 1;
2048 SET_DECL_TEMPLATE_PARM_P (decl
);
2050 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
2051 TEMPLATE_PARM_LEVEL (index
) - levels
,
2052 TEMPLATE_PARM_ORIG_LEVEL (index
),
2054 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
2056 /* Template template parameters need this. */
2057 DECL_TEMPLATE_PARMS (decl
)
2058 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
2061 return TEMPLATE_PARM_DESCENDANTS (index
);
2064 /* Process information from new template parameter NEXT and append it to the
2065 LIST being built. */
2068 process_template_parm (tree list
, tree next
)
2076 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
2077 defval
= TREE_PURPOSE (parm
);
2078 parm
= TREE_VALUE (parm
);
2079 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
2083 tree p
= TREE_VALUE (tree_last (list
));
2085 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
2086 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
2088 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
2096 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
2097 /* is a const-param */
2098 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
2100 SET_DECL_TEMPLATE_PARM_P (parm
);
2104 The top-level cv-qualifiers on the template-parameter are
2105 ignored when determining its type. */
2106 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
2108 /* A template parameter is not modifiable. */
2109 TREE_READONLY (parm
) = TREE_CONSTANT (parm
) = 1;
2110 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
2111 TREE_TYPE (parm
) = void_type_node
;
2112 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
2113 TREE_CONSTANT (decl
) = TREE_READONLY (decl
) = 1;
2114 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
2115 = build_template_parm_index (idx
, processing_template_decl
,
2116 processing_template_decl
,
2117 decl
, TREE_TYPE (parm
));
2122 parm
= TREE_VALUE (parm
);
2124 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
2126 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
2127 /* This is for distinguishing between real templates and template
2128 template parameters */
2129 TREE_TYPE (parm
) = t
;
2130 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
2135 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
2136 /* parm is either IDENTIFIER_NODE or NULL_TREE */
2137 decl
= build_decl (TYPE_DECL
, parm
, t
);
2140 TYPE_NAME (t
) = decl
;
2141 TYPE_STUB_DECL (t
) = decl
;
2143 TEMPLATE_TYPE_PARM_INDEX (t
)
2144 = build_template_parm_index (idx
, processing_template_decl
,
2145 processing_template_decl
,
2146 decl
, TREE_TYPE (parm
));
2148 DECL_ARTIFICIAL (decl
) = 1;
2149 SET_DECL_TEMPLATE_PARM_P (decl
);
2151 parm
= build_tree_list (defval
, parm
);
2152 return chainon (list
, parm
);
2155 /* The end of a template parameter list has been reached. Process the
2156 tree list into a parameter vector, converting each parameter into a more
2157 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2161 end_template_parm_list (tree parms
)
2165 tree saved_parmlist
= make_tree_vec (list_length (parms
));
2167 current_template_parms
2168 = tree_cons (size_int (processing_template_decl
),
2169 saved_parmlist
, current_template_parms
);
2171 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
2173 next
= TREE_CHAIN (parm
);
2174 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
2175 TREE_CHAIN (parm
) = NULL_TREE
;
2178 --processing_template_parmlist
;
2180 return saved_parmlist
;
2183 /* end_template_decl is called after a template declaration is seen. */
2186 end_template_decl (void)
2188 reset_specialization ();
2190 if (! processing_template_decl
)
2193 /* This matches the pushlevel in begin_template_parm_list. */
2196 --processing_template_decl
;
2197 current_template_parms
= TREE_CHAIN (current_template_parms
);
2200 /* Given a template argument vector containing the template PARMS.
2201 The innermost PARMS are given first. */
2204 current_template_args (void)
2207 tree args
= NULL_TREE
;
2208 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
2211 /* If there is only one level of template parameters, we do not
2212 create a TREE_VEC of TREE_VECs. Instead, we return a single
2213 TREE_VEC containing the arguments. */
2215 args
= make_tree_vec (length
);
2217 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
2219 tree a
= copy_node (TREE_VALUE (header
));
2222 TREE_TYPE (a
) = NULL_TREE
;
2223 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
2225 tree t
= TREE_VEC_ELT (a
, i
);
2227 /* T will be a list if we are called from within a
2228 begin/end_template_parm_list pair, but a vector directly
2229 if within a begin/end_member_template_processing pair. */
2230 if (TREE_CODE (t
) == TREE_LIST
)
2234 if (TREE_CODE (t
) == TYPE_DECL
2235 || TREE_CODE (t
) == TEMPLATE_DECL
)
2238 t
= DECL_INITIAL (t
);
2239 TREE_VEC_ELT (a
, i
) = t
;
2244 TREE_VEC_ELT (args
, --l
) = a
;
2252 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2253 template PARMS. Used by push_template_decl below. */
2256 build_template_decl (tree decl
, tree parms
)
2258 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
2259 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
2260 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
2261 if (DECL_LANG_SPECIFIC (decl
))
2263 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
2264 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
2265 DECL_DESTRUCTOR_P (tmpl
) = DECL_DESTRUCTOR_P (decl
);
2266 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2267 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
2268 if (DECL_OVERLOADED_OPERATOR_P (decl
))
2269 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
2270 DECL_OVERLOADED_OPERATOR_P (decl
));
2276 struct template_parm_data
2278 /* The level of the template parameters we are currently
2282 /* The index of the specialization argument we are currently
2286 /* An array whose size is the number of template parameters. The
2287 elements are nonzero if the parameter has been used in any one
2288 of the arguments processed so far. */
2291 /* An array whose size is the number of template arguments. The
2292 elements are nonzero if the argument makes use of template
2293 parameters of this level. */
2294 int* arg_uses_template_parms
;
2297 /* Subroutine of push_template_decl used to see if each template
2298 parameter in a partial specialization is used in the explicit
2299 argument list. If T is of the LEVEL given in DATA (which is
2300 treated as a template_parm_data*), then DATA->PARMS is marked
2304 mark_template_parm (tree t
, void* data
)
2308 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2310 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2312 level
= TEMPLATE_PARM_LEVEL (t
);
2313 idx
= TEMPLATE_PARM_IDX (t
);
2317 level
= TEMPLATE_TYPE_LEVEL (t
);
2318 idx
= TEMPLATE_TYPE_IDX (t
);
2321 if (level
== tpd
->level
)
2323 tpd
->parms
[idx
] = 1;
2324 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2327 /* Return zero so that for_each_template_parm will continue the
2328 traversal of the tree; we want to mark *every* template parm. */
2332 /* Process the partial specialization DECL. */
2335 process_partial_specialization (tree decl
)
2337 tree type
= TREE_TYPE (decl
);
2338 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2339 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2340 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
2341 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2342 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2343 int nargs
= TREE_VEC_LENGTH (inner_args
);
2344 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2346 int did_error_intro
= 0;
2347 struct template_parm_data tpd
;
2348 struct template_parm_data tpd2
;
2350 /* We check that each of the template parameters given in the
2351 partial specialization is used in the argument list to the
2352 specialization. For example:
2354 template <class T> struct S;
2355 template <class T> struct S<T*>;
2357 The second declaration is OK because `T*' uses the template
2358 parameter T, whereas
2360 template <class T> struct S<int>;
2362 is no good. Even trickier is:
2373 The S2<T> declaration is actually invalid; it is a
2374 full-specialization. Of course,
2377 struct S2<T (*)(U)>;
2379 or some such would have been OK. */
2380 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2381 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2382 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
2384 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2385 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
2386 for (i
= 0; i
< nargs
; ++i
)
2388 tpd
.current_arg
= i
;
2389 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2390 &mark_template_parm
,
2394 for (i
= 0; i
< ntparms
; ++i
)
2395 if (tpd
.parms
[i
] == 0)
2397 /* One of the template parms was not used in the
2399 if (!did_error_intro
)
2401 error ("template parameters not used in partial specialization:");
2402 did_error_intro
= 1;
2406 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2409 /* [temp.class.spec]
2411 The argument list of the specialization shall not be identical to
2412 the implicit argument list of the primary template. */
2413 if (comp_template_args
2415 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2417 error ("partial specialization `%T' does not specialize any template arguments", type
);
2419 /* [temp.class.spec]
2421 A partially specialized non-type argument expression shall not
2422 involve template parameters of the partial specialization except
2423 when the argument expression is a simple identifier.
2425 The type of a template parameter corresponding to a specialized
2426 non-type argument shall not be dependent on a parameter of the
2428 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2430 for (i
= 0; i
< nargs
; ++i
)
2432 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2433 if (/* These first two lines are the `non-type' bit. */
2435 && TREE_CODE (arg
) != TEMPLATE_DECL
2436 /* This next line is the `argument expression is not just a
2437 simple identifier' condition and also the `specialized
2438 non-type argument' bit. */
2439 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2441 if (tpd
.arg_uses_template_parms
[i
])
2442 error ("template argument `%E' involves template parameter(s)", arg
);
2445 /* Look at the corresponding template parameter,
2446 marking which template parameters its type depends
2449 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2454 /* We haven't yet initialized TPD2. Do so now. */
2455 tpd2
.arg_uses_template_parms
2456 = (int*) alloca (sizeof (int) * nargs
);
2457 /* The number of parameters here is the number in the
2458 main template, which, as checked in the assertion
2460 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2462 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2465 /* Mark the template parameters. But this time, we're
2466 looking for the template parameters of the main
2467 template, not in the specialization. */
2468 tpd2
.current_arg
= i
;
2469 tpd2
.arg_uses_template_parms
[i
] = 0;
2470 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
2471 for_each_template_parm (type
,
2472 &mark_template_parm
,
2476 if (tpd2
.arg_uses_template_parms
[i
])
2478 /* The type depended on some template parameters.
2479 If they are fully specialized in the
2480 specialization, that's OK. */
2482 for (j
= 0; j
< nargs
; ++j
)
2483 if (tpd2
.parms
[j
] != 0
2484 && tpd
.arg_uses_template_parms
[j
])
2486 error ("type `%T' of template argument `%E' depends on template parameter(s)",
2496 if (retrieve_specialization (maintmpl
, specargs
))
2497 /* We've already got this specialization. */
2500 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
2501 = tree_cons (inner_args
, inner_parms
,
2502 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2503 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2507 /* Check that a template declaration's use of default arguments is not
2508 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2509 nonzero if DECL is the thing declared by a primary template.
2510 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2513 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
, int is_partial
)
2516 int last_level_to_check
;
2521 A default template-argument shall not be specified in a
2522 function template declaration or a function template definition, nor
2523 in the template-parameter-list of the definition of a member of a
2526 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
2527 /* You can't have a function template declaration in a local
2528 scope, nor you can you define a member of a class template in a
2532 if (current_class_type
2533 && !TYPE_BEING_DEFINED (current_class_type
)
2534 && DECL_LANG_SPECIFIC (decl
)
2535 /* If this is either a friend defined in the scope of the class
2536 or a member function. */
2537 && (DECL_FUNCTION_MEMBER_P (decl
)
2538 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
2539 : DECL_FRIEND_CONTEXT (decl
)
2540 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
2542 /* And, if it was a member function, it really was defined in
2543 the scope of the class. */
2544 && (!DECL_FUNCTION_MEMBER_P (decl
)
2545 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
2546 /* We already checked these parameters when the template was
2547 declared, so there's no need to do it again now. This function
2548 was defined in class scope, but we're processing it's body now
2549 that the class is complete. */
2554 If a template-parameter has a default template-argument, all
2555 subsequent template-parameters shall have a default
2556 template-argument supplied. */
2557 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2559 tree inner_parms
= TREE_VALUE (parm_level
);
2560 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2561 int seen_def_arg_p
= 0;
2564 for (i
= 0; i
< ntparms
; ++i
)
2566 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2567 if (TREE_PURPOSE (parm
))
2569 else if (seen_def_arg_p
)
2571 error ("no default argument for `%D'", TREE_VALUE (parm
));
2572 /* For better subsequent error-recovery, we indicate that
2573 there should have been a default argument. */
2574 TREE_PURPOSE (parm
) = error_mark_node
;
2579 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2580 /* For an ordinary class template, default template arguments are
2581 allowed at the innermost level, e.g.:
2582 template <class T = int>
2584 but, in a partial specialization, they're not allowed even
2585 there, as we have in [temp.class.spec]:
2587 The template parameter list of a specialization shall not
2588 contain default template argument values.
2590 So, for a partial specialization, or for a function template,
2591 we look at all of them. */
2594 /* But, for a primary class template that is not a partial
2595 specialization we look at all template parameters except the
2597 parms
= TREE_CHAIN (parms
);
2599 /* Figure out what error message to issue. */
2600 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2601 msg
= "default template arguments may not be used in function templates";
2602 else if (is_partial
)
2603 msg
= "default template arguments may not be used in partial specializations";
2605 msg
= "default argument for template parameter for class enclosing `%D'";
2607 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2608 /* If we're inside a class definition, there's no need to
2609 examine the parameters to the class itself. On the one
2610 hand, they will be checked when the class is defined, and,
2611 on the other, default arguments are valid in things like:
2612 template <class T = double>
2613 struct S { template <class U> void f(U); };
2614 Here the default argument for `S' has no bearing on the
2615 declaration of `f'. */
2616 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2618 /* Check everything. */
2619 last_level_to_check
= 0;
2621 for (parm_level
= parms
;
2622 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2623 parm_level
= TREE_CHAIN (parm_level
))
2625 tree inner_parms
= TREE_VALUE (parm_level
);
2629 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2630 for (i
= 0; i
< ntparms
; ++i
)
2631 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2639 /* Clear out the default argument so that we are not
2641 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2644 /* At this point, if we're still interested in issuing messages,
2645 they must apply to classes surrounding the object declared. */
2647 msg
= "default argument for template parameter for class enclosing `%D'";
2651 /* Worker for push_template_decl_real, called via
2652 for_each_template_parm. DATA is really an int, indicating the
2653 level of the parameters we are interested in. If T is a template
2654 parameter of that level, return nonzero. */
2657 template_parm_this_level_p (tree t
, void* data
)
2659 int this_level
= *(int *)data
;
2662 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2663 level
= TEMPLATE_PARM_LEVEL (t
);
2665 level
= TEMPLATE_TYPE_LEVEL (t
);
2666 return level
== this_level
;
2669 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2670 parameters given by current_template_args, or reuses a
2671 previously existing one, if appropriate. Returns the DECL, or an
2672 equivalent one, if it is replaced via a call to duplicate_decls.
2674 If IS_FRIEND is nonzero, DECL is a friend declaration. */
2677 push_template_decl_real (tree decl
, int is_friend
)
2685 int new_template_p
= 0;
2687 /* See if this is a partial specialization. */
2688 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2689 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2690 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2692 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2695 /* For a friend, we want the context of the friend function, not
2696 the type of which it is a friend. */
2697 ctx
= DECL_CONTEXT (decl
);
2698 else if (CP_DECL_CONTEXT (decl
)
2699 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2700 /* In the case of a virtual function, we want the class in which
2702 ctx
= CP_DECL_CONTEXT (decl
);
2704 /* Otherwise, if we're currently defining some class, the DECL
2705 is assumed to be a member of the class. */
2706 ctx
= current_scope ();
2708 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2711 if (!DECL_CONTEXT (decl
))
2712 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2714 /* See if this is a primary template. */
2715 primary
= template_parm_scope_p ();
2719 if (current_lang_name
== lang_name_c
)
2720 error ("template with C linkage");
2721 else if (TREE_CODE (decl
) == TYPE_DECL
2722 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2723 error ("template class without a name");
2724 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2725 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2726 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2727 || TREE_CODE (decl
) == FUNCTION_DECL
)
2730 error ("template declaration of `%#D'", decl
);
2733 /* Check to see that the rules regarding the use of default
2734 arguments are not being violated. */
2735 check_default_tmpl_args (decl
, current_template_parms
,
2736 primary
, is_partial
);
2739 return process_partial_specialization (decl
);
2741 args
= current_template_args ();
2744 || TREE_CODE (ctx
) == FUNCTION_DECL
2745 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
2746 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2748 if (DECL_LANG_SPECIFIC (decl
)
2749 && DECL_TEMPLATE_INFO (decl
)
2750 && DECL_TI_TEMPLATE (decl
))
2751 tmpl
= DECL_TI_TEMPLATE (decl
);
2752 /* If DECL is a TYPE_DECL for a class-template, then there won't
2753 be DECL_LANG_SPECIFIC. The information equivalent to
2754 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2755 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2756 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2757 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2759 /* Since a template declaration already existed for this
2760 class-type, we must be redeclaring it here. Make sure
2761 that the redeclaration is valid. */
2762 redeclare_class_template (TREE_TYPE (decl
),
2763 current_template_parms
);
2764 /* We don't need to create a new TEMPLATE_DECL; just use the
2765 one we already had. */
2766 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2770 tmpl
= build_template_decl (decl
, current_template_parms
);
2773 if (DECL_LANG_SPECIFIC (decl
)
2774 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2776 /* A specialization of a member template of a template
2778 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2779 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2780 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2786 tree a
, t
, current
, parms
;
2789 if (TREE_CODE (decl
) == TYPE_DECL
)
2791 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2792 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2793 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2794 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2795 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2798 error ("`%D' does not declare a template type", decl
);
2802 else if (!DECL_LANG_SPECIFIC (decl
) || !DECL_TEMPLATE_INFO (decl
))
2804 error ("template definition of non-template `%#D'", decl
);
2808 tmpl
= DECL_TI_TEMPLATE (decl
);
2810 if (is_member_template (tmpl
)
2811 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2812 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2813 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2817 /* The declaration is a specialization of a member
2818 template, declared outside the class. Therefore, the
2819 innermost template arguments will be NULL, so we
2820 replace them with the arguments determined by the
2821 earlier call to check_explicit_specialization. */
2822 args
= DECL_TI_ARGS (decl
);
2825 = build_template_decl (decl
, current_template_parms
);
2826 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2827 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2828 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2829 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2830 DECL_TEMPLATE_INFO (new_tmpl
)
2831 = tree_cons (tmpl
, args
, NULL_TREE
);
2833 register_specialization (new_tmpl
,
2834 most_general_template (tmpl
),
2839 /* Make sure the template headers we got make sense. */
2841 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2842 i
= TMPL_PARMS_DEPTH (parms
);
2843 if (TMPL_ARGS_DEPTH (args
) != i
)
2845 error ("expected %d levels of template parms for `%#D', got %d",
2846 i
, decl
, TMPL_ARGS_DEPTH (args
));
2849 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2851 a
= TMPL_ARGS_LEVEL (args
, i
);
2852 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2854 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2856 if (current
== decl
)
2857 error ("got %d template parameters for `%#D'",
2858 TREE_VEC_LENGTH (a
), decl
);
2860 error ("got %d template parameters for `%#T'",
2861 TREE_VEC_LENGTH (a
), current
);
2862 error (" but %d required", TREE_VEC_LENGTH (t
));
2865 /* Perhaps we should also check that the parms are used in the
2866 appropriate qualifying scopes in the declarator? */
2868 if (current
== decl
)
2871 current
= TYPE_CONTEXT (current
);
2875 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2876 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2878 /* Push template declarations for global functions and types. Note
2879 that we do not try to push a global template friend declared in a
2880 template class; such a thing may well depend on the template
2881 parameters of the class. */
2882 if (new_template_p
&& !ctx
2883 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2884 tmpl
= pushdecl_namespace_level (tmpl
);
2888 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2889 if (DECL_CONV_FN_P (tmpl
))
2891 int depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2893 /* It is a conversion operator. See if the type converted to
2894 depends on innermost template operands. */
2896 if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl
)),
2897 template_parm_this_level_p
,
2900 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
2904 info
= tree_cons (tmpl
, args
, NULL_TREE
);
2906 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2908 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2909 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2910 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2911 /* Don't change the name if we've already set it up. */
2912 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl
)))
2913 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2915 else if (DECL_LANG_SPECIFIC (decl
))
2916 DECL_TEMPLATE_INFO (decl
) = info
;
2918 return DECL_TEMPLATE_RESULT (tmpl
);
2922 push_template_decl (tree decl
)
2924 return push_template_decl_real (decl
, 0);
2927 /* Called when a class template TYPE is redeclared with the indicated
2928 template PARMS, e.g.:
2930 template <class T> struct S;
2931 template <class T> struct S {}; */
2934 redeclare_class_template (tree type
, tree parms
)
2940 if (!TYPE_TEMPLATE_INFO (type
))
2942 error ("`%T' is not a template type", type
);
2946 tmpl
= TYPE_TI_TEMPLATE (type
);
2947 if (!PRIMARY_TEMPLATE_P (tmpl
))
2948 /* The type is nested in some template class. Nothing to worry
2949 about here; there are no new template parameters for the nested
2953 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2954 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2956 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2958 cp_error_at ("previous declaration `%D'", tmpl
);
2959 error ("used %d template parameter%s instead of %d",
2960 TREE_VEC_LENGTH (tmpl_parms
),
2961 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2962 TREE_VEC_LENGTH (parms
));
2966 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2968 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2969 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2970 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2971 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2973 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2975 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2976 error ("redeclared here as `%#D'", parm
);
2980 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2982 /* We have in [temp.param]:
2984 A template-parameter may not be given default arguments
2985 by two different declarations in the same scope. */
2986 error ("redefinition of default argument for `%#D'", parm
);
2987 cp_error_at (" original definition appeared here", tmpl_parm
);
2991 if (parm_default
!= NULL_TREE
)
2992 /* Update the previous template parameters (which are the ones
2993 that will really count) with the new default value. */
2994 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2995 else if (tmpl_default
!= NULL_TREE
)
2996 /* Update the new parameters, too; they'll be used as the
2997 parameters for any members. */
2998 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
3002 /* Attempt to convert the non-type template parameter EXPR to the
3003 indicated TYPE. If the conversion is successful, return the
3004 converted value. If the conversion is unsuccessful, return
3005 NULL_TREE if we issued an error message, or error_mark_node if we
3006 did not. We issue error messages for out-and-out bad template
3007 parameters, but not simply because the conversion failed, since we
3008 might be just trying to do argument deduction. By the time this
3009 function is called, neither TYPE nor EXPR may make use of template
3013 convert_nontype_argument (tree type
, tree expr
)
3015 tree expr_type
= TREE_TYPE (expr
);
3017 /* A template-argument for a non-type, non-template
3018 template-parameter shall be one of:
3020 --an integral constant-expression of integral or enumeration
3023 --the name of a non-type template-parameter; or
3025 --the name of an object or function with external linkage,
3026 including function templates and function template-ids but
3027 excluding non-static class members, expressed as id-expression;
3030 --the address of an object or function with external linkage,
3031 including function templates and function template-ids but
3032 excluding non-static class members, expressed as & id-expression
3033 where the & is optional if the name refers to a function or
3036 --a pointer to member expressed as described in _expr.unary.op_. */
3038 /* An integral constant-expression can include const variables or
3039 enumerators. Simplify things by folding them to their values,
3040 unless we're about to bind the declaration to a reference
3042 if (INTEGRAL_TYPE_P (expr_type
)
3043 && TREE_CODE (type
) != REFERENCE_TYPE
)
3044 expr
= decl_constant_value (expr
);
3046 if (is_overloaded_fn (expr
))
3047 /* OK for now. We'll check that it has external linkage later.
3048 Check this first since if expr_type is the unknown_type_node
3049 we would otherwise complain below. */
3051 else if (TYPE_PTRMEM_P (expr_type
)
3052 || TYPE_PTRMEMFUNC_P (expr_type
))
3054 if (TREE_CODE (expr
) != PTRMEM_CST
)
3057 else if (TYPE_PTR_P (expr_type
)
3058 || TYPE_PTRMEM_P (expr_type
)
3059 || TREE_CODE (expr_type
) == ARRAY_TYPE
3060 || TREE_CODE (type
) == REFERENCE_TYPE
3061 /* If expr is the address of an overloaded function, we
3062 will get the unknown_type_node at this point. */
3063 || expr_type
== unknown_type_node
)
3069 if (TREE_CODE (expr_type
) == ARRAY_TYPE
3070 || (TREE_CODE (type
) == REFERENCE_TYPE
3071 && TREE_CODE (e
) != ADDR_EXPR
))
3075 if (TREE_CODE (e
) != ADDR_EXPR
)
3078 error ("`%E' is not a valid template argument", expr
);
3079 if (TYPE_PTR_P (expr_type
))
3081 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
3082 error ("it must be the address of a function with external linkage");
3084 error ("it must be the address of an object with external linkage");
3086 else if (TYPE_PTRMEM_P (expr_type
)
3087 || TYPE_PTRMEMFUNC_P (expr_type
))
3088 error ("it must be a pointer-to-member of the form `&X::Y'");
3093 referent
= TREE_OPERAND (e
, 0);
3094 STRIP_NOPS (referent
);
3097 if (TREE_CODE (referent
) == STRING_CST
)
3099 error ("string literal %E is not a valid template argument because it is the address of an object with static linkage",
3104 if (is_overloaded_fn (referent
))
3105 /* We'll check that it has external linkage later. */
3107 else if (TREE_CODE (referent
) != VAR_DECL
)
3109 else if (!DECL_EXTERNAL_LINKAGE_P (referent
))
3111 error ("address of non-extern `%E' cannot be used as template argument", referent
);
3112 return error_mark_node
;
3115 else if (INTEGRAL_TYPE_P (expr_type
)
3116 || TYPE_PTRMEM_P (expr_type
)
3117 || TYPE_PTRMEMFUNC_P (expr_type
))
3119 if (! TREE_CONSTANT (expr
))
3122 error ("non-constant `%E' cannot be used as template argument",
3129 error ("object `%E' cannot be used as template argument", expr
);
3133 switch (TREE_CODE (type
))
3138 /* For a non-type template-parameter of integral or enumeration
3139 type, integral promotions (_conv.prom_) and integral
3140 conversions (_conv.integral_) are applied. */
3141 if (!INTEGRAL_TYPE_P (expr_type
))
3142 return error_mark_node
;
3144 /* It's safe to call digest_init in this case; we know we're
3145 just converting one integral constant expression to another. */
3146 expr
= digest_init (type
, expr
, (tree
*) 0);
3148 if (TREE_CODE (expr
) != INTEGER_CST
)
3149 /* Curiously, some TREE_CONSTANT integral expressions do not
3150 simplify to integer constants. For example, `3 % 0',
3151 remains a TRUNC_MOD_EXPR. */
3158 tree type_pointed_to
= TREE_TYPE (type
);
3160 if (TYPE_PTRMEM_P (type
))
3164 /* For a non-type template-parameter of type pointer to data
3165 member, qualification conversions (_conv.qual_) are
3167 e
= perform_qualification_conversions (type
, expr
);
3168 if (TREE_CODE (e
) == NOP_EXPR
)
3169 /* The call to perform_qualification_conversions will
3170 insert a NOP_EXPR over EXPR to do express conversion,
3171 if necessary. But, that will confuse us if we use
3172 this (converted) template parameter to instantiate
3173 another template; then the thing will not look like a
3174 valid template argument. So, just make a new
3175 constant, of the appropriate type. */
3176 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
3179 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
3181 /* For a non-type template-parameter of type pointer to
3182 function, only the function-to-pointer conversion
3183 (_conv.func_) is applied. If the template-argument
3184 represents a set of overloaded functions (or a pointer to
3185 such), the matching function is selected from the set
3190 if (TREE_CODE (expr
) == ADDR_EXPR
)
3191 fns
= TREE_OPERAND (expr
, 0);
3195 fn
= instantiate_type (type_pointed_to
, fns
, tf_none
);
3197 if (fn
== error_mark_node
)
3198 return error_mark_node
;
3200 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3202 if (really_overloaded_fn (fns
))
3203 return error_mark_node
;
3208 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
3210 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3216 /* For a non-type template-parameter of type pointer to
3217 object, qualification conversions (_conv.qual_) and the
3218 array-to-pointer conversion (_conv.array_) are applied.
3219 [Note: In particular, neither the null pointer conversion
3220 (_conv.ptr_) nor the derived-to-base conversion
3221 (_conv.ptr_) are applied. Although 0 is a valid
3222 template-argument for a non-type template-parameter of
3223 integral type, it is not a valid template-argument for a
3224 non-type template-parameter of pointer type.]
3226 The call to decay_conversion performs the
3227 array-to-pointer conversion, if appropriate. */
3228 expr
= decay_conversion (expr
);
3230 if (expr
== error_mark_node
)
3231 return error_mark_node
;
3233 return perform_qualification_conversions (type
, expr
);
3238 case REFERENCE_TYPE
:
3240 tree type_referred_to
= TREE_TYPE (type
);
3242 /* If this expression already has reference type, get the
3243 underling object. */
3244 if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
3246 my_friendly_assert (TREE_CODE (expr
) == ADDR_EXPR
, 20000604);
3247 expr
= TREE_OPERAND (expr
, 0);
3248 expr_type
= TREE_TYPE (expr
);
3251 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
3253 /* For a non-type template-parameter of type reference to
3254 function, no conversions apply. If the
3255 template-argument represents a set of overloaded
3256 functions, the matching function is selected from the
3257 set (_over.over_). */
3260 fn
= instantiate_type (type_referred_to
, expr
, tf_none
);
3262 if (fn
== error_mark_node
)
3263 return error_mark_node
;
3265 if (!DECL_EXTERNAL_LINKAGE_P (fn
))
3267 if (really_overloaded_fn (expr
))
3268 /* Don't issue an error here; we might get a different
3269 function if the overloading had worked out
3271 return error_mark_node
;
3276 my_friendly_assert (same_type_p (type_referred_to
,
3284 /* For a non-type template-parameter of type reference to
3285 object, no conversions apply. The type referred to by the
3286 reference may be more cv-qualified than the (otherwise
3287 identical) type of the template-argument. The
3288 template-parameter is bound directly to the
3289 template-argument, which must be an lvalue. */
3290 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type
),
3291 TYPE_MAIN_VARIANT (type_referred_to
))
3292 || !at_least_as_qualified_p (type_referred_to
,
3294 || !real_lvalue_p (expr
))
3295 return error_mark_node
;
3298 cxx_mark_addressable (expr
);
3299 return build1 (ADDR_EXPR
, type
, expr
);
3305 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 20010112);
3307 /* For a non-type template-parameter of type pointer to member
3308 function, no conversions apply. If the template-argument
3309 represents a set of overloaded member functions, the
3310 matching member function is selected from the set
3313 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3314 expr_type
!= unknown_type_node
)
3315 return error_mark_node
;
3317 if (TREE_CODE (expr
) == PTRMEM_CST
)
3319 /* A ptr-to-member constant. */
3320 if (!same_type_p (type
, expr_type
))
3321 return error_mark_node
;
3326 if (TREE_CODE (expr
) != ADDR_EXPR
)
3327 return error_mark_node
;
3329 expr
= instantiate_type (type
, expr
, tf_none
);
3331 if (expr
== error_mark_node
)
3332 return error_mark_node
;
3334 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3341 /* All non-type parameters must have one of these types. */
3346 return error_mark_node
;
3349 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3350 template template parameters. Both PARM_PARMS and ARG_PARMS are
3351 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3354 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3355 the case, then extra parameters must have default arguments.
3357 Consider the example:
3358 template <class T, class Allocator = allocator> class vector;
3359 template<template <class U> class TT> class C;
3361 C<vector> is a valid instantiation. PARM_PARMS for the above code
3362 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3363 T and Allocator) and OUTER_ARGS contains the argument that is used to
3364 substitute the TT parameter. */
3367 coerce_template_template_parms (tree parm_parms
,
3369 tsubst_flags_t complain
,
3373 int nparms
, nargs
, i
;
3376 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3377 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3379 nparms
= TREE_VEC_LENGTH (parm_parms
);
3380 nargs
= TREE_VEC_LENGTH (arg_parms
);
3382 /* The rule here is opposite of coerce_template_parms. */
3385 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3388 for (i
= 0; i
< nparms
; ++i
)
3390 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3391 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3393 if (arg
== NULL_TREE
|| arg
== error_mark_node
3394 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3397 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3400 switch (TREE_CODE (parm
))
3406 /* We encounter instantiations of templates like
3407 template <template <template <class> class> class TT>
3410 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3411 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3413 if (!coerce_template_template_parms (parmparm
, argparm
,
3421 /* The tsubst call is used to handle cases such as
3422 template <class T, template <T> class TT> class D;
3423 i.e. the parameter list of TT depends on earlier parameters. */
3424 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3437 /* Convert the indicated template ARG as necessary to match the
3438 indicated template PARM. Returns the converted ARG, or
3439 error_mark_node if the conversion was unsuccessful. Error and
3440 warning messages are issued under control of COMPLAIN. This
3441 conversion is for the Ith parameter in the parameter list. ARGS is
3442 the full set of template arguments deduced so far. */
3445 convert_template_argument (tree parm
,
3448 tsubst_flags_t complain
,
3454 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3456 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3458 if (TREE_CODE (arg
) == TREE_LIST
3459 && TREE_TYPE (arg
) != NULL_TREE
3460 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3462 /* The template argument was the name of some
3463 member function. That's usually
3464 invalid, but static members are OK. In any
3465 case, grab the underlying fields/functions
3466 and issue an error later if required. */
3467 arg
= TREE_VALUE (arg
);
3468 TREE_TYPE (arg
) = unknown_type_node
;
3471 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3472 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3473 || requires_tmpl_type
);
3475 if (TREE_CODE (arg
) != RECORD_TYPE
)
3476 is_tmpl_type
= ((TREE_CODE (arg
) == TEMPLATE_DECL
3477 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3478 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3479 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
3480 else if (CLASSTYPE_TEMPLATE_INFO (arg
) && !CLASSTYPE_USE_TEMPLATE (arg
)
3481 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg
)))
3483 if (is_base_of_enclosing_class (arg
, current_class_type
))
3484 /* This is a template name used within the scope of the
3485 template. It could be the template, or it could be the
3486 instantiation. Choose whichever makes sense. */
3487 is_tmpl_type
= requires_tmpl_type
;
3492 /* It is a non-template class, or a specialization of a template
3493 class, or a non-template member of a template class. */
3497 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3498 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
3499 arg
= TYPE_STUB_DECL (arg
);
3500 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3501 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3503 is_type
= TYPE_P (arg
) || is_tmpl_type
;
3505 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3506 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3508 pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg
);
3510 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3511 TREE_OPERAND (arg
, 1),
3512 complain
& tf_error
);
3515 if (is_type
!= requires_type
)
3519 if (complain
& tf_error
)
3521 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3524 error (" expected a constant of type `%T', got `%T'",
3526 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3527 else if (requires_tmpl_type
)
3528 error (" expected a class template, got `%E'", arg
);
3530 error (" expected a type, got `%E'", arg
);
3533 return error_mark_node
;
3535 if (is_tmpl_type
^ requires_tmpl_type
)
3537 if (in_decl
&& (complain
& tf_error
))
3539 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3542 error (" expected a type, got `%T'", DECL_NAME (arg
));
3544 error (" expected a class template, got `%T'", arg
);
3546 return error_mark_node
;
3551 if (requires_tmpl_type
)
3553 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
3554 /* The number of argument required is not known yet.
3555 Just accept it for now. */
3556 val
= TREE_TYPE (arg
);
3559 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3560 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3562 if (coerce_template_template_parms (parmparm
, argparm
,
3568 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3570 if (val
!= error_mark_node
3571 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3572 val
= TREE_TYPE (val
);
3576 if (in_decl
&& (complain
& tf_error
))
3578 error ("type/value mismatch at argument %d in template parameter list for `%D'",
3580 error (" expected a template of type `%D', got `%D'", parm
, arg
);
3583 val
= error_mark_node
;
3588 val
= groktypename (arg
);
3592 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3594 if (invalid_nontype_parm_type_p (t
, complain
))
3595 return error_mark_node
;
3597 if (processing_template_decl
)
3598 arg
= maybe_fold_nontype_arg (arg
);
3600 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3601 /* We used to call digest_init here. However, digest_init
3602 will report errors, which we don't want when complain
3603 is zero. More importantly, digest_init will try too
3604 hard to convert things: for example, `0' should not be
3605 converted to pointer type at this point according to
3606 the standard. Accepting this is not merely an
3607 extension, since deciding whether or not these
3608 conversions can occur is part of determining which
3609 function template to call, or whether a given explicit
3610 argument specification is valid. */
3611 val
= convert_nontype_argument (t
, arg
);
3615 if (val
== NULL_TREE
)
3616 val
= error_mark_node
;
3617 else if (val
== error_mark_node
&& (complain
& tf_error
))
3618 error ("could not convert template argument `%E' to `%T'",
3625 /* Convert all template arguments to their appropriate types, and
3626 return a vector containing the innermost resulting template
3627 arguments. If any error occurs, return error_mark_node. Error and
3628 warning messages are issued under control of COMPLAIN.
3630 If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be
3631 provided in ARGLIST, or else trailing parameters must have default
3632 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3633 deduction for any unspecified trailing arguments. */
3636 coerce_template_parms (tree parms
,
3639 tsubst_flags_t complain
,
3640 int require_all_arguments
)
3642 int nparms
, nargs
, i
, lost
= 0;
3645 tree new_inner_args
;
3647 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
3648 nargs
= NUM_TMPL_ARGS (inner_args
);
3649 nparms
= TREE_VEC_LENGTH (parms
);
3653 && require_all_arguments
3654 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3656 if (complain
& tf_error
)
3658 error ("wrong number of template arguments (%d, should be %d)",
3662 cp_error_at ("provided for `%D'", in_decl
);
3665 return error_mark_node
;
3668 new_inner_args
= make_tree_vec (nparms
);
3669 new_args
= add_outermost_template_args (args
, new_inner_args
);
3670 for (i
= 0; i
< nparms
; i
++)
3675 /* Get the Ith template parameter. */
3676 parm
= TREE_VEC_ELT (parms
, i
);
3678 /* Calculate the Ith argument. */
3679 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3681 arg
= TREE_VALUE (inner_args
);
3682 inner_args
= TREE_CHAIN (inner_args
);
3685 arg
= TREE_VEC_ELT (inner_args
, i
);
3686 /* If no template argument was supplied, look for a default
3688 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3690 /* There was no default value. */
3691 my_friendly_assert (!require_all_arguments
, 0);
3694 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3695 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3697 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3700 /* Now, convert the Ith argument, as necessary. */
3701 if (arg
== NULL_TREE
)
3702 /* We're out of arguments. */
3704 my_friendly_assert (!require_all_arguments
, 0);
3707 else if (arg
== error_mark_node
)
3709 error ("template argument %d is invalid", i
+ 1);
3710 arg
= error_mark_node
;
3713 arg
= convert_template_argument (TREE_VALUE (parm
),
3714 arg
, new_args
, complain
, i
,
3717 if (arg
== error_mark_node
)
3719 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3723 return error_mark_node
;
3725 return new_inner_args
;
3728 /* Returns 1 if template args OT and NT are equivalent. */
3731 template_args_equal (tree ot
, tree nt
)
3736 if (TREE_CODE (nt
) == TREE_VEC
)
3737 /* For member templates */
3738 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
3739 else if (TYPE_P (nt
))
3740 return TYPE_P (ot
) && same_type_p (ot
, nt
);
3741 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
3744 return cp_tree_equal (ot
, nt
);
3747 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3748 of template arguments. Returns 0 otherwise. */
3751 comp_template_args (tree oldargs
, tree newargs
)
3755 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3758 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3760 tree nt
= TREE_VEC_ELT (newargs
, i
);
3761 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3763 if (! template_args_equal (ot
, nt
))
3769 /* Given class template name and parameter list, produce a user-friendly name
3770 for the instantiation. */
3773 mangle_class_name_for_template (const char* name
, tree parms
, tree arglist
)
3775 static struct obstack scratch_obstack
;
3776 static char *scratch_firstobj
;
3779 if (!scratch_firstobj
)
3780 gcc_obstack_init (&scratch_obstack
);
3782 obstack_free (&scratch_obstack
, scratch_firstobj
);
3783 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3785 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
3786 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
3790 nparms
= TREE_VEC_LENGTH (parms
);
3791 arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
3792 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3793 for (i
= 0; i
< nparms
; i
++)
3795 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3796 tree arg
= TREE_VEC_ELT (arglist
, i
);
3801 if (TREE_CODE (parm
) == TYPE_DECL
)
3803 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3806 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3808 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3810 /* Already substituted with real template. Just output
3811 the template name here */
3812 tree context
= DECL_CONTEXT (arg
);
3815 /* The template may be defined in a namespace, or
3816 may be a member template. */
3817 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3818 || CLASS_TYPE_P (context
),
3820 cat(decl_as_string (DECL_CONTEXT (arg
), TFF_PLAIN_IDENTIFIER
));
3823 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3826 /* Output the parameter declaration */
3827 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
3831 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3833 if (TREE_CODE (arg
) == TREE_LIST
)
3835 /* New list cell was built because old chain link was in
3837 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3838 arg
= TREE_VALUE (arg
);
3840 /* No need to check arglist against parmlist here; we did that
3841 in coerce_template_parms, called from lookup_template_class. */
3842 cat (expr_as_string (arg
, TFF_PLAIN_IDENTIFIER
));
3845 char *bufp
= obstack_next_free (&scratch_obstack
);
3847 while (bufp
[offset
- 1] == ' ')
3849 obstack_blank_fast (&scratch_obstack
, offset
);
3851 /* B<C<char> >, not B<C<char>> */
3852 if (bufp
[offset
- 1] == '>')
3857 return (char *) obstack_base (&scratch_obstack
);
3861 classtype_mangled_name (tree t
)
3863 if (CLASSTYPE_TEMPLATE_INFO (t
)
3864 /* Specializations have already had their names set up in
3865 lookup_template_class. */
3866 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3868 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3870 /* For non-primary templates, the template parameters are
3871 implicit from their surrounding context. */
3872 if (PRIMARY_TEMPLATE_P (tmpl
))
3874 tree name
= DECL_NAME (tmpl
);
3875 char *mangled_name
= mangle_class_name_for_template
3876 (IDENTIFIER_POINTER (name
),
3877 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3878 CLASSTYPE_TI_ARGS (t
));
3879 tree id
= get_identifier (mangled_name
);
3880 IDENTIFIER_TEMPLATE (id
) = name
;
3885 return TYPE_IDENTIFIER (t
);
3889 add_pending_template (tree d
)
3891 tree ti
= (TYPE_P (d
)
3892 ? CLASSTYPE_TEMPLATE_INFO (d
)
3893 : DECL_TEMPLATE_INFO (d
));
3897 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3900 /* We are called both from instantiate_decl, where we've already had a
3901 tinst_level pushed, and instantiate_template, where we haven't.
3903 level
= !(current_tinst_level
&& TINST_DECL (current_tinst_level
) == d
);
3906 push_tinst_level (d
);
3908 pt
= tree_cons (current_tinst_level
, d
, NULL_TREE
);
3909 if (last_pending_template
)
3910 TREE_CHAIN (last_pending_template
) = pt
;
3912 pending_templates
= pt
;
3914 last_pending_template
= pt
;
3916 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3923 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
3924 ARGLIST. Valid choices for FNS are given in the cp-tree.def
3925 documentation for TEMPLATE_ID_EXPR. */
3928 lookup_template_function (tree fns
, tree arglist
)
3932 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
3933 return error_mark_node
;
3935 if (fns
== NULL_TREE
)
3937 error ("non-template used as template");
3938 return error_mark_node
;
3941 my_friendly_assert (TREE_CODE (fns
) == TEMPLATE_DECL
3942 || TREE_CODE (fns
) == OVERLOAD
3944 || TREE_CODE (fns
) == IDENTIFIER_NODE
3945 || TREE_CODE (fns
) == LOOKUP_EXPR
,
3948 if (BASELINK_P (fns
))
3950 BASELINK_FUNCTIONS (fns
) = build (TEMPLATE_ID_EXPR
,
3952 BASELINK_FUNCTIONS (fns
),
3957 type
= TREE_TYPE (fns
);
3958 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3959 type
= unknown_type_node
;
3961 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3964 /* Within the scope of a template class S<T>, the name S gets bound
3965 (in build_self_reference) to a TYPE_DECL for the class, not a
3966 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3967 or one of its enclosing classes, and that type is a template,
3968 return the associated TEMPLATE_DECL. Otherwise, the original
3969 DECL is returned. */
3972 maybe_get_template_decl_from_type_decl (tree decl
)
3974 return (decl
!= NULL_TREE
3975 && TREE_CODE (decl
) == TYPE_DECL
3976 && DECL_ARTIFICIAL (decl
)
3977 && CLASS_TYPE_P (TREE_TYPE (decl
))
3978 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3979 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3982 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3983 parameters, find the desired type.
3985 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3986 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3987 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3990 IN_DECL, if non-NULL, is the template declaration we are trying to
3993 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
3994 the class we are looking up.
3996 Issue error and warning messages under control of COMPLAIN.
3998 If the template class is really a local class in a template
3999 function, then the FUNCTION_CONTEXT is the function in which it is
4000 being instantiated. */
4003 lookup_template_class (tree d1
,
4008 tsubst_flags_t complain
)
4010 tree
template = NULL_TREE
, parmlist
;
4013 timevar_push (TV_NAME_LOOKUP
);
4014 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
4016 if (IDENTIFIER_VALUE (d1
)
4017 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
4018 template = IDENTIFIER_VALUE (d1
);
4022 push_decl_namespace (context
);
4023 template = lookup_name (d1
, /*prefer_type=*/0);
4024 template = maybe_get_template_decl_from_type_decl (template);
4026 pop_decl_namespace ();
4029 context
= DECL_CONTEXT (template);
4031 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
4033 tree type
= TREE_TYPE (d1
);
4035 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4036 an implicit typename for the second A. Deal with it. */
4037 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
4038 type
= TREE_TYPE (type
);
4040 if (CLASSTYPE_TEMPLATE_INFO (type
))
4042 template = CLASSTYPE_TI_TEMPLATE (type
);
4043 d1
= DECL_NAME (template);
4046 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
4047 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
4049 template = TYPE_TI_TEMPLATE (d1
);
4050 d1
= DECL_NAME (template);
4052 else if (TREE_CODE (d1
) == TEMPLATE_DECL
4053 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
4056 d1
= DECL_NAME (template);
4057 context
= DECL_CONTEXT (template);
4060 /* With something like `template <class T> class X class X { ... };'
4061 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
4062 We don't want to do that, but we have to deal with the situation,
4063 so let's give them some syntax errors to chew on instead of a
4064 crash. Alternatively D1 might not be a template type at all. */
4067 if (complain
& tf_error
)
4068 error ("`%T' is not a template", d1
);
4069 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4072 if (TREE_CODE (template) != TEMPLATE_DECL
4073 /* If we're called from the parser, make sure it's a user visible
4075 || ((!arglist
|| TREE_CODE (arglist
) == TREE_LIST
)
4076 && !DECL_TEMPLATE_PARM_P (template)
4077 && !PRIMARY_TEMPLATE_P (template)))
4079 if (complain
& tf_error
)
4081 error ("non-template type `%T' used as a template", d1
);
4083 cp_error_at ("for template declaration `%D'", in_decl
);
4085 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4088 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4090 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4091 template arguments */
4096 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
4098 /* Consider an example where a template template parameter declared as
4100 template <class T, class U = std::allocator<T> > class TT
4102 The template parameter level of T and U are one level larger than
4103 of TT. To proper process the default argument of U, say when an
4104 instantiation `TT<int>' is seen, we need to build the full
4105 arguments containing {int} as the innermost level. Outer levels,
4106 available when not appearing as default template argument, can be
4107 obtained from `current_template_args ()'.
4109 Suppose that TT is later substituted with std::vector. The above
4110 instantiation is `TT<int, std::allocator<T> >' with TT at
4111 level 1, and T at level 2, while the template arguments at level 1
4112 becomes {std::vector} and the inner level 2 is {int}. */
4114 if (current_template_parms
)
4115 arglist
= add_to_template_args (current_template_args (), arglist
);
4117 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
4118 complain
, /*require_all_args=*/1);
4119 if (arglist2
== error_mark_node
4120 || (!uses_template_parms (arglist2
)
4121 && check_instantiated_args (template, arglist2
, complain
)))
4122 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4124 parm
= bind_template_template_parm (TREE_TYPE (template), arglist2
);
4125 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
4129 tree template_type
= TREE_TYPE (template);
4132 tree found
= NULL_TREE
;
4136 int is_partial_instantiation
;
4138 gen_tmpl
= most_general_template (template);
4139 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
4140 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
4141 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
4143 if (arg_depth
== 1 && parm_depth
> 1)
4145 /* We've been given an incomplete set of template arguments.
4148 template <class T> struct S1 {
4149 template <class U> struct S2 {};
4150 template <class U> struct S2<U*> {};
4153 we will be called with an ARGLIST of `U*', but the
4154 TEMPLATE will be `template <class T> template
4155 <class U> struct S1<T>::S2'. We must fill in the missing
4158 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4160 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
4163 /* Now we should have enough arguments. */
4164 my_friendly_assert (parm_depth
== arg_depth
, 0);
4166 /* From here on, we're only interested in the most general
4168 template = gen_tmpl
;
4170 /* Calculate the BOUND_ARGS. These will be the args that are
4171 actually tsubst'd into the definition to create the
4175 /* We have multiple levels of arguments to coerce, at once. */
4177 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
4179 tree bound_args
= make_tree_vec (parm_depth
);
4181 for (i
= saved_depth
,
4182 t
= DECL_TEMPLATE_PARMS (template);
4183 i
> 0 && t
!= NULL_TREE
;
4184 --i
, t
= TREE_CHAIN (t
))
4186 tree a
= coerce_template_parms (TREE_VALUE (t
),
4188 complain
, /*require_all_args=*/1);
4190 /* Don't process further if one of the levels fails. */
4191 if (a
== error_mark_node
)
4193 /* Restore the ARGLIST to its full size. */
4194 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4195 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4198 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
4200 /* We temporarily reduce the length of the ARGLIST so
4201 that coerce_template_parms will see only the arguments
4202 corresponding to the template parameters it is
4204 TREE_VEC_LENGTH (arglist
)--;
4207 /* Restore the ARGLIST to its full size. */
4208 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4210 arglist
= bound_args
;
4214 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
4215 INNERMOST_TEMPLATE_ARGS (arglist
),
4217 complain
, /*require_all_args=*/1);
4219 if (arglist
== error_mark_node
4220 || (!uses_template_parms (INNERMOST_TEMPLATE_ARGS (arglist
))
4221 && check_instantiated_args (template,
4222 INNERMOST_TEMPLATE_ARGS (arglist
),
4224 /* We were unable to bind the arguments. */
4225 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4227 /* In the scope of a template class, explicit references to the
4228 template class refer to the type of the template, not any
4229 instantiation of it. For example, in:
4231 template <class T> class C { void f(C<T>); }
4233 the `C<T>' is just the same as `C'. Outside of the
4234 class, however, such a reference is an instantiation. */
4235 if (comp_template_args (TYPE_TI_ARGS (template_type
),
4238 found
= template_type
;
4240 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
4244 for (ctx
= current_class_type
;
4246 ctx
= TYPE_CONTEXT (ctx
))
4248 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4250 if (same_type_p (ctx
, template_type
))
4254 /* We're not in the scope of the class, so the
4255 TEMPLATE_TYPE is not the type we want after all. */
4261 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
4263 for (tp
= &DECL_TEMPLATE_INSTANTIATIONS (template);
4265 tp
= &TREE_CHAIN (*tp
))
4266 if (comp_template_args (TREE_PURPOSE (*tp
), arglist
))
4270 /* Use the move-to-front heuristic to speed up future
4272 *tp
= TREE_CHAIN (*tp
);
4274 = DECL_TEMPLATE_INSTANTIATIONS (template);
4275 DECL_TEMPLATE_INSTANTIATIONS (template) = found
;
4277 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, TREE_VALUE (found
));
4280 /* This type is a "partial instantiation" if any of the template
4281 arguments still involve template parameters. Note that we set
4282 IS_PARTIAL_INSTANTIATION for partial specializations as
4284 is_partial_instantiation
= uses_template_parms (arglist
);
4286 if (!is_partial_instantiation
4287 && !PRIMARY_TEMPLATE_P (template)
4288 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
4290 found
= xref_tag_from_type (TREE_TYPE (template),
4291 DECL_NAME (template),
4293 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
4296 context
= tsubst (DECL_CONTEXT (template), arglist
,
4299 context
= global_namespace
;
4301 /* Create the type. */
4302 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
4304 if (!is_partial_instantiation
)
4306 set_current_access_from_decl (TYPE_NAME (template_type
));
4307 t
= start_enum (TYPE_IDENTIFIER (template_type
));
4310 /* We don't want to call start_enum for this type, since
4311 the values for the enumeration constants may involve
4312 template parameters. And, no one should be interested
4313 in the enumeration constants for such a type. */
4314 t
= make_node (ENUMERAL_TYPE
);
4318 t
= make_aggr_type (TREE_CODE (template_type
));
4319 CLASSTYPE_DECLARED_CLASS (t
)
4320 = CLASSTYPE_DECLARED_CLASS (template_type
);
4321 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
4322 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
4323 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
4325 /* A local class. Make sure the decl gets registered properly. */
4326 if (context
== current_function_decl
)
4327 pushtag (DECL_NAME (template), t
, 0);
4330 /* If we called start_enum or pushtag above, this information
4331 will already be set up. */
4334 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4336 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
4337 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
4338 TYPE_STUB_DECL (t
) = type_decl
;
4339 DECL_SOURCE_LOCATION (type_decl
)
4340 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
4343 type_decl
= TYPE_NAME (t
);
4345 TREE_PRIVATE (type_decl
)
4346 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
4347 TREE_PROTECTED (type_decl
)
4348 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
4350 /* Set up the template information. We have to figure out which
4351 template is the immediate parent if this is a full
4353 if (parm_depth
== 1 || is_partial_instantiation
4354 || !PRIMARY_TEMPLATE_P (template))
4355 /* This case is easy; there are no member templates involved. */
4359 /* This is a full instantiation of a member template. Look
4360 for a partial instantiation of which this is an instance. */
4362 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4363 found
; found
= TREE_CHAIN (found
))
4366 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4368 /* We only want partial instantiations, here, not
4369 specializations or full instantiations. */
4370 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4371 || !uses_template_parms (TREE_VALUE (found
)))
4374 /* Temporarily reduce by one the number of levels in the
4375 ARGLIST and in FOUND so as to avoid comparing the
4376 last set of arguments. */
4377 TREE_VEC_LENGTH (arglist
)--;
4378 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4380 /* See if the arguments match. If they do, then TMPL is
4381 the partial instantiation we want. */
4382 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4384 /* Restore the argument vectors to their full size. */
4385 TREE_VEC_LENGTH (arglist
)++;
4386 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4397 /* There was no partial instantiation. This happens
4398 where C<T> is a member template of A<T> and it's used
4401 template <typename T> struct B { A<T>::C<int> m; };
4404 Create the partial instantiation.
4406 TREE_VEC_LENGTH (arglist
)--;
4407 found
= tsubst (template, arglist
, complain
, NULL_TREE
);
4408 TREE_VEC_LENGTH (arglist
)++;
4412 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
4413 DECL_TEMPLATE_INSTANTIATIONS (template)
4414 = tree_cons (arglist
, t
,
4415 DECL_TEMPLATE_INSTANTIATIONS (template));
4417 if (TREE_CODE (t
) == ENUMERAL_TYPE
4418 && !is_partial_instantiation
)
4419 /* Now that the type has been registered on the instantiations
4420 list, we set up the enumerators. Because the enumeration
4421 constants may involve the enumeration type itself, we make
4422 sure to register the type first, and then create the
4423 constants. That way, doing tsubst_expr for the enumeration
4424 constants won't result in recursive calls here; we'll find
4425 the instantiation and exit above. */
4426 tsubst_enum (template_type
, t
, arglist
);
4428 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4430 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4431 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4432 if (!is_partial_instantiation
)
4434 /* For backwards compatibility; code that uses
4435 -fexternal-templates expects looking up a template to
4436 instantiate it. I think DDD still relies on this.
4437 (jason 8/20/1998) */
4438 if (TREE_CODE (t
) != ENUMERAL_TYPE
4439 && flag_external_templates
4440 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4441 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4442 add_pending_template (t
);
4445 /* If the type makes use of template parameters, the
4446 code that generates debugging information will crash. */
4447 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4449 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4451 timevar_pop (TV_NAME_LOOKUP
);
4461 /* Called from for_each_template_parm via walk_tree. */
4464 for_each_template_parm_r (tree
* tp
, int* walk_subtrees
, void* d
)
4467 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
4468 tree_fn_t fn
= pfd
->fn
;
4469 void *data
= pfd
->data
;
4472 /* If we have already visited this tree, there's no need to walk
4473 subtrees. Otherwise, add it to the visited table. */
4474 slot
= htab_find_slot (pfd
->visited
, *tp
, INSERT
);
4483 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
))
4484 return error_mark_node
;
4486 switch (TREE_CODE (t
))
4489 if (TYPE_PTRMEMFUNC_P (t
))
4495 if (!TYPE_TEMPLATE_INFO (t
))
4497 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
4498 fn
, data
, pfd
->visited
))
4499 return error_mark_node
;
4503 /* Since we're not going to walk subtrees, we have to do this
4505 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
4507 return error_mark_node
;
4511 /* Check the return type. */
4512 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
4513 return error_mark_node
;
4515 /* Check the parameter types. Since default arguments are not
4516 instantiated until they are needed, the TYPE_ARG_TYPES may
4517 contain expressions that involve template parameters. But,
4518 no-one should be looking at them yet. And, once they're
4519 instantiated, they don't contain template parameters, so
4520 there's no point in looking at them then, either. */
4524 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4525 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
4527 return error_mark_node
;
4529 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4530 want walk_tree walking into them itself. */
4536 if (for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
4538 return error_mark_node
;
4543 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4544 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
4546 return error_mark_node
;
4551 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
4552 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
4554 return error_mark_node
;
4555 if (DECL_CONTEXT (t
)
4556 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
4558 return error_mark_node
;
4561 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4562 /* Record template parameters such as `T' inside `TT<T>'. */
4563 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
))
4564 return error_mark_node
;
4567 case TEMPLATE_TEMPLATE_PARM
:
4568 case TEMPLATE_TYPE_PARM
:
4569 case TEMPLATE_PARM_INDEX
:
4570 if (fn
&& (*fn
)(t
, data
))
4571 return error_mark_node
;
4573 return error_mark_node
;
4577 /* A template template parameter is encountered */
4578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
4579 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
4580 return error_mark_node
;
4582 /* Already substituted template template parameter */
4588 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
4589 data
, pfd
->visited
))
4590 return error_mark_node
;
4594 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
4595 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4596 (TREE_TYPE (t
)), fn
, data
,
4598 return error_mark_node
;
4603 /* If there's no type, then this thing must be some expression
4604 involving template parameters. */
4605 if (!fn
&& !TREE_TYPE (t
))
4606 return error_mark_node
;
4611 case REINTERPRET_CAST_EXPR
:
4612 case CONST_CAST_EXPR
:
4613 case STATIC_CAST_EXPR
:
4614 case DYNAMIC_CAST_EXPR
:
4619 case PSEUDO_DTOR_EXPR
:
4621 return error_mark_node
;
4625 /* If we do not handle this case specially, we end up walking
4626 the BINFO hierarchy, which is circular, and therefore
4627 confuses walk_tree. */
4629 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp
), fn
, data
,
4631 return error_mark_node
;
4638 /* We didn't find any template parameters we liked. */
4642 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4643 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4644 call FN with the parameter and the DATA.
4645 If FN returns nonzero, the iteration is terminated, and
4646 for_each_template_parm returns 1. Otherwise, the iteration
4647 continues. If FN never returns a nonzero value, the value
4648 returned by for_each_template_parm is 0. If FN is NULL, it is
4649 considered to be the function which always returns 1. */
4652 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
, htab_t visited
)
4654 struct pair_fn_data pfd
;
4661 /* Walk the tree. (Conceptually, we would like to walk without
4662 duplicates, but for_each_template_parm_r recursively calls
4663 for_each_template_parm, so we would need to reorganize a fair
4664 bit to use walk_tree_without_duplicates, so we keep our own
4667 pfd
.visited
= visited
;
4669 pfd
.visited
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
,
4671 result
= walk_tree (&t
,
4672 for_each_template_parm_r
,
4678 htab_delete (pfd
.visited
);
4684 uses_template_parms (tree t
)
4686 return for_each_template_parm (t
, 0, 0, NULL
);
4689 static int tinst_depth
;
4690 extern int max_tinst_depth
;
4691 #ifdef GATHER_STATISTICS
4694 static int tinst_level_tick
;
4695 static int last_template_error_tick
;
4697 /* We're starting to instantiate D; record the template instantiation context
4698 for diagnostics and to restore it later. */
4701 push_tinst_level (tree d
)
4705 if (tinst_depth
>= max_tinst_depth
)
4707 /* If the instantiation in question still has unbound template parms,
4708 we don't really care if we can't instantiate it, so just return.
4709 This happens with base instantiation for implicit `typename'. */
4710 if (uses_template_parms (d
))
4713 last_template_error_tick
= tinst_level_tick
;
4714 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
4715 max_tinst_depth
, d
);
4717 print_instantiation_context ();
4722 new = build_expr_wfl (d
, input_filename
, input_line
, 0);
4723 TREE_CHAIN (new) = current_tinst_level
;
4724 current_tinst_level
= new;
4727 #ifdef GATHER_STATISTICS
4728 if (tinst_depth
> depth_reached
)
4729 depth_reached
= tinst_depth
;
4736 /* We're done instantiating this template; return to the instantiation
4740 pop_tinst_level (void)
4742 tree old
= current_tinst_level
;
4744 /* Restore the filename and line number stashed away when we started
4745 this instantiation. */
4746 input_line
= TINST_LINE (old
);
4747 input_filename
= TINST_FILE (old
);
4748 extract_interface_info ();
4750 current_tinst_level
= TREE_CHAIN (old
);
4755 /* We're instantiating a deferred template; restore the template
4756 instantiation context in which the instantiation was requested, which
4757 is one step out from LEVEL. */
4760 reopen_tinst_level (tree level
)
4765 for (t
= level
; t
; t
= TREE_CHAIN (t
))
4768 current_tinst_level
= level
;
4772 /* Return the outermost template instantiation context, for use with
4773 -falt-external-templates. */
4776 tinst_for_decl (void)
4778 tree p
= current_tinst_level
;
4781 for (; TREE_CHAIN (p
) ; p
= TREE_CHAIN (p
))
4786 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4787 vector of template arguments, as for tsubst.
4789 Returns an appropriate tsubst'd friend declaration. */
4792 tsubst_friend_function (tree decl
, tree args
)
4795 location_t saved_loc
= input_location
;
4797 input_location
= DECL_SOURCE_LOCATION (decl
);
4799 if (TREE_CODE (decl
) == FUNCTION_DECL
4800 && DECL_TEMPLATE_INSTANTIATION (decl
)
4801 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4802 /* This was a friend declared with an explicit template
4803 argument list, e.g.:
4807 to indicate that f was a template instantiation, not a new
4808 function declaration. Now, we have to figure out what
4809 instantiation of what template. */
4811 tree template_id
, arglist
, fns
;
4814 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
4816 /* Friend functions are looked up in the containing namespace scope.
4817 We must enter that scope, to avoid finding member functions of the
4818 current cless with same name. */
4819 push_nested_namespace (ns
);
4820 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
4821 tf_error
| tf_warning
, NULL_TREE
);
4822 pop_nested_namespace (ns
);
4823 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
4824 tf_error
| tf_warning
, NULL_TREE
);
4825 template_id
= lookup_template_function (fns
, arglist
);
4827 new_friend
= tsubst (decl
, args
, tf_error
| tf_warning
, NULL_TREE
);
4828 tmpl
= determine_specialization (template_id
, new_friend
,
4830 /*need_member_template=*/0);
4831 new_friend
= instantiate_template (tmpl
, new_args
, tf_error
);
4835 new_friend
= tsubst (decl
, args
, tf_error
| tf_warning
, NULL_TREE
);
4837 /* The NEW_FRIEND will look like an instantiation, to the
4838 compiler, but is not an instantiation from the point of view of
4839 the language. For example, we might have had:
4841 template <class T> struct S {
4842 template <class U> friend void f(T, U);
4845 Then, in S<int>, template <class U> void f(int, U) is not an
4846 instantiation of anything. */
4847 DECL_USE_TEMPLATE (new_friend
) = 0;
4848 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4850 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4851 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
4852 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
4855 /* The mangled name for the NEW_FRIEND is incorrect. The function
4856 is not a template instantiation and should not be mangled like
4857 one. Therefore, we forget the mangling here; we'll recompute it
4858 later if we need it. */
4859 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4861 SET_DECL_RTL (new_friend
, NULL_RTX
);
4862 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
4865 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4868 tree new_friend_template_info
;
4869 tree new_friend_result_template_info
;
4871 int new_friend_is_defn
;
4873 /* We must save some information from NEW_FRIEND before calling
4874 duplicate decls since that function will free NEW_FRIEND if
4876 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4877 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4879 /* This declaration is a `primary' template. */
4880 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4883 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend
)) != NULL_TREE
;
4884 new_friend_result_template_info
4885 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
4889 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4890 new_friend_result_template_info
= NULL_TREE
;
4893 /* Inside pushdecl_namespace_level, we will push into the
4894 current namespace. However, the friend function should go
4895 into the namespace of the template. */
4896 ns
= decl_namespace_context (new_friend
);
4897 push_nested_namespace (ns
);
4898 old_decl
= pushdecl_namespace_level (new_friend
);
4899 pop_nested_namespace (ns
);
4901 if (old_decl
!= new_friend
)
4903 /* This new friend declaration matched an existing
4904 declaration. For example, given:
4906 template <class T> void f(T);
4907 template <class U> class C {
4908 template <class T> friend void f(T) {}
4911 the friend declaration actually provides the definition
4912 of `f', once C has been instantiated for some type. So,
4913 old_decl will be the out-of-class template declaration,
4914 while new_friend is the in-class definition.
4916 But, if `f' was called before this point, the
4917 instantiation of `f' will have DECL_TI_ARGS corresponding
4918 to `T' but not to `U', references to which might appear
4919 in the definition of `f'. Previously, the most general
4920 template for an instantiation of `f' was the out-of-class
4921 version; now it is the in-class version. Therefore, we
4922 run through all specialization of `f', adding to their
4923 DECL_TI_ARGS appropriately. In particular, they need a
4924 new set of outer arguments, corresponding to the
4925 arguments for this class instantiation.
4927 The same situation can arise with something like this:
4930 template <class T> class C {
4934 when `C<int>' is instantiated. Now, `f(int)' is defined
4937 if (!new_friend_is_defn
)
4938 /* On the other hand, if the in-class declaration does
4939 *not* provide a definition, then we don't want to alter
4940 existing definitions. We can just leave everything
4945 /* Overwrite whatever template info was there before, if
4946 any, with the new template information pertaining to
4948 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4950 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4951 /* duplicate_decls will take care of this case. */
4956 tree new_friend_args
;
4958 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
4959 = new_friend_result_template_info
;
4961 new_friend_args
= TI_ARGS (new_friend_template_info
);
4962 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4966 tree spec
= TREE_VALUE (t
);
4969 = add_outermost_template_args (new_friend_args
,
4970 DECL_TI_ARGS (spec
));
4973 /* Now, since specializations are always supposed to
4974 hang off of the most general template, we must move
4976 t
= most_general_template (old_decl
);
4979 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4980 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4981 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4982 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4987 /* The information from NEW_FRIEND has been merged into OLD_DECL
4988 by duplicate_decls. */
4989 new_friend
= old_decl
;
4992 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend
)))
4994 /* Check to see that the declaration is really present, and,
4995 possibly obtain an improved declaration. */
4996 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
5004 input_location
= saved_loc
;
5008 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5009 template arguments, as for tsubst.
5011 Returns an appropriate tsubst'd friend type or error_mark_node on
5015 tsubst_friend_class (tree friend_tmpl
, tree args
)
5021 context
= DECL_CONTEXT (friend_tmpl
);
5025 if (TREE_CODE (context
) == NAMESPACE_DECL
)
5026 push_nested_namespace (context
);
5028 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
5031 /* First, we look for a class template. */
5032 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
5034 /* But, if we don't find one, it might be because we're in a
5035 situation like this:
5043 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5044 for `S<int>', not the TEMPLATE_DECL. */
5045 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
5047 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
5048 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
5051 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
5053 /* The friend template has already been declared. Just
5054 check to see that the declarations match, and install any new
5055 default parameters. We must tsubst the default parameters,
5056 of course. We only need the innermost template parameters
5057 because that is all that redeclare_class_template will look
5059 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
5060 > TMPL_ARGS_DEPTH (args
))
5063 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
5064 args
, tf_error
| tf_warning
);
5065 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
5068 friend_type
= TREE_TYPE (tmpl
);
5072 /* The friend template has not already been declared. In this
5073 case, the instantiation of the template class will cause the
5074 injection of this template into the global scope. */
5075 tmpl
= tsubst (friend_tmpl
, args
, tf_error
| tf_warning
, NULL_TREE
);
5077 /* The new TMPL is not an instantiation of anything, so we
5078 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5079 the new type because that is supposed to be the corresponding
5080 template decl, i.e., TMPL. */
5081 DECL_USE_TEMPLATE (tmpl
) = 0;
5082 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
5083 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
5085 /* Inject this template into the global scope. */
5086 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
5091 if (TREE_CODE (context
) == NAMESPACE_DECL
)
5092 pop_nested_namespace (context
);
5094 pop_nested_class ();
5100 /* Returns zero if TYPE cannot be completed later due to circularity.
5101 Otherwise returns one. */
5104 can_complete_type_without_circularity (tree type
)
5106 if (type
== NULL_TREE
|| type
== error_mark_node
)
5108 else if (COMPLETE_TYPE_P (type
))
5110 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
5111 return can_complete_type_without_circularity (TREE_TYPE (type
));
5112 else if (CLASS_TYPE_P (type
) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
5119 instantiate_class_template (tree type
)
5121 tree
template, args
, pattern
, t
, member
;
5125 if (type
== error_mark_node
)
5126 return error_mark_node
;
5128 if (TYPE_BEING_DEFINED (type
)
5129 || COMPLETE_TYPE_P (type
)
5130 || dependent_type_p (type
))
5133 /* Figure out which template is being instantiated. */
5134 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
5135 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
5137 /* Figure out which arguments are being used to do the
5139 args
= CLASSTYPE_TI_ARGS (type
);
5141 /* Determine what specialization of the original template to
5143 t
= most_specialized_class (template, args
);
5144 if (t
== error_mark_node
)
5146 const char *str
= "candidates are:";
5147 error ("ambiguous class template instantiation for `%#T'", type
);
5148 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
5151 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
5154 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
5158 TYPE_BEING_DEFINED (type
) = 1;
5159 return error_mark_node
;
5163 pattern
= TREE_TYPE (t
);
5165 pattern
= TREE_TYPE (template);
5167 /* If the template we're instantiating is incomplete, then clearly
5168 there's nothing we can do. */
5169 if (!COMPLETE_TYPE_P (pattern
))
5172 /* If we've recursively instantiated too many templates, stop. */
5173 if (! push_tinst_level (type
))
5176 /* Now we're really doing the instantiation. Mark the type as in
5177 the process of being defined. */
5178 TYPE_BEING_DEFINED (type
) = 1;
5180 /* We may be in the middle of deferred access check. Disable
5182 push_deferring_access_checks (dk_no_deferred
);
5184 maybe_push_to_top_level (uses_template_parms (type
));
5188 /* This TYPE is actually an instantiation of a partial
5189 specialization. We replace the innermost set of ARGS with
5190 the arguments appropriate for substitution. For example,
5193 template <class T> struct S {};
5194 template <class T> struct S<T*> {};
5196 and supposing that we are instantiating S<int*>, ARGS will
5197 present be {int*} but we need {int}. */
5199 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
5202 /* If there were multiple levels in ARGS, replacing the
5203 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5204 want, so we make a copy first. */
5205 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
5207 args
= copy_node (args
);
5208 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
5214 if (flag_external_templates
)
5216 if (flag_alt_external_templates
)
5218 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
5219 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
5223 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
5224 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5225 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
5230 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
5233 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
5234 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
5235 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
5236 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
5237 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
5238 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
5239 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
5240 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
5241 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
5242 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
5243 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
5244 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5245 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5246 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5247 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5248 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
5249 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern
);
5250 TYPE_USES_MULTIPLE_INHERITANCE (type
)
5251 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
5252 TYPE_USES_VIRTUAL_BASECLASSES (type
)
5253 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
5254 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5255 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5256 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
5257 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5258 if (ANON_AGGR_TYPE_P (pattern
))
5259 SET_ANON_AGGR_TYPE_P (type
);
5261 pbinfo
= TYPE_BINFO (pattern
);
5263 if (BINFO_BASETYPES (pbinfo
))
5265 tree base_list
= NULL_TREE
;
5266 tree pbases
= BINFO_BASETYPES (pbinfo
);
5267 tree paccesses
= BINFO_BASEACCESSES (pbinfo
);
5270 /* Substitute into each of the bases to determine the actual
5272 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
5278 pbase
= TREE_VEC_ELT (pbases
, i
);
5279 access
= TREE_VEC_ELT (paccesses
, i
);
5281 /* Substitute to figure out the base class. */
5282 base
= tsubst (BINFO_TYPE (pbase
), args
, tf_error
, NULL_TREE
);
5283 if (base
== error_mark_node
)
5286 base_list
= tree_cons (access
, base
, base_list
);
5287 TREE_VIA_VIRTUAL (base_list
) = TREE_VIA_VIRTUAL (pbase
);
5290 /* The list is now in reverse order; correct that. */
5291 base_list
= nreverse (base_list
);
5293 /* Now call xref_basetypes to set up all the base-class
5295 xref_basetypes (type
, base_list
);
5298 /* Now that our base classes are set up, enter the scope of the
5299 class, so that name lookups into base classes, etc. will work
5300 correctly. This is precisely analogous to what we do in
5301 begin_class_definition when defining an ordinary non-template
5303 pushclass (type
, true);
5305 /* Now members are processed in the order of declaration. */
5306 for (member
= CLASSTYPE_DECL_LIST (pattern
); member
; member
= TREE_CHAIN (member
))
5308 tree t
= TREE_VALUE (member
);
5310 if (TREE_PURPOSE (member
))
5314 /* Build new CLASSTYPE_NESTED_UTDS. */
5317 tree name
= TYPE_IDENTIFIER (tag
);
5320 newtag
= tsubst (tag
, args
, tf_error
, NULL_TREE
);
5321 my_friendly_assert (newtag
!= error_mark_node
, 20010206);
5322 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5324 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
5325 /* Unfortunately, lookup_template_class sets
5326 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5327 instantiation (i.e., for the type of a member template
5328 class nested within a template class.) This behavior is
5329 required for maybe_process_partial_specialization to work
5330 correctly, but is not accurate in this case; the TAG is not
5331 an instantiation of anything. (The corresponding
5332 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5333 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5335 /* Now, we call pushtag to put this NEWTAG into the scope of
5336 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5337 pushtag calling push_template_decl. We don't have to do
5338 this for enums because it will already have been done in
5341 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5342 pushtag (name
, newtag
, /*globalize=*/0);
5345 else if (TREE_CODE (t
) == FUNCTION_DECL
5346 || DECL_FUNCTION_TEMPLATE_P (t
))
5348 /* Build new TYPE_METHODS. */
5350 tree r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
5351 set_current_access_from_decl (r
);
5352 grok_special_member_properties (r
);
5353 finish_member_declaration (r
);
5357 /* Build new TYPE_FIELDS. */
5359 if (TREE_CODE (t
) != CONST_DECL
)
5363 /* The the file and line for this declaration, to
5364 assist in error message reporting. Since we
5365 called push_tinst_level above, we don't need to
5367 input_location
= DECL_SOURCE_LOCATION (t
);
5369 r
= tsubst (t
, args
, tf_error
| tf_warning
, NULL_TREE
);
5370 if (TREE_CODE (r
) == VAR_DECL
)
5374 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5375 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5376 tf_error
| tf_warning
, NULL_TREE
);
5380 finish_static_data_member_decl (r
, init
,
5381 /*asmspec_tree=*/NULL_TREE
,
5384 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5385 check_static_variable_definition (r
, TREE_TYPE (r
));
5387 else if (TREE_CODE (r
) == FIELD_DECL
)
5389 /* Determine whether R has a valid type and can be
5390 completed later. If R is invalid, then it is
5391 replaced by error_mark_node so that it will not be
5392 added to TYPE_FIELDS. */
5393 tree rtype
= TREE_TYPE (r
);
5394 if (can_complete_type_without_circularity (rtype
))
5395 complete_type (rtype
);
5397 if (!COMPLETE_TYPE_P (rtype
))
5399 cxx_incomplete_type_error (r
, rtype
);
5400 r
= error_mark_node
;
5404 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5405 such a thing will already have been added to the field
5406 list by tsubst_enum in finish_member_declaration in the
5407 CLASSTYPE_NESTED_UTDS case above. */
5408 if (!(TREE_CODE (r
) == TYPE_DECL
5409 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
5410 && DECL_ARTIFICIAL (r
)))
5412 set_current_access_from_decl (r
);
5413 finish_member_declaration (r
);
5420 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
5422 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5424 tree friend_type
= t
;
5425 tree new_friend_type
;
5427 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5428 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5429 else if (uses_template_parms (friend_type
))
5430 new_friend_type
= tsubst (friend_type
, args
,
5431 tf_error
| tf_warning
, NULL_TREE
);
5434 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5436 /* The call to xref_tag_from_type does injection for friend
5438 push_nested_namespace (ns
);
5440 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5441 pop_nested_namespace (ns
);
5444 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5445 /* Trick make_friend_class into realizing that the friend
5446 we're adding is a template, not an ordinary class. It's
5447 important that we use make_friend_class since it will
5448 perform some error-checking and output cross-reference
5450 ++processing_template_decl
;
5452 if (new_friend_type
!= error_mark_node
)
5453 make_friend_class (type
, new_friend_type
);
5455 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5456 --processing_template_decl
;
5459 /* Build new DECL_FRIENDLIST. */
5460 add_friend (type
, tsubst_friend_function (t
, args
));
5464 /* Set the file and line number information to whatever is given for
5465 the class itself. This puts error messages involving generated
5466 implicit functions at a predictable point, and the same point
5467 that would be used for non-template classes. */
5468 typedecl
= TYPE_MAIN_DECL (type
);
5469 input_location
= DECL_SOURCE_LOCATION (typedecl
);
5471 unreverse_member_declarations (type
);
5472 finish_struct_1 (type
);
5473 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5475 /* Clear this now so repo_template_used is happy. */
5476 TYPE_BEING_DEFINED (type
) = 0;
5477 repo_template_used (type
);
5479 /* Now that the class is complete, instantiate default arguments for
5480 any member functions. We don't do this earlier because the
5481 default arguments may reference members of the class. */
5482 if (!PRIMARY_TEMPLATE_P (template))
5483 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5484 if (TREE_CODE (t
) == FUNCTION_DECL
5485 /* Implicitly generated member functions will not have template
5486 information; they are not instantiations, but instead are
5487 created "fresh" for each instantiation. */
5488 && DECL_TEMPLATE_INFO (t
))
5489 tsubst_default_arguments (t
);
5492 pop_from_top_level ();
5493 pop_deferring_access_checks ();
5496 if (TYPE_CONTAINS_VPTR_P (type
))
5497 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
5503 list_eq (tree t1
, tree t2
)
5505 if (t1
== NULL_TREE
)
5506 return t2
== NULL_TREE
;
5507 if (t2
== NULL_TREE
)
5509 /* Don't care if one declares its arg const and the other doesn't -- the
5510 main variant of the arg type is all that matters. */
5511 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5512 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5514 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5517 /* If arg is a non-type template parameter that does not depend on template
5518 arguments, fold it like we weren't in the body of a template. */
5521 maybe_fold_nontype_arg (tree arg
)
5523 if (arg
&& !TYPE_P (arg
) && !uses_template_parms (arg
))
5525 /* Sometimes, one of the args was an expression involving a
5526 template constant parameter, like N - 1. Now that we've
5527 tsubst'd, we might have something like 2 - 1. This will
5528 confuse lookup_template_class, so we do constant folding
5529 here. We have to unset processing_template_decl, to
5530 fool build_expr_from_tree() into building an actual
5533 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5534 as simple as it's going to get, and trying to reprocess
5535 the trees will break. */
5536 if (!TREE_TYPE (arg
))
5538 int saved_processing_template_decl
= processing_template_decl
;
5539 processing_template_decl
= 0;
5540 arg
= build_expr_from_tree (arg
);
5541 processing_template_decl
= saved_processing_template_decl
;
5549 /* Apply maybe_fold_nontype_arg on a list or vector of args. */
5552 maybe_fold_nontype_args (tree targs
)
5556 else if (TREE_CODE (targs
) == TREE_LIST
)
5559 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
5560 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
5565 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
5566 TREE_VEC_ELT (targs
, i
)
5567 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
5571 /* Substitute ARGS into the vector of template arguments T. */
5574 tsubst_template_arg_vector (tree t
, tree args
, tsubst_flags_t complain
)
5576 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5577 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5579 memset ((char *) elts
, 0, len
* sizeof (tree
));
5581 for (i
= 0; i
< len
; i
++)
5583 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5584 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5585 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5588 elts
[i
] = maybe_fold_nontype_arg
5589 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5592 if (elts
[i
] == error_mark_node
)
5593 return error_mark_node
;
5595 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5602 t
= make_tree_vec (len
);
5603 for (i
= 0; i
< len
; i
++)
5604 TREE_VEC_ELT (t
, i
) = elts
[i
];
5609 /* Return the result of substituting ARGS into the template parameters
5610 given by PARMS. If there are m levels of ARGS and m + n levels of
5611 PARMS, then the result will contain n levels of PARMS. For
5612 example, if PARMS is `template <class T> template <class U>
5613 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5614 result will be `template <int*, double, class V>'. */
5617 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
5622 for (new_parms
= &r
;
5623 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5624 new_parms
= &(TREE_CHAIN (*new_parms
)),
5625 parms
= TREE_CHAIN (parms
))
5628 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5631 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5633 tree tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
5634 tree default_value
= TREE_PURPOSE (tuple
);
5635 tree parm_decl
= TREE_VALUE (tuple
);
5637 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
5638 default_value
= tsubst_expr (default_value
, args
,
5639 complain
, NULL_TREE
);
5640 tuple
= build_tree_list (maybe_fold_nontype_arg (default_value
),
5642 TREE_VEC_ELT (new_vec
, i
) = tuple
;
5646 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
5647 - TMPL_ARGS_DEPTH (args
)),
5648 new_vec
, NULL_TREE
);
5654 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5655 type T. If T is not an aggregate or enumeration type, it is
5656 handled as if by tsubst. IN_DECL is as for tsubst. If
5657 ENTERING_SCOPE is nonzero, T is the context for a template which
5658 we are presently tsubst'ing. Return the substituted value. */
5661 tsubst_aggr_type (tree t
,
5663 tsubst_flags_t complain
,
5670 switch (TREE_CODE (t
))
5673 if (TYPE_PTRMEMFUNC_P (t
))
5674 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
5676 /* else fall through */
5679 if (TYPE_TEMPLATE_INFO (t
))
5685 /* First, determine the context for the type we are looking
5687 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5688 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5690 in_decl
, /*entering_scope=*/1);
5692 context
= NULL_TREE
;
5694 /* Then, figure out what arguments are appropriate for the
5695 type we are trying to find. For example, given:
5697 template <class T> struct S;
5698 template <class T, class U> void f(T, U) { S<U> su; }
5700 and supposing that we are instantiating f<int, double>,
5701 then our ARGS will be {int, double}, but, when looking up
5702 S we only want {double}. */
5703 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5705 if (argvec
== error_mark_node
)
5706 return error_mark_node
;
5708 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5709 entering_scope
, complain
);
5711 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
5714 /* This is not a template type, so there's nothing to do. */
5718 return tsubst (t
, args
, complain
, in_decl
);
5722 /* Substitute into the default argument ARG (a default argument for
5723 FN), which has the indicated TYPE. */
5726 tsubst_default_argument (tree fn
, tree type
, tree arg
)
5728 /* This default argument came from a template. Instantiate the
5729 default argument here, not in tsubst. In the case of
5738 we must be careful to do name lookup in the scope of S<T>,
5739 rather than in the current class.
5741 ??? current_class_type affects a lot more than name lookup. This is
5742 very fragile. Fortunately, it will go away when we do 2-phase name
5743 binding properly. */
5745 /* FN is already the desired FUNCTION_DECL. */
5746 push_access_scope (fn
);
5748 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
5749 tf_error
| tf_warning
, NULL_TREE
);
5751 pop_access_scope (fn
);
5753 /* Make sure the default argument is reasonable. */
5754 arg
= check_default_argument (type
, arg
);
5759 /* Substitute into all the default arguments for FN. */
5762 tsubst_default_arguments (tree fn
)
5767 tmpl_args
= DECL_TI_ARGS (fn
);
5769 /* If this function is not yet instantiated, we certainly don't need
5770 its default arguments. */
5771 if (uses_template_parms (tmpl_args
))
5774 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5776 arg
= TREE_CHAIN (arg
))
5777 if (TREE_PURPOSE (arg
))
5778 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5780 TREE_PURPOSE (arg
));
5783 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5784 (already computed) substitution of ARGS into TREE_TYPE (T), if
5785 appropriate. Return the result of the substitution. Issue error
5786 and warning messages under control of COMPLAIN. */
5789 tsubst_decl (tree t
, tree args
, tree type
, tsubst_flags_t complain
)
5791 location_t saved_loc
;
5795 /* Set the filename and linenumber to improve error-reporting. */
5796 saved_loc
= input_location
;
5797 input_location
= DECL_SOURCE_LOCATION (t
);
5799 switch (TREE_CODE (t
))
5803 /* We can get here when processing a member template function
5804 of a template class. */
5805 tree decl
= DECL_TEMPLATE_RESULT (t
);
5807 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5809 if (!is_template_template_parm
)
5811 /* We might already have an instance of this template.
5812 The ARGS are for the surrounding class type, so the
5813 full args contain the tsubst'd args for the context,
5814 plus the innermost args from the template decl. */
5815 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5816 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5817 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
5820 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5823 /* tsubst_template_arg_vector doesn't copy the vector if
5824 nothing changed. But, *something* should have
5826 my_friendly_assert (full_args
!= tmpl_args
, 0);
5828 spec
= retrieve_specialization (t
, full_args
);
5829 if (spec
!= NULL_TREE
)
5836 /* Make a new template decl. It will be similar to the
5837 original, but will record the current template arguments.
5838 We also create a new function declaration, which is just
5839 like the old one, but points to this new template, rather
5840 than the old one. */
5842 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5843 TREE_CHAIN (r
) = NULL_TREE
;
5845 if (is_template_template_parm
)
5847 tree new_decl
= tsubst (decl
, args
, complain
, in_decl
);
5848 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5849 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5854 = tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5856 /*entering_scope=*/1);
5857 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5859 if (TREE_CODE (decl
) == TYPE_DECL
)
5861 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
5862 TREE_TYPE (r
) = new_type
;
5863 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5864 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5865 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5869 tree new_decl
= tsubst (decl
, args
, complain
, in_decl
);
5871 DECL_TEMPLATE_RESULT (r
) = new_decl
;
5872 DECL_TI_TEMPLATE (new_decl
) = r
;
5873 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5874 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5877 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5878 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5879 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5881 /* The template parameters for this new template are all the
5882 template parameters for the old template, except the
5883 outermost level of parameters. */
5884 DECL_TEMPLATE_PARMS (r
)
5885 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5888 if (PRIMARY_TEMPLATE_P (t
))
5889 DECL_PRIMARY_TEMPLATE (r
) = r
;
5891 /* We don't partially instantiate partial specializations. */
5892 if (TREE_CODE (decl
) == TYPE_DECL
)
5895 /* Record this partial instantiation. */
5896 register_specialization (r
, t
,
5897 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)));
5905 tree argvec
= NULL_TREE
;
5912 /* Nobody should be tsubst'ing into non-template functions. */
5913 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5915 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5920 /* If T is not dependent, just return it. We have to
5921 increment PROCESSING_TEMPLATE_DECL because
5922 value_dependent_expression_p assumes that nothing is
5923 dependent when PROCESSING_TEMPLATE_DECL is zero. */
5924 ++processing_template_decl
;
5925 dependent_p
= value_dependent_expression_p (t
);
5926 --processing_template_decl
;
5930 /* Calculate the most general template of which R is a
5931 specialization, and the complete set of arguments used to
5933 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5935 = tsubst_template_arg_vector (DECL_TI_ARGS
5936 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5939 /* Check to see if we already have this specialization. */
5940 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5948 /* We can see more levels of arguments than parameters if
5949 there was a specialization of a member template, like
5952 template <class T> struct S { template <class U> void f(); }
5953 template <> template <class U> void S<int>::f(U);
5955 Here, we'll be substituting into the specialization,
5956 because that's where we can find the code we actually
5957 want to generate, but we'll have enough arguments for
5958 the most general template.
5960 We also deal with the peculiar case:
5962 template <class T> struct S {
5963 template <class U> friend void f();
5965 template <class U> void f() {}
5967 template void f<double>();
5969 Here, the ARGS for the instantiation of will be {int,
5970 double}. But, we only need as many ARGS as there are
5971 levels of template parameters in CODE_PATTERN. We are
5972 careful not to get fooled into reducing the ARGS in
5975 template <class T> struct S { template <class U> void f(U); }
5976 template <class T> template <> void S<T>::f(int) {}
5978 which we can spot because the pattern will be a
5979 specialization in this case. */
5980 args_depth
= TMPL_ARGS_DEPTH (args
);
5982 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5983 if (args_depth
> parms_depth
5984 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5985 args
= get_innermost_template_args (args
, parms_depth
);
5989 /* This special case arises when we have something like this:
5991 template <class T> struct S {
5992 friend void f<int>(int, double);
5995 Here, the DECL_TI_TEMPLATE for the friend declaration
5996 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5997 being called from tsubst_friend_function, and we want
5998 only to create a new decl (R) with appropriate types so
5999 that we can call determine_specialization. */
6000 gen_tmpl
= NULL_TREE
;
6003 if (DECL_CLASS_SCOPE_P (t
))
6005 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
6009 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6011 /*entering_scope=*/1);
6016 ctx
= DECL_CONTEXT (t
);
6018 type
= tsubst (type
, args
, complain
, in_decl
);
6019 if (type
== error_mark_node
)
6020 return error_mark_node
;
6022 /* We do NOT check for matching decls pushed separately at this
6023 point, as they may not represent instantiations of this
6024 template, and in any case are considered separate under the
6027 DECL_USE_TEMPLATE (r
) = 0;
6028 TREE_TYPE (r
) = type
;
6029 /* Clear out the mangled name and RTL for the instantiation. */
6030 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
6031 SET_DECL_RTL (r
, NULL_RTX
);
6033 DECL_CONTEXT (r
) = ctx
;
6035 if (member
&& DECL_CONV_FN_P (r
))
6036 /* Type-conversion operator. Reconstruct the name, in
6037 case it's the name of one of the template's parameters. */
6038 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
6040 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
6042 DECL_RESULT (r
) = NULL_TREE
;
6044 TREE_STATIC (r
) = 0;
6045 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
6046 DECL_EXTERNAL (r
) = 1;
6047 DECL_INTERFACE_KNOWN (r
) = 0;
6048 DECL_DEFER_OUTPUT (r
) = 0;
6049 TREE_CHAIN (r
) = NULL_TREE
;
6050 DECL_PENDING_INLINE_INFO (r
) = 0;
6051 DECL_PENDING_INLINE_P (r
) = 0;
6052 DECL_SAVED_TREE (r
) = NULL_TREE
;
6054 if (DECL_CLONED_FUNCTION (r
))
6056 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
6058 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
6059 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
6062 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6063 this in the special friend case mentioned above where
6064 GEN_TMPL is NULL. */
6067 DECL_TEMPLATE_INFO (r
)
6068 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
6069 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6070 register_specialization (r
, gen_tmpl
, argvec
);
6072 /* We're not supposed to instantiate default arguments
6073 until they are called, for a template. But, for a
6076 template <class T> void f ()
6077 { extern void g(int i = T()); }
6079 we should do the substitution when the template is
6080 instantiated. We handle the member function case in
6081 instantiate_class_template since the default arguments
6082 might refer to other members of the class. */
6084 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6085 && !uses_template_parms (argvec
))
6086 tsubst_default_arguments (r
);
6089 /* Copy the list of befriending classes. */
6090 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
6092 friends
= &TREE_CHAIN (*friends
))
6094 *friends
= copy_node (*friends
);
6095 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
6100 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
6102 maybe_retrofit_in_chrg (r
);
6103 if (DECL_CONSTRUCTOR_P (r
))
6104 grok_ctor_properties (ctx
, r
);
6105 /* If this is an instantiation of a member template, clone it.
6106 If it isn't, that'll be handled by
6107 clone_constructors_and_destructors. */
6108 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
6109 clone_function_decl (r
, /*update_method_vec_p=*/0);
6111 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
6112 grok_op_properties (r
, DECL_FRIEND_P (r
));
6119 if (DECL_TEMPLATE_PARM_P (t
))
6120 SET_DECL_TEMPLATE_PARM_P (r
);
6122 TREE_TYPE (r
) = type
;
6123 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6125 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
6126 DECL_INITIAL (r
) = TREE_TYPE (r
);
6128 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
6131 DECL_CONTEXT (r
) = NULL_TREE
;
6133 if (!DECL_TEMPLATE_PARM_P (r
))
6134 DECL_ARG_TYPE (r
) = type_passed_as (type
);
6136 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
6137 complain
, TREE_CHAIN (t
));
6144 TREE_TYPE (r
) = type
;
6145 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6147 /* We don't have to set DECL_CONTEXT here; it is set by
6148 finish_member_declaration. */
6149 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
6151 TREE_CHAIN (r
) = NULL_TREE
;
6152 if (VOID_TYPE_P (type
))
6153 cp_error_at ("instantiation of `%D' as type `%T'", r
, type
);
6161 = tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
6162 TREE_CHAIN (r
) = NULL_TREE
;
6167 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
6168 || t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
6170 /* If this is the canonical decl, we don't have to mess with
6171 instantiations, and often we can't (for typename, template
6172 type parms and such). Note that TYPE_NAME is not correct for
6173 the above test if we've copied the type for a typedef. */
6174 r
= TYPE_NAME (type
);
6182 tree argvec
= NULL_TREE
;
6183 tree gen_tmpl
= NULL_TREE
;
6185 tree tmpl
= NULL_TREE
;
6189 /* Assume this is a non-local variable. */
6192 if (TYPE_P (CP_DECL_CONTEXT (t
)))
6193 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6195 in_decl
, /*entering_scope=*/1);
6196 else if (DECL_NAMESPACE_SCOPE_P (t
))
6197 ctx
= DECL_CONTEXT (t
);
6200 /* Subsequent calls to pushdecl will fill this in. */
6205 /* Check to see if we already have this specialization. */
6208 tmpl
= DECL_TI_TEMPLATE (t
);
6209 gen_tmpl
= most_general_template (tmpl
);
6210 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
6211 spec
= retrieve_specialization (gen_tmpl
, argvec
);
6214 spec
= retrieve_local_specialization (t
);
6223 if (TREE_CODE (r
) == VAR_DECL
)
6224 type
= complete_type (type
);
6225 TREE_TYPE (r
) = type
;
6226 c_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6227 DECL_CONTEXT (r
) = ctx
;
6228 /* Clear out the mangled name and RTL for the instantiation. */
6229 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
6230 SET_DECL_RTL (r
, NULL_RTX
);
6232 /* Don't try to expand the initializer until someone tries to use
6233 this variable; otherwise we run into circular dependencies. */
6234 DECL_INITIAL (r
) = NULL_TREE
;
6235 SET_DECL_RTL (r
, NULL_RTX
);
6236 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
6238 /* Even if the original location is out of scope, the newly
6239 substituted one is not. */
6240 if (TREE_CODE (r
) == VAR_DECL
)
6242 DECL_DEAD_FOR_LOCAL (r
) = 0;
6243 DECL_INITIALIZED_P (r
) = 0;
6248 /* A static data member declaration is always marked
6249 external when it is declared in-class, even if an
6250 initializer is present. We mimic the non-template
6252 DECL_EXTERNAL (r
) = 1;
6254 register_specialization (r
, gen_tmpl
, argvec
);
6255 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
6256 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6259 register_local_specialization (r
, t
);
6261 TREE_CHAIN (r
) = NULL_TREE
;
6262 if (TREE_CODE (r
) == VAR_DECL
&& VOID_TYPE_P (type
))
6263 cp_error_at ("instantiation of `%D' as type `%T'", r
, type
);
6264 /* Compute the size, alignment, etc. of R. */
6273 /* Restore the file and line information. */
6274 input_location
= saved_loc
;
6279 /* Substitue into the ARG_TYPES of a function type. */
6282 tsubst_arg_types (tree arg_types
,
6284 tsubst_flags_t complain
,
6287 tree remaining_arg_types
;
6290 if (!arg_types
|| arg_types
== void_list_node
)
6293 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6294 args
, complain
, in_decl
);
6295 if (remaining_arg_types
== error_mark_node
)
6296 return error_mark_node
;
6298 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6299 if (type
== error_mark_node
)
6300 return error_mark_node
;
6301 if (VOID_TYPE_P (type
))
6303 if (complain
& tf_error
)
6305 error ("invalid parameter type `%T'", type
);
6307 cp_error_at ("in declaration `%D'", in_decl
);
6309 return error_mark_node
;
6312 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6313 top-level qualifiers as required. */
6314 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6316 /* Note that we do not substitute into default arguments here. The
6317 standard mandates that they be instantiated only when needed,
6318 which is done in build_over_call. */
6319 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6320 remaining_arg_types
);
6324 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6325 *not* handle the exception-specification for FNTYPE, because the
6326 initial substitution of explicitly provided template parameters
6327 during argument deduction forbids substitution into the
6328 exception-specification:
6332 All references in the function type of the function template to the
6333 corresponding template parameters are replaced by the specified tem-
6334 plate argument values. If a substitution in a template parameter or
6335 in the function type of the function template results in an invalid
6336 type, type deduction fails. [Note: The equivalent substitution in
6337 exception specifications is done only when the function is instanti-
6338 ated, at which point a program is ill-formed if the substitution
6339 results in an invalid type.] */
6342 tsubst_function_type (tree t
,
6344 tsubst_flags_t complain
,
6351 /* The TYPE_CONTEXT is not used for function/method types. */
6352 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6354 /* Substitute the return type. */
6355 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6356 if (return_type
== error_mark_node
)
6357 return error_mark_node
;
6359 /* Substitue the argument types. */
6360 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6362 if (arg_types
== error_mark_node
)
6363 return error_mark_node
;
6365 /* Construct a new type node and return it. */
6366 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6367 fntype
= build_function_type (return_type
, arg_types
);
6370 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6371 if (! IS_AGGR_TYPE (r
))
6375 Type deduction may fail for any of the following
6378 -- Attempting to create "pointer to member of T" when T
6379 is not a class type. */
6380 if (complain
& tf_error
)
6381 error ("creating pointer to member function of non-class type `%T'",
6383 return error_mark_node
;
6386 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6389 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
6390 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6395 /* Substitute into the PARMS of a call-declarator. */
6398 tsubst_call_declarator_parms (tree parms
,
6400 tsubst_flags_t complain
,
6407 if (!parms
|| parms
== void_list_node
)
6410 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6411 args
, complain
, in_decl
);
6413 /* Figure out the type of this parameter. */
6414 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6416 /* Figure out the default argument as well. Note that we use
6417 tsubst_expr since the default argument is really an expression. */
6418 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6420 /* Chain this parameter on to the front of those we have already
6421 processed. We don't use hash_tree_cons because that function
6422 doesn't check TREE_PARMLIST. */
6423 new_parms
= tree_cons (defarg
, type
, new_parms
);
6425 /* And note that these are parameters. */
6426 TREE_PARMLIST (new_parms
) = 1;
6431 /* Take the tree structure T and replace template parameters used
6432 therein with the argument vector ARGS. IN_DECL is an associated
6433 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6434 Issue error and warning messages under control of COMPLAIN. Note
6435 that we must be relatively non-tolerant of extensions here, in
6436 order to preserve conformance; if we allow substitutions that
6437 should not be allowed, we may allow argument deductions that should
6438 not succeed, and therefore report ambiguous overload situations
6439 where there are none. In theory, we could allow the substitution,
6440 but indicate that it should have failed, and allow our caller to
6441 make sure that the right thing happens, but we don't try to do this
6444 This function is used for dealing with types, decls and the like;
6445 for expressions, use tsubst_expr or tsubst_copy. */
6448 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
6452 if (t
== NULL_TREE
|| t
== error_mark_node
6453 || t
== integer_type_node
6454 || t
== void_type_node
6455 || t
== char_type_node
6456 || TREE_CODE (t
) == NAMESPACE_DECL
)
6459 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6460 type
= IDENTIFIER_TYPE_VALUE (t
);
6462 type
= TREE_TYPE (t
);
6463 if (type
== unknown_type_node
)
6466 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6467 && TREE_CODE (t
) != TYPENAME_TYPE
6468 && TREE_CODE (t
) != TEMPLATE_DECL
6469 && TREE_CODE (t
) != IDENTIFIER_NODE
6470 && TREE_CODE (t
) != FUNCTION_TYPE
6471 && TREE_CODE (t
) != METHOD_TYPE
)
6472 type
= tsubst (type
, args
, complain
, in_decl
);
6473 if (type
== error_mark_node
)
6474 return error_mark_node
;
6477 return tsubst_decl (t
, args
, type
, complain
);
6479 switch (TREE_CODE (t
))
6484 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6485 /*entering_scope=*/0);
6488 case IDENTIFIER_NODE
:
6500 if (t
== integer_type_node
)
6503 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6504 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6508 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6510 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6511 if (max
== error_mark_node
)
6512 return error_mark_node
;
6514 /* See if we can reduce this expression to something simpler. */
6515 max
= maybe_fold_nontype_arg (max
);
6516 if (!processing_template_decl
)
6517 max
= decl_constant_value (max
);
6519 if (processing_template_decl
6520 /* When providing explicit arguments to a template
6521 function, but leaving some arguments for subsequent
6522 deduction, MAX may be template-dependent even if we're
6523 not PROCESSING_TEMPLATE_DECL. We still need to check for
6524 template parms, though; MAX won't be an INTEGER_CST for
6525 dynamic arrays, either. */
6526 || (TREE_CODE (max
) != INTEGER_CST
6527 && uses_template_parms (max
)))
6529 tree itype
= make_node (INTEGER_TYPE
);
6530 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6531 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6536 if (integer_zerop (omax
))
6538 /* Still allow an explicit array of size zero. */
6540 pedwarn ("creating array with size zero");
6542 else if (integer_zerop (max
)
6543 || (TREE_CODE (max
) == INTEGER_CST
6544 && INT_CST_LT (max
, integer_zero_node
)))
6548 Type deduction may fail for any of the following
6551 Attempting to create an array with a size that is
6552 zero or negative. */
6553 if (complain
& tf_error
)
6554 error ("creating array with size zero (`%E')", max
);
6556 return error_mark_node
;
6559 return compute_array_index_type (NULL_TREE
, max
);
6562 case TEMPLATE_TYPE_PARM
:
6563 case TEMPLATE_TEMPLATE_PARM
:
6564 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6565 case TEMPLATE_PARM_INDEX
:
6573 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6574 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6575 || TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6577 idx
= TEMPLATE_TYPE_IDX (t
);
6578 level
= TEMPLATE_TYPE_LEVEL (t
);
6582 idx
= TEMPLATE_PARM_IDX (t
);
6583 level
= TEMPLATE_PARM_LEVEL (t
);
6586 if (TREE_VEC_LENGTH (args
) > 0)
6588 tree arg
= NULL_TREE
;
6590 levels
= TMPL_ARGS_DEPTH (args
);
6591 if (level
<= levels
)
6592 arg
= TMPL_ARG (args
, level
, idx
);
6594 if (arg
== error_mark_node
)
6595 return error_mark_node
;
6596 else if (arg
!= NULL_TREE
)
6598 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6600 my_friendly_assert (TYPE_P (arg
), 0);
6601 return cp_build_qualified_type_real
6602 (arg
, cp_type_quals (arg
) | cp_type_quals (t
),
6603 complain
| tf_ignore_bad_quals
);
6605 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6607 /* We are processing a type constructed from
6608 a template template parameter */
6609 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6610 args
, complain
, in_decl
);
6611 if (argvec
== error_mark_node
)
6612 return error_mark_node
;
6614 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6615 we are resolving nested-types in the signature of
6616 a member function templates.
6617 Otherwise ARG is a TEMPLATE_DECL and is the real
6618 template to be instantiated. */
6619 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6620 arg
= TYPE_NAME (arg
);
6622 r
= lookup_template_class (arg
,
6625 /*entering_scope=*/0,
6627 return cp_build_qualified_type_real
6628 (r
, TYPE_QUALS (t
), complain
);
6631 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
6639 /* This can happen during the attempted tsubst'ing in
6640 unify. This means that we don't yet have any information
6641 about the template parameter in question. */
6644 /* If we get here, we must have been looking at a parm for a
6645 more deeply nested template. Make a new version of this
6646 template parameter, but with a lower level. */
6647 switch (TREE_CODE (t
))
6649 case TEMPLATE_TYPE_PARM
:
6650 case TEMPLATE_TEMPLATE_PARM
:
6651 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6652 if (cp_type_quals (t
))
6654 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
6655 r
= cp_build_qualified_type_real
6656 (r
, cp_type_quals (t
),
6657 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6658 ? tf_ignore_bad_quals
: 0));
6663 TEMPLATE_TYPE_PARM_INDEX (r
)
6664 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6666 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6667 TYPE_MAIN_VARIANT (r
) = r
;
6668 TYPE_POINTER_TO (r
) = NULL_TREE
;
6669 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6671 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
6673 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6675 if (argvec
== error_mark_node
)
6676 return error_mark_node
;
6678 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6679 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
6684 case TEMPLATE_PARM_INDEX
:
6685 r
= reduce_template_parm_level (t
, type
, levels
);
6697 tree purpose
, value
, chain
, result
;
6699 if (t
== void_list_node
)
6702 purpose
= TREE_PURPOSE (t
);
6705 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6706 if (purpose
== error_mark_node
)
6707 return error_mark_node
;
6709 value
= TREE_VALUE (t
);
6712 value
= tsubst (value
, args
, complain
, in_decl
);
6713 if (value
== error_mark_node
)
6714 return error_mark_node
;
6716 chain
= TREE_CHAIN (t
);
6717 if (chain
&& chain
!= void_type_node
)
6719 chain
= tsubst (chain
, args
, complain
, in_decl
);
6720 if (chain
== error_mark_node
)
6721 return error_mark_node
;
6723 if (purpose
== TREE_PURPOSE (t
)
6724 && value
== TREE_VALUE (t
)
6725 && chain
== TREE_CHAIN (t
))
6727 if (TREE_PARMLIST (t
))
6729 result
= tree_cons (purpose
, value
, chain
);
6730 TREE_PARMLIST (result
) = 1;
6733 result
= hash_tree_cons (purpose
, value
, chain
);
6737 if (type
!= NULL_TREE
)
6739 /* A binfo node. We always need to make a copy, of the node
6740 itself and of its BINFO_BASETYPES. */
6744 /* Make sure type isn't a typedef copy. */
6745 type
= BINFO_TYPE (TYPE_BINFO (type
));
6747 TREE_TYPE (t
) = complete_type (type
);
6748 if (IS_AGGR_TYPE (type
))
6750 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6751 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6752 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6753 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6758 /* Otherwise, a vector of template arguments. */
6759 return tsubst_template_arg_vector (t
, args
, complain
);
6762 case REFERENCE_TYPE
:
6764 enum tree_code code
;
6766 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
6769 code
= TREE_CODE (t
);
6774 Type deduction may fail for any of the following
6777 -- Attempting to create a pointer to reference type.
6778 -- Attempting to create a reference to a reference type or
6779 a reference to void. */
6780 if (TREE_CODE (type
) == REFERENCE_TYPE
6781 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6783 static location_t last_loc
;
6785 /* We keep track of the last time we issued this error
6786 message to avoid spewing a ton of messages during a
6787 single bad template instantiation. */
6788 if (complain
& tf_error
6789 && (last_loc
.line
!= input_line
6790 || last_loc
.file
!= input_filename
))
6792 if (TREE_CODE (type
) == VOID_TYPE
)
6793 error ("forming reference to void");
6795 error ("forming %s to reference type `%T'",
6796 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6798 last_loc
= input_location
;
6801 return error_mark_node
;
6803 else if (code
== POINTER_TYPE
)
6805 r
= build_pointer_type (type
);
6806 if (TREE_CODE (type
) == METHOD_TYPE
)
6807 r
= build_ptrmemfunc_type (r
);
6810 r
= build_reference_type (type
);
6811 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6813 if (r
!= error_mark_node
)
6814 /* Will this ever be needed for TYPE_..._TO values? */
6821 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6822 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6826 Type deduction may fail for any of the following
6829 -- Attempting to create "pointer to member of T" when T
6830 is not a class type. */
6831 if (complain
& tf_error
)
6832 error ("creating pointer to member of non-class type `%T'", r
);
6833 return error_mark_node
;
6835 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6837 if (complain
& tf_error
)
6838 error ("creating pointer to member reference type `%T'", type
);
6840 return error_mark_node
;
6842 my_friendly_assert (TREE_CODE (type
) != METHOD_TYPE
, 20011231);
6843 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6844 /* This is really a method type. The cv qualifiers of the
6845 this pointer should _not_ be determined by the cv
6846 qualifiers of the class type. They should be held
6847 somewhere in the FUNCTION_TYPE, but we don't do that at
6848 the moment. Consider
6849 typedef void (Func) () const;
6851 template <typename T1> void Foo (Func T1::*);
6854 return build_cplus_method_type (TYPE_MAIN_VARIANT (r
),
6856 TYPE_ARG_TYPES (type
));
6858 return build_offset_type (r
, type
);
6866 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6867 if (fntype
== error_mark_node
)
6868 return error_mark_node
;
6870 /* Substitue the exception specification. */
6871 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6874 tree list
= NULL_TREE
;
6876 if (! TREE_VALUE (raises
))
6879 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6881 tree spec
= TREE_VALUE (raises
);
6883 spec
= tsubst (spec
, args
, complain
, in_decl
);
6884 if (spec
== error_mark_node
)
6886 list
= add_exception_specifier (list
, spec
, complain
);
6888 fntype
= build_exception_variant (fntype
, list
);
6894 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6895 if (domain
== error_mark_node
)
6896 return error_mark_node
;
6898 /* As an optimization, we avoid regenerating the array type if
6899 it will obviously be the same as T. */
6900 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6903 /* These checks should match the ones in grokdeclarator.
6907 The deduction may fail for any of the following reasons:
6909 -- Attempting to create an array with an element type that
6910 is void, a function type, or a reference type. */
6911 if (TREE_CODE (type
) == VOID_TYPE
6912 || TREE_CODE (type
) == FUNCTION_TYPE
6913 || TREE_CODE (type
) == REFERENCE_TYPE
)
6915 if (complain
& tf_error
)
6916 error ("creating array of `%T'", type
);
6917 return error_mark_node
;
6920 r
= build_cplus_array_type (type
, domain
);
6927 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6928 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6930 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6931 return error_mark_node
;
6933 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6939 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6940 if (e
== error_mark_node
)
6941 return error_mark_node
;
6943 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6948 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6949 in_decl
, /*entering_scope=*/1);
6950 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6953 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6954 return error_mark_node
;
6956 if (!IS_AGGR_TYPE (ctx
))
6958 if (complain
& tf_error
)
6959 error ("`%T' is not a class, struct, or union type",
6961 return error_mark_node
;
6963 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6965 /* Normally, make_typename_type does not require that the CTX
6966 have complete type in order to allow things like:
6968 template <class T> struct S { typename S<T>::X Y; };
6970 But, such constructs have already been resolved by this
6971 point, so here CTX really should have complete type, unless
6972 it's a partial instantiation. */
6973 ctx
= complete_type (ctx
);
6974 if (!COMPLETE_TYPE_P (ctx
))
6976 if (complain
& tf_error
)
6977 cxx_incomplete_type_error (NULL_TREE
, ctx
);
6978 return error_mark_node
;
6982 f
= make_typename_type (ctx
, f
,
6983 (complain
& tf_error
) | tf_keep_type_decl
);
6984 if (f
== error_mark_node
)
6986 if (TREE_CODE (f
) == TYPE_DECL
)
6988 complain
|= tf_ignore_bad_quals
;
6992 return cp_build_qualified_type_real
6993 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
6996 case UNBOUND_CLASS_TEMPLATE
:
6998 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6999 in_decl
, /*entering_scope=*/1);
7000 tree name
= TYPE_IDENTIFIER (t
);
7002 if (ctx
== error_mark_node
|| name
== error_mark_node
)
7003 return error_mark_node
;
7005 return make_unbound_class_template (ctx
, name
, complain
);
7010 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7011 if (e
== error_mark_node
)
7012 return error_mark_node
;
7013 return make_pointer_declarator (type
, e
);
7018 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7019 if (e
== error_mark_node
)
7020 return error_mark_node
;
7021 return make_reference_declarator (type
, e
);
7026 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7027 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7028 if (e1
== error_mark_node
|| e2
== error_mark_node
)
7029 return error_mark_node
;
7031 return build_nt (ARRAY_REF
, e1
, e2
, tsubst_expr
);
7036 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7037 tree e2
= (tsubst_call_declarator_parms
7038 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
7039 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
7042 if (e1
== error_mark_node
|| e2
== error_mark_node
7043 || e3
== error_mark_node
)
7044 return error_mark_node
;
7046 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
7051 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7052 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7053 if (e1
== error_mark_node
|| e2
== error_mark_node
)
7054 return error_mark_node
;
7056 return build_nt (TREE_CODE (t
), e1
, e2
);
7061 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
, in_decl
);
7062 if (e1
== error_mark_node
)
7063 return error_mark_node
;
7065 return cp_build_qualified_type_real (TREE_TYPE (e1
),
7067 | cp_type_quals (TREE_TYPE (e1
)),
7072 sorry ("use of `%s' in template",
7073 tree_code_name
[(int) TREE_CODE (t
)]);
7074 return error_mark_node
;
7078 /* Like tsubst, but deals with expressions. This function just replaces
7079 template parms; to finish processing the resultant expression, use
7083 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7085 enum tree_code code
;
7088 if (t
== NULL_TREE
|| t
== error_mark_node
)
7091 code
= TREE_CODE (t
);
7096 r
= retrieve_local_specialization (t
);
7097 my_friendly_assert (r
!= NULL
, 20020903);
7105 if (DECL_TEMPLATE_PARM_P (t
))
7106 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
7107 if (!DECL_CONTEXT (t
))
7108 /* This is a global enumeration constant. */
7111 /* Unfortunately, we cannot just call lookup_name here.
7114 template <int I> int f() {
7116 struct S { void g() { E e = a; } };
7119 When we instantiate f<7>::S::g(), say, lookup_name is not
7120 clever enough to find f<7>::a. */
7122 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
7123 /*entering_scope=*/0);
7125 for (v
= TYPE_VALUES (enum_type
);
7128 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
7129 return TREE_VALUE (v
);
7131 /* We didn't find the name. That should never happen; if
7132 name-lookup found it during preliminary parsing, we
7133 should find it again here during instantiation. */
7139 if (DECL_CONTEXT (t
))
7143 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
7144 /*entering_scope=*/1);
7145 if (ctx
!= DECL_CONTEXT (t
))
7146 return lookup_field (ctx
, DECL_NAME (t
), 0, false);
7152 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
7153 || local_variable_p (t
))
7154 t
= tsubst (t
, args
, complain
, in_decl
);
7161 tree qualifying_scope
;
7163 tree template_args
= 0;
7164 bool template_id_p
= false;
7166 /* A baselink indicates a function from a base class. The
7167 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7168 non-dependent types; otherwise, the lookup could not have
7169 succeeded. However, they may indicate bases of the template
7170 class, rather than the instantiated class.
7172 In addition, lookups that were not ambiguous before may be
7173 ambiguous now. Therefore, we perform the lookup again. */
7174 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (t
));
7175 fns
= BASELINK_FUNCTIONS (t
);
7176 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
7178 template_id_p
= true;
7179 template_args
= TREE_OPERAND (fns
, 1);
7180 fns
= TREE_OPERAND (fns
, 0);
7181 template_args
= tsubst_copy (template_args
, args
,
7183 maybe_fold_nontype_args (template_args
);
7185 name
= DECL_NAME (get_first_fn (fns
));
7186 t
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
7187 if (BASELINK_P (t
) && template_id_p
)
7188 BASELINK_FUNCTIONS (t
)
7189 = build_nt (TEMPLATE_ID_EXPR
,
7190 BASELINK_FUNCTIONS (t
),
7192 return adjust_result_of_qualified_name_lookup (t
,
7194 current_class_type
);
7198 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
7199 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
7200 args
, complain
, in_decl
);
7201 else if (is_member_template (t
))
7202 return tsubst (t
, args
, complain
, in_decl
);
7203 else if (DECL_CLASS_SCOPE_P (t
)
7204 && uses_template_parms (DECL_CONTEXT (t
)))
7206 /* Template template argument like the following example need
7209 template <template <class> class TT> struct C {};
7210 template <class T> struct D {
7211 template <class U> struct E {};
7216 We are processing the template argument `E' in #1 for
7217 the template instantiation #2. Originally, `E' is a
7218 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
7219 have to substitute this with one having context `D<int>'. */
7221 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
7222 return lookup_field (context
, DECL_NAME(t
), 0, false);
7225 /* Ordinary template template argument. */
7230 /* We must tsubst into a LOOKUP_EXPR in case the names to
7231 which it refers is a conversion operator; in that case the
7232 name will change. We avoid making unnecessary copies,
7235 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7237 if (id
!= TREE_OPERAND (t
, 0))
7239 r
= build_nt (LOOKUP_EXPR
, id
);
7240 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
7248 case REINTERPRET_CAST_EXPR
:
7249 case CONST_CAST_EXPR
:
7250 case STATIC_CAST_EXPR
:
7251 case DYNAMIC_CAST_EXPR
:
7254 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7255 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7259 case TRUTH_NOT_EXPR
:
7262 case CONVERT_EXPR
: /* Unary + */
7271 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7272 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7279 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7280 name
= TREE_OPERAND (t
, 1);
7281 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
7283 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7285 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7287 else if (TREE_CODE (name
) == SCOPE_REF
7288 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
7290 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7292 name
= TREE_OPERAND (name
, 1);
7293 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7295 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7296 name
= build_nt (SCOPE_REF
, base
, name
);
7299 name
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7300 return build_nt (COMPONENT_REF
, object
, name
);
7306 case TRUNC_DIV_EXPR
:
7308 case FLOOR_DIV_EXPR
:
7309 case ROUND_DIV_EXPR
:
7310 case EXACT_DIV_EXPR
:
7312 case BIT_ANDTC_EXPR
:
7315 case TRUNC_MOD_EXPR
:
7316 case FLOOR_MOD_EXPR
:
7317 case TRUTH_ANDIF_EXPR
:
7318 case TRUTH_ORIF_EXPR
:
7319 case TRUTH_AND_EXPR
:
7338 case PREDECREMENT_EXPR
:
7339 case PREINCREMENT_EXPR
:
7340 case POSTDECREMENT_EXPR
:
7341 case POSTINCREMENT_EXPR
:
7343 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7344 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7347 return build_nt (code
,
7348 tsubst_copy (TREE_OPERAND (t
, 0), args
,
7350 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7354 case METHOD_CALL_EXPR
:
7357 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7358 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7359 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
7363 /* This processing should really occur in tsubst_expr. However,
7364 tsubst_expr does not recurse into expressions, since it
7365 assumes that there aren't any statements inside them. So, we
7366 need to expand the STMT_EXPR here. */
7367 if (!processing_template_decl
)
7369 tree stmt_expr
= begin_stmt_expr ();
7370 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
);
7371 return finish_stmt_expr (stmt_expr
);
7378 case PSEUDO_DTOR_EXPR
:
7381 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7382 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7383 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7390 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7391 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7392 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7393 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7400 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7401 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7402 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7403 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7407 case TEMPLATE_ID_EXPR
:
7409 /* Substituted template arguments */
7410 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7413 maybe_fold_nontype_args (targs
);
7414 return lookup_template_function
7415 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7420 tree purpose
, value
, chain
;
7422 if (t
== void_list_node
)
7425 purpose
= TREE_PURPOSE (t
);
7427 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7428 value
= TREE_VALUE (t
);
7430 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7431 chain
= TREE_CHAIN (t
);
7432 if (chain
&& chain
!= void_type_node
)
7433 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7434 if (purpose
== TREE_PURPOSE (t
)
7435 && value
== TREE_VALUE (t
)
7436 && chain
== TREE_CHAIN (t
))
7438 return tree_cons (purpose
, value
, chain
);
7445 case TEMPLATE_TYPE_PARM
:
7446 case TEMPLATE_TEMPLATE_PARM
:
7447 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7448 case TEMPLATE_PARM_INDEX
:
7450 case REFERENCE_TYPE
:
7456 case UNBOUND_CLASS_TEMPLATE
:
7459 return tsubst (t
, args
, complain
, in_decl
);
7461 case IDENTIFIER_NODE
:
7462 if (IDENTIFIER_TYPENAME_P (t
))
7464 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7465 return mangle_conv_op_name_for_type (new_type
);
7472 r
= build_constructor
7473 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7474 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, complain
, in_decl
));
7475 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7480 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
7482 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7489 /* Like tsubst_copy for expressions, etc. but also does semantic
7493 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7497 if (t
== NULL_TREE
|| t
== error_mark_node
)
7500 if (processing_template_decl
)
7501 return tsubst_copy (t
, args
, complain
, in_decl
);
7503 if (!STATEMENT_CODE_P (TREE_CODE (t
)))
7504 return tsubst_copy_and_build (t
, args
, complain
, in_decl
);
7506 switch (TREE_CODE (t
))
7508 case CTOR_INITIALIZER
:
7510 finish_mem_initializers (tsubst_initializer_list
7511 (TREE_OPERAND (t
, 0), args
));
7516 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t
),
7517 args
, complain
, in_decl
));
7522 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7523 args
, complain
, in_decl
));
7528 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t
),
7529 args
, complain
, in_decl
));
7538 decl
= DECL_STMT_DECL (t
);
7539 if (TREE_CODE (decl
) == LABEL_DECL
)
7540 finish_label_decl (DECL_NAME (decl
));
7541 else if (TREE_CODE (decl
) == USING_DECL
)
7543 tree scope
= DECL_INITIAL (decl
);
7544 tree name
= DECL_NAME (decl
);
7546 scope
= tsubst_expr (scope
, args
, complain
, in_decl
);
7547 do_local_using_decl (lookup_qualified_name (scope
,
7554 init
= DECL_INITIAL (decl
);
7555 decl
= tsubst (decl
, args
, complain
, in_decl
);
7556 if (decl
!= error_mark_node
)
7559 DECL_INITIAL (decl
) = error_mark_node
;
7560 /* By marking the declaration as instantiated, we avoid
7561 trying to instantiate it. Since instantiate_decl can't
7562 handle local variables, and since we've already done
7563 all that needs to be done, that's the right thing to
7565 if (TREE_CODE (decl
) == VAR_DECL
)
7566 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7567 if (TREE_CODE (decl
) == VAR_DECL
7568 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
7569 /* Anonymous aggregates are a special case. */
7570 finish_anon_union (decl
);
7573 maybe_push_decl (decl
);
7574 if (DECL_PRETTY_FUNCTION_P (decl
))
7576 /* For __PRETTY_FUNCTION__ we have to adjust the
7578 const char *const name
7579 = cxx_printable_name (current_function_decl
, 2);
7580 init
= cp_fname_init (name
);
7581 TREE_TYPE (decl
) = TREE_TYPE (init
);
7584 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7585 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
7590 /* A DECL_STMT can also be used as an expression, in the condition
7591 clause of an if/for/while construct. If we aren't followed by
7592 another statement, return our decl. */
7593 if (TREE_CHAIN (t
) == NULL_TREE
)
7602 stmt
= begin_for_stmt ();
7603 tsubst_expr (FOR_INIT_STMT (t
), args
, complain
, in_decl
);
7604 finish_for_init_stmt (stmt
);
7605 finish_for_cond (tsubst_expr (FOR_COND (t
),
7606 args
, complain
, in_decl
),
7608 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7609 finish_for_expr (tmp
, stmt
);
7610 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7611 finish_for_stmt (stmt
);
7618 stmt
= begin_while_stmt ();
7619 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7620 args
, complain
, in_decl
),
7622 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7623 finish_while_stmt (stmt
);
7630 stmt
= begin_do_stmt ();
7631 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7632 finish_do_body (stmt
);
7633 finish_do_stmt (tsubst_expr (DO_COND (t
),
7634 args
, complain
, in_decl
),
7642 stmt
= begin_if_stmt ();
7643 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7644 args
, complain
, in_decl
),
7647 if (tmp
= THEN_CLAUSE (t
), tmp
)
7649 tsubst_expr (tmp
, args
, complain
, in_decl
);
7650 finish_then_clause (stmt
);
7653 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7655 begin_else_clause ();
7656 tsubst_expr (tmp
, args
, complain
, in_decl
);
7657 finish_else_clause (stmt
);
7667 if (COMPOUND_STMT_BODY_BLOCK (t
))
7668 stmt
= begin_function_body ();
7670 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7672 tsubst_expr (COMPOUND_BODY (t
), args
, complain
, in_decl
);
7674 if (COMPOUND_STMT_BODY_BLOCK (t
))
7675 finish_function_body (stmt
);
7677 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7683 finish_break_stmt ();
7688 finish_continue_stmt ();
7696 stmt
= begin_switch_stmt ();
7697 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7698 finish_switch_cond (val
, stmt
);
7699 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7700 finish_switch_stmt (stmt
);
7706 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7707 tsubst_expr (CASE_HIGH (t
), args
, complain
,
7712 input_line
= STMT_LINENO (t
);
7713 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7717 input_filename
= FILE_STMT_FILENAME (t
);
7718 add_stmt (build_nt (FILE_STMT
, FILE_STMT_FILENAME_NODE (t
)));
7723 tmp
= GOTO_DESTINATION (t
);
7724 if (TREE_CODE (tmp
) != LABEL_DECL
)
7725 /* Computed goto's must be tsubst'd into. On the other hand,
7726 non-computed gotos must not be; the identifier in question
7727 will have no binding. */
7728 tmp
= tsubst_expr (tmp
, args
, complain
, in_decl
);
7730 tmp
= DECL_NAME (tmp
);
7731 finish_goto_stmt (tmp
);
7736 tmp
= finish_asm_stmt
7738 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7739 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7740 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7741 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
7742 ASM_INPUT_P (tmp
) = ASM_INPUT_P (t
);
7749 stmt
= begin_try_block ();
7750 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7751 finish_cleanup_try_block (stmt
);
7752 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7758 if (FN_TRY_BLOCK_P (t
))
7759 stmt
= begin_function_try_block ();
7761 stmt
= begin_try_block ();
7763 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7765 if (FN_TRY_BLOCK_P (t
))
7766 finish_function_try_block (stmt
);
7768 finish_try_block (stmt
);
7770 tsubst_expr (TRY_HANDLERS (t
), args
, complain
, in_decl
);
7771 if (FN_TRY_BLOCK_P (t
))
7772 finish_function_handler_sequence (stmt
);
7774 finish_handler_sequence (stmt
);
7783 stmt
= begin_handler ();
7784 if (HANDLER_PARMS (t
))
7786 decl
= DECL_STMT_DECL (HANDLER_PARMS (t
));
7787 decl
= tsubst (decl
, args
, complain
, in_decl
);
7788 /* Prevent instantiate_decl from trying to instantiate
7789 this variable. We've already done all that needs to be
7791 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7795 finish_handler_parms (decl
, stmt
);
7796 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7797 finish_handler (stmt
);
7803 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
7810 return tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7813 /* Like tsubst but deals with expressions and performs semantic
7817 tsubst_copy_and_build (tree t
,
7819 tsubst_flags_t complain
,
7822 if (t
== NULL_TREE
|| t
== error_mark_node
)
7825 switch (TREE_CODE (t
))
7827 case IDENTIFIER_NODE
:
7828 if (IDENTIFIER_TYPENAME_P (t
))
7830 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7831 return do_identifier (mangle_conv_op_name_for_type (new_type
),
7835 return do_identifier (t
, NULL_TREE
);
7839 if (LOOKUP_EXPR_GLOBAL (t
))
7842 = tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7843 return do_scoped_id (token
, IDENTIFIER_GLOBAL_VALUE (token
));
7849 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7851 if (TREE_CODE (t
) == ALIAS_DECL
)
7852 t
= DECL_INITIAL (t
);
7857 case TEMPLATE_ID_EXPR
:
7861 = tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7863 = tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7865 if (TREE_CODE (template) == COMPONENT_REF
)
7867 object
= TREE_OPERAND (template, 0);
7868 template = TREE_OPERAND (template, 1);
7872 maybe_fold_nontype_args (targs
);
7873 template = lookup_template_function (template, targs
);
7876 return build (COMPONENT_REF
, TREE_TYPE (template),
7883 return build_x_indirect_ref
7884 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7888 return build_functional_cast
7889 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7890 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7892 case REINTERPRET_CAST_EXPR
:
7893 return build_reinterpret_cast
7894 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7895 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7897 case CONST_CAST_EXPR
:
7898 return build_const_cast
7899 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7900 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7902 case DYNAMIC_CAST_EXPR
:
7903 return build_dynamic_cast
7904 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7905 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7907 case STATIC_CAST_EXPR
:
7908 return build_static_cast
7909 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7910 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7912 case PREDECREMENT_EXPR
:
7913 case PREINCREMENT_EXPR
:
7914 case POSTDECREMENT_EXPR
:
7915 case POSTINCREMENT_EXPR
:
7917 return tsubst_copy (t
, args
, complain
, in_decl
);
7919 return build_x_unary_op
7921 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
7927 return tsubst_copy (t
, args
, complain
, in_decl
);
7929 return build_x_unary_op
7931 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
7937 return build_x_unary_op
7939 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7941 case TRUTH_NOT_EXPR
:
7943 case CONVERT_EXPR
: /* Unary + */
7947 return tsubst_copy (t
, args
, complain
, in_decl
);
7949 return build_x_unary_op
7951 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
7957 case TRUNC_DIV_EXPR
:
7959 case FLOOR_DIV_EXPR
:
7960 case ROUND_DIV_EXPR
:
7961 case EXACT_DIV_EXPR
:
7963 case BIT_ANDTC_EXPR
:
7966 case TRUNC_MOD_EXPR
:
7967 case FLOOR_MOD_EXPR
:
7968 case TRUTH_ANDIF_EXPR
:
7969 case TRUTH_ORIF_EXPR
:
7970 case TRUTH_AND_EXPR
:
7985 return build_x_binary_op
7987 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7988 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7991 return build_m_component_ref
7992 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7993 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7996 return build_offset_ref
7997 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7998 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
8002 if (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
)
8006 (ARRAY_REF
, NULL_TREE
,
8007 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
8010 return grok_array_decl
8011 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
8013 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
8021 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
8023 return TREE_CODE (t
) == SIZEOF_EXPR
?
8024 expr_sizeof (r
) : c_alignof_expr (r
);
8026 return cxx_sizeof_or_alignof_type (r
, TREE_CODE (t
), true);
8030 return build_x_modify_expr
8031 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8032 TREE_CODE (TREE_OPERAND (t
, 1)),
8033 tsubst_copy_and_build (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8036 return build_x_arrow
8037 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
8041 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8042 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8043 tsubst_copy_and_build (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
8044 NEW_EXPR_USE_GLOBAL (t
));
8047 return delete_sanity
8048 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8049 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8050 DELETE_EXPR_USE_VEC (t
),
8051 DELETE_EXPR_USE_GLOBAL (t
));
8055 if (tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
)
8057 return build_x_compound_expr
8058 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
8064 case METHOD_CALL_EXPR
:
8067 = tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
8069 if (TREE_CODE (method
) == SCOPE_REF
)
8071 tree name
= TREE_OPERAND (method
, 1);
8073 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
8074 name
= build_nt (TEMPLATE_ID_EXPR
,
8075 TREE_OPERAND (name
, 0),
8076 TREE_OPERAND (name
, 1));
8078 return build_scoped_method_call
8079 (tsubst_copy_and_build
8080 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8081 TREE_OPERAND (method
, 0),
8083 tsubst_copy_and_build
8084 (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8088 /* We can get a TEMPLATE_ID_EXPR here on code like:
8092 so we must resolve that. However, we can also get things
8093 like a BIT_NOT_EXPR here, when referring to a destructor,
8094 and things like that are not correctly resolved by this
8095 function so just use it when we really need it. */
8096 if (TREE_CODE (method
) == TEMPLATE_ID_EXPR
)
8097 method
= lookup_template_function
8098 (TREE_OPERAND (method
, 0),
8099 TREE_OPERAND (method
, 1));
8101 return build_method_call
8102 (tsubst_copy_and_build
8103 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8105 tsubst_copy_and_build
8106 (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
8107 NULL_TREE
, LOOKUP_NORMAL
);
8113 tree function
, copy_args
;
8115 function
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
8116 copy_args
= tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
,
8119 if (BASELINK_P (function
))
8120 return build_call_from_tree (function
, copy_args
, 1);
8121 else if (TREE_CODE (function
) == SCOPE_REF
)
8123 tree name
= TREE_OPERAND (function
, 1);
8124 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
8125 name
= build_nt (TEMPLATE_ID_EXPR
,
8126 TREE_OPERAND (name
, 0),
8127 build_expr_from_tree (TREE_OPERAND (name
, 1)));
8129 function
= resolve_scoped_fn_name (TREE_OPERAND (function
, 0),
8132 return build_call_from_tree (function
, copy_args
, 1);
8136 tree name
= function
;
8139 if (copy_args
!= NULL_TREE
&& TREE_CODE (name
) == LOOKUP_EXPR
8140 && !LOOKUP_EXPR_GLOBAL (name
)
8141 && (TREE_CODE ((id
= TREE_OPERAND (name
, 0)))
8143 && (!current_class_type
8144 || !lookup_member (current_class_type
, id
, 0, false)))
8146 /* Do Koenig lookup if there are no class members. */
8147 name
= do_identifier (id
, copy_args
);
8149 else if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
8150 || ! really_overloaded_fn (name
))
8151 name
= build_expr_from_tree (name
);
8153 if (TREE_CODE (name
) == OFFSET_REF
)
8154 return build_offset_ref_call_from_tree (name
, copy_args
);
8155 if (TREE_CODE (name
) == COMPONENT_REF
)
8156 return finish_object_call_expr (TREE_OPERAND (name
, 1),
8157 TREE_OPERAND (name
, 0),
8159 name
= convert_from_reference (name
);
8160 return build_call_from_tree (name
, copy_args
,
8161 /*disallow_virtual=*/false);
8166 return build_x_conditional_expr
8167 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8168 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8169 tsubst_copy_and_build (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8171 case PSEUDO_DTOR_EXPR
:
8172 return finish_pseudo_destructor_expr
8173 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8174 tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8175 tsubst_copy_and_build (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8179 tree purpose
, value
, chain
;
8181 if (t
== void_list_node
)
8184 purpose
= TREE_PURPOSE (t
);
8186 purpose
= tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
8187 value
= TREE_VALUE (t
);
8189 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
8190 chain
= TREE_CHAIN (t
);
8191 if (chain
&& chain
!= void_type_node
)
8192 chain
= tsubst_copy_and_build (chain
, args
, complain
, in_decl
);
8193 if (purpose
== TREE_PURPOSE (t
)
8194 && value
== TREE_VALUE (t
)
8195 && chain
== TREE_CHAIN (t
))
8197 return tree_cons (purpose
, value
, chain
);
8203 tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
8205 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
8207 if (!CLASS_TYPE_P (TREE_TYPE (object
)))
8209 if (TREE_CODE (member
) == BIT_NOT_EXPR
)
8210 return finish_pseudo_destructor_expr (object
,
8212 TREE_TYPE (object
));
8213 else if (TREE_CODE (member
) == SCOPE_REF
8214 && (TREE_CODE (TREE_OPERAND (member
, 1)) == BIT_NOT_EXPR
))
8215 return finish_pseudo_destructor_expr (object
,
8217 TREE_TYPE (object
));
8219 else if (TREE_CODE (member
) == SCOPE_REF
8220 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
8225 /* Lookup the template functions now that we know what the
8227 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
8228 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
8229 member
= lookup_qualified_name (TREE_OPERAND (member
, 0),
8233 if (BASELINK_P (member
))
8234 BASELINK_FUNCTIONS (member
)
8235 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
8239 error ("`%D' is not a member of `%T'",
8240 tmpl
, TREE_TYPE (object
));
8241 return error_mark_node
;
8245 return finish_class_member_access_expr (object
, member
);
8250 (tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
8256 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8259 /* digest_init will do the wrong thing if we let it. */
8260 if (type
&& TYPE_PTRMEMFUNC_P (type
))
8264 /* We do not want to process the purpose of aggregate
8265 initializers as they are identifier nodes which will be
8266 looked up by digest_init. */
8267 purpose_p
= !(type
&& IS_AGGR_TYPE (type
));
8268 for (elts
= CONSTRUCTOR_ELTS (t
);
8270 elts
= TREE_CHAIN (elts
))
8272 tree purpose
= TREE_PURPOSE (elts
);
8273 tree value
= TREE_VALUE (elts
);
8275 if (purpose
&& purpose_p
)
8277 = tsubst_copy_and_build (purpose
, args
, complain
, in_decl
);
8278 value
= tsubst_copy_and_build (value
, args
, complain
, in_decl
);
8279 r
= tree_cons (purpose
, value
, r
);
8282 r
= build_constructor (NULL_TREE
, nreverse (r
));
8283 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
8286 return digest_init (type
, r
, 0);
8293 = tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
, complain
,
8296 if (TYPE_P (operand_0
))
8297 return get_typeid (operand_0
);
8298 return build_typeid (operand_0
);
8302 return convert_from_reference (tsubst_copy (t
, args
, complain
, in_decl
));
8305 return convert_from_reference (tsubst_copy (t
, args
, complain
, in_decl
));
8308 return build_x_va_arg
8309 (tsubst_copy_and_build
8310 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8311 tsubst_copy (TREE_TYPE (t
), args
, complain
, in_decl
));
8314 return tsubst_copy (t
, args
, complain
, in_decl
);
8318 /* Verify that the instantiated ARGS are valid. For type arguments,
8319 make sure that the type's linkage is ok. For non-type arguments,
8320 make sure they are constants if they are integral or enumarations.
8321 Emit an error under control of COMPLAIN, and return TRUE on error. */
8324 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
8326 int ix
, len
= DECL_NTPARMS (tmpl
);
8327 bool result
= false;
8329 for (ix
= 0; ix
!= len
; ix
++)
8331 tree t
= TREE_VEC_ELT (args
, ix
);
8335 /* [basic.link]: A name with no linkage (notably, the name
8336 of a class or enumeration declared in a local scope)
8337 shall not be used to declare an entity with linkage.
8338 This implies that names with no linkage cannot be used as
8339 template arguments. */
8340 tree nt
= no_linkage_check (t
);
8344 if (!(complain
& tf_error
))
8346 else if (TYPE_ANONYMOUS_P (nt
))
8347 error ("`%T' uses anonymous type", t
);
8349 error ("`%T' uses local type `%T'", t
, nt
);
8352 /* In order to avoid all sorts of complications, we do not
8353 allow variably-modified types as template arguments. */
8354 else if (variably_modified_type_p (t
))
8356 if (complain
& tf_error
)
8357 error ("`%T' is a variably modified type", t
);
8361 /* A non-type argument of integral or enumerated type must be a
8363 else if (TREE_TYPE (t
)
8364 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
8365 && !TREE_CONSTANT (t
))
8367 if (complain
& tf_error
)
8368 error ("integral expression `%E' is not constant", t
);
8372 if (result
&& complain
& tf_error
)
8373 error (" trying to instantiate `%D'", tmpl
);
8377 /* Instantiate the indicated variable or function template TMPL with
8378 the template arguments in TARG_PTR. */
8381 instantiate_template (tree tmpl
, tree targ_ptr
, tsubst_flags_t complain
)
8387 if (tmpl
== error_mark_node
)
8388 return error_mark_node
;
8390 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
8392 /* If this function is a clone, handle it specially. */
8393 if (DECL_CLONED_FUNCTION_P (tmpl
))
8395 tree spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
,
8399 /* Look for the clone. */
8400 for (clone
= TREE_CHAIN (spec
);
8401 clone
&& DECL_CLONED_FUNCTION_P (clone
);
8402 clone
= TREE_CHAIN (clone
))
8403 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
8405 /* We should always have found the clone by now. */
8410 /* Check to see if we already have this specialization. */
8411 spec
= retrieve_specialization (tmpl
, targ_ptr
);
8412 if (spec
!= NULL_TREE
)
8415 gen_tmpl
= most_general_template (tmpl
);
8416 if (tmpl
!= gen_tmpl
)
8418 /* The TMPL is a partial instantiation. To get a full set of
8419 arguments we must add the arguments used to perform the
8420 partial instantiation. */
8421 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
8424 /* Check to see if we already have this specialization. */
8425 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
8426 if (spec
!= NULL_TREE
)
8430 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
8432 return error_mark_node
;
8434 /* Make sure that we can see identifiers, and compute access
8435 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8436 created earlier. Let push_access_scope_real figure that out. */
8437 push_access_scope_real
8438 (gen_tmpl
, targ_ptr
, tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
8439 complain
, gen_tmpl
));
8441 /* substitute template parameters */
8442 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
8443 targ_ptr
, complain
, gen_tmpl
);
8445 pop_access_scope (gen_tmpl
);
8447 /* The DECL_TI_TEMPLATE should always be the immediate parent
8448 template, not the most general template. */
8449 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
8451 if (flag_external_templates
)
8452 add_pending_template (fndecl
);
8454 /* If we've just instantiated the main entry point for a function,
8455 instantiate all the alternate entry points as well. We do this
8456 by cloning the instantiation of the main entry point, not by
8457 instantiating the template clones. */
8458 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
8459 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
8464 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8465 arguments that are being used when calling it. TARGS is a vector
8466 into which the deduced template arguments are placed.
8468 Return zero for success, 2 for an incomplete match that doesn't resolve
8469 all the types, and 1 for complete failure. An error message will be
8470 printed only for an incomplete match.
8472 If FN is a conversion operator, or we are trying to produce a specific
8473 specialization, RETURN_TYPE is the return type desired.
8475 The EXPLICIT_TARGS are explicit template arguments provided via a
8478 The parameter STRICT is one of:
8481 We are deducing arguments for a function call, as in
8485 We are deducing arguments for a conversion function, as in
8489 We are deducing arguments when doing an explicit instantiation
8490 as in [temp.explicit], when determining an explicit specialization
8491 as in [temp.expl.spec], or when taking the address of a function
8492 template, as in [temp.deduct.funcaddr].
8495 We are deducing arguments when calculating the partial
8496 ordering between specializations of function or class
8497 templates, as in [temp.func.order] and [temp.class.order].
8499 LEN is the number of parms to consider before returning success, or -1
8500 for all. This is used in partial ordering to avoid comparing parms for
8501 which no actual argument was passed, since they are not considered in
8502 overload resolution (and are explicitly excluded from consideration in
8503 partial ordering in [temp.func.order]/6). */
8506 fn_type_unification (tree fn
,
8507 tree explicit_targs
,
8511 unification_kind_t strict
,
8518 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
8520 fntype
= TREE_TYPE (fn
);
8525 The specified template arguments must match the template
8526 parameters in kind (i.e., type, nontype, template), and there
8527 must not be more arguments than there are parameters;
8528 otherwise type deduction fails.
8530 Nontype arguments must match the types of the corresponding
8531 nontype template parameters, or must be convertible to the
8532 types of the corresponding nontype parameters as specified in
8533 _temp.arg.nontype_, otherwise type deduction fails.
8535 All references in the function type of the function template
8536 to the corresponding template parameters are replaced by the
8537 specified template argument values. If a substitution in a
8538 template parameter or in the function type of the function
8539 template results in an invalid type, type deduction fails. */
8541 tree converted_args
;
8544 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
8545 explicit_targs
, NULL_TREE
, tf_none
,
8546 /*require_all_arguments=*/0));
8547 if (converted_args
== error_mark_node
)
8550 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
8551 if (fntype
== error_mark_node
)
8554 /* Place the explicitly specified arguments in TARGS. */
8555 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
8556 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
8559 parms
= TYPE_ARG_TYPES (fntype
);
8560 /* Never do unification on the 'this' parameter. */
8561 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
8562 parms
= TREE_CHAIN (parms
);
8566 /* We've been given a return type to match, prepend it. */
8567 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
8568 args
= tree_cons (NULL_TREE
, return_type
, args
);
8573 /* We allow incomplete unification without an error message here
8574 because the standard doesn't seem to explicitly prohibit it. Our
8575 callers must be ready to deal with unification failures in any
8577 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
8578 targs
, parms
, args
, /*subr=*/0,
8579 strict
, /*allow_incomplete*/1, len
);
8582 /* All is well so far. Now, check:
8586 When all template arguments have been deduced, all uses of
8587 template parameters in nondeduced contexts are replaced with
8588 the corresponding deduced argument values. If the
8589 substitution results in an invalid type, as described above,
8590 type deduction fails. */
8591 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
8598 /* Adjust types before performing type deduction, as described in
8599 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
8600 sections are symmetric. PARM is the type of a function parameter
8601 or the return type of the conversion function. ARG is the type of
8602 the argument passed to the call, or the type of the value
8603 initialized with the result of the conversion function. */
8606 maybe_adjust_types_for_deduction (unification_kind_t strict
,
8619 /* Swap PARM and ARG throughout the remainder of this
8620 function; the handling is precisely symmetric since PARM
8621 will initialize ARG rather than vice versa. */
8629 /* There is nothing to do in this case. */
8633 /* DR 214. [temp.func.order] is underspecified, and leads to no
8634 ordering between things like `T *' and `T const &' for `U *'.
8635 The former has T=U and the latter T=U*. The former looks more
8636 specialized and John Spicer considers it well-formed (the EDG
8637 compiler accepts it).
8639 John also confirms that deduction should proceed as in a function
8640 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
8641 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8642 to an actual call can have such a type.
8644 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8645 If only ARG is a REFERENCE_TYPE, we look through that and then
8646 proceed as with DEDUCE_CALL (which could further convert it). */
8647 if (TREE_CODE (*arg
) == REFERENCE_TYPE
)
8649 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
8651 *arg
= TREE_TYPE (*arg
);
8658 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
8660 /* [temp.deduct.call]
8662 If P is not a reference type:
8664 --If A is an array type, the pointer type produced by the
8665 array-to-pointer standard conversion (_conv.array_) is
8666 used in place of A for type deduction; otherwise,
8668 --If A is a function type, the pointer type produced by
8669 the function-to-pointer standard conversion
8670 (_conv.func_) is used in place of A for type deduction;
8673 --If A is a cv-qualified type, the top level
8674 cv-qualifiers of A's type are ignored for type
8676 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
8677 *arg
= build_pointer_type (TREE_TYPE (*arg
));
8678 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
8679 *arg
= build_pointer_type (*arg
);
8681 *arg
= TYPE_MAIN_VARIANT (*arg
);
8684 /* [temp.deduct.call]
8686 If P is a cv-qualified type, the top level cv-qualifiers
8687 of P's type are ignored for type deduction. If P is a
8688 reference type, the type referred to by P is used for
8690 *parm
= TYPE_MAIN_VARIANT (*parm
);
8691 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
8693 *parm
= TREE_TYPE (*parm
);
8694 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
8697 /* DR 322. For conversion deduction, remove a reference type on parm
8698 too (which has been swapped into ARG). */
8699 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
8700 *arg
= TREE_TYPE (*arg
);
8705 /* Most parms like fn_type_unification.
8707 If SUBR is 1, we're being called recursively (to unify the
8708 arguments of a function or method parameter of a function
8712 type_unification_real (tree tparms
,
8717 unification_kind_t strict
,
8718 int allow_incomplete
,
8723 int ntparms
= TREE_VEC_LENGTH (tparms
);
8725 int saw_undeduced
= 0;
8729 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
8730 my_friendly_assert (xparms
== NULL_TREE
8731 || TREE_CODE (xparms
) == TREE_LIST
, 290);
8732 /* ARGS could be NULL. */
8734 my_friendly_assert (TREE_CODE (xargs
) == TREE_LIST
, 291);
8735 my_friendly_assert (ntparms
> 0, 292);
8740 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
8741 | UNIFY_ALLOW_DERIVED
);
8745 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
8749 sub_strict
= UNIFY_ALLOW_NONE
;
8753 sub_strict
= UNIFY_ALLOW_NONE
;
8769 && parms
!= void_list_node
8771 && args
!= void_list_node
)
8773 parm
= TREE_VALUE (parms
);
8774 parms
= TREE_CHAIN (parms
);
8775 arg
= TREE_VALUE (args
);
8776 args
= TREE_CHAIN (args
);
8778 if (arg
== error_mark_node
)
8780 if (arg
== unknown_type_node
)
8781 /* We can't deduce anything from this, but we might get all the
8782 template args from other function args. */
8785 /* Conversions will be performed on a function argument that
8786 corresponds with a function parameter that contains only
8787 non-deducible template parameters and explicitly specified
8788 template parameters. */
8789 if (! uses_template_parms (parm
))
8794 type
= TREE_TYPE (arg
);
8801 if (strict
== DEDUCE_EXACT
|| strict
== DEDUCE_ORDER
)
8803 if (same_type_p (parm
, type
))
8807 /* It might work; we shouldn't check now, because we might
8808 get into infinite recursion. Overload resolution will
8817 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
8818 if (type_unknown_p (arg
))
8820 /* [temp.deduct.type] A template-argument can be deduced from
8821 a pointer to function or pointer to member function
8822 argument if the set of overloaded functions does not
8823 contain function templates and at most one of a set of
8824 overloaded functions provides a unique match. */
8826 if (resolve_overloaded_unification
8827 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
8832 arg
= TREE_TYPE (arg
);
8836 int arg_strict
= sub_strict
;
8839 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8841 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
8845 /* Are we done with the interesting parms? */
8849 /* Fail if we've reached the end of the parm list, and more args
8850 are present, and the parm list isn't variadic. */
8851 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
8853 /* Fail if parms are left and they don't have default values. */
8855 && parms
!= void_list_node
8856 && TREE_PURPOSE (parms
) == NULL_TREE
)
8861 for (i
= 0; i
< ntparms
; i
++)
8862 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
8864 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
8866 /* If this is an undeduced nontype parameter that depends on
8867 a type parameter, try another pass; its type may have been
8868 deduced from a later argument than the one from which
8869 this parameter can be deduced. */
8870 if (TREE_CODE (tparm
) == PARM_DECL
8871 && uses_template_parms (TREE_TYPE (tparm
))
8872 && !saw_undeduced
++)
8875 if (!allow_incomplete
)
8876 error ("incomplete type unification");
8882 /* Subroutine of type_unification_real. Args are like the variables at the
8883 call site. ARG is an overloaded function (or template-id); we try
8884 deducing template args from each of the overloads, and if only one
8885 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8888 resolve_overloaded_unification (tree tparms
,
8892 unification_kind_t strict
,
8895 tree tempargs
= copy_node (targs
);
8898 if (TREE_CODE (arg
) == ADDR_EXPR
)
8899 arg
= TREE_OPERAND (arg
, 0);
8901 if (TREE_CODE (arg
) == COMPONENT_REF
)
8902 /* Handle `&x' where `x' is some static or non-static member
8904 arg
= TREE_OPERAND (arg
, 1);
8906 if (TREE_CODE (arg
) == OFFSET_REF
)
8907 arg
= TREE_OPERAND (arg
, 1);
8909 /* Strip baselink information. */
8910 if (BASELINK_P (arg
))
8911 arg
= BASELINK_FUNCTIONS (arg
);
8913 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
8915 /* If we got some explicit template args, we need to plug them into
8916 the affected templates before we try to unify, in case the
8917 explicit args will completely resolve the templates in question. */
8919 tree expl_subargs
= TREE_OPERAND (arg
, 1);
8920 arg
= TREE_OPERAND (arg
, 0);
8922 for (; arg
; arg
= OVL_NEXT (arg
))
8924 tree fn
= OVL_CURRENT (arg
);
8927 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
8930 subargs
= get_bindings_overload (fn
, DECL_TEMPLATE_RESULT (fn
),
8934 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
8935 if (TREE_CODE (elem
) == METHOD_TYPE
)
8936 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
8937 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
8938 strict
, sub_strict
);
8942 else if (TREE_CODE (arg
) == OVERLOAD
8943 || TREE_CODE (arg
) == FUNCTION_DECL
)
8945 for (; arg
; arg
= OVL_NEXT (arg
))
8947 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
8948 if (TREE_CODE (type
) == METHOD_TYPE
)
8949 type
= build_ptrmemfunc_type (build_pointer_type (type
));
8950 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
8952 strict
, sub_strict
);
8958 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8959 to function or pointer to member function argument if the set of
8960 overloaded functions does not contain function templates and at most
8961 one of a set of overloaded functions provides a unique match.
8963 So if we found multiple possibilities, we return success but don't
8968 int i
= TREE_VEC_LENGTH (targs
);
8970 if (TREE_VEC_ELT (tempargs
, i
))
8971 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
8979 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8980 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8981 different overloads deduce different arguments for a given parm.
8982 Returns 1 on success. */
8985 try_one_overload (tree tparms
,
8990 unification_kind_t strict
,
8997 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8998 to function or pointer to member function argument if the set of
8999 overloaded functions does not contain function templates and at most
9000 one of a set of overloaded functions provides a unique match.
9002 So if this is a template, just return success. */
9004 if (uses_template_parms (arg
))
9007 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
9009 /* We don't copy orig_targs for this because if we have already deduced
9010 some template args from previous args, unify would complain when we
9011 try to deduce a template parameter for the same argument, even though
9012 there isn't really a conflict. */
9013 nargs
= TREE_VEC_LENGTH (targs
);
9014 tempargs
= make_tree_vec (nargs
);
9016 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
9019 /* First make sure we didn't deduce anything that conflicts with
9020 explicitly specified args. */
9021 for (i
= nargs
; i
--; )
9023 tree elt
= TREE_VEC_ELT (tempargs
, i
);
9024 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
9026 if (elt
== NULL_TREE
)
9028 else if (uses_template_parms (elt
))
9030 /* Since we're unifying against ourselves, we will fill in template
9031 args used in the function parm list with our own template parms.
9033 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
9036 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
9040 for (i
= nargs
; i
--; )
9042 tree elt
= TREE_VEC_ELT (tempargs
, i
);
9045 TREE_VEC_ELT (targs
, i
) = elt
;
9051 /* Verify that nondeduce template argument agrees with the type
9052 obtained from argument deduction. Return nonzero if the
9057 struct A { typedef int X; };
9058 template <class T, class U> struct C {};
9059 template <class T> struct C<T, typename T::X> {};
9061 Then with the instantiation `C<A, int>', we can deduce that
9062 `T' is `A' but unify () does not check whether `typename T::X'
9063 is `int'. This function ensure that they agree.
9065 TARGS, PARMS are the same as the arguments of unify.
9066 ARGS contains template arguments from all levels. */
9069 verify_class_unification (tree targs
, tree parms
, tree args
)
9071 parms
= tsubst (parms
, add_outermost_template_args (args
, targs
),
9072 tf_none
, NULL_TREE
);
9073 if (parms
== error_mark_node
)
9076 return !comp_template_args (parms
, INNERMOST_TEMPLATE_ARGS (args
));
9079 /* PARM is a template class (perhaps with unbound template
9080 parameters). ARG is a fully instantiated type. If ARG can be
9081 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9082 TARGS are as for unify. */
9085 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
9089 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
9090 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
9091 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
9094 /* We need to make a new template argument vector for the call to
9095 unify. If we used TARGS, we'd clutter it up with the result of
9096 the attempted unification, even if this class didn't work out.
9097 We also don't want to commit ourselves to all the unifications
9098 we've already done, since unification is supposed to be done on
9099 an argument-by-argument basis. In other words, consider the
9100 following pathological case:
9102 template <int I, int J, int K>
9105 template <int I, int J>
9106 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9108 template <int I, int J, int K>
9109 void f(S<I, J, K>, S<I, I, I>);
9118 Now, by the time we consider the unification involving `s2', we
9119 already know that we must have `f<0, 0, 0>'. But, even though
9120 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
9121 because there are two ways to unify base classes of S<0, 1, 2>
9122 with S<I, I, I>. If we kept the already deduced knowledge, we
9123 would reject the possibility I=1. */
9124 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
9126 /* If unification failed, we're done. */
9127 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
9128 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
9134 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
9135 have already discovered to be satisfactory. ARG_BINFO is the binfo
9136 for the base class of ARG that we are currently examining. */
9139 get_template_base_recursive (tree tparms
,
9148 tree arg
= BINFO_TYPE (arg_binfo
);
9150 if (!(flags
& GTB_IGNORE_TYPE
))
9152 tree r
= try_class_unification (tparms
, targs
,
9155 /* If there is more than one satisfactory baseclass, then:
9159 If they yield more than one possible deduced A, the type
9163 if (r
&& rval
&& !same_type_p (r
, rval
))
9164 return error_mark_node
;
9169 binfos
= BINFO_BASETYPES (arg_binfo
);
9170 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
9172 /* Process base types. */
9173 for (i
= 0; i
< n_baselinks
; i
++)
9175 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
9178 /* Skip this base, if we've already seen it. */
9179 if (BINFO_MARKED (base_binfo
))
9183 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
9185 /* When searching for a non-virtual, we cannot mark virtually
9188 BINFO_MARKED (base_binfo
) = 1;
9190 rval
= get_template_base_recursive (tparms
, targs
,
9194 GTB_VIA_VIRTUAL
* this_virtual
);
9196 /* If we discovered more than one matching base class, we can
9198 if (rval
== error_mark_node
)
9199 return error_mark_node
;
9205 /* Given a template type PARM and a class type ARG, find the unique
9206 base type in ARG that is an instance of PARM. We do not examine
9207 ARG itself; only its base-classes. If there is no appropriate base
9208 class, return NULL_TREE. If there is more than one, return
9209 error_mark_node. PARM may be the type of a partial specialization,
9210 as well as a plain template type. Used by unify. */
9213 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
9218 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
9220 arg_binfo
= TYPE_BINFO (complete_type (arg
));
9221 rval
= get_template_base_recursive (tparms
, targs
,
9226 /* Since get_template_base_recursive marks the bases classes, we
9227 must unmark them here. */
9228 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
9233 /* Returns the level of DECL, which declares a template parameter. */
9236 template_decl_level (tree decl
)
9238 switch (TREE_CODE (decl
))
9242 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
9245 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
9253 /* Decide whether ARG can be unified with PARM, considering only the
9254 cv-qualifiers of each type, given STRICT as documented for unify.
9255 Returns nonzero iff the unification is OK on that basis.*/
9258 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
9260 int arg_quals
= cp_type_quals (arg
);
9261 int parm_quals
= cp_type_quals (parm
);
9263 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
)
9265 /* If the cvr quals of parm will not unify with ARG, they'll be
9266 ignored in instantiation, so we have to do the same here. */
9267 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
9268 parm_quals
&= ~(TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
);
9269 if (!POINTER_TYPE_P (arg
) &&
9270 TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
9271 parm_quals
&= ~TYPE_QUAL_RESTRICT
;
9274 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
9275 && (arg_quals
& parm_quals
) != parm_quals
)
9278 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
9279 && (parm_quals
& arg_quals
) != arg_quals
)
9285 /* Takes parameters as for type_unification. Returns 0 if the
9286 type deduction succeeds, 1 otherwise. The parameter STRICT is a
9287 bitwise or of the following flags:
9290 Require an exact match between PARM and ARG.
9291 UNIFY_ALLOW_MORE_CV_QUAL:
9292 Allow the deduced ARG to be more cv-qualified (by qualification
9293 conversion) than ARG.
9294 UNIFY_ALLOW_LESS_CV_QUAL:
9295 Allow the deduced ARG to be less cv-qualified than ARG.
9296 UNIFY_ALLOW_DERIVED:
9297 Allow the deduced ARG to be a template base class of ARG,
9298 or a pointer to a template base class of the type pointed to by
9300 UNIFY_ALLOW_INTEGER:
9301 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
9302 case for more information.
9303 UNIFY_ALLOW_OUTER_LEVEL:
9304 This is the outermost level of a deduction. Used to determine validity
9305 of qualification conversions. A valid qualification conversion must
9306 have const qualified pointers leading up to the inner type which
9307 requires additional CV quals, except at the outer level, where const
9308 is not required [conv.qual]. It would be normal to set this flag in
9309 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9310 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9311 This is the outermost level of a deduction, and PARM can be more CV
9312 qualified at this point.
9313 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9314 This is the outermost level of a deduction, and PARM can be less CV
9315 qualified at this point.
9316 UNIFY_ALLOW_MAX_CORRECTION:
9317 This is an INTEGER_TYPE's maximum value. Used if the range may
9318 have been derived from a size specification, such as an array size.
9319 If the size was given by a nontype template parameter N, the maximum
9320 value will have the form N-1. The flag says that we can (and indeed
9321 must) unify N with (ARG + 1), an exception to the normal rules on
9325 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
9330 int strict_in
= strict
;
9332 /* I don't think this will do the right thing with respect to types.
9333 But the only case I've seen it in so far has been array bounds, where
9334 signedness is the only information lost, and I think that will be
9336 while (TREE_CODE (parm
) == NOP_EXPR
)
9337 parm
= TREE_OPERAND (parm
, 0);
9339 if (arg
== error_mark_node
)
9341 if (arg
== unknown_type_node
)
9342 /* We can't deduce anything from this, but we might get all the
9343 template args from other function args. */
9346 /* If PARM uses template parameters, then we can't bail out here,
9347 even if ARG == PARM, since we won't record unifications for the
9348 template parameters. We might need them if we're trying to
9349 figure out which of two things is more specialized. */
9350 if (arg
== parm
&& !uses_template_parms (parm
))
9353 /* Immediately reject some pairs that won't unify because of
9354 cv-qualification mismatches. */
9355 if (TREE_CODE (arg
) == TREE_CODE (parm
)
9357 /* It is the elements of the array which hold the cv quals of an array
9358 type, and the elements might be template type parms. We'll check
9360 && TREE_CODE (arg
) != ARRAY_TYPE
9361 /* We check the cv-qualifiers when unifying with template type
9362 parameters below. We want to allow ARG `const T' to unify with
9363 PARM `T' for example, when computing which of two templates
9364 is more specialized, for example. */
9365 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
9366 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
9369 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
9370 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
9371 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
9372 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
9373 strict
&= ~UNIFY_ALLOW_DERIVED
;
9374 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
9375 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
9376 strict
&= ~UNIFY_ALLOW_MAX_CORRECTION
;
9378 switch (TREE_CODE (parm
))
9382 case UNBOUND_CLASS_TEMPLATE
:
9383 /* In a type which contains a nested-name-specifier, template
9384 argument values cannot be deduced for template parameters used
9385 within the nested-name-specifier. */
9388 case TEMPLATE_TYPE_PARM
:
9389 case TEMPLATE_TEMPLATE_PARM
:
9390 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9391 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
9393 if (TEMPLATE_TYPE_LEVEL (parm
)
9394 != template_decl_level (tparm
))
9395 /* The PARM is not one we're trying to unify. Just check
9396 to see if it matches ARG. */
9397 return (TREE_CODE (arg
) == TREE_CODE (parm
)
9398 && same_type_p (parm
, arg
)) ? 0 : 1;
9399 idx
= TEMPLATE_TYPE_IDX (parm
);
9400 targ
= TREE_VEC_ELT (targs
, idx
);
9401 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
9403 /* Check for mixed types and values. */
9404 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
9405 && TREE_CODE (tparm
) != TYPE_DECL
)
9406 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
9407 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
9410 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9412 /* ARG must be constructed from a template class or a template
9413 template parameter. */
9414 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
9415 && (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
)))
9419 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
9420 tree parmvec
= TYPE_TI_ARGS (parm
);
9421 tree argvec
= TYPE_TI_ARGS (arg
);
9423 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
9426 /* The parameter and argument roles have to be switched here
9427 in order to handle default arguments properly. For example,
9428 template<template <class> class TT> void f(TT<int>)
9429 should be able to accept vector<int> which comes from
9430 template <class T, class Allocator = allocator>
9433 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
9437 /* Deduce arguments T, i from TT<T> or TT<i>.
9438 We check each element of PARMVEC and ARGVEC individually
9439 rather than the whole TREE_VEC since they can have
9440 different number of elements. */
9442 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
9444 tree t
= TREE_VEC_ELT (parmvec
, i
);
9446 if (unify (tparms
, targs
, t
,
9447 TREE_VEC_ELT (argvec
, i
),
9452 arg
= TYPE_TI_TEMPLATE (arg
);
9454 /* Fall through to deduce template name. */
9457 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
9458 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9460 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9462 /* Simple cases: Value already set, does match or doesn't. */
9463 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
9470 /* If ARG is an offset type, we're trying to unify '*T' with
9471 'U C::*', which is ill-formed. See the comment in the
9472 POINTER_TYPE case about this ugliness. */
9473 if (TREE_CODE (arg
) == OFFSET_TYPE
)
9476 /* If PARM is `const T' and ARG is only `int', we don't have
9477 a match unless we are allowing additional qualification.
9478 If ARG is `const int' and PARM is just `T' that's OK;
9479 that binds `const int' to `T'. */
9480 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
9484 /* Consider the case where ARG is `const volatile int' and
9485 PARM is `const T'. Then, T should be `volatile int'. */
9486 arg
= cp_build_qualified_type_real
9487 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
9488 if (arg
== error_mark_node
)
9491 /* Simple cases: Value already set, does match or doesn't. */
9492 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
9497 /* Make sure that ARG is not a variable-sized array. (Note
9498 that were talking about variable-sized arrays (like
9499 `int[n]'), rather than arrays of unknown size (like
9500 `int[]').) We'll get very confused by such a type since
9501 the bound of the array will not be computable in an
9502 instantiation. Besides, such types are not allowed in
9503 ISO C++, so we can do as we please here. */
9504 if (variably_modified_type_p (arg
))
9508 TREE_VEC_ELT (targs
, idx
) = arg
;
9511 case TEMPLATE_PARM_INDEX
:
9512 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
9514 if (TEMPLATE_PARM_LEVEL (parm
)
9515 != template_decl_level (tparm
))
9516 /* The PARM is not one we're trying to unify. Just check
9517 to see if it matches ARG. */
9518 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
9519 && cp_tree_equal (parm
, arg
));
9521 idx
= TEMPLATE_PARM_IDX (parm
);
9522 targ
= TREE_VEC_ELT (targs
, idx
);
9525 return !cp_tree_equal (targ
, arg
);
9527 /* [temp.deduct.type] If, in the declaration of a function template
9528 with a non-type template-parameter, the non-type
9529 template-parameter is used in an expression in the function
9530 parameter-list and, if the corresponding template-argument is
9531 deduced, the template-argument type shall match the type of the
9532 template-parameter exactly, except that a template-argument
9533 deduced from an array bound may be of any integral type.
9534 The non-type parameter might use already deduced type parameters. */
9535 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
9536 if (!TREE_TYPE (arg
))
9537 /* Template-parameter dependent expression. Just accept it for now.
9538 It will later be processed in convert_template_argument. */
9540 else if (same_type_p (TREE_TYPE (arg
), tparm
))
9542 else if ((strict
& UNIFY_ALLOW_INTEGER
)
9543 && (TREE_CODE (tparm
) == INTEGER_TYPE
9544 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
9546 else if (uses_template_parms (tparm
))
9547 /* We haven't deduced the type of this parameter yet. Try again
9553 TREE_VEC_ELT (targs
, idx
) = arg
;
9558 if (TREE_CODE (arg
) != POINTER_TYPE
)
9561 /* [temp.deduct.call]
9563 A can be another pointer or pointer to member type that can
9564 be converted to the deduced A via a qualification
9565 conversion (_conv.qual_).
9567 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9568 This will allow for additional cv-qualification of the
9569 pointed-to types if appropriate. */
9571 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
9572 /* The derived-to-base conversion only persists through one
9573 level of pointers. */
9574 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
9576 if (TREE_CODE (TREE_TYPE (parm
)) == OFFSET_TYPE
9577 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
9579 /* Avoid getting confused about cv-quals; don't recurse here.
9580 Pointers to members should really be just OFFSET_TYPE, not
9581 this two-level nonsense... */
9583 parm
= TREE_TYPE (parm
);
9584 arg
= TREE_TYPE (arg
);
9588 return unify (tparms
, targs
, TREE_TYPE (parm
),
9589 TREE_TYPE (arg
), strict
);
9592 case REFERENCE_TYPE
:
9593 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
9595 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
9596 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
9599 if (TREE_CODE (arg
) != ARRAY_TYPE
)
9601 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
9602 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
9604 if (TYPE_DOMAIN (parm
) != NULL_TREE
9605 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
9606 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
9608 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
9609 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
9617 if (TREE_CODE (arg
) != TREE_CODE (parm
))
9620 if (TREE_CODE (parm
) == INTEGER_TYPE
9621 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
9623 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
9624 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
9625 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
9627 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
9628 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
9629 TYPE_MAX_VALUE (arg
),
9630 UNIFY_ALLOW_INTEGER
| UNIFY_ALLOW_MAX_CORRECTION
))
9633 /* We have already checked cv-qualification at the top of the
9635 else if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
9638 /* As far as unification is concerned, this wins. Later checks
9639 will invalidate it if necessary. */
9642 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
9643 /* Type INTEGER_CST can come from ordinary constant template args. */
9645 while (TREE_CODE (arg
) == NOP_EXPR
)
9646 arg
= TREE_OPERAND (arg
, 0);
9648 if (TREE_CODE (arg
) != INTEGER_CST
)
9650 return !tree_int_cst_equal (parm
, arg
);
9655 if (TREE_CODE (arg
) != TREE_VEC
)
9657 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
9659 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
9660 if (unify (tparms
, targs
,
9661 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
9669 if (TREE_CODE (arg
) != TREE_CODE (parm
))
9672 if (TYPE_PTRMEMFUNC_P (parm
))
9674 if (!TYPE_PTRMEMFUNC_P (arg
))
9677 return unify (tparms
, targs
,
9678 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
9679 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
9683 if (CLASSTYPE_TEMPLATE_INFO (parm
))
9687 if (strict_in
& UNIFY_ALLOW_DERIVED
)
9689 /* First, we try to unify the PARM and ARG directly. */
9690 t
= try_class_unification (tparms
, targs
,
9695 /* Fallback to the special case allowed in
9698 If P is a class, and P has the form
9699 template-id, then A can be a derived class of
9700 the deduced A. Likewise, if P is a pointer to
9701 a class of the form template-id, A can be a
9702 pointer to a derived class pointed to by the
9704 t
= get_template_base (tparms
, targs
,
9707 if (! t
|| t
== error_mark_node
)
9711 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
9712 && (CLASSTYPE_TI_TEMPLATE (parm
)
9713 == CLASSTYPE_TI_TEMPLATE (arg
)))
9714 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9715 Then, we should unify `int' and `U'. */
9718 /* There's no chance of unification succeeding. */
9721 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
9722 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
9724 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
9730 if (TREE_CODE (arg
) != TREE_CODE (parm
))
9733 if (unify (tparms
, targs
, TREE_TYPE (parm
),
9734 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
9736 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
9737 TYPE_ARG_TYPES (arg
), 1,
9738 DEDUCE_EXACT
, 0, -1);
9742 if (TREE_CODE (arg
) != OFFSET_TYPE
)
9744 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
9745 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
9747 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
9751 if (DECL_TEMPLATE_PARM_P (parm
))
9752 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
9753 if (arg
!= decl_constant_value (parm
))
9758 /* Matched cases are handled by the ARG == PARM test above. */
9762 if (tree_int_cst_equal (TREE_OPERAND (parm
, 1), integer_one_node
)
9763 && (strict_in
& UNIFY_ALLOW_MAX_CORRECTION
))
9765 /* We handle this case specially, since it comes up with
9766 arrays. In particular, something like:
9768 template <int N> void f(int (&x)[N]);
9770 Here, we are trying to unify the range type, which
9771 looks like [0 ... (N - 1)]. */
9773 t1
= TREE_OPERAND (parm
, 0);
9774 t2
= TREE_OPERAND (parm
, 1);
9776 t
= fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
));
9778 return unify (tparms
, targs
, t1
, t
, strict
);
9780 /* else fall through */
9783 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
9786 /* We're looking at an expression. This can happen with
9790 void foo(S<I>, S<I + 2>);
9792 This is a "nondeduced context":
9796 The nondeduced contexts are:
9798 --A type that is a template-id in which one or more of
9799 the template-arguments is an expression that references
9800 a template-parameter.
9802 In these cases, we assume deduction succeeded, but don't
9803 actually infer any unifications. */
9805 if (!uses_template_parms (parm
)
9806 && !template_args_equal (parm
, arg
))
9812 sorry ("use of `%s' in template type unification",
9813 tree_code_name
[(int) TREE_CODE (parm
)]);
9819 /* Called if RESULT is explicitly instantiated, or is a member of an
9820 explicitly instantiated class, or if using -frepo and the
9821 instantiation of RESULT has been assigned to this file. */
9824 mark_decl_instantiated (tree result
, int extern_p
)
9826 /* We used to set this unconditionally; we moved that to
9827 do_decl_instantiation so it wouldn't get set on members of
9828 explicit class template instantiations. But we still need to set
9829 it here for the 'extern template' case in order to suppress
9830 implicit instantiations. */
9832 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9834 /* If this entity has already been written out, it's too late to
9835 make any modifications. */
9836 if (TREE_ASM_WRITTEN (result
))
9839 if (TREE_CODE (result
) != FUNCTION_DECL
)
9840 /* The TREE_PUBLIC flag for function declarations will have been
9841 set correctly by tsubst. */
9842 TREE_PUBLIC (result
) = 1;
9844 /* This might have been set by an earlier implicit instantiation. */
9845 DECL_COMDAT (result
) = 0;
9849 DECL_INTERFACE_KNOWN (result
) = 1;
9850 DECL_NOT_REALLY_EXTERN (result
) = 1;
9852 /* Always make artificials weak. */
9853 if (DECL_ARTIFICIAL (result
) && flag_weak
)
9854 comdat_linkage (result
);
9855 /* For WIN32 we also want to put explicit instantiations in
9856 linkonce sections. */
9857 else if (TREE_PUBLIC (result
))
9858 maybe_make_one_only (result
);
9861 if (TREE_CODE (result
) == FUNCTION_DECL
)
9865 /* Given two function templates PAT1 and PAT2, return:
9867 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9869 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9870 -1 if PAT2 is more specialized than PAT1.
9871 0 if neither is more specialized.
9873 LEN is passed through to fn_type_unification. */
9876 more_specialized (tree pat1
, tree pat2
, int deduce
, int len
)
9881 targs
= get_bindings_real (pat1
, DECL_TEMPLATE_RESULT (pat2
),
9882 NULL_TREE
, 0, deduce
, len
);
9886 targs
= get_bindings_real (pat2
, DECL_TEMPLATE_RESULT (pat1
),
9887 NULL_TREE
, 0, deduce
, len
);
9894 /* Given two class template specialization list nodes PAT1 and PAT2, return:
9896 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9897 -1 if PAT2 is more specialized than PAT1.
9898 0 if neither is more specialized. */
9901 more_specialized_class (tree pat1
, tree pat2
)
9906 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
9907 TREE_PURPOSE (pat2
));
9911 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
9912 TREE_PURPOSE (pat1
));
9919 /* Return the template arguments that will produce the function signature
9920 DECL from the function template FN, with the explicit template
9921 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
9922 also match. Return NULL_TREE if no satisfactory arguments could be
9923 found. DEDUCE and LEN are passed through to fn_type_unification. */
9926 get_bindings_real (tree fn
,
9933 int ntparms
= DECL_NTPARMS (fn
);
9934 tree targs
= make_tree_vec (ntparms
);
9936 tree decl_arg_types
;
9939 /* Substitute the explicit template arguments into the type of DECL.
9940 The call to fn_type_unification will handle substitution into the
9942 decl_type
= TREE_TYPE (decl
);
9943 if (explicit_args
&& uses_template_parms (decl_type
))
9946 tree converted_args
;
9948 if (DECL_TEMPLATE_INFO (decl
))
9949 tmpl
= DECL_TI_TEMPLATE (decl
);
9951 /* We can get here for some invalid specializations. */
9955 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
9956 explicit_args
, NULL_TREE
,
9957 tf_none
, /*require_all_arguments=*/0));
9958 if (converted_args
== error_mark_node
)
9961 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
9962 if (decl_type
== error_mark_node
)
9966 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
9967 /* Never do unification on the 'this' parameter. */
9968 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
9969 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
9971 i
= fn_type_unification (fn
, explicit_args
, targs
,
9973 (check_rettype
|| DECL_CONV_FN_P (fn
)
9974 ? TREE_TYPE (decl_type
) : NULL_TREE
),
9983 /* For most uses, we want to check the return type. */
9986 get_bindings (tree fn
, tree decl
, tree explicit_args
)
9988 return get_bindings_real (fn
, decl
, explicit_args
, 1, DEDUCE_EXACT
, -1);
9991 /* But for resolve_overloaded_unification, we only care about the parameter
9995 get_bindings_overload (tree fn
, tree decl
, tree explicit_args
)
9997 return get_bindings_real (fn
, decl
, explicit_args
, 0, DEDUCE_EXACT
, -1);
10000 /* Return the innermost template arguments that, when applied to a
10001 template specialization whose innermost template parameters are
10002 TPARMS, and whose specialization arguments are PARMS, yield the
10005 For example, suppose we have:
10007 template <class T, class U> struct S {};
10008 template <class T> struct S<T*, int> {};
10010 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
10011 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
10012 int}. The resulting vector will be {double}, indicating that `T'
10013 is bound to `double'. */
10016 get_class_bindings (tree tparms
, tree parms
, tree args
)
10018 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
10019 tree vec
= make_tree_vec (ntparms
);
10021 if (unify (tparms
, vec
, parms
, INNERMOST_TEMPLATE_ARGS (args
),
10025 for (i
= 0; i
< ntparms
; ++i
)
10026 if (! TREE_VEC_ELT (vec
, i
))
10029 if (verify_class_unification (vec
, parms
, args
))
10035 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10036 Pick the most specialized template, and return the corresponding
10037 instantiation, or if there is no corresponding instantiation, the
10038 template itself. If there is no most specialized template,
10039 error_mark_node is returned. If there are no templates at all,
10040 NULL_TREE is returned. */
10043 most_specialized_instantiation (tree instantiations
)
10048 if (!instantiations
)
10051 champ
= instantiations
;
10052 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
10054 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
10062 fn
= TREE_CHAIN (fn
);
10064 return error_mark_node
;
10070 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
10072 fate
= more_specialized (TREE_VALUE (champ
), TREE_VALUE (fn
),
10075 return error_mark_node
;
10078 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
10081 /* Return the most specialized of the list of templates in FNS that can
10082 produce an instantiation matching DECL, given the explicit template
10083 arguments EXPLICIT_ARGS. */
10086 most_specialized (tree fns
, tree decl
, tree explicit_args
)
10088 tree candidates
= NULL_TREE
;
10091 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
10093 tree candidate
= TREE_VALUE (fn
);
10095 args
= get_bindings (candidate
, decl
, explicit_args
);
10097 candidates
= tree_cons (NULL_TREE
, candidate
, candidates
);
10100 return most_specialized_instantiation (candidates
);
10103 /* If DECL is a specialization of some template, return the most
10104 general such template. Otherwise, returns NULL_TREE.
10106 For example, given:
10108 template <class T> struct S { template <class U> void f(U); };
10110 if TMPL is `template <class U> void S<int>::f(U)' this will return
10111 the full template. This function will not trace past partial
10112 specializations, however. For example, given in addition:
10114 template <class T> struct S<T*> { template <class U> void f(U); };
10116 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10117 `template <class T> template <class U> S<T*>::f(U)'. */
10120 most_general_template (tree decl
)
10122 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10123 an immediate specialization. */
10124 if (TREE_CODE (decl
) == FUNCTION_DECL
)
10126 if (DECL_TEMPLATE_INFO (decl
)) {
10127 decl
= DECL_TI_TEMPLATE (decl
);
10129 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10130 template friend. */
10131 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
10137 /* Look for more and more general templates. */
10138 while (DECL_TEMPLATE_INFO (decl
))
10140 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10141 in some cases. (See cp-tree.h for details.) */
10142 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10145 if (CLASS_TYPE_P (TREE_TYPE (decl
))
10146 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
10149 /* Stop if we run into an explicitly specialized class template. */
10150 if (!DECL_NAMESPACE_SCOPE_P (decl
)
10151 && DECL_CONTEXT (decl
)
10152 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
10155 decl
= DECL_TI_TEMPLATE (decl
);
10161 /* Return the most specialized of the class template specializations
10162 of TMPL which can produce an instantiation matching ARGS, or
10163 error_mark_node if the choice is ambiguous. */
10166 most_specialized_class (tree tmpl
, tree args
)
10168 tree list
= NULL_TREE
;
10173 tmpl
= most_general_template (tmpl
);
10174 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
10177 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
10180 list
= tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
10181 TREE_TYPE (list
) = TREE_TYPE (t
);
10190 t
= TREE_CHAIN (t
);
10191 for (; t
; t
= TREE_CHAIN (t
))
10193 fate
= more_specialized_class (champ
, t
);
10200 t
= TREE_CHAIN (t
);
10202 return error_mark_node
;
10208 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
10210 fate
= more_specialized_class (champ
, t
);
10212 return error_mark_node
;
10218 /* Explicitly instantiate DECL. */
10221 do_decl_instantiation (tree decl
, tree storage
)
10223 tree result
= NULL_TREE
;
10227 /* An error occurred, for which grokdeclarator has already issued
10228 an appropriate message. */
10230 else if (! DECL_LANG_SPECIFIC (decl
))
10232 error ("explicit instantiation of non-template `%#D'", decl
);
10235 else if (TREE_CODE (decl
) == VAR_DECL
)
10237 /* There is an asymmetry here in the way VAR_DECLs and
10238 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10239 the latter, the DECL we get back will be marked as a
10240 template instantiation, and the appropriate
10241 DECL_TEMPLATE_INFO will be set up. This does not happen for
10242 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10243 should handle VAR_DECLs as it currently handles
10245 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
10246 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
10248 error ("no matching template for `%D' found", decl
);
10252 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
10254 error ("explicit instantiation of `%#D'", decl
);
10260 /* Check for various error cases. Note that if the explicit
10261 instantiation is valid the RESULT will currently be marked as an
10262 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10263 until we get here. */
10265 if (DECL_TEMPLATE_SPECIALIZATION (result
))
10267 /* DR 259 [temp.spec].
10269 Both an explicit instantiation and a declaration of an explicit
10270 specialization shall not appear in a program unless the explicit
10271 instantiation follows a declaration of the explicit specialization.
10273 For a given set of template parameters, if an explicit
10274 instantiation of a template appears after a declaration of an
10275 explicit specialization for that template, the explicit
10276 instantiation has no effect. */
10279 else if (DECL_EXPLICIT_INSTANTIATION (result
))
10283 No program shall explicitly instantiate any template more
10286 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10287 instantiation was `extern' and the second is not, and EXTERN_P for
10288 the opposite case. If -frepo, chances are we already got marked
10289 as an explicit instantiation because of the repo file. */
10290 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
10291 pedwarn ("duplicate explicit instantiation of `%#D'", result
);
10293 /* If we've already instantiated the template, just return now. */
10294 if (DECL_INTERFACE_KNOWN (result
))
10297 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
10299 error ("no matching template for `%D' found", result
);
10302 else if (!DECL_TEMPLATE_INFO (result
))
10304 pedwarn ("explicit instantiation of non-template `%#D'", result
);
10308 if (flag_external_templates
)
10311 if (storage
== NULL_TREE
)
10313 else if (storage
== ridpointers
[(int) RID_EXTERN
])
10315 if (pedantic
&& !in_system_header
)
10316 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
10320 error ("storage class `%D' applied to template instantiation",
10323 SET_DECL_EXPLICIT_INSTANTIATION (result
);
10324 mark_decl_instantiated (result
, extern_p
);
10325 repo_template_instantiated (result
, extern_p
);
10327 instantiate_decl (result
, /*defer_ok=*/1);
10331 mark_class_instantiated (tree t
, int extern_p
)
10333 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
10334 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
10335 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
10336 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
10339 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
10340 rest_of_type_compilation (t
, 1);
10344 /* Called from do_type_instantiation through binding_table_foreach to
10345 do recursive instantiation for the type bound in ENTRY. */
10347 bt_instantiate_type_proc (binding_entry entry
, void *data
)
10349 tree storage
= *(tree
*) data
;
10351 if (IS_AGGR_TYPE (entry
->type
)
10352 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
10353 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
10356 /* Perform an explicit instantiation of template class T. STORAGE, if
10357 non-null, is the RID for extern, inline or static. COMPLAIN is
10358 nonzero if this is called from the parser, zero if called recursively,
10359 since the standard is unclear (as detailed below). */
10362 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
10368 if (TREE_CODE (t
) == TYPE_DECL
)
10371 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
10373 error ("explicit instantiation of non-template type `%T'", t
);
10379 /* With -fexternal-templates, explicit instantiations are treated the same
10380 as implicit ones. */
10381 if (flag_external_templates
)
10384 if (!COMPLETE_TYPE_P (t
))
10386 if (complain
& tf_error
)
10387 error ("explicit instantiation of `%#T' before definition of template",
10392 if (storage
!= NULL_TREE
)
10394 if (pedantic
&& !in_system_header
)
10395 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
10396 IDENTIFIER_POINTER (storage
));
10398 if (storage
== ridpointers
[(int) RID_INLINE
])
10400 else if (storage
== ridpointers
[(int) RID_EXTERN
])
10402 else if (storage
== ridpointers
[(int) RID_STATIC
])
10406 error ("storage class `%D' applied to template instantiation",
10412 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
10414 /* DR 259 [temp.spec].
10416 Both an explicit instantiation and a declaration of an explicit
10417 specialization shall not appear in a program unless the explicit
10418 instantiation follows a declaration of the explicit specialization.
10420 For a given set of template parameters, if an explicit
10421 instantiation of a template appears after a declaration of an
10422 explicit specialization for that template, the explicit
10423 instantiation has no effect. */
10426 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
10430 No program shall explicitly instantiate any template more
10433 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10434 was `extern'. If EXTERN_P then the second is. If -frepo, chances
10435 are we already got marked as an explicit instantiation because of the
10436 repo file. All these cases are OK. */
10437 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
10438 && (complain
& tf_error
))
10439 pedwarn ("duplicate explicit instantiation of `%#T'", t
);
10441 /* If we've already instantiated the template, just return now. */
10442 if (!CLASSTYPE_INTERFACE_ONLY (t
))
10446 mark_class_instantiated (t
, extern_p
);
10447 repo_template_instantiated (t
, extern_p
);
10455 /* In contrast to implicit instantiation, where only the
10456 declarations, and not the definitions, of members are
10457 instantiated, we have here:
10461 The explicit instantiation of a class template specialization
10462 implies the instantiation of all of its members not
10463 previously explicitly specialized in the translation unit
10464 containing the explicit instantiation.
10466 Of course, we can't instantiate member template classes, since
10467 we don't have any arguments for them. Note that the standard
10468 is unclear on whether the instantiation of the members are
10469 *explicit* instantiations or not. We choose to be generous,
10470 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10471 the explicit instantiation of a class where some of the members
10472 have no definition in the current translation unit. */
10475 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
10476 if (TREE_CODE (tmp
) == FUNCTION_DECL
10477 && DECL_TEMPLATE_INSTANTIATION (tmp
))
10479 mark_decl_instantiated (tmp
, extern_p
);
10480 repo_template_instantiated (tmp
, extern_p
);
10482 instantiate_decl (tmp
, /*defer_ok=*/1);
10485 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
10486 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
10488 mark_decl_instantiated (tmp
, extern_p
);
10489 repo_template_instantiated (tmp
, extern_p
);
10491 instantiate_decl (tmp
, /*defer_ok=*/1);
10494 if (CLASSTYPE_NESTED_UTDS (t
))
10495 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
10496 bt_instantiate_type_proc
, &storage
);
10500 /* Given a function DECL, which is a specialization of TMPL, modify
10501 DECL to be a re-instantiation of TMPL with the same template
10502 arguments. TMPL should be the template into which tsubst'ing
10503 should occur for DECL, not the most general template.
10505 One reason for doing this is a scenario like this:
10508 void f(const T&, int i);
10510 void g() { f(3, 7); }
10513 void f(const T& t, const int i) { }
10515 Note that when the template is first instantiated, with
10516 instantiate_template, the resulting DECL will have no name for the
10517 first parameter, and the wrong type for the second. So, when we go
10518 to instantiate the DECL, we regenerate it. */
10521 regenerate_decl_from_template (tree decl
, tree tmpl
)
10523 /* The most general version of TMPL. */
10525 /* The arguments used to instantiate DECL, from the most general
10532 args
= DECL_TI_ARGS (decl
);
10533 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
10535 /* Unregister the specialization so that when we tsubst we will not
10536 just return DECL. We don't have to unregister DECL from TMPL
10537 because if would only be registered there if it were a partial
10538 instantiation of a specialization, which it isn't: it's a full
10540 gen_tmpl
= most_general_template (tmpl
);
10541 push_access_scope_real (gen_tmpl
, args
, DECL_CONTEXT (decl
));
10542 unregistered
= unregister_specialization (decl
, gen_tmpl
);
10544 /* If the DECL was not unregistered then something peculiar is
10545 happening: we created a specialization but did not call
10546 register_specialization for it. */
10547 my_friendly_assert (unregistered
, 0);
10549 /* Do the substitution to get the new declaration. */
10550 new_decl
= tsubst (code_pattern
, args
, tf_error
, NULL_TREE
);
10552 if (TREE_CODE (decl
) == VAR_DECL
)
10554 /* Set up DECL_INITIAL, since tsubst doesn't. */
10555 if (!DECL_INITIALIZED_IN_CLASS_P (decl
))
10556 DECL_INITIAL (new_decl
) =
10557 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
10558 tf_error
, DECL_TI_TEMPLATE (decl
));
10560 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
10562 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10564 DECL_INITIAL (new_decl
) = error_mark_node
;
10565 /* And don't complain about a duplicate definition. */
10566 DECL_INITIAL (decl
) = NULL_TREE
;
10569 pop_access_scope (decl
);
10571 /* The immediate parent of the new template is still whatever it was
10572 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10573 general template. We also reset the DECL_ASSEMBLER_NAME since
10574 tsubst always calculates the name as if the function in question
10575 were really a template instance, and sometimes, with friend
10576 functions, this is not so. See tsubst_friend_function for
10578 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
10579 COPY_DECL_ASSEMBLER_NAME (decl
, new_decl
);
10580 COPY_DECL_RTL (decl
, new_decl
);
10581 DECL_USE_TEMPLATE (new_decl
) = DECL_USE_TEMPLATE (decl
);
10583 /* Call duplicate decls to merge the old and new declarations. */
10584 duplicate_decls (new_decl
, decl
);
10586 /* Now, re-register the specialization. */
10587 register_specialization (decl
, gen_tmpl
, args
);
10590 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10591 substituted to get DECL. */
10594 template_for_substitution (tree decl
)
10596 tree tmpl
= DECL_TI_TEMPLATE (decl
);
10598 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10599 for the instantiation. This is not always the most general
10600 template. Consider, for example:
10603 struct S { template <class U> void f();
10604 template <> void f<int>(); };
10606 and an instantiation of S<double>::f<int>. We want TD to be the
10607 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10608 while (/* An instantiation cannot have a definition, so we need a
10609 more general template. */
10610 DECL_TEMPLATE_INSTANTIATION (tmpl
)
10611 /* We must also deal with friend templates. Given:
10613 template <class T> struct S {
10614 template <class U> friend void f() {};
10617 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10618 so far as the language is concerned, but that's still
10619 where we get the pattern for the instantiation from. On
10620 other hand, if the definition comes outside the class, say:
10622 template <class T> struct S {
10623 template <class U> friend void f();
10625 template <class U> friend void f() {}
10627 we don't need to look any further. That's what the check for
10628 DECL_INITIAL is for. */
10629 || (TREE_CODE (decl
) == FUNCTION_DECL
10630 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
10631 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
10633 /* The present template, TD, should not be a definition. If it
10634 were a definition, we should be using it! Note that we
10635 cannot restructure the loop to just keep going until we find
10636 a template with a definition, since that might go too far if
10637 a specialization was declared, but not defined. */
10638 my_friendly_assert (!(TREE_CODE (decl
) == VAR_DECL
10639 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
))),
10642 /* Fetch the more general template. */
10643 tmpl
= DECL_TI_TEMPLATE (tmpl
);
10649 /* Produce the definition of D, a _DECL generated from a template. If
10650 DEFER_OK is nonzero, then we don't have to actually do the
10651 instantiation now; we just have to do it sometime. */
10654 instantiate_decl (tree d
, int defer_ok
)
10656 tree tmpl
= DECL_TI_TEMPLATE (d
);
10663 int pattern_defined
;
10665 location_t saved_loc
= input_location
;
10667 /* This function should only be used to instantiate templates for
10668 functions and static member variables. */
10669 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
10670 || TREE_CODE (d
) == VAR_DECL
, 0);
10672 /* Variables are never deferred; if instantiation is required, they
10673 are instantiated right away. That allows for better code in the
10674 case that an expression refers to the value of the variable --
10675 if the variable has a constant value the referring expression can
10676 take advantage of that fact. */
10677 if (TREE_CODE (d
) == VAR_DECL
)
10680 /* Don't instantiate cloned functions. Instead, instantiate the
10681 functions they cloned. */
10682 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
10683 d
= DECL_CLONED_FUNCTION (d
);
10685 if (DECL_TEMPLATE_INSTANTIATED (d
))
10686 /* D has already been instantiated. It might seem reasonable to
10687 check whether or not D is an explicit instantiation, and, if so,
10688 stop here. But when an explicit instantiation is deferred
10689 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10690 is set, even though we still need to do the instantiation. */
10693 /* If we already have a specialization of this declaration, then
10694 there's no reason to instantiate it. Note that
10695 retrieve_specialization gives us both instantiations and
10696 specializations, so we must explicitly check
10697 DECL_TEMPLATE_SPECIALIZATION. */
10698 gen_tmpl
= most_general_template (tmpl
);
10699 gen_args
= DECL_TI_ARGS (d
);
10700 spec
= retrieve_specialization (gen_tmpl
, gen_args
);
10701 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
10704 /* This needs to happen before any tsubsting. */
10705 if (! push_tinst_level (d
))
10708 timevar_push (TV_PARSE
);
10710 /* We may be in the middle of deferred access check. Disable
10712 push_deferring_access_checks (dk_no_deferred
);
10714 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
10715 for the instantiation. */
10716 td
= template_for_substitution (d
);
10717 code_pattern
= DECL_TEMPLATE_RESULT (td
);
10719 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
10720 || DECL_TEMPLATE_SPECIALIZATION (td
))
10721 /* In the case of a friend template whose definition is provided
10722 outside the class, we may have too many arguments. Drop the
10723 ones we don't need. The same is true for specializations. */
10724 args
= get_innermost_template_args
10725 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
10729 if (TREE_CODE (d
) == FUNCTION_DECL
)
10730 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
10732 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
10734 input_location
= DECL_SOURCE_LOCATION (d
);
10736 if (pattern_defined
)
10738 /* Let the repository code that this template definition is
10741 The repository doesn't need to know about cloned functions
10742 because they never actually show up in the object file. It
10743 does need to know about the clones; those are the symbols
10744 that the linker will be emitting error messages about. */
10745 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d
)
10746 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d
))
10750 for (t
= TREE_CHAIN (d
);
10751 t
&& DECL_CLONED_FUNCTION_P (t
);
10752 t
= TREE_CHAIN (t
))
10753 repo_template_used (t
);
10756 repo_template_used (d
);
10758 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
10760 if (flag_alt_external_templates
)
10762 if (interface_unknown
)
10763 warn_if_unknown_interface (d
);
10765 else if (DECL_INTERFACE_KNOWN (code_pattern
))
10767 DECL_INTERFACE_KNOWN (d
) = 1;
10768 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
10771 warn_if_unknown_interface (code_pattern
);
10775 import_export_decl (d
);
10780 /* Recheck the substitutions to obtain any warning messages
10781 about ignoring cv qualifiers. */
10782 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
10783 tree type
= TREE_TYPE (gen
);
10785 /* Make sure that we can see identifiers, and compute access
10786 correctly. D is already the target FUNCTION_DECL with the
10788 push_access_scope (d
);
10790 if (TREE_CODE (gen
) == FUNCTION_DECL
)
10792 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_error
| tf_warning
, d
);
10793 tsubst (TYPE_RAISES_EXCEPTIONS (type
), gen_args
,
10794 tf_error
| tf_warning
, d
);
10795 /* Don't simply tsubst the function type, as that will give
10796 duplicate warnings about poor parameter qualifications.
10797 The function arguments are the same as the decl_arguments
10798 without the top level cv qualifiers. */
10799 type
= TREE_TYPE (type
);
10801 tsubst (type
, gen_args
, tf_error
| tf_warning
, d
);
10803 pop_access_scope (d
);
10806 if (TREE_CODE (d
) == VAR_DECL
&& DECL_INITIALIZED_IN_CLASS_P (d
)
10807 && DECL_INITIAL (d
) == NULL_TREE
)
10808 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10810 /* Reject all external templates except inline functions. */
10811 else if (DECL_INTERFACE_KNOWN (d
)
10812 && ! DECL_NOT_REALLY_EXTERN (d
)
10813 && ! (TREE_CODE (d
) == FUNCTION_DECL
10814 && DECL_INLINE (d
)))
10816 /* Defer all other templates, unless we have been explicitly
10817 forbidden from doing so. We restore the source position here
10818 because it's used by add_pending_template. */
10819 else if (! pattern_defined
|| defer_ok
)
10821 input_location
= saved_loc
;
10823 if (at_eof
&& !pattern_defined
10824 && DECL_EXPLICIT_INSTANTIATION (d
))
10827 The definition of a non-exported function template, a
10828 non-exported member function template, or a non-exported
10829 member function or static data member of a class template
10830 shall be present in every translation unit in which it is
10831 explicitly instantiated. */
10833 ("explicit instantiation of `%D' but no definition available", d
);
10835 add_pending_template (d
);
10839 need_push
= !global_bindings_p ();
10841 push_to_top_level ();
10843 /* Regenerate the declaration in case the template has been modified
10844 by a subsequent redeclaration. */
10845 regenerate_decl_from_template (d
, td
);
10847 /* We already set the file and line above. Reset them now in case
10848 they changed as a result of calling
10849 regenerate_decl_from_template. */
10850 input_location
= DECL_SOURCE_LOCATION (d
);
10852 if (TREE_CODE (d
) == VAR_DECL
)
10854 /* Clear out DECL_RTL; whatever was there before may not be right
10855 since we've reset the type of the declaration. */
10856 SET_DECL_RTL (d
, NULL_RTX
);
10858 DECL_IN_AGGR_P (d
) = 0;
10859 import_export_decl (d
);
10860 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
10862 if (DECL_EXTERNAL (d
))
10864 /* The fact that this code is executing indicates that:
10866 (1) D is a template static data member, for which a
10867 definition is available.
10869 (2) An implicit or explicit instantiation has occured.
10871 (3) We are not going to emit a definition of the static
10872 data member at this time.
10874 This situation is peculiar, but it occurs on platforms
10875 without weak symbols when performing an implicit
10876 instantiation. There, we cannot implicitly instantiate a
10877 defined static data member in more than one translation
10878 unit, so import_export_decl marks the declaration as
10879 external; we must rely on explicit instantiation. */
10883 /* Mark D as instantiated so that recursive calls to
10884 instantiate_decl do not try to instantiate it again. */
10885 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
10887 (!DECL_INITIALIZED_IN_CLASS_P (d
)
10888 ? DECL_INITIAL (d
) : NULL_TREE
),
10892 else if (TREE_CODE (d
) == FUNCTION_DECL
)
10894 htab_t saved_local_specializations
;
10899 /* Mark D as instantiated so that recursive calls to
10900 instantiate_decl do not try to instantiate it again. */
10901 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
10903 /* Save away the current list, in case we are instantiating one
10904 template from within the body of another. */
10905 saved_local_specializations
= local_specializations
;
10907 /* Set up the list of local specializations. */
10908 local_specializations
= htab_create (37,
10909 hash_local_specialization
,
10910 eq_local_specializations
,
10913 /* Set up context. */
10914 import_export_decl (d
);
10915 start_function (NULL_TREE
, d
, NULL_TREE
, SF_PRE_PARSED
);
10917 /* Create substitution entries for the parameters. */
10918 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
10919 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
10920 spec_parm
= DECL_ARGUMENTS (d
);
10921 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
10923 register_local_specialization (spec_parm
, tmpl_parm
);
10924 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
10925 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
10929 register_local_specialization (spec_parm
, tmpl_parm
);
10930 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
10931 spec_parm
= TREE_CHAIN (spec_parm
);
10933 my_friendly_assert (!spec_parm
, 20020813);
10935 /* Substitute into the body of the function. */
10936 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
10937 tf_error
| tf_warning
, tmpl
);
10939 /* We don't need the local specializations any more. */
10940 htab_delete (local_specializations
);
10941 local_specializations
= saved_local_specializations
;
10943 /* Finish the function. */
10944 d
= finish_function (0);
10945 expand_or_defer_fn (d
);
10948 /* We're not deferring instantiation any more. */
10949 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
10952 pop_from_top_level ();
10955 input_location
= saved_loc
;
10956 pop_deferring_access_checks ();
10957 pop_tinst_level ();
10959 timevar_pop (TV_PARSE
);
10964 /* Run through the list of templates that we wish we could
10965 instantiate, and instantiate any we can. */
10968 instantiate_pending_templates (void)
10971 tree last
= NULL_TREE
;
10972 int instantiated_something
= 0;
10979 t
= &pending_templates
;
10982 tree instantiation
= TREE_VALUE (*t
);
10984 reopen_tinst_level (TREE_PURPOSE (*t
));
10986 if (TYPE_P (instantiation
))
10990 if (!COMPLETE_TYPE_P (instantiation
))
10992 instantiate_class_template (instantiation
);
10993 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
10994 for (fn
= TYPE_METHODS (instantiation
);
10996 fn
= TREE_CHAIN (fn
))
10997 if (! DECL_ARTIFICIAL (fn
))
10998 instantiate_decl (fn
, /*defer_ok=*/0);
10999 if (COMPLETE_TYPE_P (instantiation
))
11001 instantiated_something
= 1;
11006 if (COMPLETE_TYPE_P (instantiation
))
11007 /* If INSTANTIATION has been instantiated, then we don't
11008 need to consider it again in the future. */
11009 *t
= TREE_CHAIN (*t
);
11013 t
= &TREE_CHAIN (*t
);
11018 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
11019 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
11021 instantiation
= instantiate_decl (instantiation
,
11023 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
11025 instantiated_something
= 1;
11030 if (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
11031 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
11032 /* If INSTANTIATION has been instantiated, then we don't
11033 need to consider it again in the future. */
11034 *t
= TREE_CHAIN (*t
);
11038 t
= &TREE_CHAIN (*t
);
11042 current_tinst_level
= NULL_TREE
;
11044 last_pending_template
= last
;
11046 while (reconsider
);
11048 return instantiated_something
;
11051 /* Substitute ARGVEC into T, which is a list of initializers for
11052 either base class or a non-static data member. The TREE_PURPOSEs
11053 are DECLs, and the TREE_VALUEs are the initializer values. Used by
11054 instantiate_decl. */
11057 tsubst_initializer_list (tree t
, tree argvec
)
11059 tree inits
= NULL_TREE
;
11061 for (; t
; t
= TREE_CHAIN (t
))
11067 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
, tf_error
| tf_warning
,
11069 decl
= expand_member_init (decl
);
11070 if (decl
&& !DECL_P (decl
))
11071 in_base_initializer
= 1;
11073 init
= tsubst_expr (TREE_VALUE (t
), argvec
, tf_error
| tf_warning
,
11077 else if (TREE_CODE (init
) == TREE_LIST
)
11078 for (val
= init
; val
; val
= TREE_CHAIN (val
))
11079 TREE_VALUE (val
) = convert_from_reference (TREE_VALUE (val
));
11080 else if (init
!= void_type_node
)
11081 init
= convert_from_reference (init
);
11083 in_base_initializer
= 0;
11087 init
= build_tree_list (decl
, init
);
11088 TREE_CHAIN (init
) = inits
;
11095 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11098 set_current_access_from_decl (tree decl
)
11100 if (TREE_PRIVATE (decl
))
11101 current_access_specifier
= access_private_node
;
11102 else if (TREE_PROTECTED (decl
))
11103 current_access_specifier
= access_protected_node
;
11105 current_access_specifier
= access_public_node
;
11108 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
11109 is the instantiation (which should have been created with
11110 start_enum) and ARGS are the template arguments to use. */
11113 tsubst_enum (tree tag
, tree newtag
, tree args
)
11117 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
11121 /* Note that in a template enum, the TREE_VALUE is the
11122 CONST_DECL, not the corresponding INTEGER_CST. */
11123 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
11124 args
, tf_error
| tf_warning
,
11127 /* Give this enumeration constant the correct access. */
11128 set_current_access_from_decl (TREE_VALUE (e
));
11130 /* Actually build the enumerator itself. */
11131 build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
11134 finish_enum (newtag
);
11135 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
11136 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
11139 /* DECL is a FUNCTION_DECL that is a template specialization. Return
11140 its type -- but without substituting the innermost set of template
11141 arguments. So, innermost set of template parameters will appear in
11145 get_mostly_instantiated_function_type (tree decl
)
11153 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11154 targs
= DECL_TI_ARGS (decl
);
11155 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
11156 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
11158 /* There should be as many levels of arguments as there are levels
11160 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
11162 fn_type
= TREE_TYPE (tmpl
);
11164 if (parm_depth
== 1)
11165 /* No substitution is necessary. */
11172 /* Replace the innermost level of the TARGS with NULL_TREEs to
11173 let tsubst know not to substitute for those parameters. */
11174 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
11175 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
11176 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
11177 TMPL_ARGS_LEVEL (targs
, i
));
11178 SET_TMPL_ARGS_LEVEL (partial_args
,
11179 TMPL_ARGS_DEPTH (targs
),
11180 make_tree_vec (DECL_NTPARMS (tmpl
)));
11182 /* Make sure that we can see identifiers, and compute access
11183 correctly. We can just use the context of DECL for the
11184 partial substitution here. It depends only on outer template
11185 parameters, regardless of whether the innermost level is
11186 specialized or not. */
11187 push_access_scope (decl
);
11189 /* Now, do the (partial) substitution to figure out the
11190 appropriate function type. */
11191 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
11193 /* Substitute into the template parameters to obtain the real
11194 innermost set of parameters. This step is important if the
11195 innermost set of template parameters contains value
11196 parameters whose types depend on outer template parameters. */
11197 TREE_VEC_LENGTH (partial_args
)--;
11198 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
11200 pop_access_scope (decl
);
11206 /* Return truthvalue if we're processing a template different from
11207 the last one involved in diagnostics. */
11209 problematic_instantiation_changed (void)
11211 return last_template_error_tick
!= tinst_level_tick
;
11214 /* Remember current template involved in diagnostics. */
11216 record_last_problematic_instantiation (void)
11218 last_template_error_tick
= tinst_level_tick
;
11222 current_instantiation (void)
11224 return current_tinst_level
;
11227 /* [temp.param] Check that template non-type parm TYPE is of an allowable
11228 type. Return zero for ok, nonzero for disallowed. Issue error and
11229 warning messages under control of COMPLAIN. */
11232 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
11234 if (INTEGRAL_TYPE_P (type
))
11236 else if (POINTER_TYPE_P (type
))
11238 else if (TYPE_PTRMEM_P (type
))
11240 else if (TYPE_PTRMEMFUNC_P (type
))
11242 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
11244 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
11247 if (complain
& tf_error
)
11248 error ("`%#T' is not a valid type for a template constant parameter",
11253 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11254 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
11257 dependent_type_p_r (tree type
)
11263 A type is dependent if it is:
11265 -- a template parameter. */
11266 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
11268 /* -- a qualified-id with a nested-name-specifier which contains a
11269 class-name that names a dependent type or whose unqualified-id
11270 names a dependent type. */
11271 if (TREE_CODE (type
) == TYPENAME_TYPE
)
11273 /* -- a cv-qualified type where the cv-unqualified type is
11275 type
= TYPE_MAIN_VARIANT (type
);
11276 /* -- a compound type constructed from any dependent type. */
11277 if (TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
11278 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
11279 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11281 else if (TREE_CODE (type
) == POINTER_TYPE
11282 || TREE_CODE (type
) == REFERENCE_TYPE
)
11283 return dependent_type_p (TREE_TYPE (type
));
11284 else if (TREE_CODE (type
) == FUNCTION_TYPE
11285 || TREE_CODE (type
) == METHOD_TYPE
)
11289 if (dependent_type_p (TREE_TYPE (type
)))
11291 for (arg_type
= TYPE_ARG_TYPES (type
);
11293 arg_type
= TREE_CHAIN (arg_type
))
11294 if (dependent_type_p (TREE_VALUE (arg_type
)))
11298 /* -- an array type constructed from any dependent type or whose
11299 size is specified by a constant expression that is
11300 value-dependent. */
11301 if (TREE_CODE (type
) == ARRAY_TYPE
)
11303 if (TYPE_DOMAIN (type
)
11304 && ((value_dependent_expression_p
11305 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
11306 || (type_dependent_expression_p
11307 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))))
11309 return dependent_type_p (TREE_TYPE (type
));
11311 /* -- a template-id in which either the template name is a template
11312 parameter or any of the template arguments is a dependent type or
11313 an expression that is type-dependent or value-dependent.
11315 This language seems somewhat confused; for example, it does not
11316 discuss template template arguments. Therefore, we use the
11317 definition for dependent template arguments in [temp.dep.temp]. */
11318 if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
11319 && (dependent_template_id_p
11320 (CLASSTYPE_TI_TEMPLATE (type
),
11321 CLASSTYPE_TI_ARGS (type
))))
11323 else if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
11325 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11326 expression is not type-dependent, then it should already been
11328 if (TREE_CODE (type
) == TYPEOF_TYPE
)
11330 /* The standard does not specifically mention types that are local
11331 to template functions or local classes, but they should be
11332 considered dependent too. For example:
11334 template <int I> void f() {
11339 The size of `E' cannot be known until the value of `I' has been
11340 determined. Therefore, `E' must be considered dependent. */
11341 scope
= TYPE_CONTEXT (type
);
11342 if (scope
&& TYPE_P (scope
))
11343 return dependent_type_p (scope
);
11344 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
11345 return type_dependent_expression_p (scope
);
11347 /* Other types are non-dependent. */
11351 /* Returns TRUE if TYPE is dependent, in the sense of
11352 [temp.dep.type]. */
11355 dependent_type_p (tree type
)
11357 /* If there are no template parameters in scope, then there can't be
11358 any dependent types. */
11359 if (!processing_template_decl
)
11362 /* If the type is NULL, we have not computed a type for the entity
11363 in question; in that case, the type is dependent. */
11367 /* Erroneous types can be considered non-dependent. */
11368 if (type
== error_mark_node
)
11371 /* If we have not already computed the appropriate value for TYPE,
11373 if (!TYPE_DEPENDENT_P_VALID (type
))
11375 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
11376 TYPE_DEPENDENT_P_VALID (type
) = 1;
11379 return TYPE_DEPENDENT_P (type
);
11382 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
11383 [temp.dep.constexpr] */
11386 value_dependent_expression_p (tree expression
)
11388 if (!processing_template_decl
)
11391 /* A name declared with a dependent type. */
11392 if (TREE_CODE (expression
) == LOOKUP_EXPR
11393 || (DECL_P (expression
)
11394 && type_dependent_expression_p (expression
)))
11396 /* A non-type template parameter. */
11397 if ((TREE_CODE (expression
) == CONST_DECL
11398 && DECL_TEMPLATE_PARM_P (expression
))
11399 || TREE_CODE (expression
) == TEMPLATE_PARM_INDEX
)
11401 /* A constant with integral or enumeration type and is initialized
11402 with an expression that is value-dependent. */
11403 if (TREE_CODE (expression
) == VAR_DECL
11404 && DECL_INITIAL (expression
)
11405 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression
))
11406 || TREE_CODE (TREE_TYPE (expression
)) == ENUMERAL_TYPE
)
11407 && value_dependent_expression_p (DECL_INITIAL (expression
)))
11409 /* These expressions are value-dependent if the type to which the
11410 cast occurs is dependent or the expression being casted is
11411 value-dependent. */
11412 if ((TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
11413 || TREE_CODE (expression
) == STATIC_CAST_EXPR
11414 || TREE_CODE (expression
) == CONST_CAST_EXPR
11415 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
11416 || TREE_CODE (expression
) == CAST_EXPR
)
11417 && (dependent_type_p (TREE_TYPE (expression
))
11418 || value_dependent_expression_p (TREE_OPERAND (expression
, 0))))
11420 /* A `sizeof' expression where the sizeof operand is a type is
11421 value-dependent if the type is dependent. If the type was not
11422 dependent, we would no longer have a SIZEOF_EXPR, so any
11423 SIZEOF_EXPR is dependent. */
11424 if (TREE_CODE (expression
) == SIZEOF_EXPR
)
11426 /* A constant expression is value-dependent if any subexpression is
11427 value-dependent. */
11428 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression
))))
11430 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
11433 return (value_dependent_expression_p
11434 (TREE_OPERAND (expression
, 0)));
11437 return ((value_dependent_expression_p
11438 (TREE_OPERAND (expression
, 0)))
11439 || (value_dependent_expression_p
11440 (TREE_OPERAND (expression
, 1))));
11444 for (i
= 0; i
< first_rtl_op (TREE_CODE (expression
)); ++i
)
11445 /* In some cases, some of the operands may be missing.
11446 (For example, in the case of PREDECREMENT_EXPR, the
11447 amount to increment by may be missing.) That doesn't
11448 make the expression dependent. */
11449 if (TREE_OPERAND (expression
, i
)
11450 && (value_dependent_expression_p
11451 (TREE_OPERAND (expression
, i
))))
11458 /* The expression is not value-dependent. */
11462 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11463 [temp.dep.expr]. */
11466 type_dependent_expression_p (tree expression
)
11468 if (!processing_template_decl
)
11471 /* Some expression forms are never type-dependent. */
11472 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
11473 || TREE_CODE (expression
) == SIZEOF_EXPR
11474 || TREE_CODE (expression
) == ALIGNOF_EXPR
11475 || TREE_CODE (expression
) == TYPEID_EXPR
11476 || TREE_CODE (expression
) == DELETE_EXPR
11477 || TREE_CODE (expression
) == VEC_DELETE_EXPR
11478 || TREE_CODE (expression
) == THROW_EXPR
)
11481 /* The types of these expressions depends only on the type to which
11482 the cast occurs. */
11483 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
11484 || TREE_CODE (expression
) == STATIC_CAST_EXPR
11485 || TREE_CODE (expression
) == CONST_CAST_EXPR
11486 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
11487 || TREE_CODE (expression
) == CAST_EXPR
)
11488 return dependent_type_p (TREE_TYPE (expression
));
11489 /* The types of these expressions depends only on the type created
11490 by the expression. */
11491 else if (TREE_CODE (expression
) == NEW_EXPR
11492 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
11494 /* For NEW_EXPR tree nodes created inside a template, either
11495 the object type itself or a TREE_LIST may appear as the
11497 tree type
= TREE_OPERAND (expression
, 1);
11498 if (TREE_CODE (type
) == TREE_LIST
)
11499 /* This is an array type. We need to check array dimensions
11501 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
11502 || value_dependent_expression_p
11503 (TREE_OPERAND (TREE_VALUE (type
), 1));
11505 return dependent_type_p (type
);
11508 if (TREE_CODE (expression
) == FUNCTION_DECL
11509 && DECL_LANG_SPECIFIC (expression
)
11510 && DECL_TEMPLATE_INFO (expression
)
11511 && (dependent_template_id_p
11512 (DECL_TI_TEMPLATE (expression
),
11513 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
11516 return (dependent_type_p (TREE_TYPE (expression
)));
11519 /* Returns TRUE if the ARG (a template argument) is dependent. */
11522 dependent_template_arg_p (tree arg
)
11524 if (!processing_template_decl
)
11527 if (TREE_CODE (arg
) == TEMPLATE_DECL
11528 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11529 return dependent_template_p (arg
);
11530 else if (TYPE_P (arg
))
11531 return dependent_type_p (arg
);
11533 return (type_dependent_expression_p (arg
)
11534 || value_dependent_expression_p (arg
));
11537 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11540 dependent_template_id_p (tree tmpl
, tree args
)
11544 if (dependent_template_p (tmpl
))
11546 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
11547 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
11552 /* Returns TRUE if the template TMPL is dependent. */
11555 dependent_template_p (tree tmpl
)
11557 /* Template template parameters are dependent. */
11558 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
11559 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
11561 /* So are member templates of dependent classes. */
11562 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
11563 return dependent_type_p (DECL_CONTEXT (tmpl
));
11567 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11568 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11569 can be found. Note that this function peers inside uninstantiated
11570 templates and therefore should be used only in extremely limited
11574 resolve_typename_type (tree type
, bool only_current_p
)
11581 my_friendly_assert (TREE_CODE (type
) == TYPENAME_TYPE
,
11584 scope
= TYPE_CONTEXT (type
);
11585 name
= TYPE_IDENTIFIER (type
);
11587 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11588 it first before we can figure out what NAME refers to. */
11589 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
11590 scope
= resolve_typename_type (scope
, only_current_p
);
11591 /* If we don't know what SCOPE refers to, then we cannot resolve the
11593 if (scope
== error_mark_node
|| TREE_CODE (scope
) == TYPENAME_TYPE
)
11594 return error_mark_node
;
11595 /* If the SCOPE is a template type parameter, we have no way of
11596 resolving the name. */
11597 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
11599 /* If the SCOPE is not the current instantiation, there's no reason
11600 to look inside it. */
11601 if (only_current_p
&& !currently_open_class (scope
))
11602 return error_mark_node
;
11603 /* If SCOPE is a partial instantiation, it will not have a valid
11604 TYPE_FIELDS list, so use the original template. */
11605 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
11606 /* Enter the SCOPE so that name lookup will be resolved as if we
11607 were in the class definition. In particular, SCOPE will no
11608 longer be considered a dependent type. */
11609 push_scope (scope
);
11610 /* Look up the declaration. */
11611 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
11612 /* Obtain the set of qualifiers applied to the TYPE. */
11613 quals
= cp_type_quals (type
);
11614 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11615 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11617 type
= error_mark_node
;
11618 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
11619 && TREE_CODE (decl
) == TYPE_DECL
)
11620 type
= TREE_TYPE (decl
);
11621 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
11622 && DECL_CLASS_TEMPLATE_P (decl
))
11626 /* Obtain the template and the arguments. */
11627 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
11628 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
11629 /* Instantiate the template. */
11630 type
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
11631 /*entering_scope=*/0,
11635 type
= error_mark_node
;
11636 /* Qualify the resulting type. */
11637 if (type
!= error_mark_node
&& quals
)
11638 type
= cp_build_qualified_type (type
, quals
);
11639 /* Leave the SCOPE. */
11645 #include "gt-cp-pt.h"