PR c++/17743
[official-gcc.git] / gcc / cp / pt.c
blobe54bc5fba1ed5ad0d57ed482a29d50cae2768daa
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 /* The inverse of get_innermost_template_args: Return all but the innermost
523 EXTRA_LEVELS levels of template arguments from the ARGS. */
525 static tree
526 strip_innermost_template_args (tree args, int extra_levels)
528 tree new_args;
529 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
530 int i;
532 gcc_assert (n >= 0);
534 /* If N is 1, just return the outermost set of template arguments. */
535 if (n == 1)
536 return TMPL_ARGS_LEVEL (args, 1);
538 /* If we're not removing anything, just return the arguments we were
539 given. */
540 gcc_assert (extra_levels >= 0);
541 if (extra_levels == 0)
542 return args;
544 /* Make a new set of arguments, not containing the inner arguments. */
545 new_args = make_tree_vec (n);
546 for (i = 1; i <= n; ++i)
547 SET_TMPL_ARGS_LEVEL (new_args, i,
548 TMPL_ARGS_LEVEL (args, i));
550 return new_args;
553 /* We've got a template header coming up; push to a new level for storing
554 the parms. */
556 void
557 begin_template_parm_list (void)
559 /* We use a non-tag-transparent scope here, which causes pushtag to
560 put tags in this scope, rather than in the enclosing class or
561 namespace scope. This is the right thing, since we want
562 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
563 global template class, push_template_decl handles putting the
564 TEMPLATE_DECL into top-level scope. For a nested template class,
565 e.g.:
567 template <class T> struct S1 {
568 template <class T> struct S2 {};
571 pushtag contains special code to call pushdecl_with_scope on the
572 TEMPLATE_DECL for S2. */
573 begin_scope (sk_template_parms, NULL);
574 ++processing_template_decl;
575 ++processing_template_parmlist;
576 note_template_header (0);
579 /* This routine is called when a specialization is declared. If it is
580 invalid to declare a specialization here, an error is reported and
581 false is returned, otherwise this routine will return true. */
583 static bool
584 check_specialization_scope (void)
586 tree scope = current_scope ();
588 /* [temp.expl.spec]
590 An explicit specialization shall be declared in the namespace of
591 which the template is a member, or, for member templates, in the
592 namespace of which the enclosing class or enclosing class
593 template is a member. An explicit specialization of a member
594 function, member class or static data member of a class template
595 shall be declared in the namespace of which the class template
596 is a member. */
597 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
599 error ("explicit specialization in non-namespace scope %qD", scope);
600 return false;
603 /* [temp.expl.spec]
605 In an explicit specialization declaration for a member of a class
606 template or a member template that appears in namespace scope,
607 the member template and some of its enclosing class templates may
608 remain unspecialized, except that the declaration shall not
609 explicitly specialize a class member template if its enclosing
610 class templates are not explicitly specialized as well. */
611 if (current_template_parms)
613 error ("enclosing class templates are not explicitly specialized");
614 return false;
617 return true;
620 /* We've just seen template <>. */
622 bool
623 begin_specialization (void)
625 begin_scope (sk_template_spec, NULL);
626 note_template_header (1);
627 return check_specialization_scope ();
630 /* Called at then end of processing a declaration preceded by
631 template<>. */
633 void
634 end_specialization (void)
636 finish_scope ();
637 reset_specialization ();
640 /* Any template <>'s that we have seen thus far are not referring to a
641 function specialization. */
643 void
644 reset_specialization (void)
646 processing_specialization = 0;
647 template_header_count = 0;
650 /* We've just seen a template header. If SPECIALIZATION is nonzero,
651 it was of the form template <>. */
653 static void
654 note_template_header (int specialization)
656 processing_specialization = specialization;
657 template_header_count++;
660 /* We're beginning an explicit instantiation. */
662 void
663 begin_explicit_instantiation (void)
665 gcc_assert (!processing_explicit_instantiation);
666 processing_explicit_instantiation = true;
670 void
671 end_explicit_instantiation (void)
673 gcc_assert (processing_explicit_instantiation);
674 processing_explicit_instantiation = false;
677 /* An explicit specialization or partial specialization TMPL is being
678 declared. Check that the namespace in which the specialization is
679 occurring is permissible. Returns false iff it is invalid to
680 specialize TMPL in the current namespace. */
682 static bool
683 check_specialization_namespace (tree tmpl)
685 tree tpl_ns = decl_namespace_context (tmpl);
687 /* [tmpl.expl.spec]
689 An explicit specialization shall be declared in the namespace of
690 which the template is a member, or, for member templates, in the
691 namespace of which the enclosing class or enclosing class
692 template is a member. An explicit specialization of a member
693 function, member class or static data member of a class template
694 shall be declared in the namespace of which the class template is
695 a member. */
696 if (is_associated_namespace (current_namespace, tpl_ns))
697 /* Same or super-using namespace. */
698 return true;
699 else
701 pedwarn ("specialization of %qD in different namespace", tmpl);
702 pedwarn (" from definition of %q+#D", tmpl);
703 return false;
707 /* SPEC is an explicit instantiation. Check that it is valid to
708 perform this explicit instantiation in the current namespace. */
710 static void
711 check_explicit_instantiation_namespace (tree spec)
713 tree ns;
715 /* DR 275: An explicit instantiation shall appear in an enclosing
716 namespace of its template. */
717 ns = decl_namespace_context (spec);
718 if (!is_ancestor (current_namespace, ns))
719 pedwarn ("explicit instantiation of %qD in namespace %qD "
720 "(which does not enclose namespace %qD)",
721 spec, current_namespace, ns);
724 /* The TYPE is being declared. If it is a template type, that means it
725 is a partial specialization. Do appropriate error-checking. */
727 tree
728 maybe_process_partial_specialization (tree type)
730 tree context;
732 if (type == error_mark_node)
733 return error_mark_node;
735 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
737 error ("name of class shadows template template parameter %qD",
738 TYPE_NAME (type));
739 return error_mark_node;
742 context = TYPE_CONTEXT (type);
744 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
746 /* This is for ordinary explicit specialization and partial
747 specialization of a template class such as:
749 template <> class C<int>;
753 template <class T> class C<T*>;
755 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
757 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
758 && !COMPLETE_TYPE_P (type))
760 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
761 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
762 if (processing_template_decl)
763 push_template_decl (TYPE_MAIN_DECL (type));
765 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
766 error ("specialization of %qT after instantiation", type);
768 else if (CLASS_TYPE_P (type)
769 && !CLASSTYPE_USE_TEMPLATE (type)
770 && CLASSTYPE_TEMPLATE_INFO (type)
771 && context && CLASS_TYPE_P (context)
772 && CLASSTYPE_TEMPLATE_INFO (context))
774 /* This is for an explicit specialization of member class
775 template according to [temp.expl.spec/18]:
777 template <> template <class U> class C<int>::D;
779 The context `C<int>' must be an implicit instantiation.
780 Otherwise this is just a member class template declared
781 earlier like:
783 template <> class C<int> { template <class U> class D; };
784 template <> template <class U> class C<int>::D;
786 In the first case, `C<int>::D' is a specialization of `C<T>::D'
787 while in the second case, `C<int>::D' is a primary template
788 and `C<T>::D' may not exist. */
790 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
791 && !COMPLETE_TYPE_P (type))
793 tree t;
795 if (current_namespace
796 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
798 pedwarn ("specializing %q#T in different namespace", type);
799 pedwarn (" from definition of %q+#D",
800 CLASSTYPE_TI_TEMPLATE (type));
803 /* Check for invalid specialization after instantiation:
805 template <> template <> class C<int>::D<int>;
806 template <> template <class U> class C<int>::D; */
808 for (t = DECL_TEMPLATE_INSTANTIATIONS
809 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
810 t; t = TREE_CHAIN (t))
811 if (TREE_VALUE (t) != type
812 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
813 error ("specialization %qT after instantiation %qT",
814 type, TREE_VALUE (t));
816 /* Mark TYPE as a specialization. And as a result, we only
817 have one level of template argument for the innermost
818 class template. */
819 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
820 CLASSTYPE_TI_ARGS (type)
821 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
824 else if (processing_specialization)
826 error ("explicit specialization of non-template %qT", type);
827 return error_mark_node;
830 return type;
833 /* Returns nonzero if we can optimize the retrieval of specializations
834 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
835 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
837 static inline bool
838 optimize_specialization_lookup_p (tree tmpl)
840 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
841 && DECL_CLASS_SCOPE_P (tmpl)
842 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
843 parameter. */
844 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
845 /* The optimized lookup depends on the fact that the
846 template arguments for the member function template apply
847 purely to the containing class, which is not true if the
848 containing class is an explicit or partial
849 specialization. */
850 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
851 && !DECL_MEMBER_TEMPLATE_P (tmpl)
852 && !DECL_CONV_FN_P (tmpl)
853 /* It is possible to have a template that is not a member
854 template and is not a member of a template class:
856 template <typename T>
857 struct S { friend A::f(); };
859 Here, the friend function is a template, but the context does
860 not have template information. The optimized lookup relies
861 on having ARGS be the template arguments for both the class
862 and the function template. */
863 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
866 /* Retrieve the specialization (in the sense of [temp.spec] - a
867 specialization is either an instantiation or an explicit
868 specialization) of TMPL for the given template ARGS. If there is
869 no such specialization, return NULL_TREE. The ARGS are a vector of
870 arguments, or a vector of vectors of arguments, in the case of
871 templates with more than one level of parameters.
873 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
874 then we search for a partial specialization matching ARGS. This
875 parameter is ignored if TMPL is not a class template. */
877 static tree
878 retrieve_specialization (tree tmpl, tree args,
879 bool class_specializations_p)
881 if (args == error_mark_node)
882 return NULL_TREE;
884 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
886 /* There should be as many levels of arguments as there are
887 levels of parameters. */
888 gcc_assert (TMPL_ARGS_DEPTH (args)
889 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
891 if (optimize_specialization_lookup_p (tmpl))
893 tree class_template;
894 tree class_specialization;
895 VEC(tree,gc) *methods;
896 tree fns;
897 int idx;
899 /* The template arguments actually apply to the containing
900 class. Find the class specialization with those
901 arguments. */
902 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
903 class_specialization
904 = retrieve_specialization (class_template, args,
905 /*class_specializations_p=*/false);
906 if (!class_specialization)
907 return NULL_TREE;
908 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
909 for the specialization. */
910 idx = class_method_index_for_fn (class_specialization, tmpl);
911 if (idx == -1)
912 return NULL_TREE;
913 /* Iterate through the methods with the indicated name, looking
914 for the one that has an instance of TMPL. */
915 methods = CLASSTYPE_METHOD_VEC (class_specialization);
916 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
918 tree fn = OVL_CURRENT (fns);
919 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
920 return fn;
922 return NULL_TREE;
924 else
926 tree *sp;
927 tree *head;
929 /* Class templates store their instantiations on the
930 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
931 DECL_TEMPLATE_SPECIALIZATIONS list. */
932 if (!class_specializations_p
933 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
934 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
935 else
936 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
937 head = sp;
938 /* Iterate through the list until we find a matching template. */
939 while (*sp != NULL_TREE)
941 tree spec = *sp;
943 if (comp_template_args (TREE_PURPOSE (spec), args))
945 /* Use the move-to-front heuristic to speed up future
946 searches. */
947 if (spec != *head)
949 *sp = TREE_CHAIN (*sp);
950 TREE_CHAIN (spec) = *head;
951 *head = spec;
953 return TREE_VALUE (spec);
955 sp = &TREE_CHAIN (spec);
959 return NULL_TREE;
962 /* Like retrieve_specialization, but for local declarations. */
964 static tree
965 retrieve_local_specialization (tree tmpl)
967 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
968 htab_hash_pointer (tmpl));
969 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
972 /* Returns nonzero iff DECL is a specialization of TMPL. */
975 is_specialization_of (tree decl, tree tmpl)
977 tree t;
979 if (TREE_CODE (decl) == FUNCTION_DECL)
981 for (t = decl;
982 t != NULL_TREE;
983 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
984 if (t == tmpl)
985 return 1;
987 else
989 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
991 for (t = TREE_TYPE (decl);
992 t != NULL_TREE;
993 t = CLASSTYPE_USE_TEMPLATE (t)
994 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
995 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
996 return 1;
999 return 0;
1002 /* Returns nonzero iff DECL is a specialization of friend declaration
1003 FRIEND according to [temp.friend]. */
1005 bool
1006 is_specialization_of_friend (tree decl, tree friend)
1008 bool need_template = true;
1009 int template_depth;
1011 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1012 || TREE_CODE (decl) == TYPE_DECL);
1014 /* For [temp.friend/6] when FRIEND is an ordinary member function
1015 of a template class, we want to check if DECL is a specialization
1016 if this. */
1017 if (TREE_CODE (friend) == FUNCTION_DECL
1018 && DECL_TEMPLATE_INFO (friend)
1019 && !DECL_USE_TEMPLATE (friend))
1021 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1022 friend = DECL_TI_TEMPLATE (friend);
1023 need_template = false;
1025 else if (TREE_CODE (friend) == TEMPLATE_DECL
1026 && !PRIMARY_TEMPLATE_P (friend))
1027 need_template = false;
1029 /* There is nothing to do if this is not a template friend. */
1030 if (TREE_CODE (friend) != TEMPLATE_DECL)
1031 return false;
1033 if (is_specialization_of (decl, friend))
1034 return true;
1036 /* [temp.friend/6]
1037 A member of a class template may be declared to be a friend of a
1038 non-template class. In this case, the corresponding member of
1039 every specialization of the class template is a friend of the
1040 class granting friendship.
1042 For example, given a template friend declaration
1044 template <class T> friend void A<T>::f();
1046 the member function below is considered a friend
1048 template <> struct A<int> {
1049 void f();
1052 For this type of template friend, TEMPLATE_DEPTH below will be
1053 nonzero. To determine if DECL is a friend of FRIEND, we first
1054 check if the enclosing class is a specialization of another. */
1056 template_depth = template_class_depth (DECL_CONTEXT (friend));
1057 if (template_depth
1058 && DECL_CLASS_SCOPE_P (decl)
1059 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1060 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1062 /* Next, we check the members themselves. In order to handle
1063 a few tricky cases, such as when FRIEND's are
1065 template <class T> friend void A<T>::g(T t);
1066 template <class T> template <T t> friend void A<T>::h();
1068 and DECL's are
1070 void A<int>::g(int);
1071 template <int> void A<int>::h();
1073 we need to figure out ARGS, the template arguments from
1074 the context of DECL. This is required for template substitution
1075 of `T' in the function parameter of `g' and template parameter
1076 of `h' in the above examples. Here ARGS corresponds to `int'. */
1078 tree context = DECL_CONTEXT (decl);
1079 tree args = NULL_TREE;
1080 int current_depth = 0;
1082 while (current_depth < template_depth)
1084 if (CLASSTYPE_TEMPLATE_INFO (context))
1086 if (current_depth == 0)
1087 args = TYPE_TI_ARGS (context);
1088 else
1089 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1090 current_depth++;
1092 context = TYPE_CONTEXT (context);
1095 if (TREE_CODE (decl) == FUNCTION_DECL)
1097 bool is_template;
1098 tree friend_type;
1099 tree decl_type;
1100 tree friend_args_type;
1101 tree decl_args_type;
1103 /* Make sure that both DECL and FRIEND are templates or
1104 non-templates. */
1105 is_template = DECL_TEMPLATE_INFO (decl)
1106 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1107 if (need_template ^ is_template)
1108 return false;
1109 else if (is_template)
1111 /* If both are templates, check template parameter list. */
1112 tree friend_parms
1113 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1114 args, tf_none);
1115 if (!comp_template_parms
1116 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1117 friend_parms))
1118 return false;
1120 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1122 else
1123 decl_type = TREE_TYPE (decl);
1125 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1126 tf_none, NULL_TREE);
1127 if (friend_type == error_mark_node)
1128 return false;
1130 /* Check if return types match. */
1131 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1132 return false;
1134 /* Check if function parameter types match, ignoring the
1135 `this' parameter. */
1136 friend_args_type = TYPE_ARG_TYPES (friend_type);
1137 decl_args_type = TYPE_ARG_TYPES (decl_type);
1138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1139 friend_args_type = TREE_CHAIN (friend_args_type);
1140 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1141 decl_args_type = TREE_CHAIN (decl_args_type);
1143 return compparms (decl_args_type, friend_args_type);
1145 else
1147 /* DECL is a TYPE_DECL */
1148 bool is_template;
1149 tree decl_type = TREE_TYPE (decl);
1151 /* Make sure that both DECL and FRIEND are templates or
1152 non-templates. */
1153 is_template
1154 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1155 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1157 if (need_template ^ is_template)
1158 return false;
1159 else if (is_template)
1161 tree friend_parms;
1162 /* If both are templates, check the name of the two
1163 TEMPLATE_DECL's first because is_friend didn't. */
1164 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1165 != DECL_NAME (friend))
1166 return false;
1168 /* Now check template parameter list. */
1169 friend_parms
1170 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1171 args, tf_none);
1172 return comp_template_parms
1173 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1174 friend_parms);
1176 else
1177 return (DECL_NAME (decl)
1178 == DECL_NAME (friend));
1181 return false;
1184 /* Register the specialization SPEC as a specialization of TMPL with
1185 the indicated ARGS. IS_FRIEND indicates whether the specialization
1186 is actually just a friend declaration. Returns SPEC, or an
1187 equivalent prior declaration, if available. */
1189 static tree
1190 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1192 tree fn;
1194 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1196 if (TREE_CODE (spec) == FUNCTION_DECL
1197 && uses_template_parms (DECL_TI_ARGS (spec)))
1198 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1199 register it; we want the corresponding TEMPLATE_DECL instead.
1200 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1201 the more obvious `uses_template_parms (spec)' to avoid problems
1202 with default function arguments. In particular, given
1203 something like this:
1205 template <class T> void f(T t1, T t = T())
1207 the default argument expression is not substituted for in an
1208 instantiation unless and until it is actually needed. */
1209 return spec;
1211 fn = retrieve_specialization (tmpl, args,
1212 /*class_specializations_p=*/false);
1213 /* We can sometimes try to re-register a specialization that we've
1214 already got. In particular, regenerate_decl_from_template calls
1215 duplicate_decls which will update the specialization list. But,
1216 we'll still get called again here anyhow. It's more convenient
1217 to simply allow this than to try to prevent it. */
1218 if (fn == spec)
1219 return spec;
1220 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1222 if (DECL_TEMPLATE_INSTANTIATION (fn))
1224 if (TREE_USED (fn)
1225 || DECL_EXPLICIT_INSTANTIATION (fn))
1227 error ("specialization of %qD after instantiation",
1228 fn);
1229 return error_mark_node;
1231 else
1233 tree clone;
1234 /* This situation should occur only if the first
1235 specialization is an implicit instantiation, the
1236 second is an explicit specialization, and the
1237 implicit instantiation has not yet been used. That
1238 situation can occur if we have implicitly
1239 instantiated a member function and then specialized
1240 it later.
1242 We can also wind up here if a friend declaration that
1243 looked like an instantiation turns out to be a
1244 specialization:
1246 template <class T> void foo(T);
1247 class S { friend void foo<>(int) };
1248 template <> void foo(int);
1250 We transform the existing DECL in place so that any
1251 pointers to it become pointers to the updated
1252 declaration.
1254 If there was a definition for the template, but not
1255 for the specialization, we want this to look as if
1256 there were no definition, and vice versa. */
1257 DECL_INITIAL (fn) = NULL_TREE;
1258 duplicate_decls (spec, fn, is_friend);
1259 /* The call to duplicate_decls will have applied
1260 [temp.expl.spec]:
1262 An explicit specialization of a function template
1263 is inline only if it is explicitly declared to be,
1264 and independently of whether its function template
1267 to the primary function; now copy the inline bits to
1268 the various clones. */
1269 FOR_EACH_CLONE (clone, fn)
1271 DECL_DECLARED_INLINE_P (clone)
1272 = DECL_DECLARED_INLINE_P (fn);
1273 DECL_INLINE (clone)
1274 = DECL_INLINE (fn);
1276 check_specialization_namespace (fn);
1278 return fn;
1281 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1283 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1284 /* Dup decl failed, but this is a new definition. Set the
1285 line number so any errors match this new
1286 definition. */
1287 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1289 return fn;
1293 /* A specialization must be declared in the same namespace as the
1294 template it is specializing. */
1295 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1296 && !check_specialization_namespace (tmpl))
1297 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1299 if (!optimize_specialization_lookup_p (tmpl))
1300 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1301 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1303 return spec;
1306 /* Unregister the specialization SPEC as a specialization of TMPL.
1307 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1308 if the SPEC was listed as a specialization of TMPL. */
1310 bool
1311 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1313 tree* s;
1315 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1316 *s != NULL_TREE;
1317 s = &TREE_CHAIN (*s))
1318 if (TREE_VALUE (*s) == spec)
1320 if (!new_spec)
1321 *s = TREE_CHAIN (*s);
1322 else
1323 TREE_VALUE (*s) = new_spec;
1324 return 1;
1327 return 0;
1330 /* Compare an entry in the local specializations hash table P1 (which
1331 is really a pointer to a TREE_LIST) with P2 (which is really a
1332 DECL). */
1334 static int
1335 eq_local_specializations (const void *p1, const void *p2)
1337 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1340 /* Hash P1, an entry in the local specializations table. */
1342 static hashval_t
1343 hash_local_specialization (const void* p1)
1345 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1348 /* Like register_specialization, but for local declarations. We are
1349 registering SPEC, an instantiation of TMPL. */
1351 static void
1352 register_local_specialization (tree spec, tree tmpl)
1354 void **slot;
1356 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1357 htab_hash_pointer (tmpl), INSERT);
1358 *slot = build_tree_list (spec, tmpl);
1361 /* TYPE is a class type. Returns true if TYPE is an explicitly
1362 specialized class. */
1364 bool
1365 explicit_class_specialization_p (tree type)
1367 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1368 return false;
1369 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1372 /* Print the list of candidate FNS in an error message. */
1374 void
1375 print_candidates (tree fns)
1377 tree fn;
1379 const char *str = "candidates are:";
1381 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1383 tree f;
1385 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1386 error ("%s %+#D", str, OVL_CURRENT (f));
1387 str = " ";
1391 /* Returns the template (one of the functions given by TEMPLATE_ID)
1392 which can be specialized to match the indicated DECL with the
1393 explicit template args given in TEMPLATE_ID. The DECL may be
1394 NULL_TREE if none is available. In that case, the functions in
1395 TEMPLATE_ID are non-members.
1397 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1398 specialization of a member template.
1400 The TEMPLATE_COUNT is the number of references to qualifying
1401 template classes that appeared in the name of the function. See
1402 check_explicit_specialization for a more accurate description.
1404 TSK indicates what kind of template declaration (if any) is being
1405 declared. TSK_TEMPLATE indicates that the declaration given by
1406 DECL, though a FUNCTION_DECL, has template parameters, and is
1407 therefore a template function.
1409 The template args (those explicitly specified and those deduced)
1410 are output in a newly created vector *TARGS_OUT.
1412 If it is impossible to determine the result, an error message is
1413 issued. The error_mark_node is returned to indicate failure. */
1415 static tree
1416 determine_specialization (tree template_id,
1417 tree decl,
1418 tree* targs_out,
1419 int need_member_template,
1420 int template_count,
1421 tmpl_spec_kind tsk)
1423 tree fns;
1424 tree targs;
1425 tree explicit_targs;
1426 tree candidates = NULL_TREE;
1427 /* A TREE_LIST of templates of which DECL may be a specialization.
1428 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1429 corresponding TREE_PURPOSE is the set of template arguments that,
1430 when used to instantiate the template, would produce a function
1431 with the signature of DECL. */
1432 tree templates = NULL_TREE;
1433 int header_count;
1434 struct cp_binding_level *b;
1436 *targs_out = NULL_TREE;
1438 if (template_id == error_mark_node || decl == error_mark_node)
1439 return error_mark_node;
1441 fns = TREE_OPERAND (template_id, 0);
1442 explicit_targs = TREE_OPERAND (template_id, 1);
1444 if (fns == error_mark_node)
1445 return error_mark_node;
1447 /* Check for baselinks. */
1448 if (BASELINK_P (fns))
1449 fns = BASELINK_FUNCTIONS (fns);
1451 if (!is_overloaded_fn (fns))
1453 error ("%qD is not a function template", fns);
1454 return error_mark_node;
1457 /* Count the number of template headers specified for this
1458 specialization. */
1459 header_count = 0;
1460 for (b = current_binding_level;
1461 b->kind == sk_template_parms;
1462 b = b->level_chain)
1463 ++header_count;
1465 for (; fns; fns = OVL_NEXT (fns))
1467 tree fn = OVL_CURRENT (fns);
1469 if (TREE_CODE (fn) == TEMPLATE_DECL)
1471 tree decl_arg_types;
1472 tree fn_arg_types;
1474 /* In case of explicit specialization, we need to check if
1475 the number of template headers appearing in the specialization
1476 is correct. This is usually done in check_explicit_specialization,
1477 but the check done there cannot be exhaustive when specializing
1478 member functions. Consider the following code:
1480 template <> void A<int>::f(int);
1481 template <> template <> void A<int>::f(int);
1483 Assuming that A<int> is not itself an explicit specialization
1484 already, the first line specializes "f" which is a non-template
1485 member function, whilst the second line specializes "f" which
1486 is a template member function. So both lines are syntactically
1487 correct, and check_explicit_specialization does not reject
1488 them.
1490 Here, we can do better, as we are matching the specialization
1491 against the declarations. We count the number of template
1492 headers, and we check if they match TEMPLATE_COUNT + 1
1493 (TEMPLATE_COUNT is the number of qualifying template classes,
1494 plus there must be another header for the member template
1495 itself).
1497 Notice that if header_count is zero, this is not a
1498 specialization but rather a template instantiation, so there
1499 is no check we can perform here. */
1500 if (header_count && header_count != template_count + 1)
1501 continue;
1503 /* Check that the number of template arguments at the
1504 innermost level for DECL is the same as for FN. */
1505 if (current_binding_level->kind == sk_template_parms
1506 && !current_binding_level->explicit_spec_p
1507 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1508 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1509 (current_template_parms))))
1510 continue;
1512 /* DECL might be a specialization of FN. */
1513 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1514 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1516 /* For a non-static member function, we need to make sure
1517 that the const qualification is the same. Since
1518 get_bindings does not try to merge the "this" parameter,
1519 we must do the comparison explicitly. */
1520 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1521 && !same_type_p (TREE_VALUE (fn_arg_types),
1522 TREE_VALUE (decl_arg_types)))
1523 continue;
1525 /* Skip the "this" parameter and, for constructors of
1526 classes with virtual bases, the VTT parameter. A
1527 full specialization of a constructor will have a VTT
1528 parameter, but a template never will. */
1529 decl_arg_types
1530 = skip_artificial_parms_for (decl, decl_arg_types);
1531 fn_arg_types
1532 = skip_artificial_parms_for (fn, fn_arg_types);
1534 /* Check that the number of function parameters matches.
1535 For example,
1536 template <class T> void f(int i = 0);
1537 template <> void f<int>();
1538 The specialization f<int> is invalid but is not caught
1539 by get_bindings below. */
1540 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1541 continue;
1543 /* Function templates cannot be specializations; there are
1544 no partial specializations of functions. Therefore, if
1545 the type of DECL does not match FN, there is no
1546 match. */
1547 if (tsk == tsk_template)
1549 if (compparms (fn_arg_types, decl_arg_types))
1550 candidates = tree_cons (NULL_TREE, fn, candidates);
1551 continue;
1554 /* See whether this function might be a specialization of this
1555 template. */
1556 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1558 if (!targs)
1559 /* We cannot deduce template arguments that when used to
1560 specialize TMPL will produce DECL. */
1561 continue;
1563 /* Save this template, and the arguments deduced. */
1564 templates = tree_cons (targs, fn, templates);
1566 else if (need_member_template)
1567 /* FN is an ordinary member function, and we need a
1568 specialization of a member template. */
1570 else if (TREE_CODE (fn) != FUNCTION_DECL)
1571 /* We can get IDENTIFIER_NODEs here in certain erroneous
1572 cases. */
1574 else if (!DECL_FUNCTION_MEMBER_P (fn))
1575 /* This is just an ordinary non-member function. Nothing can
1576 be a specialization of that. */
1578 else if (DECL_ARTIFICIAL (fn))
1579 /* Cannot specialize functions that are created implicitly. */
1581 else
1583 tree decl_arg_types;
1585 /* This is an ordinary member function. However, since
1586 we're here, we can assume it's enclosing class is a
1587 template class. For example,
1589 template <typename T> struct S { void f(); };
1590 template <> void S<int>::f() {}
1592 Here, S<int>::f is a non-template, but S<int> is a
1593 template class. If FN has the same type as DECL, we
1594 might be in business. */
1596 if (!DECL_TEMPLATE_INFO (fn))
1597 /* Its enclosing class is an explicit specialization
1598 of a template class. This is not a candidate. */
1599 continue;
1601 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1602 TREE_TYPE (TREE_TYPE (fn))))
1603 /* The return types differ. */
1604 continue;
1606 /* Adjust the type of DECL in case FN is a static member. */
1607 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1608 if (DECL_STATIC_FUNCTION_P (fn)
1609 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1610 decl_arg_types = TREE_CHAIN (decl_arg_types);
1612 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1613 decl_arg_types))
1614 /* They match! */
1615 candidates = tree_cons (NULL_TREE, fn, candidates);
1619 if (templates && TREE_CHAIN (templates))
1621 /* We have:
1623 [temp.expl.spec]
1625 It is possible for a specialization with a given function
1626 signature to be instantiated from more than one function
1627 template. In such cases, explicit specification of the
1628 template arguments must be used to uniquely identify the
1629 function template specialization being specialized.
1631 Note that here, there's no suggestion that we're supposed to
1632 determine which of the candidate templates is most
1633 specialized. However, we, also have:
1635 [temp.func.order]
1637 Partial ordering of overloaded function template
1638 declarations is used in the following contexts to select
1639 the function template to which a function template
1640 specialization refers:
1642 -- when an explicit specialization refers to a function
1643 template.
1645 So, we do use the partial ordering rules, at least for now.
1646 This extension can only serve to make invalid programs valid,
1647 so it's safe. And, there is strong anecdotal evidence that
1648 the committee intended the partial ordering rules to apply;
1649 the EDG front end has that behavior, and John Spicer claims
1650 that the committee simply forgot to delete the wording in
1651 [temp.expl.spec]. */
1652 tree tmpl = most_specialized_instantiation (templates);
1653 if (tmpl != error_mark_node)
1655 templates = tmpl;
1656 TREE_CHAIN (templates) = NULL_TREE;
1660 if (templates == NULL_TREE && candidates == NULL_TREE)
1662 error ("template-id %qD for %q+D does not match any template "
1663 "declaration", template_id, decl);
1664 return error_mark_node;
1666 else if ((templates && TREE_CHAIN (templates))
1667 || (candidates && TREE_CHAIN (candidates))
1668 || (templates && candidates))
1670 error ("ambiguous template specialization %qD for %q+D",
1671 template_id, decl);
1672 chainon (candidates, templates);
1673 print_candidates (candidates);
1674 return error_mark_node;
1677 /* We have one, and exactly one, match. */
1678 if (candidates)
1680 tree fn = TREE_VALUE (candidates);
1681 /* DECL is a re-declaration of a template function. */
1682 if (TREE_CODE (fn) == TEMPLATE_DECL)
1683 return fn;
1684 /* It was a specialization of an ordinary member function in a
1685 template class. */
1686 *targs_out = copy_node (DECL_TI_ARGS (fn));
1687 return DECL_TI_TEMPLATE (fn);
1690 /* It was a specialization of a template. */
1691 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1692 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1694 *targs_out = copy_node (targs);
1695 SET_TMPL_ARGS_LEVEL (*targs_out,
1696 TMPL_ARGS_DEPTH (*targs_out),
1697 TREE_PURPOSE (templates));
1699 else
1700 *targs_out = TREE_PURPOSE (templates);
1701 return TREE_VALUE (templates);
1704 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1705 but with the default argument values filled in from those in the
1706 TMPL_TYPES. */
1708 static tree
1709 copy_default_args_to_explicit_spec_1 (tree spec_types,
1710 tree tmpl_types)
1712 tree new_spec_types;
1714 if (!spec_types)
1715 return NULL_TREE;
1717 if (spec_types == void_list_node)
1718 return void_list_node;
1720 /* Substitute into the rest of the list. */
1721 new_spec_types =
1722 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1723 TREE_CHAIN (tmpl_types));
1725 /* Add the default argument for this parameter. */
1726 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1727 TREE_VALUE (spec_types),
1728 new_spec_types);
1731 /* DECL is an explicit specialization. Replicate default arguments
1732 from the template it specializes. (That way, code like:
1734 template <class T> void f(T = 3);
1735 template <> void f(double);
1736 void g () { f (); }
1738 works, as required.) An alternative approach would be to look up
1739 the correct default arguments at the call-site, but this approach
1740 is consistent with how implicit instantiations are handled. */
1742 static void
1743 copy_default_args_to_explicit_spec (tree decl)
1745 tree tmpl;
1746 tree spec_types;
1747 tree tmpl_types;
1748 tree new_spec_types;
1749 tree old_type;
1750 tree new_type;
1751 tree t;
1752 tree object_type = NULL_TREE;
1753 tree in_charge = NULL_TREE;
1754 tree vtt = NULL_TREE;
1756 /* See if there's anything we need to do. */
1757 tmpl = DECL_TI_TEMPLATE (decl);
1758 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1759 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1760 if (TREE_PURPOSE (t))
1761 break;
1762 if (!t)
1763 return;
1765 old_type = TREE_TYPE (decl);
1766 spec_types = TYPE_ARG_TYPES (old_type);
1768 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1770 /* Remove the this pointer, but remember the object's type for
1771 CV quals. */
1772 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1773 spec_types = TREE_CHAIN (spec_types);
1774 tmpl_types = TREE_CHAIN (tmpl_types);
1776 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1778 /* DECL may contain more parameters than TMPL due to the extra
1779 in-charge parameter in constructors and destructors. */
1780 in_charge = spec_types;
1781 spec_types = TREE_CHAIN (spec_types);
1783 if (DECL_HAS_VTT_PARM_P (decl))
1785 vtt = spec_types;
1786 spec_types = TREE_CHAIN (spec_types);
1790 /* Compute the merged default arguments. */
1791 new_spec_types =
1792 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1794 /* Compute the new FUNCTION_TYPE. */
1795 if (object_type)
1797 if (vtt)
1798 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1799 TREE_VALUE (vtt),
1800 new_spec_types);
1802 if (in_charge)
1803 /* Put the in-charge parameter back. */
1804 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1805 TREE_VALUE (in_charge),
1806 new_spec_types);
1808 new_type = build_method_type_directly (object_type,
1809 TREE_TYPE (old_type),
1810 new_spec_types);
1812 else
1813 new_type = build_function_type (TREE_TYPE (old_type),
1814 new_spec_types);
1815 new_type = cp_build_type_attribute_variant (new_type,
1816 TYPE_ATTRIBUTES (old_type));
1817 new_type = build_exception_variant (new_type,
1818 TYPE_RAISES_EXCEPTIONS (old_type));
1819 TREE_TYPE (decl) = new_type;
1822 /* Check to see if the function just declared, as indicated in
1823 DECLARATOR, and in DECL, is a specialization of a function
1824 template. We may also discover that the declaration is an explicit
1825 instantiation at this point.
1827 Returns DECL, or an equivalent declaration that should be used
1828 instead if all goes well. Issues an error message if something is
1829 amiss. Returns error_mark_node if the error is not easily
1830 recoverable.
1832 FLAGS is a bitmask consisting of the following flags:
1834 2: The function has a definition.
1835 4: The function is a friend.
1837 The TEMPLATE_COUNT is the number of references to qualifying
1838 template classes that appeared in the name of the function. For
1839 example, in
1841 template <class T> struct S { void f(); };
1842 void S<int>::f();
1844 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1845 classes are not counted in the TEMPLATE_COUNT, so that in
1847 template <class T> struct S {};
1848 template <> struct S<int> { void f(); }
1849 template <> void S<int>::f();
1851 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1852 invalid; there should be no template <>.)
1854 If the function is a specialization, it is marked as such via
1855 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1856 is set up correctly, and it is added to the list of specializations
1857 for that template. */
1859 tree
1860 check_explicit_specialization (tree declarator,
1861 tree decl,
1862 int template_count,
1863 int flags)
1865 int have_def = flags & 2;
1866 int is_friend = flags & 4;
1867 int specialization = 0;
1868 int explicit_instantiation = 0;
1869 int member_specialization = 0;
1870 tree ctype = DECL_CLASS_CONTEXT (decl);
1871 tree dname = DECL_NAME (decl);
1872 tmpl_spec_kind tsk;
1874 if (is_friend)
1876 if (!processing_specialization)
1877 tsk = tsk_none;
1878 else
1879 tsk = tsk_excessive_parms;
1881 else
1882 tsk = current_tmpl_spec_kind (template_count);
1884 switch (tsk)
1886 case tsk_none:
1887 if (processing_specialization)
1889 specialization = 1;
1890 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1892 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1894 if (is_friend)
1895 /* This could be something like:
1897 template <class T> void f(T);
1898 class S { friend void f<>(int); } */
1899 specialization = 1;
1900 else
1902 /* This case handles bogus declarations like template <>
1903 template <class T> void f<int>(); */
1905 error ("template-id %qD in declaration of primary template",
1906 declarator);
1907 return decl;
1910 break;
1912 case tsk_invalid_member_spec:
1913 /* The error has already been reported in
1914 check_specialization_scope. */
1915 return error_mark_node;
1917 case tsk_invalid_expl_inst:
1918 error ("template parameter list used in explicit instantiation");
1920 /* Fall through. */
1922 case tsk_expl_inst:
1923 if (have_def)
1924 error ("definition provided for explicit instantiation");
1926 explicit_instantiation = 1;
1927 break;
1929 case tsk_excessive_parms:
1930 case tsk_insufficient_parms:
1931 if (tsk == tsk_excessive_parms)
1932 error ("too many template parameter lists in declaration of %qD",
1933 decl);
1934 else if (template_header_count)
1935 error("too few template parameter lists in declaration of %qD", decl);
1936 else
1937 error("explicit specialization of %qD must be introduced by "
1938 "%<template <>%>", decl);
1940 /* Fall through. */
1941 case tsk_expl_spec:
1942 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1943 if (ctype)
1944 member_specialization = 1;
1945 else
1946 specialization = 1;
1947 break;
1949 case tsk_template:
1950 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1952 /* This case handles bogus declarations like template <>
1953 template <class T> void f<int>(); */
1955 if (uses_template_parms (declarator))
1956 error ("function template partial specialization %qD "
1957 "is not allowed", declarator);
1958 else
1959 error ("template-id %qD in declaration of primary template",
1960 declarator);
1961 return decl;
1964 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1965 /* This is a specialization of a member template, without
1966 specialization the containing class. Something like:
1968 template <class T> struct S {
1969 template <class U> void f (U);
1971 template <> template <class U> void S<int>::f(U) {}
1973 That's a specialization -- but of the entire template. */
1974 specialization = 1;
1975 break;
1977 default:
1978 gcc_unreachable ();
1981 if (specialization || member_specialization)
1983 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1984 for (; t; t = TREE_CHAIN (t))
1985 if (TREE_PURPOSE (t))
1987 pedwarn
1988 ("default argument specified in explicit specialization");
1989 break;
1993 if (specialization || member_specialization || explicit_instantiation)
1995 tree tmpl = NULL_TREE;
1996 tree targs = NULL_TREE;
1998 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1999 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2001 tree fns;
2003 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2004 if (ctype)
2005 fns = dname;
2006 else
2008 /* If there is no class context, the explicit instantiation
2009 must be at namespace scope. */
2010 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2012 /* Find the namespace binding, using the declaration
2013 context. */
2014 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2015 false, true);
2016 if (fns == error_mark_node || !is_overloaded_fn (fns))
2018 error ("%qD is not a template function", dname);
2019 fns = error_mark_node;
2021 else
2023 tree fn = OVL_CURRENT (fns);
2024 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2025 CP_DECL_CONTEXT (fn)))
2026 error ("%qD is not declared in %qD",
2027 decl, current_namespace);
2031 declarator = lookup_template_function (fns, NULL_TREE);
2034 if (declarator == error_mark_node)
2035 return error_mark_node;
2037 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2039 if (!explicit_instantiation)
2040 /* A specialization in class scope. This is invalid,
2041 but the error will already have been flagged by
2042 check_specialization_scope. */
2043 return error_mark_node;
2044 else
2046 /* It's not valid to write an explicit instantiation in
2047 class scope, e.g.:
2049 class C { template void f(); }
2051 This case is caught by the parser. However, on
2052 something like:
2054 template class C { void f(); };
2056 (which is invalid) we can get here. The error will be
2057 issued later. */
2061 return decl;
2063 else if (ctype != NULL_TREE
2064 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2065 IDENTIFIER_NODE))
2067 /* Find the list of functions in ctype that have the same
2068 name as the declared function. */
2069 tree name = TREE_OPERAND (declarator, 0);
2070 tree fns = NULL_TREE;
2071 int idx;
2073 if (constructor_name_p (name, ctype))
2075 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2077 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2078 : !CLASSTYPE_DESTRUCTORS (ctype))
2080 /* From [temp.expl.spec]:
2082 If such an explicit specialization for the member
2083 of a class template names an implicitly-declared
2084 special member function (clause _special_), the
2085 program is ill-formed.
2087 Similar language is found in [temp.explicit]. */
2088 error ("specialization of implicitly-declared special member function");
2089 return error_mark_node;
2092 name = is_constructor ? ctor_identifier : dtor_identifier;
2095 if (!DECL_CONV_FN_P (decl))
2097 idx = lookup_fnfields_1 (ctype, name);
2098 if (idx >= 0)
2099 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2101 else
2103 VEC(tree,gc) *methods;
2104 tree ovl;
2106 /* For a type-conversion operator, we cannot do a
2107 name-based lookup. We might be looking for `operator
2108 int' which will be a specialization of `operator T'.
2109 So, we find *all* the conversion operators, and then
2110 select from them. */
2111 fns = NULL_TREE;
2113 methods = CLASSTYPE_METHOD_VEC (ctype);
2114 if (methods)
2115 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2116 VEC_iterate (tree, methods, idx, ovl);
2117 ++idx)
2119 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2120 /* There are no more conversion functions. */
2121 break;
2123 /* Glue all these conversion functions together
2124 with those we already have. */
2125 for (; ovl; ovl = OVL_NEXT (ovl))
2126 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2130 if (fns == NULL_TREE)
2132 error ("no member function %qD declared in %qT", name, ctype);
2133 return error_mark_node;
2135 else
2136 TREE_OPERAND (declarator, 0) = fns;
2139 /* Figure out what exactly is being specialized at this point.
2140 Note that for an explicit instantiation, even one for a
2141 member function, we cannot tell apriori whether the
2142 instantiation is for a member template, or just a member
2143 function of a template class. Even if a member template is
2144 being instantiated, the member template arguments may be
2145 elided if they can be deduced from the rest of the
2146 declaration. */
2147 tmpl = determine_specialization (declarator, decl,
2148 &targs,
2149 member_specialization,
2150 template_count,
2151 tsk);
2153 if (!tmpl || tmpl == error_mark_node)
2154 /* We couldn't figure out what this declaration was
2155 specializing. */
2156 return error_mark_node;
2157 else
2159 tree gen_tmpl = most_general_template (tmpl);
2161 if (explicit_instantiation)
2163 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2164 is done by do_decl_instantiation later. */
2166 int arg_depth = TMPL_ARGS_DEPTH (targs);
2167 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2169 if (arg_depth > parm_depth)
2171 /* If TMPL is not the most general template (for
2172 example, if TMPL is a friend template that is
2173 injected into namespace scope), then there will
2174 be too many levels of TARGS. Remove some of them
2175 here. */
2176 int i;
2177 tree new_targs;
2179 new_targs = make_tree_vec (parm_depth);
2180 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2181 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2182 = TREE_VEC_ELT (targs, i);
2183 targs = new_targs;
2186 return instantiate_template (tmpl, targs, tf_error);
2189 /* If we thought that the DECL was a member function, but it
2190 turns out to be specializing a static member function,
2191 make DECL a static member function as well. */
2192 if (DECL_STATIC_FUNCTION_P (tmpl)
2193 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2194 revert_static_member_fn (decl);
2196 /* If this is a specialization of a member template of a
2197 template class, we want to return the TEMPLATE_DECL, not
2198 the specialization of it. */
2199 if (tsk == tsk_template)
2201 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2202 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2203 if (have_def)
2205 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2206 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2207 = DECL_SOURCE_LOCATION (decl);
2208 /* We want to use the argument list specified in the
2209 definition, not in the original declaration. */
2210 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2211 = DECL_ARGUMENTS (decl);
2213 return tmpl;
2216 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2217 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2219 /* Inherit default function arguments from the template
2220 DECL is specializing. */
2221 copy_default_args_to_explicit_spec (decl);
2223 /* This specialization has the same protection as the
2224 template it specializes. */
2225 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2226 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2228 /* 7.1.1-1 [dcl.stc]
2230 A storage-class-specifier shall not be specified in an
2231 explicit specialization...
2233 The parser rejects these, so unless action is taken here,
2234 explicit function specializations will always appear with
2235 global linkage.
2237 The action recommended by the C++ CWG in response to C++
2238 defect report 605 is to make the storage class and linkage
2239 of the explicit specialization match the templated function:
2241 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2243 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2245 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2246 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2248 /* This specialization has the same linkage and visibility as
2249 the function template it specializes. */
2250 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2251 if (! TREE_PUBLIC (decl))
2253 DECL_INTERFACE_KNOWN (decl) = 1;
2254 DECL_NOT_REALLY_EXTERN (decl) = 1;
2256 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2257 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2259 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2260 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2264 /* If DECL is a friend declaration, declared using an
2265 unqualified name, the namespace associated with DECL may
2266 have been set incorrectly. For example, in:
2268 template <typename T> void f(T);
2269 namespace N {
2270 struct S { friend void f<int>(int); }
2273 we will have set the DECL_CONTEXT for the friend
2274 declaration to N, rather than to the global namespace. */
2275 if (DECL_NAMESPACE_SCOPE_P (decl))
2276 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2278 if (is_friend && !have_def)
2279 /* This is not really a declaration of a specialization.
2280 It's just the name of an instantiation. But, it's not
2281 a request for an instantiation, either. */
2282 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2283 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2284 /* This is indeed a specialization. In case of constructors
2285 and destructors, we need in-charge and not-in-charge
2286 versions in V3 ABI. */
2287 clone_function_decl (decl, /*update_method_vec_p=*/0);
2289 /* Register this specialization so that we can find it
2290 again. */
2291 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2295 return decl;
2298 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2299 parameters. These are represented in the same format used for
2300 DECL_TEMPLATE_PARMS. */
2303 comp_template_parms (const_tree parms1, const_tree parms2)
2305 const_tree p1;
2306 const_tree p2;
2308 if (parms1 == parms2)
2309 return 1;
2311 for (p1 = parms1, p2 = parms2;
2312 p1 != NULL_TREE && p2 != NULL_TREE;
2313 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2315 tree t1 = TREE_VALUE (p1);
2316 tree t2 = TREE_VALUE (p2);
2317 int i;
2319 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2320 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2322 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2323 return 0;
2325 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2327 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2328 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2330 /* If either of the template parameters are invalid, assume
2331 they match for the sake of error recovery. */
2332 if (parm1 == error_mark_node || parm2 == error_mark_node)
2333 return 1;
2335 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2336 return 0;
2338 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2339 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2340 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2341 continue;
2342 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2343 return 0;
2347 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2348 /* One set of parameters has more parameters lists than the
2349 other. */
2350 return 0;
2352 return 1;
2355 /* Determine whether PARM is a parameter pack. */
2356 bool
2357 template_parameter_pack_p (const_tree parm)
2359 /* Determine if we have a non-type template parameter pack. */
2360 if (TREE_CODE (parm) == PARM_DECL)
2361 return (DECL_TEMPLATE_PARM_P (parm)
2362 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2364 /* If this is a list of template parameters, we could get a
2365 TYPE_DECL or a TEMPLATE_DECL. */
2366 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2367 parm = TREE_TYPE (parm);
2369 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2370 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2371 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2374 /* Determine whether ARGS describes a variadic template args list,
2375 i.e., one that is terminated by a template argument pack. */
2376 static bool
2377 template_args_variadic_p (tree args)
2379 int nargs;
2380 tree last_parm;
2382 if (args == NULL_TREE)
2383 return false;
2385 args = INNERMOST_TEMPLATE_ARGS (args);
2386 nargs = TREE_VEC_LENGTH (args);
2388 if (nargs == 0)
2389 return false;
2391 last_parm = TREE_VEC_ELT (args, nargs - 1);
2393 return ARGUMENT_PACK_P (last_parm);
2396 /* Generate a new name for the parameter pack name NAME (an
2397 IDENTIFIER_NODE) that incorporates its */
2398 static tree
2399 make_ith_pack_parameter_name (tree name, int i)
2401 /* Munge the name to include the parameter index. */
2402 char numbuf[128];
2403 char* newname;
2405 sprintf(numbuf, "%i", i);
2406 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2407 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2408 return get_identifier (newname);
2411 /* Structure used to track the progress of find_parameter_pack_r. */
2412 struct find_parameter_pack_data
2414 tree* parameter_packs;
2415 struct pointer_set_t *visited;
2418 /* Identifiers all of the argument packs that occur in a template
2419 argument and appends them to the TREE_LIST inside DATA, which is a
2420 find_parameter_pack_Data structure. This is a subroutine of
2421 make_pack_expansion and uses_parameter_packs. */
2422 static tree
2423 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2425 tree t = *tp;
2426 struct find_parameter_pack_data* ppd =
2427 (struct find_parameter_pack_data*)data;
2429 if (TYPE_P (t))
2431 tree context = TYPE_CONTEXT (t);
2432 cp_walk_tree (&context, &find_parameter_packs_r, ppd, ppd->visited);
2435 /* This switch statement will return immediately if we don't find a
2436 parameter pack. */
2437 switch (TREE_CODE (t))
2439 case TEMPLATE_PARM_INDEX:
2440 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2441 break;
2442 return NULL_TREE;
2444 case BOUND_TEMPLATE_TEMPLATE_PARM:
2445 /* Check the template arguments. */
2446 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2447 ppd->visited);
2449 /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */
2450 t = TYPE_TI_TEMPLATE (t);
2451 if (DECL_P (t) && TREE_TYPE (t))
2452 t = TREE_TYPE (t);
2453 *walk_subtrees = 0;
2455 /* Fall through. */
2457 case TEMPLATE_TYPE_PARM:
2458 case TEMPLATE_TEMPLATE_PARM:
2459 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2460 break;
2461 return NULL_TREE;
2463 case PARM_DECL:
2464 if (FUNCTION_PARAMETER_PACK_P (t))
2466 /* We don't want to walk into the type of a PARM_DECL,
2467 because we don't want to see the type parameter pack.*/
2468 *walk_subtrees = 0;
2469 break;
2471 return NULL_TREE;
2473 case RECORD_TYPE:
2474 if (TYPE_PTRMEMFUNC_P (t))
2475 return NULL_TREE;
2476 /* Fall through. */
2478 case UNION_TYPE:
2479 case ENUMERAL_TYPE:
2480 if (TYPE_TEMPLATE_INFO (t))
2482 tree args = TREE_VALUE (TYPE_TEMPLATE_INFO (t));
2483 cp_walk_tree (&args, &find_parameter_packs_r, ppd, ppd->visited);
2486 *walk_subtrees = 0;
2487 return NULL_TREE;
2489 case TEMPLATE_DECL:
2490 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
2491 && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2492 break;
2494 *walk_subtrees = 0;
2495 return NULL_TREE;
2497 case TYPE_PACK_EXPANSION:
2498 case EXPR_PACK_EXPANSION:
2499 *walk_subtrees = 0;
2500 return NULL_TREE;
2502 case INTEGER_TYPE:
2503 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2504 ppd, ppd->visited);
2505 *walk_subtrees = 0;
2506 return NULL_TREE;
2508 default:
2509 return NULL_TREE;
2512 /* Add this parameter pack to the list. */
2513 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2515 return NULL_TREE;
2518 /* Determines if the expression or type T uses any parameter packs. */
2519 bool
2520 uses_parameter_packs (tree t)
2522 tree parameter_packs = NULL_TREE;
2523 struct find_parameter_pack_data ppd;
2524 ppd.parameter_packs = &parameter_packs;
2525 ppd.visited = pointer_set_create ();
2526 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2527 pointer_set_destroy (ppd.visited);
2528 return parameter_packs != NULL_TREE;
2531 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2532 representation a base-class initializer into a parameter pack
2533 expansion. If all goes well, the resulting node will be an
2534 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2535 respectively. */
2536 tree
2537 make_pack_expansion (tree arg)
2539 tree result;
2540 tree parameter_packs = NULL_TREE;
2541 bool for_types = false;
2542 struct find_parameter_pack_data ppd;
2544 if (!arg || arg == error_mark_node)
2545 return arg;
2547 if (TREE_CODE (arg) == TREE_LIST)
2549 /* The only time we will see a TREE_LIST here is for a base
2550 class initializer. In this case, the TREE_PURPOSE will be a
2551 _TYPE node (representing the base class expansion we're
2552 initializing) and the TREE_VALUE will be a TREE_LIST
2553 containing the initialization arguments.
2555 The resulting expansion looks somewhat different from most
2556 expansions. Rather than returning just one _EXPANSION, we
2557 return a TREE_LIST whose TREE_PURPOSE is a
2558 TYPE_PACK_EXPANSION containing the bases that will be
2559 initialized. The TREE_VALUE will be identical to the
2560 original TREE_VALUE, which is a list of arguments that will
2561 be passed to each base. We do not introduce any new pack
2562 expansion nodes into the TREE_VALUE (although it is possible
2563 that some already exist), because the TREE_PURPOSE and
2564 TREE_VALUE all need to be expanded together with the same
2565 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2566 resulting TREE_PURPOSE will mention the parameter packs in
2567 both the bases and the arguments to the bases. */
2568 tree purpose;
2569 tree value;
2570 tree parameter_packs = NULL_TREE;
2572 /* Determine which parameter packs will be used by the base
2573 class expansion. */
2574 ppd.visited = pointer_set_create ();
2575 ppd.parameter_packs = &parameter_packs;
2576 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2577 &ppd, ppd.visited);
2579 if (parameter_packs == NULL_TREE)
2581 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2582 pointer_set_destroy (ppd.visited);
2583 return error_mark_node;
2586 if (TREE_VALUE (arg) != void_type_node)
2588 /* Collect the sets of parameter packs used in each of the
2589 initialization arguments. */
2590 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2592 /* Determine which parameter packs will be expanded in this
2593 argument. */
2594 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2595 &ppd, ppd.visited);
2599 pointer_set_destroy (ppd.visited);
2601 /* Create the pack expansion type for the base type. */
2602 purpose = make_node (TYPE_PACK_EXPANSION);
2603 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2604 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2606 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2607 they will rarely be compared to anything. */
2608 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2610 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2613 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2614 for_types = true;
2616 /* Build the PACK_EXPANSION_* node. */
2617 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2618 SET_PACK_EXPANSION_PATTERN (result, arg);
2619 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2621 /* Propagate type and const-expression information. */
2622 TREE_TYPE (result) = TREE_TYPE (arg);
2623 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2625 else
2626 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2627 they will rarely be compared to anything. */
2628 SET_TYPE_STRUCTURAL_EQUALITY (result);
2630 /* Determine which parameter packs will be expanded. */
2631 ppd.parameter_packs = &parameter_packs;
2632 ppd.visited = pointer_set_create ();
2633 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2634 pointer_set_destroy (ppd.visited);
2636 /* Make sure we found some parameter packs. */
2637 if (parameter_packs == NULL_TREE)
2639 if (TYPE_P (arg))
2640 error ("expansion pattern %<%T%> contains no argument packs", arg);
2641 else
2642 error ("expansion pattern %<%E%> contains no argument packs", arg);
2643 return error_mark_node;
2645 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2647 return result;
2650 /* Checks T for any "bare" parameter packs, which have not yet been
2651 expanded, and issues an error if any are found. This operation can
2652 only be done on full expressions or types (e.g., an expression
2653 statement, "if" condition, etc.), because we could have expressions like:
2655 foo(f(g(h(args)))...)
2657 where "args" is a parameter pack. check_for_bare_parameter_packs
2658 should not be called for the subexpressions args, h(args),
2659 g(h(args)), or f(g(h(args))), because we would produce erroneous
2660 error messages.
2662 Returns TRUE if there were no bare parameter packs, returns FALSE
2663 (and emits an error) if there were bare parameter packs.*/
2664 bool
2665 check_for_bare_parameter_packs (tree t)
2667 tree parameter_packs = NULL_TREE;
2668 struct find_parameter_pack_data ppd;
2670 if (!processing_template_decl || !t || t == error_mark_node)
2671 return true;
2673 if (TREE_CODE (t) == TYPE_DECL)
2674 t = TREE_TYPE (t);
2676 ppd.parameter_packs = &parameter_packs;
2677 ppd.visited = pointer_set_create ();
2678 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2679 pointer_set_destroy (ppd.visited);
2681 if (parameter_packs)
2683 error ("parameter packs not expanded with `...':");
2684 while (parameter_packs)
2686 tree pack = TREE_VALUE (parameter_packs);
2687 tree name = NULL_TREE;
2689 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2690 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2691 name = TYPE_NAME (pack);
2692 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2693 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2694 else
2695 name = DECL_NAME (pack);
2696 inform (" %qD", name);
2698 parameter_packs = TREE_CHAIN (parameter_packs);
2701 return false;
2704 return true;
2707 /* Expand any parameter packs that occur in the template arguments in
2708 ARGS. */
2709 tree
2710 expand_template_argument_pack (tree args)
2712 tree result_args = NULL_TREE;
2713 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2714 int num_result_args = -1;
2716 /* First, determine if we need to expand anything, and the number of
2717 slots we'll need. */
2718 for (in_arg = 0; in_arg < nargs; ++in_arg)
2720 tree arg = TREE_VEC_ELT (args, in_arg);
2721 if (ARGUMENT_PACK_P (arg))
2723 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2724 if (num_result_args < 0)
2725 num_result_args = in_arg + num_packed;
2726 else
2727 num_result_args += num_packed;
2729 else
2731 if (num_result_args >= 0)
2732 num_result_args++;
2736 /* If no expansion is necessary, we're done. */
2737 if (num_result_args < 0)
2738 return args;
2740 /* Expand arguments. */
2741 result_args = make_tree_vec (num_result_args);
2742 for (in_arg = 0; in_arg < nargs; ++in_arg)
2744 tree arg = TREE_VEC_ELT (args, in_arg);
2745 if (ARGUMENT_PACK_P (arg))
2747 tree packed = ARGUMENT_PACK_ARGS (arg);
2748 int i, num_packed = TREE_VEC_LENGTH (packed);
2749 for (i = 0; i < num_packed; ++i, ++out_arg)
2750 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2752 else
2754 TREE_VEC_ELT (result_args, out_arg) = arg;
2755 ++out_arg;
2759 return result_args;
2762 /* Complain if DECL shadows a template parameter.
2764 [temp.local]: A template-parameter shall not be redeclared within its
2765 scope (including nested scopes). */
2767 void
2768 check_template_shadow (tree decl)
2770 tree olddecl;
2772 /* If we're not in a template, we can't possibly shadow a template
2773 parameter. */
2774 if (!current_template_parms)
2775 return;
2777 /* Figure out what we're shadowing. */
2778 if (TREE_CODE (decl) == OVERLOAD)
2779 decl = OVL_CURRENT (decl);
2780 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2782 /* If there's no previous binding for this name, we're not shadowing
2783 anything, let alone a template parameter. */
2784 if (!olddecl)
2785 return;
2787 /* If we're not shadowing a template parameter, we're done. Note
2788 that OLDDECL might be an OVERLOAD (or perhaps even an
2789 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2790 node. */
2791 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2792 return;
2794 /* We check for decl != olddecl to avoid bogus errors for using a
2795 name inside a class. We check TPFI to avoid duplicate errors for
2796 inline member templates. */
2797 if (decl == olddecl
2798 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2799 return;
2801 error ("declaration of %q+#D", decl);
2802 error (" shadows template parm %q+#D", olddecl);
2805 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2806 ORIG_LEVEL, DECL, and TYPE. */
2808 static tree
2809 build_template_parm_index (int index,
2810 int level,
2811 int orig_level,
2812 tree decl,
2813 tree type)
2815 tree t = make_node (TEMPLATE_PARM_INDEX);
2816 TEMPLATE_PARM_IDX (t) = index;
2817 TEMPLATE_PARM_LEVEL (t) = level;
2818 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2819 TEMPLATE_PARM_DECL (t) = decl;
2820 TREE_TYPE (t) = type;
2821 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2822 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2823 TREE_READONLY (t) = TREE_READONLY (decl);
2825 return t;
2828 /* Find the canonical type parameter for the given template type
2829 parameter. Returns the canonical type parameter, which may be TYPE
2830 if no such parameter existed. */
2831 static tree
2832 canonical_type_parameter (tree type)
2834 tree list;
2835 int idx = TEMPLATE_TYPE_IDX (type);
2836 if (!canonical_template_parms)
2837 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2839 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2840 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2842 list = VEC_index (tree, canonical_template_parms, idx);
2843 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2844 list = TREE_CHAIN (list);
2846 if (list)
2847 return TREE_VALUE (list);
2848 else
2850 VEC_replace(tree, canonical_template_parms, idx,
2851 tree_cons (NULL_TREE, type,
2852 VEC_index (tree, canonical_template_parms, idx)));
2853 return type;
2857 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2858 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2859 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2860 new one is created. */
2862 static tree
2863 reduce_template_parm_level (tree index, tree type, int levels)
2865 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2866 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2867 != TEMPLATE_PARM_LEVEL (index) - levels))
2869 tree orig_decl = TEMPLATE_PARM_DECL (index);
2870 tree decl, t;
2872 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2873 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2874 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2875 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2876 DECL_ARTIFICIAL (decl) = 1;
2877 SET_DECL_TEMPLATE_PARM_P (decl);
2879 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2880 TEMPLATE_PARM_LEVEL (index) - levels,
2881 TEMPLATE_PARM_ORIG_LEVEL (index),
2882 decl, type);
2883 TEMPLATE_PARM_DESCENDANTS (index) = t;
2884 TEMPLATE_PARM_PARAMETER_PACK (t)
2885 = TEMPLATE_PARM_PARAMETER_PACK (index);
2887 /* Template template parameters need this. */
2888 if (TREE_CODE (decl) != CONST_DECL)
2889 DECL_TEMPLATE_PARMS (decl)
2890 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2893 return TEMPLATE_PARM_DESCENDANTS (index);
2896 /* Process information from new template parameter PARM and append it to the
2897 LIST being built. This new parameter is a non-type parameter iff
2898 IS_NON_TYPE is true. This new parameter is a parameter
2899 pack iff IS_PARAMETER_PACK is true. */
2901 tree
2902 process_template_parm (tree list, tree parm, bool is_non_type,
2903 bool is_parameter_pack)
2905 tree decl = 0;
2906 tree defval;
2907 tree err_parm_list;
2908 int idx = 0;
2910 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2911 defval = TREE_PURPOSE (parm);
2913 if (list)
2915 tree p = tree_last (list);
2917 if (p && TREE_VALUE (p) != error_mark_node)
2919 p = TREE_VALUE (p);
2920 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2921 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2922 else
2923 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2926 ++idx;
2928 else
2929 idx = 0;
2931 if (is_non_type)
2933 parm = TREE_VALUE (parm);
2935 SET_DECL_TEMPLATE_PARM_P (parm);
2937 if (TREE_TYPE (parm) == error_mark_node)
2939 err_parm_list = build_tree_list (defval, parm);
2940 TREE_VALUE (err_parm_list) = error_mark_node;
2941 return chainon (list, err_parm_list);
2943 else
2945 /* [temp.param]
2947 The top-level cv-qualifiers on the template-parameter are
2948 ignored when determining its type. */
2949 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2950 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2952 err_parm_list = build_tree_list (defval, parm);
2953 TREE_VALUE (err_parm_list) = error_mark_node;
2954 return chainon (list, err_parm_list);
2957 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
2959 /* This template parameter is not a parameter pack, but it
2960 should be. Complain about "bare" parameter packs. */
2961 check_for_bare_parameter_packs (TREE_TYPE (parm));
2963 /* Recover by calling this a parameter pack. */
2964 is_parameter_pack = true;
2968 /* A template parameter is not modifiable. */
2969 TREE_CONSTANT (parm) = 1;
2970 TREE_INVARIANT (parm) = 1;
2971 TREE_READONLY (parm) = 1;
2972 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2973 TREE_CONSTANT (decl) = 1;
2974 TREE_INVARIANT (decl) = 1;
2975 TREE_READONLY (decl) = 1;
2976 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2977 = build_template_parm_index (idx, processing_template_decl,
2978 processing_template_decl,
2979 decl, TREE_TYPE (parm));
2981 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
2982 = is_parameter_pack;
2984 else
2986 tree t;
2987 parm = TREE_VALUE (TREE_VALUE (parm));
2989 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2991 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2992 /* This is for distinguishing between real templates and template
2993 template parameters */
2994 TREE_TYPE (parm) = t;
2995 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2996 decl = parm;
2998 else
3000 t = make_aggr_type (TEMPLATE_TYPE_PARM);
3001 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3002 decl = build_decl (TYPE_DECL, parm, t);
3005 TYPE_NAME (t) = decl;
3006 TYPE_STUB_DECL (t) = decl;
3007 parm = decl;
3008 TEMPLATE_TYPE_PARM_INDEX (t)
3009 = build_template_parm_index (idx, processing_template_decl,
3010 processing_template_decl,
3011 decl, TREE_TYPE (parm));
3012 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3013 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3015 DECL_ARTIFICIAL (decl) = 1;
3016 SET_DECL_TEMPLATE_PARM_P (decl);
3017 pushdecl (decl);
3018 parm = build_tree_list (defval, parm);
3019 return chainon (list, parm);
3022 /* The end of a template parameter list has been reached. Process the
3023 tree list into a parameter vector, converting each parameter into a more
3024 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3025 as PARM_DECLs. */
3027 tree
3028 end_template_parm_list (tree parms)
3030 int nparms;
3031 tree parm, next;
3032 tree saved_parmlist = make_tree_vec (list_length (parms));
3034 current_template_parms
3035 = tree_cons (size_int (processing_template_decl),
3036 saved_parmlist, current_template_parms);
3038 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3040 next = TREE_CHAIN (parm);
3041 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3042 TREE_CHAIN (parm) = NULL_TREE;
3045 --processing_template_parmlist;
3047 return saved_parmlist;
3050 /* end_template_decl is called after a template declaration is seen. */
3052 void
3053 end_template_decl (void)
3055 reset_specialization ();
3057 if (! processing_template_decl)
3058 return;
3060 /* This matches the pushlevel in begin_template_parm_list. */
3061 finish_scope ();
3063 --processing_template_decl;
3064 current_template_parms = TREE_CHAIN (current_template_parms);
3067 /* Within the declaration of a template, return all levels of template
3068 parameters that apply. The template parameters are represented as
3069 a TREE_VEC, in the form documented in cp-tree.h for template
3070 arguments. */
3072 static tree
3073 current_template_args (void)
3075 tree header;
3076 tree args = NULL_TREE;
3077 int length = TMPL_PARMS_DEPTH (current_template_parms);
3078 int l = length;
3080 /* If there is only one level of template parameters, we do not
3081 create a TREE_VEC of TREE_VECs. Instead, we return a single
3082 TREE_VEC containing the arguments. */
3083 if (length > 1)
3084 args = make_tree_vec (length);
3086 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3088 tree a = copy_node (TREE_VALUE (header));
3089 int i;
3091 TREE_TYPE (a) = NULL_TREE;
3092 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3094 tree t = TREE_VEC_ELT (a, i);
3096 /* T will be a list if we are called from within a
3097 begin/end_template_parm_list pair, but a vector directly
3098 if within a begin/end_member_template_processing pair. */
3099 if (TREE_CODE (t) == TREE_LIST)
3101 t = TREE_VALUE (t);
3103 if (t != error_mark_node)
3105 if (TREE_CODE (t) == TYPE_DECL
3106 || TREE_CODE (t) == TEMPLATE_DECL)
3108 t = TREE_TYPE (t);
3110 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3112 /* Turn this argument into a TYPE_ARGUMENT_PACK
3113 with a single element, which expands T. */
3114 tree vec = make_tree_vec (1);
3115 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3117 t = make_node (TYPE_ARGUMENT_PACK);
3118 SET_ARGUMENT_PACK_ARGS (t, vec);
3121 else
3123 t = DECL_INITIAL (t);
3125 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3127 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3128 with a single element, which expands T. */
3129 tree vec = make_tree_vec (1);
3130 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3131 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3133 t = make_node (NONTYPE_ARGUMENT_PACK);
3134 SET_ARGUMENT_PACK_ARGS (t, vec);
3135 TREE_TYPE (t) = type;
3139 TREE_VEC_ELT (a, i) = t;
3143 if (length > 1)
3144 TREE_VEC_ELT (args, --l) = a;
3145 else
3146 args = a;
3149 return args;
3152 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3153 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3154 a member template. Used by push_template_decl below. */
3156 static tree
3157 build_template_decl (tree decl, tree parms, bool member_template_p)
3159 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3160 DECL_TEMPLATE_PARMS (tmpl) = parms;
3161 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3162 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3163 if (DECL_LANG_SPECIFIC (decl))
3165 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3166 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3167 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3168 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3169 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3170 if (DECL_OVERLOADED_OPERATOR_P (decl))
3171 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3172 DECL_OVERLOADED_OPERATOR_P (decl));
3175 return tmpl;
3178 struct template_parm_data
3180 /* The level of the template parameters we are currently
3181 processing. */
3182 int level;
3184 /* The index of the specialization argument we are currently
3185 processing. */
3186 int current_arg;
3188 /* An array whose size is the number of template parameters. The
3189 elements are nonzero if the parameter has been used in any one
3190 of the arguments processed so far. */
3191 int* parms;
3193 /* An array whose size is the number of template arguments. The
3194 elements are nonzero if the argument makes use of template
3195 parameters of this level. */
3196 int* arg_uses_template_parms;
3199 /* Subroutine of push_template_decl used to see if each template
3200 parameter in a partial specialization is used in the explicit
3201 argument list. If T is of the LEVEL given in DATA (which is
3202 treated as a template_parm_data*), then DATA->PARMS is marked
3203 appropriately. */
3205 static int
3206 mark_template_parm (tree t, void* data)
3208 int level;
3209 int idx;
3210 struct template_parm_data* tpd = (struct template_parm_data*) data;
3212 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3214 level = TEMPLATE_PARM_LEVEL (t);
3215 idx = TEMPLATE_PARM_IDX (t);
3217 else
3219 level = TEMPLATE_TYPE_LEVEL (t);
3220 idx = TEMPLATE_TYPE_IDX (t);
3223 if (level == tpd->level)
3225 tpd->parms[idx] = 1;
3226 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3229 /* Return zero so that for_each_template_parm will continue the
3230 traversal of the tree; we want to mark *every* template parm. */
3231 return 0;
3234 /* Process the partial specialization DECL. */
3236 static tree
3237 process_partial_specialization (tree decl)
3239 tree type = TREE_TYPE (decl);
3240 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3241 tree specargs = CLASSTYPE_TI_ARGS (type);
3242 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3243 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3244 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3245 int nargs = TREE_VEC_LENGTH (inner_args);
3246 int ntparms = TREE_VEC_LENGTH (inner_parms);
3247 int i;
3248 int did_error_intro = 0;
3249 struct template_parm_data tpd;
3250 struct template_parm_data tpd2;
3252 /* We check that each of the template parameters given in the
3253 partial specialization is used in the argument list to the
3254 specialization. For example:
3256 template <class T> struct S;
3257 template <class T> struct S<T*>;
3259 The second declaration is OK because `T*' uses the template
3260 parameter T, whereas
3262 template <class T> struct S<int>;
3264 is no good. Even trickier is:
3266 template <class T>
3267 struct S1
3269 template <class U>
3270 struct S2;
3271 template <class U>
3272 struct S2<T>;
3275 The S2<T> declaration is actually invalid; it is a
3276 full-specialization. Of course,
3278 template <class U>
3279 struct S2<T (*)(U)>;
3281 or some such would have been OK. */
3282 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3283 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3284 memset (tpd.parms, 0, sizeof (int) * ntparms);
3286 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3287 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3288 for (i = 0; i < nargs; ++i)
3290 tpd.current_arg = i;
3291 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3292 &mark_template_parm,
3293 &tpd,
3294 NULL);
3296 for (i = 0; i < ntparms; ++i)
3297 if (tpd.parms[i] == 0)
3299 /* One of the template parms was not used in the
3300 specialization. */
3301 if (!did_error_intro)
3303 error ("template parameters not used in partial specialization:");
3304 did_error_intro = 1;
3307 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3310 /* [temp.class.spec]
3312 The argument list of the specialization shall not be identical to
3313 the implicit argument list of the primary template. */
3314 if (comp_template_args
3315 (inner_args,
3316 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3317 (maintmpl)))))
3318 error ("partial specialization %qT does not specialize any template arguments", type);
3320 /* [temp.class.spec]
3322 A partially specialized non-type argument expression shall not
3323 involve template parameters of the partial specialization except
3324 when the argument expression is a simple identifier.
3326 The type of a template parameter corresponding to a specialized
3327 non-type argument shall not be dependent on a parameter of the
3328 specialization.
3330 Also, we verify that pack expansions only occur at the
3331 end of the argument list. */
3332 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3333 tpd2.parms = 0;
3334 for (i = 0; i < nargs; ++i)
3336 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3337 tree arg = TREE_VEC_ELT (inner_args, i);
3338 tree packed_args = NULL_TREE;
3339 int j, len = 1;
3341 if (ARGUMENT_PACK_P (arg))
3343 /* Extract the arguments from the argument pack. We'll be
3344 iterating over these in the following loop. */
3345 packed_args = ARGUMENT_PACK_ARGS (arg);
3346 len = TREE_VEC_LENGTH (packed_args);
3349 for (j = 0; j < len; j++)
3351 if (packed_args)
3352 /* Get the Jth argument in the parameter pack. */
3353 arg = TREE_VEC_ELT (packed_args, j);
3355 if (PACK_EXPANSION_P (arg))
3357 /* Pack expansions must come at the end of the
3358 argument list. */
3359 if ((packed_args && j < len - 1)
3360 || (!packed_args && i < nargs - 1))
3362 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3363 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3364 else
3365 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3369 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3370 /* We only care about the pattern. */
3371 arg = PACK_EXPANSION_PATTERN (arg);
3373 if (/* These first two lines are the `non-type' bit. */
3374 !TYPE_P (arg)
3375 && TREE_CODE (arg) != TEMPLATE_DECL
3376 /* This next line is the `argument expression is not just a
3377 simple identifier' condition and also the `specialized
3378 non-type argument' bit. */
3379 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3381 if ((!packed_args && tpd.arg_uses_template_parms[i])
3382 || (packed_args && uses_template_parms (arg)))
3383 error ("template argument %qE involves template parameter(s)",
3384 arg);
3385 else
3387 /* Look at the corresponding template parameter,
3388 marking which template parameters its type depends
3389 upon. */
3390 tree type = TREE_TYPE (parm);
3392 if (!tpd2.parms)
3394 /* We haven't yet initialized TPD2. Do so now. */
3395 tpd2.arg_uses_template_parms
3396 = (int *) alloca (sizeof (int) * nargs);
3397 /* The number of parameters here is the number in the
3398 main template, which, as checked in the assertion
3399 above, is NARGS. */
3400 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3401 tpd2.level =
3402 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3405 /* Mark the template parameters. But this time, we're
3406 looking for the template parameters of the main
3407 template, not in the specialization. */
3408 tpd2.current_arg = i;
3409 tpd2.arg_uses_template_parms[i] = 0;
3410 memset (tpd2.parms, 0, sizeof (int) * nargs);
3411 for_each_template_parm (type,
3412 &mark_template_parm,
3413 &tpd2,
3414 NULL);
3416 if (tpd2.arg_uses_template_parms [i])
3418 /* The type depended on some template parameters.
3419 If they are fully specialized in the
3420 specialization, that's OK. */
3421 int j;
3422 for (j = 0; j < nargs; ++j)
3423 if (tpd2.parms[j] != 0
3424 && tpd.arg_uses_template_parms [j])
3426 error ("type %qT of template argument %qE depends "
3427 "on template parameter(s)",
3428 type,
3429 arg);
3430 break;
3438 if (retrieve_specialization (maintmpl, specargs,
3439 /*class_specializations_p=*/true))
3440 /* We've already got this specialization. */
3441 return decl;
3443 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3444 = tree_cons (specargs, inner_parms,
3445 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3446 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3447 return decl;
3450 /* Check that a template declaration's use of default arguments is not
3451 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
3452 nonzero if DECL is the thing declared by a primary template.
3453 IS_PARTIAL is nonzero if DECL is a partial specialization.
3456 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3457 declaration (but not a definition); 1 indicates a declaration, 2
3458 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3459 emitted for extraneous default arguments.
3461 Returns TRUE if there were no errors found, FALSE otherwise. */
3463 bool
3464 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3465 int is_partial, int is_friend_decl)
3467 const char *msg;
3468 int last_level_to_check;
3469 tree parm_level;
3470 bool no_errors = true;
3472 /* [temp.param]
3474 A default template-argument shall not be specified in a
3475 function template declaration or a function template definition, nor
3476 in the template-parameter-list of the definition of a member of a
3477 class template. */
3479 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3480 /* You can't have a function template declaration in a local
3481 scope, nor you can you define a member of a class template in a
3482 local scope. */
3483 return true;
3485 if (current_class_type
3486 && !TYPE_BEING_DEFINED (current_class_type)
3487 && DECL_LANG_SPECIFIC (decl)
3488 /* If this is either a friend defined in the scope of the class
3489 or a member function. */
3490 && (DECL_FUNCTION_MEMBER_P (decl)
3491 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3492 : DECL_FRIEND_CONTEXT (decl)
3493 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3494 : false)
3495 /* And, if it was a member function, it really was defined in
3496 the scope of the class. */
3497 && (!DECL_FUNCTION_MEMBER_P (decl)
3498 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3499 /* We already checked these parameters when the template was
3500 declared, so there's no need to do it again now. This function
3501 was defined in class scope, but we're processing it's body now
3502 that the class is complete. */
3503 return true;
3505 /* Core issue 226 (C++0x only): the following only applies to class
3506 templates. */
3507 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3509 /* [temp.param]
3511 If a template-parameter has a default template-argument, all
3512 subsequent template-parameters shall have a default
3513 template-argument supplied. */
3514 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3516 tree inner_parms = TREE_VALUE (parm_level);
3517 int ntparms = TREE_VEC_LENGTH (inner_parms);
3518 int seen_def_arg_p = 0;
3519 int i;
3521 for (i = 0; i < ntparms; ++i)
3523 tree parm = TREE_VEC_ELT (inner_parms, i);
3525 if (parm == error_mark_node)
3526 continue;
3528 if (TREE_PURPOSE (parm))
3529 seen_def_arg_p = 1;
3530 else if (seen_def_arg_p)
3532 error ("no default argument for %qD", TREE_VALUE (parm));
3533 /* For better subsequent error-recovery, we indicate that
3534 there should have been a default argument. */
3535 TREE_PURPOSE (parm) = error_mark_node;
3536 no_errors = false;
3542 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3543 || is_partial
3544 || !is_primary
3545 || is_friend_decl)
3546 /* For an ordinary class template, default template arguments are
3547 allowed at the innermost level, e.g.:
3548 template <class T = int>
3549 struct S {};
3550 but, in a partial specialization, they're not allowed even
3551 there, as we have in [temp.class.spec]:
3553 The template parameter list of a specialization shall not
3554 contain default template argument values.
3556 So, for a partial specialization, or for a function template
3557 (in C++98/C++03), we look at all of them. */
3559 else
3560 /* But, for a primary class template that is not a partial
3561 specialization we look at all template parameters except the
3562 innermost ones. */
3563 parms = TREE_CHAIN (parms);
3565 /* Figure out what error message to issue. */
3566 if (is_friend_decl == 2)
3567 msg = "default template arguments may not be used in function template friend re-declaration";
3568 else if (is_friend_decl)
3569 msg = "default template arguments may not be used in function template friend declarations";
3570 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3571 msg = "default template arguments may not be used in function templates";
3572 else if (is_partial)
3573 msg = "default template arguments may not be used in partial specializations";
3574 else
3575 msg = "default argument for template parameter for class enclosing %qD";
3577 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3578 /* If we're inside a class definition, there's no need to
3579 examine the parameters to the class itself. On the one
3580 hand, they will be checked when the class is defined, and,
3581 on the other, default arguments are valid in things like:
3582 template <class T = double>
3583 struct S { template <class U> void f(U); };
3584 Here the default argument for `S' has no bearing on the
3585 declaration of `f'. */
3586 last_level_to_check = template_class_depth (current_class_type) + 1;
3587 else
3588 /* Check everything. */
3589 last_level_to_check = 0;
3591 for (parm_level = parms;
3592 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3593 parm_level = TREE_CHAIN (parm_level))
3595 tree inner_parms = TREE_VALUE (parm_level);
3596 int i;
3597 int ntparms;
3599 ntparms = TREE_VEC_LENGTH (inner_parms);
3600 for (i = 0; i < ntparms; ++i)
3602 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3603 continue;
3605 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3607 if (msg)
3609 no_errors = false;
3610 if (is_friend_decl == 2)
3611 return no_errors;
3613 error (msg, decl);
3614 msg = 0;
3617 /* Clear out the default argument so that we are not
3618 confused later. */
3619 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3623 /* At this point, if we're still interested in issuing messages,
3624 they must apply to classes surrounding the object declared. */
3625 if (msg)
3626 msg = "default argument for template parameter for class enclosing %qD";
3629 return no_errors;
3632 /* Worker for push_template_decl_real, called via
3633 for_each_template_parm. DATA is really an int, indicating the
3634 level of the parameters we are interested in. If T is a template
3635 parameter of that level, return nonzero. */
3637 static int
3638 template_parm_this_level_p (tree t, void* data)
3640 int this_level = *(int *)data;
3641 int level;
3643 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3644 level = TEMPLATE_PARM_LEVEL (t);
3645 else
3646 level = TEMPLATE_TYPE_LEVEL (t);
3647 return level == this_level;
3650 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3651 parameters given by current_template_args, or reuses a
3652 previously existing one, if appropriate. Returns the DECL, or an
3653 equivalent one, if it is replaced via a call to duplicate_decls.
3655 If IS_FRIEND is true, DECL is a friend declaration. */
3657 tree
3658 push_template_decl_real (tree decl, bool is_friend)
3660 tree tmpl;
3661 tree args;
3662 tree info;
3663 tree ctx;
3664 int primary;
3665 int is_partial;
3666 int new_template_p = 0;
3667 /* True if the template is a member template, in the sense of
3668 [temp.mem]. */
3669 bool member_template_p = false;
3671 if (decl == error_mark_node)
3672 return decl;
3674 /* See if this is a partial specialization. */
3675 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3676 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3679 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3680 is_friend = true;
3682 if (is_friend)
3683 /* For a friend, we want the context of the friend function, not
3684 the type of which it is a friend. */
3685 ctx = DECL_CONTEXT (decl);
3686 else if (CP_DECL_CONTEXT (decl)
3687 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3688 /* In the case of a virtual function, we want the class in which
3689 it is defined. */
3690 ctx = CP_DECL_CONTEXT (decl);
3691 else
3692 /* Otherwise, if we're currently defining some class, the DECL
3693 is assumed to be a member of the class. */
3694 ctx = current_scope ();
3696 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3697 ctx = NULL_TREE;
3699 if (!DECL_CONTEXT (decl))
3700 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3702 /* See if this is a primary template. */
3703 if (is_friend && ctx)
3704 /* A friend template that specifies a class context, i.e.
3705 template <typename T> friend void A<T>::f();
3706 is not primary. */
3707 primary = 0;
3708 else
3709 primary = template_parm_scope_p ();
3711 if (primary)
3713 if (DECL_CLASS_SCOPE_P (decl))
3714 member_template_p = true;
3715 if (TREE_CODE (decl) == TYPE_DECL
3716 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3717 error ("template class without a name");
3718 else if (TREE_CODE (decl) == FUNCTION_DECL)
3720 if (DECL_DESTRUCTOR_P (decl))
3722 /* [temp.mem]
3724 A destructor shall not be a member template. */
3725 error ("destructor %qD declared as member template", decl);
3726 return error_mark_node;
3728 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3729 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3730 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3731 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3732 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3733 == void_list_node)))
3735 /* [basic.stc.dynamic.allocation]
3737 An allocation function can be a function
3738 template. ... Template allocation functions shall
3739 have two or more parameters. */
3740 error ("invalid template declaration of %qD", decl);
3741 return error_mark_node;
3744 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3745 && CLASS_TYPE_P (TREE_TYPE (decl)))
3746 /* OK */;
3747 else
3749 error ("template declaration of %q#D", decl);
3750 return error_mark_node;
3754 /* Check to see that the rules regarding the use of default
3755 arguments are not being violated. */
3756 check_default_tmpl_args (decl, current_template_parms,
3757 primary, is_partial, /*is_friend_decl=*/0);
3759 /* Ensure that there are no parameter packs in the type of this
3760 declaration that have not been expanded. */
3761 if (TREE_CODE (decl) == FUNCTION_DECL)
3763 /* Check each of the arguments individually to see if there are
3764 any bare parameter packs. */
3765 tree type = TREE_TYPE (decl);
3766 tree arg = DECL_ARGUMENTS (decl);
3767 tree argtype = TYPE_ARG_TYPES (type);
3769 while (arg && argtype)
3771 if (!FUNCTION_PARAMETER_PACK_P (arg)
3772 && !check_for_bare_parameter_packs (TREE_TYPE (arg)))
3774 /* This is a PARM_DECL that contains unexpanded parameter
3775 packs. We have already complained about this in the
3776 check_for_bare_parameter_packs call, so just replace
3777 these types with ERROR_MARK_NODE. */
3778 TREE_TYPE (arg) = error_mark_node;
3779 TREE_VALUE (argtype) = error_mark_node;
3782 arg = TREE_CHAIN (arg);
3783 argtype = TREE_CHAIN (argtype);
3786 /* Check for bare parameter packs in the return type and the
3787 exception specifiers. */
3788 check_for_bare_parameter_packs (TREE_TYPE (type));
3789 check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type));
3791 else
3792 check_for_bare_parameter_packs (TREE_TYPE (decl));
3794 if (is_partial)
3795 return process_partial_specialization (decl);
3797 /* A primary class template can only have one parameter pack, at the
3798 end of the template parameter list. */
3799 if (primary && TREE_CODE (decl) == TYPE_DECL)
3801 tree inner_parms
3802 = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3803 int i, len = TREE_VEC_LENGTH (inner_parms);
3804 for (i = 0; i < len - 1; i++)
3806 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
3808 if (template_parameter_pack_p (parm))
3810 if (TREE_CODE (parm) == PARM_DECL)
3811 error ("parameter pack %qE must be at the end of the"
3812 " template parameter list", parm);
3813 else
3814 error ("parameter pack %qT must be at the end of the"
3815 " template parameter list", TREE_TYPE (parm));
3820 args = current_template_args ();
3822 if (!ctx
3823 || TREE_CODE (ctx) == FUNCTION_DECL
3824 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3825 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3827 if (DECL_LANG_SPECIFIC (decl)
3828 && DECL_TEMPLATE_INFO (decl)
3829 && DECL_TI_TEMPLATE (decl))
3830 tmpl = DECL_TI_TEMPLATE (decl);
3831 /* If DECL is a TYPE_DECL for a class-template, then there won't
3832 be DECL_LANG_SPECIFIC. The information equivalent to
3833 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3834 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3835 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3836 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3838 /* Since a template declaration already existed for this
3839 class-type, we must be redeclaring it here. Make sure
3840 that the redeclaration is valid. */
3841 redeclare_class_template (TREE_TYPE (decl),
3842 current_template_parms);
3843 /* We don't need to create a new TEMPLATE_DECL; just use the
3844 one we already had. */
3845 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3847 else
3849 tmpl = build_template_decl (decl, current_template_parms,
3850 member_template_p);
3851 new_template_p = 1;
3853 if (DECL_LANG_SPECIFIC (decl)
3854 && DECL_TEMPLATE_SPECIALIZATION (decl))
3856 /* A specialization of a member template of a template
3857 class. */
3858 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3859 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3860 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3864 else
3866 tree a, t, current, parms;
3867 int i;
3869 if (TREE_CODE (decl) == TYPE_DECL)
3871 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3872 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3873 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3874 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3875 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3876 else
3878 error ("%qD does not declare a template type", decl);
3879 return decl;
3882 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3884 error ("template definition of non-template %q#D", decl);
3885 return decl;
3887 else
3888 tmpl = DECL_TI_TEMPLATE (decl);
3890 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3891 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3892 && DECL_TEMPLATE_SPECIALIZATION (decl)
3893 && DECL_MEMBER_TEMPLATE_P (tmpl))
3895 tree new_tmpl;
3897 /* The declaration is a specialization of a member
3898 template, declared outside the class. Therefore, the
3899 innermost template arguments will be NULL, so we
3900 replace them with the arguments determined by the
3901 earlier call to check_explicit_specialization. */
3902 args = DECL_TI_ARGS (decl);
3904 new_tmpl
3905 = build_template_decl (decl, current_template_parms,
3906 member_template_p);
3907 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3908 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3909 DECL_TI_TEMPLATE (decl) = new_tmpl;
3910 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3911 DECL_TEMPLATE_INFO (new_tmpl)
3912 = tree_cons (tmpl, args, NULL_TREE);
3914 register_specialization (new_tmpl,
3915 most_general_template (tmpl),
3916 args,
3917 is_friend);
3918 return decl;
3921 /* Make sure the template headers we got make sense. */
3923 parms = DECL_TEMPLATE_PARMS (tmpl);
3924 i = TMPL_PARMS_DEPTH (parms);
3925 if (TMPL_ARGS_DEPTH (args) != i)
3927 error ("expected %d levels of template parms for %q#D, got %d",
3928 i, decl, TMPL_ARGS_DEPTH (args));
3930 else
3931 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3933 a = TMPL_ARGS_LEVEL (args, i);
3934 t = INNERMOST_TEMPLATE_PARMS (parms);
3936 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3938 if (current == decl)
3939 error ("got %d template parameters for %q#D",
3940 TREE_VEC_LENGTH (a), decl);
3941 else
3942 error ("got %d template parameters for %q#T",
3943 TREE_VEC_LENGTH (a), current);
3944 error (" but %d required", TREE_VEC_LENGTH (t));
3945 return error_mark_node;
3948 /* Perhaps we should also check that the parms are used in the
3949 appropriate qualifying scopes in the declarator? */
3951 if (current == decl)
3952 current = ctx;
3953 else
3954 current = (TYPE_P (current)
3955 ? TYPE_CONTEXT (current)
3956 : DECL_CONTEXT (current));
3960 DECL_TEMPLATE_RESULT (tmpl) = decl;
3961 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3963 /* Push template declarations for global functions and types. Note
3964 that we do not try to push a global template friend declared in a
3965 template class; such a thing may well depend on the template
3966 parameters of the class. */
3967 if (new_template_p && !ctx
3968 && !(is_friend && template_class_depth (current_class_type) > 0))
3970 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3971 if (tmpl == error_mark_node)
3972 return error_mark_node;
3974 /* Hide template friend classes that haven't been declared yet. */
3975 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3977 DECL_ANTICIPATED (tmpl) = 1;
3978 DECL_FRIEND_P (tmpl) = 1;
3982 if (primary)
3984 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3985 if (DECL_CONV_FN_P (tmpl))
3987 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3989 /* It is a conversion operator. See if the type converted to
3990 depends on innermost template operands. */
3992 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3993 depth))
3994 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3998 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3999 back to its most general template. If TMPL is a specialization,
4000 ARGS may only have the innermost set of arguments. Add the missing
4001 argument levels if necessary. */
4002 if (DECL_TEMPLATE_INFO (tmpl))
4003 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4005 info = tree_cons (tmpl, args, NULL_TREE);
4007 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4008 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4009 else if (DECL_LANG_SPECIFIC (decl))
4010 DECL_TEMPLATE_INFO (decl) = info;
4012 return DECL_TEMPLATE_RESULT (tmpl);
4015 tree
4016 push_template_decl (tree decl)
4018 return push_template_decl_real (decl, false);
4021 /* Called when a class template TYPE is redeclared with the indicated
4022 template PARMS, e.g.:
4024 template <class T> struct S;
4025 template <class T> struct S {}; */
4027 bool
4028 redeclare_class_template (tree type, tree parms)
4030 tree tmpl;
4031 tree tmpl_parms;
4032 int i;
4034 if (!TYPE_TEMPLATE_INFO (type))
4036 error ("%qT is not a template type", type);
4037 return false;
4040 tmpl = TYPE_TI_TEMPLATE (type);
4041 if (!PRIMARY_TEMPLATE_P (tmpl))
4042 /* The type is nested in some template class. Nothing to worry
4043 about here; there are no new template parameters for the nested
4044 type. */
4045 return true;
4047 if (!parms)
4049 error ("template specifiers not specified in declaration of %qD",
4050 tmpl);
4051 return false;
4054 parms = INNERMOST_TEMPLATE_PARMS (parms);
4055 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4057 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4059 error ("previous declaration %q+D", tmpl);
4060 error ("used %d template parameter(s) instead of %d",
4061 TREE_VEC_LENGTH (tmpl_parms),
4062 TREE_VEC_LENGTH (parms));
4063 return false;
4066 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4068 tree tmpl_parm;
4069 tree parm;
4070 tree tmpl_default;
4071 tree parm_default;
4073 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4074 || TREE_VEC_ELT (parms, i) == error_mark_node)
4075 continue;
4077 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4078 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4079 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4080 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4082 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4083 TEMPLATE_DECL. */
4084 if (tmpl_parm != error_mark_node
4085 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4086 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4087 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
4089 error ("template parameter %q+#D", tmpl_parm);
4090 error ("redeclared here as %q#D", parm);
4091 return false;
4094 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4096 /* We have in [temp.param]:
4098 A template-parameter may not be given default arguments
4099 by two different declarations in the same scope. */
4100 error ("redefinition of default argument for %q#D", parm);
4101 error ("%J original definition appeared here", tmpl_parm);
4102 return false;
4105 if (parm_default != NULL_TREE)
4106 /* Update the previous template parameters (which are the ones
4107 that will really count) with the new default value. */
4108 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4109 else if (tmpl_default != NULL_TREE)
4110 /* Update the new parameters, too; they'll be used as the
4111 parameters for any members. */
4112 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4115 return true;
4118 /* Simplify EXPR if it is a non-dependent expression. Returns the
4119 (possibly simplified) expression. */
4121 tree
4122 fold_non_dependent_expr (tree expr)
4124 if (expr == NULL_TREE)
4125 return NULL_TREE;
4127 /* If we're in a template, but EXPR isn't value dependent, simplify
4128 it. We're supposed to treat:
4130 template <typename T> void f(T[1 + 1]);
4131 template <typename T> void f(T[2]);
4133 as two declarations of the same function, for example. */
4134 if (processing_template_decl
4135 && !type_dependent_expression_p (expr)
4136 && !value_dependent_expression_p (expr))
4138 HOST_WIDE_INT saved_processing_template_decl;
4140 saved_processing_template_decl = processing_template_decl;
4141 processing_template_decl = 0;
4142 expr = tsubst_copy_and_build (expr,
4143 /*args=*/NULL_TREE,
4144 tf_error,
4145 /*in_decl=*/NULL_TREE,
4146 /*function_p=*/false,
4147 /*integral_constant_expression_p=*/true);
4148 processing_template_decl = saved_processing_template_decl;
4150 return expr;
4153 /* EXPR is an expression which is used in a constant-expression context.
4154 For instance, it could be a VAR_DECL with a constant initializer.
4155 Extract the innest constant expression.
4157 This is basically a more powerful version of
4158 integral_constant_value, which can be used also in templates where
4159 initializers can maintain a syntactic rather than semantic form
4160 (even if they are non-dependent, for access-checking purposes). */
4162 static tree
4163 fold_decl_constant_value (tree expr)
4165 tree const_expr = expr;
4168 expr = fold_non_dependent_expr (const_expr);
4169 const_expr = integral_constant_value (expr);
4171 while (expr != const_expr);
4173 return expr;
4176 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4177 must be a function or a pointer-to-function type, as specified
4178 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4179 and check that the resulting function has external linkage. */
4181 static tree
4182 convert_nontype_argument_function (tree type, tree expr)
4184 tree fns = expr;
4185 tree fn, fn_no_ptr;
4187 fn = instantiate_type (type, fns, tf_none);
4188 if (fn == error_mark_node)
4189 return error_mark_node;
4191 fn_no_ptr = fn;
4192 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4193 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4194 if (TREE_CODE (fn_no_ptr) == BASELINK)
4195 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4197 /* [temp.arg.nontype]/1
4199 A template-argument for a non-type, non-template template-parameter
4200 shall be one of:
4201 [...]
4202 -- the address of an object or function with external linkage. */
4203 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4205 error ("%qE is not a valid template argument for type %qT "
4206 "because function %qD has not external linkage",
4207 expr, type, fn_no_ptr);
4208 return NULL_TREE;
4211 return fn;
4214 /* Attempt to convert the non-type template parameter EXPR to the
4215 indicated TYPE. If the conversion is successful, return the
4216 converted value. If the conversion is unsuccessful, return
4217 NULL_TREE if we issued an error message, or error_mark_node if we
4218 did not. We issue error messages for out-and-out bad template
4219 parameters, but not simply because the conversion failed, since we
4220 might be just trying to do argument deduction. Both TYPE and EXPR
4221 must be non-dependent.
4223 The conversion follows the special rules described in
4224 [temp.arg.nontype], and it is much more strict than an implicit
4225 conversion.
4227 This function is called twice for each template argument (see
4228 lookup_template_class for a more accurate description of this
4229 problem). This means that we need to handle expressions which
4230 are not valid in a C++ source, but can be created from the
4231 first call (for instance, casts to perform conversions). These
4232 hacks can go away after we fix the double coercion problem. */
4234 static tree
4235 convert_nontype_argument (tree type, tree expr)
4237 tree expr_type;
4239 /* Detect immediately string literals as invalid non-type argument.
4240 This special-case is not needed for correctness (we would easily
4241 catch this later), but only to provide better diagnostic for this
4242 common user mistake. As suggested by DR 100, we do not mention
4243 linkage issues in the diagnostic as this is not the point. */
4244 if (TREE_CODE (expr) == STRING_CST)
4246 error ("%qE is not a valid template argument for type %qT "
4247 "because string literals can never be used in this context",
4248 expr, type);
4249 return NULL_TREE;
4252 /* If we are in a template, EXPR may be non-dependent, but still
4253 have a syntactic, rather than semantic, form. For example, EXPR
4254 might be a SCOPE_REF, rather than the VAR_DECL to which the
4255 SCOPE_REF refers. Preserving the qualifying scope is necessary
4256 so that access checking can be performed when the template is
4257 instantiated -- but here we need the resolved form so that we can
4258 convert the argument. */
4259 expr = fold_non_dependent_expr (expr);
4260 if (error_operand_p (expr))
4261 return error_mark_node;
4262 expr_type = TREE_TYPE (expr);
4264 /* HACK: Due to double coercion, we can get a
4265 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4266 which is the tree that we built on the first call (see
4267 below when coercing to reference to object or to reference to
4268 function). We just strip everything and get to the arg.
4269 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4270 for examples. */
4271 if (TREE_CODE (expr) == NOP_EXPR)
4273 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4275 /* ??? Maybe we could use convert_from_reference here, but we
4276 would need to relax its constraints because the NOP_EXPR
4277 could actually change the type to something more cv-qualified,
4278 and this is not folded by convert_from_reference. */
4279 tree addr = TREE_OPERAND (expr, 0);
4280 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4281 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4282 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4283 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4284 (TREE_TYPE (expr_type),
4285 TREE_TYPE (TREE_TYPE (addr))));
4287 expr = TREE_OPERAND (addr, 0);
4288 expr_type = TREE_TYPE (expr);
4291 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4292 parameter is a pointer to object, through decay and
4293 qualification conversion. Let's strip everything. */
4294 else if (TYPE_PTROBV_P (type))
4296 STRIP_NOPS (expr);
4297 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4298 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4299 /* Skip the ADDR_EXPR only if it is part of the decay for
4300 an array. Otherwise, it is part of the original argument
4301 in the source code. */
4302 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4303 expr = TREE_OPERAND (expr, 0);
4304 expr_type = TREE_TYPE (expr);
4308 /* [temp.arg.nontype]/5, bullet 1
4310 For a non-type template-parameter of integral or enumeration type,
4311 integral promotions (_conv.prom_) and integral conversions
4312 (_conv.integral_) are applied. */
4313 if (INTEGRAL_TYPE_P (type))
4315 if (!INTEGRAL_TYPE_P (expr_type))
4316 return error_mark_node;
4318 expr = fold_decl_constant_value (expr);
4319 /* Notice that there are constant expressions like '4 % 0' which
4320 do not fold into integer constants. */
4321 if (TREE_CODE (expr) != INTEGER_CST)
4323 error ("%qE is not a valid template argument for type %qT "
4324 "because it is a non-constant expression", expr, type);
4325 return NULL_TREE;
4328 /* At this point, an implicit conversion does what we want,
4329 because we already know that the expression is of integral
4330 type. */
4331 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4332 if (expr == error_mark_node)
4333 return error_mark_node;
4335 /* Conversion was allowed: fold it to a bare integer constant. */
4336 expr = fold (expr);
4338 /* [temp.arg.nontype]/5, bullet 2
4340 For a non-type template-parameter of type pointer to object,
4341 qualification conversions (_conv.qual_) and the array-to-pointer
4342 conversion (_conv.array_) are applied. */
4343 else if (TYPE_PTROBV_P (type))
4345 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4347 A template-argument for a non-type, non-template template-parameter
4348 shall be one of: [...]
4350 -- the name of a non-type template-parameter;
4351 -- the address of an object or function with external linkage, [...]
4352 expressed as "& id-expression" where the & is optional if the name
4353 refers to a function or array, or if the corresponding
4354 template-parameter is a reference.
4356 Here, we do not care about functions, as they are invalid anyway
4357 for a parameter of type pointer-to-object. */
4359 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4360 /* Non-type template parameters are OK. */
4362 else if (TREE_CODE (expr) != ADDR_EXPR
4363 && TREE_CODE (expr_type) != ARRAY_TYPE)
4365 if (TREE_CODE (expr) == VAR_DECL)
4367 error ("%qD is not a valid template argument "
4368 "because %qD is a variable, not the address of "
4369 "a variable",
4370 expr, expr);
4371 return NULL_TREE;
4373 /* Other values, like integer constants, might be valid
4374 non-type arguments of some other type. */
4375 return error_mark_node;
4377 else
4379 tree decl;
4381 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4382 ? TREE_OPERAND (expr, 0) : expr);
4383 if (TREE_CODE (decl) != VAR_DECL)
4385 error ("%qE is not a valid template argument of type %qT "
4386 "because %qE is not a variable",
4387 expr, type, decl);
4388 return NULL_TREE;
4390 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4392 error ("%qE is not a valid template argument of type %qT "
4393 "because %qD does not have external linkage",
4394 expr, type, decl);
4395 return NULL_TREE;
4399 expr = decay_conversion (expr);
4400 if (expr == error_mark_node)
4401 return error_mark_node;
4403 expr = perform_qualification_conversions (type, expr);
4404 if (expr == error_mark_node)
4405 return error_mark_node;
4407 /* [temp.arg.nontype]/5, bullet 3
4409 For a non-type template-parameter of type reference to object, no
4410 conversions apply. The type referred to by the reference may be more
4411 cv-qualified than the (otherwise identical) type of the
4412 template-argument. The template-parameter is bound directly to the
4413 template-argument, which must be an lvalue. */
4414 else if (TYPE_REF_OBJ_P (type))
4416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4417 expr_type))
4418 return error_mark_node;
4420 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4422 error ("%qE is not a valid template argument for type %qT "
4423 "because of conflicts in cv-qualification", expr, type);
4424 return NULL_TREE;
4427 if (!real_lvalue_p (expr))
4429 error ("%qE is not a valid template argument for type %qT "
4430 "because it is not an lvalue", expr, type);
4431 return NULL_TREE;
4434 /* [temp.arg.nontype]/1
4436 A template-argument for a non-type, non-template template-parameter
4437 shall be one of: [...]
4439 -- the address of an object or function with external linkage. */
4440 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4442 error ("%qE is not a valid template argument for type %qT "
4443 "because object %qD has not external linkage",
4444 expr, type, expr);
4445 return NULL_TREE;
4448 expr = build_nop (type, build_address (expr));
4450 /* [temp.arg.nontype]/5, bullet 4
4452 For a non-type template-parameter of type pointer to function, only
4453 the function-to-pointer conversion (_conv.func_) is applied. If the
4454 template-argument represents a set of overloaded functions (or a
4455 pointer to such), the matching function is selected from the set
4456 (_over.over_). */
4457 else if (TYPE_PTRFN_P (type))
4459 /* If the argument is a template-id, we might not have enough
4460 context information to decay the pointer. */
4461 if (!type_unknown_p (expr_type))
4463 expr = decay_conversion (expr);
4464 if (expr == error_mark_node)
4465 return error_mark_node;
4468 expr = convert_nontype_argument_function (type, expr);
4469 if (!expr || expr == error_mark_node)
4470 return expr;
4472 /* [temp.arg.nontype]/5, bullet 5
4474 For a non-type template-parameter of type reference to function, no
4475 conversions apply. If the template-argument represents a set of
4476 overloaded functions, the matching function is selected from the set
4477 (_over.over_). */
4478 else if (TYPE_REFFN_P (type))
4480 if (TREE_CODE (expr) == ADDR_EXPR)
4482 error ("%qE is not a valid template argument for type %qT "
4483 "because it is a pointer", expr, type);
4484 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
4485 return NULL_TREE;
4488 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4489 if (!expr || expr == error_mark_node)
4490 return expr;
4492 expr = build_nop (type, build_address (expr));
4494 /* [temp.arg.nontype]/5, bullet 6
4496 For a non-type template-parameter of type pointer to member function,
4497 no conversions apply. If the template-argument represents a set of
4498 overloaded member functions, the matching member function is selected
4499 from the set (_over.over_). */
4500 else if (TYPE_PTRMEMFUNC_P (type))
4502 expr = instantiate_type (type, expr, tf_none);
4503 if (expr == error_mark_node)
4504 return error_mark_node;
4506 /* There is no way to disable standard conversions in
4507 resolve_address_of_overloaded_function (called by
4508 instantiate_type). It is possible that the call succeeded by
4509 converting &B::I to &D::I (where B is a base of D), so we need
4510 to reject this conversion here.
4512 Actually, even if there was a way to disable standard conversions,
4513 it would still be better to reject them here so that we can
4514 provide a superior diagnostic. */
4515 if (!same_type_p (TREE_TYPE (expr), type))
4517 /* Make sure we are just one standard conversion off. */
4518 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4519 error ("%qE is not a valid template argument for type %qT "
4520 "because it is of type %qT", expr, type,
4521 TREE_TYPE (expr));
4522 inform ("standard conversions are not allowed in this context");
4523 return NULL_TREE;
4526 /* [temp.arg.nontype]/5, bullet 7
4528 For a non-type template-parameter of type pointer to data member,
4529 qualification conversions (_conv.qual_) are applied. */
4530 else if (TYPE_PTRMEM_P (type))
4532 expr = perform_qualification_conversions (type, expr);
4533 if (expr == error_mark_node)
4534 return expr;
4536 /* A template non-type parameter must be one of the above. */
4537 else
4538 gcc_unreachable ();
4540 /* Sanity check: did we actually convert the argument to the
4541 right type? */
4542 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4543 return expr;
4547 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4548 template template parameters. Both PARM_PARMS and ARG_PARMS are
4549 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4550 or PARM_DECL.
4552 Consider the example:
4553 template <class T> class A;
4554 template<template <class U> class TT> class B;
4556 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4557 the parameters to A, and OUTER_ARGS contains A. */
4559 static int
4560 coerce_template_template_parms (tree parm_parms,
4561 tree arg_parms,
4562 tsubst_flags_t complain,
4563 tree in_decl,
4564 tree outer_args)
4566 int nparms, nargs, i;
4567 tree parm, arg;
4569 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4570 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4572 nparms = TREE_VEC_LENGTH (parm_parms);
4573 nargs = TREE_VEC_LENGTH (arg_parms);
4575 if (nargs != nparms)
4576 return 0;
4578 for (i = 0; i < nparms; ++i)
4580 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4581 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4582 continue;
4584 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4585 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4587 if (arg == NULL_TREE || arg == error_mark_node
4588 || parm == NULL_TREE || parm == error_mark_node)
4589 return 0;
4591 if (TREE_CODE (arg) != TREE_CODE (parm))
4592 return 0;
4594 switch (TREE_CODE (parm))
4596 case TEMPLATE_DECL:
4597 /* We encounter instantiations of templates like
4598 template <template <template <class> class> class TT>
4599 class C; */
4601 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4602 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4604 if (!coerce_template_template_parms
4605 (parmparm, argparm, complain, in_decl, outer_args))
4606 return 0;
4608 /* Fall through. */
4610 case TYPE_DECL:
4611 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))
4612 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)))
4613 /* One is a parameter pack, the other is not. */
4614 return 0;
4615 break;
4617 case PARM_DECL:
4618 /* The tsubst call is used to handle cases such as
4620 template <int> class C {};
4621 template <class T, template <T> class TT> class D {};
4622 D<int, C> d;
4624 i.e. the parameter list of TT depends on earlier parameters. */
4625 if (!dependent_type_p (TREE_TYPE (arg))
4626 && !same_type_p
4627 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4628 TREE_TYPE (arg)))
4629 return 0;
4631 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4632 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)))
4633 /* One is a parameter pack, the other is not. */
4634 return 0;
4635 break;
4637 default:
4638 gcc_unreachable ();
4641 return 1;
4644 /* Convert the indicated template ARG as necessary to match the
4645 indicated template PARM. Returns the converted ARG, or
4646 error_mark_node if the conversion was unsuccessful. Error and
4647 warning messages are issued under control of COMPLAIN. This
4648 conversion is for the Ith parameter in the parameter list. ARGS is
4649 the full set of template arguments deduced so far. */
4651 static tree
4652 convert_template_argument (tree parm,
4653 tree arg,
4654 tree args,
4655 tsubst_flags_t complain,
4656 int i,
4657 tree in_decl)
4659 tree val;
4660 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4661 tree check_arg = arg;
4663 if (TREE_CODE (arg) == TREE_LIST
4664 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4666 /* The template argument was the name of some
4667 member function. That's usually
4668 invalid, but static members are OK. In any
4669 case, grab the underlying fields/functions
4670 and issue an error later if required. */
4671 arg = TREE_VALUE (arg);
4672 TREE_TYPE (arg) = unknown_type_node;
4675 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4676 requires_type = (TREE_CODE (parm) == TYPE_DECL
4677 || requires_tmpl_type);
4679 /* When determining whether an argument pack expansion is a template,
4680 look at the pattern. */
4681 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4682 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4684 is_tmpl_type =
4685 ((TREE_CODE (check_arg) == TEMPLATE_DECL
4686 && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg)) == TYPE_DECL)
4687 || TREE_CODE (check_arg) == TEMPLATE_TEMPLATE_PARM
4688 || TREE_CODE (check_arg) == UNBOUND_CLASS_TEMPLATE);
4690 if (is_tmpl_type
4691 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4692 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4693 arg = TYPE_STUB_DECL (arg);
4695 is_type = TYPE_P (arg) || is_tmpl_type;
4697 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4698 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4700 pedwarn ("to refer to a type member of a template parameter, "
4701 "use %<typename %E%>", arg);
4703 arg = make_typename_type (TREE_OPERAND (arg, 0),
4704 TREE_OPERAND (arg, 1),
4705 typename_type,
4706 complain & tf_error);
4707 is_type = 1;
4709 if (is_type != requires_type)
4711 if (in_decl)
4713 if (complain & tf_error)
4715 error ("type/value mismatch at argument %d in template "
4716 "parameter list for %qD",
4717 i + 1, in_decl);
4718 if (is_type)
4719 error (" expected a constant of type %qT, got %qT",
4720 TREE_TYPE (parm),
4721 (is_tmpl_type ? DECL_NAME (arg) : arg));
4722 else if (requires_tmpl_type)
4723 error (" expected a class template, got %qE", arg);
4724 else
4725 error (" expected a type, got %qE", arg);
4728 return error_mark_node;
4730 if (is_tmpl_type ^ requires_tmpl_type)
4732 if (in_decl && (complain & tf_error))
4734 error ("type/value mismatch at argument %d in template "
4735 "parameter list for %qD",
4736 i + 1, in_decl);
4737 if (is_tmpl_type)
4738 error (" expected a type, got %qT", DECL_NAME (arg));
4739 else
4740 error (" expected a class template, got %qT", arg);
4742 return error_mark_node;
4745 if (is_type)
4747 if (requires_tmpl_type)
4749 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4750 /* The number of argument required is not known yet.
4751 Just accept it for now. */
4752 val = TREE_TYPE (arg);
4753 else
4755 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4756 tree argparm;
4758 check_arg = arg;
4759 /* When determining whether a pack expansion is a template,
4760 look at the pattern. */
4761 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4762 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4764 argparm = DECL_INNERMOST_TEMPLATE_PARMS (check_arg);
4766 if (coerce_template_template_parms (parmparm, argparm,
4767 complain, in_decl,
4768 args))
4770 val = arg;
4772 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4773 TEMPLATE_DECL. */
4774 if (val != error_mark_node)
4776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4777 val = TREE_TYPE (val);
4778 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
4779 && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg))
4781 val = TREE_TYPE (check_arg);
4782 val = make_pack_expansion (val);
4786 else
4788 if (in_decl && (complain & tf_error))
4790 error ("type/value mismatch at argument %d in "
4791 "template parameter list for %qD",
4792 i + 1, in_decl);
4793 error (" expected a template of type %qD, got %qD",
4794 parm, arg);
4797 val = error_mark_node;
4801 else
4802 val = arg;
4803 /* We only form one instance of each template specialization.
4804 Therefore, if we use a non-canonical variant (i.e., a
4805 typedef), any future messages referring to the type will use
4806 the typedef, which is confusing if those future uses do not
4807 themselves also use the typedef. */
4808 if (TYPE_P (val))
4809 val = canonical_type_variant (val);
4811 else
4813 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4815 if (invalid_nontype_parm_type_p (t, complain))
4816 return error_mark_node;
4818 if (!uses_template_parms (arg) && !uses_template_parms (t))
4819 /* We used to call digest_init here. However, digest_init
4820 will report errors, which we don't want when complain
4821 is zero. More importantly, digest_init will try too
4822 hard to convert things: for example, `0' should not be
4823 converted to pointer type at this point according to
4824 the standard. Accepting this is not merely an
4825 extension, since deciding whether or not these
4826 conversions can occur is part of determining which
4827 function template to call, or whether a given explicit
4828 argument specification is valid. */
4829 val = convert_nontype_argument (t, arg);
4830 else
4831 val = arg;
4833 if (val == NULL_TREE)
4834 val = error_mark_node;
4835 else if (val == error_mark_node && (complain & tf_error))
4836 error ("could not convert template argument %qE to %qT", arg, t);
4839 return val;
4842 /* Coerces the remaining template arguments in INNER_ARGS (from
4843 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
4844 Returns the coerced argument pack. PARM_IDX is the position of this
4845 parameter in the template parameter list. ARGS is the original
4846 template argument list. */
4847 static tree
4848 coerce_template_parameter_pack (tree parms,
4849 int parm_idx,
4850 tree args,
4851 tree inner_args,
4852 int arg_idx,
4853 tree new_args,
4854 int* lost,
4855 tree in_decl,
4856 tsubst_flags_t complain)
4858 tree parm = TREE_VEC_ELT (parms, parm_idx);
4859 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4860 tree packed_args;
4861 tree argument_pack;
4862 tree packed_types = NULL_TREE;
4864 if (arg_idx > nargs)
4865 arg_idx = nargs;
4867 packed_args = make_tree_vec (nargs - arg_idx);
4869 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
4870 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
4872 /* When the template parameter is a non-type template
4873 parameter pack whose type uses parameter packs, we need
4874 to look at each of the template arguments
4875 separately. Build a vector of the types for these
4876 non-type template parameters in PACKED_TYPES. */
4877 tree expansion
4878 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
4879 packed_types = tsubst_pack_expansion (expansion, args,
4880 complain, in_decl);
4882 if (packed_types == error_mark_node)
4883 return error_mark_node;
4885 /* Check that we have the right number of arguments. */
4886 if (arg_idx < nargs
4887 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
4888 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
4890 int needed_parms
4891 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
4892 error ("wrong number of template arguments (%d, should be %d)",
4893 nargs, needed_parms);
4894 return error_mark_node;
4897 /* If we aren't able to check the actual arguments now
4898 (because they haven't been expanded yet), we can at least
4899 verify that all of the types used for the non-type
4900 template parameter pack are, in fact, valid for non-type
4901 template parameters. */
4902 if (arg_idx < nargs
4903 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
4905 int j, len = TREE_VEC_LENGTH (packed_types);
4906 for (j = 0; j < len; ++j)
4908 tree t = TREE_VEC_ELT (packed_types, j);
4909 if (invalid_nontype_parm_type_p (t, complain))
4910 return error_mark_node;
4915 /* Convert the remaining arguments, which will be a part of the
4916 parameter pack "parm". */
4917 for (; arg_idx < nargs; ++arg_idx)
4919 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
4920 tree actual_parm = TREE_VALUE (parm);
4922 if (packed_types && !PACK_EXPANSION_P (arg))
4924 /* When we have a vector of types (corresponding to the
4925 non-type template parameter pack that uses parameter
4926 packs in its type, as mention above), and the
4927 argument is not an expansion (which expands to a
4928 currently unknown number of arguments), clone the
4929 parm and give it the next type in PACKED_TYPES. */
4930 actual_parm = copy_node (actual_parm);
4931 TREE_TYPE (actual_parm) =
4932 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
4935 arg = convert_template_argument (actual_parm,
4936 arg, new_args, complain, parm_idx,
4937 in_decl);
4938 if (arg == error_mark_node)
4939 (*lost)++;
4940 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
4943 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
4944 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
4945 argument_pack = make_node (TYPE_ARGUMENT_PACK);
4946 else
4948 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
4949 TREE_TYPE (argument_pack) = TREE_TYPE (TREE_VALUE (parm));
4950 TREE_CONSTANT (argument_pack) = 1;
4953 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
4954 return argument_pack;
4957 /* Convert all template arguments to their appropriate types, and
4958 return a vector containing the innermost resulting template
4959 arguments. If any error occurs, return error_mark_node. Error and
4960 warning messages are issued under control of COMPLAIN.
4962 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4963 for arguments not specified in ARGS. Otherwise, if
4964 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4965 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4966 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4967 ARGS. */
4969 static tree
4970 coerce_template_parms (tree parms,
4971 tree args,
4972 tree in_decl,
4973 tsubst_flags_t complain,
4974 bool require_all_args,
4975 bool use_default_args)
4977 int nparms, nargs, parm_idx, arg_idx, lost = 0;
4978 tree inner_args;
4979 tree new_args;
4980 tree new_inner_args;
4981 bool saved_skip_evaluation;
4983 /* When used as a boolean value, indicates whether this is a
4984 variadic template parameter list. Since it's an int, we can also
4985 subtract it from nparms to get the number of non-variadic
4986 parameters. */
4987 int variadic_p = 0;
4989 inner_args
4990 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args));
4992 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4993 nparms = TREE_VEC_LENGTH (parms);
4995 /* Determine if there are any parameter packs. */
4996 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
4998 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
4999 if (template_parameter_pack_p (tparm))
5001 variadic_p = 1;
5002 break;
5006 if ((nargs > nparms - variadic_p && !variadic_p)
5007 || (nargs < nparms - variadic_p
5008 && require_all_args
5009 && (!use_default_args
5010 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5011 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5013 if (complain & tf_error)
5015 const char *or_more = "";
5016 if (variadic_p)
5018 or_more = " or more";
5019 --nparms;
5022 error ("wrong number of template arguments (%d, should be %d%s)",
5023 nargs, nparms, or_more);
5025 if (in_decl)
5026 error ("provided for %q+D", in_decl);
5029 return error_mark_node;
5032 /* We need to evaluate the template arguments, even though this
5033 template-id may be nested within a "sizeof". */
5034 saved_skip_evaluation = skip_evaluation;
5035 skip_evaluation = false;
5036 new_inner_args = make_tree_vec (nparms);
5037 new_args = add_outermost_template_args (args, new_inner_args);
5038 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5040 tree arg;
5041 tree parm;
5043 /* Get the Ith template parameter. */
5044 parm = TREE_VEC_ELT (parms, parm_idx);
5046 if (parm == error_mark_node)
5048 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5049 continue;
5052 /* Calculate the next argument. */
5053 if (template_parameter_pack_p (TREE_VALUE (parm)))
5055 /* All remaining arguments will be placed in the
5056 template parameter pack PARM. */
5057 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5058 inner_args, arg_idx,
5059 new_args, &lost,
5060 in_decl, complain);
5062 /* Store this argument. */
5063 if (arg == error_mark_node)
5064 lost++;
5065 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5067 /* We are done with all of the arguments. */
5068 arg_idx = nargs;
5070 continue;
5072 else if (arg_idx < nargs)
5074 arg = TREE_VEC_ELT (inner_args, arg_idx);
5076 if (arg && PACK_EXPANSION_P (arg))
5078 /* If ARG is a pack expansion, but PARM is not a
5079 template parameter pack (if it were, we would have
5080 handled it above), we're trying to expand into a
5081 fixed-length argument list. */
5082 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5083 error ("cannot expand %<%E%> into a fixed-length "
5084 "argument list", arg);
5085 else
5086 error ("cannot expand %<%T%> into a fixed-length "
5087 "argument list", arg);
5090 else if (require_all_args)
5091 /* There must be a default arg in this case. */
5092 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5093 complain, in_decl);
5094 else
5095 break;
5097 if (arg == error_mark_node)
5099 if (complain & tf_error)
5100 error ("template argument %d is invalid", arg_idx + 1);
5102 else if (!arg)
5103 /* This only occurs if there was an error in the template
5104 parameter list itself (which we would already have
5105 reported) that we are trying to recover from, e.g., a class
5106 template with a parameter list such as
5107 template<typename..., typename>. */
5108 return error_mark_node;
5109 else
5110 arg = convert_template_argument (TREE_VALUE (parm),
5111 arg, new_args, complain,
5112 parm_idx, in_decl);
5114 if (arg == error_mark_node)
5115 lost++;
5116 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5118 skip_evaluation = saved_skip_evaluation;
5120 if (lost)
5121 return error_mark_node;
5123 return new_inner_args;
5126 /* Returns 1 if template args OT and NT are equivalent. */
5128 static int
5129 template_args_equal (tree ot, tree nt)
5131 if (nt == ot)
5132 return 1;
5134 if (TREE_CODE (nt) == TREE_VEC)
5135 /* For member templates */
5136 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5137 else if (PACK_EXPANSION_P (ot))
5138 return PACK_EXPANSION_P (nt)
5139 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5140 PACK_EXPANSION_PATTERN (nt));
5141 else if (TYPE_P (nt))
5142 return TYPE_P (ot) && same_type_p (ot, nt);
5143 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5144 return 0;
5145 else
5146 return cp_tree_equal (ot, nt);
5149 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5150 of template arguments. Returns 0 otherwise. */
5153 comp_template_args (tree oldargs, tree newargs)
5155 int i;
5157 oldargs = expand_template_argument_pack (oldargs);
5158 newargs = expand_template_argument_pack (newargs);
5160 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5161 return 0;
5163 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5165 tree nt = TREE_VEC_ELT (newargs, i);
5166 tree ot = TREE_VEC_ELT (oldargs, i);
5168 if (! template_args_equal (ot, nt))
5169 return 0;
5171 return 1;
5174 static void
5175 add_pending_template (tree d)
5177 tree ti = (TYPE_P (d)
5178 ? CLASSTYPE_TEMPLATE_INFO (d)
5179 : DECL_TEMPLATE_INFO (d));
5180 struct pending_template *pt;
5181 int level;
5183 if (TI_PENDING_TEMPLATE_FLAG (ti))
5184 return;
5186 /* We are called both from instantiate_decl, where we've already had a
5187 tinst_level pushed, and instantiate_template, where we haven't.
5188 Compensate. */
5189 level = !current_tinst_level || current_tinst_level->decl != d;
5191 if (level)
5192 push_tinst_level (d);
5194 pt = GGC_NEW (struct pending_template);
5195 pt->next = NULL;
5196 pt->tinst = current_tinst_level;
5197 if (last_pending_template)
5198 last_pending_template->next = pt;
5199 else
5200 pending_templates = pt;
5202 last_pending_template = pt;
5204 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5206 if (level)
5207 pop_tinst_level ();
5211 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5212 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5213 documentation for TEMPLATE_ID_EXPR. */
5215 tree
5216 lookup_template_function (tree fns, tree arglist)
5218 tree type;
5220 if (fns == error_mark_node || arglist == error_mark_node)
5221 return error_mark_node;
5223 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5224 gcc_assert (fns && (is_overloaded_fn (fns)
5225 || TREE_CODE (fns) == IDENTIFIER_NODE));
5227 if (BASELINK_P (fns))
5229 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5230 unknown_type_node,
5231 BASELINK_FUNCTIONS (fns),
5232 arglist);
5233 return fns;
5236 type = TREE_TYPE (fns);
5237 if (TREE_CODE (fns) == OVERLOAD || !type)
5238 type = unknown_type_node;
5240 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5243 /* Within the scope of a template class S<T>, the name S gets bound
5244 (in build_self_reference) to a TYPE_DECL for the class, not a
5245 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5246 or one of its enclosing classes, and that type is a template,
5247 return the associated TEMPLATE_DECL. Otherwise, the original
5248 DECL is returned. */
5250 tree
5251 maybe_get_template_decl_from_type_decl (tree decl)
5253 return (decl != NULL_TREE
5254 && TREE_CODE (decl) == TYPE_DECL
5255 && DECL_ARTIFICIAL (decl)
5256 && CLASS_TYPE_P (TREE_TYPE (decl))
5257 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5258 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5261 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5262 parameters, find the desired type.
5264 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5266 IN_DECL, if non-NULL, is the template declaration we are trying to
5267 instantiate.
5269 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5270 the class we are looking up.
5272 Issue error and warning messages under control of COMPLAIN.
5274 If the template class is really a local class in a template
5275 function, then the FUNCTION_CONTEXT is the function in which it is
5276 being instantiated.
5278 ??? Note that this function is currently called *twice* for each
5279 template-id: the first time from the parser, while creating the
5280 incomplete type (finish_template_type), and the second type during the
5281 real instantiation (instantiate_template_class). This is surely something
5282 that we want to avoid. It also causes some problems with argument
5283 coercion (see convert_nontype_argument for more information on this). */
5285 tree
5286 lookup_template_class (tree d1,
5287 tree arglist,
5288 tree in_decl,
5289 tree context,
5290 int entering_scope,
5291 tsubst_flags_t complain)
5293 tree template = NULL_TREE, parmlist;
5294 tree t;
5296 timevar_push (TV_NAME_LOOKUP);
5298 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5300 tree value = innermost_non_namespace_value (d1);
5301 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5302 template = value;
5303 else
5305 if (context)
5306 push_decl_namespace (context);
5307 template = lookup_name (d1);
5308 template = maybe_get_template_decl_from_type_decl (template);
5309 if (context)
5310 pop_decl_namespace ();
5312 if (template)
5313 context = DECL_CONTEXT (template);
5315 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
5317 tree type = TREE_TYPE (d1);
5319 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5320 an implicit typename for the second A. Deal with it. */
5321 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5322 type = TREE_TYPE (type);
5324 if (CLASSTYPE_TEMPLATE_INFO (type))
5326 template = CLASSTYPE_TI_TEMPLATE (type);
5327 d1 = DECL_NAME (template);
5330 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5331 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5333 template = TYPE_TI_TEMPLATE (d1);
5334 d1 = DECL_NAME (template);
5336 else if (TREE_CODE (d1) == TEMPLATE_DECL
5337 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5339 template = d1;
5340 d1 = DECL_NAME (template);
5341 context = DECL_CONTEXT (template);
5344 /* Issue an error message if we didn't find a template. */
5345 if (! template)
5347 if (complain & tf_error)
5348 error ("%qT is not a template", d1);
5349 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5352 if (TREE_CODE (template) != TEMPLATE_DECL
5353 /* Make sure it's a user visible template, if it was named by
5354 the user. */
5355 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
5356 && !PRIMARY_TEMPLATE_P (template)))
5358 if (complain & tf_error)
5360 error ("non-template type %qT used as a template", d1);
5361 if (in_decl)
5362 error ("for template declaration %q+D", in_decl);
5364 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5367 complain &= ~tf_user;
5369 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5371 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5372 template arguments */
5374 tree parm;
5375 tree arglist2;
5377 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
5379 /* Consider an example where a template template parameter declared as
5381 template <class T, class U = std::allocator<T> > class TT
5383 The template parameter level of T and U are one level larger than
5384 of TT. To proper process the default argument of U, say when an
5385 instantiation `TT<int>' is seen, we need to build the full
5386 arguments containing {int} as the innermost level. Outer levels,
5387 available when not appearing as default template argument, can be
5388 obtained from `current_template_args ()'.
5390 Suppose that TT is later substituted with std::vector. The above
5391 instantiation is `TT<int, std::allocator<T> >' with TT at
5392 level 1, and T at level 2, while the template arguments at level 1
5393 becomes {std::vector} and the inner level 2 is {int}. */
5395 if (current_template_parms)
5396 arglist = add_to_template_args (current_template_args (), arglist);
5398 arglist2 = coerce_template_parms (parmlist, arglist, template,
5399 complain,
5400 /*require_all_args=*/true,
5401 /*use_default_args=*/true);
5402 if (arglist2 == error_mark_node
5403 || (!uses_template_parms (arglist2)
5404 && check_instantiated_args (template, arglist2, complain)))
5405 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5407 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
5408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5410 else
5412 tree template_type = TREE_TYPE (template);
5413 tree gen_tmpl;
5414 tree type_decl;
5415 tree found = NULL_TREE;
5416 int arg_depth;
5417 int parm_depth;
5418 int is_partial_instantiation;
5420 gen_tmpl = most_general_template (template);
5421 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5422 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5423 arg_depth = TMPL_ARGS_DEPTH (arglist);
5425 if (arg_depth == 1 && parm_depth > 1)
5427 /* We've been given an incomplete set of template arguments.
5428 For example, given:
5430 template <class T> struct S1 {
5431 template <class U> struct S2 {};
5432 template <class U> struct S2<U*> {};
5435 we will be called with an ARGLIST of `U*', but the
5436 TEMPLATE will be `template <class T> template
5437 <class U> struct S1<T>::S2'. We must fill in the missing
5438 arguments. */
5439 arglist
5440 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5441 arglist);
5442 arg_depth = TMPL_ARGS_DEPTH (arglist);
5445 /* Now we should have enough arguments. */
5446 gcc_assert (parm_depth == arg_depth);
5448 /* From here on, we're only interested in the most general
5449 template. */
5450 template = gen_tmpl;
5452 /* Calculate the BOUND_ARGS. These will be the args that are
5453 actually tsubst'd into the definition to create the
5454 instantiation. */
5455 if (parm_depth > 1)
5457 /* We have multiple levels of arguments to coerce, at once. */
5458 int i;
5459 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5461 tree bound_args = make_tree_vec (parm_depth);
5463 for (i = saved_depth,
5464 t = DECL_TEMPLATE_PARMS (template);
5465 i > 0 && t != NULL_TREE;
5466 --i, t = TREE_CHAIN (t))
5468 tree a = coerce_template_parms (TREE_VALUE (t),
5469 arglist, template,
5470 complain,
5471 /*require_all_args=*/true,
5472 /*use_default_args=*/true);
5474 /* Don't process further if one of the levels fails. */
5475 if (a == error_mark_node)
5477 /* Restore the ARGLIST to its full size. */
5478 TREE_VEC_LENGTH (arglist) = saved_depth;
5479 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5482 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5484 /* We temporarily reduce the length of the ARGLIST so
5485 that coerce_template_parms will see only the arguments
5486 corresponding to the template parameters it is
5487 examining. */
5488 TREE_VEC_LENGTH (arglist)--;
5491 /* Restore the ARGLIST to its full size. */
5492 TREE_VEC_LENGTH (arglist) = saved_depth;
5494 arglist = bound_args;
5496 else
5497 arglist
5498 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5499 INNERMOST_TEMPLATE_ARGS (arglist),
5500 template,
5501 complain,
5502 /*require_all_args=*/true,
5503 /*use_default_args=*/true);
5505 if (arglist == error_mark_node)
5506 /* We were unable to bind the arguments. */
5507 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5509 /* In the scope of a template class, explicit references to the
5510 template class refer to the type of the template, not any
5511 instantiation of it. For example, in:
5513 template <class T> class C { void f(C<T>); }
5515 the `C<T>' is just the same as `C'. Outside of the
5516 class, however, such a reference is an instantiation. */
5517 if (comp_template_args (TYPE_TI_ARGS (template_type),
5518 arglist))
5520 found = template_type;
5522 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5524 tree ctx;
5526 for (ctx = current_class_type;
5527 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5528 ctx = (TYPE_P (ctx)
5529 ? TYPE_CONTEXT (ctx)
5530 : DECL_CONTEXT (ctx)))
5531 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5532 goto found_ctx;
5534 /* We're not in the scope of the class, so the
5535 TEMPLATE_TYPE is not the type we want after all. */
5536 found = NULL_TREE;
5537 found_ctx:;
5540 if (found)
5541 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5543 /* If we already have this specialization, return it. */
5544 found = retrieve_specialization (template, arglist,
5545 /*class_specializations_p=*/false);
5546 if (found)
5547 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5549 /* This type is a "partial instantiation" if any of the template
5550 arguments still involve template parameters. Note that we set
5551 IS_PARTIAL_INSTANTIATION for partial specializations as
5552 well. */
5553 is_partial_instantiation = uses_template_parms (arglist);
5555 /* If the deduced arguments are invalid, then the binding
5556 failed. */
5557 if (!is_partial_instantiation
5558 && check_instantiated_args (template,
5559 INNERMOST_TEMPLATE_ARGS (arglist),
5560 complain))
5561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5563 if (!is_partial_instantiation
5564 && !PRIMARY_TEMPLATE_P (template)
5565 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
5567 found = xref_tag_from_type (TREE_TYPE (template),
5568 DECL_NAME (template),
5569 /*tag_scope=*/ts_global);
5570 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5573 context = tsubst (DECL_CONTEXT (template), arglist,
5574 complain, in_decl);
5575 if (!context)
5576 context = global_namespace;
5578 /* Create the type. */
5579 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5581 if (!is_partial_instantiation)
5583 set_current_access_from_decl (TYPE_NAME (template_type));
5584 t = start_enum (TYPE_IDENTIFIER (template_type));
5586 else
5587 /* We don't want to call start_enum for this type, since
5588 the values for the enumeration constants may involve
5589 template parameters. And, no one should be interested
5590 in the enumeration constants for such a type. */
5591 t = make_node (ENUMERAL_TYPE);
5593 else
5595 t = make_aggr_type (TREE_CODE (template_type));
5596 CLASSTYPE_DECLARED_CLASS (t)
5597 = CLASSTYPE_DECLARED_CLASS (template_type);
5598 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5599 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5601 /* A local class. Make sure the decl gets registered properly. */
5602 if (context == current_function_decl)
5603 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
5605 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5606 /* This instantiation is another name for the primary
5607 template type. Set the TYPE_CANONICAL field
5608 appropriately. */
5609 TYPE_CANONICAL (t) = template_type;
5610 else if (any_template_arguments_need_structural_equality_p (arglist))
5611 /* Some of the template arguments require structural
5612 equality testing, so this template class requires
5613 structural equality testing. */
5614 SET_TYPE_STRUCTURAL_EQUALITY (t);
5617 /* If we called start_enum or pushtag above, this information
5618 will already be set up. */
5619 if (!TYPE_NAME (t))
5621 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5623 type_decl = create_implicit_typedef (DECL_NAME (template), t);
5624 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5625 TYPE_STUB_DECL (t) = type_decl;
5626 DECL_SOURCE_LOCATION (type_decl)
5627 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5629 else
5630 type_decl = TYPE_NAME (t);
5632 TREE_PRIVATE (type_decl)
5633 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5634 TREE_PROTECTED (type_decl)
5635 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5636 DECL_IN_SYSTEM_HEADER (type_decl)
5637 = DECL_IN_SYSTEM_HEADER (template);
5638 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5640 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5641 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5644 /* Set up the template information. We have to figure out which
5645 template is the immediate parent if this is a full
5646 instantiation. */
5647 if (parm_depth == 1 || is_partial_instantiation
5648 || !PRIMARY_TEMPLATE_P (template))
5649 /* This case is easy; there are no member templates involved. */
5650 found = template;
5651 else
5653 /* This is a full instantiation of a member template. Look
5654 for a partial instantiation of which this is an instance. */
5656 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
5657 found; found = TREE_CHAIN (found))
5659 int success;
5660 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5662 /* We only want partial instantiations, here, not
5663 specializations or full instantiations. */
5664 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5665 || !uses_template_parms (TREE_VALUE (found)))
5666 continue;
5668 /* Temporarily reduce by one the number of levels in the
5669 ARGLIST and in FOUND so as to avoid comparing the
5670 last set of arguments. */
5671 TREE_VEC_LENGTH (arglist)--;
5672 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5674 /* See if the arguments match. If they do, then TMPL is
5675 the partial instantiation we want. */
5676 success = comp_template_args (TREE_PURPOSE (found), arglist);
5678 /* Restore the argument vectors to their full size. */
5679 TREE_VEC_LENGTH (arglist)++;
5680 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
5682 if (success)
5684 found = tmpl;
5685 break;
5689 if (!found)
5691 /* There was no partial instantiation. This happens
5692 where C<T> is a member template of A<T> and it's used
5693 in something like
5695 template <typename T> struct B { A<T>::C<int> m; };
5696 B<float>;
5698 Create the partial instantiation.
5700 TREE_VEC_LENGTH (arglist)--;
5701 found = tsubst (template, arglist, complain, NULL_TREE);
5702 TREE_VEC_LENGTH (arglist)++;
5706 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
5707 DECL_TEMPLATE_INSTANTIATIONS (template)
5708 = tree_cons (arglist, t,
5709 DECL_TEMPLATE_INSTANTIATIONS (template));
5711 if (TREE_CODE (t) == ENUMERAL_TYPE
5712 && !is_partial_instantiation)
5713 /* Now that the type has been registered on the instantiations
5714 list, we set up the enumerators. Because the enumeration
5715 constants may involve the enumeration type itself, we make
5716 sure to register the type first, and then create the
5717 constants. That way, doing tsubst_expr for the enumeration
5718 constants won't result in recursive calls here; we'll find
5719 the instantiation and exit above. */
5720 tsubst_enum (template_type, t, arglist);
5722 if (is_partial_instantiation)
5723 /* If the type makes use of template parameters, the
5724 code that generates debugging information will crash. */
5725 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
5727 /* Possibly limit visibility based on template args. */
5728 TREE_PUBLIC (type_decl) = 1;
5729 determine_visibility (type_decl);
5731 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5733 timevar_pop (TV_NAME_LOOKUP);
5736 struct pair_fn_data
5738 tree_fn_t fn;
5739 void *data;
5740 struct pointer_set_t *visited;
5743 /* Called from for_each_template_parm via walk_tree. */
5745 static tree
5746 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
5748 tree t = *tp;
5749 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
5750 tree_fn_t fn = pfd->fn;
5751 void *data = pfd->data;
5753 if (TYPE_P (t)
5754 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
5755 return error_mark_node;
5757 switch (TREE_CODE (t))
5759 case RECORD_TYPE:
5760 if (TYPE_PTRMEMFUNC_P (t))
5761 break;
5762 /* Fall through. */
5764 case UNION_TYPE:
5765 case ENUMERAL_TYPE:
5766 if (!TYPE_TEMPLATE_INFO (t))
5767 *walk_subtrees = 0;
5768 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
5769 fn, data, pfd->visited))
5770 return error_mark_node;
5771 break;
5773 case INTEGER_TYPE:
5774 if (for_each_template_parm (TYPE_MIN_VALUE (t),
5775 fn, data, pfd->visited)
5776 || for_each_template_parm (TYPE_MAX_VALUE (t),
5777 fn, data, pfd->visited))
5778 return error_mark_node;
5779 break;
5781 case METHOD_TYPE:
5782 /* Since we're not going to walk subtrees, we have to do this
5783 explicitly here. */
5784 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
5785 pfd->visited))
5786 return error_mark_node;
5787 /* Fall through. */
5789 case FUNCTION_TYPE:
5790 /* Check the return type. */
5791 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5792 return error_mark_node;
5794 /* Check the parameter types. Since default arguments are not
5795 instantiated until they are needed, the TYPE_ARG_TYPES may
5796 contain expressions that involve template parameters. But,
5797 no-one should be looking at them yet. And, once they're
5798 instantiated, they don't contain template parameters, so
5799 there's no point in looking at them then, either. */
5801 tree parm;
5803 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5804 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5805 pfd->visited))
5806 return error_mark_node;
5808 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5809 want walk_tree walking into them itself. */
5810 *walk_subtrees = 0;
5812 break;
5814 case TYPEOF_TYPE:
5815 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5816 pfd->visited))
5817 return error_mark_node;
5818 break;
5820 case FUNCTION_DECL:
5821 case VAR_DECL:
5822 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5823 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5824 pfd->visited))
5825 return error_mark_node;
5826 /* Fall through. */
5828 case PARM_DECL:
5829 case CONST_DECL:
5830 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5831 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5832 pfd->visited))
5833 return error_mark_node;
5834 if (DECL_CONTEXT (t)
5835 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5836 pfd->visited))
5837 return error_mark_node;
5838 break;
5840 case BOUND_TEMPLATE_TEMPLATE_PARM:
5841 /* Record template parameters such as `T' inside `TT<T>'. */
5842 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5843 return error_mark_node;
5844 /* Fall through. */
5846 case TEMPLATE_TEMPLATE_PARM:
5847 case TEMPLATE_TYPE_PARM:
5848 case TEMPLATE_PARM_INDEX:
5849 if (fn && (*fn)(t, data))
5850 return error_mark_node;
5851 else if (!fn)
5852 return error_mark_node;
5853 break;
5855 case TEMPLATE_DECL:
5856 /* A template template parameter is encountered. */
5857 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5858 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5859 return error_mark_node;
5861 /* Already substituted template template parameter */
5862 *walk_subtrees = 0;
5863 break;
5865 case TYPENAME_TYPE:
5866 if (!fn
5867 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5868 data, pfd->visited))
5869 return error_mark_node;
5870 break;
5872 case CONSTRUCTOR:
5873 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5874 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5875 (TREE_TYPE (t)), fn, data,
5876 pfd->visited))
5877 return error_mark_node;
5878 break;
5880 case INDIRECT_REF:
5881 case COMPONENT_REF:
5882 /* If there's no type, then this thing must be some expression
5883 involving template parameters. */
5884 if (!fn && !TREE_TYPE (t))
5885 return error_mark_node;
5886 break;
5888 case MODOP_EXPR:
5889 case CAST_EXPR:
5890 case REINTERPRET_CAST_EXPR:
5891 case CONST_CAST_EXPR:
5892 case STATIC_CAST_EXPR:
5893 case DYNAMIC_CAST_EXPR:
5894 case ARROW_EXPR:
5895 case DOTSTAR_EXPR:
5896 case TYPEID_EXPR:
5897 case PSEUDO_DTOR_EXPR:
5898 if (!fn)
5899 return error_mark_node;
5900 break;
5902 default:
5903 break;
5906 /* We didn't find any template parameters we liked. */
5907 return NULL_TREE;
5910 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5911 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5912 call FN with the parameter and the DATA.
5913 If FN returns nonzero, the iteration is terminated, and
5914 for_each_template_parm returns 1. Otherwise, the iteration
5915 continues. If FN never returns a nonzero value, the value
5916 returned by for_each_template_parm is 0. If FN is NULL, it is
5917 considered to be the function which always returns 1. */
5919 static int
5920 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5921 struct pointer_set_t *visited)
5923 struct pair_fn_data pfd;
5924 int result;
5926 /* Set up. */
5927 pfd.fn = fn;
5928 pfd.data = data;
5930 /* Walk the tree. (Conceptually, we would like to walk without
5931 duplicates, but for_each_template_parm_r recursively calls
5932 for_each_template_parm, so we would need to reorganize a fair
5933 bit to use walk_tree_without_duplicates, so we keep our own
5934 visited list.) */
5935 if (visited)
5936 pfd.visited = visited;
5937 else
5938 pfd.visited = pointer_set_create ();
5939 result = cp_walk_tree (&t,
5940 for_each_template_parm_r,
5941 &pfd,
5942 pfd.visited) != NULL_TREE;
5944 /* Clean up. */
5945 if (!visited)
5947 pointer_set_destroy (pfd.visited);
5948 pfd.visited = 0;
5951 return result;
5954 /* Returns true if T depends on any template parameter. */
5957 uses_template_parms (tree t)
5959 bool dependent_p;
5960 int saved_processing_template_decl;
5962 saved_processing_template_decl = processing_template_decl;
5963 if (!saved_processing_template_decl)
5964 processing_template_decl = 1;
5965 if (TYPE_P (t))
5966 dependent_p = dependent_type_p (t);
5967 else if (TREE_CODE (t) == TREE_VEC)
5968 dependent_p = any_dependent_template_arguments_p (t);
5969 else if (TREE_CODE (t) == TREE_LIST)
5970 dependent_p = (uses_template_parms (TREE_VALUE (t))
5971 || uses_template_parms (TREE_CHAIN (t)));
5972 else if (TREE_CODE (t) == TYPE_DECL)
5973 dependent_p = dependent_type_p (TREE_TYPE (t));
5974 else if (DECL_P (t)
5975 || EXPR_P (t)
5976 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5977 || TREE_CODE (t) == OVERLOAD
5978 || TREE_CODE (t) == BASELINK
5979 || TREE_CODE (t) == IDENTIFIER_NODE
5980 || TREE_CODE (t) == TRAIT_EXPR
5981 || CONSTANT_CLASS_P (t))
5982 dependent_p = (type_dependent_expression_p (t)
5983 || value_dependent_expression_p (t));
5984 else
5986 gcc_assert (t == error_mark_node);
5987 dependent_p = false;
5990 processing_template_decl = saved_processing_template_decl;
5992 return dependent_p;
5995 /* Returns true if T depends on any template parameter with level LEVEL. */
5998 uses_template_parms_level (tree t, int level)
6000 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
6003 static int tinst_depth;
6004 extern int max_tinst_depth;
6005 #ifdef GATHER_STATISTICS
6006 int depth_reached;
6007 #endif
6008 static int tinst_level_tick;
6009 static int last_template_error_tick;
6011 /* We're starting to instantiate D; record the template instantiation context
6012 for diagnostics and to restore it later. */
6014 static int
6015 push_tinst_level (tree d)
6017 struct tinst_level *new;
6019 if (tinst_depth >= max_tinst_depth)
6021 /* If the instantiation in question still has unbound template parms,
6022 we don't really care if we can't instantiate it, so just return.
6023 This happens with base instantiation for implicit `typename'. */
6024 if (uses_template_parms (d))
6025 return 0;
6027 last_template_error_tick = tinst_level_tick;
6028 error ("template instantiation depth exceeds maximum of %d (use "
6029 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6030 max_tinst_depth, d);
6032 print_instantiation_context ();
6034 return 0;
6037 new = GGC_NEW (struct tinst_level);
6038 new->decl = d;
6039 new->locus = input_location;
6040 new->in_system_header_p = in_system_header;
6041 new->next = current_tinst_level;
6042 current_tinst_level = new;
6044 ++tinst_depth;
6045 #ifdef GATHER_STATISTICS
6046 if (tinst_depth > depth_reached)
6047 depth_reached = tinst_depth;
6048 #endif
6050 ++tinst_level_tick;
6051 return 1;
6054 /* We're done instantiating this template; return to the instantiation
6055 context. */
6057 static void
6058 pop_tinst_level (void)
6060 /* Restore the filename and line number stashed away when we started
6061 this instantiation. */
6062 input_location = current_tinst_level->locus;
6063 in_system_header = current_tinst_level->in_system_header_p;
6064 current_tinst_level = current_tinst_level->next;
6065 --tinst_depth;
6066 ++tinst_level_tick;
6069 /* We're instantiating a deferred template; restore the template
6070 instantiation context in which the instantiation was requested, which
6071 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6073 static tree
6074 reopen_tinst_level (struct tinst_level *level)
6076 struct tinst_level *t;
6078 tinst_depth = 0;
6079 for (t = level; t; t = t->next)
6080 ++tinst_depth;
6082 current_tinst_level = level;
6083 pop_tinst_level ();
6084 return level->decl;
6087 /* Returns the TINST_LEVEL which gives the original instantiation
6088 context. */
6090 struct tinst_level *
6091 outermost_tinst_level (void)
6093 struct tinst_level *level = current_tinst_level;
6094 if (level)
6095 while (level->next)
6096 level = level->next;
6097 return level;
6100 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6101 vector of template arguments, as for tsubst.
6103 Returns an appropriate tsubst'd friend declaration. */
6105 static tree
6106 tsubst_friend_function (tree decl, tree args)
6108 tree new_friend;
6110 if (TREE_CODE (decl) == FUNCTION_DECL
6111 && DECL_TEMPLATE_INSTANTIATION (decl)
6112 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6113 /* This was a friend declared with an explicit template
6114 argument list, e.g.:
6116 friend void f<>(T);
6118 to indicate that f was a template instantiation, not a new
6119 function declaration. Now, we have to figure out what
6120 instantiation of what template. */
6122 tree template_id, arglist, fns;
6123 tree new_args;
6124 tree tmpl;
6125 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6127 /* Friend functions are looked up in the containing namespace scope.
6128 We must enter that scope, to avoid finding member functions of the
6129 current cless with same name. */
6130 push_nested_namespace (ns);
6131 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6132 tf_warning_or_error, NULL_TREE,
6133 /*integral_constant_expression_p=*/false);
6134 pop_nested_namespace (ns);
6135 arglist = tsubst (DECL_TI_ARGS (decl), args,
6136 tf_warning_or_error, NULL_TREE);
6137 template_id = lookup_template_function (fns, arglist);
6139 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6140 tmpl = determine_specialization (template_id, new_friend,
6141 &new_args,
6142 /*need_member_template=*/0,
6143 TREE_VEC_LENGTH (args),
6144 tsk_none);
6145 return instantiate_template (tmpl, new_args, tf_error);
6148 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6150 /* The NEW_FRIEND will look like an instantiation, to the
6151 compiler, but is not an instantiation from the point of view of
6152 the language. For example, we might have had:
6154 template <class T> struct S {
6155 template <class U> friend void f(T, U);
6158 Then, in S<int>, template <class U> void f(int, U) is not an
6159 instantiation of anything. */
6160 if (new_friend == error_mark_node)
6161 return error_mark_node;
6163 DECL_USE_TEMPLATE (new_friend) = 0;
6164 if (TREE_CODE (decl) == TEMPLATE_DECL)
6166 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6167 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6168 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6171 /* The mangled name for the NEW_FRIEND is incorrect. The function
6172 is not a template instantiation and should not be mangled like
6173 one. Therefore, we forget the mangling here; we'll recompute it
6174 later if we need it. */
6175 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6177 SET_DECL_RTL (new_friend, NULL_RTX);
6178 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6181 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6183 tree old_decl;
6184 tree new_friend_template_info;
6185 tree new_friend_result_template_info;
6186 tree ns;
6187 int new_friend_is_defn;
6189 /* We must save some information from NEW_FRIEND before calling
6190 duplicate decls since that function will free NEW_FRIEND if
6191 possible. */
6192 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6193 new_friend_is_defn =
6194 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6195 (template_for_substitution (new_friend)))
6196 != NULL_TREE);
6197 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6199 /* This declaration is a `primary' template. */
6200 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6202 new_friend_result_template_info
6203 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6205 else
6206 new_friend_result_template_info = NULL_TREE;
6208 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6209 if (new_friend_is_defn)
6210 DECL_INITIAL (new_friend) = error_mark_node;
6212 /* Inside pushdecl_namespace_level, we will push into the
6213 current namespace. However, the friend function should go
6214 into the namespace of the template. */
6215 ns = decl_namespace_context (new_friend);
6216 push_nested_namespace (ns);
6217 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6218 pop_nested_namespace (ns);
6220 if (old_decl == error_mark_node)
6221 return error_mark_node;
6223 if (old_decl != new_friend)
6225 /* This new friend declaration matched an existing
6226 declaration. For example, given:
6228 template <class T> void f(T);
6229 template <class U> class C {
6230 template <class T> friend void f(T) {}
6233 the friend declaration actually provides the definition
6234 of `f', once C has been instantiated for some type. So,
6235 old_decl will be the out-of-class template declaration,
6236 while new_friend is the in-class definition.
6238 But, if `f' was called before this point, the
6239 instantiation of `f' will have DECL_TI_ARGS corresponding
6240 to `T' but not to `U', references to which might appear
6241 in the definition of `f'. Previously, the most general
6242 template for an instantiation of `f' was the out-of-class
6243 version; now it is the in-class version. Therefore, we
6244 run through all specialization of `f', adding to their
6245 DECL_TI_ARGS appropriately. In particular, they need a
6246 new set of outer arguments, corresponding to the
6247 arguments for this class instantiation.
6249 The same situation can arise with something like this:
6251 friend void f(int);
6252 template <class T> class C {
6253 friend void f(T) {}
6256 when `C<int>' is instantiated. Now, `f(int)' is defined
6257 in the class. */
6259 if (!new_friend_is_defn)
6260 /* On the other hand, if the in-class declaration does
6261 *not* provide a definition, then we don't want to alter
6262 existing definitions. We can just leave everything
6263 alone. */
6265 else
6267 /* Overwrite whatever template info was there before, if
6268 any, with the new template information pertaining to
6269 the declaration. */
6270 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6272 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6273 reregister_specialization (new_friend,
6274 most_general_template (old_decl),
6275 old_decl);
6276 else
6278 tree t;
6279 tree new_friend_args;
6281 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6282 = new_friend_result_template_info;
6284 new_friend_args = TI_ARGS (new_friend_template_info);
6285 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6286 t != NULL_TREE;
6287 t = TREE_CHAIN (t))
6289 tree spec = TREE_VALUE (t);
6291 DECL_TI_ARGS (spec)
6292 = add_outermost_template_args (new_friend_args,
6293 DECL_TI_ARGS (spec));
6296 /* Now, since specializations are always supposed to
6297 hang off of the most general template, we must move
6298 them. */
6299 t = most_general_template (old_decl);
6300 if (t != old_decl)
6302 DECL_TEMPLATE_SPECIALIZATIONS (t)
6303 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6304 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6305 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6310 /* The information from NEW_FRIEND has been merged into OLD_DECL
6311 by duplicate_decls. */
6312 new_friend = old_decl;
6315 else
6317 tree context = DECL_CONTEXT (new_friend);
6318 bool dependent_p;
6320 /* In the code
6321 template <class T> class C {
6322 template <class U> friend void C1<U>::f (); // case 1
6323 friend void C2<T>::f (); // case 2
6325 we only need to make sure CONTEXT is a complete type for
6326 case 2. To distinguish between the two cases, we note that
6327 CONTEXT of case 1 remains dependent type after tsubst while
6328 this isn't true for case 2. */
6329 ++processing_template_decl;
6330 dependent_p = dependent_type_p (context);
6331 --processing_template_decl;
6333 if (!dependent_p
6334 && !complete_type_or_else (context, NULL_TREE))
6335 return error_mark_node;
6337 if (COMPLETE_TYPE_P (context))
6339 /* Check to see that the declaration is really present, and,
6340 possibly obtain an improved declaration. */
6341 tree fn = check_classfn (context,
6342 new_friend, NULL_TREE);
6344 if (fn)
6345 new_friend = fn;
6349 return new_friend;
6352 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6353 template arguments, as for tsubst.
6355 Returns an appropriate tsubst'd friend type or error_mark_node on
6356 failure. */
6358 static tree
6359 tsubst_friend_class (tree friend_tmpl, tree args)
6361 tree friend_type;
6362 tree tmpl;
6363 tree context;
6365 context = DECL_CONTEXT (friend_tmpl);
6367 if (context)
6369 if (TREE_CODE (context) == NAMESPACE_DECL)
6370 push_nested_namespace (context);
6371 else
6372 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6375 /* Look for a class template declaration. We look for hidden names
6376 because two friend declarations of the same template are the
6377 same. For example, in:
6379 struct A {
6380 template <typename> friend class F;
6382 template <typename> struct B {
6383 template <typename> friend class F;
6386 both F templates are the same. */
6387 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6388 /*block_p=*/true, 0,
6389 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6391 /* But, if we don't find one, it might be because we're in a
6392 situation like this:
6394 template <class T>
6395 struct S {
6396 template <class U>
6397 friend struct S;
6400 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6401 for `S<int>', not the TEMPLATE_DECL. */
6402 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6404 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6405 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6408 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6410 /* The friend template has already been declared. Just
6411 check to see that the declarations match, and install any new
6412 default parameters. We must tsubst the default parameters,
6413 of course. We only need the innermost template parameters
6414 because that is all that redeclare_class_template will look
6415 at. */
6416 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6417 > TMPL_ARGS_DEPTH (args))
6419 tree parms;
6420 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6421 args, tf_warning_or_error);
6422 redeclare_class_template (TREE_TYPE (tmpl), parms);
6425 friend_type = TREE_TYPE (tmpl);
6427 else
6429 /* The friend template has not already been declared. In this
6430 case, the instantiation of the template class will cause the
6431 injection of this template into the global scope. */
6432 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6433 if (tmpl == error_mark_node)
6434 return error_mark_node;
6436 /* The new TMPL is not an instantiation of anything, so we
6437 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6438 the new type because that is supposed to be the corresponding
6439 template decl, i.e., TMPL. */
6440 DECL_USE_TEMPLATE (tmpl) = 0;
6441 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6442 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6443 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6444 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6446 /* Inject this template into the global scope. */
6447 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6450 if (context)
6452 if (TREE_CODE (context) == NAMESPACE_DECL)
6453 pop_nested_namespace (context);
6454 else
6455 pop_nested_class ();
6458 return friend_type;
6461 /* Returns zero if TYPE cannot be completed later due to circularity.
6462 Otherwise returns one. */
6464 static int
6465 can_complete_type_without_circularity (tree type)
6467 if (type == NULL_TREE || type == error_mark_node)
6468 return 0;
6469 else if (COMPLETE_TYPE_P (type))
6470 return 1;
6471 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6472 return can_complete_type_without_circularity (TREE_TYPE (type));
6473 else if (CLASS_TYPE_P (type)
6474 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6475 return 0;
6476 else
6477 return 1;
6480 /* Apply any attributes which had to be deferred until instantiation
6481 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6482 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6484 static void
6485 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
6486 tree args, tsubst_flags_t complain, tree in_decl)
6488 tree late_attrs = NULL_TREE;
6489 tree t;
6491 if (DECL_P (*decl_p))
6492 DECL_ATTRIBUTES (*decl_p) = attributes;
6493 else
6494 TYPE_ATTRIBUTES (*decl_p) = attributes;
6496 /* Set processing_template_decl so we can check for dependent
6497 expressions. */
6498 ++processing_template_decl;
6500 for (t = attributes; t; t = TREE_CHAIN (t))
6501 if (is_late_template_attribute (t))
6502 late_attrs = tree_cons
6503 (TREE_PURPOSE (t),
6504 tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
6505 /*integral_constant_expression_p=*/false),
6506 late_attrs);
6508 --processing_template_decl;
6510 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
6513 tree
6514 instantiate_class_template (tree type)
6516 tree template, args, pattern, t, member;
6517 tree typedecl;
6518 tree pbinfo;
6519 tree base_list;
6521 if (type == error_mark_node)
6522 return error_mark_node;
6524 if (TYPE_BEING_DEFINED (type)
6525 || COMPLETE_TYPE_P (type)
6526 || dependent_type_p (type))
6527 return type;
6529 /* Figure out which template is being instantiated. */
6530 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6531 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
6533 /* Determine what specialization of the original template to
6534 instantiate. */
6535 t = most_specialized_class (type, template);
6536 if (t == error_mark_node)
6538 TYPE_BEING_DEFINED (type) = 1;
6539 return error_mark_node;
6541 else if (t)
6543 /* This TYPE is actually an instantiation of a partial
6544 specialization. We replace the innermost set of ARGS with
6545 the arguments appropriate for substitution. For example,
6546 given:
6548 template <class T> struct S {};
6549 template <class T> struct S<T*> {};
6551 and supposing that we are instantiating S<int*>, ARGS will
6552 presently be {int*} -- but we need {int}. */
6553 pattern = TREE_TYPE (t);
6554 args = TREE_PURPOSE (t);
6556 else
6558 pattern = TREE_TYPE (template);
6559 args = CLASSTYPE_TI_ARGS (type);
6562 /* If the template we're instantiating is incomplete, then clearly
6563 there's nothing we can do. */
6564 if (!COMPLETE_TYPE_P (pattern))
6565 return type;
6567 /* If we've recursively instantiated too many templates, stop. */
6568 if (! push_tinst_level (type))
6569 return type;
6571 /* Now we're really doing the instantiation. Mark the type as in
6572 the process of being defined. */
6573 TYPE_BEING_DEFINED (type) = 1;
6575 /* We may be in the middle of deferred access check. Disable
6576 it now. */
6577 push_deferring_access_checks (dk_no_deferred);
6579 push_to_top_level ();
6581 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
6583 /* Set the input location to the template definition. This is needed
6584 if tsubsting causes an error. */
6585 typedecl = TYPE_MAIN_DECL (type);
6586 input_location = DECL_SOURCE_LOCATION (typedecl);
6587 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
6589 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
6590 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
6591 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
6592 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
6593 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
6594 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
6595 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
6596 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
6597 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
6598 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
6599 TYPE_PACKED (type) = TYPE_PACKED (pattern);
6600 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
6601 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
6602 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6603 if (ANON_AGGR_TYPE_P (pattern))
6604 SET_ANON_AGGR_TYPE_P (type);
6605 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
6607 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
6608 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
6611 pbinfo = TYPE_BINFO (pattern);
6613 /* We should never instantiate a nested class before its enclosing
6614 class; we need to look up the nested class by name before we can
6615 instantiate it, and that lookup should instantiate the enclosing
6616 class. */
6617 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
6618 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
6619 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
6621 base_list = NULL_TREE;
6622 if (BINFO_N_BASE_BINFOS (pbinfo))
6624 tree pbase_binfo;
6625 tree context = TYPE_CONTEXT (type);
6626 tree pushed_scope;
6627 int i;
6629 /* We must enter the scope containing the type, as that is where
6630 the accessibility of types named in dependent bases are
6631 looked up from. */
6632 pushed_scope = push_scope (context ? context : global_namespace);
6634 /* Substitute into each of the bases to determine the actual
6635 basetypes. */
6636 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
6638 tree base;
6639 tree access = BINFO_BASE_ACCESS (pbinfo, i);
6640 tree expanded_bases = NULL_TREE;
6641 int idx, len = 1;
6643 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
6645 expanded_bases =
6646 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
6647 args, tf_error, NULL_TREE);
6648 if (expanded_bases == error_mark_node)
6649 continue;
6651 len = TREE_VEC_LENGTH (expanded_bases);
6654 for (idx = 0; idx < len; idx++)
6656 if (expanded_bases)
6657 /* Extract the already-expanded base class. */
6658 base = TREE_VEC_ELT (expanded_bases, idx);
6659 else
6660 /* Substitute to figure out the base class. */
6661 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
6662 NULL_TREE);
6664 if (base == error_mark_node)
6665 continue;
6667 base_list = tree_cons (access, base, base_list);
6668 if (BINFO_VIRTUAL_P (pbase_binfo))
6669 TREE_TYPE (base_list) = integer_type_node;
6673 /* The list is now in reverse order; correct that. */
6674 base_list = nreverse (base_list);
6676 if (pushed_scope)
6677 pop_scope (pushed_scope);
6679 /* Now call xref_basetypes to set up all the base-class
6680 information. */
6681 xref_basetypes (type, base_list);
6683 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
6684 (int) ATTR_FLAG_TYPE_IN_PLACE,
6685 args, tf_error, NULL_TREE);
6687 /* Now that our base classes are set up, enter the scope of the
6688 class, so that name lookups into base classes, etc. will work
6689 correctly. This is precisely analogous to what we do in
6690 begin_class_definition when defining an ordinary non-template
6691 class. */
6692 pushclass (type);
6694 /* Now members are processed in the order of declaration. */
6695 for (member = CLASSTYPE_DECL_LIST (pattern);
6696 member; member = TREE_CHAIN (member))
6698 tree t = TREE_VALUE (member);
6700 if (TREE_PURPOSE (member))
6702 if (TYPE_P (t))
6704 /* Build new CLASSTYPE_NESTED_UTDS. */
6706 tree newtag;
6707 bool class_template_p;
6709 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
6710 && TYPE_LANG_SPECIFIC (t)
6711 && CLASSTYPE_IS_TEMPLATE (t));
6712 /* If the member is a class template, then -- even after
6713 substitution -- there may be dependent types in the
6714 template argument list for the class. We increment
6715 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6716 that function will assume that no types are dependent
6717 when outside of a template. */
6718 if (class_template_p)
6719 ++processing_template_decl;
6720 newtag = tsubst (t, args, tf_error, NULL_TREE);
6721 if (class_template_p)
6722 --processing_template_decl;
6723 if (newtag == error_mark_node)
6724 continue;
6726 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
6728 tree name = TYPE_IDENTIFIER (t);
6730 if (class_template_p)
6731 /* Unfortunately, lookup_template_class sets
6732 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6733 instantiation (i.e., for the type of a member
6734 template class nested within a template class.)
6735 This behavior is required for
6736 maybe_process_partial_specialization to work
6737 correctly, but is not accurate in this case;
6738 the TAG is not an instantiation of anything.
6739 (The corresponding TEMPLATE_DECL is an
6740 instantiation, but the TYPE is not.) */
6741 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
6743 /* Now, we call pushtag to put this NEWTAG into the scope of
6744 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6745 pushtag calling push_template_decl. We don't have to do
6746 this for enums because it will already have been done in
6747 tsubst_enum. */
6748 if (name)
6749 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
6750 pushtag (name, newtag, /*tag_scope=*/ts_current);
6753 else if (TREE_CODE (t) == FUNCTION_DECL
6754 || DECL_FUNCTION_TEMPLATE_P (t))
6756 /* Build new TYPE_METHODS. */
6757 tree r;
6759 if (TREE_CODE (t) == TEMPLATE_DECL)
6760 ++processing_template_decl;
6761 r = tsubst (t, args, tf_error, NULL_TREE);
6762 if (TREE_CODE (t) == TEMPLATE_DECL)
6763 --processing_template_decl;
6764 set_current_access_from_decl (r);
6765 finish_member_declaration (r);
6767 else
6769 /* Build new TYPE_FIELDS. */
6770 if (TREE_CODE (t) == STATIC_ASSERT)
6772 tree condition =
6773 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
6774 tf_warning_or_error, NULL_TREE,
6775 /*integral_constant_expression_p=*/true);
6776 finish_static_assert (condition,
6777 STATIC_ASSERT_MESSAGE (t),
6778 STATIC_ASSERT_SOURCE_LOCATION (t),
6779 /*member_p=*/true);
6781 else if (TREE_CODE (t) != CONST_DECL)
6783 tree r;
6785 /* The the file and line for this declaration, to
6786 assist in error message reporting. Since we
6787 called push_tinst_level above, we don't need to
6788 restore these. */
6789 input_location = DECL_SOURCE_LOCATION (t);
6791 if (TREE_CODE (t) == TEMPLATE_DECL)
6792 ++processing_template_decl;
6793 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
6794 if (TREE_CODE (t) == TEMPLATE_DECL)
6795 --processing_template_decl;
6796 if (TREE_CODE (r) == VAR_DECL)
6798 /* In [temp.inst]:
6800 [t]he initialization (and any associated
6801 side-effects) of a static data member does
6802 not occur unless the static data member is
6803 itself used in a way that requires the
6804 definition of the static data member to
6805 exist.
6807 Therefore, we do not substitute into the
6808 initialized for the static data member here. */
6809 finish_static_data_member_decl
6811 /*init=*/NULL_TREE,
6812 /*init_const_expr_p=*/false,
6813 /*asmspec_tree=*/NULL_TREE,
6814 /*flags=*/0);
6815 if (DECL_INITIALIZED_IN_CLASS_P (r))
6816 check_static_variable_definition (r, TREE_TYPE (r));
6818 else if (TREE_CODE (r) == FIELD_DECL)
6820 /* Determine whether R has a valid type and can be
6821 completed later. If R is invalid, then it is
6822 replaced by error_mark_node so that it will not be
6823 added to TYPE_FIELDS. */
6824 tree rtype = TREE_TYPE (r);
6825 if (can_complete_type_without_circularity (rtype))
6826 complete_type (rtype);
6828 if (!COMPLETE_TYPE_P (rtype))
6830 cxx_incomplete_type_error (r, rtype);
6831 r = error_mark_node;
6835 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6836 such a thing will already have been added to the field
6837 list by tsubst_enum in finish_member_declaration in the
6838 CLASSTYPE_NESTED_UTDS case above. */
6839 if (!(TREE_CODE (r) == TYPE_DECL
6840 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
6841 && DECL_ARTIFICIAL (r)))
6843 set_current_access_from_decl (r);
6844 finish_member_declaration (r);
6849 else
6851 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
6853 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6855 tree friend_type = t;
6856 bool adjust_processing_template_decl = false;
6858 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6860 /* template <class T> friend class C; */
6861 friend_type = tsubst_friend_class (friend_type, args);
6862 adjust_processing_template_decl = true;
6864 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6866 /* template <class T> friend class C::D; */
6867 friend_type = tsubst (friend_type, args,
6868 tf_warning_or_error, NULL_TREE);
6869 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6870 friend_type = TREE_TYPE (friend_type);
6871 adjust_processing_template_decl = true;
6873 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6875 /* This could be either
6877 friend class T::C;
6879 when dependent_type_p is false or
6881 template <class U> friend class T::C;
6883 otherwise. */
6884 friend_type = tsubst (friend_type, args,
6885 tf_warning_or_error, NULL_TREE);
6886 /* Bump processing_template_decl for correct
6887 dependent_type_p calculation. */
6888 ++processing_template_decl;
6889 if (dependent_type_p (friend_type))
6890 adjust_processing_template_decl = true;
6891 --processing_template_decl;
6893 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6894 && hidden_name_p (TYPE_NAME (friend_type)))
6896 /* friend class C;
6898 where C hasn't been declared yet. Let's lookup name
6899 from namespace scope directly, bypassing any name that
6900 come from dependent base class. */
6901 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6903 /* The call to xref_tag_from_type does injection for friend
6904 classes. */
6905 push_nested_namespace (ns);
6906 friend_type =
6907 xref_tag_from_type (friend_type, NULL_TREE,
6908 /*tag_scope=*/ts_current);
6909 pop_nested_namespace (ns);
6911 else if (uses_template_parms (friend_type))
6912 /* friend class C<T>; */
6913 friend_type = tsubst (friend_type, args,
6914 tf_warning_or_error, NULL_TREE);
6915 /* Otherwise it's
6917 friend class C;
6919 where C is already declared or
6921 friend class C<int>;
6923 We don't have to do anything in these cases. */
6925 if (adjust_processing_template_decl)
6926 /* Trick make_friend_class into realizing that the friend
6927 we're adding is a template, not an ordinary class. It's
6928 important that we use make_friend_class since it will
6929 perform some error-checking and output cross-reference
6930 information. */
6931 ++processing_template_decl;
6933 if (friend_type != error_mark_node)
6934 make_friend_class (type, friend_type, /*complain=*/false);
6936 if (adjust_processing_template_decl)
6937 --processing_template_decl;
6939 else
6941 /* Build new DECL_FRIENDLIST. */
6942 tree r;
6944 /* The the file and line for this declaration, to
6945 assist in error message reporting. Since we
6946 called push_tinst_level above, we don't need to
6947 restore these. */
6948 input_location = DECL_SOURCE_LOCATION (t);
6950 if (TREE_CODE (t) == TEMPLATE_DECL)
6952 ++processing_template_decl;
6953 push_deferring_access_checks (dk_no_check);
6956 r = tsubst_friend_function (t, args);
6957 add_friend (type, r, /*complain=*/false);
6958 if (TREE_CODE (t) == TEMPLATE_DECL)
6960 pop_deferring_access_checks ();
6961 --processing_template_decl;
6967 /* Set the file and line number information to whatever is given for
6968 the class itself. This puts error messages involving generated
6969 implicit functions at a predictable point, and the same point
6970 that would be used for non-template classes. */
6971 input_location = DECL_SOURCE_LOCATION (typedecl);
6973 unreverse_member_declarations (type);
6974 finish_struct_1 (type);
6975 TYPE_BEING_DEFINED (type) = 0;
6977 /* Now that the class is complete, instantiate default arguments for
6978 any member functions. We don't do this earlier because the
6979 default arguments may reference members of the class. */
6980 if (!PRIMARY_TEMPLATE_P (template))
6981 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6982 if (TREE_CODE (t) == FUNCTION_DECL
6983 /* Implicitly generated member functions will not have template
6984 information; they are not instantiations, but instead are
6985 created "fresh" for each instantiation. */
6986 && DECL_TEMPLATE_INFO (t))
6987 tsubst_default_arguments (t);
6989 popclass ();
6990 pop_from_top_level ();
6991 pop_deferring_access_checks ();
6992 pop_tinst_level ();
6994 /* The vtable for a template class can be emitted in any translation
6995 unit in which the class is instantiated. When there is no key
6996 method, however, finish_struct_1 will already have added TYPE to
6997 the keyed_classes list. */
6998 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6999 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7001 return type;
7004 static tree
7005 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7007 tree r;
7009 if (!t)
7010 r = t;
7011 else if (TYPE_P (t))
7012 r = tsubst (t, args, complain, in_decl);
7013 else
7015 r = tsubst_expr (t, args, complain, in_decl,
7016 /*integral_constant_expression_p=*/true);
7017 r = fold_non_dependent_expr (r);
7019 return r;
7022 /* Substitute ARGS into T, which is an pack expansion
7023 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7024 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7025 (if only a partial substitution could be performed) or
7026 ERROR_MARK_NODE if there was an error. */
7027 tree
7028 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7029 tree in_decl)
7031 tree pattern;
7032 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7033 tree first_arg_pack; int i, len = -1;
7034 tree result;
7035 int incomplete = 0;
7037 gcc_assert (PACK_EXPANSION_P (t));
7038 pattern = PACK_EXPANSION_PATTERN (t);
7040 /* Determine the argument packs that will instantiate the parameter
7041 packs used in the expansion expression. While we're at it,
7042 compute the number of arguments to be expanded and make sure it
7043 is consistent. */
7044 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7045 pack = TREE_CHAIN (pack))
7047 tree parm_pack = TREE_VALUE (pack);
7048 tree arg_pack = NULL_TREE;
7049 tree orig_arg = NULL_TREE;
7051 if (TREE_CODE (parm_pack) == PARM_DECL)
7053 if (local_specializations)
7054 arg_pack = retrieve_local_specialization (parm_pack);
7056 else
7058 int level, idx, levels;
7059 template_parm_level_and_index (parm_pack, &level, &idx);
7061 levels = TMPL_ARGS_DEPTH (args);
7062 if (level <= levels)
7063 arg_pack = TMPL_ARG (args, level, idx);
7066 orig_arg = arg_pack;
7067 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7068 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7070 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7071 /* This can only happen if we forget to expand an argument
7072 pack somewhere else. Just return an error, silently. */
7074 result = make_tree_vec (1);
7075 TREE_VEC_ELT (result, 0) = error_mark_node;
7076 return result;
7079 if (arg_pack)
7081 int my_len =
7082 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7084 /* It's all-or-nothing with incomplete argument packs. */
7085 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7086 return error_mark_node;
7088 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7089 incomplete = 1;
7091 if (len < 0)
7093 len = my_len;
7094 first_arg_pack = arg_pack;
7096 else if (len != my_len)
7098 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7099 error ("mismatched argument pack lengths while expanding "
7100 "%<%T%>",
7101 pattern);
7102 else
7103 error ("mismatched argument pack lengths while expanding "
7104 "%<%E%>",
7105 pattern);
7106 return error_mark_node;
7109 /* Keep track of the parameter packs and their corresponding
7110 argument packs. */
7111 packs = tree_cons (parm_pack, arg_pack, packs);
7112 TREE_TYPE (packs) = orig_arg;
7114 else
7115 /* We can't substitute for this parameter pack. */
7116 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7117 TREE_VALUE (pack),
7118 unsubstituted_packs);
7121 /* We cannot expand this expansion expression, because we don't have
7122 all of the argument packs we need. Substitute into the pattern
7123 and return a PACK_EXPANSION_*. The caller will need to deal with
7124 that. */
7125 if (unsubstituted_packs)
7126 return make_pack_expansion (tsubst (pattern, args, complain,
7127 in_decl));
7129 /* We could not find any argument packs that work. */
7130 if (len < 0)
7131 return error_mark_node;
7133 /* For each argument in each argument pack, substitute into the
7134 pattern. */
7135 result = make_tree_vec (len + incomplete);
7136 for (i = 0; i < len + incomplete; ++i)
7138 /* For parameter pack, change the substitution of the parameter
7139 pack to the ith argument in its argument pack, then expand
7140 the pattern. */
7141 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7143 tree parm = TREE_PURPOSE (pack);
7145 if (TREE_CODE (parm) == PARM_DECL)
7147 /* Select the Ith argument from the pack. */
7148 tree arg = make_node (ARGUMENT_PACK_SELECT);
7149 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7150 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7151 mark_used (parm);
7152 register_local_specialization (arg, parm);
7154 else
7156 tree value = parm;
7157 int idx, level;
7158 template_parm_level_and_index (parm, &level, &idx);
7160 if (i < len)
7162 /* Select the Ith argument from the pack. */
7163 value = make_node (ARGUMENT_PACK_SELECT);
7164 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7165 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7168 /* Update the corresponding argument. */
7169 TMPL_ARG (args, level, idx) = value;
7173 /* Substitute into the PATTERN with the altered arguments. */
7174 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7175 TREE_VEC_ELT (result, i) =
7176 tsubst_expr (pattern, args, complain, in_decl,
7177 /*integral_constant_expression_p=*/false);
7178 else
7179 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7181 if (i == len)
7182 /* When we have incomplete argument packs, the last "expanded"
7183 result is itself a pack expansion, which allows us
7184 to deduce more arguments. */
7185 TREE_VEC_ELT (result, i) =
7186 make_pack_expansion (TREE_VEC_ELT (result, i));
7188 if (TREE_VEC_ELT (result, i) == error_mark_node)
7190 result = error_mark_node;
7191 break;
7195 /* Update ARGS to restore the substitution from parameter packs to
7196 their argument packs. */
7197 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7199 tree parm = TREE_PURPOSE (pack);
7201 if (TREE_CODE (parm) == PARM_DECL)
7202 register_local_specialization (TREE_TYPE (pack), parm);
7203 else
7205 int idx, level;
7206 template_parm_level_and_index (parm, &level, &idx);
7208 /* Update the corresponding argument. */
7209 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7210 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7211 TREE_TYPE (pack);
7212 else
7213 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7217 return result;
7220 /* Substitute ARGS into the vector or list of template arguments T. */
7222 static tree
7223 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7225 tree orig_t = t;
7226 int len = TREE_VEC_LENGTH (t);
7227 int need_new = 0, i, expanded_len_adjust = 0, out;
7228 tree *elts = (tree *) alloca (len * sizeof (tree));
7230 for (i = 0; i < len; i++)
7232 tree orig_arg = TREE_VEC_ELT (t, i);
7233 tree new_arg;
7235 if (TREE_CODE (orig_arg) == TREE_VEC)
7236 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7237 else if (PACK_EXPANSION_P (orig_arg))
7239 /* Substitute into an expansion expression. */
7240 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7242 if (TREE_CODE (new_arg) == TREE_VEC)
7243 /* Add to the expanded length adjustment the number of
7244 expanded arguments. We subtract one from this
7245 measurement, because the argument pack expression
7246 itself is already counted as 1 in
7247 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7248 the argument pack is empty. */
7249 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7251 else if (ARGUMENT_PACK_P (orig_arg))
7253 /* Substitute into each of the arguments. */
7254 new_arg = make_node (TREE_CODE (orig_arg));
7256 SET_ARGUMENT_PACK_ARGS (
7257 new_arg,
7258 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7259 args, complain, in_decl));
7261 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7262 new_arg = error_mark_node;
7264 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7265 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7266 complain, in_decl);
7267 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7269 if (TREE_TYPE (new_arg) == error_mark_node)
7270 new_arg = error_mark_node;
7273 else
7274 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7276 if (new_arg == error_mark_node)
7277 return error_mark_node;
7279 elts[i] = new_arg;
7280 if (new_arg != orig_arg)
7281 need_new = 1;
7284 if (!need_new)
7285 return t;
7287 /* Make space for the expanded arguments coming from template
7288 argument packs. */
7289 t = make_tree_vec (len + expanded_len_adjust);
7290 for (i = 0, out = 0; i < len; i++)
7292 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7293 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7294 && TREE_CODE (elts[i]) == TREE_VEC)
7296 int idx;
7298 /* Now expand the template argument pack "in place". */
7299 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7300 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7302 else
7304 TREE_VEC_ELT (t, out) = elts[i];
7305 out++;
7309 return t;
7312 /* Return the result of substituting ARGS into the template parameters
7313 given by PARMS. If there are m levels of ARGS and m + n levels of
7314 PARMS, then the result will contain n levels of PARMS. For
7315 example, if PARMS is `template <class T> template <class U>
7316 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7317 result will be `template <int*, double, class V>'. */
7319 static tree
7320 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7322 tree r = NULL_TREE;
7323 tree* new_parms;
7325 /* When substituting into a template, we must set
7326 PROCESSING_TEMPLATE_DECL as the template parameters may be
7327 dependent if they are based on one-another, and the dependency
7328 predicates are short-circuit outside of templates. */
7329 ++processing_template_decl;
7331 for (new_parms = &r;
7332 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7333 new_parms = &(TREE_CHAIN (*new_parms)),
7334 parms = TREE_CHAIN (parms))
7336 tree new_vec =
7337 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7338 int i;
7340 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7342 tree tuple;
7343 tree default_value;
7344 tree parm_decl;
7346 if (parms == error_mark_node)
7347 continue;
7349 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7351 if (tuple == error_mark_node)
7352 continue;
7354 default_value = TREE_PURPOSE (tuple);
7355 parm_decl = TREE_VALUE (tuple);
7357 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7358 if (TREE_CODE (parm_decl) == PARM_DECL
7359 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7360 parm_decl = error_mark_node;
7361 default_value = tsubst_template_arg (default_value, args,
7362 complain, NULL_TREE);
7364 tuple = build_tree_list (default_value, parm_decl);
7365 TREE_VEC_ELT (new_vec, i) = tuple;
7368 *new_parms =
7369 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7370 - TMPL_ARGS_DEPTH (args)),
7371 new_vec, NULL_TREE);
7374 --processing_template_decl;
7376 return r;
7379 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7380 type T. If T is not an aggregate or enumeration type, it is
7381 handled as if by tsubst. IN_DECL is as for tsubst. If
7382 ENTERING_SCOPE is nonzero, T is the context for a template which
7383 we are presently tsubst'ing. Return the substituted value. */
7385 static tree
7386 tsubst_aggr_type (tree t,
7387 tree args,
7388 tsubst_flags_t complain,
7389 tree in_decl,
7390 int entering_scope)
7392 if (t == NULL_TREE)
7393 return NULL_TREE;
7395 switch (TREE_CODE (t))
7397 case RECORD_TYPE:
7398 if (TYPE_PTRMEMFUNC_P (t))
7399 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7401 /* Else fall through. */
7402 case ENUMERAL_TYPE:
7403 case UNION_TYPE:
7404 if (TYPE_TEMPLATE_INFO (t))
7406 tree argvec;
7407 tree context;
7408 tree r;
7409 bool saved_skip_evaluation;
7411 /* In "sizeof(X<I>)" we need to evaluate "I". */
7412 saved_skip_evaluation = skip_evaluation;
7413 skip_evaluation = false;
7415 /* First, determine the context for the type we are looking
7416 up. */
7417 context = TYPE_CONTEXT (t);
7418 if (context)
7419 context = tsubst_aggr_type (context, args, complain,
7420 in_decl, /*entering_scope=*/1);
7422 /* Then, figure out what arguments are appropriate for the
7423 type we are trying to find. For example, given:
7425 template <class T> struct S;
7426 template <class T, class U> void f(T, U) { S<U> su; }
7428 and supposing that we are instantiating f<int, double>,
7429 then our ARGS will be {int, double}, but, when looking up
7430 S we only want {double}. */
7431 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7432 complain, in_decl);
7433 if (argvec == error_mark_node)
7434 r = error_mark_node;
7435 else
7437 r = lookup_template_class (t, argvec, in_decl, context,
7438 entering_scope, complain);
7439 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7442 skip_evaluation = saved_skip_evaluation;
7444 return r;
7446 else
7447 /* This is not a template type, so there's nothing to do. */
7448 return t;
7450 default:
7451 return tsubst (t, args, complain, in_decl);
7455 /* Substitute into the default argument ARG (a default argument for
7456 FN), which has the indicated TYPE. */
7458 tree
7459 tsubst_default_argument (tree fn, tree type, tree arg)
7461 tree saved_class_ptr = NULL_TREE;
7462 tree saved_class_ref = NULL_TREE;
7464 /* This default argument came from a template. Instantiate the
7465 default argument here, not in tsubst. In the case of
7466 something like:
7468 template <class T>
7469 struct S {
7470 static T t();
7471 void f(T = t());
7474 we must be careful to do name lookup in the scope of S<T>,
7475 rather than in the current class. */
7476 push_access_scope (fn);
7477 /* The "this" pointer is not valid in a default argument. */
7478 if (cfun)
7480 saved_class_ptr = current_class_ptr;
7481 cp_function_chain->x_current_class_ptr = NULL_TREE;
7482 saved_class_ref = current_class_ref;
7483 cp_function_chain->x_current_class_ref = NULL_TREE;
7486 push_deferring_access_checks(dk_no_deferred);
7487 /* The default argument expression may cause implicitly defined
7488 member functions to be synthesized, which will result in garbage
7489 collection. We must treat this situation as if we were within
7490 the body of function so as to avoid collecting live data on the
7491 stack. */
7492 ++function_depth;
7493 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
7494 tf_warning_or_error, NULL_TREE,
7495 /*integral_constant_expression_p=*/false);
7496 --function_depth;
7497 pop_deferring_access_checks();
7499 /* Restore the "this" pointer. */
7500 if (cfun)
7502 cp_function_chain->x_current_class_ptr = saved_class_ptr;
7503 cp_function_chain->x_current_class_ref = saved_class_ref;
7506 pop_access_scope (fn);
7508 /* Make sure the default argument is reasonable. */
7509 arg = check_default_argument (type, arg);
7511 return arg;
7514 /* Substitute into all the default arguments for FN. */
7516 static void
7517 tsubst_default_arguments (tree fn)
7519 tree arg;
7520 tree tmpl_args;
7522 tmpl_args = DECL_TI_ARGS (fn);
7524 /* If this function is not yet instantiated, we certainly don't need
7525 its default arguments. */
7526 if (uses_template_parms (tmpl_args))
7527 return;
7529 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
7530 arg;
7531 arg = TREE_CHAIN (arg))
7532 if (TREE_PURPOSE (arg))
7533 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
7534 TREE_VALUE (arg),
7535 TREE_PURPOSE (arg));
7538 /* Substitute the ARGS into the T, which is a _DECL. Return the
7539 result of the substitution. Issue error and warning messages under
7540 control of COMPLAIN. */
7542 static tree
7543 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
7545 location_t saved_loc;
7546 tree r = NULL_TREE;
7547 tree in_decl = t;
7549 /* Set the filename and linenumber to improve error-reporting. */
7550 saved_loc = input_location;
7551 input_location = DECL_SOURCE_LOCATION (t);
7553 switch (TREE_CODE (t))
7555 case TEMPLATE_DECL:
7557 /* We can get here when processing a member function template,
7558 member class template, and template template parameter of
7559 a template class. */
7560 tree decl = DECL_TEMPLATE_RESULT (t);
7561 tree spec;
7562 tree tmpl_args;
7563 tree full_args;
7565 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7567 /* Template template parameter is treated here. */
7568 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7569 if (new_type == error_mark_node)
7570 return error_mark_node;
7572 r = copy_decl (t);
7573 TREE_CHAIN (r) = NULL_TREE;
7574 TREE_TYPE (r) = new_type;
7575 DECL_TEMPLATE_RESULT (r)
7576 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
7577 DECL_TEMPLATE_PARMS (r)
7578 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7579 complain);
7580 TYPE_NAME (new_type) = r;
7581 break;
7584 /* We might already have an instance of this template.
7585 The ARGS are for the surrounding class type, so the
7586 full args contain the tsubst'd args for the context,
7587 plus the innermost args from the template decl. */
7588 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
7589 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
7590 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7591 /* Because this is a template, the arguments will still be
7592 dependent, even after substitution. If
7593 PROCESSING_TEMPLATE_DECL is not set, the dependency
7594 predicates will short-circuit. */
7595 ++processing_template_decl;
7596 full_args = tsubst_template_args (tmpl_args, args,
7597 complain, in_decl);
7598 --processing_template_decl;
7599 if (full_args == error_mark_node)
7600 return error_mark_node;
7602 /* tsubst_template_args doesn't copy the vector if
7603 nothing changed. But, *something* should have
7604 changed. */
7605 gcc_assert (full_args != tmpl_args);
7607 spec = retrieve_specialization (t, full_args,
7608 /*class_specializations_p=*/true);
7609 if (spec != NULL_TREE)
7611 r = spec;
7612 break;
7615 /* Make a new template decl. It will be similar to the
7616 original, but will record the current template arguments.
7617 We also create a new function declaration, which is just
7618 like the old one, but points to this new template, rather
7619 than the old one. */
7620 r = copy_decl (t);
7621 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
7622 TREE_CHAIN (r) = NULL_TREE;
7624 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
7626 if (TREE_CODE (decl) == TYPE_DECL)
7628 tree new_type;
7629 ++processing_template_decl;
7630 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7631 --processing_template_decl;
7632 if (new_type == error_mark_node)
7633 return error_mark_node;
7635 TREE_TYPE (r) = new_type;
7636 CLASSTYPE_TI_TEMPLATE (new_type) = r;
7637 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
7638 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
7639 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
7641 else
7643 tree new_decl;
7644 ++processing_template_decl;
7645 new_decl = tsubst (decl, args, complain, in_decl);
7646 --processing_template_decl;
7647 if (new_decl == error_mark_node)
7648 return error_mark_node;
7650 DECL_TEMPLATE_RESULT (r) = new_decl;
7651 DECL_TI_TEMPLATE (new_decl) = r;
7652 TREE_TYPE (r) = TREE_TYPE (new_decl);
7653 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
7654 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
7657 SET_DECL_IMPLICIT_INSTANTIATION (r);
7658 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
7659 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
7661 /* The template parameters for this new template are all the
7662 template parameters for the old template, except the
7663 outermost level of parameters. */
7664 DECL_TEMPLATE_PARMS (r)
7665 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7666 complain);
7668 if (PRIMARY_TEMPLATE_P (t))
7669 DECL_PRIMARY_TEMPLATE (r) = r;
7671 if (TREE_CODE (decl) != TYPE_DECL)
7672 /* Record this non-type partial instantiation. */
7673 register_specialization (r, t,
7674 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
7675 false);
7677 break;
7679 case FUNCTION_DECL:
7681 tree ctx;
7682 tree argvec = NULL_TREE;
7683 tree *friends;
7684 tree gen_tmpl;
7685 tree type;
7686 int member;
7687 int args_depth;
7688 int parms_depth;
7690 /* Nobody should be tsubst'ing into non-template functions. */
7691 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
7693 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
7695 tree spec;
7696 bool dependent_p;
7698 /* If T is not dependent, just return it. We have to
7699 increment PROCESSING_TEMPLATE_DECL because
7700 value_dependent_expression_p assumes that nothing is
7701 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7702 ++processing_template_decl;
7703 dependent_p = value_dependent_expression_p (t);
7704 --processing_template_decl;
7705 if (!dependent_p)
7706 return t;
7708 /* Calculate the most general template of which R is a
7709 specialization, and the complete set of arguments used to
7710 specialize R. */
7711 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7712 argvec = tsubst_template_args (DECL_TI_ARGS
7713 (DECL_TEMPLATE_RESULT (gen_tmpl)),
7714 args, complain, in_decl);
7716 /* Check to see if we already have this specialization. */
7717 spec = retrieve_specialization (gen_tmpl, argvec,
7718 /*class_specializations_p=*/false);
7720 if (spec)
7722 r = spec;
7723 break;
7726 /* We can see more levels of arguments than parameters if
7727 there was a specialization of a member template, like
7728 this:
7730 template <class T> struct S { template <class U> void f(); }
7731 template <> template <class U> void S<int>::f(U);
7733 Here, we'll be substituting into the specialization,
7734 because that's where we can find the code we actually
7735 want to generate, but we'll have enough arguments for
7736 the most general template.
7738 We also deal with the peculiar case:
7740 template <class T> struct S {
7741 template <class U> friend void f();
7743 template <class U> void f() {}
7744 template S<int>;
7745 template void f<double>();
7747 Here, the ARGS for the instantiation of will be {int,
7748 double}. But, we only need as many ARGS as there are
7749 levels of template parameters in CODE_PATTERN. We are
7750 careful not to get fooled into reducing the ARGS in
7751 situations like:
7753 template <class T> struct S { template <class U> void f(U); }
7754 template <class T> template <> void S<T>::f(int) {}
7756 which we can spot because the pattern will be a
7757 specialization in this case. */
7758 args_depth = TMPL_ARGS_DEPTH (args);
7759 parms_depth =
7760 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
7761 if (args_depth > parms_depth
7762 && !DECL_TEMPLATE_SPECIALIZATION (t))
7763 args = get_innermost_template_args (args, parms_depth);
7765 else
7767 /* This special case arises when we have something like this:
7769 template <class T> struct S {
7770 friend void f<int>(int, double);
7773 Here, the DECL_TI_TEMPLATE for the friend declaration
7774 will be an IDENTIFIER_NODE. We are being called from
7775 tsubst_friend_function, and we want only to create a
7776 new decl (R) with appropriate types so that we can call
7777 determine_specialization. */
7778 gen_tmpl = NULL_TREE;
7781 if (DECL_CLASS_SCOPE_P (t))
7783 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
7784 member = 2;
7785 else
7786 member = 1;
7787 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
7788 complain, t, /*entering_scope=*/1);
7790 else
7792 member = 0;
7793 ctx = DECL_CONTEXT (t);
7795 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7796 if (type == error_mark_node)
7797 return error_mark_node;
7799 /* We do NOT check for matching decls pushed separately at this
7800 point, as they may not represent instantiations of this
7801 template, and in any case are considered separate under the
7802 discrete model. */
7803 r = copy_decl (t);
7804 DECL_USE_TEMPLATE (r) = 0;
7805 TREE_TYPE (r) = type;
7806 /* Clear out the mangled name and RTL for the instantiation. */
7807 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7808 SET_DECL_RTL (r, NULL_RTX);
7809 DECL_INITIAL (r) = NULL_TREE;
7810 DECL_CONTEXT (r) = ctx;
7812 if (member && DECL_CONV_FN_P (r))
7813 /* Type-conversion operator. Reconstruct the name, in
7814 case it's the name of one of the template's parameters. */
7815 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
7817 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
7818 complain, t);
7819 DECL_RESULT (r) = NULL_TREE;
7821 TREE_STATIC (r) = 0;
7822 TREE_PUBLIC (r) = TREE_PUBLIC (t);
7823 DECL_EXTERNAL (r) = 1;
7824 /* If this is an instantiation of a function with internal
7825 linkage, we already know what object file linkage will be
7826 assigned to the instantiation. */
7827 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
7828 DECL_DEFER_OUTPUT (r) = 0;
7829 TREE_CHAIN (r) = NULL_TREE;
7830 DECL_PENDING_INLINE_INFO (r) = 0;
7831 DECL_PENDING_INLINE_P (r) = 0;
7832 DECL_SAVED_TREE (r) = NULL_TREE;
7833 TREE_USED (r) = 0;
7834 if (DECL_CLONED_FUNCTION (r))
7836 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
7837 args, complain, t);
7838 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
7839 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
7842 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
7843 this in the special friend case mentioned above where
7844 GEN_TMPL is NULL. */
7845 if (gen_tmpl)
7847 DECL_TEMPLATE_INFO (r)
7848 = tree_cons (gen_tmpl, argvec, NULL_TREE);
7849 SET_DECL_IMPLICIT_INSTANTIATION (r);
7850 register_specialization (r, gen_tmpl, argvec, false);
7852 /* We're not supposed to instantiate default arguments
7853 until they are called, for a template. But, for a
7854 declaration like:
7856 template <class T> void f ()
7857 { extern void g(int i = T()); }
7859 we should do the substitution when the template is
7860 instantiated. We handle the member function case in
7861 instantiate_class_template since the default arguments
7862 might refer to other members of the class. */
7863 if (!member
7864 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7865 && !uses_template_parms (argvec))
7866 tsubst_default_arguments (r);
7868 else
7869 DECL_TEMPLATE_INFO (r) = NULL_TREE;
7871 /* Copy the list of befriending classes. */
7872 for (friends = &DECL_BEFRIENDING_CLASSES (r);
7873 *friends;
7874 friends = &TREE_CHAIN (*friends))
7876 *friends = copy_node (*friends);
7877 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
7878 args, complain,
7879 in_decl);
7882 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
7884 maybe_retrofit_in_chrg (r);
7885 if (DECL_CONSTRUCTOR_P (r))
7886 grok_ctor_properties (ctx, r);
7887 /* If this is an instantiation of a member template, clone it.
7888 If it isn't, that'll be handled by
7889 clone_constructors_and_destructors. */
7890 if (PRIMARY_TEMPLATE_P (gen_tmpl))
7891 clone_function_decl (r, /*update_method_vec_p=*/0);
7893 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
7894 && !grok_op_properties (r, (complain & tf_error) != 0))
7895 return error_mark_node;
7897 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
7898 SET_DECL_FRIEND_CONTEXT (r,
7899 tsubst (DECL_FRIEND_CONTEXT (t),
7900 args, complain, in_decl));
7902 /* Possibly limit visibility based on template args. */
7903 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7904 if (DECL_VISIBILITY_SPECIFIED (t))
7906 DECL_VISIBILITY_SPECIFIED (r) = 0;
7907 DECL_ATTRIBUTES (r)
7908 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7910 determine_visibility (r);
7912 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
7913 args, complain, in_decl);
7915 break;
7917 case PARM_DECL:
7919 tree type = NULL_TREE;
7920 int i, len = 1;
7921 tree expanded_types = NULL_TREE;
7922 tree prev_r = NULL_TREE;
7923 tree first_r = NULL_TREE;
7925 if (FUNCTION_PARAMETER_PACK_P (t))
7927 /* If there is a local specialization that isn't a
7928 parameter pack, it means that we're doing a "simple"
7929 substitution from inside tsubst_pack_expansion. Just
7930 return the local specialization (which will be a single
7931 parm). */
7932 tree spec = NULL_TREE;
7933 if (local_specializations)
7934 spec = retrieve_local_specialization (t);
7935 if (spec
7936 && TREE_CODE (spec) == PARM_DECL
7937 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
7938 return spec;
7940 /* Expand the TYPE_PACK_EXPANSION that provides the types for
7941 the parameters in this function parameter pack. */
7942 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
7943 complain, in_decl);
7944 if (TREE_CODE (expanded_types) == TREE_VEC)
7946 len = TREE_VEC_LENGTH (expanded_types);
7948 /* Zero-length parameter packs are boring. Just substitute
7949 into the chain. */
7950 if (len == 0)
7951 return tsubst (TREE_CHAIN (t), args, complain,
7952 TREE_CHAIN (t));
7954 else
7956 /* All we did was update the type. Make a note of that. */
7957 type = expanded_types;
7958 expanded_types = NULL_TREE;
7962 /* Loop through all of the parameter's we'll build. When T is
7963 a function parameter pack, LEN is the number of expanded
7964 types in EXPANDED_TYPES; otherwise, LEN is 1. */
7965 r = NULL_TREE;
7966 for (i = 0; i < len; ++i)
7968 prev_r = r;
7969 r = copy_node (t);
7970 if (DECL_TEMPLATE_PARM_P (t))
7971 SET_DECL_TEMPLATE_PARM_P (r);
7973 if (expanded_types)
7974 /* We're on the Ith parameter of the function parameter
7975 pack. */
7977 /* Get the Ith type. */
7978 type = TREE_VEC_ELT (expanded_types, i);
7980 if (DECL_NAME (r))
7981 /* Rename the parameter to include the index. */
7982 DECL_NAME (r) =
7983 make_ith_pack_parameter_name (DECL_NAME (r), i);
7985 else if (!type)
7986 /* We're dealing with a normal parameter. */
7987 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7989 type = type_decays_to (type);
7990 TREE_TYPE (r) = type;
7991 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7993 if (DECL_INITIAL (r))
7995 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
7996 DECL_INITIAL (r) = TREE_TYPE (r);
7997 else
7998 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
7999 complain, in_decl);
8002 DECL_CONTEXT (r) = NULL_TREE;
8004 if (!DECL_TEMPLATE_PARM_P (r))
8005 DECL_ARG_TYPE (r) = type_passed_as (type);
8007 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8008 args, complain, in_decl);
8010 /* Keep track of the first new parameter we
8011 generate. That's what will be returned to the
8012 caller. */
8013 if (!first_r)
8014 first_r = r;
8016 /* Build a proper chain of parameters when substituting
8017 into a function parameter pack. */
8018 if (prev_r)
8019 TREE_CHAIN (prev_r) = r;
8022 if (TREE_CHAIN (t))
8023 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8024 complain, TREE_CHAIN (t));
8026 /* FIRST_R contains the start of the chain we've built. */
8027 r = first_r;
8029 break;
8031 case FIELD_DECL:
8033 tree type;
8035 r = copy_decl (t);
8036 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8037 if (type == error_mark_node)
8038 return error_mark_node;
8039 TREE_TYPE (r) = type;
8040 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8042 /* DECL_INITIAL gives the number of bits in a bit-field. */
8043 DECL_INITIAL (r)
8044 = tsubst_expr (DECL_INITIAL (t), args,
8045 complain, in_decl,
8046 /*integral_constant_expression_p=*/true);
8047 /* We don't have to set DECL_CONTEXT here; it is set by
8048 finish_member_declaration. */
8049 TREE_CHAIN (r) = NULL_TREE;
8050 if (VOID_TYPE_P (type))
8051 error ("instantiation of %q+D as type %qT", r, type);
8053 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8054 args, complain, in_decl);
8056 break;
8058 case USING_DECL:
8059 /* We reach here only for member using decls. */
8060 if (DECL_DEPENDENT_P (t))
8062 r = do_class_using_decl
8063 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8064 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8065 if (!r)
8066 r = error_mark_node;
8068 else
8070 r = copy_node (t);
8071 TREE_CHAIN (r) = NULL_TREE;
8073 break;
8075 case TYPE_DECL:
8076 case VAR_DECL:
8078 tree argvec = NULL_TREE;
8079 tree gen_tmpl = NULL_TREE;
8080 tree spec;
8081 tree tmpl = NULL_TREE;
8082 tree ctx;
8083 tree type = NULL_TREE;
8084 bool local_p;
8086 if (TREE_CODE (t) == TYPE_DECL)
8088 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8089 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
8090 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8092 /* If this is the canonical decl, we don't have to
8093 mess with instantiations, and often we can't (for
8094 typename, template type parms and such). Note that
8095 TYPE_NAME is not correct for the above test if
8096 we've copied the type for a typedef. */
8097 r = TYPE_NAME (type);
8098 break;
8102 /* Check to see if we already have the specialization we
8103 need. */
8104 spec = NULL_TREE;
8105 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8107 /* T is a static data member or namespace-scope entity.
8108 We have to substitute into namespace-scope variables
8109 (even though such entities are never templates) because
8110 of cases like:
8112 template <class T> void f() { extern T t; }
8114 where the entity referenced is not known until
8115 instantiation time. */
8116 local_p = false;
8117 ctx = DECL_CONTEXT (t);
8118 if (DECL_CLASS_SCOPE_P (t))
8120 ctx = tsubst_aggr_type (ctx, args,
8121 complain,
8122 in_decl, /*entering_scope=*/1);
8123 /* If CTX is unchanged, then T is in fact the
8124 specialization we want. That situation occurs when
8125 referencing a static data member within in its own
8126 class. We can use pointer equality, rather than
8127 same_type_p, because DECL_CONTEXT is always
8128 canonical. */
8129 if (ctx == DECL_CONTEXT (t))
8130 spec = t;
8133 if (!spec)
8135 tmpl = DECL_TI_TEMPLATE (t);
8136 gen_tmpl = most_general_template (tmpl);
8137 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8138 spec = (retrieve_specialization
8139 (gen_tmpl, argvec,
8140 /*class_specializations_p=*/false));
8143 else
8145 /* A local variable. */
8146 local_p = true;
8147 /* Subsequent calls to pushdecl will fill this in. */
8148 ctx = NULL_TREE;
8149 spec = retrieve_local_specialization (t);
8151 /* If we already have the specialization we need, there is
8152 nothing more to do. */
8153 if (spec)
8155 r = spec;
8156 break;
8159 /* Create a new node for the specialization we need. */
8160 r = copy_decl (t);
8161 if (TREE_CODE (r) == VAR_DECL)
8163 /* Even if the original location is out of scope, the
8164 newly substituted one is not. */
8165 DECL_DEAD_FOR_LOCAL (r) = 0;
8166 DECL_INITIALIZED_P (r) = 0;
8167 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8168 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8169 if (type == error_mark_node)
8170 return error_mark_node;
8171 if (TREE_CODE (type) == FUNCTION_TYPE)
8173 /* It may seem that this case cannot occur, since:
8175 typedef void f();
8176 void g() { f x; }
8178 declares a function, not a variable. However:
8180 typedef void f();
8181 template <typename T> void g() { T t; }
8182 template void g<f>();
8184 is an attempt to declare a variable with function
8185 type. */
8186 error ("variable %qD has function type",
8187 /* R is not yet sufficiently initialized, so we
8188 just use its name. */
8189 DECL_NAME (r));
8190 return error_mark_node;
8192 type = complete_type (type);
8193 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8194 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8195 type = check_var_type (DECL_NAME (r), type);
8197 if (DECL_HAS_VALUE_EXPR_P (t))
8199 tree ve = DECL_VALUE_EXPR (t);
8200 ve = tsubst_expr (ve, args, complain, in_decl,
8201 /*constant_expression_p=*/false);
8202 SET_DECL_VALUE_EXPR (r, ve);
8205 else if (DECL_SELF_REFERENCE_P (t))
8206 SET_DECL_SELF_REFERENCE_P (r);
8207 TREE_TYPE (r) = type;
8208 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8209 DECL_CONTEXT (r) = ctx;
8210 /* Clear out the mangled name and RTL for the instantiation. */
8211 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8212 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8213 SET_DECL_RTL (r, NULL_RTX);
8214 /* The initializer must not be expanded until it is required;
8215 see [temp.inst]. */
8216 DECL_INITIAL (r) = NULL_TREE;
8217 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8218 SET_DECL_RTL (r, NULL_RTX);
8219 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8220 if (TREE_CODE (r) == VAR_DECL)
8222 /* Possibly limit visibility based on template args. */
8223 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8224 if (DECL_VISIBILITY_SPECIFIED (t))
8226 DECL_VISIBILITY_SPECIFIED (r) = 0;
8227 DECL_ATTRIBUTES (r)
8228 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8230 determine_visibility (r);
8232 /* Preserve a typedef that names a type. */
8233 else if (TREE_CODE (r) == TYPE_DECL
8234 && DECL_ORIGINAL_TYPE (t)
8235 && type != error_mark_node)
8237 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
8238 args, complain, in_decl);
8239 TREE_TYPE (r) = type = build_variant_type_copy (type);
8240 TYPE_NAME (type) = r;
8243 if (!local_p)
8245 /* A static data member declaration is always marked
8246 external when it is declared in-class, even if an
8247 initializer is present. We mimic the non-template
8248 processing here. */
8249 DECL_EXTERNAL (r) = 1;
8251 register_specialization (r, gen_tmpl, argvec, false);
8252 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8253 SET_DECL_IMPLICIT_INSTANTIATION (r);
8255 else
8256 register_local_specialization (r, t);
8258 TREE_CHAIN (r) = NULL_TREE;
8260 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
8261 (int) ATTR_FLAG_TYPE_IN_PLACE,
8262 args, complain, in_decl);
8263 layout_decl (r, 0);
8265 break;
8267 default:
8268 gcc_unreachable ();
8271 /* Restore the file and line information. */
8272 input_location = saved_loc;
8274 return r;
8277 /* Substitute into the ARG_TYPES of a function type. */
8279 static tree
8280 tsubst_arg_types (tree arg_types,
8281 tree args,
8282 tsubst_flags_t complain,
8283 tree in_decl)
8285 tree remaining_arg_types;
8286 tree type = NULL_TREE;
8287 int i = 1;
8288 tree expanded_args = NULL_TREE;
8289 tree default_arg;
8291 if (!arg_types || arg_types == void_list_node)
8292 return arg_types;
8294 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8295 args, complain, in_decl);
8296 if (remaining_arg_types == error_mark_node)
8297 return error_mark_node;
8299 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8301 /* For a pack expansion, perform substitution on the
8302 entire expression. Later on, we'll handle the arguments
8303 one-by-one. */
8304 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8305 args, complain, in_decl);
8307 if (TREE_CODE (expanded_args) == TREE_VEC)
8308 /* So that we'll spin through the parameters, one by one. */
8309 i = TREE_VEC_LENGTH (expanded_args);
8310 else
8312 /* We only partially substituted into the parameter
8313 pack. Our type is TYPE_PACK_EXPANSION. */
8314 type = expanded_args;
8315 expanded_args = NULL_TREE;
8319 while (i > 0) {
8320 --i;
8322 if (expanded_args)
8323 type = TREE_VEC_ELT (expanded_args, i);
8324 else if (!type)
8325 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8327 if (type == error_mark_node)
8328 return error_mark_node;
8329 if (VOID_TYPE_P (type))
8331 if (complain & tf_error)
8333 error ("invalid parameter type %qT", type);
8334 if (in_decl)
8335 error ("in declaration %q+D", in_decl);
8337 return error_mark_node;
8340 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8341 top-level qualifiers as required. */
8342 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8344 /* We do not substitute into default arguments here. The standard
8345 mandates that they be instantiated only when needed, which is
8346 done in build_over_call. */
8347 default_arg = TREE_PURPOSE (arg_types);
8349 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8351 /* We've instantiated a template before its default arguments
8352 have been parsed. This can happen for a nested template
8353 class, and is not an error unless we require the default
8354 argument in a call of this function. */
8355 remaining_arg_types =
8356 tree_cons (default_arg, type, remaining_arg_types);
8357 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8358 remaining_arg_types);
8360 else
8361 remaining_arg_types =
8362 hash_tree_cons (default_arg, type, remaining_arg_types);
8365 return remaining_arg_types;
8368 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8369 *not* handle the exception-specification for FNTYPE, because the
8370 initial substitution of explicitly provided template parameters
8371 during argument deduction forbids substitution into the
8372 exception-specification:
8374 [temp.deduct]
8376 All references in the function type of the function template to the
8377 corresponding template parameters are replaced by the specified tem-
8378 plate argument values. If a substitution in a template parameter or
8379 in the function type of the function template results in an invalid
8380 type, type deduction fails. [Note: The equivalent substitution in
8381 exception specifications is done only when the function is instanti-
8382 ated, at which point a program is ill-formed if the substitution
8383 results in an invalid type.] */
8385 static tree
8386 tsubst_function_type (tree t,
8387 tree args,
8388 tsubst_flags_t complain,
8389 tree in_decl)
8391 tree return_type;
8392 tree arg_types;
8393 tree fntype;
8395 /* The TYPE_CONTEXT is not used for function/method types. */
8396 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8398 /* Substitute the return type. */
8399 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8400 if (return_type == error_mark_node)
8401 return error_mark_node;
8402 /* The standard does not presently indicate that creation of a
8403 function type with an invalid return type is a deduction failure.
8404 However, that is clearly analogous to creating an array of "void"
8405 or a reference to a reference. This is core issue #486. */
8406 if (TREE_CODE (return_type) == ARRAY_TYPE
8407 || TREE_CODE (return_type) == FUNCTION_TYPE)
8409 if (complain & tf_error)
8411 if (TREE_CODE (return_type) == ARRAY_TYPE)
8412 error ("function returning an array");
8413 else
8414 error ("function returning a function");
8416 return error_mark_node;
8419 /* Substitute the argument types. */
8420 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8421 complain, in_decl);
8422 if (arg_types == error_mark_node)
8423 return error_mark_node;
8425 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
8426 && in_decl != NULL_TREE
8427 && !TREE_NO_WARNING (in_decl)
8428 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
8429 warning (OPT_Wreturn_type,
8430 "type qualifiers ignored on function return type");
8432 /* Construct a new type node and return it. */
8433 if (TREE_CODE (t) == FUNCTION_TYPE)
8434 fntype = build_function_type (return_type, arg_types);
8435 else
8437 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8438 if (! IS_AGGR_TYPE (r))
8440 /* [temp.deduct]
8442 Type deduction may fail for any of the following
8443 reasons:
8445 -- Attempting to create "pointer to member of T" when T
8446 is not a class type. */
8447 if (complain & tf_error)
8448 error ("creating pointer to member function of non-class type %qT",
8450 return error_mark_node;
8453 fntype = build_method_type_directly (r, return_type,
8454 TREE_CHAIN (arg_types));
8456 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8457 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8459 return fntype;
8462 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8463 ARGS into that specification, and return the substituted
8464 specification. If there is no specification, return NULL_TREE. */
8466 static tree
8467 tsubst_exception_specification (tree fntype,
8468 tree args,
8469 tsubst_flags_t complain,
8470 tree in_decl)
8472 tree specs;
8473 tree new_specs;
8475 specs = TYPE_RAISES_EXCEPTIONS (fntype);
8476 new_specs = NULL_TREE;
8477 if (specs)
8479 if (! TREE_VALUE (specs))
8480 new_specs = specs;
8481 else
8482 while (specs)
8484 tree spec;
8485 int i, len = 1;
8486 tree expanded_specs = NULL_TREE;
8488 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
8490 /* Expand the pack expansion type. */
8491 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
8492 args, complain,
8493 in_decl);
8494 len = TREE_VEC_LENGTH (expanded_specs);
8497 for (i = 0; i < len; ++i)
8499 if (expanded_specs)
8500 spec = TREE_VEC_ELT (expanded_specs, i);
8501 else
8502 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
8503 if (spec == error_mark_node)
8504 return spec;
8505 new_specs = add_exception_specifier (new_specs, spec,
8506 complain);
8509 specs = TREE_CHAIN (specs);
8512 return new_specs;
8515 /* Take the tree structure T and replace template parameters used
8516 therein with the argument vector ARGS. IN_DECL is an associated
8517 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8518 Issue error and warning messages under control of COMPLAIN. Note
8519 that we must be relatively non-tolerant of extensions here, in
8520 order to preserve conformance; if we allow substitutions that
8521 should not be allowed, we may allow argument deductions that should
8522 not succeed, and therefore report ambiguous overload situations
8523 where there are none. In theory, we could allow the substitution,
8524 but indicate that it should have failed, and allow our caller to
8525 make sure that the right thing happens, but we don't try to do this
8526 yet.
8528 This function is used for dealing with types, decls and the like;
8529 for expressions, use tsubst_expr or tsubst_copy. */
8531 static tree
8532 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8534 tree type, r;
8536 if (t == NULL_TREE || t == error_mark_node
8537 || t == integer_type_node
8538 || t == void_type_node
8539 || t == char_type_node
8540 || t == unknown_type_node
8541 || TREE_CODE (t) == NAMESPACE_DECL)
8542 return t;
8544 if (DECL_P (t))
8545 return tsubst_decl (t, args, complain);
8547 if (TREE_CODE (t) == IDENTIFIER_NODE)
8548 type = IDENTIFIER_TYPE_VALUE (t);
8549 else
8550 type = TREE_TYPE (t);
8552 gcc_assert (type != unknown_type_node);
8554 /* Reuse typedefs. This is a rather complicated way to check whether the
8555 type is a typedef from the same class template as the current scope,
8556 but I can't think of a better one.
8558 We need to do this to handle dependent attributes, specifically
8559 attribute aligned. */
8560 if (TYPE_P (t)
8561 && TYPE_NAME (t)
8562 && !IS_AGGR_TYPE (t)
8563 && current_class_type
8564 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
8566 tree decl = TYPE_NAME (t);
8567 tree context = DECL_CONTEXT (decl);
8568 if (context
8569 && CLASS_TYPE_P (context)
8570 && CLASSTYPE_TEMPLATE_INFO (context)
8571 && (CLASSTYPE_TI_TEMPLATE (context)
8572 == CLASSTYPE_TI_TEMPLATE (current_class_type))
8573 && (tsubst_aggr_type (context, args, complain, in_decl,
8574 /*entering_scope=*/0)
8575 == current_class_type))
8577 r = lookup_name (DECL_NAME (decl));
8578 if (r && TREE_CODE (r) == TYPE_DECL
8579 && DECL_CONTEXT (r) == current_class_type)
8581 r = TREE_TYPE (r);
8582 r = cp_build_qualified_type_real
8583 (r, cp_type_quals (t) | cp_type_quals (r),
8584 complain | tf_ignore_bad_quals);
8585 return r;
8586 /* Else we're instantiating the typedef, so fall through. */
8591 if (type
8592 && TREE_CODE (t) != TYPENAME_TYPE
8593 && TREE_CODE (t) != IDENTIFIER_NODE
8594 && TREE_CODE (t) != FUNCTION_TYPE
8595 && TREE_CODE (t) != METHOD_TYPE)
8596 type = tsubst (type, args, complain, in_decl);
8597 if (type == error_mark_node)
8598 return error_mark_node;
8600 switch (TREE_CODE (t))
8602 case RECORD_TYPE:
8603 case UNION_TYPE:
8604 case ENUMERAL_TYPE:
8605 return tsubst_aggr_type (t, args, complain, in_decl,
8606 /*entering_scope=*/0);
8608 case ERROR_MARK:
8609 case IDENTIFIER_NODE:
8610 case VOID_TYPE:
8611 case REAL_TYPE:
8612 case COMPLEX_TYPE:
8613 case VECTOR_TYPE:
8614 case BOOLEAN_TYPE:
8615 case INTEGER_CST:
8616 case REAL_CST:
8617 case STRING_CST:
8618 return t;
8620 case INTEGER_TYPE:
8621 if (t == integer_type_node)
8622 return t;
8624 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
8625 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
8626 return t;
8629 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
8631 max = tsubst_expr (omax, args, complain, in_decl,
8632 /*integral_constant_expression_p=*/false);
8633 max = fold_decl_constant_value (max);
8635 if (TREE_CODE (max) != INTEGER_CST
8636 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
8637 && !at_function_scope_p ())
8639 if (complain & tf_error)
8640 error ("array bound is not an integer constant");
8641 return error_mark_node;
8644 /* [temp.deduct]
8646 Type deduction may fail for any of the following
8647 reasons:
8649 Attempting to create an array with a size that is
8650 zero or negative. */
8651 if (integer_zerop (max) && !(complain & tf_error))
8652 /* We must fail if performing argument deduction (as
8653 indicated by the state of complain), so that
8654 another substitution can be found. */
8655 return error_mark_node;
8656 else if (TREE_CODE (max) == INTEGER_CST
8657 && INT_CST_LT (max, integer_zero_node))
8659 if (complain & tf_error)
8660 error ("creating array with negative size (%qE)", max);
8662 return error_mark_node;
8665 return compute_array_index_type (NULL_TREE, max);
8668 case TEMPLATE_TYPE_PARM:
8669 case TEMPLATE_TEMPLATE_PARM:
8670 case BOUND_TEMPLATE_TEMPLATE_PARM:
8671 case TEMPLATE_PARM_INDEX:
8673 int idx;
8674 int level;
8675 int levels;
8676 tree arg = NULL_TREE;
8678 r = NULL_TREE;
8680 gcc_assert (TREE_VEC_LENGTH (args) > 0);
8681 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8682 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
8683 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8685 idx = TEMPLATE_TYPE_IDX (t);
8686 level = TEMPLATE_TYPE_LEVEL (t);
8688 else
8690 idx = TEMPLATE_PARM_IDX (t);
8691 level = TEMPLATE_PARM_LEVEL (t);
8694 levels = TMPL_ARGS_DEPTH (args);
8695 if (level <= levels)
8697 arg = TMPL_ARG (args, level, idx);
8699 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
8700 /* See through ARGUMENT_PACK_SELECT arguments. */
8701 arg = ARGUMENT_PACK_SELECT_ARG (arg);
8704 if (arg == error_mark_node)
8705 return error_mark_node;
8706 else if (arg != NULL_TREE)
8708 if (ARGUMENT_PACK_P (arg))
8709 /* If ARG is an argument pack, we don't actually want to
8710 perform a substitution here, because substitutions
8711 for argument packs are only done
8712 element-by-element. We can get to this point when
8713 substituting the type of a non-type template
8714 parameter pack, when that type actually contains
8715 template parameter packs from an outer template, e.g.,
8717 template<typename... Types> struct A {
8718 template<Types... Values> struct B { };
8719 }; */
8720 return t;
8722 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
8724 int quals;
8725 gcc_assert (TYPE_P (arg));
8727 /* cv-quals from the template are discarded when
8728 substituting in a function or reference type. */
8729 if (TREE_CODE (arg) == FUNCTION_TYPE
8730 || TREE_CODE (arg) == METHOD_TYPE
8731 || TREE_CODE (arg) == REFERENCE_TYPE)
8732 quals = cp_type_quals (arg);
8733 else
8734 quals = cp_type_quals (arg) | cp_type_quals (t);
8736 return cp_build_qualified_type_real
8737 (arg, quals, complain | tf_ignore_bad_quals);
8739 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8741 /* We are processing a type constructed from a
8742 template template parameter. */
8743 tree argvec = tsubst (TYPE_TI_ARGS (t),
8744 args, complain, in_decl);
8745 if (argvec == error_mark_node)
8746 return error_mark_node;
8748 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
8749 are resolving nested-types in the signature of a
8750 member function templates. Otherwise ARG is a
8751 TEMPLATE_DECL and is the real template to be
8752 instantiated. */
8753 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8754 arg = TYPE_NAME (arg);
8756 r = lookup_template_class (arg,
8757 argvec, in_decl,
8758 DECL_CONTEXT (arg),
8759 /*entering_scope=*/0,
8760 complain);
8761 return cp_build_qualified_type_real
8762 (r, TYPE_QUALS (t), complain);
8764 else
8765 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
8766 return arg;
8769 if (level == 1)
8770 /* This can happen during the attempted tsubst'ing in
8771 unify. This means that we don't yet have any information
8772 about the template parameter in question. */
8773 return t;
8775 /* If we get here, we must have been looking at a parm for a
8776 more deeply nested template. Make a new version of this
8777 template parameter, but with a lower level. */
8778 switch (TREE_CODE (t))
8780 case TEMPLATE_TYPE_PARM:
8781 case TEMPLATE_TEMPLATE_PARM:
8782 case BOUND_TEMPLATE_TEMPLATE_PARM:
8783 if (cp_type_quals (t))
8785 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
8786 r = cp_build_qualified_type_real
8787 (r, cp_type_quals (t),
8788 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8789 ? tf_ignore_bad_quals : 0));
8791 else
8793 r = copy_type (t);
8794 TEMPLATE_TYPE_PARM_INDEX (r)
8795 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
8796 r, levels);
8797 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
8798 TYPE_MAIN_VARIANT (r) = r;
8799 TYPE_POINTER_TO (r) = NULL_TREE;
8800 TYPE_REFERENCE_TO (r) = NULL_TREE;
8802 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
8803 /* We have reduced the level of the template
8804 template parameter, but not the levels of its
8805 template parameters, so canonical_type_parameter
8806 will not be able to find the canonical template
8807 template parameter for this level. Thus, we
8808 require structural equality checking to compare
8809 TEMPLATE_TEMPLATE_PARMs. */
8810 SET_TYPE_STRUCTURAL_EQUALITY (r);
8811 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
8812 SET_TYPE_STRUCTURAL_EQUALITY (r);
8813 else
8814 TYPE_CANONICAL (r) = canonical_type_parameter (r);
8816 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8818 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
8819 complain, in_decl);
8820 if (argvec == error_mark_node)
8821 return error_mark_node;
8823 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
8824 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
8827 break;
8829 case TEMPLATE_PARM_INDEX:
8830 r = reduce_template_parm_level (t, type, levels);
8831 break;
8833 default:
8834 gcc_unreachable ();
8837 return r;
8840 case TREE_LIST:
8842 tree purpose, value, chain;
8844 if (t == void_list_node)
8845 return t;
8847 purpose = TREE_PURPOSE (t);
8848 if (purpose)
8850 purpose = tsubst (purpose, args, complain, in_decl);
8851 if (purpose == error_mark_node)
8852 return error_mark_node;
8854 value = TREE_VALUE (t);
8855 if (value)
8857 value = tsubst (value, args, complain, in_decl);
8858 if (value == error_mark_node)
8859 return error_mark_node;
8861 chain = TREE_CHAIN (t);
8862 if (chain && chain != void_type_node)
8864 chain = tsubst (chain, args, complain, in_decl);
8865 if (chain == error_mark_node)
8866 return error_mark_node;
8868 if (purpose == TREE_PURPOSE (t)
8869 && value == TREE_VALUE (t)
8870 && chain == TREE_CHAIN (t))
8871 return t;
8872 return hash_tree_cons (purpose, value, chain);
8875 case TREE_BINFO:
8876 /* We should never be tsubsting a binfo. */
8877 gcc_unreachable ();
8879 case TREE_VEC:
8880 /* A vector of template arguments. */
8881 gcc_assert (!type);
8882 return tsubst_template_args (t, args, complain, in_decl);
8884 case POINTER_TYPE:
8885 case REFERENCE_TYPE:
8887 enum tree_code code;
8889 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8890 return t;
8892 code = TREE_CODE (t);
8895 /* [temp.deduct]
8897 Type deduction may fail for any of the following
8898 reasons:
8900 -- Attempting to create a pointer to reference type.
8901 -- Attempting to create a reference to a reference type or
8902 a reference to void.
8904 Core issue 106 says that creating a reference to a reference
8905 during instantiation is no longer a cause for failure. We
8906 only enforce this check in strict C++98 mode. */
8907 if ((TREE_CODE (type) == REFERENCE_TYPE
8908 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
8909 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
8911 static location_t last_loc;
8913 /* We keep track of the last time we issued this error
8914 message to avoid spewing a ton of messages during a
8915 single bad template instantiation. */
8916 if (complain & tf_error
8917 #ifdef USE_MAPPED_LOCATION
8918 && last_loc != input_location
8919 #else
8920 && (last_loc.line != input_line
8921 || last_loc.file != input_filename)
8922 #endif
8925 if (TREE_CODE (type) == VOID_TYPE)
8926 error ("forming reference to void");
8927 else
8928 error ("forming %s to reference type %qT",
8929 (code == POINTER_TYPE) ? "pointer" : "reference",
8930 type);
8931 last_loc = input_location;
8934 return error_mark_node;
8936 else if (code == POINTER_TYPE)
8938 r = build_pointer_type (type);
8939 if (TREE_CODE (type) == METHOD_TYPE)
8940 r = build_ptrmemfunc_type (r);
8942 else if (TREE_CODE (type) == REFERENCE_TYPE)
8943 /* In C++0x, during template argument substitution, when there is an
8944 attempt to create a reference to a reference type, reference
8945 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
8947 "If a template-argument for a template-parameter T names a type
8948 that is a reference to a type A, an attempt to create the type
8949 'lvalue reference to cv T' creates the type 'lvalue reference to
8950 A,' while an attempt to create the type type rvalue reference to
8951 cv T' creates the type T"
8953 r = cp_build_reference_type
8954 (TREE_TYPE (type),
8955 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
8956 else
8957 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
8958 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8960 if (r != error_mark_node)
8961 /* Will this ever be needed for TYPE_..._TO values? */
8962 layout_type (r);
8964 return r;
8966 case OFFSET_TYPE:
8968 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
8969 if (r == error_mark_node || !IS_AGGR_TYPE (r))
8971 /* [temp.deduct]
8973 Type deduction may fail for any of the following
8974 reasons:
8976 -- Attempting to create "pointer to member of T" when T
8977 is not a class type. */
8978 if (complain & tf_error)
8979 error ("creating pointer to member of non-class type %qT", r);
8980 return error_mark_node;
8982 if (TREE_CODE (type) == REFERENCE_TYPE)
8984 if (complain & tf_error)
8985 error ("creating pointer to member reference type %qT", type);
8986 return error_mark_node;
8988 if (TREE_CODE (type) == VOID_TYPE)
8990 if (complain & tf_error)
8991 error ("creating pointer to member of type void");
8992 return error_mark_node;
8994 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
8995 if (TREE_CODE (type) == FUNCTION_TYPE)
8997 /* The type of the implicit object parameter gets its
8998 cv-qualifiers from the FUNCTION_TYPE. */
8999 tree method_type;
9000 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9001 cp_type_quals (type));
9002 tree memptr;
9003 method_type = build_method_type_directly (this_type,
9004 TREE_TYPE (type),
9005 TYPE_ARG_TYPES (type));
9006 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9007 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9008 complain);
9010 else
9011 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9012 TYPE_QUALS (t),
9013 complain);
9015 case FUNCTION_TYPE:
9016 case METHOD_TYPE:
9018 tree fntype;
9019 tree specs;
9020 fntype = tsubst_function_type (t, args, complain, in_decl);
9021 if (fntype == error_mark_node)
9022 return error_mark_node;
9024 /* Substitute the exception specification. */
9025 specs = tsubst_exception_specification (t, args, complain,
9026 in_decl);
9027 if (specs == error_mark_node)
9028 return error_mark_node;
9029 if (specs)
9030 fntype = build_exception_variant (fntype, specs);
9031 return fntype;
9033 case ARRAY_TYPE:
9035 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9036 if (domain == error_mark_node)
9037 return error_mark_node;
9039 /* As an optimization, we avoid regenerating the array type if
9040 it will obviously be the same as T. */
9041 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9042 return t;
9044 /* These checks should match the ones in grokdeclarator.
9046 [temp.deduct]
9048 The deduction may fail for any of the following reasons:
9050 -- Attempting to create an array with an element type that
9051 is void, a function type, or a reference type, or [DR337]
9052 an abstract class type. */
9053 if (TREE_CODE (type) == VOID_TYPE
9054 || TREE_CODE (type) == FUNCTION_TYPE
9055 || TREE_CODE (type) == REFERENCE_TYPE)
9057 if (complain & tf_error)
9058 error ("creating array of %qT", type);
9059 return error_mark_node;
9061 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
9063 if (complain & tf_error)
9064 error ("creating array of %qT, which is an abstract class type",
9065 type);
9066 return error_mark_node;
9069 r = build_cplus_array_type (type, domain);
9071 if (TYPE_USER_ALIGN (t))
9073 TYPE_ALIGN (r) = TYPE_ALIGN (t);
9074 TYPE_USER_ALIGN (r) = 1;
9077 return r;
9080 case PLUS_EXPR:
9081 case MINUS_EXPR:
9083 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9084 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9086 if (e1 == error_mark_node || e2 == error_mark_node)
9087 return error_mark_node;
9089 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
9092 case NEGATE_EXPR:
9093 case NOP_EXPR:
9095 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9096 if (e == error_mark_node)
9097 return error_mark_node;
9099 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
9102 case TYPENAME_TYPE:
9104 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9105 in_decl, /*entering_scope=*/1);
9106 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
9107 complain, in_decl);
9109 if (ctx == error_mark_node || f == error_mark_node)
9110 return error_mark_node;
9112 if (!IS_AGGR_TYPE (ctx))
9114 if (complain & tf_error)
9115 error ("%qT is not a class, struct, or union type", ctx);
9116 return error_mark_node;
9118 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9120 /* Normally, make_typename_type does not require that the CTX
9121 have complete type in order to allow things like:
9123 template <class T> struct S { typename S<T>::X Y; };
9125 But, such constructs have already been resolved by this
9126 point, so here CTX really should have complete type, unless
9127 it's a partial instantiation. */
9128 ctx = complete_type (ctx);
9129 if (!COMPLETE_TYPE_P (ctx))
9131 if (complain & tf_error)
9132 cxx_incomplete_type_error (NULL_TREE, ctx);
9133 return error_mark_node;
9137 f = make_typename_type (ctx, f, typename_type,
9138 (complain & tf_error) | tf_keep_type_decl);
9139 if (f == error_mark_node)
9140 return f;
9141 if (TREE_CODE (f) == TYPE_DECL)
9143 complain |= tf_ignore_bad_quals;
9144 f = TREE_TYPE (f);
9147 if (TREE_CODE (f) != TYPENAME_TYPE)
9149 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9150 error ("%qT resolves to %qT, which is not an enumeration type",
9151 t, f);
9152 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9153 error ("%qT resolves to %qT, which is is not a class type",
9154 t, f);
9157 return cp_build_qualified_type_real
9158 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9161 case UNBOUND_CLASS_TEMPLATE:
9163 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9164 in_decl, /*entering_scope=*/1);
9165 tree name = TYPE_IDENTIFIER (t);
9166 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9168 if (ctx == error_mark_node || name == error_mark_node)
9169 return error_mark_node;
9171 if (parm_list)
9172 parm_list = tsubst_template_parms (parm_list, args, complain);
9173 return make_unbound_class_template (ctx, name, parm_list, complain);
9176 case INDIRECT_REF:
9177 case ADDR_EXPR:
9178 case CALL_EXPR:
9179 gcc_unreachable ();
9181 case ARRAY_REF:
9183 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9184 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9185 /*integral_constant_expression_p=*/false);
9186 if (e1 == error_mark_node || e2 == error_mark_node)
9187 return error_mark_node;
9189 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9192 case SCOPE_REF:
9194 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9195 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9196 if (e1 == error_mark_node || e2 == error_mark_node)
9197 return error_mark_node;
9199 return build_qualified_name (/*type=*/NULL_TREE,
9200 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9203 case TYPEOF_TYPE:
9205 tree type;
9207 type = finish_typeof (tsubst_expr
9208 (TYPEOF_TYPE_EXPR (t), args,
9209 complain, in_decl,
9210 /*integral_constant_expression_p=*/false));
9211 return cp_build_qualified_type_real (type,
9212 cp_type_quals (t)
9213 | cp_type_quals (type),
9214 complain);
9217 case DECLTYPE_TYPE:
9219 tree type;
9221 type =
9222 finish_decltype_type (tsubst_expr
9223 (DECLTYPE_TYPE_EXPR (t), args,
9224 complain, in_decl,
9225 /*integral_constant_expression_p=*/false),
9226 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9227 return cp_build_qualified_type_real (type,
9228 cp_type_quals (t)
9229 | cp_type_quals (type),
9230 complain);
9233 case TYPE_ARGUMENT_PACK:
9234 case NONTYPE_ARGUMENT_PACK:
9236 tree r = make_node (TREE_CODE (t));
9237 tree packed_out =
9238 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9239 args,
9240 complain,
9241 in_decl);
9242 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9244 /* For template nontype argument packs, also substitute into
9245 the type. */
9246 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9247 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9249 return r;
9251 break;
9253 default:
9254 sorry ("use of %qs in template",
9255 tree_code_name [(int) TREE_CODE (t)]);
9256 return error_mark_node;
9260 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9261 type of the expression on the left-hand side of the "." or "->"
9262 operator. */
9264 static tree
9265 tsubst_baselink (tree baselink, tree object_type,
9266 tree args, tsubst_flags_t complain, tree in_decl)
9268 tree name;
9269 tree qualifying_scope;
9270 tree fns;
9271 tree optype;
9272 tree template_args = 0;
9273 bool template_id_p = false;
9275 /* A baselink indicates a function from a base class. Both the
9276 BASELINK_ACCESS_BINFO and the base class referenced may
9277 indicate bases of the template class, rather than the
9278 instantiated class. In addition, lookups that were not
9279 ambiguous before may be ambiguous now. Therefore, we perform
9280 the lookup again. */
9281 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9282 qualifying_scope = tsubst (qualifying_scope, args,
9283 complain, in_decl);
9284 fns = BASELINK_FUNCTIONS (baselink);
9285 optype = BASELINK_OPTYPE (baselink);
9286 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9288 template_id_p = true;
9289 template_args = TREE_OPERAND (fns, 1);
9290 fns = TREE_OPERAND (fns, 0);
9291 if (template_args)
9292 template_args = tsubst_template_args (template_args, args,
9293 complain, in_decl);
9295 name = DECL_NAME (get_first_fn (fns));
9296 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9298 /* If lookup found a single function, mark it as used at this
9299 point. (If it lookup found multiple functions the one selected
9300 later by overload resolution will be marked as used at that
9301 point.) */
9302 if (BASELINK_P (baselink))
9303 fns = BASELINK_FUNCTIONS (baselink);
9304 if (!template_id_p && !really_overloaded_fn (fns))
9305 mark_used (OVL_CURRENT (fns));
9307 /* Add back the template arguments, if present. */
9308 if (BASELINK_P (baselink) && template_id_p)
9309 BASELINK_FUNCTIONS (baselink)
9310 = build_nt (TEMPLATE_ID_EXPR,
9311 BASELINK_FUNCTIONS (baselink),
9312 template_args);
9313 /* Update the conversion operator type. */
9314 BASELINK_OPTYPE (baselink)
9315 = tsubst (optype, args, complain, in_decl);
9317 if (!object_type)
9318 object_type = current_class_type;
9319 return adjust_result_of_qualified_name_lookup (baselink,
9320 qualifying_scope,
9321 object_type);
9324 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9325 true if the qualified-id will be a postfix-expression in-and-of
9326 itself; false if more of the postfix-expression follows the
9327 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9328 of "&". */
9330 static tree
9331 tsubst_qualified_id (tree qualified_id, tree args,
9332 tsubst_flags_t complain, tree in_decl,
9333 bool done, bool address_p)
9335 tree expr;
9336 tree scope;
9337 tree name;
9338 bool is_template;
9339 tree template_args;
9341 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9343 /* Figure out what name to look up. */
9344 name = TREE_OPERAND (qualified_id, 1);
9345 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9347 is_template = true;
9348 template_args = TREE_OPERAND (name, 1);
9349 if (template_args)
9350 template_args = tsubst_template_args (template_args, args,
9351 complain, in_decl);
9352 name = TREE_OPERAND (name, 0);
9354 else
9356 is_template = false;
9357 template_args = NULL_TREE;
9360 /* Substitute into the qualifying scope. When there are no ARGS, we
9361 are just trying to simplify a non-dependent expression. In that
9362 case the qualifying scope may be dependent, and, in any case,
9363 substituting will not help. */
9364 scope = TREE_OPERAND (qualified_id, 0);
9365 if (args)
9367 scope = tsubst (scope, args, complain, in_decl);
9368 expr = tsubst_copy (name, args, complain, in_decl);
9370 else
9371 expr = name;
9373 if (dependent_type_p (scope))
9374 return build_qualified_name (/*type=*/NULL_TREE,
9375 scope, expr,
9376 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9378 if (!BASELINK_P (name) && !DECL_P (expr))
9380 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9381 /* If this were actually a destructor call, it would have been
9382 parsed as such by the parser. */
9383 expr = error_mark_node;
9384 else
9385 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9386 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9387 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9389 if (complain & tf_error)
9391 error ("dependent-name %qE is parsed as a non-type, but "
9392 "instantiation yields a type", qualified_id);
9393 inform ("say %<typename %E%> if a type is meant", qualified_id);
9395 return error_mark_node;
9399 if (DECL_P (expr))
9401 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9402 scope);
9403 /* Remember that there was a reference to this entity. */
9404 mark_used (expr);
9407 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9409 if (complain & tf_error)
9410 qualified_name_lookup_error (scope,
9411 TREE_OPERAND (qualified_id, 1),
9412 expr);
9413 return error_mark_node;
9416 if (is_template)
9417 expr = lookup_template_function (expr, template_args);
9419 if (expr == error_mark_node && complain & tf_error)
9420 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9421 expr);
9422 else if (TYPE_P (scope))
9424 expr = (adjust_result_of_qualified_name_lookup
9425 (expr, scope, current_class_type));
9426 expr = (finish_qualified_id_expr
9427 (scope, expr, done, address_p,
9428 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9429 /*template_arg_p=*/false));
9432 /* Expressions do not generally have reference type. */
9433 if (TREE_CODE (expr) != SCOPE_REF
9434 /* However, if we're about to form a pointer-to-member, we just
9435 want the referenced member referenced. */
9436 && TREE_CODE (expr) != OFFSET_REF)
9437 expr = convert_from_reference (expr);
9439 return expr;
9442 /* Like tsubst, but deals with expressions. This function just replaces
9443 template parms; to finish processing the resultant expression, use
9444 tsubst_expr. */
9446 static tree
9447 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9449 enum tree_code code;
9450 tree r;
9452 if (t == NULL_TREE || t == error_mark_node)
9453 return t;
9455 code = TREE_CODE (t);
9457 switch (code)
9459 case PARM_DECL:
9460 r = retrieve_local_specialization (t);
9461 gcc_assert (r != NULL);
9462 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
9463 r = ARGUMENT_PACK_SELECT_ARG (r);
9464 mark_used (r);
9465 return r;
9467 case CONST_DECL:
9469 tree enum_type;
9470 tree v;
9472 if (DECL_TEMPLATE_PARM_P (t))
9473 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
9474 /* There is no need to substitute into namespace-scope
9475 enumerators. */
9476 if (DECL_NAMESPACE_SCOPE_P (t))
9477 return t;
9478 /* If ARGS is NULL, then T is known to be non-dependent. */
9479 if (args == NULL_TREE)
9480 return integral_constant_value (t);
9482 /* Unfortunately, we cannot just call lookup_name here.
9483 Consider:
9485 template <int I> int f() {
9486 enum E { a = I };
9487 struct S { void g() { E e = a; } };
9490 When we instantiate f<7>::S::g(), say, lookup_name is not
9491 clever enough to find f<7>::a. */
9492 enum_type
9493 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
9494 /*entering_scope=*/0);
9496 for (v = TYPE_VALUES (enum_type);
9497 v != NULL_TREE;
9498 v = TREE_CHAIN (v))
9499 if (TREE_PURPOSE (v) == DECL_NAME (t))
9500 return TREE_VALUE (v);
9502 /* We didn't find the name. That should never happen; if
9503 name-lookup found it during preliminary parsing, we
9504 should find it again here during instantiation. */
9505 gcc_unreachable ();
9507 return t;
9509 case FIELD_DECL:
9510 if (DECL_CONTEXT (t))
9512 tree ctx;
9514 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
9515 /*entering_scope=*/1);
9516 if (ctx != DECL_CONTEXT (t))
9518 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
9519 if (!r)
9521 if (complain & tf_error)
9522 error ("using invalid field %qD", t);
9523 return error_mark_node;
9525 return r;
9529 return t;
9531 case VAR_DECL:
9532 case FUNCTION_DECL:
9533 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9534 || local_variable_p (t))
9535 t = tsubst (t, args, complain, in_decl);
9536 mark_used (t);
9537 return t;
9539 case BASELINK:
9540 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
9542 case TEMPLATE_DECL:
9543 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9544 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
9545 args, complain, in_decl);
9546 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
9547 return tsubst (t, args, complain, in_decl);
9548 else if (DECL_CLASS_SCOPE_P (t)
9549 && uses_template_parms (DECL_CONTEXT (t)))
9551 /* Template template argument like the following example need
9552 special treatment:
9554 template <template <class> class TT> struct C {};
9555 template <class T> struct D {
9556 template <class U> struct E {};
9557 C<E> c; // #1
9559 D<int> d; // #2
9561 We are processing the template argument `E' in #1 for
9562 the template instantiation #2. Originally, `E' is a
9563 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9564 have to substitute this with one having context `D<int>'. */
9566 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
9567 return lookup_field (context, DECL_NAME(t), 0, false);
9569 else
9570 /* Ordinary template template argument. */
9571 return t;
9573 case CAST_EXPR:
9574 case REINTERPRET_CAST_EXPR:
9575 case CONST_CAST_EXPR:
9576 case STATIC_CAST_EXPR:
9577 case DYNAMIC_CAST_EXPR:
9578 case NOP_EXPR:
9579 return build1
9580 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9581 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9583 case SIZEOF_EXPR:
9584 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
9586 /* We only want to compute the number of arguments. */
9587 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
9588 complain, in_decl);
9589 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
9591 /* Fall through */
9593 case INDIRECT_REF:
9594 case NEGATE_EXPR:
9595 case TRUTH_NOT_EXPR:
9596 case BIT_NOT_EXPR:
9597 case ADDR_EXPR:
9598 case UNARY_PLUS_EXPR: /* Unary + */
9599 case ALIGNOF_EXPR:
9600 case ARROW_EXPR:
9601 case THROW_EXPR:
9602 case TYPEID_EXPR:
9603 case REALPART_EXPR:
9604 case IMAGPART_EXPR:
9605 return build1
9606 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9607 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9609 case COMPONENT_REF:
9611 tree object;
9612 tree name;
9614 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
9615 name = TREE_OPERAND (t, 1);
9616 if (TREE_CODE (name) == BIT_NOT_EXPR)
9618 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9619 complain, in_decl);
9620 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9622 else if (TREE_CODE (name) == SCOPE_REF
9623 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
9625 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
9626 complain, in_decl);
9627 name = TREE_OPERAND (name, 1);
9628 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9629 complain, in_decl);
9630 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9631 name = build_qualified_name (/*type=*/NULL_TREE,
9632 base, name,
9633 /*template_p=*/false);
9635 else if (TREE_CODE (name) == BASELINK)
9636 name = tsubst_baselink (name,
9637 non_reference (TREE_TYPE (object)),
9638 args, complain,
9639 in_decl);
9640 else
9641 name = tsubst_copy (name, args, complain, in_decl);
9642 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
9645 case PLUS_EXPR:
9646 case MINUS_EXPR:
9647 case MULT_EXPR:
9648 case TRUNC_DIV_EXPR:
9649 case CEIL_DIV_EXPR:
9650 case FLOOR_DIV_EXPR:
9651 case ROUND_DIV_EXPR:
9652 case EXACT_DIV_EXPR:
9653 case BIT_AND_EXPR:
9654 case BIT_IOR_EXPR:
9655 case BIT_XOR_EXPR:
9656 case TRUNC_MOD_EXPR:
9657 case FLOOR_MOD_EXPR:
9658 case TRUTH_ANDIF_EXPR:
9659 case TRUTH_ORIF_EXPR:
9660 case TRUTH_AND_EXPR:
9661 case TRUTH_OR_EXPR:
9662 case RSHIFT_EXPR:
9663 case LSHIFT_EXPR:
9664 case RROTATE_EXPR:
9665 case LROTATE_EXPR:
9666 case EQ_EXPR:
9667 case NE_EXPR:
9668 case MAX_EXPR:
9669 case MIN_EXPR:
9670 case LE_EXPR:
9671 case GE_EXPR:
9672 case LT_EXPR:
9673 case GT_EXPR:
9674 case COMPOUND_EXPR:
9675 case DOTSTAR_EXPR:
9676 case MEMBER_REF:
9677 case PREDECREMENT_EXPR:
9678 case PREINCREMENT_EXPR:
9679 case POSTDECREMENT_EXPR:
9680 case POSTINCREMENT_EXPR:
9681 return build_nt
9682 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9683 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9685 case SCOPE_REF:
9686 return build_qualified_name (/*type=*/NULL_TREE,
9687 tsubst_copy (TREE_OPERAND (t, 0),
9688 args, complain, in_decl),
9689 tsubst_copy (TREE_OPERAND (t, 1),
9690 args, complain, in_decl),
9691 QUALIFIED_NAME_IS_TEMPLATE (t));
9693 case ARRAY_REF:
9694 return build_nt
9695 (ARRAY_REF,
9696 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9697 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9698 NULL_TREE, NULL_TREE);
9700 case CALL_EXPR:
9702 int n = VL_EXP_OPERAND_LENGTH (t);
9703 tree result = build_vl_exp (CALL_EXPR, n);
9704 int i;
9705 for (i = 0; i < n; i++)
9706 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
9707 complain, in_decl);
9708 return result;
9711 case COND_EXPR:
9712 case MODOP_EXPR:
9713 case PSEUDO_DTOR_EXPR:
9715 r = build_nt
9716 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9717 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9718 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9719 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9720 return r;
9723 case NEW_EXPR:
9725 r = build_nt
9726 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9727 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9728 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9729 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
9730 return r;
9733 case DELETE_EXPR:
9735 r = build_nt
9736 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9737 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9738 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
9739 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
9740 return r;
9743 case TEMPLATE_ID_EXPR:
9745 /* Substituted template arguments */
9746 tree fn = TREE_OPERAND (t, 0);
9747 tree targs = TREE_OPERAND (t, 1);
9749 fn = tsubst_copy (fn, args, complain, in_decl);
9750 if (targs)
9751 targs = tsubst_template_args (targs, args, complain, in_decl);
9753 return lookup_template_function (fn, targs);
9756 case TREE_LIST:
9758 tree purpose, value, chain;
9760 if (t == void_list_node)
9761 return t;
9763 purpose = TREE_PURPOSE (t);
9764 if (purpose)
9765 purpose = tsubst_copy (purpose, args, complain, in_decl);
9766 value = TREE_VALUE (t);
9767 if (value)
9768 value = tsubst_copy (value, args, complain, in_decl);
9769 chain = TREE_CHAIN (t);
9770 if (chain && chain != void_type_node)
9771 chain = tsubst_copy (chain, args, complain, in_decl);
9772 if (purpose == TREE_PURPOSE (t)
9773 && value == TREE_VALUE (t)
9774 && chain == TREE_CHAIN (t))
9775 return t;
9776 return tree_cons (purpose, value, chain);
9779 case RECORD_TYPE:
9780 case UNION_TYPE:
9781 case ENUMERAL_TYPE:
9782 case INTEGER_TYPE:
9783 case TEMPLATE_TYPE_PARM:
9784 case TEMPLATE_TEMPLATE_PARM:
9785 case BOUND_TEMPLATE_TEMPLATE_PARM:
9786 case TEMPLATE_PARM_INDEX:
9787 case POINTER_TYPE:
9788 case REFERENCE_TYPE:
9789 case OFFSET_TYPE:
9790 case FUNCTION_TYPE:
9791 case METHOD_TYPE:
9792 case ARRAY_TYPE:
9793 case TYPENAME_TYPE:
9794 case UNBOUND_CLASS_TEMPLATE:
9795 case TYPEOF_TYPE:
9796 case DECLTYPE_TYPE:
9797 case TYPE_DECL:
9798 return tsubst (t, args, complain, in_decl);
9800 case IDENTIFIER_NODE:
9801 if (IDENTIFIER_TYPENAME_P (t))
9803 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9804 return mangle_conv_op_name_for_type (new_type);
9806 else
9807 return t;
9809 case CONSTRUCTOR:
9810 /* This is handled by tsubst_copy_and_build. */
9811 gcc_unreachable ();
9813 case VA_ARG_EXPR:
9814 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
9815 in_decl),
9816 tsubst (TREE_TYPE (t), args, complain, in_decl));
9818 case CLEANUP_POINT_EXPR:
9819 /* We shouldn't have built any of these during initial template
9820 generation. Instead, they should be built during instantiation
9821 in response to the saved STMT_IS_FULL_EXPR_P setting. */
9822 gcc_unreachable ();
9824 case OFFSET_REF:
9825 mark_used (TREE_OPERAND (t, 1));
9826 return t;
9828 case EXPR_PACK_EXPANSION:
9829 error ("invalid use of pack expansion expression");
9830 return error_mark_node;
9832 case NONTYPE_ARGUMENT_PACK:
9833 error ("use %<...%> to expand argument pack");
9834 return error_mark_node;
9836 default:
9837 return t;
9841 /* Like tsubst_copy, but specifically for OpenMP clauses. */
9843 static tree
9844 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
9845 tree in_decl)
9847 tree new_clauses = NULL, nc, oc;
9849 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
9851 nc = copy_node (oc);
9852 OMP_CLAUSE_CHAIN (nc) = new_clauses;
9853 new_clauses = nc;
9855 switch (OMP_CLAUSE_CODE (nc))
9857 case OMP_CLAUSE_PRIVATE:
9858 case OMP_CLAUSE_SHARED:
9859 case OMP_CLAUSE_FIRSTPRIVATE:
9860 case OMP_CLAUSE_LASTPRIVATE:
9861 case OMP_CLAUSE_REDUCTION:
9862 case OMP_CLAUSE_COPYIN:
9863 case OMP_CLAUSE_COPYPRIVATE:
9864 case OMP_CLAUSE_IF:
9865 case OMP_CLAUSE_NUM_THREADS:
9866 case OMP_CLAUSE_SCHEDULE:
9867 OMP_CLAUSE_OPERAND (nc, 0)
9868 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
9869 in_decl, /*integral_constant_expression_p=*/false);
9870 break;
9871 case OMP_CLAUSE_NOWAIT:
9872 case OMP_CLAUSE_ORDERED:
9873 case OMP_CLAUSE_DEFAULT:
9874 break;
9875 default:
9876 gcc_unreachable ();
9880 return finish_omp_clauses (nreverse (new_clauses));
9883 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
9885 static tree
9886 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
9887 tree in_decl)
9889 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
9891 tree purpose, value, chain;
9893 if (t == NULL)
9894 return t;
9896 if (TREE_CODE (t) != TREE_LIST)
9897 return tsubst_copy_and_build (t, args, complain, in_decl,
9898 /*function_p=*/false,
9899 /*integral_constant_expression_p=*/false);
9901 if (t == void_list_node)
9902 return t;
9904 purpose = TREE_PURPOSE (t);
9905 if (purpose)
9906 purpose = RECUR (purpose);
9907 value = TREE_VALUE (t);
9908 if (value)
9909 value = RECUR (value);
9910 chain = TREE_CHAIN (t);
9911 if (chain && chain != void_type_node)
9912 chain = RECUR (chain);
9913 return tree_cons (purpose, value, chain);
9914 #undef RECUR
9917 /* Like tsubst_copy for expressions, etc. but also does semantic
9918 processing. */
9920 static tree
9921 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
9922 bool integral_constant_expression_p)
9924 #define RECUR(NODE) \
9925 tsubst_expr ((NODE), args, complain, in_decl, \
9926 integral_constant_expression_p)
9928 tree stmt, tmp;
9930 if (t == NULL_TREE || t == error_mark_node)
9931 return t;
9933 if (EXPR_HAS_LOCATION (t))
9934 input_location = EXPR_LOCATION (t);
9935 if (STATEMENT_CODE_P (TREE_CODE (t)))
9936 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
9938 switch (TREE_CODE (t))
9940 case STATEMENT_LIST:
9942 tree_stmt_iterator i;
9943 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
9944 RECUR (tsi_stmt (i));
9945 break;
9948 case CTOR_INITIALIZER:
9949 finish_mem_initializers (tsubst_initializer_list
9950 (TREE_OPERAND (t, 0), args));
9951 break;
9953 case RETURN_EXPR:
9954 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
9955 break;
9957 case EXPR_STMT:
9958 tmp = RECUR (EXPR_STMT_EXPR (t));
9959 if (EXPR_STMT_STMT_EXPR_RESULT (t))
9960 finish_stmt_expr_expr (tmp, cur_stmt_expr);
9961 else
9962 finish_expr_stmt (tmp);
9963 break;
9965 case USING_STMT:
9966 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9967 break;
9969 case DECL_EXPR:
9971 tree decl;
9972 tree init;
9974 decl = DECL_EXPR_DECL (t);
9975 if (TREE_CODE (decl) == LABEL_DECL)
9976 finish_label_decl (DECL_NAME (decl));
9977 else if (TREE_CODE (decl) == USING_DECL)
9979 tree scope = USING_DECL_SCOPE (decl);
9980 tree name = DECL_NAME (decl);
9981 tree decl;
9983 scope = RECUR (scope);
9984 decl = lookup_qualified_name (scope, name,
9985 /*is_type_p=*/false,
9986 /*complain=*/false);
9987 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
9988 qualified_name_lookup_error (scope, name, decl);
9989 else
9990 do_local_using_decl (decl, scope, name);
9992 else
9994 init = DECL_INITIAL (decl);
9995 decl = tsubst (decl, args, complain, in_decl);
9996 if (decl != error_mark_node)
9998 /* By marking the declaration as instantiated, we avoid
9999 trying to instantiate it. Since instantiate_decl can't
10000 handle local variables, and since we've already done
10001 all that needs to be done, that's the right thing to
10002 do. */
10003 if (TREE_CODE (decl) == VAR_DECL)
10004 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10005 if (TREE_CODE (decl) == VAR_DECL
10006 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
10007 /* Anonymous aggregates are a special case. */
10008 finish_anon_union (decl);
10009 else
10011 maybe_push_decl (decl);
10012 if (TREE_CODE (decl) == VAR_DECL
10013 && DECL_PRETTY_FUNCTION_P (decl))
10015 /* For __PRETTY_FUNCTION__ we have to adjust the
10016 initializer. */
10017 const char *const name
10018 = cxx_printable_name (current_function_decl, 2);
10019 init = cp_fname_init (name, &TREE_TYPE (decl));
10021 else
10023 tree t = RECUR (init);
10025 if (init && !t)
10026 /* If we had an initializer but it
10027 instantiated to nothing,
10028 value-initialize the object. This will
10029 only occur when the initializer was a
10030 pack expansion where the parameter packs
10031 used in that expansion were of length
10032 zero. */
10033 init = build_default_init (TREE_TYPE (decl),
10034 NULL_TREE);
10035 else
10036 init = t;
10039 finish_decl (decl, init, NULL_TREE);
10044 /* A DECL_EXPR can also be used as an expression, in the condition
10045 clause of an if/for/while construct. */
10046 return decl;
10049 case FOR_STMT:
10050 stmt = begin_for_stmt ();
10051 RECUR (FOR_INIT_STMT (t));
10052 finish_for_init_stmt (stmt);
10053 tmp = RECUR (FOR_COND (t));
10054 finish_for_cond (tmp, stmt);
10055 tmp = RECUR (FOR_EXPR (t));
10056 finish_for_expr (tmp, stmt);
10057 RECUR (FOR_BODY (t));
10058 finish_for_stmt (stmt);
10059 break;
10061 case WHILE_STMT:
10062 stmt = begin_while_stmt ();
10063 tmp = RECUR (WHILE_COND (t));
10064 finish_while_stmt_cond (tmp, stmt);
10065 RECUR (WHILE_BODY (t));
10066 finish_while_stmt (stmt);
10067 break;
10069 case DO_STMT:
10070 stmt = begin_do_stmt ();
10071 RECUR (DO_BODY (t));
10072 finish_do_body (stmt);
10073 tmp = RECUR (DO_COND (t));
10074 finish_do_stmt (tmp, stmt);
10075 break;
10077 case IF_STMT:
10078 stmt = begin_if_stmt ();
10079 tmp = RECUR (IF_COND (t));
10080 finish_if_stmt_cond (tmp, stmt);
10081 RECUR (THEN_CLAUSE (t));
10082 finish_then_clause (stmt);
10084 if (ELSE_CLAUSE (t))
10086 begin_else_clause (stmt);
10087 RECUR (ELSE_CLAUSE (t));
10088 finish_else_clause (stmt);
10091 finish_if_stmt (stmt);
10092 break;
10094 case BIND_EXPR:
10095 if (BIND_EXPR_BODY_BLOCK (t))
10096 stmt = begin_function_body ();
10097 else
10098 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
10099 ? BCS_TRY_BLOCK : 0);
10101 RECUR (BIND_EXPR_BODY (t));
10103 if (BIND_EXPR_BODY_BLOCK (t))
10104 finish_function_body (stmt);
10105 else
10106 finish_compound_stmt (stmt);
10107 break;
10109 case BREAK_STMT:
10110 finish_break_stmt ();
10111 break;
10113 case CONTINUE_STMT:
10114 finish_continue_stmt ();
10115 break;
10117 case SWITCH_STMT:
10118 stmt = begin_switch_stmt ();
10119 tmp = RECUR (SWITCH_STMT_COND (t));
10120 finish_switch_cond (tmp, stmt);
10121 RECUR (SWITCH_STMT_BODY (t));
10122 finish_switch_stmt (stmt);
10123 break;
10125 case CASE_LABEL_EXPR:
10126 finish_case_label (RECUR (CASE_LOW (t)),
10127 RECUR (CASE_HIGH (t)));
10128 break;
10130 case LABEL_EXPR:
10131 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
10132 break;
10134 case GOTO_EXPR:
10135 tmp = GOTO_DESTINATION (t);
10136 if (TREE_CODE (tmp) != LABEL_DECL)
10137 /* Computed goto's must be tsubst'd into. On the other hand,
10138 non-computed gotos must not be; the identifier in question
10139 will have no binding. */
10140 tmp = RECUR (tmp);
10141 else
10142 tmp = DECL_NAME (tmp);
10143 finish_goto_stmt (tmp);
10144 break;
10146 case ASM_EXPR:
10147 tmp = finish_asm_stmt
10148 (ASM_VOLATILE_P (t),
10149 RECUR (ASM_STRING (t)),
10150 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10151 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10152 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10154 tree asm_expr = tmp;
10155 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10156 asm_expr = TREE_OPERAND (asm_expr, 0);
10157 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10159 break;
10161 case TRY_BLOCK:
10162 if (CLEANUP_P (t))
10164 stmt = begin_try_block ();
10165 RECUR (TRY_STMTS (t));
10166 finish_cleanup_try_block (stmt);
10167 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10169 else
10171 tree compound_stmt = NULL_TREE;
10173 if (FN_TRY_BLOCK_P (t))
10174 stmt = begin_function_try_block (&compound_stmt);
10175 else
10176 stmt = begin_try_block ();
10178 RECUR (TRY_STMTS (t));
10180 if (FN_TRY_BLOCK_P (t))
10181 finish_function_try_block (stmt);
10182 else
10183 finish_try_block (stmt);
10185 RECUR (TRY_HANDLERS (t));
10186 if (FN_TRY_BLOCK_P (t))
10187 finish_function_handler_sequence (stmt, compound_stmt);
10188 else
10189 finish_handler_sequence (stmt);
10191 break;
10193 case HANDLER:
10195 tree decl = HANDLER_PARMS (t);
10197 if (decl)
10199 decl = tsubst (decl, args, complain, in_decl);
10200 /* Prevent instantiate_decl from trying to instantiate
10201 this variable. We've already done all that needs to be
10202 done. */
10203 if (decl != error_mark_node)
10204 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10206 stmt = begin_handler ();
10207 finish_handler_parms (decl, stmt);
10208 RECUR (HANDLER_BODY (t));
10209 finish_handler (stmt);
10211 break;
10213 case TAG_DEFN:
10214 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
10215 break;
10217 case STATIC_ASSERT:
10219 tree condition =
10220 tsubst_expr (STATIC_ASSERT_CONDITION (t),
10221 args,
10222 complain, in_decl,
10223 /*integral_constant_expression_p=*/true);
10224 finish_static_assert (condition,
10225 STATIC_ASSERT_MESSAGE (t),
10226 STATIC_ASSERT_SOURCE_LOCATION (t),
10227 /*member_p=*/false);
10229 break;
10231 case OMP_PARALLEL:
10232 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
10233 args, complain, in_decl);
10234 stmt = begin_omp_parallel ();
10235 RECUR (OMP_PARALLEL_BODY (t));
10236 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
10237 = OMP_PARALLEL_COMBINED (t);
10238 break;
10240 case OMP_FOR:
10242 tree clauses, decl, init, cond, incr, body, pre_body;
10244 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
10245 args, complain, in_decl);
10246 init = OMP_FOR_INIT (t);
10247 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10248 decl = RECUR (TREE_OPERAND (init, 0));
10249 init = RECUR (TREE_OPERAND (init, 1));
10250 cond = RECUR (OMP_FOR_COND (t));
10251 incr = RECUR (OMP_FOR_INCR (t));
10253 stmt = begin_omp_structured_block ();
10255 pre_body = push_stmt_list ();
10256 RECUR (OMP_FOR_PRE_BODY (t));
10257 pre_body = pop_stmt_list (pre_body);
10259 body = push_stmt_list ();
10260 RECUR (OMP_FOR_BODY (t));
10261 body = pop_stmt_list (body);
10263 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
10264 pre_body);
10265 if (t)
10266 OMP_FOR_CLAUSES (t) = clauses;
10268 add_stmt (finish_omp_structured_block (stmt));
10270 break;
10272 case OMP_SECTIONS:
10273 case OMP_SINGLE:
10274 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
10275 stmt = push_stmt_list ();
10276 RECUR (OMP_BODY (t));
10277 stmt = pop_stmt_list (stmt);
10279 t = copy_node (t);
10280 OMP_BODY (t) = stmt;
10281 OMP_CLAUSES (t) = tmp;
10282 add_stmt (t);
10283 break;
10285 case OMP_SECTION:
10286 case OMP_CRITICAL:
10287 case OMP_MASTER:
10288 case OMP_ORDERED:
10289 stmt = push_stmt_list ();
10290 RECUR (OMP_BODY (t));
10291 stmt = pop_stmt_list (stmt);
10293 t = copy_node (t);
10294 OMP_BODY (t) = stmt;
10295 add_stmt (t);
10296 break;
10298 case OMP_ATOMIC:
10299 if (OMP_ATOMIC_DEPENDENT_P (t))
10301 tree op1 = TREE_OPERAND (t, 1);
10302 tree lhs = RECUR (TREE_OPERAND (op1, 0));
10303 tree rhs = RECUR (TREE_OPERAND (op1, 1));
10304 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
10306 break;
10308 case EXPR_PACK_EXPANSION:
10309 error ("invalid use of pack expansion expression");
10310 return error_mark_node;
10312 case NONTYPE_ARGUMENT_PACK:
10313 error ("use %<...%> to expand argument pack");
10314 return error_mark_node;
10316 default:
10317 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
10319 return tsubst_copy_and_build (t, args, complain, in_decl,
10320 /*function_p=*/false,
10321 integral_constant_expression_p);
10324 return NULL_TREE;
10325 #undef RECUR
10328 /* T is a postfix-expression that is not being used in a function
10329 call. Return the substituted version of T. */
10331 static tree
10332 tsubst_non_call_postfix_expression (tree t, tree args,
10333 tsubst_flags_t complain,
10334 tree in_decl)
10336 if (TREE_CODE (t) == SCOPE_REF)
10337 t = tsubst_qualified_id (t, args, complain, in_decl,
10338 /*done=*/false, /*address_p=*/false);
10339 else
10340 t = tsubst_copy_and_build (t, args, complain, in_decl,
10341 /*function_p=*/false,
10342 /*integral_constant_expression_p=*/false);
10344 return t;
10347 /* Like tsubst but deals with expressions and performs semantic
10348 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10350 tree
10351 tsubst_copy_and_build (tree t,
10352 tree args,
10353 tsubst_flags_t complain,
10354 tree in_decl,
10355 bool function_p,
10356 bool integral_constant_expression_p)
10358 #define RECUR(NODE) \
10359 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10360 /*function_p=*/false, \
10361 integral_constant_expression_p)
10363 tree op1;
10365 if (t == NULL_TREE || t == error_mark_node)
10366 return t;
10368 switch (TREE_CODE (t))
10370 case USING_DECL:
10371 t = DECL_NAME (t);
10372 /* Fall through. */
10373 case IDENTIFIER_NODE:
10375 tree decl;
10376 cp_id_kind idk;
10377 bool non_integral_constant_expression_p;
10378 const char *error_msg;
10380 if (IDENTIFIER_TYPENAME_P (t))
10382 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10383 t = mangle_conv_op_name_for_type (new_type);
10386 /* Look up the name. */
10387 decl = lookup_name (t);
10389 /* By convention, expressions use ERROR_MARK_NODE to indicate
10390 failure, not NULL_TREE. */
10391 if (decl == NULL_TREE)
10392 decl = error_mark_node;
10394 decl = finish_id_expression (t, decl, NULL_TREE,
10395 &idk,
10396 integral_constant_expression_p,
10397 /*allow_non_integral_constant_expression_p=*/false,
10398 &non_integral_constant_expression_p,
10399 /*template_p=*/false,
10400 /*done=*/true,
10401 /*address_p=*/false,
10402 /*template_arg_p=*/false,
10403 &error_msg);
10404 if (error_msg)
10405 error (error_msg);
10406 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
10407 decl = unqualified_name_lookup_error (decl);
10408 return decl;
10411 case TEMPLATE_ID_EXPR:
10413 tree object;
10414 tree template = RECUR (TREE_OPERAND (t, 0));
10415 tree targs = TREE_OPERAND (t, 1);
10417 if (targs)
10418 targs = tsubst_template_args (targs, args, complain, in_decl);
10420 if (TREE_CODE (template) == COMPONENT_REF)
10422 object = TREE_OPERAND (template, 0);
10423 template = TREE_OPERAND (template, 1);
10425 else
10426 object = NULL_TREE;
10427 template = lookup_template_function (template, targs);
10429 if (object)
10430 return build3 (COMPONENT_REF, TREE_TYPE (template),
10431 object, template, NULL_TREE);
10432 else
10433 return baselink_for_fns (template);
10436 case INDIRECT_REF:
10438 tree r = RECUR (TREE_OPERAND (t, 0));
10440 if (REFERENCE_REF_P (t))
10442 /* A type conversion to reference type will be enclosed in
10443 such an indirect ref, but the substitution of the cast
10444 will have also added such an indirect ref. */
10445 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
10446 r = convert_from_reference (r);
10448 else
10449 r = build_x_indirect_ref (r, "unary *");
10450 return r;
10453 case NOP_EXPR:
10454 return build_nop
10455 (tsubst (TREE_TYPE (t), args, complain, in_decl),
10456 RECUR (TREE_OPERAND (t, 0)));
10458 case CAST_EXPR:
10459 case REINTERPRET_CAST_EXPR:
10460 case CONST_CAST_EXPR:
10461 case DYNAMIC_CAST_EXPR:
10462 case STATIC_CAST_EXPR:
10464 tree type;
10465 tree op;
10467 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10468 if (integral_constant_expression_p
10469 && !cast_valid_in_integral_constant_expression_p (type))
10471 error ("a cast to a type other than an integral or "
10472 "enumeration type cannot appear in a constant-expression");
10473 return error_mark_node;
10476 op = RECUR (TREE_OPERAND (t, 0));
10478 switch (TREE_CODE (t))
10480 case CAST_EXPR:
10481 return build_functional_cast (type, op);
10482 case REINTERPRET_CAST_EXPR:
10483 return build_reinterpret_cast (type, op);
10484 case CONST_CAST_EXPR:
10485 return build_const_cast (type, op);
10486 case DYNAMIC_CAST_EXPR:
10487 return build_dynamic_cast (type, op);
10488 case STATIC_CAST_EXPR:
10489 return build_static_cast (type, op);
10490 default:
10491 gcc_unreachable ();
10495 case POSTDECREMENT_EXPR:
10496 case POSTINCREMENT_EXPR:
10497 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10498 args, complain, in_decl);
10499 return build_x_unary_op (TREE_CODE (t), op1);
10501 case PREDECREMENT_EXPR:
10502 case PREINCREMENT_EXPR:
10503 case NEGATE_EXPR:
10504 case BIT_NOT_EXPR:
10505 case ABS_EXPR:
10506 case TRUTH_NOT_EXPR:
10507 case UNARY_PLUS_EXPR: /* Unary + */
10508 case REALPART_EXPR:
10509 case IMAGPART_EXPR:
10510 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
10512 case ADDR_EXPR:
10513 op1 = TREE_OPERAND (t, 0);
10514 if (TREE_CODE (op1) == SCOPE_REF)
10515 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
10516 /*done=*/true, /*address_p=*/true);
10517 else
10518 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
10519 in_decl);
10520 if (TREE_CODE (op1) == LABEL_DECL)
10521 return finish_label_address_expr (DECL_NAME (op1));
10522 return build_x_unary_op (ADDR_EXPR, op1);
10524 case PLUS_EXPR:
10525 case MINUS_EXPR:
10526 case MULT_EXPR:
10527 case TRUNC_DIV_EXPR:
10528 case CEIL_DIV_EXPR:
10529 case FLOOR_DIV_EXPR:
10530 case ROUND_DIV_EXPR:
10531 case EXACT_DIV_EXPR:
10532 case BIT_AND_EXPR:
10533 case BIT_IOR_EXPR:
10534 case BIT_XOR_EXPR:
10535 case TRUNC_MOD_EXPR:
10536 case FLOOR_MOD_EXPR:
10537 case TRUTH_ANDIF_EXPR:
10538 case TRUTH_ORIF_EXPR:
10539 case TRUTH_AND_EXPR:
10540 case TRUTH_OR_EXPR:
10541 case RSHIFT_EXPR:
10542 case LSHIFT_EXPR:
10543 case RROTATE_EXPR:
10544 case LROTATE_EXPR:
10545 case EQ_EXPR:
10546 case NE_EXPR:
10547 case MAX_EXPR:
10548 case MIN_EXPR:
10549 case LE_EXPR:
10550 case GE_EXPR:
10551 case LT_EXPR:
10552 case GT_EXPR:
10553 case MEMBER_REF:
10554 case DOTSTAR_EXPR:
10555 return build_x_binary_op
10556 (TREE_CODE (t),
10557 RECUR (TREE_OPERAND (t, 0)),
10558 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10559 ? ERROR_MARK
10560 : TREE_CODE (TREE_OPERAND (t, 0))),
10561 RECUR (TREE_OPERAND (t, 1)),
10562 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10563 ? ERROR_MARK
10564 : TREE_CODE (TREE_OPERAND (t, 1))),
10565 /*overloaded_p=*/NULL);
10567 case SCOPE_REF:
10568 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
10569 /*address_p=*/false);
10570 case ARRAY_REF:
10571 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10572 args, complain, in_decl);
10573 return build_x_binary_op (ARRAY_REF, op1,
10574 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10575 ? ERROR_MARK
10576 : TREE_CODE (TREE_OPERAND (t, 0))),
10577 RECUR (TREE_OPERAND (t, 1)),
10578 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10579 ? ERROR_MARK
10580 : TREE_CODE (TREE_OPERAND (t, 1))),
10581 /*overloaded_p=*/NULL);
10583 case SIZEOF_EXPR:
10584 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10586 /* We only want to compute the number of arguments. */
10587 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10588 complain, in_decl);
10589 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
10591 /* Fall through */
10593 case ALIGNOF_EXPR:
10594 op1 = TREE_OPERAND (t, 0);
10595 if (!args)
10597 /* When there are no ARGS, we are trying to evaluate a
10598 non-dependent expression from the parser. Trying to do
10599 the substitutions may not work. */
10600 if (!TYPE_P (op1))
10601 op1 = TREE_TYPE (op1);
10603 else
10605 ++skip_evaluation;
10606 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
10607 /*function_p=*/false,
10608 /*integral_constant_expression_p=*/false);
10609 --skip_evaluation;
10611 if (TYPE_P (op1))
10612 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
10613 else
10614 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
10616 case MODOP_EXPR:
10618 tree r = build_x_modify_expr
10619 (RECUR (TREE_OPERAND (t, 0)),
10620 TREE_CODE (TREE_OPERAND (t, 1)),
10621 RECUR (TREE_OPERAND (t, 2)));
10622 /* TREE_NO_WARNING must be set if either the expression was
10623 parenthesized or it uses an operator such as >>= rather
10624 than plain assignment. In the former case, it was already
10625 set and must be copied. In the latter case,
10626 build_x_modify_expr sets it and it must not be reset
10627 here. */
10628 if (TREE_NO_WARNING (t))
10629 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10630 return r;
10633 case ARROW_EXPR:
10634 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10635 args, complain, in_decl);
10636 /* Remember that there was a reference to this entity. */
10637 if (DECL_P (op1))
10638 mark_used (op1);
10639 return build_x_arrow (op1);
10641 case NEW_EXPR:
10643 tree init = RECUR (TREE_OPERAND (t, 3));
10645 if (TREE_OPERAND (t, 3) && !init)
10646 /* If there was an initializer in the the original tree, but
10647 it instantiated to an empty list, then we should pass on
10648 VOID_ZERO_NODE to tell build_new that it was an empty
10649 initializer () rather than no initializer. This can only
10650 happen when the initializer is a pack expansion whose
10651 parameter packs are of length zero. */
10652 init = void_zero_node;
10654 return build_new
10655 (RECUR (TREE_OPERAND (t, 0)),
10656 RECUR (TREE_OPERAND (t, 1)),
10657 RECUR (TREE_OPERAND (t, 2)),
10658 init,
10659 NEW_EXPR_USE_GLOBAL (t));
10662 case DELETE_EXPR:
10663 return delete_sanity
10664 (RECUR (TREE_OPERAND (t, 0)),
10665 RECUR (TREE_OPERAND (t, 1)),
10666 DELETE_EXPR_USE_VEC (t),
10667 DELETE_EXPR_USE_GLOBAL (t));
10669 case COMPOUND_EXPR:
10670 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
10671 RECUR (TREE_OPERAND (t, 1)));
10673 case CALL_EXPR:
10675 tree function;
10676 tree call_args;
10677 bool qualified_p;
10678 bool koenig_p;
10680 function = CALL_EXPR_FN (t);
10681 /* When we parsed the expression, we determined whether or
10682 not Koenig lookup should be performed. */
10683 koenig_p = KOENIG_LOOKUP_P (t);
10684 if (TREE_CODE (function) == SCOPE_REF)
10686 qualified_p = true;
10687 function = tsubst_qualified_id (function, args, complain, in_decl,
10688 /*done=*/false,
10689 /*address_p=*/false);
10691 else
10693 if (TREE_CODE (function) == COMPONENT_REF)
10695 tree op = TREE_OPERAND (function, 1);
10697 qualified_p = (TREE_CODE (op) == SCOPE_REF
10698 || (BASELINK_P (op)
10699 && BASELINK_QUALIFIED_P (op)));
10701 else
10702 qualified_p = false;
10704 function = tsubst_copy_and_build (function, args, complain,
10705 in_decl,
10706 !qualified_p,
10707 integral_constant_expression_p);
10709 if (BASELINK_P (function))
10710 qualified_p = true;
10713 /* FIXME: Rewrite this so as not to construct an arglist. */
10714 call_args = RECUR (CALL_EXPR_ARGS (t));
10716 /* We do not perform argument-dependent lookup if normal
10717 lookup finds a non-function, in accordance with the
10718 expected resolution of DR 218. */
10719 if (koenig_p
10720 && ((is_overloaded_fn (function)
10721 /* If lookup found a member function, the Koenig lookup is
10722 not appropriate, even if an unqualified-name was used
10723 to denote the function. */
10724 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
10725 || TREE_CODE (function) == IDENTIFIER_NODE))
10726 function = perform_koenig_lookup (function, call_args);
10728 if (TREE_CODE (function) == IDENTIFIER_NODE)
10730 unqualified_name_lookup_error (function);
10731 return error_mark_node;
10734 /* Remember that there was a reference to this entity. */
10735 if (DECL_P (function))
10736 mark_used (function);
10738 if (TREE_CODE (function) == OFFSET_REF)
10739 return build_offset_ref_call_from_tree (function, call_args);
10740 if (TREE_CODE (function) == COMPONENT_REF)
10742 if (!BASELINK_P (TREE_OPERAND (function, 1)))
10743 return finish_call_expr (function, call_args,
10744 /*disallow_virtual=*/false,
10745 /*koenig_p=*/false);
10746 else
10747 return (build_new_method_call
10748 (TREE_OPERAND (function, 0),
10749 TREE_OPERAND (function, 1),
10750 call_args, NULL_TREE,
10751 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
10752 /*fn_p=*/NULL));
10754 return finish_call_expr (function, call_args,
10755 /*disallow_virtual=*/qualified_p,
10756 koenig_p);
10759 case COND_EXPR:
10760 return build_x_conditional_expr
10761 (RECUR (TREE_OPERAND (t, 0)),
10762 RECUR (TREE_OPERAND (t, 1)),
10763 RECUR (TREE_OPERAND (t, 2)));
10765 case PSEUDO_DTOR_EXPR:
10766 return finish_pseudo_destructor_expr
10767 (RECUR (TREE_OPERAND (t, 0)),
10768 RECUR (TREE_OPERAND (t, 1)),
10769 RECUR (TREE_OPERAND (t, 2)));
10771 case TREE_LIST:
10773 tree purpose, value, chain;
10775 if (t == void_list_node)
10776 return t;
10778 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
10779 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
10781 /* We have pack expansions, so expand those and
10782 create a new list out of it. */
10783 tree purposevec = NULL_TREE;
10784 tree valuevec = NULL_TREE;
10785 tree chain;
10786 int i, len = -1;
10788 /* Expand the argument expressions. */
10789 if (TREE_PURPOSE (t))
10790 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
10791 complain, in_decl);
10792 if (TREE_VALUE (t))
10793 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
10794 complain, in_decl);
10796 /* Build the rest of the list. */
10797 chain = TREE_CHAIN (t);
10798 if (chain && chain != void_type_node)
10799 chain = RECUR (chain);
10801 /* Determine the number of arguments. */
10802 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
10804 len = TREE_VEC_LENGTH (purposevec);
10805 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
10807 else if (TREE_CODE (valuevec) == TREE_VEC)
10808 len = TREE_VEC_LENGTH (valuevec);
10809 else
10811 /* Since we only performed a partial substitution into
10812 the argument pack, we only return a single list
10813 node. */
10814 if (purposevec == TREE_PURPOSE (t)
10815 && valuevec == TREE_VALUE (t)
10816 && chain == TREE_CHAIN (t))
10817 return t;
10819 return tree_cons (purposevec, valuevec, chain);
10822 /* Convert the argument vectors into a TREE_LIST */
10823 i = len;
10824 while (i > 0)
10826 /* Grab the Ith values. */
10827 i--;
10828 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
10829 : NULL_TREE;
10830 value
10831 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
10832 : NULL_TREE;
10834 /* Build the list (backwards). */
10835 chain = tree_cons (purpose, value, chain);
10838 return chain;
10841 purpose = TREE_PURPOSE (t);
10842 if (purpose)
10843 purpose = RECUR (purpose);
10844 value = TREE_VALUE (t);
10845 if (value)
10846 value = RECUR (value);
10847 chain = TREE_CHAIN (t);
10848 if (chain && chain != void_type_node)
10849 chain = RECUR (chain);
10850 if (purpose == TREE_PURPOSE (t)
10851 && value == TREE_VALUE (t)
10852 && chain == TREE_CHAIN (t))
10853 return t;
10854 return tree_cons (purpose, value, chain);
10857 case COMPONENT_REF:
10859 tree object;
10860 tree object_type;
10861 tree member;
10863 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10864 args, complain, in_decl);
10865 /* Remember that there was a reference to this entity. */
10866 if (DECL_P (object))
10867 mark_used (object);
10868 object_type = TREE_TYPE (object);
10870 member = TREE_OPERAND (t, 1);
10871 if (BASELINK_P (member))
10872 member = tsubst_baselink (member,
10873 non_reference (TREE_TYPE (object)),
10874 args, complain, in_decl);
10875 else
10876 member = tsubst_copy (member, args, complain, in_decl);
10877 if (member == error_mark_node)
10878 return error_mark_node;
10880 if (object_type && !CLASS_TYPE_P (object_type))
10882 if (TREE_CODE (member) == BIT_NOT_EXPR)
10883 return finish_pseudo_destructor_expr (object,
10884 NULL_TREE,
10885 object_type);
10886 else if (TREE_CODE (member) == SCOPE_REF
10887 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
10888 return finish_pseudo_destructor_expr (object,
10889 object,
10890 object_type);
10892 else if (TREE_CODE (member) == SCOPE_REF
10893 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
10895 tree tmpl;
10896 tree args;
10898 /* Lookup the template functions now that we know what the
10899 scope is. */
10900 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
10901 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
10902 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
10903 /*is_type_p=*/false,
10904 /*complain=*/false);
10905 if (BASELINK_P (member))
10907 BASELINK_FUNCTIONS (member)
10908 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
10909 args);
10910 member = (adjust_result_of_qualified_name_lookup
10911 (member, BINFO_TYPE (BASELINK_BINFO (member)),
10912 object_type));
10914 else
10916 qualified_name_lookup_error (object_type, tmpl, member);
10917 return error_mark_node;
10920 else if (TREE_CODE (member) == SCOPE_REF
10921 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
10922 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
10924 if (complain & tf_error)
10926 if (TYPE_P (TREE_OPERAND (member, 0)))
10927 error ("%qT is not a class or namespace",
10928 TREE_OPERAND (member, 0));
10929 else
10930 error ("%qD is not a class or namespace",
10931 TREE_OPERAND (member, 0));
10933 return error_mark_node;
10935 else if (TREE_CODE (member) == FIELD_DECL)
10936 return finish_non_static_data_member (member, object, NULL_TREE);
10938 return finish_class_member_access_expr (object, member,
10939 /*template_p=*/false);
10942 case THROW_EXPR:
10943 return build_throw
10944 (RECUR (TREE_OPERAND (t, 0)));
10946 case CONSTRUCTOR:
10948 VEC(constructor_elt,gc) *n;
10949 constructor_elt *ce;
10950 unsigned HOST_WIDE_INT idx;
10951 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10952 bool process_index_p;
10953 int newlen;
10954 bool need_copy_p = false;
10956 if (type == error_mark_node)
10957 return error_mark_node;
10959 /* digest_init will do the wrong thing if we let it. */
10960 if (type && TYPE_PTRMEMFUNC_P (type))
10961 return t;
10963 /* We do not want to process the index of aggregate
10964 initializers as they are identifier nodes which will be
10965 looked up by digest_init. */
10966 process_index_p = !(type && IS_AGGR_TYPE (type));
10968 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
10969 newlen = VEC_length (constructor_elt, n);
10970 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
10972 if (ce->index && process_index_p)
10973 ce->index = RECUR (ce->index);
10975 if (PACK_EXPANSION_P (ce->value))
10977 /* Substitute into the pack expansion. */
10978 ce->value = tsubst_pack_expansion (ce->value, args, complain,
10979 in_decl);
10981 if (TREE_VEC_LENGTH (ce->value) == 1)
10982 /* Just move the argument into place. */
10983 ce->value = TREE_VEC_ELT (ce->value, 0);
10984 else
10986 /* Update the length of the final CONSTRUCTOR
10987 arguments vector, and note that we will need to
10988 copy.*/
10989 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
10990 need_copy_p = true;
10993 else
10994 ce->value = RECUR (ce->value);
10997 if (need_copy_p)
10999 VEC(constructor_elt,gc) *old_n = n;
11001 n = VEC_alloc (constructor_elt, gc, newlen);
11002 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
11003 idx++)
11005 if (TREE_CODE (ce->value) == TREE_VEC)
11007 int i, len = TREE_VEC_LENGTH (ce->value);
11008 for (i = 0; i < len; ++i)
11009 CONSTRUCTOR_APPEND_ELT (n, 0,
11010 TREE_VEC_ELT (ce->value, i));
11012 else
11013 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
11017 if (TREE_HAS_CONSTRUCTOR (t))
11018 return finish_compound_literal (type, n);
11020 return build_constructor (NULL_TREE, n);
11023 case TYPEID_EXPR:
11025 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
11026 if (TYPE_P (operand_0))
11027 return get_typeid (operand_0);
11028 return build_typeid (operand_0);
11031 case VAR_DECL:
11032 if (!args)
11033 return t;
11034 /* Fall through */
11036 case PARM_DECL:
11038 tree r = tsubst_copy (t, args, complain, in_decl);
11040 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
11041 /* If the original type was a reference, we'll be wrapped in
11042 the appropriate INDIRECT_REF. */
11043 r = convert_from_reference (r);
11044 return r;
11047 case VA_ARG_EXPR:
11048 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
11049 tsubst_copy (TREE_TYPE (t), args, complain,
11050 in_decl));
11052 case OFFSETOF_EXPR:
11053 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
11055 case TRAIT_EXPR:
11057 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
11058 complain, in_decl);
11060 tree type2 = TRAIT_EXPR_TYPE2 (t);
11061 if (type2)
11062 type2 = tsubst_copy (type2, args, complain, in_decl);
11064 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
11067 case STMT_EXPR:
11069 tree old_stmt_expr = cur_stmt_expr;
11070 tree stmt_expr = begin_stmt_expr ();
11072 cur_stmt_expr = stmt_expr;
11073 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
11074 integral_constant_expression_p);
11075 stmt_expr = finish_stmt_expr (stmt_expr, false);
11076 cur_stmt_expr = old_stmt_expr;
11078 return stmt_expr;
11081 case CONST_DECL:
11082 t = tsubst_copy (t, args, complain, in_decl);
11083 /* As in finish_id_expression, we resolve enumeration constants
11084 to their underlying values. */
11085 if (TREE_CODE (t) == CONST_DECL)
11087 used_types_insert (TREE_TYPE (t));
11088 return DECL_INITIAL (t);
11090 return t;
11092 default:
11093 /* Handle Objective-C++ constructs, if appropriate. */
11095 tree subst
11096 = objcp_tsubst_copy_and_build (t, args, complain,
11097 in_decl, /*function_p=*/false);
11098 if (subst)
11099 return subst;
11101 return tsubst_copy (t, args, complain, in_decl);
11104 #undef RECUR
11107 /* Verify that the instantiated ARGS are valid. For type arguments,
11108 make sure that the type's linkage is ok. For non-type arguments,
11109 make sure they are constants if they are integral or enumerations.
11110 Emit an error under control of COMPLAIN, and return TRUE on error. */
11112 static bool
11113 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
11115 int ix, len = DECL_NTPARMS (tmpl);
11116 bool result = false;
11118 for (ix = 0; ix != len; ix++)
11120 tree t = TREE_VEC_ELT (args, ix);
11122 if (TYPE_P (t))
11124 /* [basic.link]: A name with no linkage (notably, the name
11125 of a class or enumeration declared in a local scope)
11126 shall not be used to declare an entity with linkage.
11127 This implies that names with no linkage cannot be used as
11128 template arguments. */
11129 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
11131 if (nt)
11133 /* DR 488 makes use of a type with no linkage cause
11134 type deduction to fail. */
11135 if (complain & tf_error)
11137 if (TYPE_ANONYMOUS_P (nt))
11138 error ("%qT is/uses anonymous type", t);
11139 else
11140 error ("template argument for %qD uses local type %qT",
11141 tmpl, t);
11143 result = true;
11145 /* In order to avoid all sorts of complications, we do not
11146 allow variably-modified types as template arguments. */
11147 else if (variably_modified_type_p (t, NULL_TREE))
11149 if (complain & tf_error)
11150 error ("%qT is a variably modified type", t);
11151 result = true;
11154 /* A non-type argument of integral or enumerated type must be a
11155 constant. */
11156 else if (TREE_TYPE (t)
11157 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
11158 && !TREE_CONSTANT (t))
11160 if (complain & tf_error)
11161 error ("integral expression %qE is not constant", t);
11162 result = true;
11165 if (result && (complain & tf_error))
11166 error (" trying to instantiate %qD", tmpl);
11167 return result;
11170 /* Instantiate the indicated variable or function template TMPL with
11171 the template arguments in TARG_PTR. */
11173 tree
11174 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
11176 tree fndecl;
11177 tree gen_tmpl;
11178 tree spec;
11179 HOST_WIDE_INT saved_processing_template_decl;
11181 if (tmpl == error_mark_node)
11182 return error_mark_node;
11184 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
11186 /* If this function is a clone, handle it specially. */
11187 if (DECL_CLONED_FUNCTION_P (tmpl))
11189 tree spec;
11190 tree clone;
11192 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
11193 complain);
11194 if (spec == error_mark_node)
11195 return error_mark_node;
11197 /* Look for the clone. */
11198 FOR_EACH_CLONE (clone, spec)
11199 if (DECL_NAME (clone) == DECL_NAME (tmpl))
11200 return clone;
11201 /* We should always have found the clone by now. */
11202 gcc_unreachable ();
11203 return NULL_TREE;
11206 /* Check to see if we already have this specialization. */
11207 spec = retrieve_specialization (tmpl, targ_ptr,
11208 /*class_specializations_p=*/false);
11209 if (spec != NULL_TREE)
11210 return spec;
11212 gen_tmpl = most_general_template (tmpl);
11213 if (tmpl != gen_tmpl)
11215 /* The TMPL is a partial instantiation. To get a full set of
11216 arguments we must add the arguments used to perform the
11217 partial instantiation. */
11218 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
11219 targ_ptr);
11221 /* Check to see if we already have this specialization. */
11222 spec = retrieve_specialization (gen_tmpl, targ_ptr,
11223 /*class_specializations_p=*/false);
11224 if (spec != NULL_TREE)
11225 return spec;
11228 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
11229 complain))
11230 return error_mark_node;
11232 /* We are building a FUNCTION_DECL, during which the access of its
11233 parameters and return types have to be checked. However this
11234 FUNCTION_DECL which is the desired context for access checking
11235 is not built yet. We solve this chicken-and-egg problem by
11236 deferring all checks until we have the FUNCTION_DECL. */
11237 push_deferring_access_checks (dk_deferred);
11239 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11240 (because, for example, we have encountered a non-dependent
11241 function call in the body of a template function and must now
11242 determine which of several overloaded functions will be called),
11243 within the instantiation itself we are not processing a
11244 template. */
11245 saved_processing_template_decl = processing_template_decl;
11246 processing_template_decl = 0;
11247 /* Substitute template parameters to obtain the specialization. */
11248 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
11249 targ_ptr, complain, gen_tmpl);
11250 processing_template_decl = saved_processing_template_decl;
11251 if (fndecl == error_mark_node)
11252 return error_mark_node;
11254 /* Now we know the specialization, compute access previously
11255 deferred. */
11256 push_access_scope (fndecl);
11257 perform_deferred_access_checks ();
11258 pop_access_scope (fndecl);
11259 pop_deferring_access_checks ();
11261 /* The DECL_TI_TEMPLATE should always be the immediate parent
11262 template, not the most general template. */
11263 DECL_TI_TEMPLATE (fndecl) = tmpl;
11265 /* If we've just instantiated the main entry point for a function,
11266 instantiate all the alternate entry points as well. We do this
11267 by cloning the instantiation of the main entry point, not by
11268 instantiating the template clones. */
11269 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
11270 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
11272 return fndecl;
11275 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11276 arguments that are being used when calling it. TARGS is a vector
11277 into which the deduced template arguments are placed.
11279 Return zero for success, 2 for an incomplete match that doesn't resolve
11280 all the types, and 1 for complete failure. An error message will be
11281 printed only for an incomplete match.
11283 If FN is a conversion operator, or we are trying to produce a specific
11284 specialization, RETURN_TYPE is the return type desired.
11286 The EXPLICIT_TARGS are explicit template arguments provided via a
11287 template-id.
11289 The parameter STRICT is one of:
11291 DEDUCE_CALL:
11292 We are deducing arguments for a function call, as in
11293 [temp.deduct.call].
11295 DEDUCE_CONV:
11296 We are deducing arguments for a conversion function, as in
11297 [temp.deduct.conv].
11299 DEDUCE_EXACT:
11300 We are deducing arguments when doing an explicit instantiation
11301 as in [temp.explicit], when determining an explicit specialization
11302 as in [temp.expl.spec], or when taking the address of a function
11303 template, as in [temp.deduct.funcaddr]. */
11306 fn_type_unification (tree fn,
11307 tree explicit_targs,
11308 tree targs,
11309 tree args,
11310 tree return_type,
11311 unification_kind_t strict,
11312 int flags)
11314 tree parms;
11315 tree fntype;
11316 int result;
11317 bool incomplete_argument_packs_p = false;
11319 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
11321 fntype = TREE_TYPE (fn);
11322 if (explicit_targs)
11324 /* [temp.deduct]
11326 The specified template arguments must match the template
11327 parameters in kind (i.e., type, nontype, template), and there
11328 must not be more arguments than there are parameters;
11329 otherwise type deduction fails.
11331 Nontype arguments must match the types of the corresponding
11332 nontype template parameters, or must be convertible to the
11333 types of the corresponding nontype parameters as specified in
11334 _temp.arg.nontype_, otherwise type deduction fails.
11336 All references in the function type of the function template
11337 to the corresponding template parameters are replaced by the
11338 specified template argument values. If a substitution in a
11339 template parameter or in the function type of the function
11340 template results in an invalid type, type deduction fails. */
11341 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
11342 int i, len = TREE_VEC_LENGTH (tparms);
11343 tree converted_args;
11344 bool incomplete = false;
11346 if (explicit_targs == error_mark_node)
11347 return 1;
11349 converted_args
11350 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
11351 /*require_all_args=*/false,
11352 /*use_default_args=*/false));
11353 if (converted_args == error_mark_node)
11354 return 1;
11356 /* Substitute the explicit args into the function type. This is
11357 necessary so that, for instance, explicitly declared function
11358 arguments can match null pointed constants. If we were given
11359 an incomplete set of explicit args, we must not do semantic
11360 processing during substitution as we could create partial
11361 instantiations. */
11362 for (i = 0; i < len; i++)
11364 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11365 bool parameter_pack = false;
11367 /* Dig out the actual parm. */
11368 if (TREE_CODE (parm) == TYPE_DECL
11369 || TREE_CODE (parm) == TEMPLATE_DECL)
11371 parm = TREE_TYPE (parm);
11372 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
11374 else if (TREE_CODE (parm) == PARM_DECL)
11376 parm = DECL_INITIAL (parm);
11377 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
11380 if (parameter_pack)
11382 int level, idx;
11383 tree targ;
11384 template_parm_level_and_index (parm, &level, &idx);
11386 /* Mark the argument pack as "incomplete". We could
11387 still deduce more arguments during unification. */
11388 targ = TMPL_ARG (converted_args, level, idx);
11389 if (targ)
11391 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
11392 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
11393 = ARGUMENT_PACK_ARGS (targ);
11396 /* We have some incomplete argument packs. */
11397 incomplete_argument_packs_p = true;
11401 if (incomplete_argument_packs_p)
11402 /* Any substitution is guaranteed to be incomplete if there
11403 are incomplete argument packs, because we can still deduce
11404 more arguments. */
11405 incomplete = 1;
11406 else
11407 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
11409 processing_template_decl += incomplete;
11410 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
11411 processing_template_decl -= incomplete;
11413 if (fntype == error_mark_node)
11414 return 1;
11416 /* Place the explicitly specified arguments in TARGS. */
11417 for (i = NUM_TMPL_ARGS (converted_args); i--;)
11418 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
11421 /* Never do unification on the 'this' parameter. */
11422 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
11424 if (return_type)
11426 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
11427 args = tree_cons (NULL_TREE, return_type, args);
11430 /* We allow incomplete unification without an error message here
11431 because the standard doesn't seem to explicitly prohibit it. Our
11432 callers must be ready to deal with unification failures in any
11433 event. */
11434 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
11435 targs, parms, args, /*subr=*/0,
11436 strict, flags);
11438 if (result == 0 && incomplete_argument_packs_p)
11440 int i, len = NUM_TMPL_ARGS (targs);
11442 /* Clear the "incomplete" flags on all argument packs. */
11443 for (i = 0; i < len; i++)
11445 tree arg = TREE_VEC_ELT (targs, i);
11446 if (ARGUMENT_PACK_P (arg))
11448 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
11449 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
11454 if (result == 0)
11455 /* All is well so far. Now, check:
11457 [temp.deduct]
11459 When all template arguments have been deduced, all uses of
11460 template parameters in nondeduced contexts are replaced with
11461 the corresponding deduced argument values. If the
11462 substitution results in an invalid type, as described above,
11463 type deduction fails. */
11464 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
11465 == error_mark_node)
11466 return 1;
11468 return result;
11471 /* Adjust types before performing type deduction, as described in
11472 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11473 sections are symmetric. PARM is the type of a function parameter
11474 or the return type of the conversion function. ARG is the type of
11475 the argument passed to the call, or the type of the value
11476 initialized with the result of the conversion function.
11477 ARG_EXPR is the original argument expression, which may be null. */
11479 static int
11480 maybe_adjust_types_for_deduction (unification_kind_t strict,
11481 tree* parm,
11482 tree* arg,
11483 tree arg_expr)
11485 int result = 0;
11487 switch (strict)
11489 case DEDUCE_CALL:
11490 break;
11492 case DEDUCE_CONV:
11494 /* Swap PARM and ARG throughout the remainder of this
11495 function; the handling is precisely symmetric since PARM
11496 will initialize ARG rather than vice versa. */
11497 tree* temp = parm;
11498 parm = arg;
11499 arg = temp;
11500 break;
11503 case DEDUCE_EXACT:
11504 /* There is nothing to do in this case. */
11505 return 0;
11507 default:
11508 gcc_unreachable ();
11511 if (TREE_CODE (*parm) != REFERENCE_TYPE)
11513 /* [temp.deduct.call]
11515 If P is not a reference type:
11517 --If A is an array type, the pointer type produced by the
11518 array-to-pointer standard conversion (_conv.array_) is
11519 used in place of A for type deduction; otherwise,
11521 --If A is a function type, the pointer type produced by
11522 the function-to-pointer standard conversion
11523 (_conv.func_) is used in place of A for type deduction;
11524 otherwise,
11526 --If A is a cv-qualified type, the top level
11527 cv-qualifiers of A's type are ignored for type
11528 deduction. */
11529 if (TREE_CODE (*arg) == ARRAY_TYPE)
11530 *arg = build_pointer_type (TREE_TYPE (*arg));
11531 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
11532 *arg = build_pointer_type (*arg);
11533 else
11534 *arg = TYPE_MAIN_VARIANT (*arg);
11537 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
11538 of the form T&&, where T is a template parameter, and the argument
11539 is an lvalue, T is deduced as A& */
11540 if (TREE_CODE (*parm) == REFERENCE_TYPE
11541 && TYPE_REF_IS_RVALUE (*parm)
11542 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
11543 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
11544 && arg_expr && real_lvalue_p (arg_expr))
11545 *arg = build_reference_type (*arg);
11547 /* [temp.deduct.call]
11549 If P is a cv-qualified type, the top level cv-qualifiers
11550 of P's type are ignored for type deduction. If P is a
11551 reference type, the type referred to by P is used for
11552 type deduction. */
11553 *parm = TYPE_MAIN_VARIANT (*parm);
11554 if (TREE_CODE (*parm) == REFERENCE_TYPE)
11556 *parm = TREE_TYPE (*parm);
11557 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
11560 /* DR 322. For conversion deduction, remove a reference type on parm
11561 too (which has been swapped into ARG). */
11562 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
11563 *arg = TREE_TYPE (*arg);
11565 return result;
11568 /* Most parms like fn_type_unification.
11570 If SUBR is 1, we're being called recursively (to unify the
11571 arguments of a function or method parameter of a function
11572 template). */
11574 static int
11575 type_unification_real (tree tparms,
11576 tree targs,
11577 tree xparms,
11578 tree xargs,
11579 int subr,
11580 unification_kind_t strict,
11581 int flags)
11583 tree parm, arg, arg_expr;
11584 int i;
11585 int ntparms = TREE_VEC_LENGTH (tparms);
11586 int sub_strict;
11587 int saw_undeduced = 0;
11588 tree parms, args;
11590 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
11591 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
11592 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
11593 gcc_assert (ntparms > 0);
11595 switch (strict)
11597 case DEDUCE_CALL:
11598 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
11599 | UNIFY_ALLOW_DERIVED);
11600 break;
11602 case DEDUCE_CONV:
11603 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
11604 break;
11606 case DEDUCE_EXACT:
11607 sub_strict = UNIFY_ALLOW_NONE;
11608 break;
11610 default:
11611 gcc_unreachable ();
11614 again:
11615 parms = xparms;
11616 args = xargs;
11618 while (parms && parms != void_list_node
11619 && args && args != void_list_node)
11621 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11622 break;
11624 parm = TREE_VALUE (parms);
11625 parms = TREE_CHAIN (parms);
11626 arg = TREE_VALUE (args);
11627 args = TREE_CHAIN (args);
11628 arg_expr = NULL;
11630 if (arg == error_mark_node)
11631 return 1;
11632 if (arg == unknown_type_node)
11633 /* We can't deduce anything from this, but we might get all the
11634 template args from other function args. */
11635 continue;
11637 /* Conversions will be performed on a function argument that
11638 corresponds with a function parameter that contains only
11639 non-deducible template parameters and explicitly specified
11640 template parameters. */
11641 if (!uses_template_parms (parm))
11643 tree type;
11645 if (!TYPE_P (arg))
11646 type = TREE_TYPE (arg);
11647 else
11648 type = arg;
11650 if (same_type_p (parm, type))
11651 continue;
11652 if (strict != DEDUCE_EXACT
11653 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
11654 flags))
11655 continue;
11657 return 1;
11660 if (!TYPE_P (arg))
11662 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
11663 if (type_unknown_p (arg))
11665 /* [temp.deduct.type]
11667 A template-argument can be deduced from a pointer to
11668 function or pointer to member function argument if
11669 the set of overloaded functions does not contain
11670 function templates and at most one of a set of
11671 overloaded functions provides a unique match. */
11672 if (resolve_overloaded_unification
11673 (tparms, targs, parm, arg, strict, sub_strict))
11674 continue;
11676 return 1;
11678 arg_expr = arg;
11679 arg = unlowered_expr_type (arg);
11680 if (arg == error_mark_node)
11681 return 1;
11685 int arg_strict = sub_strict;
11687 if (!subr)
11688 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
11689 arg_expr);
11691 if (unify (tparms, targs, parm, arg, arg_strict))
11692 return 1;
11697 if (parms
11698 && parms != void_list_node
11699 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11701 /* Unify the remaining arguments with the pack expansion type. */
11702 tree argvec;
11703 tree parmvec = make_tree_vec (1);
11704 int len = 0;
11705 tree t;
11707 /* Count the number of arguments that remain. */
11708 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
11709 len++;
11711 /* Allocate a TREE_VEC and copy in all of the arguments */
11712 argvec = make_tree_vec (len);
11713 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
11715 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
11716 ++i;
11719 /* Copy the parameter into parmvec. */
11720 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
11721 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
11722 /*call_args_p=*/true, /*subr=*/subr))
11723 return 1;
11725 /* Advance to the end of the list of parameters. */
11726 parms = TREE_CHAIN (parms);
11729 /* Fail if we've reached the end of the parm list, and more args
11730 are present, and the parm list isn't variadic. */
11731 if (args && args != void_list_node && parms == void_list_node)
11732 return 1;
11733 /* Fail if parms are left and they don't have default values. */
11734 if (parms && parms != void_list_node
11735 && TREE_PURPOSE (parms) == NULL_TREE)
11736 return 1;
11738 if (!subr)
11739 for (i = 0; i < ntparms; i++)
11740 if (!TREE_VEC_ELT (targs, i))
11742 tree tparm;
11744 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
11745 continue;
11747 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11749 /* If this is an undeduced nontype parameter that depends on
11750 a type parameter, try another pass; its type may have been
11751 deduced from a later argument than the one from which
11752 this parameter can be deduced. */
11753 if (TREE_CODE (tparm) == PARM_DECL
11754 && uses_template_parms (TREE_TYPE (tparm))
11755 && !saw_undeduced++)
11756 goto again;
11758 /* Core issue #226 (C++0x) [temp.deduct]:
11760 If a template argument has not been deduced, its
11761 default template argument, if any, is used.
11763 When we are in C++98 mode, TREE_PURPOSE will either
11764 be NULL_TREE or ERROR_MARK_NODE, so we do not need
11765 to explicitly check cxx_dialect here. */
11766 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
11768 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
11769 targs, tf_none, NULL_TREE);
11770 if (arg == error_mark_node)
11771 return 1;
11772 else
11774 TREE_VEC_ELT (targs, i) = arg;
11775 continue;
11779 /* If the type parameter is a parameter pack, then it will
11780 be deduced to an empty parameter pack. */
11781 if (template_parameter_pack_p (tparm))
11783 tree arg;
11785 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
11787 arg = make_node (NONTYPE_ARGUMENT_PACK);
11788 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
11789 TREE_CONSTANT (arg) = 1;
11791 else
11792 arg = make_node (TYPE_ARGUMENT_PACK);
11794 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
11796 TREE_VEC_ELT (targs, i) = arg;
11797 continue;
11800 return 2;
11803 return 0;
11806 /* Subroutine of type_unification_real. Args are like the variables
11807 at the call site. ARG is an overloaded function (or template-id);
11808 we try deducing template args from each of the overloads, and if
11809 only one succeeds, we go with that. Modifies TARGS and returns
11810 true on success. */
11812 static bool
11813 resolve_overloaded_unification (tree tparms,
11814 tree targs,
11815 tree parm,
11816 tree arg,
11817 unification_kind_t strict,
11818 int sub_strict)
11820 tree tempargs = copy_node (targs);
11821 int good = 0;
11822 bool addr_p;
11824 if (TREE_CODE (arg) == ADDR_EXPR)
11826 arg = TREE_OPERAND (arg, 0);
11827 addr_p = true;
11829 else
11830 addr_p = false;
11832 if (TREE_CODE (arg) == COMPONENT_REF)
11833 /* Handle `&x' where `x' is some static or non-static member
11834 function name. */
11835 arg = TREE_OPERAND (arg, 1);
11837 if (TREE_CODE (arg) == OFFSET_REF)
11838 arg = TREE_OPERAND (arg, 1);
11840 /* Strip baselink information. */
11841 if (BASELINK_P (arg))
11842 arg = BASELINK_FUNCTIONS (arg);
11844 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
11846 /* If we got some explicit template args, we need to plug them into
11847 the affected templates before we try to unify, in case the
11848 explicit args will completely resolve the templates in question. */
11850 tree expl_subargs = TREE_OPERAND (arg, 1);
11851 arg = TREE_OPERAND (arg, 0);
11853 for (; arg; arg = OVL_NEXT (arg))
11855 tree fn = OVL_CURRENT (arg);
11856 tree subargs, elem;
11858 if (TREE_CODE (fn) != TEMPLATE_DECL)
11859 continue;
11861 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
11862 expl_subargs, /*check_ret=*/false);
11863 if (subargs)
11865 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
11866 good += try_one_overload (tparms, targs, tempargs, parm,
11867 elem, strict, sub_strict, addr_p);
11871 else if (TREE_CODE (arg) != OVERLOAD
11872 && TREE_CODE (arg) != FUNCTION_DECL)
11873 /* If ARG is, for example, "(0, &f)" then its type will be unknown
11874 -- but the deduction does not succeed because the expression is
11875 not just the function on its own. */
11876 return false;
11877 else
11878 for (; arg; arg = OVL_NEXT (arg))
11879 good += try_one_overload (tparms, targs, tempargs, parm,
11880 TREE_TYPE (OVL_CURRENT (arg)),
11881 strict, sub_strict, addr_p);
11883 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11884 to function or pointer to member function argument if the set of
11885 overloaded functions does not contain function templates and at most
11886 one of a set of overloaded functions provides a unique match.
11888 So if we found multiple possibilities, we return success but don't
11889 deduce anything. */
11891 if (good == 1)
11893 int i = TREE_VEC_LENGTH (targs);
11894 for (; i--; )
11895 if (TREE_VEC_ELT (tempargs, i))
11896 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
11898 if (good)
11899 return true;
11901 return false;
11904 /* Subroutine of resolve_overloaded_unification; does deduction for a single
11905 overload. Fills TARGS with any deduced arguments, or error_mark_node if
11906 different overloads deduce different arguments for a given parm.
11907 ADDR_P is true if the expression for which deduction is being
11908 performed was of the form "& fn" rather than simply "fn".
11910 Returns 1 on success. */
11912 static int
11913 try_one_overload (tree tparms,
11914 tree orig_targs,
11915 tree targs,
11916 tree parm,
11917 tree arg,
11918 unification_kind_t strict,
11919 int sub_strict,
11920 bool addr_p)
11922 int nargs;
11923 tree tempargs;
11924 int i;
11926 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11927 to function or pointer to member function argument if the set of
11928 overloaded functions does not contain function templates and at most
11929 one of a set of overloaded functions provides a unique match.
11931 So if this is a template, just return success. */
11933 if (uses_template_parms (arg))
11934 return 1;
11936 if (TREE_CODE (arg) == METHOD_TYPE)
11937 arg = build_ptrmemfunc_type (build_pointer_type (arg));
11938 else if (addr_p)
11939 arg = build_pointer_type (arg);
11941 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
11943 /* We don't copy orig_targs for this because if we have already deduced
11944 some template args from previous args, unify would complain when we
11945 try to deduce a template parameter for the same argument, even though
11946 there isn't really a conflict. */
11947 nargs = TREE_VEC_LENGTH (targs);
11948 tempargs = make_tree_vec (nargs);
11950 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
11951 return 0;
11953 /* First make sure we didn't deduce anything that conflicts with
11954 explicitly specified args. */
11955 for (i = nargs; i--; )
11957 tree elt = TREE_VEC_ELT (tempargs, i);
11958 tree oldelt = TREE_VEC_ELT (orig_targs, i);
11960 if (!elt)
11961 /*NOP*/;
11962 else if (uses_template_parms (elt))
11963 /* Since we're unifying against ourselves, we will fill in
11964 template args used in the function parm list with our own
11965 template parms. Discard them. */
11966 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
11967 else if (oldelt && !template_args_equal (oldelt, elt))
11968 return 0;
11971 for (i = nargs; i--; )
11973 tree elt = TREE_VEC_ELT (tempargs, i);
11975 if (elt)
11976 TREE_VEC_ELT (targs, i) = elt;
11979 return 1;
11982 /* PARM is a template class (perhaps with unbound template
11983 parameters). ARG is a fully instantiated type. If ARG can be
11984 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
11985 TARGS are as for unify. */
11987 static tree
11988 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
11990 tree copy_of_targs;
11992 if (!CLASSTYPE_TEMPLATE_INFO (arg)
11993 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
11994 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
11995 return NULL_TREE;
11997 /* We need to make a new template argument vector for the call to
11998 unify. If we used TARGS, we'd clutter it up with the result of
11999 the attempted unification, even if this class didn't work out.
12000 We also don't want to commit ourselves to all the unifications
12001 we've already done, since unification is supposed to be done on
12002 an argument-by-argument basis. In other words, consider the
12003 following pathological case:
12005 template <int I, int J, int K>
12006 struct S {};
12008 template <int I, int J>
12009 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12011 template <int I, int J, int K>
12012 void f(S<I, J, K>, S<I, I, I>);
12014 void g() {
12015 S<0, 0, 0> s0;
12016 S<0, 1, 2> s2;
12018 f(s0, s2);
12021 Now, by the time we consider the unification involving `s2', we
12022 already know that we must have `f<0, 0, 0>'. But, even though
12023 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12024 because there are two ways to unify base classes of S<0, 1, 2>
12025 with S<I, I, I>. If we kept the already deduced knowledge, we
12026 would reject the possibility I=1. */
12027 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
12029 /* If unification failed, we're done. */
12030 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
12031 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
12032 return NULL_TREE;
12034 return arg;
12037 /* Given a template type PARM and a class type ARG, find the unique
12038 base type in ARG that is an instance of PARM. We do not examine
12039 ARG itself; only its base-classes. If there is not exactly one
12040 appropriate base class, return NULL_TREE. PARM may be the type of
12041 a partial specialization, as well as a plain template type. Used
12042 by unify. */
12044 static tree
12045 get_template_base (tree tparms, tree targs, tree parm, tree arg)
12047 tree rval = NULL_TREE;
12048 tree binfo;
12050 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
12052 binfo = TYPE_BINFO (complete_type (arg));
12053 if (!binfo)
12054 /* The type could not be completed. */
12055 return NULL_TREE;
12057 /* Walk in inheritance graph order. The search order is not
12058 important, and this avoids multiple walks of virtual bases. */
12059 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
12061 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
12063 if (r)
12065 /* If there is more than one satisfactory baseclass, then:
12067 [temp.deduct.call]
12069 If they yield more than one possible deduced A, the type
12070 deduction fails.
12072 applies. */
12073 if (rval && !same_type_p (r, rval))
12074 return NULL_TREE;
12076 rval = r;
12080 return rval;
12083 /* Returns the level of DECL, which declares a template parameter. */
12085 static int
12086 template_decl_level (tree decl)
12088 switch (TREE_CODE (decl))
12090 case TYPE_DECL:
12091 case TEMPLATE_DECL:
12092 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
12094 case PARM_DECL:
12095 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
12097 default:
12098 gcc_unreachable ();
12100 return 0;
12103 /* Decide whether ARG can be unified with PARM, considering only the
12104 cv-qualifiers of each type, given STRICT as documented for unify.
12105 Returns nonzero iff the unification is OK on that basis. */
12107 static int
12108 check_cv_quals_for_unify (int strict, tree arg, tree parm)
12110 int arg_quals = cp_type_quals (arg);
12111 int parm_quals = cp_type_quals (parm);
12113 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12114 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12116 /* Although a CVR qualifier is ignored when being applied to a
12117 substituted template parameter ([8.3.2]/1 for example), that
12118 does not apply during deduction [14.8.2.4]/1, (even though
12119 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12120 this). Except when we're allowing additional CV qualifiers
12121 at the outer level [14.8.2.1]/3,1st bullet. */
12122 if ((TREE_CODE (arg) == REFERENCE_TYPE
12123 || TREE_CODE (arg) == FUNCTION_TYPE
12124 || TREE_CODE (arg) == METHOD_TYPE)
12125 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
12126 return 0;
12128 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
12129 && (parm_quals & TYPE_QUAL_RESTRICT))
12130 return 0;
12133 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12134 && (arg_quals & parm_quals) != parm_quals)
12135 return 0;
12137 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
12138 && (parm_quals & arg_quals) != arg_quals)
12139 return 0;
12141 return 1;
12144 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12145 void
12146 template_parm_level_and_index (tree parm, int* level, int* index)
12148 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12149 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12150 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12152 *index = TEMPLATE_TYPE_IDX (parm);
12153 *level = TEMPLATE_TYPE_LEVEL (parm);
12155 else
12157 *index = TEMPLATE_PARM_IDX (parm);
12158 *level = TEMPLATE_PARM_LEVEL (parm);
12162 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12163 expansion at the end of PACKED_PARMS. Returns 0 if the type
12164 deduction succeeds, 1 otherwise. STRICT is the same as in
12165 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12166 call argument list. We'll need to adjust the arguments to make them
12167 types. SUBR tells us if this is from a recursive call to
12168 type_unification_real. */
12170 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
12171 tree packed_args, int strict, bool call_args_p,
12172 bool subr)
12174 tree parm
12175 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
12176 tree pattern = PACK_EXPANSION_PATTERN (parm);
12177 tree pack, packs = NULL_TREE;
12178 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
12179 int len = TREE_VEC_LENGTH (packed_args);
12181 /* Determine the parameter packs we will be deducing from the
12182 pattern, and record their current deductions. */
12183 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
12184 pack; pack = TREE_CHAIN (pack))
12186 tree parm_pack = TREE_VALUE (pack);
12187 int idx, level;
12189 /* Determine the index and level of this parameter pack. */
12190 template_parm_level_and_index (parm_pack, &level, &idx);
12192 /* Keep track of the parameter packs and their corresponding
12193 argument packs. */
12194 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
12195 TREE_TYPE (packs) = make_tree_vec (len - start);
12198 /* Loop through all of the arguments that have not yet been
12199 unified and unify each with the pattern. */
12200 for (i = start; i < len; i++)
12202 tree parm = pattern;
12204 /* For each parameter pack, clear out the deduced value so that
12205 we can deduce it again. */
12206 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12208 int idx, level;
12209 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12211 TMPL_ARG (targs, level, idx) = NULL_TREE;
12214 /* Unify the pattern with the current argument. */
12216 tree arg = TREE_VEC_ELT (packed_args, i);
12217 int arg_strict = strict;
12218 bool skip_arg_p = false;
12220 if (call_args_p)
12222 int sub_strict;
12224 /* This mirrors what we do in type_unification_real. */
12225 switch (strict)
12227 case DEDUCE_CALL:
12228 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
12229 | UNIFY_ALLOW_MORE_CV_QUAL
12230 | UNIFY_ALLOW_DERIVED);
12231 break;
12233 case DEDUCE_CONV:
12234 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12235 break;
12237 case DEDUCE_EXACT:
12238 sub_strict = UNIFY_ALLOW_NONE;
12239 break;
12241 default:
12242 gcc_unreachable ();
12245 if (!TYPE_P (arg))
12247 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12248 if (type_unknown_p (arg))
12250 /* [temp.deduct.type] A template-argument can be
12251 deduced from a pointer to function or pointer
12252 to member function argument if the set of
12253 overloaded functions does not contain function
12254 templates and at most one of a set of
12255 overloaded functions provides a unique
12256 match. */
12258 if (resolve_overloaded_unification
12259 (tparms, targs, parm, arg, strict, sub_strict)
12260 != 0)
12261 return 1;
12262 skip_arg_p = true;
12265 if (!skip_arg_p)
12267 arg = TREE_TYPE (arg);
12268 if (arg == error_mark_node)
12269 return 1;
12273 arg_strict = sub_strict;
12275 if (!subr)
12276 arg_strict |=
12277 maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12280 if (!skip_arg_p)
12282 if (unify (tparms, targs, parm, arg, arg_strict))
12283 return 1;
12287 /* For each parameter pack, collect the deduced value. */
12288 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12290 int idx, level;
12291 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12293 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
12294 TMPL_ARG (targs, level, idx);
12298 /* Verify that the results of unification with the parameter packs
12299 produce results consistent with what we've seen before, and make
12300 the deduced argument packs available. */
12301 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12303 tree old_pack = TREE_VALUE (pack);
12304 tree new_args = TREE_TYPE (pack);
12306 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
12308 /* Prepend the explicit arguments onto NEW_ARGS. */
12309 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12310 tree old_args = new_args;
12311 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
12312 int len = explicit_len + TREE_VEC_LENGTH (old_args);
12314 /* Copy the explicit arguments. */
12315 new_args = make_tree_vec (len);
12316 for (i = 0; i < explicit_len; i++)
12317 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
12319 /* Copy the deduced arguments. */
12320 for (; i < len; i++)
12321 TREE_VEC_ELT (new_args, i) =
12322 TREE_VEC_ELT (old_args, i - explicit_len);
12325 if (!old_pack)
12327 tree result;
12328 int idx, level;
12330 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12332 /* Build the deduced *_ARGUMENT_PACK. */
12333 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
12335 result = make_node (NONTYPE_ARGUMENT_PACK);
12336 TREE_TYPE (result) =
12337 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
12338 TREE_CONSTANT (result) = 1;
12340 else
12341 result = make_node (TYPE_ARGUMENT_PACK);
12343 SET_ARGUMENT_PACK_ARGS (result, new_args);
12345 /* Note the deduced argument packs for this parameter
12346 pack. */
12347 TMPL_ARG (targs, level, idx) = result;
12349 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
12350 && (ARGUMENT_PACK_ARGS (old_pack)
12351 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
12353 /* We only had the explicitly-provided arguments before, but
12354 now we have a complete set of arguments. */
12355 int idx, level;
12356 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12357 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12359 /* Keep the original deduced argument pack. */
12360 TMPL_ARG (targs, level, idx) = old_pack;
12362 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
12363 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
12364 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
12366 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
12367 new_args))
12368 /* Inconsistent unification of this parameter pack. */
12369 return 1;
12370 else
12372 int idx, level;
12374 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12376 /* Keep the original deduced argument pack. */
12377 TMPL_ARG (targs, level, idx) = old_pack;
12381 return 0;
12384 /* Deduce the value of template parameters. TPARMS is the (innermost)
12385 set of template parameters to a template. TARGS is the bindings
12386 for those template parameters, as determined thus far; TARGS may
12387 include template arguments for outer levels of template parameters
12388 as well. PARM is a parameter to a template function, or a
12389 subcomponent of that parameter; ARG is the corresponding argument.
12390 This function attempts to match PARM with ARG in a manner
12391 consistent with the existing assignments in TARGS. If more values
12392 are deduced, then TARGS is updated.
12394 Returns 0 if the type deduction succeeds, 1 otherwise. The
12395 parameter STRICT is a bitwise or of the following flags:
12397 UNIFY_ALLOW_NONE:
12398 Require an exact match between PARM and ARG.
12399 UNIFY_ALLOW_MORE_CV_QUAL:
12400 Allow the deduced ARG to be more cv-qualified (by qualification
12401 conversion) than ARG.
12402 UNIFY_ALLOW_LESS_CV_QUAL:
12403 Allow the deduced ARG to be less cv-qualified than ARG.
12404 UNIFY_ALLOW_DERIVED:
12405 Allow the deduced ARG to be a template base class of ARG,
12406 or a pointer to a template base class of the type pointed to by
12407 ARG.
12408 UNIFY_ALLOW_INTEGER:
12409 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12410 case for more information.
12411 UNIFY_ALLOW_OUTER_LEVEL:
12412 This is the outermost level of a deduction. Used to determine validity
12413 of qualification conversions. A valid qualification conversion must
12414 have const qualified pointers leading up to the inner type which
12415 requires additional CV quals, except at the outer level, where const
12416 is not required [conv.qual]. It would be normal to set this flag in
12417 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12418 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12419 This is the outermost level of a deduction, and PARM can be more CV
12420 qualified at this point.
12421 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12422 This is the outermost level of a deduction, and PARM can be less CV
12423 qualified at this point. */
12425 static int
12426 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
12428 int idx;
12429 tree targ;
12430 tree tparm;
12431 int strict_in = strict;
12433 /* I don't think this will do the right thing with respect to types.
12434 But the only case I've seen it in so far has been array bounds, where
12435 signedness is the only information lost, and I think that will be
12436 okay. */
12437 while (TREE_CODE (parm) == NOP_EXPR)
12438 parm = TREE_OPERAND (parm, 0);
12440 if (arg == error_mark_node)
12441 return 1;
12442 if (arg == unknown_type_node)
12443 /* We can't deduce anything from this, but we might get all the
12444 template args from other function args. */
12445 return 0;
12447 /* If PARM uses template parameters, then we can't bail out here,
12448 even if ARG == PARM, since we won't record unifications for the
12449 template parameters. We might need them if we're trying to
12450 figure out which of two things is more specialized. */
12451 if (arg == parm && !uses_template_parms (parm))
12452 return 0;
12454 /* Immediately reject some pairs that won't unify because of
12455 cv-qualification mismatches. */
12456 if (TREE_CODE (arg) == TREE_CODE (parm)
12457 && TYPE_P (arg)
12458 /* It is the elements of the array which hold the cv quals of an array
12459 type, and the elements might be template type parms. We'll check
12460 when we recurse. */
12461 && TREE_CODE (arg) != ARRAY_TYPE
12462 /* We check the cv-qualifiers when unifying with template type
12463 parameters below. We want to allow ARG `const T' to unify with
12464 PARM `T' for example, when computing which of two templates
12465 is more specialized, for example. */
12466 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
12467 && !check_cv_quals_for_unify (strict_in, arg, parm))
12468 return 1;
12470 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
12471 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
12472 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
12473 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
12474 strict &= ~UNIFY_ALLOW_DERIVED;
12475 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12476 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
12478 switch (TREE_CODE (parm))
12480 case TYPENAME_TYPE:
12481 case SCOPE_REF:
12482 case UNBOUND_CLASS_TEMPLATE:
12483 /* In a type which contains a nested-name-specifier, template
12484 argument values cannot be deduced for template parameters used
12485 within the nested-name-specifier. */
12486 return 0;
12488 case TEMPLATE_TYPE_PARM:
12489 case TEMPLATE_TEMPLATE_PARM:
12490 case BOUND_TEMPLATE_TEMPLATE_PARM:
12491 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12492 if (tparm == error_mark_node)
12493 return 1;
12495 if (TEMPLATE_TYPE_LEVEL (parm)
12496 != template_decl_level (tparm))
12497 /* The PARM is not one we're trying to unify. Just check
12498 to see if it matches ARG. */
12499 return (TREE_CODE (arg) == TREE_CODE (parm)
12500 && same_type_p (parm, arg)) ? 0 : 1;
12501 idx = TEMPLATE_TYPE_IDX (parm);
12502 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
12505 /* Check for mixed types and values. */
12506 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12507 && TREE_CODE (tparm) != TYPE_DECL)
12508 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12509 && TREE_CODE (tparm) != TEMPLATE_DECL))
12510 return 1;
12512 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12514 /* ARG must be constructed from a template class or a template
12515 template parameter. */
12516 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
12517 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
12518 return 1;
12521 tree parmvec = TYPE_TI_ARGS (parm);
12522 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
12523 tree argtmplvec
12524 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
12525 int i;
12527 /* The resolution to DR150 makes clear that default
12528 arguments for an N-argument may not be used to bind T
12529 to a template template parameter with fewer than N
12530 parameters. It is not safe to permit the binding of
12531 default arguments as an extension, as that may change
12532 the meaning of a conforming program. Consider:
12534 struct Dense { static const unsigned int dim = 1; };
12536 template <template <typename> class View,
12537 typename Block>
12538 void operator+(float, View<Block> const&);
12540 template <typename Block,
12541 unsigned int Dim = Block::dim>
12542 struct Lvalue_proxy { operator float() const; };
12544 void
12545 test_1d (void) {
12546 Lvalue_proxy<Dense> p;
12547 float b;
12548 b + p;
12551 Here, if Lvalue_proxy is permitted to bind to View, then
12552 the global operator+ will be used; if they are not, the
12553 Lvalue_proxy will be converted to float. */
12554 if (coerce_template_parms (argtmplvec, parmvec,
12555 TYPE_TI_TEMPLATE (parm),
12556 tf_none,
12557 /*require_all_args=*/true,
12558 /*use_default_args=*/false)
12559 == error_mark_node)
12560 return 1;
12562 /* Deduce arguments T, i from TT<T> or TT<i>.
12563 We check each element of PARMVEC and ARGVEC individually
12564 rather than the whole TREE_VEC since they can have
12565 different number of elements. */
12567 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
12569 if (unify (tparms, targs,
12570 TREE_VEC_ELT (parmvec, i),
12571 TREE_VEC_ELT (argvec, i),
12572 UNIFY_ALLOW_NONE))
12573 return 1;
12576 arg = TYPE_TI_TEMPLATE (arg);
12578 /* Fall through to deduce template name. */
12581 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12582 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12584 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12586 /* Simple cases: Value already set, does match or doesn't. */
12587 if (targ != NULL_TREE && template_args_equal (targ, arg))
12588 return 0;
12589 else if (targ)
12590 return 1;
12592 else
12594 /* If PARM is `const T' and ARG is only `int', we don't have
12595 a match unless we are allowing additional qualification.
12596 If ARG is `const int' and PARM is just `T' that's OK;
12597 that binds `const int' to `T'. */
12598 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
12599 arg, parm))
12600 return 1;
12602 /* Consider the case where ARG is `const volatile int' and
12603 PARM is `const T'. Then, T should be `volatile int'. */
12604 arg = cp_build_qualified_type_real
12605 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
12606 if (arg == error_mark_node)
12607 return 1;
12609 /* Simple cases: Value already set, does match or doesn't. */
12610 if (targ != NULL_TREE && same_type_p (targ, arg))
12611 return 0;
12612 else if (targ)
12613 return 1;
12615 /* Make sure that ARG is not a variable-sized array. (Note
12616 that were talking about variable-sized arrays (like
12617 `int[n]'), rather than arrays of unknown size (like
12618 `int[]').) We'll get very confused by such a type since
12619 the bound of the array will not be computable in an
12620 instantiation. Besides, such types are not allowed in
12621 ISO C++, so we can do as we please here. */
12622 if (variably_modified_type_p (arg, NULL_TREE))
12623 return 1;
12626 /* If ARG is a parameter pack or an expansion, we cannot unify
12627 against it unless PARM is also a parameter pack. */
12628 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12629 && !template_parameter_pack_p (parm))
12630 return 1;
12632 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12633 return 0;
12635 case TEMPLATE_PARM_INDEX:
12636 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12637 if (tparm == error_mark_node)
12638 return 1;
12640 if (TEMPLATE_PARM_LEVEL (parm)
12641 != template_decl_level (tparm))
12642 /* The PARM is not one we're trying to unify. Just check
12643 to see if it matches ARG. */
12644 return !(TREE_CODE (arg) == TREE_CODE (parm)
12645 && cp_tree_equal (parm, arg));
12647 idx = TEMPLATE_PARM_IDX (parm);
12648 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12650 if (targ)
12651 return !cp_tree_equal (targ, arg);
12653 /* [temp.deduct.type] If, in the declaration of a function template
12654 with a non-type template-parameter, the non-type
12655 template-parameter is used in an expression in the function
12656 parameter-list and, if the corresponding template-argument is
12657 deduced, the template-argument type shall match the type of the
12658 template-parameter exactly, except that a template-argument
12659 deduced from an array bound may be of any integral type.
12660 The non-type parameter might use already deduced type parameters. */
12661 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
12662 if (!TREE_TYPE (arg))
12663 /* Template-parameter dependent expression. Just accept it for now.
12664 It will later be processed in convert_template_argument. */
12666 else if (same_type_p (TREE_TYPE (arg), tparm))
12667 /* OK */;
12668 else if ((strict & UNIFY_ALLOW_INTEGER)
12669 && (TREE_CODE (tparm) == INTEGER_TYPE
12670 || TREE_CODE (tparm) == BOOLEAN_TYPE))
12671 /* Convert the ARG to the type of PARM; the deduced non-type
12672 template argument must exactly match the types of the
12673 corresponding parameter. */
12674 arg = fold (build_nop (TREE_TYPE (parm), arg));
12675 else if (uses_template_parms (tparm))
12676 /* We haven't deduced the type of this parameter yet. Try again
12677 later. */
12678 return 0;
12679 else
12680 return 1;
12682 /* If ARG is a parameter pack or an expansion, we cannot unify
12683 against it unless PARM is also a parameter pack. */
12684 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12685 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
12686 return 1;
12688 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12689 return 0;
12691 case PTRMEM_CST:
12693 /* A pointer-to-member constant can be unified only with
12694 another constant. */
12695 if (TREE_CODE (arg) != PTRMEM_CST)
12696 return 1;
12698 /* Just unify the class member. It would be useless (and possibly
12699 wrong, depending on the strict flags) to unify also
12700 PTRMEM_CST_CLASS, because we want to be sure that both parm and
12701 arg refer to the same variable, even if through different
12702 classes. For instance:
12704 struct A { int x; };
12705 struct B : A { };
12707 Unification of &A::x and &B::x must succeed. */
12708 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
12709 PTRMEM_CST_MEMBER (arg), strict);
12712 case POINTER_TYPE:
12714 if (TREE_CODE (arg) != POINTER_TYPE)
12715 return 1;
12717 /* [temp.deduct.call]
12719 A can be another pointer or pointer to member type that can
12720 be converted to the deduced A via a qualification
12721 conversion (_conv.qual_).
12723 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
12724 This will allow for additional cv-qualification of the
12725 pointed-to types if appropriate. */
12727 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
12728 /* The derived-to-base conversion only persists through one
12729 level of pointers. */
12730 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
12732 return unify (tparms, targs, TREE_TYPE (parm),
12733 TREE_TYPE (arg), strict);
12736 case REFERENCE_TYPE:
12737 if (TREE_CODE (arg) != REFERENCE_TYPE)
12738 return 1;
12739 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12740 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12742 case ARRAY_TYPE:
12743 if (TREE_CODE (arg) != ARRAY_TYPE)
12744 return 1;
12745 if ((TYPE_DOMAIN (parm) == NULL_TREE)
12746 != (TYPE_DOMAIN (arg) == NULL_TREE))
12747 return 1;
12748 if (TYPE_DOMAIN (parm) != NULL_TREE)
12750 tree parm_max;
12751 tree arg_max;
12752 bool parm_cst;
12753 bool arg_cst;
12755 /* Our representation of array types uses "N - 1" as the
12756 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
12757 not an integer constant. We cannot unify arbitrarily
12758 complex expressions, so we eliminate the MINUS_EXPRs
12759 here. */
12760 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
12761 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
12762 if (!parm_cst)
12764 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
12765 parm_max = TREE_OPERAND (parm_max, 0);
12767 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
12768 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
12769 if (!arg_cst)
12771 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
12772 trying to unify the type of a variable with the type
12773 of a template parameter. For example:
12775 template <unsigned int N>
12776 void f (char (&) [N]);
12777 int g();
12778 void h(int i) {
12779 char a[g(i)];
12780 f(a);
12783 Here, the type of the ARG will be "int [g(i)]", and
12784 may be a SAVE_EXPR, etc. */
12785 if (TREE_CODE (arg_max) != MINUS_EXPR)
12786 return 1;
12787 arg_max = TREE_OPERAND (arg_max, 0);
12790 /* If only one of the bounds used a MINUS_EXPR, compensate
12791 by adding one to the other bound. */
12792 if (parm_cst && !arg_cst)
12793 parm_max = fold_build2 (PLUS_EXPR,
12794 integer_type_node,
12795 parm_max,
12796 integer_one_node);
12797 else if (arg_cst && !parm_cst)
12798 arg_max = fold_build2 (PLUS_EXPR,
12799 integer_type_node,
12800 arg_max,
12801 integer_one_node);
12803 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
12804 return 1;
12806 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12807 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12809 case REAL_TYPE:
12810 case COMPLEX_TYPE:
12811 case VECTOR_TYPE:
12812 case INTEGER_TYPE:
12813 case BOOLEAN_TYPE:
12814 case ENUMERAL_TYPE:
12815 case VOID_TYPE:
12816 if (TREE_CODE (arg) != TREE_CODE (parm))
12817 return 1;
12819 /* We have already checked cv-qualification at the top of the
12820 function. */
12821 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
12822 return 1;
12824 /* As far as unification is concerned, this wins. Later checks
12825 will invalidate it if necessary. */
12826 return 0;
12828 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
12829 /* Type INTEGER_CST can come from ordinary constant template args. */
12830 case INTEGER_CST:
12831 while (TREE_CODE (arg) == NOP_EXPR)
12832 arg = TREE_OPERAND (arg, 0);
12834 if (TREE_CODE (arg) != INTEGER_CST)
12835 return 1;
12836 return !tree_int_cst_equal (parm, arg);
12838 case TREE_VEC:
12840 int i;
12841 if (TREE_CODE (arg) != TREE_VEC)
12842 return 1;
12843 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
12844 return 1;
12845 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
12846 if (unify (tparms, targs,
12847 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
12848 UNIFY_ALLOW_NONE))
12849 return 1;
12850 return 0;
12853 case RECORD_TYPE:
12854 case UNION_TYPE:
12855 if (TREE_CODE (arg) != TREE_CODE (parm))
12856 return 1;
12858 if (TYPE_PTRMEMFUNC_P (parm))
12860 if (!TYPE_PTRMEMFUNC_P (arg))
12861 return 1;
12863 return unify (tparms, targs,
12864 TYPE_PTRMEMFUNC_FN_TYPE (parm),
12865 TYPE_PTRMEMFUNC_FN_TYPE (arg),
12866 strict);
12869 if (CLASSTYPE_TEMPLATE_INFO (parm))
12871 tree t = NULL_TREE;
12873 if (strict_in & UNIFY_ALLOW_DERIVED)
12875 /* First, we try to unify the PARM and ARG directly. */
12876 t = try_class_unification (tparms, targs,
12877 parm, arg);
12879 if (!t)
12881 /* Fallback to the special case allowed in
12882 [temp.deduct.call]:
12884 If P is a class, and P has the form
12885 template-id, then A can be a derived class of
12886 the deduced A. Likewise, if P is a pointer to
12887 a class of the form template-id, A can be a
12888 pointer to a derived class pointed to by the
12889 deduced A. */
12890 t = get_template_base (tparms, targs, parm, arg);
12892 if (!t)
12893 return 1;
12896 else if (CLASSTYPE_TEMPLATE_INFO (arg)
12897 && (CLASSTYPE_TI_TEMPLATE (parm)
12898 == CLASSTYPE_TI_TEMPLATE (arg)))
12899 /* Perhaps PARM is something like S<U> and ARG is S<int>.
12900 Then, we should unify `int' and `U'. */
12901 t = arg;
12902 else
12903 /* There's no chance of unification succeeding. */
12904 return 1;
12906 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
12907 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
12909 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
12910 return 1;
12911 return 0;
12913 case METHOD_TYPE:
12914 case FUNCTION_TYPE:
12915 if (TREE_CODE (arg) != TREE_CODE (parm))
12916 return 1;
12918 /* CV qualifications for methods can never be deduced, they must
12919 match exactly. We need to check them explicitly here,
12920 because type_unification_real treats them as any other
12921 cvqualified parameter. */
12922 if (TREE_CODE (parm) == METHOD_TYPE
12923 && (!check_cv_quals_for_unify
12924 (UNIFY_ALLOW_NONE,
12925 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
12926 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
12927 return 1;
12929 if (unify (tparms, targs, TREE_TYPE (parm),
12930 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
12931 return 1;
12932 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
12933 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
12934 LOOKUP_NORMAL);
12936 case OFFSET_TYPE:
12937 /* Unify a pointer to member with a pointer to member function, which
12938 deduces the type of the member as a function type. */
12939 if (TYPE_PTRMEMFUNC_P (arg))
12941 tree method_type;
12942 tree fntype;
12943 cp_cv_quals cv_quals;
12945 /* Check top-level cv qualifiers */
12946 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
12947 return 1;
12949 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12950 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
12951 return 1;
12953 /* Determine the type of the function we are unifying against. */
12954 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
12955 fntype =
12956 build_function_type (TREE_TYPE (method_type),
12957 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
12959 /* Extract the cv-qualifiers of the member function from the
12960 implicit object parameter and place them on the function
12961 type to be restored later. */
12962 cv_quals =
12963 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
12964 fntype = build_qualified_type (fntype, cv_quals);
12965 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
12968 if (TREE_CODE (arg) != OFFSET_TYPE)
12969 return 1;
12970 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12971 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
12972 return 1;
12973 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12974 strict);
12976 case CONST_DECL:
12977 if (DECL_TEMPLATE_PARM_P (parm))
12978 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
12979 if (arg != integral_constant_value (parm))
12980 return 1;
12981 return 0;
12983 case FIELD_DECL:
12984 case TEMPLATE_DECL:
12985 /* Matched cases are handled by the ARG == PARM test above. */
12986 return 1;
12988 case TYPE_ARGUMENT_PACK:
12989 case NONTYPE_ARGUMENT_PACK:
12991 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
12992 tree packed_args = ARGUMENT_PACK_ARGS (arg);
12993 int i, len = TREE_VEC_LENGTH (packed_parms);
12994 int argslen = TREE_VEC_LENGTH (packed_args);
12995 int parm_variadic_p = 0;
12997 /* Check if the parameters end in a pack, making them variadic. */
12998 if (len > 0
12999 && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1)))
13000 parm_variadic_p = 1;
13002 /* If we don't have enough arguments to satisfy the parameters
13003 (not counting the pack expression at the end), or we have
13004 too many arguments for a parameter list that doesn't end in
13005 a pack expression, we can't unify. */
13006 if (argslen < (len - parm_variadic_p)
13007 || (argslen > len && !parm_variadic_p))
13008 return 1;
13010 /* Unify all of the parameters that precede the (optional)
13011 pack expression. */
13012 for (i = 0; i < len - parm_variadic_p; ++i)
13014 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
13015 TREE_VEC_ELT (packed_args, i), strict))
13016 return 1;
13019 if (parm_variadic_p)
13020 return unify_pack_expansion (tparms, targs,
13021 packed_parms, packed_args,
13022 strict, /*call_args_p=*/false,
13023 /*subr=*/false);
13024 return 0;
13027 break;
13029 case TYPEOF_TYPE:
13030 case DECLTYPE_TYPE:
13031 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
13032 nodes. */
13033 return 0;
13035 default:
13036 gcc_assert (EXPR_P (parm));
13038 /* We must be looking at an expression. This can happen with
13039 something like:
13041 template <int I>
13042 void foo(S<I>, S<I + 2>);
13044 This is a "nondeduced context":
13046 [deduct.type]
13048 The nondeduced contexts are:
13050 --A type that is a template-id in which one or more of
13051 the template-arguments is an expression that references
13052 a template-parameter.
13054 In these cases, we assume deduction succeeded, but don't
13055 actually infer any unifications. */
13057 if (!uses_template_parms (parm)
13058 && !template_args_equal (parm, arg))
13059 return 1;
13060 else
13061 return 0;
13065 /* Note that DECL can be defined in this translation unit, if
13066 required. */
13068 static void
13069 mark_definable (tree decl)
13071 tree clone;
13072 DECL_NOT_REALLY_EXTERN (decl) = 1;
13073 FOR_EACH_CLONE (clone, decl)
13074 DECL_NOT_REALLY_EXTERN (clone) = 1;
13077 /* Called if RESULT is explicitly instantiated, or is a member of an
13078 explicitly instantiated class. */
13080 void
13081 mark_decl_instantiated (tree result, int extern_p)
13083 SET_DECL_EXPLICIT_INSTANTIATION (result);
13085 /* If this entity has already been written out, it's too late to
13086 make any modifications. */
13087 if (TREE_ASM_WRITTEN (result))
13088 return;
13090 if (TREE_CODE (result) != FUNCTION_DECL)
13091 /* The TREE_PUBLIC flag for function declarations will have been
13092 set correctly by tsubst. */
13093 TREE_PUBLIC (result) = 1;
13095 /* This might have been set by an earlier implicit instantiation. */
13096 DECL_COMDAT (result) = 0;
13098 if (extern_p)
13099 DECL_NOT_REALLY_EXTERN (result) = 0;
13100 else
13102 mark_definable (result);
13103 /* Always make artificials weak. */
13104 if (DECL_ARTIFICIAL (result) && flag_weak)
13105 comdat_linkage (result);
13106 /* For WIN32 we also want to put explicit instantiations in
13107 linkonce sections. */
13108 else if (TREE_PUBLIC (result))
13109 maybe_make_one_only (result);
13112 /* If EXTERN_P, then this function will not be emitted -- unless
13113 followed by an explicit instantiation, at which point its linkage
13114 will be adjusted. If !EXTERN_P, then this function will be
13115 emitted here. In neither circumstance do we want
13116 import_export_decl to adjust the linkage. */
13117 DECL_INTERFACE_KNOWN (result) = 1;
13120 /* Given two function templates PAT1 and PAT2, return:
13122 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
13123 -1 if PAT2 is more specialized than PAT1.
13124 0 if neither is more specialized.
13126 LEN indicates the number of parameters we should consider
13127 (defaulted parameters should not be considered).
13129 The 1998 std underspecified function template partial ordering, and
13130 DR214 addresses the issue. We take pairs of arguments, one from
13131 each of the templates, and deduce them against each other. One of
13132 the templates will be more specialized if all the *other*
13133 template's arguments deduce against its arguments and at least one
13134 of its arguments *does* *not* deduce against the other template's
13135 corresponding argument. Deduction is done as for class templates.
13136 The arguments used in deduction have reference and top level cv
13137 qualifiers removed. Iff both arguments were originally reference
13138 types *and* deduction succeeds in both directions, the template
13139 with the more cv-qualified argument wins for that pairing (if
13140 neither is more cv-qualified, they both are equal). Unlike regular
13141 deduction, after all the arguments have been deduced in this way,
13142 we do *not* verify the deduced template argument values can be
13143 substituted into non-deduced contexts, nor do we have to verify
13144 that all template arguments have been deduced. */
13147 more_specialized_fn (tree pat1, tree pat2, int len)
13149 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
13150 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
13151 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
13152 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
13153 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
13154 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
13155 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
13156 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
13157 int better1 = 0;
13158 int better2 = 0;
13160 /* Remove the this parameter from non-static member functions. If
13161 one is a non-static member function and the other is not a static
13162 member function, remove the first parameter from that function
13163 also. This situation occurs for operator functions where we
13164 locate both a member function (with this pointer) and non-member
13165 operator (with explicit first operand). */
13166 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
13168 len--; /* LEN is the number of significant arguments for DECL1 */
13169 args1 = TREE_CHAIN (args1);
13170 if (!DECL_STATIC_FUNCTION_P (decl2))
13171 args2 = TREE_CHAIN (args2);
13173 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
13175 args2 = TREE_CHAIN (args2);
13176 if (!DECL_STATIC_FUNCTION_P (decl1))
13178 len--;
13179 args1 = TREE_CHAIN (args1);
13183 /* If only one is a conversion operator, they are unordered. */
13184 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
13185 return 0;
13187 /* Consider the return type for a conversion function */
13188 if (DECL_CONV_FN_P (decl1))
13190 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
13191 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
13192 len++;
13195 processing_template_decl++;
13197 while (len--)
13199 tree arg1 = TREE_VALUE (args1);
13200 tree arg2 = TREE_VALUE (args2);
13201 int deduce1, deduce2;
13202 int quals1 = -1;
13203 int quals2 = -1;
13205 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13206 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13208 /* When both arguments are pack expansions, we need only
13209 unify the patterns themselves. */
13210 arg1 = PACK_EXPANSION_PATTERN (arg1);
13211 arg2 = PACK_EXPANSION_PATTERN (arg2);
13213 /* This is the last comparison we need to do. */
13214 len = 0;
13217 if (TREE_CODE (arg1) == REFERENCE_TYPE)
13219 arg1 = TREE_TYPE (arg1);
13220 quals1 = cp_type_quals (arg1);
13223 if (TREE_CODE (arg2) == REFERENCE_TYPE)
13225 arg2 = TREE_TYPE (arg2);
13226 quals2 = cp_type_quals (arg2);
13229 if ((quals1 < 0) != (quals2 < 0))
13231 /* Only of the args is a reference, see if we should apply
13232 array/function pointer decay to it. This is not part of
13233 DR214, but is, IMHO, consistent with the deduction rules
13234 for the function call itself, and with our earlier
13235 implementation of the underspecified partial ordering
13236 rules. (nathan). */
13237 if (quals1 >= 0)
13239 switch (TREE_CODE (arg1))
13241 case ARRAY_TYPE:
13242 arg1 = TREE_TYPE (arg1);
13243 /* FALLTHROUGH. */
13244 case FUNCTION_TYPE:
13245 arg1 = build_pointer_type (arg1);
13246 break;
13248 default:
13249 break;
13252 else
13254 switch (TREE_CODE (arg2))
13256 case ARRAY_TYPE:
13257 arg2 = TREE_TYPE (arg2);
13258 /* FALLTHROUGH. */
13259 case FUNCTION_TYPE:
13260 arg2 = build_pointer_type (arg2);
13261 break;
13263 default:
13264 break;
13269 arg1 = TYPE_MAIN_VARIANT (arg1);
13270 arg2 = TYPE_MAIN_VARIANT (arg2);
13272 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
13274 int i, len2 = list_length (args2);
13275 tree parmvec = make_tree_vec (1);
13276 tree argvec = make_tree_vec (len2);
13277 tree ta = args2;
13279 /* Setup the parameter vector, which contains only ARG1. */
13280 TREE_VEC_ELT (parmvec, 0) = arg1;
13282 /* Setup the argument vector, which contains the remaining
13283 arguments. */
13284 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
13285 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13287 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
13288 argvec, UNIFY_ALLOW_NONE,
13289 /*call_args_p=*/false,
13290 /*subr=*/0);
13292 /* We cannot deduce in the other direction, because ARG1 is
13293 a pack expansion but ARG2 is not. */
13294 deduce2 = 0;
13296 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13298 int i, len1 = list_length (args1);
13299 tree parmvec = make_tree_vec (1);
13300 tree argvec = make_tree_vec (len1);
13301 tree ta = args1;
13303 /* Setup the parameter vector, which contains only ARG1. */
13304 TREE_VEC_ELT (parmvec, 0) = arg2;
13306 /* Setup the argument vector, which contains the remaining
13307 arguments. */
13308 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
13309 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13311 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
13312 argvec, UNIFY_ALLOW_NONE,
13313 /*call_args_p=*/false,
13314 /*subr=*/0);
13316 /* We cannot deduce in the other direction, because ARG2 is
13317 a pack expansion but ARG1 is not.*/
13318 deduce1 = 0;
13321 else
13323 /* The normal case, where neither argument is a pack
13324 expansion. */
13325 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
13326 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
13329 if (!deduce1)
13330 better2 = -1;
13331 if (!deduce2)
13332 better1 = -1;
13333 if (better1 < 0 && better2 < 0)
13334 /* We've failed to deduce something in either direction.
13335 These must be unordered. */
13336 break;
13338 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
13340 /* Deduces in both directions, see if quals can
13341 disambiguate. Pretend the worse one failed to deduce. */
13342 if ((quals1 & quals2) == quals2)
13343 deduce1 = 0;
13344 if ((quals1 & quals2) == quals1)
13345 deduce2 = 0;
13347 if (deduce1 && !deduce2 && !better2)
13348 better2 = 1;
13349 if (deduce2 && !deduce1 && !better1)
13350 better1 = 1;
13352 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13353 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13354 /* We have already processed all of the arguments in our
13355 handing of the pack expansion type. */
13356 len = 0;
13358 args1 = TREE_CHAIN (args1);
13359 args2 = TREE_CHAIN (args2);
13362 processing_template_decl--;
13364 /* All things being equal, if the next argument is a pack expansion
13365 for one function but not for the other, prefer the
13366 non-variadic function. */
13367 if ((better1 > 0) - (better2 > 0) == 0
13368 && args1 && TREE_VALUE (args1)
13369 && args2 && TREE_VALUE (args2))
13371 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
13372 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
13373 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
13374 return 1;
13377 return (better1 > 0) - (better2 > 0);
13380 /* Determine which of two partial specializations is more specialized.
13382 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13383 to the first partial specialization. The TREE_VALUE is the
13384 innermost set of template parameters for the partial
13385 specialization. PAT2 is similar, but for the second template.
13387 Return 1 if the first partial specialization is more specialized;
13388 -1 if the second is more specialized; 0 if neither is more
13389 specialized.
13391 See [temp.class.order] for information about determining which of
13392 two templates is more specialized. */
13394 static int
13395 more_specialized_class (tree pat1, tree pat2)
13397 tree targs;
13398 tree tmpl1, tmpl2;
13399 int winner = 0;
13400 bool any_deductions = false;
13402 tmpl1 = TREE_TYPE (pat1);
13403 tmpl2 = TREE_TYPE (pat2);
13405 /* Just like what happens for functions, if we are ordering between
13406 different class template specializations, we may encounter dependent
13407 types in the arguments, and we need our dependency check functions
13408 to behave correctly. */
13409 ++processing_template_decl;
13410 targs = get_class_bindings (TREE_VALUE (pat1),
13411 CLASSTYPE_TI_ARGS (tmpl1),
13412 CLASSTYPE_TI_ARGS (tmpl2));
13413 if (targs)
13415 --winner;
13416 any_deductions = true;
13419 targs = get_class_bindings (TREE_VALUE (pat2),
13420 CLASSTYPE_TI_ARGS (tmpl2),
13421 CLASSTYPE_TI_ARGS (tmpl1));
13422 if (targs)
13424 ++winner;
13425 any_deductions = true;
13427 --processing_template_decl;
13429 /* In the case of a tie where at least one of the class templates
13430 has a parameter pack at the end, the template with the most
13431 non-packed parameters wins. */
13432 if (winner == 0
13433 && any_deductions
13434 && (template_args_variadic_p (TREE_PURPOSE (pat1))
13435 || template_args_variadic_p (TREE_PURPOSE (pat2))))
13437 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
13438 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
13439 int len1 = TREE_VEC_LENGTH (args1);
13440 int len2 = TREE_VEC_LENGTH (args2);
13442 /* We don't count the pack expansion at the end. */
13443 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
13444 --len1;
13445 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
13446 --len2;
13448 if (len1 > len2)
13449 return 1;
13450 else if (len1 < len2)
13451 return -1;
13454 return winner;
13457 /* Return the template arguments that will produce the function signature
13458 DECL from the function template FN, with the explicit template
13459 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13460 also match. Return NULL_TREE if no satisfactory arguments could be
13461 found. */
13463 static tree
13464 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
13466 int ntparms = DECL_NTPARMS (fn);
13467 tree targs = make_tree_vec (ntparms);
13468 tree decl_type;
13469 tree decl_arg_types;
13471 /* Substitute the explicit template arguments into the type of DECL.
13472 The call to fn_type_unification will handle substitution into the
13473 FN. */
13474 decl_type = TREE_TYPE (decl);
13475 if (explicit_args && uses_template_parms (decl_type))
13477 tree tmpl;
13478 tree converted_args;
13480 if (DECL_TEMPLATE_INFO (decl))
13481 tmpl = DECL_TI_TEMPLATE (decl);
13482 else
13483 /* We can get here for some invalid specializations. */
13484 return NULL_TREE;
13486 converted_args
13487 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
13488 explicit_args, NULL_TREE,
13489 tf_none,
13490 /*require_all_args=*/false,
13491 /*use_default_args=*/false);
13492 if (converted_args == error_mark_node)
13493 return NULL_TREE;
13495 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
13496 if (decl_type == error_mark_node)
13497 return NULL_TREE;
13500 /* Never do unification on the 'this' parameter. */
13501 decl_arg_types = skip_artificial_parms_for (decl,
13502 TYPE_ARG_TYPES (decl_type));
13504 if (fn_type_unification (fn, explicit_args, targs,
13505 decl_arg_types,
13506 (check_rettype || DECL_CONV_FN_P (fn)
13507 ? TREE_TYPE (decl_type) : NULL_TREE),
13508 DEDUCE_EXACT, LOOKUP_NORMAL))
13509 return NULL_TREE;
13511 return targs;
13514 /* Return the innermost template arguments that, when applied to a
13515 template specialization whose innermost template parameters are
13516 TPARMS, and whose specialization arguments are PARMS, yield the
13517 ARGS.
13519 For example, suppose we have:
13521 template <class T, class U> struct S {};
13522 template <class T> struct S<T*, int> {};
13524 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13525 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13526 int}. The resulting vector will be {double}, indicating that `T'
13527 is bound to `double'. */
13529 static tree
13530 get_class_bindings (tree tparms, tree spec_args, tree args)
13532 int i, ntparms = TREE_VEC_LENGTH (tparms);
13533 tree deduced_args;
13534 tree innermost_deduced_args;
13536 innermost_deduced_args = make_tree_vec (ntparms);
13537 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13539 deduced_args = copy_node (args);
13540 SET_TMPL_ARGS_LEVEL (deduced_args,
13541 TMPL_ARGS_DEPTH (deduced_args),
13542 innermost_deduced_args);
13544 else
13545 deduced_args = innermost_deduced_args;
13547 if (unify (tparms, deduced_args,
13548 INNERMOST_TEMPLATE_ARGS (spec_args),
13549 INNERMOST_TEMPLATE_ARGS (args),
13550 UNIFY_ALLOW_NONE))
13551 return NULL_TREE;
13553 for (i = 0; i < ntparms; ++i)
13554 if (! TREE_VEC_ELT (innermost_deduced_args, i))
13555 return NULL_TREE;
13557 /* Verify that nondeduced template arguments agree with the type
13558 obtained from argument deduction.
13560 For example:
13562 struct A { typedef int X; };
13563 template <class T, class U> struct C {};
13564 template <class T> struct C<T, typename T::X> {};
13566 Then with the instantiation `C<A, int>', we can deduce that
13567 `T' is `A' but unify () does not check whether `typename T::X'
13568 is `int'. */
13569 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
13570 if (spec_args == error_mark_node
13571 /* We only need to check the innermost arguments; the other
13572 arguments will always agree. */
13573 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
13574 INNERMOST_TEMPLATE_ARGS (args)))
13575 return NULL_TREE;
13577 return deduced_args;
13580 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13581 Return the TREE_LIST node with the most specialized template, if
13582 any. If there is no most specialized template, the error_mark_node
13583 is returned.
13585 Note that this function does not look at, or modify, the
13586 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13587 returned is one of the elements of INSTANTIATIONS, callers may
13588 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13589 and retrieve it from the value returned. */
13591 tree
13592 most_specialized_instantiation (tree templates)
13594 tree fn, champ;
13596 ++processing_template_decl;
13598 champ = templates;
13599 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
13601 int fate = 0;
13603 if (get_bindings (TREE_VALUE (champ),
13604 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13605 NULL_TREE, /*check_ret=*/false))
13606 fate--;
13608 if (get_bindings (TREE_VALUE (fn),
13609 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13610 NULL_TREE, /*check_ret=*/false))
13611 fate++;
13613 if (fate == -1)
13614 champ = fn;
13615 else if (!fate)
13617 /* Equally specialized, move to next function. If there
13618 is no next function, nothing's most specialized. */
13619 fn = TREE_CHAIN (fn);
13620 champ = fn;
13621 if (!fn)
13622 break;
13626 if (champ)
13627 /* Now verify that champ is better than everything earlier in the
13628 instantiation list. */
13629 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
13630 if (get_bindings (TREE_VALUE (champ),
13631 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13632 NULL_TREE, /*check_ret=*/false)
13633 || !get_bindings (TREE_VALUE (fn),
13634 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13635 NULL_TREE, /*check_ret=*/false))
13637 champ = NULL_TREE;
13638 break;
13641 processing_template_decl--;
13643 if (!champ)
13644 return error_mark_node;
13646 return champ;
13649 /* If DECL is a specialization of some template, return the most
13650 general such template. Otherwise, returns NULL_TREE.
13652 For example, given:
13654 template <class T> struct S { template <class U> void f(U); };
13656 if TMPL is `template <class U> void S<int>::f(U)' this will return
13657 the full template. This function will not trace past partial
13658 specializations, however. For example, given in addition:
13660 template <class T> struct S<T*> { template <class U> void f(U); };
13662 if TMPL is `template <class U> void S<int*>::f(U)' this will return
13663 `template <class T> template <class U> S<T*>::f(U)'. */
13665 tree
13666 most_general_template (tree decl)
13668 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
13669 an immediate specialization. */
13670 if (TREE_CODE (decl) == FUNCTION_DECL)
13672 if (DECL_TEMPLATE_INFO (decl)) {
13673 decl = DECL_TI_TEMPLATE (decl);
13675 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
13676 template friend. */
13677 if (TREE_CODE (decl) != TEMPLATE_DECL)
13678 return NULL_TREE;
13679 } else
13680 return NULL_TREE;
13683 /* Look for more and more general templates. */
13684 while (DECL_TEMPLATE_INFO (decl))
13686 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
13687 (See cp-tree.h for details.) */
13688 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
13689 break;
13691 if (CLASS_TYPE_P (TREE_TYPE (decl))
13692 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
13693 break;
13695 /* Stop if we run into an explicitly specialized class template. */
13696 if (!DECL_NAMESPACE_SCOPE_P (decl)
13697 && DECL_CONTEXT (decl)
13698 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
13699 break;
13701 decl = DECL_TI_TEMPLATE (decl);
13704 return decl;
13707 /* Return the most specialized of the class template partial
13708 specializations of TMPL which can produce TYPE, a specialization of
13709 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
13710 a _TYPE node corresponding to the partial specialization, while the
13711 TREE_PURPOSE is the set of template arguments that must be
13712 substituted into the TREE_TYPE in order to generate TYPE.
13714 If the choice of partial specialization is ambiguous, a diagnostic
13715 is issued, and the error_mark_node is returned. If there are no
13716 partial specializations of TMPL matching TYPE, then NULL_TREE is
13717 returned. */
13719 static tree
13720 most_specialized_class (tree type, tree tmpl)
13722 tree list = NULL_TREE;
13723 tree t;
13724 tree champ;
13725 int fate;
13726 bool ambiguous_p;
13727 tree args;
13728 tree outer_args = NULL_TREE;
13730 tmpl = most_general_template (tmpl);
13731 args = CLASSTYPE_TI_ARGS (type);
13733 /* For determining which partial specialization to use, only the
13734 innermost args are interesting. */
13735 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13737 outer_args = strip_innermost_template_args (args, 1);
13738 args = INNERMOST_TEMPLATE_ARGS (args);
13741 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
13743 tree partial_spec_args;
13744 tree spec_args;
13745 tree parms = TREE_VALUE (t);
13747 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
13748 if (outer_args)
13750 int i;
13752 ++processing_template_decl;
13754 /* Discard the outer levels of args, and then substitute in the
13755 template args from the enclosing class. */
13756 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
13757 partial_spec_args = tsubst_template_args
13758 (partial_spec_args, outer_args, tf_none, NULL_TREE);
13760 /* PARMS already refers to just the innermost parms, but the
13761 template parms in partial_spec_args had their levels lowered
13762 by tsubst, so we need to do the same for the parm list. We
13763 can't just tsubst the TREE_VEC itself, as tsubst wants to
13764 treat a TREE_VEC as an argument vector. */
13765 parms = copy_node (parms);
13766 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
13767 TREE_VEC_ELT (parms, i) =
13768 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
13770 --processing_template_decl;
13772 spec_args = get_class_bindings (parms,
13773 partial_spec_args,
13774 args);
13775 if (spec_args)
13777 if (outer_args)
13778 spec_args = add_to_template_args (outer_args, spec_args);
13779 list = tree_cons (spec_args, TREE_VALUE (t), list);
13780 TREE_TYPE (list) = TREE_TYPE (t);
13784 if (! list)
13785 return NULL_TREE;
13787 ambiguous_p = false;
13788 t = list;
13789 champ = t;
13790 t = TREE_CHAIN (t);
13791 for (; t; t = TREE_CHAIN (t))
13793 fate = more_specialized_class (champ, t);
13794 if (fate == 1)
13796 else
13798 if (fate == 0)
13800 t = TREE_CHAIN (t);
13801 if (! t)
13803 ambiguous_p = true;
13804 break;
13807 champ = t;
13811 if (!ambiguous_p)
13812 for (t = list; t && t != champ; t = TREE_CHAIN (t))
13814 fate = more_specialized_class (champ, t);
13815 if (fate != 1)
13817 ambiguous_p = true;
13818 break;
13822 if (ambiguous_p)
13824 const char *str = "candidates are:";
13825 error ("ambiguous class template instantiation for %q#T", type);
13826 for (t = list; t; t = TREE_CHAIN (t))
13828 error ("%s %+#T", str, TREE_TYPE (t));
13829 str = " ";
13831 return error_mark_node;
13834 return champ;
13837 /* Explicitly instantiate DECL. */
13839 void
13840 do_decl_instantiation (tree decl, tree storage)
13842 tree result = NULL_TREE;
13843 int extern_p = 0;
13845 if (!decl || decl == error_mark_node)
13846 /* An error occurred, for which grokdeclarator has already issued
13847 an appropriate message. */
13848 return;
13849 else if (! DECL_LANG_SPECIFIC (decl))
13851 error ("explicit instantiation of non-template %q#D", decl);
13852 return;
13854 else if (TREE_CODE (decl) == VAR_DECL)
13856 /* There is an asymmetry here in the way VAR_DECLs and
13857 FUNCTION_DECLs are handled by grokdeclarator. In the case of
13858 the latter, the DECL we get back will be marked as a
13859 template instantiation, and the appropriate
13860 DECL_TEMPLATE_INFO will be set up. This does not happen for
13861 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
13862 should handle VAR_DECLs as it currently handles
13863 FUNCTION_DECLs. */
13864 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
13865 if (!result || TREE_CODE (result) != VAR_DECL)
13867 error ("no matching template for %qD found", decl);
13868 return;
13870 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
13872 error ("type %qT for explicit instantiation %qD does not match "
13873 "declared type %qT", TREE_TYPE (result), decl,
13874 TREE_TYPE (decl));
13875 return;
13878 else if (TREE_CODE (decl) != FUNCTION_DECL)
13880 error ("explicit instantiation of %q#D", decl);
13881 return;
13883 else
13884 result = decl;
13886 /* Check for various error cases. Note that if the explicit
13887 instantiation is valid the RESULT will currently be marked as an
13888 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
13889 until we get here. */
13891 if (DECL_TEMPLATE_SPECIALIZATION (result))
13893 /* DR 259 [temp.spec].
13895 Both an explicit instantiation and a declaration of an explicit
13896 specialization shall not appear in a program unless the explicit
13897 instantiation follows a declaration of the explicit specialization.
13899 For a given set of template parameters, if an explicit
13900 instantiation of a template appears after a declaration of an
13901 explicit specialization for that template, the explicit
13902 instantiation has no effect. */
13903 return;
13905 else if (DECL_EXPLICIT_INSTANTIATION (result))
13907 /* [temp.spec]
13909 No program shall explicitly instantiate any template more
13910 than once.
13912 We check DECL_NOT_REALLY_EXTERN so as not to complain when
13913 the first instantiation was `extern' and the second is not,
13914 and EXTERN_P for the opposite case. */
13915 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
13916 pedwarn ("duplicate explicit instantiation of %q#D", result);
13917 /* If an "extern" explicit instantiation follows an ordinary
13918 explicit instantiation, the template is instantiated. */
13919 if (extern_p)
13920 return;
13922 else if (!DECL_IMPLICIT_INSTANTIATION (result))
13924 error ("no matching template for %qD found", result);
13925 return;
13927 else if (!DECL_TEMPLATE_INFO (result))
13929 pedwarn ("explicit instantiation of non-template %q#D", result);
13930 return;
13933 if (storage == NULL_TREE)
13935 else if (storage == ridpointers[(int) RID_EXTERN])
13937 if (pedantic && !in_system_header)
13938 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
13939 "instantiations");
13940 extern_p = 1;
13942 else
13943 error ("storage class %qD applied to template instantiation", storage);
13945 check_explicit_instantiation_namespace (result);
13946 mark_decl_instantiated (result, extern_p);
13947 if (! extern_p)
13948 instantiate_decl (result, /*defer_ok=*/1,
13949 /*expl_inst_class_mem_p=*/false);
13952 static void
13953 mark_class_instantiated (tree t, int extern_p)
13955 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
13956 SET_CLASSTYPE_INTERFACE_KNOWN (t);
13957 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
13958 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
13959 if (! extern_p)
13961 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
13962 rest_of_type_compilation (t, 1);
13966 /* Called from do_type_instantiation through binding_table_foreach to
13967 do recursive instantiation for the type bound in ENTRY. */
13968 static void
13969 bt_instantiate_type_proc (binding_entry entry, void *data)
13971 tree storage = *(tree *) data;
13973 if (IS_AGGR_TYPE (entry->type)
13974 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
13975 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
13978 /* Called from do_type_instantiation to instantiate a member
13979 (a member function or a static member variable) of an
13980 explicitly instantiated class template. */
13981 static void
13982 instantiate_class_member (tree decl, int extern_p)
13984 mark_decl_instantiated (decl, extern_p);
13985 if (! extern_p)
13986 instantiate_decl (decl, /*defer_ok=*/1,
13987 /*expl_inst_class_mem_p=*/true);
13990 /* Perform an explicit instantiation of template class T. STORAGE, if
13991 non-null, is the RID for extern, inline or static. COMPLAIN is
13992 nonzero if this is called from the parser, zero if called recursively,
13993 since the standard is unclear (as detailed below). */
13995 void
13996 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
13998 int extern_p = 0;
13999 int nomem_p = 0;
14000 int static_p = 0;
14001 int previous_instantiation_extern_p = 0;
14003 if (TREE_CODE (t) == TYPE_DECL)
14004 t = TREE_TYPE (t);
14006 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
14008 error ("explicit instantiation of non-template type %qT", t);
14009 return;
14012 complete_type (t);
14014 if (!COMPLETE_TYPE_P (t))
14016 if (complain & tf_error)
14017 error ("explicit instantiation of %q#T before definition of template",
14019 return;
14022 if (storage != NULL_TREE)
14024 if (pedantic && !in_system_header)
14025 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
14026 storage);
14028 if (storage == ridpointers[(int) RID_INLINE])
14029 nomem_p = 1;
14030 else if (storage == ridpointers[(int) RID_EXTERN])
14031 extern_p = 1;
14032 else if (storage == ridpointers[(int) RID_STATIC])
14033 static_p = 1;
14034 else
14036 error ("storage class %qD applied to template instantiation",
14037 storage);
14038 extern_p = 0;
14042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
14044 /* DR 259 [temp.spec].
14046 Both an explicit instantiation and a declaration of an explicit
14047 specialization shall not appear in a program unless the explicit
14048 instantiation follows a declaration of the explicit specialization.
14050 For a given set of template parameters, if an explicit
14051 instantiation of a template appears after a declaration of an
14052 explicit specialization for that template, the explicit
14053 instantiation has no effect. */
14054 return;
14056 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
14058 /* [temp.spec]
14060 No program shall explicitly instantiate any template more
14061 than once.
14063 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
14064 instantiation was `extern'. If EXTERN_P then the second is.
14065 These cases are OK. */
14066 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
14068 if (!previous_instantiation_extern_p && !extern_p
14069 && (complain & tf_error))
14070 pedwarn ("duplicate explicit instantiation of %q#T", t);
14072 /* If we've already instantiated the template, just return now. */
14073 if (!CLASSTYPE_INTERFACE_ONLY (t))
14074 return;
14077 check_explicit_instantiation_namespace (TYPE_NAME (t));
14078 mark_class_instantiated (t, extern_p);
14080 if (nomem_p)
14081 return;
14084 tree tmp;
14086 /* In contrast to implicit instantiation, where only the
14087 declarations, and not the definitions, of members are
14088 instantiated, we have here:
14090 [temp.explicit]
14092 The explicit instantiation of a class template specialization
14093 implies the instantiation of all of its members not
14094 previously explicitly specialized in the translation unit
14095 containing the explicit instantiation.
14097 Of course, we can't instantiate member template classes, since
14098 we don't have any arguments for them. Note that the standard
14099 is unclear on whether the instantiation of the members are
14100 *explicit* instantiations or not. However, the most natural
14101 interpretation is that it should be an explicit instantiation. */
14103 if (! static_p)
14104 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
14105 if (TREE_CODE (tmp) == FUNCTION_DECL
14106 && DECL_TEMPLATE_INSTANTIATION (tmp))
14107 instantiate_class_member (tmp, extern_p);
14109 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
14110 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
14111 instantiate_class_member (tmp, extern_p);
14113 if (CLASSTYPE_NESTED_UTDS (t))
14114 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
14115 bt_instantiate_type_proc, &storage);
14119 /* Given a function DECL, which is a specialization of TMPL, modify
14120 DECL to be a re-instantiation of TMPL with the same template
14121 arguments. TMPL should be the template into which tsubst'ing
14122 should occur for DECL, not the most general template.
14124 One reason for doing this is a scenario like this:
14126 template <class T>
14127 void f(const T&, int i);
14129 void g() { f(3, 7); }
14131 template <class T>
14132 void f(const T& t, const int i) { }
14134 Note that when the template is first instantiated, with
14135 instantiate_template, the resulting DECL will have no name for the
14136 first parameter, and the wrong type for the second. So, when we go
14137 to instantiate the DECL, we regenerate it. */
14139 static void
14140 regenerate_decl_from_template (tree decl, tree tmpl)
14142 /* The arguments used to instantiate DECL, from the most general
14143 template. */
14144 tree args;
14145 tree code_pattern;
14147 args = DECL_TI_ARGS (decl);
14148 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
14150 /* Make sure that we can see identifiers, and compute access
14151 correctly. */
14152 push_access_scope (decl);
14154 if (TREE_CODE (decl) == FUNCTION_DECL)
14156 tree decl_parm;
14157 tree pattern_parm;
14158 tree specs;
14159 int args_depth;
14160 int parms_depth;
14162 args_depth = TMPL_ARGS_DEPTH (args);
14163 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
14164 if (args_depth > parms_depth)
14165 args = get_innermost_template_args (args, parms_depth);
14167 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
14168 args, tf_error, NULL_TREE);
14169 if (specs)
14170 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
14171 specs);
14173 /* Merge parameter declarations. */
14174 decl_parm = skip_artificial_parms_for (decl,
14175 DECL_ARGUMENTS (decl));
14176 pattern_parm
14177 = skip_artificial_parms_for (code_pattern,
14178 DECL_ARGUMENTS (code_pattern));
14179 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
14181 tree parm_type;
14182 tree attributes;
14184 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
14185 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
14186 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
14187 NULL_TREE);
14188 parm_type = type_decays_to (parm_type);
14189 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
14190 TREE_TYPE (decl_parm) = parm_type;
14191 attributes = DECL_ATTRIBUTES (pattern_parm);
14192 if (DECL_ATTRIBUTES (decl_parm) != attributes)
14194 DECL_ATTRIBUTES (decl_parm) = attributes;
14195 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
14197 decl_parm = TREE_CHAIN (decl_parm);
14198 pattern_parm = TREE_CHAIN (pattern_parm);
14200 /* Merge any parameters that match with the function parameter
14201 pack. */
14202 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
14204 int i, len;
14205 tree expanded_types;
14206 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14207 the parameters in this function parameter pack. */
14208 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
14209 args, tf_error, NULL_TREE);
14210 len = TREE_VEC_LENGTH (expanded_types);
14211 for (i = 0; i < len; i++)
14213 tree parm_type;
14214 tree attributes;
14216 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
14217 /* Rename the parameter to include the index. */
14218 DECL_NAME (decl_parm) =
14219 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
14220 parm_type = TREE_VEC_ELT (expanded_types, i);
14221 parm_type = type_decays_to (parm_type);
14222 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
14223 TREE_TYPE (decl_parm) = parm_type;
14224 attributes = DECL_ATTRIBUTES (pattern_parm);
14225 if (DECL_ATTRIBUTES (decl_parm) != attributes)
14227 DECL_ATTRIBUTES (decl_parm) = attributes;
14228 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
14230 decl_parm = TREE_CHAIN (decl_parm);
14233 /* Merge additional specifiers from the CODE_PATTERN. */
14234 if (DECL_DECLARED_INLINE_P (code_pattern)
14235 && !DECL_DECLARED_INLINE_P (decl))
14236 DECL_DECLARED_INLINE_P (decl) = 1;
14237 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
14238 DECL_INLINE (decl) = 1;
14240 else if (TREE_CODE (decl) == VAR_DECL)
14241 DECL_INITIAL (decl) =
14242 tsubst_expr (DECL_INITIAL (code_pattern), args,
14243 tf_error, DECL_TI_TEMPLATE (decl),
14244 /*integral_constant_expression_p=*/false);
14245 else
14246 gcc_unreachable ();
14248 pop_access_scope (decl);
14251 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
14252 substituted to get DECL. */
14254 tree
14255 template_for_substitution (tree decl)
14257 tree tmpl = DECL_TI_TEMPLATE (decl);
14259 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
14260 for the instantiation. This is not always the most general
14261 template. Consider, for example:
14263 template <class T>
14264 struct S { template <class U> void f();
14265 template <> void f<int>(); };
14267 and an instantiation of S<double>::f<int>. We want TD to be the
14268 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
14269 while (/* An instantiation cannot have a definition, so we need a
14270 more general template. */
14271 DECL_TEMPLATE_INSTANTIATION (tmpl)
14272 /* We must also deal with friend templates. Given:
14274 template <class T> struct S {
14275 template <class U> friend void f() {};
14278 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
14279 so far as the language is concerned, but that's still
14280 where we get the pattern for the instantiation from. On
14281 other hand, if the definition comes outside the class, say:
14283 template <class T> struct S {
14284 template <class U> friend void f();
14286 template <class U> friend void f() {}
14288 we don't need to look any further. That's what the check for
14289 DECL_INITIAL is for. */
14290 || (TREE_CODE (decl) == FUNCTION_DECL
14291 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
14292 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
14294 /* The present template, TD, should not be a definition. If it
14295 were a definition, we should be using it! Note that we
14296 cannot restructure the loop to just keep going until we find
14297 a template with a definition, since that might go too far if
14298 a specialization was declared, but not defined. */
14299 gcc_assert (TREE_CODE (decl) != VAR_DECL
14300 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
14302 /* Fetch the more general template. */
14303 tmpl = DECL_TI_TEMPLATE (tmpl);
14306 return tmpl;
14309 /* Produce the definition of D, a _DECL generated from a template. If
14310 DEFER_OK is nonzero, then we don't have to actually do the
14311 instantiation now; we just have to do it sometime. Normally it is
14312 an error if this is an explicit instantiation but D is undefined.
14313 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
14314 explicitly instantiated class template. */
14316 tree
14317 instantiate_decl (tree d, int defer_ok,
14318 bool expl_inst_class_mem_p)
14320 tree tmpl = DECL_TI_TEMPLATE (d);
14321 tree gen_args;
14322 tree args;
14323 tree td;
14324 tree code_pattern;
14325 tree spec;
14326 tree gen_tmpl;
14327 bool pattern_defined;
14328 int need_push;
14329 location_t saved_loc = input_location;
14330 int saved_in_system_header = in_system_header;
14331 bool external_p;
14333 /* This function should only be used to instantiate templates for
14334 functions and static member variables. */
14335 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
14336 || TREE_CODE (d) == VAR_DECL);
14338 /* Variables are never deferred; if instantiation is required, they
14339 are instantiated right away. That allows for better code in the
14340 case that an expression refers to the value of the variable --
14341 if the variable has a constant value the referring expression can
14342 take advantage of that fact. */
14343 if (TREE_CODE (d) == VAR_DECL)
14344 defer_ok = 0;
14346 /* Don't instantiate cloned functions. Instead, instantiate the
14347 functions they cloned. */
14348 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
14349 d = DECL_CLONED_FUNCTION (d);
14351 if (DECL_TEMPLATE_INSTANTIATED (d))
14352 /* D has already been instantiated. It might seem reasonable to
14353 check whether or not D is an explicit instantiation, and, if so,
14354 stop here. But when an explicit instantiation is deferred
14355 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
14356 is set, even though we still need to do the instantiation. */
14357 return d;
14359 /* If we already have a specialization of this declaration, then
14360 there's no reason to instantiate it. Note that
14361 retrieve_specialization gives us both instantiations and
14362 specializations, so we must explicitly check
14363 DECL_TEMPLATE_SPECIALIZATION. */
14364 gen_tmpl = most_general_template (tmpl);
14365 gen_args = DECL_TI_ARGS (d);
14366 spec = retrieve_specialization (gen_tmpl, gen_args,
14367 /*class_specializations_p=*/false);
14368 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
14369 return spec;
14371 /* This needs to happen before any tsubsting. */
14372 if (! push_tinst_level (d))
14373 return d;
14375 timevar_push (TV_PARSE);
14377 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
14378 for the instantiation. */
14379 td = template_for_substitution (d);
14380 code_pattern = DECL_TEMPLATE_RESULT (td);
14382 /* We should never be trying to instantiate a member of a class
14383 template or partial specialization. */
14384 gcc_assert (d != code_pattern);
14386 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
14387 || DECL_TEMPLATE_SPECIALIZATION (td))
14388 /* In the case of a friend template whose definition is provided
14389 outside the class, we may have too many arguments. Drop the
14390 ones we don't need. The same is true for specializations. */
14391 args = get_innermost_template_args
14392 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
14393 else
14394 args = gen_args;
14396 if (TREE_CODE (d) == FUNCTION_DECL)
14397 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
14398 else
14399 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
14401 /* We may be in the middle of deferred access check. Disable it now. */
14402 push_deferring_access_checks (dk_no_deferred);
14404 /* Unless an explicit instantiation directive has already determined
14405 the linkage of D, remember that a definition is available for
14406 this entity. */
14407 if (pattern_defined
14408 && !DECL_INTERFACE_KNOWN (d)
14409 && !DECL_NOT_REALLY_EXTERN (d))
14410 mark_definable (d);
14412 input_location = DECL_SOURCE_LOCATION (d);
14413 in_system_header = DECL_IN_SYSTEM_HEADER (d);
14415 /* If D is a member of an explicitly instantiated class template,
14416 and no definition is available, treat it like an implicit
14417 instantiation. */
14418 if (!pattern_defined && expl_inst_class_mem_p
14419 && DECL_EXPLICIT_INSTANTIATION (d))
14421 DECL_NOT_REALLY_EXTERN (d) = 0;
14422 DECL_INTERFACE_KNOWN (d) = 0;
14423 SET_DECL_IMPLICIT_INSTANTIATION (d);
14426 if (!defer_ok)
14428 /* Recheck the substitutions to obtain any warning messages
14429 about ignoring cv qualifiers. */
14430 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
14431 tree type = TREE_TYPE (gen);
14433 /* Make sure that we can see identifiers, and compute access
14434 correctly. D is already the target FUNCTION_DECL with the
14435 right context. */
14436 push_access_scope (d);
14438 if (TREE_CODE (gen) == FUNCTION_DECL)
14440 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
14441 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
14443 /* Don't simply tsubst the function type, as that will give
14444 duplicate warnings about poor parameter qualifications.
14445 The function arguments are the same as the decl_arguments
14446 without the top level cv qualifiers. */
14447 type = TREE_TYPE (type);
14449 tsubst (type, gen_args, tf_warning_or_error, d);
14451 pop_access_scope (d);
14454 /* Check to see whether we know that this template will be
14455 instantiated in some other file, as with "extern template"
14456 extension. */
14457 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
14458 /* In general, we do not instantiate such templates... */
14459 if (external_p
14460 /* ... but we instantiate inline functions so that we can inline
14461 them and ... */
14462 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
14463 /* ... we instantiate static data members whose values are
14464 needed in integral constant expressions. */
14465 && ! (TREE_CODE (d) == VAR_DECL
14466 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
14467 goto out;
14468 /* Defer all other templates, unless we have been explicitly
14469 forbidden from doing so. */
14470 if (/* If there is no definition, we cannot instantiate the
14471 template. */
14472 ! pattern_defined
14473 /* If it's OK to postpone instantiation, do so. */
14474 || defer_ok
14475 /* If this is a static data member that will be defined
14476 elsewhere, we don't want to instantiate the entire data
14477 member, but we do want to instantiate the initializer so that
14478 we can substitute that elsewhere. */
14479 || (external_p && TREE_CODE (d) == VAR_DECL))
14481 /* The definition of the static data member is now required so
14482 we must substitute the initializer. */
14483 if (TREE_CODE (d) == VAR_DECL
14484 && !DECL_INITIAL (d)
14485 && DECL_INITIAL (code_pattern))
14487 tree ns;
14488 tree init;
14490 ns = decl_namespace_context (d);
14491 push_nested_namespace (ns);
14492 push_nested_class (DECL_CONTEXT (d));
14493 init = tsubst_expr (DECL_INITIAL (code_pattern),
14494 args,
14495 tf_warning_or_error, NULL_TREE,
14496 /*integral_constant_expression_p=*/false);
14497 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
14498 /*asmspec_tree=*/NULL_TREE,
14499 LOOKUP_ONLYCONVERTING);
14500 pop_nested_class ();
14501 pop_nested_namespace (ns);
14504 /* We restore the source position here because it's used by
14505 add_pending_template. */
14506 input_location = saved_loc;
14508 if (at_eof && !pattern_defined
14509 && DECL_EXPLICIT_INSTANTIATION (d))
14510 /* [temp.explicit]
14512 The definition of a non-exported function template, a
14513 non-exported member function template, or a non-exported
14514 member function or static data member of a class template
14515 shall be present in every translation unit in which it is
14516 explicitly instantiated. */
14517 pedwarn
14518 ("explicit instantiation of %qD but no definition available", d);
14520 /* ??? Historically, we have instantiated inline functions, even
14521 when marked as "extern template". */
14522 if (!(external_p && TREE_CODE (d) == VAR_DECL))
14523 add_pending_template (d);
14524 goto out;
14526 /* Tell the repository that D is available in this translation unit
14527 -- and see if it is supposed to be instantiated here. */
14528 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
14530 /* In a PCH file, despite the fact that the repository hasn't
14531 requested instantiation in the PCH it is still possible that
14532 an instantiation will be required in a file that includes the
14533 PCH. */
14534 if (pch_file)
14535 add_pending_template (d);
14536 /* Instantiate inline functions so that the inliner can do its
14537 job, even though we'll not be emitting a copy of this
14538 function. */
14539 if (!(TREE_CODE (d) == FUNCTION_DECL
14540 && flag_inline_trees
14541 && DECL_DECLARED_INLINE_P (d)))
14542 goto out;
14545 need_push = !cfun || !global_bindings_p ();
14546 if (need_push)
14547 push_to_top_level ();
14549 /* Mark D as instantiated so that recursive calls to
14550 instantiate_decl do not try to instantiate it again. */
14551 DECL_TEMPLATE_INSTANTIATED (d) = 1;
14553 /* Regenerate the declaration in case the template has been modified
14554 by a subsequent redeclaration. */
14555 regenerate_decl_from_template (d, td);
14557 /* We already set the file and line above. Reset them now in case
14558 they changed as a result of calling regenerate_decl_from_template. */
14559 input_location = DECL_SOURCE_LOCATION (d);
14561 if (TREE_CODE (d) == VAR_DECL)
14563 tree init;
14565 /* Clear out DECL_RTL; whatever was there before may not be right
14566 since we've reset the type of the declaration. */
14567 SET_DECL_RTL (d, NULL_RTX);
14568 DECL_IN_AGGR_P (d) = 0;
14570 /* The initializer is placed in DECL_INITIAL by
14571 regenerate_decl_from_template. Pull it out so that
14572 finish_decl can process it. */
14573 init = DECL_INITIAL (d);
14574 DECL_INITIAL (d) = NULL_TREE;
14575 DECL_INITIALIZED_P (d) = 0;
14577 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14578 initializer. That function will defer actual emission until
14579 we have a chance to determine linkage. */
14580 DECL_EXTERNAL (d) = 0;
14582 /* Enter the scope of D so that access-checking works correctly. */
14583 push_nested_class (DECL_CONTEXT (d));
14584 finish_decl (d, init, NULL_TREE);
14585 pop_nested_class ();
14587 else if (TREE_CODE (d) == FUNCTION_DECL)
14589 htab_t saved_local_specializations;
14590 tree subst_decl;
14591 tree tmpl_parm;
14592 tree spec_parm;
14594 /* Save away the current list, in case we are instantiating one
14595 template from within the body of another. */
14596 saved_local_specializations = local_specializations;
14598 /* Set up the list of local specializations. */
14599 local_specializations = htab_create (37,
14600 hash_local_specialization,
14601 eq_local_specializations,
14602 NULL);
14604 /* Set up context. */
14605 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
14607 /* Create substitution entries for the parameters. */
14608 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
14609 tmpl_parm = DECL_ARGUMENTS (subst_decl);
14610 spec_parm = DECL_ARGUMENTS (d);
14611 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
14613 register_local_specialization (spec_parm, tmpl_parm);
14614 spec_parm = skip_artificial_parms_for (d, spec_parm);
14615 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
14617 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14619 register_local_specialization (spec_parm, tmpl_parm);
14620 tmpl_parm = TREE_CHAIN (tmpl_parm);
14621 spec_parm = TREE_CHAIN (spec_parm);
14623 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14625 /* Collect all of the extra "packed" parameters into an
14626 argument pack. */
14627 tree parmvec;
14628 tree parmtypevec;
14629 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
14630 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
14631 int i, len = 0;
14632 tree t;
14634 /* Count how many parameters remain. */
14635 for (t = spec_parm; t; t = TREE_CHAIN (t))
14636 len++;
14638 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
14639 parmvec = make_tree_vec (len);
14640 parmtypevec = make_tree_vec (len);
14641 for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
14643 TREE_VEC_ELT (parmvec, i) = spec_parm;
14644 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
14647 /* Build the argument packs. */
14648 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
14649 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
14650 TREE_TYPE (argpack) = argtypepack;
14652 /* Register the (value) argument pack as a specialization of
14653 TMPL_PARM, then move on. */
14654 register_local_specialization (argpack, tmpl_parm);
14655 tmpl_parm = TREE_CHAIN (tmpl_parm);
14657 gcc_assert (!spec_parm);
14659 /* Substitute into the body of the function. */
14660 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
14661 tf_warning_or_error, tmpl,
14662 /*integral_constant_expression_p=*/false);
14664 /* Set the current input_location to the end of the function
14665 so that finish_function knows where we are. */
14666 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
14668 /* We don't need the local specializations any more. */
14669 htab_delete (local_specializations);
14670 local_specializations = saved_local_specializations;
14672 /* Finish the function. */
14673 d = finish_function (0);
14674 expand_or_defer_fn (d);
14677 /* We're not deferring instantiation any more. */
14678 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
14680 if (need_push)
14681 pop_from_top_level ();
14683 out:
14684 input_location = saved_loc;
14685 in_system_header = saved_in_system_header;
14686 pop_deferring_access_checks ();
14687 pop_tinst_level ();
14689 timevar_pop (TV_PARSE);
14691 return d;
14694 /* Run through the list of templates that we wish we could
14695 instantiate, and instantiate any we can. RETRIES is the
14696 number of times we retry pending template instantiation. */
14698 void
14699 instantiate_pending_templates (int retries)
14701 int reconsider;
14702 location_t saved_loc = input_location;
14703 int saved_in_system_header = in_system_header;
14705 /* Instantiating templates may trigger vtable generation. This in turn
14706 may require further template instantiations. We place a limit here
14707 to avoid infinite loop. */
14708 if (pending_templates && retries >= max_tinst_depth)
14710 tree decl = pending_templates->tinst->decl;
14712 error ("template instantiation depth exceeds maximum of %d"
14713 " instantiating %q+D, possibly from virtual table generation"
14714 " (use -ftemplate-depth-NN to increase the maximum)",
14715 max_tinst_depth, decl);
14716 if (TREE_CODE (decl) == FUNCTION_DECL)
14717 /* Pretend that we defined it. */
14718 DECL_INITIAL (decl) = error_mark_node;
14719 return;
14724 struct pending_template **t = &pending_templates;
14725 struct pending_template *last = NULL;
14726 reconsider = 0;
14727 while (*t)
14729 tree instantiation = reopen_tinst_level ((*t)->tinst);
14730 bool complete = false;
14732 if (TYPE_P (instantiation))
14734 tree fn;
14736 if (!COMPLETE_TYPE_P (instantiation))
14738 instantiate_class_template (instantiation);
14739 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
14740 for (fn = TYPE_METHODS (instantiation);
14742 fn = TREE_CHAIN (fn))
14743 if (! DECL_ARTIFICIAL (fn))
14744 instantiate_decl (fn,
14745 /*defer_ok=*/0,
14746 /*expl_inst_class_mem_p=*/false);
14747 if (COMPLETE_TYPE_P (instantiation))
14748 reconsider = 1;
14751 complete = COMPLETE_TYPE_P (instantiation);
14753 else
14755 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
14756 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
14758 instantiation
14759 = instantiate_decl (instantiation,
14760 /*defer_ok=*/0,
14761 /*expl_inst_class_mem_p=*/false);
14762 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
14763 reconsider = 1;
14766 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
14767 || DECL_TEMPLATE_INSTANTIATED (instantiation));
14770 if (complete)
14771 /* If INSTANTIATION has been instantiated, then we don't
14772 need to consider it again in the future. */
14773 *t = (*t)->next;
14774 else
14776 last = *t;
14777 t = &(*t)->next;
14779 tinst_depth = 0;
14780 current_tinst_level = NULL;
14782 last_pending_template = last;
14784 while (reconsider);
14786 input_location = saved_loc;
14787 in_system_header = saved_in_system_header;
14790 /* Substitute ARGVEC into T, which is a list of initializers for
14791 either base class or a non-static data member. The TREE_PURPOSEs
14792 are DECLs, and the TREE_VALUEs are the initializer values. Used by
14793 instantiate_decl. */
14795 static tree
14796 tsubst_initializer_list (tree t, tree argvec)
14798 tree inits = NULL_TREE;
14800 for (; t; t = TREE_CHAIN (t))
14802 tree decl;
14803 tree init;
14804 tree expanded_bases = NULL_TREE;
14805 tree expanded_arguments = NULL_TREE;
14806 int i, len = 1;
14808 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
14810 tree expr;
14811 tree arg;
14813 /* Expand the base class expansion type into separate base
14814 classes. */
14815 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
14816 tf_warning_or_error,
14817 NULL_TREE);
14818 if (expanded_bases == error_mark_node)
14819 continue;
14821 /* We'll be building separate TREE_LISTs of arguments for
14822 each base. */
14823 len = TREE_VEC_LENGTH (expanded_bases);
14824 expanded_arguments = make_tree_vec (len);
14825 for (i = 0; i < len; i++)
14826 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
14828 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
14829 expand each argument in the TREE_VALUE of t. */
14830 expr = make_node (EXPR_PACK_EXPANSION);
14831 PACK_EXPANSION_PARAMETER_PACKS (expr) =
14832 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
14834 /* Substitute parameter packs into each argument in the
14835 TREE_LIST. */
14836 in_base_initializer = 1;
14837 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
14839 tree expanded_exprs;
14841 /* Expand the argument. */
14842 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
14843 expanded_exprs = tsubst_pack_expansion (expr, argvec,
14844 tf_warning_or_error,
14845 NULL_TREE);
14847 /* Prepend each of the expanded expressions to the
14848 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
14849 for (i = 0; i < len; i++)
14851 TREE_VEC_ELT (expanded_arguments, i) =
14852 tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i),
14853 TREE_VEC_ELT (expanded_arguments, i));
14856 in_base_initializer = 0;
14858 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
14859 since we built them backwards. */
14860 for (i = 0; i < len; i++)
14862 TREE_VEC_ELT (expanded_arguments, i) =
14863 nreverse (TREE_VEC_ELT (expanded_arguments, i));
14867 for (i = 0; i < len; ++i)
14869 if (expanded_bases)
14871 decl = TREE_VEC_ELT (expanded_bases, i);
14872 decl = expand_member_init (decl);
14873 init = TREE_VEC_ELT (expanded_arguments, i);
14875 else
14877 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
14878 tf_warning_or_error, NULL_TREE);
14880 decl = expand_member_init (decl);
14881 if (decl && !DECL_P (decl))
14882 in_base_initializer = 1;
14884 init = tsubst_expr (TREE_VALUE (t), argvec,
14885 tf_warning_or_error, NULL_TREE,
14886 /*integral_constant_expression_p=*/false);
14887 in_base_initializer = 0;
14890 if (decl)
14892 init = build_tree_list (decl, init);
14893 TREE_CHAIN (init) = inits;
14894 inits = init;
14898 return inits;
14901 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
14903 static void
14904 set_current_access_from_decl (tree decl)
14906 if (TREE_PRIVATE (decl))
14907 current_access_specifier = access_private_node;
14908 else if (TREE_PROTECTED (decl))
14909 current_access_specifier = access_protected_node;
14910 else
14911 current_access_specifier = access_public_node;
14914 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
14915 is the instantiation (which should have been created with
14916 start_enum) and ARGS are the template arguments to use. */
14918 static void
14919 tsubst_enum (tree tag, tree newtag, tree args)
14921 tree e;
14923 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
14925 tree value;
14926 tree decl;
14928 decl = TREE_VALUE (e);
14929 /* Note that in a template enum, the TREE_VALUE is the
14930 CONST_DECL, not the corresponding INTEGER_CST. */
14931 value = tsubst_expr (DECL_INITIAL (decl),
14932 args, tf_warning_or_error, NULL_TREE,
14933 /*integral_constant_expression_p=*/true);
14935 /* Give this enumeration constant the correct access. */
14936 set_current_access_from_decl (decl);
14938 /* Actually build the enumerator itself. */
14939 build_enumerator (DECL_NAME (decl), value, newtag);
14942 finish_enum (newtag);
14943 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
14944 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
14947 /* DECL is a FUNCTION_DECL that is a template specialization. Return
14948 its type -- but without substituting the innermost set of template
14949 arguments. So, innermost set of template parameters will appear in
14950 the type. */
14952 tree
14953 get_mostly_instantiated_function_type (tree decl)
14955 tree fn_type;
14956 tree tmpl;
14957 tree targs;
14958 tree tparms;
14959 int parm_depth;
14961 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14962 targs = DECL_TI_ARGS (decl);
14963 tparms = DECL_TEMPLATE_PARMS (tmpl);
14964 parm_depth = TMPL_PARMS_DEPTH (tparms);
14966 /* There should be as many levels of arguments as there are levels
14967 of parameters. */
14968 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
14970 fn_type = TREE_TYPE (tmpl);
14972 if (parm_depth == 1)
14973 /* No substitution is necessary. */
14975 else
14977 int i, save_access_control;
14978 tree partial_args;
14980 /* Replace the innermost level of the TARGS with NULL_TREEs to
14981 let tsubst know not to substitute for those parameters. */
14982 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
14983 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
14984 SET_TMPL_ARGS_LEVEL (partial_args, i,
14985 TMPL_ARGS_LEVEL (targs, i));
14986 SET_TMPL_ARGS_LEVEL (partial_args,
14987 TMPL_ARGS_DEPTH (targs),
14988 make_tree_vec (DECL_NTPARMS (tmpl)));
14990 /* Disable access control as this function is used only during
14991 name-mangling. */
14992 save_access_control = flag_access_control;
14993 flag_access_control = 0;
14995 ++processing_template_decl;
14996 /* Now, do the (partial) substitution to figure out the
14997 appropriate function type. */
14998 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
14999 --processing_template_decl;
15001 /* Substitute into the template parameters to obtain the real
15002 innermost set of parameters. This step is important if the
15003 innermost set of template parameters contains value
15004 parameters whose types depend on outer template parameters. */
15005 TREE_VEC_LENGTH (partial_args)--;
15006 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
15008 flag_access_control = save_access_control;
15011 return fn_type;
15014 /* Return truthvalue if we're processing a template different from
15015 the last one involved in diagnostics. */
15017 problematic_instantiation_changed (void)
15019 return last_template_error_tick != tinst_level_tick;
15022 /* Remember current template involved in diagnostics. */
15023 void
15024 record_last_problematic_instantiation (void)
15026 last_template_error_tick = tinst_level_tick;
15029 struct tinst_level *
15030 current_instantiation (void)
15032 return current_tinst_level;
15035 /* [temp.param] Check that template non-type parm TYPE is of an allowable
15036 type. Return zero for ok, nonzero for disallowed. Issue error and
15037 warning messages under control of COMPLAIN. */
15039 static int
15040 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
15042 if (INTEGRAL_TYPE_P (type))
15043 return 0;
15044 else if (POINTER_TYPE_P (type))
15045 return 0;
15046 else if (TYPE_PTR_TO_MEMBER_P (type))
15047 return 0;
15048 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
15049 return 0;
15050 else if (TREE_CODE (type) == TYPENAME_TYPE)
15051 return 0;
15053 if (complain & tf_error)
15054 error ("%q#T is not a valid type for a template constant parameter", type);
15055 return 1;
15058 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
15059 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
15061 static bool
15062 dependent_type_p_r (tree type)
15064 tree scope;
15066 /* [temp.dep.type]
15068 A type is dependent if it is:
15070 -- a template parameter. Template template parameters are types
15071 for us (since TYPE_P holds true for them) so we handle
15072 them here. */
15073 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15074 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
15075 return true;
15076 /* -- a qualified-id with a nested-name-specifier which contains a
15077 class-name that names a dependent type or whose unqualified-id
15078 names a dependent type. */
15079 if (TREE_CODE (type) == TYPENAME_TYPE)
15080 return true;
15081 /* -- a cv-qualified type where the cv-unqualified type is
15082 dependent. */
15083 type = TYPE_MAIN_VARIANT (type);
15084 /* -- a compound type constructed from any dependent type. */
15085 if (TYPE_PTR_TO_MEMBER_P (type))
15086 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
15087 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
15088 (type)));
15089 else if (TREE_CODE (type) == POINTER_TYPE
15090 || TREE_CODE (type) == REFERENCE_TYPE)
15091 return dependent_type_p (TREE_TYPE (type));
15092 else if (TREE_CODE (type) == FUNCTION_TYPE
15093 || TREE_CODE (type) == METHOD_TYPE)
15095 tree arg_type;
15097 if (dependent_type_p (TREE_TYPE (type)))
15098 return true;
15099 for (arg_type = TYPE_ARG_TYPES (type);
15100 arg_type;
15101 arg_type = TREE_CHAIN (arg_type))
15102 if (dependent_type_p (TREE_VALUE (arg_type)))
15103 return true;
15104 return false;
15106 /* -- an array type constructed from any dependent type or whose
15107 size is specified by a constant expression that is
15108 value-dependent. */
15109 if (TREE_CODE (type) == ARRAY_TYPE)
15111 if (TYPE_DOMAIN (type)
15112 && ((value_dependent_expression_p
15113 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15114 || (type_dependent_expression_p
15115 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
15116 return true;
15117 return dependent_type_p (TREE_TYPE (type));
15120 /* -- a template-id in which either the template name is a template
15121 parameter ... */
15122 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15123 return true;
15124 /* ... or any of the template arguments is a dependent type or
15125 an expression that is type-dependent or value-dependent. */
15126 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
15127 && (any_dependent_template_arguments_p
15128 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
15129 return true;
15131 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
15132 argument of the `typeof' expression is not type-dependent, then
15133 it should already been have resolved. */
15134 if (TREE_CODE (type) == TYPEOF_TYPE
15135 || TREE_CODE (type) == DECLTYPE_TYPE)
15136 return true;
15138 /* A template argument pack is dependent if any of its packed
15139 arguments are. */
15140 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
15142 tree args = ARGUMENT_PACK_ARGS (type);
15143 int i, len = TREE_VEC_LENGTH (args);
15144 for (i = 0; i < len; ++i)
15145 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15146 return true;
15149 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
15150 be template parameters. */
15151 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
15152 return true;
15154 /* The standard does not specifically mention types that are local
15155 to template functions or local classes, but they should be
15156 considered dependent too. For example:
15158 template <int I> void f() {
15159 enum E { a = I };
15160 S<sizeof (E)> s;
15163 The size of `E' cannot be known until the value of `I' has been
15164 determined. Therefore, `E' must be considered dependent. */
15165 scope = TYPE_CONTEXT (type);
15166 if (scope && TYPE_P (scope))
15167 return dependent_type_p (scope);
15168 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15169 return type_dependent_expression_p (scope);
15171 /* Other types are non-dependent. */
15172 return false;
15175 /* Returns TRUE if TYPE is dependent, in the sense of
15176 [temp.dep.type]. */
15178 bool
15179 dependent_type_p (tree type)
15181 /* If there are no template parameters in scope, then there can't be
15182 any dependent types. */
15183 if (!processing_template_decl)
15185 /* If we are not processing a template, then nobody should be
15186 providing us with a dependent type. */
15187 gcc_assert (type);
15188 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
15189 return false;
15192 /* If the type is NULL, we have not computed a type for the entity
15193 in question; in that case, the type is dependent. */
15194 if (!type)
15195 return true;
15197 /* Erroneous types can be considered non-dependent. */
15198 if (type == error_mark_node)
15199 return false;
15201 /* If we have not already computed the appropriate value for TYPE,
15202 do so now. */
15203 if (!TYPE_DEPENDENT_P_VALID (type))
15205 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
15206 TYPE_DEPENDENT_P_VALID (type) = 1;
15209 return TYPE_DEPENDENT_P (type);
15212 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
15214 static bool
15215 dependent_scope_ref_p (tree expression, bool criterion (tree))
15217 tree scope;
15218 tree name;
15220 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
15222 if (!TYPE_P (TREE_OPERAND (expression, 0)))
15223 return true;
15225 scope = TREE_OPERAND (expression, 0);
15226 name = TREE_OPERAND (expression, 1);
15228 /* [temp.dep.expr]
15230 An id-expression is type-dependent if it contains a
15231 nested-name-specifier that contains a class-name that names a
15232 dependent type. */
15233 /* The suggested resolution to Core Issue 2 implies that if the
15234 qualifying type is the current class, then we must peek
15235 inside it. */
15236 if (DECL_P (name)
15237 && currently_open_class (scope)
15238 && !criterion (name))
15239 return false;
15240 if (dependent_type_p (scope))
15241 return true;
15243 return false;
15246 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
15247 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
15248 expression. */
15250 bool
15251 value_dependent_expression_p (tree expression)
15253 if (!processing_template_decl)
15254 return false;
15256 /* A name declared with a dependent type. */
15257 if (DECL_P (expression) && type_dependent_expression_p (expression))
15258 return true;
15260 switch (TREE_CODE (expression))
15262 case IDENTIFIER_NODE:
15263 /* A name that has not been looked up -- must be dependent. */
15264 return true;
15266 case TEMPLATE_PARM_INDEX:
15267 /* A non-type template parm. */
15268 return true;
15270 case CONST_DECL:
15271 /* A non-type template parm. */
15272 if (DECL_TEMPLATE_PARM_P (expression))
15273 return true;
15274 return false;
15276 case VAR_DECL:
15277 /* A constant with integral or enumeration type and is initialized
15278 with an expression that is value-dependent. */
15279 if (DECL_INITIAL (expression)
15280 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
15281 && value_dependent_expression_p (DECL_INITIAL (expression)))
15282 return true;
15283 return false;
15285 case DYNAMIC_CAST_EXPR:
15286 case STATIC_CAST_EXPR:
15287 case CONST_CAST_EXPR:
15288 case REINTERPRET_CAST_EXPR:
15289 case CAST_EXPR:
15290 /* These expressions are value-dependent if the type to which
15291 the cast occurs is dependent or the expression being casted
15292 is value-dependent. */
15294 tree type = TREE_TYPE (expression);
15296 if (dependent_type_p (type))
15297 return true;
15299 /* A functional cast has a list of operands. */
15300 expression = TREE_OPERAND (expression, 0);
15301 if (!expression)
15303 /* If there are no operands, it must be an expression such
15304 as "int()". This should not happen for aggregate types
15305 because it would form non-constant expressions. */
15306 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
15308 return false;
15311 if (TREE_CODE (expression) == TREE_LIST)
15312 return any_value_dependent_elements_p (expression);
15314 return value_dependent_expression_p (expression);
15317 case SIZEOF_EXPR:
15318 case ALIGNOF_EXPR:
15319 /* A `sizeof' expression is value-dependent if the operand is
15320 type-dependent or is a pack expansion. */
15321 expression = TREE_OPERAND (expression, 0);
15322 if (PACK_EXPANSION_P (expression))
15323 return true;
15324 else if (TYPE_P (expression))
15325 return dependent_type_p (expression);
15326 return type_dependent_expression_p (expression);
15328 case SCOPE_REF:
15329 return dependent_scope_ref_p (expression, value_dependent_expression_p);
15331 case COMPONENT_REF:
15332 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
15333 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
15335 case CALL_EXPR:
15336 /* A CALL_EXPR may appear in a constant expression if it is a
15337 call to a builtin function, e.g., __builtin_constant_p. All
15338 such calls are value-dependent. */
15339 return true;
15341 case NONTYPE_ARGUMENT_PACK:
15342 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
15343 is value-dependent. */
15345 tree values = ARGUMENT_PACK_ARGS (expression);
15346 int i, len = TREE_VEC_LENGTH (values);
15348 for (i = 0; i < len; ++i)
15349 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
15350 return true;
15352 return false;
15355 case TRAIT_EXPR:
15357 tree type2 = TRAIT_EXPR_TYPE2 (expression);
15358 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
15359 || (type2 ? dependent_type_p (type2) : false));
15362 case MODOP_EXPR:
15363 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
15364 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
15366 default:
15367 /* A constant expression is value-dependent if any subexpression is
15368 value-dependent. */
15369 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
15371 case tcc_reference:
15372 case tcc_unary:
15373 return (value_dependent_expression_p
15374 (TREE_OPERAND (expression, 0)));
15376 case tcc_comparison:
15377 case tcc_binary:
15378 return ((value_dependent_expression_p
15379 (TREE_OPERAND (expression, 0)))
15380 || (value_dependent_expression_p
15381 (TREE_OPERAND (expression, 1))));
15383 case tcc_expression:
15384 case tcc_vl_exp:
15386 int i;
15387 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
15388 /* In some cases, some of the operands may be missing.
15389 (For example, in the case of PREDECREMENT_EXPR, the
15390 amount to increment by may be missing.) That doesn't
15391 make the expression dependent. */
15392 if (TREE_OPERAND (expression, i)
15393 && (value_dependent_expression_p
15394 (TREE_OPERAND (expression, i))))
15395 return true;
15396 return false;
15399 default:
15400 break;
15404 /* The expression is not value-dependent. */
15405 return false;
15408 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
15409 [temp.dep.expr]. */
15411 bool
15412 type_dependent_expression_p (tree expression)
15414 if (!processing_template_decl)
15415 return false;
15417 if (expression == error_mark_node)
15418 return false;
15420 /* An unresolved name is always dependent. */
15421 if (TREE_CODE (expression) == IDENTIFIER_NODE
15422 || TREE_CODE (expression) == USING_DECL)
15423 return true;
15425 /* Some expression forms are never type-dependent. */
15426 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
15427 || TREE_CODE (expression) == SIZEOF_EXPR
15428 || TREE_CODE (expression) == ALIGNOF_EXPR
15429 || TREE_CODE (expression) == TRAIT_EXPR
15430 || TREE_CODE (expression) == TYPEID_EXPR
15431 || TREE_CODE (expression) == DELETE_EXPR
15432 || TREE_CODE (expression) == VEC_DELETE_EXPR
15433 || TREE_CODE (expression) == THROW_EXPR)
15434 return false;
15436 /* The types of these expressions depends only on the type to which
15437 the cast occurs. */
15438 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
15439 || TREE_CODE (expression) == STATIC_CAST_EXPR
15440 || TREE_CODE (expression) == CONST_CAST_EXPR
15441 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
15442 || TREE_CODE (expression) == CAST_EXPR)
15443 return dependent_type_p (TREE_TYPE (expression));
15445 /* The types of these expressions depends only on the type created
15446 by the expression. */
15447 if (TREE_CODE (expression) == NEW_EXPR
15448 || TREE_CODE (expression) == VEC_NEW_EXPR)
15450 /* For NEW_EXPR tree nodes created inside a template, either
15451 the object type itself or a TREE_LIST may appear as the
15452 operand 1. */
15453 tree type = TREE_OPERAND (expression, 1);
15454 if (TREE_CODE (type) == TREE_LIST)
15455 /* This is an array type. We need to check array dimensions
15456 as well. */
15457 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
15458 || value_dependent_expression_p
15459 (TREE_OPERAND (TREE_VALUE (type), 1));
15460 else
15461 return dependent_type_p (type);
15464 if (TREE_CODE (expression) == SCOPE_REF
15465 && dependent_scope_ref_p (expression,
15466 type_dependent_expression_p))
15467 return true;
15469 if (TREE_CODE (expression) == FUNCTION_DECL
15470 && DECL_LANG_SPECIFIC (expression)
15471 && DECL_TEMPLATE_INFO (expression)
15472 && (any_dependent_template_arguments_p
15473 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
15474 return true;
15476 if (TREE_CODE (expression) == TEMPLATE_DECL
15477 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
15478 return false;
15480 if (TREE_CODE (expression) == STMT_EXPR)
15481 expression = stmt_expr_value_expr (expression);
15483 if (TREE_TYPE (expression) == unknown_type_node)
15485 if (TREE_CODE (expression) == ADDR_EXPR)
15486 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
15487 if (TREE_CODE (expression) == COMPONENT_REF
15488 || TREE_CODE (expression) == OFFSET_REF)
15490 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
15491 return true;
15492 expression = TREE_OPERAND (expression, 1);
15493 if (TREE_CODE (expression) == IDENTIFIER_NODE)
15494 return false;
15496 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15497 if (TREE_CODE (expression) == SCOPE_REF)
15498 return false;
15500 if (TREE_CODE (expression) == BASELINK)
15501 expression = BASELINK_FUNCTIONS (expression);
15503 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
15505 if (any_dependent_template_arguments_p
15506 (TREE_OPERAND (expression, 1)))
15507 return true;
15508 expression = TREE_OPERAND (expression, 0);
15510 gcc_assert (TREE_CODE (expression) == OVERLOAD
15511 || TREE_CODE (expression) == FUNCTION_DECL);
15513 while (expression)
15515 if (type_dependent_expression_p (OVL_CURRENT (expression)))
15516 return true;
15517 expression = OVL_NEXT (expression);
15519 return false;
15522 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
15524 return (dependent_type_p (TREE_TYPE (expression)));
15527 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15528 contains a type-dependent expression. */
15530 bool
15531 any_type_dependent_arguments_p (const_tree args)
15533 while (args)
15535 tree arg = TREE_VALUE (args);
15537 if (type_dependent_expression_p (arg))
15538 return true;
15539 args = TREE_CHAIN (args);
15541 return false;
15544 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
15545 expressions) contains any value-dependent expressions. */
15547 bool
15548 any_value_dependent_elements_p (const_tree list)
15550 for (; list; list = TREE_CHAIN (list))
15551 if (value_dependent_expression_p (TREE_VALUE (list)))
15552 return true;
15554 return false;
15557 /* Returns TRUE if the ARG (a template argument) is dependent. */
15559 bool
15560 dependent_template_arg_p (tree arg)
15562 if (!processing_template_decl)
15563 return false;
15565 if (TREE_CODE (arg) == TEMPLATE_DECL
15566 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15567 return dependent_template_p (arg);
15568 else if (ARGUMENT_PACK_P (arg))
15570 tree args = ARGUMENT_PACK_ARGS (arg);
15571 int i, len = TREE_VEC_LENGTH (args);
15572 for (i = 0; i < len; ++i)
15574 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15575 return true;
15578 return false;
15580 else if (TYPE_P (arg))
15581 return dependent_type_p (arg);
15582 else
15583 return (type_dependent_expression_p (arg)
15584 || value_dependent_expression_p (arg));
15587 /* Returns true if ARGS (a collection of template arguments) contains
15588 any types that require structural equality testing. */
15590 bool
15591 any_template_arguments_need_structural_equality_p (tree args)
15593 int i;
15594 int j;
15596 if (!args)
15597 return false;
15598 if (args == error_mark_node)
15599 return true;
15601 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15603 tree level = TMPL_ARGS_LEVEL (args, i + 1);
15604 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15606 tree arg = TREE_VEC_ELT (level, j);
15607 tree packed_args = NULL_TREE;
15608 int k, len = 1;
15610 if (ARGUMENT_PACK_P (arg))
15612 /* Look inside the argument pack. */
15613 packed_args = ARGUMENT_PACK_ARGS (arg);
15614 len = TREE_VEC_LENGTH (packed_args);
15617 for (k = 0; k < len; ++k)
15619 if (packed_args)
15620 arg = TREE_VEC_ELT (packed_args, k);
15622 if (error_operand_p (arg))
15623 return true;
15624 else if (TREE_CODE (arg) == TEMPLATE_DECL
15625 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15626 continue;
15627 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
15628 return true;
15629 else if (!TYPE_P (arg) && TREE_TYPE (arg)
15630 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
15631 return true;
15636 return false;
15639 /* Returns true if ARGS (a collection of template arguments) contains
15640 any dependent arguments. */
15642 bool
15643 any_dependent_template_arguments_p (const_tree args)
15645 int i;
15646 int j;
15648 if (!args)
15649 return false;
15650 if (args == error_mark_node)
15651 return true;
15653 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15655 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
15656 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15657 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
15658 return true;
15661 return false;
15664 /* Returns TRUE if the template TMPL is dependent. */
15666 bool
15667 dependent_template_p (tree tmpl)
15669 if (TREE_CODE (tmpl) == OVERLOAD)
15671 while (tmpl)
15673 if (dependent_template_p (OVL_FUNCTION (tmpl)))
15674 return true;
15675 tmpl = OVL_CHAIN (tmpl);
15677 return false;
15680 /* Template template parameters are dependent. */
15681 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
15682 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15683 return true;
15684 /* So are names that have not been looked up. */
15685 if (TREE_CODE (tmpl) == SCOPE_REF
15686 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
15687 return true;
15688 /* So are member templates of dependent classes. */
15689 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
15690 return dependent_type_p (DECL_CONTEXT (tmpl));
15691 return false;
15694 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
15696 bool
15697 dependent_template_id_p (tree tmpl, tree args)
15699 return (dependent_template_p (tmpl)
15700 || any_dependent_template_arguments_p (args));
15703 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
15704 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
15705 no such TYPE can be found. Note that this function peers inside
15706 uninstantiated templates and therefore should be used only in
15707 extremely limited situations. ONLY_CURRENT_P restricts this
15708 peering to the currently open classes hierarchy (which is required
15709 when comparing types). */
15711 tree
15712 resolve_typename_type (tree type, bool only_current_p)
15714 tree scope;
15715 tree name;
15716 tree decl;
15717 int quals;
15718 tree pushed_scope;
15719 tree result;
15721 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
15723 scope = TYPE_CONTEXT (type);
15724 name = TYPE_IDENTIFIER (type);
15726 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
15727 it first before we can figure out what NAME refers to. */
15728 if (TREE_CODE (scope) == TYPENAME_TYPE)
15729 scope = resolve_typename_type (scope, only_current_p);
15730 /* If we don't know what SCOPE refers to, then we cannot resolve the
15731 TYPENAME_TYPE. */
15732 if (TREE_CODE (scope) == TYPENAME_TYPE)
15733 return type;
15734 /* If the SCOPE is a template type parameter, we have no way of
15735 resolving the name. */
15736 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
15737 return type;
15738 /* If the SCOPE is not the current instantiation, there's no reason
15739 to look inside it. */
15740 if (only_current_p && !currently_open_class (scope))
15741 return type;
15742 /* If SCOPE is a partial instantiation, it will not have a valid
15743 TYPE_FIELDS list, so use the original template. */
15744 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
15745 /* Enter the SCOPE so that name lookup will be resolved as if we
15746 were in the class definition. In particular, SCOPE will no
15747 longer be considered a dependent type. */
15748 pushed_scope = push_scope (scope);
15749 /* Look up the declaration. */
15750 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
15752 result = NULL_TREE;
15754 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
15755 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
15756 if (!decl)
15757 /*nop*/;
15758 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
15759 && TREE_CODE (decl) == TYPE_DECL)
15761 result = TREE_TYPE (decl);
15762 if (result == error_mark_node)
15763 result = NULL_TREE;
15765 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
15766 && DECL_CLASS_TEMPLATE_P (decl))
15768 tree tmpl;
15769 tree args;
15770 /* Obtain the template and the arguments. */
15771 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
15772 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
15773 /* Instantiate the template. */
15774 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
15775 /*entering_scope=*/0,
15776 tf_error | tf_user);
15777 if (result == error_mark_node)
15778 result = NULL_TREE;
15781 /* Leave the SCOPE. */
15782 if (pushed_scope)
15783 pop_scope (pushed_scope);
15785 /* If we failed to resolve it, return the original typename. */
15786 if (!result)
15787 return type;
15789 /* If lookup found a typename type, resolve that too. */
15790 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
15792 /* Ill-formed programs can cause infinite recursion here, so we
15793 must catch that. */
15794 TYPENAME_IS_RESOLVING_P (type) = 1;
15795 result = resolve_typename_type (result, only_current_p);
15796 TYPENAME_IS_RESOLVING_P (type) = 0;
15799 /* Qualify the resulting type. */
15800 quals = cp_type_quals (type);
15801 if (quals)
15802 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
15804 return result;
15807 /* EXPR is an expression which is not type-dependent. Return a proxy
15808 for EXPR that can be used to compute the types of larger
15809 expressions containing EXPR. */
15811 tree
15812 build_non_dependent_expr (tree expr)
15814 tree inner_expr;
15816 /* Preserve null pointer constants so that the type of things like
15817 "p == 0" where "p" is a pointer can be determined. */
15818 if (null_ptr_cst_p (expr))
15819 return expr;
15820 /* Preserve OVERLOADs; the functions must be available to resolve
15821 types. */
15822 inner_expr = expr;
15823 if (TREE_CODE (inner_expr) == STMT_EXPR)
15824 inner_expr = stmt_expr_value_expr (inner_expr);
15825 if (TREE_CODE (inner_expr) == ADDR_EXPR)
15826 inner_expr = TREE_OPERAND (inner_expr, 0);
15827 if (TREE_CODE (inner_expr) == COMPONENT_REF)
15828 inner_expr = TREE_OPERAND (inner_expr, 1);
15829 if (is_overloaded_fn (inner_expr)
15830 || TREE_CODE (inner_expr) == OFFSET_REF)
15831 return expr;
15832 /* There is no need to return a proxy for a variable. */
15833 if (TREE_CODE (expr) == VAR_DECL)
15834 return expr;
15835 /* Preserve string constants; conversions from string constants to
15836 "char *" are allowed, even though normally a "const char *"
15837 cannot be used to initialize a "char *". */
15838 if (TREE_CODE (expr) == STRING_CST)
15839 return expr;
15840 /* Preserve arithmetic constants, as an optimization -- there is no
15841 reason to create a new node. */
15842 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
15843 return expr;
15844 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
15845 There is at least one place where we want to know that a
15846 particular expression is a throw-expression: when checking a ?:
15847 expression, there are special rules if the second or third
15848 argument is a throw-expression. */
15849 if (TREE_CODE (expr) == THROW_EXPR)
15850 return expr;
15852 if (TREE_CODE (expr) == COND_EXPR)
15853 return build3 (COND_EXPR,
15854 TREE_TYPE (expr),
15855 TREE_OPERAND (expr, 0),
15856 (TREE_OPERAND (expr, 1)
15857 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
15858 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
15859 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
15860 if (TREE_CODE (expr) == COMPOUND_EXPR
15861 && !COMPOUND_EXPR_OVERLOADED (expr))
15862 return build2 (COMPOUND_EXPR,
15863 TREE_TYPE (expr),
15864 TREE_OPERAND (expr, 0),
15865 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
15867 /* If the type is unknown, it can't really be non-dependent */
15868 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
15870 /* Otherwise, build a NON_DEPENDENT_EXPR.
15872 REFERENCE_TYPEs are not stripped for expressions in templates
15873 because doing so would play havoc with mangling. Consider, for
15874 example:
15876 template <typename T> void f<T& g>() { g(); }
15878 In the body of "f", the expression for "g" will have
15879 REFERENCE_TYPE, even though the standard says that it should
15880 not. The reason is that we must preserve the syntactic form of
15881 the expression so that mangling (say) "f<g>" inside the body of
15882 "f" works out correctly. Therefore, the REFERENCE_TYPE is
15883 stripped here. */
15884 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
15887 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
15888 Return a new TREE_LIST with the various arguments replaced with
15889 equivalent non-dependent expressions. */
15891 tree
15892 build_non_dependent_args (tree args)
15894 tree a;
15895 tree new_args;
15897 new_args = NULL_TREE;
15898 for (a = args; a; a = TREE_CHAIN (a))
15899 new_args = tree_cons (NULL_TREE,
15900 build_non_dependent_expr (TREE_VALUE (a)),
15901 new_args);
15902 return nreverse (new_args);
15905 #include "gt-cp-pt.h"