/cp
[official-gcc.git] / gcc / cp / pt.c
blobe33197c5c45dd33256b2959a22f16a194b36adb9
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, 2007 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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "obstack.h"
33 #include "tree.h"
34 #include "pointer-set.h"
35 #include "flags.h"
36 #include "c-common.h"
37 #include "cp-tree.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "output.h"
42 #include "except.h"
43 #include "toplev.h"
44 #include "rtl.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "vecprim.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct pending_template GTY (()) {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
81 local variables. */
82 static htab_t local_specializations;
84 /* Contains canonical template parameter types. The vector is indexed by
85 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
86 TREE_LIST, whose TREE_VALUEs contain the canonical template
87 parameters of various types and levels. */
88 static GTY(()) VEC(tree,gc) *canonical_template_parms;
90 #define UNIFY_ALLOW_NONE 0
91 #define UNIFY_ALLOW_MORE_CV_QUAL 1
92 #define UNIFY_ALLOW_LESS_CV_QUAL 2
93 #define UNIFY_ALLOW_DERIVED 4
94 #define UNIFY_ALLOW_INTEGER 8
95 #define UNIFY_ALLOW_OUTER_LEVEL 16
96 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
97 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
99 static void push_access_scope (tree);
100 static void pop_access_scope (tree);
101 static bool resolve_overloaded_unification (tree, tree, tree, tree,
102 unification_kind_t, int);
103 static int try_one_overload (tree, tree, tree, tree, tree,
104 unification_kind_t, int, bool);
105 static int unify (tree, tree, tree, tree, int);
106 static void add_pending_template (tree);
107 static int push_tinst_level (tree);
108 static void pop_tinst_level (void);
109 static tree reopen_tinst_level (struct tinst_level *);
110 static tree tsubst_initializer_list (tree, tree);
111 static tree get_class_bindings (tree, tree, tree);
112 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
113 bool, bool);
114 static void tsubst_enum (tree, tree, tree);
115 static tree add_to_template_args (tree, tree);
116 static tree add_outermost_template_args (tree, tree);
117 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
118 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
119 tree);
120 static int type_unification_real (tree, tree, tree, tree,
121 int, unification_kind_t, int);
122 static void note_template_header (int);
123 static tree convert_nontype_argument_function (tree, tree);
124 static tree convert_nontype_argument (tree, tree);
125 static tree convert_template_argument (tree, tree, tree,
126 tsubst_flags_t, int, tree);
127 static int for_each_template_parm (tree, tree_fn_t, void*,
128 struct pointer_set_t*);
129 static tree expand_template_argument_pack (tree);
130 static tree build_template_parm_index (int, int, int, tree, tree);
131 static bool inline_needs_template_parms (tree);
132 static void push_inline_template_parms_recursive (tree, int);
133 static tree retrieve_local_specialization (tree);
134 static void register_local_specialization (tree, tree);
135 static tree reduce_template_parm_level (tree, tree, int);
136 static int mark_template_parm (tree, void *);
137 static int template_parm_this_level_p (tree, void *);
138 static tree tsubst_friend_function (tree, tree);
139 static tree tsubst_friend_class (tree, tree);
140 static int can_complete_type_without_circularity (tree);
141 static tree get_bindings (tree, tree, tree, bool);
142 static int template_decl_level (tree);
143 static int check_cv_quals_for_unify (int, tree, tree);
144 static void template_parm_level_and_index (tree, int*, int*);
145 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
146 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
147 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
148 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
149 static void regenerate_decl_from_template (tree, tree);
150 static tree most_specialized_class (tree, tree);
151 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
152 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
153 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
154 static bool check_specialization_scope (void);
155 static tree process_partial_specialization (tree);
156 static void set_current_access_from_decl (tree);
157 static tree get_template_base (tree, tree, tree, tree);
158 static tree try_class_unification (tree, tree, tree, tree);
159 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
160 tree, tree);
161 static int template_args_equal (tree, tree);
162 static void tsubst_default_arguments (tree);
163 static tree for_each_template_parm_r (tree *, int *, void *);
164 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
165 static void copy_default_args_to_explicit_spec (tree);
166 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
167 static int eq_local_specializations (const void *, const void *);
168 static bool dependent_template_arg_p (tree);
169 static bool any_template_arguments_need_structural_equality_p (tree);
170 static bool dependent_type_p_r (tree);
171 static tree tsubst (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
173 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
176 /* Make the current scope suitable for access checking when we are
177 processing T. T can be FUNCTION_DECL for instantiated function
178 template, or VAR_DECL for static member variable (need by
179 instantiate_decl). */
181 static void
182 push_access_scope (tree t)
184 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
185 || TREE_CODE (t) == VAR_DECL);
187 if (DECL_FRIEND_CONTEXT (t))
188 push_nested_class (DECL_FRIEND_CONTEXT (t));
189 else if (DECL_CLASS_SCOPE_P (t))
190 push_nested_class (DECL_CONTEXT (t));
191 else
192 push_to_top_level ();
194 if (TREE_CODE (t) == FUNCTION_DECL)
196 saved_access_scope = tree_cons
197 (NULL_TREE, current_function_decl, saved_access_scope);
198 current_function_decl = t;
202 /* Restore the scope set up by push_access_scope. T is the node we
203 are processing. */
205 static void
206 pop_access_scope (tree t)
208 if (TREE_CODE (t) == FUNCTION_DECL)
210 current_function_decl = TREE_VALUE (saved_access_scope);
211 saved_access_scope = TREE_CHAIN (saved_access_scope);
214 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
215 pop_nested_class ();
216 else
217 pop_from_top_level ();
220 /* Do any processing required when DECL (a member template
221 declaration) is finished. Returns the TEMPLATE_DECL corresponding
222 to DECL, unless it is a specialization, in which case the DECL
223 itself is returned. */
225 tree
226 finish_member_template_decl (tree decl)
228 if (decl == error_mark_node)
229 return error_mark_node;
231 gcc_assert (DECL_P (decl));
233 if (TREE_CODE (decl) == TYPE_DECL)
235 tree type;
237 type = TREE_TYPE (decl);
238 if (IS_AGGR_TYPE (type)
239 && CLASSTYPE_TEMPLATE_INFO (type)
240 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
242 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
243 check_member_template (tmpl);
244 return tmpl;
246 return NULL_TREE;
248 else if (TREE_CODE (decl) == FIELD_DECL)
249 error ("data member %qD cannot be a member template", decl);
250 else if (DECL_TEMPLATE_INFO (decl))
252 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
254 check_member_template (DECL_TI_TEMPLATE (decl));
255 return DECL_TI_TEMPLATE (decl);
257 else
258 return decl;
260 else
261 error ("invalid member template declaration %qD", decl);
263 return error_mark_node;
266 /* Returns the template nesting level of the indicated class TYPE.
268 For example, in:
269 template <class T>
270 struct A
272 template <class U>
273 struct B {};
276 A<T>::B<U> has depth two, while A<T> has depth one.
277 Both A<T>::B<int> and A<int>::B<U> have depth one, if
278 they are instantiations, not specializations.
280 This function is guaranteed to return 0 if passed NULL_TREE so
281 that, for example, `template_class_depth (current_class_type)' is
282 always safe. */
285 template_class_depth (tree type)
287 int depth;
289 for (depth = 0;
290 type && TREE_CODE (type) != NAMESPACE_DECL;
291 type = (TREE_CODE (type) == FUNCTION_DECL)
292 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
294 if (TREE_CODE (type) != FUNCTION_DECL)
296 if (CLASSTYPE_TEMPLATE_INFO (type)
297 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
298 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
299 ++depth;
301 else
303 if (DECL_TEMPLATE_INFO (type)
304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
305 && uses_template_parms (DECL_TI_ARGS (type)))
306 ++depth;
310 return depth;
313 /* Subroutine of maybe_begin_member_template_processing.
314 Returns true if processing DECL needs us to push template parms. */
316 static bool
317 inline_needs_template_parms (tree decl)
319 if (! DECL_TEMPLATE_INFO (decl))
320 return false;
322 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
323 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
326 /* Subroutine of maybe_begin_member_template_processing.
327 Push the template parms in PARMS, starting from LEVELS steps into the
328 chain, and ending at the beginning, since template parms are listed
329 innermost first. */
331 static void
332 push_inline_template_parms_recursive (tree parmlist, int levels)
334 tree parms = TREE_VALUE (parmlist);
335 int i;
337 if (levels > 1)
338 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
340 ++processing_template_decl;
341 current_template_parms
342 = tree_cons (size_int (processing_template_decl),
343 parms, current_template_parms);
344 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
346 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
347 NULL);
348 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
350 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
352 if (parm == error_mark_node)
353 continue;
355 gcc_assert (DECL_P (parm));
357 switch (TREE_CODE (parm))
359 case TYPE_DECL:
360 case TEMPLATE_DECL:
361 pushdecl (parm);
362 break;
364 case PARM_DECL:
366 /* Make a CONST_DECL as is done in process_template_parm.
367 It is ugly that we recreate this here; the original
368 version built in process_template_parm is no longer
369 available. */
370 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
371 TREE_TYPE (parm));
372 DECL_ARTIFICIAL (decl) = 1;
373 TREE_CONSTANT (decl) = 1;
374 TREE_INVARIANT (decl) = 1;
375 TREE_READONLY (decl) = 1;
376 DECL_INITIAL (decl) = DECL_INITIAL (parm);
377 SET_DECL_TEMPLATE_PARM_P (decl);
378 pushdecl (decl);
380 break;
382 default:
383 gcc_unreachable ();
388 /* Restore the template parameter context for a member template or
389 a friend template defined in a class definition. */
391 void
392 maybe_begin_member_template_processing (tree decl)
394 tree parms;
395 int levels = 0;
397 if (inline_needs_template_parms (decl))
399 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
400 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
402 if (DECL_TEMPLATE_SPECIALIZATION (decl))
404 --levels;
405 parms = TREE_CHAIN (parms);
408 push_inline_template_parms_recursive (parms, levels);
411 /* Remember how many levels of template parameters we pushed so that
412 we can pop them later. */
413 VEC_safe_push (int, heap, inline_parm_levels, levels);
416 /* Undo the effects of maybe_begin_member_template_processing. */
418 void
419 maybe_end_member_template_processing (void)
421 int i;
422 int last;
424 if (VEC_length (int, inline_parm_levels) == 0)
425 return;
427 last = VEC_pop (int, inline_parm_levels);
428 for (i = 0; i < last; ++i)
430 --processing_template_decl;
431 current_template_parms = TREE_CHAIN (current_template_parms);
432 poplevel (0, 0, 0);
436 /* Return a new template argument vector which contains all of ARGS,
437 but has as its innermost set of arguments the EXTRA_ARGS. */
439 static tree
440 add_to_template_args (tree args, tree extra_args)
442 tree new_args;
443 int extra_depth;
444 int i;
445 int j;
447 extra_depth = TMPL_ARGS_DEPTH (extra_args);
448 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
450 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
451 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
453 for (j = 1; j <= extra_depth; ++j, ++i)
454 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
456 return new_args;
459 /* Like add_to_template_args, but only the outermost ARGS are added to
460 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
461 (EXTRA_ARGS) levels are added. This function is used to combine
462 the template arguments from a partial instantiation with the
463 template arguments used to attain the full instantiation from the
464 partial instantiation. */
466 static tree
467 add_outermost_template_args (tree args, tree extra_args)
469 tree new_args;
471 /* If there are more levels of EXTRA_ARGS than there are ARGS,
472 something very fishy is going on. */
473 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
475 /* If *all* the new arguments will be the EXTRA_ARGS, just return
476 them. */
477 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
478 return extra_args;
480 /* For the moment, we make ARGS look like it contains fewer levels. */
481 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
483 new_args = add_to_template_args (args, extra_args);
485 /* Now, we restore ARGS to its full dimensions. */
486 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
488 return new_args;
491 /* Return the N levels of innermost template arguments from the ARGS. */
493 tree
494 get_innermost_template_args (tree args, int n)
496 tree new_args;
497 int extra_levels;
498 int i;
500 gcc_assert (n >= 0);
502 /* If N is 1, just return the innermost set of template arguments. */
503 if (n == 1)
504 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
506 /* If we're not removing anything, just return the arguments we were
507 given. */
508 extra_levels = TMPL_ARGS_DEPTH (args) - n;
509 gcc_assert (extra_levels >= 0);
510 if (extra_levels == 0)
511 return args;
513 /* Make a new set of arguments, not containing the outer arguments. */
514 new_args = make_tree_vec (n);
515 for (i = 1; i <= n; ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i,
517 TMPL_ARGS_LEVEL (args, i + extra_levels));
519 return new_args;
522 /* We've got a template header coming up; push to a new level for storing
523 the parms. */
525 void
526 begin_template_parm_list (void)
528 /* We use a non-tag-transparent scope here, which causes pushtag to
529 put tags in this scope, rather than in the enclosing class or
530 namespace scope. This is the right thing, since we want
531 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
532 global template class, push_template_decl handles putting the
533 TEMPLATE_DECL into top-level scope. For a nested template class,
534 e.g.:
536 template <class T> struct S1 {
537 template <class T> struct S2 {};
540 pushtag contains special code to call pushdecl_with_scope on the
541 TEMPLATE_DECL for S2. */
542 begin_scope (sk_template_parms, NULL);
543 ++processing_template_decl;
544 ++processing_template_parmlist;
545 note_template_header (0);
548 /* This routine is called when a specialization is declared. If it is
549 invalid to declare a specialization here, an error is reported and
550 false is returned, otherwise this routine will return true. */
552 static bool
553 check_specialization_scope (void)
555 tree scope = current_scope ();
557 /* [temp.expl.spec]
559 An explicit specialization shall be declared in the namespace of
560 which the template is a member, or, for member templates, in the
561 namespace of which the enclosing class or enclosing class
562 template is a member. An explicit specialization of a member
563 function, member class or static data member of a class template
564 shall be declared in the namespace of which the class template
565 is a member. */
566 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
568 error ("explicit specialization in non-namespace scope %qD", scope);
569 return false;
572 /* [temp.expl.spec]
574 In an explicit specialization declaration for a member of a class
575 template or a member template that appears in namespace scope,
576 the member template and some of its enclosing class templates may
577 remain unspecialized, except that the declaration shall not
578 explicitly specialize a class member template if its enclosing
579 class templates are not explicitly specialized as well. */
580 if (current_template_parms)
582 error ("enclosing class templates are not explicitly specialized");
583 return false;
586 return true;
589 /* We've just seen template <>. */
591 bool
592 begin_specialization (void)
594 begin_scope (sk_template_spec, NULL);
595 note_template_header (1);
596 return check_specialization_scope ();
599 /* Called at then end of processing a declaration preceded by
600 template<>. */
602 void
603 end_specialization (void)
605 finish_scope ();
606 reset_specialization ();
609 /* Any template <>'s that we have seen thus far are not referring to a
610 function specialization. */
612 void
613 reset_specialization (void)
615 processing_specialization = 0;
616 template_header_count = 0;
619 /* We've just seen a template header. If SPECIALIZATION is nonzero,
620 it was of the form template <>. */
622 static void
623 note_template_header (int specialization)
625 processing_specialization = specialization;
626 template_header_count++;
629 /* We're beginning an explicit instantiation. */
631 void
632 begin_explicit_instantiation (void)
634 gcc_assert (!processing_explicit_instantiation);
635 processing_explicit_instantiation = true;
639 void
640 end_explicit_instantiation (void)
642 gcc_assert (processing_explicit_instantiation);
643 processing_explicit_instantiation = false;
646 /* An explicit specialization or partial specialization TMPL is being
647 declared. Check that the namespace in which the specialization is
648 occurring is permissible. Returns false iff it is invalid to
649 specialize TMPL in the current namespace. */
651 static bool
652 check_specialization_namespace (tree tmpl)
654 tree tpl_ns = decl_namespace_context (tmpl);
656 /* [tmpl.expl.spec]
658 An explicit specialization shall be declared in the namespace of
659 which the template is a member, or, for member templates, in the
660 namespace of which the enclosing class or enclosing class
661 template is a member. An explicit specialization of a member
662 function, member class or static data member of a class template
663 shall be declared in the namespace of which the class template is
664 a member. */
665 if (is_associated_namespace (current_namespace, tpl_ns))
666 /* Same or super-using namespace. */
667 return true;
668 else
670 pedwarn ("specialization of %qD in different namespace", tmpl);
671 pedwarn (" from definition of %q+#D", tmpl);
672 return false;
676 /* SPEC is an explicit instantiation. Check that it is valid to
677 perform this explicit instantiation in the current namespace. */
679 static void
680 check_explicit_instantiation_namespace (tree spec)
682 tree ns;
684 /* DR 275: An explicit instantiation shall appear in an enclosing
685 namespace of its template. */
686 ns = decl_namespace_context (spec);
687 if (!is_ancestor (current_namespace, ns))
688 pedwarn ("explicit instantiation of %qD in namespace %qD "
689 "(which does not enclose namespace %qD)",
690 spec, current_namespace, ns);
693 /* The TYPE is being declared. If it is a template type, that means it
694 is a partial specialization. Do appropriate error-checking. */
696 tree
697 maybe_process_partial_specialization (tree type)
699 tree context;
701 if (type == error_mark_node)
702 return error_mark_node;
704 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
706 error ("name of class shadows template template parameter %qD",
707 TYPE_NAME (type));
708 return error_mark_node;
711 context = TYPE_CONTEXT (type);
713 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
715 /* This is for ordinary explicit specialization and partial
716 specialization of a template class such as:
718 template <> class C<int>;
722 template <class T> class C<T*>;
724 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
726 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
727 && !COMPLETE_TYPE_P (type))
729 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
730 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
731 if (processing_template_decl)
732 push_template_decl (TYPE_MAIN_DECL (type));
734 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
735 error ("specialization of %qT after instantiation", type);
737 else if (CLASS_TYPE_P (type)
738 && !CLASSTYPE_USE_TEMPLATE (type)
739 && CLASSTYPE_TEMPLATE_INFO (type)
740 && context && CLASS_TYPE_P (context)
741 && CLASSTYPE_TEMPLATE_INFO (context))
743 /* This is for an explicit specialization of member class
744 template according to [temp.expl.spec/18]:
746 template <> template <class U> class C<int>::D;
748 The context `C<int>' must be an implicit instantiation.
749 Otherwise this is just a member class template declared
750 earlier like:
752 template <> class C<int> { template <class U> class D; };
753 template <> template <class U> class C<int>::D;
755 In the first case, `C<int>::D' is a specialization of `C<T>::D'
756 while in the second case, `C<int>::D' is a primary template
757 and `C<T>::D' may not exist. */
759 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
760 && !COMPLETE_TYPE_P (type))
762 tree t;
764 if (current_namespace
765 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
767 pedwarn ("specializing %q#T in different namespace", type);
768 pedwarn (" from definition of %q+#D",
769 CLASSTYPE_TI_TEMPLATE (type));
772 /* Check for invalid specialization after instantiation:
774 template <> template <> class C<int>::D<int>;
775 template <> template <class U> class C<int>::D; */
777 for (t = DECL_TEMPLATE_INSTANTIATIONS
778 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
779 t; t = TREE_CHAIN (t))
780 if (TREE_VALUE (t) != type
781 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
782 error ("specialization %qT after instantiation %qT",
783 type, TREE_VALUE (t));
785 /* Mark TYPE as a specialization. And as a result, we only
786 have one level of template argument for the innermost
787 class template. */
788 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
789 CLASSTYPE_TI_ARGS (type)
790 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
793 else if (processing_specialization)
795 error ("explicit specialization of non-template %qT", type);
796 return error_mark_node;
799 return type;
802 /* Returns nonzero if we can optimize the retrieval of specializations
803 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
804 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
806 static inline bool
807 optimize_specialization_lookup_p (tree tmpl)
809 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
810 && DECL_CLASS_SCOPE_P (tmpl)
811 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
812 parameter. */
813 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
814 /* The optimized lookup depends on the fact that the
815 template arguments for the member function template apply
816 purely to the containing class, which is not true if the
817 containing class is an explicit or partial
818 specialization. */
819 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
820 && !DECL_MEMBER_TEMPLATE_P (tmpl)
821 && !DECL_CONV_FN_P (tmpl)
822 /* It is possible to have a template that is not a member
823 template and is not a member of a template class:
825 template <typename T>
826 struct S { friend A::f(); };
828 Here, the friend function is a template, but the context does
829 not have template information. The optimized lookup relies
830 on having ARGS be the template arguments for both the class
831 and the function template. */
832 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
835 /* Retrieve the specialization (in the sense of [temp.spec] - a
836 specialization is either an instantiation or an explicit
837 specialization) of TMPL for the given template ARGS. If there is
838 no such specialization, return NULL_TREE. The ARGS are a vector of
839 arguments, or a vector of vectors of arguments, in the case of
840 templates with more than one level of parameters.
842 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
843 then we search for a partial specialization matching ARGS. This
844 parameter is ignored if TMPL is not a class template. */
846 static tree
847 retrieve_specialization (tree tmpl, tree args,
848 bool class_specializations_p)
850 if (args == error_mark_node)
851 return NULL_TREE;
853 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
855 /* There should be as many levels of arguments as there are
856 levels of parameters. */
857 gcc_assert (TMPL_ARGS_DEPTH (args)
858 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
860 if (optimize_specialization_lookup_p (tmpl))
862 tree class_template;
863 tree class_specialization;
864 VEC(tree,gc) *methods;
865 tree fns;
866 int idx;
868 /* The template arguments actually apply to the containing
869 class. Find the class specialization with those
870 arguments. */
871 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
872 class_specialization
873 = retrieve_specialization (class_template, args,
874 /*class_specializations_p=*/false);
875 if (!class_specialization)
876 return NULL_TREE;
877 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
878 for the specialization. */
879 idx = class_method_index_for_fn (class_specialization, tmpl);
880 if (idx == -1)
881 return NULL_TREE;
882 /* Iterate through the methods with the indicated name, looking
883 for the one that has an instance of TMPL. */
884 methods = CLASSTYPE_METHOD_VEC (class_specialization);
885 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
887 tree fn = OVL_CURRENT (fns);
888 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
889 return fn;
891 return NULL_TREE;
893 else
895 tree *sp;
896 tree *head;
898 /* Class templates store their instantiations on the
899 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
900 DECL_TEMPLATE_SPECIALIZATIONS list. */
901 if (!class_specializations_p
902 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
903 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
904 else
905 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
906 head = sp;
907 /* Iterate through the list until we find a matching template. */
908 while (*sp != NULL_TREE)
910 tree spec = *sp;
912 if (comp_template_args (TREE_PURPOSE (spec), args))
914 /* Use the move-to-front heuristic to speed up future
915 searches. */
916 if (spec != *head)
918 *sp = TREE_CHAIN (*sp);
919 TREE_CHAIN (spec) = *head;
920 *head = spec;
922 return TREE_VALUE (spec);
924 sp = &TREE_CHAIN (spec);
928 return NULL_TREE;
931 /* Like retrieve_specialization, but for local declarations. */
933 static tree
934 retrieve_local_specialization (tree tmpl)
936 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
937 htab_hash_pointer (tmpl));
938 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
941 /* Returns nonzero iff DECL is a specialization of TMPL. */
944 is_specialization_of (tree decl, tree tmpl)
946 tree t;
948 if (TREE_CODE (decl) == FUNCTION_DECL)
950 for (t = decl;
951 t != NULL_TREE;
952 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
953 if (t == tmpl)
954 return 1;
956 else
958 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
960 for (t = TREE_TYPE (decl);
961 t != NULL_TREE;
962 t = CLASSTYPE_USE_TEMPLATE (t)
963 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
964 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
965 return 1;
968 return 0;
971 /* Returns nonzero iff DECL is a specialization of friend declaration
972 FRIEND according to [temp.friend]. */
974 bool
975 is_specialization_of_friend (tree decl, tree friend)
977 bool need_template = true;
978 int template_depth;
980 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
981 || TREE_CODE (decl) == TYPE_DECL);
983 /* For [temp.friend/6] when FRIEND is an ordinary member function
984 of a template class, we want to check if DECL is a specialization
985 if this. */
986 if (TREE_CODE (friend) == FUNCTION_DECL
987 && DECL_TEMPLATE_INFO (friend)
988 && !DECL_USE_TEMPLATE (friend))
990 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
991 friend = DECL_TI_TEMPLATE (friend);
992 need_template = false;
994 else if (TREE_CODE (friend) == TEMPLATE_DECL
995 && !PRIMARY_TEMPLATE_P (friend))
996 need_template = false;
998 /* There is nothing to do if this is not a template friend. */
999 if (TREE_CODE (friend) != TEMPLATE_DECL)
1000 return false;
1002 if (is_specialization_of (decl, friend))
1003 return true;
1005 /* [temp.friend/6]
1006 A member of a class template may be declared to be a friend of a
1007 non-template class. In this case, the corresponding member of
1008 every specialization of the class template is a friend of the
1009 class granting friendship.
1011 For example, given a template friend declaration
1013 template <class T> friend void A<T>::f();
1015 the member function below is considered a friend
1017 template <> struct A<int> {
1018 void f();
1021 For this type of template friend, TEMPLATE_DEPTH below will be
1022 nonzero. To determine if DECL is a friend of FRIEND, we first
1023 check if the enclosing class is a specialization of another. */
1025 template_depth = template_class_depth (DECL_CONTEXT (friend));
1026 if (template_depth
1027 && DECL_CLASS_SCOPE_P (decl)
1028 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1029 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1031 /* Next, we check the members themselves. In order to handle
1032 a few tricky cases, such as when FRIEND's are
1034 template <class T> friend void A<T>::g(T t);
1035 template <class T> template <T t> friend void A<T>::h();
1037 and DECL's are
1039 void A<int>::g(int);
1040 template <int> void A<int>::h();
1042 we need to figure out ARGS, the template arguments from
1043 the context of DECL. This is required for template substitution
1044 of `T' in the function parameter of `g' and template parameter
1045 of `h' in the above examples. Here ARGS corresponds to `int'. */
1047 tree context = DECL_CONTEXT (decl);
1048 tree args = NULL_TREE;
1049 int current_depth = 0;
1051 while (current_depth < template_depth)
1053 if (CLASSTYPE_TEMPLATE_INFO (context))
1055 if (current_depth == 0)
1056 args = TYPE_TI_ARGS (context);
1057 else
1058 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1059 current_depth++;
1061 context = TYPE_CONTEXT (context);
1064 if (TREE_CODE (decl) == FUNCTION_DECL)
1066 bool is_template;
1067 tree friend_type;
1068 tree decl_type;
1069 tree friend_args_type;
1070 tree decl_args_type;
1072 /* Make sure that both DECL and FRIEND are templates or
1073 non-templates. */
1074 is_template = DECL_TEMPLATE_INFO (decl)
1075 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1076 if (need_template ^ is_template)
1077 return false;
1078 else if (is_template)
1080 /* If both are templates, check template parameter list. */
1081 tree friend_parms
1082 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1083 args, tf_none);
1084 if (!comp_template_parms
1085 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1086 friend_parms))
1087 return false;
1089 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1091 else
1092 decl_type = TREE_TYPE (decl);
1094 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1095 tf_none, NULL_TREE);
1096 if (friend_type == error_mark_node)
1097 return false;
1099 /* Check if return types match. */
1100 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1101 return false;
1103 /* Check if function parameter types match, ignoring the
1104 `this' parameter. */
1105 friend_args_type = TYPE_ARG_TYPES (friend_type);
1106 decl_args_type = TYPE_ARG_TYPES (decl_type);
1107 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1108 friend_args_type = TREE_CHAIN (friend_args_type);
1109 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1110 decl_args_type = TREE_CHAIN (decl_args_type);
1112 return compparms (decl_args_type, friend_args_type);
1114 else
1116 /* DECL is a TYPE_DECL */
1117 bool is_template;
1118 tree decl_type = TREE_TYPE (decl);
1120 /* Make sure that both DECL and FRIEND are templates or
1121 non-templates. */
1122 is_template
1123 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1124 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1126 if (need_template ^ is_template)
1127 return false;
1128 else if (is_template)
1130 tree friend_parms;
1131 /* If both are templates, check the name of the two
1132 TEMPLATE_DECL's first because is_friend didn't. */
1133 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1134 != DECL_NAME (friend))
1135 return false;
1137 /* Now check template parameter list. */
1138 friend_parms
1139 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1140 args, tf_none);
1141 return comp_template_parms
1142 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1143 friend_parms);
1145 else
1146 return (DECL_NAME (decl)
1147 == DECL_NAME (friend));
1150 return false;
1153 /* Register the specialization SPEC as a specialization of TMPL with
1154 the indicated ARGS. IS_FRIEND indicates whether the specialization
1155 is actually just a friend declaration. Returns SPEC, or an
1156 equivalent prior declaration, if available. */
1158 static tree
1159 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1161 tree fn;
1163 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1165 if (TREE_CODE (spec) == FUNCTION_DECL
1166 && uses_template_parms (DECL_TI_ARGS (spec)))
1167 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1168 register it; we want the corresponding TEMPLATE_DECL instead.
1169 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1170 the more obvious `uses_template_parms (spec)' to avoid problems
1171 with default function arguments. In particular, given
1172 something like this:
1174 template <class T> void f(T t1, T t = T())
1176 the default argument expression is not substituted for in an
1177 instantiation unless and until it is actually needed. */
1178 return spec;
1180 fn = retrieve_specialization (tmpl, args,
1181 /*class_specializations_p=*/false);
1182 /* We can sometimes try to re-register a specialization that we've
1183 already got. In particular, regenerate_decl_from_template calls
1184 duplicate_decls which will update the specialization list. But,
1185 we'll still get called again here anyhow. It's more convenient
1186 to simply allow this than to try to prevent it. */
1187 if (fn == spec)
1188 return spec;
1189 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1191 if (DECL_TEMPLATE_INSTANTIATION (fn))
1193 if (TREE_USED (fn)
1194 || DECL_EXPLICIT_INSTANTIATION (fn))
1196 error ("specialization of %qD after instantiation",
1197 fn);
1198 return error_mark_node;
1200 else
1202 tree clone;
1203 /* This situation should occur only if the first
1204 specialization is an implicit instantiation, the
1205 second is an explicit specialization, and the
1206 implicit instantiation has not yet been used. That
1207 situation can occur if we have implicitly
1208 instantiated a member function and then specialized
1209 it later.
1211 We can also wind up here if a friend declaration that
1212 looked like an instantiation turns out to be a
1213 specialization:
1215 template <class T> void foo(T);
1216 class S { friend void foo<>(int) };
1217 template <> void foo(int);
1219 We transform the existing DECL in place so that any
1220 pointers to it become pointers to the updated
1221 declaration.
1223 If there was a definition for the template, but not
1224 for the specialization, we want this to look as if
1225 there were no definition, and vice versa. */
1226 DECL_INITIAL (fn) = NULL_TREE;
1227 duplicate_decls (spec, fn, is_friend);
1228 /* The call to duplicate_decls will have applied
1229 [temp.expl.spec]:
1231 An explicit specialization of a function template
1232 is inline only if it is explicitly declared to be,
1233 and independently of whether its function template
1236 to the primary function; now copy the inline bits to
1237 the various clones. */
1238 FOR_EACH_CLONE (clone, fn)
1240 DECL_DECLARED_INLINE_P (clone)
1241 = DECL_DECLARED_INLINE_P (fn);
1242 DECL_INLINE (clone)
1243 = DECL_INLINE (fn);
1245 check_specialization_namespace (fn);
1247 return fn;
1250 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1252 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1253 /* Dup decl failed, but this is a new definition. Set the
1254 line number so any errors match this new
1255 definition. */
1256 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1258 return fn;
1262 /* A specialization must be declared in the same namespace as the
1263 template it is specializing. */
1264 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1265 && !check_specialization_namespace (tmpl))
1266 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1268 if (!optimize_specialization_lookup_p (tmpl))
1269 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1270 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1272 return spec;
1275 /* Unregister the specialization SPEC as a specialization of TMPL.
1276 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1277 if the SPEC was listed as a specialization of TMPL. */
1279 bool
1280 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1282 tree* s;
1284 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1285 *s != NULL_TREE;
1286 s = &TREE_CHAIN (*s))
1287 if (TREE_VALUE (*s) == spec)
1289 if (!new_spec)
1290 *s = TREE_CHAIN (*s);
1291 else
1292 TREE_VALUE (*s) = new_spec;
1293 return 1;
1296 return 0;
1299 /* Compare an entry in the local specializations hash table P1 (which
1300 is really a pointer to a TREE_LIST) with P2 (which is really a
1301 DECL). */
1303 static int
1304 eq_local_specializations (const void *p1, const void *p2)
1306 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1309 /* Hash P1, an entry in the local specializations table. */
1311 static hashval_t
1312 hash_local_specialization (const void* p1)
1314 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1317 /* Like register_specialization, but for local declarations. We are
1318 registering SPEC, an instantiation of TMPL. */
1320 static void
1321 register_local_specialization (tree spec, tree tmpl)
1323 void **slot;
1325 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1326 htab_hash_pointer (tmpl), INSERT);
1327 *slot = build_tree_list (spec, tmpl);
1330 /* TYPE is a class type. Returns true if TYPE is an explicitly
1331 specialized class. */
1333 bool
1334 explicit_class_specialization_p (tree type)
1336 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1337 return false;
1338 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1341 /* Print the list of candidate FNS in an error message. */
1343 void
1344 print_candidates (tree fns)
1346 tree fn;
1348 const char *str = "candidates are:";
1350 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1352 tree f;
1354 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1355 error ("%s %+#D", str, OVL_CURRENT (f));
1356 str = " ";
1360 /* Returns the template (one of the functions given by TEMPLATE_ID)
1361 which can be specialized to match the indicated DECL with the
1362 explicit template args given in TEMPLATE_ID. The DECL may be
1363 NULL_TREE if none is available. In that case, the functions in
1364 TEMPLATE_ID are non-members.
1366 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1367 specialization of a member template.
1369 The TEMPLATE_COUNT is the number of references to qualifying
1370 template classes that appeared in the name of the function. See
1371 check_explicit_specialization for a more accurate description.
1373 TSK indicates what kind of template declaration (if any) is being
1374 declared. TSK_TEMPLATE indicates that the declaration given by
1375 DECL, though a FUNCTION_DECL, has template parameters, and is
1376 therefore a template function.
1378 The template args (those explicitly specified and those deduced)
1379 are output in a newly created vector *TARGS_OUT.
1381 If it is impossible to determine the result, an error message is
1382 issued. The error_mark_node is returned to indicate failure. */
1384 static tree
1385 determine_specialization (tree template_id,
1386 tree decl,
1387 tree* targs_out,
1388 int need_member_template,
1389 int template_count,
1390 tmpl_spec_kind tsk)
1392 tree fns;
1393 tree targs;
1394 tree explicit_targs;
1395 tree candidates = NULL_TREE;
1396 /* A TREE_LIST of templates of which DECL may be a specialization.
1397 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1398 corresponding TREE_PURPOSE is the set of template arguments that,
1399 when used to instantiate the template, would produce a function
1400 with the signature of DECL. */
1401 tree templates = NULL_TREE;
1402 int header_count;
1403 struct cp_binding_level *b;
1405 *targs_out = NULL_TREE;
1407 if (template_id == error_mark_node || decl == error_mark_node)
1408 return error_mark_node;
1410 fns = TREE_OPERAND (template_id, 0);
1411 explicit_targs = TREE_OPERAND (template_id, 1);
1413 if (fns == error_mark_node)
1414 return error_mark_node;
1416 /* Check for baselinks. */
1417 if (BASELINK_P (fns))
1418 fns = BASELINK_FUNCTIONS (fns);
1420 if (!is_overloaded_fn (fns))
1422 error ("%qD is not a function template", fns);
1423 return error_mark_node;
1426 /* Count the number of template headers specified for this
1427 specialization. */
1428 header_count = 0;
1429 for (b = current_binding_level;
1430 b->kind == sk_template_parms;
1431 b = b->level_chain)
1432 ++header_count;
1434 for (; fns; fns = OVL_NEXT (fns))
1436 tree fn = OVL_CURRENT (fns);
1438 if (TREE_CODE (fn) == TEMPLATE_DECL)
1440 tree decl_arg_types;
1441 tree fn_arg_types;
1443 /* In case of explicit specialization, we need to check if
1444 the number of template headers appearing in the specialization
1445 is correct. This is usually done in check_explicit_specialization,
1446 but the check done there cannot be exhaustive when specializing
1447 member functions. Consider the following code:
1449 template <> void A<int>::f(int);
1450 template <> template <> void A<int>::f(int);
1452 Assuming that A<int> is not itself an explicit specialization
1453 already, the first line specializes "f" which is a non-template
1454 member function, whilst the second line specializes "f" which
1455 is a template member function. So both lines are syntactically
1456 correct, and check_explicit_specialization does not reject
1457 them.
1459 Here, we can do better, as we are matching the specialization
1460 against the declarations. We count the number of template
1461 headers, and we check if they match TEMPLATE_COUNT + 1
1462 (TEMPLATE_COUNT is the number of qualifying template classes,
1463 plus there must be another header for the member template
1464 itself).
1466 Notice that if header_count is zero, this is not a
1467 specialization but rather a template instantiation, so there
1468 is no check we can perform here. */
1469 if (header_count && header_count != template_count + 1)
1470 continue;
1472 /* Check that the number of template arguments at the
1473 innermost level for DECL is the same as for FN. */
1474 if (current_binding_level->kind == sk_template_parms
1475 && !current_binding_level->explicit_spec_p
1476 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1477 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1478 (current_template_parms))))
1479 continue;
1481 /* DECL might be a specialization of FN. */
1482 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1483 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1485 /* For a non-static member function, we need to make sure
1486 that the const qualification is the same. Since
1487 get_bindings does not try to merge the "this" parameter,
1488 we must do the comparison explicitly. */
1489 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1490 && !same_type_p (TREE_VALUE (fn_arg_types),
1491 TREE_VALUE (decl_arg_types)))
1492 continue;
1494 /* Skip the "this" parameter and, for constructors of
1495 classes with virtual bases, the VTT parameter. A
1496 full specialization of a constructor will have a VTT
1497 parameter, but a template never will. */
1498 decl_arg_types
1499 = skip_artificial_parms_for (decl, decl_arg_types);
1500 fn_arg_types
1501 = skip_artificial_parms_for (fn, fn_arg_types);
1503 /* Check that the number of function parameters matches.
1504 For example,
1505 template <class T> void f(int i = 0);
1506 template <> void f<int>();
1507 The specialization f<int> is invalid but is not caught
1508 by get_bindings below. */
1509 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1510 continue;
1512 /* Function templates cannot be specializations; there are
1513 no partial specializations of functions. Therefore, if
1514 the type of DECL does not match FN, there is no
1515 match. */
1516 if (tsk == tsk_template)
1518 if (compparms (fn_arg_types, decl_arg_types))
1519 candidates = tree_cons (NULL_TREE, fn, candidates);
1520 continue;
1523 /* See whether this function might be a specialization of this
1524 template. */
1525 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1527 if (!targs)
1528 /* We cannot deduce template arguments that when used to
1529 specialize TMPL will produce DECL. */
1530 continue;
1532 /* Save this template, and the arguments deduced. */
1533 templates = tree_cons (targs, fn, templates);
1535 else if (need_member_template)
1536 /* FN is an ordinary member function, and we need a
1537 specialization of a member template. */
1539 else if (TREE_CODE (fn) != FUNCTION_DECL)
1540 /* We can get IDENTIFIER_NODEs here in certain erroneous
1541 cases. */
1543 else if (!DECL_FUNCTION_MEMBER_P (fn))
1544 /* This is just an ordinary non-member function. Nothing can
1545 be a specialization of that. */
1547 else if (DECL_ARTIFICIAL (fn))
1548 /* Cannot specialize functions that are created implicitly. */
1550 else
1552 tree decl_arg_types;
1554 /* This is an ordinary member function. However, since
1555 we're here, we can assume it's enclosing class is a
1556 template class. For example,
1558 template <typename T> struct S { void f(); };
1559 template <> void S<int>::f() {}
1561 Here, S<int>::f is a non-template, but S<int> is a
1562 template class. If FN has the same type as DECL, we
1563 might be in business. */
1565 if (!DECL_TEMPLATE_INFO (fn))
1566 /* Its enclosing class is an explicit specialization
1567 of a template class. This is not a candidate. */
1568 continue;
1570 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1571 TREE_TYPE (TREE_TYPE (fn))))
1572 /* The return types differ. */
1573 continue;
1575 /* Adjust the type of DECL in case FN is a static member. */
1576 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1577 if (DECL_STATIC_FUNCTION_P (fn)
1578 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1579 decl_arg_types = TREE_CHAIN (decl_arg_types);
1581 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1582 decl_arg_types))
1583 /* They match! */
1584 candidates = tree_cons (NULL_TREE, fn, candidates);
1588 if (templates && TREE_CHAIN (templates))
1590 /* We have:
1592 [temp.expl.spec]
1594 It is possible for a specialization with a given function
1595 signature to be instantiated from more than one function
1596 template. In such cases, explicit specification of the
1597 template arguments must be used to uniquely identify the
1598 function template specialization being specialized.
1600 Note that here, there's no suggestion that we're supposed to
1601 determine which of the candidate templates is most
1602 specialized. However, we, also have:
1604 [temp.func.order]
1606 Partial ordering of overloaded function template
1607 declarations is used in the following contexts to select
1608 the function template to which a function template
1609 specialization refers:
1611 -- when an explicit specialization refers to a function
1612 template.
1614 So, we do use the partial ordering rules, at least for now.
1615 This extension can only serve to make invalid programs valid,
1616 so it's safe. And, there is strong anecdotal evidence that
1617 the committee intended the partial ordering rules to apply;
1618 the EDG front end has that behavior, and John Spicer claims
1619 that the committee simply forgot to delete the wording in
1620 [temp.expl.spec]. */
1621 tree tmpl = most_specialized_instantiation (templates);
1622 if (tmpl != error_mark_node)
1624 templates = tmpl;
1625 TREE_CHAIN (templates) = NULL_TREE;
1629 if (templates == NULL_TREE && candidates == NULL_TREE)
1631 error ("template-id %qD for %q+D does not match any template "
1632 "declaration", template_id, decl);
1633 return error_mark_node;
1635 else if ((templates && TREE_CHAIN (templates))
1636 || (candidates && TREE_CHAIN (candidates))
1637 || (templates && candidates))
1639 error ("ambiguous template specialization %qD for %q+D",
1640 template_id, decl);
1641 chainon (candidates, templates);
1642 print_candidates (candidates);
1643 return error_mark_node;
1646 /* We have one, and exactly one, match. */
1647 if (candidates)
1649 tree fn = TREE_VALUE (candidates);
1650 /* DECL is a re-declaration of a template function. */
1651 if (TREE_CODE (fn) == TEMPLATE_DECL)
1652 return fn;
1653 /* It was a specialization of an ordinary member function in a
1654 template class. */
1655 *targs_out = copy_node (DECL_TI_ARGS (fn));
1656 return DECL_TI_TEMPLATE (fn);
1659 /* It was a specialization of a template. */
1660 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1661 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1663 *targs_out = copy_node (targs);
1664 SET_TMPL_ARGS_LEVEL (*targs_out,
1665 TMPL_ARGS_DEPTH (*targs_out),
1666 TREE_PURPOSE (templates));
1668 else
1669 *targs_out = TREE_PURPOSE (templates);
1670 return TREE_VALUE (templates);
1673 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1674 but with the default argument values filled in from those in the
1675 TMPL_TYPES. */
1677 static tree
1678 copy_default_args_to_explicit_spec_1 (tree spec_types,
1679 tree tmpl_types)
1681 tree new_spec_types;
1683 if (!spec_types)
1684 return NULL_TREE;
1686 if (spec_types == void_list_node)
1687 return void_list_node;
1689 /* Substitute into the rest of the list. */
1690 new_spec_types =
1691 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1692 TREE_CHAIN (tmpl_types));
1694 /* Add the default argument for this parameter. */
1695 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1696 TREE_VALUE (spec_types),
1697 new_spec_types);
1700 /* DECL is an explicit specialization. Replicate default arguments
1701 from the template it specializes. (That way, code like:
1703 template <class T> void f(T = 3);
1704 template <> void f(double);
1705 void g () { f (); }
1707 works, as required.) An alternative approach would be to look up
1708 the correct default arguments at the call-site, but this approach
1709 is consistent with how implicit instantiations are handled. */
1711 static void
1712 copy_default_args_to_explicit_spec (tree decl)
1714 tree tmpl;
1715 tree spec_types;
1716 tree tmpl_types;
1717 tree new_spec_types;
1718 tree old_type;
1719 tree new_type;
1720 tree t;
1721 tree object_type = NULL_TREE;
1722 tree in_charge = NULL_TREE;
1723 tree vtt = NULL_TREE;
1725 /* See if there's anything we need to do. */
1726 tmpl = DECL_TI_TEMPLATE (decl);
1727 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1728 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1729 if (TREE_PURPOSE (t))
1730 break;
1731 if (!t)
1732 return;
1734 old_type = TREE_TYPE (decl);
1735 spec_types = TYPE_ARG_TYPES (old_type);
1737 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1739 /* Remove the this pointer, but remember the object's type for
1740 CV quals. */
1741 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1742 spec_types = TREE_CHAIN (spec_types);
1743 tmpl_types = TREE_CHAIN (tmpl_types);
1745 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1747 /* DECL may contain more parameters than TMPL due to the extra
1748 in-charge parameter in constructors and destructors. */
1749 in_charge = spec_types;
1750 spec_types = TREE_CHAIN (spec_types);
1752 if (DECL_HAS_VTT_PARM_P (decl))
1754 vtt = spec_types;
1755 spec_types = TREE_CHAIN (spec_types);
1759 /* Compute the merged default arguments. */
1760 new_spec_types =
1761 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1763 /* Compute the new FUNCTION_TYPE. */
1764 if (object_type)
1766 if (vtt)
1767 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1768 TREE_VALUE (vtt),
1769 new_spec_types);
1771 if (in_charge)
1772 /* Put the in-charge parameter back. */
1773 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1774 TREE_VALUE (in_charge),
1775 new_spec_types);
1777 new_type = build_method_type_directly (object_type,
1778 TREE_TYPE (old_type),
1779 new_spec_types);
1781 else
1782 new_type = build_function_type (TREE_TYPE (old_type),
1783 new_spec_types);
1784 new_type = cp_build_type_attribute_variant (new_type,
1785 TYPE_ATTRIBUTES (old_type));
1786 new_type = build_exception_variant (new_type,
1787 TYPE_RAISES_EXCEPTIONS (old_type));
1788 TREE_TYPE (decl) = new_type;
1791 /* Check to see if the function just declared, as indicated in
1792 DECLARATOR, and in DECL, is a specialization of a function
1793 template. We may also discover that the declaration is an explicit
1794 instantiation at this point.
1796 Returns DECL, or an equivalent declaration that should be used
1797 instead if all goes well. Issues an error message if something is
1798 amiss. Returns error_mark_node if the error is not easily
1799 recoverable.
1801 FLAGS is a bitmask consisting of the following flags:
1803 2: The function has a definition.
1804 4: The function is a friend.
1806 The TEMPLATE_COUNT is the number of references to qualifying
1807 template classes that appeared in the name of the function. For
1808 example, in
1810 template <class T> struct S { void f(); };
1811 void S<int>::f();
1813 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1814 classes are not counted in the TEMPLATE_COUNT, so that in
1816 template <class T> struct S {};
1817 template <> struct S<int> { void f(); }
1818 template <> void S<int>::f();
1820 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1821 invalid; there should be no template <>.)
1823 If the function is a specialization, it is marked as such via
1824 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1825 is set up correctly, and it is added to the list of specializations
1826 for that template. */
1828 tree
1829 check_explicit_specialization (tree declarator,
1830 tree decl,
1831 int template_count,
1832 int flags)
1834 int have_def = flags & 2;
1835 int is_friend = flags & 4;
1836 int specialization = 0;
1837 int explicit_instantiation = 0;
1838 int member_specialization = 0;
1839 tree ctype = DECL_CLASS_CONTEXT (decl);
1840 tree dname = DECL_NAME (decl);
1841 tmpl_spec_kind tsk;
1843 if (is_friend)
1845 if (!processing_specialization)
1846 tsk = tsk_none;
1847 else
1848 tsk = tsk_excessive_parms;
1850 else
1851 tsk = current_tmpl_spec_kind (template_count);
1853 switch (tsk)
1855 case tsk_none:
1856 if (processing_specialization)
1858 specialization = 1;
1859 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1861 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1863 if (is_friend)
1864 /* This could be something like:
1866 template <class T> void f(T);
1867 class S { friend void f<>(int); } */
1868 specialization = 1;
1869 else
1871 /* This case handles bogus declarations like template <>
1872 template <class T> void f<int>(); */
1874 error ("template-id %qD in declaration of primary template",
1875 declarator);
1876 return decl;
1879 break;
1881 case tsk_invalid_member_spec:
1882 /* The error has already been reported in
1883 check_specialization_scope. */
1884 return error_mark_node;
1886 case tsk_invalid_expl_inst:
1887 error ("template parameter list used in explicit instantiation");
1889 /* Fall through. */
1891 case tsk_expl_inst:
1892 if (have_def)
1893 error ("definition provided for explicit instantiation");
1895 explicit_instantiation = 1;
1896 break;
1898 case tsk_excessive_parms:
1899 case tsk_insufficient_parms:
1900 if (tsk == tsk_excessive_parms)
1901 error ("too many template parameter lists in declaration of %qD",
1902 decl);
1903 else if (template_header_count)
1904 error("too few template parameter lists in declaration of %qD", decl);
1905 else
1906 error("explicit specialization of %qD must be introduced by "
1907 "%<template <>%>", decl);
1909 /* Fall through. */
1910 case tsk_expl_spec:
1911 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1912 if (ctype)
1913 member_specialization = 1;
1914 else
1915 specialization = 1;
1916 break;
1918 case tsk_template:
1919 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1921 /* This case handles bogus declarations like template <>
1922 template <class T> void f<int>(); */
1924 if (uses_template_parms (declarator))
1925 error ("function template partial specialization %qD "
1926 "is not allowed", declarator);
1927 else
1928 error ("template-id %qD in declaration of primary template",
1929 declarator);
1930 return decl;
1933 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1934 /* This is a specialization of a member template, without
1935 specialization the containing class. Something like:
1937 template <class T> struct S {
1938 template <class U> void f (U);
1940 template <> template <class U> void S<int>::f(U) {}
1942 That's a specialization -- but of the entire template. */
1943 specialization = 1;
1944 break;
1946 default:
1947 gcc_unreachable ();
1950 if (specialization || member_specialization)
1952 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1953 for (; t; t = TREE_CHAIN (t))
1954 if (TREE_PURPOSE (t))
1956 pedwarn
1957 ("default argument specified in explicit specialization");
1958 break;
1962 if (specialization || member_specialization || explicit_instantiation)
1964 tree tmpl = NULL_TREE;
1965 tree targs = NULL_TREE;
1967 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1968 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1970 tree fns;
1972 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1973 if (ctype)
1974 fns = dname;
1975 else
1977 /* If there is no class context, the explicit instantiation
1978 must be at namespace scope. */
1979 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1981 /* Find the namespace binding, using the declaration
1982 context. */
1983 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1984 false, true);
1985 if (fns == error_mark_node || !is_overloaded_fn (fns))
1987 error ("%qD is not a template function", dname);
1988 fns = error_mark_node;
1990 else
1992 tree fn = OVL_CURRENT (fns);
1993 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1994 CP_DECL_CONTEXT (fn)))
1995 error ("%qD is not declared in %qD",
1996 decl, current_namespace);
2000 declarator = lookup_template_function (fns, NULL_TREE);
2003 if (declarator == error_mark_node)
2004 return error_mark_node;
2006 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2008 if (!explicit_instantiation)
2009 /* A specialization in class scope. This is invalid,
2010 but the error will already have been flagged by
2011 check_specialization_scope. */
2012 return error_mark_node;
2013 else
2015 /* It's not valid to write an explicit instantiation in
2016 class scope, e.g.:
2018 class C { template void f(); }
2020 This case is caught by the parser. However, on
2021 something like:
2023 template class C { void f(); };
2025 (which is invalid) we can get here. The error will be
2026 issued later. */
2030 return decl;
2032 else if (ctype != NULL_TREE
2033 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2034 IDENTIFIER_NODE))
2036 /* Find the list of functions in ctype that have the same
2037 name as the declared function. */
2038 tree name = TREE_OPERAND (declarator, 0);
2039 tree fns = NULL_TREE;
2040 int idx;
2042 if (constructor_name_p (name, ctype))
2044 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2046 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2047 : !CLASSTYPE_DESTRUCTORS (ctype))
2049 /* From [temp.expl.spec]:
2051 If such an explicit specialization for the member
2052 of a class template names an implicitly-declared
2053 special member function (clause _special_), the
2054 program is ill-formed.
2056 Similar language is found in [temp.explicit]. */
2057 error ("specialization of implicitly-declared special member function");
2058 return error_mark_node;
2061 name = is_constructor ? ctor_identifier : dtor_identifier;
2064 if (!DECL_CONV_FN_P (decl))
2066 idx = lookup_fnfields_1 (ctype, name);
2067 if (idx >= 0)
2068 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2070 else
2072 VEC(tree,gc) *methods;
2073 tree ovl;
2075 /* For a type-conversion operator, we cannot do a
2076 name-based lookup. We might be looking for `operator
2077 int' which will be a specialization of `operator T'.
2078 So, we find *all* the conversion operators, and then
2079 select from them. */
2080 fns = NULL_TREE;
2082 methods = CLASSTYPE_METHOD_VEC (ctype);
2083 if (methods)
2084 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2085 VEC_iterate (tree, methods, idx, ovl);
2086 ++idx)
2088 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2089 /* There are no more conversion functions. */
2090 break;
2092 /* Glue all these conversion functions together
2093 with those we already have. */
2094 for (; ovl; ovl = OVL_NEXT (ovl))
2095 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2099 if (fns == NULL_TREE)
2101 error ("no member function %qD declared in %qT", name, ctype);
2102 return error_mark_node;
2104 else
2105 TREE_OPERAND (declarator, 0) = fns;
2108 /* Figure out what exactly is being specialized at this point.
2109 Note that for an explicit instantiation, even one for a
2110 member function, we cannot tell apriori whether the
2111 instantiation is for a member template, or just a member
2112 function of a template class. Even if a member template is
2113 being instantiated, the member template arguments may be
2114 elided if they can be deduced from the rest of the
2115 declaration. */
2116 tmpl = determine_specialization (declarator, decl,
2117 &targs,
2118 member_specialization,
2119 template_count,
2120 tsk);
2122 if (!tmpl || tmpl == error_mark_node)
2123 /* We couldn't figure out what this declaration was
2124 specializing. */
2125 return error_mark_node;
2126 else
2128 tree gen_tmpl = most_general_template (tmpl);
2130 if (explicit_instantiation)
2132 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2133 is done by do_decl_instantiation later. */
2135 int arg_depth = TMPL_ARGS_DEPTH (targs);
2136 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2138 if (arg_depth > parm_depth)
2140 /* If TMPL is not the most general template (for
2141 example, if TMPL is a friend template that is
2142 injected into namespace scope), then there will
2143 be too many levels of TARGS. Remove some of them
2144 here. */
2145 int i;
2146 tree new_targs;
2148 new_targs = make_tree_vec (parm_depth);
2149 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2150 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2151 = TREE_VEC_ELT (targs, i);
2152 targs = new_targs;
2155 return instantiate_template (tmpl, targs, tf_error);
2158 /* If we thought that the DECL was a member function, but it
2159 turns out to be specializing a static member function,
2160 make DECL a static member function as well. */
2161 if (DECL_STATIC_FUNCTION_P (tmpl)
2162 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2163 revert_static_member_fn (decl);
2165 /* If this is a specialization of a member template of a
2166 template class, we want to return the TEMPLATE_DECL, not
2167 the specialization of it. */
2168 if (tsk == tsk_template)
2170 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2171 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2172 if (have_def)
2174 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2175 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2176 = DECL_SOURCE_LOCATION (decl);
2177 /* We want to use the argument list specified in the
2178 definition, not in the original declaration. */
2179 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2180 = DECL_ARGUMENTS (decl);
2182 return tmpl;
2185 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2186 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2188 /* Inherit default function arguments from the template
2189 DECL is specializing. */
2190 copy_default_args_to_explicit_spec (decl);
2192 /* This specialization has the same protection as the
2193 template it specializes. */
2194 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2195 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2197 /* 7.1.1-1 [dcl.stc]
2199 A storage-class-specifier shall not be specified in an
2200 explicit specialization...
2202 The parser rejects these, so unless action is taken here,
2203 explicit function specializations will always appear with
2204 global linkage.
2206 The action recommended by the C++ CWG in response to C++
2207 defect report 605 is to make the storage class and linkage
2208 of the explicit specialization match the templated function:
2210 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2212 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2214 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2215 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2217 /* This specialization has the same linkage and visibility as
2218 the function template it specializes. */
2219 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2220 if (! TREE_PUBLIC (decl))
2222 DECL_INTERFACE_KNOWN (decl) = 1;
2223 DECL_NOT_REALLY_EXTERN (decl) = 1;
2225 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2226 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2228 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2229 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2233 /* If DECL is a friend declaration, declared using an
2234 unqualified name, the namespace associated with DECL may
2235 have been set incorrectly. For example, in:
2237 template <typename T> void f(T);
2238 namespace N {
2239 struct S { friend void f<int>(int); }
2242 we will have set the DECL_CONTEXT for the friend
2243 declaration to N, rather than to the global namespace. */
2244 if (DECL_NAMESPACE_SCOPE_P (decl))
2245 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2247 if (is_friend && !have_def)
2248 /* This is not really a declaration of a specialization.
2249 It's just the name of an instantiation. But, it's not
2250 a request for an instantiation, either. */
2251 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2252 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2253 /* This is indeed a specialization. In case of constructors
2254 and destructors, we need in-charge and not-in-charge
2255 versions in V3 ABI. */
2256 clone_function_decl (decl, /*update_method_vec_p=*/0);
2258 /* Register this specialization so that we can find it
2259 again. */
2260 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2264 return decl;
2267 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2268 parameters. These are represented in the same format used for
2269 DECL_TEMPLATE_PARMS. */
2272 comp_template_parms (const_tree parms1, const_tree parms2)
2274 const_tree p1;
2275 const_tree p2;
2277 if (parms1 == parms2)
2278 return 1;
2280 for (p1 = parms1, p2 = parms2;
2281 p1 != NULL_TREE && p2 != NULL_TREE;
2282 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2284 tree t1 = TREE_VALUE (p1);
2285 tree t2 = TREE_VALUE (p2);
2286 int i;
2288 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2289 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2291 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2292 return 0;
2294 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2296 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2297 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2299 /* If either of the template parameters are invalid, assume
2300 they match for the sake of error recovery. */
2301 if (parm1 == error_mark_node || parm2 == error_mark_node)
2302 return 1;
2304 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2305 return 0;
2307 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2308 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2309 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2310 continue;
2311 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2312 return 0;
2316 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2317 /* One set of parameters has more parameters lists than the
2318 other. */
2319 return 0;
2321 return 1;
2324 /* Determine whether PARM is a parameter pack. */
2325 bool
2326 template_parameter_pack_p (const_tree parm)
2328 /* Determine if we have a non-type template parameter pack. */
2329 if (TREE_CODE (parm) == PARM_DECL)
2330 return (DECL_TEMPLATE_PARM_P (parm)
2331 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2333 /* If this is a list of template parameters, we could get a
2334 TYPE_DECL or a TEMPLATE_DECL. */
2335 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2336 parm = TREE_TYPE (parm);
2338 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2339 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2340 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2343 /* Determine whether ARGS describes a variadic template args list,
2344 i.e., one that is terminated by a template argument pack. */
2345 static bool
2346 template_args_variadic_p (tree args)
2348 int nargs;
2349 tree last_parm;
2351 if (args == NULL_TREE)
2352 return false;
2354 args = INNERMOST_TEMPLATE_ARGS (args);
2355 nargs = TREE_VEC_LENGTH (args);
2357 if (nargs == 0)
2358 return false;
2360 last_parm = TREE_VEC_ELT (args, nargs - 1);
2362 return ARGUMENT_PACK_P (last_parm);
2365 /* Generate a new name for the parameter pack name NAME (an
2366 IDENTIFIER_NODE) that incorporates its */
2367 static tree
2368 make_ith_pack_parameter_name (tree name, int i)
2370 /* Munge the name to include the parameter index. */
2371 char numbuf[128];
2372 char* newname;
2374 sprintf(numbuf, "%i", i);
2375 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2376 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2377 return get_identifier (newname);
2380 /* Structure used to track the progress of find_parameter_pack_r. */
2381 struct find_parameter_pack_data
2383 tree* parameter_packs;
2384 struct pointer_set_t *visited;
2387 /* Identifiers all of the argument packs that occur in a template
2388 argument and appends them to the TREE_LIST inside DATA, which is a
2389 find_parameter_pack_Data structure. This is a subroutine of
2390 make_pack_expansion and uses_parameter_packs. */
2391 static tree
2392 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2394 tree t = *tp;
2395 struct find_parameter_pack_data* ppd =
2396 (struct find_parameter_pack_data*)data;
2398 if (TYPE_P (t))
2400 tree context = TYPE_CONTEXT (t);
2401 cp_walk_tree (&context, &find_parameter_packs_r, ppd, ppd->visited);
2404 /* This switch statement will return immediately if we don't find a
2405 parameter pack. */
2406 switch (TREE_CODE (t))
2408 case TEMPLATE_PARM_INDEX:
2409 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2410 break;
2411 return NULL_TREE;
2413 case BOUND_TEMPLATE_TEMPLATE_PARM:
2414 /* Check the template arguments. */
2415 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2416 ppd->visited);
2418 /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */
2419 t = TYPE_TI_TEMPLATE (t);
2420 if (DECL_P (t) && TREE_TYPE (t))
2421 t = TREE_TYPE (t);
2422 *walk_subtrees = 0;
2424 /* Fall through. */
2426 case TEMPLATE_TYPE_PARM:
2427 case TEMPLATE_TEMPLATE_PARM:
2428 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2429 break;
2430 return NULL_TREE;
2432 case PARM_DECL:
2433 if (FUNCTION_PARAMETER_PACK_P (t))
2435 /* We don't want to walk into the type of a PARM_DECL,
2436 because we don't want to see the type parameter pack.*/
2437 *walk_subtrees = 0;
2438 break;
2440 return NULL_TREE;
2442 case RECORD_TYPE:
2443 if (TYPE_PTRMEMFUNC_P (t))
2444 return NULL_TREE;
2445 /* Fall through. */
2447 case UNION_TYPE:
2448 case ENUMERAL_TYPE:
2449 if (TYPE_TEMPLATE_INFO (t))
2451 tree args = TREE_VALUE (TYPE_TEMPLATE_INFO (t));
2452 cp_walk_tree (&args, &find_parameter_packs_r, ppd, ppd->visited);
2455 *walk_subtrees = 0;
2456 return NULL_TREE;
2458 case TEMPLATE_DECL:
2459 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
2460 && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2461 break;
2463 *walk_subtrees = 0;
2464 return NULL_TREE;
2466 case TYPE_PACK_EXPANSION:
2467 case EXPR_PACK_EXPANSION:
2468 *walk_subtrees = 0;
2469 return NULL_TREE;
2471 case INTEGER_TYPE:
2472 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2473 ppd, ppd->visited);
2474 *walk_subtrees = 0;
2475 return NULL_TREE;
2477 default:
2478 return NULL_TREE;
2481 /* Add this parameter pack to the list. */
2482 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2484 return NULL_TREE;
2487 /* Determines if the expression or type T uses any parameter packs. */
2488 bool
2489 uses_parameter_packs (tree t)
2491 tree parameter_packs = NULL_TREE;
2492 struct find_parameter_pack_data ppd;
2493 ppd.parameter_packs = &parameter_packs;
2494 ppd.visited = pointer_set_create ();
2495 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2496 pointer_set_destroy (ppd.visited);
2497 return parameter_packs != NULL_TREE;
2500 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2501 representation a base-class initializer into a parameter pack
2502 expansion. If all goes well, the resulting node will be an
2503 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2504 respectively. */
2505 tree
2506 make_pack_expansion (tree arg)
2508 tree result;
2509 tree parameter_packs = NULL_TREE;
2510 bool for_types = false;
2511 struct find_parameter_pack_data ppd;
2513 if (!arg || arg == error_mark_node)
2514 return arg;
2516 if (TREE_CODE (arg) == TREE_LIST)
2518 /* The only time we will see a TREE_LIST here is for a base
2519 class initializer. In this case, the TREE_PURPOSE will be a
2520 _TYPE node (representing the base class expansion we're
2521 initializing) and the TREE_VALUE will be a TREE_LIST
2522 containing the initialization arguments.
2524 The resulting expansion looks somewhat different from most
2525 expansions. Rather than returning just one _EXPANSION, we
2526 return a TREE_LIST whose TREE_PURPOSE is a
2527 TYPE_PACK_EXPANSION containing the bases that will be
2528 initialized. The TREE_VALUE will be identical to the
2529 original TREE_VALUE, which is a list of arguments that will
2530 be passed to each base. We do not introduce any new pack
2531 expansion nodes into the TREE_VALUE (although it is possible
2532 that some already exist), because the TREE_PURPOSE and
2533 TREE_VALUE all need to be expanded together with the same
2534 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2535 resulting TREE_PURPOSE will mention the parameter packs in
2536 both the bases and the arguments to the bases. */
2537 tree purpose;
2538 tree value;
2539 tree parameter_packs = NULL_TREE;
2541 /* Determine which parameter packs will be used by the base
2542 class expansion. */
2543 ppd.visited = pointer_set_create ();
2544 ppd.parameter_packs = &parameter_packs;
2545 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2546 &ppd, ppd.visited);
2548 if (parameter_packs == NULL_TREE)
2550 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2551 pointer_set_destroy (ppd.visited);
2552 return error_mark_node;
2555 if (TREE_VALUE (arg) != void_type_node)
2557 /* Collect the sets of parameter packs used in each of the
2558 initialization arguments. */
2559 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2561 /* Determine which parameter packs will be expanded in this
2562 argument. */
2563 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2564 &ppd, ppd.visited);
2568 pointer_set_destroy (ppd.visited);
2570 /* Create the pack expansion type for the base type. */
2571 purpose = make_node (TYPE_PACK_EXPANSION);
2572 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2573 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2575 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2576 they will rarely be compared to anything. */
2577 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2579 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2582 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2583 for_types = true;
2585 /* Build the PACK_EXPANSION_* node. */
2586 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2587 SET_PACK_EXPANSION_PATTERN (result, arg);
2588 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2590 /* Propagate type and const-expression information. */
2591 TREE_TYPE (result) = TREE_TYPE (arg);
2592 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2594 else
2595 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2596 they will rarely be compared to anything. */
2597 SET_TYPE_STRUCTURAL_EQUALITY (result);
2599 /* Determine which parameter packs will be expanded. */
2600 ppd.parameter_packs = &parameter_packs;
2601 ppd.visited = pointer_set_create ();
2602 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2603 pointer_set_destroy (ppd.visited);
2605 /* Make sure we found some parameter packs. */
2606 if (parameter_packs == NULL_TREE)
2608 if (TYPE_P (arg))
2609 error ("expansion pattern %<%T%> contains no argument packs", arg);
2610 else
2611 error ("expansion pattern %<%E%> contains no argument packs", arg);
2612 return error_mark_node;
2614 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2616 return result;
2619 /* Checks T for any "bare" parameter packs, which have not yet been
2620 expanded, and issues an error if any are found. This operation can
2621 only be done on full expressions or types (e.g., an expression
2622 statement, "if" condition, etc.), because we could have expressions like:
2624 foo(f(g(h(args)))...)
2626 where "args" is a parameter pack. check_for_bare_parameter_packs
2627 should not be called for the subexpressions args, h(args),
2628 g(h(args)), or f(g(h(args))), because we would produce erroneous
2629 error messages.
2631 Returns TRUE if there were no bare parameter packs, returns FALSE
2632 (and emits an error) if there were bare parameter packs.*/
2633 bool
2634 check_for_bare_parameter_packs (tree t)
2636 tree parameter_packs = NULL_TREE;
2637 struct find_parameter_pack_data ppd;
2639 if (!processing_template_decl || !t || t == error_mark_node)
2640 return true;
2642 if (TREE_CODE (t) == TYPE_DECL)
2643 t = TREE_TYPE (t);
2645 ppd.parameter_packs = &parameter_packs;
2646 ppd.visited = pointer_set_create ();
2647 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2648 pointer_set_destroy (ppd.visited);
2650 if (parameter_packs)
2652 error ("parameter packs not expanded with `...':");
2653 while (parameter_packs)
2655 tree pack = TREE_VALUE (parameter_packs);
2656 tree name = NULL_TREE;
2658 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2659 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2660 name = TYPE_NAME (pack);
2661 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2662 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2663 else
2664 name = DECL_NAME (pack);
2665 inform (" %qD", name);
2667 parameter_packs = TREE_CHAIN (parameter_packs);
2670 return false;
2673 return true;
2676 /* Expand any parameter packs that occur in the template arguments in
2677 ARGS. */
2678 tree
2679 expand_template_argument_pack (tree args)
2681 tree result_args = NULL_TREE;
2682 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2683 int num_result_args = -1;
2685 /* First, determine if we need to expand anything, and the number of
2686 slots we'll need. */
2687 for (in_arg = 0; in_arg < nargs; ++in_arg)
2689 tree arg = TREE_VEC_ELT (args, in_arg);
2690 if (ARGUMENT_PACK_P (arg))
2692 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2693 if (num_result_args < 0)
2694 num_result_args = in_arg + num_packed;
2695 else
2696 num_result_args += num_packed;
2698 else
2700 if (num_result_args >= 0)
2701 num_result_args++;
2705 /* If no expansion is necessary, we're done. */
2706 if (num_result_args < 0)
2707 return args;
2709 /* Expand arguments. */
2710 result_args = make_tree_vec (num_result_args);
2711 for (in_arg = 0; in_arg < nargs; ++in_arg)
2713 tree arg = TREE_VEC_ELT (args, in_arg);
2714 if (ARGUMENT_PACK_P (arg))
2716 tree packed = ARGUMENT_PACK_ARGS (arg);
2717 int i, num_packed = TREE_VEC_LENGTH (packed);
2718 for (i = 0; i < num_packed; ++i, ++out_arg)
2719 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2721 else
2723 TREE_VEC_ELT (result_args, out_arg) = arg;
2724 ++out_arg;
2728 return result_args;
2731 /* Complain if DECL shadows a template parameter.
2733 [temp.local]: A template-parameter shall not be redeclared within its
2734 scope (including nested scopes). */
2736 void
2737 check_template_shadow (tree decl)
2739 tree olddecl;
2741 /* If we're not in a template, we can't possibly shadow a template
2742 parameter. */
2743 if (!current_template_parms)
2744 return;
2746 /* Figure out what we're shadowing. */
2747 if (TREE_CODE (decl) == OVERLOAD)
2748 decl = OVL_CURRENT (decl);
2749 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2751 /* If there's no previous binding for this name, we're not shadowing
2752 anything, let alone a template parameter. */
2753 if (!olddecl)
2754 return;
2756 /* If we're not shadowing a template parameter, we're done. Note
2757 that OLDDECL might be an OVERLOAD (or perhaps even an
2758 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2759 node. */
2760 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2761 return;
2763 /* We check for decl != olddecl to avoid bogus errors for using a
2764 name inside a class. We check TPFI to avoid duplicate errors for
2765 inline member templates. */
2766 if (decl == olddecl
2767 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2768 return;
2770 error ("declaration of %q+#D", decl);
2771 error (" shadows template parm %q+#D", olddecl);
2774 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2775 ORIG_LEVEL, DECL, and TYPE. */
2777 static tree
2778 build_template_parm_index (int index,
2779 int level,
2780 int orig_level,
2781 tree decl,
2782 tree type)
2784 tree t = make_node (TEMPLATE_PARM_INDEX);
2785 TEMPLATE_PARM_IDX (t) = index;
2786 TEMPLATE_PARM_LEVEL (t) = level;
2787 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2788 TEMPLATE_PARM_DECL (t) = decl;
2789 TREE_TYPE (t) = type;
2790 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2791 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2792 TREE_READONLY (t) = TREE_READONLY (decl);
2794 return t;
2797 /* Find the canonical type parameter for the given template type
2798 parameter. Returns the canonical type parameter, which may be TYPE
2799 if no such parameter existed. */
2800 static tree
2801 canonical_type_parameter (tree type)
2803 tree list;
2804 int idx = TEMPLATE_TYPE_IDX (type);
2805 if (!canonical_template_parms)
2806 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2808 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2809 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2811 list = VEC_index (tree, canonical_template_parms, idx);
2812 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2813 list = TREE_CHAIN (list);
2815 if (list)
2816 return TREE_VALUE (list);
2817 else
2819 VEC_replace(tree, canonical_template_parms, idx,
2820 tree_cons (NULL_TREE, type,
2821 VEC_index (tree, canonical_template_parms, idx)));
2822 return type;
2826 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2827 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2828 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2829 new one is created. */
2831 static tree
2832 reduce_template_parm_level (tree index, tree type, int levels)
2834 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2835 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2836 != TEMPLATE_PARM_LEVEL (index) - levels))
2838 tree orig_decl = TEMPLATE_PARM_DECL (index);
2839 tree decl, t;
2841 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2842 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2843 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2844 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2845 DECL_ARTIFICIAL (decl) = 1;
2846 SET_DECL_TEMPLATE_PARM_P (decl);
2848 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2849 TEMPLATE_PARM_LEVEL (index) - levels,
2850 TEMPLATE_PARM_ORIG_LEVEL (index),
2851 decl, type);
2852 TEMPLATE_PARM_DESCENDANTS (index) = t;
2853 TEMPLATE_PARM_PARAMETER_PACK (t)
2854 = TEMPLATE_PARM_PARAMETER_PACK (index);
2856 /* Template template parameters need this. */
2857 if (TREE_CODE (decl) != CONST_DECL)
2858 DECL_TEMPLATE_PARMS (decl)
2859 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2862 return TEMPLATE_PARM_DESCENDANTS (index);
2865 /* Process information from new template parameter PARM and append it to the
2866 LIST being built. This new parameter is a non-type parameter iff
2867 IS_NON_TYPE is true. This new parameter is a parameter
2868 pack iff IS_PARAMETER_PACK is true. */
2870 tree
2871 process_template_parm (tree list, tree parm, bool is_non_type,
2872 bool is_parameter_pack)
2874 tree decl = 0;
2875 tree defval;
2876 tree err_parm_list;
2877 int idx = 0;
2879 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2880 defval = TREE_PURPOSE (parm);
2882 if (list)
2884 tree p = tree_last (list);
2886 if (p && TREE_VALUE (p) != error_mark_node)
2888 p = TREE_VALUE (p);
2889 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2890 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2891 else
2892 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2895 ++idx;
2897 else
2898 idx = 0;
2900 if (is_non_type)
2902 parm = TREE_VALUE (parm);
2904 SET_DECL_TEMPLATE_PARM_P (parm);
2906 if (TREE_TYPE (parm) == error_mark_node)
2908 err_parm_list = build_tree_list (defval, parm);
2909 TREE_VALUE (err_parm_list) = error_mark_node;
2910 return chainon (list, err_parm_list);
2912 else
2914 /* [temp.param]
2916 The top-level cv-qualifiers on the template-parameter are
2917 ignored when determining its type. */
2918 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2919 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2921 err_parm_list = build_tree_list (defval, parm);
2922 TREE_VALUE (err_parm_list) = error_mark_node;
2923 return chainon (list, err_parm_list);
2926 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
2928 /* This template parameter is not a parameter pack, but it
2929 should be. Complain about "bare" parameter packs. */
2930 check_for_bare_parameter_packs (TREE_TYPE (parm));
2932 /* Recover by calling this a parameter pack. */
2933 is_parameter_pack = true;
2937 /* A template parameter is not modifiable. */
2938 TREE_CONSTANT (parm) = 1;
2939 TREE_INVARIANT (parm) = 1;
2940 TREE_READONLY (parm) = 1;
2941 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2942 TREE_CONSTANT (decl) = 1;
2943 TREE_INVARIANT (decl) = 1;
2944 TREE_READONLY (decl) = 1;
2945 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2946 = build_template_parm_index (idx, processing_template_decl,
2947 processing_template_decl,
2948 decl, TREE_TYPE (parm));
2950 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
2951 = is_parameter_pack;
2953 else
2955 tree t;
2956 parm = TREE_VALUE (TREE_VALUE (parm));
2958 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2960 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2961 /* This is for distinguishing between real templates and template
2962 template parameters */
2963 TREE_TYPE (parm) = t;
2964 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2965 decl = parm;
2967 else
2969 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2970 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2971 decl = build_decl (TYPE_DECL, parm, t);
2974 TYPE_NAME (t) = decl;
2975 TYPE_STUB_DECL (t) = decl;
2976 parm = decl;
2977 TEMPLATE_TYPE_PARM_INDEX (t)
2978 = build_template_parm_index (idx, processing_template_decl,
2979 processing_template_decl,
2980 decl, TREE_TYPE (parm));
2981 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
2982 TYPE_CANONICAL (t) = canonical_type_parameter (t);
2984 DECL_ARTIFICIAL (decl) = 1;
2985 SET_DECL_TEMPLATE_PARM_P (decl);
2986 pushdecl (decl);
2987 parm = build_tree_list (defval, parm);
2988 return chainon (list, parm);
2991 /* The end of a template parameter list has been reached. Process the
2992 tree list into a parameter vector, converting each parameter into a more
2993 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2994 as PARM_DECLs. */
2996 tree
2997 end_template_parm_list (tree parms)
2999 int nparms;
3000 tree parm, next;
3001 tree saved_parmlist = make_tree_vec (list_length (parms));
3003 current_template_parms
3004 = tree_cons (size_int (processing_template_decl),
3005 saved_parmlist, current_template_parms);
3007 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3009 next = TREE_CHAIN (parm);
3010 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3011 TREE_CHAIN (parm) = NULL_TREE;
3014 --processing_template_parmlist;
3016 return saved_parmlist;
3019 /* end_template_decl is called after a template declaration is seen. */
3021 void
3022 end_template_decl (void)
3024 reset_specialization ();
3026 if (! processing_template_decl)
3027 return;
3029 /* This matches the pushlevel in begin_template_parm_list. */
3030 finish_scope ();
3032 --processing_template_decl;
3033 current_template_parms = TREE_CHAIN (current_template_parms);
3036 /* Within the declaration of a template, return all levels of template
3037 parameters that apply. The template parameters are represented as
3038 a TREE_VEC, in the form documented in cp-tree.h for template
3039 arguments. */
3041 static tree
3042 current_template_args (void)
3044 tree header;
3045 tree args = NULL_TREE;
3046 int length = TMPL_PARMS_DEPTH (current_template_parms);
3047 int l = length;
3049 /* If there is only one level of template parameters, we do not
3050 create a TREE_VEC of TREE_VECs. Instead, we return a single
3051 TREE_VEC containing the arguments. */
3052 if (length > 1)
3053 args = make_tree_vec (length);
3055 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3057 tree a = copy_node (TREE_VALUE (header));
3058 int i;
3060 TREE_TYPE (a) = NULL_TREE;
3061 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3063 tree t = TREE_VEC_ELT (a, i);
3065 /* T will be a list if we are called from within a
3066 begin/end_template_parm_list pair, but a vector directly
3067 if within a begin/end_member_template_processing pair. */
3068 if (TREE_CODE (t) == TREE_LIST)
3070 t = TREE_VALUE (t);
3072 if (t != error_mark_node)
3074 if (TREE_CODE (t) == TYPE_DECL
3075 || TREE_CODE (t) == TEMPLATE_DECL)
3077 t = TREE_TYPE (t);
3079 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3081 /* Turn this argument into a TYPE_ARGUMENT_PACK
3082 with a single element, which expands T. */
3083 tree vec = make_tree_vec (1);
3084 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3086 t = make_node (TYPE_ARGUMENT_PACK);
3087 SET_ARGUMENT_PACK_ARGS (t, vec);
3090 else
3092 t = DECL_INITIAL (t);
3094 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3096 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3097 with a single element, which expands T. */
3098 tree vec = make_tree_vec (1);
3099 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3100 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3102 t = make_node (NONTYPE_ARGUMENT_PACK);
3103 SET_ARGUMENT_PACK_ARGS (t, vec);
3104 TREE_TYPE (t) = type;
3108 TREE_VEC_ELT (a, i) = t;
3112 if (length > 1)
3113 TREE_VEC_ELT (args, --l) = a;
3114 else
3115 args = a;
3118 return args;
3121 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3122 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3123 a member template. Used by push_template_decl below. */
3125 static tree
3126 build_template_decl (tree decl, tree parms, bool member_template_p)
3128 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3129 DECL_TEMPLATE_PARMS (tmpl) = parms;
3130 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3131 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3132 if (DECL_LANG_SPECIFIC (decl))
3134 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3135 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3136 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3137 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3138 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3139 if (DECL_OVERLOADED_OPERATOR_P (decl))
3140 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3141 DECL_OVERLOADED_OPERATOR_P (decl));
3144 return tmpl;
3147 struct template_parm_data
3149 /* The level of the template parameters we are currently
3150 processing. */
3151 int level;
3153 /* The index of the specialization argument we are currently
3154 processing. */
3155 int current_arg;
3157 /* An array whose size is the number of template parameters. The
3158 elements are nonzero if the parameter has been used in any one
3159 of the arguments processed so far. */
3160 int* parms;
3162 /* An array whose size is the number of template arguments. The
3163 elements are nonzero if the argument makes use of template
3164 parameters of this level. */
3165 int* arg_uses_template_parms;
3168 /* Subroutine of push_template_decl used to see if each template
3169 parameter in a partial specialization is used in the explicit
3170 argument list. If T is of the LEVEL given in DATA (which is
3171 treated as a template_parm_data*), then DATA->PARMS is marked
3172 appropriately. */
3174 static int
3175 mark_template_parm (tree t, void* data)
3177 int level;
3178 int idx;
3179 struct template_parm_data* tpd = (struct template_parm_data*) data;
3181 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3183 level = TEMPLATE_PARM_LEVEL (t);
3184 idx = TEMPLATE_PARM_IDX (t);
3186 else
3188 level = TEMPLATE_TYPE_LEVEL (t);
3189 idx = TEMPLATE_TYPE_IDX (t);
3192 if (level == tpd->level)
3194 tpd->parms[idx] = 1;
3195 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3198 /* Return zero so that for_each_template_parm will continue the
3199 traversal of the tree; we want to mark *every* template parm. */
3200 return 0;
3203 /* Process the partial specialization DECL. */
3205 static tree
3206 process_partial_specialization (tree decl)
3208 tree type = TREE_TYPE (decl);
3209 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3210 tree specargs = CLASSTYPE_TI_ARGS (type);
3211 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3212 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3213 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3214 int nargs = TREE_VEC_LENGTH (inner_args);
3215 int ntparms = TREE_VEC_LENGTH (inner_parms);
3216 int i;
3217 int did_error_intro = 0;
3218 struct template_parm_data tpd;
3219 struct template_parm_data tpd2;
3221 /* We check that each of the template parameters given in the
3222 partial specialization is used in the argument list to the
3223 specialization. For example:
3225 template <class T> struct S;
3226 template <class T> struct S<T*>;
3228 The second declaration is OK because `T*' uses the template
3229 parameter T, whereas
3231 template <class T> struct S<int>;
3233 is no good. Even trickier is:
3235 template <class T>
3236 struct S1
3238 template <class U>
3239 struct S2;
3240 template <class U>
3241 struct S2<T>;
3244 The S2<T> declaration is actually invalid; it is a
3245 full-specialization. Of course,
3247 template <class U>
3248 struct S2<T (*)(U)>;
3250 or some such would have been OK. */
3251 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3252 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3253 memset (tpd.parms, 0, sizeof (int) * ntparms);
3255 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3256 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3257 for (i = 0; i < nargs; ++i)
3259 tpd.current_arg = i;
3260 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3261 &mark_template_parm,
3262 &tpd,
3263 NULL);
3265 for (i = 0; i < ntparms; ++i)
3266 if (tpd.parms[i] == 0)
3268 /* One of the template parms was not used in the
3269 specialization. */
3270 if (!did_error_intro)
3272 error ("template parameters not used in partial specialization:");
3273 did_error_intro = 1;
3276 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3279 /* [temp.class.spec]
3281 The argument list of the specialization shall not be identical to
3282 the implicit argument list of the primary template. */
3283 if (comp_template_args
3284 (inner_args,
3285 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3286 (maintmpl)))))
3287 error ("partial specialization %qT does not specialize any template arguments", type);
3289 /* [temp.class.spec]
3291 A partially specialized non-type argument expression shall not
3292 involve template parameters of the partial specialization except
3293 when the argument expression is a simple identifier.
3295 The type of a template parameter corresponding to a specialized
3296 non-type argument shall not be dependent on a parameter of the
3297 specialization.
3299 Also, we verify that pack expansions only occur at the
3300 end of the argument list. */
3301 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3302 tpd2.parms = 0;
3303 for (i = 0; i < nargs; ++i)
3305 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3306 tree arg = TREE_VEC_ELT (inner_args, i);
3307 tree packed_args = NULL_TREE;
3308 int j, len = 1;
3310 if (ARGUMENT_PACK_P (arg))
3312 /* Extract the arguments from the argument pack. We'll be
3313 iterating over these in the following loop. */
3314 packed_args = ARGUMENT_PACK_ARGS (arg);
3315 len = TREE_VEC_LENGTH (packed_args);
3318 for (j = 0; j < len; j++)
3320 if (packed_args)
3321 /* Get the Jth argument in the parameter pack. */
3322 arg = TREE_VEC_ELT (packed_args, j);
3324 if (PACK_EXPANSION_P (arg))
3326 /* Pack expansions must come at the end of the
3327 argument list. */
3328 if ((packed_args && j < len - 1)
3329 || (!packed_args && i < nargs - 1))
3331 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3332 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3333 else
3334 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3338 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3339 /* We only care about the pattern. */
3340 arg = PACK_EXPANSION_PATTERN (arg);
3342 if (/* These first two lines are the `non-type' bit. */
3343 !TYPE_P (arg)
3344 && TREE_CODE (arg) != TEMPLATE_DECL
3345 /* This next line is the `argument expression is not just a
3346 simple identifier' condition and also the `specialized
3347 non-type argument' bit. */
3348 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3350 if ((!packed_args && tpd.arg_uses_template_parms[i])
3351 || (packed_args && uses_template_parms (arg)))
3352 error ("template argument %qE involves template parameter(s)",
3353 arg);
3354 else
3356 /* Look at the corresponding template parameter,
3357 marking which template parameters its type depends
3358 upon. */
3359 tree type = TREE_TYPE (parm);
3361 if (!tpd2.parms)
3363 /* We haven't yet initialized TPD2. Do so now. */
3364 tpd2.arg_uses_template_parms
3365 = (int *) alloca (sizeof (int) * nargs);
3366 /* The number of parameters here is the number in the
3367 main template, which, as checked in the assertion
3368 above, is NARGS. */
3369 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3370 tpd2.level =
3371 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3374 /* Mark the template parameters. But this time, we're
3375 looking for the template parameters of the main
3376 template, not in the specialization. */
3377 tpd2.current_arg = i;
3378 tpd2.arg_uses_template_parms[i] = 0;
3379 memset (tpd2.parms, 0, sizeof (int) * nargs);
3380 for_each_template_parm (type,
3381 &mark_template_parm,
3382 &tpd2,
3383 NULL);
3385 if (tpd2.arg_uses_template_parms [i])
3387 /* The type depended on some template parameters.
3388 If they are fully specialized in the
3389 specialization, that's OK. */
3390 int j;
3391 for (j = 0; j < nargs; ++j)
3392 if (tpd2.parms[j] != 0
3393 && tpd.arg_uses_template_parms [j])
3395 error ("type %qT of template argument %qE depends "
3396 "on template parameter(s)",
3397 type,
3398 arg);
3399 break;
3407 if (retrieve_specialization (maintmpl, specargs,
3408 /*class_specializations_p=*/true))
3409 /* We've already got this specialization. */
3410 return decl;
3412 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3413 = tree_cons (specargs, inner_parms,
3414 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3415 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3416 return decl;
3419 /* Check that a template declaration's use of default arguments is not
3420 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
3421 nonzero if DECL is the thing declared by a primary template.
3422 IS_PARTIAL is nonzero if DECL is a partial specialization.
3425 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3426 declaration (but not a definition); 1 indicates a declaration, 2
3427 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3428 emitted for extraneous default arguments.
3430 Returns TRUE if there were no errors found, FALSE otherwise. */
3432 bool
3433 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3434 int is_partial, int is_friend_decl)
3436 const char *msg;
3437 int last_level_to_check;
3438 tree parm_level;
3439 bool no_errors = true;
3441 /* [temp.param]
3443 A default template-argument shall not be specified in a
3444 function template declaration or a function template definition, nor
3445 in the template-parameter-list of the definition of a member of a
3446 class template. */
3448 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3449 /* You can't have a function template declaration in a local
3450 scope, nor you can you define a member of a class template in a
3451 local scope. */
3452 return true;
3454 if (current_class_type
3455 && !TYPE_BEING_DEFINED (current_class_type)
3456 && DECL_LANG_SPECIFIC (decl)
3457 /* If this is either a friend defined in the scope of the class
3458 or a member function. */
3459 && (DECL_FUNCTION_MEMBER_P (decl)
3460 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3461 : DECL_FRIEND_CONTEXT (decl)
3462 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3463 : false)
3464 /* And, if it was a member function, it really was defined in
3465 the scope of the class. */
3466 && (!DECL_FUNCTION_MEMBER_P (decl)
3467 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3468 /* We already checked these parameters when the template was
3469 declared, so there's no need to do it again now. This function
3470 was defined in class scope, but we're processing it's body now
3471 that the class is complete. */
3472 return true;
3474 /* Core issue 226 (C++0x only): the following only applies to class
3475 templates. */
3476 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3478 /* [temp.param]
3480 If a template-parameter has a default template-argument, all
3481 subsequent template-parameters shall have a default
3482 template-argument supplied. */
3483 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3485 tree inner_parms = TREE_VALUE (parm_level);
3486 int ntparms = TREE_VEC_LENGTH (inner_parms);
3487 int seen_def_arg_p = 0;
3488 int i;
3490 for (i = 0; i < ntparms; ++i)
3492 tree parm = TREE_VEC_ELT (inner_parms, i);
3494 if (parm == error_mark_node)
3495 continue;
3497 if (TREE_PURPOSE (parm))
3498 seen_def_arg_p = 1;
3499 else if (seen_def_arg_p)
3501 error ("no default argument for %qD", TREE_VALUE (parm));
3502 /* For better subsequent error-recovery, we indicate that
3503 there should have been a default argument. */
3504 TREE_PURPOSE (parm) = error_mark_node;
3505 no_errors = false;
3511 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3512 || is_partial
3513 || !is_primary
3514 || is_friend_decl)
3515 /* For an ordinary class template, default template arguments are
3516 allowed at the innermost level, e.g.:
3517 template <class T = int>
3518 struct S {};
3519 but, in a partial specialization, they're not allowed even
3520 there, as we have in [temp.class.spec]:
3522 The template parameter list of a specialization shall not
3523 contain default template argument values.
3525 So, for a partial specialization, or for a function template
3526 (in C++98/C++03), we look at all of them. */
3528 else
3529 /* But, for a primary class template that is not a partial
3530 specialization we look at all template parameters except the
3531 innermost ones. */
3532 parms = TREE_CHAIN (parms);
3534 /* Figure out what error message to issue. */
3535 if (is_friend_decl == 2)
3536 msg = "default template arguments may not be used in function template friend re-declaration";
3537 else if (is_friend_decl)
3538 msg = "default template arguments may not be used in function template friend declarations";
3539 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3540 msg = "default template arguments may not be used in function templates";
3541 else if (is_partial)
3542 msg = "default template arguments may not be used in partial specializations";
3543 else
3544 msg = "default argument for template parameter for class enclosing %qD";
3546 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3547 /* If we're inside a class definition, there's no need to
3548 examine the parameters to the class itself. On the one
3549 hand, they will be checked when the class is defined, and,
3550 on the other, default arguments are valid in things like:
3551 template <class T = double>
3552 struct S { template <class U> void f(U); };
3553 Here the default argument for `S' has no bearing on the
3554 declaration of `f'. */
3555 last_level_to_check = template_class_depth (current_class_type) + 1;
3556 else
3557 /* Check everything. */
3558 last_level_to_check = 0;
3560 for (parm_level = parms;
3561 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3562 parm_level = TREE_CHAIN (parm_level))
3564 tree inner_parms = TREE_VALUE (parm_level);
3565 int i;
3566 int ntparms;
3568 ntparms = TREE_VEC_LENGTH (inner_parms);
3569 for (i = 0; i < ntparms; ++i)
3571 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3572 continue;
3574 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3576 if (msg)
3578 no_errors = false;
3579 if (is_friend_decl == 2)
3580 return no_errors;
3582 error (msg, decl);
3583 msg = 0;
3586 /* Clear out the default argument so that we are not
3587 confused later. */
3588 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3592 /* At this point, if we're still interested in issuing messages,
3593 they must apply to classes surrounding the object declared. */
3594 if (msg)
3595 msg = "default argument for template parameter for class enclosing %qD";
3598 return no_errors;
3601 /* Worker for push_template_decl_real, called via
3602 for_each_template_parm. DATA is really an int, indicating the
3603 level of the parameters we are interested in. If T is a template
3604 parameter of that level, return nonzero. */
3606 static int
3607 template_parm_this_level_p (tree t, void* data)
3609 int this_level = *(int *)data;
3610 int level;
3612 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3613 level = TEMPLATE_PARM_LEVEL (t);
3614 else
3615 level = TEMPLATE_TYPE_LEVEL (t);
3616 return level == this_level;
3619 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3620 parameters given by current_template_args, or reuses a
3621 previously existing one, if appropriate. Returns the DECL, or an
3622 equivalent one, if it is replaced via a call to duplicate_decls.
3624 If IS_FRIEND is true, DECL is a friend declaration. */
3626 tree
3627 push_template_decl_real (tree decl, bool is_friend)
3629 tree tmpl;
3630 tree args;
3631 tree info;
3632 tree ctx;
3633 int primary;
3634 int is_partial;
3635 int new_template_p = 0;
3636 /* True if the template is a member template, in the sense of
3637 [temp.mem]. */
3638 bool member_template_p = false;
3640 if (decl == error_mark_node)
3641 return decl;
3643 /* See if this is a partial specialization. */
3644 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3645 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3646 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3648 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3649 is_friend = true;
3651 if (is_friend)
3652 /* For a friend, we want the context of the friend function, not
3653 the type of which it is a friend. */
3654 ctx = DECL_CONTEXT (decl);
3655 else if (CP_DECL_CONTEXT (decl)
3656 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3657 /* In the case of a virtual function, we want the class in which
3658 it is defined. */
3659 ctx = CP_DECL_CONTEXT (decl);
3660 else
3661 /* Otherwise, if we're currently defining some class, the DECL
3662 is assumed to be a member of the class. */
3663 ctx = current_scope ();
3665 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3666 ctx = NULL_TREE;
3668 if (!DECL_CONTEXT (decl))
3669 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3671 /* See if this is a primary template. */
3672 if (is_friend && ctx)
3673 /* A friend template that specifies a class context, i.e.
3674 template <typename T> friend void A<T>::f();
3675 is not primary. */
3676 primary = 0;
3677 else
3678 primary = template_parm_scope_p ();
3680 if (primary)
3682 if (DECL_CLASS_SCOPE_P (decl))
3683 member_template_p = true;
3684 if (TREE_CODE (decl) == TYPE_DECL
3685 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3686 error ("template class without a name");
3687 else if (TREE_CODE (decl) == FUNCTION_DECL)
3689 if (DECL_DESTRUCTOR_P (decl))
3691 /* [temp.mem]
3693 A destructor shall not be a member template. */
3694 error ("destructor %qD declared as member template", decl);
3695 return error_mark_node;
3697 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3698 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3699 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3700 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3701 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3702 == void_list_node)))
3704 /* [basic.stc.dynamic.allocation]
3706 An allocation function can be a function
3707 template. ... Template allocation functions shall
3708 have two or more parameters. */
3709 error ("invalid template declaration of %qD", decl);
3710 return error_mark_node;
3713 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3714 && CLASS_TYPE_P (TREE_TYPE (decl)))
3715 /* OK */;
3716 else
3718 error ("template declaration of %q#D", decl);
3719 return error_mark_node;
3723 /* Check to see that the rules regarding the use of default
3724 arguments are not being violated. */
3725 check_default_tmpl_args (decl, current_template_parms,
3726 primary, is_partial, /*is_friend_decl=*/0);
3728 /* Ensure that there are no parameter packs in the type of this
3729 declaration that have not been expanded. */
3730 if (TREE_CODE (decl) == FUNCTION_DECL)
3732 /* Check each of the arguments individually to see if there are
3733 any bare parameter packs. */
3734 tree type = TREE_TYPE (decl);
3735 tree arg = DECL_ARGUMENTS (decl);
3736 tree argtype = TYPE_ARG_TYPES (type);
3738 while (arg && argtype)
3740 if (!FUNCTION_PARAMETER_PACK_P (arg)
3741 && !check_for_bare_parameter_packs (TREE_TYPE (arg)))
3743 /* This is a PARM_DECL that contains unexpanded parameter
3744 packs. We have already complained about this in the
3745 check_for_bare_parameter_packs call, so just replace
3746 these types with ERROR_MARK_NODE. */
3747 TREE_TYPE (arg) = error_mark_node;
3748 TREE_VALUE (argtype) = error_mark_node;
3751 arg = TREE_CHAIN (arg);
3752 argtype = TREE_CHAIN (argtype);
3755 /* Check for bare parameter packs in the return type and the
3756 exception specifiers. */
3757 check_for_bare_parameter_packs (TREE_TYPE (type));
3758 check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type));
3760 else
3761 check_for_bare_parameter_packs (TREE_TYPE (decl));
3763 if (is_partial)
3764 return process_partial_specialization (decl);
3766 /* A primary class template can only have one parameter pack, at the
3767 end of the template parameter list. */
3768 if (primary && TREE_CODE (decl) == TYPE_DECL)
3770 tree inner_parms
3771 = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3772 int i, len = TREE_VEC_LENGTH (inner_parms);
3773 for (i = 0; i < len - 1; i++)
3775 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
3777 if (template_parameter_pack_p (parm))
3779 if (TREE_CODE (parm) == PARM_DECL)
3780 error ("parameter pack %qE must be at the end of the"
3781 " template parameter list", parm);
3782 else
3783 error ("parameter pack %qT must be at the end of the"
3784 " template parameter list", TREE_TYPE (parm));
3789 args = current_template_args ();
3791 if (!ctx
3792 || TREE_CODE (ctx) == FUNCTION_DECL
3793 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3794 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3796 if (DECL_LANG_SPECIFIC (decl)
3797 && DECL_TEMPLATE_INFO (decl)
3798 && DECL_TI_TEMPLATE (decl))
3799 tmpl = DECL_TI_TEMPLATE (decl);
3800 /* If DECL is a TYPE_DECL for a class-template, then there won't
3801 be DECL_LANG_SPECIFIC. The information equivalent to
3802 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3803 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3804 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3805 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3807 /* Since a template declaration already existed for this
3808 class-type, we must be redeclaring it here. Make sure
3809 that the redeclaration is valid. */
3810 redeclare_class_template (TREE_TYPE (decl),
3811 current_template_parms);
3812 /* We don't need to create a new TEMPLATE_DECL; just use the
3813 one we already had. */
3814 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3816 else
3818 tmpl = build_template_decl (decl, current_template_parms,
3819 member_template_p);
3820 new_template_p = 1;
3822 if (DECL_LANG_SPECIFIC (decl)
3823 && DECL_TEMPLATE_SPECIALIZATION (decl))
3825 /* A specialization of a member template of a template
3826 class. */
3827 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3828 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3829 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3833 else
3835 tree a, t, current, parms;
3836 int i;
3838 if (TREE_CODE (decl) == TYPE_DECL)
3840 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3841 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3842 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3843 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3844 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3845 else
3847 error ("%qD does not declare a template type", decl);
3848 return decl;
3851 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3853 error ("template definition of non-template %q#D", decl);
3854 return decl;
3856 else
3857 tmpl = DECL_TI_TEMPLATE (decl);
3859 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3860 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3861 && DECL_TEMPLATE_SPECIALIZATION (decl)
3862 && DECL_MEMBER_TEMPLATE_P (tmpl))
3864 tree new_tmpl;
3866 /* The declaration is a specialization of a member
3867 template, declared outside the class. Therefore, the
3868 innermost template arguments will be NULL, so we
3869 replace them with the arguments determined by the
3870 earlier call to check_explicit_specialization. */
3871 args = DECL_TI_ARGS (decl);
3873 new_tmpl
3874 = build_template_decl (decl, current_template_parms,
3875 member_template_p);
3876 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3877 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3878 DECL_TI_TEMPLATE (decl) = new_tmpl;
3879 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3880 DECL_TEMPLATE_INFO (new_tmpl)
3881 = tree_cons (tmpl, args, NULL_TREE);
3883 register_specialization (new_tmpl,
3884 most_general_template (tmpl),
3885 args,
3886 is_friend);
3887 return decl;
3890 /* Make sure the template headers we got make sense. */
3892 parms = DECL_TEMPLATE_PARMS (tmpl);
3893 i = TMPL_PARMS_DEPTH (parms);
3894 if (TMPL_ARGS_DEPTH (args) != i)
3896 error ("expected %d levels of template parms for %q#D, got %d",
3897 i, decl, TMPL_ARGS_DEPTH (args));
3899 else
3900 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3902 a = TMPL_ARGS_LEVEL (args, i);
3903 t = INNERMOST_TEMPLATE_PARMS (parms);
3905 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3907 if (current == decl)
3908 error ("got %d template parameters for %q#D",
3909 TREE_VEC_LENGTH (a), decl);
3910 else
3911 error ("got %d template parameters for %q#T",
3912 TREE_VEC_LENGTH (a), current);
3913 error (" but %d required", TREE_VEC_LENGTH (t));
3914 return error_mark_node;
3917 /* Perhaps we should also check that the parms are used in the
3918 appropriate qualifying scopes in the declarator? */
3920 if (current == decl)
3921 current = ctx;
3922 else
3923 current = (TYPE_P (current)
3924 ? TYPE_CONTEXT (current)
3925 : DECL_CONTEXT (current));
3929 DECL_TEMPLATE_RESULT (tmpl) = decl;
3930 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3932 /* Push template declarations for global functions and types. Note
3933 that we do not try to push a global template friend declared in a
3934 template class; such a thing may well depend on the template
3935 parameters of the class. */
3936 if (new_template_p && !ctx
3937 && !(is_friend && template_class_depth (current_class_type) > 0))
3939 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3940 if (tmpl == error_mark_node)
3941 return error_mark_node;
3943 /* Hide template friend classes that haven't been declared yet. */
3944 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3946 DECL_ANTICIPATED (tmpl) = 1;
3947 DECL_FRIEND_P (tmpl) = 1;
3951 if (primary)
3953 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3954 if (DECL_CONV_FN_P (tmpl))
3956 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3958 /* It is a conversion operator. See if the type converted to
3959 depends on innermost template operands. */
3961 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3962 depth))
3963 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3967 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3968 back to its most general template. If TMPL is a specialization,
3969 ARGS may only have the innermost set of arguments. Add the missing
3970 argument levels if necessary. */
3971 if (DECL_TEMPLATE_INFO (tmpl))
3972 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3974 info = tree_cons (tmpl, args, NULL_TREE);
3976 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3977 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3978 else if (DECL_LANG_SPECIFIC (decl))
3979 DECL_TEMPLATE_INFO (decl) = info;
3981 return DECL_TEMPLATE_RESULT (tmpl);
3984 tree
3985 push_template_decl (tree decl)
3987 return push_template_decl_real (decl, false);
3990 /* Called when a class template TYPE is redeclared with the indicated
3991 template PARMS, e.g.:
3993 template <class T> struct S;
3994 template <class T> struct S {}; */
3996 bool
3997 redeclare_class_template (tree type, tree parms)
3999 tree tmpl;
4000 tree tmpl_parms;
4001 int i;
4003 if (!TYPE_TEMPLATE_INFO (type))
4005 error ("%qT is not a template type", type);
4006 return false;
4009 tmpl = TYPE_TI_TEMPLATE (type);
4010 if (!PRIMARY_TEMPLATE_P (tmpl))
4011 /* The type is nested in some template class. Nothing to worry
4012 about here; there are no new template parameters for the nested
4013 type. */
4014 return true;
4016 if (!parms)
4018 error ("template specifiers not specified in declaration of %qD",
4019 tmpl);
4020 return false;
4023 parms = INNERMOST_TEMPLATE_PARMS (parms);
4024 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4026 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4028 error ("previous declaration %q+D", tmpl);
4029 error ("used %d template parameter(s) instead of %d",
4030 TREE_VEC_LENGTH (tmpl_parms),
4031 TREE_VEC_LENGTH (parms));
4032 return false;
4035 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4037 tree tmpl_parm;
4038 tree parm;
4039 tree tmpl_default;
4040 tree parm_default;
4042 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4043 || TREE_VEC_ELT (parms, i) == error_mark_node)
4044 continue;
4046 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4047 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4048 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4049 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4051 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4052 TEMPLATE_DECL. */
4053 if (tmpl_parm != error_mark_node
4054 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4055 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4056 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
4058 error ("template parameter %q+#D", tmpl_parm);
4059 error ("redeclared here as %q#D", parm);
4060 return false;
4063 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4065 /* We have in [temp.param]:
4067 A template-parameter may not be given default arguments
4068 by two different declarations in the same scope. */
4069 error ("redefinition of default argument for %q#D", parm);
4070 error ("%J original definition appeared here", tmpl_parm);
4071 return false;
4074 if (parm_default != NULL_TREE)
4075 /* Update the previous template parameters (which are the ones
4076 that will really count) with the new default value. */
4077 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4078 else if (tmpl_default != NULL_TREE)
4079 /* Update the new parameters, too; they'll be used as the
4080 parameters for any members. */
4081 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4084 return true;
4087 /* Simplify EXPR if it is a non-dependent expression. Returns the
4088 (possibly simplified) expression. */
4090 tree
4091 fold_non_dependent_expr (tree expr)
4093 if (expr == NULL_TREE)
4094 return NULL_TREE;
4096 /* If we're in a template, but EXPR isn't value dependent, simplify
4097 it. We're supposed to treat:
4099 template <typename T> void f(T[1 + 1]);
4100 template <typename T> void f(T[2]);
4102 as two declarations of the same function, for example. */
4103 if (processing_template_decl
4104 && !type_dependent_expression_p (expr)
4105 && !value_dependent_expression_p (expr))
4107 HOST_WIDE_INT saved_processing_template_decl;
4109 saved_processing_template_decl = processing_template_decl;
4110 processing_template_decl = 0;
4111 expr = tsubst_copy_and_build (expr,
4112 /*args=*/NULL_TREE,
4113 tf_error,
4114 /*in_decl=*/NULL_TREE,
4115 /*function_p=*/false,
4116 /*integral_constant_expression_p=*/true);
4117 processing_template_decl = saved_processing_template_decl;
4119 return expr;
4122 /* EXPR is an expression which is used in a constant-expression context.
4123 For instance, it could be a VAR_DECL with a constant initializer.
4124 Extract the innest constant expression.
4126 This is basically a more powerful version of
4127 integral_constant_value, which can be used also in templates where
4128 initializers can maintain a syntactic rather than semantic form
4129 (even if they are non-dependent, for access-checking purposes). */
4131 static tree
4132 fold_decl_constant_value (tree expr)
4134 tree const_expr = expr;
4137 expr = fold_non_dependent_expr (const_expr);
4138 const_expr = integral_constant_value (expr);
4140 while (expr != const_expr);
4142 return expr;
4145 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4146 must be a function or a pointer-to-function type, as specified
4147 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4148 and check that the resulting function has external linkage. */
4150 static tree
4151 convert_nontype_argument_function (tree type, tree expr)
4153 tree fns = expr;
4154 tree fn, fn_no_ptr;
4156 fn = instantiate_type (type, fns, tf_none);
4157 if (fn == error_mark_node)
4158 return error_mark_node;
4160 fn_no_ptr = fn;
4161 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4162 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4163 if (TREE_CODE (fn_no_ptr) == BASELINK)
4164 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4166 /* [temp.arg.nontype]/1
4168 A template-argument for a non-type, non-template template-parameter
4169 shall be one of:
4170 [...]
4171 -- the address of an object or function with external linkage. */
4172 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4174 error ("%qE is not a valid template argument for type %qT "
4175 "because function %qD has not external linkage",
4176 expr, type, fn_no_ptr);
4177 return NULL_TREE;
4180 return fn;
4183 /* Attempt to convert the non-type template parameter EXPR to the
4184 indicated TYPE. If the conversion is successful, return the
4185 converted value. If the conversion is unsuccessful, return
4186 NULL_TREE if we issued an error message, or error_mark_node if we
4187 did not. We issue error messages for out-and-out bad template
4188 parameters, but not simply because the conversion failed, since we
4189 might be just trying to do argument deduction. Both TYPE and EXPR
4190 must be non-dependent.
4192 The conversion follows the special rules described in
4193 [temp.arg.nontype], and it is much more strict than an implicit
4194 conversion.
4196 This function is called twice for each template argument (see
4197 lookup_template_class for a more accurate description of this
4198 problem). This means that we need to handle expressions which
4199 are not valid in a C++ source, but can be created from the
4200 first call (for instance, casts to perform conversions). These
4201 hacks can go away after we fix the double coercion problem. */
4203 static tree
4204 convert_nontype_argument (tree type, tree expr)
4206 tree expr_type;
4208 /* Detect immediately string literals as invalid non-type argument.
4209 This special-case is not needed for correctness (we would easily
4210 catch this later), but only to provide better diagnostic for this
4211 common user mistake. As suggested by DR 100, we do not mention
4212 linkage issues in the diagnostic as this is not the point. */
4213 if (TREE_CODE (expr) == STRING_CST)
4215 error ("%qE is not a valid template argument for type %qT "
4216 "because string literals can never be used in this context",
4217 expr, type);
4218 return NULL_TREE;
4221 /* If we are in a template, EXPR may be non-dependent, but still
4222 have a syntactic, rather than semantic, form. For example, EXPR
4223 might be a SCOPE_REF, rather than the VAR_DECL to which the
4224 SCOPE_REF refers. Preserving the qualifying scope is necessary
4225 so that access checking can be performed when the template is
4226 instantiated -- but here we need the resolved form so that we can
4227 convert the argument. */
4228 expr = fold_non_dependent_expr (expr);
4229 if (error_operand_p (expr))
4230 return error_mark_node;
4231 expr_type = TREE_TYPE (expr);
4233 /* HACK: Due to double coercion, we can get a
4234 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4235 which is the tree that we built on the first call (see
4236 below when coercing to reference to object or to reference to
4237 function). We just strip everything and get to the arg.
4238 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4239 for examples. */
4240 if (TREE_CODE (expr) == NOP_EXPR)
4242 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4244 /* ??? Maybe we could use convert_from_reference here, but we
4245 would need to relax its constraints because the NOP_EXPR
4246 could actually change the type to something more cv-qualified,
4247 and this is not folded by convert_from_reference. */
4248 tree addr = TREE_OPERAND (expr, 0);
4249 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4250 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4251 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4252 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4253 (TREE_TYPE (expr_type),
4254 TREE_TYPE (TREE_TYPE (addr))));
4256 expr = TREE_OPERAND (addr, 0);
4257 expr_type = TREE_TYPE (expr);
4260 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4261 parameter is a pointer to object, through decay and
4262 qualification conversion. Let's strip everything. */
4263 else if (TYPE_PTROBV_P (type))
4265 STRIP_NOPS (expr);
4266 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4267 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4268 /* Skip the ADDR_EXPR only if it is part of the decay for
4269 an array. Otherwise, it is part of the original argument
4270 in the source code. */
4271 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4272 expr = TREE_OPERAND (expr, 0);
4273 expr_type = TREE_TYPE (expr);
4277 /* [temp.arg.nontype]/5, bullet 1
4279 For a non-type template-parameter of integral or enumeration type,
4280 integral promotions (_conv.prom_) and integral conversions
4281 (_conv.integral_) are applied. */
4282 if (INTEGRAL_TYPE_P (type))
4284 if (!INTEGRAL_TYPE_P (expr_type))
4285 return error_mark_node;
4287 expr = fold_decl_constant_value (expr);
4288 /* Notice that there are constant expressions like '4 % 0' which
4289 do not fold into integer constants. */
4290 if (TREE_CODE (expr) != INTEGER_CST)
4292 error ("%qE is not a valid template argument for type %qT "
4293 "because it is a non-constant expression", expr, type);
4294 return NULL_TREE;
4297 /* At this point, an implicit conversion does what we want,
4298 because we already know that the expression is of integral
4299 type. */
4300 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4301 if (expr == error_mark_node)
4302 return error_mark_node;
4304 /* Conversion was allowed: fold it to a bare integer constant. */
4305 expr = fold (expr);
4307 /* [temp.arg.nontype]/5, bullet 2
4309 For a non-type template-parameter of type pointer to object,
4310 qualification conversions (_conv.qual_) and the array-to-pointer
4311 conversion (_conv.array_) are applied. */
4312 else if (TYPE_PTROBV_P (type))
4314 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4316 A template-argument for a non-type, non-template template-parameter
4317 shall be one of: [...]
4319 -- the name of a non-type template-parameter;
4320 -- the address of an object or function with external linkage, [...]
4321 expressed as "& id-expression" where the & is optional if the name
4322 refers to a function or array, or if the corresponding
4323 template-parameter is a reference.
4325 Here, we do not care about functions, as they are invalid anyway
4326 for a parameter of type pointer-to-object. */
4328 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4329 /* Non-type template parameters are OK. */
4331 else if (TREE_CODE (expr) != ADDR_EXPR
4332 && TREE_CODE (expr_type) != ARRAY_TYPE)
4334 if (TREE_CODE (expr) == VAR_DECL)
4336 error ("%qD is not a valid template argument "
4337 "because %qD is a variable, not the address of "
4338 "a variable",
4339 expr, expr);
4340 return NULL_TREE;
4342 /* Other values, like integer constants, might be valid
4343 non-type arguments of some other type. */
4344 return error_mark_node;
4346 else
4348 tree decl;
4350 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4351 ? TREE_OPERAND (expr, 0) : expr);
4352 if (TREE_CODE (decl) != VAR_DECL)
4354 error ("%qE is not a valid template argument of type %qT "
4355 "because %qE is not a variable",
4356 expr, type, decl);
4357 return NULL_TREE;
4359 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4361 error ("%qE is not a valid template argument of type %qT "
4362 "because %qD does not have external linkage",
4363 expr, type, decl);
4364 return NULL_TREE;
4368 expr = decay_conversion (expr);
4369 if (expr == error_mark_node)
4370 return error_mark_node;
4372 expr = perform_qualification_conversions (type, expr);
4373 if (expr == error_mark_node)
4374 return error_mark_node;
4376 /* [temp.arg.nontype]/5, bullet 3
4378 For a non-type template-parameter of type reference to object, no
4379 conversions apply. The type referred to by the reference may be more
4380 cv-qualified than the (otherwise identical) type of the
4381 template-argument. The template-parameter is bound directly to the
4382 template-argument, which must be an lvalue. */
4383 else if (TYPE_REF_OBJ_P (type))
4385 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4386 expr_type))
4387 return error_mark_node;
4389 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4391 error ("%qE is not a valid template argument for type %qT "
4392 "because of conflicts in cv-qualification", expr, type);
4393 return NULL_TREE;
4396 if (!real_lvalue_p (expr))
4398 error ("%qE is not a valid template argument for type %qT "
4399 "because it is not an lvalue", expr, type);
4400 return NULL_TREE;
4403 /* [temp.arg.nontype]/1
4405 A template-argument for a non-type, non-template template-parameter
4406 shall be one of: [...]
4408 -- the address of an object or function with external linkage. */
4409 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4411 error ("%qE is not a valid template argument for type %qT "
4412 "because object %qD has not external linkage",
4413 expr, type, expr);
4414 return NULL_TREE;
4417 expr = build_nop (type, build_address (expr));
4419 /* [temp.arg.nontype]/5, bullet 4
4421 For a non-type template-parameter of type pointer to function, only
4422 the function-to-pointer conversion (_conv.func_) is applied. If the
4423 template-argument represents a set of overloaded functions (or a
4424 pointer to such), the matching function is selected from the set
4425 (_over.over_). */
4426 else if (TYPE_PTRFN_P (type))
4428 /* If the argument is a template-id, we might not have enough
4429 context information to decay the pointer. */
4430 if (!type_unknown_p (expr_type))
4432 expr = decay_conversion (expr);
4433 if (expr == error_mark_node)
4434 return error_mark_node;
4437 expr = convert_nontype_argument_function (type, expr);
4438 if (!expr || expr == error_mark_node)
4439 return expr;
4441 /* [temp.arg.nontype]/5, bullet 5
4443 For a non-type template-parameter of type reference to function, no
4444 conversions apply. If the template-argument represents a set of
4445 overloaded functions, the matching function is selected from the set
4446 (_over.over_). */
4447 else if (TYPE_REFFN_P (type))
4449 if (TREE_CODE (expr) == ADDR_EXPR)
4451 error ("%qE is not a valid template argument for type %qT "
4452 "because it is a pointer", expr, type);
4453 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
4454 return NULL_TREE;
4457 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4458 if (!expr || expr == error_mark_node)
4459 return expr;
4461 expr = build_nop (type, build_address (expr));
4463 /* [temp.arg.nontype]/5, bullet 6
4465 For a non-type template-parameter of type pointer to member function,
4466 no conversions apply. If the template-argument represents a set of
4467 overloaded member functions, the matching member function is selected
4468 from the set (_over.over_). */
4469 else if (TYPE_PTRMEMFUNC_P (type))
4471 expr = instantiate_type (type, expr, tf_none);
4472 if (expr == error_mark_node)
4473 return error_mark_node;
4475 /* There is no way to disable standard conversions in
4476 resolve_address_of_overloaded_function (called by
4477 instantiate_type). It is possible that the call succeeded by
4478 converting &B::I to &D::I (where B is a base of D), so we need
4479 to reject this conversion here.
4481 Actually, even if there was a way to disable standard conversions,
4482 it would still be better to reject them here so that we can
4483 provide a superior diagnostic. */
4484 if (!same_type_p (TREE_TYPE (expr), type))
4486 /* Make sure we are just one standard conversion off. */
4487 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4488 error ("%qE is not a valid template argument for type %qT "
4489 "because it is of type %qT", expr, type,
4490 TREE_TYPE (expr));
4491 inform ("standard conversions are not allowed in this context");
4492 return NULL_TREE;
4495 /* [temp.arg.nontype]/5, bullet 7
4497 For a non-type template-parameter of type pointer to data member,
4498 qualification conversions (_conv.qual_) are applied. */
4499 else if (TYPE_PTRMEM_P (type))
4501 expr = perform_qualification_conversions (type, expr);
4502 if (expr == error_mark_node)
4503 return expr;
4505 /* A template non-type parameter must be one of the above. */
4506 else
4507 gcc_unreachable ();
4509 /* Sanity check: did we actually convert the argument to the
4510 right type? */
4511 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4512 return expr;
4516 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4517 template template parameters. Both PARM_PARMS and ARG_PARMS are
4518 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4519 or PARM_DECL.
4521 Consider the example:
4522 template <class T> class A;
4523 template<template <class U> class TT> class B;
4525 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4526 the parameters to A, and OUTER_ARGS contains A. */
4528 static int
4529 coerce_template_template_parms (tree parm_parms,
4530 tree arg_parms,
4531 tsubst_flags_t complain,
4532 tree in_decl,
4533 tree outer_args)
4535 int nparms, nargs, i;
4536 tree parm, arg;
4538 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4539 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4541 nparms = TREE_VEC_LENGTH (parm_parms);
4542 nargs = TREE_VEC_LENGTH (arg_parms);
4544 if (nargs != nparms)
4545 return 0;
4547 for (i = 0; i < nparms; ++i)
4549 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4550 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4551 continue;
4553 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4554 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4556 if (arg == NULL_TREE || arg == error_mark_node
4557 || parm == NULL_TREE || parm == error_mark_node)
4558 return 0;
4560 if (TREE_CODE (arg) != TREE_CODE (parm))
4561 return 0;
4563 switch (TREE_CODE (parm))
4565 case TEMPLATE_DECL:
4566 /* We encounter instantiations of templates like
4567 template <template <template <class> class> class TT>
4568 class C; */
4570 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4571 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4573 if (!coerce_template_template_parms
4574 (parmparm, argparm, complain, in_decl, outer_args))
4575 return 0;
4577 /* Fall through. */
4579 case TYPE_DECL:
4580 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))
4581 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)))
4582 /* One is a parameter pack, the other is not. */
4583 return 0;
4584 break;
4586 case PARM_DECL:
4587 /* The tsubst call is used to handle cases such as
4589 template <int> class C {};
4590 template <class T, template <T> class TT> class D {};
4591 D<int, C> d;
4593 i.e. the parameter list of TT depends on earlier parameters. */
4594 if (!dependent_type_p (TREE_TYPE (arg))
4595 && !same_type_p
4596 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4597 TREE_TYPE (arg)))
4598 return 0;
4600 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4601 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)))
4602 /* One is a parameter pack, the other is not. */
4603 return 0;
4604 break;
4606 default:
4607 gcc_unreachable ();
4610 return 1;
4613 /* Convert the indicated template ARG as necessary to match the
4614 indicated template PARM. Returns the converted ARG, or
4615 error_mark_node if the conversion was unsuccessful. Error and
4616 warning messages are issued under control of COMPLAIN. This
4617 conversion is for the Ith parameter in the parameter list. ARGS is
4618 the full set of template arguments deduced so far. */
4620 static tree
4621 convert_template_argument (tree parm,
4622 tree arg,
4623 tree args,
4624 tsubst_flags_t complain,
4625 int i,
4626 tree in_decl)
4628 tree val;
4629 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4630 tree check_arg = arg;
4632 if (TREE_CODE (arg) == TREE_LIST
4633 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4635 /* The template argument was the name of some
4636 member function. That's usually
4637 invalid, but static members are OK. In any
4638 case, grab the underlying fields/functions
4639 and issue an error later if required. */
4640 arg = TREE_VALUE (arg);
4641 TREE_TYPE (arg) = unknown_type_node;
4644 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4645 requires_type = (TREE_CODE (parm) == TYPE_DECL
4646 || requires_tmpl_type);
4648 /* When determining whether an argument pack expansion is a template,
4649 look at the pattern. */
4650 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4651 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4653 is_tmpl_type =
4654 ((TREE_CODE (check_arg) == TEMPLATE_DECL
4655 && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg)) == TYPE_DECL)
4656 || TREE_CODE (check_arg) == TEMPLATE_TEMPLATE_PARM
4657 || TREE_CODE (check_arg) == UNBOUND_CLASS_TEMPLATE);
4659 if (is_tmpl_type
4660 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4661 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4662 arg = TYPE_STUB_DECL (arg);
4664 is_type = TYPE_P (arg) || is_tmpl_type;
4666 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4667 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4669 pedwarn ("to refer to a type member of a template parameter, "
4670 "use %<typename %E%>", arg);
4672 arg = make_typename_type (TREE_OPERAND (arg, 0),
4673 TREE_OPERAND (arg, 1),
4674 typename_type,
4675 complain & tf_error);
4676 is_type = 1;
4678 if (is_type != requires_type)
4680 if (in_decl)
4682 if (complain & tf_error)
4684 error ("type/value mismatch at argument %d in template "
4685 "parameter list for %qD",
4686 i + 1, in_decl);
4687 if (is_type)
4688 error (" expected a constant of type %qT, got %qT",
4689 TREE_TYPE (parm),
4690 (is_tmpl_type ? DECL_NAME (arg) : arg));
4691 else if (requires_tmpl_type)
4692 error (" expected a class template, got %qE", arg);
4693 else
4694 error (" expected a type, got %qE", arg);
4697 return error_mark_node;
4699 if (is_tmpl_type ^ requires_tmpl_type)
4701 if (in_decl && (complain & tf_error))
4703 error ("type/value mismatch at argument %d in template "
4704 "parameter list for %qD",
4705 i + 1, in_decl);
4706 if (is_tmpl_type)
4707 error (" expected a type, got %qT", DECL_NAME (arg));
4708 else
4709 error (" expected a class template, got %qT", arg);
4711 return error_mark_node;
4714 if (is_type)
4716 if (requires_tmpl_type)
4718 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4719 /* The number of argument required is not known yet.
4720 Just accept it for now. */
4721 val = TREE_TYPE (arg);
4722 else
4724 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4725 tree argparm;
4727 check_arg = arg;
4728 /* When determining whether a pack expansion is a template,
4729 look at the pattern. */
4730 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4731 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4733 argparm = DECL_INNERMOST_TEMPLATE_PARMS (check_arg);
4735 if (coerce_template_template_parms (parmparm, argparm,
4736 complain, in_decl,
4737 args))
4739 val = arg;
4741 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4742 TEMPLATE_DECL. */
4743 if (val != error_mark_node)
4745 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4746 val = TREE_TYPE (val);
4747 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
4748 && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg))
4750 val = TREE_TYPE (check_arg);
4751 val = make_pack_expansion (val);
4755 else
4757 if (in_decl && (complain & tf_error))
4759 error ("type/value mismatch at argument %d in "
4760 "template parameter list for %qD",
4761 i + 1, in_decl);
4762 error (" expected a template of type %qD, got %qD",
4763 parm, arg);
4766 val = error_mark_node;
4770 else
4771 val = arg;
4772 /* We only form one instance of each template specialization.
4773 Therefore, if we use a non-canonical variant (i.e., a
4774 typedef), any future messages referring to the type will use
4775 the typedef, which is confusing if those future uses do not
4776 themselves also use the typedef. */
4777 if (TYPE_P (val))
4778 val = canonical_type_variant (val);
4780 else
4782 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4784 if (invalid_nontype_parm_type_p (t, complain))
4785 return error_mark_node;
4787 if (!uses_template_parms (arg) && !uses_template_parms (t))
4788 /* We used to call digest_init here. However, digest_init
4789 will report errors, which we don't want when complain
4790 is zero. More importantly, digest_init will try too
4791 hard to convert things: for example, `0' should not be
4792 converted to pointer type at this point according to
4793 the standard. Accepting this is not merely an
4794 extension, since deciding whether or not these
4795 conversions can occur is part of determining which
4796 function template to call, or whether a given explicit
4797 argument specification is valid. */
4798 val = convert_nontype_argument (t, arg);
4799 else
4800 val = arg;
4802 if (val == NULL_TREE)
4803 val = error_mark_node;
4804 else if (val == error_mark_node && (complain & tf_error))
4805 error ("could not convert template argument %qE to %qT", arg, t);
4808 return val;
4811 /* Coerces the remaining template arguments in INNER_ARGS (from
4812 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
4813 Returns the coerced argument pack. PARM_IDX is the position of this
4814 parameter in the template parameter list. ARGS is the original
4815 template argument list. */
4816 static tree
4817 coerce_template_parameter_pack (tree parms,
4818 int parm_idx,
4819 tree args,
4820 tree inner_args,
4821 int arg_idx,
4822 tree new_args,
4823 int* lost,
4824 tree in_decl,
4825 tsubst_flags_t complain)
4827 tree parm = TREE_VEC_ELT (parms, parm_idx);
4828 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4829 tree packed_args;
4830 tree argument_pack;
4831 tree packed_types = NULL_TREE;
4833 if (arg_idx > nargs)
4834 arg_idx = nargs;
4836 packed_args = make_tree_vec (nargs - arg_idx);
4838 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
4839 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
4841 /* When the template parameter is a non-type template
4842 parameter pack whose type uses parameter packs, we need
4843 to look at each of the template arguments
4844 separately. Build a vector of the types for these
4845 non-type template parameters in PACKED_TYPES. */
4846 tree expansion
4847 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
4848 packed_types = tsubst_pack_expansion (expansion, args,
4849 complain, in_decl);
4851 if (packed_types == error_mark_node)
4852 return error_mark_node;
4854 /* Check that we have the right number of arguments. */
4855 if (arg_idx < nargs
4856 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
4857 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
4859 int needed_parms
4860 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
4861 error ("wrong number of template arguments (%d, should be %d)",
4862 nargs, needed_parms);
4863 return error_mark_node;
4866 /* If we aren't able to check the actual arguments now
4867 (because they haven't been expanded yet), we can at least
4868 verify that all of the types used for the non-type
4869 template parameter pack are, in fact, valid for non-type
4870 template parameters. */
4871 if (arg_idx < nargs
4872 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
4874 int j, len = TREE_VEC_LENGTH (packed_types);
4875 for (j = 0; j < len; ++j)
4877 tree t = TREE_VEC_ELT (packed_types, j);
4878 if (invalid_nontype_parm_type_p (t, complain))
4879 return error_mark_node;
4884 /* Convert the remaining arguments, which will be a part of the
4885 parameter pack "parm". */
4886 for (; arg_idx < nargs; ++arg_idx)
4888 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
4889 tree actual_parm = TREE_VALUE (parm);
4891 if (packed_types && !PACK_EXPANSION_P (arg))
4893 /* When we have a vector of types (corresponding to the
4894 non-type template parameter pack that uses parameter
4895 packs in its type, as mention above), and the
4896 argument is not an expansion (which expands to a
4897 currently unknown number of arguments), clone the
4898 parm and give it the next type in PACKED_TYPES. */
4899 actual_parm = copy_node (actual_parm);
4900 TREE_TYPE (actual_parm) =
4901 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
4904 arg = convert_template_argument (actual_parm,
4905 arg, new_args, complain, parm_idx,
4906 in_decl);
4907 if (arg == error_mark_node)
4908 (*lost)++;
4909 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
4912 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
4913 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
4914 argument_pack = make_node (TYPE_ARGUMENT_PACK);
4915 else
4917 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
4918 TREE_TYPE (argument_pack) = TREE_TYPE (TREE_VALUE (parm));
4919 TREE_CONSTANT (argument_pack) = 1;
4922 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
4923 return argument_pack;
4926 /* Convert all template arguments to their appropriate types, and
4927 return a vector containing the innermost resulting template
4928 arguments. If any error occurs, return error_mark_node. Error and
4929 warning messages are issued under control of COMPLAIN.
4931 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4932 for arguments not specified in ARGS. Otherwise, if
4933 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4934 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4935 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4936 ARGS. */
4938 static tree
4939 coerce_template_parms (tree parms,
4940 tree args,
4941 tree in_decl,
4942 tsubst_flags_t complain,
4943 bool require_all_args,
4944 bool use_default_args)
4946 int nparms, nargs, parm_idx, arg_idx, lost = 0;
4947 tree inner_args;
4948 tree new_args;
4949 tree new_inner_args;
4950 bool saved_skip_evaluation;
4952 /* When used as a boolean value, indicates whether this is a
4953 variadic template parameter list. Since it's an int, we can also
4954 subtract it from nparms to get the number of non-variadic
4955 parameters. */
4956 int variadic_p = 0;
4958 inner_args
4959 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args));
4961 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4962 nparms = TREE_VEC_LENGTH (parms);
4964 /* Determine if there are any parameter packs. */
4965 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
4967 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
4968 if (template_parameter_pack_p (tparm))
4970 variadic_p = 1;
4971 break;
4975 if ((nargs > nparms - variadic_p && !variadic_p)
4976 || (nargs < nparms - variadic_p
4977 && require_all_args
4978 && (!use_default_args
4979 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4980 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
4982 if (complain & tf_error)
4984 const char *or_more = "";
4985 if (variadic_p)
4987 or_more = " or more";
4988 --nparms;
4991 error ("wrong number of template arguments (%d, should be %d%s)",
4992 nargs, nparms, or_more);
4994 if (in_decl)
4995 error ("provided for %q+D", in_decl);
4998 return error_mark_node;
5001 /* We need to evaluate the template arguments, even though this
5002 template-id may be nested within a "sizeof". */
5003 saved_skip_evaluation = skip_evaluation;
5004 skip_evaluation = false;
5005 new_inner_args = make_tree_vec (nparms);
5006 new_args = add_outermost_template_args (args, new_inner_args);
5007 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5009 tree arg;
5010 tree parm;
5012 /* Get the Ith template parameter. */
5013 parm = TREE_VEC_ELT (parms, parm_idx);
5015 if (parm == error_mark_node)
5017 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5018 continue;
5021 /* Calculate the next argument. */
5022 if (template_parameter_pack_p (TREE_VALUE (parm)))
5024 /* All remaining arguments will be placed in the
5025 template parameter pack PARM. */
5026 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5027 inner_args, arg_idx,
5028 new_args, &lost,
5029 in_decl, complain);
5031 /* Store this argument. */
5032 if (arg == error_mark_node)
5033 lost++;
5034 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5036 /* We are done with all of the arguments. */
5037 arg_idx = nargs;
5039 continue;
5041 else if (arg_idx < nargs)
5043 arg = TREE_VEC_ELT (inner_args, arg_idx);
5045 if (arg && PACK_EXPANSION_P (arg))
5047 /* If ARG is a pack expansion, but PARM is not a
5048 template parameter pack (if it were, we would have
5049 handled it above), we're trying to expand into a
5050 fixed-length argument list. */
5051 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5052 error ("cannot expand %<%E%> into a fixed-length "
5053 "argument list", arg);
5054 else
5055 error ("cannot expand %<%T%> into a fixed-length "
5056 "argument list", arg);
5059 else if (require_all_args)
5060 /* There must be a default arg in this case. */
5061 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5062 complain, in_decl);
5063 else
5064 break;
5066 if (arg == error_mark_node)
5068 if (complain & tf_error)
5069 error ("template argument %d is invalid", arg_idx + 1);
5071 else if (!arg)
5072 /* This only occurs if there was an error in the template
5073 parameter list itself (which we would already have
5074 reported) that we are trying to recover from, e.g., a class
5075 template with a parameter list such as
5076 template<typename..., typename>. */
5077 return error_mark_node;
5078 else
5079 arg = convert_template_argument (TREE_VALUE (parm),
5080 arg, new_args, complain,
5081 parm_idx, in_decl);
5083 if (arg == error_mark_node)
5084 lost++;
5085 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5087 skip_evaluation = saved_skip_evaluation;
5089 if (lost)
5090 return error_mark_node;
5092 return new_inner_args;
5095 /* Returns 1 if template args OT and NT are equivalent. */
5097 static int
5098 template_args_equal (tree ot, tree nt)
5100 if (nt == ot)
5101 return 1;
5103 if (TREE_CODE (nt) == TREE_VEC)
5104 /* For member templates */
5105 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5106 else if (PACK_EXPANSION_P (ot))
5107 return PACK_EXPANSION_P (nt)
5108 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5109 PACK_EXPANSION_PATTERN (nt));
5110 else if (TYPE_P (nt))
5111 return TYPE_P (ot) && same_type_p (ot, nt);
5112 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5113 return 0;
5114 else
5115 return cp_tree_equal (ot, nt);
5118 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5119 of template arguments. Returns 0 otherwise. */
5122 comp_template_args (tree oldargs, tree newargs)
5124 int i;
5126 oldargs = expand_template_argument_pack (oldargs);
5127 newargs = expand_template_argument_pack (newargs);
5129 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5130 return 0;
5132 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5134 tree nt = TREE_VEC_ELT (newargs, i);
5135 tree ot = TREE_VEC_ELT (oldargs, i);
5137 if (! template_args_equal (ot, nt))
5138 return 0;
5140 return 1;
5143 static void
5144 add_pending_template (tree d)
5146 tree ti = (TYPE_P (d)
5147 ? CLASSTYPE_TEMPLATE_INFO (d)
5148 : DECL_TEMPLATE_INFO (d));
5149 struct pending_template *pt;
5150 int level;
5152 if (TI_PENDING_TEMPLATE_FLAG (ti))
5153 return;
5155 /* We are called both from instantiate_decl, where we've already had a
5156 tinst_level pushed, and instantiate_template, where we haven't.
5157 Compensate. */
5158 level = !current_tinst_level || current_tinst_level->decl != d;
5160 if (level)
5161 push_tinst_level (d);
5163 pt = GGC_NEW (struct pending_template);
5164 pt->next = NULL;
5165 pt->tinst = current_tinst_level;
5166 if (last_pending_template)
5167 last_pending_template->next = pt;
5168 else
5169 pending_templates = pt;
5171 last_pending_template = pt;
5173 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5175 if (level)
5176 pop_tinst_level ();
5180 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5181 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5182 documentation for TEMPLATE_ID_EXPR. */
5184 tree
5185 lookup_template_function (tree fns, tree arglist)
5187 tree type;
5189 if (fns == error_mark_node || arglist == error_mark_node)
5190 return error_mark_node;
5192 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5193 gcc_assert (fns && (is_overloaded_fn (fns)
5194 || TREE_CODE (fns) == IDENTIFIER_NODE));
5196 if (BASELINK_P (fns))
5198 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5199 unknown_type_node,
5200 BASELINK_FUNCTIONS (fns),
5201 arglist);
5202 return fns;
5205 type = TREE_TYPE (fns);
5206 if (TREE_CODE (fns) == OVERLOAD || !type)
5207 type = unknown_type_node;
5209 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5212 /* Within the scope of a template class S<T>, the name S gets bound
5213 (in build_self_reference) to a TYPE_DECL for the class, not a
5214 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5215 or one of its enclosing classes, and that type is a template,
5216 return the associated TEMPLATE_DECL. Otherwise, the original
5217 DECL is returned. */
5219 tree
5220 maybe_get_template_decl_from_type_decl (tree decl)
5222 return (decl != NULL_TREE
5223 && TREE_CODE (decl) == TYPE_DECL
5224 && DECL_ARTIFICIAL (decl)
5225 && CLASS_TYPE_P (TREE_TYPE (decl))
5226 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5227 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5230 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5231 parameters, find the desired type.
5233 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5235 IN_DECL, if non-NULL, is the template declaration we are trying to
5236 instantiate.
5238 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5239 the class we are looking up.
5241 Issue error and warning messages under control of COMPLAIN.
5243 If the template class is really a local class in a template
5244 function, then the FUNCTION_CONTEXT is the function in which it is
5245 being instantiated.
5247 ??? Note that this function is currently called *twice* for each
5248 template-id: the first time from the parser, while creating the
5249 incomplete type (finish_template_type), and the second type during the
5250 real instantiation (instantiate_template_class). This is surely something
5251 that we want to avoid. It also causes some problems with argument
5252 coercion (see convert_nontype_argument for more information on this). */
5254 tree
5255 lookup_template_class (tree d1,
5256 tree arglist,
5257 tree in_decl,
5258 tree context,
5259 int entering_scope,
5260 tsubst_flags_t complain)
5262 tree template = NULL_TREE, parmlist;
5263 tree t;
5265 timevar_push (TV_NAME_LOOKUP);
5267 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5269 tree value = innermost_non_namespace_value (d1);
5270 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5271 template = value;
5272 else
5274 if (context)
5275 push_decl_namespace (context);
5276 template = lookup_name (d1);
5277 template = maybe_get_template_decl_from_type_decl (template);
5278 if (context)
5279 pop_decl_namespace ();
5281 if (template)
5282 context = DECL_CONTEXT (template);
5284 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
5286 tree type = TREE_TYPE (d1);
5288 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5289 an implicit typename for the second A. Deal with it. */
5290 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5291 type = TREE_TYPE (type);
5293 if (CLASSTYPE_TEMPLATE_INFO (type))
5295 template = CLASSTYPE_TI_TEMPLATE (type);
5296 d1 = DECL_NAME (template);
5299 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5300 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5302 template = TYPE_TI_TEMPLATE (d1);
5303 d1 = DECL_NAME (template);
5305 else if (TREE_CODE (d1) == TEMPLATE_DECL
5306 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5308 template = d1;
5309 d1 = DECL_NAME (template);
5310 context = DECL_CONTEXT (template);
5313 /* Issue an error message if we didn't find a template. */
5314 if (! template)
5316 if (complain & tf_error)
5317 error ("%qT is not a template", d1);
5318 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5321 if (TREE_CODE (template) != TEMPLATE_DECL
5322 /* Make sure it's a user visible template, if it was named by
5323 the user. */
5324 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
5325 && !PRIMARY_TEMPLATE_P (template)))
5327 if (complain & tf_error)
5329 error ("non-template type %qT used as a template", d1);
5330 if (in_decl)
5331 error ("for template declaration %q+D", in_decl);
5333 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5336 complain &= ~tf_user;
5338 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5340 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5341 template arguments */
5343 tree parm;
5344 tree arglist2;
5346 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
5348 /* Consider an example where a template template parameter declared as
5350 template <class T, class U = std::allocator<T> > class TT
5352 The template parameter level of T and U are one level larger than
5353 of TT. To proper process the default argument of U, say when an
5354 instantiation `TT<int>' is seen, we need to build the full
5355 arguments containing {int} as the innermost level. Outer levels,
5356 available when not appearing as default template argument, can be
5357 obtained from `current_template_args ()'.
5359 Suppose that TT is later substituted with std::vector. The above
5360 instantiation is `TT<int, std::allocator<T> >' with TT at
5361 level 1, and T at level 2, while the template arguments at level 1
5362 becomes {std::vector} and the inner level 2 is {int}. */
5364 if (current_template_parms)
5365 arglist = add_to_template_args (current_template_args (), arglist);
5367 arglist2 = coerce_template_parms (parmlist, arglist, template,
5368 complain,
5369 /*require_all_args=*/true,
5370 /*use_default_args=*/true);
5371 if (arglist2 == error_mark_node
5372 || (!uses_template_parms (arglist2)
5373 && check_instantiated_args (template, arglist2, complain)))
5374 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5376 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
5377 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5379 else
5381 tree template_type = TREE_TYPE (template);
5382 tree gen_tmpl;
5383 tree type_decl;
5384 tree found = NULL_TREE;
5385 int arg_depth;
5386 int parm_depth;
5387 int is_partial_instantiation;
5389 gen_tmpl = most_general_template (template);
5390 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5391 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5392 arg_depth = TMPL_ARGS_DEPTH (arglist);
5394 if (arg_depth == 1 && parm_depth > 1)
5396 /* We've been given an incomplete set of template arguments.
5397 For example, given:
5399 template <class T> struct S1 {
5400 template <class U> struct S2 {};
5401 template <class U> struct S2<U*> {};
5404 we will be called with an ARGLIST of `U*', but the
5405 TEMPLATE will be `template <class T> template
5406 <class U> struct S1<T>::S2'. We must fill in the missing
5407 arguments. */
5408 arglist
5409 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5410 arglist);
5411 arg_depth = TMPL_ARGS_DEPTH (arglist);
5414 /* Now we should have enough arguments. */
5415 gcc_assert (parm_depth == arg_depth);
5417 /* From here on, we're only interested in the most general
5418 template. */
5419 template = gen_tmpl;
5421 /* Calculate the BOUND_ARGS. These will be the args that are
5422 actually tsubst'd into the definition to create the
5423 instantiation. */
5424 if (parm_depth > 1)
5426 /* We have multiple levels of arguments to coerce, at once. */
5427 int i;
5428 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5430 tree bound_args = make_tree_vec (parm_depth);
5432 for (i = saved_depth,
5433 t = DECL_TEMPLATE_PARMS (template);
5434 i > 0 && t != NULL_TREE;
5435 --i, t = TREE_CHAIN (t))
5437 tree a = coerce_template_parms (TREE_VALUE (t),
5438 arglist, template,
5439 complain,
5440 /*require_all_args=*/true,
5441 /*use_default_args=*/true);
5443 /* Don't process further if one of the levels fails. */
5444 if (a == error_mark_node)
5446 /* Restore the ARGLIST to its full size. */
5447 TREE_VEC_LENGTH (arglist) = saved_depth;
5448 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5451 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5453 /* We temporarily reduce the length of the ARGLIST so
5454 that coerce_template_parms will see only the arguments
5455 corresponding to the template parameters it is
5456 examining. */
5457 TREE_VEC_LENGTH (arglist)--;
5460 /* Restore the ARGLIST to its full size. */
5461 TREE_VEC_LENGTH (arglist) = saved_depth;
5463 arglist = bound_args;
5465 else
5466 arglist
5467 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5468 INNERMOST_TEMPLATE_ARGS (arglist),
5469 template,
5470 complain,
5471 /*require_all_args=*/true,
5472 /*use_default_args=*/true);
5474 if (arglist == error_mark_node)
5475 /* We were unable to bind the arguments. */
5476 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5478 /* In the scope of a template class, explicit references to the
5479 template class refer to the type of the template, not any
5480 instantiation of it. For example, in:
5482 template <class T> class C { void f(C<T>); }
5484 the `C<T>' is just the same as `C'. Outside of the
5485 class, however, such a reference is an instantiation. */
5486 if (comp_template_args (TYPE_TI_ARGS (template_type),
5487 arglist))
5489 found = template_type;
5491 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5493 tree ctx;
5495 for (ctx = current_class_type;
5496 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5497 ctx = (TYPE_P (ctx)
5498 ? TYPE_CONTEXT (ctx)
5499 : DECL_CONTEXT (ctx)))
5500 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5501 goto found_ctx;
5503 /* We're not in the scope of the class, so the
5504 TEMPLATE_TYPE is not the type we want after all. */
5505 found = NULL_TREE;
5506 found_ctx:;
5509 if (found)
5510 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5512 /* If we already have this specialization, return it. */
5513 found = retrieve_specialization (template, arglist,
5514 /*class_specializations_p=*/false);
5515 if (found)
5516 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5518 /* This type is a "partial instantiation" if any of the template
5519 arguments still involve template parameters. Note that we set
5520 IS_PARTIAL_INSTANTIATION for partial specializations as
5521 well. */
5522 is_partial_instantiation = uses_template_parms (arglist);
5524 /* If the deduced arguments are invalid, then the binding
5525 failed. */
5526 if (!is_partial_instantiation
5527 && check_instantiated_args (template,
5528 INNERMOST_TEMPLATE_ARGS (arglist),
5529 complain))
5530 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5532 if (!is_partial_instantiation
5533 && !PRIMARY_TEMPLATE_P (template)
5534 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
5536 found = xref_tag_from_type (TREE_TYPE (template),
5537 DECL_NAME (template),
5538 /*tag_scope=*/ts_global);
5539 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5542 context = tsubst (DECL_CONTEXT (template), arglist,
5543 complain, in_decl);
5544 if (!context)
5545 context = global_namespace;
5547 /* Create the type. */
5548 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5550 if (!is_partial_instantiation)
5552 set_current_access_from_decl (TYPE_NAME (template_type));
5553 t = start_enum (TYPE_IDENTIFIER (template_type));
5555 else
5556 /* We don't want to call start_enum for this type, since
5557 the values for the enumeration constants may involve
5558 template parameters. And, no one should be interested
5559 in the enumeration constants for such a type. */
5560 t = make_node (ENUMERAL_TYPE);
5562 else
5564 t = make_aggr_type (TREE_CODE (template_type));
5565 CLASSTYPE_DECLARED_CLASS (t)
5566 = CLASSTYPE_DECLARED_CLASS (template_type);
5567 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5568 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5570 /* A local class. Make sure the decl gets registered properly. */
5571 if (context == current_function_decl)
5572 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
5574 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5575 /* This instantiation is another name for the primary
5576 template type. Set the TYPE_CANONICAL field
5577 appropriately. */
5578 TYPE_CANONICAL (t) = template_type;
5579 else if (any_template_arguments_need_structural_equality_p (arglist))
5580 /* Some of the template arguments require structural
5581 equality testing, so this template class requires
5582 structural equality testing. */
5583 SET_TYPE_STRUCTURAL_EQUALITY (t);
5586 /* If we called start_enum or pushtag above, this information
5587 will already be set up. */
5588 if (!TYPE_NAME (t))
5590 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5592 type_decl = create_implicit_typedef (DECL_NAME (template), t);
5593 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5594 TYPE_STUB_DECL (t) = type_decl;
5595 DECL_SOURCE_LOCATION (type_decl)
5596 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5598 else
5599 type_decl = TYPE_NAME (t);
5601 TREE_PRIVATE (type_decl)
5602 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5603 TREE_PROTECTED (type_decl)
5604 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5605 DECL_IN_SYSTEM_HEADER (type_decl)
5606 = DECL_IN_SYSTEM_HEADER (template);
5607 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5609 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5610 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5613 /* Set up the template information. We have to figure out which
5614 template is the immediate parent if this is a full
5615 instantiation. */
5616 if (parm_depth == 1 || is_partial_instantiation
5617 || !PRIMARY_TEMPLATE_P (template))
5618 /* This case is easy; there are no member templates involved. */
5619 found = template;
5620 else
5622 /* This is a full instantiation of a member template. Look
5623 for a partial instantiation of which this is an instance. */
5625 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
5626 found; found = TREE_CHAIN (found))
5628 int success;
5629 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5631 /* We only want partial instantiations, here, not
5632 specializations or full instantiations. */
5633 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5634 || !uses_template_parms (TREE_VALUE (found)))
5635 continue;
5637 /* Temporarily reduce by one the number of levels in the
5638 ARGLIST and in FOUND so as to avoid comparing the
5639 last set of arguments. */
5640 TREE_VEC_LENGTH (arglist)--;
5641 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5643 /* See if the arguments match. If they do, then TMPL is
5644 the partial instantiation we want. */
5645 success = comp_template_args (TREE_PURPOSE (found), arglist);
5647 /* Restore the argument vectors to their full size. */
5648 TREE_VEC_LENGTH (arglist)++;
5649 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
5651 if (success)
5653 found = tmpl;
5654 break;
5658 if (!found)
5660 /* There was no partial instantiation. This happens
5661 where C<T> is a member template of A<T> and it's used
5662 in something like
5664 template <typename T> struct B { A<T>::C<int> m; };
5665 B<float>;
5667 Create the partial instantiation.
5669 TREE_VEC_LENGTH (arglist)--;
5670 found = tsubst (template, arglist, complain, NULL_TREE);
5671 TREE_VEC_LENGTH (arglist)++;
5675 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
5676 DECL_TEMPLATE_INSTANTIATIONS (template)
5677 = tree_cons (arglist, t,
5678 DECL_TEMPLATE_INSTANTIATIONS (template));
5680 if (TREE_CODE (t) == ENUMERAL_TYPE
5681 && !is_partial_instantiation)
5682 /* Now that the type has been registered on the instantiations
5683 list, we set up the enumerators. Because the enumeration
5684 constants may involve the enumeration type itself, we make
5685 sure to register the type first, and then create the
5686 constants. That way, doing tsubst_expr for the enumeration
5687 constants won't result in recursive calls here; we'll find
5688 the instantiation and exit above. */
5689 tsubst_enum (template_type, t, arglist);
5691 if (is_partial_instantiation)
5692 /* If the type makes use of template parameters, the
5693 code that generates debugging information will crash. */
5694 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
5696 /* Possibly limit visibility based on template args. */
5697 TREE_PUBLIC (type_decl) = 1;
5698 determine_visibility (type_decl);
5700 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5702 timevar_pop (TV_NAME_LOOKUP);
5705 struct pair_fn_data
5707 tree_fn_t fn;
5708 void *data;
5709 struct pointer_set_t *visited;
5712 /* Called from for_each_template_parm via walk_tree. */
5714 static tree
5715 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
5717 tree t = *tp;
5718 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
5719 tree_fn_t fn = pfd->fn;
5720 void *data = pfd->data;
5722 if (TYPE_P (t)
5723 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
5724 return error_mark_node;
5726 switch (TREE_CODE (t))
5728 case RECORD_TYPE:
5729 if (TYPE_PTRMEMFUNC_P (t))
5730 break;
5731 /* Fall through. */
5733 case UNION_TYPE:
5734 case ENUMERAL_TYPE:
5735 if (!TYPE_TEMPLATE_INFO (t))
5736 *walk_subtrees = 0;
5737 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
5738 fn, data, pfd->visited))
5739 return error_mark_node;
5740 break;
5742 case INTEGER_TYPE:
5743 if (for_each_template_parm (TYPE_MIN_VALUE (t),
5744 fn, data, pfd->visited)
5745 || for_each_template_parm (TYPE_MAX_VALUE (t),
5746 fn, data, pfd->visited))
5747 return error_mark_node;
5748 break;
5750 case METHOD_TYPE:
5751 /* Since we're not going to walk subtrees, we have to do this
5752 explicitly here. */
5753 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
5754 pfd->visited))
5755 return error_mark_node;
5756 /* Fall through. */
5758 case FUNCTION_TYPE:
5759 /* Check the return type. */
5760 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5761 return error_mark_node;
5763 /* Check the parameter types. Since default arguments are not
5764 instantiated until they are needed, the TYPE_ARG_TYPES may
5765 contain expressions that involve template parameters. But,
5766 no-one should be looking at them yet. And, once they're
5767 instantiated, they don't contain template parameters, so
5768 there's no point in looking at them then, either. */
5770 tree parm;
5772 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5773 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5774 pfd->visited))
5775 return error_mark_node;
5777 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5778 want walk_tree walking into them itself. */
5779 *walk_subtrees = 0;
5781 break;
5783 case TYPEOF_TYPE:
5784 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5785 pfd->visited))
5786 return error_mark_node;
5787 break;
5789 case FUNCTION_DECL:
5790 case VAR_DECL:
5791 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5792 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5793 pfd->visited))
5794 return error_mark_node;
5795 /* Fall through. */
5797 case PARM_DECL:
5798 case CONST_DECL:
5799 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5800 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5801 pfd->visited))
5802 return error_mark_node;
5803 if (DECL_CONTEXT (t)
5804 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5805 pfd->visited))
5806 return error_mark_node;
5807 break;
5809 case BOUND_TEMPLATE_TEMPLATE_PARM:
5810 /* Record template parameters such as `T' inside `TT<T>'. */
5811 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5812 return error_mark_node;
5813 /* Fall through. */
5815 case TEMPLATE_TEMPLATE_PARM:
5816 case TEMPLATE_TYPE_PARM:
5817 case TEMPLATE_PARM_INDEX:
5818 if (fn && (*fn)(t, data))
5819 return error_mark_node;
5820 else if (!fn)
5821 return error_mark_node;
5822 break;
5824 case TEMPLATE_DECL:
5825 /* A template template parameter is encountered. */
5826 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5827 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5828 return error_mark_node;
5830 /* Already substituted template template parameter */
5831 *walk_subtrees = 0;
5832 break;
5834 case TYPENAME_TYPE:
5835 if (!fn
5836 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5837 data, pfd->visited))
5838 return error_mark_node;
5839 break;
5841 case CONSTRUCTOR:
5842 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5843 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5844 (TREE_TYPE (t)), fn, data,
5845 pfd->visited))
5846 return error_mark_node;
5847 break;
5849 case INDIRECT_REF:
5850 case COMPONENT_REF:
5851 /* If there's no type, then this thing must be some expression
5852 involving template parameters. */
5853 if (!fn && !TREE_TYPE (t))
5854 return error_mark_node;
5855 break;
5857 case MODOP_EXPR:
5858 case CAST_EXPR:
5859 case REINTERPRET_CAST_EXPR:
5860 case CONST_CAST_EXPR:
5861 case STATIC_CAST_EXPR:
5862 case DYNAMIC_CAST_EXPR:
5863 case ARROW_EXPR:
5864 case DOTSTAR_EXPR:
5865 case TYPEID_EXPR:
5866 case PSEUDO_DTOR_EXPR:
5867 if (!fn)
5868 return error_mark_node;
5869 break;
5871 default:
5872 break;
5875 /* We didn't find any template parameters we liked. */
5876 return NULL_TREE;
5879 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5880 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5881 call FN with the parameter and the DATA.
5882 If FN returns nonzero, the iteration is terminated, and
5883 for_each_template_parm returns 1. Otherwise, the iteration
5884 continues. If FN never returns a nonzero value, the value
5885 returned by for_each_template_parm is 0. If FN is NULL, it is
5886 considered to be the function which always returns 1. */
5888 static int
5889 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5890 struct pointer_set_t *visited)
5892 struct pair_fn_data pfd;
5893 int result;
5895 /* Set up. */
5896 pfd.fn = fn;
5897 pfd.data = data;
5899 /* Walk the tree. (Conceptually, we would like to walk without
5900 duplicates, but for_each_template_parm_r recursively calls
5901 for_each_template_parm, so we would need to reorganize a fair
5902 bit to use walk_tree_without_duplicates, so we keep our own
5903 visited list.) */
5904 if (visited)
5905 pfd.visited = visited;
5906 else
5907 pfd.visited = pointer_set_create ();
5908 result = cp_walk_tree (&t,
5909 for_each_template_parm_r,
5910 &pfd,
5911 pfd.visited) != NULL_TREE;
5913 /* Clean up. */
5914 if (!visited)
5916 pointer_set_destroy (pfd.visited);
5917 pfd.visited = 0;
5920 return result;
5923 /* Returns true if T depends on any template parameter. */
5926 uses_template_parms (tree t)
5928 bool dependent_p;
5929 int saved_processing_template_decl;
5931 saved_processing_template_decl = processing_template_decl;
5932 if (!saved_processing_template_decl)
5933 processing_template_decl = 1;
5934 if (TYPE_P (t))
5935 dependent_p = dependent_type_p (t);
5936 else if (TREE_CODE (t) == TREE_VEC)
5937 dependent_p = any_dependent_template_arguments_p (t);
5938 else if (TREE_CODE (t) == TREE_LIST)
5939 dependent_p = (uses_template_parms (TREE_VALUE (t))
5940 || uses_template_parms (TREE_CHAIN (t)));
5941 else if (TREE_CODE (t) == TYPE_DECL)
5942 dependent_p = dependent_type_p (TREE_TYPE (t));
5943 else if (DECL_P (t)
5944 || EXPR_P (t)
5945 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5946 || TREE_CODE (t) == OVERLOAD
5947 || TREE_CODE (t) == BASELINK
5948 || TREE_CODE (t) == IDENTIFIER_NODE
5949 || TREE_CODE (t) == TRAIT_EXPR
5950 || CONSTANT_CLASS_P (t))
5951 dependent_p = (type_dependent_expression_p (t)
5952 || value_dependent_expression_p (t));
5953 else
5955 gcc_assert (t == error_mark_node);
5956 dependent_p = false;
5959 processing_template_decl = saved_processing_template_decl;
5961 return dependent_p;
5964 /* Returns true if T depends on any template parameter with level LEVEL. */
5967 uses_template_parms_level (tree t, int level)
5969 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5972 static int tinst_depth;
5973 extern int max_tinst_depth;
5974 #ifdef GATHER_STATISTICS
5975 int depth_reached;
5976 #endif
5977 static int tinst_level_tick;
5978 static int last_template_error_tick;
5980 /* We're starting to instantiate D; record the template instantiation context
5981 for diagnostics and to restore it later. */
5983 static int
5984 push_tinst_level (tree d)
5986 struct tinst_level *new;
5988 if (tinst_depth >= max_tinst_depth)
5990 /* If the instantiation in question still has unbound template parms,
5991 we don't really care if we can't instantiate it, so just return.
5992 This happens with base instantiation for implicit `typename'. */
5993 if (uses_template_parms (d))
5994 return 0;
5996 last_template_error_tick = tinst_level_tick;
5997 error ("template instantiation depth exceeds maximum of %d (use "
5998 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5999 max_tinst_depth, d);
6001 print_instantiation_context ();
6003 return 0;
6006 new = GGC_NEW (struct tinst_level);
6007 new->decl = d;
6008 new->locus = input_location;
6009 new->in_system_header_p = in_system_header;
6010 new->next = current_tinst_level;
6011 current_tinst_level = new;
6013 ++tinst_depth;
6014 #ifdef GATHER_STATISTICS
6015 if (tinst_depth > depth_reached)
6016 depth_reached = tinst_depth;
6017 #endif
6019 ++tinst_level_tick;
6020 return 1;
6023 /* We're done instantiating this template; return to the instantiation
6024 context. */
6026 static void
6027 pop_tinst_level (void)
6029 /* Restore the filename and line number stashed away when we started
6030 this instantiation. */
6031 input_location = current_tinst_level->locus;
6032 in_system_header = current_tinst_level->in_system_header_p;
6033 current_tinst_level = current_tinst_level->next;
6034 --tinst_depth;
6035 ++tinst_level_tick;
6038 /* We're instantiating a deferred template; restore the template
6039 instantiation context in which the instantiation was requested, which
6040 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6042 static tree
6043 reopen_tinst_level (struct tinst_level *level)
6045 struct tinst_level *t;
6047 tinst_depth = 0;
6048 for (t = level; t; t = t->next)
6049 ++tinst_depth;
6051 current_tinst_level = level;
6052 pop_tinst_level ();
6053 return level->decl;
6056 /* Returns the TINST_LEVEL which gives the original instantiation
6057 context. */
6059 struct tinst_level *
6060 outermost_tinst_level (void)
6062 struct tinst_level *level = current_tinst_level;
6063 if (level)
6064 while (level->next)
6065 level = level->next;
6066 return level;
6069 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6070 vector of template arguments, as for tsubst.
6072 Returns an appropriate tsubst'd friend declaration. */
6074 static tree
6075 tsubst_friend_function (tree decl, tree args)
6077 tree new_friend;
6079 if (TREE_CODE (decl) == FUNCTION_DECL
6080 && DECL_TEMPLATE_INSTANTIATION (decl)
6081 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6082 /* This was a friend declared with an explicit template
6083 argument list, e.g.:
6085 friend void f<>(T);
6087 to indicate that f was a template instantiation, not a new
6088 function declaration. Now, we have to figure out what
6089 instantiation of what template. */
6091 tree template_id, arglist, fns;
6092 tree new_args;
6093 tree tmpl;
6094 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6096 /* Friend functions are looked up in the containing namespace scope.
6097 We must enter that scope, to avoid finding member functions of the
6098 current cless with same name. */
6099 push_nested_namespace (ns);
6100 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6101 tf_warning_or_error, NULL_TREE,
6102 /*integral_constant_expression_p=*/false);
6103 pop_nested_namespace (ns);
6104 arglist = tsubst (DECL_TI_ARGS (decl), args,
6105 tf_warning_or_error, NULL_TREE);
6106 template_id = lookup_template_function (fns, arglist);
6108 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6109 tmpl = determine_specialization (template_id, new_friend,
6110 &new_args,
6111 /*need_member_template=*/0,
6112 TREE_VEC_LENGTH (args),
6113 tsk_none);
6114 return instantiate_template (tmpl, new_args, tf_error);
6117 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6119 /* The NEW_FRIEND will look like an instantiation, to the
6120 compiler, but is not an instantiation from the point of view of
6121 the language. For example, we might have had:
6123 template <class T> struct S {
6124 template <class U> friend void f(T, U);
6127 Then, in S<int>, template <class U> void f(int, U) is not an
6128 instantiation of anything. */
6129 if (new_friend == error_mark_node)
6130 return error_mark_node;
6132 DECL_USE_TEMPLATE (new_friend) = 0;
6133 if (TREE_CODE (decl) == TEMPLATE_DECL)
6135 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6136 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6137 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6140 /* The mangled name for the NEW_FRIEND is incorrect. The function
6141 is not a template instantiation and should not be mangled like
6142 one. Therefore, we forget the mangling here; we'll recompute it
6143 later if we need it. */
6144 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6146 SET_DECL_RTL (new_friend, NULL_RTX);
6147 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6150 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6152 tree old_decl;
6153 tree new_friend_template_info;
6154 tree new_friend_result_template_info;
6155 tree ns;
6156 int new_friend_is_defn;
6158 /* We must save some information from NEW_FRIEND before calling
6159 duplicate decls since that function will free NEW_FRIEND if
6160 possible. */
6161 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6162 new_friend_is_defn =
6163 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6164 (template_for_substitution (new_friend)))
6165 != NULL_TREE);
6166 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6168 /* This declaration is a `primary' template. */
6169 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6171 new_friend_result_template_info
6172 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6174 else
6175 new_friend_result_template_info = NULL_TREE;
6177 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6178 if (new_friend_is_defn)
6179 DECL_INITIAL (new_friend) = error_mark_node;
6181 /* Inside pushdecl_namespace_level, we will push into the
6182 current namespace. However, the friend function should go
6183 into the namespace of the template. */
6184 ns = decl_namespace_context (new_friend);
6185 push_nested_namespace (ns);
6186 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6187 pop_nested_namespace (ns);
6189 if (old_decl == error_mark_node)
6190 return error_mark_node;
6192 if (old_decl != new_friend)
6194 /* This new friend declaration matched an existing
6195 declaration. For example, given:
6197 template <class T> void f(T);
6198 template <class U> class C {
6199 template <class T> friend void f(T) {}
6202 the friend declaration actually provides the definition
6203 of `f', once C has been instantiated for some type. So,
6204 old_decl will be the out-of-class template declaration,
6205 while new_friend is the in-class definition.
6207 But, if `f' was called before this point, the
6208 instantiation of `f' will have DECL_TI_ARGS corresponding
6209 to `T' but not to `U', references to which might appear
6210 in the definition of `f'. Previously, the most general
6211 template for an instantiation of `f' was the out-of-class
6212 version; now it is the in-class version. Therefore, we
6213 run through all specialization of `f', adding to their
6214 DECL_TI_ARGS appropriately. In particular, they need a
6215 new set of outer arguments, corresponding to the
6216 arguments for this class instantiation.
6218 The same situation can arise with something like this:
6220 friend void f(int);
6221 template <class T> class C {
6222 friend void f(T) {}
6225 when `C<int>' is instantiated. Now, `f(int)' is defined
6226 in the class. */
6228 if (!new_friend_is_defn)
6229 /* On the other hand, if the in-class declaration does
6230 *not* provide a definition, then we don't want to alter
6231 existing definitions. We can just leave everything
6232 alone. */
6234 else
6236 /* Overwrite whatever template info was there before, if
6237 any, with the new template information pertaining to
6238 the declaration. */
6239 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6241 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6242 reregister_specialization (new_friend,
6243 most_general_template (old_decl),
6244 old_decl);
6245 else
6247 tree t;
6248 tree new_friend_args;
6250 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6251 = new_friend_result_template_info;
6253 new_friend_args = TI_ARGS (new_friend_template_info);
6254 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6255 t != NULL_TREE;
6256 t = TREE_CHAIN (t))
6258 tree spec = TREE_VALUE (t);
6260 DECL_TI_ARGS (spec)
6261 = add_outermost_template_args (new_friend_args,
6262 DECL_TI_ARGS (spec));
6265 /* Now, since specializations are always supposed to
6266 hang off of the most general template, we must move
6267 them. */
6268 t = most_general_template (old_decl);
6269 if (t != old_decl)
6271 DECL_TEMPLATE_SPECIALIZATIONS (t)
6272 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6273 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6274 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6279 /* The information from NEW_FRIEND has been merged into OLD_DECL
6280 by duplicate_decls. */
6281 new_friend = old_decl;
6284 else
6286 tree context = DECL_CONTEXT (new_friend);
6287 bool dependent_p;
6289 /* In the code
6290 template <class T> class C {
6291 template <class U> friend void C1<U>::f (); // case 1
6292 friend void C2<T>::f (); // case 2
6294 we only need to make sure CONTEXT is a complete type for
6295 case 2. To distinguish between the two cases, we note that
6296 CONTEXT of case 1 remains dependent type after tsubst while
6297 this isn't true for case 2. */
6298 ++processing_template_decl;
6299 dependent_p = dependent_type_p (context);
6300 --processing_template_decl;
6302 if (!dependent_p
6303 && !complete_type_or_else (context, NULL_TREE))
6304 return error_mark_node;
6306 if (COMPLETE_TYPE_P (context))
6308 /* Check to see that the declaration is really present, and,
6309 possibly obtain an improved declaration. */
6310 tree fn = check_classfn (context,
6311 new_friend, NULL_TREE);
6313 if (fn)
6314 new_friend = fn;
6318 return new_friend;
6321 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6322 template arguments, as for tsubst.
6324 Returns an appropriate tsubst'd friend type or error_mark_node on
6325 failure. */
6327 static tree
6328 tsubst_friend_class (tree friend_tmpl, tree args)
6330 tree friend_type;
6331 tree tmpl;
6332 tree context;
6334 context = DECL_CONTEXT (friend_tmpl);
6336 if (context)
6338 if (TREE_CODE (context) == NAMESPACE_DECL)
6339 push_nested_namespace (context);
6340 else
6341 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6344 /* Look for a class template declaration. We look for hidden names
6345 because two friend declarations of the same template are the
6346 same. For example, in:
6348 struct A {
6349 template <typename> friend class F;
6351 template <typename> struct B {
6352 template <typename> friend class F;
6355 both F templates are the same. */
6356 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6357 /*block_p=*/true, 0,
6358 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6360 /* But, if we don't find one, it might be because we're in a
6361 situation like this:
6363 template <class T>
6364 struct S {
6365 template <class U>
6366 friend struct S;
6369 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6370 for `S<int>', not the TEMPLATE_DECL. */
6371 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6373 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6374 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6377 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6379 /* The friend template has already been declared. Just
6380 check to see that the declarations match, and install any new
6381 default parameters. We must tsubst the default parameters,
6382 of course. We only need the innermost template parameters
6383 because that is all that redeclare_class_template will look
6384 at. */
6385 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6386 > TMPL_ARGS_DEPTH (args))
6388 tree parms;
6389 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6390 args, tf_warning_or_error);
6391 redeclare_class_template (TREE_TYPE (tmpl), parms);
6394 friend_type = TREE_TYPE (tmpl);
6396 else
6398 /* The friend template has not already been declared. In this
6399 case, the instantiation of the template class will cause the
6400 injection of this template into the global scope. */
6401 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6402 if (tmpl == error_mark_node)
6403 return error_mark_node;
6405 /* The new TMPL is not an instantiation of anything, so we
6406 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6407 the new type because that is supposed to be the corresponding
6408 template decl, i.e., TMPL. */
6409 DECL_USE_TEMPLATE (tmpl) = 0;
6410 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6411 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6412 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6413 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6415 /* Inject this template into the global scope. */
6416 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6419 if (context)
6421 if (TREE_CODE (context) == NAMESPACE_DECL)
6422 pop_nested_namespace (context);
6423 else
6424 pop_nested_class ();
6427 return friend_type;
6430 /* Returns zero if TYPE cannot be completed later due to circularity.
6431 Otherwise returns one. */
6433 static int
6434 can_complete_type_without_circularity (tree type)
6436 if (type == NULL_TREE || type == error_mark_node)
6437 return 0;
6438 else if (COMPLETE_TYPE_P (type))
6439 return 1;
6440 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6441 return can_complete_type_without_circularity (TREE_TYPE (type));
6442 else if (CLASS_TYPE_P (type)
6443 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6444 return 0;
6445 else
6446 return 1;
6449 tree
6450 instantiate_class_template (tree type)
6452 tree template, args, pattern, t, member;
6453 tree typedecl;
6454 tree pbinfo;
6455 tree base_list;
6457 if (type == error_mark_node)
6458 return error_mark_node;
6460 if (TYPE_BEING_DEFINED (type)
6461 || COMPLETE_TYPE_P (type)
6462 || dependent_type_p (type))
6463 return type;
6465 /* Figure out which template is being instantiated. */
6466 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6467 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
6469 /* Determine what specialization of the original template to
6470 instantiate. */
6471 t = most_specialized_class (type, template);
6472 if (t == error_mark_node)
6474 TYPE_BEING_DEFINED (type) = 1;
6475 return error_mark_node;
6477 else if (t)
6479 /* This TYPE is actually an instantiation of a partial
6480 specialization. We replace the innermost set of ARGS with
6481 the arguments appropriate for substitution. For example,
6482 given:
6484 template <class T> struct S {};
6485 template <class T> struct S<T*> {};
6487 and supposing that we are instantiating S<int*>, ARGS will
6488 presently be {int*} -- but we need {int}. */
6489 pattern = TREE_TYPE (t);
6490 args = TREE_PURPOSE (t);
6492 else
6494 pattern = TREE_TYPE (template);
6495 args = CLASSTYPE_TI_ARGS (type);
6498 /* If the template we're instantiating is incomplete, then clearly
6499 there's nothing we can do. */
6500 if (!COMPLETE_TYPE_P (pattern))
6501 return type;
6503 /* If we've recursively instantiated too many templates, stop. */
6504 if (! push_tinst_level (type))
6505 return type;
6507 /* Now we're really doing the instantiation. Mark the type as in
6508 the process of being defined. */
6509 TYPE_BEING_DEFINED (type) = 1;
6511 /* We may be in the middle of deferred access check. Disable
6512 it now. */
6513 push_deferring_access_checks (dk_no_deferred);
6515 push_to_top_level ();
6517 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
6519 /* Set the input location to the template definition. This is needed
6520 if tsubsting causes an error. */
6521 typedecl = TYPE_MAIN_DECL (type);
6522 input_location = DECL_SOURCE_LOCATION (typedecl);
6523 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
6525 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
6526 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
6527 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
6528 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
6529 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
6530 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
6531 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
6532 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
6533 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
6534 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
6535 TYPE_PACKED (type) = TYPE_PACKED (pattern);
6536 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
6537 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
6538 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6539 if (ANON_AGGR_TYPE_P (pattern))
6540 SET_ANON_AGGR_TYPE_P (type);
6541 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
6543 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
6544 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
6547 pbinfo = TYPE_BINFO (pattern);
6549 /* We should never instantiate a nested class before its enclosing
6550 class; we need to look up the nested class by name before we can
6551 instantiate it, and that lookup should instantiate the enclosing
6552 class. */
6553 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
6554 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
6555 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
6557 base_list = NULL_TREE;
6558 if (BINFO_N_BASE_BINFOS (pbinfo))
6560 tree pbase_binfo;
6561 tree context = TYPE_CONTEXT (type);
6562 tree pushed_scope;
6563 int i;
6565 /* We must enter the scope containing the type, as that is where
6566 the accessibility of types named in dependent bases are
6567 looked up from. */
6568 pushed_scope = push_scope (context ? context : global_namespace);
6570 /* Substitute into each of the bases to determine the actual
6571 basetypes. */
6572 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
6574 tree base;
6575 tree access = BINFO_BASE_ACCESS (pbinfo, i);
6576 tree expanded_bases = NULL_TREE;
6577 int idx, len = 1;
6579 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
6581 expanded_bases =
6582 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
6583 args, tf_error, NULL_TREE);
6584 if (expanded_bases == error_mark_node)
6585 continue;
6587 len = TREE_VEC_LENGTH (expanded_bases);
6590 for (idx = 0; idx < len; idx++)
6592 if (expanded_bases)
6593 /* Extract the already-expanded base class. */
6594 base = TREE_VEC_ELT (expanded_bases, idx);
6595 else
6596 /* Substitute to figure out the base class. */
6597 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
6598 NULL_TREE);
6600 if (base == error_mark_node)
6601 continue;
6603 base_list = tree_cons (access, base, base_list);
6604 if (BINFO_VIRTUAL_P (pbase_binfo))
6605 TREE_TYPE (base_list) = integer_type_node;
6609 /* The list is now in reverse order; correct that. */
6610 base_list = nreverse (base_list);
6612 if (pushed_scope)
6613 pop_scope (pushed_scope);
6615 /* Now call xref_basetypes to set up all the base-class
6616 information. */
6617 xref_basetypes (type, base_list);
6620 /* Now that our base classes are set up, enter the scope of the
6621 class, so that name lookups into base classes, etc. will work
6622 correctly. This is precisely analogous to what we do in
6623 begin_class_definition when defining an ordinary non-template
6624 class. */
6625 pushclass (type);
6627 /* Now members are processed in the order of declaration. */
6628 for (member = CLASSTYPE_DECL_LIST (pattern);
6629 member; member = TREE_CHAIN (member))
6631 tree t = TREE_VALUE (member);
6633 if (TREE_PURPOSE (member))
6635 if (TYPE_P (t))
6637 /* Build new CLASSTYPE_NESTED_UTDS. */
6639 tree newtag;
6640 bool class_template_p;
6642 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
6643 && TYPE_LANG_SPECIFIC (t)
6644 && CLASSTYPE_IS_TEMPLATE (t));
6645 /* If the member is a class template, then -- even after
6646 substitution -- there may be dependent types in the
6647 template argument list for the class. We increment
6648 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6649 that function will assume that no types are dependent
6650 when outside of a template. */
6651 if (class_template_p)
6652 ++processing_template_decl;
6653 newtag = tsubst (t, args, tf_error, NULL_TREE);
6654 if (class_template_p)
6655 --processing_template_decl;
6656 if (newtag == error_mark_node)
6657 continue;
6659 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
6661 tree name = TYPE_IDENTIFIER (t);
6663 if (class_template_p)
6664 /* Unfortunately, lookup_template_class sets
6665 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6666 instantiation (i.e., for the type of a member
6667 template class nested within a template class.)
6668 This behavior is required for
6669 maybe_process_partial_specialization to work
6670 correctly, but is not accurate in this case;
6671 the TAG is not an instantiation of anything.
6672 (The corresponding TEMPLATE_DECL is an
6673 instantiation, but the TYPE is not.) */
6674 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
6676 /* Now, we call pushtag to put this NEWTAG into the scope of
6677 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6678 pushtag calling push_template_decl. We don't have to do
6679 this for enums because it will already have been done in
6680 tsubst_enum. */
6681 if (name)
6682 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
6683 pushtag (name, newtag, /*tag_scope=*/ts_current);
6686 else if (TREE_CODE (t) == FUNCTION_DECL
6687 || DECL_FUNCTION_TEMPLATE_P (t))
6689 /* Build new TYPE_METHODS. */
6690 tree r;
6692 if (TREE_CODE (t) == TEMPLATE_DECL)
6693 ++processing_template_decl;
6694 r = tsubst (t, args, tf_error, NULL_TREE);
6695 if (TREE_CODE (t) == TEMPLATE_DECL)
6696 --processing_template_decl;
6697 set_current_access_from_decl (r);
6698 finish_member_declaration (r);
6700 else
6702 /* Build new TYPE_FIELDS. */
6703 if (TREE_CODE (t) == STATIC_ASSERT)
6705 tree condition =
6706 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
6707 tf_warning_or_error, NULL_TREE,
6708 /*integral_constant_expression_p=*/true);
6709 finish_static_assert (condition,
6710 STATIC_ASSERT_MESSAGE (t),
6711 STATIC_ASSERT_SOURCE_LOCATION (t),
6712 /*member_p=*/true);
6714 else if (TREE_CODE (t) != CONST_DECL)
6716 tree r;
6718 /* The the file and line for this declaration, to
6719 assist in error message reporting. Since we
6720 called push_tinst_level above, we don't need to
6721 restore these. */
6722 input_location = DECL_SOURCE_LOCATION (t);
6724 if (TREE_CODE (t) == TEMPLATE_DECL)
6725 ++processing_template_decl;
6726 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
6727 if (TREE_CODE (t) == TEMPLATE_DECL)
6728 --processing_template_decl;
6729 if (TREE_CODE (r) == VAR_DECL)
6731 /* In [temp.inst]:
6733 [t]he initialization (and any associated
6734 side-effects) of a static data member does
6735 not occur unless the static data member is
6736 itself used in a way that requires the
6737 definition of the static data member to
6738 exist.
6740 Therefore, we do not substitute into the
6741 initialized for the static data member here. */
6742 finish_static_data_member_decl
6744 /*init=*/NULL_TREE,
6745 /*init_const_expr_p=*/false,
6746 /*asmspec_tree=*/NULL_TREE,
6747 /*flags=*/0);
6748 if (DECL_INITIALIZED_IN_CLASS_P (r))
6749 check_static_variable_definition (r, TREE_TYPE (r));
6751 else if (TREE_CODE (r) == FIELD_DECL)
6753 /* Determine whether R has a valid type and can be
6754 completed later. If R is invalid, then it is
6755 replaced by error_mark_node so that it will not be
6756 added to TYPE_FIELDS. */
6757 tree rtype = TREE_TYPE (r);
6758 if (can_complete_type_without_circularity (rtype))
6759 complete_type (rtype);
6761 if (!COMPLETE_TYPE_P (rtype))
6763 cxx_incomplete_type_error (r, rtype);
6764 r = error_mark_node;
6768 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6769 such a thing will already have been added to the field
6770 list by tsubst_enum in finish_member_declaration in the
6771 CLASSTYPE_NESTED_UTDS case above. */
6772 if (!(TREE_CODE (r) == TYPE_DECL
6773 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
6774 && DECL_ARTIFICIAL (r)))
6776 set_current_access_from_decl (r);
6777 finish_member_declaration (r);
6782 else
6784 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
6786 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6788 tree friend_type = t;
6789 bool adjust_processing_template_decl = false;
6791 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6793 /* template <class T> friend class C; */
6794 friend_type = tsubst_friend_class (friend_type, args);
6795 adjust_processing_template_decl = true;
6797 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6799 /* template <class T> friend class C::D; */
6800 friend_type = tsubst (friend_type, args,
6801 tf_warning_or_error, NULL_TREE);
6802 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6803 friend_type = TREE_TYPE (friend_type);
6804 adjust_processing_template_decl = true;
6806 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6808 /* This could be either
6810 friend class T::C;
6812 when dependent_type_p is false or
6814 template <class U> friend class T::C;
6816 otherwise. */
6817 friend_type = tsubst (friend_type, args,
6818 tf_warning_or_error, NULL_TREE);
6819 /* Bump processing_template_decl for correct
6820 dependent_type_p calculation. */
6821 ++processing_template_decl;
6822 if (dependent_type_p (friend_type))
6823 adjust_processing_template_decl = true;
6824 --processing_template_decl;
6826 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6827 && hidden_name_p (TYPE_NAME (friend_type)))
6829 /* friend class C;
6831 where C hasn't been declared yet. Let's lookup name
6832 from namespace scope directly, bypassing any name that
6833 come from dependent base class. */
6834 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6836 /* The call to xref_tag_from_type does injection for friend
6837 classes. */
6838 push_nested_namespace (ns);
6839 friend_type =
6840 xref_tag_from_type (friend_type, NULL_TREE,
6841 /*tag_scope=*/ts_current);
6842 pop_nested_namespace (ns);
6844 else if (uses_template_parms (friend_type))
6845 /* friend class C<T>; */
6846 friend_type = tsubst (friend_type, args,
6847 tf_warning_or_error, NULL_TREE);
6848 /* Otherwise it's
6850 friend class C;
6852 where C is already declared or
6854 friend class C<int>;
6856 We don't have to do anything in these cases. */
6858 if (adjust_processing_template_decl)
6859 /* Trick make_friend_class into realizing that the friend
6860 we're adding is a template, not an ordinary class. It's
6861 important that we use make_friend_class since it will
6862 perform some error-checking and output cross-reference
6863 information. */
6864 ++processing_template_decl;
6866 if (friend_type != error_mark_node)
6867 make_friend_class (type, friend_type, /*complain=*/false);
6869 if (adjust_processing_template_decl)
6870 --processing_template_decl;
6872 else
6874 /* Build new DECL_FRIENDLIST. */
6875 tree r;
6877 /* The the file and line for this declaration, to
6878 assist in error message reporting. Since we
6879 called push_tinst_level above, we don't need to
6880 restore these. */
6881 input_location = DECL_SOURCE_LOCATION (t);
6883 if (TREE_CODE (t) == TEMPLATE_DECL)
6885 ++processing_template_decl;
6886 push_deferring_access_checks (dk_no_check);
6889 r = tsubst_friend_function (t, args);
6890 add_friend (type, r, /*complain=*/false);
6891 if (TREE_CODE (t) == TEMPLATE_DECL)
6893 pop_deferring_access_checks ();
6894 --processing_template_decl;
6900 /* Set the file and line number information to whatever is given for
6901 the class itself. This puts error messages involving generated
6902 implicit functions at a predictable point, and the same point
6903 that would be used for non-template classes. */
6904 input_location = DECL_SOURCE_LOCATION (typedecl);
6906 unreverse_member_declarations (type);
6907 finish_struct_1 (type);
6908 TYPE_BEING_DEFINED (type) = 0;
6910 /* Now that the class is complete, instantiate default arguments for
6911 any member functions. We don't do this earlier because the
6912 default arguments may reference members of the class. */
6913 if (!PRIMARY_TEMPLATE_P (template))
6914 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6915 if (TREE_CODE (t) == FUNCTION_DECL
6916 /* Implicitly generated member functions will not have template
6917 information; they are not instantiations, but instead are
6918 created "fresh" for each instantiation. */
6919 && DECL_TEMPLATE_INFO (t))
6920 tsubst_default_arguments (t);
6922 popclass ();
6923 pop_from_top_level ();
6924 pop_deferring_access_checks ();
6925 pop_tinst_level ();
6927 /* The vtable for a template class can be emitted in any translation
6928 unit in which the class is instantiated. When there is no key
6929 method, however, finish_struct_1 will already have added TYPE to
6930 the keyed_classes list. */
6931 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6932 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6934 return type;
6937 static tree
6938 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6940 tree r;
6942 if (!t)
6943 r = t;
6944 else if (TYPE_P (t))
6945 r = tsubst (t, args, complain, in_decl);
6946 else
6948 r = tsubst_expr (t, args, complain, in_decl,
6949 /*integral_constant_expression_p=*/true);
6950 r = fold_non_dependent_expr (r);
6952 return r;
6955 /* Substitute ARGS into T, which is an pack expansion
6956 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
6957 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
6958 (if only a partial substitution could be performed) or
6959 ERROR_MARK_NODE if there was an error. */
6960 tree
6961 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
6962 tree in_decl)
6964 tree pattern;
6965 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
6966 tree first_arg_pack; int i, len = -1;
6967 tree result;
6968 int incomplete = 0;
6970 gcc_assert (PACK_EXPANSION_P (t));
6971 pattern = PACK_EXPANSION_PATTERN (t);
6973 /* Determine the argument packs that will instantiate the parameter
6974 packs used in the expansion expression. While we're at it,
6975 compute the number of arguments to be expanded and make sure it
6976 is consistent. */
6977 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6978 pack = TREE_CHAIN (pack))
6980 tree parm_pack = TREE_VALUE (pack);
6981 tree arg_pack = NULL_TREE;
6982 tree orig_arg = NULL_TREE;
6984 if (TREE_CODE (parm_pack) == PARM_DECL)
6986 if (local_specializations)
6987 arg_pack = retrieve_local_specialization (parm_pack);
6989 else
6991 int level, idx, levels;
6992 template_parm_level_and_index (parm_pack, &level, &idx);
6994 levels = TMPL_ARGS_DEPTH (args);
6995 if (level <= levels)
6996 arg_pack = TMPL_ARG (args, level, idx);
6999 orig_arg = arg_pack;
7000 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7001 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7003 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7004 /* This can only happen if we forget to expand an argument
7005 pack somewhere else. Just return an error, silently. */
7007 result = make_tree_vec (1);
7008 TREE_VEC_ELT (result, 0) = error_mark_node;
7009 return result;
7012 if (arg_pack)
7014 int my_len =
7015 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7017 /* It's all-or-nothing with incomplete argument packs. */
7018 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7019 return error_mark_node;
7021 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7022 incomplete = 1;
7024 if (len < 0)
7026 len = my_len;
7027 first_arg_pack = arg_pack;
7029 else if (len != my_len)
7031 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7032 error ("mismatched argument pack lengths while expanding "
7033 "%<%T%>",
7034 pattern);
7035 else
7036 error ("mismatched argument pack lengths while expanding "
7037 "%<%E%>",
7038 pattern);
7039 return error_mark_node;
7042 /* Keep track of the parameter packs and their corresponding
7043 argument packs. */
7044 packs = tree_cons (parm_pack, arg_pack, packs);
7045 TREE_TYPE (packs) = orig_arg;
7047 else
7048 /* We can't substitute for this parameter pack. */
7049 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7050 TREE_VALUE (pack),
7051 unsubstituted_packs);
7054 /* We cannot expand this expansion expression, because we don't have
7055 all of the argument packs we need. Substitute into the pattern
7056 and return a PACK_EXPANSION_*. The caller will need to deal with
7057 that. */
7058 if (unsubstituted_packs)
7059 return make_pack_expansion (tsubst (pattern, args, complain,
7060 in_decl));
7062 /* We could not find any argument packs that work. */
7063 if (len < 0)
7064 return error_mark_node;
7066 /* For each argument in each argument pack, substitute into the
7067 pattern. */
7068 result = make_tree_vec (len + incomplete);
7069 for (i = 0; i < len + incomplete; ++i)
7071 /* For parameter pack, change the substitution of the parameter
7072 pack to the ith argument in its argument pack, then expand
7073 the pattern. */
7074 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7076 tree parm = TREE_PURPOSE (pack);
7078 if (TREE_CODE (parm) == PARM_DECL)
7080 /* Select the Ith argument from the pack. */
7081 tree arg = make_node (ARGUMENT_PACK_SELECT);
7082 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7083 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7084 mark_used (parm);
7085 register_local_specialization (arg, parm);
7087 else
7089 tree value = parm;
7090 int idx, level;
7091 template_parm_level_and_index (parm, &level, &idx);
7093 if (i < len)
7095 /* Select the Ith argument from the pack. */
7096 value = make_node (ARGUMENT_PACK_SELECT);
7097 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7098 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7101 /* Update the corresponding argument. */
7102 TMPL_ARG (args, level, idx) = value;
7106 /* Substitute into the PATTERN with the altered arguments. */
7107 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7108 TREE_VEC_ELT (result, i) =
7109 tsubst_expr (pattern, args, complain, in_decl,
7110 /*integral_constant_expression_p=*/false);
7111 else
7112 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7114 if (i == len)
7115 /* When we have incomplete argument packs, the last "expanded"
7116 result is itself a pack expansion, which allows us
7117 to deduce more arguments. */
7118 TREE_VEC_ELT (result, i) =
7119 make_pack_expansion (TREE_VEC_ELT (result, i));
7121 if (TREE_VEC_ELT (result, i) == error_mark_node)
7123 result = error_mark_node;
7124 break;
7128 /* Update ARGS to restore the substitution from parameter packs to
7129 their argument packs. */
7130 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7132 tree parm = TREE_PURPOSE (pack);
7134 if (TREE_CODE (parm) == PARM_DECL)
7135 register_local_specialization (TREE_TYPE (pack), parm);
7136 else
7138 int idx, level;
7139 template_parm_level_and_index (parm, &level, &idx);
7141 /* Update the corresponding argument. */
7142 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7143 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7144 TREE_TYPE (pack);
7145 else
7146 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7150 return result;
7153 /* Substitute ARGS into the vector or list of template arguments T. */
7155 static tree
7156 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7158 tree orig_t = t;
7159 int len = TREE_VEC_LENGTH (t);
7160 int need_new = 0, i, expanded_len_adjust = 0, out;
7161 tree *elts = (tree *) alloca (len * sizeof (tree));
7163 for (i = 0; i < len; i++)
7165 tree orig_arg = TREE_VEC_ELT (t, i);
7166 tree new_arg;
7168 if (TREE_CODE (orig_arg) == TREE_VEC)
7169 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7170 else if (PACK_EXPANSION_P (orig_arg))
7172 /* Substitute into an expansion expression. */
7173 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7175 if (TREE_CODE (new_arg) == TREE_VEC)
7176 /* Add to the expanded length adjustment the number of
7177 expanded arguments. We subtract one from this
7178 measurement, because the argument pack expression
7179 itself is already counted as 1 in
7180 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7181 the argument pack is empty. */
7182 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7184 else if (ARGUMENT_PACK_P (orig_arg))
7186 /* Substitute into each of the arguments. */
7187 new_arg = make_node (TREE_CODE (orig_arg));
7189 SET_ARGUMENT_PACK_ARGS (
7190 new_arg,
7191 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7192 args, complain, in_decl));
7194 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7195 new_arg = error_mark_node;
7197 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7198 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7199 complain, in_decl);
7200 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7202 if (TREE_TYPE (new_arg) == error_mark_node)
7203 new_arg = error_mark_node;
7206 else
7207 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7209 if (new_arg == error_mark_node)
7210 return error_mark_node;
7212 elts[i] = new_arg;
7213 if (new_arg != orig_arg)
7214 need_new = 1;
7217 if (!need_new)
7218 return t;
7220 /* Make space for the expanded arguments coming from template
7221 argument packs. */
7222 t = make_tree_vec (len + expanded_len_adjust);
7223 for (i = 0, out = 0; i < len; i++)
7225 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7226 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7227 && TREE_CODE (elts[i]) == TREE_VEC)
7229 int idx;
7231 /* Now expand the template argument pack "in place". */
7232 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7233 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7235 else
7237 TREE_VEC_ELT (t, out) = elts[i];
7238 out++;
7242 return t;
7245 /* Return the result of substituting ARGS into the template parameters
7246 given by PARMS. If there are m levels of ARGS and m + n levels of
7247 PARMS, then the result will contain n levels of PARMS. For
7248 example, if PARMS is `template <class T> template <class U>
7249 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7250 result will be `template <int*, double, class V>'. */
7252 static tree
7253 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7255 tree r = NULL_TREE;
7256 tree* new_parms;
7258 /* When substituting into a template, we must set
7259 PROCESSING_TEMPLATE_DECL as the template parameters may be
7260 dependent if they are based on one-another, and the dependency
7261 predicates are short-circuit outside of templates. */
7262 ++processing_template_decl;
7264 for (new_parms = &r;
7265 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7266 new_parms = &(TREE_CHAIN (*new_parms)),
7267 parms = TREE_CHAIN (parms))
7269 tree new_vec =
7270 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7271 int i;
7273 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7275 tree tuple;
7276 tree default_value;
7277 tree parm_decl;
7279 if (parms == error_mark_node)
7280 continue;
7282 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7284 if (tuple == error_mark_node)
7285 continue;
7287 default_value = TREE_PURPOSE (tuple);
7288 parm_decl = TREE_VALUE (tuple);
7290 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7291 if (TREE_CODE (parm_decl) == PARM_DECL
7292 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7293 parm_decl = error_mark_node;
7294 default_value = tsubst_template_arg (default_value, args,
7295 complain, NULL_TREE);
7297 tuple = build_tree_list (default_value, parm_decl);
7298 TREE_VEC_ELT (new_vec, i) = tuple;
7301 *new_parms =
7302 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7303 - TMPL_ARGS_DEPTH (args)),
7304 new_vec, NULL_TREE);
7307 --processing_template_decl;
7309 return r;
7312 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7313 type T. If T is not an aggregate or enumeration type, it is
7314 handled as if by tsubst. IN_DECL is as for tsubst. If
7315 ENTERING_SCOPE is nonzero, T is the context for a template which
7316 we are presently tsubst'ing. Return the substituted value. */
7318 static tree
7319 tsubst_aggr_type (tree t,
7320 tree args,
7321 tsubst_flags_t complain,
7322 tree in_decl,
7323 int entering_scope)
7325 if (t == NULL_TREE)
7326 return NULL_TREE;
7328 switch (TREE_CODE (t))
7330 case RECORD_TYPE:
7331 if (TYPE_PTRMEMFUNC_P (t))
7332 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7334 /* Else fall through. */
7335 case ENUMERAL_TYPE:
7336 case UNION_TYPE:
7337 if (TYPE_TEMPLATE_INFO (t))
7339 tree argvec;
7340 tree context;
7341 tree r;
7342 bool saved_skip_evaluation;
7344 /* In "sizeof(X<I>)" we need to evaluate "I". */
7345 saved_skip_evaluation = skip_evaluation;
7346 skip_evaluation = false;
7348 /* First, determine the context for the type we are looking
7349 up. */
7350 context = TYPE_CONTEXT (t);
7351 if (context)
7352 context = tsubst_aggr_type (context, args, complain,
7353 in_decl, /*entering_scope=*/1);
7355 /* Then, figure out what arguments are appropriate for the
7356 type we are trying to find. For example, given:
7358 template <class T> struct S;
7359 template <class T, class U> void f(T, U) { S<U> su; }
7361 and supposing that we are instantiating f<int, double>,
7362 then our ARGS will be {int, double}, but, when looking up
7363 S we only want {double}. */
7364 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7365 complain, in_decl);
7366 if (argvec == error_mark_node)
7367 r = error_mark_node;
7368 else
7370 r = lookup_template_class (t, argvec, in_decl, context,
7371 entering_scope, complain);
7372 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7375 skip_evaluation = saved_skip_evaluation;
7377 return r;
7379 else
7380 /* This is not a template type, so there's nothing to do. */
7381 return t;
7383 default:
7384 return tsubst (t, args, complain, in_decl);
7388 /* Substitute into the default argument ARG (a default argument for
7389 FN), which has the indicated TYPE. */
7391 tree
7392 tsubst_default_argument (tree fn, tree type, tree arg)
7394 tree saved_class_ptr = NULL_TREE;
7395 tree saved_class_ref = NULL_TREE;
7397 /* This default argument came from a template. Instantiate the
7398 default argument here, not in tsubst. In the case of
7399 something like:
7401 template <class T>
7402 struct S {
7403 static T t();
7404 void f(T = t());
7407 we must be careful to do name lookup in the scope of S<T>,
7408 rather than in the current class. */
7409 push_access_scope (fn);
7410 /* The "this" pointer is not valid in a default argument. */
7411 if (cfun)
7413 saved_class_ptr = current_class_ptr;
7414 cp_function_chain->x_current_class_ptr = NULL_TREE;
7415 saved_class_ref = current_class_ref;
7416 cp_function_chain->x_current_class_ref = NULL_TREE;
7419 push_deferring_access_checks(dk_no_deferred);
7420 /* The default argument expression may cause implicitly defined
7421 member functions to be synthesized, which will result in garbage
7422 collection. We must treat this situation as if we were within
7423 the body of function so as to avoid collecting live data on the
7424 stack. */
7425 ++function_depth;
7426 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
7427 tf_warning_or_error, NULL_TREE,
7428 /*integral_constant_expression_p=*/false);
7429 --function_depth;
7430 pop_deferring_access_checks();
7432 /* Restore the "this" pointer. */
7433 if (cfun)
7435 cp_function_chain->x_current_class_ptr = saved_class_ptr;
7436 cp_function_chain->x_current_class_ref = saved_class_ref;
7439 pop_access_scope (fn);
7441 /* Make sure the default argument is reasonable. */
7442 arg = check_default_argument (type, arg);
7444 return arg;
7447 /* Substitute into all the default arguments for FN. */
7449 static void
7450 tsubst_default_arguments (tree fn)
7452 tree arg;
7453 tree tmpl_args;
7455 tmpl_args = DECL_TI_ARGS (fn);
7457 /* If this function is not yet instantiated, we certainly don't need
7458 its default arguments. */
7459 if (uses_template_parms (tmpl_args))
7460 return;
7462 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
7463 arg;
7464 arg = TREE_CHAIN (arg))
7465 if (TREE_PURPOSE (arg))
7466 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
7467 TREE_VALUE (arg),
7468 TREE_PURPOSE (arg));
7471 /* Substitute the ARGS into the T, which is a _DECL. Return the
7472 result of the substitution. Issue error and warning messages under
7473 control of COMPLAIN. */
7475 static tree
7476 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
7478 location_t saved_loc;
7479 tree r = NULL_TREE;
7480 tree in_decl = t;
7482 /* Set the filename and linenumber to improve error-reporting. */
7483 saved_loc = input_location;
7484 input_location = DECL_SOURCE_LOCATION (t);
7486 switch (TREE_CODE (t))
7488 case TEMPLATE_DECL:
7490 /* We can get here when processing a member function template,
7491 member class template, and template template parameter of
7492 a template class. */
7493 tree decl = DECL_TEMPLATE_RESULT (t);
7494 tree spec;
7495 tree tmpl_args;
7496 tree full_args;
7498 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7500 /* Template template parameter is treated here. */
7501 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7502 if (new_type == error_mark_node)
7503 return error_mark_node;
7505 r = copy_decl (t);
7506 TREE_CHAIN (r) = NULL_TREE;
7507 TREE_TYPE (r) = new_type;
7508 DECL_TEMPLATE_RESULT (r)
7509 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
7510 DECL_TEMPLATE_PARMS (r)
7511 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7512 complain);
7513 TYPE_NAME (new_type) = r;
7514 break;
7517 /* We might already have an instance of this template.
7518 The ARGS are for the surrounding class type, so the
7519 full args contain the tsubst'd args for the context,
7520 plus the innermost args from the template decl. */
7521 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
7522 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
7523 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7524 /* Because this is a template, the arguments will still be
7525 dependent, even after substitution. If
7526 PROCESSING_TEMPLATE_DECL is not set, the dependency
7527 predicates will short-circuit. */
7528 ++processing_template_decl;
7529 full_args = tsubst_template_args (tmpl_args, args,
7530 complain, in_decl);
7531 --processing_template_decl;
7532 if (full_args == error_mark_node)
7533 return error_mark_node;
7535 /* tsubst_template_args doesn't copy the vector if
7536 nothing changed. But, *something* should have
7537 changed. */
7538 gcc_assert (full_args != tmpl_args);
7540 spec = retrieve_specialization (t, full_args,
7541 /*class_specializations_p=*/true);
7542 if (spec != NULL_TREE)
7544 r = spec;
7545 break;
7548 /* Make a new template decl. It will be similar to the
7549 original, but will record the current template arguments.
7550 We also create a new function declaration, which is just
7551 like the old one, but points to this new template, rather
7552 than the old one. */
7553 r = copy_decl (t);
7554 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
7555 TREE_CHAIN (r) = NULL_TREE;
7557 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
7559 if (TREE_CODE (decl) == TYPE_DECL)
7561 tree new_type;
7562 ++processing_template_decl;
7563 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7564 --processing_template_decl;
7565 if (new_type == error_mark_node)
7566 return error_mark_node;
7568 TREE_TYPE (r) = new_type;
7569 CLASSTYPE_TI_TEMPLATE (new_type) = r;
7570 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
7571 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
7572 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
7574 else
7576 tree new_decl;
7577 ++processing_template_decl;
7578 new_decl = tsubst (decl, args, complain, in_decl);
7579 --processing_template_decl;
7580 if (new_decl == error_mark_node)
7581 return error_mark_node;
7583 DECL_TEMPLATE_RESULT (r) = new_decl;
7584 DECL_TI_TEMPLATE (new_decl) = r;
7585 TREE_TYPE (r) = TREE_TYPE (new_decl);
7586 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
7587 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
7590 SET_DECL_IMPLICIT_INSTANTIATION (r);
7591 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
7592 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
7594 /* The template parameters for this new template are all the
7595 template parameters for the old template, except the
7596 outermost level of parameters. */
7597 DECL_TEMPLATE_PARMS (r)
7598 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7599 complain);
7601 if (PRIMARY_TEMPLATE_P (t))
7602 DECL_PRIMARY_TEMPLATE (r) = r;
7604 if (TREE_CODE (decl) != TYPE_DECL)
7605 /* Record this non-type partial instantiation. */
7606 register_specialization (r, t,
7607 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
7608 false);
7610 break;
7612 case FUNCTION_DECL:
7614 tree ctx;
7615 tree argvec = NULL_TREE;
7616 tree *friends;
7617 tree gen_tmpl;
7618 tree type;
7619 int member;
7620 int args_depth;
7621 int parms_depth;
7623 /* Nobody should be tsubst'ing into non-template functions. */
7624 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
7626 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
7628 tree spec;
7629 bool dependent_p;
7631 /* If T is not dependent, just return it. We have to
7632 increment PROCESSING_TEMPLATE_DECL because
7633 value_dependent_expression_p assumes that nothing is
7634 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7635 ++processing_template_decl;
7636 dependent_p = value_dependent_expression_p (t);
7637 --processing_template_decl;
7638 if (!dependent_p)
7639 return t;
7641 /* Calculate the most general template of which R is a
7642 specialization, and the complete set of arguments used to
7643 specialize R. */
7644 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7645 argvec = tsubst_template_args (DECL_TI_ARGS
7646 (DECL_TEMPLATE_RESULT (gen_tmpl)),
7647 args, complain, in_decl);
7649 /* Check to see if we already have this specialization. */
7650 spec = retrieve_specialization (gen_tmpl, argvec,
7651 /*class_specializations_p=*/false);
7653 if (spec)
7655 r = spec;
7656 break;
7659 /* We can see more levels of arguments than parameters if
7660 there was a specialization of a member template, like
7661 this:
7663 template <class T> struct S { template <class U> void f(); }
7664 template <> template <class U> void S<int>::f(U);
7666 Here, we'll be substituting into the specialization,
7667 because that's where we can find the code we actually
7668 want to generate, but we'll have enough arguments for
7669 the most general template.
7671 We also deal with the peculiar case:
7673 template <class T> struct S {
7674 template <class U> friend void f();
7676 template <class U> void f() {}
7677 template S<int>;
7678 template void f<double>();
7680 Here, the ARGS for the instantiation of will be {int,
7681 double}. But, we only need as many ARGS as there are
7682 levels of template parameters in CODE_PATTERN. We are
7683 careful not to get fooled into reducing the ARGS in
7684 situations like:
7686 template <class T> struct S { template <class U> void f(U); }
7687 template <class T> template <> void S<T>::f(int) {}
7689 which we can spot because the pattern will be a
7690 specialization in this case. */
7691 args_depth = TMPL_ARGS_DEPTH (args);
7692 parms_depth =
7693 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
7694 if (args_depth > parms_depth
7695 && !DECL_TEMPLATE_SPECIALIZATION (t))
7696 args = get_innermost_template_args (args, parms_depth);
7698 else
7700 /* This special case arises when we have something like this:
7702 template <class T> struct S {
7703 friend void f<int>(int, double);
7706 Here, the DECL_TI_TEMPLATE for the friend declaration
7707 will be an IDENTIFIER_NODE. We are being called from
7708 tsubst_friend_function, and we want only to create a
7709 new decl (R) with appropriate types so that we can call
7710 determine_specialization. */
7711 gen_tmpl = NULL_TREE;
7714 if (DECL_CLASS_SCOPE_P (t))
7716 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
7717 member = 2;
7718 else
7719 member = 1;
7720 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
7721 complain, t, /*entering_scope=*/1);
7723 else
7725 member = 0;
7726 ctx = DECL_CONTEXT (t);
7728 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7729 if (type == error_mark_node)
7730 return error_mark_node;
7732 /* We do NOT check for matching decls pushed separately at this
7733 point, as they may not represent instantiations of this
7734 template, and in any case are considered separate under the
7735 discrete model. */
7736 r = copy_decl (t);
7737 DECL_USE_TEMPLATE (r) = 0;
7738 TREE_TYPE (r) = type;
7739 /* Clear out the mangled name and RTL for the instantiation. */
7740 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7741 SET_DECL_RTL (r, NULL_RTX);
7742 DECL_INITIAL (r) = NULL_TREE;
7743 DECL_CONTEXT (r) = ctx;
7745 if (member && DECL_CONV_FN_P (r))
7746 /* Type-conversion operator. Reconstruct the name, in
7747 case it's the name of one of the template's parameters. */
7748 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
7750 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
7751 complain, t);
7752 DECL_RESULT (r) = NULL_TREE;
7754 TREE_STATIC (r) = 0;
7755 TREE_PUBLIC (r) = TREE_PUBLIC (t);
7756 DECL_EXTERNAL (r) = 1;
7757 /* If this is an instantiation of a function with internal
7758 linkage, we already know what object file linkage will be
7759 assigned to the instantiation. */
7760 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
7761 DECL_DEFER_OUTPUT (r) = 0;
7762 TREE_CHAIN (r) = NULL_TREE;
7763 DECL_PENDING_INLINE_INFO (r) = 0;
7764 DECL_PENDING_INLINE_P (r) = 0;
7765 DECL_SAVED_TREE (r) = NULL_TREE;
7766 TREE_USED (r) = 0;
7767 if (DECL_CLONED_FUNCTION (r))
7769 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
7770 args, complain, t);
7771 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
7772 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
7775 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
7776 this in the special friend case mentioned above where
7777 GEN_TMPL is NULL. */
7778 if (gen_tmpl)
7780 DECL_TEMPLATE_INFO (r)
7781 = tree_cons (gen_tmpl, argvec, NULL_TREE);
7782 SET_DECL_IMPLICIT_INSTANTIATION (r);
7783 register_specialization (r, gen_tmpl, argvec, false);
7785 /* We're not supposed to instantiate default arguments
7786 until they are called, for a template. But, for a
7787 declaration like:
7789 template <class T> void f ()
7790 { extern void g(int i = T()); }
7792 we should do the substitution when the template is
7793 instantiated. We handle the member function case in
7794 instantiate_class_template since the default arguments
7795 might refer to other members of the class. */
7796 if (!member
7797 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7798 && !uses_template_parms (argvec))
7799 tsubst_default_arguments (r);
7801 else
7802 DECL_TEMPLATE_INFO (r) = NULL_TREE;
7804 /* Copy the list of befriending classes. */
7805 for (friends = &DECL_BEFRIENDING_CLASSES (r);
7806 *friends;
7807 friends = &TREE_CHAIN (*friends))
7809 *friends = copy_node (*friends);
7810 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
7811 args, complain,
7812 in_decl);
7815 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
7817 maybe_retrofit_in_chrg (r);
7818 if (DECL_CONSTRUCTOR_P (r))
7819 grok_ctor_properties (ctx, r);
7820 /* If this is an instantiation of a member template, clone it.
7821 If it isn't, that'll be handled by
7822 clone_constructors_and_destructors. */
7823 if (PRIMARY_TEMPLATE_P (gen_tmpl))
7824 clone_function_decl (r, /*update_method_vec_p=*/0);
7826 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
7827 && !grok_op_properties (r, (complain & tf_error) != 0))
7828 return error_mark_node;
7830 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
7831 SET_DECL_FRIEND_CONTEXT (r,
7832 tsubst (DECL_FRIEND_CONTEXT (t),
7833 args, complain, in_decl));
7835 /* Possibly limit visibility based on template args. */
7836 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7837 if (DECL_VISIBILITY_SPECIFIED (t))
7839 DECL_VISIBILITY_SPECIFIED (r) = 0;
7840 DECL_ATTRIBUTES (r)
7841 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7843 determine_visibility (r);
7845 break;
7847 case PARM_DECL:
7849 tree type = NULL_TREE;
7850 int i, len = 1;
7851 tree expanded_types = NULL_TREE;
7852 tree prev_r = NULL_TREE;
7853 tree first_r = NULL_TREE;
7855 if (FUNCTION_PARAMETER_PACK_P (t))
7857 /* If there is a local specialization that isn't a
7858 parameter pack, it means that we're doing a "simple"
7859 substitution from inside tsubst_pack_expansion. Just
7860 return the local specialization (which will be a single
7861 parm). */
7862 tree spec = NULL_TREE;
7863 if (local_specializations)
7864 spec = retrieve_local_specialization (t);
7865 if (spec
7866 && TREE_CODE (spec) == PARM_DECL
7867 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
7868 return spec;
7870 /* Expand the TYPE_PACK_EXPANSION that provides the types for
7871 the parameters in this function parameter pack. */
7872 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
7873 complain, in_decl);
7874 if (TREE_CODE (expanded_types) == TREE_VEC)
7876 len = TREE_VEC_LENGTH (expanded_types);
7878 /* Zero-length parameter packs are boring. Just substitute
7879 into the chain. */
7880 if (len == 0)
7881 return tsubst (TREE_CHAIN (t), args, complain,
7882 TREE_CHAIN (t));
7884 else
7886 /* All we did was update the type. Make a note of that. */
7887 type = expanded_types;
7888 expanded_types = NULL_TREE;
7892 /* Loop through all of the parameter's we'll build. When T is
7893 a function parameter pack, LEN is the number of expanded
7894 types in EXPANDED_TYPES; otherwise, LEN is 1. */
7895 r = NULL_TREE;
7896 for (i = 0; i < len; ++i)
7898 prev_r = r;
7899 r = copy_node (t);
7900 if (DECL_TEMPLATE_PARM_P (t))
7901 SET_DECL_TEMPLATE_PARM_P (r);
7903 if (expanded_types)
7904 /* We're on the Ith parameter of the function parameter
7905 pack. */
7907 /* Get the Ith type. */
7908 type = TREE_VEC_ELT (expanded_types, i);
7910 if (DECL_NAME (r))
7911 /* Rename the parameter to include the index. */
7912 DECL_NAME (r) =
7913 make_ith_pack_parameter_name (DECL_NAME (r), i);
7915 else if (!type)
7916 /* We're dealing with a normal parameter. */
7917 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7919 type = type_decays_to (type);
7920 TREE_TYPE (r) = type;
7921 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7923 if (DECL_INITIAL (r))
7925 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
7926 DECL_INITIAL (r) = TREE_TYPE (r);
7927 else
7928 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
7929 complain, in_decl);
7932 DECL_CONTEXT (r) = NULL_TREE;
7934 if (!DECL_TEMPLATE_PARM_P (r))
7935 DECL_ARG_TYPE (r) = type_passed_as (type);
7937 /* Keep track of the first new parameter we
7938 generate. That's what will be returned to the
7939 caller. */
7940 if (!first_r)
7941 first_r = r;
7943 /* Build a proper chain of parameters when substituting
7944 into a function parameter pack. */
7945 if (prev_r)
7946 TREE_CHAIN (prev_r) = r;
7949 if (TREE_CHAIN (t))
7950 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
7951 complain, TREE_CHAIN (t));
7953 /* FIRST_R contains the start of the chain we've built. */
7954 r = first_r;
7956 break;
7958 case FIELD_DECL:
7960 tree type;
7962 r = copy_decl (t);
7963 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7964 if (type == error_mark_node)
7965 return error_mark_node;
7966 TREE_TYPE (r) = type;
7967 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7969 /* DECL_INITIAL gives the number of bits in a bit-field. */
7970 DECL_INITIAL (r)
7971 = tsubst_expr (DECL_INITIAL (t), args,
7972 complain, in_decl,
7973 /*integral_constant_expression_p=*/true);
7974 /* We don't have to set DECL_CONTEXT here; it is set by
7975 finish_member_declaration. */
7976 TREE_CHAIN (r) = NULL_TREE;
7977 if (VOID_TYPE_P (type))
7978 error ("instantiation of %q+D as type %qT", r, type);
7980 break;
7982 case USING_DECL:
7983 /* We reach here only for member using decls. */
7984 if (DECL_DEPENDENT_P (t))
7986 r = do_class_using_decl
7987 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
7988 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
7989 if (!r)
7990 r = error_mark_node;
7992 else
7994 r = copy_node (t);
7995 TREE_CHAIN (r) = NULL_TREE;
7997 break;
7999 case TYPE_DECL:
8000 case VAR_DECL:
8002 tree argvec = NULL_TREE;
8003 tree gen_tmpl = NULL_TREE;
8004 tree spec;
8005 tree tmpl = NULL_TREE;
8006 tree ctx;
8007 tree type = NULL_TREE;
8008 bool local_p;
8010 if (TREE_CODE (t) == TYPE_DECL)
8012 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8013 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
8014 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8016 /* If this is the canonical decl, we don't have to
8017 mess with instantiations, and often we can't (for
8018 typename, template type parms and such). Note that
8019 TYPE_NAME is not correct for the above test if
8020 we've copied the type for a typedef. */
8021 r = TYPE_NAME (type);
8022 break;
8026 /* Check to see if we already have the specialization we
8027 need. */
8028 spec = NULL_TREE;
8029 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8031 /* T is a static data member or namespace-scope entity.
8032 We have to substitute into namespace-scope variables
8033 (even though such entities are never templates) because
8034 of cases like:
8036 template <class T> void f() { extern T t; }
8038 where the entity referenced is not known until
8039 instantiation time. */
8040 local_p = false;
8041 ctx = DECL_CONTEXT (t);
8042 if (DECL_CLASS_SCOPE_P (t))
8044 ctx = tsubst_aggr_type (ctx, args,
8045 complain,
8046 in_decl, /*entering_scope=*/1);
8047 /* If CTX is unchanged, then T is in fact the
8048 specialization we want. That situation occurs when
8049 referencing a static data member within in its own
8050 class. We can use pointer equality, rather than
8051 same_type_p, because DECL_CONTEXT is always
8052 canonical. */
8053 if (ctx == DECL_CONTEXT (t))
8054 spec = t;
8057 if (!spec)
8059 tmpl = DECL_TI_TEMPLATE (t);
8060 gen_tmpl = most_general_template (tmpl);
8061 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8062 spec = (retrieve_specialization
8063 (gen_tmpl, argvec,
8064 /*class_specializations_p=*/false));
8067 else
8069 /* A local variable. */
8070 local_p = true;
8071 /* Subsequent calls to pushdecl will fill this in. */
8072 ctx = NULL_TREE;
8073 spec = retrieve_local_specialization (t);
8075 /* If we already have the specialization we need, there is
8076 nothing more to do. */
8077 if (spec)
8079 r = spec;
8080 break;
8083 /* Create a new node for the specialization we need. */
8084 r = copy_decl (t);
8085 if (TREE_CODE (r) == VAR_DECL)
8087 /* Even if the original location is out of scope, the
8088 newly substituted one is not. */
8089 DECL_DEAD_FOR_LOCAL (r) = 0;
8090 DECL_INITIALIZED_P (r) = 0;
8091 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8092 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8093 if (type == error_mark_node)
8094 return error_mark_node;
8095 if (TREE_CODE (type) == FUNCTION_TYPE)
8097 /* It may seem that this case cannot occur, since:
8099 typedef void f();
8100 void g() { f x; }
8102 declares a function, not a variable. However:
8104 typedef void f();
8105 template <typename T> void g() { T t; }
8106 template void g<f>();
8108 is an attempt to declare a variable with function
8109 type. */
8110 error ("variable %qD has function type",
8111 /* R is not yet sufficiently initialized, so we
8112 just use its name. */
8113 DECL_NAME (r));
8114 return error_mark_node;
8116 type = complete_type (type);
8117 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8118 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8119 type = check_var_type (DECL_NAME (r), type);
8121 if (DECL_HAS_VALUE_EXPR_P (t))
8123 tree ve = DECL_VALUE_EXPR (t);
8124 ve = tsubst_expr (ve, args, complain, in_decl,
8125 /*constant_expression_p=*/false);
8126 SET_DECL_VALUE_EXPR (r, ve);
8129 else if (DECL_SELF_REFERENCE_P (t))
8130 SET_DECL_SELF_REFERENCE_P (r);
8131 TREE_TYPE (r) = type;
8132 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8133 DECL_CONTEXT (r) = ctx;
8134 /* Clear out the mangled name and RTL for the instantiation. */
8135 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8136 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8137 SET_DECL_RTL (r, NULL_RTX);
8138 /* The initializer must not be expanded until it is required;
8139 see [temp.inst]. */
8140 DECL_INITIAL (r) = NULL_TREE;
8141 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8142 SET_DECL_RTL (r, NULL_RTX);
8143 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8144 if (TREE_CODE (r) == VAR_DECL)
8146 /* Possibly limit visibility based on template args. */
8147 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8148 if (DECL_VISIBILITY_SPECIFIED (t))
8150 DECL_VISIBILITY_SPECIFIED (r) = 0;
8151 DECL_ATTRIBUTES (r)
8152 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8154 determine_visibility (r);
8157 if (!local_p)
8159 /* A static data member declaration is always marked
8160 external when it is declared in-class, even if an
8161 initializer is present. We mimic the non-template
8162 processing here. */
8163 DECL_EXTERNAL (r) = 1;
8165 register_specialization (r, gen_tmpl, argvec, false);
8166 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8167 SET_DECL_IMPLICIT_INSTANTIATION (r);
8169 else
8170 register_local_specialization (r, t);
8172 TREE_CHAIN (r) = NULL_TREE;
8173 layout_decl (r, 0);
8175 break;
8177 default:
8178 gcc_unreachable ();
8181 /* Restore the file and line information. */
8182 input_location = saved_loc;
8184 return r;
8187 /* Substitute into the ARG_TYPES of a function type. */
8189 static tree
8190 tsubst_arg_types (tree arg_types,
8191 tree args,
8192 tsubst_flags_t complain,
8193 tree in_decl)
8195 tree remaining_arg_types;
8196 tree type = NULL_TREE;
8197 int i = 1;
8198 tree expanded_args = NULL_TREE;
8199 tree default_arg;
8201 if (!arg_types || arg_types == void_list_node)
8202 return arg_types;
8204 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8205 args, complain, in_decl);
8206 if (remaining_arg_types == error_mark_node)
8207 return error_mark_node;
8209 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8211 /* For a pack expansion, perform substitution on the
8212 entire expression. Later on, we'll handle the arguments
8213 one-by-one. */
8214 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8215 args, complain, in_decl);
8217 if (TREE_CODE (expanded_args) == TREE_VEC)
8218 /* So that we'll spin through the parameters, one by one. */
8219 i = TREE_VEC_LENGTH (expanded_args);
8220 else
8222 /* We only partially substituted into the parameter
8223 pack. Our type is TYPE_PACK_EXPANSION. */
8224 type = expanded_args;
8225 expanded_args = NULL_TREE;
8229 while (i > 0) {
8230 --i;
8232 if (expanded_args)
8233 type = TREE_VEC_ELT (expanded_args, i);
8234 else if (!type)
8235 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8237 if (type == error_mark_node)
8238 return error_mark_node;
8239 if (VOID_TYPE_P (type))
8241 if (complain & tf_error)
8243 error ("invalid parameter type %qT", type);
8244 if (in_decl)
8245 error ("in declaration %q+D", in_decl);
8247 return error_mark_node;
8250 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8251 top-level qualifiers as required. */
8252 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8254 /* We do not substitute into default arguments here. The standard
8255 mandates that they be instantiated only when needed, which is
8256 done in build_over_call. */
8257 default_arg = TREE_PURPOSE (arg_types);
8259 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8261 /* We've instantiated a template before its default arguments
8262 have been parsed. This can happen for a nested template
8263 class, and is not an error unless we require the default
8264 argument in a call of this function. */
8265 remaining_arg_types =
8266 tree_cons (default_arg, type, remaining_arg_types);
8267 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8268 remaining_arg_types);
8270 else
8271 remaining_arg_types =
8272 hash_tree_cons (default_arg, type, remaining_arg_types);
8275 return remaining_arg_types;
8278 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8279 *not* handle the exception-specification for FNTYPE, because the
8280 initial substitution of explicitly provided template parameters
8281 during argument deduction forbids substitution into the
8282 exception-specification:
8284 [temp.deduct]
8286 All references in the function type of the function template to the
8287 corresponding template parameters are replaced by the specified tem-
8288 plate argument values. If a substitution in a template parameter or
8289 in the function type of the function template results in an invalid
8290 type, type deduction fails. [Note: The equivalent substitution in
8291 exception specifications is done only when the function is instanti-
8292 ated, at which point a program is ill-formed if the substitution
8293 results in an invalid type.] */
8295 static tree
8296 tsubst_function_type (tree t,
8297 tree args,
8298 tsubst_flags_t complain,
8299 tree in_decl)
8301 tree return_type;
8302 tree arg_types;
8303 tree fntype;
8305 /* The TYPE_CONTEXT is not used for function/method types. */
8306 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8308 /* Substitute the return type. */
8309 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8310 if (return_type == error_mark_node)
8311 return error_mark_node;
8312 /* The standard does not presently indicate that creation of a
8313 function type with an invalid return type is a deduction failure.
8314 However, that is clearly analogous to creating an array of "void"
8315 or a reference to a reference. This is core issue #486. */
8316 if (TREE_CODE (return_type) == ARRAY_TYPE
8317 || TREE_CODE (return_type) == FUNCTION_TYPE)
8319 if (complain & tf_error)
8321 if (TREE_CODE (return_type) == ARRAY_TYPE)
8322 error ("function returning an array");
8323 else
8324 error ("function returning a function");
8326 return error_mark_node;
8329 /* Substitute the argument types. */
8330 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8331 complain, in_decl);
8332 if (arg_types == error_mark_node)
8333 return error_mark_node;
8335 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
8336 && in_decl != NULL_TREE
8337 && !TREE_NO_WARNING (in_decl)
8338 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
8339 warning (OPT_Wreturn_type,
8340 "type qualifiers ignored on function return type");
8342 /* Construct a new type node and return it. */
8343 if (TREE_CODE (t) == FUNCTION_TYPE)
8344 fntype = build_function_type (return_type, arg_types);
8345 else
8347 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8348 if (! IS_AGGR_TYPE (r))
8350 /* [temp.deduct]
8352 Type deduction may fail for any of the following
8353 reasons:
8355 -- Attempting to create "pointer to member of T" when T
8356 is not a class type. */
8357 if (complain & tf_error)
8358 error ("creating pointer to member function of non-class type %qT",
8360 return error_mark_node;
8363 fntype = build_method_type_directly (r, return_type,
8364 TREE_CHAIN (arg_types));
8366 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8367 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8369 return fntype;
8372 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8373 ARGS into that specification, and return the substituted
8374 specification. If there is no specification, return NULL_TREE. */
8376 static tree
8377 tsubst_exception_specification (tree fntype,
8378 tree args,
8379 tsubst_flags_t complain,
8380 tree in_decl)
8382 tree specs;
8383 tree new_specs;
8385 specs = TYPE_RAISES_EXCEPTIONS (fntype);
8386 new_specs = NULL_TREE;
8387 if (specs)
8389 if (! TREE_VALUE (specs))
8390 new_specs = specs;
8391 else
8392 while (specs)
8394 tree spec;
8395 int i, len = 1;
8396 tree expanded_specs = NULL_TREE;
8398 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
8400 /* Expand the pack expansion type. */
8401 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
8402 args, complain,
8403 in_decl);
8404 len = TREE_VEC_LENGTH (expanded_specs);
8407 for (i = 0; i < len; ++i)
8409 if (expanded_specs)
8410 spec = TREE_VEC_ELT (expanded_specs, i);
8411 else
8412 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
8413 if (spec == error_mark_node)
8414 return spec;
8415 new_specs = add_exception_specifier (new_specs, spec,
8416 complain);
8419 specs = TREE_CHAIN (specs);
8422 return new_specs;
8425 /* Take the tree structure T and replace template parameters used
8426 therein with the argument vector ARGS. IN_DECL is an associated
8427 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8428 Issue error and warning messages under control of COMPLAIN. Note
8429 that we must be relatively non-tolerant of extensions here, in
8430 order to preserve conformance; if we allow substitutions that
8431 should not be allowed, we may allow argument deductions that should
8432 not succeed, and therefore report ambiguous overload situations
8433 where there are none. In theory, we could allow the substitution,
8434 but indicate that it should have failed, and allow our caller to
8435 make sure that the right thing happens, but we don't try to do this
8436 yet.
8438 This function is used for dealing with types, decls and the like;
8439 for expressions, use tsubst_expr or tsubst_copy. */
8441 static tree
8442 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8444 tree type, r;
8446 if (t == NULL_TREE || t == error_mark_node
8447 || t == integer_type_node
8448 || t == void_type_node
8449 || t == char_type_node
8450 || t == unknown_type_node
8451 || TREE_CODE (t) == NAMESPACE_DECL)
8452 return t;
8454 if (DECL_P (t))
8455 return tsubst_decl (t, args, complain);
8457 if (TREE_CODE (t) == IDENTIFIER_NODE)
8458 type = IDENTIFIER_TYPE_VALUE (t);
8459 else
8460 type = TREE_TYPE (t);
8462 gcc_assert (type != unknown_type_node);
8464 if (type
8465 && TREE_CODE (t) != TYPENAME_TYPE
8466 && TREE_CODE (t) != IDENTIFIER_NODE
8467 && TREE_CODE (t) != FUNCTION_TYPE
8468 && TREE_CODE (t) != METHOD_TYPE)
8469 type = tsubst (type, args, complain, in_decl);
8470 if (type == error_mark_node)
8471 return error_mark_node;
8473 switch (TREE_CODE (t))
8475 case RECORD_TYPE:
8476 case UNION_TYPE:
8477 case ENUMERAL_TYPE:
8478 return tsubst_aggr_type (t, args, complain, in_decl,
8479 /*entering_scope=*/0);
8481 case ERROR_MARK:
8482 case IDENTIFIER_NODE:
8483 case VOID_TYPE:
8484 case REAL_TYPE:
8485 case COMPLEX_TYPE:
8486 case VECTOR_TYPE:
8487 case BOOLEAN_TYPE:
8488 case INTEGER_CST:
8489 case REAL_CST:
8490 case STRING_CST:
8491 return t;
8493 case INTEGER_TYPE:
8494 if (t == integer_type_node)
8495 return t;
8497 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
8498 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
8499 return t;
8502 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
8504 max = tsubst_expr (omax, args, complain, in_decl,
8505 /*integral_constant_expression_p=*/false);
8506 max = fold_decl_constant_value (max);
8508 if (TREE_CODE (max) != INTEGER_CST
8509 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
8510 && !at_function_scope_p ())
8512 if (complain & tf_error)
8513 error ("array bound is not an integer constant");
8514 return error_mark_node;
8517 /* [temp.deduct]
8519 Type deduction may fail for any of the following
8520 reasons:
8522 Attempting to create an array with a size that is
8523 zero or negative. */
8524 if (integer_zerop (max) && !(complain & tf_error))
8525 /* We must fail if performing argument deduction (as
8526 indicated by the state of complain), so that
8527 another substitution can be found. */
8528 return error_mark_node;
8529 else if (TREE_CODE (max) == INTEGER_CST
8530 && INT_CST_LT (max, integer_zero_node))
8532 if (complain & tf_error)
8533 error ("creating array with negative size (%qE)", max);
8535 return error_mark_node;
8538 return compute_array_index_type (NULL_TREE, max);
8541 case TEMPLATE_TYPE_PARM:
8542 case TEMPLATE_TEMPLATE_PARM:
8543 case BOUND_TEMPLATE_TEMPLATE_PARM:
8544 case TEMPLATE_PARM_INDEX:
8546 int idx;
8547 int level;
8548 int levels;
8549 tree arg = NULL_TREE;
8551 r = NULL_TREE;
8553 gcc_assert (TREE_VEC_LENGTH (args) > 0);
8554 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8555 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
8556 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8558 idx = TEMPLATE_TYPE_IDX (t);
8559 level = TEMPLATE_TYPE_LEVEL (t);
8561 else
8563 idx = TEMPLATE_PARM_IDX (t);
8564 level = TEMPLATE_PARM_LEVEL (t);
8567 levels = TMPL_ARGS_DEPTH (args);
8568 if (level <= levels)
8570 arg = TMPL_ARG (args, level, idx);
8572 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
8573 /* See through ARGUMENT_PACK_SELECT arguments. */
8574 arg = ARGUMENT_PACK_SELECT_ARG (arg);
8577 if (arg == error_mark_node)
8578 return error_mark_node;
8579 else if (arg != NULL_TREE)
8581 if (ARGUMENT_PACK_P (arg))
8582 /* If ARG is an argument pack, we don't actually want to
8583 perform a substitution here, because substitutions
8584 for argument packs are only done
8585 element-by-element. We can get to this point when
8586 substituting the type of a non-type template
8587 parameter pack, when that type actually contains
8588 template parameter packs from an outer template, e.g.,
8590 template<typename... Types> struct A {
8591 template<Types... Values> struct B { };
8592 }; */
8593 return t;
8595 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
8597 int quals;
8598 gcc_assert (TYPE_P (arg));
8600 /* cv-quals from the template are discarded when
8601 substituting in a function or reference type. */
8602 if (TREE_CODE (arg) == FUNCTION_TYPE
8603 || TREE_CODE (arg) == METHOD_TYPE
8604 || TREE_CODE (arg) == REFERENCE_TYPE)
8605 quals = cp_type_quals (arg);
8606 else
8607 quals = cp_type_quals (arg) | cp_type_quals (t);
8609 return cp_build_qualified_type_real
8610 (arg, quals, complain | tf_ignore_bad_quals);
8612 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8614 /* We are processing a type constructed from a
8615 template template parameter. */
8616 tree argvec = tsubst (TYPE_TI_ARGS (t),
8617 args, complain, in_decl);
8618 if (argvec == error_mark_node)
8619 return error_mark_node;
8621 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
8622 are resolving nested-types in the signature of a
8623 member function templates. Otherwise ARG is a
8624 TEMPLATE_DECL and is the real template to be
8625 instantiated. */
8626 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8627 arg = TYPE_NAME (arg);
8629 r = lookup_template_class (arg,
8630 argvec, in_decl,
8631 DECL_CONTEXT (arg),
8632 /*entering_scope=*/0,
8633 complain);
8634 return cp_build_qualified_type_real
8635 (r, TYPE_QUALS (t), complain);
8637 else
8638 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
8639 return arg;
8642 if (level == 1)
8643 /* This can happen during the attempted tsubst'ing in
8644 unify. This means that we don't yet have any information
8645 about the template parameter in question. */
8646 return t;
8648 /* If we get here, we must have been looking at a parm for a
8649 more deeply nested template. Make a new version of this
8650 template parameter, but with a lower level. */
8651 switch (TREE_CODE (t))
8653 case TEMPLATE_TYPE_PARM:
8654 case TEMPLATE_TEMPLATE_PARM:
8655 case BOUND_TEMPLATE_TEMPLATE_PARM:
8656 if (cp_type_quals (t))
8658 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
8659 r = cp_build_qualified_type_real
8660 (r, cp_type_quals (t),
8661 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8662 ? tf_ignore_bad_quals : 0));
8664 else
8666 r = copy_type (t);
8667 TEMPLATE_TYPE_PARM_INDEX (r)
8668 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
8669 r, levels);
8670 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
8671 TYPE_MAIN_VARIANT (r) = r;
8672 TYPE_POINTER_TO (r) = NULL_TREE;
8673 TYPE_REFERENCE_TO (r) = NULL_TREE;
8675 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
8676 /* We have reduced the level of the template
8677 template parameter, but not the levels of its
8678 template parameters, so canonical_type_parameter
8679 will not be able to find the canonical template
8680 template parameter for this level. Thus, we
8681 require structural equality checking to compare
8682 TEMPLATE_TEMPLATE_PARMs. */
8683 SET_TYPE_STRUCTURAL_EQUALITY (r);
8684 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
8685 SET_TYPE_STRUCTURAL_EQUALITY (r);
8686 else
8687 TYPE_CANONICAL (r) = canonical_type_parameter (r);
8689 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8691 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
8692 complain, in_decl);
8693 if (argvec == error_mark_node)
8694 return error_mark_node;
8696 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
8697 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
8700 break;
8702 case TEMPLATE_PARM_INDEX:
8703 r = reduce_template_parm_level (t, type, levels);
8704 break;
8706 default:
8707 gcc_unreachable ();
8710 return r;
8713 case TREE_LIST:
8715 tree purpose, value, chain;
8717 if (t == void_list_node)
8718 return t;
8720 purpose = TREE_PURPOSE (t);
8721 if (purpose)
8723 purpose = tsubst (purpose, args, complain, in_decl);
8724 if (purpose == error_mark_node)
8725 return error_mark_node;
8727 value = TREE_VALUE (t);
8728 if (value)
8730 value = tsubst (value, args, complain, in_decl);
8731 if (value == error_mark_node)
8732 return error_mark_node;
8734 chain = TREE_CHAIN (t);
8735 if (chain && chain != void_type_node)
8737 chain = tsubst (chain, args, complain, in_decl);
8738 if (chain == error_mark_node)
8739 return error_mark_node;
8741 if (purpose == TREE_PURPOSE (t)
8742 && value == TREE_VALUE (t)
8743 && chain == TREE_CHAIN (t))
8744 return t;
8745 return hash_tree_cons (purpose, value, chain);
8748 case TREE_BINFO:
8749 /* We should never be tsubsting a binfo. */
8750 gcc_unreachable ();
8752 case TREE_VEC:
8753 /* A vector of template arguments. */
8754 gcc_assert (!type);
8755 return tsubst_template_args (t, args, complain, in_decl);
8757 case POINTER_TYPE:
8758 case REFERENCE_TYPE:
8760 enum tree_code code;
8762 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8763 return t;
8765 code = TREE_CODE (t);
8768 /* [temp.deduct]
8770 Type deduction may fail for any of the following
8771 reasons:
8773 -- Attempting to create a pointer to reference type.
8774 -- Attempting to create a reference to a reference type or
8775 a reference to void.
8777 Core issue 106 says that creating a reference to a reference
8778 during instantiation is no longer a cause for failure. We
8779 only enforce this check in strict C++98 mode. */
8780 if ((TREE_CODE (type) == REFERENCE_TYPE
8781 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
8782 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
8784 static location_t last_loc;
8786 /* We keep track of the last time we issued this error
8787 message to avoid spewing a ton of messages during a
8788 single bad template instantiation. */
8789 if (complain & tf_error
8790 #ifdef USE_MAPPED_LOCATION
8791 && last_loc != input_location
8792 #else
8793 && (last_loc.line != input_line
8794 || last_loc.file != input_filename)
8795 #endif
8798 if (TREE_CODE (type) == VOID_TYPE)
8799 error ("forming reference to void");
8800 else
8801 error ("forming %s to reference type %qT",
8802 (code == POINTER_TYPE) ? "pointer" : "reference",
8803 type);
8804 last_loc = input_location;
8807 return error_mark_node;
8809 else if (code == POINTER_TYPE)
8811 r = build_pointer_type (type);
8812 if (TREE_CODE (type) == METHOD_TYPE)
8813 r = build_ptrmemfunc_type (r);
8815 else if (TREE_CODE (type) == REFERENCE_TYPE)
8816 /* In C++0x, during template argument substitution, when there is an
8817 attempt to create a reference to a reference type, reference
8818 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
8820 "If a template-argument for a template-parameter T names a type
8821 that is a reference to a type A, an attempt to create the type
8822 'lvalue reference to cv T' creates the type 'lvalue reference to
8823 A,' while an attempt to create the type type rvalue reference to
8824 cv T' creates the type T"
8826 r = cp_build_reference_type
8827 (TREE_TYPE (type),
8828 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
8829 else
8830 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
8831 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8833 if (r != error_mark_node)
8834 /* Will this ever be needed for TYPE_..._TO values? */
8835 layout_type (r);
8837 return r;
8839 case OFFSET_TYPE:
8841 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
8842 if (r == error_mark_node || !IS_AGGR_TYPE (r))
8844 /* [temp.deduct]
8846 Type deduction may fail for any of the following
8847 reasons:
8849 -- Attempting to create "pointer to member of T" when T
8850 is not a class type. */
8851 if (complain & tf_error)
8852 error ("creating pointer to member of non-class type %qT", r);
8853 return error_mark_node;
8855 if (TREE_CODE (type) == REFERENCE_TYPE)
8857 if (complain & tf_error)
8858 error ("creating pointer to member reference type %qT", type);
8859 return error_mark_node;
8861 if (TREE_CODE (type) == VOID_TYPE)
8863 if (complain & tf_error)
8864 error ("creating pointer to member of type void");
8865 return error_mark_node;
8867 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
8868 if (TREE_CODE (type) == FUNCTION_TYPE)
8870 /* The type of the implicit object parameter gets its
8871 cv-qualifiers from the FUNCTION_TYPE. */
8872 tree method_type;
8873 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
8874 cp_type_quals (type));
8875 tree memptr;
8876 method_type = build_method_type_directly (this_type,
8877 TREE_TYPE (type),
8878 TYPE_ARG_TYPES (type));
8879 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
8880 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
8881 complain);
8883 else
8884 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
8885 TYPE_QUALS (t),
8886 complain);
8888 case FUNCTION_TYPE:
8889 case METHOD_TYPE:
8891 tree fntype;
8892 tree specs;
8893 fntype = tsubst_function_type (t, args, complain, in_decl);
8894 if (fntype == error_mark_node)
8895 return error_mark_node;
8897 /* Substitute the exception specification. */
8898 specs = tsubst_exception_specification (t, args, complain,
8899 in_decl);
8900 if (specs == error_mark_node)
8901 return error_mark_node;
8902 if (specs)
8903 fntype = build_exception_variant (fntype, specs);
8904 return fntype;
8906 case ARRAY_TYPE:
8908 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
8909 if (domain == error_mark_node)
8910 return error_mark_node;
8912 /* As an optimization, we avoid regenerating the array type if
8913 it will obviously be the same as T. */
8914 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8915 return t;
8917 /* These checks should match the ones in grokdeclarator.
8919 [temp.deduct]
8921 The deduction may fail for any of the following reasons:
8923 -- Attempting to create an array with an element type that
8924 is void, a function type, or a reference type, or [DR337]
8925 an abstract class type. */
8926 if (TREE_CODE (type) == VOID_TYPE
8927 || TREE_CODE (type) == FUNCTION_TYPE
8928 || TREE_CODE (type) == REFERENCE_TYPE)
8930 if (complain & tf_error)
8931 error ("creating array of %qT", type);
8932 return error_mark_node;
8934 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
8936 if (complain & tf_error)
8937 error ("creating array of %qT, which is an abstract class type",
8938 type);
8939 return error_mark_node;
8942 r = build_cplus_array_type (type, domain);
8943 return r;
8946 case PLUS_EXPR:
8947 case MINUS_EXPR:
8949 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8950 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
8952 if (e1 == error_mark_node || e2 == error_mark_node)
8953 return error_mark_node;
8955 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
8958 case NEGATE_EXPR:
8959 case NOP_EXPR:
8961 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8962 if (e == error_mark_node)
8963 return error_mark_node;
8965 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
8968 case TYPENAME_TYPE:
8970 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
8971 in_decl, /*entering_scope=*/1);
8972 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
8973 complain, in_decl);
8975 if (ctx == error_mark_node || f == error_mark_node)
8976 return error_mark_node;
8978 if (!IS_AGGR_TYPE (ctx))
8980 if (complain & tf_error)
8981 error ("%qT is not a class, struct, or union type", ctx);
8982 return error_mark_node;
8984 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
8986 /* Normally, make_typename_type does not require that the CTX
8987 have complete type in order to allow things like:
8989 template <class T> struct S { typename S<T>::X Y; };
8991 But, such constructs have already been resolved by this
8992 point, so here CTX really should have complete type, unless
8993 it's a partial instantiation. */
8994 ctx = complete_type (ctx);
8995 if (!COMPLETE_TYPE_P (ctx))
8997 if (complain & tf_error)
8998 cxx_incomplete_type_error (NULL_TREE, ctx);
8999 return error_mark_node;
9003 f = make_typename_type (ctx, f, typename_type,
9004 (complain & tf_error) | tf_keep_type_decl);
9005 if (f == error_mark_node)
9006 return f;
9007 if (TREE_CODE (f) == TYPE_DECL)
9009 complain |= tf_ignore_bad_quals;
9010 f = TREE_TYPE (f);
9013 if (TREE_CODE (f) != TYPENAME_TYPE)
9015 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9016 error ("%qT resolves to %qT, which is not an enumeration type",
9017 t, f);
9018 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9019 error ("%qT resolves to %qT, which is is not a class type",
9020 t, f);
9023 return cp_build_qualified_type_real
9024 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9027 case UNBOUND_CLASS_TEMPLATE:
9029 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9030 in_decl, /*entering_scope=*/1);
9031 tree name = TYPE_IDENTIFIER (t);
9032 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9034 if (ctx == error_mark_node || name == error_mark_node)
9035 return error_mark_node;
9037 if (parm_list)
9038 parm_list = tsubst_template_parms (parm_list, args, complain);
9039 return make_unbound_class_template (ctx, name, parm_list, complain);
9042 case INDIRECT_REF:
9043 case ADDR_EXPR:
9044 case CALL_EXPR:
9045 gcc_unreachable ();
9047 case ARRAY_REF:
9049 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9050 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9051 /*integral_constant_expression_p=*/false);
9052 if (e1 == error_mark_node || e2 == error_mark_node)
9053 return error_mark_node;
9055 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9058 case SCOPE_REF:
9060 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9061 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9062 if (e1 == error_mark_node || e2 == error_mark_node)
9063 return error_mark_node;
9065 return build_qualified_name (/*type=*/NULL_TREE,
9066 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9069 case TYPEOF_TYPE:
9071 tree type;
9073 type = finish_typeof (tsubst_expr
9074 (TYPEOF_TYPE_EXPR (t), args,
9075 complain, in_decl,
9076 /*integral_constant_expression_p=*/false));
9077 return cp_build_qualified_type_real (type,
9078 cp_type_quals (t)
9079 | cp_type_quals (type),
9080 complain);
9083 case DECLTYPE_TYPE:
9085 tree type;
9087 type =
9088 finish_decltype_type (tsubst_expr
9089 (DECLTYPE_TYPE_EXPR (t), args,
9090 complain, in_decl,
9091 /*integral_constant_expression_p=*/false),
9092 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9093 return cp_build_qualified_type_real (type,
9094 cp_type_quals (t)
9095 | cp_type_quals (type),
9096 complain);
9099 case TYPE_ARGUMENT_PACK:
9100 case NONTYPE_ARGUMENT_PACK:
9102 tree r = make_node (TREE_CODE (t));
9103 tree packed_out =
9104 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9105 args,
9106 complain,
9107 in_decl);
9108 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9110 /* For template nontype argument packs, also substitute into
9111 the type. */
9112 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9113 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9115 return r;
9117 break;
9119 default:
9120 sorry ("use of %qs in template",
9121 tree_code_name [(int) TREE_CODE (t)]);
9122 return error_mark_node;
9126 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9127 type of the expression on the left-hand side of the "." or "->"
9128 operator. */
9130 static tree
9131 tsubst_baselink (tree baselink, tree object_type,
9132 tree args, tsubst_flags_t complain, tree in_decl)
9134 tree name;
9135 tree qualifying_scope;
9136 tree fns;
9137 tree optype;
9138 tree template_args = 0;
9139 bool template_id_p = false;
9141 /* A baselink indicates a function from a base class. Both the
9142 BASELINK_ACCESS_BINFO and the base class referenced may
9143 indicate bases of the template class, rather than the
9144 instantiated class. In addition, lookups that were not
9145 ambiguous before may be ambiguous now. Therefore, we perform
9146 the lookup again. */
9147 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9148 qualifying_scope = tsubst (qualifying_scope, args,
9149 complain, in_decl);
9150 fns = BASELINK_FUNCTIONS (baselink);
9151 optype = BASELINK_OPTYPE (baselink);
9152 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9154 template_id_p = true;
9155 template_args = TREE_OPERAND (fns, 1);
9156 fns = TREE_OPERAND (fns, 0);
9157 if (template_args)
9158 template_args = tsubst_template_args (template_args, args,
9159 complain, in_decl);
9161 name = DECL_NAME (get_first_fn (fns));
9162 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9164 /* If lookup found a single function, mark it as used at this
9165 point. (If it lookup found multiple functions the one selected
9166 later by overload resolution will be marked as used at that
9167 point.) */
9168 if (BASELINK_P (baselink))
9169 fns = BASELINK_FUNCTIONS (baselink);
9170 if (!template_id_p && !really_overloaded_fn (fns))
9171 mark_used (OVL_CURRENT (fns));
9173 /* Add back the template arguments, if present. */
9174 if (BASELINK_P (baselink) && template_id_p)
9175 BASELINK_FUNCTIONS (baselink)
9176 = build_nt (TEMPLATE_ID_EXPR,
9177 BASELINK_FUNCTIONS (baselink),
9178 template_args);
9179 /* Update the conversion operator type. */
9180 BASELINK_OPTYPE (baselink)
9181 = tsubst (optype, args, complain, in_decl);
9183 if (!object_type)
9184 object_type = current_class_type;
9185 return adjust_result_of_qualified_name_lookup (baselink,
9186 qualifying_scope,
9187 object_type);
9190 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9191 true if the qualified-id will be a postfix-expression in-and-of
9192 itself; false if more of the postfix-expression follows the
9193 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9194 of "&". */
9196 static tree
9197 tsubst_qualified_id (tree qualified_id, tree args,
9198 tsubst_flags_t complain, tree in_decl,
9199 bool done, bool address_p)
9201 tree expr;
9202 tree scope;
9203 tree name;
9204 bool is_template;
9205 tree template_args;
9207 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9209 /* Figure out what name to look up. */
9210 name = TREE_OPERAND (qualified_id, 1);
9211 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9213 is_template = true;
9214 template_args = TREE_OPERAND (name, 1);
9215 if (template_args)
9216 template_args = tsubst_template_args (template_args, args,
9217 complain, in_decl);
9218 name = TREE_OPERAND (name, 0);
9220 else
9222 is_template = false;
9223 template_args = NULL_TREE;
9226 /* Substitute into the qualifying scope. When there are no ARGS, we
9227 are just trying to simplify a non-dependent expression. In that
9228 case the qualifying scope may be dependent, and, in any case,
9229 substituting will not help. */
9230 scope = TREE_OPERAND (qualified_id, 0);
9231 if (args)
9233 scope = tsubst (scope, args, complain, in_decl);
9234 expr = tsubst_copy (name, args, complain, in_decl);
9236 else
9237 expr = name;
9239 if (dependent_type_p (scope))
9240 return build_qualified_name (/*type=*/NULL_TREE,
9241 scope, expr,
9242 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9244 if (!BASELINK_P (name) && !DECL_P (expr))
9246 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9247 /* If this were actually a destructor call, it would have been
9248 parsed as such by the parser. */
9249 expr = error_mark_node;
9250 else
9251 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9252 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9253 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9255 if (complain & tf_error)
9257 error ("dependent-name %qE is parsed as a non-type, but "
9258 "instantiation yields a type", qualified_id);
9259 inform ("say %<typename %E%> if a type is meant", qualified_id);
9261 return error_mark_node;
9265 if (DECL_P (expr))
9267 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9268 scope);
9269 /* Remember that there was a reference to this entity. */
9270 mark_used (expr);
9273 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9275 if (complain & tf_error)
9276 qualified_name_lookup_error (scope,
9277 TREE_OPERAND (qualified_id, 1),
9278 expr);
9279 return error_mark_node;
9282 if (is_template)
9283 expr = lookup_template_function (expr, template_args);
9285 if (expr == error_mark_node && complain & tf_error)
9286 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9287 expr);
9288 else if (TYPE_P (scope))
9290 expr = (adjust_result_of_qualified_name_lookup
9291 (expr, scope, current_class_type));
9292 expr = (finish_qualified_id_expr
9293 (scope, expr, done, address_p,
9294 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9295 /*template_arg_p=*/false));
9298 /* Expressions do not generally have reference type. */
9299 if (TREE_CODE (expr) != SCOPE_REF
9300 /* However, if we're about to form a pointer-to-member, we just
9301 want the referenced member referenced. */
9302 && TREE_CODE (expr) != OFFSET_REF)
9303 expr = convert_from_reference (expr);
9305 return expr;
9308 /* Like tsubst, but deals with expressions. This function just replaces
9309 template parms; to finish processing the resultant expression, use
9310 tsubst_expr. */
9312 static tree
9313 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9315 enum tree_code code;
9316 tree r;
9318 if (t == NULL_TREE || t == error_mark_node)
9319 return t;
9321 code = TREE_CODE (t);
9323 switch (code)
9325 case PARM_DECL:
9326 r = retrieve_local_specialization (t);
9327 gcc_assert (r != NULL);
9328 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
9329 r = ARGUMENT_PACK_SELECT_ARG (r);
9330 mark_used (r);
9331 return r;
9333 case CONST_DECL:
9335 tree enum_type;
9336 tree v;
9338 if (DECL_TEMPLATE_PARM_P (t))
9339 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
9340 /* There is no need to substitute into namespace-scope
9341 enumerators. */
9342 if (DECL_NAMESPACE_SCOPE_P (t))
9343 return t;
9344 /* If ARGS is NULL, then T is known to be non-dependent. */
9345 if (args == NULL_TREE)
9346 return integral_constant_value (t);
9348 /* Unfortunately, we cannot just call lookup_name here.
9349 Consider:
9351 template <int I> int f() {
9352 enum E { a = I };
9353 struct S { void g() { E e = a; } };
9356 When we instantiate f<7>::S::g(), say, lookup_name is not
9357 clever enough to find f<7>::a. */
9358 enum_type
9359 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
9360 /*entering_scope=*/0);
9362 for (v = TYPE_VALUES (enum_type);
9363 v != NULL_TREE;
9364 v = TREE_CHAIN (v))
9365 if (TREE_PURPOSE (v) == DECL_NAME (t))
9366 return TREE_VALUE (v);
9368 /* We didn't find the name. That should never happen; if
9369 name-lookup found it during preliminary parsing, we
9370 should find it again here during instantiation. */
9371 gcc_unreachable ();
9373 return t;
9375 case FIELD_DECL:
9376 if (DECL_CONTEXT (t))
9378 tree ctx;
9380 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
9381 /*entering_scope=*/1);
9382 if (ctx != DECL_CONTEXT (t))
9384 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
9385 if (!r)
9387 if (complain & tf_error)
9388 error ("using invalid field %qD", t);
9389 return error_mark_node;
9391 return r;
9395 return t;
9397 case VAR_DECL:
9398 case FUNCTION_DECL:
9399 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9400 || local_variable_p (t))
9401 t = tsubst (t, args, complain, in_decl);
9402 mark_used (t);
9403 return t;
9405 case BASELINK:
9406 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
9408 case TEMPLATE_DECL:
9409 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9410 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
9411 args, complain, in_decl);
9412 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
9413 return tsubst (t, args, complain, in_decl);
9414 else if (DECL_CLASS_SCOPE_P (t)
9415 && uses_template_parms (DECL_CONTEXT (t)))
9417 /* Template template argument like the following example need
9418 special treatment:
9420 template <template <class> class TT> struct C {};
9421 template <class T> struct D {
9422 template <class U> struct E {};
9423 C<E> c; // #1
9425 D<int> d; // #2
9427 We are processing the template argument `E' in #1 for
9428 the template instantiation #2. Originally, `E' is a
9429 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9430 have to substitute this with one having context `D<int>'. */
9432 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
9433 return lookup_field (context, DECL_NAME(t), 0, false);
9435 else
9436 /* Ordinary template template argument. */
9437 return t;
9439 case CAST_EXPR:
9440 case REINTERPRET_CAST_EXPR:
9441 case CONST_CAST_EXPR:
9442 case STATIC_CAST_EXPR:
9443 case DYNAMIC_CAST_EXPR:
9444 case NOP_EXPR:
9445 return build1
9446 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9447 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9449 case SIZEOF_EXPR:
9450 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
9452 /* We only want to compute the number of arguments. */
9453 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
9454 complain, in_decl);
9455 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
9457 /* Fall through */
9459 case INDIRECT_REF:
9460 case NEGATE_EXPR:
9461 case TRUTH_NOT_EXPR:
9462 case BIT_NOT_EXPR:
9463 case ADDR_EXPR:
9464 case UNARY_PLUS_EXPR: /* Unary + */
9465 case ALIGNOF_EXPR:
9466 case ARROW_EXPR:
9467 case THROW_EXPR:
9468 case TYPEID_EXPR:
9469 case REALPART_EXPR:
9470 case IMAGPART_EXPR:
9471 return build1
9472 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9473 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9475 case COMPONENT_REF:
9477 tree object;
9478 tree name;
9480 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
9481 name = TREE_OPERAND (t, 1);
9482 if (TREE_CODE (name) == BIT_NOT_EXPR)
9484 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9485 complain, in_decl);
9486 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9488 else if (TREE_CODE (name) == SCOPE_REF
9489 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
9491 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
9492 complain, in_decl);
9493 name = TREE_OPERAND (name, 1);
9494 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9495 complain, in_decl);
9496 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9497 name = build_qualified_name (/*type=*/NULL_TREE,
9498 base, name,
9499 /*template_p=*/false);
9501 else if (TREE_CODE (name) == BASELINK)
9502 name = tsubst_baselink (name,
9503 non_reference (TREE_TYPE (object)),
9504 args, complain,
9505 in_decl);
9506 else
9507 name = tsubst_copy (name, args, complain, in_decl);
9508 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
9511 case PLUS_EXPR:
9512 case MINUS_EXPR:
9513 case MULT_EXPR:
9514 case TRUNC_DIV_EXPR:
9515 case CEIL_DIV_EXPR:
9516 case FLOOR_DIV_EXPR:
9517 case ROUND_DIV_EXPR:
9518 case EXACT_DIV_EXPR:
9519 case BIT_AND_EXPR:
9520 case BIT_IOR_EXPR:
9521 case BIT_XOR_EXPR:
9522 case TRUNC_MOD_EXPR:
9523 case FLOOR_MOD_EXPR:
9524 case TRUTH_ANDIF_EXPR:
9525 case TRUTH_ORIF_EXPR:
9526 case TRUTH_AND_EXPR:
9527 case TRUTH_OR_EXPR:
9528 case RSHIFT_EXPR:
9529 case LSHIFT_EXPR:
9530 case RROTATE_EXPR:
9531 case LROTATE_EXPR:
9532 case EQ_EXPR:
9533 case NE_EXPR:
9534 case MAX_EXPR:
9535 case MIN_EXPR:
9536 case LE_EXPR:
9537 case GE_EXPR:
9538 case LT_EXPR:
9539 case GT_EXPR:
9540 case COMPOUND_EXPR:
9541 case DOTSTAR_EXPR:
9542 case MEMBER_REF:
9543 case PREDECREMENT_EXPR:
9544 case PREINCREMENT_EXPR:
9545 case POSTDECREMENT_EXPR:
9546 case POSTINCREMENT_EXPR:
9547 return build_nt
9548 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9549 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9551 case SCOPE_REF:
9552 return build_qualified_name (/*type=*/NULL_TREE,
9553 tsubst_copy (TREE_OPERAND (t, 0),
9554 args, complain, in_decl),
9555 tsubst_copy (TREE_OPERAND (t, 1),
9556 args, complain, in_decl),
9557 QUALIFIED_NAME_IS_TEMPLATE (t));
9559 case ARRAY_REF:
9560 return build_nt
9561 (ARRAY_REF,
9562 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9563 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9564 NULL_TREE, NULL_TREE);
9566 case CALL_EXPR:
9568 int n = VL_EXP_OPERAND_LENGTH (t);
9569 tree result = build_vl_exp (CALL_EXPR, n);
9570 int i;
9571 for (i = 0; i < n; i++)
9572 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
9573 complain, in_decl);
9574 return result;
9577 case COND_EXPR:
9578 case MODOP_EXPR:
9579 case PSEUDO_DTOR_EXPR:
9581 r = build_nt
9582 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9583 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9584 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9585 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9586 return r;
9589 case NEW_EXPR:
9591 r = build_nt
9592 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9593 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9594 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9595 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
9596 return r;
9599 case DELETE_EXPR:
9601 r = build_nt
9602 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9603 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9604 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
9605 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
9606 return r;
9609 case TEMPLATE_ID_EXPR:
9611 /* Substituted template arguments */
9612 tree fn = TREE_OPERAND (t, 0);
9613 tree targs = TREE_OPERAND (t, 1);
9615 fn = tsubst_copy (fn, args, complain, in_decl);
9616 if (targs)
9617 targs = tsubst_template_args (targs, args, complain, in_decl);
9619 return lookup_template_function (fn, targs);
9622 case TREE_LIST:
9624 tree purpose, value, chain;
9626 if (t == void_list_node)
9627 return t;
9629 purpose = TREE_PURPOSE (t);
9630 if (purpose)
9631 purpose = tsubst_copy (purpose, args, complain, in_decl);
9632 value = TREE_VALUE (t);
9633 if (value)
9634 value = tsubst_copy (value, args, complain, in_decl);
9635 chain = TREE_CHAIN (t);
9636 if (chain && chain != void_type_node)
9637 chain = tsubst_copy (chain, args, complain, in_decl);
9638 if (purpose == TREE_PURPOSE (t)
9639 && value == TREE_VALUE (t)
9640 && chain == TREE_CHAIN (t))
9641 return t;
9642 return tree_cons (purpose, value, chain);
9645 case RECORD_TYPE:
9646 case UNION_TYPE:
9647 case ENUMERAL_TYPE:
9648 case INTEGER_TYPE:
9649 case TEMPLATE_TYPE_PARM:
9650 case TEMPLATE_TEMPLATE_PARM:
9651 case BOUND_TEMPLATE_TEMPLATE_PARM:
9652 case TEMPLATE_PARM_INDEX:
9653 case POINTER_TYPE:
9654 case REFERENCE_TYPE:
9655 case OFFSET_TYPE:
9656 case FUNCTION_TYPE:
9657 case METHOD_TYPE:
9658 case ARRAY_TYPE:
9659 case TYPENAME_TYPE:
9660 case UNBOUND_CLASS_TEMPLATE:
9661 case TYPEOF_TYPE:
9662 case DECLTYPE_TYPE:
9663 case TYPE_DECL:
9664 return tsubst (t, args, complain, in_decl);
9666 case IDENTIFIER_NODE:
9667 if (IDENTIFIER_TYPENAME_P (t))
9669 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9670 return mangle_conv_op_name_for_type (new_type);
9672 else
9673 return t;
9675 case CONSTRUCTOR:
9676 /* This is handled by tsubst_copy_and_build. */
9677 gcc_unreachable ();
9679 case VA_ARG_EXPR:
9680 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
9681 in_decl),
9682 tsubst (TREE_TYPE (t), args, complain, in_decl));
9684 case CLEANUP_POINT_EXPR:
9685 /* We shouldn't have built any of these during initial template
9686 generation. Instead, they should be built during instantiation
9687 in response to the saved STMT_IS_FULL_EXPR_P setting. */
9688 gcc_unreachable ();
9690 case OFFSET_REF:
9691 mark_used (TREE_OPERAND (t, 1));
9692 return t;
9694 case EXPR_PACK_EXPANSION:
9695 error ("invalid use of pack expansion expression");
9696 return error_mark_node;
9698 case NONTYPE_ARGUMENT_PACK:
9699 error ("use %<...%> to expand argument pack");
9700 return error_mark_node;
9702 default:
9703 return t;
9707 /* Like tsubst_copy, but specifically for OpenMP clauses. */
9709 static tree
9710 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
9711 tree in_decl)
9713 tree new_clauses = NULL, nc, oc;
9715 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
9717 nc = copy_node (oc);
9718 OMP_CLAUSE_CHAIN (nc) = new_clauses;
9719 new_clauses = nc;
9721 switch (OMP_CLAUSE_CODE (nc))
9723 case OMP_CLAUSE_PRIVATE:
9724 case OMP_CLAUSE_SHARED:
9725 case OMP_CLAUSE_FIRSTPRIVATE:
9726 case OMP_CLAUSE_LASTPRIVATE:
9727 case OMP_CLAUSE_REDUCTION:
9728 case OMP_CLAUSE_COPYIN:
9729 case OMP_CLAUSE_COPYPRIVATE:
9730 case OMP_CLAUSE_IF:
9731 case OMP_CLAUSE_NUM_THREADS:
9732 case OMP_CLAUSE_SCHEDULE:
9733 OMP_CLAUSE_OPERAND (nc, 0)
9734 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
9735 in_decl, /*integral_constant_expression_p=*/false);
9736 break;
9737 case OMP_CLAUSE_NOWAIT:
9738 case OMP_CLAUSE_ORDERED:
9739 case OMP_CLAUSE_DEFAULT:
9740 break;
9741 default:
9742 gcc_unreachable ();
9746 return finish_omp_clauses (nreverse (new_clauses));
9749 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
9751 static tree
9752 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
9753 tree in_decl)
9755 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
9757 tree purpose, value, chain;
9759 if (t == NULL)
9760 return t;
9762 if (TREE_CODE (t) != TREE_LIST)
9763 return tsubst_copy_and_build (t, args, complain, in_decl,
9764 /*function_p=*/false,
9765 /*integral_constant_expression_p=*/false);
9767 if (t == void_list_node)
9768 return t;
9770 purpose = TREE_PURPOSE (t);
9771 if (purpose)
9772 purpose = RECUR (purpose);
9773 value = TREE_VALUE (t);
9774 if (value)
9775 value = RECUR (value);
9776 chain = TREE_CHAIN (t);
9777 if (chain && chain != void_type_node)
9778 chain = RECUR (chain);
9779 return tree_cons (purpose, value, chain);
9780 #undef RECUR
9783 /* Like tsubst_copy for expressions, etc. but also does semantic
9784 processing. */
9786 static tree
9787 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
9788 bool integral_constant_expression_p)
9790 #define RECUR(NODE) \
9791 tsubst_expr ((NODE), args, complain, in_decl, \
9792 integral_constant_expression_p)
9794 tree stmt, tmp;
9796 if (t == NULL_TREE || t == error_mark_node)
9797 return t;
9799 if (EXPR_HAS_LOCATION (t))
9800 input_location = EXPR_LOCATION (t);
9801 if (STATEMENT_CODE_P (TREE_CODE (t)))
9802 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
9804 switch (TREE_CODE (t))
9806 case STATEMENT_LIST:
9808 tree_stmt_iterator i;
9809 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
9810 RECUR (tsi_stmt (i));
9811 break;
9814 case CTOR_INITIALIZER:
9815 finish_mem_initializers (tsubst_initializer_list
9816 (TREE_OPERAND (t, 0), args));
9817 break;
9819 case RETURN_EXPR:
9820 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
9821 break;
9823 case EXPR_STMT:
9824 tmp = RECUR (EXPR_STMT_EXPR (t));
9825 if (EXPR_STMT_STMT_EXPR_RESULT (t))
9826 finish_stmt_expr_expr (tmp, cur_stmt_expr);
9827 else
9828 finish_expr_stmt (tmp);
9829 break;
9831 case USING_STMT:
9832 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9833 break;
9835 case DECL_EXPR:
9837 tree decl;
9838 tree init;
9840 decl = DECL_EXPR_DECL (t);
9841 if (TREE_CODE (decl) == LABEL_DECL)
9842 finish_label_decl (DECL_NAME (decl));
9843 else if (TREE_CODE (decl) == USING_DECL)
9845 tree scope = USING_DECL_SCOPE (decl);
9846 tree name = DECL_NAME (decl);
9847 tree decl;
9849 scope = RECUR (scope);
9850 decl = lookup_qualified_name (scope, name,
9851 /*is_type_p=*/false,
9852 /*complain=*/false);
9853 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
9854 qualified_name_lookup_error (scope, name, decl);
9855 else
9856 do_local_using_decl (decl, scope, name);
9858 else
9860 init = DECL_INITIAL (decl);
9861 decl = tsubst (decl, args, complain, in_decl);
9862 if (decl != error_mark_node)
9864 /* By marking the declaration as instantiated, we avoid
9865 trying to instantiate it. Since instantiate_decl can't
9866 handle local variables, and since we've already done
9867 all that needs to be done, that's the right thing to
9868 do. */
9869 if (TREE_CODE (decl) == VAR_DECL)
9870 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
9871 if (TREE_CODE (decl) == VAR_DECL
9872 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
9873 /* Anonymous aggregates are a special case. */
9874 finish_anon_union (decl);
9875 else
9877 maybe_push_decl (decl);
9878 if (TREE_CODE (decl) == VAR_DECL
9879 && DECL_PRETTY_FUNCTION_P (decl))
9881 /* For __PRETTY_FUNCTION__ we have to adjust the
9882 initializer. */
9883 const char *const name
9884 = cxx_printable_name (current_function_decl, 2);
9885 init = cp_fname_init (name, &TREE_TYPE (decl));
9887 else
9888 init = RECUR (init);
9889 finish_decl (decl, init, NULL_TREE);
9894 /* A DECL_EXPR can also be used as an expression, in the condition
9895 clause of an if/for/while construct. */
9896 return decl;
9899 case FOR_STMT:
9900 stmt = begin_for_stmt ();
9901 RECUR (FOR_INIT_STMT (t));
9902 finish_for_init_stmt (stmt);
9903 tmp = RECUR (FOR_COND (t));
9904 finish_for_cond (tmp, stmt);
9905 tmp = RECUR (FOR_EXPR (t));
9906 finish_for_expr (tmp, stmt);
9907 RECUR (FOR_BODY (t));
9908 finish_for_stmt (stmt);
9909 break;
9911 case WHILE_STMT:
9912 stmt = begin_while_stmt ();
9913 tmp = RECUR (WHILE_COND (t));
9914 finish_while_stmt_cond (tmp, stmt);
9915 RECUR (WHILE_BODY (t));
9916 finish_while_stmt (stmt);
9917 break;
9919 case DO_STMT:
9920 stmt = begin_do_stmt ();
9921 RECUR (DO_BODY (t));
9922 finish_do_body (stmt);
9923 tmp = RECUR (DO_COND (t));
9924 finish_do_stmt (tmp, stmt);
9925 break;
9927 case IF_STMT:
9928 stmt = begin_if_stmt ();
9929 tmp = RECUR (IF_COND (t));
9930 finish_if_stmt_cond (tmp, stmt);
9931 RECUR (THEN_CLAUSE (t));
9932 finish_then_clause (stmt);
9934 if (ELSE_CLAUSE (t))
9936 begin_else_clause (stmt);
9937 RECUR (ELSE_CLAUSE (t));
9938 finish_else_clause (stmt);
9941 finish_if_stmt (stmt);
9942 break;
9944 case BIND_EXPR:
9945 if (BIND_EXPR_BODY_BLOCK (t))
9946 stmt = begin_function_body ();
9947 else
9948 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
9949 ? BCS_TRY_BLOCK : 0);
9951 RECUR (BIND_EXPR_BODY (t));
9953 if (BIND_EXPR_BODY_BLOCK (t))
9954 finish_function_body (stmt);
9955 else
9956 finish_compound_stmt (stmt);
9957 break;
9959 case BREAK_STMT:
9960 finish_break_stmt ();
9961 break;
9963 case CONTINUE_STMT:
9964 finish_continue_stmt ();
9965 break;
9967 case SWITCH_STMT:
9968 stmt = begin_switch_stmt ();
9969 tmp = RECUR (SWITCH_STMT_COND (t));
9970 finish_switch_cond (tmp, stmt);
9971 RECUR (SWITCH_STMT_BODY (t));
9972 finish_switch_stmt (stmt);
9973 break;
9975 case CASE_LABEL_EXPR:
9976 finish_case_label (RECUR (CASE_LOW (t)),
9977 RECUR (CASE_HIGH (t)));
9978 break;
9980 case LABEL_EXPR:
9981 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
9982 break;
9984 case GOTO_EXPR:
9985 tmp = GOTO_DESTINATION (t);
9986 if (TREE_CODE (tmp) != LABEL_DECL)
9987 /* Computed goto's must be tsubst'd into. On the other hand,
9988 non-computed gotos must not be; the identifier in question
9989 will have no binding. */
9990 tmp = RECUR (tmp);
9991 else
9992 tmp = DECL_NAME (tmp);
9993 finish_goto_stmt (tmp);
9994 break;
9996 case ASM_EXPR:
9997 tmp = finish_asm_stmt
9998 (ASM_VOLATILE_P (t),
9999 RECUR (ASM_STRING (t)),
10000 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10001 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10002 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10004 tree asm_expr = tmp;
10005 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10006 asm_expr = TREE_OPERAND (asm_expr, 0);
10007 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10009 break;
10011 case TRY_BLOCK:
10012 if (CLEANUP_P (t))
10014 stmt = begin_try_block ();
10015 RECUR (TRY_STMTS (t));
10016 finish_cleanup_try_block (stmt);
10017 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10019 else
10021 tree compound_stmt = NULL_TREE;
10023 if (FN_TRY_BLOCK_P (t))
10024 stmt = begin_function_try_block (&compound_stmt);
10025 else
10026 stmt = begin_try_block ();
10028 RECUR (TRY_STMTS (t));
10030 if (FN_TRY_BLOCK_P (t))
10031 finish_function_try_block (stmt);
10032 else
10033 finish_try_block (stmt);
10035 RECUR (TRY_HANDLERS (t));
10036 if (FN_TRY_BLOCK_P (t))
10037 finish_function_handler_sequence (stmt, compound_stmt);
10038 else
10039 finish_handler_sequence (stmt);
10041 break;
10043 case HANDLER:
10045 tree decl = HANDLER_PARMS (t);
10047 if (decl)
10049 decl = tsubst (decl, args, complain, in_decl);
10050 /* Prevent instantiate_decl from trying to instantiate
10051 this variable. We've already done all that needs to be
10052 done. */
10053 if (decl != error_mark_node)
10054 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10056 stmt = begin_handler ();
10057 finish_handler_parms (decl, stmt);
10058 RECUR (HANDLER_BODY (t));
10059 finish_handler (stmt);
10061 break;
10063 case TAG_DEFN:
10064 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
10065 break;
10067 case STATIC_ASSERT:
10069 tree condition =
10070 tsubst_expr (STATIC_ASSERT_CONDITION (t),
10071 args,
10072 complain, in_decl,
10073 /*integral_constant_expression_p=*/true);
10074 finish_static_assert (condition,
10075 STATIC_ASSERT_MESSAGE (t),
10076 STATIC_ASSERT_SOURCE_LOCATION (t),
10077 /*member_p=*/false);
10079 break;
10081 case OMP_PARALLEL:
10082 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
10083 args, complain, in_decl);
10084 stmt = begin_omp_parallel ();
10085 RECUR (OMP_PARALLEL_BODY (t));
10086 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
10087 = OMP_PARALLEL_COMBINED (t);
10088 break;
10090 case OMP_FOR:
10092 tree clauses, decl, init, cond, incr, body, pre_body;
10094 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
10095 args, complain, in_decl);
10096 init = OMP_FOR_INIT (t);
10097 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10098 decl = RECUR (TREE_OPERAND (init, 0));
10099 init = RECUR (TREE_OPERAND (init, 1));
10100 cond = RECUR (OMP_FOR_COND (t));
10101 incr = RECUR (OMP_FOR_INCR (t));
10103 stmt = begin_omp_structured_block ();
10105 pre_body = push_stmt_list ();
10106 RECUR (OMP_FOR_PRE_BODY (t));
10107 pre_body = pop_stmt_list (pre_body);
10109 body = push_stmt_list ();
10110 RECUR (OMP_FOR_BODY (t));
10111 body = pop_stmt_list (body);
10113 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
10114 pre_body);
10115 if (t)
10116 OMP_FOR_CLAUSES (t) = clauses;
10118 add_stmt (finish_omp_structured_block (stmt));
10120 break;
10122 case OMP_SECTIONS:
10123 case OMP_SINGLE:
10124 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
10125 stmt = push_stmt_list ();
10126 RECUR (OMP_BODY (t));
10127 stmt = pop_stmt_list (stmt);
10129 t = copy_node (t);
10130 OMP_BODY (t) = stmt;
10131 OMP_CLAUSES (t) = tmp;
10132 add_stmt (t);
10133 break;
10135 case OMP_SECTION:
10136 case OMP_CRITICAL:
10137 case OMP_MASTER:
10138 case OMP_ORDERED:
10139 stmt = push_stmt_list ();
10140 RECUR (OMP_BODY (t));
10141 stmt = pop_stmt_list (stmt);
10143 t = copy_node (t);
10144 OMP_BODY (t) = stmt;
10145 add_stmt (t);
10146 break;
10148 case OMP_ATOMIC:
10149 if (OMP_ATOMIC_DEPENDENT_P (t))
10151 tree op1 = TREE_OPERAND (t, 1);
10152 tree lhs = RECUR (TREE_OPERAND (op1, 0));
10153 tree rhs = RECUR (TREE_OPERAND (op1, 1));
10154 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
10156 break;
10158 case EXPR_PACK_EXPANSION:
10159 error ("invalid use of pack expansion expression");
10160 return error_mark_node;
10162 case NONTYPE_ARGUMENT_PACK:
10163 error ("use %<...%> to expand argument pack");
10164 return error_mark_node;
10166 default:
10167 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
10169 return tsubst_copy_and_build (t, args, complain, in_decl,
10170 /*function_p=*/false,
10171 integral_constant_expression_p);
10174 return NULL_TREE;
10175 #undef RECUR
10178 /* T is a postfix-expression that is not being used in a function
10179 call. Return the substituted version of T. */
10181 static tree
10182 tsubst_non_call_postfix_expression (tree t, tree args,
10183 tsubst_flags_t complain,
10184 tree in_decl)
10186 if (TREE_CODE (t) == SCOPE_REF)
10187 t = tsubst_qualified_id (t, args, complain, in_decl,
10188 /*done=*/false, /*address_p=*/false);
10189 else
10190 t = tsubst_copy_and_build (t, args, complain, in_decl,
10191 /*function_p=*/false,
10192 /*integral_constant_expression_p=*/false);
10194 return t;
10197 /* Like tsubst but deals with expressions and performs semantic
10198 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10200 tree
10201 tsubst_copy_and_build (tree t,
10202 tree args,
10203 tsubst_flags_t complain,
10204 tree in_decl,
10205 bool function_p,
10206 bool integral_constant_expression_p)
10208 #define RECUR(NODE) \
10209 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10210 /*function_p=*/false, \
10211 integral_constant_expression_p)
10213 tree op1;
10215 if (t == NULL_TREE || t == error_mark_node)
10216 return t;
10218 switch (TREE_CODE (t))
10220 case USING_DECL:
10221 t = DECL_NAME (t);
10222 /* Fall through. */
10223 case IDENTIFIER_NODE:
10225 tree decl;
10226 cp_id_kind idk;
10227 bool non_integral_constant_expression_p;
10228 const char *error_msg;
10230 if (IDENTIFIER_TYPENAME_P (t))
10232 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10233 t = mangle_conv_op_name_for_type (new_type);
10236 /* Look up the name. */
10237 decl = lookup_name (t);
10239 /* By convention, expressions use ERROR_MARK_NODE to indicate
10240 failure, not NULL_TREE. */
10241 if (decl == NULL_TREE)
10242 decl = error_mark_node;
10244 decl = finish_id_expression (t, decl, NULL_TREE,
10245 &idk,
10246 integral_constant_expression_p,
10247 /*allow_non_integral_constant_expression_p=*/false,
10248 &non_integral_constant_expression_p,
10249 /*template_p=*/false,
10250 /*done=*/true,
10251 /*address_p=*/false,
10252 /*template_arg_p=*/false,
10253 &error_msg);
10254 if (error_msg)
10255 error (error_msg);
10256 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
10257 decl = unqualified_name_lookup_error (decl);
10258 return decl;
10261 case TEMPLATE_ID_EXPR:
10263 tree object;
10264 tree template = RECUR (TREE_OPERAND (t, 0));
10265 tree targs = TREE_OPERAND (t, 1);
10267 if (targs)
10268 targs = tsubst_template_args (targs, args, complain, in_decl);
10270 if (TREE_CODE (template) == COMPONENT_REF)
10272 object = TREE_OPERAND (template, 0);
10273 template = TREE_OPERAND (template, 1);
10275 else
10276 object = NULL_TREE;
10277 template = lookup_template_function (template, targs);
10279 if (object)
10280 return build3 (COMPONENT_REF, TREE_TYPE (template),
10281 object, template, NULL_TREE);
10282 else
10283 return baselink_for_fns (template);
10286 case INDIRECT_REF:
10288 tree r = RECUR (TREE_OPERAND (t, 0));
10290 if (REFERENCE_REF_P (t))
10292 /* A type conversion to reference type will be enclosed in
10293 such an indirect ref, but the substitution of the cast
10294 will have also added such an indirect ref. */
10295 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
10296 r = convert_from_reference (r);
10298 else
10299 r = build_x_indirect_ref (r, "unary *");
10300 return r;
10303 case NOP_EXPR:
10304 return build_nop
10305 (tsubst (TREE_TYPE (t), args, complain, in_decl),
10306 RECUR (TREE_OPERAND (t, 0)));
10308 case CAST_EXPR:
10309 case REINTERPRET_CAST_EXPR:
10310 case CONST_CAST_EXPR:
10311 case DYNAMIC_CAST_EXPR:
10312 case STATIC_CAST_EXPR:
10314 tree type;
10315 tree op;
10317 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10318 if (integral_constant_expression_p
10319 && !cast_valid_in_integral_constant_expression_p (type))
10321 error ("a cast to a type other than an integral or "
10322 "enumeration type cannot appear in a constant-expression");
10323 return error_mark_node;
10326 op = RECUR (TREE_OPERAND (t, 0));
10328 switch (TREE_CODE (t))
10330 case CAST_EXPR:
10331 return build_functional_cast (type, op);
10332 case REINTERPRET_CAST_EXPR:
10333 return build_reinterpret_cast (type, op);
10334 case CONST_CAST_EXPR:
10335 return build_const_cast (type, op);
10336 case DYNAMIC_CAST_EXPR:
10337 return build_dynamic_cast (type, op);
10338 case STATIC_CAST_EXPR:
10339 return build_static_cast (type, op);
10340 default:
10341 gcc_unreachable ();
10345 case POSTDECREMENT_EXPR:
10346 case POSTINCREMENT_EXPR:
10347 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10348 args, complain, in_decl);
10349 return build_x_unary_op (TREE_CODE (t), op1);
10351 case PREDECREMENT_EXPR:
10352 case PREINCREMENT_EXPR:
10353 case NEGATE_EXPR:
10354 case BIT_NOT_EXPR:
10355 case ABS_EXPR:
10356 case TRUTH_NOT_EXPR:
10357 case UNARY_PLUS_EXPR: /* Unary + */
10358 case REALPART_EXPR:
10359 case IMAGPART_EXPR:
10360 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
10362 case ADDR_EXPR:
10363 op1 = TREE_OPERAND (t, 0);
10364 if (TREE_CODE (op1) == SCOPE_REF)
10365 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
10366 /*done=*/true, /*address_p=*/true);
10367 else
10368 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
10369 in_decl);
10370 if (TREE_CODE (op1) == LABEL_DECL)
10371 return finish_label_address_expr (DECL_NAME (op1));
10372 return build_x_unary_op (ADDR_EXPR, op1);
10374 case PLUS_EXPR:
10375 case MINUS_EXPR:
10376 case MULT_EXPR:
10377 case TRUNC_DIV_EXPR:
10378 case CEIL_DIV_EXPR:
10379 case FLOOR_DIV_EXPR:
10380 case ROUND_DIV_EXPR:
10381 case EXACT_DIV_EXPR:
10382 case BIT_AND_EXPR:
10383 case BIT_IOR_EXPR:
10384 case BIT_XOR_EXPR:
10385 case TRUNC_MOD_EXPR:
10386 case FLOOR_MOD_EXPR:
10387 case TRUTH_ANDIF_EXPR:
10388 case TRUTH_ORIF_EXPR:
10389 case TRUTH_AND_EXPR:
10390 case TRUTH_OR_EXPR:
10391 case RSHIFT_EXPR:
10392 case LSHIFT_EXPR:
10393 case RROTATE_EXPR:
10394 case LROTATE_EXPR:
10395 case EQ_EXPR:
10396 case NE_EXPR:
10397 case MAX_EXPR:
10398 case MIN_EXPR:
10399 case LE_EXPR:
10400 case GE_EXPR:
10401 case LT_EXPR:
10402 case GT_EXPR:
10403 case MEMBER_REF:
10404 case DOTSTAR_EXPR:
10405 return build_x_binary_op
10406 (TREE_CODE (t),
10407 RECUR (TREE_OPERAND (t, 0)),
10408 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10409 ? ERROR_MARK
10410 : TREE_CODE (TREE_OPERAND (t, 0))),
10411 RECUR (TREE_OPERAND (t, 1)),
10412 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10413 ? ERROR_MARK
10414 : TREE_CODE (TREE_OPERAND (t, 1))),
10415 /*overloaded_p=*/NULL);
10417 case SCOPE_REF:
10418 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
10419 /*address_p=*/false);
10420 case ARRAY_REF:
10421 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10422 args, complain, in_decl);
10423 return build_x_binary_op (ARRAY_REF, op1,
10424 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10425 ? ERROR_MARK
10426 : TREE_CODE (TREE_OPERAND (t, 0))),
10427 RECUR (TREE_OPERAND (t, 1)),
10428 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10429 ? ERROR_MARK
10430 : TREE_CODE (TREE_OPERAND (t, 1))),
10431 /*overloaded_p=*/NULL);
10433 case SIZEOF_EXPR:
10434 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10436 /* We only want to compute the number of arguments. */
10437 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10438 complain, in_decl);
10439 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
10441 /* Fall through */
10443 case ALIGNOF_EXPR:
10444 op1 = TREE_OPERAND (t, 0);
10445 if (!args)
10447 /* When there are no ARGS, we are trying to evaluate a
10448 non-dependent expression from the parser. Trying to do
10449 the substitutions may not work. */
10450 if (!TYPE_P (op1))
10451 op1 = TREE_TYPE (op1);
10453 else
10455 ++skip_evaluation;
10456 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
10457 /*function_p=*/false,
10458 /*integral_constant_expression_p=*/false);
10459 --skip_evaluation;
10461 if (TYPE_P (op1))
10462 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
10463 else
10464 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
10466 case MODOP_EXPR:
10468 tree r = build_x_modify_expr
10469 (RECUR (TREE_OPERAND (t, 0)),
10470 TREE_CODE (TREE_OPERAND (t, 1)),
10471 RECUR (TREE_OPERAND (t, 2)));
10472 /* TREE_NO_WARNING must be set if either the expression was
10473 parenthesized or it uses an operator such as >>= rather
10474 than plain assignment. In the former case, it was already
10475 set and must be copied. In the latter case,
10476 build_x_modify_expr sets it and it must not be reset
10477 here. */
10478 if (TREE_NO_WARNING (t))
10479 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10480 return r;
10483 case ARROW_EXPR:
10484 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10485 args, complain, in_decl);
10486 /* Remember that there was a reference to this entity. */
10487 if (DECL_P (op1))
10488 mark_used (op1);
10489 return build_x_arrow (op1);
10491 case NEW_EXPR:
10492 return build_new
10493 (RECUR (TREE_OPERAND (t, 0)),
10494 RECUR (TREE_OPERAND (t, 1)),
10495 RECUR (TREE_OPERAND (t, 2)),
10496 RECUR (TREE_OPERAND (t, 3)),
10497 NEW_EXPR_USE_GLOBAL (t));
10499 case DELETE_EXPR:
10500 return delete_sanity
10501 (RECUR (TREE_OPERAND (t, 0)),
10502 RECUR (TREE_OPERAND (t, 1)),
10503 DELETE_EXPR_USE_VEC (t),
10504 DELETE_EXPR_USE_GLOBAL (t));
10506 case COMPOUND_EXPR:
10507 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
10508 RECUR (TREE_OPERAND (t, 1)));
10510 case CALL_EXPR:
10512 tree function;
10513 tree call_args;
10514 bool qualified_p;
10515 bool koenig_p;
10517 function = CALL_EXPR_FN (t);
10518 /* When we parsed the expression, we determined whether or
10519 not Koenig lookup should be performed. */
10520 koenig_p = KOENIG_LOOKUP_P (t);
10521 if (TREE_CODE (function) == SCOPE_REF)
10523 qualified_p = true;
10524 function = tsubst_qualified_id (function, args, complain, in_decl,
10525 /*done=*/false,
10526 /*address_p=*/false);
10528 else
10530 if (TREE_CODE (function) == COMPONENT_REF)
10532 tree op = TREE_OPERAND (function, 1);
10534 qualified_p = (TREE_CODE (op) == SCOPE_REF
10535 || (BASELINK_P (op)
10536 && BASELINK_QUALIFIED_P (op)));
10538 else
10539 qualified_p = false;
10541 function = tsubst_copy_and_build (function, args, complain,
10542 in_decl,
10543 !qualified_p,
10544 integral_constant_expression_p);
10546 if (BASELINK_P (function))
10547 qualified_p = true;
10550 /* FIXME: Rewrite this so as not to construct an arglist. */
10551 call_args = RECUR (CALL_EXPR_ARGS (t));
10553 /* We do not perform argument-dependent lookup if normal
10554 lookup finds a non-function, in accordance with the
10555 expected resolution of DR 218. */
10556 if (koenig_p
10557 && ((is_overloaded_fn (function)
10558 /* If lookup found a member function, the Koenig lookup is
10559 not appropriate, even if an unqualified-name was used
10560 to denote the function. */
10561 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
10562 || TREE_CODE (function) == IDENTIFIER_NODE))
10563 function = perform_koenig_lookup (function, call_args);
10565 if (TREE_CODE (function) == IDENTIFIER_NODE)
10567 unqualified_name_lookup_error (function);
10568 return error_mark_node;
10571 /* Remember that there was a reference to this entity. */
10572 if (DECL_P (function))
10573 mark_used (function);
10575 if (TREE_CODE (function) == OFFSET_REF)
10576 return build_offset_ref_call_from_tree (function, call_args);
10577 if (TREE_CODE (function) == COMPONENT_REF)
10579 if (!BASELINK_P (TREE_OPERAND (function, 1)))
10580 return finish_call_expr (function, call_args,
10581 /*disallow_virtual=*/false,
10582 /*koenig_p=*/false);
10583 else
10584 return (build_new_method_call
10585 (TREE_OPERAND (function, 0),
10586 TREE_OPERAND (function, 1),
10587 call_args, NULL_TREE,
10588 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
10589 /*fn_p=*/NULL));
10591 return finish_call_expr (function, call_args,
10592 /*disallow_virtual=*/qualified_p,
10593 koenig_p);
10596 case COND_EXPR:
10597 return build_x_conditional_expr
10598 (RECUR (TREE_OPERAND (t, 0)),
10599 RECUR (TREE_OPERAND (t, 1)),
10600 RECUR (TREE_OPERAND (t, 2)));
10602 case PSEUDO_DTOR_EXPR:
10603 return finish_pseudo_destructor_expr
10604 (RECUR (TREE_OPERAND (t, 0)),
10605 RECUR (TREE_OPERAND (t, 1)),
10606 RECUR (TREE_OPERAND (t, 2)));
10608 case TREE_LIST:
10610 tree purpose, value, chain;
10612 if (t == void_list_node)
10613 return t;
10615 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
10616 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
10618 /* We have pack expansions, so expand those and
10619 create a new list out of it. */
10620 tree purposevec = NULL_TREE;
10621 tree valuevec = NULL_TREE;
10622 tree chain;
10623 int i, len = -1;
10625 /* Expand the argument expressions. */
10626 if (TREE_PURPOSE (t))
10627 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
10628 complain, in_decl);
10629 if (TREE_VALUE (t))
10630 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
10631 complain, in_decl);
10633 /* Build the rest of the list. */
10634 chain = TREE_CHAIN (t);
10635 if (chain && chain != void_type_node)
10636 chain = RECUR (chain);
10638 /* Determine the number of arguments. */
10639 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
10641 len = TREE_VEC_LENGTH (purposevec);
10642 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
10644 else if (TREE_CODE (valuevec) == TREE_VEC)
10645 len = TREE_VEC_LENGTH (valuevec);
10646 else
10648 /* Since we only performed a partial substitution into
10649 the argument pack, we only return a single list
10650 node. */
10651 if (purposevec == TREE_PURPOSE (t)
10652 && valuevec == TREE_VALUE (t)
10653 && chain == TREE_CHAIN (t))
10654 return t;
10656 return tree_cons (purposevec, valuevec, chain);
10659 /* Convert the argument vectors into a TREE_LIST */
10660 i = len;
10661 while (i > 0)
10663 /* Grab the Ith values. */
10664 i--;
10665 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
10666 : NULL_TREE;
10667 value
10668 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
10669 : NULL_TREE;
10671 /* Build the list (backwards). */
10672 chain = tree_cons (purpose, value, chain);
10675 return chain;
10678 purpose = TREE_PURPOSE (t);
10679 if (purpose)
10680 purpose = RECUR (purpose);
10681 value = TREE_VALUE (t);
10682 if (value)
10683 value = RECUR (value);
10684 chain = TREE_CHAIN (t);
10685 if (chain && chain != void_type_node)
10686 chain = RECUR (chain);
10687 if (purpose == TREE_PURPOSE (t)
10688 && value == TREE_VALUE (t)
10689 && chain == TREE_CHAIN (t))
10690 return t;
10691 return tree_cons (purpose, value, chain);
10694 case COMPONENT_REF:
10696 tree object;
10697 tree object_type;
10698 tree member;
10700 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10701 args, complain, in_decl);
10702 /* Remember that there was a reference to this entity. */
10703 if (DECL_P (object))
10704 mark_used (object);
10705 object_type = TREE_TYPE (object);
10707 member = TREE_OPERAND (t, 1);
10708 if (BASELINK_P (member))
10709 member = tsubst_baselink (member,
10710 non_reference (TREE_TYPE (object)),
10711 args, complain, in_decl);
10712 else
10713 member = tsubst_copy (member, args, complain, in_decl);
10714 if (member == error_mark_node)
10715 return error_mark_node;
10717 if (object_type && !CLASS_TYPE_P (object_type))
10719 if (TREE_CODE (member) == BIT_NOT_EXPR)
10720 return finish_pseudo_destructor_expr (object,
10721 NULL_TREE,
10722 object_type);
10723 else if (TREE_CODE (member) == SCOPE_REF
10724 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
10725 return finish_pseudo_destructor_expr (object,
10726 object,
10727 object_type);
10729 else if (TREE_CODE (member) == SCOPE_REF
10730 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
10732 tree tmpl;
10733 tree args;
10735 /* Lookup the template functions now that we know what the
10736 scope is. */
10737 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
10738 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
10739 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
10740 /*is_type_p=*/false,
10741 /*complain=*/false);
10742 if (BASELINK_P (member))
10744 BASELINK_FUNCTIONS (member)
10745 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
10746 args);
10747 member = (adjust_result_of_qualified_name_lookup
10748 (member, BINFO_TYPE (BASELINK_BINFO (member)),
10749 object_type));
10751 else
10753 qualified_name_lookup_error (object_type, tmpl, member);
10754 return error_mark_node;
10757 else if (TREE_CODE (member) == SCOPE_REF
10758 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
10759 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
10761 if (complain & tf_error)
10763 if (TYPE_P (TREE_OPERAND (member, 0)))
10764 error ("%qT is not a class or namespace",
10765 TREE_OPERAND (member, 0));
10766 else
10767 error ("%qD is not a class or namespace",
10768 TREE_OPERAND (member, 0));
10770 return error_mark_node;
10772 else if (TREE_CODE (member) == FIELD_DECL)
10773 return finish_non_static_data_member (member, object, NULL_TREE);
10775 return finish_class_member_access_expr (object, member,
10776 /*template_p=*/false);
10779 case THROW_EXPR:
10780 return build_throw
10781 (RECUR (TREE_OPERAND (t, 0)));
10783 case CONSTRUCTOR:
10785 VEC(constructor_elt,gc) *n;
10786 constructor_elt *ce;
10787 unsigned HOST_WIDE_INT idx;
10788 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10789 bool process_index_p;
10790 int newlen;
10791 bool need_copy_p = false;
10793 if (type == error_mark_node)
10794 return error_mark_node;
10796 /* digest_init will do the wrong thing if we let it. */
10797 if (type && TYPE_PTRMEMFUNC_P (type))
10798 return t;
10800 /* We do not want to process the index of aggregate
10801 initializers as they are identifier nodes which will be
10802 looked up by digest_init. */
10803 process_index_p = !(type && IS_AGGR_TYPE (type));
10805 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
10806 newlen = VEC_length (constructor_elt, n);
10807 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
10809 if (ce->index && process_index_p)
10810 ce->index = RECUR (ce->index);
10812 if (PACK_EXPANSION_P (ce->value))
10814 /* Substitute into the pack expansion. */
10815 ce->value = tsubst_pack_expansion (ce->value, args, complain,
10816 in_decl);
10818 if (TREE_VEC_LENGTH (ce->value) == 1)
10819 /* Just move the argument into place. */
10820 ce->value = TREE_VEC_ELT (ce->value, 0);
10821 else
10823 /* Update the length of the final CONSTRUCTOR
10824 arguments vector, and note that we will need to
10825 copy.*/
10826 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
10827 need_copy_p = true;
10830 else
10831 ce->value = RECUR (ce->value);
10834 if (need_copy_p)
10836 VEC(constructor_elt,gc) *old_n = n;
10838 n = VEC_alloc (constructor_elt, gc, newlen);
10839 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
10840 idx++)
10842 if (TREE_CODE (ce->value) == TREE_VEC)
10844 int i, len = TREE_VEC_LENGTH (ce->value);
10845 for (i = 0; i < len; ++i)
10846 CONSTRUCTOR_APPEND_ELT (n, 0,
10847 TREE_VEC_ELT (ce->value, i));
10849 else
10850 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
10854 if (TREE_HAS_CONSTRUCTOR (t))
10855 return finish_compound_literal (type, n);
10857 return build_constructor (NULL_TREE, n);
10860 case TYPEID_EXPR:
10862 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
10863 if (TYPE_P (operand_0))
10864 return get_typeid (operand_0);
10865 return build_typeid (operand_0);
10868 case VAR_DECL:
10869 if (!args)
10870 return t;
10871 /* Fall through */
10873 case PARM_DECL:
10875 tree r = tsubst_copy (t, args, complain, in_decl);
10877 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
10878 /* If the original type was a reference, we'll be wrapped in
10879 the appropriate INDIRECT_REF. */
10880 r = convert_from_reference (r);
10881 return r;
10884 case VA_ARG_EXPR:
10885 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
10886 tsubst_copy (TREE_TYPE (t), args, complain,
10887 in_decl));
10889 case OFFSETOF_EXPR:
10890 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
10892 case TRAIT_EXPR:
10894 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
10895 complain, in_decl);
10897 tree type2 = TRAIT_EXPR_TYPE2 (t);
10898 if (type2)
10899 type2 = tsubst_copy (type2, args, complain, in_decl);
10901 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
10904 case STMT_EXPR:
10906 tree old_stmt_expr = cur_stmt_expr;
10907 tree stmt_expr = begin_stmt_expr ();
10909 cur_stmt_expr = stmt_expr;
10910 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
10911 integral_constant_expression_p);
10912 stmt_expr = finish_stmt_expr (stmt_expr, false);
10913 cur_stmt_expr = old_stmt_expr;
10915 return stmt_expr;
10918 case CONST_DECL:
10919 t = tsubst_copy (t, args, complain, in_decl);
10920 /* As in finish_id_expression, we resolve enumeration constants
10921 to their underlying values. */
10922 if (TREE_CODE (t) == CONST_DECL)
10924 used_types_insert (TREE_TYPE (t));
10925 return DECL_INITIAL (t);
10927 return t;
10929 default:
10930 /* Handle Objective-C++ constructs, if appropriate. */
10932 tree subst
10933 = objcp_tsubst_copy_and_build (t, args, complain,
10934 in_decl, /*function_p=*/false);
10935 if (subst)
10936 return subst;
10938 return tsubst_copy (t, args, complain, in_decl);
10941 #undef RECUR
10944 /* Verify that the instantiated ARGS are valid. For type arguments,
10945 make sure that the type's linkage is ok. For non-type arguments,
10946 make sure they are constants if they are integral or enumerations.
10947 Emit an error under control of COMPLAIN, and return TRUE on error. */
10949 static bool
10950 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
10952 int ix, len = DECL_NTPARMS (tmpl);
10953 bool result = false;
10955 for (ix = 0; ix != len; ix++)
10957 tree t = TREE_VEC_ELT (args, ix);
10959 if (TYPE_P (t))
10961 /* [basic.link]: A name with no linkage (notably, the name
10962 of a class or enumeration declared in a local scope)
10963 shall not be used to declare an entity with linkage.
10964 This implies that names with no linkage cannot be used as
10965 template arguments. */
10966 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
10968 if (nt)
10970 /* DR 488 makes use of a type with no linkage cause
10971 type deduction to fail. */
10972 if (complain & tf_error)
10974 if (TYPE_ANONYMOUS_P (nt))
10975 error ("%qT is/uses anonymous type", t);
10976 else
10977 error ("template argument for %qD uses local type %qT",
10978 tmpl, t);
10980 result = true;
10982 /* In order to avoid all sorts of complications, we do not
10983 allow variably-modified types as template arguments. */
10984 else if (variably_modified_type_p (t, NULL_TREE))
10986 if (complain & tf_error)
10987 error ("%qT is a variably modified type", t);
10988 result = true;
10991 /* A non-type argument of integral or enumerated type must be a
10992 constant. */
10993 else if (TREE_TYPE (t)
10994 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
10995 && !TREE_CONSTANT (t))
10997 if (complain & tf_error)
10998 error ("integral expression %qE is not constant", t);
10999 result = true;
11002 if (result && (complain & tf_error))
11003 error (" trying to instantiate %qD", tmpl);
11004 return result;
11007 /* Instantiate the indicated variable or function template TMPL with
11008 the template arguments in TARG_PTR. */
11010 tree
11011 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
11013 tree fndecl;
11014 tree gen_tmpl;
11015 tree spec;
11016 HOST_WIDE_INT saved_processing_template_decl;
11018 if (tmpl == error_mark_node)
11019 return error_mark_node;
11021 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
11023 /* If this function is a clone, handle it specially. */
11024 if (DECL_CLONED_FUNCTION_P (tmpl))
11026 tree spec;
11027 tree clone;
11029 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
11030 complain);
11031 if (spec == error_mark_node)
11032 return error_mark_node;
11034 /* Look for the clone. */
11035 FOR_EACH_CLONE (clone, spec)
11036 if (DECL_NAME (clone) == DECL_NAME (tmpl))
11037 return clone;
11038 /* We should always have found the clone by now. */
11039 gcc_unreachable ();
11040 return NULL_TREE;
11043 /* Check to see if we already have this specialization. */
11044 spec = retrieve_specialization (tmpl, targ_ptr,
11045 /*class_specializations_p=*/false);
11046 if (spec != NULL_TREE)
11047 return spec;
11049 gen_tmpl = most_general_template (tmpl);
11050 if (tmpl != gen_tmpl)
11052 /* The TMPL is a partial instantiation. To get a full set of
11053 arguments we must add the arguments used to perform the
11054 partial instantiation. */
11055 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
11056 targ_ptr);
11058 /* Check to see if we already have this specialization. */
11059 spec = retrieve_specialization (gen_tmpl, targ_ptr,
11060 /*class_specializations_p=*/false);
11061 if (spec != NULL_TREE)
11062 return spec;
11065 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
11066 complain))
11067 return error_mark_node;
11069 /* We are building a FUNCTION_DECL, during which the access of its
11070 parameters and return types have to be checked. However this
11071 FUNCTION_DECL which is the desired context for access checking
11072 is not built yet. We solve this chicken-and-egg problem by
11073 deferring all checks until we have the FUNCTION_DECL. */
11074 push_deferring_access_checks (dk_deferred);
11076 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11077 (because, for example, we have encountered a non-dependent
11078 function call in the body of a template function and must now
11079 determine which of several overloaded functions will be called),
11080 within the instantiation itself we are not processing a
11081 template. */
11082 saved_processing_template_decl = processing_template_decl;
11083 processing_template_decl = 0;
11084 /* Substitute template parameters to obtain the specialization. */
11085 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
11086 targ_ptr, complain, gen_tmpl);
11087 processing_template_decl = saved_processing_template_decl;
11088 if (fndecl == error_mark_node)
11089 return error_mark_node;
11091 /* Now we know the specialization, compute access previously
11092 deferred. */
11093 push_access_scope (fndecl);
11094 perform_deferred_access_checks ();
11095 pop_access_scope (fndecl);
11096 pop_deferring_access_checks ();
11098 /* The DECL_TI_TEMPLATE should always be the immediate parent
11099 template, not the most general template. */
11100 DECL_TI_TEMPLATE (fndecl) = tmpl;
11102 /* If we've just instantiated the main entry point for a function,
11103 instantiate all the alternate entry points as well. We do this
11104 by cloning the instantiation of the main entry point, not by
11105 instantiating the template clones. */
11106 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
11107 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
11109 return fndecl;
11112 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11113 arguments that are being used when calling it. TARGS is a vector
11114 into which the deduced template arguments are placed.
11116 Return zero for success, 2 for an incomplete match that doesn't resolve
11117 all the types, and 1 for complete failure. An error message will be
11118 printed only for an incomplete match.
11120 If FN is a conversion operator, or we are trying to produce a specific
11121 specialization, RETURN_TYPE is the return type desired.
11123 The EXPLICIT_TARGS are explicit template arguments provided via a
11124 template-id.
11126 The parameter STRICT is one of:
11128 DEDUCE_CALL:
11129 We are deducing arguments for a function call, as in
11130 [temp.deduct.call].
11132 DEDUCE_CONV:
11133 We are deducing arguments for a conversion function, as in
11134 [temp.deduct.conv].
11136 DEDUCE_EXACT:
11137 We are deducing arguments when doing an explicit instantiation
11138 as in [temp.explicit], when determining an explicit specialization
11139 as in [temp.expl.spec], or when taking the address of a function
11140 template, as in [temp.deduct.funcaddr]. */
11143 fn_type_unification (tree fn,
11144 tree explicit_targs,
11145 tree targs,
11146 tree args,
11147 tree return_type,
11148 unification_kind_t strict,
11149 int flags)
11151 tree parms;
11152 tree fntype;
11153 int result;
11154 bool incomplete_argument_packs_p = false;
11156 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
11158 fntype = TREE_TYPE (fn);
11159 if (explicit_targs)
11161 /* [temp.deduct]
11163 The specified template arguments must match the template
11164 parameters in kind (i.e., type, nontype, template), and there
11165 must not be more arguments than there are parameters;
11166 otherwise type deduction fails.
11168 Nontype arguments must match the types of the corresponding
11169 nontype template parameters, or must be convertible to the
11170 types of the corresponding nontype parameters as specified in
11171 _temp.arg.nontype_, otherwise type deduction fails.
11173 All references in the function type of the function template
11174 to the corresponding template parameters are replaced by the
11175 specified template argument values. If a substitution in a
11176 template parameter or in the function type of the function
11177 template results in an invalid type, type deduction fails. */
11178 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
11179 int i, len = TREE_VEC_LENGTH (tparms);
11180 tree converted_args;
11181 bool incomplete = false;
11183 if (explicit_targs == error_mark_node)
11184 return 1;
11186 converted_args
11187 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
11188 /*require_all_args=*/false,
11189 /*use_default_args=*/false));
11190 if (converted_args == error_mark_node)
11191 return 1;
11193 /* Substitute the explicit args into the function type. This is
11194 necessary so that, for instance, explicitly declared function
11195 arguments can match null pointed constants. If we were given
11196 an incomplete set of explicit args, we must not do semantic
11197 processing during substitution as we could create partial
11198 instantiations. */
11199 for (i = 0; i < len; i++)
11201 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11202 bool parameter_pack = false;
11204 /* Dig out the actual parm. */
11205 if (TREE_CODE (parm) == TYPE_DECL
11206 || TREE_CODE (parm) == TEMPLATE_DECL)
11208 parm = TREE_TYPE (parm);
11209 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
11211 else if (TREE_CODE (parm) == PARM_DECL)
11213 parm = DECL_INITIAL (parm);
11214 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
11217 if (parameter_pack)
11219 int level, idx;
11220 tree targ;
11221 template_parm_level_and_index (parm, &level, &idx);
11223 /* Mark the argument pack as "incomplete". We could
11224 still deduce more arguments during unification. */
11225 targ = TMPL_ARG (converted_args, level, idx);
11226 if (targ)
11228 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
11229 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
11230 = ARGUMENT_PACK_ARGS (targ);
11233 /* We have some incomplete argument packs. */
11234 incomplete_argument_packs_p = true;
11238 if (incomplete_argument_packs_p)
11239 /* Any substitution is guaranteed to be incomplete if there
11240 are incomplete argument packs, because we can still deduce
11241 more arguments. */
11242 incomplete = 1;
11243 else
11244 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
11246 processing_template_decl += incomplete;
11247 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
11248 processing_template_decl -= incomplete;
11250 if (fntype == error_mark_node)
11251 return 1;
11253 /* Place the explicitly specified arguments in TARGS. */
11254 for (i = NUM_TMPL_ARGS (converted_args); i--;)
11255 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
11258 /* Never do unification on the 'this' parameter. */
11259 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
11261 if (return_type)
11263 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
11264 args = tree_cons (NULL_TREE, return_type, args);
11267 /* We allow incomplete unification without an error message here
11268 because the standard doesn't seem to explicitly prohibit it. Our
11269 callers must be ready to deal with unification failures in any
11270 event. */
11271 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
11272 targs, parms, args, /*subr=*/0,
11273 strict, flags);
11275 if (result == 0 && incomplete_argument_packs_p)
11277 int i, len = NUM_TMPL_ARGS (targs);
11279 /* Clear the "incomplete" flags on all argument packs. */
11280 for (i = 0; i < len; i++)
11282 tree arg = TREE_VEC_ELT (targs, i);
11283 if (ARGUMENT_PACK_P (arg))
11285 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
11286 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
11291 if (result == 0)
11292 /* All is well so far. Now, check:
11294 [temp.deduct]
11296 When all template arguments have been deduced, all uses of
11297 template parameters in nondeduced contexts are replaced with
11298 the corresponding deduced argument values. If the
11299 substitution results in an invalid type, as described above,
11300 type deduction fails. */
11301 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
11302 == error_mark_node)
11303 return 1;
11305 return result;
11308 /* Adjust types before performing type deduction, as described in
11309 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11310 sections are symmetric. PARM is the type of a function parameter
11311 or the return type of the conversion function. ARG is the type of
11312 the argument passed to the call, or the type of the value
11313 initialized with the result of the conversion function.
11314 ARG_EXPR is the original argument expression, which may be null. */
11316 static int
11317 maybe_adjust_types_for_deduction (unification_kind_t strict,
11318 tree* parm,
11319 tree* arg,
11320 tree arg_expr)
11322 int result = 0;
11324 switch (strict)
11326 case DEDUCE_CALL:
11327 break;
11329 case DEDUCE_CONV:
11331 /* Swap PARM and ARG throughout the remainder of this
11332 function; the handling is precisely symmetric since PARM
11333 will initialize ARG rather than vice versa. */
11334 tree* temp = parm;
11335 parm = arg;
11336 arg = temp;
11337 break;
11340 case DEDUCE_EXACT:
11341 /* There is nothing to do in this case. */
11342 return 0;
11344 default:
11345 gcc_unreachable ();
11348 if (TREE_CODE (*parm) != REFERENCE_TYPE)
11350 /* [temp.deduct.call]
11352 If P is not a reference type:
11354 --If A is an array type, the pointer type produced by the
11355 array-to-pointer standard conversion (_conv.array_) is
11356 used in place of A for type deduction; otherwise,
11358 --If A is a function type, the pointer type produced by
11359 the function-to-pointer standard conversion
11360 (_conv.func_) is used in place of A for type deduction;
11361 otherwise,
11363 --If A is a cv-qualified type, the top level
11364 cv-qualifiers of A's type are ignored for type
11365 deduction. */
11366 if (TREE_CODE (*arg) == ARRAY_TYPE)
11367 *arg = build_pointer_type (TREE_TYPE (*arg));
11368 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
11369 *arg = build_pointer_type (*arg);
11370 else
11371 *arg = TYPE_MAIN_VARIANT (*arg);
11374 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
11375 of the form T&&, where T is a template parameter, and the argument
11376 is an lvalue, T is deduced as A& */
11377 if (TREE_CODE (*parm) == REFERENCE_TYPE
11378 && TYPE_REF_IS_RVALUE (*parm)
11379 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
11380 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
11381 && arg_expr && real_lvalue_p (arg_expr))
11382 *arg = build_reference_type (*arg);
11384 /* [temp.deduct.call]
11386 If P is a cv-qualified type, the top level cv-qualifiers
11387 of P's type are ignored for type deduction. If P is a
11388 reference type, the type referred to by P is used for
11389 type deduction. */
11390 *parm = TYPE_MAIN_VARIANT (*parm);
11391 if (TREE_CODE (*parm) == REFERENCE_TYPE)
11393 *parm = TREE_TYPE (*parm);
11394 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
11397 /* DR 322. For conversion deduction, remove a reference type on parm
11398 too (which has been swapped into ARG). */
11399 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
11400 *arg = TREE_TYPE (*arg);
11402 return result;
11405 /* Most parms like fn_type_unification.
11407 If SUBR is 1, we're being called recursively (to unify the
11408 arguments of a function or method parameter of a function
11409 template). */
11411 static int
11412 type_unification_real (tree tparms,
11413 tree targs,
11414 tree xparms,
11415 tree xargs,
11416 int subr,
11417 unification_kind_t strict,
11418 int flags)
11420 tree parm, arg, arg_expr;
11421 int i;
11422 int ntparms = TREE_VEC_LENGTH (tparms);
11423 int sub_strict;
11424 int saw_undeduced = 0;
11425 tree parms, args;
11427 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
11428 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
11429 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
11430 gcc_assert (ntparms > 0);
11432 switch (strict)
11434 case DEDUCE_CALL:
11435 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
11436 | UNIFY_ALLOW_DERIVED);
11437 break;
11439 case DEDUCE_CONV:
11440 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
11441 break;
11443 case DEDUCE_EXACT:
11444 sub_strict = UNIFY_ALLOW_NONE;
11445 break;
11447 default:
11448 gcc_unreachable ();
11451 again:
11452 parms = xparms;
11453 args = xargs;
11455 while (parms && parms != void_list_node
11456 && args && args != void_list_node)
11458 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11459 break;
11461 parm = TREE_VALUE (parms);
11462 parms = TREE_CHAIN (parms);
11463 arg = TREE_VALUE (args);
11464 args = TREE_CHAIN (args);
11465 arg_expr = NULL;
11467 if (arg == error_mark_node)
11468 return 1;
11469 if (arg == unknown_type_node)
11470 /* We can't deduce anything from this, but we might get all the
11471 template args from other function args. */
11472 continue;
11474 /* Conversions will be performed on a function argument that
11475 corresponds with a function parameter that contains only
11476 non-deducible template parameters and explicitly specified
11477 template parameters. */
11478 if (!uses_template_parms (parm))
11480 tree type;
11482 if (!TYPE_P (arg))
11483 type = TREE_TYPE (arg);
11484 else
11485 type = arg;
11487 if (same_type_p (parm, type))
11488 continue;
11489 if (strict != DEDUCE_EXACT
11490 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
11491 flags))
11492 continue;
11494 return 1;
11497 if (!TYPE_P (arg))
11499 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
11500 if (type_unknown_p (arg))
11502 /* [temp.deduct.type]
11504 A template-argument can be deduced from a pointer to
11505 function or pointer to member function argument if
11506 the set of overloaded functions does not contain
11507 function templates and at most one of a set of
11508 overloaded functions provides a unique match. */
11509 if (resolve_overloaded_unification
11510 (tparms, targs, parm, arg, strict, sub_strict))
11511 continue;
11513 return 1;
11515 arg_expr = arg;
11516 arg = unlowered_expr_type (arg);
11517 if (arg == error_mark_node)
11518 return 1;
11522 int arg_strict = sub_strict;
11524 if (!subr)
11525 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
11526 arg_expr);
11528 if (unify (tparms, targs, parm, arg, arg_strict))
11529 return 1;
11534 if (parms
11535 && parms != void_list_node
11536 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11538 /* Unify the remaining arguments with the pack expansion type. */
11539 tree argvec;
11540 tree parmvec = make_tree_vec (1);
11541 int len = 0;
11542 tree t;
11544 /* Count the number of arguments that remain. */
11545 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
11546 len++;
11548 /* Allocate a TREE_VEC and copy in all of the arguments */
11549 argvec = make_tree_vec (len);
11550 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
11552 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
11553 ++i;
11556 /* Copy the parameter into parmvec. */
11557 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
11558 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
11559 /*call_args_p=*/true, /*subr=*/subr))
11560 return 1;
11562 /* Advance to the end of the list of parameters. */
11563 parms = TREE_CHAIN (parms);
11566 /* Fail if we've reached the end of the parm list, and more args
11567 are present, and the parm list isn't variadic. */
11568 if (args && args != void_list_node && parms == void_list_node)
11569 return 1;
11570 /* Fail if parms are left and they don't have default values. */
11571 if (parms && parms != void_list_node
11572 && TREE_PURPOSE (parms) == NULL_TREE)
11573 return 1;
11575 if (!subr)
11576 for (i = 0; i < ntparms; i++)
11577 if (!TREE_VEC_ELT (targs, i))
11579 tree tparm;
11581 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
11582 continue;
11584 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11586 /* If this is an undeduced nontype parameter that depends on
11587 a type parameter, try another pass; its type may have been
11588 deduced from a later argument than the one from which
11589 this parameter can be deduced. */
11590 if (TREE_CODE (tparm) == PARM_DECL
11591 && uses_template_parms (TREE_TYPE (tparm))
11592 && !saw_undeduced++)
11593 goto again;
11595 /* Core issue #226 (C++0x) [temp.deduct]:
11597 If a template argument has not been deduced, its
11598 default template argument, if any, is used.
11600 When we are in C++98 mode, TREE_PURPOSE will either
11601 be NULL_TREE or ERROR_MARK_NODE, so we do not need
11602 to explicitly check cxx_dialect here. */
11603 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
11605 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
11606 targs, tf_none, NULL_TREE);
11607 if (arg == error_mark_node)
11608 return 1;
11609 else
11611 TREE_VEC_ELT (targs, i) = arg;
11612 continue;
11616 /* If the type parameter is a parameter pack, then it will
11617 be deduced to an empty parameter pack. */
11618 if (template_parameter_pack_p (tparm))
11620 tree arg;
11622 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
11624 arg = make_node (NONTYPE_ARGUMENT_PACK);
11625 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
11626 TREE_CONSTANT (arg) = 1;
11628 else
11629 arg = make_node (TYPE_ARGUMENT_PACK);
11631 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
11633 TREE_VEC_ELT (targs, i) = arg;
11634 continue;
11637 return 2;
11640 return 0;
11643 /* Subroutine of type_unification_real. Args are like the variables
11644 at the call site. ARG is an overloaded function (or template-id);
11645 we try deducing template args from each of the overloads, and if
11646 only one succeeds, we go with that. Modifies TARGS and returns
11647 true on success. */
11649 static bool
11650 resolve_overloaded_unification (tree tparms,
11651 tree targs,
11652 tree parm,
11653 tree arg,
11654 unification_kind_t strict,
11655 int sub_strict)
11657 tree tempargs = copy_node (targs);
11658 int good = 0;
11659 bool addr_p;
11661 if (TREE_CODE (arg) == ADDR_EXPR)
11663 arg = TREE_OPERAND (arg, 0);
11664 addr_p = true;
11666 else
11667 addr_p = false;
11669 if (TREE_CODE (arg) == COMPONENT_REF)
11670 /* Handle `&x' where `x' is some static or non-static member
11671 function name. */
11672 arg = TREE_OPERAND (arg, 1);
11674 if (TREE_CODE (arg) == OFFSET_REF)
11675 arg = TREE_OPERAND (arg, 1);
11677 /* Strip baselink information. */
11678 if (BASELINK_P (arg))
11679 arg = BASELINK_FUNCTIONS (arg);
11681 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
11683 /* If we got some explicit template args, we need to plug them into
11684 the affected templates before we try to unify, in case the
11685 explicit args will completely resolve the templates in question. */
11687 tree expl_subargs = TREE_OPERAND (arg, 1);
11688 arg = TREE_OPERAND (arg, 0);
11690 for (; arg; arg = OVL_NEXT (arg))
11692 tree fn = OVL_CURRENT (arg);
11693 tree subargs, elem;
11695 if (TREE_CODE (fn) != TEMPLATE_DECL)
11696 continue;
11698 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
11699 expl_subargs, /*check_ret=*/false);
11700 if (subargs)
11702 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
11703 good += try_one_overload (tparms, targs, tempargs, parm,
11704 elem, strict, sub_strict, addr_p);
11708 else if (TREE_CODE (arg) != OVERLOAD
11709 && TREE_CODE (arg) != FUNCTION_DECL)
11710 /* If ARG is, for example, "(0, &f)" then its type will be unknown
11711 -- but the deduction does not succeed because the expression is
11712 not just the function on its own. */
11713 return false;
11714 else
11715 for (; arg; arg = OVL_NEXT (arg))
11716 good += try_one_overload (tparms, targs, tempargs, parm,
11717 TREE_TYPE (OVL_CURRENT (arg)),
11718 strict, sub_strict, addr_p);
11720 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11721 to function or pointer to member function argument if the set of
11722 overloaded functions does not contain function templates and at most
11723 one of a set of overloaded functions provides a unique match.
11725 So if we found multiple possibilities, we return success but don't
11726 deduce anything. */
11728 if (good == 1)
11730 int i = TREE_VEC_LENGTH (targs);
11731 for (; i--; )
11732 if (TREE_VEC_ELT (tempargs, i))
11733 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
11735 if (good)
11736 return true;
11738 return false;
11741 /* Subroutine of resolve_overloaded_unification; does deduction for a single
11742 overload. Fills TARGS with any deduced arguments, or error_mark_node if
11743 different overloads deduce different arguments for a given parm.
11744 ADDR_P is true if the expression for which deduction is being
11745 performed was of the form "& fn" rather than simply "fn".
11747 Returns 1 on success. */
11749 static int
11750 try_one_overload (tree tparms,
11751 tree orig_targs,
11752 tree targs,
11753 tree parm,
11754 tree arg,
11755 unification_kind_t strict,
11756 int sub_strict,
11757 bool addr_p)
11759 int nargs;
11760 tree tempargs;
11761 int i;
11763 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11764 to function or pointer to member function argument if the set of
11765 overloaded functions does not contain function templates and at most
11766 one of a set of overloaded functions provides a unique match.
11768 So if this is a template, just return success. */
11770 if (uses_template_parms (arg))
11771 return 1;
11773 if (TREE_CODE (arg) == METHOD_TYPE)
11774 arg = build_ptrmemfunc_type (build_pointer_type (arg));
11775 else if (addr_p)
11776 arg = build_pointer_type (arg);
11778 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
11780 /* We don't copy orig_targs for this because if we have already deduced
11781 some template args from previous args, unify would complain when we
11782 try to deduce a template parameter for the same argument, even though
11783 there isn't really a conflict. */
11784 nargs = TREE_VEC_LENGTH (targs);
11785 tempargs = make_tree_vec (nargs);
11787 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
11788 return 0;
11790 /* First make sure we didn't deduce anything that conflicts with
11791 explicitly specified args. */
11792 for (i = nargs; i--; )
11794 tree elt = TREE_VEC_ELT (tempargs, i);
11795 tree oldelt = TREE_VEC_ELT (orig_targs, i);
11797 if (!elt)
11798 /*NOP*/;
11799 else if (uses_template_parms (elt))
11800 /* Since we're unifying against ourselves, we will fill in
11801 template args used in the function parm list with our own
11802 template parms. Discard them. */
11803 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
11804 else if (oldelt && !template_args_equal (oldelt, elt))
11805 return 0;
11808 for (i = nargs; i--; )
11810 tree elt = TREE_VEC_ELT (tempargs, i);
11812 if (elt)
11813 TREE_VEC_ELT (targs, i) = elt;
11816 return 1;
11819 /* PARM is a template class (perhaps with unbound template
11820 parameters). ARG is a fully instantiated type. If ARG can be
11821 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
11822 TARGS are as for unify. */
11824 static tree
11825 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
11827 tree copy_of_targs;
11829 if (!CLASSTYPE_TEMPLATE_INFO (arg)
11830 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
11831 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
11832 return NULL_TREE;
11834 /* We need to make a new template argument vector for the call to
11835 unify. If we used TARGS, we'd clutter it up with the result of
11836 the attempted unification, even if this class didn't work out.
11837 We also don't want to commit ourselves to all the unifications
11838 we've already done, since unification is supposed to be done on
11839 an argument-by-argument basis. In other words, consider the
11840 following pathological case:
11842 template <int I, int J, int K>
11843 struct S {};
11845 template <int I, int J>
11846 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
11848 template <int I, int J, int K>
11849 void f(S<I, J, K>, S<I, I, I>);
11851 void g() {
11852 S<0, 0, 0> s0;
11853 S<0, 1, 2> s2;
11855 f(s0, s2);
11858 Now, by the time we consider the unification involving `s2', we
11859 already know that we must have `f<0, 0, 0>'. But, even though
11860 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
11861 because there are two ways to unify base classes of S<0, 1, 2>
11862 with S<I, I, I>. If we kept the already deduced knowledge, we
11863 would reject the possibility I=1. */
11864 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
11866 /* If unification failed, we're done. */
11867 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
11868 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
11869 return NULL_TREE;
11871 return arg;
11874 /* Given a template type PARM and a class type ARG, find the unique
11875 base type in ARG that is an instance of PARM. We do not examine
11876 ARG itself; only its base-classes. If there is not exactly one
11877 appropriate base class, return NULL_TREE. PARM may be the type of
11878 a partial specialization, as well as a plain template type. Used
11879 by unify. */
11881 static tree
11882 get_template_base (tree tparms, tree targs, tree parm, tree arg)
11884 tree rval = NULL_TREE;
11885 tree binfo;
11887 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
11889 binfo = TYPE_BINFO (complete_type (arg));
11890 if (!binfo)
11891 /* The type could not be completed. */
11892 return NULL_TREE;
11894 /* Walk in inheritance graph order. The search order is not
11895 important, and this avoids multiple walks of virtual bases. */
11896 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
11898 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
11900 if (r)
11902 /* If there is more than one satisfactory baseclass, then:
11904 [temp.deduct.call]
11906 If they yield more than one possible deduced A, the type
11907 deduction fails.
11909 applies. */
11910 if (rval && !same_type_p (r, rval))
11911 return NULL_TREE;
11913 rval = r;
11917 return rval;
11920 /* Returns the level of DECL, which declares a template parameter. */
11922 static int
11923 template_decl_level (tree decl)
11925 switch (TREE_CODE (decl))
11927 case TYPE_DECL:
11928 case TEMPLATE_DECL:
11929 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
11931 case PARM_DECL:
11932 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
11934 default:
11935 gcc_unreachable ();
11937 return 0;
11940 /* Decide whether ARG can be unified with PARM, considering only the
11941 cv-qualifiers of each type, given STRICT as documented for unify.
11942 Returns nonzero iff the unification is OK on that basis. */
11944 static int
11945 check_cv_quals_for_unify (int strict, tree arg, tree parm)
11947 int arg_quals = cp_type_quals (arg);
11948 int parm_quals = cp_type_quals (parm);
11950 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
11951 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
11953 /* Although a CVR qualifier is ignored when being applied to a
11954 substituted template parameter ([8.3.2]/1 for example), that
11955 does not apply during deduction [14.8.2.4]/1, (even though
11956 that is not explicitly mentioned, [14.8.2.4]/9 indicates
11957 this). Except when we're allowing additional CV qualifiers
11958 at the outer level [14.8.2.1]/3,1st bullet. */
11959 if ((TREE_CODE (arg) == REFERENCE_TYPE
11960 || TREE_CODE (arg) == FUNCTION_TYPE
11961 || TREE_CODE (arg) == METHOD_TYPE)
11962 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
11963 return 0;
11965 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
11966 && (parm_quals & TYPE_QUAL_RESTRICT))
11967 return 0;
11970 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
11971 && (arg_quals & parm_quals) != parm_quals)
11972 return 0;
11974 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
11975 && (parm_quals & arg_quals) != arg_quals)
11976 return 0;
11978 return 1;
11981 /* Determines the LEVEL and INDEX for the template parameter PARM. */
11982 void
11983 template_parm_level_and_index (tree parm, int* level, int* index)
11985 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
11986 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
11987 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
11989 *index = TEMPLATE_TYPE_IDX (parm);
11990 *level = TEMPLATE_TYPE_LEVEL (parm);
11992 else
11994 *index = TEMPLATE_PARM_IDX (parm);
11995 *level = TEMPLATE_PARM_LEVEL (parm);
11999 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12000 expansion at the end of PACKED_PARMS. Returns 0 if the type
12001 deduction succeeds, 1 otherwise. STRICT is the same as in
12002 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12003 call argument list. We'll need to adjust the arguments to make them
12004 types. SUBR tells us if this is from a recursive call to
12005 type_unification_real. */
12007 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
12008 tree packed_args, int strict, bool call_args_p,
12009 bool subr)
12011 tree parm
12012 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
12013 tree pattern = PACK_EXPANSION_PATTERN (parm);
12014 tree pack, packs = NULL_TREE;
12015 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
12016 int len = TREE_VEC_LENGTH (packed_args);
12018 /* Determine the parameter packs we will be deducing from the
12019 pattern, and record their current deductions. */
12020 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
12021 pack; pack = TREE_CHAIN (pack))
12023 tree parm_pack = TREE_VALUE (pack);
12024 int idx, level;
12026 /* Determine the index and level of this parameter pack. */
12027 template_parm_level_and_index (parm_pack, &level, &idx);
12029 /* Keep track of the parameter packs and their corresponding
12030 argument packs. */
12031 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
12032 TREE_TYPE (packs) = make_tree_vec (len - start);
12035 /* Loop through all of the arguments that have not yet been
12036 unified and unify each with the pattern. */
12037 for (i = start; i < len; i++)
12039 tree parm = pattern;
12041 /* For each parameter pack, clear out the deduced value so that
12042 we can deduce it again. */
12043 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12045 int idx, level;
12046 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12048 TMPL_ARG (targs, level, idx) = NULL_TREE;
12051 /* Unify the pattern with the current argument. */
12053 tree arg = TREE_VEC_ELT (packed_args, i);
12054 int arg_strict = strict;
12055 bool skip_arg_p = false;
12057 if (call_args_p)
12059 int sub_strict;
12061 /* This mirrors what we do in type_unification_real. */
12062 switch (strict)
12064 case DEDUCE_CALL:
12065 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
12066 | UNIFY_ALLOW_MORE_CV_QUAL
12067 | UNIFY_ALLOW_DERIVED);
12068 break;
12070 case DEDUCE_CONV:
12071 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12072 break;
12074 case DEDUCE_EXACT:
12075 sub_strict = UNIFY_ALLOW_NONE;
12076 break;
12078 default:
12079 gcc_unreachable ();
12082 if (!TYPE_P (arg))
12084 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12085 if (type_unknown_p (arg))
12087 /* [temp.deduct.type] A template-argument can be
12088 deduced from a pointer to function or pointer
12089 to member function argument if the set of
12090 overloaded functions does not contain function
12091 templates and at most one of a set of
12092 overloaded functions provides a unique
12093 match. */
12095 if (resolve_overloaded_unification
12096 (tparms, targs, parm, arg, strict, sub_strict)
12097 != 0)
12098 return 1;
12099 skip_arg_p = true;
12102 if (!skip_arg_p)
12104 arg = TREE_TYPE (arg);
12105 if (arg == error_mark_node)
12106 return 1;
12110 arg_strict = sub_strict;
12112 if (!subr)
12113 arg_strict |=
12114 maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12117 if (!skip_arg_p)
12119 if (unify (tparms, targs, parm, arg, arg_strict))
12120 return 1;
12124 /* For each parameter pack, collect the deduced value. */
12125 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12127 int idx, level;
12128 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12130 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
12131 TMPL_ARG (targs, level, idx);
12135 /* Verify that the results of unification with the parameter packs
12136 produce results consistent with what we've seen before, and make
12137 the deduced argument packs available. */
12138 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12140 tree old_pack = TREE_VALUE (pack);
12141 tree new_args = TREE_TYPE (pack);
12143 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
12145 /* Prepend the explicit arguments onto NEW_ARGS. */
12146 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12147 tree old_args = new_args;
12148 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
12149 int len = explicit_len + TREE_VEC_LENGTH (old_args);
12151 /* Copy the explicit arguments. */
12152 new_args = make_tree_vec (len);
12153 for (i = 0; i < explicit_len; i++)
12154 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
12156 /* Copy the deduced arguments. */
12157 for (; i < len; i++)
12158 TREE_VEC_ELT (new_args, i) =
12159 TREE_VEC_ELT (old_args, i - explicit_len);
12162 if (!old_pack)
12164 tree result;
12165 int idx, level;
12167 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12169 /* Build the deduced *_ARGUMENT_PACK. */
12170 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
12172 result = make_node (NONTYPE_ARGUMENT_PACK);
12173 TREE_TYPE (result) =
12174 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
12175 TREE_CONSTANT (result) = 1;
12177 else
12178 result = make_node (TYPE_ARGUMENT_PACK);
12180 SET_ARGUMENT_PACK_ARGS (result, new_args);
12182 /* Note the deduced argument packs for this parameter
12183 pack. */
12184 TMPL_ARG (targs, level, idx) = result;
12186 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
12187 && (ARGUMENT_PACK_ARGS (old_pack)
12188 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
12190 /* We only had the explicitly-provided arguments before, but
12191 now we have a complete set of arguments. */
12192 int idx, level;
12193 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12194 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12196 /* Keep the original deduced argument pack. */
12197 TMPL_ARG (targs, level, idx) = old_pack;
12199 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
12200 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
12201 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
12203 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
12204 new_args))
12205 /* Inconsistent unification of this parameter pack. */
12206 return 1;
12207 else
12209 int idx, level;
12211 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12213 /* Keep the original deduced argument pack. */
12214 TMPL_ARG (targs, level, idx) = old_pack;
12218 return 0;
12221 /* Deduce the value of template parameters. TPARMS is the (innermost)
12222 set of template parameters to a template. TARGS is the bindings
12223 for those template parameters, as determined thus far; TARGS may
12224 include template arguments for outer levels of template parameters
12225 as well. PARM is a parameter to a template function, or a
12226 subcomponent of that parameter; ARG is the corresponding argument.
12227 This function attempts to match PARM with ARG in a manner
12228 consistent with the existing assignments in TARGS. If more values
12229 are deduced, then TARGS is updated.
12231 Returns 0 if the type deduction succeeds, 1 otherwise. The
12232 parameter STRICT is a bitwise or of the following flags:
12234 UNIFY_ALLOW_NONE:
12235 Require an exact match between PARM and ARG.
12236 UNIFY_ALLOW_MORE_CV_QUAL:
12237 Allow the deduced ARG to be more cv-qualified (by qualification
12238 conversion) than ARG.
12239 UNIFY_ALLOW_LESS_CV_QUAL:
12240 Allow the deduced ARG to be less cv-qualified than ARG.
12241 UNIFY_ALLOW_DERIVED:
12242 Allow the deduced ARG to be a template base class of ARG,
12243 or a pointer to a template base class of the type pointed to by
12244 ARG.
12245 UNIFY_ALLOW_INTEGER:
12246 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12247 case for more information.
12248 UNIFY_ALLOW_OUTER_LEVEL:
12249 This is the outermost level of a deduction. Used to determine validity
12250 of qualification conversions. A valid qualification conversion must
12251 have const qualified pointers leading up to the inner type which
12252 requires additional CV quals, except at the outer level, where const
12253 is not required [conv.qual]. It would be normal to set this flag in
12254 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12255 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12256 This is the outermost level of a deduction, and PARM can be more CV
12257 qualified at this point.
12258 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12259 This is the outermost level of a deduction, and PARM can be less CV
12260 qualified at this point. */
12262 static int
12263 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
12265 int idx;
12266 tree targ;
12267 tree tparm;
12268 int strict_in = strict;
12270 /* I don't think this will do the right thing with respect to types.
12271 But the only case I've seen it in so far has been array bounds, where
12272 signedness is the only information lost, and I think that will be
12273 okay. */
12274 while (TREE_CODE (parm) == NOP_EXPR)
12275 parm = TREE_OPERAND (parm, 0);
12277 if (arg == error_mark_node)
12278 return 1;
12279 if (arg == unknown_type_node)
12280 /* We can't deduce anything from this, but we might get all the
12281 template args from other function args. */
12282 return 0;
12284 /* If PARM uses template parameters, then we can't bail out here,
12285 even if ARG == PARM, since we won't record unifications for the
12286 template parameters. We might need them if we're trying to
12287 figure out which of two things is more specialized. */
12288 if (arg == parm && !uses_template_parms (parm))
12289 return 0;
12291 /* Immediately reject some pairs that won't unify because of
12292 cv-qualification mismatches. */
12293 if (TREE_CODE (arg) == TREE_CODE (parm)
12294 && TYPE_P (arg)
12295 /* It is the elements of the array which hold the cv quals of an array
12296 type, and the elements might be template type parms. We'll check
12297 when we recurse. */
12298 && TREE_CODE (arg) != ARRAY_TYPE
12299 /* We check the cv-qualifiers when unifying with template type
12300 parameters below. We want to allow ARG `const T' to unify with
12301 PARM `T' for example, when computing which of two templates
12302 is more specialized, for example. */
12303 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
12304 && !check_cv_quals_for_unify (strict_in, arg, parm))
12305 return 1;
12307 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
12308 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
12309 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
12310 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
12311 strict &= ~UNIFY_ALLOW_DERIVED;
12312 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12313 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
12315 switch (TREE_CODE (parm))
12317 case TYPENAME_TYPE:
12318 case SCOPE_REF:
12319 case UNBOUND_CLASS_TEMPLATE:
12320 /* In a type which contains a nested-name-specifier, template
12321 argument values cannot be deduced for template parameters used
12322 within the nested-name-specifier. */
12323 return 0;
12325 case TEMPLATE_TYPE_PARM:
12326 case TEMPLATE_TEMPLATE_PARM:
12327 case BOUND_TEMPLATE_TEMPLATE_PARM:
12328 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12329 if (tparm == error_mark_node)
12330 return 1;
12332 if (TEMPLATE_TYPE_LEVEL (parm)
12333 != template_decl_level (tparm))
12334 /* The PARM is not one we're trying to unify. Just check
12335 to see if it matches ARG. */
12336 return (TREE_CODE (arg) == TREE_CODE (parm)
12337 && same_type_p (parm, arg)) ? 0 : 1;
12338 idx = TEMPLATE_TYPE_IDX (parm);
12339 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12340 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
12342 /* Check for mixed types and values. */
12343 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12344 && TREE_CODE (tparm) != TYPE_DECL)
12345 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12346 && TREE_CODE (tparm) != TEMPLATE_DECL))
12347 return 1;
12349 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12351 /* ARG must be constructed from a template class or a template
12352 template parameter. */
12353 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
12354 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
12355 return 1;
12358 tree parmvec = TYPE_TI_ARGS (parm);
12359 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
12360 tree argtmplvec
12361 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
12362 int i;
12364 /* The resolution to DR150 makes clear that default
12365 arguments for an N-argument may not be used to bind T
12366 to a template template parameter with fewer than N
12367 parameters. It is not safe to permit the binding of
12368 default arguments as an extension, as that may change
12369 the meaning of a conforming program. Consider:
12371 struct Dense { static const unsigned int dim = 1; };
12373 template <template <typename> class View,
12374 typename Block>
12375 void operator+(float, View<Block> const&);
12377 template <typename Block,
12378 unsigned int Dim = Block::dim>
12379 struct Lvalue_proxy { operator float() const; };
12381 void
12382 test_1d (void) {
12383 Lvalue_proxy<Dense> p;
12384 float b;
12385 b + p;
12388 Here, if Lvalue_proxy is permitted to bind to View, then
12389 the global operator+ will be used; if they are not, the
12390 Lvalue_proxy will be converted to float. */
12391 if (coerce_template_parms (argtmplvec, parmvec,
12392 TYPE_TI_TEMPLATE (parm),
12393 tf_none,
12394 /*require_all_args=*/true,
12395 /*use_default_args=*/false)
12396 == error_mark_node)
12397 return 1;
12399 /* Deduce arguments T, i from TT<T> or TT<i>.
12400 We check each element of PARMVEC and ARGVEC individually
12401 rather than the whole TREE_VEC since they can have
12402 different number of elements. */
12404 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
12406 if (unify (tparms, targs,
12407 TREE_VEC_ELT (parmvec, i),
12408 TREE_VEC_ELT (argvec, i),
12409 UNIFY_ALLOW_NONE))
12410 return 1;
12413 arg = TYPE_TI_TEMPLATE (arg);
12415 /* Fall through to deduce template name. */
12418 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12419 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12421 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12423 /* Simple cases: Value already set, does match or doesn't. */
12424 if (targ != NULL_TREE && template_args_equal (targ, arg))
12425 return 0;
12426 else if (targ)
12427 return 1;
12429 else
12431 /* If PARM is `const T' and ARG is only `int', we don't have
12432 a match unless we are allowing additional qualification.
12433 If ARG is `const int' and PARM is just `T' that's OK;
12434 that binds `const int' to `T'. */
12435 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
12436 arg, parm))
12437 return 1;
12439 /* Consider the case where ARG is `const volatile int' and
12440 PARM is `const T'. Then, T should be `volatile int'. */
12441 arg = cp_build_qualified_type_real
12442 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
12443 if (arg == error_mark_node)
12444 return 1;
12446 /* Simple cases: Value already set, does match or doesn't. */
12447 if (targ != NULL_TREE && same_type_p (targ, arg))
12448 return 0;
12449 else if (targ)
12450 return 1;
12452 /* Make sure that ARG is not a variable-sized array. (Note
12453 that were talking about variable-sized arrays (like
12454 `int[n]'), rather than arrays of unknown size (like
12455 `int[]').) We'll get very confused by such a type since
12456 the bound of the array will not be computable in an
12457 instantiation. Besides, such types are not allowed in
12458 ISO C++, so we can do as we please here. */
12459 if (variably_modified_type_p (arg, NULL_TREE))
12460 return 1;
12463 /* If ARG is a parameter pack or an expansion, we cannot unify
12464 against it unless PARM is also a parameter pack. */
12465 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12466 && !template_parameter_pack_p (parm))
12467 return 1;
12469 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12470 return 0;
12472 case TEMPLATE_PARM_INDEX:
12473 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12474 if (tparm == error_mark_node)
12475 return 1;
12477 if (TEMPLATE_PARM_LEVEL (parm)
12478 != template_decl_level (tparm))
12479 /* The PARM is not one we're trying to unify. Just check
12480 to see if it matches ARG. */
12481 return !(TREE_CODE (arg) == TREE_CODE (parm)
12482 && cp_tree_equal (parm, arg));
12484 idx = TEMPLATE_PARM_IDX (parm);
12485 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12487 if (targ)
12488 return !cp_tree_equal (targ, arg);
12490 /* [temp.deduct.type] If, in the declaration of a function template
12491 with a non-type template-parameter, the non-type
12492 template-parameter is used in an expression in the function
12493 parameter-list and, if the corresponding template-argument is
12494 deduced, the template-argument type shall match the type of the
12495 template-parameter exactly, except that a template-argument
12496 deduced from an array bound may be of any integral type.
12497 The non-type parameter might use already deduced type parameters. */
12498 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
12499 if (!TREE_TYPE (arg))
12500 /* Template-parameter dependent expression. Just accept it for now.
12501 It will later be processed in convert_template_argument. */
12503 else if (same_type_p (TREE_TYPE (arg), tparm))
12504 /* OK */;
12505 else if ((strict & UNIFY_ALLOW_INTEGER)
12506 && (TREE_CODE (tparm) == INTEGER_TYPE
12507 || TREE_CODE (tparm) == BOOLEAN_TYPE))
12508 /* Convert the ARG to the type of PARM; the deduced non-type
12509 template argument must exactly match the types of the
12510 corresponding parameter. */
12511 arg = fold (build_nop (TREE_TYPE (parm), arg));
12512 else if (uses_template_parms (tparm))
12513 /* We haven't deduced the type of this parameter yet. Try again
12514 later. */
12515 return 0;
12516 else
12517 return 1;
12519 /* If ARG is a parameter pack or an expansion, we cannot unify
12520 against it unless PARM is also a parameter pack. */
12521 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12522 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
12523 return 1;
12525 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12526 return 0;
12528 case PTRMEM_CST:
12530 /* A pointer-to-member constant can be unified only with
12531 another constant. */
12532 if (TREE_CODE (arg) != PTRMEM_CST)
12533 return 1;
12535 /* Just unify the class member. It would be useless (and possibly
12536 wrong, depending on the strict flags) to unify also
12537 PTRMEM_CST_CLASS, because we want to be sure that both parm and
12538 arg refer to the same variable, even if through different
12539 classes. For instance:
12541 struct A { int x; };
12542 struct B : A { };
12544 Unification of &A::x and &B::x must succeed. */
12545 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
12546 PTRMEM_CST_MEMBER (arg), strict);
12549 case POINTER_TYPE:
12551 if (TREE_CODE (arg) != POINTER_TYPE)
12552 return 1;
12554 /* [temp.deduct.call]
12556 A can be another pointer or pointer to member type that can
12557 be converted to the deduced A via a qualification
12558 conversion (_conv.qual_).
12560 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
12561 This will allow for additional cv-qualification of the
12562 pointed-to types if appropriate. */
12564 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
12565 /* The derived-to-base conversion only persists through one
12566 level of pointers. */
12567 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
12569 return unify (tparms, targs, TREE_TYPE (parm),
12570 TREE_TYPE (arg), strict);
12573 case REFERENCE_TYPE:
12574 if (TREE_CODE (arg) != REFERENCE_TYPE)
12575 return 1;
12576 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12577 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12579 case ARRAY_TYPE:
12580 if (TREE_CODE (arg) != ARRAY_TYPE)
12581 return 1;
12582 if ((TYPE_DOMAIN (parm) == NULL_TREE)
12583 != (TYPE_DOMAIN (arg) == NULL_TREE))
12584 return 1;
12585 if (TYPE_DOMAIN (parm) != NULL_TREE)
12587 tree parm_max;
12588 tree arg_max;
12589 bool parm_cst;
12590 bool arg_cst;
12592 /* Our representation of array types uses "N - 1" as the
12593 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
12594 not an integer constant. We cannot unify arbitrarily
12595 complex expressions, so we eliminate the MINUS_EXPRs
12596 here. */
12597 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
12598 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
12599 if (!parm_cst)
12601 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
12602 parm_max = TREE_OPERAND (parm_max, 0);
12604 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
12605 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
12606 if (!arg_cst)
12608 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
12609 trying to unify the type of a variable with the type
12610 of a template parameter. For example:
12612 template <unsigned int N>
12613 void f (char (&) [N]);
12614 int g();
12615 void h(int i) {
12616 char a[g(i)];
12617 f(a);
12620 Here, the type of the ARG will be "int [g(i)]", and
12621 may be a SAVE_EXPR, etc. */
12622 if (TREE_CODE (arg_max) != MINUS_EXPR)
12623 return 1;
12624 arg_max = TREE_OPERAND (arg_max, 0);
12627 /* If only one of the bounds used a MINUS_EXPR, compensate
12628 by adding one to the other bound. */
12629 if (parm_cst && !arg_cst)
12630 parm_max = fold_build2 (PLUS_EXPR,
12631 integer_type_node,
12632 parm_max,
12633 integer_one_node);
12634 else if (arg_cst && !parm_cst)
12635 arg_max = fold_build2 (PLUS_EXPR,
12636 integer_type_node,
12637 arg_max,
12638 integer_one_node);
12640 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
12641 return 1;
12643 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12644 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12646 case REAL_TYPE:
12647 case COMPLEX_TYPE:
12648 case VECTOR_TYPE:
12649 case INTEGER_TYPE:
12650 case BOOLEAN_TYPE:
12651 case ENUMERAL_TYPE:
12652 case VOID_TYPE:
12653 if (TREE_CODE (arg) != TREE_CODE (parm))
12654 return 1;
12656 /* We have already checked cv-qualification at the top of the
12657 function. */
12658 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
12659 return 1;
12661 /* As far as unification is concerned, this wins. Later checks
12662 will invalidate it if necessary. */
12663 return 0;
12665 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
12666 /* Type INTEGER_CST can come from ordinary constant template args. */
12667 case INTEGER_CST:
12668 while (TREE_CODE (arg) == NOP_EXPR)
12669 arg = TREE_OPERAND (arg, 0);
12671 if (TREE_CODE (arg) != INTEGER_CST)
12672 return 1;
12673 return !tree_int_cst_equal (parm, arg);
12675 case TREE_VEC:
12677 int i;
12678 if (TREE_CODE (arg) != TREE_VEC)
12679 return 1;
12680 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
12681 return 1;
12682 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
12683 if (unify (tparms, targs,
12684 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
12685 UNIFY_ALLOW_NONE))
12686 return 1;
12687 return 0;
12690 case RECORD_TYPE:
12691 case UNION_TYPE:
12692 if (TREE_CODE (arg) != TREE_CODE (parm))
12693 return 1;
12695 if (TYPE_PTRMEMFUNC_P (parm))
12697 if (!TYPE_PTRMEMFUNC_P (arg))
12698 return 1;
12700 return unify (tparms, targs,
12701 TYPE_PTRMEMFUNC_FN_TYPE (parm),
12702 TYPE_PTRMEMFUNC_FN_TYPE (arg),
12703 strict);
12706 if (CLASSTYPE_TEMPLATE_INFO (parm))
12708 tree t = NULL_TREE;
12710 if (strict_in & UNIFY_ALLOW_DERIVED)
12712 /* First, we try to unify the PARM and ARG directly. */
12713 t = try_class_unification (tparms, targs,
12714 parm, arg);
12716 if (!t)
12718 /* Fallback to the special case allowed in
12719 [temp.deduct.call]:
12721 If P is a class, and P has the form
12722 template-id, then A can be a derived class of
12723 the deduced A. Likewise, if P is a pointer to
12724 a class of the form template-id, A can be a
12725 pointer to a derived class pointed to by the
12726 deduced A. */
12727 t = get_template_base (tparms, targs, parm, arg);
12729 if (!t)
12730 return 1;
12733 else if (CLASSTYPE_TEMPLATE_INFO (arg)
12734 && (CLASSTYPE_TI_TEMPLATE (parm)
12735 == CLASSTYPE_TI_TEMPLATE (arg)))
12736 /* Perhaps PARM is something like S<U> and ARG is S<int>.
12737 Then, we should unify `int' and `U'. */
12738 t = arg;
12739 else
12740 /* There's no chance of unification succeeding. */
12741 return 1;
12743 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
12744 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
12746 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
12747 return 1;
12748 return 0;
12750 case METHOD_TYPE:
12751 case FUNCTION_TYPE:
12752 if (TREE_CODE (arg) != TREE_CODE (parm))
12753 return 1;
12755 /* CV qualifications for methods can never be deduced, they must
12756 match exactly. We need to check them explicitly here,
12757 because type_unification_real treats them as any other
12758 cvqualified parameter. */
12759 if (TREE_CODE (parm) == METHOD_TYPE
12760 && (!check_cv_quals_for_unify
12761 (UNIFY_ALLOW_NONE,
12762 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
12763 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
12764 return 1;
12766 if (unify (tparms, targs, TREE_TYPE (parm),
12767 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
12768 return 1;
12769 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
12770 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
12771 LOOKUP_NORMAL);
12773 case OFFSET_TYPE:
12774 /* Unify a pointer to member with a pointer to member function, which
12775 deduces the type of the member as a function type. */
12776 if (TYPE_PTRMEMFUNC_P (arg))
12778 tree method_type;
12779 tree fntype;
12780 cp_cv_quals cv_quals;
12782 /* Check top-level cv qualifiers */
12783 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
12784 return 1;
12786 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12787 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
12788 return 1;
12790 /* Determine the type of the function we are unifying against. */
12791 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
12792 fntype =
12793 build_function_type (TREE_TYPE (method_type),
12794 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
12796 /* Extract the cv-qualifiers of the member function from the
12797 implicit object parameter and place them on the function
12798 type to be restored later. */
12799 cv_quals =
12800 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
12801 fntype = build_qualified_type (fntype, cv_quals);
12802 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
12805 if (TREE_CODE (arg) != OFFSET_TYPE)
12806 return 1;
12807 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12808 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
12809 return 1;
12810 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12811 strict);
12813 case CONST_DECL:
12814 if (DECL_TEMPLATE_PARM_P (parm))
12815 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
12816 if (arg != integral_constant_value (parm))
12817 return 1;
12818 return 0;
12820 case FIELD_DECL:
12821 case TEMPLATE_DECL:
12822 /* Matched cases are handled by the ARG == PARM test above. */
12823 return 1;
12825 case TYPE_ARGUMENT_PACK:
12826 case NONTYPE_ARGUMENT_PACK:
12828 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
12829 tree packed_args = ARGUMENT_PACK_ARGS (arg);
12830 int i, len = TREE_VEC_LENGTH (packed_parms);
12831 int argslen = TREE_VEC_LENGTH (packed_args);
12832 int parm_variadic_p = 0;
12834 /* Check if the parameters end in a pack, making them variadic. */
12835 if (len > 0
12836 && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1)))
12837 parm_variadic_p = 1;
12839 /* If we don't have enough arguments to satisfy the parameters
12840 (not counting the pack expression at the end), or we have
12841 too many arguments for a parameter list that doesn't end in
12842 a pack expression, we can't unify. */
12843 if (argslen < (len - parm_variadic_p)
12844 || (argslen > len && !parm_variadic_p))
12845 return 1;
12847 /* Unify all of the parameters that precede the (optional)
12848 pack expression. */
12849 for (i = 0; i < len - parm_variadic_p; ++i)
12851 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
12852 TREE_VEC_ELT (packed_args, i), strict))
12853 return 1;
12856 if (parm_variadic_p)
12857 return unify_pack_expansion (tparms, targs,
12858 packed_parms, packed_args,
12859 strict, /*call_args_p=*/false,
12860 /*subr=*/false);
12861 return 0;
12864 break;
12866 case TYPEOF_TYPE:
12867 case DECLTYPE_TYPE:
12868 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
12869 nodes. */
12870 return 0;
12872 default:
12873 gcc_assert (EXPR_P (parm));
12875 /* We must be looking at an expression. This can happen with
12876 something like:
12878 template <int I>
12879 void foo(S<I>, S<I + 2>);
12881 This is a "nondeduced context":
12883 [deduct.type]
12885 The nondeduced contexts are:
12887 --A type that is a template-id in which one or more of
12888 the template-arguments is an expression that references
12889 a template-parameter.
12891 In these cases, we assume deduction succeeded, but don't
12892 actually infer any unifications. */
12894 if (!uses_template_parms (parm)
12895 && !template_args_equal (parm, arg))
12896 return 1;
12897 else
12898 return 0;
12902 /* Note that DECL can be defined in this translation unit, if
12903 required. */
12905 static void
12906 mark_definable (tree decl)
12908 tree clone;
12909 DECL_NOT_REALLY_EXTERN (decl) = 1;
12910 FOR_EACH_CLONE (clone, decl)
12911 DECL_NOT_REALLY_EXTERN (clone) = 1;
12914 /* Called if RESULT is explicitly instantiated, or is a member of an
12915 explicitly instantiated class. */
12917 void
12918 mark_decl_instantiated (tree result, int extern_p)
12920 SET_DECL_EXPLICIT_INSTANTIATION (result);
12922 /* If this entity has already been written out, it's too late to
12923 make any modifications. */
12924 if (TREE_ASM_WRITTEN (result))
12925 return;
12927 if (TREE_CODE (result) != FUNCTION_DECL)
12928 /* The TREE_PUBLIC flag for function declarations will have been
12929 set correctly by tsubst. */
12930 TREE_PUBLIC (result) = 1;
12932 /* This might have been set by an earlier implicit instantiation. */
12933 DECL_COMDAT (result) = 0;
12935 if (extern_p)
12936 DECL_NOT_REALLY_EXTERN (result) = 0;
12937 else
12939 mark_definable (result);
12940 /* Always make artificials weak. */
12941 if (DECL_ARTIFICIAL (result) && flag_weak)
12942 comdat_linkage (result);
12943 /* For WIN32 we also want to put explicit instantiations in
12944 linkonce sections. */
12945 else if (TREE_PUBLIC (result))
12946 maybe_make_one_only (result);
12949 /* If EXTERN_P, then this function will not be emitted -- unless
12950 followed by an explicit instantiation, at which point its linkage
12951 will be adjusted. If !EXTERN_P, then this function will be
12952 emitted here. In neither circumstance do we want
12953 import_export_decl to adjust the linkage. */
12954 DECL_INTERFACE_KNOWN (result) = 1;
12957 /* Given two function templates PAT1 and PAT2, return:
12959 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
12960 -1 if PAT2 is more specialized than PAT1.
12961 0 if neither is more specialized.
12963 LEN indicates the number of parameters we should consider
12964 (defaulted parameters should not be considered).
12966 The 1998 std underspecified function template partial ordering, and
12967 DR214 addresses the issue. We take pairs of arguments, one from
12968 each of the templates, and deduce them against each other. One of
12969 the templates will be more specialized if all the *other*
12970 template's arguments deduce against its arguments and at least one
12971 of its arguments *does* *not* deduce against the other template's
12972 corresponding argument. Deduction is done as for class templates.
12973 The arguments used in deduction have reference and top level cv
12974 qualifiers removed. Iff both arguments were originally reference
12975 types *and* deduction succeeds in both directions, the template
12976 with the more cv-qualified argument wins for that pairing (if
12977 neither is more cv-qualified, they both are equal). Unlike regular
12978 deduction, after all the arguments have been deduced in this way,
12979 we do *not* verify the deduced template argument values can be
12980 substituted into non-deduced contexts, nor do we have to verify
12981 that all template arguments have been deduced. */
12984 more_specialized_fn (tree pat1, tree pat2, int len)
12986 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
12987 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
12988 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
12989 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
12990 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
12991 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
12992 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
12993 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
12994 int better1 = 0;
12995 int better2 = 0;
12997 /* Remove the this parameter from non-static member functions. If
12998 one is a non-static member function and the other is not a static
12999 member function, remove the first parameter from that function
13000 also. This situation occurs for operator functions where we
13001 locate both a member function (with this pointer) and non-member
13002 operator (with explicit first operand). */
13003 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
13005 len--; /* LEN is the number of significant arguments for DECL1 */
13006 args1 = TREE_CHAIN (args1);
13007 if (!DECL_STATIC_FUNCTION_P (decl2))
13008 args2 = TREE_CHAIN (args2);
13010 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
13012 args2 = TREE_CHAIN (args2);
13013 if (!DECL_STATIC_FUNCTION_P (decl1))
13015 len--;
13016 args1 = TREE_CHAIN (args1);
13020 /* If only one is a conversion operator, they are unordered. */
13021 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
13022 return 0;
13024 /* Consider the return type for a conversion function */
13025 if (DECL_CONV_FN_P (decl1))
13027 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
13028 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
13029 len++;
13032 processing_template_decl++;
13034 while (len--)
13036 tree arg1 = TREE_VALUE (args1);
13037 tree arg2 = TREE_VALUE (args2);
13038 int deduce1, deduce2;
13039 int quals1 = -1;
13040 int quals2 = -1;
13042 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13043 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13045 /* When both arguments are pack expansions, we need only
13046 unify the patterns themselves. */
13047 arg1 = PACK_EXPANSION_PATTERN (arg1);
13048 arg2 = PACK_EXPANSION_PATTERN (arg2);
13050 /* This is the last comparison we need to do. */
13051 len = 0;
13054 if (TREE_CODE (arg1) == REFERENCE_TYPE)
13056 arg1 = TREE_TYPE (arg1);
13057 quals1 = cp_type_quals (arg1);
13060 if (TREE_CODE (arg2) == REFERENCE_TYPE)
13062 arg2 = TREE_TYPE (arg2);
13063 quals2 = cp_type_quals (arg2);
13066 if ((quals1 < 0) != (quals2 < 0))
13068 /* Only of the args is a reference, see if we should apply
13069 array/function pointer decay to it. This is not part of
13070 DR214, but is, IMHO, consistent with the deduction rules
13071 for the function call itself, and with our earlier
13072 implementation of the underspecified partial ordering
13073 rules. (nathan). */
13074 if (quals1 >= 0)
13076 switch (TREE_CODE (arg1))
13078 case ARRAY_TYPE:
13079 arg1 = TREE_TYPE (arg1);
13080 /* FALLTHROUGH. */
13081 case FUNCTION_TYPE:
13082 arg1 = build_pointer_type (arg1);
13083 break;
13085 default:
13086 break;
13089 else
13091 switch (TREE_CODE (arg2))
13093 case ARRAY_TYPE:
13094 arg2 = TREE_TYPE (arg2);
13095 /* FALLTHROUGH. */
13096 case FUNCTION_TYPE:
13097 arg2 = build_pointer_type (arg2);
13098 break;
13100 default:
13101 break;
13106 arg1 = TYPE_MAIN_VARIANT (arg1);
13107 arg2 = TYPE_MAIN_VARIANT (arg2);
13109 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
13111 int i, len2 = list_length (args2);
13112 tree parmvec = make_tree_vec (1);
13113 tree argvec = make_tree_vec (len2);
13114 tree ta = args2;
13116 /* Setup the parameter vector, which contains only ARG1. */
13117 TREE_VEC_ELT (parmvec, 0) = arg1;
13119 /* Setup the argument vector, which contains the remaining
13120 arguments. */
13121 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
13122 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13124 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
13125 argvec, UNIFY_ALLOW_NONE,
13126 /*call_args_p=*/false,
13127 /*subr=*/0);
13129 /* We cannot deduce in the other direction, because ARG1 is
13130 a pack expansion but ARG2 is not. */
13131 deduce2 = 0;
13133 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13135 int i, len1 = list_length (args1);
13136 tree parmvec = make_tree_vec (1);
13137 tree argvec = make_tree_vec (len1);
13138 tree ta = args1;
13140 /* Setup the parameter vector, which contains only ARG1. */
13141 TREE_VEC_ELT (parmvec, 0) = arg2;
13143 /* Setup the argument vector, which contains the remaining
13144 arguments. */
13145 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
13146 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13148 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
13149 argvec, UNIFY_ALLOW_NONE,
13150 /*call_args_p=*/false,
13151 /*subr=*/0);
13153 /* We cannot deduce in the other direction, because ARG2 is
13154 a pack expansion but ARG1 is not.*/
13155 deduce1 = 0;
13158 else
13160 /* The normal case, where neither argument is a pack
13161 expansion. */
13162 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
13163 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
13166 if (!deduce1)
13167 better2 = -1;
13168 if (!deduce2)
13169 better1 = -1;
13170 if (better1 < 0 && better2 < 0)
13171 /* We've failed to deduce something in either direction.
13172 These must be unordered. */
13173 break;
13175 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
13177 /* Deduces in both directions, see if quals can
13178 disambiguate. Pretend the worse one failed to deduce. */
13179 if ((quals1 & quals2) == quals2)
13180 deduce1 = 0;
13181 if ((quals1 & quals2) == quals1)
13182 deduce2 = 0;
13184 if (deduce1 && !deduce2 && !better2)
13185 better2 = 1;
13186 if (deduce2 && !deduce1 && !better1)
13187 better1 = 1;
13189 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13190 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13191 /* We have already processed all of the arguments in our
13192 handing of the pack expansion type. */
13193 len = 0;
13195 args1 = TREE_CHAIN (args1);
13196 args2 = TREE_CHAIN (args2);
13199 processing_template_decl--;
13201 /* All things being equal, if the next argument is a pack expansion
13202 for one function but not for the other, prefer the
13203 non-variadic function. */
13204 if ((better1 > 0) - (better2 > 0) == 0
13205 && args1 && TREE_VALUE (args1)
13206 && args2 && TREE_VALUE (args2))
13208 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
13209 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
13210 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
13211 return 1;
13214 return (better1 > 0) - (better2 > 0);
13217 /* Determine which of two partial specializations is more specialized.
13219 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13220 to the first partial specialization. The TREE_VALUE is the
13221 innermost set of template parameters for the partial
13222 specialization. PAT2 is similar, but for the second template.
13224 Return 1 if the first partial specialization is more specialized;
13225 -1 if the second is more specialized; 0 if neither is more
13226 specialized.
13228 See [temp.class.order] for information about determining which of
13229 two templates is more specialized. */
13231 static int
13232 more_specialized_class (tree pat1, tree pat2)
13234 tree targs;
13235 tree tmpl1, tmpl2;
13236 int winner = 0;
13237 bool any_deductions = false;
13239 tmpl1 = TREE_TYPE (pat1);
13240 tmpl2 = TREE_TYPE (pat2);
13242 /* Just like what happens for functions, if we are ordering between
13243 different class template specializations, we may encounter dependent
13244 types in the arguments, and we need our dependency check functions
13245 to behave correctly. */
13246 ++processing_template_decl;
13247 targs = get_class_bindings (TREE_VALUE (pat1),
13248 CLASSTYPE_TI_ARGS (tmpl1),
13249 CLASSTYPE_TI_ARGS (tmpl2));
13250 if (targs)
13252 --winner;
13253 any_deductions = true;
13256 targs = get_class_bindings (TREE_VALUE (pat2),
13257 CLASSTYPE_TI_ARGS (tmpl2),
13258 CLASSTYPE_TI_ARGS (tmpl1));
13259 if (targs)
13261 ++winner;
13262 any_deductions = true;
13264 --processing_template_decl;
13266 /* In the case of a tie where at least one of the class templates
13267 has a parameter pack at the end, the template with the most
13268 non-packed parameters wins. */
13269 if (winner == 0
13270 && any_deductions
13271 && (template_args_variadic_p (TREE_PURPOSE (pat1))
13272 || template_args_variadic_p (TREE_PURPOSE (pat2))))
13274 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
13275 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
13276 int len1 = TREE_VEC_LENGTH (args1);
13277 int len2 = TREE_VEC_LENGTH (args2);
13279 /* We don't count the pack expansion at the end. */
13280 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
13281 --len1;
13282 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
13283 --len2;
13285 if (len1 > len2)
13286 return 1;
13287 else if (len1 < len2)
13288 return -1;
13291 return winner;
13294 /* Return the template arguments that will produce the function signature
13295 DECL from the function template FN, with the explicit template
13296 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13297 also match. Return NULL_TREE if no satisfactory arguments could be
13298 found. */
13300 static tree
13301 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
13303 int ntparms = DECL_NTPARMS (fn);
13304 tree targs = make_tree_vec (ntparms);
13305 tree decl_type;
13306 tree decl_arg_types;
13308 /* Substitute the explicit template arguments into the type of DECL.
13309 The call to fn_type_unification will handle substitution into the
13310 FN. */
13311 decl_type = TREE_TYPE (decl);
13312 if (explicit_args && uses_template_parms (decl_type))
13314 tree tmpl;
13315 tree converted_args;
13317 if (DECL_TEMPLATE_INFO (decl))
13318 tmpl = DECL_TI_TEMPLATE (decl);
13319 else
13320 /* We can get here for some invalid specializations. */
13321 return NULL_TREE;
13323 converted_args
13324 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
13325 explicit_args, NULL_TREE,
13326 tf_none,
13327 /*require_all_args=*/false,
13328 /*use_default_args=*/false);
13329 if (converted_args == error_mark_node)
13330 return NULL_TREE;
13332 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
13333 if (decl_type == error_mark_node)
13334 return NULL_TREE;
13337 /* Never do unification on the 'this' parameter. */
13338 decl_arg_types = skip_artificial_parms_for (decl,
13339 TYPE_ARG_TYPES (decl_type));
13341 if (fn_type_unification (fn, explicit_args, targs,
13342 decl_arg_types,
13343 (check_rettype || DECL_CONV_FN_P (fn)
13344 ? TREE_TYPE (decl_type) : NULL_TREE),
13345 DEDUCE_EXACT, LOOKUP_NORMAL))
13346 return NULL_TREE;
13348 return targs;
13351 /* Return the innermost template arguments that, when applied to a
13352 template specialization whose innermost template parameters are
13353 TPARMS, and whose specialization arguments are PARMS, yield the
13354 ARGS.
13356 For example, suppose we have:
13358 template <class T, class U> struct S {};
13359 template <class T> struct S<T*, int> {};
13361 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13362 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13363 int}. The resulting vector will be {double}, indicating that `T'
13364 is bound to `double'. */
13366 static tree
13367 get_class_bindings (tree tparms, tree spec_args, tree args)
13369 int i, ntparms = TREE_VEC_LENGTH (tparms);
13370 tree deduced_args;
13371 tree innermost_deduced_args;
13373 innermost_deduced_args = make_tree_vec (ntparms);
13374 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13376 deduced_args = copy_node (args);
13377 SET_TMPL_ARGS_LEVEL (deduced_args,
13378 TMPL_ARGS_DEPTH (deduced_args),
13379 innermost_deduced_args);
13381 else
13382 deduced_args = innermost_deduced_args;
13384 if (unify (tparms, deduced_args,
13385 INNERMOST_TEMPLATE_ARGS (spec_args),
13386 INNERMOST_TEMPLATE_ARGS (args),
13387 UNIFY_ALLOW_NONE))
13388 return NULL_TREE;
13390 for (i = 0; i < ntparms; ++i)
13391 if (! TREE_VEC_ELT (innermost_deduced_args, i))
13392 return NULL_TREE;
13394 /* Verify that nondeduced template arguments agree with the type
13395 obtained from argument deduction.
13397 For example:
13399 struct A { typedef int X; };
13400 template <class T, class U> struct C {};
13401 template <class T> struct C<T, typename T::X> {};
13403 Then with the instantiation `C<A, int>', we can deduce that
13404 `T' is `A' but unify () does not check whether `typename T::X'
13405 is `int'. */
13406 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
13407 if (spec_args == error_mark_node
13408 /* We only need to check the innermost arguments; the other
13409 arguments will always agree. */
13410 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
13411 INNERMOST_TEMPLATE_ARGS (args)))
13412 return NULL_TREE;
13414 return deduced_args;
13417 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13418 Return the TREE_LIST node with the most specialized template, if
13419 any. If there is no most specialized template, the error_mark_node
13420 is returned.
13422 Note that this function does not look at, or modify, the
13423 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13424 returned is one of the elements of INSTANTIATIONS, callers may
13425 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13426 and retrieve it from the value returned. */
13428 tree
13429 most_specialized_instantiation (tree templates)
13431 tree fn, champ;
13433 ++processing_template_decl;
13435 champ = templates;
13436 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
13438 int fate = 0;
13440 if (get_bindings (TREE_VALUE (champ),
13441 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13442 NULL_TREE, /*check_ret=*/false))
13443 fate--;
13445 if (get_bindings (TREE_VALUE (fn),
13446 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13447 NULL_TREE, /*check_ret=*/false))
13448 fate++;
13450 if (fate == -1)
13451 champ = fn;
13452 else if (!fate)
13454 /* Equally specialized, move to next function. If there
13455 is no next function, nothing's most specialized. */
13456 fn = TREE_CHAIN (fn);
13457 champ = fn;
13458 if (!fn)
13459 break;
13463 if (champ)
13464 /* Now verify that champ is better than everything earlier in the
13465 instantiation list. */
13466 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
13467 if (get_bindings (TREE_VALUE (champ),
13468 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13469 NULL_TREE, /*check_ret=*/false)
13470 || !get_bindings (TREE_VALUE (fn),
13471 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13472 NULL_TREE, /*check_ret=*/false))
13474 champ = NULL_TREE;
13475 break;
13478 processing_template_decl--;
13480 if (!champ)
13481 return error_mark_node;
13483 return champ;
13486 /* If DECL is a specialization of some template, return the most
13487 general such template. Otherwise, returns NULL_TREE.
13489 For example, given:
13491 template <class T> struct S { template <class U> void f(U); };
13493 if TMPL is `template <class U> void S<int>::f(U)' this will return
13494 the full template. This function will not trace past partial
13495 specializations, however. For example, given in addition:
13497 template <class T> struct S<T*> { template <class U> void f(U); };
13499 if TMPL is `template <class U> void S<int*>::f(U)' this will return
13500 `template <class T> template <class U> S<T*>::f(U)'. */
13502 tree
13503 most_general_template (tree decl)
13505 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
13506 an immediate specialization. */
13507 if (TREE_CODE (decl) == FUNCTION_DECL)
13509 if (DECL_TEMPLATE_INFO (decl)) {
13510 decl = DECL_TI_TEMPLATE (decl);
13512 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
13513 template friend. */
13514 if (TREE_CODE (decl) != TEMPLATE_DECL)
13515 return NULL_TREE;
13516 } else
13517 return NULL_TREE;
13520 /* Look for more and more general templates. */
13521 while (DECL_TEMPLATE_INFO (decl))
13523 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
13524 (See cp-tree.h for details.) */
13525 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
13526 break;
13528 if (CLASS_TYPE_P (TREE_TYPE (decl))
13529 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
13530 break;
13532 /* Stop if we run into an explicitly specialized class template. */
13533 if (!DECL_NAMESPACE_SCOPE_P (decl)
13534 && DECL_CONTEXT (decl)
13535 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
13536 break;
13538 decl = DECL_TI_TEMPLATE (decl);
13541 return decl;
13544 /* Return the most specialized of the class template partial
13545 specializations of TMPL which can produce TYPE, a specialization of
13546 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
13547 a _TYPE node corresponding to the partial specialization, while the
13548 TREE_PURPOSE is the set of template arguments that must be
13549 substituted into the TREE_TYPE in order to generate TYPE.
13551 If the choice of partial specialization is ambiguous, a diagnostic
13552 is issued, and the error_mark_node is returned. If there are no
13553 partial specializations of TMPL matching TYPE, then NULL_TREE is
13554 returned. */
13556 static tree
13557 most_specialized_class (tree type, tree tmpl)
13559 tree list = NULL_TREE;
13560 tree t;
13561 tree champ;
13562 int fate;
13563 bool ambiguous_p;
13564 tree args;
13566 tmpl = most_general_template (tmpl);
13567 args = CLASSTYPE_TI_ARGS (type);
13568 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
13570 tree partial_spec_args;
13571 tree spec_args;
13573 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
13574 spec_args = get_class_bindings (TREE_VALUE (t),
13575 partial_spec_args,
13576 args);
13577 if (spec_args)
13579 list = tree_cons (spec_args, TREE_VALUE (t), list);
13580 TREE_TYPE (list) = TREE_TYPE (t);
13584 if (! list)
13585 return NULL_TREE;
13587 ambiguous_p = false;
13588 t = list;
13589 champ = t;
13590 t = TREE_CHAIN (t);
13591 for (; t; t = TREE_CHAIN (t))
13593 fate = more_specialized_class (champ, t);
13594 if (fate == 1)
13596 else
13598 if (fate == 0)
13600 t = TREE_CHAIN (t);
13601 if (! t)
13603 ambiguous_p = true;
13604 break;
13607 champ = t;
13611 if (!ambiguous_p)
13612 for (t = list; t && t != champ; t = TREE_CHAIN (t))
13614 fate = more_specialized_class (champ, t);
13615 if (fate != 1)
13617 ambiguous_p = true;
13618 break;
13622 if (ambiguous_p)
13624 const char *str = "candidates are:";
13625 error ("ambiguous class template instantiation for %q#T", type);
13626 for (t = list; t; t = TREE_CHAIN (t))
13628 error ("%s %+#T", str, TREE_TYPE (t));
13629 str = " ";
13631 return error_mark_node;
13634 return champ;
13637 /* Explicitly instantiate DECL. */
13639 void
13640 do_decl_instantiation (tree decl, tree storage)
13642 tree result = NULL_TREE;
13643 int extern_p = 0;
13645 if (!decl || decl == error_mark_node)
13646 /* An error occurred, for which grokdeclarator has already issued
13647 an appropriate message. */
13648 return;
13649 else if (! DECL_LANG_SPECIFIC (decl))
13651 error ("explicit instantiation of non-template %q#D", decl);
13652 return;
13654 else if (TREE_CODE (decl) == VAR_DECL)
13656 /* There is an asymmetry here in the way VAR_DECLs and
13657 FUNCTION_DECLs are handled by grokdeclarator. In the case of
13658 the latter, the DECL we get back will be marked as a
13659 template instantiation, and the appropriate
13660 DECL_TEMPLATE_INFO will be set up. This does not happen for
13661 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
13662 should handle VAR_DECLs as it currently handles
13663 FUNCTION_DECLs. */
13664 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
13665 if (!result || TREE_CODE (result) != VAR_DECL)
13667 error ("no matching template for %qD found", decl);
13668 return;
13670 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
13672 error ("type %qT for explicit instantiation %qD does not match "
13673 "declared type %qT", TREE_TYPE (result), decl,
13674 TREE_TYPE (decl));
13675 return;
13678 else if (TREE_CODE (decl) != FUNCTION_DECL)
13680 error ("explicit instantiation of %q#D", decl);
13681 return;
13683 else
13684 result = decl;
13686 /* Check for various error cases. Note that if the explicit
13687 instantiation is valid the RESULT will currently be marked as an
13688 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
13689 until we get here. */
13691 if (DECL_TEMPLATE_SPECIALIZATION (result))
13693 /* DR 259 [temp.spec].
13695 Both an explicit instantiation and a declaration of an explicit
13696 specialization shall not appear in a program unless the explicit
13697 instantiation follows a declaration of the explicit specialization.
13699 For a given set of template parameters, if an explicit
13700 instantiation of a template appears after a declaration of an
13701 explicit specialization for that template, the explicit
13702 instantiation has no effect. */
13703 return;
13705 else if (DECL_EXPLICIT_INSTANTIATION (result))
13707 /* [temp.spec]
13709 No program shall explicitly instantiate any template more
13710 than once.
13712 We check DECL_NOT_REALLY_EXTERN so as not to complain when
13713 the first instantiation was `extern' and the second is not,
13714 and EXTERN_P for the opposite case. */
13715 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
13716 pedwarn ("duplicate explicit instantiation of %q#D", result);
13717 /* If an "extern" explicit instantiation follows an ordinary
13718 explicit instantiation, the template is instantiated. */
13719 if (extern_p)
13720 return;
13722 else if (!DECL_IMPLICIT_INSTANTIATION (result))
13724 error ("no matching template for %qD found", result);
13725 return;
13727 else if (!DECL_TEMPLATE_INFO (result))
13729 pedwarn ("explicit instantiation of non-template %q#D", result);
13730 return;
13733 if (storage == NULL_TREE)
13735 else if (storage == ridpointers[(int) RID_EXTERN])
13737 if (pedantic && !in_system_header)
13738 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
13739 "instantiations");
13740 extern_p = 1;
13742 else
13743 error ("storage class %qD applied to template instantiation", storage);
13745 check_explicit_instantiation_namespace (result);
13746 mark_decl_instantiated (result, extern_p);
13747 if (! extern_p)
13748 instantiate_decl (result, /*defer_ok=*/1,
13749 /*expl_inst_class_mem_p=*/false);
13752 static void
13753 mark_class_instantiated (tree t, int extern_p)
13755 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
13756 SET_CLASSTYPE_INTERFACE_KNOWN (t);
13757 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
13758 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
13759 if (! extern_p)
13761 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
13762 rest_of_type_compilation (t, 1);
13766 /* Called from do_type_instantiation through binding_table_foreach to
13767 do recursive instantiation for the type bound in ENTRY. */
13768 static void
13769 bt_instantiate_type_proc (binding_entry entry, void *data)
13771 tree storage = *(tree *) data;
13773 if (IS_AGGR_TYPE (entry->type)
13774 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
13775 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
13778 /* Called from do_type_instantiation to instantiate a member
13779 (a member function or a static member variable) of an
13780 explicitly instantiated class template. */
13781 static void
13782 instantiate_class_member (tree decl, int extern_p)
13784 mark_decl_instantiated (decl, extern_p);
13785 if (! extern_p)
13786 instantiate_decl (decl, /*defer_ok=*/1,
13787 /*expl_inst_class_mem_p=*/true);
13790 /* Perform an explicit instantiation of template class T. STORAGE, if
13791 non-null, is the RID for extern, inline or static. COMPLAIN is
13792 nonzero if this is called from the parser, zero if called recursively,
13793 since the standard is unclear (as detailed below). */
13795 void
13796 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
13798 int extern_p = 0;
13799 int nomem_p = 0;
13800 int static_p = 0;
13801 int previous_instantiation_extern_p = 0;
13803 if (TREE_CODE (t) == TYPE_DECL)
13804 t = TREE_TYPE (t);
13806 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
13808 error ("explicit instantiation of non-template type %qT", t);
13809 return;
13812 complete_type (t);
13814 if (!COMPLETE_TYPE_P (t))
13816 if (complain & tf_error)
13817 error ("explicit instantiation of %q#T before definition of template",
13819 return;
13822 if (storage != NULL_TREE)
13824 if (pedantic && !in_system_header)
13825 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
13826 storage);
13828 if (storage == ridpointers[(int) RID_INLINE])
13829 nomem_p = 1;
13830 else if (storage == ridpointers[(int) RID_EXTERN])
13831 extern_p = 1;
13832 else if (storage == ridpointers[(int) RID_STATIC])
13833 static_p = 1;
13834 else
13836 error ("storage class %qD applied to template instantiation",
13837 storage);
13838 extern_p = 0;
13842 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
13844 /* DR 259 [temp.spec].
13846 Both an explicit instantiation and a declaration of an explicit
13847 specialization shall not appear in a program unless the explicit
13848 instantiation follows a declaration of the explicit specialization.
13850 For a given set of template parameters, if an explicit
13851 instantiation of a template appears after a declaration of an
13852 explicit specialization for that template, the explicit
13853 instantiation has no effect. */
13854 return;
13856 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
13858 /* [temp.spec]
13860 No program shall explicitly instantiate any template more
13861 than once.
13863 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
13864 instantiation was `extern'. If EXTERN_P then the second is.
13865 These cases are OK. */
13866 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
13868 if (!previous_instantiation_extern_p && !extern_p
13869 && (complain & tf_error))
13870 pedwarn ("duplicate explicit instantiation of %q#T", t);
13872 /* If we've already instantiated the template, just return now. */
13873 if (!CLASSTYPE_INTERFACE_ONLY (t))
13874 return;
13877 check_explicit_instantiation_namespace (TYPE_NAME (t));
13878 mark_class_instantiated (t, extern_p);
13880 if (nomem_p)
13881 return;
13884 tree tmp;
13886 /* In contrast to implicit instantiation, where only the
13887 declarations, and not the definitions, of members are
13888 instantiated, we have here:
13890 [temp.explicit]
13892 The explicit instantiation of a class template specialization
13893 implies the instantiation of all of its members not
13894 previously explicitly specialized in the translation unit
13895 containing the explicit instantiation.
13897 Of course, we can't instantiate member template classes, since
13898 we don't have any arguments for them. Note that the standard
13899 is unclear on whether the instantiation of the members are
13900 *explicit* instantiations or not. However, the most natural
13901 interpretation is that it should be an explicit instantiation. */
13903 if (! static_p)
13904 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
13905 if (TREE_CODE (tmp) == FUNCTION_DECL
13906 && DECL_TEMPLATE_INSTANTIATION (tmp))
13907 instantiate_class_member (tmp, extern_p);
13909 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
13910 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
13911 instantiate_class_member (tmp, extern_p);
13913 if (CLASSTYPE_NESTED_UTDS (t))
13914 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
13915 bt_instantiate_type_proc, &storage);
13919 /* Given a function DECL, which is a specialization of TMPL, modify
13920 DECL to be a re-instantiation of TMPL with the same template
13921 arguments. TMPL should be the template into which tsubst'ing
13922 should occur for DECL, not the most general template.
13924 One reason for doing this is a scenario like this:
13926 template <class T>
13927 void f(const T&, int i);
13929 void g() { f(3, 7); }
13931 template <class T>
13932 void f(const T& t, const int i) { }
13934 Note that when the template is first instantiated, with
13935 instantiate_template, the resulting DECL will have no name for the
13936 first parameter, and the wrong type for the second. So, when we go
13937 to instantiate the DECL, we regenerate it. */
13939 static void
13940 regenerate_decl_from_template (tree decl, tree tmpl)
13942 /* The arguments used to instantiate DECL, from the most general
13943 template. */
13944 tree args;
13945 tree code_pattern;
13947 args = DECL_TI_ARGS (decl);
13948 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
13950 /* Make sure that we can see identifiers, and compute access
13951 correctly. */
13952 push_access_scope (decl);
13954 if (TREE_CODE (decl) == FUNCTION_DECL)
13956 tree decl_parm;
13957 tree pattern_parm;
13958 tree specs;
13959 int args_depth;
13960 int parms_depth;
13962 args_depth = TMPL_ARGS_DEPTH (args);
13963 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
13964 if (args_depth > parms_depth)
13965 args = get_innermost_template_args (args, parms_depth);
13967 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
13968 args, tf_error, NULL_TREE);
13969 if (specs)
13970 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
13971 specs);
13973 /* Merge parameter declarations. */
13974 decl_parm = skip_artificial_parms_for (decl,
13975 DECL_ARGUMENTS (decl));
13976 pattern_parm
13977 = skip_artificial_parms_for (code_pattern,
13978 DECL_ARGUMENTS (code_pattern));
13979 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
13981 tree parm_type;
13982 tree attributes;
13984 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
13985 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
13986 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
13987 NULL_TREE);
13988 parm_type = type_decays_to (parm_type);
13989 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
13990 TREE_TYPE (decl_parm) = parm_type;
13991 attributes = DECL_ATTRIBUTES (pattern_parm);
13992 if (DECL_ATTRIBUTES (decl_parm) != attributes)
13994 DECL_ATTRIBUTES (decl_parm) = attributes;
13995 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
13997 decl_parm = TREE_CHAIN (decl_parm);
13998 pattern_parm = TREE_CHAIN (pattern_parm);
14000 /* Merge any parameters that match with the function parameter
14001 pack. */
14002 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
14004 int i, len;
14005 tree expanded_types;
14006 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14007 the parameters in this function parameter pack. */
14008 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
14009 args, tf_error, NULL_TREE);
14010 len = TREE_VEC_LENGTH (expanded_types);
14011 for (i = 0; i < len; i++)
14013 tree parm_type;
14014 tree attributes;
14016 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
14017 /* Rename the parameter to include the index. */
14018 DECL_NAME (decl_parm) =
14019 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
14020 parm_type = TREE_VEC_ELT (expanded_types, i);
14021 parm_type = type_decays_to (parm_type);
14022 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
14023 TREE_TYPE (decl_parm) = parm_type;
14024 attributes = DECL_ATTRIBUTES (pattern_parm);
14025 if (DECL_ATTRIBUTES (decl_parm) != attributes)
14027 DECL_ATTRIBUTES (decl_parm) = attributes;
14028 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
14030 decl_parm = TREE_CHAIN (decl_parm);
14033 /* Merge additional specifiers from the CODE_PATTERN. */
14034 if (DECL_DECLARED_INLINE_P (code_pattern)
14035 && !DECL_DECLARED_INLINE_P (decl))
14036 DECL_DECLARED_INLINE_P (decl) = 1;
14037 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
14038 DECL_INLINE (decl) = 1;
14040 else if (TREE_CODE (decl) == VAR_DECL)
14041 DECL_INITIAL (decl) =
14042 tsubst_expr (DECL_INITIAL (code_pattern), args,
14043 tf_error, DECL_TI_TEMPLATE (decl),
14044 /*integral_constant_expression_p=*/false);
14045 else
14046 gcc_unreachable ();
14048 pop_access_scope (decl);
14051 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
14052 substituted to get DECL. */
14054 tree
14055 template_for_substitution (tree decl)
14057 tree tmpl = DECL_TI_TEMPLATE (decl);
14059 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
14060 for the instantiation. This is not always the most general
14061 template. Consider, for example:
14063 template <class T>
14064 struct S { template <class U> void f();
14065 template <> void f<int>(); };
14067 and an instantiation of S<double>::f<int>. We want TD to be the
14068 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
14069 while (/* An instantiation cannot have a definition, so we need a
14070 more general template. */
14071 DECL_TEMPLATE_INSTANTIATION (tmpl)
14072 /* We must also deal with friend templates. Given:
14074 template <class T> struct S {
14075 template <class U> friend void f() {};
14078 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
14079 so far as the language is concerned, but that's still
14080 where we get the pattern for the instantiation from. On
14081 other hand, if the definition comes outside the class, say:
14083 template <class T> struct S {
14084 template <class U> friend void f();
14086 template <class U> friend void f() {}
14088 we don't need to look any further. That's what the check for
14089 DECL_INITIAL is for. */
14090 || (TREE_CODE (decl) == FUNCTION_DECL
14091 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
14092 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
14094 /* The present template, TD, should not be a definition. If it
14095 were a definition, we should be using it! Note that we
14096 cannot restructure the loop to just keep going until we find
14097 a template with a definition, since that might go too far if
14098 a specialization was declared, but not defined. */
14099 gcc_assert (TREE_CODE (decl) != VAR_DECL
14100 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
14102 /* Fetch the more general template. */
14103 tmpl = DECL_TI_TEMPLATE (tmpl);
14106 return tmpl;
14109 /* Produce the definition of D, a _DECL generated from a template. If
14110 DEFER_OK is nonzero, then we don't have to actually do the
14111 instantiation now; we just have to do it sometime. Normally it is
14112 an error if this is an explicit instantiation but D is undefined.
14113 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
14114 explicitly instantiated class template. */
14116 tree
14117 instantiate_decl (tree d, int defer_ok,
14118 bool expl_inst_class_mem_p)
14120 tree tmpl = DECL_TI_TEMPLATE (d);
14121 tree gen_args;
14122 tree args;
14123 tree td;
14124 tree code_pattern;
14125 tree spec;
14126 tree gen_tmpl;
14127 bool pattern_defined;
14128 int need_push;
14129 location_t saved_loc = input_location;
14130 int saved_in_system_header = in_system_header;
14131 bool external_p;
14133 /* This function should only be used to instantiate templates for
14134 functions and static member variables. */
14135 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
14136 || TREE_CODE (d) == VAR_DECL);
14138 /* Variables are never deferred; if instantiation is required, they
14139 are instantiated right away. That allows for better code in the
14140 case that an expression refers to the value of the variable --
14141 if the variable has a constant value the referring expression can
14142 take advantage of that fact. */
14143 if (TREE_CODE (d) == VAR_DECL)
14144 defer_ok = 0;
14146 /* Don't instantiate cloned functions. Instead, instantiate the
14147 functions they cloned. */
14148 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
14149 d = DECL_CLONED_FUNCTION (d);
14151 if (DECL_TEMPLATE_INSTANTIATED (d))
14152 /* D has already been instantiated. It might seem reasonable to
14153 check whether or not D is an explicit instantiation, and, if so,
14154 stop here. But when an explicit instantiation is deferred
14155 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
14156 is set, even though we still need to do the instantiation. */
14157 return d;
14159 /* If we already have a specialization of this declaration, then
14160 there's no reason to instantiate it. Note that
14161 retrieve_specialization gives us both instantiations and
14162 specializations, so we must explicitly check
14163 DECL_TEMPLATE_SPECIALIZATION. */
14164 gen_tmpl = most_general_template (tmpl);
14165 gen_args = DECL_TI_ARGS (d);
14166 spec = retrieve_specialization (gen_tmpl, gen_args,
14167 /*class_specializations_p=*/false);
14168 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
14169 return spec;
14171 /* This needs to happen before any tsubsting. */
14172 if (! push_tinst_level (d))
14173 return d;
14175 timevar_push (TV_PARSE);
14177 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
14178 for the instantiation. */
14179 td = template_for_substitution (d);
14180 code_pattern = DECL_TEMPLATE_RESULT (td);
14182 /* We should never be trying to instantiate a member of a class
14183 template or partial specialization. */
14184 gcc_assert (d != code_pattern);
14186 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
14187 || DECL_TEMPLATE_SPECIALIZATION (td))
14188 /* In the case of a friend template whose definition is provided
14189 outside the class, we may have too many arguments. Drop the
14190 ones we don't need. The same is true for specializations. */
14191 args = get_innermost_template_args
14192 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
14193 else
14194 args = gen_args;
14196 if (TREE_CODE (d) == FUNCTION_DECL)
14197 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
14198 else
14199 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
14201 /* We may be in the middle of deferred access check. Disable it now. */
14202 push_deferring_access_checks (dk_no_deferred);
14204 /* Unless an explicit instantiation directive has already determined
14205 the linkage of D, remember that a definition is available for
14206 this entity. */
14207 if (pattern_defined
14208 && !DECL_INTERFACE_KNOWN (d)
14209 && !DECL_NOT_REALLY_EXTERN (d))
14210 mark_definable (d);
14212 input_location = DECL_SOURCE_LOCATION (d);
14213 in_system_header = DECL_IN_SYSTEM_HEADER (d);
14215 /* If D is a member of an explicitly instantiated class template,
14216 and no definition is available, treat it like an implicit
14217 instantiation. */
14218 if (!pattern_defined && expl_inst_class_mem_p
14219 && DECL_EXPLICIT_INSTANTIATION (d))
14221 DECL_NOT_REALLY_EXTERN (d) = 0;
14222 DECL_INTERFACE_KNOWN (d) = 0;
14223 SET_DECL_IMPLICIT_INSTANTIATION (d);
14226 if (!defer_ok)
14228 /* Recheck the substitutions to obtain any warning messages
14229 about ignoring cv qualifiers. */
14230 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
14231 tree type = TREE_TYPE (gen);
14233 /* Make sure that we can see identifiers, and compute access
14234 correctly. D is already the target FUNCTION_DECL with the
14235 right context. */
14236 push_access_scope (d);
14238 if (TREE_CODE (gen) == FUNCTION_DECL)
14240 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
14241 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
14243 /* Don't simply tsubst the function type, as that will give
14244 duplicate warnings about poor parameter qualifications.
14245 The function arguments are the same as the decl_arguments
14246 without the top level cv qualifiers. */
14247 type = TREE_TYPE (type);
14249 tsubst (type, gen_args, tf_warning_or_error, d);
14251 pop_access_scope (d);
14254 /* Check to see whether we know that this template will be
14255 instantiated in some other file, as with "extern template"
14256 extension. */
14257 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
14258 /* In general, we do not instantiate such templates... */
14259 if (external_p
14260 /* ... but we instantiate inline functions so that we can inline
14261 them and ... */
14262 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
14263 /* ... we instantiate static data members whose values are
14264 needed in integral constant expressions. */
14265 && ! (TREE_CODE (d) == VAR_DECL
14266 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
14267 goto out;
14268 /* Defer all other templates, unless we have been explicitly
14269 forbidden from doing so. */
14270 if (/* If there is no definition, we cannot instantiate the
14271 template. */
14272 ! pattern_defined
14273 /* If it's OK to postpone instantiation, do so. */
14274 || defer_ok
14275 /* If this is a static data member that will be defined
14276 elsewhere, we don't want to instantiate the entire data
14277 member, but we do want to instantiate the initializer so that
14278 we can substitute that elsewhere. */
14279 || (external_p && TREE_CODE (d) == VAR_DECL))
14281 /* The definition of the static data member is now required so
14282 we must substitute the initializer. */
14283 if (TREE_CODE (d) == VAR_DECL
14284 && !DECL_INITIAL (d)
14285 && DECL_INITIAL (code_pattern))
14287 tree ns;
14288 tree init;
14290 ns = decl_namespace_context (d);
14291 push_nested_namespace (ns);
14292 push_nested_class (DECL_CONTEXT (d));
14293 init = tsubst_expr (DECL_INITIAL (code_pattern),
14294 args,
14295 tf_warning_or_error, NULL_TREE,
14296 /*integral_constant_expression_p=*/false);
14297 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
14298 /*asmspec_tree=*/NULL_TREE,
14299 LOOKUP_ONLYCONVERTING);
14300 pop_nested_class ();
14301 pop_nested_namespace (ns);
14304 /* We restore the source position here because it's used by
14305 add_pending_template. */
14306 input_location = saved_loc;
14308 if (at_eof && !pattern_defined
14309 && DECL_EXPLICIT_INSTANTIATION (d))
14310 /* [temp.explicit]
14312 The definition of a non-exported function template, a
14313 non-exported member function template, or a non-exported
14314 member function or static data member of a class template
14315 shall be present in every translation unit in which it is
14316 explicitly instantiated. */
14317 pedwarn
14318 ("explicit instantiation of %qD but no definition available", d);
14320 /* ??? Historically, we have instantiated inline functions, even
14321 when marked as "extern template". */
14322 if (!(external_p && TREE_CODE (d) == VAR_DECL))
14323 add_pending_template (d);
14324 goto out;
14326 /* Tell the repository that D is available in this translation unit
14327 -- and see if it is supposed to be instantiated here. */
14328 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
14330 /* In a PCH file, despite the fact that the repository hasn't
14331 requested instantiation in the PCH it is still possible that
14332 an instantiation will be required in a file that includes the
14333 PCH. */
14334 if (pch_file)
14335 add_pending_template (d);
14336 /* Instantiate inline functions so that the inliner can do its
14337 job, even though we'll not be emitting a copy of this
14338 function. */
14339 if (!(TREE_CODE (d) == FUNCTION_DECL
14340 && flag_inline_trees
14341 && DECL_DECLARED_INLINE_P (d)))
14342 goto out;
14345 need_push = !cfun || !global_bindings_p ();
14346 if (need_push)
14347 push_to_top_level ();
14349 /* Mark D as instantiated so that recursive calls to
14350 instantiate_decl do not try to instantiate it again. */
14351 DECL_TEMPLATE_INSTANTIATED (d) = 1;
14353 /* Regenerate the declaration in case the template has been modified
14354 by a subsequent redeclaration. */
14355 regenerate_decl_from_template (d, td);
14357 /* We already set the file and line above. Reset them now in case
14358 they changed as a result of calling regenerate_decl_from_template. */
14359 input_location = DECL_SOURCE_LOCATION (d);
14361 if (TREE_CODE (d) == VAR_DECL)
14363 tree init;
14365 /* Clear out DECL_RTL; whatever was there before may not be right
14366 since we've reset the type of the declaration. */
14367 SET_DECL_RTL (d, NULL_RTX);
14368 DECL_IN_AGGR_P (d) = 0;
14370 /* The initializer is placed in DECL_INITIAL by
14371 regenerate_decl_from_template. Pull it out so that
14372 finish_decl can process it. */
14373 init = DECL_INITIAL (d);
14374 DECL_INITIAL (d) = NULL_TREE;
14375 DECL_INITIALIZED_P (d) = 0;
14377 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14378 initializer. That function will defer actual emission until
14379 we have a chance to determine linkage. */
14380 DECL_EXTERNAL (d) = 0;
14382 /* Enter the scope of D so that access-checking works correctly. */
14383 push_nested_class (DECL_CONTEXT (d));
14384 finish_decl (d, init, NULL_TREE);
14385 pop_nested_class ();
14387 else if (TREE_CODE (d) == FUNCTION_DECL)
14389 htab_t saved_local_specializations;
14390 tree subst_decl;
14391 tree tmpl_parm;
14392 tree spec_parm;
14394 /* Save away the current list, in case we are instantiating one
14395 template from within the body of another. */
14396 saved_local_specializations = local_specializations;
14398 /* Set up the list of local specializations. */
14399 local_specializations = htab_create (37,
14400 hash_local_specialization,
14401 eq_local_specializations,
14402 NULL);
14404 /* Set up context. */
14405 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
14407 /* Create substitution entries for the parameters. */
14408 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
14409 tmpl_parm = DECL_ARGUMENTS (subst_decl);
14410 spec_parm = DECL_ARGUMENTS (d);
14411 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
14413 register_local_specialization (spec_parm, tmpl_parm);
14414 spec_parm = skip_artificial_parms_for (d, spec_parm);
14415 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
14417 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14419 register_local_specialization (spec_parm, tmpl_parm);
14420 tmpl_parm = TREE_CHAIN (tmpl_parm);
14421 spec_parm = TREE_CHAIN (spec_parm);
14423 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14425 /* Collect all of the extra "packed" parameters into an
14426 argument pack. */
14427 tree parmvec;
14428 tree parmtypevec;
14429 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
14430 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
14431 int i, len = 0;
14432 tree t;
14434 /* Count how many parameters remain. */
14435 for (t = spec_parm; t; t = TREE_CHAIN (t))
14436 len++;
14438 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
14439 parmvec = make_tree_vec (len);
14440 parmtypevec = make_tree_vec (len);
14441 for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
14443 TREE_VEC_ELT (parmvec, i) = spec_parm;
14444 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
14447 /* Build the argument packs. */
14448 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
14449 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
14450 TREE_TYPE (argpack) = argtypepack;
14452 /* Register the (value) argument pack as a specialization of
14453 TMPL_PARM, then move on. */
14454 register_local_specialization (argpack, tmpl_parm);
14455 tmpl_parm = TREE_CHAIN (tmpl_parm);
14457 gcc_assert (!spec_parm);
14459 /* Substitute into the body of the function. */
14460 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
14461 tf_warning_or_error, tmpl,
14462 /*integral_constant_expression_p=*/false);
14464 /* Set the current input_location to the end of the function
14465 so that finish_function knows where we are. */
14466 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
14468 /* We don't need the local specializations any more. */
14469 htab_delete (local_specializations);
14470 local_specializations = saved_local_specializations;
14472 /* Finish the function. */
14473 d = finish_function (0);
14474 expand_or_defer_fn (d);
14477 /* We're not deferring instantiation any more. */
14478 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
14480 if (need_push)
14481 pop_from_top_level ();
14483 out:
14484 input_location = saved_loc;
14485 in_system_header = saved_in_system_header;
14486 pop_deferring_access_checks ();
14487 pop_tinst_level ();
14489 timevar_pop (TV_PARSE);
14491 return d;
14494 /* Run through the list of templates that we wish we could
14495 instantiate, and instantiate any we can. RETRIES is the
14496 number of times we retry pending template instantiation. */
14498 void
14499 instantiate_pending_templates (int retries)
14501 int reconsider;
14502 location_t saved_loc = input_location;
14503 int saved_in_system_header = in_system_header;
14505 /* Instantiating templates may trigger vtable generation. This in turn
14506 may require further template instantiations. We place a limit here
14507 to avoid infinite loop. */
14508 if (pending_templates && retries >= max_tinst_depth)
14510 tree decl = pending_templates->tinst->decl;
14512 error ("template instantiation depth exceeds maximum of %d"
14513 " instantiating %q+D, possibly from virtual table generation"
14514 " (use -ftemplate-depth-NN to increase the maximum)",
14515 max_tinst_depth, decl);
14516 if (TREE_CODE (decl) == FUNCTION_DECL)
14517 /* Pretend that we defined it. */
14518 DECL_INITIAL (decl) = error_mark_node;
14519 return;
14524 struct pending_template **t = &pending_templates;
14525 struct pending_template *last = NULL;
14526 reconsider = 0;
14527 while (*t)
14529 tree instantiation = reopen_tinst_level ((*t)->tinst);
14530 bool complete = false;
14532 if (TYPE_P (instantiation))
14534 tree fn;
14536 if (!COMPLETE_TYPE_P (instantiation))
14538 instantiate_class_template (instantiation);
14539 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
14540 for (fn = TYPE_METHODS (instantiation);
14542 fn = TREE_CHAIN (fn))
14543 if (! DECL_ARTIFICIAL (fn))
14544 instantiate_decl (fn,
14545 /*defer_ok=*/0,
14546 /*expl_inst_class_mem_p=*/false);
14547 if (COMPLETE_TYPE_P (instantiation))
14548 reconsider = 1;
14551 complete = COMPLETE_TYPE_P (instantiation);
14553 else
14555 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
14556 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
14558 instantiation
14559 = instantiate_decl (instantiation,
14560 /*defer_ok=*/0,
14561 /*expl_inst_class_mem_p=*/false);
14562 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
14563 reconsider = 1;
14566 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
14567 || DECL_TEMPLATE_INSTANTIATED (instantiation));
14570 if (complete)
14571 /* If INSTANTIATION has been instantiated, then we don't
14572 need to consider it again in the future. */
14573 *t = (*t)->next;
14574 else
14576 last = *t;
14577 t = &(*t)->next;
14579 tinst_depth = 0;
14580 current_tinst_level = NULL;
14582 last_pending_template = last;
14584 while (reconsider);
14586 input_location = saved_loc;
14587 in_system_header = saved_in_system_header;
14590 /* Substitute ARGVEC into T, which is a list of initializers for
14591 either base class or a non-static data member. The TREE_PURPOSEs
14592 are DECLs, and the TREE_VALUEs are the initializer values. Used by
14593 instantiate_decl. */
14595 static tree
14596 tsubst_initializer_list (tree t, tree argvec)
14598 tree inits = NULL_TREE;
14600 for (; t; t = TREE_CHAIN (t))
14602 tree decl;
14603 tree init;
14604 tree expanded_bases = NULL_TREE;
14605 tree expanded_arguments = NULL_TREE;
14606 int i, len = 1;
14608 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
14610 tree expr;
14611 tree arg;
14613 /* Expand the base class expansion type into separate base
14614 classes. */
14615 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
14616 tf_warning_or_error,
14617 NULL_TREE);
14618 if (expanded_bases == error_mark_node)
14619 continue;
14621 /* We'll be building separate TREE_LISTs of arguments for
14622 each base. */
14623 len = TREE_VEC_LENGTH (expanded_bases);
14624 expanded_arguments = make_tree_vec (len);
14625 for (i = 0; i < len; i++)
14626 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
14628 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
14629 expand each argument in the TREE_VALUE of t. */
14630 expr = make_node (EXPR_PACK_EXPANSION);
14631 PACK_EXPANSION_PARAMETER_PACKS (expr) =
14632 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
14634 /* Substitute parameter packs into each argument in the
14635 TREE_LIST. */
14636 in_base_initializer = 1;
14637 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
14639 tree expanded_exprs;
14641 /* Expand the argument. */
14642 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
14643 expanded_exprs = tsubst_pack_expansion (expr, argvec,
14644 tf_warning_or_error,
14645 NULL_TREE);
14647 /* Prepend each of the expanded expressions to the
14648 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
14649 for (i = 0; i < len; i++)
14651 TREE_VEC_ELT (expanded_arguments, i) =
14652 tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i),
14653 TREE_VEC_ELT (expanded_arguments, i));
14656 in_base_initializer = 0;
14658 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
14659 since we built them backwards. */
14660 for (i = 0; i < len; i++)
14662 TREE_VEC_ELT (expanded_arguments, i) =
14663 nreverse (TREE_VEC_ELT (expanded_arguments, i));
14667 for (i = 0; i < len; ++i)
14669 if (expanded_bases)
14671 decl = TREE_VEC_ELT (expanded_bases, i);
14672 decl = expand_member_init (decl);
14673 init = TREE_VEC_ELT (expanded_arguments, i);
14675 else
14677 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
14678 tf_warning_or_error, NULL_TREE);
14680 decl = expand_member_init (decl);
14681 if (decl && !DECL_P (decl))
14682 in_base_initializer = 1;
14684 init = tsubst_expr (TREE_VALUE (t), argvec,
14685 tf_warning_or_error, NULL_TREE,
14686 /*integral_constant_expression_p=*/false);
14687 in_base_initializer = 0;
14690 if (decl)
14692 init = build_tree_list (decl, init);
14693 TREE_CHAIN (init) = inits;
14694 inits = init;
14698 return inits;
14701 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
14703 static void
14704 set_current_access_from_decl (tree decl)
14706 if (TREE_PRIVATE (decl))
14707 current_access_specifier = access_private_node;
14708 else if (TREE_PROTECTED (decl))
14709 current_access_specifier = access_protected_node;
14710 else
14711 current_access_specifier = access_public_node;
14714 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
14715 is the instantiation (which should have been created with
14716 start_enum) and ARGS are the template arguments to use. */
14718 static void
14719 tsubst_enum (tree tag, tree newtag, tree args)
14721 tree e;
14723 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
14725 tree value;
14726 tree decl;
14728 decl = TREE_VALUE (e);
14729 /* Note that in a template enum, the TREE_VALUE is the
14730 CONST_DECL, not the corresponding INTEGER_CST. */
14731 value = tsubst_expr (DECL_INITIAL (decl),
14732 args, tf_warning_or_error, NULL_TREE,
14733 /*integral_constant_expression_p=*/true);
14735 /* Give this enumeration constant the correct access. */
14736 set_current_access_from_decl (decl);
14738 /* Actually build the enumerator itself. */
14739 build_enumerator (DECL_NAME (decl), value, newtag);
14742 finish_enum (newtag);
14743 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
14744 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
14747 /* DECL is a FUNCTION_DECL that is a template specialization. Return
14748 its type -- but without substituting the innermost set of template
14749 arguments. So, innermost set of template parameters will appear in
14750 the type. */
14752 tree
14753 get_mostly_instantiated_function_type (tree decl)
14755 tree fn_type;
14756 tree tmpl;
14757 tree targs;
14758 tree tparms;
14759 int parm_depth;
14761 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14762 targs = DECL_TI_ARGS (decl);
14763 tparms = DECL_TEMPLATE_PARMS (tmpl);
14764 parm_depth = TMPL_PARMS_DEPTH (tparms);
14766 /* There should be as many levels of arguments as there are levels
14767 of parameters. */
14768 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
14770 fn_type = TREE_TYPE (tmpl);
14772 if (parm_depth == 1)
14773 /* No substitution is necessary. */
14775 else
14777 int i, save_access_control;
14778 tree partial_args;
14780 /* Replace the innermost level of the TARGS with NULL_TREEs to
14781 let tsubst know not to substitute for those parameters. */
14782 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
14783 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
14784 SET_TMPL_ARGS_LEVEL (partial_args, i,
14785 TMPL_ARGS_LEVEL (targs, i));
14786 SET_TMPL_ARGS_LEVEL (partial_args,
14787 TMPL_ARGS_DEPTH (targs),
14788 make_tree_vec (DECL_NTPARMS (tmpl)));
14790 /* Disable access control as this function is used only during
14791 name-mangling. */
14792 save_access_control = flag_access_control;
14793 flag_access_control = 0;
14795 ++processing_template_decl;
14796 /* Now, do the (partial) substitution to figure out the
14797 appropriate function type. */
14798 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
14799 --processing_template_decl;
14801 /* Substitute into the template parameters to obtain the real
14802 innermost set of parameters. This step is important if the
14803 innermost set of template parameters contains value
14804 parameters whose types depend on outer template parameters. */
14805 TREE_VEC_LENGTH (partial_args)--;
14806 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
14808 flag_access_control = save_access_control;
14811 return fn_type;
14814 /* Return truthvalue if we're processing a template different from
14815 the last one involved in diagnostics. */
14817 problematic_instantiation_changed (void)
14819 return last_template_error_tick != tinst_level_tick;
14822 /* Remember current template involved in diagnostics. */
14823 void
14824 record_last_problematic_instantiation (void)
14826 last_template_error_tick = tinst_level_tick;
14829 struct tinst_level *
14830 current_instantiation (void)
14832 return current_tinst_level;
14835 /* [temp.param] Check that template non-type parm TYPE is of an allowable
14836 type. Return zero for ok, nonzero for disallowed. Issue error and
14837 warning messages under control of COMPLAIN. */
14839 static int
14840 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
14842 if (INTEGRAL_TYPE_P (type))
14843 return 0;
14844 else if (POINTER_TYPE_P (type))
14845 return 0;
14846 else if (TYPE_PTR_TO_MEMBER_P (type))
14847 return 0;
14848 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14849 return 0;
14850 else if (TREE_CODE (type) == TYPENAME_TYPE)
14851 return 0;
14853 if (complain & tf_error)
14854 error ("%q#T is not a valid type for a template constant parameter", type);
14855 return 1;
14858 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
14859 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
14861 static bool
14862 dependent_type_p_r (tree type)
14864 tree scope;
14866 /* [temp.dep.type]
14868 A type is dependent if it is:
14870 -- a template parameter. Template template parameters are types
14871 for us (since TYPE_P holds true for them) so we handle
14872 them here. */
14873 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
14874 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
14875 return true;
14876 /* -- a qualified-id with a nested-name-specifier which contains a
14877 class-name that names a dependent type or whose unqualified-id
14878 names a dependent type. */
14879 if (TREE_CODE (type) == TYPENAME_TYPE)
14880 return true;
14881 /* -- a cv-qualified type where the cv-unqualified type is
14882 dependent. */
14883 type = TYPE_MAIN_VARIANT (type);
14884 /* -- a compound type constructed from any dependent type. */
14885 if (TYPE_PTR_TO_MEMBER_P (type))
14886 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
14887 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
14888 (type)));
14889 else if (TREE_CODE (type) == POINTER_TYPE
14890 || TREE_CODE (type) == REFERENCE_TYPE)
14891 return dependent_type_p (TREE_TYPE (type));
14892 else if (TREE_CODE (type) == FUNCTION_TYPE
14893 || TREE_CODE (type) == METHOD_TYPE)
14895 tree arg_type;
14897 if (dependent_type_p (TREE_TYPE (type)))
14898 return true;
14899 for (arg_type = TYPE_ARG_TYPES (type);
14900 arg_type;
14901 arg_type = TREE_CHAIN (arg_type))
14902 if (dependent_type_p (TREE_VALUE (arg_type)))
14903 return true;
14904 return false;
14906 /* -- an array type constructed from any dependent type or whose
14907 size is specified by a constant expression that is
14908 value-dependent. */
14909 if (TREE_CODE (type) == ARRAY_TYPE)
14911 if (TYPE_DOMAIN (type)
14912 && ((value_dependent_expression_p
14913 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14914 || (type_dependent_expression_p
14915 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
14916 return true;
14917 return dependent_type_p (TREE_TYPE (type));
14920 /* -- a template-id in which either the template name is a template
14921 parameter ... */
14922 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
14923 return true;
14924 /* ... or any of the template arguments is a dependent type or
14925 an expression that is type-dependent or value-dependent. */
14926 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
14927 && (any_dependent_template_arguments_p
14928 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
14929 return true;
14931 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
14932 argument of the `typeof' expression is not type-dependent, then
14933 it should already been have resolved. */
14934 if (TREE_CODE (type) == TYPEOF_TYPE
14935 || TREE_CODE (type) == DECLTYPE_TYPE)
14936 return true;
14938 /* A template argument pack is dependent if any of its packed
14939 arguments are. */
14940 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
14942 tree args = ARGUMENT_PACK_ARGS (type);
14943 int i, len = TREE_VEC_LENGTH (args);
14944 for (i = 0; i < len; ++i)
14945 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
14946 return true;
14949 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
14950 be template parameters. */
14951 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
14952 return true;
14954 /* The standard does not specifically mention types that are local
14955 to template functions or local classes, but they should be
14956 considered dependent too. For example:
14958 template <int I> void f() {
14959 enum E { a = I };
14960 S<sizeof (E)> s;
14963 The size of `E' cannot be known until the value of `I' has been
14964 determined. Therefore, `E' must be considered dependent. */
14965 scope = TYPE_CONTEXT (type);
14966 if (scope && TYPE_P (scope))
14967 return dependent_type_p (scope);
14968 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14969 return type_dependent_expression_p (scope);
14971 /* Other types are non-dependent. */
14972 return false;
14975 /* Returns TRUE if TYPE is dependent, in the sense of
14976 [temp.dep.type]. */
14978 bool
14979 dependent_type_p (tree type)
14981 /* If there are no template parameters in scope, then there can't be
14982 any dependent types. */
14983 if (!processing_template_decl)
14985 /* If we are not processing a template, then nobody should be
14986 providing us with a dependent type. */
14987 gcc_assert (type);
14988 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
14989 return false;
14992 /* If the type is NULL, we have not computed a type for the entity
14993 in question; in that case, the type is dependent. */
14994 if (!type)
14995 return true;
14997 /* Erroneous types can be considered non-dependent. */
14998 if (type == error_mark_node)
14999 return false;
15001 /* If we have not already computed the appropriate value for TYPE,
15002 do so now. */
15003 if (!TYPE_DEPENDENT_P_VALID (type))
15005 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
15006 TYPE_DEPENDENT_P_VALID (type) = 1;
15009 return TYPE_DEPENDENT_P (type);
15012 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
15014 static bool
15015 dependent_scope_ref_p (tree expression, bool criterion (tree))
15017 tree scope;
15018 tree name;
15020 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
15022 if (!TYPE_P (TREE_OPERAND (expression, 0)))
15023 return true;
15025 scope = TREE_OPERAND (expression, 0);
15026 name = TREE_OPERAND (expression, 1);
15028 /* [temp.dep.expr]
15030 An id-expression is type-dependent if it contains a
15031 nested-name-specifier that contains a class-name that names a
15032 dependent type. */
15033 /* The suggested resolution to Core Issue 2 implies that if the
15034 qualifying type is the current class, then we must peek
15035 inside it. */
15036 if (DECL_P (name)
15037 && currently_open_class (scope)
15038 && !criterion (name))
15039 return false;
15040 if (dependent_type_p (scope))
15041 return true;
15043 return false;
15046 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
15047 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
15048 expression. */
15050 bool
15051 value_dependent_expression_p (tree expression)
15053 if (!processing_template_decl)
15054 return false;
15056 /* A name declared with a dependent type. */
15057 if (DECL_P (expression) && type_dependent_expression_p (expression))
15058 return true;
15060 switch (TREE_CODE (expression))
15062 case IDENTIFIER_NODE:
15063 /* A name that has not been looked up -- must be dependent. */
15064 return true;
15066 case TEMPLATE_PARM_INDEX:
15067 /* A non-type template parm. */
15068 return true;
15070 case CONST_DECL:
15071 /* A non-type template parm. */
15072 if (DECL_TEMPLATE_PARM_P (expression))
15073 return true;
15074 return false;
15076 case VAR_DECL:
15077 /* A constant with integral or enumeration type and is initialized
15078 with an expression that is value-dependent. */
15079 if (DECL_INITIAL (expression)
15080 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
15081 && value_dependent_expression_p (DECL_INITIAL (expression)))
15082 return true;
15083 return false;
15085 case DYNAMIC_CAST_EXPR:
15086 case STATIC_CAST_EXPR:
15087 case CONST_CAST_EXPR:
15088 case REINTERPRET_CAST_EXPR:
15089 case CAST_EXPR:
15090 /* These expressions are value-dependent if the type to which
15091 the cast occurs is dependent or the expression being casted
15092 is value-dependent. */
15094 tree type = TREE_TYPE (expression);
15096 if (dependent_type_p (type))
15097 return true;
15099 /* A functional cast has a list of operands. */
15100 expression = TREE_OPERAND (expression, 0);
15101 if (!expression)
15103 /* If there are no operands, it must be an expression such
15104 as "int()". This should not happen for aggregate types
15105 because it would form non-constant expressions. */
15106 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
15108 return false;
15111 if (TREE_CODE (expression) == TREE_LIST)
15112 return any_value_dependent_elements_p (expression);
15114 return value_dependent_expression_p (expression);
15117 case SIZEOF_EXPR:
15118 case ALIGNOF_EXPR:
15119 /* A `sizeof' expression is value-dependent if the operand is
15120 type-dependent or is a pack expansion. */
15121 expression = TREE_OPERAND (expression, 0);
15122 if (PACK_EXPANSION_P (expression))
15123 return true;
15124 else if (TYPE_P (expression))
15125 return dependent_type_p (expression);
15126 return type_dependent_expression_p (expression);
15128 case SCOPE_REF:
15129 return dependent_scope_ref_p (expression, value_dependent_expression_p);
15131 case COMPONENT_REF:
15132 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
15133 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
15135 case CALL_EXPR:
15136 /* A CALL_EXPR may appear in a constant expression if it is a
15137 call to a builtin function, e.g., __builtin_constant_p. All
15138 such calls are value-dependent. */
15139 return true;
15141 case NONTYPE_ARGUMENT_PACK:
15142 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
15143 is value-dependent. */
15145 tree values = ARGUMENT_PACK_ARGS (expression);
15146 int i, len = TREE_VEC_LENGTH (values);
15148 for (i = 0; i < len; ++i)
15149 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
15150 return true;
15152 return false;
15155 case TRAIT_EXPR:
15157 tree type2 = TRAIT_EXPR_TYPE2 (expression);
15158 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
15159 || (type2 ? dependent_type_p (type2) : false));
15162 case MODOP_EXPR:
15163 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
15164 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
15166 default:
15167 /* A constant expression is value-dependent if any subexpression is
15168 value-dependent. */
15169 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
15171 case tcc_reference:
15172 case tcc_unary:
15173 return (value_dependent_expression_p
15174 (TREE_OPERAND (expression, 0)));
15176 case tcc_comparison:
15177 case tcc_binary:
15178 return ((value_dependent_expression_p
15179 (TREE_OPERAND (expression, 0)))
15180 || (value_dependent_expression_p
15181 (TREE_OPERAND (expression, 1))));
15183 case tcc_expression:
15184 case tcc_vl_exp:
15186 int i;
15187 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
15188 /* In some cases, some of the operands may be missing.
15189 (For example, in the case of PREDECREMENT_EXPR, the
15190 amount to increment by may be missing.) That doesn't
15191 make the expression dependent. */
15192 if (TREE_OPERAND (expression, i)
15193 && (value_dependent_expression_p
15194 (TREE_OPERAND (expression, i))))
15195 return true;
15196 return false;
15199 default:
15200 break;
15204 /* The expression is not value-dependent. */
15205 return false;
15208 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
15209 [temp.dep.expr]. */
15211 bool
15212 type_dependent_expression_p (tree expression)
15214 if (!processing_template_decl)
15215 return false;
15217 if (expression == error_mark_node)
15218 return false;
15220 /* An unresolved name is always dependent. */
15221 if (TREE_CODE (expression) == IDENTIFIER_NODE
15222 || TREE_CODE (expression) == USING_DECL)
15223 return true;
15225 /* Some expression forms are never type-dependent. */
15226 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
15227 || TREE_CODE (expression) == SIZEOF_EXPR
15228 || TREE_CODE (expression) == ALIGNOF_EXPR
15229 || TREE_CODE (expression) == TRAIT_EXPR
15230 || TREE_CODE (expression) == TYPEID_EXPR
15231 || TREE_CODE (expression) == DELETE_EXPR
15232 || TREE_CODE (expression) == VEC_DELETE_EXPR
15233 || TREE_CODE (expression) == THROW_EXPR)
15234 return false;
15236 /* The types of these expressions depends only on the type to which
15237 the cast occurs. */
15238 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
15239 || TREE_CODE (expression) == STATIC_CAST_EXPR
15240 || TREE_CODE (expression) == CONST_CAST_EXPR
15241 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
15242 || TREE_CODE (expression) == CAST_EXPR)
15243 return dependent_type_p (TREE_TYPE (expression));
15245 /* The types of these expressions depends only on the type created
15246 by the expression. */
15247 if (TREE_CODE (expression) == NEW_EXPR
15248 || TREE_CODE (expression) == VEC_NEW_EXPR)
15250 /* For NEW_EXPR tree nodes created inside a template, either
15251 the object type itself or a TREE_LIST may appear as the
15252 operand 1. */
15253 tree type = TREE_OPERAND (expression, 1);
15254 if (TREE_CODE (type) == TREE_LIST)
15255 /* This is an array type. We need to check array dimensions
15256 as well. */
15257 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
15258 || value_dependent_expression_p
15259 (TREE_OPERAND (TREE_VALUE (type), 1));
15260 else
15261 return dependent_type_p (type);
15264 if (TREE_CODE (expression) == SCOPE_REF
15265 && dependent_scope_ref_p (expression,
15266 type_dependent_expression_p))
15267 return true;
15269 if (TREE_CODE (expression) == FUNCTION_DECL
15270 && DECL_LANG_SPECIFIC (expression)
15271 && DECL_TEMPLATE_INFO (expression)
15272 && (any_dependent_template_arguments_p
15273 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
15274 return true;
15276 if (TREE_CODE (expression) == TEMPLATE_DECL
15277 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
15278 return false;
15280 if (TREE_TYPE (expression) == unknown_type_node)
15282 if (TREE_CODE (expression) == ADDR_EXPR)
15283 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
15284 if (TREE_CODE (expression) == COMPONENT_REF
15285 || TREE_CODE (expression) == OFFSET_REF)
15287 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
15288 return true;
15289 expression = TREE_OPERAND (expression, 1);
15290 if (TREE_CODE (expression) == IDENTIFIER_NODE)
15291 return false;
15293 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15294 if (TREE_CODE (expression) == SCOPE_REF)
15295 return false;
15297 if (TREE_CODE (expression) == BASELINK)
15298 expression = BASELINK_FUNCTIONS (expression);
15300 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
15302 if (any_dependent_template_arguments_p
15303 (TREE_OPERAND (expression, 1)))
15304 return true;
15305 expression = TREE_OPERAND (expression, 0);
15307 gcc_assert (TREE_CODE (expression) == OVERLOAD
15308 || TREE_CODE (expression) == FUNCTION_DECL);
15310 while (expression)
15312 if (type_dependent_expression_p (OVL_CURRENT (expression)))
15313 return true;
15314 expression = OVL_NEXT (expression);
15316 return false;
15319 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
15321 return (dependent_type_p (TREE_TYPE (expression)));
15324 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15325 contains a type-dependent expression. */
15327 bool
15328 any_type_dependent_arguments_p (const_tree args)
15330 while (args)
15332 tree arg = TREE_VALUE (args);
15334 if (type_dependent_expression_p (arg))
15335 return true;
15336 args = TREE_CHAIN (args);
15338 return false;
15341 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
15342 expressions) contains any value-dependent expressions. */
15344 bool
15345 any_value_dependent_elements_p (const_tree list)
15347 for (; list; list = TREE_CHAIN (list))
15348 if (value_dependent_expression_p (TREE_VALUE (list)))
15349 return true;
15351 return false;
15354 /* Returns TRUE if the ARG (a template argument) is dependent. */
15356 bool
15357 dependent_template_arg_p (tree arg)
15359 if (!processing_template_decl)
15360 return false;
15362 if (TREE_CODE (arg) == TEMPLATE_DECL
15363 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15364 return dependent_template_p (arg);
15365 else if (ARGUMENT_PACK_P (arg))
15367 tree args = ARGUMENT_PACK_ARGS (arg);
15368 int i, len = TREE_VEC_LENGTH (args);
15369 for (i = 0; i < len; ++i)
15371 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15372 return true;
15375 return false;
15377 else if (TYPE_P (arg))
15378 return dependent_type_p (arg);
15379 else
15380 return (type_dependent_expression_p (arg)
15381 || value_dependent_expression_p (arg));
15384 /* Returns true if ARGS (a collection of template arguments) contains
15385 any types that require structural equality testing. */
15387 bool
15388 any_template_arguments_need_structural_equality_p (tree args)
15390 int i;
15391 int j;
15393 if (!args)
15394 return false;
15395 if (args == error_mark_node)
15396 return true;
15398 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15400 tree level = TMPL_ARGS_LEVEL (args, i + 1);
15401 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15403 tree arg = TREE_VEC_ELT (level, j);
15404 tree packed_args = NULL_TREE;
15405 int k, len = 1;
15407 if (ARGUMENT_PACK_P (arg))
15409 /* Look inside the argument pack. */
15410 packed_args = ARGUMENT_PACK_ARGS (arg);
15411 len = TREE_VEC_LENGTH (packed_args);
15414 for (k = 0; k < len; ++k)
15416 if (packed_args)
15417 arg = TREE_VEC_ELT (packed_args, k);
15419 if (error_operand_p (arg))
15420 return true;
15421 else if (TREE_CODE (arg) == TEMPLATE_DECL
15422 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15423 continue;
15424 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
15425 return true;
15426 else if (!TYPE_P (arg) && TREE_TYPE (arg)
15427 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
15428 return true;
15433 return false;
15436 /* Returns true if ARGS (a collection of template arguments) contains
15437 any dependent arguments. */
15439 bool
15440 any_dependent_template_arguments_p (const_tree args)
15442 int i;
15443 int j;
15445 if (!args)
15446 return false;
15447 if (args == error_mark_node)
15448 return true;
15450 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15452 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
15453 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15454 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
15455 return true;
15458 return false;
15461 /* Returns TRUE if the template TMPL is dependent. */
15463 bool
15464 dependent_template_p (tree tmpl)
15466 if (TREE_CODE (tmpl) == OVERLOAD)
15468 while (tmpl)
15470 if (dependent_template_p (OVL_FUNCTION (tmpl)))
15471 return true;
15472 tmpl = OVL_CHAIN (tmpl);
15474 return false;
15477 /* Template template parameters are dependent. */
15478 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
15479 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15480 return true;
15481 /* So are names that have not been looked up. */
15482 if (TREE_CODE (tmpl) == SCOPE_REF
15483 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
15484 return true;
15485 /* So are member templates of dependent classes. */
15486 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
15487 return dependent_type_p (DECL_CONTEXT (tmpl));
15488 return false;
15491 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
15493 bool
15494 dependent_template_id_p (tree tmpl, tree args)
15496 return (dependent_template_p (tmpl)
15497 || any_dependent_template_arguments_p (args));
15500 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
15501 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
15502 no such TYPE can be found. Note that this function peers inside
15503 uninstantiated templates and therefore should be used only in
15504 extremely limited situations. ONLY_CURRENT_P restricts this
15505 peering to the currently open classes hierarchy (which is required
15506 when comparing types). */
15508 tree
15509 resolve_typename_type (tree type, bool only_current_p)
15511 tree scope;
15512 tree name;
15513 tree decl;
15514 int quals;
15515 tree pushed_scope;
15516 tree result;
15518 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
15520 scope = TYPE_CONTEXT (type);
15521 name = TYPE_IDENTIFIER (type);
15523 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
15524 it first before we can figure out what NAME refers to. */
15525 if (TREE_CODE (scope) == TYPENAME_TYPE)
15526 scope = resolve_typename_type (scope, only_current_p);
15527 /* If we don't know what SCOPE refers to, then we cannot resolve the
15528 TYPENAME_TYPE. */
15529 if (TREE_CODE (scope) == TYPENAME_TYPE)
15530 return type;
15531 /* If the SCOPE is a template type parameter, we have no way of
15532 resolving the name. */
15533 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
15534 return type;
15535 /* If the SCOPE is not the current instantiation, there's no reason
15536 to look inside it. */
15537 if (only_current_p && !currently_open_class (scope))
15538 return type;
15539 /* If SCOPE is a partial instantiation, it will not have a valid
15540 TYPE_FIELDS list, so use the original template. */
15541 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
15542 /* Enter the SCOPE so that name lookup will be resolved as if we
15543 were in the class definition. In particular, SCOPE will no
15544 longer be considered a dependent type. */
15545 pushed_scope = push_scope (scope);
15546 /* Look up the declaration. */
15547 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
15549 result = NULL_TREE;
15551 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
15552 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
15553 if (!decl)
15554 /*nop*/;
15555 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
15556 && TREE_CODE (decl) == TYPE_DECL)
15558 result = TREE_TYPE (decl);
15559 if (result == error_mark_node)
15560 result = NULL_TREE;
15562 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
15563 && DECL_CLASS_TEMPLATE_P (decl))
15565 tree tmpl;
15566 tree args;
15567 /* Obtain the template and the arguments. */
15568 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
15569 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
15570 /* Instantiate the template. */
15571 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
15572 /*entering_scope=*/0,
15573 tf_error | tf_user);
15574 if (result == error_mark_node)
15575 result = NULL_TREE;
15578 /* Leave the SCOPE. */
15579 if (pushed_scope)
15580 pop_scope (pushed_scope);
15582 /* If we failed to resolve it, return the original typename. */
15583 if (!result)
15584 return type;
15586 /* If lookup found a typename type, resolve that too. */
15587 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
15589 /* Ill-formed programs can cause infinite recursion here, so we
15590 must catch that. */
15591 TYPENAME_IS_RESOLVING_P (type) = 1;
15592 result = resolve_typename_type (result, only_current_p);
15593 TYPENAME_IS_RESOLVING_P (type) = 0;
15596 /* Qualify the resulting type. */
15597 quals = cp_type_quals (type);
15598 if (quals)
15599 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
15601 return result;
15604 /* EXPR is an expression which is not type-dependent. Return a proxy
15605 for EXPR that can be used to compute the types of larger
15606 expressions containing EXPR. */
15608 tree
15609 build_non_dependent_expr (tree expr)
15611 tree inner_expr;
15613 /* Preserve null pointer constants so that the type of things like
15614 "p == 0" where "p" is a pointer can be determined. */
15615 if (null_ptr_cst_p (expr))
15616 return expr;
15617 /* Preserve OVERLOADs; the functions must be available to resolve
15618 types. */
15619 inner_expr = expr;
15620 if (TREE_CODE (inner_expr) == ADDR_EXPR)
15621 inner_expr = TREE_OPERAND (inner_expr, 0);
15622 if (TREE_CODE (inner_expr) == COMPONENT_REF)
15623 inner_expr = TREE_OPERAND (inner_expr, 1);
15624 if (is_overloaded_fn (inner_expr)
15625 || TREE_CODE (inner_expr) == OFFSET_REF)
15626 return expr;
15627 /* There is no need to return a proxy for a variable. */
15628 if (TREE_CODE (expr) == VAR_DECL)
15629 return expr;
15630 /* Preserve string constants; conversions from string constants to
15631 "char *" are allowed, even though normally a "const char *"
15632 cannot be used to initialize a "char *". */
15633 if (TREE_CODE (expr) == STRING_CST)
15634 return expr;
15635 /* Preserve arithmetic constants, as an optimization -- there is no
15636 reason to create a new node. */
15637 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
15638 return expr;
15639 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
15640 There is at least one place where we want to know that a
15641 particular expression is a throw-expression: when checking a ?:
15642 expression, there are special rules if the second or third
15643 argument is a throw-expression. */
15644 if (TREE_CODE (expr) == THROW_EXPR)
15645 return expr;
15647 if (TREE_CODE (expr) == COND_EXPR)
15648 return build3 (COND_EXPR,
15649 TREE_TYPE (expr),
15650 TREE_OPERAND (expr, 0),
15651 (TREE_OPERAND (expr, 1)
15652 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
15653 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
15654 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
15655 if (TREE_CODE (expr) == COMPOUND_EXPR
15656 && !COMPOUND_EXPR_OVERLOADED (expr))
15657 return build2 (COMPOUND_EXPR,
15658 TREE_TYPE (expr),
15659 TREE_OPERAND (expr, 0),
15660 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
15662 /* If the type is unknown, it can't really be non-dependent */
15663 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
15665 /* Otherwise, build a NON_DEPENDENT_EXPR.
15667 REFERENCE_TYPEs are not stripped for expressions in templates
15668 because doing so would play havoc with mangling. Consider, for
15669 example:
15671 template <typename T> void f<T& g>() { g(); }
15673 In the body of "f", the expression for "g" will have
15674 REFERENCE_TYPE, even though the standard says that it should
15675 not. The reason is that we must preserve the syntactic form of
15676 the expression so that mangling (say) "f<g>" inside the body of
15677 "f" works out correctly. Therefore, the REFERENCE_TYPE is
15678 stripped here. */
15679 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
15682 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
15683 Return a new TREE_LIST with the various arguments replaced with
15684 equivalent non-dependent expressions. */
15686 tree
15687 build_non_dependent_args (tree args)
15689 tree a;
15690 tree new_args;
15692 new_args = NULL_TREE;
15693 for (a = args; a; a = TREE_CHAIN (a))
15694 new_args = tree_cons (NULL_TREE,
15695 build_non_dependent_expr (TREE_VALUE (a)),
15696 new_args);
15697 return nreverse (new_args);
15700 #include "gt-cp-pt.h"