Fix ChangeLog date.
[official-gcc.git] / gcc / cp / pt.c
blob906b8d5b1a9dd400d86a9855a335042a40d36949
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 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)
12 any later version.
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "vecprim.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work.
57 The TREE_PURPOSE of each entry is either a DECL (for a function or
58 static data member), or a TYPE (for a class) indicating what we are
59 hoping to instantiate. The TREE_VALUE is not used. */
60 static GTY(()) tree pending_templates;
61 static GTY(()) tree last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) tree current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static htab_t local_specializations;
83 /* Contains canonical template parameter types. The vector is indexed by
84 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
85 TREE_LIST, whose TREE_VALUEs contain the canonical template
86 parameters of various types and levels. */
87 static GTY(()) VEC(tree,gc) *canonical_template_parms;
89 #define UNIFY_ALLOW_NONE 0
90 #define UNIFY_ALLOW_MORE_CV_QUAL 1
91 #define UNIFY_ALLOW_LESS_CV_QUAL 2
92 #define UNIFY_ALLOW_DERIVED 4
93 #define UNIFY_ALLOW_INTEGER 8
94 #define UNIFY_ALLOW_OUTER_LEVEL 16
95 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
96 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
98 static void push_access_scope (tree);
99 static void pop_access_scope (tree);
100 static int resolve_overloaded_unification (tree, tree, tree, tree,
101 unification_kind_t, int);
102 static int try_one_overload (tree, tree, tree, tree, tree,
103 unification_kind_t, int, bool);
104 static int unify (tree, tree, tree, tree, int);
105 static void add_pending_template (tree);
106 static int push_tinst_level (tree);
107 static void pop_tinst_level (void);
108 static void reopen_tinst_level (tree);
109 static tree tsubst_initializer_list (tree, tree);
110 static tree get_class_bindings (tree, tree, tree);
111 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
112 bool, bool);
113 static void tsubst_enum (tree, tree, tree);
114 static tree add_to_template_args (tree, tree);
115 static tree add_outermost_template_args (tree, tree);
116 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
117 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
118 static int type_unification_real (tree, tree, tree, tree,
119 int, unification_kind_t, int);
120 static void note_template_header (int);
121 static tree convert_nontype_argument_function (tree, tree);
122 static tree convert_nontype_argument (tree, tree);
123 static tree convert_template_argument (tree, tree, tree,
124 tsubst_flags_t, int, tree);
125 static int for_each_template_parm (tree, tree_fn_t, void*,
126 struct pointer_set_t*);
127 static tree expand_template_argument_pack (tree);
128 static tree build_template_parm_index (int, int, int, tree, tree);
129 static int inline_needs_template_parms (tree);
130 static void push_inline_template_parms_recursive (tree, int);
131 static tree retrieve_local_specialization (tree);
132 static void register_local_specialization (tree, tree);
133 static tree reduce_template_parm_level (tree, tree, int);
134 static int mark_template_parm (tree, void *);
135 static int template_parm_this_level_p (tree, void *);
136 static tree tsubst_friend_function (tree, tree);
137 static tree tsubst_friend_class (tree, tree);
138 static int can_complete_type_without_circularity (tree);
139 static tree get_bindings (tree, tree, tree, bool);
140 static int template_decl_level (tree);
141 static int check_cv_quals_for_unify (int, tree, tree);
142 static void template_parm_level_and_index (tree, int*, int*);
143 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
144 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
145 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
146 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
147 static void regenerate_decl_from_template (tree, tree);
148 static tree most_specialized_class (tree, tree);
149 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
150 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
151 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
152 static bool check_specialization_scope (void);
153 static tree process_partial_specialization (tree);
154 static void set_current_access_from_decl (tree);
155 static tree get_template_base (tree, tree, tree, tree);
156 static tree try_class_unification (tree, tree, tree, tree);
157 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
158 tree, tree);
159 static int template_args_equal (tree, tree);
160 static void tsubst_default_arguments (tree);
161 static tree for_each_template_parm_r (tree *, int *, void *);
162 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
163 static void copy_default_args_to_explicit_spec (tree);
164 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
165 static int eq_local_specializations (const void *, const void *);
166 static bool dependent_template_arg_p (tree);
167 static bool any_template_arguments_need_structural_equality_p (tree);
168 static bool dependent_type_p_r (tree);
169 static tree tsubst (tree, tree, tsubst_flags_t, tree);
170 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
171 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
174 /* Make the current scope suitable for access checking when we are
175 processing T. T can be FUNCTION_DECL for instantiated function
176 template, or VAR_DECL for static member variable (need by
177 instantiate_decl). */
179 static void
180 push_access_scope (tree t)
182 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
183 || TREE_CODE (t) == VAR_DECL);
185 if (DECL_FRIEND_CONTEXT (t))
186 push_nested_class (DECL_FRIEND_CONTEXT (t));
187 else if (DECL_CLASS_SCOPE_P (t))
188 push_nested_class (DECL_CONTEXT (t));
189 else
190 push_to_top_level ();
192 if (TREE_CODE (t) == FUNCTION_DECL)
194 saved_access_scope = tree_cons
195 (NULL_TREE, current_function_decl, saved_access_scope);
196 current_function_decl = t;
200 /* Restore the scope set up by push_access_scope. T is the node we
201 are processing. */
203 static void
204 pop_access_scope (tree t)
206 if (TREE_CODE (t) == FUNCTION_DECL)
208 current_function_decl = TREE_VALUE (saved_access_scope);
209 saved_access_scope = TREE_CHAIN (saved_access_scope);
212 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
213 pop_nested_class ();
214 else
215 pop_from_top_level ();
218 /* Do any processing required when DECL (a member template
219 declaration) is finished. Returns the TEMPLATE_DECL corresponding
220 to DECL, unless it is a specialization, in which case the DECL
221 itself is returned. */
223 tree
224 finish_member_template_decl (tree decl)
226 if (decl == error_mark_node)
227 return error_mark_node;
229 gcc_assert (DECL_P (decl));
231 if (TREE_CODE (decl) == TYPE_DECL)
233 tree type;
235 type = TREE_TYPE (decl);
236 if (IS_AGGR_TYPE (type)
237 && CLASSTYPE_TEMPLATE_INFO (type)
238 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
240 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
241 check_member_template (tmpl);
242 return tmpl;
244 return NULL_TREE;
246 else if (TREE_CODE (decl) == FIELD_DECL)
247 error ("data member %qD cannot be a member template", decl);
248 else if (DECL_TEMPLATE_INFO (decl))
250 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
252 check_member_template (DECL_TI_TEMPLATE (decl));
253 return DECL_TI_TEMPLATE (decl);
255 else
256 return decl;
258 else
259 error ("invalid member template declaration %qD", decl);
261 return error_mark_node;
264 /* Returns the template nesting level of the indicated class TYPE.
266 For example, in:
267 template <class T>
268 struct A
270 template <class U>
271 struct B {};
274 A<T>::B<U> has depth two, while A<T> has depth one.
275 Both A<T>::B<int> and A<int>::B<U> have depth one, if
276 they are instantiations, not specializations.
278 This function is guaranteed to return 0 if passed NULL_TREE so
279 that, for example, `template_class_depth (current_class_type)' is
280 always safe. */
283 template_class_depth (tree type)
285 int depth;
287 for (depth = 0;
288 type && TREE_CODE (type) != NAMESPACE_DECL;
289 type = (TREE_CODE (type) == FUNCTION_DECL)
290 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
292 if (TREE_CODE (type) != FUNCTION_DECL)
294 if (CLASSTYPE_TEMPLATE_INFO (type)
295 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
296 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
297 ++depth;
299 else
301 if (DECL_TEMPLATE_INFO (type)
302 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
303 && uses_template_parms (DECL_TI_ARGS (type)))
304 ++depth;
308 return depth;
311 /* Returns 1 if processing DECL as part of do_pending_inlines
312 needs us to push template parms. */
314 static int
315 inline_needs_template_parms (tree decl)
317 if (! DECL_TEMPLATE_INFO (decl))
318 return 0;
320 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
321 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
324 /* Subroutine of maybe_begin_member_template_processing.
325 Push the template parms in PARMS, starting from LEVELS steps into the
326 chain, and ending at the beginning, since template parms are listed
327 innermost first. */
329 static void
330 push_inline_template_parms_recursive (tree parmlist, int levels)
332 tree parms = TREE_VALUE (parmlist);
333 int i;
335 if (levels > 1)
336 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
338 ++processing_template_decl;
339 current_template_parms
340 = tree_cons (size_int (processing_template_decl),
341 parms, current_template_parms);
342 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
344 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
345 NULL);
346 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
348 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
350 if (parm == error_mark_node)
351 continue;
353 gcc_assert (DECL_P (parm));
355 switch (TREE_CODE (parm))
357 case TYPE_DECL:
358 case TEMPLATE_DECL:
359 pushdecl (parm);
360 break;
362 case PARM_DECL:
364 /* Make a CONST_DECL as is done in process_template_parm.
365 It is ugly that we recreate this here; the original
366 version built in process_template_parm is no longer
367 available. */
368 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
369 TREE_TYPE (parm));
370 DECL_ARTIFICIAL (decl) = 1;
371 TREE_CONSTANT (decl) = 1;
372 TREE_INVARIANT (decl) = 1;
373 TREE_READONLY (decl) = 1;
374 DECL_INITIAL (decl) = DECL_INITIAL (parm);
375 SET_DECL_TEMPLATE_PARM_P (decl);
376 pushdecl (decl);
378 break;
380 default:
381 gcc_unreachable ();
386 /* Restore the template parameter context for a member template or
387 a friend template defined in a class definition. */
389 void
390 maybe_begin_member_template_processing (tree decl)
392 tree parms;
393 int levels = 0;
395 if (inline_needs_template_parms (decl))
397 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
398 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
400 if (DECL_TEMPLATE_SPECIALIZATION (decl))
402 --levels;
403 parms = TREE_CHAIN (parms);
406 push_inline_template_parms_recursive (parms, levels);
409 /* Remember how many levels of template parameters we pushed so that
410 we can pop them later. */
411 VEC_safe_push (int, heap, inline_parm_levels, levels);
414 /* Undo the effects of maybe_begin_member_template_processing. */
416 void
417 maybe_end_member_template_processing (void)
419 int i;
420 int last;
422 if (VEC_length (int, inline_parm_levels) == 0)
423 return;
425 last = VEC_pop (int, inline_parm_levels);
426 for (i = 0; i < last; ++i)
428 --processing_template_decl;
429 current_template_parms = TREE_CHAIN (current_template_parms);
430 poplevel (0, 0, 0);
434 /* Return a new template argument vector which contains all of ARGS,
435 but has as its innermost set of arguments the EXTRA_ARGS. */
437 static tree
438 add_to_template_args (tree args, tree extra_args)
440 tree new_args;
441 int extra_depth;
442 int i;
443 int j;
445 extra_depth = TMPL_ARGS_DEPTH (extra_args);
446 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
448 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
449 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
451 for (j = 1; j <= extra_depth; ++j, ++i)
452 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
454 return new_args;
457 /* Like add_to_template_args, but only the outermost ARGS are added to
458 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
459 (EXTRA_ARGS) levels are added. This function is used to combine
460 the template arguments from a partial instantiation with the
461 template arguments used to attain the full instantiation from the
462 partial instantiation. */
464 static tree
465 add_outermost_template_args (tree args, tree extra_args)
467 tree new_args;
469 /* If there are more levels of EXTRA_ARGS than there are ARGS,
470 something very fishy is going on. */
471 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
473 /* If *all* the new arguments will be the EXTRA_ARGS, just return
474 them. */
475 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
476 return extra_args;
478 /* For the moment, we make ARGS look like it contains fewer levels. */
479 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
481 new_args = add_to_template_args (args, extra_args);
483 /* Now, we restore ARGS to its full dimensions. */
484 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
486 return new_args;
489 /* Return the N levels of innermost template arguments from the ARGS. */
491 tree
492 get_innermost_template_args (tree args, int n)
494 tree new_args;
495 int extra_levels;
496 int i;
498 gcc_assert (n >= 0);
500 /* If N is 1, just return the innermost set of template arguments. */
501 if (n == 1)
502 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
504 /* If we're not removing anything, just return the arguments we were
505 given. */
506 extra_levels = TMPL_ARGS_DEPTH (args) - n;
507 gcc_assert (extra_levels >= 0);
508 if (extra_levels == 0)
509 return args;
511 /* Make a new set of arguments, not containing the outer arguments. */
512 new_args = make_tree_vec (n);
513 for (i = 1; i <= n; ++i)
514 SET_TMPL_ARGS_LEVEL (new_args, i,
515 TMPL_ARGS_LEVEL (args, i + extra_levels));
517 return new_args;
520 /* We've got a template header coming up; push to a new level for storing
521 the parms. */
523 void
524 begin_template_parm_list (void)
526 /* We use a non-tag-transparent scope here, which causes pushtag to
527 put tags in this scope, rather than in the enclosing class or
528 namespace scope. This is the right thing, since we want
529 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
530 global template class, push_template_decl handles putting the
531 TEMPLATE_DECL into top-level scope. For a nested template class,
532 e.g.:
534 template <class T> struct S1 {
535 template <class T> struct S2 {};
538 pushtag contains special code to call pushdecl_with_scope on the
539 TEMPLATE_DECL for S2. */
540 begin_scope (sk_template_parms, NULL);
541 ++processing_template_decl;
542 ++processing_template_parmlist;
543 note_template_header (0);
546 /* This routine is called when a specialization is declared. If it is
547 invalid to declare a specialization here, an error is reported and
548 false is returned, otherwise this routine will return true. */
550 static bool
551 check_specialization_scope (void)
553 tree scope = current_scope ();
555 /* [temp.expl.spec]
557 An explicit specialization shall be declared in the namespace of
558 which the template is a member, or, for member templates, in the
559 namespace of which the enclosing class or enclosing class
560 template is a member. An explicit specialization of a member
561 function, member class or static data member of a class template
562 shall be declared in the namespace of which the class template
563 is a member. */
564 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
566 error ("explicit specialization in non-namespace scope %qD", scope);
567 return false;
570 /* [temp.expl.spec]
572 In an explicit specialization declaration for a member of a class
573 template or a member template that appears in namespace scope,
574 the member template and some of its enclosing class templates may
575 remain unspecialized, except that the declaration shall not
576 explicitly specialize a class member template if its enclosing
577 class templates are not explicitly specialized as well. */
578 if (current_template_parms)
580 error ("enclosing class templates are not explicitly specialized");
581 return false;
584 return true;
587 /* We've just seen template <>. */
589 bool
590 begin_specialization (void)
592 begin_scope (sk_template_spec, NULL);
593 note_template_header (1);
594 return check_specialization_scope ();
597 /* Called at then end of processing a declaration preceded by
598 template<>. */
600 void
601 end_specialization (void)
603 finish_scope ();
604 reset_specialization ();
607 /* Any template <>'s that we have seen thus far are not referring to a
608 function specialization. */
610 void
611 reset_specialization (void)
613 processing_specialization = 0;
614 template_header_count = 0;
617 /* We've just seen a template header. If SPECIALIZATION is nonzero,
618 it was of the form template <>. */
620 static void
621 note_template_header (int specialization)
623 processing_specialization = specialization;
624 template_header_count++;
627 /* We're beginning an explicit instantiation. */
629 void
630 begin_explicit_instantiation (void)
632 gcc_assert (!processing_explicit_instantiation);
633 processing_explicit_instantiation = true;
637 void
638 end_explicit_instantiation (void)
640 gcc_assert (processing_explicit_instantiation);
641 processing_explicit_instantiation = false;
644 /* An explicit specialization or partial specialization TMPL is being
645 declared. Check that the namespace in which the specialization is
646 occurring is permissible. Returns false iff it is invalid to
647 specialize TMPL in the current namespace. */
649 static bool
650 check_specialization_namespace (tree tmpl)
652 tree tpl_ns = decl_namespace_context (tmpl);
654 /* [tmpl.expl.spec]
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template is
662 a member. */
663 if (is_associated_namespace (current_namespace, tpl_ns))
664 /* Same or super-using namespace. */
665 return true;
666 else
668 pedwarn ("specialization of %qD in different namespace", tmpl);
669 pedwarn (" from definition of %q+#D", tmpl);
670 return false;
674 /* SPEC is an explicit instantiation. Check that it is valid to
675 perform this explicit instantiation in the current namespace. */
677 static void
678 check_explicit_instantiation_namespace (tree spec)
680 tree ns;
682 /* DR 275: An explicit instantiation shall appear in an enclosing
683 namespace of its template. */
684 ns = decl_namespace_context (spec);
685 if (!is_ancestor (current_namespace, ns))
686 pedwarn ("explicit instantiation of %qD in namespace %qD "
687 "(which does not enclose namespace %qD)",
688 spec, current_namespace, ns);
691 /* The TYPE is being declared. If it is a template type, that means it
692 is a partial specialization. Do appropriate error-checking. */
694 tree
695 maybe_process_partial_specialization (tree type)
697 tree context;
699 if (type == error_mark_node)
700 return error_mark_node;
702 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
704 error ("name of class shadows template template parameter %qD",
705 TYPE_NAME (type));
706 return error_mark_node;
709 context = TYPE_CONTEXT (type);
711 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
713 /* This is for ordinary explicit specialization and partial
714 specialization of a template class such as:
716 template <> class C<int>;
720 template <class T> class C<T*>;
722 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
724 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
725 && !COMPLETE_TYPE_P (type))
727 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
728 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
729 if (processing_template_decl)
730 push_template_decl (TYPE_MAIN_DECL (type));
732 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
733 error ("specialization of %qT after instantiation", type);
735 else if (CLASS_TYPE_P (type)
736 && !CLASSTYPE_USE_TEMPLATE (type)
737 && CLASSTYPE_TEMPLATE_INFO (type)
738 && context && CLASS_TYPE_P (context)
739 && CLASSTYPE_TEMPLATE_INFO (context))
741 /* This is for an explicit specialization of member class
742 template according to [temp.expl.spec/18]:
744 template <> template <class U> class C<int>::D;
746 The context `C<int>' must be an implicit instantiation.
747 Otherwise this is just a member class template declared
748 earlier like:
750 template <> class C<int> { template <class U> class D; };
751 template <> template <class U> class C<int>::D;
753 In the first case, `C<int>::D' is a specialization of `C<T>::D'
754 while in the second case, `C<int>::D' is a primary template
755 and `C<T>::D' may not exist. */
757 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
758 && !COMPLETE_TYPE_P (type))
760 tree t;
762 if (current_namespace
763 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
765 pedwarn ("specializing %q#T in different namespace", type);
766 pedwarn (" from definition of %q+#D",
767 CLASSTYPE_TI_TEMPLATE (type));
770 /* Check for invalid specialization after instantiation:
772 template <> template <> class C<int>::D<int>;
773 template <> template <class U> class C<int>::D; */
775 for (t = DECL_TEMPLATE_INSTANTIATIONS
776 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
777 t; t = TREE_CHAIN (t))
778 if (TREE_VALUE (t) != type
779 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
780 error ("specialization %qT after instantiation %qT",
781 type, TREE_VALUE (t));
783 /* Mark TYPE as a specialization. And as a result, we only
784 have one level of template argument for the innermost
785 class template. */
786 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
787 CLASSTYPE_TI_ARGS (type)
788 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
791 else if (processing_specialization)
793 error ("explicit specialization of non-template %qT", type);
794 return error_mark_node;
797 return type;
800 /* Returns nonzero if we can optimize the retrieval of specializations
801 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
802 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
804 static inline bool
805 optimize_specialization_lookup_p (tree tmpl)
807 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
808 && DECL_CLASS_SCOPE_P (tmpl)
809 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
810 parameter. */
811 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
812 /* The optimized lookup depends on the fact that the
813 template arguments for the member function template apply
814 purely to the containing class, which is not true if the
815 containing class is an explicit or partial
816 specialization. */
817 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
818 && !DECL_MEMBER_TEMPLATE_P (tmpl)
819 && !DECL_CONV_FN_P (tmpl)
820 /* It is possible to have a template that is not a member
821 template and is not a member of a template class:
823 template <typename T>
824 struct S { friend A::f(); };
826 Here, the friend function is a template, but the context does
827 not have template information. The optimized lookup relies
828 on having ARGS be the template arguments for both the class
829 and the function template. */
830 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
833 /* Retrieve the specialization (in the sense of [temp.spec] - a
834 specialization is either an instantiation or an explicit
835 specialization) of TMPL for the given template ARGS. If there is
836 no such specialization, return NULL_TREE. The ARGS are a vector of
837 arguments, or a vector of vectors of arguments, in the case of
838 templates with more than one level of parameters.
840 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
841 then we search for a partial specialization matching ARGS. This
842 parameter is ignored if TMPL is not a class template. */
844 static tree
845 retrieve_specialization (tree tmpl, tree args,
846 bool class_specializations_p)
848 if (args == error_mark_node)
849 return NULL_TREE;
851 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
853 /* There should be as many levels of arguments as there are
854 levels of parameters. */
855 gcc_assert (TMPL_ARGS_DEPTH (args)
856 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
858 if (optimize_specialization_lookup_p (tmpl))
860 tree class_template;
861 tree class_specialization;
862 VEC(tree,gc) *methods;
863 tree fns;
864 int idx;
866 /* The template arguments actually apply to the containing
867 class. Find the class specialization with those
868 arguments. */
869 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
870 class_specialization
871 = retrieve_specialization (class_template, args,
872 /*class_specializations_p=*/false);
873 if (!class_specialization)
874 return NULL_TREE;
875 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
876 for the specialization. */
877 idx = class_method_index_for_fn (class_specialization, tmpl);
878 if (idx == -1)
879 return NULL_TREE;
880 /* Iterate through the methods with the indicated name, looking
881 for the one that has an instance of TMPL. */
882 methods = CLASSTYPE_METHOD_VEC (class_specialization);
883 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
885 tree fn = OVL_CURRENT (fns);
886 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
887 return fn;
889 return NULL_TREE;
891 else
893 tree *sp;
894 tree *head;
896 /* Class templates store their instantiations on the
897 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
898 DECL_TEMPLATE_SPECIALIZATIONS list. */
899 if (!class_specializations_p
900 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
901 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
902 else
903 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
904 head = sp;
905 /* Iterate through the list until we find a matching template. */
906 while (*sp != NULL_TREE)
908 tree spec = *sp;
910 if (comp_template_args (TREE_PURPOSE (spec), args))
912 /* Use the move-to-front heuristic to speed up future
913 searches. */
914 if (spec != *head)
916 *sp = TREE_CHAIN (*sp);
917 TREE_CHAIN (spec) = *head;
918 *head = spec;
920 return TREE_VALUE (spec);
922 sp = &TREE_CHAIN (spec);
926 return NULL_TREE;
929 /* Like retrieve_specialization, but for local declarations. */
931 static tree
932 retrieve_local_specialization (tree tmpl)
934 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
935 htab_hash_pointer (tmpl));
936 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
939 /* Returns nonzero iff DECL is a specialization of TMPL. */
942 is_specialization_of (tree decl, tree tmpl)
944 tree t;
946 if (TREE_CODE (decl) == FUNCTION_DECL)
948 for (t = decl;
949 t != NULL_TREE;
950 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
951 if (t == tmpl)
952 return 1;
954 else
956 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
958 for (t = TREE_TYPE (decl);
959 t != NULL_TREE;
960 t = CLASSTYPE_USE_TEMPLATE (t)
961 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
962 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
963 return 1;
966 return 0;
969 /* Returns nonzero iff DECL is a specialization of friend declaration
970 FRIEND according to [temp.friend]. */
972 bool
973 is_specialization_of_friend (tree decl, tree friend)
975 bool need_template = true;
976 int template_depth;
978 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
979 || TREE_CODE (decl) == TYPE_DECL);
981 /* For [temp.friend/6] when FRIEND is an ordinary member function
982 of a template class, we want to check if DECL is a specialization
983 if this. */
984 if (TREE_CODE (friend) == FUNCTION_DECL
985 && DECL_TEMPLATE_INFO (friend)
986 && !DECL_USE_TEMPLATE (friend))
988 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
989 friend = DECL_TI_TEMPLATE (friend);
990 need_template = false;
992 else if (TREE_CODE (friend) == TEMPLATE_DECL
993 && !PRIMARY_TEMPLATE_P (friend))
994 need_template = false;
996 /* There is nothing to do if this is not a template friend. */
997 if (TREE_CODE (friend) != TEMPLATE_DECL)
998 return false;
1000 if (is_specialization_of (decl, friend))
1001 return true;
1003 /* [temp.friend/6]
1004 A member of a class template may be declared to be a friend of a
1005 non-template class. In this case, the corresponding member of
1006 every specialization of the class template is a friend of the
1007 class granting friendship.
1009 For example, given a template friend declaration
1011 template <class T> friend void A<T>::f();
1013 the member function below is considered a friend
1015 template <> struct A<int> {
1016 void f();
1019 For this type of template friend, TEMPLATE_DEPTH below will be
1020 nonzero. To determine if DECL is a friend of FRIEND, we first
1021 check if the enclosing class is a specialization of another. */
1023 template_depth = template_class_depth (DECL_CONTEXT (friend));
1024 if (template_depth
1025 && DECL_CLASS_SCOPE_P (decl)
1026 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1027 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1029 /* Next, we check the members themselves. In order to handle
1030 a few tricky cases, such as when FRIEND's are
1032 template <class T> friend void A<T>::g(T t);
1033 template <class T> template <T t> friend void A<T>::h();
1035 and DECL's are
1037 void A<int>::g(int);
1038 template <int> void A<int>::h();
1040 we need to figure out ARGS, the template arguments from
1041 the context of DECL. This is required for template substitution
1042 of `T' in the function parameter of `g' and template parameter
1043 of `h' in the above examples. Here ARGS corresponds to `int'. */
1045 tree context = DECL_CONTEXT (decl);
1046 tree args = NULL_TREE;
1047 int current_depth = 0;
1049 while (current_depth < template_depth)
1051 if (CLASSTYPE_TEMPLATE_INFO (context))
1053 if (current_depth == 0)
1054 args = TYPE_TI_ARGS (context);
1055 else
1056 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1057 current_depth++;
1059 context = TYPE_CONTEXT (context);
1062 if (TREE_CODE (decl) == FUNCTION_DECL)
1064 bool is_template;
1065 tree friend_type;
1066 tree decl_type;
1067 tree friend_args_type;
1068 tree decl_args_type;
1070 /* Make sure that both DECL and FRIEND are templates or
1071 non-templates. */
1072 is_template = DECL_TEMPLATE_INFO (decl)
1073 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1074 if (need_template ^ is_template)
1075 return false;
1076 else if (is_template)
1078 /* If both are templates, check template parameter list. */
1079 tree friend_parms
1080 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1081 args, tf_none);
1082 if (!comp_template_parms
1083 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1084 friend_parms))
1085 return false;
1087 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1089 else
1090 decl_type = TREE_TYPE (decl);
1092 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1093 tf_none, NULL_TREE);
1094 if (friend_type == error_mark_node)
1095 return false;
1097 /* Check if return types match. */
1098 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1099 return false;
1101 /* Check if function parameter types match, ignoring the
1102 `this' parameter. */
1103 friend_args_type = TYPE_ARG_TYPES (friend_type);
1104 decl_args_type = TYPE_ARG_TYPES (decl_type);
1105 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1106 friend_args_type = TREE_CHAIN (friend_args_type);
1107 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1108 decl_args_type = TREE_CHAIN (decl_args_type);
1110 return compparms (decl_args_type, friend_args_type);
1112 else
1114 /* DECL is a TYPE_DECL */
1115 bool is_template;
1116 tree decl_type = TREE_TYPE (decl);
1118 /* Make sure that both DECL and FRIEND are templates or
1119 non-templates. */
1120 is_template
1121 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1122 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1124 if (need_template ^ is_template)
1125 return false;
1126 else if (is_template)
1128 tree friend_parms;
1129 /* If both are templates, check the name of the two
1130 TEMPLATE_DECL's first because is_friend didn't. */
1131 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1132 != DECL_NAME (friend))
1133 return false;
1135 /* Now check template parameter list. */
1136 friend_parms
1137 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1138 args, tf_none);
1139 return comp_template_parms
1140 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1141 friend_parms);
1143 else
1144 return (DECL_NAME (decl)
1145 == DECL_NAME (friend));
1148 return false;
1151 /* Register the specialization SPEC as a specialization of TMPL with
1152 the indicated ARGS. IS_FRIEND indicates whether the specialization
1153 is actually just a friend declaration. Returns SPEC, or an
1154 equivalent prior declaration, if available. */
1156 static tree
1157 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1159 tree fn;
1161 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1163 if (TREE_CODE (spec) == FUNCTION_DECL
1164 && uses_template_parms (DECL_TI_ARGS (spec)))
1165 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1166 register it; we want the corresponding TEMPLATE_DECL instead.
1167 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1168 the more obvious `uses_template_parms (spec)' to avoid problems
1169 with default function arguments. In particular, given
1170 something like this:
1172 template <class T> void f(T t1, T t = T())
1174 the default argument expression is not substituted for in an
1175 instantiation unless and until it is actually needed. */
1176 return spec;
1178 fn = retrieve_specialization (tmpl, args,
1179 /*class_specializations_p=*/false);
1180 /* We can sometimes try to re-register a specialization that we've
1181 already got. In particular, regenerate_decl_from_template calls
1182 duplicate_decls which will update the specialization list. But,
1183 we'll still get called again here anyhow. It's more convenient
1184 to simply allow this than to try to prevent it. */
1185 if (fn == spec)
1186 return spec;
1187 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1189 if (DECL_TEMPLATE_INSTANTIATION (fn))
1191 if (TREE_USED (fn)
1192 || DECL_EXPLICIT_INSTANTIATION (fn))
1194 error ("specialization of %qD after instantiation",
1195 fn);
1196 return error_mark_node;
1198 else
1200 tree clone;
1201 /* This situation should occur only if the first
1202 specialization is an implicit instantiation, the
1203 second is an explicit specialization, and the
1204 implicit instantiation has not yet been used. That
1205 situation can occur if we have implicitly
1206 instantiated a member function and then specialized
1207 it later.
1209 We can also wind up here if a friend declaration that
1210 looked like an instantiation turns out to be a
1211 specialization:
1213 template <class T> void foo(T);
1214 class S { friend void foo<>(int) };
1215 template <> void foo(int);
1217 We transform the existing DECL in place so that any
1218 pointers to it become pointers to the updated
1219 declaration.
1221 If there was a definition for the template, but not
1222 for the specialization, we want this to look as if
1223 there were no definition, and vice versa. */
1224 DECL_INITIAL (fn) = NULL_TREE;
1225 duplicate_decls (spec, fn, is_friend);
1226 /* The call to duplicate_decls will have applied
1227 [temp.expl.spec]:
1229 An explicit specialization of a function template
1230 is inline only if it is explicitly declared to be,
1231 and independently of whether its function template
1234 to the primary function; now copy the inline bits to
1235 the various clones. */
1236 FOR_EACH_CLONE (clone, fn)
1238 DECL_DECLARED_INLINE_P (clone)
1239 = DECL_DECLARED_INLINE_P (fn);
1240 DECL_INLINE (clone)
1241 = DECL_INLINE (fn);
1243 check_specialization_namespace (fn);
1245 return fn;
1248 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1250 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1251 /* Dup decl failed, but this is a new definition. Set the
1252 line number so any errors match this new
1253 definition. */
1254 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1256 return fn;
1260 /* A specialization must be declared in the same namespace as the
1261 template it is specializing. */
1262 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1263 && !check_specialization_namespace (tmpl))
1264 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1266 if (!optimize_specialization_lookup_p (tmpl))
1267 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1268 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1270 return spec;
1273 /* Unregister the specialization SPEC as a specialization of TMPL.
1274 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1275 if the SPEC was listed as a specialization of TMPL. */
1277 bool
1278 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1280 tree* s;
1282 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1283 *s != NULL_TREE;
1284 s = &TREE_CHAIN (*s))
1285 if (TREE_VALUE (*s) == spec)
1287 if (!new_spec)
1288 *s = TREE_CHAIN (*s);
1289 else
1290 TREE_VALUE (*s) = new_spec;
1291 return 1;
1294 return 0;
1297 /* Compare an entry in the local specializations hash table P1 (which
1298 is really a pointer to a TREE_LIST) with P2 (which is really a
1299 DECL). */
1301 static int
1302 eq_local_specializations (const void *p1, const void *p2)
1304 return TREE_VALUE ((tree) p1) == (tree) p2;
1307 /* Hash P1, an entry in the local specializations table. */
1309 static hashval_t
1310 hash_local_specialization (const void* p1)
1312 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1315 /* Like register_specialization, but for local declarations. We are
1316 registering SPEC, an instantiation of TMPL. */
1318 static void
1319 register_local_specialization (tree spec, tree tmpl)
1321 void **slot;
1323 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1324 htab_hash_pointer (tmpl), INSERT);
1325 *slot = build_tree_list (spec, tmpl);
1328 /* TYPE is a class type. Returns true if TYPE is an explicitly
1329 specialized class. */
1331 bool
1332 explicit_class_specialization_p (tree type)
1334 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1335 return false;
1336 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1339 /* Print the list of candidate FNS in an error message. */
1341 void
1342 print_candidates (tree fns)
1344 tree fn;
1346 const char *str = "candidates are:";
1348 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1350 tree f;
1352 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1353 error ("%s %+#D", str, OVL_CURRENT (f));
1354 str = " ";
1358 /* Returns the template (one of the functions given by TEMPLATE_ID)
1359 which can be specialized to match the indicated DECL with the
1360 explicit template args given in TEMPLATE_ID. The DECL may be
1361 NULL_TREE if none is available. In that case, the functions in
1362 TEMPLATE_ID are non-members.
1364 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1365 specialization of a member template.
1367 The TEMPLATE_COUNT is the number of references to qualifying
1368 template classes that appeared in the name of the function. See
1369 check_explicit_specialization for a more accurate description.
1371 TSK indicates what kind of template declaration (if any) is being
1372 declared. TSK_TEMPLATE indicates that the declaration given by
1373 DECL, though a FUNCTION_DECL, has template parameters, and is
1374 therefore a template function.
1376 The template args (those explicitly specified and those deduced)
1377 are output in a newly created vector *TARGS_OUT.
1379 If it is impossible to determine the result, an error message is
1380 issued. The error_mark_node is returned to indicate failure. */
1382 static tree
1383 determine_specialization (tree template_id,
1384 tree decl,
1385 tree* targs_out,
1386 int need_member_template,
1387 int template_count,
1388 tmpl_spec_kind tsk)
1390 tree fns;
1391 tree targs;
1392 tree explicit_targs;
1393 tree candidates = NULL_TREE;
1394 /* A TREE_LIST of templates of which DECL may be a specialization.
1395 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1396 corresponding TREE_PURPOSE is the set of template arguments that,
1397 when used to instantiate the template, would produce a function
1398 with the signature of DECL. */
1399 tree templates = NULL_TREE;
1400 int header_count;
1401 struct cp_binding_level *b;
1403 *targs_out = NULL_TREE;
1405 if (template_id == error_mark_node || decl == error_mark_node)
1406 return error_mark_node;
1408 fns = TREE_OPERAND (template_id, 0);
1409 explicit_targs = TREE_OPERAND (template_id, 1);
1411 if (fns == error_mark_node)
1412 return error_mark_node;
1414 /* Check for baselinks. */
1415 if (BASELINK_P (fns))
1416 fns = BASELINK_FUNCTIONS (fns);
1418 if (!is_overloaded_fn (fns))
1420 error ("%qD is not a function template", fns);
1421 return error_mark_node;
1424 /* Count the number of template headers specified for this
1425 specialization. */
1426 header_count = 0;
1427 for (b = current_binding_level;
1428 b->kind == sk_template_parms;
1429 b = b->level_chain)
1430 ++header_count;
1432 for (; fns; fns = OVL_NEXT (fns))
1434 tree fn = OVL_CURRENT (fns);
1436 if (TREE_CODE (fn) == TEMPLATE_DECL)
1438 tree decl_arg_types;
1439 tree fn_arg_types;
1441 /* In case of explicit specialization, we need to check if
1442 the number of template headers appearing in the specialization
1443 is correct. This is usually done in check_explicit_specialization,
1444 but the check done there cannot be exhaustive when specializing
1445 member functions. Consider the following code:
1447 template <> void A<int>::f(int);
1448 template <> template <> void A<int>::f(int);
1450 Assuming that A<int> is not itself an explicit specialization
1451 already, the first line specializes "f" which is a non-template
1452 member function, whilst the second line specializes "f" which
1453 is a template member function. So both lines are syntactically
1454 correct, and check_explicit_specialization does not reject
1455 them.
1457 Here, we can do better, as we are matching the specialization
1458 against the declarations. We count the number of template
1459 headers, and we check if they match TEMPLATE_COUNT + 1
1460 (TEMPLATE_COUNT is the number of qualifying template classes,
1461 plus there must be another header for the member template
1462 itself).
1464 Notice that if header_count is zero, this is not a
1465 specialization but rather a template instantiation, so there
1466 is no check we can perform here. */
1467 if (header_count && header_count != template_count + 1)
1468 continue;
1470 /* Check that the number of template arguments at the
1471 innermost level for DECL is the same as for FN. */
1472 if (current_binding_level->kind == sk_template_parms
1473 && !current_binding_level->explicit_spec_p
1474 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1475 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1476 (current_template_parms))))
1477 continue;
1479 /* DECL might be a specialization of FN. */
1480 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1481 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1483 /* For a non-static member function, we need to make sure
1484 that the const qualification is the same. Since
1485 get_bindings does not try to merge the "this" parameter,
1486 we must do the comparison explicitly. */
1487 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1488 && !same_type_p (TREE_VALUE (fn_arg_types),
1489 TREE_VALUE (decl_arg_types)))
1490 continue;
1492 /* Skip the "this" parameter and, for constructors of
1493 classes with virtual bases, the VTT parameter. A
1494 full specialization of a constructor will have a VTT
1495 parameter, but a template never will. */
1496 decl_arg_types
1497 = skip_artificial_parms_for (decl, decl_arg_types);
1498 fn_arg_types
1499 = skip_artificial_parms_for (fn, fn_arg_types);
1501 /* Check that the number of function parameters matches.
1502 For example,
1503 template <class T> void f(int i = 0);
1504 template <> void f<int>();
1505 The specialization f<int> is invalid but is not caught
1506 by get_bindings below. */
1507 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1508 continue;
1510 /* Function templates cannot be specializations; there are
1511 no partial specializations of functions. Therefore, if
1512 the type of DECL does not match FN, there is no
1513 match. */
1514 if (tsk == tsk_template)
1516 if (compparms (fn_arg_types, decl_arg_types))
1517 candidates = tree_cons (NULL_TREE, fn, candidates);
1518 continue;
1521 /* See whether this function might be a specialization of this
1522 template. */
1523 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1525 if (!targs)
1526 /* We cannot deduce template arguments that when used to
1527 specialize TMPL will produce DECL. */
1528 continue;
1530 /* Save this template, and the arguments deduced. */
1531 templates = tree_cons (targs, fn, templates);
1533 else if (need_member_template)
1534 /* FN is an ordinary member function, and we need a
1535 specialization of a member template. */
1537 else if (TREE_CODE (fn) != FUNCTION_DECL)
1538 /* We can get IDENTIFIER_NODEs here in certain erroneous
1539 cases. */
1541 else if (!DECL_FUNCTION_MEMBER_P (fn))
1542 /* This is just an ordinary non-member function. Nothing can
1543 be a specialization of that. */
1545 else if (DECL_ARTIFICIAL (fn))
1546 /* Cannot specialize functions that are created implicitly. */
1548 else
1550 tree decl_arg_types;
1552 /* This is an ordinary member function. However, since
1553 we're here, we can assume it's enclosing class is a
1554 template class. For example,
1556 template <typename T> struct S { void f(); };
1557 template <> void S<int>::f() {}
1559 Here, S<int>::f is a non-template, but S<int> is a
1560 template class. If FN has the same type as DECL, we
1561 might be in business. */
1563 if (!DECL_TEMPLATE_INFO (fn))
1564 /* Its enclosing class is an explicit specialization
1565 of a template class. This is not a candidate. */
1566 continue;
1568 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1569 TREE_TYPE (TREE_TYPE (fn))))
1570 /* The return types differ. */
1571 continue;
1573 /* Adjust the type of DECL in case FN is a static member. */
1574 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1575 if (DECL_STATIC_FUNCTION_P (fn)
1576 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1577 decl_arg_types = TREE_CHAIN (decl_arg_types);
1579 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1580 decl_arg_types))
1581 /* They match! */
1582 candidates = tree_cons (NULL_TREE, fn, candidates);
1586 if (templates && TREE_CHAIN (templates))
1588 /* We have:
1590 [temp.expl.spec]
1592 It is possible for a specialization with a given function
1593 signature to be instantiated from more than one function
1594 template. In such cases, explicit specification of the
1595 template arguments must be used to uniquely identify the
1596 function template specialization being specialized.
1598 Note that here, there's no suggestion that we're supposed to
1599 determine which of the candidate templates is most
1600 specialized. However, we, also have:
1602 [temp.func.order]
1604 Partial ordering of overloaded function template
1605 declarations is used in the following contexts to select
1606 the function template to which a function template
1607 specialization refers:
1609 -- when an explicit specialization refers to a function
1610 template.
1612 So, we do use the partial ordering rules, at least for now.
1613 This extension can only serve to make invalid programs valid,
1614 so it's safe. And, there is strong anecdotal evidence that
1615 the committee intended the partial ordering rules to apply;
1616 the EDG front end has that behavior, and John Spicer claims
1617 that the committee simply forgot to delete the wording in
1618 [temp.expl.spec]. */
1619 tree tmpl = most_specialized_instantiation (templates);
1620 if (tmpl != error_mark_node)
1622 templates = tmpl;
1623 TREE_CHAIN (templates) = NULL_TREE;
1627 if (templates == NULL_TREE && candidates == NULL_TREE)
1629 error ("template-id %qD for %q+D does not match any template "
1630 "declaration", template_id, decl);
1631 return error_mark_node;
1633 else if ((templates && TREE_CHAIN (templates))
1634 || (candidates && TREE_CHAIN (candidates))
1635 || (templates && candidates))
1637 error ("ambiguous template specialization %qD for %q+D",
1638 template_id, decl);
1639 chainon (candidates, templates);
1640 print_candidates (candidates);
1641 return error_mark_node;
1644 /* We have one, and exactly one, match. */
1645 if (candidates)
1647 tree fn = TREE_VALUE (candidates);
1648 /* DECL is a re-declaration of a template function. */
1649 if (TREE_CODE (fn) == TEMPLATE_DECL)
1650 return fn;
1651 /* It was a specialization of an ordinary member function in a
1652 template class. */
1653 *targs_out = copy_node (DECL_TI_ARGS (fn));
1654 return DECL_TI_TEMPLATE (fn);
1657 /* It was a specialization of a template. */
1658 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1659 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1661 *targs_out = copy_node (targs);
1662 SET_TMPL_ARGS_LEVEL (*targs_out,
1663 TMPL_ARGS_DEPTH (*targs_out),
1664 TREE_PURPOSE (templates));
1666 else
1667 *targs_out = TREE_PURPOSE (templates);
1668 return TREE_VALUE (templates);
1671 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1672 but with the default argument values filled in from those in the
1673 TMPL_TYPES. */
1675 static tree
1676 copy_default_args_to_explicit_spec_1 (tree spec_types,
1677 tree tmpl_types)
1679 tree new_spec_types;
1681 if (!spec_types)
1682 return NULL_TREE;
1684 if (spec_types == void_list_node)
1685 return void_list_node;
1687 /* Substitute into the rest of the list. */
1688 new_spec_types =
1689 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1690 TREE_CHAIN (tmpl_types));
1692 /* Add the default argument for this parameter. */
1693 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1694 TREE_VALUE (spec_types),
1695 new_spec_types);
1698 /* DECL is an explicit specialization. Replicate default arguments
1699 from the template it specializes. (That way, code like:
1701 template <class T> void f(T = 3);
1702 template <> void f(double);
1703 void g () { f (); }
1705 works, as required.) An alternative approach would be to look up
1706 the correct default arguments at the call-site, but this approach
1707 is consistent with how implicit instantiations are handled. */
1709 static void
1710 copy_default_args_to_explicit_spec (tree decl)
1712 tree tmpl;
1713 tree spec_types;
1714 tree tmpl_types;
1715 tree new_spec_types;
1716 tree old_type;
1717 tree new_type;
1718 tree t;
1719 tree object_type = NULL_TREE;
1720 tree in_charge = NULL_TREE;
1721 tree vtt = NULL_TREE;
1723 /* See if there's anything we need to do. */
1724 tmpl = DECL_TI_TEMPLATE (decl);
1725 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1726 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1727 if (TREE_PURPOSE (t))
1728 break;
1729 if (!t)
1730 return;
1732 old_type = TREE_TYPE (decl);
1733 spec_types = TYPE_ARG_TYPES (old_type);
1735 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1737 /* Remove the this pointer, but remember the object's type for
1738 CV quals. */
1739 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1740 spec_types = TREE_CHAIN (spec_types);
1741 tmpl_types = TREE_CHAIN (tmpl_types);
1743 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1745 /* DECL may contain more parameters than TMPL due to the extra
1746 in-charge parameter in constructors and destructors. */
1747 in_charge = spec_types;
1748 spec_types = TREE_CHAIN (spec_types);
1750 if (DECL_HAS_VTT_PARM_P (decl))
1752 vtt = spec_types;
1753 spec_types = TREE_CHAIN (spec_types);
1757 /* Compute the merged default arguments. */
1758 new_spec_types =
1759 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1761 /* Compute the new FUNCTION_TYPE. */
1762 if (object_type)
1764 if (vtt)
1765 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1766 TREE_VALUE (vtt),
1767 new_spec_types);
1769 if (in_charge)
1770 /* Put the in-charge parameter back. */
1771 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1772 TREE_VALUE (in_charge),
1773 new_spec_types);
1775 new_type = build_method_type_directly (object_type,
1776 TREE_TYPE (old_type),
1777 new_spec_types);
1779 else
1780 new_type = build_function_type (TREE_TYPE (old_type),
1781 new_spec_types);
1782 new_type = cp_build_type_attribute_variant (new_type,
1783 TYPE_ATTRIBUTES (old_type));
1784 new_type = build_exception_variant (new_type,
1785 TYPE_RAISES_EXCEPTIONS (old_type));
1786 TREE_TYPE (decl) = new_type;
1789 /* Check to see if the function just declared, as indicated in
1790 DECLARATOR, and in DECL, is a specialization of a function
1791 template. We may also discover that the declaration is an explicit
1792 instantiation at this point.
1794 Returns DECL, or an equivalent declaration that should be used
1795 instead if all goes well. Issues an error message if something is
1796 amiss. Returns error_mark_node if the error is not easily
1797 recoverable.
1799 FLAGS is a bitmask consisting of the following flags:
1801 2: The function has a definition.
1802 4: The function is a friend.
1804 The TEMPLATE_COUNT is the number of references to qualifying
1805 template classes that appeared in the name of the function. For
1806 example, in
1808 template <class T> struct S { void f(); };
1809 void S<int>::f();
1811 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1812 classes are not counted in the TEMPLATE_COUNT, so that in
1814 template <class T> struct S {};
1815 template <> struct S<int> { void f(); }
1816 template <> void S<int>::f();
1818 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1819 invalid; there should be no template <>.)
1821 If the function is a specialization, it is marked as such via
1822 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1823 is set up correctly, and it is added to the list of specializations
1824 for that template. */
1826 tree
1827 check_explicit_specialization (tree declarator,
1828 tree decl,
1829 int template_count,
1830 int flags)
1832 int have_def = flags & 2;
1833 int is_friend = flags & 4;
1834 int specialization = 0;
1835 int explicit_instantiation = 0;
1836 int member_specialization = 0;
1837 tree ctype = DECL_CLASS_CONTEXT (decl);
1838 tree dname = DECL_NAME (decl);
1839 tmpl_spec_kind tsk;
1841 if (is_friend)
1843 if (!processing_specialization)
1844 tsk = tsk_none;
1845 else
1846 tsk = tsk_excessive_parms;
1848 else
1849 tsk = current_tmpl_spec_kind (template_count);
1851 switch (tsk)
1853 case tsk_none:
1854 if (processing_specialization)
1856 specialization = 1;
1857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1859 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1861 if (is_friend)
1862 /* This could be something like:
1864 template <class T> void f(T);
1865 class S { friend void f<>(int); } */
1866 specialization = 1;
1867 else
1869 /* This case handles bogus declarations like template <>
1870 template <class T> void f<int>(); */
1872 error ("template-id %qD in declaration of primary template",
1873 declarator);
1874 return decl;
1877 break;
1879 case tsk_invalid_member_spec:
1880 /* The error has already been reported in
1881 check_specialization_scope. */
1882 return error_mark_node;
1884 case tsk_invalid_expl_inst:
1885 error ("template parameter list used in explicit instantiation");
1887 /* Fall through. */
1889 case tsk_expl_inst:
1890 if (have_def)
1891 error ("definition provided for explicit instantiation");
1893 explicit_instantiation = 1;
1894 break;
1896 case tsk_excessive_parms:
1897 case tsk_insufficient_parms:
1898 if (tsk == tsk_excessive_parms)
1899 error ("too many template parameter lists in declaration of %qD",
1900 decl);
1901 else if (template_header_count)
1902 error("too few template parameter lists in declaration of %qD", decl);
1903 else
1904 error("explicit specialization of %qD must be introduced by "
1905 "%<template <>%>", decl);
1907 /* Fall through. */
1908 case tsk_expl_spec:
1909 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1910 if (ctype)
1911 member_specialization = 1;
1912 else
1913 specialization = 1;
1914 break;
1916 case tsk_template:
1917 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1919 /* This case handles bogus declarations like template <>
1920 template <class T> void f<int>(); */
1922 if (uses_template_parms (declarator))
1923 error ("function template partial specialization %qD "
1924 "is not allowed", declarator);
1925 else
1926 error ("template-id %qD in declaration of primary template",
1927 declarator);
1928 return decl;
1931 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1932 /* This is a specialization of a member template, without
1933 specialization the containing class. Something like:
1935 template <class T> struct S {
1936 template <class U> void f (U);
1938 template <> template <class U> void S<int>::f(U) {}
1940 That's a specialization -- but of the entire template. */
1941 specialization = 1;
1942 break;
1944 default:
1945 gcc_unreachable ();
1948 if (specialization || member_specialization)
1950 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1951 for (; t; t = TREE_CHAIN (t))
1952 if (TREE_PURPOSE (t))
1954 pedwarn
1955 ("default argument specified in explicit specialization");
1956 break;
1960 if (specialization || member_specialization || explicit_instantiation)
1962 tree tmpl = NULL_TREE;
1963 tree targs = NULL_TREE;
1965 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1966 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1968 tree fns;
1970 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1971 if (ctype)
1972 fns = dname;
1973 else
1975 /* If there is no class context, the explicit instantiation
1976 must be at namespace scope. */
1977 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1979 /* Find the namespace binding, using the declaration
1980 context. */
1981 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1982 false, true);
1983 if (!fns || !is_overloaded_fn (fns))
1985 error ("%qD is not a template function", dname);
1986 fns = error_mark_node;
1988 else
1990 tree fn = OVL_CURRENT (fns);
1991 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1992 CP_DECL_CONTEXT (fn)))
1993 error ("%qD is not declared in %qD",
1994 decl, current_namespace);
1998 declarator = lookup_template_function (fns, NULL_TREE);
2001 if (declarator == error_mark_node)
2002 return error_mark_node;
2004 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2006 if (!explicit_instantiation)
2007 /* A specialization in class scope. This is invalid,
2008 but the error will already have been flagged by
2009 check_specialization_scope. */
2010 return error_mark_node;
2011 else
2013 /* It's not valid to write an explicit instantiation in
2014 class scope, e.g.:
2016 class C { template void f(); }
2018 This case is caught by the parser. However, on
2019 something like:
2021 template class C { void f(); };
2023 (which is invalid) we can get here. The error will be
2024 issued later. */
2028 return decl;
2030 else if (ctype != NULL_TREE
2031 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2032 IDENTIFIER_NODE))
2034 /* Find the list of functions in ctype that have the same
2035 name as the declared function. */
2036 tree name = TREE_OPERAND (declarator, 0);
2037 tree fns = NULL_TREE;
2038 int idx;
2040 if (constructor_name_p (name, ctype))
2042 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2044 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2045 : !CLASSTYPE_DESTRUCTORS (ctype))
2047 /* From [temp.expl.spec]:
2049 If such an explicit specialization for the member
2050 of a class template names an implicitly-declared
2051 special member function (clause _special_), the
2052 program is ill-formed.
2054 Similar language is found in [temp.explicit]. */
2055 error ("specialization of implicitly-declared special member function");
2056 return error_mark_node;
2059 name = is_constructor ? ctor_identifier : dtor_identifier;
2062 if (!DECL_CONV_FN_P (decl))
2064 idx = lookup_fnfields_1 (ctype, name);
2065 if (idx >= 0)
2066 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2068 else
2070 VEC(tree,gc) *methods;
2071 tree ovl;
2073 /* For a type-conversion operator, we cannot do a
2074 name-based lookup. We might be looking for `operator
2075 int' which will be a specialization of `operator T'.
2076 So, we find *all* the conversion operators, and then
2077 select from them. */
2078 fns = NULL_TREE;
2080 methods = CLASSTYPE_METHOD_VEC (ctype);
2081 if (methods)
2082 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2083 VEC_iterate (tree, methods, idx, ovl);
2084 ++idx)
2086 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2087 /* There are no more conversion functions. */
2088 break;
2090 /* Glue all these conversion functions together
2091 with those we already have. */
2092 for (; ovl; ovl = OVL_NEXT (ovl))
2093 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2097 if (fns == NULL_TREE)
2099 error ("no member function %qD declared in %qT", name, ctype);
2100 return error_mark_node;
2102 else
2103 TREE_OPERAND (declarator, 0) = fns;
2106 /* Figure out what exactly is being specialized at this point.
2107 Note that for an explicit instantiation, even one for a
2108 member function, we cannot tell apriori whether the
2109 instantiation is for a member template, or just a member
2110 function of a template class. Even if a member template is
2111 being instantiated, the member template arguments may be
2112 elided if they can be deduced from the rest of the
2113 declaration. */
2114 tmpl = determine_specialization (declarator, decl,
2115 &targs,
2116 member_specialization,
2117 template_count,
2118 tsk);
2120 if (!tmpl || tmpl == error_mark_node)
2121 /* We couldn't figure out what this declaration was
2122 specializing. */
2123 return error_mark_node;
2124 else
2126 tree gen_tmpl = most_general_template (tmpl);
2128 if (explicit_instantiation)
2130 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2131 is done by do_decl_instantiation later. */
2133 int arg_depth = TMPL_ARGS_DEPTH (targs);
2134 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2136 if (arg_depth > parm_depth)
2138 /* If TMPL is not the most general template (for
2139 example, if TMPL is a friend template that is
2140 injected into namespace scope), then there will
2141 be too many levels of TARGS. Remove some of them
2142 here. */
2143 int i;
2144 tree new_targs;
2146 new_targs = make_tree_vec (parm_depth);
2147 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2148 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2149 = TREE_VEC_ELT (targs, i);
2150 targs = new_targs;
2153 return instantiate_template (tmpl, targs, tf_error);
2156 /* If we thought that the DECL was a member function, but it
2157 turns out to be specializing a static member function,
2158 make DECL a static member function as well. */
2159 if (DECL_STATIC_FUNCTION_P (tmpl)
2160 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2161 revert_static_member_fn (decl);
2163 /* If this is a specialization of a member template of a
2164 template class, we want to return the TEMPLATE_DECL, not
2165 the specialization of it. */
2166 if (tsk == tsk_template)
2168 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2169 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2170 if (have_def)
2172 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2173 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2174 = DECL_SOURCE_LOCATION (decl);
2175 /* We want to use the argument list specified in the
2176 definition, not in the original declaration. */
2177 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2178 = DECL_ARGUMENTS (decl);
2180 return tmpl;
2183 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2184 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2186 /* Inherit default function arguments from the template
2187 DECL is specializing. */
2188 copy_default_args_to_explicit_spec (decl);
2190 /* This specialization has the same protection as the
2191 template it specializes. */
2192 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2193 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2195 /* If DECL is a friend declaration, declared using an
2196 unqualified name, the namespace associated with DECL may
2197 have been set incorrectly. For example, in:
2199 template <typename T> void f(T);
2200 namespace N {
2201 struct S { friend void f<int>(int); }
2204 we will have set the DECL_CONTEXT for the friend
2205 declaration to N, rather than to the global namespace. */
2206 if (DECL_NAMESPACE_SCOPE_P (decl))
2207 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2209 if (is_friend && !have_def)
2210 /* This is not really a declaration of a specialization.
2211 It's just the name of an instantiation. But, it's not
2212 a request for an instantiation, either. */
2213 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2214 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2215 /* This is indeed a specialization. In case of constructors
2216 and destructors, we need in-charge and not-in-charge
2217 versions in V3 ABI. */
2218 clone_function_decl (decl, /*update_method_vec_p=*/0);
2220 /* Register this specialization so that we can find it
2221 again. */
2222 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2226 return decl;
2229 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2230 parameters. These are represented in the same format used for
2231 DECL_TEMPLATE_PARMS. */
2234 comp_template_parms (tree parms1, tree parms2)
2236 tree p1;
2237 tree p2;
2239 if (parms1 == parms2)
2240 return 1;
2242 for (p1 = parms1, p2 = parms2;
2243 p1 != NULL_TREE && p2 != NULL_TREE;
2244 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2246 tree t1 = TREE_VALUE (p1);
2247 tree t2 = TREE_VALUE (p2);
2248 int i;
2250 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2251 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2253 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2254 return 0;
2256 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2258 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2259 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2261 /* If either of the template parameters are invalid, assume
2262 they match for the sake of error recovery. */
2263 if (parm1 == error_mark_node || parm2 == error_mark_node)
2264 return 1;
2266 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2267 return 0;
2269 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2270 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2271 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2272 continue;
2273 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2274 return 0;
2278 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2279 /* One set of parameters has more parameters lists than the
2280 other. */
2281 return 0;
2283 return 1;
2286 /* Determine whether PARM is a parameter pack. */
2287 bool
2288 template_parameter_pack_p (tree parm)
2290 /* Determine if we have a non-type template parameter pack. */
2291 if (TREE_CODE (parm) == PARM_DECL)
2292 return (DECL_TEMPLATE_PARM_P (parm)
2293 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2295 /* If this is a list of template parameters, we could get a
2296 TYPE_DECL or a TEMPLATE_DECL. */
2297 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2298 parm = TREE_TYPE (parm);
2300 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2301 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2302 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2305 /* Determine whether PARMS describes a variadic template parameter
2306 list, i.e., one that is terminated by a template parameter pack. */
2307 bool
2308 template_parms_variadic_p (tree parms)
2310 int nparms = TREE_VEC_LENGTH (parms);
2311 tree last_parm = TREE_VALUE (TREE_VEC_ELT (parms, nparms - 1));
2313 return template_parameter_pack_p (last_parm);
2316 /* Determine whether ARGS describes a variadic template args list,
2317 i.e., one that is terminated by a template argument pack. */
2318 static bool
2319 template_args_variadic_p (tree args)
2321 int nargs;
2322 tree last_parm;
2324 if (args == NULL_TREE)
2325 return false;
2327 args = INNERMOST_TEMPLATE_ARGS (args);
2328 nargs = TREE_VEC_LENGTH (args);
2330 if (nargs == 0)
2331 return false;
2333 last_parm = TREE_VEC_ELT (args, nargs - 1);
2335 return ARGUMENT_PACK_P (last_parm);
2338 /* Generate a new name for the parameter pack name NAME (an
2339 IDENTIFIER_NODE) that incorporates its */
2340 static tree
2341 make_ith_pack_parameter_name (tree name, int i)
2343 /* Munge the name to include the parameter index. */
2344 char numbuf[128];
2345 char* newname;
2347 sprintf(numbuf, "%i", i);
2348 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2349 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2350 return get_identifier (newname);
2353 /* Structure used to track the progress of find_parameter_pack_r. */
2354 struct find_parameter_pack_data
2356 tree* parameter_packs;
2357 struct pointer_set_t *visited;
2360 /* Identifiers all of the argument packs that occur in a template
2361 argument and appends them to the TREE_LIST inside DATA, which is a
2362 find_parameter_pack_Data structure. This is a subroutine of
2363 make_pack_expansion and uses_parameter_packs. */
2364 static tree
2365 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2367 tree t = *tp;
2368 struct find_parameter_pack_data* ppd =
2369 (struct find_parameter_pack_data*)data;
2371 if (TYPE_P (t))
2373 tree context = TYPE_CONTEXT (t);
2374 walk_tree (&context, &find_parameter_packs_r, ppd, ppd->visited);
2377 /* This switch statement will return immediately if we don't find a
2378 parameter pack. */
2379 switch (TREE_CODE (t))
2381 case TEMPLATE_PARM_INDEX:
2382 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2383 break;
2384 return NULL_TREE;
2386 case BOUND_TEMPLATE_TEMPLATE_PARM:
2387 /* Check the template arguments. */
2388 walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2389 ppd->visited);
2391 /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */
2392 t = TYPE_TI_TEMPLATE (t);
2393 if (DECL_P (t) && TREE_TYPE (t))
2394 t = TREE_TYPE (t);
2395 *walk_subtrees = 0;
2397 /* Fall through. */
2399 case TEMPLATE_TYPE_PARM:
2400 case TEMPLATE_TEMPLATE_PARM:
2401 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2402 break;
2403 return NULL_TREE;
2405 case PARM_DECL:
2406 if (FUNCTION_PARAMETER_PACK_P (t))
2408 /* We don't want to walk into the type of a PARM_DECL,
2409 because we don't want to see the type parameter pack.*/
2410 *walk_subtrees = 0;
2411 break;
2413 return NULL_TREE;
2415 case RECORD_TYPE:
2416 if (TYPE_PTRMEMFUNC_P (t))
2417 return NULL_TREE;
2418 /* Fall through. */
2420 case UNION_TYPE:
2421 case ENUMERAL_TYPE:
2422 if (TYPE_TEMPLATE_INFO (t))
2424 tree args = TREE_VALUE (TYPE_TEMPLATE_INFO (t));
2425 walk_tree (&args, &find_parameter_packs_r, ppd, ppd->visited);
2428 *walk_subtrees = 0;
2429 return NULL_TREE;
2431 case TEMPLATE_DECL:
2432 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
2433 && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2434 break;
2436 *walk_subtrees = 0;
2437 return NULL_TREE;
2439 case TYPE_PACK_EXPANSION:
2440 case EXPR_PACK_EXPANSION:
2441 *walk_subtrees = 0;
2442 return NULL_TREE;
2444 case INTEGER_TYPE:
2445 walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2446 ppd, ppd->visited);
2447 *walk_subtrees = 0;
2448 return NULL_TREE;
2450 default:
2451 return NULL_TREE;
2454 /* Add this parameter pack to the list. */
2455 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2457 return NULL_TREE;
2460 /* Determines if the expression or type T uses any parameter packs. */
2461 bool
2462 uses_parameter_packs (tree t)
2464 tree parameter_packs = NULL_TREE;
2465 struct find_parameter_pack_data ppd;
2466 ppd.parameter_packs = &parameter_packs;
2467 ppd.visited = pointer_set_create ();
2468 walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2469 pointer_set_destroy (ppd.visited);
2470 return parameter_packs != NULL_TREE;
2473 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2474 representation a base-class initializer into a parameter pack
2475 expansion. If all goes well, the resulting node will be an
2476 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2477 respectively. */
2478 tree
2479 make_pack_expansion (tree arg)
2481 tree result;
2482 tree parameter_packs = NULL_TREE;
2483 bool for_types = false;
2484 struct find_parameter_pack_data ppd;
2486 if (!arg || arg == error_mark_node)
2487 return arg;
2489 if (TREE_CODE (arg) == TREE_LIST)
2491 /* The only time we will see a TREE_LIST here is for a base
2492 class initializer. In this case, the TREE_PURPOSE will be a
2493 _TYPE node (representing the base class expansion we're
2494 initializing) and the TREE_VALUE will be a TREE_LIST
2495 containing the initialization arguments.
2497 The resulting expansion looks somewhat different from most
2498 expansions. Rather than returning just one _EXPANSION, we
2499 return a TREE_LIST whose TREE_PURPOSE is a
2500 TYPE_PACK_EXPANSION containing the bases that will be
2501 initialized. The TREE_VALUE will be identical to the
2502 original TREE_VALUE, which is a list of arguments that will
2503 be passed to each base. We do not introduce any new pack
2504 expansion nodes into the TREE_VALUE (although it is possible
2505 that some already exist), because the TREE_PURPOSE and
2506 TREE_VALUE all need to be expanded together with the same
2507 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2508 resulting TREE_PURPOSE will mention the parameter packs in
2509 both the bases and the arguments to the bases. */
2510 tree purpose;
2511 tree value;
2512 tree parameter_packs = NULL_TREE;
2514 /* Determine which parameter packs will be used by the base
2515 class expansion. */
2516 ppd.visited = pointer_set_create ();
2517 ppd.parameter_packs = &parameter_packs;
2518 walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2519 &ppd, ppd.visited);
2521 if (parameter_packs == NULL_TREE)
2523 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2524 pointer_set_destroy (ppd.visited);
2525 return error_mark_node;
2528 if (TREE_VALUE (arg) != void_type_node)
2530 /* Collect the sets of parameter packs used in each of the
2531 initialization arguments. */
2532 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2534 /* Determine which parameter packs will be expanded in this
2535 argument. */
2536 walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2537 &ppd, ppd.visited);
2541 pointer_set_destroy (ppd.visited);
2543 /* Create the pack expansion type for the base type. */
2544 purpose = make_node (TYPE_PACK_EXPANSION);
2545 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2546 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2548 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2549 they will rarely be compared to anything. */
2550 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2552 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2555 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2556 for_types = true;
2558 /* Build the PACK_EXPANSION_* node. */
2559 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2560 SET_PACK_EXPANSION_PATTERN (result, arg);
2561 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2563 /* Propagate type and const-expression information. */
2564 TREE_TYPE (result) = TREE_TYPE (arg);
2565 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2567 else
2568 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2569 they will rarely be compared to anything. */
2570 SET_TYPE_STRUCTURAL_EQUALITY (result);
2572 /* Determine which parameter packs will be expanded. */
2573 ppd.parameter_packs = &parameter_packs;
2574 ppd.visited = pointer_set_create ();
2575 walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2576 pointer_set_destroy (ppd.visited);
2578 /* Make sure we found some parameter packs. */
2579 if (parameter_packs == NULL_TREE)
2581 if (TYPE_P (arg))
2582 error ("expansion pattern %<%T%> contains no argument packs", arg);
2583 else
2584 error ("expansion pattern %<%E%> contains no argument packs", arg);
2585 return error_mark_node;
2587 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2589 return result;
2592 /* Checks T for any "bare" parameter packs, which have not yet been
2593 expanded, and issues an error if any are found. This operation can
2594 only be done on full expressions or types (e.g., an expression
2595 statement, "if" condition, etc.), because we could have expressions like:
2597 foo(f(g(h(args)))...)
2599 where "args" is a parameter pack. check_for_bare_parameter_packs
2600 should not be called for the subexpressions args, h(args),
2601 g(h(args)), or f(g(h(args))), because we would produce erroneous
2602 error messages. */
2603 void
2604 check_for_bare_parameter_packs (tree t)
2606 tree parameter_packs = NULL_TREE;
2607 struct find_parameter_pack_data ppd;
2609 if (!processing_template_decl || !t || t == error_mark_node)
2610 return;
2612 if (TREE_CODE (t) == TYPE_DECL)
2613 t = TREE_TYPE (t);
2615 ppd.parameter_packs = &parameter_packs;
2616 ppd.visited = pointer_set_create ();
2617 walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2618 pointer_set_destroy (ppd.visited);
2620 if (parameter_packs) {
2621 error ("parameter packs not expanded with `...':");
2622 while (parameter_packs)
2624 tree pack = TREE_VALUE (parameter_packs);
2625 tree name = NULL_TREE;
2627 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2628 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2629 name = TYPE_NAME (pack);
2630 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2631 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2632 else
2633 name = DECL_NAME (pack);
2634 inform (" %qD", name);
2636 parameter_packs = TREE_CHAIN (parameter_packs);
2641 /* Expand any parameter packs that occur in the template arguments in
2642 ARGS. */
2643 tree
2644 expand_template_argument_pack (tree args)
2646 tree result_args = NULL_TREE;
2647 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2648 int num_result_args = -1;
2650 /* First, determine if we need to expand anything, and the number of
2651 slots we'll need. */
2652 for (in_arg = 0; in_arg < nargs; ++in_arg)
2654 tree arg = TREE_VEC_ELT (args, in_arg);
2655 if (ARGUMENT_PACK_P (arg))
2657 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2658 if (num_result_args < 0)
2659 num_result_args = in_arg + num_packed;
2660 else
2661 num_result_args += num_packed;
2663 else
2665 if (num_result_args >= 0)
2666 num_result_args++;
2670 /* If no expansion is necessary, we're done. */
2671 if (num_result_args < 0)
2672 return args;
2674 /* Expand arguments. */
2675 result_args = make_tree_vec (num_result_args);
2676 for (in_arg = 0; in_arg < nargs; ++in_arg)
2678 tree arg = TREE_VEC_ELT (args, in_arg);
2679 if (ARGUMENT_PACK_P (arg))
2681 tree packed = ARGUMENT_PACK_ARGS (arg);
2682 int i, num_packed = TREE_VEC_LENGTH (packed);
2683 for (i = 0; i < num_packed; ++i, ++out_arg)
2684 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2686 else
2688 TREE_VEC_ELT (result_args, out_arg) = arg;
2689 ++out_arg;
2693 return result_args;
2696 /* Complain if DECL shadows a template parameter.
2698 [temp.local]: A template-parameter shall not be redeclared within its
2699 scope (including nested scopes). */
2701 void
2702 check_template_shadow (tree decl)
2704 tree olddecl;
2706 /* If we're not in a template, we can't possibly shadow a template
2707 parameter. */
2708 if (!current_template_parms)
2709 return;
2711 /* Figure out what we're shadowing. */
2712 if (TREE_CODE (decl) == OVERLOAD)
2713 decl = OVL_CURRENT (decl);
2714 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2716 /* If there's no previous binding for this name, we're not shadowing
2717 anything, let alone a template parameter. */
2718 if (!olddecl)
2719 return;
2721 /* If we're not shadowing a template parameter, we're done. Note
2722 that OLDDECL might be an OVERLOAD (or perhaps even an
2723 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2724 node. */
2725 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2726 return;
2728 /* We check for decl != olddecl to avoid bogus errors for using a
2729 name inside a class. We check TPFI to avoid duplicate errors for
2730 inline member templates. */
2731 if (decl == olddecl
2732 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2733 return;
2735 error ("declaration of %q+#D", decl);
2736 error (" shadows template parm %q+#D", olddecl);
2739 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2740 ORIG_LEVEL, DECL, and TYPE. */
2742 static tree
2743 build_template_parm_index (int index,
2744 int level,
2745 int orig_level,
2746 tree decl,
2747 tree type)
2749 tree t = make_node (TEMPLATE_PARM_INDEX);
2750 TEMPLATE_PARM_IDX (t) = index;
2751 TEMPLATE_PARM_LEVEL (t) = level;
2752 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2753 TEMPLATE_PARM_DECL (t) = decl;
2754 TREE_TYPE (t) = type;
2755 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2756 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2757 TREE_READONLY (t) = TREE_READONLY (decl);
2759 return t;
2762 /* Find the canonical type parameter for the given template type
2763 parameter. Returns the canonical type parameter, which may be TYPE
2764 if no such parameter existed. */
2765 static tree
2766 canonical_type_parameter (tree type)
2768 tree list;
2769 int idx = TEMPLATE_TYPE_IDX (type);
2770 if (!canonical_template_parms)
2771 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2773 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2774 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2776 list = VEC_index (tree, canonical_template_parms, idx);
2777 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2778 list = TREE_CHAIN (list);
2780 if (list)
2781 return TREE_VALUE (list);
2782 else
2784 VEC_replace(tree, canonical_template_parms, idx,
2785 tree_cons (NULL_TREE, type,
2786 VEC_index (tree, canonical_template_parms, idx)));
2787 return type;
2791 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2792 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2793 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2794 new one is created. */
2796 static tree
2797 reduce_template_parm_level (tree index, tree type, int levels)
2799 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2800 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2801 != TEMPLATE_PARM_LEVEL (index) - levels))
2803 tree orig_decl = TEMPLATE_PARM_DECL (index);
2804 tree decl, t;
2806 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2807 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2808 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2809 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2810 DECL_ARTIFICIAL (decl) = 1;
2811 SET_DECL_TEMPLATE_PARM_P (decl);
2813 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2814 TEMPLATE_PARM_LEVEL (index) - levels,
2815 TEMPLATE_PARM_ORIG_LEVEL (index),
2816 decl, type);
2817 TEMPLATE_PARM_DESCENDANTS (index) = t;
2818 TEMPLATE_PARM_PARAMETER_PACK (t)
2819 = TEMPLATE_PARM_PARAMETER_PACK (index);
2821 /* Template template parameters need this. */
2822 if (TREE_CODE (decl) != CONST_DECL)
2823 DECL_TEMPLATE_PARMS (decl)
2824 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2827 return TEMPLATE_PARM_DESCENDANTS (index);
2830 /* Process information from new template parameter PARM and append it to the
2831 LIST being built. This new parameter is a non-type parameter iff
2832 IS_NON_TYPE is true. This new parameter is a parameter
2833 pack iff IS_PARAMETER_PACK is true. */
2835 tree
2836 process_template_parm (tree list, tree parm, bool is_non_type,
2837 bool is_parameter_pack)
2839 tree decl = 0;
2840 tree defval;
2841 tree err_parm_list;
2842 int idx = 0;
2844 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2845 defval = TREE_PURPOSE (parm);
2847 if (list)
2849 tree p = tree_last (list);
2851 if (p && TREE_VALUE (p) != error_mark_node)
2853 p = TREE_VALUE (p);
2854 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2855 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2856 else
2857 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2860 ++idx;
2862 else
2863 idx = 0;
2865 if (is_non_type)
2867 parm = TREE_VALUE (parm);
2869 SET_DECL_TEMPLATE_PARM_P (parm);
2871 if (TREE_TYPE (parm) == error_mark_node)
2873 err_parm_list = build_tree_list (defval, parm);
2874 TREE_VALUE (err_parm_list) = error_mark_node;
2875 return chainon (list, err_parm_list);
2877 else
2879 /* [temp.param]
2881 The top-level cv-qualifiers on the template-parameter are
2882 ignored when determining its type. */
2883 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2884 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2886 err_parm_list = build_tree_list (defval, parm);
2887 TREE_VALUE (err_parm_list) = error_mark_node;
2888 return chainon (list, err_parm_list);
2891 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
2893 /* This template parameter is not a parameter pack, but it
2894 should be. Complain about "bare" parameter packs. */
2895 check_for_bare_parameter_packs (TREE_TYPE (parm));
2897 /* Recover by calling this a parameter pack. */
2898 is_parameter_pack = true;
2902 /* A template parameter is not modifiable. */
2903 TREE_CONSTANT (parm) = 1;
2904 TREE_INVARIANT (parm) = 1;
2905 TREE_READONLY (parm) = 1;
2906 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2907 TREE_CONSTANT (decl) = 1;
2908 TREE_INVARIANT (decl) = 1;
2909 TREE_READONLY (decl) = 1;
2910 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2911 = build_template_parm_index (idx, processing_template_decl,
2912 processing_template_decl,
2913 decl, TREE_TYPE (parm));
2915 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
2916 = is_parameter_pack;
2918 else
2920 tree t;
2921 parm = TREE_VALUE (TREE_VALUE (parm));
2923 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2925 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2926 /* This is for distinguishing between real templates and template
2927 template parameters */
2928 TREE_TYPE (parm) = t;
2929 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2930 decl = parm;
2932 else
2934 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2935 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2936 decl = build_decl (TYPE_DECL, parm, t);
2939 TYPE_NAME (t) = decl;
2940 TYPE_STUB_DECL (t) = decl;
2941 parm = decl;
2942 TEMPLATE_TYPE_PARM_INDEX (t)
2943 = build_template_parm_index (idx, processing_template_decl,
2944 processing_template_decl,
2945 decl, TREE_TYPE (parm));
2946 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
2947 TYPE_CANONICAL (t) = canonical_type_parameter (t);
2949 DECL_ARTIFICIAL (decl) = 1;
2950 SET_DECL_TEMPLATE_PARM_P (decl);
2951 pushdecl (decl);
2952 parm = build_tree_list (defval, parm);
2953 return chainon (list, parm);
2956 /* The end of a template parameter list has been reached. Process the
2957 tree list into a parameter vector, converting each parameter into a more
2958 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2959 as PARM_DECLs. */
2961 tree
2962 end_template_parm_list (tree parms)
2964 int nparms;
2965 tree parm, next;
2966 tree saved_parmlist = make_tree_vec (list_length (parms));
2968 current_template_parms
2969 = tree_cons (size_int (processing_template_decl),
2970 saved_parmlist, current_template_parms);
2972 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2974 next = TREE_CHAIN (parm);
2975 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2976 TREE_CHAIN (parm) = NULL_TREE;
2979 --processing_template_parmlist;
2981 return saved_parmlist;
2984 /* end_template_decl is called after a template declaration is seen. */
2986 void
2987 end_template_decl (void)
2989 reset_specialization ();
2991 if (! processing_template_decl)
2992 return;
2994 /* This matches the pushlevel in begin_template_parm_list. */
2995 finish_scope ();
2997 --processing_template_decl;
2998 current_template_parms = TREE_CHAIN (current_template_parms);
3001 /* Within the declaration of a template, return all levels of template
3002 parameters that apply. The template parameters are represented as
3003 a TREE_VEC, in the form documented in cp-tree.h for template
3004 arguments. */
3006 static tree
3007 current_template_args (void)
3009 tree header;
3010 tree args = NULL_TREE;
3011 int length = TMPL_PARMS_DEPTH (current_template_parms);
3012 int l = length;
3014 /* If there is only one level of template parameters, we do not
3015 create a TREE_VEC of TREE_VECs. Instead, we return a single
3016 TREE_VEC containing the arguments. */
3017 if (length > 1)
3018 args = make_tree_vec (length);
3020 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3022 tree a = copy_node (TREE_VALUE (header));
3023 int i;
3025 TREE_TYPE (a) = NULL_TREE;
3026 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3028 tree t = TREE_VEC_ELT (a, i);
3030 /* T will be a list if we are called from within a
3031 begin/end_template_parm_list pair, but a vector directly
3032 if within a begin/end_member_template_processing pair. */
3033 if (TREE_CODE (t) == TREE_LIST)
3035 t = TREE_VALUE (t);
3037 if (t != error_mark_node)
3039 if (TREE_CODE (t) == TYPE_DECL
3040 || TREE_CODE (t) == TEMPLATE_DECL)
3042 t = TREE_TYPE (t);
3044 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3046 /* Turn this argument into a TYPE_ARGUMENT_PACK
3047 with a single element, which expands T. */
3048 tree vec = make_tree_vec (1);
3049 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3051 t = make_node (TYPE_ARGUMENT_PACK);
3052 SET_ARGUMENT_PACK_ARGS (t, vec);
3055 else
3057 t = DECL_INITIAL (t);
3059 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3061 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3062 with a single element, which expands T. */
3063 tree vec = make_tree_vec (1);
3064 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3065 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3067 t = make_node (NONTYPE_ARGUMENT_PACK);
3068 SET_ARGUMENT_PACK_ARGS (t, vec);
3069 TREE_TYPE (t) = type;
3073 TREE_VEC_ELT (a, i) = t;
3077 if (length > 1)
3078 TREE_VEC_ELT (args, --l) = a;
3079 else
3080 args = a;
3083 return args;
3086 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3087 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3088 a member template. Used by push_template_decl below. */
3090 static tree
3091 build_template_decl (tree decl, tree parms, bool member_template_p)
3093 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3094 DECL_TEMPLATE_PARMS (tmpl) = parms;
3095 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3096 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3097 if (DECL_LANG_SPECIFIC (decl))
3099 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3100 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3101 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3102 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3103 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3104 if (DECL_OVERLOADED_OPERATOR_P (decl))
3105 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3106 DECL_OVERLOADED_OPERATOR_P (decl));
3109 return tmpl;
3112 struct template_parm_data
3114 /* The level of the template parameters we are currently
3115 processing. */
3116 int level;
3118 /* The index of the specialization argument we are currently
3119 processing. */
3120 int current_arg;
3122 /* An array whose size is the number of template parameters. The
3123 elements are nonzero if the parameter has been used in any one
3124 of the arguments processed so far. */
3125 int* parms;
3127 /* An array whose size is the number of template arguments. The
3128 elements are nonzero if the argument makes use of template
3129 parameters of this level. */
3130 int* arg_uses_template_parms;
3133 /* Subroutine of push_template_decl used to see if each template
3134 parameter in a partial specialization is used in the explicit
3135 argument list. If T is of the LEVEL given in DATA (which is
3136 treated as a template_parm_data*), then DATA->PARMS is marked
3137 appropriately. */
3139 static int
3140 mark_template_parm (tree t, void* data)
3142 int level;
3143 int idx;
3144 struct template_parm_data* tpd = (struct template_parm_data*) data;
3146 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3148 level = TEMPLATE_PARM_LEVEL (t);
3149 idx = TEMPLATE_PARM_IDX (t);
3151 else
3153 level = TEMPLATE_TYPE_LEVEL (t);
3154 idx = TEMPLATE_TYPE_IDX (t);
3157 if (level == tpd->level)
3159 tpd->parms[idx] = 1;
3160 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3163 /* Return zero so that for_each_template_parm will continue the
3164 traversal of the tree; we want to mark *every* template parm. */
3165 return 0;
3168 /* Process the partial specialization DECL. */
3170 static tree
3171 process_partial_specialization (tree decl)
3173 tree type = TREE_TYPE (decl);
3174 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3175 tree specargs = CLASSTYPE_TI_ARGS (type);
3176 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3177 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3178 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3179 int nargs = TREE_VEC_LENGTH (inner_args);
3180 int ntparms = TREE_VEC_LENGTH (inner_parms);
3181 int i;
3182 int did_error_intro = 0;
3183 struct template_parm_data tpd;
3184 struct template_parm_data tpd2;
3186 /* We check that each of the template parameters given in the
3187 partial specialization is used in the argument list to the
3188 specialization. For example:
3190 template <class T> struct S;
3191 template <class T> struct S<T*>;
3193 The second declaration is OK because `T*' uses the template
3194 parameter T, whereas
3196 template <class T> struct S<int>;
3198 is no good. Even trickier is:
3200 template <class T>
3201 struct S1
3203 template <class U>
3204 struct S2;
3205 template <class U>
3206 struct S2<T>;
3209 The S2<T> declaration is actually invalid; it is a
3210 full-specialization. Of course,
3212 template <class U>
3213 struct S2<T (*)(U)>;
3215 or some such would have been OK. */
3216 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3217 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3218 memset (tpd.parms, 0, sizeof (int) * ntparms);
3220 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3221 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3222 for (i = 0; i < nargs; ++i)
3224 tpd.current_arg = i;
3225 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3226 &mark_template_parm,
3227 &tpd,
3228 NULL);
3230 for (i = 0; i < ntparms; ++i)
3231 if (tpd.parms[i] == 0)
3233 /* One of the template parms was not used in the
3234 specialization. */
3235 if (!did_error_intro)
3237 error ("template parameters not used in partial specialization:");
3238 did_error_intro = 1;
3241 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3244 /* [temp.class.spec]
3246 The argument list of the specialization shall not be identical to
3247 the implicit argument list of the primary template. */
3248 if (comp_template_args
3249 (inner_args,
3250 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3251 (maintmpl)))))
3252 error ("partial specialization %qT does not specialize any template arguments", type);
3254 /* [temp.class.spec]
3256 A partially specialized non-type argument expression shall not
3257 involve template parameters of the partial specialization except
3258 when the argument expression is a simple identifier.
3260 The type of a template parameter corresponding to a specialized
3261 non-type argument shall not be dependent on a parameter of the
3262 specialization.
3264 Also, we verify that pack expansions only occur at the
3265 end of the argument list. */
3266 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3267 tpd2.parms = 0;
3268 for (i = 0; i < nargs; ++i)
3270 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3271 tree arg = TREE_VEC_ELT (inner_args, i);
3272 tree packed_args = NULL_TREE;
3273 int j, len = 1;
3275 if (ARGUMENT_PACK_P (arg))
3277 /* Extract the arguments from the argument pack. We'll be
3278 iterating over these in the following loop. */
3279 packed_args = ARGUMENT_PACK_ARGS (arg);
3280 len = TREE_VEC_LENGTH (packed_args);
3283 for (j = 0; j < len; j++)
3285 if (packed_args)
3286 /* Get the Jth argument in the parameter pack. */
3287 arg = TREE_VEC_ELT (packed_args, j);
3289 if (PACK_EXPANSION_P (arg))
3291 /* Pack expansions must come at the end of the
3292 argument list. */
3293 if ((packed_args && j < len - 1)
3294 || (!packed_args && i < nargs - 1))
3296 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3297 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3298 else
3299 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3303 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3304 /* We only care about the pattern. */
3305 arg = PACK_EXPANSION_PATTERN (arg);
3307 if (/* These first two lines are the `non-type' bit. */
3308 !TYPE_P (arg)
3309 && TREE_CODE (arg) != TEMPLATE_DECL
3310 /* This next line is the `argument expression is not just a
3311 simple identifier' condition and also the `specialized
3312 non-type argument' bit. */
3313 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3315 if ((!packed_args && tpd.arg_uses_template_parms[i])
3316 || (packed_args && uses_template_parms (arg)))
3317 error ("template argument %qE involves template parameter(s)",
3318 arg);
3319 else
3321 /* Look at the corresponding template parameter,
3322 marking which template parameters its type depends
3323 upon. */
3324 tree type = TREE_TYPE (parm);
3326 if (!tpd2.parms)
3328 /* We haven't yet initialized TPD2. Do so now. */
3329 tpd2.arg_uses_template_parms
3330 = (int *) alloca (sizeof (int) * nargs);
3331 /* The number of parameters here is the number in the
3332 main template, which, as checked in the assertion
3333 above, is NARGS. */
3334 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3335 tpd2.level =
3336 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3339 /* Mark the template parameters. But this time, we're
3340 looking for the template parameters of the main
3341 template, not in the specialization. */
3342 tpd2.current_arg = i;
3343 tpd2.arg_uses_template_parms[i] = 0;
3344 memset (tpd2.parms, 0, sizeof (int) * nargs);
3345 for_each_template_parm (type,
3346 &mark_template_parm,
3347 &tpd2,
3348 NULL);
3350 if (tpd2.arg_uses_template_parms [i])
3352 /* The type depended on some template parameters.
3353 If they are fully specialized in the
3354 specialization, that's OK. */
3355 int j;
3356 for (j = 0; j < nargs; ++j)
3357 if (tpd2.parms[j] != 0
3358 && tpd.arg_uses_template_parms [j])
3360 error ("type %qT of template argument %qE depends "
3361 "on template parameter(s)",
3362 type,
3363 arg);
3364 break;
3372 if (retrieve_specialization (maintmpl, specargs,
3373 /*class_specializations_p=*/true))
3374 /* We've already got this specialization. */
3375 return decl;
3377 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3378 = tree_cons (specargs, inner_parms,
3379 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3380 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3381 return decl;
3384 /* Check that a template declaration's use of default arguments is not
3385 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
3386 nonzero if DECL is the thing declared by a primary template.
3387 IS_PARTIAL is nonzero if DECL is a partial specialization.
3390 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3391 declaration (but not a definition); 1 indicates a declaration, 2
3392 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3393 emitted for extraneous default arguments.
3395 Returns TRUE if there were no errors found, FALSE otherwise. */
3397 bool
3398 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3399 int is_partial, int is_friend_decl)
3401 const char *msg;
3402 int last_level_to_check;
3403 tree parm_level;
3404 bool no_errors = true;
3406 /* [temp.param]
3408 A default template-argument shall not be specified in a
3409 function template declaration or a function template definition, nor
3410 in the template-parameter-list of the definition of a member of a
3411 class template. */
3413 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3414 /* You can't have a function template declaration in a local
3415 scope, nor you can you define a member of a class template in a
3416 local scope. */
3417 return true;
3419 if (current_class_type
3420 && !TYPE_BEING_DEFINED (current_class_type)
3421 && DECL_LANG_SPECIFIC (decl)
3422 /* If this is either a friend defined in the scope of the class
3423 or a member function. */
3424 && (DECL_FUNCTION_MEMBER_P (decl)
3425 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3426 : DECL_FRIEND_CONTEXT (decl)
3427 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3428 : false)
3429 /* And, if it was a member function, it really was defined in
3430 the scope of the class. */
3431 && (!DECL_FUNCTION_MEMBER_P (decl)
3432 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3433 /* We already checked these parameters when the template was
3434 declared, so there's no need to do it again now. This function
3435 was defined in class scope, but we're processing it's body now
3436 that the class is complete. */
3437 return true;
3439 /* Core issue 226 (C++0x only): the following only applies to class
3440 templates. */
3441 if (!flag_cpp0x || TREE_CODE (decl) != FUNCTION_DECL)
3443 /* [temp.param]
3445 If a template-parameter has a default template-argument, all
3446 subsequent template-parameters shall have a default
3447 template-argument supplied. */
3448 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3450 tree inner_parms = TREE_VALUE (parm_level);
3451 int ntparms = TREE_VEC_LENGTH (inner_parms);
3452 int seen_def_arg_p = 0;
3453 int i;
3455 for (i = 0; i < ntparms; ++i)
3457 tree parm = TREE_VEC_ELT (inner_parms, i);
3459 if (parm == error_mark_node)
3460 continue;
3462 if (TREE_PURPOSE (parm))
3463 seen_def_arg_p = 1;
3464 else if (seen_def_arg_p)
3466 error ("no default argument for %qD", TREE_VALUE (parm));
3467 /* For better subsequent error-recovery, we indicate that
3468 there should have been a default argument. */
3469 TREE_PURPOSE (parm) = error_mark_node;
3470 no_errors = false;
3476 if ((!flag_cpp0x && TREE_CODE (decl) != TYPE_DECL)
3477 || is_partial
3478 || !is_primary
3479 || is_friend_decl)
3480 /* For an ordinary class template, default template arguments are
3481 allowed at the innermost level, e.g.:
3482 template <class T = int>
3483 struct S {};
3484 but, in a partial specialization, they're not allowed even
3485 there, as we have in [temp.class.spec]:
3487 The template parameter list of a specialization shall not
3488 contain default template argument values.
3490 So, for a partial specialization, or for a function template
3491 (in C++98/C++03), we look at all of them. */
3493 else
3494 /* But, for a primary class template that is not a partial
3495 specialization we look at all template parameters except the
3496 innermost ones. */
3497 parms = TREE_CHAIN (parms);
3499 /* Figure out what error message to issue. */
3500 if (is_friend_decl == 2)
3501 msg = "default template arguments may not be used in function template friend re-declaration";
3502 else if (is_friend_decl)
3503 msg = "default template arguments may not be used in function template friend declarations";
3504 else if (TREE_CODE (decl) == FUNCTION_DECL && !flag_cpp0x)
3505 msg = "default template arguments may not be used in function templates";
3506 else if (is_partial)
3507 msg = "default template arguments may not be used in partial specializations";
3508 else
3509 msg = "default argument for template parameter for class enclosing %qD";
3511 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3512 /* If we're inside a class definition, there's no need to
3513 examine the parameters to the class itself. On the one
3514 hand, they will be checked when the class is defined, and,
3515 on the other, default arguments are valid in things like:
3516 template <class T = double>
3517 struct S { template <class U> void f(U); };
3518 Here the default argument for `S' has no bearing on the
3519 declaration of `f'. */
3520 last_level_to_check = template_class_depth (current_class_type) + 1;
3521 else
3522 /* Check everything. */
3523 last_level_to_check = 0;
3525 for (parm_level = parms;
3526 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3527 parm_level = TREE_CHAIN (parm_level))
3529 tree inner_parms = TREE_VALUE (parm_level);
3530 int i;
3531 int ntparms;
3533 ntparms = TREE_VEC_LENGTH (inner_parms);
3534 for (i = 0; i < ntparms; ++i)
3536 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3537 continue;
3539 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3541 if (msg)
3543 no_errors = false;
3544 if (is_friend_decl == 2)
3545 return no_errors;
3547 error (msg, decl);
3548 msg = 0;
3551 /* Clear out the default argument so that we are not
3552 confused later. */
3553 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3557 /* At this point, if we're still interested in issuing messages,
3558 they must apply to classes surrounding the object declared. */
3559 if (msg)
3560 msg = "default argument for template parameter for class enclosing %qD";
3563 return no_errors;
3566 /* Worker for push_template_decl_real, called via
3567 for_each_template_parm. DATA is really an int, indicating the
3568 level of the parameters we are interested in. If T is a template
3569 parameter of that level, return nonzero. */
3571 static int
3572 template_parm_this_level_p (tree t, void* data)
3574 int this_level = *(int *)data;
3575 int level;
3577 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3578 level = TEMPLATE_PARM_LEVEL (t);
3579 else
3580 level = TEMPLATE_TYPE_LEVEL (t);
3581 return level == this_level;
3584 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3585 parameters given by current_template_args, or reuses a
3586 previously existing one, if appropriate. Returns the DECL, or an
3587 equivalent one, if it is replaced via a call to duplicate_decls.
3589 If IS_FRIEND is true, DECL is a friend declaration. */
3591 tree
3592 push_template_decl_real (tree decl, bool is_friend)
3594 tree tmpl;
3595 tree args;
3596 tree info;
3597 tree ctx;
3598 int primary;
3599 int is_partial;
3600 int new_template_p = 0;
3601 /* True if the template is a member template, in the sense of
3602 [temp.mem]. */
3603 bool member_template_p = false;
3605 if (decl == error_mark_node)
3606 return decl;
3608 /* See if this is a partial specialization. */
3609 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3610 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3611 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3613 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3614 is_friend = true;
3616 if (is_friend)
3617 /* For a friend, we want the context of the friend function, not
3618 the type of which it is a friend. */
3619 ctx = DECL_CONTEXT (decl);
3620 else if (CP_DECL_CONTEXT (decl)
3621 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3622 /* In the case of a virtual function, we want the class in which
3623 it is defined. */
3624 ctx = CP_DECL_CONTEXT (decl);
3625 else
3626 /* Otherwise, if we're currently defining some class, the DECL
3627 is assumed to be a member of the class. */
3628 ctx = current_scope ();
3630 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3631 ctx = NULL_TREE;
3633 if (!DECL_CONTEXT (decl))
3634 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3636 /* See if this is a primary template. */
3637 if (is_friend && ctx)
3638 /* A friend template that specifies a class context, i.e.
3639 template <typename T> friend void A<T>::f();
3640 is not primary. */
3641 primary = 0;
3642 else
3643 primary = template_parm_scope_p ();
3645 if (primary)
3647 if (DECL_CLASS_SCOPE_P (decl))
3648 member_template_p = true;
3649 if (TREE_CODE (decl) == TYPE_DECL
3650 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3651 error ("template class without a name");
3652 else if (TREE_CODE (decl) == FUNCTION_DECL)
3654 if (DECL_DESTRUCTOR_P (decl))
3656 /* [temp.mem]
3658 A destructor shall not be a member template. */
3659 error ("destructor %qD declared as member template", decl);
3660 return error_mark_node;
3662 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3663 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3664 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3665 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3666 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3667 == void_list_node)))
3669 /* [basic.stc.dynamic.allocation]
3671 An allocation function can be a function
3672 template. ... Template allocation functions shall
3673 have two or more parameters. */
3674 error ("invalid template declaration of %qD", decl);
3675 return error_mark_node;
3678 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3679 && CLASS_TYPE_P (TREE_TYPE (decl)))
3680 /* OK */;
3681 else
3683 error ("template declaration of %q#D", decl);
3684 return error_mark_node;
3688 /* Check to see that the rules regarding the use of default
3689 arguments are not being violated. */
3690 check_default_tmpl_args (decl, current_template_parms,
3691 primary, is_partial, /*is_friend_decl=*/0);
3693 /* Ensure that there are no parameter packs in the type of this
3694 declaration that have not been expanded. */
3695 check_for_bare_parameter_packs (TREE_TYPE (decl));
3697 if (is_partial)
3698 return process_partial_specialization (decl);
3700 /* A primary class template can only have one parameter pack, at the
3701 end of the template parameter list. */
3702 if (primary && TREE_CODE (decl) == TYPE_DECL)
3704 tree inner_parms
3705 = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3706 int i, len = TREE_VEC_LENGTH (inner_parms);
3707 for (i = 0; i < len - 1; i++)
3709 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
3711 if (template_parameter_pack_p (parm))
3713 if (TREE_CODE (parm) == PARM_DECL)
3714 error ("parameter pack %qE must be at the end of the"
3715 " template parameter list", parm);
3716 else
3717 error ("parameter pack %qT must be at the end of the"
3718 " template parameter list", TREE_TYPE (parm));
3723 args = current_template_args ();
3725 if (!ctx
3726 || TREE_CODE (ctx) == FUNCTION_DECL
3727 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3728 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3730 if (DECL_LANG_SPECIFIC (decl)
3731 && DECL_TEMPLATE_INFO (decl)
3732 && DECL_TI_TEMPLATE (decl))
3733 tmpl = DECL_TI_TEMPLATE (decl);
3734 /* If DECL is a TYPE_DECL for a class-template, then there won't
3735 be DECL_LANG_SPECIFIC. The information equivalent to
3736 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3737 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3738 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3739 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3741 /* Since a template declaration already existed for this
3742 class-type, we must be redeclaring it here. Make sure
3743 that the redeclaration is valid. */
3744 redeclare_class_template (TREE_TYPE (decl),
3745 current_template_parms);
3746 /* We don't need to create a new TEMPLATE_DECL; just use the
3747 one we already had. */
3748 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3750 else
3752 tmpl = build_template_decl (decl, current_template_parms,
3753 member_template_p);
3754 new_template_p = 1;
3756 if (DECL_LANG_SPECIFIC (decl)
3757 && DECL_TEMPLATE_SPECIALIZATION (decl))
3759 /* A specialization of a member template of a template
3760 class. */
3761 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3762 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3763 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3767 else
3769 tree a, t, current, parms;
3770 int i;
3772 if (TREE_CODE (decl) == TYPE_DECL)
3774 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3775 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3776 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3777 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3778 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3779 else
3781 error ("%qD does not declare a template type", decl);
3782 return decl;
3785 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3787 error ("template definition of non-template %q#D", decl);
3788 return decl;
3790 else
3791 tmpl = DECL_TI_TEMPLATE (decl);
3793 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3794 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3795 && DECL_TEMPLATE_SPECIALIZATION (decl)
3796 && DECL_MEMBER_TEMPLATE_P (tmpl))
3798 tree new_tmpl;
3800 /* The declaration is a specialization of a member
3801 template, declared outside the class. Therefore, the
3802 innermost template arguments will be NULL, so we
3803 replace them with the arguments determined by the
3804 earlier call to check_explicit_specialization. */
3805 args = DECL_TI_ARGS (decl);
3807 new_tmpl
3808 = build_template_decl (decl, current_template_parms,
3809 member_template_p);
3810 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3811 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3812 DECL_TI_TEMPLATE (decl) = new_tmpl;
3813 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3814 DECL_TEMPLATE_INFO (new_tmpl)
3815 = tree_cons (tmpl, args, NULL_TREE);
3817 register_specialization (new_tmpl,
3818 most_general_template (tmpl),
3819 args,
3820 is_friend);
3821 return decl;
3824 /* Make sure the template headers we got make sense. */
3826 parms = DECL_TEMPLATE_PARMS (tmpl);
3827 i = TMPL_PARMS_DEPTH (parms);
3828 if (TMPL_ARGS_DEPTH (args) != i)
3830 error ("expected %d levels of template parms for %q#D, got %d",
3831 i, decl, TMPL_ARGS_DEPTH (args));
3833 else
3834 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3836 a = TMPL_ARGS_LEVEL (args, i);
3837 t = INNERMOST_TEMPLATE_PARMS (parms);
3839 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3841 if (current == decl)
3842 error ("got %d template parameters for %q#D",
3843 TREE_VEC_LENGTH (a), decl);
3844 else
3845 error ("got %d template parameters for %q#T",
3846 TREE_VEC_LENGTH (a), current);
3847 error (" but %d required", TREE_VEC_LENGTH (t));
3848 return error_mark_node;
3851 /* Perhaps we should also check that the parms are used in the
3852 appropriate qualifying scopes in the declarator? */
3854 if (current == decl)
3855 current = ctx;
3856 else
3857 current = TYPE_CONTEXT (current);
3861 DECL_TEMPLATE_RESULT (tmpl) = decl;
3862 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3864 /* Push template declarations for global functions and types. Note
3865 that we do not try to push a global template friend declared in a
3866 template class; such a thing may well depend on the template
3867 parameters of the class. */
3868 if (new_template_p && !ctx
3869 && !(is_friend && template_class_depth (current_class_type) > 0))
3871 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3872 if (tmpl == error_mark_node)
3873 return error_mark_node;
3875 /* Hide template friend classes that haven't been declared yet. */
3876 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3878 DECL_ANTICIPATED (tmpl) = 1;
3879 DECL_FRIEND_P (tmpl) = 1;
3883 if (primary)
3885 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3886 if (DECL_CONV_FN_P (tmpl))
3888 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3890 /* It is a conversion operator. See if the type converted to
3891 depends on innermost template operands. */
3893 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3894 depth))
3895 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3899 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3900 back to its most general template. If TMPL is a specialization,
3901 ARGS may only have the innermost set of arguments. Add the missing
3902 argument levels if necessary. */
3903 if (DECL_TEMPLATE_INFO (tmpl))
3904 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3906 info = tree_cons (tmpl, args, NULL_TREE);
3908 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3909 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3910 else if (DECL_LANG_SPECIFIC (decl))
3911 DECL_TEMPLATE_INFO (decl) = info;
3913 return DECL_TEMPLATE_RESULT (tmpl);
3916 tree
3917 push_template_decl (tree decl)
3919 return push_template_decl_real (decl, false);
3922 /* Called when a class template TYPE is redeclared with the indicated
3923 template PARMS, e.g.:
3925 template <class T> struct S;
3926 template <class T> struct S {}; */
3928 bool
3929 redeclare_class_template (tree type, tree parms)
3931 tree tmpl;
3932 tree tmpl_parms;
3933 int i;
3935 if (!TYPE_TEMPLATE_INFO (type))
3937 error ("%qT is not a template type", type);
3938 return false;
3941 tmpl = TYPE_TI_TEMPLATE (type);
3942 if (!PRIMARY_TEMPLATE_P (tmpl))
3943 /* The type is nested in some template class. Nothing to worry
3944 about here; there are no new template parameters for the nested
3945 type. */
3946 return true;
3948 if (!parms)
3950 error ("template specifiers not specified in declaration of %qD",
3951 tmpl);
3952 return false;
3955 parms = INNERMOST_TEMPLATE_PARMS (parms);
3956 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3958 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3960 error ("previous declaration %q+D", tmpl);
3961 error ("used %d template parameter(s) instead of %d",
3962 TREE_VEC_LENGTH (tmpl_parms),
3963 TREE_VEC_LENGTH (parms));
3964 return false;
3967 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3969 tree tmpl_parm;
3970 tree parm;
3971 tree tmpl_default;
3972 tree parm_default;
3974 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3975 || TREE_VEC_ELT (parms, i) == error_mark_node)
3976 continue;
3978 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3979 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3980 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3981 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3983 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3984 TEMPLATE_DECL. */
3985 if (tmpl_parm != error_mark_node
3986 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3987 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3988 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
3990 error ("template parameter %q+#D", tmpl_parm);
3991 error ("redeclared here as %q#D", parm);
3992 return false;
3995 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3997 /* We have in [temp.param]:
3999 A template-parameter may not be given default arguments
4000 by two different declarations in the same scope. */
4001 error ("redefinition of default argument for %q#D", parm);
4002 error ("%J original definition appeared here", tmpl_parm);
4003 return false;
4006 if (parm_default != NULL_TREE)
4007 /* Update the previous template parameters (which are the ones
4008 that will really count) with the new default value. */
4009 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4010 else if (tmpl_default != NULL_TREE)
4011 /* Update the new parameters, too; they'll be used as the
4012 parameters for any members. */
4013 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4016 return true;
4019 /* Simplify EXPR if it is a non-dependent expression. Returns the
4020 (possibly simplified) expression. */
4022 tree
4023 fold_non_dependent_expr (tree expr)
4025 if (expr == NULL_TREE)
4026 return NULL_TREE;
4028 /* If we're in a template, but EXPR isn't value dependent, simplify
4029 it. We're supposed to treat:
4031 template <typename T> void f(T[1 + 1]);
4032 template <typename T> void f(T[2]);
4034 as two declarations of the same function, for example. */
4035 if (processing_template_decl
4036 && !type_dependent_expression_p (expr)
4037 && !value_dependent_expression_p (expr))
4039 HOST_WIDE_INT saved_processing_template_decl;
4041 saved_processing_template_decl = processing_template_decl;
4042 processing_template_decl = 0;
4043 expr = tsubst_copy_and_build (expr,
4044 /*args=*/NULL_TREE,
4045 tf_error,
4046 /*in_decl=*/NULL_TREE,
4047 /*function_p=*/false,
4048 /*integral_constant_expression_p=*/true);
4049 processing_template_decl = saved_processing_template_decl;
4051 return expr;
4054 /* EXPR is an expression which is used in a constant-expression context.
4055 For instance, it could be a VAR_DECL with a constant initializer.
4056 Extract the innest constant expression.
4058 This is basically a more powerful version of
4059 integral_constant_value, which can be used also in templates where
4060 initializers can maintain a syntactic rather than semantic form
4061 (even if they are non-dependent, for access-checking purposes). */
4063 static tree
4064 fold_decl_constant_value (tree expr)
4066 tree const_expr = expr;
4069 expr = fold_non_dependent_expr (const_expr);
4070 const_expr = integral_constant_value (expr);
4072 while (expr != const_expr);
4074 return expr;
4077 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4078 must be a function or a pointer-to-function type, as specified
4079 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4080 and check that the resulting function has external linkage. */
4082 static tree
4083 convert_nontype_argument_function (tree type, tree expr)
4085 tree fns = expr;
4086 tree fn, fn_no_ptr;
4088 fn = instantiate_type (type, fns, tf_none);
4089 if (fn == error_mark_node)
4090 return error_mark_node;
4092 fn_no_ptr = fn;
4093 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4094 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4095 if (TREE_CODE (fn_no_ptr) == BASELINK)
4096 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4098 /* [temp.arg.nontype]/1
4100 A template-argument for a non-type, non-template template-parameter
4101 shall be one of:
4102 [...]
4103 -- the address of an object or function with external linkage. */
4104 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4106 error ("%qE is not a valid template argument for type %qT "
4107 "because function %qD has not external linkage",
4108 expr, type, fn_no_ptr);
4109 return NULL_TREE;
4112 return fn;
4115 /* Attempt to convert the non-type template parameter EXPR to the
4116 indicated TYPE. If the conversion is successful, return the
4117 converted value. If the conversion is unsuccessful, return
4118 NULL_TREE if we issued an error message, or error_mark_node if we
4119 did not. We issue error messages for out-and-out bad template
4120 parameters, but not simply because the conversion failed, since we
4121 might be just trying to do argument deduction. Both TYPE and EXPR
4122 must be non-dependent.
4124 The conversion follows the special rules described in
4125 [temp.arg.nontype], and it is much more strict than an implicit
4126 conversion.
4128 This function is called twice for each template argument (see
4129 lookup_template_class for a more accurate description of this
4130 problem). This means that we need to handle expressions which
4131 are not valid in a C++ source, but can be created from the
4132 first call (for instance, casts to perform conversions). These
4133 hacks can go away after we fix the double coercion problem. */
4135 static tree
4136 convert_nontype_argument (tree type, tree expr)
4138 tree expr_type;
4140 /* Detect immediately string literals as invalid non-type argument.
4141 This special-case is not needed for correctness (we would easily
4142 catch this later), but only to provide better diagnostic for this
4143 common user mistake. As suggested by DR 100, we do not mention
4144 linkage issues in the diagnostic as this is not the point. */
4145 if (TREE_CODE (expr) == STRING_CST)
4147 error ("%qE is not a valid template argument for type %qT "
4148 "because string literals can never be used in this context",
4149 expr, type);
4150 return NULL_TREE;
4153 /* If we are in a template, EXPR may be non-dependent, but still
4154 have a syntactic, rather than semantic, form. For example, EXPR
4155 might be a SCOPE_REF, rather than the VAR_DECL to which the
4156 SCOPE_REF refers. Preserving the qualifying scope is necessary
4157 so that access checking can be performed when the template is
4158 instantiated -- but here we need the resolved form so that we can
4159 convert the argument. */
4160 expr = fold_non_dependent_expr (expr);
4161 if (error_operand_p (expr))
4162 return error_mark_node;
4163 expr_type = TREE_TYPE (expr);
4165 /* HACK: Due to double coercion, we can get a
4166 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4167 which is the tree that we built on the first call (see
4168 below when coercing to reference to object or to reference to
4169 function). We just strip everything and get to the arg.
4170 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4171 for examples. */
4172 if (TREE_CODE (expr) == NOP_EXPR)
4174 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4176 /* ??? Maybe we could use convert_from_reference here, but we
4177 would need to relax its constraints because the NOP_EXPR
4178 could actually change the type to something more cv-qualified,
4179 and this is not folded by convert_from_reference. */
4180 tree addr = TREE_OPERAND (expr, 0);
4181 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4182 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4183 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4184 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4185 (TREE_TYPE (expr_type),
4186 TREE_TYPE (TREE_TYPE (addr))));
4188 expr = TREE_OPERAND (addr, 0);
4189 expr_type = TREE_TYPE (expr);
4192 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4193 parameter is a pointer to object, through decay and
4194 qualification conversion. Let's strip everything. */
4195 else if (TYPE_PTROBV_P (type))
4197 STRIP_NOPS (expr);
4198 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4199 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4200 /* Skip the ADDR_EXPR only if it is part of the decay for
4201 an array. Otherwise, it is part of the original argument
4202 in the source code. */
4203 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4204 expr = TREE_OPERAND (expr, 0);
4205 expr_type = TREE_TYPE (expr);
4209 /* [temp.arg.nontype]/5, bullet 1
4211 For a non-type template-parameter of integral or enumeration type,
4212 integral promotions (_conv.prom_) and integral conversions
4213 (_conv.integral_) are applied. */
4214 if (INTEGRAL_TYPE_P (type))
4216 if (!INTEGRAL_TYPE_P (expr_type))
4217 return error_mark_node;
4219 expr = fold_decl_constant_value (expr);
4220 /* Notice that there are constant expressions like '4 % 0' which
4221 do not fold into integer constants. */
4222 if (TREE_CODE (expr) != INTEGER_CST)
4224 error ("%qE is not a valid template argument for type %qT "
4225 "because it is a non-constant expression", expr, type);
4226 return NULL_TREE;
4229 /* At this point, an implicit conversion does what we want,
4230 because we already know that the expression is of integral
4231 type. */
4232 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4233 if (expr == error_mark_node)
4234 return error_mark_node;
4236 /* Conversion was allowed: fold it to a bare integer constant. */
4237 expr = fold (expr);
4239 /* [temp.arg.nontype]/5, bullet 2
4241 For a non-type template-parameter of type pointer to object,
4242 qualification conversions (_conv.qual_) and the array-to-pointer
4243 conversion (_conv.array_) are applied. */
4244 else if (TYPE_PTROBV_P (type))
4246 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4248 A template-argument for a non-type, non-template template-parameter
4249 shall be one of: [...]
4251 -- the name of a non-type template-parameter;
4252 -- the address of an object or function with external linkage, [...]
4253 expressed as "& id-expression" where the & is optional if the name
4254 refers to a function or array, or if the corresponding
4255 template-parameter is a reference.
4257 Here, we do not care about functions, as they are invalid anyway
4258 for a parameter of type pointer-to-object. */
4260 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4261 /* Non-type template parameters are OK. */
4263 else if (TREE_CODE (expr) != ADDR_EXPR
4264 && TREE_CODE (expr_type) != ARRAY_TYPE)
4266 if (TREE_CODE (expr) == VAR_DECL)
4268 error ("%qD is not a valid template argument "
4269 "because %qD is a variable, not the address of "
4270 "a variable",
4271 expr, expr);
4272 return NULL_TREE;
4274 /* Other values, like integer constants, might be valid
4275 non-type arguments of some other type. */
4276 return error_mark_node;
4278 else
4280 tree decl;
4282 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4283 ? TREE_OPERAND (expr, 0) : expr);
4284 if (TREE_CODE (decl) != VAR_DECL)
4286 error ("%qE is not a valid template argument of type %qT "
4287 "because %qE is not a variable",
4288 expr, type, decl);
4289 return NULL_TREE;
4291 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4293 error ("%qE is not a valid template argument of type %qT "
4294 "because %qD does not have external linkage",
4295 expr, type, decl);
4296 return NULL_TREE;
4300 expr = decay_conversion (expr);
4301 if (expr == error_mark_node)
4302 return error_mark_node;
4304 expr = perform_qualification_conversions (type, expr);
4305 if (expr == error_mark_node)
4306 return error_mark_node;
4308 /* [temp.arg.nontype]/5, bullet 3
4310 For a non-type template-parameter of type reference to object, no
4311 conversions apply. The type referred to by the reference may be more
4312 cv-qualified than the (otherwise identical) type of the
4313 template-argument. The template-parameter is bound directly to the
4314 template-argument, which must be an lvalue. */
4315 else if (TYPE_REF_OBJ_P (type))
4317 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4318 expr_type))
4319 return error_mark_node;
4321 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4323 error ("%qE is not a valid template argument for type %qT "
4324 "because of conflicts in cv-qualification", expr, type);
4325 return NULL_TREE;
4328 if (!real_lvalue_p (expr))
4330 error ("%qE is not a valid template argument for type %qT "
4331 "because it is not an lvalue", expr, type);
4332 return NULL_TREE;
4335 /* [temp.arg.nontype]/1
4337 A template-argument for a non-type, non-template template-parameter
4338 shall be one of: [...]
4340 -- the address of an object or function with external linkage. */
4341 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4343 error ("%qE is not a valid template argument for type %qT "
4344 "because object %qD has not external linkage",
4345 expr, type, expr);
4346 return NULL_TREE;
4349 expr = build_nop (type, build_address (expr));
4351 /* [temp.arg.nontype]/5, bullet 4
4353 For a non-type template-parameter of type pointer to function, only
4354 the function-to-pointer conversion (_conv.func_) is applied. If the
4355 template-argument represents a set of overloaded functions (or a
4356 pointer to such), the matching function is selected from the set
4357 (_over.over_). */
4358 else if (TYPE_PTRFN_P (type))
4360 /* If the argument is a template-id, we might not have enough
4361 context information to decay the pointer. */
4362 if (!type_unknown_p (expr_type))
4364 expr = decay_conversion (expr);
4365 if (expr == error_mark_node)
4366 return error_mark_node;
4369 expr = convert_nontype_argument_function (type, expr);
4370 if (!expr || expr == error_mark_node)
4371 return expr;
4373 /* [temp.arg.nontype]/5, bullet 5
4375 For a non-type template-parameter of type reference to function, no
4376 conversions apply. If the template-argument represents a set of
4377 overloaded functions, the matching function is selected from the set
4378 (_over.over_). */
4379 else if (TYPE_REFFN_P (type))
4381 if (TREE_CODE (expr) == ADDR_EXPR)
4383 error ("%qE is not a valid template argument for type %qT "
4384 "because it is a pointer", expr, type);
4385 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
4386 return NULL_TREE;
4389 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4390 if (!expr || expr == error_mark_node)
4391 return expr;
4393 expr = build_nop (type, build_address (expr));
4395 /* [temp.arg.nontype]/5, bullet 6
4397 For a non-type template-parameter of type pointer to member function,
4398 no conversions apply. If the template-argument represents a set of
4399 overloaded member functions, the matching member function is selected
4400 from the set (_over.over_). */
4401 else if (TYPE_PTRMEMFUNC_P (type))
4403 expr = instantiate_type (type, expr, tf_none);
4404 if (expr == error_mark_node)
4405 return error_mark_node;
4407 /* There is no way to disable standard conversions in
4408 resolve_address_of_overloaded_function (called by
4409 instantiate_type). It is possible that the call succeeded by
4410 converting &B::I to &D::I (where B is a base of D), so we need
4411 to reject this conversion here.
4413 Actually, even if there was a way to disable standard conversions,
4414 it would still be better to reject them here so that we can
4415 provide a superior diagnostic. */
4416 if (!same_type_p (TREE_TYPE (expr), type))
4418 /* Make sure we are just one standard conversion off. */
4419 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4420 error ("%qE is not a valid template argument for type %qT "
4421 "because it is of type %qT", expr, type,
4422 TREE_TYPE (expr));
4423 inform ("standard conversions are not allowed in this context");
4424 return NULL_TREE;
4427 /* [temp.arg.nontype]/5, bullet 7
4429 For a non-type template-parameter of type pointer to data member,
4430 qualification conversions (_conv.qual_) are applied. */
4431 else if (TYPE_PTRMEM_P (type))
4433 expr = perform_qualification_conversions (type, expr);
4434 if (expr == error_mark_node)
4435 return expr;
4437 /* A template non-type parameter must be one of the above. */
4438 else
4439 gcc_unreachable ();
4441 /* Sanity check: did we actually convert the argument to the
4442 right type? */
4443 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4444 return expr;
4448 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4449 template template parameters. Both PARM_PARMS and ARG_PARMS are
4450 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4451 or PARM_DECL.
4453 Consider the example:
4454 template <class T> class A;
4455 template<template <class U> class TT> class B;
4457 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4458 the parameters to A, and OUTER_ARGS contains A. */
4460 static int
4461 coerce_template_template_parms (tree parm_parms,
4462 tree arg_parms,
4463 tsubst_flags_t complain,
4464 tree in_decl,
4465 tree outer_args)
4467 int nparms, nargs, i;
4468 tree parm, arg;
4470 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4471 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4473 nparms = TREE_VEC_LENGTH (parm_parms);
4474 nargs = TREE_VEC_LENGTH (arg_parms);
4476 if (nargs != nparms)
4477 return 0;
4479 for (i = 0; i < nparms; ++i)
4481 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4482 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4483 continue;
4485 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4486 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4488 if (arg == NULL_TREE || arg == error_mark_node
4489 || parm == NULL_TREE || parm == error_mark_node)
4490 return 0;
4492 if (TREE_CODE (arg) != TREE_CODE (parm))
4493 return 0;
4495 switch (TREE_CODE (parm))
4497 case TEMPLATE_DECL:
4498 /* We encounter instantiations of templates like
4499 template <template <template <class> class> class TT>
4500 class C; */
4502 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4503 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4505 if (!coerce_template_template_parms
4506 (parmparm, argparm, complain, in_decl, outer_args))
4507 return 0;
4509 /* Fall through. */
4511 case TYPE_DECL:
4512 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))
4513 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)))
4514 /* One is a parameter pack, the other is not. */
4515 return 0;
4516 break;
4518 case PARM_DECL:
4519 /* The tsubst call is used to handle cases such as
4521 template <int> class C {};
4522 template <class T, template <T> class TT> class D {};
4523 D<int, C> d;
4525 i.e. the parameter list of TT depends on earlier parameters. */
4526 if (!dependent_type_p (TREE_TYPE (arg))
4527 && !same_type_p
4528 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4529 TREE_TYPE (arg)))
4530 return 0;
4532 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4533 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)))
4534 /* One is a parameter pack, the other is not. */
4535 return 0;
4536 break;
4538 default:
4539 gcc_unreachable ();
4542 return 1;
4545 /* Convert the indicated template ARG as necessary to match the
4546 indicated template PARM. Returns the converted ARG, or
4547 error_mark_node if the conversion was unsuccessful. Error and
4548 warning messages are issued under control of COMPLAIN. This
4549 conversion is for the Ith parameter in the parameter list. ARGS is
4550 the full set of template arguments deduced so far. */
4552 static tree
4553 convert_template_argument (tree parm,
4554 tree arg,
4555 tree args,
4556 tsubst_flags_t complain,
4557 int i,
4558 tree in_decl)
4560 tree val;
4561 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4562 tree check_arg = arg;
4564 if (TREE_CODE (arg) == TREE_LIST
4565 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4567 /* The template argument was the name of some
4568 member function. That's usually
4569 invalid, but static members are OK. In any
4570 case, grab the underlying fields/functions
4571 and issue an error later if required. */
4572 arg = TREE_VALUE (arg);
4573 TREE_TYPE (arg) = unknown_type_node;
4576 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4577 requires_type = (TREE_CODE (parm) == TYPE_DECL
4578 || requires_tmpl_type);
4580 /* When determining whether a argument pack expansion is a template,
4581 look at the pattern. */
4582 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4583 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4585 is_tmpl_type =
4586 ((TREE_CODE (check_arg) == TEMPLATE_DECL
4587 && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg)) == TYPE_DECL)
4588 || TREE_CODE (check_arg) == TEMPLATE_TEMPLATE_PARM
4589 || TREE_CODE (check_arg) == UNBOUND_CLASS_TEMPLATE);
4591 if (is_tmpl_type
4592 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4593 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4594 arg = TYPE_STUB_DECL (arg);
4596 is_type = TYPE_P (arg) || is_tmpl_type;
4598 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4599 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4601 pedwarn ("to refer to a type member of a template parameter, "
4602 "use %<typename %E%>", arg);
4604 arg = make_typename_type (TREE_OPERAND (arg, 0),
4605 TREE_OPERAND (arg, 1),
4606 typename_type,
4607 complain & tf_error);
4608 is_type = 1;
4610 if (is_type != requires_type)
4612 if (in_decl)
4614 if (complain & tf_error)
4616 error ("type/value mismatch at argument %d in template "
4617 "parameter list for %qD",
4618 i + 1, in_decl);
4619 if (is_type)
4620 error (" expected a constant of type %qT, got %qT",
4621 TREE_TYPE (parm),
4622 (is_tmpl_type ? DECL_NAME (arg) : arg));
4623 else if (requires_tmpl_type)
4624 error (" expected a class template, got %qE", arg);
4625 else
4626 error (" expected a type, got %qE", arg);
4629 return error_mark_node;
4631 if (is_tmpl_type ^ requires_tmpl_type)
4633 if (in_decl && (complain & tf_error))
4635 error ("type/value mismatch at argument %d in template "
4636 "parameter list for %qD",
4637 i + 1, in_decl);
4638 if (is_tmpl_type)
4639 error (" expected a type, got %qT", DECL_NAME (arg));
4640 else
4641 error (" expected a class template, got %qT", arg);
4643 return error_mark_node;
4646 if (is_type)
4648 if (requires_tmpl_type)
4650 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4651 /* The number of argument required is not known yet.
4652 Just accept it for now. */
4653 val = TREE_TYPE (arg);
4654 else
4656 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4657 tree argparm;
4659 check_arg = arg;
4660 /* When determining whether a pack expansion is a template,
4661 look at the pattern. */
4662 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4663 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4665 argparm = DECL_INNERMOST_TEMPLATE_PARMS (check_arg);
4667 if (coerce_template_template_parms (parmparm, argparm,
4668 complain, in_decl,
4669 args))
4671 val = arg;
4673 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4674 TEMPLATE_DECL. */
4675 if (val != error_mark_node)
4677 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4678 val = TREE_TYPE (val);
4679 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
4680 && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg))
4682 val = TREE_TYPE (check_arg);
4683 val = make_pack_expansion (val);
4687 else
4689 if (in_decl && (complain & tf_error))
4691 error ("type/value mismatch at argument %d in "
4692 "template parameter list for %qD",
4693 i + 1, in_decl);
4694 error (" expected a template of type %qD, got %qD",
4695 parm, arg);
4698 val = error_mark_node;
4702 else
4703 val = arg;
4704 /* We only form one instance of each template specialization.
4705 Therefore, if we use a non-canonical variant (i.e., a
4706 typedef), any future messages referring to the type will use
4707 the typedef, which is confusing if those future uses do not
4708 themselves also use the typedef. */
4709 if (TYPE_P (val))
4710 val = canonical_type_variant (val);
4712 else
4714 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4716 if (invalid_nontype_parm_type_p (t, complain))
4717 return error_mark_node;
4719 if (!uses_template_parms (arg) && !uses_template_parms (t))
4720 /* We used to call digest_init here. However, digest_init
4721 will report errors, which we don't want when complain
4722 is zero. More importantly, digest_init will try too
4723 hard to convert things: for example, `0' should not be
4724 converted to pointer type at this point according to
4725 the standard. Accepting this is not merely an
4726 extension, since deciding whether or not these
4727 conversions can occur is part of determining which
4728 function template to call, or whether a given explicit
4729 argument specification is valid. */
4730 val = convert_nontype_argument (t, arg);
4731 else
4732 val = arg;
4734 if (val == NULL_TREE)
4735 val = error_mark_node;
4736 else if (val == error_mark_node && (complain & tf_error))
4737 error ("could not convert template argument %qE to %qT", arg, t);
4740 return val;
4743 /* Convert all template arguments to their appropriate types, and
4744 return a vector containing the innermost resulting template
4745 arguments. If any error occurs, return error_mark_node. Error and
4746 warning messages are issued under control of COMPLAIN.
4748 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4749 for arguments not specified in ARGS. Otherwise, if
4750 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4751 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4752 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4753 ARGS. */
4755 static tree
4756 coerce_template_parms (tree parms,
4757 tree args,
4758 tree in_decl,
4759 tsubst_flags_t complain,
4760 bool require_all_args,
4761 bool use_default_args)
4763 int nparms, nargs, i, lost = 0;
4764 tree inner_args;
4765 tree new_args;
4766 tree new_inner_args;
4767 bool saved_skip_evaluation;
4769 /* When used as a boolean value, indicates whether this is a
4770 variadic template parameter list. Since it's an int, we can also
4771 subtract it from nparms to get the number of non-variadic
4772 parameters. */
4773 int variadic_p = template_parms_variadic_p (parms) ? 1 : 0;
4775 inner_args
4776 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args));
4778 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4779 nparms = TREE_VEC_LENGTH (parms);
4781 if ((nargs > nparms - variadic_p && !variadic_p)
4782 || (nargs < nparms - variadic_p
4783 && require_all_args
4784 && (!use_default_args
4785 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4786 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
4788 if (complain & tf_error)
4790 const char *or_more = "";
4791 if (variadic_p)
4793 or_more = " or more";
4794 --nparms;
4797 error ("wrong number of template arguments (%d, should be %d%s)",
4798 nargs, nparms, or_more);
4800 if (in_decl)
4801 error ("provided for %q+D", in_decl);
4804 return error_mark_node;
4807 /* We need to evaluate the template arguments, even though this
4808 template-id may be nested within a "sizeof". */
4809 saved_skip_evaluation = skip_evaluation;
4810 skip_evaluation = false;
4811 new_inner_args = make_tree_vec (nparms);
4812 new_args = add_outermost_template_args (args, new_inner_args);
4813 for (i = 0; i < nparms - variadic_p; i++)
4815 tree arg;
4816 tree parm;
4818 /* Get the Ith template parameter. */
4819 parm = TREE_VEC_ELT (parms, i);
4821 if (parm == error_mark_node)
4823 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
4824 continue;
4827 /* Calculate the Ith argument. */
4828 if (i < nargs)
4830 arg = TREE_VEC_ELT (inner_args, i);
4832 if (PACK_EXPANSION_P (arg))
4834 /* If ARG is a pack expansion, then PARM must be
4835 a template parameter pack. We can't expand into a
4836 fixed-length argument list. */
4837 tree actual_parm = TREE_VALUE (parm);
4838 bool parm_is_parameter_pack
4839 = template_parameter_pack_p (actual_parm);
4841 if (!parm_is_parameter_pack)
4843 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4844 error ("cannot expand %<%E%> into a fixed-length "
4845 "argument list", arg);
4846 else
4847 error ("cannot expand %<%T%> into a fixed-length "
4848 "argument list", arg);
4852 else if (require_all_args)
4853 /* There must be a default arg in this case. */
4854 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4855 complain, in_decl);
4856 else
4857 break;
4859 gcc_assert (arg);
4860 if (arg == error_mark_node)
4862 if (complain & tf_error)
4863 error ("template argument %d is invalid", i + 1);
4865 else
4866 arg = convert_template_argument (TREE_VALUE (parm),
4867 arg, new_args, complain, i,
4868 in_decl);
4870 if (arg == error_mark_node)
4871 lost++;
4872 TREE_VEC_ELT (new_inner_args, i) = arg;
4874 skip_evaluation = saved_skip_evaluation;
4876 if (variadic_p)
4878 int expected_len = nargs - nparms + 1;
4879 tree parm = TREE_VEC_ELT (parms, nparms - 1);
4880 tree packed_args;
4881 tree argument_pack;
4882 tree packed_types = NULL_TREE;
4884 packed_args = make_tree_vec (expected_len >= 0 ? expected_len : 0);
4886 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
4887 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
4889 /* When the template parameter is a non-type template
4890 parameter pack whose type uses parameter packs, we need
4891 to look at each of the template arguments
4892 separately. Build a vector of the types for these
4893 non-type template parameters in PACKED_TYPES. */
4894 tree expansion
4895 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
4896 packed_types = tsubst_pack_expansion (expansion, args,
4897 complain, in_decl);
4899 if (packed_types == error_mark_node)
4900 return error_mark_node;
4902 /* Check that we have the right number of arguments. */
4903 if (i < nargs
4904 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, i))
4905 && nargs - i != TREE_VEC_LENGTH (packed_types))
4907 error ("wrong number of template arguments (%d, should be %d)",
4908 nargs, nparms - 1 + TREE_VEC_LENGTH (packed_types));
4909 return error_mark_node;
4912 /* If we aren't able to check the actual arguments now
4913 (because they haven't been expanded yet), we can at least
4914 verify that all of the types used for the non-type
4915 template parameter pack are, in fact, valid for non-type
4916 template parameters. */
4917 if (i < nargs && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, i)))
4919 int j, len = TREE_VEC_LENGTH (packed_types);
4920 for (j = 0; j < len; ++j)
4922 tree t = TREE_VEC_ELT (packed_types, j);
4923 if (invalid_nontype_parm_type_p (t, complain))
4924 return error_mark_node;
4929 /* Convert the remaining arguments, which will be a part of the
4930 parameter pack "parm". */
4931 for (; i < nargs; ++i)
4933 tree arg = TREE_VEC_ELT (inner_args, i);
4934 tree actual_parm = TREE_VALUE (parm);
4936 if (packed_types && !PACK_EXPANSION_P (arg))
4938 /* When we have a vector of types (corresponding to the
4939 non-type template parameter pack that uses parameter
4940 packs in its type, as mention above), and the
4941 argument is not an expansion (which expands to a
4942 currently unknown number of arguments), clone the
4943 parm and give it the next type in PACKED_TYPES. */
4944 actual_parm = copy_node (actual_parm);
4945 TREE_TYPE (actual_parm) =
4946 TREE_VEC_ELT (packed_types, i - nparms + 1);
4949 arg = convert_template_argument (actual_parm,
4950 arg, new_args, complain, i,
4951 in_decl);
4952 if (arg == error_mark_node)
4953 lost++;
4954 TREE_VEC_ELT (packed_args, i - nparms + 1) = arg;
4957 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
4958 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
4959 argument_pack = make_node (TYPE_ARGUMENT_PACK);
4960 else
4962 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
4963 TREE_TYPE (argument_pack) = TREE_TYPE (TREE_VALUE (parm));
4964 TREE_CONSTANT (argument_pack) = 1;
4967 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
4968 TREE_VEC_ELT (new_inner_args, nparms - 1) = argument_pack;
4971 if (lost)
4972 return error_mark_node;
4974 return new_inner_args;
4977 /* Returns 1 if template args OT and NT are equivalent. */
4979 static int
4980 template_args_equal (tree ot, tree nt)
4982 if (nt == ot)
4983 return 1;
4985 if (TREE_CODE (nt) == TREE_VEC)
4986 /* For member templates */
4987 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4988 else if (PACK_EXPANSION_P (ot))
4989 return PACK_EXPANSION_P (nt)
4990 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
4991 PACK_EXPANSION_PATTERN (nt));
4992 else if (TYPE_P (nt))
4993 return TYPE_P (ot) && same_type_p (ot, nt);
4994 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4995 return 0;
4996 else
4997 return cp_tree_equal (ot, nt);
5000 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5001 of template arguments. Returns 0 otherwise. */
5004 comp_template_args (tree oldargs, tree newargs)
5006 int i;
5008 oldargs = expand_template_argument_pack (oldargs);
5009 newargs = expand_template_argument_pack (newargs);
5011 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5012 return 0;
5014 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5016 tree nt = TREE_VEC_ELT (newargs, i);
5017 tree ot = TREE_VEC_ELT (oldargs, i);
5019 if (! template_args_equal (ot, nt))
5020 return 0;
5022 return 1;
5025 static void
5026 add_pending_template (tree d)
5028 tree ti = (TYPE_P (d)
5029 ? CLASSTYPE_TEMPLATE_INFO (d)
5030 : DECL_TEMPLATE_INFO (d));
5031 tree pt;
5032 int level;
5034 if (TI_PENDING_TEMPLATE_FLAG (ti))
5035 return;
5037 /* We are called both from instantiate_decl, where we've already had a
5038 tinst_level pushed, and instantiate_template, where we haven't.
5039 Compensate. */
5040 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
5042 if (level)
5043 push_tinst_level (d);
5045 pt = tree_cons (current_tinst_level, d, NULL_TREE);
5046 if (last_pending_template)
5047 TREE_CHAIN (last_pending_template) = pt;
5048 else
5049 pending_templates = pt;
5051 last_pending_template = pt;
5053 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5055 if (level)
5056 pop_tinst_level ();
5060 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5061 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5062 documentation for TEMPLATE_ID_EXPR. */
5064 tree
5065 lookup_template_function (tree fns, tree arglist)
5067 tree type;
5069 if (fns == error_mark_node || arglist == error_mark_node)
5070 return error_mark_node;
5072 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5073 gcc_assert (fns && (is_overloaded_fn (fns)
5074 || TREE_CODE (fns) == IDENTIFIER_NODE));
5076 if (BASELINK_P (fns))
5078 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5079 unknown_type_node,
5080 BASELINK_FUNCTIONS (fns),
5081 arglist);
5082 return fns;
5085 type = TREE_TYPE (fns);
5086 if (TREE_CODE (fns) == OVERLOAD || !type)
5087 type = unknown_type_node;
5089 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5092 /* Within the scope of a template class S<T>, the name S gets bound
5093 (in build_self_reference) to a TYPE_DECL for the class, not a
5094 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5095 or one of its enclosing classes, and that type is a template,
5096 return the associated TEMPLATE_DECL. Otherwise, the original
5097 DECL is returned. */
5099 tree
5100 maybe_get_template_decl_from_type_decl (tree decl)
5102 return (decl != NULL_TREE
5103 && TREE_CODE (decl) == TYPE_DECL
5104 && DECL_ARTIFICIAL (decl)
5105 && CLASS_TYPE_P (TREE_TYPE (decl))
5106 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5107 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5110 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5111 parameters, find the desired type.
5113 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5115 IN_DECL, if non-NULL, is the template declaration we are trying to
5116 instantiate.
5118 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5119 the class we are looking up.
5121 Issue error and warning messages under control of COMPLAIN.
5123 If the template class is really a local class in a template
5124 function, then the FUNCTION_CONTEXT is the function in which it is
5125 being instantiated.
5127 ??? Note that this function is currently called *twice* for each
5128 template-id: the first time from the parser, while creating the
5129 incomplete type (finish_template_type), and the second type during the
5130 real instantiation (instantiate_template_class). This is surely something
5131 that we want to avoid. It also causes some problems with argument
5132 coercion (see convert_nontype_argument for more information on this). */
5134 tree
5135 lookup_template_class (tree d1,
5136 tree arglist,
5137 tree in_decl,
5138 tree context,
5139 int entering_scope,
5140 tsubst_flags_t complain)
5142 tree template = NULL_TREE, parmlist;
5143 tree t;
5145 timevar_push (TV_NAME_LOOKUP);
5147 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5149 tree value = innermost_non_namespace_value (d1);
5150 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5151 template = value;
5152 else
5154 if (context)
5155 push_decl_namespace (context);
5156 template = lookup_name (d1);
5157 template = maybe_get_template_decl_from_type_decl (template);
5158 if (context)
5159 pop_decl_namespace ();
5161 if (template)
5162 context = DECL_CONTEXT (template);
5164 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
5166 tree type = TREE_TYPE (d1);
5168 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5169 an implicit typename for the second A. Deal with it. */
5170 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5171 type = TREE_TYPE (type);
5173 if (CLASSTYPE_TEMPLATE_INFO (type))
5175 template = CLASSTYPE_TI_TEMPLATE (type);
5176 d1 = DECL_NAME (template);
5179 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5180 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5182 template = TYPE_TI_TEMPLATE (d1);
5183 d1 = DECL_NAME (template);
5185 else if (TREE_CODE (d1) == TEMPLATE_DECL
5186 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5188 template = d1;
5189 d1 = DECL_NAME (template);
5190 context = DECL_CONTEXT (template);
5193 /* Issue an error message if we didn't find a template. */
5194 if (! template)
5196 if (complain & tf_error)
5197 error ("%qT is not a template", d1);
5198 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5201 if (TREE_CODE (template) != TEMPLATE_DECL
5202 /* Make sure it's a user visible template, if it was named by
5203 the user. */
5204 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
5205 && !PRIMARY_TEMPLATE_P (template)))
5207 if (complain & tf_error)
5209 error ("non-template type %qT used as a template", d1);
5210 if (in_decl)
5211 error ("for template declaration %q+D", in_decl);
5213 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5216 complain &= ~tf_user;
5218 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5220 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5221 template arguments */
5223 tree parm;
5224 tree arglist2;
5226 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
5228 /* Consider an example where a template template parameter declared as
5230 template <class T, class U = std::allocator<T> > class TT
5232 The template parameter level of T and U are one level larger than
5233 of TT. To proper process the default argument of U, say when an
5234 instantiation `TT<int>' is seen, we need to build the full
5235 arguments containing {int} as the innermost level. Outer levels,
5236 available when not appearing as default template argument, can be
5237 obtained from `current_template_args ()'.
5239 Suppose that TT is later substituted with std::vector. The above
5240 instantiation is `TT<int, std::allocator<T> >' with TT at
5241 level 1, and T at level 2, while the template arguments at level 1
5242 becomes {std::vector} and the inner level 2 is {int}. */
5244 if (current_template_parms)
5245 arglist = add_to_template_args (current_template_args (), arglist);
5247 arglist2 = coerce_template_parms (parmlist, arglist, template,
5248 complain,
5249 /*require_all_args=*/true,
5250 /*use_default_args=*/true);
5251 if (arglist2 == error_mark_node
5252 || (!uses_template_parms (arglist2)
5253 && check_instantiated_args (template, arglist2, complain)))
5254 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5256 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
5257 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5259 else
5261 tree template_type = TREE_TYPE (template);
5262 tree gen_tmpl;
5263 tree type_decl;
5264 tree found = NULL_TREE;
5265 int arg_depth;
5266 int parm_depth;
5267 int is_partial_instantiation;
5269 gen_tmpl = most_general_template (template);
5270 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5271 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5272 arg_depth = TMPL_ARGS_DEPTH (arglist);
5274 if (arg_depth == 1 && parm_depth > 1)
5276 /* We've been given an incomplete set of template arguments.
5277 For example, given:
5279 template <class T> struct S1 {
5280 template <class U> struct S2 {};
5281 template <class U> struct S2<U*> {};
5284 we will be called with an ARGLIST of `U*', but the
5285 TEMPLATE will be `template <class T> template
5286 <class U> struct S1<T>::S2'. We must fill in the missing
5287 arguments. */
5288 arglist
5289 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5290 arglist);
5291 arg_depth = TMPL_ARGS_DEPTH (arglist);
5294 /* Now we should have enough arguments. */
5295 gcc_assert (parm_depth == arg_depth);
5297 /* From here on, we're only interested in the most general
5298 template. */
5299 template = gen_tmpl;
5301 /* Calculate the BOUND_ARGS. These will be the args that are
5302 actually tsubst'd into the definition to create the
5303 instantiation. */
5304 if (parm_depth > 1)
5306 /* We have multiple levels of arguments to coerce, at once. */
5307 int i;
5308 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5310 tree bound_args = make_tree_vec (parm_depth);
5312 for (i = saved_depth,
5313 t = DECL_TEMPLATE_PARMS (template);
5314 i > 0 && t != NULL_TREE;
5315 --i, t = TREE_CHAIN (t))
5317 tree a = coerce_template_parms (TREE_VALUE (t),
5318 arglist, template,
5319 complain,
5320 /*require_all_args=*/true,
5321 /*use_default_args=*/true);
5323 /* Don't process further if one of the levels fails. */
5324 if (a == error_mark_node)
5326 /* Restore the ARGLIST to its full size. */
5327 TREE_VEC_LENGTH (arglist) = saved_depth;
5328 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5331 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5333 /* We temporarily reduce the length of the ARGLIST so
5334 that coerce_template_parms will see only the arguments
5335 corresponding to the template parameters it is
5336 examining. */
5337 TREE_VEC_LENGTH (arglist)--;
5340 /* Restore the ARGLIST to its full size. */
5341 TREE_VEC_LENGTH (arglist) = saved_depth;
5343 arglist = bound_args;
5345 else
5346 arglist
5347 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5348 INNERMOST_TEMPLATE_ARGS (arglist),
5349 template,
5350 complain,
5351 /*require_all_args=*/true,
5352 /*use_default_args=*/true);
5354 if (arglist == error_mark_node)
5355 /* We were unable to bind the arguments. */
5356 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5358 /* In the scope of a template class, explicit references to the
5359 template class refer to the type of the template, not any
5360 instantiation of it. For example, in:
5362 template <class T> class C { void f(C<T>); }
5364 the `C<T>' is just the same as `C'. Outside of the
5365 class, however, such a reference is an instantiation. */
5366 if (comp_template_args (TYPE_TI_ARGS (template_type),
5367 arglist))
5369 found = template_type;
5371 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5373 tree ctx;
5375 for (ctx = current_class_type;
5376 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5377 ctx = (TYPE_P (ctx)
5378 ? TYPE_CONTEXT (ctx)
5379 : DECL_CONTEXT (ctx)))
5380 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5381 goto found_ctx;
5383 /* We're not in the scope of the class, so the
5384 TEMPLATE_TYPE is not the type we want after all. */
5385 found = NULL_TREE;
5386 found_ctx:;
5389 if (found)
5390 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5392 /* If we already have this specialization, return it. */
5393 found = retrieve_specialization (template, arglist,
5394 /*class_specializations_p=*/false);
5395 if (found)
5396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5398 /* This type is a "partial instantiation" if any of the template
5399 arguments still involve template parameters. Note that we set
5400 IS_PARTIAL_INSTANTIATION for partial specializations as
5401 well. */
5402 is_partial_instantiation = uses_template_parms (arglist);
5404 /* If the deduced arguments are invalid, then the binding
5405 failed. */
5406 if (!is_partial_instantiation
5407 && check_instantiated_args (template,
5408 INNERMOST_TEMPLATE_ARGS (arglist),
5409 complain))
5410 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5412 if (!is_partial_instantiation
5413 && !PRIMARY_TEMPLATE_P (template)
5414 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
5416 found = xref_tag_from_type (TREE_TYPE (template),
5417 DECL_NAME (template),
5418 /*tag_scope=*/ts_global);
5419 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5422 context = tsubst (DECL_CONTEXT (template), arglist,
5423 complain, in_decl);
5424 if (!context)
5425 context = global_namespace;
5427 /* Create the type. */
5428 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5430 if (!is_partial_instantiation)
5432 set_current_access_from_decl (TYPE_NAME (template_type));
5433 t = start_enum (TYPE_IDENTIFIER (template_type));
5435 else
5436 /* We don't want to call start_enum for this type, since
5437 the values for the enumeration constants may involve
5438 template parameters. And, no one should be interested
5439 in the enumeration constants for such a type. */
5440 t = make_node (ENUMERAL_TYPE);
5442 else
5444 t = make_aggr_type (TREE_CODE (template_type));
5445 CLASSTYPE_DECLARED_CLASS (t)
5446 = CLASSTYPE_DECLARED_CLASS (template_type);
5447 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5448 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5450 /* A local class. Make sure the decl gets registered properly. */
5451 if (context == current_function_decl)
5452 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
5454 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5455 /* This instantiation is another name for the primary
5456 template type. Set the TYPE_CANONICAL field
5457 appropriately. */
5458 TYPE_CANONICAL (t) = template_type;
5459 else if (any_template_arguments_need_structural_equality_p (arglist))
5460 /* Some of the template arguments require structural
5461 equality testing, so this template class requires
5462 structural equality testing. */
5463 SET_TYPE_STRUCTURAL_EQUALITY (t);
5466 /* If we called start_enum or pushtag above, this information
5467 will already be set up. */
5468 if (!TYPE_NAME (t))
5470 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5472 type_decl = create_implicit_typedef (DECL_NAME (template), t);
5473 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5474 TYPE_STUB_DECL (t) = type_decl;
5475 DECL_SOURCE_LOCATION (type_decl)
5476 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5478 else
5479 type_decl = TYPE_NAME (t);
5481 TREE_PRIVATE (type_decl)
5482 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5483 TREE_PROTECTED (type_decl)
5484 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5485 DECL_IN_SYSTEM_HEADER (type_decl)
5486 = DECL_IN_SYSTEM_HEADER (template);
5487 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5489 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5490 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5493 /* Set up the template information. We have to figure out which
5494 template is the immediate parent if this is a full
5495 instantiation. */
5496 if (parm_depth == 1 || is_partial_instantiation
5497 || !PRIMARY_TEMPLATE_P (template))
5498 /* This case is easy; there are no member templates involved. */
5499 found = template;
5500 else
5502 /* This is a full instantiation of a member template. Look
5503 for a partial instantiation of which this is an instance. */
5505 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
5506 found; found = TREE_CHAIN (found))
5508 int success;
5509 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5511 /* We only want partial instantiations, here, not
5512 specializations or full instantiations. */
5513 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5514 || !uses_template_parms (TREE_VALUE (found)))
5515 continue;
5517 /* Temporarily reduce by one the number of levels in the
5518 ARGLIST and in FOUND so as to avoid comparing the
5519 last set of arguments. */
5520 TREE_VEC_LENGTH (arglist)--;
5521 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5523 /* See if the arguments match. If they do, then TMPL is
5524 the partial instantiation we want. */
5525 success = comp_template_args (TREE_PURPOSE (found), arglist);
5527 /* Restore the argument vectors to their full size. */
5528 TREE_VEC_LENGTH (arglist)++;
5529 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
5531 if (success)
5533 found = tmpl;
5534 break;
5538 if (!found)
5540 /* There was no partial instantiation. This happens
5541 where C<T> is a member template of A<T> and it's used
5542 in something like
5544 template <typename T> struct B { A<T>::C<int> m; };
5545 B<float>;
5547 Create the partial instantiation.
5549 TREE_VEC_LENGTH (arglist)--;
5550 found = tsubst (template, arglist, complain, NULL_TREE);
5551 TREE_VEC_LENGTH (arglist)++;
5555 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
5556 DECL_TEMPLATE_INSTANTIATIONS (template)
5557 = tree_cons (arglist, t,
5558 DECL_TEMPLATE_INSTANTIATIONS (template));
5560 if (TREE_CODE (t) == ENUMERAL_TYPE
5561 && !is_partial_instantiation)
5562 /* Now that the type has been registered on the instantiations
5563 list, we set up the enumerators. Because the enumeration
5564 constants may involve the enumeration type itself, we make
5565 sure to register the type first, and then create the
5566 constants. That way, doing tsubst_expr for the enumeration
5567 constants won't result in recursive calls here; we'll find
5568 the instantiation and exit above. */
5569 tsubst_enum (template_type, t, arglist);
5571 if (is_partial_instantiation)
5572 /* If the type makes use of template parameters, the
5573 code that generates debugging information will crash. */
5574 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
5576 /* Possibly limit visibility based on template args. */
5577 TREE_PUBLIC (type_decl) = 1;
5578 determine_visibility (type_decl);
5580 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5582 timevar_pop (TV_NAME_LOOKUP);
5585 struct pair_fn_data
5587 tree_fn_t fn;
5588 void *data;
5589 struct pointer_set_t *visited;
5592 /* Called from for_each_template_parm via walk_tree. */
5594 static tree
5595 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
5597 tree t = *tp;
5598 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
5599 tree_fn_t fn = pfd->fn;
5600 void *data = pfd->data;
5602 if (TYPE_P (t)
5603 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
5604 return error_mark_node;
5606 switch (TREE_CODE (t))
5608 case RECORD_TYPE:
5609 if (TYPE_PTRMEMFUNC_P (t))
5610 break;
5611 /* Fall through. */
5613 case UNION_TYPE:
5614 case ENUMERAL_TYPE:
5615 if (!TYPE_TEMPLATE_INFO (t))
5616 *walk_subtrees = 0;
5617 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
5618 fn, data, pfd->visited))
5619 return error_mark_node;
5620 break;
5622 case INTEGER_TYPE:
5623 if (for_each_template_parm (TYPE_MIN_VALUE (t),
5624 fn, data, pfd->visited)
5625 || for_each_template_parm (TYPE_MAX_VALUE (t),
5626 fn, data, pfd->visited))
5627 return error_mark_node;
5628 break;
5630 case METHOD_TYPE:
5631 /* Since we're not going to walk subtrees, we have to do this
5632 explicitly here. */
5633 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
5634 pfd->visited))
5635 return error_mark_node;
5636 /* Fall through. */
5638 case FUNCTION_TYPE:
5639 /* Check the return type. */
5640 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5641 return error_mark_node;
5643 /* Check the parameter types. Since default arguments are not
5644 instantiated until they are needed, the TYPE_ARG_TYPES may
5645 contain expressions that involve template parameters. But,
5646 no-one should be looking at them yet. And, once they're
5647 instantiated, they don't contain template parameters, so
5648 there's no point in looking at them then, either. */
5650 tree parm;
5652 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5653 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5654 pfd->visited))
5655 return error_mark_node;
5657 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5658 want walk_tree walking into them itself. */
5659 *walk_subtrees = 0;
5661 break;
5663 case TYPEOF_TYPE:
5664 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5665 pfd->visited))
5666 return error_mark_node;
5667 break;
5669 case FUNCTION_DECL:
5670 case VAR_DECL:
5671 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5672 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5673 pfd->visited))
5674 return error_mark_node;
5675 /* Fall through. */
5677 case PARM_DECL:
5678 case CONST_DECL:
5679 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5680 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5681 pfd->visited))
5682 return error_mark_node;
5683 if (DECL_CONTEXT (t)
5684 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5685 pfd->visited))
5686 return error_mark_node;
5687 break;
5689 case BOUND_TEMPLATE_TEMPLATE_PARM:
5690 /* Record template parameters such as `T' inside `TT<T>'. */
5691 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5692 return error_mark_node;
5693 /* Fall through. */
5695 case TEMPLATE_TEMPLATE_PARM:
5696 case TEMPLATE_TYPE_PARM:
5697 case TEMPLATE_PARM_INDEX:
5698 if (fn && (*fn)(t, data))
5699 return error_mark_node;
5700 else if (!fn)
5701 return error_mark_node;
5702 break;
5704 case TEMPLATE_DECL:
5705 /* A template template parameter is encountered. */
5706 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5707 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5708 return error_mark_node;
5710 /* Already substituted template template parameter */
5711 *walk_subtrees = 0;
5712 break;
5714 case TYPENAME_TYPE:
5715 if (!fn
5716 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5717 data, pfd->visited))
5718 return error_mark_node;
5719 break;
5721 case CONSTRUCTOR:
5722 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5723 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5724 (TREE_TYPE (t)), fn, data,
5725 pfd->visited))
5726 return error_mark_node;
5727 break;
5729 case INDIRECT_REF:
5730 case COMPONENT_REF:
5731 /* If there's no type, then this thing must be some expression
5732 involving template parameters. */
5733 if (!fn && !TREE_TYPE (t))
5734 return error_mark_node;
5735 break;
5737 case MODOP_EXPR:
5738 case CAST_EXPR:
5739 case REINTERPRET_CAST_EXPR:
5740 case CONST_CAST_EXPR:
5741 case STATIC_CAST_EXPR:
5742 case DYNAMIC_CAST_EXPR:
5743 case ARROW_EXPR:
5744 case DOTSTAR_EXPR:
5745 case TYPEID_EXPR:
5746 case PSEUDO_DTOR_EXPR:
5747 if (!fn)
5748 return error_mark_node;
5749 break;
5751 default:
5752 break;
5755 /* We didn't find any template parameters we liked. */
5756 return NULL_TREE;
5759 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5760 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5761 call FN with the parameter and the DATA.
5762 If FN returns nonzero, the iteration is terminated, and
5763 for_each_template_parm returns 1. Otherwise, the iteration
5764 continues. If FN never returns a nonzero value, the value
5765 returned by for_each_template_parm is 0. If FN is NULL, it is
5766 considered to be the function which always returns 1. */
5768 static int
5769 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5770 struct pointer_set_t *visited)
5772 struct pair_fn_data pfd;
5773 int result;
5775 /* Set up. */
5776 pfd.fn = fn;
5777 pfd.data = data;
5779 /* Walk the tree. (Conceptually, we would like to walk without
5780 duplicates, but for_each_template_parm_r recursively calls
5781 for_each_template_parm, so we would need to reorganize a fair
5782 bit to use walk_tree_without_duplicates, so we keep our own
5783 visited list.) */
5784 if (visited)
5785 pfd.visited = visited;
5786 else
5787 pfd.visited = pointer_set_create ();
5788 result = walk_tree (&t,
5789 for_each_template_parm_r,
5790 &pfd,
5791 pfd.visited) != NULL_TREE;
5793 /* Clean up. */
5794 if (!visited)
5796 pointer_set_destroy (pfd.visited);
5797 pfd.visited = 0;
5800 return result;
5803 /* Returns true if T depends on any template parameter. */
5806 uses_template_parms (tree t)
5808 bool dependent_p;
5809 int saved_processing_template_decl;
5811 saved_processing_template_decl = processing_template_decl;
5812 if (!saved_processing_template_decl)
5813 processing_template_decl = 1;
5814 if (TYPE_P (t))
5815 dependent_p = dependent_type_p (t);
5816 else if (TREE_CODE (t) == TREE_VEC)
5817 dependent_p = any_dependent_template_arguments_p (t);
5818 else if (TREE_CODE (t) == TREE_LIST)
5819 dependent_p = (uses_template_parms (TREE_VALUE (t))
5820 || uses_template_parms (TREE_CHAIN (t)));
5821 else if (TREE_CODE (t) == TYPE_DECL)
5822 dependent_p = dependent_type_p (TREE_TYPE (t));
5823 else if (DECL_P (t)
5824 || EXPR_P (t)
5825 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5826 || TREE_CODE (t) == OVERLOAD
5827 || TREE_CODE (t) == BASELINK
5828 || TREE_CODE (t) == IDENTIFIER_NODE
5829 || TREE_CODE (t) == TRAIT_EXPR
5830 || CONSTANT_CLASS_P (t))
5831 dependent_p = (type_dependent_expression_p (t)
5832 || value_dependent_expression_p (t));
5833 else
5835 gcc_assert (t == error_mark_node);
5836 dependent_p = false;
5839 processing_template_decl = saved_processing_template_decl;
5841 return dependent_p;
5844 /* Returns true if T depends on any template parameter with level LEVEL. */
5847 uses_template_parms_level (tree t, int level)
5849 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5852 static int tinst_depth;
5853 extern int max_tinst_depth;
5854 #ifdef GATHER_STATISTICS
5855 int depth_reached;
5856 #endif
5857 static int tinst_level_tick;
5858 static int last_template_error_tick;
5860 /* We're starting to instantiate D; record the template instantiation context
5861 for diagnostics and to restore it later. */
5863 static int
5864 push_tinst_level (tree d)
5866 tree new;
5868 if (tinst_depth >= max_tinst_depth)
5870 /* If the instantiation in question still has unbound template parms,
5871 we don't really care if we can't instantiate it, so just return.
5872 This happens with base instantiation for implicit `typename'. */
5873 if (uses_template_parms (d))
5874 return 0;
5876 last_template_error_tick = tinst_level_tick;
5877 error ("template instantiation depth exceeds maximum of %d (use "
5878 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5879 max_tinst_depth, d);
5881 print_instantiation_context ();
5883 return 0;
5886 new = make_node (TINST_LEVEL);
5887 TINST_DECL (new) = d;
5888 TINST_LOCATION (new) = input_location;
5889 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5890 TREE_CHAIN (new) = current_tinst_level;
5891 current_tinst_level = new;
5893 ++tinst_depth;
5894 #ifdef GATHER_STATISTICS
5895 if (tinst_depth > depth_reached)
5896 depth_reached = tinst_depth;
5897 #endif
5899 ++tinst_level_tick;
5900 return 1;
5903 /* We're done instantiating this template; return to the instantiation
5904 context. */
5906 static void
5907 pop_tinst_level (void)
5909 tree old = current_tinst_level;
5911 /* Restore the filename and line number stashed away when we started
5912 this instantiation. */
5913 input_location = TINST_LOCATION (old);
5914 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5915 current_tinst_level = TREE_CHAIN (old);
5916 --tinst_depth;
5917 ++tinst_level_tick;
5920 /* We're instantiating a deferred template; restore the template
5921 instantiation context in which the instantiation was requested, which
5922 is one step out from LEVEL. */
5924 static void
5925 reopen_tinst_level (tree level)
5927 tree t;
5929 tinst_depth = 0;
5930 for (t = level; t; t = TREE_CHAIN (t))
5931 ++tinst_depth;
5933 current_tinst_level = level;
5934 pop_tinst_level ();
5937 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5938 vector of template arguments, as for tsubst.
5940 Returns an appropriate tsubst'd friend declaration. */
5942 static tree
5943 tsubst_friend_function (tree decl, tree args)
5945 tree new_friend;
5947 if (TREE_CODE (decl) == FUNCTION_DECL
5948 && DECL_TEMPLATE_INSTANTIATION (decl)
5949 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5950 /* This was a friend declared with an explicit template
5951 argument list, e.g.:
5953 friend void f<>(T);
5955 to indicate that f was a template instantiation, not a new
5956 function declaration. Now, we have to figure out what
5957 instantiation of what template. */
5959 tree template_id, arglist, fns;
5960 tree new_args;
5961 tree tmpl;
5962 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5964 /* Friend functions are looked up in the containing namespace scope.
5965 We must enter that scope, to avoid finding member functions of the
5966 current cless with same name. */
5967 push_nested_namespace (ns);
5968 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5969 tf_warning_or_error, NULL_TREE,
5970 /*integral_constant_expression_p=*/false);
5971 pop_nested_namespace (ns);
5972 arglist = tsubst (DECL_TI_ARGS (decl), args,
5973 tf_warning_or_error, NULL_TREE);
5974 template_id = lookup_template_function (fns, arglist);
5976 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5977 tmpl = determine_specialization (template_id, new_friend,
5978 &new_args,
5979 /*need_member_template=*/0,
5980 TREE_VEC_LENGTH (args),
5981 tsk_none);
5982 return instantiate_template (tmpl, new_args, tf_error);
5985 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5987 /* The NEW_FRIEND will look like an instantiation, to the
5988 compiler, but is not an instantiation from the point of view of
5989 the language. For example, we might have had:
5991 template <class T> struct S {
5992 template <class U> friend void f(T, U);
5995 Then, in S<int>, template <class U> void f(int, U) is not an
5996 instantiation of anything. */
5997 if (new_friend == error_mark_node)
5998 return error_mark_node;
6000 DECL_USE_TEMPLATE (new_friend) = 0;
6001 if (TREE_CODE (decl) == TEMPLATE_DECL)
6003 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6004 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6005 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6008 /* The mangled name for the NEW_FRIEND is incorrect. The function
6009 is not a template instantiation and should not be mangled like
6010 one. Therefore, we forget the mangling here; we'll recompute it
6011 later if we need it. */
6012 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6014 SET_DECL_RTL (new_friend, NULL_RTX);
6015 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6018 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6020 tree old_decl;
6021 tree new_friend_template_info;
6022 tree new_friend_result_template_info;
6023 tree ns;
6024 int new_friend_is_defn;
6026 /* We must save some information from NEW_FRIEND before calling
6027 duplicate decls since that function will free NEW_FRIEND if
6028 possible. */
6029 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6030 new_friend_is_defn =
6031 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6032 (template_for_substitution (new_friend)))
6033 != NULL_TREE);
6034 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6036 /* This declaration is a `primary' template. */
6037 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6039 new_friend_result_template_info
6040 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6042 else
6043 new_friend_result_template_info = NULL_TREE;
6045 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6046 if (new_friend_is_defn)
6047 DECL_INITIAL (new_friend) = error_mark_node;
6049 /* Inside pushdecl_namespace_level, we will push into the
6050 current namespace. However, the friend function should go
6051 into the namespace of the template. */
6052 ns = decl_namespace_context (new_friend);
6053 push_nested_namespace (ns);
6054 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6055 pop_nested_namespace (ns);
6057 if (old_decl == error_mark_node)
6058 return error_mark_node;
6060 if (old_decl != new_friend)
6062 /* This new friend declaration matched an existing
6063 declaration. For example, given:
6065 template <class T> void f(T);
6066 template <class U> class C {
6067 template <class T> friend void f(T) {}
6070 the friend declaration actually provides the definition
6071 of `f', once C has been instantiated for some type. So,
6072 old_decl will be the out-of-class template declaration,
6073 while new_friend is the in-class definition.
6075 But, if `f' was called before this point, the
6076 instantiation of `f' will have DECL_TI_ARGS corresponding
6077 to `T' but not to `U', references to which might appear
6078 in the definition of `f'. Previously, the most general
6079 template for an instantiation of `f' was the out-of-class
6080 version; now it is the in-class version. Therefore, we
6081 run through all specialization of `f', adding to their
6082 DECL_TI_ARGS appropriately. In particular, they need a
6083 new set of outer arguments, corresponding to the
6084 arguments for this class instantiation.
6086 The same situation can arise with something like this:
6088 friend void f(int);
6089 template <class T> class C {
6090 friend void f(T) {}
6093 when `C<int>' is instantiated. Now, `f(int)' is defined
6094 in the class. */
6096 if (!new_friend_is_defn)
6097 /* On the other hand, if the in-class declaration does
6098 *not* provide a definition, then we don't want to alter
6099 existing definitions. We can just leave everything
6100 alone. */
6102 else
6104 /* Overwrite whatever template info was there before, if
6105 any, with the new template information pertaining to
6106 the declaration. */
6107 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6109 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6110 reregister_specialization (new_friend,
6111 most_general_template (old_decl),
6112 old_decl);
6113 else
6115 tree t;
6116 tree new_friend_args;
6118 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6119 = new_friend_result_template_info;
6121 new_friend_args = TI_ARGS (new_friend_template_info);
6122 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6123 t != NULL_TREE;
6124 t = TREE_CHAIN (t))
6126 tree spec = TREE_VALUE (t);
6128 DECL_TI_ARGS (spec)
6129 = add_outermost_template_args (new_friend_args,
6130 DECL_TI_ARGS (spec));
6133 /* Now, since specializations are always supposed to
6134 hang off of the most general template, we must move
6135 them. */
6136 t = most_general_template (old_decl);
6137 if (t != old_decl)
6139 DECL_TEMPLATE_SPECIALIZATIONS (t)
6140 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6141 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6142 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6147 /* The information from NEW_FRIEND has been merged into OLD_DECL
6148 by duplicate_decls. */
6149 new_friend = old_decl;
6152 else
6154 tree context = DECL_CONTEXT (new_friend);
6155 bool dependent_p;
6157 /* In the code
6158 template <class T> class C {
6159 template <class U> friend void C1<U>::f (); // case 1
6160 friend void C2<T>::f (); // case 2
6162 we only need to make sure CONTEXT is a complete type for
6163 case 2. To distinguish between the two cases, we note that
6164 CONTEXT of case 1 remains dependent type after tsubst while
6165 this isn't true for case 2. */
6166 ++processing_template_decl;
6167 dependent_p = dependent_type_p (context);
6168 --processing_template_decl;
6170 if (!dependent_p
6171 && !complete_type_or_else (context, NULL_TREE))
6172 return error_mark_node;
6174 if (COMPLETE_TYPE_P (context))
6176 /* Check to see that the declaration is really present, and,
6177 possibly obtain an improved declaration. */
6178 tree fn = check_classfn (context,
6179 new_friend, NULL_TREE);
6181 if (fn)
6182 new_friend = fn;
6186 return new_friend;
6189 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6190 template arguments, as for tsubst.
6192 Returns an appropriate tsubst'd friend type or error_mark_node on
6193 failure. */
6195 static tree
6196 tsubst_friend_class (tree friend_tmpl, tree args)
6198 tree friend_type;
6199 tree tmpl;
6200 tree context;
6202 context = DECL_CONTEXT (friend_tmpl);
6204 if (context)
6206 if (TREE_CODE (context) == NAMESPACE_DECL)
6207 push_nested_namespace (context);
6208 else
6209 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6212 /* Look for a class template declaration. We look for hidden names
6213 because two friend declarations of the same template are the
6214 same. For example, in:
6216 struct A {
6217 template <typename> friend class F;
6219 template <typename> struct B {
6220 template <typename> friend class F;
6223 both F templates are the same. */
6224 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6225 /*block_p=*/true, 0,
6226 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6228 /* But, if we don't find one, it might be because we're in a
6229 situation like this:
6231 template <class T>
6232 struct S {
6233 template <class U>
6234 friend struct S;
6237 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6238 for `S<int>', not the TEMPLATE_DECL. */
6239 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6241 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6242 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6245 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6247 /* The friend template has already been declared. Just
6248 check to see that the declarations match, and install any new
6249 default parameters. We must tsubst the default parameters,
6250 of course. We only need the innermost template parameters
6251 because that is all that redeclare_class_template will look
6252 at. */
6253 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6254 > TMPL_ARGS_DEPTH (args))
6256 tree parms;
6257 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6258 args, tf_warning_or_error);
6259 redeclare_class_template (TREE_TYPE (tmpl), parms);
6262 friend_type = TREE_TYPE (tmpl);
6264 else
6266 /* The friend template has not already been declared. In this
6267 case, the instantiation of the template class will cause the
6268 injection of this template into the global scope. */
6269 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6270 if (tmpl == error_mark_node)
6271 return error_mark_node;
6273 /* The new TMPL is not an instantiation of anything, so we
6274 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6275 the new type because that is supposed to be the corresponding
6276 template decl, i.e., TMPL. */
6277 DECL_USE_TEMPLATE (tmpl) = 0;
6278 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6279 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6280 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6281 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6283 /* Inject this template into the global scope. */
6284 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6287 if (context)
6289 if (TREE_CODE (context) == NAMESPACE_DECL)
6290 pop_nested_namespace (context);
6291 else
6292 pop_nested_class ();
6295 return friend_type;
6298 /* Returns zero if TYPE cannot be completed later due to circularity.
6299 Otherwise returns one. */
6301 static int
6302 can_complete_type_without_circularity (tree type)
6304 if (type == NULL_TREE || type == error_mark_node)
6305 return 0;
6306 else if (COMPLETE_TYPE_P (type))
6307 return 1;
6308 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6309 return can_complete_type_without_circularity (TREE_TYPE (type));
6310 else if (CLASS_TYPE_P (type)
6311 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6312 return 0;
6313 else
6314 return 1;
6317 tree
6318 instantiate_class_template (tree type)
6320 tree template, args, pattern, t, member;
6321 tree typedecl;
6322 tree pbinfo;
6323 tree base_list;
6325 if (type == error_mark_node)
6326 return error_mark_node;
6328 if (TYPE_BEING_DEFINED (type)
6329 || COMPLETE_TYPE_P (type)
6330 || dependent_type_p (type))
6331 return type;
6333 /* Figure out which template is being instantiated. */
6334 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6335 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
6337 /* Determine what specialization of the original template to
6338 instantiate. */
6339 t = most_specialized_class (type, template);
6340 if (t == error_mark_node)
6342 TYPE_BEING_DEFINED (type) = 1;
6343 return error_mark_node;
6345 else if (t)
6347 /* This TYPE is actually an instantiation of a partial
6348 specialization. We replace the innermost set of ARGS with
6349 the arguments appropriate for substitution. For example,
6350 given:
6352 template <class T> struct S {};
6353 template <class T> struct S<T*> {};
6355 and supposing that we are instantiating S<int*>, ARGS will
6356 presently be {int*} -- but we need {int}. */
6357 pattern = TREE_TYPE (t);
6358 args = TREE_PURPOSE (t);
6360 else
6362 pattern = TREE_TYPE (template);
6363 args = CLASSTYPE_TI_ARGS (type);
6366 /* If the template we're instantiating is incomplete, then clearly
6367 there's nothing we can do. */
6368 if (!COMPLETE_TYPE_P (pattern))
6369 return type;
6371 /* If we've recursively instantiated too many templates, stop. */
6372 if (! push_tinst_level (type))
6373 return type;
6375 /* Now we're really doing the instantiation. Mark the type as in
6376 the process of being defined. */
6377 TYPE_BEING_DEFINED (type) = 1;
6379 /* We may be in the middle of deferred access check. Disable
6380 it now. */
6381 push_deferring_access_checks (dk_no_deferred);
6383 push_to_top_level ();
6385 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
6387 /* Set the input location to the template definition. This is needed
6388 if tsubsting causes an error. */
6389 typedecl = TYPE_MAIN_DECL (type);
6390 input_location = DECL_SOURCE_LOCATION (typedecl);
6391 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
6393 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
6394 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
6395 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
6396 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
6397 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
6398 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
6399 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
6400 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
6401 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
6402 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
6403 TYPE_PACKED (type) = TYPE_PACKED (pattern);
6404 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
6405 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
6406 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6407 if (ANON_AGGR_TYPE_P (pattern))
6408 SET_ANON_AGGR_TYPE_P (type);
6409 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
6411 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
6412 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
6415 pbinfo = TYPE_BINFO (pattern);
6417 /* We should never instantiate a nested class before its enclosing
6418 class; we need to look up the nested class by name before we can
6419 instantiate it, and that lookup should instantiate the enclosing
6420 class. */
6421 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
6422 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
6423 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
6425 base_list = NULL_TREE;
6426 if (BINFO_N_BASE_BINFOS (pbinfo))
6428 tree pbase_binfo;
6429 tree context = TYPE_CONTEXT (type);
6430 tree pushed_scope;
6431 int i;
6433 /* We must enter the scope containing the type, as that is where
6434 the accessibility of types named in dependent bases are
6435 looked up from. */
6436 pushed_scope = push_scope (context ? context : global_namespace);
6438 /* Substitute into each of the bases to determine the actual
6439 basetypes. */
6440 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
6442 tree base;
6443 tree access = BINFO_BASE_ACCESS (pbinfo, i);
6444 tree expanded_bases = NULL_TREE;
6445 int idx, len = 1;
6447 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
6449 expanded_bases =
6450 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
6451 args, tf_error, NULL_TREE);
6452 if (expanded_bases == error_mark_node)
6453 continue;
6455 len = TREE_VEC_LENGTH (expanded_bases);
6458 for (idx = 0; idx < len; idx++)
6460 if (expanded_bases)
6461 /* Extract the already-expanded base class. */
6462 base = TREE_VEC_ELT (expanded_bases, idx);
6463 else
6464 /* Substitute to figure out the base class. */
6465 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
6466 NULL_TREE);
6468 if (base == error_mark_node)
6469 continue;
6471 base_list = tree_cons (access, base, base_list);
6472 if (BINFO_VIRTUAL_P (pbase_binfo))
6473 TREE_TYPE (base_list) = integer_type_node;
6477 /* The list is now in reverse order; correct that. */
6478 base_list = nreverse (base_list);
6480 if (pushed_scope)
6481 pop_scope (pushed_scope);
6483 /* Now call xref_basetypes to set up all the base-class
6484 information. */
6485 xref_basetypes (type, base_list);
6488 /* Now that our base classes are set up, enter the scope of the
6489 class, so that name lookups into base classes, etc. will work
6490 correctly. This is precisely analogous to what we do in
6491 begin_class_definition when defining an ordinary non-template
6492 class. */
6493 pushclass (type);
6495 /* Now members are processed in the order of declaration. */
6496 for (member = CLASSTYPE_DECL_LIST (pattern);
6497 member; member = TREE_CHAIN (member))
6499 tree t = TREE_VALUE (member);
6501 if (TREE_PURPOSE (member))
6503 if (TYPE_P (t))
6505 /* Build new CLASSTYPE_NESTED_UTDS. */
6507 tree newtag;
6508 bool class_template_p;
6510 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
6511 && TYPE_LANG_SPECIFIC (t)
6512 && CLASSTYPE_IS_TEMPLATE (t));
6513 /* If the member is a class template, then -- even after
6514 substitution -- there may be dependent types in the
6515 template argument list for the class. We increment
6516 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6517 that function will assume that no types are dependent
6518 when outside of a template. */
6519 if (class_template_p)
6520 ++processing_template_decl;
6521 newtag = tsubst (t, args, tf_error, NULL_TREE);
6522 if (class_template_p)
6523 --processing_template_decl;
6524 if (newtag == error_mark_node)
6525 continue;
6527 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
6529 tree name = TYPE_IDENTIFIER (t);
6531 if (class_template_p)
6532 /* Unfortunately, lookup_template_class sets
6533 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6534 instantiation (i.e., for the type of a member
6535 template class nested within a template class.)
6536 This behavior is required for
6537 maybe_process_partial_specialization to work
6538 correctly, but is not accurate in this case;
6539 the TAG is not an instantiation of anything.
6540 (The corresponding TEMPLATE_DECL is an
6541 instantiation, but the TYPE is not.) */
6542 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
6544 /* Now, we call pushtag to put this NEWTAG into the scope of
6545 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6546 pushtag calling push_template_decl. We don't have to do
6547 this for enums because it will already have been done in
6548 tsubst_enum. */
6549 if (name)
6550 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
6551 pushtag (name, newtag, /*tag_scope=*/ts_current);
6554 else if (TREE_CODE (t) == FUNCTION_DECL
6555 || DECL_FUNCTION_TEMPLATE_P (t))
6557 /* Build new TYPE_METHODS. */
6558 tree r;
6560 if (TREE_CODE (t) == TEMPLATE_DECL)
6561 ++processing_template_decl;
6562 r = tsubst (t, args, tf_error, NULL_TREE);
6563 if (TREE_CODE (t) == TEMPLATE_DECL)
6564 --processing_template_decl;
6565 set_current_access_from_decl (r);
6566 finish_member_declaration (r);
6568 else
6570 /* Build new TYPE_FIELDS. */
6571 if (TREE_CODE (t) == STATIC_ASSERT)
6573 tree condition =
6574 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
6575 tf_warning_or_error, NULL_TREE,
6576 /*integral_constant_expression_p=*/true);
6577 finish_static_assert (condition,
6578 STATIC_ASSERT_MESSAGE (t),
6579 STATIC_ASSERT_SOURCE_LOCATION (t),
6580 /*member_p=*/true);
6582 else if (TREE_CODE (t) != CONST_DECL)
6584 tree r;
6586 /* The the file and line for this declaration, to
6587 assist in error message reporting. Since we
6588 called push_tinst_level above, we don't need to
6589 restore these. */
6590 input_location = DECL_SOURCE_LOCATION (t);
6592 if (TREE_CODE (t) == TEMPLATE_DECL)
6593 ++processing_template_decl;
6594 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
6595 if (TREE_CODE (t) == TEMPLATE_DECL)
6596 --processing_template_decl;
6597 if (TREE_CODE (r) == VAR_DECL)
6599 /* In [temp.inst]:
6601 [t]he initialization (and any associated
6602 side-effects) of a static data member does
6603 not occur unless the static data member is
6604 itself used in a way that requires the
6605 definition of the static data member to
6606 exist.
6608 Therefore, we do not substitute into the
6609 initialized for the static data member here. */
6610 finish_static_data_member_decl
6612 /*init=*/NULL_TREE,
6613 /*init_const_expr_p=*/false,
6614 /*asmspec_tree=*/NULL_TREE,
6615 /*flags=*/0);
6616 if (DECL_INITIALIZED_IN_CLASS_P (r))
6617 check_static_variable_definition (r, TREE_TYPE (r));
6619 else if (TREE_CODE (r) == FIELD_DECL)
6621 /* Determine whether R has a valid type and can be
6622 completed later. If R is invalid, then it is
6623 replaced by error_mark_node so that it will not be
6624 added to TYPE_FIELDS. */
6625 tree rtype = TREE_TYPE (r);
6626 if (can_complete_type_without_circularity (rtype))
6627 complete_type (rtype);
6629 if (!COMPLETE_TYPE_P (rtype))
6631 cxx_incomplete_type_error (r, rtype);
6632 r = error_mark_node;
6636 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6637 such a thing will already have been added to the field
6638 list by tsubst_enum in finish_member_declaration in the
6639 CLASSTYPE_NESTED_UTDS case above. */
6640 if (!(TREE_CODE (r) == TYPE_DECL
6641 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
6642 && DECL_ARTIFICIAL (r)))
6644 set_current_access_from_decl (r);
6645 finish_member_declaration (r);
6650 else
6652 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
6654 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6656 tree friend_type = t;
6657 bool adjust_processing_template_decl = false;
6659 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6661 /* template <class T> friend class C; */
6662 friend_type = tsubst_friend_class (friend_type, args);
6663 adjust_processing_template_decl = true;
6665 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6667 /* template <class T> friend class C::D; */
6668 friend_type = tsubst (friend_type, args,
6669 tf_warning_or_error, NULL_TREE);
6670 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6671 friend_type = TREE_TYPE (friend_type);
6672 adjust_processing_template_decl = true;
6674 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6676 /* This could be either
6678 friend class T::C;
6680 when dependent_type_p is false or
6682 template <class U> friend class T::C;
6684 otherwise. */
6685 friend_type = tsubst (friend_type, args,
6686 tf_warning_or_error, NULL_TREE);
6687 /* Bump processing_template_decl for correct
6688 dependent_type_p calculation. */
6689 ++processing_template_decl;
6690 if (dependent_type_p (friend_type))
6691 adjust_processing_template_decl = true;
6692 --processing_template_decl;
6694 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6695 && hidden_name_p (TYPE_NAME (friend_type)))
6697 /* friend class C;
6699 where C hasn't been declared yet. Let's lookup name
6700 from namespace scope directly, bypassing any name that
6701 come from dependent base class. */
6702 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6704 /* The call to xref_tag_from_type does injection for friend
6705 classes. */
6706 push_nested_namespace (ns);
6707 friend_type =
6708 xref_tag_from_type (friend_type, NULL_TREE,
6709 /*tag_scope=*/ts_current);
6710 pop_nested_namespace (ns);
6712 else if (uses_template_parms (friend_type))
6713 /* friend class C<T>; */
6714 friend_type = tsubst (friend_type, args,
6715 tf_warning_or_error, NULL_TREE);
6716 /* Otherwise it's
6718 friend class C;
6720 where C is already declared or
6722 friend class C<int>;
6724 We don't have to do anything in these cases. */
6726 if (adjust_processing_template_decl)
6727 /* Trick make_friend_class into realizing that the friend
6728 we're adding is a template, not an ordinary class. It's
6729 important that we use make_friend_class since it will
6730 perform some error-checking and output cross-reference
6731 information. */
6732 ++processing_template_decl;
6734 if (friend_type != error_mark_node)
6735 make_friend_class (type, friend_type, /*complain=*/false);
6737 if (adjust_processing_template_decl)
6738 --processing_template_decl;
6740 else
6742 /* Build new DECL_FRIENDLIST. */
6743 tree r;
6745 /* The the file and line for this declaration, to
6746 assist in error message reporting. Since we
6747 called push_tinst_level above, we don't need to
6748 restore these. */
6749 input_location = DECL_SOURCE_LOCATION (t);
6751 if (TREE_CODE (t) == TEMPLATE_DECL)
6753 ++processing_template_decl;
6754 push_deferring_access_checks (dk_no_check);
6757 r = tsubst_friend_function (t, args);
6758 add_friend (type, r, /*complain=*/false);
6759 if (TREE_CODE (t) == TEMPLATE_DECL)
6761 pop_deferring_access_checks ();
6762 --processing_template_decl;
6768 /* Set the file and line number information to whatever is given for
6769 the class itself. This puts error messages involving generated
6770 implicit functions at a predictable point, and the same point
6771 that would be used for non-template classes. */
6772 input_location = DECL_SOURCE_LOCATION (typedecl);
6774 unreverse_member_declarations (type);
6775 finish_struct_1 (type);
6776 TYPE_BEING_DEFINED (type) = 0;
6778 /* Now that the class is complete, instantiate default arguments for
6779 any member functions. We don't do this earlier because the
6780 default arguments may reference members of the class. */
6781 if (!PRIMARY_TEMPLATE_P (template))
6782 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6783 if (TREE_CODE (t) == FUNCTION_DECL
6784 /* Implicitly generated member functions will not have template
6785 information; they are not instantiations, but instead are
6786 created "fresh" for each instantiation. */
6787 && DECL_TEMPLATE_INFO (t))
6788 tsubst_default_arguments (t);
6790 popclass ();
6791 pop_from_top_level ();
6792 pop_deferring_access_checks ();
6793 pop_tinst_level ();
6795 /* The vtable for a template class can be emitted in any translation
6796 unit in which the class is instantiated. When there is no key
6797 method, however, finish_struct_1 will already have added TYPE to
6798 the keyed_classes list. */
6799 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6800 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6802 return type;
6805 static tree
6806 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6808 tree r;
6810 if (!t)
6811 r = t;
6812 else if (TYPE_P (t))
6813 r = tsubst (t, args, complain, in_decl);
6814 else
6816 r = tsubst_expr (t, args, complain, in_decl,
6817 /*integral_constant_expression_p=*/true);
6818 r = fold_non_dependent_expr (r);
6820 return r;
6823 /* Substitute ARGS into T, which is an pack expansion
6824 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
6825 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
6826 (if only a partial substitution could be performed) or
6827 ERROR_MARK_NODE if there was an error. */
6828 tree
6829 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
6830 tree in_decl)
6832 tree pattern;
6833 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
6834 tree first_arg_pack; int i, len = -1;
6835 tree result;
6836 int incomplete = 0;
6838 gcc_assert (PACK_EXPANSION_P (t));
6839 pattern = PACK_EXPANSION_PATTERN (t);
6841 /* Determine the argument packs that will instantiate the parameter
6842 packs used in the expansion expression. While we're at it,
6843 compute the number of arguments to be expanded and make sure it
6844 is consistent. */
6845 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6846 pack = TREE_CHAIN (pack))
6848 tree parm_pack = TREE_VALUE (pack);
6849 tree arg_pack = NULL_TREE;
6850 tree orig_arg = NULL_TREE;
6852 if (TREE_CODE (parm_pack) == PARM_DECL)
6854 if (local_specializations)
6855 arg_pack = retrieve_local_specialization (parm_pack);
6857 else
6859 int level, idx, levels;
6860 template_parm_level_and_index (parm_pack, &level, &idx);
6862 levels = TMPL_ARGS_DEPTH (args);
6863 if (level <= levels)
6864 arg_pack = TMPL_ARG (args, level, idx);
6867 orig_arg = arg_pack;
6868 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
6869 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
6871 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
6872 /* This can only happen if we forget to expand an argument
6873 pack somewhere else. Just return an error, silently. */
6875 result = make_tree_vec (1);
6876 TREE_VEC_ELT (result, 0) = error_mark_node;
6877 return result;
6880 if (arg_pack)
6882 int my_len =
6883 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
6885 /* It's all-or-nothing with incomplete argument packs. */
6886 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
6887 return error_mark_node;
6889 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
6890 incomplete = 1;
6892 if (len < 0)
6894 len = my_len;
6895 first_arg_pack = arg_pack;
6897 else if (len != my_len)
6899 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
6900 error ("mismatched argument pack lengths while expanding "
6901 "%<%T%>",
6902 pattern);
6903 else
6904 error ("mismatched argument pack lengths while expanding "
6905 "%<%E%>",
6906 pattern);
6907 return error_mark_node;
6910 /* Keep track of the parameter packs and their corresponding
6911 argument packs. */
6912 packs = tree_cons (parm_pack, arg_pack, packs);
6913 TREE_TYPE (packs) = orig_arg;
6915 else
6916 /* We can't substitute for this parameter pack. */
6917 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
6918 TREE_VALUE (pack),
6919 unsubstituted_packs);
6922 /* We cannot expand this expansion expression, because we don't have
6923 all of the argument packs we need. Substitute into the pattern
6924 and return a PACK_EXPANSION_*. The caller will need to deal with
6925 that. */
6926 if (unsubstituted_packs)
6927 return make_pack_expansion (tsubst (pattern, args, complain,
6928 in_decl));
6930 /* We could not find any argument packs that work. */
6931 if (len < 0)
6932 return error_mark_node;
6934 /* For each argument in each argument pack, substitute into the
6935 pattern. */
6936 result = make_tree_vec (len + incomplete);
6937 for (i = 0; i < len + incomplete; ++i)
6939 /* For parameter pack, change the substitution of the parameter
6940 pack to the ith argument in its argument pack, then expand
6941 the pattern. */
6942 for (pack = packs; pack; pack = TREE_CHAIN (pack))
6944 tree parm = TREE_PURPOSE (pack);
6946 if (TREE_CODE (parm) == PARM_DECL)
6948 /* Select the Ith argument from the pack. */
6949 tree arg = make_node (ARGUMENT_PACK_SELECT);
6950 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
6951 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
6952 mark_used (parm);
6953 register_local_specialization (arg, parm);
6955 else
6957 tree value = parm;
6958 int idx, level;
6959 template_parm_level_and_index (parm, &level, &idx);
6961 if (i < len)
6963 /* Select the Ith argument from the pack. */
6964 value = make_node (ARGUMENT_PACK_SELECT);
6965 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
6966 ARGUMENT_PACK_SELECT_INDEX (value) = i;
6969 /* Update the corresponding argument. */
6970 TMPL_ARG (args, level, idx) = value;
6974 /* Substitute into the PATTERN with the altered arguments. */
6975 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
6976 TREE_VEC_ELT (result, i) =
6977 tsubst_expr (pattern, args, complain, in_decl,
6978 /*integral_constant_expression_p=*/false);
6979 else
6980 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
6982 if (i == len)
6983 /* When we have incomplete argument packs, the last "expanded"
6984 result is itself a pack expansion, which allows us
6985 to deduce more arguments. */
6986 TREE_VEC_ELT (result, i) =
6987 make_pack_expansion (TREE_VEC_ELT (result, i));
6989 if (TREE_VEC_ELT (result, i) == error_mark_node)
6991 result = error_mark_node;
6992 break;
6996 /* Update ARGS to restore the substitution from parameter packs to
6997 their argument packs. */
6998 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7000 tree parm = TREE_PURPOSE (pack);
7002 if (TREE_CODE (parm) == PARM_DECL)
7003 register_local_specialization (TREE_TYPE (pack), parm);
7004 else
7006 int idx, level;
7007 template_parm_level_and_index (parm, &level, &idx);
7009 /* Update the corresponding argument. */
7010 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7011 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7012 TREE_TYPE (pack);
7013 else
7014 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7018 return result;
7021 /* Substitute ARGS into the vector or list of template arguments T. */
7023 static tree
7024 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7026 tree orig_t = t;
7027 int len = TREE_VEC_LENGTH (t);
7028 int need_new = 0, i, expanded_len_adjust = 0, out;
7029 tree *elts = (tree *) alloca (len * sizeof (tree));
7031 for (i = 0; i < len; i++)
7033 tree orig_arg = TREE_VEC_ELT (t, i);
7034 tree new_arg;
7036 if (TREE_CODE (orig_arg) == TREE_VEC)
7037 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7038 else if (PACK_EXPANSION_P (orig_arg))
7040 /* Substitute into an expansion expression. */
7041 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7043 if (TREE_CODE (new_arg) == TREE_VEC)
7044 /* Add to the expanded length adjustment the number of
7045 expanded arguments. We subtract one from this
7046 measurement, because the argument pack expression
7047 itself is already counted as 1 in
7048 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7049 the argument pack is empty. */
7050 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7052 else if (ARGUMENT_PACK_P (orig_arg))
7054 /* Substitute into each of the arguments. */
7055 new_arg = make_node (TREE_CODE (orig_arg));
7057 SET_ARGUMENT_PACK_ARGS (
7058 new_arg,
7059 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7060 args, complain, in_decl));
7062 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7063 new_arg = error_mark_node;
7065 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7066 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7067 complain, in_decl);
7068 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7070 if (TREE_TYPE (new_arg) == error_mark_node)
7071 new_arg = error_mark_node;
7074 else
7075 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7077 if (new_arg == error_mark_node)
7078 return error_mark_node;
7080 elts[i] = new_arg;
7081 if (new_arg != orig_arg)
7082 need_new = 1;
7085 if (!need_new)
7086 return t;
7088 /* Make space for the expanded arguments coming from template
7089 argument packs. */
7090 t = make_tree_vec (len + expanded_len_adjust);
7091 for (i = 0, out = 0; i < len; i++)
7093 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7094 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7095 && TREE_CODE (elts[i]) == TREE_VEC)
7097 int idx;
7099 /* Now expand the template argument pack "in place". */
7100 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7101 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7103 else
7105 TREE_VEC_ELT (t, out) = elts[i];
7106 out++;
7110 return t;
7113 /* Return the result of substituting ARGS into the template parameters
7114 given by PARMS. If there are m levels of ARGS and m + n levels of
7115 PARMS, then the result will contain n levels of PARMS. For
7116 example, if PARMS is `template <class T> template <class U>
7117 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7118 result will be `template <int*, double, class V>'. */
7120 static tree
7121 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7123 tree r = NULL_TREE;
7124 tree* new_parms;
7126 /* When substituting into a template, we must set
7127 PROCESSING_TEMPLATE_DECL as the template parameters may be
7128 dependent if they are based on one-another, and the dependency
7129 predicates are short-circuit outside of templates. */
7130 ++processing_template_decl;
7132 for (new_parms = &r;
7133 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7134 new_parms = &(TREE_CHAIN (*new_parms)),
7135 parms = TREE_CHAIN (parms))
7137 tree new_vec =
7138 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7139 int i;
7141 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7143 tree tuple;
7144 tree default_value;
7145 tree parm_decl;
7147 if (parms == error_mark_node)
7148 continue;
7150 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7152 if (tuple == error_mark_node)
7153 continue;
7155 default_value = TREE_PURPOSE (tuple);
7156 parm_decl = TREE_VALUE (tuple);
7158 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7159 if (TREE_CODE (parm_decl) == PARM_DECL
7160 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7161 parm_decl = error_mark_node;
7162 default_value = tsubst_template_arg (default_value, args,
7163 complain, NULL_TREE);
7165 tuple = build_tree_list (default_value, parm_decl);
7166 TREE_VEC_ELT (new_vec, i) = tuple;
7169 *new_parms =
7170 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7171 - TMPL_ARGS_DEPTH (args)),
7172 new_vec, NULL_TREE);
7175 --processing_template_decl;
7177 return r;
7180 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7181 type T. If T is not an aggregate or enumeration type, it is
7182 handled as if by tsubst. IN_DECL is as for tsubst. If
7183 ENTERING_SCOPE is nonzero, T is the context for a template which
7184 we are presently tsubst'ing. Return the substituted value. */
7186 static tree
7187 tsubst_aggr_type (tree t,
7188 tree args,
7189 tsubst_flags_t complain,
7190 tree in_decl,
7191 int entering_scope)
7193 if (t == NULL_TREE)
7194 return NULL_TREE;
7196 switch (TREE_CODE (t))
7198 case RECORD_TYPE:
7199 if (TYPE_PTRMEMFUNC_P (t))
7200 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7202 /* Else fall through. */
7203 case ENUMERAL_TYPE:
7204 case UNION_TYPE:
7205 if (TYPE_TEMPLATE_INFO (t))
7207 tree argvec;
7208 tree context;
7209 tree r;
7210 bool saved_skip_evaluation;
7212 /* In "sizeof(X<I>)" we need to evaluate "I". */
7213 saved_skip_evaluation = skip_evaluation;
7214 skip_evaluation = false;
7216 /* First, determine the context for the type we are looking
7217 up. */
7218 context = TYPE_CONTEXT (t);
7219 if (context)
7220 context = tsubst_aggr_type (context, args, complain,
7221 in_decl, /*entering_scope=*/1);
7223 /* Then, figure out what arguments are appropriate for the
7224 type we are trying to find. For example, given:
7226 template <class T> struct S;
7227 template <class T, class U> void f(T, U) { S<U> su; }
7229 and supposing that we are instantiating f<int, double>,
7230 then our ARGS will be {int, double}, but, when looking up
7231 S we only want {double}. */
7232 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7233 complain, in_decl);
7234 if (argvec == error_mark_node)
7235 r = error_mark_node;
7236 else
7238 r = lookup_template_class (t, argvec, in_decl, context,
7239 entering_scope, complain);
7240 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7243 skip_evaluation = saved_skip_evaluation;
7245 return r;
7247 else
7248 /* This is not a template type, so there's nothing to do. */
7249 return t;
7251 default:
7252 return tsubst (t, args, complain, in_decl);
7256 /* Substitute into the default argument ARG (a default argument for
7257 FN), which has the indicated TYPE. */
7259 tree
7260 tsubst_default_argument (tree fn, tree type, tree arg)
7262 tree saved_class_ptr = NULL_TREE;
7263 tree saved_class_ref = NULL_TREE;
7265 /* This default argument came from a template. Instantiate the
7266 default argument here, not in tsubst. In the case of
7267 something like:
7269 template <class T>
7270 struct S {
7271 static T t();
7272 void f(T = t());
7275 we must be careful to do name lookup in the scope of S<T>,
7276 rather than in the current class. */
7277 push_access_scope (fn);
7278 /* The "this" pointer is not valid in a default argument. */
7279 if (cfun)
7281 saved_class_ptr = current_class_ptr;
7282 cp_function_chain->x_current_class_ptr = NULL_TREE;
7283 saved_class_ref = current_class_ref;
7284 cp_function_chain->x_current_class_ref = NULL_TREE;
7287 push_deferring_access_checks(dk_no_deferred);
7288 /* The default argument expression may cause implicitly defined
7289 member functions to be synthesized, which will result in garbage
7290 collection. We must treat this situation as if we were within
7291 the body of function so as to avoid collecting live data on the
7292 stack. */
7293 ++function_depth;
7294 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
7295 tf_warning_or_error, NULL_TREE,
7296 /*integral_constant_expression_p=*/false);
7297 --function_depth;
7298 pop_deferring_access_checks();
7300 /* Restore the "this" pointer. */
7301 if (cfun)
7303 cp_function_chain->x_current_class_ptr = saved_class_ptr;
7304 cp_function_chain->x_current_class_ref = saved_class_ref;
7307 pop_access_scope (fn);
7309 /* Make sure the default argument is reasonable. */
7310 arg = check_default_argument (type, arg);
7312 return arg;
7315 /* Substitute into all the default arguments for FN. */
7317 static void
7318 tsubst_default_arguments (tree fn)
7320 tree arg;
7321 tree tmpl_args;
7323 tmpl_args = DECL_TI_ARGS (fn);
7325 /* If this function is not yet instantiated, we certainly don't need
7326 its default arguments. */
7327 if (uses_template_parms (tmpl_args))
7328 return;
7330 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
7331 arg;
7332 arg = TREE_CHAIN (arg))
7333 if (TREE_PURPOSE (arg))
7334 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
7335 TREE_VALUE (arg),
7336 TREE_PURPOSE (arg));
7339 /* Substitute the ARGS into the T, which is a _DECL. Return the
7340 result of the substitution. Issue error and warning messages under
7341 control of COMPLAIN. */
7343 static tree
7344 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
7346 location_t saved_loc;
7347 tree r = NULL_TREE;
7348 tree in_decl = t;
7350 /* Set the filename and linenumber to improve error-reporting. */
7351 saved_loc = input_location;
7352 input_location = DECL_SOURCE_LOCATION (t);
7354 switch (TREE_CODE (t))
7356 case TEMPLATE_DECL:
7358 /* We can get here when processing a member function template,
7359 member class template, and template template parameter of
7360 a template class. */
7361 tree decl = DECL_TEMPLATE_RESULT (t);
7362 tree spec;
7363 tree tmpl_args;
7364 tree full_args;
7366 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7368 /* Template template parameter is treated here. */
7369 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7370 if (new_type == error_mark_node)
7371 return error_mark_node;
7373 r = copy_decl (t);
7374 TREE_CHAIN (r) = NULL_TREE;
7375 TREE_TYPE (r) = new_type;
7376 DECL_TEMPLATE_RESULT (r)
7377 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
7378 DECL_TEMPLATE_PARMS (r)
7379 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7380 complain);
7381 TYPE_NAME (new_type) = r;
7382 break;
7385 /* We might already have an instance of this template.
7386 The ARGS are for the surrounding class type, so the
7387 full args contain the tsubst'd args for the context,
7388 plus the innermost args from the template decl. */
7389 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
7390 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
7391 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7392 /* Because this is a template, the arguments will still be
7393 dependent, even after substitution. If
7394 PROCESSING_TEMPLATE_DECL is not set, the dependency
7395 predicates will short-circuit. */
7396 ++processing_template_decl;
7397 full_args = tsubst_template_args (tmpl_args, args,
7398 complain, in_decl);
7399 --processing_template_decl;
7400 if (full_args == error_mark_node)
7401 return error_mark_node;
7403 /* tsubst_template_args doesn't copy the vector if
7404 nothing changed. But, *something* should have
7405 changed. */
7406 gcc_assert (full_args != tmpl_args);
7408 spec = retrieve_specialization (t, full_args,
7409 /*class_specializations_p=*/true);
7410 if (spec != NULL_TREE)
7412 r = spec;
7413 break;
7416 /* Make a new template decl. It will be similar to the
7417 original, but will record the current template arguments.
7418 We also create a new function declaration, which is just
7419 like the old one, but points to this new template, rather
7420 than the old one. */
7421 r = copy_decl (t);
7422 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
7423 TREE_CHAIN (r) = NULL_TREE;
7425 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
7427 if (TREE_CODE (decl) == TYPE_DECL)
7429 tree new_type;
7430 ++processing_template_decl;
7431 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7432 --processing_template_decl;
7433 if (new_type == error_mark_node)
7434 return error_mark_node;
7436 TREE_TYPE (r) = new_type;
7437 CLASSTYPE_TI_TEMPLATE (new_type) = r;
7438 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
7439 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
7440 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
7442 else
7444 tree new_decl;
7445 ++processing_template_decl;
7446 new_decl = tsubst (decl, args, complain, in_decl);
7447 --processing_template_decl;
7448 if (new_decl == error_mark_node)
7449 return error_mark_node;
7451 DECL_TEMPLATE_RESULT (r) = new_decl;
7452 DECL_TI_TEMPLATE (new_decl) = r;
7453 TREE_TYPE (r) = TREE_TYPE (new_decl);
7454 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
7455 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
7458 SET_DECL_IMPLICIT_INSTANTIATION (r);
7459 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
7460 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
7462 /* The template parameters for this new template are all the
7463 template parameters for the old template, except the
7464 outermost level of parameters. */
7465 DECL_TEMPLATE_PARMS (r)
7466 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7467 complain);
7469 if (PRIMARY_TEMPLATE_P (t))
7470 DECL_PRIMARY_TEMPLATE (r) = r;
7472 if (TREE_CODE (decl) != TYPE_DECL)
7473 /* Record this non-type partial instantiation. */
7474 register_specialization (r, t,
7475 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
7476 false);
7478 break;
7480 case FUNCTION_DECL:
7482 tree ctx;
7483 tree argvec = NULL_TREE;
7484 tree *friends;
7485 tree gen_tmpl;
7486 tree type;
7487 int member;
7488 int args_depth;
7489 int parms_depth;
7491 /* Nobody should be tsubst'ing into non-template functions. */
7492 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
7494 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
7496 tree spec;
7497 bool dependent_p;
7499 /* If T is not dependent, just return it. We have to
7500 increment PROCESSING_TEMPLATE_DECL because
7501 value_dependent_expression_p assumes that nothing is
7502 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7503 ++processing_template_decl;
7504 dependent_p = value_dependent_expression_p (t);
7505 --processing_template_decl;
7506 if (!dependent_p)
7507 return t;
7509 /* Calculate the most general template of which R is a
7510 specialization, and the complete set of arguments used to
7511 specialize R. */
7512 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7513 argvec = tsubst_template_args (DECL_TI_ARGS
7514 (DECL_TEMPLATE_RESULT (gen_tmpl)),
7515 args, complain, in_decl);
7517 /* Check to see if we already have this specialization. */
7518 spec = retrieve_specialization (gen_tmpl, argvec,
7519 /*class_specializations_p=*/false);
7521 if (spec)
7523 r = spec;
7524 break;
7527 /* We can see more levels of arguments than parameters if
7528 there was a specialization of a member template, like
7529 this:
7531 template <class T> struct S { template <class U> void f(); }
7532 template <> template <class U> void S<int>::f(U);
7534 Here, we'll be substituting into the specialization,
7535 because that's where we can find the code we actually
7536 want to generate, but we'll have enough arguments for
7537 the most general template.
7539 We also deal with the peculiar case:
7541 template <class T> struct S {
7542 template <class U> friend void f();
7544 template <class U> void f() {}
7545 template S<int>;
7546 template void f<double>();
7548 Here, the ARGS for the instantiation of will be {int,
7549 double}. But, we only need as many ARGS as there are
7550 levels of template parameters in CODE_PATTERN. We are
7551 careful not to get fooled into reducing the ARGS in
7552 situations like:
7554 template <class T> struct S { template <class U> void f(U); }
7555 template <class T> template <> void S<T>::f(int) {}
7557 which we can spot because the pattern will be a
7558 specialization in this case. */
7559 args_depth = TMPL_ARGS_DEPTH (args);
7560 parms_depth =
7561 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
7562 if (args_depth > parms_depth
7563 && !DECL_TEMPLATE_SPECIALIZATION (t))
7564 args = get_innermost_template_args (args, parms_depth);
7566 else
7568 /* This special case arises when we have something like this:
7570 template <class T> struct S {
7571 friend void f<int>(int, double);
7574 Here, the DECL_TI_TEMPLATE for the friend declaration
7575 will be an IDENTIFIER_NODE. We are being called from
7576 tsubst_friend_function, and we want only to create a
7577 new decl (R) with appropriate types so that we can call
7578 determine_specialization. */
7579 gen_tmpl = NULL_TREE;
7582 if (DECL_CLASS_SCOPE_P (t))
7584 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
7585 member = 2;
7586 else
7587 member = 1;
7588 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
7589 complain, t, /*entering_scope=*/1);
7591 else
7593 member = 0;
7594 ctx = DECL_CONTEXT (t);
7596 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7597 if (type == error_mark_node)
7598 return error_mark_node;
7600 /* We do NOT check for matching decls pushed separately at this
7601 point, as they may not represent instantiations of this
7602 template, and in any case are considered separate under the
7603 discrete model. */
7604 r = copy_decl (t);
7605 DECL_USE_TEMPLATE (r) = 0;
7606 TREE_TYPE (r) = type;
7607 /* Clear out the mangled name and RTL for the instantiation. */
7608 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7609 SET_DECL_RTL (r, NULL_RTX);
7610 DECL_INITIAL (r) = NULL_TREE;
7611 DECL_CONTEXT (r) = ctx;
7613 if (member && DECL_CONV_FN_P (r))
7614 /* Type-conversion operator. Reconstruct the name, in
7615 case it's the name of one of the template's parameters. */
7616 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
7618 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
7619 complain, t);
7620 DECL_RESULT (r) = NULL_TREE;
7622 TREE_STATIC (r) = 0;
7623 TREE_PUBLIC (r) = TREE_PUBLIC (t);
7624 DECL_EXTERNAL (r) = 1;
7625 /* If this is an instantiation of a function with internal
7626 linkage, we already know what object file linkage will be
7627 assigned to the instantiation. */
7628 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
7629 DECL_DEFER_OUTPUT (r) = 0;
7630 TREE_CHAIN (r) = NULL_TREE;
7631 DECL_PENDING_INLINE_INFO (r) = 0;
7632 DECL_PENDING_INLINE_P (r) = 0;
7633 DECL_SAVED_TREE (r) = NULL_TREE;
7634 TREE_USED (r) = 0;
7635 if (DECL_CLONED_FUNCTION (r))
7637 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
7638 args, complain, t);
7639 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
7640 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
7643 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
7644 this in the special friend case mentioned above where
7645 GEN_TMPL is NULL. */
7646 if (gen_tmpl)
7648 DECL_TEMPLATE_INFO (r)
7649 = tree_cons (gen_tmpl, argvec, NULL_TREE);
7650 SET_DECL_IMPLICIT_INSTANTIATION (r);
7651 register_specialization (r, gen_tmpl, argvec, false);
7653 /* We're not supposed to instantiate default arguments
7654 until they are called, for a template. But, for a
7655 declaration like:
7657 template <class T> void f ()
7658 { extern void g(int i = T()); }
7660 we should do the substitution when the template is
7661 instantiated. We handle the member function case in
7662 instantiate_class_template since the default arguments
7663 might refer to other members of the class. */
7664 if (!member
7665 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7666 && !uses_template_parms (argvec))
7667 tsubst_default_arguments (r);
7669 else
7670 DECL_TEMPLATE_INFO (r) = NULL_TREE;
7672 /* Copy the list of befriending classes. */
7673 for (friends = &DECL_BEFRIENDING_CLASSES (r);
7674 *friends;
7675 friends = &TREE_CHAIN (*friends))
7677 *friends = copy_node (*friends);
7678 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
7679 args, complain,
7680 in_decl);
7683 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
7685 maybe_retrofit_in_chrg (r);
7686 if (DECL_CONSTRUCTOR_P (r))
7687 grok_ctor_properties (ctx, r);
7688 /* If this is an instantiation of a member template, clone it.
7689 If it isn't, that'll be handled by
7690 clone_constructors_and_destructors. */
7691 if (PRIMARY_TEMPLATE_P (gen_tmpl))
7692 clone_function_decl (r, /*update_method_vec_p=*/0);
7694 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
7695 && !grok_op_properties (r, (complain & tf_error) != 0))
7696 return error_mark_node;
7698 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
7699 SET_DECL_FRIEND_CONTEXT (r,
7700 tsubst (DECL_FRIEND_CONTEXT (t),
7701 args, complain, in_decl));
7703 /* Possibly limit visibility based on template args. */
7704 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7705 if (DECL_VISIBILITY_SPECIFIED (t))
7707 DECL_VISIBILITY_SPECIFIED (r) = 0;
7708 DECL_ATTRIBUTES (r)
7709 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7711 determine_visibility (r);
7713 break;
7715 case PARM_DECL:
7717 tree type = NULL_TREE;
7718 int i, len = 1;
7719 tree expanded_types = NULL_TREE;
7720 tree prev_r = NULL_TREE;
7721 tree first_r = NULL_TREE;
7723 if (FUNCTION_PARAMETER_PACK_P (t))
7725 /* If there is a local specialization that isn't a
7726 parameter pack, it means that we're doing a "simple"
7727 substitution from inside tsubst_pack_expansion. Just
7728 return the local specialization (which will be a single
7729 parm). */
7730 tree spec = NULL_TREE;
7731 if (local_specializations)
7732 spec = retrieve_local_specialization (t);
7733 if (spec
7734 && TREE_CODE (spec) == PARM_DECL
7735 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
7736 return spec;
7738 /* Expand the TYPE_PACK_EXPANSION that provides the types for
7739 the parameters in this function parameter pack. */
7740 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
7741 complain, in_decl);
7742 if (TREE_CODE (expanded_types) == TREE_VEC)
7744 len = TREE_VEC_LENGTH (expanded_types);
7746 /* Zero-length parameter packs are boring. Just substitute
7747 into the chain. */
7748 if (len == 0)
7749 return tsubst (TREE_CHAIN (t), args, complain,
7750 TREE_CHAIN (t));
7752 else
7754 /* All we did was update the type. Make a note of that. */
7755 type = expanded_types;
7756 expanded_types = NULL_TREE;
7760 /* Loop through all of the parameter's we'll build. When T is
7761 a function parameter pack, LEN is the number of expanded
7762 types in EXPANDED_TYPES; otherwise, LEN is 1. */
7763 r = NULL_TREE;
7764 for (i = 0; i < len; ++i)
7766 prev_r = r;
7767 r = copy_node (t);
7768 if (DECL_TEMPLATE_PARM_P (t))
7769 SET_DECL_TEMPLATE_PARM_P (r);
7771 if (expanded_types)
7772 /* We're on the Ith parameter of the function parameter
7773 pack. */
7775 /* Get the Ith type. */
7776 type = TREE_VEC_ELT (expanded_types, i);
7778 if (DECL_NAME (r))
7779 /* Rename the parameter to include the index. */
7780 DECL_NAME (r) =
7781 make_ith_pack_parameter_name (DECL_NAME (r), i);
7783 else if (!type)
7784 /* We're dealing with a normal parameter. */
7785 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7787 type = type_decays_to (type);
7788 TREE_TYPE (r) = type;
7789 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7791 if (DECL_INITIAL (r))
7793 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
7794 DECL_INITIAL (r) = TREE_TYPE (r);
7795 else
7796 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
7797 complain, in_decl);
7800 DECL_CONTEXT (r) = NULL_TREE;
7802 if (!DECL_TEMPLATE_PARM_P (r))
7803 DECL_ARG_TYPE (r) = type_passed_as (type);
7805 /* Keep track of the first new parameter we
7806 generate. That's what will be returned to the
7807 caller. */
7808 if (!first_r)
7809 first_r = r;
7811 /* Build a proper chain of parameters when substituting
7812 into a function parameter pack. */
7813 if (prev_r)
7814 TREE_CHAIN (prev_r) = r;
7817 if (TREE_CHAIN (t))
7818 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
7819 complain, TREE_CHAIN (t));
7821 /* FIRST_R contains the start of the chain we've built. */
7822 r = first_r;
7824 break;
7826 case FIELD_DECL:
7828 tree type;
7830 r = copy_decl (t);
7831 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7832 if (type == error_mark_node)
7833 return error_mark_node;
7834 TREE_TYPE (r) = type;
7835 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7837 /* DECL_INITIAL gives the number of bits in a bit-field. */
7838 DECL_INITIAL (r)
7839 = tsubst_expr (DECL_INITIAL (t), args,
7840 complain, in_decl,
7841 /*integral_constant_expression_p=*/true);
7842 /* We don't have to set DECL_CONTEXT here; it is set by
7843 finish_member_declaration. */
7844 TREE_CHAIN (r) = NULL_TREE;
7845 if (VOID_TYPE_P (type))
7846 error ("instantiation of %q+D as type %qT", r, type);
7848 break;
7850 case USING_DECL:
7851 /* We reach here only for member using decls. */
7852 if (DECL_DEPENDENT_P (t))
7854 r = do_class_using_decl
7855 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
7856 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
7857 if (!r)
7858 r = error_mark_node;
7860 else
7862 r = copy_node (t);
7863 TREE_CHAIN (r) = NULL_TREE;
7865 break;
7867 case TYPE_DECL:
7868 case VAR_DECL:
7870 tree argvec = NULL_TREE;
7871 tree gen_tmpl = NULL_TREE;
7872 tree spec;
7873 tree tmpl = NULL_TREE;
7874 tree ctx;
7875 tree type = NULL_TREE;
7876 bool local_p;
7878 if (TREE_CODE (t) == TYPE_DECL)
7880 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7881 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
7882 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
7884 /* If this is the canonical decl, we don't have to
7885 mess with instantiations, and often we can't (for
7886 typename, template type parms and such). Note that
7887 TYPE_NAME is not correct for the above test if
7888 we've copied the type for a typedef. */
7889 r = TYPE_NAME (type);
7890 break;
7894 /* Check to see if we already have the specialization we
7895 need. */
7896 spec = NULL_TREE;
7897 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
7899 /* T is a static data member or namespace-scope entity.
7900 We have to substitute into namespace-scope variables
7901 (even though such entities are never templates) because
7902 of cases like:
7904 template <class T> void f() { extern T t; }
7906 where the entity referenced is not known until
7907 instantiation time. */
7908 local_p = false;
7909 ctx = DECL_CONTEXT (t);
7910 if (DECL_CLASS_SCOPE_P (t))
7912 ctx = tsubst_aggr_type (ctx, args,
7913 complain,
7914 in_decl, /*entering_scope=*/1);
7915 /* If CTX is unchanged, then T is in fact the
7916 specialization we want. That situation occurs when
7917 referencing a static data member within in its own
7918 class. We can use pointer equality, rather than
7919 same_type_p, because DECL_CONTEXT is always
7920 canonical. */
7921 if (ctx == DECL_CONTEXT (t))
7922 spec = t;
7925 if (!spec)
7927 tmpl = DECL_TI_TEMPLATE (t);
7928 gen_tmpl = most_general_template (tmpl);
7929 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
7930 spec = (retrieve_specialization
7931 (gen_tmpl, argvec,
7932 /*class_specializations_p=*/false));
7935 else
7937 /* A local variable. */
7938 local_p = true;
7939 /* Subsequent calls to pushdecl will fill this in. */
7940 ctx = NULL_TREE;
7941 spec = retrieve_local_specialization (t);
7943 /* If we already have the specialization we need, there is
7944 nothing more to do. */
7945 if (spec)
7947 r = spec;
7948 break;
7951 /* Create a new node for the specialization we need. */
7952 r = copy_decl (t);
7953 if (TREE_CODE (r) == VAR_DECL)
7955 /* Even if the original location is out of scope, the
7956 newly substituted one is not. */
7957 DECL_DEAD_FOR_LOCAL (r) = 0;
7958 DECL_INITIALIZED_P (r) = 0;
7959 DECL_TEMPLATE_INSTANTIATED (r) = 0;
7960 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7961 if (type == error_mark_node)
7962 return error_mark_node;
7963 if (TREE_CODE (type) == FUNCTION_TYPE)
7965 /* It may seem that this case cannot occur, since:
7967 typedef void f();
7968 void g() { f x; }
7970 declares a function, not a variable. However:
7972 typedef void f();
7973 template <typename T> void g() { T t; }
7974 template void g<f>();
7976 is an attempt to declare a variable with function
7977 type. */
7978 error ("variable %qD has function type",
7979 /* R is not yet sufficiently initialized, so we
7980 just use its name. */
7981 DECL_NAME (r));
7982 return error_mark_node;
7984 type = complete_type (type);
7985 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
7986 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
7987 type = check_var_type (DECL_NAME (r), type);
7989 if (DECL_HAS_VALUE_EXPR_P (t))
7991 tree ve = DECL_VALUE_EXPR (t);
7992 ve = tsubst_expr (ve, args, complain, in_decl,
7993 /*constant_expression_p=*/false);
7994 SET_DECL_VALUE_EXPR (r, ve);
7997 else if (DECL_SELF_REFERENCE_P (t))
7998 SET_DECL_SELF_REFERENCE_P (r);
7999 TREE_TYPE (r) = type;
8000 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8001 DECL_CONTEXT (r) = ctx;
8002 /* Clear out the mangled name and RTL for the instantiation. */
8003 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8004 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8005 SET_DECL_RTL (r, NULL_RTX);
8006 /* The initializer must not be expanded until it is required;
8007 see [temp.inst]. */
8008 DECL_INITIAL (r) = NULL_TREE;
8009 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8010 SET_DECL_RTL (r, NULL_RTX);
8011 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8012 if (TREE_CODE (r) == VAR_DECL)
8014 /* Possibly limit visibility based on template args. */
8015 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8016 if (DECL_VISIBILITY_SPECIFIED (t))
8018 DECL_VISIBILITY_SPECIFIED (r) = 0;
8019 DECL_ATTRIBUTES (r)
8020 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8022 determine_visibility (r);
8025 if (!local_p)
8027 /* A static data member declaration is always marked
8028 external when it is declared in-class, even if an
8029 initializer is present. We mimic the non-template
8030 processing here. */
8031 DECL_EXTERNAL (r) = 1;
8033 register_specialization (r, gen_tmpl, argvec, false);
8034 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8035 SET_DECL_IMPLICIT_INSTANTIATION (r);
8037 else
8038 register_local_specialization (r, t);
8040 TREE_CHAIN (r) = NULL_TREE;
8041 layout_decl (r, 0);
8043 break;
8045 default:
8046 gcc_unreachable ();
8049 /* Restore the file and line information. */
8050 input_location = saved_loc;
8052 return r;
8055 /* Substitute into the ARG_TYPES of a function type. */
8057 static tree
8058 tsubst_arg_types (tree arg_types,
8059 tree args,
8060 tsubst_flags_t complain,
8061 tree in_decl)
8063 tree remaining_arg_types;
8064 tree type = NULL_TREE;
8065 int i = 1;
8066 tree expanded_args = NULL_TREE;
8067 tree default_arg;
8069 if (!arg_types || arg_types == void_list_node)
8070 return arg_types;
8072 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8073 args, complain, in_decl);
8074 if (remaining_arg_types == error_mark_node)
8075 return error_mark_node;
8077 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8079 /* For a pack expansion, perform substitution on the
8080 entire expression. Later on, we'll handle the arguments
8081 one-by-one. */
8082 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8083 args, complain, in_decl);
8085 if (TREE_CODE (expanded_args) == TREE_VEC)
8086 /* So that we'll spin through the parameters, one by one. */
8087 i = TREE_VEC_LENGTH (expanded_args);
8088 else
8090 /* We only partially substituted into the parameter
8091 pack. Our type is TYPE_PACK_EXPANSION. */
8092 type = expanded_args;
8093 expanded_args = NULL_TREE;
8097 while (i > 0) {
8098 --i;
8100 if (expanded_args)
8101 type = TREE_VEC_ELT (expanded_args, i);
8102 else if (!type)
8103 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8105 if (type == error_mark_node)
8106 return error_mark_node;
8107 if (VOID_TYPE_P (type))
8109 if (complain & tf_error)
8111 error ("invalid parameter type %qT", type);
8112 if (in_decl)
8113 error ("in declaration %q+D", in_decl);
8115 return error_mark_node;
8118 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8119 top-level qualifiers as required. */
8120 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8122 /* We do not substitute into default arguments here. The standard
8123 mandates that they be instantiated only when needed, which is
8124 done in build_over_call. */
8125 default_arg = TREE_PURPOSE (arg_types);
8127 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8129 /* We've instantiated a template before its default arguments
8130 have been parsed. This can happen for a nested template
8131 class, and is not an error unless we require the default
8132 argument in a call of this function. */
8133 remaining_arg_types =
8134 tree_cons (default_arg, type, remaining_arg_types);
8135 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8136 remaining_arg_types);
8138 else
8139 remaining_arg_types =
8140 hash_tree_cons (default_arg, type, remaining_arg_types);
8143 return remaining_arg_types;
8146 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8147 *not* handle the exception-specification for FNTYPE, because the
8148 initial substitution of explicitly provided template parameters
8149 during argument deduction forbids substitution into the
8150 exception-specification:
8152 [temp.deduct]
8154 All references in the function type of the function template to the
8155 corresponding template parameters are replaced by the specified tem-
8156 plate argument values. If a substitution in a template parameter or
8157 in the function type of the function template results in an invalid
8158 type, type deduction fails. [Note: The equivalent substitution in
8159 exception specifications is done only when the function is instanti-
8160 ated, at which point a program is ill-formed if the substitution
8161 results in an invalid type.] */
8163 static tree
8164 tsubst_function_type (tree t,
8165 tree args,
8166 tsubst_flags_t complain,
8167 tree in_decl)
8169 tree return_type;
8170 tree arg_types;
8171 tree fntype;
8173 /* The TYPE_CONTEXT is not used for function/method types. */
8174 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8176 /* Substitute the return type. */
8177 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8178 if (return_type == error_mark_node)
8179 return error_mark_node;
8180 /* The standard does not presently indicate that creation of a
8181 function type with an invalid return type is a deduction failure.
8182 However, that is clearly analogous to creating an array of "void"
8183 or a reference to a reference. This is core issue #486. */
8184 if (TREE_CODE (return_type) == ARRAY_TYPE
8185 || TREE_CODE (return_type) == FUNCTION_TYPE)
8187 if (complain & tf_error)
8189 if (TREE_CODE (return_type) == ARRAY_TYPE)
8190 error ("function returning an array");
8191 else
8192 error ("function returning a function");
8194 return error_mark_node;
8197 /* Substitute the argument types. */
8198 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8199 complain, in_decl);
8200 if (arg_types == error_mark_node)
8201 return error_mark_node;
8203 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
8204 && in_decl != NULL_TREE
8205 && !TREE_NO_WARNING (in_decl)
8206 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
8207 warning (OPT_Wreturn_type,
8208 "type qualifiers ignored on function return type");
8210 /* Construct a new type node and return it. */
8211 if (TREE_CODE (t) == FUNCTION_TYPE)
8212 fntype = build_function_type (return_type, arg_types);
8213 else
8215 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8216 if (! IS_AGGR_TYPE (r))
8218 /* [temp.deduct]
8220 Type deduction may fail for any of the following
8221 reasons:
8223 -- Attempting to create "pointer to member of T" when T
8224 is not a class type. */
8225 if (complain & tf_error)
8226 error ("creating pointer to member function of non-class type %qT",
8228 return error_mark_node;
8231 fntype = build_method_type_directly (r, return_type,
8232 TREE_CHAIN (arg_types));
8234 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8235 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8237 return fntype;
8240 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8241 ARGS into that specification, and return the substituted
8242 specification. If there is no specification, return NULL_TREE. */
8244 static tree
8245 tsubst_exception_specification (tree fntype,
8246 tree args,
8247 tsubst_flags_t complain,
8248 tree in_decl)
8250 tree specs;
8251 tree new_specs;
8253 specs = TYPE_RAISES_EXCEPTIONS (fntype);
8254 new_specs = NULL_TREE;
8255 if (specs)
8257 if (! TREE_VALUE (specs))
8258 new_specs = specs;
8259 else
8260 while (specs)
8262 tree spec;
8263 int i, len = 1;
8264 tree expanded_specs = NULL_TREE;
8266 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
8268 /* Expand the pack expansion type. */
8269 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
8270 args, complain,
8271 in_decl);
8272 len = TREE_VEC_LENGTH (expanded_specs);
8275 for (i = 0; i < len; ++i)
8277 if (expanded_specs)
8278 spec = TREE_VEC_ELT (expanded_specs, i);
8279 else
8280 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
8281 if (spec == error_mark_node)
8282 return spec;
8283 new_specs = add_exception_specifier (new_specs, spec,
8284 complain);
8287 specs = TREE_CHAIN (specs);
8290 return new_specs;
8293 /* Take the tree structure T and replace template parameters used
8294 therein with the argument vector ARGS. IN_DECL is an associated
8295 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8296 Issue error and warning messages under control of COMPLAIN. Note
8297 that we must be relatively non-tolerant of extensions here, in
8298 order to preserve conformance; if we allow substitutions that
8299 should not be allowed, we may allow argument deductions that should
8300 not succeed, and therefore report ambiguous overload situations
8301 where there are none. In theory, we could allow the substitution,
8302 but indicate that it should have failed, and allow our caller to
8303 make sure that the right thing happens, but we don't try to do this
8304 yet.
8306 This function is used for dealing with types, decls and the like;
8307 for expressions, use tsubst_expr or tsubst_copy. */
8309 static tree
8310 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8312 tree type, r;
8314 if (t == NULL_TREE || t == error_mark_node
8315 || t == integer_type_node
8316 || t == void_type_node
8317 || t == char_type_node
8318 || t == unknown_type_node
8319 || TREE_CODE (t) == NAMESPACE_DECL)
8320 return t;
8322 if (DECL_P (t))
8323 return tsubst_decl (t, args, complain);
8325 if (TREE_CODE (t) == IDENTIFIER_NODE)
8326 type = IDENTIFIER_TYPE_VALUE (t);
8327 else
8328 type = TREE_TYPE (t);
8330 gcc_assert (type != unknown_type_node);
8332 if (type
8333 && TREE_CODE (t) != TYPENAME_TYPE
8334 && TREE_CODE (t) != IDENTIFIER_NODE
8335 && TREE_CODE (t) != FUNCTION_TYPE
8336 && TREE_CODE (t) != METHOD_TYPE)
8337 type = tsubst (type, args, complain, in_decl);
8338 if (type == error_mark_node)
8339 return error_mark_node;
8341 switch (TREE_CODE (t))
8343 case RECORD_TYPE:
8344 case UNION_TYPE:
8345 case ENUMERAL_TYPE:
8346 return tsubst_aggr_type (t, args, complain, in_decl,
8347 /*entering_scope=*/0);
8349 case ERROR_MARK:
8350 case IDENTIFIER_NODE:
8351 case VOID_TYPE:
8352 case REAL_TYPE:
8353 case COMPLEX_TYPE:
8354 case VECTOR_TYPE:
8355 case BOOLEAN_TYPE:
8356 case INTEGER_CST:
8357 case REAL_CST:
8358 case STRING_CST:
8359 return t;
8361 case INTEGER_TYPE:
8362 if (t == integer_type_node)
8363 return t;
8365 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
8366 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
8367 return t;
8370 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
8372 max = tsubst_expr (omax, args, complain, in_decl,
8373 /*integral_constant_expression_p=*/false);
8374 max = fold_decl_constant_value (max);
8376 if (TREE_CODE (max) != INTEGER_CST
8377 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
8378 && !at_function_scope_p ())
8380 if (complain & tf_error)
8381 error ("array bound is not an integer constant");
8382 return error_mark_node;
8385 /* [temp.deduct]
8387 Type deduction may fail for any of the following
8388 reasons:
8390 Attempting to create an array with a size that is
8391 zero or negative. */
8392 if (integer_zerop (max) && !(complain & tf_error))
8393 /* We must fail if performing argument deduction (as
8394 indicated by the state of complain), so that
8395 another substitution can be found. */
8396 return error_mark_node;
8397 else if (TREE_CODE (max) == INTEGER_CST
8398 && INT_CST_LT (max, integer_zero_node))
8400 if (complain & tf_error)
8401 error ("creating array with negative size (%qE)", max);
8403 return error_mark_node;
8406 return compute_array_index_type (NULL_TREE, max);
8409 case TEMPLATE_TYPE_PARM:
8410 case TEMPLATE_TEMPLATE_PARM:
8411 case BOUND_TEMPLATE_TEMPLATE_PARM:
8412 case TEMPLATE_PARM_INDEX:
8414 int idx;
8415 int level;
8416 int levels;
8417 tree arg = NULL_TREE;
8419 r = NULL_TREE;
8421 gcc_assert (TREE_VEC_LENGTH (args) > 0);
8422 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8423 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
8424 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8426 idx = TEMPLATE_TYPE_IDX (t);
8427 level = TEMPLATE_TYPE_LEVEL (t);
8429 else
8431 idx = TEMPLATE_PARM_IDX (t);
8432 level = TEMPLATE_PARM_LEVEL (t);
8435 levels = TMPL_ARGS_DEPTH (args);
8436 if (level <= levels)
8438 arg = TMPL_ARG (args, level, idx);
8440 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
8441 /* See through ARGUMENT_PACK_SELECT arguments. */
8442 arg = ARGUMENT_PACK_SELECT_ARG (arg);
8445 if (arg == error_mark_node)
8446 return error_mark_node;
8447 else if (arg != NULL_TREE)
8449 if (ARGUMENT_PACK_P (arg))
8450 /* If ARG is an argument pack, we don't actually want to
8451 perform a substitution here, because substitutions
8452 for argument packs are only done
8453 element-by-element. We can get to this point when
8454 substituting the type of a non-type template
8455 parameter pack, when that type actually contains
8456 template parameter packs from an outer template, e.g.,
8458 template<typename... Types> struct A {
8459 template<Types... Values> struct B { };
8460 }; */
8461 return t;
8463 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
8465 int quals;
8466 gcc_assert (TYPE_P (arg));
8468 /* cv-quals from the template are discarded when
8469 substituting in a function or reference type. */
8470 if (TREE_CODE (arg) == FUNCTION_TYPE
8471 || TREE_CODE (arg) == METHOD_TYPE
8472 || TREE_CODE (arg) == REFERENCE_TYPE)
8473 quals = cp_type_quals (arg);
8474 else
8475 quals = cp_type_quals (arg) | cp_type_quals (t);
8477 return cp_build_qualified_type_real
8478 (arg, quals, complain | tf_ignore_bad_quals);
8480 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8482 /* We are processing a type constructed from a
8483 template template parameter. */
8484 tree argvec = tsubst (TYPE_TI_ARGS (t),
8485 args, complain, in_decl);
8486 if (argvec == error_mark_node)
8487 return error_mark_node;
8489 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
8490 are resolving nested-types in the signature of a
8491 member function templates. Otherwise ARG is a
8492 TEMPLATE_DECL and is the real template to be
8493 instantiated. */
8494 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8495 arg = TYPE_NAME (arg);
8497 r = lookup_template_class (arg,
8498 argvec, in_decl,
8499 DECL_CONTEXT (arg),
8500 /*entering_scope=*/0,
8501 complain);
8502 return cp_build_qualified_type_real
8503 (r, TYPE_QUALS (t), complain);
8505 else
8506 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
8507 return arg;
8510 if (level == 1)
8511 /* This can happen during the attempted tsubst'ing in
8512 unify. This means that we don't yet have any information
8513 about the template parameter in question. */
8514 return t;
8516 /* If we get here, we must have been looking at a parm for a
8517 more deeply nested template. Make a new version of this
8518 template parameter, but with a lower level. */
8519 switch (TREE_CODE (t))
8521 case TEMPLATE_TYPE_PARM:
8522 case TEMPLATE_TEMPLATE_PARM:
8523 case BOUND_TEMPLATE_TEMPLATE_PARM:
8524 if (cp_type_quals (t))
8526 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
8527 r = cp_build_qualified_type_real
8528 (r, cp_type_quals (t),
8529 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8530 ? tf_ignore_bad_quals : 0));
8532 else
8534 r = copy_type (t);
8535 TEMPLATE_TYPE_PARM_INDEX (r)
8536 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
8537 r, levels);
8538 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
8539 TYPE_MAIN_VARIANT (r) = r;
8540 TYPE_POINTER_TO (r) = NULL_TREE;
8541 TYPE_REFERENCE_TO (r) = NULL_TREE;
8543 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
8544 /* We have reduced the level of the template
8545 template parameter, but not the levels of its
8546 template parameters, so canonical_type_parameter
8547 will not be able to find the canonical template
8548 template parameter for this level. Thus, we
8549 require structural equality checking to compare
8550 TEMPLATE_TEMPLATE_PARMs. */
8551 SET_TYPE_STRUCTURAL_EQUALITY (r);
8552 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
8553 SET_TYPE_STRUCTURAL_EQUALITY (r);
8554 else
8555 TYPE_CANONICAL (r) = canonical_type_parameter (r);
8557 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8559 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
8560 complain, in_decl);
8561 if (argvec == error_mark_node)
8562 return error_mark_node;
8564 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
8565 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
8568 break;
8570 case TEMPLATE_PARM_INDEX:
8571 r = reduce_template_parm_level (t, type, levels);
8572 break;
8574 default:
8575 gcc_unreachable ();
8578 return r;
8581 case TREE_LIST:
8583 tree purpose, value, chain;
8585 if (t == void_list_node)
8586 return t;
8588 purpose = TREE_PURPOSE (t);
8589 if (purpose)
8591 purpose = tsubst (purpose, args, complain, in_decl);
8592 if (purpose == error_mark_node)
8593 return error_mark_node;
8595 value = TREE_VALUE (t);
8596 if (value)
8598 value = tsubst (value, args, complain, in_decl);
8599 if (value == error_mark_node)
8600 return error_mark_node;
8602 chain = TREE_CHAIN (t);
8603 if (chain && chain != void_type_node)
8605 chain = tsubst (chain, args, complain, in_decl);
8606 if (chain == error_mark_node)
8607 return error_mark_node;
8609 if (purpose == TREE_PURPOSE (t)
8610 && value == TREE_VALUE (t)
8611 && chain == TREE_CHAIN (t))
8612 return t;
8613 return hash_tree_cons (purpose, value, chain);
8616 case TREE_BINFO:
8617 /* We should never be tsubsting a binfo. */
8618 gcc_unreachable ();
8620 case TREE_VEC:
8621 /* A vector of template arguments. */
8622 gcc_assert (!type);
8623 return tsubst_template_args (t, args, complain, in_decl);
8625 case POINTER_TYPE:
8626 case REFERENCE_TYPE:
8628 enum tree_code code;
8630 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8631 return t;
8633 code = TREE_CODE (t);
8636 /* [temp.deduct]
8638 Type deduction may fail for any of the following
8639 reasons:
8641 -- Attempting to create a pointer to reference type.
8642 -- Attempting to create a reference to a reference type or
8643 a reference to void. */
8644 if (TREE_CODE (type) == REFERENCE_TYPE
8645 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
8647 static location_t last_loc;
8649 /* We keep track of the last time we issued this error
8650 message to avoid spewing a ton of messages during a
8651 single bad template instantiation. */
8652 if (complain & tf_error
8653 #ifdef USE_MAPPED_LOCATION
8654 && last_loc != input_location
8655 #else
8656 && (last_loc.line != input_line
8657 || last_loc.file != input_filename)
8658 #endif
8661 if (TREE_CODE (type) == VOID_TYPE)
8662 error ("forming reference to void");
8663 else
8664 error ("forming %s to reference type %qT",
8665 (code == POINTER_TYPE) ? "pointer" : "reference",
8666 type);
8667 last_loc = input_location;
8670 return error_mark_node;
8672 else if (code == POINTER_TYPE)
8674 r = build_pointer_type (type);
8675 if (TREE_CODE (type) == METHOD_TYPE)
8676 r = build_ptrmemfunc_type (r);
8678 else
8679 r = build_reference_type (type);
8680 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8682 if (r != error_mark_node)
8683 /* Will this ever be needed for TYPE_..._TO values? */
8684 layout_type (r);
8686 return r;
8688 case OFFSET_TYPE:
8690 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
8691 if (r == error_mark_node || !IS_AGGR_TYPE (r))
8693 /* [temp.deduct]
8695 Type deduction may fail for any of the following
8696 reasons:
8698 -- Attempting to create "pointer to member of T" when T
8699 is not a class type. */
8700 if (complain & tf_error)
8701 error ("creating pointer to member of non-class type %qT", r);
8702 return error_mark_node;
8704 if (TREE_CODE (type) == REFERENCE_TYPE)
8706 if (complain & tf_error)
8707 error ("creating pointer to member reference type %qT", type);
8708 return error_mark_node;
8710 if (TREE_CODE (type) == VOID_TYPE)
8712 if (complain & tf_error)
8713 error ("creating pointer to member of type void");
8714 return error_mark_node;
8716 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
8717 if (TREE_CODE (type) == FUNCTION_TYPE)
8719 /* The type of the implicit object parameter gets its
8720 cv-qualifiers from the FUNCTION_TYPE. */
8721 tree method_type;
8722 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
8723 cp_type_quals (type));
8724 tree memptr;
8725 method_type = build_method_type_directly (this_type,
8726 TREE_TYPE (type),
8727 TYPE_ARG_TYPES (type));
8728 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
8729 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
8730 complain);
8732 else
8733 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
8734 TYPE_QUALS (t),
8735 complain);
8737 case FUNCTION_TYPE:
8738 case METHOD_TYPE:
8740 tree fntype;
8741 tree specs;
8742 fntype = tsubst_function_type (t, args, complain, in_decl);
8743 if (fntype == error_mark_node)
8744 return error_mark_node;
8746 /* Substitute the exception specification. */
8747 specs = tsubst_exception_specification (t, args, complain,
8748 in_decl);
8749 if (specs == error_mark_node)
8750 return error_mark_node;
8751 if (specs)
8752 fntype = build_exception_variant (fntype, specs);
8753 return fntype;
8755 case ARRAY_TYPE:
8757 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
8758 if (domain == error_mark_node)
8759 return error_mark_node;
8761 /* As an optimization, we avoid regenerating the array type if
8762 it will obviously be the same as T. */
8763 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8764 return t;
8766 /* These checks should match the ones in grokdeclarator.
8768 [temp.deduct]
8770 The deduction may fail for any of the following reasons:
8772 -- Attempting to create an array with an element type that
8773 is void, a function type, or a reference type, or [DR337]
8774 an abstract class type. */
8775 if (TREE_CODE (type) == VOID_TYPE
8776 || TREE_CODE (type) == FUNCTION_TYPE
8777 || TREE_CODE (type) == REFERENCE_TYPE)
8779 if (complain & tf_error)
8780 error ("creating array of %qT", type);
8781 return error_mark_node;
8783 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
8785 if (complain & tf_error)
8786 error ("creating array of %qT, which is an abstract class type",
8787 type);
8788 return error_mark_node;
8791 r = build_cplus_array_type (type, domain);
8792 return r;
8795 case PLUS_EXPR:
8796 case MINUS_EXPR:
8798 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8799 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
8801 if (e1 == error_mark_node || e2 == error_mark_node)
8802 return error_mark_node;
8804 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
8807 case NEGATE_EXPR:
8808 case NOP_EXPR:
8810 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8811 if (e == error_mark_node)
8812 return error_mark_node;
8814 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
8817 case TYPENAME_TYPE:
8819 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
8820 in_decl, /*entering_scope=*/1);
8821 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
8822 complain, in_decl);
8824 if (ctx == error_mark_node || f == error_mark_node)
8825 return error_mark_node;
8827 if (!IS_AGGR_TYPE (ctx))
8829 if (complain & tf_error)
8830 error ("%qT is not a class, struct, or union type", ctx);
8831 return error_mark_node;
8833 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
8835 /* Normally, make_typename_type does not require that the CTX
8836 have complete type in order to allow things like:
8838 template <class T> struct S { typename S<T>::X Y; };
8840 But, such constructs have already been resolved by this
8841 point, so here CTX really should have complete type, unless
8842 it's a partial instantiation. */
8843 ctx = complete_type (ctx);
8844 if (!COMPLETE_TYPE_P (ctx))
8846 if (complain & tf_error)
8847 cxx_incomplete_type_error (NULL_TREE, ctx);
8848 return error_mark_node;
8852 f = make_typename_type (ctx, f, typename_type,
8853 (complain & tf_error) | tf_keep_type_decl);
8854 if (f == error_mark_node)
8855 return f;
8856 if (TREE_CODE (f) == TYPE_DECL)
8858 complain |= tf_ignore_bad_quals;
8859 f = TREE_TYPE (f);
8862 if (TREE_CODE (f) != TYPENAME_TYPE)
8864 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
8865 error ("%qT resolves to %qT, which is not an enumeration type",
8866 t, f);
8867 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
8868 error ("%qT resolves to %qT, which is is not a class type",
8869 t, f);
8872 return cp_build_qualified_type_real
8873 (f, cp_type_quals (f) | cp_type_quals (t), complain);
8876 case UNBOUND_CLASS_TEMPLATE:
8878 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
8879 in_decl, /*entering_scope=*/1);
8880 tree name = TYPE_IDENTIFIER (t);
8881 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
8883 if (ctx == error_mark_node || name == error_mark_node)
8884 return error_mark_node;
8886 if (parm_list)
8887 parm_list = tsubst_template_parms (parm_list, args, complain);
8888 return make_unbound_class_template (ctx, name, parm_list, complain);
8891 case INDIRECT_REF:
8892 case ADDR_EXPR:
8893 case CALL_EXPR:
8894 gcc_unreachable ();
8896 case ARRAY_REF:
8898 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8899 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
8900 /*integral_constant_expression_p=*/false);
8901 if (e1 == error_mark_node || e2 == error_mark_node)
8902 return error_mark_node;
8904 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
8907 case SCOPE_REF:
8909 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8910 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
8911 if (e1 == error_mark_node || e2 == error_mark_node)
8912 return error_mark_node;
8914 return build_qualified_name (/*type=*/NULL_TREE,
8915 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
8918 case TYPEOF_TYPE:
8920 tree type;
8922 type = finish_typeof (tsubst_expr
8923 (TYPEOF_TYPE_EXPR (t), args,
8924 complain, in_decl,
8925 /*integral_constant_expression_p=*/false));
8926 return cp_build_qualified_type_real (type,
8927 cp_type_quals (t)
8928 | cp_type_quals (type),
8929 complain);
8932 case TYPE_ARGUMENT_PACK:
8933 case NONTYPE_ARGUMENT_PACK:
8935 tree r = make_node (TREE_CODE (t));
8936 tree packed_out =
8937 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
8938 args,
8939 complain,
8940 in_decl);
8941 SET_ARGUMENT_PACK_ARGS (r, packed_out);
8943 /* For template nontype argument packs, also substitute into
8944 the type. */
8945 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
8946 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
8948 return r;
8950 break;
8952 default:
8953 sorry ("use of %qs in template",
8954 tree_code_name [(int) TREE_CODE (t)]);
8955 return error_mark_node;
8959 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
8960 type of the expression on the left-hand side of the "." or "->"
8961 operator. */
8963 static tree
8964 tsubst_baselink (tree baselink, tree object_type,
8965 tree args, tsubst_flags_t complain, tree in_decl)
8967 tree name;
8968 tree qualifying_scope;
8969 tree fns;
8970 tree optype;
8971 tree template_args = 0;
8972 bool template_id_p = false;
8974 /* A baselink indicates a function from a base class. Both the
8975 BASELINK_ACCESS_BINFO and the base class referenced may
8976 indicate bases of the template class, rather than the
8977 instantiated class. In addition, lookups that were not
8978 ambiguous before may be ambiguous now. Therefore, we perform
8979 the lookup again. */
8980 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
8981 qualifying_scope = tsubst (qualifying_scope, args,
8982 complain, in_decl);
8983 fns = BASELINK_FUNCTIONS (baselink);
8984 optype = BASELINK_OPTYPE (baselink);
8985 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
8987 template_id_p = true;
8988 template_args = TREE_OPERAND (fns, 1);
8989 fns = TREE_OPERAND (fns, 0);
8990 if (template_args)
8991 template_args = tsubst_template_args (template_args, args,
8992 complain, in_decl);
8994 name = DECL_NAME (get_first_fn (fns));
8995 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
8997 /* If lookup found a single function, mark it as used at this
8998 point. (If it lookup found multiple functions the one selected
8999 later by overload resolution will be marked as used at that
9000 point.) */
9001 if (BASELINK_P (baselink))
9002 fns = BASELINK_FUNCTIONS (baselink);
9003 if (!template_id_p && !really_overloaded_fn (fns))
9004 mark_used (OVL_CURRENT (fns));
9006 /* Add back the template arguments, if present. */
9007 if (BASELINK_P (baselink) && template_id_p)
9008 BASELINK_FUNCTIONS (baselink)
9009 = build_nt (TEMPLATE_ID_EXPR,
9010 BASELINK_FUNCTIONS (baselink),
9011 template_args);
9012 /* Update the conversion operator type. */
9013 BASELINK_OPTYPE (baselink)
9014 = tsubst (optype, args, complain, in_decl);
9016 if (!object_type)
9017 object_type = current_class_type;
9018 return adjust_result_of_qualified_name_lookup (baselink,
9019 qualifying_scope,
9020 object_type);
9023 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9024 true if the qualified-id will be a postfix-expression in-and-of
9025 itself; false if more of the postfix-expression follows the
9026 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9027 of "&". */
9029 static tree
9030 tsubst_qualified_id (tree qualified_id, tree args,
9031 tsubst_flags_t complain, tree in_decl,
9032 bool done, bool address_p)
9034 tree expr;
9035 tree scope;
9036 tree name;
9037 bool is_template;
9038 tree template_args;
9040 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9042 /* Figure out what name to look up. */
9043 name = TREE_OPERAND (qualified_id, 1);
9044 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9046 is_template = true;
9047 template_args = TREE_OPERAND (name, 1);
9048 if (template_args)
9049 template_args = tsubst_template_args (template_args, args,
9050 complain, in_decl);
9051 name = TREE_OPERAND (name, 0);
9053 else
9055 is_template = false;
9056 template_args = NULL_TREE;
9059 /* Substitute into the qualifying scope. When there are no ARGS, we
9060 are just trying to simplify a non-dependent expression. In that
9061 case the qualifying scope may be dependent, and, in any case,
9062 substituting will not help. */
9063 scope = TREE_OPERAND (qualified_id, 0);
9064 if (args)
9066 scope = tsubst (scope, args, complain, in_decl);
9067 expr = tsubst_copy (name, args, complain, in_decl);
9069 else
9070 expr = name;
9072 if (dependent_type_p (scope))
9073 return build_qualified_name (/*type=*/NULL_TREE,
9074 scope, expr,
9075 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9077 if (!BASELINK_P (name) && !DECL_P (expr))
9079 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9080 /* If this were actually a destructor call, it would have been
9081 parsed as such by the parser. */
9082 expr = error_mark_node;
9083 else
9084 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9085 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9086 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9088 if (complain & tf_error)
9090 error ("dependent-name %qE is parsed as a non-type, but "
9091 "instantiation yields a type", qualified_id);
9092 inform ("say %<typename %E%> if a type is meant", qualified_id);
9094 return error_mark_node;
9098 if (DECL_P (expr))
9100 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9101 scope);
9102 /* Remember that there was a reference to this entity. */
9103 mark_used (expr);
9106 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9108 if (complain & tf_error)
9109 qualified_name_lookup_error (scope,
9110 TREE_OPERAND (qualified_id, 1),
9111 expr);
9112 return error_mark_node;
9115 if (is_template)
9116 expr = lookup_template_function (expr, template_args);
9118 if (expr == error_mark_node && complain & tf_error)
9119 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9120 expr);
9121 else if (TYPE_P (scope))
9123 expr = (adjust_result_of_qualified_name_lookup
9124 (expr, scope, current_class_type));
9125 expr = (finish_qualified_id_expr
9126 (scope, expr, done, address_p,
9127 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9128 /*template_arg_p=*/false));
9131 /* Expressions do not generally have reference type. */
9132 if (TREE_CODE (expr) != SCOPE_REF
9133 /* However, if we're about to form a pointer-to-member, we just
9134 want the referenced member referenced. */
9135 && TREE_CODE (expr) != OFFSET_REF)
9136 expr = convert_from_reference (expr);
9138 return expr;
9141 /* Like tsubst, but deals with expressions. This function just replaces
9142 template parms; to finish processing the resultant expression, use
9143 tsubst_expr. */
9145 static tree
9146 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9148 enum tree_code code;
9149 tree r;
9151 if (t == NULL_TREE || t == error_mark_node)
9152 return t;
9154 code = TREE_CODE (t);
9156 switch (code)
9158 case PARM_DECL:
9159 r = retrieve_local_specialization (t);
9160 gcc_assert (r != NULL);
9161 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
9162 r = ARGUMENT_PACK_SELECT_ARG (r);
9163 mark_used (r);
9164 return r;
9166 case CONST_DECL:
9168 tree enum_type;
9169 tree v;
9171 if (DECL_TEMPLATE_PARM_P (t))
9172 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
9173 /* There is no need to substitute into namespace-scope
9174 enumerators. */
9175 if (DECL_NAMESPACE_SCOPE_P (t))
9176 return t;
9177 /* If ARGS is NULL, then T is known to be non-dependent. */
9178 if (args == NULL_TREE)
9179 return integral_constant_value (t);
9181 /* Unfortunately, we cannot just call lookup_name here.
9182 Consider:
9184 template <int I> int f() {
9185 enum E { a = I };
9186 struct S { void g() { E e = a; } };
9189 When we instantiate f<7>::S::g(), say, lookup_name is not
9190 clever enough to find f<7>::a. */
9191 enum_type
9192 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
9193 /*entering_scope=*/0);
9195 for (v = TYPE_VALUES (enum_type);
9196 v != NULL_TREE;
9197 v = TREE_CHAIN (v))
9198 if (TREE_PURPOSE (v) == DECL_NAME (t))
9199 return TREE_VALUE (v);
9201 /* We didn't find the name. That should never happen; if
9202 name-lookup found it during preliminary parsing, we
9203 should find it again here during instantiation. */
9204 gcc_unreachable ();
9206 return t;
9208 case FIELD_DECL:
9209 if (DECL_CONTEXT (t))
9211 tree ctx;
9213 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
9214 /*entering_scope=*/1);
9215 if (ctx != DECL_CONTEXT (t))
9217 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
9218 if (!r)
9220 if (complain & tf_error)
9221 error ("using invalid field %qD", t);
9222 return error_mark_node;
9224 return r;
9228 return t;
9230 case VAR_DECL:
9231 case FUNCTION_DECL:
9232 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9233 || local_variable_p (t))
9234 t = tsubst (t, args, complain, in_decl);
9235 mark_used (t);
9236 return t;
9238 case BASELINK:
9239 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
9241 case TEMPLATE_DECL:
9242 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9243 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
9244 args, complain, in_decl);
9245 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
9246 return tsubst (t, args, complain, in_decl);
9247 else if (DECL_CLASS_SCOPE_P (t)
9248 && uses_template_parms (DECL_CONTEXT (t)))
9250 /* Template template argument like the following example need
9251 special treatment:
9253 template <template <class> class TT> struct C {};
9254 template <class T> struct D {
9255 template <class U> struct E {};
9256 C<E> c; // #1
9258 D<int> d; // #2
9260 We are processing the template argument `E' in #1 for
9261 the template instantiation #2. Originally, `E' is a
9262 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9263 have to substitute this with one having context `D<int>'. */
9265 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
9266 return lookup_field (context, DECL_NAME(t), 0, false);
9268 else
9269 /* Ordinary template template argument. */
9270 return t;
9272 case CAST_EXPR:
9273 case REINTERPRET_CAST_EXPR:
9274 case CONST_CAST_EXPR:
9275 case STATIC_CAST_EXPR:
9276 case DYNAMIC_CAST_EXPR:
9277 case NOP_EXPR:
9278 return build1
9279 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9280 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9282 case SIZEOF_EXPR:
9283 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
9285 /* We only want to compute the number of arguments. */
9286 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
9287 complain, in_decl);
9288 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
9290 /* Fall through */
9292 case INDIRECT_REF:
9293 case NEGATE_EXPR:
9294 case TRUTH_NOT_EXPR:
9295 case BIT_NOT_EXPR:
9296 case ADDR_EXPR:
9297 case UNARY_PLUS_EXPR: /* Unary + */
9298 case ALIGNOF_EXPR:
9299 case ARROW_EXPR:
9300 case THROW_EXPR:
9301 case TYPEID_EXPR:
9302 case REALPART_EXPR:
9303 case IMAGPART_EXPR:
9304 return build1
9305 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9306 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9308 case COMPONENT_REF:
9310 tree object;
9311 tree name;
9313 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
9314 name = TREE_OPERAND (t, 1);
9315 if (TREE_CODE (name) == BIT_NOT_EXPR)
9317 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9318 complain, in_decl);
9319 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9321 else if (TREE_CODE (name) == SCOPE_REF
9322 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
9324 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
9325 complain, in_decl);
9326 name = TREE_OPERAND (name, 1);
9327 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9328 complain, in_decl);
9329 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9330 name = build_qualified_name (/*type=*/NULL_TREE,
9331 base, name,
9332 /*template_p=*/false);
9334 else if (TREE_CODE (name) == BASELINK)
9335 name = tsubst_baselink (name,
9336 non_reference (TREE_TYPE (object)),
9337 args, complain,
9338 in_decl);
9339 else
9340 name = tsubst_copy (name, args, complain, in_decl);
9341 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
9344 case PLUS_EXPR:
9345 case MINUS_EXPR:
9346 case MULT_EXPR:
9347 case TRUNC_DIV_EXPR:
9348 case CEIL_DIV_EXPR:
9349 case FLOOR_DIV_EXPR:
9350 case ROUND_DIV_EXPR:
9351 case EXACT_DIV_EXPR:
9352 case BIT_AND_EXPR:
9353 case BIT_IOR_EXPR:
9354 case BIT_XOR_EXPR:
9355 case TRUNC_MOD_EXPR:
9356 case FLOOR_MOD_EXPR:
9357 case TRUTH_ANDIF_EXPR:
9358 case TRUTH_ORIF_EXPR:
9359 case TRUTH_AND_EXPR:
9360 case TRUTH_OR_EXPR:
9361 case RSHIFT_EXPR:
9362 case LSHIFT_EXPR:
9363 case RROTATE_EXPR:
9364 case LROTATE_EXPR:
9365 case EQ_EXPR:
9366 case NE_EXPR:
9367 case MAX_EXPR:
9368 case MIN_EXPR:
9369 case LE_EXPR:
9370 case GE_EXPR:
9371 case LT_EXPR:
9372 case GT_EXPR:
9373 case COMPOUND_EXPR:
9374 case DOTSTAR_EXPR:
9375 case MEMBER_REF:
9376 case PREDECREMENT_EXPR:
9377 case PREINCREMENT_EXPR:
9378 case POSTDECREMENT_EXPR:
9379 case POSTINCREMENT_EXPR:
9380 return build_nt
9381 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9382 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9384 case SCOPE_REF:
9385 return build_qualified_name (/*type=*/NULL_TREE,
9386 tsubst_copy (TREE_OPERAND (t, 0),
9387 args, complain, in_decl),
9388 tsubst_copy (TREE_OPERAND (t, 1),
9389 args, complain, in_decl),
9390 QUALIFIED_NAME_IS_TEMPLATE (t));
9392 case ARRAY_REF:
9393 return build_nt
9394 (ARRAY_REF,
9395 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9396 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9397 NULL_TREE, NULL_TREE);
9399 case CALL_EXPR:
9401 int n = VL_EXP_OPERAND_LENGTH (t);
9402 tree result = build_vl_exp (CALL_EXPR, n);
9403 int i;
9404 for (i = 0; i < n; i++)
9405 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
9406 complain, in_decl);
9407 return result;
9410 case COND_EXPR:
9411 case MODOP_EXPR:
9412 case PSEUDO_DTOR_EXPR:
9414 r = build_nt
9415 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9416 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9417 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9418 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9419 return r;
9422 case NEW_EXPR:
9424 r = build_nt
9425 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9426 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9427 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9428 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
9429 return r;
9432 case DELETE_EXPR:
9434 r = build_nt
9435 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9436 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9437 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
9438 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
9439 return r;
9442 case TEMPLATE_ID_EXPR:
9444 /* Substituted template arguments */
9445 tree fn = TREE_OPERAND (t, 0);
9446 tree targs = TREE_OPERAND (t, 1);
9448 fn = tsubst_copy (fn, args, complain, in_decl);
9449 if (targs)
9450 targs = tsubst_template_args (targs, args, complain, in_decl);
9452 return lookup_template_function (fn, targs);
9455 case TREE_LIST:
9457 tree purpose, value, chain;
9459 if (t == void_list_node)
9460 return t;
9462 purpose = TREE_PURPOSE (t);
9463 if (purpose)
9464 purpose = tsubst_copy (purpose, args, complain, in_decl);
9465 value = TREE_VALUE (t);
9466 if (value)
9467 value = tsubst_copy (value, args, complain, in_decl);
9468 chain = TREE_CHAIN (t);
9469 if (chain && chain != void_type_node)
9470 chain = tsubst_copy (chain, args, complain, in_decl);
9471 if (purpose == TREE_PURPOSE (t)
9472 && value == TREE_VALUE (t)
9473 && chain == TREE_CHAIN (t))
9474 return t;
9475 return tree_cons (purpose, value, chain);
9478 case RECORD_TYPE:
9479 case UNION_TYPE:
9480 case ENUMERAL_TYPE:
9481 case INTEGER_TYPE:
9482 case TEMPLATE_TYPE_PARM:
9483 case TEMPLATE_TEMPLATE_PARM:
9484 case BOUND_TEMPLATE_TEMPLATE_PARM:
9485 case TEMPLATE_PARM_INDEX:
9486 case POINTER_TYPE:
9487 case REFERENCE_TYPE:
9488 case OFFSET_TYPE:
9489 case FUNCTION_TYPE:
9490 case METHOD_TYPE:
9491 case ARRAY_TYPE:
9492 case TYPENAME_TYPE:
9493 case UNBOUND_CLASS_TEMPLATE:
9494 case TYPEOF_TYPE:
9495 case TYPE_DECL:
9496 return tsubst (t, args, complain, in_decl);
9498 case IDENTIFIER_NODE:
9499 if (IDENTIFIER_TYPENAME_P (t))
9501 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9502 return mangle_conv_op_name_for_type (new_type);
9504 else
9505 return t;
9507 case CONSTRUCTOR:
9508 /* This is handled by tsubst_copy_and_build. */
9509 gcc_unreachable ();
9511 case VA_ARG_EXPR:
9512 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
9513 in_decl),
9514 tsubst (TREE_TYPE (t), args, complain, in_decl));
9516 case CLEANUP_POINT_EXPR:
9517 /* We shouldn't have built any of these during initial template
9518 generation. Instead, they should be built during instantiation
9519 in response to the saved STMT_IS_FULL_EXPR_P setting. */
9520 gcc_unreachable ();
9522 case OFFSET_REF:
9523 mark_used (TREE_OPERAND (t, 1));
9524 return t;
9526 case EXPR_PACK_EXPANSION:
9527 error ("invalid use of pack expansion expression");
9528 return error_mark_node;
9530 case NONTYPE_ARGUMENT_PACK:
9531 error ("use %<...%> to expand argument pack");
9532 return error_mark_node;
9534 default:
9535 return t;
9539 /* Like tsubst_copy, but specifically for OpenMP clauses. */
9541 static tree
9542 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
9543 tree in_decl)
9545 tree new_clauses = NULL, nc, oc;
9547 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
9549 nc = copy_node (oc);
9550 OMP_CLAUSE_CHAIN (nc) = new_clauses;
9551 new_clauses = nc;
9553 switch (OMP_CLAUSE_CODE (nc))
9555 case OMP_CLAUSE_PRIVATE:
9556 case OMP_CLAUSE_SHARED:
9557 case OMP_CLAUSE_FIRSTPRIVATE:
9558 case OMP_CLAUSE_LASTPRIVATE:
9559 case OMP_CLAUSE_REDUCTION:
9560 case OMP_CLAUSE_COPYIN:
9561 case OMP_CLAUSE_COPYPRIVATE:
9562 case OMP_CLAUSE_IF:
9563 case OMP_CLAUSE_NUM_THREADS:
9564 case OMP_CLAUSE_SCHEDULE:
9565 OMP_CLAUSE_OPERAND (nc, 0)
9566 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
9567 in_decl, /*integral_constant_expression_p=*/false);
9568 break;
9569 case OMP_CLAUSE_NOWAIT:
9570 case OMP_CLAUSE_ORDERED:
9571 case OMP_CLAUSE_DEFAULT:
9572 break;
9573 default:
9574 gcc_unreachable ();
9578 return finish_omp_clauses (nreverse (new_clauses));
9581 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
9583 static tree
9584 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
9585 tree in_decl)
9587 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
9589 tree purpose, value, chain;
9591 if (t == NULL)
9592 return t;
9594 if (TREE_CODE (t) != TREE_LIST)
9595 return tsubst_copy_and_build (t, args, complain, in_decl,
9596 /*function_p=*/false,
9597 /*integral_constant_expression_p=*/false);
9599 if (t == void_list_node)
9600 return t;
9602 purpose = TREE_PURPOSE (t);
9603 if (purpose)
9604 purpose = RECUR (purpose);
9605 value = TREE_VALUE (t);
9606 if (value)
9607 value = RECUR (value);
9608 chain = TREE_CHAIN (t);
9609 if (chain && chain != void_type_node)
9610 chain = RECUR (chain);
9611 return tree_cons (purpose, value, chain);
9612 #undef RECUR
9615 /* Like tsubst_copy for expressions, etc. but also does semantic
9616 processing. */
9618 static tree
9619 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
9620 bool integral_constant_expression_p)
9622 #define RECUR(NODE) \
9623 tsubst_expr ((NODE), args, complain, in_decl, \
9624 integral_constant_expression_p)
9626 tree stmt, tmp;
9628 if (t == NULL_TREE || t == error_mark_node)
9629 return t;
9631 if (EXPR_HAS_LOCATION (t))
9632 input_location = EXPR_LOCATION (t);
9633 if (STATEMENT_CODE_P (TREE_CODE (t)))
9634 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
9636 switch (TREE_CODE (t))
9638 case STATEMENT_LIST:
9640 tree_stmt_iterator i;
9641 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
9642 RECUR (tsi_stmt (i));
9643 break;
9646 case CTOR_INITIALIZER:
9647 finish_mem_initializers (tsubst_initializer_list
9648 (TREE_OPERAND (t, 0), args));
9649 break;
9651 case RETURN_EXPR:
9652 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
9653 break;
9655 case EXPR_STMT:
9656 tmp = RECUR (EXPR_STMT_EXPR (t));
9657 if (EXPR_STMT_STMT_EXPR_RESULT (t))
9658 finish_stmt_expr_expr (tmp, cur_stmt_expr);
9659 else
9660 finish_expr_stmt (tmp);
9661 break;
9663 case USING_STMT:
9664 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9665 break;
9667 case DECL_EXPR:
9669 tree decl;
9670 tree init;
9672 decl = DECL_EXPR_DECL (t);
9673 if (TREE_CODE (decl) == LABEL_DECL)
9674 finish_label_decl (DECL_NAME (decl));
9675 else if (TREE_CODE (decl) == USING_DECL)
9677 tree scope = USING_DECL_SCOPE (decl);
9678 tree name = DECL_NAME (decl);
9679 tree decl;
9681 scope = RECUR (scope);
9682 decl = lookup_qualified_name (scope, name,
9683 /*is_type_p=*/false,
9684 /*complain=*/false);
9685 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
9686 qualified_name_lookup_error (scope, name, decl);
9687 else
9688 do_local_using_decl (decl, scope, name);
9690 else
9692 init = DECL_INITIAL (decl);
9693 decl = tsubst (decl, args, complain, in_decl);
9694 if (decl != error_mark_node)
9696 /* By marking the declaration as instantiated, we avoid
9697 trying to instantiate it. Since instantiate_decl can't
9698 handle local variables, and since we've already done
9699 all that needs to be done, that's the right thing to
9700 do. */
9701 if (TREE_CODE (decl) == VAR_DECL)
9702 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
9703 if (TREE_CODE (decl) == VAR_DECL
9704 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
9705 /* Anonymous aggregates are a special case. */
9706 finish_anon_union (decl);
9707 else
9709 maybe_push_decl (decl);
9710 if (TREE_CODE (decl) == VAR_DECL
9711 && DECL_PRETTY_FUNCTION_P (decl))
9713 /* For __PRETTY_FUNCTION__ we have to adjust the
9714 initializer. */
9715 const char *const name
9716 = cxx_printable_name (current_function_decl, 2);
9717 init = cp_fname_init (name, &TREE_TYPE (decl));
9719 else
9720 init = RECUR (init);
9721 finish_decl (decl, init, NULL_TREE);
9726 /* A DECL_EXPR can also be used as an expression, in the condition
9727 clause of an if/for/while construct. */
9728 return decl;
9731 case FOR_STMT:
9732 stmt = begin_for_stmt ();
9733 RECUR (FOR_INIT_STMT (t));
9734 finish_for_init_stmt (stmt);
9735 tmp = RECUR (FOR_COND (t));
9736 finish_for_cond (tmp, stmt);
9737 tmp = RECUR (FOR_EXPR (t));
9738 finish_for_expr (tmp, stmt);
9739 RECUR (FOR_BODY (t));
9740 finish_for_stmt (stmt);
9741 break;
9743 case WHILE_STMT:
9744 stmt = begin_while_stmt ();
9745 tmp = RECUR (WHILE_COND (t));
9746 finish_while_stmt_cond (tmp, stmt);
9747 RECUR (WHILE_BODY (t));
9748 finish_while_stmt (stmt);
9749 break;
9751 case DO_STMT:
9752 stmt = begin_do_stmt ();
9753 RECUR (DO_BODY (t));
9754 finish_do_body (stmt);
9755 tmp = RECUR (DO_COND (t));
9756 finish_do_stmt (tmp, stmt);
9757 break;
9759 case IF_STMT:
9760 stmt = begin_if_stmt ();
9761 tmp = RECUR (IF_COND (t));
9762 finish_if_stmt_cond (tmp, stmt);
9763 RECUR (THEN_CLAUSE (t));
9764 finish_then_clause (stmt);
9766 if (ELSE_CLAUSE (t))
9768 begin_else_clause (stmt);
9769 RECUR (ELSE_CLAUSE (t));
9770 finish_else_clause (stmt);
9773 finish_if_stmt (stmt);
9774 break;
9776 case BIND_EXPR:
9777 if (BIND_EXPR_BODY_BLOCK (t))
9778 stmt = begin_function_body ();
9779 else
9780 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
9781 ? BCS_TRY_BLOCK : 0);
9783 RECUR (BIND_EXPR_BODY (t));
9785 if (BIND_EXPR_BODY_BLOCK (t))
9786 finish_function_body (stmt);
9787 else
9788 finish_compound_stmt (stmt);
9789 break;
9791 case BREAK_STMT:
9792 finish_break_stmt ();
9793 break;
9795 case CONTINUE_STMT:
9796 finish_continue_stmt ();
9797 break;
9799 case SWITCH_STMT:
9800 stmt = begin_switch_stmt ();
9801 tmp = RECUR (SWITCH_STMT_COND (t));
9802 finish_switch_cond (tmp, stmt);
9803 RECUR (SWITCH_STMT_BODY (t));
9804 finish_switch_stmt (stmt);
9805 break;
9807 case CASE_LABEL_EXPR:
9808 finish_case_label (RECUR (CASE_LOW (t)),
9809 RECUR (CASE_HIGH (t)));
9810 break;
9812 case LABEL_EXPR:
9813 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
9814 break;
9816 case GOTO_EXPR:
9817 tmp = GOTO_DESTINATION (t);
9818 if (TREE_CODE (tmp) != LABEL_DECL)
9819 /* Computed goto's must be tsubst'd into. On the other hand,
9820 non-computed gotos must not be; the identifier in question
9821 will have no binding. */
9822 tmp = RECUR (tmp);
9823 else
9824 tmp = DECL_NAME (tmp);
9825 finish_goto_stmt (tmp);
9826 break;
9828 case ASM_EXPR:
9829 tmp = finish_asm_stmt
9830 (ASM_VOLATILE_P (t),
9831 RECUR (ASM_STRING (t)),
9832 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
9833 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
9834 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
9836 tree asm_expr = tmp;
9837 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
9838 asm_expr = TREE_OPERAND (asm_expr, 0);
9839 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
9841 break;
9843 case TRY_BLOCK:
9844 if (CLEANUP_P (t))
9846 stmt = begin_try_block ();
9847 RECUR (TRY_STMTS (t));
9848 finish_cleanup_try_block (stmt);
9849 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
9851 else
9853 tree compound_stmt = NULL_TREE;
9855 if (FN_TRY_BLOCK_P (t))
9856 stmt = begin_function_try_block (&compound_stmt);
9857 else
9858 stmt = begin_try_block ();
9860 RECUR (TRY_STMTS (t));
9862 if (FN_TRY_BLOCK_P (t))
9863 finish_function_try_block (stmt);
9864 else
9865 finish_try_block (stmt);
9867 RECUR (TRY_HANDLERS (t));
9868 if (FN_TRY_BLOCK_P (t))
9869 finish_function_handler_sequence (stmt, compound_stmt);
9870 else
9871 finish_handler_sequence (stmt);
9873 break;
9875 case HANDLER:
9877 tree decl = HANDLER_PARMS (t);
9879 if (decl)
9881 decl = tsubst (decl, args, complain, in_decl);
9882 /* Prevent instantiate_decl from trying to instantiate
9883 this variable. We've already done all that needs to be
9884 done. */
9885 if (decl != error_mark_node)
9886 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
9888 stmt = begin_handler ();
9889 finish_handler_parms (decl, stmt);
9890 RECUR (HANDLER_BODY (t));
9891 finish_handler (stmt);
9893 break;
9895 case TAG_DEFN:
9896 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
9897 break;
9899 case STATIC_ASSERT:
9901 tree condition =
9902 tsubst_expr (STATIC_ASSERT_CONDITION (t),
9903 args,
9904 complain, in_decl,
9905 /*integral_constant_expression_p=*/true);
9906 finish_static_assert (condition,
9907 STATIC_ASSERT_MESSAGE (t),
9908 STATIC_ASSERT_SOURCE_LOCATION (t),
9909 /*member_p=*/false);
9911 break;
9913 case OMP_PARALLEL:
9914 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
9915 args, complain, in_decl);
9916 stmt = begin_omp_parallel ();
9917 RECUR (OMP_PARALLEL_BODY (t));
9918 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
9919 = OMP_PARALLEL_COMBINED (t);
9920 break;
9922 case OMP_FOR:
9924 tree clauses, decl, init, cond, incr, body, pre_body;
9926 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
9927 args, complain, in_decl);
9928 init = OMP_FOR_INIT (t);
9929 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
9930 decl = RECUR (TREE_OPERAND (init, 0));
9931 init = RECUR (TREE_OPERAND (init, 1));
9932 cond = RECUR (OMP_FOR_COND (t));
9933 incr = RECUR (OMP_FOR_INCR (t));
9935 stmt = begin_omp_structured_block ();
9937 pre_body = push_stmt_list ();
9938 RECUR (OMP_FOR_PRE_BODY (t));
9939 pre_body = pop_stmt_list (pre_body);
9941 body = push_stmt_list ();
9942 RECUR (OMP_FOR_BODY (t));
9943 body = pop_stmt_list (body);
9945 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
9946 pre_body);
9947 if (t)
9948 OMP_FOR_CLAUSES (t) = clauses;
9950 add_stmt (finish_omp_structured_block (stmt));
9952 break;
9954 case OMP_SECTIONS:
9955 case OMP_SINGLE:
9956 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
9957 stmt = push_stmt_list ();
9958 RECUR (OMP_BODY (t));
9959 stmt = pop_stmt_list (stmt);
9961 t = copy_node (t);
9962 OMP_BODY (t) = stmt;
9963 OMP_CLAUSES (t) = tmp;
9964 add_stmt (t);
9965 break;
9967 case OMP_SECTION:
9968 case OMP_CRITICAL:
9969 case OMP_MASTER:
9970 case OMP_ORDERED:
9971 stmt = push_stmt_list ();
9972 RECUR (OMP_BODY (t));
9973 stmt = pop_stmt_list (stmt);
9975 t = copy_node (t);
9976 OMP_BODY (t) = stmt;
9977 add_stmt (t);
9978 break;
9980 case OMP_ATOMIC:
9981 if (OMP_ATOMIC_DEPENDENT_P (t))
9983 tree op1 = TREE_OPERAND (t, 1);
9984 tree lhs = RECUR (TREE_OPERAND (op1, 0));
9985 tree rhs = RECUR (TREE_OPERAND (op1, 1));
9986 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
9988 break;
9990 case EXPR_PACK_EXPANSION:
9991 error ("invalid use of pack expansion expression");
9992 return error_mark_node;
9994 case NONTYPE_ARGUMENT_PACK:
9995 error ("use %<...%> to expand argument pack");
9996 return error_mark_node;
9998 default:
9999 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
10001 return tsubst_copy_and_build (t, args, complain, in_decl,
10002 /*function_p=*/false,
10003 integral_constant_expression_p);
10006 return NULL_TREE;
10007 #undef RECUR
10010 /* T is a postfix-expression that is not being used in a function
10011 call. Return the substituted version of T. */
10013 static tree
10014 tsubst_non_call_postfix_expression (tree t, tree args,
10015 tsubst_flags_t complain,
10016 tree in_decl)
10018 if (TREE_CODE (t) == SCOPE_REF)
10019 t = tsubst_qualified_id (t, args, complain, in_decl,
10020 /*done=*/false, /*address_p=*/false);
10021 else
10022 t = tsubst_copy_and_build (t, args, complain, in_decl,
10023 /*function_p=*/false,
10024 /*integral_constant_expression_p=*/false);
10026 return t;
10029 /* Like tsubst but deals with expressions and performs semantic
10030 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10032 tree
10033 tsubst_copy_and_build (tree t,
10034 tree args,
10035 tsubst_flags_t complain,
10036 tree in_decl,
10037 bool function_p,
10038 bool integral_constant_expression_p)
10040 #define RECUR(NODE) \
10041 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10042 /*function_p=*/false, \
10043 integral_constant_expression_p)
10045 tree op1;
10047 if (t == NULL_TREE || t == error_mark_node)
10048 return t;
10050 switch (TREE_CODE (t))
10052 case USING_DECL:
10053 t = DECL_NAME (t);
10054 /* Fall through. */
10055 case IDENTIFIER_NODE:
10057 tree decl;
10058 cp_id_kind idk;
10059 bool non_integral_constant_expression_p;
10060 const char *error_msg;
10062 if (IDENTIFIER_TYPENAME_P (t))
10064 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10065 t = mangle_conv_op_name_for_type (new_type);
10068 /* Look up the name. */
10069 decl = lookup_name (t);
10071 /* By convention, expressions use ERROR_MARK_NODE to indicate
10072 failure, not NULL_TREE. */
10073 if (decl == NULL_TREE)
10074 decl = error_mark_node;
10076 decl = finish_id_expression (t, decl, NULL_TREE,
10077 &idk,
10078 integral_constant_expression_p,
10079 /*allow_non_integral_constant_expression_p=*/false,
10080 &non_integral_constant_expression_p,
10081 /*template_p=*/false,
10082 /*done=*/true,
10083 /*address_p=*/false,
10084 /*template_arg_p=*/false,
10085 &error_msg);
10086 if (error_msg)
10087 error (error_msg);
10088 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
10089 decl = unqualified_name_lookup_error (decl);
10090 return decl;
10093 case TEMPLATE_ID_EXPR:
10095 tree object;
10096 tree template = RECUR (TREE_OPERAND (t, 0));
10097 tree targs = TREE_OPERAND (t, 1);
10099 if (targs)
10100 targs = tsubst_template_args (targs, args, complain, in_decl);
10102 if (TREE_CODE (template) == COMPONENT_REF)
10104 object = TREE_OPERAND (template, 0);
10105 template = TREE_OPERAND (template, 1);
10107 else
10108 object = NULL_TREE;
10109 template = lookup_template_function (template, targs);
10111 if (object)
10112 return build3 (COMPONENT_REF, TREE_TYPE (template),
10113 object, template, NULL_TREE);
10114 else
10115 return baselink_for_fns (template);
10118 case INDIRECT_REF:
10120 tree r = RECUR (TREE_OPERAND (t, 0));
10122 if (REFERENCE_REF_P (t))
10124 /* A type conversion to reference type will be enclosed in
10125 such an indirect ref, but the substitution of the cast
10126 will have also added such an indirect ref. */
10127 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
10128 r = convert_from_reference (r);
10130 else
10131 r = build_x_indirect_ref (r, "unary *");
10132 return r;
10135 case NOP_EXPR:
10136 return build_nop
10137 (tsubst (TREE_TYPE (t), args, complain, in_decl),
10138 RECUR (TREE_OPERAND (t, 0)));
10140 case CAST_EXPR:
10141 case REINTERPRET_CAST_EXPR:
10142 case CONST_CAST_EXPR:
10143 case DYNAMIC_CAST_EXPR:
10144 case STATIC_CAST_EXPR:
10146 tree type;
10147 tree op;
10149 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10150 if (integral_constant_expression_p
10151 && !cast_valid_in_integral_constant_expression_p (type))
10153 error ("a cast to a type other than an integral or "
10154 "enumeration type cannot appear in a constant-expression");
10155 return error_mark_node;
10158 op = RECUR (TREE_OPERAND (t, 0));
10160 switch (TREE_CODE (t))
10162 case CAST_EXPR:
10163 return build_functional_cast (type, op);
10164 case REINTERPRET_CAST_EXPR:
10165 return build_reinterpret_cast (type, op);
10166 case CONST_CAST_EXPR:
10167 return build_const_cast (type, op);
10168 case DYNAMIC_CAST_EXPR:
10169 return build_dynamic_cast (type, op);
10170 case STATIC_CAST_EXPR:
10171 return build_static_cast (type, op);
10172 default:
10173 gcc_unreachable ();
10177 case POSTDECREMENT_EXPR:
10178 case POSTINCREMENT_EXPR:
10179 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10180 args, complain, in_decl);
10181 return build_x_unary_op (TREE_CODE (t), op1);
10183 case PREDECREMENT_EXPR:
10184 case PREINCREMENT_EXPR:
10185 case NEGATE_EXPR:
10186 case BIT_NOT_EXPR:
10187 case ABS_EXPR:
10188 case TRUTH_NOT_EXPR:
10189 case UNARY_PLUS_EXPR: /* Unary + */
10190 case REALPART_EXPR:
10191 case IMAGPART_EXPR:
10192 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
10194 case ADDR_EXPR:
10195 op1 = TREE_OPERAND (t, 0);
10196 if (TREE_CODE (op1) == SCOPE_REF)
10197 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
10198 /*done=*/true, /*address_p=*/true);
10199 else
10200 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
10201 in_decl);
10202 if (TREE_CODE (op1) == LABEL_DECL)
10203 return finish_label_address_expr (DECL_NAME (op1));
10204 return build_x_unary_op (ADDR_EXPR, op1);
10206 case PLUS_EXPR:
10207 case MINUS_EXPR:
10208 case MULT_EXPR:
10209 case TRUNC_DIV_EXPR:
10210 case CEIL_DIV_EXPR:
10211 case FLOOR_DIV_EXPR:
10212 case ROUND_DIV_EXPR:
10213 case EXACT_DIV_EXPR:
10214 case BIT_AND_EXPR:
10215 case BIT_IOR_EXPR:
10216 case BIT_XOR_EXPR:
10217 case TRUNC_MOD_EXPR:
10218 case FLOOR_MOD_EXPR:
10219 case TRUTH_ANDIF_EXPR:
10220 case TRUTH_ORIF_EXPR:
10221 case TRUTH_AND_EXPR:
10222 case TRUTH_OR_EXPR:
10223 case RSHIFT_EXPR:
10224 case LSHIFT_EXPR:
10225 case RROTATE_EXPR:
10226 case LROTATE_EXPR:
10227 case EQ_EXPR:
10228 case NE_EXPR:
10229 case MAX_EXPR:
10230 case MIN_EXPR:
10231 case LE_EXPR:
10232 case GE_EXPR:
10233 case LT_EXPR:
10234 case GT_EXPR:
10235 case MEMBER_REF:
10236 case DOTSTAR_EXPR:
10237 return build_x_binary_op
10238 (TREE_CODE (t),
10239 RECUR (TREE_OPERAND (t, 0)),
10240 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10241 ? ERROR_MARK
10242 : TREE_CODE (TREE_OPERAND (t, 0))),
10243 RECUR (TREE_OPERAND (t, 1)),
10244 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10245 ? ERROR_MARK
10246 : TREE_CODE (TREE_OPERAND (t, 1))),
10247 /*overloaded_p=*/NULL);
10249 case SCOPE_REF:
10250 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
10251 /*address_p=*/false);
10252 case ARRAY_REF:
10253 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10254 args, complain, in_decl);
10255 return build_x_binary_op (ARRAY_REF, op1,
10256 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10257 ? ERROR_MARK
10258 : TREE_CODE (TREE_OPERAND (t, 0))),
10259 RECUR (TREE_OPERAND (t, 1)),
10260 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10261 ? ERROR_MARK
10262 : TREE_CODE (TREE_OPERAND (t, 1))),
10263 /*overloaded_p=*/NULL);
10265 case SIZEOF_EXPR:
10266 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10268 /* We only want to compute the number of arguments. */
10269 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10270 complain, in_decl);
10271 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
10273 /* Fall through */
10275 case ALIGNOF_EXPR:
10276 op1 = TREE_OPERAND (t, 0);
10277 if (!args)
10279 /* When there are no ARGS, we are trying to evaluate a
10280 non-dependent expression from the parser. Trying to do
10281 the substitutions may not work. */
10282 if (!TYPE_P (op1))
10283 op1 = TREE_TYPE (op1);
10285 else
10287 ++skip_evaluation;
10288 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
10289 /*function_p=*/false,
10290 /*integral_constant_expression_p=*/false);
10291 --skip_evaluation;
10293 if (TYPE_P (op1))
10294 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
10295 else
10296 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
10298 case MODOP_EXPR:
10300 tree r = build_x_modify_expr
10301 (RECUR (TREE_OPERAND (t, 0)),
10302 TREE_CODE (TREE_OPERAND (t, 1)),
10303 RECUR (TREE_OPERAND (t, 2)));
10304 /* TREE_NO_WARNING must be set if either the expression was
10305 parenthesized or it uses an operator such as >>= rather
10306 than plain assignment. In the former case, it was already
10307 set and must be copied. In the latter case,
10308 build_x_modify_expr sets it and it must not be reset
10309 here. */
10310 if (TREE_NO_WARNING (t))
10311 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10312 return r;
10315 case ARROW_EXPR:
10316 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10317 args, complain, in_decl);
10318 /* Remember that there was a reference to this entity. */
10319 if (DECL_P (op1))
10320 mark_used (op1);
10321 return build_x_arrow (op1);
10323 case NEW_EXPR:
10324 return build_new
10325 (RECUR (TREE_OPERAND (t, 0)),
10326 RECUR (TREE_OPERAND (t, 1)),
10327 RECUR (TREE_OPERAND (t, 2)),
10328 RECUR (TREE_OPERAND (t, 3)),
10329 NEW_EXPR_USE_GLOBAL (t));
10331 case DELETE_EXPR:
10332 return delete_sanity
10333 (RECUR (TREE_OPERAND (t, 0)),
10334 RECUR (TREE_OPERAND (t, 1)),
10335 DELETE_EXPR_USE_VEC (t),
10336 DELETE_EXPR_USE_GLOBAL (t));
10338 case COMPOUND_EXPR:
10339 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
10340 RECUR (TREE_OPERAND (t, 1)));
10342 case CALL_EXPR:
10344 tree function;
10345 tree call_args;
10346 bool qualified_p;
10347 bool koenig_p;
10349 function = CALL_EXPR_FN (t);
10350 /* When we parsed the expression, we determined whether or
10351 not Koenig lookup should be performed. */
10352 koenig_p = KOENIG_LOOKUP_P (t);
10353 if (TREE_CODE (function) == SCOPE_REF)
10355 qualified_p = true;
10356 function = tsubst_qualified_id (function, args, complain, in_decl,
10357 /*done=*/false,
10358 /*address_p=*/false);
10360 else
10362 if (TREE_CODE (function) == COMPONENT_REF)
10364 tree op = TREE_OPERAND (function, 1);
10366 qualified_p = (TREE_CODE (op) == SCOPE_REF
10367 || (BASELINK_P (op)
10368 && BASELINK_QUALIFIED_P (op)));
10370 else
10371 qualified_p = false;
10373 function = tsubst_copy_and_build (function, args, complain,
10374 in_decl,
10375 !qualified_p,
10376 integral_constant_expression_p);
10378 if (BASELINK_P (function))
10379 qualified_p = true;
10382 /* FIXME: Rewrite this so as not to construct an arglist. */
10383 call_args = RECUR (CALL_EXPR_ARGS (t));
10385 /* We do not perform argument-dependent lookup if normal
10386 lookup finds a non-function, in accordance with the
10387 expected resolution of DR 218. */
10388 if (koenig_p
10389 && ((is_overloaded_fn (function)
10390 /* If lookup found a member function, the Koenig lookup is
10391 not appropriate, even if an unqualified-name was used
10392 to denote the function. */
10393 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
10394 || TREE_CODE (function) == IDENTIFIER_NODE))
10395 function = perform_koenig_lookup (function, call_args);
10397 if (TREE_CODE (function) == IDENTIFIER_NODE)
10399 unqualified_name_lookup_error (function);
10400 return error_mark_node;
10403 /* Remember that there was a reference to this entity. */
10404 if (DECL_P (function))
10405 mark_used (function);
10407 if (TREE_CODE (function) == OFFSET_REF)
10408 return build_offset_ref_call_from_tree (function, call_args);
10409 if (TREE_CODE (function) == COMPONENT_REF)
10411 if (!BASELINK_P (TREE_OPERAND (function, 1)))
10412 return finish_call_expr (function, call_args,
10413 /*disallow_virtual=*/false,
10414 /*koenig_p=*/false);
10415 else
10416 return (build_new_method_call
10417 (TREE_OPERAND (function, 0),
10418 TREE_OPERAND (function, 1),
10419 call_args, NULL_TREE,
10420 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
10421 /*fn_p=*/NULL));
10423 return finish_call_expr (function, call_args,
10424 /*disallow_virtual=*/qualified_p,
10425 koenig_p);
10428 case COND_EXPR:
10429 return build_x_conditional_expr
10430 (RECUR (TREE_OPERAND (t, 0)),
10431 RECUR (TREE_OPERAND (t, 1)),
10432 RECUR (TREE_OPERAND (t, 2)));
10434 case PSEUDO_DTOR_EXPR:
10435 return finish_pseudo_destructor_expr
10436 (RECUR (TREE_OPERAND (t, 0)),
10437 RECUR (TREE_OPERAND (t, 1)),
10438 RECUR (TREE_OPERAND (t, 2)));
10440 case TREE_LIST:
10442 tree purpose, value, chain;
10444 if (t == void_list_node)
10445 return t;
10447 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
10448 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
10450 /* We have pack expansions, so expand those and
10451 create a new list out of it. */
10452 tree purposevec = NULL_TREE;
10453 tree valuevec = NULL_TREE;
10454 tree chain;
10455 int i, len = -1;
10457 /* Expand the argument expressions. */
10458 if (TREE_PURPOSE (t))
10459 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
10460 complain, in_decl);
10461 if (TREE_VALUE (t))
10462 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
10463 complain, in_decl);
10465 /* Build the rest of the list. */
10466 chain = TREE_CHAIN (t);
10467 if (chain && chain != void_type_node)
10468 chain = RECUR (chain);
10470 /* Determine the number of arguments. */
10471 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
10473 len = TREE_VEC_LENGTH (purposevec);
10474 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
10476 else if (TREE_CODE (valuevec) == TREE_VEC)
10477 len = TREE_VEC_LENGTH (valuevec);
10478 else
10480 /* Since we only performed a partial substitution into
10481 the argument pack, we only return a single list
10482 node. */
10483 if (purposevec == TREE_PURPOSE (t)
10484 && valuevec == TREE_VALUE (t)
10485 && chain == TREE_CHAIN (t))
10486 return t;
10488 return tree_cons (purposevec, valuevec, chain);
10491 /* Convert the argument vectors into a TREE_LIST */
10492 i = len;
10493 while (i > 0)
10495 /* Grab the Ith values. */
10496 i--;
10497 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
10498 : NULL_TREE;
10499 value
10500 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
10501 : NULL_TREE;
10503 /* Build the list (backwards). */
10504 chain = tree_cons (purpose, value, chain);
10507 return chain;
10510 purpose = TREE_PURPOSE (t);
10511 if (purpose)
10512 purpose = RECUR (purpose);
10513 value = TREE_VALUE (t);
10514 if (value)
10515 value = RECUR (value);
10516 chain = TREE_CHAIN (t);
10517 if (chain && chain != void_type_node)
10518 chain = RECUR (chain);
10519 if (purpose == TREE_PURPOSE (t)
10520 && value == TREE_VALUE (t)
10521 && chain == TREE_CHAIN (t))
10522 return t;
10523 return tree_cons (purpose, value, chain);
10526 case COMPONENT_REF:
10528 tree object;
10529 tree object_type;
10530 tree member;
10532 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10533 args, complain, in_decl);
10534 /* Remember that there was a reference to this entity. */
10535 if (DECL_P (object))
10536 mark_used (object);
10537 object_type = TREE_TYPE (object);
10539 member = TREE_OPERAND (t, 1);
10540 if (BASELINK_P (member))
10541 member = tsubst_baselink (member,
10542 non_reference (TREE_TYPE (object)),
10543 args, complain, in_decl);
10544 else
10545 member = tsubst_copy (member, args, complain, in_decl);
10546 if (member == error_mark_node)
10547 return error_mark_node;
10549 if (object_type && !CLASS_TYPE_P (object_type))
10551 if (TREE_CODE (member) == BIT_NOT_EXPR)
10552 return finish_pseudo_destructor_expr (object,
10553 NULL_TREE,
10554 object_type);
10555 else if (TREE_CODE (member) == SCOPE_REF
10556 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
10557 return finish_pseudo_destructor_expr (object,
10558 object,
10559 object_type);
10561 else if (TREE_CODE (member) == SCOPE_REF
10562 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
10564 tree tmpl;
10565 tree args;
10567 /* Lookup the template functions now that we know what the
10568 scope is. */
10569 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
10570 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
10571 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
10572 /*is_type_p=*/false,
10573 /*complain=*/false);
10574 if (BASELINK_P (member))
10576 BASELINK_FUNCTIONS (member)
10577 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
10578 args);
10579 member = (adjust_result_of_qualified_name_lookup
10580 (member, BINFO_TYPE (BASELINK_BINFO (member)),
10581 object_type));
10583 else
10585 qualified_name_lookup_error (object_type, tmpl, member);
10586 return error_mark_node;
10589 else if (TREE_CODE (member) == SCOPE_REF
10590 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
10591 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
10593 if (complain & tf_error)
10595 if (TYPE_P (TREE_OPERAND (member, 0)))
10596 error ("%qT is not a class or namespace",
10597 TREE_OPERAND (member, 0));
10598 else
10599 error ("%qD is not a class or namespace",
10600 TREE_OPERAND (member, 0));
10602 return error_mark_node;
10604 else if (TREE_CODE (member) == FIELD_DECL)
10605 return finish_non_static_data_member (member, object, NULL_TREE);
10607 return finish_class_member_access_expr (object, member,
10608 /*template_p=*/false);
10611 case THROW_EXPR:
10612 return build_throw
10613 (RECUR (TREE_OPERAND (t, 0)));
10615 case CONSTRUCTOR:
10617 VEC(constructor_elt,gc) *n;
10618 constructor_elt *ce;
10619 unsigned HOST_WIDE_INT idx;
10620 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10621 bool process_index_p;
10622 int newlen;
10623 bool need_copy_p = false;
10625 if (type == error_mark_node)
10626 return error_mark_node;
10628 /* digest_init will do the wrong thing if we let it. */
10629 if (type && TYPE_PTRMEMFUNC_P (type))
10630 return t;
10632 /* We do not want to process the index of aggregate
10633 initializers as they are identifier nodes which will be
10634 looked up by digest_init. */
10635 process_index_p = !(type && IS_AGGR_TYPE (type));
10637 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
10638 newlen = VEC_length (constructor_elt, n);
10639 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
10641 if (ce->index && process_index_p)
10642 ce->index = RECUR (ce->index);
10644 if (PACK_EXPANSION_P (ce->value))
10646 /* Substitute into the pack expansion. */
10647 ce->value = tsubst_pack_expansion (ce->value, args, complain,
10648 in_decl);
10650 if (TREE_VEC_LENGTH (ce->value) == 1)
10651 /* Just move the argument into place. */
10652 ce->value = TREE_VEC_ELT (ce->value, 0);
10653 else
10655 /* Update the length of the final CONSTRUCTOR
10656 arguments vector, and note that we will need to
10657 copy.*/
10658 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
10659 need_copy_p = true;
10662 else
10663 ce->value = RECUR (ce->value);
10666 if (need_copy_p)
10668 VEC(constructor_elt,gc) *old_n = n;
10670 n = VEC_alloc (constructor_elt, gc, newlen);
10671 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
10672 idx++)
10674 if (TREE_CODE (ce->value) == TREE_VEC)
10676 int i, len = TREE_VEC_LENGTH (ce->value);
10677 for (i = 0; i < len; ++i)
10678 CONSTRUCTOR_APPEND_ELT (n, 0,
10679 TREE_VEC_ELT (ce->value, i));
10681 else
10682 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
10686 if (TREE_HAS_CONSTRUCTOR (t))
10687 return finish_compound_literal (type, n);
10689 return build_constructor (NULL_TREE, n);
10692 case TYPEID_EXPR:
10694 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
10695 if (TYPE_P (operand_0))
10696 return get_typeid (operand_0);
10697 return build_typeid (operand_0);
10700 case VAR_DECL:
10701 if (!args)
10702 return t;
10703 /* Fall through */
10705 case PARM_DECL:
10707 tree r = tsubst_copy (t, args, complain, in_decl);
10709 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
10710 /* If the original type was a reference, we'll be wrapped in
10711 the appropriate INDIRECT_REF. */
10712 r = convert_from_reference (r);
10713 return r;
10716 case VA_ARG_EXPR:
10717 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
10718 tsubst_copy (TREE_TYPE (t), args, complain,
10719 in_decl));
10721 case OFFSETOF_EXPR:
10722 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
10724 case TRAIT_EXPR:
10726 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
10727 complain, in_decl);
10729 tree type2 = TRAIT_EXPR_TYPE2 (t);
10730 if (type2)
10731 type2 = tsubst_copy (type2, args, complain, in_decl);
10733 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
10736 case STMT_EXPR:
10738 tree old_stmt_expr = cur_stmt_expr;
10739 tree stmt_expr = begin_stmt_expr ();
10741 cur_stmt_expr = stmt_expr;
10742 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
10743 integral_constant_expression_p);
10744 stmt_expr = finish_stmt_expr (stmt_expr, false);
10745 cur_stmt_expr = old_stmt_expr;
10747 return stmt_expr;
10750 case CONST_DECL:
10751 t = tsubst_copy (t, args, complain, in_decl);
10752 /* As in finish_id_expression, we resolve enumeration constants
10753 to their underlying values. */
10754 if (TREE_CODE (t) == CONST_DECL)
10756 used_types_insert (TREE_TYPE (t));
10757 return DECL_INITIAL (t);
10759 return t;
10761 default:
10762 /* Handle Objective-C++ constructs, if appropriate. */
10764 tree subst
10765 = objcp_tsubst_copy_and_build (t, args, complain,
10766 in_decl, /*function_p=*/false);
10767 if (subst)
10768 return subst;
10770 return tsubst_copy (t, args, complain, in_decl);
10773 #undef RECUR
10776 /* Verify that the instantiated ARGS are valid. For type arguments,
10777 make sure that the type's linkage is ok. For non-type arguments,
10778 make sure they are constants if they are integral or enumerations.
10779 Emit an error under control of COMPLAIN, and return TRUE on error. */
10781 static bool
10782 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
10784 int ix, len = DECL_NTPARMS (tmpl);
10785 bool result = false;
10787 for (ix = 0; ix != len; ix++)
10789 tree t = TREE_VEC_ELT (args, ix);
10791 if (TYPE_P (t))
10793 /* [basic.link]: A name with no linkage (notably, the name
10794 of a class or enumeration declared in a local scope)
10795 shall not be used to declare an entity with linkage.
10796 This implies that names with no linkage cannot be used as
10797 template arguments. */
10798 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
10800 if (nt)
10802 /* DR 488 makes use of a type with no linkage cause
10803 type deduction to fail. */
10804 if (complain & tf_error)
10806 if (TYPE_ANONYMOUS_P (nt))
10807 error ("%qT is/uses anonymous type", t);
10808 else
10809 error ("template argument for %qD uses local type %qT",
10810 tmpl, t);
10812 result = true;
10814 /* In order to avoid all sorts of complications, we do not
10815 allow variably-modified types as template arguments. */
10816 else if (variably_modified_type_p (t, NULL_TREE))
10818 if (complain & tf_error)
10819 error ("%qT is a variably modified type", t);
10820 result = true;
10823 /* A non-type argument of integral or enumerated type must be a
10824 constant. */
10825 else if (TREE_TYPE (t)
10826 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
10827 && !TREE_CONSTANT (t))
10829 if (complain & tf_error)
10830 error ("integral expression %qE is not constant", t);
10831 result = true;
10834 if (result && (complain & tf_error))
10835 error (" trying to instantiate %qD", tmpl);
10836 return result;
10839 /* Instantiate the indicated variable or function template TMPL with
10840 the template arguments in TARG_PTR. */
10842 tree
10843 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
10845 tree fndecl;
10846 tree gen_tmpl;
10847 tree spec;
10848 HOST_WIDE_INT saved_processing_template_decl;
10850 if (tmpl == error_mark_node)
10851 return error_mark_node;
10853 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
10855 /* If this function is a clone, handle it specially. */
10856 if (DECL_CLONED_FUNCTION_P (tmpl))
10858 tree spec;
10859 tree clone;
10861 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
10862 complain);
10863 if (spec == error_mark_node)
10864 return error_mark_node;
10866 /* Look for the clone. */
10867 FOR_EACH_CLONE (clone, spec)
10868 if (DECL_NAME (clone) == DECL_NAME (tmpl))
10869 return clone;
10870 /* We should always have found the clone by now. */
10871 gcc_unreachable ();
10872 return NULL_TREE;
10875 /* Check to see if we already have this specialization. */
10876 spec = retrieve_specialization (tmpl, targ_ptr,
10877 /*class_specializations_p=*/false);
10878 if (spec != NULL_TREE)
10879 return spec;
10881 gen_tmpl = most_general_template (tmpl);
10882 if (tmpl != gen_tmpl)
10884 /* The TMPL is a partial instantiation. To get a full set of
10885 arguments we must add the arguments used to perform the
10886 partial instantiation. */
10887 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
10888 targ_ptr);
10890 /* Check to see if we already have this specialization. */
10891 spec = retrieve_specialization (gen_tmpl, targ_ptr,
10892 /*class_specializations_p=*/false);
10893 if (spec != NULL_TREE)
10894 return spec;
10897 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
10898 complain))
10899 return error_mark_node;
10901 /* We are building a FUNCTION_DECL, during which the access of its
10902 parameters and return types have to be checked. However this
10903 FUNCTION_DECL which is the desired context for access checking
10904 is not built yet. We solve this chicken-and-egg problem by
10905 deferring all checks until we have the FUNCTION_DECL. */
10906 push_deferring_access_checks (dk_deferred);
10908 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
10909 (because, for example, we have encountered a non-dependent
10910 function call in the body of a template function and must now
10911 determine which of several overloaded functions will be called),
10912 within the instantiation itself we are not processing a
10913 template. */
10914 saved_processing_template_decl = processing_template_decl;
10915 processing_template_decl = 0;
10916 /* Substitute template parameters to obtain the specialization. */
10917 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
10918 targ_ptr, complain, gen_tmpl);
10919 processing_template_decl = saved_processing_template_decl;
10920 if (fndecl == error_mark_node)
10921 return error_mark_node;
10923 /* Now we know the specialization, compute access previously
10924 deferred. */
10925 push_access_scope (fndecl);
10926 perform_deferred_access_checks ();
10927 pop_access_scope (fndecl);
10928 pop_deferring_access_checks ();
10930 /* The DECL_TI_TEMPLATE should always be the immediate parent
10931 template, not the most general template. */
10932 DECL_TI_TEMPLATE (fndecl) = tmpl;
10934 /* If we've just instantiated the main entry point for a function,
10935 instantiate all the alternate entry points as well. We do this
10936 by cloning the instantiation of the main entry point, not by
10937 instantiating the template clones. */
10938 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
10939 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
10941 return fndecl;
10944 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
10945 arguments that are being used when calling it. TARGS is a vector
10946 into which the deduced template arguments are placed.
10948 Return zero for success, 2 for an incomplete match that doesn't resolve
10949 all the types, and 1 for complete failure. An error message will be
10950 printed only for an incomplete match.
10952 If FN is a conversion operator, or we are trying to produce a specific
10953 specialization, RETURN_TYPE is the return type desired.
10955 The EXPLICIT_TARGS are explicit template arguments provided via a
10956 template-id.
10958 The parameter STRICT is one of:
10960 DEDUCE_CALL:
10961 We are deducing arguments for a function call, as in
10962 [temp.deduct.call].
10964 DEDUCE_CONV:
10965 We are deducing arguments for a conversion function, as in
10966 [temp.deduct.conv].
10968 DEDUCE_EXACT:
10969 We are deducing arguments when doing an explicit instantiation
10970 as in [temp.explicit], when determining an explicit specialization
10971 as in [temp.expl.spec], or when taking the address of a function
10972 template, as in [temp.deduct.funcaddr]. */
10975 fn_type_unification (tree fn,
10976 tree explicit_targs,
10977 tree targs,
10978 tree args,
10979 tree return_type,
10980 unification_kind_t strict,
10981 int flags)
10983 tree parms;
10984 tree fntype;
10985 int result;
10986 bool incomplete_argument_packs_p = false;
10988 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
10990 fntype = TREE_TYPE (fn);
10991 if (explicit_targs)
10993 /* [temp.deduct]
10995 The specified template arguments must match the template
10996 parameters in kind (i.e., type, nontype, template), and there
10997 must not be more arguments than there are parameters;
10998 otherwise type deduction fails.
11000 Nontype arguments must match the types of the corresponding
11001 nontype template parameters, or must be convertible to the
11002 types of the corresponding nontype parameters as specified in
11003 _temp.arg.nontype_, otherwise type deduction fails.
11005 All references in the function type of the function template
11006 to the corresponding template parameters are replaced by the
11007 specified template argument values. If a substitution in a
11008 template parameter or in the function type of the function
11009 template results in an invalid type, type deduction fails. */
11010 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
11011 int i, len = TREE_VEC_LENGTH (tparms);
11012 tree converted_args;
11013 bool incomplete = false;
11015 if (explicit_targs == error_mark_node)
11016 return 1;
11018 converted_args
11019 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
11020 /*require_all_args=*/false,
11021 /*use_default_args=*/false));
11022 if (converted_args == error_mark_node)
11023 return 1;
11025 /* Substitute the explicit args into the function type. This is
11026 necessary so that, for instance, explicitly declared function
11027 arguments can match null pointed constants. If we were given
11028 an incomplete set of explicit args, we must not do semantic
11029 processing during substitution as we could create partial
11030 instantiations. */
11031 for (i = 0; i < len; i++)
11033 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11034 bool parameter_pack = false;
11036 /* Dig out the actual parm. */
11037 if (TREE_CODE (parm) == TYPE_DECL
11038 || TREE_CODE (parm) == TEMPLATE_DECL)
11040 parm = TREE_TYPE (parm);
11041 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
11043 else if (TREE_CODE (parm) == PARM_DECL)
11045 parm = DECL_INITIAL (parm);
11046 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
11049 if (parameter_pack)
11051 int level, idx;
11052 tree targ;
11053 template_parm_level_and_index (parm, &level, &idx);
11055 /* Mark the argument pack as "incomplete". We could
11056 still deduce more arguments during unification. */
11057 targ = TMPL_ARG (converted_args, level, idx);
11058 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
11059 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = ARGUMENT_PACK_ARGS (targ);
11061 /* We have some incomplete argument packs. */
11062 incomplete_argument_packs_p = true;
11066 if (incomplete_argument_packs_p)
11067 /* Any substitution is guaranteed to be incomplete if there
11068 are incomplete argument packs, because we can still deduce
11069 more arguments. */
11070 incomplete = 1;
11071 else
11072 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
11074 processing_template_decl += incomplete;
11075 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
11076 processing_template_decl -= incomplete;
11078 if (fntype == error_mark_node)
11079 return 1;
11081 /* Place the explicitly specified arguments in TARGS. */
11082 for (i = NUM_TMPL_ARGS (converted_args); i--;)
11083 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
11086 /* Never do unification on the 'this' parameter. */
11087 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
11089 if (return_type)
11091 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
11092 args = tree_cons (NULL_TREE, return_type, args);
11095 /* We allow incomplete unification without an error message here
11096 because the standard doesn't seem to explicitly prohibit it. Our
11097 callers must be ready to deal with unification failures in any
11098 event. */
11099 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
11100 targs, parms, args, /*subr=*/0,
11101 strict, flags);
11103 if (result == 0 && incomplete_argument_packs_p)
11105 int i, len = NUM_TMPL_ARGS (targs);
11107 /* Clear the "incomplete" flags on all argument packs. */
11108 for (i = 0; i < len; i++)
11110 tree arg = TREE_VEC_ELT (targs, i);
11111 if (ARGUMENT_PACK_P (arg))
11113 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
11114 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
11119 if (result == 0)
11120 /* All is well so far. Now, check:
11122 [temp.deduct]
11124 When all template arguments have been deduced, all uses of
11125 template parameters in nondeduced contexts are replaced with
11126 the corresponding deduced argument values. If the
11127 substitution results in an invalid type, as described above,
11128 type deduction fails. */
11129 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
11130 == error_mark_node)
11131 return 1;
11133 return result;
11136 /* Adjust types before performing type deduction, as described in
11137 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11138 sections are symmetric. PARM is the type of a function parameter
11139 or the return type of the conversion function. ARG is the type of
11140 the argument passed to the call, or the type of the value
11141 initialized with the result of the conversion function. */
11143 static int
11144 maybe_adjust_types_for_deduction (unification_kind_t strict,
11145 tree* parm,
11146 tree* arg)
11148 int result = 0;
11150 switch (strict)
11152 case DEDUCE_CALL:
11153 break;
11155 case DEDUCE_CONV:
11157 /* Swap PARM and ARG throughout the remainder of this
11158 function; the handling is precisely symmetric since PARM
11159 will initialize ARG rather than vice versa. */
11160 tree* temp = parm;
11161 parm = arg;
11162 arg = temp;
11163 break;
11166 case DEDUCE_EXACT:
11167 /* There is nothing to do in this case. */
11168 return 0;
11170 default:
11171 gcc_unreachable ();
11174 if (TREE_CODE (*parm) != REFERENCE_TYPE)
11176 /* [temp.deduct.call]
11178 If P is not a reference type:
11180 --If A is an array type, the pointer type produced by the
11181 array-to-pointer standard conversion (_conv.array_) is
11182 used in place of A for type deduction; otherwise,
11184 --If A is a function type, the pointer type produced by
11185 the function-to-pointer standard conversion
11186 (_conv.func_) is used in place of A for type deduction;
11187 otherwise,
11189 --If A is a cv-qualified type, the top level
11190 cv-qualifiers of A's type are ignored for type
11191 deduction. */
11192 if (TREE_CODE (*arg) == ARRAY_TYPE)
11193 *arg = build_pointer_type (TREE_TYPE (*arg));
11194 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
11195 *arg = build_pointer_type (*arg);
11196 else
11197 *arg = TYPE_MAIN_VARIANT (*arg);
11200 /* [temp.deduct.call]
11202 If P is a cv-qualified type, the top level cv-qualifiers
11203 of P's type are ignored for type deduction. If P is a
11204 reference type, the type referred to by P is used for
11205 type deduction. */
11206 *parm = TYPE_MAIN_VARIANT (*parm);
11207 if (TREE_CODE (*parm) == REFERENCE_TYPE)
11209 *parm = TREE_TYPE (*parm);
11210 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
11213 /* DR 322. For conversion deduction, remove a reference type on parm
11214 too (which has been swapped into ARG). */
11215 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
11216 *arg = TREE_TYPE (*arg);
11218 return result;
11221 /* Most parms like fn_type_unification.
11223 If SUBR is 1, we're being called recursively (to unify the
11224 arguments of a function or method parameter of a function
11225 template). */
11227 static int
11228 type_unification_real (tree tparms,
11229 tree targs,
11230 tree xparms,
11231 tree xargs,
11232 int subr,
11233 unification_kind_t strict,
11234 int flags)
11236 tree parm, arg;
11237 int i;
11238 int ntparms = TREE_VEC_LENGTH (tparms);
11239 int sub_strict;
11240 int saw_undeduced = 0;
11241 tree parms, args;
11243 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
11244 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
11245 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
11246 gcc_assert (ntparms > 0);
11248 switch (strict)
11250 case DEDUCE_CALL:
11251 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
11252 | UNIFY_ALLOW_DERIVED);
11253 break;
11255 case DEDUCE_CONV:
11256 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
11257 break;
11259 case DEDUCE_EXACT:
11260 sub_strict = UNIFY_ALLOW_NONE;
11261 break;
11263 default:
11264 gcc_unreachable ();
11267 again:
11268 parms = xparms;
11269 args = xargs;
11271 while (parms && parms != void_list_node
11272 && args && args != void_list_node)
11274 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11275 break;
11277 parm = TREE_VALUE (parms);
11278 parms = TREE_CHAIN (parms);
11279 arg = TREE_VALUE (args);
11280 args = TREE_CHAIN (args);
11282 if (arg == error_mark_node)
11283 return 1;
11284 if (arg == unknown_type_node)
11285 /* We can't deduce anything from this, but we might get all the
11286 template args from other function args. */
11287 continue;
11289 /* Conversions will be performed on a function argument that
11290 corresponds with a function parameter that contains only
11291 non-deducible template parameters and explicitly specified
11292 template parameters. */
11293 if (!uses_template_parms (parm))
11295 tree type;
11297 if (!TYPE_P (arg))
11298 type = TREE_TYPE (arg);
11299 else
11300 type = arg;
11302 if (same_type_p (parm, type))
11303 continue;
11304 if (strict != DEDUCE_EXACT
11305 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
11306 flags))
11307 continue;
11309 return 1;
11312 if (!TYPE_P (arg))
11314 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
11315 if (type_unknown_p (arg))
11317 /* [temp.deduct.type] A template-argument can be deduced from
11318 a pointer to function or pointer to member function
11319 argument if the set of overloaded functions does not
11320 contain function templates and at most one of a set of
11321 overloaded functions provides a unique match. */
11323 if (resolve_overloaded_unification
11324 (tparms, targs, parm, arg, strict, sub_strict)
11325 != 0)
11326 return 1;
11327 continue;
11329 arg = unlowered_expr_type (arg);
11330 if (arg == error_mark_node)
11331 return 1;
11335 int arg_strict = sub_strict;
11337 if (!subr)
11338 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
11340 if (unify (tparms, targs, parm, arg, arg_strict))
11341 return 1;
11346 if (parms
11347 && parms != void_list_node
11348 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11350 /* Unify the remaining arguments with the pack expansion type. */
11351 tree argvec;
11352 tree parmvec = make_tree_vec (1);
11353 int len = 0;
11354 tree t;
11356 /* Count the number of arguments that remain. */
11357 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
11358 len++;
11360 /* Allocate a TREE_VEC and copy in all of the arguments */
11361 argvec = make_tree_vec (len);
11362 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
11364 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
11365 ++i;
11368 /* Copy the parameter into parmvec. */
11369 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
11370 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
11371 /*call_args_p=*/true, /*subr=*/subr))
11372 return 1;
11374 /* Advance to the end of the list of parameters. */
11375 parms = TREE_CHAIN (parms);
11378 /* Fail if we've reached the end of the parm list, and more args
11379 are present, and the parm list isn't variadic. */
11380 if (args && args != void_list_node && parms == void_list_node)
11381 return 1;
11382 /* Fail if parms are left and they don't have default values. */
11383 if (parms && parms != void_list_node
11384 && TREE_PURPOSE (parms) == NULL_TREE)
11385 return 1;
11387 if (!subr)
11388 for (i = 0; i < ntparms; i++)
11389 if (!TREE_VEC_ELT (targs, i))
11391 tree tparm;
11393 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
11394 continue;
11396 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11398 /* If this is an undeduced nontype parameter that depends on
11399 a type parameter, try another pass; its type may have been
11400 deduced from a later argument than the one from which
11401 this parameter can be deduced. */
11402 if (TREE_CODE (tparm) == PARM_DECL
11403 && uses_template_parms (TREE_TYPE (tparm))
11404 && !saw_undeduced++)
11405 goto again;
11407 /* Core issue #226 (C++0x) [temp.deduct]:
11409 If a template argument has not been deduced, its
11410 default template argument, if any, is used.
11412 When we are not in C++0x mode (i.e., !flag_cpp0x),
11413 TREE_PURPOSE will either be NULL_TREE or ERROR_MARK_NODE,
11414 so we do not need to explicitly check flag_cpp0x here. */
11415 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
11417 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
11418 targs, tf_none, NULL_TREE);
11419 if (arg == error_mark_node)
11420 return 1;
11421 else
11423 TREE_VEC_ELT (targs, i) = arg;
11424 continue;
11428 return 2;
11431 return 0;
11434 /* Subroutine of type_unification_real. Args are like the variables at the
11435 call site. ARG is an overloaded function (or template-id); we try
11436 deducing template args from each of the overloads, and if only one
11437 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
11439 static int
11440 resolve_overloaded_unification (tree tparms,
11441 tree targs,
11442 tree parm,
11443 tree arg,
11444 unification_kind_t strict,
11445 int sub_strict)
11447 tree tempargs = copy_node (targs);
11448 int good = 0;
11449 bool addr_p;
11451 if (TREE_CODE (arg) == ADDR_EXPR)
11453 arg = TREE_OPERAND (arg, 0);
11454 addr_p = true;
11456 else
11457 addr_p = false;
11459 if (TREE_CODE (arg) == COMPONENT_REF)
11460 /* Handle `&x' where `x' is some static or non-static member
11461 function name. */
11462 arg = TREE_OPERAND (arg, 1);
11464 if (TREE_CODE (arg) == OFFSET_REF)
11465 arg = TREE_OPERAND (arg, 1);
11467 /* Strip baselink information. */
11468 if (BASELINK_P (arg))
11469 arg = BASELINK_FUNCTIONS (arg);
11471 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
11473 /* If we got some explicit template args, we need to plug them into
11474 the affected templates before we try to unify, in case the
11475 explicit args will completely resolve the templates in question. */
11477 tree expl_subargs = TREE_OPERAND (arg, 1);
11478 arg = TREE_OPERAND (arg, 0);
11480 for (; arg; arg = OVL_NEXT (arg))
11482 tree fn = OVL_CURRENT (arg);
11483 tree subargs, elem;
11485 if (TREE_CODE (fn) != TEMPLATE_DECL)
11486 continue;
11488 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
11489 expl_subargs, /*check_ret=*/false);
11490 if (subargs)
11492 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
11493 good += try_one_overload (tparms, targs, tempargs, parm,
11494 elem, strict, sub_strict, addr_p);
11498 else
11500 gcc_assert (TREE_CODE (arg) == OVERLOAD
11501 || TREE_CODE (arg) == FUNCTION_DECL);
11503 for (; arg; arg = OVL_NEXT (arg))
11504 good += try_one_overload (tparms, targs, tempargs, parm,
11505 TREE_TYPE (OVL_CURRENT (arg)),
11506 strict, sub_strict, addr_p);
11509 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11510 to function or pointer to member function argument if the set of
11511 overloaded functions does not contain function templates and at most
11512 one of a set of overloaded functions provides a unique match.
11514 So if we found multiple possibilities, we return success but don't
11515 deduce anything. */
11517 if (good == 1)
11519 int i = TREE_VEC_LENGTH (targs);
11520 for (; i--; )
11521 if (TREE_VEC_ELT (tempargs, i))
11522 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
11524 if (good)
11525 return 0;
11527 return 1;
11530 /* Subroutine of resolve_overloaded_unification; does deduction for a single
11531 overload. Fills TARGS with any deduced arguments, or error_mark_node if
11532 different overloads deduce different arguments for a given parm.
11533 ADDR_P is true if the expression for which deduction is being
11534 performed was of the form "& fn" rather than simply "fn".
11536 Returns 1 on success. */
11538 static int
11539 try_one_overload (tree tparms,
11540 tree orig_targs,
11541 tree targs,
11542 tree parm,
11543 tree arg,
11544 unification_kind_t strict,
11545 int sub_strict,
11546 bool addr_p)
11548 int nargs;
11549 tree tempargs;
11550 int i;
11552 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11553 to function or pointer to member function argument if the set of
11554 overloaded functions does not contain function templates and at most
11555 one of a set of overloaded functions provides a unique match.
11557 So if this is a template, just return success. */
11559 if (uses_template_parms (arg))
11560 return 1;
11562 if (TREE_CODE (arg) == METHOD_TYPE)
11563 arg = build_ptrmemfunc_type (build_pointer_type (arg));
11564 else if (addr_p)
11565 arg = build_pointer_type (arg);
11567 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
11569 /* We don't copy orig_targs for this because if we have already deduced
11570 some template args from previous args, unify would complain when we
11571 try to deduce a template parameter for the same argument, even though
11572 there isn't really a conflict. */
11573 nargs = TREE_VEC_LENGTH (targs);
11574 tempargs = make_tree_vec (nargs);
11576 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
11577 return 0;
11579 /* First make sure we didn't deduce anything that conflicts with
11580 explicitly specified args. */
11581 for (i = nargs; i--; )
11583 tree elt = TREE_VEC_ELT (tempargs, i);
11584 tree oldelt = TREE_VEC_ELT (orig_targs, i);
11586 if (!elt)
11587 /*NOP*/;
11588 else if (uses_template_parms (elt))
11589 /* Since we're unifying against ourselves, we will fill in
11590 template args used in the function parm list with our own
11591 template parms. Discard them. */
11592 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
11593 else if (oldelt && !template_args_equal (oldelt, elt))
11594 return 0;
11597 for (i = nargs; i--; )
11599 tree elt = TREE_VEC_ELT (tempargs, i);
11601 if (elt)
11602 TREE_VEC_ELT (targs, i) = elt;
11605 return 1;
11608 /* PARM is a template class (perhaps with unbound template
11609 parameters). ARG is a fully instantiated type. If ARG can be
11610 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
11611 TARGS are as for unify. */
11613 static tree
11614 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
11616 tree copy_of_targs;
11618 if (!CLASSTYPE_TEMPLATE_INFO (arg)
11619 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
11620 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
11621 return NULL_TREE;
11623 /* We need to make a new template argument vector for the call to
11624 unify. If we used TARGS, we'd clutter it up with the result of
11625 the attempted unification, even if this class didn't work out.
11626 We also don't want to commit ourselves to all the unifications
11627 we've already done, since unification is supposed to be done on
11628 an argument-by-argument basis. In other words, consider the
11629 following pathological case:
11631 template <int I, int J, int K>
11632 struct S {};
11634 template <int I, int J>
11635 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
11637 template <int I, int J, int K>
11638 void f(S<I, J, K>, S<I, I, I>);
11640 void g() {
11641 S<0, 0, 0> s0;
11642 S<0, 1, 2> s2;
11644 f(s0, s2);
11647 Now, by the time we consider the unification involving `s2', we
11648 already know that we must have `f<0, 0, 0>'. But, even though
11649 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
11650 because there are two ways to unify base classes of S<0, 1, 2>
11651 with S<I, I, I>. If we kept the already deduced knowledge, we
11652 would reject the possibility I=1. */
11653 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
11655 /* If unification failed, we're done. */
11656 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
11657 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
11658 return NULL_TREE;
11660 return arg;
11663 /* Given a template type PARM and a class type ARG, find the unique
11664 base type in ARG that is an instance of PARM. We do not examine
11665 ARG itself; only its base-classes. If there is not exactly one
11666 appropriate base class, return NULL_TREE. PARM may be the type of
11667 a partial specialization, as well as a plain template type. Used
11668 by unify. */
11670 static tree
11671 get_template_base (tree tparms, tree targs, tree parm, tree arg)
11673 tree rval = NULL_TREE;
11674 tree binfo;
11676 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
11678 binfo = TYPE_BINFO (complete_type (arg));
11679 if (!binfo)
11680 /* The type could not be completed. */
11681 return NULL_TREE;
11683 /* Walk in inheritance graph order. The search order is not
11684 important, and this avoids multiple walks of virtual bases. */
11685 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
11687 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
11689 if (r)
11691 /* If there is more than one satisfactory baseclass, then:
11693 [temp.deduct.call]
11695 If they yield more than one possible deduced A, the type
11696 deduction fails.
11698 applies. */
11699 if (rval && !same_type_p (r, rval))
11700 return NULL_TREE;
11702 rval = r;
11706 return rval;
11709 /* Returns the level of DECL, which declares a template parameter. */
11711 static int
11712 template_decl_level (tree decl)
11714 switch (TREE_CODE (decl))
11716 case TYPE_DECL:
11717 case TEMPLATE_DECL:
11718 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
11720 case PARM_DECL:
11721 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
11723 default:
11724 gcc_unreachable ();
11726 return 0;
11729 /* Decide whether ARG can be unified with PARM, considering only the
11730 cv-qualifiers of each type, given STRICT as documented for unify.
11731 Returns nonzero iff the unification is OK on that basis. */
11733 static int
11734 check_cv_quals_for_unify (int strict, tree arg, tree parm)
11736 int arg_quals = cp_type_quals (arg);
11737 int parm_quals = cp_type_quals (parm);
11739 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
11740 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
11742 /* Although a CVR qualifier is ignored when being applied to a
11743 substituted template parameter ([8.3.2]/1 for example), that
11744 does not apply during deduction [14.8.2.4]/1, (even though
11745 that is not explicitly mentioned, [14.8.2.4]/9 indicates
11746 this). Except when we're allowing additional CV qualifiers
11747 at the outer level [14.8.2.1]/3,1st bullet. */
11748 if ((TREE_CODE (arg) == REFERENCE_TYPE
11749 || TREE_CODE (arg) == FUNCTION_TYPE
11750 || TREE_CODE (arg) == METHOD_TYPE)
11751 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
11752 return 0;
11754 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
11755 && (parm_quals & TYPE_QUAL_RESTRICT))
11756 return 0;
11759 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
11760 && (arg_quals & parm_quals) != parm_quals)
11761 return 0;
11763 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
11764 && (parm_quals & arg_quals) != arg_quals)
11765 return 0;
11767 return 1;
11770 /* Determines the LEVEL and INDEX for the template parameter PARM. */
11771 void
11772 template_parm_level_and_index (tree parm, int* level, int* index)
11774 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
11775 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
11776 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
11778 *index = TEMPLATE_TYPE_IDX (parm);
11779 *level = TEMPLATE_TYPE_LEVEL (parm);
11781 else
11783 *index = TEMPLATE_PARM_IDX (parm);
11784 *level = TEMPLATE_PARM_LEVEL (parm);
11788 /* Unifies the remaining arguments in PACKED_ARGS with the pack
11789 expansion at the end of PACKED_PARMS. Returns 0 if the type
11790 deduction succeeds, 1 otherwise. STRICT is the same as in
11791 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
11792 call argument list. We'll need to adjust the arguments to make them
11793 types. SUBR tells us if this is from a recursive call to
11794 type_unification_real. */
11796 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
11797 tree packed_args, int strict, bool call_args_p,
11798 bool subr)
11800 tree parm
11801 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
11802 tree pattern = PACK_EXPANSION_PATTERN (parm);
11803 tree pack, packs = NULL_TREE;
11804 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
11805 int len = TREE_VEC_LENGTH (packed_args);
11807 /* Determine the parameter packs we will be deducing from the
11808 pattern, and record their current deductions. */
11809 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
11810 pack; pack = TREE_CHAIN (pack))
11812 tree parm_pack = TREE_VALUE (pack);
11813 int idx, level;
11815 /* Determine the index and level of this parameter pack. */
11816 template_parm_level_and_index (parm_pack, &level, &idx);
11818 /* Keep track of the parameter packs and their corresponding
11819 argument packs. */
11820 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
11821 TREE_TYPE (packs) = make_tree_vec (len - start);
11824 /* Loop through all of the arguments that have not yet been
11825 unified and unify each with the pattern. */
11826 for (i = start; i < len; i++)
11828 tree parm = pattern;
11830 /* For each parameter pack, clear out the deduced value so that
11831 we can deduce it again. */
11832 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11834 int idx, level;
11835 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
11837 TMPL_ARG (targs, level, idx) = NULL_TREE;
11840 /* Unify the pattern with the current argument. */
11842 tree arg = TREE_VEC_ELT (packed_args, i);
11843 int arg_strict = strict;
11844 bool skip_arg_p = false;
11846 if (call_args_p)
11848 int sub_strict;
11850 /* This mirrors what we do in type_unification_real. */
11851 switch (strict)
11853 case DEDUCE_CALL:
11854 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
11855 | UNIFY_ALLOW_MORE_CV_QUAL
11856 | UNIFY_ALLOW_DERIVED);
11857 break;
11859 case DEDUCE_CONV:
11860 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
11861 break;
11863 case DEDUCE_EXACT:
11864 sub_strict = UNIFY_ALLOW_NONE;
11865 break;
11867 default:
11868 gcc_unreachable ();
11871 if (!TYPE_P (arg))
11873 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
11874 if (type_unknown_p (arg))
11876 /* [temp.deduct.type] A template-argument can be
11877 deduced from a pointer to function or pointer
11878 to member function argument if the set of
11879 overloaded functions does not contain function
11880 templates and at most one of a set of
11881 overloaded functions provides a unique
11882 match. */
11884 if (resolve_overloaded_unification
11885 (tparms, targs, parm, arg, strict, sub_strict)
11886 != 0)
11887 return 1;
11888 skip_arg_p = true;
11891 if (!skip_arg_p)
11893 arg = TREE_TYPE (arg);
11894 if (arg == error_mark_node)
11895 return 1;
11899 arg_strict = sub_strict;
11901 if (!subr)
11902 arg_strict |=
11903 maybe_adjust_types_for_deduction (strict, &parm, &arg);
11906 if (!skip_arg_p)
11908 if (unify (tparms, targs, parm, arg, arg_strict))
11909 return 1;
11913 /* For each parameter pack, collect the deduced value. */
11914 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11916 int idx, level;
11917 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
11919 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
11920 TMPL_ARG (targs, level, idx);
11924 /* Verify that the results of unification with the parameter packs
11925 produce results consistent with what we've seen before, and make
11926 the deduced argument packs available. */
11927 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11929 tree old_pack = TREE_VALUE (pack);
11930 tree new_args = TREE_TYPE (pack);
11932 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
11934 /* Prepend the explicit arguments onto NEW_ARGS. */
11935 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
11936 tree old_args = new_args;
11937 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
11938 int len = explicit_len + TREE_VEC_LENGTH (old_args);
11940 /* Copy the explicit arguments. */
11941 new_args = make_tree_vec (len);
11942 for (i = 0; i < explicit_len; i++)
11943 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
11945 /* Copy the deduced arguments. */
11946 for (; i < len; i++)
11947 TREE_VEC_ELT (new_args, i) =
11948 TREE_VEC_ELT (old_args, i - explicit_len);
11951 if (!old_pack)
11953 tree result;
11954 int idx, level;
11956 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
11958 /* Build the deduced *_ARGUMENT_PACK. */
11959 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
11961 result = make_node (NONTYPE_ARGUMENT_PACK);
11962 TREE_TYPE (result) =
11963 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
11964 TREE_CONSTANT (result) = 1;
11966 else
11967 result = make_node (TYPE_ARGUMENT_PACK);
11969 SET_ARGUMENT_PACK_ARGS (result, new_args);
11971 /* Note the deduced argument packs for this parameter
11972 pack. */
11973 TMPL_ARG (targs, level, idx) = result;
11975 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
11976 && (ARGUMENT_PACK_ARGS (old_pack)
11977 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
11979 /* We only had the explicitly-provided arguments before, but
11980 now we have a complete set of arguments. */
11981 int idx, level;
11982 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
11983 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
11985 /* Keep the original deduced argument pack. */
11986 TMPL_ARG (targs, level, idx) = old_pack;
11988 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
11989 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
11990 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
11992 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
11993 new_args))
11994 /* Inconsistent unification of this parameter pack. */
11995 return 1;
11996 else
11998 int idx, level;
12000 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12002 /* Keep the original deduced argument pack. */
12003 TMPL_ARG (targs, level, idx) = old_pack;
12007 return 0;
12010 /* Deduce the value of template parameters. TPARMS is the (innermost)
12011 set of template parameters to a template. TARGS is the bindings
12012 for those template parameters, as determined thus far; TARGS may
12013 include template arguments for outer levels of template parameters
12014 as well. PARM is a parameter to a template function, or a
12015 subcomponent of that parameter; ARG is the corresponding argument.
12016 This function attempts to match PARM with ARG in a manner
12017 consistent with the existing assignments in TARGS. If more values
12018 are deduced, then TARGS is updated.
12020 Returns 0 if the type deduction succeeds, 1 otherwise. The
12021 parameter STRICT is a bitwise or of the following flags:
12023 UNIFY_ALLOW_NONE:
12024 Require an exact match between PARM and ARG.
12025 UNIFY_ALLOW_MORE_CV_QUAL:
12026 Allow the deduced ARG to be more cv-qualified (by qualification
12027 conversion) than ARG.
12028 UNIFY_ALLOW_LESS_CV_QUAL:
12029 Allow the deduced ARG to be less cv-qualified than ARG.
12030 UNIFY_ALLOW_DERIVED:
12031 Allow the deduced ARG to be a template base class of ARG,
12032 or a pointer to a template base class of the type pointed to by
12033 ARG.
12034 UNIFY_ALLOW_INTEGER:
12035 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12036 case for more information.
12037 UNIFY_ALLOW_OUTER_LEVEL:
12038 This is the outermost level of a deduction. Used to determine validity
12039 of qualification conversions. A valid qualification conversion must
12040 have const qualified pointers leading up to the inner type which
12041 requires additional CV quals, except at the outer level, where const
12042 is not required [conv.qual]. It would be normal to set this flag in
12043 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12044 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12045 This is the outermost level of a deduction, and PARM can be more CV
12046 qualified at this point.
12047 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12048 This is the outermost level of a deduction, and PARM can be less CV
12049 qualified at this point. */
12051 static int
12052 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
12054 int idx;
12055 tree targ;
12056 tree tparm;
12057 int strict_in = strict;
12059 /* I don't think this will do the right thing with respect to types.
12060 But the only case I've seen it in so far has been array bounds, where
12061 signedness is the only information lost, and I think that will be
12062 okay. */
12063 while (TREE_CODE (parm) == NOP_EXPR)
12064 parm = TREE_OPERAND (parm, 0);
12066 if (arg == error_mark_node)
12067 return 1;
12068 if (arg == unknown_type_node)
12069 /* We can't deduce anything from this, but we might get all the
12070 template args from other function args. */
12071 return 0;
12073 /* If PARM uses template parameters, then we can't bail out here,
12074 even if ARG == PARM, since we won't record unifications for the
12075 template parameters. We might need them if we're trying to
12076 figure out which of two things is more specialized. */
12077 if (arg == parm && !uses_template_parms (parm))
12078 return 0;
12080 /* Immediately reject some pairs that won't unify because of
12081 cv-qualification mismatches. */
12082 if (TREE_CODE (arg) == TREE_CODE (parm)
12083 && TYPE_P (arg)
12084 /* It is the elements of the array which hold the cv quals of an array
12085 type, and the elements might be template type parms. We'll check
12086 when we recurse. */
12087 && TREE_CODE (arg) != ARRAY_TYPE
12088 /* We check the cv-qualifiers when unifying with template type
12089 parameters below. We want to allow ARG `const T' to unify with
12090 PARM `T' for example, when computing which of two templates
12091 is more specialized, for example. */
12092 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
12093 && !check_cv_quals_for_unify (strict_in, arg, parm))
12094 return 1;
12096 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
12097 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
12098 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
12099 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
12100 strict &= ~UNIFY_ALLOW_DERIVED;
12101 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12102 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
12104 switch (TREE_CODE (parm))
12106 case TYPENAME_TYPE:
12107 case SCOPE_REF:
12108 case UNBOUND_CLASS_TEMPLATE:
12109 /* In a type which contains a nested-name-specifier, template
12110 argument values cannot be deduced for template parameters used
12111 within the nested-name-specifier. */
12112 return 0;
12114 case TEMPLATE_TYPE_PARM:
12115 case TEMPLATE_TEMPLATE_PARM:
12116 case BOUND_TEMPLATE_TEMPLATE_PARM:
12117 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12119 if (TEMPLATE_TYPE_LEVEL (parm)
12120 != template_decl_level (tparm))
12121 /* The PARM is not one we're trying to unify. Just check
12122 to see if it matches ARG. */
12123 return (TREE_CODE (arg) == TREE_CODE (parm)
12124 && same_type_p (parm, arg)) ? 0 : 1;
12125 idx = TEMPLATE_TYPE_IDX (parm);
12126 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12127 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
12129 /* Check for mixed types and values. */
12130 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12131 && TREE_CODE (tparm) != TYPE_DECL)
12132 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12133 && TREE_CODE (tparm) != TEMPLATE_DECL))
12134 return 1;
12136 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12138 /* ARG must be constructed from a template class or a template
12139 template parameter. */
12140 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
12141 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
12142 return 1;
12145 tree parmvec = TYPE_TI_ARGS (parm);
12146 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
12147 tree argtmplvec
12148 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
12149 int i;
12151 /* The resolution to DR150 makes clear that default
12152 arguments for an N-argument may not be used to bind T
12153 to a template template parameter with fewer than N
12154 parameters. It is not safe to permit the binding of
12155 default arguments as an extension, as that may change
12156 the meaning of a conforming program. Consider:
12158 struct Dense { static const unsigned int dim = 1; };
12160 template <template <typename> class View,
12161 typename Block>
12162 void operator+(float, View<Block> const&);
12164 template <typename Block,
12165 unsigned int Dim = Block::dim>
12166 struct Lvalue_proxy { operator float() const; };
12168 void
12169 test_1d (void) {
12170 Lvalue_proxy<Dense> p;
12171 float b;
12172 b + p;
12175 Here, if Lvalue_proxy is permitted to bind to View, then
12176 the global operator+ will be used; if they are not, the
12177 Lvalue_proxy will be converted to float. */
12178 if (coerce_template_parms (argtmplvec, parmvec,
12179 TYPE_TI_TEMPLATE (parm),
12180 tf_none,
12181 /*require_all_args=*/true,
12182 /*use_default_args=*/false)
12183 == error_mark_node)
12184 return 1;
12186 /* Deduce arguments T, i from TT<T> or TT<i>.
12187 We check each element of PARMVEC and ARGVEC individually
12188 rather than the whole TREE_VEC since they can have
12189 different number of elements. */
12191 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
12193 if (unify (tparms, targs,
12194 TREE_VEC_ELT (parmvec, i),
12195 TREE_VEC_ELT (argvec, i),
12196 UNIFY_ALLOW_NONE))
12197 return 1;
12200 arg = TYPE_TI_TEMPLATE (arg);
12202 /* Fall through to deduce template name. */
12205 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12206 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12208 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12210 /* Simple cases: Value already set, does match or doesn't. */
12211 if (targ != NULL_TREE && template_args_equal (targ, arg))
12212 return 0;
12213 else if (targ)
12214 return 1;
12216 else
12218 /* If PARM is `const T' and ARG is only `int', we don't have
12219 a match unless we are allowing additional qualification.
12220 If ARG is `const int' and PARM is just `T' that's OK;
12221 that binds `const int' to `T'. */
12222 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
12223 arg, parm))
12224 return 1;
12226 /* Consider the case where ARG is `const volatile int' and
12227 PARM is `const T'. Then, T should be `volatile int'. */
12228 arg = cp_build_qualified_type_real
12229 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
12230 if (arg == error_mark_node)
12231 return 1;
12233 /* Simple cases: Value already set, does match or doesn't. */
12234 if (targ != NULL_TREE && same_type_p (targ, arg))
12235 return 0;
12236 else if (targ)
12237 return 1;
12239 /* Make sure that ARG is not a variable-sized array. (Note
12240 that were talking about variable-sized arrays (like
12241 `int[n]'), rather than arrays of unknown size (like
12242 `int[]').) We'll get very confused by such a type since
12243 the bound of the array will not be computable in an
12244 instantiation. Besides, such types are not allowed in
12245 ISO C++, so we can do as we please here. */
12246 if (variably_modified_type_p (arg, NULL_TREE))
12247 return 1;
12250 /* If ARG is a parameter pack or an expansion, we cannot unify
12251 against it unless PARM is also a parameter pack. */
12252 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12253 && !template_parameter_pack_p (parm))
12254 return 1;
12256 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12257 return 0;
12259 case TEMPLATE_PARM_INDEX:
12260 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12261 if (tparm == error_mark_node)
12262 return 1;
12264 if (TEMPLATE_PARM_LEVEL (parm)
12265 != template_decl_level (tparm))
12266 /* The PARM is not one we're trying to unify. Just check
12267 to see if it matches ARG. */
12268 return !(TREE_CODE (arg) == TREE_CODE (parm)
12269 && cp_tree_equal (parm, arg));
12271 idx = TEMPLATE_PARM_IDX (parm);
12272 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12274 if (targ)
12275 return !cp_tree_equal (targ, arg);
12277 /* [temp.deduct.type] If, in the declaration of a function template
12278 with a non-type template-parameter, the non-type
12279 template-parameter is used in an expression in the function
12280 parameter-list and, if the corresponding template-argument is
12281 deduced, the template-argument type shall match the type of the
12282 template-parameter exactly, except that a template-argument
12283 deduced from an array bound may be of any integral type.
12284 The non-type parameter might use already deduced type parameters. */
12285 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
12286 if (!TREE_TYPE (arg))
12287 /* Template-parameter dependent expression. Just accept it for now.
12288 It will later be processed in convert_template_argument. */
12290 else if (same_type_p (TREE_TYPE (arg), tparm))
12291 /* OK */;
12292 else if ((strict & UNIFY_ALLOW_INTEGER)
12293 && (TREE_CODE (tparm) == INTEGER_TYPE
12294 || TREE_CODE (tparm) == BOOLEAN_TYPE))
12295 /* Convert the ARG to the type of PARM; the deduced non-type
12296 template argument must exactly match the types of the
12297 corresponding parameter. */
12298 arg = fold (build_nop (TREE_TYPE (parm), arg));
12299 else if (uses_template_parms (tparm))
12300 /* We haven't deduced the type of this parameter yet. Try again
12301 later. */
12302 return 0;
12303 else
12304 return 1;
12306 /* If ARG is a parameter pack or an expansion, we cannot unify
12307 against it unless PARM is also a parameter pack. */
12308 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12309 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
12310 return 1;
12312 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12313 return 0;
12315 case PTRMEM_CST:
12317 /* A pointer-to-member constant can be unified only with
12318 another constant. */
12319 if (TREE_CODE (arg) != PTRMEM_CST)
12320 return 1;
12322 /* Just unify the class member. It would be useless (and possibly
12323 wrong, depending on the strict flags) to unify also
12324 PTRMEM_CST_CLASS, because we want to be sure that both parm and
12325 arg refer to the same variable, even if through different
12326 classes. For instance:
12328 struct A { int x; };
12329 struct B : A { };
12331 Unification of &A::x and &B::x must succeed. */
12332 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
12333 PTRMEM_CST_MEMBER (arg), strict);
12336 case POINTER_TYPE:
12338 if (TREE_CODE (arg) != POINTER_TYPE)
12339 return 1;
12341 /* [temp.deduct.call]
12343 A can be another pointer or pointer to member type that can
12344 be converted to the deduced A via a qualification
12345 conversion (_conv.qual_).
12347 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
12348 This will allow for additional cv-qualification of the
12349 pointed-to types if appropriate. */
12351 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
12352 /* The derived-to-base conversion only persists through one
12353 level of pointers. */
12354 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
12356 return unify (tparms, targs, TREE_TYPE (parm),
12357 TREE_TYPE (arg), strict);
12360 case REFERENCE_TYPE:
12361 if (TREE_CODE (arg) != REFERENCE_TYPE)
12362 return 1;
12363 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12364 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12366 case ARRAY_TYPE:
12367 if (TREE_CODE (arg) != ARRAY_TYPE)
12368 return 1;
12369 if ((TYPE_DOMAIN (parm) == NULL_TREE)
12370 != (TYPE_DOMAIN (arg) == NULL_TREE))
12371 return 1;
12372 if (TYPE_DOMAIN (parm) != NULL_TREE)
12374 tree parm_max;
12375 tree arg_max;
12376 bool parm_cst;
12377 bool arg_cst;
12379 /* Our representation of array types uses "N - 1" as the
12380 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
12381 not an integer constant. We cannot unify arbitrarily
12382 complex expressions, so we eliminate the MINUS_EXPRs
12383 here. */
12384 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
12385 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
12386 if (!parm_cst)
12388 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
12389 parm_max = TREE_OPERAND (parm_max, 0);
12391 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
12392 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
12393 if (!arg_cst)
12395 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
12396 trying to unify the type of a variable with the type
12397 of a template parameter. For example:
12399 template <unsigned int N>
12400 void f (char (&) [N]);
12401 int g();
12402 void h(int i) {
12403 char a[g(i)];
12404 f(a);
12407 Here, the type of the ARG will be "int [g(i)]", and
12408 may be a SAVE_EXPR, etc. */
12409 if (TREE_CODE (arg_max) != MINUS_EXPR)
12410 return 1;
12411 arg_max = TREE_OPERAND (arg_max, 0);
12414 /* If only one of the bounds used a MINUS_EXPR, compensate
12415 by adding one to the other bound. */
12416 if (parm_cst && !arg_cst)
12417 parm_max = fold_build2 (PLUS_EXPR,
12418 integer_type_node,
12419 parm_max,
12420 integer_one_node);
12421 else if (arg_cst && !parm_cst)
12422 arg_max = fold_build2 (PLUS_EXPR,
12423 integer_type_node,
12424 arg_max,
12425 integer_one_node);
12427 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
12428 return 1;
12430 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12431 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12433 case REAL_TYPE:
12434 case COMPLEX_TYPE:
12435 case VECTOR_TYPE:
12436 case INTEGER_TYPE:
12437 case BOOLEAN_TYPE:
12438 case ENUMERAL_TYPE:
12439 case VOID_TYPE:
12440 if (TREE_CODE (arg) != TREE_CODE (parm))
12441 return 1;
12443 /* We have already checked cv-qualification at the top of the
12444 function. */
12445 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
12446 return 1;
12448 /* As far as unification is concerned, this wins. Later checks
12449 will invalidate it if necessary. */
12450 return 0;
12452 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
12453 /* Type INTEGER_CST can come from ordinary constant template args. */
12454 case INTEGER_CST:
12455 while (TREE_CODE (arg) == NOP_EXPR)
12456 arg = TREE_OPERAND (arg, 0);
12458 if (TREE_CODE (arg) != INTEGER_CST)
12459 return 1;
12460 return !tree_int_cst_equal (parm, arg);
12462 case TREE_VEC:
12464 int i;
12465 if (TREE_CODE (arg) != TREE_VEC)
12466 return 1;
12467 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
12468 return 1;
12469 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
12470 if (unify (tparms, targs,
12471 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
12472 UNIFY_ALLOW_NONE))
12473 return 1;
12474 return 0;
12477 case RECORD_TYPE:
12478 case UNION_TYPE:
12479 if (TREE_CODE (arg) != TREE_CODE (parm))
12480 return 1;
12482 if (TYPE_PTRMEMFUNC_P (parm))
12484 if (!TYPE_PTRMEMFUNC_P (arg))
12485 return 1;
12487 return unify (tparms, targs,
12488 TYPE_PTRMEMFUNC_FN_TYPE (parm),
12489 TYPE_PTRMEMFUNC_FN_TYPE (arg),
12490 strict);
12493 if (CLASSTYPE_TEMPLATE_INFO (parm))
12495 tree t = NULL_TREE;
12497 if (strict_in & UNIFY_ALLOW_DERIVED)
12499 /* First, we try to unify the PARM and ARG directly. */
12500 t = try_class_unification (tparms, targs,
12501 parm, arg);
12503 if (!t)
12505 /* Fallback to the special case allowed in
12506 [temp.deduct.call]:
12508 If P is a class, and P has the form
12509 template-id, then A can be a derived class of
12510 the deduced A. Likewise, if P is a pointer to
12511 a class of the form template-id, A can be a
12512 pointer to a derived class pointed to by the
12513 deduced A. */
12514 t = get_template_base (tparms, targs, parm, arg);
12516 if (!t)
12517 return 1;
12520 else if (CLASSTYPE_TEMPLATE_INFO (arg)
12521 && (CLASSTYPE_TI_TEMPLATE (parm)
12522 == CLASSTYPE_TI_TEMPLATE (arg)))
12523 /* Perhaps PARM is something like S<U> and ARG is S<int>.
12524 Then, we should unify `int' and `U'. */
12525 t = arg;
12526 else
12527 /* There's no chance of unification succeeding. */
12528 return 1;
12530 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
12531 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
12533 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
12534 return 1;
12535 return 0;
12537 case METHOD_TYPE:
12538 case FUNCTION_TYPE:
12539 if (TREE_CODE (arg) != TREE_CODE (parm))
12540 return 1;
12542 /* CV qualifications for methods can never be deduced, they must
12543 match exactly. We need to check them explicitly here,
12544 because type_unification_real treats them as any other
12545 cvqualified parameter. */
12546 if (TREE_CODE (parm) == METHOD_TYPE
12547 && (!check_cv_quals_for_unify
12548 (UNIFY_ALLOW_NONE,
12549 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
12550 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
12551 return 1;
12553 if (unify (tparms, targs, TREE_TYPE (parm),
12554 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
12555 return 1;
12556 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
12557 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
12558 LOOKUP_NORMAL);
12560 case OFFSET_TYPE:
12561 /* Unify a pointer to member with a pointer to member function, which
12562 deduces the type of the member as a function type. */
12563 if (TYPE_PTRMEMFUNC_P (arg))
12565 tree method_type;
12566 tree fntype;
12567 cp_cv_quals cv_quals;
12569 /* Check top-level cv qualifiers */
12570 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
12571 return 1;
12573 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12574 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
12575 return 1;
12577 /* Determine the type of the function we are unifying against. */
12578 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
12579 fntype =
12580 build_function_type (TREE_TYPE (method_type),
12581 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
12583 /* Extract the cv-qualifiers of the member function from the
12584 implicit object parameter and place them on the function
12585 type to be restored later. */
12586 cv_quals =
12587 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
12588 fntype = build_qualified_type (fntype, cv_quals);
12589 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
12592 if (TREE_CODE (arg) != OFFSET_TYPE)
12593 return 1;
12594 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12595 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
12596 return 1;
12597 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12598 strict);
12600 case CONST_DECL:
12601 if (DECL_TEMPLATE_PARM_P (parm))
12602 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
12603 if (arg != integral_constant_value (parm))
12604 return 1;
12605 return 0;
12607 case FIELD_DECL:
12608 case TEMPLATE_DECL:
12609 /* Matched cases are handled by the ARG == PARM test above. */
12610 return 1;
12612 case TYPE_ARGUMENT_PACK:
12613 case NONTYPE_ARGUMENT_PACK:
12615 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
12616 tree packed_args = ARGUMENT_PACK_ARGS (arg);
12617 int i, len = TREE_VEC_LENGTH (packed_parms);
12618 int argslen = TREE_VEC_LENGTH (packed_args);
12619 int parm_variadic_p = 0;
12621 /* Check if the parameters end in a pack, making them variadic. */
12622 if (len > 0
12623 && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1)))
12624 parm_variadic_p = 1;
12626 /* If we don't have enough arguments to satisfy the parameters
12627 (not counting the pack expression at the end), or we have
12628 too many arguments for a parameter list that doesn't end in
12629 a pack expression, we can't unify. */
12630 if (argslen < (len - parm_variadic_p)
12631 || (argslen > len && !parm_variadic_p))
12632 return 1;
12634 /* Unify all of the parameters that precede the (optional)
12635 pack expression. */
12636 for (i = 0; i < len - parm_variadic_p; ++i)
12638 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
12639 TREE_VEC_ELT (packed_args, i), strict))
12640 return 1;
12643 if (parm_variadic_p)
12644 return unify_pack_expansion (tparms, targs,
12645 packed_parms, packed_args,
12646 strict, /*call_args_p=*/false,
12647 /*subr=*/false);
12648 return 0;
12651 break;
12653 default:
12654 gcc_assert (EXPR_P (parm));
12656 /* We must be looking at an expression. This can happen with
12657 something like:
12659 template <int I>
12660 void foo(S<I>, S<I + 2>);
12662 This is a "nondeduced context":
12664 [deduct.type]
12666 The nondeduced contexts are:
12668 --A type that is a template-id in which one or more of
12669 the template-arguments is an expression that references
12670 a template-parameter.
12672 In these cases, we assume deduction succeeded, but don't
12673 actually infer any unifications. */
12675 if (!uses_template_parms (parm)
12676 && !template_args_equal (parm, arg))
12677 return 1;
12678 else
12679 return 0;
12683 /* Note that DECL can be defined in this translation unit, if
12684 required. */
12686 static void
12687 mark_definable (tree decl)
12689 tree clone;
12690 DECL_NOT_REALLY_EXTERN (decl) = 1;
12691 FOR_EACH_CLONE (clone, decl)
12692 DECL_NOT_REALLY_EXTERN (clone) = 1;
12695 /* Called if RESULT is explicitly instantiated, or is a member of an
12696 explicitly instantiated class. */
12698 void
12699 mark_decl_instantiated (tree result, int extern_p)
12701 SET_DECL_EXPLICIT_INSTANTIATION (result);
12703 /* If this entity has already been written out, it's too late to
12704 make any modifications. */
12705 if (TREE_ASM_WRITTEN (result))
12706 return;
12708 if (TREE_CODE (result) != FUNCTION_DECL)
12709 /* The TREE_PUBLIC flag for function declarations will have been
12710 set correctly by tsubst. */
12711 TREE_PUBLIC (result) = 1;
12713 /* This might have been set by an earlier implicit instantiation. */
12714 DECL_COMDAT (result) = 0;
12716 if (extern_p)
12717 DECL_NOT_REALLY_EXTERN (result) = 0;
12718 else
12720 mark_definable (result);
12721 /* Always make artificials weak. */
12722 if (DECL_ARTIFICIAL (result) && flag_weak)
12723 comdat_linkage (result);
12724 /* For WIN32 we also want to put explicit instantiations in
12725 linkonce sections. */
12726 else if (TREE_PUBLIC (result))
12727 maybe_make_one_only (result);
12730 /* If EXTERN_P, then this function will not be emitted -- unless
12731 followed by an explicit instantiation, at which point its linkage
12732 will be adjusted. If !EXTERN_P, then this function will be
12733 emitted here. In neither circumstance do we want
12734 import_export_decl to adjust the linkage. */
12735 DECL_INTERFACE_KNOWN (result) = 1;
12738 /* Given two function templates PAT1 and PAT2, return:
12740 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
12741 -1 if PAT2 is more specialized than PAT1.
12742 0 if neither is more specialized.
12744 LEN indicates the number of parameters we should consider
12745 (defaulted parameters should not be considered).
12747 The 1998 std underspecified function template partial ordering, and
12748 DR214 addresses the issue. We take pairs of arguments, one from
12749 each of the templates, and deduce them against each other. One of
12750 the templates will be more specialized if all the *other*
12751 template's arguments deduce against its arguments and at least one
12752 of its arguments *does* *not* deduce against the other template's
12753 corresponding argument. Deduction is done as for class templates.
12754 The arguments used in deduction have reference and top level cv
12755 qualifiers removed. Iff both arguments were originally reference
12756 types *and* deduction succeeds in both directions, the template
12757 with the more cv-qualified argument wins for that pairing (if
12758 neither is more cv-qualified, they both are equal). Unlike regular
12759 deduction, after all the arguments have been deduced in this way,
12760 we do *not* verify the deduced template argument values can be
12761 substituted into non-deduced contexts, nor do we have to verify
12762 that all template arguments have been deduced. */
12765 more_specialized_fn (tree pat1, tree pat2, int len)
12767 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
12768 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
12769 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
12770 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
12771 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
12772 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
12773 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
12774 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
12775 int better1 = 0;
12776 int better2 = 0;
12778 /* Remove the this parameter from non-static member functions. If
12779 one is a non-static member function and the other is not a static
12780 member function, remove the first parameter from that function
12781 also. This situation occurs for operator functions where we
12782 locate both a member function (with this pointer) and non-member
12783 operator (with explicit first operand). */
12784 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
12786 len--; /* LEN is the number of significant arguments for DECL1 */
12787 args1 = TREE_CHAIN (args1);
12788 if (!DECL_STATIC_FUNCTION_P (decl2))
12789 args2 = TREE_CHAIN (args2);
12791 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
12793 args2 = TREE_CHAIN (args2);
12794 if (!DECL_STATIC_FUNCTION_P (decl1))
12796 len--;
12797 args1 = TREE_CHAIN (args1);
12801 /* If only one is a conversion operator, they are unordered. */
12802 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
12803 return 0;
12805 /* Consider the return type for a conversion function */
12806 if (DECL_CONV_FN_P (decl1))
12808 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
12809 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
12810 len++;
12813 processing_template_decl++;
12815 while (len--)
12817 tree arg1 = TREE_VALUE (args1);
12818 tree arg2 = TREE_VALUE (args2);
12819 int deduce1, deduce2;
12820 int quals1 = -1;
12821 int quals2 = -1;
12823 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
12824 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
12826 /* When both arguments are pack expansions, we need only
12827 unify the patterns themselves. */
12828 arg1 = PACK_EXPANSION_PATTERN (arg1);
12829 arg2 = PACK_EXPANSION_PATTERN (arg2);
12831 /* This is the last comparison we need to do. */
12832 len = 0;
12835 if (TREE_CODE (arg1) == REFERENCE_TYPE)
12837 arg1 = TREE_TYPE (arg1);
12838 quals1 = cp_type_quals (arg1);
12841 if (TREE_CODE (arg2) == REFERENCE_TYPE)
12843 arg2 = TREE_TYPE (arg2);
12844 quals2 = cp_type_quals (arg2);
12847 if ((quals1 < 0) != (quals2 < 0))
12849 /* Only of the args is a reference, see if we should apply
12850 array/function pointer decay to it. This is not part of
12851 DR214, but is, IMHO, consistent with the deduction rules
12852 for the function call itself, and with our earlier
12853 implementation of the underspecified partial ordering
12854 rules. (nathan). */
12855 if (quals1 >= 0)
12857 switch (TREE_CODE (arg1))
12859 case ARRAY_TYPE:
12860 arg1 = TREE_TYPE (arg1);
12861 /* FALLTHROUGH. */
12862 case FUNCTION_TYPE:
12863 arg1 = build_pointer_type (arg1);
12864 break;
12866 default:
12867 break;
12870 else
12872 switch (TREE_CODE (arg2))
12874 case ARRAY_TYPE:
12875 arg2 = TREE_TYPE (arg2);
12876 /* FALLTHROUGH. */
12877 case FUNCTION_TYPE:
12878 arg2 = build_pointer_type (arg2);
12879 break;
12881 default:
12882 break;
12887 arg1 = TYPE_MAIN_VARIANT (arg1);
12888 arg2 = TYPE_MAIN_VARIANT (arg2);
12890 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
12892 int i, len2 = len + 1;
12893 tree parmvec = make_tree_vec (1);
12894 tree argvec = make_tree_vec (len2);
12895 tree ta = args2;
12897 /* Setup the parameter vector, which contains only ARG1. */
12898 TREE_VEC_ELT (parmvec, 0) = arg1;
12900 /* Setup the argument vector, which contains the remaining
12901 arguments. */
12902 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
12903 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
12905 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
12906 argvec, UNIFY_ALLOW_NONE,
12907 /*call_args_p=*/false,
12908 /*subr=*/0);
12910 /* We cannot deduce in the other direction, because ARG1 is
12911 a pack expansion but ARG2 is not. */
12912 deduce2 = 0;
12914 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
12916 int i, len1 = len + 1;
12917 tree parmvec = make_tree_vec (1);
12918 tree argvec = make_tree_vec (len1);
12919 tree ta = args1;
12921 /* Setup the parameter vector, which contains only ARG1. */
12922 TREE_VEC_ELT (parmvec, 0) = arg2;
12924 /* Setup the argument vector, which contains the remaining
12925 arguments. */
12926 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
12927 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
12929 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
12930 argvec, UNIFY_ALLOW_NONE,
12931 /*call_args_p=*/false,
12932 /*subr=*/0);
12934 /* We cannot deduce in the other direction, because ARG2 is
12935 a pack expansion but ARG1 is not.*/
12936 deduce1 = 0;
12939 else
12941 /* The normal case, where neither argument is a pack
12942 expansion. */
12943 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
12944 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
12947 if (!deduce1)
12948 better2 = -1;
12949 if (!deduce2)
12950 better1 = -1;
12951 if (better1 < 0 && better2 < 0)
12952 /* We've failed to deduce something in either direction.
12953 These must be unordered. */
12954 break;
12956 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
12958 /* Deduces in both directions, see if quals can
12959 disambiguate. Pretend the worse one failed to deduce. */
12960 if ((quals1 & quals2) == quals2)
12961 deduce1 = 0;
12962 if ((quals1 & quals2) == quals1)
12963 deduce2 = 0;
12965 if (deduce1 && !deduce2 && !better2)
12966 better2 = 1;
12967 if (deduce2 && !deduce1 && !better1)
12968 better1 = 1;
12970 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
12971 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
12972 /* We have already processed all of the arguments in our
12973 handing of the pack expansion type. */
12974 len = 0;
12976 args1 = TREE_CHAIN (args1);
12977 args2 = TREE_CHAIN (args2);
12980 processing_template_decl--;
12982 /* All things being equal, if the next argument is a pack expansion
12983 for one function but not for the other, prefer the
12984 non-variadic function. */
12985 if ((better1 > 0) - (better2 > 0) == 0
12986 && args1 && TREE_VALUE (args1)
12987 && args2 && TREE_VALUE (args2))
12989 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
12990 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
12991 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
12992 return 1;
12995 return (better1 > 0) - (better2 > 0);
12998 /* Determine which of two partial specializations is more specialized.
13000 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13001 to the first partial specialization. The TREE_VALUE is the
13002 innermost set of template parameters for the partial
13003 specialization. PAT2 is similar, but for the second template.
13005 Return 1 if the first partial specialization is more specialized;
13006 -1 if the second is more specialized; 0 if neither is more
13007 specialized.
13009 See [temp.class.order] for information about determining which of
13010 two templates is more specialized. */
13012 static int
13013 more_specialized_class (tree pat1, tree pat2)
13015 tree targs;
13016 tree tmpl1, tmpl2;
13017 int winner = 0;
13018 bool any_deductions = false;
13020 tmpl1 = TREE_TYPE (pat1);
13021 tmpl2 = TREE_TYPE (pat2);
13023 /* Just like what happens for functions, if we are ordering between
13024 different class template specializations, we may encounter dependent
13025 types in the arguments, and we need our dependency check functions
13026 to behave correctly. */
13027 ++processing_template_decl;
13028 targs = get_class_bindings (TREE_VALUE (pat1),
13029 CLASSTYPE_TI_ARGS (tmpl1),
13030 CLASSTYPE_TI_ARGS (tmpl2));
13031 if (targs)
13033 --winner;
13034 any_deductions = true;
13037 targs = get_class_bindings (TREE_VALUE (pat2),
13038 CLASSTYPE_TI_ARGS (tmpl2),
13039 CLASSTYPE_TI_ARGS (tmpl1));
13040 if (targs)
13042 ++winner;
13043 any_deductions = true;
13045 --processing_template_decl;
13047 /* In the case of a tie where at least one of the class templates
13048 has a parameter pack at the end, the template with the most
13049 non-packed parameters wins. */
13050 if (winner == 0
13051 && any_deductions
13052 && (template_args_variadic_p (TREE_PURPOSE (pat1))
13053 || template_args_variadic_p (TREE_PURPOSE (pat2))))
13055 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
13056 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
13057 int len1 = TREE_VEC_LENGTH (args1);
13058 int len2 = TREE_VEC_LENGTH (args2);
13060 /* We don't count the pack expansion at the end. */
13061 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
13062 --len1;
13063 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
13064 --len2;
13066 if (len1 > len2)
13067 return 1;
13068 else if (len1 < len2)
13069 return -1;
13072 return winner;
13075 /* Return the template arguments that will produce the function signature
13076 DECL from the function template FN, with the explicit template
13077 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13078 also match. Return NULL_TREE if no satisfactory arguments could be
13079 found. */
13081 static tree
13082 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
13084 int ntparms = DECL_NTPARMS (fn);
13085 tree targs = make_tree_vec (ntparms);
13086 tree decl_type;
13087 tree decl_arg_types;
13089 /* Substitute the explicit template arguments into the type of DECL.
13090 The call to fn_type_unification will handle substitution into the
13091 FN. */
13092 decl_type = TREE_TYPE (decl);
13093 if (explicit_args && uses_template_parms (decl_type))
13095 tree tmpl;
13096 tree converted_args;
13098 if (DECL_TEMPLATE_INFO (decl))
13099 tmpl = DECL_TI_TEMPLATE (decl);
13100 else
13101 /* We can get here for some invalid specializations. */
13102 return NULL_TREE;
13104 converted_args
13105 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
13106 explicit_args, NULL_TREE,
13107 tf_none,
13108 /*require_all_args=*/false,
13109 /*use_default_args=*/false);
13110 if (converted_args == error_mark_node)
13111 return NULL_TREE;
13113 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
13114 if (decl_type == error_mark_node)
13115 return NULL_TREE;
13118 /* Never do unification on the 'this' parameter. */
13119 decl_arg_types = skip_artificial_parms_for (decl,
13120 TYPE_ARG_TYPES (decl_type));
13122 if (fn_type_unification (fn, explicit_args, targs,
13123 decl_arg_types,
13124 (check_rettype || DECL_CONV_FN_P (fn)
13125 ? TREE_TYPE (decl_type) : NULL_TREE),
13126 DEDUCE_EXACT, LOOKUP_NORMAL))
13127 return NULL_TREE;
13129 return targs;
13132 /* Return the innermost template arguments that, when applied to a
13133 template specialization whose innermost template parameters are
13134 TPARMS, and whose specialization arguments are PARMS, yield the
13135 ARGS.
13137 For example, suppose we have:
13139 template <class T, class U> struct S {};
13140 template <class T> struct S<T*, int> {};
13142 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13143 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13144 int}. The resulting vector will be {double}, indicating that `T'
13145 is bound to `double'. */
13147 static tree
13148 get_class_bindings (tree tparms, tree spec_args, tree args)
13150 int i, ntparms = TREE_VEC_LENGTH (tparms);
13151 tree deduced_args;
13152 tree innermost_deduced_args;
13154 innermost_deduced_args = make_tree_vec (ntparms);
13155 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13157 deduced_args = copy_node (args);
13158 SET_TMPL_ARGS_LEVEL (deduced_args,
13159 TMPL_ARGS_DEPTH (deduced_args),
13160 innermost_deduced_args);
13162 else
13163 deduced_args = innermost_deduced_args;
13165 if (unify (tparms, deduced_args,
13166 INNERMOST_TEMPLATE_ARGS (spec_args),
13167 INNERMOST_TEMPLATE_ARGS (args),
13168 UNIFY_ALLOW_NONE))
13169 return NULL_TREE;
13171 for (i = 0; i < ntparms; ++i)
13172 if (! TREE_VEC_ELT (innermost_deduced_args, i))
13173 return NULL_TREE;
13175 /* Verify that nondeduced template arguments agree with the type
13176 obtained from argument deduction.
13178 For example:
13180 struct A { typedef int X; };
13181 template <class T, class U> struct C {};
13182 template <class T> struct C<T, typename T::X> {};
13184 Then with the instantiation `C<A, int>', we can deduce that
13185 `T' is `A' but unify () does not check whether `typename T::X'
13186 is `int'. */
13187 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
13188 if (spec_args == error_mark_node
13189 /* We only need to check the innermost arguments; the other
13190 arguments will always agree. */
13191 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
13192 INNERMOST_TEMPLATE_ARGS (args)))
13193 return NULL_TREE;
13195 return deduced_args;
13198 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13199 Return the TREE_LIST node with the most specialized template, if
13200 any. If there is no most specialized template, the error_mark_node
13201 is returned.
13203 Note that this function does not look at, or modify, the
13204 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13205 returned is one of the elements of INSTANTIATIONS, callers may
13206 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13207 and retrieve it from the value returned. */
13209 tree
13210 most_specialized_instantiation (tree templates)
13212 tree fn, champ;
13214 ++processing_template_decl;
13216 champ = templates;
13217 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
13219 int fate = 0;
13221 if (get_bindings (TREE_VALUE (champ),
13222 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13223 NULL_TREE, /*check_ret=*/false))
13224 fate--;
13226 if (get_bindings (TREE_VALUE (fn),
13227 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13228 NULL_TREE, /*check_ret=*/false))
13229 fate++;
13231 if (fate == -1)
13232 champ = fn;
13233 else if (!fate)
13235 /* Equally specialized, move to next function. If there
13236 is no next function, nothing's most specialized. */
13237 fn = TREE_CHAIN (fn);
13238 champ = fn;
13239 if (!fn)
13240 break;
13244 if (champ)
13245 /* Now verify that champ is better than everything earlier in the
13246 instantiation list. */
13247 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
13248 if (get_bindings (TREE_VALUE (champ),
13249 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13250 NULL_TREE, /*check_ret=*/false)
13251 || !get_bindings (TREE_VALUE (fn),
13252 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13253 NULL_TREE, /*check_ret=*/false))
13255 champ = NULL_TREE;
13256 break;
13259 processing_template_decl--;
13261 if (!champ)
13262 return error_mark_node;
13264 return champ;
13267 /* If DECL is a specialization of some template, return the most
13268 general such template. Otherwise, returns NULL_TREE.
13270 For example, given:
13272 template <class T> struct S { template <class U> void f(U); };
13274 if TMPL is `template <class U> void S<int>::f(U)' this will return
13275 the full template. This function will not trace past partial
13276 specializations, however. For example, given in addition:
13278 template <class T> struct S<T*> { template <class U> void f(U); };
13280 if TMPL is `template <class U> void S<int*>::f(U)' this will return
13281 `template <class T> template <class U> S<T*>::f(U)'. */
13283 tree
13284 most_general_template (tree decl)
13286 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
13287 an immediate specialization. */
13288 if (TREE_CODE (decl) == FUNCTION_DECL)
13290 if (DECL_TEMPLATE_INFO (decl)) {
13291 decl = DECL_TI_TEMPLATE (decl);
13293 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
13294 template friend. */
13295 if (TREE_CODE (decl) != TEMPLATE_DECL)
13296 return NULL_TREE;
13297 } else
13298 return NULL_TREE;
13301 /* Look for more and more general templates. */
13302 while (DECL_TEMPLATE_INFO (decl))
13304 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
13305 (See cp-tree.h for details.) */
13306 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
13307 break;
13309 if (CLASS_TYPE_P (TREE_TYPE (decl))
13310 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
13311 break;
13313 /* Stop if we run into an explicitly specialized class template. */
13314 if (!DECL_NAMESPACE_SCOPE_P (decl)
13315 && DECL_CONTEXT (decl)
13316 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
13317 break;
13319 decl = DECL_TI_TEMPLATE (decl);
13322 return decl;
13325 /* Return the most specialized of the class template partial
13326 specializations of TMPL which can produce TYPE, a specialization of
13327 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
13328 a _TYPE node corresponding to the partial specialization, while the
13329 TREE_PURPOSE is the set of template arguments that must be
13330 substituted into the TREE_TYPE in order to generate TYPE.
13332 If the choice of partial specialization is ambiguous, a diagnostic
13333 is issued, and the error_mark_node is returned. If there are no
13334 partial specializations of TMPL matching TYPE, then NULL_TREE is
13335 returned. */
13337 static tree
13338 most_specialized_class (tree type, tree tmpl)
13340 tree list = NULL_TREE;
13341 tree t;
13342 tree champ;
13343 int fate;
13344 bool ambiguous_p;
13345 tree args;
13347 tmpl = most_general_template (tmpl);
13348 args = CLASSTYPE_TI_ARGS (type);
13349 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
13351 tree partial_spec_args;
13352 tree spec_args;
13354 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
13355 spec_args = get_class_bindings (TREE_VALUE (t),
13356 partial_spec_args,
13357 args);
13358 if (spec_args)
13360 list = tree_cons (spec_args, TREE_VALUE (t), list);
13361 TREE_TYPE (list) = TREE_TYPE (t);
13365 if (! list)
13366 return NULL_TREE;
13368 ambiguous_p = false;
13369 t = list;
13370 champ = t;
13371 t = TREE_CHAIN (t);
13372 for (; t; t = TREE_CHAIN (t))
13374 fate = more_specialized_class (champ, t);
13375 if (fate == 1)
13377 else
13379 if (fate == 0)
13381 t = TREE_CHAIN (t);
13382 if (! t)
13384 ambiguous_p = true;
13385 break;
13388 champ = t;
13392 if (!ambiguous_p)
13393 for (t = list; t && t != champ; t = TREE_CHAIN (t))
13395 fate = more_specialized_class (champ, t);
13396 if (fate != 1)
13398 ambiguous_p = true;
13399 break;
13403 if (ambiguous_p)
13405 const char *str = "candidates are:";
13406 error ("ambiguous class template instantiation for %q#T", type);
13407 for (t = list; t; t = TREE_CHAIN (t))
13409 error ("%s %+#T", str, TREE_TYPE (t));
13410 str = " ";
13412 return error_mark_node;
13415 return champ;
13418 /* Explicitly instantiate DECL. */
13420 void
13421 do_decl_instantiation (tree decl, tree storage)
13423 tree result = NULL_TREE;
13424 int extern_p = 0;
13426 if (!decl || decl == error_mark_node)
13427 /* An error occurred, for which grokdeclarator has already issued
13428 an appropriate message. */
13429 return;
13430 else if (! DECL_LANG_SPECIFIC (decl))
13432 error ("explicit instantiation of non-template %q#D", decl);
13433 return;
13435 else if (TREE_CODE (decl) == VAR_DECL)
13437 /* There is an asymmetry here in the way VAR_DECLs and
13438 FUNCTION_DECLs are handled by grokdeclarator. In the case of
13439 the latter, the DECL we get back will be marked as a
13440 template instantiation, and the appropriate
13441 DECL_TEMPLATE_INFO will be set up. This does not happen for
13442 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
13443 should handle VAR_DECLs as it currently handles
13444 FUNCTION_DECLs. */
13445 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
13446 if (!result || TREE_CODE (result) != VAR_DECL)
13448 error ("no matching template for %qD found", decl);
13449 return;
13451 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
13453 error ("type %qT for explicit instantiation %qD does not match "
13454 "declared type %qT", TREE_TYPE (result), decl,
13455 TREE_TYPE (decl));
13456 return;
13459 else if (TREE_CODE (decl) != FUNCTION_DECL)
13461 error ("explicit instantiation of %q#D", decl);
13462 return;
13464 else
13465 result = decl;
13467 /* Check for various error cases. Note that if the explicit
13468 instantiation is valid the RESULT will currently be marked as an
13469 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
13470 until we get here. */
13472 if (DECL_TEMPLATE_SPECIALIZATION (result))
13474 /* DR 259 [temp.spec].
13476 Both an explicit instantiation and a declaration of an explicit
13477 specialization shall not appear in a program unless the explicit
13478 instantiation follows a declaration of the explicit specialization.
13480 For a given set of template parameters, if an explicit
13481 instantiation of a template appears after a declaration of an
13482 explicit specialization for that template, the explicit
13483 instantiation has no effect. */
13484 return;
13486 else if (DECL_EXPLICIT_INSTANTIATION (result))
13488 /* [temp.spec]
13490 No program shall explicitly instantiate any template more
13491 than once.
13493 We check DECL_NOT_REALLY_EXTERN so as not to complain when
13494 the first instantiation was `extern' and the second is not,
13495 and EXTERN_P for the opposite case. */
13496 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
13497 pedwarn ("duplicate explicit instantiation of %q#D", result);
13498 /* If an "extern" explicit instantiation follows an ordinary
13499 explicit instantiation, the template is instantiated. */
13500 if (extern_p)
13501 return;
13503 else if (!DECL_IMPLICIT_INSTANTIATION (result))
13505 error ("no matching template for %qD found", result);
13506 return;
13508 else if (!DECL_TEMPLATE_INFO (result))
13510 pedwarn ("explicit instantiation of non-template %q#D", result);
13511 return;
13514 if (storage == NULL_TREE)
13516 else if (storage == ridpointers[(int) RID_EXTERN])
13518 if (pedantic && !in_system_header)
13519 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
13520 "instantiations");
13521 extern_p = 1;
13523 else
13524 error ("storage class %qD applied to template instantiation", storage);
13526 check_explicit_instantiation_namespace (result);
13527 mark_decl_instantiated (result, extern_p);
13528 if (! extern_p)
13529 instantiate_decl (result, /*defer_ok=*/1,
13530 /*expl_inst_class_mem_p=*/false);
13533 static void
13534 mark_class_instantiated (tree t, int extern_p)
13536 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
13537 SET_CLASSTYPE_INTERFACE_KNOWN (t);
13538 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
13539 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
13540 if (! extern_p)
13542 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
13543 rest_of_type_compilation (t, 1);
13547 /* Called from do_type_instantiation through binding_table_foreach to
13548 do recursive instantiation for the type bound in ENTRY. */
13549 static void
13550 bt_instantiate_type_proc (binding_entry entry, void *data)
13552 tree storage = *(tree *) data;
13554 if (IS_AGGR_TYPE (entry->type)
13555 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
13556 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
13559 /* Called from do_type_instantiation to instantiate a member
13560 (a member function or a static member variable) of an
13561 explicitly instantiated class template. */
13562 static void
13563 instantiate_class_member (tree decl, int extern_p)
13565 mark_decl_instantiated (decl, extern_p);
13566 if (! extern_p)
13567 instantiate_decl (decl, /*defer_ok=*/1,
13568 /*expl_inst_class_mem_p=*/true);
13571 /* Perform an explicit instantiation of template class T. STORAGE, if
13572 non-null, is the RID for extern, inline or static. COMPLAIN is
13573 nonzero if this is called from the parser, zero if called recursively,
13574 since the standard is unclear (as detailed below). */
13576 void
13577 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
13579 int extern_p = 0;
13580 int nomem_p = 0;
13581 int static_p = 0;
13582 int previous_instantiation_extern_p = 0;
13584 if (TREE_CODE (t) == TYPE_DECL)
13585 t = TREE_TYPE (t);
13587 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
13589 error ("explicit instantiation of non-template type %qT", t);
13590 return;
13593 complete_type (t);
13595 if (!COMPLETE_TYPE_P (t))
13597 if (complain & tf_error)
13598 error ("explicit instantiation of %q#T before definition of template",
13600 return;
13603 if (storage != NULL_TREE)
13605 if (pedantic && !in_system_header)
13606 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
13607 storage);
13609 if (storage == ridpointers[(int) RID_INLINE])
13610 nomem_p = 1;
13611 else if (storage == ridpointers[(int) RID_EXTERN])
13612 extern_p = 1;
13613 else if (storage == ridpointers[(int) RID_STATIC])
13614 static_p = 1;
13615 else
13617 error ("storage class %qD applied to template instantiation",
13618 storage);
13619 extern_p = 0;
13623 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
13625 /* DR 259 [temp.spec].
13627 Both an explicit instantiation and a declaration of an explicit
13628 specialization shall not appear in a program unless the explicit
13629 instantiation follows a declaration of the explicit specialization.
13631 For a given set of template parameters, if an explicit
13632 instantiation of a template appears after a declaration of an
13633 explicit specialization for that template, the explicit
13634 instantiation has no effect. */
13635 return;
13637 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
13639 /* [temp.spec]
13641 No program shall explicitly instantiate any template more
13642 than once.
13644 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
13645 instantiation was `extern'. If EXTERN_P then the second is.
13646 These cases are OK. */
13647 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
13649 if (!previous_instantiation_extern_p && !extern_p
13650 && (complain & tf_error))
13651 pedwarn ("duplicate explicit instantiation of %q#T", t);
13653 /* If we've already instantiated the template, just return now. */
13654 if (!CLASSTYPE_INTERFACE_ONLY (t))
13655 return;
13658 check_explicit_instantiation_namespace (TYPE_NAME (t));
13659 mark_class_instantiated (t, extern_p);
13661 if (nomem_p)
13662 return;
13665 tree tmp;
13667 /* In contrast to implicit instantiation, where only the
13668 declarations, and not the definitions, of members are
13669 instantiated, we have here:
13671 [temp.explicit]
13673 The explicit instantiation of a class template specialization
13674 implies the instantiation of all of its members not
13675 previously explicitly specialized in the translation unit
13676 containing the explicit instantiation.
13678 Of course, we can't instantiate member template classes, since
13679 we don't have any arguments for them. Note that the standard
13680 is unclear on whether the instantiation of the members are
13681 *explicit* instantiations or not. However, the most natural
13682 interpretation is that it should be an explicit instantiation. */
13684 if (! static_p)
13685 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
13686 if (TREE_CODE (tmp) == FUNCTION_DECL
13687 && DECL_TEMPLATE_INSTANTIATION (tmp))
13688 instantiate_class_member (tmp, extern_p);
13690 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
13691 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
13692 instantiate_class_member (tmp, extern_p);
13694 if (CLASSTYPE_NESTED_UTDS (t))
13695 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
13696 bt_instantiate_type_proc, &storage);
13700 /* Given a function DECL, which is a specialization of TMPL, modify
13701 DECL to be a re-instantiation of TMPL with the same template
13702 arguments. TMPL should be the template into which tsubst'ing
13703 should occur for DECL, not the most general template.
13705 One reason for doing this is a scenario like this:
13707 template <class T>
13708 void f(const T&, int i);
13710 void g() { f(3, 7); }
13712 template <class T>
13713 void f(const T& t, const int i) { }
13715 Note that when the template is first instantiated, with
13716 instantiate_template, the resulting DECL will have no name for the
13717 first parameter, and the wrong type for the second. So, when we go
13718 to instantiate the DECL, we regenerate it. */
13720 static void
13721 regenerate_decl_from_template (tree decl, tree tmpl)
13723 /* The arguments used to instantiate DECL, from the most general
13724 template. */
13725 tree args;
13726 tree code_pattern;
13728 args = DECL_TI_ARGS (decl);
13729 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
13731 /* Make sure that we can see identifiers, and compute access
13732 correctly. */
13733 push_access_scope (decl);
13735 if (TREE_CODE (decl) == FUNCTION_DECL)
13737 tree decl_parm;
13738 tree pattern_parm;
13739 tree specs;
13740 int args_depth;
13741 int parms_depth;
13743 args_depth = TMPL_ARGS_DEPTH (args);
13744 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
13745 if (args_depth > parms_depth)
13746 args = get_innermost_template_args (args, parms_depth);
13748 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
13749 args, tf_error, NULL_TREE);
13750 if (specs)
13751 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
13752 specs);
13754 /* Merge parameter declarations. */
13755 decl_parm = skip_artificial_parms_for (decl,
13756 DECL_ARGUMENTS (decl));
13757 pattern_parm
13758 = skip_artificial_parms_for (code_pattern,
13759 DECL_ARGUMENTS (code_pattern));
13760 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
13762 tree parm_type;
13763 tree attributes;
13765 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
13766 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
13767 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
13768 NULL_TREE);
13769 parm_type = type_decays_to (parm_type);
13770 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
13771 TREE_TYPE (decl_parm) = parm_type;
13772 attributes = DECL_ATTRIBUTES (pattern_parm);
13773 if (DECL_ATTRIBUTES (decl_parm) != attributes)
13775 DECL_ATTRIBUTES (decl_parm) = attributes;
13776 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
13778 decl_parm = TREE_CHAIN (decl_parm);
13779 pattern_parm = TREE_CHAIN (pattern_parm);
13781 /* Merge any parameters that match with the function parameter
13782 pack. */
13783 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
13785 int i, len;
13786 tree expanded_types;
13787 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13788 the parameters in this function parameter pack. */
13789 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
13790 args, tf_error, NULL_TREE);
13791 len = TREE_VEC_LENGTH (expanded_types);
13792 for (i = 0; i < len; i++)
13794 tree parm_type;
13795 tree attributes;
13797 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
13798 /* Rename the parameter to include the index. */
13799 DECL_NAME (decl_parm) =
13800 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
13801 parm_type = TREE_VEC_ELT (expanded_types, i);
13802 parm_type = type_decays_to (parm_type);
13803 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
13804 TREE_TYPE (decl_parm) = parm_type;
13805 attributes = DECL_ATTRIBUTES (pattern_parm);
13806 if (DECL_ATTRIBUTES (decl_parm) != attributes)
13808 DECL_ATTRIBUTES (decl_parm) = attributes;
13809 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
13811 decl_parm = TREE_CHAIN (decl_parm);
13814 /* Merge additional specifiers from the CODE_PATTERN. */
13815 if (DECL_DECLARED_INLINE_P (code_pattern)
13816 && !DECL_DECLARED_INLINE_P (decl))
13817 DECL_DECLARED_INLINE_P (decl) = 1;
13818 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
13819 DECL_INLINE (decl) = 1;
13821 else if (TREE_CODE (decl) == VAR_DECL)
13822 DECL_INITIAL (decl) =
13823 tsubst_expr (DECL_INITIAL (code_pattern), args,
13824 tf_error, DECL_TI_TEMPLATE (decl),
13825 /*integral_constant_expression_p=*/false);
13826 else
13827 gcc_unreachable ();
13829 pop_access_scope (decl);
13832 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
13833 substituted to get DECL. */
13835 tree
13836 template_for_substitution (tree decl)
13838 tree tmpl = DECL_TI_TEMPLATE (decl);
13840 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
13841 for the instantiation. This is not always the most general
13842 template. Consider, for example:
13844 template <class T>
13845 struct S { template <class U> void f();
13846 template <> void f<int>(); };
13848 and an instantiation of S<double>::f<int>. We want TD to be the
13849 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
13850 while (/* An instantiation cannot have a definition, so we need a
13851 more general template. */
13852 DECL_TEMPLATE_INSTANTIATION (tmpl)
13853 /* We must also deal with friend templates. Given:
13855 template <class T> struct S {
13856 template <class U> friend void f() {};
13859 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
13860 so far as the language is concerned, but that's still
13861 where we get the pattern for the instantiation from. On
13862 other hand, if the definition comes outside the class, say:
13864 template <class T> struct S {
13865 template <class U> friend void f();
13867 template <class U> friend void f() {}
13869 we don't need to look any further. That's what the check for
13870 DECL_INITIAL is for. */
13871 || (TREE_CODE (decl) == FUNCTION_DECL
13872 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
13873 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
13875 /* The present template, TD, should not be a definition. If it
13876 were a definition, we should be using it! Note that we
13877 cannot restructure the loop to just keep going until we find
13878 a template with a definition, since that might go too far if
13879 a specialization was declared, but not defined. */
13880 gcc_assert (TREE_CODE (decl) != VAR_DECL
13881 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
13883 /* Fetch the more general template. */
13884 tmpl = DECL_TI_TEMPLATE (tmpl);
13887 return tmpl;
13890 /* Produce the definition of D, a _DECL generated from a template. If
13891 DEFER_OK is nonzero, then we don't have to actually do the
13892 instantiation now; we just have to do it sometime. Normally it is
13893 an error if this is an explicit instantiation but D is undefined.
13894 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
13895 explicitly instantiated class template. */
13897 tree
13898 instantiate_decl (tree d, int defer_ok,
13899 bool expl_inst_class_mem_p)
13901 tree tmpl = DECL_TI_TEMPLATE (d);
13902 tree gen_args;
13903 tree args;
13904 tree td;
13905 tree code_pattern;
13906 tree spec;
13907 tree gen_tmpl;
13908 bool pattern_defined;
13909 int need_push;
13910 location_t saved_loc = input_location;
13911 int saved_in_system_header = in_system_header;
13912 bool external_p;
13914 /* This function should only be used to instantiate templates for
13915 functions and static member variables. */
13916 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
13917 || TREE_CODE (d) == VAR_DECL);
13919 /* Variables are never deferred; if instantiation is required, they
13920 are instantiated right away. That allows for better code in the
13921 case that an expression refers to the value of the variable --
13922 if the variable has a constant value the referring expression can
13923 take advantage of that fact. */
13924 if (TREE_CODE (d) == VAR_DECL)
13925 defer_ok = 0;
13927 /* Don't instantiate cloned functions. Instead, instantiate the
13928 functions they cloned. */
13929 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
13930 d = DECL_CLONED_FUNCTION (d);
13932 if (DECL_TEMPLATE_INSTANTIATED (d))
13933 /* D has already been instantiated. It might seem reasonable to
13934 check whether or not D is an explicit instantiation, and, if so,
13935 stop here. But when an explicit instantiation is deferred
13936 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
13937 is set, even though we still need to do the instantiation. */
13938 return d;
13940 /* If we already have a specialization of this declaration, then
13941 there's no reason to instantiate it. Note that
13942 retrieve_specialization gives us both instantiations and
13943 specializations, so we must explicitly check
13944 DECL_TEMPLATE_SPECIALIZATION. */
13945 gen_tmpl = most_general_template (tmpl);
13946 gen_args = DECL_TI_ARGS (d);
13947 spec = retrieve_specialization (gen_tmpl, gen_args,
13948 /*class_specializations_p=*/false);
13949 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
13950 return spec;
13952 /* This needs to happen before any tsubsting. */
13953 if (! push_tinst_level (d))
13954 return d;
13956 timevar_push (TV_PARSE);
13958 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
13959 for the instantiation. */
13960 td = template_for_substitution (d);
13961 code_pattern = DECL_TEMPLATE_RESULT (td);
13963 /* We should never be trying to instantiate a member of a class
13964 template or partial specialization. */
13965 gcc_assert (d != code_pattern);
13967 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
13968 || DECL_TEMPLATE_SPECIALIZATION (td))
13969 /* In the case of a friend template whose definition is provided
13970 outside the class, we may have too many arguments. Drop the
13971 ones we don't need. The same is true for specializations. */
13972 args = get_innermost_template_args
13973 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
13974 else
13975 args = gen_args;
13977 if (TREE_CODE (d) == FUNCTION_DECL)
13978 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
13979 else
13980 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
13982 /* We may be in the middle of deferred access check. Disable it now. */
13983 push_deferring_access_checks (dk_no_deferred);
13985 /* Unless an explicit instantiation directive has already determined
13986 the linkage of D, remember that a definition is available for
13987 this entity. */
13988 if (pattern_defined
13989 && !DECL_INTERFACE_KNOWN (d)
13990 && !DECL_NOT_REALLY_EXTERN (d))
13991 mark_definable (d);
13993 input_location = DECL_SOURCE_LOCATION (d);
13994 in_system_header = DECL_IN_SYSTEM_HEADER (d);
13996 /* If D is a member of an explicitly instantiated class template,
13997 and no definition is available, treat it like an implicit
13998 instantiation. */
13999 if (!pattern_defined && expl_inst_class_mem_p
14000 && DECL_EXPLICIT_INSTANTIATION (d))
14002 DECL_NOT_REALLY_EXTERN (d) = 0;
14003 DECL_INTERFACE_KNOWN (d) = 0;
14004 SET_DECL_IMPLICIT_INSTANTIATION (d);
14007 if (!defer_ok)
14009 /* Recheck the substitutions to obtain any warning messages
14010 about ignoring cv qualifiers. */
14011 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
14012 tree type = TREE_TYPE (gen);
14014 /* Make sure that we can see identifiers, and compute access
14015 correctly. D is already the target FUNCTION_DECL with the
14016 right context. */
14017 push_access_scope (d);
14019 if (TREE_CODE (gen) == FUNCTION_DECL)
14021 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
14022 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
14024 /* Don't simply tsubst the function type, as that will give
14025 duplicate warnings about poor parameter qualifications.
14026 The function arguments are the same as the decl_arguments
14027 without the top level cv qualifiers. */
14028 type = TREE_TYPE (type);
14030 tsubst (type, gen_args, tf_warning_or_error, d);
14032 pop_access_scope (d);
14035 /* Check to see whether we know that this template will be
14036 instantiated in some other file, as with "extern template"
14037 extension. */
14038 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
14039 /* In general, we do not instantiate such templates... */
14040 if (external_p
14041 /* ... but we instantiate inline functions so that we can inline
14042 them and ... */
14043 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
14044 /* ... we instantiate static data members whose values are
14045 needed in integral constant expressions. */
14046 && ! (TREE_CODE (d) == VAR_DECL
14047 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
14048 goto out;
14049 /* Defer all other templates, unless we have been explicitly
14050 forbidden from doing so. */
14051 if (/* If there is no definition, we cannot instantiate the
14052 template. */
14053 ! pattern_defined
14054 /* If it's OK to postpone instantiation, do so. */
14055 || defer_ok
14056 /* If this is a static data member that will be defined
14057 elsewhere, we don't want to instantiate the entire data
14058 member, but we do want to instantiate the initializer so that
14059 we can substitute that elsewhere. */
14060 || (external_p && TREE_CODE (d) == VAR_DECL))
14062 /* The definition of the static data member is now required so
14063 we must substitute the initializer. */
14064 if (TREE_CODE (d) == VAR_DECL
14065 && !DECL_INITIAL (d)
14066 && DECL_INITIAL (code_pattern))
14068 tree ns;
14069 tree init;
14071 ns = decl_namespace_context (d);
14072 push_nested_namespace (ns);
14073 push_nested_class (DECL_CONTEXT (d));
14074 init = tsubst_expr (DECL_INITIAL (code_pattern),
14075 args,
14076 tf_warning_or_error, NULL_TREE,
14077 /*integral_constant_expression_p=*/false);
14078 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
14079 /*asmspec_tree=*/NULL_TREE,
14080 LOOKUP_ONLYCONVERTING);
14081 pop_nested_class ();
14082 pop_nested_namespace (ns);
14085 /* We restore the source position here because it's used by
14086 add_pending_template. */
14087 input_location = saved_loc;
14089 if (at_eof && !pattern_defined
14090 && DECL_EXPLICIT_INSTANTIATION (d))
14091 /* [temp.explicit]
14093 The definition of a non-exported function template, a
14094 non-exported member function template, or a non-exported
14095 member function or static data member of a class template
14096 shall be present in every translation unit in which it is
14097 explicitly instantiated. */
14098 pedwarn
14099 ("explicit instantiation of %qD but no definition available", d);
14101 /* ??? Historically, we have instantiated inline functions, even
14102 when marked as "extern template". */
14103 if (!(external_p && TREE_CODE (d) == VAR_DECL))
14104 add_pending_template (d);
14105 goto out;
14107 /* Tell the repository that D is available in this translation unit
14108 -- and see if it is supposed to be instantiated here. */
14109 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
14111 /* In a PCH file, despite the fact that the repository hasn't
14112 requested instantiation in the PCH it is still possible that
14113 an instantiation will be required in a file that includes the
14114 PCH. */
14115 if (pch_file)
14116 add_pending_template (d);
14117 /* Instantiate inline functions so that the inliner can do its
14118 job, even though we'll not be emitting a copy of this
14119 function. */
14120 if (!(TREE_CODE (d) == FUNCTION_DECL
14121 && flag_inline_trees
14122 && DECL_DECLARED_INLINE_P (d)))
14123 goto out;
14126 need_push = !cfun || !global_bindings_p ();
14127 if (need_push)
14128 push_to_top_level ();
14130 /* Mark D as instantiated so that recursive calls to
14131 instantiate_decl do not try to instantiate it again. */
14132 DECL_TEMPLATE_INSTANTIATED (d) = 1;
14134 /* Regenerate the declaration in case the template has been modified
14135 by a subsequent redeclaration. */
14136 regenerate_decl_from_template (d, td);
14138 /* We already set the file and line above. Reset them now in case
14139 they changed as a result of calling regenerate_decl_from_template. */
14140 input_location = DECL_SOURCE_LOCATION (d);
14142 if (TREE_CODE (d) == VAR_DECL)
14144 tree init;
14146 /* Clear out DECL_RTL; whatever was there before may not be right
14147 since we've reset the type of the declaration. */
14148 SET_DECL_RTL (d, NULL_RTX);
14149 DECL_IN_AGGR_P (d) = 0;
14151 /* The initializer is placed in DECL_INITIAL by
14152 regenerate_decl_from_template. Pull it out so that
14153 finish_decl can process it. */
14154 init = DECL_INITIAL (d);
14155 DECL_INITIAL (d) = NULL_TREE;
14156 DECL_INITIALIZED_P (d) = 0;
14158 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14159 initializer. That function will defer actual emission until
14160 we have a chance to determine linkage. */
14161 DECL_EXTERNAL (d) = 0;
14163 /* Enter the scope of D so that access-checking works correctly. */
14164 push_nested_class (DECL_CONTEXT (d));
14165 finish_decl (d, init, NULL_TREE);
14166 pop_nested_class ();
14168 else if (TREE_CODE (d) == FUNCTION_DECL)
14170 htab_t saved_local_specializations;
14171 tree subst_decl;
14172 tree tmpl_parm;
14173 tree spec_parm;
14175 /* Save away the current list, in case we are instantiating one
14176 template from within the body of another. */
14177 saved_local_specializations = local_specializations;
14179 /* Set up the list of local specializations. */
14180 local_specializations = htab_create (37,
14181 hash_local_specialization,
14182 eq_local_specializations,
14183 NULL);
14185 /* Set up context. */
14186 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
14188 /* Create substitution entries for the parameters. */
14189 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
14190 tmpl_parm = DECL_ARGUMENTS (subst_decl);
14191 spec_parm = DECL_ARGUMENTS (d);
14192 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
14194 register_local_specialization (spec_parm, tmpl_parm);
14195 spec_parm = skip_artificial_parms_for (d, spec_parm);
14196 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
14198 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14200 register_local_specialization (spec_parm, tmpl_parm);
14201 tmpl_parm = TREE_CHAIN (tmpl_parm);
14202 spec_parm = TREE_CHAIN (spec_parm);
14204 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14206 /* Collect all of the extra "packed" parameters into an
14207 argument pack. */
14208 tree parmvec;
14209 tree parmtypevec;
14210 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
14211 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
14212 int i, len = 0;
14213 tree t;
14215 /* Count how many parameters remain. */
14216 for (t = spec_parm; t; t = TREE_CHAIN (t))
14217 len++;
14219 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
14220 parmvec = make_tree_vec (len);
14221 parmtypevec = make_tree_vec (len);
14222 for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
14224 TREE_VEC_ELT (parmvec, i) = spec_parm;
14225 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
14228 /* Build the argument packs. */
14229 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
14230 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
14231 TREE_TYPE (argpack) = argtypepack;
14233 /* Register the (value) argument pack as a specialization of
14234 TMPL_PARM, then move on. */
14235 register_local_specialization (argpack, tmpl_parm);
14236 tmpl_parm = TREE_CHAIN (tmpl_parm);
14238 gcc_assert (!spec_parm);
14240 /* Substitute into the body of the function. */
14241 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
14242 tf_warning_or_error, tmpl,
14243 /*integral_constant_expression_p=*/false);
14245 /* We don't need the local specializations any more. */
14246 htab_delete (local_specializations);
14247 local_specializations = saved_local_specializations;
14249 /* Finish the function. */
14250 d = finish_function (0);
14251 expand_or_defer_fn (d);
14254 /* We're not deferring instantiation any more. */
14255 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
14257 if (need_push)
14258 pop_from_top_level ();
14260 out:
14261 input_location = saved_loc;
14262 in_system_header = saved_in_system_header;
14263 pop_deferring_access_checks ();
14264 pop_tinst_level ();
14266 timevar_pop (TV_PARSE);
14268 return d;
14271 /* Run through the list of templates that we wish we could
14272 instantiate, and instantiate any we can. RETRIES is the
14273 number of times we retry pending template instantiation. */
14275 void
14276 instantiate_pending_templates (int retries)
14278 tree *t;
14279 tree last = NULL_TREE;
14280 int reconsider;
14281 location_t saved_loc = input_location;
14282 int saved_in_system_header = in_system_header;
14284 /* Instantiating templates may trigger vtable generation. This in turn
14285 may require further template instantiations. We place a limit here
14286 to avoid infinite loop. */
14287 if (pending_templates && retries >= max_tinst_depth)
14289 tree decl = TREE_VALUE (pending_templates);
14291 error ("template instantiation depth exceeds maximum of %d"
14292 " instantiating %q+D, possibly from virtual table generation"
14293 " (use -ftemplate-depth-NN to increase the maximum)",
14294 max_tinst_depth, decl);
14295 if (TREE_CODE (decl) == FUNCTION_DECL)
14296 /* Pretend that we defined it. */
14297 DECL_INITIAL (decl) = error_mark_node;
14298 return;
14303 reconsider = 0;
14305 t = &pending_templates;
14306 while (*t)
14308 tree instantiation = TREE_VALUE (*t);
14310 reopen_tinst_level (TREE_PURPOSE (*t));
14312 if (TYPE_P (instantiation))
14314 tree fn;
14316 if (!COMPLETE_TYPE_P (instantiation))
14318 instantiate_class_template (instantiation);
14319 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
14320 for (fn = TYPE_METHODS (instantiation);
14322 fn = TREE_CHAIN (fn))
14323 if (! DECL_ARTIFICIAL (fn))
14324 instantiate_decl (fn,
14325 /*defer_ok=*/0,
14326 /*expl_inst_class_mem_p=*/false);
14327 if (COMPLETE_TYPE_P (instantiation))
14328 reconsider = 1;
14331 if (COMPLETE_TYPE_P (instantiation))
14332 /* If INSTANTIATION has been instantiated, then we don't
14333 need to consider it again in the future. */
14334 *t = TREE_CHAIN (*t);
14335 else
14337 last = *t;
14338 t = &TREE_CHAIN (*t);
14341 else
14343 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
14344 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
14346 instantiation
14347 = instantiate_decl (instantiation,
14348 /*defer_ok=*/0,
14349 /*expl_inst_class_mem_p=*/false);
14350 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
14351 reconsider = 1;
14354 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
14355 || DECL_TEMPLATE_INSTANTIATED (instantiation))
14356 /* If INSTANTIATION has been instantiated, then we don't
14357 need to consider it again in the future. */
14358 *t = TREE_CHAIN (*t);
14359 else
14361 last = *t;
14362 t = &TREE_CHAIN (*t);
14365 tinst_depth = 0;
14366 current_tinst_level = NULL_TREE;
14368 last_pending_template = last;
14370 while (reconsider);
14372 input_location = saved_loc;
14373 in_system_header = saved_in_system_header;
14376 /* Substitute ARGVEC into T, which is a list of initializers for
14377 either base class or a non-static data member. The TREE_PURPOSEs
14378 are DECLs, and the TREE_VALUEs are the initializer values. Used by
14379 instantiate_decl. */
14381 static tree
14382 tsubst_initializer_list (tree t, tree argvec)
14384 tree inits = NULL_TREE;
14386 for (; t; t = TREE_CHAIN (t))
14388 tree decl;
14389 tree init;
14390 tree expanded_bases = NULL_TREE;
14391 tree expanded_arguments = NULL_TREE;
14392 int i, len = 1;
14394 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
14396 tree expr;
14397 tree arg;
14399 /* Expand the base class expansion type into separate base
14400 classes. */
14401 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
14402 tf_warning_or_error,
14403 NULL_TREE);
14404 if (expanded_bases == error_mark_node)
14405 continue;
14407 /* We'll be building separate TREE_LISTs of arguments for
14408 each base. */
14409 len = TREE_VEC_LENGTH (expanded_bases);
14410 expanded_arguments = make_tree_vec (len);
14411 for (i = 0; i < len; i++)
14412 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
14414 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
14415 expand each argument in the TREE_VALUE of t. */
14416 expr = make_node (EXPR_PACK_EXPANSION);
14417 PACK_EXPANSION_PARAMETER_PACKS (expr) =
14418 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
14420 /* Substitute parameter packs into each argument in the
14421 TREE_LIST. */
14422 in_base_initializer = 1;
14423 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
14425 tree expanded_exprs;
14427 /* Expand the argument. */
14428 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
14429 expanded_exprs = tsubst_pack_expansion (expr, argvec,
14430 tf_warning_or_error,
14431 NULL_TREE);
14433 /* Prepend each of the expanded expressions to the
14434 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
14435 for (i = 0; i < len; i++)
14437 TREE_VEC_ELT (expanded_arguments, i) =
14438 tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i),
14439 TREE_VEC_ELT (expanded_arguments, i));
14442 in_base_initializer = 0;
14444 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
14445 since we built them backwards. */
14446 for (i = 0; i < len; i++)
14448 TREE_VEC_ELT (expanded_arguments, i) =
14449 nreverse (TREE_VEC_ELT (expanded_arguments, i));
14453 for (i = 0; i < len; ++i)
14455 if (expanded_bases)
14457 decl = TREE_VEC_ELT (expanded_bases, i);
14458 decl = expand_member_init (decl);
14459 init = TREE_VEC_ELT (expanded_arguments, i);
14461 else
14463 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
14464 tf_warning_or_error, NULL_TREE);
14466 decl = expand_member_init (decl);
14467 if (decl && !DECL_P (decl))
14468 in_base_initializer = 1;
14470 init = tsubst_expr (TREE_VALUE (t), argvec,
14471 tf_warning_or_error, NULL_TREE,
14472 /*integral_constant_expression_p=*/false);
14473 in_base_initializer = 0;
14476 if (decl)
14478 init = build_tree_list (decl, init);
14479 TREE_CHAIN (init) = inits;
14480 inits = init;
14484 return inits;
14487 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
14489 static void
14490 set_current_access_from_decl (tree decl)
14492 if (TREE_PRIVATE (decl))
14493 current_access_specifier = access_private_node;
14494 else if (TREE_PROTECTED (decl))
14495 current_access_specifier = access_protected_node;
14496 else
14497 current_access_specifier = access_public_node;
14500 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
14501 is the instantiation (which should have been created with
14502 start_enum) and ARGS are the template arguments to use. */
14504 static void
14505 tsubst_enum (tree tag, tree newtag, tree args)
14507 tree e;
14509 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
14511 tree value;
14512 tree decl;
14514 decl = TREE_VALUE (e);
14515 /* Note that in a template enum, the TREE_VALUE is the
14516 CONST_DECL, not the corresponding INTEGER_CST. */
14517 value = tsubst_expr (DECL_INITIAL (decl),
14518 args, tf_warning_or_error, NULL_TREE,
14519 /*integral_constant_expression_p=*/true);
14521 /* Give this enumeration constant the correct access. */
14522 set_current_access_from_decl (decl);
14524 /* Actually build the enumerator itself. */
14525 build_enumerator (DECL_NAME (decl), value, newtag);
14528 finish_enum (newtag);
14529 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
14530 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
14533 /* DECL is a FUNCTION_DECL that is a template specialization. Return
14534 its type -- but without substituting the innermost set of template
14535 arguments. So, innermost set of template parameters will appear in
14536 the type. */
14538 tree
14539 get_mostly_instantiated_function_type (tree decl)
14541 tree fn_type;
14542 tree tmpl;
14543 tree targs;
14544 tree tparms;
14545 int parm_depth;
14547 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14548 targs = DECL_TI_ARGS (decl);
14549 tparms = DECL_TEMPLATE_PARMS (tmpl);
14550 parm_depth = TMPL_PARMS_DEPTH (tparms);
14552 /* There should be as many levels of arguments as there are levels
14553 of parameters. */
14554 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
14556 fn_type = TREE_TYPE (tmpl);
14558 if (parm_depth == 1)
14559 /* No substitution is necessary. */
14561 else
14563 int i, save_access_control;
14564 tree partial_args;
14566 /* Replace the innermost level of the TARGS with NULL_TREEs to
14567 let tsubst know not to substitute for those parameters. */
14568 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
14569 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
14570 SET_TMPL_ARGS_LEVEL (partial_args, i,
14571 TMPL_ARGS_LEVEL (targs, i));
14572 SET_TMPL_ARGS_LEVEL (partial_args,
14573 TMPL_ARGS_DEPTH (targs),
14574 make_tree_vec (DECL_NTPARMS (tmpl)));
14576 /* Disable access control as this function is used only during
14577 name-mangling. */
14578 save_access_control = flag_access_control;
14579 flag_access_control = 0;
14581 ++processing_template_decl;
14582 /* Now, do the (partial) substitution to figure out the
14583 appropriate function type. */
14584 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
14585 --processing_template_decl;
14587 /* Substitute into the template parameters to obtain the real
14588 innermost set of parameters. This step is important if the
14589 innermost set of template parameters contains value
14590 parameters whose types depend on outer template parameters. */
14591 TREE_VEC_LENGTH (partial_args)--;
14592 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
14594 flag_access_control = save_access_control;
14597 return fn_type;
14600 /* Return truthvalue if we're processing a template different from
14601 the last one involved in diagnostics. */
14603 problematic_instantiation_changed (void)
14605 return last_template_error_tick != tinst_level_tick;
14608 /* Remember current template involved in diagnostics. */
14609 void
14610 record_last_problematic_instantiation (void)
14612 last_template_error_tick = tinst_level_tick;
14615 tree
14616 current_instantiation (void)
14618 return current_tinst_level;
14621 /* [temp.param] Check that template non-type parm TYPE is of an allowable
14622 type. Return zero for ok, nonzero for disallowed. Issue error and
14623 warning messages under control of COMPLAIN. */
14625 static int
14626 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
14628 if (INTEGRAL_TYPE_P (type))
14629 return 0;
14630 else if (POINTER_TYPE_P (type))
14631 return 0;
14632 else if (TYPE_PTR_TO_MEMBER_P (type))
14633 return 0;
14634 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14635 return 0;
14636 else if (TREE_CODE (type) == TYPENAME_TYPE)
14637 return 0;
14639 if (complain & tf_error)
14640 error ("%q#T is not a valid type for a template constant parameter", type);
14641 return 1;
14644 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
14645 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
14647 static bool
14648 dependent_type_p_r (tree type)
14650 tree scope;
14652 /* [temp.dep.type]
14654 A type is dependent if it is:
14656 -- a template parameter. Template template parameters are types
14657 for us (since TYPE_P holds true for them) so we handle
14658 them here. */
14659 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
14660 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
14661 return true;
14662 /* -- a qualified-id with a nested-name-specifier which contains a
14663 class-name that names a dependent type or whose unqualified-id
14664 names a dependent type. */
14665 if (TREE_CODE (type) == TYPENAME_TYPE)
14666 return true;
14667 /* -- a cv-qualified type where the cv-unqualified type is
14668 dependent. */
14669 type = TYPE_MAIN_VARIANT (type);
14670 /* -- a compound type constructed from any dependent type. */
14671 if (TYPE_PTR_TO_MEMBER_P (type))
14672 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
14673 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
14674 (type)));
14675 else if (TREE_CODE (type) == POINTER_TYPE
14676 || TREE_CODE (type) == REFERENCE_TYPE)
14677 return dependent_type_p (TREE_TYPE (type));
14678 else if (TREE_CODE (type) == FUNCTION_TYPE
14679 || TREE_CODE (type) == METHOD_TYPE)
14681 tree arg_type;
14683 if (dependent_type_p (TREE_TYPE (type)))
14684 return true;
14685 for (arg_type = TYPE_ARG_TYPES (type);
14686 arg_type;
14687 arg_type = TREE_CHAIN (arg_type))
14688 if (dependent_type_p (TREE_VALUE (arg_type)))
14689 return true;
14690 return false;
14692 /* -- an array type constructed from any dependent type or whose
14693 size is specified by a constant expression that is
14694 value-dependent. */
14695 if (TREE_CODE (type) == ARRAY_TYPE)
14697 if (TYPE_DOMAIN (type)
14698 && ((value_dependent_expression_p
14699 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14700 || (type_dependent_expression_p
14701 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
14702 return true;
14703 return dependent_type_p (TREE_TYPE (type));
14706 /* -- a template-id in which either the template name is a template
14707 parameter ... */
14708 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
14709 return true;
14710 /* ... or any of the template arguments is a dependent type or
14711 an expression that is type-dependent or value-dependent. */
14712 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
14713 && (any_dependent_template_arguments_p
14714 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
14715 return true;
14717 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
14718 expression is not type-dependent, then it should already been
14719 have resolved. */
14720 if (TREE_CODE (type) == TYPEOF_TYPE)
14721 return true;
14723 /* A template argument pack is dependent if any of its packed
14724 arguments are. */
14725 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
14727 tree args = ARGUMENT_PACK_ARGS (type);
14728 int i, len = TREE_VEC_LENGTH (args);
14729 for (i = 0; i < len; ++i)
14730 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
14731 return true;
14734 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
14735 be template parameters. */
14736 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
14737 return true;
14739 /* The standard does not specifically mention types that are local
14740 to template functions or local classes, but they should be
14741 considered dependent too. For example:
14743 template <int I> void f() {
14744 enum E { a = I };
14745 S<sizeof (E)> s;
14748 The size of `E' cannot be known until the value of `I' has been
14749 determined. Therefore, `E' must be considered dependent. */
14750 scope = TYPE_CONTEXT (type);
14751 if (scope && TYPE_P (scope))
14752 return dependent_type_p (scope);
14753 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14754 return type_dependent_expression_p (scope);
14756 /* Other types are non-dependent. */
14757 return false;
14760 /* Returns TRUE if TYPE is dependent, in the sense of
14761 [temp.dep.type]. */
14763 bool
14764 dependent_type_p (tree type)
14766 /* If there are no template parameters in scope, then there can't be
14767 any dependent types. */
14768 if (!processing_template_decl)
14770 /* If we are not processing a template, then nobody should be
14771 providing us with a dependent type. */
14772 gcc_assert (type);
14773 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
14774 return false;
14777 /* If the type is NULL, we have not computed a type for the entity
14778 in question; in that case, the type is dependent. */
14779 if (!type)
14780 return true;
14782 /* Erroneous types can be considered non-dependent. */
14783 if (type == error_mark_node)
14784 return false;
14786 /* If we have not already computed the appropriate value for TYPE,
14787 do so now. */
14788 if (!TYPE_DEPENDENT_P_VALID (type))
14790 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
14791 TYPE_DEPENDENT_P_VALID (type) = 1;
14794 return TYPE_DEPENDENT_P (type);
14797 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
14799 static bool
14800 dependent_scope_ref_p (tree expression, bool criterion (tree))
14802 tree scope;
14803 tree name;
14805 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
14807 if (!TYPE_P (TREE_OPERAND (expression, 0)))
14808 return true;
14810 scope = TREE_OPERAND (expression, 0);
14811 name = TREE_OPERAND (expression, 1);
14813 /* [temp.dep.expr]
14815 An id-expression is type-dependent if it contains a
14816 nested-name-specifier that contains a class-name that names a
14817 dependent type. */
14818 /* The suggested resolution to Core Issue 2 implies that if the
14819 qualifying type is the current class, then we must peek
14820 inside it. */
14821 if (DECL_P (name)
14822 && currently_open_class (scope)
14823 && !criterion (name))
14824 return false;
14825 if (dependent_type_p (scope))
14826 return true;
14828 return false;
14831 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
14832 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
14833 expression. */
14835 bool
14836 value_dependent_expression_p (tree expression)
14838 if (!processing_template_decl)
14839 return false;
14841 /* A name declared with a dependent type. */
14842 if (DECL_P (expression) && type_dependent_expression_p (expression))
14843 return true;
14845 switch (TREE_CODE (expression))
14847 case IDENTIFIER_NODE:
14848 /* A name that has not been looked up -- must be dependent. */
14849 return true;
14851 case TEMPLATE_PARM_INDEX:
14852 /* A non-type template parm. */
14853 return true;
14855 case CONST_DECL:
14856 /* A non-type template parm. */
14857 if (DECL_TEMPLATE_PARM_P (expression))
14858 return true;
14859 return false;
14861 case VAR_DECL:
14862 /* A constant with integral or enumeration type and is initialized
14863 with an expression that is value-dependent. */
14864 if (DECL_INITIAL (expression)
14865 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
14866 && value_dependent_expression_p (DECL_INITIAL (expression)))
14867 return true;
14868 return false;
14870 case DYNAMIC_CAST_EXPR:
14871 case STATIC_CAST_EXPR:
14872 case CONST_CAST_EXPR:
14873 case REINTERPRET_CAST_EXPR:
14874 case CAST_EXPR:
14875 /* These expressions are value-dependent if the type to which
14876 the cast occurs is dependent or the expression being casted
14877 is value-dependent. */
14879 tree type = TREE_TYPE (expression);
14881 if (dependent_type_p (type))
14882 return true;
14884 /* A functional cast has a list of operands. */
14885 expression = TREE_OPERAND (expression, 0);
14886 if (!expression)
14888 /* If there are no operands, it must be an expression such
14889 as "int()". This should not happen for aggregate types
14890 because it would form non-constant expressions. */
14891 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
14893 return false;
14896 if (TREE_CODE (expression) == TREE_LIST)
14898 for (; expression; expression = TREE_CHAIN (expression))
14899 if (value_dependent_expression_p (TREE_VALUE (expression)))
14900 return true;
14901 return false;
14904 return value_dependent_expression_p (expression);
14907 case SIZEOF_EXPR:
14908 case ALIGNOF_EXPR:
14909 /* A `sizeof' expression is value-dependent if the operand is
14910 type-dependent or is a pack expansion. */
14911 expression = TREE_OPERAND (expression, 0);
14912 if (PACK_EXPANSION_P (expression))
14913 return true;
14914 else if (TYPE_P (expression))
14915 return dependent_type_p (expression);
14916 return type_dependent_expression_p (expression);
14918 case SCOPE_REF:
14919 return dependent_scope_ref_p (expression, value_dependent_expression_p);
14921 case COMPONENT_REF:
14922 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
14923 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
14925 case CALL_EXPR:
14926 /* A CALL_EXPR may appear in a constant expression if it is a
14927 call to a builtin function, e.g., __builtin_constant_p. All
14928 such calls are value-dependent. */
14929 return true;
14931 case NONTYPE_ARGUMENT_PACK:
14932 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
14933 is value-dependent. */
14935 tree values = ARGUMENT_PACK_ARGS (expression);
14936 int i, len = TREE_VEC_LENGTH (values);
14938 for (i = 0; i < len; ++i)
14939 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
14940 return true;
14942 return false;
14945 case TRAIT_EXPR:
14947 tree type2 = TRAIT_EXPR_TYPE2 (expression);
14948 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
14949 || (type2 ? dependent_type_p (type2) : false));
14952 default:
14953 /* A constant expression is value-dependent if any subexpression is
14954 value-dependent. */
14955 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
14957 case tcc_reference:
14958 case tcc_unary:
14959 return (value_dependent_expression_p
14960 (TREE_OPERAND (expression, 0)));
14962 case tcc_comparison:
14963 case tcc_binary:
14964 return ((value_dependent_expression_p
14965 (TREE_OPERAND (expression, 0)))
14966 || (value_dependent_expression_p
14967 (TREE_OPERAND (expression, 1))));
14969 case tcc_expression:
14970 case tcc_vl_exp:
14972 int i;
14973 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
14974 /* In some cases, some of the operands may be missing.
14975 (For example, in the case of PREDECREMENT_EXPR, the
14976 amount to increment by may be missing.) That doesn't
14977 make the expression dependent. */
14978 if (TREE_OPERAND (expression, i)
14979 && (value_dependent_expression_p
14980 (TREE_OPERAND (expression, i))))
14981 return true;
14982 return false;
14985 default:
14986 break;
14990 /* The expression is not value-dependent. */
14991 return false;
14994 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
14995 [temp.dep.expr]. */
14997 bool
14998 type_dependent_expression_p (tree expression)
15000 if (!processing_template_decl)
15001 return false;
15003 if (expression == error_mark_node)
15004 return false;
15006 /* An unresolved name is always dependent. */
15007 if (TREE_CODE (expression) == IDENTIFIER_NODE
15008 || TREE_CODE (expression) == USING_DECL)
15009 return true;
15011 /* Some expression forms are never type-dependent. */
15012 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
15013 || TREE_CODE (expression) == SIZEOF_EXPR
15014 || TREE_CODE (expression) == ALIGNOF_EXPR
15015 || TREE_CODE (expression) == TRAIT_EXPR
15016 || TREE_CODE (expression) == TYPEID_EXPR
15017 || TREE_CODE (expression) == DELETE_EXPR
15018 || TREE_CODE (expression) == VEC_DELETE_EXPR
15019 || TREE_CODE (expression) == THROW_EXPR)
15020 return false;
15022 /* The types of these expressions depends only on the type to which
15023 the cast occurs. */
15024 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
15025 || TREE_CODE (expression) == STATIC_CAST_EXPR
15026 || TREE_CODE (expression) == CONST_CAST_EXPR
15027 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
15028 || TREE_CODE (expression) == CAST_EXPR)
15029 return dependent_type_p (TREE_TYPE (expression));
15031 /* The types of these expressions depends only on the type created
15032 by the expression. */
15033 if (TREE_CODE (expression) == NEW_EXPR
15034 || TREE_CODE (expression) == VEC_NEW_EXPR)
15036 /* For NEW_EXPR tree nodes created inside a template, either
15037 the object type itself or a TREE_LIST may appear as the
15038 operand 1. */
15039 tree type = TREE_OPERAND (expression, 1);
15040 if (TREE_CODE (type) == TREE_LIST)
15041 /* This is an array type. We need to check array dimensions
15042 as well. */
15043 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
15044 || value_dependent_expression_p
15045 (TREE_OPERAND (TREE_VALUE (type), 1));
15046 else
15047 return dependent_type_p (type);
15050 if (TREE_CODE (expression) == SCOPE_REF
15051 && dependent_scope_ref_p (expression,
15052 type_dependent_expression_p))
15053 return true;
15055 if (TREE_CODE (expression) == FUNCTION_DECL
15056 && DECL_LANG_SPECIFIC (expression)
15057 && DECL_TEMPLATE_INFO (expression)
15058 && (any_dependent_template_arguments_p
15059 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
15060 return true;
15062 if (TREE_CODE (expression) == TEMPLATE_DECL
15063 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
15064 return false;
15066 if (TREE_TYPE (expression) == unknown_type_node)
15068 if (TREE_CODE (expression) == ADDR_EXPR)
15069 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
15070 if (TREE_CODE (expression) == COMPONENT_REF
15071 || TREE_CODE (expression) == OFFSET_REF)
15073 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
15074 return true;
15075 expression = TREE_OPERAND (expression, 1);
15076 if (TREE_CODE (expression) == IDENTIFIER_NODE)
15077 return false;
15079 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15080 if (TREE_CODE (expression) == SCOPE_REF)
15081 return false;
15083 if (TREE_CODE (expression) == BASELINK)
15084 expression = BASELINK_FUNCTIONS (expression);
15086 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
15088 if (any_dependent_template_arguments_p
15089 (TREE_OPERAND (expression, 1)))
15090 return true;
15091 expression = TREE_OPERAND (expression, 0);
15093 gcc_assert (TREE_CODE (expression) == OVERLOAD
15094 || TREE_CODE (expression) == FUNCTION_DECL);
15096 while (expression)
15098 if (type_dependent_expression_p (OVL_CURRENT (expression)))
15099 return true;
15100 expression = OVL_NEXT (expression);
15102 return false;
15105 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
15107 return (dependent_type_p (TREE_TYPE (expression)));
15110 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15111 contains a type-dependent expression. */
15113 bool
15114 any_type_dependent_arguments_p (tree args)
15116 while (args)
15118 tree arg = TREE_VALUE (args);
15120 if (type_dependent_expression_p (arg))
15121 return true;
15122 args = TREE_CHAIN (args);
15124 return false;
15127 /* Returns TRUE if the ARG (a template argument) is dependent. */
15129 bool
15130 dependent_template_arg_p (tree arg)
15132 if (!processing_template_decl)
15133 return false;
15135 if (TREE_CODE (arg) == TEMPLATE_DECL
15136 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15137 return dependent_template_p (arg);
15138 else if (ARGUMENT_PACK_P (arg))
15140 tree args = ARGUMENT_PACK_ARGS (arg);
15141 int i, len = TREE_VEC_LENGTH (args);
15142 for (i = 0; i < len; ++i)
15144 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15145 return true;
15148 return false;
15150 else if (TYPE_P (arg))
15151 return dependent_type_p (arg);
15152 else
15153 return (type_dependent_expression_p (arg)
15154 || value_dependent_expression_p (arg));
15157 /* Returns true if ARGS (a collection of template arguments) contains
15158 any types that require structural equality testing. */
15160 bool
15161 any_template_arguments_need_structural_equality_p (tree args)
15163 int i;
15164 int j;
15166 if (!args)
15167 return false;
15168 if (args == error_mark_node)
15169 return true;
15171 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15173 tree level = TMPL_ARGS_LEVEL (args, i + 1);
15174 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15176 tree arg = TREE_VEC_ELT (level, j);
15177 tree packed_args = NULL_TREE;
15178 int k, len = 1;
15180 if (ARGUMENT_PACK_P (arg))
15182 /* Look inside the argument pack. */
15183 packed_args = ARGUMENT_PACK_ARGS (arg);
15184 len = TREE_VEC_LENGTH (packed_args);
15187 for (k = 0; k < len; ++k)
15189 if (packed_args)
15190 arg = TREE_VEC_ELT (packed_args, k);
15192 if (error_operand_p (arg))
15193 return true;
15194 else if (TREE_CODE (arg) == TEMPLATE_DECL
15195 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15196 continue;
15197 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
15198 return true;
15199 else if (!TYPE_P (arg) && TREE_TYPE (arg)
15200 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
15201 return true;
15206 return false;
15209 /* Returns true if ARGS (a collection of template arguments) contains
15210 any dependent arguments. */
15212 bool
15213 any_dependent_template_arguments_p (tree args)
15215 int i;
15216 int j;
15218 if (!args)
15219 return false;
15220 if (args == error_mark_node)
15221 return true;
15223 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15225 tree level = TMPL_ARGS_LEVEL (args, i + 1);
15226 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15227 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
15228 return true;
15231 return false;
15234 /* Returns TRUE if the template TMPL is dependent. */
15236 bool
15237 dependent_template_p (tree tmpl)
15239 if (TREE_CODE (tmpl) == OVERLOAD)
15241 while (tmpl)
15243 if (dependent_template_p (OVL_FUNCTION (tmpl)))
15244 return true;
15245 tmpl = OVL_CHAIN (tmpl);
15247 return false;
15250 /* Template template parameters are dependent. */
15251 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
15252 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15253 return true;
15254 /* So are names that have not been looked up. */
15255 if (TREE_CODE (tmpl) == SCOPE_REF
15256 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
15257 return true;
15258 /* So are member templates of dependent classes. */
15259 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
15260 return dependent_type_p (DECL_CONTEXT (tmpl));
15261 return false;
15264 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
15266 bool
15267 dependent_template_id_p (tree tmpl, tree args)
15269 return (dependent_template_p (tmpl)
15270 || any_dependent_template_arguments_p (args));
15273 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
15274 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
15275 can be found. Note that this function peers inside uninstantiated
15276 templates and therefore should be used only in extremely limited
15277 situations. ONLY_CURRENT_P restricts this peering to the currently
15278 open classes hierarchy (which is required when comparing types). */
15280 tree
15281 resolve_typename_type (tree type, bool only_current_p)
15283 tree scope;
15284 tree name;
15285 tree decl;
15286 int quals;
15287 tree pushed_scope;
15289 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
15291 scope = TYPE_CONTEXT (type);
15292 name = TYPE_IDENTIFIER (type);
15294 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
15295 it first before we can figure out what NAME refers to. */
15296 if (TREE_CODE (scope) == TYPENAME_TYPE)
15297 scope = resolve_typename_type (scope, only_current_p);
15298 /* If we don't know what SCOPE refers to, then we cannot resolve the
15299 TYPENAME_TYPE. */
15300 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
15301 return error_mark_node;
15302 /* If the SCOPE is a template type parameter, we have no way of
15303 resolving the name. */
15304 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
15305 return type;
15306 /* If the SCOPE is not the current instantiation, there's no reason
15307 to look inside it. */
15308 if (only_current_p && !currently_open_class (scope))
15309 return error_mark_node;
15310 /* If SCOPE is a partial instantiation, it will not have a valid
15311 TYPE_FIELDS list, so use the original template. */
15312 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
15313 /* Enter the SCOPE so that name lookup will be resolved as if we
15314 were in the class definition. In particular, SCOPE will no
15315 longer be considered a dependent type. */
15316 pushed_scope = push_scope (scope);
15317 /* Look up the declaration. */
15318 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
15319 /* Obtain the set of qualifiers applied to the TYPE. */
15320 quals = cp_type_quals (type);
15321 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
15322 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
15323 if (!decl)
15324 type = error_mark_node;
15325 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
15326 && TREE_CODE (decl) == TYPE_DECL)
15327 type = TREE_TYPE (decl);
15328 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
15329 && DECL_CLASS_TEMPLATE_P (decl))
15331 tree tmpl;
15332 tree args;
15333 /* Obtain the template and the arguments. */
15334 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
15335 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
15336 /* Instantiate the template. */
15337 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
15338 /*entering_scope=*/0, tf_error | tf_user);
15340 else
15341 type = error_mark_node;
15342 /* Qualify the resulting type. */
15343 if (type != error_mark_node && quals)
15344 type = cp_build_qualified_type (type, quals);
15345 /* Leave the SCOPE. */
15346 if (pushed_scope)
15347 pop_scope (pushed_scope);
15349 return type;
15352 /* EXPR is an expression which is not type-dependent. Return a proxy
15353 for EXPR that can be used to compute the types of larger
15354 expressions containing EXPR. */
15356 tree
15357 build_non_dependent_expr (tree expr)
15359 tree inner_expr;
15361 /* Preserve null pointer constants so that the type of things like
15362 "p == 0" where "p" is a pointer can be determined. */
15363 if (null_ptr_cst_p (expr))
15364 return expr;
15365 /* Preserve OVERLOADs; the functions must be available to resolve
15366 types. */
15367 inner_expr = expr;
15368 if (TREE_CODE (inner_expr) == ADDR_EXPR)
15369 inner_expr = TREE_OPERAND (inner_expr, 0);
15370 if (TREE_CODE (inner_expr) == COMPONENT_REF)
15371 inner_expr = TREE_OPERAND (inner_expr, 1);
15372 if (is_overloaded_fn (inner_expr)
15373 || TREE_CODE (inner_expr) == OFFSET_REF)
15374 return expr;
15375 /* There is no need to return a proxy for a variable. */
15376 if (TREE_CODE (expr) == VAR_DECL)
15377 return expr;
15378 /* Preserve string constants; conversions from string constants to
15379 "char *" are allowed, even though normally a "const char *"
15380 cannot be used to initialize a "char *". */
15381 if (TREE_CODE (expr) == STRING_CST)
15382 return expr;
15383 /* Preserve arithmetic constants, as an optimization -- there is no
15384 reason to create a new node. */
15385 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
15386 return expr;
15387 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
15388 There is at least one place where we want to know that a
15389 particular expression is a throw-expression: when checking a ?:
15390 expression, there are special rules if the second or third
15391 argument is a throw-expression. */
15392 if (TREE_CODE (expr) == THROW_EXPR)
15393 return expr;
15395 if (TREE_CODE (expr) == COND_EXPR)
15396 return build3 (COND_EXPR,
15397 TREE_TYPE (expr),
15398 TREE_OPERAND (expr, 0),
15399 (TREE_OPERAND (expr, 1)
15400 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
15401 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
15402 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
15403 if (TREE_CODE (expr) == COMPOUND_EXPR
15404 && !COMPOUND_EXPR_OVERLOADED (expr))
15405 return build2 (COMPOUND_EXPR,
15406 TREE_TYPE (expr),
15407 TREE_OPERAND (expr, 0),
15408 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
15410 /* If the type is unknown, it can't really be non-dependent */
15411 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
15413 /* Otherwise, build a NON_DEPENDENT_EXPR.
15415 REFERENCE_TYPEs are not stripped for expressions in templates
15416 because doing so would play havoc with mangling. Consider, for
15417 example:
15419 template <typename T> void f<T& g>() { g(); }
15421 In the body of "f", the expression for "g" will have
15422 REFERENCE_TYPE, even though the standard says that it should
15423 not. The reason is that we must preserve the syntactic form of
15424 the expression so that mangling (say) "f<g>" inside the body of
15425 "f" works out correctly. Therefore, the REFERENCE_TYPE is
15426 stripped here. */
15427 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
15430 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
15431 Return a new TREE_LIST with the various arguments replaced with
15432 equivalent non-dependent expressions. */
15434 tree
15435 build_non_dependent_args (tree args)
15437 tree a;
15438 tree new_args;
15440 new_args = NULL_TREE;
15441 for (a = args; a; a = TREE_CHAIN (a))
15442 new_args = tree_cons (NULL_TREE,
15443 build_non_dependent_expr (TREE_VALUE (a)),
15444 new_args);
15445 return nreverse (new_args);
15448 #include "gt-cp-pt.h"