* toplev.h (floor_log2): If GCC_VERSION >= 3004, declare as static
[official-gcc.git] / gcc / cp / pt.c
blob5645b23e27f6f0bc23e785a6fbafcc585bbf446c
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, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "cp-tree.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "rtl.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "vecprim.h"
50 /* The type of functions taking a tree, and some additional data, and
51 returning an int. */
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY (()) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
82 local variables. */
83 static htab_t local_specializations;
85 /* Contains canonical template parameter types. The vector is indexed by
86 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
87 TREE_LIST, whose TREE_VALUEs contain the canonical template
88 parameters of various types and levels. */
89 static GTY(()) VEC(tree,gc) *canonical_template_parms;
91 #define UNIFY_ALLOW_NONE 0
92 #define UNIFY_ALLOW_MORE_CV_QUAL 1
93 #define UNIFY_ALLOW_LESS_CV_QUAL 2
94 #define UNIFY_ALLOW_DERIVED 4
95 #define UNIFY_ALLOW_INTEGER 8
96 #define UNIFY_ALLOW_OUTER_LEVEL 16
97 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
98 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
100 static void push_access_scope (tree);
101 static void pop_access_scope (tree);
102 static bool resolve_overloaded_unification (tree, tree, tree, tree,
103 unification_kind_t, int);
104 static int try_one_overload (tree, tree, tree, tree, tree,
105 unification_kind_t, int, bool);
106 static int unify (tree, tree, tree, tree, int);
107 static void add_pending_template (tree);
108 static int push_tinst_level (tree);
109 static void pop_tinst_level (void);
110 static tree reopen_tinst_level (struct tinst_level *);
111 static tree tsubst_initializer_list (tree, tree);
112 static tree get_class_bindings (tree, tree, tree);
113 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
114 bool, bool);
115 static void tsubst_enum (tree, tree, tree);
116 static tree add_to_template_args (tree, tree);
117 static tree add_outermost_template_args (tree, tree);
118 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
119 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
120 tree);
121 static int type_unification_real (tree, tree, tree, const tree *,
122 unsigned int, int, unification_kind_t, int);
123 static void note_template_header (int);
124 static tree convert_nontype_argument_function (tree, tree);
125 static tree convert_nontype_argument (tree, tree);
126 static tree convert_template_argument (tree, tree, tree,
127 tsubst_flags_t, int, tree);
128 static int for_each_template_parm (tree, tree_fn_t, void*,
129 struct pointer_set_t*, bool);
130 static tree expand_template_argument_pack (tree);
131 static tree build_template_parm_index (int, int, int, tree, tree);
132 static bool inline_needs_template_parms (tree);
133 static void push_inline_template_parms_recursive (tree, int);
134 static tree retrieve_local_specialization (tree);
135 static void register_local_specialization (tree, tree);
136 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
137 static int mark_template_parm (tree, void *);
138 static int template_parm_this_level_p (tree, void *);
139 static tree tsubst_friend_function (tree, tree);
140 static tree tsubst_friend_class (tree, tree);
141 static int can_complete_type_without_circularity (tree);
142 static tree get_bindings (tree, tree, tree, bool);
143 static int template_decl_level (tree);
144 static int check_cv_quals_for_unify (int, tree, tree);
145 static void template_parm_level_and_index (tree, int*, int*);
146 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
147 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
148 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
149 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
150 static void regenerate_decl_from_template (tree, tree);
151 static tree most_specialized_class (tree, tree);
152 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
153 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
154 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
155 static bool check_specialization_scope (void);
156 static tree process_partial_specialization (tree);
157 static void set_current_access_from_decl (tree);
158 static tree get_template_base (tree, tree, tree, tree);
159 static tree try_class_unification (tree, tree, tree, tree);
160 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
161 tree, tree);
162 static bool template_template_parm_bindings_ok_p (tree, tree);
163 static int template_args_equal (tree, tree);
164 static void tsubst_default_arguments (tree);
165 static tree for_each_template_parm_r (tree *, int *, void *);
166 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
167 static void copy_default_args_to_explicit_spec (tree);
168 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
169 static int eq_local_specializations (const void *, const void *);
170 static bool dependent_template_arg_p (tree);
171 static bool any_template_arguments_need_structural_equality_p (tree);
172 static bool dependent_type_p_r (tree);
173 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
174 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
175 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_decl (tree, tree, tsubst_flags_t);
177 static void perform_typedefs_access_check (tree tmpl, tree targs);
178 static void append_type_to_template_for_access_check_1 (tree, tree, tree);
180 /* Make the current scope suitable for access checking when we are
181 processing T. T can be FUNCTION_DECL for instantiated function
182 template, or VAR_DECL for static member variable (need by
183 instantiate_decl). */
185 static void
186 push_access_scope (tree t)
188 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
189 || TREE_CODE (t) == VAR_DECL);
191 if (DECL_FRIEND_CONTEXT (t))
192 push_nested_class (DECL_FRIEND_CONTEXT (t));
193 else if (DECL_CLASS_SCOPE_P (t))
194 push_nested_class (DECL_CONTEXT (t));
195 else
196 push_to_top_level ();
198 if (TREE_CODE (t) == FUNCTION_DECL)
200 saved_access_scope = tree_cons
201 (NULL_TREE, current_function_decl, saved_access_scope);
202 current_function_decl = t;
206 /* Restore the scope set up by push_access_scope. T is the node we
207 are processing. */
209 static void
210 pop_access_scope (tree t)
212 if (TREE_CODE (t) == FUNCTION_DECL)
214 current_function_decl = TREE_VALUE (saved_access_scope);
215 saved_access_scope = TREE_CHAIN (saved_access_scope);
218 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
219 pop_nested_class ();
220 else
221 pop_from_top_level ();
224 /* Do any processing required when DECL (a member template
225 declaration) is finished. Returns the TEMPLATE_DECL corresponding
226 to DECL, unless it is a specialization, in which case the DECL
227 itself is returned. */
229 tree
230 finish_member_template_decl (tree decl)
232 if (decl == error_mark_node)
233 return error_mark_node;
235 gcc_assert (DECL_P (decl));
237 if (TREE_CODE (decl) == TYPE_DECL)
239 tree type;
241 type = TREE_TYPE (decl);
242 if (type == error_mark_node)
243 return error_mark_node;
244 if (MAYBE_CLASS_TYPE_P (type)
245 && CLASSTYPE_TEMPLATE_INFO (type)
246 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
248 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
249 check_member_template (tmpl);
250 return tmpl;
252 return NULL_TREE;
254 else if (TREE_CODE (decl) == FIELD_DECL)
255 error ("data member %qD cannot be a member template", decl);
256 else if (DECL_TEMPLATE_INFO (decl))
258 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
260 check_member_template (DECL_TI_TEMPLATE (decl));
261 return DECL_TI_TEMPLATE (decl);
263 else
264 return decl;
266 else
267 error ("invalid member template declaration %qD", decl);
269 return error_mark_node;
272 /* Return the template info node corresponding to T, whatever T is. */
274 tree
275 get_template_info (tree t)
277 tree tinfo = NULL_TREE;
279 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
280 tinfo = DECL_TEMPLATE_INFO (t);
282 if (!tinfo && TREE_CODE (t) == TYPE_DECL)
283 t = TREE_TYPE (t);
285 if (TAGGED_TYPE_P (t))
286 tinfo = TYPE_TEMPLATE_INFO (t);
288 return tinfo;
291 /* Returns the template nesting level of the indicated class TYPE.
293 For example, in:
294 template <class T>
295 struct A
297 template <class U>
298 struct B {};
301 A<T>::B<U> has depth two, while A<T> has depth one.
302 Both A<T>::B<int> and A<int>::B<U> have depth one, if
303 they are instantiations, not specializations.
305 This function is guaranteed to return 0 if passed NULL_TREE so
306 that, for example, `template_class_depth (current_class_type)' is
307 always safe. */
310 template_class_depth (tree type)
312 int depth;
314 for (depth = 0;
315 type && TREE_CODE (type) != NAMESPACE_DECL;
316 type = (TREE_CODE (type) == FUNCTION_DECL)
317 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
319 tree tinfo = get_template_info (type);
321 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
322 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
323 ++depth;
326 return depth;
329 /* Subroutine of maybe_begin_member_template_processing.
330 Returns true if processing DECL needs us to push template parms. */
332 static bool
333 inline_needs_template_parms (tree decl)
335 if (! DECL_TEMPLATE_INFO (decl))
336 return false;
338 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
339 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
342 /* Subroutine of maybe_begin_member_template_processing.
343 Push the template parms in PARMS, starting from LEVELS steps into the
344 chain, and ending at the beginning, since template parms are listed
345 innermost first. */
347 static void
348 push_inline_template_parms_recursive (tree parmlist, int levels)
350 tree parms = TREE_VALUE (parmlist);
351 int i;
353 if (levels > 1)
354 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
356 ++processing_template_decl;
357 current_template_parms
358 = tree_cons (size_int (processing_template_decl),
359 parms, current_template_parms);
360 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
362 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
363 NULL);
364 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
366 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
368 if (parm == error_mark_node)
369 continue;
371 gcc_assert (DECL_P (parm));
373 switch (TREE_CODE (parm))
375 case TYPE_DECL:
376 case TEMPLATE_DECL:
377 pushdecl (parm);
378 break;
380 case PARM_DECL:
382 /* Make a CONST_DECL as is done in process_template_parm.
383 It is ugly that we recreate this here; the original
384 version built in process_template_parm is no longer
385 available. */
386 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
387 CONST_DECL, DECL_NAME (parm),
388 TREE_TYPE (parm));
389 DECL_ARTIFICIAL (decl) = 1;
390 TREE_CONSTANT (decl) = 1;
391 TREE_READONLY (decl) = 1;
392 DECL_INITIAL (decl) = DECL_INITIAL (parm);
393 SET_DECL_TEMPLATE_PARM_P (decl);
394 pushdecl (decl);
396 break;
398 default:
399 gcc_unreachable ();
404 /* Restore the template parameter context for a member template or
405 a friend template defined in a class definition. */
407 void
408 maybe_begin_member_template_processing (tree decl)
410 tree parms;
411 int levels = 0;
413 if (inline_needs_template_parms (decl))
415 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
416 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
418 if (DECL_TEMPLATE_SPECIALIZATION (decl))
420 --levels;
421 parms = TREE_CHAIN (parms);
424 push_inline_template_parms_recursive (parms, levels);
427 /* Remember how many levels of template parameters we pushed so that
428 we can pop them later. */
429 VEC_safe_push (int, heap, inline_parm_levels, levels);
432 /* Undo the effects of maybe_begin_member_template_processing. */
434 void
435 maybe_end_member_template_processing (void)
437 int i;
438 int last;
440 if (VEC_length (int, inline_parm_levels) == 0)
441 return;
443 last = VEC_pop (int, inline_parm_levels);
444 for (i = 0; i < last; ++i)
446 --processing_template_decl;
447 current_template_parms = TREE_CHAIN (current_template_parms);
448 poplevel (0, 0, 0);
452 /* Return a new template argument vector which contains all of ARGS,
453 but has as its innermost set of arguments the EXTRA_ARGS. */
455 static tree
456 add_to_template_args (tree args, tree extra_args)
458 tree new_args;
459 int extra_depth;
460 int i;
461 int j;
463 extra_depth = TMPL_ARGS_DEPTH (extra_args);
464 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
466 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
467 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
469 for (j = 1; j <= extra_depth; ++j, ++i)
470 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
472 return new_args;
475 /* Like add_to_template_args, but only the outermost ARGS are added to
476 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
477 (EXTRA_ARGS) levels are added. This function is used to combine
478 the template arguments from a partial instantiation with the
479 template arguments used to attain the full instantiation from the
480 partial instantiation. */
482 static tree
483 add_outermost_template_args (tree args, tree extra_args)
485 tree new_args;
487 /* If there are more levels of EXTRA_ARGS than there are ARGS,
488 something very fishy is going on. */
489 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
491 /* If *all* the new arguments will be the EXTRA_ARGS, just return
492 them. */
493 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
494 return extra_args;
496 /* For the moment, we make ARGS look like it contains fewer levels. */
497 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
499 new_args = add_to_template_args (args, extra_args);
501 /* Now, we restore ARGS to its full dimensions. */
502 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
504 return new_args;
507 /* Return the N levels of innermost template arguments from the ARGS. */
509 tree
510 get_innermost_template_args (tree args, int n)
512 tree new_args;
513 int extra_levels;
514 int i;
516 gcc_assert (n >= 0);
518 /* If N is 1, just return the innermost set of template arguments. */
519 if (n == 1)
520 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
522 /* If we're not removing anything, just return the arguments we were
523 given. */
524 extra_levels = TMPL_ARGS_DEPTH (args) - n;
525 gcc_assert (extra_levels >= 0);
526 if (extra_levels == 0)
527 return args;
529 /* Make a new set of arguments, not containing the outer arguments. */
530 new_args = make_tree_vec (n);
531 for (i = 1; i <= n; ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i,
533 TMPL_ARGS_LEVEL (args, i + extra_levels));
535 return new_args;
538 /* The inverse of get_innermost_template_args: Return all but the innermost
539 EXTRA_LEVELS levels of template arguments from the ARGS. */
541 static tree
542 strip_innermost_template_args (tree args, int extra_levels)
544 tree new_args;
545 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
546 int i;
548 gcc_assert (n >= 0);
550 /* If N is 1, just return the outermost set of template arguments. */
551 if (n == 1)
552 return TMPL_ARGS_LEVEL (args, 1);
554 /* If we're not removing anything, just return the arguments we were
555 given. */
556 gcc_assert (extra_levels >= 0);
557 if (extra_levels == 0)
558 return args;
560 /* Make a new set of arguments, not containing the inner arguments. */
561 new_args = make_tree_vec (n);
562 for (i = 1; i <= n; ++i)
563 SET_TMPL_ARGS_LEVEL (new_args, i,
564 TMPL_ARGS_LEVEL (args, i));
566 return new_args;
569 /* We've got a template header coming up; push to a new level for storing
570 the parms. */
572 void
573 begin_template_parm_list (void)
575 /* We use a non-tag-transparent scope here, which causes pushtag to
576 put tags in this scope, rather than in the enclosing class or
577 namespace scope. This is the right thing, since we want
578 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
579 global template class, push_template_decl handles putting the
580 TEMPLATE_DECL into top-level scope. For a nested template class,
581 e.g.:
583 template <class T> struct S1 {
584 template <class T> struct S2 {};
587 pushtag contains special code to call pushdecl_with_scope on the
588 TEMPLATE_DECL for S2. */
589 begin_scope (sk_template_parms, NULL);
590 ++processing_template_decl;
591 ++processing_template_parmlist;
592 note_template_header (0);
595 /* This routine is called when a specialization is declared. If it is
596 invalid to declare a specialization here, an error is reported and
597 false is returned, otherwise this routine will return true. */
599 static bool
600 check_specialization_scope (void)
602 tree scope = current_scope ();
604 /* [temp.expl.spec]
606 An explicit specialization shall be declared in the namespace of
607 which the template is a member, or, for member templates, in the
608 namespace of which the enclosing class or enclosing class
609 template is a member. An explicit specialization of a member
610 function, member class or static data member of a class template
611 shall be declared in the namespace of which the class template
612 is a member. */
613 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
615 error ("explicit specialization in non-namespace scope %qD", scope);
616 return false;
619 /* [temp.expl.spec]
621 In an explicit specialization declaration for a member of a class
622 template or a member template that appears in namespace scope,
623 the member template and some of its enclosing class templates may
624 remain unspecialized, except that the declaration shall not
625 explicitly specialize a class member template if its enclosing
626 class templates are not explicitly specialized as well. */
627 if (current_template_parms)
629 error ("enclosing class templates are not explicitly specialized");
630 return false;
633 return true;
636 /* We've just seen template <>. */
638 bool
639 begin_specialization (void)
641 begin_scope (sk_template_spec, NULL);
642 note_template_header (1);
643 return check_specialization_scope ();
646 /* Called at then end of processing a declaration preceded by
647 template<>. */
649 void
650 end_specialization (void)
652 finish_scope ();
653 reset_specialization ();
656 /* Any template <>'s that we have seen thus far are not referring to a
657 function specialization. */
659 void
660 reset_specialization (void)
662 processing_specialization = 0;
663 template_header_count = 0;
666 /* We've just seen a template header. If SPECIALIZATION is nonzero,
667 it was of the form template <>. */
669 static void
670 note_template_header (int specialization)
672 processing_specialization = specialization;
673 template_header_count++;
676 /* We're beginning an explicit instantiation. */
678 void
679 begin_explicit_instantiation (void)
681 gcc_assert (!processing_explicit_instantiation);
682 processing_explicit_instantiation = true;
686 void
687 end_explicit_instantiation (void)
689 gcc_assert (processing_explicit_instantiation);
690 processing_explicit_instantiation = false;
693 /* An explicit specialization or partial specialization TMPL is being
694 declared. Check that the namespace in which the specialization is
695 occurring is permissible. Returns false iff it is invalid to
696 specialize TMPL in the current namespace. */
698 static bool
699 check_specialization_namespace (tree tmpl)
701 tree tpl_ns = decl_namespace_context (tmpl);
703 /* [tmpl.expl.spec]
705 An explicit specialization shall be declared in the namespace of
706 which the template is a member, or, for member templates, in the
707 namespace of which the enclosing class or enclosing class
708 template is a member. An explicit specialization of a member
709 function, member class or static data member of a class template
710 shall be declared in the namespace of which the class template is
711 a member. */
712 if (is_associated_namespace (current_namespace, tpl_ns))
713 /* Same or super-using namespace. */
714 return true;
715 else
717 permerror (input_location, "specialization of %qD in different namespace", tmpl);
718 permerror (input_location, " from definition of %q+#D", tmpl);
719 return false;
723 /* SPEC is an explicit instantiation. Check that it is valid to
724 perform this explicit instantiation in the current namespace. */
726 static void
727 check_explicit_instantiation_namespace (tree spec)
729 tree ns;
731 /* DR 275: An explicit instantiation shall appear in an enclosing
732 namespace of its template. */
733 ns = decl_namespace_context (spec);
734 if (!is_ancestor (current_namespace, ns))
735 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
736 "(which does not enclose namespace %qD)",
737 spec, current_namespace, ns);
740 /* The TYPE is being declared. If it is a template type, that means it
741 is a partial specialization. Do appropriate error-checking. */
743 tree
744 maybe_process_partial_specialization (tree type)
746 tree context;
748 if (type == error_mark_node)
749 return error_mark_node;
751 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
753 error ("name of class shadows template template parameter %qD",
754 TYPE_NAME (type));
755 return error_mark_node;
758 context = TYPE_CONTEXT (type);
760 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
762 /* This is for ordinary explicit specialization and partial
763 specialization of a template class such as:
765 template <> class C<int>;
769 template <class T> class C<T*>;
771 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
773 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
774 && !COMPLETE_TYPE_P (type))
776 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
777 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
778 if (processing_template_decl)
780 if (push_template_decl (TYPE_MAIN_DECL (type))
781 == error_mark_node)
782 return error_mark_node;
785 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
786 error ("specialization of %qT after instantiation", type);
788 else if (CLASS_TYPE_P (type)
789 && !CLASSTYPE_USE_TEMPLATE (type)
790 && CLASSTYPE_TEMPLATE_INFO (type)
791 && context && CLASS_TYPE_P (context)
792 && CLASSTYPE_TEMPLATE_INFO (context))
794 /* This is for an explicit specialization of member class
795 template according to [temp.expl.spec/18]:
797 template <> template <class U> class C<int>::D;
799 The context `C<int>' must be an implicit instantiation.
800 Otherwise this is just a member class template declared
801 earlier like:
803 template <> class C<int> { template <class U> class D; };
804 template <> template <class U> class C<int>::D;
806 In the first case, `C<int>::D' is a specialization of `C<T>::D'
807 while in the second case, `C<int>::D' is a primary template
808 and `C<T>::D' may not exist. */
810 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
811 && !COMPLETE_TYPE_P (type))
813 tree t;
815 if (current_namespace
816 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
818 permerror (input_location, "specializing %q#T in different namespace", type);
819 permerror (input_location, " from definition of %q+#D",
820 CLASSTYPE_TI_TEMPLATE (type));
823 /* Check for invalid specialization after instantiation:
825 template <> template <> class C<int>::D<int>;
826 template <> template <class U> class C<int>::D; */
828 for (t = DECL_TEMPLATE_INSTANTIATIONS
829 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
830 t; t = TREE_CHAIN (t))
831 if (TREE_VALUE (t) != type
832 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
833 error ("specialization %qT after instantiation %qT",
834 type, TREE_VALUE (t));
836 /* Mark TYPE as a specialization. And as a result, we only
837 have one level of template argument for the innermost
838 class template. */
839 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
840 CLASSTYPE_TI_ARGS (type)
841 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
844 else if (processing_specialization)
846 error ("explicit specialization of non-template %qT", type);
847 return error_mark_node;
850 return type;
853 /* Returns nonzero if we can optimize the retrieval of specializations
854 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
855 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
857 static inline bool
858 optimize_specialization_lookup_p (tree tmpl)
860 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
861 && DECL_CLASS_SCOPE_P (tmpl)
862 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
863 parameter. */
864 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
865 /* The optimized lookup depends on the fact that the
866 template arguments for the member function template apply
867 purely to the containing class, which is not true if the
868 containing class is an explicit or partial
869 specialization. */
870 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
871 && !DECL_MEMBER_TEMPLATE_P (tmpl)
872 && !DECL_CONV_FN_P (tmpl)
873 /* It is possible to have a template that is not a member
874 template and is not a member of a template class:
876 template <typename T>
877 struct S { friend A::f(); };
879 Here, the friend function is a template, but the context does
880 not have template information. The optimized lookup relies
881 on having ARGS be the template arguments for both the class
882 and the function template. */
883 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
886 /* Retrieve the specialization (in the sense of [temp.spec] - a
887 specialization is either an instantiation or an explicit
888 specialization) of TMPL for the given template ARGS. If there is
889 no such specialization, return NULL_TREE. The ARGS are a vector of
890 arguments, or a vector of vectors of arguments, in the case of
891 templates with more than one level of parameters.
893 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
894 then we search for a partial specialization matching ARGS. This
895 parameter is ignored if TMPL is not a class template. */
897 static tree
898 retrieve_specialization (tree tmpl, tree args,
899 bool class_specializations_p)
901 if (args == error_mark_node)
902 return NULL_TREE;
904 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
906 /* There should be as many levels of arguments as there are
907 levels of parameters. */
908 gcc_assert (TMPL_ARGS_DEPTH (args)
909 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
911 if (optimize_specialization_lookup_p (tmpl))
913 tree class_template;
914 tree class_specialization;
915 VEC(tree,gc) *methods;
916 tree fns;
917 int idx;
919 /* The template arguments actually apply to the containing
920 class. Find the class specialization with those
921 arguments. */
922 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
923 class_specialization
924 = retrieve_specialization (class_template, args,
925 /*class_specializations_p=*/false);
926 if (!class_specialization)
927 return NULL_TREE;
928 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
929 for the specialization. */
930 idx = class_method_index_for_fn (class_specialization, tmpl);
931 if (idx == -1)
932 return NULL_TREE;
933 /* Iterate through the methods with the indicated name, looking
934 for the one that has an instance of TMPL. */
935 methods = CLASSTYPE_METHOD_VEC (class_specialization);
936 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
938 tree fn = OVL_CURRENT (fns);
939 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
940 return fn;
942 return NULL_TREE;
944 else
946 tree *sp;
947 tree *head;
949 /* Class templates store their instantiations on the
950 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
951 DECL_TEMPLATE_SPECIALIZATIONS list. */
952 if (!class_specializations_p
953 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL
954 && !is_typedef_decl (DECL_TEMPLATE_RESULT (tmpl))
955 && TAGGED_TYPE_P (TREE_TYPE (tmpl)))
956 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
957 else
958 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
959 head = sp;
960 /* Iterate through the list until we find a matching template. */
961 while (*sp != NULL_TREE)
963 tree spec = *sp;
965 if (comp_template_args (TREE_PURPOSE (spec), args))
967 /* Use the move-to-front heuristic to speed up future
968 searches. */
969 if (spec != *head)
971 *sp = TREE_CHAIN (*sp);
972 TREE_CHAIN (spec) = *head;
973 *head = spec;
975 return TREE_VALUE (spec);
977 sp = &TREE_CHAIN (spec);
981 return NULL_TREE;
984 /* Like retrieve_specialization, but for local declarations. */
986 static tree
987 retrieve_local_specialization (tree tmpl)
989 tree spec;
991 if (local_specializations == NULL)
992 return NULL_TREE;
994 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
995 htab_hash_pointer (tmpl));
996 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
999 /* Returns nonzero iff DECL is a specialization of TMPL. */
1002 is_specialization_of (tree decl, tree tmpl)
1004 tree t;
1006 if (TREE_CODE (decl) == FUNCTION_DECL)
1008 for (t = decl;
1009 t != NULL_TREE;
1010 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1011 if (t == tmpl)
1012 return 1;
1014 else
1016 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1018 for (t = TREE_TYPE (decl);
1019 t != NULL_TREE;
1020 t = CLASSTYPE_USE_TEMPLATE (t)
1021 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1022 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1023 return 1;
1026 return 0;
1029 /* Returns nonzero iff DECL is a specialization of friend declaration
1030 FRIEND_DECL according to [temp.friend]. */
1032 bool
1033 is_specialization_of_friend (tree decl, tree friend_decl)
1035 bool need_template = true;
1036 int template_depth;
1038 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1039 || TREE_CODE (decl) == TYPE_DECL);
1041 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1042 of a template class, we want to check if DECL is a specialization
1043 if this. */
1044 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1045 && DECL_TEMPLATE_INFO (friend_decl)
1046 && !DECL_USE_TEMPLATE (friend_decl))
1048 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1049 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1050 need_template = false;
1052 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1053 && !PRIMARY_TEMPLATE_P (friend_decl))
1054 need_template = false;
1056 /* There is nothing to do if this is not a template friend. */
1057 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1058 return false;
1060 if (is_specialization_of (decl, friend_decl))
1061 return true;
1063 /* [temp.friend/6]
1064 A member of a class template may be declared to be a friend of a
1065 non-template class. In this case, the corresponding member of
1066 every specialization of the class template is a friend of the
1067 class granting friendship.
1069 For example, given a template friend declaration
1071 template <class T> friend void A<T>::f();
1073 the member function below is considered a friend
1075 template <> struct A<int> {
1076 void f();
1079 For this type of template friend, TEMPLATE_DEPTH below will be
1080 nonzero. To determine if DECL is a friend of FRIEND, we first
1081 check if the enclosing class is a specialization of another. */
1083 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1084 if (template_depth
1085 && DECL_CLASS_SCOPE_P (decl)
1086 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1087 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1089 /* Next, we check the members themselves. In order to handle
1090 a few tricky cases, such as when FRIEND_DECL's are
1092 template <class T> friend void A<T>::g(T t);
1093 template <class T> template <T t> friend void A<T>::h();
1095 and DECL's are
1097 void A<int>::g(int);
1098 template <int> void A<int>::h();
1100 we need to figure out ARGS, the template arguments from
1101 the context of DECL. This is required for template substitution
1102 of `T' in the function parameter of `g' and template parameter
1103 of `h' in the above examples. Here ARGS corresponds to `int'. */
1105 tree context = DECL_CONTEXT (decl);
1106 tree args = NULL_TREE;
1107 int current_depth = 0;
1109 while (current_depth < template_depth)
1111 if (CLASSTYPE_TEMPLATE_INFO (context))
1113 if (current_depth == 0)
1114 args = TYPE_TI_ARGS (context);
1115 else
1116 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1117 current_depth++;
1119 context = TYPE_CONTEXT (context);
1122 if (TREE_CODE (decl) == FUNCTION_DECL)
1124 bool is_template;
1125 tree friend_type;
1126 tree decl_type;
1127 tree friend_args_type;
1128 tree decl_args_type;
1130 /* Make sure that both DECL and FRIEND_DECL are templates or
1131 non-templates. */
1132 is_template = DECL_TEMPLATE_INFO (decl)
1133 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1134 if (need_template ^ is_template)
1135 return false;
1136 else if (is_template)
1138 /* If both are templates, check template parameter list. */
1139 tree friend_parms
1140 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1141 args, tf_none);
1142 if (!comp_template_parms
1143 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1144 friend_parms))
1145 return false;
1147 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1149 else
1150 decl_type = TREE_TYPE (decl);
1152 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1153 tf_none, NULL_TREE);
1154 if (friend_type == error_mark_node)
1155 return false;
1157 /* Check if return types match. */
1158 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1159 return false;
1161 /* Check if function parameter types match, ignoring the
1162 `this' parameter. */
1163 friend_args_type = TYPE_ARG_TYPES (friend_type);
1164 decl_args_type = TYPE_ARG_TYPES (decl_type);
1165 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1166 friend_args_type = TREE_CHAIN (friend_args_type);
1167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1168 decl_args_type = TREE_CHAIN (decl_args_type);
1170 return compparms (decl_args_type, friend_args_type);
1172 else
1174 /* DECL is a TYPE_DECL */
1175 bool is_template;
1176 tree decl_type = TREE_TYPE (decl);
1178 /* Make sure that both DECL and FRIEND_DECL are templates or
1179 non-templates. */
1180 is_template
1181 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1182 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1184 if (need_template ^ is_template)
1185 return false;
1186 else if (is_template)
1188 tree friend_parms;
1189 /* If both are templates, check the name of the two
1190 TEMPLATE_DECL's first because is_friend didn't. */
1191 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1192 != DECL_NAME (friend_decl))
1193 return false;
1195 /* Now check template parameter list. */
1196 friend_parms
1197 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1198 args, tf_none);
1199 return comp_template_parms
1200 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1201 friend_parms);
1203 else
1204 return (DECL_NAME (decl)
1205 == DECL_NAME (friend_decl));
1208 return false;
1211 /* Register the specialization SPEC as a specialization of TMPL with
1212 the indicated ARGS. IS_FRIEND indicates whether the specialization
1213 is actually just a friend declaration. Returns SPEC, or an
1214 equivalent prior declaration, if available. */
1216 static tree
1217 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1219 tree fn;
1221 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1223 if (TREE_CODE (spec) == FUNCTION_DECL
1224 && uses_template_parms (DECL_TI_ARGS (spec)))
1225 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1226 register it; we want the corresponding TEMPLATE_DECL instead.
1227 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1228 the more obvious `uses_template_parms (spec)' to avoid problems
1229 with default function arguments. In particular, given
1230 something like this:
1232 template <class T> void f(T t1, T t = T())
1234 the default argument expression is not substituted for in an
1235 instantiation unless and until it is actually needed. */
1236 return spec;
1238 fn = retrieve_specialization (tmpl, args,
1239 /*class_specializations_p=*/false);
1240 /* We can sometimes try to re-register a specialization that we've
1241 already got. In particular, regenerate_decl_from_template calls
1242 duplicate_decls which will update the specialization list. But,
1243 we'll still get called again here anyhow. It's more convenient
1244 to simply allow this than to try to prevent it. */
1245 if (fn == spec)
1246 return spec;
1247 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1249 if (DECL_TEMPLATE_INSTANTIATION (fn))
1251 if (TREE_USED (fn)
1252 || DECL_EXPLICIT_INSTANTIATION (fn))
1254 error ("specialization of %qD after instantiation",
1255 fn);
1256 return error_mark_node;
1258 else
1260 tree clone;
1261 /* This situation should occur only if the first
1262 specialization is an implicit instantiation, the
1263 second is an explicit specialization, and the
1264 implicit instantiation has not yet been used. That
1265 situation can occur if we have implicitly
1266 instantiated a member function and then specialized
1267 it later.
1269 We can also wind up here if a friend declaration that
1270 looked like an instantiation turns out to be a
1271 specialization:
1273 template <class T> void foo(T);
1274 class S { friend void foo<>(int) };
1275 template <> void foo(int);
1277 We transform the existing DECL in place so that any
1278 pointers to it become pointers to the updated
1279 declaration.
1281 If there was a definition for the template, but not
1282 for the specialization, we want this to look as if
1283 there were no definition, and vice versa. */
1284 DECL_INITIAL (fn) = NULL_TREE;
1285 duplicate_decls (spec, fn, is_friend);
1286 /* The call to duplicate_decls will have applied
1287 [temp.expl.spec]:
1289 An explicit specialization of a function template
1290 is inline only if it is explicitly declared to be,
1291 and independently of whether its function template
1294 to the primary function; now copy the inline bits to
1295 the various clones. */
1296 FOR_EACH_CLONE (clone, fn)
1297 DECL_DECLARED_INLINE_P (clone)
1298 = DECL_DECLARED_INLINE_P (fn);
1299 check_specialization_namespace (fn);
1301 return fn;
1304 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1306 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1307 /* Dup decl failed, but this is a new definition. Set the
1308 line number so any errors match this new
1309 definition. */
1310 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1312 return fn;
1316 /* A specialization must be declared in the same namespace as the
1317 template it is specializing. */
1318 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1319 && !check_specialization_namespace (tmpl))
1320 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1322 if (!optimize_specialization_lookup_p (tmpl))
1323 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1324 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1326 return spec;
1329 /* Unregister the specialization SPEC as a specialization of TMPL.
1330 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1331 if the SPEC was listed as a specialization of TMPL. */
1333 bool
1334 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1336 tree* s;
1338 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1339 *s != NULL_TREE;
1340 s = &TREE_CHAIN (*s))
1341 if (TREE_VALUE (*s) == spec)
1343 if (!new_spec)
1344 *s = TREE_CHAIN (*s);
1345 else
1346 TREE_VALUE (*s) = new_spec;
1347 return 1;
1350 return 0;
1353 /* Compare an entry in the local specializations hash table P1 (which
1354 is really a pointer to a TREE_LIST) with P2 (which is really a
1355 DECL). */
1357 static int
1358 eq_local_specializations (const void *p1, const void *p2)
1360 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1363 /* Hash P1, an entry in the local specializations table. */
1365 static hashval_t
1366 hash_local_specialization (const void* p1)
1368 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1371 /* Like register_specialization, but for local declarations. We are
1372 registering SPEC, an instantiation of TMPL. */
1374 static void
1375 register_local_specialization (tree spec, tree tmpl)
1377 void **slot;
1379 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1380 htab_hash_pointer (tmpl), INSERT);
1381 *slot = build_tree_list (spec, tmpl);
1384 /* TYPE is a class type. Returns true if TYPE is an explicitly
1385 specialized class. */
1387 bool
1388 explicit_class_specialization_p (tree type)
1390 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1391 return false;
1392 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1395 /* Print the list of candidate FNS in an error message. */
1397 void
1398 print_candidates (tree fns)
1400 tree fn;
1402 const char *str = "candidates are:";
1404 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1406 tree f;
1408 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1409 error ("%s %+#D", str, OVL_CURRENT (f));
1410 str = " ";
1414 /* Returns the template (one of the functions given by TEMPLATE_ID)
1415 which can be specialized to match the indicated DECL with the
1416 explicit template args given in TEMPLATE_ID. The DECL may be
1417 NULL_TREE if none is available. In that case, the functions in
1418 TEMPLATE_ID are non-members.
1420 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1421 specialization of a member template.
1423 The TEMPLATE_COUNT is the number of references to qualifying
1424 template classes that appeared in the name of the function. See
1425 check_explicit_specialization for a more accurate description.
1427 TSK indicates what kind of template declaration (if any) is being
1428 declared. TSK_TEMPLATE indicates that the declaration given by
1429 DECL, though a FUNCTION_DECL, has template parameters, and is
1430 therefore a template function.
1432 The template args (those explicitly specified and those deduced)
1433 are output in a newly created vector *TARGS_OUT.
1435 If it is impossible to determine the result, an error message is
1436 issued. The error_mark_node is returned to indicate failure. */
1438 static tree
1439 determine_specialization (tree template_id,
1440 tree decl,
1441 tree* targs_out,
1442 int need_member_template,
1443 int template_count,
1444 tmpl_spec_kind tsk)
1446 tree fns;
1447 tree targs;
1448 tree explicit_targs;
1449 tree candidates = NULL_TREE;
1450 /* A TREE_LIST of templates of which DECL may be a specialization.
1451 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1452 corresponding TREE_PURPOSE is the set of template arguments that,
1453 when used to instantiate the template, would produce a function
1454 with the signature of DECL. */
1455 tree templates = NULL_TREE;
1456 int header_count;
1457 struct cp_binding_level *b;
1459 *targs_out = NULL_TREE;
1461 if (template_id == error_mark_node || decl == error_mark_node)
1462 return error_mark_node;
1464 fns = TREE_OPERAND (template_id, 0);
1465 explicit_targs = TREE_OPERAND (template_id, 1);
1467 if (fns == error_mark_node)
1468 return error_mark_node;
1470 /* Check for baselinks. */
1471 if (BASELINK_P (fns))
1472 fns = BASELINK_FUNCTIONS (fns);
1474 if (!is_overloaded_fn (fns))
1476 error ("%qD is not a function template", fns);
1477 return error_mark_node;
1480 /* Count the number of template headers specified for this
1481 specialization. */
1482 header_count = 0;
1483 for (b = current_binding_level;
1484 b->kind == sk_template_parms;
1485 b = b->level_chain)
1486 ++header_count;
1488 for (; fns; fns = OVL_NEXT (fns))
1490 tree fn = OVL_CURRENT (fns);
1492 if (TREE_CODE (fn) == TEMPLATE_DECL)
1494 tree decl_arg_types;
1495 tree fn_arg_types;
1497 /* In case of explicit specialization, we need to check if
1498 the number of template headers appearing in the specialization
1499 is correct. This is usually done in check_explicit_specialization,
1500 but the check done there cannot be exhaustive when specializing
1501 member functions. Consider the following code:
1503 template <> void A<int>::f(int);
1504 template <> template <> void A<int>::f(int);
1506 Assuming that A<int> is not itself an explicit specialization
1507 already, the first line specializes "f" which is a non-template
1508 member function, whilst the second line specializes "f" which
1509 is a template member function. So both lines are syntactically
1510 correct, and check_explicit_specialization does not reject
1511 them.
1513 Here, we can do better, as we are matching the specialization
1514 against the declarations. We count the number of template
1515 headers, and we check if they match TEMPLATE_COUNT + 1
1516 (TEMPLATE_COUNT is the number of qualifying template classes,
1517 plus there must be another header for the member template
1518 itself).
1520 Notice that if header_count is zero, this is not a
1521 specialization but rather a template instantiation, so there
1522 is no check we can perform here. */
1523 if (header_count && header_count != template_count + 1)
1524 continue;
1526 /* Check that the number of template arguments at the
1527 innermost level for DECL is the same as for FN. */
1528 if (current_binding_level->kind == sk_template_parms
1529 && !current_binding_level->explicit_spec_p
1530 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1531 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1532 (current_template_parms))))
1533 continue;
1535 /* DECL might be a specialization of FN. */
1536 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1537 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1539 /* For a non-static member function, we need to make sure
1540 that the const qualification is the same. Since
1541 get_bindings does not try to merge the "this" parameter,
1542 we must do the comparison explicitly. */
1543 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1544 && !same_type_p (TREE_VALUE (fn_arg_types),
1545 TREE_VALUE (decl_arg_types)))
1546 continue;
1548 /* Skip the "this" parameter and, for constructors of
1549 classes with virtual bases, the VTT parameter. A
1550 full specialization of a constructor will have a VTT
1551 parameter, but a template never will. */
1552 decl_arg_types
1553 = skip_artificial_parms_for (decl, decl_arg_types);
1554 fn_arg_types
1555 = skip_artificial_parms_for (fn, fn_arg_types);
1557 /* Check that the number of function parameters matches.
1558 For example,
1559 template <class T> void f(int i = 0);
1560 template <> void f<int>();
1561 The specialization f<int> is invalid but is not caught
1562 by get_bindings below. */
1563 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1564 continue;
1566 /* Function templates cannot be specializations; there are
1567 no partial specializations of functions. Therefore, if
1568 the type of DECL does not match FN, there is no
1569 match. */
1570 if (tsk == tsk_template)
1572 if (compparms (fn_arg_types, decl_arg_types))
1573 candidates = tree_cons (NULL_TREE, fn, candidates);
1574 continue;
1577 /* See whether this function might be a specialization of this
1578 template. */
1579 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1581 if (!targs)
1582 /* We cannot deduce template arguments that when used to
1583 specialize TMPL will produce DECL. */
1584 continue;
1586 /* Save this template, and the arguments deduced. */
1587 templates = tree_cons (targs, fn, templates);
1589 else if (need_member_template)
1590 /* FN is an ordinary member function, and we need a
1591 specialization of a member template. */
1593 else if (TREE_CODE (fn) != FUNCTION_DECL)
1594 /* We can get IDENTIFIER_NODEs here in certain erroneous
1595 cases. */
1597 else if (!DECL_FUNCTION_MEMBER_P (fn))
1598 /* This is just an ordinary non-member function. Nothing can
1599 be a specialization of that. */
1601 else if (DECL_ARTIFICIAL (fn))
1602 /* Cannot specialize functions that are created implicitly. */
1604 else
1606 tree decl_arg_types;
1608 /* This is an ordinary member function. However, since
1609 we're here, we can assume it's enclosing class is a
1610 template class. For example,
1612 template <typename T> struct S { void f(); };
1613 template <> void S<int>::f() {}
1615 Here, S<int>::f is a non-template, but S<int> is a
1616 template class. If FN has the same type as DECL, we
1617 might be in business. */
1619 if (!DECL_TEMPLATE_INFO (fn))
1620 /* Its enclosing class is an explicit specialization
1621 of a template class. This is not a candidate. */
1622 continue;
1624 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1625 TREE_TYPE (TREE_TYPE (fn))))
1626 /* The return types differ. */
1627 continue;
1629 /* Adjust the type of DECL in case FN is a static member. */
1630 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1631 if (DECL_STATIC_FUNCTION_P (fn)
1632 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1633 decl_arg_types = TREE_CHAIN (decl_arg_types);
1635 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1636 decl_arg_types))
1637 /* They match! */
1638 candidates = tree_cons (NULL_TREE, fn, candidates);
1642 if (templates && TREE_CHAIN (templates))
1644 /* We have:
1646 [temp.expl.spec]
1648 It is possible for a specialization with a given function
1649 signature to be instantiated from more than one function
1650 template. In such cases, explicit specification of the
1651 template arguments must be used to uniquely identify the
1652 function template specialization being specialized.
1654 Note that here, there's no suggestion that we're supposed to
1655 determine which of the candidate templates is most
1656 specialized. However, we, also have:
1658 [temp.func.order]
1660 Partial ordering of overloaded function template
1661 declarations is used in the following contexts to select
1662 the function template to which a function template
1663 specialization refers:
1665 -- when an explicit specialization refers to a function
1666 template.
1668 So, we do use the partial ordering rules, at least for now.
1669 This extension can only serve to make invalid programs valid,
1670 so it's safe. And, there is strong anecdotal evidence that
1671 the committee intended the partial ordering rules to apply;
1672 the EDG front end has that behavior, and John Spicer claims
1673 that the committee simply forgot to delete the wording in
1674 [temp.expl.spec]. */
1675 tree tmpl = most_specialized_instantiation (templates);
1676 if (tmpl != error_mark_node)
1678 templates = tmpl;
1679 TREE_CHAIN (templates) = NULL_TREE;
1683 if (templates == NULL_TREE && candidates == NULL_TREE)
1685 error ("template-id %qD for %q+D does not match any template "
1686 "declaration", template_id, decl);
1687 return error_mark_node;
1689 else if ((templates && TREE_CHAIN (templates))
1690 || (candidates && TREE_CHAIN (candidates))
1691 || (templates && candidates))
1693 error ("ambiguous template specialization %qD for %q+D",
1694 template_id, decl);
1695 chainon (candidates, templates);
1696 print_candidates (candidates);
1697 return error_mark_node;
1700 /* We have one, and exactly one, match. */
1701 if (candidates)
1703 tree fn = TREE_VALUE (candidates);
1704 /* DECL is a re-declaration of a template function. */
1705 if (TREE_CODE (fn) == TEMPLATE_DECL)
1706 return fn;
1707 /* It was a specialization of an ordinary member function in a
1708 template class. */
1709 *targs_out = copy_node (DECL_TI_ARGS (fn));
1710 return DECL_TI_TEMPLATE (fn);
1713 /* It was a specialization of a template. */
1714 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1715 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1717 *targs_out = copy_node (targs);
1718 SET_TMPL_ARGS_LEVEL (*targs_out,
1719 TMPL_ARGS_DEPTH (*targs_out),
1720 TREE_PURPOSE (templates));
1722 else
1723 *targs_out = TREE_PURPOSE (templates);
1724 return TREE_VALUE (templates);
1727 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1728 but with the default argument values filled in from those in the
1729 TMPL_TYPES. */
1731 static tree
1732 copy_default_args_to_explicit_spec_1 (tree spec_types,
1733 tree tmpl_types)
1735 tree new_spec_types;
1737 if (!spec_types)
1738 return NULL_TREE;
1740 if (spec_types == void_list_node)
1741 return void_list_node;
1743 /* Substitute into the rest of the list. */
1744 new_spec_types =
1745 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1746 TREE_CHAIN (tmpl_types));
1748 /* Add the default argument for this parameter. */
1749 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1750 TREE_VALUE (spec_types),
1751 new_spec_types);
1754 /* DECL is an explicit specialization. Replicate default arguments
1755 from the template it specializes. (That way, code like:
1757 template <class T> void f(T = 3);
1758 template <> void f(double);
1759 void g () { f (); }
1761 works, as required.) An alternative approach would be to look up
1762 the correct default arguments at the call-site, but this approach
1763 is consistent with how implicit instantiations are handled. */
1765 static void
1766 copy_default_args_to_explicit_spec (tree decl)
1768 tree tmpl;
1769 tree spec_types;
1770 tree tmpl_types;
1771 tree new_spec_types;
1772 tree old_type;
1773 tree new_type;
1774 tree t;
1775 tree object_type = NULL_TREE;
1776 tree in_charge = NULL_TREE;
1777 tree vtt = NULL_TREE;
1779 /* See if there's anything we need to do. */
1780 tmpl = DECL_TI_TEMPLATE (decl);
1781 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1782 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1783 if (TREE_PURPOSE (t))
1784 break;
1785 if (!t)
1786 return;
1788 old_type = TREE_TYPE (decl);
1789 spec_types = TYPE_ARG_TYPES (old_type);
1791 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1793 /* Remove the this pointer, but remember the object's type for
1794 CV quals. */
1795 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1796 spec_types = TREE_CHAIN (spec_types);
1797 tmpl_types = TREE_CHAIN (tmpl_types);
1799 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1801 /* DECL may contain more parameters than TMPL due to the extra
1802 in-charge parameter in constructors and destructors. */
1803 in_charge = spec_types;
1804 spec_types = TREE_CHAIN (spec_types);
1806 if (DECL_HAS_VTT_PARM_P (decl))
1808 vtt = spec_types;
1809 spec_types = TREE_CHAIN (spec_types);
1813 /* Compute the merged default arguments. */
1814 new_spec_types =
1815 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1817 /* Compute the new FUNCTION_TYPE. */
1818 if (object_type)
1820 if (vtt)
1821 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1822 TREE_VALUE (vtt),
1823 new_spec_types);
1825 if (in_charge)
1826 /* Put the in-charge parameter back. */
1827 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1828 TREE_VALUE (in_charge),
1829 new_spec_types);
1831 new_type = build_method_type_directly (object_type,
1832 TREE_TYPE (old_type),
1833 new_spec_types);
1835 else
1836 new_type = build_function_type (TREE_TYPE (old_type),
1837 new_spec_types);
1838 new_type = cp_build_type_attribute_variant (new_type,
1839 TYPE_ATTRIBUTES (old_type));
1840 new_type = build_exception_variant (new_type,
1841 TYPE_RAISES_EXCEPTIONS (old_type));
1842 TREE_TYPE (decl) = new_type;
1845 /* Check to see if the function just declared, as indicated in
1846 DECLARATOR, and in DECL, is a specialization of a function
1847 template. We may also discover that the declaration is an explicit
1848 instantiation at this point.
1850 Returns DECL, or an equivalent declaration that should be used
1851 instead if all goes well. Issues an error message if something is
1852 amiss. Returns error_mark_node if the error is not easily
1853 recoverable.
1855 FLAGS is a bitmask consisting of the following flags:
1857 2: The function has a definition.
1858 4: The function is a friend.
1860 The TEMPLATE_COUNT is the number of references to qualifying
1861 template classes that appeared in the name of the function. For
1862 example, in
1864 template <class T> struct S { void f(); };
1865 void S<int>::f();
1867 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1868 classes are not counted in the TEMPLATE_COUNT, so that in
1870 template <class T> struct S {};
1871 template <> struct S<int> { void f(); }
1872 template <> void S<int>::f();
1874 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1875 invalid; there should be no template <>.)
1877 If the function is a specialization, it is marked as such via
1878 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1879 is set up correctly, and it is added to the list of specializations
1880 for that template. */
1882 tree
1883 check_explicit_specialization (tree declarator,
1884 tree decl,
1885 int template_count,
1886 int flags)
1888 int have_def = flags & 2;
1889 int is_friend = flags & 4;
1890 int specialization = 0;
1891 int explicit_instantiation = 0;
1892 int member_specialization = 0;
1893 tree ctype = DECL_CLASS_CONTEXT (decl);
1894 tree dname = DECL_NAME (decl);
1895 tmpl_spec_kind tsk;
1897 if (is_friend)
1899 if (!processing_specialization)
1900 tsk = tsk_none;
1901 else
1902 tsk = tsk_excessive_parms;
1904 else
1905 tsk = current_tmpl_spec_kind (template_count);
1907 switch (tsk)
1909 case tsk_none:
1910 if (processing_specialization)
1912 specialization = 1;
1913 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1915 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1917 if (is_friend)
1918 /* This could be something like:
1920 template <class T> void f(T);
1921 class S { friend void f<>(int); } */
1922 specialization = 1;
1923 else
1925 /* This case handles bogus declarations like template <>
1926 template <class T> void f<int>(); */
1928 error ("template-id %qD in declaration of primary template",
1929 declarator);
1930 return decl;
1933 break;
1935 case tsk_invalid_member_spec:
1936 /* The error has already been reported in
1937 check_specialization_scope. */
1938 return error_mark_node;
1940 case tsk_invalid_expl_inst:
1941 error ("template parameter list used in explicit instantiation");
1943 /* Fall through. */
1945 case tsk_expl_inst:
1946 if (have_def)
1947 error ("definition provided for explicit instantiation");
1949 explicit_instantiation = 1;
1950 break;
1952 case tsk_excessive_parms:
1953 case tsk_insufficient_parms:
1954 if (tsk == tsk_excessive_parms)
1955 error ("too many template parameter lists in declaration of %qD",
1956 decl);
1957 else if (template_header_count)
1958 error("too few template parameter lists in declaration of %qD", decl);
1959 else
1960 error("explicit specialization of %qD must be introduced by "
1961 "%<template <>%>", decl);
1963 /* Fall through. */
1964 case tsk_expl_spec:
1965 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1966 if (ctype)
1967 member_specialization = 1;
1968 else
1969 specialization = 1;
1970 break;
1972 case tsk_template:
1973 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1975 /* This case handles bogus declarations like template <>
1976 template <class T> void f<int>(); */
1978 if (uses_template_parms (declarator))
1979 error ("function template partial specialization %qD "
1980 "is not allowed", declarator);
1981 else
1982 error ("template-id %qD in declaration of primary template",
1983 declarator);
1984 return decl;
1987 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1988 /* This is a specialization of a member template, without
1989 specialization the containing class. Something like:
1991 template <class T> struct S {
1992 template <class U> void f (U);
1994 template <> template <class U> void S<int>::f(U) {}
1996 That's a specialization -- but of the entire template. */
1997 specialization = 1;
1998 break;
2000 default:
2001 gcc_unreachable ();
2004 if (specialization || member_specialization)
2006 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2007 for (; t; t = TREE_CHAIN (t))
2008 if (TREE_PURPOSE (t))
2010 permerror (input_location,
2011 "default argument specified in explicit specialization");
2012 break;
2016 if (specialization || member_specialization || explicit_instantiation)
2018 tree tmpl = NULL_TREE;
2019 tree targs = NULL_TREE;
2021 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2022 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2024 tree fns;
2026 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2027 if (ctype)
2028 fns = dname;
2029 else
2031 /* If there is no class context, the explicit instantiation
2032 must be at namespace scope. */
2033 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2035 /* Find the namespace binding, using the declaration
2036 context. */
2037 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2038 false, true);
2039 if (fns == error_mark_node || !is_overloaded_fn (fns))
2041 error ("%qD is not a template function", dname);
2042 fns = error_mark_node;
2044 else
2046 tree fn = OVL_CURRENT (fns);
2047 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2048 CP_DECL_CONTEXT (fn)))
2049 error ("%qD is not declared in %qD",
2050 decl, current_namespace);
2054 declarator = lookup_template_function (fns, NULL_TREE);
2057 if (declarator == error_mark_node)
2058 return error_mark_node;
2060 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2062 if (!explicit_instantiation)
2063 /* A specialization in class scope. This is invalid,
2064 but the error will already have been flagged by
2065 check_specialization_scope. */
2066 return error_mark_node;
2067 else
2069 /* It's not valid to write an explicit instantiation in
2070 class scope, e.g.:
2072 class C { template void f(); }
2074 This case is caught by the parser. However, on
2075 something like:
2077 template class C { void f(); };
2079 (which is invalid) we can get here. The error will be
2080 issued later. */
2084 return decl;
2086 else if (ctype != NULL_TREE
2087 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2088 IDENTIFIER_NODE))
2090 /* Find the list of functions in ctype that have the same
2091 name as the declared function. */
2092 tree name = TREE_OPERAND (declarator, 0);
2093 tree fns = NULL_TREE;
2094 int idx;
2096 if (constructor_name_p (name, ctype))
2098 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2100 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2101 : !CLASSTYPE_DESTRUCTORS (ctype))
2103 /* From [temp.expl.spec]:
2105 If such an explicit specialization for the member
2106 of a class template names an implicitly-declared
2107 special member function (clause _special_), the
2108 program is ill-formed.
2110 Similar language is found in [temp.explicit]. */
2111 error ("specialization of implicitly-declared special member function");
2112 return error_mark_node;
2115 name = is_constructor ? ctor_identifier : dtor_identifier;
2118 if (!DECL_CONV_FN_P (decl))
2120 idx = lookup_fnfields_1 (ctype, name);
2121 if (idx >= 0)
2122 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2124 else
2126 VEC(tree,gc) *methods;
2127 tree ovl;
2129 /* For a type-conversion operator, we cannot do a
2130 name-based lookup. We might be looking for `operator
2131 int' which will be a specialization of `operator T'.
2132 So, we find *all* the conversion operators, and then
2133 select from them. */
2134 fns = NULL_TREE;
2136 methods = CLASSTYPE_METHOD_VEC (ctype);
2137 if (methods)
2138 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2139 VEC_iterate (tree, methods, idx, ovl);
2140 ++idx)
2142 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2143 /* There are no more conversion functions. */
2144 break;
2146 /* Glue all these conversion functions together
2147 with those we already have. */
2148 for (; ovl; ovl = OVL_NEXT (ovl))
2149 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2153 if (fns == NULL_TREE)
2155 error ("no member function %qD declared in %qT", name, ctype);
2156 return error_mark_node;
2158 else
2159 TREE_OPERAND (declarator, 0) = fns;
2162 /* Figure out what exactly is being specialized at this point.
2163 Note that for an explicit instantiation, even one for a
2164 member function, we cannot tell apriori whether the
2165 instantiation is for a member template, or just a member
2166 function of a template class. Even if a member template is
2167 being instantiated, the member template arguments may be
2168 elided if they can be deduced from the rest of the
2169 declaration. */
2170 tmpl = determine_specialization (declarator, decl,
2171 &targs,
2172 member_specialization,
2173 template_count,
2174 tsk);
2176 if (!tmpl || tmpl == error_mark_node)
2177 /* We couldn't figure out what this declaration was
2178 specializing. */
2179 return error_mark_node;
2180 else
2182 tree gen_tmpl = most_general_template (tmpl);
2184 if (explicit_instantiation)
2186 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2187 is done by do_decl_instantiation later. */
2189 int arg_depth = TMPL_ARGS_DEPTH (targs);
2190 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2192 if (arg_depth > parm_depth)
2194 /* If TMPL is not the most general template (for
2195 example, if TMPL is a friend template that is
2196 injected into namespace scope), then there will
2197 be too many levels of TARGS. Remove some of them
2198 here. */
2199 int i;
2200 tree new_targs;
2202 new_targs = make_tree_vec (parm_depth);
2203 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2204 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2205 = TREE_VEC_ELT (targs, i);
2206 targs = new_targs;
2209 return instantiate_template (tmpl, targs, tf_error);
2212 /* If we thought that the DECL was a member function, but it
2213 turns out to be specializing a static member function,
2214 make DECL a static member function as well. */
2215 if (DECL_STATIC_FUNCTION_P (tmpl)
2216 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2217 revert_static_member_fn (decl);
2219 /* If this is a specialization of a member template of a
2220 template class, we want to return the TEMPLATE_DECL, not
2221 the specialization of it. */
2222 if (tsk == tsk_template)
2224 tree result = DECL_TEMPLATE_RESULT (tmpl);
2225 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2226 DECL_INITIAL (result) = NULL_TREE;
2227 if (have_def)
2229 tree parm;
2230 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2231 DECL_SOURCE_LOCATION (result)
2232 = DECL_SOURCE_LOCATION (decl);
2233 /* We want to use the argument list specified in the
2234 definition, not in the original declaration. */
2235 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2236 for (parm = DECL_ARGUMENTS (result); parm;
2237 parm = TREE_CHAIN (parm))
2238 DECL_CONTEXT (parm) = result;
2240 return tmpl;
2243 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2244 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2246 /* Inherit default function arguments from the template
2247 DECL is specializing. */
2248 copy_default_args_to_explicit_spec (decl);
2250 /* This specialization has the same protection as the
2251 template it specializes. */
2252 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2253 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2255 /* 7.1.1-1 [dcl.stc]
2257 A storage-class-specifier shall not be specified in an
2258 explicit specialization...
2260 The parser rejects these, so unless action is taken here,
2261 explicit function specializations will always appear with
2262 global linkage.
2264 The action recommended by the C++ CWG in response to C++
2265 defect report 605 is to make the storage class and linkage
2266 of the explicit specialization match the templated function:
2268 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2270 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2272 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2273 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2275 /* This specialization has the same linkage and visibility as
2276 the function template it specializes. */
2277 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2278 if (! TREE_PUBLIC (decl))
2280 DECL_INTERFACE_KNOWN (decl) = 1;
2281 DECL_NOT_REALLY_EXTERN (decl) = 1;
2283 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2284 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2286 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2287 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2291 /* If DECL is a friend declaration, declared using an
2292 unqualified name, the namespace associated with DECL may
2293 have been set incorrectly. For example, in:
2295 template <typename T> void f(T);
2296 namespace N {
2297 struct S { friend void f<int>(int); }
2300 we will have set the DECL_CONTEXT for the friend
2301 declaration to N, rather than to the global namespace. */
2302 if (DECL_NAMESPACE_SCOPE_P (decl))
2303 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2305 if (is_friend && !have_def)
2306 /* This is not really a declaration of a specialization.
2307 It's just the name of an instantiation. But, it's not
2308 a request for an instantiation, either. */
2309 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2310 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2311 /* This is indeed a specialization. In case of constructors
2312 and destructors, we need in-charge and not-in-charge
2313 versions in V3 ABI. */
2314 clone_function_decl (decl, /*update_method_vec_p=*/0);
2316 /* Register this specialization so that we can find it
2317 again. */
2318 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2322 return decl;
2325 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2326 parameters. These are represented in the same format used for
2327 DECL_TEMPLATE_PARMS. */
2330 comp_template_parms (const_tree parms1, const_tree parms2)
2332 const_tree p1;
2333 const_tree p2;
2335 if (parms1 == parms2)
2336 return 1;
2338 for (p1 = parms1, p2 = parms2;
2339 p1 != NULL_TREE && p2 != NULL_TREE;
2340 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2342 tree t1 = TREE_VALUE (p1);
2343 tree t2 = TREE_VALUE (p2);
2344 int i;
2346 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2347 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2349 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2350 return 0;
2352 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2354 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2355 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2357 /* If either of the template parameters are invalid, assume
2358 they match for the sake of error recovery. */
2359 if (parm1 == error_mark_node || parm2 == error_mark_node)
2360 return 1;
2362 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2363 return 0;
2365 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2366 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2367 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2368 continue;
2369 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2370 return 0;
2374 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2375 /* One set of parameters has more parameters lists than the
2376 other. */
2377 return 0;
2379 return 1;
2382 /* Determine whether PARM is a parameter pack. */
2383 bool
2384 template_parameter_pack_p (const_tree parm)
2386 /* Determine if we have a non-type template parameter pack. */
2387 if (TREE_CODE (parm) == PARM_DECL)
2388 return (DECL_TEMPLATE_PARM_P (parm)
2389 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2391 /* If this is a list of template parameters, we could get a
2392 TYPE_DECL or a TEMPLATE_DECL. */
2393 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2394 parm = TREE_TYPE (parm);
2396 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2397 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2398 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2401 /* Determine whether ARGS describes a variadic template args list,
2402 i.e., one that is terminated by a template argument pack. */
2403 static bool
2404 template_args_variadic_p (tree args)
2406 int nargs;
2407 tree last_parm;
2409 if (args == NULL_TREE)
2410 return false;
2412 args = INNERMOST_TEMPLATE_ARGS (args);
2413 nargs = TREE_VEC_LENGTH (args);
2415 if (nargs == 0)
2416 return false;
2418 last_parm = TREE_VEC_ELT (args, nargs - 1);
2420 return ARGUMENT_PACK_P (last_parm);
2423 /* Generate a new name for the parameter pack name NAME (an
2424 IDENTIFIER_NODE) that incorporates its */
2425 static tree
2426 make_ith_pack_parameter_name (tree name, int i)
2428 /* Munge the name to include the parameter index. */
2429 char numbuf[128];
2430 char* newname;
2432 sprintf(numbuf, "%i", i);
2433 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2434 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2435 return get_identifier (newname);
2438 /* Structure used to track the progress of find_parameter_packs_r. */
2439 struct find_parameter_pack_data
2441 /* TREE_LIST that will contain all of the parameter packs found by
2442 the traversal. */
2443 tree* parameter_packs;
2445 /* Set of AST nodes that have been visited by the traversal. */
2446 struct pointer_set_t *visited;
2449 /* Identifies all of the argument packs that occur in a template
2450 argument and appends them to the TREE_LIST inside DATA, which is a
2451 find_parameter_pack_data structure. This is a subroutine of
2452 make_pack_expansion and uses_parameter_packs. */
2453 static tree
2454 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2456 tree t = *tp;
2457 struct find_parameter_pack_data* ppd =
2458 (struct find_parameter_pack_data*)data;
2459 bool parameter_pack_p = false;
2461 /* Identify whether this is a parameter pack or not. */
2462 switch (TREE_CODE (t))
2464 case TEMPLATE_PARM_INDEX:
2465 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2466 parameter_pack_p = true;
2467 break;
2469 case TEMPLATE_TYPE_PARM:
2470 case TEMPLATE_TEMPLATE_PARM:
2471 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2472 parameter_pack_p = true;
2473 break;
2475 case PARM_DECL:
2476 if (FUNCTION_PARAMETER_PACK_P (t))
2478 /* We don't want to walk into the type of a PARM_DECL,
2479 because we don't want to see the type parameter pack. */
2480 *walk_subtrees = 0;
2481 parameter_pack_p = true;
2483 break;
2485 default:
2486 /* Not a parameter pack. */
2487 break;
2490 if (parameter_pack_p)
2492 /* Add this parameter pack to the list. */
2493 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2496 if (TYPE_P (t))
2497 cp_walk_tree (&TYPE_CONTEXT (t),
2498 &find_parameter_packs_r, ppd, ppd->visited);
2500 /* This switch statement will return immediately if we don't find a
2501 parameter pack. */
2502 switch (TREE_CODE (t))
2504 case TEMPLATE_PARM_INDEX:
2505 return NULL_TREE;
2507 case BOUND_TEMPLATE_TEMPLATE_PARM:
2508 /* Check the template itself. */
2509 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2510 &find_parameter_packs_r, ppd, ppd->visited);
2511 /* Check the template arguments. */
2512 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2513 ppd->visited);
2514 *walk_subtrees = 0;
2515 return NULL_TREE;
2517 case TEMPLATE_TYPE_PARM:
2518 case TEMPLATE_TEMPLATE_PARM:
2519 return NULL_TREE;
2521 case PARM_DECL:
2522 return NULL_TREE;
2524 case RECORD_TYPE:
2525 if (TYPE_PTRMEMFUNC_P (t))
2526 return NULL_TREE;
2527 /* Fall through. */
2529 case UNION_TYPE:
2530 case ENUMERAL_TYPE:
2531 if (TYPE_TEMPLATE_INFO (t))
2532 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2533 &find_parameter_packs_r, ppd, ppd->visited);
2535 *walk_subtrees = 0;
2536 return NULL_TREE;
2538 case TEMPLATE_DECL:
2539 cp_walk_tree (&TREE_TYPE (t),
2540 &find_parameter_packs_r, ppd, ppd->visited);
2541 return NULL_TREE;
2543 case TYPENAME_TYPE:
2544 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2545 ppd, ppd->visited);
2546 *walk_subtrees = 0;
2547 return NULL_TREE;
2549 case TYPE_PACK_EXPANSION:
2550 case EXPR_PACK_EXPANSION:
2551 *walk_subtrees = 0;
2552 return NULL_TREE;
2554 case INTEGER_TYPE:
2555 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2556 ppd, ppd->visited);
2557 *walk_subtrees = 0;
2558 return NULL_TREE;
2560 case IDENTIFIER_NODE:
2561 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2562 ppd->visited);
2563 *walk_subtrees = 0;
2564 return NULL_TREE;
2566 default:
2567 return NULL_TREE;
2570 return NULL_TREE;
2573 /* Determines if the expression or type T uses any parameter packs. */
2574 bool
2575 uses_parameter_packs (tree t)
2577 tree parameter_packs = NULL_TREE;
2578 struct find_parameter_pack_data ppd;
2579 ppd.parameter_packs = &parameter_packs;
2580 ppd.visited = pointer_set_create ();
2581 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2582 pointer_set_destroy (ppd.visited);
2583 return parameter_packs != NULL_TREE;
2586 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2587 representation a base-class initializer into a parameter pack
2588 expansion. If all goes well, the resulting node will be an
2589 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2590 respectively. */
2591 tree
2592 make_pack_expansion (tree arg)
2594 tree result;
2595 tree parameter_packs = NULL_TREE;
2596 bool for_types = false;
2597 struct find_parameter_pack_data ppd;
2599 if (!arg || arg == error_mark_node)
2600 return arg;
2602 if (TREE_CODE (arg) == TREE_LIST)
2604 /* The only time we will see a TREE_LIST here is for a base
2605 class initializer. In this case, the TREE_PURPOSE will be a
2606 _TYPE node (representing the base class expansion we're
2607 initializing) and the TREE_VALUE will be a TREE_LIST
2608 containing the initialization arguments.
2610 The resulting expansion looks somewhat different from most
2611 expansions. Rather than returning just one _EXPANSION, we
2612 return a TREE_LIST whose TREE_PURPOSE is a
2613 TYPE_PACK_EXPANSION containing the bases that will be
2614 initialized. The TREE_VALUE will be identical to the
2615 original TREE_VALUE, which is a list of arguments that will
2616 be passed to each base. We do not introduce any new pack
2617 expansion nodes into the TREE_VALUE (although it is possible
2618 that some already exist), because the TREE_PURPOSE and
2619 TREE_VALUE all need to be expanded together with the same
2620 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2621 resulting TREE_PURPOSE will mention the parameter packs in
2622 both the bases and the arguments to the bases. */
2623 tree purpose;
2624 tree value;
2625 tree parameter_packs = NULL_TREE;
2627 /* Determine which parameter packs will be used by the base
2628 class expansion. */
2629 ppd.visited = pointer_set_create ();
2630 ppd.parameter_packs = &parameter_packs;
2631 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2632 &ppd, ppd.visited);
2634 if (parameter_packs == NULL_TREE)
2636 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2637 pointer_set_destroy (ppd.visited);
2638 return error_mark_node;
2641 if (TREE_VALUE (arg) != void_type_node)
2643 /* Collect the sets of parameter packs used in each of the
2644 initialization arguments. */
2645 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2647 /* Determine which parameter packs will be expanded in this
2648 argument. */
2649 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2650 &ppd, ppd.visited);
2654 pointer_set_destroy (ppd.visited);
2656 /* Create the pack expansion type for the base type. */
2657 purpose = make_node (TYPE_PACK_EXPANSION);
2658 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2659 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2661 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2662 they will rarely be compared to anything. */
2663 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2665 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2668 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2669 for_types = true;
2671 /* Build the PACK_EXPANSION_* node. */
2672 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2673 SET_PACK_EXPANSION_PATTERN (result, arg);
2674 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2676 /* Propagate type and const-expression information. */
2677 TREE_TYPE (result) = TREE_TYPE (arg);
2678 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2680 else
2681 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2682 they will rarely be compared to anything. */
2683 SET_TYPE_STRUCTURAL_EQUALITY (result);
2685 /* Determine which parameter packs will be expanded. */
2686 ppd.parameter_packs = &parameter_packs;
2687 ppd.visited = pointer_set_create ();
2688 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2689 pointer_set_destroy (ppd.visited);
2691 /* Make sure we found some parameter packs. */
2692 if (parameter_packs == NULL_TREE)
2694 if (TYPE_P (arg))
2695 error ("expansion pattern %<%T%> contains no argument packs", arg);
2696 else
2697 error ("expansion pattern %<%E%> contains no argument packs", arg);
2698 return error_mark_node;
2700 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2702 return result;
2705 /* Checks T for any "bare" parameter packs, which have not yet been
2706 expanded, and issues an error if any are found. This operation can
2707 only be done on full expressions or types (e.g., an expression
2708 statement, "if" condition, etc.), because we could have expressions like:
2710 foo(f(g(h(args)))...)
2712 where "args" is a parameter pack. check_for_bare_parameter_packs
2713 should not be called for the subexpressions args, h(args),
2714 g(h(args)), or f(g(h(args))), because we would produce erroneous
2715 error messages.
2717 Returns TRUE and emits an error if there were bare parameter packs,
2718 returns FALSE otherwise. */
2719 bool
2720 check_for_bare_parameter_packs (tree t)
2722 tree parameter_packs = NULL_TREE;
2723 struct find_parameter_pack_data ppd;
2725 if (!processing_template_decl || !t || t == error_mark_node)
2726 return false;
2728 if (TREE_CODE (t) == TYPE_DECL)
2729 t = TREE_TYPE (t);
2731 ppd.parameter_packs = &parameter_packs;
2732 ppd.visited = pointer_set_create ();
2733 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2734 pointer_set_destroy (ppd.visited);
2736 if (parameter_packs)
2738 error ("parameter packs not expanded with %<...%>:");
2739 while (parameter_packs)
2741 tree pack = TREE_VALUE (parameter_packs);
2742 tree name = NULL_TREE;
2744 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2745 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2746 name = TYPE_NAME (pack);
2747 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2748 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2749 else
2750 name = DECL_NAME (pack);
2752 if (name)
2753 inform (input_location, " %qD", name);
2754 else
2755 inform (input_location, " <anonymous>");
2757 parameter_packs = TREE_CHAIN (parameter_packs);
2760 return true;
2763 return false;
2766 /* Expand any parameter packs that occur in the template arguments in
2767 ARGS. */
2768 tree
2769 expand_template_argument_pack (tree args)
2771 tree result_args = NULL_TREE;
2772 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2773 int num_result_args = -1;
2775 /* First, determine if we need to expand anything, and the number of
2776 slots we'll need. */
2777 for (in_arg = 0; in_arg < nargs; ++in_arg)
2779 tree arg = TREE_VEC_ELT (args, in_arg);
2780 if (ARGUMENT_PACK_P (arg))
2782 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2783 if (num_result_args < 0)
2784 num_result_args = in_arg + num_packed;
2785 else
2786 num_result_args += num_packed;
2788 else
2790 if (num_result_args >= 0)
2791 num_result_args++;
2795 /* If no expansion is necessary, we're done. */
2796 if (num_result_args < 0)
2797 return args;
2799 /* Expand arguments. */
2800 result_args = make_tree_vec (num_result_args);
2801 for (in_arg = 0; in_arg < nargs; ++in_arg)
2803 tree arg = TREE_VEC_ELT (args, in_arg);
2804 if (ARGUMENT_PACK_P (arg))
2806 tree packed = ARGUMENT_PACK_ARGS (arg);
2807 int i, num_packed = TREE_VEC_LENGTH (packed);
2808 for (i = 0; i < num_packed; ++i, ++out_arg)
2809 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2811 else
2813 TREE_VEC_ELT (result_args, out_arg) = arg;
2814 ++out_arg;
2818 return result_args;
2821 /* Checks if DECL shadows a template parameter.
2823 [temp.local]: A template-parameter shall not be redeclared within its
2824 scope (including nested scopes).
2826 Emits an error and returns TRUE if the DECL shadows a parameter,
2827 returns FALSE otherwise. */
2829 bool
2830 check_template_shadow (tree decl)
2832 tree olddecl;
2834 /* If we're not in a template, we can't possibly shadow a template
2835 parameter. */
2836 if (!current_template_parms)
2837 return true;
2839 /* Figure out what we're shadowing. */
2840 if (TREE_CODE (decl) == OVERLOAD)
2841 decl = OVL_CURRENT (decl);
2842 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2844 /* If there's no previous binding for this name, we're not shadowing
2845 anything, let alone a template parameter. */
2846 if (!olddecl)
2847 return true;
2849 /* If we're not shadowing a template parameter, we're done. Note
2850 that OLDDECL might be an OVERLOAD (or perhaps even an
2851 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2852 node. */
2853 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2854 return true;
2856 /* We check for decl != olddecl to avoid bogus errors for using a
2857 name inside a class. We check TPFI to avoid duplicate errors for
2858 inline member templates. */
2859 if (decl == olddecl
2860 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2861 return true;
2863 error ("declaration of %q+#D", decl);
2864 error (" shadows template parm %q+#D", olddecl);
2865 return false;
2868 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2869 ORIG_LEVEL, DECL, and TYPE. */
2871 static tree
2872 build_template_parm_index (int index,
2873 int level,
2874 int orig_level,
2875 tree decl,
2876 tree type)
2878 tree t = make_node (TEMPLATE_PARM_INDEX);
2879 TEMPLATE_PARM_IDX (t) = index;
2880 TEMPLATE_PARM_LEVEL (t) = level;
2881 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2882 TEMPLATE_PARM_DECL (t) = decl;
2883 TREE_TYPE (t) = type;
2884 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2885 TREE_READONLY (t) = TREE_READONLY (decl);
2887 return t;
2890 /* Find the canonical type parameter for the given template type
2891 parameter. Returns the canonical type parameter, which may be TYPE
2892 if no such parameter existed. */
2893 static tree
2894 canonical_type_parameter (tree type)
2896 tree list;
2897 int idx = TEMPLATE_TYPE_IDX (type);
2898 if (!canonical_template_parms)
2899 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2901 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2902 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2904 list = VEC_index (tree, canonical_template_parms, idx);
2905 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2906 list = TREE_CHAIN (list);
2908 if (list)
2909 return TREE_VALUE (list);
2910 else
2912 VEC_replace(tree, canonical_template_parms, idx,
2913 tree_cons (NULL_TREE, type,
2914 VEC_index (tree, canonical_template_parms, idx)));
2915 return type;
2919 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2920 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2921 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2922 new one is created. */
2924 static tree
2925 reduce_template_parm_level (tree index, tree type, int levels, tree args,
2926 tsubst_flags_t complain)
2928 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2929 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2930 != TEMPLATE_PARM_LEVEL (index) - levels))
2932 tree orig_decl = TEMPLATE_PARM_DECL (index);
2933 tree decl, t;
2935 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
2936 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2937 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2938 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2939 DECL_ARTIFICIAL (decl) = 1;
2940 SET_DECL_TEMPLATE_PARM_P (decl);
2942 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2943 TEMPLATE_PARM_LEVEL (index) - levels,
2944 TEMPLATE_PARM_ORIG_LEVEL (index),
2945 decl, type);
2946 TEMPLATE_PARM_DESCENDANTS (index) = t;
2947 TEMPLATE_PARM_PARAMETER_PACK (t)
2948 = TEMPLATE_PARM_PARAMETER_PACK (index);
2950 /* Template template parameters need this. */
2951 if (TREE_CODE (decl) == TEMPLATE_DECL)
2952 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
2953 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
2954 args, complain);
2957 return TEMPLATE_PARM_DESCENDANTS (index);
2960 /* Process information from new template parameter PARM and append it to the
2961 LIST being built. This new parameter is a non-type parameter iff
2962 IS_NON_TYPE is true. This new parameter is a parameter
2963 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
2964 PARM_LOC. */
2966 tree
2967 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
2968 bool is_parameter_pack)
2970 tree decl = 0;
2971 tree defval;
2972 tree err_parm_list;
2973 int idx = 0;
2975 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2976 defval = TREE_PURPOSE (parm);
2978 if (list)
2980 tree p = tree_last (list);
2982 if (p && TREE_VALUE (p) != error_mark_node)
2984 p = TREE_VALUE (p);
2985 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2986 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2987 else
2988 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2991 ++idx;
2993 else
2994 idx = 0;
2996 if (is_non_type)
2998 parm = TREE_VALUE (parm);
3000 SET_DECL_TEMPLATE_PARM_P (parm);
3002 if (TREE_TYPE (parm) == error_mark_node)
3004 err_parm_list = build_tree_list (defval, parm);
3005 TREE_VALUE (err_parm_list) = error_mark_node;
3006 return chainon (list, err_parm_list);
3008 else
3010 /* [temp.param]
3012 The top-level cv-qualifiers on the template-parameter are
3013 ignored when determining its type. */
3014 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3015 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3017 err_parm_list = build_tree_list (defval, parm);
3018 TREE_VALUE (err_parm_list) = error_mark_node;
3019 return chainon (list, err_parm_list);
3022 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3024 /* This template parameter is not a parameter pack, but it
3025 should be. Complain about "bare" parameter packs. */
3026 check_for_bare_parameter_packs (TREE_TYPE (parm));
3028 /* Recover by calling this a parameter pack. */
3029 is_parameter_pack = true;
3033 /* A template parameter is not modifiable. */
3034 TREE_CONSTANT (parm) = 1;
3035 TREE_READONLY (parm) = 1;
3036 decl = build_decl (parm_loc,
3037 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3038 TREE_CONSTANT (decl) = 1;
3039 TREE_READONLY (decl) = 1;
3040 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3041 = build_template_parm_index (idx, processing_template_decl,
3042 processing_template_decl,
3043 decl, TREE_TYPE (parm));
3045 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3046 = is_parameter_pack;
3048 else
3050 tree t;
3051 parm = TREE_VALUE (TREE_VALUE (parm));
3053 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3055 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3056 /* This is for distinguishing between real templates and template
3057 template parameters */
3058 TREE_TYPE (parm) = t;
3059 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3060 decl = parm;
3062 else
3064 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3065 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3066 decl = build_decl (parm_loc,
3067 TYPE_DECL, parm, t);
3070 TYPE_NAME (t) = decl;
3071 TYPE_STUB_DECL (t) = decl;
3072 parm = decl;
3073 TEMPLATE_TYPE_PARM_INDEX (t)
3074 = build_template_parm_index (idx, processing_template_decl,
3075 processing_template_decl,
3076 decl, TREE_TYPE (parm));
3077 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3078 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3080 DECL_ARTIFICIAL (decl) = 1;
3081 SET_DECL_TEMPLATE_PARM_P (decl);
3082 pushdecl (decl);
3083 parm = build_tree_list (defval, parm);
3084 return chainon (list, parm);
3087 /* The end of a template parameter list has been reached. Process the
3088 tree list into a parameter vector, converting each parameter into a more
3089 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3090 as PARM_DECLs. */
3092 tree
3093 end_template_parm_list (tree parms)
3095 int nparms;
3096 tree parm, next;
3097 tree saved_parmlist = make_tree_vec (list_length (parms));
3099 current_template_parms
3100 = tree_cons (size_int (processing_template_decl),
3101 saved_parmlist, current_template_parms);
3103 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3105 next = TREE_CHAIN (parm);
3106 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3107 TREE_CHAIN (parm) = NULL_TREE;
3110 --processing_template_parmlist;
3112 return saved_parmlist;
3115 /* end_template_decl is called after a template declaration is seen. */
3117 void
3118 end_template_decl (void)
3120 reset_specialization ();
3122 if (! processing_template_decl)
3123 return;
3125 /* This matches the pushlevel in begin_template_parm_list. */
3126 finish_scope ();
3128 --processing_template_decl;
3129 current_template_parms = TREE_CHAIN (current_template_parms);
3132 /* Within the declaration of a template, return all levels of template
3133 parameters that apply. The template parameters are represented as
3134 a TREE_VEC, in the form documented in cp-tree.h for template
3135 arguments. */
3137 static tree
3138 current_template_args (void)
3140 tree header;
3141 tree args = NULL_TREE;
3142 int length = TMPL_PARMS_DEPTH (current_template_parms);
3143 int l = length;
3145 /* If there is only one level of template parameters, we do not
3146 create a TREE_VEC of TREE_VECs. Instead, we return a single
3147 TREE_VEC containing the arguments. */
3148 if (length > 1)
3149 args = make_tree_vec (length);
3151 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3153 tree a = copy_node (TREE_VALUE (header));
3154 int i;
3156 TREE_TYPE (a) = NULL_TREE;
3157 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3159 tree t = TREE_VEC_ELT (a, i);
3161 /* T will be a list if we are called from within a
3162 begin/end_template_parm_list pair, but a vector directly
3163 if within a begin/end_member_template_processing pair. */
3164 if (TREE_CODE (t) == TREE_LIST)
3166 t = TREE_VALUE (t);
3168 if (!error_operand_p (t))
3170 if (TREE_CODE (t) == TYPE_DECL
3171 || TREE_CODE (t) == TEMPLATE_DECL)
3173 t = TREE_TYPE (t);
3175 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3177 /* Turn this argument into a TYPE_ARGUMENT_PACK
3178 with a single element, which expands T. */
3179 tree vec = make_tree_vec (1);
3180 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3182 t = make_node (TYPE_ARGUMENT_PACK);
3183 SET_ARGUMENT_PACK_ARGS (t, vec);
3186 else
3188 t = DECL_INITIAL (t);
3190 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3192 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3193 with a single element, which expands T. */
3194 tree vec = make_tree_vec (1);
3195 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3196 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3198 t = make_node (NONTYPE_ARGUMENT_PACK);
3199 SET_ARGUMENT_PACK_ARGS (t, vec);
3200 TREE_TYPE (t) = type;
3203 TREE_VEC_ELT (a, i) = t;
3208 if (length > 1)
3209 TREE_VEC_ELT (args, --l) = a;
3210 else
3211 args = a;
3214 return args;
3217 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3218 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3219 a member template. Used by push_template_decl below. */
3221 static tree
3222 build_template_decl (tree decl, tree parms, bool member_template_p)
3224 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3225 DECL_TEMPLATE_PARMS (tmpl) = parms;
3226 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3227 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3228 if (DECL_LANG_SPECIFIC (decl))
3230 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3231 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3232 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3233 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3234 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3235 if (DECL_OVERLOADED_OPERATOR_P (decl))
3236 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3237 DECL_OVERLOADED_OPERATOR_P (decl));
3240 return tmpl;
3243 struct template_parm_data
3245 /* The level of the template parameters we are currently
3246 processing. */
3247 int level;
3249 /* The index of the specialization argument we are currently
3250 processing. */
3251 int current_arg;
3253 /* An array whose size is the number of template parameters. The
3254 elements are nonzero if the parameter has been used in any one
3255 of the arguments processed so far. */
3256 int* parms;
3258 /* An array whose size is the number of template arguments. The
3259 elements are nonzero if the argument makes use of template
3260 parameters of this level. */
3261 int* arg_uses_template_parms;
3264 /* Subroutine of push_template_decl used to see if each template
3265 parameter in a partial specialization is used in the explicit
3266 argument list. If T is of the LEVEL given in DATA (which is
3267 treated as a template_parm_data*), then DATA->PARMS is marked
3268 appropriately. */
3270 static int
3271 mark_template_parm (tree t, void* data)
3273 int level;
3274 int idx;
3275 struct template_parm_data* tpd = (struct template_parm_data*) data;
3277 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3279 level = TEMPLATE_PARM_LEVEL (t);
3280 idx = TEMPLATE_PARM_IDX (t);
3282 else
3284 level = TEMPLATE_TYPE_LEVEL (t);
3285 idx = TEMPLATE_TYPE_IDX (t);
3288 if (level == tpd->level)
3290 tpd->parms[idx] = 1;
3291 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3294 /* Return zero so that for_each_template_parm will continue the
3295 traversal of the tree; we want to mark *every* template parm. */
3296 return 0;
3299 /* Process the partial specialization DECL. */
3301 static tree
3302 process_partial_specialization (tree decl)
3304 tree type = TREE_TYPE (decl);
3305 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3306 tree specargs = CLASSTYPE_TI_ARGS (type);
3307 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3308 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3309 tree inner_parms;
3310 int nargs = TREE_VEC_LENGTH (inner_args);
3311 int ntparms;
3312 int i;
3313 int did_error_intro = 0;
3314 struct template_parm_data tpd;
3315 struct template_parm_data tpd2;
3317 gcc_assert (current_template_parms);
3319 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3320 ntparms = TREE_VEC_LENGTH (inner_parms);
3322 /* We check that each of the template parameters given in the
3323 partial specialization is used in the argument list to the
3324 specialization. For example:
3326 template <class T> struct S;
3327 template <class T> struct S<T*>;
3329 The second declaration is OK because `T*' uses the template
3330 parameter T, whereas
3332 template <class T> struct S<int>;
3334 is no good. Even trickier is:
3336 template <class T>
3337 struct S1
3339 template <class U>
3340 struct S2;
3341 template <class U>
3342 struct S2<T>;
3345 The S2<T> declaration is actually invalid; it is a
3346 full-specialization. Of course,
3348 template <class U>
3349 struct S2<T (*)(U)>;
3351 or some such would have been OK. */
3352 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3353 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3354 memset (tpd.parms, 0, sizeof (int) * ntparms);
3356 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3357 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3358 for (i = 0; i < nargs; ++i)
3360 tpd.current_arg = i;
3361 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3362 &mark_template_parm,
3363 &tpd,
3364 NULL,
3365 /*include_nondeduced_p=*/false);
3367 for (i = 0; i < ntparms; ++i)
3368 if (tpd.parms[i] == 0)
3370 /* One of the template parms was not used in the
3371 specialization. */
3372 if (!did_error_intro)
3374 error ("template parameters not used in partial specialization:");
3375 did_error_intro = 1;
3378 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3381 /* [temp.class.spec]
3383 The argument list of the specialization shall not be identical to
3384 the implicit argument list of the primary template. */
3385 if (comp_template_args
3386 (inner_args,
3387 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3388 (maintmpl)))))
3389 error ("partial specialization %qT does not specialize any template arguments", type);
3391 /* [temp.class.spec]
3393 A partially specialized non-type argument expression shall not
3394 involve template parameters of the partial specialization except
3395 when the argument expression is a simple identifier.
3397 The type of a template parameter corresponding to a specialized
3398 non-type argument shall not be dependent on a parameter of the
3399 specialization.
3401 Also, we verify that pack expansions only occur at the
3402 end of the argument list. */
3403 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3404 tpd2.parms = 0;
3405 for (i = 0; i < nargs; ++i)
3407 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3408 tree arg = TREE_VEC_ELT (inner_args, i);
3409 tree packed_args = NULL_TREE;
3410 int j, len = 1;
3412 if (ARGUMENT_PACK_P (arg))
3414 /* Extract the arguments from the argument pack. We'll be
3415 iterating over these in the following loop. */
3416 packed_args = ARGUMENT_PACK_ARGS (arg);
3417 len = TREE_VEC_LENGTH (packed_args);
3420 for (j = 0; j < len; j++)
3422 if (packed_args)
3423 /* Get the Jth argument in the parameter pack. */
3424 arg = TREE_VEC_ELT (packed_args, j);
3426 if (PACK_EXPANSION_P (arg))
3428 /* Pack expansions must come at the end of the
3429 argument list. */
3430 if ((packed_args && j < len - 1)
3431 || (!packed_args && i < nargs - 1))
3433 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3434 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3435 else
3436 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3438 if (packed_args)
3439 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3443 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3444 /* We only care about the pattern. */
3445 arg = PACK_EXPANSION_PATTERN (arg);
3447 if (/* These first two lines are the `non-type' bit. */
3448 !TYPE_P (arg)
3449 && TREE_CODE (arg) != TEMPLATE_DECL
3450 /* This next line is the `argument expression is not just a
3451 simple identifier' condition and also the `specialized
3452 non-type argument' bit. */
3453 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3455 if ((!packed_args && tpd.arg_uses_template_parms[i])
3456 || (packed_args && uses_template_parms (arg)))
3457 error ("template argument %qE involves template parameter(s)",
3458 arg);
3459 else
3461 /* Look at the corresponding template parameter,
3462 marking which template parameters its type depends
3463 upon. */
3464 tree type = TREE_TYPE (parm);
3466 if (!tpd2.parms)
3468 /* We haven't yet initialized TPD2. Do so now. */
3469 tpd2.arg_uses_template_parms
3470 = (int *) alloca (sizeof (int) * nargs);
3471 /* The number of parameters here is the number in the
3472 main template, which, as checked in the assertion
3473 above, is NARGS. */
3474 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3475 tpd2.level =
3476 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3479 /* Mark the template parameters. But this time, we're
3480 looking for the template parameters of the main
3481 template, not in the specialization. */
3482 tpd2.current_arg = i;
3483 tpd2.arg_uses_template_parms[i] = 0;
3484 memset (tpd2.parms, 0, sizeof (int) * nargs);
3485 for_each_template_parm (type,
3486 &mark_template_parm,
3487 &tpd2,
3488 NULL,
3489 /*include_nondeduced_p=*/false);
3491 if (tpd2.arg_uses_template_parms [i])
3493 /* The type depended on some template parameters.
3494 If they are fully specialized in the
3495 specialization, that's OK. */
3496 int j;
3497 for (j = 0; j < nargs; ++j)
3498 if (tpd2.parms[j] != 0
3499 && tpd.arg_uses_template_parms [j])
3501 error ("type %qT of template argument %qE depends "
3502 "on template parameter(s)",
3503 type,
3504 arg);
3505 break;
3513 if (retrieve_specialization (maintmpl, specargs,
3514 /*class_specializations_p=*/true))
3515 /* We've already got this specialization. */
3516 return decl;
3518 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3519 = tree_cons (specargs, inner_parms,
3520 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3521 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3522 return decl;
3525 /* Check that a template declaration's use of default arguments and
3526 parameter packs is not invalid. Here, PARMS are the template
3527 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3528 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3529 specialization.
3532 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3533 declaration (but not a definition); 1 indicates a declaration, 2
3534 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3535 emitted for extraneous default arguments.
3537 Returns TRUE if there were no errors found, FALSE otherwise. */
3539 bool
3540 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3541 int is_partial, int is_friend_decl)
3543 const char *msg;
3544 int last_level_to_check;
3545 tree parm_level;
3546 bool no_errors = true;
3548 /* [temp.param]
3550 A default template-argument shall not be specified in a
3551 function template declaration or a function template definition, nor
3552 in the template-parameter-list of the definition of a member of a
3553 class template. */
3555 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3556 /* You can't have a function template declaration in a local
3557 scope, nor you can you define a member of a class template in a
3558 local scope. */
3559 return true;
3561 if (current_class_type
3562 && !TYPE_BEING_DEFINED (current_class_type)
3563 && DECL_LANG_SPECIFIC (decl)
3564 /* If this is either a friend defined in the scope of the class
3565 or a member function. */
3566 && (DECL_FUNCTION_MEMBER_P (decl)
3567 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3568 : DECL_FRIEND_CONTEXT (decl)
3569 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3570 : false)
3571 /* And, if it was a member function, it really was defined in
3572 the scope of the class. */
3573 && (!DECL_FUNCTION_MEMBER_P (decl)
3574 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3575 /* We already checked these parameters when the template was
3576 declared, so there's no need to do it again now. This function
3577 was defined in class scope, but we're processing it's body now
3578 that the class is complete. */
3579 return true;
3581 /* Core issue 226 (C++0x only): the following only applies to class
3582 templates. */
3583 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3585 /* [temp.param]
3587 If a template-parameter has a default template-argument, all
3588 subsequent template-parameters shall have a default
3589 template-argument supplied. */
3590 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3592 tree inner_parms = TREE_VALUE (parm_level);
3593 int ntparms = TREE_VEC_LENGTH (inner_parms);
3594 int seen_def_arg_p = 0;
3595 int i;
3597 for (i = 0; i < ntparms; ++i)
3599 tree parm = TREE_VEC_ELT (inner_parms, i);
3601 if (parm == error_mark_node)
3602 continue;
3604 if (TREE_PURPOSE (parm))
3605 seen_def_arg_p = 1;
3606 else if (seen_def_arg_p
3607 && !template_parameter_pack_p (TREE_VALUE (parm)))
3609 error ("no default argument for %qD", TREE_VALUE (parm));
3610 /* For better subsequent error-recovery, we indicate that
3611 there should have been a default argument. */
3612 TREE_PURPOSE (parm) = error_mark_node;
3613 no_errors = false;
3615 else if (is_primary
3616 && !is_partial
3617 && !is_friend_decl
3618 /* Don't complain about an enclosing partial
3619 specialization. */
3620 && parm_level == parms
3621 && TREE_CODE (decl) == TYPE_DECL
3622 && i < ntparms - 1
3623 && template_parameter_pack_p (TREE_VALUE (parm)))
3625 /* A primary class template can only have one
3626 parameter pack, at the end of the template
3627 parameter list. */
3629 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3630 error ("parameter pack %qE must be at the end of the"
3631 " template parameter list", TREE_VALUE (parm));
3632 else
3633 error ("parameter pack %qT must be at the end of the"
3634 " template parameter list",
3635 TREE_TYPE (TREE_VALUE (parm)));
3637 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3638 = error_mark_node;
3639 no_errors = false;
3645 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3646 || is_partial
3647 || !is_primary
3648 || is_friend_decl)
3649 /* For an ordinary class template, default template arguments are
3650 allowed at the innermost level, e.g.:
3651 template <class T = int>
3652 struct S {};
3653 but, in a partial specialization, they're not allowed even
3654 there, as we have in [temp.class.spec]:
3656 The template parameter list of a specialization shall not
3657 contain default template argument values.
3659 So, for a partial specialization, or for a function template
3660 (in C++98/C++03), we look at all of them. */
3662 else
3663 /* But, for a primary class template that is not a partial
3664 specialization we look at all template parameters except the
3665 innermost ones. */
3666 parms = TREE_CHAIN (parms);
3668 /* Figure out what error message to issue. */
3669 if (is_friend_decl == 2)
3670 msg = "default template arguments may not be used in function template friend re-declaration";
3671 else if (is_friend_decl)
3672 msg = "default template arguments may not be used in function template friend declarations";
3673 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3674 msg = "default template arguments may not be used in function templates";
3675 else if (is_partial)
3676 msg = "default template arguments may not be used in partial specializations";
3677 else
3678 msg = "default argument for template parameter for class enclosing %qD";
3680 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3681 /* If we're inside a class definition, there's no need to
3682 examine the parameters to the class itself. On the one
3683 hand, they will be checked when the class is defined, and,
3684 on the other, default arguments are valid in things like:
3685 template <class T = double>
3686 struct S { template <class U> void f(U); };
3687 Here the default argument for `S' has no bearing on the
3688 declaration of `f'. */
3689 last_level_to_check = template_class_depth (current_class_type) + 1;
3690 else
3691 /* Check everything. */
3692 last_level_to_check = 0;
3694 for (parm_level = parms;
3695 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3696 parm_level = TREE_CHAIN (parm_level))
3698 tree inner_parms = TREE_VALUE (parm_level);
3699 int i;
3700 int ntparms;
3702 ntparms = TREE_VEC_LENGTH (inner_parms);
3703 for (i = 0; i < ntparms; ++i)
3705 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3706 continue;
3708 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3710 if (msg)
3712 no_errors = false;
3713 if (is_friend_decl == 2)
3714 return no_errors;
3716 error (msg, decl);
3717 msg = 0;
3720 /* Clear out the default argument so that we are not
3721 confused later. */
3722 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3726 /* At this point, if we're still interested in issuing messages,
3727 they must apply to classes surrounding the object declared. */
3728 if (msg)
3729 msg = "default argument for template parameter for class enclosing %qD";
3732 return no_errors;
3735 /* Worker for push_template_decl_real, called via
3736 for_each_template_parm. DATA is really an int, indicating the
3737 level of the parameters we are interested in. If T is a template
3738 parameter of that level, return nonzero. */
3740 static int
3741 template_parm_this_level_p (tree t, void* data)
3743 int this_level = *(int *)data;
3744 int level;
3746 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3747 level = TEMPLATE_PARM_LEVEL (t);
3748 else
3749 level = TEMPLATE_TYPE_LEVEL (t);
3750 return level == this_level;
3753 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3754 parameters given by current_template_args, or reuses a
3755 previously existing one, if appropriate. Returns the DECL, or an
3756 equivalent one, if it is replaced via a call to duplicate_decls.
3758 If IS_FRIEND is true, DECL is a friend declaration. */
3760 tree
3761 push_template_decl_real (tree decl, bool is_friend)
3763 tree tmpl;
3764 tree args;
3765 tree info;
3766 tree ctx;
3767 int primary;
3768 int is_partial;
3769 int new_template_p = 0;
3770 /* True if the template is a member template, in the sense of
3771 [temp.mem]. */
3772 bool member_template_p = false;
3774 if (decl == error_mark_node || !current_template_parms)
3775 return error_mark_node;
3777 /* See if this is a partial specialization. */
3778 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3779 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3780 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3782 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3783 is_friend = true;
3785 if (is_friend)
3786 /* For a friend, we want the context of the friend function, not
3787 the type of which it is a friend. */
3788 ctx = DECL_CONTEXT (decl);
3789 else if (CP_DECL_CONTEXT (decl)
3790 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3791 /* In the case of a virtual function, we want the class in which
3792 it is defined. */
3793 ctx = CP_DECL_CONTEXT (decl);
3794 else
3795 /* Otherwise, if we're currently defining some class, the DECL
3796 is assumed to be a member of the class. */
3797 ctx = current_scope ();
3799 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3800 ctx = NULL_TREE;
3802 if (!DECL_CONTEXT (decl))
3803 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3805 /* See if this is a primary template. */
3806 if (is_friend && ctx)
3807 /* A friend template that specifies a class context, i.e.
3808 template <typename T> friend void A<T>::f();
3809 is not primary. */
3810 primary = 0;
3811 else
3812 primary = template_parm_scope_p ();
3814 if (primary)
3816 if (DECL_CLASS_SCOPE_P (decl))
3817 member_template_p = true;
3818 if (TREE_CODE (decl) == TYPE_DECL
3819 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3821 error ("template class without a name");
3822 return error_mark_node;
3824 else if (TREE_CODE (decl) == FUNCTION_DECL)
3826 if (DECL_DESTRUCTOR_P (decl))
3828 /* [temp.mem]
3830 A destructor shall not be a member template. */
3831 error ("destructor %qD declared as member template", decl);
3832 return error_mark_node;
3834 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3835 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3836 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3837 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3838 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3839 == void_list_node)))
3841 /* [basic.stc.dynamic.allocation]
3843 An allocation function can be a function
3844 template. ... Template allocation functions shall
3845 have two or more parameters. */
3846 error ("invalid template declaration of %qD", decl);
3847 return error_mark_node;
3850 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3851 && CLASS_TYPE_P (TREE_TYPE (decl)))
3852 /* OK */;
3853 else
3855 error ("template declaration of %q#D", decl);
3856 return error_mark_node;
3860 /* Check to see that the rules regarding the use of default
3861 arguments are not being violated. */
3862 check_default_tmpl_args (decl, current_template_parms,
3863 primary, is_partial, /*is_friend_decl=*/0);
3865 /* Ensure that there are no parameter packs in the type of this
3866 declaration that have not been expanded. */
3867 if (TREE_CODE (decl) == FUNCTION_DECL)
3869 /* Check each of the arguments individually to see if there are
3870 any bare parameter packs. */
3871 tree type = TREE_TYPE (decl);
3872 tree arg = DECL_ARGUMENTS (decl);
3873 tree argtype = TYPE_ARG_TYPES (type);
3875 while (arg && argtype)
3877 if (!FUNCTION_PARAMETER_PACK_P (arg)
3878 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
3880 /* This is a PARM_DECL that contains unexpanded parameter
3881 packs. We have already complained about this in the
3882 check_for_bare_parameter_packs call, so just replace
3883 these types with ERROR_MARK_NODE. */
3884 TREE_TYPE (arg) = error_mark_node;
3885 TREE_VALUE (argtype) = error_mark_node;
3888 arg = TREE_CHAIN (arg);
3889 argtype = TREE_CHAIN (argtype);
3892 /* Check for bare parameter packs in the return type and the
3893 exception specifiers. */
3894 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
3895 /* Errors were already issued, set return type to int
3896 as the frontend doesn't expect error_mark_node as
3897 the return type. */
3898 TREE_TYPE (type) = integer_type_node;
3899 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
3900 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
3902 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3904 TREE_TYPE (decl) = error_mark_node;
3905 return error_mark_node;
3908 if (is_partial)
3909 return process_partial_specialization (decl);
3911 args = current_template_args ();
3913 if (!ctx
3914 || TREE_CODE (ctx) == FUNCTION_DECL
3915 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3916 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3918 if (DECL_LANG_SPECIFIC (decl)
3919 && DECL_TEMPLATE_INFO (decl)
3920 && DECL_TI_TEMPLATE (decl))
3921 tmpl = DECL_TI_TEMPLATE (decl);
3922 /* If DECL is a TYPE_DECL for a class-template, then there won't
3923 be DECL_LANG_SPECIFIC. The information equivalent to
3924 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3925 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3926 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3927 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3929 /* Since a template declaration already existed for this
3930 class-type, we must be redeclaring it here. Make sure
3931 that the redeclaration is valid. */
3932 redeclare_class_template (TREE_TYPE (decl),
3933 current_template_parms);
3934 /* We don't need to create a new TEMPLATE_DECL; just use the
3935 one we already had. */
3936 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3938 else
3940 tmpl = build_template_decl (decl, current_template_parms,
3941 member_template_p);
3942 new_template_p = 1;
3944 if (DECL_LANG_SPECIFIC (decl)
3945 && DECL_TEMPLATE_SPECIALIZATION (decl))
3947 /* A specialization of a member template of a template
3948 class. */
3949 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3950 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3951 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3955 else
3957 tree a, t, current, parms;
3958 int i;
3959 tree tinfo = get_template_info (decl);
3961 if (!tinfo)
3963 error ("template definition of non-template %q#D", decl);
3964 return error_mark_node;
3967 tmpl = TI_TEMPLATE (tinfo);
3969 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3970 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3971 && DECL_TEMPLATE_SPECIALIZATION (decl)
3972 && DECL_MEMBER_TEMPLATE_P (tmpl))
3974 tree new_tmpl;
3976 /* The declaration is a specialization of a member
3977 template, declared outside the class. Therefore, the
3978 innermost template arguments will be NULL, so we
3979 replace them with the arguments determined by the
3980 earlier call to check_explicit_specialization. */
3981 args = DECL_TI_ARGS (decl);
3983 new_tmpl
3984 = build_template_decl (decl, current_template_parms,
3985 member_template_p);
3986 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3987 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3988 DECL_TI_TEMPLATE (decl) = new_tmpl;
3989 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3990 DECL_TEMPLATE_INFO (new_tmpl)
3991 = tree_cons (tmpl, args, NULL_TREE);
3993 register_specialization (new_tmpl,
3994 most_general_template (tmpl),
3995 args,
3996 is_friend);
3997 return decl;
4000 /* Make sure the template headers we got make sense. */
4002 parms = DECL_TEMPLATE_PARMS (tmpl);
4003 i = TMPL_PARMS_DEPTH (parms);
4004 if (TMPL_ARGS_DEPTH (args) != i)
4006 error ("expected %d levels of template parms for %q#D, got %d",
4007 i, decl, TMPL_ARGS_DEPTH (args));
4009 else
4010 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4012 a = TMPL_ARGS_LEVEL (args, i);
4013 t = INNERMOST_TEMPLATE_PARMS (parms);
4015 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4017 if (current == decl)
4018 error ("got %d template parameters for %q#D",
4019 TREE_VEC_LENGTH (a), decl);
4020 else
4021 error ("got %d template parameters for %q#T",
4022 TREE_VEC_LENGTH (a), current);
4023 error (" but %d required", TREE_VEC_LENGTH (t));
4024 return error_mark_node;
4027 if (current == decl)
4028 current = ctx;
4029 else
4030 current = (TYPE_P (current)
4031 ? TYPE_CONTEXT (current)
4032 : DECL_CONTEXT (current));
4035 /* Check that the parms are used in the appropriate qualifying scopes
4036 in the declarator. */
4037 if (!comp_template_args
4038 (TI_ARGS (tinfo),
4039 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4041 error ("\
4042 template arguments to %qD do not match original template %qD",
4043 decl, DECL_TEMPLATE_RESULT (tmpl));
4044 if (!uses_template_parms (TI_ARGS (tinfo)))
4045 inform (input_location, "use template<> for an explicit specialization");
4046 /* Avoid crash in import_export_decl. */
4047 DECL_INTERFACE_KNOWN (decl) = 1;
4048 return error_mark_node;
4052 DECL_TEMPLATE_RESULT (tmpl) = decl;
4053 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4055 /* Push template declarations for global functions and types. Note
4056 that we do not try to push a global template friend declared in a
4057 template class; such a thing may well depend on the template
4058 parameters of the class. */
4059 if (new_template_p && !ctx
4060 && !(is_friend && template_class_depth (current_class_type) > 0))
4062 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4063 if (tmpl == error_mark_node)
4064 return error_mark_node;
4066 /* Hide template friend classes that haven't been declared yet. */
4067 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4069 DECL_ANTICIPATED (tmpl) = 1;
4070 DECL_FRIEND_P (tmpl) = 1;
4074 if (primary)
4076 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4077 int i;
4079 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4080 if (DECL_CONV_FN_P (tmpl))
4082 int depth = TMPL_PARMS_DEPTH (parms);
4084 /* It is a conversion operator. See if the type converted to
4085 depends on innermost template operands. */
4087 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4088 depth))
4089 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4092 /* Give template template parms a DECL_CONTEXT of the template
4093 for which they are a parameter. */
4094 parms = INNERMOST_TEMPLATE_PARMS (parms);
4095 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4097 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4098 if (TREE_CODE (parm) == TEMPLATE_DECL)
4099 DECL_CONTEXT (parm) = tmpl;
4103 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4104 back to its most general template. If TMPL is a specialization,
4105 ARGS may only have the innermost set of arguments. Add the missing
4106 argument levels if necessary. */
4107 if (DECL_TEMPLATE_INFO (tmpl))
4108 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4110 info = tree_cons (tmpl, args, NULL_TREE);
4112 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4113 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4114 else if (DECL_LANG_SPECIFIC (decl))
4115 DECL_TEMPLATE_INFO (decl) = info;
4117 return DECL_TEMPLATE_RESULT (tmpl);
4120 tree
4121 push_template_decl (tree decl)
4123 return push_template_decl_real (decl, false);
4126 /* Called when a class template TYPE is redeclared with the indicated
4127 template PARMS, e.g.:
4129 template <class T> struct S;
4130 template <class T> struct S {}; */
4132 bool
4133 redeclare_class_template (tree type, tree parms)
4135 tree tmpl;
4136 tree tmpl_parms;
4137 int i;
4139 if (!TYPE_TEMPLATE_INFO (type))
4141 error ("%qT is not a template type", type);
4142 return false;
4145 tmpl = TYPE_TI_TEMPLATE (type);
4146 if (!PRIMARY_TEMPLATE_P (tmpl))
4147 /* The type is nested in some template class. Nothing to worry
4148 about here; there are no new template parameters for the nested
4149 type. */
4150 return true;
4152 if (!parms)
4154 error ("template specifiers not specified in declaration of %qD",
4155 tmpl);
4156 return false;
4159 parms = INNERMOST_TEMPLATE_PARMS (parms);
4160 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4162 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4164 error ("redeclared with %d template parameter(s)",
4165 TREE_VEC_LENGTH (parms));
4166 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4167 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4168 return false;
4171 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4173 tree tmpl_parm;
4174 tree parm;
4175 tree tmpl_default;
4176 tree parm_default;
4178 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4179 || TREE_VEC_ELT (parms, i) == error_mark_node)
4180 continue;
4182 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4183 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4184 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4185 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4187 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4188 TEMPLATE_DECL. */
4189 if (tmpl_parm != error_mark_node
4190 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4191 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4192 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4193 || (TREE_CODE (tmpl_parm) != PARM_DECL
4194 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4195 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4196 || (TREE_CODE (tmpl_parm) == PARM_DECL
4197 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4198 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4200 error ("template parameter %q+#D", tmpl_parm);
4201 error ("redeclared here as %q#D", parm);
4202 return false;
4205 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4207 /* We have in [temp.param]:
4209 A template-parameter may not be given default arguments
4210 by two different declarations in the same scope. */
4211 error ("redefinition of default argument for %q#D", parm);
4212 inform (input_location, "%Joriginal definition appeared here", tmpl_parm);
4213 return false;
4216 if (parm_default != NULL_TREE)
4217 /* Update the previous template parameters (which are the ones
4218 that will really count) with the new default value. */
4219 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4220 else if (tmpl_default != NULL_TREE)
4221 /* Update the new parameters, too; they'll be used as the
4222 parameters for any members. */
4223 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4226 return true;
4229 /* Simplify EXPR if it is a non-dependent expression. Returns the
4230 (possibly simplified) expression. */
4232 tree
4233 fold_non_dependent_expr (tree expr)
4235 if (expr == NULL_TREE)
4236 return NULL_TREE;
4238 /* If we're in a template, but EXPR isn't value dependent, simplify
4239 it. We're supposed to treat:
4241 template <typename T> void f(T[1 + 1]);
4242 template <typename T> void f(T[2]);
4244 as two declarations of the same function, for example. */
4245 if (processing_template_decl
4246 && !type_dependent_expression_p (expr)
4247 && !value_dependent_expression_p (expr))
4249 HOST_WIDE_INT saved_processing_template_decl;
4251 saved_processing_template_decl = processing_template_decl;
4252 processing_template_decl = 0;
4253 expr = tsubst_copy_and_build (expr,
4254 /*args=*/NULL_TREE,
4255 tf_error,
4256 /*in_decl=*/NULL_TREE,
4257 /*function_p=*/false,
4258 /*integral_constant_expression_p=*/true);
4259 processing_template_decl = saved_processing_template_decl;
4261 return expr;
4264 /* EXPR is an expression which is used in a constant-expression context.
4265 For instance, it could be a VAR_DECL with a constant initializer.
4266 Extract the innermost constant expression.
4268 This is basically a more powerful version of
4269 integral_constant_value, which can be used also in templates where
4270 initializers can maintain a syntactic rather than semantic form
4271 (even if they are non-dependent, for access-checking purposes). */
4273 static tree
4274 fold_decl_constant_value (tree expr)
4276 tree const_expr = expr;
4279 expr = fold_non_dependent_expr (const_expr);
4280 const_expr = integral_constant_value (expr);
4282 while (expr != const_expr);
4284 return expr;
4287 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4288 must be a function or a pointer-to-function type, as specified
4289 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4290 and check that the resulting function has external linkage. */
4292 static tree
4293 convert_nontype_argument_function (tree type, tree expr)
4295 tree fns = expr;
4296 tree fn, fn_no_ptr;
4298 fn = instantiate_type (type, fns, tf_none);
4299 if (fn == error_mark_node)
4300 return error_mark_node;
4302 fn_no_ptr = fn;
4303 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4304 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4305 if (TREE_CODE (fn_no_ptr) == BASELINK)
4306 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4308 /* [temp.arg.nontype]/1
4310 A template-argument for a non-type, non-template template-parameter
4311 shall be one of:
4312 [...]
4313 -- the address of an object or function with external linkage. */
4314 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4316 error ("%qE is not a valid template argument for type %qT "
4317 "because function %qD has not external linkage",
4318 expr, type, fn_no_ptr);
4319 return NULL_TREE;
4322 return fn;
4325 /* Attempt to convert the non-type template parameter EXPR to the
4326 indicated TYPE. If the conversion is successful, return the
4327 converted value. If the conversion is unsuccessful, return
4328 NULL_TREE if we issued an error message, or error_mark_node if we
4329 did not. We issue error messages for out-and-out bad template
4330 parameters, but not simply because the conversion failed, since we
4331 might be just trying to do argument deduction. Both TYPE and EXPR
4332 must be non-dependent.
4334 The conversion follows the special rules described in
4335 [temp.arg.nontype], and it is much more strict than an implicit
4336 conversion.
4338 This function is called twice for each template argument (see
4339 lookup_template_class for a more accurate description of this
4340 problem). This means that we need to handle expressions which
4341 are not valid in a C++ source, but can be created from the
4342 first call (for instance, casts to perform conversions). These
4343 hacks can go away after we fix the double coercion problem. */
4345 static tree
4346 convert_nontype_argument (tree type, tree expr)
4348 tree expr_type;
4350 /* Detect immediately string literals as invalid non-type argument.
4351 This special-case is not needed for correctness (we would easily
4352 catch this later), but only to provide better diagnostic for this
4353 common user mistake. As suggested by DR 100, we do not mention
4354 linkage issues in the diagnostic as this is not the point. */
4355 if (TREE_CODE (expr) == STRING_CST)
4357 error ("%qE is not a valid template argument for type %qT "
4358 "because string literals can never be used in this context",
4359 expr, type);
4360 return NULL_TREE;
4363 /* If we are in a template, EXPR may be non-dependent, but still
4364 have a syntactic, rather than semantic, form. For example, EXPR
4365 might be a SCOPE_REF, rather than the VAR_DECL to which the
4366 SCOPE_REF refers. Preserving the qualifying scope is necessary
4367 so that access checking can be performed when the template is
4368 instantiated -- but here we need the resolved form so that we can
4369 convert the argument. */
4370 expr = fold_non_dependent_expr (expr);
4371 if (error_operand_p (expr))
4372 return error_mark_node;
4373 expr_type = TREE_TYPE (expr);
4375 /* HACK: Due to double coercion, we can get a
4376 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4377 which is the tree that we built on the first call (see
4378 below when coercing to reference to object or to reference to
4379 function). We just strip everything and get to the arg.
4380 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4381 for examples. */
4382 if (TREE_CODE (expr) == NOP_EXPR)
4384 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4386 /* ??? Maybe we could use convert_from_reference here, but we
4387 would need to relax its constraints because the NOP_EXPR
4388 could actually change the type to something more cv-qualified,
4389 and this is not folded by convert_from_reference. */
4390 tree addr = TREE_OPERAND (expr, 0);
4391 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4392 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4393 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4394 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4395 (TREE_TYPE (expr_type),
4396 TREE_TYPE (TREE_TYPE (addr))));
4398 expr = TREE_OPERAND (addr, 0);
4399 expr_type = TREE_TYPE (expr);
4402 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4403 parameter is a pointer to object, through decay and
4404 qualification conversion. Let's strip everything. */
4405 else if (TYPE_PTROBV_P (type))
4407 STRIP_NOPS (expr);
4408 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4409 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4410 /* Skip the ADDR_EXPR only if it is part of the decay for
4411 an array. Otherwise, it is part of the original argument
4412 in the source code. */
4413 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4414 expr = TREE_OPERAND (expr, 0);
4415 expr_type = TREE_TYPE (expr);
4419 /* [temp.arg.nontype]/5, bullet 1
4421 For a non-type template-parameter of integral or enumeration type,
4422 integral promotions (_conv.prom_) and integral conversions
4423 (_conv.integral_) are applied. */
4424 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4426 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4427 return error_mark_node;
4429 expr = fold_decl_constant_value (expr);
4430 /* Notice that there are constant expressions like '4 % 0' which
4431 do not fold into integer constants. */
4432 if (TREE_CODE (expr) != INTEGER_CST)
4434 error ("%qE is not a valid template argument for type %qT "
4435 "because it is a non-constant expression", expr, type);
4436 return NULL_TREE;
4439 /* At this point, an implicit conversion does what we want,
4440 because we already know that the expression is of integral
4441 type. */
4442 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4443 if (expr == error_mark_node)
4444 return error_mark_node;
4446 /* Conversion was allowed: fold it to a bare integer constant. */
4447 expr = fold (expr);
4449 /* [temp.arg.nontype]/5, bullet 2
4451 For a non-type template-parameter of type pointer to object,
4452 qualification conversions (_conv.qual_) and the array-to-pointer
4453 conversion (_conv.array_) are applied. */
4454 else if (TYPE_PTROBV_P (type))
4456 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4458 A template-argument for a non-type, non-template template-parameter
4459 shall be one of: [...]
4461 -- the name of a non-type template-parameter;
4462 -- the address of an object or function with external linkage, [...]
4463 expressed as "& id-expression" where the & is optional if the name
4464 refers to a function or array, or if the corresponding
4465 template-parameter is a reference.
4467 Here, we do not care about functions, as they are invalid anyway
4468 for a parameter of type pointer-to-object. */
4470 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4471 /* Non-type template parameters are OK. */
4473 else if (TREE_CODE (expr) != ADDR_EXPR
4474 && TREE_CODE (expr_type) != ARRAY_TYPE)
4476 if (TREE_CODE (expr) == VAR_DECL)
4478 error ("%qD is not a valid template argument "
4479 "because %qD is a variable, not the address of "
4480 "a variable",
4481 expr, expr);
4482 return NULL_TREE;
4484 /* Other values, like integer constants, might be valid
4485 non-type arguments of some other type. */
4486 return error_mark_node;
4488 else
4490 tree decl;
4492 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4493 ? TREE_OPERAND (expr, 0) : expr);
4494 if (TREE_CODE (decl) != VAR_DECL)
4496 error ("%qE is not a valid template argument of type %qT "
4497 "because %qE is not a variable",
4498 expr, type, decl);
4499 return NULL_TREE;
4501 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4503 error ("%qE is not a valid template argument of type %qT "
4504 "because %qD does not have external linkage",
4505 expr, type, decl);
4506 return NULL_TREE;
4510 expr = decay_conversion (expr);
4511 if (expr == error_mark_node)
4512 return error_mark_node;
4514 expr = perform_qualification_conversions (type, expr);
4515 if (expr == error_mark_node)
4516 return error_mark_node;
4518 /* [temp.arg.nontype]/5, bullet 3
4520 For a non-type template-parameter of type reference to object, no
4521 conversions apply. The type referred to by the reference may be more
4522 cv-qualified than the (otherwise identical) type of the
4523 template-argument. The template-parameter is bound directly to the
4524 template-argument, which must be an lvalue. */
4525 else if (TYPE_REF_OBJ_P (type))
4527 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4528 expr_type))
4529 return error_mark_node;
4531 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4533 error ("%qE is not a valid template argument for type %qT "
4534 "because of conflicts in cv-qualification", expr, type);
4535 return NULL_TREE;
4538 if (!real_lvalue_p (expr))
4540 error ("%qE is not a valid template argument for type %qT "
4541 "because it is not an lvalue", expr, type);
4542 return NULL_TREE;
4545 /* [temp.arg.nontype]/1
4547 A template-argument for a non-type, non-template template-parameter
4548 shall be one of: [...]
4550 -- the address of an object or function with external linkage. */
4551 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4553 error ("%qE is not a valid template argument for type %qT "
4554 "because object %qD has not external linkage",
4555 expr, type, expr);
4556 return NULL_TREE;
4559 expr = build_nop (type, build_address (expr));
4561 /* [temp.arg.nontype]/5, bullet 4
4563 For a non-type template-parameter of type pointer to function, only
4564 the function-to-pointer conversion (_conv.func_) is applied. If the
4565 template-argument represents a set of overloaded functions (or a
4566 pointer to such), the matching function is selected from the set
4567 (_over.over_). */
4568 else if (TYPE_PTRFN_P (type))
4570 /* If the argument is a template-id, we might not have enough
4571 context information to decay the pointer. */
4572 if (!type_unknown_p (expr_type))
4574 expr = decay_conversion (expr);
4575 if (expr == error_mark_node)
4576 return error_mark_node;
4579 expr = convert_nontype_argument_function (type, expr);
4580 if (!expr || expr == error_mark_node)
4581 return expr;
4583 if (TREE_CODE (expr) != ADDR_EXPR)
4585 error ("%qE is not a valid template argument for type %qT", expr, type);
4586 error ("it must be the address of a function with external linkage");
4587 return NULL_TREE;
4590 /* [temp.arg.nontype]/5, bullet 5
4592 For a non-type template-parameter of type reference to function, no
4593 conversions apply. If the template-argument represents a set of
4594 overloaded functions, the matching function is selected from the set
4595 (_over.over_). */
4596 else if (TYPE_REFFN_P (type))
4598 if (TREE_CODE (expr) == ADDR_EXPR)
4600 error ("%qE is not a valid template argument for type %qT "
4601 "because it is a pointer", expr, type);
4602 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
4603 return NULL_TREE;
4606 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4607 if (!expr || expr == error_mark_node)
4608 return expr;
4610 expr = build_nop (type, build_address (expr));
4612 /* [temp.arg.nontype]/5, bullet 6
4614 For a non-type template-parameter of type pointer to member function,
4615 no conversions apply. If the template-argument represents a set of
4616 overloaded member functions, the matching member function is selected
4617 from the set (_over.over_). */
4618 else if (TYPE_PTRMEMFUNC_P (type))
4620 expr = instantiate_type (type, expr, tf_none);
4621 if (expr == error_mark_node)
4622 return error_mark_node;
4624 /* There is no way to disable standard conversions in
4625 resolve_address_of_overloaded_function (called by
4626 instantiate_type). It is possible that the call succeeded by
4627 converting &B::I to &D::I (where B is a base of D), so we need
4628 to reject this conversion here.
4630 Actually, even if there was a way to disable standard conversions,
4631 it would still be better to reject them here so that we can
4632 provide a superior diagnostic. */
4633 if (!same_type_p (TREE_TYPE (expr), type))
4635 /* Make sure we are just one standard conversion off. */
4636 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4637 error ("%qE is not a valid template argument for type %qT "
4638 "because it is of type %qT", expr, type,
4639 TREE_TYPE (expr));
4640 inform (input_location, "standard conversions are not allowed in this context");
4641 return NULL_TREE;
4644 /* [temp.arg.nontype]/5, bullet 7
4646 For a non-type template-parameter of type pointer to data member,
4647 qualification conversions (_conv.qual_) are applied. */
4648 else if (TYPE_PTRMEM_P (type))
4650 expr = perform_qualification_conversions (type, expr);
4651 if (expr == error_mark_node)
4652 return expr;
4654 /* A template non-type parameter must be one of the above. */
4655 else
4656 gcc_unreachable ();
4658 /* Sanity check: did we actually convert the argument to the
4659 right type? */
4660 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4661 return expr;
4664 /* Subroutine of coerce_template_template_parms, which returns 1 if
4665 PARM_PARM and ARG_PARM match using the rule for the template
4666 parameters of template template parameters. Both PARM and ARG are
4667 template parameters; the rest of the arguments are the same as for
4668 coerce_template_template_parms.
4670 static int
4671 coerce_template_template_parm (tree parm,
4672 tree arg,
4673 tsubst_flags_t complain,
4674 tree in_decl,
4675 tree outer_args)
4677 if (arg == NULL_TREE || arg == error_mark_node
4678 || parm == NULL_TREE || parm == error_mark_node)
4679 return 0;
4681 if (TREE_CODE (arg) != TREE_CODE (parm))
4682 return 0;
4684 switch (TREE_CODE (parm))
4686 case TEMPLATE_DECL:
4687 /* We encounter instantiations of templates like
4688 template <template <template <class> class> class TT>
4689 class C; */
4691 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4692 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4694 if (!coerce_template_template_parms
4695 (parmparm, argparm, complain, in_decl, outer_args))
4696 return 0;
4698 /* Fall through. */
4700 case TYPE_DECL:
4701 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
4702 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4703 /* Argument is a parameter pack but parameter is not. */
4704 return 0;
4705 break;
4707 case PARM_DECL:
4708 /* The tsubst call is used to handle cases such as
4710 template <int> class C {};
4711 template <class T, template <T> class TT> class D {};
4712 D<int, C> d;
4714 i.e. the parameter list of TT depends on earlier parameters. */
4715 if (!uses_template_parms (TREE_TYPE (arg))
4716 && !same_type_p
4717 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4718 TREE_TYPE (arg)))
4719 return 0;
4721 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
4722 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4723 /* Argument is a parameter pack but parameter is not. */
4724 return 0;
4726 break;
4728 default:
4729 gcc_unreachable ();
4732 return 1;
4736 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4737 template template parameters. Both PARM_PARMS and ARG_PARMS are
4738 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4739 or PARM_DECL.
4741 Consider the example:
4742 template <class T> class A;
4743 template<template <class U> class TT> class B;
4745 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4746 the parameters to A, and OUTER_ARGS contains A. */
4748 static int
4749 coerce_template_template_parms (tree parm_parms,
4750 tree arg_parms,
4751 tsubst_flags_t complain,
4752 tree in_decl,
4753 tree outer_args)
4755 int nparms, nargs, i;
4756 tree parm, arg;
4757 int variadic_p = 0;
4759 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4760 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4762 nparms = TREE_VEC_LENGTH (parm_parms);
4763 nargs = TREE_VEC_LENGTH (arg_parms);
4765 /* Determine whether we have a parameter pack at the end of the
4766 template template parameter's template parameter list. */
4767 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
4769 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
4771 if (parm == error_mark_node)
4772 return 0;
4774 switch (TREE_CODE (parm))
4776 case TEMPLATE_DECL:
4777 case TYPE_DECL:
4778 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4779 variadic_p = 1;
4780 break;
4782 case PARM_DECL:
4783 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4784 variadic_p = 1;
4785 break;
4787 default:
4788 gcc_unreachable ();
4792 if (nargs != nparms
4793 && !(variadic_p && nargs >= nparms - 1))
4794 return 0;
4796 /* Check all of the template parameters except the parameter pack at
4797 the end (if any). */
4798 for (i = 0; i < nparms - variadic_p; ++i)
4800 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4801 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4802 continue;
4804 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4805 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4807 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4808 outer_args))
4809 return 0;
4813 if (variadic_p)
4815 /* Check each of the template parameters in the template
4816 argument against the template parameter pack at the end of
4817 the template template parameter. */
4818 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
4819 return 0;
4821 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4823 for (; i < nargs; ++i)
4825 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4826 continue;
4828 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4830 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4831 outer_args))
4832 return 0;
4836 return 1;
4839 /* Verifies that the deduced template arguments (in TARGS) for the
4840 template template parameters (in TPARMS) represent valid bindings,
4841 by comparing the template parameter list of each template argument
4842 to the template parameter list of its corresponding template
4843 template parameter, in accordance with DR150. This
4844 routine can only be called after all template arguments have been
4845 deduced. It will return TRUE if all of the template template
4846 parameter bindings are okay, FALSE otherwise. */
4847 bool
4848 template_template_parm_bindings_ok_p (tree tparms, tree targs)
4850 int i, ntparms = TREE_VEC_LENGTH (tparms);
4851 bool ret = true;
4853 /* We're dealing with template parms in this process. */
4854 ++processing_template_decl;
4856 targs = INNERMOST_TEMPLATE_ARGS (targs);
4858 for (i = 0; i < ntparms; ++i)
4860 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
4861 tree targ = TREE_VEC_ELT (targs, i);
4863 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
4865 tree packed_args = NULL_TREE;
4866 int idx, len = 1;
4868 if (ARGUMENT_PACK_P (targ))
4870 /* Look inside the argument pack. */
4871 packed_args = ARGUMENT_PACK_ARGS (targ);
4872 len = TREE_VEC_LENGTH (packed_args);
4875 for (idx = 0; idx < len; ++idx)
4877 tree targ_parms = NULL_TREE;
4879 if (packed_args)
4880 /* Extract the next argument from the argument
4881 pack. */
4882 targ = TREE_VEC_ELT (packed_args, idx);
4884 if (PACK_EXPANSION_P (targ))
4885 /* Look at the pattern of the pack expansion. */
4886 targ = PACK_EXPANSION_PATTERN (targ);
4888 /* Extract the template parameters from the template
4889 argument. */
4890 if (TREE_CODE (targ) == TEMPLATE_DECL)
4891 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
4892 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
4893 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
4895 /* Verify that we can coerce the template template
4896 parameters from the template argument to the template
4897 parameter. This requires an exact match. */
4898 if (targ_parms
4899 && !coerce_template_template_parms
4900 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
4901 targ_parms,
4902 tf_none,
4903 tparm,
4904 targs))
4906 ret = false;
4907 goto out;
4913 out:
4915 --processing_template_decl;
4916 return ret;
4919 /* Convert the indicated template ARG as necessary to match the
4920 indicated template PARM. Returns the converted ARG, or
4921 error_mark_node if the conversion was unsuccessful. Error and
4922 warning messages are issued under control of COMPLAIN. This
4923 conversion is for the Ith parameter in the parameter list. ARGS is
4924 the full set of template arguments deduced so far. */
4926 static tree
4927 convert_template_argument (tree parm,
4928 tree arg,
4929 tree args,
4930 tsubst_flags_t complain,
4931 int i,
4932 tree in_decl)
4934 tree orig_arg;
4935 tree val;
4936 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4938 if (TREE_CODE (arg) == TREE_LIST
4939 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4941 /* The template argument was the name of some
4942 member function. That's usually
4943 invalid, but static members are OK. In any
4944 case, grab the underlying fields/functions
4945 and issue an error later if required. */
4946 orig_arg = TREE_VALUE (arg);
4947 TREE_TYPE (arg) = unknown_type_node;
4950 orig_arg = arg;
4952 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4953 requires_type = (TREE_CODE (parm) == TYPE_DECL
4954 || requires_tmpl_type);
4956 /* When determining whether an argument pack expansion is a template,
4957 look at the pattern. */
4958 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
4959 arg = PACK_EXPANSION_PATTERN (arg);
4961 is_tmpl_type =
4962 ((TREE_CODE (arg) == TEMPLATE_DECL
4963 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
4964 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4965 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
4967 if (is_tmpl_type
4968 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4969 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4970 arg = TYPE_STUB_DECL (arg);
4972 is_type = TYPE_P (arg) || is_tmpl_type;
4974 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4975 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4977 permerror (input_location, "to refer to a type member of a template parameter, "
4978 "use %<typename %E%>", orig_arg);
4980 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
4981 TREE_OPERAND (arg, 1),
4982 typename_type,
4983 complain & tf_error);
4984 arg = orig_arg;
4985 is_type = 1;
4987 if (is_type != requires_type)
4989 if (in_decl)
4991 if (complain & tf_error)
4993 error ("type/value mismatch at argument %d in template "
4994 "parameter list for %qD",
4995 i + 1, in_decl);
4996 if (is_type)
4997 error (" expected a constant of type %qT, got %qT",
4998 TREE_TYPE (parm),
4999 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5000 else if (requires_tmpl_type)
5001 error (" expected a class template, got %qE", orig_arg);
5002 else
5003 error (" expected a type, got %qE", orig_arg);
5006 return error_mark_node;
5008 if (is_tmpl_type ^ requires_tmpl_type)
5010 if (in_decl && (complain & tf_error))
5012 error ("type/value mismatch at argument %d in template "
5013 "parameter list for %qD",
5014 i + 1, in_decl);
5015 if (is_tmpl_type)
5016 error (" expected a type, got %qT", DECL_NAME (arg));
5017 else
5018 error (" expected a class template, got %qT", orig_arg);
5020 return error_mark_node;
5023 if (is_type)
5025 if (requires_tmpl_type)
5027 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5028 /* The number of argument required is not known yet.
5029 Just accept it for now. */
5030 val = TREE_TYPE (arg);
5031 else
5033 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5034 tree argparm;
5036 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5038 if (coerce_template_template_parms (parmparm, argparm,
5039 complain, in_decl,
5040 args))
5042 val = orig_arg;
5044 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5045 TEMPLATE_DECL. */
5046 if (val != error_mark_node)
5048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5049 val = TREE_TYPE (val);
5050 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5051 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5053 val = TREE_TYPE (arg);
5054 val = make_pack_expansion (val);
5058 else
5060 if (in_decl && (complain & tf_error))
5062 error ("type/value mismatch at argument %d in "
5063 "template parameter list for %qD",
5064 i + 1, in_decl);
5065 error (" expected a template of type %qD, got %qD",
5066 parm, orig_arg);
5069 val = error_mark_node;
5073 else
5074 val = orig_arg;
5075 /* We only form one instance of each template specialization.
5076 Therefore, if we use a non-canonical variant (i.e., a
5077 typedef), any future messages referring to the type will use
5078 the typedef, which is confusing if those future uses do not
5079 themselves also use the typedef. */
5080 if (TYPE_P (val))
5081 val = strip_typedefs (val);
5083 else
5085 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5087 if (invalid_nontype_parm_type_p (t, complain))
5088 return error_mark_node;
5090 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5092 if (same_type_p (t, TREE_TYPE (orig_arg)))
5093 val = orig_arg;
5094 else
5096 /* Not sure if this is reachable, but it doesn't hurt
5097 to be robust. */
5098 error ("type mismatch in nontype parameter pack");
5099 val = error_mark_node;
5102 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5103 /* We used to call digest_init here. However, digest_init
5104 will report errors, which we don't want when complain
5105 is zero. More importantly, digest_init will try too
5106 hard to convert things: for example, `0' should not be
5107 converted to pointer type at this point according to
5108 the standard. Accepting this is not merely an
5109 extension, since deciding whether or not these
5110 conversions can occur is part of determining which
5111 function template to call, or whether a given explicit
5112 argument specification is valid. */
5113 val = convert_nontype_argument (t, orig_arg);
5114 else
5115 val = orig_arg;
5117 if (val == NULL_TREE)
5118 val = error_mark_node;
5119 else if (val == error_mark_node && (complain & tf_error))
5120 error ("could not convert template argument %qE to %qT", orig_arg, t);
5123 return val;
5126 /* Coerces the remaining template arguments in INNER_ARGS (from
5127 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5128 Returns the coerced argument pack. PARM_IDX is the position of this
5129 parameter in the template parameter list. ARGS is the original
5130 template argument list. */
5131 static tree
5132 coerce_template_parameter_pack (tree parms,
5133 int parm_idx,
5134 tree args,
5135 tree inner_args,
5136 int arg_idx,
5137 tree new_args,
5138 int* lost,
5139 tree in_decl,
5140 tsubst_flags_t complain)
5142 tree parm = TREE_VEC_ELT (parms, parm_idx);
5143 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5144 tree packed_args;
5145 tree argument_pack;
5146 tree packed_types = NULL_TREE;
5148 if (arg_idx > nargs)
5149 arg_idx = nargs;
5151 packed_args = make_tree_vec (nargs - arg_idx);
5153 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5154 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5156 /* When the template parameter is a non-type template
5157 parameter pack whose type uses parameter packs, we need
5158 to look at each of the template arguments
5159 separately. Build a vector of the types for these
5160 non-type template parameters in PACKED_TYPES. */
5161 tree expansion
5162 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5163 packed_types = tsubst_pack_expansion (expansion, args,
5164 complain, in_decl);
5166 if (packed_types == error_mark_node)
5167 return error_mark_node;
5169 /* Check that we have the right number of arguments. */
5170 if (arg_idx < nargs
5171 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5172 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5174 int needed_parms
5175 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5176 error ("wrong number of template arguments (%d, should be %d)",
5177 nargs, needed_parms);
5178 return error_mark_node;
5181 /* If we aren't able to check the actual arguments now
5182 (because they haven't been expanded yet), we can at least
5183 verify that all of the types used for the non-type
5184 template parameter pack are, in fact, valid for non-type
5185 template parameters. */
5186 if (arg_idx < nargs
5187 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5189 int j, len = TREE_VEC_LENGTH (packed_types);
5190 for (j = 0; j < len; ++j)
5192 tree t = TREE_VEC_ELT (packed_types, j);
5193 if (invalid_nontype_parm_type_p (t, complain))
5194 return error_mark_node;
5199 /* Convert the remaining arguments, which will be a part of the
5200 parameter pack "parm". */
5201 for (; arg_idx < nargs; ++arg_idx)
5203 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5204 tree actual_parm = TREE_VALUE (parm);
5206 if (packed_types && !PACK_EXPANSION_P (arg))
5208 /* When we have a vector of types (corresponding to the
5209 non-type template parameter pack that uses parameter
5210 packs in its type, as mention above), and the
5211 argument is not an expansion (which expands to a
5212 currently unknown number of arguments), clone the
5213 parm and give it the next type in PACKED_TYPES. */
5214 actual_parm = copy_node (actual_parm);
5215 TREE_TYPE (actual_parm) =
5216 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5219 if (arg != error_mark_node)
5220 arg = convert_template_argument (actual_parm,
5221 arg, new_args, complain, parm_idx,
5222 in_decl);
5223 if (arg == error_mark_node)
5224 (*lost)++;
5225 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5228 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5229 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5230 argument_pack = make_node (TYPE_ARGUMENT_PACK);
5231 else
5233 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5234 TREE_TYPE (argument_pack)
5235 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5236 TREE_CONSTANT (argument_pack) = 1;
5239 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5240 return argument_pack;
5243 /* Convert all template arguments to their appropriate types, and
5244 return a vector containing the innermost resulting template
5245 arguments. If any error occurs, return error_mark_node. Error and
5246 warning messages are issued under control of COMPLAIN.
5248 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5249 for arguments not specified in ARGS. Otherwise, if
5250 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5251 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5252 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5253 ARGS. */
5255 static tree
5256 coerce_template_parms (tree parms,
5257 tree args,
5258 tree in_decl,
5259 tsubst_flags_t complain,
5260 bool require_all_args,
5261 bool use_default_args)
5263 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5264 tree inner_args;
5265 tree new_args;
5266 tree new_inner_args;
5267 int saved_unevaluated_operand;
5268 int saved_inhibit_evaluation_warnings;
5270 /* When used as a boolean value, indicates whether this is a
5271 variadic template parameter list. Since it's an int, we can also
5272 subtract it from nparms to get the number of non-variadic
5273 parameters. */
5274 int variadic_p = 0;
5276 nparms = TREE_VEC_LENGTH (parms);
5278 /* Determine if there are any parameter packs. */
5279 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5281 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5282 if (template_parameter_pack_p (tparm))
5283 ++variadic_p;
5286 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5287 /* If there are 0 or 1 parameter packs, we need to expand any argument
5288 packs so that we can deduce a parameter pack from some non-packed args
5289 followed by an argument pack, as in variadic85.C. If there are more
5290 than that, we need to leave argument packs intact so the arguments are
5291 assigned to the right parameter packs. This should only happen when
5292 dealing with a nested class inside a partial specialization of a class
5293 template, as in variadic92.C. */
5294 if (variadic_p <= 1)
5295 inner_args = expand_template_argument_pack (inner_args);
5297 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5298 if ((nargs > nparms && !variadic_p)
5299 || (nargs < nparms - variadic_p
5300 && require_all_args
5301 && (!use_default_args
5302 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5303 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5305 if (complain & tf_error)
5307 const char *or_more = "";
5308 if (variadic_p)
5310 or_more = " or more";
5311 --nparms;
5314 error ("wrong number of template arguments (%d, should be %d%s)",
5315 nargs, nparms, or_more);
5317 if (in_decl)
5318 error ("provided for %q+D", in_decl);
5321 return error_mark_node;
5324 /* We need to evaluate the template arguments, even though this
5325 template-id may be nested within a "sizeof". */
5326 saved_unevaluated_operand = cp_unevaluated_operand;
5327 cp_unevaluated_operand = 0;
5328 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5329 c_inhibit_evaluation_warnings = 0;
5330 new_inner_args = make_tree_vec (nparms);
5331 new_args = add_outermost_template_args (args, new_inner_args);
5332 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5334 tree arg;
5335 tree parm;
5337 /* Get the Ith template parameter. */
5338 parm = TREE_VEC_ELT (parms, parm_idx);
5340 if (parm == error_mark_node)
5342 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5343 continue;
5346 /* Calculate the next argument. */
5347 if (arg_idx < nargs)
5348 arg = TREE_VEC_ELT (inner_args, arg_idx);
5349 else
5350 arg = NULL_TREE;
5352 if (template_parameter_pack_p (TREE_VALUE (parm))
5353 && !(arg && ARGUMENT_PACK_P (arg)))
5355 /* All remaining arguments will be placed in the
5356 template parameter pack PARM. */
5357 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5358 inner_args, arg_idx,
5359 new_args, &lost,
5360 in_decl, complain);
5362 /* Store this argument. */
5363 if (arg == error_mark_node)
5364 lost++;
5365 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5367 /* We are done with all of the arguments. */
5368 arg_idx = nargs;
5370 continue;
5372 else if (arg)
5374 if (PACK_EXPANSION_P (arg))
5376 if (complain & tf_error)
5378 /* FIXME this restriction was removed by N2555; see
5379 bug 35722. */
5380 /* If ARG is a pack expansion, but PARM is not a
5381 template parameter pack (if it were, we would have
5382 handled it above), we're trying to expand into a
5383 fixed-length argument list. */
5384 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5385 sorry ("cannot expand %<%E%> into a fixed-length "
5386 "argument list", arg);
5387 else
5388 sorry ("cannot expand %<%T%> into a fixed-length "
5389 "argument list", arg);
5391 return error_mark_node;
5394 else if (require_all_args)
5395 /* There must be a default arg in this case. */
5396 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5397 complain, in_decl);
5398 else
5399 break;
5401 if (arg == error_mark_node)
5403 if (complain & tf_error)
5404 error ("template argument %d is invalid", arg_idx + 1);
5406 else if (!arg)
5407 /* This only occurs if there was an error in the template
5408 parameter list itself (which we would already have
5409 reported) that we are trying to recover from, e.g., a class
5410 template with a parameter list such as
5411 template<typename..., typename>. */
5412 return error_mark_node;
5413 else
5414 arg = convert_template_argument (TREE_VALUE (parm),
5415 arg, new_args, complain,
5416 parm_idx, in_decl);
5418 if (arg == error_mark_node)
5419 lost++;
5420 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5422 cp_unevaluated_operand = saved_unevaluated_operand;
5423 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5425 if (lost)
5426 return error_mark_node;
5428 return new_inner_args;
5431 /* Returns 1 if template args OT and NT are equivalent. */
5433 static int
5434 template_args_equal (tree ot, tree nt)
5436 if (nt == ot)
5437 return 1;
5439 if (TREE_CODE (nt) == TREE_VEC)
5440 /* For member templates */
5441 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5442 else if (PACK_EXPANSION_P (ot))
5443 return PACK_EXPANSION_P (nt)
5444 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5445 PACK_EXPANSION_PATTERN (nt));
5446 else if (ARGUMENT_PACK_P (ot))
5448 int i, len;
5449 tree opack, npack;
5451 if (!ARGUMENT_PACK_P (nt))
5452 return 0;
5454 opack = ARGUMENT_PACK_ARGS (ot);
5455 npack = ARGUMENT_PACK_ARGS (nt);
5456 len = TREE_VEC_LENGTH (opack);
5457 if (TREE_VEC_LENGTH (npack) != len)
5458 return 0;
5459 for (i = 0; i < len; ++i)
5460 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5461 TREE_VEC_ELT (npack, i)))
5462 return 0;
5463 return 1;
5465 else if (TYPE_P (nt))
5466 return TYPE_P (ot) && same_type_p (ot, nt);
5467 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5468 return 0;
5469 else
5470 return cp_tree_equal (ot, nt);
5473 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5474 of template arguments. Returns 0 otherwise. */
5477 comp_template_args (tree oldargs, tree newargs)
5479 int i;
5481 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5482 return 0;
5484 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5486 tree nt = TREE_VEC_ELT (newargs, i);
5487 tree ot = TREE_VEC_ELT (oldargs, i);
5489 if (! template_args_equal (ot, nt))
5490 return 0;
5492 return 1;
5495 static void
5496 add_pending_template (tree d)
5498 tree ti = (TYPE_P (d)
5499 ? CLASSTYPE_TEMPLATE_INFO (d)
5500 : DECL_TEMPLATE_INFO (d));
5501 struct pending_template *pt;
5502 int level;
5504 if (TI_PENDING_TEMPLATE_FLAG (ti))
5505 return;
5507 /* We are called both from instantiate_decl, where we've already had a
5508 tinst_level pushed, and instantiate_template, where we haven't.
5509 Compensate. */
5510 level = !current_tinst_level || current_tinst_level->decl != d;
5512 if (level)
5513 push_tinst_level (d);
5515 pt = GGC_NEW (struct pending_template);
5516 pt->next = NULL;
5517 pt->tinst = current_tinst_level;
5518 if (last_pending_template)
5519 last_pending_template->next = pt;
5520 else
5521 pending_templates = pt;
5523 last_pending_template = pt;
5525 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5527 if (level)
5528 pop_tinst_level ();
5532 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5533 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5534 documentation for TEMPLATE_ID_EXPR. */
5536 tree
5537 lookup_template_function (tree fns, tree arglist)
5539 tree type;
5541 if (fns == error_mark_node || arglist == error_mark_node)
5542 return error_mark_node;
5544 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5545 gcc_assert (fns && (is_overloaded_fn (fns)
5546 || TREE_CODE (fns) == IDENTIFIER_NODE));
5548 if (BASELINK_P (fns))
5550 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5551 unknown_type_node,
5552 BASELINK_FUNCTIONS (fns),
5553 arglist);
5554 return fns;
5557 type = TREE_TYPE (fns);
5558 if (TREE_CODE (fns) == OVERLOAD || !type)
5559 type = unknown_type_node;
5561 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5564 /* Within the scope of a template class S<T>, the name S gets bound
5565 (in build_self_reference) to a TYPE_DECL for the class, not a
5566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5567 or one of its enclosing classes, and that type is a template,
5568 return the associated TEMPLATE_DECL. Otherwise, the original
5569 DECL is returned. */
5571 tree
5572 maybe_get_template_decl_from_type_decl (tree decl)
5574 return (decl != NULL_TREE
5575 && TREE_CODE (decl) == TYPE_DECL
5576 && DECL_ARTIFICIAL (decl)
5577 && CLASS_TYPE_P (TREE_TYPE (decl))
5578 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5579 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5582 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5583 parameters, find the desired type.
5585 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5587 IN_DECL, if non-NULL, is the template declaration we are trying to
5588 instantiate.
5590 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5591 the class we are looking up.
5593 Issue error and warning messages under control of COMPLAIN.
5595 If the template class is really a local class in a template
5596 function, then the FUNCTION_CONTEXT is the function in which it is
5597 being instantiated.
5599 ??? Note that this function is currently called *twice* for each
5600 template-id: the first time from the parser, while creating the
5601 incomplete type (finish_template_type), and the second type during the
5602 real instantiation (instantiate_template_class). This is surely something
5603 that we want to avoid. It also causes some problems with argument
5604 coercion (see convert_nontype_argument for more information on this). */
5606 tree
5607 lookup_template_class (tree d1,
5608 tree arglist,
5609 tree in_decl,
5610 tree context,
5611 int entering_scope,
5612 tsubst_flags_t complain)
5614 tree templ = NULL_TREE, parmlist;
5615 tree t;
5617 timevar_push (TV_NAME_LOOKUP);
5619 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5621 tree value = innermost_non_namespace_value (d1);
5622 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5623 templ = value;
5624 else
5626 if (context)
5627 push_decl_namespace (context);
5628 templ = lookup_name (d1);
5629 templ = maybe_get_template_decl_from_type_decl (templ);
5630 if (context)
5631 pop_decl_namespace ();
5633 if (templ)
5634 context = DECL_CONTEXT (templ);
5636 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
5638 tree type = TREE_TYPE (d1);
5640 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5641 an implicit typename for the second A. Deal with it. */
5642 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5643 type = TREE_TYPE (type);
5645 if (CLASSTYPE_TEMPLATE_INFO (type))
5647 templ = CLASSTYPE_TI_TEMPLATE (type);
5648 d1 = DECL_NAME (templ);
5651 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5652 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
5654 templ = TYPE_TI_TEMPLATE (d1);
5655 d1 = DECL_NAME (templ);
5657 else if (TREE_CODE (d1) == TEMPLATE_DECL
5658 && DECL_TEMPLATE_RESULT (d1)
5659 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5661 templ = d1;
5662 d1 = DECL_NAME (templ);
5663 context = DECL_CONTEXT (templ);
5666 /* Issue an error message if we didn't find a template. */
5667 if (! templ)
5669 if (complain & tf_error)
5670 error ("%qT is not a template", d1);
5671 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5674 if (TREE_CODE (templ) != TEMPLATE_DECL
5675 /* Make sure it's a user visible template, if it was named by
5676 the user. */
5677 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
5678 && !PRIMARY_TEMPLATE_P (templ)))
5680 if (complain & tf_error)
5682 error ("non-template type %qT used as a template", d1);
5683 if (in_decl)
5684 error ("for template declaration %q+D", in_decl);
5686 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5689 complain &= ~tf_user;
5691 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
5693 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5694 template arguments */
5696 tree parm;
5697 tree arglist2;
5698 tree outer;
5700 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
5702 /* Consider an example where a template template parameter declared as
5704 template <class T, class U = std::allocator<T> > class TT
5706 The template parameter level of T and U are one level larger than
5707 of TT. To proper process the default argument of U, say when an
5708 instantiation `TT<int>' is seen, we need to build the full
5709 arguments containing {int} as the innermost level. Outer levels,
5710 available when not appearing as default template argument, can be
5711 obtained from the arguments of the enclosing template.
5713 Suppose that TT is later substituted with std::vector. The above
5714 instantiation is `TT<int, std::allocator<T> >' with TT at
5715 level 1, and T at level 2, while the template arguments at level 1
5716 becomes {std::vector} and the inner level 2 is {int}. */
5718 outer = DECL_CONTEXT (templ);
5719 if (outer)
5720 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
5721 else if (current_template_parms)
5722 /* This is an argument of the current template, so we haven't set
5723 DECL_CONTEXT yet. */
5724 outer = current_template_args ();
5726 if (outer)
5727 arglist = add_to_template_args (outer, arglist);
5729 arglist2 = coerce_template_parms (parmlist, arglist, templ,
5730 complain,
5731 /*require_all_args=*/true,
5732 /*use_default_args=*/true);
5733 if (arglist2 == error_mark_node
5734 || (!uses_template_parms (arglist2)
5735 && check_instantiated_args (templ, arglist2, complain)))
5736 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5738 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
5739 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5741 else
5743 tree template_type = TREE_TYPE (templ);
5744 tree gen_tmpl;
5745 tree type_decl;
5746 tree found = NULL_TREE;
5747 int arg_depth;
5748 int parm_depth;
5749 int is_partial_instantiation;
5751 gen_tmpl = most_general_template (templ);
5752 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5753 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5754 arg_depth = TMPL_ARGS_DEPTH (arglist);
5756 if (arg_depth == 1 && parm_depth > 1)
5758 /* We've been given an incomplete set of template arguments.
5759 For example, given:
5761 template <class T> struct S1 {
5762 template <class U> struct S2 {};
5763 template <class U> struct S2<U*> {};
5766 we will be called with an ARGLIST of `U*', but the
5767 TEMPLATE will be `template <class T> template
5768 <class U> struct S1<T>::S2'. We must fill in the missing
5769 arguments. */
5770 arglist
5771 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
5772 arglist);
5773 arg_depth = TMPL_ARGS_DEPTH (arglist);
5776 /* Now we should have enough arguments. */
5777 gcc_assert (parm_depth == arg_depth);
5779 /* From here on, we're only interested in the most general
5780 template. */
5781 templ = gen_tmpl;
5783 /* Calculate the BOUND_ARGS. These will be the args that are
5784 actually tsubst'd into the definition to create the
5785 instantiation. */
5786 if (parm_depth > 1)
5788 /* We have multiple levels of arguments to coerce, at once. */
5789 int i;
5790 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5792 tree bound_args = make_tree_vec (parm_depth);
5794 for (i = saved_depth,
5795 t = DECL_TEMPLATE_PARMS (templ);
5796 i > 0 && t != NULL_TREE;
5797 --i, t = TREE_CHAIN (t))
5799 tree a = coerce_template_parms (TREE_VALUE (t),
5800 arglist, templ,
5801 complain,
5802 /*require_all_args=*/true,
5803 /*use_default_args=*/true);
5805 /* Don't process further if one of the levels fails. */
5806 if (a == error_mark_node)
5808 /* Restore the ARGLIST to its full size. */
5809 TREE_VEC_LENGTH (arglist) = saved_depth;
5810 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5813 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5815 /* We temporarily reduce the length of the ARGLIST so
5816 that coerce_template_parms will see only the arguments
5817 corresponding to the template parameters it is
5818 examining. */
5819 TREE_VEC_LENGTH (arglist)--;
5822 /* Restore the ARGLIST to its full size. */
5823 TREE_VEC_LENGTH (arglist) = saved_depth;
5825 arglist = bound_args;
5827 else
5828 arglist
5829 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5830 INNERMOST_TEMPLATE_ARGS (arglist),
5831 templ,
5832 complain,
5833 /*require_all_args=*/true,
5834 /*use_default_args=*/true);
5836 if (arglist == error_mark_node)
5837 /* We were unable to bind the arguments. */
5838 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5840 /* In the scope of a template class, explicit references to the
5841 template class refer to the type of the template, not any
5842 instantiation of it. For example, in:
5844 template <class T> class C { void f(C<T>); }
5846 the `C<T>' is just the same as `C'. Outside of the
5847 class, however, such a reference is an instantiation. */
5848 if (comp_template_args (TYPE_TI_ARGS (template_type),
5849 arglist))
5851 found = template_type;
5853 if (!entering_scope && PRIMARY_TEMPLATE_P (templ))
5855 tree ctx;
5857 for (ctx = current_class_type;
5858 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5859 ctx = (TYPE_P (ctx)
5860 ? TYPE_CONTEXT (ctx)
5861 : DECL_CONTEXT (ctx)))
5862 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5863 goto found_ctx;
5865 /* We're not in the scope of the class, so the
5866 TEMPLATE_TYPE is not the type we want after all. */
5867 found = NULL_TREE;
5868 found_ctx:;
5871 if (found)
5872 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5874 /* If we already have this specialization, return it. */
5875 found = retrieve_specialization (templ, arglist,
5876 /*class_specializations_p=*/false);
5877 if (found)
5878 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5880 /* This type is a "partial instantiation" if any of the template
5881 arguments still involve template parameters. Note that we set
5882 IS_PARTIAL_INSTANTIATION for partial specializations as
5883 well. */
5884 is_partial_instantiation = uses_template_parms (arglist);
5886 /* If the deduced arguments are invalid, then the binding
5887 failed. */
5888 if (!is_partial_instantiation
5889 && check_instantiated_args (templ,
5890 INNERMOST_TEMPLATE_ARGS (arglist),
5891 complain))
5892 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5894 if (!is_partial_instantiation
5895 && !PRIMARY_TEMPLATE_P (templ)
5896 && TREE_CODE (CP_DECL_CONTEXT (templ)) == NAMESPACE_DECL)
5898 found = xref_tag_from_type (TREE_TYPE (templ),
5899 DECL_NAME (templ),
5900 /*tag_scope=*/ts_global);
5901 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5904 context = tsubst (DECL_CONTEXT (templ), arglist,
5905 complain, in_decl);
5906 if (!context)
5907 context = global_namespace;
5909 /* Create the type. */
5910 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5912 if (!is_partial_instantiation)
5914 set_current_access_from_decl (TYPE_NAME (template_type));
5915 t = start_enum (TYPE_IDENTIFIER (template_type),
5916 tsubst (ENUM_UNDERLYING_TYPE (template_type),
5917 arglist, complain, in_decl),
5918 SCOPED_ENUM_P (template_type));
5920 else
5922 /* We don't want to call start_enum for this type, since
5923 the values for the enumeration constants may involve
5924 template parameters. And, no one should be interested
5925 in the enumeration constants for such a type. */
5926 t = make_node (ENUMERAL_TYPE);
5927 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
5930 else
5932 t = make_class_type (TREE_CODE (template_type));
5933 CLASSTYPE_DECLARED_CLASS (t)
5934 = CLASSTYPE_DECLARED_CLASS (template_type);
5935 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5936 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5938 /* A local class. Make sure the decl gets registered properly. */
5939 if (context == current_function_decl)
5940 pushtag (DECL_NAME (templ), t, /*tag_scope=*/ts_current);
5942 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5943 /* This instantiation is another name for the primary
5944 template type. Set the TYPE_CANONICAL field
5945 appropriately. */
5946 TYPE_CANONICAL (t) = template_type;
5947 else if (any_template_arguments_need_structural_equality_p (arglist))
5948 /* Some of the template arguments require structural
5949 equality testing, so this template class requires
5950 structural equality testing. */
5951 SET_TYPE_STRUCTURAL_EQUALITY (t);
5954 /* If we called start_enum or pushtag above, this information
5955 will already be set up. */
5956 if (!TYPE_NAME (t))
5958 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5960 type_decl = create_implicit_typedef (DECL_NAME (templ), t);
5961 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5962 TYPE_STUB_DECL (t) = type_decl;
5963 DECL_SOURCE_LOCATION (type_decl)
5964 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5966 else
5967 type_decl = TYPE_NAME (t);
5969 TREE_PRIVATE (type_decl)
5970 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5971 TREE_PROTECTED (type_decl)
5972 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5973 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5975 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5976 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5979 /* Set up the template information. We have to figure out which
5980 template is the immediate parent if this is a full
5981 instantiation. */
5982 if (parm_depth == 1 || is_partial_instantiation
5983 || !PRIMARY_TEMPLATE_P (templ))
5984 /* This case is easy; there are no member templates involved. */
5985 found = templ;
5986 else
5988 /* This is a full instantiation of a member template. Look
5989 for a partial instantiation of which this is an instance. */
5991 for (found = DECL_TEMPLATE_INSTANTIATIONS (templ);
5992 found; found = TREE_CHAIN (found))
5994 int success;
5995 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5997 /* We only want partial instantiations, here, not
5998 specializations or full instantiations. */
5999 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
6000 || !uses_template_parms (TREE_VALUE (found)))
6001 continue;
6003 /* Temporarily reduce by one the number of levels in the
6004 ARGLIST and in FOUND so as to avoid comparing the
6005 last set of arguments. */
6006 TREE_VEC_LENGTH (arglist)--;
6007 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
6009 /* See if the arguments match. If they do, then TMPL is
6010 the partial instantiation we want. */
6011 success = comp_template_args (TREE_PURPOSE (found), arglist);
6013 /* Restore the argument vectors to their full size. */
6014 TREE_VEC_LENGTH (arglist)++;
6015 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
6017 if (success)
6019 found = tmpl;
6020 break;
6024 if (!found)
6026 /* There was no partial instantiation. This happens
6027 where C<T> is a member template of A<T> and it's used
6028 in something like
6030 template <typename T> struct B { A<T>::C<int> m; };
6031 B<float>;
6033 Create the partial instantiation.
6035 TREE_VEC_LENGTH (arglist)--;
6036 found = tsubst (templ, arglist, complain, NULL_TREE);
6037 TREE_VEC_LENGTH (arglist)++;
6041 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6042 DECL_TEMPLATE_INSTANTIATIONS (templ)
6043 = tree_cons (arglist, t,
6044 DECL_TEMPLATE_INSTANTIATIONS (templ));
6046 if (TREE_CODE (t) == ENUMERAL_TYPE
6047 && !is_partial_instantiation)
6048 /* Now that the type has been registered on the instantiations
6049 list, we set up the enumerators. Because the enumeration
6050 constants may involve the enumeration type itself, we make
6051 sure to register the type first, and then create the
6052 constants. That way, doing tsubst_expr for the enumeration
6053 constants won't result in recursive calls here; we'll find
6054 the instantiation and exit above. */
6055 tsubst_enum (template_type, t, arglist);
6057 if (is_partial_instantiation)
6058 /* If the type makes use of template parameters, the
6059 code that generates debugging information will crash. */
6060 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6062 /* Possibly limit visibility based on template args. */
6063 TREE_PUBLIC (type_decl) = 1;
6064 determine_visibility (type_decl);
6066 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6068 timevar_pop (TV_NAME_LOOKUP);
6071 struct pair_fn_data
6073 tree_fn_t fn;
6074 void *data;
6075 /* True when we should also visit template parameters that occur in
6076 non-deduced contexts. */
6077 bool include_nondeduced_p;
6078 struct pointer_set_t *visited;
6081 /* Called from for_each_template_parm via walk_tree. */
6083 static tree
6084 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6086 tree t = *tp;
6087 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6088 tree_fn_t fn = pfd->fn;
6089 void *data = pfd->data;
6091 if (TYPE_P (t)
6092 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6093 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6094 pfd->include_nondeduced_p))
6095 return error_mark_node;
6097 switch (TREE_CODE (t))
6099 case RECORD_TYPE:
6100 if (TYPE_PTRMEMFUNC_P (t))
6101 break;
6102 /* Fall through. */
6104 case UNION_TYPE:
6105 case ENUMERAL_TYPE:
6106 if (!TYPE_TEMPLATE_INFO (t))
6107 *walk_subtrees = 0;
6108 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6109 fn, data, pfd->visited,
6110 pfd->include_nondeduced_p))
6111 return error_mark_node;
6112 break;
6114 case INTEGER_TYPE:
6115 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6116 fn, data, pfd->visited,
6117 pfd->include_nondeduced_p)
6118 || for_each_template_parm (TYPE_MAX_VALUE (t),
6119 fn, data, pfd->visited,
6120 pfd->include_nondeduced_p))
6121 return error_mark_node;
6122 break;
6124 case METHOD_TYPE:
6125 /* Since we're not going to walk subtrees, we have to do this
6126 explicitly here. */
6127 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6128 pfd->visited, pfd->include_nondeduced_p))
6129 return error_mark_node;
6130 /* Fall through. */
6132 case FUNCTION_TYPE:
6133 /* Check the return type. */
6134 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6135 pfd->include_nondeduced_p))
6136 return error_mark_node;
6138 /* Check the parameter types. Since default arguments are not
6139 instantiated until they are needed, the TYPE_ARG_TYPES may
6140 contain expressions that involve template parameters. But,
6141 no-one should be looking at them yet. And, once they're
6142 instantiated, they don't contain template parameters, so
6143 there's no point in looking at them then, either. */
6145 tree parm;
6147 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6148 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6149 pfd->visited, pfd->include_nondeduced_p))
6150 return error_mark_node;
6152 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6153 want walk_tree walking into them itself. */
6154 *walk_subtrees = 0;
6156 break;
6158 case TYPEOF_TYPE:
6159 if (pfd->include_nondeduced_p
6160 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6161 pfd->visited,
6162 pfd->include_nondeduced_p))
6163 return error_mark_node;
6164 break;
6166 case FUNCTION_DECL:
6167 case VAR_DECL:
6168 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6169 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6170 pfd->visited, pfd->include_nondeduced_p))
6171 return error_mark_node;
6172 /* Fall through. */
6174 case PARM_DECL:
6175 case CONST_DECL:
6176 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6177 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6178 pfd->visited, pfd->include_nondeduced_p))
6179 return error_mark_node;
6180 if (DECL_CONTEXT (t)
6181 && pfd->include_nondeduced_p
6182 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6183 pfd->visited, pfd->include_nondeduced_p))
6184 return error_mark_node;
6185 break;
6187 case BOUND_TEMPLATE_TEMPLATE_PARM:
6188 /* Record template parameters such as `T' inside `TT<T>'. */
6189 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6190 pfd->include_nondeduced_p))
6191 return error_mark_node;
6192 /* Fall through. */
6194 case TEMPLATE_TEMPLATE_PARM:
6195 case TEMPLATE_TYPE_PARM:
6196 case TEMPLATE_PARM_INDEX:
6197 if (fn && (*fn)(t, data))
6198 return error_mark_node;
6199 else if (!fn)
6200 return error_mark_node;
6201 break;
6203 case TEMPLATE_DECL:
6204 /* A template template parameter is encountered. */
6205 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6206 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6207 pfd->include_nondeduced_p))
6208 return error_mark_node;
6210 /* Already substituted template template parameter */
6211 *walk_subtrees = 0;
6212 break;
6214 case TYPENAME_TYPE:
6215 if (!fn
6216 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6217 data, pfd->visited,
6218 pfd->include_nondeduced_p))
6219 return error_mark_node;
6220 break;
6222 case CONSTRUCTOR:
6223 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6224 && pfd->include_nondeduced_p
6225 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6226 (TREE_TYPE (t)), fn, data,
6227 pfd->visited, pfd->include_nondeduced_p))
6228 return error_mark_node;
6229 break;
6231 case INDIRECT_REF:
6232 case COMPONENT_REF:
6233 /* If there's no type, then this thing must be some expression
6234 involving template parameters. */
6235 if (!fn && !TREE_TYPE (t))
6236 return error_mark_node;
6237 break;
6239 case MODOP_EXPR:
6240 case CAST_EXPR:
6241 case REINTERPRET_CAST_EXPR:
6242 case CONST_CAST_EXPR:
6243 case STATIC_CAST_EXPR:
6244 case DYNAMIC_CAST_EXPR:
6245 case ARROW_EXPR:
6246 case DOTSTAR_EXPR:
6247 case TYPEID_EXPR:
6248 case PSEUDO_DTOR_EXPR:
6249 if (!fn)
6250 return error_mark_node;
6251 break;
6253 default:
6254 break;
6257 /* We didn't find any template parameters we liked. */
6258 return NULL_TREE;
6261 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6262 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6263 call FN with the parameter and the DATA.
6264 If FN returns nonzero, the iteration is terminated, and
6265 for_each_template_parm returns 1. Otherwise, the iteration
6266 continues. If FN never returns a nonzero value, the value
6267 returned by for_each_template_parm is 0. If FN is NULL, it is
6268 considered to be the function which always returns 1.
6270 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6271 parameters that occur in non-deduced contexts. When false, only
6272 visits those template parameters that can be deduced. */
6274 static int
6275 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6276 struct pointer_set_t *visited,
6277 bool include_nondeduced_p)
6279 struct pair_fn_data pfd;
6280 int result;
6282 /* Set up. */
6283 pfd.fn = fn;
6284 pfd.data = data;
6285 pfd.include_nondeduced_p = include_nondeduced_p;
6287 /* Walk the tree. (Conceptually, we would like to walk without
6288 duplicates, but for_each_template_parm_r recursively calls
6289 for_each_template_parm, so we would need to reorganize a fair
6290 bit to use walk_tree_without_duplicates, so we keep our own
6291 visited list.) */
6292 if (visited)
6293 pfd.visited = visited;
6294 else
6295 pfd.visited = pointer_set_create ();
6296 result = cp_walk_tree (&t,
6297 for_each_template_parm_r,
6298 &pfd,
6299 pfd.visited) != NULL_TREE;
6301 /* Clean up. */
6302 if (!visited)
6304 pointer_set_destroy (pfd.visited);
6305 pfd.visited = 0;
6308 return result;
6311 /* Returns true if T depends on any template parameter. */
6314 uses_template_parms (tree t)
6316 bool dependent_p;
6317 int saved_processing_template_decl;
6319 saved_processing_template_decl = processing_template_decl;
6320 if (!saved_processing_template_decl)
6321 processing_template_decl = 1;
6322 if (TYPE_P (t))
6323 dependent_p = dependent_type_p (t);
6324 else if (TREE_CODE (t) == TREE_VEC)
6325 dependent_p = any_dependent_template_arguments_p (t);
6326 else if (TREE_CODE (t) == TREE_LIST)
6327 dependent_p = (uses_template_parms (TREE_VALUE (t))
6328 || uses_template_parms (TREE_CHAIN (t)));
6329 else if (TREE_CODE (t) == TYPE_DECL)
6330 dependent_p = dependent_type_p (TREE_TYPE (t));
6331 else if (DECL_P (t)
6332 || EXPR_P (t)
6333 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6334 || TREE_CODE (t) == OVERLOAD
6335 || TREE_CODE (t) == BASELINK
6336 || TREE_CODE (t) == IDENTIFIER_NODE
6337 || TREE_CODE (t) == TRAIT_EXPR
6338 || TREE_CODE (t) == CONSTRUCTOR
6339 || CONSTANT_CLASS_P (t))
6340 dependent_p = (type_dependent_expression_p (t)
6341 || value_dependent_expression_p (t));
6342 else
6344 gcc_assert (t == error_mark_node);
6345 dependent_p = false;
6348 processing_template_decl = saved_processing_template_decl;
6350 return dependent_p;
6353 /* Returns true if T depends on any template parameter with level LEVEL. */
6356 uses_template_parms_level (tree t, int level)
6358 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6359 /*include_nondeduced_p=*/true);
6362 static int tinst_depth;
6363 extern int max_tinst_depth;
6364 #ifdef GATHER_STATISTICS
6365 int depth_reached;
6366 #endif
6367 static int tinst_level_tick;
6368 static int last_template_error_tick;
6370 /* We're starting to instantiate D; record the template instantiation context
6371 for diagnostics and to restore it later. */
6373 static int
6374 push_tinst_level (tree d)
6376 struct tinst_level *new_level;
6378 if (tinst_depth >= max_tinst_depth)
6380 /* If the instantiation in question still has unbound template parms,
6381 we don't really care if we can't instantiate it, so just return.
6382 This happens with base instantiation for implicit `typename'. */
6383 if (uses_template_parms (d))
6384 return 0;
6386 last_template_error_tick = tinst_level_tick;
6387 error ("template instantiation depth exceeds maximum of %d (use "
6388 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6389 max_tinst_depth, d);
6391 print_instantiation_context ();
6393 return 0;
6396 new_level = GGC_NEW (struct tinst_level);
6397 new_level->decl = d;
6398 new_level->locus = input_location;
6399 new_level->in_system_header_p = in_system_header;
6400 new_level->next = current_tinst_level;
6401 current_tinst_level = new_level;
6403 ++tinst_depth;
6404 #ifdef GATHER_STATISTICS
6405 if (tinst_depth > depth_reached)
6406 depth_reached = tinst_depth;
6407 #endif
6409 ++tinst_level_tick;
6410 return 1;
6413 /* We're done instantiating this template; return to the instantiation
6414 context. */
6416 static void
6417 pop_tinst_level (void)
6419 /* Restore the filename and line number stashed away when we started
6420 this instantiation. */
6421 input_location = current_tinst_level->locus;
6422 current_tinst_level = current_tinst_level->next;
6423 --tinst_depth;
6424 ++tinst_level_tick;
6427 /* We're instantiating a deferred template; restore the template
6428 instantiation context in which the instantiation was requested, which
6429 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6431 static tree
6432 reopen_tinst_level (struct tinst_level *level)
6434 struct tinst_level *t;
6436 tinst_depth = 0;
6437 for (t = level; t; t = t->next)
6438 ++tinst_depth;
6440 current_tinst_level = level;
6441 pop_tinst_level ();
6442 return level->decl;
6445 /* Returns the TINST_LEVEL which gives the original instantiation
6446 context. */
6448 struct tinst_level *
6449 outermost_tinst_level (void)
6451 struct tinst_level *level = current_tinst_level;
6452 if (level)
6453 while (level->next)
6454 level = level->next;
6455 return level;
6458 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6460 bool
6461 parameter_of_template_p (tree parm, tree templ)
6463 tree parms;
6464 int i;
6466 if (!parm || !templ)
6467 return false;
6469 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6470 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6472 parms = DECL_TEMPLATE_PARMS (templ);
6473 parms = INNERMOST_TEMPLATE_PARMS (parms);
6475 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6476 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6477 return true;
6479 return false;
6482 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6483 vector of template arguments, as for tsubst.
6485 Returns an appropriate tsubst'd friend declaration. */
6487 static tree
6488 tsubst_friend_function (tree decl, tree args)
6490 tree new_friend;
6492 if (TREE_CODE (decl) == FUNCTION_DECL
6493 && DECL_TEMPLATE_INSTANTIATION (decl)
6494 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6495 /* This was a friend declared with an explicit template
6496 argument list, e.g.:
6498 friend void f<>(T);
6500 to indicate that f was a template instantiation, not a new
6501 function declaration. Now, we have to figure out what
6502 instantiation of what template. */
6504 tree template_id, arglist, fns;
6505 tree new_args;
6506 tree tmpl;
6507 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6509 /* Friend functions are looked up in the containing namespace scope.
6510 We must enter that scope, to avoid finding member functions of the
6511 current class with same name. */
6512 push_nested_namespace (ns);
6513 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6514 tf_warning_or_error, NULL_TREE,
6515 /*integral_constant_expression_p=*/false);
6516 pop_nested_namespace (ns);
6517 arglist = tsubst (DECL_TI_ARGS (decl), args,
6518 tf_warning_or_error, NULL_TREE);
6519 template_id = lookup_template_function (fns, arglist);
6521 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6522 tmpl = determine_specialization (template_id, new_friend,
6523 &new_args,
6524 /*need_member_template=*/0,
6525 TREE_VEC_LENGTH (args),
6526 tsk_none);
6527 return instantiate_template (tmpl, new_args, tf_error);
6530 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6532 /* The NEW_FRIEND will look like an instantiation, to the
6533 compiler, but is not an instantiation from the point of view of
6534 the language. For example, we might have had:
6536 template <class T> struct S {
6537 template <class U> friend void f(T, U);
6540 Then, in S<int>, template <class U> void f(int, U) is not an
6541 instantiation of anything. */
6542 if (new_friend == error_mark_node)
6543 return error_mark_node;
6545 DECL_USE_TEMPLATE (new_friend) = 0;
6546 if (TREE_CODE (decl) == TEMPLATE_DECL)
6548 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6549 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6550 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6553 /* The mangled name for the NEW_FRIEND is incorrect. The function
6554 is not a template instantiation and should not be mangled like
6555 one. Therefore, we forget the mangling here; we'll recompute it
6556 later if we need it. */
6557 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6559 SET_DECL_RTL (new_friend, NULL_RTX);
6560 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6563 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6565 tree old_decl;
6566 tree new_friend_template_info;
6567 tree new_friend_result_template_info;
6568 tree ns;
6569 int new_friend_is_defn;
6571 /* We must save some information from NEW_FRIEND before calling
6572 duplicate decls since that function will free NEW_FRIEND if
6573 possible. */
6574 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6575 new_friend_is_defn =
6576 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6577 (template_for_substitution (new_friend)))
6578 != NULL_TREE);
6579 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6581 /* This declaration is a `primary' template. */
6582 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6584 new_friend_result_template_info
6585 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6587 else
6588 new_friend_result_template_info = NULL_TREE;
6590 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6591 if (new_friend_is_defn)
6592 DECL_INITIAL (new_friend) = error_mark_node;
6594 /* Inside pushdecl_namespace_level, we will push into the
6595 current namespace. However, the friend function should go
6596 into the namespace of the template. */
6597 ns = decl_namespace_context (new_friend);
6598 push_nested_namespace (ns);
6599 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6600 pop_nested_namespace (ns);
6602 if (old_decl == error_mark_node)
6603 return error_mark_node;
6605 if (old_decl != new_friend)
6607 /* This new friend declaration matched an existing
6608 declaration. For example, given:
6610 template <class T> void f(T);
6611 template <class U> class C {
6612 template <class T> friend void f(T) {}
6615 the friend declaration actually provides the definition
6616 of `f', once C has been instantiated for some type. So,
6617 old_decl will be the out-of-class template declaration,
6618 while new_friend is the in-class definition.
6620 But, if `f' was called before this point, the
6621 instantiation of `f' will have DECL_TI_ARGS corresponding
6622 to `T' but not to `U', references to which might appear
6623 in the definition of `f'. Previously, the most general
6624 template for an instantiation of `f' was the out-of-class
6625 version; now it is the in-class version. Therefore, we
6626 run through all specialization of `f', adding to their
6627 DECL_TI_ARGS appropriately. In particular, they need a
6628 new set of outer arguments, corresponding to the
6629 arguments for this class instantiation.
6631 The same situation can arise with something like this:
6633 friend void f(int);
6634 template <class T> class C {
6635 friend void f(T) {}
6638 when `C<int>' is instantiated. Now, `f(int)' is defined
6639 in the class. */
6641 if (!new_friend_is_defn)
6642 /* On the other hand, if the in-class declaration does
6643 *not* provide a definition, then we don't want to alter
6644 existing definitions. We can just leave everything
6645 alone. */
6647 else
6649 /* Overwrite whatever template info was there before, if
6650 any, with the new template information pertaining to
6651 the declaration. */
6652 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6654 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6655 reregister_specialization (new_friend,
6656 most_general_template (old_decl),
6657 old_decl);
6658 else
6660 tree t;
6661 tree new_friend_args;
6663 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6664 = new_friend_result_template_info;
6666 new_friend_args = TI_ARGS (new_friend_template_info);
6667 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6668 t != NULL_TREE;
6669 t = TREE_CHAIN (t))
6671 tree spec = TREE_VALUE (t);
6673 DECL_TI_ARGS (spec)
6674 = add_outermost_template_args (new_friend_args,
6675 DECL_TI_ARGS (spec));
6678 /* Now, since specializations are always supposed to
6679 hang off of the most general template, we must move
6680 them. */
6681 t = most_general_template (old_decl);
6682 if (t != old_decl)
6684 DECL_TEMPLATE_SPECIALIZATIONS (t)
6685 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6686 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6687 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6692 /* The information from NEW_FRIEND has been merged into OLD_DECL
6693 by duplicate_decls. */
6694 new_friend = old_decl;
6697 else
6699 tree context = DECL_CONTEXT (new_friend);
6700 bool dependent_p;
6702 /* In the code
6703 template <class T> class C {
6704 template <class U> friend void C1<U>::f (); // case 1
6705 friend void C2<T>::f (); // case 2
6707 we only need to make sure CONTEXT is a complete type for
6708 case 2. To distinguish between the two cases, we note that
6709 CONTEXT of case 1 remains dependent type after tsubst while
6710 this isn't true for case 2. */
6711 ++processing_template_decl;
6712 dependent_p = dependent_type_p (context);
6713 --processing_template_decl;
6715 if (!dependent_p
6716 && !complete_type_or_else (context, NULL_TREE))
6717 return error_mark_node;
6719 if (COMPLETE_TYPE_P (context))
6721 /* Check to see that the declaration is really present, and,
6722 possibly obtain an improved declaration. */
6723 tree fn = check_classfn (context,
6724 new_friend, NULL_TREE);
6726 if (fn)
6727 new_friend = fn;
6731 return new_friend;
6734 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6735 template arguments, as for tsubst.
6737 Returns an appropriate tsubst'd friend type or error_mark_node on
6738 failure. */
6740 static tree
6741 tsubst_friend_class (tree friend_tmpl, tree args)
6743 tree friend_type;
6744 tree tmpl;
6745 tree context;
6747 context = DECL_CONTEXT (friend_tmpl);
6749 if (context)
6751 if (TREE_CODE (context) == NAMESPACE_DECL)
6752 push_nested_namespace (context);
6753 else
6754 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6757 /* Look for a class template declaration. We look for hidden names
6758 because two friend declarations of the same template are the
6759 same. For example, in:
6761 struct A {
6762 template <typename> friend class F;
6764 template <typename> struct B {
6765 template <typename> friend class F;
6768 both F templates are the same. */
6769 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6770 /*block_p=*/true, 0,
6771 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6773 /* But, if we don't find one, it might be because we're in a
6774 situation like this:
6776 template <class T>
6777 struct S {
6778 template <class U>
6779 friend struct S;
6782 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6783 for `S<int>', not the TEMPLATE_DECL. */
6784 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6786 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6787 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6790 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6792 /* The friend template has already been declared. Just
6793 check to see that the declarations match, and install any new
6794 default parameters. We must tsubst the default parameters,
6795 of course. We only need the innermost template parameters
6796 because that is all that redeclare_class_template will look
6797 at. */
6798 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6799 > TMPL_ARGS_DEPTH (args))
6801 tree parms;
6802 location_t saved_input_location;
6803 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6804 args, tf_warning_or_error);
6806 saved_input_location = input_location;
6807 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
6808 redeclare_class_template (TREE_TYPE (tmpl), parms);
6809 input_location = saved_input_location;
6813 friend_type = TREE_TYPE (tmpl);
6815 else
6817 /* The friend template has not already been declared. In this
6818 case, the instantiation of the template class will cause the
6819 injection of this template into the global scope. */
6820 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6821 if (tmpl == error_mark_node)
6822 return error_mark_node;
6824 /* The new TMPL is not an instantiation of anything, so we
6825 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6826 the new type because that is supposed to be the corresponding
6827 template decl, i.e., TMPL. */
6828 DECL_USE_TEMPLATE (tmpl) = 0;
6829 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6830 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6831 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6832 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6834 /* Inject this template into the global scope. */
6835 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6838 if (context)
6840 if (TREE_CODE (context) == NAMESPACE_DECL)
6841 pop_nested_namespace (context);
6842 else
6843 pop_nested_class ();
6846 return friend_type;
6849 /* Returns zero if TYPE cannot be completed later due to circularity.
6850 Otherwise returns one. */
6852 static int
6853 can_complete_type_without_circularity (tree type)
6855 if (type == NULL_TREE || type == error_mark_node)
6856 return 0;
6857 else if (COMPLETE_TYPE_P (type))
6858 return 1;
6859 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6860 return can_complete_type_without_circularity (TREE_TYPE (type));
6861 else if (CLASS_TYPE_P (type)
6862 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6863 return 0;
6864 else
6865 return 1;
6868 /* Apply any attributes which had to be deferred until instantiation
6869 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6870 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6872 static void
6873 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
6874 tree args, tsubst_flags_t complain, tree in_decl)
6876 tree last_dep = NULL_TREE;
6877 tree t;
6878 tree *p;
6880 for (t = attributes; t; t = TREE_CHAIN (t))
6881 if (ATTR_IS_DEPENDENT (t))
6883 last_dep = t;
6884 attributes = copy_list (attributes);
6885 break;
6888 if (DECL_P (*decl_p))
6890 if (TREE_TYPE (*decl_p) == error_mark_node)
6891 return;
6892 p = &DECL_ATTRIBUTES (*decl_p);
6894 else
6895 p = &TYPE_ATTRIBUTES (*decl_p);
6897 if (last_dep)
6899 tree late_attrs = NULL_TREE;
6900 tree *q = &late_attrs;
6902 for (*p = attributes; *p; )
6904 t = *p;
6905 if (ATTR_IS_DEPENDENT (t))
6907 *p = TREE_CHAIN (t);
6908 TREE_CHAIN (t) = NULL_TREE;
6909 /* If the first attribute argument is an identifier, don't
6910 pass it through tsubst. Attributes like mode, format,
6911 cleanup and several target specific attributes expect it
6912 unmodified. */
6913 if (TREE_VALUE (t)
6914 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
6915 && TREE_VALUE (TREE_VALUE (t))
6916 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
6917 == IDENTIFIER_NODE))
6919 tree chain
6920 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
6921 in_decl,
6922 /*integral_constant_expression_p=*/false);
6923 if (chain != TREE_CHAIN (TREE_VALUE (t)))
6924 TREE_VALUE (t)
6925 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
6926 chain);
6928 else
6929 TREE_VALUE (t)
6930 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
6931 /*integral_constant_expression_p=*/false);
6932 *q = t;
6933 q = &TREE_CHAIN (t);
6935 else
6936 p = &TREE_CHAIN (t);
6939 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
6943 /* Perform (or defer) access check for typedefs that were referenced
6944 from within the template TMPL code.
6945 This is a subroutine of instantiate_template and instantiate_class_template.
6946 TMPL is the template to consider and TARGS is the list of arguments of
6947 that template. */
6949 static void
6950 perform_typedefs_access_check (tree tmpl, tree targs)
6952 tree t;
6954 if (!tmpl
6955 || (TREE_CODE (tmpl) != RECORD_TYPE
6956 && TREE_CODE (tmpl) != FUNCTION_DECL))
6957 return;
6959 for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
6961 tree type_decl = TREE_PURPOSE (t);
6962 tree type_scope = TREE_VALUE (t);
6964 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
6965 continue;
6967 if (uses_template_parms (type_decl))
6968 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
6969 if (uses_template_parms (type_scope))
6970 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
6972 perform_or_defer_access_check (TYPE_BINFO (type_scope),
6973 type_decl, type_decl);
6977 tree
6978 instantiate_class_template (tree type)
6980 tree templ, args, pattern, t, member;
6981 tree typedecl;
6982 tree pbinfo;
6983 tree base_list;
6985 if (type == error_mark_node)
6986 return error_mark_node;
6988 if (TYPE_BEING_DEFINED (type)
6989 || COMPLETE_TYPE_P (type)
6990 || dependent_type_p (type))
6991 return type;
6993 /* Figure out which template is being instantiated. */
6994 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6995 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6997 /* Determine what specialization of the original template to
6998 instantiate. */
6999 t = most_specialized_class (type, templ);
7000 if (t == error_mark_node)
7002 TYPE_BEING_DEFINED (type) = 1;
7003 return error_mark_node;
7005 else if (t)
7007 /* This TYPE is actually an instantiation of a partial
7008 specialization. We replace the innermost set of ARGS with
7009 the arguments appropriate for substitution. For example,
7010 given:
7012 template <class T> struct S {};
7013 template <class T> struct S<T*> {};
7015 and supposing that we are instantiating S<int*>, ARGS will
7016 presently be {int*} -- but we need {int}. */
7017 pattern = TREE_TYPE (t);
7018 args = TREE_PURPOSE (t);
7020 else
7022 pattern = TREE_TYPE (templ);
7023 args = CLASSTYPE_TI_ARGS (type);
7026 /* If the template we're instantiating is incomplete, then clearly
7027 there's nothing we can do. */
7028 if (!COMPLETE_TYPE_P (pattern))
7029 return type;
7031 /* If we've recursively instantiated too many templates, stop. */
7032 if (! push_tinst_level (type))
7033 return type;
7035 /* Now we're really doing the instantiation. Mark the type as in
7036 the process of being defined. */
7037 TYPE_BEING_DEFINED (type) = 1;
7039 /* We may be in the middle of deferred access check. Disable
7040 it now. */
7041 push_deferring_access_checks (dk_no_deferred);
7043 push_to_top_level ();
7045 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7047 /* Set the input location to the most specialized template definition.
7048 This is needed if tsubsting causes an error. */
7049 typedecl = TYPE_MAIN_DECL (pattern);
7050 input_location = DECL_SOURCE_LOCATION (typedecl);
7052 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7053 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7054 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7055 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7056 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7057 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7058 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7059 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7060 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7061 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7062 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7063 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7064 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7065 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7066 if (ANON_AGGR_TYPE_P (pattern))
7067 SET_ANON_AGGR_TYPE_P (type);
7068 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7070 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7071 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7074 pbinfo = TYPE_BINFO (pattern);
7076 /* We should never instantiate a nested class before its enclosing
7077 class; we need to look up the nested class by name before we can
7078 instantiate it, and that lookup should instantiate the enclosing
7079 class. */
7080 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7081 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7082 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7084 base_list = NULL_TREE;
7085 if (BINFO_N_BASE_BINFOS (pbinfo))
7087 tree pbase_binfo;
7088 tree context = TYPE_CONTEXT (type);
7089 tree pushed_scope;
7090 int i;
7092 /* We must enter the scope containing the type, as that is where
7093 the accessibility of types named in dependent bases are
7094 looked up from. */
7095 pushed_scope = push_scope (context ? context : global_namespace);
7097 /* Substitute into each of the bases to determine the actual
7098 basetypes. */
7099 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7101 tree base;
7102 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7103 tree expanded_bases = NULL_TREE;
7104 int idx, len = 1;
7106 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7108 expanded_bases =
7109 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7110 args, tf_error, NULL_TREE);
7111 if (expanded_bases == error_mark_node)
7112 continue;
7114 len = TREE_VEC_LENGTH (expanded_bases);
7117 for (idx = 0; idx < len; idx++)
7119 if (expanded_bases)
7120 /* Extract the already-expanded base class. */
7121 base = TREE_VEC_ELT (expanded_bases, idx);
7122 else
7123 /* Substitute to figure out the base class. */
7124 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7125 NULL_TREE);
7127 if (base == error_mark_node)
7128 continue;
7130 base_list = tree_cons (access, base, base_list);
7131 if (BINFO_VIRTUAL_P (pbase_binfo))
7132 TREE_TYPE (base_list) = integer_type_node;
7136 /* The list is now in reverse order; correct that. */
7137 base_list = nreverse (base_list);
7139 if (pushed_scope)
7140 pop_scope (pushed_scope);
7142 /* Now call xref_basetypes to set up all the base-class
7143 information. */
7144 xref_basetypes (type, base_list);
7146 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7147 (int) ATTR_FLAG_TYPE_IN_PLACE,
7148 args, tf_error, NULL_TREE);
7150 /* Now that our base classes are set up, enter the scope of the
7151 class, so that name lookups into base classes, etc. will work
7152 correctly. This is precisely analogous to what we do in
7153 begin_class_definition when defining an ordinary non-template
7154 class, except we also need to push the enclosing classes. */
7155 push_nested_class (type);
7157 /* Now members are processed in the order of declaration. */
7158 for (member = CLASSTYPE_DECL_LIST (pattern);
7159 member; member = TREE_CHAIN (member))
7161 tree t = TREE_VALUE (member);
7163 if (TREE_PURPOSE (member))
7165 if (TYPE_P (t))
7167 /* Build new CLASSTYPE_NESTED_UTDS. */
7169 tree newtag;
7170 bool class_template_p;
7172 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7173 && TYPE_LANG_SPECIFIC (t)
7174 && CLASSTYPE_IS_TEMPLATE (t));
7175 /* If the member is a class template, then -- even after
7176 substitution -- there may be dependent types in the
7177 template argument list for the class. We increment
7178 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7179 that function will assume that no types are dependent
7180 when outside of a template. */
7181 if (class_template_p)
7182 ++processing_template_decl;
7183 newtag = tsubst (t, args, tf_error, NULL_TREE);
7184 if (class_template_p)
7185 --processing_template_decl;
7186 if (newtag == error_mark_node)
7187 continue;
7189 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7191 tree name = TYPE_IDENTIFIER (t);
7193 if (class_template_p)
7194 /* Unfortunately, lookup_template_class sets
7195 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7196 instantiation (i.e., for the type of a member
7197 template class nested within a template class.)
7198 This behavior is required for
7199 maybe_process_partial_specialization to work
7200 correctly, but is not accurate in this case;
7201 the TAG is not an instantiation of anything.
7202 (The corresponding TEMPLATE_DECL is an
7203 instantiation, but the TYPE is not.) */
7204 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7206 /* Now, we call pushtag to put this NEWTAG into the scope of
7207 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7208 pushtag calling push_template_decl. We don't have to do
7209 this for enums because it will already have been done in
7210 tsubst_enum. */
7211 if (name)
7212 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7213 pushtag (name, newtag, /*tag_scope=*/ts_current);
7216 else if (TREE_CODE (t) == FUNCTION_DECL
7217 || DECL_FUNCTION_TEMPLATE_P (t))
7219 /* Build new TYPE_METHODS. */
7220 tree r;
7222 if (TREE_CODE (t) == TEMPLATE_DECL)
7223 ++processing_template_decl;
7224 r = tsubst (t, args, tf_error, NULL_TREE);
7225 if (TREE_CODE (t) == TEMPLATE_DECL)
7226 --processing_template_decl;
7227 set_current_access_from_decl (r);
7228 finish_member_declaration (r);
7230 else
7232 /* Build new TYPE_FIELDS. */
7233 if (TREE_CODE (t) == STATIC_ASSERT)
7235 tree condition =
7236 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7237 tf_warning_or_error, NULL_TREE,
7238 /*integral_constant_expression_p=*/true);
7239 finish_static_assert (condition,
7240 STATIC_ASSERT_MESSAGE (t),
7241 STATIC_ASSERT_SOURCE_LOCATION (t),
7242 /*member_p=*/true);
7244 else if (TREE_CODE (t) != CONST_DECL)
7246 tree r;
7248 /* The file and line for this declaration, to
7249 assist in error message reporting. Since we
7250 called push_tinst_level above, we don't need to
7251 restore these. */
7252 input_location = DECL_SOURCE_LOCATION (t);
7254 if (TREE_CODE (t) == TEMPLATE_DECL)
7255 ++processing_template_decl;
7256 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7257 if (TREE_CODE (t) == TEMPLATE_DECL)
7258 --processing_template_decl;
7259 if (TREE_CODE (r) == VAR_DECL)
7261 /* In [temp.inst]:
7263 [t]he initialization (and any associated
7264 side-effects) of a static data member does
7265 not occur unless the static data member is
7266 itself used in a way that requires the
7267 definition of the static data member to
7268 exist.
7270 Therefore, we do not substitute into the
7271 initialized for the static data member here. */
7272 finish_static_data_member_decl
7274 /*init=*/NULL_TREE,
7275 /*init_const_expr_p=*/false,
7276 /*asmspec_tree=*/NULL_TREE,
7277 /*flags=*/0);
7278 if (DECL_INITIALIZED_IN_CLASS_P (r))
7279 check_static_variable_definition (r, TREE_TYPE (r));
7281 else if (TREE_CODE (r) == FIELD_DECL)
7283 /* Determine whether R has a valid type and can be
7284 completed later. If R is invalid, then it is
7285 replaced by error_mark_node so that it will not be
7286 added to TYPE_FIELDS. */
7287 tree rtype = TREE_TYPE (r);
7288 if (can_complete_type_without_circularity (rtype))
7289 complete_type (rtype);
7291 if (!COMPLETE_TYPE_P (rtype))
7293 cxx_incomplete_type_error (r, rtype);
7294 r = error_mark_node;
7298 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7299 such a thing will already have been added to the field
7300 list by tsubst_enum in finish_member_declaration in the
7301 CLASSTYPE_NESTED_UTDS case above. */
7302 if (!(TREE_CODE (r) == TYPE_DECL
7303 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7304 && DECL_ARTIFICIAL (r)))
7306 set_current_access_from_decl (r);
7307 finish_member_declaration (r);
7312 else
7314 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7316 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7318 tree friend_type = t;
7319 bool adjust_processing_template_decl = false;
7321 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7323 /* template <class T> friend class C; */
7324 friend_type = tsubst_friend_class (friend_type, args);
7325 adjust_processing_template_decl = true;
7327 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7329 /* template <class T> friend class C::D; */
7330 friend_type = tsubst (friend_type, args,
7331 tf_warning_or_error, NULL_TREE);
7332 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7333 friend_type = TREE_TYPE (friend_type);
7334 adjust_processing_template_decl = true;
7336 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7338 /* This could be either
7340 friend class T::C;
7342 when dependent_type_p is false or
7344 template <class U> friend class T::C;
7346 otherwise. */
7347 friend_type = tsubst (friend_type, args,
7348 tf_warning_or_error, NULL_TREE);
7349 /* Bump processing_template_decl for correct
7350 dependent_type_p calculation. */
7351 ++processing_template_decl;
7352 if (dependent_type_p (friend_type))
7353 adjust_processing_template_decl = true;
7354 --processing_template_decl;
7356 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7357 && hidden_name_p (TYPE_NAME (friend_type)))
7359 /* friend class C;
7361 where C hasn't been declared yet. Let's lookup name
7362 from namespace scope directly, bypassing any name that
7363 come from dependent base class. */
7364 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7366 /* The call to xref_tag_from_type does injection for friend
7367 classes. */
7368 push_nested_namespace (ns);
7369 friend_type =
7370 xref_tag_from_type (friend_type, NULL_TREE,
7371 /*tag_scope=*/ts_current);
7372 pop_nested_namespace (ns);
7374 else if (uses_template_parms (friend_type))
7375 /* friend class C<T>; */
7376 friend_type = tsubst (friend_type, args,
7377 tf_warning_or_error, NULL_TREE);
7378 /* Otherwise it's
7380 friend class C;
7382 where C is already declared or
7384 friend class C<int>;
7386 We don't have to do anything in these cases. */
7388 if (adjust_processing_template_decl)
7389 /* Trick make_friend_class into realizing that the friend
7390 we're adding is a template, not an ordinary class. It's
7391 important that we use make_friend_class since it will
7392 perform some error-checking and output cross-reference
7393 information. */
7394 ++processing_template_decl;
7396 if (friend_type != error_mark_node)
7397 make_friend_class (type, friend_type, /*complain=*/false);
7399 if (adjust_processing_template_decl)
7400 --processing_template_decl;
7402 else
7404 /* Build new DECL_FRIENDLIST. */
7405 tree r;
7407 /* The file and line for this declaration, to
7408 assist in error message reporting. Since we
7409 called push_tinst_level above, we don't need to
7410 restore these. */
7411 input_location = DECL_SOURCE_LOCATION (t);
7413 if (TREE_CODE (t) == TEMPLATE_DECL)
7415 ++processing_template_decl;
7416 push_deferring_access_checks (dk_no_check);
7419 r = tsubst_friend_function (t, args);
7420 add_friend (type, r, /*complain=*/false);
7421 if (TREE_CODE (t) == TEMPLATE_DECL)
7423 pop_deferring_access_checks ();
7424 --processing_template_decl;
7430 /* Set the file and line number information to whatever is given for
7431 the class itself. This puts error messages involving generated
7432 implicit functions at a predictable point, and the same point
7433 that would be used for non-template classes. */
7434 input_location = DECL_SOURCE_LOCATION (typedecl);
7436 unreverse_member_declarations (type);
7437 finish_struct_1 (type);
7438 TYPE_BEING_DEFINED (type) = 0;
7440 /* Now that the class is complete, instantiate default arguments for
7441 any member functions. We don't do this earlier because the
7442 default arguments may reference members of the class. */
7443 if (!PRIMARY_TEMPLATE_P (templ))
7444 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7445 if (TREE_CODE (t) == FUNCTION_DECL
7446 /* Implicitly generated member functions will not have template
7447 information; they are not instantiations, but instead are
7448 created "fresh" for each instantiation. */
7449 && DECL_TEMPLATE_INFO (t))
7450 tsubst_default_arguments (t);
7452 /* Some typedefs referenced from within the template code need to be access
7453 checked at template instantiation time, i.e now. These types were
7454 added to the template at parsing time. Let's get those and perform
7455 the access checks then. */
7456 perform_typedefs_access_check (pattern, args);
7457 perform_deferred_access_checks ();
7458 pop_nested_class ();
7459 pop_from_top_level ();
7460 pop_deferring_access_checks ();
7461 pop_tinst_level ();
7463 /* The vtable for a template class can be emitted in any translation
7464 unit in which the class is instantiated. When there is no key
7465 method, however, finish_struct_1 will already have added TYPE to
7466 the keyed_classes list. */
7467 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7468 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7470 return type;
7473 static tree
7474 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7476 tree r;
7478 if (!t)
7479 r = t;
7480 else if (TYPE_P (t))
7481 r = tsubst (t, args, complain, in_decl);
7482 else
7484 r = tsubst_expr (t, args, complain, in_decl,
7485 /*integral_constant_expression_p=*/true);
7486 r = fold_non_dependent_expr (r);
7488 return r;
7491 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7492 NONTYPE_ARGUMENT_PACK. */
7494 static tree
7495 make_fnparm_pack (tree spec_parm)
7497 /* Collect all of the extra "packed" parameters into an
7498 argument pack. */
7499 tree parmvec;
7500 tree parmtypevec;
7501 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7502 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
7503 int i, len = list_length (spec_parm);
7505 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7506 parmvec = make_tree_vec (len);
7507 parmtypevec = make_tree_vec (len);
7508 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7510 TREE_VEC_ELT (parmvec, i) = spec_parm;
7511 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7514 /* Build the argument packs. */
7515 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7516 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7517 TREE_TYPE (argpack) = argtypepack;
7519 return argpack;
7522 /* Substitute ARGS into T, which is an pack expansion
7523 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7524 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7525 (if only a partial substitution could be performed) or
7526 ERROR_MARK_NODE if there was an error. */
7527 tree
7528 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7529 tree in_decl)
7531 tree pattern;
7532 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7533 tree first_arg_pack; int i, len = -1;
7534 tree result;
7535 int incomplete = 0;
7536 bool very_local_specializations = false;
7538 gcc_assert (PACK_EXPANSION_P (t));
7539 pattern = PACK_EXPANSION_PATTERN (t);
7541 /* Determine the argument packs that will instantiate the parameter
7542 packs used in the expansion expression. While we're at it,
7543 compute the number of arguments to be expanded and make sure it
7544 is consistent. */
7545 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7546 pack = TREE_CHAIN (pack))
7548 tree parm_pack = TREE_VALUE (pack);
7549 tree arg_pack = NULL_TREE;
7550 tree orig_arg = NULL_TREE;
7552 if (TREE_CODE (parm_pack) == PARM_DECL)
7554 arg_pack = retrieve_local_specialization (parm_pack);
7555 if (arg_pack == NULL_TREE)
7557 /* This can happen for a parameter name used later in a function
7558 declaration (such as in a late-specified return type). Just
7559 make a dummy decl, since it's only used for its type. */
7560 gcc_assert (cp_unevaluated_operand != 0);
7561 arg_pack = tsubst_decl (parm_pack, args, complain);
7562 arg_pack = make_fnparm_pack (arg_pack);
7565 else
7567 int level, idx, levels;
7568 template_parm_level_and_index (parm_pack, &level, &idx);
7570 levels = TMPL_ARGS_DEPTH (args);
7571 if (level <= levels)
7572 arg_pack = TMPL_ARG (args, level, idx);
7575 orig_arg = arg_pack;
7576 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7577 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7579 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7580 /* This can only happen if we forget to expand an argument
7581 pack somewhere else. Just return an error, silently. */
7583 result = make_tree_vec (1);
7584 TREE_VEC_ELT (result, 0) = error_mark_node;
7585 return result;
7588 if (arg_pack
7589 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7590 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7592 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7593 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7594 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7595 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7596 /* The argument pack that the parameter maps to is just an
7597 expansion of the parameter itself, such as one would
7598 find in the implicit typedef of a class inside the
7599 class itself. Consider this parameter "unsubstituted",
7600 so that we will maintain the outer pack expansion. */
7601 arg_pack = NULL_TREE;
7604 if (arg_pack)
7606 int my_len =
7607 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7609 /* It's all-or-nothing with incomplete argument packs. */
7610 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7611 return error_mark_node;
7613 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7614 incomplete = 1;
7616 if (len < 0)
7618 len = my_len;
7619 first_arg_pack = arg_pack;
7621 else if (len != my_len)
7623 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7624 error ("mismatched argument pack lengths while expanding "
7625 "%<%T%>",
7626 pattern);
7627 else
7628 error ("mismatched argument pack lengths while expanding "
7629 "%<%E%>",
7630 pattern);
7631 return error_mark_node;
7634 /* Keep track of the parameter packs and their corresponding
7635 argument packs. */
7636 packs = tree_cons (parm_pack, arg_pack, packs);
7637 TREE_TYPE (packs) = orig_arg;
7639 else
7640 /* We can't substitute for this parameter pack. */
7641 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7642 TREE_VALUE (pack),
7643 unsubstituted_packs);
7646 /* We cannot expand this expansion expression, because we don't have
7647 all of the argument packs we need. Substitute into the pattern
7648 and return a PACK_EXPANSION_*. The caller will need to deal with
7649 that. */
7650 if (unsubstituted_packs)
7651 return make_pack_expansion (tsubst (pattern, args, complain,
7652 in_decl));
7654 /* We could not find any argument packs that work. */
7655 if (len < 0)
7656 return error_mark_node;
7658 if (!local_specializations)
7660 /* We're in a late-specified return type, so we don't have a local
7661 specializations table. Create one for doing this expansion. */
7662 very_local_specializations = true;
7663 local_specializations = htab_create (37,
7664 hash_local_specialization,
7665 eq_local_specializations,
7666 NULL);
7669 /* For each argument in each argument pack, substitute into the
7670 pattern. */
7671 result = make_tree_vec (len + incomplete);
7672 for (i = 0; i < len + incomplete; ++i)
7674 /* For parameter pack, change the substitution of the parameter
7675 pack to the ith argument in its argument pack, then expand
7676 the pattern. */
7677 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7679 tree parm = TREE_PURPOSE (pack);
7681 if (TREE_CODE (parm) == PARM_DECL)
7683 /* Select the Ith argument from the pack. */
7684 tree arg = make_node (ARGUMENT_PACK_SELECT);
7685 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7686 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7687 mark_used (parm);
7688 register_local_specialization (arg, parm);
7690 else
7692 tree value = parm;
7693 int idx, level;
7694 template_parm_level_and_index (parm, &level, &idx);
7696 if (i < len)
7698 /* Select the Ith argument from the pack. */
7699 value = make_node (ARGUMENT_PACK_SELECT);
7700 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7701 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7704 /* Update the corresponding argument. */
7705 TMPL_ARG (args, level, idx) = value;
7709 /* Substitute into the PATTERN with the altered arguments. */
7710 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7711 TREE_VEC_ELT (result, i) =
7712 tsubst_expr (pattern, args, complain, in_decl,
7713 /*integral_constant_expression_p=*/false);
7714 else
7715 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7717 if (i == len)
7718 /* When we have incomplete argument packs, the last "expanded"
7719 result is itself a pack expansion, which allows us
7720 to deduce more arguments. */
7721 TREE_VEC_ELT (result, i) =
7722 make_pack_expansion (TREE_VEC_ELT (result, i));
7724 if (TREE_VEC_ELT (result, i) == error_mark_node)
7726 result = error_mark_node;
7727 break;
7731 /* Update ARGS to restore the substitution from parameter packs to
7732 their argument packs. */
7733 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7735 tree parm = TREE_PURPOSE (pack);
7737 if (TREE_CODE (parm) == PARM_DECL)
7738 register_local_specialization (TREE_TYPE (pack), parm);
7739 else
7741 int idx, level;
7742 template_parm_level_and_index (parm, &level, &idx);
7744 /* Update the corresponding argument. */
7745 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7746 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7747 TREE_TYPE (pack);
7748 else
7749 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7753 if (very_local_specializations)
7755 htab_delete (local_specializations);
7756 local_specializations = NULL;
7759 return result;
7762 /* Substitute ARGS into the vector or list of template arguments T. */
7764 static tree
7765 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7767 tree orig_t = t;
7768 int len = TREE_VEC_LENGTH (t);
7769 int need_new = 0, i, expanded_len_adjust = 0, out;
7770 tree *elts = (tree *) alloca (len * sizeof (tree));
7772 for (i = 0; i < len; i++)
7774 tree orig_arg = TREE_VEC_ELT (t, i);
7775 tree new_arg;
7777 if (TREE_CODE (orig_arg) == TREE_VEC)
7778 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7779 else if (PACK_EXPANSION_P (orig_arg))
7781 /* Substitute into an expansion expression. */
7782 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7784 if (TREE_CODE (new_arg) == TREE_VEC)
7785 /* Add to the expanded length adjustment the number of
7786 expanded arguments. We subtract one from this
7787 measurement, because the argument pack expression
7788 itself is already counted as 1 in
7789 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7790 the argument pack is empty. */
7791 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7793 else if (ARGUMENT_PACK_P (orig_arg))
7795 /* Substitute into each of the arguments. */
7796 new_arg = make_node (TREE_CODE (orig_arg));
7798 SET_ARGUMENT_PACK_ARGS (
7799 new_arg,
7800 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7801 args, complain, in_decl));
7803 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7804 new_arg = error_mark_node;
7806 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7807 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7808 complain, in_decl);
7809 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7811 if (TREE_TYPE (new_arg) == error_mark_node)
7812 new_arg = error_mark_node;
7815 else
7816 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7818 if (new_arg == error_mark_node)
7819 return error_mark_node;
7821 elts[i] = new_arg;
7822 if (new_arg != orig_arg)
7823 need_new = 1;
7826 if (!need_new)
7827 return t;
7829 /* Make space for the expanded arguments coming from template
7830 argument packs. */
7831 t = make_tree_vec (len + expanded_len_adjust);
7832 for (i = 0, out = 0; i < len; i++)
7834 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7835 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7836 && TREE_CODE (elts[i]) == TREE_VEC)
7838 int idx;
7840 /* Now expand the template argument pack "in place". */
7841 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7842 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7844 else
7846 TREE_VEC_ELT (t, out) = elts[i];
7847 out++;
7851 return t;
7854 /* Return the result of substituting ARGS into the template parameters
7855 given by PARMS. If there are m levels of ARGS and m + n levels of
7856 PARMS, then the result will contain n levels of PARMS. For
7857 example, if PARMS is `template <class T> template <class U>
7858 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7859 result will be `template <int*, double, class V>'. */
7861 static tree
7862 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7864 tree r = NULL_TREE;
7865 tree* new_parms;
7867 /* When substituting into a template, we must set
7868 PROCESSING_TEMPLATE_DECL as the template parameters may be
7869 dependent if they are based on one-another, and the dependency
7870 predicates are short-circuit outside of templates. */
7871 ++processing_template_decl;
7873 for (new_parms = &r;
7874 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7875 new_parms = &(TREE_CHAIN (*new_parms)),
7876 parms = TREE_CHAIN (parms))
7878 tree new_vec =
7879 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7880 int i;
7882 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7884 tree tuple;
7885 tree default_value;
7886 tree parm_decl;
7888 if (parms == error_mark_node)
7889 continue;
7891 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7893 if (tuple == error_mark_node)
7894 continue;
7896 default_value = TREE_PURPOSE (tuple);
7897 parm_decl = TREE_VALUE (tuple);
7899 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7900 if (TREE_CODE (parm_decl) == PARM_DECL
7901 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7902 parm_decl = error_mark_node;
7903 default_value = tsubst_template_arg (default_value, args,
7904 complain, NULL_TREE);
7906 tuple = build_tree_list (default_value, parm_decl);
7907 TREE_VEC_ELT (new_vec, i) = tuple;
7910 *new_parms =
7911 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7912 - TMPL_ARGS_DEPTH (args)),
7913 new_vec, NULL_TREE);
7916 --processing_template_decl;
7918 return r;
7921 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7922 type T. If T is not an aggregate or enumeration type, it is
7923 handled as if by tsubst. IN_DECL is as for tsubst. If
7924 ENTERING_SCOPE is nonzero, T is the context for a template which
7925 we are presently tsubst'ing. Return the substituted value. */
7927 static tree
7928 tsubst_aggr_type (tree t,
7929 tree args,
7930 tsubst_flags_t complain,
7931 tree in_decl,
7932 int entering_scope)
7934 if (t == NULL_TREE)
7935 return NULL_TREE;
7937 switch (TREE_CODE (t))
7939 case RECORD_TYPE:
7940 if (TYPE_PTRMEMFUNC_P (t))
7941 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7943 /* Else fall through. */
7944 case ENUMERAL_TYPE:
7945 case UNION_TYPE:
7946 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
7948 tree argvec;
7949 tree context;
7950 tree r;
7951 int saved_unevaluated_operand;
7952 int saved_inhibit_evaluation_warnings;
7954 /* In "sizeof(X<I>)" we need to evaluate "I". */
7955 saved_unevaluated_operand = cp_unevaluated_operand;
7956 cp_unevaluated_operand = 0;
7957 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7958 c_inhibit_evaluation_warnings = 0;
7960 /* First, determine the context for the type we are looking
7961 up. */
7962 context = TYPE_CONTEXT (t);
7963 if (context)
7965 context = tsubst_aggr_type (context, args, complain,
7966 in_decl, /*entering_scope=*/1);
7967 /* If context is a nested class inside a class template,
7968 it may still need to be instantiated (c++/33959). */
7969 if (TYPE_P (context))
7970 context = complete_type (context);
7973 /* Then, figure out what arguments are appropriate for the
7974 type we are trying to find. For example, given:
7976 template <class T> struct S;
7977 template <class T, class U> void f(T, U) { S<U> su; }
7979 and supposing that we are instantiating f<int, double>,
7980 then our ARGS will be {int, double}, but, when looking up
7981 S we only want {double}. */
7982 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7983 complain, in_decl);
7984 if (argvec == error_mark_node)
7985 r = error_mark_node;
7986 else
7988 r = lookup_template_class (t, argvec, in_decl, context,
7989 entering_scope, complain);
7990 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7993 cp_unevaluated_operand = saved_unevaluated_operand;
7994 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7996 return r;
7998 else
7999 /* This is not a template type, so there's nothing to do. */
8000 return t;
8002 default:
8003 return tsubst (t, args, complain, in_decl);
8007 /* Substitute into the default argument ARG (a default argument for
8008 FN), which has the indicated TYPE. */
8010 tree
8011 tsubst_default_argument (tree fn, tree type, tree arg)
8013 tree saved_class_ptr = NULL_TREE;
8014 tree saved_class_ref = NULL_TREE;
8016 /* This default argument came from a template. Instantiate the
8017 default argument here, not in tsubst. In the case of
8018 something like:
8020 template <class T>
8021 struct S {
8022 static T t();
8023 void f(T = t());
8026 we must be careful to do name lookup in the scope of S<T>,
8027 rather than in the current class. */
8028 push_access_scope (fn);
8029 /* The "this" pointer is not valid in a default argument. */
8030 if (cfun)
8032 saved_class_ptr = current_class_ptr;
8033 cp_function_chain->x_current_class_ptr = NULL_TREE;
8034 saved_class_ref = current_class_ref;
8035 cp_function_chain->x_current_class_ref = NULL_TREE;
8038 push_deferring_access_checks(dk_no_deferred);
8039 /* The default argument expression may cause implicitly defined
8040 member functions to be synthesized, which will result in garbage
8041 collection. We must treat this situation as if we were within
8042 the body of function so as to avoid collecting live data on the
8043 stack. */
8044 ++function_depth;
8045 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8046 tf_warning_or_error, NULL_TREE,
8047 /*integral_constant_expression_p=*/false);
8048 --function_depth;
8049 pop_deferring_access_checks();
8051 /* Restore the "this" pointer. */
8052 if (cfun)
8054 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8055 cp_function_chain->x_current_class_ref = saved_class_ref;
8058 pop_access_scope (fn);
8060 /* Make sure the default argument is reasonable. */
8061 arg = check_default_argument (type, arg);
8063 return arg;
8066 /* Substitute into all the default arguments for FN. */
8068 static void
8069 tsubst_default_arguments (tree fn)
8071 tree arg;
8072 tree tmpl_args;
8074 tmpl_args = DECL_TI_ARGS (fn);
8076 /* If this function is not yet instantiated, we certainly don't need
8077 its default arguments. */
8078 if (uses_template_parms (tmpl_args))
8079 return;
8081 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8082 arg;
8083 arg = TREE_CHAIN (arg))
8084 if (TREE_PURPOSE (arg))
8085 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8086 TREE_VALUE (arg),
8087 TREE_PURPOSE (arg));
8090 /* Substitute the ARGS into the T, which is a _DECL. Return the
8091 result of the substitution. Issue error and warning messages under
8092 control of COMPLAIN. */
8094 static tree
8095 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8097 location_t saved_loc;
8098 tree r = NULL_TREE;
8099 tree in_decl = t;
8101 /* Set the filename and linenumber to improve error-reporting. */
8102 saved_loc = input_location;
8103 input_location = DECL_SOURCE_LOCATION (t);
8105 switch (TREE_CODE (t))
8107 case TEMPLATE_DECL:
8109 /* We can get here when processing a member function template,
8110 member class template, and template template parameter of
8111 a template class. */
8112 tree decl = DECL_TEMPLATE_RESULT (t);
8113 tree spec;
8114 tree tmpl_args;
8115 tree full_args;
8117 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8119 /* Template template parameter is treated here. */
8120 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8121 if (new_type == error_mark_node)
8122 return error_mark_node;
8124 r = copy_decl (t);
8125 TREE_CHAIN (r) = NULL_TREE;
8126 TREE_TYPE (r) = new_type;
8127 DECL_TEMPLATE_RESULT (r)
8128 = build_decl (DECL_SOURCE_LOCATION (decl),
8129 TYPE_DECL, DECL_NAME (decl), new_type);
8130 DECL_TEMPLATE_PARMS (r)
8131 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8132 complain);
8133 TYPE_NAME (new_type) = r;
8134 break;
8137 /* We might already have an instance of this template.
8138 The ARGS are for the surrounding class type, so the
8139 full args contain the tsubst'd args for the context,
8140 plus the innermost args from the template decl. */
8141 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8142 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8143 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8144 /* Because this is a template, the arguments will still be
8145 dependent, even after substitution. If
8146 PROCESSING_TEMPLATE_DECL is not set, the dependency
8147 predicates will short-circuit. */
8148 ++processing_template_decl;
8149 full_args = tsubst_template_args (tmpl_args, args,
8150 complain, in_decl);
8151 --processing_template_decl;
8152 if (full_args == error_mark_node)
8153 return error_mark_node;
8155 /* tsubst_template_args doesn't copy the vector if
8156 nothing changed. But, *something* should have
8157 changed. */
8158 gcc_assert (full_args != tmpl_args);
8160 spec = retrieve_specialization (t, full_args,
8161 /*class_specializations_p=*/true);
8162 if (spec != NULL_TREE)
8164 r = spec;
8165 break;
8168 /* Make a new template decl. It will be similar to the
8169 original, but will record the current template arguments.
8170 We also create a new function declaration, which is just
8171 like the old one, but points to this new template, rather
8172 than the old one. */
8173 r = copy_decl (t);
8174 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8175 TREE_CHAIN (r) = NULL_TREE;
8177 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8179 if (TREE_CODE (decl) == TYPE_DECL)
8181 tree new_type;
8182 ++processing_template_decl;
8183 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8184 --processing_template_decl;
8185 if (new_type == error_mark_node)
8186 return error_mark_node;
8188 TREE_TYPE (r) = new_type;
8189 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8190 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8191 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8192 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8194 else
8196 tree new_decl;
8197 ++processing_template_decl;
8198 new_decl = tsubst (decl, args, complain, in_decl);
8199 --processing_template_decl;
8200 if (new_decl == error_mark_node)
8201 return error_mark_node;
8203 DECL_TEMPLATE_RESULT (r) = new_decl;
8204 DECL_TI_TEMPLATE (new_decl) = r;
8205 TREE_TYPE (r) = TREE_TYPE (new_decl);
8206 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8207 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8210 SET_DECL_IMPLICIT_INSTANTIATION (r);
8211 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8212 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8214 /* The template parameters for this new template are all the
8215 template parameters for the old template, except the
8216 outermost level of parameters. */
8217 DECL_TEMPLATE_PARMS (r)
8218 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8219 complain);
8221 if (PRIMARY_TEMPLATE_P (t))
8222 DECL_PRIMARY_TEMPLATE (r) = r;
8224 if (TREE_CODE (decl) != TYPE_DECL)
8225 /* Record this non-type partial instantiation. */
8226 register_specialization (r, t,
8227 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8228 false);
8230 break;
8232 case FUNCTION_DECL:
8234 tree ctx;
8235 tree argvec = NULL_TREE;
8236 tree *friends;
8237 tree gen_tmpl;
8238 tree type;
8239 int member;
8240 int args_depth;
8241 int parms_depth;
8243 /* Nobody should be tsubst'ing into non-template functions. */
8244 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8246 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8248 tree spec;
8249 bool dependent_p;
8251 /* If T is not dependent, just return it. We have to
8252 increment PROCESSING_TEMPLATE_DECL because
8253 value_dependent_expression_p assumes that nothing is
8254 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8255 ++processing_template_decl;
8256 dependent_p = value_dependent_expression_p (t);
8257 --processing_template_decl;
8258 if (!dependent_p)
8259 return t;
8261 /* Calculate the most general template of which R is a
8262 specialization, and the complete set of arguments used to
8263 specialize R. */
8264 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8265 argvec = tsubst_template_args (DECL_TI_ARGS
8266 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8267 args, complain, in_decl);
8269 /* Check to see if we already have this specialization. */
8270 spec = retrieve_specialization (gen_tmpl, argvec,
8271 /*class_specializations_p=*/false);
8273 if (spec)
8275 r = spec;
8276 break;
8279 /* We can see more levels of arguments than parameters if
8280 there was a specialization of a member template, like
8281 this:
8283 template <class T> struct S { template <class U> void f(); }
8284 template <> template <class U> void S<int>::f(U);
8286 Here, we'll be substituting into the specialization,
8287 because that's where we can find the code we actually
8288 want to generate, but we'll have enough arguments for
8289 the most general template.
8291 We also deal with the peculiar case:
8293 template <class T> struct S {
8294 template <class U> friend void f();
8296 template <class U> void f() {}
8297 template S<int>;
8298 template void f<double>();
8300 Here, the ARGS for the instantiation of will be {int,
8301 double}. But, we only need as many ARGS as there are
8302 levels of template parameters in CODE_PATTERN. We are
8303 careful not to get fooled into reducing the ARGS in
8304 situations like:
8306 template <class T> struct S { template <class U> void f(U); }
8307 template <class T> template <> void S<T>::f(int) {}
8309 which we can spot because the pattern will be a
8310 specialization in this case. */
8311 args_depth = TMPL_ARGS_DEPTH (args);
8312 parms_depth =
8313 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8314 if (args_depth > parms_depth
8315 && !DECL_TEMPLATE_SPECIALIZATION (t))
8316 args = get_innermost_template_args (args, parms_depth);
8318 else
8320 /* This special case arises when we have something like this:
8322 template <class T> struct S {
8323 friend void f<int>(int, double);
8326 Here, the DECL_TI_TEMPLATE for the friend declaration
8327 will be an IDENTIFIER_NODE. We are being called from
8328 tsubst_friend_function, and we want only to create a
8329 new decl (R) with appropriate types so that we can call
8330 determine_specialization. */
8331 gen_tmpl = NULL_TREE;
8334 if (DECL_CLASS_SCOPE_P (t))
8336 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8337 member = 2;
8338 else
8339 member = 1;
8340 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8341 complain, t, /*entering_scope=*/1);
8343 else
8345 member = 0;
8346 ctx = DECL_CONTEXT (t);
8348 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8349 if (type == error_mark_node)
8350 return error_mark_node;
8352 /* We do NOT check for matching decls pushed separately at this
8353 point, as they may not represent instantiations of this
8354 template, and in any case are considered separate under the
8355 discrete model. */
8356 r = copy_decl (t);
8357 DECL_USE_TEMPLATE (r) = 0;
8358 TREE_TYPE (r) = type;
8359 /* Clear out the mangled name and RTL for the instantiation. */
8360 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8361 SET_DECL_RTL (r, NULL_RTX);
8362 /* Leave DECL_INITIAL set on deleted instantiations. */
8363 if (!DECL_DELETED_FN (r))
8364 DECL_INITIAL (r) = NULL_TREE;
8365 DECL_CONTEXT (r) = ctx;
8367 if (member && DECL_CONV_FN_P (r))
8368 /* Type-conversion operator. Reconstruct the name, in
8369 case it's the name of one of the template's parameters. */
8370 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8372 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8373 complain, t);
8374 DECL_RESULT (r) = NULL_TREE;
8376 TREE_STATIC (r) = 0;
8377 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8378 DECL_EXTERNAL (r) = 1;
8379 /* If this is an instantiation of a function with internal
8380 linkage, we already know what object file linkage will be
8381 assigned to the instantiation. */
8382 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8383 DECL_DEFER_OUTPUT (r) = 0;
8384 TREE_CHAIN (r) = NULL_TREE;
8385 DECL_PENDING_INLINE_INFO (r) = 0;
8386 DECL_PENDING_INLINE_P (r) = 0;
8387 DECL_SAVED_TREE (r) = NULL_TREE;
8388 DECL_STRUCT_FUNCTION (r) = NULL;
8389 TREE_USED (r) = 0;
8390 if (DECL_CLONED_FUNCTION (r))
8392 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
8393 args, complain, t);
8394 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
8395 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
8398 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8399 this in the special friend case mentioned above where
8400 GEN_TMPL is NULL. */
8401 if (gen_tmpl)
8403 DECL_TEMPLATE_INFO (r)
8404 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8405 SET_DECL_IMPLICIT_INSTANTIATION (r);
8406 register_specialization (r, gen_tmpl, argvec, false);
8408 /* We're not supposed to instantiate default arguments
8409 until they are called, for a template. But, for a
8410 declaration like:
8412 template <class T> void f ()
8413 { extern void g(int i = T()); }
8415 we should do the substitution when the template is
8416 instantiated. We handle the member function case in
8417 instantiate_class_template since the default arguments
8418 might refer to other members of the class. */
8419 if (!member
8420 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8421 && !uses_template_parms (argvec))
8422 tsubst_default_arguments (r);
8424 else
8425 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8427 /* Copy the list of befriending classes. */
8428 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8429 *friends;
8430 friends = &TREE_CHAIN (*friends))
8432 *friends = copy_node (*friends);
8433 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8434 args, complain,
8435 in_decl);
8438 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8440 maybe_retrofit_in_chrg (r);
8441 if (DECL_CONSTRUCTOR_P (r))
8442 grok_ctor_properties (ctx, r);
8443 /* If this is an instantiation of a member template, clone it.
8444 If it isn't, that'll be handled by
8445 clone_constructors_and_destructors. */
8446 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8447 clone_function_decl (r, /*update_method_vec_p=*/0);
8449 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8450 && !grok_op_properties (r, (complain & tf_error) != 0))
8451 return error_mark_node;
8453 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8454 SET_DECL_FRIEND_CONTEXT (r,
8455 tsubst (DECL_FRIEND_CONTEXT (t),
8456 args, complain, in_decl));
8458 /* Possibly limit visibility based on template args. */
8459 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8460 if (DECL_VISIBILITY_SPECIFIED (t))
8462 DECL_VISIBILITY_SPECIFIED (r) = 0;
8463 DECL_ATTRIBUTES (r)
8464 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8466 determine_visibility (r);
8468 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8469 args, complain, in_decl);
8471 break;
8473 case PARM_DECL:
8475 tree type = NULL_TREE;
8476 int i, len = 1;
8477 tree expanded_types = NULL_TREE;
8478 tree prev_r = NULL_TREE;
8479 tree first_r = NULL_TREE;
8481 if (FUNCTION_PARAMETER_PACK_P (t))
8483 /* If there is a local specialization that isn't a
8484 parameter pack, it means that we're doing a "simple"
8485 substitution from inside tsubst_pack_expansion. Just
8486 return the local specialization (which will be a single
8487 parm). */
8488 tree spec = retrieve_local_specialization (t);
8489 if (spec
8490 && TREE_CODE (spec) == PARM_DECL
8491 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8492 return spec;
8494 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8495 the parameters in this function parameter pack. */
8496 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8497 complain, in_decl);
8498 if (TREE_CODE (expanded_types) == TREE_VEC)
8500 len = TREE_VEC_LENGTH (expanded_types);
8502 /* Zero-length parameter packs are boring. Just substitute
8503 into the chain. */
8504 if (len == 0)
8505 return tsubst (TREE_CHAIN (t), args, complain,
8506 TREE_CHAIN (t));
8508 else
8510 /* All we did was update the type. Make a note of that. */
8511 type = expanded_types;
8512 expanded_types = NULL_TREE;
8516 /* Loop through all of the parameter's we'll build. When T is
8517 a function parameter pack, LEN is the number of expanded
8518 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8519 r = NULL_TREE;
8520 for (i = 0; i < len; ++i)
8522 prev_r = r;
8523 r = copy_node (t);
8524 if (DECL_TEMPLATE_PARM_P (t))
8525 SET_DECL_TEMPLATE_PARM_P (r);
8527 if (expanded_types)
8528 /* We're on the Ith parameter of the function parameter
8529 pack. */
8531 /* Get the Ith type. */
8532 type = TREE_VEC_ELT (expanded_types, i);
8534 if (DECL_NAME (r))
8535 /* Rename the parameter to include the index. */
8536 DECL_NAME (r) =
8537 make_ith_pack_parameter_name (DECL_NAME (r), i);
8539 else if (!type)
8540 /* We're dealing with a normal parameter. */
8541 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8543 type = type_decays_to (type);
8544 TREE_TYPE (r) = type;
8545 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8547 if (DECL_INITIAL (r))
8549 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8550 DECL_INITIAL (r) = TREE_TYPE (r);
8551 else
8552 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8553 complain, in_decl);
8556 DECL_CONTEXT (r) = NULL_TREE;
8558 if (!DECL_TEMPLATE_PARM_P (r))
8559 DECL_ARG_TYPE (r) = type_passed_as (type);
8561 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8562 args, complain, in_decl);
8564 /* Keep track of the first new parameter we
8565 generate. That's what will be returned to the
8566 caller. */
8567 if (!first_r)
8568 first_r = r;
8570 /* Build a proper chain of parameters when substituting
8571 into a function parameter pack. */
8572 if (prev_r)
8573 TREE_CHAIN (prev_r) = r;
8576 if (TREE_CHAIN (t))
8577 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8578 complain, TREE_CHAIN (t));
8580 /* FIRST_R contains the start of the chain we've built. */
8581 r = first_r;
8583 break;
8585 case FIELD_DECL:
8587 tree type;
8589 r = copy_decl (t);
8590 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8591 if (type == error_mark_node)
8592 return error_mark_node;
8593 TREE_TYPE (r) = type;
8594 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8596 /* DECL_INITIAL gives the number of bits in a bit-field. */
8597 DECL_INITIAL (r)
8598 = tsubst_expr (DECL_INITIAL (t), args,
8599 complain, in_decl,
8600 /*integral_constant_expression_p=*/true);
8601 /* We don't have to set DECL_CONTEXT here; it is set by
8602 finish_member_declaration. */
8603 TREE_CHAIN (r) = NULL_TREE;
8604 if (VOID_TYPE_P (type))
8605 error ("instantiation of %q+D as type %qT", r, type);
8607 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8608 args, complain, in_decl);
8610 break;
8612 case USING_DECL:
8613 /* We reach here only for member using decls. */
8614 if (DECL_DEPENDENT_P (t))
8616 r = do_class_using_decl
8617 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8618 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8619 if (!r)
8620 r = error_mark_node;
8621 else
8623 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8624 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8627 else
8629 r = copy_node (t);
8630 TREE_CHAIN (r) = NULL_TREE;
8632 break;
8634 case TYPE_DECL:
8635 case VAR_DECL:
8637 tree argvec = NULL_TREE;
8638 tree gen_tmpl = NULL_TREE;
8639 tree spec;
8640 tree tmpl = NULL_TREE;
8641 tree ctx;
8642 tree type = NULL_TREE;
8643 bool local_p;
8645 if (TREE_CODE (t) == TYPE_DECL
8646 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8648 /* If this is the canonical decl, we don't have to
8649 mess with instantiations, and often we can't (for
8650 typename, template type parms and such). Note that
8651 TYPE_NAME is not correct for the above test if
8652 we've copied the type for a typedef. */
8653 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8654 if (type == error_mark_node)
8655 return error_mark_node;
8656 r = TYPE_NAME (type);
8657 break;
8660 /* Check to see if we already have the specialization we
8661 need. */
8662 spec = NULL_TREE;
8663 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8665 /* T is a static data member or namespace-scope entity.
8666 We have to substitute into namespace-scope variables
8667 (even though such entities are never templates) because
8668 of cases like:
8670 template <class T> void f() { extern T t; }
8672 where the entity referenced is not known until
8673 instantiation time. */
8674 local_p = false;
8675 ctx = DECL_CONTEXT (t);
8676 if (DECL_CLASS_SCOPE_P (t))
8678 ctx = tsubst_aggr_type (ctx, args,
8679 complain,
8680 in_decl, /*entering_scope=*/1);
8681 /* If CTX is unchanged, then T is in fact the
8682 specialization we want. That situation occurs when
8683 referencing a static data member within in its own
8684 class. We can use pointer equality, rather than
8685 same_type_p, because DECL_CONTEXT is always
8686 canonical. */
8687 if (ctx == DECL_CONTEXT (t))
8688 spec = t;
8691 if (!spec)
8693 tmpl = DECL_TI_TEMPLATE (t);
8694 gen_tmpl = most_general_template (tmpl);
8695 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8696 spec = (retrieve_specialization
8697 (gen_tmpl, argvec,
8698 /*class_specializations_p=*/false));
8701 else
8703 /* A local variable. */
8704 local_p = true;
8705 /* Subsequent calls to pushdecl will fill this in. */
8706 ctx = NULL_TREE;
8707 spec = retrieve_local_specialization (t);
8709 /* If we already have the specialization we need, there is
8710 nothing more to do. */
8711 if (spec)
8713 r = spec;
8714 break;
8717 /* Create a new node for the specialization we need. */
8718 r = copy_decl (t);
8719 if (type == NULL_TREE)
8720 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8721 if (TREE_CODE (r) == VAR_DECL)
8723 /* Even if the original location is out of scope, the
8724 newly substituted one is not. */
8725 DECL_DEAD_FOR_LOCAL (r) = 0;
8726 DECL_INITIALIZED_P (r) = 0;
8727 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8728 if (type == error_mark_node)
8729 return error_mark_node;
8730 if (TREE_CODE (type) == FUNCTION_TYPE)
8732 /* It may seem that this case cannot occur, since:
8734 typedef void f();
8735 void g() { f x; }
8737 declares a function, not a variable. However:
8739 typedef void f();
8740 template <typename T> void g() { T t; }
8741 template void g<f>();
8743 is an attempt to declare a variable with function
8744 type. */
8745 error ("variable %qD has function type",
8746 /* R is not yet sufficiently initialized, so we
8747 just use its name. */
8748 DECL_NAME (r));
8749 return error_mark_node;
8751 type = complete_type (type);
8752 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8753 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8754 type = check_var_type (DECL_NAME (r), type);
8756 if (DECL_HAS_VALUE_EXPR_P (t))
8758 tree ve = DECL_VALUE_EXPR (t);
8759 ve = tsubst_expr (ve, args, complain, in_decl,
8760 /*constant_expression_p=*/false);
8761 SET_DECL_VALUE_EXPR (r, ve);
8764 else if (DECL_SELF_REFERENCE_P (t))
8765 SET_DECL_SELF_REFERENCE_P (r);
8766 TREE_TYPE (r) = type;
8767 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8768 DECL_CONTEXT (r) = ctx;
8769 /* Clear out the mangled name and RTL for the instantiation. */
8770 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8771 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8772 SET_DECL_RTL (r, NULL_RTX);
8773 /* The initializer must not be expanded until it is required;
8774 see [temp.inst]. */
8775 DECL_INITIAL (r) = NULL_TREE;
8776 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8777 SET_DECL_RTL (r, NULL_RTX);
8778 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8779 if (TREE_CODE (r) == VAR_DECL)
8781 /* Possibly limit visibility based on template args. */
8782 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8783 if (DECL_VISIBILITY_SPECIFIED (t))
8785 DECL_VISIBILITY_SPECIFIED (r) = 0;
8786 DECL_ATTRIBUTES (r)
8787 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8789 determine_visibility (r);
8791 /* Preserve a typedef that names a type. */
8792 else if (TREE_CODE (r) == TYPE_DECL
8793 && DECL_ORIGINAL_TYPE (t)
8794 && type != error_mark_node)
8796 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
8797 args, complain, in_decl);
8798 TREE_TYPE (r) = type = build_variant_type_copy (type);
8799 TYPE_NAME (type) = r;
8802 if (!local_p)
8804 /* A static data member declaration is always marked
8805 external when it is declared in-class, even if an
8806 initializer is present. We mimic the non-template
8807 processing here. */
8808 DECL_EXTERNAL (r) = 1;
8810 register_specialization (r, gen_tmpl, argvec, false);
8811 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8812 SET_DECL_IMPLICIT_INSTANTIATION (r);
8814 else
8815 register_local_specialization (r, t);
8817 TREE_CHAIN (r) = NULL_TREE;
8819 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
8820 (int) ATTR_FLAG_TYPE_IN_PLACE,
8821 args, complain, in_decl);
8822 layout_decl (r, 0);
8824 break;
8826 default:
8827 gcc_unreachable ();
8830 /* Restore the file and line information. */
8831 input_location = saved_loc;
8833 return r;
8836 /* Substitute into the ARG_TYPES of a function type. */
8838 static tree
8839 tsubst_arg_types (tree arg_types,
8840 tree args,
8841 tsubst_flags_t complain,
8842 tree in_decl)
8844 tree remaining_arg_types;
8845 tree type = NULL_TREE;
8846 int i = 1;
8847 tree expanded_args = NULL_TREE;
8848 tree default_arg;
8850 if (!arg_types || arg_types == void_list_node)
8851 return arg_types;
8853 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8854 args, complain, in_decl);
8855 if (remaining_arg_types == error_mark_node)
8856 return error_mark_node;
8858 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8860 /* For a pack expansion, perform substitution on the
8861 entire expression. Later on, we'll handle the arguments
8862 one-by-one. */
8863 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8864 args, complain, in_decl);
8866 if (TREE_CODE (expanded_args) == TREE_VEC)
8867 /* So that we'll spin through the parameters, one by one. */
8868 i = TREE_VEC_LENGTH (expanded_args);
8869 else
8871 /* We only partially substituted into the parameter
8872 pack. Our type is TYPE_PACK_EXPANSION. */
8873 type = expanded_args;
8874 expanded_args = NULL_TREE;
8878 while (i > 0) {
8879 --i;
8881 if (expanded_args)
8882 type = TREE_VEC_ELT (expanded_args, i);
8883 else if (!type)
8884 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8886 if (type == error_mark_node)
8887 return error_mark_node;
8888 if (VOID_TYPE_P (type))
8890 if (complain & tf_error)
8892 error ("invalid parameter type %qT", type);
8893 if (in_decl)
8894 error ("in declaration %q+D", in_decl);
8896 return error_mark_node;
8899 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8900 top-level qualifiers as required. */
8901 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8903 /* We do not substitute into default arguments here. The standard
8904 mandates that they be instantiated only when needed, which is
8905 done in build_over_call. */
8906 default_arg = TREE_PURPOSE (arg_types);
8908 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8910 /* We've instantiated a template before its default arguments
8911 have been parsed. This can happen for a nested template
8912 class, and is not an error unless we require the default
8913 argument in a call of this function. */
8914 remaining_arg_types =
8915 tree_cons (default_arg, type, remaining_arg_types);
8916 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8917 remaining_arg_types);
8919 else
8920 remaining_arg_types =
8921 hash_tree_cons (default_arg, type, remaining_arg_types);
8924 return remaining_arg_types;
8927 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8928 *not* handle the exception-specification for FNTYPE, because the
8929 initial substitution of explicitly provided template parameters
8930 during argument deduction forbids substitution into the
8931 exception-specification:
8933 [temp.deduct]
8935 All references in the function type of the function template to the
8936 corresponding template parameters are replaced by the specified tem-
8937 plate argument values. If a substitution in a template parameter or
8938 in the function type of the function template results in an invalid
8939 type, type deduction fails. [Note: The equivalent substitution in
8940 exception specifications is done only when the function is instanti-
8941 ated, at which point a program is ill-formed if the substitution
8942 results in an invalid type.] */
8944 static tree
8945 tsubst_function_type (tree t,
8946 tree args,
8947 tsubst_flags_t complain,
8948 tree in_decl)
8950 tree return_type;
8951 tree arg_types;
8952 tree fntype;
8954 /* The TYPE_CONTEXT is not used for function/method types. */
8955 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8957 /* Substitute the return type. */
8958 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8959 if (return_type == error_mark_node)
8960 return error_mark_node;
8961 /* The standard does not presently indicate that creation of a
8962 function type with an invalid return type is a deduction failure.
8963 However, that is clearly analogous to creating an array of "void"
8964 or a reference to a reference. This is core issue #486. */
8965 if (TREE_CODE (return_type) == ARRAY_TYPE
8966 || TREE_CODE (return_type) == FUNCTION_TYPE)
8968 if (complain & tf_error)
8970 if (TREE_CODE (return_type) == ARRAY_TYPE)
8971 error ("function returning an array");
8972 else
8973 error ("function returning a function");
8975 return error_mark_node;
8978 /* Substitute the argument types. */
8979 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8980 complain, in_decl);
8981 if (arg_types == error_mark_node)
8982 return error_mark_node;
8984 /* Construct a new type node and return it. */
8985 if (TREE_CODE (t) == FUNCTION_TYPE)
8986 fntype = build_function_type (return_type, arg_types);
8987 else
8989 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8990 if (! MAYBE_CLASS_TYPE_P (r))
8992 /* [temp.deduct]
8994 Type deduction may fail for any of the following
8995 reasons:
8997 -- Attempting to create "pointer to member of T" when T
8998 is not a class type. */
8999 if (complain & tf_error)
9000 error ("creating pointer to member function of non-class type %qT",
9002 return error_mark_node;
9005 fntype = build_method_type_directly (r, return_type,
9006 TREE_CHAIN (arg_types));
9008 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9009 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9011 return fntype;
9014 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9015 ARGS into that specification, and return the substituted
9016 specification. If there is no specification, return NULL_TREE. */
9018 static tree
9019 tsubst_exception_specification (tree fntype,
9020 tree args,
9021 tsubst_flags_t complain,
9022 tree in_decl)
9024 tree specs;
9025 tree new_specs;
9027 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9028 new_specs = NULL_TREE;
9029 if (specs)
9031 if (! TREE_VALUE (specs))
9032 new_specs = specs;
9033 else
9034 while (specs)
9036 tree spec;
9037 int i, len = 1;
9038 tree expanded_specs = NULL_TREE;
9040 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9042 /* Expand the pack expansion type. */
9043 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9044 args, complain,
9045 in_decl);
9047 if (expanded_specs == error_mark_node)
9048 return error_mark_node;
9049 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9050 len = TREE_VEC_LENGTH (expanded_specs);
9051 else
9053 /* We're substituting into a member template, so
9054 we got a TYPE_PACK_EXPANSION back. Add that
9055 expansion and move on. */
9056 gcc_assert (TREE_CODE (expanded_specs)
9057 == TYPE_PACK_EXPANSION);
9058 new_specs = add_exception_specifier (new_specs,
9059 expanded_specs,
9060 complain);
9061 specs = TREE_CHAIN (specs);
9062 continue;
9066 for (i = 0; i < len; ++i)
9068 if (expanded_specs)
9069 spec = TREE_VEC_ELT (expanded_specs, i);
9070 else
9071 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9072 if (spec == error_mark_node)
9073 return spec;
9074 new_specs = add_exception_specifier (new_specs, spec,
9075 complain);
9078 specs = TREE_CHAIN (specs);
9081 return new_specs;
9084 /* Take the tree structure T and replace template parameters used
9085 therein with the argument vector ARGS. IN_DECL is an associated
9086 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9087 Issue error and warning messages under control of COMPLAIN. Note
9088 that we must be relatively non-tolerant of extensions here, in
9089 order to preserve conformance; if we allow substitutions that
9090 should not be allowed, we may allow argument deductions that should
9091 not succeed, and therefore report ambiguous overload situations
9092 where there are none. In theory, we could allow the substitution,
9093 but indicate that it should have failed, and allow our caller to
9094 make sure that the right thing happens, but we don't try to do this
9095 yet.
9097 This function is used for dealing with types, decls and the like;
9098 for expressions, use tsubst_expr or tsubst_copy. */
9100 tree
9101 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9103 tree type, r;
9105 if (t == NULL_TREE || t == error_mark_node
9106 || t == integer_type_node
9107 || t == void_type_node
9108 || t == char_type_node
9109 || t == unknown_type_node
9110 || TREE_CODE (t) == NAMESPACE_DECL)
9111 return t;
9113 if (DECL_P (t))
9114 return tsubst_decl (t, args, complain);
9116 if (args == NULL_TREE)
9117 return t;
9119 if (TREE_CODE (t) == IDENTIFIER_NODE)
9120 type = IDENTIFIER_TYPE_VALUE (t);
9121 else
9122 type = TREE_TYPE (t);
9124 gcc_assert (type != unknown_type_node);
9126 /* Reuse typedefs. We need to do this to handle dependent attributes,
9127 such as attribute aligned. */
9128 if (TYPE_P (t)
9129 && TYPE_NAME (t)
9130 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9132 tree decl = TYPE_NAME (t);
9134 if (DECL_CLASS_SCOPE_P (decl)
9135 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9136 && uses_template_parms (DECL_CONTEXT (decl)))
9138 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9139 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9140 r = retrieve_specialization (tmpl, gen_args, false);
9142 else if (DECL_FUNCTION_SCOPE_P (decl)
9143 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9144 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9145 r = retrieve_local_specialization (decl);
9146 else
9147 /* The typedef is from a non-template context. */
9148 return t;
9150 if (r)
9152 r = TREE_TYPE (r);
9153 r = cp_build_qualified_type_real
9154 (r, cp_type_quals (t) | cp_type_quals (r),
9155 complain | tf_ignore_bad_quals);
9156 return r;
9158 /* Else we must be instantiating the typedef, so fall through. */
9161 if (type
9162 && TREE_CODE (t) != TYPENAME_TYPE
9163 && TREE_CODE (t) != IDENTIFIER_NODE
9164 && TREE_CODE (t) != FUNCTION_TYPE
9165 && TREE_CODE (t) != METHOD_TYPE)
9166 type = tsubst (type, args, complain, in_decl);
9167 if (type == error_mark_node)
9168 return error_mark_node;
9170 switch (TREE_CODE (t))
9172 case RECORD_TYPE:
9173 case UNION_TYPE:
9174 case ENUMERAL_TYPE:
9175 return tsubst_aggr_type (t, args, complain, in_decl,
9176 /*entering_scope=*/0);
9178 case ERROR_MARK:
9179 case IDENTIFIER_NODE:
9180 case VOID_TYPE:
9181 case REAL_TYPE:
9182 case COMPLEX_TYPE:
9183 case VECTOR_TYPE:
9184 case BOOLEAN_TYPE:
9185 case INTEGER_CST:
9186 case REAL_CST:
9187 case STRING_CST:
9188 return t;
9190 case INTEGER_TYPE:
9191 if (t == integer_type_node)
9192 return t;
9194 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9195 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9196 return t;
9199 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9201 max = tsubst_expr (omax, args, complain, in_decl,
9202 /*integral_constant_expression_p=*/false);
9204 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9205 needed. */
9206 if (TREE_CODE (max) == NOP_EXPR
9207 && TREE_SIDE_EFFECTS (omax)
9208 && !TREE_TYPE (max))
9209 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9211 max = fold_decl_constant_value (max);
9213 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9214 with TREE_SIDE_EFFECTS that indicates this is not an integral
9215 constant expression. */
9216 if (processing_template_decl
9217 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9219 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9220 TREE_SIDE_EFFECTS (max) = 1;
9223 if (TREE_CODE (max) != INTEGER_CST
9224 && !at_function_scope_p ()
9225 && !TREE_SIDE_EFFECTS (max)
9226 && !value_dependent_expression_p (max))
9228 if (complain & tf_error)
9229 error ("array bound is not an integer constant");
9230 return error_mark_node;
9233 /* [temp.deduct]
9235 Type deduction may fail for any of the following
9236 reasons:
9238 Attempting to create an array with a size that is
9239 zero or negative. */
9240 if (integer_zerop (max) && !(complain & tf_error))
9241 /* We must fail if performing argument deduction (as
9242 indicated by the state of complain), so that
9243 another substitution can be found. */
9244 return error_mark_node;
9245 else if (TREE_CODE (max) == INTEGER_CST
9246 && INT_CST_LT (max, integer_zero_node))
9248 if (complain & tf_error)
9249 error ("creating array with negative size (%qE)", max);
9251 return error_mark_node;
9254 return compute_array_index_type (NULL_TREE, max);
9257 case TEMPLATE_TYPE_PARM:
9258 case TEMPLATE_TEMPLATE_PARM:
9259 case BOUND_TEMPLATE_TEMPLATE_PARM:
9260 case TEMPLATE_PARM_INDEX:
9262 int idx;
9263 int level;
9264 int levels;
9265 tree arg = NULL_TREE;
9267 r = NULL_TREE;
9269 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9270 template_parm_level_and_index (t, &level, &idx);
9272 levels = TMPL_ARGS_DEPTH (args);
9273 if (level <= levels)
9275 arg = TMPL_ARG (args, level, idx);
9277 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9278 /* See through ARGUMENT_PACK_SELECT arguments. */
9279 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9282 if (arg == error_mark_node)
9283 return error_mark_node;
9284 else if (arg != NULL_TREE)
9286 if (ARGUMENT_PACK_P (arg))
9287 /* If ARG is an argument pack, we don't actually want to
9288 perform a substitution here, because substitutions
9289 for argument packs are only done
9290 element-by-element. We can get to this point when
9291 substituting the type of a non-type template
9292 parameter pack, when that type actually contains
9293 template parameter packs from an outer template, e.g.,
9295 template<typename... Types> struct A {
9296 template<Types... Values> struct B { };
9297 }; */
9298 return t;
9300 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9302 int quals;
9303 gcc_assert (TYPE_P (arg));
9305 /* cv-quals from the template are discarded when
9306 substituting in a function or reference type. */
9307 if (TREE_CODE (arg) == FUNCTION_TYPE
9308 || TREE_CODE (arg) == METHOD_TYPE
9309 || TREE_CODE (arg) == REFERENCE_TYPE)
9310 quals = cp_type_quals (arg);
9311 else
9312 quals = cp_type_quals (arg) | cp_type_quals (t);
9314 return cp_build_qualified_type_real
9315 (arg, quals, complain | tf_ignore_bad_quals);
9317 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9319 /* We are processing a type constructed from a
9320 template template parameter. */
9321 tree argvec = tsubst (TYPE_TI_ARGS (t),
9322 args, complain, in_decl);
9323 if (argvec == error_mark_node)
9324 return error_mark_node;
9326 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9327 are resolving nested-types in the signature of a
9328 member function templates. Otherwise ARG is a
9329 TEMPLATE_DECL and is the real template to be
9330 instantiated. */
9331 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9332 arg = TYPE_NAME (arg);
9334 r = lookup_template_class (arg,
9335 argvec, in_decl,
9336 DECL_CONTEXT (arg),
9337 /*entering_scope=*/0,
9338 complain);
9339 return cp_build_qualified_type_real
9340 (r, TYPE_QUALS (t), complain);
9342 else
9343 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9344 return arg;
9347 if (level == 1)
9348 /* This can happen during the attempted tsubst'ing in
9349 unify. This means that we don't yet have any information
9350 about the template parameter in question. */
9351 return t;
9353 /* If we get here, we must have been looking at a parm for a
9354 more deeply nested template. Make a new version of this
9355 template parameter, but with a lower level. */
9356 switch (TREE_CODE (t))
9358 case TEMPLATE_TYPE_PARM:
9359 case TEMPLATE_TEMPLATE_PARM:
9360 case BOUND_TEMPLATE_TEMPLATE_PARM:
9361 if (cp_type_quals (t))
9363 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9364 r = cp_build_qualified_type_real
9365 (r, cp_type_quals (t),
9366 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9367 ? tf_ignore_bad_quals : 0));
9369 else
9371 r = copy_type (t);
9372 TEMPLATE_TYPE_PARM_INDEX (r)
9373 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9374 r, levels, args, complain);
9375 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9376 TYPE_MAIN_VARIANT (r) = r;
9377 TYPE_POINTER_TO (r) = NULL_TREE;
9378 TYPE_REFERENCE_TO (r) = NULL_TREE;
9380 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9381 /* We have reduced the level of the template
9382 template parameter, but not the levels of its
9383 template parameters, so canonical_type_parameter
9384 will not be able to find the canonical template
9385 template parameter for this level. Thus, we
9386 require structural equality checking to compare
9387 TEMPLATE_TEMPLATE_PARMs. */
9388 SET_TYPE_STRUCTURAL_EQUALITY (r);
9389 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9390 SET_TYPE_STRUCTURAL_EQUALITY (r);
9391 else
9392 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9394 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9396 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9397 complain, in_decl);
9398 if (argvec == error_mark_node)
9399 return error_mark_node;
9401 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9402 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9405 break;
9407 case TEMPLATE_PARM_INDEX:
9408 r = reduce_template_parm_level (t, type, levels, args, complain);
9409 break;
9411 default:
9412 gcc_unreachable ();
9415 return r;
9418 case TREE_LIST:
9420 tree purpose, value, chain;
9422 if (t == void_list_node)
9423 return t;
9425 purpose = TREE_PURPOSE (t);
9426 if (purpose)
9428 purpose = tsubst (purpose, args, complain, in_decl);
9429 if (purpose == error_mark_node)
9430 return error_mark_node;
9432 value = TREE_VALUE (t);
9433 if (value)
9435 value = tsubst (value, args, complain, in_decl);
9436 if (value == error_mark_node)
9437 return error_mark_node;
9439 chain = TREE_CHAIN (t);
9440 if (chain && chain != void_type_node)
9442 chain = tsubst (chain, args, complain, in_decl);
9443 if (chain == error_mark_node)
9444 return error_mark_node;
9446 if (purpose == TREE_PURPOSE (t)
9447 && value == TREE_VALUE (t)
9448 && chain == TREE_CHAIN (t))
9449 return t;
9450 return hash_tree_cons (purpose, value, chain);
9453 case TREE_BINFO:
9454 /* We should never be tsubsting a binfo. */
9455 gcc_unreachable ();
9457 case TREE_VEC:
9458 /* A vector of template arguments. */
9459 gcc_assert (!type);
9460 return tsubst_template_args (t, args, complain, in_decl);
9462 case POINTER_TYPE:
9463 case REFERENCE_TYPE:
9465 enum tree_code code;
9467 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9468 return t;
9470 code = TREE_CODE (t);
9473 /* [temp.deduct]
9475 Type deduction may fail for any of the following
9476 reasons:
9478 -- Attempting to create a pointer to reference type.
9479 -- Attempting to create a reference to a reference type or
9480 a reference to void.
9482 Core issue 106 says that creating a reference to a reference
9483 during instantiation is no longer a cause for failure. We
9484 only enforce this check in strict C++98 mode. */
9485 if ((TREE_CODE (type) == REFERENCE_TYPE
9486 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9487 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9489 static location_t last_loc;
9491 /* We keep track of the last time we issued this error
9492 message to avoid spewing a ton of messages during a
9493 single bad template instantiation. */
9494 if (complain & tf_error
9495 && last_loc != input_location)
9497 if (TREE_CODE (type) == VOID_TYPE)
9498 error ("forming reference to void");
9499 else
9500 error ("forming %s to reference type %qT",
9501 (code == POINTER_TYPE) ? "pointer" : "reference",
9502 type);
9503 last_loc = input_location;
9506 return error_mark_node;
9508 else if (code == POINTER_TYPE)
9510 r = build_pointer_type (type);
9511 if (TREE_CODE (type) == METHOD_TYPE)
9512 r = build_ptrmemfunc_type (r);
9514 else if (TREE_CODE (type) == REFERENCE_TYPE)
9515 /* In C++0x, during template argument substitution, when there is an
9516 attempt to create a reference to a reference type, reference
9517 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9519 "If a template-argument for a template-parameter T names a type
9520 that is a reference to a type A, an attempt to create the type
9521 'lvalue reference to cv T' creates the type 'lvalue reference to
9522 A,' while an attempt to create the type type rvalue reference to
9523 cv T' creates the type T"
9525 r = cp_build_reference_type
9526 (TREE_TYPE (type),
9527 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9528 else
9529 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9530 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
9532 if (r != error_mark_node)
9533 /* Will this ever be needed for TYPE_..._TO values? */
9534 layout_type (r);
9536 return r;
9538 case OFFSET_TYPE:
9540 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9541 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
9543 /* [temp.deduct]
9545 Type deduction may fail for any of the following
9546 reasons:
9548 -- Attempting to create "pointer to member of T" when T
9549 is not a class type. */
9550 if (complain & tf_error)
9551 error ("creating pointer to member of non-class type %qT", r);
9552 return error_mark_node;
9554 if (TREE_CODE (type) == REFERENCE_TYPE)
9556 if (complain & tf_error)
9557 error ("creating pointer to member reference type %qT", type);
9558 return error_mark_node;
9560 if (TREE_CODE (type) == VOID_TYPE)
9562 if (complain & tf_error)
9563 error ("creating pointer to member of type void");
9564 return error_mark_node;
9566 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9567 if (TREE_CODE (type) == FUNCTION_TYPE)
9569 /* The type of the implicit object parameter gets its
9570 cv-qualifiers from the FUNCTION_TYPE. */
9571 tree method_type;
9572 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9573 cp_type_quals (type));
9574 tree memptr;
9575 method_type = build_method_type_directly (this_type,
9576 TREE_TYPE (type),
9577 TYPE_ARG_TYPES (type));
9578 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9579 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9580 complain);
9582 else
9583 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9584 TYPE_QUALS (t),
9585 complain);
9587 case FUNCTION_TYPE:
9588 case METHOD_TYPE:
9590 tree fntype;
9591 tree specs;
9592 fntype = tsubst_function_type (t, args, complain, in_decl);
9593 if (fntype == error_mark_node)
9594 return error_mark_node;
9596 /* Substitute the exception specification. */
9597 specs = tsubst_exception_specification (t, args, complain,
9598 in_decl);
9599 if (specs == error_mark_node)
9600 return error_mark_node;
9601 if (specs)
9602 fntype = build_exception_variant (fntype, specs);
9603 return fntype;
9605 case ARRAY_TYPE:
9607 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9608 if (domain == error_mark_node)
9609 return error_mark_node;
9611 /* As an optimization, we avoid regenerating the array type if
9612 it will obviously be the same as T. */
9613 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9614 return t;
9616 /* These checks should match the ones in grokdeclarator.
9618 [temp.deduct]
9620 The deduction may fail for any of the following reasons:
9622 -- Attempting to create an array with an element type that
9623 is void, a function type, or a reference type, or [DR337]
9624 an abstract class type. */
9625 if (TREE_CODE (type) == VOID_TYPE
9626 || TREE_CODE (type) == FUNCTION_TYPE
9627 || TREE_CODE (type) == REFERENCE_TYPE)
9629 if (complain & tf_error)
9630 error ("creating array of %qT", type);
9631 return error_mark_node;
9633 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
9635 if (complain & tf_error)
9636 error ("creating array of %qT, which is an abstract class type",
9637 type);
9638 return error_mark_node;
9641 r = build_cplus_array_type (type, domain);
9643 if (TYPE_USER_ALIGN (t))
9645 TYPE_ALIGN (r) = TYPE_ALIGN (t);
9646 TYPE_USER_ALIGN (r) = 1;
9649 return r;
9652 case PLUS_EXPR:
9653 case MINUS_EXPR:
9655 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9656 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9658 if (e1 == error_mark_node || e2 == error_mark_node)
9659 return error_mark_node;
9661 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
9664 case NEGATE_EXPR:
9665 case NOP_EXPR:
9667 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9668 if (e == error_mark_node)
9669 return error_mark_node;
9671 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
9674 case TYPENAME_TYPE:
9676 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9677 in_decl, /*entering_scope=*/1);
9678 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
9679 complain, in_decl);
9681 if (ctx == error_mark_node || f == error_mark_node)
9682 return error_mark_node;
9684 if (!MAYBE_CLASS_TYPE_P (ctx))
9686 if (complain & tf_error)
9687 error ("%qT is not a class, struct, or union type", ctx);
9688 return error_mark_node;
9690 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9692 /* Normally, make_typename_type does not require that the CTX
9693 have complete type in order to allow things like:
9695 template <class T> struct S { typename S<T>::X Y; };
9697 But, such constructs have already been resolved by this
9698 point, so here CTX really should have complete type, unless
9699 it's a partial instantiation. */
9700 ctx = complete_type (ctx);
9701 if (!COMPLETE_TYPE_P (ctx))
9703 if (complain & tf_error)
9704 cxx_incomplete_type_error (NULL_TREE, ctx);
9705 return error_mark_node;
9709 f = make_typename_type (ctx, f, typename_type,
9710 (complain & tf_error) | tf_keep_type_decl);
9711 if (f == error_mark_node)
9712 return f;
9713 if (TREE_CODE (f) == TYPE_DECL)
9715 complain |= tf_ignore_bad_quals;
9716 f = TREE_TYPE (f);
9719 if (TREE_CODE (f) != TYPENAME_TYPE)
9721 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9722 error ("%qT resolves to %qT, which is not an enumeration type",
9723 t, f);
9724 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9725 error ("%qT resolves to %qT, which is is not a class type",
9726 t, f);
9729 return cp_build_qualified_type_real
9730 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9733 case UNBOUND_CLASS_TEMPLATE:
9735 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9736 in_decl, /*entering_scope=*/1);
9737 tree name = TYPE_IDENTIFIER (t);
9738 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9740 if (ctx == error_mark_node || name == error_mark_node)
9741 return error_mark_node;
9743 if (parm_list)
9744 parm_list = tsubst_template_parms (parm_list, args, complain);
9745 return make_unbound_class_template (ctx, name, parm_list, complain);
9748 case INDIRECT_REF:
9749 case ADDR_EXPR:
9750 case CALL_EXPR:
9751 gcc_unreachable ();
9753 case ARRAY_REF:
9755 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9756 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9757 /*integral_constant_expression_p=*/false);
9758 if (e1 == error_mark_node || e2 == error_mark_node)
9759 return error_mark_node;
9761 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9764 case SCOPE_REF:
9766 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9767 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9768 if (e1 == error_mark_node || e2 == error_mark_node)
9769 return error_mark_node;
9771 return build_qualified_name (/*type=*/NULL_TREE,
9772 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9775 case TYPEOF_TYPE:
9777 tree type;
9779 type = finish_typeof (tsubst_expr
9780 (TYPEOF_TYPE_EXPR (t), args,
9781 complain, in_decl,
9782 /*integral_constant_expression_p=*/false));
9783 return cp_build_qualified_type_real (type,
9784 cp_type_quals (t)
9785 | cp_type_quals (type),
9786 complain);
9789 case DECLTYPE_TYPE:
9791 tree type;
9793 ++cp_unevaluated_operand;
9794 ++c_inhibit_evaluation_warnings;
9796 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
9797 complain, in_decl,
9798 /*integral_constant_expression_p=*/false);
9800 --cp_unevaluated_operand;
9801 --c_inhibit_evaluation_warnings;
9803 type =
9804 finish_decltype_type (type,
9805 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9806 return cp_build_qualified_type_real (type,
9807 cp_type_quals (t)
9808 | cp_type_quals (type),
9809 complain);
9812 case TYPE_ARGUMENT_PACK:
9813 case NONTYPE_ARGUMENT_PACK:
9815 tree r = make_node (TREE_CODE (t));
9816 tree packed_out =
9817 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9818 args,
9819 complain,
9820 in_decl);
9821 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9823 /* For template nontype argument packs, also substitute into
9824 the type. */
9825 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9826 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9828 return r;
9830 break;
9832 default:
9833 sorry ("use of %qs in template",
9834 tree_code_name [(int) TREE_CODE (t)]);
9835 return error_mark_node;
9839 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9840 type of the expression on the left-hand side of the "." or "->"
9841 operator. */
9843 static tree
9844 tsubst_baselink (tree baselink, tree object_type,
9845 tree args, tsubst_flags_t complain, tree in_decl)
9847 tree name;
9848 tree qualifying_scope;
9849 tree fns;
9850 tree optype;
9851 tree template_args = 0;
9852 bool template_id_p = false;
9854 /* A baselink indicates a function from a base class. Both the
9855 BASELINK_ACCESS_BINFO and the base class referenced may
9856 indicate bases of the template class, rather than the
9857 instantiated class. In addition, lookups that were not
9858 ambiguous before may be ambiguous now. Therefore, we perform
9859 the lookup again. */
9860 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9861 qualifying_scope = tsubst (qualifying_scope, args,
9862 complain, in_decl);
9863 fns = BASELINK_FUNCTIONS (baselink);
9864 optype = BASELINK_OPTYPE (baselink);
9865 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9867 template_id_p = true;
9868 template_args = TREE_OPERAND (fns, 1);
9869 fns = TREE_OPERAND (fns, 0);
9870 if (template_args)
9871 template_args = tsubst_template_args (template_args, args,
9872 complain, in_decl);
9874 name = DECL_NAME (get_first_fn (fns));
9875 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9877 /* If lookup found a single function, mark it as used at this
9878 point. (If it lookup found multiple functions the one selected
9879 later by overload resolution will be marked as used at that
9880 point.) */
9881 if (BASELINK_P (baselink))
9882 fns = BASELINK_FUNCTIONS (baselink);
9883 if (!template_id_p && !really_overloaded_fn (fns))
9884 mark_used (OVL_CURRENT (fns));
9886 /* Add back the template arguments, if present. */
9887 if (BASELINK_P (baselink) && template_id_p)
9888 BASELINK_FUNCTIONS (baselink)
9889 = build_nt (TEMPLATE_ID_EXPR,
9890 BASELINK_FUNCTIONS (baselink),
9891 template_args);
9892 /* Update the conversion operator type. */
9893 BASELINK_OPTYPE (baselink)
9894 = tsubst (optype, args, complain, in_decl);
9896 if (!object_type)
9897 object_type = current_class_type;
9898 return adjust_result_of_qualified_name_lookup (baselink,
9899 qualifying_scope,
9900 object_type);
9903 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9904 true if the qualified-id will be a postfix-expression in-and-of
9905 itself; false if more of the postfix-expression follows the
9906 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9907 of "&". */
9909 static tree
9910 tsubst_qualified_id (tree qualified_id, tree args,
9911 tsubst_flags_t complain, tree in_decl,
9912 bool done, bool address_p)
9914 tree expr;
9915 tree scope;
9916 tree name;
9917 bool is_template;
9918 tree template_args;
9920 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9922 /* Figure out what name to look up. */
9923 name = TREE_OPERAND (qualified_id, 1);
9924 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9926 is_template = true;
9927 template_args = TREE_OPERAND (name, 1);
9928 if (template_args)
9929 template_args = tsubst_template_args (template_args, args,
9930 complain, in_decl);
9931 name = TREE_OPERAND (name, 0);
9933 else
9935 is_template = false;
9936 template_args = NULL_TREE;
9939 /* Substitute into the qualifying scope. When there are no ARGS, we
9940 are just trying to simplify a non-dependent expression. In that
9941 case the qualifying scope may be dependent, and, in any case,
9942 substituting will not help. */
9943 scope = TREE_OPERAND (qualified_id, 0);
9944 if (args)
9946 scope = tsubst (scope, args, complain, in_decl);
9947 expr = tsubst_copy (name, args, complain, in_decl);
9949 else
9950 expr = name;
9952 if (dependent_type_p (scope))
9954 tree type = NULL_TREE;
9955 if (DECL_P (expr) && !dependent_scope_p (scope))
9956 type = TREE_TYPE (expr);
9957 return build_qualified_name (type, scope, expr,
9958 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9961 if (!BASELINK_P (name) && !DECL_P (expr))
9963 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9965 /* A BIT_NOT_EXPR is used to represent a destructor. */
9966 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
9968 error ("qualifying type %qT does not match destructor name ~%qT",
9969 scope, TREE_OPERAND (expr, 0));
9970 expr = error_mark_node;
9972 else
9973 expr = lookup_qualified_name (scope, complete_dtor_identifier,
9974 /*is_type_p=*/0, false);
9976 else
9977 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9978 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9979 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9981 if (complain & tf_error)
9983 error ("dependent-name %qE is parsed as a non-type, but "
9984 "instantiation yields a type", qualified_id);
9985 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
9987 return error_mark_node;
9991 if (DECL_P (expr))
9993 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9994 scope);
9995 /* Remember that there was a reference to this entity. */
9996 mark_used (expr);
9999 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10001 if (complain & tf_error)
10002 qualified_name_lookup_error (scope,
10003 TREE_OPERAND (qualified_id, 1),
10004 expr, input_location);
10005 return error_mark_node;
10008 if (is_template)
10009 expr = lookup_template_function (expr, template_args);
10011 if (expr == error_mark_node && complain & tf_error)
10012 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10013 expr, input_location);
10014 else if (TYPE_P (scope))
10016 expr = (adjust_result_of_qualified_name_lookup
10017 (expr, scope, current_class_type));
10018 expr = (finish_qualified_id_expr
10019 (scope, expr, done, address_p,
10020 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10021 /*template_arg_p=*/false));
10024 /* Expressions do not generally have reference type. */
10025 if (TREE_CODE (expr) != SCOPE_REF
10026 /* However, if we're about to form a pointer-to-member, we just
10027 want the referenced member referenced. */
10028 && TREE_CODE (expr) != OFFSET_REF)
10029 expr = convert_from_reference (expr);
10031 return expr;
10034 /* Like tsubst, but deals with expressions. This function just replaces
10035 template parms; to finish processing the resultant expression, use
10036 tsubst_expr. */
10038 static tree
10039 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10041 enum tree_code code;
10042 tree r;
10044 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10045 return t;
10047 code = TREE_CODE (t);
10049 switch (code)
10051 case PARM_DECL:
10052 r = retrieve_local_specialization (t);
10054 if (r == NULL)
10056 tree c;
10057 /* This can happen for a parameter name used later in a function
10058 declaration (such as in a late-specified return type). Just
10059 make a dummy decl, since it's only used for its type. */
10060 gcc_assert (cp_unevaluated_operand != 0);
10061 /* We copy T because want to tsubst the PARM_DECL only,
10062 not the following PARM_DECLs that are chained to T. */
10063 c = copy_node (t);
10064 r = tsubst_decl (c, args, complain);
10065 /* Give it the template pattern as its context; its true context
10066 hasn't been instantiated yet and this is good enough for
10067 mangling. */
10068 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10071 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10072 r = ARGUMENT_PACK_SELECT_ARG (r);
10073 mark_used (r);
10074 return r;
10076 case CONST_DECL:
10078 tree enum_type;
10079 tree v;
10081 if (DECL_TEMPLATE_PARM_P (t))
10082 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10083 /* There is no need to substitute into namespace-scope
10084 enumerators. */
10085 if (DECL_NAMESPACE_SCOPE_P (t))
10086 return t;
10087 /* If ARGS is NULL, then T is known to be non-dependent. */
10088 if (args == NULL_TREE)
10089 return integral_constant_value (t);
10091 /* Unfortunately, we cannot just call lookup_name here.
10092 Consider:
10094 template <int I> int f() {
10095 enum E { a = I };
10096 struct S { void g() { E e = a; } };
10099 When we instantiate f<7>::S::g(), say, lookup_name is not
10100 clever enough to find f<7>::a. */
10101 enum_type
10102 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10103 /*entering_scope=*/0);
10105 for (v = TYPE_VALUES (enum_type);
10106 v != NULL_TREE;
10107 v = TREE_CHAIN (v))
10108 if (TREE_PURPOSE (v) == DECL_NAME (t))
10109 return TREE_VALUE (v);
10111 /* We didn't find the name. That should never happen; if
10112 name-lookup found it during preliminary parsing, we
10113 should find it again here during instantiation. */
10114 gcc_unreachable ();
10116 return t;
10118 case FIELD_DECL:
10119 if (DECL_CONTEXT (t))
10121 tree ctx;
10123 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10124 /*entering_scope=*/1);
10125 if (ctx != DECL_CONTEXT (t))
10127 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10128 if (!r)
10130 if (complain & tf_error)
10131 error ("using invalid field %qD", t);
10132 return error_mark_node;
10134 return r;
10138 return t;
10140 case VAR_DECL:
10141 case FUNCTION_DECL:
10142 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10143 || local_variable_p (t))
10144 t = tsubst (t, args, complain, in_decl);
10145 mark_used (t);
10146 return t;
10148 case BASELINK:
10149 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10151 case TEMPLATE_DECL:
10152 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10153 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10154 args, complain, in_decl);
10155 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10156 return tsubst (t, args, complain, in_decl);
10157 else if (DECL_CLASS_SCOPE_P (t)
10158 && uses_template_parms (DECL_CONTEXT (t)))
10160 /* Template template argument like the following example need
10161 special treatment:
10163 template <template <class> class TT> struct C {};
10164 template <class T> struct D {
10165 template <class U> struct E {};
10166 C<E> c; // #1
10168 D<int> d; // #2
10170 We are processing the template argument `E' in #1 for
10171 the template instantiation #2. Originally, `E' is a
10172 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10173 have to substitute this with one having context `D<int>'. */
10175 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10176 return lookup_field (context, DECL_NAME(t), 0, false);
10178 else
10179 /* Ordinary template template argument. */
10180 return t;
10182 case CAST_EXPR:
10183 case REINTERPRET_CAST_EXPR:
10184 case CONST_CAST_EXPR:
10185 case STATIC_CAST_EXPR:
10186 case DYNAMIC_CAST_EXPR:
10187 case NOP_EXPR:
10188 return build1
10189 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10190 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10192 case SIZEOF_EXPR:
10193 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10195 /* We only want to compute the number of arguments. */
10196 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10197 complain, in_decl);
10198 int len = 0;
10200 if (TREE_CODE (expanded) == TREE_VEC)
10201 len = TREE_VEC_LENGTH (expanded);
10203 if (expanded == error_mark_node)
10204 return error_mark_node;
10205 else if (PACK_EXPANSION_P (expanded)
10206 || (TREE_CODE (expanded) == TREE_VEC
10207 && len > 0
10208 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10210 if (TREE_CODE (expanded) == TREE_VEC)
10211 expanded = TREE_VEC_ELT (expanded, len - 1);
10213 if (TYPE_P (expanded))
10214 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10215 complain & tf_error);
10216 else
10217 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10218 complain & tf_error);
10220 else
10221 return build_int_cst (size_type_node, len);
10223 /* Fall through */
10225 case INDIRECT_REF:
10226 case NEGATE_EXPR:
10227 case TRUTH_NOT_EXPR:
10228 case BIT_NOT_EXPR:
10229 case ADDR_EXPR:
10230 case UNARY_PLUS_EXPR: /* Unary + */
10231 case ALIGNOF_EXPR:
10232 case ARROW_EXPR:
10233 case THROW_EXPR:
10234 case TYPEID_EXPR:
10235 case REALPART_EXPR:
10236 case IMAGPART_EXPR:
10237 return build1
10238 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10239 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10241 case COMPONENT_REF:
10243 tree object;
10244 tree name;
10246 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10247 name = TREE_OPERAND (t, 1);
10248 if (TREE_CODE (name) == BIT_NOT_EXPR)
10250 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10251 complain, in_decl);
10252 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10254 else if (TREE_CODE (name) == SCOPE_REF
10255 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10257 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10258 complain, in_decl);
10259 name = TREE_OPERAND (name, 1);
10260 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10261 complain, in_decl);
10262 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10263 name = build_qualified_name (/*type=*/NULL_TREE,
10264 base, name,
10265 /*template_p=*/false);
10267 else if (TREE_CODE (name) == BASELINK)
10268 name = tsubst_baselink (name,
10269 non_reference (TREE_TYPE (object)),
10270 args, complain,
10271 in_decl);
10272 else
10273 name = tsubst_copy (name, args, complain, in_decl);
10274 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10277 case PLUS_EXPR:
10278 case MINUS_EXPR:
10279 case MULT_EXPR:
10280 case TRUNC_DIV_EXPR:
10281 case CEIL_DIV_EXPR:
10282 case FLOOR_DIV_EXPR:
10283 case ROUND_DIV_EXPR:
10284 case EXACT_DIV_EXPR:
10285 case BIT_AND_EXPR:
10286 case BIT_IOR_EXPR:
10287 case BIT_XOR_EXPR:
10288 case TRUNC_MOD_EXPR:
10289 case FLOOR_MOD_EXPR:
10290 case TRUTH_ANDIF_EXPR:
10291 case TRUTH_ORIF_EXPR:
10292 case TRUTH_AND_EXPR:
10293 case TRUTH_OR_EXPR:
10294 case RSHIFT_EXPR:
10295 case LSHIFT_EXPR:
10296 case RROTATE_EXPR:
10297 case LROTATE_EXPR:
10298 case EQ_EXPR:
10299 case NE_EXPR:
10300 case MAX_EXPR:
10301 case MIN_EXPR:
10302 case LE_EXPR:
10303 case GE_EXPR:
10304 case LT_EXPR:
10305 case GT_EXPR:
10306 case COMPOUND_EXPR:
10307 case DOTSTAR_EXPR:
10308 case MEMBER_REF:
10309 case PREDECREMENT_EXPR:
10310 case PREINCREMENT_EXPR:
10311 case POSTDECREMENT_EXPR:
10312 case POSTINCREMENT_EXPR:
10313 return build_nt
10314 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10315 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10317 case SCOPE_REF:
10318 return build_qualified_name (/*type=*/NULL_TREE,
10319 tsubst_copy (TREE_OPERAND (t, 0),
10320 args, complain, in_decl),
10321 tsubst_copy (TREE_OPERAND (t, 1),
10322 args, complain, in_decl),
10323 QUALIFIED_NAME_IS_TEMPLATE (t));
10325 case ARRAY_REF:
10326 return build_nt
10327 (ARRAY_REF,
10328 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10329 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10330 NULL_TREE, NULL_TREE);
10332 case CALL_EXPR:
10334 int n = VL_EXP_OPERAND_LENGTH (t);
10335 tree result = build_vl_exp (CALL_EXPR, n);
10336 int i;
10337 for (i = 0; i < n; i++)
10338 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10339 complain, in_decl);
10340 return result;
10343 case COND_EXPR:
10344 case MODOP_EXPR:
10345 case PSEUDO_DTOR_EXPR:
10347 r = build_nt
10348 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10349 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10350 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10351 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10352 return r;
10355 case NEW_EXPR:
10357 r = build_nt
10358 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10359 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10360 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10361 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10362 return r;
10365 case DELETE_EXPR:
10367 r = build_nt
10368 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10369 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10370 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10371 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10372 return r;
10375 case TEMPLATE_ID_EXPR:
10377 /* Substituted template arguments */
10378 tree fn = TREE_OPERAND (t, 0);
10379 tree targs = TREE_OPERAND (t, 1);
10381 fn = tsubst_copy (fn, args, complain, in_decl);
10382 if (targs)
10383 targs = tsubst_template_args (targs, args, complain, in_decl);
10385 return lookup_template_function (fn, targs);
10388 case TREE_LIST:
10390 tree purpose, value, chain;
10392 if (t == void_list_node)
10393 return t;
10395 purpose = TREE_PURPOSE (t);
10396 if (purpose)
10397 purpose = tsubst_copy (purpose, args, complain, in_decl);
10398 value = TREE_VALUE (t);
10399 if (value)
10400 value = tsubst_copy (value, args, complain, in_decl);
10401 chain = TREE_CHAIN (t);
10402 if (chain && chain != void_type_node)
10403 chain = tsubst_copy (chain, args, complain, in_decl);
10404 if (purpose == TREE_PURPOSE (t)
10405 && value == TREE_VALUE (t)
10406 && chain == TREE_CHAIN (t))
10407 return t;
10408 return tree_cons (purpose, value, chain);
10411 case RECORD_TYPE:
10412 case UNION_TYPE:
10413 case ENUMERAL_TYPE:
10414 case INTEGER_TYPE:
10415 case TEMPLATE_TYPE_PARM:
10416 case TEMPLATE_TEMPLATE_PARM:
10417 case BOUND_TEMPLATE_TEMPLATE_PARM:
10418 case TEMPLATE_PARM_INDEX:
10419 case POINTER_TYPE:
10420 case REFERENCE_TYPE:
10421 case OFFSET_TYPE:
10422 case FUNCTION_TYPE:
10423 case METHOD_TYPE:
10424 case ARRAY_TYPE:
10425 case TYPENAME_TYPE:
10426 case UNBOUND_CLASS_TEMPLATE:
10427 case TYPEOF_TYPE:
10428 case DECLTYPE_TYPE:
10429 case TYPE_DECL:
10430 return tsubst (t, args, complain, in_decl);
10432 case IDENTIFIER_NODE:
10433 if (IDENTIFIER_TYPENAME_P (t))
10435 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10436 return mangle_conv_op_name_for_type (new_type);
10438 else
10439 return t;
10441 case CONSTRUCTOR:
10442 /* This is handled by tsubst_copy_and_build. */
10443 gcc_unreachable ();
10445 case VA_ARG_EXPR:
10446 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10447 in_decl),
10448 tsubst (TREE_TYPE (t), args, complain, in_decl));
10450 case CLEANUP_POINT_EXPR:
10451 /* We shouldn't have built any of these during initial template
10452 generation. Instead, they should be built during instantiation
10453 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10454 gcc_unreachable ();
10456 case OFFSET_REF:
10457 mark_used (TREE_OPERAND (t, 1));
10458 return t;
10460 case EXPR_PACK_EXPANSION:
10461 error ("invalid use of pack expansion expression");
10462 return error_mark_node;
10464 case NONTYPE_ARGUMENT_PACK:
10465 error ("use %<...%> to expand argument pack");
10466 return error_mark_node;
10468 default:
10469 return t;
10473 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10475 static tree
10476 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10477 tree in_decl)
10479 tree new_clauses = NULL, nc, oc;
10481 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10483 nc = copy_node (oc);
10484 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10485 new_clauses = nc;
10487 switch (OMP_CLAUSE_CODE (nc))
10489 case OMP_CLAUSE_LASTPRIVATE:
10490 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10492 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10493 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10494 in_decl, /*integral_constant_expression_p=*/false);
10495 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10496 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10498 /* FALLTHRU */
10499 case OMP_CLAUSE_PRIVATE:
10500 case OMP_CLAUSE_SHARED:
10501 case OMP_CLAUSE_FIRSTPRIVATE:
10502 case OMP_CLAUSE_REDUCTION:
10503 case OMP_CLAUSE_COPYIN:
10504 case OMP_CLAUSE_COPYPRIVATE:
10505 case OMP_CLAUSE_IF:
10506 case OMP_CLAUSE_NUM_THREADS:
10507 case OMP_CLAUSE_SCHEDULE:
10508 case OMP_CLAUSE_COLLAPSE:
10509 OMP_CLAUSE_OPERAND (nc, 0)
10510 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10511 in_decl, /*integral_constant_expression_p=*/false);
10512 break;
10513 case OMP_CLAUSE_NOWAIT:
10514 case OMP_CLAUSE_ORDERED:
10515 case OMP_CLAUSE_DEFAULT:
10516 case OMP_CLAUSE_UNTIED:
10517 break;
10518 default:
10519 gcc_unreachable ();
10523 return finish_omp_clauses (nreverse (new_clauses));
10526 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10528 static tree
10529 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10530 tree in_decl)
10532 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10534 tree purpose, value, chain;
10536 if (t == NULL)
10537 return t;
10539 if (TREE_CODE (t) != TREE_LIST)
10540 return tsubst_copy_and_build (t, args, complain, in_decl,
10541 /*function_p=*/false,
10542 /*integral_constant_expression_p=*/false);
10544 if (t == void_list_node)
10545 return t;
10547 purpose = TREE_PURPOSE (t);
10548 if (purpose)
10549 purpose = RECUR (purpose);
10550 value = TREE_VALUE (t);
10551 if (value)
10552 value = RECUR (value);
10553 chain = TREE_CHAIN (t);
10554 if (chain && chain != void_type_node)
10555 chain = RECUR (chain);
10556 return tree_cons (purpose, value, chain);
10557 #undef RECUR
10560 /* Substitute one OMP_FOR iterator. */
10562 static void
10563 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10564 tree condv, tree incrv, tree *clauses,
10565 tree args, tsubst_flags_t complain, tree in_decl,
10566 bool integral_constant_expression_p)
10568 #define RECUR(NODE) \
10569 tsubst_expr ((NODE), args, complain, in_decl, \
10570 integral_constant_expression_p)
10571 tree decl, init, cond, incr, auto_node;
10573 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10574 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10575 decl = RECUR (TREE_OPERAND (init, 0));
10576 init = TREE_OPERAND (init, 1);
10577 auto_node = type_uses_auto (TREE_TYPE (decl));
10578 if (auto_node && init)
10580 tree init_expr = init;
10581 if (TREE_CODE (init_expr) == DECL_EXPR)
10582 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
10583 init_expr = RECUR (init_expr);
10584 TREE_TYPE (decl)
10585 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
10587 gcc_assert (!type_dependent_expression_p (decl));
10589 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10591 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10592 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10593 if (TREE_CODE (incr) == MODIFY_EXPR)
10594 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10595 RECUR (TREE_OPERAND (incr, 1)),
10596 complain);
10597 else
10598 incr = RECUR (incr);
10599 TREE_VEC_ELT (declv, i) = decl;
10600 TREE_VEC_ELT (initv, i) = init;
10601 TREE_VEC_ELT (condv, i) = cond;
10602 TREE_VEC_ELT (incrv, i) = incr;
10603 return;
10606 if (init && TREE_CODE (init) != DECL_EXPR)
10608 tree c;
10609 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10611 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10612 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10613 && OMP_CLAUSE_DECL (c) == decl)
10614 break;
10615 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10616 && OMP_CLAUSE_DECL (c) == decl)
10617 error ("iteration variable %qD should not be firstprivate", decl);
10618 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10619 && OMP_CLAUSE_DECL (c) == decl)
10620 error ("iteration variable %qD should not be reduction", decl);
10622 if (c == NULL)
10624 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
10625 OMP_CLAUSE_DECL (c) = decl;
10626 c = finish_omp_clauses (c);
10627 if (c)
10629 OMP_CLAUSE_CHAIN (c) = *clauses;
10630 *clauses = c;
10634 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
10635 if (COMPARISON_CLASS_P (cond))
10636 cond = build2 (TREE_CODE (cond), boolean_type_node,
10637 RECUR (TREE_OPERAND (cond, 0)),
10638 RECUR (TREE_OPERAND (cond, 1)));
10639 else
10640 cond = RECUR (cond);
10641 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10642 switch (TREE_CODE (incr))
10644 case PREINCREMENT_EXPR:
10645 case PREDECREMENT_EXPR:
10646 case POSTINCREMENT_EXPR:
10647 case POSTDECREMENT_EXPR:
10648 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
10649 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
10650 break;
10651 case MODIFY_EXPR:
10652 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10653 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10655 tree rhs = TREE_OPERAND (incr, 1);
10656 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10657 RECUR (TREE_OPERAND (incr, 0)),
10658 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10659 RECUR (TREE_OPERAND (rhs, 0)),
10660 RECUR (TREE_OPERAND (rhs, 1))));
10662 else
10663 incr = RECUR (incr);
10664 break;
10665 case MODOP_EXPR:
10666 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10667 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10669 tree lhs = RECUR (TREE_OPERAND (incr, 0));
10670 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
10671 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
10672 TREE_TYPE (decl), lhs,
10673 RECUR (TREE_OPERAND (incr, 2))));
10675 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
10676 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
10677 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
10679 tree rhs = TREE_OPERAND (incr, 2);
10680 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10681 RECUR (TREE_OPERAND (incr, 0)),
10682 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10683 RECUR (TREE_OPERAND (rhs, 0)),
10684 RECUR (TREE_OPERAND (rhs, 1))));
10686 else
10687 incr = RECUR (incr);
10688 break;
10689 default:
10690 incr = RECUR (incr);
10691 break;
10694 TREE_VEC_ELT (declv, i) = decl;
10695 TREE_VEC_ELT (initv, i) = init;
10696 TREE_VEC_ELT (condv, i) = cond;
10697 TREE_VEC_ELT (incrv, i) = incr;
10698 #undef RECUR
10701 /* Like tsubst_copy for expressions, etc. but also does semantic
10702 processing. */
10704 static tree
10705 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
10706 bool integral_constant_expression_p)
10708 #define RECUR(NODE) \
10709 tsubst_expr ((NODE), args, complain, in_decl, \
10710 integral_constant_expression_p)
10712 tree stmt, tmp;
10714 if (t == NULL_TREE || t == error_mark_node)
10715 return t;
10717 if (EXPR_HAS_LOCATION (t))
10718 input_location = EXPR_LOCATION (t);
10719 if (STATEMENT_CODE_P (TREE_CODE (t)))
10720 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
10722 switch (TREE_CODE (t))
10724 case STATEMENT_LIST:
10726 tree_stmt_iterator i;
10727 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
10728 RECUR (tsi_stmt (i));
10729 break;
10732 case CTOR_INITIALIZER:
10733 finish_mem_initializers (tsubst_initializer_list
10734 (TREE_OPERAND (t, 0), args));
10735 break;
10737 case RETURN_EXPR:
10738 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
10739 break;
10741 case EXPR_STMT:
10742 tmp = RECUR (EXPR_STMT_EXPR (t));
10743 if (EXPR_STMT_STMT_EXPR_RESULT (t))
10744 finish_stmt_expr_expr (tmp, cur_stmt_expr);
10745 else
10746 finish_expr_stmt (tmp);
10747 break;
10749 case USING_STMT:
10750 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
10751 break;
10753 case DECL_EXPR:
10755 tree decl;
10756 tree init;
10758 decl = DECL_EXPR_DECL (t);
10759 if (TREE_CODE (decl) == LABEL_DECL)
10760 finish_label_decl (DECL_NAME (decl));
10761 else if (TREE_CODE (decl) == USING_DECL)
10763 tree scope = USING_DECL_SCOPE (decl);
10764 tree name = DECL_NAME (decl);
10765 tree decl;
10767 scope = RECUR (scope);
10768 decl = lookup_qualified_name (scope, name,
10769 /*is_type_p=*/false,
10770 /*complain=*/false);
10771 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
10772 qualified_name_lookup_error (scope, name, decl, input_location);
10773 else
10774 do_local_using_decl (decl, scope, name);
10776 else
10778 init = DECL_INITIAL (decl);
10779 decl = tsubst (decl, args, complain, in_decl);
10780 if (decl != error_mark_node)
10782 /* By marking the declaration as instantiated, we avoid
10783 trying to instantiate it. Since instantiate_decl can't
10784 handle local variables, and since we've already done
10785 all that needs to be done, that's the right thing to
10786 do. */
10787 if (TREE_CODE (decl) == VAR_DECL)
10788 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10789 if (TREE_CODE (decl) == VAR_DECL
10790 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
10791 /* Anonymous aggregates are a special case. */
10792 finish_anon_union (decl);
10793 else
10795 maybe_push_decl (decl);
10796 if (TREE_CODE (decl) == VAR_DECL
10797 && DECL_PRETTY_FUNCTION_P (decl))
10799 /* For __PRETTY_FUNCTION__ we have to adjust the
10800 initializer. */
10801 const char *const name
10802 = cxx_printable_name (current_function_decl, 2);
10803 init = cp_fname_init (name, &TREE_TYPE (decl));
10805 else
10807 tree t = RECUR (init);
10809 if (init && !t)
10810 /* If we had an initializer but it
10811 instantiated to nothing,
10812 value-initialize the object. This will
10813 only occur when the initializer was a
10814 pack expansion where the parameter packs
10815 used in that expansion were of length
10816 zero. */
10817 init = build_value_init (TREE_TYPE (decl));
10818 else
10819 init = t;
10822 finish_decl (decl, init, NULL_TREE, NULL_TREE);
10827 /* A DECL_EXPR can also be used as an expression, in the condition
10828 clause of an if/for/while construct. */
10829 return decl;
10832 case FOR_STMT:
10833 stmt = begin_for_stmt ();
10834 RECUR (FOR_INIT_STMT (t));
10835 finish_for_init_stmt (stmt);
10836 tmp = RECUR (FOR_COND (t));
10837 finish_for_cond (tmp, stmt);
10838 tmp = RECUR (FOR_EXPR (t));
10839 finish_for_expr (tmp, stmt);
10840 RECUR (FOR_BODY (t));
10841 finish_for_stmt (stmt);
10842 break;
10844 case WHILE_STMT:
10845 stmt = begin_while_stmt ();
10846 tmp = RECUR (WHILE_COND (t));
10847 finish_while_stmt_cond (tmp, stmt);
10848 RECUR (WHILE_BODY (t));
10849 finish_while_stmt (stmt);
10850 break;
10852 case DO_STMT:
10853 stmt = begin_do_stmt ();
10854 RECUR (DO_BODY (t));
10855 finish_do_body (stmt);
10856 tmp = RECUR (DO_COND (t));
10857 finish_do_stmt (tmp, stmt);
10858 break;
10860 case IF_STMT:
10861 stmt = begin_if_stmt ();
10862 tmp = RECUR (IF_COND (t));
10863 finish_if_stmt_cond (tmp, stmt);
10864 RECUR (THEN_CLAUSE (t));
10865 finish_then_clause (stmt);
10867 if (ELSE_CLAUSE (t))
10869 begin_else_clause (stmt);
10870 RECUR (ELSE_CLAUSE (t));
10871 finish_else_clause (stmt);
10874 finish_if_stmt (stmt);
10875 break;
10877 case BIND_EXPR:
10878 if (BIND_EXPR_BODY_BLOCK (t))
10879 stmt = begin_function_body ();
10880 else
10881 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
10882 ? BCS_TRY_BLOCK : 0);
10884 RECUR (BIND_EXPR_BODY (t));
10886 if (BIND_EXPR_BODY_BLOCK (t))
10887 finish_function_body (stmt);
10888 else
10889 finish_compound_stmt (stmt);
10890 break;
10892 case BREAK_STMT:
10893 finish_break_stmt ();
10894 break;
10896 case CONTINUE_STMT:
10897 finish_continue_stmt ();
10898 break;
10900 case SWITCH_STMT:
10901 stmt = begin_switch_stmt ();
10902 tmp = RECUR (SWITCH_STMT_COND (t));
10903 finish_switch_cond (tmp, stmt);
10904 RECUR (SWITCH_STMT_BODY (t));
10905 finish_switch_stmt (stmt);
10906 break;
10908 case CASE_LABEL_EXPR:
10909 finish_case_label (EXPR_LOCATION (t),
10910 RECUR (CASE_LOW (t)),
10911 RECUR (CASE_HIGH (t)));
10912 break;
10914 case LABEL_EXPR:
10916 tree decl = LABEL_EXPR_LABEL (t);
10917 tree label;
10919 label = finish_label_stmt (DECL_NAME (decl));
10920 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
10921 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
10923 break;
10925 case GOTO_EXPR:
10926 tmp = GOTO_DESTINATION (t);
10927 if (TREE_CODE (tmp) != LABEL_DECL)
10928 /* Computed goto's must be tsubst'd into. On the other hand,
10929 non-computed gotos must not be; the identifier in question
10930 will have no binding. */
10931 tmp = RECUR (tmp);
10932 else
10933 tmp = DECL_NAME (tmp);
10934 finish_goto_stmt (tmp);
10935 break;
10937 case ASM_EXPR:
10938 tmp = finish_asm_stmt
10939 (ASM_VOLATILE_P (t),
10940 RECUR (ASM_STRING (t)),
10941 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10942 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10943 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10945 tree asm_expr = tmp;
10946 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10947 asm_expr = TREE_OPERAND (asm_expr, 0);
10948 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10950 break;
10952 case TRY_BLOCK:
10953 if (CLEANUP_P (t))
10955 stmt = begin_try_block ();
10956 RECUR (TRY_STMTS (t));
10957 finish_cleanup_try_block (stmt);
10958 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10960 else
10962 tree compound_stmt = NULL_TREE;
10964 if (FN_TRY_BLOCK_P (t))
10965 stmt = begin_function_try_block (&compound_stmt);
10966 else
10967 stmt = begin_try_block ();
10969 RECUR (TRY_STMTS (t));
10971 if (FN_TRY_BLOCK_P (t))
10972 finish_function_try_block (stmt);
10973 else
10974 finish_try_block (stmt);
10976 RECUR (TRY_HANDLERS (t));
10977 if (FN_TRY_BLOCK_P (t))
10978 finish_function_handler_sequence (stmt, compound_stmt);
10979 else
10980 finish_handler_sequence (stmt);
10982 break;
10984 case HANDLER:
10986 tree decl = HANDLER_PARMS (t);
10988 if (decl)
10990 decl = tsubst (decl, args, complain, in_decl);
10991 /* Prevent instantiate_decl from trying to instantiate
10992 this variable. We've already done all that needs to be
10993 done. */
10994 if (decl != error_mark_node)
10995 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10997 stmt = begin_handler ();
10998 finish_handler_parms (decl, stmt);
10999 RECUR (HANDLER_BODY (t));
11000 finish_handler (stmt);
11002 break;
11004 case TAG_DEFN:
11005 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11006 break;
11008 case STATIC_ASSERT:
11010 tree condition =
11011 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11012 args,
11013 complain, in_decl,
11014 /*integral_constant_expression_p=*/true);
11015 finish_static_assert (condition,
11016 STATIC_ASSERT_MESSAGE (t),
11017 STATIC_ASSERT_SOURCE_LOCATION (t),
11018 /*member_p=*/false);
11020 break;
11022 case OMP_PARALLEL:
11023 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11024 args, complain, in_decl);
11025 stmt = begin_omp_parallel ();
11026 RECUR (OMP_PARALLEL_BODY (t));
11027 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11028 = OMP_PARALLEL_COMBINED (t);
11029 break;
11031 case OMP_TASK:
11032 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11033 args, complain, in_decl);
11034 stmt = begin_omp_task ();
11035 RECUR (OMP_TASK_BODY (t));
11036 finish_omp_task (tmp, stmt);
11037 break;
11039 case OMP_FOR:
11041 tree clauses, body, pre_body;
11042 tree declv, initv, condv, incrv;
11043 int i;
11045 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11046 args, complain, in_decl);
11047 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11048 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11049 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11050 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11052 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11053 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11054 &clauses, args, complain, in_decl,
11055 integral_constant_expression_p);
11057 stmt = begin_omp_structured_block ();
11059 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11060 if (TREE_VEC_ELT (initv, i) == NULL
11061 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11062 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11063 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11065 tree init = RECUR (TREE_VEC_ELT (initv, i));
11066 gcc_assert (init == TREE_VEC_ELT (declv, i));
11067 TREE_VEC_ELT (initv, i) = NULL_TREE;
11069 else
11071 tree decl_expr = TREE_VEC_ELT (initv, i);
11072 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11073 gcc_assert (init != NULL);
11074 TREE_VEC_ELT (initv, i) = RECUR (init);
11075 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11076 RECUR (decl_expr);
11077 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11080 pre_body = push_stmt_list ();
11081 RECUR (OMP_FOR_PRE_BODY (t));
11082 pre_body = pop_stmt_list (pre_body);
11084 body = push_stmt_list ();
11085 RECUR (OMP_FOR_BODY (t));
11086 body = pop_stmt_list (body);
11088 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11089 body, pre_body, clauses);
11091 add_stmt (finish_omp_structured_block (stmt));
11093 break;
11095 case OMP_SECTIONS:
11096 case OMP_SINGLE:
11097 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11098 stmt = push_stmt_list ();
11099 RECUR (OMP_BODY (t));
11100 stmt = pop_stmt_list (stmt);
11102 t = copy_node (t);
11103 OMP_BODY (t) = stmt;
11104 OMP_CLAUSES (t) = tmp;
11105 add_stmt (t);
11106 break;
11108 case OMP_SECTION:
11109 case OMP_CRITICAL:
11110 case OMP_MASTER:
11111 case OMP_ORDERED:
11112 stmt = push_stmt_list ();
11113 RECUR (OMP_BODY (t));
11114 stmt = pop_stmt_list (stmt);
11116 t = copy_node (t);
11117 OMP_BODY (t) = stmt;
11118 add_stmt (t);
11119 break;
11121 case OMP_ATOMIC:
11122 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11124 tree op1 = TREE_OPERAND (t, 1);
11125 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11126 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11127 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11129 break;
11131 case EXPR_PACK_EXPANSION:
11132 error ("invalid use of pack expansion expression");
11133 return error_mark_node;
11135 case NONTYPE_ARGUMENT_PACK:
11136 error ("use %<...%> to expand argument pack");
11137 return error_mark_node;
11139 default:
11140 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11142 return tsubst_copy_and_build (t, args, complain, in_decl,
11143 /*function_p=*/false,
11144 integral_constant_expression_p);
11147 return NULL_TREE;
11148 #undef RECUR
11151 /* T is a postfix-expression that is not being used in a function
11152 call. Return the substituted version of T. */
11154 static tree
11155 tsubst_non_call_postfix_expression (tree t, tree args,
11156 tsubst_flags_t complain,
11157 tree in_decl)
11159 if (TREE_CODE (t) == SCOPE_REF)
11160 t = tsubst_qualified_id (t, args, complain, in_decl,
11161 /*done=*/false, /*address_p=*/false);
11162 else
11163 t = tsubst_copy_and_build (t, args, complain, in_decl,
11164 /*function_p=*/false,
11165 /*integral_constant_expression_p=*/false);
11167 return t;
11170 /* Like tsubst but deals with expressions and performs semantic
11171 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11173 tree
11174 tsubst_copy_and_build (tree t,
11175 tree args,
11176 tsubst_flags_t complain,
11177 tree in_decl,
11178 bool function_p,
11179 bool integral_constant_expression_p)
11181 #define RECUR(NODE) \
11182 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11183 /*function_p=*/false, \
11184 integral_constant_expression_p)
11186 tree op1;
11188 if (t == NULL_TREE || t == error_mark_node)
11189 return t;
11191 switch (TREE_CODE (t))
11193 case USING_DECL:
11194 t = DECL_NAME (t);
11195 /* Fall through. */
11196 case IDENTIFIER_NODE:
11198 tree decl;
11199 cp_id_kind idk;
11200 bool non_integral_constant_expression_p;
11201 const char *error_msg;
11203 if (IDENTIFIER_TYPENAME_P (t))
11205 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11206 t = mangle_conv_op_name_for_type (new_type);
11209 /* Look up the name. */
11210 decl = lookup_name (t);
11212 /* By convention, expressions use ERROR_MARK_NODE to indicate
11213 failure, not NULL_TREE. */
11214 if (decl == NULL_TREE)
11215 decl = error_mark_node;
11217 decl = finish_id_expression (t, decl, NULL_TREE,
11218 &idk,
11219 integral_constant_expression_p,
11220 /*allow_non_integral_constant_expression_p=*/false,
11221 &non_integral_constant_expression_p,
11222 /*template_p=*/false,
11223 /*done=*/true,
11224 /*address_p=*/false,
11225 /*template_arg_p=*/false,
11226 &error_msg,
11227 input_location);
11228 if (error_msg)
11229 error (error_msg);
11230 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11231 decl = unqualified_name_lookup_error (decl);
11232 return decl;
11235 case TEMPLATE_ID_EXPR:
11237 tree object;
11238 tree templ = RECUR (TREE_OPERAND (t, 0));
11239 tree targs = TREE_OPERAND (t, 1);
11241 if (targs)
11242 targs = tsubst_template_args (targs, args, complain, in_decl);
11244 if (TREE_CODE (templ) == COMPONENT_REF)
11246 object = TREE_OPERAND (templ, 0);
11247 templ = TREE_OPERAND (templ, 1);
11249 else
11250 object = NULL_TREE;
11251 templ = lookup_template_function (templ, targs);
11253 if (object)
11254 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11255 object, templ, NULL_TREE);
11256 else
11257 return baselink_for_fns (templ);
11260 case INDIRECT_REF:
11262 tree r = RECUR (TREE_OPERAND (t, 0));
11264 if (REFERENCE_REF_P (t))
11266 /* A type conversion to reference type will be enclosed in
11267 such an indirect ref, but the substitution of the cast
11268 will have also added such an indirect ref. */
11269 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11270 r = convert_from_reference (r);
11272 else
11273 r = build_x_indirect_ref (r, "unary *", complain);
11274 return r;
11277 case NOP_EXPR:
11278 return build_nop
11279 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11280 RECUR (TREE_OPERAND (t, 0)));
11282 case CAST_EXPR:
11283 case REINTERPRET_CAST_EXPR:
11284 case CONST_CAST_EXPR:
11285 case DYNAMIC_CAST_EXPR:
11286 case STATIC_CAST_EXPR:
11288 tree type;
11289 tree op;
11291 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11292 if (integral_constant_expression_p
11293 && !cast_valid_in_integral_constant_expression_p (type))
11295 if (complain & tf_error)
11296 error ("a cast to a type other than an integral or "
11297 "enumeration type cannot appear in a constant-expression");
11298 return error_mark_node;
11301 op = RECUR (TREE_OPERAND (t, 0));
11303 switch (TREE_CODE (t))
11305 case CAST_EXPR:
11306 return build_functional_cast (type, op, complain);
11307 case REINTERPRET_CAST_EXPR:
11308 return build_reinterpret_cast (type, op, complain);
11309 case CONST_CAST_EXPR:
11310 return build_const_cast (type, op, complain);
11311 case DYNAMIC_CAST_EXPR:
11312 return build_dynamic_cast (type, op, complain);
11313 case STATIC_CAST_EXPR:
11314 return build_static_cast (type, op, complain);
11315 default:
11316 gcc_unreachable ();
11320 case POSTDECREMENT_EXPR:
11321 case POSTINCREMENT_EXPR:
11322 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11323 args, complain, in_decl);
11324 return build_x_unary_op (TREE_CODE (t), op1, complain);
11326 case PREDECREMENT_EXPR:
11327 case PREINCREMENT_EXPR:
11328 case NEGATE_EXPR:
11329 case BIT_NOT_EXPR:
11330 case ABS_EXPR:
11331 case TRUTH_NOT_EXPR:
11332 case UNARY_PLUS_EXPR: /* Unary + */
11333 case REALPART_EXPR:
11334 case IMAGPART_EXPR:
11335 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11336 complain);
11338 case ADDR_EXPR:
11339 op1 = TREE_OPERAND (t, 0);
11340 if (TREE_CODE (op1) == SCOPE_REF)
11341 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11342 /*done=*/true, /*address_p=*/true);
11343 else
11344 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11345 in_decl);
11346 if (TREE_CODE (op1) == LABEL_DECL)
11347 return finish_label_address_expr (DECL_NAME (op1),
11348 EXPR_LOCATION (op1));
11349 return build_x_unary_op (ADDR_EXPR, op1, complain);
11351 case PLUS_EXPR:
11352 case MINUS_EXPR:
11353 case MULT_EXPR:
11354 case TRUNC_DIV_EXPR:
11355 case CEIL_DIV_EXPR:
11356 case FLOOR_DIV_EXPR:
11357 case ROUND_DIV_EXPR:
11358 case EXACT_DIV_EXPR:
11359 case BIT_AND_EXPR:
11360 case BIT_IOR_EXPR:
11361 case BIT_XOR_EXPR:
11362 case TRUNC_MOD_EXPR:
11363 case FLOOR_MOD_EXPR:
11364 case TRUTH_ANDIF_EXPR:
11365 case TRUTH_ORIF_EXPR:
11366 case TRUTH_AND_EXPR:
11367 case TRUTH_OR_EXPR:
11368 case RSHIFT_EXPR:
11369 case LSHIFT_EXPR:
11370 case RROTATE_EXPR:
11371 case LROTATE_EXPR:
11372 case EQ_EXPR:
11373 case NE_EXPR:
11374 case MAX_EXPR:
11375 case MIN_EXPR:
11376 case LE_EXPR:
11377 case GE_EXPR:
11378 case LT_EXPR:
11379 case GT_EXPR:
11380 case MEMBER_REF:
11381 case DOTSTAR_EXPR:
11382 return build_x_binary_op
11383 (TREE_CODE (t),
11384 RECUR (TREE_OPERAND (t, 0)),
11385 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11386 ? ERROR_MARK
11387 : TREE_CODE (TREE_OPERAND (t, 0))),
11388 RECUR (TREE_OPERAND (t, 1)),
11389 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11390 ? ERROR_MARK
11391 : TREE_CODE (TREE_OPERAND (t, 1))),
11392 /*overloaded_p=*/NULL,
11393 complain);
11395 case SCOPE_REF:
11396 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11397 /*address_p=*/false);
11398 case ARRAY_REF:
11399 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11400 args, complain, in_decl);
11401 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11403 case SIZEOF_EXPR:
11404 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11405 return tsubst_copy (t, args, complain, in_decl);
11406 /* Fall through */
11408 case ALIGNOF_EXPR:
11409 op1 = TREE_OPERAND (t, 0);
11410 if (!args)
11412 /* When there are no ARGS, we are trying to evaluate a
11413 non-dependent expression from the parser. Trying to do
11414 the substitutions may not work. */
11415 if (!TYPE_P (op1))
11416 op1 = TREE_TYPE (op1);
11418 else
11420 ++cp_unevaluated_operand;
11421 ++c_inhibit_evaluation_warnings;
11422 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11423 /*function_p=*/false,
11424 /*integral_constant_expression_p=*/false);
11425 --cp_unevaluated_operand;
11426 --c_inhibit_evaluation_warnings;
11428 if (TYPE_P (op1))
11429 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11430 complain & tf_error);
11431 else
11432 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11433 complain & tf_error);
11435 case MODOP_EXPR:
11437 tree r = build_x_modify_expr
11438 (RECUR (TREE_OPERAND (t, 0)),
11439 TREE_CODE (TREE_OPERAND (t, 1)),
11440 RECUR (TREE_OPERAND (t, 2)),
11441 complain);
11442 /* TREE_NO_WARNING must be set if either the expression was
11443 parenthesized or it uses an operator such as >>= rather
11444 than plain assignment. In the former case, it was already
11445 set and must be copied. In the latter case,
11446 build_x_modify_expr sets it and it must not be reset
11447 here. */
11448 if (TREE_NO_WARNING (t))
11449 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11450 return r;
11453 case ARROW_EXPR:
11454 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11455 args, complain, in_decl);
11456 /* Remember that there was a reference to this entity. */
11457 if (DECL_P (op1))
11458 mark_used (op1);
11459 return build_x_arrow (op1);
11461 case NEW_EXPR:
11463 tree placement = RECUR (TREE_OPERAND (t, 0));
11464 tree init = RECUR (TREE_OPERAND (t, 3));
11465 VEC(tree,gc) *placement_vec;
11466 VEC(tree,gc) *init_vec;
11467 tree ret;
11469 if (placement == NULL_TREE)
11470 placement_vec = NULL;
11471 else
11473 placement_vec = make_tree_vector ();
11474 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
11475 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
11478 /* If there was an initializer in the original tree, but it
11479 instantiated to an empty list, then we should pass a
11480 non-NULL empty vector to tell build_new that it was an
11481 empty initializer() rather than no initializer. This can
11482 only happen when the initializer is a pack expansion whose
11483 parameter packs are of length zero. */
11484 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
11485 init_vec = NULL;
11486 else
11488 init_vec = make_tree_vector ();
11489 if (init == void_zero_node)
11490 gcc_assert (init_vec != NULL);
11491 else
11493 for (; init != NULL_TREE; init = TREE_CHAIN (init))
11494 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
11498 ret = build_new (&placement_vec,
11499 RECUR (TREE_OPERAND (t, 1)),
11500 RECUR (TREE_OPERAND (t, 2)),
11501 &init_vec,
11502 NEW_EXPR_USE_GLOBAL (t),
11503 complain);
11505 if (placement_vec != NULL)
11506 release_tree_vector (placement_vec);
11507 if (init_vec != NULL)
11508 release_tree_vector (init_vec);
11510 return ret;
11513 case DELETE_EXPR:
11514 return delete_sanity
11515 (RECUR (TREE_OPERAND (t, 0)),
11516 RECUR (TREE_OPERAND (t, 1)),
11517 DELETE_EXPR_USE_VEC (t),
11518 DELETE_EXPR_USE_GLOBAL (t));
11520 case COMPOUND_EXPR:
11521 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
11522 RECUR (TREE_OPERAND (t, 1)),
11523 complain);
11525 case CALL_EXPR:
11527 tree function;
11528 VEC(tree,gc) *call_args;
11529 unsigned int nargs, i;
11530 bool qualified_p;
11531 bool koenig_p;
11532 tree ret;
11534 function = CALL_EXPR_FN (t);
11535 /* When we parsed the expression, we determined whether or
11536 not Koenig lookup should be performed. */
11537 koenig_p = KOENIG_LOOKUP_P (t);
11538 if (TREE_CODE (function) == SCOPE_REF)
11540 qualified_p = true;
11541 function = tsubst_qualified_id (function, args, complain, in_decl,
11542 /*done=*/false,
11543 /*address_p=*/false);
11545 else
11547 if (TREE_CODE (function) == COMPONENT_REF)
11549 tree op = TREE_OPERAND (function, 1);
11551 qualified_p = (TREE_CODE (op) == SCOPE_REF
11552 || (BASELINK_P (op)
11553 && BASELINK_QUALIFIED_P (op)));
11555 else
11556 qualified_p = false;
11558 function = tsubst_copy_and_build (function, args, complain,
11559 in_decl,
11560 !qualified_p,
11561 integral_constant_expression_p);
11563 if (BASELINK_P (function))
11564 qualified_p = true;
11567 nargs = call_expr_nargs (t);
11568 call_args = make_tree_vector ();
11569 for (i = 0; i < nargs; ++i)
11571 tree arg = CALL_EXPR_ARG (t, i);
11573 if (!PACK_EXPANSION_P (arg))
11574 VEC_safe_push (tree, gc, call_args,
11575 RECUR (CALL_EXPR_ARG (t, i)));
11576 else
11578 /* Expand the pack expansion and push each entry onto
11579 CALL_ARGS. */
11580 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
11581 if (TREE_CODE (arg) == TREE_VEC)
11583 unsigned int len, j;
11585 len = TREE_VEC_LENGTH (arg);
11586 for (j = 0; j < len; ++j)
11588 tree value = TREE_VEC_ELT (arg, j);
11589 if (value != NULL_TREE)
11590 value = convert_from_reference (value);
11591 VEC_safe_push (tree, gc, call_args, value);
11594 else
11596 /* A partial substitution. Add one entry. */
11597 VEC_safe_push (tree, gc, call_args, arg);
11602 /* We do not perform argument-dependent lookup if normal
11603 lookup finds a non-function, in accordance with the
11604 expected resolution of DR 218. */
11605 if (koenig_p
11606 && ((is_overloaded_fn (function)
11607 /* If lookup found a member function, the Koenig lookup is
11608 not appropriate, even if an unqualified-name was used
11609 to denote the function. */
11610 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
11611 || TREE_CODE (function) == IDENTIFIER_NODE)
11612 /* Only do this when substitution turns a dependent call
11613 into a non-dependent call. */
11614 && type_dependent_expression_p_push (t)
11615 && !any_type_dependent_arguments_p (call_args))
11616 function = perform_koenig_lookup (function, call_args);
11618 if (TREE_CODE (function) == IDENTIFIER_NODE)
11620 unqualified_name_lookup_error (function);
11621 release_tree_vector (call_args);
11622 return error_mark_node;
11625 /* Remember that there was a reference to this entity. */
11626 if (DECL_P (function))
11627 mark_used (function);
11629 if (TREE_CODE (function) == OFFSET_REF)
11630 ret = build_offset_ref_call_from_tree (function, &call_args);
11631 else if (TREE_CODE (function) == COMPONENT_REF)
11633 if (!BASELINK_P (TREE_OPERAND (function, 1)))
11634 ret = finish_call_expr (function, &call_args,
11635 /*disallow_virtual=*/false,
11636 /*koenig_p=*/false,
11637 complain);
11638 else
11639 ret = (build_new_method_call
11640 (TREE_OPERAND (function, 0),
11641 TREE_OPERAND (function, 1),
11642 &call_args, NULL_TREE,
11643 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
11644 /*fn_p=*/NULL,
11645 complain));
11647 else
11648 ret = finish_call_expr (function, &call_args,
11649 /*disallow_virtual=*/qualified_p,
11650 koenig_p,
11651 complain);
11653 release_tree_vector (call_args);
11655 return ret;
11658 case COND_EXPR:
11659 return build_x_conditional_expr
11660 (RECUR (TREE_OPERAND (t, 0)),
11661 RECUR (TREE_OPERAND (t, 1)),
11662 RECUR (TREE_OPERAND (t, 2)),
11663 complain);
11665 case PSEUDO_DTOR_EXPR:
11666 return finish_pseudo_destructor_expr
11667 (RECUR (TREE_OPERAND (t, 0)),
11668 RECUR (TREE_OPERAND (t, 1)),
11669 RECUR (TREE_OPERAND (t, 2)));
11671 case TREE_LIST:
11673 tree purpose, value, chain;
11675 if (t == void_list_node)
11676 return t;
11678 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
11679 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
11681 /* We have pack expansions, so expand those and
11682 create a new list out of it. */
11683 tree purposevec = NULL_TREE;
11684 tree valuevec = NULL_TREE;
11685 tree chain;
11686 int i, len = -1;
11688 /* Expand the argument expressions. */
11689 if (TREE_PURPOSE (t))
11690 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
11691 complain, in_decl);
11692 if (TREE_VALUE (t))
11693 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
11694 complain, in_decl);
11696 /* Build the rest of the list. */
11697 chain = TREE_CHAIN (t);
11698 if (chain && chain != void_type_node)
11699 chain = RECUR (chain);
11701 /* Determine the number of arguments. */
11702 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
11704 len = TREE_VEC_LENGTH (purposevec);
11705 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
11707 else if (TREE_CODE (valuevec) == TREE_VEC)
11708 len = TREE_VEC_LENGTH (valuevec);
11709 else
11711 /* Since we only performed a partial substitution into
11712 the argument pack, we only return a single list
11713 node. */
11714 if (purposevec == TREE_PURPOSE (t)
11715 && valuevec == TREE_VALUE (t)
11716 && chain == TREE_CHAIN (t))
11717 return t;
11719 return tree_cons (purposevec, valuevec, chain);
11722 /* Convert the argument vectors into a TREE_LIST */
11723 i = len;
11724 while (i > 0)
11726 /* Grab the Ith values. */
11727 i--;
11728 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
11729 : NULL_TREE;
11730 value
11731 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
11732 : NULL_TREE;
11734 /* Build the list (backwards). */
11735 chain = tree_cons (purpose, value, chain);
11738 return chain;
11741 purpose = TREE_PURPOSE (t);
11742 if (purpose)
11743 purpose = RECUR (purpose);
11744 value = TREE_VALUE (t);
11745 if (value)
11746 value = RECUR (value);
11747 chain = TREE_CHAIN (t);
11748 if (chain && chain != void_type_node)
11749 chain = RECUR (chain);
11750 if (purpose == TREE_PURPOSE (t)
11751 && value == TREE_VALUE (t)
11752 && chain == TREE_CHAIN (t))
11753 return t;
11754 return tree_cons (purpose, value, chain);
11757 case COMPONENT_REF:
11759 tree object;
11760 tree object_type;
11761 tree member;
11763 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11764 args, complain, in_decl);
11765 /* Remember that there was a reference to this entity. */
11766 if (DECL_P (object))
11767 mark_used (object);
11768 object_type = TREE_TYPE (object);
11770 member = TREE_OPERAND (t, 1);
11771 if (BASELINK_P (member))
11772 member = tsubst_baselink (member,
11773 non_reference (TREE_TYPE (object)),
11774 args, complain, in_decl);
11775 else
11776 member = tsubst_copy (member, args, complain, in_decl);
11777 if (member == error_mark_node)
11778 return error_mark_node;
11780 if (object_type && !CLASS_TYPE_P (object_type))
11782 if (SCALAR_TYPE_P (object_type))
11784 tree s = NULL_TREE;
11785 tree dtor = member;
11787 if (TREE_CODE (dtor) == SCOPE_REF)
11789 s = TREE_OPERAND (dtor, 0);
11790 dtor = TREE_OPERAND (dtor, 1);
11792 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
11794 dtor = TREE_OPERAND (dtor, 0);
11795 if (TYPE_P (dtor))
11796 return finish_pseudo_destructor_expr (object, s, dtor);
11800 else if (TREE_CODE (member) == SCOPE_REF
11801 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
11803 tree tmpl;
11804 tree args;
11806 /* Lookup the template functions now that we know what the
11807 scope is. */
11808 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
11809 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
11810 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
11811 /*is_type_p=*/false,
11812 /*complain=*/false);
11813 if (BASELINK_P (member))
11815 BASELINK_FUNCTIONS (member)
11816 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
11817 args);
11818 member = (adjust_result_of_qualified_name_lookup
11819 (member, BINFO_TYPE (BASELINK_BINFO (member)),
11820 object_type));
11822 else
11824 qualified_name_lookup_error (object_type, tmpl, member,
11825 input_location);
11826 return error_mark_node;
11829 else if (TREE_CODE (member) == SCOPE_REF
11830 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
11831 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
11833 if (complain & tf_error)
11835 if (TYPE_P (TREE_OPERAND (member, 0)))
11836 error ("%qT is not a class or namespace",
11837 TREE_OPERAND (member, 0));
11838 else
11839 error ("%qD is not a class or namespace",
11840 TREE_OPERAND (member, 0));
11842 return error_mark_node;
11844 else if (TREE_CODE (member) == FIELD_DECL)
11845 return finish_non_static_data_member (member, object, NULL_TREE);
11847 return finish_class_member_access_expr (object, member,
11848 /*template_p=*/false,
11849 complain);
11852 case THROW_EXPR:
11853 return build_throw
11854 (RECUR (TREE_OPERAND (t, 0)));
11856 case CONSTRUCTOR:
11858 VEC(constructor_elt,gc) *n;
11859 constructor_elt *ce;
11860 unsigned HOST_WIDE_INT idx;
11861 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11862 bool process_index_p;
11863 int newlen;
11864 bool need_copy_p = false;
11865 tree r;
11867 if (type == error_mark_node)
11868 return error_mark_node;
11870 /* digest_init will do the wrong thing if we let it. */
11871 if (type && TYPE_PTRMEMFUNC_P (type))
11872 return t;
11874 /* We do not want to process the index of aggregate
11875 initializers as they are identifier nodes which will be
11876 looked up by digest_init. */
11877 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
11879 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
11880 newlen = VEC_length (constructor_elt, n);
11881 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
11883 if (ce->index && process_index_p)
11884 ce->index = RECUR (ce->index);
11886 if (PACK_EXPANSION_P (ce->value))
11888 /* Substitute into the pack expansion. */
11889 ce->value = tsubst_pack_expansion (ce->value, args, complain,
11890 in_decl);
11892 if (ce->value == error_mark_node)
11894 else if (TREE_VEC_LENGTH (ce->value) == 1)
11895 /* Just move the argument into place. */
11896 ce->value = TREE_VEC_ELT (ce->value, 0);
11897 else
11899 /* Update the length of the final CONSTRUCTOR
11900 arguments vector, and note that we will need to
11901 copy.*/
11902 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
11903 need_copy_p = true;
11906 else
11907 ce->value = RECUR (ce->value);
11910 if (need_copy_p)
11912 VEC(constructor_elt,gc) *old_n = n;
11914 n = VEC_alloc (constructor_elt, gc, newlen);
11915 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
11916 idx++)
11918 if (TREE_CODE (ce->value) == TREE_VEC)
11920 int i, len = TREE_VEC_LENGTH (ce->value);
11921 for (i = 0; i < len; ++i)
11922 CONSTRUCTOR_APPEND_ELT (n, 0,
11923 TREE_VEC_ELT (ce->value, i));
11925 else
11926 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
11930 r = build_constructor (init_list_type_node, n);
11931 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
11933 if (TREE_HAS_CONSTRUCTOR (t))
11934 return finish_compound_literal (type, r);
11936 return r;
11939 case TYPEID_EXPR:
11941 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
11942 if (TYPE_P (operand_0))
11943 return get_typeid (operand_0);
11944 return build_typeid (operand_0);
11947 case VAR_DECL:
11948 if (!args)
11949 return t;
11950 /* Fall through */
11952 case PARM_DECL:
11954 tree r = tsubst_copy (t, args, complain, in_decl);
11956 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
11957 /* If the original type was a reference, we'll be wrapped in
11958 the appropriate INDIRECT_REF. */
11959 r = convert_from_reference (r);
11960 return r;
11963 case VA_ARG_EXPR:
11964 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
11965 tsubst_copy (TREE_TYPE (t), args, complain,
11966 in_decl));
11968 case OFFSETOF_EXPR:
11969 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
11971 case TRAIT_EXPR:
11973 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
11974 complain, in_decl);
11976 tree type2 = TRAIT_EXPR_TYPE2 (t);
11977 if (type2)
11978 type2 = tsubst_copy (type2, args, complain, in_decl);
11980 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
11983 case STMT_EXPR:
11985 tree old_stmt_expr = cur_stmt_expr;
11986 tree stmt_expr = begin_stmt_expr ();
11988 cur_stmt_expr = stmt_expr;
11989 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
11990 integral_constant_expression_p);
11991 stmt_expr = finish_stmt_expr (stmt_expr, false);
11992 cur_stmt_expr = old_stmt_expr;
11994 return stmt_expr;
11997 case CONST_DECL:
11998 t = tsubst_copy (t, args, complain, in_decl);
11999 /* As in finish_id_expression, we resolve enumeration constants
12000 to their underlying values. */
12001 if (TREE_CODE (t) == CONST_DECL)
12003 used_types_insert (TREE_TYPE (t));
12004 return DECL_INITIAL (t);
12006 return t;
12008 default:
12009 /* Handle Objective-C++ constructs, if appropriate. */
12011 tree subst
12012 = objcp_tsubst_copy_and_build (t, args, complain,
12013 in_decl, /*function_p=*/false);
12014 if (subst)
12015 return subst;
12017 return tsubst_copy (t, args, complain, in_decl);
12020 #undef RECUR
12023 /* Verify that the instantiated ARGS are valid. For type arguments,
12024 make sure that the type's linkage is ok. For non-type arguments,
12025 make sure they are constants if they are integral or enumerations.
12026 Emit an error under control of COMPLAIN, and return TRUE on error. */
12028 static bool
12029 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12031 if (ARGUMENT_PACK_P (t))
12033 tree vec = ARGUMENT_PACK_ARGS (t);
12034 int len = TREE_VEC_LENGTH (vec);
12035 bool result = false;
12036 int i;
12038 for (i = 0; i < len; ++i)
12039 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12040 result = true;
12041 return result;
12043 else if (TYPE_P (t))
12045 /* [basic.link]: A name with no linkage (notably, the name
12046 of a class or enumeration declared in a local scope)
12047 shall not be used to declare an entity with linkage.
12048 This implies that names with no linkage cannot be used as
12049 template arguments. */
12050 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
12052 if (nt)
12054 /* DR 488 makes use of a type with no linkage cause
12055 type deduction to fail. */
12056 if (complain & tf_error)
12058 if (TYPE_ANONYMOUS_P (nt))
12059 error ("%qT is/uses anonymous type", t);
12060 else
12061 error ("template argument for %qD uses local type %qT",
12062 tmpl, t);
12064 return true;
12066 /* In order to avoid all sorts of complications, we do not
12067 allow variably-modified types as template arguments. */
12068 else if (variably_modified_type_p (t, NULL_TREE))
12070 if (complain & tf_error)
12071 error ("%qT is a variably modified type", t);
12072 return true;
12075 /* A non-type argument of integral or enumerated type must be a
12076 constant. */
12077 else if (TREE_TYPE (t)
12078 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12079 && !TREE_CONSTANT (t))
12081 if (complain & tf_error)
12082 error ("integral expression %qE is not constant", t);
12083 return true;
12085 return false;
12088 static bool
12089 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12091 int ix, len = DECL_NTPARMS (tmpl);
12092 bool result = false;
12094 for (ix = 0; ix != len; ix++)
12096 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12097 result = true;
12099 if (result && (complain & tf_error))
12100 error (" trying to instantiate %qD", tmpl);
12101 return result;
12104 /* Instantiate the indicated variable or function template TMPL with
12105 the template arguments in TARG_PTR. */
12107 tree
12108 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
12110 tree fndecl;
12111 tree gen_tmpl;
12112 tree spec;
12113 HOST_WIDE_INT saved_processing_template_decl;
12115 if (tmpl == error_mark_node)
12116 return error_mark_node;
12118 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12120 /* If this function is a clone, handle it specially. */
12121 if (DECL_CLONED_FUNCTION_P (tmpl))
12123 tree spec;
12124 tree clone;
12126 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
12127 complain);
12128 if (spec == error_mark_node)
12129 return error_mark_node;
12131 /* Look for the clone. */
12132 FOR_EACH_CLONE (clone, spec)
12133 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12134 return clone;
12135 /* We should always have found the clone by now. */
12136 gcc_unreachable ();
12137 return NULL_TREE;
12140 /* Check to see if we already have this specialization. */
12141 spec = retrieve_specialization (tmpl, targ_ptr,
12142 /*class_specializations_p=*/false);
12143 if (spec != NULL_TREE)
12144 return spec;
12146 gen_tmpl = most_general_template (tmpl);
12147 if (tmpl != gen_tmpl)
12149 /* The TMPL is a partial instantiation. To get a full set of
12150 arguments we must add the arguments used to perform the
12151 partial instantiation. */
12152 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12153 targ_ptr);
12155 /* Check to see if we already have this specialization. */
12156 spec = retrieve_specialization (gen_tmpl, targ_ptr,
12157 /*class_specializations_p=*/false);
12158 if (spec != NULL_TREE)
12159 return spec;
12162 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12163 complain))
12164 return error_mark_node;
12166 /* We are building a FUNCTION_DECL, during which the access of its
12167 parameters and return types have to be checked. However this
12168 FUNCTION_DECL which is the desired context for access checking
12169 is not built yet. We solve this chicken-and-egg problem by
12170 deferring all checks until we have the FUNCTION_DECL. */
12171 push_deferring_access_checks (dk_deferred);
12173 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12174 (because, for example, we have encountered a non-dependent
12175 function call in the body of a template function and must now
12176 determine which of several overloaded functions will be called),
12177 within the instantiation itself we are not processing a
12178 template. */
12179 saved_processing_template_decl = processing_template_decl;
12180 processing_template_decl = 0;
12181 /* Substitute template parameters to obtain the specialization. */
12182 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12183 targ_ptr, complain, gen_tmpl);
12184 processing_template_decl = saved_processing_template_decl;
12185 if (fndecl == error_mark_node)
12186 return error_mark_node;
12188 /* Now we know the specialization, compute access previously
12189 deferred. */
12190 push_access_scope (fndecl);
12192 /* Some typedefs referenced from within the template code need to be access
12193 checked at template instantiation time, i.e now. These types were
12194 added to the template at parsing time. Let's get those and perfom
12195 the acces checks then. */
12196 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12197 perform_deferred_access_checks ();
12198 pop_access_scope (fndecl);
12199 pop_deferring_access_checks ();
12201 /* The DECL_TI_TEMPLATE should always be the immediate parent
12202 template, not the most general template. */
12203 DECL_TI_TEMPLATE (fndecl) = tmpl;
12205 /* If we've just instantiated the main entry point for a function,
12206 instantiate all the alternate entry points as well. We do this
12207 by cloning the instantiation of the main entry point, not by
12208 instantiating the template clones. */
12209 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12210 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12212 return fndecl;
12215 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12216 NARGS elements of the arguments that are being used when calling
12217 it. TARGS is a vector into which the deduced template arguments
12218 are placed.
12220 Return zero for success, 2 for an incomplete match that doesn't resolve
12221 all the types, and 1 for complete failure. An error message will be
12222 printed only for an incomplete match.
12224 If FN is a conversion operator, or we are trying to produce a specific
12225 specialization, RETURN_TYPE is the return type desired.
12227 The EXPLICIT_TARGS are explicit template arguments provided via a
12228 template-id.
12230 The parameter STRICT is one of:
12232 DEDUCE_CALL:
12233 We are deducing arguments for a function call, as in
12234 [temp.deduct.call].
12236 DEDUCE_CONV:
12237 We are deducing arguments for a conversion function, as in
12238 [temp.deduct.conv].
12240 DEDUCE_EXACT:
12241 We are deducing arguments when doing an explicit instantiation
12242 as in [temp.explicit], when determining an explicit specialization
12243 as in [temp.expl.spec], or when taking the address of a function
12244 template, as in [temp.deduct.funcaddr]. */
12247 fn_type_unification (tree fn,
12248 tree explicit_targs,
12249 tree targs,
12250 const tree *args,
12251 unsigned int nargs,
12252 tree return_type,
12253 unification_kind_t strict,
12254 int flags)
12256 tree parms;
12257 tree fntype;
12258 int result;
12259 bool incomplete_argument_packs_p = false;
12261 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12263 fntype = TREE_TYPE (fn);
12264 if (explicit_targs)
12266 /* [temp.deduct]
12268 The specified template arguments must match the template
12269 parameters in kind (i.e., type, nontype, template), and there
12270 must not be more arguments than there are parameters;
12271 otherwise type deduction fails.
12273 Nontype arguments must match the types of the corresponding
12274 nontype template parameters, or must be convertible to the
12275 types of the corresponding nontype parameters as specified in
12276 _temp.arg.nontype_, otherwise type deduction fails.
12278 All references in the function type of the function template
12279 to the corresponding template parameters are replaced by the
12280 specified template argument values. If a substitution in a
12281 template parameter or in the function type of the function
12282 template results in an invalid type, type deduction fails. */
12283 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12284 int i, len = TREE_VEC_LENGTH (tparms);
12285 tree converted_args;
12286 bool incomplete = false;
12288 if (explicit_targs == error_mark_node)
12289 return 1;
12291 converted_args
12292 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12293 /*require_all_args=*/false,
12294 /*use_default_args=*/false));
12295 if (converted_args == error_mark_node)
12296 return 1;
12298 /* Substitute the explicit args into the function type. This is
12299 necessary so that, for instance, explicitly declared function
12300 arguments can match null pointed constants. If we were given
12301 an incomplete set of explicit args, we must not do semantic
12302 processing during substitution as we could create partial
12303 instantiations. */
12304 for (i = 0; i < len; i++)
12306 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12307 bool parameter_pack = false;
12309 /* Dig out the actual parm. */
12310 if (TREE_CODE (parm) == TYPE_DECL
12311 || TREE_CODE (parm) == TEMPLATE_DECL)
12313 parm = TREE_TYPE (parm);
12314 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12316 else if (TREE_CODE (parm) == PARM_DECL)
12318 parm = DECL_INITIAL (parm);
12319 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12322 if (parameter_pack)
12324 int level, idx;
12325 tree targ;
12326 template_parm_level_and_index (parm, &level, &idx);
12328 /* Mark the argument pack as "incomplete". We could
12329 still deduce more arguments during unification. */
12330 targ = TMPL_ARG (converted_args, level, idx);
12331 if (targ)
12333 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12334 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12335 = ARGUMENT_PACK_ARGS (targ);
12338 /* We have some incomplete argument packs. */
12339 incomplete_argument_packs_p = true;
12343 if (incomplete_argument_packs_p)
12344 /* Any substitution is guaranteed to be incomplete if there
12345 are incomplete argument packs, because we can still deduce
12346 more arguments. */
12347 incomplete = 1;
12348 else
12349 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12351 processing_template_decl += incomplete;
12352 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12353 processing_template_decl -= incomplete;
12355 if (fntype == error_mark_node)
12356 return 1;
12358 /* Place the explicitly specified arguments in TARGS. */
12359 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12360 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12363 /* Never do unification on the 'this' parameter. */
12364 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12366 if (return_type)
12368 tree *new_args;
12370 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12371 new_args = XALLOCAVEC (tree, nargs + 1);
12372 new_args[0] = return_type;
12373 memcpy (new_args + 1, args, nargs * sizeof (tree));
12374 args = new_args;
12375 ++nargs;
12378 /* We allow incomplete unification without an error message here
12379 because the standard doesn't seem to explicitly prohibit it. Our
12380 callers must be ready to deal with unification failures in any
12381 event. */
12382 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12383 targs, parms, args, nargs, /*subr=*/0,
12384 strict, flags);
12386 if (result == 0 && incomplete_argument_packs_p)
12388 int i, len = NUM_TMPL_ARGS (targs);
12390 /* Clear the "incomplete" flags on all argument packs. */
12391 for (i = 0; i < len; i++)
12393 tree arg = TREE_VEC_ELT (targs, i);
12394 if (ARGUMENT_PACK_P (arg))
12396 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12397 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12402 /* Now that we have bindings for all of the template arguments,
12403 ensure that the arguments deduced for the template template
12404 parameters have compatible template parameter lists. We cannot
12405 check this property before we have deduced all template
12406 arguments, because the template parameter types of a template
12407 template parameter might depend on prior template parameters
12408 deduced after the template template parameter. The following
12409 ill-formed example illustrates this issue:
12411 template<typename T, template<T> class C> void f(C<5>, T);
12413 template<int N> struct X {};
12415 void g() {
12416 f(X<5>(), 5l); // error: template argument deduction fails
12419 The template parameter list of 'C' depends on the template type
12420 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12421 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12422 time that we deduce 'C'. */
12423 if (result == 0
12424 && !template_template_parm_bindings_ok_p
12425 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12426 return 1;
12428 if (result == 0)
12429 /* All is well so far. Now, check:
12431 [temp.deduct]
12433 When all template arguments have been deduced, all uses of
12434 template parameters in nondeduced contexts are replaced with
12435 the corresponding deduced argument values. If the
12436 substitution results in an invalid type, as described above,
12437 type deduction fails. */
12439 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12440 if (substed == error_mark_node)
12441 return 1;
12443 /* If we're looking for an exact match, check that what we got
12444 is indeed an exact match. It might not be if some template
12445 parameters are used in non-deduced contexts. */
12446 if (strict == DEDUCE_EXACT)
12448 unsigned int i;
12450 tree sarg
12451 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
12452 if (return_type)
12453 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
12454 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
12455 if (!same_type_p (args[i], TREE_VALUE (sarg)))
12456 return 1;
12460 return result;
12463 /* Adjust types before performing type deduction, as described in
12464 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12465 sections are symmetric. PARM is the type of a function parameter
12466 or the return type of the conversion function. ARG is the type of
12467 the argument passed to the call, or the type of the value
12468 initialized with the result of the conversion function.
12469 ARG_EXPR is the original argument expression, which may be null. */
12471 static int
12472 maybe_adjust_types_for_deduction (unification_kind_t strict,
12473 tree* parm,
12474 tree* arg,
12475 tree arg_expr)
12477 int result = 0;
12479 switch (strict)
12481 case DEDUCE_CALL:
12482 break;
12484 case DEDUCE_CONV:
12486 /* Swap PARM and ARG throughout the remainder of this
12487 function; the handling is precisely symmetric since PARM
12488 will initialize ARG rather than vice versa. */
12489 tree* temp = parm;
12490 parm = arg;
12491 arg = temp;
12492 break;
12495 case DEDUCE_EXACT:
12496 /* There is nothing to do in this case. */
12497 return 0;
12499 default:
12500 gcc_unreachable ();
12503 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12505 /* [temp.deduct.call]
12507 If P is not a reference type:
12509 --If A is an array type, the pointer type produced by the
12510 array-to-pointer standard conversion (_conv.array_) is
12511 used in place of A for type deduction; otherwise,
12513 --If A is a function type, the pointer type produced by
12514 the function-to-pointer standard conversion
12515 (_conv.func_) is used in place of A for type deduction;
12516 otherwise,
12518 --If A is a cv-qualified type, the top level
12519 cv-qualifiers of A's type are ignored for type
12520 deduction. */
12521 if (TREE_CODE (*arg) == ARRAY_TYPE)
12522 *arg = build_pointer_type (TREE_TYPE (*arg));
12523 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
12524 *arg = build_pointer_type (*arg);
12525 else
12526 *arg = TYPE_MAIN_VARIANT (*arg);
12529 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12530 of the form T&&, where T is a template parameter, and the argument
12531 is an lvalue, T is deduced as A& */
12532 if (TREE_CODE (*parm) == REFERENCE_TYPE
12533 && TYPE_REF_IS_RVALUE (*parm)
12534 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12535 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12536 && arg_expr && real_lvalue_p (arg_expr))
12537 *arg = build_reference_type (*arg);
12539 /* [temp.deduct.call]
12541 If P is a cv-qualified type, the top level cv-qualifiers
12542 of P's type are ignored for type deduction. If P is a
12543 reference type, the type referred to by P is used for
12544 type deduction. */
12545 *parm = TYPE_MAIN_VARIANT (*parm);
12546 if (TREE_CODE (*parm) == REFERENCE_TYPE)
12548 *parm = TREE_TYPE (*parm);
12549 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12552 /* DR 322. For conversion deduction, remove a reference type on parm
12553 too (which has been swapped into ARG). */
12554 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12555 *arg = TREE_TYPE (*arg);
12557 return result;
12560 /* Most parms like fn_type_unification.
12562 If SUBR is 1, we're being called recursively (to unify the
12563 arguments of a function or method parameter of a function
12564 template). */
12566 static int
12567 type_unification_real (tree tparms,
12568 tree targs,
12569 tree xparms,
12570 const tree *xargs,
12571 unsigned int xnargs,
12572 int subr,
12573 unification_kind_t strict,
12574 int flags)
12576 tree parm, arg, arg_expr;
12577 int i;
12578 int ntparms = TREE_VEC_LENGTH (tparms);
12579 int sub_strict;
12580 int saw_undeduced = 0;
12581 tree parms;
12582 const tree *args;
12583 unsigned int nargs;
12584 unsigned int ia;
12586 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12587 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
12588 gcc_assert (ntparms > 0);
12590 switch (strict)
12592 case DEDUCE_CALL:
12593 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
12594 | UNIFY_ALLOW_DERIVED);
12595 break;
12597 case DEDUCE_CONV:
12598 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12599 break;
12601 case DEDUCE_EXACT:
12602 sub_strict = UNIFY_ALLOW_NONE;
12603 break;
12605 default:
12606 gcc_unreachable ();
12609 again:
12610 parms = xparms;
12611 args = xargs;
12612 nargs = xnargs;
12614 ia = 0;
12615 while (parms && parms != void_list_node
12616 && ia < nargs)
12618 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12619 break;
12621 parm = TREE_VALUE (parms);
12622 parms = TREE_CHAIN (parms);
12623 arg = args[ia];
12624 ++ia;
12625 arg_expr = NULL;
12627 if (arg == error_mark_node)
12628 return 1;
12629 if (arg == unknown_type_node)
12630 /* We can't deduce anything from this, but we might get all the
12631 template args from other function args. */
12632 continue;
12634 /* Conversions will be performed on a function argument that
12635 corresponds with a function parameter that contains only
12636 non-deducible template parameters and explicitly specified
12637 template parameters. */
12638 if (!uses_template_parms (parm))
12640 tree type;
12642 if (!TYPE_P (arg))
12643 type = TREE_TYPE (arg);
12644 else
12645 type = arg;
12647 if (same_type_p (parm, type))
12648 continue;
12649 if (strict != DEDUCE_EXACT
12650 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
12651 flags))
12652 continue;
12654 return 1;
12657 if (!TYPE_P (arg))
12659 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12660 if (type_unknown_p (arg))
12662 /* [temp.deduct.type]
12664 A template-argument can be deduced from a pointer to
12665 function or pointer to member function argument if
12666 the set of overloaded functions does not contain
12667 function templates and at most one of a set of
12668 overloaded functions provides a unique match. */
12669 if (resolve_overloaded_unification
12670 (tparms, targs, parm, arg, strict, sub_strict))
12671 continue;
12673 return 1;
12675 arg_expr = arg;
12676 arg = unlowered_expr_type (arg);
12677 if (arg == error_mark_node)
12678 return 1;
12682 int arg_strict = sub_strict;
12684 if (!subr)
12685 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
12686 arg_expr);
12688 if (arg == init_list_type_node && arg_expr)
12689 arg = arg_expr;
12690 if (unify (tparms, targs, parm, arg, arg_strict))
12691 return 1;
12696 if (parms
12697 && parms != void_list_node
12698 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12700 /* Unify the remaining arguments with the pack expansion type. */
12701 tree argvec;
12702 tree parmvec = make_tree_vec (1);
12704 /* Allocate a TREE_VEC and copy in all of the arguments */
12705 argvec = make_tree_vec (nargs - ia);
12706 for (i = 0; ia < nargs; ++ia, ++i)
12707 TREE_VEC_ELT (argvec, i) = args[ia];
12709 /* Copy the parameter into parmvec. */
12710 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
12711 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
12712 /*call_args_p=*/true, /*subr=*/subr))
12713 return 1;
12715 /* Advance to the end of the list of parameters. */
12716 parms = TREE_CHAIN (parms);
12719 /* Fail if we've reached the end of the parm list, and more args
12720 are present, and the parm list isn't variadic. */
12721 if (ia < nargs && parms == void_list_node)
12722 return 1;
12723 /* Fail if parms are left and they don't have default values. */
12724 if (parms && parms != void_list_node
12725 && TREE_PURPOSE (parms) == NULL_TREE)
12726 return 1;
12728 if (!subr)
12729 for (i = 0; i < ntparms; i++)
12730 if (!TREE_VEC_ELT (targs, i))
12732 tree tparm;
12734 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
12735 continue;
12737 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12739 /* If this is an undeduced nontype parameter that depends on
12740 a type parameter, try another pass; its type may have been
12741 deduced from a later argument than the one from which
12742 this parameter can be deduced. */
12743 if (TREE_CODE (tparm) == PARM_DECL
12744 && uses_template_parms (TREE_TYPE (tparm))
12745 && !saw_undeduced++)
12746 goto again;
12748 /* Core issue #226 (C++0x) [temp.deduct]:
12750 If a template argument has not been deduced, its
12751 default template argument, if any, is used.
12753 When we are in C++98 mode, TREE_PURPOSE will either
12754 be NULL_TREE or ERROR_MARK_NODE, so we do not need
12755 to explicitly check cxx_dialect here. */
12756 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
12758 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
12759 targs, tf_none, NULL_TREE);
12760 if (arg == error_mark_node)
12761 return 1;
12762 else
12764 TREE_VEC_ELT (targs, i) = arg;
12765 continue;
12769 /* If the type parameter is a parameter pack, then it will
12770 be deduced to an empty parameter pack. */
12771 if (template_parameter_pack_p (tparm))
12773 tree arg;
12775 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
12777 arg = make_node (NONTYPE_ARGUMENT_PACK);
12778 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
12779 TREE_CONSTANT (arg) = 1;
12781 else
12782 arg = make_node (TYPE_ARGUMENT_PACK);
12784 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
12786 TREE_VEC_ELT (targs, i) = arg;
12787 continue;
12790 return 2;
12793 return 0;
12796 /* Subroutine of type_unification_real. Args are like the variables
12797 at the call site. ARG is an overloaded function (or template-id);
12798 we try deducing template args from each of the overloads, and if
12799 only one succeeds, we go with that. Modifies TARGS and returns
12800 true on success. */
12802 static bool
12803 resolve_overloaded_unification (tree tparms,
12804 tree targs,
12805 tree parm,
12806 tree arg,
12807 unification_kind_t strict,
12808 int sub_strict)
12810 tree tempargs = copy_node (targs);
12811 int good = 0;
12812 tree goodfn = NULL_TREE;
12813 bool addr_p;
12815 if (TREE_CODE (arg) == ADDR_EXPR)
12817 arg = TREE_OPERAND (arg, 0);
12818 addr_p = true;
12820 else
12821 addr_p = false;
12823 if (TREE_CODE (arg) == COMPONENT_REF)
12824 /* Handle `&x' where `x' is some static or non-static member
12825 function name. */
12826 arg = TREE_OPERAND (arg, 1);
12828 if (TREE_CODE (arg) == OFFSET_REF)
12829 arg = TREE_OPERAND (arg, 1);
12831 /* Strip baselink information. */
12832 if (BASELINK_P (arg))
12833 arg = BASELINK_FUNCTIONS (arg);
12835 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
12837 /* If we got some explicit template args, we need to plug them into
12838 the affected templates before we try to unify, in case the
12839 explicit args will completely resolve the templates in question. */
12841 tree expl_subargs = TREE_OPERAND (arg, 1);
12842 arg = TREE_OPERAND (arg, 0);
12844 for (; arg; arg = OVL_NEXT (arg))
12846 tree fn = OVL_CURRENT (arg);
12847 tree subargs, elem;
12849 if (TREE_CODE (fn) != TEMPLATE_DECL)
12850 continue;
12852 ++processing_template_decl;
12853 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
12854 expl_subargs, /*check_ret=*/false);
12855 if (subargs)
12857 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
12858 if (try_one_overload (tparms, targs, tempargs, parm,
12859 elem, strict, sub_strict, addr_p)
12860 && (!goodfn || !decls_match (goodfn, elem)))
12862 goodfn = elem;
12863 ++good;
12866 --processing_template_decl;
12869 else if (TREE_CODE (arg) != OVERLOAD
12870 && TREE_CODE (arg) != FUNCTION_DECL)
12871 /* If ARG is, for example, "(0, &f)" then its type will be unknown
12872 -- but the deduction does not succeed because the expression is
12873 not just the function on its own. */
12874 return false;
12875 else
12876 for (; arg; arg = OVL_NEXT (arg))
12877 if (try_one_overload (tparms, targs, tempargs, parm,
12878 TREE_TYPE (OVL_CURRENT (arg)),
12879 strict, sub_strict, addr_p)
12880 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
12882 goodfn = OVL_CURRENT (arg);
12883 ++good;
12886 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12887 to function or pointer to member function argument if the set of
12888 overloaded functions does not contain function templates and at most
12889 one of a set of overloaded functions provides a unique match.
12891 So if we found multiple possibilities, we return success but don't
12892 deduce anything. */
12894 if (good == 1)
12896 int i = TREE_VEC_LENGTH (targs);
12897 for (; i--; )
12898 if (TREE_VEC_ELT (tempargs, i))
12899 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
12901 if (good)
12902 return true;
12904 return false;
12907 /* Subroutine of resolve_overloaded_unification; does deduction for a single
12908 overload. Fills TARGS with any deduced arguments, or error_mark_node if
12909 different overloads deduce different arguments for a given parm.
12910 ADDR_P is true if the expression for which deduction is being
12911 performed was of the form "& fn" rather than simply "fn".
12913 Returns 1 on success. */
12915 static int
12916 try_one_overload (tree tparms,
12917 tree orig_targs,
12918 tree targs,
12919 tree parm,
12920 tree arg,
12921 unification_kind_t strict,
12922 int sub_strict,
12923 bool addr_p)
12925 int nargs;
12926 tree tempargs;
12927 int i;
12929 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12930 to function or pointer to member function argument if the set of
12931 overloaded functions does not contain function templates and at most
12932 one of a set of overloaded functions provides a unique match.
12934 So if this is a template, just return success. */
12936 if (uses_template_parms (arg))
12937 return 1;
12939 if (TREE_CODE (arg) == METHOD_TYPE)
12940 arg = build_ptrmemfunc_type (build_pointer_type (arg));
12941 else if (addr_p)
12942 arg = build_pointer_type (arg);
12944 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12946 /* We don't copy orig_targs for this because if we have already deduced
12947 some template args from previous args, unify would complain when we
12948 try to deduce a template parameter for the same argument, even though
12949 there isn't really a conflict. */
12950 nargs = TREE_VEC_LENGTH (targs);
12951 tempargs = make_tree_vec (nargs);
12953 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
12954 return 0;
12956 /* First make sure we didn't deduce anything that conflicts with
12957 explicitly specified args. */
12958 for (i = nargs; i--; )
12960 tree elt = TREE_VEC_ELT (tempargs, i);
12961 tree oldelt = TREE_VEC_ELT (orig_targs, i);
12963 if (!elt)
12964 /*NOP*/;
12965 else if (uses_template_parms (elt))
12966 /* Since we're unifying against ourselves, we will fill in
12967 template args used in the function parm list with our own
12968 template parms. Discard them. */
12969 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
12970 else if (oldelt && !template_args_equal (oldelt, elt))
12971 return 0;
12974 for (i = nargs; i--; )
12976 tree elt = TREE_VEC_ELT (tempargs, i);
12978 if (elt)
12979 TREE_VEC_ELT (targs, i) = elt;
12982 return 1;
12985 /* PARM is a template class (perhaps with unbound template
12986 parameters). ARG is a fully instantiated type. If ARG can be
12987 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
12988 TARGS are as for unify. */
12990 static tree
12991 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
12993 tree copy_of_targs;
12995 if (!CLASSTYPE_TEMPLATE_INFO (arg)
12996 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
12997 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
12998 return NULL_TREE;
13000 /* We need to make a new template argument vector for the call to
13001 unify. If we used TARGS, we'd clutter it up with the result of
13002 the attempted unification, even if this class didn't work out.
13003 We also don't want to commit ourselves to all the unifications
13004 we've already done, since unification is supposed to be done on
13005 an argument-by-argument basis. In other words, consider the
13006 following pathological case:
13008 template <int I, int J, int K>
13009 struct S {};
13011 template <int I, int J>
13012 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13014 template <int I, int J, int K>
13015 void f(S<I, J, K>, S<I, I, I>);
13017 void g() {
13018 S<0, 0, 0> s0;
13019 S<0, 1, 2> s2;
13021 f(s0, s2);
13024 Now, by the time we consider the unification involving `s2', we
13025 already know that we must have `f<0, 0, 0>'. But, even though
13026 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13027 because there are two ways to unify base classes of S<0, 1, 2>
13028 with S<I, I, I>. If we kept the already deduced knowledge, we
13029 would reject the possibility I=1. */
13030 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13032 /* If unification failed, we're done. */
13033 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13034 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13035 return NULL_TREE;
13037 return arg;
13040 /* Given a template type PARM and a class type ARG, find the unique
13041 base type in ARG that is an instance of PARM. We do not examine
13042 ARG itself; only its base-classes. If there is not exactly one
13043 appropriate base class, return NULL_TREE. PARM may be the type of
13044 a partial specialization, as well as a plain template type. Used
13045 by unify. */
13047 static tree
13048 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13050 tree rval = NULL_TREE;
13051 tree binfo;
13053 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13055 binfo = TYPE_BINFO (complete_type (arg));
13056 if (!binfo)
13057 /* The type could not be completed. */
13058 return NULL_TREE;
13060 /* Walk in inheritance graph order. The search order is not
13061 important, and this avoids multiple walks of virtual bases. */
13062 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13064 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13066 if (r)
13068 /* If there is more than one satisfactory baseclass, then:
13070 [temp.deduct.call]
13072 If they yield more than one possible deduced A, the type
13073 deduction fails.
13075 applies. */
13076 if (rval && !same_type_p (r, rval))
13077 return NULL_TREE;
13079 rval = r;
13083 return rval;
13086 /* Returns the level of DECL, which declares a template parameter. */
13088 static int
13089 template_decl_level (tree decl)
13091 switch (TREE_CODE (decl))
13093 case TYPE_DECL:
13094 case TEMPLATE_DECL:
13095 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13097 case PARM_DECL:
13098 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13100 default:
13101 gcc_unreachable ();
13103 return 0;
13106 /* Decide whether ARG can be unified with PARM, considering only the
13107 cv-qualifiers of each type, given STRICT as documented for unify.
13108 Returns nonzero iff the unification is OK on that basis. */
13110 static int
13111 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13113 int arg_quals = cp_type_quals (arg);
13114 int parm_quals = cp_type_quals (parm);
13116 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13117 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13119 /* Although a CVR qualifier is ignored when being applied to a
13120 substituted template parameter ([8.3.2]/1 for example), that
13121 does not apply during deduction [14.8.2.4]/1, (even though
13122 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13123 this). Except when we're allowing additional CV qualifiers
13124 at the outer level [14.8.2.1]/3,1st bullet. */
13125 if ((TREE_CODE (arg) == REFERENCE_TYPE
13126 || TREE_CODE (arg) == FUNCTION_TYPE
13127 || TREE_CODE (arg) == METHOD_TYPE)
13128 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13129 return 0;
13131 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13132 && (parm_quals & TYPE_QUAL_RESTRICT))
13133 return 0;
13136 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13137 && (arg_quals & parm_quals) != parm_quals)
13138 return 0;
13140 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13141 && (parm_quals & arg_quals) != arg_quals)
13142 return 0;
13144 return 1;
13147 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13148 void
13149 template_parm_level_and_index (tree parm, int* level, int* index)
13151 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13152 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13153 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13155 *index = TEMPLATE_TYPE_IDX (parm);
13156 *level = TEMPLATE_TYPE_LEVEL (parm);
13158 else
13160 *index = TEMPLATE_PARM_IDX (parm);
13161 *level = TEMPLATE_PARM_LEVEL (parm);
13165 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13166 expansion at the end of PACKED_PARMS. Returns 0 if the type
13167 deduction succeeds, 1 otherwise. STRICT is the same as in
13168 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13169 call argument list. We'll need to adjust the arguments to make them
13170 types. SUBR tells us if this is from a recursive call to
13171 type_unification_real. */
13173 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13174 tree packed_args, int strict, bool call_args_p,
13175 bool subr)
13177 tree parm
13178 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13179 tree pattern = PACK_EXPANSION_PATTERN (parm);
13180 tree pack, packs = NULL_TREE;
13181 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13182 int len = TREE_VEC_LENGTH (packed_args);
13184 /* Determine the parameter packs we will be deducing from the
13185 pattern, and record their current deductions. */
13186 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13187 pack; pack = TREE_CHAIN (pack))
13189 tree parm_pack = TREE_VALUE (pack);
13190 int idx, level;
13192 /* Determine the index and level of this parameter pack. */
13193 template_parm_level_and_index (parm_pack, &level, &idx);
13195 /* Keep track of the parameter packs and their corresponding
13196 argument packs. */
13197 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13198 TREE_TYPE (packs) = make_tree_vec (len - start);
13201 /* Loop through all of the arguments that have not yet been
13202 unified and unify each with the pattern. */
13203 for (i = start; i < len; i++)
13205 tree parm = pattern;
13207 /* For each parameter pack, clear out the deduced value so that
13208 we can deduce it again. */
13209 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13211 int idx, level;
13212 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13214 TMPL_ARG (targs, level, idx) = NULL_TREE;
13217 /* Unify the pattern with the current argument. */
13219 tree arg = TREE_VEC_ELT (packed_args, i);
13220 tree arg_expr = NULL_TREE;
13221 int arg_strict = strict;
13222 bool skip_arg_p = false;
13224 if (call_args_p)
13226 int sub_strict;
13228 /* This mirrors what we do in type_unification_real. */
13229 switch (strict)
13231 case DEDUCE_CALL:
13232 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13233 | UNIFY_ALLOW_MORE_CV_QUAL
13234 | UNIFY_ALLOW_DERIVED);
13235 break;
13237 case DEDUCE_CONV:
13238 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13239 break;
13241 case DEDUCE_EXACT:
13242 sub_strict = UNIFY_ALLOW_NONE;
13243 break;
13245 default:
13246 gcc_unreachable ();
13249 if (!TYPE_P (arg))
13251 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13252 if (type_unknown_p (arg))
13254 /* [temp.deduct.type] A template-argument can be
13255 deduced from a pointer to function or pointer
13256 to member function argument if the set of
13257 overloaded functions does not contain function
13258 templates and at most one of a set of
13259 overloaded functions provides a unique
13260 match. */
13262 if (resolve_overloaded_unification
13263 (tparms, targs, parm, arg,
13264 (unification_kind_t) strict,
13265 sub_strict)
13266 != 0)
13267 return 1;
13268 skip_arg_p = true;
13271 if (!skip_arg_p)
13273 arg_expr = arg;
13274 arg = unlowered_expr_type (arg);
13275 if (arg == error_mark_node)
13276 return 1;
13280 arg_strict = sub_strict;
13282 if (!subr)
13283 arg_strict |=
13284 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13285 &parm, &arg, arg_expr);
13288 if (!skip_arg_p)
13290 if (unify (tparms, targs, parm, arg, arg_strict))
13291 return 1;
13295 /* For each parameter pack, collect the deduced value. */
13296 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13298 int idx, level;
13299 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13301 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13302 TMPL_ARG (targs, level, idx);
13306 /* Verify that the results of unification with the parameter packs
13307 produce results consistent with what we've seen before, and make
13308 the deduced argument packs available. */
13309 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13311 tree old_pack = TREE_VALUE (pack);
13312 tree new_args = TREE_TYPE (pack);
13313 int i, len = TREE_VEC_LENGTH (new_args);
13314 bool nondeduced_p = false;
13316 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13317 actually deduce anything. */
13318 for (i = 0; i < len && !nondeduced_p; ++i)
13319 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13320 nondeduced_p = true;
13321 if (nondeduced_p)
13322 continue;
13324 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13326 /* Prepend the explicit arguments onto NEW_ARGS. */
13327 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13328 tree old_args = new_args;
13329 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13330 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13332 /* Copy the explicit arguments. */
13333 new_args = make_tree_vec (len);
13334 for (i = 0; i < explicit_len; i++)
13335 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13337 /* Copy the deduced arguments. */
13338 for (; i < len; i++)
13339 TREE_VEC_ELT (new_args, i) =
13340 TREE_VEC_ELT (old_args, i - explicit_len);
13343 if (!old_pack)
13345 tree result;
13346 int idx, level;
13348 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13350 /* Build the deduced *_ARGUMENT_PACK. */
13351 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13353 result = make_node (NONTYPE_ARGUMENT_PACK);
13354 TREE_TYPE (result) =
13355 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13356 TREE_CONSTANT (result) = 1;
13358 else
13359 result = make_node (TYPE_ARGUMENT_PACK);
13361 SET_ARGUMENT_PACK_ARGS (result, new_args);
13363 /* Note the deduced argument packs for this parameter
13364 pack. */
13365 TMPL_ARG (targs, level, idx) = result;
13367 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13368 && (ARGUMENT_PACK_ARGS (old_pack)
13369 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13371 /* We only had the explicitly-provided arguments before, but
13372 now we have a complete set of arguments. */
13373 int idx, level;
13374 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13375 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13377 /* Keep the original deduced argument pack. */
13378 TMPL_ARG (targs, level, idx) = old_pack;
13380 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13381 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13382 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13384 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13385 new_args))
13386 /* Inconsistent unification of this parameter pack. */
13387 return 1;
13388 else
13390 int idx, level;
13392 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13394 /* Keep the original deduced argument pack. */
13395 TMPL_ARG (targs, level, idx) = old_pack;
13399 return 0;
13402 /* Deduce the value of template parameters. TPARMS is the (innermost)
13403 set of template parameters to a template. TARGS is the bindings
13404 for those template parameters, as determined thus far; TARGS may
13405 include template arguments for outer levels of template parameters
13406 as well. PARM is a parameter to a template function, or a
13407 subcomponent of that parameter; ARG is the corresponding argument.
13408 This function attempts to match PARM with ARG in a manner
13409 consistent with the existing assignments in TARGS. If more values
13410 are deduced, then TARGS is updated.
13412 Returns 0 if the type deduction succeeds, 1 otherwise. The
13413 parameter STRICT is a bitwise or of the following flags:
13415 UNIFY_ALLOW_NONE:
13416 Require an exact match between PARM and ARG.
13417 UNIFY_ALLOW_MORE_CV_QUAL:
13418 Allow the deduced ARG to be more cv-qualified (by qualification
13419 conversion) than ARG.
13420 UNIFY_ALLOW_LESS_CV_QUAL:
13421 Allow the deduced ARG to be less cv-qualified than ARG.
13422 UNIFY_ALLOW_DERIVED:
13423 Allow the deduced ARG to be a template base class of ARG,
13424 or a pointer to a template base class of the type pointed to by
13425 ARG.
13426 UNIFY_ALLOW_INTEGER:
13427 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13428 case for more information.
13429 UNIFY_ALLOW_OUTER_LEVEL:
13430 This is the outermost level of a deduction. Used to determine validity
13431 of qualification conversions. A valid qualification conversion must
13432 have const qualified pointers leading up to the inner type which
13433 requires additional CV quals, except at the outer level, where const
13434 is not required [conv.qual]. It would be normal to set this flag in
13435 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13436 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13437 This is the outermost level of a deduction, and PARM can be more CV
13438 qualified at this point.
13439 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13440 This is the outermost level of a deduction, and PARM can be less CV
13441 qualified at this point. */
13443 static int
13444 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
13446 int idx;
13447 tree targ;
13448 tree tparm;
13449 int strict_in = strict;
13451 /* I don't think this will do the right thing with respect to types.
13452 But the only case I've seen it in so far has been array bounds, where
13453 signedness is the only information lost, and I think that will be
13454 okay. */
13455 while (TREE_CODE (parm) == NOP_EXPR)
13456 parm = TREE_OPERAND (parm, 0);
13458 if (arg == error_mark_node)
13459 return 1;
13460 if (arg == unknown_type_node
13461 || arg == init_list_type_node)
13462 /* We can't deduce anything from this, but we might get all the
13463 template args from other function args. */
13464 return 0;
13466 /* If PARM uses template parameters, then we can't bail out here,
13467 even if ARG == PARM, since we won't record unifications for the
13468 template parameters. We might need them if we're trying to
13469 figure out which of two things is more specialized. */
13470 if (arg == parm && !uses_template_parms (parm))
13471 return 0;
13473 /* Handle init lists early, so the rest of the function can assume
13474 we're dealing with a type. */
13475 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13477 tree elt, elttype;
13478 unsigned i;
13480 if (!is_std_init_list (parm))
13481 /* We can only deduce from an initializer list argument if the
13482 parameter is std::initializer_list; otherwise this is a
13483 non-deduced context. */
13484 return 0;
13486 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13488 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13490 int elt_strict = strict;
13491 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
13493 tree type = TREE_TYPE (elt);
13494 /* It should only be possible to get here for a call. */
13495 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13496 elt_strict |= maybe_adjust_types_for_deduction
13497 (DEDUCE_CALL, &elttype, &type, elt);
13498 elt = type;
13501 if (unify (tparms, targs, elttype, elt, elt_strict))
13502 return 1;
13504 return 0;
13507 /* Immediately reject some pairs that won't unify because of
13508 cv-qualification mismatches. */
13509 if (TREE_CODE (arg) == TREE_CODE (parm)
13510 && TYPE_P (arg)
13511 /* It is the elements of the array which hold the cv quals of an array
13512 type, and the elements might be template type parms. We'll check
13513 when we recurse. */
13514 && TREE_CODE (arg) != ARRAY_TYPE
13515 /* We check the cv-qualifiers when unifying with template type
13516 parameters below. We want to allow ARG `const T' to unify with
13517 PARM `T' for example, when computing which of two templates
13518 is more specialized, for example. */
13519 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
13520 && !check_cv_quals_for_unify (strict_in, arg, parm))
13521 return 1;
13523 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
13524 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
13525 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13526 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13527 strict &= ~UNIFY_ALLOW_DERIVED;
13528 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13529 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
13531 switch (TREE_CODE (parm))
13533 case TYPENAME_TYPE:
13534 case SCOPE_REF:
13535 case UNBOUND_CLASS_TEMPLATE:
13536 /* In a type which contains a nested-name-specifier, template
13537 argument values cannot be deduced for template parameters used
13538 within the nested-name-specifier. */
13539 return 0;
13541 case TEMPLATE_TYPE_PARM:
13542 case TEMPLATE_TEMPLATE_PARM:
13543 case BOUND_TEMPLATE_TEMPLATE_PARM:
13544 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13545 if (tparm == error_mark_node)
13546 return 1;
13548 if (TEMPLATE_TYPE_LEVEL (parm)
13549 != template_decl_level (tparm))
13550 /* The PARM is not one we're trying to unify. Just check
13551 to see if it matches ARG. */
13552 return (TREE_CODE (arg) == TREE_CODE (parm)
13553 && same_type_p (parm, arg)) ? 0 : 1;
13554 idx = TEMPLATE_TYPE_IDX (parm);
13555 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13556 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
13558 /* Check for mixed types and values. */
13559 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13560 && TREE_CODE (tparm) != TYPE_DECL)
13561 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13562 && TREE_CODE (tparm) != TEMPLATE_DECL))
13563 return 1;
13565 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13567 /* ARG must be constructed from a template class or a template
13568 template parameter. */
13569 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
13570 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
13571 return 1;
13574 tree parmvec = TYPE_TI_ARGS (parm);
13575 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
13576 tree parm_parms
13577 = DECL_INNERMOST_TEMPLATE_PARMS
13578 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
13579 int i, len;
13580 int parm_variadic_p = 0;
13582 /* The resolution to DR150 makes clear that default
13583 arguments for an N-argument may not be used to bind T
13584 to a template template parameter with fewer than N
13585 parameters. It is not safe to permit the binding of
13586 default arguments as an extension, as that may change
13587 the meaning of a conforming program. Consider:
13589 struct Dense { static const unsigned int dim = 1; };
13591 template <template <typename> class View,
13592 typename Block>
13593 void operator+(float, View<Block> const&);
13595 template <typename Block,
13596 unsigned int Dim = Block::dim>
13597 struct Lvalue_proxy { operator float() const; };
13599 void
13600 test_1d (void) {
13601 Lvalue_proxy<Dense> p;
13602 float b;
13603 b + p;
13606 Here, if Lvalue_proxy is permitted to bind to View, then
13607 the global operator+ will be used; if they are not, the
13608 Lvalue_proxy will be converted to float. */
13609 if (coerce_template_parms (parm_parms,
13610 argvec,
13611 TYPE_TI_TEMPLATE (parm),
13612 tf_none,
13613 /*require_all_args=*/true,
13614 /*use_default_args=*/false)
13615 == error_mark_node)
13616 return 1;
13618 /* Deduce arguments T, i from TT<T> or TT<i>.
13619 We check each element of PARMVEC and ARGVEC individually
13620 rather than the whole TREE_VEC since they can have
13621 different number of elements. */
13623 parmvec = expand_template_argument_pack (parmvec);
13624 argvec = expand_template_argument_pack (argvec);
13626 len = TREE_VEC_LENGTH (parmvec);
13628 /* Check if the parameters end in a pack, making them
13629 variadic. */
13630 if (len > 0
13631 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
13632 parm_variadic_p = 1;
13634 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
13635 return 1;
13637 for (i = 0; i < len - parm_variadic_p; ++i)
13639 if (unify (tparms, targs,
13640 TREE_VEC_ELT (parmvec, i),
13641 TREE_VEC_ELT (argvec, i),
13642 UNIFY_ALLOW_NONE))
13643 return 1;
13646 if (parm_variadic_p
13647 && unify_pack_expansion (tparms, targs,
13648 parmvec, argvec,
13649 UNIFY_ALLOW_NONE,
13650 /*call_args_p=*/false,
13651 /*subr=*/false))
13652 return 1;
13654 arg = TYPE_TI_TEMPLATE (arg);
13656 /* Fall through to deduce template name. */
13659 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13660 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13662 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
13664 /* Simple cases: Value already set, does match or doesn't. */
13665 if (targ != NULL_TREE && template_args_equal (targ, arg))
13666 return 0;
13667 else if (targ)
13668 return 1;
13670 else
13672 /* If PARM is `const T' and ARG is only `int', we don't have
13673 a match unless we are allowing additional qualification.
13674 If ARG is `const int' and PARM is just `T' that's OK;
13675 that binds `const int' to `T'. */
13676 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
13677 arg, parm))
13678 return 1;
13680 /* Consider the case where ARG is `const volatile int' and
13681 PARM is `const T'. Then, T should be `volatile int'. */
13682 arg = cp_build_qualified_type_real
13683 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
13684 if (arg == error_mark_node)
13685 return 1;
13687 /* Simple cases: Value already set, does match or doesn't. */
13688 if (targ != NULL_TREE && same_type_p (targ, arg))
13689 return 0;
13690 else if (targ)
13691 return 1;
13693 /* Make sure that ARG is not a variable-sized array. (Note
13694 that were talking about variable-sized arrays (like
13695 `int[n]'), rather than arrays of unknown size (like
13696 `int[]').) We'll get very confused by such a type since
13697 the bound of the array will not be computable in an
13698 instantiation. Besides, such types are not allowed in
13699 ISO C++, so we can do as we please here. */
13700 if (variably_modified_type_p (arg, NULL_TREE))
13701 return 1;
13703 /* Strip typedefs as in convert_template_argument. */
13704 arg = strip_typedefs (arg);
13707 /* If ARG is a parameter pack or an expansion, we cannot unify
13708 against it unless PARM is also a parameter pack. */
13709 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13710 && !template_parameter_pack_p (parm))
13711 return 1;
13713 /* If the argument deduction results is a METHOD_TYPE,
13714 then there is a problem.
13715 METHOD_TYPE doesn't map to any real C++ type the result of
13716 the deduction can not be of that type. */
13717 if (TREE_CODE (arg) == METHOD_TYPE)
13718 return 1;
13720 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13721 return 0;
13723 case TEMPLATE_PARM_INDEX:
13724 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13725 if (tparm == error_mark_node)
13726 return 1;
13728 if (TEMPLATE_PARM_LEVEL (parm)
13729 != template_decl_level (tparm))
13730 /* The PARM is not one we're trying to unify. Just check
13731 to see if it matches ARG. */
13732 return !(TREE_CODE (arg) == TREE_CODE (parm)
13733 && cp_tree_equal (parm, arg));
13735 idx = TEMPLATE_PARM_IDX (parm);
13736 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13738 if (targ)
13739 return !cp_tree_equal (targ, arg);
13741 /* [temp.deduct.type] If, in the declaration of a function template
13742 with a non-type template-parameter, the non-type
13743 template-parameter is used in an expression in the function
13744 parameter-list and, if the corresponding template-argument is
13745 deduced, the template-argument type shall match the type of the
13746 template-parameter exactly, except that a template-argument
13747 deduced from an array bound may be of any integral type.
13748 The non-type parameter might use already deduced type parameters. */
13749 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
13750 if (!TREE_TYPE (arg))
13751 /* Template-parameter dependent expression. Just accept it for now.
13752 It will later be processed in convert_template_argument. */
13754 else if (same_type_p (TREE_TYPE (arg), tparm))
13755 /* OK */;
13756 else if ((strict & UNIFY_ALLOW_INTEGER)
13757 && (TREE_CODE (tparm) == INTEGER_TYPE
13758 || TREE_CODE (tparm) == BOOLEAN_TYPE))
13759 /* Convert the ARG to the type of PARM; the deduced non-type
13760 template argument must exactly match the types of the
13761 corresponding parameter. */
13762 arg = fold (build_nop (tparm, arg));
13763 else if (uses_template_parms (tparm))
13764 /* We haven't deduced the type of this parameter yet. Try again
13765 later. */
13766 return 0;
13767 else
13768 return 1;
13770 /* If ARG is a parameter pack or an expansion, we cannot unify
13771 against it unless PARM is also a parameter pack. */
13772 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13773 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
13774 return 1;
13776 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13777 return 0;
13779 case PTRMEM_CST:
13781 /* A pointer-to-member constant can be unified only with
13782 another constant. */
13783 if (TREE_CODE (arg) != PTRMEM_CST)
13784 return 1;
13786 /* Just unify the class member. It would be useless (and possibly
13787 wrong, depending on the strict flags) to unify also
13788 PTRMEM_CST_CLASS, because we want to be sure that both parm and
13789 arg refer to the same variable, even if through different
13790 classes. For instance:
13792 struct A { int x; };
13793 struct B : A { };
13795 Unification of &A::x and &B::x must succeed. */
13796 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
13797 PTRMEM_CST_MEMBER (arg), strict);
13800 case POINTER_TYPE:
13802 if (TREE_CODE (arg) != POINTER_TYPE)
13803 return 1;
13805 /* [temp.deduct.call]
13807 A can be another pointer or pointer to member type that can
13808 be converted to the deduced A via a qualification
13809 conversion (_conv.qual_).
13811 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
13812 This will allow for additional cv-qualification of the
13813 pointed-to types if appropriate. */
13815 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
13816 /* The derived-to-base conversion only persists through one
13817 level of pointers. */
13818 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
13820 return unify (tparms, targs, TREE_TYPE (parm),
13821 TREE_TYPE (arg), strict);
13824 case REFERENCE_TYPE:
13825 if (TREE_CODE (arg) != REFERENCE_TYPE)
13826 return 1;
13827 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13828 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13830 case ARRAY_TYPE:
13831 if (TREE_CODE (arg) != ARRAY_TYPE)
13832 return 1;
13833 if ((TYPE_DOMAIN (parm) == NULL_TREE)
13834 != (TYPE_DOMAIN (arg) == NULL_TREE))
13835 return 1;
13836 if (TYPE_DOMAIN (parm) != NULL_TREE)
13838 tree parm_max;
13839 tree arg_max;
13840 bool parm_cst;
13841 bool arg_cst;
13843 /* Our representation of array types uses "N - 1" as the
13844 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
13845 not an integer constant. We cannot unify arbitrarily
13846 complex expressions, so we eliminate the MINUS_EXPRs
13847 here. */
13848 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
13849 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
13850 if (!parm_cst)
13852 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
13853 parm_max = TREE_OPERAND (parm_max, 0);
13855 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
13856 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
13857 if (!arg_cst)
13859 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
13860 trying to unify the type of a variable with the type
13861 of a template parameter. For example:
13863 template <unsigned int N>
13864 void f (char (&) [N]);
13865 int g();
13866 void h(int i) {
13867 char a[g(i)];
13868 f(a);
13871 Here, the type of the ARG will be "int [g(i)]", and
13872 may be a SAVE_EXPR, etc. */
13873 if (TREE_CODE (arg_max) != MINUS_EXPR)
13874 return 1;
13875 arg_max = TREE_OPERAND (arg_max, 0);
13878 /* If only one of the bounds used a MINUS_EXPR, compensate
13879 by adding one to the other bound. */
13880 if (parm_cst && !arg_cst)
13881 parm_max = fold_build2 (PLUS_EXPR,
13882 integer_type_node,
13883 parm_max,
13884 integer_one_node);
13885 else if (arg_cst && !parm_cst)
13886 arg_max = fold_build2 (PLUS_EXPR,
13887 integer_type_node,
13888 arg_max,
13889 integer_one_node);
13891 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
13892 return 1;
13894 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13895 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13897 case REAL_TYPE:
13898 case COMPLEX_TYPE:
13899 case VECTOR_TYPE:
13900 case INTEGER_TYPE:
13901 case BOOLEAN_TYPE:
13902 case ENUMERAL_TYPE:
13903 case VOID_TYPE:
13904 if (TREE_CODE (arg) != TREE_CODE (parm))
13905 return 1;
13907 /* We have already checked cv-qualification at the top of the
13908 function. */
13909 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
13910 return 1;
13912 /* As far as unification is concerned, this wins. Later checks
13913 will invalidate it if necessary. */
13914 return 0;
13916 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
13917 /* Type INTEGER_CST can come from ordinary constant template args. */
13918 case INTEGER_CST:
13919 while (TREE_CODE (arg) == NOP_EXPR)
13920 arg = TREE_OPERAND (arg, 0);
13922 if (TREE_CODE (arg) != INTEGER_CST)
13923 return 1;
13924 return !tree_int_cst_equal (parm, arg);
13926 case TREE_VEC:
13928 int i;
13929 if (TREE_CODE (arg) != TREE_VEC)
13930 return 1;
13931 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
13932 return 1;
13933 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
13934 if (unify (tparms, targs,
13935 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
13936 UNIFY_ALLOW_NONE))
13937 return 1;
13938 return 0;
13941 case RECORD_TYPE:
13942 case UNION_TYPE:
13943 if (TREE_CODE (arg) != TREE_CODE (parm))
13944 return 1;
13946 if (TYPE_PTRMEMFUNC_P (parm))
13948 if (!TYPE_PTRMEMFUNC_P (arg))
13949 return 1;
13951 return unify (tparms, targs,
13952 TYPE_PTRMEMFUNC_FN_TYPE (parm),
13953 TYPE_PTRMEMFUNC_FN_TYPE (arg),
13954 strict);
13957 if (CLASSTYPE_TEMPLATE_INFO (parm))
13959 tree t = NULL_TREE;
13961 if (strict_in & UNIFY_ALLOW_DERIVED)
13963 /* First, we try to unify the PARM and ARG directly. */
13964 t = try_class_unification (tparms, targs,
13965 parm, arg);
13967 if (!t)
13969 /* Fallback to the special case allowed in
13970 [temp.deduct.call]:
13972 If P is a class, and P has the form
13973 template-id, then A can be a derived class of
13974 the deduced A. Likewise, if P is a pointer to
13975 a class of the form template-id, A can be a
13976 pointer to a derived class pointed to by the
13977 deduced A. */
13978 t = get_template_base (tparms, targs, parm, arg);
13980 if (!t)
13981 return 1;
13984 else if (CLASSTYPE_TEMPLATE_INFO (arg)
13985 && (CLASSTYPE_TI_TEMPLATE (parm)
13986 == CLASSTYPE_TI_TEMPLATE (arg)))
13987 /* Perhaps PARM is something like S<U> and ARG is S<int>.
13988 Then, we should unify `int' and `U'. */
13989 t = arg;
13990 else
13991 /* There's no chance of unification succeeding. */
13992 return 1;
13994 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
13995 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
13997 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
13998 return 1;
13999 return 0;
14001 case METHOD_TYPE:
14002 case FUNCTION_TYPE:
14004 unsigned int nargs;
14005 tree *args;
14006 tree a;
14007 unsigned int i;
14009 if (TREE_CODE (arg) != TREE_CODE (parm))
14010 return 1;
14012 /* CV qualifications for methods can never be deduced, they must
14013 match exactly. We need to check them explicitly here,
14014 because type_unification_real treats them as any other
14015 cv-qualified parameter. */
14016 if (TREE_CODE (parm) == METHOD_TYPE
14017 && (!check_cv_quals_for_unify
14018 (UNIFY_ALLOW_NONE,
14019 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14020 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14021 return 1;
14023 if (unify (tparms, targs, TREE_TYPE (parm),
14024 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14025 return 1;
14027 nargs = list_length (TYPE_ARG_TYPES (arg));
14028 args = XALLOCAVEC (tree, nargs);
14029 for (a = TYPE_ARG_TYPES (arg), i = 0;
14030 a != NULL_TREE && a != void_list_node;
14031 a = TREE_CHAIN (a), ++i)
14032 args[i] = TREE_VALUE (a);
14033 nargs = i;
14035 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14036 args, nargs, 1, DEDUCE_EXACT,
14037 LOOKUP_NORMAL);
14040 case OFFSET_TYPE:
14041 /* Unify a pointer to member with a pointer to member function, which
14042 deduces the type of the member as a function type. */
14043 if (TYPE_PTRMEMFUNC_P (arg))
14045 tree method_type;
14046 tree fntype;
14047 cp_cv_quals cv_quals;
14049 /* Check top-level cv qualifiers */
14050 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14051 return 1;
14053 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14054 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14055 return 1;
14057 /* Determine the type of the function we are unifying against. */
14058 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14059 fntype =
14060 build_function_type (TREE_TYPE (method_type),
14061 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14063 /* Extract the cv-qualifiers of the member function from the
14064 implicit object parameter and place them on the function
14065 type to be restored later. */
14066 cv_quals =
14067 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14068 fntype = build_qualified_type (fntype, cv_quals);
14069 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14072 if (TREE_CODE (arg) != OFFSET_TYPE)
14073 return 1;
14074 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14075 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14076 return 1;
14077 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14078 strict);
14080 case CONST_DECL:
14081 if (DECL_TEMPLATE_PARM_P (parm))
14082 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14083 if (arg != integral_constant_value (parm))
14084 return 1;
14085 return 0;
14087 case FIELD_DECL:
14088 case TEMPLATE_DECL:
14089 /* Matched cases are handled by the ARG == PARM test above. */
14090 return 1;
14092 case TYPE_ARGUMENT_PACK:
14093 case NONTYPE_ARGUMENT_PACK:
14095 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14096 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14097 int i, len = TREE_VEC_LENGTH (packed_parms);
14098 int argslen = TREE_VEC_LENGTH (packed_args);
14099 int parm_variadic_p = 0;
14101 for (i = 0; i < len; ++i)
14103 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14105 if (i == len - 1)
14106 /* We can unify against something with a trailing
14107 parameter pack. */
14108 parm_variadic_p = 1;
14109 else
14110 /* Since there is something following the pack
14111 expansion, we cannot unify this template argument
14112 list. */
14113 return 0;
14118 /* If we don't have enough arguments to satisfy the parameters
14119 (not counting the pack expression at the end), or we have
14120 too many arguments for a parameter list that doesn't end in
14121 a pack expression, we can't unify. */
14122 if (argslen < (len - parm_variadic_p)
14123 || (argslen > len && !parm_variadic_p))
14124 return 1;
14126 /* Unify all of the parameters that precede the (optional)
14127 pack expression. */
14128 for (i = 0; i < len - parm_variadic_p; ++i)
14130 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14131 TREE_VEC_ELT (packed_args, i), strict))
14132 return 1;
14135 if (parm_variadic_p)
14136 return unify_pack_expansion (tparms, targs,
14137 packed_parms, packed_args,
14138 strict, /*call_args_p=*/false,
14139 /*subr=*/false);
14140 return 0;
14143 break;
14145 case TYPEOF_TYPE:
14146 case DECLTYPE_TYPE:
14147 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14148 nodes. */
14149 return 0;
14151 case ERROR_MARK:
14152 /* Unification fails if we hit an error node. */
14153 return 1;
14155 default:
14156 gcc_assert (EXPR_P (parm));
14158 /* We must be looking at an expression. This can happen with
14159 something like:
14161 template <int I>
14162 void foo(S<I>, S<I + 2>);
14164 This is a "nondeduced context":
14166 [deduct.type]
14168 The nondeduced contexts are:
14170 --A type that is a template-id in which one or more of
14171 the template-arguments is an expression that references
14172 a template-parameter.
14174 In these cases, we assume deduction succeeded, but don't
14175 actually infer any unifications. */
14177 if (!uses_template_parms (parm)
14178 && !template_args_equal (parm, arg))
14179 return 1;
14180 else
14181 return 0;
14185 /* Note that DECL can be defined in this translation unit, if
14186 required. */
14188 static void
14189 mark_definable (tree decl)
14191 tree clone;
14192 DECL_NOT_REALLY_EXTERN (decl) = 1;
14193 FOR_EACH_CLONE (clone, decl)
14194 DECL_NOT_REALLY_EXTERN (clone) = 1;
14197 /* Called if RESULT is explicitly instantiated, or is a member of an
14198 explicitly instantiated class. */
14200 void
14201 mark_decl_instantiated (tree result, int extern_p)
14203 SET_DECL_EXPLICIT_INSTANTIATION (result);
14205 /* If this entity has already been written out, it's too late to
14206 make any modifications. */
14207 if (TREE_ASM_WRITTEN (result))
14208 return;
14210 if (TREE_CODE (result) != FUNCTION_DECL)
14211 /* The TREE_PUBLIC flag for function declarations will have been
14212 set correctly by tsubst. */
14213 TREE_PUBLIC (result) = 1;
14215 /* This might have been set by an earlier implicit instantiation. */
14216 DECL_COMDAT (result) = 0;
14218 if (extern_p)
14219 DECL_NOT_REALLY_EXTERN (result) = 0;
14220 else
14222 mark_definable (result);
14223 /* Always make artificials weak. */
14224 if (DECL_ARTIFICIAL (result) && flag_weak)
14225 comdat_linkage (result);
14226 /* For WIN32 we also want to put explicit instantiations in
14227 linkonce sections. */
14228 else if (TREE_PUBLIC (result))
14229 maybe_make_one_only (result);
14232 /* If EXTERN_P, then this function will not be emitted -- unless
14233 followed by an explicit instantiation, at which point its linkage
14234 will be adjusted. If !EXTERN_P, then this function will be
14235 emitted here. In neither circumstance do we want
14236 import_export_decl to adjust the linkage. */
14237 DECL_INTERFACE_KNOWN (result) = 1;
14240 /* Given two function templates PAT1 and PAT2, return:
14242 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14243 -1 if PAT2 is more specialized than PAT1.
14244 0 if neither is more specialized.
14246 LEN indicates the number of parameters we should consider
14247 (defaulted parameters should not be considered).
14249 The 1998 std underspecified function template partial ordering, and
14250 DR214 addresses the issue. We take pairs of arguments, one from
14251 each of the templates, and deduce them against each other. One of
14252 the templates will be more specialized if all the *other*
14253 template's arguments deduce against its arguments and at least one
14254 of its arguments *does* *not* deduce against the other template's
14255 corresponding argument. Deduction is done as for class templates.
14256 The arguments used in deduction have reference and top level cv
14257 qualifiers removed. Iff both arguments were originally reference
14258 types *and* deduction succeeds in both directions, the template
14259 with the more cv-qualified argument wins for that pairing (if
14260 neither is more cv-qualified, they both are equal). Unlike regular
14261 deduction, after all the arguments have been deduced in this way,
14262 we do *not* verify the deduced template argument values can be
14263 substituted into non-deduced contexts, nor do we have to verify
14264 that all template arguments have been deduced. */
14267 more_specialized_fn (tree pat1, tree pat2, int len)
14269 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14270 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14271 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14272 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14273 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14274 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14275 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14276 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14277 int better1 = 0;
14278 int better2 = 0;
14280 /* Remove the this parameter from non-static member functions. If
14281 one is a non-static member function and the other is not a static
14282 member function, remove the first parameter from that function
14283 also. This situation occurs for operator functions where we
14284 locate both a member function (with this pointer) and non-member
14285 operator (with explicit first operand). */
14286 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
14288 len--; /* LEN is the number of significant arguments for DECL1 */
14289 args1 = TREE_CHAIN (args1);
14290 if (!DECL_STATIC_FUNCTION_P (decl2))
14291 args2 = TREE_CHAIN (args2);
14293 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14295 args2 = TREE_CHAIN (args2);
14296 if (!DECL_STATIC_FUNCTION_P (decl1))
14298 len--;
14299 args1 = TREE_CHAIN (args1);
14303 /* If only one is a conversion operator, they are unordered. */
14304 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14305 return 0;
14307 /* Consider the return type for a conversion function */
14308 if (DECL_CONV_FN_P (decl1))
14310 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14311 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14312 len++;
14315 processing_template_decl++;
14317 while (len--
14318 /* Stop when an ellipsis is seen. */
14319 && args1 != NULL_TREE && args2 != NULL_TREE)
14321 tree arg1 = TREE_VALUE (args1);
14322 tree arg2 = TREE_VALUE (args2);
14323 int deduce1, deduce2;
14324 int quals1 = -1;
14325 int quals2 = -1;
14327 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14328 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14330 /* When both arguments are pack expansions, we need only
14331 unify the patterns themselves. */
14332 arg1 = PACK_EXPANSION_PATTERN (arg1);
14333 arg2 = PACK_EXPANSION_PATTERN (arg2);
14335 /* This is the last comparison we need to do. */
14336 len = 0;
14339 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14341 arg1 = TREE_TYPE (arg1);
14342 quals1 = cp_type_quals (arg1);
14345 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14347 arg2 = TREE_TYPE (arg2);
14348 quals2 = cp_type_quals (arg2);
14351 if ((quals1 < 0) != (quals2 < 0))
14353 /* Only of the args is a reference, see if we should apply
14354 array/function pointer decay to it. This is not part of
14355 DR214, but is, IMHO, consistent with the deduction rules
14356 for the function call itself, and with our earlier
14357 implementation of the underspecified partial ordering
14358 rules. (nathan). */
14359 if (quals1 >= 0)
14361 switch (TREE_CODE (arg1))
14363 case ARRAY_TYPE:
14364 arg1 = TREE_TYPE (arg1);
14365 /* FALLTHROUGH. */
14366 case FUNCTION_TYPE:
14367 arg1 = build_pointer_type (arg1);
14368 break;
14370 default:
14371 break;
14374 else
14376 switch (TREE_CODE (arg2))
14378 case ARRAY_TYPE:
14379 arg2 = TREE_TYPE (arg2);
14380 /* FALLTHROUGH. */
14381 case FUNCTION_TYPE:
14382 arg2 = build_pointer_type (arg2);
14383 break;
14385 default:
14386 break;
14391 arg1 = TYPE_MAIN_VARIANT (arg1);
14392 arg2 = TYPE_MAIN_VARIANT (arg2);
14394 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14396 int i, len2 = list_length (args2);
14397 tree parmvec = make_tree_vec (1);
14398 tree argvec = make_tree_vec (len2);
14399 tree ta = args2;
14401 /* Setup the parameter vector, which contains only ARG1. */
14402 TREE_VEC_ELT (parmvec, 0) = arg1;
14404 /* Setup the argument vector, which contains the remaining
14405 arguments. */
14406 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14407 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14409 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14410 argvec, UNIFY_ALLOW_NONE,
14411 /*call_args_p=*/false,
14412 /*subr=*/0);
14414 /* We cannot deduce in the other direction, because ARG1 is
14415 a pack expansion but ARG2 is not. */
14416 deduce2 = 0;
14418 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14420 int i, len1 = list_length (args1);
14421 tree parmvec = make_tree_vec (1);
14422 tree argvec = make_tree_vec (len1);
14423 tree ta = args1;
14425 /* Setup the parameter vector, which contains only ARG1. */
14426 TREE_VEC_ELT (parmvec, 0) = arg2;
14428 /* Setup the argument vector, which contains the remaining
14429 arguments. */
14430 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14431 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14433 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14434 argvec, UNIFY_ALLOW_NONE,
14435 /*call_args_p=*/false,
14436 /*subr=*/0);
14438 /* We cannot deduce in the other direction, because ARG2 is
14439 a pack expansion but ARG1 is not.*/
14440 deduce1 = 0;
14443 else
14445 /* The normal case, where neither argument is a pack
14446 expansion. */
14447 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14448 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14451 if (!deduce1)
14452 better2 = -1;
14453 if (!deduce2)
14454 better1 = -1;
14455 if (better1 < 0 && better2 < 0)
14456 /* We've failed to deduce something in either direction.
14457 These must be unordered. */
14458 break;
14460 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14462 /* Deduces in both directions, see if quals can
14463 disambiguate. Pretend the worse one failed to deduce. */
14464 if ((quals1 & quals2) == quals2)
14465 deduce1 = 0;
14466 if ((quals1 & quals2) == quals1)
14467 deduce2 = 0;
14469 if (deduce1 && !deduce2 && !better2)
14470 better2 = 1;
14471 if (deduce2 && !deduce1 && !better1)
14472 better1 = 1;
14474 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14475 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14476 /* We have already processed all of the arguments in our
14477 handing of the pack expansion type. */
14478 len = 0;
14480 args1 = TREE_CHAIN (args1);
14481 args2 = TREE_CHAIN (args2);
14484 processing_template_decl--;
14486 /* All things being equal, if the next argument is a pack expansion
14487 for one function but not for the other, prefer the
14488 non-variadic function. */
14489 if ((better1 > 0) - (better2 > 0) == 0
14490 && args1 && TREE_VALUE (args1)
14491 && args2 && TREE_VALUE (args2))
14493 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14494 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14495 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14496 return 1;
14499 return (better1 > 0) - (better2 > 0);
14502 /* Determine which of two partial specializations is more specialized.
14504 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14505 to the first partial specialization. The TREE_VALUE is the
14506 innermost set of template parameters for the partial
14507 specialization. PAT2 is similar, but for the second template.
14509 Return 1 if the first partial specialization is more specialized;
14510 -1 if the second is more specialized; 0 if neither is more
14511 specialized.
14513 See [temp.class.order] for information about determining which of
14514 two templates is more specialized. */
14516 static int
14517 more_specialized_class (tree pat1, tree pat2)
14519 tree targs;
14520 tree tmpl1, tmpl2;
14521 int winner = 0;
14522 bool any_deductions = false;
14524 tmpl1 = TREE_TYPE (pat1);
14525 tmpl2 = TREE_TYPE (pat2);
14527 /* Just like what happens for functions, if we are ordering between
14528 different class template specializations, we may encounter dependent
14529 types in the arguments, and we need our dependency check functions
14530 to behave correctly. */
14531 ++processing_template_decl;
14532 targs = get_class_bindings (TREE_VALUE (pat1),
14533 CLASSTYPE_TI_ARGS (tmpl1),
14534 CLASSTYPE_TI_ARGS (tmpl2));
14535 if (targs)
14537 --winner;
14538 any_deductions = true;
14541 targs = get_class_bindings (TREE_VALUE (pat2),
14542 CLASSTYPE_TI_ARGS (tmpl2),
14543 CLASSTYPE_TI_ARGS (tmpl1));
14544 if (targs)
14546 ++winner;
14547 any_deductions = true;
14549 --processing_template_decl;
14551 /* In the case of a tie where at least one of the class templates
14552 has a parameter pack at the end, the template with the most
14553 non-packed parameters wins. */
14554 if (winner == 0
14555 && any_deductions
14556 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14557 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14559 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14560 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14561 int len1 = TREE_VEC_LENGTH (args1);
14562 int len2 = TREE_VEC_LENGTH (args2);
14564 /* We don't count the pack expansion at the end. */
14565 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14566 --len1;
14567 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14568 --len2;
14570 if (len1 > len2)
14571 return 1;
14572 else if (len1 < len2)
14573 return -1;
14576 return winner;
14579 /* Return the template arguments that will produce the function signature
14580 DECL from the function template FN, with the explicit template
14581 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
14582 also match. Return NULL_TREE if no satisfactory arguments could be
14583 found. */
14585 static tree
14586 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
14588 int ntparms = DECL_NTPARMS (fn);
14589 tree targs = make_tree_vec (ntparms);
14590 tree decl_type;
14591 tree decl_arg_types;
14592 tree *args;
14593 unsigned int nargs, ix;
14594 tree arg;
14596 /* Substitute the explicit template arguments into the type of DECL.
14597 The call to fn_type_unification will handle substitution into the
14598 FN. */
14599 decl_type = TREE_TYPE (decl);
14600 if (explicit_args && uses_template_parms (decl_type))
14602 tree tmpl;
14603 tree converted_args;
14605 if (DECL_TEMPLATE_INFO (decl))
14606 tmpl = DECL_TI_TEMPLATE (decl);
14607 else
14608 /* We can get here for some invalid specializations. */
14609 return NULL_TREE;
14611 converted_args
14612 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
14613 explicit_args, NULL_TREE,
14614 tf_none,
14615 /*require_all_args=*/false,
14616 /*use_default_args=*/false);
14617 if (converted_args == error_mark_node)
14618 return NULL_TREE;
14620 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
14621 if (decl_type == error_mark_node)
14622 return NULL_TREE;
14625 /* Never do unification on the 'this' parameter. */
14626 decl_arg_types = skip_artificial_parms_for (decl,
14627 TYPE_ARG_TYPES (decl_type));
14629 nargs = list_length (decl_arg_types);
14630 args = XALLOCAVEC (tree, nargs);
14631 for (arg = decl_arg_types, ix = 0;
14632 arg != NULL_TREE && arg != void_list_node;
14633 arg = TREE_CHAIN (arg), ++ix)
14634 args[ix] = TREE_VALUE (arg);
14636 if (fn_type_unification (fn, explicit_args, targs,
14637 args, ix,
14638 (check_rettype || DECL_CONV_FN_P (fn)
14639 ? TREE_TYPE (decl_type) : NULL_TREE),
14640 DEDUCE_EXACT, LOOKUP_NORMAL))
14641 return NULL_TREE;
14643 return targs;
14646 /* Return the innermost template arguments that, when applied to a
14647 template specialization whose innermost template parameters are
14648 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
14649 ARGS.
14651 For example, suppose we have:
14653 template <class T, class U> struct S {};
14654 template <class T> struct S<T*, int> {};
14656 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
14657 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
14658 int}. The resulting vector will be {double}, indicating that `T'
14659 is bound to `double'. */
14661 static tree
14662 get_class_bindings (tree tparms, tree spec_args, tree args)
14664 int i, ntparms = TREE_VEC_LENGTH (tparms);
14665 tree deduced_args;
14666 tree innermost_deduced_args;
14668 innermost_deduced_args = make_tree_vec (ntparms);
14669 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14671 deduced_args = copy_node (args);
14672 SET_TMPL_ARGS_LEVEL (deduced_args,
14673 TMPL_ARGS_DEPTH (deduced_args),
14674 innermost_deduced_args);
14676 else
14677 deduced_args = innermost_deduced_args;
14679 if (unify (tparms, deduced_args,
14680 INNERMOST_TEMPLATE_ARGS (spec_args),
14681 INNERMOST_TEMPLATE_ARGS (args),
14682 UNIFY_ALLOW_NONE))
14683 return NULL_TREE;
14685 for (i = 0; i < ntparms; ++i)
14686 if (! TREE_VEC_ELT (innermost_deduced_args, i))
14687 return NULL_TREE;
14689 /* Verify that nondeduced template arguments agree with the type
14690 obtained from argument deduction.
14692 For example:
14694 struct A { typedef int X; };
14695 template <class T, class U> struct C {};
14696 template <class T> struct C<T, typename T::X> {};
14698 Then with the instantiation `C<A, int>', we can deduce that
14699 `T' is `A' but unify () does not check whether `typename T::X'
14700 is `int'. */
14701 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
14702 if (spec_args == error_mark_node
14703 /* We only need to check the innermost arguments; the other
14704 arguments will always agree. */
14705 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
14706 INNERMOST_TEMPLATE_ARGS (args)))
14707 return NULL_TREE;
14709 /* Now that we have bindings for all of the template arguments,
14710 ensure that the arguments deduced for the template template
14711 parameters have compatible template parameter lists. See the use
14712 of template_template_parm_bindings_ok_p in fn_type_unification
14713 for more information. */
14714 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
14715 return NULL_TREE;
14717 return deduced_args;
14720 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
14721 Return the TREE_LIST node with the most specialized template, if
14722 any. If there is no most specialized template, the error_mark_node
14723 is returned.
14725 Note that this function does not look at, or modify, the
14726 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
14727 returned is one of the elements of INSTANTIATIONS, callers may
14728 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
14729 and retrieve it from the value returned. */
14731 tree
14732 most_specialized_instantiation (tree templates)
14734 tree fn, champ;
14736 ++processing_template_decl;
14738 champ = templates;
14739 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
14741 int fate = 0;
14743 if (get_bindings (TREE_VALUE (champ),
14744 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14745 NULL_TREE, /*check_ret=*/false))
14746 fate--;
14748 if (get_bindings (TREE_VALUE (fn),
14749 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14750 NULL_TREE, /*check_ret=*/false))
14751 fate++;
14753 if (fate == -1)
14754 champ = fn;
14755 else if (!fate)
14757 /* Equally specialized, move to next function. If there
14758 is no next function, nothing's most specialized. */
14759 fn = TREE_CHAIN (fn);
14760 champ = fn;
14761 if (!fn)
14762 break;
14766 if (champ)
14767 /* Now verify that champ is better than everything earlier in the
14768 instantiation list. */
14769 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
14770 if (get_bindings (TREE_VALUE (champ),
14771 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14772 NULL_TREE, /*check_ret=*/false)
14773 || !get_bindings (TREE_VALUE (fn),
14774 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14775 NULL_TREE, /*check_ret=*/false))
14777 champ = NULL_TREE;
14778 break;
14781 processing_template_decl--;
14783 if (!champ)
14784 return error_mark_node;
14786 return champ;
14789 /* If DECL is a specialization of some template, return the most
14790 general such template. Otherwise, returns NULL_TREE.
14792 For example, given:
14794 template <class T> struct S { template <class U> void f(U); };
14796 if TMPL is `template <class U> void S<int>::f(U)' this will return
14797 the full template. This function will not trace past partial
14798 specializations, however. For example, given in addition:
14800 template <class T> struct S<T*> { template <class U> void f(U); };
14802 if TMPL is `template <class U> void S<int*>::f(U)' this will return
14803 `template <class T> template <class U> S<T*>::f(U)'. */
14805 tree
14806 most_general_template (tree decl)
14808 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
14809 an immediate specialization. */
14810 if (TREE_CODE (decl) == FUNCTION_DECL)
14812 if (DECL_TEMPLATE_INFO (decl)) {
14813 decl = DECL_TI_TEMPLATE (decl);
14815 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
14816 template friend. */
14817 if (TREE_CODE (decl) != TEMPLATE_DECL)
14818 return NULL_TREE;
14819 } else
14820 return NULL_TREE;
14823 /* Look for more and more general templates. */
14824 while (DECL_TEMPLATE_INFO (decl))
14826 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
14827 (See cp-tree.h for details.) */
14828 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
14829 break;
14831 if (CLASS_TYPE_P (TREE_TYPE (decl))
14832 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
14833 break;
14835 /* Stop if we run into an explicitly specialized class template. */
14836 if (!DECL_NAMESPACE_SCOPE_P (decl)
14837 && DECL_CONTEXT (decl)
14838 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
14839 break;
14841 decl = DECL_TI_TEMPLATE (decl);
14844 return decl;
14847 /* Return the most specialized of the class template partial
14848 specializations of TMPL which can produce TYPE, a specialization of
14849 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
14850 a _TYPE node corresponding to the partial specialization, while the
14851 TREE_PURPOSE is the set of template arguments that must be
14852 substituted into the TREE_TYPE in order to generate TYPE.
14854 If the choice of partial specialization is ambiguous, a diagnostic
14855 is issued, and the error_mark_node is returned. If there are no
14856 partial specializations of TMPL matching TYPE, then NULL_TREE is
14857 returned. */
14859 static tree
14860 most_specialized_class (tree type, tree tmpl)
14862 tree list = NULL_TREE;
14863 tree t;
14864 tree champ;
14865 int fate;
14866 bool ambiguous_p;
14867 tree args;
14868 tree outer_args = NULL_TREE;
14870 tmpl = most_general_template (tmpl);
14871 args = CLASSTYPE_TI_ARGS (type);
14873 /* For determining which partial specialization to use, only the
14874 innermost args are interesting. */
14875 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14877 outer_args = strip_innermost_template_args (args, 1);
14878 args = INNERMOST_TEMPLATE_ARGS (args);
14881 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
14883 tree partial_spec_args;
14884 tree spec_args;
14885 tree parms = TREE_VALUE (t);
14887 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
14888 if (outer_args)
14890 int i;
14892 ++processing_template_decl;
14894 /* Discard the outer levels of args, and then substitute in the
14895 template args from the enclosing class. */
14896 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
14897 partial_spec_args = tsubst_template_args
14898 (partial_spec_args, outer_args, tf_none, NULL_TREE);
14900 /* PARMS already refers to just the innermost parms, but the
14901 template parms in partial_spec_args had their levels lowered
14902 by tsubst, so we need to do the same for the parm list. We
14903 can't just tsubst the TREE_VEC itself, as tsubst wants to
14904 treat a TREE_VEC as an argument vector. */
14905 parms = copy_node (parms);
14906 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
14907 TREE_VEC_ELT (parms, i) =
14908 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
14910 --processing_template_decl;
14912 spec_args = get_class_bindings (parms,
14913 partial_spec_args,
14914 args);
14915 if (spec_args)
14917 if (outer_args)
14918 spec_args = add_to_template_args (outer_args, spec_args);
14919 list = tree_cons (spec_args, TREE_VALUE (t), list);
14920 TREE_TYPE (list) = TREE_TYPE (t);
14924 if (! list)
14925 return NULL_TREE;
14927 ambiguous_p = false;
14928 t = list;
14929 champ = t;
14930 t = TREE_CHAIN (t);
14931 for (; t; t = TREE_CHAIN (t))
14933 fate = more_specialized_class (champ, t);
14934 if (fate == 1)
14936 else
14938 if (fate == 0)
14940 t = TREE_CHAIN (t);
14941 if (! t)
14943 ambiguous_p = true;
14944 break;
14947 champ = t;
14951 if (!ambiguous_p)
14952 for (t = list; t && t != champ; t = TREE_CHAIN (t))
14954 fate = more_specialized_class (champ, t);
14955 if (fate != 1)
14957 ambiguous_p = true;
14958 break;
14962 if (ambiguous_p)
14964 const char *str = "candidates are:";
14965 error ("ambiguous class template instantiation for %q#T", type);
14966 for (t = list; t; t = TREE_CHAIN (t))
14968 error ("%s %+#T", str, TREE_TYPE (t));
14969 str = " ";
14971 return error_mark_node;
14974 return champ;
14977 /* Explicitly instantiate DECL. */
14979 void
14980 do_decl_instantiation (tree decl, tree storage)
14982 tree result = NULL_TREE;
14983 int extern_p = 0;
14985 if (!decl || decl == error_mark_node)
14986 /* An error occurred, for which grokdeclarator has already issued
14987 an appropriate message. */
14988 return;
14989 else if (! DECL_LANG_SPECIFIC (decl))
14991 error ("explicit instantiation of non-template %q#D", decl);
14992 return;
14994 else if (TREE_CODE (decl) == VAR_DECL)
14996 /* There is an asymmetry here in the way VAR_DECLs and
14997 FUNCTION_DECLs are handled by grokdeclarator. In the case of
14998 the latter, the DECL we get back will be marked as a
14999 template instantiation, and the appropriate
15000 DECL_TEMPLATE_INFO will be set up. This does not happen for
15001 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15002 should handle VAR_DECLs as it currently handles
15003 FUNCTION_DECLs. */
15004 if (!DECL_CLASS_SCOPE_P (decl))
15006 error ("%qD is not a static data member of a class template", decl);
15007 return;
15009 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15010 if (!result || TREE_CODE (result) != VAR_DECL)
15012 error ("no matching template for %qD found", decl);
15013 return;
15015 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15017 error ("type %qT for explicit instantiation %qD does not match "
15018 "declared type %qT", TREE_TYPE (result), decl,
15019 TREE_TYPE (decl));
15020 return;
15023 else if (TREE_CODE (decl) != FUNCTION_DECL)
15025 error ("explicit instantiation of %q#D", decl);
15026 return;
15028 else
15029 result = decl;
15031 /* Check for various error cases. Note that if the explicit
15032 instantiation is valid the RESULT will currently be marked as an
15033 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15034 until we get here. */
15036 if (DECL_TEMPLATE_SPECIALIZATION (result))
15038 /* DR 259 [temp.spec].
15040 Both an explicit instantiation and a declaration of an explicit
15041 specialization shall not appear in a program unless the explicit
15042 instantiation follows a declaration of the explicit specialization.
15044 For a given set of template parameters, if an explicit
15045 instantiation of a template appears after a declaration of an
15046 explicit specialization for that template, the explicit
15047 instantiation has no effect. */
15048 return;
15050 else if (DECL_EXPLICIT_INSTANTIATION (result))
15052 /* [temp.spec]
15054 No program shall explicitly instantiate any template more
15055 than once.
15057 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15058 the first instantiation was `extern' and the second is not,
15059 and EXTERN_P for the opposite case. */
15060 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15061 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15062 /* If an "extern" explicit instantiation follows an ordinary
15063 explicit instantiation, the template is instantiated. */
15064 if (extern_p)
15065 return;
15067 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15069 error ("no matching template for %qD found", result);
15070 return;
15072 else if (!DECL_TEMPLATE_INFO (result))
15074 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15075 return;
15078 if (storage == NULL_TREE)
15080 else if (storage == ridpointers[(int) RID_EXTERN])
15082 if (!in_system_header && (cxx_dialect == cxx98))
15083 pedwarn (input_location, OPT_pedantic,
15084 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15085 "instantiations");
15086 extern_p = 1;
15088 else
15089 error ("storage class %qD applied to template instantiation", storage);
15091 check_explicit_instantiation_namespace (result);
15092 mark_decl_instantiated (result, extern_p);
15093 if (! extern_p)
15094 instantiate_decl (result, /*defer_ok=*/1,
15095 /*expl_inst_class_mem_p=*/false);
15098 static void
15099 mark_class_instantiated (tree t, int extern_p)
15101 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15102 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15103 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15104 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15105 if (! extern_p)
15107 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15108 rest_of_type_compilation (t, 1);
15112 /* Called from do_type_instantiation through binding_table_foreach to
15113 do recursive instantiation for the type bound in ENTRY. */
15114 static void
15115 bt_instantiate_type_proc (binding_entry entry, void *data)
15117 tree storage = *(tree *) data;
15119 if (MAYBE_CLASS_TYPE_P (entry->type)
15120 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15121 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15124 /* Called from do_type_instantiation to instantiate a member
15125 (a member function or a static member variable) of an
15126 explicitly instantiated class template. */
15127 static void
15128 instantiate_class_member (tree decl, int extern_p)
15130 mark_decl_instantiated (decl, extern_p);
15131 if (! extern_p)
15132 instantiate_decl (decl, /*defer_ok=*/1,
15133 /*expl_inst_class_mem_p=*/true);
15136 /* Perform an explicit instantiation of template class T. STORAGE, if
15137 non-null, is the RID for extern, inline or static. COMPLAIN is
15138 nonzero if this is called from the parser, zero if called recursively,
15139 since the standard is unclear (as detailed below). */
15141 void
15142 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15144 int extern_p = 0;
15145 int nomem_p = 0;
15146 int static_p = 0;
15147 int previous_instantiation_extern_p = 0;
15149 if (TREE_CODE (t) == TYPE_DECL)
15150 t = TREE_TYPE (t);
15152 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15154 error ("explicit instantiation of non-template type %qT", t);
15155 return;
15158 complete_type (t);
15160 if (!COMPLETE_TYPE_P (t))
15162 if (complain & tf_error)
15163 error ("explicit instantiation of %q#T before definition of template",
15165 return;
15168 if (storage != NULL_TREE)
15170 if (!in_system_header)
15172 if (storage == ridpointers[(int) RID_EXTERN])
15174 if (cxx_dialect == cxx98)
15175 pedwarn (input_location, OPT_pedantic,
15176 "ISO C++ 1998 forbids the use of %<extern%> on "
15177 "explicit instantiations");
15179 else
15180 pedwarn (input_location, OPT_pedantic,
15181 "ISO C++ forbids the use of %qE"
15182 " on explicit instantiations", storage);
15185 if (storage == ridpointers[(int) RID_INLINE])
15186 nomem_p = 1;
15187 else if (storage == ridpointers[(int) RID_EXTERN])
15188 extern_p = 1;
15189 else if (storage == ridpointers[(int) RID_STATIC])
15190 static_p = 1;
15191 else
15193 error ("storage class %qD applied to template instantiation",
15194 storage);
15195 extern_p = 0;
15199 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
15201 /* DR 259 [temp.spec].
15203 Both an explicit instantiation and a declaration of an explicit
15204 specialization shall not appear in a program unless the explicit
15205 instantiation follows a declaration of the explicit specialization.
15207 For a given set of template parameters, if an explicit
15208 instantiation of a template appears after a declaration of an
15209 explicit specialization for that template, the explicit
15210 instantiation has no effect. */
15211 return;
15213 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
15215 /* [temp.spec]
15217 No program shall explicitly instantiate any template more
15218 than once.
15220 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15221 instantiation was `extern'. If EXTERN_P then the second is.
15222 These cases are OK. */
15223 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15225 if (!previous_instantiation_extern_p && !extern_p
15226 && (complain & tf_error))
15227 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
15229 /* If we've already instantiated the template, just return now. */
15230 if (!CLASSTYPE_INTERFACE_ONLY (t))
15231 return;
15234 check_explicit_instantiation_namespace (TYPE_NAME (t));
15235 mark_class_instantiated (t, extern_p);
15237 if (nomem_p)
15238 return;
15241 tree tmp;
15243 /* In contrast to implicit instantiation, where only the
15244 declarations, and not the definitions, of members are
15245 instantiated, we have here:
15247 [temp.explicit]
15249 The explicit instantiation of a class template specialization
15250 implies the instantiation of all of its members not
15251 previously explicitly specialized in the translation unit
15252 containing the explicit instantiation.
15254 Of course, we can't instantiate member template classes, since
15255 we don't have any arguments for them. Note that the standard
15256 is unclear on whether the instantiation of the members are
15257 *explicit* instantiations or not. However, the most natural
15258 interpretation is that it should be an explicit instantiation. */
15260 if (! static_p)
15261 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
15262 if (TREE_CODE (tmp) == FUNCTION_DECL
15263 && DECL_TEMPLATE_INSTANTIATION (tmp))
15264 instantiate_class_member (tmp, extern_p);
15266 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15267 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
15268 instantiate_class_member (tmp, extern_p);
15270 if (CLASSTYPE_NESTED_UTDS (t))
15271 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
15272 bt_instantiate_type_proc, &storage);
15276 /* Given a function DECL, which is a specialization of TMPL, modify
15277 DECL to be a re-instantiation of TMPL with the same template
15278 arguments. TMPL should be the template into which tsubst'ing
15279 should occur for DECL, not the most general template.
15281 One reason for doing this is a scenario like this:
15283 template <class T>
15284 void f(const T&, int i);
15286 void g() { f(3, 7); }
15288 template <class T>
15289 void f(const T& t, const int i) { }
15291 Note that when the template is first instantiated, with
15292 instantiate_template, the resulting DECL will have no name for the
15293 first parameter, and the wrong type for the second. So, when we go
15294 to instantiate the DECL, we regenerate it. */
15296 static void
15297 regenerate_decl_from_template (tree decl, tree tmpl)
15299 /* The arguments used to instantiate DECL, from the most general
15300 template. */
15301 tree args;
15302 tree code_pattern;
15304 args = DECL_TI_ARGS (decl);
15305 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
15307 /* Make sure that we can see identifiers, and compute access
15308 correctly. */
15309 push_access_scope (decl);
15311 if (TREE_CODE (decl) == FUNCTION_DECL)
15313 tree decl_parm;
15314 tree pattern_parm;
15315 tree specs;
15316 int args_depth;
15317 int parms_depth;
15319 args_depth = TMPL_ARGS_DEPTH (args);
15320 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
15321 if (args_depth > parms_depth)
15322 args = get_innermost_template_args (args, parms_depth);
15324 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15325 args, tf_error, NULL_TREE);
15326 if (specs)
15327 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15328 specs);
15330 /* Merge parameter declarations. */
15331 decl_parm = skip_artificial_parms_for (decl,
15332 DECL_ARGUMENTS (decl));
15333 pattern_parm
15334 = skip_artificial_parms_for (code_pattern,
15335 DECL_ARGUMENTS (code_pattern));
15336 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
15338 tree parm_type;
15339 tree attributes;
15341 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15342 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15343 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15344 NULL_TREE);
15345 parm_type = type_decays_to (parm_type);
15346 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15347 TREE_TYPE (decl_parm) = parm_type;
15348 attributes = DECL_ATTRIBUTES (pattern_parm);
15349 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15351 DECL_ATTRIBUTES (decl_parm) = attributes;
15352 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15354 decl_parm = TREE_CHAIN (decl_parm);
15355 pattern_parm = TREE_CHAIN (pattern_parm);
15357 /* Merge any parameters that match with the function parameter
15358 pack. */
15359 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15361 int i, len;
15362 tree expanded_types;
15363 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15364 the parameters in this function parameter pack. */
15365 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15366 args, tf_error, NULL_TREE);
15367 len = TREE_VEC_LENGTH (expanded_types);
15368 for (i = 0; i < len; i++)
15370 tree parm_type;
15371 tree attributes;
15373 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15374 /* Rename the parameter to include the index. */
15375 DECL_NAME (decl_parm) =
15376 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15377 parm_type = TREE_VEC_ELT (expanded_types, i);
15378 parm_type = type_decays_to (parm_type);
15379 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15380 TREE_TYPE (decl_parm) = parm_type;
15381 attributes = DECL_ATTRIBUTES (pattern_parm);
15382 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15384 DECL_ATTRIBUTES (decl_parm) = attributes;
15385 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15387 decl_parm = TREE_CHAIN (decl_parm);
15390 /* Merge additional specifiers from the CODE_PATTERN. */
15391 if (DECL_DECLARED_INLINE_P (code_pattern)
15392 && !DECL_DECLARED_INLINE_P (decl))
15393 DECL_DECLARED_INLINE_P (decl) = 1;
15395 else if (TREE_CODE (decl) == VAR_DECL)
15396 DECL_INITIAL (decl) =
15397 tsubst_expr (DECL_INITIAL (code_pattern), args,
15398 tf_error, DECL_TI_TEMPLATE (decl),
15399 /*integral_constant_expression_p=*/false);
15400 else
15401 gcc_unreachable ();
15403 pop_access_scope (decl);
15406 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15407 substituted to get DECL. */
15409 tree
15410 template_for_substitution (tree decl)
15412 tree tmpl = DECL_TI_TEMPLATE (decl);
15414 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15415 for the instantiation. This is not always the most general
15416 template. Consider, for example:
15418 template <class T>
15419 struct S { template <class U> void f();
15420 template <> void f<int>(); };
15422 and an instantiation of S<double>::f<int>. We want TD to be the
15423 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15424 while (/* An instantiation cannot have a definition, so we need a
15425 more general template. */
15426 DECL_TEMPLATE_INSTANTIATION (tmpl)
15427 /* We must also deal with friend templates. Given:
15429 template <class T> struct S {
15430 template <class U> friend void f() {};
15433 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15434 so far as the language is concerned, but that's still
15435 where we get the pattern for the instantiation from. On
15436 other hand, if the definition comes outside the class, say:
15438 template <class T> struct S {
15439 template <class U> friend void f();
15441 template <class U> friend void f() {}
15443 we don't need to look any further. That's what the check for
15444 DECL_INITIAL is for. */
15445 || (TREE_CODE (decl) == FUNCTION_DECL
15446 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15447 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15449 /* The present template, TD, should not be a definition. If it
15450 were a definition, we should be using it! Note that we
15451 cannot restructure the loop to just keep going until we find
15452 a template with a definition, since that might go too far if
15453 a specialization was declared, but not defined. */
15454 gcc_assert (TREE_CODE (decl) != VAR_DECL
15455 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
15457 /* Fetch the more general template. */
15458 tmpl = DECL_TI_TEMPLATE (tmpl);
15461 return tmpl;
15464 /* Produce the definition of D, a _DECL generated from a template. If
15465 DEFER_OK is nonzero, then we don't have to actually do the
15466 instantiation now; we just have to do it sometime. Normally it is
15467 an error if this is an explicit instantiation but D is undefined.
15468 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15469 explicitly instantiated class template. */
15471 tree
15472 instantiate_decl (tree d, int defer_ok,
15473 bool expl_inst_class_mem_p)
15475 tree tmpl = DECL_TI_TEMPLATE (d);
15476 tree gen_args;
15477 tree args;
15478 tree td;
15479 tree code_pattern;
15480 tree spec;
15481 tree gen_tmpl;
15482 bool pattern_defined;
15483 int need_push;
15484 location_t saved_loc = input_location;
15485 bool external_p;
15487 /* This function should only be used to instantiate templates for
15488 functions and static member variables. */
15489 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15490 || TREE_CODE (d) == VAR_DECL);
15492 /* Variables are never deferred; if instantiation is required, they
15493 are instantiated right away. That allows for better code in the
15494 case that an expression refers to the value of the variable --
15495 if the variable has a constant value the referring expression can
15496 take advantage of that fact. */
15497 if (TREE_CODE (d) == VAR_DECL)
15498 defer_ok = 0;
15500 /* Don't instantiate cloned functions. Instead, instantiate the
15501 functions they cloned. */
15502 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15503 d = DECL_CLONED_FUNCTION (d);
15505 if (DECL_TEMPLATE_INSTANTIATED (d))
15506 /* D has already been instantiated. It might seem reasonable to
15507 check whether or not D is an explicit instantiation, and, if so,
15508 stop here. But when an explicit instantiation is deferred
15509 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
15510 is set, even though we still need to do the instantiation. */
15511 return d;
15513 /* If we already have a specialization of this declaration, then
15514 there's no reason to instantiate it. Note that
15515 retrieve_specialization gives us both instantiations and
15516 specializations, so we must explicitly check
15517 DECL_TEMPLATE_SPECIALIZATION. */
15518 gen_tmpl = most_general_template (tmpl);
15519 gen_args = DECL_TI_ARGS (d);
15520 spec = retrieve_specialization (gen_tmpl, gen_args,
15521 /*class_specializations_p=*/false);
15522 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
15523 return spec;
15525 /* This needs to happen before any tsubsting. */
15526 if (! push_tinst_level (d))
15527 return d;
15529 timevar_push (TV_PARSE);
15531 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15532 for the instantiation. */
15533 td = template_for_substitution (d);
15534 code_pattern = DECL_TEMPLATE_RESULT (td);
15536 /* We should never be trying to instantiate a member of a class
15537 template or partial specialization. */
15538 gcc_assert (d != code_pattern);
15540 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15541 || DECL_TEMPLATE_SPECIALIZATION (td))
15542 /* In the case of a friend template whose definition is provided
15543 outside the class, we may have too many arguments. Drop the
15544 ones we don't need. The same is true for specializations. */
15545 args = get_innermost_template_args
15546 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
15547 else
15548 args = gen_args;
15550 if (TREE_CODE (d) == FUNCTION_DECL)
15551 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
15552 else
15553 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
15555 /* We may be in the middle of deferred access check. Disable it now. */
15556 push_deferring_access_checks (dk_no_deferred);
15558 /* Unless an explicit instantiation directive has already determined
15559 the linkage of D, remember that a definition is available for
15560 this entity. */
15561 if (pattern_defined
15562 && !DECL_INTERFACE_KNOWN (d)
15563 && !DECL_NOT_REALLY_EXTERN (d))
15564 mark_definable (d);
15566 input_location = DECL_SOURCE_LOCATION (d);
15568 /* If D is a member of an explicitly instantiated class template,
15569 and no definition is available, treat it like an implicit
15570 instantiation. */
15571 if (!pattern_defined && expl_inst_class_mem_p
15572 && DECL_EXPLICIT_INSTANTIATION (d))
15574 DECL_NOT_REALLY_EXTERN (d) = 0;
15575 DECL_INTERFACE_KNOWN (d) = 0;
15576 SET_DECL_IMPLICIT_INSTANTIATION (d);
15579 if (!defer_ok)
15581 /* Recheck the substitutions to obtain any warning messages
15582 about ignoring cv qualifiers. */
15583 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
15584 tree type = TREE_TYPE (gen);
15586 /* Make sure that we can see identifiers, and compute access
15587 correctly. D is already the target FUNCTION_DECL with the
15588 right context. */
15589 push_access_scope (d);
15591 if (TREE_CODE (gen) == FUNCTION_DECL)
15593 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
15594 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
15596 /* Don't simply tsubst the function type, as that will give
15597 duplicate warnings about poor parameter qualifications.
15598 The function arguments are the same as the decl_arguments
15599 without the top level cv qualifiers. */
15600 type = TREE_TYPE (type);
15602 tsubst (type, gen_args, tf_warning_or_error, d);
15604 pop_access_scope (d);
15607 /* Check to see whether we know that this template will be
15608 instantiated in some other file, as with "extern template"
15609 extension. */
15610 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15611 /* In general, we do not instantiate such templates... */
15612 if (external_p
15613 /* ... but we instantiate inline functions so that we can inline
15614 them. An explicit instantiation declaration prohibits implicit
15615 instantiation of non-inline functions. With high levels of
15616 optimization, we would normally inline non-inline functions
15617 -- but we're not allowed to do that for "extern template" functions.
15618 Therefore, we check DECL_DECLARED_INLINE_P, rather than
15619 possibly_inlined_p. And ... */
15620 && ! (TREE_CODE (d) == FUNCTION_DECL
15621 && DECL_DECLARED_INLINE_P (d))
15622 /* ... we instantiate static data members whose values are
15623 needed in integral constant expressions. */
15624 && ! (TREE_CODE (d) == VAR_DECL
15625 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
15626 goto out;
15627 /* Defer all other templates, unless we have been explicitly
15628 forbidden from doing so. */
15629 if (/* If there is no definition, we cannot instantiate the
15630 template. */
15631 ! pattern_defined
15632 /* If it's OK to postpone instantiation, do so. */
15633 || defer_ok
15634 /* If this is a static data member that will be defined
15635 elsewhere, we don't want to instantiate the entire data
15636 member, but we do want to instantiate the initializer so that
15637 we can substitute that elsewhere. */
15638 || (external_p && TREE_CODE (d) == VAR_DECL))
15640 /* The definition of the static data member is now required so
15641 we must substitute the initializer. */
15642 if (TREE_CODE (d) == VAR_DECL
15643 && !DECL_INITIAL (d)
15644 && DECL_INITIAL (code_pattern))
15646 tree ns;
15647 tree init;
15649 ns = decl_namespace_context (d);
15650 push_nested_namespace (ns);
15651 push_nested_class (DECL_CONTEXT (d));
15652 init = tsubst_expr (DECL_INITIAL (code_pattern),
15653 args,
15654 tf_warning_or_error, NULL_TREE,
15655 /*integral_constant_expression_p=*/false);
15656 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
15657 /*asmspec_tree=*/NULL_TREE,
15658 LOOKUP_ONLYCONVERTING);
15659 pop_nested_class ();
15660 pop_nested_namespace (ns);
15663 /* We restore the source position here because it's used by
15664 add_pending_template. */
15665 input_location = saved_loc;
15667 if (at_eof && !pattern_defined
15668 && DECL_EXPLICIT_INSTANTIATION (d)
15669 && DECL_NOT_REALLY_EXTERN (d))
15670 /* [temp.explicit]
15672 The definition of a non-exported function template, a
15673 non-exported member function template, or a non-exported
15674 member function or static data member of a class template
15675 shall be present in every translation unit in which it is
15676 explicitly instantiated. */
15677 permerror (input_location, "explicit instantiation of %qD "
15678 "but no definition available", d);
15680 /* ??? Historically, we have instantiated inline functions, even
15681 when marked as "extern template". */
15682 if (!(external_p && TREE_CODE (d) == VAR_DECL))
15683 add_pending_template (d);
15684 goto out;
15686 /* Tell the repository that D is available in this translation unit
15687 -- and see if it is supposed to be instantiated here. */
15688 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
15690 /* In a PCH file, despite the fact that the repository hasn't
15691 requested instantiation in the PCH it is still possible that
15692 an instantiation will be required in a file that includes the
15693 PCH. */
15694 if (pch_file)
15695 add_pending_template (d);
15696 /* Instantiate inline functions so that the inliner can do its
15697 job, even though we'll not be emitting a copy of this
15698 function. */
15699 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
15700 goto out;
15703 need_push = !cfun || !global_bindings_p ();
15704 if (need_push)
15705 push_to_top_level ();
15707 /* Mark D as instantiated so that recursive calls to
15708 instantiate_decl do not try to instantiate it again. */
15709 DECL_TEMPLATE_INSTANTIATED (d) = 1;
15711 /* Regenerate the declaration in case the template has been modified
15712 by a subsequent redeclaration. */
15713 regenerate_decl_from_template (d, td);
15715 /* We already set the file and line above. Reset them now in case
15716 they changed as a result of calling regenerate_decl_from_template. */
15717 input_location = DECL_SOURCE_LOCATION (d);
15719 if (TREE_CODE (d) == VAR_DECL)
15721 tree init;
15723 /* Clear out DECL_RTL; whatever was there before may not be right
15724 since we've reset the type of the declaration. */
15725 SET_DECL_RTL (d, NULL_RTX);
15726 DECL_IN_AGGR_P (d) = 0;
15728 /* The initializer is placed in DECL_INITIAL by
15729 regenerate_decl_from_template. Pull it out so that
15730 finish_decl can process it. */
15731 init = DECL_INITIAL (d);
15732 DECL_INITIAL (d) = NULL_TREE;
15733 DECL_INITIALIZED_P (d) = 0;
15735 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
15736 initializer. That function will defer actual emission until
15737 we have a chance to determine linkage. */
15738 DECL_EXTERNAL (d) = 0;
15740 /* Enter the scope of D so that access-checking works correctly. */
15741 push_nested_class (DECL_CONTEXT (d));
15742 finish_decl (d, init, NULL_TREE, NULL_TREE);
15743 pop_nested_class ();
15745 else if (TREE_CODE (d) == FUNCTION_DECL)
15747 htab_t saved_local_specializations;
15748 tree subst_decl;
15749 tree tmpl_parm;
15750 tree spec_parm;
15752 /* Save away the current list, in case we are instantiating one
15753 template from within the body of another. */
15754 saved_local_specializations = local_specializations;
15756 /* Set up the list of local specializations. */
15757 local_specializations = htab_create (37,
15758 hash_local_specialization,
15759 eq_local_specializations,
15760 NULL);
15762 /* Set up context. */
15763 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
15765 /* Create substitution entries for the parameters. */
15766 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
15767 tmpl_parm = DECL_ARGUMENTS (subst_decl);
15768 spec_parm = DECL_ARGUMENTS (d);
15769 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
15771 register_local_specialization (spec_parm, tmpl_parm);
15772 spec_parm = skip_artificial_parms_for (d, spec_parm);
15773 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
15775 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15777 register_local_specialization (spec_parm, tmpl_parm);
15778 tmpl_parm = TREE_CHAIN (tmpl_parm);
15779 spec_parm = TREE_CHAIN (spec_parm);
15781 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15783 /* Register the (value) argument pack as a specialization of
15784 TMPL_PARM, then move on. */
15785 tree argpack = make_fnparm_pack (spec_parm);
15786 register_local_specialization (argpack, tmpl_parm);
15787 tmpl_parm = TREE_CHAIN (tmpl_parm);
15788 spec_parm = NULL_TREE;
15790 gcc_assert (!spec_parm);
15792 /* Substitute into the body of the function. */
15793 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
15794 tf_warning_or_error, tmpl,
15795 /*integral_constant_expression_p=*/false);
15797 /* Set the current input_location to the end of the function
15798 so that finish_function knows where we are. */
15799 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
15801 /* We don't need the local specializations any more. */
15802 htab_delete (local_specializations);
15803 local_specializations = saved_local_specializations;
15805 /* Finish the function. */
15806 d = finish_function (0);
15807 expand_or_defer_fn (d);
15810 /* We're not deferring instantiation any more. */
15811 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
15813 if (need_push)
15814 pop_from_top_level ();
15816 out:
15817 input_location = saved_loc;
15818 pop_deferring_access_checks ();
15819 pop_tinst_level ();
15821 timevar_pop (TV_PARSE);
15823 return d;
15826 /* Run through the list of templates that we wish we could
15827 instantiate, and instantiate any we can. RETRIES is the
15828 number of times we retry pending template instantiation. */
15830 void
15831 instantiate_pending_templates (int retries)
15833 int reconsider;
15834 location_t saved_loc = input_location;
15836 /* Instantiating templates may trigger vtable generation. This in turn
15837 may require further template instantiations. We place a limit here
15838 to avoid infinite loop. */
15839 if (pending_templates && retries >= max_tinst_depth)
15841 tree decl = pending_templates->tinst->decl;
15843 error ("template instantiation depth exceeds maximum of %d"
15844 " instantiating %q+D, possibly from virtual table generation"
15845 " (use -ftemplate-depth-NN to increase the maximum)",
15846 max_tinst_depth, decl);
15847 if (TREE_CODE (decl) == FUNCTION_DECL)
15848 /* Pretend that we defined it. */
15849 DECL_INITIAL (decl) = error_mark_node;
15850 return;
15855 struct pending_template **t = &pending_templates;
15856 struct pending_template *last = NULL;
15857 reconsider = 0;
15858 while (*t)
15860 tree instantiation = reopen_tinst_level ((*t)->tinst);
15861 bool complete = false;
15863 if (TYPE_P (instantiation))
15865 tree fn;
15867 if (!COMPLETE_TYPE_P (instantiation))
15869 instantiate_class_template (instantiation);
15870 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
15871 for (fn = TYPE_METHODS (instantiation);
15873 fn = TREE_CHAIN (fn))
15874 if (! DECL_ARTIFICIAL (fn))
15875 instantiate_decl (fn,
15876 /*defer_ok=*/0,
15877 /*expl_inst_class_mem_p=*/false);
15878 if (COMPLETE_TYPE_P (instantiation))
15879 reconsider = 1;
15882 complete = COMPLETE_TYPE_P (instantiation);
15884 else
15886 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
15887 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
15889 instantiation
15890 = instantiate_decl (instantiation,
15891 /*defer_ok=*/0,
15892 /*expl_inst_class_mem_p=*/false);
15893 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
15894 reconsider = 1;
15897 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
15898 || DECL_TEMPLATE_INSTANTIATED (instantiation));
15901 if (complete)
15902 /* If INSTANTIATION has been instantiated, then we don't
15903 need to consider it again in the future. */
15904 *t = (*t)->next;
15905 else
15907 last = *t;
15908 t = &(*t)->next;
15910 tinst_depth = 0;
15911 current_tinst_level = NULL;
15913 last_pending_template = last;
15915 while (reconsider);
15917 input_location = saved_loc;
15920 /* Substitute ARGVEC into T, which is a list of initializers for
15921 either base class or a non-static data member. The TREE_PURPOSEs
15922 are DECLs, and the TREE_VALUEs are the initializer values. Used by
15923 instantiate_decl. */
15925 static tree
15926 tsubst_initializer_list (tree t, tree argvec)
15928 tree inits = NULL_TREE;
15930 for (; t; t = TREE_CHAIN (t))
15932 tree decl;
15933 tree init;
15934 tree expanded_bases = NULL_TREE;
15935 tree expanded_arguments = NULL_TREE;
15936 int i, len = 1;
15938 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
15940 tree expr;
15941 tree arg;
15943 /* Expand the base class expansion type into separate base
15944 classes. */
15945 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
15946 tf_warning_or_error,
15947 NULL_TREE);
15948 if (expanded_bases == error_mark_node)
15949 continue;
15951 /* We'll be building separate TREE_LISTs of arguments for
15952 each base. */
15953 len = TREE_VEC_LENGTH (expanded_bases);
15954 expanded_arguments = make_tree_vec (len);
15955 for (i = 0; i < len; i++)
15956 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
15958 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
15959 expand each argument in the TREE_VALUE of t. */
15960 expr = make_node (EXPR_PACK_EXPANSION);
15961 PACK_EXPANSION_PARAMETER_PACKS (expr) =
15962 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
15964 if (TREE_VALUE (t) == void_type_node)
15965 /* VOID_TYPE_NODE is used to indicate
15966 value-initialization. */
15968 for (i = 0; i < len; i++)
15969 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
15971 else
15973 /* Substitute parameter packs into each argument in the
15974 TREE_LIST. */
15975 in_base_initializer = 1;
15976 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
15978 tree expanded_exprs;
15980 /* Expand the argument. */
15981 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
15982 expanded_exprs
15983 = tsubst_pack_expansion (expr, argvec,
15984 tf_warning_or_error,
15985 NULL_TREE);
15986 if (expanded_exprs == error_mark_node)
15987 continue;
15989 /* Prepend each of the expanded expressions to the
15990 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
15991 for (i = 0; i < len; i++)
15993 TREE_VEC_ELT (expanded_arguments, i) =
15994 tree_cons (NULL_TREE,
15995 TREE_VEC_ELT (expanded_exprs, i),
15996 TREE_VEC_ELT (expanded_arguments, i));
15999 in_base_initializer = 0;
16001 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16002 since we built them backwards. */
16003 for (i = 0; i < len; i++)
16005 TREE_VEC_ELT (expanded_arguments, i) =
16006 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16011 for (i = 0; i < len; ++i)
16013 if (expanded_bases)
16015 decl = TREE_VEC_ELT (expanded_bases, i);
16016 decl = expand_member_init (decl);
16017 init = TREE_VEC_ELT (expanded_arguments, i);
16019 else
16021 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16022 tf_warning_or_error, NULL_TREE);
16024 decl = expand_member_init (decl);
16025 if (decl && !DECL_P (decl))
16026 in_base_initializer = 1;
16028 init = tsubst_expr (TREE_VALUE (t), argvec,
16029 tf_warning_or_error, NULL_TREE,
16030 /*integral_constant_expression_p=*/false);
16031 in_base_initializer = 0;
16034 if (decl)
16036 init = build_tree_list (decl, init);
16037 TREE_CHAIN (init) = inits;
16038 inits = init;
16042 return inits;
16045 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16047 static void
16048 set_current_access_from_decl (tree decl)
16050 if (TREE_PRIVATE (decl))
16051 current_access_specifier = access_private_node;
16052 else if (TREE_PROTECTED (decl))
16053 current_access_specifier = access_protected_node;
16054 else
16055 current_access_specifier = access_public_node;
16058 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16059 is the instantiation (which should have been created with
16060 start_enum) and ARGS are the template arguments to use. */
16062 static void
16063 tsubst_enum (tree tag, tree newtag, tree args)
16065 tree e;
16067 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16069 tree value;
16070 tree decl;
16072 decl = TREE_VALUE (e);
16073 /* Note that in a template enum, the TREE_VALUE is the
16074 CONST_DECL, not the corresponding INTEGER_CST. */
16075 value = tsubst_expr (DECL_INITIAL (decl),
16076 args, tf_warning_or_error, NULL_TREE,
16077 /*integral_constant_expression_p=*/true);
16079 /* Give this enumeration constant the correct access. */
16080 set_current_access_from_decl (decl);
16082 /* Actually build the enumerator itself. */
16083 build_enumerator (DECL_NAME (decl), value, newtag);
16086 finish_enum (newtag);
16087 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16088 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16091 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16092 its type -- but without substituting the innermost set of template
16093 arguments. So, innermost set of template parameters will appear in
16094 the type. */
16096 tree
16097 get_mostly_instantiated_function_type (tree decl)
16099 tree fn_type;
16100 tree tmpl;
16101 tree targs;
16102 tree tparms;
16103 int parm_depth;
16105 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16106 targs = DECL_TI_ARGS (decl);
16107 tparms = DECL_TEMPLATE_PARMS (tmpl);
16108 parm_depth = TMPL_PARMS_DEPTH (tparms);
16110 /* There should be as many levels of arguments as there are levels
16111 of parameters. */
16112 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16114 fn_type = TREE_TYPE (tmpl);
16116 if (parm_depth == 1)
16117 /* No substitution is necessary. */
16119 else
16121 int i, save_access_control;
16122 tree partial_args;
16124 /* Replace the innermost level of the TARGS with NULL_TREEs to
16125 let tsubst know not to substitute for those parameters. */
16126 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16127 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16128 SET_TMPL_ARGS_LEVEL (partial_args, i,
16129 TMPL_ARGS_LEVEL (targs, i));
16130 SET_TMPL_ARGS_LEVEL (partial_args,
16131 TMPL_ARGS_DEPTH (targs),
16132 make_tree_vec (DECL_NTPARMS (tmpl)));
16134 /* Disable access control as this function is used only during
16135 name-mangling. */
16136 save_access_control = flag_access_control;
16137 flag_access_control = 0;
16139 ++processing_template_decl;
16140 /* Now, do the (partial) substitution to figure out the
16141 appropriate function type. */
16142 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16143 --processing_template_decl;
16145 /* Substitute into the template parameters to obtain the real
16146 innermost set of parameters. This step is important if the
16147 innermost set of template parameters contains value
16148 parameters whose types depend on outer template parameters. */
16149 TREE_VEC_LENGTH (partial_args)--;
16150 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16152 flag_access_control = save_access_control;
16155 return fn_type;
16158 /* Return truthvalue if we're processing a template different from
16159 the last one involved in diagnostics. */
16161 problematic_instantiation_changed (void)
16163 return last_template_error_tick != tinst_level_tick;
16166 /* Remember current template involved in diagnostics. */
16167 void
16168 record_last_problematic_instantiation (void)
16170 last_template_error_tick = tinst_level_tick;
16173 struct tinst_level *
16174 current_instantiation (void)
16176 return current_tinst_level;
16179 /* [temp.param] Check that template non-type parm TYPE is of an allowable
16180 type. Return zero for ok, nonzero for disallowed. Issue error and
16181 warning messages under control of COMPLAIN. */
16183 static int
16184 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
16186 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
16187 return 0;
16188 else if (POINTER_TYPE_P (type))
16189 return 0;
16190 else if (TYPE_PTR_TO_MEMBER_P (type))
16191 return 0;
16192 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16193 return 0;
16194 else if (TREE_CODE (type) == TYPENAME_TYPE)
16195 return 0;
16197 if (complain & tf_error)
16198 error ("%q#T is not a valid type for a template constant parameter", type);
16199 return 1;
16202 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16203 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16205 static bool
16206 dependent_type_p_r (tree type)
16208 tree scope;
16210 /* [temp.dep.type]
16212 A type is dependent if it is:
16214 -- a template parameter. Template template parameters are types
16215 for us (since TYPE_P holds true for them) so we handle
16216 them here. */
16217 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16218 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
16219 return true;
16220 /* -- a qualified-id with a nested-name-specifier which contains a
16221 class-name that names a dependent type or whose unqualified-id
16222 names a dependent type. */
16223 if (TREE_CODE (type) == TYPENAME_TYPE)
16224 return true;
16225 /* -- a cv-qualified type where the cv-unqualified type is
16226 dependent. */
16227 type = TYPE_MAIN_VARIANT (type);
16228 /* -- a compound type constructed from any dependent type. */
16229 if (TYPE_PTR_TO_MEMBER_P (type))
16230 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
16231 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16232 (type)));
16233 else if (TREE_CODE (type) == POINTER_TYPE
16234 || TREE_CODE (type) == REFERENCE_TYPE)
16235 return dependent_type_p (TREE_TYPE (type));
16236 else if (TREE_CODE (type) == FUNCTION_TYPE
16237 || TREE_CODE (type) == METHOD_TYPE)
16239 tree arg_type;
16241 if (dependent_type_p (TREE_TYPE (type)))
16242 return true;
16243 for (arg_type = TYPE_ARG_TYPES (type);
16244 arg_type;
16245 arg_type = TREE_CHAIN (arg_type))
16246 if (dependent_type_p (TREE_VALUE (arg_type)))
16247 return true;
16248 return false;
16250 /* -- an array type constructed from any dependent type or whose
16251 size is specified by a constant expression that is
16252 value-dependent. */
16253 if (TREE_CODE (type) == ARRAY_TYPE)
16255 if (TYPE_DOMAIN (type)
16256 && dependent_type_p (TYPE_DOMAIN (type)))
16257 return true;
16258 return dependent_type_p (TREE_TYPE (type));
16260 else if (TREE_CODE (type) == INTEGER_TYPE
16261 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16263 /* If this is the TYPE_DOMAIN of an array type, consider it
16264 dependent. We already checked for value-dependence in
16265 compute_array_index_type. */
16266 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
16269 /* -- a template-id in which either the template name is a template
16270 parameter ... */
16271 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16272 return true;
16273 /* ... or any of the template arguments is a dependent type or
16274 an expression that is type-dependent or value-dependent. */
16275 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
16276 && (any_dependent_template_arguments_p
16277 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
16278 return true;
16280 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16281 argument of the `typeof' expression is not type-dependent, then
16282 it should already been have resolved. */
16283 if (TREE_CODE (type) == TYPEOF_TYPE
16284 || TREE_CODE (type) == DECLTYPE_TYPE)
16285 return true;
16287 /* A template argument pack is dependent if any of its packed
16288 arguments are. */
16289 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16291 tree args = ARGUMENT_PACK_ARGS (type);
16292 int i, len = TREE_VEC_LENGTH (args);
16293 for (i = 0; i < len; ++i)
16294 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16295 return true;
16298 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16299 be template parameters. */
16300 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16301 return true;
16303 /* The standard does not specifically mention types that are local
16304 to template functions or local classes, but they should be
16305 considered dependent too. For example:
16307 template <int I> void f() {
16308 enum E { a = I };
16309 S<sizeof (E)> s;
16312 The size of `E' cannot be known until the value of `I' has been
16313 determined. Therefore, `E' must be considered dependent. */
16314 scope = TYPE_CONTEXT (type);
16315 if (scope && TYPE_P (scope))
16316 return dependent_type_p (scope);
16317 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16318 return type_dependent_expression_p (scope);
16320 /* Other types are non-dependent. */
16321 return false;
16324 /* Returns TRUE if TYPE is dependent, in the sense of
16325 [temp.dep.type]. */
16327 bool
16328 dependent_type_p (tree type)
16330 /* If there are no template parameters in scope, then there can't be
16331 any dependent types. */
16332 if (!processing_template_decl)
16334 /* If we are not processing a template, then nobody should be
16335 providing us with a dependent type. */
16336 gcc_assert (type);
16337 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
16338 return false;
16341 /* If the type is NULL, we have not computed a type for the entity
16342 in question; in that case, the type is dependent. */
16343 if (!type)
16344 return true;
16346 /* Erroneous types can be considered non-dependent. */
16347 if (type == error_mark_node)
16348 return false;
16350 /* If we have not already computed the appropriate value for TYPE,
16351 do so now. */
16352 if (!TYPE_DEPENDENT_P_VALID (type))
16354 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16355 TYPE_DEPENDENT_P_VALID (type) = 1;
16358 return TYPE_DEPENDENT_P (type);
16361 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16362 lookup. In other words, a dependent type that is not the current
16363 instantiation. */
16365 bool
16366 dependent_scope_p (tree scope)
16368 return (scope && TYPE_P (scope) && dependent_type_p (scope)
16369 && !currently_open_class (scope));
16372 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16374 static bool
16375 dependent_scope_ref_p (tree expression, bool criterion (tree))
16377 tree scope;
16378 tree name;
16380 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
16382 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16383 return true;
16385 scope = TREE_OPERAND (expression, 0);
16386 name = TREE_OPERAND (expression, 1);
16388 /* [temp.dep.expr]
16390 An id-expression is type-dependent if it contains a
16391 nested-name-specifier that contains a class-name that names a
16392 dependent type. */
16393 /* The suggested resolution to Core Issue 224 implies that if the
16394 qualifying type is the current class, then we must peek
16395 inside it. */
16396 if (DECL_P (name)
16397 && currently_open_class (scope)
16398 && !criterion (name))
16399 return false;
16400 if (dependent_type_p (scope))
16401 return true;
16403 return false;
16406 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16407 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16408 expression. */
16410 bool
16411 value_dependent_expression_p (tree expression)
16413 if (!processing_template_decl)
16414 return false;
16416 /* A name declared with a dependent type. */
16417 if (DECL_P (expression) && type_dependent_expression_p (expression))
16418 return true;
16420 switch (TREE_CODE (expression))
16422 case IDENTIFIER_NODE:
16423 /* A name that has not been looked up -- must be dependent. */
16424 return true;
16426 case TEMPLATE_PARM_INDEX:
16427 /* A non-type template parm. */
16428 return true;
16430 case CONST_DECL:
16431 /* A non-type template parm. */
16432 if (DECL_TEMPLATE_PARM_P (expression))
16433 return true;
16434 return value_dependent_expression_p (DECL_INITIAL (expression));
16436 case VAR_DECL:
16437 /* A constant with integral or enumeration type and is initialized
16438 with an expression that is value-dependent. */
16439 if (DECL_INITIAL (expression)
16440 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16441 && value_dependent_expression_p (DECL_INITIAL (expression)))
16442 return true;
16443 return false;
16445 case DYNAMIC_CAST_EXPR:
16446 case STATIC_CAST_EXPR:
16447 case CONST_CAST_EXPR:
16448 case REINTERPRET_CAST_EXPR:
16449 case CAST_EXPR:
16450 /* These expressions are value-dependent if the type to which
16451 the cast occurs is dependent or the expression being casted
16452 is value-dependent. */
16454 tree type = TREE_TYPE (expression);
16456 if (dependent_type_p (type))
16457 return true;
16459 /* A functional cast has a list of operands. */
16460 expression = TREE_OPERAND (expression, 0);
16461 if (!expression)
16463 /* If there are no operands, it must be an expression such
16464 as "int()". This should not happen for aggregate types
16465 because it would form non-constant expressions. */
16466 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
16468 return false;
16471 if (TREE_CODE (expression) == TREE_LIST)
16472 return any_value_dependent_elements_p (expression);
16474 return value_dependent_expression_p (expression);
16477 case SIZEOF_EXPR:
16478 case ALIGNOF_EXPR:
16479 /* A `sizeof' expression is value-dependent if the operand is
16480 type-dependent or is a pack expansion. */
16481 expression = TREE_OPERAND (expression, 0);
16482 if (PACK_EXPANSION_P (expression))
16483 return true;
16484 else if (TYPE_P (expression))
16485 return dependent_type_p (expression);
16486 return type_dependent_expression_p (expression);
16488 case SCOPE_REF:
16489 return dependent_scope_ref_p (expression, value_dependent_expression_p);
16491 case COMPONENT_REF:
16492 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16493 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16495 case CALL_EXPR:
16496 /* A CALL_EXPR may appear in a constant expression if it is a
16497 call to a builtin function, e.g., __builtin_constant_p. All
16498 such calls are value-dependent. */
16499 return true;
16501 case NONTYPE_ARGUMENT_PACK:
16502 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16503 is value-dependent. */
16505 tree values = ARGUMENT_PACK_ARGS (expression);
16506 int i, len = TREE_VEC_LENGTH (values);
16508 for (i = 0; i < len; ++i)
16509 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16510 return true;
16512 return false;
16515 case TRAIT_EXPR:
16517 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16518 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16519 || (type2 ? dependent_type_p (type2) : false));
16522 case MODOP_EXPR:
16523 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16524 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16526 default:
16527 /* A constant expression is value-dependent if any subexpression is
16528 value-dependent. */
16529 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16531 case tcc_reference:
16532 case tcc_unary:
16533 return (value_dependent_expression_p
16534 (TREE_OPERAND (expression, 0)));
16536 case tcc_comparison:
16537 case tcc_binary:
16538 return ((value_dependent_expression_p
16539 (TREE_OPERAND (expression, 0)))
16540 || (value_dependent_expression_p
16541 (TREE_OPERAND (expression, 1))));
16543 case tcc_expression:
16544 case tcc_vl_exp:
16546 int i;
16547 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
16548 /* In some cases, some of the operands may be missing.
16549 (For example, in the case of PREDECREMENT_EXPR, the
16550 amount to increment by may be missing.) That doesn't
16551 make the expression dependent. */
16552 if (TREE_OPERAND (expression, i)
16553 && (value_dependent_expression_p
16554 (TREE_OPERAND (expression, i))))
16555 return true;
16556 return false;
16559 default:
16560 break;
16564 /* The expression is not value-dependent. */
16565 return false;
16568 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16569 [temp.dep.expr]. */
16571 bool
16572 type_dependent_expression_p (tree expression)
16574 if (!processing_template_decl)
16575 return false;
16577 if (expression == error_mark_node)
16578 return false;
16580 /* An unresolved name is always dependent. */
16581 if (TREE_CODE (expression) == IDENTIFIER_NODE
16582 || TREE_CODE (expression) == USING_DECL)
16583 return true;
16585 /* Some expression forms are never type-dependent. */
16586 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
16587 || TREE_CODE (expression) == SIZEOF_EXPR
16588 || TREE_CODE (expression) == ALIGNOF_EXPR
16589 || TREE_CODE (expression) == TRAIT_EXPR
16590 || TREE_CODE (expression) == TYPEID_EXPR
16591 || TREE_CODE (expression) == DELETE_EXPR
16592 || TREE_CODE (expression) == VEC_DELETE_EXPR
16593 || TREE_CODE (expression) == THROW_EXPR)
16594 return false;
16596 /* The types of these expressions depends only on the type to which
16597 the cast occurs. */
16598 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
16599 || TREE_CODE (expression) == STATIC_CAST_EXPR
16600 || TREE_CODE (expression) == CONST_CAST_EXPR
16601 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
16602 || TREE_CODE (expression) == CAST_EXPR)
16603 return dependent_type_p (TREE_TYPE (expression));
16605 /* The types of these expressions depends only on the type created
16606 by the expression. */
16607 if (TREE_CODE (expression) == NEW_EXPR
16608 || TREE_CODE (expression) == VEC_NEW_EXPR)
16610 /* For NEW_EXPR tree nodes created inside a template, either
16611 the object type itself or a TREE_LIST may appear as the
16612 operand 1. */
16613 tree type = TREE_OPERAND (expression, 1);
16614 if (TREE_CODE (type) == TREE_LIST)
16615 /* This is an array type. We need to check array dimensions
16616 as well. */
16617 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
16618 || value_dependent_expression_p
16619 (TREE_OPERAND (TREE_VALUE (type), 1));
16620 else
16621 return dependent_type_p (type);
16624 if (TREE_CODE (expression) == SCOPE_REF
16625 && dependent_scope_ref_p (expression,
16626 type_dependent_expression_p))
16627 return true;
16629 if (TREE_CODE (expression) == FUNCTION_DECL
16630 && DECL_LANG_SPECIFIC (expression)
16631 && DECL_TEMPLATE_INFO (expression)
16632 && (any_dependent_template_arguments_p
16633 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
16634 return true;
16636 if (TREE_CODE (expression) == TEMPLATE_DECL
16637 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
16638 return false;
16640 if (TREE_CODE (expression) == STMT_EXPR)
16641 expression = stmt_expr_value_expr (expression);
16643 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
16645 tree elt;
16646 unsigned i;
16648 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
16650 if (type_dependent_expression_p (elt))
16651 return true;
16653 return false;
16656 if (TREE_TYPE (expression) == unknown_type_node)
16658 if (TREE_CODE (expression) == ADDR_EXPR)
16659 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
16660 if (TREE_CODE (expression) == COMPONENT_REF
16661 || TREE_CODE (expression) == OFFSET_REF)
16663 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
16664 return true;
16665 expression = TREE_OPERAND (expression, 1);
16666 if (TREE_CODE (expression) == IDENTIFIER_NODE)
16667 return false;
16669 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
16670 if (TREE_CODE (expression) == SCOPE_REF)
16671 return false;
16673 if (TREE_CODE (expression) == BASELINK)
16674 expression = BASELINK_FUNCTIONS (expression);
16676 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
16678 if (any_dependent_template_arguments_p
16679 (TREE_OPERAND (expression, 1)))
16680 return true;
16681 expression = TREE_OPERAND (expression, 0);
16683 gcc_assert (TREE_CODE (expression) == OVERLOAD
16684 || TREE_CODE (expression) == FUNCTION_DECL);
16686 while (expression)
16688 if (type_dependent_expression_p (OVL_CURRENT (expression)))
16689 return true;
16690 expression = OVL_NEXT (expression);
16692 return false;
16695 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
16697 return (dependent_type_p (TREE_TYPE (expression)));
16700 /* Like type_dependent_expression_p, but it also works while not processing
16701 a template definition, i.e. during substitution or mangling. */
16703 bool
16704 type_dependent_expression_p_push (tree expr)
16706 bool b;
16707 ++processing_template_decl;
16708 b = type_dependent_expression_p (expr);
16709 --processing_template_decl;
16710 return b;
16713 /* Returns TRUE if ARGS contains a type-dependent expression. */
16715 bool
16716 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
16718 unsigned int i;
16719 tree arg;
16721 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
16723 if (type_dependent_expression_p (arg))
16724 return true;
16726 return false;
16729 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
16730 expressions) contains any value-dependent expressions. */
16732 bool
16733 any_value_dependent_elements_p (const_tree list)
16735 for (; list; list = TREE_CHAIN (list))
16736 if (value_dependent_expression_p (TREE_VALUE (list)))
16737 return true;
16739 return false;
16742 /* Returns TRUE if the ARG (a template argument) is dependent. */
16744 bool
16745 dependent_template_arg_p (tree arg)
16747 if (!processing_template_decl)
16748 return false;
16750 if (TREE_CODE (arg) == TEMPLATE_DECL
16751 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16752 return dependent_template_p (arg);
16753 else if (ARGUMENT_PACK_P (arg))
16755 tree args = ARGUMENT_PACK_ARGS (arg);
16756 int i, len = TREE_VEC_LENGTH (args);
16757 for (i = 0; i < len; ++i)
16759 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16760 return true;
16763 return false;
16765 else if (TYPE_P (arg))
16766 return dependent_type_p (arg);
16767 else
16768 return (type_dependent_expression_p (arg)
16769 || value_dependent_expression_p (arg));
16772 /* Returns true if ARGS (a collection of template arguments) contains
16773 any types that require structural equality testing. */
16775 bool
16776 any_template_arguments_need_structural_equality_p (tree args)
16778 int i;
16779 int j;
16781 if (!args)
16782 return false;
16783 if (args == error_mark_node)
16784 return true;
16786 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16788 tree level = TMPL_ARGS_LEVEL (args, i + 1);
16789 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16791 tree arg = TREE_VEC_ELT (level, j);
16792 tree packed_args = NULL_TREE;
16793 int k, len = 1;
16795 if (ARGUMENT_PACK_P (arg))
16797 /* Look inside the argument pack. */
16798 packed_args = ARGUMENT_PACK_ARGS (arg);
16799 len = TREE_VEC_LENGTH (packed_args);
16802 for (k = 0; k < len; ++k)
16804 if (packed_args)
16805 arg = TREE_VEC_ELT (packed_args, k);
16807 if (error_operand_p (arg))
16808 return true;
16809 else if (TREE_CODE (arg) == TEMPLATE_DECL
16810 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16811 continue;
16812 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
16813 return true;
16814 else if (!TYPE_P (arg) && TREE_TYPE (arg)
16815 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
16816 return true;
16821 return false;
16824 /* Returns true if ARGS (a collection of template arguments) contains
16825 any dependent arguments. */
16827 bool
16828 any_dependent_template_arguments_p (const_tree args)
16830 int i;
16831 int j;
16833 if (!args)
16834 return false;
16835 if (args == error_mark_node)
16836 return true;
16838 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16840 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
16841 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16842 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
16843 return true;
16846 return false;
16849 /* Returns TRUE if the template TMPL is dependent. */
16851 bool
16852 dependent_template_p (tree tmpl)
16854 if (TREE_CODE (tmpl) == OVERLOAD)
16856 while (tmpl)
16858 if (dependent_template_p (OVL_FUNCTION (tmpl)))
16859 return true;
16860 tmpl = OVL_CHAIN (tmpl);
16862 return false;
16865 /* Template template parameters are dependent. */
16866 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
16867 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16868 return true;
16869 /* So are names that have not been looked up. */
16870 if (TREE_CODE (tmpl) == SCOPE_REF
16871 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
16872 return true;
16873 /* So are member templates of dependent classes. */
16874 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
16875 return dependent_type_p (DECL_CONTEXT (tmpl));
16876 return false;
16879 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
16881 bool
16882 dependent_template_id_p (tree tmpl, tree args)
16884 return (dependent_template_p (tmpl)
16885 || any_dependent_template_arguments_p (args));
16888 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
16889 is dependent. */
16891 bool
16892 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
16894 int i;
16896 if (!processing_template_decl)
16897 return false;
16899 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
16901 tree decl = TREE_VEC_ELT (declv, i);
16902 tree init = TREE_VEC_ELT (initv, i);
16903 tree cond = TREE_VEC_ELT (condv, i);
16904 tree incr = TREE_VEC_ELT (incrv, i);
16906 if (type_dependent_expression_p (decl))
16907 return true;
16909 if (init && type_dependent_expression_p (init))
16910 return true;
16912 if (type_dependent_expression_p (cond))
16913 return true;
16915 if (COMPARISON_CLASS_P (cond)
16916 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
16917 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
16918 return true;
16920 if (TREE_CODE (incr) == MODOP_EXPR)
16922 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
16923 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
16924 return true;
16926 else if (type_dependent_expression_p (incr))
16927 return true;
16928 else if (TREE_CODE (incr) == MODIFY_EXPR)
16930 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
16931 return true;
16932 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
16934 tree t = TREE_OPERAND (incr, 1);
16935 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
16936 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
16937 return true;
16942 return false;
16945 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
16946 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
16947 no such TYPE can be found. Note that this function peers inside
16948 uninstantiated templates and therefore should be used only in
16949 extremely limited situations. ONLY_CURRENT_P restricts this
16950 peering to the currently open classes hierarchy (which is required
16951 when comparing types). */
16953 tree
16954 resolve_typename_type (tree type, bool only_current_p)
16956 tree scope;
16957 tree name;
16958 tree decl;
16959 int quals;
16960 tree pushed_scope;
16961 tree result;
16963 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
16965 scope = TYPE_CONTEXT (type);
16966 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
16967 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
16968 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
16969 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
16970 identifier of the TYPENAME_TYPE anymore.
16971 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
16972 TYPENAME_TYPE instead, we avoid messing up with a possible
16973 typedef variant case. */
16974 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
16976 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
16977 it first before we can figure out what NAME refers to. */
16978 if (TREE_CODE (scope) == TYPENAME_TYPE)
16979 scope = resolve_typename_type (scope, only_current_p);
16980 /* If we don't know what SCOPE refers to, then we cannot resolve the
16981 TYPENAME_TYPE. */
16982 if (TREE_CODE (scope) == TYPENAME_TYPE)
16983 return type;
16984 /* If the SCOPE is a template type parameter, we have no way of
16985 resolving the name. */
16986 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
16987 return type;
16988 /* If the SCOPE is not the current instantiation, there's no reason
16989 to look inside it. */
16990 if (only_current_p && !currently_open_class (scope))
16991 return type;
16992 /* If SCOPE isn't the template itself, it will not have a valid
16993 TYPE_FIELDS list. */
16994 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
16995 /* scope is either the template itself or a compatible instantiation
16996 like X<T>, so look up the name in the original template. */
16997 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
16998 else
16999 /* scope is a partial instantiation, so we can't do the lookup or we
17000 will lose the template arguments. */
17001 return type;
17002 /* Enter the SCOPE so that name lookup will be resolved as if we
17003 were in the class definition. In particular, SCOPE will no
17004 longer be considered a dependent type. */
17005 pushed_scope = push_scope (scope);
17006 /* Look up the declaration. */
17007 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17009 result = NULL_TREE;
17011 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17012 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17013 if (!decl)
17014 /*nop*/;
17015 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17016 && TREE_CODE (decl) == TYPE_DECL)
17018 result = TREE_TYPE (decl);
17019 if (result == error_mark_node)
17020 result = NULL_TREE;
17022 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17023 && DECL_CLASS_TEMPLATE_P (decl))
17025 tree tmpl;
17026 tree args;
17027 /* Obtain the template and the arguments. */
17028 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17029 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17030 /* Instantiate the template. */
17031 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17032 /*entering_scope=*/0,
17033 tf_error | tf_user);
17034 if (result == error_mark_node)
17035 result = NULL_TREE;
17038 /* Leave the SCOPE. */
17039 if (pushed_scope)
17040 pop_scope (pushed_scope);
17042 /* If we failed to resolve it, return the original typename. */
17043 if (!result)
17044 return type;
17046 /* If lookup found a typename type, resolve that too. */
17047 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17049 /* Ill-formed programs can cause infinite recursion here, so we
17050 must catch that. */
17051 TYPENAME_IS_RESOLVING_P (type) = 1;
17052 result = resolve_typename_type (result, only_current_p);
17053 TYPENAME_IS_RESOLVING_P (type) = 0;
17056 /* Qualify the resulting type. */
17057 quals = cp_type_quals (type);
17058 if (quals)
17059 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17061 return result;
17064 /* EXPR is an expression which is not type-dependent. Return a proxy
17065 for EXPR that can be used to compute the types of larger
17066 expressions containing EXPR. */
17068 tree
17069 build_non_dependent_expr (tree expr)
17071 tree inner_expr;
17073 /* Preserve null pointer constants so that the type of things like
17074 "p == 0" where "p" is a pointer can be determined. */
17075 if (null_ptr_cst_p (expr))
17076 return expr;
17077 /* Preserve OVERLOADs; the functions must be available to resolve
17078 types. */
17079 inner_expr = expr;
17080 if (TREE_CODE (inner_expr) == STMT_EXPR)
17081 inner_expr = stmt_expr_value_expr (inner_expr);
17082 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17083 inner_expr = TREE_OPERAND (inner_expr, 0);
17084 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17085 inner_expr = TREE_OPERAND (inner_expr, 1);
17086 if (is_overloaded_fn (inner_expr)
17087 || TREE_CODE (inner_expr) == OFFSET_REF)
17088 return expr;
17089 /* There is no need to return a proxy for a variable. */
17090 if (TREE_CODE (expr) == VAR_DECL)
17091 return expr;
17092 /* Preserve string constants; conversions from string constants to
17093 "char *" are allowed, even though normally a "const char *"
17094 cannot be used to initialize a "char *". */
17095 if (TREE_CODE (expr) == STRING_CST)
17096 return expr;
17097 /* Preserve arithmetic constants, as an optimization -- there is no
17098 reason to create a new node. */
17099 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17100 return expr;
17101 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17102 There is at least one place where we want to know that a
17103 particular expression is a throw-expression: when checking a ?:
17104 expression, there are special rules if the second or third
17105 argument is a throw-expression. */
17106 if (TREE_CODE (expr) == THROW_EXPR)
17107 return expr;
17109 if (TREE_CODE (expr) == COND_EXPR)
17110 return build3 (COND_EXPR,
17111 TREE_TYPE (expr),
17112 TREE_OPERAND (expr, 0),
17113 (TREE_OPERAND (expr, 1)
17114 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17115 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17116 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17117 if (TREE_CODE (expr) == COMPOUND_EXPR
17118 && !COMPOUND_EXPR_OVERLOADED (expr))
17119 return build2 (COMPOUND_EXPR,
17120 TREE_TYPE (expr),
17121 TREE_OPERAND (expr, 0),
17122 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17124 /* If the type is unknown, it can't really be non-dependent */
17125 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17127 /* Otherwise, build a NON_DEPENDENT_EXPR.
17129 REFERENCE_TYPEs are not stripped for expressions in templates
17130 because doing so would play havoc with mangling. Consider, for
17131 example:
17133 template <typename T> void f<T& g>() { g(); }
17135 In the body of "f", the expression for "g" will have
17136 REFERENCE_TYPE, even though the standard says that it should
17137 not. The reason is that we must preserve the syntactic form of
17138 the expression so that mangling (say) "f<g>" inside the body of
17139 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17140 stripped here. */
17141 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17144 /* ARGS is a vector of expressions as arguments to a function call.
17145 Replace the arguments with equivalent non-dependent expressions.
17146 This modifies ARGS in place. */
17148 void
17149 make_args_non_dependent (VEC(tree,gc) *args)
17151 unsigned int ix;
17152 tree arg;
17154 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
17156 tree newarg = build_non_dependent_expr (arg);
17157 if (newarg != arg)
17158 VEC_replace (tree, args, ix, newarg);
17162 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17163 with a level one deeper than the actual template parms. */
17165 tree
17166 make_auto (void)
17168 tree au;
17170 /* ??? Is it worth caching this for multiple autos at the same level? */
17171 au = cxx_make_type (TEMPLATE_TYPE_PARM);
17172 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
17173 TYPE_DECL, get_identifier ("auto"), au);
17174 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17175 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17176 (0, processing_template_decl + 1, processing_template_decl + 1,
17177 TYPE_NAME (au), NULL_TREE);
17178 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17179 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17180 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17182 return au;
17185 /* Replace auto in TYPE with std::initializer_list<auto>. */
17187 static tree
17188 listify_autos (tree type, tree auto_node)
17190 tree std_init_list = namespace_binding
17191 (get_identifier ("initializer_list"), std_node);
17192 tree argvec;
17193 tree init_auto;
17194 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17196 error ("deducing auto from brace-enclosed initializer list requires "
17197 "#include <initializer_list>");
17198 return error_mark_node;
17200 argvec = make_tree_vec (1);
17201 TREE_VEC_ELT (argvec, 0) = auto_node;
17202 init_auto = lookup_template_class (std_init_list, argvec, NULL_TREE,
17203 NULL_TREE, 0, tf_warning_or_error);
17205 TREE_VEC_ELT (argvec, 0) = init_auto;
17206 if (processing_template_decl)
17207 argvec = add_to_template_args (current_template_args (), argvec);
17208 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17211 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17212 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17214 tree
17215 do_auto_deduction (tree type, tree init, tree auto_node)
17217 tree parms, tparms, targs;
17218 tree args[1];
17219 int val;
17221 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17222 with either a new invented type template parameter U or, if the
17223 initializer is a braced-init-list (8.5.4), with
17224 std::initializer_list<U>. */
17225 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17226 type = listify_autos (type, auto_node);
17228 parms = build_tree_list (NULL_TREE, type);
17229 args[0] = init;
17230 tparms = make_tree_vec (1);
17231 targs = make_tree_vec (1);
17232 TREE_VEC_ELT (tparms, 0)
17233 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
17234 val = type_unification_real (tparms, targs, parms, args, 1, 0,
17235 DEDUCE_CALL, LOOKUP_NORMAL);
17236 if (val > 0)
17238 error ("unable to deduce %qT from %qE", type, init);
17239 return error_mark_node;
17242 if (processing_template_decl)
17243 targs = add_to_template_args (current_template_args (), targs);
17244 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17247 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17248 result. */
17250 tree
17251 splice_late_return_type (tree type, tree late_return_type)
17253 tree argvec;
17255 if (late_return_type == NULL_TREE)
17256 return type;
17257 argvec = make_tree_vec (1);
17258 TREE_VEC_ELT (argvec, 0) = late_return_type;
17259 if (processing_template_decl)
17260 argvec = add_to_template_args (current_template_args (), argvec);
17261 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17264 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17266 bool
17267 is_auto (const_tree type)
17269 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17270 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
17271 return true;
17272 else
17273 return false;
17276 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17277 appear as a type-specifier for the declaration in question, we don't
17278 have to look through the whole type. */
17280 tree
17281 type_uses_auto (tree type)
17283 enum tree_code code;
17284 if (is_auto (type))
17285 return type;
17287 code = TREE_CODE (type);
17289 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17290 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17291 || code == METHOD_TYPE || code == ARRAY_TYPE)
17292 return type_uses_auto (TREE_TYPE (type));
17294 if (TYPE_PTRMEMFUNC_P (type))
17295 return type_uses_auto (TREE_TYPE (TREE_TYPE
17296 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17298 return NULL_TREE;
17301 /* For a given template T, return the list of typedefs referenced
17302 in T for which access check is needed at T instantiation time.
17303 T is either a FUNCTION_DECL or a RECORD_TYPE.
17304 Those typedefs were added to T by the function
17305 append_type_to_template_for_access_check. */
17307 tree
17308 get_types_needing_access_check (tree t)
17310 tree ti, result = NULL_TREE;
17312 if (!t || t == error_mark_node)
17313 return t;
17315 if (!(ti = get_template_info (t)))
17316 return NULL_TREE;
17318 if (TREE_CODE (t) == RECORD_TYPE || TREE_CODE (t) == FUNCTION_DECL)
17320 if (!TI_TEMPLATE (ti))
17321 return NULL_TREE;
17323 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
17326 return result;
17329 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
17330 tied to T. That list of typedefs will be access checked at
17331 T instantiation time.
17332 T is either a FUNCTION_DECL or a RECORD_TYPE.
17333 TYPE_DECL is a TYPE_DECL node representing a typedef.
17334 SCOPE is the scope through which TYPE_DECL is accessed.
17336 This function is a subroutine of
17337 append_type_to_template_for_access_check. */
17339 static void
17340 append_type_to_template_for_access_check_1 (tree t,
17341 tree type_decl,
17342 tree scope)
17344 tree ti;
17346 if (!t || t == error_mark_node)
17347 return;
17349 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
17350 || TREE_CODE (t) == RECORD_TYPE)
17351 && type_decl
17352 && TREE_CODE (type_decl) == TYPE_DECL
17353 && scope);
17355 if (!(ti = get_template_info (t)))
17356 return;
17358 gcc_assert (TI_TEMPLATE (ti));
17360 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
17361 tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
17364 /* Append TYPE_DECL to the template TEMPL.
17365 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
17366 At TEMPL instanciation time, TYPE_DECL will be checked to see
17367 if it can be accessed through SCOPE.
17369 e.g. consider the following code snippet:
17371 class C
17373 typedef int myint;
17376 template<class U> struct S
17378 C::myint mi;
17381 S<char> s;
17383 At S<char> instantiation time, we need to check the access of C::myint
17384 In other words, we need to check the access of the myint typedef through
17385 the C scope. For that purpose, this function will add the myint typedef
17386 and the scope C through which its being accessed to a list of typedefs
17387 tied to the template S. That list will be walked at template instantiation
17388 time and access check performed on each typedefs it contains.
17389 Note that this particular code snippet should yield an error because
17390 myint is private to C. */
17392 void
17393 append_type_to_template_for_access_check (tree templ,
17394 tree type_decl,
17395 tree scope)
17397 tree node;
17399 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
17401 /* Make sure we don't append the type to the template twice. */
17402 for (node = get_types_needing_access_check (templ);
17403 node;
17404 node = TREE_CHAIN (node))
17406 tree decl = TREE_PURPOSE (node);
17407 tree type_scope = TREE_VALUE (node);
17409 if (decl == type_decl && type_scope == scope)
17410 return;
17413 append_type_to_template_for_access_check_1 (templ, type_decl, scope);
17416 #include "gt-cp-pt.h"